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