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