*** 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 (provide 'rmailsort)
23 (require 'rmail)
24 (require 'sort)
25
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
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."
35 (interactive "P")
36 (rmail-sort-messages reverse
37 (function
38 (lambda (msg)
39 (rmail-sortable-date-string
40 (rmail-fetch-field msg "Date"))))))
41
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."
45 (interactive "P")
46 (rmail-sort-messages reverse
47 (function
48 (lambda (msg)
49 (let ((key (or (rmail-fetch-field msg "Subject") ""))
50 (case-fold-search t))
51 ;; Remove `Re:'
52 (if (string-match "^\\(re:[ \t]+\\)*" key)
53 (substring key (match-end 0)) key))))))
54
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."
58 (interactive "P")
59 (rmail-sort-messages reverse
60 (function
61 (lambda (msg)
62 (mail-strip-quoted-names
63 (or (rmail-fetch-field msg "From")
64 (rmail-fetch-field msg "Sender") ""))))))
65
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."
69 (interactive "P")
70 (rmail-sort-messages reverse
71 (function
72 (lambda (msg)
73 (mail-strip-quoted-names
74 (or (rmail-fetch-field msg "To")
75 (rmail-fetch-field msg "Apparently-To") "")
76 )))))
77
78 \f
79
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)
85 (sort-lists nil))
86 (message "Finding sort keys...")
87 (widen)
88 (let ((msgnum 1))
89 (while (>= rmail-total-messages msgnum)
90 (setq sort-lists
91 (cons (cons (funcall keyfunc msgnum) ;A sort key.
92 (buffer-substring
93 (rmail-msgbeg msgnum) (rmail-msgend msgnum)))
94 sort-lists))
95 (setq msgnum (1+ msgnum))))
96 (or reverse (setq sort-lists (nreverse sort-lists)))
97 (setq sort-lists
98 (sort sort-lists
99 (function
100 (lambda (a b)
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))
105 (while sort-lists
106 (insert (cdr (car sort-lists)))
107 (setq sort-lists (cdr sort-lists)))
108 (rmail-set-message-counters)
109 (rmail-show-message)
110 ))
111
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)))
116 (save-restriction
117 (goto-char (rmail-msgbeg msg))
118 (narrow-to-region (if (search-forward "\n*** EOOH ***\n" next t)
119 (point)
120 (forward-line 1)
121 (point))
122 (progn (search-forward "\n\n" nil t) (point)))
123 (mail-fetch-field field))))
124
125 ;; Copy of the function gnus-comparable-date in gnus.el
126
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")))
133 (date (or date "")))
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
137 (if (string-match
138 "\\([0-9]+\\) +\\([^ ,]+\\) +\\([0-9]+\\) +\\([0-9:]+\\)" date)
139 (concat
140 ;; Year (discarding century)
141 (substring (substring date (match-beginning 3) (match-end 3)) -2)
142 ;; Month
143 (cdr
144 (assoc
145 (upcase (substring date (match-beginning 2) (match-end 2))) month))
146 ;; Day
147 (format "%2d" (string-to-int
148 (substring date
149 (match-beginning 1) (match-end 1))))
150 ;; Time
151 (substring date (match-beginning 4) (match-end 4)))
152 ;; Cannot understand DATE string.
153 date
154 )
155 ))