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