(rmail-end-of-message): New command to go to the
[bpt/emacs.git] / lisp / mail / rmail.el
1 ;;; rmail.el --- main code of "RMAIL" mail reader for Emacs
2
3 ;; Copyright (C) 1985, 1986, 1987, 1988, 1993, 1994, 1995, 1996, 1997, 1998,
4 ;; 2000, 2001, 2002, 2003, 2004, 2005 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:"
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-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 ;; Emacs 21.1 and later writes RMAIL files in emacs-mule, but
892 ;; earlier versions did that with the current buffer's encoding.
893 ;; So we want to favor detection of emacs-mule (whose normal
894 ;; priority is quite low), but still allow detection of other
895 ;; encodings if emacs-mule won't fit. The call to
896 ;; detect-coding-with-priority below achieves that.
897 (car (detect-coding-with-priority
898 from to
899 '((coding-category-emacs-mule . emacs-mule))))))
900 (unless (memq coding-system
901 '(undecided undecided-unix))
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-end-of-message)
956 (define-key rmail-mode-map "<" 'rmail-first-message)
957 (define-key rmail-mode-map ">" 'rmail-last-message)
958 (define-key rmail-mode-map " " 'scroll-up)
959 (define-key rmail-mode-map "\177" 'scroll-down)
960 (define-key rmail-mode-map "?" 'describe-mode)
961 (define-key rmail-mode-map "\C-c\C-s\C-d" 'rmail-sort-by-date)
962 (define-key rmail-mode-map "\C-c\C-s\C-s" 'rmail-sort-by-subject)
963 (define-key rmail-mode-map "\C-c\C-s\C-a" 'rmail-sort-by-author)
964 (define-key rmail-mode-map "\C-c\C-s\C-r" 'rmail-sort-by-recipient)
965 (define-key rmail-mode-map "\C-c\C-s\C-c" 'rmail-sort-by-correspondent)
966 (define-key rmail-mode-map "\C-c\C-s\C-l" 'rmail-sort-by-lines)
967 (define-key rmail-mode-map "\C-c\C-s\C-k" 'rmail-sort-by-labels)
968 (define-key rmail-mode-map "\C-c\C-n" 'rmail-next-same-subject)
969 (define-key rmail-mode-map "\C-c\C-p" 'rmail-previous-same-subject)
970 )
971 \f
972 (define-key rmail-mode-map [menu-bar] (make-sparse-keymap))
973
974 (define-key rmail-mode-map [menu-bar classify]
975 (cons "Classify" (make-sparse-keymap "Classify")))
976
977 (define-key rmail-mode-map [menu-bar classify input-menu]
978 nil)
979
980 (define-key rmail-mode-map [menu-bar classify output-menu]
981 nil)
982
983 (define-key rmail-mode-map [menu-bar classify output-body]
984 '("Output body to file..." . rmail-output-body-to-file))
985
986 (define-key rmail-mode-map [menu-bar classify output-inbox]
987 '("Output (inbox)..." . rmail-output))
988
989 (define-key rmail-mode-map [menu-bar classify output]
990 '("Output (Rmail)..." . rmail-output-to-rmail-file))
991
992 (define-key rmail-mode-map [menu-bar classify kill-label]
993 '("Kill Label..." . rmail-kill-label))
994
995 (define-key rmail-mode-map [menu-bar classify add-label]
996 '("Add Label..." . rmail-add-label))
997
998 (define-key rmail-mode-map [menu-bar summary]
999 (cons "Summary" (make-sparse-keymap "Summary")))
1000
1001 (define-key rmail-mode-map [menu-bar summary senders]
1002 '("By Senders..." . rmail-summary-by-senders))
1003
1004 (define-key rmail-mode-map [menu-bar summary labels]
1005 '("By Labels..." . rmail-summary-by-labels))
1006
1007 (define-key rmail-mode-map [menu-bar summary recipients]
1008 '("By Recipients..." . rmail-summary-by-recipients))
1009
1010 (define-key rmail-mode-map [menu-bar summary topic]
1011 '("By Topic..." . rmail-summary-by-topic))
1012
1013 (define-key rmail-mode-map [menu-bar summary regexp]
1014 '("By Regexp..." . rmail-summary-by-regexp))
1015
1016 (define-key rmail-mode-map [menu-bar summary all]
1017 '("All" . rmail-summary))
1018
1019 (define-key rmail-mode-map [menu-bar mail]
1020 (cons "Mail" (make-sparse-keymap "Mail")))
1021
1022 (define-key rmail-mode-map [menu-bar mail rmail-get-new-mail]
1023 '("Get New Mail" . rmail-get-new-mail))
1024
1025 (define-key rmail-mode-map [menu-bar mail lambda]
1026 '("----"))
1027
1028 (define-key rmail-mode-map [menu-bar mail continue]
1029 '("Continue" . rmail-continue))
1030
1031 (define-key rmail-mode-map [menu-bar mail resend]
1032 '("Re-send..." . rmail-resend))
1033
1034 (define-key rmail-mode-map [menu-bar mail forward]
1035 '("Forward" . rmail-forward))
1036
1037 (define-key rmail-mode-map [menu-bar mail retry]
1038 '("Retry" . rmail-retry-failure))
1039
1040 (define-key rmail-mode-map [menu-bar mail reply]
1041 '("Reply" . rmail-reply))
1042
1043 (define-key rmail-mode-map [menu-bar mail mail]
1044 '("Mail" . rmail-mail))
1045
1046 (define-key rmail-mode-map [menu-bar delete]
1047 (cons "Delete" (make-sparse-keymap "Delete")))
1048
1049 (define-key rmail-mode-map [menu-bar delete expunge/save]
1050 '("Expunge/Save" . rmail-expunge-and-save))
1051
1052 (define-key rmail-mode-map [menu-bar delete expunge]
1053 '("Expunge" . rmail-expunge))
1054
1055 (define-key rmail-mode-map [menu-bar delete undelete]
1056 '("Undelete" . rmail-undelete-previous-message))
1057
1058 (define-key rmail-mode-map [menu-bar delete delete]
1059 '("Delete" . rmail-delete-forward))
1060
1061 (define-key rmail-mode-map [menu-bar move]
1062 (cons "Move" (make-sparse-keymap "Move")))
1063
1064 (define-key rmail-mode-map [menu-bar move search-back]
1065 '("Search Back..." . rmail-search-backwards))
1066
1067 (define-key rmail-mode-map [menu-bar move search]
1068 '("Search..." . rmail-search))
1069
1070 (define-key rmail-mode-map [menu-bar move previous]
1071 '("Previous Nondeleted" . rmail-previous-undeleted-message))
1072
1073 (define-key rmail-mode-map [menu-bar move next]
1074 '("Next Nondeleted" . rmail-next-undeleted-message))
1075
1076 (define-key rmail-mode-map [menu-bar move last]
1077 '("Last" . rmail-last-message))
1078
1079 (define-key rmail-mode-map [menu-bar move first]
1080 '("First" . rmail-first-message))
1081
1082 (define-key rmail-mode-map [menu-bar move previous]
1083 '("Previous" . rmail-previous-message))
1084
1085 (define-key rmail-mode-map [menu-bar move next]
1086 '("Next" . rmail-next-message))
1087 \f
1088 ;; Rmail mode is suitable only for specially formatted data.
1089 (put 'rmail-mode 'mode-class 'special)
1090
1091 (defun rmail-mode-kill-summary ()
1092 (if rmail-summary-buffer (kill-buffer rmail-summary-buffer)))
1093
1094 ;;;###autoload
1095 (defun rmail-mode ()
1096 "Rmail Mode is used by \\<rmail-mode-map>\\[rmail] for editing Rmail files.
1097 All normal editing commands are turned off.
1098 Instead, these commands are available:
1099
1100 \\[rmail-beginning-of-message] Move point to front of this message.
1101 \\[rmail-end-of-message] Move point to bottom of this message.
1102 \\[scroll-up] Scroll to next screen of this message.
1103 \\[scroll-down] Scroll to previous screen of this message.
1104 \\[rmail-next-undeleted-message] Move to Next non-deleted message.
1105 \\[rmail-previous-undeleted-message] Move to Previous non-deleted message.
1106 \\[rmail-next-message] Move to Next message whether deleted or not.
1107 \\[rmail-previous-message] Move to Previous message whether deleted or not.
1108 \\[rmail-first-message] Move to the first message in Rmail file.
1109 \\[rmail-last-message] Move to the last message in Rmail file.
1110 \\[rmail-show-message] Jump to message specified by numeric position in file.
1111 \\[rmail-search] Search for string and show message it is found in.
1112 \\[rmail-delete-forward] Delete this message, move to next nondeleted.
1113 \\[rmail-delete-backward] Delete this message, move to previous nondeleted.
1114 \\[rmail-undelete-previous-message] Undelete message. Tries current message, then earlier messages
1115 till a deleted message is found.
1116 \\[rmail-edit-current-message] Edit the current message. \\[rmail-cease-edit] to return to Rmail.
1117 \\[rmail-expunge] Expunge deleted messages.
1118 \\[rmail-expunge-and-save] Expunge and save the file.
1119 \\[rmail-quit] Quit Rmail: expunge, save, then switch to another buffer.
1120 \\[save-buffer] Save without expunging.
1121 \\[rmail-get-new-mail] Move new mail from system spool directory into this file.
1122 \\[rmail-mail] Mail a message (same as \\[mail-other-window]).
1123 \\[rmail-continue] Continue composing outgoing message started before.
1124 \\[rmail-reply] Reply to this message. Like \\[rmail-mail] but initializes some fields.
1125 \\[rmail-retry-failure] Send this message again. Used on a mailer failure message.
1126 \\[rmail-forward] Forward this message to another user.
1127 \\[rmail-output-to-rmail-file] Output this message to an Rmail file (append it).
1128 \\[rmail-output] Output this message to a Unix-format mail file (append it).
1129 \\[rmail-output-body-to-file] Save message body to a file. Default filename comes from Subject line.
1130 \\[rmail-input] Input Rmail file. Run Rmail on that file.
1131 \\[rmail-add-label] Add label to message. It will be displayed in the mode line.
1132 \\[rmail-kill-label] Kill label. Remove a label from current message.
1133 \\[rmail-next-labeled-message] Move to Next message with specified label
1134 (label defaults to last one specified).
1135 Standard labels: filed, unseen, answered, forwarded, deleted.
1136 Any other label is present only if you add it with \\[rmail-add-label].
1137 \\[rmail-previous-labeled-message] Move to Previous message with specified label
1138 \\[rmail-summary] Show headers buffer, with a one line summary of each message.
1139 \\[rmail-summary-by-labels] Summarize only messages with particular label(s).
1140 \\[rmail-summary-by-recipients] Summarize only messages with particular recipient(s).
1141 \\[rmail-summary-by-regexp] Summarize only messages with particular regexp(s).
1142 \\[rmail-summary-by-topic] Summarize only messages with subject line regexp(s).
1143 \\[rmail-toggle-header] Toggle display of complete header."
1144 (interactive)
1145 (let ((finding-rmail-file (not (eq major-mode 'rmail-mode))))
1146 (rmail-mode-2)
1147 (when (and finding-rmail-file
1148 (null coding-system-for-read)
1149 default-enable-multibyte-characters)
1150 (let ((rmail-enable-multibyte t))
1151 (rmail-require-mime-maybe)
1152 (rmail-convert-file)
1153 (goto-char (point-max))
1154 (set-buffer-multibyte t)))
1155 (rmail-set-message-counters)
1156 (rmail-show-message rmail-total-messages)
1157 (when finding-rmail-file
1158 (when rmail-display-summary
1159 (rmail-summary))
1160 (rmail-construct-io-menu))
1161 (run-mode-hooks 'rmail-mode-hook)))
1162
1163 (defun rmail-mode-2 ()
1164 (kill-all-local-variables)
1165 (rmail-mode-1)
1166 (rmail-perm-variables)
1167 (rmail-variables))
1168
1169 (defun rmail-mode-1 ()
1170 (setq major-mode 'rmail-mode)
1171 (setq mode-name "RMAIL")
1172 (setq buffer-read-only t)
1173 ;; No need to auto save RMAIL files in normal circumstances
1174 ;; because they contain no info except attribute changes
1175 ;; and deletion of messages.
1176 ;; The one exception is when messages are copied into an Rmail mode buffer.
1177 ;; rmail-output-to-rmail-file enables auto save when you do that.
1178 (setq buffer-auto-save-file-name nil)
1179 (setq mode-line-modified "--")
1180 (use-local-map rmail-mode-map)
1181 (set-syntax-table text-mode-syntax-table)
1182 (setq local-abbrev-table text-mode-abbrev-table))
1183
1184 ;; Set up the permanent locals associated with an Rmail file.
1185 (defun rmail-perm-variables ()
1186 (make-local-variable 'rmail-last-label)
1187 (make-local-variable 'rmail-last-regexp)
1188 (make-local-variable 'rmail-deleted-vector)
1189 (make-local-variable 'rmail-buffer)
1190 (setq rmail-buffer (current-buffer))
1191 (make-local-variable 'rmail-view-buffer)
1192 (setq rmail-view-buffer rmail-buffer)
1193 (make-local-variable 'rmail-summary-buffer)
1194 (make-local-variable 'rmail-summary-vector)
1195 (make-local-variable 'rmail-current-message)
1196 (make-local-variable 'rmail-total-messages)
1197 (make-local-variable 'rmail-overlay-list)
1198 (setq rmail-overlay-list nil)
1199 (make-local-variable 'rmail-message-vector)
1200 (make-local-variable 'rmail-msgref-vector)
1201 (make-local-variable 'rmail-inbox-list)
1202 (setq rmail-inbox-list (rmail-parse-file-inboxes))
1203 ;; Provide default set of inboxes for primary mail file ~/RMAIL.
1204 (and (null rmail-inbox-list)
1205 (or (equal buffer-file-name (expand-file-name rmail-file-name))
1206 (equal buffer-file-truename
1207 (abbreviate-file-name (file-truename rmail-file-name))))
1208 (setq rmail-inbox-list
1209 (or rmail-primary-inbox-list
1210 (list (or (getenv "MAIL")
1211 (concat rmail-spool-directory
1212 (user-login-name)))))))
1213 (make-local-variable 'rmail-keywords)
1214 ;; this gets generated as needed
1215 (setq rmail-keywords nil))
1216
1217 ;; Set up the non-permanent locals associated with Rmail mode.
1218 (defun rmail-variables ()
1219 (make-local-variable 'save-buffer-coding-system)
1220 ;; If we don't already have a value for save-buffer-coding-system,
1221 ;; get it from buffer-file-coding-system, and clear that
1222 ;; because it should be determined in rmail-show-message.
1223 (unless save-buffer-coding-system
1224 (setq save-buffer-coding-system (or buffer-file-coding-system 'undecided))
1225 (setq buffer-file-coding-system nil))
1226 ;; Don't let a local variables list in a message cause confusion.
1227 (make-local-variable 'local-enable-local-variables)
1228 (setq local-enable-local-variables nil)
1229 (make-local-variable 'revert-buffer-function)
1230 (setq revert-buffer-function 'rmail-revert)
1231 (make-local-variable 'font-lock-defaults)
1232 (setq font-lock-defaults
1233 '(rmail-font-lock-keywords
1234 t t nil nil
1235 (font-lock-maximum-size . nil)
1236 (font-lock-fontify-buffer-function . rmail-fontify-buffer-function)
1237 (font-lock-unfontify-buffer-function . rmail-unfontify-buffer-function)
1238 (font-lock-inhibit-thing-lock . (lazy-lock-mode fast-lock-mode))))
1239 (make-local-variable 'require-final-newline)
1240 (setq require-final-newline nil)
1241 (make-local-variable 'version-control)
1242 (setq version-control 'never)
1243 (make-local-variable 'kill-buffer-hook)
1244 (add-hook 'kill-buffer-hook 'rmail-mode-kill-summary)
1245 (make-local-variable 'file-precious-flag)
1246 (setq file-precious-flag t)
1247 (make-local-variable 'desktop-save-buffer)
1248 (setq desktop-save-buffer t))
1249
1250 ;; Handle M-x revert-buffer done in an rmail-mode buffer.
1251 (defun rmail-revert (arg noconfirm)
1252 (set-buffer rmail-buffer)
1253 (let* ((revert-buffer-function (default-value 'revert-buffer-function))
1254 (rmail-enable-multibyte enable-multibyte-characters)
1255 ;; See similar code in `rmail'.
1256 (coding-system-for-read (and rmail-enable-multibyte 'raw-text)))
1257 ;; Call our caller again, but this time it does the default thing.
1258 (if (revert-buffer arg noconfirm)
1259 ;; If the user said "yes", and we changed something,
1260 ;; reparse the messages.
1261 (progn
1262 (set-buffer rmail-buffer)
1263 (rmail-mode-2)
1264 ;; Convert all or part to Babyl file if possible.
1265 (rmail-convert-file)
1266 ;; We have read the file as raw-text, so the buffer is set to
1267 ;; unibyte. Make it multibyte if necessary.
1268 (if (and rmail-enable-multibyte
1269 (not enable-multibyte-characters))
1270 (set-buffer-multibyte t))
1271 (goto-char (point-max))
1272 (rmail-set-message-counters)
1273 (rmail-show-message rmail-total-messages)
1274 (run-hooks 'rmail-mode-hook)))))
1275
1276 ;; Return a list of files from this buffer's Mail: option.
1277 ;; Does not assume that messages have been parsed.
1278 ;; Just returns nil if buffer does not look like Babyl format.
1279 (defun rmail-parse-file-inboxes ()
1280 (save-excursion
1281 (save-restriction
1282 (widen)
1283 (goto-char 1)
1284 (cond ((looking-at "BABYL OPTIONS:")
1285 (search-forward "\n\^_" nil 'move)
1286 (narrow-to-region 1 (point))
1287 (goto-char 1)
1288 (if (search-forward "\nMail:" nil t)
1289 (progn
1290 (narrow-to-region (point) (progn (end-of-line) (point)))
1291 (goto-char (point-min))
1292 (mail-parse-comma-list))))))))
1293
1294 (defun rmail-expunge-and-save ()
1295 "Expunge and save RMAIL file."
1296 (interactive)
1297 (rmail-expunge)
1298 (set-buffer rmail-buffer)
1299 (save-buffer)
1300 (if (rmail-summary-exists)
1301 (rmail-select-summary (set-buffer-modified-p nil))))
1302
1303 (defun rmail-quit ()
1304 "Quit out of RMAIL.
1305 Hook `rmail-quit-hook' is run after expunging."
1306 (interactive)
1307 (rmail-expunge-and-save)
1308 (when (boundp 'rmail-quit-hook)
1309 (run-hooks 'rmail-quit-hook))
1310 ;; Don't switch to the summary buffer even if it was recently visible.
1311 (when rmail-summary-buffer
1312 (replace-buffer-in-windows rmail-summary-buffer)
1313 (bury-buffer rmail-summary-buffer))
1314 (if rmail-enable-mime
1315 (let ((obuf rmail-buffer)
1316 (ovbuf rmail-view-buffer))
1317 (set-buffer rmail-view-buffer)
1318 (quit-window)
1319 (replace-buffer-in-windows ovbuf)
1320 (replace-buffer-in-windows obuf)
1321 (bury-buffer obuf))
1322 (let ((obuf (current-buffer)))
1323 (quit-window)
1324 (replace-buffer-in-windows obuf))))
1325
1326 (defun rmail-bury ()
1327 "Bury current Rmail buffer and its summary buffer."
1328 (interactive)
1329 ;; This let var was called rmail-buffer, but that interfered
1330 ;; with the buffer-local var used in summary buffers.
1331 (let ((buffer-to-bury (current-buffer)))
1332 (if (rmail-summary-exists)
1333 (let (window)
1334 (while (setq window (get-buffer-window rmail-summary-buffer))
1335 (quit-window nil window))
1336 (bury-buffer rmail-summary-buffer)))
1337 (quit-window)))
1338
1339 (defun rmail-duplicate-message ()
1340 "Create a duplicated copy of the current message.
1341 The duplicate copy goes into the Rmail file just after the
1342 original copy."
1343 (interactive)
1344 (widen)
1345 (let ((buffer-read-only nil)
1346 (number rmail-current-message)
1347 (string (buffer-substring (rmail-msgbeg rmail-current-message)
1348 (rmail-msgend rmail-current-message))))
1349 (goto-char (rmail-msgend rmail-current-message))
1350 (insert string)
1351 (rmail-forget-messages)
1352 (rmail-show-message number)
1353 (message "Message duplicated")))
1354
1355 ;;;###autoload
1356 (defun rmail-input (filename)
1357 "Run Rmail on file FILENAME."
1358 (interactive "FRun rmail on RMAIL file: ")
1359 (rmail filename))
1360
1361
1362 ;; This used to scan subdirectories recursively, but someone pointed out
1363 ;; that if the user wants that, person can put all the files in one dir.
1364 ;; And the recursive scan was slow. So I took it out.
1365 ;; rms, Sep 1996.
1366 (defun rmail-find-all-files (start)
1367 "Return list of file in dir START that match `rmail-secondary-file-regexp'."
1368 (if (file-accessible-directory-p start)
1369 ;; Don't sort here.
1370 (let* ((case-fold-search t)
1371 (files (directory-files start t rmail-secondary-file-regexp)))
1372 ;; Sort here instead of in directory-files
1373 ;; because this list is usually much shorter.
1374 (sort files 'string<))))
1375
1376 (defun rmail-list-to-menu (menu-name l action &optional full-name)
1377 (let ((menu (make-sparse-keymap menu-name)))
1378 (mapcar
1379 (function (lambda (item)
1380 (let (command)
1381 (if (consp item)
1382 (progn
1383 (setq command
1384 (rmail-list-to-menu (car item) (cdr item)
1385 action
1386 (if full-name
1387 (concat full-name "/"
1388 (car item))
1389 (car item))))
1390 (setq name (car item)))
1391 (progn
1392 (setq name item)
1393 (setq command
1394 (list 'lambda () '(interactive)
1395 (list action
1396 (expand-file-name
1397 (if full-name
1398 (concat full-name "/" item)
1399 item)
1400 rmail-secondary-file-directory))))))
1401 (define-key menu (vector (intern name))
1402 (cons name command)))))
1403 (reverse l))
1404 menu))
1405
1406 ;; This command is always "disabled" when it appears in a menu.
1407 (put 'rmail-disable-menu 'menu-enable ''nil)
1408
1409 (defun rmail-construct-io-menu ()
1410 (let ((files (rmail-find-all-files rmail-secondary-file-directory)))
1411 (if files
1412 (progn
1413 (define-key rmail-mode-map [menu-bar classify input-menu]
1414 (cons "Input Rmail File"
1415 (rmail-list-to-menu "Input Rmail File"
1416 files
1417 'rmail-input)))
1418 (define-key rmail-mode-map [menu-bar classify output-menu]
1419 (cons "Output Rmail File"
1420 (rmail-list-to-menu "Output Rmail File"
1421 files
1422 'rmail-output-to-rmail-file))))
1423
1424 (define-key rmail-mode-map [menu-bar classify input-menu]
1425 '("Input Rmail File" . rmail-disable-menu))
1426 (define-key rmail-mode-map [menu-bar classify output-menu]
1427 '("Output Rmail File" . rmail-disable-menu)))))
1428
1429 \f
1430 ;;;; *** Rmail input ***
1431
1432 ;; RLK feature not added in this version:
1433 ;; argument specifies inbox file or files in various ways.
1434
1435 (defun rmail-get-new-mail (&optional file-name)
1436 "Move any new mail from this RMAIL file's inbox files.
1437 The inbox files can be specified with the file's Mail: option. The
1438 variable `rmail-primary-inbox-list' specifies the inboxes for your
1439 primary RMAIL file if it has no Mail: option. By default, this is
1440 your /usr/spool/mail/$USER.
1441
1442 You can also specify the file to get new mail from. In this case, the
1443 file of new mail is not changed or deleted. Noninteractively, you can
1444 pass the inbox file name as an argument. Interactively, a prefix
1445 argument causes us to read a file name and use that file as the inbox.
1446
1447 If the variable `rmail-preserve-inbox' is non-nil, new mail will
1448 always be left in inbox files rather than deleted.
1449
1450 This function runs `rmail-get-new-mail-hook' before saving the updated file.
1451 It returns t if it got any new messages."
1452 (interactive
1453 (list (if current-prefix-arg
1454 (read-file-name "Get new mail from file: "))))
1455 (run-hooks 'rmail-before-get-new-mail-hook)
1456 ;; If the disk file has been changed from under us,
1457 ;; revert to it before we get new mail.
1458 (or (verify-visited-file-modtime (current-buffer))
1459 (find-file (buffer-file-name)))
1460 (set-buffer rmail-buffer)
1461 (rmail-maybe-set-message-counters)
1462 (widen)
1463 ;; Get rid of all undo records for this buffer.
1464 (or (eq buffer-undo-list t)
1465 (setq buffer-undo-list nil))
1466 (let ((all-files (if file-name (list file-name)
1467 rmail-inbox-list))
1468 (rmail-enable-multibyte (default-value 'enable-multibyte-characters))
1469 found)
1470 (unwind-protect
1471 (progn
1472 (while all-files
1473 (let ((opoint (point))
1474 (new-messages 0)
1475 (rsf-number-of-spam 0)
1476 (delete-files ())
1477 ;; If buffer has not changed yet, and has not been saved yet,
1478 ;; don't replace the old backup file now.
1479 (make-backup-files (and make-backup-files (buffer-modified-p)))
1480 (buffer-read-only nil)
1481 ;; Don't make undo records for what we do in getting mail.
1482 (buffer-undo-list t)
1483 success
1484 ;; Files to insert this time around.
1485 files
1486 ;; Last names of those files.
1487 file-last-names)
1488 ;; Pull files off all-files onto files
1489 ;; as long as there is no name conflict.
1490 ;; A conflict happens when two inbox file names
1491 ;; have the same last component.
1492 (while (and all-files
1493 (not (member (file-name-nondirectory (car all-files))
1494 file-last-names)))
1495 (setq files (cons (car all-files) files)
1496 file-last-names
1497 (cons (file-name-nondirectory (car all-files)) files))
1498 (setq all-files (cdr all-files)))
1499 ;; Put them back in their original order.
1500 (setq files (nreverse files))
1501
1502 (goto-char (point-max))
1503 (skip-chars-backward " \t\n") ; just in case of brain damage
1504 (delete-region (point) (point-max)) ; caused by require-final-newline
1505 (save-excursion
1506 (save-restriction
1507 (narrow-to-region (point) (point))
1508 ;; Read in the contents of the inbox files,
1509 ;; renaming them as necessary,
1510 ;; and adding to the list of files to delete eventually.
1511 (if file-name
1512 (rmail-insert-inbox-text files nil)
1513 (setq delete-files (rmail-insert-inbox-text files t)))
1514 ;; Scan the new text and convert each message to babyl format.
1515 (goto-char (point-min))
1516 (unwind-protect
1517 (save-excursion
1518 (setq new-messages (rmail-convert-to-babyl-format)
1519 success t))
1520 ;; Try to delete the garbage just inserted.
1521 (or success (delete-region (point-min) (point-max)))
1522 ;; If we could not convert the file's inboxes,
1523 ;; rename the files we tried to read
1524 ;; so we won't over and over again.
1525 (if (and (not file-name) (not success))
1526 (let ((delfiles delete-files)
1527 (count 0))
1528 (while delfiles
1529 (while (file-exists-p (format "RMAILOSE.%d" count))
1530 (setq count (1+ count)))
1531 (rename-file (car delfiles)
1532 (format "RMAILOSE.%d" count))
1533 (setq delfiles (cdr delfiles))))))
1534 (or (zerop new-messages)
1535 (let (success)
1536 (widen)
1537 (search-backward "\n\^_" nil t)
1538 (narrow-to-region (point) (point-max))
1539 (goto-char (1+ (point-min)))
1540 (rmail-count-new-messages)
1541 (run-hooks 'rmail-get-new-mail-hook)
1542 (save-buffer)))
1543 ;; Delete the old files, now that babyl file is saved.
1544 (while delete-files
1545 (condition-case ()
1546 ;; First, try deleting.
1547 (condition-case ()
1548 (delete-file (car delete-files))
1549 (file-error
1550 ;; If we can't delete it, truncate it.
1551 (write-region (point) (point) (car delete-files))))
1552 (file-error nil))
1553 (setq delete-files (cdr delete-files)))))
1554 (if (= new-messages 0)
1555 (progn (goto-char opoint)
1556 (if (or file-name rmail-inbox-list)
1557 (message "(No new mail has arrived)")))
1558 ;; check new messages to see if any of them is spam:
1559 (if (and (featurep 'rmail-spam-filter)
1560 rmail-use-spam-filter)
1561 (let*
1562 ((old-messages (- rmail-total-messages new-messages))
1563 (rsf-scanned-message-number (1+ old-messages))
1564 ;; save deletion flags of old messages: vector starts
1565 ;; at zero (is one longer that no of messages),
1566 ;; therefore take 1+ old-messages
1567 (save-deleted
1568 (substring rmail-deleted-vector 0 (1+
1569 old-messages))))
1570 ;; set all messages to undeleted
1571 (setq rmail-deleted-vector
1572 (make-string (1+ rmail-total-messages) ?\ ))
1573 (while (<= rsf-scanned-message-number
1574 rmail-total-messages)
1575 (progn
1576 (if (not (rmail-spam-filter rsf-scanned-message-number))
1577 (progn (setq rsf-number-of-spam (1+ rsf-number-of-spam)))
1578 )
1579 (setq rsf-scanned-message-number (1+ rsf-scanned-message-number))
1580 ))
1581 (if (> rsf-number-of-spam 0)
1582 (progn
1583 (when (rmail-expunge-confirmed)
1584 (rmail-only-expunge t))
1585 ))
1586 (setq rmail-deleted-vector
1587 (concat
1588 save-deleted
1589 (make-string (- rmail-total-messages old-messages)
1590 ?\ )))
1591 ))
1592 (if (rmail-summary-exists)
1593 (rmail-select-summary
1594 (rmail-update-summary)))
1595 (message "%d new message%s read%s"
1596 new-messages (if (= 1 new-messages) "" "s")
1597 ;; print out a message on number of spam messages found:
1598 (if (and (featurep 'rmail-spam-filter)
1599 rmail-use-spam-filter
1600 (> rsf-number-of-spam 0))
1601 (if (= 1 new-messages)
1602 ", and found to be a spam message"
1603 (if (> rsf-number-of-spam 1)
1604 (format ", %d of which found to be spam messages"
1605 rsf-number-of-spam)
1606 ", one of which found to be a spam message"))
1607 ""))
1608 (if (and (featurep 'rmail-spam-filter)
1609 rmail-use-spam-filter
1610 (> rsf-number-of-spam 0))
1611 (progn (if rsf-beep (beep t))
1612 (sleep-for rsf-sleep-after-message)))
1613
1614 ;; Move to the first new message
1615 ;; unless we have other unseen messages before it.
1616 (rmail-show-message (rmail-first-unseen-message))
1617 (run-hooks 'rmail-after-get-new-mail-hook)
1618 (setq found t))))
1619 found)
1620 ;; Don't leave the buffer screwed up if we get a disk-full error.
1621 (or found (rmail-show-message)))))
1622
1623 (defun rmail-parse-url (file)
1624 "Parse the supplied URL. Return (list MAILBOX-NAME REMOTE PASSWORD GOT-PASSWORD)
1625 WHERE MAILBOX-NAME is the name of the mailbox suitable as argument to the
1626 actual version of `movemail', REMOTE is non-nil if MAILBOX-NAME refers to
1627 a remote mailbox, PASSWORD is the password if it should be
1628 supplied as a separate argument to `movemail' or nil otherwise, GOT-PASSWORD
1629 is non-nil if the user has supplied the password interactively.
1630 "
1631 (cond
1632 ((string-match "^\\([^:]+\\)://\\(\\([^:@]+\\)\\(:\\([^@]+\\)\\)?@\\)?.*" file)
1633 (let (got-password supplied-password
1634 (proto (match-string 1 file))
1635 (user (match-string 3 file))
1636 (pass (match-string 5 file))
1637 (host (substring file (or (match-end 2)
1638 (+ 3 (match-end 1))))))
1639
1640 (if (not pass)
1641 (when rmail-remote-password-required
1642 (setq got-password (not (rmail-have-password)))
1643 (setq supplied-password (rmail-get-remote-password
1644 (string-equal proto "imap")))))
1645
1646 (if (rmail-movemail-variant-p 'emacs)
1647 (if (string-equal proto "pop")
1648 (list (concat "po:" user ":" host)
1649 t
1650 (or pass supplied-password)
1651 got-password)
1652 (error "Emacs movemail does not support %s protocol" proto))
1653 (list file
1654 (or (string-equal proto "pop") (string-equal proto "imap"))
1655 supplied-password
1656 got-password))))
1657
1658 ((string-match "^po:\\([^:]+\\)\\(:\\(.*\\)\\)?" file)
1659 (let (got-password supplied-password
1660 (proto "pop")
1661 (user (match-string 1 file))
1662 (host (match-string 3 file)))
1663
1664 (when rmail-remote-password-required
1665 (setq got-password (not (rmail-have-password)))
1666 (setq supplied-password (rmail-get-remote-password nil)))
1667
1668 (list file "pop" supplied-password got-password)))
1669
1670 (t
1671 (list file nil nil nil))))
1672
1673 (defun rmail-insert-inbox-text (files renamep)
1674 ;; Detect a locked file now, so that we avoid moving mail
1675 ;; out of the real inbox file. (That could scare people.)
1676 (or (memq (file-locked-p buffer-file-name) '(nil t))
1677 (error "RMAIL file %s is locked"
1678 (file-name-nondirectory buffer-file-name)))
1679 (let (file tofile delete-files movemail popmail got-password password)
1680 (while files
1681 ;; Handle remote mailbox names specially; don't expand as filenames
1682 ;; in case the userid contains a directory separator.
1683 (setq file (car files))
1684 (let ((url-data (rmail-parse-url file)))
1685 (setq file (nth 0 url-data))
1686 (setq popmail (nth 1 url-data))
1687 (setq password (nth 2 url-data))
1688 (setq got-password (nth 3 url-data)))
1689
1690 (if popmail
1691 (setq renamep t)
1692 (setq file (file-truename
1693 (substitute-in-file-name (expand-file-name file)))))
1694 (setq tofile (expand-file-name
1695 ;; Generate name to move to from inbox name,
1696 ;; in case of multiple inboxes that need moving.
1697 (concat ".newmail-"
1698 (file-name-nondirectory
1699 (if (memq system-type '(windows-nt cygwin))
1700 ;; cannot have "po:" in file name
1701 (substring file 3)
1702 file)))
1703 ;; Use the directory of this rmail file
1704 ;; because it's a nuisance to use the homedir
1705 ;; if that is on a full disk and this rmail
1706 ;; file isn't.
1707 (file-name-directory
1708 (expand-file-name buffer-file-name))))
1709 ;; Always use movemail to rename the file,
1710 ;; since there can be mailboxes in various directories.
1711 (if (not popmail)
1712 (progn
1713 ;; On some systems, /usr/spool/mail/foo is a directory
1714 ;; and the actual inbox is /usr/spool/mail/foo/foo.
1715 (if (file-directory-p file)
1716 (setq file (expand-file-name (user-login-name)
1717 file)))))
1718 (cond (popmail
1719 (message "Getting mail from the remote server ..."))
1720 ((and (file-exists-p tofile)
1721 (/= 0 (nth 7 (file-attributes tofile))))
1722 (message "Getting mail from %s..." tofile))
1723 ((and (file-exists-p file)
1724 (/= 0 (nth 7 (file-attributes file))))
1725 (message "Getting mail from %s..." file)))
1726 ;; Set TOFILE if have not already done so, and
1727 ;; rename or copy the file FILE to TOFILE if and as appropriate.
1728 (cond ((not renamep)
1729 (setq tofile file))
1730 ((or (file-exists-p tofile) (and (not popmail)
1731 (not (file-exists-p file))))
1732 nil)
1733 (t
1734 (with-temp-buffer
1735 (let ((errors (current-buffer)))
1736 (buffer-disable-undo errors)
1737 (let ((args
1738 (append
1739 (list (or rmail-movemail-program
1740 (expand-file-name "movemail"
1741 exec-directory))
1742 nil errors nil)
1743 (if rmail-preserve-inbox
1744 (list "-p")
1745 nil)
1746 (if (rmail-movemail-variant-p 'mailutils)
1747 (append (list "--emacs") rmail-movemail-flags)
1748 rmail-movemail-flags)
1749 (list file tofile)
1750 (if password (list password) nil))))
1751 (apply 'call-process args))
1752 (if (not (buffer-modified-p errors))
1753 ;; No output => movemail won
1754 nil
1755 (set-buffer errors)
1756 (subst-char-in-region (point-min) (point-max)
1757 ?\n ?\ )
1758 (goto-char (point-max))
1759 (skip-chars-backward " \t")
1760 (delete-region (point) (point-max))
1761 (goto-char (point-min))
1762 (if (looking-at "movemail: ")
1763 (delete-region (point-min) (match-end 0)))
1764 (beep t)
1765 ;; If we just read the password, most likely it is
1766 ;; wrong. Otherwise, see if there is a specific
1767 ;; reason to think that the problem is a wrong passwd.
1768 (if (or got-password
1769 (re-search-forward rmail-remote-password-error
1770 nil t))
1771 (rmail-set-remote-password nil))
1772
1773 ;; If using Mailutils, remove initial error code
1774 ;; abbreviation
1775 (when (rmail-movemail-variant-p 'mailutils)
1776 (goto-char (point-min))
1777 (when (looking-at "[A-Z][A-Z0-9_]*:")
1778 (delete-region (point-min) (match-end 0))))
1779
1780 (message "movemail: %s"
1781 (buffer-substring (point-min)
1782 (point-max)))
1783
1784 (sit-for 3)
1785 nil)))))
1786
1787 ;; At this point, TOFILE contains the name to read:
1788 ;; Either the alternate name (if we renamed)
1789 ;; or the actual inbox (if not renaming).
1790 (if (file-exists-p tofile)
1791 (let ((coding-system-for-read 'no-conversion)
1792 size)
1793 (goto-char (point-max))
1794 (setq size (nth 1 (insert-file-contents tofile)))
1795 (goto-char (point-max))
1796 (or (= (preceding-char) ?\n)
1797 (zerop size)
1798 (insert ?\n))
1799 (if (not (and rmail-preserve-inbox (string= file tofile)))
1800 (setq delete-files (cons tofile delete-files)))))
1801 (message "")
1802 (setq files (cdr files)))
1803 delete-files))
1804
1805 ;; Decode the region specified by FROM and TO by CODING.
1806 ;; If CODING is nil or an invalid coding system, decode by `undecided'.
1807 (defun rmail-decode-region (from to coding)
1808 (if (or (not coding) (not (coding-system-p coding)))
1809 (setq coding 'undecided))
1810 ;; Use -dos decoding, to remove ^M characters left from base64 or
1811 ;; rogue qp-encoded text.
1812 (decode-coding-region from to
1813 (coding-system-change-eol-conversion coding 1))
1814 ;; Don't reveal the fact we used -dos decoding, as users generally
1815 ;; will not expect the RMAIL buffer to use DOS EOL format.
1816 (setq buffer-file-coding-system
1817 (setq last-coding-system-used
1818 (coding-system-change-eol-conversion coding 0))))
1819
1820 ;; the rmail-break-forwarded-messages feature is not implemented
1821 (defun rmail-convert-to-babyl-format ()
1822 (let ((count 0) start
1823 (case-fold-search nil)
1824 (invalid-input-resync
1825 (function (lambda ()
1826 (message "Invalid Babyl format in inbox!")
1827 (sit-for 3)
1828 ;; Try to get back in sync with a real message.
1829 (if (re-search-forward
1830 (concat rmail-mmdf-delim1 "\\|^From") nil t)
1831 (beginning-of-line)
1832 (goto-char (point-max)))))))
1833 (goto-char (point-min))
1834 (save-restriction
1835 (while (not (eobp))
1836 (setq start (point))
1837 (cond ((looking-at "BABYL OPTIONS:");Babyl header
1838 (if (search-forward "\n\^_" nil t)
1839 ;; If we find the proper terminator, delete through there.
1840 (delete-region (point-min) (point))
1841 (funcall invalid-input-resync)
1842 (delete-region (point-min) (point))))
1843 ;; Babyl format message
1844 ((looking-at "\^L")
1845 (or (search-forward "\n\^_" nil t)
1846 (funcall invalid-input-resync))
1847 (setq count (1+ count))
1848 ;; Make sure there is no extra white space after the ^_
1849 ;; at the end of the message.
1850 ;; Narrowing will make sure that whatever follows the junk
1851 ;; will be treated properly.
1852 (delete-region (point)
1853 (save-excursion
1854 (skip-chars-forward " \t\n")
1855 (point)))
1856 (save-excursion
1857 (let* ((header-end
1858 (progn
1859 (save-excursion
1860 (goto-char start)
1861 (forward-line 1)
1862 (if (looking-at "0")
1863 (forward-line 1)
1864 (forward-line 2))
1865 (save-restriction
1866 (narrow-to-region (point) (point-max))
1867 (rfc822-goto-eoh)
1868 (point)))))
1869 (case-fold-search t)
1870 (quoted-printable-header-field-end
1871 (save-excursion
1872 (goto-char start)
1873 (re-search-forward
1874 "^content-transfer-encoding:\\(\n?[\t ]\\)*quoted-printable\\(\n?[\t ]\\)*"
1875 header-end t)))
1876 (base64-header-field-end
1877 (save-excursion
1878 (goto-char start)
1879 (re-search-forward
1880 "^content-transfer-encoding:\\(\n?[\t ]\\)*base64\\(\n?[\t ]\\)*"
1881 header-end t))))
1882 (if quoted-printable-header-field-end
1883 (save-excursion
1884 (unless
1885 (mail-unquote-printable-region header-end (point) nil t t)
1886 (message "Malformed MIME quoted-printable message"))
1887 ;; Change "quoted-printable" to "8bit",
1888 ;; to reflect the decoding we just did.
1889 (goto-char quoted-printable-header-field-end)
1890 (delete-region (point) (search-backward ":"))
1891 (insert ": 8bit")))
1892 (if base64-header-field-end
1893 (save-excursion
1894 (when
1895 (condition-case nil
1896 (progn
1897 (base64-decode-region (1+ header-end)
1898 (- (point) 2))
1899 t)
1900 (error nil))
1901 ;; Change "base64" to "8bit", to reflect the
1902 ;; decoding we just did.
1903 (goto-char base64-header-field-end)
1904 (delete-region (point) (search-backward ":"))
1905 (insert ": 8bit"))))
1906 (setq last-coding-system-used nil)
1907 (or rmail-enable-mime
1908 (not rmail-enable-multibyte)
1909 (let ((mime-charset
1910 (if (and rmail-decode-mime-charset
1911 (save-excursion
1912 (goto-char start)
1913 (search-forward "\n\n" nil t)
1914 (let ((case-fold-search t))
1915 (re-search-backward
1916 rmail-mime-charset-pattern
1917 start t))))
1918 (intern (downcase (match-string 1))))))
1919 (rmail-decode-region start (point) mime-charset)))))
1920 ;; Add an X-Coding-System: header if we don't have one.
1921 (save-excursion
1922 (goto-char start)
1923 (forward-line 1)
1924 (if (looking-at "0")
1925 (forward-line 1)
1926 (forward-line 2))
1927 (or (save-restriction
1928 (narrow-to-region (point) (point-max))
1929 (rfc822-goto-eoh)
1930 (goto-char (point-min))
1931 (re-search-forward "^X-Coding-System:" nil t))
1932 (insert "X-Coding-System: "
1933 (symbol-name last-coding-system-used)
1934 "\n")))
1935 (narrow-to-region (point) (point-max))
1936 (and (= 0 (% count 10))
1937 (message "Converting to Babyl format...%d" count)))
1938 ;;*** MMDF format
1939 ((let ((case-fold-search t))
1940 (looking-at rmail-mmdf-delim1))
1941 (let ((case-fold-search t))
1942 (replace-match "\^L\n0, unseen,,\n*** EOOH ***\n")
1943 (re-search-forward rmail-mmdf-delim2 nil t)
1944 (replace-match "\^_"))
1945 (save-excursion
1946 (save-restriction
1947 (narrow-to-region start (1- (point)))
1948 (goto-char (point-min))
1949 (while (search-forward "\n\^_" nil t); single char "\^_"
1950 (replace-match "\n^_")))); 2 chars: "^" and "_"
1951 (setq last-coding-system-used nil)
1952 (or rmail-enable-mime
1953 (not rmail-enable-multibyte)
1954 (decode-coding-region start (point) 'undecided))
1955 (save-excursion
1956 (goto-char start)
1957 (forward-line 3)
1958 (insert "X-Coding-System: "
1959 (symbol-name last-coding-system-used)
1960 "\n"))
1961 (narrow-to-region (point) (point-max))
1962 (setq count (1+ count))
1963 (and (= 0 (% count 10))
1964 (message "Converting to Babyl format...%d" count)))
1965 ;;*** Mail format
1966 ((looking-at "^From ")
1967 (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
1968 (rmail-nuke-pinhead-header)
1969 ;; If this message has a Content-Length field,
1970 ;; skip to the end of the contents.
1971 (let* ((header-end (save-excursion
1972 (and (re-search-forward "\n\n" nil t)
1973 (1- (point)))))
1974 (case-fold-search t)
1975 (quoted-printable-header-field-end
1976 (save-excursion
1977 (re-search-forward
1978 "^content-transfer-encoding:\\(\n?[\t ]\\)*quoted-printable\\(\n?[\t ]\\)*"
1979 header-end t)))
1980 (base64-header-field-end
1981 (save-excursion
1982 (re-search-forward
1983 "^content-transfer-encoding:\\(\n?[\t ]\\)*base64\\(\n?[\t ]\\)*"
1984 header-end t)))
1985 (size
1986 ;; Get the numeric value from the Content-Length field.
1987 (save-excursion
1988 ;; Back up to end of prev line,
1989 ;; in case the Content-Length field comes first.
1990 (forward-char -1)
1991 (and (search-forward "\ncontent-length: "
1992 header-end t)
1993 (let ((beg (point))
1994 (eol (progn (end-of-line) (point))))
1995 (string-to-number (buffer-substring beg eol)))))))
1996 (and size
1997 (if (and (natnump size)
1998 (<= (+ header-end size) (point-max))
1999 ;; Make sure this would put us at a position
2000 ;; that we could continue from.
2001 (save-excursion
2002 (goto-char (+ header-end size))
2003 (skip-chars-forward "\n")
2004 (or (eobp)
2005 (and (looking-at "BABYL OPTIONS:")
2006 (search-forward "\n\^_" nil t))
2007 (and (looking-at "\^L")
2008 (search-forward "\n\^_" nil t))
2009 (let ((case-fold-search t))
2010 (looking-at rmail-mmdf-delim1))
2011 (looking-at "From "))))
2012 (goto-char (+ header-end size))
2013 (message "Ignoring invalid Content-Length field")
2014 (sit-for 1 0 t)))
2015 (if (let ((case-fold-search nil))
2016 (re-search-forward
2017 (concat "^[\^_]?\\("
2018 rmail-unix-mail-delimiter
2019 "\\|"
2020 rmail-mmdf-delim1 "\\|"
2021 "^BABYL OPTIONS:\\|"
2022 "\^L\n[01],\\)") nil t))
2023 (goto-char (match-beginning 1))
2024 (goto-char (point-max)))
2025 (setq count (1+ count))
2026 (if quoted-printable-header-field-end
2027 (save-excursion
2028 (unless
2029 (mail-unquote-printable-region header-end (point) nil t t)
2030 (message "Malformed MIME quoted-printable message"))
2031 ;; Change "quoted-printable" to "8bit",
2032 ;; to reflect the decoding we just did.
2033 (goto-char quoted-printable-header-field-end)
2034 (delete-region (point) (search-backward ":"))
2035 (insert ": 8bit")))
2036 (if base64-header-field-end
2037 (save-excursion
2038 (when
2039 (condition-case nil
2040 (progn
2041 (base64-decode-region
2042 (1+ header-end)
2043 (save-excursion
2044 ;; Prevent base64-decode-region
2045 ;; from removing newline characters.
2046 (skip-chars-backward "\n\t ")
2047 (point)))
2048 t)
2049 (error nil))
2050 ;; Change "base64" to "8bit", to reflect the
2051 ;; decoding we just did.
2052 (goto-char base64-header-field-end)
2053 (delete-region (point) (search-backward ":"))
2054 (insert ": 8bit")))))
2055
2056 (save-excursion
2057 (save-restriction
2058 (narrow-to-region start (point))
2059 (goto-char (point-min))
2060 (while (search-forward "\n\^_" nil t); single char
2061 (replace-match "\n^_")))); 2 chars: "^" and "_"
2062 ;; This is for malformed messages that don't end in newline.
2063 ;; There shouldn't be any, but some users say occasionally
2064 ;; there are some.
2065 (or (bolp) (newline))
2066 (insert ?\^_)
2067 (setq last-coding-system-used nil)
2068 (or rmail-enable-mime
2069 (not rmail-enable-multibyte)
2070 (let ((mime-charset
2071 (if (and rmail-decode-mime-charset
2072 (save-excursion
2073 (goto-char start)
2074 (search-forward "\n\n" nil t)
2075 (let ((case-fold-search t))
2076 (re-search-backward
2077 rmail-mime-charset-pattern
2078 start t))))
2079 (intern (downcase (match-string 1))))))
2080 (rmail-decode-region start (point) mime-charset)))
2081 (save-excursion
2082 (goto-char start)
2083 (forward-line 3)
2084 (insert "X-Coding-System: "
2085 (symbol-name last-coding-system-used)
2086 "\n"))
2087 (narrow-to-region (point) (point-max))
2088 (and (= 0 (% count 10))
2089 (message "Converting to Babyl format...%d" count)))
2090 ;;
2091 ;; This kludge is because some versions of sendmail.el
2092 ;; insert an extra newline at the beginning that shouldn't
2093 ;; be there. sendmail.el has been fixed, but old versions
2094 ;; may still be in use. -- rms, 7 May 1993.
2095 ((eolp) (delete-char 1))
2096 (t (error "Cannot convert to babyl format")))))
2097 count))
2098
2099 ;; Delete the "From ..." line, creating various other headers with
2100 ;; information from it if they don't already exist. Now puts the
2101 ;; original line into a mail-from: header line for debugging and for
2102 ;; use by the rmail-output function.
2103 (defun rmail-nuke-pinhead-header ()
2104 (save-excursion
2105 (save-restriction
2106 (let ((start (point))
2107 (end (progn
2108 (condition-case ()
2109 (search-forward "\n\n")
2110 (error
2111 (goto-char (point-max))
2112 (insert "\n\n")))
2113 (point)))
2114 has-from has-date)
2115 (narrow-to-region start end)
2116 (let ((case-fold-search t))
2117 (goto-char start)
2118 (setq has-from (search-forward "\nFrom:" nil t))
2119 (goto-char start)
2120 (setq has-date (and (search-forward "\nDate:" nil t) (point)))
2121 (goto-char start))
2122 (let ((case-fold-search nil))
2123 (if (re-search-forward (concat "^" rmail-unix-mail-delimiter) nil t)
2124 (replace-match
2125 (concat
2126 "Mail-from: \\&"
2127 ;; Keep and reformat the date if we don't
2128 ;; have a Date: field.
2129 (if has-date
2130 ""
2131 (concat
2132 "Date: \\2, \\4 \\3 \\9 \\5 "
2133
2134 ;; The timezone could be matched by group 7 or group 10.
2135 ;; If neither of them matched, assume EST, since only
2136 ;; Easterners would be so sloppy.
2137 ;; It's a shame the substitution can't use "\\10".
2138 (cond
2139 ((/= (match-beginning 7) (match-end 7)) "\\7")
2140 ((/= (match-beginning 10) (match-end 10))
2141 (buffer-substring (match-beginning 10)
2142 (match-end 10)))
2143 (t "EST"))
2144 "\n"))
2145 ;; Keep and reformat the sender if we don't
2146 ;; have a From: field.
2147 (if has-from
2148 ""
2149 "From: \\1\n"))
2150 t)))))))
2151 \f
2152 ;;;; *** Rmail Message Formatting and Header Manipulation ***
2153
2154 (defun rmail-reformat-message (beg end)
2155 (goto-char beg)
2156 (forward-line 1)
2157 (if (/= (following-char) ?0)
2158 (error "Bad format in RMAIL file"))
2159 (let ((inhibit-read-only t)
2160 (delta (- (buffer-size) end)))
2161 (delete-char 1)
2162 (insert ?1)
2163 (forward-line 1)
2164 (let ((case-fold-search t))
2165 (while (looking-at "Summary-line:\\|Mail-From:")
2166 (forward-line 1)))
2167 (if (looking-at "\\*\\*\\* EOOH \\*\\*\\*\n")
2168 (delete-region (point)
2169 (progn (forward-line 1) (point))))
2170 (let ((str (buffer-substring (point)
2171 (save-excursion (search-forward "\n\n" end 'move)
2172 (point)))))
2173 (insert str "*** EOOH ***\n")
2174 (narrow-to-region (point) (- (buffer-size) delta)))
2175 (goto-char (point-min))
2176 (if rmail-message-filter (funcall rmail-message-filter))
2177 (if (or rmail-displayed-headers rmail-ignored-headers)
2178 (rmail-clear-headers))))
2179
2180 (defun rmail-clear-headers (&optional ignored-headers)
2181 "Delete all header fields that Rmail should not show.
2182 If the optional argument IGNORED-HEADERS is non-nil,
2183 delete all header fields whose names match that regexp.
2184 Otherwise, if `rmail-displayed-headers' is non-nil,
2185 delete all header fields *except* those whose names match that regexp.
2186 Otherwise, delete all header fields whose names match `rmail-ignored-headers'."
2187 (when (search-forward "\n\n" nil t)
2188 (forward-char -1)
2189 (let ((case-fold-search t)
2190 (buffer-read-only nil))
2191 (if (and rmail-displayed-headers (null ignored-headers))
2192 (save-restriction
2193 (narrow-to-region (point-min) (point))
2194 (let (lim next)
2195 (goto-char (point-min))
2196 (while (and (not (eobp))
2197 (save-excursion
2198 (if (re-search-forward "\n[^ \t]" nil t)
2199 (setq lim (match-beginning 0)
2200 next (1+ lim))
2201 (setq lim nil next (point-max)))))
2202 (if (save-excursion
2203 (re-search-forward rmail-displayed-headers lim t))
2204 (goto-char next)
2205 (delete-region (point) next))))
2206 (goto-char (point-min)))
2207 (or ignored-headers (setq ignored-headers rmail-ignored-headers))
2208 (save-restriction
2209 (narrow-to-region (point-min) (point))
2210 (while (and ignored-headers
2211 (progn
2212 (goto-char (point-min))
2213 (re-search-forward ignored-headers nil t)))
2214 (beginning-of-line)
2215 (delete-region (point)
2216 (if (re-search-forward "\n[^ \t]" nil t)
2217 (1- (point))
2218 (point-max)))))))))
2219
2220 (defun rmail-msg-is-pruned ()
2221 (rmail-maybe-set-message-counters)
2222 (save-restriction
2223 (narrow-to-region (rmail-msgbeg rmail-current-message) (point-max))
2224 (save-excursion
2225 (goto-char (point-min))
2226 (forward-line 1)
2227 (= (following-char) ?1))))
2228
2229 (defun rmail-msg-restore-non-pruned-header ()
2230 (let ((old-point (point))
2231 new-point
2232 new-start
2233 (inhibit-read-only t))
2234 (save-excursion
2235 (narrow-to-region (rmail-msgbeg rmail-current-message) (point-max))
2236 (goto-char (point-min))
2237 (forward-line 1)
2238 ;; Change 1 to 0.
2239 (delete-char 1)
2240 (insert ?0)
2241 ;; Insert new EOOH line at the proper place.
2242 (forward-line 1)
2243 (let ((case-fold-search t))
2244 (while (looking-at "Summary-Line:\\|Mail-From:")
2245 (forward-line 1)))
2246 (insert "*** EOOH ***\n")
2247 (setq new-start (point))
2248 ;; Delete the old reformatted header.
2249 (forward-char -1)
2250 (search-forward "\n*** EOOH ***\n")
2251 (forward-line -1)
2252 (let ((start (point)))
2253 (search-forward "\n\n")
2254 (if (and (<= start old-point)
2255 (<= old-point (point)))
2256 (setq new-point new-start))
2257 (delete-region start (point)))
2258 ;; Narrow to after the new EOOH line.
2259 (narrow-to-region new-start (point-max)))
2260 (if new-point
2261 (goto-char new-point))))
2262
2263 (defun rmail-msg-prune-header ()
2264 (let ((new-point
2265 (= (point) (point-min))))
2266 (save-excursion
2267 (narrow-to-region (rmail-msgbeg rmail-current-message) (point-max))
2268 (rmail-reformat-message (point-min) (point-max)))
2269 (if new-point
2270 (goto-char (point-min)))))
2271
2272 (defun rmail-toggle-header (&optional arg)
2273 "Show original message header if pruned header currently shown, or vice versa.
2274 With argument ARG, show the message header pruned if ARG is greater than zero;
2275 otherwise, show it in full."
2276 (interactive "P")
2277 (let* ((pruned (with-current-buffer rmail-buffer
2278 (rmail-msg-is-pruned)))
2279 (prune (if arg
2280 (> (prefix-numeric-value arg) 0)
2281 (not pruned))))
2282 (if (eq pruned prune)
2283 t
2284 (set-buffer rmail-buffer)
2285 (rmail-maybe-set-message-counters)
2286 (if rmail-enable-mime
2287 (let ((buffer-read-only nil))
2288 (if pruned
2289 (rmail-msg-restore-non-pruned-header)
2290 (rmail-msg-prune-header))
2291 (funcall rmail-show-mime-function))
2292 (let* ((buffer-read-only nil)
2293 (window (get-buffer-window (current-buffer)))
2294 (at-point-min (= (point) (point-min)))
2295 (all-headers-visible (= (window-start window) (point-min)))
2296 (on-header
2297 (save-excursion
2298 (and (not (search-backward "\n\n" nil t))
2299 (progn
2300 (end-of-line)
2301 (re-search-backward "^[-A-Za-z0-9]+:" nil t))
2302 (match-string 0))))
2303 (old-screen-line
2304 (rmail-count-screen-lines (window-start window) (point))))
2305 (if pruned
2306 (rmail-msg-restore-non-pruned-header)
2307 (rmail-msg-prune-header))
2308 (cond (at-point-min
2309 (goto-char (point-min)))
2310 (on-header
2311 (goto-char (point-min))
2312 (search-forward "\n\n")
2313 (or (re-search-backward
2314 (concat "^" (regexp-quote on-header)) nil t)
2315 (goto-char (point-min))))
2316 (t
2317 (save-selected-window
2318 (select-window window)
2319 (recenter old-screen-line)
2320 (if (and all-headers-visible
2321 (not (= (window-start) (point-min))))
2322 (recenter (- (window-height) 2))))))))
2323 (rmail-highlight-headers))))
2324
2325 (defun rmail-narrow-to-non-pruned-header ()
2326 "Narrow to the whole (original) header of the current message."
2327 (let (start end)
2328 (narrow-to-region (rmail-msgbeg rmail-current-message) (point-max))
2329 (goto-char (point-min))
2330 (forward-line 1)
2331 (if (= (following-char) ?1)
2332 (progn
2333 (forward-line 1)
2334 (setq start (point))
2335 (search-forward "*** EOOH ***\n")
2336 (setq end (match-beginning 0)))
2337 (forward-line 2)
2338 (setq start (point))
2339 (search-forward "\n\n")
2340 (setq end (1- (point))))
2341 (narrow-to-region start end)
2342 (goto-char start)))
2343
2344 ;; Lifted from repos-count-screen-lines.
2345 ;; Return number of screen lines between START and END.
2346 (defun rmail-count-screen-lines (start end)
2347 (save-excursion
2348 (save-restriction
2349 (narrow-to-region start end)
2350 (goto-char (point-min))
2351 (vertical-motion (- (point-max) (point-min))))))
2352 \f
2353 ;;;; *** Rmail Attributes and Keywords ***
2354
2355 ;; Make a string describing current message's attributes and keywords
2356 ;; and set it up as the name of a minor mode
2357 ;; so it will appear in the mode line.
2358 (defun rmail-display-labels ()
2359 (let ((blurb "") (beg (point-min-marker)) (end (point-max-marker)))
2360 (save-excursion
2361 (unwind-protect
2362 (progn
2363 (widen)
2364 (goto-char (rmail-msgbeg rmail-current-message))
2365 (forward-line 1)
2366 (if (looking-at "[01],")
2367 (progn
2368 (narrow-to-region (point) (progn (end-of-line) (point)))
2369 ;; Truly valid BABYL format requires a space before each
2370 ;; attribute or keyword name. Put them in if missing.
2371 (let (buffer-read-only)
2372 (goto-char (point-min))
2373 (while (search-forward "," nil t)
2374 (or (looking-at "[ ,]") (eobp)
2375 (insert " "))))
2376 (goto-char (point-max))
2377 (if (search-backward ",," nil 'move)
2378 (progn
2379 (if (> (point) (1+ (point-min)))
2380 (setq blurb (buffer-substring (+ 1 (point-min)) (point))))
2381 (if (> (- (point-max) (point)) 2)
2382 (setq blurb
2383 (concat blurb
2384 ";"
2385 (buffer-substring (+ (point) 3)
2386 (1- (point-max)))))))))))
2387 ;; Note: we don't use save-restriction because that does not work right
2388 ;; if changes are made outside the saved restriction
2389 ;; before that restriction is restored.
2390 (narrow-to-region beg end)
2391 (set-marker beg nil)
2392 (set-marker end nil)))
2393 (while (string-match " +," blurb)
2394 (setq blurb (concat (substring blurb 0 (match-beginning 0)) ","
2395 (substring blurb (match-end 0)))))
2396 (while (string-match ", +" blurb)
2397 (setq blurb (concat (substring blurb 0 (match-beginning 0)) ","
2398 (substring blurb (match-end 0)))))
2399 (setq mode-line-process
2400 (format " %d/%d%s"
2401 rmail-current-message rmail-total-messages blurb))
2402 ;; If rmail-enable-mime is non-nil, we may have to update
2403 ;; `mode-line-process' of rmail-view-buffer too.
2404 (if (and rmail-enable-mime
2405 (not (eq (current-buffer) rmail-view-buffer))
2406 (buffer-live-p rmail-view-buffer))
2407 (let ((mlp mode-line-process))
2408 (with-current-buffer rmail-view-buffer
2409 (setq mode-line-process mlp))))))
2410
2411 ;; Turn an attribute of a message on or off according to STATE.
2412 ;; ATTR is the name of the attribute, as a string.
2413 ;; MSGNUM is message number to change; nil means current message.
2414 (defun rmail-set-attribute (attr state &optional msgnum)
2415 (set-buffer rmail-buffer)
2416 (let ((omax (point-max-marker))
2417 (omin (point-min-marker))
2418 (buffer-read-only nil))
2419 (or msgnum (setq msgnum rmail-current-message))
2420 (if (> msgnum 0)
2421 (unwind-protect
2422 (save-excursion
2423 (widen)
2424 (goto-char (+ 3 (rmail-msgbeg msgnum)))
2425 (let ((curstate
2426 (not
2427 (null (search-backward (concat ", " attr ",")
2428 (prog1 (point) (end-of-line)) t)))))
2429 (or (eq curstate (not (not state)))
2430 (if curstate
2431 (delete-region (point) (1- (match-end 0)))
2432 (beginning-of-line)
2433 (forward-char 2)
2434 (insert " " attr ","))))
2435 (if (string= attr "deleted")
2436 (rmail-set-message-deleted-p msgnum state)))
2437 ;; Note: we don't use save-restriction because that does not work right
2438 ;; if changes are made outside the saved restriction
2439 ;; before that restriction is restored.
2440 (narrow-to-region omin omax)
2441 (set-marker omin nil)
2442 (set-marker omax nil)
2443 (if (= msgnum rmail-current-message)
2444 (rmail-display-labels))))))
2445
2446 ;; Return t if the attributes/keywords line of msg number MSG
2447 ;; contains a match for the regexp LABELS.
2448 (defun rmail-message-labels-p (msg labels)
2449 (save-excursion
2450 (save-restriction
2451 (widen)
2452 (goto-char (rmail-msgbeg msg))
2453 (forward-char 3)
2454 (re-search-backward labels (prog1 (point) (end-of-line)) t))))
2455 \f
2456 ;;;; *** Rmail Message Selection And Support ***
2457
2458 (defun rmail-msgend (n)
2459 (marker-position (aref rmail-message-vector (1+ n))))
2460
2461 (defun rmail-msgbeg (n)
2462 (marker-position (aref rmail-message-vector n)))
2463
2464 (defun rmail-widen-to-current-msgbeg (function)
2465 "Call FUNCTION with point at start of internal data of current message.
2466 Assumes that bounds were previously narrowed to display the message in Rmail.
2467 The bounds are widened enough to move point where desired, then narrowed
2468 again afterward.
2469
2470 FUNCTION may not change the visible text of the message, but it may
2471 change the invisible header text."
2472 (save-excursion
2473 (unwind-protect
2474 (progn
2475 (narrow-to-region (rmail-msgbeg rmail-current-message)
2476 (point-max))
2477 (goto-char (point-min))
2478 (funcall function))
2479 ;; Note: we don't use save-restriction because that does not work right
2480 ;; if changes are made outside the saved restriction
2481 ;; before that restriction is restored.
2482 (narrow-to-region (rmail-msgbeg rmail-current-message)
2483 (rmail-msgend rmail-current-message)))))
2484
2485 (defun rmail-forget-messages ()
2486 (unwind-protect
2487 (if (vectorp rmail-message-vector)
2488 (let* ((i 0)
2489 (v rmail-message-vector)
2490 (n (length v)))
2491 (while (< i n)
2492 (move-marker (aref v i) nil)
2493 (setq i (1+ i)))))
2494 (setq rmail-message-vector nil)
2495 (setq rmail-msgref-vector nil)
2496 (setq rmail-deleted-vector nil)))
2497
2498 (defun rmail-maybe-set-message-counters ()
2499 (if (not (and rmail-deleted-vector
2500 rmail-message-vector
2501 rmail-current-message
2502 rmail-total-messages))
2503 (rmail-set-message-counters)))
2504
2505 (defun rmail-count-new-messages (&optional nomsg)
2506 (let* ((case-fold-search nil)
2507 (total-messages 0)
2508 (messages-head nil)
2509 (deleted-head nil))
2510 (or nomsg (message "Counting new messages..."))
2511 (goto-char (point-max))
2512 ;; Put at the end of messages-head
2513 ;; the entry for message N+1, which marks
2514 ;; the end of message N. (N = number of messages).
2515 (search-backward "\n\^_")
2516 (forward-char 1)
2517 (setq messages-head (list (point-marker)))
2518 (rmail-set-message-counters-counter (point-min))
2519 (setq rmail-current-message (1+ rmail-total-messages))
2520 (setq rmail-total-messages
2521 (+ rmail-total-messages total-messages))
2522 (setq rmail-message-vector
2523 (vconcat rmail-message-vector (cdr messages-head)))
2524 (aset rmail-message-vector
2525 rmail-current-message (car messages-head))
2526 (setq rmail-deleted-vector
2527 (concat rmail-deleted-vector deleted-head))
2528 (setq rmail-summary-vector
2529 (vconcat rmail-summary-vector (make-vector total-messages nil)))
2530 (setq rmail-msgref-vector
2531 (vconcat rmail-msgref-vector (make-vector total-messages nil)))
2532 ;; Fill in the new elements of rmail-msgref-vector.
2533 (let ((i (1+ (- rmail-total-messages total-messages))))
2534 (while (<= i rmail-total-messages)
2535 (aset rmail-msgref-vector i (list i))
2536 (setq i (1+ i))))
2537 (goto-char (point-min))
2538 (or nomsg (message "Counting new messages...done (%d)" total-messages))))
2539
2540 (defun rmail-set-message-counters ()
2541 (rmail-forget-messages)
2542 (save-excursion
2543 (save-restriction
2544 (widen)
2545 (let* ((point-save (point))
2546 (total-messages 0)
2547 (messages-after-point)
2548 (case-fold-search nil)
2549 (messages-head nil)
2550 (deleted-head nil))
2551 (message "Counting messages...")
2552 (goto-char (point-max))
2553 ;; Put at the end of messages-head
2554 ;; the entry for message N+1, which marks
2555 ;; the end of message N. (N = number of messages).
2556 (search-backward "\n\^_" nil t)
2557 (if (/= (point) (point-max)) (forward-char 1))
2558 (setq messages-head (list (point-marker)))
2559 (rmail-set-message-counters-counter (min (point) point-save))
2560 (setq messages-after-point total-messages)
2561 (rmail-set-message-counters-counter)
2562 (setq rmail-total-messages total-messages)
2563 (setq rmail-current-message
2564 (min total-messages
2565 (max 1 (- total-messages messages-after-point))))
2566 (setq rmail-message-vector
2567 (apply 'vector (cons (point-min-marker) messages-head))
2568 rmail-deleted-vector (concat "0" deleted-head)
2569 rmail-summary-vector (make-vector rmail-total-messages nil)
2570 rmail-msgref-vector (make-vector (1+ rmail-total-messages) nil))
2571 (let ((i 0))
2572 (while (<= i rmail-total-messages)
2573 (aset rmail-msgref-vector i (list i))
2574 (setq i (1+ i))))
2575 (message "Counting messages...done")))))
2576
2577 (defun rmail-set-message-counters-counter (&optional stop)
2578 (let ((start (point))
2579 next)
2580 (while (search-backward "\n\^_\^L" stop t)
2581 ;; Detect messages that have been added with DOS line endings and
2582 ;; convert the line endings for such messages.
2583 (setq next (point))
2584 (if (looking-at "\n\^_\^L\r\n")
2585 (let ((buffer-read-only nil)
2586 (buffer-undo t))
2587 (message "Counting messages...(converting line endings)")
2588 (save-excursion
2589 (goto-char start)
2590 (while (search-backward "\r\n" next t)
2591 (delete-char 1)))))
2592 (setq start next)
2593 (forward-char 1)
2594 (setq messages-head (cons (point-marker) messages-head))
2595 (save-excursion
2596 (setq deleted-head
2597 (cons (if (search-backward ", deleted,"
2598 (prog1 (point)
2599 (forward-line 2))
2600 t)
2601 ?D ?\ )
2602 deleted-head)))
2603 (if (zerop (% (setq total-messages (1+ total-messages)) 20))
2604 (message "Counting messages...%d" total-messages)))))
2605
2606 (defun rmail-beginning-of-message ()
2607 "Show current message starting from the beginning."
2608 (interactive)
2609 (let ((rmail-show-message-hook
2610 (list (function (lambda ()
2611 (goto-char (point-min)))))))
2612 (rmail-show-message rmail-current-message)))
2613
2614 (defun rmail-end-of-message ()
2615 "Show bottom of current message."
2616 (interactive)
2617 (let ((rmail-show-message-hook
2618 (list (function (lambda ()
2619 (goto-char (point-max))
2620 (recenter (1- (window-height))))))))
2621 (rmail-show-message rmail-current-message)))
2622
2623 (defun rmail-unknown-mail-followup-to ()
2624 "Handle a \"Mail-Followup-To\" header field with an unknown mailing list.
2625 Ask the user whether to add that list name to `mail-mailing-lists'."
2626 (save-restriction
2627 (rmail-narrow-to-non-pruned-header)
2628 (let ((mail-followup-to (mail-fetch-field "mail-followup-to" nil t)))
2629 (when mail-followup-to
2630 (let ((addresses
2631 (split-string
2632 (mail-strip-quoted-names mail-followup-to)
2633 ",[[:space:]]+" t)))
2634 (dolist (addr addresses)
2635 (when (and (not (member addr mail-mailing-lists))
2636 (not
2637 ;; taken from rmailsum.el
2638 (string-match
2639 (or rmail-user-mail-address-regexp
2640 (concat "^\\("
2641 (regexp-quote (user-login-name))
2642 "\\($\\|@\\)\\|"
2643 (regexp-quote
2644 (or user-mail-address
2645 (concat (user-login-name) "@"
2646 (or mail-host-address
2647 (system-name)))))
2648 "\\>\\)"))
2649 addr))
2650 (y-or-n-p
2651 (format "Add `%s' to `mail-mailing-lists'? "
2652 addr)))
2653 (customize-save-variable 'mail-mailing-lists
2654 (cons addr mail-mailing-lists)))))))))
2655
2656 (defun rmail-show-message (&optional n no-summary)
2657 "Show message number N (prefix argument), counting from start of file.
2658 If summary buffer is currently displayed, update current message there also."
2659 (interactive "p")
2660 (or (eq major-mode 'rmail-mode)
2661 (switch-to-buffer rmail-buffer))
2662 (rmail-maybe-set-message-counters)
2663 (widen)
2664 (if (zerop rmail-total-messages)
2665 (progn (narrow-to-region (point-min) (1- (point-max)))
2666 (goto-char (point-min))
2667 (setq mode-line-process nil))
2668 (let (blurb coding-system)
2669 (if (not n)
2670 (setq n rmail-current-message)
2671 (cond ((<= n 0)
2672 (setq n 1
2673 rmail-current-message 1
2674 blurb "No previous message"))
2675 ((> n rmail-total-messages)
2676 (setq n rmail-total-messages
2677 rmail-current-message rmail-total-messages
2678 blurb "No following message"))
2679 (t
2680 (setq rmail-current-message n))))
2681 (let ((beg (rmail-msgbeg n)))
2682 (goto-char beg)
2683 (forward-line 1)
2684 (save-excursion
2685 (let ((end (rmail-msgend n)))
2686 (save-restriction
2687 (if (prog1 (= (following-char) ?0)
2688 (forward-line 2)
2689 ;; If there's a Summary-line in the (otherwise empty)
2690 ;; header, we didn't yet get past the EOOH line.
2691 (if (looking-at "^\\*\\*\\* EOOH \\*\\*\\*\n")
2692 (forward-line 1))
2693 (narrow-to-region (point) end))
2694 (rfc822-goto-eoh)
2695 (search-forward "\n*** EOOH ***\n" end t))
2696 (narrow-to-region beg (point))
2697 (goto-char (point-min))
2698 (if (re-search-forward "^X-Coding-System: *\\(.*\\)$" nil t)
2699 (let ((coding-system (intern (match-string 1))))
2700 (condition-case nil
2701 (progn
2702 (check-coding-system coding-system)
2703 (setq buffer-file-coding-system coding-system))
2704 (error
2705 (setq buffer-file-coding-system nil))))
2706 (setq buffer-file-coding-system nil)))))
2707 ;; Clear the "unseen" attribute when we show a message.
2708 (rmail-set-attribute "unseen" nil)
2709 (let ((end (rmail-msgend n)))
2710 ;; Reformat the header, or else find the reformatted header.
2711 (if (= (following-char) ?0)
2712 (rmail-reformat-message beg end)
2713 (search-forward "\n*** EOOH ***\n" end t)
2714 (narrow-to-region (point) end)))
2715 (goto-char (point-min))
2716 (walk-windows
2717 (function (lambda (window)
2718 (if (eq (window-buffer window) (current-buffer))
2719 (set-window-point window (point)))))
2720 nil t)
2721 (rmail-display-labels)
2722 (if (eq rmail-enable-mime t)
2723 (funcall rmail-show-mime-function)
2724 (setq rmail-view-buffer rmail-buffer))
2725 (when mail-mailing-lists
2726 (rmail-unknown-mail-followup-to))
2727 (rmail-highlight-headers)
2728 (if transient-mark-mode (deactivate-mark))
2729 (run-hooks 'rmail-show-message-hook)
2730 ;; If there is a summary buffer, try to move to this message
2731 ;; in that buffer. But don't complain if this message
2732 ;; is not mentioned in the summary.
2733 ;; Don't do this at all if we were called on behalf
2734 ;; of cursor motion in the summary buffer.
2735 (and (rmail-summary-exists) (not no-summary)
2736 (let ((curr-msg rmail-current-message))
2737 (rmail-select-summary
2738 (rmail-summary-goto-msg curr-msg t t))))
2739 (with-current-buffer rmail-buffer
2740 (rmail-auto-file))
2741 (if blurb
2742 (message blurb))))))
2743
2744 (defun rmail-redecode-body (coding)
2745 "Decode the body of the current message using coding system CODING.
2746 This is useful with mail messages that have malformed or missing
2747 charset= headers.
2748
2749 This function assumes that the current message is already decoded
2750 and displayed in the RMAIL buffer, but the coding system used to
2751 decode it was incorrect. It then encodes the message back to its
2752 original form, and decodes it again, using the coding system CODING.
2753
2754 Note that if Emacs erroneously auto-detected one of the iso-2022
2755 encodings in the message, this function might fail because the escape
2756 sequences that switch between character sets and also single-shift and
2757 locking-shift codes are impossible to recover. This function is meant
2758 to be used to fix messages encoded with 8-bit encodings, such as
2759 iso-8859, koi8-r, etc."
2760 (interactive "zCoding system for re-decoding this message: ")
2761 (when (not rmail-enable-mime)
2762 (or (eq major-mode 'rmail-mode)
2763 (switch-to-buffer rmail-buffer))
2764 (save-excursion
2765 (let ((pruned (rmail-msg-is-pruned)))
2766 (unwind-protect
2767 (let ((msgbeg (rmail-msgbeg rmail-current-message))
2768 (msgend (rmail-msgend rmail-current-message))
2769 x-coding-header)
2770 ;; We need the message headers pruned (we later restore
2771 ;; the pruned stat to what it was, see the end of
2772 ;; unwind-protect form).
2773 (or pruned
2774 (rmail-toggle-header 1))
2775 (narrow-to-region msgbeg msgend)
2776 (goto-char (point-min))
2777 (when (search-forward "\n*** EOOH ***\n" (point-max) t)
2778 (narrow-to-region msgbeg (point)))
2779 (goto-char (point-min))
2780 (if (re-search-forward "^X-Coding-System: *\\(.*\\)$" nil t)
2781 (let ((old-coding (intern (match-string 1)))
2782 (buffer-read-only nil))
2783 (check-coding-system old-coding)
2784 ;; Make sure the new coding system uses the same EOL
2785 ;; conversion, to prevent ^M characters from popping
2786 ;; up all over the place.
2787 (setq coding
2788 (coding-system-change-eol-conversion
2789 coding
2790 (coding-system-eol-type old-coding)))
2791 (setq x-coding-header (point-marker))
2792 (narrow-to-region msgbeg msgend)
2793 (encode-coding-region (point) msgend old-coding)
2794 (decode-coding-region (point) msgend coding)
2795 (setq last-coding-system-used coding)
2796 ;; Rewrite the coding-system header according
2797 ;; to what we did.
2798 (goto-char x-coding-header)
2799 (delete-region (point)
2800 (save-excursion
2801 (beginning-of-line)
2802 (point)))
2803 (insert "X-Coding-System: "
2804 (symbol-name last-coding-system-used))
2805 (set-marker x-coding-header nil)
2806 (rmail-show-message))
2807 (error "No X-Coding-System header found")))
2808 (or pruned
2809 (rmail-toggle-header 0)))))))
2810
2811 ;; Find all occurrences of certain fields, and highlight them.
2812 (defun rmail-highlight-headers ()
2813 ;; Do this only if the system supports faces.
2814 (if (and (fboundp 'internal-find-face)
2815 rmail-highlighted-headers)
2816 (save-excursion
2817 (search-forward "\n\n" nil 'move)
2818 (save-restriction
2819 (narrow-to-region (point-min) (point))
2820 (let ((case-fold-search t)
2821 (inhibit-read-only t)
2822 ;; Highlight with boldface if that is available.
2823 ;; Otherwise use the `highlight' face.
2824 (face (or rmail-highlight-face
2825 (if (face-differs-from-default-p 'bold)
2826 'bold 'highlight)))
2827 ;; List of overlays to reuse.
2828 (overlays rmail-overlay-list))
2829 (goto-char (point-min))
2830 (while (re-search-forward rmail-highlighted-headers nil t)
2831 (skip-chars-forward " \t")
2832 (let ((beg (point))
2833 overlay)
2834 (while (progn (forward-line 1)
2835 (looking-at "[ \t]")))
2836 ;; Back up over newline, then trailing spaces or tabs
2837 (forward-char -1)
2838 (while (member (preceding-char) '(? ?\t))
2839 (forward-char -1))
2840 (if overlays
2841 ;; Reuse an overlay we already have.
2842 (progn
2843 (setq overlay (car overlays)
2844 overlays (cdr overlays))
2845 (overlay-put overlay 'face face)
2846 (move-overlay overlay beg (point)))
2847 ;; Make a new overlay and add it to
2848 ;; rmail-overlay-list.
2849 (setq overlay (make-overlay beg (point)))
2850 (overlay-put overlay 'face face)
2851 (setq rmail-overlay-list
2852 (cons overlay rmail-overlay-list))))))))))
2853
2854 (defun rmail-auto-file ()
2855 "Automatically move a message into a sub-folder based on criteria.
2856 Called when a new message is displayed."
2857 (if (or (rmail-message-labels-p rmail-current-message "filed")
2858 (not (string= (buffer-file-name)
2859 (expand-file-name rmail-file-name))))
2860 ;; Do nothing if it's already been filed.
2861 nil
2862 ;; Find out some basics (common fields)
2863 (let ((from (mail-fetch-field "from"))
2864 (subj (mail-fetch-field "subject"))
2865 (to (concat (mail-fetch-field "to") "," (mail-fetch-field "cc")))
2866 (d rmail-automatic-folder-directives)
2867 (directive-loop nil)
2868 (folder nil))
2869 (while d
2870 (setq folder (car (car d))
2871 directive-loop (cdr (car d)))
2872 (while (and (car directive-loop)
2873 (let ((f (cond
2874 ((string= (car directive-loop) "from") from)
2875 ((string= (car directive-loop) "to") to)
2876 ((string= (car directive-loop) "subject") subj)
2877 (t (mail-fetch-field (car directive-loop))))))
2878 (and f (string-match (car (cdr directive-loop)) f))))
2879 (setq directive-loop (cdr (cdr directive-loop))))
2880 ;; If there are no directives left, then it was a complete match.
2881 (if (null directive-loop)
2882 (if (null folder)
2883 (rmail-delete-forward)
2884 (if (string= "/dev/null" folder)
2885 (rmail-delete-message)
2886 (rmail-output-to-rmail-file folder 1 t)
2887 (setq d nil))))
2888 (setq d (cdr d))))))
2889
2890 (defun rmail-next-message (n)
2891 "Show following message whether deleted or not.
2892 With prefix arg N, moves forward N messages, or backward if N is negative."
2893 (interactive "p")
2894 (set-buffer rmail-buffer)
2895 (rmail-maybe-set-message-counters)
2896 (rmail-show-message (+ rmail-current-message n)))
2897
2898 (defun rmail-previous-message (n)
2899 "Show previous message whether deleted or not.
2900 With prefix arg N, moves backward N messages, or forward if N is negative."
2901 (interactive "p")
2902 (rmail-next-message (- n)))
2903
2904 (defun rmail-next-undeleted-message (n)
2905 "Show following non-deleted message.
2906 With prefix arg N, moves forward N non-deleted messages,
2907 or backward if N is negative.
2908
2909 Returns t if a new message is being shown, nil otherwise."
2910 (interactive "p")
2911 (set-buffer rmail-buffer)
2912 (rmail-maybe-set-message-counters)
2913 (let ((lastwin rmail-current-message)
2914 (current rmail-current-message))
2915 (while (and (> n 0) (< current rmail-total-messages))
2916 (setq current (1+ current))
2917 (if (not (rmail-message-deleted-p current))
2918 (setq lastwin current n (1- n))))
2919 (while (and (< n 0) (> current 1))
2920 (setq current (1- current))
2921 (if (not (rmail-message-deleted-p current))
2922 (setq lastwin current n (1+ n))))
2923 (if (/= lastwin rmail-current-message)
2924 (progn (rmail-show-message lastwin)
2925 t)
2926 (if (< n 0)
2927 (message "No previous nondeleted message"))
2928 (if (> n 0)
2929 (message "No following nondeleted message"))
2930 nil)))
2931
2932 (defun rmail-previous-undeleted-message (n)
2933 "Show previous non-deleted message.
2934 With prefix argument N, moves backward N non-deleted messages,
2935 or forward if N is negative."
2936 (interactive "p")
2937 (rmail-next-undeleted-message (- n)))
2938
2939 (defun rmail-first-message ()
2940 "Show first message in file."
2941 (interactive)
2942 (rmail-maybe-set-message-counters)
2943 (rmail-show-message 1))
2944
2945 (defun rmail-last-message ()
2946 "Show last message in file."
2947 (interactive)
2948 (rmail-maybe-set-message-counters)
2949 (rmail-show-message rmail-total-messages))
2950
2951 (defun rmail-what-message ()
2952 (let ((where (point))
2953 (low 1)
2954 (high rmail-total-messages)
2955 (mid (/ rmail-total-messages 2)))
2956 (while (> (- high low) 1)
2957 (if (>= where (rmail-msgbeg mid))
2958 (setq low mid)
2959 (setq high mid))
2960 (setq mid (+ low (/ (- high low) 2))))
2961 (if (>= where (rmail-msgbeg high)) high low)))
2962
2963 (defun rmail-message-recipients-p (msg recipients &optional primary-only)
2964 (save-restriction
2965 (goto-char (rmail-msgbeg msg))
2966 (search-forward "\n*** EOOH ***\n")
2967 (narrow-to-region (point) (progn (search-forward "\n\n") (point)))
2968 (or (string-match recipients (or (mail-fetch-field "To") ""))
2969 (string-match recipients (or (mail-fetch-field "From") ""))
2970 (if (not primary-only)
2971 (string-match recipients (or (mail-fetch-field "Cc") ""))))))
2972
2973 (defun rmail-message-regexp-p (n regexp)
2974 "Return t, if for message number N, regexp REGEXP matches in the header."
2975 (let ((beg (rmail-msgbeg n))
2976 (end (rmail-msgend n)))
2977 (goto-char beg)
2978 (forward-line 1)
2979 (save-excursion
2980 (save-restriction
2981 (if (prog1 (= (following-char) ?0)
2982 (forward-line 2)
2983 ;; If there's a Summary-line in the (otherwise empty)
2984 ;; header, we didn't yet get past the EOOH line.
2985 (when (looking-at "^\\*\\*\\* EOOH \\*\\*\\*\n")
2986 (forward-line 1))
2987 (setq beg (point))
2988 (narrow-to-region (point) end))
2989 (progn
2990 (rfc822-goto-eoh)
2991 (setq end (point)))
2992 (setq beg (point))
2993 (search-forward "\n*** EOOH ***\n" end t)
2994 (setq end (1+ (match-beginning 0)))))
2995 (goto-char beg)
2996 (if rmail-enable-mime
2997 (funcall rmail-search-mime-header-function n regexp end)
2998 (re-search-forward regexp end t)))))
2999
3000 (defun rmail-search-message (msg regexp)
3001 "Return non-nil, if for message number MSG, regexp REGEXP matches."
3002 (goto-char (rmail-msgbeg msg))
3003 (if rmail-enable-mime
3004 (funcall rmail-search-mime-message-function msg regexp)
3005 (re-search-forward regexp (rmail-msgend msg) t)))
3006
3007 (defvar rmail-search-last-regexp nil)
3008 (defun rmail-search (regexp &optional n)
3009 "Show message containing next match for REGEXP (but not the current msg).
3010 Prefix argument gives repeat count; negative argument means search
3011 backwards (through earlier messages).
3012 Interactively, empty argument means use same regexp used last time."
3013 (interactive
3014 (let* ((reversep (< (prefix-numeric-value current-prefix-arg) 0))
3015 (prompt
3016 (concat (if reversep "Reverse " "") "Rmail search (regexp): "))
3017 regexp)
3018 (if rmail-search-last-regexp
3019 (setq prompt (concat prompt
3020 "(default "
3021 rmail-search-last-regexp
3022 ") ")))
3023 (setq regexp (read-string prompt))
3024 (cond ((not (equal regexp ""))
3025 (setq rmail-search-last-regexp regexp))
3026 ((not rmail-search-last-regexp)
3027 (error "No previous Rmail search string")))
3028 (list rmail-search-last-regexp
3029 (prefix-numeric-value current-prefix-arg))))
3030 (or n (setq n 1))
3031 (message "%sRmail search for %s..."
3032 (if (< n 0) "Reverse " "")
3033 regexp)
3034 (set-buffer rmail-buffer)
3035 (rmail-maybe-set-message-counters)
3036 (let ((omin (point-min))
3037 (omax (point-max))
3038 (opoint (point))
3039 win
3040 (reversep (< n 0))
3041 (msg rmail-current-message))
3042 (unwind-protect
3043 (progn
3044 (widen)
3045 (while (/= n 0)
3046 ;; Check messages one by one, advancing message number up or down
3047 ;; but searching forward through each message.
3048 (if reversep
3049 (while (and (null win) (> msg 1))
3050 (setq msg (1- msg)
3051 win (rmail-search-message msg regexp)))
3052 (while (and (null win) (< msg rmail-total-messages))
3053 (setq msg (1+ msg)
3054 win (rmail-search-message msg regexp))))
3055 (setq n (+ n (if reversep 1 -1)))))
3056 (if win
3057 (progn
3058 (rmail-show-message msg)
3059 ;; Search forward (if this is a normal search) or backward
3060 ;; (if this is a reverse search) through this message to
3061 ;; position point. This search may fail because REGEXP
3062 ;; was found in the hidden portion of this message. In
3063 ;; that case, move point to the beginning of visible
3064 ;; portion.
3065 (if reversep
3066 (progn
3067 (goto-char (point-max))
3068 (re-search-backward regexp nil 'move))
3069 (goto-char (point-min))
3070 (re-search-forward regexp nil t))
3071 (message "%sRmail search for %s...done"
3072 (if reversep "Reverse " "")
3073 regexp))
3074 (goto-char opoint)
3075 (narrow-to-region omin omax)
3076 (ding)
3077 (message "Search failed: %s" regexp)))))
3078
3079 (defun rmail-search-backwards (regexp &optional n)
3080 "Show message containing previous match for REGEXP.
3081 Prefix argument gives repeat count; negative argument means search
3082 forward (through later messages).
3083 Interactively, empty argument means use same regexp used last time."
3084 (interactive
3085 (let* ((reversep (>= (prefix-numeric-value current-prefix-arg) 0))
3086 (prompt
3087 (concat (if reversep "Reverse " "") "Rmail search (regexp): "))
3088 regexp)
3089 (if rmail-search-last-regexp
3090 (setq prompt (concat prompt
3091 "(default "
3092 rmail-search-last-regexp
3093 ") ")))
3094 (setq regexp (read-string prompt))
3095 (cond ((not (equal regexp ""))
3096 (setq rmail-search-last-regexp regexp))
3097 ((not rmail-search-last-regexp)
3098 (error "No previous Rmail search string")))
3099 (list rmail-search-last-regexp
3100 (prefix-numeric-value current-prefix-arg))))
3101 (rmail-search regexp (- (or n 1))))
3102
3103 ;; Show the first message which has the `unseen' attribute.
3104 (defun rmail-first-unseen-message ()
3105 (rmail-maybe-set-message-counters)
3106 (let ((current 1)
3107 found)
3108 (save-restriction
3109 (widen)
3110 (while (and (not found) (<= current rmail-total-messages))
3111 (if (rmail-message-labels-p current ", ?\\(unseen\\),")
3112 (setq found current))
3113 (setq current (1+ current))))
3114 ;; Let the caller show the message.
3115 ;; (if found
3116 ;; (rmail-show-message found))
3117 found))
3118
3119 (defun rmail-next-same-subject (n)
3120 "Go to the next mail message having the same subject header.
3121 With prefix argument N, do this N times.
3122 If N is negative, go backwards instead."
3123 (interactive "p")
3124 (let ((subject (mail-fetch-field "Subject"))
3125 (forward (> n 0))
3126 (i rmail-current-message)
3127 (case-fold-search t)
3128 search-regexp found)
3129 (if (string-match "\\`[ \t]+" subject)
3130 (setq subject (substring subject (match-end 0))))
3131 (if (string-match "Re:[ \t]*" subject)
3132 (setq subject (substring subject (match-end 0))))
3133 (if (string-match "[ \t]+\\'" subject)
3134 (setq subject (substring subject 0 (match-beginning 0))))
3135 (setq search-regexp (concat "^Subject: *\\(Re:[ \t]*\\)?"
3136 (regexp-quote subject)
3137 "[ \t]*\n"))
3138 (save-excursion
3139 (save-restriction
3140 (widen)
3141 (while (and (/= n 0)
3142 (if forward
3143 (< i rmail-total-messages)
3144 (> i 1)))
3145 (let (done)
3146 (while (and (not done)
3147 (if forward
3148 (< i rmail-total-messages)
3149 (> i 1)))
3150 (setq i (if forward (1+ i) (1- i)))
3151 (goto-char (rmail-msgbeg i))
3152 (search-forward "\n*** EOOH ***\n")
3153 (let ((beg (point)) end)
3154 (search-forward "\n\n")
3155 (setq end (point))
3156 (goto-char beg)
3157 (setq done (re-search-forward search-regexp end t))))
3158 (if done (setq found i)))
3159 (setq n (if forward (1- n) (1+ n))))))
3160 (if found
3161 (rmail-show-message found)
3162 (error "No %s message with same subject"
3163 (if forward "following" "previous")))))
3164
3165 (defun rmail-previous-same-subject (n)
3166 "Go to the previous mail message having the same subject header.
3167 With prefix argument N, do this N times.
3168 If N is negative, go forwards instead."
3169 (interactive "p")
3170 (rmail-next-same-subject (- n)))
3171 \f
3172 ;;;; *** Rmail Message Deletion Commands ***
3173
3174 (defun rmail-message-deleted-p (n)
3175 (= (aref rmail-deleted-vector n) ?D))
3176
3177 (defun rmail-set-message-deleted-p (n state)
3178 (aset rmail-deleted-vector n (if state ?D ?\ )))
3179
3180 (defun rmail-delete-message ()
3181 "Delete this message and stay on it."
3182 (interactive)
3183 (rmail-set-attribute "deleted" t)
3184 (run-hooks 'rmail-delete-message-hook))
3185
3186 (defun rmail-undelete-previous-message ()
3187 "Back up to deleted message, select it, and undelete it."
3188 (interactive)
3189 (set-buffer rmail-buffer)
3190 (let ((msg rmail-current-message))
3191 (while (and (> msg 0)
3192 (not (rmail-message-deleted-p msg)))
3193 (setq msg (1- msg)))
3194 (if (= msg 0)
3195 (error "No previous deleted message")
3196 (if (/= msg rmail-current-message)
3197 (rmail-show-message msg))
3198 (rmail-set-attribute "deleted" nil)
3199 (if (rmail-summary-exists)
3200 (save-excursion
3201 (set-buffer rmail-summary-buffer)
3202 (rmail-summary-mark-undeleted msg)))
3203 (rmail-maybe-display-summary))))
3204
3205 (defun rmail-delete-forward (&optional backward)
3206 "Delete this message and move to next nondeleted one.
3207 Deleted messages stay in the file until the \\[rmail-expunge] command is given.
3208 With prefix argument, delete and move backward.
3209
3210 Returns t if a new message is displayed after the delete, or nil otherwise."
3211 (interactive "P")
3212 (rmail-set-attribute "deleted" t)
3213 (run-hooks 'rmail-delete-message-hook)
3214 (let ((del-msg rmail-current-message))
3215 (if (rmail-summary-exists)
3216 (rmail-select-summary
3217 (rmail-summary-mark-deleted del-msg)))
3218 (prog1 (rmail-next-undeleted-message (if backward -1 1))
3219 (rmail-maybe-display-summary))))
3220
3221 (defun rmail-delete-backward ()
3222 "Delete this message and move to previous nondeleted one.
3223 Deleted messages stay in the file until the \\[rmail-expunge] command is given."
3224 (interactive)
3225 (rmail-delete-forward t))
3226
3227 ;; Compute the message number a given message would have after expunging.
3228 ;; The present number of the message is OLDNUM.
3229 ;; DELETEDVEC should be rmail-deleted-vector.
3230 ;; The value is nil for a message that would be deleted.
3231 (defun rmail-msg-number-after-expunge (deletedvec oldnum)
3232 (if (or (null oldnum) (= (aref deletedvec oldnum) ?D))
3233 nil
3234 (let ((i 0)
3235 (newnum 0))
3236 (while (< i oldnum)
3237 (if (/= (aref deletedvec i) ?D)
3238 (setq newnum (1+ newnum)))
3239 (setq i (1+ i)))
3240 newnum)))
3241
3242 (defun rmail-expunge-confirmed ()
3243 "Return t if deleted message should be expunged. If necessary, ask the user.
3244 See also user-option `rmail-confirm-expunge'."
3245 (set-buffer rmail-buffer)
3246 (or (not (stringp rmail-deleted-vector))
3247 (not (string-match "D" rmail-deleted-vector))
3248 (null rmail-confirm-expunge)
3249 (funcall rmail-confirm-expunge
3250 "Erase deleted messages from Rmail file? ")))
3251
3252 (defun rmail-only-expunge (&optional dont-show)
3253 "Actually erase all deleted messages in the file."
3254 (interactive)
3255 (set-buffer rmail-buffer)
3256 (message "Expunging deleted messages...")
3257 ;; Discard all undo records for this buffer.
3258 (or (eq buffer-undo-list t)
3259 (setq buffer-undo-list nil))
3260 (rmail-maybe-set-message-counters)
3261 (let* ((omax (- (buffer-size) (point-max)))
3262 (omin (- (buffer-size) (point-min)))
3263 (opoint (if (and (> rmail-current-message 0)
3264 (rmail-message-deleted-p rmail-current-message))
3265 0
3266 (if rmail-enable-mime
3267 (with-current-buffer rmail-view-buffer
3268 (- (point)(point-min)))
3269 (- (point) (point-min)))))
3270 (messages-head (cons (aref rmail-message-vector 0) nil))
3271 (messages-tail messages-head)
3272 ;; Don't make any undo records for the expunging.
3273 (buffer-undo-list t)
3274 (win))
3275 (unwind-protect
3276 (save-excursion
3277 (widen)
3278 (goto-char (point-min))
3279 (let ((counter 0)
3280 (number 1)
3281 (total rmail-total-messages)
3282 (new-message-number rmail-current-message)
3283 (new-summary nil)
3284 (new-msgref (list (list 0)))
3285 (rmailbuf (current-buffer))
3286 (buffer-read-only nil)
3287 (messages rmail-message-vector)
3288 (deleted rmail-deleted-vector)
3289 (summary rmail-summary-vector))
3290 (setq rmail-total-messages nil
3291 rmail-current-message nil
3292 rmail-message-vector nil
3293 rmail-deleted-vector nil
3294 rmail-summary-vector nil)
3295
3296 (while (<= number total)
3297 (if (= (aref deleted number) ?D)
3298 (progn
3299 (delete-region
3300 (marker-position (aref messages number))
3301 (marker-position (aref messages (1+ number))))
3302 (move-marker (aref messages number) nil)
3303 (if (> new-message-number counter)
3304 (setq new-message-number (1- new-message-number))))
3305 (setq counter (1+ counter))
3306 (setq messages-tail
3307 (setcdr messages-tail
3308 (cons (aref messages number) nil)))
3309 (setq new-summary
3310 (cons (if (= counter number) (aref summary (1- number)))
3311 new-summary))
3312 (setq new-msgref
3313 (cons (aref rmail-msgref-vector number)
3314 new-msgref))
3315 (setcar (car new-msgref) counter))
3316 (if (zerop (% (setq number (1+ number)) 20))
3317 (message "Expunging deleted messages...%d" number)))
3318 (setq messages-tail
3319 (setcdr messages-tail
3320 (cons (aref messages number) nil)))
3321 (setq rmail-current-message new-message-number
3322 rmail-total-messages counter
3323 rmail-message-vector (apply 'vector messages-head)
3324 rmail-deleted-vector (make-string (1+ counter) ?\ )
3325 rmail-summary-vector (vconcat (nreverse new-summary))
3326 rmail-msgref-vector (apply 'vector (nreverse new-msgref))
3327 win t)))
3328 (message "Expunging deleted messages...done")
3329 (if (not win)
3330 (narrow-to-region (- (buffer-size) omin) (- (buffer-size) omax)))
3331 (if (not dont-show)
3332 (rmail-show-message
3333 (if (zerop rmail-current-message) 1 nil)))
3334 (if rmail-enable-mime
3335 (goto-char (+ (point-min) opoint))
3336 (goto-char (+ (point) opoint))))))
3337
3338 (defun rmail-expunge ()
3339 "Erase deleted messages from Rmail file and summary buffer."
3340 (interactive)
3341 (when (rmail-expunge-confirmed)
3342 (rmail-only-expunge)
3343 (if (rmail-summary-exists)
3344 (rmail-select-summary (rmail-update-summary)))))
3345 \f
3346 ;;;; *** Rmail Mailing Commands ***
3347
3348 (defun rmail-start-mail (&optional noerase to subject in-reply-to cc
3349 replybuffer sendactions same-window others)
3350 (let (yank-action)
3351 (if replybuffer
3352 (setq yank-action (list 'insert-buffer replybuffer)))
3353 (setq others (cons (cons "cc" cc) others))
3354 (setq others (cons (cons "in-reply-to" in-reply-to) others))
3355 (if same-window
3356 (compose-mail to subject others
3357 noerase nil
3358 yank-action sendactions)
3359 (if rmail-mail-new-frame
3360 (prog1
3361 (compose-mail to subject others
3362 noerase 'switch-to-buffer-other-frame
3363 yank-action sendactions)
3364 ;; This is not a standard frame parameter;
3365 ;; nothing except sendmail.el looks at it.
3366 (modify-frame-parameters (selected-frame)
3367 '((mail-dedicated-frame . t))))
3368 (compose-mail to subject others
3369 noerase 'switch-to-buffer-other-window
3370 yank-action sendactions)))))
3371
3372 (defun rmail-mail ()
3373 "Send mail in another window.
3374 While composing the message, use \\[mail-yank-original] to yank the
3375 original message into it."
3376 (interactive)
3377 (rmail-start-mail nil nil nil nil nil rmail-view-buffer))
3378
3379 (defun rmail-continue ()
3380 "Continue composing outgoing message previously being composed."
3381 (interactive)
3382 (rmail-start-mail t))
3383
3384 (defun rmail-reply (just-sender)
3385 "Reply to the current message.
3386 Normally include CC: to all other recipients of original message;
3387 prefix argument means ignore them. While composing the reply,
3388 use \\[mail-yank-original] to yank the original message into it."
3389 (interactive "P")
3390 (let (from reply-to cc subject date to message-id references
3391 resent-to resent-cc resent-reply-to
3392 (msgnum rmail-current-message))
3393 (save-excursion
3394 (save-restriction
3395 (if rmail-enable-mime
3396 (narrow-to-region
3397 (goto-char (point-min))
3398 (if (search-forward "\n\n" nil 'move)
3399 (1+ (match-beginning 0))
3400 (point)))
3401 (widen)
3402 (goto-char (rmail-msgbeg rmail-current-message))
3403 (forward-line 1)
3404 (if (= (following-char) ?0)
3405 (narrow-to-region
3406 (progn (forward-line 2)
3407 (point))
3408 (progn (search-forward "\n\n" (rmail-msgend rmail-current-message)
3409 'move)
3410 (point)))
3411 (narrow-to-region (point)
3412 (progn (search-forward "\n*** EOOH ***\n")
3413 (beginning-of-line) (point)))))
3414 (setq from (mail-fetch-field "from")
3415 reply-to (or (if just-sender
3416 (mail-fetch-field "mail-reply-to" nil t)
3417 (mail-fetch-field "mail-followup-to" nil t))
3418 (mail-fetch-field "reply-to" nil t)
3419 from)
3420 cc (and (not just-sender)
3421 ;; mail-followup-to, if given, overrides cc.
3422 (not (mail-fetch-field "mail-followup-to" nil t))
3423 (mail-fetch-field "cc" nil t))
3424 subject (mail-fetch-field "subject")
3425 date (mail-fetch-field "date")
3426 to (or (mail-fetch-field "to" nil t) "")
3427 message-id (mail-fetch-field "message-id")
3428 references (mail-fetch-field "references" nil nil t)
3429 resent-reply-to (mail-fetch-field "resent-reply-to" nil t)
3430 resent-cc (and (not just-sender)
3431 (mail-fetch-field "resent-cc" nil t))
3432 resent-to (or (mail-fetch-field "resent-to" nil t) "")
3433 ;;; resent-subject (mail-fetch-field "resent-subject")
3434 ;;; resent-date (mail-fetch-field "resent-date")
3435 ;;; resent-message-id (mail-fetch-field "resent-message-id")
3436 )))
3437 ;; Merge the resent-to and resent-cc into the to and cc.
3438 (if (and resent-to (not (equal resent-to "")))
3439 (if (not (equal to ""))
3440 (setq to (concat to ", " resent-to))
3441 (setq to resent-to)))
3442 (if (and resent-cc (not (equal resent-cc "")))
3443 (if (not (equal cc ""))
3444 (setq cc (concat cc ", " resent-cc))
3445 (setq cc resent-cc)))
3446 ;; Add `Re: ' to subject if not there already.
3447 (and (stringp subject)
3448 (setq subject
3449 (concat rmail-reply-prefix
3450 (if (let ((case-fold-search t))
3451 (string-match rmail-reply-regexp subject))
3452 (substring subject (match-end 0))
3453 subject))))
3454 (rmail-start-mail
3455 nil
3456 ;; Using mail-strip-quoted-names is undesirable with newer mailers
3457 ;; since they can handle the names unstripped.
3458 ;; I don't know whether there are other mailers that still
3459 ;; need the names to be stripped.
3460 ;;; (mail-strip-quoted-names reply-to)
3461 ;; Remove unwanted names from reply-to, since Mail-Followup-To
3462 ;; header causes all the names in it to wind up in reply-to, not
3463 ;; in cc. But if what's left is an empty list, use the original.
3464 (let* ((reply-to-list (rmail-dont-reply-to reply-to)))
3465 (if (string= reply-to-list "") reply-to reply-to-list))
3466 subject
3467 (rmail-make-in-reply-to-field from date message-id)
3468 (if just-sender
3469 nil
3470 ;; mail-strip-quoted-names is NOT necessary for rmail-dont-reply-to
3471 ;; to do its job.
3472 (let* ((cc-list (rmail-dont-reply-to
3473 (mail-strip-quoted-names
3474 (if (null cc) to (concat to ", " cc))))))
3475 (if (string= cc-list "") nil cc-list)))
3476 rmail-view-buffer
3477 (list (list 'rmail-mark-message
3478 rmail-buffer
3479 (with-current-buffer rmail-buffer
3480 (aref rmail-msgref-vector msgnum))
3481 "answered"))
3482 nil
3483 (list (cons "References" (concat (mapconcat 'identity references " ")
3484 " " message-id))))))
3485
3486 (defun rmail-mark-message (buffer msgnum-list attribute)
3487 "Give BUFFER's message number in MSGNUM-LIST the attribute ATTRIBUTE.
3488 This is use in the send-actions for message buffers.
3489 MSGNUM-LIST is a list of the form (MSGNUM)
3490 which is an element of rmail-msgref-vector."
3491 (save-excursion
3492 (set-buffer buffer)
3493 (if (car msgnum-list)
3494 (rmail-set-attribute attribute t (car msgnum-list)))))
3495
3496 (defun rmail-make-in-reply-to-field (from date message-id)
3497 (cond ((not from)
3498 (if message-id
3499 message-id
3500 nil))
3501 (mail-use-rfc822
3502 (require 'rfc822)
3503 (let ((tem (car (rfc822-addresses from))))
3504 (if message-id
3505 (if (or (not tem)
3506 (string-match
3507 (regexp-quote (if (string-match "@[^@]*\\'" tem)
3508 (substring tem 0
3509 (match-beginning 0))
3510 tem))
3511 message-id))
3512 ;; missing From, or Message-ID is sufficiently informative
3513 message-id
3514 (concat message-id " (" tem ")"))
3515 ;; Copy TEM, discarding text properties.
3516 (setq tem (copy-sequence tem))
3517 (set-text-properties 0 (length tem) nil tem)
3518 (setq tem (copy-sequence tem))
3519 ;; Use prin1 to fake RFC822 quoting
3520 (let ((field (prin1-to-string tem)))
3521 (if date
3522 (concat field "'s message of " date)
3523 field)))))
3524 ((let* ((foo "[^][\000-\037()<>@,;:\\\" ]+")
3525 (bar "[^][\000-\037()<>@,;:\\\"]+"))
3526 ;; These strings both match all non-ASCII characters.
3527 (or (string-match (concat "\\`[ \t]*\\(" bar
3528 "\\)\\(<" foo "@" foo ">\\)?[ \t]*\\'")
3529 ;; "Unix Loser <Foo@bar.edu>" => "Unix Loser"
3530 from)
3531 (string-match (concat "\\`[ \t]*<" foo "@" foo ">[ \t]*(\\("
3532 bar "\\))[ \t]*\\'")
3533 ;; "<Bugs@bar.edu>" (Losing Unix) => "Losing Unix"
3534 from)))
3535 (let ((start (match-beginning 1))
3536 (end (match-end 1)))
3537 ;; Trim whitespace which above regexp match allows
3538 (while (and (< start end)
3539 (memq (aref from start) '(?\t ?\ )))
3540 (setq start (1+ start)))
3541 (while (and (< start end)
3542 (memq (aref from (1- end)) '(?\t ?\ )))
3543 (setq end (1- end)))
3544 (let ((field (substring from start end)))
3545 (if date (setq field (concat "message from " field " on " date)))
3546 (if message-id
3547 ;; "<AA259@bar.edu> (message from Unix Loser on 1-Apr-89)"
3548 (concat message-id " (" field ")")
3549 field))))
3550 (t
3551 ;; If we can't kludge it simply, do it correctly
3552 (let ((mail-use-rfc822 t))
3553 (rmail-make-in-reply-to-field from date message-id)))))
3554 \f
3555 (defun rmail-forward (resend)
3556 "Forward the current message to another user.
3557 With prefix argument, \"resend\" the message instead of forwarding it;
3558 see the documentation of `rmail-resend'."
3559 (interactive "P")
3560 (if resend
3561 (call-interactively 'rmail-resend)
3562 (let ((forward-buffer rmail-buffer)
3563 (msgnum rmail-current-message)
3564 (subject (concat "["
3565 (let ((from (or (mail-fetch-field "From")
3566 (mail-fetch-field ">From"))))
3567 (if from
3568 (concat (mail-strip-quoted-names from) ": ")
3569 ""))
3570 (or (mail-fetch-field "Subject") "")
3571 "]")))
3572 (if (rmail-start-mail
3573 nil nil subject nil nil nil
3574 (list (list 'rmail-mark-message
3575 forward-buffer
3576 (with-current-buffer rmail-buffer
3577 (aref rmail-msgref-vector msgnum))
3578 "forwarded"))
3579 ;; If only one window, use it for the mail buffer.
3580 ;; Otherwise, use another window for the mail buffer
3581 ;; so that the Rmail buffer remains visible
3582 ;; and sending the mail will get back to it.
3583 (and (not rmail-mail-new-frame) (one-window-p t)))
3584 ;; The mail buffer is now current.
3585 (save-excursion
3586 ;; Insert after header separator--before signature if any.
3587 (goto-char (mail-text-start))
3588 (if (or rmail-enable-mime rmail-enable-mime-composing)
3589 (funcall rmail-insert-mime-forwarded-message-function
3590 forward-buffer)
3591 (insert "------- Start of forwarded message -------\n")
3592 ;; Quote lines with `- ' if they start with `-'.
3593 (let ((beg (point)) end)
3594 (setq end (point-marker))
3595 (set-marker-insertion-type end t)
3596 (insert-buffer-substring forward-buffer)
3597 (goto-char beg)
3598 (while (re-search-forward "^-" end t)
3599 (beginning-of-line)
3600 (insert "- ")
3601 (forward-line 1))
3602 (goto-char end)
3603 (skip-chars-backward "\n")
3604 (if (< (point) end)
3605 (forward-char 1))
3606 (delete-region (point) end)
3607 (set-marker end nil))
3608 (insert "------- End of forwarded message -------\n"))
3609 (push-mark))))))
3610 \f
3611 (defun rmail-resend (address &optional from comment mail-alias-file)
3612 "Resend current message to ADDRESSES.
3613 ADDRESSES should be a single address, a string consisting of several
3614 addresses separated by commas, or a list of addresses.
3615
3616 Optional FROM is the address to resend the message from, and
3617 defaults from the value of `user-mail-address'.
3618 Optional COMMENT is a string to insert as a comment in the resent message.
3619 Optional ALIAS-FILE is alternate aliases file to be used by sendmail,
3620 typically for purposes of moderating a list."
3621 (interactive "sResend to: ")
3622 (require 'sendmail)
3623 (require 'mailalias)
3624 (unless (or (eq rmail-view-buffer (current-buffer))
3625 (eq rmail-buffer (current-buffer)))
3626 (error "Not an Rmail buffer"))
3627 (if (not from) (setq from user-mail-address))
3628 (let ((tembuf (generate-new-buffer " sendmail temp"))
3629 (case-fold-search nil)
3630 (mail-personal-alias-file
3631 (or mail-alias-file mail-personal-alias-file))
3632 (mailbuf rmail-buffer))
3633 (unwind-protect
3634 (with-current-buffer tembuf
3635 ;;>> Copy message into temp buffer
3636 (if rmail-enable-mime
3637 (funcall rmail-insert-mime-resent-message-function mailbuf)
3638 (insert-buffer-substring mailbuf))
3639 (goto-char (point-min))
3640 ;; Delete any Sender field, since that's not specifiable.
3641 ; Only delete Sender fields in the actual header.
3642 (re-search-forward "^$" nil 'move)
3643 ; Using "while" here rather than "if" because some buggy mail
3644 ; software may have inserted multiple Sender fields.
3645 (while (re-search-backward "^Sender:" nil t)
3646 (let (beg)
3647 (setq beg (point))
3648 (forward-line 1)
3649 (while (looking-at "[ \t]")
3650 (forward-line 1))
3651 (delete-region beg (point))))
3652 ; Go back to the beginning of the buffer so the Resent- fields
3653 ; are inserted there.
3654 (goto-char (point-min))
3655 ;;>> Insert resent-from:
3656 (insert "Resent-From: " from "\n")
3657 (insert "Resent-Date: " (mail-rfc822-date) "\n")
3658 ;;>> Insert resent-to: and bcc if need be.
3659 (let ((before (point)))
3660 (if mail-self-blind
3661 (insert "Resent-Bcc: " (user-login-name) "\n"))
3662 (insert "Resent-To: " (if (stringp address)
3663 address
3664 (mapconcat 'identity address ",\n\t"))
3665 "\n")
3666 ;; Expand abbrevs in the recipients.
3667 (save-excursion
3668 (if (featurep 'mailabbrev)
3669 (let ((end (point-marker))
3670 (local-abbrev-table mail-abbrevs)
3671 (old-syntax-table (syntax-table)))
3672 (if (and (not (vectorp mail-abbrevs))
3673 (file-exists-p mail-personal-alias-file))
3674 (build-mail-abbrevs))
3675 (unless mail-abbrev-syntax-table
3676 (mail-abbrev-make-syntax-table))
3677 (set-syntax-table mail-abbrev-syntax-table)
3678 (goto-char before)
3679 (while (and (< (point) end)
3680 (progn (forward-word 1)
3681 (<= (point) end)))
3682 (expand-abbrev))
3683 (set-syntax-table old-syntax-table))
3684 (expand-mail-aliases before (point)))))
3685 ;;>> Set up comment, if any.
3686 (if (and (sequencep comment) (not (zerop (length comment))))
3687 (let ((before (point))
3688 after)
3689 (insert comment)
3690 (or (eolp) (insert "\n"))
3691 (setq after (point))
3692 (goto-char before)
3693 (while (< (point) after)
3694 (insert "Resent-Comment: ")
3695 (forward-line 1))))
3696 ;; Don't expand aliases in the destination fields
3697 ;; of the original message.
3698 (let (mail-aliases)
3699 (funcall send-mail-function)))
3700 (kill-buffer tembuf))
3701 (with-current-buffer rmail-buffer
3702 (rmail-set-attribute "resent" t rmail-current-message))))
3703 \f
3704 (defvar mail-unsent-separator
3705 (concat "^ *---+ +Unsent message follows +---+ *$\\|"
3706 "^ *---+ +Returned message +---+ *$\\|"
3707 "^ *---+ *Returned mail follows *---+ *$\\|"
3708 "^Start of returned message$\\|"
3709 "^---+ Below this line is a copy of the message.$\\|"
3710 "^ *---+ +Original message +---+ *$\\|"
3711 "^ *--+ +begin message +--+ *$\\|"
3712 "^ *---+ +Original message follows +---+ *$\\|"
3713 "^ *---+ +Your message follows +---+ *$\\|"
3714 "^|? *---+ +Message text follows: +---+ *|?$\\|"
3715 "^ *---+ +This is a copy of \\w+ message, including all the headers.*---+ *$")
3716 "A regexp that matches the separator before the text of a failed message.")
3717
3718 (defvar mail-mime-unsent-header "^Content-Type: message/rfc822 *$"
3719 "A regexp that matches the header of a MIME body part with a failed message.")
3720
3721 (defun rmail-retry-failure ()
3722 "Edit a mail message which is based on the contents of the current message.
3723 For a message rejected by the mail system, extract the interesting headers and
3724 the body of the original message.
3725 If the failed message is a MIME multipart message, it is searched for a
3726 body part with a header which matches the variable `mail-mime-unsent-header'.
3727 Otherwise, the variable `mail-unsent-separator' should match the string that
3728 delimits the returned original message.
3729 The variable `rmail-retry-ignored-headers' is a regular expression
3730 specifying headers which should not be copied into the new message."
3731 (interactive)
3732 (require 'mail-utils)
3733 (let ((rmail-this-buffer (current-buffer))
3734 (msgnum rmail-current-message)
3735 bounce-start bounce-end bounce-indent resending
3736 ;; Fetch any content-type header in current message
3737 ;; Must search thru the whole unpruned header.
3738 (content-type
3739 (save-excursion
3740 (save-restriction
3741 (rmail-narrow-to-non-pruned-header)
3742 (mail-fetch-field "Content-Type") ))))
3743 (save-excursion
3744 (goto-char (point-min))
3745 (let ((case-fold-search t))
3746 (if (and content-type
3747 (string-match
3748 ";[\n\t ]*boundary=\"?\\([-0-9a-z'()+_,./:=? ]+\\)\"?"
3749 content-type))
3750 ;; Handle a MIME multipart bounce message.
3751 (let ((codestring
3752 (concat "\n--"
3753 (substring content-type (match-beginning 1)
3754 (match-end 1)))))
3755 (unless (re-search-forward mail-mime-unsent-header nil t)
3756 (error "Cannot find beginning of header in failed message"))
3757 (unless (search-forward "\n\n" nil t)
3758 (error "Cannot find start of Mime data in failed message"))
3759 (setq bounce-start (point))
3760 (if (search-forward codestring nil t)
3761 (setq bounce-end (match-beginning 0))
3762 (setq bounce-end (point-max))))
3763 ;; Non-MIME bounce.
3764 (or (re-search-forward mail-unsent-separator nil t)
3765 (error "Cannot parse this as a failure message"))
3766 (skip-chars-forward "\n")
3767 ;; Support a style of failure message in which the original
3768 ;; message is indented, and included within lines saying
3769 ;; `Start of returned message' and `End of returned message'.
3770 (if (looking-at " +Received:")
3771 (progn
3772 (setq bounce-start (point))
3773 (skip-chars-forward " ")
3774 (setq bounce-indent (- (current-column)))
3775 (goto-char (point-max))
3776 (re-search-backward "^End of returned message$" nil t)
3777 (setq bounce-end (point)))
3778 ;; One message contained a few random lines before
3779 ;; the old message header. The first line of the
3780 ;; message started with two hyphens. A blank line
3781 ;; followed these random lines. The same line
3782 ;; beginning with two hyphens was possibly marking
3783 ;; the end of the message.
3784 (if (looking-at "^--")
3785 (let ((boundary (buffer-substring-no-properties
3786 (point)
3787 (progn (end-of-line) (point)))))
3788 (search-forward "\n\n")
3789 (skip-chars-forward "\n")
3790 (setq bounce-start (point))
3791 (goto-char (point-max))
3792 (search-backward (concat "\n\n" boundary) bounce-start t)
3793 (setq bounce-end (point)))
3794 (setq bounce-start (point)
3795 bounce-end (point-max)))
3796 (unless (search-forward "\n\n" nil t)
3797 (error "Cannot find end of header in failed message"))))))
3798 ;; We have found the message that bounced, within the current message.
3799 ;; Now start sending new message; default header fields from original.
3800 ;; Turn off the usual actions for initializing the message body
3801 ;; because we want to get only the text from the failure message.
3802 (let (mail-signature mail-setup-hook)
3803 (if (rmail-start-mail nil nil nil nil nil rmail-this-buffer
3804 (list (list 'rmail-mark-message
3805 rmail-this-buffer
3806 (aref rmail-msgref-vector msgnum)
3807 "retried")))
3808 ;; Insert original text as initial text of new draft message.
3809 ;; Bind inhibit-read-only since the header delimiter
3810 ;; of the previous message was probably read-only.
3811 (let ((inhibit-read-only t)
3812 rmail-displayed-headers
3813 rmail-ignored-headers)
3814 (erase-buffer)
3815 (insert-buffer-substring rmail-this-buffer
3816 bounce-start bounce-end)
3817 (goto-char (point-min))
3818 (if bounce-indent
3819 (indent-rigidly (point-min) (point-max) bounce-indent))
3820 (rmail-clear-headers rmail-retry-ignored-headers)
3821 (rmail-clear-headers "^sender:\\|^return-path:\\|^received:")
3822 (mail-sendmail-delimit-header)
3823 (save-restriction
3824 (narrow-to-region (point-min) (mail-header-end))
3825 (setq resending (mail-fetch-field "resent-to"))
3826 (if mail-self-blind
3827 (if resending
3828 (insert "Resent-Bcc: " (user-login-name) "\n")
3829 (insert "BCC: " (user-login-name) "\n"))))
3830 (goto-char (point-min))
3831 (mail-position-on-field (if resending "Resent-To" "To") t))))))
3832 \f
3833 (defun rmail-summary-exists ()
3834 "Non-nil iff in an RMAIL buffer and an associated summary buffer exists.
3835 In fact, the non-nil value returned is the summary buffer itself."
3836 (and rmail-summary-buffer (buffer-name rmail-summary-buffer)
3837 rmail-summary-buffer))
3838
3839 (defun rmail-summary-displayed ()
3840 "t iff in RMAIL buffer and an associated summary buffer is displayed."
3841 (and rmail-summary-buffer (get-buffer-window rmail-summary-buffer)))
3842
3843 (defcustom rmail-redisplay-summary nil
3844 "*Non-nil means Rmail should show the summary when it changes.
3845 This has an effect only if a summary buffer exists."
3846 :type 'boolean
3847 :group 'rmail-summary)
3848
3849 (defcustom rmail-summary-window-size nil
3850 "*Non-nil means specify the height for an Rmail summary window."
3851 :type '(choice (const :tag "Disabled" nil) integer)
3852 :group 'rmail-summary)
3853
3854 ;; Put the summary buffer back on the screen, if user wants that.
3855 (defun rmail-maybe-display-summary ()
3856 (let ((selected (selected-window))
3857 window)
3858 ;; If requested, make sure the summary is displayed.
3859 (and rmail-summary-buffer (buffer-name rmail-summary-buffer)
3860 rmail-redisplay-summary
3861 (if (get-buffer-window rmail-summary-buffer 0)
3862 ;; It's already in some frame; show that one.
3863 (let ((frame (window-frame
3864 (get-buffer-window rmail-summary-buffer 0))))
3865 (make-frame-visible frame)
3866 (raise-frame frame))
3867 (display-buffer rmail-summary-buffer)))
3868 ;; If requested, set the height of the summary window.
3869 (and rmail-summary-buffer (buffer-name rmail-summary-buffer)
3870 rmail-summary-window-size
3871 (setq window (get-buffer-window rmail-summary-buffer))
3872 ;; Don't try to change the size if just one window in frame.
3873 (not (eq window (frame-root-window (window-frame window))))
3874 (unwind-protect
3875 (progn
3876 (select-window window)
3877 (enlarge-window (- rmail-summary-window-size (window-height))))
3878 (select-window selected)))))
3879 \f
3880 ;;;; *** Rmail Local Fontification ***
3881
3882 (defun rmail-fontify-buffer-function ()
3883 ;; This function's symbol is bound to font-lock-fontify-buffer-function.
3884 (add-hook 'rmail-show-message-hook 'rmail-fontify-message nil t)
3885 ;; If we're already showing a message, fontify it now.
3886 (if rmail-current-message (rmail-fontify-message))
3887 ;; Prevent Font Lock mode from kicking in.
3888 (setq font-lock-fontified t))
3889
3890 (defun rmail-unfontify-buffer-function ()
3891 ;; This function's symbol is bound to font-lock-fontify-unbuffer-function.
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-restriction
3897 (widen)
3898 (remove-hook 'rmail-show-message-hook 'rmail-fontify-message t)
3899 (remove-text-properties (point-min) (point-max) '(rmail-fontified nil))
3900 (font-lock-default-unfontify-buffer)
3901 (and (not modified) (buffer-modified-p) (set-buffer-modified-p nil)))))
3902
3903 (defun rmail-fontify-message ()
3904 ;; Fontify the current message if it is not already fontified.
3905 (if (text-property-any (point-min) (point-max) 'rmail-fontified nil)
3906 (let ((modified (buffer-modified-p))
3907 (buffer-undo-list t) (inhibit-read-only t)
3908 before-change-functions after-change-functions
3909 buffer-file-name buffer-file-truename)
3910 (save-excursion
3911 (save-match-data
3912 (add-text-properties (point-min) (point-max) '(rmail-fontified t))
3913 (font-lock-fontify-region (point-min) (point-max))
3914 (and (not modified) (buffer-modified-p) (set-buffer-modified-p nil)))))))
3915 \f
3916 ;;; Speedbar support for RMAIL files.
3917 (eval-when-compile (require 'speedbar))
3918
3919 (defvar rmail-speedbar-match-folder-regexp "^[A-Z0-9]+\\(\\.[A-Z0-9]+\\)?$"
3920 "*This regex is used to match folder names to be displayed in speedbar.
3921 Enabling this will permit speedbar to display your folders for easy
3922 browsing, and moving of messages.")
3923
3924 (defvar rmail-speedbar-last-user nil
3925 "The last user to be displayed in the speedbar.")
3926
3927 (defvar rmail-speedbar-key-map nil
3928 "Keymap used when in rmail display mode.")
3929
3930 (defun rmail-install-speedbar-variables ()
3931 "Install those variables used by speedbar to enhance rmail."
3932 (if rmail-speedbar-key-map
3933 nil
3934 (setq rmail-speedbar-key-map (speedbar-make-specialized-keymap))
3935
3936 (define-key rmail-speedbar-key-map "e" 'speedbar-edit-line)
3937 (define-key rmail-speedbar-key-map "r" 'speedbar-edit-line)
3938 (define-key rmail-speedbar-key-map "\C-m" 'speedbar-edit-line)
3939 (define-key rmail-speedbar-key-map "M"
3940 'rmail-speedbar-move-message-to-folder-on-line)))
3941
3942 (defvar rmail-speedbar-menu-items
3943 '(["Read Folder" speedbar-edit-line t]
3944 ["Move message to folder" rmail-speedbar-move-message-to-folder-on-line
3945 (save-excursion (beginning-of-line)
3946 (looking-at "<M> "))])
3947 "Additional menu-items to add to speedbar frame.")
3948
3949 ;; Make sure our special speedbar major mode is loaded
3950 (if (featurep 'speedbar)
3951 (rmail-install-speedbar-variables)
3952 (add-hook 'speedbar-load-hook 'rmail-install-speedbar-variables))
3953
3954 (defun rmail-speedbar-buttons (buffer)
3955 "Create buttons for BUFFER containing rmail messages.
3956 Click on the address under Reply to: to reply to this person.
3957 Under Folders: Click a name to read it, or on the <M> to move the
3958 current message into that RMAIL folder."
3959 (let ((from nil))
3960 (save-excursion
3961 (set-buffer buffer)
3962 (goto-char (point-min))
3963 (if (not (re-search-forward "^Reply-To: " nil t))
3964 (if (not (re-search-forward "^From:? " nil t))
3965 (setq from t)))
3966 (if from
3967 nil
3968 (setq from (buffer-substring (point) (save-excursion
3969 (end-of-line)
3970 (point))))))
3971 (goto-char (point-min))
3972 (if (and (looking-at "Reply to:")
3973 (equal from rmail-speedbar-last-user))
3974 nil
3975 (setq rmail-speedbar-last-user from)
3976 (erase-buffer)
3977 (insert "Reply To:\n")
3978 (if (stringp from)
3979 (speedbar-insert-button from 'speedbar-directory-face 'highlight
3980 'rmail-speedbar-button 'rmail-reply))
3981 (insert "Folders:\n")
3982 (let* ((case-fold-search nil)
3983 (df (directory-files (save-excursion (set-buffer buffer)
3984 default-directory)
3985 nil rmail-speedbar-match-folder-regexp)))
3986 (while df
3987 (speedbar-insert-button "<M>" 'speedbar-button-face 'highlight
3988 'rmail-speedbar-move-message (car df))
3989 (speedbar-insert-button (car df) 'speedbar-file-face 'highlight
3990 'rmail-speedbar-find-file nil t)
3991 (setq df (cdr df)))))))
3992
3993 (defun rmail-speedbar-button (text token indent)
3994 "Execute an rmail command specified by TEXT.
3995 The command used is TOKEN. INDENT is not used."
3996 (speedbar-with-attached-buffer
3997 (funcall token t)))
3998
3999 (defun rmail-speedbar-find-file (text token indent)
4000 "Load in the rmail file TEXT.
4001 TOKEN and INDENT are not used."
4002 (speedbar-with-attached-buffer
4003 (message "Loading in RMAIL file %s..." text)
4004 (find-file text)))
4005
4006 (defun rmail-speedbar-move-message-to-folder-on-line ()
4007 "If the current line is a folder, move current message to it."
4008 (interactive)
4009 (save-excursion
4010 (beginning-of-line)
4011 (if (re-search-forward "<M> " (save-excursion (end-of-line) (point)) t)
4012 (progn
4013 (forward-char -2)
4014 (speedbar-do-function-pointer)))))
4015
4016 (defun rmail-speedbar-move-message (text token indent)
4017 "From button TEXT, copy current message to the rmail file specified by TOKEN.
4018 TEXT and INDENT are not used."
4019 (speedbar-with-attached-buffer
4020 (message "Moving message to %s" token)
4021 (rmail-output-to-rmail-file token)))
4022
4023 ; Functions for setting, getting and encoding the POP password.
4024 ; The password is encoded to prevent it from being easily accessible
4025 ; to "prying eyes." Obviously, this encoding isn't "real security,"
4026 ; nor is it meant to be.
4027
4028 ;;;###autoload
4029 (defun rmail-set-remote-password (password)
4030 "Set PASSWORD to be used for retrieving mail from a POP or IMAP server."
4031 (interactive "sPassword: ")
4032 (if password
4033 (setq rmail-encoded-remote-password
4034 (rmail-encode-string password (emacs-pid)))
4035 (setq rmail-remote-password nil)
4036 (setq rmail-encoded-remote-password nil)))
4037
4038 (defun rmail-get-remote-password (imap)
4039 "Get the password for retrieving mail from a POP or IMAP server. If none
4040 has been set, then prompt the user for one."
4041 (when (not rmail-encoded-remote-password)
4042 (if (not rmail-remote-password)
4043 (setq rmail-remote-password
4044 (read-passwd (if imap
4045 "IMAP password: "
4046 "POP password: "))))
4047 (rmail-set-remote-password rmail-remote-password)
4048 (setq rmail-remote-password nil))
4049 (rmail-encode-string rmail-encoded-remote-password (emacs-pid)))
4050
4051 (defun rmail-have-password ()
4052 (or rmail-remote-password rmail-encoded-remote-password))
4053
4054 (defun rmail-encode-string (string mask)
4055 "Encode STRING with integer MASK, by taking the exclusive OR of the
4056 lowest byte in the mask with the first character of string, the
4057 second-lowest-byte with the second character of the string, etc.,
4058 restarting at the lowest byte of the mask whenever it runs out.
4059 Returns the encoded string. Calling the function again with an
4060 encoded string (and the same mask) will decode the string."
4061 (setq mask (abs mask)) ; doesn't work if negative
4062 (let* ((string-vector (string-to-vector string)) (i 0)
4063 (len (length string-vector)) (curmask mask) charmask)
4064 (while (< i len)
4065 (if (= curmask 0)
4066 (setq curmask mask))
4067 (setq charmask (% curmask 256))
4068 (setq curmask (lsh curmask -8))
4069 (aset string-vector i (logxor charmask (aref string-vector i)))
4070 (setq i (1+ i)))
4071 (concat string-vector)))
4072
4073 ;;;; Desktop support
4074
4075 (defun rmail-restore-desktop-buffer (desktop-buffer-file-name
4076 desktop-buffer-name
4077 desktop-buffer-misc)
4078 "Restore an rmail buffer specified in a desktop file."
4079 (condition-case error
4080 (progn
4081 (rmail-input desktop-buffer-file-name)
4082 (if (eq major-mode 'rmail-mode)
4083 (current-buffer)
4084 rmail-buffer))
4085 (file-locked
4086 (kill-buffer (current-buffer))
4087 nil)))
4088
4089 (add-to-list 'desktop-buffer-mode-handlers
4090 '(rmail-mode . rmail-restore-desktop-buffer))
4091
4092 (provide 'rmail)
4093
4094 ;;; arch-tag: cff0a950-57fe-4f73-a86e-91ff75afd06c
4095 ;;; rmail.el ends here