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