* mail/mspools.el (rmail-get-new-mail):
[emacs.git] / lisp / mail / rmailsort.el
blobbed40cd0820c04d2ff4479a40391bc36fbd9bd99
1 ;;; rmailsort.el --- Rmail: sort messages
3 ;; Copyright (C) 1990, 1993, 1994, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007 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 3, 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
155 (declare-function rmail-update-summary "rmailsum" (&rest ignore))
157 (defun rmail-sort-messages (reverse keyfun)
158 "Sort messages of current Rmail file.
159 If 1st argument REVERSE is non-nil, sort them in reverse order.
160 2nd argument KEYFUN is called with a message number, and should return a key."
161 (save-current-buffer
162 ;; If we are in a summary buffer, operate on the Rmail buffer.
163 (if (eq major-mode 'rmail-summary-mode)
164 (set-buffer rmail-buffer))
165 (let ((buffer-read-only nil)
166 (point-offset (- (point) (point-min)))
167 (predicate nil) ;< or string-lessp
168 (sort-lists nil))
169 (message "Finding sort keys...")
170 (widen)
171 (let ((msgnum 1))
172 (while (>= rmail-total-messages msgnum)
173 (setq sort-lists
174 (cons (list (funcall keyfun msgnum) ;Make sorting key
175 (eq rmail-current-message msgnum) ;True if current
176 (aref rmail-message-vector msgnum)
177 (aref rmail-message-vector (1+ msgnum)))
178 sort-lists))
179 (if (zerop (% msgnum 10))
180 (message "Finding sort keys...%d" msgnum))
181 (setq msgnum (1+ msgnum))))
182 (or reverse (setq sort-lists (nreverse sort-lists)))
183 ;; Decide predicate: < or string-lessp
184 (if (numberp (car (car sort-lists))) ;Is a key numeric?
185 (setq predicate (function <))
186 (setq predicate (function string-lessp)))
187 (setq sort-lists
188 (sort sort-lists
189 (function
190 (lambda (a b)
191 (funcall predicate (car a) (car b))))))
192 (if reverse (setq sort-lists (nreverse sort-lists)))
193 ;; Now we enter critical region. So, keyboard quit is disabled.
194 (message "Reordering messages...")
195 (let ((inhibit-quit t) ;Inhibit quit
196 (current-message nil)
197 (msgnum 1)
198 (msginfo nil))
199 ;; There's little hope that we can easily undo after that.
200 (buffer-disable-undo (current-buffer))
201 (goto-char (rmail-msgbeg 1))
202 ;; To force update of all markers.
203 (insert-before-markers ?Z)
204 (backward-char 1)
205 ;; Now reorder messages.
206 (while sort-lists
207 (setq msginfo (car sort-lists))
208 ;; Swap two messages.
209 (insert-buffer-substring
210 (current-buffer) (nth 2 msginfo) (nth 3 msginfo))
211 (delete-region (nth 2 msginfo) (nth 3 msginfo))
212 ;; Is current message?
213 (if (nth 1 msginfo)
214 (setq current-message msgnum))
215 (setq sort-lists (cdr sort-lists))
216 (if (zerop (% msgnum 10))
217 (message "Reordering messages...%d" msgnum))
218 (setq msgnum (1+ msgnum)))
219 ;; Delete the garbage inserted before.
220 (delete-char 1)
221 (setq quit-flag nil)
222 (buffer-enable-undo)
223 (rmail-set-message-counters)
224 (rmail-show-message current-message)
225 (goto-char (+ point-offset (point-min)))
226 (if (rmail-summary-exists)
227 (rmail-select-summary
228 (rmail-update-summary)))))))
230 (defun rmail-fetch-field (msg field)
231 "Return the value of the header FIELD of MSG.
232 Arguments are MSG and FIELD."
233 (save-restriction
234 (widen)
235 (let ((next (rmail-msgend msg)))
236 (goto-char (rmail-msgbeg msg))
237 (narrow-to-region (if (search-forward "\n*** EOOH ***\n" next t)
238 (point)
239 (forward-line 1)
240 (point))
241 (progn (search-forward "\n\n" nil t) (point)))
242 (mail-fetch-field field))))
244 (defun rmail-make-date-sortable (date)
245 "Make DATE sortable using the function string-lessp."
246 ;; Assume the default time zone is GMT.
247 (timezone-make-date-sortable date "GMT" "GMT"))
249 (provide 'rmailsort)
251 ;;; arch-tag: 0d90896b-0c35-46ac-b240-38be5ada2360
252 ;;; rmailsort.el ends here