*** empty log message ***
[emacs.git] / lisp / mail / rmail.el
blobc45f7552aae41f5e3aedd228eb26625aa6aa7822
1 ;; "RMAIL" mail reader for Emacs.
2 ;; Copyright (C) 1985, 1986, 1987, 1988, 1991, 1992 Free Software Foundation, Inc.
4 ;; This file is part of GNU Emacs.
6 ;; GNU Emacs is free software; you can redistribute it and/or modify
7 ;; it under the terms of the GNU General Public License as published by
8 ;; the Free Software Foundation; either version 1, or (at your option)
9 ;; any later version.
11 ;; GNU Emacs is distributed in the hope that it will be useful,
12 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;; GNU General Public License for more details.
16 ;; You should have received a copy of the GNU General Public License
17 ;; along with GNU Emacs; see the file COPYING. If not, write to
18 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
21 ;; Souped up by shane@mit-ajax based on ideas of rlk@athena.mit.edu
22 ;; New features include attribute and keyword support, message
23 ;; selection by dispatch table, summary by attributes and keywords,
24 ;; expunging by dispatch table, sticky options for file commands.
26 (require 'mail-utils)
27 ; These variables now declared paths.el
28 ;(defvar rmail-spool-directory "/usr/spool/mail/"
29 ; "This is the name of the directory used by the system mailer for\n\
30 ;delivering new mail. It's name should end with a slash.")
31 ;(defvar rmail-file-name
32 ; (expand-file-name "~/RMAIL")
33 ; "")
35 ;;;###autoload
36 (defvar rmail-dont-reply-to-names nil "\
37 *A regexp specifying names to prune of reply to messages.
38 nil means dont reply to yourself.")
40 ;;;###autoload
41 (defvar rmail-default-dont-reply-to-names "info-" "\
42 A regular expression specifying part of the value of the default value of
43 the variable `rmail-dont-reply-to-names', for when the user does not set
44 `rmail-dont-reply-to-names' explicitly. (The other part of the default
45 value is the user's name.)
46 It is useful to set this variable in the site customisation file.")
48 ;;;###autoload
49 (defvar rmail-ignored-headers "^via:\\|^mail-from:\\|^origin:\\|^status:\\|^received:\\|^message-id:\\|^summary-line:" "\
50 *Gubbish headers one would rather not see.")
52 ;;;###autoload
53 (defvar rmail-delete-after-output nil "\
54 *Non-nil means automatically delete a message that is copied to a file.")
56 ;;;###autoload
57 (defconst rmail-primary-inbox-list nil "\
58 *List of files which are inboxes for user's primary mail file ~/RMAIL.
59 `nil' means the default, which is (\"/usr/spool/mail/$USER\" \"~/mbox\")
60 \(the first name varies depending on the operating system,
61 and the value of the environment variable MAIL overrides it).")
63 ;; these may be altered by site-init.el to match the format of mmdf files
64 ;; delimitation used on a given host (delim1 and delim2 from the config
65 ;; files)
67 (defvar mmdf-delim1 "^\001\001\001\001\n"
68 "Regexp marking the start of an mmdf message")
69 (defvar mmdf-delim2 "^\001\001\001\001\n"
70 "Regexp marking the end of an mmdf message")
72 (defvar rmail-message-filter nil
73 "If non nil, is a filter function for new headers in RMAIL.
74 Called with region narrowed to unformatted header.")
76 (defvar rmail-mode-map nil)
78 (defvar rmail-inbox-list nil)
79 (defvar rmail-keywords nil)
81 ;; Message counters and markers. Deleted flags.
83 (defvar rmail-current-message nil)
84 (defvar rmail-total-messages nil)
85 (defvar rmail-message-vector nil)
86 (defvar rmail-deleted-vector nil)
88 ;; These are used by autoloaded rmail-summary.
90 (defvar rmail-summary-buffer nil)
91 (defvar rmail-summary-vector nil)
93 ;; `Sticky' default variables.
95 ;; Last individual label specified to a or k.
96 (defvar rmail-last-label nil)
97 ;; Last set of labels specified to C-M-n or C-M-p or C-M-l.
98 (defvar rmail-last-multi-labels nil)
99 (defvar rmail-last-file nil)
100 (defvar rmail-last-rmail-file nil)
102 ;;; Regexp matching the delimiter of messages in UNIX mail format
103 ;;; (UNIX From lines), minus the initial ^. Note that if you change
104 ;;; this expression, you must change the code in rmail-nuke-pinhead-header
105 ;;; that knows the exact ordering of the \\( \\) subexpressions.
106 (defvar rmail-unix-mail-delimiter
107 "From \\([^ \n]*\\(\\|\".*\"[^ \n]*\\)\\) ?\\([^ \n]*\\) \\([^ ]*\\) *\\([0-9]*\\) \\([0-9:]*\\)\\( ?[A-Z]?[A-Z][A-Z]T\\( DST\\)?\\| ?[-+]?[0-9][0-9][0-9][0-9]\\|\\) [0-9][0-9]\\([0-9]*\\) *\\(remote from [^\n]*\\)?\n" nil)
109 ;;;; *** Rmail Mode ***
111 ;;;###autoload
112 (defun rmail (&optional file-name-arg)
113 "Read and edit incoming mail.
114 Moves messages into file named by rmail-file-name (a babyl format file)
115 and edits that file in RMAIL Mode.
116 Type \\[describe-mode] once editing that file, for a list of RMAIL commands.
118 May be called with filename as argument; then performs rmail editing on
119 that file, but does not copy any new mail into the file."
120 (interactive (if current-prefix-arg
121 (list (read-file-name "Run rmail on RMAIL file: "
122 nil nil t))))
123 (or rmail-last-file
124 (setq rmail-last-file (expand-file-name "~/xmail")))
125 (or rmail-last-rmail-file
126 (setq rmail-last-rmail-file (expand-file-name "~/XMAIL")))
127 (let* ((file-name (expand-file-name (or file-name-arg rmail-file-name)))
128 (existed (get-file-buffer file-name)))
129 ;; Like find-file, but in the case where a buffer existed
130 ;; and the file was reverted, recompute the message-data.
131 (if (and existed (not (verify-visited-file-modtime existed)))
132 (progn
133 ;; Don't be confused by apparent local-variables spec
134 ;; in the last message in the RMAIL file.
135 (let ((inhibit-local-variables t))
136 (find-file file-name))
137 (if (verify-visited-file-modtime existed)
138 (progn (rmail-forget-messages)
139 (rmail-set-message-counters))))
140 (let ((inhibit-local-variables t))
141 (find-file file-name)))
142 (if (and existed (> (buffer-size) 0))
143 ;; Buffer not new and not empty; ensure in proper mode, but that's all.
144 (or (eq major-mode 'rmail-mode)
145 (rmail-mode-2))
146 (rmail-mode-2)
147 ;; Provide default set of inboxes for primary mail file ~/RMAIL.
148 (and (null rmail-inbox-list)
149 (null file-name-arg)
150 (setq rmail-inbox-list
151 (or rmail-primary-inbox-list
152 (list "~/mbox"
153 (or (getenv "MAIL")
154 (concat rmail-spool-directory
155 (user-original-login-name)))))))
156 ;; Convert all or part to Babyl file if possible.
157 (rmail-convert-file)
158 (goto-char (point-max))
159 (if (null rmail-inbox-list)
160 (progn
161 (rmail-set-message-counters)
162 (rmail-show-message))))
163 (rmail-get-new-mail)
164 ;; Show the first unseen message, which might be from a previous session
165 ;; or might have been just read in by rmail-get-new-mail.
166 (rmail-first-unseen-message)))
168 ;; Given the value of MAILPATH, return a list of inbox file names.
169 ;; This is turned off because it is not clear that the user wants
170 ;; all these inboxes to feed into the primary rmail file.
171 ; (defun rmail-convert-mailpath (string)
172 ; (let (idx list)
173 ; (while (setq idx (string-match "[%:]" string))
174 ; (let ((this (substring string 0 idx)))
175 ; (setq string (substring string (1+ idx)))
176 ; (setq list (cons (if (string-match "%" this)
177 ; (substring this 0 (string-match "%" this))
178 ; this)
179 ; list))))
180 ; list))
182 ; I have checked that adding "-*- rmail -*-" to the BABYL OPTIONS line
183 ; will not cause emacs 18.55 problems.
185 (defun rmail-convert-file ()
186 (let (convert)
187 (widen)
188 (goto-char (point-min))
189 ;; If file doesn't start like a Babyl file,
190 ;; convert it to one, by adding a header and converting each message.
191 (cond ((looking-at "BABYL OPTIONS:"))
192 ((looking-at "Version: 5\n")
193 ;; Losing babyl file made by old version of Rmail.
194 ;; Just fix the babyl file header; don't make a new one,
195 ;; so we don't lose the Labels: file attribute, etc.
196 (let ((buffer-read-only nil))
197 (insert "BABYL OPTIONS: -*- rmail -*-\n")))
199 (setq convert t)
200 (rmail-insert-rmail-file-header)))
201 ;; If file was not a Babyl file or if there are
202 ;; Unix format messages added at the end,
203 ;; convert file as necessary.
204 (if (or convert
205 (progn (goto-char (point-max))
206 (search-backward "\^_")
207 (forward-char 1)
208 (looking-at "\n*From ")))
209 (let ((buffer-read-only nil))
210 (message "Converting to Babyl format...")
211 (narrow-to-region (point) (point-max))
212 (rmail-convert-to-babyl-format)
213 (message "Converting to Babyl format...done")))))
215 ; I have checked that adding "-*- rmail -*-" to the BABYL OPTIONS line
216 ; will not cause emacs 18.55 problems.
218 (defun rmail-insert-rmail-file-header ()
219 (let ((buffer-read-only nil))
220 (insert "BABYL OPTIONS: -*- rmail -*-
221 Version: 5
222 Labels:
223 Note: This is the header of an rmail file.
224 Note: If you are seeing it in rmail,
225 Note: it means the file has no messages in it.\n\^_")))
227 (if rmail-mode-map
229 (setq rmail-mode-map (make-keymap))
230 (suppress-keymap rmail-mode-map)
231 (define-key rmail-mode-map "." 'rmail-beginning-of-message)
232 (define-key rmail-mode-map " " 'scroll-up)
233 (define-key rmail-mode-map "\177" 'scroll-down)
234 (define-key rmail-mode-map "n" 'rmail-next-undeleted-message)
235 (define-key rmail-mode-map "p" 'rmail-previous-undeleted-message)
236 (define-key rmail-mode-map "\en" 'rmail-next-message)
237 (define-key rmail-mode-map "\ep" 'rmail-previous-message)
238 (define-key rmail-mode-map "\e\C-n" 'rmail-next-labeled-message)
239 (define-key rmail-mode-map "\e\C-p" 'rmail-previous-labeled-message)
240 (define-key rmail-mode-map "a" 'rmail-add-label)
241 (define-key rmail-mode-map "k" 'rmail-kill-label)
242 (define-key rmail-mode-map "d" 'rmail-delete-forward)
243 (define-key rmail-mode-map "u" 'rmail-undelete-previous-message)
244 (define-key rmail-mode-map "x" 'rmail-expunge)
245 (define-key rmail-mode-map "s" 'rmail-expunge-and-save)
246 (define-key rmail-mode-map "g" 'rmail-get-new-mail)
247 (define-key rmail-mode-map "h" 'rmail-summary)
248 (define-key rmail-mode-map "\e\C-h" 'rmail-summary)
249 (define-key rmail-mode-map "l" 'rmail-summary-by-labels)
250 (define-key rmail-mode-map "\e\C-l" 'rmail-summary-by-labels)
251 (define-key rmail-mode-map "\e\C-r" 'rmail-summary-by-recipients)
252 (define-key rmail-mode-map "\e\C-s" 'rmail-summary-by-regexp)
253 (define-key rmail-mode-map "t" 'rmail-toggle-header)
254 (define-key rmail-mode-map "m" 'rmail-mail)
255 (define-key rmail-mode-map "r" 'rmail-reply)
256 (define-key rmail-mode-map "\e\C-m" 'rmail-retry-failure)
257 (define-key rmail-mode-map "c" 'rmail-continue)
258 (define-key rmail-mode-map "f" 'rmail-forward)
259 (define-key rmail-mode-map "\es" 'rmail-search)
260 (define-key rmail-mode-map "<" 'rmail-first-message)
261 (define-key rmail-mode-map ">" 'rmail-last-message)
262 (define-key rmail-mode-map "j" 'rmail-show-message)
263 (define-key rmail-mode-map "o" 'rmail-output-to-rmail-file)
264 (define-key rmail-mode-map "\C-o" 'rmail-output)
265 (define-key rmail-mode-map "i" 'rmail-input)
266 (define-key rmail-mode-map "q" 'rmail-quit)
267 (define-key rmail-mode-map "?" 'describe-mode)
268 (define-key rmail-mode-map "w" 'rmail-edit-current-message)
269 (define-key rmail-mode-map "e" 'rmail-edit-current-message)
270 (define-key rmail-mode-map "\C-d" 'rmail-delete-backward))
272 ;; Rmail mode is suitable only for specially formatted data.
273 (put 'rmail-mode 'mode-class 'special)
275 (defun rmail-mode ()
276 "Rmail Mode is used by \\<rmail-mode-map>\\[rmail] for editing Rmail files.
277 All normal editing commands are turned off.
278 Instead, these commands are available:
280 \\[rmail-beginning-of-message] Move point to front of this message (same as \\[beginning-of-buffer]).
281 \\[scroll-up] Scroll to next screen of this message.
282 \\[scroll-down] Scroll to previous screen of this message.
283 \\[rmail-next-undeleted-message] Move to Next non-deleted message.
284 \\[rmail-previous-undeleted-message] Move to Previous non-deleted message.
285 \\[rmail-next-message] Move to Next message whether deleted or not.
286 \\[rmail-previous-message] Move to Previous message whether deleted or not.
287 \\[rmail-first-message] Move to the first message in Rmail file.
288 \\[rmail-last-message] Move to the last message in Rmail file.
289 \\[rmail-show-message] Jump to message specified by numeric position in file.
290 \\[rmail-search] Search for string and show message it is found in.
291 \\[rmail-delete-forward] Delete this message, move to next nondeleted.
292 \\[rmail-delete-backward] Delete this message, move to previous nondeleted.
293 \\[rmail-undelete-previous-message] Undelete message. Tries current message, then earlier messages
294 till a deleted message is found.
295 \\[rmail-expunge] Expunge deleted messages.
296 \\[rmail-expunge-and-save] Expunge and save the file.
297 \\[rmail-quit] Quit Rmail: expunge, save, then switch to another buffer.
298 \\[save-buffer] Save without expunging.
299 \\[rmail-get-new-mail] Move new mail from system spool directory or mbox into this file.
300 \\[rmail-mail] Mail a message (same as \\[mail-other-window]).
301 \\[rmail-continue] Continue composing outgoing message started before.
302 \\[rmail-reply] Reply to this message. Like m but initializes some fields.
303 \\[rmail-retry-failure] Send this message again. Used on a mailer failure message.
304 \\[rmail-forward] Forward this message to another user.
305 \\[rmail-output-to-rmail-file] Output this message to an Rmail file (append it).
306 \\[rmail-output] Output this message to a Unix-format mail file (append it).
307 \\[rmail-input] Input Rmail file. Run Rmail on that file.
308 \\[rmail-add-label] Add label to message. It will be displayed in the mode line.
309 \\[rmail-kill-label] Kill label. Remove a label from current message.
310 \\[rmail-next-labeled-message] Move to Next message with specified label
311 (label defaults to last one specified).
312 Standard labels: filed, unseen, answered, forwarded, deleted.
313 Any other label is present only if you add it with `a'.
314 \\[rmail-previous-labeled-message] Move to Previous message with specified label
315 \\[rmail-summary] Show headers buffer, with a one line summary of each message.
316 \\[rmail-summary-by-labels] Like h only just messages with particular label(s) are summarized.
317 \\[rmail-summary-by-recipients] Like h only just messages with particular recipient(s) are summarized.
318 \\[rmail-toggle-header] Toggle header, show Rmail header if unformatted or vice versa.
319 \\[rmail-edit-current-message] Edit the current message. \\[rmail-cease-edit] to return to Rmail."
320 (interactive)
321 (rmail-mode-2)
322 (rmail-set-message-counters)
323 (rmail-show-message))
325 (defun rmail-mode-2 ()
326 (kill-all-local-variables)
327 (rmail-mode-1)
328 (rmail-variables)
329 (run-hooks 'rmail-mode-hook))
331 (defun rmail-mode-1 ()
332 (setq major-mode 'rmail-mode)
333 (setq mode-name "RMAIL")
334 (setq buffer-read-only t)
335 ;; No need to auto save RMAIL files.
336 (setq buffer-auto-save-file-name nil)
337 (if (boundp 'mode-line-modified)
338 (setq mode-line-modified "--- ")
339 (setq mode-line-format
340 (cons "--- " (cdr (default-value 'mode-line-format)))))
341 (use-local-map rmail-mode-map)
342 (set-syntax-table text-mode-syntax-table)
343 (setq local-abbrev-table text-mode-abbrev-table))
345 (defun rmail-variables ()
346 (make-local-variable 'revert-buffer-function)
347 (setq revert-buffer-function 'rmail-revert)
348 (make-local-variable 'rmail-last-label)
349 (make-local-variable 'rmail-deleted-vector)
350 (make-local-variable 'rmail-summary-buffer)
351 (make-local-variable 'rmail-summary-vector)
352 (make-local-variable 'rmail-current-message)
353 (make-local-variable 'rmail-total-messages)
354 (make-local-variable 'require-final-newline)
355 (setq require-final-newline nil)
356 (make-local-variable 'version-control)
357 (setq version-control 'never)
358 (make-local-variable 'file-precious-flag)
359 (setq file-precious-flag t)
360 (make-local-variable 'rmail-message-vector)
361 (make-local-variable 'rmail-last-file)
362 (make-local-variable 'rmail-inbox-list)
363 (setq rmail-inbox-list (rmail-parse-file-inboxes))
364 (make-local-variable 'rmail-keywords)
365 ;; this gets generated as needed
366 (setq rmail-keywords nil))
368 ;; Handle M-x revert-buffer done in an rmail-mode buffer.
369 (defun rmail-revert (arg noconfirm)
370 (let (revert-buffer-function)
371 ;; Call our caller again, but this time it does the default thing.
372 (if (revert-buffer arg noconfirm)
373 ;; If the user said "yes", and we changed something,
374 ;; reparse the messages.
375 (progn
376 (rmail-convert-file)
377 (goto-char (point-max))
378 (rmail-set-message-counters)
379 (rmail-show-message)))))
381 ;; Return a list of files from this buffer's Mail: option.
382 ;; Does not assume that messages have been parsed.
383 ;; Just returns nil if buffer does not look like Babyl format.
384 (defun rmail-parse-file-inboxes ()
385 (save-excursion
386 (save-restriction
387 (widen)
388 (goto-char 1)
389 (cond ((looking-at "BABYL OPTIONS:")
390 (search-forward "\n\^_" nil 'move)
391 (narrow-to-region 1 (point))
392 (goto-char 1)
393 (if (search-forward "\nMail:" nil t)
394 (progn
395 (narrow-to-region (point) (progn (end-of-line) (point)))
396 (goto-char (point-min))
397 (mail-parse-comma-list))))))))
399 (defun rmail-expunge-and-save ()
400 "Expunge and save RMAIL file."
401 (interactive)
402 (rmail-expunge)
403 (save-buffer))
405 (defun rmail-quit ()
406 "Quit out of RMAIL."
407 (interactive)
408 (rmail-expunge-and-save)
409 ;; Don't switch to the summary buffer even if it was recently visible.
410 (if rmail-summary-buffer
411 (bury-buffer rmail-summary-buffer))
412 (let ((obuf (current-buffer)))
413 (switch-to-buffer (other-buffer))
414 (bury-buffer obuf)))
416 ;;;###autoload
417 (defun rmail-input (filename)
418 "Run RMAIL on file FILENAME."
419 (interactive "FRun rmail on RMAIL file: ")
420 (rmail filename))
423 ;;;; *** Rmail input ***
425 ;; RLK feature not added in this version:
426 ;; argument specifies inbox file or files in various ways.
428 (defun rmail-get-new-mail (&optional file-name)
429 "Move any new mail from this RMAIL file's inbox files.
430 The inbox files can be specified with the file's Mail: option. The
431 variable `rmail-primary-inbox-list' specifies the inboxes for your
432 primary RMAIL file if it has no Mail: option. These are normally your
433 ~/mbox and your /usr/spool/mail/$USER.
435 You can also specify the file to get new mail from. In this case, the
436 file of new mail is not changed or deleted. Noninteractively, you can
437 pass the inbox file name as an argument. Interactively, a prefix
438 argument causes us to read a file name and use that file as the inbox."
439 (interactive
440 (list (if current-prefix-arg
441 (read-file-name "Get new mail from file: "))))
442 (or (verify-visited-file-modtime (current-buffer))
443 (progn
444 (find-file (buffer-file-name))
445 (if (verify-visited-file-modtime (current-buffer))
446 (rmail-forget-messages))))
447 (rmail-maybe-set-message-counters)
448 (widen)
449 ;; Get rid of all undo records for this buffer.
450 (or (eq buffer-undo-list t)
451 (setq buffer-undo-list nil))
452 (unwind-protect
453 (let ((opoint (point))
454 (new-messages 0)
455 (delete-files ())
456 ;; If buffer has not changed yet, and has not been saved yet,
457 ;; don't replace the old backup file now.
458 (make-backup-files (and make-backup-files (buffer-modified-p)))
459 (buffer-read-only nil)
460 ;; Don't make undo records for what we do in getting mail.
461 (buffer-undo-list t))
462 (goto-char (point-max))
463 (skip-chars-backward " \t\n") ; just in case of brain damage
464 (delete-region (point) (point-max)) ; caused by require-final-newline
465 (save-excursion
466 (save-restriction
467 (narrow-to-region (point) (point))
468 ;; Read in the contents of the inbox files,
469 ;; renaming them as necessary,
470 ;; and adding to the list of files to delete eventually.
471 (if file-name
472 (rmail-insert-inbox-text (list file-name) nil)
473 (setq delete-files (rmail-insert-inbox-text rmail-inbox-list t)))
474 ;; Scan the new text and convert each message to babyl format.
475 (goto-char (point-min))
476 (save-excursion
477 (setq new-messages (rmail-convert-to-babyl-format)))
478 (or (zerop new-messages)
479 (let (success)
480 (widen)
481 (search-backward "\n\^_")
482 (narrow-to-region (point) (point-max))
483 (goto-char (1+ (point-min)))
484 (rmail-count-new-messages)
485 (save-buffer)))
486 ;; Delete the old files, now that babyl file is saved.
487 (while delete-files
488 (condition-case ()
489 (delete-file (car delete-files))
490 (file-error nil))
491 (setq delete-files (cdr delete-files)))))
492 (if (= new-messages 0)
493 (progn (goto-char opoint)
494 (if (or file-name rmail-inbox-list)
495 (message "(No new mail has arrived)")))
496 (message "%d new message%s read"
497 new-messages (if (= 1 new-messages) "" "s"))
498 (and (boundp 'display-time-string)
499 (string-match " Mail" display-time-string)
500 (setq display-time-string
501 (concat
502 (substring display-time-string 0 (match-beginning 0))
503 (substring display-time-string (match-end 0))))
504 (force-mode-line-update 'all))))
505 ;; Don't leave the buffer screwed up if we get a disk-full error.
506 (rmail-show-message)))
508 (defun rmail-insert-inbox-text (files renamep)
509 (let (file tofile delete-files movemail)
510 (while files
511 (setq file (expand-file-name (substitute-in-file-name (car files)))
512 ;;>> un*x specific <<
513 tofile (concat file "~"))
514 ;; If getting from mail spool directory,
515 ;; use movemail to move rather than just renaming,
516 ;; so as to interlock with the mailer.
517 (setq movemail (equal (file-name-directory file) rmail-spool-directory))
518 (if movemail
519 (progn
520 (setq tofile (expand-file-name
521 ;; Generate name to move to from inbox name,
522 ;; in case of multiple inboxes that need moving.
523 (concat ".newmail-" (file-name-nondirectory file))
524 (file-name-directory
525 (expand-file-name rmail-file-name))))
526 ;; On some systems, /usr/spool/mail/foo is a directory
527 ;; and the actual inbox is /usr/spool/mail/foo/foo.
528 (if (file-directory-p file)
529 (setq file (expand-file-name (user-original-login-name)
530 file)))))
531 (if (or (file-exists-p tofile) (file-exists-p file))
532 (message "Getting mail from %s..." file))
533 ;; Set TOFILE if have not already done so, and
534 ;; rename or copy the file FILE to TOFILE if and as appropriate.
535 (cond ((not renamep)
536 (setq tofile file))
537 ((or (file-exists-p tofile) (not (file-exists-p file)))
538 nil)
539 ((not movemail)
540 (rename-file file tofile nil))
542 (let ((errors nil))
543 (unwind-protect
544 (save-excursion
545 (setq errors (generate-new-buffer " *rmail loss*"))
546 (buffer-disable-undo errors)
547 (call-process
548 (expand-file-name "movemail" exec-directory)
549 nil errors nil file tofile)
550 (if (not (buffer-modified-p errors))
551 ;; No output => movemail won
553 (set-buffer errors)
554 (subst-char-in-region (point-min) (point-max)
555 ?\n ?\ )
556 (goto-char (point-max))
557 (skip-chars-backward " \t")
558 (delete-region (point) (point-max))
559 (goto-char (point-min))
560 (if (looking-at "movemail: ")
561 (delete-region (point-min) (match-end 0)))
562 (beep t)
563 (message (concat "movemail: "
564 (buffer-substring (point-min)
565 (point-max))))
566 (sit-for 3)
567 nil))
568 (if errors (kill-buffer errors))))))
569 ;; At this point, TOFILE contains the name to read:
570 ;; Either the alternate name (if we renamed)
571 ;; or the actual inbox (if not renaming).
572 (if (file-exists-p tofile)
573 (let (size)
574 (goto-char (point-max))
575 (setq size (nth 1 (insert-file-contents tofile)))
576 (goto-char (point-max))
577 (or (= (preceding-char) ?\n)
578 (zerop size)
579 (insert ?\n))
580 (setq delete-files (cons tofile delete-files))))
581 (message "")
582 (setq files (cdr files)))
583 delete-files))
585 ;; the rmail-break-forwarded-messages feature is not implemented
586 (defun rmail-convert-to-babyl-format ()
587 (let ((count 0) start
588 (case-fold-search nil)
589 (invalid-input-resync
590 (function (lambda ()
591 (message "Invalid Babyl format in inbox!")
592 (sit-for 1)
593 ;; Try to get back in sync with a real message.
594 (if (re-search-forward
595 (concat mmdf-delim1 "\\|^From") nil t)
596 (beginning-of-line)
597 (goto-char (point-max)))))))
598 (goto-char (point-min))
599 (save-restriction
600 (while (not (eobp))
601 (cond ((looking-at "BABYL OPTIONS:");Babyl header
602 (if (search-forward "\n\^_" nil t)
603 ;; If we find the proper terminator, delete through there.
604 (delete-region (point-min) (point))
605 (funcall invalid-input-resync)
606 (delete-region (point-min) (point))))
607 ;; Babyl format message
608 ((looking-at "\^L")
609 (or (search-forward "\n\^_" nil t)
610 (funcall invalid-input-resync))
611 (setq count (1+ count))
612 ;; Make sure there is no extra white space after the ^_
613 ;; at the end of the message.
614 ;; Narrowing will make sure that whatever follows the junk
615 ;; will be treated properly.
616 (delete-region (point)
617 (save-excursion
618 (skip-chars-forward " \t\n")
619 (point)))
620 (narrow-to-region (point) (point-max)))
621 ;;*** MMDF format
622 ((let ((case-fold-search t))
623 (looking-at mmdf-delim1))
624 (let ((case-fold-search t))
625 (replace-match "\^L\n0, unseen,,\n*** EOOH ***\n")
626 (setq start (point))
627 (re-search-forward mmdf-delim2 nil t)
628 (replace-match "\^_"))
629 (save-excursion
630 (save-restriction
631 (narrow-to-region start (1- (point)))
632 (goto-char (point-min))
633 (while (search-forward "\n\^_" nil t); single char "\^_"
634 (replace-match "\n^_")))); 2 chars: "^" and "_"
635 (narrow-to-region (point) (point-max))
636 (setq count (1+ count)))
637 ;;*** Mail format
638 ((looking-at "^From ")
639 (setq start (point))
640 (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
641 (rmail-nuke-pinhead-header)
642 (if (re-search-forward
643 (concat "^[\^_]?\\("
644 rmail-unix-mail-delimiter
645 "\\|"
646 mmdf-delim1 "\\|"
647 "^BABYL OPTIONS:\\|"
648 "\^L\n[01],\\)") nil t)
649 (goto-char (match-beginning 1))
650 (goto-char (point-max)))
651 (setq count (1+ count))
652 (save-excursion
653 (save-restriction
654 (narrow-to-region start (point))
655 (goto-char (point-min))
656 (while (search-forward "\n\^_" nil t); single char
657 (replace-match "\n^_")))); 2 chars: "^" and "_"
658 (insert ?\^_)
659 (narrow-to-region (point) (point-max)))
661 ;;This is a kludge, in case we're wrong about mmdf not
662 ;;allowing anything in between. If it loses, we'll have
663 ;;to look for something else
664 (t (error "Cannot convert to babyl format")))))
665 count))
667 ;; Delete the "From ..." line, creating various other headers with
668 ;; information from it if they don't already exist. Now puts the
669 ;; original line into a mail-from: header line for debugging.
670 (defun rmail-nuke-pinhead-header ()
671 (save-excursion
672 (save-restriction
673 (let ((start (point))
674 (end (progn
675 (condition-case ()
676 (search-forward "\n\n")
677 (error
678 (goto-char (point-max))
679 (insert "\n\n")))
680 (point)))
681 has-from has-date)
682 (narrow-to-region start end)
683 (let ((case-fold-search t))
684 (goto-char start)
685 (setq has-from (search-forward "\nFrom:" nil t))
686 (goto-char start)
687 (setq has-date (and (search-forward "\nDate:" nil t) (point)))
688 (goto-char start))
689 (let ((case-fold-search nil))
690 (if (re-search-forward (concat "^" rmail-unix-mail-delimiter) nil t)
691 (replace-match
692 (concat
693 "Mail-from: \\&"
694 ;; Keep and reformat the date if we don't
695 ;; have a Date: field.
696 (if has-date
698 ;; If no time zone specified, assume est.
699 (if (= (match-beginning 7) (match-end 7))
700 "Date: \\3, \\5 \\4 \\9 \\6 EST\n"
701 "Date: \\3, \\5 \\4 \\9 \\6\\7\n"))
702 ;; Keep and reformat the sender if we don't
703 ;; have a From: field.
704 (if has-from
706 "From: \\1\n")))))))))
708 ;;;; *** Rmail Message Formatting and Header Manipulation ***
710 (defun rmail-reformat-message (beg end)
711 (goto-char beg)
712 (forward-line 1)
713 (if (/= (following-char) ?0)
714 (error "Bad format in RMAIL file."))
715 (let ((buffer-read-only nil)
716 (delta (- (buffer-size) end)))
717 (delete-char 1)
718 (insert ?1)
719 (forward-line 1)
720 (if (looking-at "Summary-line: ")
721 (forward-line 1))
722 (if (looking-at "\\*\\*\\* EOOH \\*\\*\\*\n")
723 (delete-region (point)
724 (progn (forward-line 1) (point))))
725 (let ((str (buffer-substring (point)
726 (save-excursion (search-forward "\n\n" end 'move)
727 (point)))))
728 (insert str "*** EOOH ***\n")
729 (narrow-to-region (point) (- (buffer-size) delta)))
730 (goto-char (point-min))
731 (if rmail-ignored-headers (rmail-clear-headers))
732 (if rmail-message-filter (funcall rmail-message-filter))))
734 (defun rmail-clear-headers ()
735 (if (search-forward "\n\n" nil t)
736 (save-restriction
737 (narrow-to-region (point-min) (point))
738 (let ((buffer-read-only nil))
739 (while (let ((case-fold-search t))
740 (goto-char (point-min))
741 (re-search-forward rmail-ignored-headers nil t))
742 (beginning-of-line)
743 (delete-region (point)
744 (progn (re-search-forward "\n[^ \t]")
745 (forward-char -1)
746 (point))))))))
748 (defun rmail-toggle-header ()
749 "Show original message header if pruned header currently shown, or vice versa."
750 (interactive)
751 (rmail-maybe-set-message-counters)
752 (narrow-to-region (rmail-msgbeg rmail-current-message) (point-max))
753 (let ((buffer-read-only nil))
754 (goto-char (point-min))
755 (forward-line 1)
756 (if (= (following-char) ?1)
757 (progn (delete-char 1)
758 (insert ?0)
759 (forward-line 1)
760 (if (looking-at "Summary-Line:")
761 (forward-line 1))
762 (insert "*** EOOH ***\n")
763 (forward-char -1)
764 (search-forward "\n*** EOOH ***\n")
765 (forward-line -1)
766 (let ((temp (point)))
767 (and (search-forward "\n\n" nil t)
768 (delete-region temp (point))))
769 (goto-char (point-min))
770 (search-forward "\n*** EOOH ***\n")
771 (narrow-to-region (point) (point-max)))
772 (rmail-reformat-message (point-min) (point-max)))))
774 ;;;; *** Rmail Attributes and Keywords ***
776 ;; Make a string describing current message's attributes and keywords
777 ;; and set it up as the name of a minor mode
778 ;; so it will appear in the mode line.
779 (defun rmail-display-labels ()
780 (let ((blurb "") (beg (point-min-marker)) (end (point-max-marker)))
781 (save-excursion
782 (unwind-protect
783 (progn
784 (widen)
785 (goto-char (rmail-msgbeg rmail-current-message))
786 (forward-line 1)
787 (if (looking-at "[01],")
788 (progn
789 (narrow-to-region (point) (progn (end-of-line) (point)))
790 ;; Truly valid BABYL format requires a space before each
791 ;; attribute or keyword name. Put them in if missing.
792 (let (buffer-read-only)
793 (goto-char (point-min))
794 (while (search-forward "," nil t)
795 (or (looking-at "[ ,]") (eobp)
796 (insert " "))))
797 (goto-char (point-max))
798 (if (search-backward ",," nil 'move)
799 (progn
800 (if (> (point) (1+ (point-min)))
801 (setq blurb (buffer-substring (+ 1 (point-min)) (point))))
802 (if (> (- (point-max) (point)) 2)
803 (setq blurb
804 (concat blurb
806 (buffer-substring (+ (point) 3)
807 (1- (point-max)))))))))))
808 ;; Note: we don't use save-restriction because that does not work right
809 ;; if changes are made outside the saved restriction
810 ;; before that restriction is restored.
811 (narrow-to-region beg end)
812 (set-marker beg nil)
813 (set-marker end nil)))
814 (while (string-match " +," blurb)
815 (setq blurb (concat (substring blurb 0 (match-beginning 0)) ","
816 (substring blurb (match-end 0)))))
817 (while (string-match ", +" blurb)
818 (setq blurb (concat (substring blurb 0 (match-beginning 0)) ","
819 (substring blurb (match-end 0)))))
820 (setq mode-line-process
821 (concat " " rmail-current-message "/" rmail-total-messages
822 blurb))))
824 ;; Turn an attribute of a message on or off according to STATE.
825 ;; ATTR is the name of the attribute, as a string.
826 ;; MSGNUM is message number to change; nil means current message.
827 (defun rmail-set-attribute (attr state &optional msgnum)
828 (let ((omax (point-max-marker))
829 (omin (point-min-marker))
830 (buffer-read-only nil))
831 (or msgnum (setq msgnum rmail-current-message))
832 (unwind-protect
833 (save-excursion
834 (widen)
835 (goto-char (+ 3 (rmail-msgbeg msgnum)))
836 (let ((curstate
837 (not
838 (null (search-backward (concat ", " attr ",")
839 (prog1 (point) (end-of-line)) t)))))
840 (or (eq curstate (not (not state)))
841 (if curstate
842 (delete-region (point) (1- (match-end 0)))
843 (beginning-of-line)
844 (forward-char 2)
845 (insert " " attr ","))))
846 (if (string= attr "deleted")
847 (rmail-set-message-deleted-p msgnum state)))
848 ;; Note: we don't use save-restriction because that does not work right
849 ;; if changes are made outside the saved restriction
850 ;; before that restriction is restored.
851 (narrow-to-region omin omax)
852 (set-marker omin nil)
853 (set-marker omax nil)
854 (if (= msgnum rmail-current-message)
855 (rmail-display-labels)))))
857 ;; Return t if the attributes/keywords line of msg number MSG
858 ;; contains a match for the regexp LABELS.
859 (defun rmail-message-labels-p (msg labels)
860 (save-excursion
861 (save-restriction
862 (widen)
863 (goto-char (rmail-msgbeg msg))
864 (forward-char 3)
865 (re-search-backward labels (prog1 (point) (end-of-line)) t))))
867 ;;;; *** Rmail Message Selection And Support ***
869 (defun rmail-msgend (n)
870 (marker-position (aref rmail-message-vector (1+ n))))
872 (defun rmail-msgbeg (n)
873 (marker-position (aref rmail-message-vector n)))
875 (defun rmail-widen-to-current-msgbeg (function)
876 "Call FUNCTION with point at start of internal data of current message.
877 Assumes that bounds were previously narrowed to display the message in Rmail.
878 The bounds are widened enough to move point where desired, then narrowed
879 again afterward.
881 FUNCTION may not change the visible text of the message, but it may
882 change the invisible header text."
883 (save-excursion
884 (let ((obeg (- (point-max) (point-min))))
885 (unwind-protect
886 (progn
887 (narrow-to-region (rmail-msgbeg rmail-current-message)
888 (point-max))
889 (goto-char (point-min))
890 (funcall function))
891 ;; Note: we don't use save-restriction because that does not work right
892 ;; if changes are made outside the saved restriction
893 ;; before that restriction is restored.
894 ;; Here we assume that changes made by FUNCTION
895 ;; occur before the visible region of the message.
896 (narrow-to-region (- (point-max) obeg) (point-max))))))
898 (defun rmail-forget-messages ()
899 (unwind-protect
900 (if (vectorp rmail-message-vector)
901 (let* ((i 0)
902 (v rmail-message-vector)
903 (n (length v)))
904 (while (< i n)
905 (move-marker (aref v i) nil)
906 (setq i (1+ i)))))
907 (setq rmail-message-vector nil)
908 (setq rmail-deleted-vector nil)))
910 (defun rmail-maybe-set-message-counters ()
911 (if (not (and rmail-deleted-vector
912 rmail-message-vector
913 rmail-current-message
914 rmail-total-messages))
915 (rmail-set-message-counters)))
917 (defun rmail-count-new-messages (&optional nomsg)
918 (let* ((case-fold-search nil)
919 (total-messages 0)
920 (messages-head nil)
921 (deleted-head nil))
922 (or nomsg (message "Counting new messages..."))
923 (goto-char (point-max))
924 ;; Put at the end of messages-head
925 ;; the entry for message N+1, which marks
926 ;; the end of message N. (N = number of messages).
927 (search-backward "\n\^_")
928 (forward-char 1)
929 (setq messages-head (list (point-marker)))
930 (rmail-set-message-counters-counter (point-min))
931 (setq rmail-current-message (1+ rmail-total-messages))
932 (setq rmail-total-messages
933 (+ rmail-total-messages total-messages))
934 (setq rmail-message-vector
935 (vconcat rmail-message-vector (cdr messages-head)))
936 (aset rmail-message-vector
937 rmail-current-message (car messages-head))
938 (setq rmail-deleted-vector
939 (concat rmail-deleted-vector deleted-head))
940 (setq rmail-summary-vector
941 (vconcat rmail-summary-vector (make-vector total-messages nil)))
942 (goto-char (point-min))
943 (or nomsg (message "Counting new messages...done (%d)" total-messages))))
945 (defun rmail-set-message-counters ()
946 (rmail-forget-messages)
947 (save-excursion
948 (save-restriction
949 (widen)
950 (let* ((point-save (point))
951 (total-messages 0)
952 (messages-after-point)
953 (case-fold-search nil)
954 (messages-head nil)
955 (deleted-head nil))
956 (message "Counting messages...")
957 (goto-char (point-max))
958 ;; Put at the end of messages-head
959 ;; the entry for message N+1, which marks
960 ;; the end of message N. (N = number of messages).
961 (search-backward "\n\^_")
962 (forward-char 1)
963 (setq messages-head (list (point-marker)))
964 (rmail-set-message-counters-counter (min (point) point-save))
965 (setq messages-after-point total-messages)
966 (rmail-set-message-counters-counter)
967 (setq rmail-total-messages total-messages)
968 (setq rmail-current-message
969 (min total-messages
970 (max 1 (- total-messages messages-after-point))))
971 (setq rmail-message-vector
972 (apply 'vector (cons (point-min-marker) messages-head))
973 rmail-deleted-vector (concat "D" deleted-head)
974 rmail-summary-vector (make-vector rmail-total-messages nil))
975 (message "Counting messages...done")))))
977 (defun rmail-set-message-counters-counter (&optional stop)
978 (while (search-backward "\n\^_\^L\n" stop t)
979 (forward-char 1)
980 (setq messages-head (cons (point-marker) messages-head))
981 (save-excursion
982 (setq deleted-head
983 (cons (if (search-backward ", deleted,"
984 (prog1 (point)
985 (forward-line 2))
987 ?D ?\ )
988 deleted-head)))
989 (if (zerop (% (setq total-messages (1+ total-messages)) 20))
990 (message "Counting messages...%d" total-messages))))
992 (defun rmail-beginning-of-message ()
993 "Show current message starting from the beginning."
994 (interactive)
995 (rmail-show-message rmail-current-message))
997 (defun rmail-show-message (&optional n)
998 "Show message number N (prefix argument), counting from start of file."
999 (interactive "p")
1000 (rmail-maybe-set-message-counters)
1001 (widen)
1002 (if (zerop rmail-total-messages)
1003 (progn (narrow-to-region (point-min) (1- (point-max)))
1004 (goto-char (point-min))
1005 (setq mode-line-process nil))
1006 (let (blurb)
1007 (if (not n)
1008 (setq n rmail-current-message)
1009 (cond ((<= n 0)
1010 (setq n 1
1011 rmail-current-message 1
1012 blurb "No previous message"))
1013 ((> n rmail-total-messages)
1014 (setq n rmail-total-messages
1015 rmail-current-message rmail-total-messages
1016 blurb "No following message"))
1018 (setq rmail-current-message n))))
1019 (let ((beg (rmail-msgbeg n))
1020 (end (rmail-msgend n)))
1021 (goto-char beg)
1022 (forward-line 1)
1023 (if (= (following-char) ?0)
1024 (progn
1025 (rmail-reformat-message beg end)
1026 (rmail-set-attribute "unseen" nil))
1027 (search-forward "\n*** EOOH ***\n" end t)
1028 (narrow-to-region (point) end))
1029 (goto-char (point-min))
1030 (rmail-display-labels)
1031 (run-hooks 'rmail-show-message-hook)
1032 (if blurb
1033 (message blurb))))))
1035 (defun rmail-next-message (n)
1036 "Show following message whether deleted or not.
1037 With prefix arg N, moves forward N messages, or backward if N is negative."
1038 (interactive "p")
1039 (rmail-maybe-set-message-counters)
1040 (rmail-show-message (+ rmail-current-message n)))
1042 (defun rmail-previous-message (n)
1043 "Show previous message whether deleted or not.
1044 With prefix arg N, moves backward N messages, or forward if N is negative."
1045 (interactive "p")
1046 (rmail-next-message (- n)))
1048 (defun rmail-next-undeleted-message (n)
1049 "Show following non-deleted message.
1050 With prefix arg N, moves forward N non-deleted messages,
1051 or backward if N is negative."
1052 (interactive "p")
1053 (rmail-maybe-set-message-counters)
1054 (let ((lastwin rmail-current-message)
1055 (current rmail-current-message))
1056 (while (and (> n 0) (< current rmail-total-messages))
1057 (setq current (1+ current))
1058 (if (not (rmail-message-deleted-p current))
1059 (setq lastwin current n (1- n))))
1060 (while (and (< n 0) (> current 1))
1061 (setq current (1- current))
1062 (if (not (rmail-message-deleted-p current))
1063 (setq lastwin current n (1+ n))))
1064 (if (/= lastwin rmail-current-message)
1065 (rmail-show-message lastwin))
1066 (if (< n 0)
1067 (error "No previous nondeleted message"))
1068 (if (> n 0)
1069 (error "No following nondeleted message"))))
1071 (defun rmail-previous-undeleted-message (n)
1072 "Show previous non-deleted message.
1073 With prefix argument N, moves backward N non-deleted messages,
1074 or forward if N is negative."
1075 (interactive "p")
1076 (rmail-next-undeleted-message (- n)))
1078 (defun rmail-first-message ()
1079 "Show first message in file."
1080 (interactive)
1081 (rmail-maybe-set-message-counters)
1082 (rmail-show-message 1))
1084 (defun rmail-last-message ()
1085 "Show last message in file."
1086 (interactive)
1087 (rmail-maybe-set-message-counters)
1088 (rmail-show-message rmail-total-messages))
1090 (defun rmail-what-message ()
1091 (let ((where (point))
1092 (low 1)
1093 (high rmail-total-messages)
1094 (mid (/ rmail-total-messages 2)))
1095 (while (> (- high low) 1)
1096 (if (>= where (rmail-msgbeg mid))
1097 (setq low mid)
1098 (setq high mid))
1099 (setq mid (+ low (/ (- high low) 2))))
1100 (if (>= where (rmail-msgbeg high)) high low)))
1102 (defvar rmail-search-last-regexp nil)
1103 (defun rmail-search (regexp &optional reversep)
1104 "Show message containing next match for REGEXP.
1105 Search in reverse (earlier messages) with non-nil second arg REVERSEP.
1106 Interactively, empty argument means use same regexp used last time,
1107 and reverse search is specified by a negative numeric arg."
1108 (interactive
1109 (let* ((reversep (< (prefix-numeric-value current-prefix-arg) 0))
1110 (prompt
1111 (concat (if reversep "Reverse " "") "Rmail search (regexp): "))
1112 regexp)
1113 (if rmail-search-last-regexp
1114 (setq prompt (concat prompt
1115 "(default "
1116 rmail-search-last-regexp
1117 ") ")))
1118 (setq regexp (read-string prompt))
1119 (cond ((not (equal regexp ""))
1120 (setq rmail-search-last-regexp regexp))
1121 ((not rmail-search-last-regexp)
1122 (error "No previous Rmail search string")))
1123 (list rmail-search-last-regexp reversep)))
1124 (message "%sRmail search for %s..."
1125 (if reversep "Reverse " "")
1126 regexp)
1127 (rmail-maybe-set-message-counters)
1128 (let ((omin (point-min))
1129 (omax (point-max))
1130 (opoint (point))
1132 (msg rmail-current-message))
1133 (unwind-protect
1134 (progn
1135 (widen)
1136 ;; Check messages one by one, advancing message number up or down
1137 ;; but searching forward through each message.
1138 (if reversep
1139 (while (and (null win) (> msg 1))
1140 (goto-char (rmail-msgbeg (setq msg (1- msg))))
1141 (setq win (re-search-forward
1142 regexp (rmail-msgend msg) t)))
1143 (while (and (null win) (< msg rmail-total-messages))
1144 (goto-char (rmail-msgbeg (setq msg (1+ msg))))
1145 (setq win (re-search-forward regexp (rmail-msgend msg) t)))))
1146 (if win
1147 (progn
1148 ;; If this is a reverse search and we found a message,
1149 ;; search backward thru this message to position point.
1150 (if reversep
1151 (progn
1152 (goto-char (rmail-msgend msg))
1153 (re-search-backward
1154 regexp (rmail-msgbeg msg) t)))
1155 (setq win (point))
1156 (rmail-show-message msg)
1157 (message "%sRmail search for %s...done"
1158 (if reversep "Reverse " "")
1159 regexp)
1160 (goto-char win))
1161 (goto-char opoint)
1162 (narrow-to-region omin omax)
1163 (ding)
1164 (message "Search failed: %s" regexp)))))
1166 ;; Show the first message which has the `unseen' attribute.
1167 (defun rmail-first-unseen-message ()
1168 (let ((current 1)
1169 found)
1170 (save-restriction
1171 (widen)
1172 (while (and (not found) (< current rmail-total-messages))
1173 (if (rmail-message-labels-p current ", ?\\(unseen\\),")
1174 (setq found current))
1175 (setq current (1+ current))))
1176 (if found
1177 (rmail-show-message found))))
1179 ;;;; *** Rmail Message Deletion Commands ***
1181 (defun rmail-message-deleted-p (n)
1182 (= (aref rmail-deleted-vector n) ?D))
1184 (defun rmail-set-message-deleted-p (n state)
1185 (aset rmail-deleted-vector n (if state ?D ?\ )))
1187 (defun rmail-delete-message ()
1188 "Delete this message and stay on it."
1189 (interactive)
1190 (rmail-set-attribute "deleted" t))
1192 (defun rmail-undelete-previous-message ()
1193 "Back up to deleted message, select it, and undelete it."
1194 (interactive)
1195 (let ((msg rmail-current-message))
1196 (while (and (> msg 0)
1197 (not (rmail-message-deleted-p msg)))
1198 (setq msg (1- msg)))
1199 (if (= msg 0)
1200 (error "No previous deleted message")
1201 (if (/= msg rmail-current-message)
1202 (rmail-show-message msg))
1203 (rmail-set-attribute "deleted" nil))))
1205 (defun rmail-delete-forward (&optional backward)
1206 "Delete this message and move to next nondeleted one.
1207 Deleted messages stay in the file until the \\[rmail-expunge] command is given.
1208 With prefix argument, delete and move backward. If there is no nondeleted
1209 message to move to in the preferred or specified direction, move in the
1210 other direction."
1211 (interactive "P")
1212 (rmail-set-attribute "deleted" t)
1213 (condition-case ()
1214 (rmail-next-undeleted-message (if backward -1 1))
1215 (error
1216 (condition-case ()
1217 (rmail-previous-undeleted-message (if backward -1 1))
1218 (error nil)))))
1220 (defun rmail-delete-backward ()
1221 "Delete this message and move to previous nondeleted one.
1222 Deleted messages stay in the file until the \\[rmail-expunge] command is given."
1223 (interactive)
1224 (rmail-delete-forward t))
1226 (defun rmail-expunge ()
1227 "Actually erase all deleted messages in the file."
1228 (interactive)
1229 (message "Expunging deleted messages...")
1230 ;; Discard all undo records for this buffer.
1231 (or (eq buffer-undo-list t)
1232 (setq buffer-undo-list nil))
1233 (rmail-maybe-set-message-counters)
1234 (let* ((omax (- (buffer-size) (point-max)))
1235 (omin (- (buffer-size) (point-min)))
1236 (opoint (if (and (> rmail-current-message 0)
1237 (= ?D (aref rmail-deleted-vector rmail-current-message)))
1238 0 (- (point) (point-min))))
1239 (messages-head (cons (aref rmail-message-vector 0) nil))
1240 (messages-tail messages-head)
1241 ;; Don't make any undo records for the expunging.
1242 (buffer-undo-list t)
1243 (win))
1244 (unwind-protect
1245 (save-excursion
1246 (widen)
1247 (goto-char (point-min))
1248 (let ((counter 0)
1249 (number 1)
1250 (total rmail-total-messages)
1251 (new-message-number rmail-current-message)
1252 (new-summary nil)
1253 (buffer-read-only nil)
1254 (messages rmail-message-vector)
1255 (deleted rmail-deleted-vector)
1256 (summary rmail-summary-vector))
1257 (setq rmail-total-messages nil
1258 rmail-current-message nil
1259 rmail-message-vector nil
1260 rmail-deleted-vector nil
1261 rmail-summary-vector nil)
1262 (while (<= number total)
1263 (if (= (aref deleted number) ?D)
1264 (progn
1265 (delete-region
1266 (marker-position (aref messages number))
1267 (marker-position (aref messages (1+ number))))
1268 (move-marker (aref messages number) nil)
1269 (if (> new-message-number counter)
1270 (setq new-message-number (1- new-message-number))))
1271 (setq counter (1+ counter))
1272 (setq messages-tail
1273 (setcdr messages-tail
1274 (cons (aref messages number) nil)))
1275 (setq new-summary
1276 (cons (if (= counter number) (aref summary (1- number)))
1277 new-summary)))
1278 (if (zerop (% (setq number (1+ number)) 20))
1279 (message "Expunging deleted messages...%d" number)))
1280 (setq messages-tail
1281 (setcdr messages-tail
1282 (cons (aref messages number) nil)))
1283 (setq rmail-current-message new-message-number
1284 rmail-total-messages counter
1285 rmail-message-vector (apply 'vector messages-head)
1286 rmail-deleted-vector (make-string (1+ counter) ?\ )
1287 rmail-summary-vector (vconcat (nreverse new-summary))
1288 win t)))
1289 (message "Expunging deleted messages...done")
1290 (if (not win)
1291 (narrow-to-region (- (buffer-size) omin) (- (buffer-size) omax)))
1292 (rmail-show-message
1293 (if (zerop rmail-current-message) 1 nil))
1294 (forward-char opoint))))
1296 ;;;; *** Rmail Mailing Commands ***
1298 (defun rmail-mail ()
1299 "Send mail in another window. While composing the message, use
1300 \\[mail-yank-original] to yank the original message into it."
1301 (interactive)
1302 (mail-other-window nil nil nil nil nil (current-buffer)))
1304 (defun rmail-continue ()
1305 "Continue composing outgoing message previously being composed."
1306 (interactive)
1307 (mail-other-window t))
1309 (defun rmail-reply (just-sender)
1310 "Reply to the current message.
1311 Normally include CC: to all other recipients of original message;
1312 prefix argument means ignore them. While composing the reply,
1313 use \\[mail-yank-original] to yank the original message into it."
1314 (interactive "P")
1315 (let (from reply-to cc subject date to message-id resent-reply-to)
1316 (save-excursion
1317 (save-restriction
1318 (widen)
1319 (goto-char (rmail-msgbeg rmail-current-message))
1320 (forward-line 1)
1321 (if (= (following-char) ?0)
1322 (narrow-to-region
1323 (progn (forward-line 2)
1324 (point))
1325 (progn (search-forward "\n\n" (rmail-msgend rmail-current-message)
1326 'move)
1327 (point)))
1328 (narrow-to-region (point)
1329 (progn (search-forward "\n*** EOOH ***\n")
1330 (beginning-of-line) (point))))
1331 (setq resent-reply-to (mail-fetch-field "resent-reply-to" t)
1332 from (mail-fetch-field "from")
1333 reply-to (or resent-reply-to
1334 (mail-fetch-field "reply-to" nil t)
1335 from)
1336 cc (cond (just-sender nil)
1337 (resent-reply-to (mail-fetch-field "resent-cc" t))
1338 (t (mail-fetch-field "cc" nil t)))
1339 subject (or (and resent-reply-to
1340 (mail-fetch-field "resent-subject" t))
1341 (mail-fetch-field "subject"))
1342 date (cond (resent-reply-to
1343 (mail-fetch-field "resent-date" t))
1344 ((mail-fetch-field "date")))
1345 to (cond (resent-reply-to
1346 (mail-fetch-field "resent-to" t))
1347 ((mail-fetch-field "to" nil t))
1348 ;((mail-fetch-field "apparently-to")) ack gag barf
1349 (t ""))
1350 message-id (cond (resent-reply-to
1351 (mail-fetch-field "resent-message-id" t))
1352 ((mail-fetch-field "message-id"))))))
1353 (and subject
1354 (string-match "\\`Re: " subject)
1355 (setq subject (substring subject 4)))
1356 (mail-other-window nil
1357 (mail-strip-quoted-names reply-to)
1358 subject
1359 (rmail-make-in-reply-to-field from date message-id)
1360 (if just-sender
1362 (let* ((cc-list (rmail-dont-reply-to
1363 (mail-strip-quoted-names
1364 (if (null cc) to (concat to ", " cc))))))
1365 (if (string= cc-list "") nil cc-list)))
1366 (current-buffer)
1367 (list (list '(lambda (buf msgnum)
1368 (save-excursion
1369 (set-buffer buf)
1370 (rmail-set-attribute "answered" t msgnum)))
1371 (current-buffer) rmail-current-message)))))
1373 (defun rmail-make-in-reply-to-field (from date message-id)
1374 (cond ((not from)
1375 (if message-id
1376 message-id
1377 nil))
1378 (mail-use-rfc822
1379 (require 'rfc822)
1380 (let ((tem (car (rfc822-addresses from))))
1381 (if message-id
1382 (if (string-match
1383 (regexp-quote (if (string-match "@[^@]*\\'" tem)
1384 (substring tem 0 (match-beginning 0))
1385 tem))
1386 message-id)
1387 ;; Message-ID is sufficiently informative
1388 message-id
1389 (concat message-id " (" tem ")"))
1390 ;; Use prin1 to fake RFC822 quoting
1391 (let ((field (prin1-to-string tem)))
1392 (if date
1393 (concat field "'s message of " date)
1394 field)))))
1395 ((let* ((foo "[^][\000-\037\177-\377()<>@,;:\\\" ]+")
1396 (bar "[^][\000-\037\177-\377()<>@,;:\\\"]+"))
1397 ;; Can't use format because format loses on \000 (unix *^&%*^&%$!!)
1398 (or (string-match (concat "\\`[ \t]*\\(" bar
1399 "\\)\\(<" foo "@" foo ">\\)?[ \t]*\\'")
1400 ;; "Unix Loser <Foo@bar.edu>" => "Unix Loser"
1401 from)
1402 (string-match (concat "\\`[ \t]*<" foo "@" foo ">[ \t]*(\\("
1403 bar "\\))[ \t]*\\'")
1404 ;; "<Bugs@bar.edu>" (Losing Unix) => "Losing Unix"
1405 from)))
1406 (let ((start (match-beginning 1))
1407 (end (match-end 1)))
1408 ;; Trim whitespace which above regexp match allows
1409 (while (and (< start end)
1410 (memq (aref from start) '(?\t ?\ )))
1411 (setq start (1+ start)))
1412 (while (and (< start end)
1413 (memq (aref from (1- end)) '(?\t ?\ )))
1414 (setq end (1- end)))
1415 (let ((field (substring from start end)))
1416 (if date (setq field (concat "message from " field " on " date)))
1417 (if message-id
1418 ;; "<AA259@bar.edu> (message from Unix Loser on 1-Apr-89)"
1419 (concat message-id " (" field ")")
1420 field))))
1422 ;; If we can't kludge it simply, do it correctly
1423 (let ((mail-use-rfc822 t))
1424 (rmail-make-in-reply-to-field from date message-id)))))
1426 (defun rmail-forward ()
1427 "Forward the current message to another user."
1428 (interactive)
1429 (let ((forward-buffer (current-buffer))
1430 (subject (concat "["
1431 (mail-strip-quoted-names (mail-fetch-field "From"))
1432 ": " (or (mail-fetch-field "Subject") "") "]")))
1433 ;; If only one window, use it for the mail buffer.
1434 ;; Otherwise, use another window for the mail buffer
1435 ;; so that the Rmail buffer remains visible
1436 ;; and sending the mail will get back to it.
1437 (if (funcall (if (one-window-p t)
1438 (function mail)
1439 (function mail-other-window))
1440 nil nil subject nil nil nil
1441 (list (list (function (lambda (buf msgnum)
1442 (save-excursion
1443 (set-buffer buf)
1444 (rmail-set-attribute "forwarded" t msgnum))))
1445 (current-buffer)
1446 rmail-current-message)))
1447 (save-excursion
1448 (goto-char (point-max))
1449 (forward-line 1)
1450 (insert-buffer forward-buffer)))))
1452 (defun rmail-resend (address &optional from comment mail-alias-file)
1453 "Resend current message to ADDRESSES.
1454 ADDRESSES should be a single address, a a string consisting of several
1455 addresses separated by commas, or a list of addresses.
1457 Optional FROM is the address to resend the message from, and
1458 defaults to the username of the person redistributing the message.
1459 Optional COMMENT is a string that will be inserted as a comment in the
1460 resent message.
1461 Optional ALIAS-FILE is alternate aliases file to be used by sendmail,
1462 typically for purposes of moderating a list."
1463 (interactive "sResend to: ")
1464 (if (not from) (setq from (user-login-name)))
1465 (let ((tembuf (generate-new-buffer " sendmail temp"))
1466 (mail-header-separator "")
1467 (case-fold-search nil)
1468 (mailbuf (current-buffer)))
1469 (unwind-protect
1470 (save-excursion
1471 ;;>> Copy message into temp buffer
1472 (set-buffer tembuf)
1473 (insert-buffer-substring mailbuf)
1474 (goto-char (point-min))
1475 ;;>> Insert resent-from:
1476 (insert "Resent-From: " from "\n")
1477 (insert "Resent-Date: " (current-time-string) "\n")
1478 ;;>> Insert resent-to: and bcc if need be.
1479 (let ((before (point)))
1480 (insert "Resent-To: " (if (stringp address)
1481 address
1482 (mapconcat 'identity address ",\n\t"))
1483 "\n")
1484 (expand-mail-aliases before (point)))
1485 ;;>> Set up comment, if any.
1486 (if (and (sequencep comment) (not (zerop (length comment))))
1487 (let ((before (point))
1488 after)
1489 (insert comment)
1490 (or (eolp) (insert "\n"))
1491 (setq after (point))
1492 (goto-char before)
1493 (while (< (point) after)
1494 (insert "Resent-Comment: ")
1495 (forward-line 1))))
1496 ;; Don't expand aliases in the destination fields
1497 ;; of the original message.
1498 (let (mail-aliases)
1499 (sendmail-send-it)))
1500 (kill-buffer tembuf))))
1502 (defvar mail-unsent-separator "^ ----- Unsent message follows -----$")
1504 (defun rmail-retry-failure ()
1505 "Edit a mail message which is based on the contents of the current message.
1506 For a message rejected by the mail system, extract the interesting headers and
1507 the body of the original message; otherwise copy the current message."
1508 (interactive)
1509 (require 'mail-utils)
1510 (let (to subj irp2 cc orig-message)
1511 (save-excursion
1512 ;; Narrow down to just the quoted original message
1513 (rmail-beginning-of-message)
1514 (or (re-search-forward mail-unsent-separator nil t)
1515 (error "Cannot parse this as a failure message"))
1516 (save-restriction
1517 (narrow-to-region (point) (point-max))
1518 ;; Now mail-fetch-field will get from headers of the original message,
1519 ;; not from the headers of the rejection.
1520 (setq to (mail-fetch-field "To")
1521 subj (mail-fetch-field "Subject")
1522 irp2 (mail-fetch-field "In-reply-to")
1523 cc (mail-fetch-field "Cc"))
1524 ;; Get the entire text (not headers) of the original message.
1525 (setq orig-message
1526 (buffer-substring
1527 (progn (search-forward "\n\n") (point))
1528 (point-max)))))
1529 ;; Start sending a new message; default header fields from the original.
1530 (if (mail-other-window nil to subj irp2 cc (current-buffer))
1531 ;; Insert original text as initial text of new draft message.
1532 (progn
1533 (goto-char (point-max))
1534 (insert orig-message)
1535 (goto-char (point-min))
1536 (end-of-line)))))
1538 ;;;; *** Rmail Specify Inbox Files ***
1540 (autoload 'set-rmail-inbox-list "rmailmsc"
1541 "Set the inbox list of the current RMAIL file to FILE-NAME.
1542 This may be a list of file names separated by commas.
1543 If FILE-NAME is empty, remove any inbox list."
1546 ;;;; *** Rmail Commands for Labels ***
1548 (autoload 'rmail-add-label "rmailkwd"
1549 "Add LABEL to labels associated with current RMAIL message.
1550 Completion is performed over known labels when reading."
1553 (autoload 'rmail-kill-label "rmailkwd"
1554 "Remove LABEL from labels associated with current RMAIL message.
1555 Completion is performed over known labels when reading."
1558 (autoload 'rmail-next-labeled-message "rmailkwd"
1559 "Show next message with LABEL. Defaults to last label used.
1560 With prefix argument N moves forward N messages with this label."
1563 (autoload 'rmail-previous-labeled-message "rmailkwd"
1564 "Show previous message with LABEL. Defaults to last label used.
1565 With prefix argument N moves backward N messages with this label."
1568 ;;;; *** Rmail Edit Mode ***
1570 (autoload 'rmail-edit-current-message "rmailedit"
1571 "Edit the contents of the current message"
1574 ;;;; *** Rmail Summary Mode ***
1576 (autoload 'rmail-summary "rmailsum"
1577 "Display a summary of all messages, one line per message."
1580 (autoload 'rmail-summary-by-labels "rmailsum"
1581 "Display a summary of all messages with one or more LABELS.
1582 LABELS should be a string containing the desired labels, separated by commas."
1585 (autoload 'rmail-summary-by-recipients "rmailsum"
1586 "Display a summary of all messages with the given RECIPIENTS.
1587 Normally checks the To, From and Cc fields of headers; but if PRIMARY-ONLY
1588 is non-nil (prefix arg given), only look in the To and From fields.
1589 RECIPIENTS is a string of names separated by commas."
1592 ;;;; *** Rmail output messages to files ***
1594 (autoload 'rmail-output-to-rmail-file "rmailout"
1595 "Append the current message to an Rmail file named FILE-NAME.
1596 If the file does not exist, ask if it should be created.
1597 If file is being visited, the message is appended to the Emacs
1598 buffer visiting that file."
1601 (autoload 'rmail-output "rmailout"
1602 "Append this message to Unix mail file named FILE-NAME."
1605 ;;;; *** Rmail undigestification ***
1607 (autoload 'undigestify-rmail-message "undigest"
1608 "Break up a digest message into its constituent messages.
1609 Leaves original message, deleted, before the undigestified messages."
1612 (provide 'rmail)