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