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