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