* doc/misc/eshell.texi: Fill most of the missing sections.
[bpt/emacs.git] / lisp / mail / rmailsum.el
1 ;;; rmailsum.el --- make summary buffers for the mail reader
2
3 ;; Copyright (C) 1985, 1993-1996, 2000-2013 Free Software Foundation,
4 ;; Inc.
5
6 ;; Maintainer: FSF
7 ;; Keywords: mail
8 ;; Package: rmail
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 ;; Extended by Bob Weiner of Motorola
28 ;; Provided all commands from rmail-mode in rmail-summary-mode and made key
29 ;; bindings in both modes wholly compatible.
30
31 ;;; Code:
32
33 ;; For rmail-select-summary.
34 (require 'rmail)
35 (require 'rfc2047)
36
37 (defcustom rmail-summary-scroll-between-messages t
38 "Non-nil means Rmail summary scroll commands move between messages.
39 That is, after `rmail-summary-scroll-msg-up' reaches the end of a
40 message, it moves to the next message; and similarly for
41 `rmail-summary-scroll-msg-down'."
42 :type 'boolean
43 :group 'rmail-summary)
44
45 ;; FIXME could do with a :set function that regenerates the summary
46 ;; and updates rmail-summary-vector.
47 (defcustom rmail-summary-line-count-flag t
48 "Non-nil means Rmail summary should show the number of lines in each message.
49 Setting this option to nil might speed up the generation of summaries."
50 :type 'boolean
51 :group 'rmail-summary)
52
53 (defvar rmail-summary-font-lock-keywords
54 '(("^.....D.*" . font-lock-string-face) ; Deleted.
55 ("^.....-.*" . font-lock-type-face) ; Unread.
56 ;; Neither of the below will be highlighted if either of the above are:
57 ("^.....[^D-] \\(......\\)" 1 font-lock-keyword-face) ; Date.
58 ("{ \\([^\n}]+\\) }" 1 font-lock-comment-face)) ; Labels.
59 "Additional expressions to highlight in Rmail Summary mode.")
60
61 (defvar rmail-summary-redo nil
62 "(FUNCTION . ARGS) to regenerate this Rmail summary buffer.")
63
64 (defvar rmail-summary-overlay nil
65 "Overlay used to highlight the current message in the Rmail summary.")
66 (put 'rmail-summary-overlay 'permanent-local t)
67
68 (defvar rmail-summary-mode-map
69 (let ((map (make-keymap)))
70 (suppress-keymap map)
71 (define-key map [mouse-2] 'rmail-summary-mouse-goto-message)
72 (define-key map "a" 'rmail-summary-add-label)
73 (define-key map "b" 'rmail-summary-bury)
74 (define-key map "c" 'rmail-summary-continue)
75 (define-key map "d" 'rmail-summary-delete-forward)
76 (define-key map "\C-d" 'rmail-summary-delete-backward)
77 (define-key map "e" 'rmail-summary-edit-current-message)
78 (define-key map "f" 'rmail-summary-forward)
79 (define-key map "g" 'rmail-summary-get-new-mail)
80 (define-key map "h" 'rmail-summary)
81 (define-key map "i" 'rmail-summary-input)
82 (define-key map "j" 'rmail-summary-goto-msg)
83 (define-key map "\C-m" 'rmail-summary-goto-msg)
84 (define-key map "k" 'rmail-summary-kill-label)
85 (define-key map "l" 'rmail-summary-by-labels)
86 (define-key map "\e\C-h" 'rmail-summary)
87 (define-key map "\e\C-l" 'rmail-summary-by-labels)
88 (define-key map "\e\C-r" 'rmail-summary-by-recipients)
89 (define-key map "\e\C-s" 'rmail-summary-by-regexp)
90 ;; `f' for "from".
91 (define-key map "\e\C-f" 'rmail-summary-by-senders)
92 (define-key map "\e\C-t" 'rmail-summary-by-topic)
93 (define-key map "m" 'rmail-summary-mail)
94 (define-key map "\M-m" 'rmail-summary-retry-failure)
95 (define-key map "n" 'rmail-summary-next-msg)
96 (define-key map "\en" 'rmail-summary-next-all)
97 (define-key map "\e\C-n" 'rmail-summary-next-labeled-message)
98 (define-key map "o" 'rmail-summary-output)
99 (define-key map "\C-o" 'rmail-summary-output-as-seen)
100 (define-key map "p" 'rmail-summary-previous-msg)
101 (define-key map "\ep" 'rmail-summary-previous-all)
102 (define-key map "\e\C-p" 'rmail-summary-previous-labeled-message)
103 (define-key map "q" 'rmail-summary-quit)
104 (define-key map "Q" 'rmail-summary-wipe)
105 (define-key map "r" 'rmail-summary-reply)
106 (define-key map "s" 'rmail-summary-expunge-and-save)
107 ;; See rms's comment in rmail.el
108 ;; (define-key map "\er" 'rmail-summary-search-backward)
109 (define-key map "\es" 'rmail-summary-search)
110 (define-key map "t" 'rmail-summary-toggle-header)
111 (define-key map "u" 'rmail-summary-undelete)
112 (define-key map "\M-u" 'rmail-summary-undelete-many)
113 (define-key map "x" 'rmail-summary-expunge)
114 (define-key map "w" 'rmail-summary-output-body)
115 (define-key map "v" 'rmail-mime)
116 (define-key map "." 'rmail-summary-beginning-of-message)
117 (define-key map "/" 'rmail-summary-end-of-message)
118 (define-key map "<" 'rmail-summary-first-message)
119 (define-key map ">" 'rmail-summary-last-message)
120 (define-key map " " 'rmail-summary-scroll-msg-up)
121 (define-key map "\177" 'rmail-summary-scroll-msg-down)
122 (define-key map "?" 'describe-mode)
123 (define-key map "\C-c\C-n" 'rmail-summary-next-same-subject)
124 (define-key map "\C-c\C-p" 'rmail-summary-previous-same-subject)
125 (define-key map "\C-c\C-s\C-d" 'rmail-summary-sort-by-date)
126 (define-key map "\C-c\C-s\C-s" 'rmail-summary-sort-by-subject)
127 (define-key map "\C-c\C-s\C-a" 'rmail-summary-sort-by-author)
128 (define-key map "\C-c\C-s\C-r" 'rmail-summary-sort-by-recipient)
129 (define-key map "\C-c\C-s\C-c" 'rmail-summary-sort-by-correspondent)
130 (define-key map "\C-c\C-s\C-l" 'rmail-summary-sort-by-lines)
131 (define-key map "\C-c\C-s\C-k" 'rmail-summary-sort-by-labels)
132 (define-key map "\C-x\C-s" 'rmail-summary-save-buffer)
133
134 ;; Menu bar bindings.
135
136 (define-key map [menu-bar] (make-sparse-keymap))
137
138 (define-key map [menu-bar classify]
139 (cons "Classify" (make-sparse-keymap "Classify")))
140
141 (define-key map [menu-bar classify output-menu]
142 '("Output (Rmail Menu)..." . rmail-summary-output-menu))
143
144 (define-key map [menu-bar classify input-menu]
145 '("Input Rmail File (menu)..." . rmail-input-menu))
146
147 (define-key map [menu-bar classify input-menu]
148 '(nil))
149
150 (define-key map [menu-bar classify output-menu]
151 '(nil))
152
153 (define-key map [menu-bar classify output-body]
154 '("Output body..." . rmail-summary-output-body))
155
156 (define-key map [menu-bar classify output-inbox]
157 '("Output..." . rmail-summary-output))
158
159 (define-key map [menu-bar classify output]
160 '("Output as seen..." . rmail-summary-output-as-seen))
161
162 (define-key map [menu-bar classify kill-label]
163 '("Kill Label..." . rmail-summary-kill-label))
164
165 (define-key map [menu-bar classify add-label]
166 '("Add Label..." . rmail-summary-add-label))
167
168 (define-key map [menu-bar summary]
169 (cons "Summary" (make-sparse-keymap "Summary")))
170
171 (define-key map [menu-bar summary senders]
172 '("By Senders..." . rmail-summary-by-senders))
173
174 (define-key map [menu-bar summary labels]
175 '("By Labels..." . rmail-summary-by-labels))
176
177 (define-key map [menu-bar summary recipients]
178 '("By Recipients..." . rmail-summary-by-recipients))
179
180 (define-key map [menu-bar summary topic]
181 '("By Topic..." . rmail-summary-by-topic))
182
183 (define-key map [menu-bar summary regexp]
184 '("By Regexp..." . rmail-summary-by-regexp))
185
186 (define-key map [menu-bar summary all]
187 '("All" . rmail-summary))
188
189 (define-key map [menu-bar mail]
190 (cons "Mail" (make-sparse-keymap "Mail")))
191
192 (define-key map [menu-bar mail rmail-summary-get-new-mail]
193 '("Get New Mail" . rmail-summary-get-new-mail))
194
195 (define-key map [menu-bar mail lambda]
196 '("----"))
197
198 (define-key map [menu-bar mail continue]
199 '("Continue" . rmail-summary-continue))
200
201 (define-key map [menu-bar mail resend]
202 '("Re-send..." . rmail-summary-resend))
203
204 (define-key map [menu-bar mail forward]
205 '("Forward" . rmail-summary-forward))
206
207 (define-key map [menu-bar mail retry]
208 '("Retry" . rmail-summary-retry-failure))
209
210 (define-key map [menu-bar mail reply]
211 '("Reply" . rmail-summary-reply))
212
213 (define-key map [menu-bar mail mail]
214 '("Mail" . rmail-summary-mail))
215
216 (define-key map [menu-bar delete]
217 (cons "Delete" (make-sparse-keymap "Delete")))
218
219 (define-key map [menu-bar delete expunge/save]
220 '("Expunge/Save" . rmail-summary-expunge-and-save))
221
222 (define-key map [menu-bar delete expunge]
223 '("Expunge" . rmail-summary-expunge))
224
225 (define-key map [menu-bar delete undelete]
226 '("Undelete" . rmail-summary-undelete))
227
228 (define-key map [menu-bar delete delete]
229 '("Delete" . rmail-summary-delete-forward))
230
231 (define-key map [menu-bar move]
232 (cons "Move" (make-sparse-keymap "Move")))
233
234 (define-key map [menu-bar move search-back]
235 '("Search Back..." . rmail-summary-search-backward))
236
237 (define-key map [menu-bar move search]
238 '("Search..." . rmail-summary-search))
239
240 (define-key map [menu-bar move previous]
241 '("Previous Nondeleted" . rmail-summary-previous-msg))
242
243 (define-key map [menu-bar move next]
244 '("Next Nondeleted" . rmail-summary-next-msg))
245
246 (define-key map [menu-bar move last]
247 '("Last" . rmail-summary-last-message))
248
249 (define-key map [menu-bar move first]
250 '("First" . rmail-summary-first-message))
251
252 (define-key map [menu-bar move previous]
253 '("Previous" . rmail-summary-previous-all))
254
255 (define-key map [menu-bar move next]
256 '("Next" . rmail-summary-next-all))
257 map)
258 "Keymap used in Rmail summary mode.")
259
260 ;; Entry points for making a summary buffer.
261
262 ;; Regenerate the contents of the summary
263 ;; using the same selection criterion as last time.
264 ;; M-x revert-buffer in a summary buffer calls this function.
265 (defun rmail-update-summary (&rest ignore)
266 (apply (car rmail-summary-redo) (cdr rmail-summary-redo)))
267
268 ;;;###autoload
269 (defun rmail-summary ()
270 "Display a summary of all messages, one line per message."
271 (interactive)
272 (rmail-new-summary "All" '(rmail-summary) nil))
273
274 ;;;###autoload
275 (defun rmail-summary-by-labels (labels)
276 "Display a summary of all messages with one or more LABELS.
277 LABELS should be a string containing the desired labels, separated by commas."
278 (interactive "sLabels to summarize by: ")
279 (if (string= labels "")
280 (setq labels (or rmail-last-multi-labels
281 (error "No label specified"))))
282 (setq rmail-last-multi-labels labels)
283 (rmail-new-summary (concat "labels " labels)
284 (list 'rmail-summary-by-labels labels)
285 'rmail-message-labels-p
286 (concat " \\("
287 (mail-comma-list-regexp labels)
288 "\\)\\(,\\|\\'\\)")))
289
290 ;; FIXME "a string of regexps separated by commas" makes no sense because:
291 ;; i) it's pointless (you can just use \\|)
292 ;; ii) it's broken (you can't specify a literal comma)
293 ;; rmail-summary-by-topic and rmail-summary-by-senders have the same issue.
294 ;;;###autoload
295 (defun rmail-summary-by-recipients (recipients &optional primary-only)
296 "Display a summary of all messages with the given RECIPIENTS.
297 Normally checks the To, From and Cc fields of headers;
298 but if PRIMARY-ONLY is non-nil (prefix arg given),
299 only look in the To and From fields.
300 RECIPIENTS is a string of regexps separated by commas."
301 (interactive "sRecipients to summarize by: \nP")
302 (rmail-new-summary
303 (concat "recipients " recipients)
304 (list 'rmail-summary-by-recipients recipients primary-only)
305 'rmail-message-recipients-p
306 (mail-comma-list-regexp recipients) primary-only))
307
308 (defun rmail-message-recipients-p (msg recipients &optional primary-only)
309 (rmail-apply-in-message msg 'rmail-message-recipients-p-1
310 recipients primary-only))
311
312 (defun rmail-message-recipients-p-1 (recipients &optional primary-only)
313 ;; mail-fetch-field does not care where it starts from.
314 (narrow-to-region (point) (progn (search-forward "\n\n") (point)))
315 (or (string-match recipients (or (mail-fetch-field "To") ""))
316 (string-match recipients (or (mail-fetch-field "From") ""))
317 (if (not primary-only)
318 (string-match recipients (or (mail-fetch-field "Cc") "")))))
319
320 ;; FIXME I find this a non-obvious name for what this function does.
321 ;; Also, the optional WHOLE-MESSAGE argument of r-s-by-topic would
322 ;; seem more natural here.
323 ;;;###autoload
324 (defun rmail-summary-by-regexp (regexp)
325 "Display a summary of all messages according to regexp REGEXP.
326 If the regular expression is found in the header of the message
327 \(including in the date and other lines, as well as the subject line),
328 Emacs will list the message in the summary."
329 (interactive "sRegexp to summarize by: ")
330 (if (string= regexp "")
331 (setq regexp (or rmail-last-regexp
332 (error "No regexp specified"))))
333 (setq rmail-last-regexp regexp)
334 (rmail-new-summary (concat "regexp " regexp)
335 (list 'rmail-summary-by-regexp regexp)
336 'rmail-message-regexp-p
337 regexp))
338
339 (defun rmail-message-regexp-p (msg regexp)
340 "Return t, if for message number MSG, regexp REGEXP matches in the header."
341 (rmail-apply-in-message msg 'rmail-message-regexp-p-1 msg regexp))
342
343 (defun rmail-message-regexp-p-1 (msg regexp)
344 ;; Search functions can expect to start from the beginning.
345 (narrow-to-region (point) (save-excursion (search-forward "\n\n") (point)))
346 (if (and rmail-enable-mime
347 rmail-search-mime-header-function)
348 (funcall rmail-search-mime-header-function msg regexp (point))
349 (re-search-forward regexp nil t)))
350
351 ;;;###autoload
352 (defun rmail-summary-by-topic (subject &optional whole-message)
353 "Display a summary of all messages with the given SUBJECT.
354 Normally checks just the Subject field of headers; but with prefix
355 argument WHOLE-MESSAGE is non-nil, looks in the whole message.
356 SUBJECT is a string of regexps separated by commas."
357 (interactive
358 ;; We quote the default subject, because if it contains regexp
359 ;; special characters (eg "?"), it can fail to match itself. (Bug#2333)
360 (let* ((subject (regexp-quote (rmail-simplified-subject)))
361 (prompt (concat "Topics to summarize by (regexp"
362 (if subject ", default current subject" "")
363 "): ")))
364 (list (read-string prompt nil nil subject) current-prefix-arg)))
365 (rmail-new-summary
366 (concat "about " subject)
367 (list 'rmail-summary-by-topic subject whole-message)
368 'rmail-message-subject-p
369 (mail-comma-list-regexp subject) whole-message))
370
371 (defun rmail-message-subject-p (msg subject &optional whole-message)
372 (if whole-message
373 (rmail-apply-in-message msg 're-search-forward subject nil t)
374 (string-match subject (rmail-simplified-subject msg))))
375
376 ;;;###autoload
377 (defun rmail-summary-by-senders (senders)
378 "Display a summary of all messages whose \"From\" field matches SENDERS.
379 SENDERS is a string of regexps separated by commas."
380 (interactive "sSenders to summarize by: ")
381 (rmail-new-summary
382 (concat "senders " senders)
383 (list 'rmail-summary-by-senders senders)
384 'rmail-message-senders-p
385 (mail-comma-list-regexp senders)))
386
387 (defun rmail-message-senders-p (msg senders)
388 (string-match senders (or (rmail-get-header "From" msg) "")))
389 \f
390 ;; General making of a summary buffer.
391
392 (defvar rmail-summary-symbol-number 0)
393
394 (defvar rmail-new-summary-line-count)
395
396 (defun rmail-new-summary (desc redo func &rest args)
397 "Create a summary of selected messages.
398 DESC makes part of the mode line of the summary buffer. REDO is form ...
399 For each message, FUNC is applied to the message number and ARGS...
400 and if the result is non-nil, that message is included.
401 nil for FUNCTION means all messages."
402 (message "Computing summary lines...")
403 (unless rmail-buffer
404 (error "No RMAIL buffer found"))
405 (let (mesg was-in-summary sumbuf)
406 (if (eq major-mode 'rmail-summary-mode)
407 (setq was-in-summary t))
408 (with-current-buffer rmail-buffer
409 (setq rmail-summary-buffer (rmail-new-summary-1 desc redo func args)
410 ;; r-s-b is buffer-local.
411 sumbuf rmail-summary-buffer
412 mesg rmail-current-message))
413 ;; Now display the summary buffer and go to the right place in it.
414 (unless was-in-summary
415 (if (and (one-window-p)
416 pop-up-windows
417 (not pop-up-frames))
418 ;; If there is just one window, put the summary on the top.
419 (progn
420 (split-window (selected-window) rmail-summary-window-size)
421 (select-window (next-window (frame-first-window)))
422 (rmail-pop-to-buffer sumbuf)
423 ;; If pop-to-buffer did not use that window, delete that
424 ;; window. (This can happen if it uses another frame.)
425 (if (not (eq sumbuf (window-buffer (frame-first-window))))
426 (delete-other-windows)))
427 (rmail-pop-to-buffer sumbuf))
428 (set-buffer rmail-buffer)
429 ;; This is how rmail makes the summary buffer reappear.
430 ;; We do this here to make the window the proper size.
431 (rmail-select-summary nil)
432 (set-buffer sumbuf))
433 (rmail-summary-goto-msg mesg t t)
434 (rmail-summary-construct-io-menu)
435 (message "Computing summary lines...done")))
436
437 (defun rmail-new-summary-1 (description form function args)
438 "Filter messages to obtain summary lines.
439 DESCRIPTION is added to the mode line.
440
441 Return the summary buffer by invoking FUNCTION on each message
442 passing the message number and ARGS...
443
444 REDO is a form ...
445
446 The current buffer must be a Rmail buffer either containing a
447 collection of mbox formatted messages or displaying a single
448 message."
449 (let ((summary-msgs ())
450 (rmail-new-summary-line-count 0)
451 (sumbuf (rmail-get-create-summary-buffer)))
452 ;; Scan the messages, getting their summary strings
453 ;; and putting the list of them in SUMMARY-MSGS.
454 (let ((msgnum 1)
455 (main-buffer (current-buffer))
456 (total rmail-total-messages)
457 (inhibit-read-only t))
458 (save-excursion
459 ;; Go where the mbox text is.
460 (if (rmail-buffers-swapped-p)
461 (set-buffer rmail-view-buffer))
462 (let ((old-min (point-min-marker))
463 (old-max (point-max-marker)))
464 (unwind-protect
465 ;; Can't use save-restriction here; that doesn't work if we
466 ;; plan to modify text outside the original restriction.
467 (save-excursion
468 (widen)
469 (goto-char (point-min))
470 (while (>= total msgnum)
471 ;; Go back to the Rmail buffer so
472 ;; so FUNCTION and rmail-get-summary can see its local vars.
473 (with-current-buffer main-buffer
474 ;; First test whether to include this message.
475 (if (or (null function)
476 (apply function msgnum args))
477 (setq summary-msgs
478 (cons (cons msgnum (rmail-get-summary msgnum))
479 summary-msgs))))
480 (setq msgnum (1+ msgnum))
481 ;; Provide a periodic User progress message.
482 (if (and (not (zerop rmail-new-summary-line-count))
483 (zerop (% rmail-new-summary-line-count 10)))
484 (message "Computing summary lines...%d"
485 rmail-new-summary-line-count)))
486 (setq summary-msgs (nreverse summary-msgs)))
487 (narrow-to-region old-min old-max)))))
488 ;; Temporarily, while summary buffer is unfinished,
489 ;; we "don't have" a summary.
490 (setq rmail-summary-buffer nil)
491 ;; I have not a clue what this clause is doing. If you read this
492 ;; chunk of code and have a clue, then please email that clue to
493 ;; pmr@pajato.com
494 (if rmail-enable-mime
495 (with-current-buffer rmail-buffer
496 (setq rmail-summary-buffer nil)))
497 (save-excursion
498 (let ((rbuf (current-buffer))
499 (total rmail-total-messages))
500 (set-buffer sumbuf)
501 ;; Set up the summary buffer's contents.
502 (let ((buffer-read-only nil))
503 (erase-buffer)
504 (while summary-msgs
505 (princ (cdr (car summary-msgs)) sumbuf)
506 (setq summary-msgs (cdr summary-msgs)))
507 (goto-char (point-min)))
508 ;; Set up the rest of its state and local variables.
509 (setq buffer-read-only t)
510 (rmail-summary-mode)
511 (make-local-variable 'minor-mode-alist)
512 (setq minor-mode-alist (list (list t (concat ": " description))))
513 (setq rmail-buffer rbuf
514 rmail-summary-redo form
515 rmail-total-messages total)))
516 sumbuf))
517
518 (defun rmail-get-create-summary-buffer ()
519 "Return the Rmail summary buffer.
520 If necessary, it is created and undo is disabled."
521 (if (and rmail-summary-buffer (buffer-name rmail-summary-buffer))
522 rmail-summary-buffer
523 (let ((buff (generate-new-buffer (concat (buffer-name) "-summary"))))
524 (with-current-buffer buff
525 (setq buffer-undo-list t))
526 buff)))
527
528 \f
529 ;; Low levels of generating a summary.
530
531 (defun rmail-get-summary (msgnum)
532 "Return the summary line for message MSGNUM.
533 The mbox buffer must be current when you call this function
534 even if its text is swapped.
535
536 If the message has a summary line already, it will be stored in
537 the message as a header and simply returned, otherwise the
538 summary line is created, saved in the message header, cached and
539 returned.
540
541 The current buffer contains the unrestricted message collection."
542 (let ((line (aref rmail-summary-vector (1- msgnum))))
543 (unless line
544 ;; Register a summary line for MSGNUM.
545 (setq rmail-new-summary-line-count (1+ rmail-new-summary-line-count)
546 line (rmail-create-summary-line msgnum))
547 ;; Cache the summary line for use during this Rmail session.
548 (aset rmail-summary-vector (1- msgnum) line))
549 line))
550
551 (defcustom rmail-summary-line-decoder (function rfc2047-decode-string)
552 "Function to decode a Rmail summary line.
553 It receives the summary line for one message as a string
554 and should return the decoded string.
555
556 By default, it is `rfc2047-decode-string', which decodes MIME-encoded
557 subject."
558 :type 'function
559 :version "23.3"
560 :group 'rmail-summary)
561
562 (defun rmail-create-summary-line (msgnum)
563 "Return the summary line for message MSGNUM.
564 Obtain the message summary from the header if it is available
565 otherwise create it and store it in the message header.
566
567 The mbox buffer must be current when you call this function
568 even if its text is swapped."
569 (let ((beg (rmail-msgbeg msgnum))
570 (end (rmail-msgend msgnum))
571 (deleted (rmail-message-deleted-p msgnum))
572 ;; Does not work (swapped?)
573 ;;; (unseen (rmail-message-unseen-p msgnum))
574 unseen lines)
575 (save-excursion
576 ;; Switch to the buffer that has the whole mbox text.
577 (if (rmail-buffers-swapped-p)
578 (set-buffer rmail-view-buffer))
579 ;; Now we can compute the line count.
580 (if rmail-summary-line-count-flag
581 (setq lines (count-lines beg end)))
582 ;; Narrow to the message header.
583 (save-excursion
584 (save-restriction
585 (widen)
586 (goto-char beg)
587 (if (search-forward "\n\n" end t)
588 (progn
589 (narrow-to-region beg (point))
590 ;; Replace rmail-message-unseen-p from above.
591 (goto-char beg)
592 (setq unseen (and (search-forward
593 (concat rmail-attribute-header ": ") nil t)
594 (looking-at "......U")))
595 ;; Generate a status line from the message.
596 (rmail-create-summary msgnum deleted unseen lines))
597 (rmail-error-bad-format msgnum)))))))
598
599 ;; FIXME this is now unused.
600 ;; The intention was to display in the summary something like {E}
601 ;; for an edited messaged, similarly for answered, etc.
602 ;; But that conflicts with the previous rmail usage, where
603 ;; any user-defined { labels } occupied this space.
604 ;; So whilst it would be nice to have this information in the summary,
605 ;; it would need to go somewhere else.
606 (defun rmail-get-summary-status ()
607 "Return a coded string wrapped in curly braces denoting the status.
608
609 The current buffer must already be narrowed to the message headers for
610 the message being processed."
611 (let ((status (mail-fetch-field rmail-attribute-header))
612 (index 0)
613 (result "")
614 char)
615 ;; Strip off the read/unread and the deleted attribute which are
616 ;; handled separately.
617 (setq status
618 (if status
619 (concat (substring status 0 1) (substring status 2 6))
620 ""))
621 (while (< index (length status))
622 (unless (string= "-" (setq char (substring status index (1+ index))))
623 (setq result (concat result char)))
624 (setq index (1+ index)))
625 (when (> (length result) 0)
626 (setq result (concat "{" result "}")))
627 result))
628
629 (autoload 'rmail-make-label "rmailkwd")
630
631 (defun rmail-get-summary-labels ()
632 "Return a string wrapped in curly braces with the current message labels.
633 Returns nil if there are no labels. The current buffer must
634 already be narrowed to the message headers for the message being
635 processed."
636 (let ((labels (mail-fetch-field rmail-keyword-header)))
637 (and labels
638 (not (string-equal labels ""))
639 (progn
640 ;; Intern so that rmail-read-label can offer completion.
641 (mapc 'rmail-make-label (split-string labels ", "))
642 (format "{ %s } " labels)))))
643
644 (defun rmail-create-summary (msgnum deleted unseen lines)
645 "Return the summary line for message MSGNUM.
646 The current buffer should already be narrowed to the header for that message.
647 It could be either buffer, so don't access Rmail local variables.
648 DELETED is t if this message is marked deleted.
649 UNSEEN is t if it is marked unseen.
650 LINES is the number of lines in the message (if we should display that)
651 or else nil."
652 (goto-char (point-min))
653 (let ((line (rmail-header-summary))
654 (labels (rmail-get-summary-labels))
655 pos status prefix basic-start basic-end linecount-string)
656
657 (setq linecount-string
658 (cond
659 ((not lines) " ")
660 ((<= lines 9) (format " [%d]" lines))
661 ((<= lines 99) (format " [%d]" lines))
662 ((<= lines 999) (format " [%d]" lines))
663 ((<= lines 9999) (format " [%dk]" (/ lines 1000)))
664 ((<= lines 99999) (format " [%dk]" (/ lines 1000)))
665 (t (format "[%dk]" (/ lines 1000)))))
666
667 (setq status (cond
668 (deleted ?D)
669 (unseen ?-)
670 (t ? ))
671 prefix (format "%5d%c " msgnum status)
672 basic-start (car line)
673 basic-end (cadr line))
674 (funcall rmail-summary-line-decoder
675 (concat prefix basic-start linecount-string " "
676 labels basic-end))))
677
678 (defun rmail-header-summary ()
679 "Return a message summary based on the message headers.
680 The value is a list of two strings, the first and second parts of the summary.
681
682 The current buffer must already be narrowed to the message headers for
683 the message being processed."
684 (goto-char (point-min))
685 (list
686 (concat (save-excursion
687 (if (not (re-search-forward "^Date:" nil t))
688 " "
689 ;; Match month names case-insensitively
690 (cond ((let ((case-fold-search t))
691 (re-search-forward "\\([^0-9:]\\)\\([0-3]?[0-9]\\)\\([- \t_]+\\)\\([adfjmnos][aceopu][bcglnprtvy]\\)"
692 (line-end-position) t))
693 (format "%2d-%3s"
694 (string-to-number (buffer-substring
695 (match-beginning 2)
696 (match-end 2)))
697 (buffer-substring
698 (match-beginning 4) (match-end 4))))
699 ((let ((case-fold-search t))
700 (re-search-forward "\\([^a-z]\\)\\([adfjmnos][acepou][bcglnprtvy]\\)\\([-a-z \t_]*\\)\\([0-9][0-9]?\\)"
701 (line-end-position) t))
702 (format "%2d-%3s"
703 (string-to-number (buffer-substring
704 (match-beginning 4)
705 (match-end 4)))
706 (buffer-substring
707 (match-beginning 2) (match-end 2))))
708 ((re-search-forward "\\(19\\|20\\)\\([0-9][0-9]\\)-\\([01][0-9]\\)-\\([0-3][0-9]\\)"
709 (line-end-position) t)
710 (format "%2s%2s%2s"
711 (buffer-substring
712 (match-beginning 2) (match-end 2))
713 (buffer-substring
714 (match-beginning 3) (match-end 3))
715 (buffer-substring
716 (match-beginning 4) (match-end 4))))
717 (t "??????"))))
718 " "
719 (save-excursion
720 (let* ((from (and (re-search-forward "^From:[ \t]*" nil t)
721 (mail-strip-quoted-names
722 (buffer-substring
723 (1- (point))
724 ;; Get all the lines of the From field
725 ;; so that we get a whole comment if there is one,
726 ;; so that mail-strip-quoted-names can discard it.
727 (let ((opoint (point)))
728 (while (progn (forward-line 1)
729 (looking-at "[ \t]")))
730 ;; Back up over newline, then trailing spaces or tabs
731 (forward-char -1)
732 (skip-chars-backward " \t")
733 (point))))))
734 len mch lo)
735 (if (or (null from)
736 (string-match
737 (or rmail-user-mail-address-regexp
738 (concat "^\\("
739 (regexp-quote (user-login-name))
740 "\\($\\|@\\)\\|"
741 (regexp-quote
742 ;; Don't lose if run from init file
743 ;; where user-mail-address is not
744 ;; set yet.
745 (or user-mail-address
746 (concat (user-login-name) "@"
747 (or mail-host-address
748 (system-name)))))
749 "\\>\\)"))
750 from))
751 ;; No From field, or it's this user.
752 (save-excursion
753 (goto-char (point-min))
754 (if (not (re-search-forward "^To:[ \t]*" nil t))
755 nil
756 (setq from
757 (concat "to: "
758 (mail-strip-quoted-names
759 (buffer-substring
760 (point)
761 (progn (end-of-line)
762 (skip-chars-backward " \t")
763 (point)))))))))
764 (if (null from)
765 " "
766 ;; We are going to return only 25 characters of the
767 ;; address, so make sure it is RFC2047 decoded before
768 ;; taking its substring. This is important when the address is not on the same line as the name, e.g.:
769 ;; To: =?UTF-8?Q?=C5=A0t=C4=9Bp=C3=A1n_?= =?UTF-8?Q?N=C4=9Bmec?=
770 ;; <stepnem@gmail.com>
771 (setq from (rfc2047-decode-string from))
772 (setq len (length from))
773 (setq mch (string-match "[@%]" from))
774 (format "%25s"
775 (if (or (not mch) (<= len 25))
776 (substring from (max 0 (- len 25)))
777 (substring from
778 (setq lo (cond ((< (- mch 14) 0) 0)
779 ((< len (+ mch 11))
780 (- len 25))
781 (t (- mch 14))))
782 (min len (+ lo 25)))))))))
783 (concat (if (re-search-forward "^Subject:" nil t)
784 (let (pos str)
785 (skip-chars-forward " \t")
786 (setq pos (point))
787 (forward-line 1)
788 (setq str (buffer-substring pos (1- (point))))
789 (while (looking-at "[ \t]")
790 (setq str (concat str " "
791 (buffer-substring (match-end 0)
792 (line-end-position))))
793 (forward-line 1))
794 str)
795 (re-search-forward "[\n][\n]+" nil t)
796 (buffer-substring (point) (progn (end-of-line) (point))))
797 "\n")))
798 \f
799 ;; Simple motion in a summary buffer.
800
801 (defun rmail-summary-next-all (&optional number)
802 (interactive "p")
803 (forward-line (if number number 1))
804 ;; It doesn't look nice to move forward past the last message line.
805 (and (eobp) (> number 0)
806 (forward-line -1))
807 (display-buffer rmail-buffer))
808
809 (defun rmail-summary-previous-all (&optional number)
810 (interactive "p")
811 (forward-line (- (if number number 1)))
812 ;; It doesn't look nice to move forward past the last message line.
813 (and (eobp) (< number 0)
814 (forward-line -1))
815 (display-buffer rmail-buffer))
816
817 (defun rmail-summary-next-msg (&optional number)
818 "Display next non-deleted msg from rmail file.
819 With optional prefix argument NUMBER, moves forward this number of non-deleted
820 messages, or backward if NUMBER is negative."
821 (interactive "p")
822 (forward-line 0)
823 (and (> number 0) (end-of-line))
824 (let ((count (if (< number 0) (- number) number))
825 (search (if (> number 0) 're-search-forward 're-search-backward))
826 (non-del-msg-found nil))
827 (while (and (> count 0) (setq non-del-msg-found
828 (or (funcall search "^.....[^D]" nil t)
829 non-del-msg-found)))
830 (setq count (1- count))))
831 (beginning-of-line)
832 (display-buffer rmail-buffer))
833
834 (defun rmail-summary-previous-msg (&optional number)
835 "Display previous non-deleted msg from rmail file.
836 With optional prefix argument NUMBER, moves backward this number of
837 non-deleted messages."
838 (interactive "p")
839 (rmail-summary-next-msg (- (if number number 1))))
840
841 (defun rmail-summary-next-labeled-message (n labels)
842 "Show next message with LABELS. Defaults to last labels used.
843 With prefix argument N moves forward N messages with these labels."
844 (interactive "p\nsMove to next msg with labels: ")
845 (let (msg)
846 (with-current-buffer rmail-buffer
847 (rmail-next-labeled-message n labels)
848 (setq msg rmail-current-message))
849 (rmail-summary-goto-msg msg)))
850
851 (defun rmail-summary-previous-labeled-message (n labels)
852 "Show previous message with LABELS. Defaults to last labels used.
853 With prefix argument N moves backward N messages with these labels."
854 (interactive "p\nsMove to previous msg with labels: ")
855 (let (msg)
856 (with-current-buffer rmail-buffer
857 (rmail-previous-labeled-message n labels)
858 (setq msg rmail-current-message))
859 (rmail-summary-goto-msg msg)))
860
861 (defun rmail-summary-next-same-subject (n)
862 "Go to the next message in the summary having the same subject.
863 With prefix argument N, do this N times.
864 If N is negative, go backwards."
865 (interactive "p")
866 (let ((forward (> n 0))
867 subject i found)
868 (with-current-buffer rmail-buffer
869 (setq subject (rmail-simplified-subject)
870 i rmail-current-message))
871 (save-excursion
872 (while (and (/= n 0)
873 (if forward
874 (not (eobp))
875 (not (bobp))))
876 (let (done)
877 (while (and (not done)
878 (if forward
879 (not (eobp))
880 (not (bobp))))
881 ;; Advance thru summary.
882 (forward-line (if forward 1 -1))
883 ;; Get msg number of this line.
884 (setq i (string-to-number
885 (buffer-substring (point)
886 (min (point-max) (+ 6 (point))))))
887 (setq done (string-equal subject (rmail-simplified-subject i))))
888 (if done (setq found i)))
889 (setq n (if forward (1- n) (1+ n)))))
890 (if found
891 (rmail-summary-goto-msg found)
892 (error "No %s message with same subject"
893 (if forward "following" "previous")))))
894
895 (defun rmail-summary-previous-same-subject (n)
896 "Go to the previous message in the summary having the same subject.
897 With prefix argument N, do this N times.
898 If N is negative, go forwards instead."
899 (interactive "p")
900 (rmail-summary-next-same-subject (- n)))
901 \f
902 ;; Delete and undelete summary commands.
903
904 (defun rmail-summary-delete-forward (&optional count)
905 "Delete this message and move to next nondeleted one.
906 Deleted messages stay in the file until the \\[rmail-expunge] command is given.
907 A prefix argument serves as a repeat count;
908 a negative argument means to delete and move backward."
909 (interactive "p")
910 (unless (numberp count) (setq count 1))
911 (let (end del-msg
912 (backward (< count 0)))
913 (while (/= count 0)
914 (rmail-summary-goto-msg)
915 (with-current-buffer rmail-buffer
916 (rmail-delete-message)
917 (setq del-msg rmail-current-message))
918 (rmail-summary-mark-deleted del-msg)
919 (while (and (not (if backward (bobp) (eobp)))
920 (save-excursion (beginning-of-line)
921 (looking-at " *[0-9]+D")))
922 (forward-line (if backward -1 1)))
923 ;; It looks ugly to move to the empty line at end of buffer.
924 (and (eobp) (not backward)
925 (forward-line -1))
926 (setq count
927 (if (> count 0) (1- count) (1+ count))))))
928
929 (defun rmail-summary-delete-backward (&optional count)
930 "Delete this message and move to previous nondeleted one.
931 Deleted messages stay in the file until the \\[rmail-expunge] command is given.
932 A prefix argument serves as a repeat count;
933 a negative argument means to delete and move forward."
934 (interactive "p")
935 (rmail-summary-delete-forward (- count)))
936
937 (defun rmail-summary-mark-deleted (&optional n undel)
938 ;; Since third arg is t, this only alters the summary, not the Rmail buf.
939 (and n (rmail-summary-goto-msg n t t))
940 (or (eobp)
941 (not (overlay-get rmail-summary-overlay 'face))
942 (let ((buffer-read-only nil))
943 (skip-chars-forward " ")
944 (skip-chars-forward "0-9")
945 (if undel
946 (if (looking-at "D")
947 (progn (delete-char 1) (insert " ")))
948 (delete-char 1)
949 (insert "D"))
950 ;; Register a new summary line.
951 (with-current-buffer rmail-buffer
952 (aset rmail-summary-vector (1- n) (rmail-create-summary-line n)))))
953 (beginning-of-line))
954
955 (defun rmail-summary-update-line (n)
956 "Update the summary line for message N."
957 (when (rmail-summary-goto-msg n t t)
958 (let* ((buffer-read-only nil)
959 (start (line-beginning-position))
960 (end (line-beginning-position 2))
961 (overlays (overlays-in start end))
962 high ov)
963 (while (and (setq ov (car overlays))
964 (not (setq high (overlay-get ov 'rmail-summary))))
965 (setq overlays (cdr overlays)))
966 (delete-region start end)
967 (princ
968 (with-current-buffer rmail-buffer
969 (aset rmail-summary-vector (1- n) (rmail-create-summary-line n)))
970 (current-buffer))
971 (when high
972 (forward-line -1)
973 (rmail-summary-update-highlight nil)))))
974
975 (defun rmail-summary-mark-undeleted (n)
976 (rmail-summary-mark-deleted n t))
977
978 (defun rmail-summary-deleted-p (&optional n)
979 (save-excursion
980 (and n (rmail-summary-goto-msg n nil t))
981 (skip-chars-forward " ")
982 (skip-chars-forward "0-9")
983 (looking-at "D")))
984
985 (defun rmail-summary-undelete (&optional arg)
986 "Undelete current message.
987 Optional prefix ARG means undelete ARG previous messages."
988 (interactive "p")
989 (if (/= arg 1)
990 (rmail-summary-undelete-many arg)
991 (let ((buffer-read-only nil)
992 (opoint (point)))
993 (end-of-line)
994 (cond ((re-search-backward "\\(^ *[0-9]*\\)\\(D\\)" nil t)
995 (replace-match "\\1 ")
996 (rmail-summary-goto-msg)
997 (if rmail-enable-mime
998 (set-buffer rmail-buffer)
999 (rmail-pop-to-buffer rmail-buffer))
1000 (and (rmail-message-deleted-p rmail-current-message)
1001 (rmail-undelete-previous-message))
1002 (if rmail-enable-mime
1003 (rmail-pop-to-buffer rmail-buffer))
1004 (rmail-pop-to-buffer rmail-summary-buffer))
1005 (t (goto-char opoint))))))
1006
1007 (defun rmail-summary-undelete-many (&optional n)
1008 "Undelete all deleted msgs, optional prefix arg N means undelete N prev msgs."
1009 (interactive "P")
1010 (with-current-buffer rmail-buffer
1011 (let* ((init-msg (if n rmail-current-message rmail-total-messages))
1012 (rmail-current-message init-msg)
1013 (n (or n rmail-total-messages))
1014 (msgs-undeled 0))
1015 (while (and (> rmail-current-message 0)
1016 (< msgs-undeled n))
1017 (if (rmail-message-deleted-p rmail-current-message)
1018 (progn (rmail-set-attribute rmail-deleted-attr-index nil)
1019 (setq msgs-undeled (1+ msgs-undeled))))
1020 (setq rmail-current-message (1- rmail-current-message)))
1021 (set-buffer rmail-summary-buffer)
1022 (setq rmail-current-message init-msg msgs-undeled 0)
1023 (while (and (> rmail-current-message 0)
1024 (< msgs-undeled n))
1025 (if (rmail-summary-deleted-p rmail-current-message)
1026 (progn (rmail-summary-mark-undeleted rmail-current-message)
1027 (setq msgs-undeled (1+ msgs-undeled))))
1028 (setq rmail-current-message (1- rmail-current-message))))
1029 (rmail-summary-goto-msg)))
1030 \f
1031 ;; Rmail Summary mode is suitable only for specially formatted data.
1032 (put 'rmail-summary-mode 'mode-class 'special)
1033
1034 (defun rmail-summary-mode ()
1035 "Rmail Summary Mode is invoked from Rmail Mode by using \\<rmail-mode-map>\\[rmail-summary].
1036 As commands are issued in the summary buffer, they are applied to the
1037 corresponding mail messages in the rmail buffer.
1038
1039 All normal editing commands are turned off.
1040 Instead, nearly all the Rmail mode commands are available,
1041 though many of them move only among the messages in the summary.
1042
1043 These additional commands exist:
1044
1045 \\[rmail-summary-undelete-many] Undelete all or prefix arg deleted messages.
1046 \\[rmail-summary-wipe] Delete the summary and go to the Rmail buffer.
1047
1048 Commands for sorting the summary:
1049
1050 \\[rmail-summary-sort-by-date] Sort by date.
1051 \\[rmail-summary-sort-by-subject] Sort by subject.
1052 \\[rmail-summary-sort-by-author] Sort by author.
1053 \\[rmail-summary-sort-by-recipient] Sort by recipient.
1054 \\[rmail-summary-sort-by-correspondent] Sort by correspondent.
1055 \\[rmail-summary-sort-by-lines] Sort by lines.
1056 \\[rmail-summary-sort-by-labels] Sort by labels."
1057 (interactive)
1058 (kill-all-local-variables)
1059 (setq major-mode 'rmail-summary-mode)
1060 (setq mode-name "RMAIL Summary")
1061 (setq truncate-lines t)
1062 (setq buffer-read-only t)
1063 (set-syntax-table text-mode-syntax-table)
1064 (make-local-variable 'rmail-buffer)
1065 (make-local-variable 'rmail-total-messages)
1066 (make-local-variable 'rmail-current-message)
1067 (setq rmail-current-message nil)
1068 (make-local-variable 'rmail-summary-redo)
1069 (setq rmail-summary-redo nil)
1070 (make-local-variable 'revert-buffer-function)
1071 (make-local-variable 'font-lock-defaults)
1072 (setq font-lock-defaults '(rmail-summary-font-lock-keywords t))
1073 (rmail-summary-enable)
1074 (run-mode-hooks 'rmail-summary-mode-hook))
1075
1076 ;; Summary features need to be disabled during edit mode.
1077 (defun rmail-summary-disable ()
1078 (use-local-map text-mode-map)
1079 (remove-hook 'post-command-hook 'rmail-summary-rmail-update t)
1080 (setq revert-buffer-function nil))
1081
1082 (defun rmail-summary-enable ()
1083 (use-local-map rmail-summary-mode-map)
1084 (add-hook 'post-command-hook 'rmail-summary-rmail-update nil t)
1085 (setq revert-buffer-function 'rmail-update-summary))
1086
1087 (defun rmail-summary-mark-seen (n &optional nomove unseen)
1088 "Remove the unseen mark from the current message, update the summary vector.
1089 N is the number of the current message. Optional argument NOMOVE
1090 non-nil means we are already at the right column. Optional argument
1091 UNSEEN non-nil means mark the message as unseen."
1092 (save-excursion
1093 (unless nomove
1094 (beginning-of-line)
1095 (skip-chars-forward " ")
1096 (skip-chars-forward "0-9"))
1097 (when (char-equal (following-char) (if unseen ?\s ?-))
1098 (let ((buffer-read-only nil))
1099 (delete-char 1)
1100 (insert (if unseen "-" " ")))
1101 (let ((line (buffer-substring-no-properties (line-beginning-position)
1102 (line-beginning-position 2))))
1103 (with-current-buffer rmail-buffer
1104 (aset rmail-summary-vector (1- n) line))))))
1105
1106 (defvar rmail-summary-put-back-unseen nil
1107 "Used for communicating between calls to `rmail-summary-rmail-update'.
1108 If it moves to a message within an Incremental Search, and removes
1109 the `unseen' attribute from that message, it sets this flag
1110 so that if the next motion between messages is in the same Incremental
1111 Search, the `unseen' attribute is restored.")
1112
1113 ;; Show in Rmail the message described by the summary line that point is on,
1114 ;; but only if the Rmail buffer is already visible.
1115 ;; This is a post-command-hook in summary buffers.
1116 (defun rmail-summary-rmail-update ()
1117 (let (buffer-read-only)
1118 (save-excursion
1119 ;; If at end of buffer, pretend we are on the last text line.
1120 (if (eobp)
1121 (forward-line -1))
1122 (beginning-of-line)
1123 (skip-chars-forward " ")
1124 (let ((msg-num (string-to-number (buffer-substring
1125 (point)
1126 (progn (skip-chars-forward "0-9")
1127 (point))))))
1128 ;; Always leave `unseen' removed
1129 ;; if we get out of isearch mode.
1130 ;; Don't let a subsequent isearch restore that `unseen'.
1131 (if (not isearch-mode)
1132 (setq rmail-summary-put-back-unseen nil))
1133
1134 (or (eq rmail-current-message msg-num)
1135 (let ((window (get-buffer-window rmail-buffer t))
1136 (owin (selected-window)))
1137 (if isearch-mode
1138 (progn
1139 ;; If we first saw the previous message in this search,
1140 ;; and we have gone to a different message while searching,
1141 ;; put back `unseen' on the former one.
1142 (when rmail-summary-put-back-unseen
1143 (rmail-set-attribute rmail-unseen-attr-index t
1144 rmail-current-message)
1145 (save-excursion
1146 (goto-char rmail-summary-put-back-unseen)
1147 (rmail-summary-mark-seen rmail-current-message t t)))
1148 ;; Arrange to do that later, for the new current message,
1149 ;; if it still has `unseen'.
1150 (setq rmail-summary-put-back-unseen
1151 (if (rmail-message-unseen-p msg-num)
1152 (point))))
1153 (setq rmail-summary-put-back-unseen nil))
1154 ;; Go to the desired message.
1155 (setq rmail-current-message msg-num)
1156 ;; Update the summary to show the message has been seen.
1157 (rmail-summary-mark-seen msg-num t)
1158 (if window
1159 ;; Using save-window-excursion would cause the new value
1160 ;; of point to get lost.
1161 (unwind-protect
1162 (progn
1163 (select-window window)
1164 (rmail-show-message msg-num t))
1165 (select-window owin))
1166 (if (buffer-name rmail-buffer)
1167 (with-current-buffer rmail-buffer
1168 (rmail-show-message msg-num t))))
1169 ;; In linum mode, the message buffer must be specially
1170 ;; updated (Bug#4878).
1171 (and (fboundp 'linum-update)
1172 (buffer-name rmail-buffer)
1173 (linum-update rmail-buffer))))
1174 (rmail-summary-update-highlight nil)))))
1175
1176 (defun rmail-summary-save-buffer ()
1177 "Save the buffer associated with this RMAIL summary."
1178 (interactive)
1179 (save-window-excursion
1180 (save-excursion
1181 (switch-to-buffer rmail-buffer)
1182 (save-buffer))))
1183 \f
1184 (defun rmail-summary-mouse-goto-message (event)
1185 "Select the message whose summary line you click on."
1186 (interactive "@e")
1187 (goto-char (posn-point (event-end event)))
1188 (rmail-summary-goto-msg))
1189
1190 (defun rmail-summary-goto-msg (&optional n nowarn skip-rmail)
1191 "Go to message N in the summary buffer and the Rmail buffer.
1192 If N is nil, use the message corresponding to point in the summary
1193 and move to that message in the Rmail buffer.
1194
1195 If NOWARN, don't say anything if N is out of range.
1196 If SKIP-RMAIL, don't do anything to the Rmail buffer.
1197 Returns non-nil if message N was found."
1198 (interactive "P")
1199 (if (consp n) (setq n (prefix-numeric-value n)))
1200 (if (eobp) (forward-line -1))
1201 (beginning-of-line)
1202 (let* ((obuf (current-buffer))
1203 (buf rmail-buffer)
1204 (cur (point))
1205 message-not-found
1206 (curmsg (string-to-number
1207 (buffer-substring (point)
1208 (min (point-max) (+ 6 (point))))))
1209 (total (with-current-buffer buf rmail-total-messages)))
1210 ;; If message number N was specified, find that message's line
1211 ;; or set message-not-found.
1212 ;; If N wasn't specified or that message can't be found.
1213 ;; set N by default.
1214 (if (not n)
1215 (setq n curmsg)
1216 (if (< n 1)
1217 (progn (message "No preceding message")
1218 (setq n 1)))
1219 (if (and (> n total)
1220 (> total 0))
1221 (progn (message "No following message")
1222 (goto-char (point-max))
1223 (rmail-summary-goto-msg nil nowarn skip-rmail)))
1224 (goto-char (point-min))
1225 (if (not (re-search-forward (format "^%5d[^0-9]" n) nil t))
1226 (progn (or nowarn (message "Message %d not found" n))
1227 (setq n curmsg)
1228 (setq message-not-found t)
1229 (goto-char cur))))
1230 (rmail-summary-mark-seen n)
1231 (rmail-summary-update-highlight message-not-found)
1232 (beginning-of-line)
1233 (unless skip-rmail
1234 (let ((selwin (selected-window)))
1235 (unwind-protect
1236 (progn (rmail-pop-to-buffer buf)
1237 (rmail-show-message n))
1238 (select-window selwin)
1239 ;; The actions above can alter the current buffer. Preserve it.
1240 (set-buffer obuf))))
1241 (not message-not-found)))
1242
1243 ;; Update the highlighted line in an rmail summary buffer.
1244 ;; That should be current. We highlight the line point is on.
1245 ;; If NOT-FOUND is non-nil, we turn off highlighting.
1246 (defun rmail-summary-update-highlight (not-found)
1247 ;; Make sure we have an overlay to use.
1248 (or rmail-summary-overlay
1249 (progn
1250 (make-local-variable 'rmail-summary-overlay)
1251 (setq rmail-summary-overlay (make-overlay (point) (point)))
1252 (overlay-put rmail-summary-overlay 'rmail-summary t)))
1253 ;; If this message is in the summary, use the overlay to highlight it.
1254 ;; Otherwise, don't highlight anything.
1255 (if not-found
1256 (overlay-put rmail-summary-overlay 'face nil)
1257 (move-overlay rmail-summary-overlay
1258 (save-excursion (beginning-of-line)
1259 (skip-chars-forward " ")
1260 (point))
1261 (line-end-position))
1262 (overlay-put rmail-summary-overlay 'face 'highlight)))
1263 \f
1264 (defun rmail-summary-scroll-msg-up (&optional dist)
1265 "Scroll the Rmail window forward.
1266 If the Rmail window is displaying the end of a message,
1267 advance to the next message."
1268 (interactive "P")
1269 (if (eq dist '-)
1270 (rmail-summary-scroll-msg-down nil)
1271 (let ((rmail-buffer-window (get-buffer-window rmail-buffer)))
1272 (if rmail-buffer-window
1273 (if (let ((rmail-summary-window (selected-window)))
1274 (select-window rmail-buffer-window)
1275 (prog1
1276 ;; Is EOB visible in the buffer?
1277 (save-excursion
1278 (let ((ht (window-height (selected-window))))
1279 (move-to-window-line (- ht 2))
1280 (end-of-line)
1281 (eobp)))
1282 (select-window rmail-summary-window)))
1283 (if (not rmail-summary-scroll-between-messages)
1284 (error "End of buffer")
1285 (rmail-summary-next-msg (or dist 1)))
1286 (let ((other-window-scroll-buffer rmail-buffer))
1287 (scroll-other-window dist)))
1288 ;; If it isn't visible at all, show the beginning.
1289 (rmail-summary-beginning-of-message)))))
1290
1291 (defun rmail-summary-scroll-msg-down (&optional dist)
1292 "Scroll the Rmail window backward.
1293 If the Rmail window is now displaying the beginning of a message,
1294 move to the previous message."
1295 (interactive "P")
1296 (if (eq dist '-)
1297 (rmail-summary-scroll-msg-up nil)
1298 (let ((rmail-buffer-window (get-buffer-window rmail-buffer)))
1299 (if rmail-buffer-window
1300 (if (let ((rmail-summary-window (selected-window)))
1301 (select-window rmail-buffer-window)
1302 (prog1
1303 ;; Is BOB visible in the buffer?
1304 (save-excursion
1305 (move-to-window-line 0)
1306 (beginning-of-line)
1307 (bobp))
1308 (select-window rmail-summary-window)))
1309 (if (not rmail-summary-scroll-between-messages)
1310 (error "Beginning of buffer")
1311 (rmail-summary-previous-msg (or dist 1)))
1312 (let ((other-window-scroll-buffer rmail-buffer))
1313 (scroll-other-window-down dist)))
1314 ;; If it isn't visible at all, show the beginning.
1315 (rmail-summary-beginning-of-message)))))
1316
1317 (defun rmail-summary-beginning-of-message ()
1318 "Show current message from the beginning."
1319 (interactive)
1320 (rmail-summary-show-message 'BEG))
1321
1322 (defun rmail-summary-end-of-message ()
1323 "Show bottom of current message."
1324 (interactive)
1325 (rmail-summary-show-message 'END))
1326
1327 (defun rmail-summary-show-message (where)
1328 "Show current mail message.
1329 Position it according to WHERE which can be BEG or END"
1330 (if (and (one-window-p) (not pop-up-frames))
1331 ;; If there is just one window, put the summary on the top.
1332 (let ((buffer rmail-buffer))
1333 (split-window (selected-window) rmail-summary-window-size)
1334 (select-window (frame-first-window))
1335 (rmail-pop-to-buffer rmail-buffer)
1336 ;; If pop-to-buffer did not use that window, delete that
1337 ;; window. (This can happen if it uses another frame.)
1338 (or (eq buffer (window-buffer (next-window (frame-first-window))))
1339 (delete-other-windows)))
1340 (rmail-pop-to-buffer rmail-buffer))
1341 (cond
1342 ((eq where 'BEG)
1343 (goto-char (point-min))
1344 (search-forward "\n\n"))
1345 ((eq where 'END)
1346 (goto-char (point-max))
1347 (recenter (1- (window-height))))
1348 )
1349 (rmail-pop-to-buffer rmail-summary-buffer))
1350
1351 (defun rmail-summary-bury ()
1352 "Bury the Rmail buffer and the Rmail summary buffer."
1353 (interactive)
1354 (let ((buffer-to-bury (current-buffer)))
1355 (let (window)
1356 (while (setq window (get-buffer-window rmail-buffer))
1357 (set-window-buffer window (other-buffer rmail-buffer)))
1358 (bury-buffer rmail-buffer))
1359 (switch-to-buffer (other-buffer buffer-to-bury))
1360 (bury-buffer buffer-to-bury)))
1361
1362 (defun rmail-summary-quit ()
1363 "Quit out of Rmail and Rmail summary."
1364 (interactive)
1365 (rmail-summary-wipe)
1366 (rmail-quit))
1367
1368 (defun rmail-summary-wipe ()
1369 "Kill and wipe away Rmail summary, remaining within Rmail."
1370 (interactive)
1371 (with-current-buffer rmail-buffer (setq rmail-summary-buffer nil))
1372 (let ((local-rmail-buffer rmail-buffer))
1373 (kill-buffer (current-buffer))
1374 ;; Delete window if not only one.
1375 (if (not (eq (selected-window) (next-window nil 'no-minibuf)))
1376 (delete-window))
1377 ;; Switch windows to the rmail buffer, or switch to it in this window.
1378 (rmail-pop-to-buffer local-rmail-buffer)))
1379
1380 (defun rmail-summary-expunge ()
1381 "Actually erase all deleted messages and recompute summary headers."
1382 (interactive)
1383 (with-current-buffer rmail-buffer
1384 (when (rmail-expunge-confirmed)
1385 (rmail-only-expunge)))
1386 (rmail-update-summary))
1387
1388 (defun rmail-summary-expunge-and-save ()
1389 "Expunge and save RMAIL file."
1390 (interactive)
1391 (save-excursion
1392 (rmail-expunge-and-save))
1393 (rmail-update-summary)
1394 (set-buffer-modified-p nil))
1395
1396 (defun rmail-summary-get-new-mail (&optional file-name)
1397 "Get new mail and recompute summary headers.
1398
1399 Optionally you can specify the file to get new mail from. In this case,
1400 the file of new mail is not changed or deleted. Noninteractively, you can
1401 pass the inbox file name as an argument. Interactively, a prefix
1402 argument says to read a file name and use that file as the inbox."
1403 (interactive
1404 (list (if current-prefix-arg
1405 (read-file-name "Get new mail from file: "))))
1406 (let (msg)
1407 (with-current-buffer rmail-buffer
1408 (rmail-get-new-mail file-name)
1409 ;; Get the proper new message number.
1410 (setq msg rmail-current-message))
1411 ;; Make sure that message is displayed.
1412 (or (zerop msg)
1413 (rmail-summary-goto-msg msg))))
1414
1415 (defun rmail-summary-input (filename)
1416 "Run Rmail on file FILENAME."
1417 (interactive "FRun rmail on RMAIL file: ")
1418 ;; We switch windows here, then display the other Rmail file there.
1419 (rmail-pop-to-buffer rmail-buffer)
1420 (rmail filename))
1421
1422 (defun rmail-summary-first-message ()
1423 "Show first message in Rmail file from summary buffer."
1424 (interactive)
1425 (with-no-warnings
1426 (beginning-of-buffer)))
1427
1428 (defun rmail-summary-last-message ()
1429 "Show last message in Rmail file from summary buffer."
1430 (interactive)
1431 (with-no-warnings
1432 (end-of-buffer))
1433 (forward-line -1))
1434
1435 (declare-function rmail-abort-edit "rmailedit" ())
1436 (declare-function rmail-cease-edit "rmailedit"())
1437 (declare-function rmail-set-label "rmailkwd" (l state &optional n))
1438 (declare-function rmail-output-read-file-name "rmailout" ())
1439 (declare-function mail-send-and-exit "sendmail" (&optional arg))
1440
1441 (defvar rmail-summary-edit-map nil)
1442 (if rmail-summary-edit-map
1443 nil
1444 (setq rmail-summary-edit-map
1445 (nconc (make-sparse-keymap) text-mode-map))
1446 (define-key rmail-summary-edit-map "\C-c\C-c" 'rmail-cease-edit)
1447 (define-key rmail-summary-edit-map "\C-c\C-]" 'rmail-abort-edit))
1448
1449 (defun rmail-summary-edit-current-message ()
1450 "Edit the contents of this message."
1451 (interactive)
1452 (rmail-pop-to-buffer rmail-buffer)
1453 (rmail-edit-current-message)
1454 (use-local-map rmail-summary-edit-map))
1455
1456 (defun rmail-summary-cease-edit ()
1457 "Finish editing message, then go back to Rmail summary buffer."
1458 (interactive)
1459 (rmail-cease-edit)
1460 (rmail-pop-to-buffer rmail-summary-buffer))
1461
1462 (defun rmail-summary-abort-edit ()
1463 "Abort edit of current message; restore original contents.
1464 Go back to summary buffer."
1465 (interactive)
1466 (rmail-abort-edit)
1467 (rmail-pop-to-buffer rmail-summary-buffer))
1468
1469 (defun rmail-summary-search-backward (regexp &optional n)
1470 "Show message containing next match for REGEXP.
1471 Prefix argument gives repeat count; negative argument means search
1472 backwards (through earlier messages).
1473 Interactively, empty argument means use same regexp used last time."
1474 (interactive
1475 (let* ((reversep (>= (prefix-numeric-value current-prefix-arg) 0))
1476 (prompt
1477 (concat (if reversep "Reverse " "") "Rmail search (regexp"))
1478 regexp)
1479 (setq prompt
1480 (concat prompt
1481 (if rmail-search-last-regexp
1482 (concat ", default "
1483 rmail-search-last-regexp "): ")
1484 "): ")))
1485 (setq regexp (read-string prompt))
1486 (cond ((not (equal regexp ""))
1487 (setq rmail-search-last-regexp regexp))
1488 ((not rmail-search-last-regexp)
1489 (error "No previous Rmail search string")))
1490 (list rmail-search-last-regexp
1491 (prefix-numeric-value current-prefix-arg))))
1492 ;; Don't use save-excursion because that prevents point from moving
1493 ;; properly in the summary buffer.
1494 (with-current-buffer rmail-buffer
1495 (rmail-search regexp (- n))))
1496
1497 (defun rmail-summary-search (regexp &optional n)
1498 "Show message containing next match for REGEXP.
1499 Prefix argument gives repeat count; negative argument means search
1500 backwards (through earlier messages).
1501 Interactively, empty argument means use same regexp used last time."
1502 (interactive
1503 (let* ((reversep (< (prefix-numeric-value current-prefix-arg) 0))
1504 (prompt
1505 (concat (if reversep "Reverse " "") "Rmail search (regexp"))
1506 regexp)
1507 (setq prompt
1508 (concat prompt
1509 (if rmail-search-last-regexp
1510 (concat ", default "
1511 rmail-search-last-regexp "): ")
1512 "): ")))
1513 (setq regexp (read-string prompt))
1514 (cond ((not (equal regexp ""))
1515 (setq rmail-search-last-regexp regexp))
1516 ((not rmail-search-last-regexp)
1517 (error "No previous Rmail search string")))
1518 (list rmail-search-last-regexp
1519 (prefix-numeric-value current-prefix-arg))))
1520 ;; Don't use save-excursion because that prevents point from moving
1521 ;; properly in the summary buffer.
1522 (let ((buffer (current-buffer))
1523 (selwin (selected-window)))
1524 (unwind-protect
1525 (progn
1526 (rmail-pop-to-buffer rmail-buffer)
1527 (rmail-search regexp n))
1528 (select-window selwin)
1529 (set-buffer buffer))))
1530
1531 (defun rmail-summary-toggle-header ()
1532 "Show original message header if pruned header currently shown, or vice versa."
1533 (interactive)
1534 (save-window-excursion
1535 (set-buffer rmail-buffer)
1536 (rmail-toggle-header))
1537 ;; Inside save-excursion, some changes to point in the RMAIL buffer are lost.
1538 ;; Set point to point-min in the RMAIL buffer, if it is visible.
1539 (let ((window (get-buffer-window rmail-buffer)))
1540 (if window
1541 ;; Using save-window-excursion would lose the new value of point.
1542 (let ((owin (selected-window)))
1543 (unwind-protect
1544 (progn
1545 (select-window window)
1546 (goto-char (point-min)))
1547 (select-window owin))))))
1548
1549
1550 (defun rmail-summary-add-label (label)
1551 "Add LABEL to labels associated with current Rmail message.
1552 Completion is performed over known labels when reading."
1553 (interactive (list (with-current-buffer rmail-buffer
1554 (rmail-read-label "Add label"))))
1555 (with-current-buffer rmail-buffer
1556 (rmail-add-label label)))
1557
1558 (defun rmail-summary-kill-label (label)
1559 "Remove LABEL from labels associated with current Rmail message.
1560 Completion is performed over known labels when reading."
1561 (interactive (list (with-current-buffer rmail-buffer
1562 (rmail-read-label "Kill label"))))
1563 (with-current-buffer rmail-buffer
1564 (rmail-set-label label nil)))
1565 \f
1566 ;;;; *** Rmail Summary Mailing Commands ***
1567
1568 (defun rmail-summary-override-mail-send-and-exit ()
1569 "Replace bindings to `mail-send-and-exit' with `rmail-summary-send-and-exit'."
1570 (use-local-map (copy-keymap (current-local-map)))
1571 (dolist (key (where-is-internal 'mail-send-and-exit))
1572 (define-key (current-local-map) key 'rmail-summary-send-and-exit)))
1573
1574 (defun rmail-summary-mail ()
1575 "Send mail in another window.
1576 While composing the message, use \\[mail-yank-original] to yank the
1577 original message into it."
1578 (interactive)
1579 (let ((window (get-buffer-window rmail-buffer)))
1580 (if window
1581 (select-window window)
1582 (set-buffer rmail-buffer)))
1583 (rmail-start-mail nil nil nil nil nil (current-buffer))
1584 (rmail-summary-override-mail-send-and-exit))
1585
1586 (defun rmail-summary-continue ()
1587 "Continue composing outgoing message previously being composed."
1588 (interactive)
1589 (let ((window (get-buffer-window rmail-buffer)))
1590 (if window
1591 (select-window window)
1592 (set-buffer rmail-buffer)))
1593 (rmail-start-mail t))
1594
1595 (defun rmail-summary-reply (just-sender)
1596 "Reply to the current message.
1597 Normally include CC: to all other recipients of original message;
1598 prefix argument means ignore them. While composing the reply,
1599 use \\[mail-yank-original] to yank the original message into it."
1600 (interactive "P")
1601 (let ((window (get-buffer-window rmail-buffer)))
1602 (if window
1603 (select-window window)
1604 (set-buffer rmail-buffer)))
1605 (rmail-reply just-sender)
1606 (rmail-summary-override-mail-send-and-exit))
1607
1608 (defun rmail-summary-retry-failure ()
1609 "Edit a mail message which is based on the contents of the current message.
1610 For a message rejected by the mail system, extract the interesting headers and
1611 the body of the original message; otherwise copy the current message."
1612 (interactive)
1613 (let ((window (get-buffer-window rmail-buffer)))
1614 (if window
1615 (select-window window)
1616 (set-buffer rmail-buffer)))
1617 (rmail-retry-failure)
1618 (rmail-summary-override-mail-send-and-exit))
1619
1620 (defun rmail-summary-send-and-exit ()
1621 "Send mail reply and return to summary buffer."
1622 (interactive)
1623 (mail-send-and-exit t))
1624
1625 (defun rmail-summary-forward (resend)
1626 "Forward the current message to another user.
1627 With prefix argument, \"resend\" the message instead of forwarding it;
1628 see the documentation of `rmail-resend'."
1629 (interactive "P")
1630 (save-excursion
1631 (let ((window (get-buffer-window rmail-buffer)))
1632 (if window
1633 (select-window window)
1634 (set-buffer rmail-buffer)))
1635 (rmail-forward resend)
1636 (rmail-summary-override-mail-send-and-exit)))
1637
1638 (defun rmail-summary-resend ()
1639 "Resend current message using `rmail-resend'."
1640 (interactive)
1641 (save-excursion
1642 (let ((window (get-buffer-window rmail-buffer)))
1643 (if window
1644 (select-window window)
1645 (set-buffer rmail-buffer)))
1646 (call-interactively 'rmail-resend)))
1647 \f
1648 ;; Summary output commands.
1649
1650 (defun rmail-summary-output (&optional file-name n)
1651 "Append this message to mail file FILE-NAME.
1652 This works with both mbox format and Babyl format files,
1653 outputting in the appropriate format for each.
1654 The default file name comes from `rmail-default-file',
1655 which is updated to the name you use in this command.
1656
1657 A prefix argument N says to output that many consecutive messages
1658 from those in the summary, starting with the current one.
1659 Deleted messages are skipped and don't count.
1660 When called from Lisp code, N may be omitted and defaults to 1.
1661
1662 This command always outputs the complete message header,
1663 even the header display is currently pruned."
1664 (interactive
1665 (progn (require 'rmailout)
1666 (list (rmail-output-read-file-name)
1667 (prefix-numeric-value current-prefix-arg))))
1668 (let ((i 0) prev-msg)
1669 (while
1670 (and (< i n)
1671 (progn (rmail-summary-goto-msg)
1672 (not (eq prev-msg
1673 (setq prev-msg
1674 (with-current-buffer rmail-buffer
1675 rmail-current-message))))))
1676 (setq i (1+ i))
1677 (with-current-buffer rmail-buffer
1678 (let ((rmail-delete-after-output nil))
1679 (rmail-output file-name 1)))
1680 (if rmail-delete-after-output
1681 (rmail-summary-delete-forward nil)
1682 (if (< i n)
1683 (rmail-summary-next-msg 1))))))
1684
1685 (defalias 'rmail-summary-output-to-rmail-file 'rmail-summary-output)
1686
1687 (declare-function rmail-output-as-seen "rmailout"
1688 (file-name &optional count noattribute from-gnus))
1689
1690 (defun rmail-summary-output-as-seen (&optional file-name n)
1691 "Append this message to mbox file named FILE-NAME.
1692 A prefix argument N says to output that many consecutive messages,
1693 from the summary, starting with the current one.
1694 Deleted messages are skipped and don't count.
1695 When called from Lisp code, N may be omitted and defaults to 1.
1696
1697 This outputs the message header as you see it (or would see it)
1698 displayed in Rmail.
1699
1700 The default file name comes from `rmail-default-file',
1701 which is updated to the name you use in this command."
1702 (interactive
1703 (progn (require 'rmailout)
1704 (list (rmail-output-read-file-name)
1705 (prefix-numeric-value current-prefix-arg))))
1706 (require 'rmailout) ; for rmail-output-as-seen in non-interactive case
1707 (let ((i 0) prev-msg)
1708 (while
1709 (and (< i n)
1710 (progn (rmail-summary-goto-msg)
1711 (not (eq prev-msg
1712 (setq prev-msg
1713 (with-current-buffer rmail-buffer
1714 rmail-current-message))))))
1715 (setq i (1+ i))
1716 (with-current-buffer rmail-buffer
1717 (let ((rmail-delete-after-output nil))
1718 (rmail-output-as-seen file-name 1)))
1719 (if rmail-delete-after-output
1720 (rmail-summary-delete-forward nil)
1721 (if (< i n)
1722 (rmail-summary-next-msg 1))))))
1723
1724 (defun rmail-summary-output-menu ()
1725 "Output current message to another Rmail file, chosen with a menu.
1726 Also set the default for subsequent \\[rmail-output-to-babyl-file] commands.
1727 The variables `rmail-secondary-file-directory' and
1728 `rmail-secondary-file-regexp' control which files are offered in the menu."
1729 (interactive)
1730 (with-current-buffer rmail-buffer
1731 (let ((rmail-delete-after-output nil))
1732 (call-interactively 'rmail-output-menu)))
1733 (if rmail-delete-after-output
1734 (rmail-summary-delete-forward nil)))
1735
1736 (defun rmail-summary-construct-io-menu ()
1737 (let ((files (rmail-find-all-files rmail-secondary-file-directory)))
1738 (if files
1739 (progn
1740 (define-key rmail-summary-mode-map [menu-bar classify input-menu]
1741 (cons "Input Rmail File"
1742 (rmail-list-to-menu "Input Rmail File"
1743 files
1744 'rmail-summary-input)))
1745 (define-key rmail-summary-mode-map [menu-bar classify output-menu]
1746 (cons "Output Rmail File"
1747 (rmail-list-to-menu "Output Rmail File"
1748 files
1749 'rmail-summary-output))))
1750 (define-key rmail-summary-mode-map [menu-bar classify input-menu]
1751 '("Input Rmail File" . rmail-disable-menu))
1752 (define-key rmail-summary-mode-map [menu-bar classify output-menu]
1753 '("Output Rmail File" . rmail-disable-menu)))))
1754
1755 (defun rmail-summary-output-body (&optional file-name)
1756 "Write this message body to the file FILE-NAME.
1757 FILE-NAME defaults, interactively, from the Subject field of the message."
1758 (interactive)
1759 (with-current-buffer rmail-buffer
1760 (let ((rmail-delete-after-output nil))
1761 (if file-name
1762 (rmail-output-body-to-file file-name)
1763 (call-interactively 'rmail-output-body-to-file))))
1764 (if rmail-delete-after-output
1765 (rmail-summary-delete-forward nil)))
1766 \f
1767 ;; Sorting messages in Rmail Summary buffer.
1768
1769 (defun rmail-summary-sort-by-date (reverse)
1770 "Sort messages of current Rmail summary by \"Date\" header.
1771 If prefix argument REVERSE is non-nil, sorts in reverse order."
1772 (interactive "P")
1773 (rmail-sort-from-summary (function rmail-sort-by-date) reverse))
1774
1775 (defun rmail-summary-sort-by-subject (reverse)
1776 "Sort messages of current Rmail summary by \"Subject\" header.
1777 Ignores any \"Re: \" prefix. If prefix argument REVERSE is
1778 non-nil, sorts in reverse order."
1779 (interactive "P")
1780 (rmail-sort-from-summary (function rmail-sort-by-subject) reverse))
1781
1782 (defun rmail-summary-sort-by-author (reverse)
1783 "Sort messages of current Rmail summary by author.
1784 This uses either the \"From\" or \"Sender\" header, downcased.
1785 If prefix argument REVERSE is non-nil, sorts in reverse order."
1786 (interactive "P")
1787 (rmail-sort-from-summary (function rmail-sort-by-author) reverse))
1788
1789 (defun rmail-summary-sort-by-recipient (reverse)
1790 "Sort messages of current Rmail summary by recipient.
1791 This uses either the \"To\" or \"Apparently-To\" header, downcased.
1792 If prefix argument REVERSE is non-nil, sorts in reverse order."
1793 (interactive "P")
1794 (rmail-sort-from-summary (function rmail-sort-by-recipient) reverse))
1795
1796 (defun rmail-summary-sort-by-correspondent (reverse)
1797 "Sort messages of current Rmail summary by other correspondent.
1798 This uses either the \"From\", \"Sender\", \"To\", or
1799 \"Apparently-To\" header, downcased. Uses the first header not
1800 excluded by `mail-dont-reply-to-names'. If prefix argument
1801 REVERSE is non-nil, sorts in reverse order."
1802 (interactive "P")
1803 (rmail-sort-from-summary (function rmail-sort-by-correspondent) reverse))
1804
1805 (defun rmail-summary-sort-by-lines (reverse)
1806 "Sort messages of current Rmail summary by the number of lines.
1807 If prefix argument REVERSE is non-nil, sorts in reverse order."
1808 (interactive "P")
1809 (rmail-sort-from-summary (function rmail-sort-by-lines) reverse))
1810
1811 (defun rmail-summary-sort-by-labels (reverse labels)
1812 "Sort messages of current Rmail summary by labels.
1813 LABELS is a comma-separated list of labels.
1814 If prefix argument REVERSE is non-nil, sorts in reverse order."
1815 (interactive "P\nsSort by labels: ")
1816 (rmail-sort-from-summary
1817 (lambda (reverse) (rmail-sort-by-labels reverse labels))
1818 reverse))
1819
1820 (defun rmail-sort-from-summary (sortfun reverse)
1821 "Sort the Rmail buffer using sorting function SORTFUN.
1822 Passes REVERSE to SORTFUN as its sole argument. Then regenerates
1823 the summary. Note that the whole Rmail buffer is sorted, even if
1824 the summary is only showing a subset of messages."
1825 (require 'rmailsort)
1826 (let ((selwin (selected-window)))
1827 (unwind-protect
1828 (progn (rmail-pop-to-buffer rmail-buffer)
1829 (funcall sortfun reverse))
1830 (select-window selwin))))
1831
1832 (provide 'rmailsum)
1833
1834 ;; Local Variables:
1835 ;; generated-autoload-file: "rmail.el"
1836 ;; End:
1837
1838 ;;; rmailsum.el ends here