*** empty log message ***
[bpt/emacs.git] / lisp / mail / rmailsort.el
1 ;;; Rmail: sort messages.
2 ;; Copyright (C) 1990 Masanobu UMEDA
3 ;; umerin@tc.Nagasaki.GO.JP?
4
5 ;; This file is part of GNU Emacs.
6
7 ;; GNU Emacs is distributed in the hope that it will be useful,
8 ;; but WITHOUT ANY WARRANTY. No author or distributor
9 ;; accepts responsibility to anyone for the consequences of using it
10 ;; or for whether it serves any particular purpose or works at all,
11 ;; unless he says so in writing. Refer to the GNU Emacs General Public
12 ;; License for full details.
13
14 ;; Everyone is granted permission to copy, modify and redistribute
15 ;; GNU Emacs, but only under the conditions described in the
16 ;; GNU Emacs General Public License. A copy of this license is
17 ;; supposed to have been given to you along with GNU Emacs so you
18 ;; can know your rights and responsibilities. It should be in a
19 ;; file named COPYING. Among other things, the copyright notice
20 ;; and this notice must be preserved on all copies.
21
22 (require 'rmail)
23 (require 'sort)
24
25 ;; GNUS compatible key bindings.
26 (define-key rmail-mode-map "\C-c\C-s\C-d" 'rmail-sort-by-date)
27 (define-key rmail-mode-map "\C-c\C-s\C-s" 'rmail-sort-by-subject)
28 (define-key rmail-mode-map "\C-c\C-s\C-a" 'rmail-sort-by-author)
29 (define-key rmail-mode-map "\C-c\C-s\C-r" 'rmail-sort-by-recipient)
30 (define-key rmail-mode-map "\C-c\C-s\C-c" 'rmail-sort-by-correspondent)
31 (define-key rmail-mode-map "\C-c\C-s\C-l" 'rmail-sort-by-size-lines)
32
33 (defun rmail-sort-by-date (reverse)
34 "Sort messages of current Rmail file by date.
35 If prefix argument REVERSE is non-nil, sort them in reverse order."
36 (interactive "P")
37 (rmail-sort-messages reverse
38 (function
39 (lambda (msg)
40 (rmail-sortable-date-string
41 (rmail-fetch-field msg "Date"))))))
42
43 (defun rmail-sort-by-subject (reverse)
44 "Sort messages of current Rmail file by subject.
45 If prefix argument REVERSE is non-nil, sort them in reverse order."
46 (interactive "P")
47 (rmail-sort-messages reverse
48 (function
49 (lambda (msg)
50 (let ((key (or (rmail-fetch-field msg "Subject") ""))
51 (case-fold-search t))
52 ;; Remove `Re:'
53 (if (string-match "^\\(re:[ \t]+\\)*" key)
54 (substring key (match-end 0)) key))))))
55
56 (defun rmail-sort-by-author (reverse)
57 "Sort messages of current Rmail file by author.
58 If prefix argument REVERSE is non-nil, sort them in reverse order."
59 (interactive "P")
60 (rmail-sort-messages reverse
61 (function
62 (lambda (msg)
63 (mail-strip-quoted-names
64 (or (rmail-fetch-field msg "From")
65 (rmail-fetch-field msg "Sender") ""))))))
66
67 (defun rmail-sort-by-recipient (reverse)
68 "Sort messages of current Rmail file by recipient.
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 (mail-strip-quoted-names
75 (or (rmail-fetch-field msg "To")
76 (rmail-fetch-field msg "Apparently-To") "")
77 )))))
78
79 (defun rmail-sort-by-correspondent (reverse)
80 "Sort messages of current Rmail file by other correspondent.
81 If prefix argument REVERSE is non-nil, sort them in reverse order."
82 (interactive "P")
83 (rmail-sort-messages reverse
84 (function
85 (lambda (msg)
86 (rmail-select-correspondent
87 msg
88 '("From" "Sender" "To" "Apparently-To"))))))
89
90 (defun rmail-select-correspondent (msg fields)
91 (let ((ans ""))
92 (while (and fields (string= ans ""))
93 (setq ans
94 (rmail-dont-reply-to
95 (mail-strip-quoted-names
96 (or (rmail-fetch-field msg (car fields)) ""))))
97 (setq fields (cdr fields)))
98 ans))
99
100 (defun rmail-sort-by-size-lines (reverse)
101 "Sort messages of current Rmail file by message size.
102 If prefix argument REVERSE is non-nil, sort them in reverse order."
103 (interactive "P")
104 (rmail-sort-messages reverse
105 (function
106 (lambda (msg)
107 (format "%9d"
108 (count-lines (rmail-msgbeg msgnum)
109 (rmail-msgend msgnum)))))))
110 \f
111
112 (defun rmail-sort-messages (reverse keyfunc)
113 "Sort messages of current Rmail file.
114 1st argument REVERSE is non-nil, sort them in reverse order.
115 2nd argument KEYFUNC is called with message number, and should return a key."
116 (let ((buffer-read-only nil)
117 (sort-lists nil))
118 (message "Finding sort keys...")
119 (widen)
120 (let ((msgnum 1))
121 (while (>= rmail-total-messages msgnum)
122 (setq sort-lists
123 (cons (cons (funcall keyfunc msgnum) ;A sort key.
124 (buffer-substring
125 (rmail-msgbeg msgnum) (rmail-msgend msgnum)))
126 sort-lists))
127 (if (zerop (% msgnum 10))
128 (message "Finding sort keys...%d" msgnum))
129 (setq msgnum (1+ msgnum))))
130 (or reverse (setq sort-lists (nreverse sort-lists)))
131 (setq sort-lists
132 (sort sort-lists
133 (function
134 (lambda (a b)
135 (string-lessp (car a) (car b))))))
136 (if reverse (setq sort-lists (nreverse sort-lists)))
137 (message "Reordering buffer...")
138 (delete-region (rmail-msgbeg 1) (rmail-msgend rmail-total-messages))
139 (let ((msgnum 1))
140 (while sort-lists
141 (insert (cdr (car sort-lists)))
142 (if (zerop (% msgnum 10))
143 (message "Reordering buffer...%d" msgnum))
144 (setq sort-lists (cdr sort-lists))
145 (setq msgnum (1+ msgnum))))
146 (rmail-set-message-counters)
147 (rmail-show-message 1)))
148
149 (defun rmail-fetch-field (msg field)
150 "Return the value of the header field FIELD of MSG.
151 Arguments are MSG and FIELD."
152 (let ((next (rmail-msgend msg)))
153 (save-restriction
154 (goto-char (rmail-msgbeg msg))
155 (narrow-to-region (if (search-forward "\n*** EOOH ***\n" next t)
156 (point)
157 (forward-line 1)
158 (point))
159 (progn (search-forward "\n\n" nil t) (point)))
160 (mail-fetch-field field))))
161
162 ;; Copy of the function gnus-comparable-date in gnus.el
163
164 (defun rmail-sortable-date-string (date)
165 "Make sortable string by string-lessp from DATE."
166 (let ((month '(("JAN" . " 1")("FEB" . " 2")("MAR" . " 3")
167 ("APR" . " 4")("MAY" . " 5")("JUN" . " 6")
168 ("JUL" . " 7")("AUG" . " 8")("SEP" . " 9")
169 ("OCT" . "10")("NOV" . "11")("DEC" . "12")
170 ("JANUARY" . " 1") ("FEBRUARY" . " 2")
171 ("MARCH" . " 3") ("APRIL" . " 4")
172 ("MAY" . " 5") ("JUNE" . " 6")
173 ("JULY" . " 7") ("AUGUST" . " 8")
174 ("SEPTEMBER" " 9") ("OCTOBER" . "10")
175 ("NOVEMBER" "11") ("DECEMBER" . "12")))
176 (date (or date "")))
177 ;; Can understand the following styles:
178 ;; (1) 14 Apr 89 03:20:12 GMT
179 ;; (2) Fri, 17 Mar 89 4:01:33 GMT
180 (if (string-match
181 "\\([0-9]+\\) +\\([^ ,]+\\) +\\([0-9]+\\) +\\([0-9:]+\\)" date)
182 (concat
183 ;; Year
184 (rmail-date-full-year
185 (substring date (match-beginning 3) (match-end 3)))
186 ;; Month
187 (cdr
188 (assoc
189 (upcase (substring date (match-beginning 2) (match-end 2))) month))
190 ;; Day
191 (format "%2d" (string-to-int
192 (substring date
193 (match-beginning 1) (match-end 1))))
194 ;; Time
195 (substring date (match-beginning 4) (match-end 4)))
196 ;; Cannot understand DATE string.
197 date)))
198
199 (defun rmail-date-full-year (year-string)
200 (if (<= (length year-string) 2)
201 (concat "19" year-string)
202 year-string))
203
204 (provide 'rmailsort)