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