]> code.delx.au - gnu-emacs/blob - lisp/mail/rmail.el
(rmail-make-basic-summary-line): Accept ISO 8601 dates as well.
[gnu-emacs] / lisp / mail / rmail.el
1 ;;; rmail.el --- main code of "RMAIL" mail reader for Emacs.
2
3 ;; Copyright (C) 1985,86,87,88,93,94,95,96 Free Software Foundation, Inc.
4
5 ;; Maintainer: FSF
6 ;; Keywords: mail
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, 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 ;; Extended by Bob Weiner of Motorola
33 ;; New features include: rmail and rmail-summary buffers remain
34 ;; synchronized and key bindings basically operate the same way in both
35 ;; buffers, summary by topic or by regular expression, rmail-reply-prefix
36 ;; variable, and a bury rmail buffer (wipe) command.
37 ;;
38
39 (require 'mail-utils)
40
41 ;; For Emacs V18 compatibility
42 (and (not (fboundp 'buffer-disable-undo))
43 (fboundp 'buffer-flush-undo)
44 (defalias 'buffer-disable-undo 'buffer-flush-undo))
45
46 ; These variables now declared in paths.el.
47 ;(defvar rmail-spool-directory "/usr/spool/mail/"
48 ; "This is the name of the directory used by the system mailer for\n\
49 ;delivering new mail. It's name should end with a slash.")
50 ;(defvar rmail-file-name
51 ; (expand-file-name "~/RMAIL")
52 ; "")
53
54 (defvar rmail-movemail-program nil
55 "If non-nil, name of program for fetching new mail.")
56
57 (defvar rmail-pop-password nil
58 "*Password to use when reading mail from a POP server, if required.")
59
60 (defvar rmail-pop-password-required nil
61 "*Non-nil if a password is required when reading mail using POP.")
62
63 ;;;###autoload
64 (defvar rmail-dont-reply-to-names nil "\
65 *A regexp specifying names to prune of reply to messages.
66 A value of nil means exclude your own name only.")
67
68 ;;;###autoload
69 (defvar rmail-default-dont-reply-to-names "info-" "\
70 A regular expression specifying part of the value of the default value of
71 the variable `rmail-dont-reply-to-names', for when the user does not set
72 `rmail-dont-reply-to-names' explicitly. (The other part of the default
73 value is the user's name.)
74 It is useful to set this variable in the site customization file.")
75
76 ;;;###autoload
77 (defvar rmail-ignored-headers "^via:\\|^mail-from:\\|^origin:\\|^status:\\|^received:\\|^x400-originator:\\|^x400-recipients:\\|^x400-received:\\|^x400-mts-identifier:\\|^x400-content-type:\\|^\\(resent-\\|\\)message-id:\\|^summary-line:\\|^resent-date:\\|^nntp-posting-host:"
78 "*Regexp to match Header fields that Rmail should normally hide.")
79
80 ;;;###autoload
81 (defvar rmail-displayed-headers nil
82 "*Regexp to match Header fields that Rmail should display.
83 If nil, display all header fields except those matched by
84 `rmail-ignored-headers'.")
85
86 ;;;###autoload
87 (defvar rmail-retry-ignored-headers nil "\
88 *Headers that should be stripped when retrying a failed message.")
89
90 ;;;###autoload
91 (defvar rmail-highlighted-headers "^From:\\|^Subject:" "\
92 *Regexp to match Header fields that Rmail should normally highlight.
93 A value of nil means don't highlight.
94 See also `rmail-highlight-face'.")
95
96 ;;;###autoload
97 (defvar rmail-highlight-face nil "\
98 *Face used by Rmail for highlighting headers.")
99
100 ;;;###autoload
101 (defvar rmail-delete-after-output nil "\
102 *Non-nil means automatically delete a message that is copied to a file.")
103
104 ;;;###autoload
105 (defvar rmail-primary-inbox-list nil "\
106 *List of files which are inboxes for user's primary mail file `~/RMAIL'.
107 `nil' means the default, which is (\"/usr/spool/mail/$USER\")
108 \(the name varies depending on the operating system,
109 and the value of the environment variable MAIL overrides it).")
110
111 ;;;###autoload
112 (defvar rmail-mail-new-frame nil
113 "*Non-nil means Rmail makes a new frame for composing outgoing mail.")
114
115 ;;;###autoload
116 (defvar rmail-secondary-file-directory "~/"
117 "*Directory for additional secondary Rmail files.")
118 ;;;###autoload
119 (defvar rmail-secondary-file-regexp "\\.xmail$"
120 "*Regexp for which files are secondary Rmail files.")
121
122 ;;;###autoload
123 (defvar rmail-mode-hook nil
124 "List of functions to call when Rmail is invoked.")
125
126 ;;;###autoload
127 (defvar rmail-get-new-mail-hook nil
128 "List of functions to call when Rmail has retrieved new mail.")
129
130 ;;;###autoload
131 (defvar rmail-show-message-hook nil
132 "List of functions to call when Rmail displays a message.")
133
134 ;;;###autoload
135 (defvar rmail-delete-message-hook nil
136 "List of functions to call when Rmail deletes a message.
137 When the hooks are called, the message has been marked deleted but is
138 still the current message in the Rmail buffer.")
139
140 ;; These may be altered by site-init.el to match the format of mmdf files
141 ;; delimiting used on a given host (delim1 and delim2 from the config
142 ;; files).
143
144 (defvar mmdf-delim1 "^\001\001\001\001\n"
145 "Regexp marking the start of an mmdf message")
146 (defvar mmdf-delim2 "^\001\001\001\001\n"
147 "Regexp marking the end of an mmdf message")
148
149 (defvar rmail-message-filter nil
150 "If non nil, a filter function for new messages in RMAIL.
151 Called with region narrowed to the message, including headers.")
152
153 (defvar rmail-reply-prefix "Re: "
154 "String to prepend to Subject line when replying to a message.")
155
156 ;; Some mailers use "Re(2):" or "Re^2:" or "Re: Re:".
157 ;; This pattern should catch all the common variants.
158 (defvar rmail-reply-regexp "\\`\\(Re\\(([0-9]+)\\|\\^[0-9]+\\)?: *\\)*"
159 "Regexp to delete from Subject line before inserting rmail-reply-prefix.")
160
161 (defvar rmail-display-summary nil
162 "If non nil, the summary buffer is always displayed.")
163
164 (defvar rmail-mode-map nil)
165
166 (defvar rmail-inbox-list nil)
167 (defvar rmail-keywords nil)
168
169 ;; Message counters and markers. Deleted flags.
170
171 (defvar rmail-current-message nil)
172 (defvar rmail-total-messages nil)
173 (defvar rmail-message-vector nil)
174 (defvar rmail-deleted-vector nil)
175
176 (defvar rmail-overlay-list nil)
177
178 (defvar rmail-font-lock-keywords
179 (eval-when-compile
180 (let* ((cite-prefix "A-Za-z") (cite-suffix (concat cite-prefix "0-9_.@-")))
181 (list
182 '("^\\(From\\|Sender\\):" . font-lock-function-name-face)
183 '("^Reply-To:.*$" . font-lock-function-name-face)
184 '("^Subject:" . font-lock-comment-face)
185 '("^\\(To\\|Apparently-To\\|Cc\\):" . font-lock-keyword-face)
186 (cons (concat "^[ \t]*"
187 "\\([" cite-prefix "]+[" cite-suffix "]*\\)?"
188 "[>|}].*")
189 'font-lock-reference-face)
190 '("^\\(X-[A-Za-z0-9-]+\\|In-reply-to\\|Date\\):.*$"
191 . font-lock-string-face))))
192 "Additional expressions to highlight in Rmail mode.")
193
194 ;; These are used by autoloaded rmail-summary.
195
196 (defvar rmail-summary-buffer nil)
197 (defvar rmail-summary-vector nil)
198
199 ;; `Sticky' default variables.
200
201 ;; Last individual label specified to a or k.
202 (defvar rmail-last-label nil)
203 ;; Last set of values specified to C-M-n, C-M-p, C-M-s or C-M-l.
204 (defvar rmail-last-multi-labels nil)
205 (defvar rmail-last-regexp nil)
206 (defvar rmail-default-file "~/xmail"
207 "*Default file name for \\[rmail-output].")
208 (defvar rmail-default-rmail-file "~/XMAIL"
209 "*Default file name for \\[rmail-output-to-rmail-file].")
210
211 ;;; Regexp matching the delimiter of messages in UNIX mail format
212 ;;; (UNIX From lines), minus the initial ^. Note that if you change
213 ;;; this expression, you must change the code in rmail-nuke-pinhead-header
214 ;;; that knows the exact ordering of the \\( \\) subexpressions.
215 (defvar rmail-unix-mail-delimiter
216 (let ((time-zone-regexp
217 (concat "\\([A-Z]?[A-Z]?[A-Z][A-Z]\\( DST\\)?"
218 "\\|[-+]?[0-9][0-9][0-9][0-9]"
219 "\\|"
220 "\\) *")))
221 (concat
222 "From "
223
224 ;; Many things can happen to an RFC 822 mailbox before it is put into
225 ;; a `From' line. The leading phrase can be stripped, e.g.
226 ;; `Joe <@w.x:joe@y.z>' -> `<@w.x:joe@y.z>'. The <> can be stripped, e.g.
227 ;; `<@x.y:joe@y.z>' -> `@x.y:joe@y.z'. Everything starting with a CRLF
228 ;; can be removed, e.g.
229 ;; From: joe@y.z (Joe K
230 ;; User)
231 ;; can yield `From joe@y.z (Joe K Fri Mar 22 08:11:15 1996', and
232 ;; From: Joe User
233 ;; <joe@y.z>
234 ;; can yield `From Joe User Fri Mar 22 08:11:15 1996'.
235 ;; We want to match the results of any of these manglings.
236 ;; The following regexp rejects names whose first characters are
237 ;; obviously bogus, but after that anything goes.
238 "\\([^\0-\r \^?].*\\)? "
239
240 ;; The time the message was sent.
241 "\\([^\0-\r \^?]+\\) +" ; day of the week
242 "\\([^\0-\r \^?]+\\) +" ; month
243 "\\([0-3]?[0-9]\\) +" ; day of month
244 "\\([0-2][0-9]:[0-5][0-9]\\(:[0-6][0-9]\\)?\\) *" ; time of day
245
246 ;; Perhaps a time zone, specified by an abbreviation, or by a
247 ;; numeric offset.
248 time-zone-regexp
249
250 ;; The year.
251 " \\([0-9][0-9]+\\) *"
252
253 ;; On some systems the time zone can appear after the year, too.
254 time-zone-regexp
255
256 ;; Old uucp cruft.
257 "\\(remote from .*\\)?"
258
259 "\n"))
260 nil)
261
262 ;; Perform BODY in the summary buffer
263 ;; in such a way that its cursor is properly updated in its own window.
264 (defmacro rmail-select-summary (&rest body)
265 (` (let ((total rmail-total-messages))
266 (if (rmail-summary-displayed)
267 (let ((window (selected-window)))
268 (save-excursion
269 (unwind-protect
270 (progn
271 (pop-to-buffer rmail-summary-buffer)
272 ;; rmail-total-messages is a buffer-local var
273 ;; in the rmail buffer.
274 ;; This way we make it available for the body
275 ;; even tho the rmail buffer is not current.
276 (let ((rmail-total-messages total))
277 (,@ body)))
278 (select-window window))))
279 (save-excursion
280 (set-buffer rmail-summary-buffer)
281 (let ((rmail-total-messages total))
282 (,@ body))))
283 (rmail-maybe-display-summary))))
284 \f
285 ;;;; *** Rmail Mode ***
286
287 ;;;###autoload
288 (defun rmail (&optional file-name-arg)
289 "Read and edit incoming mail.
290 Moves messages into file named by `rmail-file-name' (a babyl format file)
291 and edits that file in RMAIL Mode.
292 Type \\[describe-mode] once editing that file, for a list of RMAIL commands.
293
294 May be called with file name as argument; then performs rmail editing on
295 that file, but does not copy any new mail into the file.
296 Interactively, if you supply a prefix argument, then you
297 have a chance to specify a file name with the minibuffer.
298
299 If `rmail-display-summary' is non-nil, make a summary for this RMAIL file."
300 (interactive (if current-prefix-arg
301 (list (read-file-name "Run rmail on RMAIL file: "))))
302 (let* ((file-name (expand-file-name (or file-name-arg rmail-file-name)))
303 (existed (get-file-buffer file-name)))
304 ;; Like find-file, but in the case where a buffer existed
305 ;; and the file was reverted, recompute the message-data.
306 (if (and existed (not (verify-visited-file-modtime existed)))
307 (progn
308 ;; Don't be confused by apparent local-variables spec
309 ;; in the last message in the RMAIL file.
310 (let ((enable-local-variables nil))
311 (find-file file-name))
312 (if (and (verify-visited-file-modtime existed)
313 (eq major-mode 'rmail-mode))
314 (progn (rmail-forget-messages)
315 (rmail-set-message-counters))))
316 (let ((enable-local-variables nil))
317 (find-file file-name)))
318 (if (eq major-mode 'rmail-edit-mode)
319 (error "Exit Rmail Edit mode before getting new mail."))
320 (if (and existed (> (buffer-size) 0))
321 ;; Buffer not new and not empty; ensure in proper mode, but that's all.
322 (or (eq major-mode 'rmail-mode)
323 (rmail-mode-2))
324 (rmail-mode-2)
325 ;; Convert all or part to Babyl file if possible.
326 (rmail-convert-file)
327 (goto-char (point-max))
328 (if (null rmail-inbox-list)
329 (progn
330 (rmail-set-message-counters)
331 (rmail-show-message))))
332 (or (and (null file-name-arg)
333 (rmail-get-new-mail))
334 (rmail-show-message (rmail-first-unseen-message)))
335 (if rmail-display-summary (rmail-summary))
336 (rmail-construct-io-menu)))
337
338 ;; Given the value of MAILPATH, return a list of inbox file names.
339 ;; This is turned off because it is not clear that the user wants
340 ;; all these inboxes to feed into the primary rmail file.
341 ; (defun rmail-convert-mailpath (string)
342 ; (let (idx list)
343 ; (while (setq idx (string-match "[%:]" string))
344 ; (let ((this (substring string 0 idx)))
345 ; (setq string (substring string (1+ idx)))
346 ; (setq list (cons (if (string-match "%" this)
347 ; (substring this 0 (string-match "%" this))
348 ; this)
349 ; list))))
350 ; list))
351
352 ; I have checked that adding "-*- rmail -*-" to the BABYL OPTIONS line
353 ; will not cause emacs 18.55 problems.
354
355 (defun rmail-convert-file ()
356 (let (convert)
357 (widen)
358 (goto-char (point-min))
359 ;; If file doesn't start like a Babyl file,
360 ;; convert it to one, by adding a header and converting each message.
361 (cond ((looking-at "BABYL OPTIONS:"))
362 ((looking-at "Version: 5\n")
363 ;; Losing babyl file made by old version of Rmail.
364 ;; Just fix the babyl file header; don't make a new one,
365 ;; so we don't lose the Labels: file attribute, etc.
366 (let ((buffer-read-only nil))
367 (insert "BABYL OPTIONS: -*- rmail -*-\n")))
368 ((equal (point-min) (point-max))
369 ;; Empty RMAIL file. Just insert the header.
370 (rmail-insert-rmail-file-header))
371 (t
372 ;; Non-empty file in non-RMAIL format. Add header and convert.
373 (setq convert t)
374 (rmail-insert-rmail-file-header)))
375 ;; If file was not a Babyl file or if there are
376 ;; Unix format messages added at the end,
377 ;; convert file as necessary.
378 (if (or convert
379 (save-excursion
380 (goto-char (point-max))
381 (search-backward "\n\^_")
382 (forward-char 2)
383 (looking-at "\n*From ")))
384 (let ((buffer-read-only nil))
385 (message "Converting to Babyl format...")
386 ;; If file needs conversion, convert it all,
387 ;; except for the BABYL header.
388 ;; (rmail-convert-to-babyl-format would delete the header.)
389 (goto-char (point-min))
390 (search-forward "\n\^_" nil t)
391 (narrow-to-region (point) (point-max))
392 (rmail-convert-to-babyl-format)
393 (message "Converting to Babyl format...done")))))
394
395 ;;; I have checked that adding "-*- rmail -*-" to the BABYL OPTIONS line
396 ;;; will not cause emacs 18.55 problems.
397
398 (defun rmail-insert-rmail-file-header ()
399 (let ((buffer-read-only nil))
400 (insert "BABYL OPTIONS: -*- rmail -*-
401 Version: 5
402 Labels:
403 Note: This is the header of an rmail file.
404 Note: If you are seeing it in rmail,
405 Note: it means the file has no messages in it.\n\^_")))
406
407 (if rmail-mode-map
408 nil
409 (setq rmail-mode-map (make-keymap))
410 (suppress-keymap rmail-mode-map)
411 (define-key rmail-mode-map "a" 'rmail-add-label)
412 (define-key rmail-mode-map "b" 'rmail-bury)
413 (define-key rmail-mode-map "c" 'rmail-continue)
414 (define-key rmail-mode-map "d" 'rmail-delete-forward)
415 (define-key rmail-mode-map "\C-d" 'rmail-delete-backward)
416 (define-key rmail-mode-map "e" 'rmail-edit-current-message)
417 (define-key rmail-mode-map "f" 'rmail-forward)
418 (define-key rmail-mode-map "g" 'rmail-get-new-mail)
419 (define-key rmail-mode-map "h" 'rmail-summary)
420 (define-key rmail-mode-map "i" 'rmail-input)
421 (define-key rmail-mode-map "j" 'rmail-show-message)
422 (define-key rmail-mode-map "k" 'rmail-kill-label)
423 (define-key rmail-mode-map "l" 'rmail-summary-by-labels)
424 (define-key rmail-mode-map "\e\C-h" 'rmail-summary)
425 (define-key rmail-mode-map "\e\C-l" 'rmail-summary-by-labels)
426 (define-key rmail-mode-map "\e\C-r" 'rmail-summary-by-recipients)
427 (define-key rmail-mode-map "\e\C-s" 'rmail-summary-by-regexp)
428 (define-key rmail-mode-map "\e\C-t" 'rmail-summary-by-topic)
429 (define-key rmail-mode-map "m" 'rmail-mail)
430 (define-key rmail-mode-map "\em" 'rmail-retry-failure)
431 (define-key rmail-mode-map "n" 'rmail-next-undeleted-message)
432 (define-key rmail-mode-map "\en" 'rmail-next-message)
433 (define-key rmail-mode-map "\e\C-n" 'rmail-next-labeled-message)
434 (define-key rmail-mode-map "o" 'rmail-output-to-rmail-file)
435 (define-key rmail-mode-map "\C-o" 'rmail-output)
436 (define-key rmail-mode-map "p" 'rmail-previous-undeleted-message)
437 (define-key rmail-mode-map "\ep" 'rmail-previous-message)
438 (define-key rmail-mode-map "\e\C-p" 'rmail-previous-labeled-message)
439 (define-key rmail-mode-map "q" 'rmail-quit)
440 (define-key rmail-mode-map "r" 'rmail-reply)
441 ;; I find I can't live without the default M-r command -- rms.
442 ;; (define-key rmail-mode-map "\er" 'rmail-search-backwards)
443 (define-key rmail-mode-map "s" 'rmail-expunge-and-save)
444 (define-key rmail-mode-map "\es" 'rmail-search)
445 (define-key rmail-mode-map "t" 'rmail-toggle-header)
446 (define-key rmail-mode-map "u" 'rmail-undelete-previous-message)
447 (define-key rmail-mode-map "w" 'rmail-edit-current-message)
448 (define-key rmail-mode-map "x" 'rmail-expunge)
449 (define-key rmail-mode-map "." 'rmail-beginning-of-message)
450 (define-key rmail-mode-map "<" 'rmail-first-message)
451 (define-key rmail-mode-map ">" 'rmail-last-message)
452 (define-key rmail-mode-map " " 'scroll-up)
453 (define-key rmail-mode-map "\177" 'scroll-down)
454 (define-key rmail-mode-map "?" 'describe-mode)
455 (define-key rmail-mode-map "\C-c\C-s\C-d" 'rmail-sort-by-date)
456 (define-key rmail-mode-map "\C-c\C-s\C-s" 'rmail-sort-by-subject)
457 (define-key rmail-mode-map "\C-c\C-s\C-a" 'rmail-sort-by-author)
458 (define-key rmail-mode-map "\C-c\C-s\C-r" 'rmail-sort-by-recipient)
459 (define-key rmail-mode-map "\C-c\C-s\C-c" 'rmail-sort-by-correspondent)
460 (define-key rmail-mode-map "\C-c\C-s\C-l" 'rmail-sort-by-lines)
461 (define-key rmail-mode-map "\C-c\C-s\C-k" 'rmail-sort-by-keywords)
462 (define-key rmail-mode-map "\C-c\C-n" 'rmail-next-same-subject)
463 (define-key rmail-mode-map "\C-c\C-p" 'rmail-previous-same-subject)
464 )
465 \f
466 (define-key rmail-mode-map [menu-bar] (make-sparse-keymap))
467
468 (define-key rmail-mode-map [menu-bar classify]
469 (cons "Classify" (make-sparse-keymap "Classify")))
470
471 (define-key rmail-mode-map [menu-bar classify input-menu]
472 nil)
473
474 (define-key rmail-mode-map [menu-bar classify output-menu]
475 nil)
476
477 (define-key rmail-mode-map [menu-bar classify output-inbox]
478 '("Output (inbox)..." . rmail-output))
479
480 (define-key rmail-mode-map [menu-bar classify output]
481 '("Output (Rmail)..." . rmail-output-to-rmail-file))
482
483 (define-key rmail-mode-map [menu-bar classify kill-label]
484 '("Kill Label..." . rmail-kill-label))
485
486 (define-key rmail-mode-map [menu-bar classify add-label]
487 '("Add Label..." . rmail-add-label))
488
489 (define-key rmail-mode-map [menu-bar summary]
490 (cons "Summary" (make-sparse-keymap "Summary")))
491
492 (define-key rmail-mode-map [menu-bar summary senders]
493 '("By Senders..." . rmail-summary-by-senders))
494
495 (define-key rmail-mode-map [menu-bar summary labels]
496 '("By Labels..." . rmail-summary-by-labels))
497
498 (define-key rmail-mode-map [menu-bar summary recipients]
499 '("By Recipients..." . rmail-summary-by-recipients))
500
501 (define-key rmail-mode-map [menu-bar summary topic]
502 '("By Topic..." . rmail-summary-by-topic))
503
504 (define-key rmail-mode-map [menu-bar summary regexp]
505 '("By Regexp..." . rmail-summary-by-regexp))
506
507 (define-key rmail-mode-map [menu-bar summary all]
508 '("All" . rmail-summary))
509
510 (define-key rmail-mode-map [menu-bar mail]
511 (cons "Mail" (make-sparse-keymap "Mail")))
512
513 (define-key rmail-mode-map [menu-bar mail rmail-get-new-mail]
514 '("Get New Mail" . rmail-get-new-mail))
515
516 (define-key rmail-mode-map [menu-bar mail lambda]
517 '("----"))
518
519 (define-key rmail-mode-map [menu-bar mail continue]
520 '("Continue" . rmail-continue))
521
522 (define-key rmail-mode-map [menu-bar mail resend]
523 '("Re-send..." . rmail-resend))
524
525 (define-key rmail-mode-map [menu-bar mail forward]
526 '("Forward" . rmail-forward))
527
528 (define-key rmail-mode-map [menu-bar mail retry]
529 '("Retry" . rmail-retry-failure))
530
531 (define-key rmail-mode-map [menu-bar mail reply]
532 '("Reply" . rmail-reply))
533
534 (define-key rmail-mode-map [menu-bar mail mail]
535 '("Mail" . rmail-mail))
536
537 (define-key rmail-mode-map [menu-bar delete]
538 (cons "Delete" (make-sparse-keymap "Delete")))
539
540 (define-key rmail-mode-map [menu-bar delete expunge/save]
541 '("Expunge/Save" . rmail-expunge-and-save))
542
543 (define-key rmail-mode-map [menu-bar delete expunge]
544 '("Expunge" . rmail-expunge))
545
546 (define-key rmail-mode-map [menu-bar delete undelete]
547 '("Undelete" . rmail-undelete-previous-message))
548
549 (define-key rmail-mode-map [menu-bar delete delete]
550 '("Delete" . rmail-delete-forward))
551
552 (define-key rmail-mode-map [menu-bar move]
553 (cons "Move" (make-sparse-keymap "Move")))
554
555 (define-key rmail-mode-map [menu-bar move search-back]
556 '("Search Back..." . rmail-search-backwards))
557
558 (define-key rmail-mode-map [menu-bar move search]
559 '("Search..." . rmail-search))
560
561 (define-key rmail-mode-map [menu-bar move previous]
562 '("Previous Nondeleted" . rmail-previous-undeleted-message))
563
564 (define-key rmail-mode-map [menu-bar move next]
565 '("Next Nondeleted" . rmail-next-undeleted-message))
566
567 (define-key rmail-mode-map [menu-bar move last]
568 '("Last" . rmail-last-message))
569
570 (define-key rmail-mode-map [menu-bar move first]
571 '("First" . rmail-first-message))
572
573 (define-key rmail-mode-map [menu-bar move previous]
574 '("Previous" . rmail-previous-message))
575
576 (define-key rmail-mode-map [menu-bar move next]
577 '("Next" . rmail-next-message))
578 \f
579 ;; Rmail mode is suitable only for specially formatted data.
580 (put 'rmail-mode 'mode-class 'special)
581
582 (defun rmail-mode-kill-summary ()
583 (if rmail-summary-buffer (kill-buffer rmail-summary-buffer)))
584
585 ;;;###autoload
586 (defun rmail-mode ()
587 "Rmail Mode is used by \\<rmail-mode-map>\\[rmail] for editing Rmail files.
588 All normal editing commands are turned off.
589 Instead, these commands are available:
590
591 \\[rmail-beginning-of-message] Move point to front of this message (same as \\[beginning-of-buffer]).
592 \\[scroll-up] Scroll to next screen of this message.
593 \\[scroll-down] Scroll to previous screen of this message.
594 \\[rmail-next-undeleted-message] Move to Next non-deleted message.
595 \\[rmail-previous-undeleted-message] Move to Previous non-deleted message.
596 \\[rmail-next-message] Move to Next message whether deleted or not.
597 \\[rmail-previous-message] Move to Previous message whether deleted or not.
598 \\[rmail-first-message] Move to the first message in Rmail file.
599 \\[rmail-last-message] Move to the last message in Rmail file.
600 \\[rmail-show-message] Jump to message specified by numeric position in file.
601 \\[rmail-search] Search for string and show message it is found in.
602 \\[rmail-delete-forward] Delete this message, move to next nondeleted.
603 \\[rmail-delete-backward] Delete this message, move to previous nondeleted.
604 \\[rmail-undelete-previous-message] Undelete message. Tries current message, then earlier messages
605 till a deleted message is found.
606 \\[rmail-edit-current-message] Edit the current message. \\[rmail-cease-edit] to return to Rmail.
607 \\[rmail-expunge] Expunge deleted messages.
608 \\[rmail-expunge-and-save] Expunge and save the file.
609 \\[rmail-quit] Quit Rmail: expunge, save, then switch to another buffer.
610 \\[save-buffer] Save without expunging.
611 \\[rmail-get-new-mail] Move new mail from system spool directory into this file.
612 \\[rmail-mail] Mail a message (same as \\[mail-other-window]).
613 \\[rmail-continue] Continue composing outgoing message started before.
614 \\[rmail-reply] Reply to this message. Like \\[rmail-mail] but initializes some fields.
615 \\[rmail-retry-failure] Send this message again. Used on a mailer failure message.
616 \\[rmail-forward] Forward this message to another user.
617 \\[rmail-output-to-rmail-file] Output this message to an Rmail file (append it).
618 \\[rmail-output] Output this message to a Unix-format mail file (append it).
619 \\[rmail-input] Input Rmail file. Run Rmail on that file.
620 \\[rmail-add-label] Add label to message. It will be displayed in the mode line.
621 \\[rmail-kill-label] Kill label. Remove a label from current message.
622 \\[rmail-next-labeled-message] Move to Next message with specified label
623 (label defaults to last one specified).
624 Standard labels: filed, unseen, answered, forwarded, deleted.
625 Any other label is present only if you add it with \\[rmail-add-label].
626 \\[rmail-previous-labeled-message] Move to Previous message with specified label
627 \\[rmail-summary] Show headers buffer, with a one line summary of each message.
628 \\[rmail-summary-by-labels] Summarize only messages with particular label(s).
629 \\[rmail-summary-by-recipients] Summarize only messages with particular recipient(s).
630 \\[rmail-summary-by-regexp] Summarize only messages with particular regexp(s).
631 \\[rmail-summary-by-topic] Summarize only messages with subject line regexp(s).
632 \\[rmail-toggle-header] Toggle display of complete header."
633 (interactive)
634 (rmail-mode-2)
635 (rmail-set-message-counters)
636 (rmail-show-message rmail-total-messages))
637
638 (defun rmail-mode-2 ()
639 (kill-all-local-variables)
640 (rmail-mode-1)
641 (rmail-variables)
642 (run-hooks 'rmail-mode-hook))
643
644 (defun rmail-mode-1 ()
645 (setq major-mode 'rmail-mode)
646 (setq mode-name "RMAIL")
647 (setq buffer-read-only t)
648 ;; No need to auto save RMAIL files in normal circumstances
649 ;; because they contain no info except attribute changes
650 ;; and deletion of messages.
651 ;; The one exception is when messages are copied into an Rmail mode buffer.
652 ;; rmail-output-to-rmail-file enables auto save when you do that.
653 (setq buffer-auto-save-file-name nil)
654 (if (boundp 'mode-line-modified)
655 (setq mode-line-modified "--- ")
656 (setq mode-line-format
657 (cons "--- " (cdr (default-value 'mode-line-format)))))
658 (use-local-map rmail-mode-map)
659 (set-syntax-table text-mode-syntax-table)
660 (setq local-abbrev-table text-mode-abbrev-table))
661
662 (defun rmail-variables ()
663 (make-local-variable 'revert-buffer-function)
664 (setq revert-buffer-function 'rmail-revert)
665 (make-local-variable 'font-lock-defaults)
666 (setq font-lock-defaults
667 '(rmail-font-lock-keywords t nil nil nil
668 (font-lock-maximum-size . nil)
669 (font-lock-fontify-buffer-function . rmail-fontify-buffer-function)
670 (font-lock-unfontify-buffer-function . rmail-unfontify-buffer-function)
671 (font-lock-inhibit-thing-lock . (lazy-lock-mode fast-lock-mode))))
672 (make-local-variable 'rmail-last-label)
673 (make-local-variable 'rmail-last-regexp)
674 (make-local-variable 'rmail-deleted-vector)
675 (make-local-variable 'rmail-summary-buffer)
676 (make-local-variable 'rmail-summary-vector)
677 (make-local-variable 'rmail-current-message)
678 (make-local-variable 'rmail-total-messages)
679 (make-local-variable 'require-final-newline)
680 (setq require-final-newline nil)
681 (make-local-variable 'rmail-overlay-list)
682 (setq rmail-overlay-list nil)
683 (make-local-variable 'version-control)
684 (setq version-control 'never)
685 (make-local-variable 'kill-buffer-hook)
686 (add-hook 'kill-buffer-hook 'rmail-mode-kill-summary)
687 (make-local-variable 'file-precious-flag)
688 (setq file-precious-flag t)
689 (make-local-variable 'rmail-message-vector)
690 (make-local-variable 'rmail-inbox-list)
691 (setq rmail-inbox-list (rmail-parse-file-inboxes))
692 ;; Provide default set of inboxes for primary mail file ~/RMAIL.
693 (and (null rmail-inbox-list)
694 (or (equal buffer-file-name (expand-file-name rmail-file-name))
695 (equal buffer-file-truename
696 (abbreviate-file-name (file-truename rmail-file-name))))
697 (setq rmail-inbox-list
698 (or rmail-primary-inbox-list
699 (list (or (getenv "MAIL")
700 (concat rmail-spool-directory
701 (user-login-name)))))))
702 (make-local-variable 'rmail-keywords)
703 ;; this gets generated as needed
704 (setq rmail-keywords nil))
705
706 ;; Handle M-x revert-buffer done in an rmail-mode buffer.
707 (defun rmail-revert (arg noconfirm)
708 (let ((revert-buffer-function (default-value 'revert-buffer-function)))
709 ;; Call our caller again, but this time it does the default thing.
710 (if (revert-buffer arg noconfirm)
711 ;; If the user said "yes", and we changed something,
712 ;; reparse the messages.
713 (progn
714 (rmail-convert-file)
715 (goto-char (point-max))
716 (rmail-mode)))))
717
718 ;; Return a list of files from this buffer's Mail: option.
719 ;; Does not assume that messages have been parsed.
720 ;; Just returns nil if buffer does not look like Babyl format.
721 (defun rmail-parse-file-inboxes ()
722 (save-excursion
723 (save-restriction
724 (widen)
725 (goto-char 1)
726 (cond ((looking-at "BABYL OPTIONS:")
727 (search-forward "\n\^_" nil 'move)
728 (narrow-to-region 1 (point))
729 (goto-char 1)
730 (if (search-forward "\nMail:" nil t)
731 (progn
732 (narrow-to-region (point) (progn (end-of-line) (point)))
733 (goto-char (point-min))
734 (mail-parse-comma-list))))))))
735
736 (defun rmail-expunge-and-save ()
737 "Expunge and save RMAIL file."
738 (interactive)
739 (rmail-expunge)
740 (save-buffer)
741 (if (rmail-summary-exists)
742 (rmail-select-summary (set-buffer-modified-p nil))))
743
744 (defun rmail-quit ()
745 "Quit out of RMAIL."
746 (interactive)
747 (rmail-expunge-and-save)
748 ;; Don't switch to the summary buffer even if it was recently visible.
749 (if rmail-summary-buffer
750 (progn
751 (replace-buffer-in-windows rmail-summary-buffer)
752 (bury-buffer rmail-summary-buffer)))
753 (let ((obuf (current-buffer)))
754 (replace-buffer-in-windows obuf)
755 (bury-buffer obuf)))
756
757 (defun rmail-bury ()
758 "Bury current Rmail buffer and its summary buffer."
759 (interactive)
760 ;; This let var was called rmail-buffer, but that interfered
761 ;; with the buffer-local var used in summary buffers.
762 (let ((buffer-to-bury (current-buffer)))
763 (if (rmail-summary-exists)
764 (let (window)
765 (while (setq window (get-buffer-window rmail-summary-buffer))
766 (set-window-buffer window (other-buffer rmail-summary-buffer)))
767 (bury-buffer rmail-summary-buffer)))
768 (switch-to-buffer (other-buffer (current-buffer)))
769 (bury-buffer buffer-to-bury)))
770
771 (defun rmail-duplicate-message ()
772 "Create a duplicated copy of the current message.
773 The duplicate copy goes into the Rmail file just after the
774 original copy."
775 (interactive)
776 (widen)
777 (let ((buffer-read-only nil)
778 (number rmail-current-message)
779 (string (buffer-substring (rmail-msgbeg rmail-current-message)
780 (rmail-msgend rmail-current-message))))
781 (goto-char (rmail-msgend rmail-current-message))
782 (insert string)
783 (rmail-forget-messages)
784 (rmail-show-message number)
785 (message "Message duplicated")))
786
787 ;;;###autoload
788 (defun rmail-input (filename)
789 "Run Rmail on file FILENAME."
790 (interactive "FRun rmail on RMAIL file: ")
791 (rmail filename))
792
793 ;; Return a list of file names for all files in or under START
794 ;; whose names match rmail-secondary-file-regexp.
795 ;; This includes START itself, if that name matches.
796 ;; But normally START is a directory.
797 (defun rmail-find-all-files (start)
798 (if (file-accessible-directory-p start)
799 ;; Don't sort here.
800 (let ((files (directory-files start t
801 rmail-secondary-file-regexp t))
802 (ret nil)
803 file)
804 (while files
805 (setq file (car files))
806 (setq files (cdr files))
807 (or (member (file-name-nondirectory start) '("." ".."))
808 (setq ret (nconc
809 (rmail-find-all-files file)
810 ret))))
811 ;; Sort here instead of in directory-files
812 ;; because this list is usually much shorter.
813 (sort ret 'string<))
814 (let ((case-fold-search nil))
815 (if (string-match rmail-secondary-file-regexp start)
816 (list (file-name-nondirectory start))))))
817
818 (defun rmail-list-to-menu (menu-name l action &optional full-name)
819 (let ((menu (make-sparse-keymap menu-name)))
820 (mapcar
821 (function (lambda (item)
822 (let (command)
823 (if (consp item)
824 (progn
825 (setq command
826 (rmail-list-to-menu (car item) (cdr item)
827 action
828 (if full-name
829 (concat full-name "/"
830 (car item))
831 (car item))))
832 (setq name (car item)))
833 (progn
834 (setq name item)
835 (setq command
836 (list 'lambda () '(interactive)
837 (list action
838 (expand-file-name
839 (if full-name
840 (concat full-name "/" item)
841 item)
842 rmail-secondary-file-directory))))))
843 (define-key menu (vector (intern name))
844 (cons name command)))))
845 (reverse l))
846 menu))
847
848 ;; This command is always "disabled" when it appears in a menu.
849 (put 'rmail-disable-menu 'menu-enable ''nil)
850
851 (defun rmail-construct-io-menu ()
852 (let ((files (rmail-find-all-files rmail-secondary-file-directory)))
853 (if files
854 (progn
855 (define-key rmail-mode-map [menu-bar classify input-menu]
856 (cons "Input Rmail File"
857 (rmail-list-to-menu "Input Rmail File"
858 files
859 'rmail-input)))
860 (define-key rmail-mode-map [menu-bar classify output-menu]
861 (cons "Output Rmail File"
862 (rmail-list-to-menu "Output Rmail File"
863 files
864 'rmail-output-to-rmail-file))))
865
866 (define-key rmail-mode-map [menu-bar classify input-menu]
867 '("Input Rmail File" . rmail-disable-menu))
868 (define-key rmail-mode-map [menu-bar classify output-menu]
869 '("Output Rmail File" . rmail-disable-menu)))))
870
871 \f
872 ;;;; *** Rmail input ***
873
874 ;; RLK feature not added in this version:
875 ;; argument specifies inbox file or files in various ways.
876
877 (defun rmail-get-new-mail (&optional file-name)
878 "Move any new mail from this RMAIL file's inbox files.
879 The inbox files can be specified with the file's Mail: option. The
880 variable `rmail-primary-inbox-list' specifies the inboxes for your
881 primary RMAIL file if it has no Mail: option. By default, this is
882 your /usr/spool/mail/$USER.
883
884 You can also specify the file to get new mail from. In this case, the
885 file of new mail is not changed or deleted. Noninteractively, you can
886 pass the inbox file name as an argument. Interactively, a prefix
887 argument causes us to read a file name and use that file as the inbox.
888
889 This function runs `rmail-get-new-mail-hook' before saving the updated file.
890 It returns t if it got any new messages."
891 (interactive
892 (list (if current-prefix-arg
893 (read-file-name "Get new mail from file: "))))
894 (run-hooks 'rmail-before-get-new-mail-hook)
895 ;; If the disk file has been changed from under us,
896 ;; revert to it before we get new mail.
897 (or (verify-visited-file-modtime (current-buffer))
898 (find-file (buffer-file-name)))
899 (rmail-maybe-set-message-counters)
900 (widen)
901 ;; Get rid of all undo records for this buffer.
902 (or (eq buffer-undo-list t)
903 (setq buffer-undo-list nil))
904 (unwind-protect
905 (let ((opoint (point))
906 (new-messages 0)
907 (delete-files ())
908 ;; If buffer has not changed yet, and has not been saved yet,
909 ;; don't replace the old backup file now.
910 (make-backup-files (and make-backup-files (buffer-modified-p)))
911 (buffer-read-only nil)
912 ;; Don't make undo records for what we do in getting mail.
913 (buffer-undo-list t)
914 success)
915 (goto-char (point-max))
916 (skip-chars-backward " \t\n") ; just in case of brain damage
917 (delete-region (point) (point-max)) ; caused by require-final-newline
918 (save-excursion
919 (save-restriction
920 (narrow-to-region (point) (point))
921 ;; Read in the contents of the inbox files,
922 ;; renaming them as necessary,
923 ;; and adding to the list of files to delete eventually.
924 (if file-name
925 (rmail-insert-inbox-text (list file-name) nil)
926 (setq delete-files (rmail-insert-inbox-text rmail-inbox-list t)))
927 ;; Scan the new text and convert each message to babyl format.
928 (goto-char (point-min))
929 (unwind-protect
930 (save-excursion
931 (setq new-messages (rmail-convert-to-babyl-format)
932 success t))
933 ;; If we could not convert the file's inboxes,
934 ;; rename the files we tried to read
935 ;; so we won't over and over again.
936 (if (and (not file-name) (not success))
937 (let ((files delete-files)
938 (count 0))
939 (while files
940 (while (file-exists-p (format "RMAILOSE.%d" count))
941 (setq count (1+ count)))
942 (rename-file (car files)
943 (format "RMAILOSE.%d" count))
944 (setq files (cdr files))))))
945 (or (zerop new-messages)
946 (let (success)
947 (widen)
948 (search-backward "\n\^_" nil t)
949 (narrow-to-region (point) (point-max))
950 (goto-char (1+ (point-min)))
951 (rmail-count-new-messages)
952 (run-hooks 'rmail-get-new-mail-hook)
953 (save-buffer)))
954 ;; Delete the old files, now that babyl file is saved.
955 (while delete-files
956 (condition-case ()
957 ;; First, try deleting.
958 (condition-case ()
959 (delete-file (car delete-files))
960 (file-error
961 ;; If we can't delete it, truncate it.
962 (write-region (point) (point) (car delete-files))))
963 (file-error nil))
964 (setq delete-files (cdr delete-files)))))
965 (if (= new-messages 0)
966 (progn (goto-char opoint)
967 (if (or file-name rmail-inbox-list)
968 (message "(No new mail has arrived)"))
969 nil)
970 (if (rmail-summary-exists)
971 (rmail-select-summary
972 (rmail-update-summary)))
973 (message "%d new message%s read"
974 new-messages (if (= 1 new-messages) "" "s"))
975 ;; Move to the first new message
976 ;; unless we have other unseen messages before it.
977 (rmail-show-message (rmail-first-unseen-message))
978 (run-hooks 'rmail-after-get-new-mail-hook)
979 t))
980 ;; Don't leave the buffer screwed up if we get a disk-full error.
981 (rmail-show-message)))
982
983 (defun rmail-insert-inbox-text (files renamep)
984 (let (file tofile delete-files movemail popmail)
985 (while files
986 (setq file (file-truename
987 (expand-file-name (substitute-in-file-name (car files))))
988 tofile (expand-file-name
989 ;; Generate name to move to from inbox name,
990 ;; in case of multiple inboxes that need moving.
991 (concat ".newmail-" (file-name-nondirectory file))
992 ;; Use the directory of this rmail file
993 ;; because it's a nuisance to use the homedir
994 ;; if that is on a full disk and this rmail
995 ;; file isn't.
996 (file-name-directory
997 (expand-file-name buffer-file-name))))
998 ;; Always use movemail to rename the file,
999 ;; since there can be mailboxes in various directories.
1000 (setq movemail t)
1001 ;;; ;; If getting from mail spool directory,
1002 ;;; ;; use movemail to move rather than just renaming,
1003 ;;; ;; so as to interlock with the mailer.
1004 ;;; (setq movemail (string= file
1005 ;;; (file-truename
1006 ;;; (concat rmail-spool-directory
1007 ;;; (file-name-nondirectory file)))))
1008 (setq popmail (string-match "^po:" (file-name-nondirectory file)))
1009 (if popmail (setq file (file-name-nondirectory file)
1010 renamep t))
1011 (if movemail
1012 (progn
1013 ;; On some systems, /usr/spool/mail/foo is a directory
1014 ;; and the actual inbox is /usr/spool/mail/foo/foo.
1015 (if (file-directory-p file)
1016 (setq file (expand-file-name (user-login-name)
1017 file)))))
1018 (cond (popmail
1019 (if (and rmail-pop-password-required (not rmail-pop-password))
1020 (setq rmail-pop-password
1021 (rmail-read-passwd
1022 (format "Password for %s: "
1023 (substring file (+ popmail 3))))))
1024 (if (eq system-type 'windows-nt)
1025 ;; cannot have "po:" in file name
1026 (setq tofile
1027 (expand-file-name
1028 (concat ".newmail-pop-" (substring file (+ popmail 3)))
1029 (file-name-directory
1030 (expand-file-name buffer-file-name)))))
1031 (message "Getting mail from post office ..."))
1032 ((and (file-exists-p tofile)
1033 (/= 0 (nth 7 (file-attributes tofile))))
1034 (message "Getting mail from %s..." tofile))
1035 ((and (file-exists-p file)
1036 (/= 0 (nth 7 (file-attributes file))))
1037 (message "Getting mail from %s..." file)))
1038 ;; Set TOFILE if have not already done so, and
1039 ;; rename or copy the file FILE to TOFILE if and as appropriate.
1040 (cond ((not renamep)
1041 (setq tofile file))
1042 ((or (file-exists-p tofile) (and (not popmail)
1043 (not (file-exists-p file))))
1044 nil)
1045 ((and (not movemail) (not popmail))
1046 ;; Try copying. If that fails (perhaps no space),
1047 ;; rename instead.
1048 (condition-case nil
1049 (copy-file file tofile nil)
1050 (error
1051 ;; Third arg is t so we can replace existing file TOFILE.
1052 (rename-file file tofile t)))
1053 ;; Make the real inbox file empty.
1054 ;; Leaving it deleted could cause lossage
1055 ;; because mailers often won't create the file.
1056 (condition-case ()
1057 (write-region (point) (point) file)
1058 (file-error nil)))
1059 (t
1060 (let ((errors nil))
1061 (unwind-protect
1062 (save-excursion
1063 (setq errors (generate-new-buffer " *rmail loss*"))
1064 (buffer-disable-undo errors)
1065 (if rmail-pop-password
1066 (call-process
1067 (or rmail-movemail-program
1068 (expand-file-name "movemail" exec-directory))
1069 nil errors nil file tofile rmail-pop-password)
1070 (call-process
1071 (or rmail-movemail-program
1072 (expand-file-name "movemail" exec-directory))
1073 nil errors nil file tofile))
1074 (if (not (buffer-modified-p errors))
1075 ;; No output => movemail won
1076 nil
1077 (set-buffer errors)
1078 (subst-char-in-region (point-min) (point-max)
1079 ?\n ?\ )
1080 (goto-char (point-max))
1081 (skip-chars-backward " \t")
1082 (delete-region (point) (point-max))
1083 (goto-char (point-min))
1084 (if (looking-at "movemail: ")
1085 (delete-region (point-min) (match-end 0)))
1086 (beep t)
1087 (message "movemail: %s"
1088 (buffer-substring (point-min)
1089 (point-max)))
1090 (sit-for 3)
1091 nil))
1092 (if errors (kill-buffer errors))))))
1093 ;; At this point, TOFILE contains the name to read:
1094 ;; Either the alternate name (if we renamed)
1095 ;; or the actual inbox (if not renaming).
1096 (if (file-exists-p tofile)
1097 (let (size)
1098 (goto-char (point-max))
1099 (setq size (nth 1 (insert-file-contents tofile)))
1100 (goto-char (point-max))
1101 (or (= (preceding-char) ?\n)
1102 (zerop size)
1103 (insert ?\n))
1104 (setq delete-files (cons tofile delete-files))))
1105 (message "")
1106 (setq files (cdr files)))
1107 delete-files))
1108
1109 (defun rmail-read-passwd (prompt &optional default)
1110 "Read a password, echoing `.' for each character typed.
1111 End with RET, LFD, or ESC. DEL or C-h rubs out. C-u kills line.
1112 Optional DEFAULT is password to start with."
1113 (let ((pass (if default default ""))
1114 (c 0)
1115 (echo-keystrokes 0)
1116 (cursor-in-echo-area t))
1117 (while (progn (message "%s%s"
1118 prompt
1119 (make-string (length pass) ?.))
1120 (setq c (read-char))
1121 (and (/= c ?\r) (/= c ?\n) (/= c ?\e)))
1122 (if (= c ?\C-u)
1123 (setq pass "")
1124 (if (and (/= c ?\b) (/= c ?\177))
1125 (setq pass (concat pass (char-to-string c)))
1126 (if (> (length pass) 0)
1127 (setq pass (substring pass 0 -1))))))
1128 (message "")
1129 (message nil)
1130 pass))
1131
1132 ;; the rmail-break-forwarded-messages feature is not implemented
1133 (defun rmail-convert-to-babyl-format ()
1134 (let ((count 0) start
1135 (case-fold-search nil)
1136 (invalid-input-resync
1137 (function (lambda ()
1138 (message "Invalid Babyl format in inbox!")
1139 (sit-for 1)
1140 ;; Try to get back in sync with a real message.
1141 (if (re-search-forward
1142 (concat mmdf-delim1 "\\|^From") nil t)
1143 (beginning-of-line)
1144 (goto-char (point-max)))))))
1145 (goto-char (point-min))
1146 (save-restriction
1147 (while (not (eobp))
1148 (cond ((looking-at "BABYL OPTIONS:");Babyl header
1149 (if (search-forward "\n\^_" nil t)
1150 ;; If we find the proper terminator, delete through there.
1151 (delete-region (point-min) (point))
1152 (funcall invalid-input-resync)
1153 (delete-region (point-min) (point))))
1154 ;; Babyl format message
1155 ((looking-at "\^L")
1156 (or (search-forward "\n\^_" nil t)
1157 (funcall invalid-input-resync))
1158 (setq count (1+ count))
1159 ;; Make sure there is no extra white space after the ^_
1160 ;; at the end of the message.
1161 ;; Narrowing will make sure that whatever follows the junk
1162 ;; will be treated properly.
1163 (delete-region (point)
1164 (save-excursion
1165 (skip-chars-forward " \t\n")
1166 (point)))
1167 (narrow-to-region (point) (point-max)))
1168 ;;*** MMDF format
1169 ((let ((case-fold-search t))
1170 (looking-at mmdf-delim1))
1171 (let ((case-fold-search t))
1172 (replace-match "\^L\n0, unseen,,\n*** EOOH ***\n")
1173 (setq start (point))
1174 (re-search-forward mmdf-delim2 nil t)
1175 (replace-match "\^_"))
1176 (save-excursion
1177 (save-restriction
1178 (narrow-to-region start (1- (point)))
1179 (goto-char (point-min))
1180 (while (search-forward "\n\^_" nil t); single char "\^_"
1181 (replace-match "\n^_")))); 2 chars: "^" and "_"
1182 (narrow-to-region (point) (point-max))
1183 (setq count (1+ count)))
1184 ;;*** Mail format
1185 ((looking-at "^From ")
1186 (setq start (point))
1187 (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
1188 (rmail-nuke-pinhead-header)
1189 ;; If this message has a Content-Length field,
1190 ;; skip to the end of the contents.
1191 (let* ((header-end (save-excursion
1192 (and (re-search-forward "\n\n" nil t)
1193 (1- (point)))))
1194 (case-fold-search t)
1195 (size
1196 ;; Get the numeric value from the Content-Length field.
1197 (save-excursion
1198 ;; Back up to end of prev line,
1199 ;; in case the Content-Length field comes first.
1200 (forward-char -1)
1201 (and (search-forward "\ncontent-length: "
1202 header-end t)
1203 (let ((beg (point))
1204 (eol (progn (end-of-line) (point))))
1205 (string-to-int (buffer-substring beg eol)))))))
1206 (and size
1207 (if (and (natnump size)
1208 (<= (+ header-end size) (point-max))
1209 ;; Make sure this would put us at a position
1210 ;; that we could continue from.
1211 (save-excursion
1212 (goto-char (+ header-end size))
1213 (skip-chars-forward "\n")
1214 (or (eobp)
1215 (and (looking-at "BABYL OPTIONS:")
1216 (search-forward "\n\^_" nil t))
1217 (and (looking-at "\^L")
1218 (search-forward "\n\^_" nil t))
1219 (let ((case-fold-search t))
1220 (looking-at mmdf-delim1))
1221 (looking-at "From "))))
1222 (goto-char (+ header-end size))
1223 (message "Ignoring invalid Content-Length field")
1224 (sit-for 1 0 t))))
1225
1226 (if (re-search-forward
1227 (concat "^[\^_]?\\("
1228 rmail-unix-mail-delimiter
1229 "\\|"
1230 mmdf-delim1 "\\|"
1231 "^BABYL OPTIONS:\\|"
1232 "\^L\n[01],\\)") nil t)
1233 (goto-char (match-beginning 1))
1234 (goto-char (point-max)))
1235 (setq count (1+ count))
1236 (save-excursion
1237 (save-restriction
1238 (narrow-to-region start (point))
1239 (goto-char (point-min))
1240 (while (search-forward "\n\^_" nil t); single char
1241 (replace-match "\n^_")))); 2 chars: "^" and "_"
1242 (insert ?\^_)
1243 (narrow-to-region (point) (point-max)))
1244 ;;
1245 ;; This kludge is because some versions of sendmail.el
1246 ;; insert an extra newline at the beginning that shouldn't
1247 ;; be there. sendmail.el has been fixed, but old versions
1248 ;; may still be in use. -- rms, 7 May 1993.
1249 ((eolp) (delete-char 1))
1250 (t (error "Cannot convert to babyl format")))))
1251 count))
1252
1253 ;; Delete the "From ..." line, creating various other headers with
1254 ;; information from it if they don't already exist. Now puts the
1255 ;; original line into a mail-from: header line for debugging and for
1256 ;; use by the rmail-output function.
1257 (defun rmail-nuke-pinhead-header ()
1258 (save-excursion
1259 (save-restriction
1260 (let ((start (point))
1261 (end (progn
1262 (condition-case ()
1263 (search-forward "\n\n")
1264 (error
1265 (goto-char (point-max))
1266 (insert "\n\n")))
1267 (point)))
1268 has-from has-date)
1269 (narrow-to-region start end)
1270 (let ((case-fold-search t))
1271 (goto-char start)
1272 (setq has-from (search-forward "\nFrom:" nil t))
1273 (goto-char start)
1274 (setq has-date (and (search-forward "\nDate:" nil t) (point)))
1275 (goto-char start))
1276 (let ((case-fold-search nil))
1277 (if (re-search-forward (concat "^" rmail-unix-mail-delimiter) nil t)
1278 (replace-match
1279 (concat
1280 "Mail-from: \\&"
1281 ;; Keep and reformat the date if we don't
1282 ;; have a Date: field.
1283 (if has-date
1284 ""
1285 (concat
1286 "Date: \\2, \\4 \\3 \\9 \\5 "
1287
1288 ;; The timezone could be matched by group 7 or group 10.
1289 ;; If neither of them matched, assume EST, since only
1290 ;; Easterners would be so sloppy.
1291 ;; It's a shame the substitution can't use "\\10".
1292 (cond
1293 ((/= (match-beginning 7) (match-end 7)) "\\7")
1294 ((/= (match-beginning 10) (match-end 10))
1295 (buffer-substring (match-beginning 10)
1296 (match-end 10)))
1297 (t "EST"))
1298 "\n"))
1299 ;; Keep and reformat the sender if we don't
1300 ;; have a From: field.
1301 (if has-from
1302 ""
1303 "From: \\1\n"))
1304 t)))))))
1305 \f
1306 ;;;; *** Rmail Message Formatting and Header Manipulation ***
1307
1308 (defun rmail-reformat-message (beg end)
1309 (goto-char beg)
1310 (forward-line 1)
1311 (if (/= (following-char) ?0)
1312 (error "Bad format in RMAIL file."))
1313 (let ((buffer-read-only nil)
1314 (delta (- (buffer-size) end)))
1315 (delete-char 1)
1316 (insert ?1)
1317 (forward-line 1)
1318 (let ((case-fold-search t))
1319 (while (looking-at "Summary-line:\\|Mail-From:")
1320 (forward-line 1)))
1321 (if (looking-at "\\*\\*\\* EOOH \\*\\*\\*\n")
1322 (delete-region (point)
1323 (progn (forward-line 1) (point))))
1324 (let ((str (buffer-substring (point)
1325 (save-excursion (search-forward "\n\n" end 'move)
1326 (point)))))
1327 (insert str "*** EOOH ***\n")
1328 (narrow-to-region (point) (- (buffer-size) delta)))
1329 (goto-char (point-min))
1330 (if rmail-message-filter (funcall rmail-message-filter))
1331 (if (or rmail-displayed-headers rmail-ignored-headers)
1332 (rmail-clear-headers))))
1333
1334 (defun rmail-clear-headers (&optional ignored-headers)
1335 "Delete all header fields that Rmail should not show.
1336 If the optional argument IGNORED-HEADERS is non-nil,
1337 delete all header fields whose names match that regexp.
1338 Otherwise, if `rmail-displayed-headers' is non-nil,
1339 delete all header fields *except* those whose names match that regexp.
1340 Otherwise, delete all header fields whose names match `rmail-ignored-headers'."
1341 (if (search-forward "\n\n" nil t)
1342 (let ((case-fold-search t)
1343 (buffer-read-only nil))
1344 (if (and rmail-displayed-headers (null ignored-headers))
1345 (save-restriction
1346 (narrow-to-region (point-min) (point))
1347 (let (lim)
1348 (goto-char (point-min))
1349 (while (save-excursion
1350 (re-search-forward "\n[^ \t]")
1351 (and (not (eobp))
1352 (setq lim (1- (point)))))
1353 (if (save-excursion
1354 (re-search-forward rmail-displayed-headers lim t))
1355 (goto-char lim)
1356 (delete-region (point) lim))))
1357 (goto-char (point-min)))
1358 (or ignored-headers (setq ignored-headers rmail-ignored-headers))
1359 (save-restriction
1360 (narrow-to-region (point-min) (point))
1361 (while (progn
1362 (goto-char (point-min))
1363 (re-search-forward ignored-headers nil t))
1364 (beginning-of-line)
1365 (delete-region (point)
1366 (progn (re-search-forward "\n[^ \t]")
1367 (1- (point))))))))))
1368
1369 (defun rmail-toggle-header ()
1370 "Show original message header if pruned header currently shown, or vice versa."
1371 (interactive)
1372 (rmail-maybe-set-message-counters)
1373 (narrow-to-region (rmail-msgbeg rmail-current-message) (point-max))
1374 (let ((buffer-read-only nil))
1375 (goto-char (point-min))
1376 (forward-line 1)
1377 (if (= (following-char) ?1)
1378 (progn (delete-char 1)
1379 (insert ?0)
1380 (forward-line 1)
1381 (let ((case-fold-search t))
1382 (while (looking-at "Summary-Line:\\|Mail-From:")
1383 (forward-line 1)))
1384 (insert "*** EOOH ***\n")
1385 (forward-char -1)
1386 (search-forward "\n*** EOOH ***\n")
1387 (forward-line -1)
1388 (let ((temp (point)))
1389 (and (search-forward "\n\n" nil t)
1390 (delete-region temp (point))))
1391 (goto-char (point-min))
1392 (search-forward "\n*** EOOH ***\n")
1393 (narrow-to-region (point) (point-max)))
1394 (rmail-reformat-message (point-min) (point-max))))
1395 (rmail-highlight-headers))
1396 \f
1397 ;;;; *** Rmail Attributes and Keywords ***
1398
1399 ;; Make a string describing current message's attributes and keywords
1400 ;; and set it up as the name of a minor mode
1401 ;; so it will appear in the mode line.
1402 (defun rmail-display-labels ()
1403 (let ((blurb "") (beg (point-min-marker)) (end (point-max-marker)))
1404 (save-excursion
1405 (unwind-protect
1406 (progn
1407 (widen)
1408 (goto-char (rmail-msgbeg rmail-current-message))
1409 (forward-line 1)
1410 (if (looking-at "[01],")
1411 (progn
1412 (narrow-to-region (point) (progn (end-of-line) (point)))
1413 ;; Truly valid BABYL format requires a space before each
1414 ;; attribute or keyword name. Put them in if missing.
1415 (let (buffer-read-only)
1416 (goto-char (point-min))
1417 (while (search-forward "," nil t)
1418 (or (looking-at "[ ,]") (eobp)
1419 (insert " "))))
1420 (goto-char (point-max))
1421 (if (search-backward ",," nil 'move)
1422 (progn
1423 (if (> (point) (1+ (point-min)))
1424 (setq blurb (buffer-substring (+ 1 (point-min)) (point))))
1425 (if (> (- (point-max) (point)) 2)
1426 (setq blurb
1427 (concat blurb
1428 ";"
1429 (buffer-substring (+ (point) 3)
1430 (1- (point-max)))))))))))
1431 ;; Note: we don't use save-restriction because that does not work right
1432 ;; if changes are made outside the saved restriction
1433 ;; before that restriction is restored.
1434 (narrow-to-region beg end)
1435 (set-marker beg nil)
1436 (set-marker end nil)))
1437 (while (string-match " +," blurb)
1438 (setq blurb (concat (substring blurb 0 (match-beginning 0)) ","
1439 (substring blurb (match-end 0)))))
1440 (while (string-match ", +" blurb)
1441 (setq blurb (concat (substring blurb 0 (match-beginning 0)) ","
1442 (substring blurb (match-end 0)))))
1443 (setq mode-line-process
1444 (format " %d/%d%s"
1445 rmail-current-message rmail-total-messages blurb))))
1446
1447 ;; Turn an attribute of a message on or off according to STATE.
1448 ;; ATTR is the name of the attribute, as a string.
1449 ;; MSGNUM is message number to change; nil means current message.
1450 (defun rmail-set-attribute (attr state &optional msgnum)
1451 (let ((omax (point-max-marker))
1452 (omin (point-min-marker))
1453 (buffer-read-only nil))
1454 (or msgnum (setq msgnum rmail-current-message))
1455 (if (> msgnum 0)
1456 (unwind-protect
1457 (save-excursion
1458 (widen)
1459 (goto-char (+ 3 (rmail-msgbeg msgnum)))
1460 (let ((curstate
1461 (not
1462 (null (search-backward (concat ", " attr ",")
1463 (prog1 (point) (end-of-line)) t)))))
1464 (or (eq curstate (not (not state)))
1465 (if curstate
1466 (delete-region (point) (1- (match-end 0)))
1467 (beginning-of-line)
1468 (forward-char 2)
1469 (insert " " attr ","))))
1470 (if (string= attr "deleted")
1471 (rmail-set-message-deleted-p msgnum state)))
1472 ;; Note: we don't use save-restriction because that does not work right
1473 ;; if changes are made outside the saved restriction
1474 ;; before that restriction is restored.
1475 (narrow-to-region omin omax)
1476 (set-marker omin nil)
1477 (set-marker omax nil)
1478 (if (= msgnum rmail-current-message)
1479 (rmail-display-labels))))))
1480
1481 ;; Return t if the attributes/keywords line of msg number MSG
1482 ;; contains a match for the regexp LABELS.
1483 (defun rmail-message-labels-p (msg labels)
1484 (save-excursion
1485 (save-restriction
1486 (widen)
1487 (goto-char (rmail-msgbeg msg))
1488 (forward-char 3)
1489 (re-search-backward labels (prog1 (point) (end-of-line)) t))))
1490 \f
1491 ;;;; *** Rmail Message Selection And Support ***
1492
1493 (defun rmail-msgend (n)
1494 (marker-position (aref rmail-message-vector (1+ n))))
1495
1496 (defun rmail-msgbeg (n)
1497 (marker-position (aref rmail-message-vector n)))
1498
1499 (defun rmail-widen-to-current-msgbeg (function)
1500 "Call FUNCTION with point at start of internal data of current message.
1501 Assumes that bounds were previously narrowed to display the message in Rmail.
1502 The bounds are widened enough to move point where desired, then narrowed
1503 again afterward.
1504
1505 FUNCTION may not change the visible text of the message, but it may
1506 change the invisible header text."
1507 (save-excursion
1508 (let ((obeg (- (point-max) (point-min))))
1509 (unwind-protect
1510 (progn
1511 (narrow-to-region (rmail-msgbeg rmail-current-message)
1512 (point-max))
1513 (goto-char (point-min))
1514 (funcall function))
1515 ;; Note: we don't use save-restriction because that does not work right
1516 ;; if changes are made outside the saved restriction
1517 ;; before that restriction is restored.
1518 ;; Here we assume that changes made by FUNCTION
1519 ;; occur before the visible region of the message.
1520 (narrow-to-region (- (point-max) obeg) (point-max))))))
1521
1522 (defun rmail-forget-messages ()
1523 (unwind-protect
1524 (if (vectorp rmail-message-vector)
1525 (let* ((i 0)
1526 (v rmail-message-vector)
1527 (n (length v)))
1528 (while (< i n)
1529 (move-marker (aref v i) nil)
1530 (setq i (1+ i)))))
1531 (setq rmail-message-vector nil)
1532 (setq rmail-deleted-vector nil)))
1533
1534 (defun rmail-maybe-set-message-counters ()
1535 (if (not (and rmail-deleted-vector
1536 rmail-message-vector
1537 rmail-current-message
1538 rmail-total-messages))
1539 (rmail-set-message-counters)))
1540
1541 (defun rmail-count-new-messages (&optional nomsg)
1542 (let* ((case-fold-search nil)
1543 (total-messages 0)
1544 (messages-head nil)
1545 (deleted-head nil))
1546 (or nomsg (message "Counting new messages..."))
1547 (goto-char (point-max))
1548 ;; Put at the end of messages-head
1549 ;; the entry for message N+1, which marks
1550 ;; the end of message N. (N = number of messages).
1551 (search-backward "\n\^_")
1552 (forward-char 1)
1553 (setq messages-head (list (point-marker)))
1554 (rmail-set-message-counters-counter (point-min))
1555 (setq rmail-current-message (1+ rmail-total-messages))
1556 (setq rmail-total-messages
1557 (+ rmail-total-messages total-messages))
1558 (setq rmail-message-vector
1559 (vconcat rmail-message-vector (cdr messages-head)))
1560 (aset rmail-message-vector
1561 rmail-current-message (car messages-head))
1562 (setq rmail-deleted-vector
1563 (concat rmail-deleted-vector deleted-head))
1564 (setq rmail-summary-vector
1565 (vconcat rmail-summary-vector (make-vector total-messages nil)))
1566 (goto-char (point-min))
1567 (or nomsg (message "Counting new messages...done (%d)" total-messages))))
1568
1569 (defun rmail-set-message-counters ()
1570 (rmail-forget-messages)
1571 (save-excursion
1572 (save-restriction
1573 (widen)
1574 (let* ((point-save (point))
1575 (total-messages 0)
1576 (messages-after-point)
1577 (case-fold-search nil)
1578 (messages-head nil)
1579 (deleted-head nil))
1580 (message "Counting messages...")
1581 (goto-char (point-max))
1582 ;; Put at the end of messages-head
1583 ;; the entry for message N+1, which marks
1584 ;; the end of message N. (N = number of messages).
1585 (search-backward "\n\^_" nil t)
1586 (if (/= (point) (point-max)) (forward-char 1))
1587 (setq messages-head (list (point-marker)))
1588 (rmail-set-message-counters-counter (min (point) point-save))
1589 (setq messages-after-point total-messages)
1590 (rmail-set-message-counters-counter)
1591 (setq rmail-total-messages total-messages)
1592 (setq rmail-current-message
1593 (min total-messages
1594 (max 1 (- total-messages messages-after-point))))
1595 (setq rmail-message-vector
1596 (apply 'vector (cons (point-min-marker) messages-head))
1597 rmail-deleted-vector (concat "D" deleted-head)
1598 rmail-summary-vector (make-vector rmail-total-messages nil))
1599 (message "Counting messages...done")))))
1600
1601 (defun rmail-set-message-counters-counter (&optional stop)
1602 (while (search-backward "\n\^_\^L\n" stop t)
1603 (forward-char 1)
1604 (setq messages-head (cons (point-marker) messages-head))
1605 (save-excursion
1606 (setq deleted-head
1607 (cons (if (search-backward ", deleted,"
1608 (prog1 (point)
1609 (forward-line 2))
1610 t)
1611 ?D ?\ )
1612 deleted-head)))
1613 (if (zerop (% (setq total-messages (1+ total-messages)) 20))
1614 (message "Counting messages...%d" total-messages))))
1615
1616 (defun rmail-beginning-of-message ()
1617 "Show current message starting from the beginning."
1618 (interactive)
1619 (rmail-show-message rmail-current-message))
1620
1621 (defun rmail-show-message (&optional n no-summary)
1622 "Show message number N (prefix argument), counting from start of file.
1623 If summary buffer is currently displayed, update current message there also."
1624 (interactive "p")
1625 (rmail-maybe-set-message-counters)
1626 (widen)
1627 (if (zerop rmail-total-messages)
1628 (progn (narrow-to-region (point-min) (1- (point-max)))
1629 (goto-char (point-min))
1630 (setq mode-line-process nil))
1631 (let (blurb)
1632 (if (not n)
1633 (setq n rmail-current-message)
1634 (cond ((<= n 0)
1635 (setq n 1
1636 rmail-current-message 1
1637 blurb "No previous message"))
1638 ((> n rmail-total-messages)
1639 (setq n rmail-total-messages
1640 rmail-current-message rmail-total-messages
1641 blurb "No following message"))
1642 (t
1643 (setq rmail-current-message n))))
1644 (let ((beg (rmail-msgbeg n))
1645 (end (rmail-msgend n)))
1646 (goto-char beg)
1647 (forward-line 1)
1648 (if (= (following-char) ?0)
1649 (progn
1650 (rmail-reformat-message beg end)
1651 (rmail-set-attribute "unseen" nil))
1652 (search-forward "\n*** EOOH ***\n" end t)
1653 (narrow-to-region (point) end))
1654 (goto-char (point-min))
1655 (rmail-display-labels)
1656 (rmail-highlight-headers)
1657 (if transient-mark-mode (deactivate-mark))
1658 (run-hooks 'rmail-show-message-hook)
1659 ;; If there is a summary buffer, try to move to this message
1660 ;; in that buffer. But don't complain if this message
1661 ;; is not mentioned in the summary.
1662 ;; Don't do this at all if we were called on behalf
1663 ;; of cursor motion in the summary buffer.
1664 (and (rmail-summary-exists) (not no-summary)
1665 (let ((curr-msg rmail-current-message))
1666 (rmail-select-summary
1667 (rmail-summary-goto-msg curr-msg t t))))
1668 (if blurb
1669 (message blurb))))))
1670
1671 ;; Find all occurrences of certain fields, and highlight them.
1672 (defun rmail-highlight-headers ()
1673 ;; Do this only if the system supports faces.
1674 (if (and (fboundp 'internal-find-face)
1675 rmail-highlighted-headers)
1676 (save-excursion
1677 (search-forward "\n\n" nil 'move)
1678 (save-restriction
1679 (narrow-to-region (point-min) (point))
1680 (let ((case-fold-search t)
1681 (inhibit-read-only t)
1682 ;; Highlight with boldface if that is available.
1683 ;; Otherwise use the `highlight' face.
1684 (face (or rmail-highlight-face
1685 (if (face-differs-from-default-p 'bold)
1686 'bold 'highlight)))
1687 ;; List of overlays to reuse.
1688 (overlays rmail-overlay-list))
1689 (goto-char (point-min))
1690 (while (re-search-forward rmail-highlighted-headers nil t)
1691 (skip-chars-forward " \t")
1692 (let ((beg (point))
1693 overlay)
1694 (while (progn (forward-line 1)
1695 (looking-at "[ \t]")))
1696 ;; Back up over newline, then trailing spaces or tabs
1697 (forward-char -1)
1698 (while (member (preceding-char) '(? ?\t))
1699 (forward-char -1))
1700 (if overlays
1701 ;; Reuse an overlay we already have.
1702 (progn
1703 (setq overlay (car overlays)
1704 overlays (cdr overlays))
1705 (overlay-put overlay 'face face)
1706 (move-overlay overlay beg (point)))
1707 ;; Make a new overlay and add it to
1708 ;; rmail-overlay-list.
1709 (setq overlay (make-overlay beg (point)))
1710 (overlay-put overlay 'face face)
1711 (setq rmail-overlay-list
1712 (cons overlay rmail-overlay-list))))))))))
1713
1714 (defun rmail-next-message (n)
1715 "Show following message whether deleted or not.
1716 With prefix arg N, moves forward N messages, or backward if N is negative."
1717 (interactive "p")
1718 (rmail-maybe-set-message-counters)
1719 (rmail-show-message (+ rmail-current-message n)))
1720
1721 (defun rmail-previous-message (n)
1722 "Show previous message whether deleted or not.
1723 With prefix arg N, moves backward N messages, or forward if N is negative."
1724 (interactive "p")
1725 (rmail-next-message (- n)))
1726
1727 (defun rmail-next-undeleted-message (n)
1728 "Show following non-deleted message.
1729 With prefix arg N, moves forward N non-deleted messages,
1730 or backward if N is negative.
1731
1732 Returns t if a new message is being shown, nil otherwise."
1733 (interactive "p")
1734 (rmail-maybe-set-message-counters)
1735 (let ((lastwin rmail-current-message)
1736 (current rmail-current-message))
1737 (while (and (> n 0) (< current rmail-total-messages))
1738 (setq current (1+ current))
1739 (if (not (rmail-message-deleted-p current))
1740 (setq lastwin current n (1- n))))
1741 (while (and (< n 0) (> current 1))
1742 (setq current (1- current))
1743 (if (not (rmail-message-deleted-p current))
1744 (setq lastwin current n (1+ n))))
1745 (if (/= lastwin rmail-current-message)
1746 (progn (rmail-show-message lastwin)
1747 t)
1748 (if (< n 0)
1749 (message "No previous nondeleted message"))
1750 (if (> n 0)
1751 (message "No following nondeleted message"))
1752 nil)))
1753
1754 (defun rmail-previous-undeleted-message (n)
1755 "Show previous non-deleted message.
1756 With prefix argument N, moves backward N non-deleted messages,
1757 or forward if N is negative."
1758 (interactive "p")
1759 (rmail-next-undeleted-message (- n)))
1760
1761 (defun rmail-first-message ()
1762 "Show first message in file."
1763 (interactive)
1764 (rmail-maybe-set-message-counters)
1765 (rmail-show-message 1))
1766
1767 (defun rmail-last-message ()
1768 "Show last message in file."
1769 (interactive)
1770 (rmail-maybe-set-message-counters)
1771 (rmail-show-message rmail-total-messages))
1772
1773 (defun rmail-what-message ()
1774 (let ((where (point))
1775 (low 1)
1776 (high rmail-total-messages)
1777 (mid (/ rmail-total-messages 2)))
1778 (while (> (- high low) 1)
1779 (if (>= where (rmail-msgbeg mid))
1780 (setq low mid)
1781 (setq high mid))
1782 (setq mid (+ low (/ (- high low) 2))))
1783 (if (>= where (rmail-msgbeg high)) high low)))
1784
1785 (defun rmail-message-recipients-p (msg recipients &optional primary-only)
1786 (save-restriction
1787 (goto-char (rmail-msgbeg msg))
1788 (search-forward "\n*** EOOH ***\n")
1789 (narrow-to-region (point) (progn (search-forward "\n\n") (point)))
1790 (or (string-match recipients (or (mail-fetch-field "To") ""))
1791 (string-match recipients (or (mail-fetch-field "From") ""))
1792 (if (not primary-only)
1793 (string-match recipients (or (mail-fetch-field "Cc") ""))))))
1794
1795 (defun rmail-message-regexp-p (msg regexp)
1796 "Return t, if for message number MSG, regexp REGEXP matches in the header."
1797 (goto-char (rmail-msgbeg msg))
1798 (let ((end
1799 (save-excursion
1800 (search-forward "*** EOOH ***" (point-max)) (point))))
1801 (re-search-forward regexp end t)))
1802
1803 (defvar rmail-search-last-regexp nil)
1804 (defun rmail-search (regexp &optional n)
1805 "Show message containing next match for REGEXP (but not the current msg).
1806 Prefix argument gives repeat count; negative argument means search
1807 backwards (through earlier messages).
1808 Interactively, empty argument means use same regexp used last time."
1809 (interactive
1810 (let* ((reversep (< (prefix-numeric-value current-prefix-arg) 0))
1811 (prompt
1812 (concat (if reversep "Reverse " "") "Rmail search (regexp): "))
1813 regexp)
1814 (if rmail-search-last-regexp
1815 (setq prompt (concat prompt
1816 "(default "
1817 rmail-search-last-regexp
1818 ") ")))
1819 (setq regexp (read-string prompt))
1820 (cond ((not (equal regexp ""))
1821 (setq rmail-search-last-regexp regexp))
1822 ((not rmail-search-last-regexp)
1823 (error "No previous Rmail search string")))
1824 (list rmail-search-last-regexp
1825 (prefix-numeric-value current-prefix-arg))))
1826 (or n (setq n 1))
1827 (message "%sRmail search for %s..."
1828 (if (< n 0) "Reverse " "")
1829 regexp)
1830 (rmail-maybe-set-message-counters)
1831 (let ((omin (point-min))
1832 (omax (point-max))
1833 (opoint (point))
1834 win
1835 (reversep (< n 0))
1836 (msg rmail-current-message))
1837 (unwind-protect
1838 (progn
1839 (widen)
1840 (while (/= n 0)
1841 ;; Check messages one by one, advancing message number up or down
1842 ;; but searching forward through each message.
1843 (if reversep
1844 (while (and (null win) (> msg 1))
1845 (goto-char (rmail-msgbeg (setq msg (1- msg))))
1846 (setq win (re-search-forward
1847 regexp (rmail-msgend msg) t)))
1848 (while (and (null win) (< msg rmail-total-messages))
1849 (goto-char (rmail-msgbeg (setq msg (1+ msg))))
1850 (setq win (re-search-forward regexp (rmail-msgend msg) t))))
1851 (setq n (+ n (if reversep 1 -1)))))
1852 (if win
1853 (progn
1854 ;; If this is a reverse search and we found a message,
1855 ;; search backward thru this message to position point.
1856 (if reversep
1857 (progn
1858 (goto-char (rmail-msgend msg))
1859 (re-search-backward
1860 regexp (rmail-msgbeg msg) t)))
1861 (setq win (point))
1862 (rmail-show-message msg)
1863 (message "%sRmail search for %s...done"
1864 (if reversep "Reverse " "")
1865 regexp)
1866 (goto-char win))
1867 (goto-char opoint)
1868 (narrow-to-region omin omax)
1869 (ding)
1870 (message "Search failed: %s" regexp)))))
1871
1872 (defun rmail-search-backwards (regexp &optional n)
1873 "Show message containing previous match for REGEXP.
1874 Prefix argument gives repeat count; negative argument means search
1875 forward (through later messages).
1876 Interactively, empty argument means use same regexp used last time."
1877 (interactive
1878 (let* ((reversep (>= (prefix-numeric-value current-prefix-arg) 0))
1879 (prompt
1880 (concat (if reversep "Reverse " "") "Rmail search (regexp): "))
1881 regexp)
1882 (if rmail-search-last-regexp
1883 (setq prompt (concat prompt
1884 "(default "
1885 rmail-search-last-regexp
1886 ") ")))
1887 (setq regexp (read-string prompt))
1888 (cond ((not (equal regexp ""))
1889 (setq rmail-search-last-regexp regexp))
1890 ((not rmail-search-last-regexp)
1891 (error "No previous Rmail search string")))
1892 (list rmail-search-last-regexp
1893 (prefix-numeric-value current-prefix-arg))))
1894 (rmail-search regexp (- (or n 1))))
1895
1896 ;; Show the first message which has the `unseen' attribute.
1897 (defun rmail-first-unseen-message ()
1898 (rmail-maybe-set-message-counters)
1899 (let ((current 1)
1900 found)
1901 (save-restriction
1902 (widen)
1903 (while (and (not found) (<= current rmail-total-messages))
1904 (if (rmail-message-labels-p current ", ?\\(unseen\\),")
1905 (setq found current))
1906 (setq current (1+ current))))
1907 ;; Let the caller show the message.
1908 ;; (if found
1909 ;; (rmail-show-message found))
1910 found))
1911
1912 (defun rmail-next-same-subject (n)
1913 "Go to the next mail message having the same subject header.
1914 With prefix argument N, do this N times.
1915 If N is negative, go backwards instead."
1916 (interactive "p")
1917 (let* ((subject (mail-fetch-field "Subject"))
1918 (search-regexp (concat "^Subject: *\\(Re: *\\)?"
1919 (regexp-quote subject)
1920 "\n"))
1921 (forward (> n 0))
1922 (i rmail-current-message)
1923 found)
1924 (if (string-match "Re:[ \t]*" subject)
1925 (setq subject (substring subject (match-end 0))))
1926 (save-excursion
1927 (save-restriction
1928 (widen)
1929 (while (and (/= n 0)
1930 (if forward
1931 (< i rmail-total-messages)
1932 (> i 1)))
1933 (let (done)
1934 (while (and (not done)
1935 (if forward
1936 (< i rmail-total-messages)
1937 (> i 1)))
1938 (setq i (if forward (1+ i) (1- i)))
1939 (goto-char (rmail-msgbeg i))
1940 (search-forward "\n*** EOOH ***\n")
1941 (let ((beg (point)) end)
1942 (search-forward "\n\n")
1943 (setq end (point))
1944 (goto-char beg)
1945 (setq done (re-search-forward search-regexp end t))))
1946 (if done (setq found i)))
1947 (setq n (if forward (1- n) (1+ n))))))
1948 (if found
1949 (rmail-show-message found)
1950 (error "No %s message with same subject"
1951 (if forward "following" "previous")))))
1952
1953 (defun rmail-previous-same-subject (n)
1954 "Go to the previous mail message having the same subject header.
1955 With prefix argument N, do this N times.
1956 If N is negative, go forwards instead."
1957 (interactive "p")
1958 (rmail-next-same-subject (- n)))
1959 \f
1960 ;;;; *** Rmail Message Deletion Commands ***
1961
1962 (defun rmail-message-deleted-p (n)
1963 (= (aref rmail-deleted-vector n) ?D))
1964
1965 (defun rmail-set-message-deleted-p (n state)
1966 (aset rmail-deleted-vector n (if state ?D ?\ )))
1967
1968 (defun rmail-delete-message ()
1969 "Delete this message and stay on it."
1970 (interactive)
1971 (rmail-set-attribute "deleted" t)
1972 (run-hooks 'rmail-delete-message-hook))
1973
1974 (defun rmail-undelete-previous-message ()
1975 "Back up to deleted message, select it, and undelete it."
1976 (interactive)
1977 (let ((msg rmail-current-message))
1978 (while (and (> msg 0)
1979 (not (rmail-message-deleted-p msg)))
1980 (setq msg (1- msg)))
1981 (if (= msg 0)
1982 (error "No previous deleted message")
1983 (if (/= msg rmail-current-message)
1984 (rmail-show-message msg))
1985 (rmail-set-attribute "deleted" nil)
1986 (if (rmail-summary-exists)
1987 (save-excursion
1988 (set-buffer rmail-summary-buffer)
1989 (rmail-summary-mark-undeleted msg)))
1990 (rmail-maybe-display-summary))))
1991
1992 (defun rmail-delete-forward (&optional backward)
1993 "Delete this message and move to next nondeleted one.
1994 Deleted messages stay in the file until the \\[rmail-expunge] command is given.
1995 With prefix argument, delete and move backward.
1996
1997 Returns t if a new message is displayed after the delete, or nil otherwise."
1998 (interactive "P")
1999 (rmail-set-attribute "deleted" t)
2000 (run-hooks 'rmail-delete-message-hook)
2001 (let ((del-msg rmail-current-message))
2002 (if (rmail-summary-exists)
2003 (rmail-select-summary
2004 (rmail-summary-mark-deleted del-msg)))
2005 (prog1 (rmail-next-undeleted-message (if backward -1 1))
2006 (rmail-maybe-display-summary))))
2007
2008 (defun rmail-delete-backward ()
2009 "Delete this message and move to previous nondeleted one.
2010 Deleted messages stay in the file until the \\[rmail-expunge] command is given."
2011 (interactive)
2012 (rmail-delete-forward t))
2013
2014 ;; Compute the message number a given message would have after expunging.
2015 ;; The present number of the message is OLDNUM.
2016 ;; DELETEDVEC should be rmail-deleted-vector.
2017 ;; The value is nil for a message that would be deleted.
2018 (defun rmail-msg-number-after-expunge (deletedvec oldnum)
2019 (if (or (null oldnum) (= (aref deletedvec oldnum) ?D))
2020 nil
2021 (let ((i 0)
2022 (newnum 0))
2023 (while (< i oldnum)
2024 (if (/= (aref deletedvec i) ?D)
2025 (setq newnum (1+ newnum)))
2026 (setq i (1+ i)))
2027 newnum)))
2028
2029 (defun rmail-only-expunge ()
2030 "Actually erase all deleted messages in the file."
2031 (interactive)
2032 (message "Expunging deleted messages...")
2033 ;; Discard all undo records for this buffer.
2034 (or (eq buffer-undo-list t)
2035 (setq buffer-undo-list nil))
2036 (rmail-maybe-set-message-counters)
2037 (let* ((omax (- (buffer-size) (point-max)))
2038 (omin (- (buffer-size) (point-min)))
2039 (opoint (if (and (> rmail-current-message 0)
2040 (rmail-message-deleted-p rmail-current-message))
2041 0
2042 (- (point) (point-min))))
2043 (messages-head (cons (aref rmail-message-vector 0) nil))
2044 (messages-tail messages-head)
2045 ;; Don't make any undo records for the expunging.
2046 (buffer-undo-list t)
2047 (win))
2048 (unwind-protect
2049 (save-excursion
2050 (widen)
2051 (goto-char (point-min))
2052 (let ((counter 0)
2053 (number 1)
2054 (total rmail-total-messages)
2055 (new-message-number rmail-current-message)
2056 (new-summary nil)
2057 (rmailbuf (current-buffer))
2058 (buffer-read-only nil)
2059 (messages rmail-message-vector)
2060 (deleted rmail-deleted-vector)
2061 (summary rmail-summary-vector))
2062 (setq rmail-total-messages nil
2063 rmail-current-message nil
2064 rmail-message-vector nil
2065 rmail-deleted-vector nil
2066 rmail-summary-vector nil)
2067
2068 ;; Find each sendmail buffer that is set to reply
2069 ;; to a message in this buffer, and update its
2070 ;; message number.
2071 (let ((bufs (buffer-list)))
2072 (while bufs
2073 (save-excursion
2074 (set-buffer (car bufs))
2075 (and (boundp 'rmail-send-actions-rmail-buffer)
2076 (eq rmail-send-actions-rmail-buffer rmailbuf)
2077 (setq rmail-send-actions-rmail-msg-number
2078 (rmail-msg-number-after-expunge
2079 deleted
2080 rmail-send-actions-rmail-msg-number))))
2081 (setq bufs (cdr bufs))))
2082
2083 (while (<= number total)
2084 (if (= (aref deleted number) ?D)
2085 (progn
2086 (delete-region
2087 (marker-position (aref messages number))
2088 (marker-position (aref messages (1+ number))))
2089 (move-marker (aref messages number) nil)
2090 (if (> new-message-number counter)
2091 (setq new-message-number (1- new-message-number))))
2092 (setq counter (1+ counter))
2093 (setq messages-tail
2094 (setcdr messages-tail
2095 (cons (aref messages number) nil)))
2096 (setq new-summary
2097 (cons (if (= counter number) (aref summary (1- number)))
2098 new-summary)))
2099 (if (zerop (% (setq number (1+ number)) 20))
2100 (message "Expunging deleted messages...%d" number)))
2101 (setq messages-tail
2102 (setcdr messages-tail
2103 (cons (aref messages number) nil)))
2104 (setq rmail-current-message new-message-number
2105 rmail-total-messages counter
2106 rmail-message-vector (apply 'vector messages-head)
2107 rmail-deleted-vector (make-string (1+ counter) ?\ )
2108 rmail-summary-vector (vconcat (nreverse new-summary))
2109 win t)))
2110 (message "Expunging deleted messages...done")
2111 (if (not win)
2112 (narrow-to-region (- (buffer-size) omin) (- (buffer-size) omax)))
2113 (rmail-show-message
2114 (if (zerop rmail-current-message) 1 nil))
2115 (forward-char opoint))))
2116
2117 (defun rmail-expunge ()
2118 "Erase deleted messages from Rmail file and summary buffer."
2119 (interactive)
2120 (rmail-only-expunge)
2121 (if (rmail-summary-exists)
2122 (rmail-select-summary
2123 (rmail-update-summary))))
2124 \f
2125 ;;;; *** Rmail Mailing Commands ***
2126
2127 (defun rmail-start-mail (&rest args)
2128 (if (and window-system rmail-mail-new-frame)
2129 (prog1
2130 (apply 'mail-other-frame args)
2131 ;; This is not a standard frame parameter;
2132 ;; nothing except sendmail.el looks at it.
2133 (modify-frame-parameters (selected-frame)
2134 '((mail-dedicated-frame . t))))
2135 (apply 'mail-other-window args)))
2136
2137 (defun rmail-mail ()
2138 "Send mail in another window.
2139 While composing the message, use \\[mail-yank-original] to yank the
2140 original message into it."
2141 (interactive)
2142 (rmail-start-mail nil nil nil nil nil (current-buffer)))
2143
2144 (defun rmail-continue ()
2145 "Continue composing outgoing message previously being composed."
2146 (interactive)
2147 (rmail-start-mail t))
2148
2149 (put 'rmail-send-actions-rmail-buffer 'permanent-local t)
2150 (put 'rmail-send-actions-rmail-msg-number 'permanent-local t)
2151
2152 (defun rmail-reply (just-sender)
2153 "Reply to the current message.
2154 Normally include CC: to all other recipients of original message;
2155 prefix argument means ignore them. While composing the reply,
2156 use \\[mail-yank-original] to yank the original message into it."
2157 (interactive "P")
2158 (let (from reply-to cc subject date to message-id resent-reply-to
2159 (msgnum rmail-current-message)
2160 (rmail-buffer (current-buffer)))
2161 (save-excursion
2162 (save-restriction
2163 (widen)
2164 (goto-char (rmail-msgbeg rmail-current-message))
2165 (forward-line 1)
2166 (if (= (following-char) ?0)
2167 (narrow-to-region
2168 (progn (forward-line 2)
2169 (point))
2170 (progn (search-forward "\n\n" (rmail-msgend rmail-current-message)
2171 'move)
2172 (point)))
2173 (narrow-to-region (point)
2174 (progn (search-forward "\n*** EOOH ***\n")
2175 (beginning-of-line) (point))))
2176 (setq resent-reply-to (mail-fetch-field "resent-reply-to" t)
2177 from (mail-fetch-field "from")
2178 reply-to (or resent-reply-to
2179 (mail-fetch-field "reply-to" nil t)
2180 from)
2181 cc (cond (just-sender nil)
2182 (resent-reply-to (mail-fetch-field "resent-cc" t))
2183 (t (mail-fetch-field "cc" nil t)))
2184 subject (or (and resent-reply-to
2185 (mail-fetch-field "resent-subject" t))
2186 (mail-fetch-field "subject"))
2187 date (or (and resent-reply-to
2188 (mail-fetch-field "resent-date" t))
2189 (mail-fetch-field "date"))
2190 to (cond (resent-reply-to
2191 (or (mail-fetch-field "resent-to" t)) "")
2192 ((mail-fetch-field "to" nil t))
2193 ;((mail-fetch-field "apparently-to")) ack gag barf
2194 (t ""))
2195 message-id (cond (resent-reply-to
2196 (mail-fetch-field "resent-message-id" t))
2197 ((mail-fetch-field "message-id"))))))
2198 (and (stringp subject)
2199 (setq subject
2200 (concat rmail-reply-prefix
2201 (if (string-match rmail-reply-regexp subject)
2202 (substring subject (match-end 0))
2203 subject))))
2204 (rmail-start-mail nil
2205 (mail-strip-quoted-names reply-to)
2206 subject
2207 (rmail-make-in-reply-to-field from date message-id)
2208 (if just-sender
2209 nil
2210 (let* ((cc-list (rmail-dont-reply-to
2211 (mail-strip-quoted-names
2212 (if (null cc) to (concat to ", " cc))))))
2213 (if (string= cc-list "") nil cc-list)))
2214 (current-buffer)
2215 (list (list '(lambda ()
2216 (let ((msgnum rmail-send-actions-rmail-msg-number))
2217 (save-excursion
2218 (set-buffer rmail-send-actions-rmail-buffer)
2219 (if msgnum
2220 (rmail-set-attribute "answered" t msgnum))))))))
2221 ;; We keep the rmail buffer and message number in these
2222 ;; buffer-local vars in the sendmail buffer,
2223 ;; so that rmail-only-expunge can relocate the message number.
2224 (make-local-variable 'rmail-send-actions-rmail-buffer)
2225 (make-local-variable 'rmail-send-actions-rmail-msg-number)
2226 (setq rmail-send-actions-rmail-buffer rmail-buffer)
2227 (setq rmail-send-actions-rmail-msg-number msgnum)))
2228
2229 (defun rmail-make-in-reply-to-field (from date message-id)
2230 (cond ((not from)
2231 (if message-id
2232 message-id
2233 nil))
2234 (mail-use-rfc822
2235 (require 'rfc822)
2236 (let ((tem (car (rfc822-addresses from))))
2237 (if message-id
2238 (if (string-match
2239 (regexp-quote (if (string-match "@[^@]*\\'" tem)
2240 (substring tem 0 (match-beginning 0))
2241 tem))
2242 message-id)
2243 ;; Message-ID is sufficiently informative
2244 message-id
2245 (concat message-id " (" tem ")"))
2246 ;; Copy TEM, discarding text properties.
2247 (setq tem (copy-sequence tem))
2248 (set-text-properties 0 (length tem) nil tem)
2249 (setq tem (copy-sequence tem))
2250 ;; Use prin1 to fake RFC822 quoting
2251 (let ((field (prin1-to-string tem)))
2252 (if date
2253 (concat field "'s message of " date)
2254 field)))))
2255 ((let* ((foo "[^][\000-\037\177-\377()<>@,;:\\\" ]+")
2256 (bar "[^][\000-\037\177-\377()<>@,;:\\\"]+"))
2257 ;; Can't use format because format loses on \000 (unix *^&%*^&%$!!)
2258 (or (string-match (concat "\\`[ \t]*\\(" bar
2259 "\\)\\(<" foo "@" foo ">\\)?[ \t]*\\'")
2260 ;; "Unix Loser <Foo@bar.edu>" => "Unix Loser"
2261 from)
2262 (string-match (concat "\\`[ \t]*<" foo "@" foo ">[ \t]*(\\("
2263 bar "\\))[ \t]*\\'")
2264 ;; "<Bugs@bar.edu>" (Losing Unix) => "Losing Unix"
2265 from)))
2266 (let ((start (match-beginning 1))
2267 (end (match-end 1)))
2268 ;; Trim whitespace which above regexp match allows
2269 (while (and (< start end)
2270 (memq (aref from start) '(?\t ?\ )))
2271 (setq start (1+ start)))
2272 (while (and (< start end)
2273 (memq (aref from (1- end)) '(?\t ?\ )))
2274 (setq end (1- end)))
2275 (let ((field (substring from start end)))
2276 (if date (setq field (concat "message from " field " on " date)))
2277 (if message-id
2278 ;; "<AA259@bar.edu> (message from Unix Loser on 1-Apr-89)"
2279 (concat message-id " (" field ")")
2280 field))))
2281 (t
2282 ;; If we can't kludge it simply, do it correctly
2283 (let ((mail-use-rfc822 t))
2284 (rmail-make-in-reply-to-field from date message-id)))))
2285 \f
2286 (defun rmail-forward (resend)
2287 "Forward the current message to another user.
2288 With prefix argument, \"resend\" the message instead of forwarding it;
2289 see the documentation of `rmail-resend'."
2290 (interactive "P")
2291 (if resend
2292 (call-interactively 'rmail-resend)
2293 (let ((forward-buffer (current-buffer))
2294 (msgnum rmail-current-message)
2295 (subject (concat "["
2296 (let ((from (or (mail-fetch-field "From")
2297 (mail-fetch-field ">From"))))
2298 (if from
2299 (concat (mail-strip-quoted-names from) ": ")
2300 ""))
2301 (or (mail-fetch-field "Subject") "")
2302 "]")))
2303 ;; If only one window, use it for the mail buffer.
2304 ;; Otherwise, use another window for the mail buffer
2305 ;; so that the Rmail buffer remains visible
2306 ;; and sending the mail will get back to it.
2307 (if (funcall (if (and (not rmail-mail-new-frame) (one-window-p t))
2308 (function mail)
2309 (function rmail-start-mail))
2310 nil nil subject nil nil nil
2311 (list (list (function
2312 (lambda ()
2313 (let ((msgnum
2314 rmail-send-actions-rmail-msg-number))
2315 (save-excursion
2316 (set-buffer rmail-send-actions-rmail-buffer)
2317 (if msgnum
2318 (rmail-set-attribute
2319 "forwarded" t msgnum)))))))))
2320 ;; The mail buffer is now current.
2321 (save-excursion
2322 ;; We keep the rmail buffer and message number in these
2323 ;; buffer-local vars in the sendmail buffer,
2324 ;; so that rmail-only-expunge can relocate the message number.
2325 (make-local-variable 'rmail-send-actions-rmail-buffer)
2326 (make-local-variable 'rmail-send-actions-rmail-msg-number)
2327 (setq rmail-send-actions-rmail-buffer forward-buffer)
2328 (setq rmail-send-actions-rmail-msg-number msgnum)
2329 ;; Insert after header separator--before signature if any.
2330 (goto-char (point-min))
2331 (search-forward-regexp
2332 (concat "^" (regexp-quote mail-header-separator) "$"))
2333 (forward-line 1)
2334 (insert "------- Start of forwarded message -------\n")
2335 ;; Quote lines with `- ' if they start with `-'.
2336 (let ((beg (point)) end)
2337 (setq end (point-marker))
2338 (set-marker-insertion-type end t)
2339 (insert-buffer-substring forward-buffer)
2340 (goto-char beg)
2341 (while (re-search-forward "^-" nil t)
2342 (beginning-of-line)
2343 (insert "- ")
2344 (forward-line 1))
2345 (goto-char end)
2346 (skip-chars-backward "\n")
2347 (if (< (point) end)
2348 (forward-char 1))
2349 (delete-region (point) end)
2350 (set-marker end nil))
2351 (insert "------- End of forwarded message -------\n")
2352 (push-mark))))))
2353 \f
2354 (defun rmail-resend (address &optional from comment mail-alias-file)
2355 "Resend current message to ADDRESSES.
2356 ADDRESSES should be a single address, a string consisting of several
2357 addresses separated by commas, or a list of addresses.
2358
2359 Optional FROM is the address to resend the message from, and
2360 defaults to the username of the person redistributing the message.
2361 Optional COMMENT is a string that will be inserted as a comment in the
2362 resent message.
2363 Optional ALIAS-FILE is alternate aliases file to be used by sendmail,
2364 typically for purposes of moderating a list."
2365 (interactive "sResend to: ")
2366 (require 'sendmail)
2367 (require 'mailalias)
2368 (if (not from) (setq from (user-login-name)))
2369 (let ((tembuf (generate-new-buffer " sendmail temp"))
2370 (mail-header-separator "")
2371 (case-fold-search nil)
2372 (mailbuf (current-buffer)))
2373 (unwind-protect
2374 (save-excursion
2375 ;;>> Copy message into temp buffer
2376 (set-buffer tembuf)
2377 (insert-buffer-substring mailbuf)
2378 (goto-char (point-min))
2379 ;; Delete any Sender field, since that's not specifiable.
2380 ; Only delete Sender fields in the actual header.
2381 (re-search-forward "^$" nil 'move)
2382 ; Using "while" here rather than "if" because some buggy mail
2383 ; software may have inserted multiple Sender fields.
2384 (while (re-search-backward "^Sender:" nil t)
2385 (let (beg)
2386 (setq beg (point))
2387 (forward-line 1)
2388 (while (looking-at "[ \t]")
2389 (forward-line 1))
2390 (delete-region beg (point))))
2391 ; Go back to the beginning of the buffer so the Resent- fields
2392 ; are inserted there.
2393 (goto-char (point-min))
2394 ;;>> Insert resent-from:
2395 (insert "Resent-From: " from "\n")
2396 (insert "Resent-Date: " (mail-rfc822-date) "\n")
2397 ;;>> Insert resent-to: and bcc if need be.
2398 (let ((before (point)))
2399 (if mail-self-blind
2400 (insert "Resent-Bcc: " (user-login-name) "\n"))
2401 (insert "Resent-To: " (if (stringp address)
2402 address
2403 (mapconcat 'identity address ",\n\t"))
2404 "\n")
2405 ;; Expand abbrevs in the recipients.
2406 (save-excursion
2407 (if (featurep 'mailabbrev)
2408 (let ((end (point-marker))
2409 (local-abbrev-table mail-abbrevs)
2410 (old-syntax-table (syntax-table)))
2411 (if (and (not (vectorp mail-abbrevs))
2412 (file-exists-p mail-personal-alias-file))
2413 (build-mail-abbrevs))
2414 (set-syntax-table mail-abbrev-syntax-table)
2415 (goto-char before)
2416 (while (and (< (point) end)
2417 (progn (forward-word 1)
2418 (<= (point) end)))
2419 (expand-abbrev))
2420 (set-syntax-table old-syntax-table))
2421 (expand-mail-aliases before (point)))))
2422 ;;>> Set up comment, if any.
2423 (if (and (sequencep comment) (not (zerop (length comment))))
2424 (let ((before (point))
2425 after)
2426 (insert comment)
2427 (or (eolp) (insert "\n"))
2428 (setq after (point))
2429 (goto-char before)
2430 (while (< (point) after)
2431 (insert "Resent-Comment: ")
2432 (forward-line 1))))
2433 ;; Don't expand aliases in the destination fields
2434 ;; of the original message.
2435 (let (mail-aliases)
2436 (funcall send-mail-function)))
2437 (kill-buffer tembuf))
2438 (rmail-set-attribute "resent" t rmail-current-message)))
2439 \f
2440 (defvar mail-unsent-separator
2441 (concat "^ *---+ +Unsent message follows +---+ *$\\|"
2442 "^ *---+ +Returned message +---+ *$\\|"
2443 "^Start of returned message$\\|"
2444 "^ *---+ +Original message +---+ *$\\|"
2445 "^ *--+ +begin message +--+ *$\\|"
2446 "^ *---+ +Original message follows +---+ *$\\|"
2447 "^|? *---+ +Message text follows: +---+ *|?$")
2448 "A regexp that matches the separator before the text of a failed message.")
2449
2450 (defun rmail-retry-failure ()
2451 "Edit a mail message which is based on the contents of the current message.
2452 For a message rejected by the mail system, extract the interesting headers and
2453 the body of the original message.
2454 The variable `mail-unsent-separator' should match the string that
2455 delimits the returned original message.
2456 The variable `rmail-retry-ignored-headers' is a regular expression
2457 specifying headers which should not be copied into the new message."
2458 (interactive)
2459 (require 'mail-utils)
2460 (let ((rmail-buffer (current-buffer))
2461 (msgnum rmail-current-message)
2462 bounce-start bounce-end bounce-indent resending)
2463 (save-excursion
2464 ;; Narrow down to just the quoted original message
2465 (rmail-beginning-of-message)
2466 (let ((case-fold-search t))
2467 (if (search-forward "This is a MIME-encapsulated message\n\n--" nil t)
2468 (let ((codestring
2469 (buffer-substring (progn (beginning-of-line) (point))
2470 (progn (end-of-line) (point)))))
2471 (or (re-search-forward mail-unsent-separator nil t)
2472 (error "Cannot find beginning of header in failed message"))
2473 (or (and (search-forward codestring nil t)
2474 (search-forward "\n\n" nil t))
2475 (error "Cannot find end of Mime data in failed message"))
2476 (setq bounce-start (point))
2477 (save-excursion
2478 (goto-char (point-max))
2479 (search-backward codestring)
2480 (setq bounce-end (point)))
2481 (or (search-forward "\n\n" nil t)
2482 (error "Cannot find end of header in failed message")))
2483 (or (re-search-forward mail-unsent-separator nil t)
2484 (error "Cannot parse this as a failure message"))
2485 (skip-chars-forward "\n")
2486 ;; Support a style of failure message in which the original
2487 ;; message is indented, and included within lines saying
2488 ;; `Start of returned message' and `End of returned message'.
2489 (if (looking-at " +Received:")
2490 (progn
2491 (setq bounce-start (point))
2492 (skip-chars-forward " ")
2493 (setq bounce-indent (- (current-column)))
2494 (goto-char (point-max))
2495 (re-search-backward "^End of returned message$" nil t)
2496 (setq bounce-end (point)))
2497 ;; One message contained a few random lines before the old
2498 ;; message header. The first line of the message started with
2499 ;; two hyphens. A blank line followed these random lines.
2500 ;; The same line beginning with two hyphens was possibly
2501 ;; marking the end of the message.
2502 (if (looking-at "^--")
2503 (let ((boundary (buffer-substring-no-properties
2504 (point)
2505 (progn (end-of-line) (point)))))
2506 (search-forward "\n\n")
2507 (skip-chars-forward "\n")
2508 (setq bounce-start (point))
2509 (goto-char (point-max))
2510 (search-backward (concat "\n\n" boundary) bounce-start t)
2511 (setq bounce-end (point)))
2512 (setq bounce-start (point)
2513 bounce-end (point-max)))
2514 (or (search-forward "\n\n" nil t)
2515 (error "Cannot find end of header in failed message"))
2516 ))))
2517 ;; Start sending a new message; default header fields from the original.
2518 ;; Turn off the usual actions for initializing the message body
2519 ;; because we want to get only the text from the failure message.
2520 (let ((action
2521 ;; This function will be called when the user sends the retry.
2522 ;; It will mark the bounce message as "retried".
2523 (function (lambda ()
2524 (let ((msgnum rmail-send-actions-rmail-msg-number))
2525 (save-excursion
2526 (set-buffer rmail-send-actions-rmail-buffer)
2527 (if msgnum
2528 (rmail-set-attribute "retried" t msgnum)))))))
2529 mail-signature mail-setup-hook)
2530 (if (rmail-start-mail nil nil nil nil nil rmail-buffer
2531 (list (list action)))
2532 ;; Insert original text as initial text of new draft message.
2533 ;; Bind inhibit-read-only since the header delimiter
2534 ;; of the previous message was probably read-only.
2535 (let ((inhibit-read-only t))
2536 ;; We keep the rmail buffer and message number in these
2537 ;; buffer-local vars in the sendmail buffer,
2538 ;; so that the rmail-only-expunge can relocate the message number.
2539 (make-local-variable 'rmail-send-actions-rmail-buffer)
2540 (make-local-variable 'rmail-send-actions-rmail-msg-number)
2541 (setq rmail-send-actions-rmail-buffer rmail-buffer)
2542 (setq rmail-send-actions-rmail-msg-number msgnum)
2543 (erase-buffer)
2544 (insert-buffer-substring rmail-buffer bounce-start bounce-end)
2545 (goto-char (point-min))
2546 (if bounce-indent
2547 (indent-rigidly (point-min) (point-max) bounce-indent))
2548 (rmail-clear-headers rmail-retry-ignored-headers)
2549 (rmail-clear-headers "^sender:\\|^from:\\|^return-path:")
2550 (goto-char (point-min))
2551 (save-restriction
2552 (search-forward "\n\n")
2553 (forward-line -1)
2554 (narrow-to-region (point-min) (point))
2555 (setq resending (mail-fetch-field "resent-to"))
2556 (if mail-self-blind
2557 (if resending
2558 (insert "Resent-Bcc: " (user-login-name) "\n")
2559 (insert "BCC: " (user-login-name) "\n"))))
2560 (insert mail-header-separator)
2561 (mail-position-on-field (if resending "Resent-To" "To") t)
2562 (set-buffer rmail-buffer)
2563 (rmail-beginning-of-message))))))
2564 \f
2565 (defun rmail-summary-exists ()
2566 "Non-nil iff in an RMAIL buffer and an associated summary buffer exists.
2567 In fact, the non-nil value returned is the summary buffer itself."
2568 (and rmail-summary-buffer (buffer-name rmail-summary-buffer)
2569 rmail-summary-buffer))
2570
2571 (defun rmail-summary-displayed ()
2572 "t iff in RMAIL buffer and an associated summary buffer is displayed."
2573 (and rmail-summary-buffer (get-buffer-window rmail-summary-buffer)))
2574
2575 (defvar rmail-redisplay-summary nil
2576 "*Non-nil means Rmail should show the summary when it changes.
2577 This has an effect only if a summary buffer exists.")
2578
2579 (defvar rmail-summary-window-size nil
2580 "*Non-nil means specify the height for an Rmail summary window.")
2581
2582 ;; Put the summary buffer back on the screen, if user wants that.
2583 (defun rmail-maybe-display-summary ()
2584 (let ((selected (selected-window))
2585 window)
2586 ;; If requested, make sure the summary is displayed.
2587 (and rmail-summary-buffer (buffer-name rmail-summary-buffer)
2588 rmail-redisplay-summary
2589 (if (get-buffer-window rmail-summary-buffer 0)
2590 ;; It's already in some frame; show that one.
2591 (let ((frame (window-frame
2592 (get-buffer-window rmail-summary-buffer 0))))
2593 (make-frame-visible frame)
2594 (raise-frame frame))
2595 (display-buffer rmail-summary-buffer)))
2596 ;; If requested, set the height of the summary window.
2597 (and rmail-summary-buffer (buffer-name rmail-summary-buffer)
2598 rmail-summary-window-size
2599 (setq window (get-buffer-window rmail-summary-buffer))
2600 ;; Don't try to change the size if just one window in frame.
2601 (not (eq window (frame-root-window (window-frame window))))
2602 (unwind-protect
2603 (progn
2604 (select-window window)
2605 (enlarge-window (- rmail-summary-window-size (window-height))))
2606 (select-window selected)))))
2607 \f
2608 ;;;; *** Rmail Local Fontification ***
2609
2610 (defun rmail-fontify-buffer-function ()
2611 ;; This function's symbol is bound to font-lock-fontify-buffer-function.
2612 (make-local-hook 'rmail-show-message-hook)
2613 (add-hook 'rmail-show-message-hook 'rmail-fontify-message nil t)
2614 ;; If we're already showing a message, fontify it now.
2615 (if rmail-current-message (rmail-fontify-message))
2616 ;; Prevent Font Lock mode from kicking in.
2617 (setq font-lock-fontified t))
2618
2619 (defun rmail-unfontify-buffer-function ()
2620 ;; This function's symbol is bound to font-lock-fontify-unbuffer-function.
2621 (let ((modified (buffer-modified-p))
2622 (buffer-undo-list t) (inhibit-read-only t)
2623 before-change-functions after-change-functions
2624 buffer-file-name buffer-file-truename)
2625 (save-restriction
2626 (widen)
2627 (remove-hook 'rmail-show-message-hook 'rmail-fontify-message t)
2628 (remove-text-properties (point-min) (point-max) '(rmail-fontified nil))
2629 (font-lock-default-unfontify-buffer)
2630 (and (not modified) (buffer-modified-p) (set-buffer-modified-p nil)))))
2631
2632 (defun rmail-fontify-message ()
2633 ;; Fontify the current message if it is not already fontified.
2634 (if (text-property-any (point-min) (point-max) 'rmail-fontified nil)
2635 (let ((modified (buffer-modified-p))
2636 (buffer-undo-list t) (inhibit-read-only t)
2637 before-change-functions after-change-functions
2638 buffer-file-name buffer-file-truename)
2639 (save-excursion
2640 (save-match-data
2641 (add-text-properties (point-min) (point-max) '(rmail-fontified t))
2642 (font-lock-fontify-region (point-min) (point-max))
2643 (and (not modified) (buffer-modified-p) (set-buffer-modified-p nil)))))))
2644 \f
2645 ;;;; *** Rmail Specify Inbox Files ***
2646
2647 (autoload 'set-rmail-inbox-list "rmailmsc"
2648 "Set the inbox list of the current RMAIL file to FILE-NAME.
2649 This may be a list of file names separated by commas.
2650 If FILE-NAME is empty, remove any inbox list."
2651 t)
2652
2653 ;;;; *** Rmail Commands for Labels ***
2654
2655 (autoload 'rmail-add-label "rmailkwd"
2656 "Add LABEL to labels associated with current RMAIL message.
2657 Completion is performed over known labels when reading."
2658 t)
2659
2660 (autoload 'rmail-kill-label "rmailkwd"
2661 "Remove LABEL from labels associated with current RMAIL message.
2662 Completion is performed over known labels when reading."
2663 t)
2664
2665 (autoload 'rmail-next-labeled-message "rmailkwd"
2666 "Show next message with LABEL. Defaults to last label used.
2667 With prefix argument N moves forward N messages with this label."
2668 t)
2669
2670 (autoload 'rmail-previous-labeled-message "rmailkwd"
2671 "Show previous message with LABEL. Defaults to last label used.
2672 With prefix argument N moves backward N messages with this label."
2673 t)
2674
2675 (autoload 'rmail-read-label "rmailkwd"
2676 "PROMPT and read with completion an Rmail message label."
2677 t)
2678
2679 ;;;; *** Rmail Edit Mode ***
2680
2681 (autoload 'rmail-edit-current-message "rmailedit"
2682 "Edit the contents of the current message"
2683 t)
2684
2685 ;;;; *** Rmail Sorting ***
2686
2687 (autoload 'rmail-sort-by-date "rmailsort"
2688 "Sort messages of current Rmail file by date.
2689 If prefix argument REVERSE is non-nil, sort them in reverse order." t)
2690
2691 (autoload 'rmail-sort-by-subject "rmailsort"
2692 "Sort messages of current Rmail file by subject.
2693 If prefix argument REVERSE is non-nil, sort them in reverse order." t)
2694
2695 (autoload 'rmail-sort-by-author "rmailsort"
2696 "Sort messages of current Rmail file by author.
2697 If prefix argument REVERSE is non-nil, sort them in reverse order." t)
2698
2699 (autoload 'rmail-sort-by-recipient "rmailsort"
2700 "Sort messages of current Rmail file by recipient.
2701 If prefix argument REVERSE is non-nil, sort them in reverse order." t)
2702
2703 (autoload 'rmail-sort-by-correspondent "rmailsort"
2704 "Sort messages of current Rmail file by other correspondent.
2705 If prefix argument REVERSE is non-nil, sort them in reverse order." t)
2706
2707 (autoload 'rmail-sort-by-lines "rmailsort"
2708 "Sort messages of current Rmail file by number of lines.
2709 If prefix argument REVERSE is non-nil, sort them in reverse order." t)
2710
2711 (autoload 'rmail-sort-by-keywords "rmailsort"
2712 "Sort messages of current Rmail file by labels.
2713 If prefix argument REVERSE is non-nil, sort them in reverse order.
2714 KEYWORDS is a comma-separated list of labels." t)
2715 \f
2716 ;;;; *** Rmail Summary Mode ***
2717
2718 (autoload 'rmail-summary "rmailsum"
2719 "Display a summary of all messages, one line per message."
2720 t)
2721
2722 (autoload 'rmail-summary-by-labels "rmailsum"
2723 "Display a summary of all messages with one or more LABELS.
2724 LABELS should be a string containing the desired labels, separated by commas."
2725 t)
2726
2727 (autoload 'rmail-summary-by-recipients "rmailsum"
2728 "Display a summary of all messages with the given RECIPIENTS.
2729 Normally checks the To, From and Cc fields of headers; but if PRIMARY-ONLY
2730 is non-nil (prefix arg given), only look in the To and From fields.
2731 RECIPIENTS is a string of regexps separated by commas."
2732 t)
2733
2734 (autoload 'rmail-summary-by-regexp "rmailsum"
2735 "Display a summary of all messages according to regexp REGEXP.
2736 If the regular expression is found in the header of the message
2737 \(including in the date and other lines, as well as the subject line),
2738 Emacs will list the header line in the RMAIL-summary."
2739 t)
2740
2741 (autoload 'rmail-summary-by-topic "rmailsum"
2742 "Display a summary of all messages with the given SUBJECT.
2743 Normally checks the Subject field of headers;
2744 but if WHOLE-MESSAGE is non-nil (prefix arg given),
2745 look in the whole message.
2746 SUBJECT is a string of regexps separated by commas."
2747 t)
2748
2749 (autoload 'rmail-summary-by-senders "rmailsum"
2750 "Display a summary of all messages with the given SENDERS.
2751 SENDERS is a string of names separated by commas."
2752 t)
2753 \f
2754 ;;;; *** Rmail output messages to files ***
2755
2756 (autoload 'rmail-output-to-rmail-file "rmailout"
2757 "Append the current message to an Rmail file named FILE-NAME.
2758 If the file does not exist, ask if it should be created.
2759 If file is being visited, the message is appended to the Emacs
2760 buffer visiting that file."
2761 t)
2762
2763 (autoload 'rmail-output "rmailout"
2764 "Append this message to Unix mail file named FILE-NAME."
2765 t)
2766
2767 ;;;; *** Rmail undigestification ***
2768
2769 (autoload 'undigestify-rmail-message "undigest"
2770 "Break up a digest message into its constituent messages.
2771 Leaves original message, deleted, before the undigestified messages."
2772 t)
2773
2774 (autoload 'unforward-rmail-message "undigest"
2775 "Extract a forwarded message from the containing message.
2776 This puts the forwarded message into a separate rmail message
2777 following the containing message."
2778 t)
2779
2780 (provide 'rmail)
2781
2782 ;;; rmail.el ends here