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