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