(rmail-perm-variables): rmail-overlay-list needs to be buffer-local in
[bpt/emacs.git] / lisp / mail / rmailsort.el
CommitLineData
537ab246
BG
1;;; rmailsort.el --- Rmail: sort messages
2
65ad1920
GM
3;; Copyright (C) 1990, 1993, 1994, 2001, 2002, 2003, 2004, 2005, 2006,
4;; 2007, 2008, 2009 Free Software Foundation, Inc.
537ab246
BG
5
6;; Author: Masanobu UMEDA <umerin@mse.kyutech.ac.jp>
7;; Maintainer: FSF
8;; Keywords: mail
9
10;; This file is part of GNU Emacs.
11
12;; GNU Emacs is free software: you can redistribute it and/or modify
13;; it under the terms of the GNU General Public License as published by
14;; the Free Software Foundation, either version 3 of the License, or
15;; (at your option) any later version.
16
17;; GNU Emacs is distributed in the hope that it will be useful,
18;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20;; GNU General Public License for more details.
21
22;; You should have received a copy of the GNU General Public License
23;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24
25;;; Commentary:
26
65ad1920
GM
27;; Functions for sorting messages in an Rmail buffer.
28
537ab246
BG
29;;; Code:
30
efb656fd 31(require 'rmail)
537ab246 32
537ab246
BG
33;;;###autoload
34(defun rmail-sort-by-date (reverse)
65ad1920
GM
35 "Sort messages of current Rmail buffer by \"Date\" header.
36If prefix argument REVERSE is non-nil, sorts in reverse order."
537ab246
BG
37 (interactive "P")
38 (rmail-sort-messages reverse
65ad1920
GM
39 (lambda (msg)
40 (rmail-make-date-sortable
41 (rmail-get-header "Date" msg)))))
537ab246
BG
42
43;;;###autoload
44(defun rmail-sort-by-subject (reverse)
65ad1920
GM
45 "Sort messages of current Rmail buffer by \"Subject\" header.
46Ignores any \"Re: \" prefix. If prefix argument REVERSE is
47non-nil, sorts in reverse order."
48 ;; Note this is a case-sensitive sort.
537ab246
BG
49 (interactive "P")
50 (rmail-sort-messages reverse
65ad1920
GM
51 (lambda (msg)
52 (let ((key (or (rmail-get-header "Subject" msg) ""))
53 (case-fold-search t))
54 ;; Remove `Re:'
55 (if (string-match "^\\(re:[ \t]*\\)*" key)
56 (substring key (match-end 0))
57 key)))))
537ab246
BG
58
59;;;###autoload
60(defun rmail-sort-by-author (reverse)
65ad1920
GM
61 "Sort messages of current Rmail buffer by author.
62This uses either the \"From\" or \"Sender\" header, downcased.
63If prefix argument REVERSE is non-nil, sorts in reverse order."
537ab246
BG
64 (interactive "P")
65 (rmail-sort-messages reverse
65ad1920
GM
66 (lambda (msg)
67 (downcase ; canonical name
68 (mail-strip-quoted-names
69 (or (rmail-get-header "From" msg)
70 (rmail-get-header "Sender" msg) ""))))))
537ab246
BG
71
72;;;###autoload
73(defun rmail-sort-by-recipient (reverse)
65ad1920
GM
74 "Sort messages of current Rmail buffer by recipient.
75This uses either the \"To\" or \"Apparently-To\" header, downcased.
76If prefix argument REVERSE is non-nil, sorts in reverse order."
537ab246
BG
77 (interactive "P")
78 (rmail-sort-messages reverse
65ad1920
GM
79 (lambda (msg)
80 (downcase ; canonical name
81 (mail-strip-quoted-names
82 (or (rmail-get-header "To" msg)
83 (rmail-get-header "Apparently-To" msg) ""))))))
537ab246
BG
84
85;;;###autoload
86(defun rmail-sort-by-correspondent (reverse)
65ad1920
GM
87 "Sort messages of current Rmail buffer by other correspondent.
88This uses either the \"From\", \"Sender\", \"To\", or
89\"Apparently-To\" header, downcased. Uses the first header not
90excluded by `rmail-dont-reply-to-names'. If prefix argument
91REVERSE is non-nil, sorts in reverse order."
537ab246
BG
92 (interactive "P")
93 (rmail-sort-messages reverse
65ad1920
GM
94 (lambda (msg)
95 (downcase
537ab246
BG
96 (rmail-select-correspondent
97 msg
98 '("From" "Sender" "To" "Apparently-To"))))))
99
100(defun rmail-select-correspondent (msg fields)
65ad1920
GM
101 "Find the first header not excluded by `rmail-dont-reply-to-names'.
102MSG is a message number. FIELDS is a list of header names."
537ab246
BG
103 (let ((ans ""))
104 (while (and fields (string= ans ""))
105 (setq ans
106 ;; NB despite the name, this lives in mail-utils.el.
107 (rmail-dont-reply-to
108 (mail-strip-quoted-names
109 (or (rmail-get-header (car fields) msg) ""))))
110 (setq fields (cdr fields)))
111 ans))
112
113;;;###autoload
114(defun rmail-sort-by-lines (reverse)
65ad1920
GM
115 "Sort messages of current Rmail buffer by the number of lines.
116If prefix argument REVERSE is non-nil, sorts in reverse order."
537ab246
BG
117 (interactive "P")
118 (rmail-sort-messages reverse
65ad1920
GM
119 (lambda (msg)
120 (count-lines (rmail-msgbeg msg)
121 (rmail-msgend msg)))))
537ab246
BG
122
123;;;###autoload
124(defun rmail-sort-by-labels (reverse labels)
65ad1920
GM
125 "Sort messages of current Rmail buffer by labels.
126LABELS is a comma-separated list of labels. The order of these
127labels specifies the order of messages: messages with the first
128label come first, messages with the second label come second, and
129so on. Messages that have none of these labels come last.
130If prefix argument REVERSE is non-nil, sorts in reverse order."
537ab246 131 (interactive "P\nsSort by labels: ")
65ad1920 132 (or (string-match "[^ \t]" labels) ; need some non-whitespace
537ab246 133 (error "No labels specified"))
65ad1920 134 ;; Remove leading whitespace, add trailing comma.
537ab246 135 (setq labels (concat (substring labels (match-beginning 0)) ","))
65ad1920
GM
136 (let (labelvec nmax)
137 ;; Convert "l1,..." into "\\(, \\|\\`\\)l1\\(,\\|\\'\\)" "..." ...
537ab246
BG
138 (while (string-match "[ \t]*,[ \t]*" labels)
139 (setq labelvec (cons
65ad1920 140 (concat "\\(, \\|\\`\\)"
537ab246 141 (substring labels 0 (match-beginning 0))
65ad1920 142 "\\(,\\|\\'\\)")
537ab246
BG
143 labelvec))
144 (setq labels (substring labels (match-end 0))))
65ad1920
GM
145 (setq labelvec (apply 'vector (nreverse labelvec))
146 nmax (length labelvec))
537ab246 147 (rmail-sort-messages reverse
65ad1920
GM
148 ;; If no labels match, returns nmax; if they
149 ;; match the first specified in LABELS,
150 ;; returns 0; if they match the second, returns 1; etc.
151 ;; Hence sorts as described in the doc-string.
152 (lambda (msg)
153 (let ((n 0)
154 (str (concat (rmail-get-attr-names msg)
155 ", "
156 (rmail-get-keywords msg))))
157 ;; No labels: can't match anything.
158 (if (string-equal ", " str)
159 nmax
160 (while (and (< n nmax)
161 (not (string-match (aref labelvec n)
162 str)))
163 (setq n (1+ n)))
164 n))))))
537ab246
BG
165\f
166;; Basic functions
efb656fd 167(declare-function rmail-update-summary "rmailsum" (&rest ignore))
537ab246
BG
168
169(defun rmail-sort-messages (reverse keyfun)
65ad1920
GM
170 "Sort messages of current Rmail buffer.
171If REVERSE is non-nil, sorts in reverse order. Calls the
172function KEYFUN with a message number (it should return a sort key).
173Numeric keys are sorted numerically, all others as strings."
537ab246
BG
174 (with-current-buffer rmail-buffer
175 (let ((return-to-point
176 (if (rmail-buffers-swapped-p)
177 (point)))
537ab246
BG
178 (sort-lists nil))
179 (rmail-swap-buffers-maybe)
180 (message "Finding sort keys...")
181 (widen)
182 (let ((msgnum 1))
183 (while (>= rmail-total-messages msgnum)
184 (setq sort-lists
185 (cons (list (funcall keyfun msgnum) ;Make sorting key
186 (eq rmail-current-message msgnum) ;True if current
187 (aref rmail-message-vector msgnum)
188 (aref rmail-message-vector (1+ msgnum)))
189 sort-lists))
190 (if (zerop (% msgnum 10))
191 (message "Finding sort keys...%d" msgnum))
192 (setq msgnum (1+ msgnum))))
193 (or reverse (setq sort-lists (nreverse sort-lists)))
537ab246
BG
194 (setq sort-lists
195 (sort sort-lists
73d7bcb9
SM
196 ;; Decide predicate: < or string-lessp
197 (if (numberp (car (car sort-lists))) ;Is a key numeric?
198 'car-less-than-car
65ad1920
GM
199 (lambda (a b)
200 (string-lessp (car a) (car b))))))
537ab246
BG
201 (if reverse (setq sort-lists (nreverse sort-lists)))
202 ;; Now we enter critical region. So, keyboard quit is disabled.
203 (message "Reordering messages...")
204 (let ((inhibit-quit t) ;Inhibit quit
205 (inhibit-read-only t)
206 (current-message nil)
207 (msgnum 1)
65ad1920
GM
208 (msginfo nil)
209 (undo (not (eq buffer-undo-list t))))
537ab246
BG
210 ;; There's little hope that we can easily undo after that.
211 (buffer-disable-undo (current-buffer))
212 (goto-char (rmail-msgbeg 1))
213 ;; To force update of all markers,
214 ;; keep the new copies separated from the remaining old messages.
215 (insert-before-markers ?Z)
216 (backward-char 1)
217 ;; Now reorder messages.
218 (dolist (msginfo sort-lists)
219 ;; Swap two messages.
220 (insert-buffer-substring
221 (current-buffer) (nth 2 msginfo) (nth 3 msginfo))
222 ;; The last message may not have \n\n after it.
65ad1920 223 (unless (bolp)
537ab246
BG
224 (insert "\n"))
225 (unless (looking-back "\n\n")
226 (insert "\n"))
227 (delete-region (nth 2 msginfo) (nth 3 msginfo))
228 ;; Is current message?
229 (if (nth 1 msginfo)
230 (setq current-message msgnum))
231 (if (zerop (% msgnum 10))
232 (message "Reordering messages...%d" msgnum))
233 (setq msgnum (1+ msgnum)))
234 ;; Delete the dummy separator Z inserted before.
235 (delete-char 1)
236 (setq quit-flag nil)
65ad1920
GM
237 ;; If undo was on before, re-enable it. But note that it is
238 ;; disabled in mbox Rmail, so this is kind of pointless.
239 (if undo (buffer-enable-undo))
537ab246 240 (rmail-set-message-counters)
a1a29341 241 (rmail-show-message-1 current-message)
537ab246
BG
242 (if return-to-point
243 (goto-char return-to-point))
244 (if (rmail-summary-exists)
245 (rmail-select-summary (rmail-update-summary)))))))
246
efb656fd
GM
247(autoload 'timezone-make-date-sortable "timezone")
248
537ab246 249(defun rmail-make-date-sortable (date)
65ad1920 250 "Make DATE sortable using the function `string-lessp'."
537ab246
BG
251 ;; Assume the default time zone is GMT.
252 (timezone-make-date-sortable date "GMT" "GMT"))
253
254(provide 'rmailsort)
255
537ab246
BG
256;; arch-tag: 665da245-f6a7-4115-ad8c-ba19216988d5
257;;; rmailsort.el ends here