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