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