1 ;;; rmailsort.el --- Rmail: sort messages
3 ;; Copyright (C) 1990, 1993, 1994, 2001, 2002, 2003, 2004,
4 ;; 2005 Free Software Foundation, Inc.
6 ;; Author: Masanobu UMEDA <umerin@mse.kyutech.ac.jp>
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)
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.
33 ;; For rmail-select-summary
36 (autoload 'timezone-make-date-sortable
"timezone")
38 ;; Sorting messages in Rmail buffer
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."
45 (rmail-sort-messages reverse
48 (rmail-make-date-sortable
49 (rmail-fetch-field msg
"Date"))))))
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."
56 (rmail-sort-messages reverse
59 (let ((key (or (rmail-fetch-field msg
"Subject") ""))
62 (if (string-match "^\\(re:[ \t]*\\)*" key
)
63 (substring key
(match-end 0))
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."
71 (rmail-sort-messages reverse
74 (downcase ;Canonical name
75 (mail-strip-quoted-names
76 (or (rmail-fetch-field msg
"From")
77 (rmail-fetch-field msg
"Sender") "")))))))
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."
84 (rmail-sort-messages reverse
87 (downcase ;Canonical name
88 (mail-strip-quoted-names
89 (or (rmail-fetch-field msg
"To")
90 (rmail-fetch-field msg
"Apparently-To") "")
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."
98 (rmail-sort-messages reverse
101 (rmail-select-correspondent
103 '("From" "Sender" "To" "Apparently-To"))))))
105 (defun rmail-select-correspondent (msg fields
)
107 (while (and fields
(string= ans
""))
110 (mail-strip-quoted-names
111 (or (rmail-fetch-field msg
(car fields
)) ""))))
112 (setq fields
(cdr fields
)))
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."
120 (rmail-sort-messages reverse
123 (count-lines (rmail-msgbeg msg
)
124 (rmail-msgend msg
))))))
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)) ","))
136 (while (string-match "[ \t]*,[ \t]*" labels
)
139 (substring labels
0 (match-beginning 0))
142 (setq labels
(substring labels
(match-end 0))))
143 (setq labelvec
(apply 'vector
(nreverse labelvec
)))
144 (rmail-sort-messages reverse
148 (while (and (< n
(length labelvec
))
149 (not (rmail-message-labels-p
150 msg
(aref labelvec n
))))
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."
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
168 (message "Finding sort keys...")
171 (while (>= rmail-total-messages msgnum
)
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
)))
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
)))
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
)
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
)
204 ;; Now reorder messages.
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?
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.
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."
234 (let ((next (rmail-msgend msg
)))
235 (goto-char (rmail-msgbeg msg
))
236 (narrow-to-region (if (search-forward "\n*** EOOH ***\n" next t
)
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"))
250 ;;; arch-tag: 0d90896b-0c35-46ac-b240-38be5ada2360
251 ;;; rmailsort.el ends here