Commit | Line | Data |
---|---|---|
55535639 | 1 | ;;; rmailsort.el --- Rmail: sort messages |
c88ab9ce | 2 | |
e84b4b86 | 3 | ;; Copyright (C) 1990, 1993, 1994, 2001, 2002, 2003, 2004, |
d7a0267c | 4 | ;; 2005, 2006, 2007 Free Software Foundation, Inc. |
9750e079 | 5 | |
f00de7c3 | 6 | ;; Author: Masanobu UMEDA <umerin@mse.kyutech.ac.jp> |
1884e325 | 7 | ;; Maintainer: FSF |
d7b4d18f | 8 | ;; Keywords: mail |
e5167999 | 9 | |
8fc3fee1 RS |
10 | ;; This file is part of GNU Emacs. |
11 | ||
70e1dad8 RS |
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 | |
ceaeecb0 | 14 | ;; the Free Software Foundation; either version 3, or (at your option) |
70e1dad8 RS |
15 | ;; any later version. |
16 | ||
8fc3fee1 | 17 | ;; GNU Emacs is distributed in the hope that it will be useful, |
70e1dad8 RS |
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 | |
71bccd7f | 23 | ;; along with GNU Emacs; see the file COPYING. If not, write to the |
3a35cf56 LK |
24 | ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
25 | ;; Boston, MA 02110-1301, USA. | |
70e1dad8 | 26 | |
55535639 PJ |
27 | ;;; Commentary: |
28 | ||
e5167999 | 29 | ;;; Code: |
8fc3fee1 | 30 | |
8fc3fee1 RS |
31 | (require 'sort) |
32 | ||
7999547d KH |
33 | ;; For rmail-select-summary |
34 | (require 'rmail) | |
35 | ||
f00de7c3 RS |
36 | (autoload 'timezone-make-date-sortable "timezone") |
37 | ||
f00de7c3 | 38 | ;; Sorting messages in Rmail buffer |
8fc3fee1 | 39 | |
372a91d7 | 40 | ;;;###autoload |
8fc3fee1 RS |
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) | |
f00de7c3 | 48 | (rmail-make-date-sortable |
8fc3fee1 RS |
49 | (rmail-fetch-field msg "Date")))))) |
50 | ||
372a91d7 | 51 | ;;;###autoload |
8fc3fee1 RS |
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:' | |
b0304aef KH |
62 | (if (string-match "^\\(re:[ \t]*\\)*" key) |
63 | (substring key (match-end 0)) | |
64 | key)))))) | |
8fc3fee1 | 65 | |
372a91d7 | 66 | ;;;###autoload |
8fc3fee1 RS |
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) | |
f00de7c3 RS |
74 | (downcase ;Canonical name |
75 | (mail-strip-quoted-names | |
76 | (or (rmail-fetch-field msg "From") | |
77 | (rmail-fetch-field msg "Sender") ""))))))) | |
8fc3fee1 | 78 | |
372a91d7 | 79 | ;;;###autoload |
8fc3fee1 RS |
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) | |
f00de7c3 RS |
87 | (downcase ;Canonical name |
88 | (mail-strip-quoted-names | |
89 | (or (rmail-fetch-field msg "To") | |
90 | (rmail-fetch-field msg "Apparently-To") "") | |
91 | )))))) | |
8fc3fee1 | 92 | |
372a91d7 | 93 | ;;;###autoload |
22a72f53 RS |
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 | |
102 | msg | |
103 | '("From" "Sender" "To" "Apparently-To")))))) | |
104 | ||
105 | (defun rmail-select-correspondent (msg fields) | |
106 | (let ((ans "")) | |
f00de7c3 RS |
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)) | |
114 | ||
372a91d7 | 115 | ;;;###autoload |
f00de7c3 | 116 | (defun rmail-sort-by-lines (reverse) |
8b1d5b31 | 117 | "Sort messages of current Rmail file by number of lines. |
51693c8c RS |
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) | |
be4e7157 KH |
123 | (count-lines (rmail-msgbeg msg) |
124 | (rmail-msgend msg)))))) | |
15589977 | 125 | |
372a91d7 | 126 | ;;;###autoload |
27d42947 | 127 | (defun rmail-sort-by-labels (reverse labels) |
15589977 KH |
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) | |
a1506d29 | 137 | (setq labelvec (cons |
15589977 KH |
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)))))) | |
8fc3fee1 | 153 | \f |
f00de7c3 | 154 | ;; Basic functions |
2b54af74 | 155 | (declare-function rmail-update-summary "rmailsum" (&rest ignore)) |
f00de7c3 RS |
156 | |
157 | (defun rmail-sort-messages (reverse keyfun) | |
8fc3fee1 | 158 | "Sort messages of current Rmail file. |
f00de7c3 RS |
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." | |
1884e325 | 161 | (save-current-buffer |
36bedc4f RS |
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) | |
1884e325 | 166 | (point-offset (- (point) (point-min))) |
36bedc4f RS |
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. | |
9baf79c7 | 200 | (buffer-disable-undo (current-buffer)) |
36bedc4f RS |
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) | |
030ecb3c | 224 | (rmail-show-message current-message) |
1884e325 | 225 | (goto-char (+ point-offset (point-min))) |
030ecb3c KH |
226 | (if (rmail-summary-exists) |
227 | (rmail-select-summary | |
228 | (rmail-update-summary))))))) | |
f00de7c3 | 229 | |
8fc3fee1 | 230 | (defun rmail-fetch-field (msg field) |
f00de7c3 | 231 | "Return the value of the header FIELD of MSG. |
8fc3fee1 | 232 | Arguments are MSG and FIELD." |
f00de7c3 RS |
233 | (save-restriction |
234 | (widen) | |
235 | (let ((next (rmail-msgend msg))) | |
8fc3fee1 RS |
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)))) | |
243 | ||
f00de7c3 RS |
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")) | |
248 | ||
49116ac0 | 249 | (provide 'rmailsort) |
c88ab9ce | 250 | |
ab5796a9 | 251 | ;;; arch-tag: 0d90896b-0c35-46ac-b240-38be5ada2360 |
c88ab9ce | 252 | ;;; rmailsort.el ends here |