Update my email address.
[emacs.git] / lisp / mail / rmailsort.el
blobc3f7362b18ac291a224ebebec538105ab617bd50
1 ;;; rmailsort.el --- Rmail: sort messages
3 ;; Copyright (C) 1990, 1993, 1994, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006 Free Software Foundation, Inc.
6 ;; Author: Masanobu UMEDA <umerin@mse.kyutech.ac.jp>
7 ;; Maintainer: FSF
8 ;; Keywords: mail
10 ;; This file is part of GNU Emacs.
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 2, or (at your option)
15 ;; any later version.
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.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
27 ;;; Commentary:
29 ;;; Code:
31 (require 'sort)
33 ;; For rmail-select-summary
34 (require 'rmail)
36 (autoload 'timezone-make-date-sortable "timezone")
38 ;; Sorting messages in Rmail buffer
40 ;;;###autoload
41 (defun rmail-sort-by-date (reverse)
42 "Sort messages of current Rmail file by date.
43 If prefix argument REVERSE is non-nil, sort them in reverse order."
44 (interactive "P")
45 (rmail-sort-messages reverse
46 (function
47 (lambda (msg)
48 (rmail-make-date-sortable
49 (rmail-fetch-field msg "Date"))))))
51 ;;;###autoload
52 (defun rmail-sort-by-subject (reverse)
53 "Sort messages of current Rmail file by subject.
54 If prefix argument REVERSE is non-nil, sort them in reverse order."
55 (interactive "P")
56 (rmail-sort-messages reverse
57 (function
58 (lambda (msg)
59 (let ((key (or (rmail-fetch-field msg "Subject") ""))
60 (case-fold-search t))
61 ;; Remove `Re:'
62 (if (string-match "^\\(re:[ \t]*\\)*" key)
63 (substring key (match-end 0))
64 key))))))
66 ;;;###autoload
67 (defun rmail-sort-by-author (reverse)
68 "Sort messages of current Rmail file by author.
69 If prefix argument REVERSE is non-nil, sort them in reverse order."
70 (interactive "P")
71 (rmail-sort-messages reverse
72 (function
73 (lambda (msg)
74 (downcase ;Canonical name
75 (mail-strip-quoted-names
76 (or (rmail-fetch-field msg "From")
77 (rmail-fetch-field msg "Sender") "")))))))
79 ;;;###autoload
80 (defun rmail-sort-by-recipient (reverse)
81 "Sort messages of current Rmail file by recipient.
82 If prefix argument REVERSE is non-nil, sort them in reverse order."
83 (interactive "P")
84 (rmail-sort-messages reverse
85 (function
86 (lambda (msg)
87 (downcase ;Canonical name
88 (mail-strip-quoted-names
89 (or (rmail-fetch-field msg "To")
90 (rmail-fetch-field msg "Apparently-To") "")
91 ))))))
93 ;;;###autoload
94 (defun rmail-sort-by-correspondent (reverse)
95 "Sort messages of current Rmail file by other correspondent.
96 If prefix argument REVERSE is non-nil, sort them in reverse order."
97 (interactive "P")
98 (rmail-sort-messages reverse
99 (function
100 (lambda (msg)
101 (rmail-select-correspondent
103 '("From" "Sender" "To" "Apparently-To"))))))
105 (defun rmail-select-correspondent (msg fields)
106 (let ((ans ""))
107 (while (and fields (string= ans ""))
108 (setq ans
109 (rmail-dont-reply-to
110 (mail-strip-quoted-names
111 (or (rmail-fetch-field msg (car fields)) ""))))
112 (setq fields (cdr fields)))
113 ans))
115 ;;;###autoload
116 (defun rmail-sort-by-lines (reverse)
117 "Sort messages of current Rmail file by number of lines.
118 If prefix argument REVERSE is non-nil, sort them in reverse order."
119 (interactive "P")
120 (rmail-sort-messages reverse
121 (function
122 (lambda (msg)
123 (count-lines (rmail-msgbeg msg)
124 (rmail-msgend msg))))))
126 ;;;###autoload
127 (defun rmail-sort-by-labels (reverse labels)
128 "Sort messages of current Rmail file by labels.
129 If prefix argument REVERSE is non-nil, sort them in reverse order.
130 KEYWORDS is a comma-separated list of labels."
131 (interactive "P\nsSort by labels: ")
132 (or (string-match "[^ \t]" labels)
133 (error "No labels specified"))
134 (setq labels (concat (substring labels (match-beginning 0)) ","))
135 (let (labelvec)
136 (while (string-match "[ \t]*,[ \t]*" labels)
137 (setq labelvec (cons
138 (concat ", ?\\("
139 (substring labels 0 (match-beginning 0))
140 "\\),")
141 labelvec))
142 (setq labels (substring labels (match-end 0))))
143 (setq labelvec (apply 'vector (nreverse labelvec)))
144 (rmail-sort-messages reverse
145 (function
146 (lambda (msg)
147 (let ((n 0))
148 (while (and (< n (length labelvec))
149 (not (rmail-message-labels-p
150 msg (aref labelvec n))))
151 (setq n (1+ n)))
152 n))))))
154 ;; Basic functions
156 (defun rmail-sort-messages (reverse keyfun)
157 "Sort messages of current Rmail file.
158 If 1st argument REVERSE is non-nil, sort them in reverse order.
159 2nd argument KEYFUN is called with a message number, and should return a key."
160 (save-current-buffer
161 ;; If we are in a summary buffer, operate on the Rmail buffer.
162 (if (eq major-mode 'rmail-summary-mode)
163 (set-buffer rmail-buffer))
164 (let ((buffer-read-only nil)
165 (point-offset (- (point) (point-min)))
166 (predicate nil) ;< or string-lessp
167 (sort-lists nil))
168 (message "Finding sort keys...")
169 (widen)
170 (let ((msgnum 1))
171 (while (>= rmail-total-messages msgnum)
172 (setq sort-lists
173 (cons (list (funcall keyfun msgnum) ;Make sorting key
174 (eq rmail-current-message msgnum) ;True if current
175 (aref rmail-message-vector msgnum)
176 (aref rmail-message-vector (1+ msgnum)))
177 sort-lists))
178 (if (zerop (% msgnum 10))
179 (message "Finding sort keys...%d" msgnum))
180 (setq msgnum (1+ msgnum))))
181 (or reverse (setq sort-lists (nreverse sort-lists)))
182 ;; Decide predicate: < or string-lessp
183 (if (numberp (car (car sort-lists))) ;Is a key numeric?
184 (setq predicate (function <))
185 (setq predicate (function string-lessp)))
186 (setq sort-lists
187 (sort sort-lists
188 (function
189 (lambda (a b)
190 (funcall predicate (car a) (car b))))))
191 (if reverse (setq sort-lists (nreverse sort-lists)))
192 ;; Now we enter critical region. So, keyboard quit is disabled.
193 (message "Reordering messages...")
194 (let ((inhibit-quit t) ;Inhibit quit
195 (current-message nil)
196 (msgnum 1)
197 (msginfo nil))
198 ;; There's little hope that we can easily undo after that.
199 (buffer-disable-undo (current-buffer))
200 (goto-char (rmail-msgbeg 1))
201 ;; To force update of all markers.
202 (insert-before-markers ?Z)
203 (backward-char 1)
204 ;; Now reorder messages.
205 (while sort-lists
206 (setq msginfo (car sort-lists))
207 ;; Swap two messages.
208 (insert-buffer-substring
209 (current-buffer) (nth 2 msginfo) (nth 3 msginfo))
210 (delete-region (nth 2 msginfo) (nth 3 msginfo))
211 ;; Is current message?
212 (if (nth 1 msginfo)
213 (setq current-message msgnum))
214 (setq sort-lists (cdr sort-lists))
215 (if (zerop (% msgnum 10))
216 (message "Reordering messages...%d" msgnum))
217 (setq msgnum (1+ msgnum)))
218 ;; Delete the garbage inserted before.
219 (delete-char 1)
220 (setq quit-flag nil)
221 (buffer-enable-undo)
222 (rmail-set-message-counters)
223 (rmail-show-message current-message)
224 (goto-char (+ point-offset (point-min)))
225 (if (rmail-summary-exists)
226 (rmail-select-summary
227 (rmail-update-summary)))))))
229 (defun rmail-fetch-field (msg field)
230 "Return the value of the header FIELD of MSG.
231 Arguments are MSG and FIELD."
232 (save-restriction
233 (widen)
234 (let ((next (rmail-msgend msg)))
235 (goto-char (rmail-msgbeg msg))
236 (narrow-to-region (if (search-forward "\n*** EOOH ***\n" next t)
237 (point)
238 (forward-line 1)
239 (point))
240 (progn (search-forward "\n\n" nil t) (point)))
241 (mail-fetch-field field))))
243 (defun rmail-make-date-sortable (date)
244 "Make DATE sortable using the function string-lessp."
245 ;; Assume the default time zone is GMT.
246 (timezone-make-date-sortable date "GMT" "GMT"))
248 (provide 'rmailsort)
250 ;;; arch-tag: 0d90896b-0c35-46ac-b240-38be5ada2360
251 ;;; rmailsort.el ends here