1 ;;; Rmail: sort messages.
2 ;; Copyright (C) 1990 Masanobu UMEDA
3 ;; umerin@tc.Nagasaki.GO.JP?
5 ;; This file is part of GNU Emacs.
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.
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.
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
)
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."
37 (rmail-sort-messages reverse
40 (rmail-sortable-date-string
41 (rmail-fetch-field msg
"Date"))))))
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."
47 (rmail-sort-messages reverse
50 (let ((key (or (rmail-fetch-field msg
"Subject") ""))
53 (if (string-match "^\\(re:[ \t]+\\)*" key
)
54 (substring key
(match-end 0)) key
))))))
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."
60 (rmail-sort-messages reverse
63 (mail-strip-quoted-names
64 (or (rmail-fetch-field msg
"From")
65 (rmail-fetch-field msg
"Sender") ""))))))
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."
71 (rmail-sort-messages reverse
74 (mail-strip-quoted-names
75 (or (rmail-fetch-field msg
"To")
76 (rmail-fetch-field msg
"Apparently-To") "")
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."
83 (rmail-sort-messages reverse
86 (rmail-select-correspondent
88 '("From" "Sender" "To" "Apparently-To"))))))
90 (defun rmail-select-correspondent (msg fields
)
92 (while (and fields
(string= ans
""))
95 (mail-strip-quoted-names
96 (or (rmail-fetch-field msg
(car fields
)) ""))))
97 (setq fields
(cdr fields
)))
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."
104 (rmail-sort-messages reverse
108 (count-lines (rmail-msgbeg msgnum
)
109 (rmail-msgend msgnum
)))))))
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
)
118 (message "Finding sort keys...")
121 (while (>= rmail-total-messages msgnum
)
123 (cons (cons (funcall keyfunc msgnum
) ;A sort key.
125 (rmail-msgbeg msgnum
) (rmail-msgend msgnum
)))
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
)))
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
))
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)))
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
)))
154 (goto-char (rmail-msgbeg msg
))
155 (narrow-to-region (if (search-forward "\n*** EOOH ***\n" next t
)
159 (progn (search-forward "\n\n" nil t
) (point)))
160 (mail-fetch-field field
))))
162 ;; Copy of the function gnus-comparable-date in gnus.el
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")))
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
181 "\\([0-9]+\\) +\\([^ ,]+\\) +\\([0-9]+\\) +\\([0-9:]+\\)" date
)
184 (rmail-date-full-year
185 (substring date
(match-beginning 3) (match-end 3)))
189 (upcase (substring date
(match-beginning 2) (match-end 2))) month
))
191 (format "%2d" (string-to-int
193 (match-beginning 1) (match-end 1))))
195 (substring date
(match-beginning 4) (match-end 4)))
196 ;; Cannot understand DATE string.
199 (defun rmail-date-full-year (year-string)
200 (if (<= (length year-string
) 2)
201 (concat "19" year-string
)