2009-01-22 17:09:23 +00:00
|
|
|
|
;;; rmailsort.el --- Rmail: sort messages
|
|
|
|
|
|
2015-01-01 22:26:41 +00:00
|
|
|
|
;; Copyright (C) 1990, 1993-1994, 2001-2015 Free Software Foundation,
|
2013-01-01 09:11:05 +00:00
|
|
|
|
;; Inc.
|
2009-01-22 17:09:23 +00:00
|
|
|
|
|
|
|
|
|
;; Author: Masanobu UMEDA <umerin@mse.kyutech.ac.jp>
|
2014-02-10 01:34:22 +00:00
|
|
|
|
;; Maintainer: emacs-devel@gnu.org
|
2009-01-22 17:09:23 +00:00
|
|
|
|
;; Keywords: mail
|
2010-08-29 16:17:13 +00:00
|
|
|
|
;; Package: rmail
|
2009-01-22 17:09:23 +00:00
|
|
|
|
|
|
|
|
|
;; This file is part of GNU Emacs.
|
|
|
|
|
|
|
|
|
|
;; GNU Emacs is free software: you can redistribute it and/or modify
|
|
|
|
|
;; it under the terms of the GNU General Public License as published by
|
|
|
|
|
;; the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
;; (at your option) any later version.
|
|
|
|
|
|
|
|
|
|
;; GNU Emacs is distributed in the hope that it will be useful,
|
|
|
|
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
;; GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
;; You should have received a copy of the GNU General Public License
|
|
|
|
|
;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
|
2009-03-04 04:19:12 +00:00
|
|
|
|
;; Functions for sorting messages in an Rmail buffer.
|
|
|
|
|
|
2009-01-22 17:09:23 +00:00
|
|
|
|
;;; Code:
|
|
|
|
|
|
2009-02-10 03:36:00 +00:00
|
|
|
|
(require 'rmail)
|
2009-01-22 17:09:23 +00:00
|
|
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
|
(defun rmail-sort-by-date (reverse)
|
2009-03-04 04:19:12 +00:00
|
|
|
|
"Sort messages of current Rmail buffer by \"Date\" header.
|
|
|
|
|
If prefix argument REVERSE is non-nil, sorts in reverse order."
|
2009-01-22 17:09:23 +00:00
|
|
|
|
(interactive "P")
|
|
|
|
|
(rmail-sort-messages reverse
|
2009-03-04 04:19:12 +00:00
|
|
|
|
(lambda (msg)
|
|
|
|
|
(rmail-make-date-sortable
|
|
|
|
|
(rmail-get-header "Date" msg)))))
|
2009-01-22 17:09:23 +00:00
|
|
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
|
(defun rmail-sort-by-subject (reverse)
|
2009-03-04 04:19:12 +00:00
|
|
|
|
"Sort messages of current Rmail buffer by \"Subject\" header.
|
|
|
|
|
Ignores any \"Re: \" prefix. If prefix argument REVERSE is
|
|
|
|
|
non-nil, sorts in reverse order."
|
|
|
|
|
;; Note this is a case-sensitive sort.
|
2009-01-22 17:09:23 +00:00
|
|
|
|
(interactive "P")
|
|
|
|
|
(rmail-sort-messages reverse
|
2009-03-04 04:19:12 +00:00
|
|
|
|
(lambda (msg)
|
|
|
|
|
(let ((key (or (rmail-get-header "Subject" msg) ""))
|
|
|
|
|
(case-fold-search t))
|
|
|
|
|
;; Remove `Re:'
|
|
|
|
|
(if (string-match "^\\(re:[ \t]*\\)*" key)
|
|
|
|
|
(substring key (match-end 0))
|
|
|
|
|
key)))))
|
2009-01-22 17:09:23 +00:00
|
|
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
|
(defun rmail-sort-by-author (reverse)
|
2009-03-04 04:19:12 +00:00
|
|
|
|
"Sort messages of current Rmail buffer by author.
|
|
|
|
|
This uses either the \"From\" or \"Sender\" header, downcased.
|
|
|
|
|
If prefix argument REVERSE is non-nil, sorts in reverse order."
|
2009-01-22 17:09:23 +00:00
|
|
|
|
(interactive "P")
|
|
|
|
|
(rmail-sort-messages reverse
|
2009-03-04 04:19:12 +00:00
|
|
|
|
(lambda (msg)
|
|
|
|
|
(downcase ; canonical name
|
|
|
|
|
(mail-strip-quoted-names
|
|
|
|
|
(or (rmail-get-header "From" msg)
|
|
|
|
|
(rmail-get-header "Sender" msg) ""))))))
|
2009-01-22 17:09:23 +00:00
|
|
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
|
(defun rmail-sort-by-recipient (reverse)
|
2009-03-04 04:19:12 +00:00
|
|
|
|
"Sort messages of current Rmail buffer by recipient.
|
|
|
|
|
This uses either the \"To\" or \"Apparently-To\" header, downcased.
|
|
|
|
|
If prefix argument REVERSE is non-nil, sorts in reverse order."
|
2009-01-22 17:09:23 +00:00
|
|
|
|
(interactive "P")
|
|
|
|
|
(rmail-sort-messages reverse
|
2009-03-04 04:19:12 +00:00
|
|
|
|
(lambda (msg)
|
|
|
|
|
(downcase ; canonical name
|
|
|
|
|
(mail-strip-quoted-names
|
|
|
|
|
(or (rmail-get-header "To" msg)
|
|
|
|
|
(rmail-get-header "Apparently-To" msg) ""))))))
|
2009-01-22 17:09:23 +00:00
|
|
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
|
(defun rmail-sort-by-correspondent (reverse)
|
2009-03-04 04:19:12 +00:00
|
|
|
|
"Sort messages of current Rmail buffer by other correspondent.
|
|
|
|
|
This uses either the \"From\", \"Sender\", \"To\", or
|
|
|
|
|
\"Apparently-To\" header, downcased. Uses the first header not
|
2011-02-12 21:45:42 +00:00
|
|
|
|
excluded by `mail-dont-reply-to-names'. If prefix argument
|
2009-03-04 04:19:12 +00:00
|
|
|
|
REVERSE is non-nil, sorts in reverse order."
|
2009-01-22 17:09:23 +00:00
|
|
|
|
(interactive "P")
|
|
|
|
|
(rmail-sort-messages reverse
|
2009-03-04 04:19:12 +00:00
|
|
|
|
(lambda (msg)
|
|
|
|
|
(downcase
|
2009-01-22 17:09:23 +00:00
|
|
|
|
(rmail-select-correspondent
|
|
|
|
|
msg
|
|
|
|
|
'("From" "Sender" "To" "Apparently-To"))))))
|
|
|
|
|
|
|
|
|
|
(defun rmail-select-correspondent (msg fields)
|
2011-02-12 21:45:42 +00:00
|
|
|
|
"Find the first header not excluded by `mail-dont-reply-to-names'.
|
2009-03-04 04:19:12 +00:00
|
|
|
|
MSG is a message number. FIELDS is a list of header names."
|
2009-01-22 17:09:23 +00:00
|
|
|
|
(let ((ans ""))
|
|
|
|
|
(while (and fields (string= ans ""))
|
|
|
|
|
(setq ans
|
2011-02-12 21:45:42 +00:00
|
|
|
|
(mail-dont-reply-to
|
2009-01-22 17:09:23 +00:00
|
|
|
|
(mail-strip-quoted-names
|
|
|
|
|
(or (rmail-get-header (car fields) msg) ""))))
|
|
|
|
|
(setq fields (cdr fields)))
|
|
|
|
|
ans))
|
|
|
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
|
(defun rmail-sort-by-lines (reverse)
|
2009-03-04 04:19:12 +00:00
|
|
|
|
"Sort messages of current Rmail buffer by the number of lines.
|
|
|
|
|
If prefix argument REVERSE is non-nil, sorts in reverse order."
|
2009-01-22 17:09:23 +00:00
|
|
|
|
(interactive "P")
|
|
|
|
|
(rmail-sort-messages reverse
|
2009-03-04 04:19:12 +00:00
|
|
|
|
(lambda (msg)
|
|
|
|
|
(count-lines (rmail-msgbeg msg)
|
|
|
|
|
(rmail-msgend msg)))))
|
2009-01-22 17:09:23 +00:00
|
|
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
|
(defun rmail-sort-by-labels (reverse labels)
|
2009-03-04 04:19:12 +00:00
|
|
|
|
"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."
|
2009-01-22 17:09:23 +00:00
|
|
|
|
(interactive "P\nsSort by labels: ")
|
2009-03-04 04:19:12 +00:00
|
|
|
|
(or (string-match "[^ \t]" labels) ; need some non-whitespace
|
2009-01-22 17:09:23 +00:00
|
|
|
|
(error "No labels specified"))
|
2009-03-04 04:19:12 +00:00
|
|
|
|
;; Remove leading whitespace, add trailing comma.
|
2009-01-22 17:09:23 +00:00
|
|
|
|
(setq labels (concat (substring labels (match-beginning 0)) ","))
|
2009-03-04 04:19:12 +00:00
|
|
|
|
(let (labelvec nmax)
|
|
|
|
|
;; Convert "l1,..." into "\\(, \\|\\`\\)l1\\(,\\|\\'\\)" "..." ...
|
2009-01-22 17:09:23 +00:00
|
|
|
|
(while (string-match "[ \t]*,[ \t]*" labels)
|
|
|
|
|
(setq labelvec (cons
|
2009-03-04 04:19:12 +00:00
|
|
|
|
(concat "\\(, \\|\\`\\)"
|
2009-01-22 17:09:23 +00:00
|
|
|
|
(substring labels 0 (match-beginning 0))
|
2009-03-04 04:19:12 +00:00
|
|
|
|
"\\(,\\|\\'\\)")
|
2009-01-22 17:09:23 +00:00
|
|
|
|
labelvec))
|
|
|
|
|
(setq labels (substring labels (match-end 0))))
|
2009-03-04 04:19:12 +00:00
|
|
|
|
(setq labelvec (apply 'vector (nreverse labelvec))
|
|
|
|
|
nmax (length labelvec))
|
2009-01-22 17:09:23 +00:00
|
|
|
|
(rmail-sort-messages reverse
|
2009-03-04 04:19:12 +00:00
|
|
|
|
;; 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))))))
|
2009-01-22 17:09:23 +00:00
|
|
|
|
|
|
|
|
|
;; Basic functions
|
2009-02-10 03:36:00 +00:00
|
|
|
|
(declare-function rmail-update-summary "rmailsum" (&rest ignore))
|
2009-01-22 17:09:23 +00:00
|
|
|
|
|
|
|
|
|
(defun rmail-sort-messages (reverse keyfun)
|
2009-03-04 04:19:12 +00:00
|
|
|
|
"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."
|
2009-01-22 17:09:23 +00:00
|
|
|
|
(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
|
2009-02-01 03:28:33 +00:00
|
|
|
|
;; Decide predicate: < or string-lessp
|
|
|
|
|
(if (numberp (car (car sort-lists))) ;Is a key numeric?
|
|
|
|
|
'car-less-than-car
|
2009-03-04 04:19:12 +00:00
|
|
|
|
(lambda (a b)
|
|
|
|
|
(string-lessp (car a) (car b))))))
|
2009-01-22 17:09:23 +00:00
|
|
|
|
(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)
|
2009-03-04 04:19:12 +00:00
|
|
|
|
(msginfo nil)
|
|
|
|
|
(undo (not (eq buffer-undo-list t))))
|
2009-01-22 17:09:23 +00:00
|
|
|
|
;; 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.
|
2009-03-05 03:44:04 +00:00
|
|
|
|
(rmail-ensure-blank-line)
|
2009-01-22 17:09:23 +00:00
|
|
|
|
(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)
|
2009-03-04 04:19:12 +00:00
|
|
|
|
;; 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))
|
2009-01-22 17:09:23 +00:00
|
|
|
|
(rmail-set-message-counters)
|
2009-02-13 07:44:18 +00:00
|
|
|
|
(rmail-show-message-1 current-message)
|
2009-01-22 17:09:23 +00:00
|
|
|
|
(if return-to-point
|
|
|
|
|
(goto-char return-to-point))
|
|
|
|
|
(if (rmail-summary-exists)
|
|
|
|
|
(rmail-select-summary (rmail-update-summary)))))))
|
|
|
|
|
|
2009-02-10 03:36:00 +00:00
|
|
|
|
(autoload 'timezone-make-date-sortable "timezone")
|
|
|
|
|
|
2009-01-22 17:09:23 +00:00
|
|
|
|
(defun rmail-make-date-sortable (date)
|
2009-03-04 04:19:12 +00:00
|
|
|
|
"Make DATE sortable using the function `string-lessp'."
|
2009-01-22 17:09:23 +00:00
|
|
|
|
;; Assume the default time zone is GMT.
|
|
|
|
|
(timezone-make-date-sortable date "GMT" "GMT"))
|
|
|
|
|
|
|
|
|
|
(provide 'rmailsort)
|
|
|
|
|
|
2009-09-10 06:18:23 +00:00
|
|
|
|
;; Local Variables:
|
|
|
|
|
;; generated-autoload-file: "rmail.el"
|
|
|
|
|
;; End:
|
|
|
|
|
|
2009-01-22 17:09:23 +00:00
|
|
|
|
;;; rmailsort.el ends here
|