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