+(defun rmail-next-error-move (msg-pos bad-marker)
+ "Move to an error locus (probably grep hit) in an Rmail buffer.
+MSG-POS is a marker pointing at the error message in the grep buffer.
+BAD-MARKER is a marker that ought to point at where to move to,
+but probably is garbage."
+
+ (let* ((message-loc (compilation--message->loc
+ (get-text-property msg-pos 'compilation-message
+ (marker-buffer msg-pos))))
+ (column (car message-loc))
+ (linenum (cadr message-loc))
+ line-text
+ pos
+ msgnum msgbeg msgend
+ header-field
+ line-number-within)
+
+ ;; Look at the whole Rmail file.
+ (rmail-swap-buffers-maybe)
+
+ (save-restriction
+ (widen)
+ (save-excursion
+ ;; Find the line that the error message points at.
+ (goto-char (point-min))
+ (forward-line (1- linenum))
+ (setq pos (point))
+
+ ;; Find the text at the start of the line,
+ ;; before the first = sign.
+ ;; This text has a good chance of being also in the
+ ;; decoded message.
+ (save-excursion
+ (skip-chars-forward "^=\n")
+ (setq line-text (buffer-substring pos (point))))
+
+ ;; Find which message this position is in,
+ ;; and the limits of that message.
+ (setq msgnum (rmail-what-message pos))
+ (setq msgbeg (rmail-msgbeg msgnum))
+ (setq msgend (rmail-msgend msgnum))
+
+ ;; Find which header this locus is in,
+ ;; or if it's in the message body,
+ ;; and the line-based position within that.
+ (goto-char msgbeg)
+ (let ((header-end msgend))
+ (if (search-forward "\n\n" nil t)
+ (setq header-end (point)))
+ (if (>= pos header-end)
+ (setq line-number-within
+ (count-lines header-end pos))
+ (goto-char pos)
+ (unless (looking-at "^[^ \t]")
+ (re-search-backward "^[^ \t]"))
+ (looking-at "[^:\n]*[:\n]")
+ (setq header-field (match-string 0)
+ line-number-within (count-lines (point) pos))))))
+
+ ;; Display the right message.
+ (rmail-show-message msgnum)
+
+ ;; Move to the right position within the displayed message.
+ ;; Or at least try. The decoded message's lines may not
+ ;; correspond to the lines in the inbox file.
+ (goto-char (point-min))
+ (if header-field
+ (progn
+ (re-search-forward (concat "^" (regexp-quote header-field)) nil t)
+ (forward-line line-number-within))
+ (search-forward "\n\n" nil t)
+ (if (re-search-forward (concat "^" (regexp-quote line-text)) nil t)
+ (goto-char (match-beginning 0))))
+ (if (eobp)
+ ;; If the decoded message doesn't have enough lines,
+ ;; go to the beginning rather than the end.
+ (goto-char (point-min))
+ ;; Otherwise, go to the right column.
+ (if column
+ (forward-char column)))))
+
+(defun rmail-what-message (&optional pos)
+ "Return message number POS (or point) is in."