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