1 ;;; rmailsort.el --- Rmail: sort messages.
3 ;; Copyright (C) 1990, 1993 Free Software Foundation, Inc.
5 ;; Author: Masanobu UMEDA <umerin@mse.kyutech.ac.jp>
6 ;; Version: !Header: /home/fsf/rms/e19/lisp/RCS/rmailsort.el,v 1.16 1993/11/24 08:08:56 rms Exp !
9 ;; This file is part of XEmacs.
11 ;; XEmacs is free software; you can redistribute it and/or modify it
12 ;; under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; XEmacs is distributed in the hope that it will be useful, but
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ;; General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with XEmacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
30 (autoload 'timezone-make-date-sortable "timezone")
32 ;; Sorting messages in Rmail buffer
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."
38 (rmail-sort-messages reverse
41 (rmail-make-date-sortable
42 (rmail-fetch-field msg "Date"))))))
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."
48 (rmail-sort-messages reverse
51 (let ((key (or (rmail-fetch-field msg "Subject") ""))
54 (if (string-match "^\\(re:[ \t]+\\)*" key)
55 (substring key (match-end 0)) key))))))
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."
61 (rmail-sort-messages reverse
64 (downcase ;Canonical name
65 (mail-strip-quoted-names
66 (or (rmail-fetch-field msg "From")
67 (rmail-fetch-field msg "Sender") "")))))))
69 (defun rmail-sort-by-recipient (reverse)
70 "Sort messages of current Rmail file by recipient.
71 If prefix argument REVERSE is non-nil, sort them in reverse order."
73 (rmail-sort-messages reverse
76 (downcase ;Canonical name
77 (mail-strip-quoted-names
78 (or (rmail-fetch-field msg "To")
79 (rmail-fetch-field msg "Apparently-To") "")
82 (defun rmail-sort-by-correspondent (reverse)
83 "Sort messages of current Rmail file by other correspondent.
84 If prefix argument REVERSE is non-nil, sort them in reverse order."
86 (rmail-sort-messages reverse
89 (rmail-select-correspondent
91 '("From" "Sender" "To" "Apparently-To"))))))
93 (defun rmail-select-correspondent (msg fields)
95 (while (and fields (string= ans ""))
98 (mail-strip-quoted-names
99 (or (rmail-fetch-field msg (car fields)) ""))))
100 (setq fields (cdr fields)))
103 (defun rmail-sort-by-lines (reverse)
104 "Sort messages of current Rmail file by number of lines.
105 If prefix argument REVERSE is non-nil, sort them in reverse order."
107 (rmail-sort-messages reverse
110 (count-lines (rmail-msgbeg msgnum)
111 (rmail-msgend msgnum))))))
115 (defun rmail-sort-messages (reverse keyfun)
116 "Sort messages of current Rmail file.
117 If 1st argument REVERSE is non-nil, sort them in reverse order.
118 2nd argument KEYFUN is called with a message number, and should return a key."
120 ;; If we are in a summary buffer, operate on the Rmail buffer.
121 (if (eq major-mode 'rmail-summary-mode)
122 (set-buffer rmail-buffer))
123 (let ((buffer-read-only nil)
124 (predicate nil) ;< or string-lessp
126 (message "Finding sort keys...")
129 (while (>= rmail-total-messages msgnum)
131 (cons (list (funcall keyfun msgnum) ;Make sorting key
132 (eq rmail-current-message msgnum) ;True if current
133 (aref rmail-message-vector msgnum)
134 (aref rmail-message-vector (1+ msgnum)))
136 (if (zerop (% msgnum 10))
137 (message "Finding sort keys...%d" msgnum))
138 (setq msgnum (1+ msgnum))))
139 (or reverse (setq sort-lists (nreverse sort-lists)))
140 ;; Decide predicate: < or string-lessp
141 (if (numberp (car (car sort-lists))) ;Is a key numeric?
142 (setq predicate (function <))
143 (setq predicate (function string-lessp)))
148 (funcall predicate (car a) (car b))))))
149 (if reverse (setq sort-lists (nreverse sort-lists)))
150 ;; Now we enter critical region. So, keyboard quit is disabled.
151 (message "Reordering messages...")
152 (let ((inhibit-quit t) ;Inhibit quit
153 (current-message nil)
156 ;; There's little hope that we can easily undo after that.
157 (buffer-disable-undo (current-buffer))
158 (goto-char (rmail-msgbeg 1))
159 ;; To force update of all markers.
160 (insert-before-markers ?Z)
162 ;; Now reorder messages.
164 (setq msginfo (car sort-lists))
165 ;; Swap two messages.
166 (insert-buffer-substring
167 (current-buffer) (nth 2 msginfo) (nth 3 msginfo))
168 (delete-region (nth 2 msginfo) (nth 3 msginfo))
169 ;; Is current message?
171 (setq current-message msgnum))
172 (setq sort-lists (cdr sort-lists))
173 (if (zerop (% msgnum 10))
174 (message "Reordering messages...%d" msgnum))
175 (setq msgnum (1+ msgnum)))
176 ;; Delete the garbage inserted before.
180 (rmail-set-message-counters)
181 (rmail-show-message current-message)))))
183 (defun rmail-fetch-field (msg field)
184 "Return the value of the header FIELD of MSG.
185 Arguments are MSG and FIELD."
188 (let ((next (rmail-msgend msg)))
189 (goto-char (rmail-msgbeg msg))
190 (narrow-to-region (if (search-forward "\n*** EOOH ***\n" next t)
194 (progn (search-forward "\n\n" nil t) (point)))
195 (mail-fetch-field field))))
197 (defun rmail-make-date-sortable (date)
198 "Make DATE sortable using the function string-lessp."
199 ;; Assume the default time zone is GMT.
200 (timezone-make-date-sortable date "GMT" "GMT"))
204 ;;; rmailsort.el ends here