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