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