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