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