]> code.delx.au - gnu-emacs/blob - lisp/mail/rmail.el
*** empty log message ***
[gnu-emacs] / 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 (goto-char (point-min))
585 (save-restriction
586 (while (not (eobp))
587 (cond ((looking-at "BABYL OPTIONS:");Babyl header
588 (search-forward "\n\^_")
589 (delete-region (point-min) (point)))
590 ;; Babyl format message
591 ((looking-at "\^L")
592 (or (search-forward "\n\^_" nil t)
593 (progn
594 (message "Invalid Babyl format in inbox!")
595 (sit-for 1)
596 (goto-char (point-max))))
597 (setq count (1+ count))
598 ;; Make sure there is no extra white space after the ^_
599 ;; at the end of the message.
600 ;; Narrowing will make sure that whatever follows the junk
601 ;; will be treated properly.
602 (delete-region (point)
603 (save-excursion
604 (skip-chars-forward " \t\n")
605 (point)))
606 (narrow-to-region (point) (point-max)))
607 ;;*** MMDF format
608 ((let ((case-fold-search t))
609 (looking-at mmdf-delim1))
610 (let ((case-fold-search t))
611 (replace-match "\^L\n0, unseen,,\n*** EOOH ***\n")
612 (setq start (point))
613 (re-search-forward mmdf-delim2 nil t)
614 (replace-match "\^_"))
615 (save-excursion
616 (save-restriction
617 (narrow-to-region start (1- (point)))
618 (goto-char (point-min))
619 (while (search-forward "\n\^_" nil t); single char "\^_"
620 (replace-match "\n^_")))); 2 chars: "^" and "_"
621 (narrow-to-region (point) (point-max))
622 (setq count (1+ count)))
623 ;;*** Mail format
624 ((looking-at "^From ")
625 (setq start (point))
626 (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
627 (rmail-nuke-pinhead-header)
628 (if (re-search-forward
629 (concat "^[\^_]?\\("
630 "From [^ \n]*\\(\\|\".*\"[^ \n]*\\) ?[^ \n]* [^ \n]* *"
631 "[0-9]* [0-9:]*\\( ?[A-Z]?[A-Z][A-Z]T\\| ?[-+]?[0-9][0-9][0-9][0-9]\\|\\) " ; EDT, -0500
632 "19[0-9]* *\\(remote from [^\n]*\\)?$\\|"
633 mmdf-delim1 "\\|"
634 "^BABYL OPTIONS:\\|"
635 "\^L\n[01],\\)") nil t)
636 (goto-char (match-beginning 1))
637 (goto-char (point-max)))
638 (setq count (1+ count))
639 (save-excursion
640 (save-restriction
641 (narrow-to-region start (point))
642 (goto-char (point-min))
643 (while (search-forward "\n\^_" nil t); single char
644 (replace-match "\n^_")))); 2 chars: "^" and "_"
645 (insert ?\^_)
646 (narrow-to-region (point) (point-max)))
647 ;;
648 ;;This is a kludge, in case we're wrong about mmdf not
649 ;;allowing anything in between. If it loses, we'll have
650 ;;to look for something else
651 (t (error "Cannot convert to babyl format")))))
652 count))
653
654 ;; Delete the "From ..." line, creating various other headers with
655 ;; information from it if they don't already exist. Now puts the
656 ;; original line into a mail-from: header line for debugging.
657 (defun rmail-nuke-pinhead-header ()
658 (save-excursion
659 (save-restriction
660 (let ((start (point))
661 (end (progn
662 (condition-case ()
663 (search-forward "\n\n")
664 (error
665 (goto-char (point-max))
666 (insert "\n\n")))
667 (point)))
668 has-from has-date)
669 (narrow-to-region start end)
670 (let ((case-fold-search t))
671 (goto-char start)
672 (setq has-from (search-forward "\nFrom:" nil t))
673 (goto-char start)
674 (setq has-date (and (search-forward "\nDate:" nil t) (point)))
675 (goto-char start))
676 (let ((case-fold-search nil))
677 (if (re-search-forward
678 "^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)
679 (replace-match
680 (concat
681 "Mail-from: \\&"
682 ;; Keep and reformat the date if we don't
683 ;; have a Date: field.
684 (if has-date
685 ""
686 ;; If no time zone specified, assume est.
687 (if (= (match-beginning 7) (match-end 7))
688 "Date: \\3, \\5 \\4 \\8 \\6 EST\n"
689 "Date: \\3, \\5 \\4 \\8 \\6\\7\n"))
690 ;; Keep and reformat the sender if we don't
691 ;; have a From: field.
692 (if has-from
693 ""
694 "From: \\1\n")))))))))
695 \f
696 ;;;; *** Rmail Message Formatting and Header Manipulation ***
697
698 (defun rmail-reformat-message (beg end)
699 (goto-char beg)
700 (forward-line 1)
701 (if (/= (following-char) ?0)
702 (error "Bad format in RMAIL file."))
703 (let ((buffer-read-only nil)
704 (delta (- (buffer-size) end)))
705 (delete-char 1)
706 (insert ?1)
707 (forward-line 1)
708 (if (looking-at "Summary-line: ")
709 (forward-line 1))
710 (if (looking-at "\\*\\*\\* EOOH \\*\\*\\*\n")
711 (delete-region (point)
712 (progn (forward-line 1) (point))))
713 (let ((str (buffer-substring (point)
714 (save-excursion (search-forward "\n\n" end 'move)
715 (point)))))
716 (insert str "*** EOOH ***\n")
717 (narrow-to-region (point) (- (buffer-size) delta)))
718 (goto-char (point-min))
719 (if rmail-ignored-headers (rmail-clear-headers))
720 (if rmail-message-filter (funcall rmail-message-filter))))
721
722 (defun rmail-clear-headers ()
723 (if (search-forward "\n\n" nil t)
724 (save-restriction
725 (narrow-to-region (point-min) (point))
726 (let ((buffer-read-only nil))
727 (while (let ((case-fold-search t))
728 (goto-char (point-min))
729 (re-search-forward rmail-ignored-headers nil t))
730 (beginning-of-line)
731 (delete-region (point)
732 (progn (re-search-forward "\n[^ \t]")
733 (forward-char -1)
734 (point))))))))
735
736 (defun rmail-toggle-header ()
737 "Show original message header if pruned header currently shown, or vice versa."
738 (interactive)
739 (rmail-maybe-set-message-counters)
740 (narrow-to-region (rmail-msgbeg rmail-current-message) (point-max))
741 (let ((buffer-read-only nil))
742 (goto-char (point-min))
743 (forward-line 1)
744 (if (= (following-char) ?1)
745 (progn (delete-char 1)
746 (insert ?0)
747 (forward-line 1)
748 (if (looking-at "Summary-Line:")
749 (forward-line 1))
750 (insert "*** EOOH ***\n")
751 (forward-char -1)
752 (search-forward "\n*** EOOH ***\n")
753 (forward-line -1)
754 (let ((temp (point)))
755 (and (search-forward "\n\n" nil t)
756 (delete-region temp (point))))
757 (goto-char (point-min))
758 (search-forward "\n*** EOOH ***\n")
759 (narrow-to-region (point) (point-max)))
760 (rmail-reformat-message (point-min) (point-max)))))
761 \f
762 ;;;; *** Rmail Attributes and Keywords ***
763
764 ;; Make a string describing current message's attributes and keywords
765 ;; and set it up as the name of a minor mode
766 ;; so it will appear in the mode line.
767 (defun rmail-display-labels ()
768 (let ((blurb "") (beg (point-min-marker)) (end (point-max-marker)))
769 (save-excursion
770 (unwind-protect
771 (progn
772 (widen)
773 (goto-char (rmail-msgbeg rmail-current-message))
774 (forward-line 1)
775 (if (looking-at "[01],")
776 (progn
777 (narrow-to-region (point) (progn (end-of-line) (point)))
778 ;; Truly valid BABYL format requires a space before each
779 ;; attribute or keyword name. Put them in if missing.
780 (let (buffer-read-only)
781 (goto-char (point-min))
782 (while (search-forward "," nil t)
783 (or (looking-at "[ ,]") (eobp)
784 (insert " "))))
785 (goto-char (point-max))
786 (if (search-backward ",," nil 'move)
787 (progn
788 (if (> (point) (1+ (point-min)))
789 (setq blurb (buffer-substring (+ 1 (point-min)) (point))))
790 (if (> (- (point-max) (point)) 2)
791 (setq blurb
792 (concat blurb
793 ";"
794 (buffer-substring (+ (point) 3)
795 (1- (point-max)))))))))))
796 ;; Note: we don't use save-restriction because that does not work right
797 ;; if changes are made outside the saved restriction
798 ;; before that restriction is restored.
799 (narrow-to-region beg end)
800 (set-marker beg nil)
801 (set-marker end nil)))
802 (while (string-match " +," blurb)
803 (setq blurb (concat (substring blurb 0 (match-beginning 0)) ","
804 (substring blurb (match-end 0)))))
805 (while (string-match ", +" blurb)
806 (setq blurb (concat (substring blurb 0 (match-beginning 0)) ","
807 (substring blurb (match-end 0)))))
808 (setq mode-line-process
809 (concat " " rmail-current-message "/" rmail-total-messages
810 blurb))))
811
812 ;; Turn an attribute of a message on or off according to STATE.
813 ;; ATTR is the name of the attribute, as a string.
814 ;; MSGNUM is message number to change; nil means current message.
815 (defun rmail-set-attribute (attr state &optional msgnum)
816 (let ((omax (point-max-marker))
817 (omin (point-min-marker))
818 (buffer-read-only nil))
819 (or msgnum (setq msgnum rmail-current-message))
820 (unwind-protect
821 (save-excursion
822 (widen)
823 (goto-char (+ 3 (rmail-msgbeg msgnum)))
824 (let ((curstate
825 (not
826 (null (search-backward (concat ", " attr ",")
827 (prog1 (point) (end-of-line)) t)))))
828 (or (eq curstate (not (not state)))
829 (if curstate
830 (delete-region (point) (1- (match-end 0)))
831 (beginning-of-line)
832 (forward-char 2)
833 (insert " " attr ","))))
834 (if (string= attr "deleted")
835 (rmail-set-message-deleted-p msgnum state)))
836 ;; Note: we don't use save-restriction because that does not work right
837 ;; if changes are made outside the saved restriction
838 ;; before that restriction is restored.
839 (narrow-to-region omin omax)
840 (set-marker omin nil)
841 (set-marker omax nil)
842 (if (= msgnum rmail-current-message)
843 (rmail-display-labels)))))
844
845 ;; Return t if the attributes/keywords line of msg number MSG
846 ;; contains a match for the regexp LABELS.
847 (defun rmail-message-labels-p (msg labels)
848 (save-excursion
849 (save-restriction
850 (widen)
851 (goto-char (rmail-msgbeg msg))
852 (forward-char 3)
853 (re-search-backward labels (prog1 (point) (end-of-line)) t))))
854 \f
855 ;;;; *** Rmail Message Selection And Support ***
856
857 (defun rmail-msgend (n)
858 (marker-position (aref rmail-message-vector (1+ n))))
859
860 (defun rmail-msgbeg (n)
861 (marker-position (aref rmail-message-vector n)))
862
863 (defun rmail-widen-to-current-msgbeg (function)
864 "Call FUNCTION with point at start of internal data of current message.
865 Assumes that bounds were previously narrowed to display the message in Rmail.
866 The bounds are widened enough to move point where desired, then narrowed
867 again afterward.
868
869 FUNCTION may not change the visible text of the message, but it may
870 change the invisible header text."
871 (save-excursion
872 (let ((obeg (- (point-max) (point-min)))
873 (unwind-protect
874 (progn
875 (narrow-to-region (rmail-msgbeg rmail-current-message)
876 (point-max))
877 (goto-char (point-min))
878 (funcall function))
879 ;; Note: we don't use save-restriction because that does not work right
880 ;; if changes are made outside the saved restriction
881 ;; before that restriction is restored.
882 ;; Here we assume that changes made by FUNCTION
883 ;; occur before the visible region of the message.
884 (narrow-to-region (- (point-max) obeg) (point-max)))))))
885
886 (defun rmail-forget-messages ()
887 (unwind-protect
888 (if (vectorp rmail-message-vector)
889 (let* ((i 0)
890 (v rmail-message-vector)
891 (n (length v)))
892 (while (< i n)
893 (move-marker (aref v i) nil)
894 (setq i (1+ i)))))
895 (setq rmail-message-vector nil)
896 (setq rmail-deleted-vector nil)))
897
898 (defun rmail-maybe-set-message-counters ()
899 (if (not (and rmail-deleted-vector
900 rmail-message-vector
901 rmail-current-message
902 rmail-total-messages))
903 (rmail-set-message-counters)))
904
905 (defun rmail-count-new-messages (&optional nomsg)
906 (let* ((case-fold-search nil)
907 (total-messages 0)
908 (messages-head nil)
909 (deleted-head nil))
910 (or nomsg (message "Counting new messages..."))
911 (goto-char (point-max))
912 ;; Put at the end of messages-head
913 ;; the entry for message N+1, which marks
914 ;; the end of message N. (N = number of messages).
915 (search-backward "\n\^_")
916 (forward-char 1)
917 (setq messages-head (list (point-marker)))
918 (rmail-set-message-counters-counter (point-min))
919 (setq rmail-current-message (1+ rmail-total-messages))
920 (setq rmail-total-messages
921 (+ rmail-total-messages total-messages))
922 (setq rmail-message-vector
923 (vconcat rmail-message-vector (cdr messages-head)))
924 (aset rmail-message-vector
925 rmail-current-message (car messages-head))
926 (setq rmail-deleted-vector
927 (concat rmail-deleted-vector deleted-head))
928 (setq rmail-summary-vector
929 (vconcat rmail-summary-vector (make-vector total-messages nil)))
930 (goto-char (point-min))
931 (or nomsg (message "Counting new messages...done (%d)" total-messages))))
932
933 (defun rmail-set-message-counters ()
934 (rmail-forget-messages)
935 (save-excursion
936 (save-restriction
937 (widen)
938 (let* ((point-save (point))
939 (total-messages 0)
940 (messages-after-point)
941 (case-fold-search nil)
942 (messages-head nil)
943 (deleted-head nil))
944 (message "Counting messages...")
945 (goto-char (point-max))
946 ;; Put at the end of messages-head
947 ;; the entry for message N+1, which marks
948 ;; the end of message N. (N = number of messages).
949 (search-backward "\n\^_")
950 (forward-char 1)
951 (setq messages-head (list (point-marker)))
952 (rmail-set-message-counters-counter (min (point) point-save))
953 (setq messages-after-point total-messages)
954 (rmail-set-message-counters-counter)
955 (setq rmail-total-messages total-messages)
956 (setq rmail-current-message
957 (min total-messages
958 (max 1 (- total-messages messages-after-point))))
959 (setq rmail-message-vector
960 (apply 'vector (cons (point-min-marker) messages-head))
961 rmail-deleted-vector (concat "D" deleted-head)
962 rmail-summary-vector (make-vector rmail-total-messages nil))
963 (message "Counting messages...done")))))
964
965 (defun rmail-set-message-counters-counter (&optional stop)
966 (while (search-backward "\n\^_\^L\n" stop t)
967 (forward-char 1)
968 (setq messages-head (cons (point-marker) messages-head))
969 (save-excursion
970 (setq deleted-head
971 (cons (if (search-backward ", deleted,"
972 (prog1 (point)
973 (forward-line 2))
974 t)
975 ?D ?\ )
976 deleted-head)))
977 (if (zerop (% (setq total-messages (1+ total-messages)) 20))
978 (message "Counting messages...%d" total-messages))))
979
980 (defun rmail-beginning-of-message ()
981 "Show current message starting from the beginning."
982 (interactive)
983 (rmail-show-message rmail-current-message))
984
985 (defun rmail-show-message (&optional n)
986 "Show message number N (prefix argument), counting from start of file."
987 (interactive "p")
988 (rmail-maybe-set-message-counters)
989 (widen)
990 (if (zerop rmail-total-messages)
991 (progn (narrow-to-region (point-min) (1- (point-max)))
992 (goto-char (point-min))
993 (setq mode-line-process nil))
994 (let (blurb)
995 (if (not n)
996 (setq n rmail-current-message)
997 (cond ((<= n 0)
998 (setq n 1
999 rmail-current-message 1
1000 blurb "No previous message"))
1001 ((> n rmail-total-messages)
1002 (setq n rmail-total-messages
1003 rmail-current-message rmail-total-messages
1004 blurb "No following message"))
1005 (t
1006 (setq rmail-current-message n))))
1007 (let ((beg (rmail-msgbeg n))
1008 (end (rmail-msgend n)))
1009 (goto-char beg)
1010 (forward-line 1)
1011 (if (= (following-char) ?0)
1012 (progn
1013 (rmail-reformat-message beg end)
1014 (rmail-set-attribute "unseen" nil))
1015 (search-forward "\n*** EOOH ***\n" end t)
1016 (narrow-to-region (point) end))
1017 (goto-char (point-min))
1018 (rmail-display-labels)
1019 (run-hooks 'rmail-show-message-hook)
1020 (if blurb
1021 (message blurb))))))
1022
1023 (defun rmail-next-message (n)
1024 "Show following message whether deleted or not.
1025 With prefix arg N, moves forward N messages, or backward if N is negative."
1026 (interactive "p")
1027 (rmail-maybe-set-message-counters)
1028 (rmail-show-message (+ rmail-current-message n)))
1029
1030 (defun rmail-previous-message (n)
1031 "Show previous message whether deleted or not.
1032 With prefix arg N, moves backward N messages, or forward if N is negative."
1033 (interactive "p")
1034 (rmail-next-message (- n)))
1035
1036 (defun rmail-next-undeleted-message (n)
1037 "Show following non-deleted message.
1038 With prefix arg N, moves forward N non-deleted messages,
1039 or backward if N is negative."
1040 (interactive "p")
1041 (rmail-maybe-set-message-counters)
1042 (let ((lastwin rmail-current-message)
1043 (current rmail-current-message))
1044 (while (and (> n 0) (< current rmail-total-messages))
1045 (setq current (1+ current))
1046 (if (not (rmail-message-deleted-p current))
1047 (setq lastwin current n (1- n))))
1048 (while (and (< n 0) (> current 1))
1049 (setq current (1- current))
1050 (if (not (rmail-message-deleted-p current))
1051 (setq lastwin current n (1+ n))))
1052 (if (/= lastwin rmail-current-message)
1053 (rmail-show-message lastwin))
1054 (if (< n 0)
1055 (error "No previous nondeleted message"))
1056 (if (> n 0)
1057 (error "No following nondeleted message"))))
1058
1059 (defun rmail-previous-undeleted-message (n)
1060 "Show previous non-deleted message.
1061 With prefix argument N, moves backward N non-deleted messages,
1062 or forward if N is negative."
1063 (interactive "p")
1064 (rmail-next-undeleted-message (- n)))
1065
1066 (defun rmail-first-message ()
1067 "Show first message in file."
1068 (interactive)
1069 (rmail-maybe-set-message-counters)
1070 (rmail-show-message 1))
1071
1072 (defun rmail-last-message ()
1073 "Show last message in file."
1074 (interactive)
1075 (rmail-maybe-set-message-counters)
1076 (rmail-show-message rmail-total-messages))
1077
1078 (defun rmail-what-message ()
1079 (let ((where (point))
1080 (low 1)
1081 (high rmail-total-messages)
1082 (mid (/ rmail-total-messages 2)))
1083 (while (> (- high low) 1)
1084 (if (>= where (rmail-msgbeg mid))
1085 (setq low mid)
1086 (setq high mid))
1087 (setq mid (+ low (/ (- high low) 2))))
1088 (if (>= where (rmail-msgbeg high)) high low)))
1089
1090 (defvar rmail-search-last-regexp nil)
1091 (defun rmail-search (regexp &optional reversep)
1092 "Show message containing next match for REGEXP.
1093 Search in reverse (earlier messages) with non-nil second arg REVERSEP.
1094 Interactively, empty argument means use same regexp used last time,
1095 and reverse search is specified by a negative numeric arg."
1096 (interactive
1097 (let* ((reversep (< (prefix-numeric-value current-prefix-arg) 0))
1098 (prompt
1099 (concat (if reversep "Reverse " "") "Rmail search (regexp): "))
1100 regexp)
1101 (if rmail-search-last-regexp
1102 (setq prompt (concat prompt
1103 "(default "
1104 rmail-search-last-regexp
1105 ") ")))
1106 (setq regexp (read-string prompt))
1107 (cond ((not (equal regexp ""))
1108 (setq rmail-search-last-regexp regexp))
1109 ((not rmail-search-last-regexp)
1110 (error "No previous Rmail search string")))
1111 (list rmail-search-last-regexp reversep)))
1112 (message "%sRmail search for %s..."
1113 (if reversep "Reverse " "")
1114 regexp)
1115 (rmail-maybe-set-message-counters)
1116 (let ((omin (point-min))
1117 (omax (point-max))
1118 (opoint (point))
1119 win
1120 (msg rmail-current-message))
1121 (unwind-protect
1122 (progn
1123 (widen)
1124 ;; Check messages one by one, advancing message number up or down
1125 ;; but searching forward through each message.
1126 (if reversep
1127 (while (and (null win) (> msg 1))
1128 (goto-char (rmail-msgbeg (setq msg (1- msg))))
1129 (setq win (re-search-forward
1130 regexp (rmail-msgend msg) t)))
1131 (while (and (null win) (< msg rmail-total-messages))
1132 (goto-char (rmail-msgbeg (setq msg (1+ msg))))
1133 (setq win (re-search-forward regexp (rmail-msgend msg) t)))))
1134 (if win
1135 (progn
1136 ;; If this is a reverse search and we found a message,
1137 ;; search backward thru this message to position point.
1138 (if reversep
1139 (progn
1140 (goto-char (rmail-msgend msg))
1141 (re-search-backward
1142 regexp (rmail-msgbeg msg) t)))
1143 (setq win (point))
1144 (rmail-show-message msg)
1145 (message "%sRmail search for %s...done"
1146 (if reversep "Reverse " "")
1147 regexp)
1148 (goto-char win))
1149 (goto-char opoint)
1150 (narrow-to-region omin omax)
1151 (ding)
1152 (message "Search failed: %s" regexp)))))
1153
1154 ;; Show the first message which has the `unseen' attribute.
1155 (defun rmail-first-unseen-message ()
1156 (let ((current 1)
1157 found)
1158 (save-restriction
1159 (widen)
1160 (while (and (not found) (< current rmail-total-messages))
1161 (setq current (1+ current))
1162 (if (rmail-message-labels-p current ", ?\\(unseen\\),")
1163 (setq found current))))
1164 (if found
1165 (rmail-show-message found))))
1166 \f
1167 ;;;; *** Rmail Message Deletion Commands ***
1168
1169 (defun rmail-message-deleted-p (n)
1170 (= (aref rmail-deleted-vector n) ?D))
1171
1172 (defun rmail-set-message-deleted-p (n state)
1173 (aset rmail-deleted-vector n (if state ?D ?\ )))
1174
1175 (defun rmail-delete-message ()
1176 "Delete this message and stay on it."
1177 (interactive)
1178 (rmail-set-attribute "deleted" t))
1179
1180 (defun rmail-undelete-previous-message ()
1181 "Back up to deleted message, select it, and undelete it."
1182 (interactive)
1183 (let ((msg rmail-current-message))
1184 (while (and (> msg 0)
1185 (not (rmail-message-deleted-p msg)))
1186 (setq msg (1- msg)))
1187 (if (= msg 0)
1188 (error "No previous deleted message")
1189 (if (/= msg rmail-current-message)
1190 (rmail-show-message msg))
1191 (rmail-set-attribute "deleted" nil))))
1192
1193 (defun rmail-delete-forward (&optional backward)
1194 "Delete this message and move to next nondeleted one.
1195 Deleted messages stay in the file until the \\[rmail-expunge] command is given.
1196 With prefix argument, delete and move backward. If there is no nondeleted
1197 message to move to in the preferred or specified direction, move in the
1198 other direction."
1199 (interactive "P")
1200 (rmail-set-attribute "deleted" t)
1201 (condition-case ()
1202 (rmail-next-undeleted-message (if backward -1 1))
1203 (error
1204 (condition-case ()
1205 (rmail-previous-undeleted-message (if backward -1 1))
1206 (error nil)))))
1207
1208 (defun rmail-delete-backward ()
1209 "Delete this message and move to previous nondeleted one.
1210 Deleted messages stay in the file until the \\[rmail-expunge] command is given."
1211 (interactive)
1212 (rmail-delete-forward t))
1213
1214 (defun rmail-expunge ()
1215 "Actually erase all deleted messages in the file."
1216 (interactive)
1217 (message "Expunging deleted messages...")
1218 ;; Discard all undo records for this buffer.
1219 (or (eq buffer-undo-list t)
1220 (setq buffer-undo-list nil))
1221 (rmail-maybe-set-message-counters)
1222 (let* ((omax (- (buffer-size) (point-max)))
1223 (omin (- (buffer-size) (point-min)))
1224 (opoint (if (and (> rmail-current-message 0)
1225 (= ?D (aref rmail-deleted-vector rmail-current-message)))
1226 0 (- (point) (point-min))))
1227 (messages-head (cons (aref rmail-message-vector 0) nil))
1228 (messages-tail messages-head)
1229 ;; Don't make any undo records for the expunging.
1230 (buffer-undo-list t)
1231 (win))
1232 (unwind-protect
1233 (save-excursion
1234 (widen)
1235 (goto-char (point-min))
1236 (let ((counter 0)
1237 (number 1)
1238 (total rmail-total-messages)
1239 (new-message-number rmail-current-message)
1240 (new-summary nil)
1241 (buffer-read-only nil)
1242 (messages rmail-message-vector)
1243 (deleted rmail-deleted-vector)
1244 (summary rmail-summary-vector))
1245 (setq rmail-total-messages nil
1246 rmail-current-message nil
1247 rmail-message-vector nil
1248 rmail-deleted-vector nil
1249 rmail-summary-vector nil)
1250 (while (<= number total)
1251 (if (= (aref deleted number) ?D)
1252 (progn
1253 (delete-region
1254 (marker-position (aref messages number))
1255 (marker-position (aref messages (1+ number))))
1256 (move-marker (aref messages number) nil)
1257 (if (> new-message-number counter)
1258 (setq new-message-number (1- new-message-number))))
1259 (setq counter (1+ counter))
1260 (setq messages-tail
1261 (setcdr messages-tail
1262 (cons (aref messages number) nil)))
1263 (setq new-summary
1264 (cons (if (= counter number) (aref summary (1- number)))
1265 new-summary)))
1266 (if (zerop (% (setq number (1+ number)) 20))
1267 (message "Expunging deleted messages...%d" number)))
1268 (setq messages-tail
1269 (setcdr messages-tail
1270 (cons (aref messages number) nil)))
1271 (setq rmail-current-message new-message-number
1272 rmail-total-messages counter
1273 rmail-message-vector (apply 'vector messages-head)
1274 rmail-deleted-vector (make-string (1+ counter) ?\ )
1275 rmail-summary-vector (vconcat (nreverse new-summary))
1276 win t)))
1277 (message "Expunging deleted messages...done")
1278 (if (not win)
1279 (narrow-to-region (- (buffer-size) omin) (- (buffer-size) omax)))
1280 (rmail-show-message
1281 (if (zerop rmail-current-message) 1 nil))
1282 (forward-char opoint))))
1283 \f
1284 ;;;; *** Rmail Mailing Commands ***
1285
1286 (defun rmail-mail ()
1287 "Send mail in another window. While composing the message, use
1288 \\[mail-yank-original] to yank the original message into it."
1289 (interactive)
1290 (mail-other-window nil nil nil nil nil (current-buffer)))
1291
1292 (defun rmail-continue ()
1293 "Continue composing outgoing message previously being composed."
1294 (interactive)
1295 (mail-other-window t))
1296
1297 (defun rmail-reply (just-sender)
1298 "Reply to the current message.
1299 Normally include CC: to all other recipients of original message;
1300 prefix argument means ignore them. While composing the reply,
1301 use \\[mail-yank-original] to yank the original message into it."
1302 (interactive "P")
1303 (let (from reply-to cc subject date to message-id resent-reply-to)
1304 (save-excursion
1305 (save-restriction
1306 (widen)
1307 (goto-char (rmail-msgbeg rmail-current-message))
1308 (forward-line 1)
1309 (if (= (following-char) ?0)
1310 (narrow-to-region
1311 (progn (forward-line 2)
1312 (point))
1313 (progn (search-forward "\n\n" (rmail-msgend rmail-current-message)
1314 'move)
1315 (point)))
1316 (narrow-to-region (point)
1317 (progn (search-forward "\n*** EOOH ***\n")
1318 (beginning-of-line) (point))))
1319 (setq resent-reply-to (mail-fetch-field "resent-reply-to" t)
1320 from (mail-fetch-field "from")
1321 reply-to (or resent-reply-to
1322 (mail-fetch-field "reply-to" nil t)
1323 from)
1324 cc (cond (just-sender nil)
1325 (resent-reply-to (mail-fetch-field "resent-cc" t))
1326 (t (mail-fetch-field "cc" nil t)))
1327 subject (or (and resent-reply-to
1328 (mail-fetch-field "resent-subject" t))
1329 (mail-fetch-field "subject"))
1330 date (cond (resent-reply-to
1331 (mail-fetch-field "resent-date" t))
1332 ((mail-fetch-field "date")))
1333 to (cond (resent-reply-to
1334 (mail-fetch-field "resent-to" t))
1335 ((mail-fetch-field "to" nil t))
1336 ;((mail-fetch-field "apparently-to")) ack gag barf
1337 (t ""))
1338 message-id (cond (resent-reply-to
1339 (mail-fetch-field "resent-message-id" t))
1340 ((mail-fetch-field "message-id"))))))
1341 (and subject
1342 (string-match "\\`Re: " subject)
1343 (setq subject (substring subject 4)))
1344 (mail-other-window nil
1345 (mail-strip-quoted-names reply-to)
1346 subject
1347 (rmail-make-in-reply-to-field from date message-id)
1348 (if just-sender
1349 nil
1350 (let* ((cc-list (rmail-dont-reply-to
1351 (mail-strip-quoted-names
1352 (if (null cc) to (concat to ", " cc))))))
1353 (if (string= cc-list "") nil cc-list)))
1354 (current-buffer)
1355 (list (list '(lambda (buf msgnum)
1356 (save-excursion
1357 (set-buffer buf)
1358 (rmail-set-attribute "answered" t msgnum)))
1359 (current-buffer) rmail-current-message)))))
1360
1361 (defun rmail-make-in-reply-to-field (from date message-id)
1362 (cond ((not from)
1363 (if message-id
1364 message-id
1365 nil))
1366 (mail-use-rfc822
1367 (require 'rfc822)
1368 (let ((tem (car (rfc822-addresses from))))
1369 (if message-id
1370 (if (string-match
1371 (regexp-quote (if (string-match "@[^@]*\\'" tem)
1372 (substring tem 0 (match-beginning 0))
1373 tem))
1374 message-id)
1375 ;; Message-ID is sufficiently informative
1376 message-id
1377 (concat message-id " (" tem ")"))
1378 ;; Use prin1 to fake RFC822 quoting
1379 (let ((field (prin1-to-string tem)))
1380 (if date
1381 (concat field "'s message of " date)
1382 field)))))
1383 ((let* ((foo "[^][\000-\037\177-\377()<>@,;:\\\" ]+")
1384 (bar "[^][\000-\037\177-\377()<>@,;:\\\"]+"))
1385 ;; Can't use format because format loses on \000 (unix *^&%*^&%$!!)
1386 (or (string-match (concat "\\`[ \t]*\\(" bar
1387 "\\)\\(<" foo "@" foo ">\\)?[ \t]*\\'")
1388 ;; "Unix Loser <Foo@bar.edu>" => "Unix Loser"
1389 from)
1390 (string-match (concat "\\`[ \t]*<" foo "@" foo ">[ \t]*(\\("
1391 bar "\\))[ \t]*\\'")
1392 ;; "<Bugs@bar.edu>" (Losing Unix) => "Losing Unix"
1393 from)))
1394 (let ((start (match-beginning 1))
1395 (end (match-end 1)))
1396 ;; Trim whitespace which above regexp match allows
1397 (while (and (< start end)
1398 (memq (aref from start) '(?\t ?\ )))
1399 (setq start (1+ start)))
1400 (while (and (< start end)
1401 (memq (aref from (1- end)) '(?\t ?\ )))
1402 (setq end (1- end)))
1403 (let ((field (substring from start end)))
1404 (if date (setq field (concat "message from " field " on " date)))
1405 (if message-id
1406 ;; "<AA259@bar.edu> (message from Unix Loser on 1-Apr-89)"
1407 (concat message-id " (" field ")")
1408 field))))
1409 (t
1410 ;; If we can't kludge it simply, do it correctly
1411 (let ((mail-use-rfc822 t))
1412 (rmail-make-in-reply-to-field from date message-id)))))
1413
1414 (defun rmail-forward ()
1415 "Forward the current message to another user."
1416 (interactive)
1417 (let ((forward-buffer (current-buffer))
1418 (subject (concat "["
1419 (mail-strip-quoted-names (mail-fetch-field "From"))
1420 ": " (or (mail-fetch-field "Subject") "") "]")))
1421 ;; If only one window, use it for the mail buffer.
1422 ;; Otherwise, use another window for the mail buffer
1423 ;; so that the Rmail buffer remains visible
1424 ;; and sending the mail will get back to it.
1425 (if (funcall (if (one-window-p t)
1426 (function mail)
1427 (function mail-other-window))
1428 nil nil subject nil nil nil
1429 (list (list (function (lambda (buf msgnum)
1430 (save-excursion
1431 (set-buffer buf)
1432 (rmail-set-attribute "forwarded" t msgnum))))
1433 (current-buffer)
1434 rmail-current-message)))
1435 (save-excursion
1436 (goto-char (point-max))
1437 (forward-line 1)
1438 (insert-buffer forward-buffer)))))
1439
1440 (defun rmail-resend (address &optional from comment mail-alias-file)
1441 "Resend current message to ADDRESSES.
1442 ADDRESSES should be a single address, a a string consisting of several
1443 addresses separated by commas, or a list of addresses.
1444
1445 Optional FROM is the address to resend the message from, and
1446 defaults to the username of the person redistributing the message.
1447 Optional COMMENT is a string that will be inserted as a comment in the
1448 resent message.
1449 Optional ALIAS-FILE is alternate aliases file to be used by sendmail,
1450 typically for purposes of moderating a list."
1451 (interactive "sResend to: ")
1452 (if (not from) (setq from (user-login-name)))
1453 (let ((tembuf (generate-new-buffer " sendmail temp"))
1454 (mail-header-separator "")
1455 (case-fold-search nil)
1456 (mailbuf (current-buffer)))
1457 (unwind-protect
1458 (save-excursion
1459 ;;>> Copy message into temp buffer
1460 (set-buffer tembuf)
1461 (insert-buffer-substring mailbuf)
1462 (goto-char (point-min))
1463 ;;>> Insert resent-from:
1464 (insert "Resent-From: " from "\n")
1465 (insert "Resent-Date: " (current-time-string) "\n")
1466 ;;>> Insert resent-to: and bcc if need be.
1467 (let ((before (point)))
1468 (insert "Resent-To: " (if (stringp address)
1469 address
1470 (mapconcat 'identity address ",\n\t"))
1471 "\n")
1472 (expand-mail-aliases before (point)))
1473 ;;>> Set up comment, if any.
1474 (if (and (sequencep comment) (not (zerop (length comment))))
1475 (let ((before (point))
1476 after)
1477 (insert comment)
1478 (or (eolp) (insert "\n"))
1479 (setq after (point))
1480 (goto-char before)
1481 (while (< (point) after)
1482 (insert "Resent-Comment: ")
1483 (forward-line 1))))
1484 ;; Don't expand aliases in the destination fields
1485 ;; of the original message.
1486 (let (mail-aliases)
1487 (sendmail-send-it)))
1488 (kill-buffer tembuf))))
1489
1490 (defvar mail-unsent-separator "^ ----- Unsent message follows -----$")
1491
1492 (defun rmail-retry-failure ()
1493 "Edit a mail message which is based on the contents of the current message.
1494 For a message rejected by the mail system, extract the interesting headers and
1495 the body of the original message; otherwise copy the current message."
1496 (interactive)
1497 (require 'mail-utils)
1498 (let (to subj irp2 cc orig-message)
1499 (save-excursion
1500 ;; Narrow down to just the quoted original message
1501 (rmail-beginning-of-message)
1502 (or (re-search-forward mail-unsent-separator nil t)
1503 (error "Cannot parse this as a failure message"))
1504 (save-restriction
1505 (narrow-to-region (point) (point-max))
1506 ;; Now mail-fetch-field will get from headers of the original message,
1507 ;; not from the headers of the rejection.
1508 (setq to (mail-fetch-field "To")
1509 subj (mail-fetch-field "Subject")
1510 irp2 (mail-fetch-field "In-reply-to")
1511 cc (mail-fetch-field "Cc"))
1512 ;; Get the entire text (not headers) of the original message.
1513 (setq orig-message
1514 (buffer-substring
1515 (progn (search-forward "\n\n") (point))
1516 (point-max)))))
1517 ;; Start sending a new message; default header fields from the original.
1518 (if (mail-other-window nil to subj irp2 cc (current-buffer))
1519 ;; Insert original text as initial text of new draft message.
1520 (progn
1521 (goto-char (point-max))
1522 (insert orig-message)
1523 (goto-char (point-min))
1524 (end-of-line)))))
1525 \f
1526 ;;;; *** Rmail Specify Inbox Files ***
1527
1528 (autoload 'set-rmail-inbox-list "rmailmsc"
1529 "Set the inbox list of the current RMAIL file to FILE-NAME.
1530 This may be a list of file names separated by commas.
1531 If FILE-NAME is empty, remove any inbox list."
1532 t)
1533
1534 ;;;; *** Rmail Commands for Labels ***
1535
1536 (autoload 'rmail-add-label "rmailkwd"
1537 "Add LABEL to labels associated with current RMAIL message.
1538 Completion is performed over known labels when reading."
1539 t)
1540
1541 (autoload 'rmail-kill-label "rmailkwd"
1542 "Remove LABEL from labels associated with current RMAIL message.
1543 Completion is performed over known labels when reading."
1544 t)
1545
1546 (autoload 'rmail-next-labeled-message "rmailkwd"
1547 "Show next message with LABEL. Defaults to last label used.
1548 With prefix argument N moves forward N messages with this label."
1549 t)
1550
1551 (autoload 'rmail-previous-labeled-message "rmailkwd"
1552 "Show previous message with LABEL. Defaults to last label used.
1553 With prefix argument N moves backward N messages with this label."
1554 t)
1555
1556 ;;;; *** Rmail Edit Mode ***
1557
1558 (autoload 'rmail-edit-current-message "rmailedit"
1559 "Edit the contents of the current message"
1560 t)
1561 \f
1562 ;;;; *** Rmail Summary Mode ***
1563
1564 (autoload 'rmail-summary "rmailsum"
1565 "Display a summary of all messages, one line per message."
1566 t)
1567
1568 (autoload 'rmail-summary-by-labels "rmailsum"
1569 "Display a summary of all messages with one or more LABELS.
1570 LABELS should be a string containing the desired labels, separated by commas."
1571 t)
1572
1573 (autoload 'rmail-summary-by-recipients "rmailsum"
1574 "Display a summary of all messages with the given RECIPIENTS.
1575 Normally checks the To, From and Cc fields of headers; but if PRIMARY-ONLY
1576 is non-nil (prefix arg given), only look in the To and From fields.
1577 RECIPIENTS is a string of names separated by commas."
1578 t)
1579 \f
1580 ;;;; *** Rmail output messages to files ***
1581
1582 (autoload 'rmail-output-to-rmail-file "rmailout"
1583 "Append the current message to an Rmail file named FILE-NAME.
1584 If the file does not exist, ask if it should be created.
1585 If file is being visited, the message is appended to the Emacs
1586 buffer visiting that file."
1587 t)
1588
1589 (autoload 'rmail-output "rmailout"
1590 "Append this message to Unix mail file named FILE-NAME."
1591 t)
1592
1593 ;;;; *** Rmail undigestification ***
1594
1595 (autoload 'undigestify-rmail-message "undigest"
1596 "Break up a digest message into its constituent messages.
1597 Leaves original message, deleted, before the undigestified messages."
1598 t)