Comment change.
[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, 2001
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 value of the default value of
144 the 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 ;; Emacs 21.1 and later writes RMAIL files in emacs-mule, but
788 ;; earlier versions did that with the current buffer's encoding.
789 ;; So we want to favor detection of emacs-mule (whose normal
790 ;; priority is quite low), but still allow detection of other
791 ;; encodings if emacs-mule won't fit. The call to
792 ;; detect-coding-with-priority below achieves that.
793 (car (detect-coding-with-priority
794 from to
795 '((coding-category-emacs-mule . emacs-mule))))))
796 (unless (memq coding-system
797 '(undecided undecided-unix))
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 (delete-files ())
1368 ;; If buffer has not changed yet, and has not been saved yet,
1369 ;; don't replace the old backup file now.
1370 (make-backup-files (and make-backup-files (buffer-modified-p)))
1371 (buffer-read-only nil)
1372 ;; Don't make undo records for what we do in getting mail.
1373 (buffer-undo-list t)
1374 success
1375 ;; Files to insert this time around.
1376 files
1377 ;; Last names of those files.
1378 file-last-names)
1379 ;; Pull files off all-files onto files
1380 ;; as long as there is no name conflict.
1381 ;; A conflict happens when two inbox file names
1382 ;; have the same last component.
1383 (while (and all-files
1384 (not (member (file-name-nondirectory (car all-files))
1385 file-last-names)))
1386 (setq files (cons (car all-files) files)
1387 file-last-names
1388 (cons (file-name-nondirectory (car all-files)) files))
1389 (setq all-files (cdr all-files)))
1390 ;; Put them back in their original order.
1391 (setq files (nreverse files))
1392
1393 (goto-char (point-max))
1394 (skip-chars-backward " \t\n") ; just in case of brain damage
1395 (delete-region (point) (point-max)) ; caused by require-final-newline
1396 (save-excursion
1397 (save-restriction
1398 (narrow-to-region (point) (point))
1399 ;; Read in the contents of the inbox files,
1400 ;; renaming them as necessary,
1401 ;; and adding to the list of files to delete eventually.
1402 (if file-name
1403 (rmail-insert-inbox-text files nil)
1404 (setq delete-files (rmail-insert-inbox-text files t)))
1405 ;; Scan the new text and convert each message to babyl format.
1406 (goto-char (point-min))
1407 (unwind-protect
1408 (save-excursion
1409 (setq new-messages (rmail-convert-to-babyl-format)
1410 success t))
1411 ;; Try to delete the garbage just inserted.
1412 (or success (delete-region (point-min) (point-max)))
1413 ;; If we could not convert the file's inboxes,
1414 ;; rename the files we tried to read
1415 ;; so we won't over and over again.
1416 (if (and (not file-name) (not success))
1417 (let ((delfiles delete-files)
1418 (count 0))
1419 (while delfiles
1420 (while (file-exists-p (format "RMAILOSE.%d" count))
1421 (setq count (1+ count)))
1422 (rename-file (car delfiles)
1423 (format "RMAILOSE.%d" count))
1424 (setq delfiles (cdr delfiles))))))
1425 (or (zerop new-messages)
1426 (let (success)
1427 (widen)
1428 (search-backward "\n\^_" nil t)
1429 (narrow-to-region (point) (point-max))
1430 (goto-char (1+ (point-min)))
1431 (rmail-count-new-messages)
1432 (run-hooks 'rmail-get-new-mail-hook)
1433 (save-buffer)))
1434 ;; Delete the old files, now that babyl file is saved.
1435 (while delete-files
1436 (condition-case ()
1437 ;; First, try deleting.
1438 (condition-case ()
1439 (delete-file (car delete-files))
1440 (file-error
1441 ;; If we can't delete it, truncate it.
1442 (write-region (point) (point) (car delete-files))))
1443 (file-error nil))
1444 (setq delete-files (cdr delete-files)))))
1445 (if (= new-messages 0)
1446 (progn (goto-char opoint)
1447 (if (or file-name rmail-inbox-list)
1448 (message "(No new mail has arrived)")))
1449 (if (rmail-summary-exists)
1450 (rmail-select-summary
1451 (rmail-update-summary)))
1452 (message "%d new message%s read"
1453 new-messages (if (= 1 new-messages) "" "s"))
1454 ;; Move to the first new message
1455 ;; unless we have other unseen messages before it.
1456 (rmail-show-message (rmail-first-unseen-message))
1457 (run-hooks 'rmail-after-get-new-mail-hook)
1458 (setq found t))))
1459 found)
1460 ;; Don't leave the buffer screwed up if we get a disk-full error.
1461 (or found (rmail-show-message)))))
1462
1463 (defun rmail-insert-inbox-text (files renamep)
1464 ;; Detect a locked file now, so that we avoid moving mail
1465 ;; out of the real inbox file. (That could scare people.)
1466 (or (memq (file-locked-p buffer-file-name) '(nil t))
1467 (error "RMAIL file %s is locked"
1468 (file-name-nondirectory buffer-file-name)))
1469 (let (file tofile delete-files movemail popmail got-password password)
1470 (while files
1471 ;; Handle POP mailbox names specially; don't expand as filenames
1472 ;; in case the userid contains a directory separator.
1473 (setq file (car files))
1474 (setq popmail (string-match "^po:" file))
1475 (if popmail
1476 (setq renamep t)
1477 (setq file (file-truename
1478 (substitute-in-file-name (expand-file-name file)))))
1479 (setq tofile (expand-file-name
1480 ;; Generate name to move to from inbox name,
1481 ;; in case of multiple inboxes that need moving.
1482 (concat ".newmail-" (file-name-nondirectory file))
1483 ;; Use the directory of this rmail file
1484 ;; because it's a nuisance to use the homedir
1485 ;; if that is on a full disk and this rmail
1486 ;; file isn't.
1487 (file-name-directory
1488 (expand-file-name buffer-file-name))))
1489 ;; Always use movemail to rename the file,
1490 ;; since there can be mailboxes in various directories.
1491 (setq movemail t)
1492 ;;; ;; If getting from mail spool directory,
1493 ;;; ;; use movemail to move rather than just renaming,
1494 ;;; ;; so as to interlock with the mailer.
1495 ;;; (setq movemail (string= file
1496 ;;; (file-truename
1497 ;;; (concat rmail-spool-directory
1498 ;;; (file-name-nondirectory file)))))
1499 (if (and movemail (not popmail))
1500 (progn
1501 ;; On some systems, /usr/spool/mail/foo is a directory
1502 ;; and the actual inbox is /usr/spool/mail/foo/foo.
1503 (if (file-directory-p file)
1504 (setq file (expand-file-name (user-login-name)
1505 file)))))
1506 (cond (popmail
1507 (if rmail-pop-password-required
1508 (progn (setq got-password (not (rmail-have-password)))
1509 (setq password (rmail-get-pop-password))))
1510 (if (memq system-type '(windows-nt cygwin))
1511 ;; cannot have "po:" in file name
1512 (setq tofile
1513 (expand-file-name
1514 (concat ".newmail-pop-"
1515 (file-name-nondirectory (substring file 3)))
1516 (file-name-directory
1517 (expand-file-name buffer-file-name)))))
1518 (message "Getting mail from post office ..."))
1519 ((and (file-exists-p tofile)
1520 (/= 0 (nth 7 (file-attributes tofile))))
1521 (message "Getting mail from %s..." tofile))
1522 ((and (file-exists-p file)
1523 (/= 0 (nth 7 (file-attributes file))))
1524 (message "Getting mail from %s..." file)))
1525 ;; Set TOFILE if have not already done so, and
1526 ;; rename or copy the file FILE to TOFILE if and as appropriate.
1527 (cond ((not renamep)
1528 (setq tofile file))
1529 ((or (file-exists-p tofile) (and (not popmail)
1530 (not (file-exists-p file))))
1531 nil)
1532 ((and (not movemail) (not popmail))
1533 ;; Try copying. If that fails (perhaps no space) and
1534 ;; we're allowed to blow away the inbox, rename instead.
1535 (if rmail-preserve-inbox
1536 (copy-file file tofile nil)
1537 (condition-case nil
1538 (copy-file file tofile nil)
1539 (error
1540 ;; Third arg is t so we can replace existing file TOFILE.
1541 (rename-file file tofile t))))
1542 ;; Make the real inbox file empty.
1543 ;; Leaving it deleted could cause lossage
1544 ;; because mailers often won't create the file.
1545 (if (not rmail-preserve-inbox)
1546 (condition-case ()
1547 (write-region (point) (point) file)
1548 (file-error nil))))
1549 (t
1550 (let ((errors nil))
1551 (unwind-protect
1552 (save-excursion
1553 (setq errors (generate-new-buffer " *rmail loss*"))
1554 (buffer-disable-undo errors)
1555 (let ((args
1556 (append
1557 (list (or rmail-movemail-program
1558 (expand-file-name "movemail"
1559 exec-directory))
1560 nil errors nil)
1561 (if rmail-preserve-inbox
1562 (list "-p")
1563 nil)
1564 rmail-movemail-flags
1565 (list file tofile)
1566 (if password (list password) nil))))
1567 (apply 'call-process args))
1568 (if (not (buffer-modified-p errors))
1569 ;; No output => movemail won
1570 nil
1571 (set-buffer errors)
1572 (subst-char-in-region (point-min) (point-max)
1573 ?\n ?\ )
1574 (goto-char (point-max))
1575 (skip-chars-backward " \t")
1576 (delete-region (point) (point-max))
1577 (goto-char (point-min))
1578 (if (looking-at "movemail: ")
1579 (delete-region (point-min) (match-end 0)))
1580 (beep t)
1581 (message "movemail: %s"
1582 (buffer-substring (point-min)
1583 (point-max)))
1584 ;; If we just read the password, most likely it is
1585 ;; wrong. Otherwise, see if there is a specific
1586 ;; reason to think that the problem is a wrong passwd.
1587 (if (or got-password
1588 (re-search-forward rmail-pop-password-error
1589 nil t))
1590 (rmail-set-pop-password nil))
1591 (sit-for 3)
1592 nil))
1593 (if errors (kill-buffer errors))))))
1594 ;; At this point, TOFILE contains the name to read:
1595 ;; Either the alternate name (if we renamed)
1596 ;; or the actual inbox (if not renaming).
1597 (if (file-exists-p tofile)
1598 (let ((coding-system-for-read 'no-conversion)
1599 size)
1600 (goto-char (point-max))
1601 (setq size (nth 1 (insert-file-contents tofile)))
1602 (goto-char (point-max))
1603 (or (= (preceding-char) ?\n)
1604 (zerop size)
1605 (insert ?\n))
1606 (if (not (and rmail-preserve-inbox (string= file tofile)))
1607 (setq delete-files (cons tofile delete-files)))))
1608 (message "")
1609 (setq files (cdr files)))
1610 delete-files))
1611
1612 ;; Decode the region specified by FROM and TO by CODING.
1613 ;; If CODING is nil or an invalid coding system, decode by `undecided'.
1614 (defun rmail-decode-region (from to coding)
1615 (if (or (not coding) (not (coding-system-p coding)))
1616 (setq coding 'undecided))
1617 (decode-coding-region from to coding))
1618
1619 ;; the rmail-break-forwarded-messages feature is not implemented
1620 (defun rmail-convert-to-babyl-format ()
1621 (let ((count 0) start
1622 (case-fold-search nil)
1623 (invalid-input-resync
1624 (function (lambda ()
1625 (message "Invalid Babyl format in inbox!")
1626 (sit-for 3)
1627 ;; Try to get back in sync with a real message.
1628 (if (re-search-forward
1629 (concat rmail-mmdf-delim1 "\\|^From") nil t)
1630 (beginning-of-line)
1631 (goto-char (point-max)))))))
1632 (goto-char (point-min))
1633 (save-restriction
1634 (while (not (eobp))
1635 (setq start (point))
1636 (cond ((looking-at "BABYL OPTIONS:");Babyl header
1637 (if (search-forward "\n\^_" nil t)
1638 ;; If we find the proper terminator, delete through there.
1639 (delete-region (point-min) (point))
1640 (funcall invalid-input-resync)
1641 (delete-region (point-min) (point))))
1642 ;; Babyl format message
1643 ((looking-at "\^L")
1644 (or (search-forward "\n\^_" nil t)
1645 (funcall invalid-input-resync))
1646 (setq count (1+ count))
1647 ;; Make sure there is no extra white space after the ^_
1648 ;; at the end of the message.
1649 ;; Narrowing will make sure that whatever follows the junk
1650 ;; will be treated properly.
1651 (delete-region (point)
1652 (save-excursion
1653 (skip-chars-forward " \t\n")
1654 (point)))
1655 (save-excursion
1656 (let* ((header-end
1657 (progn
1658 (save-excursion
1659 (goto-char start)
1660 (forward-line 1)
1661 (if (looking-at "0")
1662 (forward-line 1)
1663 (forward-line 2))
1664 (save-restriction
1665 (narrow-to-region (point) (point-max))
1666 (rfc822-goto-eoh)
1667 (point)))))
1668 (case-fold-search t)
1669 (quoted-printable-header-field-end
1670 (save-excursion
1671 (goto-char start)
1672 (re-search-forward
1673 "^content-transfer-encoding:\\(\n?[\t ]\\)*quoted-printable\\(\n?[\t ]\\)*"
1674 header-end t)))
1675 (base64-header-field-end
1676 (save-excursion
1677 (goto-char start)
1678 (re-search-forward
1679 "^content-transfer-encoding:\\(\n?[\t ]\\)*base64\\(\n?[\t ]\\)*"
1680 header-end t))))
1681 (if quoted-printable-header-field-end
1682 (save-excursion
1683 (rmail-decode-quoted-printable header-end (point))
1684 ;; Change "quoted-printable" to "8bit",
1685 ;; to reflect the decoding we just did.
1686 (goto-char quoted-printable-header-field-end)
1687 (delete-region (point) (search-backward ":"))
1688 (insert ": 8bit")))
1689 (if base64-header-field-end
1690 (save-excursion
1691 (base64-decode-region (1+ header-end)
1692 (- (point) 2))
1693 ;; Change "base64" to "8bit", to reflect the
1694 ;; decoding we just did.
1695 (goto-char (1+ header-end))
1696 (while (search-forward "\r\n" (point-max) t)
1697 (replace-match "\n"))
1698 (goto-char base64-header-field-end)
1699 (delete-region (point) (search-backward ":"))
1700 (insert ": 8bit")))
1701 (setq last-coding-system-used nil)
1702 (or rmail-enable-mime
1703 (not rmail-enable-multibyte)
1704 (let ((mime-charset
1705 (if (and rmail-decode-mime-charset
1706 (save-excursion
1707 (goto-char start)
1708 (search-forward "\n\n" nil t)
1709 (let ((case-fold-search t))
1710 (re-search-backward
1711 rmail-mime-charset-pattern
1712 start t))))
1713 (intern (downcase (match-string 1))))))
1714 (rmail-decode-region start (point) mime-charset)))))
1715 ;; Add an X-Coding-System: header if we don't have one.
1716 (save-excursion
1717 (goto-char start)
1718 (forward-line 1)
1719 (if (looking-at "0")
1720 (forward-line 1)
1721 (forward-line 2))
1722 (or (save-restriction
1723 (narrow-to-region (point) (point-max))
1724 (rfc822-goto-eoh)
1725 (goto-char (point-min))
1726 (re-search-forward "^X-Coding-System:" nil t))
1727 (insert "X-Coding-System: "
1728 (symbol-name last-coding-system-used)
1729 "\n")))
1730 (narrow-to-region (point) (point-max)))
1731 ;;*** MMDF format
1732 ((let ((case-fold-search t))
1733 (looking-at rmail-mmdf-delim1))
1734 (let ((case-fold-search t))
1735 (replace-match "\^L\n0, unseen,,\n*** EOOH ***\n")
1736 (re-search-forward rmail-mmdf-delim2 nil t)
1737 (replace-match "\^_"))
1738 (save-excursion
1739 (save-restriction
1740 (narrow-to-region start (1- (point)))
1741 (goto-char (point-min))
1742 (while (search-forward "\n\^_" nil t); single char "\^_"
1743 (replace-match "\n^_")))); 2 chars: "^" and "_"
1744 (setq last-coding-system-used nil)
1745 (or rmail-enable-mime
1746 (not rmail-enable-multibyte)
1747 (decode-coding-region start (point) 'undecided))
1748 (save-excursion
1749 (goto-char start)
1750 (forward-line 3)
1751 (insert "X-Coding-System: "
1752 (symbol-name last-coding-system-used)
1753 "\n"))
1754 (narrow-to-region (point) (point-max))
1755 (setq count (1+ count)))
1756 ;;*** Mail format
1757 ((looking-at "^From ")
1758 (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
1759 (rmail-nuke-pinhead-header)
1760 ;; If this message has a Content-Length field,
1761 ;; skip to the end of the contents.
1762 (let* ((header-end (save-excursion
1763 (and (re-search-forward "\n\n" nil t)
1764 (1- (point)))))
1765 (case-fold-search t)
1766 (quoted-printable-header-field-end
1767 (save-excursion
1768 (re-search-forward
1769 "^content-transfer-encoding:\\(\n?[\t ]\\)*quoted-printable\\(\n?[\t ]\\)*"
1770 header-end t)))
1771 (base64-header-field-end
1772 (save-excursion
1773 (re-search-forward
1774 "^content-transfer-encoding:\\(\n?[\t ]\\)*base64\\(\n?[\t ]\\)*"
1775 header-end t)))
1776 (size
1777 ;; Get the numeric value from the Content-Length field.
1778 (save-excursion
1779 ;; Back up to end of prev line,
1780 ;; in case the Content-Length field comes first.
1781 (forward-char -1)
1782 (and (search-forward "\ncontent-length: "
1783 header-end t)
1784 (let ((beg (point))
1785 (eol (progn (end-of-line) (point))))
1786 (string-to-int (buffer-substring beg eol)))))))
1787 (and size
1788 (if (and (natnump size)
1789 (<= (+ header-end size) (point-max))
1790 ;; Make sure this would put us at a position
1791 ;; that we could continue from.
1792 (save-excursion
1793 (goto-char (+ header-end size))
1794 (skip-chars-forward "\n")
1795 (or (eobp)
1796 (and (looking-at "BABYL OPTIONS:")
1797 (search-forward "\n\^_" nil t))
1798 (and (looking-at "\^L")
1799 (search-forward "\n\^_" nil t))
1800 (let ((case-fold-search t))
1801 (looking-at rmail-mmdf-delim1))
1802 (looking-at "From "))))
1803 (goto-char (+ header-end size))
1804 (message "Ignoring invalid Content-Length field")
1805 (sit-for 1 0 t)))
1806 (if (let ((case-fold-search nil))
1807 (re-search-forward
1808 (concat "^[\^_]?\\("
1809 rmail-unix-mail-delimiter
1810 "\\|"
1811 rmail-mmdf-delim1 "\\|"
1812 "^BABYL OPTIONS:\\|"
1813 "\^L\n[01],\\)") nil t))
1814 (goto-char (match-beginning 1))
1815 (goto-char (point-max)))
1816 (setq count (1+ count))
1817 (if quoted-printable-header-field-end
1818 (save-excursion
1819 (rmail-decode-quoted-printable header-end (point))
1820 ;; Change "quoted-printable" to "8bit",
1821 ;; to reflect the decoding we just did.
1822 (goto-char quoted-printable-header-field-end)
1823 (delete-region (point) (search-backward ":"))
1824 (insert ": 8bit")))
1825 (if base64-header-field-end
1826 (save-excursion
1827 (base64-decode-region header-end (point))
1828 ;; Change "base64" to "8bit", to reflect the
1829 ;; decoding we just did.
1830 (goto-char base64-header-field-end)
1831 (delete-region (point) (search-backward ":"))
1832 (insert ": 8bit"))))
1833
1834 (save-excursion
1835 (save-restriction
1836 (narrow-to-region start (point))
1837 (goto-char (point-min))
1838 (while (search-forward "\n\^_" nil t); single char
1839 (replace-match "\n^_")))); 2 chars: "^" and "_"
1840 (insert ?\^_)
1841 (setq last-coding-system-used nil)
1842 (or rmail-enable-mime
1843 (not rmail-enable-multibyte)
1844 (let ((mime-charset
1845 (if (and rmail-decode-mime-charset
1846 (save-excursion
1847 (goto-char start)
1848 (search-forward "\n\n" nil t)
1849 (let ((case-fold-search t))
1850 (re-search-backward
1851 rmail-mime-charset-pattern
1852 start t))))
1853 (intern (downcase (match-string 1))))))
1854 (rmail-decode-region start (point) mime-charset)))
1855 (save-excursion
1856 (goto-char start)
1857 (forward-line 3)
1858 (insert "X-Coding-System: "
1859 (symbol-name last-coding-system-used)
1860 "\n"))
1861 (narrow-to-region (point) (point-max)))
1862 ;;
1863 ;; This kludge is because some versions of sendmail.el
1864 ;; insert an extra newline at the beginning that shouldn't
1865 ;; be there. sendmail.el has been fixed, but old versions
1866 ;; may still be in use. -- rms, 7 May 1993.
1867 ((eolp) (delete-char 1))
1868 (t (error "Cannot convert to babyl format")))))
1869 count))
1870
1871 (defun rmail-hex-char-to-integer (character)
1872 "Return CHARACTER's value interpreted as a hex digit."
1873 (if (and (>= character ?0) (<= character ?9))
1874 (- character ?0)
1875 (let ((ch (logior character 32)))
1876 (if (and (>= ch ?a) (<= ch ?f))
1877 (- ch (- ?a 10))
1878 (error "Invalid hex digit `%c'" ch)))))
1879
1880 (defun rmail-hex-string-to-integer (hex-string)
1881 "Return decimal integer for HEX-STRING."
1882 (let ((hex-num 0)
1883 (index 0))
1884 (while (< index (length hex-string))
1885 (setq hex-num (+ (* hex-num 16)
1886 (rmail-hex-char-to-integer (aref hex-string index))))
1887 (setq index (1+ index)))
1888 hex-num))
1889
1890 (defun rmail-decode-quoted-printable (from to)
1891 "Decode Quoted-Printable in the region between FROM and TO."
1892 (interactive "r")
1893 (goto-char from)
1894 (or (markerp to)
1895 (setq to (copy-marker to)))
1896 (while (search-forward "=" to t)
1897 (cond ((eq (following-char) ?\n)
1898 (delete-char -1)
1899 (delete-char 1))
1900 ((looking-at "[0-9A-F][0-9A-F]")
1901 (let ((byte (rmail-hex-string-to-integer
1902 (buffer-substring (point) (+ 2 (point))))))
1903 (delete-region (1- (point)) (+ 2 (point)))
1904 (insert byte)))
1905 ((looking-at "=")
1906 (delete-char 1))
1907 (t
1908 (message "Malformed MIME quoted-printable message")))))
1909
1910 ;; Delete the "From ..." line, creating various other headers with
1911 ;; information from it if they don't already exist. Now puts the
1912 ;; original line into a mail-from: header line for debugging and for
1913 ;; use by the rmail-output function.
1914 (defun rmail-nuke-pinhead-header ()
1915 (save-excursion
1916 (save-restriction
1917 (let ((start (point))
1918 (end (progn
1919 (condition-case ()
1920 (search-forward "\n\n")
1921 (error
1922 (goto-char (point-max))
1923 (insert "\n\n")))
1924 (point)))
1925 has-from has-date)
1926 (narrow-to-region start end)
1927 (let ((case-fold-search t))
1928 (goto-char start)
1929 (setq has-from (search-forward "\nFrom:" nil t))
1930 (goto-char start)
1931 (setq has-date (and (search-forward "\nDate:" nil t) (point)))
1932 (goto-char start))
1933 (let ((case-fold-search nil))
1934 (if (re-search-forward (concat "^" rmail-unix-mail-delimiter) nil t)
1935 (replace-match
1936 (concat
1937 "Mail-from: \\&"
1938 ;; Keep and reformat the date if we don't
1939 ;; have a Date: field.
1940 (if has-date
1941 ""
1942 (concat
1943 "Date: \\2, \\4 \\3 \\9 \\5 "
1944
1945 ;; The timezone could be matched by group 7 or group 10.
1946 ;; If neither of them matched, assume EST, since only
1947 ;; Easterners would be so sloppy.
1948 ;; It's a shame the substitution can't use "\\10".
1949 (cond
1950 ((/= (match-beginning 7) (match-end 7)) "\\7")
1951 ((/= (match-beginning 10) (match-end 10))
1952 (buffer-substring (match-beginning 10)
1953 (match-end 10)))
1954 (t "EST"))
1955 "\n"))
1956 ;; Keep and reformat the sender if we don't
1957 ;; have a From: field.
1958 (if has-from
1959 ""
1960 "From: \\1\n"))
1961 t)))))))
1962 \f
1963 ;;;; *** Rmail Message Formatting and Header Manipulation ***
1964
1965 (defun rmail-reformat-message (beg end)
1966 (goto-char beg)
1967 (forward-line 1)
1968 (if (/= (following-char) ?0)
1969 (error "Bad format in RMAIL file"))
1970 (let ((inhibit-read-only t)
1971 (delta (- (buffer-size) end)))
1972 (delete-char 1)
1973 (insert ?1)
1974 (forward-line 1)
1975 (let ((case-fold-search t))
1976 (while (looking-at "Summary-line:\\|Mail-From:")
1977 (forward-line 1)))
1978 (if (looking-at "\\*\\*\\* EOOH \\*\\*\\*\n")
1979 (delete-region (point)
1980 (progn (forward-line 1) (point))))
1981 (let ((str (buffer-substring (point)
1982 (save-excursion (search-forward "\n\n" end 'move)
1983 (point)))))
1984 (insert str "*** EOOH ***\n")
1985 (narrow-to-region (point) (- (buffer-size) delta)))
1986 (goto-char (point-min))
1987 (if rmail-message-filter (funcall rmail-message-filter))
1988 (if (or rmail-displayed-headers rmail-ignored-headers)
1989 (rmail-clear-headers))))
1990
1991 (defun rmail-clear-headers (&optional ignored-headers)
1992 "Delete all header fields that Rmail should not show.
1993 If the optional argument IGNORED-HEADERS is non-nil,
1994 delete all header fields whose names match that regexp.
1995 Otherwise, if `rmail-displayed-headers' is non-nil,
1996 delete all header fields *except* those whose names match that regexp.
1997 Otherwise, delete all header fields whose names match `rmail-ignored-headers'."
1998 (when (search-forward "\n\n" nil t)
1999 (forward-char -1)
2000 (let ((case-fold-search t)
2001 (buffer-read-only nil))
2002 (if (and rmail-displayed-headers (null ignored-headers))
2003 (save-restriction
2004 (narrow-to-region (point-min) (point))
2005 (let (lim next)
2006 (goto-char (point-min))
2007 (while (and (not (eobp))
2008 (save-excursion
2009 (if (re-search-forward "\n[^ \t]" nil t)
2010 (setq lim (match-beginning 0)
2011 next (1+ lim))
2012 (setq lim nil next (point-max)))))
2013 (if (save-excursion
2014 (re-search-forward rmail-displayed-headers lim t))
2015 (goto-char next)
2016 (delete-region (point) next))))
2017 (goto-char (point-min)))
2018 (or ignored-headers (setq ignored-headers rmail-ignored-headers))
2019 (save-restriction
2020 (narrow-to-region (point-min) (point))
2021 (while (and ignored-headers
2022 (progn
2023 (goto-char (point-min))
2024 (re-search-forward ignored-headers nil t)))
2025 (beginning-of-line)
2026 (delete-region (point)
2027 (if (re-search-forward "\n[^ \t]" nil t)
2028 (1- (point))
2029 (point-max)))))))))
2030
2031 (defun rmail-msg-is-pruned ()
2032 (rmail-maybe-set-message-counters)
2033 (save-restriction
2034 (narrow-to-region (rmail-msgbeg rmail-current-message) (point-max))
2035 (save-excursion
2036 (goto-char (point-min))
2037 (forward-line 1)
2038 (= (following-char) ?1))))
2039
2040 (defun rmail-msg-restore-non-pruned-header ()
2041 (let ((old-point (point))
2042 new-point
2043 new-start
2044 (inhibit-read-only t))
2045 (save-excursion
2046 (narrow-to-region (rmail-msgbeg rmail-current-message) (point-max))
2047 (goto-char (point-min))
2048 (forward-line 1)
2049 ;; Change 1 to 0.
2050 (delete-char 1)
2051 (insert ?0)
2052 ;; Insert new EOOH line at the proper place.
2053 (forward-line 1)
2054 (let ((case-fold-search t))
2055 (while (looking-at "Summary-Line:\\|Mail-From:")
2056 (forward-line 1)))
2057 (insert "*** EOOH ***\n")
2058 (setq new-start (point))
2059 ;; Delete the old reformatted header.
2060 (forward-char -1)
2061 (search-forward "\n*** EOOH ***\n")
2062 (forward-line -1)
2063 (let ((start (point)))
2064 (search-forward "\n\n")
2065 (if (and (<= start old-point)
2066 (<= old-point (point)))
2067 (setq new-point new-start))
2068 (delete-region start (point)))
2069 ;; Narrow to after the new EOOH line.
2070 (narrow-to-region new-start (point-max)))
2071 (if new-point
2072 (goto-char new-point))))
2073
2074 (defun rmail-msg-prune-header ()
2075 (let ((new-point
2076 (= (point) (point-min))))
2077 (save-excursion
2078 (narrow-to-region (rmail-msgbeg rmail-current-message) (point-max))
2079 (rmail-reformat-message (point-min) (point-max)))
2080 (if new-point
2081 (goto-char (point-min)))))
2082
2083 (defun rmail-toggle-header (&optional arg)
2084 "Show original message header if pruned header currently shown, or vice versa.
2085 With argument ARG, show the message header pruned if ARG is greater than zero;
2086 otherwise, show it in full."
2087 (interactive "P")
2088 (let* ((pruned (with-current-buffer rmail-buffer
2089 (rmail-msg-is-pruned)))
2090 (prune (if arg
2091 (> (prefix-numeric-value arg) 0)
2092 (not pruned))))
2093 (if (eq pruned prune)
2094 t
2095 (set-buffer rmail-buffer)
2096 (rmail-maybe-set-message-counters)
2097 (if rmail-enable-mime
2098 (let ((buffer-read-only nil))
2099 (if pruned
2100 (rmail-msg-restore-non-pruned-header)
2101 (rmail-msg-prune-header))
2102 (funcall rmail-show-mime-function))
2103 (let* ((buffer-read-only nil)
2104 (window (get-buffer-window (current-buffer)))
2105 (at-point-min (= (point) (point-min)))
2106 (all-headers-visible (= (window-start window) (point-min)))
2107 (on-header
2108 (save-excursion
2109 (and (not (search-backward "\n\n" nil t))
2110 (progn
2111 (end-of-line)
2112 (re-search-backward "^[-A-Za-z0-9]+:" nil t))
2113 (match-string 0))))
2114 (old-screen-line
2115 (rmail-count-screen-lines (window-start window) (point))))
2116 (if pruned
2117 (rmail-msg-restore-non-pruned-header)
2118 (rmail-msg-prune-header))
2119 (cond (at-point-min
2120 (goto-char (point-min)))
2121 (on-header
2122 (goto-char (point-min))
2123 (search-forward "\n\n")
2124 (or (re-search-backward
2125 (concat "^" (regexp-quote on-header)) nil t)
2126 (goto-char (point-min))))
2127 (t
2128 (save-selected-window
2129 (select-window window)
2130 (recenter old-screen-line)
2131 (if (and all-headers-visible
2132 (not (= (window-start) (point-min))))
2133 (recenter (- (window-height) 2))))))))
2134 (rmail-highlight-headers))))
2135
2136 (defun rmail-narrow-to-non-pruned-header ()
2137 "Narrow to the whole (original) header of the current message."
2138 (let (start end)
2139 (narrow-to-region (rmail-msgbeg rmail-current-message) (point-max))
2140 (goto-char (point-min))
2141 (forward-line 1)
2142 (if (= (following-char) ?1)
2143 (progn
2144 (forward-line 1)
2145 (setq start (point))
2146 (search-forward "*** EOOH ***\n")
2147 (setq end (match-beginning 0)))
2148 (forward-line 2)
2149 (setq start (point))
2150 (search-forward "\n\n")
2151 (setq end (1- (point))))
2152 (narrow-to-region start end)
2153 (goto-char start)))
2154
2155 ;; Lifted from repos-count-screen-lines.
2156 ;; Return number of screen lines between START and END.
2157 (defun rmail-count-screen-lines (start end)
2158 (save-excursion
2159 (save-restriction
2160 (narrow-to-region start end)
2161 (goto-char (point-min))
2162 (vertical-motion (- (point-max) (point-min))))))
2163 \f
2164 ;;;; *** Rmail Attributes and Keywords ***
2165
2166 ;; Make a string describing current message's attributes and keywords
2167 ;; and set it up as the name of a minor mode
2168 ;; so it will appear in the mode line.
2169 (defun rmail-display-labels ()
2170 (let ((blurb "") (beg (point-min-marker)) (end (point-max-marker)))
2171 (save-excursion
2172 (unwind-protect
2173 (progn
2174 (widen)
2175 (goto-char (rmail-msgbeg rmail-current-message))
2176 (forward-line 1)
2177 (if (looking-at "[01],")
2178 (progn
2179 (narrow-to-region (point) (progn (end-of-line) (point)))
2180 ;; Truly valid BABYL format requires a space before each
2181 ;; attribute or keyword name. Put them in if missing.
2182 (let (buffer-read-only)
2183 (goto-char (point-min))
2184 (while (search-forward "," nil t)
2185 (or (looking-at "[ ,]") (eobp)
2186 (insert " "))))
2187 (goto-char (point-max))
2188 (if (search-backward ",," nil 'move)
2189 (progn
2190 (if (> (point) (1+ (point-min)))
2191 (setq blurb (buffer-substring (+ 1 (point-min)) (point))))
2192 (if (> (- (point-max) (point)) 2)
2193 (setq blurb
2194 (concat blurb
2195 ";"
2196 (buffer-substring (+ (point) 3)
2197 (1- (point-max)))))))))))
2198 ;; Note: we don't use save-restriction because that does not work right
2199 ;; if changes are made outside the saved restriction
2200 ;; before that restriction is restored.
2201 (narrow-to-region beg end)
2202 (set-marker beg nil)
2203 (set-marker end nil)))
2204 (while (string-match " +," blurb)
2205 (setq blurb (concat (substring blurb 0 (match-beginning 0)) ","
2206 (substring blurb (match-end 0)))))
2207 (while (string-match ", +" blurb)
2208 (setq blurb (concat (substring blurb 0 (match-beginning 0)) ","
2209 (substring blurb (match-end 0)))))
2210 (setq mode-line-process
2211 (format " %d/%d%s"
2212 rmail-current-message rmail-total-messages blurb))
2213 ;; If rmail-enable-mime is non-nil, we may have to update
2214 ;; `mode-line-process' of rmail-view-buffer too.
2215 (if (and rmail-enable-mime
2216 (not (eq (current-buffer) rmail-view-buffer))
2217 (buffer-live-p rmail-view-buffer))
2218 (let ((mlp mode-line-process))
2219 (with-current-buffer rmail-view-buffer
2220 (setq mode-line-process mlp))))))
2221
2222 ;; Turn an attribute of a message on or off according to STATE.
2223 ;; ATTR is the name of the attribute, as a string.
2224 ;; MSGNUM is message number to change; nil means current message.
2225 (defun rmail-set-attribute (attr state &optional msgnum)
2226 (set-buffer rmail-buffer)
2227 (let ((omax (point-max-marker))
2228 (omin (point-min-marker))
2229 (buffer-read-only nil))
2230 (or msgnum (setq msgnum rmail-current-message))
2231 (if (> msgnum 0)
2232 (unwind-protect
2233 (save-excursion
2234 (widen)
2235 (goto-char (+ 3 (rmail-msgbeg msgnum)))
2236 (let ((curstate
2237 (not
2238 (null (search-backward (concat ", " attr ",")
2239 (prog1 (point) (end-of-line)) t)))))
2240 (or (eq curstate (not (not state)))
2241 (if curstate
2242 (delete-region (point) (1- (match-end 0)))
2243 (beginning-of-line)
2244 (forward-char 2)
2245 (insert " " attr ","))))
2246 (if (string= attr "deleted")
2247 (rmail-set-message-deleted-p msgnum state)))
2248 ;; Note: we don't use save-restriction because that does not work right
2249 ;; if changes are made outside the saved restriction
2250 ;; before that restriction is restored.
2251 (narrow-to-region omin omax)
2252 (set-marker omin nil)
2253 (set-marker omax nil)
2254 (if (= msgnum rmail-current-message)
2255 (rmail-display-labels))))))
2256
2257 ;; Return t if the attributes/keywords line of msg number MSG
2258 ;; contains a match for the regexp LABELS.
2259 (defun rmail-message-labels-p (msg labels)
2260 (save-excursion
2261 (save-restriction
2262 (widen)
2263 (goto-char (rmail-msgbeg msg))
2264 (forward-char 3)
2265 (re-search-backward labels (prog1 (point) (end-of-line)) t))))
2266 \f
2267 ;;;; *** Rmail Message Selection And Support ***
2268
2269 (defun rmail-msgend (n)
2270 (marker-position (aref rmail-message-vector (1+ n))))
2271
2272 (defun rmail-msgbeg (n)
2273 (marker-position (aref rmail-message-vector n)))
2274
2275 (defun rmail-widen-to-current-msgbeg (function)
2276 "Call FUNCTION with point at start of internal data of current message.
2277 Assumes that bounds were previously narrowed to display the message in Rmail.
2278 The bounds are widened enough to move point where desired, then narrowed
2279 again afterward.
2280
2281 FUNCTION may not change the visible text of the message, but it may
2282 change the invisible header text."
2283 (save-excursion
2284 (unwind-protect
2285 (progn
2286 (narrow-to-region (rmail-msgbeg rmail-current-message)
2287 (point-max))
2288 (goto-char (point-min))
2289 (funcall function))
2290 ;; Note: we don't use save-restriction because that does not work right
2291 ;; if changes are made outside the saved restriction
2292 ;; before that restriction is restored.
2293 (narrow-to-region (rmail-msgbeg rmail-current-message)
2294 (rmail-msgend rmail-current-message)))))
2295
2296 (defun rmail-forget-messages ()
2297 (unwind-protect
2298 (if (vectorp rmail-message-vector)
2299 (let* ((i 0)
2300 (v rmail-message-vector)
2301 (n (length v)))
2302 (while (< i n)
2303 (move-marker (aref v i) nil)
2304 (setq i (1+ i)))))
2305 (setq rmail-message-vector nil)
2306 (setq rmail-msgref-vector nil)
2307 (setq rmail-deleted-vector nil)))
2308
2309 (defun rmail-maybe-set-message-counters ()
2310 (if (not (and rmail-deleted-vector
2311 rmail-message-vector
2312 rmail-current-message
2313 rmail-total-messages))
2314 (rmail-set-message-counters)))
2315
2316 (defun rmail-count-new-messages (&optional nomsg)
2317 (let* ((case-fold-search nil)
2318 (total-messages 0)
2319 (messages-head nil)
2320 (deleted-head nil))
2321 (or nomsg (message "Counting new messages..."))
2322 (goto-char (point-max))
2323 ;; Put at the end of messages-head
2324 ;; the entry for message N+1, which marks
2325 ;; the end of message N. (N = number of messages).
2326 (search-backward "\n\^_")
2327 (forward-char 1)
2328 (setq messages-head (list (point-marker)))
2329 (rmail-set-message-counters-counter (point-min))
2330 (setq rmail-current-message (1+ rmail-total-messages))
2331 (setq rmail-total-messages
2332 (+ rmail-total-messages total-messages))
2333 (setq rmail-message-vector
2334 (vconcat rmail-message-vector (cdr messages-head)))
2335 (aset rmail-message-vector
2336 rmail-current-message (car messages-head))
2337 (setq rmail-deleted-vector
2338 (concat rmail-deleted-vector deleted-head))
2339 (setq rmail-summary-vector
2340 (vconcat rmail-summary-vector (make-vector total-messages nil)))
2341 (setq rmail-msgref-vector
2342 (vconcat rmail-msgref-vector (make-vector total-messages nil)))
2343 ;; Fill in the new elements of rmail-msgref-vector.
2344 (let ((i (1+ (- rmail-total-messages total-messages))))
2345 (while (<= i rmail-total-messages)
2346 (aset rmail-msgref-vector i (list i))
2347 (setq i (1+ i))))
2348 (goto-char (point-min))
2349 (or nomsg (message "Counting new messages...done (%d)" total-messages))))
2350
2351 (defun rmail-set-message-counters ()
2352 (rmail-forget-messages)
2353 (save-excursion
2354 (save-restriction
2355 (widen)
2356 (let* ((point-save (point))
2357 (total-messages 0)
2358 (messages-after-point)
2359 (case-fold-search nil)
2360 (messages-head nil)
2361 (deleted-head nil))
2362 (message "Counting messages...")
2363 (goto-char (point-max))
2364 ;; Put at the end of messages-head
2365 ;; the entry for message N+1, which marks
2366 ;; the end of message N. (N = number of messages).
2367 (search-backward "\n\^_" nil t)
2368 (if (/= (point) (point-max)) (forward-char 1))
2369 (setq messages-head (list (point-marker)))
2370 (rmail-set-message-counters-counter (min (point) point-save))
2371 (setq messages-after-point total-messages)
2372 (rmail-set-message-counters-counter)
2373 (setq rmail-total-messages total-messages)
2374 (setq rmail-current-message
2375 (min total-messages
2376 (max 1 (- total-messages messages-after-point))))
2377 (setq rmail-message-vector
2378 (apply 'vector (cons (point-min-marker) messages-head))
2379 rmail-deleted-vector (concat "0" deleted-head)
2380 rmail-summary-vector (make-vector rmail-total-messages nil)
2381 rmail-msgref-vector (make-vector (1+ rmail-total-messages) nil))
2382 (let ((i 0))
2383 (while (<= i rmail-total-messages)
2384 (aset rmail-msgref-vector i (list i))
2385 (setq i (1+ i))))
2386 (message "Counting messages...done")))))
2387
2388 (defun rmail-set-message-counters-counter (&optional stop)
2389 (let ((start (point))
2390 next)
2391 (while (search-backward "\n\^_\^L" stop t)
2392 ;; Detect messages that have been added with DOS line endings and
2393 ;; convert the line endings for such messages.
2394 (setq next (point))
2395 (if (looking-at "\n\^_\^L\r\n")
2396 (let ((buffer-read-only nil)
2397 (buffer-undo t))
2398 (message "Counting messages...(converting line endings)")
2399 (save-excursion
2400 (goto-char start)
2401 (while (search-backward "\r\n" next t)
2402 (delete-char 1)))))
2403 (setq start next)
2404 (forward-char 1)
2405 (setq messages-head (cons (point-marker) messages-head))
2406 (save-excursion
2407 (setq deleted-head
2408 (cons (if (search-backward ", deleted,"
2409 (prog1 (point)
2410 (forward-line 2))
2411 t)
2412 ?D ?\ )
2413 deleted-head)))
2414 (if (zerop (% (setq total-messages (1+ total-messages)) 20))
2415 (message "Counting messages...%d" total-messages)))))
2416
2417 (defun rmail-beginning-of-message ()
2418 "Show current message starting from the beginning."
2419 (interactive)
2420 (rmail-show-message rmail-current-message))
2421
2422 (defun rmail-show-message (&optional n no-summary)
2423 "Show message number N (prefix argument), counting from start of file.
2424 If summary buffer is currently displayed, update current message there also."
2425 (interactive "p")
2426 (or (eq major-mode 'rmail-mode)
2427 (switch-to-buffer rmail-buffer))
2428 (rmail-maybe-set-message-counters)
2429 (widen)
2430 (if (zerop rmail-total-messages)
2431 (progn (narrow-to-region (point-min) (1- (point-max)))
2432 (goto-char (point-min))
2433 (setq mode-line-process nil))
2434 (let (blurb coding-system)
2435 (if (not n)
2436 (setq n rmail-current-message)
2437 (cond ((<= n 0)
2438 (setq n 1
2439 rmail-current-message 1
2440 blurb "No previous message"))
2441 ((> n rmail-total-messages)
2442 (setq n rmail-total-messages
2443 rmail-current-message rmail-total-messages
2444 blurb "No following message"))
2445 (t
2446 (setq rmail-current-message n))))
2447 (let ((beg (rmail-msgbeg n)))
2448 (goto-char beg)
2449 (forward-line 1)
2450 (save-excursion
2451 (let ((end (rmail-msgend n)))
2452 (save-restriction
2453 (if (prog1 (= (following-char) ?0)
2454 (forward-line 2)
2455 ;; If there's a Summary-line in the (otherwise empty)
2456 ;; header, we didn't yet get past the EOOH line.
2457 (if (looking-at "^\\*\\*\\* EOOH \\*\\*\\*\n")
2458 (forward-line 1))
2459 (narrow-to-region (point) end))
2460 (rfc822-goto-eoh)
2461 (search-forward "\n*** EOOH ***\n" end t))
2462 (narrow-to-region beg (point))
2463 (goto-char (point-min))
2464 (if (re-search-forward "^X-Coding-System: *\\(.*\\)$" nil t)
2465 (let ((coding-system (intern (match-string 1))))
2466 (condition-case nil
2467 (progn
2468 (check-coding-system coding-system)
2469 (setq buffer-file-coding-system coding-system))
2470 (error
2471 (setq buffer-file-coding-system nil))))
2472 (setq buffer-file-coding-system nil)))))
2473 ;; Clear the "unseen" attribute when we show a message.
2474 (rmail-set-attribute "unseen" nil)
2475 (let ((end (rmail-msgend n)))
2476 ;; Reformat the header, or else find the reformatted header.
2477 (if (= (following-char) ?0)
2478 (rmail-reformat-message beg end)
2479 (search-forward "\n*** EOOH ***\n" end t)
2480 (narrow-to-region (point) end)))
2481 (goto-char (point-min))
2482 (walk-windows
2483 (function (lambda (window)
2484 (if (eq (window-buffer window) (current-buffer))
2485 (set-window-point window (point)))))
2486 nil t)
2487 (rmail-display-labels)
2488 (if (eq rmail-enable-mime t)
2489 (funcall rmail-show-mime-function)
2490 (setq rmail-view-buffer rmail-buffer)
2491 )
2492 (rmail-highlight-headers)
2493 (if transient-mark-mode (deactivate-mark))
2494 (run-hooks 'rmail-show-message-hook)
2495 ;; If there is a summary buffer, try to move to this message
2496 ;; in that buffer. But don't complain if this message
2497 ;; is not mentioned in the summary.
2498 ;; Don't do this at all if we were called on behalf
2499 ;; of cursor motion in the summary buffer.
2500 (and (rmail-summary-exists) (not no-summary)
2501 (let ((curr-msg rmail-current-message))
2502 (rmail-select-summary
2503 (rmail-summary-goto-msg curr-msg t t))))
2504 (with-current-buffer rmail-buffer
2505 (rmail-auto-file))
2506 (if blurb
2507 (message blurb))))))
2508
2509 (defun rmail-redecode-body (coding)
2510 "Decode the body of the current message using coding system CODING.
2511 This is useful with mail messages that have malformed or missing
2512 charset= headers.
2513
2514 This function assumes that the current message is already decoded
2515 and displayed in the RMAIL buffer, but the coding system used to
2516 decode it was incorrect. It then encodes the message back to its
2517 original form, and decodes it again, using the coding system CODING.
2518
2519 Note that if Emacs erroneously auto-detected one of the iso-2022
2520 encodings in the message, this function might fail because the escape
2521 sequences that switch between character sets and also single-shift and
2522 locking-shift codes are impossible to recover. This function is meant
2523 to be used to fix messages encoded with 8-bit encodings, such as
2524 iso-8859, koi8-r, etc."
2525 (interactive "zCoding system for re-decoding this message: ")
2526 (when (not rmail-enable-mime)
2527 (or (eq major-mode 'rmail-mode)
2528 (switch-to-buffer rmail-buffer))
2529 (save-excursion
2530 (let ((pruned (rmail-msg-is-pruned)))
2531 (unwind-protect
2532 (let ((msgbeg (rmail-msgbeg rmail-current-message))
2533 (msgend (rmail-msgend rmail-current-message))
2534 x-coding-header)
2535 ;; We need the message headers pruned (we later restore
2536 ;; the pruned stat to what it was, see the end of
2537 ;; unwind-protect form).
2538 (or pruned
2539 (rmail-toggle-header 1))
2540 (narrow-to-region msgbeg msgend)
2541 (goto-char (point-min))
2542 (when (search-forward "\n*** EOOH ***\n" (point-max) t)
2543 (narrow-to-region msgbeg (point)))
2544 (goto-char (point-min))
2545 (if (re-search-forward "^X-Coding-System: *\\(.*\\)$" nil t)
2546 (let ((old-coding (intern (match-string 1)))
2547 (buffer-read-only nil))
2548 (check-coding-system old-coding)
2549 ;; Make sure the new coding system uses the same EOL
2550 ;; conversion, to prevent ^M characters from popping
2551 ;; up all over the place.
2552 (setq coding
2553 (coding-system-change-eol-conversion
2554 coding
2555 (coding-system-eol-type old-coding)))
2556 (setq x-coding-header (point-marker))
2557 (narrow-to-region msgbeg msgend)
2558 (encode-coding-region (point) msgend old-coding)
2559 (decode-coding-region (point) msgend coding)
2560 (setq last-coding-system-used coding)
2561 ;; Rewrite the coding-system header according
2562 ;; to what we did.
2563 (goto-char x-coding-header)
2564 (delete-region (point)
2565 (save-excursion
2566 (beginning-of-line)
2567 (point)))
2568 (insert "X-Coding-System: "
2569 (symbol-name last-coding-system-used))
2570 (set-marker x-coding-header nil)
2571 (rmail-show-message))
2572 (error "No X-Coding-System header found")))
2573 (or pruned
2574 (rmail-toggle-header 0)))))))
2575
2576 ;; Find all occurrences of certain fields, and highlight them.
2577 (defun rmail-highlight-headers ()
2578 ;; Do this only if the system supports faces.
2579 (if (and (fboundp 'internal-find-face)
2580 rmail-highlighted-headers)
2581 (save-excursion
2582 (search-forward "\n\n" nil 'move)
2583 (save-restriction
2584 (narrow-to-region (point-min) (point))
2585 (let ((case-fold-search t)
2586 (inhibit-read-only t)
2587 ;; Highlight with boldface if that is available.
2588 ;; Otherwise use the `highlight' face.
2589 (face (or rmail-highlight-face
2590 (if (face-differs-from-default-p 'bold)
2591 'bold 'highlight)))
2592 ;; List of overlays to reuse.
2593 (overlays rmail-overlay-list))
2594 (goto-char (point-min))
2595 (while (re-search-forward rmail-highlighted-headers nil t)
2596 (skip-chars-forward " \t")
2597 (let ((beg (point))
2598 overlay)
2599 (while (progn (forward-line 1)
2600 (looking-at "[ \t]")))
2601 ;; Back up over newline, then trailing spaces or tabs
2602 (forward-char -1)
2603 (while (member (preceding-char) '(? ?\t))
2604 (forward-char -1))
2605 (if overlays
2606 ;; Reuse an overlay we already have.
2607 (progn
2608 (setq overlay (car overlays)
2609 overlays (cdr overlays))
2610 (overlay-put overlay 'face face)
2611 (move-overlay overlay beg (point)))
2612 ;; Make a new overlay and add it to
2613 ;; rmail-overlay-list.
2614 (setq overlay (make-overlay beg (point)))
2615 (overlay-put overlay 'face face)
2616 (setq rmail-overlay-list
2617 (cons overlay rmail-overlay-list))))))))))
2618
2619 (defun rmail-auto-file ()
2620 "Automatically move a message into a sub-folder based on criteria.
2621 Called when a new message is displayed."
2622 (if (or (rmail-message-labels-p rmail-current-message "filed")
2623 (not (string= (buffer-file-name)
2624 (expand-file-name rmail-file-name))))
2625 ;; Do nothing if it's already been filed.
2626 nil
2627 ;; Find out some basics (common fields)
2628 (let ((from (mail-fetch-field "from"))
2629 (subj (mail-fetch-field "subject"))
2630 (to (concat (mail-fetch-field "to") "," (mail-fetch-field "cc")))
2631 (d rmail-automatic-folder-directives)
2632 (directive-loop nil)
2633 (folder nil))
2634 (while d
2635 (setq folder (car (car d))
2636 directive-loop (cdr (car d)))
2637 (while (and (car directive-loop)
2638 (let ((f (cond
2639 ((string= (car directive-loop) "from") from)
2640 ((string= (car directive-loop) "to") to)
2641 ((string= (car directive-loop) "subject") subj)
2642 (t (mail-fetch-field (car directive-loop))))))
2643 (and f (string-match (car (cdr directive-loop)) f))))
2644 (setq directive-loop (cdr (cdr directive-loop))))
2645 ;; If there are no directives left, then it was a complete match.
2646 (if (null directive-loop)
2647 (if (null folder)
2648 (rmail-delete-forward)
2649 (if (string= "/dev/null" folder)
2650 (rmail-delete-message)
2651 (rmail-output-to-rmail-file folder 1 t)
2652 (setq d nil))))
2653 (setq d (cdr d))))))
2654
2655 (defun rmail-next-message (n)
2656 "Show following message whether deleted or not.
2657 With prefix arg N, moves forward N messages, or backward if N is negative."
2658 (interactive "p")
2659 (set-buffer rmail-buffer)
2660 (rmail-maybe-set-message-counters)
2661 (rmail-show-message (+ rmail-current-message n)))
2662
2663 (defun rmail-previous-message (n)
2664 "Show previous message whether deleted or not.
2665 With prefix arg N, moves backward N messages, or forward if N is negative."
2666 (interactive "p")
2667 (rmail-next-message (- n)))
2668
2669 (defun rmail-next-undeleted-message (n)
2670 "Show following non-deleted message.
2671 With prefix arg N, moves forward N non-deleted messages,
2672 or backward if N is negative.
2673
2674 Returns t if a new message is being shown, nil otherwise."
2675 (interactive "p")
2676 (set-buffer rmail-buffer)
2677 (rmail-maybe-set-message-counters)
2678 (let ((lastwin rmail-current-message)
2679 (current rmail-current-message))
2680 (while (and (> n 0) (< current rmail-total-messages))
2681 (setq current (1+ current))
2682 (if (not (rmail-message-deleted-p current))
2683 (setq lastwin current n (1- n))))
2684 (while (and (< n 0) (> current 1))
2685 (setq current (1- current))
2686 (if (not (rmail-message-deleted-p current))
2687 (setq lastwin current n (1+ n))))
2688 (if (/= lastwin rmail-current-message)
2689 (progn (rmail-show-message lastwin)
2690 t)
2691 (if (< n 0)
2692 (message "No previous nondeleted message"))
2693 (if (> n 0)
2694 (message "No following nondeleted message"))
2695 nil)))
2696
2697 (defun rmail-previous-undeleted-message (n)
2698 "Show previous non-deleted message.
2699 With prefix argument N, moves backward N non-deleted messages,
2700 or forward if N is negative."
2701 (interactive "p")
2702 (rmail-next-undeleted-message (- n)))
2703
2704 (defun rmail-first-message ()
2705 "Show first message in file."
2706 (interactive)
2707 (rmail-maybe-set-message-counters)
2708 (rmail-show-message 1))
2709
2710 (defun rmail-last-message ()
2711 "Show last message in file."
2712 (interactive)
2713 (rmail-maybe-set-message-counters)
2714 (rmail-show-message rmail-total-messages))
2715
2716 (defun rmail-what-message ()
2717 (let ((where (point))
2718 (low 1)
2719 (high rmail-total-messages)
2720 (mid (/ rmail-total-messages 2)))
2721 (while (> (- high low) 1)
2722 (if (>= where (rmail-msgbeg mid))
2723 (setq low mid)
2724 (setq high mid))
2725 (setq mid (+ low (/ (- high low) 2))))
2726 (if (>= where (rmail-msgbeg high)) high low)))
2727
2728 (defun rmail-message-recipients-p (msg recipients &optional primary-only)
2729 (save-restriction
2730 (goto-char (rmail-msgbeg msg))
2731 (search-forward "\n*** EOOH ***\n")
2732 (narrow-to-region (point) (progn (search-forward "\n\n") (point)))
2733 (or (string-match recipients (or (mail-fetch-field "To") ""))
2734 (string-match recipients (or (mail-fetch-field "From") ""))
2735 (if (not primary-only)
2736 (string-match recipients (or (mail-fetch-field "Cc") ""))))))
2737
2738 (defun rmail-message-regexp-p (n regexp)
2739 "Return t, if for message number N, regexp REGEXP matches in the header."
2740 (let ((beg (rmail-msgbeg n))
2741 (end (rmail-msgend n)))
2742 (goto-char beg)
2743 (forward-line 1)
2744 (save-excursion
2745 (save-restriction
2746 (if (prog1 (= (following-char) ?0)
2747 (forward-line 2)
2748 ;; If there's a Summary-line in the (otherwise empty)
2749 ;; header, we didn't yet get past the EOOH line.
2750 (when (looking-at "^\\*\\*\\* EOOH \\*\\*\\*\n")
2751 (forward-line 1))
2752 (setq beg (point))
2753 (narrow-to-region (point) end))
2754 (progn
2755 (rfc822-goto-eoh)
2756 (setq end (point)))
2757 (setq beg (point))
2758 (search-forward "\n*** EOOH ***\n" end t)
2759 (setq end (1+ (match-beginning 0)))))
2760 (goto-char beg)
2761 (if rmail-enable-mime
2762 (funcall rmail-search-mime-header-function n regexp end)
2763 (re-search-forward regexp end t)))))
2764
2765 (defun rmail-search-message (msg regexp)
2766 "Return non-nil, if for message number MSG, regexp REGEXP matches."
2767 (goto-char (rmail-msgbeg msg))
2768 (if rmail-enable-mime
2769 (funcall rmail-search-mime-message-function msg regexp)
2770 (re-search-forward regexp (rmail-msgend msg) t)))
2771
2772 (defvar rmail-search-last-regexp nil)
2773 (defun rmail-search (regexp &optional n)
2774 "Show message containing next match for REGEXP (but not the current msg).
2775 Prefix argument gives repeat count; negative argument means search
2776 backwards (through earlier messages).
2777 Interactively, empty argument means use same regexp used last time."
2778 (interactive
2779 (let* ((reversep (< (prefix-numeric-value current-prefix-arg) 0))
2780 (prompt
2781 (concat (if reversep "Reverse " "") "Rmail search (regexp): "))
2782 regexp)
2783 (if rmail-search-last-regexp
2784 (setq prompt (concat prompt
2785 "(default "
2786 rmail-search-last-regexp
2787 ") ")))
2788 (setq regexp (read-string prompt))
2789 (cond ((not (equal regexp ""))
2790 (setq rmail-search-last-regexp regexp))
2791 ((not rmail-search-last-regexp)
2792 (error "No previous Rmail search string")))
2793 (list rmail-search-last-regexp
2794 (prefix-numeric-value current-prefix-arg))))
2795 (or n (setq n 1))
2796 (message "%sRmail search for %s..."
2797 (if (< n 0) "Reverse " "")
2798 regexp)
2799 (set-buffer rmail-buffer)
2800 (rmail-maybe-set-message-counters)
2801 (let ((omin (point-min))
2802 (omax (point-max))
2803 (opoint (point))
2804 win
2805 (reversep (< n 0))
2806 (msg rmail-current-message))
2807 (unwind-protect
2808 (progn
2809 (widen)
2810 (while (/= n 0)
2811 ;; Check messages one by one, advancing message number up or down
2812 ;; but searching forward through each message.
2813 (if reversep
2814 (while (and (null win) (> msg 1))
2815 (setq msg (1- msg)
2816 win (rmail-search-message msg regexp)))
2817 (while (and (null win) (< msg rmail-total-messages))
2818 (setq msg (1+ msg)
2819 win (rmail-search-message msg regexp))))
2820 (setq n (+ n (if reversep 1 -1)))))
2821 (if win
2822 (progn
2823 (rmail-show-message msg)
2824 ;; Search forward (if this is a normal search) or backward
2825 ;; (if this is a reverse search) through this message to
2826 ;; position point. This search may fail because REGEXP
2827 ;; was found in the hidden portion of this message. In
2828 ;; that case, move point to the beginning of visible
2829 ;; portion.
2830 (if reversep
2831 (progn
2832 (goto-char (point-max))
2833 (re-search-backward regexp nil 'move))
2834 (goto-char (point-min))
2835 (re-search-forward regexp nil t))
2836 (message "%sRmail search for %s...done"
2837 (if reversep "Reverse " "")
2838 regexp))
2839 (goto-char opoint)
2840 (narrow-to-region omin omax)
2841 (ding)
2842 (message "Search failed: %s" regexp)))))
2843
2844 (defun rmail-search-backwards (regexp &optional n)
2845 "Show message containing previous match for REGEXP.
2846 Prefix argument gives repeat count; negative argument means search
2847 forward (through later messages).
2848 Interactively, empty argument means use same regexp used last time."
2849 (interactive
2850 (let* ((reversep (>= (prefix-numeric-value current-prefix-arg) 0))
2851 (prompt
2852 (concat (if reversep "Reverse " "") "Rmail search (regexp): "))
2853 regexp)
2854 (if rmail-search-last-regexp
2855 (setq prompt (concat prompt
2856 "(default "
2857 rmail-search-last-regexp
2858 ") ")))
2859 (setq regexp (read-string prompt))
2860 (cond ((not (equal regexp ""))
2861 (setq rmail-search-last-regexp regexp))
2862 ((not rmail-search-last-regexp)
2863 (error "No previous Rmail search string")))
2864 (list rmail-search-last-regexp
2865 (prefix-numeric-value current-prefix-arg))))
2866 (rmail-search regexp (- (or n 1))))
2867
2868 ;; Show the first message which has the `unseen' attribute.
2869 (defun rmail-first-unseen-message ()
2870 (rmail-maybe-set-message-counters)
2871 (let ((current 1)
2872 found)
2873 (save-restriction
2874 (widen)
2875 (while (and (not found) (<= current rmail-total-messages))
2876 (if (rmail-message-labels-p current ", ?\\(unseen\\),")
2877 (setq found current))
2878 (setq current (1+ current))))
2879 ;; Let the caller show the message.
2880 ;; (if found
2881 ;; (rmail-show-message found))
2882 found))
2883
2884 (defun rmail-next-same-subject (n)
2885 "Go to the next mail message having the same subject header.
2886 With prefix argument N, do this N times.
2887 If N is negative, go backwards instead."
2888 (interactive "p")
2889 (let ((subject (mail-fetch-field "Subject"))
2890 (forward (> n 0))
2891 (i rmail-current-message)
2892 (case-fold-search t)
2893 search-regexp found)
2894 (if (string-match "\\`[ \t]+" subject)
2895 (setq subject (substring subject (match-end 0))))
2896 (if (string-match "Re:[ \t]*" subject)
2897 (setq subject (substring subject (match-end 0))))
2898 (if (string-match "[ \t]+\\'" subject)
2899 (setq subject (substring subject 0 (match-beginning 0))))
2900 (setq search-regexp (concat "^Subject: *\\(Re:[ \t]*\\)?"
2901 (regexp-quote subject)
2902 "[ \t]*\n"))
2903 (save-excursion
2904 (save-restriction
2905 (widen)
2906 (while (and (/= n 0)
2907 (if forward
2908 (< i rmail-total-messages)
2909 (> i 1)))
2910 (let (done)
2911 (while (and (not done)
2912 (if forward
2913 (< i rmail-total-messages)
2914 (> i 1)))
2915 (setq i (if forward (1+ i) (1- i)))
2916 (goto-char (rmail-msgbeg i))
2917 (search-forward "\n*** EOOH ***\n")
2918 (let ((beg (point)) end)
2919 (search-forward "\n\n")
2920 (setq end (point))
2921 (goto-char beg)
2922 (setq done (re-search-forward search-regexp end t))))
2923 (if done (setq found i)))
2924 (setq n (if forward (1- n) (1+ n))))))
2925 (if found
2926 (rmail-show-message found)
2927 (error "No %s message with same subject"
2928 (if forward "following" "previous")))))
2929
2930 (defun rmail-previous-same-subject (n)
2931 "Go to the previous mail message having the same subject header.
2932 With prefix argument N, do this N times.
2933 If N is negative, go forwards instead."
2934 (interactive "p")
2935 (rmail-next-same-subject (- n)))
2936 \f
2937 ;;;; *** Rmail Message Deletion Commands ***
2938
2939 (defun rmail-message-deleted-p (n)
2940 (= (aref rmail-deleted-vector n) ?D))
2941
2942 (defun rmail-set-message-deleted-p (n state)
2943 (aset rmail-deleted-vector n (if state ?D ?\ )))
2944
2945 (defun rmail-delete-message ()
2946 "Delete this message and stay on it."
2947 (interactive)
2948 (rmail-set-attribute "deleted" t)
2949 (run-hooks 'rmail-delete-message-hook))
2950
2951 (defun rmail-undelete-previous-message ()
2952 "Back up to deleted message, select it, and undelete it."
2953 (interactive)
2954 (set-buffer rmail-buffer)
2955 (let ((msg rmail-current-message))
2956 (while (and (> msg 0)
2957 (not (rmail-message-deleted-p msg)))
2958 (setq msg (1- msg)))
2959 (if (= msg 0)
2960 (error "No previous deleted message")
2961 (if (/= msg rmail-current-message)
2962 (rmail-show-message msg))
2963 (rmail-set-attribute "deleted" nil)
2964 (if (rmail-summary-exists)
2965 (save-excursion
2966 (set-buffer rmail-summary-buffer)
2967 (rmail-summary-mark-undeleted msg)))
2968 (rmail-maybe-display-summary))))
2969
2970 (defun rmail-delete-forward (&optional backward)
2971 "Delete this message and move to next nondeleted one.
2972 Deleted messages stay in the file until the \\[rmail-expunge] command is given.
2973 With prefix argument, delete and move backward.
2974
2975 Returns t if a new message is displayed after the delete, or nil otherwise."
2976 (interactive "P")
2977 (rmail-set-attribute "deleted" t)
2978 (run-hooks 'rmail-delete-message-hook)
2979 (let ((del-msg rmail-current-message))
2980 (if (rmail-summary-exists)
2981 (rmail-select-summary
2982 (rmail-summary-mark-deleted del-msg)))
2983 (prog1 (rmail-next-undeleted-message (if backward -1 1))
2984 (rmail-maybe-display-summary))))
2985
2986 (defun rmail-delete-backward ()
2987 "Delete this message and move to previous nondeleted one.
2988 Deleted messages stay in the file until the \\[rmail-expunge] command is given."
2989 (interactive)
2990 (rmail-delete-forward t))
2991
2992 ;; Compute the message number a given message would have after expunging.
2993 ;; The present number of the message is OLDNUM.
2994 ;; DELETEDVEC should be rmail-deleted-vector.
2995 ;; The value is nil for a message that would be deleted.
2996 (defun rmail-msg-number-after-expunge (deletedvec oldnum)
2997 (if (or (null oldnum) (= (aref deletedvec oldnum) ?D))
2998 nil
2999 (let ((i 0)
3000 (newnum 0))
3001 (while (< i oldnum)
3002 (if (/= (aref deletedvec i) ?D)
3003 (setq newnum (1+ newnum)))
3004 (setq i (1+ i)))
3005 newnum)))
3006
3007 (defun rmail-expunge-confirmed ()
3008 "Return t if deleted message should be expunged. If necessary, ask the user.
3009 See also user-option `rmail-confirm-expunge'."
3010 (set-buffer rmail-buffer)
3011 (or (not (stringp rmail-deleted-vector))
3012 (not (string-match "D" rmail-deleted-vector))
3013 (null rmail-confirm-expunge)
3014 (funcall rmail-confirm-expunge
3015 "Erase deleted messages from Rmail file? ")))
3016
3017 (defun rmail-only-expunge ()
3018 "Actually erase all deleted messages in the file."
3019 (interactive)
3020 (set-buffer rmail-buffer)
3021 (message "Expunging deleted messages...")
3022 ;; Discard all undo records for this buffer.
3023 (or (eq buffer-undo-list t)
3024 (setq buffer-undo-list nil))
3025 (rmail-maybe-set-message-counters)
3026 (let* ((omax (- (buffer-size) (point-max)))
3027 (omin (- (buffer-size) (point-min)))
3028 (opoint (if (and (> rmail-current-message 0)
3029 (rmail-message-deleted-p rmail-current-message))
3030 0
3031 (if rmail-enable-mime
3032 (with-current-buffer rmail-view-buffer
3033 (- (point)(point-min)))
3034 (- (point) (point-min)))))
3035 (messages-head (cons (aref rmail-message-vector 0) nil))
3036 (messages-tail messages-head)
3037 ;; Don't make any undo records for the expunging.
3038 (buffer-undo-list t)
3039 (win))
3040 (unwind-protect
3041 (save-excursion
3042 (widen)
3043 (goto-char (point-min))
3044 (let ((counter 0)
3045 (number 1)
3046 (total rmail-total-messages)
3047 (new-message-number rmail-current-message)
3048 (new-summary nil)
3049 (new-msgref (list (list 0)))
3050 (rmailbuf (current-buffer))
3051 (buffer-read-only nil)
3052 (messages rmail-message-vector)
3053 (deleted rmail-deleted-vector)
3054 (summary rmail-summary-vector))
3055 (setq rmail-total-messages nil
3056 rmail-current-message nil
3057 rmail-message-vector nil
3058 rmail-deleted-vector nil
3059 rmail-summary-vector nil)
3060
3061 (while (<= number total)
3062 (if (= (aref deleted number) ?D)
3063 (progn
3064 (delete-region
3065 (marker-position (aref messages number))
3066 (marker-position (aref messages (1+ number))))
3067 (move-marker (aref messages number) nil)
3068 (if (> new-message-number counter)
3069 (setq new-message-number (1- new-message-number))))
3070 (setq counter (1+ counter))
3071 (setq messages-tail
3072 (setcdr messages-tail
3073 (cons (aref messages number) nil)))
3074 (setq new-summary
3075 (cons (if (= counter number) (aref summary (1- number)))
3076 new-summary))
3077 (setq new-msgref
3078 (cons (aref rmail-msgref-vector number)
3079 new-msgref))
3080 (setcar (car new-msgref) counter))
3081 (if (zerop (% (setq number (1+ number)) 20))
3082 (message "Expunging deleted messages...%d" number)))
3083 (setq messages-tail
3084 (setcdr messages-tail
3085 (cons (aref messages number) nil)))
3086 (setq rmail-current-message new-message-number
3087 rmail-total-messages counter
3088 rmail-message-vector (apply 'vector messages-head)
3089 rmail-deleted-vector (make-string (1+ counter) ?\ )
3090 rmail-summary-vector (vconcat (nreverse new-summary))
3091 rmail-msgref-vector (apply 'vector (nreverse new-msgref))
3092 win t)))
3093 (message "Expunging deleted messages...done")
3094 (if (not win)
3095 (narrow-to-region (- (buffer-size) omin) (- (buffer-size) omax)))
3096 (rmail-show-message
3097 (if (zerop rmail-current-message) 1 nil))
3098 (if rmail-enable-mime
3099 (goto-char (+ (point-min) opoint))
3100 (goto-char (+ (point) opoint))))))
3101
3102 (defun rmail-expunge ()
3103 "Erase deleted messages from Rmail file and summary buffer."
3104 (interactive)
3105 (when (rmail-expunge-confirmed)
3106 (rmail-only-expunge)
3107 (if (rmail-summary-exists)
3108 (rmail-select-summary (rmail-update-summary)))))
3109 \f
3110 ;;;; *** Rmail Mailing Commands ***
3111
3112 (defun rmail-start-mail (&optional noerase to subject in-reply-to cc
3113 replybuffer sendactions same-window others)
3114 (let (yank-action)
3115 (if replybuffer
3116 (setq yank-action (list 'insert-buffer replybuffer)))
3117 (setq others (cons (cons "cc" cc) others))
3118 (setq others (cons (cons "in-reply-to" in-reply-to) others))
3119 (if same-window
3120 (compose-mail to subject others
3121 noerase nil
3122 yank-action sendactions)
3123 (if (and (display-multi-frame-p) rmail-mail-new-frame)
3124 (prog1
3125 (compose-mail to subject others
3126 noerase 'switch-to-buffer-other-frame
3127 yank-action sendactions)
3128 ;; This is not a standard frame parameter;
3129 ;; nothing except sendmail.el looks at it.
3130 (modify-frame-parameters (selected-frame)
3131 '((mail-dedicated-frame . t))))
3132 (compose-mail to subject others
3133 noerase 'switch-to-buffer-other-window
3134 yank-action sendactions)))))
3135
3136 (defun rmail-mail ()
3137 "Send mail in another window.
3138 While composing the message, use \\[mail-yank-original] to yank the
3139 original message into it."
3140 (interactive)
3141 (rmail-start-mail nil nil nil nil nil rmail-view-buffer))
3142
3143 (defun rmail-continue ()
3144 "Continue composing outgoing message previously being composed."
3145 (interactive)
3146 (rmail-start-mail t))
3147
3148 (defun rmail-reply (just-sender)
3149 "Reply to the current message.
3150 Normally include CC: to all other recipients of original message;
3151 prefix argument means ignore them. While composing the reply,
3152 use \\[mail-yank-original] to yank the original message into it."
3153 (interactive "P")
3154 (let (from reply-to cc subject date to message-id references
3155 resent-to resent-cc resent-reply-to
3156 (msgnum rmail-current-message))
3157 (save-excursion
3158 (save-restriction
3159 (if rmail-enable-mime
3160 (narrow-to-region
3161 (goto-char (point-min))
3162 (if (search-forward "\n\n" nil 'move)
3163 (1+ (match-beginning 0))
3164 (point)))
3165 (widen)
3166 (goto-char (rmail-msgbeg rmail-current-message))
3167 (forward-line 1)
3168 (if (= (following-char) ?0)
3169 (narrow-to-region
3170 (progn (forward-line 2)
3171 (point))
3172 (progn (search-forward "\n\n" (rmail-msgend rmail-current-message)
3173 'move)
3174 (point)))
3175 (narrow-to-region (point)
3176 (progn (search-forward "\n*** EOOH ***\n")
3177 (beginning-of-line) (point)))))
3178 (setq from (mail-fetch-field "from")
3179 reply-to (or (mail-fetch-field "reply-to" nil t)
3180 from)
3181 cc (and (not just-sender)
3182 (mail-fetch-field "cc" nil t))
3183 subject (mail-fetch-field "subject")
3184 date (mail-fetch-field "date")
3185 to (or (mail-fetch-field "to" nil t) "")
3186 message-id (mail-fetch-field "message-id")
3187 references (mail-fetch-field "references" nil nil t)
3188 resent-reply-to (mail-fetch-field "resent-reply-to" nil t)
3189 resent-cc (and (not just-sender)
3190 (mail-fetch-field "resent-cc" nil t))
3191 resent-to (or (mail-fetch-field "resent-to" nil t) "")
3192 ;;; resent-subject (mail-fetch-field "resent-subject")
3193 ;;; resent-date (mail-fetch-field "resent-date")
3194 ;;; resent-message-id (mail-fetch-field "resent-message-id")
3195 )))
3196 ;; Merge the resent-to and resent-cc into the to and cc.
3197 (if (and resent-to (not (equal resent-to "")))
3198 (if (not (equal to ""))
3199 (setq to (concat to ", " resent-to))
3200 (setq to resent-to)))
3201 (if (and resent-cc (not (equal resent-cc "")))
3202 (if (not (equal cc ""))
3203 (setq cc (concat cc ", " resent-cc))
3204 (setq cc resent-cc)))
3205 ;; Add `Re: ' to subject if not there already.
3206 (and (stringp subject)
3207 (setq subject
3208 (concat rmail-reply-prefix
3209 (if (let ((case-fold-search t))
3210 (string-match rmail-reply-regexp subject))
3211 (substring subject (match-end 0))
3212 subject))))
3213 (rmail-start-mail
3214 nil
3215 ;; Using mail-strip-quoted-names is undesirable with newer mailers
3216 ;; since they can handle the names unstripped.
3217 ;; I don't know whether there are other mailers that still
3218 ;; need the names to be stripped.
3219 ;;; (mail-strip-quoted-names reply-to)
3220 reply-to
3221 subject
3222 (rmail-make-in-reply-to-field from date message-id)
3223 (if just-sender
3224 nil
3225 ;; mail-strip-quoted-names is NOT necessary for rmail-dont-reply-to
3226 ;; to do its job.
3227 (let* ((cc-list (rmail-dont-reply-to
3228 (mail-strip-quoted-names
3229 (if (null cc) to (concat to ", " cc))))))
3230 (if (string= cc-list "") nil cc-list)))
3231 rmail-view-buffer
3232 (list (list 'rmail-mark-message
3233 rmail-buffer
3234 (with-current-buffer rmail-buffer
3235 (aref rmail-msgref-vector msgnum))
3236 "answered"))
3237 nil
3238 (list (cons "References" (concat (mapconcat 'identity references " ")
3239 " " message-id))))))
3240
3241 (defun rmail-mark-message (buffer msgnum-list attribute)
3242 "Give BUFFER's message number in MSGNUM-LIST the attribute ATTRIBUTE.
3243 This is use in the send-actions for message buffers.
3244 MSGNUM-LIST is a list of the form (MSGNUM)
3245 which is an element of rmail-msgref-vector."
3246 (save-excursion
3247 (set-buffer buffer)
3248 (if (car msgnum-list)
3249 (rmail-set-attribute attribute t (car msgnum-list)))))
3250
3251 (defun rmail-make-in-reply-to-field (from date message-id)
3252 (cond ((not from)
3253 (if message-id
3254 message-id
3255 nil))
3256 (mail-use-rfc822
3257 (require 'rfc822)
3258 (let ((tem (car (rfc822-addresses from))))
3259 (if message-id
3260 (if (or (not tem)
3261 (string-match
3262 (regexp-quote (if (string-match "@[^@]*\\'" tem)
3263 (substring tem 0
3264 (match-beginning 0))
3265 tem))
3266 message-id))
3267 ;; missing From, or Message-ID is sufficiently informative
3268 message-id
3269 (concat message-id " (" tem ")"))
3270 ;; Copy TEM, discarding text properties.
3271 (setq tem (copy-sequence tem))
3272 (set-text-properties 0 (length tem) nil tem)
3273 (setq tem (copy-sequence tem))
3274 ;; Use prin1 to fake RFC822 quoting
3275 (let ((field (prin1-to-string tem)))
3276 (if date
3277 (concat field "'s message of " date)
3278 field)))))
3279 ((let* ((foo "[^][\000-\037()<>@,;:\\\" ]+")
3280 (bar "[^][\000-\037()<>@,;:\\\"]+"))
3281 ;; These strings both match all non-ASCII characters.
3282 (or (string-match (concat "\\`[ \t]*\\(" bar
3283 "\\)\\(<" foo "@" foo ">\\)?[ \t]*\\'")
3284 ;; "Unix Loser <Foo@bar.edu>" => "Unix Loser"
3285 from)
3286 (string-match (concat "\\`[ \t]*<" foo "@" foo ">[ \t]*(\\("
3287 bar "\\))[ \t]*\\'")
3288 ;; "<Bugs@bar.edu>" (Losing Unix) => "Losing Unix"
3289 from)))
3290 (let ((start (match-beginning 1))
3291 (end (match-end 1)))
3292 ;; Trim whitespace which above regexp match allows
3293 (while (and (< start end)
3294 (memq (aref from start) '(?\t ?\ )))
3295 (setq start (1+ start)))
3296 (while (and (< start end)
3297 (memq (aref from (1- end)) '(?\t ?\ )))
3298 (setq end (1- end)))
3299 (let ((field (substring from start end)))
3300 (if date (setq field (concat "message from " field " on " date)))
3301 (if message-id
3302 ;; "<AA259@bar.edu> (message from Unix Loser on 1-Apr-89)"
3303 (concat message-id " (" field ")")
3304 field))))
3305 (t
3306 ;; If we can't kludge it simply, do it correctly
3307 (let ((mail-use-rfc822 t))
3308 (rmail-make-in-reply-to-field from date message-id)))))
3309 \f
3310 (defun rmail-forward (resend)
3311 "Forward the current message to another user.
3312 With prefix argument, \"resend\" the message instead of forwarding it;
3313 see the documentation of `rmail-resend'."
3314 (interactive "P")
3315 (if resend
3316 (call-interactively 'rmail-resend)
3317 (let ((forward-buffer rmail-buffer)
3318 (msgnum rmail-current-message)
3319 (subject (concat "["
3320 (let ((from (or (mail-fetch-field "From")
3321 (mail-fetch-field ">From"))))
3322 (if from
3323 (concat (mail-strip-quoted-names from) ": ")
3324 ""))
3325 (or (mail-fetch-field "Subject") "")
3326 "]")))
3327 (if (rmail-start-mail
3328 nil nil subject nil nil nil
3329 (list (list 'rmail-mark-message
3330 forward-buffer
3331 (with-current-buffer rmail-buffer
3332 (aref rmail-msgref-vector msgnum))
3333 "forwarded"))
3334 ;; If only one window, use it for the mail buffer.
3335 ;; Otherwise, use another window for the mail buffer
3336 ;; so that the Rmail buffer remains visible
3337 ;; and sending the mail will get back to it.
3338 (and (not rmail-mail-new-frame) (one-window-p t)))
3339 ;; The mail buffer is now current.
3340 (save-excursion
3341 ;; Insert after header separator--before signature if any.
3342 (goto-char (mail-text-start))
3343 (if (or rmail-enable-mime rmail-enable-mime-composing)
3344 (funcall rmail-insert-mime-forwarded-message-function
3345 forward-buffer)
3346 (insert "------- Start of forwarded message -------\n")
3347 ;; Quote lines with `- ' if they start with `-'.
3348 (let ((beg (point)) end)
3349 (setq end (point-marker))
3350 (set-marker-insertion-type end t)
3351 (insert-buffer-substring forward-buffer)
3352 (goto-char beg)
3353 (while (re-search-forward "^-" end t)
3354 (beginning-of-line)
3355 (insert "- ")
3356 (forward-line 1))
3357 (goto-char end)
3358 (skip-chars-backward "\n")
3359 (if (< (point) end)
3360 (forward-char 1))
3361 (delete-region (point) end)
3362 (set-marker end nil))
3363 (insert "------- End of forwarded message -------\n"))
3364 (push-mark))))))
3365 \f
3366 (defun rmail-resend (address &optional from comment mail-alias-file)
3367 "Resend current message to ADDRESSES.
3368 ADDRESSES should be a single address, a string consisting of several
3369 addresses separated by commas, or a list of addresses.
3370
3371 Optional FROM is the address to resend the message from, and
3372 defaults from the value of `user-mail-address'.
3373 Optional COMMENT is a string to insert as a comment in the resent message.
3374 Optional ALIAS-FILE is alternate aliases file to be used by sendmail,
3375 typically for purposes of moderating a list."
3376 (interactive "sResend to: ")
3377 (require 'sendmail)
3378 (require 'mailalias)
3379 (unless (or (eq rmail-view-buffer (current-buffer))
3380 (eq rmail-buffer (current-buffer)))
3381 (error "Not an Rmail buffer"))
3382 (if (not from) (setq from user-mail-address))
3383 (let ((tembuf (generate-new-buffer " sendmail temp"))
3384 (case-fold-search nil)
3385 (mailbuf rmail-buffer))
3386 (unwind-protect
3387 (with-current-buffer tembuf
3388 ;;>> Copy message into temp buffer
3389 (if rmail-enable-mime
3390 (funcall rmail-insert-mime-resent-message-function mailbuf)
3391 (insert-buffer-substring mailbuf))
3392 (goto-char (point-min))
3393 ;; Delete any Sender field, since that's not specifiable.
3394 ; Only delete Sender fields in the actual header.
3395 (re-search-forward "^$" nil 'move)
3396 ; Using "while" here rather than "if" because some buggy mail
3397 ; software may have inserted multiple Sender fields.
3398 (while (re-search-backward "^Sender:" nil t)
3399 (let (beg)
3400 (setq beg (point))
3401 (forward-line 1)
3402 (while (looking-at "[ \t]")
3403 (forward-line 1))
3404 (delete-region beg (point))))
3405 ; Go back to the beginning of the buffer so the Resent- fields
3406 ; are inserted there.
3407 (goto-char (point-min))
3408 ;;>> Insert resent-from:
3409 (insert "Resent-From: " from "\n")
3410 (insert "Resent-Date: " (mail-rfc822-date) "\n")
3411 ;;>> Insert resent-to: and bcc if need be.
3412 (let ((before (point)))
3413 (if mail-self-blind
3414 (insert "Resent-Bcc: " (user-login-name) "\n"))
3415 (insert "Resent-To: " (if (stringp address)
3416 address
3417 (mapconcat 'identity address ",\n\t"))
3418 "\n")
3419 ;; Expand abbrevs in the recipients.
3420 (save-excursion
3421 (if (featurep 'mailabbrev)
3422 (let ((end (point-marker))
3423 (local-abbrev-table mail-abbrevs)
3424 (old-syntax-table (syntax-table)))
3425 (if (and (not (vectorp mail-abbrevs))
3426 (file-exists-p mail-personal-alias-file))
3427 (build-mail-abbrevs))
3428 (unless mail-abbrev-syntax-table
3429 (mail-abbrev-make-syntax-table))
3430 (set-syntax-table mail-abbrev-syntax-table)
3431 (goto-char before)
3432 (while (and (< (point) end)
3433 (progn (forward-word 1)
3434 (<= (point) end)))
3435 (expand-abbrev))
3436 (set-syntax-table old-syntax-table))
3437 (expand-mail-aliases before (point)))))
3438 ;;>> Set up comment, if any.
3439 (if (and (sequencep comment) (not (zerop (length comment))))
3440 (let ((before (point))
3441 after)
3442 (insert comment)
3443 (or (eolp) (insert "\n"))
3444 (setq after (point))
3445 (goto-char before)
3446 (while (< (point) after)
3447 (insert "Resent-Comment: ")
3448 (forward-line 1))))
3449 ;; Don't expand aliases in the destination fields
3450 ;; of the original message.
3451 (let (mail-aliases)
3452 (funcall send-mail-function)))
3453 (kill-buffer tembuf))
3454 (with-current-buffer rmail-buffer
3455 (rmail-set-attribute "resent" t rmail-current-message))))
3456 \f
3457 (defvar mail-unsent-separator
3458 (concat "^ *---+ +Unsent message follows +---+ *$\\|"
3459 "^ *---+ +Returned message +---+ *$\\|"
3460 "^ *---+ *Returned mail follows *---+ *$\\|"
3461 "^Start of returned message$\\|"
3462 "^---+ Below this line is a copy of the message.$\\|"
3463 "^ *---+ +Original message +---+ *$\\|"
3464 "^ *--+ +begin message +--+ *$\\|"
3465 "^ *---+ +Original message follows +---+ *$\\|"
3466 "^ *---+ +Your message follows +---+ *$\\|"
3467 "^|? *---+ +Message text follows: +---+ *|?$\\|"
3468 "^ *---+ +This is a copy of \\w+ message, including all the headers.*---+ *$")
3469 "A regexp that matches the separator before the text of a failed message.")
3470
3471 (defvar mail-mime-unsent-header "^Content-Type: message/rfc822 *$"
3472 "A regexp that matches the header of a MIME body part with a failed message.")
3473
3474 (defun rmail-retry-failure ()
3475 "Edit a mail message which is based on the contents of the current message.
3476 For a message rejected by the mail system, extract the interesting headers and
3477 the body of the original message.
3478 If the failed message is a MIME multipart message, it is searched for a
3479 body part with a header which matches the variable `mail-mime-unsent-header'.
3480 Otherwise, the variable `mail-unsent-separator' should match the string that
3481 delimits the returned original message.
3482 The variable `rmail-retry-ignored-headers' is a regular expression
3483 specifying headers which should not be copied into the new message."
3484 (interactive)
3485 (require 'mail-utils)
3486 (let ((rmail-this-buffer (current-buffer))
3487 (msgnum rmail-current-message)
3488 bounce-start bounce-end bounce-indent resending
3489 ;; Fetch any content-type header in current message
3490 ;; Must search thru the whole unpruned header.
3491 (content-type
3492 (save-excursion
3493 (save-restriction
3494 (rmail-narrow-to-non-pruned-header)
3495 (mail-fetch-field "Content-Type") ))))
3496 (save-excursion
3497 (goto-char (point-min))
3498 (let ((case-fold-search t))
3499 (if (and content-type
3500 (string-match
3501 ";[\n\t ]*boundary=\"?\\([-0-9a-z'()+_,./:=? ]+\\)\"?"
3502 content-type))
3503 ;; Handle a MIME multipart bounce message.
3504 (let ((codestring
3505 (concat "\n--"
3506 (substring content-type (match-beginning 1)
3507 (match-end 1)))))
3508 (unless (re-search-forward mail-mime-unsent-header nil t)
3509 (error "Cannot find beginning of header in failed message"))
3510 (unless (search-forward "\n\n" nil t)
3511 (error "Cannot find start of Mime data in failed message"))
3512 (setq bounce-start (point))
3513 (if (search-forward codestring nil t)
3514 (setq bounce-end (match-beginning 0))
3515 (setq bounce-end (point-max))))
3516 ;; Non-MIME bounce.
3517 (or (re-search-forward mail-unsent-separator nil t)
3518 (error "Cannot parse this as a failure message"))
3519 (skip-chars-forward "\n")
3520 ;; Support a style of failure message in which the original
3521 ;; message is indented, and included within lines saying
3522 ;; `Start of returned message' and `End of returned message'.
3523 (if (looking-at " +Received:")
3524 (progn
3525 (setq bounce-start (point))
3526 (skip-chars-forward " ")
3527 (setq bounce-indent (- (current-column)))
3528 (goto-char (point-max))
3529 (re-search-backward "^End of returned message$" nil t)
3530 (setq bounce-end (point)))
3531 ;; One message contained a few random lines before
3532 ;; the old message header. The first line of the
3533 ;; message started with two hyphens. A blank line
3534 ;; followed these random lines. The same line
3535 ;; beginning with two hyphens was possibly marking
3536 ;; the end of the message.
3537 (if (looking-at "^--")
3538 (let ((boundary (buffer-substring-no-properties
3539 (point)
3540 (progn (end-of-line) (point)))))
3541 (search-forward "\n\n")
3542 (skip-chars-forward "\n")
3543 (setq bounce-start (point))
3544 (goto-char (point-max))
3545 (search-backward (concat "\n\n" boundary) bounce-start t)
3546 (setq bounce-end (point)))
3547 (setq bounce-start (point)
3548 bounce-end (point-max)))
3549 (unless (search-forward "\n\n" nil t)
3550 (error "Cannot find end of header in failed message"))))))
3551 ;; We have found the message that bounced, within the current message.
3552 ;; Now start sending new message; default header fields from original.
3553 ;; Turn off the usual actions for initializing the message body
3554 ;; because we want to get only the text from the failure message.
3555 (let (mail-signature mail-setup-hook)
3556 (if (rmail-start-mail nil nil nil nil nil rmail-this-buffer
3557 (list (list 'rmail-mark-message
3558 rmail-this-buffer
3559 (aref rmail-msgref-vector msgnum)
3560 "retried")))
3561 ;; Insert original text as initial text of new draft message.
3562 ;; Bind inhibit-read-only since the header delimiter
3563 ;; of the previous message was probably read-only.
3564 (let ((inhibit-read-only t)
3565 rmail-displayed-headers
3566 rmail-ignored-headers)
3567 (erase-buffer)
3568 (insert-buffer-substring rmail-this-buffer
3569 bounce-start bounce-end)
3570 (goto-char (point-min))
3571 (if bounce-indent
3572 (indent-rigidly (point-min) (point-max) bounce-indent))
3573 (rmail-clear-headers rmail-retry-ignored-headers)
3574 (rmail-clear-headers "^sender:\\|^return-path:\\|^received:")
3575 (mail-sendmail-delimit-header)
3576 (save-restriction
3577 (narrow-to-region (point-min) (mail-header-end))
3578 (setq resending (mail-fetch-field "resent-to"))
3579 (if mail-self-blind
3580 (if resending
3581 (insert "Resent-Bcc: " (user-login-name) "\n")
3582 (insert "BCC: " (user-login-name) "\n"))))
3583 (goto-char (point-min))
3584 (mail-position-on-field (if resending "Resent-To" "To") t))))))
3585 \f
3586 (defun rmail-summary-exists ()
3587 "Non-nil iff in an RMAIL buffer and an associated summary buffer exists.
3588 In fact, the non-nil value returned is the summary buffer itself."
3589 (and rmail-summary-buffer (buffer-name rmail-summary-buffer)
3590 rmail-summary-buffer))
3591
3592 (defun rmail-summary-displayed ()
3593 "t iff in RMAIL buffer and an associated summary buffer is displayed."
3594 (and rmail-summary-buffer (get-buffer-window rmail-summary-buffer)))
3595
3596 (defcustom rmail-redisplay-summary nil
3597 "*Non-nil means Rmail should show the summary when it changes.
3598 This has an effect only if a summary buffer exists."
3599 :type 'boolean
3600 :group 'rmail-summary)
3601
3602 (defcustom rmail-summary-window-size nil
3603 "*Non-nil means specify the height for an Rmail summary window."
3604 :type '(choice (const :tag "Disabled" nil) integer)
3605 :group 'rmail-summary)
3606
3607 ;; Put the summary buffer back on the screen, if user wants that.
3608 (defun rmail-maybe-display-summary ()
3609 (let ((selected (selected-window))
3610 window)
3611 ;; If requested, make sure the summary is displayed.
3612 (and rmail-summary-buffer (buffer-name rmail-summary-buffer)
3613 rmail-redisplay-summary
3614 (if (get-buffer-window rmail-summary-buffer 0)
3615 ;; It's already in some frame; show that one.
3616 (let ((frame (window-frame
3617 (get-buffer-window rmail-summary-buffer 0))))
3618 (make-frame-visible frame)
3619 (raise-frame frame))
3620 (display-buffer rmail-summary-buffer)))
3621 ;; If requested, set the height of the summary window.
3622 (and rmail-summary-buffer (buffer-name rmail-summary-buffer)
3623 rmail-summary-window-size
3624 (setq window (get-buffer-window rmail-summary-buffer))
3625 ;; Don't try to change the size if just one window in frame.
3626 (not (eq window (frame-root-window (window-frame window))))
3627 (unwind-protect
3628 (progn
3629 (select-window window)
3630 (enlarge-window (- rmail-summary-window-size (window-height))))
3631 (select-window selected)))))
3632 \f
3633 ;;;; *** Rmail Local Fontification ***
3634
3635 (defun rmail-fontify-buffer-function ()
3636 ;; This function's symbol is bound to font-lock-fontify-buffer-function.
3637 (add-hook 'rmail-show-message-hook 'rmail-fontify-message nil t)
3638 ;; If we're already showing a message, fontify it now.
3639 (if rmail-current-message (rmail-fontify-message))
3640 ;; Prevent Font Lock mode from kicking in.
3641 (setq font-lock-fontified t))
3642
3643 (defun rmail-unfontify-buffer-function ()
3644 ;; This function's symbol is bound to font-lock-fontify-unbuffer-function.
3645 (let ((modified (buffer-modified-p))
3646 (buffer-undo-list t) (inhibit-read-only t)
3647 before-change-functions after-change-functions
3648 buffer-file-name buffer-file-truename)
3649 (save-restriction
3650 (widen)
3651 (remove-hook 'rmail-show-message-hook 'rmail-fontify-message t)
3652 (remove-text-properties (point-min) (point-max) '(rmail-fontified nil))
3653 (font-lock-default-unfontify-buffer)
3654 (and (not modified) (buffer-modified-p) (set-buffer-modified-p nil)))))
3655
3656 (defun rmail-fontify-message ()
3657 ;; Fontify the current message if it is not already fontified.
3658 (if (text-property-any (point-min) (point-max) 'rmail-fontified nil)
3659 (let ((modified (buffer-modified-p))
3660 (buffer-undo-list t) (inhibit-read-only t)
3661 before-change-functions after-change-functions
3662 buffer-file-name buffer-file-truename)
3663 (save-excursion
3664 (save-match-data
3665 (add-text-properties (point-min) (point-max) '(rmail-fontified t))
3666 (font-lock-fontify-region (point-min) (point-max))
3667 (and (not modified) (buffer-modified-p) (set-buffer-modified-p nil)))))))
3668 \f
3669 ;;; Speedbar support for RMAIL files.
3670 (eval-when-compile (require 'speedbar))
3671
3672 (defvar rmail-speedbar-match-folder-regexp "^[A-Z0-9]+\\(\\.[A-Z0-9]+\\)?$"
3673 "*This regex is used to match folder names to be displayed in speedbar.
3674 Enabling this will permit speedbar to display your folders for easy
3675 browsing, and moving of messages.")
3676
3677 (defvar rmail-speedbar-last-user nil
3678 "The last user to be displayed in the speedbar.")
3679
3680 (defvar rmail-speedbar-key-map nil
3681 "Keymap used when in rmail display mode.")
3682
3683 (defun rmail-install-speedbar-variables ()
3684 "Install those variables used by speedbar to enhance rmail."
3685 (if rmail-speedbar-key-map
3686 nil
3687 (setq rmail-speedbar-key-map (speedbar-make-specialized-keymap))
3688
3689 (define-key rmail-speedbar-key-map "e" 'speedbar-edit-line)
3690 (define-key rmail-speedbar-key-map "r" 'speedbar-edit-line)
3691 (define-key rmail-speedbar-key-map "\C-m" 'speedbar-edit-line)
3692 (define-key rmail-speedbar-key-map "M"
3693 'rmail-speedbar-move-message-to-folder-on-line)))
3694
3695 (defvar rmail-speedbar-menu-items
3696 '(["Read Folder" speedbar-edit-line t]
3697 ["Move message to folder" rmail-speedbar-move-message-to-folder-on-line
3698 (save-excursion (beginning-of-line)
3699 (looking-at "<M> "))])
3700 "Additional menu-items to add to speedbar frame.")
3701
3702 ;; Make sure our special speedbar major mode is loaded
3703 (if (featurep 'speedbar)
3704 (rmail-install-speedbar-variables)
3705 (add-hook 'speedbar-load-hook 'rmail-install-speedbar-variables))
3706
3707 (defun rmail-speedbar-buttons (buffer)
3708 "Create buttons for BUFFER containing rmail messages.
3709 Click on the address under Reply to: to reply to this person.
3710 Under Folders: Click a name to read it, or on the <M> to move the
3711 current message into that RMAIL folder."
3712 (let ((from nil))
3713 (save-excursion
3714 (set-buffer buffer)
3715 (goto-char (point-min))
3716 (if (not (re-search-forward "^Reply-To: " nil t))
3717 (if (not (re-search-forward "^From:? " nil t))
3718 (setq from t)))
3719 (if from
3720 nil
3721 (setq from (buffer-substring (point) (save-excursion
3722 (end-of-line)
3723 (point))))))
3724 (goto-char (point-min))
3725 (if (and (looking-at "Reply to:")
3726 (equal from rmail-speedbar-last-user))
3727 nil
3728 (setq rmail-speedbar-last-user from)
3729 (erase-buffer)
3730 (insert "Reply To:\n")
3731 (if (stringp from)
3732 (speedbar-insert-button from 'speedbar-directory-face 'highlight
3733 'rmail-speedbar-button 'rmail-reply))
3734 (insert "Folders:\n")
3735 (let* ((case-fold-search nil)
3736 (df (directory-files (save-excursion (set-buffer buffer)
3737 default-directory)
3738 nil rmail-speedbar-match-folder-regexp)))
3739 (while df
3740 (speedbar-insert-button "<M>" 'speedbar-button-face 'highlight
3741 'rmail-speedbar-move-message (car df))
3742 (speedbar-insert-button (car df) 'speedbar-file-face 'highlight
3743 'rmail-speedbar-find-file nil t)
3744 (setq df (cdr df)))))))
3745
3746 (defun rmail-speedbar-button (text token indent)
3747 "Execute an rmail command specified by TEXT.
3748 The command used is TOKEN. INDENT is not used."
3749 (speedbar-with-attached-buffer
3750 (funcall token t)))
3751
3752 (defun rmail-speedbar-find-file (text token indent)
3753 "Load in the rmail file TEXT.
3754 TOKEN and INDENT are not used."
3755 (speedbar-with-attached-buffer
3756 (message "Loading in RMAIL file %s..." text)
3757 (find-file text)))
3758
3759 (defun rmail-speedbar-move-message-to-folder-on-line ()
3760 "If the current line is a folder, move current message to it."
3761 (interactive)
3762 (save-excursion
3763 (beginning-of-line)
3764 (if (re-search-forward "<M> " (save-excursion (end-of-line) (point)) t)
3765 (progn
3766 (forward-char -2)
3767 (speedbar-do-function-pointer)))))
3768
3769 (defun rmail-speedbar-move-message (text token indent)
3770 "From button TEXT, copy current message to the rmail file specified by TOKEN.
3771 TEXT and INDENT are not used."
3772 (speedbar-with-attached-buffer
3773 (message "Moving message to %s" token)
3774 (rmail-output-to-rmail-file token)))
3775
3776 ; Functions for setting, getting and encoding the POP password.
3777 ; The password is encoded to prevent it from being easily accessible
3778 ; to "prying eyes." Obviously, this encoding isn't "real security,"
3779 ; nor is it meant to be.
3780
3781 ;;;###autoload
3782 (defun rmail-set-pop-password (password)
3783 "Set PASSWORD to be used for retrieving mail from a POP server."
3784 (interactive "sPassword: ")
3785 (if password
3786 (setq rmail-encoded-pop-password
3787 (rmail-encode-string password (emacs-pid)))
3788 (setq rmail-pop-password nil)
3789 (setq rmail-encoded-pop-password nil)))
3790
3791 (defun rmail-get-pop-password ()
3792 "Get the password for retrieving mail from a POP server. If none
3793 has been set, then prompt the user for one."
3794 (if (not rmail-encoded-pop-password)
3795 (progn (if (not rmail-pop-password)
3796 (setq rmail-pop-password (read-passwd "POP password: ")))
3797 (rmail-set-pop-password rmail-pop-password)
3798 (setq rmail-pop-password nil)))
3799 (rmail-encode-string rmail-encoded-pop-password (emacs-pid)))
3800
3801 (defun rmail-have-password ()
3802 (or rmail-pop-password rmail-encoded-pop-password))
3803
3804 (defun rmail-encode-string (string mask)
3805 "Encode STRING with integer MASK, by taking the exclusive OR of the
3806 lowest byte in the mask with the first character of string, the
3807 second-lowest-byte with the second character of the string, etc.,
3808 restarting at the lowest byte of the mask whenever it runs out.
3809 Returns the encoded string. Calling the function again with an
3810 encoded string (and the same mask) will decode the string."
3811 (setq mask (abs mask)) ; doesn't work if negative
3812 (let* ((string-vector (string-to-vector string)) (i 0)
3813 (len (length string-vector)) (curmask mask) charmask)
3814 (while (< i len)
3815 (if (= curmask 0)
3816 (setq curmask mask))
3817 (setq charmask (% curmask 256))
3818 (setq curmask (lsh curmask -8))
3819 (aset string-vector i (logxor charmask (aref string-vector i)))
3820 (setq i (1+ i)))
3821 (concat string-vector)))
3822
3823 (provide 'rmail)
3824
3825 ;;; arch-tag: cff0a950-57fe-4f73-a86e-91ff75afd06c
3826 ;;; rmail.el ends here