                                  (copy-marker (cdr bounds)))))
               (acons 'bounds bounds err))))
          (errors   (mapcar err-point errors)))
-    (setq merlin-pending-errors errors)
-    (when merlin-error-in-fringe
-      (dolist (err errors)
-        (let* ((bounds (cdr (assoc 'bounds err)))
-               (overlay (make-overlay (car bounds) (cdr bounds))))
-          (overlay-put overlay 'merlin-kind 'error)
-          (overlay-put overlay 'merlin-pending-error err)
-          (push #'merlin--kill-error-if-edited
-                (overlay-get overlay 'modification-hooks))
+    (dolist (err errors)
+      (let* ((bounds (cdr (assoc 'bounds err)))
+             (overlay (make-overlay (car bounds) (cdr bounds))))
+        (overlay-put overlay 'merlin-kind 'error)
+        (overlay-put overlay 'merlin-pending-error err)
+        (push #'merlin--kill-error-if-edited
+              (overlay-get overlay 'modification-hooks))
+        (when merlin-error-in-fringe
           (if (merlin-error-warning-p (cdr (assoc 'message err)))
-	      (merlin-add-display-properties overlay
-					     'question-mark
-					     "?"
-					     'merlin-compilation-warning-face)
+              (merlin-add-display-properties overlay
+                                             'question-mark
+                                             "?"
+                                             'merlin-compilation-warning-face)
             (merlin-add-display-properties overlay
-					   'exclamation-mark
-					   "!"
-					   'merlin-compilation-error-face))
-          overlay)))))
+                                           'exclamation-mark
+                                           "!"
+                                           'merlin-compilation-error-face)))
+        overlay))))
 
 (defun merlin--error-check (view-errors-p)
   "Check for errors.

				     end
				     &optional length)
  "Remove an error from the pending error lists if it is edited by the user."
  (when is-after (merlin--clear-error-overlay overlay)))

(defun merlin-transform-display-errors (errors)
  "Populate the error list with ERRORS, transformed into an

  "Remove whitespace at the beginning and end of STR."
  (replace-regexp-in-string "^[[:space:]\n]\+\\|[[:space:]\n]\+$" "" str))

(defun merlin--error-position-delta (point err)
  "Distance between point and error."
  (setq err (cdr (assoc 'bounds err)))
  (cond ((< point (car err)) (cons (- (car err) point) 0))
        ((> point (cdr err)) (cons (- point (cdr err)) 0))
        (t (cons 0 (min (- (cdr err) point) (- point (car err)))))))

(defun merlin--error-at-position (point errors)
  "Returns error from ERRORS list most relevant at POINT"
  (let ((err nil) (d nil))
    (dolist (err- errors err)
      (let ((d- (merlin--error-position-delta (point) err-)))
        (when (or (not err) (< (car d-) (car d))
                  (and (= (car d-) (car d)) (< (cdr d-) (cdr d))))
          (setq d d-) (setq err err-))))))

(defun merlin-show-error-on-current-line ()
  "Show the error of the current line in the echo area.
  If there is no error, do nothing."
  (when (and merlin-mode (not (current-message)))
    (let* ((errors (overlays-in (line-beginning-position) (line-end-position)))
           (err nil))
      (setq errors (remove nil (mapcar 'merlin--overlay-pending-error errors)))
      (setq err (merlin--error-at-position (point) errors))
      (when err (message (merlin-chomp (cdr (assoc 'message err))))))))

(defun merlin--overlay-next-property-set (point prop &optional limit)
  "Find next point where PROP is set (like next-single-char-property-change but ensure that prop is not-nil)."
  (let ((point- (next-single-char-property-change point prop nil limit)))
    (if (eq point point-)
        (setq point (next-single-char-property-change (1+ point) prop nil limit))
      (setq point point-))
    (unless (find-if (lambda (a) (overlay-get a prop)) (overlays-at point))
      (setq point (next-single-char-property-change point prop nil limit)))
    point))

(defun merlin--overlay-previous-property-set (point prop &optional limit)
  "Find previous point where PROP is set (like previous-single-char-property-change but ensure that prop is not-nil)."
  (setq point (previous-single-char-property-change point prop nil limit))
  (unless (find-if (lambda (a) (overlay-get a prop)) (overlays-at point))
    (setq point (previous-single-char-property-change point prop nil limit)))
  point)

(defun merlin--error-prev-cycle ()
  "Returns previous error, cycling when reaching beginning of buffer"
  (let ((point (point)) (errors nil) (err nil))
    (setq point (merlin--overlay-previous-property-set point 'merlin-pending-error))
    (when (eq point (point-min))
      (setq point (merlin--overlay-previous-property-set (point-max) 'merlin-pending-error (point))))
    (setq errors (overlays-at point))
    (setq errors (remove nil (mapcar 'merlin--overlay-pending-error errors)))
    (setq err (merlin--error-at-position point errors))
    (if err (cons point err) nil)))

(defun merlin--error-next-cycle ()
  "Returns next error, cycling when reaching end of buffer"
  (let ((point (point)) (errors nil) (err nil))
    (setq point (merlin--overlay-next-property-set point 'merlin-pending-error))
    (when (eq point (point-max))
      (setq point (merlin--overlay-next-property-set (point-min) 'merlin-pending-error (point))))
    (setq errors (overlays-at point))
    (setq errors (remove nil (mapcar 'merlin--overlay-pending-error errors)))
    (setq err (merlin--error-at-position point errors))
    (if err (cons point err) nil)))

(defun merlin-error-prev ()
  "Jump back to previous error."
  (interactive)
  (let (err (merlin--error-prev-cycle))
    (unless err
      (merlin--acquire-buffer)
      (merlin--error-check nil)
      (setq err (merlin--error-prev-cycle)))
    (unless err (message "No more errors"))
    (when err
      (goto-char (car err))
      (message (merlin-chomp (cdr (assoc 'message (cdr err)))))
      (merlin-highlight (cdr (assoc 'bounds (cdr err))) 'next-error))))

(defun merlin-error-next ()
  "Jump to next error."
  (interactive)
  (let (err (merlin--error-next-cycle))
    (unless err
      (merlin--acquire-buffer)
      (merlin--error-check nil)
      (setq err (merlin--error-next-cycle)))
    (unless err (message "No more errors"))
    (when err
      (goto-char (car err))
      (message (merlin-chomp (cdr (assoc 'message (cdr err)))))
      (merlin-highlight (cdr (assoc 'bounds (cdr err))) 'next-error))))

(defun merlin-error-delete-overlays ()
  "Remove error overlays."
