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