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