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