Download Git

;;; Rmail's nice, but I'd prefer to have the headers shown in a predictable order. The code below does this.

(defvar rmail-sort-headers-headers 
  '("^Subject:" "^Cc:" "^To:" "^From:" "^Date:")
  "A list of regexps to match headers to search for, in the order
that they should appear into the buffer.")

;; Sort the headers in an Rmail buffer into a predictable order

(defun rmail-sort-headers ()
  (goto-char (point-min))
  (if (looking-at "^from ")		; Skip over mbox-format header
    ;; Set range of header to consider, and make the buffer non-read-only.
  (let ((start (point))
	(eoh (save-excursion (search-forward "\n\n" (point-max) t) (point)))
	(inhibit-read-only t))
     (lambda (header-re)
       ;; If we can find this header...
       (when (re-search-forward header-re eoh t)
	 ;; Grab it...
	 (let* ((beg (point-at-bol))
		(end (save-excursion 
		       (re-search-forward "^\\S-" eoh 'move)
		       (1- (point))))
		(header (buffer-substring beg end)))
	   ;; Delete it from the buffer...
	   (delete-region beg end)
	   (goto-char start)
	   ;; And insert it at the top.
	   (insert header)))
       (goto-char start))
     ;; The list of header regexps to search for, in reverse order that they
     ;; should appear into the buffer, as each is inserted before the next.
     (reverse rmail-sort-headers-headers))))

(add-hook 'rmail-show-message-hook 'rmail-sort-headers)