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