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