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