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