*** empty log message ***
[emacs.git] / lisp / mail / rmailsum.el
blob12f6cf527a380247292aeef788c74353f4e718b4
1 ;;; rmailsum.el --- "RMAIL" mail reader for Emacs.
3 ;; Copyright (C) 1985 Free Software Foundation, Inc.
5 ;; This file is part of GNU Emacs.
7 ;; GNU Emacs is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation; either version 1, or (at your option)
10 ;; any later version.
12 ;; GNU Emacs is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs; see the file COPYING. If not, write to
19 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
22 ;; summary things
24 (defun rmail-summary ()
25 "Display a summary of all messages, one line per message."
26 (interactive)
27 (rmail-new-summary "All" nil))
29 (defun rmail-summary-by-labels (labels)
30 "Display a summary of all messages with one or more LABELS.
31 LABELS should be a string containing the desired labels, separated by commas."
32 (interactive "sLabels to summarize by: ")
33 (if (string= labels "")
34 (setq labels (or rmail-last-multi-labels
35 (error "No label specified"))))
36 (setq rmail-last-multi-labels labels)
37 (rmail-new-summary (concat "labels " labels)
38 'rmail-message-labels-p
39 (concat ", \\(" (mail-comma-list-regexp labels) "\\),")))
41 (defun rmail-summary-by-recipients (recipients &optional primary-only)
42 "Display a summary of all messages with the given RECIPIENTS.
43 Normally checks the To, From and Cc fields of headers;
44 but if PRIMARY-ONLY is non-nil (prefix arg given),
45 only look in the To and From fields.
46 RECIPIENTS is a string of names separated by commas."
47 (interactive "sRecipients to summarize by: \nP")
48 (rmail-new-summary
49 (concat "recipients " recipients)
50 'rmail-message-recipients-p
51 (mail-comma-list-regexp recipients) primary-only))
53 (defun rmail-message-recipients-p (msg recipients &optional primary-only)
54 (save-restriction
55 (goto-char (rmail-msgbeg msg))
56 (search-forward "\n*** EOOH ***\n")
57 (narrow-to-region (point) (progn (search-forward "\n\n") (point)))
58 (or (string-match recipients (or (mail-fetch-field "To") ""))
59 (string-match recipients (or (mail-fetch-field "From") ""))
60 (if (not primary-only)
61 (string-match recipients (or (mail-fetch-field "Cc") ""))))))
63 (defun rmail-summary-by-regexp (regexp)
64 "Display a summary of all messages according to regexp REGEXP.
65 If the regular expression is found in the header of the message
66 \(including in the date and other lines, as well as the subject line),
67 Emacs will list the header line in the RMAIL-summary."
68 (interactive "sRegexp to summarize by: ")
69 (if (string= regexp "")
70 (setq regexp (or rmail-last-regexp
71 (error "No regexp specified"))))
72 (setq rmail-last-regexp regexp)
73 (rmail-new-summary (concat "regexp " regexp)
74 'rmail-message-regexp-p
75 regexp))
77 (defun rmail-message-regexp-p (msg regexp)
78 "Return t, if for message number MSG, regexp REGEXP matches in the header."
79 (goto-char (rmail-msgbeg msg))
80 (let ((end
81 (save-excursion
82 (search-forward "*** EOOH ***" (point-max)) (point))))
83 (re-search-forward regexp end t)))
85 (defun rmail-new-summary (description function &rest args)
86 "Create a summary of selected messages.
87 DESCRIPTION makes part of the mode line of the summary buffer.
88 For each message, FUNCTION is applied to the message number and ARGS...
89 and if the result is non-nil, that message is included.
90 nil for FUNCTION means all messages."
91 (message "Computing summary lines...")
92 (or (and rmail-summary-buffer
93 (buffer-name rmail-summary-buffer))
94 (setq rmail-summary-buffer
95 (generate-new-buffer (concat (buffer-name) "-summary"))))
96 (let ((summary-msgs ())
97 (new-summary-line-count 0))
98 (let ((msgnum 1)
99 (buffer-read-only nil))
100 (save-restriction
101 (save-excursion
102 (widen)
103 (goto-char (point-min))
104 (while (>= rmail-total-messages msgnum)
105 (if (or (null function)
106 (apply function (cons msgnum args)))
107 (setq summary-msgs
108 (cons (rmail-make-summary-line msgnum)
109 summary-msgs)))
110 (setq msgnum (1+ msgnum))))))
111 (let ((sbuf rmail-summary-buffer)
112 (rbuf (current-buffer))
113 (total rmail-total-messages)
114 (mesg rmail-current-message))
115 (pop-to-buffer sbuf)
116 ;; Our scroll command should always scroll the Rmail buffer.
117 (make-local-variable 'other-window-scroll-buffer)
118 (setq other-window-scroll-buffer rbuf)
119 (let ((buffer-read-only nil))
120 (erase-buffer)
121 (cond (summary-msgs
122 (princ (nreverse summary-msgs) sbuf)
123 (delete-char -1)
124 (subst-char-in-region 1 2 ?\( ?\ ))))
125 (setq buffer-read-only t)
126 (goto-char (point-min))
127 (rmail-summary-mode)
128 (make-local-variable 'minor-mode-alist)
129 (setq minor-mode-alist (list ": " description))
130 (setq rmail-buffer rbuf
131 rmail-total-messages total)
132 (rmail-summary-goto-msg mesg t)))
133 (message "Computing summary lines...done"))
135 (defun rmail-make-summary-line (msg)
136 (let ((line (or (aref rmail-summary-vector (1- msg))
137 (progn
138 (setq new-summary-line-count
139 (1+ new-summary-line-count))
140 (if (zerop (% new-summary-line-count 10))
141 (message "Computing summary lines...%d"
142 new-summary-line-count))
143 (rmail-make-summary-line-1 msg)))))
144 ;; Fix up the part of the summary that says "deleted" or "unseen".
145 (aset line 4
146 (if (rmail-message-deleted-p msg) ?\D
147 (if (= ?0 (char-after (+ 3 (rmail-msgbeg msg))))
148 ?\- ?\ )))
149 line))
151 (defun rmail-make-summary-line-1 (msg)
152 (goto-char (rmail-msgbeg msg))
153 (let* ((lim (save-excursion (forward-line 2) (point)))
155 (labels
156 (progn
157 (forward-char 3)
158 (concat
159 ; (if (save-excursion (re-search-forward ",answered," lim t))
160 ; "*" "")
161 ; (if (save-excursion (re-search-forward ",filed," lim t))
162 ; "!" "")
163 (if (progn (search-forward ",,") (eolp))
165 (concat "{"
166 (buffer-substring (point)
167 (progn (end-of-line) (point)))
168 "} ")))))
169 (line
170 (progn
171 (forward-line 1)
172 (if (looking-at "Summary-line: ")
173 (progn
174 (goto-char (match-end 0))
175 (setq line
176 (buffer-substring (point)
177 (progn (forward-line 1) (point)))))))))
178 ;; Obsolete status lines lacking a # should be flushed.
179 (and line
180 (not (string-match "#" line))
181 (progn
182 (delete-region (point)
183 (progn (forward-line -1) (point)))
184 (setq line nil)))
185 ;; If we didn't get a valid status line from the message,
186 ;; make a new one and put it in the message.
187 (or line
188 (let* ((case-fold-search t)
189 (next (rmail-msgend msg))
190 (beg (if (progn (goto-char (rmail-msgbeg msg))
191 (search-forward "\n*** EOOH ***\n" next t))
192 (point)
193 (forward-line 1)
194 (point)))
195 (end (progn (search-forward "\n\n" nil t) (point))))
196 (save-restriction
197 (narrow-to-region beg end)
198 (goto-char beg)
199 (setq line (rmail-make-basic-summary-line)))
200 (goto-char (rmail-msgbeg msg))
201 (forward-line 2)
202 (insert "Summary-line: " line)))
203 (setq pos (string-match "#" line))
204 (aset rmail-summary-vector (1- msg)
205 (concat (format "%4d " msg)
206 (substring line 0 pos)
207 labels
208 (substring line (1+ pos))))))
210 (defun rmail-make-basic-summary-line ()
211 (goto-char (point-min))
212 (concat (save-excursion
213 (if (not (re-search-forward "^Date:" nil t))
215 (cond ((re-search-forward "\\([^0-9:]\\)\\([0-3]?[0-9]\\)\\([- \t_]+\\)\\([adfjmnos][aceopu][bcglnprtvy]\\)"
216 (save-excursion (end-of-line) (point)) t)
217 (format "%2d-%3s"
218 (string-to-int (buffer-substring
219 (match-beginning 2)
220 (match-end 2)))
221 (buffer-substring
222 (match-beginning 4) (match-end 4))))
223 ((re-search-forward "\\([^a-z]\\)\\([adfjmnos][acepou][bcglnprtvy]\\)\\([-a-z \t_]*\\)\\([0-9][0-9]?\\)"
224 (save-excursion (end-of-line) (point)) t)
225 (format "%2d-%3s"
226 (string-to-int (buffer-substring
227 (match-beginning 4)
228 (match-end 4)))
229 (buffer-substring
230 (match-beginning 2) (match-end 2))))
231 (t "??????"))))
233 (save-excursion
234 (if (not (re-search-forward "^From:[ \t]*" nil t))
236 (let* ((from (mail-strip-quoted-names
237 (buffer-substring
238 (1- (point))
239 (progn (end-of-line)
240 (skip-chars-backward " \t")
241 (point)))))
242 len mch lo)
243 (if (string-match (concat "^"
244 (regexp-quote (user-login-name))
245 "\\($\\|@\\)")
246 from)
247 (save-excursion
248 (goto-char (point-min))
249 (if (not (re-search-forward "^To:[ \t]*" nil t))
251 (setq from
252 (concat "to: "
253 (mail-strip-quoted-names
254 (buffer-substring
255 (point)
256 (progn (end-of-line)
257 (skip-chars-backward " \t")
258 (point)))))))))
259 (setq len (length from))
260 (setq mch (string-match "[@%]" from))
261 (format "%25s"
262 (if (or (not mch) (<= len 25))
263 (substring from (max 0 (- len 25)))
264 (substring from
265 (setq lo (cond ((< (- mch 9) 0) 0)
266 ((< len (+ mch 16))
267 (- len 25))
268 (t (- mch 9))))
269 (min len (+ lo 25))))))))
270 " #"
271 (if (re-search-forward "^Subject:" nil t)
272 (progn (skip-chars-forward " \t")
273 (buffer-substring (point)
274 (progn (end-of-line)
275 (point))))
276 (re-search-forward "[\n][\n]+" nil t)
277 (buffer-substring (point) (progn (end-of-line) (point))))
278 "\n"))
280 (defun rmail-summary-next-all (&optional number)
281 (interactive "p")
282 (forward-line (if number number 1))
283 (rmail-summary-goto-msg))
285 (defun rmail-summary-previous-all (&optional number)
286 (interactive "p")
287 (forward-line (- (if number number 1)))
288 (rmail-summary-goto-msg))
290 (defun rmail-summary-next-msg (&optional number)
291 (interactive "p")
292 (forward-line 0)
293 (and (> number 0) (forward-line 1))
294 (let ((count (if (< number 0) (- number) number))
295 (search (if (> number 0) 're-search-forward 're-search-backward))
296 end)
297 (while (and (> count 0) (funcall search "^.....[^D]" nil t))
298 (setq count (1- count)))
299 (rmail-summary-goto-msg)))
301 (defun rmail-summary-previous-msg (&optional number)
302 (interactive "p")
303 (rmail-summary-next-msg (- (if number number 1))))
305 (defun rmail-summary-delete-forward ()
306 (interactive)
307 (let (end)
308 (rmail-summary-goto-msg)
309 (pop-to-buffer rmail-buffer)
310 (rmail-delete-message)
311 (pop-to-buffer rmail-summary-buffer)
312 (let ((buffer-read-only nil))
313 (skip-chars-forward " ")
314 (skip-chars-forward "[0-9]")
315 (delete-char 1)
316 (insert "D"))
317 (rmail-summary-next-msg 1)))
319 (defun rmail-summary-delete-backward ()
320 (interactive)
321 (let (end)
322 (rmail-summary-goto-msg)
323 (pop-to-buffer rmail-buffer)
324 (rmail-delete-message)
325 (pop-to-buffer rmail-summary-buffer)
326 (let ((buffer-read-only nil))
327 (skip-chars-forward " ")
328 (skip-chars-forward "[0-9]")
329 (delete-char 1)
330 (insert "D"))
331 (rmail-summary-next-msg -1)))
333 (defun rmail-summary-undelete ()
334 (interactive)
335 (let ((buffer-read-only nil))
336 (end-of-line)
337 (cond ((re-search-backward "\\(^ *[0-9]*\\)\\(D\\)" nil t)
338 (replace-match "\\1 ")
339 (rmail-summary-goto-msg)
340 (pop-to-buffer rmail-buffer)
341 (and (rmail-message-deleted-p rmail-current-message)
342 (rmail-undelete-previous-message))
343 (pop-to-buffer rmail-summary-buffer))
345 (rmail-summary-goto-msg)))))
347 ;; Rmail Summary mode is suitable only for specially formatted data.
348 (put 'rmail-summary-mode 'mode-class 'special)
350 (defun rmail-summary-mode ()
351 "Major mode in effect in Rmail summary buffer.
352 A subset of the Rmail mode commands are supported in this mode.
353 As commands are issued in the summary buffer the corresponding
354 mail message is displayed in the rmail buffer.
356 n Move to next undeleted message, or arg messages.
357 p Move to previous undeleted message, or arg messages.
358 M-n Move to next, or forward arg messages.
359 M-p Move to previous, or previous arg messages.
360 j Jump to the message at the cursor location.
361 d Delete the message at the cursor location and move to next message.
362 C-d Delete the message at the cursor location and move to previous message.
363 u Undelete this or previous deleted message.
364 q Quit Rmail.
365 x Exit and kill the summary window.
366 space Scroll message in other window forward.
367 delete Scroll message backward.
369 Entering this mode calls value of hook variable rmail-summary-mode-hook."
370 (interactive)
371 (kill-all-local-variables)
372 (make-local-variable 'rmail-buffer)
373 (make-local-variable 'rmail-total-messages)
374 (setq major-mode 'rmail-summary-mode)
375 (setq mode-name "RMAIL Summary")
376 (use-local-map rmail-summary-mode-map)
377 (setq truncate-lines t)
378 (setq buffer-read-only t)
379 (set-syntax-table text-mode-syntax-table)
380 (run-hooks 'rmail-summary-mode-hook))
382 (defun rmail-summary-goto-msg (&optional n nowarn)
383 (interactive "P")
384 (if (consp n) (setq n (prefix-numeric-value n)))
385 (if (eobp) (forward-line -1))
386 (beginning-of-line)
387 (let ((buf rmail-buffer)
388 (cur (point))
389 (curmsg (string-to-int
390 (buffer-substring (point)
391 (min (point-max) (+ 5 (point)))))))
392 (if (not n)
393 (setq n curmsg)
394 (if (< n 1)
395 (progn (message "No preceding message")
396 (setq n 1)))
397 (if (> n rmail-total-messages)
398 (progn (message "No following message")
399 (goto-char (point-max))
400 (rmail-summary-goto-msg)))
401 (goto-char (point-min))
402 (if (not (re-search-forward (concat "^ *" (int-to-string n)) nil t))
403 (progn (or nowarn (message "Message %d not found" n))
404 (setq n curmsg)
405 (goto-char cur))))
406 (beginning-of-line)
407 (skip-chars-forward " ")
408 (skip-chars-forward "0-9")
409 (save-excursion (if (= (following-char) ?-)
410 (let ((buffer-read-only nil))
411 (delete-char 1)
412 (insert " "))))
413 (beginning-of-line)
414 (pop-to-buffer buf)
415 (rmail-show-message n)
416 (pop-to-buffer rmail-summary-buffer)))
418 (defvar rmail-summary-mode-map nil)
420 (if rmail-summary-mode-map
422 (setq rmail-summary-mode-map (make-keymap))
423 (suppress-keymap rmail-summary-mode-map)
424 (define-key rmail-summary-mode-map "j" 'rmail-summary-goto-msg)
425 (define-key rmail-summary-mode-map "n" 'rmail-summary-next-msg)
426 (define-key rmail-summary-mode-map "p" 'rmail-summary-previous-msg)
427 (define-key rmail-summary-mode-map "\en" 'rmail-summary-next-all)
428 (define-key rmail-summary-mode-map "\ep" 'rmail-summary-previous-all)
429 (define-key rmail-summary-mode-map " " 'rmail-summary-scroll-msg-up)
430 (define-key rmail-summary-mode-map "q" 'rmail-summary-quit)
431 (define-key rmail-summary-mode-map "u" 'rmail-summary-undelete)
432 (define-key rmail-summary-mode-map "x" 'rmail-summary-exit)
433 (define-key rmail-summary-mode-map "\177" 'rmail-summary-scroll-msg-down)
434 (define-key rmail-summary-mode-map "d" 'rmail-summary-delete-forward)
435 (define-key rmail-summary-mode-map "\C-d" 'rmail-summary-delete-backward))
437 (defun rmail-summary-scroll-msg-up (&optional dist)
438 "Scroll other window forward."
439 (interactive "P")
440 (let ((window (selected-window))
441 (new-window (display-buffer rmail-buffer)))
442 (unwind-protect
443 (progn
444 (select-window new-window)
445 (scroll-up dist))
446 (select-window window))))
448 (defun rmail-summary-scroll-msg-down (&optional dist)
449 "Scroll other window backward."
450 (interactive "P")
451 (let ((window (selected-window))
452 (new-window (display-buffer rmail-buffer)))
453 (unwind-protect
454 (progn
455 (select-window new-window)
456 (scroll-down dist))
457 (select-window window))))
459 (defun rmail-summary-quit ()
460 "Quit out of rmail and rmail summary."
461 (interactive)
462 (rmail-summary-exit)
463 (rmail-quit))
465 (defun rmail-summary-exit ()
466 "Exit rmail summary, remaining within rmail."
467 (interactive)
468 (bury-buffer (current-buffer))
469 (if (get-buffer-window rmail-buffer)
470 ;; Select the window with rmail in it, then delete this window.
471 (select-window (prog1
472 (get-buffer-window rmail-buffer)
473 (delete-window (selected-window))))
474 ;; Switch to the rmail buffer in this window.
475 (switch-to-buffer rmail-buffer)))
477 ;;; rmailsum.el ends here