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