Mercurial > emacs
annotate lisp/mail/rmailsort.el @ 389:6e0510766e66
Initial revision
author | Jim Blandy <jimb@redhat.com> |
---|---|
date | Wed, 14 Aug 1991 01:04:47 +0000 |
parents | 00792257e669 |
children | 4cd7543be581 |
rev | line source |
---|---|
90 | 1 ;;; Rmail: sort messages. |
2 ;; Copyright (C) 1990 Masanobu UMEDA | |
92 | 3 ;; umerin@tc.Nagasaki.GO.JP? |
90 | 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) | |
131 | 31 (define-key rmail-mode-map "\C-c\C-s\C-c" 'rmail-sort-by-correspondent) |
148
a099f0c77321
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
131
diff
changeset
|
32 (define-key rmail-mode-map "\C-c\C-s\C-l" 'rmail-sort-by-size-lines) |
90 | 33 |
34 (defun rmail-sort-by-date (reverse) | |
35 "Sort messages of current Rmail file by date. | |
36 If prefix argument REVERSE is non-nil, sort them in reverse order." | |
37 (interactive "P") | |
38 (rmail-sort-messages reverse | |
39 (function | |
40 (lambda (msg) | |
41 (rmail-sortable-date-string | |
42 (rmail-fetch-field msg "Date")))))) | |
43 | |
44 (defun rmail-sort-by-subject (reverse) | |
45 "Sort messages of current Rmail file by subject. | |
46 If prefix argument REVERSE is non-nil, sort them in reverse order." | |
47 (interactive "P") | |
48 (rmail-sort-messages reverse | |
49 (function | |
50 (lambda (msg) | |
51 (let ((key (or (rmail-fetch-field msg "Subject") "")) | |
52 (case-fold-search t)) | |
53 ;; Remove `Re:' | |
54 (if (string-match "^\\(re:[ \t]+\\)*" key) | |
55 (substring key (match-end 0)) key)))))) | |
56 | |
57 (defun rmail-sort-by-author (reverse) | |
58 "Sort messages of current Rmail file by author. | |
59 If prefix argument REVERSE is non-nil, sort them in reverse order." | |
60 (interactive "P") | |
61 (rmail-sort-messages reverse | |
62 (function | |
63 (lambda (msg) | |
64 (mail-strip-quoted-names | |
65 (or (rmail-fetch-field msg "From") | |
66 (rmail-fetch-field msg "Sender") "")))))) | |
67 | |
68 (defun rmail-sort-by-recipient (reverse) | |
69 "Sort messages of current Rmail file by recipient. | |
70 If prefix argument REVERSE is non-nil, sort them in reverse order." | |
71 (interactive "P") | |
72 (rmail-sort-messages reverse | |
73 (function | |
74 (lambda (msg) | |
75 (mail-strip-quoted-names | |
76 (or (rmail-fetch-field msg "To") | |
77 (rmail-fetch-field msg "Apparently-To") "") | |
78 ))))) | |
79 | |
131 | 80 (defun rmail-sort-by-correspondent (reverse) |
81 "Sort messages of current Rmail file by other correspondent. | |
82 If prefix argument REVERSE is non-nil, sort them in reverse order." | |
83 (interactive "P") | |
84 (rmail-sort-messages reverse | |
85 (function | |
86 (lambda (msg) | |
87 (rmail-select-correspondent | |
88 msg | |
89 '("From" "Sender" "To" "Apparently-To")))))) | |
90 | |
91 (defun rmail-select-correspondent (msg fields) | |
92 (let ((ans "")) | |
93 (while (and fields (string= ans "")) | |
94 (setq ans | |
95 (rmail-dont-reply-to | |
96 (mail-strip-quoted-names | |
97 (or (rmail-fetch-field msg (car fields)) "")))) | |
98 (setq fields (cdr fields))) | |
99 ans)) | |
148
a099f0c77321
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
131
diff
changeset
|
100 |
a099f0c77321
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
131
diff
changeset
|
101 (defun rmail-sort-by-size-lines (reverse) |
a099f0c77321
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
131
diff
changeset
|
102 "Sort messages of current Rmail file by message size. |
a099f0c77321
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
131
diff
changeset
|
103 If prefix argument REVERSE is non-nil, sort them in reverse order." |
a099f0c77321
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
131
diff
changeset
|
104 (interactive "P") |
a099f0c77321
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
131
diff
changeset
|
105 (rmail-sort-messages reverse |
a099f0c77321
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
131
diff
changeset
|
106 (function |
a099f0c77321
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
131
diff
changeset
|
107 (lambda (msg) |
a099f0c77321
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
131
diff
changeset
|
108 (format "%9d" |
a099f0c77321
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
131
diff
changeset
|
109 (count-lines (rmail-msgbeg msgnum) |
a099f0c77321
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
131
diff
changeset
|
110 (rmail-msgend msgnum))))))) |
90 | 111 |
112 | |
113 (defun rmail-sort-messages (reverse keyfunc) | |
114 "Sort messages of current Rmail file. | |
115 1st argument REVERSE is non-nil, sort them in reverse order. | |
116 2nd argument KEYFUNC is called with message number, and should return a key." | |
117 (let ((buffer-read-only nil) | |
118 (sort-lists nil)) | |
119 (message "Finding sort keys...") | |
120 (widen) | |
121 (let ((msgnum 1)) | |
122 (while (>= rmail-total-messages msgnum) | |
123 (setq sort-lists | |
124 (cons (cons (funcall keyfunc msgnum) ;A sort key. | |
125 (buffer-substring | |
126 (rmail-msgbeg msgnum) (rmail-msgend msgnum))) | |
127 sort-lists)) | |
148
a099f0c77321
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
131
diff
changeset
|
128 (if (zerop (% msgnum 10)) |
a099f0c77321
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
131
diff
changeset
|
129 (message "Finding sort keys...%d" msgnum)) |
90 | 130 (setq msgnum (1+ msgnum)))) |
131 (or reverse (setq sort-lists (nreverse sort-lists))) | |
132 (setq sort-lists | |
133 (sort sort-lists | |
134 (function | |
135 (lambda (a b) | |
136 (string-lessp (car a) (car b)))))) | |
137 (if reverse (setq sort-lists (nreverse sort-lists))) | |
138 (message "Reordering buffer...") | |
139 (delete-region (rmail-msgbeg 1) (rmail-msgend rmail-total-messages)) | |
148
a099f0c77321
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
131
diff
changeset
|
140 (let ((msgnum 1)) |
a099f0c77321
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
131
diff
changeset
|
141 (while sort-lists |
a099f0c77321
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
131
diff
changeset
|
142 (insert (cdr (car sort-lists))) |
a099f0c77321
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
131
diff
changeset
|
143 (if (zerop (% msgnum 10)) |
a099f0c77321
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
131
diff
changeset
|
144 (message "Reordering buffer...%d" msgnum)) |
a099f0c77321
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
131
diff
changeset
|
145 (setq sort-lists (cdr sort-lists)) |
a099f0c77321
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
131
diff
changeset
|
146 (setq msgnum (1+ msgnum)))) |
90 | 147 (rmail-set-message-counters) |
150 | 148 (rmail-show-message 1))) |
90 | 149 |
150 (defun rmail-fetch-field (msg field) | |
151 "Return the value of the header field FIELD of MSG. | |
152 Arguments are MSG and FIELD." | |
153 (let ((next (rmail-msgend msg))) | |
154 (save-restriction | |
155 (goto-char (rmail-msgbeg msg)) | |
156 (narrow-to-region (if (search-forward "\n*** EOOH ***\n" next t) | |
157 (point) | |
158 (forward-line 1) | |
159 (point)) | |
160 (progn (search-forward "\n\n" nil t) (point))) | |
161 (mail-fetch-field field)))) | |
162 | |
163 ;; Copy of the function gnus-comparable-date in gnus.el | |
164 | |
165 (defun rmail-sortable-date-string (date) | |
166 "Make sortable string by string-lessp from DATE." | |
167 (let ((month '(("JAN" . " 1")("FEB" . " 2")("MAR" . " 3") | |
168 ("APR" . " 4")("MAY" . " 5")("JUN" . " 6") | |
169 ("JUL" . " 7")("AUG" . " 8")("SEP" . " 9") | |
150 | 170 ("OCT" . "10")("NOV" . "11")("DEC" . "12") |
171 ("JANUARY" . " 1") ("FEBRUARY" . " 2") | |
172 ("MARCH" . " 3") ("APRIL" . " 4") | |
173 ("MAY" . " 5") ("JUNE" . " 6") | |
174 ("JULY" . " 7") ("AUGUST" . " 8") | |
175 ("SEPTEMBER" " 9") ("OCTOBER" . "10") | |
176 ("NOVEMBER" "11") ("DECEMBER" . "12"))) | |
90 | 177 (date (or date ""))) |
178 ;; Can understand the following styles: | |
179 ;; (1) 14 Apr 89 03:20:12 GMT | |
180 ;; (2) Fri, 17 Mar 89 4:01:33 GMT | |
181 (if (string-match | |
92 | 182 "\\([0-9]+\\) +\\([^ ,]+\\) +\\([0-9]+\\) +\\([0-9:]+\\)" date) |
90 | 183 (concat |
131 | 184 ;; Year |
185 (rmail-date-full-year | |
186 (substring date (match-beginning 3) (match-end 3))) | |
90 | 187 ;; Month |
188 (cdr | |
189 (assoc | |
190 (upcase (substring date (match-beginning 2) (match-end 2))) month)) | |
191 ;; Day | |
192 (format "%2d" (string-to-int | |
193 (substring date | |
194 (match-beginning 1) (match-end 1)))) | |
195 ;; Time | |
196 (substring date (match-beginning 4) (match-end 4))) | |
197 ;; Cannot understand DATE string. | |
148
a099f0c77321
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
131
diff
changeset
|
198 date))) |
131 | 199 |
200 (defun rmail-date-full-year (year-string) | |
201 (if (<= (length year-string) 2) | |
202 (concat "19" year-string) | |
203 year-string)) |