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