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