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