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