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