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