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