Function: rmail-sort-by-labels

rmail-sort-by-labels is an autoloaded, interactive and byte-compiled function defined in rmailsort.el.gz.

Signature

(rmail-sort-by-labels REVERSE LABELS)

Documentation

Sort messages of current Rmail buffer by labels.

LABELS is a comma-separated list of labels. The order of these labels specifies the order of messages: messages with the first label come first, messages with the second label come second, and so on. Messages that have none of these labels come last. If prefix argument REVERSE is non-nil, sorts in reverse order.

View in manual

Key Bindings

Source Code

;; Defined in /usr/src/emacs/lisp/mail/rmailsort.el.gz
;;;###autoload
(defun rmail-sort-by-labels (reverse labels)
  "Sort messages of current Rmail buffer by labels.
LABELS is a comma-separated list of labels.  The order of these
labels specifies the order of messages: messages with the first
label come first, messages with the second label come second, and
so on.  Messages that have none of these labels come last.
If prefix argument REVERSE is non-nil, sorts in reverse order."
  (interactive "P\nsSort by labels: ")
  (or (string-match "[^ \t]" labels)	; need some non-whitespace
      (error "No labels specified"))
  ;; Remove leading whitespace, add trailing comma.
  (setq labels (concat (substring labels (match-beginning 0)) ","))
  (let (labelvec nmax)
    ;; Convert "l1,..." into "\\(, \\|\\`\\)l1\\(,\\|\\'\\)" "..." ...
    (while (string-match "[ \t]*,[ \t]*" labels)
      (setq labelvec (cons
		      (concat "\\(, \\|\\`\\)"
			      (substring labels 0 (match-beginning 0))
			      "\\(,\\|\\'\\)")
		      labelvec))
      (setq labels (substring labels (match-end 0))))
    (setq labelvec (apply #'vector (nreverse labelvec))
	  nmax (length labelvec))
    (rmail-sort-messages reverse
			 ;; If no labels match, returns nmax; if they
			 ;; match the first specified in LABELS,
			 ;; returns 0; if they match the second, returns 1; etc.
			 ;; Hence sorts as described in the doc-string.
			 (lambda (msg)
			   (let ((n 0)
				 (str (concat (rmail-get-attr-names msg)
					      ", "
					      (rmail-get-keywords msg))))
			     ;; No labels: can't match anything.
			     (if (string-equal ", " str)
				 nmax
			       (while (and (< n nmax)
					   (not (string-match (aref labelvec n)
							      str)))
				 (setq n (1+ n)))
			       n))))))