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.
84LABELS 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.
98Normally checks the To, From and Cc fields of headers;
99but if PRIMARY-ONLY is non-nil (prefix arg given),
100 only look in the To and From fields.
101RECIPIENTS 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.
112If 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),
114Emacs 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.
131Normally checks the Subject field of headers;
132but if WHOLE-MESSAGE is non-nil (prefix arg given),
133 look in the whole message.
134SUBJECT 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.
167SENDERS 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
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.
190DESCRIPTION makes part of the mode line of the summary buffer.
191For each message, FUNCTION is applied to the message number and ARGS...
192and if the result is non-nil, that message is included.
193nil 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
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
303By 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.
374If non-nil, this variable is used to identify the correspondent
375when receiving new mail.  If it matches the address of the sender,
376the recipient is taken as correspondent of a mail.
377If nil \(default value\), your `user-login-name' and `user-mail-address'
378are used to exclude yourself as correspondent.
379
380Usually you don't have to set this variable, except if you collect mails
381sent by you under different user names.
382Then it should be a regexp matching your mail addresses.
383
384Setting 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
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.
529With optional prefix argument NUMBER, moves forward this number of non-deleted
530messages, 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.
546With optional prefix argument NUMBER, moves backward this number of
547non-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.
553With 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.
564With 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.
575With prefix argument N, do this N times.
576If 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.
620With prefix argument N, do this N times.
621If N is negative, go forwards instead."
622  (interactive "p")
623  (rmail-summary-next-same-subject (- n)))
624
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.
629Deleted messages stay in the file until the \\[rmail-expunge] command is given.
630A prefix argument serves as a repeat count;
631a 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.
654Deleted messages stay in the file until the \\[rmail-expunge] command is given.
655A prefix argument serves as a repeat count;
656a 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.
687Optional 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
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].
737As commands are issued in the summary buffer, they are applied to the
738corresponding mail messages in the rmail buffer.
739
740All normal editing commands are turned off.
741Instead, nearly all the Rmail mode commands are available,
742though many of them move only among the messages in the summary.
743
744These 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
749Commands 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'.
791If it moves to a message within an Incremental Search, and removes
792the `unseen' attribute from that message, it sets this flag
793so that if the next motion between messages is in the same Incremental
794Search, 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(if rmail-summary-mode-map
859    nil
860  (setq rmail-summary-mode-map (make-keymap))
861  (suppress-keymap rmail-summary-mode-map)
862
863  (define-key rmail-summary-mode-map [mouse-2] 'rmail-summary-mouse-goto-message)
864  (define-key rmail-summary-mode-map "a"      'rmail-summary-add-label)
865  (define-key rmail-summary-mode-map "b"      'rmail-summary-bury)
866  (define-key rmail-summary-mode-map "c"      'rmail-summary-continue)
867  (define-key rmail-summary-mode-map "d"      'rmail-summary-delete-forward)
868  (define-key rmail-summary-mode-map "\C-d"   'rmail-summary-delete-backward)
869  (define-key rmail-summary-mode-map "e"      'rmail-summary-edit-current-message)
870  (define-key rmail-summary-mode-map "f"      'rmail-summary-forward)
871  (define-key rmail-summary-mode-map "g"      'rmail-summary-get-new-mail)
872  (define-key rmail-summary-mode-map "h"      'rmail-summary)
873  (define-key rmail-summary-mode-map "i"      'rmail-summary-input)
874  (define-key rmail-summary-mode-map "j"      'rmail-summary-goto-msg)
875  (define-key rmail-summary-mode-map "\C-m"   'rmail-summary-goto-msg)
876  (define-key rmail-summary-mode-map "k"      'rmail-summary-kill-label)
877  (define-key rmail-summary-mode-map "l"      'rmail-summary-by-labels)
878  (define-key rmail-summary-mode-map "\e\C-h" 'rmail-summary)
879  (define-key rmail-summary-mode-map "\e\C-l" 'rmail-summary-by-labels)
880  (define-key rmail-summary-mode-map "\e\C-r" 'rmail-summary-by-recipients)
881  (define-key rmail-summary-mode-map "\e\C-s" 'rmail-summary-by-regexp)
882  (define-key rmail-summary-mode-map "\e\C-t" 'rmail-summary-by-topic)
883  (define-key rmail-summary-mode-map "m"      'rmail-summary-mail)
884  (define-key rmail-summary-mode-map "\M-m"   'rmail-summary-retry-failure)
885  (define-key rmail-summary-mode-map "n"      'rmail-summary-next-msg)
886  (define-key rmail-summary-mode-map "\en"    'rmail-summary-next-all)
887  (define-key rmail-summary-mode-map "\e\C-n" 'rmail-summary-next-labeled-message)
888  (define-key rmail-summary-mode-map "o"      'rmail-summary-output-to-rmail-file)
889  (define-key rmail-summary-mode-map "\C-o"   'rmail-summary-output)
890  (define-key rmail-summary-mode-map "p"      'rmail-summary-previous-msg)
891  (define-key rmail-summary-mode-map "\ep"    'rmail-summary-previous-all)
892  (define-key rmail-summary-mode-map "\e\C-p" 'rmail-summary-previous-labeled-message)
893  (define-key rmail-summary-mode-map "q"      'rmail-summary-quit)
894  (define-key rmail-summary-mode-map "Q"      'rmail-summary-wipe)
895  (define-key rmail-summary-mode-map "r"      'rmail-summary-reply)
896  (define-key rmail-summary-mode-map "s"      'rmail-summary-expunge-and-save)
897  (define-key rmail-summary-mode-map "\es"    'rmail-summary-search)
898  (define-key rmail-summary-mode-map "t"      'rmail-summary-toggle-header)
899  (define-key rmail-summary-mode-map "u"      'rmail-summary-undelete)
900  (define-key rmail-summary-mode-map "\M-u"   'rmail-summary-undelete-many)
901  (define-key rmail-summary-mode-map "x"      'rmail-summary-expunge)
902  (define-key rmail-summary-mode-map "w"      'rmail-summary-output-body)
903  (define-key rmail-summary-mode-map "."      'rmail-summary-beginning-of-message)
904  (define-key rmail-summary-mode-map "/"      'rmail-summary-end-of-message)
905  (define-key rmail-summary-mode-map "<"      'rmail-summary-first-message)
906  (define-key rmail-summary-mode-map ">"      'rmail-summary-last-message)
907  (define-key rmail-summary-mode-map " "      'rmail-summary-scroll-msg-up)
908  (define-key rmail-summary-mode-map "\177"   'rmail-summary-scroll-msg-down)
909  (define-key rmail-summary-mode-map "?"      'describe-mode)
910  (define-key rmail-summary-mode-map "\C-c\C-n" 'rmail-summary-next-same-subject)
911  (define-key rmail-summary-mode-map "\C-c\C-p" 'rmail-summary-previous-same-subject)
912  (define-key rmail-summary-mode-map "\C-c\C-s\C-d"
913    'rmail-summary-sort-by-date)
914  (define-key rmail-summary-mode-map "\C-c\C-s\C-s"
915    'rmail-summary-sort-by-subject)
916  (define-key rmail-summary-mode-map "\C-c\C-s\C-a"
917    'rmail-summary-sort-by-author)
918  (define-key rmail-summary-mode-map "\C-c\C-s\C-r"
919    'rmail-summary-sort-by-recipient)
920  (define-key rmail-summary-mode-map "\C-c\C-s\C-c"
921    'rmail-summary-sort-by-correspondent)
922  (define-key rmail-summary-mode-map "\C-c\C-s\C-l"
923    'rmail-summary-sort-by-lines)
924  (define-key rmail-summary-mode-map "\C-c\C-s\C-k"
925    'rmail-summary-sort-by-labels)
926  )
927
928;;; Menu bar bindings.
929
930(define-key rmail-summary-mode-map [menu-bar] (make-sparse-keymap))
931
932(define-key rmail-summary-mode-map [menu-bar classify]
933  (cons "Classify" (make-sparse-keymap "Classify")))
934
935(define-key rmail-summary-mode-map [menu-bar classify output-menu]
936  '("Output (Rmail Menu)..." . rmail-summary-output-menu))
937
938(define-key rmail-summary-mode-map [menu-bar classify input-menu]
939  '("Input Rmail File (menu)..." . rmail-input-menu))
940
941(define-key rmail-summary-mode-map [menu-bar classify input-menu]
942  '(nil))
943
944(define-key rmail-summary-mode-map [menu-bar classify output-menu]
945  '(nil))
946
947(define-key rmail-summary-mode-map [menu-bar classify output-body]
948  '("Output (body)..." . rmail-summary-output-body))
949
950(define-key rmail-summary-mode-map [menu-bar classify output-inbox]
951  '("Output (inbox)..." . rmail-summary-output))
952
953(define-key rmail-summary-mode-map [menu-bar classify output]
954  '("Output (Rmail)..." . rmail-summary-output-to-rmail-file))
955
956(define-key rmail-summary-mode-map [menu-bar classify kill-label]
957  '("Kill Label..." . rmail-summary-kill-label))
958
959(define-key rmail-summary-mode-map [menu-bar classify add-label]
960  '("Add Label..." . rmail-summary-add-label))
961
962(define-key rmail-summary-mode-map [menu-bar summary]
963  (cons "Summary" (make-sparse-keymap "Summary")))
964
965(define-key rmail-summary-mode-map [menu-bar summary senders]
966  '("By Senders..." . rmail-summary-by-senders))
967
968(define-key rmail-summary-mode-map [menu-bar summary labels]
969  '("By Labels..." . rmail-summary-by-labels))
970
971(define-key rmail-summary-mode-map [menu-bar summary recipients]
972  '("By Recipients..." . rmail-summary-by-recipients))
973
974(define-key rmail-summary-mode-map [menu-bar summary topic]
975  '("By Topic..." . rmail-summary-by-topic))
976
977(define-key rmail-summary-mode-map [menu-bar summary regexp]
978  '("By Regexp..." . rmail-summary-by-regexp))
979
980(define-key rmail-summary-mode-map [menu-bar summary all]
981  '("All" . rmail-summary))
982
983(define-key rmail-summary-mode-map [menu-bar mail]
984  (cons "Mail" (make-sparse-keymap "Mail")))
985
986(define-key rmail-summary-mode-map [menu-bar mail rmail-summary-get-new-mail]
987  '("Get New Mail" . rmail-summary-get-new-mail))
988
989(define-key rmail-summary-mode-map [menu-bar mail lambda]
990  '("----"))
991
992(define-key rmail-summary-mode-map [menu-bar mail continue]
993  '("Continue" . rmail-summary-continue))
994
995(define-key rmail-summary-mode-map [menu-bar mail resend]
996  '("Re-send..." . rmail-summary-resend))
997
998(define-key rmail-summary-mode-map [menu-bar mail forward]
999  '("Forward" . rmail-summary-forward))
1000
1001(define-key rmail-summary-mode-map [menu-bar mail retry]
1002  '("Retry" . rmail-summary-retry-failure))
1003
1004(define-key rmail-summary-mode-map [menu-bar mail reply]
1005  '("Reply" . rmail-summary-reply))
1006
1007(define-key rmail-summary-mode-map [menu-bar mail mail]
1008  '("Mail" . rmail-summary-mail))
1009
1010(define-key rmail-summary-mode-map [menu-bar delete]
1011  (cons "Delete" (make-sparse-keymap "Delete")))
1012
1013(define-key rmail-summary-mode-map [menu-bar delete expunge/save]
1014  '("Expunge/Save" . rmail-summary-expunge-and-save))
1015
1016(define-key rmail-summary-mode-map [menu-bar delete expunge]
1017  '("Expunge" . rmail-summary-expunge))
1018
1019(define-key rmail-summary-mode-map [menu-bar delete undelete]
1020  '("Undelete" . rmail-summary-undelete))
1021
1022(define-key rmail-summary-mode-map [menu-bar delete delete]
1023  '("Delete" . rmail-summary-delete-forward))
1024
1025(define-key rmail-summary-mode-map [menu-bar move]
1026  (cons "Move" (make-sparse-keymap "Move")))
1027
1028(define-key rmail-summary-mode-map [menu-bar move search-back]
1029  '("Search Back..." . rmail-summary-search-backward))
1030
1031(define-key rmail-summary-mode-map [menu-bar move search]
1032  '("Search..." . rmail-summary-search))
1033
1034(define-key rmail-summary-mode-map [menu-bar move previous]
1035  '("Previous Nondeleted" . rmail-summary-previous-msg))
1036
1037(define-key rmail-summary-mode-map [menu-bar move next]
1038  '("Next Nondeleted" . rmail-summary-next-msg))
1039
1040(define-key rmail-summary-mode-map [menu-bar move last]
1041  '("Last" . rmail-summary-last-message))
1042
1043(define-key rmail-summary-mode-map [menu-bar move first]
1044  '("First" . rmail-summary-first-message))
1045
1046(define-key rmail-summary-mode-map [menu-bar move previous]
1047  '("Previous" . rmail-summary-previous-all))
1048
1049(define-key rmail-summary-mode-map [menu-bar move next]
1050  '("Next" . rmail-summary-next-all))
1051
1052(defun rmail-summary-mouse-goto-message (event)
1053  "Select the message whose summary line you click on."
1054  (interactive "@e")
1055  (goto-char (posn-point (event-end event)))
1056  (rmail-summary-goto-msg))
1057
1058(defun rmail-summary-goto-msg (&optional n nowarn skip-rmail)
1059  "Go to message N in the summary buffer and the Rmail buffer.
1060If N is nil, use the message corresponding to point in the summary
1061and move to that message in the Rmail buffer.
1062
1063If NOWARN, don't say anything if N is out of range.
1064If SKIP-RMAIL, don't do anything to the Rmail buffer."
1065  (interactive "P")
1066  (if (consp n) (setq n (prefix-numeric-value n)))
1067  (if (eobp) (forward-line -1))
1068  (beginning-of-line)
1069  (let* ((obuf (current-buffer))
1070	 (buf rmail-buffer)
1071	 (cur (point))
1072	 message-not-found
1073	 (curmsg (string-to-number
1074		  (buffer-substring (point)
1075				    (min (point-max) (+ 6 (point))))))
1076	 (total (save-excursion (set-buffer buf) rmail-total-messages)))
1077    ;; If message number N was specified, find that message's line
1078    ;; or set message-not-found.
1079    ;; If N wasn't specified or that message can't be found.
1080    ;; set N by default.
1081    (if (not n)
1082	(setq n curmsg)
1083      (if (< n 1)
1084	  (progn (message "No preceding message")
1085		 (setq n 1)))
1086      (if (and (> n total)
1087	       (> total 0))
1088	  (progn (message "No following message")
1089		 (goto-char (point-max))
1090		 (rmail-summary-goto-msg nil nowarn skip-rmail)))
1091      (goto-char (point-min))
1092      (if (not (re-search-forward (format "^%5d[^0-9]" n) nil t))
1093	  (progn (or nowarn (message "Message %d not found" n))
1094		 (setq n curmsg)
1095		 (setq message-not-found t)
1096		 (goto-char cur))))
1097    (beginning-of-line)
1098    (skip-chars-forward " ")
1099    (skip-chars-forward "0-9")
1100    (save-excursion (if (= (following-char) ?-)
1101			(let ((buffer-read-only nil))
1102			  (delete-char 1)
1103			  (insert " "))))
1104    (rmail-summary-update-highlight message-not-found)
1105    (beginning-of-line)
1106    (if skip-rmail
1107	nil
1108      (let ((selwin (selected-window)))
1109	(unwind-protect
1110	    (progn (pop-to-buffer buf)
1111		   (rmail-show-message n))
1112	  (select-window selwin)
1113	  ;; The actions above can alter the current buffer.  Preserve it.
1114	  (set-buffer obuf))))))
1115
1116;; Update the highlighted line in an rmail summary buffer.
1117;; That should be current.  We highlight the line point is on.
1118;; If NOT-FOUND is non-nil, we turn off highlighting.
1119(defun rmail-summary-update-highlight (not-found)
1120  ;; Make sure we have an overlay to use.
1121  (or rmail-summary-overlay
1122      (progn
1123	(make-local-variable 'rmail-summary-overlay)
1124	(setq rmail-summary-overlay (make-overlay (point) (point)))))
1125  ;; If this message is in the summary, use the overlay to highlight it.
1126  ;; Otherwise, don't highlight anything.
1127  (if not-found
1128      (overlay-put rmail-summary-overlay 'face nil)
1129    (move-overlay rmail-summary-overlay
1130		  (save-excursion (beginning-of-line)
1131				  (skip-chars-forward " ")
1132				  (point))
1133		  (save-excursion (end-of-line) (point)))
1134    (overlay-put rmail-summary-overlay 'face 'highlight)))
1135
1136(defun rmail-summary-scroll-msg-up (&optional dist)
1137  "Scroll the Rmail window forward.
1138If the Rmail window is displaying the end of a message,
1139advance to the next message."
1140  (interactive "P")
1141  (if (eq dist '-)
1142      (rmail-summary-scroll-msg-down nil)
1143    (let ((rmail-buffer-window (get-buffer-window rmail-view-buffer)))
1144      (if rmail-buffer-window
1145	  (if (let ((rmail-summary-window (selected-window)))
1146		(select-window rmail-buffer-window)
1147		(prog1
1148		    ;; Is EOB visible in the buffer?
1149		    (save-excursion
1150		      (let ((ht (window-height (selected-window))))
1151			(move-to-window-line (- ht 2))
1152			(end-of-line)
1153			(eobp)))
1154		  (select-window rmail-summary-window)))
1155	      (if (not rmail-summary-scroll-between-messages)
1156		  (error "End of buffer")
1157		(rmail-summary-next-msg (or dist 1)))
1158	    (let ((other-window-scroll-buffer rmail-view-buffer))
1159	      (scroll-other-window dist)))
1160	;; If it isn't visible at all, show the beginning.
1161	(rmail-summary-beginning-of-message)))))
1162
1163(defun rmail-summary-scroll-msg-down (&optional dist)
1164  "Scroll the Rmail window backward.
1165If the Rmail window is now displaying the beginning of a message,
1166move to the previous message."
1167  (interactive "P")
1168  (if (eq dist '-)
1169      (rmail-summary-scroll-msg-up nil)
1170    (let ((rmail-buffer-window (get-buffer-window rmail-view-buffer)))
1171      (if rmail-buffer-window
1172	  (if (let ((rmail-summary-window (selected-window)))
1173		(select-window rmail-buffer-window)
1174		(prog1
1175		    ;; Is BOB visible in the buffer?
1176		    (save-excursion
1177		      (move-to-window-line 0)
1178		      (beginning-of-line)
1179		      (bobp))
1180		  (select-window rmail-summary-window)))
1181	      (if (not rmail-summary-scroll-between-messages)
1182		  (error "Beginning of buffer")
1183		(rmail-summary-previous-msg (or dist 1)))
1184	    (let ((other-window-scroll-buffer rmail-view-buffer))
1185	      (scroll-other-window-down dist)))
1186	;; If it isn't visible at all, show the beginning.
1187	(rmail-summary-beginning-of-message)))))
1188
1189(defun rmail-summary-beginning-of-message ()
1190  "Show current message from the beginning."
1191  (interactive)
1192  (rmail-summary-show-message 'BEG))
1193
1194(defun rmail-summary-end-of-message ()
1195  "Show bottom of current message."
1196  (interactive)
1197  (rmail-summary-show-message 'END))
1198
1199(defun rmail-summary-show-message (where)
1200  "Show current mail message.
1201Position it according to WHERE which can be BEG or END"
1202  (if (and (one-window-p) (not pop-up-frames))
1203      ;; If there is just one window, put the summary on the top.
1204      (let ((buffer rmail-view-buffer))
1205	(split-window (selected-window) rmail-summary-window-size)
1206	(select-window (frame-first-window))
1207	(pop-to-buffer rmail-view-buffer)
1208	;; If pop-to-buffer did not use that window, delete that
1209	;; window.  (This can happen if it uses another frame.)
1210	(or (eq buffer (window-buffer (next-window (frame-first-window))))
1211	    (delete-other-windows)))
1212    (pop-to-buffer rmail-view-buffer))
1213  (cond
1214   ((eq where 'BEG)
1215	(goto-char (point-min))
1216	(search-forward "\n\n"))
1217   ((eq where 'END)
1218	(goto-char (point-max))
1219	(recenter (1- (window-height))))
1220   )
1221  (pop-to-buffer rmail-summary-buffer))
1222
1223(defun rmail-summary-bury ()
1224  "Bury the Rmail buffer and the Rmail summary buffer."
1225  (interactive)
1226  (let ((buffer-to-bury (current-buffer)))
1227    (let (window)
1228      (while (setq window (get-buffer-window rmail-buffer))
1229	(set-window-buffer window (other-buffer rmail-buffer)))
1230      (bury-buffer rmail-buffer))
1231    (switch-to-buffer (other-buffer buffer-to-bury))
1232    (bury-buffer buffer-to-bury)))
1233
1234(defun rmail-summary-quit ()
1235  "Quit out of Rmail and Rmail summary."
1236  (interactive)
1237  (rmail-summary-wipe)
1238  (rmail-quit))
1239
1240(defun rmail-summary-wipe ()
1241  "Kill and wipe away Rmail summary, remaining within Rmail."
1242  (interactive)
1243  (save-excursion (set-buffer rmail-buffer) (setq rmail-summary-buffer nil))
1244  (let ((local-rmail-buffer rmail-view-buffer))
1245    (kill-buffer (current-buffer))
1246    ;; Delete window if not only one.
1247    (if (not (eq (selected-window) (next-window nil 'no-minibuf)))
1248	(delete-window))
1249    ;; Switch windows to the rmail buffer, or switch to it in this window.
1250    (pop-to-buffer local-rmail-buffer)))
1251
1252(defun rmail-summary-expunge ()
1253  "Actually erase all deleted messages and recompute summary headers."
1254  (interactive)
1255  (save-excursion
1256    (set-buffer rmail-buffer)
1257    (when (rmail-expunge-confirmed)
1258      (rmail-only-expunge)))
1259  (rmail-update-summary))
1260
1261(defun rmail-summary-expunge-and-save ()
1262  "Expunge and save RMAIL file."
1263  (interactive)
1264  (save-excursion
1265    (set-buffer rmail-buffer)
1266    (when (rmail-expunge-confirmed)
1267      (rmail-only-expunge)))
1268  (rmail-update-summary)
1269  (save-excursion
1270    (set-buffer rmail-buffer)
1271    (save-buffer))
1272  (set-buffer-modified-p nil))
1273
1274(defun rmail-summary-get-new-mail (&optional file-name)
1275  "Get new mail and recompute summary headers.
1276
1277Optionally you can specify the file to get new mail from.  In this case,
1278the file of new mail is not changed or deleted.  Noninteractively, you can
1279pass the inbox file name as an argument.  Interactively, a prefix
1280argument says to read a file name and use that file as the inbox."
1281  (interactive
1282   (list (if current-prefix-arg
1283	     (read-file-name "Get new mail from file: "))))
1284  (let (msg)
1285    (save-excursion
1286      (set-buffer rmail-buffer)
1287      (rmail-get-new-mail file-name)
1288      ;; Get the proper new message number.
1289      (setq msg rmail-current-message))
1290    ;; Make sure that message is displayed.
1291    (or (zerop msg)
1292	(rmail-summary-goto-msg msg))))
1293
1294(defun rmail-summary-input (filename)
1295  "Run Rmail on file FILENAME."
1296  (interactive "FRun rmail on RMAIL file: ")
1297  ;; We switch windows here, then display the other Rmail file there.
1298  (pop-to-buffer rmail-buffer)
1299  (rmail filename))
1300
1301(defun rmail-summary-first-message ()
1302  "Show first message in Rmail file from summary buffer."
1303  (interactive)
1304  (with-no-warnings
1305    (beginning-of-buffer)))
1306
1307(defun rmail-summary-last-message ()
1308  "Show last message in Rmail file from summary buffer."
1309  (interactive)
1310  (with-no-warnings
1311    (end-of-buffer))
1312  (forward-line -1))
1313
1314(defvar rmail-summary-edit-map nil)
1315(if rmail-summary-edit-map
1316    nil
1317  (setq rmail-summary-edit-map
1318	(nconc (make-sparse-keymap) text-mode-map))
1319  (define-key rmail-summary-edit-map "\C-c\C-c" 'rmail-cease-edit)
1320  (define-key rmail-summary-edit-map "\C-c\C-]" 'rmail-abort-edit))
1321
1322(defun rmail-summary-edit-current-message ()
1323  "Edit the contents of this message."
1324  (interactive)
1325  (pop-to-buffer rmail-buffer)
1326  (rmail-edit-current-message)
1327  (use-local-map rmail-summary-edit-map))
1328
1329(defun rmail-summary-cease-edit ()
1330  "Finish editing message, then go back to Rmail summary buffer."
1331  (interactive)
1332  (rmail-cease-edit)
1333  (pop-to-buffer rmail-summary-buffer))
1334
1335(defun rmail-summary-abort-edit ()
1336  "Abort edit of current message; restore original contents.
1337Go back to summary buffer."
1338  (interactive)
1339  (rmail-abort-edit)
1340  (pop-to-buffer rmail-summary-buffer))
1341
1342(defun rmail-summary-search-backward (regexp &optional n)
1343  "Show message containing next match for REGEXP.
1344Prefix argument gives repeat count; negative argument means search
1345backwards (through earlier messages).
1346Interactively, empty argument means use same regexp used last time."
1347  (interactive
1348    (let* ((reversep (>= (prefix-numeric-value current-prefix-arg) 0))
1349	   (prompt
1350	    (concat (if reversep "Reverse " "") "Rmail search (regexp"))
1351	   regexp)
1352      (setq prompt
1353	    (concat prompt
1354		    (if rmail-search-last-regexp
1355			(concat ", default "
1356				rmail-search-last-regexp "): ")
1357		      "): ")))
1358      (setq regexp (read-string prompt))
1359      (cond ((not (equal regexp ""))
1360	     (setq rmail-search-last-regexp regexp))
1361	    ((not rmail-search-last-regexp)
1362	     (error "No previous Rmail search string")))
1363      (list rmail-search-last-regexp
1364	    (prefix-numeric-value current-prefix-arg))))
1365  ;; Don't use save-excursion because that prevents point from moving
1366  ;; properly in the summary buffer.
1367  (let ((buffer (current-buffer)))
1368    (unwind-protect
1369	(progn
1370	  (set-buffer rmail-buffer)
1371	  (rmail-search regexp (- n)))
1372      (set-buffer buffer))))
1373
1374(defun rmail-summary-search (regexp &optional n)
1375  "Show message containing next match for REGEXP.
1376Prefix argument gives repeat count; negative argument means search
1377backwards (through earlier messages).
1378Interactively, empty argument means use same regexp used last time."
1379  (interactive
1380    (let* ((reversep (< (prefix-numeric-value current-prefix-arg) 0))
1381	   (prompt
1382	    (concat (if reversep "Reverse " "") "Rmail search (regexp"))
1383	   regexp)
1384      (setq prompt
1385	    (concat prompt
1386		    (if rmail-search-last-regexp
1387			(concat ", default "
1388				rmail-search-last-regexp "): ")
1389		      "): ")))
1390      (setq regexp (read-string prompt))
1391      (cond ((not (equal regexp ""))
1392	     (setq rmail-search-last-regexp regexp))
1393	    ((not rmail-search-last-regexp)
1394	     (error "No previous Rmail search string")))
1395      (list rmail-search-last-regexp
1396	    (prefix-numeric-value current-prefix-arg))))
1397  ;; Don't use save-excursion because that prevents point from moving
1398  ;; properly in the summary buffer.
1399  (let ((buffer (current-buffer)))
1400    (unwind-protect
1401	(progn
1402	  (set-buffer rmail-buffer)
1403	  (rmail-search regexp n))
1404      (set-buffer buffer))))
1405
1406(defun rmail-summary-toggle-header ()
1407  "Show original message header if pruned header currently shown, or vice versa."
1408  (interactive)
1409  (save-window-excursion
1410    (set-buffer rmail-buffer)
1411    (rmail-toggle-header))
1412  ;; Inside save-excursion, some changes to point in the RMAIL buffer are lost.
1413  ;; Set point to point-min in the RMAIL buffer, if it is visible.
1414  (let ((window (get-buffer-window rmail-view-buffer)))
1415    (if window
1416        ;; Using save-window-excursion would lose the new value of point.
1417        (let ((owin (selected-window)))
1418          (unwind-protect
1419              (progn
1420                (select-window window)
1421                (goto-char (point-min)))
1422            (select-window owin))))))
1423
1424
1425(defun rmail-summary-add-label (label)
1426  "Add LABEL to labels associated with current Rmail message.
1427Completion is performed over known labels when reading."
1428  (interactive (list (save-excursion
1429		       (set-buffer rmail-buffer)
1430		       (rmail-read-label "Add label"))))
1431  (save-excursion
1432    (set-buffer rmail-buffer)
1433    (rmail-add-label label)))
1434
1435(defun rmail-summary-kill-label (label)
1436  "Remove LABEL from labels associated with current Rmail message.
1437Completion is performed over known labels when reading."
1438  (interactive (list (save-excursion
1439		       (set-buffer rmail-buffer)
1440		       (rmail-read-label "Kill label"))))
1441  (save-excursion
1442    (set-buffer rmail-buffer)
1443    (rmail-set-label label nil)))
1444
1445;;;; *** Rmail Summary Mailing Commands ***
1446
1447(defun rmail-summary-override-mail-send-and-exit ()
1448  "Replace bindings to `mail-send-and-exit' with `rmail-summary-send-and-exit'."
1449  (use-local-map (copy-keymap (current-local-map)))
1450  (dolist (key (where-is-internal 'mail-send-and-exit))
1451    (define-key (current-local-map) key 'rmail-summary-send-and-exit)))
1452
1453(defun rmail-summary-mail ()
1454  "Send mail in another window.
1455While composing the message, use \\[mail-yank-original] to yank the
1456original message into it."
1457  (interactive)
1458  (let ((window (get-buffer-window rmail-buffer)))
1459    (if window
1460	(select-window window)
1461      (set-buffer rmail-buffer)))
1462  (rmail-start-mail nil nil nil nil nil (current-buffer))
1463  (rmail-summary-override-mail-send-and-exit))
1464
1465(defun rmail-summary-continue ()
1466  "Continue composing outgoing message previously being composed."
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 t))
1473
1474(defun rmail-summary-reply (just-sender)
1475  "Reply to the current message.
1476Normally include CC: to all other recipients of original message;
1477prefix argument means ignore them.  While composing the reply,
1478use \\[mail-yank-original] to yank the original message into it."
1479  (interactive "P")
1480  (let ((window (get-buffer-window rmail-view-buffer)))
1481    (if window
1482	(select-window window)
1483      (set-buffer rmail-view-buffer)))
1484  (rmail-reply just-sender)
1485  (rmail-summary-override-mail-send-and-exit))
1486
1487(defun rmail-summary-retry-failure ()
1488  "Edit a mail message which is based on the contents of the current message.
1489For a message rejected by the mail system, extract the interesting headers and
1490the body of the original message; otherwise copy the current message."
1491  (interactive)
1492  (let ((window (get-buffer-window rmail-buffer)))
1493    (if window
1494	(select-window window)
1495      (set-buffer rmail-buffer)))
1496  (rmail-retry-failure)
1497  (rmail-summary-override-mail-send-and-exit))
1498
1499(defun rmail-summary-send-and-exit ()
1500  "Send mail reply and return to summary buffer."
1501  (interactive)
1502  (mail-send-and-exit t))
1503
1504(defun rmail-summary-forward (resend)
1505  "Forward the current message to another user.
1506With prefix argument, \"resend\" the message instead of forwarding it;
1507see the documentation of `rmail-resend'."
1508  (interactive "P")
1509  (save-excursion
1510    (let ((window (get-buffer-window rmail-buffer)))
1511      (if window
1512	  (select-window window)
1513	(set-buffer rmail-buffer)))
1514    (rmail-forward resend)
1515    (rmail-summary-override-mail-send-and-exit)))
1516
1517(defun rmail-summary-resend ()
1518  "Resend current message using `rmail-resend'."
1519  (interactive)
1520  (save-excursion
1521    (let ((window (get-buffer-window rmail-buffer)))
1522      (if window
1523	  (select-window window)
1524	(set-buffer rmail-buffer)))
1525    (call-interactively 'rmail-resend)))
1526
1527;; Summary output commands.
1528
1529(defun rmail-summary-output-to-rmail-file (&optional file-name n)
1530  "Append the current message to an Rmail file named FILE-NAME.
1531If the file does not exist, ask if it should be created.
1532If file is being visited, the message is appended to the Emacs
1533buffer visiting that file.
1534
1535A prefix argument N says to output N consecutive messages
1536starting with the current one.  Deleted messages are skipped and don't count."
1537  (interactive
1538   (progn (require 'rmailout)
1539	  (list (rmail-output-read-rmail-file-name)
1540		(prefix-numeric-value current-prefix-arg))))
1541  (let ((i 0) prev-msg)
1542    (while
1543	(and (< i n)
1544	     (progn (rmail-summary-goto-msg)
1545		    (not (eq prev-msg
1546			     (setq prev-msg
1547				   (with-current-buffer rmail-buffer
1548				     rmail-current-message))))))
1549      (setq i (1+ i))
1550      (with-current-buffer rmail-buffer
1551	(let ((rmail-delete-after-output nil))
1552	  (rmail-output-to-rmail-file file-name 1)))
1553      (if rmail-delete-after-output
1554	  (rmail-summary-delete-forward nil)
1555	(if (< i n)
1556	    (rmail-summary-next-msg 1))))))
1557
1558(defun rmail-summary-output (&optional file-name n)
1559  "Append this message to Unix mail file named FILE-NAME.
1560
1561A prefix argument N says to output N consecutive messages
1562starting with the current one.  Deleted messages are skipped and don't count."
1563  (interactive
1564   (progn (require 'rmailout)
1565	  (list (rmail-output-read-file-name)
1566		(prefix-numeric-value current-prefix-arg))))
1567  (let ((i 0) prev-msg)
1568    (while
1569	(and (< i n)
1570	     (progn (rmail-summary-goto-msg)
1571		    (not (eq prev-msg
1572			     (setq prev-msg
1573				   (with-current-buffer rmail-buffer
1574				     rmail-current-message))))))
1575      (setq i (1+ i))
1576      (with-current-buffer rmail-buffer
1577	(let ((rmail-delete-after-output nil))
1578	  (rmail-output file-name 1)))
1579      (if rmail-delete-after-output
1580	  (rmail-summary-delete-forward nil)
1581	(if (< i n)
1582	    (rmail-summary-next-msg 1))))))
1583
1584(defun rmail-summary-output-menu ()
1585  "Output current message to another Rmail file, chosen with a menu.
1586Also set the default for subsequent \\[rmail-output-to-rmail-file] commands.
1587The variables `rmail-secondary-file-directory' and
1588`rmail-secondary-file-regexp' control which files are offered in the menu."
1589  (interactive)
1590  (save-excursion
1591    (set-buffer rmail-buffer)
1592    (let ((rmail-delete-after-output nil))
1593      (call-interactively 'rmail-output-menu)))
1594  (if rmail-delete-after-output
1595      (rmail-summary-delete-forward nil)))
1596
1597(defun rmail-summary-construct-io-menu ()
1598  (let ((files (rmail-find-all-files rmail-secondary-file-directory)))
1599    (if files
1600	(progn
1601	  (define-key rmail-summary-mode-map [menu-bar classify input-menu]
1602	    (cons "Input Rmail File"
1603		  (rmail-list-to-menu "Input Rmail File"
1604				      files
1605				      'rmail-summary-input)))
1606	  (define-key rmail-summary-mode-map [menu-bar classify output-menu]
1607	    (cons "Output Rmail File"
1608		  (rmail-list-to-menu "Output Rmail File"
1609				      files
1610				      'rmail-summary-output-to-rmail-file))))
1611      (define-key rmail-summary-mode-map [menu-bar classify input-menu]
1612	'("Input Rmail File" . rmail-disable-menu))
1613      (define-key rmail-summary-mode-map [menu-bar classify output-menu]
1614	'("Output Rmail File" . rmail-disable-menu)))))
1615
1616(defun rmail-summary-output-body (&optional file-name)
1617  "Write this message body to the file FILE-NAME.
1618FILE-NAME defaults, interactively, from the Subject field of the message."
1619  (interactive)
1620  (save-excursion
1621    (set-buffer rmail-buffer)
1622    (let ((rmail-delete-after-output nil))
1623      (if file-name
1624	  (rmail-output-body-to-file file-name)
1625	(call-interactively 'rmail-output-body-to-file))))
1626  (if rmail-delete-after-output
1627      (rmail-summary-delete-forward nil)))
1628
1629;; Sorting messages in Rmail Summary buffer.
1630
1631(defun rmail-summary-sort-by-date (reverse)
1632  "Sort messages of current Rmail summary by date.
1633If prefix argument REVERSE is non-nil, sort them in reverse order."
1634  (interactive "P")
1635  (rmail-sort-from-summary (function rmail-sort-by-date) reverse))
1636
1637(defun rmail-summary-sort-by-subject (reverse)
1638  "Sort messages of current Rmail summary by subject.
1639If prefix argument REVERSE is non-nil, sort them in reverse order."
1640  (interactive "P")
1641  (rmail-sort-from-summary (function rmail-sort-by-subject) reverse))
1642
1643(defun rmail-summary-sort-by-author (reverse)
1644  "Sort messages of current Rmail summary by author.
1645If prefix argument REVERSE is non-nil, sort them in reverse order."
1646  (interactive "P")
1647  (rmail-sort-from-summary (function rmail-sort-by-author) reverse))
1648
1649(defun rmail-summary-sort-by-recipient (reverse)
1650  "Sort messages of current Rmail summary by recipient.
1651If prefix argument REVERSE is non-nil, sort them in reverse order."
1652  (interactive "P")
1653  (rmail-sort-from-summary (function rmail-sort-by-recipient) reverse))
1654
1655(defun rmail-summary-sort-by-correspondent (reverse)
1656  "Sort messages of current Rmail summary by other correspondent.
1657If prefix argument REVERSE is non-nil, sort them in reverse order."
1658  (interactive "P")
1659  (rmail-sort-from-summary (function rmail-sort-by-correspondent) reverse))
1660
1661(defun rmail-summary-sort-by-lines (reverse)
1662  "Sort messages of current Rmail summary by lines of the message.
1663If prefix argument REVERSE is non-nil, sort them in reverse order."
1664  (interactive "P")
1665  (rmail-sort-from-summary (function rmail-sort-by-lines) reverse))
1666
1667(defun rmail-summary-sort-by-labels (reverse labels)
1668  "Sort messages of current Rmail summary by labels.
1669If prefix argument REVERSE is non-nil, sort them in reverse order.
1670KEYWORDS is a comma-separated list of labels."
1671  (interactive "P\nsSort by labels: ")
1672  (rmail-sort-from-summary
1673   (function (lambda (reverse)
1674	       (rmail-sort-by-labels reverse labels)))
1675   reverse))
1676
1677(defun rmail-sort-from-summary (sortfun reverse)
1678  "Sort Rmail messages from Summary buffer and update it after sorting."
1679  (require 'rmailsort)
1680  (let ((selwin (selected-window)))
1681    (unwind-protect
1682	(progn (pop-to-buffer rmail-buffer)
1683	       (funcall sortfun reverse))
1684      (select-window selwin))))
1685
1686(provide 'rmailsum)
1687
1688;;; arch-tag: 556079ee-75c1-47f5-9884-2e0a0bc6c5a1
1689;;; rmailsum.el ends here
1690