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)
|
90
|
32
|
|
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."
|
|
36 (interactive "P")
|
|
37 (rmail-sort-messages reverse
|
|
38 (function
|
|
39 (lambda (msg)
|
|
40 (rmail-sortable-date-string
|
|
41 (rmail-fetch-field msg "Date"))))))
|
|
42
|
|
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."
|
|
46 (interactive "P")
|
|
47 (rmail-sort-messages reverse
|
|
48 (function
|
|
49 (lambda (msg)
|
|
50 (let ((key (or (rmail-fetch-field msg "Subject") ""))
|
|
51 (case-fold-search t))
|
|
52 ;; Remove `Re:'
|
|
53 (if (string-match "^\\(re:[ \t]+\\)*" key)
|
|
54 (substring key (match-end 0)) key))))))
|
|
55
|
|
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."
|
|
59 (interactive "P")
|
|
60 (rmail-sort-messages reverse
|
|
61 (function
|
|
62 (lambda (msg)
|
|
63 (mail-strip-quoted-names
|
|
64 (or (rmail-fetch-field msg "From")
|
|
65 (rmail-fetch-field msg "Sender") ""))))))
|
|
66
|
|
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."
|
|
70 (interactive "P")
|
|
71 (rmail-sort-messages reverse
|
|
72 (function
|
|
73 (lambda (msg)
|
|
74 (mail-strip-quoted-names
|
|
75 (or (rmail-fetch-field msg "To")
|
|
76 (rmail-fetch-field msg "Apparently-To") "")
|
|
77 )))))
|
|
78
|
131
|
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."
|
|
82 (interactive "P")
|
|
83 (rmail-sort-messages reverse
|
|
84 (function
|
|
85 (lambda (msg)
|
|
86 (rmail-select-correspondent
|
|
87 msg
|
|
88 '("From" "Sender" "To" "Apparently-To"))))))
|
|
89
|
|
90 (defun rmail-select-correspondent (msg fields)
|
|
91 (let ((ans ""))
|
|
92 (while (and fields (string= ans ""))
|
|
93 (setq ans
|
|
94 (rmail-dont-reply-to
|
|
95 (mail-strip-quoted-names
|
|
96 (or (rmail-fetch-field msg (car fields)) ""))))
|
|
97 (setq fields (cdr fields)))
|
|
98 ans))
|
90
|
99
|
|
100
|
|
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)
|
|
106 (sort-lists nil))
|
|
107 (message "Finding sort keys...")
|
|
108 (widen)
|
|
109 (let ((msgnum 1))
|
|
110 (while (>= rmail-total-messages msgnum)
|
|
111 (setq sort-lists
|
|
112 (cons (cons (funcall keyfunc msgnum) ;A sort key.
|
|
113 (buffer-substring
|
|
114 (rmail-msgbeg msgnum) (rmail-msgend msgnum)))
|
|
115 sort-lists))
|
|
116 (setq msgnum (1+ msgnum))))
|
|
117 (or reverse (setq sort-lists (nreverse sort-lists)))
|
|
118 (setq sort-lists
|
|
119 (sort sort-lists
|
|
120 (function
|
|
121 (lambda (a b)
|
|
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))
|
|
126 (while sort-lists
|
|
127 (insert (cdr (car sort-lists)))
|
|
128 (setq sort-lists (cdr sort-lists)))
|
|
129 (rmail-set-message-counters)
|
|
130 (rmail-show-message)
|
|
131 ))
|
|
132
|
|
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)))
|
|
137 (save-restriction
|
|
138 (goto-char (rmail-msgbeg msg))
|
|
139 (narrow-to-region (if (search-forward "\n*** EOOH ***\n" next t)
|
|
140 (point)
|
|
141 (forward-line 1)
|
|
142 (point))
|
|
143 (progn (search-forward "\n\n" nil t) (point)))
|
|
144 (mail-fetch-field field))))
|
|
145
|
|
146 ;; Copy of the function gnus-comparable-date in gnus.el
|
|
147
|
|
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")))
|
|
154 (date (or date "")))
|
|
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
|
|
158 (if (string-match
|
92
|
159 "\\([0-9]+\\) +\\([^ ,]+\\) +\\([0-9]+\\) +\\([0-9:]+\\)" date)
|
90
|
160 (concat
|
131
|
161 ;; Year
|
|
162 (rmail-date-full-year
|
|
163 (substring date (match-beginning 3) (match-end 3)))
|
90
|
164 ;; Month
|
|
165 (cdr
|
|
166 (assoc
|
|
167 (upcase (substring date (match-beginning 2) (match-end 2))) month))
|
|
168 ;; Day
|
|
169 (format "%2d" (string-to-int
|
|
170 (substring date
|
|
171 (match-beginning 1) (match-end 1))))
|
|
172 ;; Time
|
|
173 (substring date (match-beginning 4) (match-end 4)))
|
|
174 ;; Cannot understand DATE string.
|
|
175 date
|
|
176 )
|
|
177 ))
|
131
|
178
|
|
179 (defun rmail-date-full-year (year-string)
|
|
180 (if (<= (length year-string) 2)
|
|
181 (concat "19" year-string)
|
|
182 year-string))
|