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