(mail-specify-envelope-from): Doc change.
[emacs.git] / lisp / mail / rmailsum.el
blobcb14d6a7c44c2cede7db69a12fa46b66749a0fb6
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 means 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 (let* ((from (and (re-search-forward "^From:[ \t]*" nil t)
400 (mail-strip-quoted-names
401 (buffer-substring
402 (1- (point))
403 ;; Get all the lines of the From field
404 ;; so that we get a whole comment if there is one,
405 ;; so that mail-strip-quoted-names can discard it.
406 (let ((opoint (point)))
407 (while (progn (forward-line 1)
408 (looking-at "[ \t]")))
409 ;; Back up over newline, then trailing spaces or tabs
410 (forward-char -1)
411 (skip-chars-backward " \t")
412 (point))))))
413 len mch lo)
414 (if (or (null from)
415 (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 ;; No From field, or it's this user.
431 (save-excursion
432 (goto-char (point-min))
433 (if (not (re-search-forward "^To:[ \t]*" nil t))
435 (setq from
436 (concat "to: "
437 (mail-strip-quoted-names
438 (buffer-substring
439 (point)
440 (progn (end-of-line)
441 (skip-chars-backward " \t")
442 (point)))))))))
443 (if (null from)
445 (setq len (length from))
446 (setq mch (string-match "[@%]" from))
447 (format "%25s"
448 (if (or (not mch) (<= len 25))
449 (substring from (max 0 (- len 25)))
450 (substring from
451 (setq lo (cond ((< (- mch 14) 0) 0)
452 ((< len (+ mch 11))
453 (- len 25))
454 (t (- mch 14))))
455 (min len (+ lo 25))))))))
456 (if rmail-summary-line-count-flag
457 (save-excursion
458 (save-restriction
459 (widen)
460 (let ((beg (rmail-msgbeg msgnum))
461 (end (rmail-msgend msgnum))
462 lines)
463 (save-excursion
464 (goto-char beg)
465 ;; Count only lines in the reformatted header,
466 ;; if we have reformatted it.
467 (search-forward "\n*** EOOH ***\n" end t)
468 (setq lines (count-lines (point) end)))
469 (format (cond
470 ((<= lines 9) " [%d]")
471 ((<= lines 99) " [%d]")
472 ((<= lines 999) " [%3d]")
473 (t "[%d]"))
474 lines))))
475 " ")
476 " #" ;The # is part of the format.
477 (if (re-search-forward "^Subject:" nil t)
478 (progn (skip-chars-forward " \t")
479 (buffer-substring (point)
480 (progn (end-of-line)
481 (point))))
482 (re-search-forward "[\n][\n]+" nil t)
483 (buffer-substring (point) (progn (end-of-line) (point))))
484 "\n"))
486 ;; Simple motion in a summary buffer.
488 (defun rmail-summary-next-all (&optional number)
489 (interactive "p")
490 (forward-line (if number number 1))
491 ;; It doesn't look nice to move forward past the last message line.
492 (and (eobp) (> number 0)
493 (forward-line -1))
494 (display-buffer rmail-buffer))
496 (defun rmail-summary-previous-all (&optional number)
497 (interactive "p")
498 (forward-line (- (if number number 1)))
499 ;; It doesn't look nice to move forward past the last message line.
500 (and (eobp) (< number 0)
501 (forward-line -1))
502 (display-buffer rmail-buffer))
504 (defun rmail-summary-next-msg (&optional number)
505 "Display next non-deleted msg from rmail file.
506 With optional prefix argument NUMBER, moves forward this number of non-deleted
507 messages, or backward if NUMBER is negative."
508 (interactive "p")
509 (forward-line 0)
510 (and (> number 0) (end-of-line))
511 (let ((count (if (< number 0) (- number) number))
512 (search (if (> number 0) 're-search-forward 're-search-backward))
513 (non-del-msg-found nil))
514 (while (and (> count 0) (setq non-del-msg-found
515 (or (funcall search "^....[^D]" nil t)
516 non-del-msg-found)))
517 (setq count (1- count))))
518 (beginning-of-line)
519 (display-buffer rmail-view-buffer))
521 (defun rmail-summary-previous-msg (&optional number)
522 "Display previous non-deleted msg from rmail file.
523 With optional prefix argument NUMBER, moves backward this number of
524 non-deleted messages."
525 (interactive "p")
526 (rmail-summary-next-msg (- (if number number 1))))
528 (defun rmail-summary-next-labeled-message (n labels)
529 "Show next message with LABELS. Defaults to last labels used.
530 With prefix argument N moves forward N messages with these labels."
531 (interactive "p\nsMove to next msg with labels: ")
532 (let (msg)
533 (save-excursion
534 (set-buffer rmail-buffer)
535 (rmail-next-labeled-message n labels)
536 (setq msg rmail-current-message))
537 (rmail-summary-goto-msg msg)))
539 (defun rmail-summary-previous-labeled-message (n labels)
540 "Show previous message with LABELS. Defaults to last labels used.
541 With prefix argument N moves backward N messages with these labels."
542 (interactive "p\nsMove to previous msg with labels: ")
543 (let (msg)
544 (save-excursion
545 (set-buffer rmail-buffer)
546 (rmail-previous-labeled-message n labels)
547 (setq msg rmail-current-message))
548 (rmail-summary-goto-msg msg)))
550 (defun rmail-summary-next-same-subject (n)
551 "Go to the next message in the summary having the same subject.
552 With prefix argument N, do this N times.
553 If N is negative, go backwards."
554 (interactive "p")
555 (let (subject search-regexp i found
556 (forward (> n 0)))
557 (save-excursion
558 (set-buffer rmail-buffer)
559 (setq subject (mail-fetch-field "Subject"))
560 (setq i rmail-current-message))
561 (if (string-match "Re:[ \t]*" subject)
562 (setq subject (substring subject (match-end 0))))
563 (setq search-regexp (concat "^Subject: *\\(Re: *\\)?"
564 (regexp-quote subject)
565 "\n"))
566 (save-excursion
567 (while (and (/= n 0)
568 (if forward
569 (not (eobp))
570 (not (bobp))))
571 (let (done)
572 (while (and (not done)
573 (if forward
574 (not (eobp))
575 (not (bobp))))
576 ;; Advance thru summary.
577 (forward-line (if forward 1 -1))
578 ;; Get msg number of this line.
579 (setq i (string-to-int
580 (buffer-substring (point)
581 (min (point-max) (+ 6 (point))))))
582 ;; See if that msg has desired subject.
583 (save-excursion
584 (set-buffer rmail-buffer)
585 (save-restriction
586 (widen)
587 (goto-char (rmail-msgbeg i))
588 (search-forward "\n*** EOOH ***\n")
589 (let ((beg (point)) end)
590 (search-forward "\n\n")
591 (setq end (point))
592 (goto-char beg)
593 (setq done (re-search-forward search-regexp end t))))))
594 (if done (setq found i)))
595 (setq n (if forward (1- n) (1+ n)))))
596 (if found
597 (rmail-summary-goto-msg found)
598 (error "No %s message with same subject"
599 (if forward "following" "previous")))))
601 (defun rmail-summary-previous-same-subject (n)
602 "Go to the previous message in the summary having the same subject.
603 With prefix argument N, do this N times.
604 If N is negative, go forwards instead."
605 (interactive "p")
606 (rmail-summary-next-same-subject (- n)))
608 ;; Delete and undelete summary commands.
610 (defun rmail-summary-delete-forward (&optional count)
611 "Delete this message and move to next nondeleted one.
612 Deleted messages stay in the file until the \\[rmail-expunge] command is given.
613 A prefix argument serves as a repeat count;
614 a negative argument means to delete and move backward."
615 (interactive "p")
616 (unless (numberp count) (setq count 1))
617 (let (end del-msg
618 (backward (< count 0)))
619 (while (/= count 0)
620 (rmail-summary-goto-msg)
621 (with-current-buffer rmail-buffer
622 (rmail-delete-message)
623 (setq del-msg rmail-current-message))
624 (rmail-summary-mark-deleted del-msg)
625 (while (and (not (if backward (bobp) (eobp)))
626 (save-excursion (beginning-of-line)
627 (looking-at " *[0-9]+D")))
628 (forward-line (if backward -1 1)))
629 ;; It looks ugly to move to the empty line at end of buffer.
630 (and (eobp) (not backward)
631 (forward-line -1))
632 (setq count
633 (if (> count 0) (1- count) (1+ count))))))
635 (defun rmail-summary-delete-backward (&optional count)
636 "Delete this message and move to previous nondeleted one.
637 Deleted messages stay in the file until the \\[rmail-expunge] command is given.
638 A prefix argument serves as a repeat count;
639 a negative argument means to delete and move forward."
640 (interactive "p")
641 (rmail-summary-delete-forward (- count)))
643 (defun rmail-summary-mark-deleted (&optional n undel)
644 ;; Since third arg is t, this only alters the summary, not the Rmail buf.
645 (and n (rmail-summary-goto-msg n t t))
646 (or (eobp)
647 (not (overlay-get rmail-summary-overlay 'face))
648 (let ((buffer-read-only nil))
649 (skip-chars-forward " ")
650 (skip-chars-forward "[0-9]")
651 (if undel
652 (if (looking-at "D")
653 (progn (delete-char 1) (insert " ")))
654 (delete-char 1)
655 (insert "D"))))
656 (beginning-of-line))
658 (defun rmail-summary-mark-undeleted (n)
659 (rmail-summary-mark-deleted n t))
661 (defun rmail-summary-deleted-p (&optional n)
662 (save-excursion
663 (and n (rmail-summary-goto-msg n nil t))
664 (skip-chars-forward " ")
665 (skip-chars-forward "[0-9]")
666 (looking-at "D")))
668 (defun rmail-summary-undelete (&optional arg)
669 "Undelete current message.
670 Optional prefix ARG means undelete ARG previous messages."
671 (interactive "p")
672 (if (/= arg 1)
673 (rmail-summary-undelete-many arg)
674 (let ((buffer-read-only nil)
675 (opoint (point)))
676 (end-of-line)
677 (cond ((re-search-backward "\\(^ *[0-9]*\\)\\(D\\)" nil t)
678 (replace-match "\\1 ")
679 (rmail-summary-goto-msg)
680 (if rmail-enable-mime
681 (set-buffer rmail-buffer)
682 (pop-to-buffer rmail-buffer))
683 (and (rmail-message-deleted-p rmail-current-message)
684 (rmail-undelete-previous-message))
685 (if rmail-enable-mime
686 (pop-to-buffer rmail-view-buffer))
687 (pop-to-buffer rmail-summary-buffer))
688 (t (goto-char opoint))))))
690 (defun rmail-summary-undelete-many (&optional n)
691 "Undelete all deleted msgs, optional prefix arg N means undelete N prev msgs."
692 (interactive "P")
693 (save-excursion
694 (set-buffer rmail-buffer)
695 (let* ((init-msg (if n rmail-current-message rmail-total-messages))
696 (rmail-current-message init-msg)
697 (n (or n rmail-total-messages))
698 (msgs-undeled 0))
699 (while (and (> rmail-current-message 0)
700 (< msgs-undeled n))
701 (if (rmail-message-deleted-p rmail-current-message)
702 (progn (rmail-set-attribute "deleted" nil)
703 (setq msgs-undeled (1+ msgs-undeled))))
704 (setq rmail-current-message (1- rmail-current-message)))
705 (set-buffer rmail-summary-buffer)
706 (setq rmail-current-message init-msg msgs-undeled 0)
707 (while (and (> rmail-current-message 0)
708 (< msgs-undeled n))
709 (if (rmail-summary-deleted-p rmail-current-message)
710 (progn (rmail-summary-mark-undeleted rmail-current-message)
711 (setq msgs-undeled (1+ msgs-undeled))))
712 (setq rmail-current-message (1- rmail-current-message))))
713 (rmail-summary-goto-msg)))
715 ;; Rmail Summary mode is suitable only for specially formatted data.
716 (put 'rmail-summary-mode 'mode-class 'special)
718 (defun rmail-summary-mode ()
719 "Rmail Summary Mode is invoked from Rmail Mode by using \\<rmail-mode-map>\\[rmail-summary].
720 As commands are issued in the summary buffer, they are applied to the
721 corresponding mail messages in the rmail buffer.
723 All normal editing commands are turned off.
724 Instead, nearly all the Rmail mode commands are available,
725 though many of them move only among the messages in the summary.
727 These additional commands exist:
729 \\[rmail-summary-undelete-many] Undelete all or prefix arg deleted messages.
730 \\[rmail-summary-wipe] Delete the summary and go to the Rmail buffer.
732 Commands for sorting the summary:
734 \\[rmail-summary-sort-by-date] Sort by date.
735 \\[rmail-summary-sort-by-subject] Sort by subject.
736 \\[rmail-summary-sort-by-author] Sort by author.
737 \\[rmail-summary-sort-by-recipient] Sort by recipient.
738 \\[rmail-summary-sort-by-correspondent] Sort by correspondent.
739 \\[rmail-summary-sort-by-lines] Sort by lines.
740 \\[rmail-summary-sort-by-labels] Sort by labels."
741 (interactive)
742 (kill-all-local-variables)
743 (setq major-mode 'rmail-summary-mode)
744 (setq mode-name "RMAIL Summary")
745 (setq truncate-lines t)
746 (setq buffer-read-only t)
747 (set-syntax-table text-mode-syntax-table)
748 (make-local-variable 'rmail-buffer)
749 (make-local-variable 'rmail-view-buffer)
750 (make-local-variable 'rmail-total-messages)
751 (make-local-variable 'rmail-current-message)
752 (setq rmail-current-message nil)
753 (make-local-variable 'rmail-summary-redo)
754 (setq rmail-summary-redo nil)
755 (make-local-variable 'revert-buffer-function)
756 (make-local-variable 'font-lock-defaults)
757 (setq font-lock-defaults '(rmail-summary-font-lock-keywords t))
758 (rmail-summary-enable)
759 (run-hooks 'rmail-summary-mode-hook))
761 ;; Summary features need to be disabled during edit mode.
762 (defun rmail-summary-disable ()
763 (use-local-map text-mode-map)
764 (remove-hook 'post-command-hook 'rmail-summary-rmail-update t)
765 (setq revert-buffer-function nil))
767 (defun rmail-summary-enable ()
768 (use-local-map rmail-summary-mode-map)
769 (add-hook 'post-command-hook 'rmail-summary-rmail-update nil t)
770 (setq revert-buffer-function 'rmail-update-summary))
772 (defvar rmail-summary-put-back-unseen nil
773 "Used for communicating between calls to `rmail-summary-rmail-update'.
774 If it moves to a message within an Incremental Search, and removes
775 the `unseen' attribute from that message, it sets this flag
776 so that if the next motion between messages is in the same Incremental
777 Search, the `unseen' attribute is restored.")
779 ;; Show in Rmail the message described by the summary line that point is on,
780 ;; but only if the Rmail buffer is already visible.
781 ;; This is a post-command-hook in summary buffers.
782 (defun rmail-summary-rmail-update ()
783 (let (buffer-read-only)
784 (save-excursion
785 ;; If at end of buffer, pretend we are on the last text line.
786 (if (eobp)
787 (forward-line -1))
788 (beginning-of-line)
789 (skip-chars-forward " ")
790 (let ((msg-num (string-to-int (buffer-substring
791 (point)
792 (progn (skip-chars-forward "0-9")
793 (point))))))
794 ;; Always leave `unseen' removed
795 ;; if we get out of isearch mode.
796 ;; Don't let a subsequent isearch restore that `unseen'.
797 (if (not isearch-mode)
798 (setq rmail-summary-put-back-unseen nil))
800 (or (eq rmail-current-message msg-num)
801 (let ((window (get-buffer-window rmail-view-buffer t))
802 (owin (selected-window)))
803 (if isearch-mode
804 (save-excursion
805 (set-buffer rmail-buffer)
806 ;; If we first saw the previous message in this search,
807 ;; and we have gone to a different message while searching,
808 ;; put back `unseen' on the former one.
809 (if rmail-summary-put-back-unseen
810 (rmail-set-attribute "unseen" t
811 rmail-current-message))
812 ;; Arrange to do that later, for the new current message,
813 ;; if it still has `unseen'.
814 (setq rmail-summary-put-back-unseen
815 (rmail-message-labels-p msg-num ", ?\\(unseen\\),")))
816 (setq rmail-summary-put-back-unseen nil))
818 ;; Go to the desired message.
819 (setq rmail-current-message msg-num)
821 ;; Update the summary to show the message has been seen.
822 (if (= (following-char) ?-)
823 (progn
824 (delete-char 1)
825 (insert " ")))
827 (if window
828 ;; Using save-window-excursion would cause the new value
829 ;; of point to get lost.
830 (unwind-protect
831 (progn
832 (select-window window)
833 (rmail-show-message msg-num t))
834 (select-window owin))
835 (if (buffer-name rmail-buffer)
836 (save-excursion
837 (set-buffer rmail-buffer)
838 (rmail-show-message msg-num t))))))
839 (rmail-summary-update-highlight nil)))))
841 (defvar rmail-summary-mode-map nil)
843 (if rmail-summary-mode-map
845 (setq rmail-summary-mode-map (make-keymap))
846 (suppress-keymap rmail-summary-mode-map)
848 (define-key rmail-summary-mode-map [mouse-2] 'rmail-summary-mouse-goto-message)
849 (define-key rmail-summary-mode-map "a" 'rmail-summary-add-label)
850 (define-key rmail-summary-mode-map "b" 'rmail-summary-bury)
851 (define-key rmail-summary-mode-map "c" 'rmail-summary-continue)
852 (define-key rmail-summary-mode-map "d" 'rmail-summary-delete-forward)
853 (define-key rmail-summary-mode-map "\C-d" 'rmail-summary-delete-backward)
854 (define-key rmail-summary-mode-map "e" 'rmail-summary-edit-current-message)
855 (define-key rmail-summary-mode-map "f" 'rmail-summary-forward)
856 (define-key rmail-summary-mode-map "g" 'rmail-summary-get-new-mail)
857 (define-key rmail-summary-mode-map "h" 'rmail-summary)
858 (define-key rmail-summary-mode-map "i" 'rmail-summary-input)
859 (define-key rmail-summary-mode-map "j" 'rmail-summary-goto-msg)
860 (define-key rmail-summary-mode-map "\C-m" 'rmail-summary-goto-msg)
861 (define-key rmail-summary-mode-map "k" 'rmail-summary-kill-label)
862 (define-key rmail-summary-mode-map "l" 'rmail-summary-by-labels)
863 (define-key rmail-summary-mode-map "\e\C-h" 'rmail-summary)
864 (define-key rmail-summary-mode-map "\e\C-l" 'rmail-summary-by-labels)
865 (define-key rmail-summary-mode-map "\e\C-r" 'rmail-summary-by-recipients)
866 (define-key rmail-summary-mode-map "\e\C-s" 'rmail-summary-by-regexp)
867 (define-key rmail-summary-mode-map "\e\C-t" 'rmail-summary-by-topic)
868 (define-key rmail-summary-mode-map "m" 'rmail-summary-mail)
869 (define-key rmail-summary-mode-map "\M-m" 'rmail-summary-retry-failure)
870 (define-key rmail-summary-mode-map "n" 'rmail-summary-next-msg)
871 (define-key rmail-summary-mode-map "\en" 'rmail-summary-next-all)
872 (define-key rmail-summary-mode-map "\e\C-n" 'rmail-summary-next-labeled-message)
873 (define-key rmail-summary-mode-map "o" 'rmail-summary-output-to-rmail-file)
874 (define-key rmail-summary-mode-map "\C-o" 'rmail-summary-output)
875 (define-key rmail-summary-mode-map "p" 'rmail-summary-previous-msg)
876 (define-key rmail-summary-mode-map "\ep" 'rmail-summary-previous-all)
877 (define-key rmail-summary-mode-map "\e\C-p" 'rmail-summary-previous-labeled-message)
878 (define-key rmail-summary-mode-map "q" 'rmail-summary-quit)
879 (define-key rmail-summary-mode-map "Q" 'rmail-summary-wipe)
880 (define-key rmail-summary-mode-map "r" 'rmail-summary-reply)
881 (define-key rmail-summary-mode-map "s" 'rmail-summary-expunge-and-save)
882 (define-key rmail-summary-mode-map "\es" 'rmail-summary-search)
883 (define-key rmail-summary-mode-map "t" 'rmail-summary-toggle-header)
884 (define-key rmail-summary-mode-map "u" 'rmail-summary-undelete)
885 (define-key rmail-summary-mode-map "\M-u" 'rmail-summary-undelete-many)
886 (define-key rmail-summary-mode-map "x" 'rmail-summary-expunge)
887 (define-key rmail-summary-mode-map "w" 'rmail-summary-output-body)
888 (define-key rmail-summary-mode-map "." 'rmail-summary-beginning-of-message)
889 (define-key rmail-summary-mode-map "<" 'rmail-summary-first-message)
890 (define-key rmail-summary-mode-map ">" 'rmail-summary-last-message)
891 (define-key rmail-summary-mode-map " " 'rmail-summary-scroll-msg-up)
892 (define-key rmail-summary-mode-map "\177" 'rmail-summary-scroll-msg-down)
893 (define-key rmail-summary-mode-map "?" 'describe-mode)
894 (define-key rmail-summary-mode-map "\C-c\C-n" 'rmail-summary-next-same-subject)
895 (define-key rmail-summary-mode-map "\C-c\C-p" 'rmail-summary-previous-same-subject)
896 (define-key rmail-summary-mode-map "\C-c\C-s\C-d"
897 'rmail-summary-sort-by-date)
898 (define-key rmail-summary-mode-map "\C-c\C-s\C-s"
899 'rmail-summary-sort-by-subject)
900 (define-key rmail-summary-mode-map "\C-c\C-s\C-a"
901 'rmail-summary-sort-by-author)
902 (define-key rmail-summary-mode-map "\C-c\C-s\C-r"
903 'rmail-summary-sort-by-recipient)
904 (define-key rmail-summary-mode-map "\C-c\C-s\C-c"
905 'rmail-summary-sort-by-correspondent)
906 (define-key rmail-summary-mode-map "\C-c\C-s\C-l"
907 'rmail-summary-sort-by-lines)
908 (define-key rmail-summary-mode-map "\C-c\C-s\C-k"
909 'rmail-summary-sort-by-labels)
912 ;;; Menu bar bindings.
914 (define-key rmail-summary-mode-map [menu-bar] (make-sparse-keymap))
916 (define-key rmail-summary-mode-map [menu-bar classify]
917 (cons "Classify" (make-sparse-keymap "Classify")))
919 (define-key rmail-summary-mode-map [menu-bar classify output-menu]
920 '("Output (Rmail Menu)..." . rmail-summary-output-menu))
922 (define-key rmail-summary-mode-map [menu-bar classify input-menu]
923 '("Input Rmail File (menu)..." . rmail-input-menu))
925 (define-key rmail-summary-mode-map [menu-bar classify input-menu]
926 '(nil))
928 (define-key rmail-summary-mode-map [menu-bar classify output-menu]
929 '(nil))
931 (define-key rmail-summary-mode-map [menu-bar classify output-body]
932 '("Output (body)..." . rmail-summary-output-body))
934 (define-key rmail-summary-mode-map [menu-bar classify output-inbox]
935 '("Output (inbox)..." . rmail-summary-output))
937 (define-key rmail-summary-mode-map [menu-bar classify output]
938 '("Output (Rmail)..." . rmail-summary-output-to-rmail-file))
940 (define-key rmail-summary-mode-map [menu-bar classify kill-label]
941 '("Kill Label..." . rmail-summary-kill-label))
943 (define-key rmail-summary-mode-map [menu-bar classify add-label]
944 '("Add Label..." . rmail-summary-add-label))
946 (define-key rmail-summary-mode-map [menu-bar summary]
947 (cons "Summary" (make-sparse-keymap "Summary")))
949 (define-key rmail-summary-mode-map [menu-bar summary senders]
950 '("By Senders..." . rmail-summary-by-senders))
952 (define-key rmail-summary-mode-map [menu-bar summary labels]
953 '("By Labels..." . rmail-summary-by-labels))
955 (define-key rmail-summary-mode-map [menu-bar summary recipients]
956 '("By Recipients..." . rmail-summary-by-recipients))
958 (define-key rmail-summary-mode-map [menu-bar summary topic]
959 '("By Topic..." . rmail-summary-by-topic))
961 (define-key rmail-summary-mode-map [menu-bar summary regexp]
962 '("By Regexp..." . rmail-summary-by-regexp))
964 (define-key rmail-summary-mode-map [menu-bar summary all]
965 '("All" . rmail-summary))
967 (define-key rmail-summary-mode-map [menu-bar mail]
968 (cons "Mail" (make-sparse-keymap "Mail")))
970 (define-key rmail-summary-mode-map [menu-bar mail rmail-summary-get-new-mail]
971 '("Get New Mail" . rmail-summary-get-new-mail))
973 (define-key rmail-summary-mode-map [menu-bar mail lambda]
974 '("----"))
976 (define-key rmail-summary-mode-map [menu-bar mail continue]
977 '("Continue" . rmail-summary-continue))
979 (define-key rmail-summary-mode-map [menu-bar mail resend]
980 '("Re-send..." . rmail-summary-resend))
982 (define-key rmail-summary-mode-map [menu-bar mail forward]
983 '("Forward" . rmail-summary-forward))
985 (define-key rmail-summary-mode-map [menu-bar mail retry]
986 '("Retry" . rmail-summary-retry-failure))
988 (define-key rmail-summary-mode-map [menu-bar mail reply]
989 '("Reply" . rmail-summary-reply))
991 (define-key rmail-summary-mode-map [menu-bar mail mail]
992 '("Mail" . rmail-summary-mail))
994 (define-key rmail-summary-mode-map [menu-bar delete]
995 (cons "Delete" (make-sparse-keymap "Delete")))
997 (define-key rmail-summary-mode-map [menu-bar delete expunge/save]
998 '("Expunge/Save" . rmail-summary-expunge-and-save))
1000 (define-key rmail-summary-mode-map [menu-bar delete expunge]
1001 '("Expunge" . rmail-summary-expunge))
1003 (define-key rmail-summary-mode-map [menu-bar delete undelete]
1004 '("Undelete" . rmail-summary-undelete))
1006 (define-key rmail-summary-mode-map [menu-bar delete delete]
1007 '("Delete" . rmail-summary-delete-forward))
1009 (define-key rmail-summary-mode-map [menu-bar move]
1010 (cons "Move" (make-sparse-keymap "Move")))
1012 (define-key rmail-summary-mode-map [menu-bar move search-back]
1013 '("Search Back..." . rmail-summary-search-backward))
1015 (define-key rmail-summary-mode-map [menu-bar move search]
1016 '("Search..." . rmail-summary-search))
1018 (define-key rmail-summary-mode-map [menu-bar move previous]
1019 '("Previous Nondeleted" . rmail-summary-previous-msg))
1021 (define-key rmail-summary-mode-map [menu-bar move next]
1022 '("Next Nondeleted" . rmail-summary-next-msg))
1024 (define-key rmail-summary-mode-map [menu-bar move last]
1025 '("Last" . rmail-summary-last-message))
1027 (define-key rmail-summary-mode-map [menu-bar move first]
1028 '("First" . rmail-summary-first-message))
1030 (define-key rmail-summary-mode-map [menu-bar move previous]
1031 '("Previous" . rmail-summary-previous-all))
1033 (define-key rmail-summary-mode-map [menu-bar move next]
1034 '("Next" . rmail-summary-next-all))
1036 (defvar rmail-summary-overlay nil)
1037 (put 'rmail-summary-overlay 'permanent-local t)
1039 (defun rmail-summary-mouse-goto-message (event)
1040 "Select the message whose summary line you click on."
1041 (interactive "@e")
1042 (goto-char (posn-point (event-end event)))
1043 (rmail-summary-goto-msg))
1045 (defun rmail-summary-goto-msg (&optional n nowarn skip-rmail)
1046 "Go to message N in the summary buffer and the Rmail buffer.
1047 If N is nil, use the message corresponding to point in the summary
1048 and move to that message in the Rmail buffer.
1050 If NOWARN, don't say anything if N is out of range.
1051 If SKIP-RMAIL, don't do anything to the Rmail buffer."
1052 (interactive "P")
1053 (if (consp n) (setq n (prefix-numeric-value n)))
1054 (if (eobp) (forward-line -1))
1055 (beginning-of-line)
1056 (let* ((obuf (current-buffer))
1057 (buf rmail-buffer)
1058 (cur (point))
1059 message-not-found
1060 (curmsg (string-to-int
1061 (buffer-substring (point)
1062 (min (point-max) (+ 6 (point))))))
1063 (total (save-excursion (set-buffer buf) rmail-total-messages)))
1064 ;; If message number N was specified, find that message's line
1065 ;; or set message-not-found.
1066 ;; If N wasn't specified or that message can't be found.
1067 ;; set N by default.
1068 (if (not n)
1069 (setq n curmsg)
1070 (if (< n 1)
1071 (progn (message "No preceding message")
1072 (setq n 1)))
1073 (if (> n total)
1074 (progn (message "No following message")
1075 (goto-char (point-max))
1076 (rmail-summary-goto-msg nil nowarn skip-rmail)))
1077 (goto-char (point-min))
1078 (if (not (re-search-forward (format "^%5d[^0-9]" n) nil t))
1079 (progn (or nowarn (message "Message %d not found" n))
1080 (setq n curmsg)
1081 (setq message-not-found t)
1082 (goto-char cur))))
1083 (beginning-of-line)
1084 (skip-chars-forward " ")
1085 (skip-chars-forward "0-9")
1086 (save-excursion (if (= (following-char) ?-)
1087 (let ((buffer-read-only nil))
1088 (delete-char 1)
1089 (insert " "))))
1090 (rmail-summary-update-highlight message-not-found)
1091 (beginning-of-line)
1092 (if skip-rmail
1094 (let ((selwin (selected-window)))
1095 (unwind-protect
1096 (progn (pop-to-buffer buf)
1097 (rmail-show-message n))
1098 (select-window selwin)
1099 ;; The actions above can alter the current buffer. Preserve it.
1100 (set-buffer obuf))))))
1102 ;; Update the highlighted line in an rmail summary buffer.
1103 ;; That should be current. We highlight the line point is on.
1104 ;; If NOT-FOUND is non-nil, we turn off highlighting.
1105 (defun rmail-summary-update-highlight (not-found)
1106 ;; Make sure we have an overlay to use.
1107 (or rmail-summary-overlay
1108 (progn
1109 (make-local-variable 'rmail-summary-overlay)
1110 (setq rmail-summary-overlay (make-overlay (point) (point)))))
1111 ;; If this message is in the summary, use the overlay to highlight it.
1112 ;; Otherwise, don't highlight anything.
1113 (if not-found
1114 (overlay-put rmail-summary-overlay 'face nil)
1115 (move-overlay rmail-summary-overlay
1116 (save-excursion (beginning-of-line)
1117 (skip-chars-forward " ")
1118 (point))
1119 (save-excursion (end-of-line) (point)))
1120 (overlay-put rmail-summary-overlay 'face 'highlight)))
1122 (defun rmail-summary-scroll-msg-up (&optional dist)
1123 "Scroll the Rmail window forward.
1124 If the Rmail window is displaying the end of a message,
1125 advance to the next message."
1126 (interactive "P")
1127 (if (eq dist '-)
1128 (rmail-summary-scroll-msg-down nil)
1129 (let ((rmail-buffer-window (get-buffer-window rmail-view-buffer)))
1130 (if rmail-buffer-window
1131 (if (let ((rmail-summary-window (selected-window)))
1132 (select-window rmail-buffer-window)
1133 (prog1
1134 ;; Is EOB visible in the buffer?
1135 (save-excursion
1136 (let ((ht (window-height (selected-window))))
1137 (move-to-window-line (- ht 2))
1138 (end-of-line)
1139 (eobp)))
1140 (select-window rmail-summary-window)))
1141 (if (not rmail-summary-scroll-between-messages)
1142 (error "End of buffer")
1143 (rmail-summary-next-msg (or dist 1)))
1144 (let ((other-window-scroll-buffer rmail-view-buffer))
1145 (scroll-other-window dist)))
1146 ;; If it isn't visible at all, show the beginning.
1147 (rmail-summary-beginning-of-message)))))
1149 (defun rmail-summary-scroll-msg-down (&optional dist)
1150 "Scroll the Rmail window backward.
1151 If the Rmail window is now displaying the beginning of a message,
1152 move to the previous message."
1153 (interactive "P")
1154 (if (eq dist '-)
1155 (rmail-summary-scroll-msg-up nil)
1156 (let ((rmail-buffer-window (get-buffer-window rmail-view-buffer)))
1157 (if rmail-buffer-window
1158 (if (let ((rmail-summary-window (selected-window)))
1159 (select-window rmail-buffer-window)
1160 (prog1
1161 ;; Is BOB visible in the buffer?
1162 (save-excursion
1163 (move-to-window-line 0)
1164 (beginning-of-line)
1165 (bobp))
1166 (select-window rmail-summary-window)))
1167 (if (not rmail-summary-scroll-between-messages)
1168 (error "Beginning of buffer")
1169 (rmail-summary-previous-msg (or dist 1)))
1170 (let ((other-window-scroll-buffer rmail-view-buffer))
1171 (scroll-other-window-down dist)))
1172 ;; If it isn't visible at all, show the beginning.
1173 (rmail-summary-beginning-of-message)))))
1175 (defun rmail-summary-beginning-of-message ()
1176 "Show current message from the beginning."
1177 (interactive)
1178 (if (and (one-window-p) (not pop-up-frames))
1179 ;; If there is just one window, put the summary on the top.
1180 (let ((buffer rmail-view-buffer))
1181 (split-window (selected-window) rmail-summary-window-size)
1182 (select-window (frame-first-window))
1183 (pop-to-buffer rmail-view-buffer)
1184 ;; If pop-to-buffer did not use that window, delete that
1185 ;; window. (This can happen if it uses another frame.)
1186 (or (eq buffer (window-buffer (next-window (frame-first-window))))
1187 (delete-other-windows)))
1188 (pop-to-buffer rmail-view-buffer))
1189 (beginning-of-buffer)
1190 (pop-to-buffer rmail-summary-buffer))
1192 (defun rmail-summary-bury ()
1193 "Bury the Rmail buffer and the Rmail summary buffer."
1194 (interactive)
1195 (let ((buffer-to-bury (current-buffer)))
1196 (let (window)
1197 (while (setq window (get-buffer-window rmail-buffer))
1198 (set-window-buffer window (other-buffer rmail-buffer)))
1199 (bury-buffer rmail-buffer))
1200 (switch-to-buffer (other-buffer buffer-to-bury))
1201 (bury-buffer buffer-to-bury)))
1203 (defun rmail-summary-quit ()
1204 "Quit out of Rmail and Rmail summary."
1205 (interactive)
1206 (rmail-summary-wipe)
1207 (rmail-quit))
1209 (defun rmail-summary-wipe ()
1210 "Kill and wipe away Rmail summary, remaining within Rmail."
1211 (interactive)
1212 (save-excursion (set-buffer rmail-buffer) (setq rmail-summary-buffer nil))
1213 (let ((local-rmail-buffer rmail-view-buffer))
1214 (kill-buffer (current-buffer))
1215 ;; Delete window if not only one.
1216 (if (not (eq (selected-window) (next-window nil 'no-minibuf)))
1217 (delete-window))
1218 ;; Switch windows to the rmail buffer, or switch to it in this window.
1219 (pop-to-buffer local-rmail-buffer)))
1221 (defun rmail-summary-expunge ()
1222 "Actually erase all deleted messages and recompute summary headers."
1223 (interactive)
1224 (save-excursion
1225 (set-buffer rmail-buffer)
1226 (when (rmail-expunge-confirmed)
1227 (rmail-only-expunge)))
1228 (rmail-update-summary))
1230 (defun rmail-summary-expunge-and-save ()
1231 "Expunge and save RMAIL file."
1232 (interactive)
1233 (save-excursion
1234 (set-buffer rmail-buffer)
1235 (when (rmail-expunge-confirmed)
1236 (rmail-only-expunge)))
1237 (rmail-update-summary)
1238 (save-excursion
1239 (set-buffer rmail-buffer)
1240 (save-buffer))
1241 (set-buffer-modified-p nil))
1243 (defun rmail-summary-get-new-mail (&optional file-name)
1244 "Get new mail and recompute summary headers.
1246 Optionally you can specify the file to get new mail from. In this case,
1247 the file of new mail is not changed or deleted. Noninteractively, you can
1248 pass the inbox file name as an argument. Interactively, a prefix
1249 argument says to read a file name and use that file as the inbox."
1250 (interactive
1251 (list (if current-prefix-arg
1252 (read-file-name "Get new mail from file: "))))
1253 (let (msg)
1254 (save-excursion
1255 (set-buffer rmail-buffer)
1256 (rmail-get-new-mail file-name)
1257 ;; Get the proper new message number.
1258 (setq msg rmail-current-message))
1259 ;; Make sure that message is displayed.
1260 (or (zerop msg)
1261 (rmail-summary-goto-msg msg))))
1263 (defun rmail-summary-input (filename)
1264 "Run Rmail on file FILENAME."
1265 (interactive "FRun rmail on RMAIL file: ")
1266 ;; We switch windows here, then display the other Rmail file there.
1267 (pop-to-buffer rmail-buffer)
1268 (rmail filename))
1270 (defun rmail-summary-first-message ()
1271 "Show first message in Rmail file from summary buffer."
1272 (interactive)
1273 (beginning-of-buffer))
1275 (defun rmail-summary-last-message ()
1276 "Show last message in Rmail file from summary buffer."
1277 (interactive)
1278 (end-of-buffer)
1279 (forward-line -1))
1281 (defvar rmail-summary-edit-map nil)
1282 (if rmail-summary-edit-map
1284 (setq rmail-summary-edit-map
1285 (nconc (make-sparse-keymap) text-mode-map))
1286 (define-key rmail-summary-edit-map "\C-c\C-c" 'rmail-cease-edit)
1287 (define-key rmail-summary-edit-map "\C-c\C-]" 'rmail-abort-edit))
1289 (defun rmail-summary-edit-current-message ()
1290 "Edit the contents of this message."
1291 (interactive)
1292 (pop-to-buffer rmail-buffer)
1293 (rmail-edit-current-message)
1294 (use-local-map rmail-summary-edit-map))
1296 (defun rmail-summary-cease-edit ()
1297 "Finish editing message, then go back to Rmail summary buffer."
1298 (interactive)
1299 (rmail-cease-edit)
1300 (pop-to-buffer rmail-summary-buffer))
1302 (defun rmail-summary-abort-edit ()
1303 "Abort edit of current message; restore original contents.
1304 Go back to summary buffer."
1305 (interactive)
1306 (rmail-abort-edit)
1307 (pop-to-buffer rmail-summary-buffer))
1309 (defun rmail-summary-search-backward (regexp &optional n)
1310 "Show message containing next match for REGEXP.
1311 Prefix argument gives repeat count; negative argument means search
1312 backwards (through earlier messages).
1313 Interactively, empty argument means use same regexp used last time."
1314 (interactive
1315 (let* ((reversep (>= (prefix-numeric-value current-prefix-arg) 0))
1316 (prompt
1317 (concat (if reversep "Reverse " "") "Rmail search (regexp): "))
1318 regexp)
1319 (if rmail-search-last-regexp
1320 (setq prompt (concat prompt
1321 "(default "
1322 rmail-search-last-regexp
1323 ") ")))
1324 (setq regexp (read-string prompt))
1325 (cond ((not (equal regexp ""))
1326 (setq rmail-search-last-regexp regexp))
1327 ((not rmail-search-last-regexp)
1328 (error "No previous Rmail search string")))
1329 (list rmail-search-last-regexp
1330 (prefix-numeric-value current-prefix-arg))))
1331 ;; Don't use save-excursion because that prevents point from moving
1332 ;; properly in the summary buffer.
1333 (let ((buffer (current-buffer)))
1334 (unwind-protect
1335 (progn
1336 (set-buffer rmail-buffer)
1337 (rmail-search regexp (- n)))
1338 (set-buffer buffer))))
1340 (defun rmail-summary-search (regexp &optional n)
1341 "Show message containing next match for REGEXP.
1342 Prefix argument gives repeat count; negative argument means search
1343 backwards (through earlier messages).
1344 Interactively, empty argument means use same regexp used last time."
1345 (interactive
1346 (let* ((reversep (< (prefix-numeric-value current-prefix-arg) 0))
1347 (prompt
1348 (concat (if reversep "Reverse " "") "Rmail search (regexp): "))
1349 regexp)
1350 (if rmail-search-last-regexp
1351 (setq prompt (concat prompt
1352 "(default "
1353 rmail-search-last-regexp
1354 ") ")))
1355 (setq regexp (read-string prompt))
1356 (cond ((not (equal regexp ""))
1357 (setq rmail-search-last-regexp regexp))
1358 ((not rmail-search-last-regexp)
1359 (error "No previous Rmail search string")))
1360 (list rmail-search-last-regexp
1361 (prefix-numeric-value current-prefix-arg))))
1362 ;; Don't use save-excursion because that prevents point from moving
1363 ;; properly in the summary buffer.
1364 (let ((buffer (current-buffer)))
1365 (unwind-protect
1366 (progn
1367 (set-buffer rmail-buffer)
1368 (rmail-search regexp n))
1369 (set-buffer buffer))))
1371 (defun rmail-summary-toggle-header ()
1372 "Show original message header if pruned header currently shown, or vice versa."
1373 (interactive)
1374 (save-window-excursion
1375 (set-buffer rmail-buffer)
1376 (rmail-toggle-header))
1377 ;; Inside save-excursion, some changes to point in the RMAIL buffer are lost.
1378 ;; Set point to point-min in the RMAIL buffer, if it is visible.
1379 (let ((window (get-buffer-window rmail-view-buffer)))
1380 (if window
1381 ;; Using save-window-excursion would lose the new value of point.
1382 (let ((owin (selected-window)))
1383 (unwind-protect
1384 (progn
1385 (select-window window)
1386 (goto-char (point-min)))
1387 (select-window owin))))))
1390 (defun rmail-summary-add-label (label)
1391 "Add LABEL to labels associated with current Rmail message.
1392 Completion is performed over known labels when reading."
1393 (interactive (list (save-excursion
1394 (set-buffer rmail-buffer)
1395 (rmail-read-label "Add label"))))
1396 (save-excursion
1397 (set-buffer rmail-buffer)
1398 (rmail-add-label label)))
1400 (defun rmail-summary-kill-label (label)
1401 "Remove LABEL from labels associated with current Rmail message.
1402 Completion is performed over known labels when reading."
1403 (interactive (list (save-excursion
1404 (set-buffer rmail-buffer)
1405 (rmail-read-label "Kill label"))))
1406 (save-excursion
1407 (set-buffer rmail-buffer)
1408 (rmail-set-label label nil)))
1410 ;;;; *** Rmail Summary Mailing Commands ***
1412 (defun rmail-summary-override-mail-send-and-exit ()
1413 "Replace bindings to `mail-send-and-exit' with `rmail-summary-send-and-exit'."
1414 (use-local-map (copy-keymap (current-local-map)))
1415 (dolist (key (where-is-internal 'mail-send-and-exit))
1416 (define-key (current-local-map) key 'rmail-summary-send-and-exit)))
1418 (defun rmail-summary-mail ()
1419 "Send mail in another window.
1420 While composing the message, use \\[mail-yank-original] to yank the
1421 original message into it."
1422 (interactive)
1423 (let ((window (get-buffer-window rmail-buffer)))
1424 (if window
1425 (select-window window)
1426 (set-buffer rmail-buffer)))
1427 (rmail-start-mail nil nil nil nil nil (current-buffer))
1428 (rmail-summary-override-mail-send-and-exit))
1430 (defun rmail-summary-continue ()
1431 "Continue composing outgoing message previously being composed."
1432 (interactive)
1433 (let ((window (get-buffer-window rmail-buffer)))
1434 (if window
1435 (select-window window)
1436 (set-buffer rmail-buffer)))
1437 (rmail-start-mail t))
1439 (defun rmail-summary-reply (just-sender)
1440 "Reply to the current message.
1441 Normally include CC: to all other recipients of original message;
1442 prefix argument means ignore them. While composing the reply,
1443 use \\[mail-yank-original] to yank the original message into it."
1444 (interactive "P")
1445 (let ((window (get-buffer-window rmail-view-buffer)))
1446 (if window
1447 (select-window window)
1448 (set-buffer rmail-view-buffer)))
1449 (rmail-reply just-sender)
1450 (rmail-summary-override-mail-send-and-exit))
1452 (defun rmail-summary-retry-failure ()
1453 "Edit a mail message which is based on the contents of the current message.
1454 For a message rejected by the mail system, extract the interesting headers and
1455 the body of the original message; otherwise copy the current message."
1456 (interactive)
1457 (let ((window (get-buffer-window rmail-buffer)))
1458 (if window
1459 (select-window window)
1460 (set-buffer rmail-buffer)))
1461 (rmail-retry-failure)
1462 (rmail-summary-override-mail-send-and-exit))
1464 (defun rmail-summary-send-and-exit ()
1465 "Send mail reply and return to summary buffer."
1466 (interactive)
1467 (mail-send-and-exit t))
1469 (defun rmail-summary-forward (resend)
1470 "Forward the current message to another user.
1471 With prefix argument, \"resend\" the message instead of forwarding it;
1472 see the documentation of `rmail-resend'."
1473 (interactive "P")
1474 (save-excursion
1475 (let ((window (get-buffer-window rmail-buffer)))
1476 (if window
1477 (select-window window)
1478 (set-buffer rmail-buffer)))
1479 (rmail-forward resend)
1480 (rmail-summary-override-mail-send-and-exit)))
1482 (defun rmail-summary-resend ()
1483 "Resend current message using `rmail-resend'."
1484 (interactive)
1485 (save-excursion
1486 (let ((window (get-buffer-window rmail-buffer)))
1487 (if window
1488 (select-window window)
1489 (set-buffer rmail-buffer)))
1490 (call-interactively 'rmail-resend)))
1492 ;; Summary output commands.
1494 (defun rmail-summary-output-to-rmail-file (&optional file-name n)
1495 "Append the current message to an Rmail file named FILE-NAME.
1496 If the file does not exist, ask if it should be created.
1497 If file is being visited, the message is appended to the Emacs
1498 buffer visiting that file.
1500 A prefix argument N says to output N consecutive messages
1501 starting with the current one. Deleted messages are skipped and don't count."
1502 (interactive
1503 (progn (require 'rmailout)
1504 (list (rmail-output-read-rmail-file-name)
1505 (prefix-numeric-value current-prefix-arg))))
1506 (let ((i 0) prev-msg)
1507 (while
1508 (and (< i n)
1509 (progn (rmail-summary-goto-msg)
1510 (not (eq prev-msg
1511 (setq prev-msg
1512 (with-current-buffer rmail-buffer
1513 rmail-current-message))))))
1514 (setq i (1+ i))
1515 (with-current-buffer rmail-buffer
1516 (let ((rmail-delete-after-output nil))
1517 (rmail-output-to-rmail-file file-name 1)))
1518 (if rmail-delete-after-output
1519 (rmail-summary-delete-forward nil)
1520 (if (< i n)
1521 (rmail-summary-next-msg 1))))))
1523 (defun rmail-summary-output (&optional file-name n)
1524 "Append this message to Unix mail file named FILE-NAME.
1526 A prefix argument N says to output N consecutive messages
1527 starting with the current one. Deleted messages are skipped and don't count."
1528 (interactive
1529 (progn (require 'rmailout)
1530 (list (rmail-output-read-file-name)
1531 (prefix-numeric-value current-prefix-arg))))
1532 (let ((i 0) prev-msg)
1533 (while
1534 (and (< i n)
1535 (progn (rmail-summary-goto-msg)
1536 (not (eq prev-msg
1537 (setq prev-msg
1538 (with-current-buffer rmail-buffer
1539 rmail-current-message))))))
1540 (setq i (1+ i))
1541 (with-current-buffer rmail-buffer
1542 (let ((rmail-delete-after-output nil))
1543 (rmail-output file-name 1)))
1544 (if rmail-delete-after-output
1545 (rmail-summary-delete-forward nil)
1546 (if (< i n)
1547 (rmail-summary-next-msg 1))))))
1549 (defun rmail-summary-output-menu ()
1550 "Output current message to another Rmail file, chosen with a menu.
1551 Also set the default for subsequent \\[rmail-output-to-rmail-file] commands.
1552 The variables `rmail-secondary-file-directory' and
1553 `rmail-secondary-file-regexp' control which files are offered in the menu."
1554 (interactive)
1555 (save-excursion
1556 (set-buffer rmail-buffer)
1557 (let ((rmail-delete-after-output nil))
1558 (call-interactively 'rmail-output-menu)))
1559 (if rmail-delete-after-output
1560 (rmail-summary-delete-forward nil)))
1562 (defun rmail-summary-construct-io-menu ()
1563 (let ((files (rmail-find-all-files rmail-secondary-file-directory)))
1564 (if files
1565 (progn
1566 (define-key rmail-summary-mode-map [menu-bar classify input-menu]
1567 (cons "Input Rmail File"
1568 (rmail-list-to-menu "Input Rmail File"
1569 files
1570 'rmail-summary-input)))
1571 (define-key rmail-summary-mode-map [menu-bar classify output-menu]
1572 (cons "Output Rmail File"
1573 (rmail-list-to-menu "Output Rmail File"
1574 files
1575 'rmail-summary-output-to-rmail-file))))
1576 (define-key rmail-summary-mode-map [menu-bar classify input-menu]
1577 '("Input Rmail File" . rmail-disable-menu))
1578 (define-key rmail-summary-mode-map [menu-bar classify output-menu]
1579 '("Output Rmail File" . rmail-disable-menu)))))
1581 (defun rmail-summary-output-body (&optional file-name)
1582 "Write this message body to the file FILE-NAME.
1583 FILE-NAME defaults, interactively, from the Subject field of the message."
1584 (interactive)
1585 (save-excursion
1586 (set-buffer rmail-buffer)
1587 (let ((rmail-delete-after-output nil))
1588 (if file-name
1589 (rmail-output-body-to-file file-name)
1590 (call-interactively 'rmail-output-body-to-file))))
1591 (if rmail-delete-after-output
1592 (rmail-summary-delete-forward nil)))
1594 ;; Sorting messages in Rmail Summary buffer.
1596 (defun rmail-summary-sort-by-date (reverse)
1597 "Sort messages of current Rmail summary by date.
1598 If prefix argument REVERSE is non-nil, sort them in reverse order."
1599 (interactive "P")
1600 (rmail-sort-from-summary (function rmail-sort-by-date) reverse))
1602 (defun rmail-summary-sort-by-subject (reverse)
1603 "Sort messages of current Rmail summary by subject.
1604 If prefix argument REVERSE is non-nil, sort them in reverse order."
1605 (interactive "P")
1606 (rmail-sort-from-summary (function rmail-sort-by-subject) reverse))
1608 (defun rmail-summary-sort-by-author (reverse)
1609 "Sort messages of current Rmail summary by author.
1610 If prefix argument REVERSE is non-nil, sort them in reverse order."
1611 (interactive "P")
1612 (rmail-sort-from-summary (function rmail-sort-by-author) reverse))
1614 (defun rmail-summary-sort-by-recipient (reverse)
1615 "Sort messages of current Rmail summary by recipient.
1616 If prefix argument REVERSE is non-nil, sort them in reverse order."
1617 (interactive "P")
1618 (rmail-sort-from-summary (function rmail-sort-by-recipient) reverse))
1620 (defun rmail-summary-sort-by-correspondent (reverse)
1621 "Sort messages of current Rmail summary by other correspondent.
1622 If prefix argument REVERSE is non-nil, sort them in reverse order."
1623 (interactive "P")
1624 (rmail-sort-from-summary (function rmail-sort-by-correspondent) reverse))
1626 (defun rmail-summary-sort-by-lines (reverse)
1627 "Sort messages of current Rmail summary by lines of the message.
1628 If prefix argument REVERSE is non-nil, sort them in reverse order."
1629 (interactive "P")
1630 (rmail-sort-from-summary (function rmail-sort-by-lines) reverse))
1632 (defun rmail-summary-sort-by-labels (reverse labels)
1633 "Sort messages of current Rmail summary by labels.
1634 If prefix argument REVERSE is non-nil, sort them in reverse order.
1635 KEYWORDS is a comma-separated list of labels."
1636 (interactive "P\nsSort by labels: ")
1637 (rmail-sort-from-summary
1638 (function (lambda (reverse)
1639 (rmail-sort-by-labels reverse labels)))
1640 reverse))
1642 (defun rmail-sort-from-summary (sortfun reverse)
1643 "Sort Rmail messages from Summary buffer and update it after sorting."
1644 (require 'rmailsort)
1645 (let ((selwin (selected-window)))
1646 (unwind-protect
1647 (progn (pop-to-buffer rmail-buffer)
1648 (funcall sortfun reverse))
1649 (select-window selwin))))
1651 (provide 'rmailsum)
1653 ;;; rmailsum.el ends here