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