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