*** empty log message ***
[bpt/emacs.git] / lisp / mail / rmail.el
1 ;;; rmail.el --- main code of "RMAIL" mail reader for Emacs.
2
3 ;; Maintainer: FSF
4 ;; Last-Modified: 24 Apr 1992
5 ;; Keywords: mail
6
7 ;; Copyright (C) 1985, 1986, 1987, 1988, 1991, 1992 Free Software Foundation, Inc.
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to
23 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
24
25 ;;; Code:
26
27 ;; Souped up by shane@mit-ajax based on ideas of rlk@athena.mit.edu
28 ;; New features include attribute and keyword support, message
29 ;; selection by dispatch table, summary by attributes and keywords,
30 ;; expunging by dispatch table, sticky options for file commands.
31
32 (require 'mail-utils)
33 ; These variables now declared paths.el
34 ;(defvar rmail-spool-directory "/usr/spool/mail/"
35 ; "This is the name of the directory used by the system mailer for\n\
36 ;delivering new mail. It's name should end with a slash.")
37 ;(defvar rmail-file-name
38 ; (expand-file-name "~/RMAIL")
39 ; "")
40
41 ;;;###autoload
42 (defvar rmail-dont-reply-to-names nil "\
43 *A regexp specifying names to prune of reply to messages.
44 nil means dont reply to yourself.")
45
46 ;;;###autoload
47 (defvar rmail-default-dont-reply-to-names "info-" "\
48 A regular expression specifying part of the value of the default value of
49 the variable `rmail-dont-reply-to-names', for when the user does not set
50 `rmail-dont-reply-to-names' explicitly. (The other part of the default
51 value is the user's name.)
52 It is useful to set this variable in the site customisation file.")
53
54 ;;;###autoload
55 (defvar rmail-ignored-headers "^via:\\|^mail-from:\\|^origin:\\|^status:\\|^received:\\|^message-id:\\|^summary-line:" "\
56 *Gubbish headers one would rather not see.")
57
58 ;;;###autoload
59 (defvar rmail-delete-after-output nil "\
60 *Non-nil means automatically delete a message that is copied to a file.")
61
62 ;;;###autoload
63 (defconst rmail-primary-inbox-list nil "\
64 *List of files which are inboxes for user's primary mail file ~/RMAIL.
65 `nil' means the default, which is (\"/usr/spool/mail/$USER\" \"~/mbox\")
66 \(the first name varies depending on the operating system,
67 and the value of the environment variable MAIL overrides it).")
68
69 ;; these may be altered by site-init.el to match the format of mmdf files
70 ;; delimitation used on a given host (delim1 and delim2 from the config
71 ;; files)
72
73 (defvar mmdf-delim1 "^\001\001\001\001\n"
74 "Regexp marking the start of an mmdf message")
75 (defvar mmdf-delim2 "^\001\001\001\001\n"
76 "Regexp marking the end of an mmdf message")
77
78 (defvar rmail-message-filter nil
79 "If non nil, is a filter function for new headers in RMAIL.
80 Called with region narrowed to unformatted header.")
81
82 (defvar rmail-mode-map nil)
83
84 (defvar rmail-inbox-list nil)
85 (defvar rmail-keywords nil)
86
87 ;; Message counters and markers. Deleted flags.
88
89 (defvar rmail-current-message nil)
90 (defvar rmail-total-messages nil)
91 (defvar rmail-message-vector nil)
92 (defvar rmail-deleted-vector nil)
93
94 ;; These are used by autoloaded rmail-summary.
95
96 (defvar rmail-summary-buffer nil)
97 (defvar rmail-summary-vector nil)
98
99 ;; `Sticky' default variables.
100
101 ;; Last individual label specified to a or k.
102 (defvar rmail-last-label nil)
103 ;; Last set of labels specified to C-M-n or C-M-p or C-M-l.
104 (defvar rmail-last-multi-labels nil)
105 (defvar rmail-last-file nil)
106 (defvar rmail-last-rmail-file nil)
107
108 ;;; Regexp matching the delimiter of messages in UNIX mail format
109 ;;; (UNIX From lines), minus the initial ^. Note that if you change
110 ;;; this expression, you must change the code in rmail-nuke-pinhead-header
111 ;;; that knows the exact ordering of the \\( \\) subexpressions.
112 (defvar rmail-unix-mail-delimiter
113 "From \\([^ \n]*\\(\\|\".*\"[^ \n]*\\)\\) ?\\([^ \n]*\\) \\([^ ]*\\) *\\([0-9]*\\) \\([0-9:]*\\)\\( ?[A-Z]?[A-Z][A-Z]T\\( DST\\)?\\| ?[-+]?[0-9][0-9][0-9][0-9]\\|\\) [0-9][0-9]\\([0-9]*\\) *\\(remote from [^\n]*\\)?\n" nil)
114 \f
115 ;;;; *** Rmail Mode ***
116
117 ;;;###autoload
118 (defun rmail (&optional file-name-arg)
119 "Read and edit incoming mail.
120 Moves messages into file named by rmail-file-name (a babyl format file)
121 and edits that file in RMAIL Mode.
122 Type \\[describe-mode] once editing that file, for a list of RMAIL commands.
123
124 May be called with filename as argument; then performs rmail editing on
125 that file, but does not copy any new mail into the file."
126 (interactive (if current-prefix-arg
127 (list (read-file-name "Run rmail on RMAIL file: "
128 nil nil t))))
129 (or rmail-last-file
130 (setq rmail-last-file (expand-file-name "~/xmail")))
131 (or rmail-last-rmail-file
132 (setq rmail-last-rmail-file (expand-file-name "~/XMAIL")))
133 (let* ((file-name (expand-file-name (or file-name-arg rmail-file-name)))
134 (existed (get-file-buffer file-name)))
135 ;; Like find-file, but in the case where a buffer existed
136 ;; and the file was reverted, recompute the message-data.
137 (if (and existed (not (verify-visited-file-modtime existed)))
138 (progn
139 ;; Don't be confused by apparent local-variables spec
140 ;; in the last message in the RMAIL file.
141 (let ((inhibit-local-variables t))
142 (find-file file-name))
143 (if (verify-visited-file-modtime existed)
144 (progn (rmail-forget-messages)
145 (rmail-set-message-counters))))
146 (let ((inhibit-local-variables t))
147 (find-file file-name)))
148 (if (and existed (> (buffer-size) 0))
149 ;; Buffer not new and not empty; ensure in proper mode, but that's all.
150 (or (eq major-mode 'rmail-mode)
151 (rmail-mode-2))
152 (rmail-mode-2)
153 ;; Provide default set of inboxes for primary mail file ~/RMAIL.
154 (and (null rmail-inbox-list)
155 (null file-name-arg)
156 (setq rmail-inbox-list
157 (or rmail-primary-inbox-list
158 (list "~/mbox"
159 (or (getenv "MAIL")
160 (concat rmail-spool-directory
161 (user-original-login-name)))))))
162 ;; Convert all or part to Babyl file if possible.
163 (rmail-convert-file)
164 (goto-char (point-max))
165 (if (null rmail-inbox-list)
166 (progn
167 (rmail-set-message-counters)
168 (rmail-show-message))))
169 (rmail-get-new-mail)
170 ;; Show the first unseen message, which might be from a previous session
171 ;; or might have been just read in by rmail-get-new-mail.
172 (rmail-first-unseen-message)))
173
174 ;; Given the value of MAILPATH, return a list of inbox file names.
175 ;; This is turned off because it is not clear that the user wants
176 ;; all these inboxes to feed into the primary rmail file.
177 ; (defun rmail-convert-mailpath (string)
178 ; (let (idx list)
179 ; (while (setq idx (string-match "[%:]" string))
180 ; (let ((this (substring string 0 idx)))
181 ; (setq string (substring string (1+ idx)))
182 ; (setq list (cons (if (string-match "%" this)
183 ; (substring this 0 (string-match "%" this))
184 ; this)
185 ; list))))
186 ; list))
187
188 ; I have checked that adding "-*- rmail -*-" to the BABYL OPTIONS line
189 ; will not cause emacs 18.55 problems.
190
191 (defun rmail-convert-file ()
192 (let (convert)
193 (widen)
194 (goto-char (point-min))
195 ;; If file doesn't start like a Babyl file,
196 ;; convert it to one, by adding a header and converting each message.
197 (cond ((looking-at "BABYL OPTIONS:"))
198 ((looking-at "Version: 5\n")
199 ;; Losing babyl file made by old version of Rmail.
200 ;; Just fix the babyl file header; don't make a new one,
201 ;; so we don't lose the Labels: file attribute, etc.
202 (let ((buffer-read-only nil))
203 (insert "BABYL OPTIONS: -*- rmail -*-\n")))
204 (t
205 (setq convert t)
206 (rmail-insert-rmail-file-header)))
207 ;; If file was not a Babyl file or if there are
208 ;; Unix format messages added at the end,
209 ;; convert file as necessary.
210 (if (or convert
211 (progn (goto-char (point-max))
212 (search-backward "\^_")
213 (forward-char 1)
214 (looking-at "\n*From ")))
215 (let ((buffer-read-only nil))
216 (message "Converting to Babyl format...")
217 (narrow-to-region (point) (point-max))
218 (rmail-convert-to-babyl-format)
219 (message "Converting to Babyl format...done")))))
220
221 ; I have checked that adding "-*- rmail -*-" to the BABYL OPTIONS line
222 ; will not cause emacs 18.55 problems.
223
224 (defun rmail-insert-rmail-file-header ()
225 (let ((buffer-read-only nil))
226 (insert "BABYL OPTIONS: -*- rmail -*-
227 Version: 5
228 Labels:
229 Note: This is the header of an rmail file.
230 Note: If you are seeing it in rmail,
231 Note: it means the file has no messages in it.\n\^_")))
232
233 (if rmail-mode-map
234 nil
235 (setq rmail-mode-map (make-keymap))
236 (suppress-keymap rmail-mode-map)
237 (define-key rmail-mode-map "." 'rmail-beginning-of-message)
238 (define-key rmail-mode-map " " 'scroll-up)
239 (define-key rmail-mode-map "\177" 'scroll-down)
240 (define-key rmail-mode-map "n" 'rmail-next-undeleted-message)
241 (define-key rmail-mode-map "p" 'rmail-previous-undeleted-message)
242 (define-key rmail-mode-map "\en" 'rmail-next-message)
243 (define-key rmail-mode-map "\ep" 'rmail-previous-message)
244 (define-key rmail-mode-map "\e\C-n" 'rmail-next-labeled-message)
245 (define-key rmail-mode-map "\e\C-p" 'rmail-previous-labeled-message)
246 (define-key rmail-mode-map "a" 'rmail-add-label)
247 (define-key rmail-mode-map "k" 'rmail-kill-label)
248 (define-key rmail-mode-map "d" 'rmail-delete-forward)
249 (define-key rmail-mode-map "u" 'rmail-undelete-previous-message)
250 (define-key rmail-mode-map "x" 'rmail-expunge)
251 (define-key rmail-mode-map "s" 'rmail-expunge-and-save)
252 (define-key rmail-mode-map "g" 'rmail-get-new-mail)
253 (define-key rmail-mode-map "h" 'rmail-summary)
254 (define-key rmail-mode-map "\e\C-h" 'rmail-summary)
255 (define-key rmail-mode-map "l" 'rmail-summary-by-labels)
256 (define-key rmail-mode-map "\e\C-l" 'rmail-summary-by-labels)
257 (define-key rmail-mode-map "\e\C-r" 'rmail-summary-by-recipients)
258 (define-key rmail-mode-map "\e\C-s" 'rmail-summary-by-regexp)
259 (define-key rmail-mode-map "t" 'rmail-toggle-header)
260 (define-key rmail-mode-map "m" 'rmail-mail)
261 (define-key rmail-mode-map "r" 'rmail-reply)
262 (define-key rmail-mode-map "\e\C-m" 'rmail-retry-failure)
263 (define-key rmail-mode-map "c" 'rmail-continue)
264 (define-key rmail-mode-map "f" 'rmail-forward)
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 (error "No previous nondeleted message"))
1077 (if (> n 0)
1078 (error "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 reversep)
1113 "Show message containing next match for REGEXP.
1114 Search in reverse (earlier messages) with non-nil second arg REVERSEP.
1115 Interactively, empty argument means use same regexp used last time,
1116 and reverse search is specified by a negative numeric arg."
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 reversep)))
1133 (message "%sRmail search for %s..."
1134 (if reversep "Reverse " "")
1135 regexp)
1136 (rmail-maybe-set-message-counters)
1137 (let ((omin (point-min))
1138 (omax (point-max))
1139 (opoint (point))
1140 win
1141 (msg rmail-current-message))
1142 (unwind-protect
1143 (progn
1144 (widen)
1145 ;; Check messages one by one, advancing message number up or down
1146 ;; but searching forward through each message.
1147 (if reversep
1148 (while (and (null win) (> msg 1))
1149 (goto-char (rmail-msgbeg (setq msg (1- msg))))
1150 (setq win (re-search-forward
1151 regexp (rmail-msgend msg) t)))
1152 (while (and (null win) (< msg rmail-total-messages))
1153 (goto-char (rmail-msgbeg (setq msg (1+ msg))))
1154 (setq win (re-search-forward regexp (rmail-msgend msg) t)))))
1155 (if win
1156 (progn
1157 ;; If this is a reverse search and we found a message,
1158 ;; search backward thru this message to position point.
1159 (if reversep
1160 (progn
1161 (goto-char (rmail-msgend msg))
1162 (re-search-backward
1163 regexp (rmail-msgbeg msg) t)))
1164 (setq win (point))
1165 (rmail-show-message msg)
1166 (message "%sRmail search for %s...done"
1167 (if reversep "Reverse " "")
1168 regexp)
1169 (goto-char win))
1170 (goto-char opoint)
1171 (narrow-to-region omin omax)
1172 (ding)
1173 (message "Search failed: %s" regexp)))))
1174
1175 ;; Show the first message which has the `unseen' attribute.
1176 (defun rmail-first-unseen-message ()
1177 (let ((current 1)
1178 found)
1179 (save-restriction
1180 (widen)
1181 (while (and (not found) (< current rmail-total-messages))
1182 (if (rmail-message-labels-p current ", ?\\(unseen\\),")
1183 (setq found current))
1184 (setq current (1+ current))))
1185 (if found
1186 (rmail-show-message found))))
1187 \f
1188 ;;;; *** Rmail Message Deletion Commands ***
1189
1190 (defun rmail-message-deleted-p (n)
1191 (= (aref rmail-deleted-vector n) ?D))
1192
1193 (defun rmail-set-message-deleted-p (n state)
1194 (aset rmail-deleted-vector n (if state ?D ?\ )))
1195
1196 (defun rmail-delete-message ()
1197 "Delete this message and stay on it."
1198 (interactive)
1199 (rmail-set-attribute "deleted" t))
1200
1201 (defun rmail-undelete-previous-message ()
1202 "Back up to deleted message, select it, and undelete it."
1203 (interactive)
1204 (let ((msg rmail-current-message))
1205 (while (and (> msg 0)
1206 (not (rmail-message-deleted-p msg)))
1207 (setq msg (1- msg)))
1208 (if (= msg 0)
1209 (error "No previous deleted message")
1210 (if (/= msg rmail-current-message)
1211 (rmail-show-message msg))
1212 (rmail-set-attribute "deleted" nil))))
1213
1214 (defun rmail-delete-forward (&optional backward)
1215 "Delete this message and move to next nondeleted one.
1216 Deleted messages stay in the file until the \\[rmail-expunge] command is given.
1217 With prefix argument, delete and move backward. If there is no nondeleted
1218 message to move to in the preferred or specified direction, move in the
1219 other direction."
1220 (interactive "P")
1221 (rmail-set-attribute "deleted" t)
1222 (condition-case ()
1223 (rmail-next-undeleted-message (if backward -1 1))
1224 (error
1225 (condition-case ()
1226 (rmail-previous-undeleted-message (if backward -1 1))
1227 (error nil)))))
1228
1229 (defun rmail-delete-backward ()
1230 "Delete this message and move to previous nondeleted one.
1231 Deleted messages stay in the file until the \\[rmail-expunge] command is given."
1232 (interactive)
1233 (rmail-delete-forward t))
1234
1235 (defun rmail-expunge ()
1236 "Actually erase all deleted messages in the file."
1237 (interactive)
1238 (message "Expunging deleted messages...")
1239 ;; Discard all undo records for this buffer.
1240 (or (eq buffer-undo-list t)
1241 (setq buffer-undo-list nil))
1242 (rmail-maybe-set-message-counters)
1243 (let* ((omax (- (buffer-size) (point-max)))
1244 (omin (- (buffer-size) (point-min)))
1245 (opoint (if (and (> rmail-current-message 0)
1246 (= ?D (aref rmail-deleted-vector rmail-current-message)))
1247 0 (- (point) (point-min))))
1248 (messages-head (cons (aref rmail-message-vector 0) nil))
1249 (messages-tail messages-head)
1250 ;; Don't make any undo records for the expunging.
1251 (buffer-undo-list t)
1252 (win))
1253 (unwind-protect
1254 (save-excursion
1255 (widen)
1256 (goto-char (point-min))
1257 (let ((counter 0)
1258 (number 1)
1259 (total rmail-total-messages)
1260 (new-message-number rmail-current-message)
1261 (new-summary nil)
1262 (buffer-read-only nil)
1263 (messages rmail-message-vector)
1264 (deleted rmail-deleted-vector)
1265 (summary rmail-summary-vector))
1266 (setq rmail-total-messages nil
1267 rmail-current-message nil
1268 rmail-message-vector nil
1269 rmail-deleted-vector nil
1270 rmail-summary-vector nil)
1271 (while (<= number total)
1272 (if (= (aref deleted number) ?D)
1273 (progn
1274 (delete-region
1275 (marker-position (aref messages number))
1276 (marker-position (aref messages (1+ number))))
1277 (move-marker (aref messages number) nil)
1278 (if (> new-message-number counter)
1279 (setq new-message-number (1- new-message-number))))
1280 (setq counter (1+ counter))
1281 (setq messages-tail
1282 (setcdr messages-tail
1283 (cons (aref messages number) nil)))
1284 (setq new-summary
1285 (cons (if (= counter number) (aref summary (1- number)))
1286 new-summary)))
1287 (if (zerop (% (setq number (1+ number)) 20))
1288 (message "Expunging deleted messages...%d" number)))
1289 (setq messages-tail
1290 (setcdr messages-tail
1291 (cons (aref messages number) nil)))
1292 (setq rmail-current-message new-message-number
1293 rmail-total-messages counter
1294 rmail-message-vector (apply 'vector messages-head)
1295 rmail-deleted-vector (make-string (1+ counter) ?\ )
1296 rmail-summary-vector (vconcat (nreverse new-summary))
1297 win t)))
1298 (message "Expunging deleted messages...done")
1299 (if (not win)
1300 (narrow-to-region (- (buffer-size) omin) (- (buffer-size) omax)))
1301 (rmail-show-message
1302 (if (zerop rmail-current-message) 1 nil))
1303 (forward-char opoint))))
1304 \f
1305 ;;;; *** Rmail Mailing Commands ***
1306
1307 (defun rmail-mail ()
1308 "Send mail in another window. While composing the message, use
1309 \\[mail-yank-original] to yank the original message into it."
1310 (interactive)
1311 (mail-other-window nil nil nil nil nil (current-buffer)))
1312
1313 (defun rmail-continue ()
1314 "Continue composing outgoing message previously being composed."
1315 (interactive)
1316 (mail-other-window t))
1317
1318 (defun rmail-reply (just-sender)
1319 "Reply to the current message.
1320 Normally include CC: to all other recipients of original message;
1321 prefix argument means ignore them. While composing the reply,
1322 use \\[mail-yank-original] to yank the original message into it."
1323 (interactive "P")
1324 (let (from reply-to cc subject date to message-id resent-reply-to)
1325 (save-excursion
1326 (save-restriction
1327 (widen)
1328 (goto-char (rmail-msgbeg rmail-current-message))
1329 (forward-line 1)
1330 (if (= (following-char) ?0)
1331 (narrow-to-region
1332 (progn (forward-line 2)
1333 (point))
1334 (progn (search-forward "\n\n" (rmail-msgend rmail-current-message)
1335 'move)
1336 (point)))
1337 (narrow-to-region (point)
1338 (progn (search-forward "\n*** EOOH ***\n")
1339 (beginning-of-line) (point))))
1340 (setq resent-reply-to (mail-fetch-field "resent-reply-to" t)
1341 from (mail-fetch-field "from")
1342 reply-to (or resent-reply-to
1343 (mail-fetch-field "reply-to" nil t)
1344 from)
1345 cc (cond (just-sender nil)
1346 (resent-reply-to (mail-fetch-field "resent-cc" t))
1347 (t (mail-fetch-field "cc" nil t)))
1348 subject (or (and resent-reply-to
1349 (mail-fetch-field "resent-subject" t))
1350 (mail-fetch-field "subject"))
1351 date (cond (resent-reply-to
1352 (mail-fetch-field "resent-date" t))
1353 ((mail-fetch-field "date")))
1354 to (cond (resent-reply-to
1355 (mail-fetch-field "resent-to" t))
1356 ((mail-fetch-field "to" nil t))
1357 ;((mail-fetch-field "apparently-to")) ack gag barf
1358 (t ""))
1359 message-id (cond (resent-reply-to
1360 (mail-fetch-field "resent-message-id" t))
1361 ((mail-fetch-field "message-id"))))))
1362 (and subject
1363 (string-match "\\`Re: " subject)
1364 (setq subject (substring subject 4)))
1365 (mail-other-window nil
1366 (mail-strip-quoted-names reply-to)
1367 subject
1368 (rmail-make-in-reply-to-field from date message-id)
1369 (if just-sender
1370 nil
1371 (let* ((cc-list (rmail-dont-reply-to
1372 (mail-strip-quoted-names
1373 (if (null cc) to (concat to ", " cc))))))
1374 (if (string= cc-list "") nil cc-list)))
1375 (current-buffer)
1376 (list (list '(lambda (buf msgnum)
1377 (save-excursion
1378 (set-buffer buf)
1379 (rmail-set-attribute "answered" t msgnum)))
1380 (current-buffer) rmail-current-message)))))
1381
1382 (defun rmail-make-in-reply-to-field (from date message-id)
1383 (cond ((not from)
1384 (if message-id
1385 message-id
1386 nil))
1387 (mail-use-rfc822
1388 (require 'rfc822)
1389 (let ((tem (car (rfc822-addresses from))))
1390 (if message-id
1391 (if (string-match
1392 (regexp-quote (if (string-match "@[^@]*\\'" tem)
1393 (substring tem 0 (match-beginning 0))
1394 tem))
1395 message-id)
1396 ;; Message-ID is sufficiently informative
1397 message-id
1398 (concat message-id " (" tem ")"))
1399 ;; Use prin1 to fake RFC822 quoting
1400 (let ((field (prin1-to-string tem)))
1401 (if date
1402 (concat field "'s message of " date)
1403 field)))))
1404 ((let* ((foo "[^][\000-\037\177-\377()<>@,;:\\\" ]+")
1405 (bar "[^][\000-\037\177-\377()<>@,;:\\\"]+"))
1406 ;; Can't use format because format loses on \000 (unix *^&%*^&%$!!)
1407 (or (string-match (concat "\\`[ \t]*\\(" bar
1408 "\\)\\(<" foo "@" foo ">\\)?[ \t]*\\'")
1409 ;; "Unix Loser <Foo@bar.edu>" => "Unix Loser"
1410 from)
1411 (string-match (concat "\\`[ \t]*<" foo "@" foo ">[ \t]*(\\("
1412 bar "\\))[ \t]*\\'")
1413 ;; "<Bugs@bar.edu>" (Losing Unix) => "Losing Unix"
1414 from)))
1415 (let ((start (match-beginning 1))
1416 (end (match-end 1)))
1417 ;; Trim whitespace which above regexp match allows
1418 (while (and (< start end)
1419 (memq (aref from start) '(?\t ?\ )))
1420 (setq start (1+ start)))
1421 (while (and (< start end)
1422 (memq (aref from (1- end)) '(?\t ?\ )))
1423 (setq end (1- end)))
1424 (let ((field (substring from start end)))
1425 (if date (setq field (concat "message from " field " on " date)))
1426 (if message-id
1427 ;; "<AA259@bar.edu> (message from Unix Loser on 1-Apr-89)"
1428 (concat message-id " (" field ")")
1429 field))))
1430 (t
1431 ;; If we can't kludge it simply, do it correctly
1432 (let ((mail-use-rfc822 t))
1433 (rmail-make-in-reply-to-field from date message-id)))))
1434
1435 (defun rmail-forward ()
1436 "Forward the current message to another user."
1437 (interactive)
1438 (let ((forward-buffer (current-buffer))
1439 (subject (concat "["
1440 (mail-strip-quoted-names (mail-fetch-field "From"))
1441 ": " (or (mail-fetch-field "Subject") "") "]")))
1442 ;; If only one window, use it for the mail buffer.
1443 ;; Otherwise, use another window for the mail buffer
1444 ;; so that the Rmail buffer remains visible
1445 ;; and sending the mail will get back to it.
1446 (if (funcall (if (one-window-p t)
1447 (function mail)
1448 (function mail-other-window))
1449 nil nil subject nil nil nil
1450 (list (list (function (lambda (buf msgnum)
1451 (save-excursion
1452 (set-buffer buf)
1453 (rmail-set-attribute "forwarded" t msgnum))))
1454 (current-buffer)
1455 rmail-current-message)))
1456 (save-excursion
1457 (goto-char (point-max))
1458 (forward-line 1)
1459 (insert-buffer forward-buffer)))))
1460
1461 (defun rmail-resend (address &optional from comment mail-alias-file)
1462 "Resend current message to ADDRESSES.
1463 ADDRESSES should be a single address, a a string consisting of several
1464 addresses separated by commas, or a list of addresses.
1465
1466 Optional FROM is the address to resend the message from, and
1467 defaults to the username of the person redistributing the message.
1468 Optional COMMENT is a string that will be inserted as a comment in the
1469 resent message.
1470 Optional ALIAS-FILE is alternate aliases file to be used by sendmail,
1471 typically for purposes of moderating a list."
1472 (interactive "sResend to: ")
1473 (if (not from) (setq from (user-login-name)))
1474 (let ((tembuf (generate-new-buffer " sendmail temp"))
1475 (mail-header-separator "")
1476 (case-fold-search nil)
1477 (mailbuf (current-buffer)))
1478 (unwind-protect
1479 (save-excursion
1480 ;;>> Copy message into temp buffer
1481 (set-buffer tembuf)
1482 (insert-buffer-substring mailbuf)
1483 (goto-char (point-min))
1484 ;;>> Insert resent-from:
1485 (insert "Resent-From: " from "\n")
1486 (insert "Resent-Date: " (current-time-string) "\n")
1487 ;;>> Insert resent-to: and bcc if need be.
1488 (let ((before (point)))
1489 (insert "Resent-To: " (if (stringp address)
1490 address
1491 (mapconcat 'identity address ",\n\t"))
1492 "\n")
1493 (expand-mail-aliases before (point)))
1494 ;;>> Set up comment, if any.
1495 (if (and (sequencep comment) (not (zerop (length comment))))
1496 (let ((before (point))
1497 after)
1498 (insert comment)
1499 (or (eolp) (insert "\n"))
1500 (setq after (point))
1501 (goto-char before)
1502 (while (< (point) after)
1503 (insert "Resent-Comment: ")
1504 (forward-line 1))))
1505 ;; Don't expand aliases in the destination fields
1506 ;; of the original message.
1507 (let (mail-aliases)
1508 (sendmail-send-it)))
1509 (kill-buffer tembuf))))
1510
1511 (defvar mail-unsent-separator "^ ----- Unsent message follows -----$")
1512
1513 (defun rmail-retry-failure ()
1514 "Edit a mail message which is based on the contents of the current message.
1515 For a message rejected by the mail system, extract the interesting headers and
1516 the body of the original message; otherwise copy the current message."
1517 (interactive)
1518 (require 'mail-utils)
1519 (let (to subj irp2 cc orig-message)
1520 (save-excursion
1521 ;; Narrow down to just the quoted original message
1522 (rmail-beginning-of-message)
1523 (or (re-search-forward mail-unsent-separator nil t)
1524 (error "Cannot parse this as a failure message"))
1525 (save-restriction
1526 (narrow-to-region (point) (point-max))
1527 ;; Now mail-fetch-field will get from headers of the original message,
1528 ;; not from the headers of the rejection.
1529 (setq to (mail-fetch-field "To")
1530 subj (mail-fetch-field "Subject")
1531 irp2 (mail-fetch-field "In-reply-to")
1532 cc (mail-fetch-field "Cc"))
1533 ;; Get the entire text (not headers) of the original message.
1534 (setq orig-message
1535 (buffer-substring
1536 (progn (search-forward "\n\n") (point))
1537 (point-max)))))
1538 ;; Start sending a new message; default header fields from the original.
1539 (if (mail-other-window nil to subj irp2 cc (current-buffer))
1540 ;; Insert original text as initial text of new draft message.
1541 (progn
1542 (goto-char (point-max))
1543 (insert orig-message)
1544 (goto-char (point-min))
1545 (end-of-line)))))
1546 \f
1547 ;;;; *** Rmail Specify Inbox Files ***
1548
1549 (autoload 'set-rmail-inbox-list "rmailmsc"
1550 "Set the inbox list of the current RMAIL file to FILE-NAME.
1551 This may be a list of file names separated by commas.
1552 If FILE-NAME is empty, remove any inbox list."
1553 t)
1554
1555 ;;;; *** Rmail Commands for Labels ***
1556
1557 (autoload 'rmail-add-label "rmailkwd"
1558 "Add LABEL to labels associated with current RMAIL message.
1559 Completion is performed over known labels when reading."
1560 t)
1561
1562 (autoload 'rmail-kill-label "rmailkwd"
1563 "Remove LABEL from labels associated with current RMAIL message.
1564 Completion is performed over known labels when reading."
1565 t)
1566
1567 (autoload 'rmail-next-labeled-message "rmailkwd"
1568 "Show next message with LABEL. Defaults to last label used.
1569 With prefix argument N moves forward N messages with this label."
1570 t)
1571
1572 (autoload 'rmail-previous-labeled-message "rmailkwd"
1573 "Show previous message with LABEL. Defaults to last label used.
1574 With prefix argument N moves backward N messages with this label."
1575 t)
1576
1577 ;;;; *** Rmail Edit Mode ***
1578
1579 (autoload 'rmail-edit-current-message "rmailedit"
1580 "Edit the contents of the current message"
1581 t)
1582 \f
1583 ;;;; *** Rmail Summary Mode ***
1584
1585 (autoload 'rmail-summary "rmailsum"
1586 "Display a summary of all messages, one line per message."
1587 t)
1588
1589 (autoload 'rmail-summary-by-labels "rmailsum"
1590 "Display a summary of all messages with one or more LABELS.
1591 LABELS should be a string containing the desired labels, separated by commas."
1592 t)
1593
1594 (autoload 'rmail-summary-by-recipients "rmailsum"
1595 "Display a summary of all messages with the given RECIPIENTS.
1596 Normally checks the To, From and Cc fields of headers; but if PRIMARY-ONLY
1597 is non-nil (prefix arg given), only look in the To and From fields.
1598 RECIPIENTS is a string of names separated by commas."
1599 t)
1600 \f
1601 ;;;; *** Rmail output messages to files ***
1602
1603 (autoload 'rmail-output-to-rmail-file "rmailout"
1604 "Append the current message to an Rmail file named FILE-NAME.
1605 If the file does not exist, ask if it should be created.
1606 If file is being visited, the message is appended to the Emacs
1607 buffer visiting that file."
1608 t)
1609
1610 (autoload 'rmail-output "rmailout"
1611 "Append this message to Unix mail file named FILE-NAME."
1612 t)
1613
1614 ;;;; *** Rmail undigestification ***
1615
1616 (autoload 'undigestify-rmail-message "undigest"
1617 "Break up a digest message into its constituent messages.
1618 Leaves original message, deleted, before the undigestified messages."
1619 t)
1620
1621 (provide 'rmail)
1622
1623 ;;; rmail.el ends here