]> code.delx.au - gnu-emacs/blob - lisp/mail/rmail.el
(rmail-get-new-mail): Avoid error if file is empty.
[gnu-emacs] / lisp / mail / rmail.el
1 ;;; rmail.el --- main code of "RMAIL" mail reader for Emacs.
2
3 ;; Copyright (C) 1985, 1986, 1987, 1988, 1993 Free Software Foundation, Inc.
4
5 ;; Maintainer: FSF
6 ;; Keywords: mail
7
8 ;; This file is part of GNU Emacs.
9
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.
14
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.
19
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.
23
24 ;;; Code:
25
26 ;; Souped up by shane@mit-ajax based on ideas of rlk@athena.mit.edu
27 ;; New features include attribute and keyword support, message
28 ;; selection by dispatch table, summary by attributes and keywords,
29 ;; expunging by dispatch table, sticky options for file commands.
30
31 ;; Extended by Bob Weiner of Motorola
32 ;; New features include: rmail and rmail-summary buffers remain
33 ;; synchronized and key bindings basically operate the same way in both
34 ;; buffers, summary by topic or by regular expression, rmail-reply-prefix
35 ;; variable, and a bury rmail buffer (wipe) command.
36 ;;
37
38 (require 'mail-utils)
39
40 ;; For Emacs V18 compatibility
41 (and (not (fboundp 'user-original-login-name))
42 (fboundp 'user-real-login-name)
43 (defalias 'user-original-login-name 'user-real-login-name))
44 (and (not (fboundp 'buffer-disable-undo))
45 (fboundp 'buffer-flush-undo)
46 (defalias 'buffer-disable-undo 'buffer-flush-undo))
47
48 ; These variables now declared in paths.el.
49 ;(defvar rmail-spool-directory "/usr/spool/mail/"
50 ; "This is the name of the directory used by the system mailer for\n\
51 ;delivering new mail. It's name should end with a slash.")
52 ;(defvar rmail-file-name
53 ; (expand-file-name "~/RMAIL")
54 ; "")
55
56 ;;;###autoload
57 (defvar rmail-dont-reply-to-names nil "\
58 *A regexp specifying names to prune of reply to messages.
59 A value of nil means exclude your own name only.")
60
61 ;;;###autoload
62 (defvar rmail-default-dont-reply-to-names "info-" "\
63 A regular expression specifying part of the value of the default value of
64 the variable `rmail-dont-reply-to-names', for when the user does not set
65 `rmail-dont-reply-to-names' explicitly. (The other part of the default
66 value is the user's name.)
67 It is useful to set this variable in the site customization file.")
68
69 ;;;###autoload
70 (defvar rmail-ignored-headers "^via:\\|^mail-from:\\|^origin:\\|^status:\\|^received:\\|^message-id:\\|^summary-line:" "\
71 *Gubbish headers one would rather not see.")
72
73 ;;;###autoload
74 (defvar rmail-delete-after-output nil "\
75 *Non-nil means automatically delete a message that is copied to a file.")
76
77 ;;;###autoload
78 (defvar rmail-primary-inbox-list nil "\
79 *List of files which are inboxes for user's primary mail file `~/RMAIL'.
80 `nil' means the default, which is (\"/usr/spool/mail/$USER\")
81 \(the name varies depending on the operating system,
82 and the value of the environment variable MAIL overrides it).")
83
84 ;;;###autoload
85 (defvar rmail-mail-new-frame nil
86 "*Non-nil means Rmail makes a new frame for composing outgoing mail.")
87
88 ;;;###autoload
89 (defvar rmail-retry-setup-hook nil
90 "Hook that `rmail-retry-failure' uses in place of `mail-setup-hook'.")
91
92 ;; These may be altered by site-init.el to match the format of mmdf files
93 ;; delimiting used on a given host (delim1 and delim2 from the config
94 ;; files).
95
96 (defvar mmdf-delim1 "^\001\001\001\001\n"
97 "Regexp marking the start of an mmdf message")
98 (defvar mmdf-delim2 "^\001\001\001\001\n"
99 "Regexp marking the end of an mmdf message")
100
101 (defvar rmail-message-filter nil
102 "If non nil, is a filter function for new headers in RMAIL.
103 Called with region narrowed to unformatted header.")
104
105 (defvar rmail-reply-prefix "Re: "
106 "String to prepend to Subject line when replying to a message.")
107
108 (defvar rmail-mode-map nil)
109
110 (defvar rmail-inbox-list nil)
111 (defvar rmail-keywords nil)
112
113 ;; Message counters and markers. Deleted flags.
114
115 (defvar rmail-current-message nil)
116 (defvar rmail-total-messages nil)
117 (defvar rmail-message-vector nil)
118 (defvar rmail-deleted-vector nil)
119
120 ;; These are used by autoloaded rmail-summary.
121
122 (defvar rmail-summary-buffer nil)
123 (defvar rmail-summary-vector nil)
124
125 ;; `Sticky' default variables.
126
127 ;; Last individual label specified to a or k.
128 (defvar rmail-last-label nil)
129 ;; Last set of values specified to C-M-n, C-M-p, C-M-s or C-M-l.
130 (defvar rmail-last-multi-labels nil)
131 (defvar rmail-last-regexp nil)
132 (defvar rmail-default-file nil
133 "*Default file name for \\[rmail-output].")
134 (defvar rmail-default-rmail-file (expand-file-name "~/XMAIL")
135 "*Default file name for \\[rmail-output-to-rmail-file].")
136
137 ;;; Regexp matching the delimiter of messages in UNIX mail format
138 ;;; (UNIX From lines), minus the initial ^. Note that if you change
139 ;;; this expression, you must change the code in rmail-nuke-pinhead-header
140 ;;; that knows the exact ordering of the \\( \\) subexpressions.
141 (defvar rmail-unix-mail-delimiter
142 (let ((time-zone-regexp
143 (concat "\\([A-Z]?[A-Z][A-Z][A-Z]\\( DST\\)?"
144 "\\|[-+]?[0-9][0-9][0-9][0-9]"
145 "\\|"
146 "\\) *")))
147 (concat
148 "From "
149
150 ;; Username, perhaps with a quoted section that can contain spaces.
151 "\\("
152 "[^ \n]*"
153 "\\(\\|\".*\"[^ \n]*\\)"
154 "\\) ?"
155
156 ;; The time the message was sent.
157 "\\([^ \n]*\\) *" ; day of the week
158 "\\([^ ]*\\) *" ; month
159 "\\([0-9]*\\) *" ; day of month
160 "\\([0-9:]*\\) *" ; time of day
161
162 ;; Perhaps a time zone, specified by an abbreviation, or by a
163 ;; numeric offset.
164 time-zone-regexp
165
166 ;; The year.
167 " [0-9][0-9]\\([0-9]*\\) *"
168
169 ;; On some systems the time zone can appear after the year, too.
170 time-zone-regexp
171
172 ;; I'm not sure what this is.
173 "\\(remote from [^\n]*\\)?"
174
175 "\n"))
176 nil)
177
178 ;; Perform BODY in the summary buffer
179 ;; in such a way that its cursor is properly updated in its own window.
180 (defmacro rmail-select-summary (&rest body)
181 (` (progn (if (rmail-summary-displayed)
182 (let ((window (selected-window)))
183 (save-excursion
184 (unwind-protect
185 (progn
186 (pop-to-buffer rmail-summary-buffer)
187 (,@ body))
188 (select-window window))))
189 (save-excursion
190 (set-buffer rmail-summary-buffer)
191 (progn (,@ body))))
192 (rmail-maybe-display-summary))))
193 \f
194 ;;;; *** Rmail Mode ***
195
196 ;;;###autoload
197 (defun rmail (&optional file-name-arg)
198 "Read and edit incoming mail.
199 Moves messages into file named by `rmail-file-name' (a babyl format file)
200 and edits that file in RMAIL Mode.
201 Type \\[describe-mode] once editing that file, for a list of RMAIL commands.
202
203 May be called with file name as argument; then performs rmail editing on
204 that file, but does not copy any new mail into the file.
205 Interactively, if you supply a prefix argument, then you
206 have a chance to specify a file name with the minibuffer."
207 (interactive (if current-prefix-arg
208 (list (read-file-name "Run rmail on RMAIL file: "
209 nil nil t))))
210 (or rmail-default-file
211 (setq rmail-default-file (expand-file-name "~/xmail")))
212 (let* ((file-name (expand-file-name (or file-name-arg rmail-file-name)))
213 (existed (get-file-buffer file-name)))
214 ;; Like find-file, but in the case where a buffer existed
215 ;; and the file was reverted, recompute the message-data.
216 (if (and existed (not (verify-visited-file-modtime existed)))
217 (progn
218 ;; Don't be confused by apparent local-variables spec
219 ;; in the last message in the RMAIL file.
220 (let ((enable-local-variables nil))
221 (find-file file-name))
222 (if (and (verify-visited-file-modtime existed)
223 (eq major-mode 'rmail-mode))
224 (progn (rmail-forget-messages)
225 (rmail-set-message-counters))))
226 (let ((enable-local-variables nil))
227 (find-file file-name)))
228 (if (eq major-mode 'rmail-edit-mode)
229 (error "Exit Rmail Edit mode before getting new mail."))
230 (if (and existed (> (buffer-size) 0))
231 ;; Buffer not new and not empty; ensure in proper mode, but that's all.
232 (or (eq major-mode 'rmail-mode)
233 (rmail-mode-2))
234 (rmail-mode-2)
235 ;; Convert all or part to Babyl file if possible.
236 (rmail-convert-file)
237 (goto-char (point-max))
238 (if (null rmail-inbox-list)
239 (progn
240 (rmail-set-message-counters)
241 (rmail-show-message))))
242 (let ((existing-unseen (rmail-first-unseen-message)))
243 (or file-name-arg
244 (rmail-get-new-mail))
245 ;; Show the first unseen message, which might be from a previous session
246 ;; or might have been just read in by rmail-get-new-mail. Must
247 ;; determine already unseen messages first, as rmail-get-new-mail
248 ;; positions on the first new message, thus marking it as seen.
249 (rmail-show-message existing-unseen))))
250
251 ;; Given the value of MAILPATH, return a list of inbox file names.
252 ;; This is turned off because it is not clear that the user wants
253 ;; all these inboxes to feed into the primary rmail file.
254 ; (defun rmail-convert-mailpath (string)
255 ; (let (idx list)
256 ; (while (setq idx (string-match "[%:]" string))
257 ; (let ((this (substring string 0 idx)))
258 ; (setq string (substring string (1+ idx)))
259 ; (setq list (cons (if (string-match "%" this)
260 ; (substring this 0 (string-match "%" this))
261 ; this)
262 ; list))))
263 ; list))
264
265 ; I have checked that adding "-*- rmail -*-" to the BABYL OPTIONS line
266 ; will not cause emacs 18.55 problems.
267
268 (defun rmail-convert-file ()
269 (let (convert)
270 (widen)
271 (goto-char (point-min))
272 ;; If file doesn't start like a Babyl file,
273 ;; convert it to one, by adding a header and converting each message.
274 (cond ((looking-at "BABYL OPTIONS:"))
275 ((looking-at "Version: 5\n")
276 ;; Losing babyl file made by old version of Rmail.
277 ;; Just fix the babyl file header; don't make a new one,
278 ;; so we don't lose the Labels: file attribute, etc.
279 (let ((buffer-read-only nil))
280 (insert "BABYL OPTIONS: -*- rmail -*-\n")))
281 ((equal (point-min) (point-max))
282 ;; Empty RMAIL file. Just insert the header.
283 (rmail-insert-rmail-file-header))
284 (t
285 ;; Non-empty file in non-RMAIL format. Add header and convert.
286 (setq convert t)
287 (rmail-insert-rmail-file-header)))
288 ;; If file was not a Babyl file or if there are
289 ;; Unix format messages added at the end,
290 ;; convert file as necessary.
291 (if (or convert
292 (save-excursion
293 (goto-char (point-max))
294 (search-backward "\^_")
295 (forward-char 1)
296 (looking-at "\n*From ")))
297 (let ((buffer-read-only nil))
298 (message "Converting to Babyl format...")
299 ;; If file needs conversion, convert it all,
300 ;; except for the BABYL header.
301 ;; (rmail-convert-to-babyl-format would delete the header.)
302 (goto-char (point-min))
303 (search-forward "\n\^_" nil t)
304 (narrow-to-region (point) (point-max))
305 (rmail-convert-to-babyl-format)
306 (message "Converting to Babyl format...done")))))
307
308 ;;; I have checked that adding "-*- rmail -*-" to the BABYL OPTIONS line
309 ;;; will not cause emacs 18.55 problems.
310
311 (defun rmail-insert-rmail-file-header ()
312 (let ((buffer-read-only nil))
313 (insert "BABYL OPTIONS: -*- rmail -*-
314 Version: 5
315 Labels:
316 Note: This is the header of an rmail file.
317 Note: If you are seeing it in rmail,
318 Note: it means the file has no messages in it.\n\^_")))
319
320 (if rmail-mode-map
321 nil
322 (setq rmail-mode-map (make-keymap))
323 (suppress-keymap rmail-mode-map)
324 (define-key rmail-mode-map "a" 'rmail-add-label)
325 (define-key rmail-mode-map "b" 'rmail-bury)
326 (define-key rmail-mode-map "c" 'rmail-continue)
327 (define-key rmail-mode-map "d" 'rmail-delete-forward)
328 (define-key rmail-mode-map "\C-d" 'rmail-delete-backward)
329 (define-key rmail-mode-map "e" 'rmail-edit-current-message)
330 (define-key rmail-mode-map "f" 'rmail-forward)
331 (define-key rmail-mode-map "g" 'rmail-get-new-mail)
332 (define-key rmail-mode-map "h" 'rmail-summary)
333 (define-key rmail-mode-map "i" 'rmail-input)
334 (define-key rmail-mode-map "j" 'rmail-show-message)
335 (define-key rmail-mode-map "k" 'rmail-kill-label)
336 (define-key rmail-mode-map "l" 'rmail-summary-by-labels)
337 (define-key rmail-mode-map "\e\C-h" 'rmail-summary)
338 (define-key rmail-mode-map "\e\C-l" 'rmail-summary-by-labels)
339 (define-key rmail-mode-map "\e\C-r" 'rmail-summary-by-recipients)
340 (define-key rmail-mode-map "\e\C-s" 'rmail-summary-by-regexp)
341 (define-key rmail-mode-map "\e\C-t" 'rmail-summary-by-topic)
342 (define-key rmail-mode-map "m" 'rmail-mail)
343 (define-key rmail-mode-map "\em" 'rmail-retry-failure)
344 (define-key rmail-mode-map "n" 'rmail-next-undeleted-message)
345 (define-key rmail-mode-map "\en" 'rmail-next-message)
346 (define-key rmail-mode-map "\e\C-n" 'rmail-next-labeled-message)
347 (define-key rmail-mode-map "o" 'rmail-output-to-rmail-file)
348 (define-key rmail-mode-map "\C-o" 'rmail-output)
349 (define-key rmail-mode-map "p" 'rmail-previous-undeleted-message)
350 (define-key rmail-mode-map "\ep" 'rmail-previous-message)
351 (define-key rmail-mode-map "\e\C-p" 'rmail-previous-labeled-message)
352 (define-key rmail-mode-map "q" 'rmail-quit)
353 (define-key rmail-mode-map "r" 'rmail-reply)
354 ;; I find I can't live without the default M-r command -- rms.
355 ;; (define-key rmail-mode-map "\er" 'rmail-search-backwards)
356 (define-key rmail-mode-map "s" 'rmail-expunge-and-save)
357 (define-key rmail-mode-map "\es" 'rmail-search)
358 (define-key rmail-mode-map "t" 'rmail-toggle-header)
359 (define-key rmail-mode-map "u" 'rmail-undelete-previous-message)
360 (define-key rmail-mode-map "w" 'rmail-edit-current-message)
361 (define-key rmail-mode-map "x" 'rmail-expunge)
362 (define-key rmail-mode-map "." 'rmail-beginning-of-message)
363 (define-key rmail-mode-map "<" 'rmail-first-message)
364 (define-key rmail-mode-map ">" 'rmail-last-message)
365 (define-key rmail-mode-map " " 'scroll-up)
366 (define-key rmail-mode-map "\177" 'scroll-down)
367 (define-key rmail-mode-map "?" 'describe-mode)
368 (define-key rmail-mode-map "\C-c\C-s\C-d" 'rmail-sort-by-date)
369 (define-key rmail-mode-map "\C-c\C-s\C-s" 'rmail-sort-by-subject)
370 (define-key rmail-mode-map "\C-c\C-s\C-a" 'rmail-sort-by-author)
371 (define-key rmail-mode-map "\C-c\C-s\C-r" 'rmail-sort-by-recipient)
372 (define-key rmail-mode-map "\C-c\C-s\C-c" 'rmail-sort-by-correspondent)
373 (define-key rmail-mode-map "\C-c\C-s\C-l" 'rmail-sort-by-lines)
374 )
375 \f
376 (define-key rmail-mode-map [menu-bar] (make-sparse-keymap))
377
378 (define-key rmail-mode-map [menu-bar classify]
379 (cons "Classify" (make-sparse-keymap "Classify")))
380
381 (define-key rmail-mode-map [menu-bar classify output-inbox]
382 '("Output (inbox)" . rmail-output))
383
384 (define-key rmail-mode-map [menu-bar classify output]
385 '("Output (Rmail)" . rmail-output-to-rmail-file))
386
387 (define-key rmail-mode-map [menu-bar classify kill-label]
388 '("Kill Label" . rmail-kill-label))
389
390 (define-key rmail-mode-map [menu-bar classify add-label]
391 '("Add Label" . rmail-add-label))
392
393 (define-key rmail-mode-map [menu-bar summary]
394 (cons "Summary" (make-sparse-keymap "Summary")))
395
396 (define-key rmail-mode-map [menu-bar summary labels]
397 '("By Labels" . rmail-summary-by-labels))
398
399 (define-key rmail-mode-map [menu-bar summary recipients]
400 '("By Recipients" . rmail-summary-by-recipients))
401
402 (define-key rmail-mode-map [menu-bar summary topic]
403 '("By Topic" . rmail-summary-by-topic))
404
405 (define-key rmail-mode-map [menu-bar summary regexp]
406 '("By Regexp" . rmail-summary-by-regexp))
407
408 (define-key rmail-mode-map [menu-bar summary all]
409 '("All" . rmail-summary))
410
411 (define-key rmail-mode-map [menu-bar mail]
412 (cons "Mail" (make-sparse-keymap "Mail")))
413
414 (define-key rmail-mode-map [menu-bar mail continue]
415 '("Continue" . rmail-continue))
416
417 (define-key rmail-mode-map [menu-bar mail forward]
418 '("Forward" . rmail-forward))
419
420 (define-key rmail-mode-map [menu-bar mail retry]
421 '("Retry" . rmail-retry-failure))
422
423 (define-key rmail-mode-map [menu-bar mail reply]
424 '("Reply" . rmail-reply))
425
426 (define-key rmail-mode-map [menu-bar mail mail]
427 '("Mail" . rmail-mail))
428
429 (define-key rmail-mode-map [menu-bar delete]
430 (cons "Delete" (make-sparse-keymap "Delete")))
431
432 (define-key rmail-mode-map [menu-bar delete expunge/save]
433 '("Expunge/Save" . rmail-expunge-and-save))
434
435 (define-key rmail-mode-map [menu-bar delete expunge]
436 '("Expunge" . rmail-expunge))
437
438 (define-key rmail-mode-map [menu-bar delete undelete]
439 '("Undelete" . rmail-undelete-previous-message))
440
441 (define-key rmail-mode-map [menu-bar delete delete]
442 '("Delete" . rmail-delete-forward))
443
444 (define-key rmail-mode-map [menu-bar move]
445 (cons "Move" (make-sparse-keymap "Move")))
446
447 (define-key rmail-mode-map [menu-bar move search-back]
448 '("Search Back" . rmail-search-backward))
449
450 (define-key rmail-mode-map [menu-bar move search]
451 '("Search" . rmail-search))
452
453 (define-key rmail-mode-map [menu-bar move previous]
454 '("Previous Nondeleted" . rmail-previous-undeleted-message))
455
456 (define-key rmail-mode-map [menu-bar move next]
457 '("Next Nondeleted" . rmail-next-undeleted-message))
458
459 (define-key rmail-mode-map [menu-bar move last]
460 '("Last" . rmail-last-message))
461
462 (define-key rmail-mode-map [menu-bar move first]
463 '("First" . rmail-first-message))
464
465 (define-key rmail-mode-map [menu-bar move previous]
466 '("Previous" . rmail-previous-message))
467
468 (define-key rmail-mode-map [menu-bar move next]
469 '("Next" . rmail-next-message))
470 \f
471 ;; Rmail mode is suitable only for specially formatted data.
472 (put 'rmail-mode 'mode-class 'special)
473
474 ;;;###autoload
475 (defun rmail-mode ()
476 "Rmail Mode is used by \\<rmail-mode-map>\\[rmail] for editing Rmail files.
477 All normal editing commands are turned off.
478 Instead, these commands are available:
479
480 \\[rmail-beginning-of-message] Move point to front of this message (same as \\[beginning-of-buffer]).
481 \\[scroll-up] Scroll to next screen of this message.
482 \\[scroll-down] Scroll to previous screen of this message.
483 \\[rmail-next-undeleted-message] Move to Next non-deleted message.
484 \\[rmail-previous-undeleted-message] Move to Previous non-deleted message.
485 \\[rmail-next-message] Move to Next message whether deleted or not.
486 \\[rmail-previous-message] Move to Previous message whether deleted or not.
487 \\[rmail-first-message] Move to the first message in Rmail file.
488 \\[rmail-last-message] Move to the last message in Rmail file.
489 \\[rmail-show-message] Jump to message specified by numeric position in file.
490 \\[rmail-search] Search for string and show message it is found in.
491 \\[rmail-delete-forward] Delete this message, move to next nondeleted.
492 \\[rmail-delete-backward] Delete this message, move to previous nondeleted.
493 \\[rmail-undelete-previous-message] Undelete message. Tries current message, then earlier messages
494 till a deleted message is found.
495 \\[rmail-edit-current-message] Edit the current message. \\[rmail-cease-edit] to return to Rmail.
496 \\[rmail-expunge] Expunge deleted messages.
497 \\[rmail-expunge-and-save] Expunge and save the file.
498 \\[rmail-quit] Quit Rmail: expunge, save, then switch to another buffer.
499 \\[save-buffer] Save without expunging.
500 \\[rmail-get-new-mail] Move new mail from system spool directory into this file.
501 \\[rmail-mail] Mail a message (same as \\[mail-other-window]).
502 \\[rmail-continue] Continue composing outgoing message started before.
503 \\[rmail-reply] Reply to this message. Like \\[rmail-mail] but initializes some fields.
504 \\[rmail-retry-failure] Send this message again. Used on a mailer failure message.
505 \\[rmail-forward] Forward this message to another user.
506 \\[rmail-output-to-rmail-file] Output this message to an Rmail file (append it).
507 \\[rmail-output] Output this message to a Unix-format mail file (append it).
508 \\[rmail-input] Input Rmail file. Run Rmail on that file.
509 \\[rmail-add-label] Add label to message. It will be displayed in the mode line.
510 \\[rmail-kill-label] Kill label. Remove a label from current message.
511 \\[rmail-next-labeled-message] Move to Next message with specified label
512 (label defaults to last one specified).
513 Standard labels: filed, unseen, answered, forwarded, deleted.
514 Any other label is present only if you add it with \\[rmail-add-label].
515 \\[rmail-previous-labeled-message] Move to Previous message with specified label
516 \\[rmail-summary] Show headers buffer, with a one line summary of each message.
517 \\[rmail-summary-by-labels] Summarize only messages with particular label(s).
518 \\[rmail-summary-by-recipients] Summarize only messages with particular recipient(s).
519 \\[rmail-summary-by-regexp] Summarize only messages with particular regexp(s).
520 \\[rmail-summary-by-topic] Summarize only messages with subject line regexp(s).
521 \\[rmail-toggle-header] Toggle display of complete header."
522 (interactive)
523 (rmail-mode-2)
524 (rmail-set-message-counters)
525 (rmail-show-message rmail-total-messages))
526
527 (defun rmail-mode-2 ()
528 (kill-all-local-variables)
529 (rmail-mode-1)
530 (rmail-variables)
531 (run-hooks 'rmail-mode-hook))
532
533 (defun rmail-mode-1 ()
534 (setq major-mode 'rmail-mode)
535 (setq mode-name "RMAIL")
536 (setq buffer-read-only t)
537 ;; No need to auto save RMAIL files in normal circumstances
538 ;; because they contain no info except attribute changes
539 ;; and deletion of messages.
540 ;; The one exception is when messages are copied into an Rmail mode buffer.
541 ;; rmail-output-to-rmail-file enables auto save when you do that.
542 (setq buffer-auto-save-file-name nil)
543 (if (boundp 'mode-line-modified)
544 (setq mode-line-modified "--- ")
545 (setq mode-line-format
546 (cons "--- " (cdr (default-value 'mode-line-format)))))
547 (use-local-map rmail-mode-map)
548 (set-syntax-table text-mode-syntax-table)
549 (setq local-abbrev-table text-mode-abbrev-table))
550
551 (defun rmail-variables ()
552 (make-local-variable 'revert-buffer-function)
553 (setq revert-buffer-function 'rmail-revert)
554 (make-local-variable 'rmail-last-label)
555 (make-local-variable 'rmail-last-regexp)
556 (make-local-variable 'rmail-deleted-vector)
557 (make-local-variable 'rmail-summary-buffer)
558 (make-local-variable 'rmail-summary-vector)
559 (make-local-variable 'rmail-current-message)
560 (make-local-variable 'rmail-total-messages)
561 (make-local-variable 'require-final-newline)
562 (setq require-final-newline nil)
563 (make-local-variable 'version-control)
564 (setq version-control 'never)
565 (make-local-variable 'file-precious-flag)
566 (setq file-precious-flag t)
567 (make-local-variable 'rmail-message-vector)
568 (make-local-variable 'rmail-last-file)
569 (make-local-variable 'rmail-inbox-list)
570 (setq rmail-inbox-list (rmail-parse-file-inboxes))
571 ;; Provide default set of inboxes for primary mail file ~/RMAIL.
572 (and (null rmail-inbox-list)
573 (or (equal buffer-file-name (expand-file-name rmail-file-name))
574 (equal buffer-file-truename
575 (abbreviate-file-name (file-truename rmail-file-name))))
576 (setq rmail-inbox-list
577 (or rmail-primary-inbox-list
578 (list (or (getenv "MAIL")
579 (concat rmail-spool-directory
580 (user-original-login-name)))))))
581 (make-local-variable 'rmail-keywords)
582 ;; this gets generated as needed
583 (setq rmail-keywords nil))
584
585 ;; Handle M-x revert-buffer done in an rmail-mode buffer.
586 (defun rmail-revert (arg noconfirm)
587 (let (revert-buffer-function)
588 ;; Call our caller again, but this time it does the default thing.
589 (if (revert-buffer arg noconfirm)
590 ;; If the user said "yes", and we changed something,
591 ;; reparse the messages.
592 (progn
593 (rmail-convert-file)
594 (goto-char (point-max))
595 (rmail-set-message-counters)
596 (rmail-show-message)))))
597
598 ;; Return a list of files from this buffer's Mail: option.
599 ;; Does not assume that messages have been parsed.
600 ;; Just returns nil if buffer does not look like Babyl format.
601 (defun rmail-parse-file-inboxes ()
602 (save-excursion
603 (save-restriction
604 (widen)
605 (goto-char 1)
606 (cond ((looking-at "BABYL OPTIONS:")
607 (search-forward "\n\^_" nil 'move)
608 (narrow-to-region 1 (point))
609 (goto-char 1)
610 (if (search-forward "\nMail:" nil t)
611 (progn
612 (narrow-to-region (point) (progn (end-of-line) (point)))
613 (goto-char (point-min))
614 (mail-parse-comma-list))))))))
615
616 (defun rmail-expunge-and-save ()
617 "Expunge and save RMAIL file."
618 (interactive)
619 (rmail-expunge)
620 (save-buffer))
621
622 (defun rmail-quit ()
623 "Quit out of RMAIL."
624 (interactive)
625 (rmail-expunge-and-save)
626 ;; Don't switch to the summary buffer even if it was recently visible.
627 (if rmail-summary-buffer
628 (bury-buffer rmail-summary-buffer))
629 (let ((obuf (current-buffer)))
630 (switch-to-buffer (other-buffer))
631 (bury-buffer obuf)))
632
633 ;;;###autoload
634 (defun rmail-input (filename)
635 "Run Rmail on file FILENAME."
636 (interactive "FRun rmail on RMAIL file: ")
637 (rmail filename))
638
639 \f
640 ;;;; *** Rmail input ***
641
642 ;; RLK feature not added in this version:
643 ;; argument specifies inbox file or files in various ways.
644
645 (defun rmail-get-new-mail (&optional file-name)
646 "Move any new mail from this RMAIL file's inbox files.
647 The inbox files can be specified with the file's Mail: option. The
648 variable `rmail-primary-inbox-list' specifies the inboxes for your
649 primary RMAIL file if it has no Mail: option. By default, this is
650 your /usr/spool/mail/$USER.
651
652 You can also specify the file to get new mail from. In this case, the
653 file of new mail is not changed or deleted. Noninteractively, you can
654 pass the inbox file name as an argument. Interactively, a prefix
655 argument causes us to read a file name and use that file as the inbox."
656 (interactive
657 (list (if current-prefix-arg
658 (read-file-name "Get new mail from file: "))))
659 (or (verify-visited-file-modtime (current-buffer))
660 (progn
661 (find-file (buffer-file-name))
662 (setq buffer-read-only t)
663 (if (verify-visited-file-modtime (current-buffer))
664 (rmail-forget-messages))))
665 (rmail-maybe-set-message-counters)
666 (widen)
667 ;; Get rid of all undo records for this buffer.
668 (or (eq buffer-undo-list t)
669 (setq buffer-undo-list nil))
670 (unwind-protect
671 (let ((opoint (point))
672 (new-messages 0)
673 (delete-files ())
674 ;; If buffer has not changed yet, and has not been saved yet,
675 ;; don't replace the old backup file now.
676 (make-backup-files (and make-backup-files (buffer-modified-p)))
677 (buffer-read-only nil)
678 ;; Don't make undo records for what we do in getting mail.
679 (buffer-undo-list t))
680 (goto-char (point-max))
681 (skip-chars-backward " \t\n") ; just in case of brain damage
682 (delete-region (point) (point-max)) ; caused by require-final-newline
683 (save-excursion
684 (save-restriction
685 (narrow-to-region (point) (point))
686 ;; Read in the contents of the inbox files,
687 ;; renaming them as necessary,
688 ;; and adding to the list of files to delete eventually.
689 (if file-name
690 (rmail-insert-inbox-text (list file-name) nil)
691 (setq delete-files (rmail-insert-inbox-text rmail-inbox-list t)))
692 ;; Scan the new text and convert each message to babyl format.
693 (goto-char (point-min))
694 (save-excursion
695 (setq new-messages (rmail-convert-to-babyl-format)))
696 (or (zerop new-messages)
697 (let (success)
698 (widen)
699 (search-backward "\n\^_" nil t)
700 (narrow-to-region (point) (point-max))
701 (goto-char (1+ (point-min)))
702 (rmail-count-new-messages)
703 (save-buffer)))
704 ;; Delete the old files, now that babyl file is saved.
705 (while delete-files
706 (condition-case ()
707 ;; First, try deleting.
708 (condition-case ()
709 (delete-file (car delete-files))
710 (file-error
711 ;; If we can't delete it, truncate it.
712 (write-region (point) (point) (car delete-files))))
713 (file-error nil))
714 (setq delete-files (cdr delete-files)))))
715 (if (= new-messages 0)
716 (progn (goto-char opoint)
717 (if (or file-name rmail-inbox-list)
718 (message "(No new mail has arrived)")))
719 (if (rmail-summary-exists)
720 (rmail-select-summary
721 (rmail-update-summary)))
722 (message "%d new message%s read"
723 new-messages (if (= 1 new-messages) "" "s"))
724 (and (boundp 'display-time-string)
725 (stringp display-time-string)
726 (string-match " Mail" display-time-string)
727 (setq display-time-string
728 (concat
729 (substring display-time-string 0 (match-beginning 0))
730 (substring display-time-string (match-end 0))))
731 (force-mode-line-update 'all))))
732 ;; Don't leave the buffer screwed up if we get a disk-full error.
733 (rmail-show-message)))
734
735 (defun rmail-insert-inbox-text (files renamep)
736 (let (file tofile delete-files movemail popmail)
737 (while files
738 (setq file (expand-file-name (substitute-in-file-name (car files)))
739 ;;>> un*x specific <<
740 ;; The "+" used to be "~", which is an extremely poor choice;
741 ;; it might accidentally be deleted when space is low
742 ;; (as happened to me!).
743 tofile (concat file "+"))
744 ;; If getting from mail spool directory,
745 ;; use movemail to move rather than just renaming,
746 ;; so as to interlock with the mailer.
747 (setq movemail (equal (file-name-directory file) rmail-spool-directory)
748 popmail (string-match "^po:" (file-name-nondirectory file)))
749 (if popmail (setq file (file-name-nondirectory file)
750 renamep t))
751 (if movemail
752 (progn
753 (setq tofile (expand-file-name
754 ;; Generate name to move to from inbox name,
755 ;; in case of multiple inboxes that need moving.
756 (concat ".newmail-" (file-name-nondirectory file))
757 ;; Use the directory of this rmail file
758 ;; because it's a nuisance to use the homedir
759 ;; if that is on a full disk and this rmail
760 ;; file isn't.
761 (file-name-directory
762 (expand-file-name buffer-file-name))))
763 ;; On some systems, /usr/spool/mail/foo is a directory
764 ;; and the actual inbox is /usr/spool/mail/foo/foo.
765 (if (file-directory-p file)
766 (setq file (expand-file-name (user-original-login-name)
767 file)))))
768 (if popmail
769 (message "Getting mail from post office ...")
770 (if (or (and (file-exists-p tofile)
771 (/= 0 (nth 7 (file-attributes tofile))))
772 (and (file-exists-p file)
773 (/= 0 (nth 7 (file-attributes file)))))
774 (message "Getting mail from %s..." file)))
775 ;; Set TOFILE if have not already done so, and
776 ;; rename or copy the file FILE to TOFILE if and as appropriate.
777 (cond ((not renamep)
778 (setq tofile file))
779 ((or (file-exists-p tofile) (and (not popmail)
780 (not (file-exists-p file))))
781 nil)
782 ((and (not movemail) (not popmail))
783 ;; Try copying. If that fails (perhaps no space),
784 ;; rename instead.
785 (condition-case nil
786 (copy-file file tofile nil)
787 (error
788 ;; Third arg is t so we can replace existing file TOFILE.
789 (rename-file file tofile t)))
790 ;; Make the real inbox file empty.
791 ;; Leaving it deleted could cause lossage
792 ;; because mailers often won't create the file.
793 (condition-case ()
794 (write-region (point) (point) file)
795 (file-error nil)))
796 (t
797 (let ((errors nil))
798 (unwind-protect
799 (save-excursion
800 (setq errors (generate-new-buffer " *rmail loss*"))
801 (buffer-disable-undo errors)
802 (call-process
803 (expand-file-name "movemail" exec-directory)
804 nil errors nil file tofile)
805 (if (not (buffer-modified-p errors))
806 ;; No output => movemail won
807 nil
808 (set-buffer errors)
809 (subst-char-in-region (point-min) (point-max)
810 ?\n ?\ )
811 (goto-char (point-max))
812 (skip-chars-backward " \t")
813 (delete-region (point) (point-max))
814 (goto-char (point-min))
815 (if (looking-at "movemail: ")
816 (delete-region (point-min) (match-end 0)))
817 (beep t)
818 (message (concat "movemail: "
819 (buffer-substring (point-min)
820 (point-max))))
821 (sit-for 3)
822 nil))
823 (if errors (kill-buffer errors))))))
824 ;; At this point, TOFILE contains the name to read:
825 ;; Either the alternate name (if we renamed)
826 ;; or the actual inbox (if not renaming).
827 (if (file-exists-p tofile)
828 (let (size)
829 (goto-char (point-max))
830 (setq size (nth 1 (insert-file-contents tofile)))
831 (goto-char (point-max))
832 (or (= (preceding-char) ?\n)
833 (zerop size)
834 (insert ?\n))
835 (setq delete-files (cons tofile delete-files))))
836 (message "")
837 (setq files (cdr files)))
838 delete-files))
839
840 ;; the rmail-break-forwarded-messages feature is not implemented
841 (defun rmail-convert-to-babyl-format ()
842 (let ((count 0) start
843 (case-fold-search nil)
844 (invalid-input-resync
845 (function (lambda ()
846 (message "Invalid Babyl format in inbox!")
847 (sit-for 1)
848 ;; Try to get back in sync with a real message.
849 (if (re-search-forward
850 (concat mmdf-delim1 "\\|^From") nil t)
851 (beginning-of-line)
852 (goto-char (point-max)))))))
853 (goto-char (point-min))
854 (save-restriction
855 (while (not (eobp))
856 (cond ((looking-at "BABYL OPTIONS:");Babyl header
857 (if (search-forward "\n\^_" nil t)
858 ;; If we find the proper terminator, delete through there.
859 (delete-region (point-min) (point))
860 (funcall invalid-input-resync)
861 (delete-region (point-min) (point))))
862 ;; Babyl format message
863 ((looking-at "\^L")
864 (or (search-forward "\n\^_" nil t)
865 (funcall invalid-input-resync))
866 (setq count (1+ count))
867 ;; Make sure there is no extra white space after the ^_
868 ;; at the end of the message.
869 ;; Narrowing will make sure that whatever follows the junk
870 ;; will be treated properly.
871 (delete-region (point)
872 (save-excursion
873 (skip-chars-forward " \t\n")
874 (point)))
875 (narrow-to-region (point) (point-max)))
876 ;;*** MMDF format
877 ((let ((case-fold-search t))
878 (looking-at mmdf-delim1))
879 (let ((case-fold-search t))
880 (replace-match "\^L\n0, unseen,,\n*** EOOH ***\n")
881 (setq start (point))
882 (re-search-forward mmdf-delim2 nil t)
883 (replace-match "\^_"))
884 (save-excursion
885 (save-restriction
886 (narrow-to-region start (1- (point)))
887 (goto-char (point-min))
888 (while (search-forward "\n\^_" nil t); single char "\^_"
889 (replace-match "\n^_")))); 2 chars: "^" and "_"
890 (narrow-to-region (point) (point-max))
891 (setq count (1+ count)))
892 ;;*** Mail format
893 ((looking-at "^From ")
894 (setq start (point))
895 (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
896 (rmail-nuke-pinhead-header)
897 ;; If this message has a Content-Length field,
898 ;; skip to the end of the contents.
899 (let* ((header-end (save-excursion
900 (and (re-search-forward "\n\n" nil t)
901 (1- (point)))))
902 (case-fold-search t)
903 (size
904 ;; Get the numeric value from the Content-Length field.
905 (save-excursion
906 ;; Back up to end of prev line,
907 ;; in case the Content-Length field comes first.
908 (forward-char -1)
909 (and (search-forward "\ncontent-length: "
910 header-end t)
911 (let ((beg (point))
912 (eol (progn (end-of-line) (point))))
913 (read (buffer-substring beg eol)))))))
914 (and size (numberp size) (>= size 0)
915 (goto-char (+ header-end size))))
916
917 (if (re-search-forward
918 (concat "^[\^_]?\\("
919 rmail-unix-mail-delimiter
920 "\\|"
921 mmdf-delim1 "\\|"
922 "^BABYL OPTIONS:\\|"
923 "\^L\n[01],\\)") nil t)
924 (goto-char (match-beginning 1))
925 (goto-char (point-max)))
926 (setq count (1+ count))
927 (save-excursion
928 (save-restriction
929 (narrow-to-region start (point))
930 (goto-char (point-min))
931 (while (search-forward "\n\^_" nil t); single char
932 (replace-match "\n^_")))); 2 chars: "^" and "_"
933 (insert ?\^_)
934 (narrow-to-region (point) (point-max)))
935 ;;
936 ;; This kludge is because some versions of sendmail.el
937 ;; insert an extra newline at the beginning that shouldn't
938 ;; be there. sendmail.el has been fixed, but old versions
939 ;; may still be in use. -- rms, 7 May 1993.
940 ((eolp) (delete-char 1))
941 (t (error "Cannot convert to babyl format")))))
942 count))
943
944 ;; Delete the "From ..." line, creating various other headers with
945 ;; information from it if they don't already exist. Now puts the
946 ;; original line into a mail-from: header line for debugging and for
947 ;; use by the rmail-output function.
948 (defun rmail-nuke-pinhead-header ()
949 (save-excursion
950 (save-restriction
951 (let ((start (point))
952 (end (progn
953 (condition-case ()
954 (search-forward "\n\n")
955 (error
956 (goto-char (point-max))
957 (insert "\n\n")))
958 (point)))
959 has-from has-date)
960 (narrow-to-region start end)
961 (let ((case-fold-search t))
962 (goto-char start)
963 (setq has-from (search-forward "\nFrom:" nil t))
964 (goto-char start)
965 (setq has-date (and (search-forward "\nDate:" nil t) (point)))
966 (goto-char start))
967 (let ((case-fold-search nil))
968 (if (re-search-forward (concat "^" rmail-unix-mail-delimiter) nil t)
969 (replace-match
970 (concat
971 "Mail-from: \\&"
972 ;; Keep and reformat the date if we don't
973 ;; have a Date: field.
974 (if has-date
975 ""
976 (concat
977 "Date: \\3, \\5 \\4 \\9 \\6 "
978
979 ;; The timezone could be matched by group 7 or group 10.
980 ;; If neither of them matched, assume EST, since only
981 ;; Easterners would be so sloppy.
982 ;; It's a shame the substitution can't use "\\10".
983 (cond
984 ((/= (match-beginning 7) (match-end 7)) "\\7")
985 ((/= (match-beginning 10) (match-end 10))
986 (buffer-substring (match-beginning 10)
987 (match-end 10)))
988 (t "EST"))
989 "\n"))
990 ;; Keep and reformat the sender if we don't
991 ;; have a From: field.
992 (if has-from
993 ""
994 "From: \\1\n"))
995 t)))))))
996 \f
997 ;;;; *** Rmail Message Formatting and Header Manipulation ***
998
999 (defun rmail-reformat-message (beg end)
1000 (goto-char beg)
1001 (forward-line 1)
1002 (if (/= (following-char) ?0)
1003 (error "Bad format in RMAIL file."))
1004 (let ((buffer-read-only nil)
1005 (delta (- (buffer-size) end)))
1006 (delete-char 1)
1007 (insert ?1)
1008 (forward-line 1)
1009 (let ((case-fold-search t))
1010 (while (looking-at "Summary-line:\\|Mail-From:")
1011 (forward-line 1)))
1012 (if (looking-at "\\*\\*\\* EOOH \\*\\*\\*\n")
1013 (delete-region (point)
1014 (progn (forward-line 1) (point))))
1015 (let ((str (buffer-substring (point)
1016 (save-excursion (search-forward "\n\n" end 'move)
1017 (point)))))
1018 (insert str "*** EOOH ***\n")
1019 (narrow-to-region (point) (- (buffer-size) delta)))
1020 (goto-char (point-min))
1021 (if rmail-ignored-headers (rmail-clear-headers))
1022 (if rmail-message-filter (funcall rmail-message-filter))))
1023
1024 (defun rmail-clear-headers ()
1025 (if (search-forward "\n\n" nil t)
1026 (save-restriction
1027 (narrow-to-region (point-min) (point))
1028 (let ((buffer-read-only nil))
1029 (while (let ((case-fold-search t))
1030 (goto-char (point-min))
1031 (re-search-forward rmail-ignored-headers nil t))
1032 (beginning-of-line)
1033 (delete-region (point)
1034 (progn (re-search-forward "\n[^ \t]")
1035 (forward-char -1)
1036 (point))))))))
1037
1038 (defun rmail-toggle-header ()
1039 "Show original message header if pruned header currently shown, or vice versa."
1040 (interactive)
1041 (rmail-maybe-set-message-counters)
1042 (narrow-to-region (rmail-msgbeg rmail-current-message) (point-max))
1043 (let ((buffer-read-only nil))
1044 (goto-char (point-min))
1045 (forward-line 1)
1046 (if (= (following-char) ?1)
1047 (progn (delete-char 1)
1048 (insert ?0)
1049 (forward-line 1)
1050 (let ((case-fold-search t))
1051 (while (looking-at "Summary-Line:\\|Mail-From:")
1052 (forward-line 1)))
1053 (insert "*** EOOH ***\n")
1054 (forward-char -1)
1055 (search-forward "\n*** EOOH ***\n")
1056 (forward-line -1)
1057 (let ((temp (point)))
1058 (and (search-forward "\n\n" nil t)
1059 (delete-region temp (point))))
1060 (goto-char (point-min))
1061 (search-forward "\n*** EOOH ***\n")
1062 (narrow-to-region (point) (point-max)))
1063 (rmail-reformat-message (point-min) (point-max)))))
1064 \f
1065 ;;;; *** Rmail Attributes and Keywords ***
1066
1067 ;; Make a string describing current message's attributes and keywords
1068 ;; and set it up as the name of a minor mode
1069 ;; so it will appear in the mode line.
1070 (defun rmail-display-labels ()
1071 (let ((blurb "") (beg (point-min-marker)) (end (point-max-marker)))
1072 (save-excursion
1073 (unwind-protect
1074 (progn
1075 (widen)
1076 (goto-char (rmail-msgbeg rmail-current-message))
1077 (forward-line 1)
1078 (if (looking-at "[01],")
1079 (progn
1080 (narrow-to-region (point) (progn (end-of-line) (point)))
1081 ;; Truly valid BABYL format requires a space before each
1082 ;; attribute or keyword name. Put them in if missing.
1083 (let (buffer-read-only)
1084 (goto-char (point-min))
1085 (while (search-forward "," nil t)
1086 (or (looking-at "[ ,]") (eobp)
1087 (insert " "))))
1088 (goto-char (point-max))
1089 (if (search-backward ",," nil 'move)
1090 (progn
1091 (if (> (point) (1+ (point-min)))
1092 (setq blurb (buffer-substring (+ 1 (point-min)) (point))))
1093 (if (> (- (point-max) (point)) 2)
1094 (setq blurb
1095 (concat blurb
1096 ";"
1097 (buffer-substring (+ (point) 3)
1098 (1- (point-max)))))))))))
1099 ;; Note: we don't use save-restriction because that does not work right
1100 ;; if changes are made outside the saved restriction
1101 ;; before that restriction is restored.
1102 (narrow-to-region beg end)
1103 (set-marker beg nil)
1104 (set-marker end nil)))
1105 (while (string-match " +," blurb)
1106 (setq blurb (concat (substring blurb 0 (match-beginning 0)) ","
1107 (substring blurb (match-end 0)))))
1108 (while (string-match ", +" blurb)
1109 (setq blurb (concat (substring blurb 0 (match-beginning 0)) ","
1110 (substring blurb (match-end 0)))))
1111 (setq mode-line-process
1112 (concat " " rmail-current-message "/" rmail-total-messages
1113 blurb))))
1114
1115 ;; Turn an attribute of a message on or off according to STATE.
1116 ;; ATTR is the name of the attribute, as a string.
1117 ;; MSGNUM is message number to change; nil means current message.
1118 (defun rmail-set-attribute (attr state &optional msgnum)
1119 (let ((omax (point-max-marker))
1120 (omin (point-min-marker))
1121 (buffer-read-only nil))
1122 (or msgnum (setq msgnum rmail-current-message))
1123 (if (> msgnum 0)
1124 (unwind-protect
1125 (save-excursion
1126 (widen)
1127 (goto-char (+ 3 (rmail-msgbeg msgnum)))
1128 (let ((curstate
1129 (not
1130 (null (search-backward (concat ", " attr ",")
1131 (prog1 (point) (end-of-line)) t)))))
1132 (or (eq curstate (not (not state)))
1133 (if curstate
1134 (delete-region (point) (1- (match-end 0)))
1135 (beginning-of-line)
1136 (forward-char 2)
1137 (insert " " attr ","))))
1138 (if (string= attr "deleted")
1139 (rmail-set-message-deleted-p msgnum state)))
1140 ;; Note: we don't use save-restriction because that does not work right
1141 ;; if changes are made outside the saved restriction
1142 ;; before that restriction is restored.
1143 (narrow-to-region omin omax)
1144 (set-marker omin nil)
1145 (set-marker omax nil)
1146 (if (= msgnum rmail-current-message)
1147 (rmail-display-labels))))))
1148
1149 ;; Return t if the attributes/keywords line of msg number MSG
1150 ;; contains a match for the regexp LABELS.
1151 (defun rmail-message-labels-p (msg labels)
1152 (save-excursion
1153 (save-restriction
1154 (widen)
1155 (goto-char (rmail-msgbeg msg))
1156 (forward-char 3)
1157 (re-search-backward labels (prog1 (point) (end-of-line)) t))))
1158 \f
1159 ;;;; *** Rmail Message Selection And Support ***
1160
1161 (defun rmail-msgend (n)
1162 (marker-position (aref rmail-message-vector (1+ n))))
1163
1164 (defun rmail-msgbeg (n)
1165 (marker-position (aref rmail-message-vector n)))
1166
1167 (defun rmail-widen-to-current-msgbeg (function)
1168 "Call FUNCTION with point at start of internal data of current message.
1169 Assumes that bounds were previously narrowed to display the message in Rmail.
1170 The bounds are widened enough to move point where desired, then narrowed
1171 again afterward.
1172
1173 FUNCTION may not change the visible text of the message, but it may
1174 change the invisible header text."
1175 (save-excursion
1176 (let ((obeg (- (point-max) (point-min))))
1177 (unwind-protect
1178 (progn
1179 (narrow-to-region (rmail-msgbeg rmail-current-message)
1180 (point-max))
1181 (goto-char (point-min))
1182 (funcall function))
1183 ;; Note: we don't use save-restriction because that does not work right
1184 ;; if changes are made outside the saved restriction
1185 ;; before that restriction is restored.
1186 ;; Here we assume that changes made by FUNCTION
1187 ;; occur before the visible region of the message.
1188 (narrow-to-region (- (point-max) obeg) (point-max))))))
1189
1190 (defun rmail-forget-messages ()
1191 (unwind-protect
1192 (if (vectorp rmail-message-vector)
1193 (let* ((i 0)
1194 (v rmail-message-vector)
1195 (n (length v)))
1196 (while (< i n)
1197 (move-marker (aref v i) nil)
1198 (setq i (1+ i)))))
1199 (setq rmail-message-vector nil)
1200 (setq rmail-deleted-vector nil)))
1201
1202 (defun rmail-maybe-set-message-counters ()
1203 (if (not (and rmail-deleted-vector
1204 rmail-message-vector
1205 rmail-current-message
1206 rmail-total-messages))
1207 (rmail-set-message-counters)))
1208
1209 (defun rmail-count-new-messages (&optional nomsg)
1210 (let* ((case-fold-search nil)
1211 (total-messages 0)
1212 (messages-head nil)
1213 (deleted-head nil))
1214 (or nomsg (message "Counting new messages..."))
1215 (goto-char (point-max))
1216 ;; Put at the end of messages-head
1217 ;; the entry for message N+1, which marks
1218 ;; the end of message N. (N = number of messages).
1219 (search-backward "\n\^_")
1220 (forward-char 1)
1221 (setq messages-head (list (point-marker)))
1222 (rmail-set-message-counters-counter (point-min))
1223 (setq rmail-current-message (1+ rmail-total-messages))
1224 (setq rmail-total-messages
1225 (+ rmail-total-messages total-messages))
1226 (setq rmail-message-vector
1227 (vconcat rmail-message-vector (cdr messages-head)))
1228 (aset rmail-message-vector
1229 rmail-current-message (car messages-head))
1230 (setq rmail-deleted-vector
1231 (concat rmail-deleted-vector deleted-head))
1232 (setq rmail-summary-vector
1233 (vconcat rmail-summary-vector (make-vector total-messages nil)))
1234 (goto-char (point-min))
1235 (or nomsg (message "Counting new messages...done (%d)" total-messages))))
1236
1237 (defun rmail-set-message-counters ()
1238 (rmail-forget-messages)
1239 (save-excursion
1240 (save-restriction
1241 (widen)
1242 (let* ((point-save (point))
1243 (total-messages 0)
1244 (messages-after-point)
1245 (case-fold-search nil)
1246 (messages-head nil)
1247 (deleted-head nil))
1248 (message "Counting messages...")
1249 (goto-char (point-max))
1250 ;; Put at the end of messages-head
1251 ;; the entry for message N+1, which marks
1252 ;; the end of message N. (N = number of messages).
1253 (search-backward "\n\^_" nil t)
1254 (if (/= (point) (point-max)) (forward-char 1))
1255 (setq messages-head (list (point-marker)))
1256 (rmail-set-message-counters-counter (min (point) point-save))
1257 (setq messages-after-point total-messages)
1258 (rmail-set-message-counters-counter)
1259 (setq rmail-total-messages total-messages)
1260 (setq rmail-current-message
1261 (min total-messages
1262 (max 1 (- total-messages messages-after-point))))
1263 (setq rmail-message-vector
1264 (apply 'vector (cons (point-min-marker) messages-head))
1265 rmail-deleted-vector (concat "D" deleted-head)
1266 rmail-summary-vector (make-vector rmail-total-messages nil))
1267 (message "Counting messages...done")))))
1268
1269 (defun rmail-set-message-counters-counter (&optional stop)
1270 (while (search-backward "\n\^_\^L\n" stop t)
1271 (forward-char 1)
1272 (setq messages-head (cons (point-marker) messages-head))
1273 (save-excursion
1274 (setq deleted-head
1275 (cons (if (search-backward ", deleted,"
1276 (prog1 (point)
1277 (forward-line 2))
1278 t)
1279 ?D ?\ )
1280 deleted-head)))
1281 (if (zerop (% (setq total-messages (1+ total-messages)) 20))
1282 (message "Counting messages...%d" total-messages))))
1283
1284 (defun rmail-beginning-of-message ()
1285 "Show current message starting from the beginning."
1286 (interactive)
1287 (rmail-show-message rmail-current-message))
1288
1289 (defun rmail-show-message (&optional n)
1290 "Show message number N (prefix argument), counting from start of file.
1291 If summary buffer is currently displayed, update current message there also."
1292 (interactive "p")
1293 (rmail-maybe-set-message-counters)
1294 (widen)
1295 (if (zerop rmail-total-messages)
1296 (progn (narrow-to-region (point-min) (1- (point-max)))
1297 (goto-char (point-min))
1298 (setq mode-line-process nil))
1299 (let (blurb)
1300 (if (not n)
1301 (setq n rmail-current-message)
1302 (cond ((<= n 0)
1303 (setq n 1
1304 rmail-current-message 1
1305 blurb "No previous message"))
1306 ((> n rmail-total-messages)
1307 (setq n rmail-total-messages
1308 rmail-current-message rmail-total-messages
1309 blurb "No following message"))
1310 (t
1311 (setq rmail-current-message n))))
1312 (let ((beg (rmail-msgbeg n))
1313 (end (rmail-msgend n)))
1314 (goto-char beg)
1315 (forward-line 1)
1316 (if (= (following-char) ?0)
1317 (progn
1318 (rmail-reformat-message beg end)
1319 (rmail-set-attribute "unseen" nil))
1320 (search-forward "\n*** EOOH ***\n" end t)
1321 (narrow-to-region (point) end))
1322 (goto-char (point-min))
1323 (rmail-display-labels)
1324 (run-hooks 'rmail-show-message-hook)
1325 ;; If there is a summary buffer, try to move to this message
1326 ;; in that buffer. But don't complain if this message
1327 ;; is not mentioned in the summary.
1328 (if (rmail-summary-exists)
1329 (let ((curr-msg rmail-current-message))
1330 (rmail-select-summary
1331 (rmail-summary-goto-msg curr-msg t t))))
1332 (if blurb
1333 (message blurb))))))
1334
1335 (defun rmail-next-message (n)
1336 "Show following message whether deleted or not.
1337 With prefix arg N, moves forward N messages, or backward if N is negative."
1338 (interactive "p")
1339 (rmail-maybe-set-message-counters)
1340 (rmail-show-message (+ rmail-current-message n)))
1341
1342 (defun rmail-previous-message (n)
1343 "Show previous message whether deleted or not.
1344 With prefix arg N, moves backward N messages, or forward if N is negative."
1345 (interactive "p")
1346 (rmail-next-message (- n)))
1347
1348 (defun rmail-next-undeleted-message (n)
1349 "Show following non-deleted message.
1350 With prefix arg N, moves forward N non-deleted messages,
1351 or backward if N is negative.
1352
1353 Returns t if a new message is being shown, nil otherwise."
1354 (interactive "p")
1355 (rmail-maybe-set-message-counters)
1356 (let ((lastwin rmail-current-message)
1357 (current rmail-current-message))
1358 (while (and (> n 0) (< current rmail-total-messages))
1359 (setq current (1+ current))
1360 (if (not (rmail-message-deleted-p current))
1361 (setq lastwin current n (1- n))))
1362 (while (and (< n 0) (> current 1))
1363 (setq current (1- current))
1364 (if (not (rmail-message-deleted-p current))
1365 (setq lastwin current n (1+ n))))
1366 (if (/= lastwin rmail-current-message)
1367 (progn (rmail-show-message lastwin)
1368 t)
1369 (if (< n 0)
1370 (message "No previous nondeleted message"))
1371 (if (> n 0)
1372 (message "No following nondeleted message"))
1373 nil)))
1374
1375 (defun rmail-previous-undeleted-message (n)
1376 "Show previous non-deleted message.
1377 With prefix argument N, moves backward N non-deleted messages,
1378 or forward if N is negative."
1379 (interactive "p")
1380 (rmail-next-undeleted-message (- n)))
1381
1382 (defun rmail-first-message ()
1383 "Show first message in file."
1384 (interactive)
1385 (rmail-maybe-set-message-counters)
1386 (rmail-show-message 1))
1387
1388 (defun rmail-last-message ()
1389 "Show last message in file."
1390 (interactive)
1391 (rmail-maybe-set-message-counters)
1392 (rmail-show-message rmail-total-messages))
1393
1394 (defun rmail-what-message ()
1395 (let ((where (point))
1396 (low 1)
1397 (high rmail-total-messages)
1398 (mid (/ rmail-total-messages 2)))
1399 (while (> (- high low) 1)
1400 (if (>= where (rmail-msgbeg mid))
1401 (setq low mid)
1402 (setq high mid))
1403 (setq mid (+ low (/ (- high low) 2))))
1404 (if (>= where (rmail-msgbeg high)) high low)))
1405
1406 (defun rmail-message-recipients-p (msg recipients &optional primary-only)
1407 (save-restriction
1408 (goto-char (rmail-msgbeg msg))
1409 (search-forward "\n*** EOOH ***\n")
1410 (narrow-to-region (point) (progn (search-forward "\n\n") (point)))
1411 (or (string-match recipients (or (mail-fetch-field "To") ""))
1412 (string-match recipients (or (mail-fetch-field "From") ""))
1413 (if (not primary-only)
1414 (string-match recipients (or (mail-fetch-field "Cc") ""))))))
1415
1416 (defun rmail-message-regexp-p (msg regexp)
1417 "Return t, if for message number MSG, regexp REGEXP matches in the header."
1418 (goto-char (rmail-msgbeg msg))
1419 (let ((end
1420 (save-excursion
1421 (search-forward "*** EOOH ***" (point-max)) (point))))
1422 (re-search-forward regexp end t)))
1423
1424 (defun rmail-search-backward (regexp &optional n)
1425 "Show message containing next match for REGEXP.
1426 Prefix argument gives repeat count; negative argument means search
1427 backwards (through earlier messages).
1428 Interactively, empty argument means use same regexp used last time."
1429 (interactive
1430 (let* ((reversep (>= (prefix-numeric-value current-prefix-arg) 0))
1431 (prompt
1432 (concat (if reversep "Reverse " "") "Rmail search (regexp): "))
1433 regexp)
1434 (if rmail-search-last-regexp
1435 (setq prompt (concat prompt
1436 "(default "
1437 rmail-search-last-regexp
1438 ") ")))
1439 (setq regexp (read-string prompt))
1440 (cond ((not (equal regexp ""))
1441 (setq rmail-search-last-regexp regexp))
1442 ((not rmail-search-last-regexp)
1443 (error "No previous Rmail search string")))
1444 (list rmail-search-last-regexp
1445 (prefix-numeric-value current-prefix-arg))))
1446 (rmail-search regexp (- n)))
1447
1448 (defvar rmail-search-last-regexp nil)
1449 (defun rmail-search (regexp &optional n)
1450 "Show message containing next match for REGEXP.
1451 Prefix argument gives repeat count; negative argument means search
1452 backwards (through earlier messages).
1453 Interactively, empty argument means use same regexp used last time."
1454 (interactive
1455 (let* ((reversep (< (prefix-numeric-value current-prefix-arg) 0))
1456 (prompt
1457 (concat (if reversep "Reverse " "") "Rmail search (regexp): "))
1458 regexp)
1459 (if rmail-search-last-regexp
1460 (setq prompt (concat prompt
1461 "(default "
1462 rmail-search-last-regexp
1463 ") ")))
1464 (setq regexp (read-string prompt))
1465 (cond ((not (equal regexp ""))
1466 (setq rmail-search-last-regexp regexp))
1467 ((not rmail-search-last-regexp)
1468 (error "No previous Rmail search string")))
1469 (list rmail-search-last-regexp
1470 (prefix-numeric-value current-prefix-arg))))
1471 (or n (setq n 1))
1472 (message "%sRmail search for %s..."
1473 (if (< n 0) "Reverse " "")
1474 regexp)
1475 (rmail-maybe-set-message-counters)
1476 (let ((omin (point-min))
1477 (omax (point-max))
1478 (opoint (point))
1479 win
1480 (reversep (< n 0))
1481 (msg rmail-current-message))
1482 (unwind-protect
1483 (progn
1484 (widen)
1485 (while (/= n 0)
1486 ;; Check messages one by one, advancing message number up or down
1487 ;; but searching forward through each message.
1488 (if reversep
1489 (while (and (null win) (> msg 1))
1490 (goto-char (rmail-msgbeg (setq msg (1- msg))))
1491 (setq win (re-search-forward
1492 regexp (rmail-msgend msg) t)))
1493 (while (and (null win) (< msg rmail-total-messages))
1494 (goto-char (rmail-msgbeg (setq msg (1+ msg))))
1495 (setq win (re-search-forward regexp (rmail-msgend msg) t))))
1496 (setq n (+ n (if reversep 1 -1)))))
1497 (if win
1498 (progn
1499 ;; If this is a reverse search and we found a message,
1500 ;; search backward thru this message to position point.
1501 (if reversep
1502 (progn
1503 (goto-char (rmail-msgend msg))
1504 (re-search-backward
1505 regexp (rmail-msgbeg msg) t)))
1506 (setq win (point))
1507 (rmail-show-message msg)
1508 (message "%sRmail search for %s...done"
1509 (if reversep "Reverse " "")
1510 regexp)
1511 (goto-char win))
1512 (goto-char opoint)
1513 (narrow-to-region omin omax)
1514 (ding)
1515 (message "Search failed: %s" regexp)))))
1516
1517 (defun rmail-search-backwards (regexp &optional n)
1518 "Show message containing previous match for REGEXP.
1519 Prefix argument gives repeat count; negative argument means search
1520 forward (through later messages).
1521 Interactively, empty argument means use same regexp used last time."
1522 (interactive
1523 (let* ((reversep (>= (prefix-numeric-value current-prefix-arg) 0))
1524 (prompt
1525 (concat (if reversep "Reverse " "") "Rmail search (regexp): "))
1526 regexp)
1527 (if rmail-search-last-regexp
1528 (setq prompt (concat prompt
1529 "(default "
1530 rmail-search-last-regexp
1531 ") ")))
1532 (setq regexp (read-string prompt))
1533 (cond ((not (equal regexp ""))
1534 (setq rmail-search-last-regexp regexp))
1535 ((not rmail-search-last-regexp)
1536 (error "No previous Rmail search string")))
1537 (list rmail-search-last-regexp
1538 (prefix-numeric-value current-prefix-arg))))
1539 (rmail-search regexp (- (or n -1))))
1540
1541 ;; Show the first message which has the `unseen' attribute.
1542 (defun rmail-first-unseen-message ()
1543 (rmail-maybe-set-message-counters)
1544 (let ((current 1)
1545 found)
1546 (save-restriction
1547 (widen)
1548 (while (and (not found) (< current rmail-total-messages))
1549 (if (rmail-message-labels-p current ", ?\\(unseen\\),")
1550 (setq found current))
1551 (setq current (1+ current))))
1552 ;; Let the caller show the message.
1553 ;; (if found
1554 ;; (rmail-show-message found))
1555 found))
1556 \f
1557 ;;;; *** Rmail Message Deletion Commands ***
1558
1559 (defun rmail-message-deleted-p (n)
1560 (= (aref rmail-deleted-vector n) ?D))
1561
1562 (defun rmail-set-message-deleted-p (n state)
1563 (aset rmail-deleted-vector n (if state ?D ?\ )))
1564
1565 (defun rmail-delete-message ()
1566 "Delete this message and stay on it."
1567 (interactive)
1568 (rmail-set-attribute "deleted" t))
1569
1570 (defun rmail-undelete-previous-message ()
1571 "Back up to deleted message, select it, and undelete it."
1572 (interactive)
1573 (let ((msg rmail-current-message))
1574 (while (and (> msg 0)
1575 (not (rmail-message-deleted-p msg)))
1576 (setq msg (1- msg)))
1577 (if (= msg 0)
1578 (error "No previous deleted message")
1579 (if (/= msg rmail-current-message)
1580 (rmail-show-message msg))
1581 (rmail-set-attribute "deleted" nil)
1582 (if (rmail-summary-exists)
1583 (save-excursion
1584 (set-buffer rmail-summary-buffer)
1585 (rmail-summary-mark-undeleted msg)))
1586 (rmail-maybe-display-summary))))
1587
1588 (defun rmail-delete-forward (&optional backward)
1589 "Delete this message and move to next nondeleted one.
1590 Deleted messages stay in the file until the \\[rmail-expunge] command is given.
1591 With prefix argument, delete and move backward.
1592
1593 Returns t if a new message is displayed after the delete, or nil otherwise."
1594 (interactive "P")
1595 (rmail-set-attribute "deleted" t)
1596 (let ((del-msg rmail-current-message))
1597 (if (rmail-summary-exists)
1598 (save-excursion
1599 (set-buffer rmail-summary-buffer)
1600 (rmail-summary-mark-deleted del-msg)))
1601 (prog1 (rmail-next-undeleted-message (if backward -1 1))
1602 (rmail-maybe-display-summary))))
1603
1604 (defun rmail-delete-backward ()
1605 "Delete this message and move to previous nondeleted one.
1606 Deleted messages stay in the file until the \\[rmail-expunge] command is given."
1607 (interactive)
1608 (rmail-delete-forward t))
1609
1610 (defun rmail-only-expunge ()
1611 "Actually erase all deleted messages in the file."
1612 (interactive)
1613 (message "Expunging deleted messages...")
1614 ;; Discard all undo records for this buffer.
1615 (or (eq buffer-undo-list t)
1616 (setq buffer-undo-list nil))
1617 (rmail-maybe-set-message-counters)
1618 (let* ((omax (- (buffer-size) (point-max)))
1619 (omin (- (buffer-size) (point-min)))
1620 (opoint (if (and (> rmail-current-message 0)
1621 (= ?D (aref rmail-deleted-vector rmail-current-message)))
1622 0 (- (point) (point-min))))
1623 (messages-head (cons (aref rmail-message-vector 0) nil))
1624 (messages-tail messages-head)
1625 ;; Don't make any undo records for the expunging.
1626 (buffer-undo-list t)
1627 (win))
1628 (unwind-protect
1629 (save-excursion
1630 (widen)
1631 (goto-char (point-min))
1632 (let ((counter 0)
1633 (number 1)
1634 (total rmail-total-messages)
1635 (new-message-number rmail-current-message)
1636 (new-summary nil)
1637 (buffer-read-only nil)
1638 (messages rmail-message-vector)
1639 (deleted rmail-deleted-vector)
1640 (summary rmail-summary-vector))
1641 (setq rmail-total-messages nil
1642 rmail-current-message nil
1643 rmail-message-vector nil
1644 rmail-deleted-vector nil
1645 rmail-summary-vector nil)
1646 (while (<= number total)
1647 (if (= (aref deleted number) ?D)
1648 (progn
1649 (delete-region
1650 (marker-position (aref messages number))
1651 (marker-position (aref messages (1+ number))))
1652 (move-marker (aref messages number) nil)
1653 (if (> new-message-number counter)
1654 (setq new-message-number (1- new-message-number))))
1655 (setq counter (1+ counter))
1656 (setq messages-tail
1657 (setcdr messages-tail
1658 (cons (aref messages number) nil)))
1659 (setq new-summary
1660 (cons (if (= counter number) (aref summary (1- number)))
1661 new-summary)))
1662 (if (zerop (% (setq number (1+ number)) 20))
1663 (message "Expunging deleted messages...%d" number)))
1664 (setq messages-tail
1665 (setcdr messages-tail
1666 (cons (aref messages number) nil)))
1667 (setq rmail-current-message new-message-number
1668 rmail-total-messages counter
1669 rmail-message-vector (apply 'vector messages-head)
1670 rmail-deleted-vector (make-string (1+ counter) ?\ )
1671 rmail-summary-vector (vconcat (nreverse new-summary))
1672 win t)))
1673 (message "Expunging deleted messages...done")
1674 (if (not win)
1675 (narrow-to-region (- (buffer-size) omin) (- (buffer-size) omax)))
1676 (rmail-show-message
1677 (if (zerop rmail-current-message) 1 nil))
1678 (forward-char opoint))))
1679
1680 (defun rmail-expunge ()
1681 "Erase deleted messages from Rmail file and summary buffer."
1682 (interactive)
1683 (rmail-only-expunge)
1684 (if (rmail-summary-exists)
1685 (rmail-select-summary
1686 (rmail-update-summary))))
1687 \f
1688 ;;;; *** Rmail Mailing Commands ***
1689
1690 (defun rmail-start-mail (&rest args)
1691 (if (and window-system rmail-mail-new-frame)
1692 (prog1
1693 (apply 'mail-other-frame args)
1694 (modify-frame-parameters (selected-frame)
1695 '((dedicated . t))))
1696 (apply 'mail-other-window args)))
1697
1698 (defun rmail-mail ()
1699 "Send mail in another window.
1700 While composing the message, use \\[mail-yank-original] to yank the
1701 original message into it."
1702 (interactive)
1703 (rmail-start-mail nil nil nil nil nil (current-buffer)))
1704
1705 (defun rmail-continue ()
1706 "Continue composing outgoing message previously being composed."
1707 (interactive)
1708 (rmail-start-mail t))
1709
1710 (defun rmail-reply (just-sender)
1711 "Reply to the current message.
1712 Normally include CC: to all other recipients of original message;
1713 prefix argument means ignore them. While composing the reply,
1714 use \\[mail-yank-original] to yank the original message into it."
1715 (interactive "P")
1716 (let (from reply-to cc subject date to message-id resent-reply-to)
1717 (save-excursion
1718 (save-restriction
1719 (widen)
1720 (goto-char (rmail-msgbeg rmail-current-message))
1721 (forward-line 1)
1722 (if (= (following-char) ?0)
1723 (narrow-to-region
1724 (progn (forward-line 2)
1725 (point))
1726 (progn (search-forward "\n\n" (rmail-msgend rmail-current-message)
1727 'move)
1728 (point)))
1729 (narrow-to-region (point)
1730 (progn (search-forward "\n*** EOOH ***\n")
1731 (beginning-of-line) (point))))
1732 (setq resent-reply-to (mail-fetch-field "resent-reply-to" t)
1733 from (mail-fetch-field "from")
1734 reply-to (or resent-reply-to
1735 (mail-fetch-field "reply-to" nil t)
1736 from)
1737 cc (cond (just-sender nil)
1738 (resent-reply-to (mail-fetch-field "resent-cc" t))
1739 (t (mail-fetch-field "cc" nil t)))
1740 subject (or (and resent-reply-to
1741 (mail-fetch-field "resent-subject" t))
1742 (mail-fetch-field "subject"))
1743 date (cond (resent-reply-to
1744 (mail-fetch-field "resent-date" t))
1745 ((mail-fetch-field "date")))
1746 to (cond (resent-reply-to
1747 (mail-fetch-field "resent-to" t))
1748 ((mail-fetch-field "to" nil t))
1749 ;((mail-fetch-field "apparently-to")) ack gag barf
1750 (t ""))
1751 message-id (cond (resent-reply-to
1752 (mail-fetch-field "resent-message-id" t))
1753 ((mail-fetch-field "message-id"))))))
1754 (and (stringp subject)
1755 (or (string-match (concat "\\`" (regexp-quote rmail-reply-prefix))
1756 subject)
1757 (setq subject (concat rmail-reply-prefix subject))))
1758 (rmail-start-mail nil
1759 (mail-strip-quoted-names reply-to)
1760 subject
1761 (rmail-make-in-reply-to-field from date message-id)
1762 (if just-sender
1763 nil
1764 (let* ((cc-list (rmail-dont-reply-to
1765 (mail-strip-quoted-names
1766 (if (null cc) to (concat to ", " cc))))))
1767 (if (string= cc-list "") nil cc-list)))
1768 (current-buffer)
1769 (list (list '(lambda (buf msgnum)
1770 (save-excursion
1771 (set-buffer buf)
1772 (rmail-set-attribute "answered" t msgnum)))
1773 (current-buffer) rmail-current-message)))))
1774
1775 (defun rmail-make-in-reply-to-field (from date message-id)
1776 (cond ((not from)
1777 (if message-id
1778 message-id
1779 nil))
1780 (mail-use-rfc822
1781 (require 'rfc822)
1782 (let ((tem (car (rfc822-addresses from))))
1783 (if message-id
1784 (if (string-match
1785 (regexp-quote (if (string-match "@[^@]*\\'" tem)
1786 (substring tem 0 (match-beginning 0))
1787 tem))
1788 message-id)
1789 ;; Message-ID is sufficiently informative
1790 message-id
1791 (concat message-id " (" tem ")"))
1792 ;; Use prin1 to fake RFC822 quoting
1793 (let ((field (prin1-to-string tem)))
1794 (if date
1795 (concat field "'s message of " date)
1796 field)))))
1797 ((let* ((foo "[^][\000-\037\177-\377()<>@,;:\\\" ]+")
1798 (bar "[^][\000-\037\177-\377()<>@,;:\\\"]+"))
1799 ;; Can't use format because format loses on \000 (unix *^&%*^&%$!!)
1800 (or (string-match (concat "\\`[ \t]*\\(" bar
1801 "\\)\\(<" foo "@" foo ">\\)?[ \t]*\\'")
1802 ;; "Unix Loser <Foo@bar.edu>" => "Unix Loser"
1803 from)
1804 (string-match (concat "\\`[ \t]*<" foo "@" foo ">[ \t]*(\\("
1805 bar "\\))[ \t]*\\'")
1806 ;; "<Bugs@bar.edu>" (Losing Unix) => "Losing Unix"
1807 from)))
1808 (let ((start (match-beginning 1))
1809 (end (match-end 1)))
1810 ;; Trim whitespace which above regexp match allows
1811 (while (and (< start end)
1812 (memq (aref from start) '(?\t ?\ )))
1813 (setq start (1+ start)))
1814 (while (and (< start end)
1815 (memq (aref from (1- end)) '(?\t ?\ )))
1816 (setq end (1- end)))
1817 (let ((field (substring from start end)))
1818 (if date (setq field (concat "message from " field " on " date)))
1819 (if message-id
1820 ;; "<AA259@bar.edu> (message from Unix Loser on 1-Apr-89)"
1821 (concat message-id " (" field ")")
1822 field))))
1823 (t
1824 ;; If we can't kludge it simply, do it correctly
1825 (let ((mail-use-rfc822 t))
1826 (rmail-make-in-reply-to-field from date message-id)))))
1827
1828 (defun rmail-forward (resend)
1829 "Forward the current message to another user.
1830 With prefix argument, \"resend\" the message instead of forwarding it;
1831 see the documentation of `rmail-resend'."
1832 (interactive "P")
1833 (if resend
1834 (call-interactively 'rmail-resend)
1835 (let ((forward-buffer (current-buffer))
1836 (subject (concat "["
1837 (let ((from (or (mail-fetch-field "From")
1838 (mail-fetch-field ">From"))))
1839 (if from
1840 (concat (mail-strip-quoted-names from) ": ")
1841 ""))
1842 (or (mail-fetch-field "Subject") "")
1843 "]")))
1844 ;; If only one window, use it for the mail buffer.
1845 ;; Otherwise, use another window for the mail buffer
1846 ;; so that the Rmail buffer remains visible
1847 ;; and sending the mail will get back to it.
1848 (if (funcall (if (and (not rmail-mail-new-frame) (one-window-p t))
1849 (function mail)
1850 (function rmail-start-mail))
1851 nil nil subject nil nil nil
1852 (list (list (function (lambda (buf msgnum)
1853 (save-excursion
1854 (set-buffer buf)
1855 (rmail-set-attribute
1856 "forwarded" t msgnum))))
1857 (current-buffer)
1858 rmail-current-message)))
1859 (save-excursion
1860 ;; Insert after header separator--before signature if any.
1861 (goto-char (point-min))
1862 (search-forward-regexp
1863 (concat "^" (regexp-quote mail-header-separator)))
1864 (forward-line 1)
1865 (insert-buffer forward-buffer))))))
1866
1867 (defun rmail-resend (address &optional from comment mail-alias-file)
1868 "Resend current message to ADDRESSES.
1869 ADDRESSES should be a single address, a string consisting of several
1870 addresses separated by commas, or a list of addresses.
1871
1872 Optional FROM is the address to resend the message from, and
1873 defaults to the username of the person redistributing the message.
1874 Optional COMMENT is a string that will be inserted as a comment in the
1875 resent message.
1876 Optional ALIAS-FILE is alternate aliases file to be used by sendmail,
1877 typically for purposes of moderating a list."
1878 (interactive "sResend to: ")
1879 (require 'sendmail)
1880 (require 'mailalias)
1881 (if (not from) (setq from (user-login-name)))
1882 (let ((tembuf (generate-new-buffer " sendmail temp"))
1883 (mail-header-separator "")
1884 (case-fold-search nil)
1885 (mailbuf (current-buffer)))
1886 (unwind-protect
1887 (save-excursion
1888 ;;>> Copy message into temp buffer
1889 (set-buffer tembuf)
1890 (insert-buffer-substring mailbuf)
1891 (goto-char (point-min))
1892 ;; Delete any Sender field, since that's not specifyable.
1893 ; Only delete Sender fields in the actual header.
1894 (re-search-forward "^$" nil 'move)
1895 ; Using "while" here rather than "if" because some buggy mail
1896 ; software may have inserted multiple Sender fields.
1897 (while (re-search-backward "^Sender:" nil t)
1898 (let (beg)
1899 (setq beg (point))
1900 (forward-line 1)
1901 (while (looking-at "[ \t]")
1902 (forward-line 1))
1903 (delete-region beg (point))))
1904 ; Go back to the beginning of the buffer so the Resent- fields
1905 ; are inserted there.
1906 (goto-char (point-min))
1907 ;;>> Insert resent-from:
1908 (insert "Resent-From: " from "\n")
1909 (insert "Resent-Date: " (mail-rfc822-date) "\n")
1910 ;;>> Insert resent-to: and bcc if need be.
1911 (let ((before (point)))
1912 (if mail-self-blind
1913 (insert "Resent-Bcc: " (user-login-name) "\n"))
1914 (insert "Resent-To: " (if (stringp address)
1915 address
1916 (mapconcat 'identity address ",\n\t"))
1917 "\n")
1918 (expand-mail-aliases before (point)))
1919 ;;>> Set up comment, if any.
1920 (if (and (sequencep comment) (not (zerop (length comment))))
1921 (let ((before (point))
1922 after)
1923 (insert comment)
1924 (or (eolp) (insert "\n"))
1925 (setq after (point))
1926 (goto-char before)
1927 (while (< (point) after)
1928 (insert "Resent-Comment: ")
1929 (forward-line 1))))
1930 ;; Don't expand aliases in the destination fields
1931 ;; of the original message.
1932 (let (mail-aliases)
1933 (funcall send-mail-function)))
1934 (kill-buffer tembuf))
1935 (rmail-set-attribute "resent" t rmail-current-message)))
1936
1937 (defvar mail-unsent-separator
1938 (concat "^ *---+ +Unsent message follows +---+ *$\\|"
1939 "^ *---+ +Returned message +---+ *$\\|"
1940 "^ *---+ +Original message +---+ *$\\|"
1941 "^ *--+ +begin message +--+ *$\\|"
1942 "^ *---+ +Original message follows +---+ *$\\|"
1943 "^|? *---+ +Message text follows: +---+ *|?$"))
1944
1945 (defun rmail-retry-failure ()
1946 "Edit a mail message which is based on the contents of the current message.
1947 For a message rejected by the mail system, extract the interesting headers and
1948 the body of the original message."
1949 (interactive)
1950 (require 'mail-utils)
1951 (let (to subj irp2 cc orig-message)
1952 (save-excursion
1953 ;; Narrow down to just the quoted original message
1954 (rmail-beginning-of-message)
1955 (let ((case-fold-search t))
1956 (or (re-search-forward mail-unsent-separator nil t)
1957 (error "Cannot parse this as a failure message")))
1958 (save-restriction
1959 (narrow-to-region (point) (point-max))
1960 ;; Now mail-fetch-field will get from headers of the original message,
1961 ;; not from the headers of the rejection.
1962 (setq to (mail-fetch-field "To")
1963 subj (mail-fetch-field "Subject")
1964 irp2 (mail-fetch-field "In-reply-to")
1965 cc (mail-fetch-field "Cc"))
1966 ;; Get the entire text (not headers) of the original message.
1967 (setq orig-message
1968 (buffer-substring
1969 (progn (search-forward "\n\n") (point))
1970 (point-max)))))
1971 ;; Start sending a new message; default header fields from the original.
1972 ;; Turn off the usual actions for initializing the message body
1973 ;; because we want to get only the text from the failure message.
1974 (let (mail-signature
1975 (mail-setup-hook rmail-retry-setup-hook))
1976 (if (rmail-start-mail nil to subj irp2 cc (current-buffer))
1977 ;; Insert original text as initial text of new draft message.
1978 (progn
1979 (goto-char (point-max))
1980 (insert orig-message)
1981 (goto-char (point-min))
1982 (end-of-line))))))
1983
1984 (defun rmail-bury ()
1985 "Bury current Rmail buffer and its summary buffer."
1986 (interactive)
1987 ;; This let var was called rmail-buffer, but that interfered
1988 ;; with the buffer-local var used in summary buffers.
1989 (let ((buffer-to-bury (current-buffer)))
1990 (if (rmail-summary-exists)
1991 (let (window)
1992 (while (setq window (get-buffer-window rmail-summary-buffer))
1993 (set-window-buffer window (other-buffer rmail-summary-buffer)))
1994 (bury-buffer rmail-summary-buffer)))
1995 (switch-to-buffer (other-buffer (current-buffer)))
1996 (bury-buffer buffer-to-bury)))
1997
1998 (defun rmail-summary-exists ()
1999 "Non-nil iff in an RMAIL buffer and an associated summary buffer exists.
2000 Non-nil value returned is the summary buffer."
2001 (and rmail-summary-buffer (buffer-name rmail-summary-buffer)
2002 rmail-summary-buffer))
2003
2004 (defun rmail-summary-displayed ()
2005 "t iff in RMAIL buffer and an associated summary buffer is displayed."
2006 (and rmail-summary-buffer (get-buffer-window rmail-summary-buffer)))
2007
2008 (defvar rmail-redisplay-summary nil
2009 "*Non-nil means Rmail should show the summary when it changes.
2010 This has an effect only if a summary buffer exists.")
2011
2012 ;; Put the summary buffer back on the screen, if user wants that.
2013 (defun rmail-maybe-display-summary ()
2014 (and rmail-summary-buffer (buffer-name rmail-summary-buffer)
2015 rmail-redisplay-summary
2016 (display-buffer rmail-summary-buffer)))
2017 \f
2018 ;;;; *** Rmail Specify Inbox Files ***
2019
2020 (autoload 'set-rmail-inbox-list "rmailmsc"
2021 "Set the inbox list of the current RMAIL file to FILE-NAME.
2022 This may be a list of file names separated by commas.
2023 If FILE-NAME is empty, remove any inbox list."
2024 t)
2025
2026 ;;;; *** Rmail Commands for Labels ***
2027
2028 (autoload 'rmail-add-label "rmailkwd"
2029 "Add LABEL to labels associated with current RMAIL message.
2030 Completion is performed over known labels when reading."
2031 t)
2032
2033 (autoload 'rmail-kill-label "rmailkwd"
2034 "Remove LABEL from labels associated with current RMAIL message.
2035 Completion is performed over known labels when reading."
2036 t)
2037
2038 (autoload 'rmail-next-labeled-message "rmailkwd"
2039 "Show next message with LABEL. Defaults to last label used.
2040 With prefix argument N moves forward N messages with this label."
2041 t)
2042
2043 (autoload 'rmail-previous-labeled-message "rmailkwd"
2044 "Show previous message with LABEL. Defaults to last label used.
2045 With prefix argument N moves backward N messages with this label."
2046 t)
2047
2048 (autoload 'rmail-read-label "rmailkwd"
2049 "PROMPT and read with completion an Rmail message label."
2050 t)
2051
2052 ;;;; *** Rmail Edit Mode ***
2053
2054 (autoload 'rmail-edit-current-message "rmailedit"
2055 "Edit the contents of the current message"
2056 t)
2057
2058 ;;;; *** Rmail Sorting ***
2059
2060 (autoload 'rmail-sort-by-date "rmailsort"
2061 "Sort messages of current Rmail file by date.
2062 If prefix argument REVERSE is non-nil, sort them in reverse order." t)
2063
2064 (autoload 'rmail-sort-by-subject "rmailsort"
2065 "Sort messages of current Rmail file by subject.
2066 If prefix argument REVERSE is non-nil, sort them in reverse order." t)
2067
2068 (autoload 'rmail-sort-by-author "rmailsort"
2069 "Sort messages of current Rmail file by author.
2070 If prefix argument REVERSE is non-nil, sort them in reverse order." t)
2071
2072 (autoload 'rmail-sort-by-recipient "rmailsort"
2073 "Sort messages of current Rmail file by recipient.
2074 If prefix argument REVERSE is non-nil, sort them in reverse order." t)
2075
2076 (autoload 'rmail-sort-by-correspondent "rmailsort"
2077 "Sort messages of current Rmail file by other correspondent.
2078 If prefix argument REVERSE is non-nil, sort them in reverse order." t)
2079
2080 (autoload 'rmail-sort-by-lines "rmailsort"
2081 "Sort messages of current Rmail file by number of lines.
2082 If prefix argument REVERSE is non-nil, sort them in reverse order." t)
2083 \f
2084 ;;;; *** Rmail Summary Mode ***
2085
2086 (autoload 'rmail-summary "rmailsum"
2087 "Display a summary of all messages, one line per message."
2088 t)
2089
2090 (autoload 'rmail-summary-by-labels "rmailsum"
2091 "Display a summary of all messages with one or more LABELS.
2092 LABELS should be a string containing the desired labels, separated by commas."
2093 t)
2094
2095 (autoload 'rmail-summary-by-recipients "rmailsum"
2096 "Display a summary of all messages with the given RECIPIENTS.
2097 Normally checks the To, From and Cc fields of headers; but if PRIMARY-ONLY
2098 is non-nil (prefix arg given), only look in the To and From fields.
2099 RECIPIENTS is a string of regexps separated by commas."
2100 t)
2101
2102 (autoload 'rmail-summary-by-regexp "rmailsum"
2103 "Display a summary of all messages according to regexp REGEXP.
2104 If the regular expression is found in the header of the message
2105 \(including in the date and other lines, as well as the subject line),
2106 Emacs will list the header line in the RMAIL-summary."
2107 t)
2108
2109 (autoload 'rmail-summary-by-topic "rmailsum"
2110 "Display a summary of all messages with the given SUBJECT.
2111 Normally checks the Subject field of headers;
2112 but if WHOLE-MESSAGE is non-nil (prefix arg given),
2113 look in the whole message.
2114 SUBJECT is a string of regexps separated by commas."
2115 t)
2116 \f
2117 ;;;; *** Rmail output messages to files ***
2118
2119 (autoload 'rmail-output-to-rmail-file "rmailout"
2120 "Append the current message to an Rmail file named FILE-NAME.
2121 If the file does not exist, ask if it should be created.
2122 If file is being visited, the message is appended to the Emacs
2123 buffer visiting that file."
2124 t)
2125
2126 (autoload 'rmail-output "rmailout"
2127 "Append this message to Unix mail file named FILE-NAME."
2128 t)
2129
2130 ;;;; *** Rmail undigestification ***
2131
2132 (autoload 'undigestify-rmail-message "undigest"
2133 "Break up a digest message into its constituent messages.
2134 Leaves original message, deleted, before the undigestified messages."
2135 t)
2136
2137 (provide 'rmail)
2138
2139 ;;; rmail.el ends here