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