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)
31 (define-key rmail-mode-map "\C-c\C-s\C-c" 'rmail-sort-by-correspondent)
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)))
101 (defun rmail-sort-messages (reverse keyfunc)
102 "Sort messages of current Rmail file.
103 1st argument REVERSE is non-nil, sort them in reverse order.
104 2nd argument KEYFUNC is called with message number, and should return a key."
105 (let ((buffer-read-only nil)
107 (message "Finding sort keys...")
110 (while (>= rmail-total-messages msgnum)
112 (cons (cons (funcall keyfunc msgnum) ;A sort key.
114 (rmail-msgbeg msgnum) (rmail-msgend msgnum)))
116 (setq msgnum (1+ msgnum))))
117 (or reverse (setq sort-lists (nreverse sort-lists)))
122 (string-lessp (car a) (car b))))))
123 (if reverse (setq sort-lists (nreverse sort-lists)))
124 (message "Reordering buffer...")
125 (delete-region (rmail-msgbeg 1) (rmail-msgend rmail-total-messages))
127 (insert (cdr (car sort-lists)))
128 (setq sort-lists (cdr sort-lists)))
129 (rmail-set-message-counters)
133 (defun rmail-fetch-field (msg field)
134 "Return the value of the header field FIELD of MSG.
135 Arguments are MSG and FIELD."
136 (let ((next (rmail-msgend msg)))
138 (goto-char (rmail-msgbeg msg))
139 (narrow-to-region (if (search-forward "\n*** EOOH ***\n" next t)
143 (progn (search-forward "\n\n" nil t) (point)))
144 (mail-fetch-field field))))
146 ;; Copy of the function gnus-comparable-date in gnus.el
148 (defun rmail-sortable-date-string (date)
149 "Make sortable string by string-lessp from DATE."
150 (let ((month '(("JAN" . " 1")("FEB" . " 2")("MAR" . " 3")
151 ("APR" . " 4")("MAY" . " 5")("JUN" . " 6")
152 ("JUL" . " 7")("AUG" . " 8")("SEP" . " 9")
153 ("OCT" . "10")("NOV" . "11")("DEC" . "12")))
155 ;; Can understand the following styles:
156 ;; (1) 14 Apr 89 03:20:12 GMT
157 ;; (2) Fri, 17 Mar 89 4:01:33 GMT
159 "\\([0-9]+\\) +\\([^ ,]+\\) +\\([0-9]+\\) +\\([0-9:]+\\)" date)
162 (rmail-date-full-year
163 (substring date (match-beginning 3) (match-end 3)))
167 (upcase (substring date (match-beginning 2) (match-end 2))) month))
169 (format "%2d" (string-to-int
171 (match-beginning 1) (match-end 1))))
173 (substring date (match-beginning 4) (match-end 4)))
174 ;; Cannot understand DATE string.
179 (defun rmail-date-full-year (year-string)
180 (if (<= (length year-string) 2)
181 (concat "19" year-string)