Function: rmail-sort-messages

rmail-sort-messages is a byte-compiled function defined in rmailsort.el.gz.

Signature

(rmail-sort-messages REVERSE KEYFUN)

Documentation

Sort messages of current Rmail buffer.

If REVERSE is non-nil, sorts in reverse order. Calls the function KEYFUN with a message number (it should return a sort key). Numeric keys are sorted numerically, all others as strings.

Source Code

;; Defined in /usr/src/emacs/lisp/mail/rmailsort.el.gz
(defun rmail-sort-messages (reverse keyfun)
  "Sort messages of current Rmail buffer.
If REVERSE is non-nil, sorts in reverse order.  Calls the
function KEYFUN with a message number (it should return a sort key).
Numeric keys are sorted numerically, all others as strings."
  (with-current-buffer rmail-buffer
    (let ((return-to-point
	   (if (rmail-buffers-swapped-p)
	       (point)))
	  (sort-lists nil))
      (rmail-swap-buffers-maybe)
      (message "Finding sort keys...")
      (widen)
      (let ((msgnum 1))
	(while (>= rmail-total-messages msgnum)
	  (setq sort-lists
		(cons (list (funcall keyfun msgnum) ;Make sorting key
			    (eq rmail-current-message msgnum) ;True if current
			    (aref rmail-message-vector msgnum)
			    (aref rmail-message-vector (1+ msgnum)))
		      sort-lists))
	  (if (zerop (% msgnum 10))
	      (message "Finding sort keys...%d" msgnum))
	  (setq msgnum (1+ msgnum))))
      (or reverse (setq sort-lists (nreverse sort-lists)))
      (setq sort-lists
	    (sort sort-lists
                  ;; Decide predicate: < or string-lessp
                  (if (numberp (car (car sort-lists))) ;Is a key numeric?
                      'car-less-than-car
		    (lambda (a b)
		      (string-lessp (car a) (car b))))))
      (if reverse (setq sort-lists (nreverse sort-lists)))
      ;; Now we enter critical region.  So, keyboard quit is disabled.
      (message "Reordering messages...")
      (let ((inhibit-quit t)		;Inhibit quit
	    (inhibit-read-only t)
	    (current-message nil)
	    (msgnum 1)
	    ;; (msginfo nil)
	    (undo (not (eq buffer-undo-list t))))
	;; There's little hope that we can easily undo after that.
	(buffer-disable-undo (current-buffer))
	(goto-char (rmail-msgbeg 1))
	;; To force update of all markers,
	;; keep the new copies separated from the remaining old messages.
	(insert-before-markers ?Z)
	(backward-char 1)
	;; Now reorder messages.
	(dolist (msginfo sort-lists)
	  ;; Swap two messages.
	  (insert-buffer-substring
	   (current-buffer) (nth 2 msginfo) (nth 3 msginfo))
	  ;; The last message may not have \n\n after it.
	  (rmail-ensure-blank-line)
	  (delete-region (nth 2 msginfo) (nth 3 msginfo))
	  ;; Is current message?
	  (if (nth 1 msginfo)
	      (setq current-message msgnum))
	  (if (zerop (% msgnum 10))
	      (message "Reordering messages...%d" msgnum))
	  (setq msgnum (1+ msgnum)))
	;; Delete the dummy separator Z inserted before.
	(delete-char 1)
	(setq quit-flag nil)
	;; If undo was on before, re-enable it.  But note that it is
	;; disabled in mbox Rmail, so this is kind of pointless.
	(if undo (buffer-enable-undo))
	(rmail-set-message-counters)
	(rmail-show-message-1 current-message)
	(if return-to-point
	    (goto-char return-to-point))
	(if (rmail-summary-exists)
	    (rmail-select-summary (rmail-update-summary)))))))