(rmail-autodetect): Add .exe extension to movemail on Windows.
[emacs.git] / lisp / mail / rmail.el
blob4869f95689ec8d728366b93931715be79644ce5b
1 ;;; rmail.el --- main code of "RMAIL" mail reader for Emacs
3 ;; Copyright (C) 1985, 1986, 1987, 1988, 1993, 1994, 1995, 1996, 1997, 1998,
4 ;; 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
5 ;; Free Software Foundation, Inc.
7 ;; Maintainer: FSF
8 ;; Keywords: mail
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 3, or (at your option)
15 ;; any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
27 ;;; Commentary:
29 ;;; Code:
31 ;; Souped up by shane@mit-ajax based on ideas of rlk@athena.mit.edu
32 ;; New features include attribute and keyword support, message
33 ;; selection by dispatch table, summary by attributes and keywords,
34 ;; expunging by dispatch table, sticky options for file commands.
36 ;; Extended by Bob Weiner of Motorola
37 ;; New features include: rmail and rmail-summary buffers remain
38 ;; synchronized and key bindings basically operate the same way in both
39 ;; buffers, summary by topic or by regular expression, rmail-reply-prefix
40 ;; variable, and a bury rmail buffer (wipe) command.
43 (require 'mail-utils)
44 (eval-when-compile (require 'mule-util)) ; for detect-coding-with-priority
46 (defvar deleted-head)
47 (defvar font-lock-fontified)
48 (defvar mail-abbrev-syntax-table)
49 (defvar mail-abbrevs)
50 (defvar messages-head)
51 (defvar rmail-use-spam-filter)
52 (defvar rsf-beep)
53 (defvar rsf-sleep-after-message)
54 (defvar total-messages)
55 (defvar tool-bar-map)
57 ; These variables now declared in paths.el.
58 ;(defvar rmail-spool-directory "/usr/spool/mail/"
59 ; "This is the name of the directory used by the system mailer for\n\
60 ;delivering new mail. Its name should end with a slash.")
61 ;(defvar rmail-file-name
62 ; (expand-file-name "~/RMAIL")
63 ; "")
65 (defgroup rmail nil
66 "Mail reader for Emacs."
67 :group 'mail)
69 (defgroup rmail-retrieve nil
70 "Rmail retrieval options."
71 :prefix "rmail-"
72 :group 'rmail)
74 (defgroup rmail-files nil
75 "Rmail files."
76 :prefix "rmail-"
77 :group 'rmail)
79 (defgroup rmail-headers nil
80 "Rmail header options."
81 :prefix "rmail-"
82 :group 'rmail)
84 (defgroup rmail-reply nil
85 "Rmail reply options."
86 :prefix "rmail-"
87 :group 'rmail)
89 (defgroup rmail-summary nil
90 "Rmail summary options."
91 :prefix "rmail-"
92 :prefix "rmail-summary-"
93 :group 'rmail)
95 (defgroup rmail-output nil
96 "Output message to a file."
97 :prefix "rmail-output-"
98 :prefix "rmail-"
99 :group 'rmail)
101 (defgroup rmail-edit nil
102 "Rmail editing."
103 :prefix "rmail-edit-"
104 :group 'rmail)
106 (defgroup rmail-obsolete nil
107 "Rmail obsolete customization variables."
108 :group 'rmail)
110 (defcustom rmail-movemail-program nil
111 "If non-nil, the file name of the `movemail' program."
112 :group 'rmail-retrieve
113 :type '(choice (const nil) string))
115 (defcustom rmail-pop-password nil
116 "*Password to use when reading mail from POP server.
117 Please use `rmail-remote-password' instead."
118 :type '(choice (string :tag "Password")
119 (const :tag "Not Required" nil))
120 :group 'rmail-obsolete)
122 (defcustom rmail-pop-password-required nil
123 "*Non-nil if a password is required when reading mail from a POP server.
124 Please use rmail-remote-password-required instead."
125 :type 'boolean
126 :group 'rmail-obsolete)
128 (defcustom rmail-remote-password nil
129 "*Password to use when reading mail from a remote server.
130 This setting is ignored for mailboxes whose URL already contains a password."
131 :type '(choice (string :tag "Password")
132 (const :tag "Not Required" nil))
133 :set-after '(rmail-pop-password)
134 :set #'(lambda (symbol value)
135 (set-default symbol
136 (if (and (not value)
137 (boundp 'rmail-pop-password)
138 rmail-pop-password)
139 rmail-pop-password
140 value))
141 (setq rmail-pop-password nil))
142 :group 'rmail-retrieve
143 :version "22.1")
145 (defcustom rmail-remote-password-required nil
146 "*Non-nil if a password is required when reading mail from a remote server."
147 :type 'boolean
148 :set-after '(rmail-pop-password-required)
149 :set #'(lambda (symbol value)
150 (set-default symbol
151 (if (and (not value)
152 (boundp 'rmail-pop-password-required)
153 rmail-pop-password-required)
154 rmail-pop-password-required
155 value))
156 (setq rmail-pop-password-required nil))
157 :group 'rmail-retrieve
158 :version "22.1")
160 (defcustom rmail-movemail-flags nil
161 "*List of flags to pass to movemail.
162 Most commonly used to specify `-g' to enable GSS-API authentication
163 or `-k' to enable Kerberos authentication."
164 :type '(repeat string)
165 :group 'rmail-retrieve
166 :version "20.3")
168 (defvar rmail-remote-password-error "invalid usercode or password\\|
169 unknown user name or bad password\\|Authentication failed\\|MU_ERR_AUTH_FAILURE"
170 "Regular expression matching incorrect-password POP or IMAP server error
171 messages.
172 If you get an incorrect-password error that this expression does not match,
173 please report it with \\[report-emacs-bug].")
175 (defvar rmail-encoded-remote-password nil)
177 (defcustom rmail-preserve-inbox nil
178 "*Non-nil means leave incoming mail in the user's inbox--don't delete it."
179 :type 'boolean
180 :group 'rmail-retrieve)
182 (defcustom rmail-movemail-search-path nil
183 "*List of directories to search for movemail (in addition to `exec-path')."
184 :group 'rmail-retrieve
185 :type '(repeat (directory)))
187 (defun rmail-probe (prog)
188 "Determine what flavor of movemail PROG is.
189 We do this by executing it with `--version' and analyzing its output."
190 (with-temp-buffer
191 (let ((tbuf (current-buffer)))
192 (buffer-disable-undo tbuf)
193 (call-process prog nil tbuf nil "--version")
194 (if (not (buffer-modified-p tbuf))
195 ;; Should not happen...
197 (goto-char (point-min))
198 (cond
199 ((looking-at ".*movemail: invalid option")
200 'emacs) ;; Possibly...
201 ((looking-at "movemail (GNU Mailutils .*)")
202 'mailutils)
204 ;; FIXME:
205 'emacs))))))
207 (defun rmail-autodetect ()
208 "Determine and return the file name of the `movemail' program.
209 If `rmail-movemail-program' is non-nil, use it.
210 Otherwise, look for `movemail' in the directories in
211 `rmail-movemail-search-path', those in `exec-path', and `exec-directory'."
212 (if rmail-movemail-program
213 (rmail-probe rmail-movemail-program)
214 (catch 'scan
215 (dolist (dir (append rmail-movemail-search-path exec-path
216 (list exec-directory)))
217 (when (and dir (file-accessible-directory-p dir))
218 ;; Previously, this didn't have to work on Windows, because
219 ;; rmail-insert-inbox-text before r1.439 fell back to using
220 ;; (expand-file-name "movemail" exec-directory) and just
221 ;; assuming it would work.
222 ;; http://lists.gnu.org/archive/html/bug-gnu-emacs/2008-02/msg00087.html
223 (let ((progname (expand-file-name
224 (concat "movemail"
225 (if (memq system-type '(ms-dos windows-nt))
226 ".exe")) dir)))
227 (when (and (not (file-directory-p progname))
228 (file-executable-p progname))
229 (let ((x (rmail-probe progname)))
230 (when x
231 (setq rmail-movemail-program progname)
232 (throw 'scan x))))))))))
234 (defvar rmail-movemail-variant-in-use nil
235 "The movemail variant currently in use. Known variants are:
237 `emacs' Means any implementation, compatible with the native Emacs one.
238 This is the default;
239 `mailutils' Means GNU mailutils implementation, capable of handling full
240 mail URLs as the source mailbox.")
242 ;;;###autoload
243 (defun rmail-movemail-variant-p (&rest variants)
244 "Return t if the current movemail variant is any of VARIANTS.
245 Currently known variants are 'emacs and 'mailutils."
246 (when (not rmail-movemail-variant-in-use)
247 ;; Autodetect
248 (setq rmail-movemail-variant-in-use (rmail-autodetect)))
249 (not (null (member rmail-movemail-variant-in-use variants))))
251 ;; Call for effect, to set rmail-movemail-program (if not set by the
252 ;; user), and rmail-movemail-variant-in-use. Used by various functions.
253 ;; I'm not sure if M-x rmail is the only entry point to this package.
254 ;; If so, this can be moved there.
255 (rmail-movemail-variant-p)
257 ;;;###autoload
258 (defcustom rmail-dont-reply-to-names nil "\
259 *A regexp specifying addresses to prune from a reply message.
260 A value of nil means exclude your own email address as an address
261 plus whatever is specified by `rmail-default-dont-reply-to-names'."
262 :type '(choice regexp (const :tag "Your Name" nil))
263 :group 'rmail-reply)
265 ;;;###autoload
266 (defvar rmail-default-dont-reply-to-names "\\`info-" "\
267 A regular expression specifying part of the default value of the
268 variable `rmail-dont-reply-to-names', for when the user does not set
269 `rmail-dont-reply-to-names' explicitly. (The other part of the default
270 value is the user's email address and name.)
271 It is useful to set this variable in the site customization file.")
273 ;;;###autoload
274 (defcustom rmail-ignored-headers
275 (concat "^via:\\|^mail-from:\\|^origin:\\|^references:\\|^sender:"
276 "\\|^status:\\|^received:\\|^x400-originator:\\|^x400-recipients:"
277 "\\|^x400-received:\\|^x400-mts-identifier:\\|^x400-content-type:"
278 "\\|^\\(resent-\\|\\)message-id:\\|^summary-line:\\|^resent-date:"
279 "\\|^nntp-posting-host:\\|^path:\\|^x-char.*:\\|^x-face:\\|^face:"
280 "\\|^x-mailer:\\|^delivered-to:\\|^lines:"
281 "\\|^content-transfer-encoding:\\|^x-coding-system:"
282 "\\|^return-path:\\|^errors-to:\\|^return-receipt-to:"
283 "\\|^precedence:\\|^list-help:\\|^list-post:\\|^list-subscribe:"
284 "\\|^list-id:\\|^list-unsubscribe:\\|^list-archive:"
285 "\\|^content-length:\\|^nntp-posting-date:\\|^user-agent"
286 "\\|^importance:\\|^envelope-to:\\|^delivery-date\\|^openpgp:"
287 "\\|^mbox-line:\\|^cancel-lock:\\|^DomainKey-Signature:"
288 "\\|^resent-face:\\|^resent-x.*:\\|^resent-organization:\\|^resent-openpgp:"
290 "\\|^x-.*:")
291 "*Regexp to match header fields that Rmail should normally hide.
292 \(See also `rmail-nonignored-headers', which overrides this regexp.)
293 This variable is used for reformatting the message header,
294 which normally happens once for each message,
295 when you view the message for the first time in Rmail.
296 To make a change in this variable take effect
297 for a message that you have already viewed,
298 go to that message and type \\[rmail-toggle-header] twice."
299 :type 'regexp
300 :group 'rmail-headers)
302 (defcustom rmail-nonignored-headers "^x-spam-status:"
303 "*Regexp to match X header fields that Rmail should show.
304 This regexp overrides `rmail-ignored-headers'; if both this regexp
305 and that one match a certain header field, Rmail shows the field.
307 This variable is used for reformatting the message header,
308 which normally happens once for each message,
309 when you view the message for the first time in Rmail.
310 To make a change in this variable take effect
311 for a message that you have already viewed,
312 go to that message and type \\[rmail-toggle-header] twice."
313 :type 'regexp
314 :group 'rmail-headers)
316 ;;;###autoload
317 (defcustom rmail-displayed-headers nil
318 "*Regexp to match Header fields that Rmail should display.
319 If nil, display all header fields except those matched by
320 `rmail-ignored-headers'."
321 :type '(choice regexp (const :tag "All"))
322 :group 'rmail-headers)
324 ;;;###autoload
325 (defcustom rmail-retry-ignored-headers "^x-authentication-warning:" "\
326 *Headers that should be stripped when retrying a failed message."
327 :type '(choice regexp (const nil :tag "None"))
328 :group 'rmail-headers)
330 ;;;###autoload
331 (defcustom rmail-highlighted-headers "^From:\\|^Subject:" "\
332 *Regexp to match Header fields that Rmail should normally highlight.
333 A value of nil means don't highlight.
334 See also `rmail-highlight-face'."
335 :type 'regexp
336 :group 'rmail-headers)
338 (defface rmail-highlight
339 '((t (:inherit highlight)))
340 "Face to use for highlighting the most important header fields."
341 :group 'rmail-headers
342 :version "22.1")
344 ;;;###autoload
345 (defcustom rmail-highlight-face 'rmail-highlight "\
346 *Face used by Rmail for highlighting headers."
347 :type '(choice (const :tag "Default" nil)
348 face)
349 :group 'rmail-headers)
351 ;;;###autoload
352 (defcustom rmail-delete-after-output nil "\
353 *Non-nil means automatically delete a message that is copied to a file."
354 :type 'boolean
355 :group 'rmail-files)
357 ;;;###autoload
358 (defcustom rmail-primary-inbox-list nil "\
359 *List of files which are inboxes for user's primary mail file `~/RMAIL'.
360 nil means the default, which is (\"/usr/spool/mail/$USER\")
361 \(the name varies depending on the operating system,
362 and the value of the environment variable MAIL overrides it)."
363 ;; Don't use backquote here, because we don't want to need it
364 ;; at load time.
365 :type (list 'choice '(const :tag "Default" nil)
366 (list 'repeat ':value (list (or (getenv "MAIL")
367 (concat "/var/spool/mail/"
368 (getenv "USER"))))
369 'file))
370 :group 'rmail-retrieve
371 :group 'rmail-files)
373 ;;;###autoload
374 (defcustom rmail-mail-new-frame nil
375 "*Non-nil means Rmail makes a new frame for composing outgoing mail.
376 This is handy if you want to preserve the window configuration of
377 the frame where you have the RMAIL buffer displayed."
378 :type 'boolean
379 :group 'rmail-reply)
381 ;;;###autoload
382 (defcustom rmail-secondary-file-directory "~/"
383 "*Directory for additional secondary Rmail files."
384 :type 'directory
385 :group 'rmail-files)
386 ;;;###autoload
387 (defcustom rmail-secondary-file-regexp "\\.xmail$"
388 "*Regexp for which files are secondary Rmail files."
389 :type 'regexp
390 :group 'rmail-files)
392 ;;;###autoload
393 (defcustom rmail-confirm-expunge 'y-or-n-p
394 "*Whether and how to ask for confirmation before expunging deleted messages."
395 :type '(choice (const :tag "No confirmation" nil)
396 (const :tag "Confirm with y-or-n-p" y-or-n-p)
397 (const :tag "Confirm with yes-or-no-p" yes-or-no-p))
398 :version "21.1"
399 :group 'rmail-files)
401 ;;;###autoload
402 (defvar rmail-mode-hook nil
403 "List of functions to call when Rmail is invoked.")
405 ;;;###autoload
406 (defvar rmail-get-new-mail-hook nil
407 "List of functions to call when Rmail has retrieved new mail.")
409 ;;;###autoload
410 (defcustom rmail-show-message-hook nil
411 "List of functions to call when Rmail displays a message."
412 :type 'hook
413 :options '(goto-address)
414 :group 'rmail)
416 ;;;###autoload
417 (defvar rmail-quit-hook nil
418 "List of functions to call when quitting out of Rmail.")
420 ;;;###autoload
421 (defvar rmail-delete-message-hook nil
422 "List of functions to call when Rmail deletes a message.
423 When the hooks are called, the message has been marked deleted but is
424 still the current message in the Rmail buffer.")
426 ;; These may be altered by site-init.el to match the format of mmdf files
427 ;; delimiting used on a given host (delim1 and delim2 from the config
428 ;; files).
430 (defvar rmail-mmdf-delim1 "^\001\001\001\001\n"
431 "Regexp marking the start of an mmdf message.")
432 (defvar rmail-mmdf-delim2 "^\001\001\001\001\n"
433 "Regexp marking the end of an mmdf message.")
435 (defcustom rmail-message-filter nil
436 "If non-nil, a filter function for new messages in RMAIL.
437 Called with region narrowed to the message, including headers,
438 before obeying `rmail-ignored-headers'."
439 :group 'rmail-headers
440 :type '(choice (const nil) function))
442 (defcustom rmail-automatic-folder-directives nil
443 "List of directives specifying where to put a message.
444 Each element of the list is of the form:
446 (FOLDERNAME FIELD REGEXP [ FIELD REGEXP ] ... )
448 Where FOLDERNAME is the name of a BABYL format folder to put the
449 message. If any of the field regexp's are nil, then it is ignored.
451 If FOLDERNAME is \"/dev/null\", it is deleted.
452 If FOLDERNAME is nil then it is deleted, and skipped.
454 FIELD is the plain text name of a field in the message, such as
455 \"subject\" or \"from\". A FIELD of \"to\" will automatically include
456 all text from the \"cc\" field as well.
458 REGEXP is an expression to match in the preceeding specified FIELD.
459 FIELD/REGEXP pairs continue in the list.
461 examples:
462 (\"/dev/null\" \"from\" \"@spam.com\") ; delete all mail from spam.com
463 (\"RMS\" \"from\" \"rms@\") ; save all mail from RMS."
464 :group 'rmail
465 :version "21.1"
466 :type '(repeat (sexp :tag "Directive")))
468 (defvar rmail-reply-prefix "Re: "
469 "String to prepend to Subject line when replying to a message.")
471 ;; Some mailers use "Re(2):" or "Re^2:" or "Re: Re:" or "Re[2]:".
472 ;; This pattern should catch all the common variants.
473 ;; rms: I deleted the change to delete tags in square brackets
474 ;; because they mess up RT tags.
475 (defvar rmail-reply-regexp "\\`\\(Re\\(([0-9]+)\\|\\[[0-9]+\\]\\|\\^[0-9]+\\)?: *\\)*"
476 "Regexp to delete from Subject line before inserting `rmail-reply-prefix'.")
478 (defcustom rmail-display-summary nil
479 "*If non-nil, Rmail always displays the summary buffer."
480 :group 'rmail-summary
481 :type 'boolean)
483 (defvar rmail-inbox-list nil)
484 (put 'rmail-inbox-list 'permanent-local t)
486 (defvar rmail-keywords nil)
487 (put 'rmail-keywords 'permanent-local t)
489 (defvar rmail-buffer nil
490 "The RMAIL buffer related to the current buffer.
491 In an RMAIL buffer, this holds the RMAIL buffer itself.
492 In a summary buffer, this holds the RMAIL buffer it is a summary for.")
493 (put 'rmail-buffer 'permanent-local t)
495 ;; Message counters and markers. Deleted flags.
497 (defvar rmail-current-message nil)
498 (put 'rmail-current-message 'permanent-local t)
500 (defvar rmail-total-messages nil)
501 (put 'rmail-total-messages 'permanent-local t)
503 (defvar rmail-message-vector nil)
504 (put 'rmail-message-vector 'permanent-local t)
506 (defvar rmail-deleted-vector nil)
507 (put 'rmail-deleted-vector 'permanent-local t)
509 (defvar rmail-msgref-vector nil
510 "In an Rmail buffer, a vector whose Nth element is a list (N).
511 When expunging renumbers messages, these lists are modified
512 by substituting the new message number into the existing list.")
513 (put 'rmail-msgref-vector 'permanent-local t)
515 (defvar rmail-overlay-list nil)
516 (put 'rmail-overlay-list 'permanent-local t)
518 ;; These are used by autoloaded rmail-summary.
520 (defvar rmail-summary-buffer nil)
521 (put 'rmail-summary-buffer 'permanent-local t)
522 (defvar rmail-summary-vector nil)
523 (put 'rmail-summary-vector 'permanent-local t)
525 (defvar rmail-view-buffer nil
526 "Buffer which holds RMAIL message for MIME displaying.")
527 (put 'rmail-view-buffer 'permanent-local t)
529 ;; `Sticky' default variables.
531 ;; Last individual label specified to a or k.
532 (defvar rmail-last-label nil)
533 (put 'rmail-last-label 'permanent-local t)
535 ;; Last set of values specified to C-M-n, C-M-p, C-M-s or C-M-l.
536 (defvar rmail-last-multi-labels nil)
538 (defvar rmail-last-regexp nil)
539 (put 'rmail-last-regexp 'permanent-local t)
541 (defcustom rmail-default-file "~/xmail"
542 "*Default file name for \\[rmail-output]."
543 :type 'file
544 :group 'rmail-files)
545 (defcustom rmail-default-rmail-file "~/XMAIL"
546 "*Default file name for \\[rmail-output-to-rmail-file]."
547 :type 'file
548 :group 'rmail-files)
549 (defcustom rmail-default-body-file "~/mailout"
550 "*Default file name for \\[rmail-output-body-to-file]."
551 :type 'file
552 :group 'rmail-files
553 :version "20.3")
555 ;; Mule and MIME related variables.
557 ;;;###autoload
558 (defvar rmail-file-coding-system nil
559 "Coding system used in RMAIL file.
561 This is set to nil by default.")
563 ;;;###autoload
564 (defcustom rmail-enable-mime nil
565 "*If non-nil, RMAIL uses MIME feature.
566 If the value is t, RMAIL automatically shows MIME decoded message.
567 If the value is neither t nor nil, RMAIL does not show MIME decoded message
568 until a user explicitly requires it.
570 Even if the value is non-nil, you can't use MIME feature
571 if the feature specified by `rmail-mime-feature' is not available
572 in your session."
573 :type '(choice (const :tag "on" t)
574 (const :tag "off" nil)
575 (other :tag "when asked" ask))
576 :group 'rmail)
578 (defvar rmail-enable-mime-composing nil
579 "*If non-nil, RMAIL uses `rmail-insert-mime-forwarded-message-function' to forward.")
581 ;;;###autoload
582 (defvar rmail-show-mime-function nil
583 "Function to show MIME decoded message of RMAIL file.
584 This function is called when `rmail-enable-mime' is non-nil.
585 It is called with no argument.")
587 ;;;###autoload
588 (defvar rmail-insert-mime-forwarded-message-function nil
589 "Function to insert a message in MIME format so it can be forwarded.
590 This function is called if `rmail-enable-mime' or
591 `rmail-enable-mime-composing' is non-nil.
592 It is called with one argument FORWARD-BUFFER, which is a
593 buffer containing the message to forward. The current buffer
594 is the outgoing mail buffer.")
596 ;;;###autoload
597 (defvar rmail-insert-mime-resent-message-function nil
598 "Function to insert a message in MIME format so it can be resent.
599 This function is called if `rmail-enable-mime' is non-nil.
600 It is called with one argument FORWARD-BUFFER, which is a
601 buffer containing the message to forward. The current buffer
602 is the outgoing mail buffer.")
604 ;;;###autoload
605 (defvar rmail-search-mime-message-function nil
606 "Function to check if a regexp matches a MIME message.
607 This function is called if `rmail-enable-mime' is non-nil.
608 It is called with two arguments MSG and REGEXP, where
609 MSG is the message number, REGEXP is the regular expression.")
611 ;;;###autoload
612 (defvar rmail-search-mime-header-function nil
613 "Function to check if a regexp matches a header of MIME message.
614 This function is called if `rmail-enable-mime' is non-nil.
615 It is called with three arguments MSG, REGEXP, and LIMIT, where
616 MSG is the message number,
617 REGEXP is the regular expression,
618 LIMIT is the position specifying the end of header.")
620 ;;;###autoload
621 (defvar rmail-mime-feature 'rmail-mime
622 "Feature to require to load MIME support in Rmail.
623 When starting Rmail, if `rmail-enable-mime' is non-nil,
624 this feature is required with `require'.
626 The default value is `rmail-mime'. This feature is provided by
627 the rmail-mime package available at <http://www.m17n.org/rmail-mime/>.")
629 ;;;###autoload
630 (defvar rmail-decode-mime-charset t
631 "*Non-nil means a message is decoded by MIME's charset specification.
632 If this variable is nil, or the message has not MIME specification,
633 the message is decoded as normal way.
635 If the variable `rmail-enable-mime' is non-nil, this variables is
636 ignored, and all the decoding work is done by a feature specified by
637 the variable `rmail-mime-feature'.")
639 ;;;###autoload
640 (defvar rmail-mime-charset-pattern
641 (concat "^content-type:[ \t]*text/plain;"
642 "\\(?:[ \t\n]*\\(?:format\\|delsp\\)=\"?[-a-z0-9]+\"?;\\)*"
643 "[ \t\n]*charset=\"?\\([^ \t\n\";]+\\)\"?")
644 "Regexp to match MIME-charset specification in a header of message.
645 The first parenthesized expression should match the MIME-charset name.")
648 ;;; Regexp matching the delimiter of messages in UNIX mail format
649 ;;; (UNIX From lines), minus the initial ^. Note that if you change
650 ;;; this expression, you must change the code in rmail-nuke-pinhead-header
651 ;;; that knows the exact ordering of the \\( \\) subexpressions.
652 (defvar rmail-unix-mail-delimiter
653 (let ((time-zone-regexp
654 (concat "\\([A-Z]?[A-Z]?[A-Z][A-Z]\\( DST\\)?"
655 "\\|[-+]?[0-9][0-9][0-9][0-9]"
656 "\\|"
657 "\\) *")))
658 (concat
659 "From "
661 ;; Many things can happen to an RFC 822 mailbox before it is put into
662 ;; a `From' line. The leading phrase can be stripped, e.g.
663 ;; `Joe <@w.x:joe@y.z>' -> `<@w.x:joe@y.z>'. The <> can be stripped, e.g.
664 ;; `<@x.y:joe@y.z>' -> `@x.y:joe@y.z'. Everything starting with a CRLF
665 ;; can be removed, e.g.
666 ;; From: joe@y.z (Joe K
667 ;; User)
668 ;; can yield `From joe@y.z (Joe K Fri Mar 22 08:11:15 1996', and
669 ;; From: Joe User
670 ;; <joe@y.z>
671 ;; can yield `From Joe User Fri Mar 22 08:11:15 1996'.
672 ;; The mailbox can be removed or be replaced by white space, e.g.
673 ;; From: "Joe User"{space}{tab}
674 ;; <joe@y.z>
675 ;; can yield `From {space}{tab} Fri Mar 22 08:11:15 1996',
676 ;; where {space} and {tab} represent the Ascii space and tab characters.
677 ;; We want to match the results of any of these manglings.
678 ;; The following regexp rejects names whose first characters are
679 ;; obviously bogus, but after that anything goes.
680 "\\([^\0-\b\n-\r\^?].*\\)? "
682 ;; The time the message was sent.
683 "\\([^\0-\r \^?]+\\) +" ; day of the week
684 "\\([^\0-\r \^?]+\\) +" ; month
685 "\\([0-3]?[0-9]\\) +" ; day of month
686 "\\([0-2][0-9]:[0-5][0-9]\\(:[0-6][0-9]\\)?\\) *" ; time of day
688 ;; Perhaps a time zone, specified by an abbreviation, or by a
689 ;; numeric offset.
690 time-zone-regexp
692 ;; The year.
693 " \\([0-9][0-9]+\\) *"
695 ;; On some systems the time zone can appear after the year, too.
696 time-zone-regexp
698 ;; Old uucp cruft.
699 "\\(remote from .*\\)?"
701 "\n"))
702 nil)
704 (defvar rmail-font-lock-keywords
705 ;; These are all matched case-insensitively.
706 (eval-when-compile
707 (let* ((cite-chars "[>|}]")
708 (cite-prefix "a-z")
709 (cite-suffix (concat cite-prefix "0-9_.@-`'\"")))
710 (list '("^\\(From\\|Sender\\|Resent-From\\):"
711 . font-lock-function-name-face)
712 '("^Reply-To:.*$" . font-lock-function-name-face)
713 '("^Subject:" . font-lock-comment-face)
714 '("^X-Spam-Status:" . font-lock-keyword-face)
715 '("^\\(To\\|Apparently-To\\|Cc\\|Newsgroups\\):"
716 . font-lock-keyword-face)
717 ;; Use MATCH-ANCHORED to effectively anchor the regexp left side.
718 `(,cite-chars
719 (,(concat "\\=[ \t]*"
720 "\\(\\(\\([" cite-prefix "]+[" cite-suffix "]*\\)?"
721 "\\(" cite-chars "[ \t]*\\)\\)+\\)"
722 "\\(.*\\)")
723 (beginning-of-line) (end-of-line)
724 (1 font-lock-comment-delimiter-face nil t)
725 (5 font-lock-comment-face nil t)))
726 '("^\\(X-[a-z0-9-]+\\|In-reply-to\\|Date\\):.*\\(\n[ \t]+.*\\)*$"
727 . font-lock-string-face))))
728 "Additional expressions to highlight in Rmail mode.")
730 ;; Perform BODY in the summary buffer
731 ;; in such a way that its cursor is properly updated in its own window.
732 (defmacro rmail-select-summary (&rest body)
733 `(let ((total rmail-total-messages))
734 (if (rmail-summary-displayed)
735 (let ((window (selected-window)))
736 (save-excursion
737 (unwind-protect
738 (progn
739 (pop-to-buffer rmail-summary-buffer)
740 ;; rmail-total-messages is a buffer-local var
741 ;; in the rmail buffer.
742 ;; This way we make it available for the body
743 ;; even tho the rmail buffer is not current.
744 (let ((rmail-total-messages total))
745 ,@body))
746 (select-window window))))
747 (save-excursion
748 (set-buffer rmail-summary-buffer)
749 (let ((rmail-total-messages total))
750 ,@body)))
751 (rmail-maybe-display-summary)))
753 ;;;; *** Rmail Mode ***
755 ;; This variable is dynamically bound. The defvar is here to placate
756 ;; the byte compiler.
758 (defvar rmail-enable-multibyte nil)
761 (defun rmail-require-mime-maybe ()
762 "Require `rmail-mime-feature' if that is non-nil.
763 Signal an error and set `rmail-mime-feature' to nil if the feature
764 isn't provided."
765 (when rmail-enable-mime
766 (condition-case err
767 (require rmail-mime-feature)
768 (error
769 (display-warning
770 :warning
771 (format "Although MIME support is requested
772 by setting `rmail-enable-mime' to non-nil, the required feature
773 `%s' (the value of `rmail-mime-feature')
774 is not available in the current session.
775 So, the MIME support is turned off for the moment."
776 rmail-mime-feature))
777 (setq rmail-enable-mime nil)))))
780 ;;;###autoload
781 (defun rmail (&optional file-name-arg)
782 "Read and edit incoming mail.
783 Moves messages into file named by `rmail-file-name' (a babyl format file)
784 and edits that file in RMAIL Mode.
785 Type \\[describe-mode] once editing that file, for a list of RMAIL commands.
787 May be called with file name as argument; then performs rmail editing on
788 that file, but does not copy any new mail into the file.
789 Interactively, if you supply a prefix argument, then you
790 have a chance to specify a file name with the minibuffer.
792 If `rmail-display-summary' is non-nil, make a summary for this RMAIL file."
793 (interactive (if current-prefix-arg
794 (list (read-file-name "Run rmail on RMAIL file: "))))
795 (rmail-require-mime-maybe)
796 (let* ((file-name (expand-file-name (or file-name-arg rmail-file-name)))
797 ;; Use find-buffer-visiting, not get-file-buffer, for those users
798 ;; who have find-file-visit-truename set to t.
799 (existed (find-buffer-visiting file-name))
800 ;; This binding is necessary because we must decide if we
801 ;; need code conversion while the buffer is unibyte
802 ;; (i.e. enable-multibyte-characters is nil).
803 (rmail-enable-multibyte
804 (if existed
805 (with-current-buffer existed enable-multibyte-characters)
806 (default-value 'enable-multibyte-characters)))
807 ;; Since the file may contain messages of different encodings
808 ;; at the tail (non-BYBYL part), we can't decode them at once
809 ;; on reading. So, at first, we read the file without text
810 ;; code conversion, then decode the messages one by one by
811 ;; rmail-decode-babyl-format or
812 ;; rmail-convert-to-babyl-format.
813 (coding-system-for-read (and rmail-enable-multibyte 'raw-text))
814 run-mail-hook msg-shown)
815 ;; Like find-file, but in the case where a buffer existed
816 ;; and the file was reverted, recompute the message-data.
817 ;; We used to bind enable-local-variables to nil here,
818 ;; but that should not be needed now that rmail-mode
819 ;; sets it locally to nil.
820 ;; (Binding a variable locally with let is not safe if it has
821 ;; buffer-local bindings.)
822 (if (and existed (not (verify-visited-file-modtime existed)))
823 (progn
824 (find-file file-name)
825 (if (and (verify-visited-file-modtime existed)
826 (eq major-mode 'rmail-mode))
827 (progn (rmail-forget-messages)
828 (rmail-set-message-counters))))
829 (switch-to-buffer
830 (let ((enable-local-variables nil))
831 (find-file-noselect file-name))))
832 (if (eq major-mode 'rmail-edit-mode)
833 (error "Exit Rmail Edit mode before getting new mail"))
834 (if (and existed (> (buffer-size) 0))
835 ;; Buffer not new and not empty; ensure in proper mode, but that's all.
836 (or (eq major-mode 'rmail-mode)
837 (progn (rmail-mode-2)
838 (setq run-mail-hook t)))
839 (setq run-mail-hook t)
840 (rmail-mode-2)
841 ;; Convert all or part to Babyl file if possible.
842 (rmail-convert-file)
843 (goto-char (point-max)))
844 ;; As we have read a file by raw-text, the buffer is set to
845 ;; unibyte. We must make it multibyte if necessary.
846 (if (and rmail-enable-multibyte
847 (not enable-multibyte-characters))
848 (set-buffer-multibyte t))
849 ;; If necessary, scan to find all the messages.
850 (rmail-maybe-set-message-counters)
851 (unwind-protect
852 (unless (and (not file-name-arg)
853 (rmail-get-new-mail))
854 (rmail-show-message (rmail-first-unseen-message)))
855 (progn
856 (if rmail-display-summary (rmail-summary))
857 (rmail-construct-io-menu)
858 (if run-mail-hook
859 (run-hooks 'rmail-mode-hook))))))
861 ;; Given the value of MAILPATH, return a list of inbox file names.
862 ;; This is turned off because it is not clear that the user wants
863 ;; all these inboxes to feed into the primary rmail file.
864 ; (defun rmail-convert-mailpath (string)
865 ; (let (idx list)
866 ; (while (setq idx (string-match "[%:]" string))
867 ; (let ((this (substring string 0 idx)))
868 ; (setq string (substring string (1+ idx)))
869 ; (setq list (cons (if (string-match "%" this)
870 ; (substring this 0 (string-match "%" this))
871 ; this)
872 ; list))))
873 ; list))
875 ; I have checked that adding "-*- rmail -*-" to the BABYL OPTIONS line
876 ; will not cause emacs 18.55 problems.
878 ;; This calls rmail-decode-babyl-format if the file is already Babyl.
880 (defun rmail-convert-file ()
881 (let (convert)
882 (widen)
883 (goto-char (point-min))
884 ;; If file doesn't start like a Babyl file,
885 ;; convert it to one, by adding a header and converting each message.
886 (cond ((looking-at "BABYL OPTIONS:"))
887 ((looking-at "Version: 5\n")
888 ;; Losing babyl file made by old version of Rmail.
889 ;; Just fix the babyl file header; don't make a new one,
890 ;; so we don't lose the Labels: file attribute, etc.
891 (let ((buffer-read-only nil))
892 (insert "BABYL OPTIONS: -*- rmail -*-\n")))
893 ((equal (point-min) (point-max))
894 ;; Empty RMAIL file. Just insert the header.
895 (rmail-insert-rmail-file-header))
897 ;; Non-empty file in non-RMAIL format. Add header and convert.
898 (setq convert t)
899 (rmail-insert-rmail-file-header)))
900 ;; If file was not a Babyl file or if there are
901 ;; Unix format messages added at the end,
902 ;; convert file as necessary.
903 (if (or convert
904 (save-excursion
905 (goto-char (point-max))
906 (search-backward "\n\^_")
907 (forward-char 2)
908 (looking-at "\n*From ")))
909 (let ((buffer-read-only nil))
910 (message "Converting to Babyl format...")
911 ;; If file needs conversion, convert it all,
912 ;; except for the BABYL header.
913 ;; (rmail-convert-to-babyl-format would delete the header.)
914 (goto-char (point-min))
915 (search-forward "\n\^_" nil t)
916 (narrow-to-region (point) (point-max))
917 (rmail-convert-to-babyl-format)
918 (message "Converting to Babyl format...done"))
919 (if (and (not rmail-enable-mime)
920 rmail-enable-multibyte)
921 ;; We still have to decode BABYL part.
922 (rmail-decode-babyl-format)))))
924 (defun rmail-insert-rmail-file-header ()
925 (let ((buffer-read-only nil))
926 ;; -*-rmail-*- is here so that visiting the file normally
927 ;; recognizes it as an Rmail file.
928 (insert "BABYL OPTIONS: -*- rmail -*-
929 Version: 5
930 Labels:
931 Note: This is the header of an rmail file.
932 Note: If you are seeing it in rmail,
933 Note: it means the file has no messages in it.\n\^_")))
935 ;; Decode Babyl formatted part at the head of current buffer by
936 ;; rmail-file-coding-system, or if it is nil, do auto conversion.
938 (defun rmail-decode-babyl-format ()
939 (let ((modifiedp (buffer-modified-p))
940 (buffer-read-only nil)
941 (coding-system rmail-file-coding-system)
942 from to)
943 (goto-char (point-min))
944 (search-forward "\n\^_" nil t) ; Skip BABYL header.
945 (setq from (point))
946 (goto-char (point-max))
947 (search-backward "\n\^_" from 'mv)
948 (setq to (point))
949 (unless (and coding-system
950 (coding-system-p coding-system))
951 (setq coding-system
952 ;; Emacs 21.1 and later writes RMAIL files in emacs-mule, but
953 ;; earlier versions did that with the current buffer's encoding.
954 ;; So we want to favor detection of emacs-mule (whose normal
955 ;; priority is quite low), but still allow detection of other
956 ;; encodings if emacs-mule won't fit. The call to
957 ;; detect-coding-with-priority below achieves that.
958 (car (detect-coding-with-priority
959 from to
960 '((coding-category-emacs-mule . emacs-mule))))))
961 (unless (memq coding-system
962 '(undecided undecided-unix))
963 (set-buffer-modified-p t) ; avoid locking when decoding
964 (let ((buffer-undo-list t))
965 (decode-coding-region from to coding-system))
966 (setq coding-system last-coding-system-used))
967 (set-buffer-modified-p modifiedp)
968 (setq buffer-file-coding-system nil)
969 (setq save-buffer-coding-system
970 (or coding-system 'undecided))))
972 (defvar rmail-mode-map nil)
973 (if rmail-mode-map
975 (setq rmail-mode-map (make-keymap))
976 (suppress-keymap rmail-mode-map)
977 (define-key rmail-mode-map "a" 'rmail-add-label)
978 (define-key rmail-mode-map "b" 'rmail-bury)
979 (define-key rmail-mode-map "c" 'rmail-continue)
980 (define-key rmail-mode-map "d" 'rmail-delete-forward)
981 (define-key rmail-mode-map "\C-d" 'rmail-delete-backward)
982 (define-key rmail-mode-map "e" 'rmail-edit-current-message)
983 (define-key rmail-mode-map "f" 'rmail-forward)
984 (define-key rmail-mode-map "g" 'rmail-get-new-mail)
985 (define-key rmail-mode-map "h" 'rmail-summary)
986 (define-key rmail-mode-map "i" 'rmail-input)
987 (define-key rmail-mode-map "j" 'rmail-show-message)
988 (define-key rmail-mode-map "k" 'rmail-kill-label)
989 (define-key rmail-mode-map "l" 'rmail-summary-by-labels)
990 (define-key rmail-mode-map "\e\C-h" 'rmail-summary)
991 (define-key rmail-mode-map "\e\C-l" 'rmail-summary-by-labels)
992 (define-key rmail-mode-map "\e\C-r" 'rmail-summary-by-recipients)
993 (define-key rmail-mode-map "\e\C-s" 'rmail-summary-by-regexp)
994 (define-key rmail-mode-map "\e\C-t" 'rmail-summary-by-topic)
995 (define-key rmail-mode-map "m" 'rmail-mail)
996 (define-key rmail-mode-map "\em" 'rmail-retry-failure)
997 (define-key rmail-mode-map "n" 'rmail-next-undeleted-message)
998 (define-key rmail-mode-map "\en" 'rmail-next-message)
999 (define-key rmail-mode-map "\e\C-n" 'rmail-next-labeled-message)
1000 (define-key rmail-mode-map "o" 'rmail-output-to-rmail-file)
1001 (define-key rmail-mode-map "\C-o" 'rmail-output)
1002 (define-key rmail-mode-map "p" 'rmail-previous-undeleted-message)
1003 (define-key rmail-mode-map "\ep" 'rmail-previous-message)
1004 (define-key rmail-mode-map "\e\C-p" 'rmail-previous-labeled-message)
1005 (define-key rmail-mode-map "q" 'rmail-quit)
1006 (define-key rmail-mode-map "r" 'rmail-reply)
1007 ;; I find I can't live without the default M-r command -- rms.
1008 ;; (define-key rmail-mode-map "\er" 'rmail-search-backwards)
1009 (define-key rmail-mode-map "s" 'rmail-expunge-and-save)
1010 (define-key rmail-mode-map "\es" 'rmail-search)
1011 (define-key rmail-mode-map "t" 'rmail-toggle-header)
1012 (define-key rmail-mode-map "u" 'rmail-undelete-previous-message)
1013 (define-key rmail-mode-map "w" 'rmail-output-body-to-file)
1014 (define-key rmail-mode-map "x" 'rmail-expunge)
1015 (define-key rmail-mode-map "." 'rmail-beginning-of-message)
1016 (define-key rmail-mode-map "/" 'rmail-end-of-message)
1017 (define-key rmail-mode-map "<" 'rmail-first-message)
1018 (define-key rmail-mode-map ">" 'rmail-last-message)
1019 (define-key rmail-mode-map " " 'scroll-up)
1020 (define-key rmail-mode-map "\177" 'scroll-down)
1021 (define-key rmail-mode-map "?" 'describe-mode)
1022 (define-key rmail-mode-map "\C-c\C-s\C-d" 'rmail-sort-by-date)
1023 (define-key rmail-mode-map "\C-c\C-s\C-s" 'rmail-sort-by-subject)
1024 (define-key rmail-mode-map "\C-c\C-s\C-a" 'rmail-sort-by-author)
1025 (define-key rmail-mode-map "\C-c\C-s\C-r" 'rmail-sort-by-recipient)
1026 (define-key rmail-mode-map "\C-c\C-s\C-c" 'rmail-sort-by-correspondent)
1027 (define-key rmail-mode-map "\C-c\C-s\C-l" 'rmail-sort-by-lines)
1028 (define-key rmail-mode-map "\C-c\C-s\C-k" 'rmail-sort-by-labels)
1029 (define-key rmail-mode-map "\C-c\C-n" 'rmail-next-same-subject)
1030 (define-key rmail-mode-map "\C-c\C-p" 'rmail-previous-same-subject)
1033 (define-key rmail-mode-map [menu-bar] (make-sparse-keymap))
1035 (define-key rmail-mode-map [menu-bar classify]
1036 (cons "Classify" (make-sparse-keymap "Classify")))
1038 (define-key rmail-mode-map [menu-bar classify input-menu]
1039 nil)
1041 (define-key rmail-mode-map [menu-bar classify output-menu]
1042 nil)
1044 (define-key rmail-mode-map [menu-bar classify output-body]
1045 '("Output body to file..." . rmail-output-body-to-file))
1047 (define-key rmail-mode-map [menu-bar classify output-inbox]
1048 '("Output (inbox)..." . rmail-output))
1050 (define-key rmail-mode-map [menu-bar classify output]
1051 '("Output (Rmail)..." . rmail-output-to-rmail-file))
1053 (define-key rmail-mode-map [menu-bar classify kill-label]
1054 '("Kill Label..." . rmail-kill-label))
1056 (define-key rmail-mode-map [menu-bar classify add-label]
1057 '("Add Label..." . rmail-add-label))
1059 (define-key rmail-mode-map [menu-bar summary]
1060 (cons "Summary" (make-sparse-keymap "Summary")))
1062 (define-key rmail-mode-map [menu-bar summary senders]
1063 '("By Senders..." . rmail-summary-by-senders))
1065 (define-key rmail-mode-map [menu-bar summary labels]
1066 '("By Labels..." . rmail-summary-by-labels))
1068 (define-key rmail-mode-map [menu-bar summary recipients]
1069 '("By Recipients..." . rmail-summary-by-recipients))
1071 (define-key rmail-mode-map [menu-bar summary topic]
1072 '("By Topic..." . rmail-summary-by-topic))
1074 (define-key rmail-mode-map [menu-bar summary regexp]
1075 '("By Regexp..." . rmail-summary-by-regexp))
1077 (define-key rmail-mode-map [menu-bar summary all]
1078 '("All" . rmail-summary))
1080 (define-key rmail-mode-map [menu-bar mail]
1081 (cons "Mail" (make-sparse-keymap "Mail")))
1083 (define-key rmail-mode-map [menu-bar mail rmail-get-new-mail]
1084 '("Get New Mail" . rmail-get-new-mail))
1086 (define-key rmail-mode-map [menu-bar mail lambda]
1087 '("----"))
1089 (define-key rmail-mode-map [menu-bar mail continue]
1090 '("Continue" . rmail-continue))
1092 (define-key rmail-mode-map [menu-bar mail resend]
1093 '("Re-send..." . rmail-resend))
1095 (define-key rmail-mode-map [menu-bar mail forward]
1096 '("Forward" . rmail-forward))
1098 (define-key rmail-mode-map [menu-bar mail retry]
1099 '("Retry" . rmail-retry-failure))
1101 (define-key rmail-mode-map [menu-bar mail reply]
1102 '("Reply" . rmail-reply))
1104 (define-key rmail-mode-map [menu-bar mail mail]
1105 '("Mail" . rmail-mail))
1107 (define-key rmail-mode-map [menu-bar delete]
1108 (cons "Delete" (make-sparse-keymap "Delete")))
1110 (define-key rmail-mode-map [menu-bar delete expunge/save]
1111 '("Expunge/Save" . rmail-expunge-and-save))
1113 (define-key rmail-mode-map [menu-bar delete expunge]
1114 '("Expunge" . rmail-expunge))
1116 (define-key rmail-mode-map [menu-bar delete undelete]
1117 '("Undelete" . rmail-undelete-previous-message))
1119 (define-key rmail-mode-map [menu-bar delete delete]
1120 '("Delete" . rmail-delete-forward))
1122 (define-key rmail-mode-map [menu-bar move]
1123 (cons "Move" (make-sparse-keymap "Move")))
1125 (define-key rmail-mode-map [menu-bar move search-back]
1126 '("Search Back..." . rmail-search-backwards))
1128 (define-key rmail-mode-map [menu-bar move search]
1129 '("Search..." . rmail-search))
1131 (define-key rmail-mode-map [menu-bar move previous]
1132 '("Previous Nondeleted" . rmail-previous-undeleted-message))
1134 (define-key rmail-mode-map [menu-bar move next]
1135 '("Next Nondeleted" . rmail-next-undeleted-message))
1137 (define-key rmail-mode-map [menu-bar move last]
1138 '("Last" . rmail-last-message))
1140 (define-key rmail-mode-map [menu-bar move first]
1141 '("First" . rmail-first-message))
1143 (define-key rmail-mode-map [menu-bar move previous]
1144 '("Previous" . rmail-previous-message))
1146 (define-key rmail-mode-map [menu-bar move next]
1147 '("Next" . rmail-next-message))
1149 ;; Rmail toolbar
1150 (defvar rmail-tool-bar-map
1151 (if (display-graphic-p)
1152 (let ((map (make-sparse-keymap)))
1153 (tool-bar-local-item-from-menu 'rmail-get-new-mail "mail/inbox"
1154 map rmail-mode-map)
1155 (tool-bar-local-item-from-menu 'rmail-next-undeleted-message "right-arrow"
1156 map rmail-mode-map)
1157 (tool-bar-local-item-from-menu 'rmail-previous-undeleted-message "left-arrow"
1158 map rmail-mode-map)
1159 (tool-bar-local-item-from-menu 'rmail-search "search"
1160 map rmail-mode-map)
1161 (tool-bar-local-item-from-menu 'rmail-input "open"
1162 map rmail-mode-map)
1163 (tool-bar-local-item-from-menu 'rmail-mail "mail/compose"
1164 map rmail-mode-map)
1165 (tool-bar-local-item-from-menu 'rmail-reply "mail/reply-all"
1166 map rmail-mode-map)
1167 (tool-bar-local-item-from-menu 'rmail-forward "mail/forward"
1168 map rmail-mode-map)
1169 (tool-bar-local-item-from-menu 'rmail-delete-forward "close"
1170 map rmail-mode-map)
1171 (tool-bar-local-item-from-menu 'rmail-output "mail/move"
1172 map rmail-mode-map)
1173 (tool-bar-local-item-from-menu 'rmail-output-body-to-file "mail/save"
1174 map rmail-mode-map)
1175 (tool-bar-local-item-from-menu 'rmail-expunge "delete"
1176 map rmail-mode-map)
1177 map)))
1181 ;; Rmail mode is suitable only for specially formatted data.
1182 (put 'rmail-mode 'mode-class 'special)
1184 (defun rmail-mode-kill-summary ()
1185 (if rmail-summary-buffer (kill-buffer rmail-summary-buffer)))
1187 ;;;###autoload
1188 (defun rmail-mode ()
1189 "Rmail Mode is used by \\<rmail-mode-map>\\[rmail] for editing Rmail files.
1190 All normal editing commands are turned off.
1191 Instead, these commands are available:
1193 \\[rmail-beginning-of-message] Move point to front of this message.
1194 \\[rmail-end-of-message] Move point to bottom of this message.
1195 \\[scroll-up] Scroll to next screen of this message.
1196 \\[scroll-down] Scroll to previous screen of this message.
1197 \\[rmail-next-undeleted-message] Move to Next non-deleted message.
1198 \\[rmail-previous-undeleted-message] Move to Previous non-deleted message.
1199 \\[rmail-next-message] Move to Next message whether deleted or not.
1200 \\[rmail-previous-message] Move to Previous message whether deleted or not.
1201 \\[rmail-first-message] Move to the first message in Rmail file.
1202 \\[rmail-last-message] Move to the last message in Rmail file.
1203 \\[rmail-show-message] Jump to message specified by numeric position in file.
1204 \\[rmail-search] Search for string and show message it is found in.
1205 \\[rmail-delete-forward] Delete this message, move to next nondeleted.
1206 \\[rmail-delete-backward] Delete this message, move to previous nondeleted.
1207 \\[rmail-undelete-previous-message] Undelete message. Tries current message, then earlier messages
1208 till a deleted message is found.
1209 \\[rmail-edit-current-message] Edit the current message. \\[rmail-cease-edit] to return to Rmail.
1210 \\[rmail-expunge] Expunge deleted messages.
1211 \\[rmail-expunge-and-save] Expunge and save the file.
1212 \\[rmail-quit] Quit Rmail: expunge, save, then switch to another buffer.
1213 \\[save-buffer] Save without expunging.
1214 \\[rmail-get-new-mail] Move new mail from system spool directory into this file.
1215 \\[rmail-mail] Mail a message (same as \\[mail-other-window]).
1216 \\[rmail-continue] Continue composing outgoing message started before.
1217 \\[rmail-reply] Reply to this message. Like \\[rmail-mail] but initializes some fields.
1218 \\[rmail-retry-failure] Send this message again. Used on a mailer failure message.
1219 \\[rmail-forward] Forward this message to another user.
1220 \\[rmail-output-to-rmail-file] Output this message to an Rmail file (append it).
1221 \\[rmail-output] Output this message to a Unix-format mail file (append it).
1222 \\[rmail-output-body-to-file] Save message body to a file. Default filename comes from Subject line.
1223 \\[rmail-input] Input Rmail file. Run Rmail on that file.
1224 \\[rmail-add-label] Add label to message. It will be displayed in the mode line.
1225 \\[rmail-kill-label] Kill label. Remove a label from current message.
1226 \\[rmail-next-labeled-message] Move to Next message with specified label
1227 (label defaults to last one specified).
1228 Standard labels: filed, unseen, answered, forwarded, deleted.
1229 Any other label is present only if you add it with \\[rmail-add-label].
1230 \\[rmail-previous-labeled-message] Move to Previous message with specified label
1231 \\[rmail-summary] Show headers buffer, with a one line summary of each message.
1232 \\[rmail-summary-by-labels] Summarize only messages with particular label(s).
1233 \\[rmail-summary-by-recipients] Summarize only messages with particular recipient(s).
1234 \\[rmail-summary-by-regexp] Summarize only messages with particular regexp(s).
1235 \\[rmail-summary-by-topic] Summarize only messages with subject line regexp(s).
1236 \\[rmail-toggle-header] Toggle display of complete header."
1237 (interactive)
1238 (let ((finding-rmail-file (not (eq major-mode 'rmail-mode))))
1239 (rmail-mode-2)
1240 (when (and finding-rmail-file
1241 (null coding-system-for-read)
1242 default-enable-multibyte-characters)
1243 (let ((rmail-enable-multibyte t))
1244 (rmail-require-mime-maybe)
1245 (rmail-convert-file)
1246 (goto-char (point-max))
1247 (set-buffer-multibyte t)))
1248 (rmail-set-message-counters)
1249 (rmail-show-message rmail-total-messages)
1250 (when finding-rmail-file
1251 (when rmail-display-summary
1252 (rmail-summary))
1253 (rmail-construct-io-menu))
1254 (run-mode-hooks 'rmail-mode-hook)))
1256 (defun rmail-mode-2 ()
1257 (kill-all-local-variables)
1258 (rmail-mode-1)
1259 (rmail-perm-variables)
1260 (rmail-variables))
1262 (defun rmail-mode-1 ()
1263 (setq major-mode 'rmail-mode)
1264 (setq mode-name "RMAIL")
1265 (setq buffer-read-only t)
1266 ;; No need to auto save RMAIL files in normal circumstances
1267 ;; because they contain no info except attribute changes
1268 ;; and deletion of messages.
1269 ;; The one exception is when messages are copied into an Rmail mode buffer.
1270 ;; rmail-output-to-rmail-file enables auto save when you do that.
1271 (setq buffer-auto-save-file-name nil)
1272 (setq mode-line-modified "--")
1273 (use-local-map rmail-mode-map)
1274 (set-syntax-table text-mode-syntax-table)
1275 (setq local-abbrev-table text-mode-abbrev-table))
1277 ;; Set up the permanent locals associated with an Rmail file.
1278 (defun rmail-perm-variables ()
1279 (make-local-variable 'rmail-last-label)
1280 (make-local-variable 'rmail-last-regexp)
1281 (make-local-variable 'rmail-deleted-vector)
1282 (make-local-variable 'rmail-buffer)
1283 (setq rmail-buffer (current-buffer))
1284 (make-local-variable 'rmail-view-buffer)
1285 (setq rmail-view-buffer rmail-buffer)
1286 (make-local-variable 'rmail-summary-buffer)
1287 (make-local-variable 'rmail-summary-vector)
1288 (make-local-variable 'rmail-current-message)
1289 (make-local-variable 'rmail-total-messages)
1290 (make-local-variable 'rmail-overlay-list)
1291 (setq rmail-overlay-list nil)
1292 (make-local-variable 'rmail-message-vector)
1293 (make-local-variable 'rmail-msgref-vector)
1294 (make-local-variable 'rmail-inbox-list)
1295 (setq rmail-inbox-list (rmail-parse-file-inboxes))
1296 ;; Provide default set of inboxes for primary mail file ~/RMAIL.
1297 (and (null rmail-inbox-list)
1298 (or (equal buffer-file-name (expand-file-name rmail-file-name))
1299 (equal buffer-file-truename
1300 (abbreviate-file-name (file-truename rmail-file-name))))
1301 (setq rmail-inbox-list
1302 (or rmail-primary-inbox-list
1303 (list (or (getenv "MAIL")
1304 (concat rmail-spool-directory
1305 (user-login-name)))))))
1306 (make-local-variable 'rmail-keywords)
1307 (set (make-local-variable 'tool-bar-map) rmail-tool-bar-map)
1308 ;; this gets generated as needed
1309 (setq rmail-keywords nil))
1311 ;; Set up the non-permanent locals associated with Rmail mode.
1312 (defun rmail-variables ()
1313 (make-local-variable 'save-buffer-coding-system)
1314 ;; If we don't already have a value for save-buffer-coding-system,
1315 ;; get it from buffer-file-coding-system, and clear that
1316 ;; because it should be determined in rmail-show-message.
1317 (unless save-buffer-coding-system
1318 (setq save-buffer-coding-system (or buffer-file-coding-system 'undecided))
1319 (setq buffer-file-coding-system nil))
1320 ;; Don't let a local variables list in a message cause confusion.
1321 (make-local-variable 'local-enable-local-variables)
1322 (setq local-enable-local-variables nil)
1323 (make-local-variable 'revert-buffer-function)
1324 (setq revert-buffer-function 'rmail-revert)
1325 (make-local-variable 'font-lock-defaults)
1326 (setq font-lock-defaults
1327 '(rmail-font-lock-keywords
1328 t t nil nil
1329 (font-lock-maximum-size . nil)
1330 (font-lock-fontify-buffer-function . rmail-fontify-buffer-function)
1331 (font-lock-unfontify-buffer-function . rmail-unfontify-buffer-function)
1332 (font-lock-inhibit-thing-lock . (lazy-lock-mode fast-lock-mode))))
1333 (make-local-variable 'require-final-newline)
1334 (setq require-final-newline nil)
1335 (make-local-variable 'version-control)
1336 (setq version-control 'never)
1337 (make-local-variable 'kill-buffer-hook)
1338 (add-hook 'kill-buffer-hook 'rmail-mode-kill-summary)
1339 (make-local-variable 'file-precious-flag)
1340 (setq file-precious-flag t)
1341 (make-local-variable 'desktop-save-buffer)
1342 (setq desktop-save-buffer t))
1344 ;; Handle M-x revert-buffer done in an rmail-mode buffer.
1345 (defun rmail-revert (arg noconfirm)
1346 (set-buffer rmail-buffer)
1347 (let* ((revert-buffer-function (default-value 'revert-buffer-function))
1348 (rmail-enable-multibyte enable-multibyte-characters)
1349 ;; See similar code in `rmail'.
1350 (coding-system-for-read (and rmail-enable-multibyte 'raw-text)))
1351 ;; Call our caller again, but this time it does the default thing.
1352 (if (revert-buffer arg noconfirm)
1353 ;; If the user said "yes", and we changed something,
1354 ;; reparse the messages.
1355 (progn
1356 (set-buffer rmail-buffer)
1357 (rmail-mode-2)
1358 ;; Convert all or part to Babyl file if possible.
1359 (rmail-convert-file)
1360 ;; We have read the file as raw-text, so the buffer is set to
1361 ;; unibyte. Make it multibyte if necessary.
1362 (if (and rmail-enable-multibyte
1363 (not enable-multibyte-characters))
1364 (set-buffer-multibyte t))
1365 (goto-char (point-max))
1366 (rmail-set-message-counters)
1367 (rmail-show-message rmail-total-messages)
1368 (run-hooks 'rmail-mode-hook)))))
1370 ;; Return a list of files from this buffer's Mail: option.
1371 ;; Does not assume that messages have been parsed.
1372 ;; Just returns nil if buffer does not look like Babyl format.
1373 (defun rmail-parse-file-inboxes ()
1374 (save-excursion
1375 (save-restriction
1376 (widen)
1377 (goto-char 1)
1378 (cond ((looking-at "BABYL OPTIONS:")
1379 (search-forward "\n\^_" nil 'move)
1380 (narrow-to-region 1 (point))
1381 (goto-char 1)
1382 (if (search-forward "\nMail:" nil t)
1383 (progn
1384 (narrow-to-region (point) (progn (end-of-line) (point)))
1385 (goto-char (point-min))
1386 (mail-parse-comma-list))))))))
1388 (defun rmail-expunge-and-save ()
1389 "Expunge and save RMAIL file."
1390 (interactive)
1391 (rmail-expunge)
1392 (set-buffer rmail-buffer)
1393 (save-buffer)
1394 (if (rmail-summary-exists)
1395 (rmail-select-summary (set-buffer-modified-p nil))))
1397 (defun rmail-quit ()
1398 "Quit out of RMAIL.
1399 Hook `rmail-quit-hook' is run after expunging."
1400 (interactive)
1401 (rmail-expunge-and-save)
1402 (when (boundp 'rmail-quit-hook)
1403 (run-hooks 'rmail-quit-hook))
1404 ;; Don't switch to the summary buffer even if it was recently visible.
1405 (when rmail-summary-buffer
1406 (replace-buffer-in-windows rmail-summary-buffer)
1407 (bury-buffer rmail-summary-buffer))
1408 (if rmail-enable-mime
1409 (let ((obuf rmail-buffer)
1410 (ovbuf rmail-view-buffer))
1411 (set-buffer rmail-view-buffer)
1412 (quit-window)
1413 (replace-buffer-in-windows ovbuf)
1414 (replace-buffer-in-windows obuf)
1415 (bury-buffer obuf))
1416 (let ((obuf (current-buffer)))
1417 (quit-window)
1418 (replace-buffer-in-windows obuf))))
1420 (defun rmail-bury ()
1421 "Bury current Rmail buffer and its summary buffer."
1422 (interactive)
1423 ;; This let var was called rmail-buffer, but that interfered
1424 ;; with the buffer-local var used in summary buffers.
1425 (let ((buffer-to-bury (current-buffer)))
1426 (if (rmail-summary-exists)
1427 (let (window)
1428 (while (setq window (get-buffer-window rmail-summary-buffer))
1429 (quit-window nil window))
1430 (bury-buffer rmail-summary-buffer)))
1431 (quit-window)))
1433 (defun rmail-duplicate-message ()
1434 "Create a duplicated copy of the current message.
1435 The duplicate copy goes into the Rmail file just after the
1436 original copy."
1437 (interactive)
1438 (widen)
1439 (let ((buffer-read-only nil)
1440 (number rmail-current-message)
1441 (string (buffer-substring (rmail-msgbeg rmail-current-message)
1442 (rmail-msgend rmail-current-message))))
1443 (goto-char (rmail-msgend rmail-current-message))
1444 (insert string)
1445 (rmail-forget-messages)
1446 (rmail-show-message number)
1447 (message "Message duplicated")))
1449 ;;;###autoload
1450 (defun rmail-input (filename)
1451 "Run Rmail on file FILENAME."
1452 (interactive "FRun rmail on RMAIL file: ")
1453 (rmail filename))
1456 ;; This used to scan subdirectories recursively, but someone pointed out
1457 ;; that if the user wants that, person can put all the files in one dir.
1458 ;; And the recursive scan was slow. So I took it out.
1459 ;; rms, Sep 1996.
1460 (defun rmail-find-all-files (start)
1461 "Return list of file in dir START that match `rmail-secondary-file-regexp'."
1462 (if (file-accessible-directory-p start)
1463 ;; Don't sort here.
1464 (let* ((case-fold-search t)
1465 (files (directory-files start t rmail-secondary-file-regexp)))
1466 ;; Sort here instead of in directory-files
1467 ;; because this list is usually much shorter.
1468 (sort files 'string<))))
1470 (defun rmail-list-to-menu (menu-name l action &optional full-name)
1471 (let ((menu (make-sparse-keymap menu-name)))
1472 (mapcar
1473 (function (lambda (item)
1474 (let (command)
1475 (if (consp item)
1476 (progn
1477 (setq command
1478 (rmail-list-to-menu (car item) (cdr item)
1479 action
1480 (if full-name
1481 (concat full-name "/"
1482 (car item))
1483 (car item))))
1484 (setq name (car item)))
1485 (progn
1486 (setq name item)
1487 (setq command
1488 (list 'lambda () '(interactive)
1489 (list action
1490 (expand-file-name
1491 (if full-name
1492 (concat full-name "/" item)
1493 item)
1494 rmail-secondary-file-directory))))))
1495 (define-key menu (vector (intern name))
1496 (cons name command)))))
1497 (reverse l))
1498 menu))
1500 ;; This command is always "disabled" when it appears in a menu.
1501 (put 'rmail-disable-menu 'menu-enable ''nil)
1503 (defun rmail-construct-io-menu ()
1504 (let ((files (rmail-find-all-files rmail-secondary-file-directory)))
1505 (if files
1506 (progn
1507 (define-key rmail-mode-map [menu-bar classify input-menu]
1508 (cons "Input Rmail File"
1509 (rmail-list-to-menu "Input Rmail File"
1510 files
1511 'rmail-input)))
1512 (define-key rmail-mode-map [menu-bar classify output-menu]
1513 (cons "Output Rmail File"
1514 (rmail-list-to-menu "Output Rmail File"
1515 files
1516 'rmail-output-to-rmail-file))))
1518 (define-key rmail-mode-map [menu-bar classify input-menu]
1519 '("Input Rmail File" . rmail-disable-menu))
1520 (define-key rmail-mode-map [menu-bar classify output-menu]
1521 '("Output Rmail File" . rmail-disable-menu)))))
1524 ;;;; *** Rmail input ***
1526 ;; RLK feature not added in this version:
1527 ;; argument specifies inbox file or files in various ways.
1529 (defun rmail-get-new-mail (&optional file-name)
1530 "Move any new mail from this RMAIL file's inbox files.
1531 The inbox files can be specified with the file's Mail: option. The
1532 variable `rmail-primary-inbox-list' specifies the inboxes for your
1533 primary RMAIL file if it has no Mail: option. By default, this is
1534 your /usr/spool/mail/$USER.
1536 You can also specify the file to get new mail from. In this case, the
1537 file of new mail is not changed or deleted. Noninteractively, you can
1538 pass the inbox file name as an argument. Interactively, a prefix
1539 argument causes us to read a file name and use that file as the inbox.
1541 If the variable `rmail-preserve-inbox' is non-nil, new mail will
1542 always be left in inbox files rather than deleted.
1544 This function runs `rmail-get-new-mail-hook' before saving the updated file.
1545 It returns t if it got any new messages."
1546 (interactive
1547 (list (if current-prefix-arg
1548 (read-file-name "Get new mail from file: "))))
1549 (run-hooks 'rmail-before-get-new-mail-hook)
1550 ;; If the disk file has been changed from under us,
1551 ;; revert to it before we get new mail.
1552 (or (verify-visited-file-modtime (current-buffer))
1553 (find-file (buffer-file-name)))
1554 (set-buffer rmail-buffer)
1555 (rmail-maybe-set-message-counters)
1556 (widen)
1557 ;; Get rid of all undo records for this buffer.
1558 (or (eq buffer-undo-list t)
1559 (setq buffer-undo-list nil))
1560 (let ((all-files (if file-name (list file-name)
1561 rmail-inbox-list))
1562 (rmail-enable-multibyte (default-value 'enable-multibyte-characters))
1563 found)
1564 (unwind-protect
1565 (progn
1566 (while all-files
1567 (let ((opoint (point))
1568 (new-messages 0)
1569 (rsf-number-of-spam 0)
1570 (delete-files ())
1571 ;; If buffer has not changed yet, and has not been saved yet,
1572 ;; don't replace the old backup file now.
1573 (make-backup-files (and make-backup-files (buffer-modified-p)))
1574 (buffer-read-only nil)
1575 ;; Don't make undo records for what we do in getting mail.
1576 (buffer-undo-list t)
1577 success
1578 ;; Files to insert this time around.
1579 files
1580 ;; Last names of those files.
1581 file-last-names)
1582 ;; Pull files off all-files onto files
1583 ;; as long as there is no name conflict.
1584 ;; A conflict happens when two inbox file names
1585 ;; have the same last component.
1586 (while (and all-files
1587 (not (member (file-name-nondirectory (car all-files))
1588 file-last-names)))
1589 (setq files (cons (car all-files) files)
1590 file-last-names
1591 (cons (file-name-nondirectory (car all-files)) files))
1592 (setq all-files (cdr all-files)))
1593 ;; Put them back in their original order.
1594 (setq files (nreverse files))
1596 (goto-char (point-max))
1597 (skip-chars-backward " \t\n") ; just in case of brain damage
1598 (delete-region (point) (point-max)) ; caused by require-final-newline
1599 (save-excursion
1600 (save-restriction
1601 (narrow-to-region (point) (point))
1602 ;; Read in the contents of the inbox files,
1603 ;; renaming them as necessary,
1604 ;; and adding to the list of files to delete eventually.
1605 (if file-name
1606 (rmail-insert-inbox-text files nil)
1607 (setq delete-files (rmail-insert-inbox-text files t)))
1608 ;; Scan the new text and convert each message to babyl format.
1609 (goto-char (point-min))
1610 (unwind-protect
1611 (save-excursion
1612 (setq new-messages (rmail-convert-to-babyl-format)
1613 success t))
1614 ;; Try to delete the garbage just inserted.
1615 (or success (delete-region (point-min) (point-max)))
1616 ;; If we could not convert the file's inboxes,
1617 ;; rename the files we tried to read
1618 ;; so we won't over and over again.
1619 (if (and (not file-name) (not success))
1620 (let ((delfiles delete-files)
1621 (count 0))
1622 (while delfiles
1623 (while (file-exists-p (format "RMAILOSE.%d" count))
1624 (setq count (1+ count)))
1625 (rename-file (car delfiles)
1626 (format "RMAILOSE.%d" count))
1627 (setq delfiles (cdr delfiles))))))
1628 (or (zerop new-messages)
1629 (let (success)
1630 (widen)
1631 (search-backward "\n\^_" nil t)
1632 (narrow-to-region (point) (point-max))
1633 (goto-char (1+ (point-min)))
1634 (rmail-count-new-messages)
1635 (run-hooks 'rmail-get-new-mail-hook)
1636 (save-buffer)))
1637 ;; Delete the old files, now that babyl file is saved.
1638 (while delete-files
1639 (condition-case ()
1640 ;; First, try deleting.
1641 (condition-case ()
1642 (delete-file (car delete-files))
1643 (file-error
1644 ;; If we can't delete it, truncate it.
1645 (write-region (point) (point) (car delete-files))))
1646 (file-error nil))
1647 (setq delete-files (cdr delete-files)))))
1648 (if (= new-messages 0)
1649 (progn (goto-char opoint)
1650 (if (or file-name rmail-inbox-list)
1651 (message "(No new mail has arrived)")))
1652 ;; check new messages to see if any of them is spam:
1653 (if (and (featurep 'rmail-spam-filter)
1654 rmail-use-spam-filter)
1655 (let*
1656 ((old-messages (- rmail-total-messages new-messages))
1657 (rsf-scanned-message-number (1+ old-messages))
1658 ;; save deletion flags of old messages: vector starts
1659 ;; at zero (is one longer that no of messages),
1660 ;; therefore take 1+ old-messages
1661 (save-deleted
1662 (substring rmail-deleted-vector 0 (1+
1663 old-messages))))
1664 ;; set all messages to undeleted
1665 (setq rmail-deleted-vector
1666 (make-string (1+ rmail-total-messages) ?\ ))
1667 (while (<= rsf-scanned-message-number
1668 rmail-total-messages)
1669 (progn
1670 (if (not (rmail-spam-filter rsf-scanned-message-number))
1671 (progn (setq rsf-number-of-spam (1+ rsf-number-of-spam)))
1673 (setq rsf-scanned-message-number (1+ rsf-scanned-message-number))
1675 (if (> rsf-number-of-spam 0)
1676 (progn
1677 (when (rmail-expunge-confirmed)
1678 (rmail-only-expunge t))
1680 (setq rmail-deleted-vector
1681 (concat
1682 save-deleted
1683 (make-string (- rmail-total-messages old-messages)
1684 ?\ )))
1686 (if (rmail-summary-exists)
1687 (rmail-select-summary
1688 (rmail-update-summary)))
1689 (message "%d new message%s read%s"
1690 new-messages (if (= 1 new-messages) "" "s")
1691 ;; print out a message on number of spam messages found:
1692 (if (and (featurep 'rmail-spam-filter)
1693 rmail-use-spam-filter
1694 (> rsf-number-of-spam 0))
1695 (cond ((= 1 new-messages)
1696 ", and appears to be spam")
1697 ((= rsf-number-of-spam new-messages)
1698 ", and all appear to be spam")
1699 ((> rsf-number-of-spam 1)
1700 (format ", and %d appear to be spam"
1701 rsf-number-of-spam))
1703 ", and 1 appears to be spam"))
1704 ""))
1705 (if (and (featurep 'rmail-spam-filter)
1706 rmail-use-spam-filter
1707 (> rsf-number-of-spam 0))
1708 (progn (if rsf-beep (beep t))
1709 (sleep-for rsf-sleep-after-message)))
1711 ;; Move to the first new message
1712 ;; unless we have other unseen messages before it.
1713 (rmail-show-message (rmail-first-unseen-message))
1714 (run-hooks 'rmail-after-get-new-mail-hook)
1715 (setq found t))))
1716 found)
1717 ;; Don't leave the buffer screwed up if we get a disk-full error.
1718 (or found (rmail-show-message)))))
1720 (defun rmail-parse-url (file)
1721 "Parse the supplied URL. Return (list MAILBOX-NAME REMOTE PASSWORD GOT-PASSWORD)
1722 WHERE MAILBOX-NAME is the name of the mailbox suitable as argument to the
1723 actual version of `movemail', REMOTE is non-nil if MAILBOX-NAME refers to
1724 a remote mailbox, PASSWORD is the password if it should be
1725 supplied as a separate argument to `movemail' or nil otherwise, GOT-PASSWORD
1726 is non-nil if the user has supplied the password interactively.
1728 (cond
1729 ((string-match "^\\([^:]+\\)://\\(\\([^:@]+\\)\\(:\\([^@]+\\)\\)?@\\)?.*" file)
1730 (let (got-password supplied-password
1731 (proto (match-string 1 file))
1732 (user (match-string 3 file))
1733 (pass (match-string 5 file))
1734 (host (substring file (or (match-end 2)
1735 (+ 3 (match-end 1))))))
1737 (if (not pass)
1738 (when rmail-remote-password-required
1739 (setq got-password (not (rmail-have-password)))
1740 (setq supplied-password (rmail-get-remote-password
1741 (string-equal proto "imap")))))
1743 (if (rmail-movemail-variant-p 'emacs)
1744 (if (string-equal proto "pop")
1745 (list (concat "po:" user ":" host)
1747 (or pass supplied-password)
1748 got-password)
1749 (error "Emacs movemail does not support %s protocol" proto))
1750 (list file
1751 (or (string-equal proto "pop") (string-equal proto "imap"))
1752 supplied-password
1753 got-password))))
1755 ((string-match "^po:\\([^:]+\\)\\(:\\(.*\\)\\)?" file)
1756 (let (got-password supplied-password
1757 (proto "pop")
1758 (user (match-string 1 file))
1759 (host (match-string 3 file)))
1761 (when rmail-remote-password-required
1762 (setq got-password (not (rmail-have-password)))
1763 (setq supplied-password (rmail-get-remote-password nil)))
1765 (list file "pop" supplied-password got-password)))
1768 (list file nil nil nil))))
1770 (defun rmail-insert-inbox-text (files renamep)
1771 ;; Detect a locked file now, so that we avoid moving mail
1772 ;; out of the real inbox file. (That could scare people.)
1773 (or (memq (file-locked-p buffer-file-name) '(nil t))
1774 (error "RMAIL file %s is locked"
1775 (file-name-nondirectory buffer-file-name)))
1776 (let (file tofile delete-files movemail popmail got-password password)
1777 (while files
1778 ;; Handle remote mailbox names specially; don't expand as filenames
1779 ;; in case the userid contains a directory separator.
1780 (setq file (car files))
1781 (let ((url-data (rmail-parse-url file)))
1782 (setq file (nth 0 url-data))
1783 (setq popmail (nth 1 url-data))
1784 (setq password (nth 2 url-data))
1785 (setq got-password (nth 3 url-data)))
1787 (if popmail
1788 (setq renamep t)
1789 (setq file (file-truename
1790 (substitute-in-file-name (expand-file-name file)))))
1791 (setq tofile (expand-file-name
1792 ;; Generate name to move to from inbox name,
1793 ;; in case of multiple inboxes that need moving.
1794 (concat ".newmail-"
1795 (file-name-nondirectory
1796 (if (memq system-type '(windows-nt cygwin))
1797 ;; cannot have "po:" in file name
1798 (substring file 3)
1799 file)))
1800 ;; Use the directory of this rmail file
1801 ;; because it's a nuisance to use the homedir
1802 ;; if that is on a full disk and this rmail
1803 ;; file isn't.
1804 (file-name-directory
1805 (expand-file-name buffer-file-name))))
1806 ;; Always use movemail to rename the file,
1807 ;; since there can be mailboxes in various directories.
1808 (if (not popmail)
1809 (progn
1810 ;; On some systems, /usr/spool/mail/foo is a directory
1811 ;; and the actual inbox is /usr/spool/mail/foo/foo.
1812 (if (file-directory-p file)
1813 (setq file (expand-file-name (user-login-name)
1814 file)))))
1815 (cond (popmail
1816 (message "Getting mail from the remote server ..."))
1817 ((and (file-exists-p tofile)
1818 (/= 0 (nth 7 (file-attributes tofile))))
1819 (message "Getting mail from %s..." tofile))
1820 ((and (file-exists-p file)
1821 (/= 0 (nth 7 (file-attributes file))))
1822 (message "Getting mail from %s..." file)))
1823 ;; Set TOFILE if have not already done so, and
1824 ;; rename or copy the file FILE to TOFILE if and as appropriate.
1825 (cond ((not renamep)
1826 (setq tofile file))
1827 ((or (file-exists-p tofile) (and (not popmail)
1828 (not (file-exists-p file))))
1829 nil)
1831 (with-temp-buffer
1832 (let ((errors (current-buffer)))
1833 (buffer-disable-undo errors)
1834 (let ((args
1835 (append
1836 (list rmail-movemail-program nil errors nil)
1837 (if rmail-preserve-inbox
1838 (list "-p")
1839 nil)
1840 (if (rmail-movemail-variant-p 'mailutils)
1841 (append (list "--emacs") rmail-movemail-flags)
1842 rmail-movemail-flags)
1843 (list file tofile)
1844 (if password (list password) nil))))
1845 (apply 'call-process args))
1846 (if (not (buffer-modified-p errors))
1847 ;; No output => movemail won
1849 (set-buffer errors)
1850 (subst-char-in-region (point-min) (point-max)
1851 ?\n ?\ )
1852 (goto-char (point-max))
1853 (skip-chars-backward " \t")
1854 (delete-region (point) (point-max))
1855 (goto-char (point-min))
1856 (if (looking-at "movemail: ")
1857 (delete-region (point-min) (match-end 0)))
1858 (beep t)
1859 ;; If we just read the password, most likely it is
1860 ;; wrong. Otherwise, see if there is a specific
1861 ;; reason to think that the problem is a wrong passwd.
1862 (if (or got-password
1863 (re-search-forward rmail-remote-password-error
1864 nil t))
1865 (rmail-set-remote-password nil))
1867 ;; If using Mailutils, remove initial error code
1868 ;; abbreviation
1869 (when (rmail-movemail-variant-p 'mailutils)
1870 (goto-char (point-min))
1871 (when (looking-at "[A-Z][A-Z0-9_]*:")
1872 (delete-region (point-min) (match-end 0))))
1874 (message "movemail: %s"
1875 (buffer-substring (point-min)
1876 (point-max)))
1878 (sit-for 3)
1879 nil)))))
1881 ;; At this point, TOFILE contains the name to read:
1882 ;; Either the alternate name (if we renamed)
1883 ;; or the actual inbox (if not renaming).
1884 (if (file-exists-p tofile)
1885 (let ((coding-system-for-read 'no-conversion)
1886 size)
1887 (goto-char (point-max))
1888 (setq size (nth 1 (insert-file-contents tofile)))
1889 (goto-char (point-max))
1890 (or (= (preceding-char) ?\n)
1891 (zerop size)
1892 (insert ?\n))
1893 (if (not (and rmail-preserve-inbox (string= file tofile)))
1894 (setq delete-files (cons tofile delete-files)))))
1895 (message "")
1896 (setq files (cdr files)))
1897 delete-files))
1899 ;; Decode the region specified by FROM and TO by CODING.
1900 ;; If CODING is nil or an invalid coding system, decode by `undecided'.
1901 (defun rmail-decode-region (from to coding)
1902 (if (or (not coding) (not (coding-system-p coding)))
1903 (setq coding 'undecided))
1904 ;; Use -dos decoding, to remove ^M characters left from base64 or
1905 ;; rogue qp-encoded text.
1906 (decode-coding-region from to
1907 (coding-system-change-eol-conversion coding 1))
1908 ;; Don't reveal the fact we used -dos decoding, as users generally
1909 ;; will not expect the RMAIL buffer to use DOS EOL format.
1910 (setq buffer-file-coding-system
1911 (setq last-coding-system-used
1912 (coding-system-change-eol-conversion coding 0))))
1914 ;; the rmail-break-forwarded-messages feature is not implemented
1915 (defun rmail-convert-to-babyl-format ()
1916 (let ((count 0) start
1917 (case-fold-search nil)
1918 (buffer-undo-list t)
1919 (invalid-input-resync
1920 (function (lambda ()
1921 (message "Invalid Babyl format in inbox!")
1922 (sit-for 3)
1923 ;; Try to get back in sync with a real message.
1924 (if (re-search-forward
1925 (concat rmail-mmdf-delim1 "\\|^From") nil t)
1926 (beginning-of-line)
1927 (goto-char (point-max)))))))
1928 (goto-char (point-min))
1929 (save-restriction
1930 (while (not (eobp))
1931 (setq start (point))
1932 (cond ((looking-at "BABYL OPTIONS:");Babyl header
1933 (if (search-forward "\n\^_" nil t)
1934 ;; If we find the proper terminator, delete through there.
1935 (delete-region (point-min) (point))
1936 (funcall invalid-input-resync)
1937 (delete-region (point-min) (point))))
1938 ;; Babyl format message
1939 ((looking-at "\^L")
1940 (or (search-forward "\n\^_" nil t)
1941 (funcall invalid-input-resync))
1942 (setq count (1+ count))
1943 ;; Make sure there is no extra white space after the ^_
1944 ;; at the end of the message.
1945 ;; Narrowing will make sure that whatever follows the junk
1946 ;; will be treated properly.
1947 (delete-region (point)
1948 (save-excursion
1949 (skip-chars-forward " \t\n")
1950 (point)))
1951 (save-excursion
1952 (let* ((header-end
1953 (progn
1954 (save-excursion
1955 (goto-char start)
1956 (forward-line 1)
1957 (if (looking-at "0")
1958 (forward-line 1)
1959 (forward-line 2))
1960 (save-restriction
1961 (narrow-to-region (point) (point-max))
1962 (rfc822-goto-eoh)
1963 (point)))))
1964 (case-fold-search t)
1965 (quoted-printable-header-field-end
1966 (save-excursion
1967 (goto-char start)
1968 (re-search-forward
1969 "^content-transfer-encoding:\\(\n?[\t ]\\)*quoted-printable\\(\n?[\t ]\\)*"
1970 header-end t)))
1971 (base64-header-field-end
1972 (save-excursion
1973 (goto-char start)
1974 ;; Don't try to decode non-text data.
1975 (and (re-search-forward
1976 "^content-type:\\(\n?[\t ]\\)\\(text\\|message\\)/"
1977 header-end t)
1978 (goto-char start)
1979 (re-search-forward
1980 "^content-transfer-encoding:\\(\n?[\t ]\\)*base64\\(\n?[\t ]\\)*"
1981 header-end t)))))
1982 (if quoted-printable-header-field-end
1983 (save-excursion
1984 (unless
1985 (mail-unquote-printable-region header-end (point) nil t t)
1986 (message "Malformed MIME quoted-printable message"))
1987 ;; Change "quoted-printable" to "8bit",
1988 ;; to reflect the decoding we just did.
1989 (goto-char quoted-printable-header-field-end)
1990 (delete-region (point) (search-backward ":"))
1991 (insert ": 8bit")))
1992 (if base64-header-field-end
1993 (save-excursion
1994 (when
1995 (condition-case nil
1996 (progn
1997 (base64-decode-region (1+ header-end)
1998 (- (point) 2))
2000 (error nil))
2001 ;; Change "base64" to "8bit", to reflect the
2002 ;; decoding we just did.
2003 (goto-char base64-header-field-end)
2004 (delete-region (point) (search-backward ":"))
2005 (insert ": 8bit"))))
2006 (setq last-coding-system-used nil)
2007 (or rmail-enable-mime
2008 (not rmail-enable-multibyte)
2009 (let ((mime-charset
2010 (if (and rmail-decode-mime-charset
2011 (save-excursion
2012 (goto-char start)
2013 (search-forward "\n\n" nil t)
2014 (let ((case-fold-search t))
2015 (re-search-backward
2016 rmail-mime-charset-pattern
2017 start t))))
2018 (intern (downcase (match-string 1))))))
2019 (rmail-decode-region start (point) mime-charset)))))
2020 ;; Add an X-Coding-System: header if we don't have one.
2021 (save-excursion
2022 (goto-char start)
2023 (forward-line 1)
2024 (if (looking-at "0")
2025 (forward-line 1)
2026 (forward-line 2))
2027 (or (save-restriction
2028 (narrow-to-region (point) (point-max))
2029 (rfc822-goto-eoh)
2030 (goto-char (point-min))
2031 (re-search-forward "^X-Coding-System:" nil t))
2032 (insert "X-Coding-System: "
2033 (symbol-name last-coding-system-used)
2034 "\n")))
2035 (narrow-to-region (point) (point-max))
2036 (and (= 0 (% count 10))
2037 (message "Converting to Babyl format...%d" count)))
2038 ;;*** MMDF format
2039 ((let ((case-fold-search t))
2040 (looking-at rmail-mmdf-delim1))
2041 (let ((case-fold-search t))
2042 (replace-match "\^L\n0, unseen,,\n*** EOOH ***\n")
2043 (re-search-forward rmail-mmdf-delim2 nil t)
2044 (replace-match "\^_"))
2045 (save-excursion
2046 (save-restriction
2047 (narrow-to-region start (1- (point)))
2048 (goto-char (point-min))
2049 (while (search-forward "\n\^_" nil t); single char "\^_"
2050 (replace-match "\n^_")))); 2 chars: "^" and "_"
2051 (setq last-coding-system-used nil)
2052 (or rmail-enable-mime
2053 (not rmail-enable-multibyte)
2054 (decode-coding-region start (point) 'undecided))
2055 (save-excursion
2056 (goto-char start)
2057 (forward-line 3)
2058 (insert "X-Coding-System: "
2059 (symbol-name last-coding-system-used)
2060 "\n"))
2061 (narrow-to-region (point) (point-max))
2062 (setq count (1+ count))
2063 (and (= 0 (% count 10))
2064 (message "Converting to Babyl format...%d" count)))
2065 ;;*** Mail format
2066 ((looking-at "^From ")
2067 (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
2068 (rmail-nuke-pinhead-header)
2069 ;; If this message has a Content-Length field,
2070 ;; skip to the end of the contents.
2071 (let* ((header-end (save-excursion
2072 (and (re-search-forward "\n\n" nil t)
2073 (1- (point)))))
2074 (case-fold-search t)
2075 (quoted-printable-header-field-end
2076 (save-excursion
2077 (re-search-forward
2078 "^content-transfer-encoding:\\(\n?[\t ]\\)*quoted-printable\\(\n?[\t ]\\)*"
2079 header-end t)))
2080 (base64-header-field-end
2081 (and
2082 ;; Don't decode non-text data.
2083 (save-excursion
2084 (re-search-forward
2085 "^content-type:\\(\n?[\t ]\\)\\(text\\|message\\)/"
2086 header-end t))
2087 (save-excursion
2088 (re-search-forward
2089 "^content-transfer-encoding:\\(\n?[\t ]\\)*base64\\(\n?[\t ]\\)*"
2090 header-end t))))
2091 (size
2092 ;; Get the numeric value from the Content-Length field.
2093 (save-excursion
2094 ;; Back up to end of prev line,
2095 ;; in case the Content-Length field comes first.
2096 (forward-char -1)
2097 (and (search-forward "\ncontent-length: "
2098 header-end t)
2099 (let ((beg (point))
2100 (eol (progn (end-of-line) (point))))
2101 (string-to-number (buffer-substring beg eol)))))))
2102 (and size
2103 (if (and (natnump size)
2104 (<= (+ header-end size) (point-max))
2105 ;; Make sure this would put us at a position
2106 ;; that we could continue from.
2107 (save-excursion
2108 (goto-char (+ header-end size))
2109 (skip-chars-forward "\n")
2110 (or (eobp)
2111 (and (looking-at "BABYL OPTIONS:")
2112 (search-forward "\n\^_" nil t))
2113 (and (looking-at "\^L")
2114 (search-forward "\n\^_" nil t))
2115 (let ((case-fold-search t))
2116 (looking-at rmail-mmdf-delim1))
2117 (looking-at "From "))))
2118 (goto-char (+ header-end size))
2119 (message "Ignoring invalid Content-Length field")
2120 (sit-for 1 0 t)))
2121 (if (let ((case-fold-search nil))
2122 (re-search-forward
2123 (concat "^[\^_]?\\("
2124 rmail-unix-mail-delimiter
2125 "\\|"
2126 rmail-mmdf-delim1 "\\|"
2127 "^BABYL OPTIONS:\\|"
2128 "\^L\n[01],\\)") nil t))
2129 (goto-char (match-beginning 1))
2130 (goto-char (point-max)))
2131 (setq count (1+ count))
2132 (if quoted-printable-header-field-end
2133 (save-excursion
2134 (unless
2135 (mail-unquote-printable-region header-end (point) nil t t)
2136 (message "Malformed MIME quoted-printable message"))
2137 ;; Change "quoted-printable" to "8bit",
2138 ;; to reflect the decoding we just did.
2139 (goto-char quoted-printable-header-field-end)
2140 (delete-region (point) (search-backward ":"))
2141 (insert ": 8bit")))
2142 (if base64-header-field-end
2143 (save-excursion
2144 (when
2145 (condition-case nil
2146 (progn
2147 (base64-decode-region
2148 (1+ header-end)
2149 (save-excursion
2150 ;; Prevent base64-decode-region
2151 ;; from removing newline characters.
2152 (skip-chars-backward "\n\t ")
2153 (point)))
2155 (error nil))
2156 ;; Change "base64" to "8bit", to reflect the
2157 ;; decoding we just did.
2158 (goto-char base64-header-field-end)
2159 (delete-region (point) (search-backward ":"))
2160 (insert ": 8bit")))))
2162 (save-excursion
2163 (save-restriction
2164 (narrow-to-region start (point))
2165 (goto-char (point-min))
2166 (while (search-forward "\n\^_" nil t); single char
2167 (replace-match "\n^_")))); 2 chars: "^" and "_"
2168 ;; This is for malformed messages that don't end in newline.
2169 ;; There shouldn't be any, but some users say occasionally
2170 ;; there are some.
2171 (or (bolp) (newline))
2172 (insert ?\^_)
2173 (setq last-coding-system-used nil)
2174 (or rmail-enable-mime
2175 (not rmail-enable-multibyte)
2176 (let ((mime-charset
2177 (if (and rmail-decode-mime-charset
2178 (save-excursion
2179 (goto-char start)
2180 (search-forward "\n\n" nil t)
2181 (let ((case-fold-search t))
2182 (re-search-backward
2183 rmail-mime-charset-pattern
2184 start t))))
2185 (intern (downcase (match-string 1))))))
2186 (rmail-decode-region start (point) mime-charset)))
2187 (save-excursion
2188 (goto-char start)
2189 (forward-line 3)
2190 (insert "X-Coding-System: "
2191 (symbol-name last-coding-system-used)
2192 "\n"))
2193 (narrow-to-region (point) (point-max))
2194 (and (= 0 (% count 10))
2195 (message "Converting to Babyl format...%d" count)))
2197 ;; This kludge is because some versions of sendmail.el
2198 ;; insert an extra newline at the beginning that shouldn't
2199 ;; be there. sendmail.el has been fixed, but old versions
2200 ;; may still be in use. -- rms, 7 May 1993.
2201 ((eolp) (delete-char 1))
2202 (t (error "Cannot convert to babyl format")))))
2203 (setq buffer-undo-list nil)
2204 count))
2206 ;; Delete the "From ..." line, creating various other headers with
2207 ;; information from it if they don't already exist. Now puts the
2208 ;; original line into a mail-from: header line for debugging and for
2209 ;; use by the rmail-output function.
2210 (defun rmail-nuke-pinhead-header ()
2211 (save-excursion
2212 (save-restriction
2213 (let ((start (point))
2214 (end (progn
2215 (condition-case ()
2216 (search-forward "\n\n")
2217 (error
2218 (goto-char (point-max))
2219 (insert "\n\n")))
2220 (point)))
2221 has-from has-date)
2222 (narrow-to-region start end)
2223 (let ((case-fold-search t))
2224 (goto-char start)
2225 (setq has-from (search-forward "\nFrom:" nil t))
2226 (goto-char start)
2227 (setq has-date (and (search-forward "\nDate:" nil t) (point)))
2228 (goto-char start))
2229 (let ((case-fold-search nil))
2230 (if (re-search-forward (concat "^" rmail-unix-mail-delimiter) nil t)
2231 (replace-match
2232 (concat
2233 "Mail-from: \\&"
2234 ;; Keep and reformat the date if we don't
2235 ;; have a Date: field.
2236 (if has-date
2238 (concat
2239 "Date: \\2, \\4 \\3 \\9 \\5 "
2241 ;; The timezone could be matched by group 7 or group 10.
2242 ;; If neither of them matched, assume EST, since only
2243 ;; Easterners would be so sloppy.
2244 ;; It's a shame the substitution can't use "\\10".
2245 (cond
2246 ((/= (match-beginning 7) (match-end 7)) "\\7")
2247 ((/= (match-beginning 10) (match-end 10))
2248 (buffer-substring (match-beginning 10)
2249 (match-end 10)))
2250 (t "EST"))
2251 "\n"))
2252 ;; Keep and reformat the sender if we don't
2253 ;; have a From: field.
2254 (if has-from
2256 "From: \\1\n"))
2257 t)))))))
2259 ;;;; *** Rmail Message Formatting and Header Manipulation ***
2261 (defun rmail-reformat-message (beg end)
2262 (goto-char beg)
2263 (forward-line 1)
2264 (if (/= (following-char) ?0)
2265 (error "Bad format in RMAIL file"))
2266 (let ((inhibit-read-only t)
2267 (delta (- (buffer-size) end)))
2268 (delete-char 1)
2269 (insert ?1)
2270 (forward-line 1)
2271 (let ((case-fold-search t))
2272 (while (looking-at "Summary-line:\\|Mail-From:")
2273 (forward-line 1)))
2274 (if (looking-at "\\*\\*\\* EOOH \\*\\*\\*\n")
2275 (delete-region (point)
2276 (progn (forward-line 1) (point))))
2277 (let ((str (buffer-substring (point)
2278 (save-excursion (search-forward "\n\n" end 'move)
2279 (point)))))
2280 (insert str "*** EOOH ***\n")
2281 (narrow-to-region (point) (- (buffer-size) delta)))
2282 (goto-char (point-min))
2283 (if rmail-message-filter (funcall rmail-message-filter))
2284 (if (or rmail-displayed-headers rmail-ignored-headers)
2285 (rmail-clear-headers))))
2287 (defun rmail-clear-headers (&optional ignored-headers)
2288 "Delete all header fields that Rmail should not show.
2289 If the optional argument IGNORED-HEADERS is non-nil,
2290 delete all header fields whose names match that regexp.
2291 Otherwise, if `rmail-displayed-headers' is non-nil,
2292 delete all header fields *except* those whose names match that regexp.
2293 Otherwise, delete all header fields whose names match `rmail-ignored-headers'
2294 unless they also match `rmail-nonignored-headers'."
2295 (when (search-forward "\n\n" nil t)
2296 (forward-char -1)
2297 (let ((case-fold-search t)
2298 (buffer-read-only nil))
2299 (if (and rmail-displayed-headers (null ignored-headers))
2300 (save-restriction
2301 (narrow-to-region (point-min) (point))
2302 (let (lim next)
2303 (goto-char (point-min))
2304 (while (and (not (eobp))
2305 (save-excursion
2306 (if (re-search-forward "\n[^ \t]" nil t)
2307 (setq lim (match-beginning 0)
2308 next (1+ lim))
2309 (setq lim nil next (point-max)))))
2310 (if (save-excursion
2311 (re-search-forward rmail-displayed-headers lim t))
2312 (goto-char next)
2313 (delete-region (point) next))))
2314 (goto-char (point-min)))
2315 (or ignored-headers (setq ignored-headers rmail-ignored-headers))
2316 (save-restriction
2317 (narrow-to-region (point-min) (point))
2318 (goto-char (point-min))
2319 (while (and ignored-headers
2320 (re-search-forward ignored-headers nil t))
2321 (beginning-of-line)
2322 (if (looking-at rmail-nonignored-headers)
2323 (forward-line 1)
2324 (delete-region (point)
2325 (save-excursion
2326 (if (re-search-forward "\n[^ \t]" nil t)
2327 (1- (point))
2328 (point-max)))))))))))
2330 (defun rmail-msg-is-pruned ()
2331 (rmail-maybe-set-message-counters)
2332 (save-restriction
2333 (narrow-to-region (rmail-msgbeg rmail-current-message) (point-max))
2334 (save-excursion
2335 (goto-char (point-min))
2336 (forward-line 1)
2337 (= (following-char) ?1))))
2339 (defun rmail-msg-restore-non-pruned-header ()
2340 (let ((old-point (point))
2341 new-point
2342 new-start
2343 (inhibit-read-only t))
2344 (save-excursion
2345 (narrow-to-region (rmail-msgbeg rmail-current-message) (point-max))
2346 (goto-char (point-min))
2347 (forward-line 1)
2348 ;; Change 1 to 0.
2349 (delete-char 1)
2350 (insert ?0)
2351 ;; Insert new EOOH line at the proper place.
2352 (forward-line 1)
2353 (let ((case-fold-search t))
2354 (while (looking-at "Summary-Line:\\|Mail-From:")
2355 (forward-line 1)))
2356 (insert "*** EOOH ***\n")
2357 (setq new-start (point))
2358 ;; Delete the old reformatted header.
2359 (forward-char -1)
2360 (search-forward "\n*** EOOH ***\n")
2361 (forward-line -1)
2362 (let ((start (point)))
2363 (search-forward "\n\n")
2364 (if (and (<= start old-point)
2365 (<= old-point (point)))
2366 (setq new-point new-start))
2367 (delete-region start (point)))
2368 ;; Narrow to after the new EOOH line.
2369 (narrow-to-region new-start (point-max)))
2370 (if new-point
2371 (goto-char new-point))))
2373 (defun rmail-msg-prune-header ()
2374 (let ((new-point
2375 (= (point) (point-min))))
2376 (save-excursion
2377 (narrow-to-region (rmail-msgbeg rmail-current-message) (point-max))
2378 (rmail-reformat-message (point-min) (point-max)))
2379 (if new-point
2380 (goto-char (point-min)))))
2382 (defun rmail-toggle-header (&optional arg)
2383 "Show original message header if pruned header currently shown, or vice versa.
2384 With argument ARG, show the message header pruned if ARG is greater than zero;
2385 otherwise, show it in full."
2386 (interactive "P")
2387 (let* ((pruned (with-current-buffer rmail-buffer
2388 (rmail-msg-is-pruned)))
2389 (prune (if arg
2390 (> (prefix-numeric-value arg) 0)
2391 (not pruned))))
2392 (if (eq pruned prune)
2394 (set-buffer rmail-buffer)
2395 (rmail-maybe-set-message-counters)
2396 (if rmail-enable-mime
2397 (let ((buffer-read-only nil))
2398 (if pruned
2399 (rmail-msg-restore-non-pruned-header)
2400 (rmail-msg-prune-header))
2401 (funcall rmail-show-mime-function))
2402 (let* ((buffer-read-only nil)
2403 (window (get-buffer-window (current-buffer)))
2404 (at-point-min (= (point) (point-min)))
2405 (all-headers-visible (= (window-start window) (point-min)))
2406 (on-header
2407 (save-excursion
2408 (and (not (search-backward "\n\n" nil t))
2409 (progn
2410 (end-of-line)
2411 (re-search-backward "^[-A-Za-z0-9]+:" nil t))
2412 (match-string 0))))
2413 (old-screen-line
2414 (rmail-count-screen-lines (window-start window) (point))))
2415 (if pruned
2416 (rmail-msg-restore-non-pruned-header)
2417 (rmail-msg-prune-header))
2418 (cond (at-point-min
2419 (goto-char (point-min)))
2420 (on-header
2421 (goto-char (point-min))
2422 (search-forward "\n\n")
2423 (or (re-search-backward
2424 (concat "^" (regexp-quote on-header)) nil t)
2425 (goto-char (point-min))))
2427 (save-selected-window
2428 (select-window window)
2429 (recenter old-screen-line)
2430 (if (and all-headers-visible
2431 (not (= (window-start) (point-min))))
2432 (recenter (- (window-height) 2))))))))
2433 (rmail-highlight-headers))))
2435 (defun rmail-narrow-to-non-pruned-header ()
2436 "Narrow to the whole (original) header of the current message."
2437 (let (start end)
2438 (narrow-to-region (rmail-msgbeg rmail-current-message) (point-max))
2439 (goto-char (point-min))
2440 (forward-line 1)
2441 (if (= (following-char) ?1)
2442 (progn
2443 (forward-line 1)
2444 (setq start (point))
2445 (search-forward "*** EOOH ***\n")
2446 (setq end (match-beginning 0)))
2447 (forward-line 2)
2448 (setq start (point))
2449 (search-forward "\n\n")
2450 (setq end (1- (point))))
2451 (narrow-to-region start end)
2452 (goto-char start)))
2454 ;; Lifted from repos-count-screen-lines.
2455 ;; Return number of screen lines between START and END.
2456 (defun rmail-count-screen-lines (start end)
2457 (save-excursion
2458 (save-restriction
2459 (narrow-to-region start end)
2460 (goto-char (point-min))
2461 (vertical-motion (- (point-max) (point-min))))))
2463 ;;;; *** Rmail Attributes and Keywords ***
2465 ;; Make a string describing current message's attributes and keywords
2466 ;; and set it up as the name of a minor mode
2467 ;; so it will appear in the mode line.
2468 (defun rmail-display-labels ()
2469 (let ((blurb "") (beg (point-min-marker)) (end (point-max-marker)))
2470 (save-excursion
2471 (unwind-protect
2472 (progn
2473 (widen)
2474 (goto-char (rmail-msgbeg rmail-current-message))
2475 (forward-line 1)
2476 (if (looking-at "[01],")
2477 (progn
2478 (narrow-to-region (point) (progn (end-of-line) (point)))
2479 ;; Truly valid BABYL format requires a space before each
2480 ;; attribute or keyword name. Put them in if missing.
2481 (let (buffer-read-only)
2482 (goto-char (point-min))
2483 (while (search-forward "," nil t)
2484 (or (looking-at "[ ,]") (eobp)
2485 (insert " "))))
2486 (goto-char (point-max))
2487 (if (search-backward ",," nil 'move)
2488 (progn
2489 (if (> (point) (1+ (point-min)))
2490 (setq blurb (buffer-substring (+ 1 (point-min)) (point))))
2491 (if (> (- (point-max) (point)) 2)
2492 (setq blurb
2493 (concat blurb
2495 (buffer-substring (+ (point) 3)
2496 (1- (point-max)))))))))))
2497 ;; Note: we don't use save-restriction because that does not work right
2498 ;; if changes are made outside the saved restriction
2499 ;; before that restriction is restored.
2500 (narrow-to-region beg end)
2501 (set-marker beg nil)
2502 (set-marker end nil)))
2503 (while (string-match " +," blurb)
2504 (setq blurb (concat (substring blurb 0 (match-beginning 0)) ","
2505 (substring blurb (match-end 0)))))
2506 (while (string-match ", +" blurb)
2507 (setq blurb (concat (substring blurb 0 (match-beginning 0)) ","
2508 (substring blurb (match-end 0)))))
2509 (setq mode-line-process
2510 (format " %d/%d%s"
2511 rmail-current-message rmail-total-messages blurb))
2512 ;; If rmail-enable-mime is non-nil, we may have to update
2513 ;; `mode-line-process' of rmail-view-buffer too.
2514 (if (and rmail-enable-mime
2515 (not (eq (current-buffer) rmail-view-buffer))
2516 (buffer-live-p rmail-view-buffer))
2517 (let ((mlp mode-line-process))
2518 (with-current-buffer rmail-view-buffer
2519 (setq mode-line-process mlp))))))
2521 ;; Turn an attribute of a message on or off according to STATE.
2522 ;; ATTR is the name of the attribute, as a string.
2523 ;; MSGNUM is message number to change; nil means current message.
2524 (defun rmail-set-attribute (attr state &optional msgnum)
2525 (set-buffer rmail-buffer)
2526 (let ((omax (point-max-marker))
2527 (omin (point-min-marker))
2528 (buffer-read-only nil))
2529 (or msgnum (setq msgnum rmail-current-message))
2530 (if (> msgnum 0)
2531 (unwind-protect
2532 (save-excursion
2533 (widen)
2534 (goto-char (+ 3 (rmail-msgbeg msgnum)))
2535 (let ((curstate
2536 (not
2537 (null (search-backward (concat ", " attr ",")
2538 (prog1 (point) (end-of-line)) t)))))
2539 (or (eq curstate (not (not state)))
2540 (if curstate
2541 (delete-region (point) (1- (match-end 0)))
2542 (beginning-of-line)
2543 (forward-char 2)
2544 (insert " " attr ","))))
2545 (if (string= attr "deleted")
2546 (rmail-set-message-deleted-p msgnum state)))
2547 ;; Note: we don't use save-restriction because that does not work right
2548 ;; if changes are made outside the saved restriction
2549 ;; before that restriction is restored.
2550 (narrow-to-region omin omax)
2551 (set-marker omin nil)
2552 (set-marker omax nil)
2553 (if (= msgnum rmail-current-message)
2554 (rmail-display-labels))))))
2556 ;; Return t if the attributes/keywords line of msg number MSG
2557 ;; contains a match for the regexp LABELS.
2558 (defun rmail-message-labels-p (msg labels)
2559 (save-excursion
2560 (save-restriction
2561 (widen)
2562 (goto-char (rmail-msgbeg msg))
2563 (forward-char 3)
2564 (re-search-backward labels (prog1 (point) (end-of-line)) t))))
2566 ;;;; *** Rmail Message Selection And Support ***
2568 (defun rmail-msgend (n)
2569 (marker-position (aref rmail-message-vector (1+ n))))
2571 (defun rmail-msgbeg (n)
2572 (marker-position (aref rmail-message-vector n)))
2574 (defun rmail-widen-to-current-msgbeg (function)
2575 "Call FUNCTION with point at start of internal data of current message.
2576 Assumes that bounds were previously narrowed to display the message in Rmail.
2577 The bounds are widened enough to move point where desired, then narrowed
2578 again afterward.
2580 FUNCTION may not change the visible text of the message, but it may
2581 change the invisible header text."
2582 (save-excursion
2583 (unwind-protect
2584 (progn
2585 (narrow-to-region (rmail-msgbeg rmail-current-message)
2586 (point-max))
2587 (goto-char (point-min))
2588 (funcall function))
2589 ;; Note: we don't use save-restriction because that does not work right
2590 ;; if changes are made outside the saved restriction
2591 ;; before that restriction is restored.
2592 (narrow-to-region (rmail-msgbeg rmail-current-message)
2593 (rmail-msgend rmail-current-message)))))
2595 (defun rmail-forget-messages ()
2596 (unwind-protect
2597 (if (vectorp rmail-message-vector)
2598 (let* ((i 0)
2599 (v rmail-message-vector)
2600 (n (length v)))
2601 (while (< i n)
2602 (move-marker (aref v i) nil)
2603 (setq i (1+ i)))))
2604 (setq rmail-message-vector nil)
2605 (setq rmail-msgref-vector nil)
2606 (setq rmail-deleted-vector nil)))
2608 (defun rmail-maybe-set-message-counters ()
2609 (if (not (and rmail-deleted-vector
2610 rmail-message-vector
2611 rmail-current-message
2612 rmail-total-messages))
2613 (rmail-set-message-counters)))
2615 (defun rmail-count-new-messages (&optional nomsg)
2616 (let* ((case-fold-search nil)
2617 (total-messages 0)
2618 (messages-head nil)
2619 (deleted-head nil))
2620 (or nomsg (message "Counting new messages..."))
2621 (goto-char (point-max))
2622 ;; Put at the end of messages-head
2623 ;; the entry for message N+1, which marks
2624 ;; the end of message N. (N = number of messages).
2625 (search-backward "\n\^_")
2626 (forward-char 1)
2627 (setq messages-head (list (point-marker)))
2628 (rmail-set-message-counters-counter (point-min))
2629 (setq rmail-current-message (1+ rmail-total-messages))
2630 (setq rmail-total-messages
2631 (+ rmail-total-messages total-messages))
2632 (setq rmail-message-vector
2633 (vconcat rmail-message-vector (cdr messages-head)))
2634 (aset rmail-message-vector
2635 rmail-current-message (car messages-head))
2636 (setq rmail-deleted-vector
2637 (concat rmail-deleted-vector deleted-head))
2638 (setq rmail-summary-vector
2639 (vconcat rmail-summary-vector (make-vector total-messages nil)))
2640 (setq rmail-msgref-vector
2641 (vconcat rmail-msgref-vector (make-vector total-messages nil)))
2642 ;; Fill in the new elements of rmail-msgref-vector.
2643 (let ((i (1+ (- rmail-total-messages total-messages))))
2644 (while (<= i rmail-total-messages)
2645 (aset rmail-msgref-vector i (list i))
2646 (setq i (1+ i))))
2647 (goto-char (point-min))
2648 (or nomsg (message "Counting new messages...done (%d)" total-messages))))
2650 (defun rmail-set-message-counters ()
2651 (rmail-forget-messages)
2652 (save-excursion
2653 (save-restriction
2654 (widen)
2655 (let* ((point-save (point))
2656 (total-messages 0)
2657 (messages-after-point)
2658 (case-fold-search nil)
2659 (messages-head nil)
2660 (deleted-head nil))
2661 (message "Counting messages...")
2662 (goto-char (point-max))
2663 ;; Put at the end of messages-head
2664 ;; the entry for message N+1, which marks
2665 ;; the end of message N. (N = number of messages).
2666 (search-backward "\n\^_" nil t)
2667 (if (/= (point) (point-max)) (forward-char 1))
2668 (setq messages-head (list (point-marker)))
2669 (rmail-set-message-counters-counter (min (point) point-save))
2670 (setq messages-after-point total-messages)
2671 (rmail-set-message-counters-counter)
2672 (setq rmail-total-messages total-messages)
2673 (setq rmail-current-message
2674 (min total-messages
2675 (max 1 (- total-messages messages-after-point))))
2676 (setq rmail-message-vector
2677 (apply 'vector (cons (point-min-marker) messages-head))
2678 rmail-deleted-vector (concat "0" deleted-head)
2679 rmail-summary-vector (make-vector rmail-total-messages nil)
2680 rmail-msgref-vector (make-vector (1+ rmail-total-messages) nil))
2681 (let ((i 0))
2682 (while (<= i rmail-total-messages)
2683 (aset rmail-msgref-vector i (list i))
2684 (setq i (1+ i))))
2685 (message "Counting messages...done")))))
2687 (defun rmail-set-message-counters-counter (&optional stop)
2688 (let ((start (point))
2689 next)
2690 (while (search-backward "\n\^_\^L" stop t)
2691 ;; Detect messages that have been added with DOS line endings and
2692 ;; convert the line endings for such messages.
2693 (setq next (point))
2694 (if (looking-at "\n\^_\^L\r\n")
2695 (let ((buffer-read-only nil)
2696 (buffer-undo t))
2697 (message "Counting messages...(converting line endings)")
2698 (save-excursion
2699 (goto-char start)
2700 (while (search-backward "\r\n" next t)
2701 (delete-char 1)))))
2702 (setq start next)
2703 (forward-char 1)
2704 (setq messages-head (cons (point-marker) messages-head))
2705 (save-excursion
2706 (setq deleted-head
2707 (cons (if (search-backward ", deleted,"
2708 (prog1 (point)
2709 (forward-line 2))
2711 ?D ?\ )
2712 deleted-head)))
2713 (if (zerop (% (setq total-messages (1+ total-messages)) 20))
2714 (message "Counting messages...%d" total-messages)))))
2716 (defun rmail-beginning-of-message ()
2717 "Show current message starting from the beginning."
2718 (interactive)
2719 (let ((rmail-show-message-hook
2720 (list (function (lambda ()
2721 (goto-char (point-min)))))))
2722 (rmail-show-message rmail-current-message)))
2724 (defun rmail-end-of-message ()
2725 "Show bottom of current message."
2726 (interactive)
2727 (let ((rmail-show-message-hook
2728 (list (function (lambda ()
2729 (goto-char (point-max))
2730 (recenter (1- (window-height))))))))
2731 (rmail-show-message rmail-current-message)))
2733 (defun rmail-unknown-mail-followup-to ()
2734 "Handle a \"Mail-Followup-To\" header field with an unknown mailing list.
2735 Ask the user whether to add that list name to `mail-mailing-lists'."
2736 (save-restriction
2737 (rmail-narrow-to-non-pruned-header)
2738 (let ((mail-followup-to (mail-fetch-field "mail-followup-to" nil t)))
2739 (when mail-followup-to
2740 (let ((addresses
2741 (split-string
2742 (mail-strip-quoted-names mail-followup-to)
2743 ",[[:space:]]+" t)))
2744 (dolist (addr addresses)
2745 (when (and (not (member addr mail-mailing-lists))
2746 (not
2747 ;; taken from rmailsum.el
2748 (string-match
2749 (or rmail-user-mail-address-regexp
2750 (concat "^\\("
2751 (regexp-quote (user-login-name))
2752 "\\($\\|@\\)\\|"
2753 (regexp-quote
2754 (or user-mail-address
2755 (concat (user-login-name) "@"
2756 (or mail-host-address
2757 (system-name)))))
2758 "\\>\\)"))
2759 addr))
2760 (y-or-n-p
2761 (format "Add `%s' to `mail-mailing-lists'? "
2762 addr)))
2763 (customize-save-variable 'mail-mailing-lists
2764 (cons addr mail-mailing-lists)))))))))
2766 (defun rmail-show-message (&optional n no-summary)
2767 "Show message number N (prefix argument), counting from start of file.
2768 If summary buffer is currently displayed, update current message there also."
2769 (interactive "p")
2770 (or (eq major-mode 'rmail-mode)
2771 (switch-to-buffer rmail-buffer))
2772 (rmail-maybe-set-message-counters)
2773 (widen)
2774 (if (zerop rmail-total-messages)
2775 (progn (narrow-to-region (point-min) (1- (point-max)))
2776 (goto-char (point-min))
2777 (setq mode-line-process nil))
2778 (let (blurb coding-system)
2779 (if (not n)
2780 (setq n rmail-current-message)
2781 (cond ((<= n 0)
2782 (setq n 1
2783 rmail-current-message 1
2784 blurb "No previous message"))
2785 ((> n rmail-total-messages)
2786 (setq n rmail-total-messages
2787 rmail-current-message rmail-total-messages
2788 blurb "No following message"))
2790 (setq rmail-current-message n))))
2791 (let ((beg (rmail-msgbeg n)))
2792 (goto-char beg)
2793 (forward-line 1)
2794 (save-excursion
2795 (let ((end (rmail-msgend n)))
2796 (save-restriction
2797 (if (prog1 (= (following-char) ?0)
2798 (forward-line 2)
2799 ;; If there's a Summary-line in the (otherwise empty)
2800 ;; header, we didn't yet get past the EOOH line.
2801 (if (looking-at "^\\*\\*\\* EOOH \\*\\*\\*\n")
2802 (forward-line 1))
2803 (narrow-to-region (point) end))
2804 (rfc822-goto-eoh)
2805 (search-forward "\n*** EOOH ***\n" end t))
2806 (narrow-to-region beg (point))
2807 (goto-char (point-min))
2808 (if (re-search-forward "^X-Coding-System: *\\(.*\\)$" nil t)
2809 (let ((coding-system (intern (match-string 1))))
2810 (condition-case nil
2811 (progn
2812 (check-coding-system coding-system)
2813 (setq buffer-file-coding-system coding-system))
2814 (error
2815 (setq buffer-file-coding-system nil))))
2816 (setq buffer-file-coding-system nil)))))
2817 ;; Clear the "unseen" attribute when we show a message.
2818 (rmail-set-attribute "unseen" nil)
2819 (let ((end (rmail-msgend n)))
2820 ;; Reformat the header, or else find the reformatted header.
2821 (if (= (following-char) ?0)
2822 (rmail-reformat-message beg end)
2823 (search-forward "\n*** EOOH ***\n" end t)
2824 (narrow-to-region (point) end)))
2825 (goto-char (point-min))
2826 (walk-windows
2827 (function (lambda (window)
2828 (if (eq (window-buffer window) (current-buffer))
2829 (set-window-point window (point)))))
2830 nil t)
2831 (rmail-display-labels)
2832 (if (eq rmail-enable-mime t)
2833 (funcall rmail-show-mime-function)
2834 (setq rmail-view-buffer rmail-buffer))
2835 (when mail-mailing-lists
2836 (rmail-unknown-mail-followup-to))
2837 (rmail-highlight-headers)
2838 (if transient-mark-mode (deactivate-mark))
2839 (run-hooks 'rmail-show-message-hook)
2840 ;; If there is a summary buffer, try to move to this message
2841 ;; in that buffer. But don't complain if this message
2842 ;; is not mentioned in the summary.
2843 ;; Don't do this at all if we were called on behalf
2844 ;; of cursor motion in the summary buffer.
2845 (and (rmail-summary-exists) (not no-summary)
2846 (let ((curr-msg rmail-current-message))
2847 (rmail-select-summary
2848 (rmail-summary-goto-msg curr-msg t t))))
2849 (with-current-buffer rmail-buffer
2850 (rmail-auto-file))
2851 (if blurb
2852 (message blurb))))))
2854 (defun rmail-redecode-body (coding &optional raw)
2855 "Decode the body of the current message using coding system CODING.
2856 This is useful with mail messages that have malformed or missing
2857 charset= headers.
2859 This function assumes that the current message is already decoded
2860 and displayed in the RMAIL buffer, but the coding system used to
2861 decode it was incorrect. It then encodes the message back to its
2862 original form, and decodes it again, using the coding system CODING.
2864 Optional argument RAW, if non-nil, means don't encode the message
2865 before decoding it with the new CODING. This is useful if the current
2866 message text was produced by some function which invokes `insert',
2867 since `insert' leaves unibyte character codes 128 through 255 unconverted
2868 to multibyte. One example of such a situation is when the text was
2869 produced by `base64-decode-region'.
2871 Interactively, invoke the function with a prefix argument to set RAW
2872 non-nil.
2874 Note that if Emacs erroneously auto-detected one of the iso-2022
2875 encodings in the message, this function might fail because the escape
2876 sequences that switch between character sets and also single-shift and
2877 locking-shift codes are impossible to recover. This function is meant
2878 to be used to fix messages encoded with 8-bit encodings, such as
2879 iso-8859, koi8-r, etc."
2880 (interactive "zCoding system for re-decoding this message: ")
2881 (when (not rmail-enable-mime)
2882 (or (eq major-mode 'rmail-mode)
2883 (switch-to-buffer rmail-buffer))
2884 (save-excursion
2885 (let ((pruned (rmail-msg-is-pruned))
2886 (raw (or raw current-prefix-arg)))
2887 (unwind-protect
2888 (let ((msgbeg (rmail-msgbeg rmail-current-message))
2889 (msgend (rmail-msgend rmail-current-message))
2890 x-coding-header)
2891 ;; We need the message headers pruned (we later restore
2892 ;; the pruned stat to what it was, see the end of
2893 ;; unwind-protect form).
2894 (or pruned
2895 (rmail-toggle-header 1))
2896 (narrow-to-region msgbeg msgend)
2897 (goto-char (point-min))
2898 (when (search-forward "\n*** EOOH ***\n" (point-max) t)
2899 (narrow-to-region msgbeg (point)))
2900 (goto-char (point-min))
2901 (if (re-search-forward "^X-Coding-System: *\\(.*\\)$" nil t)
2902 (let ((old-coding (intern (match-string 1)))
2903 (buffer-read-only nil))
2904 (check-coding-system old-coding)
2905 ;; Make sure the new coding system uses the same EOL
2906 ;; conversion, to prevent ^M characters from popping
2907 ;; up all over the place.
2908 (setq coding
2909 (coding-system-change-eol-conversion
2910 coding
2911 (coding-system-eol-type old-coding)))
2912 ;; If old-coding is `undecided', encode-coding-region
2913 ;; will not encode the text at all. Find a proper
2914 ;; non-trivial encoding to use.
2915 (if (memq (coding-system-base old-coding) '(nil undecided))
2916 (setq old-coding
2917 (car (find-coding-systems-region msgbeg msgend))))
2918 (setq x-coding-header (point-marker))
2919 (narrow-to-region msgbeg msgend)
2920 (and (null raw)
2921 ;; If old and new encoding are the same, it
2922 ;; clearly doesn't make sense to encode.
2923 (not (coding-system-equal
2924 (coding-system-base old-coding)
2925 (coding-system-base coding)))
2926 ;; If the body includes only eight-bit-*
2927 ;; characters, encoding might fail, e.g. with
2928 ;; UTF-8, and isn't needed anyway.
2929 (> (length (delq 'ascii
2930 (delq 'eight-bit-graphic
2931 (delq 'eight-bit-control
2932 (find-charset-region
2933 msgbeg msgend)))))
2935 (encode-coding-region (point) msgend old-coding))
2936 (decode-coding-region (point) msgend coding)
2937 (setq last-coding-system-used coding)
2938 ;; Rewrite the coding-system header according
2939 ;; to what we did.
2940 (goto-char x-coding-header)
2941 (delete-region (point)
2942 (save-excursion
2943 (beginning-of-line)
2944 (point)))
2945 (insert "X-Coding-System: "
2946 (symbol-name last-coding-system-used))
2947 (set-marker x-coding-header nil)
2948 (rmail-show-message))
2949 (error "No X-Coding-System header found")))
2950 (or pruned
2951 (rmail-toggle-header 0)))))))
2953 ;; Find all occurrences of certain fields, and highlight them.
2954 (defun rmail-highlight-headers ()
2955 ;; Do this only if the system supports faces.
2956 (if (and (fboundp 'internal-find-face)
2957 rmail-highlighted-headers)
2958 (save-excursion
2959 (search-forward "\n\n" nil 'move)
2960 (save-restriction
2961 (narrow-to-region (point-min) (point))
2962 (let ((case-fold-search t)
2963 (inhibit-read-only t)
2964 ;; Highlight with boldface if that is available.
2965 ;; Otherwise use the `highlight' face.
2966 (face (or rmail-highlight-face
2967 (if (face-differs-from-default-p 'bold)
2968 'bold 'highlight)))
2969 ;; List of overlays to reuse.
2970 (overlays rmail-overlay-list))
2971 (goto-char (point-min))
2972 (while (re-search-forward rmail-highlighted-headers nil t)
2973 (skip-chars-forward " \t")
2974 (let ((beg (point))
2975 overlay)
2976 (while (progn (forward-line 1)
2977 (looking-at "[ \t]")))
2978 ;; Back up over newline, then trailing spaces or tabs
2979 (forward-char -1)
2980 (while (member (preceding-char) '(? ?\t))
2981 (forward-char -1))
2982 (if overlays
2983 ;; Reuse an overlay we already have.
2984 (progn
2985 (setq overlay (car overlays)
2986 overlays (cdr overlays))
2987 (overlay-put overlay 'face face)
2988 (move-overlay overlay beg (point)))
2989 ;; Make a new overlay and add it to
2990 ;; rmail-overlay-list.
2991 (setq overlay (make-overlay beg (point)))
2992 (overlay-put overlay 'face face)
2993 (setq rmail-overlay-list
2994 (cons overlay rmail-overlay-list))))))))))
2996 (defun rmail-auto-file ()
2997 "Automatically move a message into a sub-folder based on criteria.
2998 Called when a new message is displayed."
2999 (if (or (rmail-message-labels-p rmail-current-message "filed")
3000 (not (string= (buffer-file-name)
3001 (expand-file-name rmail-file-name))))
3002 ;; Do nothing if it's already been filed.
3004 ;; Find out some basics (common fields)
3005 (let ((from (mail-fetch-field "from"))
3006 (subj (mail-fetch-field "subject"))
3007 (to (concat (mail-fetch-field "to") "," (mail-fetch-field "cc")))
3008 (d rmail-automatic-folder-directives)
3009 (directive-loop nil)
3010 (folder nil))
3011 (while d
3012 (setq folder (car (car d))
3013 directive-loop (cdr (car d)))
3014 (while (and (car directive-loop)
3015 (let ((f (cond
3016 ((string= (car directive-loop) "from") from)
3017 ((string= (car directive-loop) "to") to)
3018 ((string= (car directive-loop) "subject") subj)
3019 (t (mail-fetch-field (car directive-loop))))))
3020 (and f (string-match (car (cdr directive-loop)) f))))
3021 (setq directive-loop (cdr (cdr directive-loop))))
3022 ;; If there are no directives left, then it was a complete match.
3023 (if (null directive-loop)
3024 (if (null folder)
3025 (rmail-delete-forward)
3026 (if (string= "/dev/null" folder)
3027 (rmail-delete-message)
3028 (rmail-output-to-rmail-file folder 1 t)
3029 (setq d nil))))
3030 (setq d (cdr d))))))
3032 (defun rmail-next-message (n)
3033 "Show following message whether deleted or not.
3034 With prefix arg N, moves forward N messages, or backward if N is negative."
3035 (interactive "p")
3036 (set-buffer rmail-buffer)
3037 (rmail-maybe-set-message-counters)
3038 (rmail-show-message (+ rmail-current-message n)))
3040 (defun rmail-previous-message (n)
3041 "Show previous message whether deleted or not.
3042 With prefix arg N, moves backward N messages, or forward if N is negative."
3043 (interactive "p")
3044 (rmail-next-message (- n)))
3046 (defun rmail-next-undeleted-message (n)
3047 "Show following non-deleted message.
3048 With prefix arg N, moves forward N non-deleted messages,
3049 or backward if N is negative.
3051 Returns t if a new message is being shown, nil otherwise."
3052 (interactive "p")
3053 (set-buffer rmail-buffer)
3054 (rmail-maybe-set-message-counters)
3055 (let ((lastwin rmail-current-message)
3056 (current rmail-current-message))
3057 (while (and (> n 0) (< current rmail-total-messages))
3058 (setq current (1+ current))
3059 (if (not (rmail-message-deleted-p current))
3060 (setq lastwin current n (1- n))))
3061 (while (and (< n 0) (> current 1))
3062 (setq current (1- current))
3063 (if (not (rmail-message-deleted-p current))
3064 (setq lastwin current n (1+ n))))
3065 (if (/= lastwin rmail-current-message)
3066 (progn (rmail-show-message lastwin)
3068 (if (< n 0)
3069 (message "No previous nondeleted message"))
3070 (if (> n 0)
3071 (message "No following nondeleted message"))
3072 nil)))
3074 (defun rmail-previous-undeleted-message (n)
3075 "Show previous non-deleted message.
3076 With prefix argument N, moves backward N non-deleted messages,
3077 or forward if N is negative."
3078 (interactive "p")
3079 (rmail-next-undeleted-message (- n)))
3081 (defun rmail-first-message ()
3082 "Show first message in file."
3083 (interactive)
3084 (rmail-maybe-set-message-counters)
3085 (rmail-show-message 1))
3087 (defun rmail-last-message ()
3088 "Show last message in file."
3089 (interactive)
3090 (rmail-maybe-set-message-counters)
3091 (rmail-show-message rmail-total-messages))
3093 (defun rmail-what-message ()
3094 (let ((where (point))
3095 (low 1)
3096 (high rmail-total-messages)
3097 (mid (/ rmail-total-messages 2)))
3098 (while (> (- high low) 1)
3099 (if (>= where (rmail-msgbeg mid))
3100 (setq low mid)
3101 (setq high mid))
3102 (setq mid (+ low (/ (- high low) 2))))
3103 (if (>= where (rmail-msgbeg high)) high low)))
3105 (defun rmail-message-recipients-p (msg recipients &optional primary-only)
3106 (save-restriction
3107 (goto-char (rmail-msgbeg msg))
3108 (search-forward "\n*** EOOH ***\n")
3109 (narrow-to-region (point) (progn (search-forward "\n\n") (point)))
3110 (or (string-match recipients (or (mail-fetch-field "To") ""))
3111 (string-match recipients (or (mail-fetch-field "From") ""))
3112 (if (not primary-only)
3113 (string-match recipients (or (mail-fetch-field "Cc") ""))))))
3115 (defun rmail-message-regexp-p (n regexp)
3116 "Return t, if for message number N, regexp REGEXP matches in the header."
3117 (let ((beg (rmail-msgbeg n))
3118 (end (rmail-msgend n)))
3119 (goto-char beg)
3120 (forward-line 1)
3121 (save-excursion
3122 (save-restriction
3123 (if (prog1 (= (following-char) ?0)
3124 (forward-line 2)
3125 ;; If there's a Summary-line in the (otherwise empty)
3126 ;; header, we didn't yet get past the EOOH line.
3127 (when (looking-at "^\\*\\*\\* EOOH \\*\\*\\*\n")
3128 (forward-line 1))
3129 (setq beg (point))
3130 (narrow-to-region (point) end))
3131 (progn
3132 (rfc822-goto-eoh)
3133 (setq end (point)))
3134 (setq beg (point))
3135 (search-forward "\n*** EOOH ***\n" end t)
3136 (setq end (1+ (match-beginning 0)))))
3137 (goto-char beg)
3138 (if rmail-enable-mime
3139 (funcall rmail-search-mime-header-function n regexp end)
3140 (re-search-forward regexp end t)))))
3142 (defun rmail-search-message (msg regexp)
3143 "Return non-nil, if for message number MSG, regexp REGEXP matches."
3144 (goto-char (rmail-msgbeg msg))
3145 (if rmail-enable-mime
3146 (funcall rmail-search-mime-message-function msg regexp)
3147 (re-search-forward regexp (rmail-msgend msg) t)))
3149 (defvar rmail-search-last-regexp nil)
3150 (defun rmail-search (regexp &optional n)
3151 "Show message containing next match for REGEXP (but not the current msg).
3152 Prefix argument gives repeat count; negative argument means search
3153 backwards (through earlier messages).
3154 Interactively, empty argument means use same regexp used last time."
3155 (interactive
3156 (let* ((reversep (< (prefix-numeric-value current-prefix-arg) 0))
3157 (prompt
3158 (concat (if reversep "Reverse " "") "Rmail search (regexp"))
3159 regexp)
3160 (setq prompt
3161 (concat prompt
3162 (if rmail-search-last-regexp
3163 (concat ", default "
3164 rmail-search-last-regexp "): ")
3165 "): ")))
3166 (setq regexp (read-string prompt))
3167 (cond ((not (equal regexp ""))
3168 (setq rmail-search-last-regexp regexp))
3169 ((not rmail-search-last-regexp)
3170 (error "No previous Rmail search string")))
3171 (list rmail-search-last-regexp
3172 (prefix-numeric-value current-prefix-arg))))
3173 (or n (setq n 1))
3174 (message "%sRmail search for %s..."
3175 (if (< n 0) "Reverse " "")
3176 regexp)
3177 (set-buffer rmail-buffer)
3178 (rmail-maybe-set-message-counters)
3179 (let ((omin (point-min))
3180 (omax (point-max))
3181 (opoint (point))
3183 (reversep (< n 0))
3184 (msg rmail-current-message))
3185 (unwind-protect
3186 (progn
3187 (widen)
3188 (while (/= n 0)
3189 ;; Check messages one by one, advancing message number up or down
3190 ;; but searching forward through each message.
3191 (if reversep
3192 (while (and (null win) (> msg 1))
3193 (setq msg (1- msg)
3194 win (rmail-search-message msg regexp)))
3195 (while (and (null win) (< msg rmail-total-messages))
3196 (setq msg (1+ msg)
3197 win (rmail-search-message msg regexp))))
3198 (setq n (+ n (if reversep 1 -1)))))
3199 (if win
3200 (progn
3201 (rmail-show-message msg)
3202 ;; Search forward (if this is a normal search) or backward
3203 ;; (if this is a reverse search) through this message to
3204 ;; position point. This search may fail because REGEXP
3205 ;; was found in the hidden portion of this message. In
3206 ;; that case, move point to the beginning of visible
3207 ;; portion.
3208 (if reversep
3209 (progn
3210 (goto-char (point-max))
3211 (re-search-backward regexp nil 'move))
3212 (goto-char (point-min))
3213 (re-search-forward regexp nil t))
3214 (message "%sRmail search for %s...done"
3215 (if reversep "Reverse " "")
3216 regexp))
3217 (goto-char opoint)
3218 (narrow-to-region omin omax)
3219 (ding)
3220 (message "Search failed: %s" regexp)))))
3222 (defun rmail-search-backwards (regexp &optional n)
3223 "Show message containing previous match for REGEXP.
3224 Prefix argument gives repeat count; negative argument means search
3225 forward (through later messages).
3226 Interactively, empty argument means use same regexp used last time."
3227 (interactive
3228 (let* ((reversep (>= (prefix-numeric-value current-prefix-arg) 0))
3229 (prompt
3230 (concat (if reversep "Reverse " "") "Rmail search (regexp"))
3231 regexp)
3232 (setq prompt
3233 (concat prompt
3234 (if rmail-search-last-regexp
3235 (concat ", default "
3236 rmail-search-last-regexp "): ")
3237 "): ")))
3238 (setq regexp (read-string prompt))
3239 (cond ((not (equal regexp ""))
3240 (setq rmail-search-last-regexp regexp))
3241 ((not rmail-search-last-regexp)
3242 (error "No previous Rmail search string")))
3243 (list rmail-search-last-regexp
3244 (prefix-numeric-value current-prefix-arg))))
3245 (rmail-search regexp (- (or n 1))))
3247 ;; Show the first message which has the `unseen' attribute.
3248 (defun rmail-first-unseen-message ()
3249 (rmail-maybe-set-message-counters)
3250 (let ((current 1)
3251 found)
3252 (save-restriction
3253 (widen)
3254 (while (and (not found) (<= current rmail-total-messages))
3255 (if (rmail-message-labels-p current ", ?\\(unseen\\),")
3256 (setq found current))
3257 (setq current (1+ current))))
3258 ;; Let the caller show the message.
3259 ;; (if found
3260 ;; (rmail-show-message found))
3261 found))
3263 (defun rmail-current-subject ()
3264 "Return the current subject.
3265 The subject is stripped of leading and trailing whitespace, and
3266 of typical reply prefixes such as Re:."
3267 (let ((subject (or (mail-fetch-field "Subject") "")))
3268 (if (string-match "\\`[ \t]+" subject)
3269 (setq subject (substring subject (match-end 0))))
3270 (if (string-match rmail-reply-regexp subject)
3271 (setq subject (substring subject (match-end 0))))
3272 (if (string-match "[ \t]+\\'" subject)
3273 (setq subject (substring subject 0 (match-beginning 0))))
3274 subject))
3276 (defun rmail-current-subject-regexp ()
3277 "Return a regular expression matching the current subject.
3278 The regular expression matches the subject header line of
3279 messages about the same subject. The subject itself is stripped
3280 of leading and trailing whitespace, of typical reply prefixes
3281 such as Re: and whitespace within the subject is replaced by a
3282 regular expression matching whitespace in general in order to
3283 take into account that subject header lines may include newlines
3284 and more whitespace. The returned regular expressions contains
3285 `rmail-reply-regexp' and ends with a newline."
3286 (let ((subject (rmail-current-subject)))
3287 ;; If Subject is long, mailers will break it into several lines at
3288 ;; arbitrary places, so replace whitespace with a regexp that will
3289 ;; match any sequence of spaces, TABs, and newlines.
3290 (setq subject (regexp-quote subject))
3291 (setq subject
3292 (replace-regexp-in-string "[ \t\n]+" "[ \t\n]+" subject t t))
3293 ;; Some mailers insert extra spaces after "Subject:", so allow any
3294 ;; amount of them.
3295 (concat "^Subject:[ \t]+"
3296 (if (string= "\\`" (substring rmail-reply-regexp 0 2))
3297 (substring rmail-reply-regexp 2)
3298 rmail-reply-regexp)
3299 subject "[ \t]*\n")))
3301 (defun rmail-next-same-subject (n)
3302 "Go to the next mail message having the same subject header.
3303 With prefix argument N, do this N times.
3304 If N is negative, go backwards instead."
3305 (interactive "p")
3306 (let ((search-regexp (rmail-current-subject-regexp))
3307 (forward (> n 0))
3308 (i rmail-current-message)
3309 (case-fold-search t)
3310 found)
3311 (save-excursion
3312 (save-restriction
3313 (widen)
3314 (while (and (/= n 0)
3315 (if forward
3316 (< i rmail-total-messages)
3317 (> i 1)))
3318 (let (done)
3319 (while (and (not done)
3320 (if forward
3321 (< i rmail-total-messages)
3322 (> i 1)))
3323 (setq i (if forward (1+ i) (1- i)))
3324 (goto-char (rmail-msgbeg i))
3325 (search-forward "\n*** EOOH ***\n")
3326 (let ((beg (point)) end)
3327 (search-forward "\n\n")
3328 (setq end (point))
3329 (goto-char beg)
3330 (setq done (re-search-forward search-regexp end t))))
3331 (if done (setq found i)))
3332 (setq n (if forward (1- n) (1+ n))))))
3333 (if found
3334 (rmail-show-message found)
3335 (error "No %s message with same subject"
3336 (if forward "following" "previous")))))
3338 (defun rmail-previous-same-subject (n)
3339 "Go to the previous mail message having the same subject header.
3340 With prefix argument N, do this N times.
3341 If N is negative, go forwards instead."
3342 (interactive "p")
3343 (rmail-next-same-subject (- n)))
3345 ;;;; *** Rmail Message Deletion Commands ***
3347 (defun rmail-message-deleted-p (n)
3348 (= (aref rmail-deleted-vector n) ?D))
3350 (defun rmail-set-message-deleted-p (n state)
3351 (aset rmail-deleted-vector n (if state ?D ?\ )))
3353 (defun rmail-delete-message ()
3354 "Delete this message and stay on it."
3355 (interactive)
3356 (rmail-set-attribute "deleted" t)
3357 (run-hooks 'rmail-delete-message-hook))
3359 (defun rmail-undelete-previous-message ()
3360 "Back up to deleted message, select it, and undelete it."
3361 (interactive)
3362 (set-buffer rmail-buffer)
3363 (let ((msg rmail-current-message))
3364 (while (and (> msg 0)
3365 (not (rmail-message-deleted-p msg)))
3366 (setq msg (1- msg)))
3367 (if (= msg 0)
3368 (error "No previous deleted message")
3369 (if (/= msg rmail-current-message)
3370 (rmail-show-message msg))
3371 (rmail-set-attribute "deleted" nil)
3372 (if (rmail-summary-exists)
3373 (save-excursion
3374 (set-buffer rmail-summary-buffer)
3375 (rmail-summary-mark-undeleted msg)))
3376 (rmail-maybe-display-summary))))
3378 (defun rmail-delete-forward (&optional backward)
3379 "Delete this message and move to next nondeleted one.
3380 Deleted messages stay in the file until the \\[rmail-expunge] command is given.
3381 With prefix argument, delete and move backward.
3383 Returns t if a new message is displayed after the delete, or nil otherwise."
3384 (interactive "P")
3385 (rmail-set-attribute "deleted" t)
3386 (run-hooks 'rmail-delete-message-hook)
3387 (let ((del-msg rmail-current-message))
3388 (if (rmail-summary-exists)
3389 (rmail-select-summary
3390 (rmail-summary-mark-deleted del-msg)))
3391 (prog1 (rmail-next-undeleted-message (if backward -1 1))
3392 (rmail-maybe-display-summary))))
3394 (defun rmail-delete-backward ()
3395 "Delete this message and move to previous nondeleted one.
3396 Deleted messages stay in the file until the \\[rmail-expunge] command is given."
3397 (interactive)
3398 (rmail-delete-forward t))
3400 ;; Compute the message number a given message would have after expunging.
3401 ;; The present number of the message is OLDNUM.
3402 ;; DELETEDVEC should be rmail-deleted-vector.
3403 ;; The value is nil for a message that would be deleted.
3404 (defun rmail-msg-number-after-expunge (deletedvec oldnum)
3405 (if (or (null oldnum) (= (aref deletedvec oldnum) ?D))
3407 (let ((i 0)
3408 (newnum 0))
3409 (while (< i oldnum)
3410 (if (/= (aref deletedvec i) ?D)
3411 (setq newnum (1+ newnum)))
3412 (setq i (1+ i)))
3413 newnum)))
3415 (defun rmail-expunge-confirmed ()
3416 "Return t if deleted message should be expunged. If necessary, ask the user.
3417 See also user-option `rmail-confirm-expunge'."
3418 (set-buffer rmail-buffer)
3419 (or (not (stringp rmail-deleted-vector))
3420 (not (string-match "D" rmail-deleted-vector))
3421 (null rmail-confirm-expunge)
3422 (funcall rmail-confirm-expunge
3423 "Erase deleted messages from Rmail file? ")))
3425 (defun rmail-only-expunge (&optional dont-show)
3426 "Actually erase all deleted messages in the file."
3427 (interactive)
3428 (set-buffer rmail-buffer)
3429 (message "Expunging deleted messages...")
3430 ;; Discard all undo records for this buffer.
3431 (or (eq buffer-undo-list t)
3432 (setq buffer-undo-list nil))
3433 (rmail-maybe-set-message-counters)
3434 (let* ((omax (- (buffer-size) (point-max)))
3435 (omin (- (buffer-size) (point-min)))
3436 (opoint (if (and (> rmail-current-message 0)
3437 (rmail-message-deleted-p rmail-current-message))
3439 (if rmail-enable-mime
3440 (with-current-buffer rmail-view-buffer
3441 (- (point)(point-min)))
3442 (- (point) (point-min)))))
3443 (messages-head (cons (aref rmail-message-vector 0) nil))
3444 (messages-tail messages-head)
3445 ;; Don't make any undo records for the expunging.
3446 (buffer-undo-list t)
3447 (win))
3448 (unwind-protect
3449 (save-excursion
3450 (widen)
3451 (goto-char (point-min))
3452 (let ((counter 0)
3453 (number 1)
3454 (total rmail-total-messages)
3455 (new-message-number rmail-current-message)
3456 (new-summary nil)
3457 (new-msgref (list (list 0)))
3458 (rmailbuf (current-buffer))
3459 (buffer-read-only nil)
3460 (messages rmail-message-vector)
3461 (deleted rmail-deleted-vector)
3462 (summary rmail-summary-vector))
3463 (setq rmail-total-messages nil
3464 rmail-current-message nil
3465 rmail-message-vector nil
3466 rmail-deleted-vector nil
3467 rmail-summary-vector nil)
3469 (while (<= number total)
3470 (if (= (aref deleted number) ?D)
3471 (progn
3472 (delete-region
3473 (marker-position (aref messages number))
3474 (marker-position (aref messages (1+ number))))
3475 (move-marker (aref messages number) nil)
3476 (if (> new-message-number counter)
3477 (setq new-message-number (1- new-message-number))))
3478 (setq counter (1+ counter))
3479 (setq messages-tail
3480 (setcdr messages-tail
3481 (cons (aref messages number) nil)))
3482 (setq new-summary
3483 (cons (if (= counter number) (aref summary (1- number)))
3484 new-summary))
3485 (setq new-msgref
3486 (cons (aref rmail-msgref-vector number)
3487 new-msgref))
3488 (setcar (car new-msgref) counter))
3489 (if (zerop (% (setq number (1+ number)) 20))
3490 (message "Expunging deleted messages...%d" number)))
3491 (setq messages-tail
3492 (setcdr messages-tail
3493 (cons (aref messages number) nil)))
3494 (setq rmail-current-message new-message-number
3495 rmail-total-messages counter
3496 rmail-message-vector (apply 'vector messages-head)
3497 rmail-deleted-vector (make-string (1+ counter) ?\ )
3498 rmail-summary-vector (vconcat (nreverse new-summary))
3499 rmail-msgref-vector (apply 'vector (nreverse new-msgref))
3500 win t)))
3501 (message "Expunging deleted messages...done")
3502 (if (not win)
3503 (narrow-to-region (- (buffer-size) omin) (- (buffer-size) omax)))
3504 (if (not dont-show)
3505 (rmail-show-message
3506 (if (zerop rmail-current-message) 1 nil)))
3507 (if rmail-enable-mime
3508 (goto-char (+ (point-min) opoint))
3509 (goto-char (+ (point) opoint))))))
3511 (defun rmail-expunge ()
3512 "Erase deleted messages from Rmail file and summary buffer."
3513 (interactive)
3514 (when (rmail-expunge-confirmed)
3515 (rmail-only-expunge)
3516 (if (rmail-summary-exists)
3517 (rmail-select-summary (rmail-update-summary)))))
3519 ;;;; *** Rmail Mailing Commands ***
3521 (defun rmail-start-mail (&optional noerase to subject in-reply-to cc
3522 replybuffer sendactions same-window others)
3523 (let (yank-action)
3524 (if replybuffer
3525 (setq yank-action (list 'insert-buffer replybuffer)))
3526 (setq others (cons (cons "cc" cc) others))
3527 (setq others (cons (cons "in-reply-to" in-reply-to) others))
3528 (if same-window
3529 (compose-mail to subject others
3530 noerase nil
3531 yank-action sendactions)
3532 (if rmail-mail-new-frame
3533 (prog1
3534 (compose-mail to subject others
3535 noerase 'switch-to-buffer-other-frame
3536 yank-action sendactions)
3537 ;; This is not a standard frame parameter;
3538 ;; nothing except sendmail.el looks at it.
3539 (modify-frame-parameters (selected-frame)
3540 '((mail-dedicated-frame . t))))
3541 (compose-mail to subject others
3542 noerase 'switch-to-buffer-other-window
3543 yank-action sendactions)))))
3545 (defun rmail-mail ()
3546 "Send mail in another window.
3547 While composing the message, use \\[mail-yank-original] to yank the
3548 original message into it."
3549 (interactive)
3550 (rmail-start-mail nil nil nil nil nil rmail-view-buffer))
3552 (defun rmail-continue ()
3553 "Continue composing outgoing message previously being composed."
3554 (interactive)
3555 (rmail-start-mail t))
3557 (defun rmail-reply (just-sender)
3558 "Reply to the current message.
3559 Normally include CC: to all other recipients of original message;
3560 prefix argument means ignore them. While composing the reply,
3561 use \\[mail-yank-original] to yank the original message into it."
3562 (interactive "P")
3563 (let (from reply-to cc subject date to message-id references
3564 resent-to resent-cc resent-reply-to
3565 (msgnum rmail-current-message))
3566 (save-excursion
3567 (save-restriction
3568 (if rmail-enable-mime
3569 (narrow-to-region
3570 (goto-char (point-min))
3571 (if (search-forward "\n\n" nil 'move)
3572 (1+ (match-beginning 0))
3573 (point)))
3574 (widen)
3575 (goto-char (rmail-msgbeg rmail-current-message))
3576 (forward-line 1)
3577 (if (= (following-char) ?0)
3578 (narrow-to-region
3579 (progn (forward-line 2)
3580 (point))
3581 (progn (search-forward "\n\n" (rmail-msgend rmail-current-message)
3582 'move)
3583 (point)))
3584 (narrow-to-region (point)
3585 (progn (search-forward "\n*** EOOH ***\n")
3586 (beginning-of-line) (point)))))
3587 (setq from (mail-fetch-field "from")
3588 reply-to (or (mail-fetch-field "mail-reply-to" nil t)
3589 (mail-fetch-field "reply-to" nil t)
3590 from)
3591 subject (mail-fetch-field "subject")
3592 date (mail-fetch-field "date")
3593 message-id (mail-fetch-field "message-id")
3594 references (mail-fetch-field "references" nil nil t)
3595 resent-reply-to (mail-fetch-field "resent-reply-to" nil t)
3596 resent-cc (and (not just-sender)
3597 (mail-fetch-field "resent-cc" nil t))
3598 resent-to (or (mail-fetch-field "resent-to" nil t) "")
3599 ;;; resent-subject (mail-fetch-field "resent-subject")
3600 ;;; resent-date (mail-fetch-field "resent-date")
3601 ;;; resent-message-id (mail-fetch-field "resent-message-id")
3603 (unless just-sender
3604 (if (mail-fetch-field "mail-followup-to" nil t)
3605 ;; If this header field is present, use it instead of the To and CC fields.
3606 (setq to (mail-fetch-field "mail-followup-to" nil t))
3607 (setq cc (or (mail-fetch-field "cc" nil t) "")
3608 to (or (mail-fetch-field "to" nil t) ""))))
3612 ;; Merge the resent-to and resent-cc into the to and cc.
3613 (if (and resent-to (not (equal resent-to "")))
3614 (if (not (equal to ""))
3615 (setq to (concat to ", " resent-to))
3616 (setq to resent-to)))
3617 (if (and resent-cc (not (equal resent-cc "")))
3618 (if (not (equal cc ""))
3619 (setq cc (concat cc ", " resent-cc))
3620 (setq cc resent-cc)))
3621 ;; Add `Re: ' to subject if not there already.
3622 (and (stringp subject)
3623 (setq subject
3624 (concat rmail-reply-prefix
3625 (if (let ((case-fold-search t))
3626 (string-match rmail-reply-regexp subject))
3627 (substring subject (match-end 0))
3628 subject))))
3629 (rmail-start-mail
3631 ;; Using mail-strip-quoted-names is undesirable with newer mailers
3632 ;; since they can handle the names unstripped.
3633 ;; I don't know whether there are other mailers that still
3634 ;; need the names to be stripped.
3635 ;;; (mail-strip-quoted-names reply-to)
3636 ;; Remove unwanted names from reply-to, since Mail-Followup-To
3637 ;; header causes all the names in it to wind up in reply-to, not
3638 ;; in cc. But if what's left is an empty list, use the original.
3639 (let* ((reply-to-list (rmail-dont-reply-to reply-to)))
3640 (if (string= reply-to-list "") reply-to reply-to-list))
3641 subject
3642 (rmail-make-in-reply-to-field from date message-id)
3643 (if just-sender
3645 ;; mail-strip-quoted-names is NOT necessary for rmail-dont-reply-to
3646 ;; to do its job.
3647 (let* ((cc-list (rmail-dont-reply-to
3648 (mail-strip-quoted-names
3649 (if (null cc) to (concat to ", " cc))))))
3650 (if (string= cc-list "") nil cc-list)))
3651 rmail-view-buffer
3652 (list (list 'rmail-mark-message
3653 rmail-buffer
3654 (with-current-buffer rmail-buffer
3655 (aref rmail-msgref-vector msgnum))
3656 "answered"))
3658 (list (cons "References" (concat (mapconcat 'identity references " ")
3659 " " message-id))))))
3661 (defun rmail-mark-message (buffer msgnum-list attribute)
3662 "Give BUFFER's message number in MSGNUM-LIST the attribute ATTRIBUTE.
3663 This is use in the send-actions for message buffers.
3664 MSGNUM-LIST is a list of the form (MSGNUM)
3665 which is an element of rmail-msgref-vector."
3666 (save-excursion
3667 (set-buffer buffer)
3668 (if (car msgnum-list)
3669 (rmail-set-attribute attribute t (car msgnum-list)))))
3671 (defun rmail-make-in-reply-to-field (from date message-id)
3672 (cond ((not from)
3673 (if message-id
3674 message-id
3675 nil))
3676 (mail-use-rfc822
3677 (require 'rfc822)
3678 (let ((tem (car (rfc822-addresses from))))
3679 (if message-id
3680 (if (or (not tem)
3681 (string-match
3682 (regexp-quote (if (string-match "@[^@]*\\'" tem)
3683 (substring tem 0
3684 (match-beginning 0))
3685 tem))
3686 message-id))
3687 ;; missing From, or Message-ID is sufficiently informative
3688 message-id
3689 (concat message-id " (" tem ")"))
3690 ;; Copy TEM, discarding text properties.
3691 (setq tem (copy-sequence tem))
3692 (set-text-properties 0 (length tem) nil tem)
3693 (setq tem (copy-sequence tem))
3694 ;; Use prin1 to fake RFC822 quoting
3695 (let ((field (prin1-to-string tem)))
3696 (if date
3697 (concat field "'s message of " date)
3698 field)))))
3699 ((let* ((foo "[^][\000-\037()<>@,;:\\\" ]+")
3700 (bar "[^][\000-\037()<>@,;:\\\"]+"))
3701 ;; These strings both match all non-ASCII characters.
3702 (or (string-match (concat "\\`[ \t]*\\(" bar
3703 "\\)\\(<" foo "@" foo ">\\)?[ \t]*\\'")
3704 ;; "Unix Loser <Foo@bar.edu>" => "Unix Loser"
3705 from)
3706 (string-match (concat "\\`[ \t]*<" foo "@" foo ">[ \t]*(\\("
3707 bar "\\))[ \t]*\\'")
3708 ;; "<Bugs@bar.edu>" (Losing Unix) => "Losing Unix"
3709 from)))
3710 (let ((start (match-beginning 1))
3711 (end (match-end 1)))
3712 ;; Trim whitespace which above regexp match allows
3713 (while (and (< start end)
3714 (memq (aref from start) '(?\t ?\ )))
3715 (setq start (1+ start)))
3716 (while (and (< start end)
3717 (memq (aref from (1- end)) '(?\t ?\ )))
3718 (setq end (1- end)))
3719 (let ((field (substring from start end)))
3720 (if date (setq field (concat "message from " field " on " date)))
3721 (if message-id
3722 ;; "<AA259@bar.edu> (message from Unix Loser on 1-Apr-89)"
3723 (concat message-id " (" field ")")
3724 field))))
3726 ;; If we can't kludge it simply, do it correctly
3727 (let ((mail-use-rfc822 t))
3728 (rmail-make-in-reply-to-field from date message-id)))))
3730 (defun rmail-forward (resend)
3731 "Forward the current message to another user.
3732 With prefix argument, \"resend\" the message instead of forwarding it;
3733 see the documentation of `rmail-resend'."
3734 (interactive "P")
3735 (if resend
3736 (call-interactively 'rmail-resend)
3737 (let ((forward-buffer rmail-buffer)
3738 (msgnum rmail-current-message)
3739 (subject (concat "["
3740 (let ((from (or (mail-fetch-field "From")
3741 (mail-fetch-field ">From"))))
3742 (if from
3743 (concat (mail-strip-quoted-names from) ": ")
3744 ""))
3745 (or (mail-fetch-field "Subject") "")
3746 "]")))
3747 (if (rmail-start-mail
3748 nil nil subject nil nil nil
3749 (list (list 'rmail-mark-message
3750 forward-buffer
3751 (with-current-buffer rmail-buffer
3752 (aref rmail-msgref-vector msgnum))
3753 "forwarded"))
3754 ;; If only one window, use it for the mail buffer.
3755 ;; Otherwise, use another window for the mail buffer
3756 ;; so that the Rmail buffer remains visible
3757 ;; and sending the mail will get back to it.
3758 (and (not rmail-mail-new-frame) (one-window-p t)))
3759 ;; The mail buffer is now current.
3760 (save-excursion
3761 ;; Insert after header separator--before signature if any.
3762 (goto-char (mail-text-start))
3763 (if (or rmail-enable-mime rmail-enable-mime-composing)
3764 (funcall rmail-insert-mime-forwarded-message-function
3765 forward-buffer)
3766 (insert "------- Start of forwarded message -------\n")
3767 ;; Quote lines with `- ' if they start with `-'.
3768 (let ((beg (point)) end)
3769 (setq end (point-marker))
3770 (set-marker-insertion-type end t)
3771 (insert-buffer-substring forward-buffer)
3772 (goto-char beg)
3773 (while (re-search-forward "^-" end t)
3774 (beginning-of-line)
3775 (insert "- ")
3776 (forward-line 1))
3777 (goto-char end)
3778 (skip-chars-backward "\n")
3779 (if (< (point) end)
3780 (forward-char 1))
3781 (delete-region (point) end)
3782 (set-marker end nil))
3783 (insert "------- End of forwarded message -------\n"))
3784 (push-mark))))))
3786 (defun rmail-resend (address &optional from comment mail-alias-file)
3787 "Resend current message to ADDRESSES.
3788 ADDRESSES should be a single address, a string consisting of several
3789 addresses separated by commas, or a list of addresses.
3791 Optional FROM is the address to resend the message from, and
3792 defaults from the value of `user-mail-address'.
3793 Optional COMMENT is a string to insert as a comment in the resent message.
3794 Optional ALIAS-FILE is alternate aliases file to be used by sendmail,
3795 typically for purposes of moderating a list."
3796 (interactive "sResend to: ")
3797 (require 'sendmail)
3798 (require 'mailalias)
3799 (unless (or (eq rmail-view-buffer (current-buffer))
3800 (eq rmail-buffer (current-buffer)))
3801 (error "Not an Rmail buffer"))
3802 (if (not from) (setq from user-mail-address))
3803 (let ((tembuf (generate-new-buffer " sendmail temp"))
3804 (case-fold-search nil)
3805 (mail-personal-alias-file
3806 (or mail-alias-file mail-personal-alias-file))
3807 (mailbuf rmail-buffer))
3808 (unwind-protect
3809 (with-current-buffer tembuf
3810 ;;>> Copy message into temp buffer
3811 (if rmail-enable-mime
3812 (funcall rmail-insert-mime-resent-message-function mailbuf)
3813 (insert-buffer-substring mailbuf))
3814 (goto-char (point-min))
3815 ;; Delete any Sender field, since that's not specifiable.
3816 ; Only delete Sender fields in the actual header.
3817 (re-search-forward "^$" nil 'move)
3818 ; Using "while" here rather than "if" because some buggy mail
3819 ; software may have inserted multiple Sender fields.
3820 (while (re-search-backward "^Sender:" nil t)
3821 (let (beg)
3822 (setq beg (point))
3823 (forward-line 1)
3824 (while (looking-at "[ \t]")
3825 (forward-line 1))
3826 (delete-region beg (point))))
3827 ; Go back to the beginning of the buffer so the Resent- fields
3828 ; are inserted there.
3829 (goto-char (point-min))
3830 ;;>> Insert resent-from:
3831 (insert "Resent-From: " from "\n")
3832 (insert "Resent-Date: " (mail-rfc822-date) "\n")
3833 ;;>> Insert resent-to: and bcc if need be.
3834 (let ((before (point)))
3835 (if mail-self-blind
3836 (insert "Resent-Bcc: " (user-login-name) "\n"))
3837 (insert "Resent-To: " (if (stringp address)
3838 address
3839 (mapconcat 'identity address ",\n\t"))
3840 "\n")
3841 ;; Expand abbrevs in the recipients.
3842 (save-excursion
3843 (if (featurep 'mailabbrev)
3844 (let ((end (point-marker))
3845 (local-abbrev-table mail-abbrevs)
3846 (old-syntax-table (syntax-table)))
3847 (if (and (not (vectorp mail-abbrevs))
3848 (file-exists-p mail-personal-alias-file))
3849 (build-mail-abbrevs))
3850 (unless mail-abbrev-syntax-table
3851 (mail-abbrev-make-syntax-table))
3852 (set-syntax-table mail-abbrev-syntax-table)
3853 (goto-char before)
3854 (while (and (< (point) end)
3855 (progn (forward-word 1)
3856 (<= (point) end)))
3857 (expand-abbrev))
3858 (set-syntax-table old-syntax-table))
3859 (expand-mail-aliases before (point)))))
3860 ;;>> Set up comment, if any.
3861 (if (and (sequencep comment) (not (zerop (length comment))))
3862 (let ((before (point))
3863 after)
3864 (insert comment)
3865 (or (eolp) (insert "\n"))
3866 (setq after (point))
3867 (goto-char before)
3868 (while (< (point) after)
3869 (insert "Resent-Comment: ")
3870 (forward-line 1))))
3871 ;; Don't expand aliases in the destination fields
3872 ;; of the original message.
3873 (let (mail-aliases)
3874 (funcall send-mail-function)))
3875 (kill-buffer tembuf))
3876 (with-current-buffer rmail-buffer
3877 (rmail-set-attribute "resent" t rmail-current-message))))
3879 (defvar mail-unsent-separator
3880 (concat "^ *---+ +Unsent message follows +---+ *$\\|"
3881 "^ *---+ +Returned message +---+ *$\\|"
3882 "^ *---+ *Returned mail follows *---+ *$\\|"
3883 "^Start of returned message$\\|"
3884 "^---+ Below this line is a copy of the message.$\\|"
3885 "^ *---+ +Original message +---+ *$\\|"
3886 "^ *--+ +begin message +--+ *$\\|"
3887 "^ *---+ +Original message follows +---+ *$\\|"
3888 "^ *---+ +Your message follows +---+ *$\\|"
3889 "^|? *---+ +Message text follows: +---+ *|?$\\|"
3890 "^ *---+ +This is a copy of \\w+ message, including all the headers.*---+ *$")
3891 "A regexp that matches the separator before the text of a failed message.")
3893 (defvar mail-mime-unsent-header "^Content-Type: message/rfc822 *$"
3894 "A regexp that matches the header of a MIME body part with a failed message.")
3896 (defun rmail-retry-failure ()
3897 "Edit a mail message which is based on the contents of the current message.
3898 For a message rejected by the mail system, extract the interesting headers and
3899 the body of the original message.
3900 If the failed message is a MIME multipart message, it is searched for a
3901 body part with a header which matches the variable `mail-mime-unsent-header'.
3902 Otherwise, the variable `mail-unsent-separator' should match the string that
3903 delimits the returned original message.
3904 The variable `rmail-retry-ignored-headers' is a regular expression
3905 specifying headers which should not be copied into the new message."
3906 (interactive)
3907 (require 'mail-utils)
3908 (let ((rmail-this-buffer (current-buffer))
3909 (msgnum rmail-current-message)
3910 bounce-start bounce-end bounce-indent resending
3911 ;; Fetch any content-type header in current message
3912 ;; Must search thru the whole unpruned header.
3913 (content-type
3914 (save-excursion
3915 (save-restriction
3916 (rmail-narrow-to-non-pruned-header)
3917 (mail-fetch-field "Content-Type") ))))
3918 (save-excursion
3919 (goto-char (point-min))
3920 (let ((case-fold-search t))
3921 (if (and content-type
3922 (string-match
3923 ";[\n\t ]*boundary=\"?\\([-0-9a-z'()+_,./:=? ]+\\)\"?"
3924 content-type))
3925 ;; Handle a MIME multipart bounce message.
3926 (let ((codestring
3927 (concat "\n--"
3928 (substring content-type (match-beginning 1)
3929 (match-end 1)))))
3930 (unless (re-search-forward mail-mime-unsent-header nil t)
3931 (error "Cannot find beginning of header in failed message"))
3932 (unless (search-forward "\n\n" nil t)
3933 (error "Cannot find start of Mime data in failed message"))
3934 (setq bounce-start (point))
3935 (if (search-forward codestring nil t)
3936 (setq bounce-end (match-beginning 0))
3937 (setq bounce-end (point-max))))
3938 ;; Non-MIME bounce.
3939 (or (re-search-forward mail-unsent-separator nil t)
3940 (error "Cannot parse this as a failure message"))
3941 (skip-chars-forward "\n")
3942 ;; Support a style of failure message in which the original
3943 ;; message is indented, and included within lines saying
3944 ;; `Start of returned message' and `End of returned message'.
3945 (if (looking-at " +Received:")
3946 (progn
3947 (setq bounce-start (point))
3948 (skip-chars-forward " ")
3949 (setq bounce-indent (- (current-column)))
3950 (goto-char (point-max))
3951 (re-search-backward "^End of returned message$" nil t)
3952 (setq bounce-end (point)))
3953 ;; One message contained a few random lines before
3954 ;; the old message header. The first line of the
3955 ;; message started with two hyphens. A blank line
3956 ;; followed these random lines. The same line
3957 ;; beginning with two hyphens was possibly marking
3958 ;; the end of the message.
3959 (if (looking-at "^--")
3960 (let ((boundary (buffer-substring-no-properties
3961 (point)
3962 (progn (end-of-line) (point)))))
3963 (search-forward "\n\n")
3964 (skip-chars-forward "\n")
3965 (setq bounce-start (point))
3966 (goto-char (point-max))
3967 (search-backward (concat "\n\n" boundary) bounce-start t)
3968 (setq bounce-end (point)))
3969 (setq bounce-start (point)
3970 bounce-end (point-max)))
3971 (unless (search-forward "\n\n" nil t)
3972 (error "Cannot find end of header in failed message"))))))
3973 ;; We have found the message that bounced, within the current message.
3974 ;; Now start sending new message; default header fields from original.
3975 ;; Turn off the usual actions for initializing the message body
3976 ;; because we want to get only the text from the failure message.
3977 (let (mail-signature mail-setup-hook)
3978 (if (rmail-start-mail nil nil nil nil nil rmail-this-buffer
3979 (list (list 'rmail-mark-message
3980 rmail-this-buffer
3981 (aref rmail-msgref-vector msgnum)
3982 "retried")))
3983 ;; Insert original text as initial text of new draft message.
3984 ;; Bind inhibit-read-only since the header delimiter
3985 ;; of the previous message was probably read-only.
3986 (let ((inhibit-read-only t)
3987 rmail-displayed-headers
3988 rmail-ignored-headers)
3989 (erase-buffer)
3990 (insert-buffer-substring rmail-this-buffer
3991 bounce-start bounce-end)
3992 (goto-char (point-min))
3993 (if bounce-indent
3994 (indent-rigidly (point-min) (point-max) bounce-indent))
3995 (rmail-clear-headers rmail-retry-ignored-headers)
3996 (rmail-clear-headers "^sender:\\|^return-path:\\|^received:")
3997 (mail-sendmail-delimit-header)
3998 (save-restriction
3999 (narrow-to-region (point-min) (mail-header-end))
4000 (setq resending (mail-fetch-field "resent-to"))
4001 (if mail-self-blind
4002 (if resending
4003 (insert "Resent-Bcc: " (user-login-name) "\n")
4004 (insert "BCC: " (user-login-name) "\n"))))
4005 (goto-char (point-min))
4006 (mail-position-on-field (if resending "Resent-To" "To") t))))))
4008 (defun rmail-summary-exists ()
4009 "Non-nil if in an RMAIL buffer and an associated summary buffer exists.
4010 In fact, the non-nil value returned is the summary buffer itself."
4011 (and rmail-summary-buffer (buffer-name rmail-summary-buffer)
4012 rmail-summary-buffer))
4014 (defun rmail-summary-displayed ()
4015 "t if in RMAIL buffer and an associated summary buffer is displayed."
4016 (and rmail-summary-buffer (get-buffer-window rmail-summary-buffer)))
4018 (defcustom rmail-redisplay-summary nil
4019 "*Non-nil means Rmail should show the summary when it changes.
4020 This has an effect only if a summary buffer exists."
4021 :type 'boolean
4022 :group 'rmail-summary)
4024 (defcustom rmail-summary-window-size nil
4025 "*Non-nil means specify the height for an Rmail summary window."
4026 :type '(choice (const :tag "Disabled" nil) integer)
4027 :group 'rmail-summary)
4029 ;; Put the summary buffer back on the screen, if user wants that.
4030 (defun rmail-maybe-display-summary ()
4031 (let ((selected (selected-window))
4032 window)
4033 ;; If requested, make sure the summary is displayed.
4034 (and rmail-summary-buffer (buffer-name rmail-summary-buffer)
4035 rmail-redisplay-summary
4036 (if (get-buffer-window rmail-summary-buffer 0)
4037 ;; It's already in some frame; show that one.
4038 (let ((frame (window-frame
4039 (get-buffer-window rmail-summary-buffer 0))))
4040 (make-frame-visible frame)
4041 (raise-frame frame))
4042 (display-buffer rmail-summary-buffer)))
4043 ;; If requested, set the height of the summary window.
4044 (and rmail-summary-buffer (buffer-name rmail-summary-buffer)
4045 rmail-summary-window-size
4046 (setq window (get-buffer-window rmail-summary-buffer))
4047 ;; Don't try to change the size if just one window in frame.
4048 (not (eq window (frame-root-window (window-frame window))))
4049 (unwind-protect
4050 (progn
4051 (select-window window)
4052 (enlarge-window (- rmail-summary-window-size (window-height))))
4053 (select-window selected)))))
4055 ;;;; *** Rmail Local Fontification ***
4057 (defun rmail-fontify-buffer-function ()
4058 ;; This function's symbol is bound to font-lock-fontify-buffer-function.
4059 (add-hook 'rmail-show-message-hook 'rmail-fontify-message nil t)
4060 ;; If we're already showing a message, fontify it now.
4061 (if rmail-current-message (rmail-fontify-message))
4062 ;; Prevent Font Lock mode from kicking in.
4063 (setq font-lock-fontified t))
4065 (defun rmail-unfontify-buffer-function ()
4066 ;; This function's symbol is bound to font-lock-fontify-unbuffer-function.
4067 (let ((modified (buffer-modified-p))
4068 (buffer-undo-list t) (inhibit-read-only t)
4069 before-change-functions after-change-functions
4070 buffer-file-name buffer-file-truename)
4071 (save-restriction
4072 (widen)
4073 (remove-hook 'rmail-show-message-hook 'rmail-fontify-message t)
4074 (remove-text-properties (point-min) (point-max) '(rmail-fontified nil))
4075 (font-lock-default-unfontify-buffer)
4076 (and (not modified) (buffer-modified-p) (set-buffer-modified-p nil)))))
4078 (defun rmail-fontify-message ()
4079 ;; Fontify the current message if it is not already fontified.
4080 (if (text-property-any (point-min) (point-max) 'rmail-fontified nil)
4081 (let ((modified (buffer-modified-p))
4082 (buffer-undo-list t) (inhibit-read-only t)
4083 before-change-functions after-change-functions
4084 buffer-file-name buffer-file-truename)
4085 (save-excursion
4086 (save-match-data
4087 (add-text-properties (point-min) (point-max) '(rmail-fontified t))
4088 (font-lock-fontify-region (point-min) (point-max))
4089 (and (not modified) (buffer-modified-p) (set-buffer-modified-p nil)))))))
4091 ;;; Speedbar support for RMAIL files.
4092 (eval-when-compile (require 'speedbar))
4094 (defvar rmail-speedbar-match-folder-regexp "^[A-Z0-9]+\\(\\.[A-Z0-9]+\\)?$"
4095 "*This regex is used to match folder names to be displayed in speedbar.
4096 Enabling this will permit speedbar to display your folders for easy
4097 browsing, and moving of messages.")
4099 (defvar rmail-speedbar-last-user nil
4100 "The last user to be displayed in the speedbar.")
4102 (defvar rmail-speedbar-key-map nil
4103 "Keymap used when in rmail display mode.")
4105 (defun rmail-install-speedbar-variables ()
4106 "Install those variables used by speedbar to enhance rmail."
4107 (if rmail-speedbar-key-map
4109 (setq rmail-speedbar-key-map (speedbar-make-specialized-keymap))
4111 (define-key rmail-speedbar-key-map "e" 'speedbar-edit-line)
4112 (define-key rmail-speedbar-key-map "r" 'speedbar-edit-line)
4113 (define-key rmail-speedbar-key-map "\C-m" 'speedbar-edit-line)
4114 (define-key rmail-speedbar-key-map "M"
4115 'rmail-speedbar-move-message-to-folder-on-line)))
4117 (defvar rmail-speedbar-menu-items
4118 '(["Read Folder" speedbar-edit-line t]
4119 ["Move message to folder" rmail-speedbar-move-message-to-folder-on-line
4120 (save-excursion (beginning-of-line)
4121 (looking-at "<M> "))])
4122 "Additional menu-items to add to speedbar frame.")
4124 ;; Make sure our special speedbar major mode is loaded
4125 (if (featurep 'speedbar)
4126 (rmail-install-speedbar-variables)
4127 (add-hook 'speedbar-load-hook 'rmail-install-speedbar-variables))
4129 (defun rmail-speedbar-buttons (buffer)
4130 "Create buttons for BUFFER containing rmail messages.
4131 Click on the address under Reply to: to reply to this person.
4132 Under Folders: Click a name to read it, or on the <M> to move the
4133 current message into that RMAIL folder."
4134 (let ((from nil))
4135 (save-excursion
4136 (set-buffer buffer)
4137 (goto-char (point-min))
4138 (if (not (re-search-forward "^Reply-To: " nil t))
4139 (if (not (re-search-forward "^From:? " nil t))
4140 (setq from t)))
4141 (if from
4143 (setq from (buffer-substring (point) (save-excursion
4144 (end-of-line)
4145 (point))))))
4146 (goto-char (point-min))
4147 (if (and (looking-at "Reply to:")
4148 (equal from rmail-speedbar-last-user))
4150 (setq rmail-speedbar-last-user from)
4151 (erase-buffer)
4152 (insert "Reply To:\n")
4153 (if (stringp from)
4154 (speedbar-insert-button from 'speedbar-directory-face 'highlight
4155 'rmail-speedbar-button 'rmail-reply))
4156 (insert "Folders:\n")
4157 (let* ((case-fold-search nil)
4158 (df (directory-files (save-excursion (set-buffer buffer)
4159 default-directory)
4160 nil rmail-speedbar-match-folder-regexp)))
4161 (while df
4162 (speedbar-insert-button "<M>" 'speedbar-button-face 'highlight
4163 'rmail-speedbar-move-message (car df))
4164 (speedbar-insert-button (car df) 'speedbar-file-face 'highlight
4165 'rmail-speedbar-find-file nil t)
4166 (setq df (cdr df)))))))
4168 (defun rmail-speedbar-button (text token indent)
4169 "Execute an rmail command specified by TEXT.
4170 The command used is TOKEN. INDENT is not used."
4171 (speedbar-with-attached-buffer
4172 (funcall token t)))
4174 (defun rmail-speedbar-find-file (text token indent)
4175 "Load in the rmail file TEXT.
4176 TOKEN and INDENT are not used."
4177 (speedbar-with-attached-buffer
4178 (message "Loading in RMAIL file %s..." text)
4179 (find-file text)))
4181 (defun rmail-speedbar-move-message-to-folder-on-line ()
4182 "If the current line is a folder, move current message to it."
4183 (interactive)
4184 (save-excursion
4185 (beginning-of-line)
4186 (if (re-search-forward "<M> " (save-excursion (end-of-line) (point)) t)
4187 (progn
4188 (forward-char -2)
4189 (speedbar-do-function-pointer)))))
4191 (defun rmail-speedbar-move-message (text token indent)
4192 "From button TEXT, copy current message to the rmail file specified by TOKEN.
4193 TEXT and INDENT are not used."
4194 (speedbar-with-attached-buffer
4195 (message "Moving message to %s" token)
4196 (rmail-output-to-rmail-file token)))
4198 ; Functions for setting, getting and encoding the POP password.
4199 ; The password is encoded to prevent it from being easily accessible
4200 ; to "prying eyes." Obviously, this encoding isn't "real security,"
4201 ; nor is it meant to be.
4203 ;;;###autoload
4204 (defun rmail-set-remote-password (password)
4205 "Set PASSWORD to be used for retrieving mail from a POP or IMAP server."
4206 (interactive "sPassword: ")
4207 (if password
4208 (setq rmail-encoded-remote-password
4209 (rmail-encode-string password (emacs-pid)))
4210 (setq rmail-remote-password nil)
4211 (setq rmail-encoded-remote-password nil)))
4213 (defun rmail-get-remote-password (imap)
4214 "Get the password for retrieving mail from a POP or IMAP server. If none
4215 has been set, then prompt the user for one."
4216 (when (not rmail-encoded-remote-password)
4217 (if (not rmail-remote-password)
4218 (setq rmail-remote-password
4219 (read-passwd (if imap
4220 "IMAP password: "
4221 "POP password: "))))
4222 (rmail-set-remote-password rmail-remote-password)
4223 (setq rmail-remote-password nil))
4224 (rmail-encode-string rmail-encoded-remote-password (emacs-pid)))
4226 (defun rmail-have-password ()
4227 (or rmail-remote-password rmail-encoded-remote-password))
4229 (defun rmail-encode-string (string mask)
4230 "Encode STRING with integer MASK, by taking the exclusive OR of the
4231 lowest byte in the mask with the first character of string, the
4232 second-lowest-byte with the second character of the string, etc.,
4233 restarting at the lowest byte of the mask whenever it runs out.
4234 Returns the encoded string. Calling the function again with an
4235 encoded string (and the same mask) will decode the string."
4236 (setq mask (abs mask)) ; doesn't work if negative
4237 (let* ((string-vector (string-to-vector string)) (i 0)
4238 (len (length string-vector)) (curmask mask) charmask)
4239 (while (< i len)
4240 (if (= curmask 0)
4241 (setq curmask mask))
4242 (setq charmask (% curmask 256))
4243 (setq curmask (lsh curmask -8))
4244 (aset string-vector i (logxor charmask (aref string-vector i)))
4245 (setq i (1+ i)))
4246 (concat string-vector)))
4248 ;;;; Desktop support
4250 (defun rmail-restore-desktop-buffer (desktop-buffer-file-name
4251 desktop-buffer-name
4252 desktop-buffer-misc)
4253 "Restore an rmail buffer specified in a desktop file."
4254 (condition-case error
4255 (progn
4256 (rmail-input desktop-buffer-file-name)
4257 (if (eq major-mode 'rmail-mode)
4258 (current-buffer)
4259 rmail-buffer))
4260 (file-locked
4261 (kill-buffer (current-buffer))
4262 nil)))
4264 (add-to-list 'desktop-buffer-mode-handlers
4265 '(rmail-mode . rmail-restore-desktop-buffer))
4267 (provide 'rmail)
4269 ;;; arch-tag: cff0a950-57fe-4f73-a86e-91ff75afd06c
4270 ;;; rmail.el ends here