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