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