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