(rmail-summary-rmail-update): Update rmail buffer even if it isn't displayed.
[emacs.git] / lisp / mail / rmailsum.el
blob2a80ff4469cfa4e43d0e749df8781e8a6d3afe11
1 ;;; rmailsum.el --- make summary buffers for the mail reader
3 ;; Copyright (C) 1985, 1993 Free Software Foundation, Inc.
5 ;; Maintainer: FSF
6 ;; Keywords: mail
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
24 ;;; Commentary:
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.
30 ;;; Code:
32 ;; Entry points for making a summary buffer.
34 ;; Regenerate the contents of the summary
35 ;; using the same selection criterion as last time.
36 ;; M-x revert-buffer in a summary buffer calls this function.
37 (defun rmail-update-summary (&rest ignore)
38 (apply (car rmail-summary-redo) (cdr rmail-summary-redo)))
40 (defun rmail-summary ()
41 "Display a summary of all messages, one line per message."
42 (interactive)
43 (rmail-new-summary "All" '(rmail-summary) nil))
45 (defun rmail-summary-by-labels (labels)
46 "Display a summary of all messages with one or more LABELS.
47 LABELS should be a string containing the desired labels, separated by commas."
48 (interactive "sLabels to summarize by: ")
49 (if (string= labels "")
50 (setq labels (or rmail-last-multi-labels
51 (error "No label specified"))))
52 (setq rmail-last-multi-labels labels)
53 (rmail-new-summary (concat "labels " labels)
54 (list 'rmail-summary-by-labels labels)
55 'rmail-message-labels-p
56 (concat ", \\(" (mail-comma-list-regexp labels) "\\),")))
58 (defun rmail-summary-by-recipients (recipients &optional primary-only)
59 "Display a summary of all messages with the given RECIPIENTS.
60 Normally checks the To, From and Cc fields of headers;
61 but if PRIMARY-ONLY is non-nil (prefix arg given),
62 only look in the To and From fields.
63 RECIPIENTS is a string of regexps separated by commas."
64 (interactive "sRecipients to summarize by: \nP")
65 (rmail-new-summary
66 (concat "recipients " recipients)
67 (list 'rmail-summary-by-recipients recipients primary-only)
68 'rmail-message-recipients-p
69 (mail-comma-list-regexp recipients) primary-only))
71 (defun rmail-summary-by-regexp (regexp)
72 "Display a summary of all messages according to regexp REGEXP.
73 If the regular expression is found in the header of the message
74 \(including in the date and other lines, as well as the subject line),
75 Emacs will list the header line in the RMAIL-summary."
76 (interactive "sRegexp to summarize by: ")
77 (if (string= regexp "")
78 (setq regexp (or rmail-last-regexp
79 (error "No regexp specified."))))
80 (setq rmail-last-regexp regexp)
81 (rmail-new-summary (concat "regexp " regexp)
82 (list 'rmail-summary-by-regexp regexp)
83 'rmail-message-regexp-p
84 regexp))
86 ;; rmail-summary-by-topic
87 ;; 1989 R.A. Schnitzler
89 (defun rmail-summary-by-topic (subject &optional whole-message)
90 "Display a summary of all messages with the given SUBJECT.
91 Normally checks the Subject field of headers;
92 but if WHOLE-MESSAGE is non-nil (prefix arg given),
93 look in the whole message.
94 SUBJECT is a string of regexps separated by commas."
95 (interactive "sTopics to summarize by: \nP")
96 (rmail-new-summary
97 (concat "about " subject)
98 (list 'rmail-summary-by-topic subject whole-message)
99 'rmail-message-subject-p
100 (mail-comma-list-regexp subject) whole-message))
102 (defun rmail-message-subject-p (msg subject &optional whole-message)
103 (save-restriction
104 (goto-char (rmail-msgbeg msg))
105 (search-forward "\n*** EOOH ***\n")
106 (narrow-to-region
107 (point)
108 (progn (search-forward (if whole-message "\^_" "\n\n")) (point)))
109 (goto-char (point-min))
110 (if whole-message (re-search-forward subject nil t)
111 (string-match subject (or (mail-fetch-field "Subject") "")) )))
113 (defun rmail-summary-by-senders (senders)
114 "Display a summary of all messages with the given SENDERS.
115 SENDERS is a string of names separated by commas."
116 (interactive "sSenders to summarize by: ")
117 (rmail-new-summary
118 (concat "senders " senders)
119 (list 'rmail-summary-by-senders senders)
120 'rmail-message-senders-p
121 (mail-comma-list-regexp senders)))
123 (defun rmail-message-senders-p (msg senders)
124 (save-restriction
125 (goto-char (rmail-msgbeg msg))
126 (search-forward "\n*** EOOH ***\n")
127 (narrow-to-region (point) (progn (search-forward "\n\n") (point)))
128 (string-match senders (or (mail-fetch-field "From") ""))))
130 ;; General making of a summary buffer.
132 (defvar rmail-summary-symbol-number 0)
134 (defun rmail-new-summary (description redo-form function &rest args)
135 "Create a summary of selected messages.
136 DESCRIPTION makes part of the mode line of the summary buffer.
137 For each message, FUNCTION is applied to the message number and ARGS...
138 and if the result is non-nil, that message is included.
139 nil for FUNCTION means all messages."
140 (message "Computing summary lines...")
141 (let (sumbuf mesg was-in-summary)
142 (save-excursion
143 ;; Go to the Rmail buffer.
144 (if (eq major-mode 'rmail-summary-mode)
145 (progn
146 (setq was-in-summary t)
147 (set-buffer rmail-buffer)))
148 ;; Find its summary buffer, or make one.
149 (setq sumbuf
150 (if (and rmail-summary-buffer
151 (buffer-name rmail-summary-buffer))
152 rmail-summary-buffer
153 (generate-new-buffer (concat (buffer-name) "-summary"))))
154 (setq mesg rmail-current-message)
155 ;; Filter the messages; make or get their summary lines.
156 (let ((summary-msgs ())
157 (new-summary-line-count 0))
158 (let ((msgnum 1)
159 (buffer-read-only nil)
160 (old-min (point-min-marker))
161 (old-max (point-max-marker)))
162 ;; Can't use save-restriction here; that doesn't work if we
163 ;; plan to modify text outside the original restriction.
164 (save-excursion
165 (widen)
166 (goto-char (point-min))
167 (while (>= rmail-total-messages msgnum)
168 (if (or (null function)
169 (apply function (cons msgnum args)))
170 (setq summary-msgs
171 (cons (cons msgnum (rmail-make-summary-line msgnum))
172 summary-msgs)))
173 (setq msgnum (1+ msgnum)))
174 (setq summary-msgs (nreverse summary-msgs)))
175 (narrow-to-region old-min old-max))
176 ;; Temporarily, while summary buffer is unfinished,
177 ;; we "don't have" a summary.
178 (setq rmail-summary-buffer nil)
179 (save-excursion
180 (let ((rbuf (current-buffer))
181 (total rmail-total-messages))
182 (set-buffer sumbuf)
183 ;; Set up the summary buffer's contents.
184 (let ((buffer-read-only nil))
185 (erase-buffer)
186 (while summary-msgs
187 (princ (cdr (car summary-msgs)) sumbuf)
188 (setq summary-msgs (cdr summary-msgs)))
189 (goto-char (point-min)))
190 ;; Set up the rest of its state and local variables.
191 (setq buffer-read-only t)
192 (rmail-summary-mode)
193 (make-local-variable 'minor-mode-alist)
194 (setq minor-mode-alist (list '(t (concat ": " description))))
195 (setq rmail-buffer rbuf
196 rmail-summary-redo redo-form
197 rmail-total-messages total))))
198 (setq rmail-summary-buffer sumbuf))
199 ;; Now display the summary buffer and go to the right place in it.
200 (or was-in-summary
201 (pop-to-buffer sumbuf))
202 (rmail-summary-goto-msg mesg t t)
203 (message "Computing summary lines...done")))
205 ;; Low levels of generating a summary.
207 (defun rmail-make-summary-line (msg)
208 (let ((line (or (aref rmail-summary-vector (1- msg))
209 (progn
210 (setq new-summary-line-count
211 (1+ new-summary-line-count))
212 (if (zerop (% new-summary-line-count 10))
213 (message "Computing summary lines...%d"
214 new-summary-line-count))
215 (rmail-make-summary-line-1 msg)))))
216 ;; Fix up the part of the summary that says "deleted" or "unseen".
217 (aset line 4
218 (if (rmail-message-deleted-p msg) ?\D
219 (if (= ?0 (char-after (+ 3 (rmail-msgbeg msg))))
220 ?\- ?\ )))
221 line))
223 (defun rmail-make-summary-line-1 (msg)
224 (goto-char (rmail-msgbeg msg))
225 (let* ((lim (save-excursion (forward-line 2) (point)))
227 (labels
228 (progn
229 (forward-char 3)
230 (concat
231 ; (if (save-excursion (re-search-forward ",answered," lim t))
232 ; "*" "")
233 ; (if (save-excursion (re-search-forward ",filed," lim t))
234 ; "!" "")
235 (if (progn (search-forward ",,") (eolp))
237 (concat "{"
238 (buffer-substring (point)
239 (progn (end-of-line) (point)))
240 "} ")))))
241 (line
242 (progn
243 (forward-line 1)
244 (if (looking-at "Summary-line: ")
245 (progn
246 (goto-char (match-end 0))
247 (setq line
248 (buffer-substring (point)
249 (progn (forward-line 1) (point)))))))))
250 ;; Obsolete status lines lacking a # should be flushed.
251 (and line
252 (not (string-match "#" line))
253 (progn
254 (delete-region (point)
255 (progn (forward-line -1) (point)))
256 (setq line nil)))
257 ;; If we didn't get a valid status line from the message,
258 ;; make a new one and put it in the message.
259 (or line
260 (let* ((case-fold-search t)
261 (next (rmail-msgend msg))
262 (beg (if (progn (goto-char (rmail-msgbeg msg))
263 (search-forward "\n*** EOOH ***\n" next t))
264 (point)
265 (forward-line 1)
266 (point)))
267 (end (progn (search-forward "\n\n" nil t) (point))))
268 (save-restriction
269 (narrow-to-region beg end)
270 (goto-char beg)
271 (setq line (rmail-make-basic-summary-line)))
272 (goto-char (rmail-msgbeg msg))
273 (forward-line 2)
274 (insert "Summary-line: " line)))
275 (setq pos (string-match "#" line))
276 (aset rmail-summary-vector (1- msg)
277 (concat (format "%4d " msg)
278 (substring line 0 pos)
279 labels
280 (substring line (1+ pos))))))
282 (defun rmail-make-basic-summary-line ()
283 (goto-char (point-min))
284 (concat (save-excursion
285 (if (not (re-search-forward "^Date:" nil t))
287 (cond ((re-search-forward "\\([^0-9:]\\)\\([0-3]?[0-9]\\)\\([- \t_]+\\)\\([adfjmnos][aceopu][bcglnprtvy]\\)"
288 (save-excursion (end-of-line) (point)) t)
289 (format "%2d-%3s"
290 (string-to-int (buffer-substring
291 (match-beginning 2)
292 (match-end 2)))
293 (buffer-substring
294 (match-beginning 4) (match-end 4))))
295 ((re-search-forward "\\([^a-z]\\)\\([adfjmnos][acepou][bcglnprtvy]\\)\\([-a-z \t_]*\\)\\([0-9][0-9]?\\)"
296 (save-excursion (end-of-line) (point)) t)
297 (format "%2d-%3s"
298 (string-to-int (buffer-substring
299 (match-beginning 4)
300 (match-end 4)))
301 (buffer-substring
302 (match-beginning 2) (match-end 2))))
303 (t "??????"))))
305 (save-excursion
306 (if (not (re-search-forward "^From:[ \t]*" nil t))
308 (let* ((from (mail-strip-quoted-names
309 (buffer-substring
310 (1- (point))
311 (progn (end-of-line)
312 (skip-chars-backward " \t")
313 (point)))))
314 len mch lo)
315 (if (string-match (concat "^"
316 (regexp-quote (user-login-name))
317 "\\($\\|@\\)")
318 from)
319 (save-excursion
320 (goto-char (point-min))
321 (if (not (re-search-forward "^To:[ \t]*" nil t))
323 (setq from
324 (concat "to: "
325 (mail-strip-quoted-names
326 (buffer-substring
327 (point)
328 (progn (end-of-line)
329 (skip-chars-backward " \t")
330 (point)))))))))
331 (setq len (length from))
332 (setq mch (string-match "[@%]" from))
333 (format "%25s"
334 (if (or (not mch) (<= len 25))
335 (substring from (max 0 (- len 25)))
336 (substring from
337 (setq lo (cond ((< (- mch 9) 0) 0)
338 ((< len (+ mch 16))
339 (- len 25))
340 (t (- mch 9))))
341 (min len (+ lo 25))))))))
342 " #"
343 (if (re-search-forward "^Subject:" nil t)
344 (progn (skip-chars-forward " \t")
345 (buffer-substring (point)
346 (progn (end-of-line)
347 (point))))
348 (re-search-forward "[\n][\n]+" nil t)
349 (buffer-substring (point) (progn (end-of-line) (point))))
350 "\n"))
352 ;; Simple motion in a summary buffer.
354 (defun rmail-summary-next-all (&optional number)
355 (interactive "p")
356 (forward-line (if number number 1))
357 (display-buffer rmail-buffer))
359 (defun rmail-summary-previous-all (&optional number)
360 (interactive "p")
361 (forward-line (- (if number number 1)))
362 (display-buffer rmail-buffer))
364 (defun rmail-summary-next-msg (&optional number)
365 "Display next non-deleted msg from rmail file.
366 With optional prefix argument NUMBER, moves forward this number of non-deleted
367 messages, or backward if NUMBER is negative."
368 (interactive "p")
369 (forward-line 0)
370 (and (> number 0) (end-of-line))
371 (let ((count (if (< number 0) (- number) number))
372 (search (if (> number 0) 're-search-forward 're-search-backward))
373 (non-del-msg-found nil))
374 (while (and (> count 0) (setq non-del-msg-found
375 (or (funcall search "^....[^D]" nil t)
376 non-del-msg-found)))
377 (setq count (1- count))))
378 (beginning-of-line)
379 (display-buffer rmail-buffer))
381 (defun rmail-summary-previous-msg (&optional number)
382 (interactive "p")
383 (rmail-summary-next-msg (- (if number number 1))))
385 (defun rmail-summary-next-labeled-message (n labels)
386 "Show next message with LABEL. Defaults to last labels used.
387 With prefix argument N moves forward N messages with these labels."
388 (interactive "p\nsMove to next msg with labels: ")
389 (save-excursion
390 (set-buffer rmail-buffer)
391 (rmail-next-labeled-message n labels)))
393 (defun rmail-summary-previous-labeled-message (n labels)
394 "Show previous message with LABEL. Defaults to last labels used.
395 With prefix argument N moves backward N messages with these labels."
396 (interactive "p\nsMove to previous msg with labels: ")
397 (save-excursion
398 (set-buffer rmail-buffer)
399 (rmail-previous-labeled-message n labels)))
401 ;; Delete and undelete summary commands.
403 (defun rmail-summary-delete-forward (&optional backward)
404 "Delete this message and move to next nondeleted one.
405 Deleted messages stay in the file until the \\[rmail-expunge] command is given.
406 With prefix argument, delete and move backward."
407 (interactive "P")
408 (let (end)
409 (rmail-summary-goto-msg)
410 (pop-to-buffer rmail-buffer)
411 (rmail-delete-message)
412 (let ((del-msg rmail-current-message))
413 (pop-to-buffer rmail-summary-buffer)
414 (rmail-summary-mark-deleted del-msg)
415 (while (and (not (if backward (bobp) (eobp)))
416 (save-excursion (beginning-of-line)
417 (looking-at " +[0-9]+D")))
418 (forward-line (if backward -1 1))))))
420 (defun rmail-summary-delete-backward ()
421 "Delete this message and move to previous nondeleted one.
422 Deleted messages stay in the file until the \\[rmail-expunge] command is given."
423 (interactive)
424 (rmail-summary-delete-forward t))
426 (defun rmail-summary-mark-deleted (&optional n undel)
427 (and n (rmail-summary-goto-msg n t t))
428 (or (eobp)
429 (let ((buffer-read-only nil))
430 (skip-chars-forward " ")
431 (skip-chars-forward "[0-9]")
432 (if undel
433 (if (looking-at "D")
434 (progn (delete-char 1) (insert " ")))
435 (delete-char 1)
436 (insert "D"))))
437 (beginning-of-line))
439 (defun rmail-summary-mark-undeleted (n)
440 (rmail-summary-mark-deleted n t))
442 (defun rmail-summary-deleted-p (&optional n)
443 (save-excursion
444 (and n (rmail-summary-goto-msg n nil t))
445 (skip-chars-forward " ")
446 (skip-chars-forward "[0-9]")
447 (looking-at "D")))
449 (defun rmail-summary-undelete (&optional arg)
450 "Undelete current message.
451 Optional prefix ARG means undelete ARG previous messages."
452 (interactive "p")
453 (if (/= arg 1)
454 (rmail-summary-undelete-many arg)
455 (let ((buffer-read-only nil)
456 (opoint (point)))
457 (end-of-line)
458 (cond ((re-search-backward "\\(^ *[0-9]*\\)\\(D\\)" nil t)
459 (replace-match "\\1 ")
460 (rmail-summary-goto-msg)
461 (pop-to-buffer rmail-buffer)
462 (and (rmail-message-deleted-p rmail-current-message)
463 (rmail-undelete-previous-message))
464 (pop-to-buffer rmail-summary-buffer))
465 (t (goto-char opoint))))))
467 (defun rmail-summary-undelete-many (&optional n)
468 "Undelete all deleted msgs, optional prefix arg N means undelete N prev msgs."
469 (interactive "P")
470 (save-excursion
471 (set-buffer rmail-buffer)
472 (let* ((init-msg (if n rmail-current-message rmail-total-messages))
473 (rmail-current-message init-msg)
474 (n (or n rmail-total-messages))
475 (msgs-undeled 0))
476 (while (and (> rmail-current-message 0)
477 (< msgs-undeled n))
478 (if (rmail-message-deleted-p rmail-current-message)
479 (progn (rmail-set-attribute "deleted" nil)
480 (setq msgs-undeled (1+ msgs-undeled))))
481 (setq rmail-current-message (1- rmail-current-message)))
482 (set-buffer rmail-summary-buffer)
483 (setq rmail-current-message init-msg msgs-undeled 0)
484 (while (and (> rmail-current-message 0)
485 (< msgs-undeled n))
486 (if (rmail-summary-deleted-p rmail-current-message)
487 (progn (rmail-summary-mark-undeleted rmail-current-message)
488 (setq msgs-undeled (1+ msgs-undeled))))
489 (setq rmail-current-message (1- rmail-current-message))))
490 (rmail-summary-goto-msg)))
492 ;; Rmail Summary mode is suitable only for specially formatted data.
493 (put 'rmail-summary-mode 'mode-class 'special)
495 (defun rmail-summary-mode ()
496 "Rmail Summary Mode is invoked from Rmail Mode by using \\<rmail-mode-map>\\[rmail-summary].
497 As commands are issued in the summary buffer, they are applied to the
498 corresponding mail messages in the rmail buffer.
500 All normal editing commands are turned off.
501 Instead, nearly all the Rmail mode commands are available,
502 though many of them move only among the messages in the summary.
504 These additional commands exist:
506 \\[rmail-summary-undelete-many] Undelete all or prefix arg deleted messages.
507 \\[rmail-summary-wipe] Delete the summary and go to the Rmail buffer.
509 Commands for sorting the summary:
511 \\[rmail-summary-sort-by-date] Sort by date.
512 \\[rmail-summary-sort-by-subject] Sort by subject.
513 \\[rmail-summary-sort-by-author] Sort by author.
514 \\[rmail-summary-sort-by-recipient] Sort by recipient.
515 \\[rmail-summary-sort-by-correspondent] Sort by correspondent.
516 \\[rmail-summary-sort-by-lines] Sort by lines.
517 \\[rmail-summary-sort-by-keywords] Sort by keywords."
518 (interactive)
519 (kill-all-local-variables)
520 (setq major-mode 'rmail-summary-mode)
521 (setq mode-name "RMAIL Summary")
522 (setq truncate-lines t)
523 (setq buffer-read-only t)
524 (set-syntax-table text-mode-syntax-table)
525 (make-local-variable 'rmail-buffer)
526 (make-local-variable 'rmail-total-messages)
527 (make-local-variable 'rmail-current-message)
528 (setq rmail-current-message nil)
529 (make-local-variable 'rmail-summary-redo)
530 (setq rmail-summary-redo nil)
531 (make-local-variable 'revert-buffer-function)
532 (make-local-variable 'post-command-hook)
533 (rmail-summary-enable)
534 (run-hooks 'rmail-summary-mode-hook))
536 ;; Summary features need to be disabled during edit mode.
537 (defun rmail-summary-disable ()
538 (use-local-map text-mode-map)
539 (remove-hook 'post-command-hook 'rmail-summary-rmail-update)
540 (setq revert-buffer-function nil))
542 (defun rmail-summary-enable ()
543 (use-local-map rmail-summary-mode-map)
544 (add-hook 'post-command-hook 'rmail-summary-rmail-update)
545 (setq revert-buffer-function 'rmail-update-summary))
547 ;; Show in Rmail the message described by the summary line that point is on,
548 ;; but only if the Rmail buffer is already visible.
549 ;; This is a post-command-hook in summary buffers.
550 (defun rmail-summary-rmail-update ()
551 (let (buffer-read-only)
552 (save-excursion
553 ;; If at end of buffer, pretend we are on the last text line.
554 (if (eobp)
555 (forward-line -1))
556 (beginning-of-line)
557 (skip-chars-forward " ")
558 (let ((msg-num (string-to-int (buffer-substring
559 (point)
560 (progn (skip-chars-forward "0-9")
561 (point))))))
562 (or (eq rmail-current-message msg-num)
563 (let ((window (get-buffer-window rmail-buffer))
564 (owin (selected-window)))
565 (setq rmail-current-message msg-num)
566 (if (= (following-char) ?-)
567 (progn
568 (delete-char 1)
569 (insert " ")))
570 (if window
571 ;; Using save-window-excursion would cause the new value
572 ;; of point to get lost.
573 (unwind-protect
574 (progn
575 (select-window window)
576 (rmail-show-message msg-num))
577 (select-window owin))
578 (save-excursion
579 (set-buffer rmail-buffer)
580 (rmail-show-message msg-num)))))))))
582 (defvar rmail-summary-mode-map nil)
584 (if rmail-summary-mode-map
586 (setq rmail-summary-mode-map (make-keymap))
587 (suppress-keymap rmail-summary-mode-map)
588 (define-key rmail-summary-mode-map "a" 'rmail-summary-add-label)
589 (define-key rmail-summary-mode-map "c" 'rmail-summary-continue)
590 (define-key rmail-summary-mode-map "d" 'rmail-summary-delete-forward)
591 (define-key rmail-summary-mode-map "\C-d" 'rmail-summary-delete-backward)
592 (define-key rmail-summary-mode-map "e" 'rmail-summary-edit-current-message)
593 (define-key rmail-summary-mode-map "f" 'rmail-summary-forward)
594 (define-key rmail-summary-mode-map "g" 'rmail-summary-get-new-mail)
595 (define-key rmail-summary-mode-map "h" 'rmail-summary)
596 (define-key rmail-summary-mode-map "i" 'rmail-summary-input)
597 (define-key rmail-summary-mode-map "j" 'rmail-summary-goto-msg)
598 (define-key rmail-summary-mode-map "k" 'rmail-summary-kill-label)
599 (define-key rmail-summary-mode-map "l" 'rmail-summary-by-labels)
600 (define-key rmail-summary-mode-map "\e\C-h" 'rmail-summary)
601 (define-key rmail-summary-mode-map "\e\C-l" 'rmail-summary-by-labels)
602 (define-key rmail-summary-mode-map "\e\C-r" 'rmail-summary-by-recipients)
603 (define-key rmail-summary-mode-map "\e\C-s" 'rmail-summary-by-regexp)
604 (define-key rmail-summary-mode-map "\e\C-t" 'rmail-summary-by-topic)
605 (define-key rmail-summary-mode-map "m" 'rmail-summary-mail)
606 (define-key rmail-summary-mode-map "\M-m" 'rmail-summary-retry-failure)
607 (define-key rmail-summary-mode-map "n" 'rmail-summary-next-msg)
608 (define-key rmail-summary-mode-map "\en" 'rmail-summary-next-all)
609 (define-key rmail-summary-mode-map "\e\C-n" 'rmail-summary-next-labeled-message)
610 (define-key rmail-summary-mode-map "o" 'rmail-summary-output-to-rmail-file)
611 (define-key rmail-summary-mode-map "\C-o" 'rmail-summary-output)
612 (define-key rmail-summary-mode-map "p" 'rmail-summary-previous-msg)
613 (define-key rmail-summary-mode-map "\ep" 'rmail-summary-previous-all)
614 (define-key rmail-summary-mode-map "\e\C-p" 'rmail-summary-previous-labeled-message)
615 (define-key rmail-summary-mode-map "q" 'rmail-summary-quit)
616 (define-key rmail-summary-mode-map "r" 'rmail-summary-reply)
617 (define-key rmail-summary-mode-map "s" 'rmail-summary-expunge-and-save)
618 (define-key rmail-summary-mode-map "\es" 'rmail-summary-search)
619 (define-key rmail-summary-mode-map "t" 'rmail-summary-toggle-header)
620 (define-key rmail-summary-mode-map "u" 'rmail-summary-undelete)
621 (define-key rmail-summary-mode-map "\M-u" 'rmail-summary-undelete-many)
622 (define-key rmail-summary-mode-map "w" 'rmail-summary-wipe)
623 (define-key rmail-summary-mode-map "x" 'rmail-summary-expunge)
624 (define-key rmail-summary-mode-map "." 'rmail-summary-beginning-of-message)
625 (define-key rmail-summary-mode-map "<" 'rmail-summary-first-message)
626 (define-key rmail-summary-mode-map ">" 'rmail-summary-last-message)
627 (define-key rmail-summary-mode-map " " 'rmail-summary-scroll-msg-up)
628 (define-key rmail-summary-mode-map "\177" 'rmail-summary-scroll-msg-down)
629 (define-key rmail-summary-mode-map "?" 'describe-mode)
630 (define-key rmail-summary-mode-map "\C-c\C-s\C-d"
631 'rmail-summary-sort-by-date)
632 (define-key rmail-summary-mode-map "\C-c\C-s\C-s"
633 'rmail-summary-sort-by-subject)
634 (define-key rmail-summary-mode-map "\C-c\C-s\C-a"
635 'rmail-summary-sort-by-author)
636 (define-key rmail-summary-mode-map "\C-c\C-s\C-r"
637 'rmail-summary-sort-by-recipient)
638 (define-key rmail-summary-mode-map "\C-c\C-s\C-c"
639 'rmail-summary-sort-by-correspondent)
640 (define-key rmail-summary-mode-map "\C-c\C-s\C-l"
641 'rmail-summary-sort-by-lines)
642 (define-key rmail-summary-mode-map "\C-c\C-s\C-k"
643 'rmail-summary-sort-by-keywords)
646 ;;; Menu bar bindings.
648 (define-key rmail-summary-mode-map [menu-bar] (make-sparse-keymap))
650 (define-key rmail-summary-mode-map [menu-bar classify]
651 (cons "Classify" (make-sparse-keymap "Classify")))
653 (define-key rmail-summary-mode-map [menu-bar classify output-inbox]
654 '("Output (inbox)" . rmail-summary-output))
656 (define-key rmail-summary-mode-map [menu-bar classify output]
657 '("Output (Rmail)" . rmail-summary-output-to-rmail-file))
659 (define-key rmail-summary-mode-map [menu-bar classify kill-label]
660 '("Kill Label" . rmail-summary-kill-label))
662 (define-key rmail-summary-mode-map [menu-bar classify add-label]
663 '("Add Label" . rmail-summary-add-label))
665 (define-key rmail-summary-mode-map [menu-bar summary]
666 (cons "Summary" (make-sparse-keymap "Summary")))
668 (define-key rmail-summary-mode-map [menu-bar summary labels]
669 '("By Labels" . rmail-summary-by-labels))
671 (define-key rmail-summary-mode-map [menu-bar summary recipients]
672 '("By Recipients" . rmail-summary-by-recipients))
674 (define-key rmail-summary-mode-map [menu-bar summary topic]
675 '("By Topic" . rmail-summary-by-topic))
677 (define-key rmail-summary-mode-map [menu-bar summary regexp]
678 '("By Regexp" . rmail-summary-by-regexp))
680 (define-key rmail-summary-mode-map [menu-bar summary all]
681 '("All" . rmail-summary))
683 (define-key rmail-summary-mode-map [menu-bar mail]
684 (cons "Mail" (make-sparse-keymap "Mail")))
686 (define-key rmail-summary-mode-map [menu-bar mail continue]
687 '("Continue" . rmail-summary-continue))
689 (define-key rmail-summary-mode-map [menu-bar mail forward]
690 '("Forward" . rmail-summary-forward))
692 (define-key rmail-summary-mode-map [menu-bar mail retry]
693 '("Retry" . rmail-summary-retry-failure))
695 (define-key rmail-summary-mode-map [menu-bar mail reply]
696 '("Reply" . rmail-summary-reply))
698 (define-key rmail-summary-mode-map [menu-bar mail mail]
699 '("Mail" . rmail-summary-mail))
701 (define-key rmail-summary-mode-map [menu-bar delete]
702 (cons "Delete" (make-sparse-keymap "Delete")))
704 (define-key rmail-summary-mode-map [menu-bar delete expunge/save]
705 '("Expunge/Save" . rmail-summary-expunge-and-save))
707 (define-key rmail-summary-mode-map [menu-bar delete expunge]
708 '("Expunge" . rmail-summary-expunge))
710 (define-key rmail-summary-mode-map [menu-bar delete undelete]
711 '("Undelete" . rmail-summary-undelete))
713 (define-key rmail-summary-mode-map [menu-bar delete delete]
714 '("Delete" . rmail-summary-delete-forward))
716 (define-key rmail-summary-mode-map [menu-bar move]
717 (cons "Move" (make-sparse-keymap "Move")))
719 (define-key rmail-summary-mode-map [menu-bar move search-back]
720 '("Search Back" . rmail-summary-search-backward))
722 (define-key rmail-summary-mode-map [menu-bar move search]
723 '("Search" . rmail-summary-search))
725 (define-key rmail-summary-mode-map [menu-bar move previous]
726 '("Previous Nondeleted" . rmail-summary-previous-msg))
728 (define-key rmail-summary-mode-map [menu-bar move next]
729 '("Next Nondeleted" . rmail-summary-next-msg))
731 (define-key rmail-summary-mode-map [menu-bar move last]
732 '("Last" . rmail-summary-last-message))
734 (define-key rmail-summary-mode-map [menu-bar move first]
735 '("First" . rmail-summary-first-message))
737 (define-key rmail-summary-mode-map [menu-bar move previous]
738 '("Previous" . rmail-summary-previous-all))
740 (define-key rmail-summary-mode-map [menu-bar move next]
741 '("Next" . rmail-summary-next-all))
743 (defvar rmail-summary-overlay nil)
745 (defun rmail-summary-goto-msg (&optional n nowarn skip-rmail)
746 (interactive "P")
747 (if (consp n) (setq n (prefix-numeric-value n)))
748 (if (eobp) (forward-line -1))
749 (beginning-of-line)
750 (let ((buf rmail-buffer)
751 (cur (point))
752 message-not-found
753 (curmsg (string-to-int
754 (buffer-substring (point)
755 (min (point-max) (+ 5 (point)))))))
756 ;; If message number N was specified, find that message's line
757 ;; or set message-not-found.
758 ;; If N wasn't specified or that message can't be found.
759 ;; set N by default.
760 (if (not n)
761 (setq n curmsg)
762 (if (< n 1)
763 (progn (message "No preceding message")
764 (setq n 1)))
765 (if (> n rmail-total-messages)
766 (progn (message "No following message")
767 (goto-char (point-max))
768 (rmail-summary-goto-msg)))
769 (goto-char (point-min))
770 (if (not (re-search-forward (concat "^ *" (int-to-string n)) nil t))
771 (progn (or nowarn (message "Message %d not found" n))
772 (setq n curmsg)
773 (setq message-not-found t)
774 (goto-char cur))))
775 (beginning-of-line)
776 (skip-chars-forward " ")
777 (skip-chars-forward "0-9")
778 (save-excursion (if (= (following-char) ?-)
779 (let ((buffer-read-only nil))
780 (delete-char 1)
781 (insert " "))))
782 ;; Make sure we have an overlay to use.
783 (or rmail-summary-overlay
784 (progn
785 (make-local-variable 'rmail-summary-overlay)
786 (setq rmail-summary-overlay (make-overlay (point) (point)))))
787 ;; If this message is in the summary, use the overlay to highlight it.
788 ;; Otherwise, don't highlight anything.
789 (if message-not-found
790 (overlay-put rmail-summary-overlay 'face nil)
791 (move-overlay rmail-summary-overlay
792 (save-excursion (beginning-of-line) (1+ (point)))
793 (save-excursion (end-of-line) (point)))
794 (overlay-put rmail-summary-overlay 'face 'highlight))
795 (beginning-of-line)
796 (if skip-rmail
798 (let ((selwin (selected-window)))
799 (unwind-protect
800 (progn (pop-to-buffer buf)
801 (rmail-show-message n))
802 (select-window selwin))))))
804 (defun rmail-summary-scroll-msg-up (&optional dist)
805 "Scroll other window forward."
806 (interactive "P")
807 (scroll-other-window dist))
809 (defun rmail-summary-scroll-msg-down (&optional dist)
810 "Scroll other window backward."
811 (interactive "P")
812 (scroll-other-window
813 (cond ((eq dist '-) nil)
814 ((null dist) '-)
815 (t (- (prefix-numeric-value dist))))))
817 (defun rmail-summary-beginning-of-message ()
818 "Show current message from the beginning."
819 (interactive)
820 (pop-to-buffer rmail-buffer)
821 (beginning-of-buffer)
822 (pop-to-buffer rmail-summary-buffer))
824 (defun rmail-summary-quit ()
825 "Quit out of Rmail and Rmail summary."
826 (interactive)
827 (rmail-summary-wipe)
828 (rmail-quit))
830 (defun rmail-summary-wipe ()
831 "Kill and wipe away Rmail summary, remaining within Rmail."
832 (interactive)
833 (save-excursion (set-buffer rmail-buffer) (setq rmail-summary-buffer nil))
834 (let ((local-rmail-buffer rmail-buffer))
835 (kill-buffer (current-buffer))
836 ;; Delete window if not only one.
837 (if (not (eq (selected-window) (next-window nil 'no-minibuf)))
838 (delete-window))
839 ;; Switch windows to the rmail buffer, or switch to it in this window.
840 (pop-to-buffer local-rmail-buffer)))
842 (defun rmail-summary-expunge ()
843 "Actually erase all deleted messages and recompute summary headers."
844 (interactive)
845 (save-excursion
846 (set-buffer rmail-buffer)
847 (rmail-only-expunge))
848 (rmail-update-summary))
850 (defun rmail-summary-expunge-and-save ()
851 "Expunge and save RMAIL file."
852 (interactive)
853 (save-excursion
854 (set-buffer rmail-buffer)
855 (rmail-only-expunge))
856 (rmail-update-summary)
857 (save-excursion
858 (set-buffer rmail-buffer)
859 (save-buffer))
860 (set-buffer-modified-p nil))
862 (defun rmail-summary-get-new-mail ()
863 "Get new mail and recompute summary headers."
864 (interactive)
865 (let (msg)
866 (save-excursion
867 (set-buffer rmail-buffer)
868 (rmail-get-new-mail)
869 ;; Get the proper new message number.
870 (setq msg rmail-current-message))
871 ;; Make sure that message is displayed.
872 (rmail-summary-goto-msg msg)))
874 (defun rmail-summary-input (filename)
875 "Run Rmail on file FILENAME."
876 (interactive "FRun rmail on RMAIL file: ")
877 ;; We switch windows here, then display the other Rmail file there.
878 (pop-to-buffer rmail-buffer)
879 (rmail filename))
881 (defun rmail-summary-first-message ()
882 "Show first message in Rmail file from summary buffer."
883 (interactive)
884 (beginning-of-buffer))
886 (defun rmail-summary-last-message ()
887 "Show last message in Rmail file from summary buffer."
888 (interactive)
889 (end-of-buffer)
890 (forward-line -1))
892 (defvar rmail-summary-edit-map nil)
893 (if rmail-summary-edit-map
895 (setq rmail-summary-edit-map
896 (nconc (make-sparse-keymap) text-mode-map))
897 (define-key rmail-summary-edit-map "\C-c\C-c" 'rmail-cease-edit)
898 (define-key rmail-summary-edit-map "\C-c\C-]" 'rmail-abort-edit))
900 (defun rmail-summary-edit-current-message ()
901 "Edit the contents of this message."
902 (interactive)
903 (pop-to-buffer rmail-buffer)
904 (rmail-edit-current-message)
905 (use-local-map rmail-summary-edit-map))
907 (defun rmail-summary-cease-edit ()
908 "Finish editing message, then go back to Rmail summary buffer."
909 (interactive)
910 (rmail-cease-edit)
911 (pop-to-buffer rmail-summary-buffer))
913 (defun rmail-summary-abort-edit ()
914 "Abort edit of current message; restore original contents.
915 Go back to summary buffer."
916 (interactive)
917 (rmail-abort-edit)
918 (pop-to-buffer rmail-summary-buffer))
920 (defun rmail-summary-search-backward (regexp &optional n)
921 "Show message containing next match for REGEXP.
922 Prefix argument gives repeat count; negative argument means search
923 backwards (through earlier messages).
924 Interactively, empty argument means use same regexp used last time."
925 (interactive
926 (let* ((reversep (>= (prefix-numeric-value current-prefix-arg) 0))
927 (prompt
928 (concat (if reversep "Reverse " "") "Rmail search (regexp): "))
929 regexp)
930 (if rmail-search-last-regexp
931 (setq prompt (concat prompt
932 "(default "
933 rmail-search-last-regexp
934 ") ")))
935 (setq regexp (read-string prompt))
936 (cond ((not (equal regexp ""))
937 (setq rmail-search-last-regexp regexp))
938 ((not rmail-search-last-regexp)
939 (error "No previous Rmail search string")))
940 (list rmail-search-last-regexp
941 (prefix-numeric-value current-prefix-arg))))
942 ;; Don't use save-excursion because that prevents point from moving
943 ;; properly in the summary buffer.
944 (let ((buffer (current-buffer)))
945 (unwind-protect
946 (progn
947 (set-buffer rmail-buffer)
948 (rmail-search regexp (- n)))
949 (set-buffer buffer))))
951 (defun rmail-summary-search (regexp &optional n)
952 "Show message containing next match for REGEXP.
953 Prefix argument gives repeat count; negative argument means search
954 backwards (through earlier messages).
955 Interactively, empty argument means use same regexp used last time."
956 (interactive
957 (let* ((reversep (< (prefix-numeric-value current-prefix-arg) 0))
958 (prompt
959 (concat (if reversep "Reverse " "") "Rmail search (regexp): "))
960 regexp)
961 (if rmail-search-last-regexp
962 (setq prompt (concat prompt
963 "(default "
964 rmail-search-last-regexp
965 ") ")))
966 (setq regexp (read-string prompt))
967 (cond ((not (equal regexp ""))
968 (setq rmail-search-last-regexp regexp))
969 ((not rmail-search-last-regexp)
970 (error "No previous Rmail search string")))
971 (list rmail-search-last-regexp
972 (prefix-numeric-value current-prefix-arg))))
973 ;; Don't use save-excursion because that prevents point from moving
974 ;; properly in the summary buffer.
975 (let ((buffer (current-buffer)))
976 (unwind-protect
977 (progn
978 (set-buffer rmail-buffer)
979 (rmail-search regexp n))
980 (set-buffer buffer))))
982 (defun rmail-summary-toggle-header ()
983 "Show original message header if pruned header currently shown, or vice versa."
984 (interactive)
985 (save-excursion
986 (set-buffer rmail-buffer)
987 (rmail-toggle-header)))
989 (defun rmail-summary-add-label (label)
990 "Add LABEL to labels associated with current Rmail message.
991 Completion is performed over known labels when reading."
992 (interactive (list (save-excursion
993 (set-buffer rmail-buffer)
994 (rmail-read-label "Add label"))))
995 (save-excursion
996 (set-buffer rmail-buffer)
997 (rmail-add-label label)))
999 (defun rmail-summary-kill-label (label)
1000 "Remove LABEL from labels associated with current Rmail message.
1001 Completion is performed over known labels when reading."
1002 (interactive (list (save-excursion
1003 (set-buffer rmail-buffer)
1004 (rmail-read-label "Kill label"))))
1005 (save-excursion
1006 (set-buffer rmail-buffer)
1007 (rmail-set-label label nil)))
1009 ;;;; *** Rmail Summary Mailing Commands ***
1011 (defun rmail-summary-mail ()
1012 "Send mail in another window.
1013 While composing the message, use \\[mail-yank-original] to yank the
1014 original message into it."
1015 (interactive)
1016 (rmail-start-mail nil nil nil nil nil rmail-buffer)
1017 (use-local-map (copy-keymap (current-local-map)))
1018 (define-key (current-local-map)
1019 "\C-c\C-c" 'rmail-summary-send-and-exit))
1021 (defun rmail-summary-continue ()
1022 "Continue composing outgoing message previously being composed."
1023 (interactive)
1024 (rmail-start-mail t))
1026 (defun rmail-summary-reply (just-sender)
1027 "Reply to the current message.
1028 Normally include CC: to all other recipients of original message;
1029 prefix argument means ignore them. While composing the reply,
1030 use \\[mail-yank-original] to yank the original message into it."
1031 (interactive "P")
1032 (set-buffer rmail-buffer)
1033 (rmail-reply just-sender)
1034 (use-local-map (copy-keymap (current-local-map)))
1035 (define-key (current-local-map)
1036 "\C-c\C-c" 'rmail-summary-send-and-exit))
1038 (defun rmail-summary-retry-failure ()
1039 "Edit a mail message which is based on the contents of the current message.
1040 For a message rejected by the mail system, extract the interesting headers and
1041 the body of the original message; otherwise copy the current message."
1042 (interactive)
1043 (set-buffer rmail-buffer)
1044 (rmail-retry-failure)
1045 (use-local-map (copy-keymap (current-local-map)))
1046 (define-key (current-local-map)
1047 "\C-c\C-c" 'rmail-summary-send-and-exit))
1049 (defun rmail-summary-send-and-exit ()
1050 "Send mail reply and return to summary buffer."
1051 (interactive)
1052 (mail-send-and-exit t))
1054 (defun rmail-summary-forward (resend)
1055 "Forward the current message to another user.
1056 With prefix argument, \"resend\" the message instead of forwarding it;
1057 see the documentation of `rmail-resend'."
1058 (interactive "P")
1059 (save-excursion
1060 (set-buffer rmail-buffer)
1061 (rmail-forward resend)
1062 (use-local-map (copy-keymap (current-local-map)))
1063 (define-key (current-local-map)
1064 "\C-c\C-c" 'rmail-summary-send-and-exit)))
1066 ;; Summary output commands.
1068 (defun rmail-summary-output-to-rmail-file ()
1069 "Append the current message to an Rmail file named FILE-NAME.
1070 If the file does not exist, ask if it should be created.
1071 If file is being visited, the message is appended to the Emacs
1072 buffer visiting that file."
1073 (interactive)
1074 (save-excursion
1075 (set-buffer rmail-buffer)
1076 (let ((rmail-delete-after-output nil))
1077 (call-interactively 'rmail-output-to-rmail-file)))
1078 (if rmail-delete-after-output
1079 (rmail-summary-delete-forward nil)))
1081 (defun rmail-summary-output ()
1082 "Append this message to Unix mail file named FILE-NAME."
1083 (interactive)
1084 (save-excursion
1085 (set-buffer rmail-buffer)
1086 (let ((rmail-delete-after-output nil))
1087 (call-interactively 'rmail-output)))
1088 (if rmail-delete-after-output
1089 (rmail-summary-delete-forward nil)))
1091 ;; Sorting messages in Rmail Summary buffer.
1093 (defun rmail-summary-sort-by-date (reverse)
1094 "Sort messages of current Rmail summary by date.
1095 If prefix argument REVERSE is non-nil, sort them in reverse order."
1096 (interactive "P")
1097 (rmail-sort-from-summary (function rmail-sort-by-date) reverse))
1099 (defun rmail-summary-sort-by-subject (reverse)
1100 "Sort messages of current Rmail summary by subject.
1101 If prefix argument REVERSE is non-nil, sort them in reverse order."
1102 (interactive "P")
1103 (rmail-sort-from-summary (function rmail-sort-by-subject) reverse))
1105 (defun rmail-summary-sort-by-author (reverse)
1106 "Sort messages of current Rmail summary by author.
1107 If prefix argument REVERSE is non-nil, sort them in reverse order."
1108 (interactive "P")
1109 (rmail-sort-from-summary (function rmail-sort-by-author) reverse))
1111 (defun rmail-summary-sort-by-recipient (reverse)
1112 "Sort messages of current Rmail summary by recipient.
1113 If prefix argument REVERSE is non-nil, sort them in reverse order."
1114 (interactive "P")
1115 (rmail-sort-from-summary (function rmail-sort-by-recipient) reverse))
1117 (defun rmail-summary-sort-by-correspondent (reverse)
1118 "Sort messages of current Rmail summary by other correspondent.
1119 If prefix argument REVERSE is non-nil, sort them in reverse order."
1120 (interactive "P")
1121 (rmail-sort-from-summary (function rmail-sort-by-correspondent) reverse))
1123 (defun rmail-summary-sort-by-lines (reverse)
1124 "Sort messages of current Rmail summary by lines of the message.
1125 If prefix argument REVERSE is non-nil, sort them in reverse order."
1126 (interactive "P")
1127 (rmail-sort-from-summary (function rmail-sort-by-lines) reverse))
1129 (defun rmail-summary-sort-by-keywords (reverse labels)
1130 "Sort messages of current Rmail summary by keywords.
1131 If prefix argument REVERSE is non-nil, sort them in reverse order.
1132 KEYWORDS is a comma-separated list of labels."
1133 (interactive "P\nsSort by labels: ")
1134 (rmail-sort-from-summary
1135 (function (lambda (reverse)
1136 (rmail-sort-by-keywords reverse labels)))
1137 reverse))
1139 (defun rmail-sort-from-summary (sortfun reverse)
1140 "Sort Rmail messages from Summary buffer and update it after sorting."
1141 (require 'rmailsort)
1142 (let ((selwin (selected-window)))
1143 (unwind-protect
1144 (progn (pop-to-buffer rmail-buffer)
1145 (funcall sortfun reverse))
1146 (select-window selwin))))
1148 ;;; rmailsum.el ends here