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.
26 ;; GNUS compatible key bindings.
27 (define-key rmail-mode-map
"\C-c\C-s\C-d" 'rmail-sort-by-date
)
28 (define-key rmail-mode-map
"\C-c\C-s\C-s" 'rmail-sort-by-subject
)
29 (define-key rmail-mode-map
"\C-c\C-s\C-a" 'rmail-sort-by-author
)
30 (define-key rmail-mode-map
"\C-c\C-s\C-r" 'rmail-sort-by-recipient
)
32 (defun rmail-sort-by-date (reverse)
33 "Sort messages of current Rmail file by date.
34 If prefix argument REVERSE is non-nil, sort them in reverse order."
36 (rmail-sort-messages reverse
39 (rmail-sortable-date-string
40 (rmail-fetch-field msg
"Date"))))))
42 (defun rmail-sort-by-subject (reverse)
43 "Sort messages of current Rmail file by subject.
44 If prefix argument REVERSE is non-nil, sort them in reverse order."
46 (rmail-sort-messages reverse
49 (let ((key (or (rmail-fetch-field msg
"Subject") ""))
52 (if (string-match "^\\(re:[ \t]+\\)*" key
)
53 (substring key
(match-end 0)) key
))))))
55 (defun rmail-sort-by-author (reverse)
56 "Sort messages of current Rmail file by author.
57 If prefix argument REVERSE is non-nil, sort them in reverse order."
59 (rmail-sort-messages reverse
62 (mail-strip-quoted-names
63 (or (rmail-fetch-field msg
"From")
64 (rmail-fetch-field msg
"Sender") ""))))))
66 (defun rmail-sort-by-recipient (reverse)
67 "Sort messages of current Rmail file by recipient.
68 If prefix argument REVERSE is non-nil, sort them in reverse order."
70 (rmail-sort-messages reverse
73 (mail-strip-quoted-names
74 (or (rmail-fetch-field msg
"To")
75 (rmail-fetch-field msg
"Apparently-To") "")
80 (defun rmail-sort-messages (reverse keyfunc
)
81 "Sort messages of current Rmail file.
82 1st argument REVERSE is non-nil, sort them in reverse order.
83 2nd argument KEYFUNC is called with message number, and should return a key."
84 (let ((buffer-read-only nil
)
86 (message "Finding sort keys...")
89 (while (>= rmail-total-messages msgnum
)
91 (cons (cons (funcall keyfunc msgnum
) ;A sort key.
93 (rmail-msgbeg msgnum
) (rmail-msgend msgnum
)))
95 (setq msgnum
(1+ msgnum
))))
96 (or reverse
(setq sort-lists
(nreverse sort-lists
)))
101 (string-lessp (car a
) (car b
))))))
102 (if reverse
(setq sort-lists
(nreverse sort-lists
)))
103 (message "Reordering buffer...")
104 (delete-region (rmail-msgbeg 1) (rmail-msgend rmail-total-messages
))
106 (insert (cdr (car sort-lists
)))
107 (setq sort-lists
(cdr sort-lists
)))
108 (rmail-set-message-counters)
112 (defun rmail-fetch-field (msg field
)
113 "Return the value of the header field FIELD of MSG.
114 Arguments are MSG and FIELD."
115 (let ((next (rmail-msgend msg
)))
117 (goto-char (rmail-msgbeg msg
))
118 (narrow-to-region (if (search-forward "\n*** EOOH ***\n" next t
)
122 (progn (search-forward "\n\n" nil t
) (point)))
123 (mail-fetch-field field
))))
125 ;; Copy of the function gnus-comparable-date in gnus.el
127 (defun rmail-sortable-date-string (date)
128 "Make sortable string by string-lessp from DATE."
129 (let ((month '(("JAN" .
" 1")("FEB" .
" 2")("MAR" .
" 3")
130 ("APR" .
" 4")("MAY" .
" 5")("JUN" .
" 6")
131 ("JUL" .
" 7")("AUG" .
" 8")("SEP" .
" 9")
132 ("OCT" .
"10")("NOV" .
"11")("DEC" .
"12")))
134 ;; Can understand the following styles:
135 ;; (1) 14 Apr 89 03:20:12 GMT
136 ;; (2) Fri, 17 Mar 89 4:01:33 GMT
138 "\\([0-9]+\\) +\\([^ ,]+\\) +\\([0-9]+\\) +\\([0-9:]+\\)" date
)
140 ;; Year (discarding century)
141 (substring (substring date
(match-beginning 3) (match-end 3)) -
2)
145 (upcase (substring date
(match-beginning 2) (match-end 2))) month
))
147 (format "%2d" (string-to-int
149 (match-beginning 1) (match-end 1))))
151 (substring date
(match-beginning 4) (match-end 4)))
152 ;; Cannot understand DATE string.