(outline-next-heading): Make sure the match-data is correct where returning.
[emacs.git] / lisp / mail / rmailsum.el
blob86324242d45e1626ec24233d00884929818f40f8
1 ;;; rmailsum.el --- make summary buffers for the mail reader
3 ;; Copyright (C) 1985, 1993, 1994, 1995, 1996, 2000, 2001
4 ;; Free Software Foundation, Inc.
6 ;; Maintainer: FSF
7 ;; Keywords: mail
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
26 ;;; Commentary:
28 ;; Extended by Bob Weiner of Motorola
29 ;; Provided all commands from rmail-mode in rmail-summary-mode and made key
30 ;; bindings in both modes wholly compatible.
32 ;;; Code:
34 ;; For rmail-select-summary
35 (require 'rmail)
37 ;;;###autoload
38 (defcustom rmail-summary-scroll-between-messages t
39 "*Non-nil means Rmail summary scroll commands move between messages."
40 :type 'boolean
41 :group 'rmail-summary)
43 ;;;###autoload
44 (defcustom rmail-summary-line-count-flag t
45 "*Non-nil if Rmail summary should show the number of lines in each message."
46 :type 'boolean
47 :group 'rmail-summary)
49 (defvar rmail-summary-font-lock-keywords
50 '(("^.....D.*" . font-lock-string-face) ; Deleted.
51 ("^.....-.*" . font-lock-type-face) ; Unread.
52 ;; Neither of the below will be highlighted if either of the above are:
53 ("^.....[^D-] \\(......\\)" 1 font-lock-keyword-face) ; Date.
54 ("{ \\([^\n}]+\\),}" 1 font-lock-comment-face)) ; Labels.
55 "Additional expressions to highlight in Rmail Summary mode.")
57 ;; Entry points for making a summary buffer.
59 ;; Regenerate the contents of the summary
60 ;; using the same selection criterion as last time.
61 ;; M-x revert-buffer in a summary buffer calls this function.
62 (defun rmail-update-summary (&rest ignore)
63 (apply (car rmail-summary-redo) (cdr rmail-summary-redo)))
65 ;;;###autoload
66 (defun rmail-summary ()
67 "Display a summary of all messages, one line per message."
68 (interactive)
69 (rmail-new-summary "All" '(rmail-summary) nil))
71 ;;;###autoload
72 (defun rmail-summary-by-labels (labels)
73 "Display a summary of all messages with one or more LABELS.
74 LABELS should be a string containing the desired labels, separated by commas."
75 (interactive "sLabels to summarize by: ")
76 (if (string= labels "")
77 (setq labels (or rmail-last-multi-labels
78 (error "No label specified"))))
79 (setq rmail-last-multi-labels labels)
80 (rmail-new-summary (concat "labels " labels)
81 (list 'rmail-summary-by-labels labels)
82 'rmail-message-labels-p
83 (concat ", \\(" (mail-comma-list-regexp labels) "\\),")))
85 ;;;###autoload
86 (defun rmail-summary-by-recipients (recipients &optional primary-only)
87 "Display a summary of all messages with the given RECIPIENTS.
88 Normally checks the To, From and Cc fields of headers;
89 but if PRIMARY-ONLY is non-nil (prefix arg given),
90 only look in the To and From fields.
91 RECIPIENTS is a string of regexps separated by commas."
92 (interactive "sRecipients to summarize by: \nP")
93 (rmail-new-summary
94 (concat "recipients " recipients)
95 (list 'rmail-summary-by-recipients recipients primary-only)
96 'rmail-message-recipients-p
97 (mail-comma-list-regexp recipients) primary-only))
99 ;;;###autoload
100 (defun rmail-summary-by-regexp (regexp)
101 "Display a summary of all messages according to regexp REGEXP.
102 If the regular expression is found in the header of the message
103 \(including in the date and other lines, as well as the subject line),
104 Emacs will list the header line in the RMAIL-summary."
105 (interactive "sRegexp to summarize by: ")
106 (if (string= regexp "")
107 (setq regexp (or rmail-last-regexp
108 (error "No regexp specified"))))
109 (setq rmail-last-regexp regexp)
110 (rmail-new-summary (concat "regexp " regexp)
111 (list 'rmail-summary-by-regexp regexp)
112 'rmail-message-regexp-p
113 regexp))
115 ;; rmail-summary-by-topic
116 ;; 1989 R.A. Schnitzler
118 ;;;###autoload
119 (defun rmail-summary-by-topic (subject &optional whole-message)
120 "Display a summary of all messages with the given SUBJECT.
121 Normally checks the Subject field of headers;
122 but if WHOLE-MESSAGE is non-nil (prefix arg given),
123 look in the whole message.
124 SUBJECT is a string of regexps separated by commas."
125 (interactive "sTopics to summarize by: \nP")
126 (rmail-new-summary
127 (concat "about " subject)
128 (list 'rmail-summary-by-topic subject whole-message)
129 'rmail-message-subject-p
130 (mail-comma-list-regexp subject) whole-message))
132 (defun rmail-message-subject-p (msg subject &optional whole-message)
133 (save-restriction
134 (goto-char (rmail-msgbeg msg))
135 (search-forward "\n*** EOOH ***\n" (rmail-msgend msg) 'move)
136 (narrow-to-region
137 (point)
138 (progn (search-forward (if whole-message "\^_" "\n\n")) (point)))
139 (goto-char (point-min))
140 (if whole-message (re-search-forward subject nil t)
141 (string-match subject (let ((subj (mail-fetch-field "Subject")))
142 (if subj
143 (funcall rmail-summary-line-decoder subj)
144 ""))))))
146 ;;;###autoload
147 (defun rmail-summary-by-senders (senders)
148 "Display a summary of all messages with the given SENDERS.
149 SENDERS is a string of names separated by commas."
150 (interactive "sSenders to summarize by: ")
151 (rmail-new-summary
152 (concat "senders " senders)
153 (list 'rmail-summary-by-senders senders)
154 'rmail-message-senders-p
155 (mail-comma-list-regexp senders)))
157 (defun rmail-message-senders-p (msg senders)
158 (save-restriction
159 (goto-char (rmail-msgbeg msg))
160 (search-forward "\n*** EOOH ***\n")
161 (narrow-to-region (point) (progn (search-forward "\n\n") (point)))
162 (string-match senders (or (mail-fetch-field "From") ""))))
164 ;; General making of a summary buffer.
166 (defvar rmail-summary-symbol-number 0)
168 (defun rmail-new-summary (description redo-form function &rest args)
169 "Create a summary of selected messages.
170 DESCRIPTION makes part of the mode line of the summary buffer.
171 For each message, FUNCTION is applied to the message number and ARGS...
172 and if the result is non-nil, that message is included.
173 nil for FUNCTION means all messages."
174 (message "Computing summary lines...")
175 (let (sumbuf mesg was-in-summary)
176 (save-excursion
177 ;; Go to the Rmail buffer.
178 (if (eq major-mode 'rmail-summary-mode)
179 (setq was-in-summary t))
180 (set-buffer rmail-buffer)
181 ;; Find its summary buffer, or make one.
182 (setq sumbuf
183 (if (and rmail-summary-buffer
184 (buffer-name rmail-summary-buffer))
185 rmail-summary-buffer
186 (generate-new-buffer (concat (buffer-name) "-summary"))))
187 (setq mesg rmail-current-message)
188 ;; Filter the messages; make or get their summary lines.
189 (let ((summary-msgs ())
190 (new-summary-line-count 0))
191 (let ((msgnum 1)
192 (buffer-read-only nil)
193 (old-min (point-min-marker))
194 (old-max (point-max-marker)))
195 ;; Can't use save-restriction here; that doesn't work if we
196 ;; plan to modify text outside the original restriction.
197 (save-excursion
198 (widen)
199 (goto-char (point-min))
200 (while (>= rmail-total-messages msgnum)
201 (if (or (null function)
202 (apply function (cons msgnum args)))
203 (setq summary-msgs
204 (cons (cons msgnum (rmail-make-summary-line msgnum))
205 summary-msgs)))
206 (setq msgnum (1+ msgnum)))
207 (setq summary-msgs (nreverse summary-msgs)))
208 (narrow-to-region old-min old-max))
209 ;; Temporarily, while summary buffer is unfinished,
210 ;; we "don't have" a summary.
211 (setq rmail-summary-buffer nil)
212 (if rmail-enable-mime
213 (with-current-buffer rmail-view-buffer
214 (setq rmail-summary-buffer nil)))
215 (save-excursion
216 (let ((rbuf (current-buffer))
217 (vbuf rmail-view-buffer)
218 (total rmail-total-messages))
219 (set-buffer sumbuf)
220 ;; Set up the summary buffer's contents.
221 (let ((buffer-read-only nil))
222 (erase-buffer)
223 (while summary-msgs
224 (princ (cdr (car summary-msgs)) sumbuf)
225 (setq summary-msgs (cdr summary-msgs)))
226 (goto-char (point-min)))
227 ;; Set up the rest of its state and local variables.
228 (setq buffer-read-only t)
229 (rmail-summary-mode)
230 (make-local-variable 'minor-mode-alist)
231 (setq minor-mode-alist (list (list t (concat ": " description))))
232 (setq rmail-buffer rbuf
233 rmail-view-buffer vbuf
234 rmail-summary-redo redo-form
235 rmail-total-messages total))))
236 (setq rmail-summary-buffer sumbuf))
237 ;; Now display the summary buffer and go to the right place in it.
238 (or was-in-summary
239 (progn
240 (if (and (one-window-p)
241 pop-up-windows (not pop-up-frames))
242 ;; If there is just one window, put the summary on the top.
243 (progn
244 (split-window (selected-window) rmail-summary-window-size)
245 (select-window (next-window (frame-first-window)))
246 (pop-to-buffer sumbuf)
247 ;; If pop-to-buffer did not use that window, delete that
248 ;; window. (This can happen if it uses another frame.)
249 (if (not (eq sumbuf (window-buffer (frame-first-window))))
250 (delete-other-windows)))
251 (pop-to-buffer sumbuf))
252 (set-buffer rmail-buffer)
253 ;; This is how rmail makes the summary buffer reappear.
254 ;; We do this here to make the window the proper size.
255 (rmail-select-summary nil)
256 (set-buffer rmail-summary-buffer)))
257 (rmail-summary-goto-msg mesg t t)
258 (rmail-summary-construct-io-menu)
259 (message "Computing summary lines...done")))
261 ;; Low levels of generating a summary.
263 (defun rmail-make-summary-line (msg)
264 (let ((line (or (aref rmail-summary-vector (1- msg))
265 (progn
266 (setq new-summary-line-count
267 (1+ new-summary-line-count))
268 (if (zerop (% new-summary-line-count 10))
269 (message "Computing summary lines...%d"
270 new-summary-line-count))
271 (rmail-make-summary-line-1 msg)))))
272 ;; Fix up the part of the summary that says "deleted" or "unseen".
273 (aset line 5
274 (if (rmail-message-deleted-p msg) ?\D
275 (if (= ?0 (char-after (+ 3 (rmail-msgbeg msg))))
276 ?\- ?\ )))
277 line))
279 ;;;###autoload
280 (defcustom rmail-summary-line-decoder (function identity)
281 "*Function to decode summary-line.
283 By default, `identity' is set."
284 :type 'function
285 :group 'rmail-summary)
287 (defun rmail-make-summary-line-1 (msg)
288 (goto-char (rmail-msgbeg msg))
289 (let* ((lim (save-excursion (forward-line 2) (point)))
291 (labels
292 (progn
293 (forward-char 3)
294 (concat
295 ; (if (save-excursion (re-search-forward ",answered," lim t))
296 ; "*" "")
297 ; (if (save-excursion (re-search-forward ",filed," lim t))
298 ; "!" "")
299 (if (progn (search-forward ",,") (eolp))
301 (concat "{"
302 (buffer-substring (point)
303 (progn (end-of-line) (point)))
304 "} ")))))
305 (line
306 (progn
307 (forward-line 1)
308 (if (looking-at "Summary-line: ")
309 (progn
310 (goto-char (match-end 0))
311 (setq line
312 (buffer-substring (point)
313 (progn (forward-line 1) (point)))))))))
314 ;; Obsolete status lines lacking a # should be flushed.
315 (and line
316 (not (string-match "#" line))
317 (progn
318 (delete-region (point)
319 (progn (forward-line -1) (point)))
320 (setq line nil)))
321 ;; If we didn't get a valid status line from the message,
322 ;; make a new one and put it in the message.
323 (or line
324 (let* ((case-fold-search t)
325 (next (rmail-msgend msg))
326 (beg (if (progn (goto-char (rmail-msgbeg msg))
327 (search-forward "\n*** EOOH ***\n" next t))
328 (point)
329 (forward-line 1)
330 (point)))
331 (end (progn (search-forward "\n\n" nil t) (point))))
332 (save-restriction
333 (narrow-to-region beg end)
334 (goto-char beg)
335 (setq line (rmail-make-basic-summary-line)))
336 (goto-char (rmail-msgbeg msg))
337 (forward-line 2)
338 (insert "Summary-line: " line)))
339 (setq pos (string-match "#" line))
340 (aset rmail-summary-vector (1- msg)
341 (funcall rmail-summary-line-decoder
342 (concat (format "%5d " msg)
343 (substring line 0 pos)
344 labels
345 (substring line (1+ pos)))))
348 ;;;###autoload
349 (defcustom rmail-user-mail-address-regexp nil
350 "*Regexp matching user mail addresses.
351 If non-nil, this variable is used to identify the correspondent
352 when receiving new mail. If it matches the address of the sender,
353 the recipient is taken as correspondent of a mail.
354 If nil \(default value\), your `user-login-name' and `user-mail-address'
355 are used to exclude yourself as correspondent.
357 Usually you don't have to set this variable, except if you collect mails
358 sent by you under different user names.
359 Then it should be a regexp matching your mail addresses.
361 Setting this variable has an effect only before reading a mail."
362 :type '(choice (const :tag "None" nil) regexp)
363 :group 'rmail-retrieve
364 :version "21.1")
366 (defun rmail-make-basic-summary-line ()
367 (goto-char (point-min))
368 (concat (save-excursion
369 (if (not (re-search-forward "^Date:" nil t))
371 (cond ((re-search-forward "\\([^0-9:]\\)\\([0-3]?[0-9]\\)\\([- \t_]+\\)\\([adfjmnos][aceopu][bcglnprtvy]\\)"
372 (save-excursion (end-of-line) (point)) t)
373 (format "%2d-%3s"
374 (string-to-int (buffer-substring
375 (match-beginning 2)
376 (match-end 2)))
377 (buffer-substring
378 (match-beginning 4) (match-end 4))))
379 ((re-search-forward "\\([^a-z]\\)\\([adfjmnos][acepou][bcglnprtvy]\\)\\([-a-z \t_]*\\)\\([0-9][0-9]?\\)"
380 (save-excursion (end-of-line) (point)) t)
381 (format "%2d-%3s"
382 (string-to-int (buffer-substring
383 (match-beginning 4)
384 (match-end 4)))
385 (buffer-substring
386 (match-beginning 2) (match-end 2))))
387 ((re-search-forward "\\(19\\|20\\)\\([0-9][0-9]\\)-\\([01][0-9]\\)-\\([0-3][0-9]\\)"
388 (save-excursion (end-of-line) (point)) t)
389 (format "%2s%2s%2s"
390 (buffer-substring
391 (match-beginning 2) (match-end 2))
392 (buffer-substring
393 (match-beginning 3) (match-end 3))
394 (buffer-substring
395 (match-beginning 4) (match-end 4))))
396 (t "??????"))))
398 (save-excursion
399 (if (not (re-search-forward "^From:[ \t]*" nil t))
401 (let* ((from (mail-strip-quoted-names
402 (buffer-substring
403 (1- (point))
404 ;; Get all the lines of the From field
405 ;; so that we get a whole comment if there is one,
406 ;; so that mail-strip-quoted-names can discard it.
407 (let ((opoint (point)))
408 (while (progn (forward-line 1)
409 (looking-at "[ \t]")))
410 ;; Back up over newline, then trailing spaces or tabs
411 (forward-char -1)
412 (skip-chars-backward " \t")
413 (point)))))
414 len mch lo)
415 (if (string-match
416 (or rmail-user-mail-address-regexp
417 (concat "^\\("
418 (regexp-quote (user-login-name))
419 "\\($\\|@\\)\\|"
420 (regexp-quote
421 ;; Don't lose if run from init file
422 ;; where user-mail-address is not
423 ;; set yet.
424 (or user-mail-address
425 (concat (user-login-name) "@"
426 (or mail-host-address
427 (system-name)))))
428 "\\>\\)"))
429 from)
430 (save-excursion
431 (goto-char (point-min))
432 (if (not (re-search-forward "^To:[ \t]*" nil t))
434 (setq from
435 (concat "to: "
436 (mail-strip-quoted-names
437 (buffer-substring
438 (point)
439 (progn (end-of-line)
440 (skip-chars-backward " \t")
441 (point)))))))))
442 (setq len (length from))
443 (setq mch (string-match "[@%]" from))
444 (format "%25s"
445 (if (or (not mch) (<= len 25))
446 (substring from (max 0 (- len 25)))
447 (substring from
448 (setq lo (cond ((< (- mch 14) 0) 0)
449 ((< len (+ mch 11))
450 (- len 25))
451 (t (- mch 14))))
452 (min len (+ lo 25))))))))
453 (if rmail-summary-line-count-flag
454 (save-excursion
455 (save-restriction
456 (widen)
457 (let ((beg (rmail-msgbeg msgnum))
458 (end (rmail-msgend msgnum))
459 lines)
460 (save-excursion
461 (goto-char beg)
462 ;; Count only lines in the reformatted header,
463 ;; if we have reformatted it.
464 (search-forward "\n*** EOOH ***\n" end t)
465 (setq lines (count-lines (point) end)))
466 (format (cond
467 ((<= lines 9) " [%d]")
468 ((<= lines 99) " [%d]")
469 ((<= lines 999) " [%3d]")
470 (t "[%d]"))
471 lines))))
472 " ")
473 " #" ;The # is part of the format.
474 (if (re-search-forward "^Subject:" nil t)
475 (progn (skip-chars-forward " \t")
476 (buffer-substring (point)
477 (progn (end-of-line)
478 (point))))
479 (re-search-forward "[\n][\n]+" nil t)
480 (buffer-substring (point) (progn (end-of-line) (point))))
481 "\n"))
483 ;; Simple motion in a summary buffer.
485 (defun rmail-summary-next-all (&optional number)
486 (interactive "p")
487 (forward-line (if number number 1))
488 ;; It doesn't look nice to move forward past the last message line.
489 (and (eobp) (> number 0)
490 (forward-line -1))
491 (display-buffer rmail-buffer))
493 (defun rmail-summary-previous-all (&optional number)
494 (interactive "p")
495 (forward-line (- (if number number 1)))
496 ;; It doesn't look nice to move forward past the last message line.
497 (and (eobp) (< number 0)
498 (forward-line -1))
499 (display-buffer rmail-buffer))
501 (defun rmail-summary-next-msg (&optional number)
502 "Display next non-deleted msg from rmail file.
503 With optional prefix argument NUMBER, moves forward this number of non-deleted
504 messages, or backward if NUMBER is negative."
505 (interactive "p")
506 (forward-line 0)
507 (and (> number 0) (end-of-line))
508 (let ((count (if (< number 0) (- number) number))
509 (search (if (> number 0) 're-search-forward 're-search-backward))
510 (non-del-msg-found nil))
511 (while (and (> count 0) (setq non-del-msg-found
512 (or (funcall search "^....[^D]" nil t)
513 non-del-msg-found)))
514 (setq count (1- count))))
515 (beginning-of-line)
516 (display-buffer rmail-view-buffer)
519 (defun rmail-summary-previous-msg (&optional number)
520 (interactive "p")
521 (rmail-summary-next-msg (- (if number number 1))))
523 (defun rmail-summary-next-labeled-message (n labels)
524 "Show next message with LABEL. Defaults to last labels used.
525 With prefix argument N moves forward N messages with these labels."
526 (interactive "p\nsMove to next msg with labels: ")
527 (let (msg)
528 (save-excursion
529 (set-buffer rmail-buffer)
530 (rmail-next-labeled-message n labels)
531 (setq msg rmail-current-message))
532 (rmail-summary-goto-msg msg)))
534 (defun rmail-summary-previous-labeled-message (n labels)
535 "Show previous message with LABEL. Defaults to last labels used.
536 With prefix argument N moves backward N messages with these labels."
537 (interactive "p\nsMove to previous msg with labels: ")
538 (let (msg)
539 (save-excursion
540 (set-buffer rmail-buffer)
541 (rmail-previous-labeled-message n labels)
542 (setq msg rmail-current-message))
543 (rmail-summary-goto-msg msg)))
545 (defun rmail-summary-next-same-subject (n)
546 "Go to the next message in the summary having the same subject.
547 With prefix argument N, do this N times.
548 If N is negative, go backwards."
549 (interactive "p")
550 (let (subject search-regexp i found
551 (forward (> n 0)))
552 (save-excursion
553 (set-buffer rmail-buffer)
554 (setq subject (mail-fetch-field "Subject"))
555 (setq i rmail-current-message))
556 (if (string-match "Re:[ \t]*" subject)
557 (setq subject (substring subject (match-end 0))))
558 (setq search-regexp (concat "^Subject: *\\(Re: *\\)?"
559 (regexp-quote subject)
560 "\n"))
561 (save-excursion
562 (while (and (/= n 0)
563 (if forward
564 (not (eobp))
565 (not (bobp))))
566 (let (done)
567 (while (and (not done)
568 (if forward
569 (not (eobp))
570 (not (bobp))))
571 ;; Advance thru summary.
572 (forward-line (if forward 1 -1))
573 ;; Get msg number of this line.
574 (setq i (string-to-int
575 (buffer-substring (point)
576 (min (point-max) (+ 6 (point))))))
577 ;; See if that msg has desired subject.
578 (save-excursion
579 (set-buffer rmail-buffer)
580 (save-restriction
581 (widen)
582 (goto-char (rmail-msgbeg i))
583 (search-forward "\n*** EOOH ***\n")
584 (let ((beg (point)) end)
585 (search-forward "\n\n")
586 (setq end (point))
587 (goto-char beg)
588 (setq done (re-search-forward search-regexp end t))))))
589 (if done (setq found i)))
590 (setq n (if forward (1- n) (1+ n)))))
591 (if found
592 (rmail-summary-goto-msg found)
593 (error "No %s message with same subject"
594 (if forward "following" "previous")))))
596 (defun rmail-summary-previous-same-subject (n)
597 "Go to the previous message in the summary having the same subject.
598 With prefix argument N, do this N times.
599 If N is negative, go forwards instead."
600 (interactive "p")
601 (rmail-summary-next-same-subject (- n)))
603 ;; Delete and undelete summary commands.
605 (defun rmail-summary-delete-forward (&optional count)
606 "Delete this message and move to next nondeleted one.
607 Deleted messages stay in the file until the \\[rmail-expunge] command is given.
608 A prefix argument serves as a repeat count;
609 a negative argument means to delete and move backward."
610 (interactive "p")
611 (unless (numberp count) (setq count 1))
612 (let (end del-msg
613 (backward (< count 0)))
614 (while (/= count 0)
615 (rmail-summary-goto-msg)
616 (with-current-buffer rmail-buffer
617 (rmail-delete-message)
618 (setq del-msg rmail-current-message))
619 (rmail-summary-mark-deleted del-msg)
620 (while (and (not (if backward (bobp) (eobp)))
621 (save-excursion (beginning-of-line)
622 (looking-at " *[0-9]+D")))
623 (forward-line (if backward -1 1)))
624 ;; It looks ugly to move to the empty line at end of buffer.
625 (and (eobp) (not backward)
626 (forward-line -1))
627 (setq count
628 (if (> count 0) (1- count) (1+ count))))))
630 (defun rmail-summary-delete-backward (&optional count)
631 "Delete this message and move to previous nondeleted one.
632 Deleted messages stay in the file until the \\[rmail-expunge] command is given.
633 A prefix argument serves as a repeat count;
634 a negative argument means to delete and move forward."
635 (interactive "p")
636 (rmail-summary-delete-forward (- count)))
638 (defun rmail-summary-mark-deleted (&optional n undel)
639 ;; Since third arg is t, this only alters the summary, not the Rmail buf.
640 (and n (rmail-summary-goto-msg n t t))
641 (or (eobp)
642 (not (overlay-get rmail-summary-overlay 'face))
643 (let ((buffer-read-only nil))
644 (skip-chars-forward " ")
645 (skip-chars-forward "[0-9]")
646 (if undel
647 (if (looking-at "D")
648 (progn (delete-char 1) (insert " ")))
649 (delete-char 1)
650 (insert "D"))))
651 (beginning-of-line))
653 (defun rmail-summary-mark-undeleted (n)
654 (rmail-summary-mark-deleted n t))
656 (defun rmail-summary-deleted-p (&optional n)
657 (save-excursion
658 (and n (rmail-summary-goto-msg n nil t))
659 (skip-chars-forward " ")
660 (skip-chars-forward "[0-9]")
661 (looking-at "D")))
663 (defun rmail-summary-undelete (&optional arg)
664 "Undelete current message.
665 Optional prefix ARG means undelete ARG previous messages."
666 (interactive "p")
667 (if (/= arg 1)
668 (rmail-summary-undelete-many arg)
669 (let ((buffer-read-only nil)
670 (opoint (point)))
671 (end-of-line)
672 (cond ((re-search-backward "\\(^ *[0-9]*\\)\\(D\\)" nil t)
673 (replace-match "\\1 ")
674 (rmail-summary-goto-msg)
675 (if rmail-enable-mime
676 (set-buffer rmail-buffer)
677 (pop-to-buffer rmail-buffer))
678 (and (rmail-message-deleted-p rmail-current-message)
679 (rmail-undelete-previous-message))
680 (if rmail-enable-mime
681 (pop-to-buffer rmail-view-buffer))
682 (pop-to-buffer rmail-summary-buffer))
683 (t (goto-char opoint))))))
685 (defun rmail-summary-undelete-many (&optional n)
686 "Undelete all deleted msgs, optional prefix arg N means undelete N prev msgs."
687 (interactive "P")
688 (save-excursion
689 (set-buffer rmail-buffer)
690 (let* ((init-msg (if n rmail-current-message rmail-total-messages))
691 (rmail-current-message init-msg)
692 (n (or n rmail-total-messages))
693 (msgs-undeled 0))
694 (while (and (> rmail-current-message 0)
695 (< msgs-undeled n))
696 (if (rmail-message-deleted-p rmail-current-message)
697 (progn (rmail-set-attribute "deleted" nil)
698 (setq msgs-undeled (1+ msgs-undeled))))
699 (setq rmail-current-message (1- rmail-current-message)))
700 (set-buffer rmail-summary-buffer)
701 (setq rmail-current-message init-msg msgs-undeled 0)
702 (while (and (> rmail-current-message 0)
703 (< msgs-undeled n))
704 (if (rmail-summary-deleted-p rmail-current-message)
705 (progn (rmail-summary-mark-undeleted rmail-current-message)
706 (setq msgs-undeled (1+ msgs-undeled))))
707 (setq rmail-current-message (1- rmail-current-message))))
708 (rmail-summary-goto-msg)))
710 ;; Rmail Summary mode is suitable only for specially formatted data.
711 (put 'rmail-summary-mode 'mode-class 'special)
713 (defun rmail-summary-mode ()
714 "Rmail Summary Mode is invoked from Rmail Mode by using \\<rmail-mode-map>\\[rmail-summary].
715 As commands are issued in the summary buffer, they are applied to the
716 corresponding mail messages in the rmail buffer.
718 All normal editing commands are turned off.
719 Instead, nearly all the Rmail mode commands are available,
720 though many of them move only among the messages in the summary.
722 These additional commands exist:
724 \\[rmail-summary-undelete-many] Undelete all or prefix arg deleted messages.
725 \\[rmail-summary-wipe] Delete the summary and go to the Rmail buffer.
727 Commands for sorting the summary:
729 \\[rmail-summary-sort-by-date] Sort by date.
730 \\[rmail-summary-sort-by-subject] Sort by subject.
731 \\[rmail-summary-sort-by-author] Sort by author.
732 \\[rmail-summary-sort-by-recipient] Sort by recipient.
733 \\[rmail-summary-sort-by-correspondent] Sort by correspondent.
734 \\[rmail-summary-sort-by-lines] Sort by lines.
735 \\[rmail-summary-sort-by-labels] Sort by labels."
736 (interactive)
737 (kill-all-local-variables)
738 (setq major-mode 'rmail-summary-mode)
739 (setq mode-name "RMAIL Summary")
740 (setq truncate-lines t)
741 (setq buffer-read-only t)
742 (set-syntax-table text-mode-syntax-table)
743 (make-local-variable 'rmail-buffer)
744 (make-local-variable 'rmail-view-buffer)
745 (make-local-variable 'rmail-total-messages)
746 (make-local-variable 'rmail-current-message)
747 (setq rmail-current-message nil)
748 (make-local-variable 'rmail-summary-redo)
749 (setq rmail-summary-redo nil)
750 (make-local-variable 'revert-buffer-function)
751 (make-local-variable 'font-lock-defaults)
752 (setq font-lock-defaults '(rmail-summary-font-lock-keywords t))
753 (rmail-summary-enable)
754 (run-hooks 'rmail-summary-mode-hook))
756 ;; Summary features need to be disabled during edit mode.
757 (defun rmail-summary-disable ()
758 (use-local-map text-mode-map)
759 (remove-hook 'post-command-hook 'rmail-summary-rmail-update t)
760 (setq revert-buffer-function nil))
762 (defun rmail-summary-enable ()
763 (use-local-map rmail-summary-mode-map)
764 (add-hook 'post-command-hook 'rmail-summary-rmail-update nil t)
765 (setq revert-buffer-function 'rmail-update-summary))
767 (defvar rmail-summary-put-back-unseen nil
768 "Used for communicating between calls to `rmail-summary-rmail-update'.
769 If it moves to a message within an Incremental Search, and removes
770 the `unseen' attribute from that message, it sets this flag
771 so that if the next motion between messages is in the same Incremental
772 Search, the `unseen' attribute is restored.")
774 ;; Show in Rmail the message described by the summary line that point is on,
775 ;; but only if the Rmail buffer is already visible.
776 ;; This is a post-command-hook in summary buffers.
777 (defun rmail-summary-rmail-update ()
778 (let (buffer-read-only)
779 (save-excursion
780 ;; If at end of buffer, pretend we are on the last text line.
781 (if (eobp)
782 (forward-line -1))
783 (beginning-of-line)
784 (skip-chars-forward " ")
785 (let ((msg-num (string-to-int (buffer-substring
786 (point)
787 (progn (skip-chars-forward "0-9")
788 (point))))))
789 ;; Always leave `unseen' removed
790 ;; if we get out of isearch mode.
791 ;; Don't let a subsequent isearch restore that `unseen'.
792 (if (not isearch-mode)
793 (setq rmail-summary-put-back-unseen nil))
795 (or (eq rmail-current-message msg-num)
796 (let ((window (get-buffer-window rmail-view-buffer t))
797 (owin (selected-window)))
798 (if isearch-mode
799 (save-excursion
800 (set-buffer rmail-buffer)
801 ;; If we first saw the previous message in this search,
802 ;; and we have gone to a different message while searching,
803 ;; put back `unseen' on the former one.
804 (if rmail-summary-put-back-unseen
805 (rmail-set-attribute "unseen" t
806 rmail-current-message))
807 ;; Arrange to do that later, for the new current message,
808 ;; if it still has `unseen'.
809 (setq rmail-summary-put-back-unseen
810 (rmail-message-labels-p msg-num ", ?\\(unseen\\),")))
811 (setq rmail-summary-put-back-unseen nil))
813 ;; Go to the desired message.
814 (setq rmail-current-message msg-num)
816 ;; Update the summary to show the message has been seen.
817 (if (= (following-char) ?-)
818 (progn
819 (delete-char 1)
820 (insert " ")))
822 (if window
823 ;; Using save-window-excursion would cause the new value
824 ;; of point to get lost.
825 (unwind-protect
826 (progn
827 (select-window window)
828 (rmail-show-message msg-num t))
829 (select-window owin))
830 (if (buffer-name rmail-buffer)
831 (save-excursion
832 (set-buffer rmail-buffer)
833 (rmail-show-message msg-num t))))))
834 (rmail-summary-update-highlight nil)))))
836 (defvar rmail-summary-mode-map nil)
838 (if rmail-summary-mode-map
840 (setq rmail-summary-mode-map (make-keymap))
841 (suppress-keymap rmail-summary-mode-map)
843 (define-key rmail-summary-mode-map [mouse-2] 'rmail-summary-mouse-goto-message)
844 (define-key rmail-summary-mode-map "a" 'rmail-summary-add-label)
845 (define-key rmail-summary-mode-map "b" 'rmail-summary-bury)
846 (define-key rmail-summary-mode-map "c" 'rmail-summary-continue)
847 (define-key rmail-summary-mode-map "d" 'rmail-summary-delete-forward)
848 (define-key rmail-summary-mode-map "\C-d" 'rmail-summary-delete-backward)
849 (define-key rmail-summary-mode-map "e" 'rmail-summary-edit-current-message)
850 (define-key rmail-summary-mode-map "f" 'rmail-summary-forward)
851 (define-key rmail-summary-mode-map "g" 'rmail-summary-get-new-mail)
852 (define-key rmail-summary-mode-map "h" 'rmail-summary)
853 (define-key rmail-summary-mode-map "i" 'rmail-summary-input)
854 (define-key rmail-summary-mode-map "j" 'rmail-summary-goto-msg)
855 (define-key rmail-summary-mode-map "\C-m" 'rmail-summary-goto-msg)
856 (define-key rmail-summary-mode-map "k" 'rmail-summary-kill-label)
857 (define-key rmail-summary-mode-map "l" 'rmail-summary-by-labels)
858 (define-key rmail-summary-mode-map "\e\C-h" 'rmail-summary)
859 (define-key rmail-summary-mode-map "\e\C-l" 'rmail-summary-by-labels)
860 (define-key rmail-summary-mode-map "\e\C-r" 'rmail-summary-by-recipients)
861 (define-key rmail-summary-mode-map "\e\C-s" 'rmail-summary-by-regexp)
862 (define-key rmail-summary-mode-map "\e\C-t" 'rmail-summary-by-topic)
863 (define-key rmail-summary-mode-map "m" 'rmail-summary-mail)
864 (define-key rmail-summary-mode-map "\M-m" 'rmail-summary-retry-failure)
865 (define-key rmail-summary-mode-map "n" 'rmail-summary-next-msg)
866 (define-key rmail-summary-mode-map "\en" 'rmail-summary-next-all)
867 (define-key rmail-summary-mode-map "\e\C-n" 'rmail-summary-next-labeled-message)
868 (define-key rmail-summary-mode-map "o" 'rmail-summary-output-to-rmail-file)
869 (define-key rmail-summary-mode-map "\C-o" 'rmail-summary-output)
870 (define-key rmail-summary-mode-map "p" 'rmail-summary-previous-msg)
871 (define-key rmail-summary-mode-map "\ep" 'rmail-summary-previous-all)
872 (define-key rmail-summary-mode-map "\e\C-p" 'rmail-summary-previous-labeled-message)
873 (define-key rmail-summary-mode-map "q" 'rmail-summary-quit)
874 (define-key rmail-summary-mode-map "Q" 'rmail-summary-wipe)
875 (define-key rmail-summary-mode-map "r" 'rmail-summary-reply)
876 (define-key rmail-summary-mode-map "s" 'rmail-summary-expunge-and-save)
877 (define-key rmail-summary-mode-map "\es" 'rmail-summary-search)
878 (define-key rmail-summary-mode-map "t" 'rmail-summary-toggle-header)
879 (define-key rmail-summary-mode-map "u" 'rmail-summary-undelete)
880 (define-key rmail-summary-mode-map "\M-u" 'rmail-summary-undelete-many)
881 (define-key rmail-summary-mode-map "x" 'rmail-summary-expunge)
882 (define-key rmail-summary-mode-map "w" 'rmail-summary-output-body)
883 (define-key rmail-summary-mode-map "." 'rmail-summary-beginning-of-message)
884 (define-key rmail-summary-mode-map "<" 'rmail-summary-first-message)
885 (define-key rmail-summary-mode-map ">" 'rmail-summary-last-message)
886 (define-key rmail-summary-mode-map " " 'rmail-summary-scroll-msg-up)
887 (define-key rmail-summary-mode-map "\177" 'rmail-summary-scroll-msg-down)
888 (define-key rmail-summary-mode-map "?" 'describe-mode)
889 (define-key rmail-summary-mode-map "\C-c\C-n" 'rmail-summary-next-same-subject)
890 (define-key rmail-summary-mode-map "\C-c\C-p" 'rmail-summary-previous-same-subject)
891 (define-key rmail-summary-mode-map "\C-c\C-s\C-d"
892 'rmail-summary-sort-by-date)
893 (define-key rmail-summary-mode-map "\C-c\C-s\C-s"
894 'rmail-summary-sort-by-subject)
895 (define-key rmail-summary-mode-map "\C-c\C-s\C-a"
896 'rmail-summary-sort-by-author)
897 (define-key rmail-summary-mode-map "\C-c\C-s\C-r"
898 'rmail-summary-sort-by-recipient)
899 (define-key rmail-summary-mode-map "\C-c\C-s\C-c"
900 'rmail-summary-sort-by-correspondent)
901 (define-key rmail-summary-mode-map "\C-c\C-s\C-l"
902 'rmail-summary-sort-by-lines)
903 (define-key rmail-summary-mode-map "\C-c\C-s\C-k"
904 'rmail-summary-sort-by-labels)
907 ;;; Menu bar bindings.
909 (define-key rmail-summary-mode-map [menu-bar] (make-sparse-keymap))
911 (define-key rmail-summary-mode-map [menu-bar classify]
912 (cons "Classify" (make-sparse-keymap "Classify")))
914 (define-key rmail-summary-mode-map [menu-bar classify output-menu]
915 '("Output (Rmail Menu)..." . rmail-summary-output-menu))
917 (define-key rmail-summary-mode-map [menu-bar classify input-menu]
918 '("Input Rmail File (menu)..." . rmail-input-menu))
920 (define-key rmail-summary-mode-map [menu-bar classify input-menu]
921 '(nil))
923 (define-key rmail-summary-mode-map [menu-bar classify output-menu]
924 '(nil))
926 (define-key rmail-summary-mode-map [menu-bar classify output-body]
927 '("Output (body)..." . rmail-summary-output-body))
929 (define-key rmail-summary-mode-map [menu-bar classify output-inbox]
930 '("Output (inbox)..." . rmail-summary-output))
932 (define-key rmail-summary-mode-map [menu-bar classify output]
933 '("Output (Rmail)..." . rmail-summary-output-to-rmail-file))
935 (define-key rmail-summary-mode-map [menu-bar classify kill-label]
936 '("Kill Label..." . rmail-summary-kill-label))
938 (define-key rmail-summary-mode-map [menu-bar classify add-label]
939 '("Add Label..." . rmail-summary-add-label))
941 (define-key rmail-summary-mode-map [menu-bar summary]
942 (cons "Summary" (make-sparse-keymap "Summary")))
944 (define-key rmail-summary-mode-map [menu-bar summary senders]
945 '("By Senders..." . rmail-summary-by-senders))
947 (define-key rmail-summary-mode-map [menu-bar summary labels]
948 '("By Labels..." . rmail-summary-by-labels))
950 (define-key rmail-summary-mode-map [menu-bar summary recipients]
951 '("By Recipients..." . rmail-summary-by-recipients))
953 (define-key rmail-summary-mode-map [menu-bar summary topic]
954 '("By Topic..." . rmail-summary-by-topic))
956 (define-key rmail-summary-mode-map [menu-bar summary regexp]
957 '("By Regexp..." . rmail-summary-by-regexp))
959 (define-key rmail-summary-mode-map [menu-bar summary all]
960 '("All" . rmail-summary))
962 (define-key rmail-summary-mode-map [menu-bar mail]
963 (cons "Mail" (make-sparse-keymap "Mail")))
965 (define-key rmail-summary-mode-map [menu-bar mail rmail-summary-get-new-mail]
966 '("Get New Mail" . rmail-summary-get-new-mail))
968 (define-key rmail-summary-mode-map [menu-bar mail lambda]
969 '("----"))
971 (define-key rmail-summary-mode-map [menu-bar mail continue]
972 '("Continue" . rmail-summary-continue))
974 (define-key rmail-summary-mode-map [menu-bar mail resend]
975 '("Re-send..." . rmail-summary-resend))
977 (define-key rmail-summary-mode-map [menu-bar mail forward]
978 '("Forward" . rmail-summary-forward))
980 (define-key rmail-summary-mode-map [menu-bar mail retry]
981 '("Retry" . rmail-summary-retry-failure))
983 (define-key rmail-summary-mode-map [menu-bar mail reply]
984 '("Reply" . rmail-summary-reply))
986 (define-key rmail-summary-mode-map [menu-bar mail mail]
987 '("Mail" . rmail-summary-mail))
989 (define-key rmail-summary-mode-map [menu-bar delete]
990 (cons "Delete" (make-sparse-keymap "Delete")))
992 (define-key rmail-summary-mode-map [menu-bar delete expunge/save]
993 '("Expunge/Save" . rmail-summary-expunge-and-save))
995 (define-key rmail-summary-mode-map [menu-bar delete expunge]
996 '("Expunge" . rmail-summary-expunge))
998 (define-key rmail-summary-mode-map [menu-bar delete undelete]
999 '("Undelete" . rmail-summary-undelete))
1001 (define-key rmail-summary-mode-map [menu-bar delete delete]
1002 '("Delete" . rmail-summary-delete-forward))
1004 (define-key rmail-summary-mode-map [menu-bar move]
1005 (cons "Move" (make-sparse-keymap "Move")))
1007 (define-key rmail-summary-mode-map [menu-bar move search-back]
1008 '("Search Back..." . rmail-summary-search-backward))
1010 (define-key rmail-summary-mode-map [menu-bar move search]
1011 '("Search..." . rmail-summary-search))
1013 (define-key rmail-summary-mode-map [menu-bar move previous]
1014 '("Previous Nondeleted" . rmail-summary-previous-msg))
1016 (define-key rmail-summary-mode-map [menu-bar move next]
1017 '("Next Nondeleted" . rmail-summary-next-msg))
1019 (define-key rmail-summary-mode-map [menu-bar move last]
1020 '("Last" . rmail-summary-last-message))
1022 (define-key rmail-summary-mode-map [menu-bar move first]
1023 '("First" . rmail-summary-first-message))
1025 (define-key rmail-summary-mode-map [menu-bar move previous]
1026 '("Previous" . rmail-summary-previous-all))
1028 (define-key rmail-summary-mode-map [menu-bar move next]
1029 '("Next" . rmail-summary-next-all))
1031 (defvar rmail-summary-overlay nil)
1032 (put 'rmail-summary-overlay 'permanent-local t)
1034 (defun rmail-summary-mouse-goto-message (event)
1035 "Select the message whose summary line you click on."
1036 (interactive "@e")
1037 (goto-char (posn-point (event-end event)))
1038 (rmail-summary-goto-msg))
1040 (defun rmail-summary-goto-msg (&optional n nowarn skip-rmail)
1041 "Go to message N in the summary buffer and the Rmail buffer.
1042 If N is nil, use the message corresponding to point in the summary
1043 and move to that message in the Rmail buffer.
1045 If NOWARN, don't say anything if N is out of range.
1046 If SKIP-RMAIL, don't do anything to the Rmail buffer."
1047 (interactive "P")
1048 (if (consp n) (setq n (prefix-numeric-value n)))
1049 (if (eobp) (forward-line -1))
1050 (beginning-of-line)
1051 (let* ((obuf (current-buffer))
1052 (buf rmail-buffer)
1053 (cur (point))
1054 message-not-found
1055 (curmsg (string-to-int
1056 (buffer-substring (point)
1057 (min (point-max) (+ 6 (point))))))
1058 (total (save-excursion (set-buffer buf) rmail-total-messages)))
1059 ;; If message number N was specified, find that message's line
1060 ;; or set message-not-found.
1061 ;; If N wasn't specified or that message can't be found.
1062 ;; set N by default.
1063 (if (not n)
1064 (setq n curmsg)
1065 (if (< n 1)
1066 (progn (message "No preceding message")
1067 (setq n 1)))
1068 (if (> n total)
1069 (progn (message "No following message")
1070 (goto-char (point-max))
1071 (rmail-summary-goto-msg nil nowarn skip-rmail)))
1072 (goto-char (point-min))
1073 (if (not (re-search-forward (format "^%5d[^0-9]" n) nil t))
1074 (progn (or nowarn (message "Message %d not found" n))
1075 (setq n curmsg)
1076 (setq message-not-found t)
1077 (goto-char cur))))
1078 (beginning-of-line)
1079 (skip-chars-forward " ")
1080 (skip-chars-forward "0-9")
1081 (save-excursion (if (= (following-char) ?-)
1082 (let ((buffer-read-only nil))
1083 (delete-char 1)
1084 (insert " "))))
1085 (rmail-summary-update-highlight message-not-found)
1086 (beginning-of-line)
1087 (if skip-rmail
1089 (let ((selwin (selected-window)))
1090 (unwind-protect
1091 (progn (pop-to-buffer buf)
1092 (rmail-show-message n))
1093 (select-window selwin)
1094 ;; The actions above can alter the current buffer. Preserve it.
1095 (set-buffer obuf))))))
1097 ;; Update the highlighted line in an rmail summary buffer.
1098 ;; That should be current. We highlight the line point is on.
1099 ;; If NOT-FOUND is non-nil, we turn off highlighting.
1100 (defun rmail-summary-update-highlight (not-found)
1101 ;; Make sure we have an overlay to use.
1102 (or rmail-summary-overlay
1103 (progn
1104 (make-local-variable 'rmail-summary-overlay)
1105 (setq rmail-summary-overlay (make-overlay (point) (point)))))
1106 ;; If this message is in the summary, use the overlay to highlight it.
1107 ;; Otherwise, don't highlight anything.
1108 (if not-found
1109 (overlay-put rmail-summary-overlay 'face nil)
1110 (move-overlay rmail-summary-overlay
1111 (save-excursion (beginning-of-line)
1112 (skip-chars-forward " ")
1113 (point))
1114 (save-excursion (end-of-line) (point)))
1115 (overlay-put rmail-summary-overlay 'face 'highlight)))
1117 (defun rmail-summary-scroll-msg-up (&optional dist)
1118 "Scroll the Rmail window forward.
1119 If the Rmail window is displaying the end of a message,
1120 advance to the next message."
1121 (interactive "P")
1122 (if (eq dist '-)
1123 (rmail-summary-scroll-msg-down nil)
1124 (let ((rmail-buffer-window (get-buffer-window rmail-view-buffer)))
1125 (if rmail-buffer-window
1126 (if (let ((rmail-summary-window (selected-window)))
1127 (select-window rmail-buffer-window)
1128 (prog1
1129 ;; Is EOB visible in the buffer?
1130 (save-excursion
1131 (let ((ht (window-height (selected-window))))
1132 (move-to-window-line (- ht 2))
1133 (end-of-line)
1134 (eobp)))
1135 (select-window rmail-summary-window)))
1136 (if (not rmail-summary-scroll-between-messages)
1137 (error "End of buffer")
1138 (rmail-summary-next-msg (or dist 1)))
1139 (let ((other-window-scroll-buffer rmail-view-buffer))
1140 (scroll-other-window dist)))
1141 ;; If it isn't visible at all, show the beginning.
1142 (rmail-summary-beginning-of-message)))))
1144 (defun rmail-summary-scroll-msg-down (&optional dist)
1145 "Scroll the Rmail window backward.
1146 If the Rmail window is now displaying the beginning of a message,
1147 move to the previous message."
1148 (interactive "P")
1149 (if (eq dist '-)
1150 (rmail-summary-scroll-msg-up nil)
1151 (let ((rmail-buffer-window (get-buffer-window rmail-view-buffer)))
1152 (if rmail-buffer-window
1153 (if (let ((rmail-summary-window (selected-window)))
1154 (select-window rmail-buffer-window)
1155 (prog1
1156 ;; Is BOB visible in the buffer?
1157 (save-excursion
1158 (move-to-window-line 0)
1159 (beginning-of-line)
1160 (bobp))
1161 (select-window rmail-summary-window)))
1162 (if (not rmail-summary-scroll-between-messages)
1163 (error "Beginning of buffer")
1164 (rmail-summary-previous-msg (or dist 1)))
1165 (let ((other-window-scroll-buffer rmail-view-buffer))
1166 (scroll-other-window-down dist)))
1167 ;; If it isn't visible at all, show the beginning.
1168 (rmail-summary-beginning-of-message)))))
1170 (defun rmail-summary-beginning-of-message ()
1171 "Show current message from the beginning."
1172 (interactive)
1173 (if (and (one-window-p) (not pop-up-frames))
1174 ;; If there is just one window, put the summary on the top.
1175 (let ((buffer rmail-view-buffer))
1176 (split-window (selected-window) rmail-summary-window-size)
1177 (select-window (frame-first-window))
1178 (pop-to-buffer rmail-view-buffer)
1179 ;; If pop-to-buffer did not use that window, delete that
1180 ;; window. (This can happen if it uses another frame.)
1181 (or (eq buffer (window-buffer (next-window (frame-first-window))))
1182 (delete-other-windows)))
1183 (pop-to-buffer rmail-view-buffer))
1184 (beginning-of-buffer)
1185 (pop-to-buffer rmail-summary-buffer))
1187 (defun rmail-summary-bury ()
1188 "Bury the Rmail buffer and the Rmail summary buffer."
1189 (interactive)
1190 (let ((buffer-to-bury (current-buffer)))
1191 (let (window)
1192 (while (setq window (get-buffer-window rmail-buffer))
1193 (set-window-buffer window (other-buffer rmail-buffer)))
1194 (bury-buffer rmail-buffer))
1195 (switch-to-buffer (other-buffer buffer-to-bury))
1196 (bury-buffer buffer-to-bury)))
1198 (defun rmail-summary-quit ()
1199 "Quit out of Rmail and Rmail summary."
1200 (interactive)
1201 (rmail-summary-wipe)
1202 (rmail-quit))
1204 (defun rmail-summary-wipe ()
1205 "Kill and wipe away Rmail summary, remaining within Rmail."
1206 (interactive)
1207 (save-excursion (set-buffer rmail-buffer) (setq rmail-summary-buffer nil))
1208 (let ((local-rmail-buffer rmail-view-buffer))
1209 (kill-buffer (current-buffer))
1210 ;; Delete window if not only one.
1211 (if (not (eq (selected-window) (next-window nil 'no-minibuf)))
1212 (delete-window))
1213 ;; Switch windows to the rmail buffer, or switch to it in this window.
1214 (pop-to-buffer local-rmail-buffer)))
1216 (defun rmail-summary-expunge ()
1217 "Actually erase all deleted messages and recompute summary headers."
1218 (interactive)
1219 (save-excursion
1220 (set-buffer rmail-buffer)
1221 (when (rmail-expunge-confirmed)
1222 (rmail-only-expunge)))
1223 (rmail-update-summary))
1225 (defun rmail-summary-expunge-and-save ()
1226 "Expunge and save RMAIL file."
1227 (interactive)
1228 (save-excursion
1229 (set-buffer rmail-buffer)
1230 (when (rmail-expunge-confirmed)
1231 (rmail-only-expunge)))
1232 (rmail-update-summary)
1233 (save-excursion
1234 (set-buffer rmail-buffer)
1235 (save-buffer))
1236 (set-buffer-modified-p nil))
1238 (defun rmail-summary-get-new-mail (&optional file-name)
1239 "Get new mail and recompute summary headers.
1241 Optionally you can specify the file to get new mail from. In this case,
1242 the file of new mail is not changed or deleted. Noninteractively, you can
1243 pass the inbox file name as an argument. Interactively, a prefix
1244 argument says to read a file name and use that file as the inbox."
1245 (interactive
1246 (list (if current-prefix-arg
1247 (read-file-name "Get new mail from file: "))))
1248 (let (msg)
1249 (save-excursion
1250 (set-buffer rmail-buffer)
1251 (rmail-get-new-mail file-name)
1252 ;; Get the proper new message number.
1253 (setq msg rmail-current-message))
1254 ;; Make sure that message is displayed.
1255 (or (zerop msg)
1256 (rmail-summary-goto-msg msg))))
1258 (defun rmail-summary-input (filename)
1259 "Run Rmail on file FILENAME."
1260 (interactive "FRun rmail on RMAIL file: ")
1261 ;; We switch windows here, then display the other Rmail file there.
1262 (pop-to-buffer rmail-buffer)
1263 (rmail filename))
1265 (defun rmail-summary-first-message ()
1266 "Show first message in Rmail file from summary buffer."
1267 (interactive)
1268 (beginning-of-buffer))
1270 (defun rmail-summary-last-message ()
1271 "Show last message in Rmail file from summary buffer."
1272 (interactive)
1273 (end-of-buffer)
1274 (forward-line -1))
1276 (defvar rmail-summary-edit-map nil)
1277 (if rmail-summary-edit-map
1279 (setq rmail-summary-edit-map
1280 (nconc (make-sparse-keymap) text-mode-map))
1281 (define-key rmail-summary-edit-map "\C-c\C-c" 'rmail-cease-edit)
1282 (define-key rmail-summary-edit-map "\C-c\C-]" 'rmail-abort-edit))
1284 (defun rmail-summary-edit-current-message ()
1285 "Edit the contents of this message."
1286 (interactive)
1287 (pop-to-buffer rmail-buffer)
1288 (rmail-edit-current-message)
1289 (use-local-map rmail-summary-edit-map))
1291 (defun rmail-summary-cease-edit ()
1292 "Finish editing message, then go back to Rmail summary buffer."
1293 (interactive)
1294 (rmail-cease-edit)
1295 (pop-to-buffer rmail-summary-buffer))
1297 (defun rmail-summary-abort-edit ()
1298 "Abort edit of current message; restore original contents.
1299 Go back to summary buffer."
1300 (interactive)
1301 (rmail-abort-edit)
1302 (pop-to-buffer rmail-summary-buffer))
1304 (defun rmail-summary-search-backward (regexp &optional n)
1305 "Show message containing next match for REGEXP.
1306 Prefix argument gives repeat count; negative argument means search
1307 backwards (through earlier messages).
1308 Interactively, empty argument means use same regexp used last time."
1309 (interactive
1310 (let* ((reversep (>= (prefix-numeric-value current-prefix-arg) 0))
1311 (prompt
1312 (concat (if reversep "Reverse " "") "Rmail search (regexp): "))
1313 regexp)
1314 (if rmail-search-last-regexp
1315 (setq prompt (concat prompt
1316 "(default "
1317 rmail-search-last-regexp
1318 ") ")))
1319 (setq regexp (read-string prompt))
1320 (cond ((not (equal regexp ""))
1321 (setq rmail-search-last-regexp regexp))
1322 ((not rmail-search-last-regexp)
1323 (error "No previous Rmail search string")))
1324 (list rmail-search-last-regexp
1325 (prefix-numeric-value current-prefix-arg))))
1326 ;; Don't use save-excursion because that prevents point from moving
1327 ;; properly in the summary buffer.
1328 (let ((buffer (current-buffer)))
1329 (unwind-protect
1330 (progn
1331 (set-buffer rmail-buffer)
1332 (rmail-search regexp (- n)))
1333 (set-buffer buffer))))
1335 (defun rmail-summary-search (regexp &optional n)
1336 "Show message containing next match for REGEXP.
1337 Prefix argument gives repeat count; negative argument means search
1338 backwards (through earlier messages).
1339 Interactively, empty argument means use same regexp used last time."
1340 (interactive
1341 (let* ((reversep (< (prefix-numeric-value current-prefix-arg) 0))
1342 (prompt
1343 (concat (if reversep "Reverse " "") "Rmail search (regexp): "))
1344 regexp)
1345 (if rmail-search-last-regexp
1346 (setq prompt (concat prompt
1347 "(default "
1348 rmail-search-last-regexp
1349 ") ")))
1350 (setq regexp (read-string prompt))
1351 (cond ((not (equal regexp ""))
1352 (setq rmail-search-last-regexp regexp))
1353 ((not rmail-search-last-regexp)
1354 (error "No previous Rmail search string")))
1355 (list rmail-search-last-regexp
1356 (prefix-numeric-value current-prefix-arg))))
1357 ;; Don't use save-excursion because that prevents point from moving
1358 ;; properly in the summary buffer.
1359 (let ((buffer (current-buffer)))
1360 (unwind-protect
1361 (progn
1362 (set-buffer rmail-buffer)
1363 (rmail-search regexp n))
1364 (set-buffer buffer))))
1366 (defun rmail-summary-toggle-header ()
1367 "Show original message header if pruned header currently shown, or vice versa."
1368 (interactive)
1369 (save-window-excursion
1370 (set-buffer rmail-buffer)
1371 (rmail-toggle-header))
1372 ;; Inside save-excursion, some changes to point in the RMAIL buffer are lost.
1373 ;; Set point to point-min in the RMAIL buffer, if it is visible.
1374 (let ((window (get-buffer-window rmail-view-buffer)))
1375 (if window
1376 ;; Using save-window-excursion would lose the new value of point.
1377 (let ((owin (selected-window)))
1378 (unwind-protect
1379 (progn
1380 (select-window window)
1381 (goto-char (point-min)))
1382 (select-window owin))))))
1385 (defun rmail-summary-add-label (label)
1386 "Add LABEL to labels associated with current Rmail message.
1387 Completion is performed over known labels when reading."
1388 (interactive (list (save-excursion
1389 (set-buffer rmail-buffer)
1390 (rmail-read-label "Add label"))))
1391 (save-excursion
1392 (set-buffer rmail-buffer)
1393 (rmail-add-label label)))
1395 (defun rmail-summary-kill-label (label)
1396 "Remove LABEL from labels associated with current Rmail message.
1397 Completion is performed over known labels when reading."
1398 (interactive (list (save-excursion
1399 (set-buffer rmail-buffer)
1400 (rmail-read-label "Kill label"))))
1401 (save-excursion
1402 (set-buffer rmail-buffer)
1403 (rmail-set-label label nil)))
1405 ;;;; *** Rmail Summary Mailing Commands ***
1407 (defun rmail-summary-override-mail-send-and-exit ()
1408 "Replace bindings to 'mail-send-and-exit with 'rmail-summary-send-and-exit"
1409 (use-local-map (copy-keymap (current-local-map)))
1410 (dolist (key (where-is-internal 'mail-send-and-exit))
1411 (define-key (current-local-map) key 'rmail-summary-send-and-exit)))
1413 (defun rmail-summary-mail ()
1414 "Send mail in another window.
1415 While composing the message, use \\[mail-yank-original] to yank the
1416 original message into it."
1417 (interactive)
1418 (let ((window (get-buffer-window rmail-buffer)))
1419 (if window
1420 (select-window window)
1421 (set-buffer rmail-buffer)))
1422 (rmail-start-mail nil nil nil nil nil (current-buffer))
1423 (rmail-summary-override-mail-send-and-exit))
1425 (defun rmail-summary-continue ()
1426 "Continue composing outgoing message previously being composed."
1427 (interactive)
1428 (let ((window (get-buffer-window rmail-buffer)))
1429 (if window
1430 (select-window window)
1431 (set-buffer rmail-buffer)))
1432 (rmail-start-mail t))
1434 (defun rmail-summary-reply (just-sender)
1435 "Reply to the current message.
1436 Normally include CC: to all other recipients of original message;
1437 prefix argument means ignore them. While composing the reply,
1438 use \\[mail-yank-original] to yank the original message into it."
1439 (interactive "P")
1440 (let ((window (get-buffer-window rmail-view-buffer)))
1441 (if window
1442 (select-window window)
1443 (set-buffer rmail-view-buffer)))
1444 (rmail-reply just-sender)
1445 (rmail-summary-override-mail-send-and-exit))
1447 (defun rmail-summary-retry-failure ()
1448 "Edit a mail message which is based on the contents of the current message.
1449 For a message rejected by the mail system, extract the interesting headers and
1450 the body of the original message; otherwise copy the current message."
1451 (interactive)
1452 (let ((window (get-buffer-window rmail-buffer)))
1453 (if window
1454 (select-window window)
1455 (set-buffer rmail-buffer)))
1456 (rmail-retry-failure)
1457 (rmail-summary-override-mail-send-and-exit))
1459 (defun rmail-summary-send-and-exit ()
1460 "Send mail reply and return to summary buffer."
1461 (interactive)
1462 (mail-send-and-exit t))
1464 (defun rmail-summary-forward (resend)
1465 "Forward the current message to another user.
1466 With prefix argument, \"resend\" the message instead of forwarding it;
1467 see the documentation of `rmail-resend'."
1468 (interactive "P")
1469 (save-excursion
1470 (let ((window (get-buffer-window rmail-buffer)))
1471 (if window
1472 (select-window window)
1473 (set-buffer rmail-buffer)))
1474 (rmail-forward resend)
1475 (rmail-summary-override-mail-send-and-exit)))
1477 (defun rmail-summary-resend ()
1478 "Resend current message using 'rmail-resend'."
1479 (interactive)
1480 (save-excursion
1481 (let ((window (get-buffer-window rmail-buffer)))
1482 (if window
1483 (select-window window)
1484 (set-buffer rmail-buffer)))
1485 (call-interactively 'rmail-resend)))
1487 ;; Summary output commands.
1489 (defun rmail-summary-output-to-rmail-file (&optional file-name n)
1490 "Append the current message to an Rmail file named FILE-NAME.
1491 If the file does not exist, ask if it should be created.
1492 If file is being visited, the message is appended to the Emacs
1493 buffer visiting that file.
1495 A prefix argument N says to output N consecutive messages
1496 starting with the current one. Deleted messages are skipped and don't count."
1497 (interactive
1498 (progn (require 'rmailout)
1499 (list (rmail-output-read-rmail-file-name)
1500 (prefix-numeric-value current-prefix-arg))))
1501 (let ((i 0) prev-msg)
1502 (while
1503 (and (< i n)
1504 (progn (rmail-summary-goto-msg)
1505 (not (eq prev-msg
1506 (setq prev-msg
1507 (with-current-buffer rmail-buffer
1508 rmail-current-message))))))
1509 (setq i (1+ i))
1510 (with-current-buffer rmail-buffer
1511 (let ((rmail-delete-after-output nil))
1512 (rmail-output-to-rmail-file file-name 1)))
1513 (if rmail-delete-after-output
1514 (rmail-summary-delete-forward nil)
1515 (if (< i n)
1516 (rmail-summary-next-msg 1))))))
1518 (defun rmail-summary-output (&optional file-name n)
1519 "Append this message to Unix mail file named FILE-NAME.
1521 A prefix argument N says to output N consecutive messages
1522 starting with the current one. Deleted messages are skipped and don't count."
1523 (interactive
1524 (progn (require 'rmailout)
1525 (list (rmail-output-read-file-name)
1526 (prefix-numeric-value current-prefix-arg))))
1527 (let ((i 0) prev-msg)
1528 (while
1529 (and (< i n)
1530 (progn (rmail-summary-goto-msg)
1531 (not (eq prev-msg
1532 (setq prev-msg
1533 (with-current-buffer rmail-buffer
1534 rmail-current-message))))))
1535 (setq i (1+ i))
1536 (with-current-buffer rmail-buffer
1537 (let ((rmail-delete-after-output nil))
1538 (rmail-output file-name 1)))
1539 (if rmail-delete-after-output
1540 (rmail-summary-delete-forward nil)
1541 (if (< i n)
1542 (rmail-summary-next-msg 1))))))
1544 (defun rmail-summary-output-menu ()
1545 "Output current message to another Rmail file, chosen with a menu.
1546 Also set the default for subsequent \\[rmail-output-to-rmail-file] commands.
1547 The variables `rmail-secondary-file-directory' and
1548 `rmail-secondary-file-regexp' control which files are offered in the menu."
1549 (interactive)
1550 (save-excursion
1551 (set-buffer rmail-buffer)
1552 (let ((rmail-delete-after-output nil))
1553 (call-interactively 'rmail-output-menu)))
1554 (if rmail-delete-after-output
1555 (rmail-summary-delete-forward nil)))
1557 (defun rmail-summary-construct-io-menu ()
1558 (let ((files (rmail-find-all-files rmail-secondary-file-directory)))
1559 (if files
1560 (progn
1561 (define-key rmail-summary-mode-map [menu-bar classify input-menu]
1562 (cons "Input Rmail File"
1563 (rmail-list-to-menu "Input Rmail File"
1564 files
1565 'rmail-summary-input)))
1566 (define-key rmail-summary-mode-map [menu-bar classify output-menu]
1567 (cons "Output Rmail File"
1568 (rmail-list-to-menu "Output Rmail File"
1569 files
1570 'rmail-summary-output-to-rmail-file))))
1571 (define-key rmail-summary-mode-map [menu-bar classify input-menu]
1572 '("Input Rmail File" . rmail-disable-menu))
1573 (define-key rmail-summary-mode-map [menu-bar classify output-menu]
1574 '("Output Rmail File" . rmail-disable-menu)))))
1576 (defun rmail-summary-output-body (&optional file-name)
1577 "Write this message body to the file FILE-NAME.
1578 FILE-NAME defaults, interactively, from the Subject field of the message."
1579 (interactive)
1580 (save-excursion
1581 (set-buffer rmail-buffer)
1582 (let ((rmail-delete-after-output nil))
1583 (if file-name
1584 (rmail-output-body-to-file file-name)
1585 (call-interactively 'rmail-output-body-to-file))))
1586 (if rmail-delete-after-output
1587 (rmail-summary-delete-forward nil)))
1589 ;; Sorting messages in Rmail Summary buffer.
1591 (defun rmail-summary-sort-by-date (reverse)
1592 "Sort messages of current Rmail summary by date.
1593 If prefix argument REVERSE is non-nil, sort them in reverse order."
1594 (interactive "P")
1595 (rmail-sort-from-summary (function rmail-sort-by-date) reverse))
1597 (defun rmail-summary-sort-by-subject (reverse)
1598 "Sort messages of current Rmail summary by subject.
1599 If prefix argument REVERSE is non-nil, sort them in reverse order."
1600 (interactive "P")
1601 (rmail-sort-from-summary (function rmail-sort-by-subject) reverse))
1603 (defun rmail-summary-sort-by-author (reverse)
1604 "Sort messages of current Rmail summary by author.
1605 If prefix argument REVERSE is non-nil, sort them in reverse order."
1606 (interactive "P")
1607 (rmail-sort-from-summary (function rmail-sort-by-author) reverse))
1609 (defun rmail-summary-sort-by-recipient (reverse)
1610 "Sort messages of current Rmail summary by recipient.
1611 If prefix argument REVERSE is non-nil, sort them in reverse order."
1612 (interactive "P")
1613 (rmail-sort-from-summary (function rmail-sort-by-recipient) reverse))
1615 (defun rmail-summary-sort-by-correspondent (reverse)
1616 "Sort messages of current Rmail summary by other correspondent.
1617 If prefix argument REVERSE is non-nil, sort them in reverse order."
1618 (interactive "P")
1619 (rmail-sort-from-summary (function rmail-sort-by-correspondent) reverse))
1621 (defun rmail-summary-sort-by-lines (reverse)
1622 "Sort messages of current Rmail summary by lines of the message.
1623 If prefix argument REVERSE is non-nil, sort them in reverse order."
1624 (interactive "P")
1625 (rmail-sort-from-summary (function rmail-sort-by-lines) reverse))
1627 (defun rmail-summary-sort-by-labels (reverse labels)
1628 "Sort messages of current Rmail summary by labels.
1629 If prefix argument REVERSE is non-nil, sort them in reverse order.
1630 KEYWORDS is a comma-separated list of labels."
1631 (interactive "P\nsSort by labels: ")
1632 (rmail-sort-from-summary
1633 (function (lambda (reverse)
1634 (rmail-sort-by-labels reverse labels)))
1635 reverse))
1637 (defun rmail-sort-from-summary (sortfun reverse)
1638 "Sort Rmail messages from Summary buffer and update it after sorting."
1639 (require 'rmailsort)
1640 (let ((selwin (selected-window)))
1641 (unwind-protect
1642 (progn (pop-to-buffer rmail-buffer)
1643 (funcall sortfun reverse))
1644 (select-window selwin))))
1646 (provide 'rmailsum)
1648 ;;; rmailsum.el ends here