Merge from emacs--rel--22
[bpt/emacs.git] / lisp / mail / pmailsort.el
CommitLineData
e131541f
PR
1;;; pmailsort.el --- Pmail: sort messages
2
3;; Copyright (C) 1990, 1993, 1994, 2001, 2002, 2003, 2004,
4;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
5
6;; Author: Masanobu UMEDA <umerin@mse.kyutech.ac.jp>
7;; Maintainer: FSF
8;; Keywords: mail
9
10;; This file is part of GNU Emacs.
11
12;; GNU Emacs is free software: you can redistribute it and/or modify
13;; it under the terms of the GNU General Public License as published by
14;; the Free Software Foundation, either version 3 of the License, or
15;; (at your option) any later version.
16
17;; GNU Emacs is distributed in the hope that it will be useful,
18;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20;; GNU General Public License for more details.
21
22;; You should have received a copy of the GNU General Public License
23;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24
25;;; Commentary:
26
27;;; Code:
28
29(eval-when-compile
30 (require 'mail-utils)
31 (require 'sort)
32 (require 'pmail))
33
34(autoload 'timezone-make-date-sortable "timezone")
35
acdc2006
PR
36(declare-function pmail-dont-reply-to "mail-utils" (destinations))
37(declare-function pmail-desc-get-marker-end "pmailsort" (msgnum))
38(declare-function pmail-desc-get-marker-start "pmailsort" (msgnum))
39(declare-function pmail-update-summary "pmailsum" (&rest ignore))
40
e131541f
PR
41;; Sorting messages in Pmail buffer
42
43;;;###autoload
44(defun pmail-sort-by-date (reverse)
45 "Sort messages of current Pmail file by date.
46If prefix argument REVERSE is non-nil, sort them in reverse order."
47 (interactive "P")
48 (pmail-sort-messages reverse
49 (function
50 (lambda (msg)
51 (pmail-make-date-sortable
52 (pmail-fetch-field msg "Date"))))))
53
54;;;###autoload
55(defun pmail-sort-by-subject (reverse)
56 "Sort messages of current Pmail file by subject.
57If prefix argument REVERSE is non-nil, sort them in reverse order."
58 (interactive "P")
59 (pmail-sort-messages reverse
60 (function
61 (lambda (msg)
62 (let ((key (or (pmail-fetch-field msg "Subject") ""))
63 (case-fold-search t))
64 ;; Remove `Re:'
65 (if (string-match "^\\(re:[ \t]*\\)*" key)
66 (substring key (match-end 0))
67 key))))))
68
69;;;###autoload
70(defun pmail-sort-by-author (reverse)
71 "Sort messages of current Pmail file by author.
72If prefix argument REVERSE is non-nil, sort them in reverse order."
73 (interactive "P")
74 (pmail-sort-messages reverse
75 (function
76 (lambda (msg)
77 (downcase ;Canonical name
78 (mail-strip-quoted-names
79 (or (pmail-fetch-field msg "From")
80 (pmail-fetch-field msg "Sender") "")))))))
81
82;;;###autoload
83(defun pmail-sort-by-recipient (reverse)
84 "Sort messages of current Pmail file by recipient.
85If prefix argument REVERSE is non-nil, sort them in reverse order."
86 (interactive "P")
87 (pmail-sort-messages reverse
88 (function
89 (lambda (msg)
90 (downcase ;Canonical name
91 (mail-strip-quoted-names
92 (or (pmail-fetch-field msg "To")
93 (pmail-fetch-field msg "Apparently-To") "")
94 ))))))
95
96;;;###autoload
97(defun pmail-sort-by-correspondent (reverse)
98 "Sort messages of current Pmail file by other correspondent.
99If prefix argument REVERSE is non-nil, sort them in reverse order."
100 (interactive "P")
101 (pmail-sort-messages reverse
102 (function
103 (lambda (msg)
104 (pmail-select-correspondent
105 msg
106 '("From" "Sender" "To" "Apparently-To"))))))
107
108(defun pmail-select-correspondent (msg fields)
109 (let ((ans ""))
110 (while (and fields (string= ans ""))
111 (setq ans
112 (pmail-dont-reply-to
113 (mail-strip-quoted-names
114 (or (pmail-fetch-field msg (car fields)) ""))))
115 (setq fields (cdr fields)))
116 ans))
117
118;;;###autoload
119(defun pmail-sort-by-lines (reverse)
120 "Sort messages of current Pmail file by number of lines.
121If prefix argument REVERSE is non-nil, sort them in reverse order."
122 (interactive "P")
123 (pmail-sort-messages reverse
124 (function
125 (lambda (msg)
126 (count-lines (pmail-desc-get-start msg)
127 (pmail-desc-get-end msg))))))
128
129;;;###autoload
130(defun pmail-sort-by-labels (reverse labels)
131 "Sort messages of current Pmail file by labels.
132If prefix argument REVERSE is non-nil, sort them in reverse order.
133KEYWORDS is a comma-separated list of labels."
134 (interactive "P\nsSort by labels: ")
135 (or (string-match "[^ \t]" labels)
136 (error "No labels specified"))
137 (setq labels (concat (substring labels (match-beginning 0)) ","))
138 (let (labelvec)
139 (while (string-match "[ \t]*,[ \t]*" labels)
140 (setq labelvec (cons
141 (concat ", ?\\("
142 (substring labels 0 (match-beginning 0))
143 "\\),")
144 labelvec))
145 (setq labels (substring labels (match-end 0))))
146 (setq labelvec (apply 'vector (nreverse labelvec)))
147 (pmail-sort-messages reverse
148 (function
149 (lambda (msg)
150 (let ((n 0))
151 (while (and (< n (length labelvec))
152 (not (pmail-message-labels-p
153 msg (aref labelvec n))))
154 (setq n (1+ n)))
155 n))))))
156\f
157;; Basic functions
158
e131541f
PR
159(defun pmail-sort-messages (reverse keyfun)
160 "Sort messages of current Pmail file.
161If 1st argument REVERSE is non-nil, sort them in reverse order.
1622nd argument KEYFUN is called with a message number, and should return a key."
163 (save-current-buffer
164 ;; If we are in a summary buffer, operate on the Pmail buffer.
165 (if (eq major-mode 'pmail-summary-mode)
166 (set-buffer pmail-buffer))
167 (let ((buffer-read-only nil)
168 (point-offset (- (point) (point-min)))
169 (predicate nil) ;< or string-lessp
170 (sort-lists nil))
171 (message "Finding sort keys...")
172 (pmail-header-show-headers)
173 (widen)
174 (let ((msgnum 1))
175 (while (>= pmail-total-messages msgnum)
176 (setq sort-lists
177 (cons (list (funcall keyfun msgnum) ;Make sorting key
178 (eq pmail-current-message msgnum) ;True if current
179 (pmail-desc-get-marker-start msgnum)
180 (pmail-desc-get-marker-end msgnum))
181 sort-lists))
182 (if (zerop (% msgnum 10))
183 (message "Finding sort keys...%d" msgnum))
184 (setq msgnum (1+ msgnum))))
185 (or reverse (setq sort-lists (nreverse sort-lists)))
186 ;; Decide predicate: < or string-lessp
187 (if (numberp (car (car sort-lists))) ;Is a key numeric?
188 (setq predicate (function <))
189 (setq predicate (function string-lessp)))
190 (setq sort-lists
191 (sort sort-lists
192 (function
193 (lambda (a b)
194 (funcall predicate (car a) (car b))))))
195 (if reverse (setq sort-lists (nreverse sort-lists)))
196 ;; Now we enter critical region. So, keyboard quit is disabled.
197 (message "Reordering messages...")
198 (let ((inhibit-quit t) ;Inhibit quit
199 (current-message nil)
200 (msgnum 1)
201 (msginfo nil))
202 ;; There's little hope that we can easily undo after that.
203 (buffer-disable-undo (current-buffer))
204 (goto-char (pmail-desc-get-start 1))
205 ;; To force update of all markers.
206 (insert-before-markers ?Z)
207 (backward-char 1)
208 ;; Now reorder messages.
209 (while sort-lists
210 (setq msginfo (car sort-lists))
211 ;; Swap two messages.
212 (insert-buffer-substring
213 (current-buffer) (nth 2 msginfo) (nth 3 msginfo))
214 (delete-region (nth 2 msginfo) (nth 3 msginfo))
215 ;; Is current message?
216 (if (nth 1 msginfo)
217 (setq current-message msgnum))
218 (setq sort-lists (cdr sort-lists))
219 (if (zerop (% msgnum 10))
220 (message "Reordering messages...%d" msgnum))
221 (setq msgnum (1+ msgnum)))
222 ;; Delete the garbage inserted before.
223 (delete-char 1)
224 (setq quit-flag nil)
225 (buffer-enable-undo)
226 (goto-char (point-min))
227 (pmail-initialize-messages)
228 (pmail-show-message current-message)
229 (if (pmail-summary-exists)
230 (pmail-select-summary
231 (pmail-update-summary)))))))
232
233;; mbox: ready
234(defun pmail-fetch-field (msg field)
235 "Return the value of the header FIELD of MSG.
236Arguments are MSG and FIELD."
237 (save-restriction
238 (widen)
239 (narrow-to-region
240 (pmail-desc-get-start msg)
241 (pmail-desc-get-end msg))
242 (pmail-header-get-header field)))
243
244(defun pmail-make-date-sortable (date)
245 "Make DATE sortable using the function string-lessp."
246 ;; Assume the default time zone is GMT.
247 (timezone-make-date-sortable date "GMT" "GMT"))
248
249(provide 'pmailsort)
250
0faeefbb 251;; arch-tag: 665da245-f6a7-4115-ad8c-ba19216988d5
e131541f 252;;; pmailsort.el ends here