1 ;; "RMAIL" mail reader for Emacs.
2 ;; Copyright (C) 1985 Free Software Foundation, Inc.
4 ;; This file is part of GNU Emacs.
6 ;; GNU Emacs is free software; you can redistribute it and/or modify
7 ;; it under the terms of the GNU General Public License as published by
8 ;; the Free Software Foundation; either version 1, or (at your option)
11 ;; GNU Emacs is distributed in the hope that it will be useful,
12 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;; GNU General Public License for more details.
16 ;; You should have received a copy of the GNU General Public License
17 ;; along with GNU Emacs; see the file COPYING. If not, write to
18 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23 (defun rmail-summary ()
24 "Display a summary of all messages, one line per message."
26 (rmail-new-summary "All" nil))
28 (defun rmail-summary-by-labels (labels)
29 "Display a summary of all messages with one or more LABELS.
30 LABELS should be a string containing the desired labels, separated by commas."
31 (interactive "sLabels to summarize by: ")
32 (if (string= labels "")
33 (setq labels (or rmail-last-multi-labels
34 (error "No label specified"))))
35 (setq rmail-last-multi-labels labels)
36 (rmail-new-summary (concat "labels " labels)
37 'rmail-message-labels-p
38 (concat ", \\(" (mail-comma-list-regexp labels) "\\),")))
40 (defun rmail-summary-by-recipients (recipients &optional primary-only)
41 "Display a summary of all messages with the given RECIPIENTS.
42 Normally checks the To, From and Cc fields of headers;
43 but if PRIMARY-ONLY is non-nil (prefix arg given),
44 only look in the To and From fields.
45 RECIPIENTS is a string of names separated by commas."
46 (interactive "sRecipients to summarize by: \nP")
48 (concat "recipients " recipients)
49 'rmail-message-recipients-p
50 (mail-comma-list-regexp recipients) primary-only))
52 (defun rmail-message-recipients-p (msg recipients &optional primary-only)
54 (goto-char (rmail-msgbeg msg))
55 (search-forward "\n*** EOOH ***\n")
56 (narrow-to-region (point) (progn (search-forward "\n\n") (point)))
57 (or (string-match recipients (or (mail-fetch-field "To") ""))
58 (string-match recipients (or (mail-fetch-field "From") ""))
59 (if (not primary-only)
60 (string-match recipients (or (mail-fetch-field "Cc") ""))))))
62 (defun rmail-summary-by-regexp (regexp)
63 "Display a summary of all messages according to regexp REGEXP.
64 If the regular expression is found in the header of the message
65 \(including in the date and other lines, as well as the subject line),
66 Emacs will list the header line in the RMAIL-summary."
67 (interactive "sRegexp to summarize by: ")
68 (if (string= regexp "")
69 (setq regexp (or rmail-last-regexp
70 (error "No regexp specified"))))
71 (setq rmail-last-regexp regexp)
72 (rmail-new-summary (concat "regexp " regexp)
73 'rmail-message-regexp-p
76 (defun rmail-message-regexp-p (msg regexp)
77 "Return t, if for message number MSG, regexp REGEXP matches in the header."
78 (goto-char (rmail-msgbeg msg))
81 (search-forward "*** EOOH ***" (point-max)) (point))))
82 (re-search-forward regexp end t)))
84 (defun rmail-new-summary (description function &rest args)
85 "Create a summary of selected messages.
86 DESCRIPTION makes part of the mode line of the summary buffer.
87 For each message, FUNCTION is applied to the message number and ARGS...
88 and if the result is non-nil, that message is included.
89 nil for FUNCTION means all messages."
90 (message "Computing summary lines...")
91 (or (and rmail-summary-buffer
92 (buffer-name rmail-summary-buffer))
93 (setq rmail-summary-buffer
94 (generate-new-buffer (concat (buffer-name) "-summary"))))
95 (let ((summary-msgs ())
96 (new-summary-line-count 0))
98 (buffer-read-only nil))
102 (goto-char (point-min))
103 (while (>= rmail-total-messages msgnum)
104 (if (or (null function)
105 (apply function (cons msgnum args)))
107 (cons (rmail-make-summary-line msgnum)
109 (setq msgnum (1+ msgnum))))))
110 (let ((sbuf rmail-summary-buffer)
111 (rbuf (current-buffer))
112 (total rmail-total-messages)
113 (mesg rmail-current-message))
115 ;; Our scroll command should always scroll the Rmail buffer.
116 (make-local-variable 'other-window-scroll-buffer)
117 (setq other-window-scroll-buffer rbuf)
118 (let ((buffer-read-only nil))
121 (princ (nreverse summary-msgs) sbuf)
123 (subst-char-in-region 1 2 ?\( ?\ ))))
124 (setq buffer-read-only t)
125 (goto-char (point-min))
127 (make-local-variable 'minor-mode-alist)
128 (setq minor-mode-alist (list ": " description))
129 (setq rmail-buffer rbuf
130 rmail-total-messages total)
131 (rmail-summary-goto-msg mesg t)))
132 (message "Computing summary lines...done"))
134 (defun rmail-make-summary-line (msg)
135 (let ((line (or (aref rmail-summary-vector (1- msg))
137 (setq new-summary-line-count
138 (1+ new-summary-line-count))
139 (if (zerop (% new-summary-line-count 10))
140 (message "Computing summary lines...%d"
141 new-summary-line-count))
142 (rmail-make-summary-line-1 msg)))))
143 ;; Fix up the part of the summary that says "deleted" or "unseen".
145 (if (rmail-message-deleted-p msg) ?\D
146 (if (= ?0 (char-after (+ 3 (rmail-msgbeg msg))))
150 (defun rmail-make-summary-line-1 (msg)
151 (goto-char (rmail-msgbeg msg))
152 (let* ((lim (save-excursion (forward-line 2) (point)))
158 ; (if (save-excursion (re-search-forward ",answered," lim t))
160 ; (if (save-excursion (re-search-forward ",filed," lim t))
162 (if (progn (search-forward ",,") (eolp))
165 (buffer-substring (point)
166 (progn (end-of-line) (point)))
171 (if (looking-at "Summary-line: ")
173 (goto-char (match-end 0))
175 (buffer-substring (point)
176 (progn (forward-line 1) (point)))))))))
177 ;; Obsolete status lines lacking a # should be flushed.
179 (not (string-match "#" line))
181 (delete-region (point)
182 (progn (forward-line -1) (point)))
184 ;; If we didn't get a valid status line from the message,
185 ;; make a new one and put it in the message.
187 (let* ((case-fold-search t)
188 (next (rmail-msgend msg))
189 (beg (if (progn (goto-char (rmail-msgbeg msg))
190 (search-forward "\n*** EOOH ***\n" next t))
194 (end (progn (search-forward "\n\n" nil t) (point))))
196 (narrow-to-region beg end)
198 (setq line (rmail-make-basic-summary-line)))
199 (goto-char (rmail-msgbeg msg))
201 (insert "Summary-line: " line)))
202 (setq pos (string-match "#" line))
203 (aset rmail-summary-vector (1- msg)
204 (concat (format "%4d " msg)
205 (substring line 0 pos)
207 (substring line (1+ pos))))))
209 (defun rmail-make-basic-summary-line ()
210 (goto-char (point-min))
211 (concat (save-excursion
212 (if (not (re-search-forward "^Date:" nil t))
214 (cond ((re-search-forward "\\([^0-9:]\\)\\([0-3]?[0-9]\\)\\([- \t_]+\\)\\([adfjmnos][aceopu][bcglnprtvy]\\)"
215 (save-excursion (end-of-line) (point)) t)
217 (string-to-int (buffer-substring
221 (match-beginning 4) (match-end 4))))
222 ((re-search-forward "\\([^a-z]\\)\\([adfjmnos][acepou][bcglnprtvy]\\)\\([-a-z \t_]*\\)\\([0-9][0-9]?\\)"
223 (save-excursion (end-of-line) (point)) t)
225 (string-to-int (buffer-substring
229 (match-beginning 2) (match-end 2))))
233 (if (not (re-search-forward "^From:[ \t]*" nil t))
235 (let* ((from (mail-strip-quoted-names
239 (skip-chars-backward " \t")
242 (if (string-match (concat "^"
243 (regexp-quote (user-login-name))
247 (goto-char (point-min))
248 (if (not (re-search-forward "^To:[ \t]*" nil t))
252 (mail-strip-quoted-names
256 (skip-chars-backward " \t")
258 (setq len (length from))
259 (setq mch (string-match "[@%]" from))
261 (if (or (not mch) (<= len 25))
262 (substring from (max 0 (- len 25)))
264 (setq lo (cond ((< (- mch 9) 0) 0)
268 (min len (+ lo 25))))))))
270 (if (re-search-forward "^Subject:" nil t)
271 (progn (skip-chars-forward " \t")
272 (buffer-substring (point)
275 (re-search-forward "[\n][\n]+" nil t)
276 (buffer-substring (point) (progn (end-of-line) (point))))
279 (defun rmail-summary-next-all (&optional number)
281 (forward-line (if number number 1))
282 (rmail-summary-goto-msg))
284 (defun rmail-summary-previous-all (&optional number)
286 (forward-line (- (if number number 1)))
287 (rmail-summary-goto-msg))
289 (defun rmail-summary-next-msg (&optional number)
292 (and (> number 0) (forward-line 1))
293 (let ((count (if (< number 0) (- number) number))
294 (search (if (> number 0) 're-search-forward 're-search-backward))
296 (while (and (> count 0) (funcall search "^.....[^D]" nil t))
297 (setq count (1- count)))
298 (rmail-summary-goto-msg)))
300 (defun rmail-summary-previous-msg (&optional number)
302 (rmail-summary-next-msg (- (if number number 1))))
304 (defun rmail-summary-delete-forward ()
307 (rmail-summary-goto-msg)
308 (pop-to-buffer rmail-buffer)
309 (rmail-delete-message)
310 (pop-to-buffer rmail-summary-buffer)
311 (let ((buffer-read-only nil))
312 (skip-chars-forward " ")
313 (skip-chars-forward "[0-9]")
316 (rmail-summary-next-msg 1)))
318 (defun rmail-summary-delete-backward ()
321 (rmail-summary-goto-msg)
322 (pop-to-buffer rmail-buffer)
323 (rmail-delete-message)
324 (pop-to-buffer rmail-summary-buffer)
325 (let ((buffer-read-only nil))
326 (skip-chars-forward " ")
327 (skip-chars-forward "[0-9]")
330 (rmail-summary-next-msg -1)))
332 (defun rmail-summary-undelete ()
334 (let ((buffer-read-only nil))
336 (cond ((re-search-backward "\\(^ *[0-9]*\\)\\(D\\)" nil t)
337 (replace-match "\\1 ")
338 (rmail-summary-goto-msg)
339 (pop-to-buffer rmail-buffer)
340 (and (rmail-message-deleted-p rmail-current-message)
341 (rmail-undelete-previous-message))
342 (pop-to-buffer rmail-summary-buffer))
344 (rmail-summary-goto-msg)))))
346 ;; Rmail Summary mode is suitable only for specially formatted data.
347 (put 'rmail-summary-mode 'mode-class 'special)
349 (defun rmail-summary-mode ()
350 "Major mode in effect in Rmail summary buffer.
351 A subset of the Rmail mode commands are supported in this mode.
352 As commands are issued in the summary buffer the corresponding
353 mail message is displayed in the rmail buffer.
355 n Move to next undeleted message, or arg messages.
356 p Move to previous undeleted message, or arg messages.
357 M-n Move to next, or forward arg messages.
358 M-p Move to previous, or previous arg messages.
359 j Jump to the message at the cursor location.
360 d Delete the message at the cursor location and move to next message.
361 C-d Delete the message at the cursor location and move to previous message.
362 u Undelete this or previous deleted message.
364 x Exit and kill the summary window.
365 space Scroll message in other window forward.
366 delete Scroll message backward.
368 Entering this mode calls value of hook variable rmail-summary-mode-hook."
370 (kill-all-local-variables)
371 (make-local-variable 'rmail-buffer)
372 (make-local-variable 'rmail-total-messages)
373 (setq major-mode 'rmail-summary-mode)
374 (setq mode-name "RMAIL Summary")
375 (use-local-map rmail-summary-mode-map)
376 (setq truncate-lines t)
377 (setq buffer-read-only t)
378 (set-syntax-table text-mode-syntax-table)
379 (run-hooks 'rmail-summary-mode-hook))
381 (defun rmail-summary-goto-msg (&optional n nowarn)
383 (if (consp n) (setq n (prefix-numeric-value n)))
384 (if (eobp) (forward-line -1))
386 (let ((buf rmail-buffer)
388 (curmsg (string-to-int
389 (buffer-substring (point)
390 (min (point-max) (+ 5 (point)))))))
394 (progn (message "No preceding message")
396 (if (> n rmail-total-messages)
397 (progn (message "No following message")
398 (goto-char (point-max))
399 (rmail-summary-goto-msg)))
400 (goto-char (point-min))
401 (if (not (re-search-forward (concat "^ *" (int-to-string n)) nil t))
402 (progn (or nowarn (message "Message %d not found" n))
406 (skip-chars-forward " ")
407 (skip-chars-forward "0-9")
408 (save-excursion (if (= (following-char) ?-)
409 (let ((buffer-read-only nil))
414 (rmail-show-message n)
415 (pop-to-buffer rmail-summary-buffer)))
417 (defvar rmail-summary-mode-map nil)
419 (if rmail-summary-mode-map
421 (setq rmail-summary-mode-map (make-keymap))
422 (suppress-keymap rmail-summary-mode-map)
423 (define-key rmail-summary-mode-map "j" 'rmail-summary-goto-msg)
424 (define-key rmail-summary-mode-map "n" 'rmail-summary-next-msg)
425 (define-key rmail-summary-mode-map "p" 'rmail-summary-previous-msg)
426 (define-key rmail-summary-mode-map "\en" 'rmail-summary-next-all)
427 (define-key rmail-summary-mode-map "\ep" 'rmail-summary-previous-all)
428 (define-key rmail-summary-mode-map " " 'rmail-summary-scroll-msg-up)
429 (define-key rmail-summary-mode-map "q" 'rmail-summary-quit)
430 (define-key rmail-summary-mode-map "u" 'rmail-summary-undelete)
431 (define-key rmail-summary-mode-map "x" 'rmail-summary-exit)
432 (define-key rmail-summary-mode-map "\177" 'rmail-summary-scroll-msg-down)
433 (define-key rmail-summary-mode-map "d" 'rmail-summary-delete-forward)
434 (define-key rmail-summary-mode-map "\C-d" 'rmail-summary-delete-backward))
436 (defun rmail-summary-scroll-msg-up (&optional dist)
437 "Scroll other window forward."
439 (let ((window (selected-window))
440 (new-window (display-buffer rmail-buffer)))
443 (select-window new-window)
445 (select-window window))))
447 (defun rmail-summary-scroll-msg-down (&optional dist)
448 "Scroll other window backward."
450 (let ((window (selected-window))
451 (new-window (display-buffer rmail-buffer)))
454 (select-window new-window)
456 (select-window window))))
458 (defun rmail-summary-quit ()
459 "Quit out of rmail and rmail summary."
464 (defun rmail-summary-exit ()
465 "Exit rmail summary, remaining within rmail."
467 (bury-buffer (current-buffer))
468 (if (get-buffer-window rmail-buffer)
469 ;; Select the window with rmail in it, then delete this window.
470 (select-window (prog1
471 (get-buffer-window rmail-buffer)
472 (delete-window (selected-window))))
473 ;; Switch to the rmail buffer in this window.
474 (switch-to-buffer rmail-buffer)))