]> code.delx.au - gnu-emacs/blob - lisp/mail/rmail.el
2008-02-11 Drew Adams <drew.adams@oracle.com>
[gnu-emacs] / lisp / mail / rmail.el
1 ;;; rmail.el --- main code of "RMAIL" mail reader for Emacs
2
3 ;; Copyright (C) 1985, 1986, 1987, 1988, 1993, 1994, 1995, 1996, 1997, 1998,
4 ;; 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
5 ;; Free Software Foundation, Inc.
6
7 ;; Maintainer: FSF
8 ;; Keywords: mail
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 3, or (at your option)
15 ;; any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
26
27 ;;; Commentary:
28
29 ;;; Code:
30
31 ;; Souped up by shane@mit-ajax based on ideas of rlk@athena.mit.edu
32 ;; New features include attribute and keyword support, message
33 ;; selection by dispatch table, summary by attributes and keywords,
34 ;; expunging by dispatch table, sticky options for file commands.
35
36 ;; Extended by Bob Weiner of Motorola
37 ;; New features include: rmail and rmail-summary buffers remain
38 ;; synchronized and key bindings basically operate the same way in both
39 ;; buffers, summary by topic or by regular expression, rmail-reply-prefix
40 ;; variable, and a bury rmail buffer (wipe) command.
41 ;;
42
43 (require 'mail-utils)
44 (eval-when-compile (require 'mule-util)) ; for detect-coding-with-priority
45
46 (defvar deleted-head)
47 (defvar font-lock-fontified)
48 (defvar mail-abbrev-syntax-table)
49 (defvar mail-abbrevs)
50 (defvar messages-head)
51 (defvar rmail-use-spam-filter)
52 (defvar rsf-beep)
53 (defvar rsf-sleep-after-message)
54 (defvar total-messages)
55 (defvar tool-bar-map)
56
57 ; These variables now declared in paths.el.
58 ;(defvar rmail-spool-directory "/usr/spool/mail/"
59 ; "This is the name of the directory used by the system mailer for\n\
60 ;delivering new mail. Its name should end with a slash.")
61 ;(defvar rmail-file-name
62 ; (expand-file-name "~/RMAIL")
63 ; "")
64
65 (defgroup rmail nil
66 "Mail reader for Emacs."
67 :group 'mail)
68
69 (defgroup rmail-retrieve nil
70 "Rmail retrieval options."
71 :prefix "rmail-"
72 :group 'rmail)
73
74 (defgroup rmail-files nil
75 "Rmail files."
76 :prefix "rmail-"
77 :group 'rmail)
78
79 (defgroup rmail-headers nil
80 "Rmail header options."
81 :prefix "rmail-"
82 :group 'rmail)
83
84 (defgroup rmail-reply nil
85 "Rmail reply options."
86 :prefix "rmail-"
87 :group 'rmail)
88
89 (defgroup rmail-summary nil
90 "Rmail summary options."
91 :prefix "rmail-"
92 :prefix "rmail-summary-"
93 :group 'rmail)
94
95 (defgroup rmail-output nil
96 "Output message to a file."
97 :prefix "rmail-output-"
98 :prefix "rmail-"
99 :group 'rmail)
100
101 (defgroup rmail-edit nil
102 "Rmail editing."
103 :prefix "rmail-edit-"
104 :group 'rmail)
105
106 (defgroup rmail-obsolete nil
107 "Rmail obsolete customization variables."
108 :group 'rmail)
109
110 (defcustom rmail-movemail-program nil
111 "If non-nil, the file name of the `movemail' program."
112 :group 'rmail-retrieve
113 :type '(choice (const nil) string))
114
115 (defcustom rmail-pop-password nil
116 "*Password to use when reading mail from POP server.
117 Please use `rmail-remote-password' instead."
118 :type '(choice (string :tag "Password")
119 (const :tag "Not Required" nil))
120 :group 'rmail-obsolete)
121
122 (defcustom rmail-pop-password-required nil
123 "*Non-nil if a password is required when reading mail from a POP server.
124 Please use rmail-remote-password-required instead."
125 :type 'boolean
126 :group 'rmail-obsolete)
127
128 (defcustom rmail-remote-password nil
129 "*Password to use when reading mail from a remote server.
130 This setting is ignored for mailboxes whose URL already contains a password."
131 :type '(choice (string :tag "Password")
132 (const :tag "Not Required" nil))
133 :set-after '(rmail-pop-password)
134 :set #'(lambda (symbol value)
135 (set-default symbol
136 (if (and (not value)
137 (boundp 'rmail-pop-password)
138 rmail-pop-password)
139 rmail-pop-password
140 value))
141 (setq rmail-pop-password nil))
142 :group 'rmail-retrieve
143 :version "22.1")
144
145 (defcustom rmail-remote-password-required nil
146 "*Non-nil if a password is required when reading mail from a remote server."
147 :type 'boolean
148 :set-after '(rmail-pop-password-required)
149 :set #'(lambda (symbol value)
150 (set-default symbol
151 (if (and (not value)
152 (boundp 'rmail-pop-password-required)
153 rmail-pop-password-required)
154 rmail-pop-password-required
155 value))
156 (setq rmail-pop-password-required nil))
157 :group 'rmail-retrieve
158 :version "22.1")
159
160 (defcustom rmail-movemail-flags nil
161 "*List of flags to pass to movemail.
162 Most commonly used to specify `-g' to enable GSS-API authentication
163 or `-k' to enable Kerberos authentication."
164 :type '(repeat string)
165 :group 'rmail-retrieve
166 :version "20.3")
167
168 (defvar rmail-remote-password-error "invalid usercode or password\\|
169 unknown user name or bad password\\|Authentication failed\\|MU_ERR_AUTH_FAILURE"
170 "Regular expression matching incorrect-password POP or IMAP server error
171 messages.
172 If you get an incorrect-password error that this expression does not match,
173 please report it with \\[report-emacs-bug].")
174
175 (defvar rmail-encoded-remote-password nil)
176
177 (defcustom rmail-preserve-inbox nil
178 "*Non-nil means leave incoming mail in the user's inbox--don't delete it."
179 :type 'boolean
180 :group 'rmail-retrieve)
181
182 (defcustom rmail-movemail-search-path nil
183 "*List of directories to search for movemail (in addition to `exec-path')."
184 :group 'rmail-retrieve
185 :type '(repeat (directory)))
186
187 (declare-function mail-position-on-field "sendmail" (field &optional soft))
188 (declare-function mail-text-start "sendmail" ())
189 (declare-function rmail-update-summary "rmailsum" (&rest ignore))
190
191 (defun rmail-probe (prog)
192 "Determine what flavor of movemail PROG is.
193 We do this by executing it with `--version' and analyzing its output."
194 (with-temp-buffer
195 (let ((tbuf (current-buffer)))
196 (buffer-disable-undo tbuf)
197 (call-process prog nil tbuf nil "--version")
198 (if (not (buffer-modified-p tbuf))
199 ;; Should not happen...
200 nil
201 (goto-char (point-min))
202 (cond
203 ((looking-at ".*movemail: invalid option")
204 'emacs) ;; Possibly...
205 ((looking-at "movemail (GNU Mailutils .*)")
206 'mailutils)
207 (t
208 ;; FIXME:
209 'emacs))))))
210
211 (defun rmail-autodetect ()
212 "Determine the file name of the `movemail' program and return its flavor.
213 If `rmail-movemail-program' is non-nil, use it.
214 Otherwise, look for `movemail' in the directories in
215 `rmail-movemail-search-path', those in `exec-path', and `exec-directory'."
216 (if rmail-movemail-program
217 (rmail-probe rmail-movemail-program)
218 (catch 'scan
219 (dolist (dir (append rmail-movemail-search-path exec-path
220 (list exec-directory)))
221 (when (and dir (file-accessible-directory-p dir))
222 (let ((progname (expand-file-name "movemail" dir)))
223 (when (and (not (file-directory-p progname))
224 (file-executable-p progname))
225 (let ((x (rmail-probe progname)))
226 (when x
227 (setq rmail-movemail-program progname)
228 (throw 'scan x))))))))))
229
230 (defvar rmail-movemail-variant-in-use nil
231 "The movemail variant currently in use. Known variants are:
232
233 `emacs' Means any implementation, compatible with the native Emacs one.
234 This is the default;
235 `mailutils' Means GNU mailutils implementation, capable of handling full
236 mail URLs as the source mailbox.")
237
238 ;;;###autoload
239 (defun rmail-movemail-variant-p (&rest variants)
240 "Return t if the current movemail variant is any of VARIANTS.
241 Currently known variants are 'emacs and 'mailutils."
242 (when (not rmail-movemail-variant-in-use)
243 ;; Autodetect
244 (setq rmail-movemail-variant-in-use (rmail-autodetect)))
245 (not (null (member rmail-movemail-variant-in-use variants))))
246
247 ;; Call for effect, to set rmail-movemail-program (if not set by the
248 ;; user), and rmail-movemail-variant-in-use. Used by various functions.
249 ;; I'm not sure if M-x rmail is the only entry point to this package.
250 ;; If so, this can be moved there.
251 (rmail-movemail-variant-p)
252
253 ;;;###autoload
254 (defcustom rmail-dont-reply-to-names nil "\
255 *A regexp specifying addresses to prune from a reply message.
256 A value of nil means exclude your own email address as an address
257 plus whatever is specified by `rmail-default-dont-reply-to-names'."
258 :type '(choice regexp (const :tag "Your Name" nil))
259 :group 'rmail-reply)
260
261 ;;;###autoload
262 (defvar rmail-default-dont-reply-to-names "\\`info-" "\
263 A regular expression specifying part of the default value of the
264 variable `rmail-dont-reply-to-names', for when the user does not set
265 `rmail-dont-reply-to-names' explicitly. (The other part of the default
266 value is the user's email address and name.)
267 It is useful to set this variable in the site customization file.")
268
269 ;;;###autoload
270 (defcustom rmail-ignored-headers
271 (concat "^via:\\|^mail-from:\\|^origin:\\|^references:\\|^sender:"
272 "\\|^status:\\|^received:\\|^x400-originator:\\|^x400-recipients:"
273 "\\|^x400-received:\\|^x400-mts-identifier:\\|^x400-content-type:"
274 "\\|^\\(resent-\\|\\)message-id:\\|^summary-line:\\|^resent-date:"
275 "\\|^nntp-posting-host:\\|^path:\\|^x-char.*:\\|^x-face:\\|^face:"
276 "\\|^x-mailer:\\|^delivered-to:\\|^lines:"
277 "\\|^content-transfer-encoding:\\|^x-coding-system:"
278 "\\|^return-path:\\|^errors-to:\\|^return-receipt-to:"
279 "\\|^precedence:\\|^list-help:\\|^list-post:\\|^list-subscribe:"
280 "\\|^list-id:\\|^list-unsubscribe:\\|^list-archive:"
281 "\\|^content-length:\\|^nntp-posting-date:\\|^user-agent"
282 "\\|^importance:\\|^envelope-to:\\|^delivery-date\\|^openpgp:"
283 "\\|^mbox-line:\\|^cancel-lock:\\|^DomainKey-Signature:"
284 "\\|^resent-face:\\|^resent-x.*:\\|^resent-organization:\\|^resent-openpgp:"
285
286 "\\|^x-.*:")
287 "*Regexp to match header fields that Rmail should normally hide.
288 \(See also `rmail-nonignored-headers', which overrides this regexp.)
289 This variable is used for reformatting the message header,
290 which normally happens once for each message,
291 when you view the message for the first time in Rmail.
292 To make a change in this variable take effect
293 for a message that you have already viewed,
294 go to that message and type \\[rmail-toggle-header] twice."
295 :type 'regexp
296 :group 'rmail-headers)
297
298 (defcustom rmail-nonignored-headers "^x-spam-status:"
299 "*Regexp to match X header fields that Rmail should show.
300 This regexp overrides `rmail-ignored-headers'; if both this regexp
301 and that one match a certain header field, Rmail shows the field.
302 If this is nil, ignore all header fields in `rmail-ignored-headers'.
303
304 This variable is used for reformatting the message header,
305 which normally happens once for each message,
306 when you view the message for the first time in Rmail.
307 To make a change in this variable take effect
308 for a message that you have already viewed,
309 go to that message and type \\[rmail-toggle-header] twice."
310 :type '(choice (const nil) (regexp))
311 :group 'rmail-headers)
312
313 ;;;###autoload
314 (defcustom rmail-displayed-headers nil
315 "*Regexp to match Header fields that Rmail should display.
316 If nil, display all header fields except those matched by
317 `rmail-ignored-headers'."
318 :type '(choice regexp (const :tag "All"))
319 :group 'rmail-headers)
320
321 ;;;###autoload
322 (defcustom rmail-retry-ignored-headers "^x-authentication-warning:" "\
323 *Headers that should be stripped when retrying a failed message."
324 :type '(choice regexp (const nil :tag "None"))
325 :group 'rmail-headers)
326
327 ;;;###autoload
328 (defcustom rmail-highlighted-headers "^From:\\|^Subject:" "\
329 *Regexp to match Header fields that Rmail should normally highlight.
330 A value of nil means don't highlight.
331 See also `rmail-highlight-face'."
332 :type 'regexp
333 :group 'rmail-headers)
334
335 (defface rmail-highlight
336 '((t (:inherit highlight)))
337 "Face to use for highlighting the most important header fields."
338 :group 'rmail-headers
339 :version "22.1")
340
341 ;;;###autoload
342 (defcustom rmail-highlight-face 'rmail-highlight "\
343 *Face used by Rmail for highlighting headers."
344 :type '(choice (const :tag "Default" nil)
345 face)
346 :group 'rmail-headers)
347
348 (defface rmail-header-name
349 '((t (:inherit font-lock-function-name-face)))
350 "Face to use for highlighting the header names."
351 :group 'rmail-headers
352 :version "23.1")
353
354 ;;;###autoload
355 (defcustom rmail-header-name-face 'rmail-header-name "\
356 *Face to use for highlighting the header names."
357 :type '(choice (const :tag "Default" nil)
358 face)
359 :group 'rmail-headers)
360
361 ;;;###autoload
362 (defcustom rmail-delete-after-output nil "\
363 *Non-nil means automatically delete a message that is copied to a file."
364 :type 'boolean
365 :group 'rmail-files)
366
367 ;;;###autoload
368 (defcustom rmail-primary-inbox-list nil "\
369 *List of files which are inboxes for user's primary mail file `~/RMAIL'.
370 nil means the default, which is (\"/usr/spool/mail/$USER\")
371 \(the name varies depending on the operating system,
372 and the value of the environment variable MAIL overrides it)."
373 ;; Don't use backquote here, because we don't want to need it
374 ;; at load time.
375 :type (list 'choice '(const :tag "Default" nil)
376 (list 'repeat ':value (list (or (getenv "MAIL")
377 (concat "/var/spool/mail/"
378 (getenv "USER"))))
379 'file))
380 :group 'rmail-retrieve
381 :group 'rmail-files)
382
383 ;;;###autoload
384 (defcustom rmail-mail-new-frame nil
385 "*Non-nil means Rmail makes a new frame for composing outgoing mail.
386 This is handy if you want to preserve the window configuration of
387 the frame where you have the RMAIL buffer displayed."
388 :type 'boolean
389 :group 'rmail-reply)
390
391 ;;;###autoload
392 (defcustom rmail-secondary-file-directory "~/"
393 "*Directory for additional secondary Rmail files."
394 :type 'directory
395 :group 'rmail-files)
396 ;;;###autoload
397 (defcustom rmail-secondary-file-regexp "\\.xmail$"
398 "*Regexp for which files are secondary Rmail files."
399 :type 'regexp
400 :group 'rmail-files)
401
402 ;;;###autoload
403 (defcustom rmail-confirm-expunge 'y-or-n-p
404 "*Whether and how to ask for confirmation before expunging deleted messages."
405 :type '(choice (const :tag "No confirmation" nil)
406 (const :tag "Confirm with y-or-n-p" y-or-n-p)
407 (const :tag "Confirm with yes-or-no-p" yes-or-no-p))
408 :version "21.1"
409 :group 'rmail-files)
410
411 ;;;###autoload
412 (defvar rmail-mode-hook nil
413 "List of functions to call when Rmail is invoked.")
414
415 ;;;###autoload
416 (defvar rmail-get-new-mail-hook nil
417 "List of functions to call when Rmail has retrieved new mail.")
418
419 ;;;###autoload
420 (defcustom rmail-show-message-hook nil
421 "List of functions to call when Rmail displays a message."
422 :type 'hook
423 :options '(goto-address)
424 :group 'rmail)
425
426 ;;;###autoload
427 (defvar rmail-quit-hook nil
428 "List of functions to call when quitting out of Rmail.")
429
430 ;;;###autoload
431 (defvar rmail-delete-message-hook nil
432 "List of functions to call when Rmail deletes a message.
433 When the hooks are called, the message has been marked deleted but is
434 still the current message in the Rmail buffer.")
435
436 ;; These may be altered by site-init.el to match the format of mmdf files
437 ;; delimiting used on a given host (delim1 and delim2 from the config
438 ;; files).
439
440 (defvar rmail-mmdf-delim1 "^\001\001\001\001\n"
441 "Regexp marking the start of an mmdf message.")
442 (defvar rmail-mmdf-delim2 "^\001\001\001\001\n"
443 "Regexp marking the end of an mmdf message.")
444
445 (defcustom rmail-message-filter nil
446 "If non-nil, a filter function for new messages in RMAIL.
447 Called with region narrowed to the message, including headers,
448 before obeying `rmail-ignored-headers'."
449 :group 'rmail-headers
450 :type '(choice (const nil) function))
451
452 (defcustom rmail-automatic-folder-directives nil
453 "List of directives specifying where to put a message.
454 Each element of the list is of the form:
455
456 (FOLDERNAME FIELD REGEXP [ FIELD REGEXP ] ... )
457
458 Where FOLDERNAME is the name of a BABYL format folder to put the
459 message. If any of the field regexp's are nil, then it is ignored.
460
461 If FOLDERNAME is \"/dev/null\", it is deleted.
462 If FOLDERNAME is nil then it is deleted, and skipped.
463
464 FIELD is the plain text name of a field in the message, such as
465 \"subject\" or \"from\". A FIELD of \"to\" will automatically include
466 all text from the \"cc\" field as well.
467
468 REGEXP is an expression to match in the preceeding specified FIELD.
469 FIELD/REGEXP pairs continue in the list.
470
471 examples:
472 (\"/dev/null\" \"from\" \"@spam.com\") ; delete all mail from spam.com
473 (\"RMS\" \"from\" \"rms@\") ; save all mail from RMS."
474 :group 'rmail
475 :version "21.1"
476 :type '(repeat (sexp :tag "Directive")))
477
478 (defvar rmail-reply-prefix "Re: "
479 "String to prepend to Subject line when replying to a message.")
480
481 ;; Some mailers use "Re(2):" or "Re^2:" or "Re: Re:" or "Re[2]:".
482 ;; This pattern should catch all the common variants.
483 ;; rms: I deleted the change to delete tags in square brackets
484 ;; because they mess up RT tags.
485 (defvar rmail-reply-regexp "\\`\\(Re\\(([0-9]+)\\|\\[[0-9]+\\]\\|\\^[0-9]+\\)?: *\\)*"
486 "Regexp to delete from Subject line before inserting `rmail-reply-prefix'.")
487
488 (defcustom rmail-display-summary nil
489 "*If non-nil, Rmail always displays the summary buffer."
490 :group 'rmail-summary
491 :type 'boolean)
492 \f
493 (defvar rmail-inbox-list nil)
494 (put 'rmail-inbox-list 'permanent-local t)
495
496 (defvar rmail-keywords nil)
497 (put 'rmail-keywords 'permanent-local t)
498
499 (defvar rmail-buffer nil
500 "The RMAIL buffer related to the current buffer.
501 In an RMAIL buffer, this holds the RMAIL buffer itself.
502 In a summary buffer, this holds the RMAIL buffer it is a summary for.")
503 (put 'rmail-buffer 'permanent-local t)
504
505 ;; Message counters and markers. Deleted flags.
506
507 (defvar rmail-current-message nil)
508 (put 'rmail-current-message 'permanent-local t)
509
510 (defvar rmail-total-messages nil)
511 (put 'rmail-total-messages 'permanent-local t)
512
513 (defvar rmail-message-vector nil)
514 (put 'rmail-message-vector 'permanent-local t)
515
516 (defvar rmail-deleted-vector nil)
517 (put 'rmail-deleted-vector 'permanent-local t)
518
519 (defvar rmail-msgref-vector nil
520 "In an Rmail buffer, a vector whose Nth element is a list (N).
521 When expunging renumbers messages, these lists are modified
522 by substituting the new message number into the existing list.")
523 (put 'rmail-msgref-vector 'permanent-local t)
524
525 (defvar rmail-overlay-list nil)
526 (put 'rmail-overlay-list 'permanent-local t)
527
528 ;; These are used by autoloaded rmail-summary.
529
530 (defvar rmail-summary-buffer nil)
531 (put 'rmail-summary-buffer 'permanent-local t)
532 (defvar rmail-summary-vector nil)
533 (put 'rmail-summary-vector 'permanent-local t)
534
535 (defvar rmail-view-buffer nil
536 "Buffer which holds RMAIL message for MIME displaying.")
537 (put 'rmail-view-buffer 'permanent-local t)
538 \f
539 ;; `Sticky' default variables.
540
541 ;; Last individual label specified to a or k.
542 (defvar rmail-last-label nil)
543 (put 'rmail-last-label 'permanent-local t)
544
545 ;; Last set of values specified to C-M-n, C-M-p, C-M-s or C-M-l.
546 (defvar rmail-last-multi-labels nil)
547
548 (defvar rmail-last-regexp nil)
549 (put 'rmail-last-regexp 'permanent-local t)
550
551 (defcustom rmail-default-file "~/xmail"
552 "*Default file name for \\[rmail-output]."
553 :type 'file
554 :group 'rmail-files)
555 (defcustom rmail-default-rmail-file "~/XMAIL"
556 "*Default file name for \\[rmail-output-to-rmail-file]."
557 :type 'file
558 :group 'rmail-files)
559 (defcustom rmail-default-body-file "~/mailout"
560 "*Default file name for \\[rmail-output-body-to-file]."
561 :type 'file
562 :group 'rmail-files
563 :version "20.3")
564
565 ;; Mule and MIME related variables.
566
567 ;;;###autoload
568 (defvar rmail-file-coding-system nil
569 "Coding system used in RMAIL file.
570
571 This is set to nil by default.")
572
573 ;;;###autoload
574 (defcustom rmail-enable-mime nil
575 "*If non-nil, RMAIL uses MIME feature.
576 If the value is t, RMAIL automatically shows MIME decoded message.
577 If the value is neither t nor nil, RMAIL does not show MIME decoded message
578 until a user explicitly requires it.
579
580 Even if the value is non-nil, you can't use MIME feature
581 if the feature specified by `rmail-mime-feature' is not available
582 in your session."
583 :type '(choice (const :tag "on" t)
584 (const :tag "off" nil)
585 (other :tag "when asked" ask))
586 :group 'rmail)
587
588 (defvar rmail-enable-mime-composing nil
589 "*If non-nil, RMAIL uses `rmail-insert-mime-forwarded-message-function' to forward.")
590
591 ;;;###autoload
592 (defvar rmail-show-mime-function nil
593 "Function to show MIME decoded message of RMAIL file.
594 This function is called when `rmail-enable-mime' is non-nil.
595 It is called with no argument.")
596
597 ;;;###autoload
598 (defvar rmail-insert-mime-forwarded-message-function nil
599 "Function to insert a message in MIME format so it can be forwarded.
600 This function is called if `rmail-enable-mime' or
601 `rmail-enable-mime-composing' is non-nil.
602 It is called with one argument FORWARD-BUFFER, which is a
603 buffer containing the message to forward. The current buffer
604 is the outgoing mail buffer.")
605
606 ;;;###autoload
607 (defvar rmail-insert-mime-resent-message-function nil
608 "Function to insert a message in MIME format so it can be resent.
609 This function is called if `rmail-enable-mime' is non-nil.
610 It is called with one argument FORWARD-BUFFER, which is a
611 buffer containing the message to forward. The current buffer
612 is the outgoing mail buffer.")
613
614 ;;;###autoload
615 (defvar rmail-search-mime-message-function nil
616 "Function to check if a regexp matches a MIME message.
617 This function is called if `rmail-enable-mime' is non-nil.
618 It is called with two arguments MSG and REGEXP, where
619 MSG is the message number, REGEXP is the regular expression.")
620
621 ;;;###autoload
622 (defvar rmail-search-mime-header-function nil
623 "Function to check if a regexp matches a header of MIME message.
624 This function is called if `rmail-enable-mime' is non-nil.
625 It is called with three arguments MSG, REGEXP, and LIMIT, where
626 MSG is the message number,
627 REGEXP is the regular expression,
628 LIMIT is the position specifying the end of header.")
629
630 ;;;###autoload
631 (defvar rmail-mime-feature 'rmail-mime
632 "Feature to require to load MIME support in Rmail.
633 When starting Rmail, if `rmail-enable-mime' is non-nil,
634 this feature is required with `require'.
635
636 The default value is `rmail-mime'. This feature is provided by
637 the rmail-mime package available at <http://www.m17n.org/rmail-mime/>.")
638
639 ;;;###autoload
640 (defvar rmail-decode-mime-charset t
641 "*Non-nil means a message is decoded by MIME's charset specification.
642 If this variable is nil, or the message has not MIME specification,
643 the message is decoded as normal way.
644
645 If the variable `rmail-enable-mime' is non-nil, this variables is
646 ignored, and all the decoding work is done by a feature specified by
647 the variable `rmail-mime-feature'.")
648
649 ;;;###autoload
650 (defvar rmail-mime-charset-pattern
651 (concat "^content-type:[ \t]*text/plain;"
652 "\\(?:[ \t\n]*\\(?:format\\|delsp\\)=\"?[-a-z0-9]+\"?;\\)*"
653 "[ \t\n]*charset=\"?\\([^ \t\n\";]+\\)\"?")
654 "Regexp to match MIME-charset specification in a header of message.
655 The first parenthesized expression should match the MIME-charset name.")
656
657 \f
658 ;;; Regexp matching the delimiter of messages in UNIX mail format
659 ;;; (UNIX From lines), minus the initial ^. Note that if you change
660 ;;; this expression, you must change the code in rmail-nuke-pinhead-header
661 ;;; that knows the exact ordering of the \\( \\) subexpressions.
662 (defvar rmail-unix-mail-delimiter
663 (let ((time-zone-regexp
664 (concat "\\([A-Z]?[A-Z]?[A-Z][A-Z]\\( DST\\)?"
665 "\\|[-+]?[0-9][0-9][0-9][0-9]"
666 "\\|"
667 "\\) *")))
668 (concat
669 "From "
670
671 ;; Many things can happen to an RFC 822 mailbox before it is put into
672 ;; a `From' line. The leading phrase can be stripped, e.g.
673 ;; `Joe <@w.x:joe@y.z>' -> `<@w.x:joe@y.z>'. The <> can be stripped, e.g.
674 ;; `<@x.y:joe@y.z>' -> `@x.y:joe@y.z'. Everything starting with a CRLF
675 ;; can be removed, e.g.
676 ;; From: joe@y.z (Joe K
677 ;; User)
678 ;; can yield `From joe@y.z (Joe K Fri Mar 22 08:11:15 1996', and
679 ;; From: Joe User
680 ;; <joe@y.z>
681 ;; can yield `From Joe User Fri Mar 22 08:11:15 1996'.
682 ;; The mailbox can be removed or be replaced by white space, e.g.
683 ;; From: "Joe User"{space}{tab}
684 ;; <joe@y.z>
685 ;; can yield `From {space}{tab} Fri Mar 22 08:11:15 1996',
686 ;; where {space} and {tab} represent the Ascii space and tab characters.
687 ;; We want to match the results of any of these manglings.
688 ;; The following regexp rejects names whose first characters are
689 ;; obviously bogus, but after that anything goes.
690 "\\([^\0-\b\n-\r\^?].*\\)? "
691
692 ;; The time the message was sent.
693 "\\([^\0-\r \^?]+\\) +" ; day of the week
694 "\\([^\0-\r \^?]+\\) +" ; month
695 "\\([0-3]?[0-9]\\) +" ; day of month
696 "\\([0-2][0-9]:[0-5][0-9]\\(:[0-6][0-9]\\)?\\) *" ; time of day
697
698 ;; Perhaps a time zone, specified by an abbreviation, or by a
699 ;; numeric offset.
700 time-zone-regexp
701
702 ;; The year.
703 " \\([0-9][0-9]+\\) *"
704
705 ;; On some systems the time zone can appear after the year, too.
706 time-zone-regexp
707
708 ;; Old uucp cruft.
709 "\\(remote from .*\\)?"
710
711 "\n"))
712 nil)
713
714 (setq rmail-font-lock-keywords
715 ;; These are all matched case-insensitively.
716 ;;(eval-when-compile
717 (let* ((cite-chars "[>|}]")
718 (cite-prefix "a-z")
719 (cite-suffix (concat cite-prefix "0-9_.@-`'\"")))
720 (list '("^\\(From\\|Sender\\|Resent-From\\):"
721 . 'rmail-header-name)
722 '("^Reply-To:.*$" . 'rmail-header-name)
723 '("^Subject:" . 'rmail-header-name)
724 '("^X-Spam-Status:" . 'rmail-header-name)
725 '("^\\(To\\|Apparently-To\\|Cc\\|Newsgroups\\):"
726 . 'rmail-header-name)
727 ;; Use MATCH-ANCHORED to effectively anchor the regexp left side.
728 `(,cite-chars
729 (,(concat "\\=[ \t]*"
730 "\\(\\(\\([" cite-prefix "]+[" cite-suffix "]*\\)?"
731 "\\(" cite-chars "[ \t]*\\)\\)+\\)"
732 "\\(.*\\)")
733 (beginning-of-line) (end-of-line)
734 (1 font-lock-comment-delimiter-face nil t)
735 (5 font-lock-comment-face nil t)))
736 '("^\\(X-[a-z0-9-]+\\|In-reply-to\\|Date\\):.*\\(\n[ \t]+.*\\)*$"
737 . 'rmail-header-name))))
738 ;;)
739 ;; "Additional expressions to highlight in Rmail mode.")
740
741 ;; Perform BODY in the summary buffer
742 ;; in such a way that its cursor is properly updated in its own window.
743 (defmacro rmail-select-summary (&rest body)
744 `(let ((total rmail-total-messages))
745 (if (rmail-summary-displayed)
746 (let ((window (selected-window)))
747 (save-excursion
748 (unwind-protect
749 (progn
750 (pop-to-buffer rmail-summary-buffer)
751 ;; rmail-total-messages is a buffer-local var
752 ;; in the rmail buffer.
753 ;; This way we make it available for the body
754 ;; even tho the rmail buffer is not current.
755 (let ((rmail-total-messages total))
756 ,@body))
757 (select-window window))))
758 (save-excursion
759 (set-buffer rmail-summary-buffer)
760 (let ((rmail-total-messages total))
761 ,@body)))
762 (rmail-maybe-display-summary)))
763 \f
764 ;;;; *** Rmail Mode ***
765
766 ;; This variable is dynamically bound. The defvar is here to placate
767 ;; the byte compiler.
768
769 (defvar rmail-enable-multibyte nil)
770
771
772 (defun rmail-require-mime-maybe ()
773 "Require `rmail-mime-feature' if that is non-nil.
774 Signal an error and set `rmail-mime-feature' to nil if the feature
775 isn't provided."
776 (when rmail-enable-mime
777 (condition-case err
778 (require rmail-mime-feature)
779 (error
780 (display-warning
781 :warning
782 (format "Although MIME support is requested
783 by setting `rmail-enable-mime' to non-nil, the required feature
784 `%s' (the value of `rmail-mime-feature')
785 is not available in the current session.
786 So, the MIME support is turned off for the moment."
787 rmail-mime-feature))
788 (setq rmail-enable-mime nil)))))
789
790
791 ;;;###autoload
792 (defun rmail (&optional file-name-arg)
793 "Read and edit incoming mail.
794 Moves messages into file named by `rmail-file-name' (a babyl format file)
795 and edits that file in RMAIL Mode.
796 Type \\[describe-mode] once editing that file, for a list of RMAIL commands.
797
798 May be called with file name as argument; then performs rmail editing on
799 that file, but does not copy any new mail into the file.
800 Interactively, if you supply a prefix argument, then you
801 have a chance to specify a file name with the minibuffer.
802
803 If `rmail-display-summary' is non-nil, make a summary for this RMAIL file."
804 (interactive (if current-prefix-arg
805 (list (read-file-name "Run rmail on RMAIL file: "))))
806 (rmail-require-mime-maybe)
807 (let* ((file-name (expand-file-name (or file-name-arg rmail-file-name)))
808 ;; Use find-buffer-visiting, not get-file-buffer, for those users
809 ;; who have find-file-visit-truename set to t.
810 (existed (find-buffer-visiting file-name))
811 ;; This binding is necessary because we must decide if we
812 ;; need code conversion while the buffer is unibyte
813 ;; (i.e. enable-multibyte-characters is nil).
814 (rmail-enable-multibyte
815 (if existed
816 (with-current-buffer existed enable-multibyte-characters)
817 (default-value 'enable-multibyte-characters)))
818 ;; Since the file may contain messages of different encodings
819 ;; at the tail (non-BYBYL part), we can't decode them at once
820 ;; on reading. So, at first, we read the file without text
821 ;; code conversion, then decode the messages one by one by
822 ;; rmail-decode-babyl-format or
823 ;; rmail-convert-to-babyl-format.
824 (coding-system-for-read (and rmail-enable-multibyte 'raw-text))
825 run-mail-hook msg-shown)
826 ;; Like find-file, but in the case where a buffer existed
827 ;; and the file was reverted, recompute the message-data.
828 ;; We used to bind enable-local-variables to nil here,
829 ;; but that should not be needed now that rmail-mode
830 ;; sets it locally to nil.
831 ;; (Binding a variable locally with let is not safe if it has
832 ;; buffer-local bindings.)
833 (if (and existed (not (verify-visited-file-modtime existed)))
834 (progn
835 (find-file file-name)
836 (if (and (verify-visited-file-modtime existed)
837 (eq major-mode 'rmail-mode))
838 (progn (rmail-forget-messages)
839 (rmail-set-message-counters))))
840 (switch-to-buffer
841 (let ((enable-local-variables nil))
842 (find-file-noselect file-name))))
843 (if (eq major-mode 'rmail-edit-mode)
844 (error "Exit Rmail Edit mode before getting new mail"))
845 (if (and existed (> (buffer-size) 0))
846 ;; Buffer not new and not empty; ensure in proper mode, but that's all.
847 (or (eq major-mode 'rmail-mode)
848 (progn (rmail-mode-2)
849 (setq run-mail-hook t)))
850 (setq run-mail-hook t)
851 (rmail-mode-2)
852 ;; Convert all or part to Babyl file if possible.
853 (rmail-convert-file)
854 (goto-char (point-max)))
855 ;; As we have read a file by raw-text, the buffer is set to
856 ;; unibyte. We must make it multibyte if necessary.
857 (if (and rmail-enable-multibyte
858 (not enable-multibyte-characters))
859 (set-buffer-multibyte t))
860 ;; If necessary, scan to find all the messages.
861 (rmail-maybe-set-message-counters)
862 (unwind-protect
863 (unless (and (not file-name-arg)
864 (rmail-get-new-mail))
865 (rmail-show-message (rmail-first-unseen-message)))
866 (progn
867 (if rmail-display-summary (rmail-summary))
868 (rmail-construct-io-menu)
869 (if run-mail-hook
870 (run-hooks 'rmail-mode-hook))))))
871
872 ;; Given the value of MAILPATH, return a list of inbox file names.
873 ;; This is turned off because it is not clear that the user wants
874 ;; all these inboxes to feed into the primary rmail file.
875 ; (defun rmail-convert-mailpath (string)
876 ; (let (idx list)
877 ; (while (setq idx (string-match "[%:]" string))
878 ; (let ((this (substring string 0 idx)))
879 ; (setq string (substring string (1+ idx)))
880 ; (setq list (cons (if (string-match "%" this)
881 ; (substring this 0 (string-match "%" this))
882 ; this)
883 ; list))))
884 ; list))
885
886 ; I have checked that adding "-*- rmail -*-" to the BABYL OPTIONS line
887 ; will not cause emacs 18.55 problems.
888
889 ;; This calls rmail-decode-babyl-format if the file is already Babyl.
890
891 (defun rmail-convert-file ()
892 (let (convert)
893 (widen)
894 (goto-char (point-min))
895 ;; If file doesn't start like a Babyl file,
896 ;; convert it to one, by adding a header and converting each message.
897 (cond ((looking-at "BABYL OPTIONS:"))
898 ((looking-at "Version: 5\n")
899 ;; Losing babyl file made by old version of Rmail.
900 ;; Just fix the babyl file header; don't make a new one,
901 ;; so we don't lose the Labels: file attribute, etc.
902 (let ((buffer-read-only nil))
903 (insert "BABYL OPTIONS: -*- rmail -*-\n")))
904 ((equal (point-min) (point-max))
905 ;; Empty RMAIL file. Just insert the header.
906 (rmail-insert-rmail-file-header))
907 (t
908 ;; Non-empty file in non-RMAIL format. Add header and convert.
909 (setq convert t)
910 (rmail-insert-rmail-file-header)))
911 ;; If file was not a Babyl file or if there are
912 ;; Unix format messages added at the end,
913 ;; convert file as necessary.
914 (if (or convert
915 (save-excursion
916 (goto-char (point-max))
917 (search-backward "\n\^_")
918 (forward-char 2)
919 (looking-at "\n*From ")))
920 (let ((buffer-read-only nil))
921 (message "Converting to Babyl format...")
922 ;; If file needs conversion, convert it all,
923 ;; except for the BABYL header.
924 ;; (rmail-convert-to-babyl-format would delete the header.)
925 (goto-char (point-min))
926 (search-forward "\n\^_" nil t)
927 (narrow-to-region (point) (point-max))
928 (rmail-convert-to-babyl-format)
929 (message "Converting to Babyl format...done"))
930 (if (and (not rmail-enable-mime)
931 rmail-enable-multibyte)
932 ;; We still have to decode BABYL part.
933 (rmail-decode-babyl-format)))))
934
935 (defun rmail-insert-rmail-file-header ()
936 (let ((buffer-read-only nil))
937 ;; -*-rmail-*- is here so that visiting the file normally
938 ;; recognizes it as an Rmail file.
939 (insert "BABYL OPTIONS: -*- rmail -*-
940 Version: 5
941 Labels:
942 Note: This is the header of an rmail file.
943 Note: If you are seeing it in rmail,
944 Note: it means the file has no messages in it.\n\^_")))
945
946 ;; Decode Babyl formatted part at the head of current buffer by
947 ;; rmail-file-coding-system, or if it is nil, do auto conversion.
948
949 (defun rmail-decode-babyl-format ()
950 (let ((modifiedp (buffer-modified-p))
951 (buffer-read-only nil)
952 (coding-system rmail-file-coding-system)
953 from to)
954 (goto-char (point-min))
955 (search-forward "\n\^_" nil t) ; Skip BABYL header.
956 (setq from (point))
957 (goto-char (point-max))
958 (search-backward "\n\^_" from 'mv)
959 (setq to (point))
960 (unless (and coding-system
961 (coding-system-p coding-system))
962 (setq coding-system
963 ;; If rmail-file-coding-system is nil, Emacs 21 writes
964 ;; RMAIL files in emacs-mule, Emacs 22 in utf-8, but
965 ;; earlier versions did that with the current buffer's
966 ;; encoding. So we want to favor detection of emacs-mule
967 ;; (whose normal priority is quite low) and utf-8, but
968 ;; still allow detection of other encodings if they won't
969 ;; fit. The call to with-coding-priority below achieves
970 ;; that.
971 (with-coding-priority '(emacs-mule utf-8)
972 (detect-coding-region from to 'highest))))
973 (unless (eq (coding-system-type coding-system) 'undecided)
974 (set-buffer-modified-p t) ; avoid locking when decoding
975 (let ((buffer-undo-list t))
976 (decode-coding-region from to coding-system))
977 (setq coding-system last-coding-system-used))
978 (set-buffer-modified-p modifiedp)
979 (setq buffer-file-coding-system nil)
980 (setq save-buffer-coding-system
981 (or coding-system 'undecided))))
982
983 (defvar rmail-mode-map nil)
984 (if rmail-mode-map
985 nil
986 (setq rmail-mode-map (make-keymap))
987 (suppress-keymap rmail-mode-map)
988 (define-key rmail-mode-map "a" 'rmail-add-label)
989 (define-key rmail-mode-map "b" 'rmail-bury)
990 (define-key rmail-mode-map "c" 'rmail-continue)
991 (define-key rmail-mode-map "d" 'rmail-delete-forward)
992 (define-key rmail-mode-map "\C-d" 'rmail-delete-backward)
993 (define-key rmail-mode-map "e" 'rmail-edit-current-message)
994 (define-key rmail-mode-map "f" 'rmail-forward)
995 (define-key rmail-mode-map "g" 'rmail-get-new-mail)
996 (define-key rmail-mode-map "h" 'rmail-summary)
997 (define-key rmail-mode-map "i" 'rmail-input)
998 (define-key rmail-mode-map "j" 'rmail-show-message)
999 (define-key rmail-mode-map "k" 'rmail-kill-label)
1000 (define-key rmail-mode-map "l" 'rmail-summary-by-labels)
1001 (define-key rmail-mode-map "\e\C-h" 'rmail-summary)
1002 (define-key rmail-mode-map "\e\C-l" 'rmail-summary-by-labels)
1003 (define-key rmail-mode-map "\e\C-r" 'rmail-summary-by-recipients)
1004 (define-key rmail-mode-map "\e\C-s" 'rmail-summary-by-regexp)
1005 (define-key rmail-mode-map "\e\C-t" 'rmail-summary-by-topic)
1006 (define-key rmail-mode-map "m" 'rmail-mail)
1007 (define-key rmail-mode-map "\em" 'rmail-retry-failure)
1008 (define-key rmail-mode-map "n" 'rmail-next-undeleted-message)
1009 (define-key rmail-mode-map "\en" 'rmail-next-message)
1010 (define-key rmail-mode-map "\e\C-n" 'rmail-next-labeled-message)
1011 (define-key rmail-mode-map "o" 'rmail-output-to-rmail-file)
1012 (define-key rmail-mode-map "\C-o" 'rmail-output)
1013 (define-key rmail-mode-map "p" 'rmail-previous-undeleted-message)
1014 (define-key rmail-mode-map "\ep" 'rmail-previous-message)
1015 (define-key rmail-mode-map "\e\C-p" 'rmail-previous-labeled-message)
1016 (define-key rmail-mode-map "q" 'rmail-quit)
1017 (define-key rmail-mode-map "r" 'rmail-reply)
1018 ;; I find I can't live without the default M-r command -- rms.
1019 ;; (define-key rmail-mode-map "\er" 'rmail-search-backwards)
1020 (define-key rmail-mode-map "s" 'rmail-expunge-and-save)
1021 (define-key rmail-mode-map "\es" 'rmail-search)
1022 (define-key rmail-mode-map "t" 'rmail-toggle-header)
1023 (define-key rmail-mode-map "u" 'rmail-undelete-previous-message)
1024 (define-key rmail-mode-map "w" 'rmail-output-body-to-file)
1025 (define-key rmail-mode-map "x" 'rmail-expunge)
1026 (define-key rmail-mode-map "." 'rmail-beginning-of-message)
1027 (define-key rmail-mode-map "/" 'rmail-end-of-message)
1028 (define-key rmail-mode-map "<" 'rmail-first-message)
1029 (define-key rmail-mode-map ">" 'rmail-last-message)
1030 (define-key rmail-mode-map " " 'scroll-up)
1031 (define-key rmail-mode-map "\177" 'scroll-down)
1032 (define-key rmail-mode-map "?" 'describe-mode)
1033 (define-key rmail-mode-map "\C-c\C-s\C-d" 'rmail-sort-by-date)
1034 (define-key rmail-mode-map "\C-c\C-s\C-s" 'rmail-sort-by-subject)
1035 (define-key rmail-mode-map "\C-c\C-s\C-a" 'rmail-sort-by-author)
1036 (define-key rmail-mode-map "\C-c\C-s\C-r" 'rmail-sort-by-recipient)
1037 (define-key rmail-mode-map "\C-c\C-s\C-c" 'rmail-sort-by-correspondent)
1038 (define-key rmail-mode-map "\C-c\C-s\C-l" 'rmail-sort-by-lines)
1039 (define-key rmail-mode-map "\C-c\C-s\C-k" 'rmail-sort-by-labels)
1040 (define-key rmail-mode-map "\C-c\C-n" 'rmail-next-same-subject)
1041 (define-key rmail-mode-map "\C-c\C-p" 'rmail-previous-same-subject)
1042 )
1043 \f
1044 (define-key rmail-mode-map [menu-bar] (make-sparse-keymap))
1045
1046 (define-key rmail-mode-map [menu-bar classify]
1047 (cons "Classify" (make-sparse-keymap "Classify")))
1048
1049 (define-key rmail-mode-map [menu-bar classify input-menu]
1050 nil)
1051
1052 (define-key rmail-mode-map [menu-bar classify output-menu]
1053 nil)
1054
1055 (define-key rmail-mode-map [menu-bar classify output-body]
1056 '("Output body to file..." . rmail-output-body-to-file))
1057
1058 (define-key rmail-mode-map [menu-bar classify output-inbox]
1059 '("Output (inbox)..." . rmail-output))
1060
1061 (define-key rmail-mode-map [menu-bar classify output]
1062 '("Output (Rmail)..." . rmail-output-to-rmail-file))
1063
1064 (define-key rmail-mode-map [menu-bar classify kill-label]
1065 '("Kill Label..." . rmail-kill-label))
1066
1067 (define-key rmail-mode-map [menu-bar classify add-label]
1068 '("Add Label..." . rmail-add-label))
1069
1070 (define-key rmail-mode-map [menu-bar summary]
1071 (cons "Summary" (make-sparse-keymap "Summary")))
1072
1073 (define-key rmail-mode-map [menu-bar summary senders]
1074 '("By Senders..." . rmail-summary-by-senders))
1075
1076 (define-key rmail-mode-map [menu-bar summary labels]
1077 '("By Labels..." . rmail-summary-by-labels))
1078
1079 (define-key rmail-mode-map [menu-bar summary recipients]
1080 '("By Recipients..." . rmail-summary-by-recipients))
1081
1082 (define-key rmail-mode-map [menu-bar summary topic]
1083 '("By Topic..." . rmail-summary-by-topic))
1084
1085 (define-key rmail-mode-map [menu-bar summary regexp]
1086 '("By Regexp..." . rmail-summary-by-regexp))
1087
1088 (define-key rmail-mode-map [menu-bar summary all]
1089 '("All" . rmail-summary))
1090
1091 (define-key rmail-mode-map [menu-bar mail]
1092 (cons "Mail" (make-sparse-keymap "Mail")))
1093
1094 (define-key rmail-mode-map [menu-bar mail rmail-get-new-mail]
1095 '("Get New Mail" . rmail-get-new-mail))
1096
1097 (define-key rmail-mode-map [menu-bar mail lambda]
1098 '("----"))
1099
1100 (define-key rmail-mode-map [menu-bar mail continue]
1101 '("Continue" . rmail-continue))
1102
1103 (define-key rmail-mode-map [menu-bar mail resend]
1104 '("Re-send..." . rmail-resend))
1105
1106 (define-key rmail-mode-map [menu-bar mail forward]
1107 '("Forward" . rmail-forward))
1108
1109 (define-key rmail-mode-map [menu-bar mail retry]
1110 '("Retry" . rmail-retry-failure))
1111
1112 (define-key rmail-mode-map [menu-bar mail reply]
1113 '("Reply" . rmail-reply))
1114
1115 (define-key rmail-mode-map [menu-bar mail mail]
1116 '("Mail" . rmail-mail))
1117
1118 (define-key rmail-mode-map [menu-bar delete]
1119 (cons "Delete" (make-sparse-keymap "Delete")))
1120
1121 (define-key rmail-mode-map [menu-bar delete expunge/save]
1122 '("Expunge/Save" . rmail-expunge-and-save))
1123
1124 (define-key rmail-mode-map [menu-bar delete expunge]
1125 '("Expunge" . rmail-expunge))
1126
1127 (define-key rmail-mode-map [menu-bar delete undelete]
1128 '("Undelete" . rmail-undelete-previous-message))
1129
1130 (define-key rmail-mode-map [menu-bar delete delete]
1131 '("Delete" . rmail-delete-forward))
1132
1133 (define-key rmail-mode-map [menu-bar move]
1134 (cons "Move" (make-sparse-keymap "Move")))
1135
1136 (define-key rmail-mode-map [menu-bar move search-back]
1137 '("Search Back..." . rmail-search-backwards))
1138
1139 (define-key rmail-mode-map [menu-bar move search]
1140 '("Search..." . rmail-search))
1141
1142 (define-key rmail-mode-map [menu-bar move previous]
1143 '("Previous Nondeleted" . rmail-previous-undeleted-message))
1144
1145 (define-key rmail-mode-map [menu-bar move next]
1146 '("Next Nondeleted" . rmail-next-undeleted-message))
1147
1148 (define-key rmail-mode-map [menu-bar move last]
1149 '("Last" . rmail-last-message))
1150
1151 (define-key rmail-mode-map [menu-bar move first]
1152 '("First" . rmail-first-message))
1153
1154 (define-key rmail-mode-map [menu-bar move previous]
1155 '("Previous" . rmail-previous-message))
1156
1157 (define-key rmail-mode-map [menu-bar move next]
1158 '("Next" . rmail-next-message))
1159
1160 ;; Rmail toolbar
1161 (defvar rmail-tool-bar-map
1162 (if (display-graphic-p)
1163 (let ((map (make-sparse-keymap)))
1164 (tool-bar-local-item-from-menu 'rmail-get-new-mail "mail/inbox"
1165 map rmail-mode-map)
1166 (tool-bar-local-item-from-menu 'rmail-next-undeleted-message "right-arrow"
1167 map rmail-mode-map)
1168 (tool-bar-local-item-from-menu 'rmail-previous-undeleted-message "left-arrow"
1169 map rmail-mode-map)
1170 (tool-bar-local-item-from-menu 'rmail-search "search"
1171 map rmail-mode-map)
1172 (tool-bar-local-item-from-menu 'rmail-input "open"
1173 map rmail-mode-map)
1174 (tool-bar-local-item-from-menu 'rmail-mail "mail/compose"
1175 map rmail-mode-map)
1176 (tool-bar-local-item-from-menu 'rmail-reply "mail/reply-all"
1177 map rmail-mode-map)
1178 (tool-bar-local-item-from-menu 'rmail-forward "mail/forward"
1179 map rmail-mode-map)
1180 (tool-bar-local-item-from-menu 'rmail-delete-forward "close"
1181 map rmail-mode-map)
1182 (tool-bar-local-item-from-menu 'rmail-output "mail/move"
1183 map rmail-mode-map)
1184 (tool-bar-local-item-from-menu 'rmail-output-body-to-file "mail/save"
1185 map rmail-mode-map)
1186 (tool-bar-local-item-from-menu 'rmail-expunge "delete"
1187 map rmail-mode-map)
1188 map)))
1189
1190
1191 \f
1192 ;; Rmail mode is suitable only for specially formatted data.
1193 (put 'rmail-mode 'mode-class 'special)
1194
1195 (defun rmail-mode-kill-summary ()
1196 (if rmail-summary-buffer (kill-buffer rmail-summary-buffer)))
1197
1198 ;;;###autoload
1199 (defun rmail-mode ()
1200 "Rmail Mode is used by \\<rmail-mode-map>\\[rmail] for editing Rmail files.
1201 All normal editing commands are turned off.
1202 Instead, these commands are available:
1203
1204 \\[rmail-beginning-of-message] Move point to front of this message.
1205 \\[rmail-end-of-message] Move point to bottom of this message.
1206 \\[scroll-up] Scroll to next screen of this message.
1207 \\[scroll-down] Scroll to previous screen of this message.
1208 \\[rmail-next-undeleted-message] Move to Next non-deleted message.
1209 \\[rmail-previous-undeleted-message] Move to Previous non-deleted message.
1210 \\[rmail-next-message] Move to Next message whether deleted or not.
1211 \\[rmail-previous-message] Move to Previous message whether deleted or not.
1212 \\[rmail-first-message] Move to the first message in Rmail file.
1213 \\[rmail-last-message] Move to the last message in Rmail file.
1214 \\[rmail-show-message] Jump to message specified by numeric position in file.
1215 \\[rmail-search] Search for string and show message it is found in.
1216 \\[rmail-delete-forward] Delete this message, move to next nondeleted.
1217 \\[rmail-delete-backward] Delete this message, move to previous nondeleted.
1218 \\[rmail-undelete-previous-message] Undelete message. Tries current message, then earlier messages
1219 till a deleted message is found.
1220 \\[rmail-edit-current-message] Edit the current message. \\[rmail-cease-edit] to return to Rmail.
1221 \\[rmail-expunge] Expunge deleted messages.
1222 \\[rmail-expunge-and-save] Expunge and save the file.
1223 \\[rmail-quit] Quit Rmail: expunge, save, then switch to another buffer.
1224 \\[save-buffer] Save without expunging.
1225 \\[rmail-get-new-mail] Move new mail from system spool directory into this file.
1226 \\[rmail-mail] Mail a message (same as \\[mail-other-window]).
1227 \\[rmail-continue] Continue composing outgoing message started before.
1228 \\[rmail-reply] Reply to this message. Like \\[rmail-mail] but initializes some fields.
1229 \\[rmail-retry-failure] Send this message again. Used on a mailer failure message.
1230 \\[rmail-forward] Forward this message to another user.
1231 \\[rmail-output-to-rmail-file] Output this message to an Rmail file (append it).
1232 \\[rmail-output] Output this message to a Unix-format mail file (append it).
1233 \\[rmail-output-body-to-file] Save message body to a file. Default filename comes from Subject line.
1234 \\[rmail-input] Input Rmail file. Run Rmail on that file.
1235 \\[rmail-add-label] Add label to message. It will be displayed in the mode line.
1236 \\[rmail-kill-label] Kill label. Remove a label from current message.
1237 \\[rmail-next-labeled-message] Move to Next message with specified label
1238 (label defaults to last one specified).
1239 Standard labels: filed, unseen, answered, forwarded, deleted.
1240 Any other label is present only if you add it with \\[rmail-add-label].
1241 \\[rmail-previous-labeled-message] Move to Previous message with specified label
1242 \\[rmail-summary] Show headers buffer, with a one line summary of each message.
1243 \\[rmail-summary-by-labels] Summarize only messages with particular label(s).
1244 \\[rmail-summary-by-recipients] Summarize only messages with particular recipient(s).
1245 \\[rmail-summary-by-regexp] Summarize only messages with particular regexp(s).
1246 \\[rmail-summary-by-topic] Summarize only messages with subject line regexp(s).
1247 \\[rmail-toggle-header] Toggle display of complete header."
1248 (interactive)
1249 (let ((finding-rmail-file (not (eq major-mode 'rmail-mode))))
1250 (rmail-mode-2)
1251 (when (and finding-rmail-file
1252 (null coding-system-for-read)
1253 default-enable-multibyte-characters)
1254 (let ((rmail-enable-multibyte t))
1255 (rmail-require-mime-maybe)
1256 (rmail-convert-file)
1257 (goto-char (point-max))
1258 (set-buffer-multibyte t)))
1259 (rmail-set-message-counters)
1260 (rmail-show-message rmail-total-messages)
1261 (when finding-rmail-file
1262 (when rmail-display-summary
1263 (rmail-summary))
1264 (rmail-construct-io-menu))
1265 (run-mode-hooks 'rmail-mode-hook)))
1266
1267 (defun rmail-mode-2 ()
1268 (kill-all-local-variables)
1269 (rmail-mode-1)
1270 (rmail-perm-variables)
1271 (rmail-variables))
1272
1273 (defun rmail-mode-1 ()
1274 (setq major-mode 'rmail-mode)
1275 (setq mode-name "RMAIL")
1276 (setq buffer-read-only t)
1277 ;; No need to auto save RMAIL files in normal circumstances
1278 ;; because they contain no info except attribute changes
1279 ;; and deletion of messages.
1280 ;; The one exception is when messages are copied into an Rmail mode buffer.
1281 ;; rmail-output-to-rmail-file enables auto save when you do that.
1282 (setq buffer-auto-save-file-name nil)
1283 (setq mode-line-modified "--")
1284 (use-local-map rmail-mode-map)
1285 (set-syntax-table text-mode-syntax-table)
1286 (setq local-abbrev-table text-mode-abbrev-table))
1287
1288 ;; Set up the permanent locals associated with an Rmail file.
1289 (defun rmail-perm-variables ()
1290 (make-local-variable 'rmail-last-label)
1291 (make-local-variable 'rmail-last-regexp)
1292 (make-local-variable 'rmail-deleted-vector)
1293 (make-local-variable 'rmail-buffer)
1294 (setq rmail-buffer (current-buffer))
1295 (make-local-variable 'rmail-view-buffer)
1296 (setq rmail-view-buffer rmail-buffer)
1297 (make-local-variable 'rmail-summary-buffer)
1298 (make-local-variable 'rmail-summary-vector)
1299 (make-local-variable 'rmail-current-message)
1300 (make-local-variable 'rmail-total-messages)
1301 (make-local-variable 'rmail-overlay-list)
1302 (setq rmail-overlay-list nil)
1303 (make-local-variable 'rmail-message-vector)
1304 (make-local-variable 'rmail-msgref-vector)
1305 (make-local-variable 'rmail-inbox-list)
1306 (setq rmail-inbox-list (rmail-parse-file-inboxes))
1307 ;; Provide default set of inboxes for primary mail file ~/RMAIL.
1308 (and (null rmail-inbox-list)
1309 (or (equal buffer-file-name (expand-file-name rmail-file-name))
1310 (equal buffer-file-truename
1311 (abbreviate-file-name (file-truename rmail-file-name))))
1312 (setq rmail-inbox-list
1313 (or rmail-primary-inbox-list
1314 (list (or (getenv "MAIL")
1315 (concat rmail-spool-directory
1316 (user-login-name)))))))
1317 (make-local-variable 'rmail-keywords)
1318 (set (make-local-variable 'tool-bar-map) rmail-tool-bar-map)
1319 ;; this gets generated as needed
1320 (setq rmail-keywords nil))
1321
1322 ;; Set up the non-permanent locals associated with Rmail mode.
1323 (defun rmail-variables ()
1324 (make-local-variable 'save-buffer-coding-system)
1325 ;; If we don't already have a value for save-buffer-coding-system,
1326 ;; get it from buffer-file-coding-system, and clear that
1327 ;; because it should be determined in rmail-show-message.
1328 (unless save-buffer-coding-system
1329 (setq save-buffer-coding-system (or buffer-file-coding-system 'undecided))
1330 (setq buffer-file-coding-system nil))
1331 ;; Don't let a local variables list in a message cause confusion.
1332 (make-local-variable 'local-enable-local-variables)
1333 (setq local-enable-local-variables nil)
1334 (make-local-variable 'revert-buffer-function)
1335 (setq revert-buffer-function 'rmail-revert)
1336 (make-local-variable 'font-lock-defaults)
1337 (setq font-lock-defaults
1338 '(rmail-font-lock-keywords
1339 t t nil nil
1340 (font-lock-maximum-size . nil)
1341 (font-lock-fontify-buffer-function . rmail-fontify-buffer-function)
1342 (font-lock-unfontify-buffer-function . rmail-unfontify-buffer-function)
1343 (font-lock-inhibit-thing-lock . (lazy-lock-mode fast-lock-mode))))
1344 (make-local-variable 'require-final-newline)
1345 (setq require-final-newline nil)
1346 (make-local-variable 'version-control)
1347 (setq version-control 'never)
1348 (make-local-variable 'kill-buffer-hook)
1349 (add-hook 'kill-buffer-hook 'rmail-mode-kill-summary)
1350 (make-local-variable 'file-precious-flag)
1351 (setq file-precious-flag t)
1352 (make-local-variable 'desktop-save-buffer)
1353 (setq desktop-save-buffer t))
1354
1355 ;; Handle M-x revert-buffer done in an rmail-mode buffer.
1356 (defun rmail-revert (arg noconfirm)
1357 (set-buffer rmail-buffer)
1358 (let* ((revert-buffer-function (default-value 'revert-buffer-function))
1359 (rmail-enable-multibyte enable-multibyte-characters)
1360 ;; See similar code in `rmail'.
1361 (coding-system-for-read (and rmail-enable-multibyte 'raw-text)))
1362 ;; Call our caller again, but this time it does the default thing.
1363 (if (revert-buffer arg noconfirm)
1364 ;; If the user said "yes", and we changed something,
1365 ;; reparse the messages.
1366 (progn
1367 (set-buffer rmail-buffer)
1368 (rmail-mode-2)
1369 ;; Convert all or part to Babyl file if possible.
1370 (rmail-convert-file)
1371 ;; We have read the file as raw-text, so the buffer is set to
1372 ;; unibyte. Make it multibyte if necessary.
1373 (if (and rmail-enable-multibyte
1374 (not enable-multibyte-characters))
1375 (set-buffer-multibyte t))
1376 (goto-char (point-max))
1377 (rmail-set-message-counters)
1378 (rmail-show-message rmail-total-messages)
1379 (run-hooks 'rmail-mode-hook)))))
1380
1381 ;; Return a list of files from this buffer's Mail: option.
1382 ;; Does not assume that messages have been parsed.
1383 ;; Just returns nil if buffer does not look like Babyl format.
1384 (defun rmail-parse-file-inboxes ()
1385 (save-excursion
1386 (save-restriction
1387 (widen)
1388 (goto-char 1)
1389 (cond ((looking-at "BABYL OPTIONS:")
1390 (search-forward "\n\^_" nil 'move)
1391 (narrow-to-region 1 (point))
1392 (goto-char 1)
1393 (if (search-forward "\nMail:" nil t)
1394 (progn
1395 (narrow-to-region (point) (progn (end-of-line) (point)))
1396 (goto-char (point-min))
1397 (mail-parse-comma-list))))))))
1398
1399 (defun rmail-expunge-and-save ()
1400 "Expunge and save RMAIL file."
1401 (interactive)
1402 (rmail-expunge)
1403 (set-buffer rmail-buffer)
1404 (save-buffer)
1405 (if (rmail-summary-exists)
1406 (rmail-select-summary (set-buffer-modified-p nil))))
1407
1408 (defun rmail-quit ()
1409 "Quit out of RMAIL.
1410 Hook `rmail-quit-hook' is run after expunging."
1411 (interactive)
1412 (rmail-expunge-and-save)
1413 (when (boundp 'rmail-quit-hook)
1414 (run-hooks 'rmail-quit-hook))
1415 ;; Don't switch to the summary buffer even if it was recently visible.
1416 (when rmail-summary-buffer
1417 (replace-buffer-in-windows rmail-summary-buffer)
1418 (bury-buffer rmail-summary-buffer))
1419 (if rmail-enable-mime
1420 (let ((obuf rmail-buffer)
1421 (ovbuf rmail-view-buffer))
1422 (set-buffer rmail-view-buffer)
1423 (quit-window)
1424 (replace-buffer-in-windows ovbuf)
1425 (replace-buffer-in-windows obuf)
1426 (bury-buffer obuf))
1427 (let ((obuf (current-buffer)))
1428 (quit-window)
1429 (replace-buffer-in-windows obuf))))
1430
1431 (defun rmail-bury ()
1432 "Bury current Rmail buffer and its summary buffer."
1433 (interactive)
1434 ;; This let var was called rmail-buffer, but that interfered
1435 ;; with the buffer-local var used in summary buffers.
1436 (let ((buffer-to-bury (current-buffer)))
1437 (if (rmail-summary-exists)
1438 (let (window)
1439 (while (setq window (get-buffer-window rmail-summary-buffer))
1440 (quit-window nil window))
1441 (bury-buffer rmail-summary-buffer)))
1442 (quit-window)))
1443
1444 (defun rmail-duplicate-message ()
1445 "Create a duplicated copy of the current message.
1446 The duplicate copy goes into the Rmail file just after the
1447 original copy."
1448 (interactive)
1449 (widen)
1450 (let ((buffer-read-only nil)
1451 (number rmail-current-message)
1452 (string (buffer-substring (rmail-msgbeg rmail-current-message)
1453 (rmail-msgend rmail-current-message))))
1454 (goto-char (rmail-msgend rmail-current-message))
1455 (insert string)
1456 (rmail-forget-messages)
1457 (rmail-show-message number)
1458 (message "Message duplicated")))
1459
1460 ;;;###autoload
1461 (defun rmail-input (filename)
1462 "Run Rmail on file FILENAME."
1463 (interactive "FRun rmail on RMAIL file: ")
1464 (rmail filename))
1465
1466
1467 ;; This used to scan subdirectories recursively, but someone pointed out
1468 ;; that if the user wants that, person can put all the files in one dir.
1469 ;; And the recursive scan was slow. So I took it out.
1470 ;; rms, Sep 1996.
1471 (defun rmail-find-all-files (start)
1472 "Return list of file in dir START that match `rmail-secondary-file-regexp'."
1473 (if (file-accessible-directory-p start)
1474 ;; Don't sort here.
1475 (let* ((case-fold-search t)
1476 (files (directory-files start t rmail-secondary-file-regexp)))
1477 ;; Sort here instead of in directory-files
1478 ;; because this list is usually much shorter.
1479 (sort files 'string<))))
1480
1481 (defun rmail-list-to-menu (menu-name l action &optional full-name)
1482 (let ((menu (make-sparse-keymap menu-name)))
1483 (mapc
1484 (function (lambda (item)
1485 (let (command)
1486 (if (consp item)
1487 (progn
1488 (setq command
1489 (rmail-list-to-menu (car item) (cdr item)
1490 action
1491 (if full-name
1492 (concat full-name "/"
1493 (car item))
1494 (car item))))
1495 (setq name (car item)))
1496 (progn
1497 (setq name item)
1498 (setq command
1499 (list 'lambda () '(interactive)
1500 (list action
1501 (expand-file-name
1502 (if full-name
1503 (concat full-name "/" item)
1504 item)
1505 rmail-secondary-file-directory))))))
1506 (define-key menu (vector (intern name))
1507 (cons name command)))))
1508 (reverse l))
1509 menu))
1510
1511 ;; This command is always "disabled" when it appears in a menu.
1512 (put 'rmail-disable-menu 'menu-enable ''nil)
1513
1514 (defun rmail-construct-io-menu ()
1515 (let ((files (rmail-find-all-files rmail-secondary-file-directory)))
1516 (if files
1517 (progn
1518 (define-key rmail-mode-map [menu-bar classify input-menu]
1519 (cons "Input Rmail File"
1520 (rmail-list-to-menu "Input Rmail File"
1521 files
1522 'rmail-input)))
1523 (define-key rmail-mode-map [menu-bar classify output-menu]
1524 (cons "Output Rmail File"
1525 (rmail-list-to-menu "Output Rmail File"
1526 files
1527 'rmail-output-to-rmail-file))))
1528
1529 (define-key rmail-mode-map [menu-bar classify input-menu]
1530 '("Input Rmail File" . rmail-disable-menu))
1531 (define-key rmail-mode-map [menu-bar classify output-menu]
1532 '("Output Rmail File" . rmail-disable-menu)))))
1533
1534 \f
1535 ;;;; *** Rmail input ***
1536
1537 (declare-function rmail-spam-filter "rmail-spam-filter" (msg))
1538 (declare-function rmail-summary-goto-msg "rmailsum" (&optional n nowarn skip-rmail))
1539 (declare-function rmail-summary-mark-undeleted "rmailsum" (n))
1540 (declare-function rmail-summary-mark-deleted "rmailsum" (&optional n undel))
1541 (declare-function rfc822-addresses "rfc822" (header-text))
1542 (declare-function mail-abbrev-make-syntax-table "mailabbrev.el" ())
1543 (declare-function mail-sendmail-delimit-header "sendmail" ())
1544 (declare-function mail-header-end "sendmail" ())
1545
1546 ;; RLK feature not added in this version:
1547 ;; argument specifies inbox file or files in various ways.
1548
1549 (defun rmail-get-new-mail (&optional file-name)
1550 "Move any new mail from this RMAIL file's inbox files.
1551 The inbox files can be specified with the file's Mail: option. The
1552 variable `rmail-primary-inbox-list' specifies the inboxes for your
1553 primary RMAIL file if it has no Mail: option. By default, this is
1554 your /usr/spool/mail/$USER.
1555
1556 You can also specify the file to get new mail from. In this case, the
1557 file of new mail is not changed or deleted. Noninteractively, you can
1558 pass the inbox file name as an argument. Interactively, a prefix
1559 argument causes us to read a file name and use that file as the inbox.
1560
1561 If the variable `rmail-preserve-inbox' is non-nil, new mail will
1562 always be left in inbox files rather than deleted.
1563
1564 This function runs `rmail-get-new-mail-hook' before saving the updated file.
1565 It returns t if it got any new messages."
1566 (interactive
1567 (list (if current-prefix-arg
1568 (read-file-name "Get new mail from file: "))))
1569 (run-hooks 'rmail-before-get-new-mail-hook)
1570 ;; If the disk file has been changed from under us,
1571 ;; revert to it before we get new mail.
1572 (or (verify-visited-file-modtime (current-buffer))
1573 (find-file (buffer-file-name)))
1574 (set-buffer rmail-buffer)
1575 (rmail-maybe-set-message-counters)
1576 (widen)
1577 ;; Get rid of all undo records for this buffer.
1578 (or (eq buffer-undo-list t)
1579 (setq buffer-undo-list nil))
1580 (let ((all-files (if file-name (list file-name)
1581 rmail-inbox-list))
1582 (rmail-enable-multibyte (default-value 'enable-multibyte-characters))
1583 found)
1584 (unwind-protect
1585 (progn
1586 (while all-files
1587 (let ((opoint (point))
1588 (new-messages 0)
1589 (rsf-number-of-spam 0)
1590 (delete-files ())
1591 ;; If buffer has not changed yet, and has not been saved yet,
1592 ;; don't replace the old backup file now.
1593 (make-backup-files (and make-backup-files (buffer-modified-p)))
1594 (buffer-read-only nil)
1595 ;; Don't make undo records for what we do in getting mail.
1596 (buffer-undo-list t)
1597 success
1598 ;; Files to insert this time around.
1599 files
1600 ;; Last names of those files.
1601 file-last-names)
1602 ;; Pull files off all-files onto files
1603 ;; as long as there is no name conflict.
1604 ;; A conflict happens when two inbox file names
1605 ;; have the same last component.
1606 (while (and all-files
1607 (not (member (file-name-nondirectory (car all-files))
1608 file-last-names)))
1609 (setq files (cons (car all-files) files)
1610 file-last-names
1611 (cons (file-name-nondirectory (car all-files)) files))
1612 (setq all-files (cdr all-files)))
1613 ;; Put them back in their original order.
1614 (setq files (nreverse files))
1615
1616 (goto-char (point-max))
1617 (skip-chars-backward " \t\n") ; just in case of brain damage
1618 (delete-region (point) (point-max)) ; caused by require-final-newline
1619 (save-excursion
1620 (save-restriction
1621 (narrow-to-region (point) (point))
1622 ;; Read in the contents of the inbox files,
1623 ;; renaming them as necessary,
1624 ;; and adding to the list of files to delete eventually.
1625 (if file-name
1626 (rmail-insert-inbox-text files nil)
1627 (setq delete-files (rmail-insert-inbox-text files t)))
1628 ;; Scan the new text and convert each message to babyl format.
1629 (goto-char (point-min))
1630 (unwind-protect
1631 (save-excursion
1632 (setq new-messages (rmail-convert-to-babyl-format)
1633 success t))
1634 ;; Try to delete the garbage just inserted.
1635 (or success (delete-region (point-min) (point-max)))
1636 ;; If we could not convert the file's inboxes,
1637 ;; rename the files we tried to read
1638 ;; so we won't over and over again.
1639 (if (and (not file-name) (not success))
1640 (let ((delfiles delete-files)
1641 (count 0))
1642 (while delfiles
1643 (while (file-exists-p (format "RMAILOSE.%d" count))
1644 (setq count (1+ count)))
1645 (rename-file (car delfiles)
1646 (format "RMAILOSE.%d" count))
1647 (setq delfiles (cdr delfiles))))))
1648 (or (zerop new-messages)
1649 (let (success)
1650 (widen)
1651 (search-backward "\n\^_" nil t)
1652 (narrow-to-region (point) (point-max))
1653 (goto-char (1+ (point-min)))
1654 (rmail-count-new-messages)
1655 (run-hooks 'rmail-get-new-mail-hook)
1656 (save-buffer)))
1657 ;; Delete the old files, now that babyl file is saved.
1658 (while delete-files
1659 (condition-case ()
1660 ;; First, try deleting.
1661 (condition-case ()
1662 (delete-file (car delete-files))
1663 (file-error
1664 ;; If we can't delete it, truncate it.
1665 (write-region (point) (point) (car delete-files))))
1666 (file-error nil))
1667 (setq delete-files (cdr delete-files)))))
1668 (if (= new-messages 0)
1669 (progn (goto-char opoint)
1670 (if (or file-name rmail-inbox-list)
1671 (message "(No new mail has arrived)")))
1672 ;; check new messages to see if any of them is spam:
1673 (if (and (featurep 'rmail-spam-filter)
1674 rmail-use-spam-filter)
1675 (let*
1676 ((old-messages (- rmail-total-messages new-messages))
1677 (rsf-scanned-message-number (1+ old-messages))
1678 ;; save deletion flags of old messages: vector starts
1679 ;; at zero (is one longer that no of messages),
1680 ;; therefore take 1+ old-messages
1681 (save-deleted
1682 (substring rmail-deleted-vector 0 (1+
1683 old-messages))))
1684 ;; set all messages to undeleted
1685 (setq rmail-deleted-vector
1686 (make-string (1+ rmail-total-messages) ?\ ))
1687 (while (<= rsf-scanned-message-number
1688 rmail-total-messages)
1689 (progn
1690 (if (not (rmail-spam-filter rsf-scanned-message-number))
1691 (progn (setq rsf-number-of-spam (1+ rsf-number-of-spam)))
1692 )
1693 (setq rsf-scanned-message-number (1+ rsf-scanned-message-number))
1694 ))
1695 (if (> rsf-number-of-spam 0)
1696 (progn
1697 (when (rmail-expunge-confirmed)
1698 (rmail-only-expunge t))
1699 ))
1700 (setq rmail-deleted-vector
1701 (concat
1702 save-deleted
1703 (make-string (- rmail-total-messages old-messages)
1704 ?\ )))
1705 ))
1706 (if (rmail-summary-exists)
1707 (rmail-select-summary
1708 (rmail-update-summary)))
1709 (message "%d new message%s read%s"
1710 new-messages (if (= 1 new-messages) "" "s")
1711 ;; print out a message on number of spam messages found:
1712 (if (and (featurep 'rmail-spam-filter)
1713 rmail-use-spam-filter
1714 (> rsf-number-of-spam 0))
1715 (cond ((= 1 new-messages)
1716 ", and appears to be spam")
1717 ((= rsf-number-of-spam new-messages)
1718 ", and all appear to be spam")
1719 ((> rsf-number-of-spam 1)
1720 (format ", and %d appear to be spam"
1721 rsf-number-of-spam))
1722 (t
1723 ", and 1 appears to be spam"))
1724 ""))
1725 (if (and (featurep 'rmail-spam-filter)
1726 rmail-use-spam-filter
1727 (> rsf-number-of-spam 0))
1728 (progn (if rsf-beep (beep t))
1729 (sleep-for rsf-sleep-after-message)))
1730
1731 ;; Move to the first new message
1732 ;; unless we have other unseen messages before it.
1733 (rmail-show-message (rmail-first-unseen-message))
1734 (run-hooks 'rmail-after-get-new-mail-hook)
1735 (setq found t))))
1736 found)
1737 ;; Don't leave the buffer screwed up if we get a disk-full error.
1738 (or found (rmail-show-message)))))
1739
1740 (defun rmail-parse-url (file)
1741 "Parse the supplied URL. Return (list MAILBOX-NAME REMOTE PASSWORD GOT-PASSWORD)
1742 WHERE MAILBOX-NAME is the name of the mailbox suitable as argument to the
1743 actual version of `movemail', REMOTE is non-nil if MAILBOX-NAME refers to
1744 a remote mailbox, PASSWORD is the password if it should be
1745 supplied as a separate argument to `movemail' or nil otherwise, GOT-PASSWORD
1746 is non-nil if the user has supplied the password interactively.
1747 "
1748 (cond
1749 ((string-match "^\\([^:]+\\)://\\(\\([^:@]+\\)\\(:\\([^@]+\\)\\)?@\\)?.*" file)
1750 (let (got-password supplied-password
1751 (proto (match-string 1 file))
1752 (user (match-string 3 file))
1753 (pass (match-string 5 file))
1754 (host (substring file (or (match-end 2)
1755 (+ 3 (match-end 1))))))
1756
1757 (if (not pass)
1758 (when rmail-remote-password-required
1759 (setq got-password (not (rmail-have-password)))
1760 (setq supplied-password (rmail-get-remote-password
1761 (string-equal proto "imap")))))
1762
1763 (if (rmail-movemail-variant-p 'emacs)
1764 (if (string-equal proto "pop")
1765 (list (concat "po:" user ":" host)
1766 t
1767 (or pass supplied-password)
1768 got-password)
1769 (error "Emacs movemail does not support %s protocol" proto))
1770 (list file
1771 (or (string-equal proto "pop") (string-equal proto "imap"))
1772 supplied-password
1773 got-password))))
1774
1775 ((string-match "^po:\\([^:]+\\)\\(:\\(.*\\)\\)?" file)
1776 (let (got-password supplied-password
1777 (proto "pop")
1778 (user (match-string 1 file))
1779 (host (match-string 3 file)))
1780
1781 (when rmail-remote-password-required
1782 (setq got-password (not (rmail-have-password)))
1783 (setq supplied-password (rmail-get-remote-password nil)))
1784
1785 (list file "pop" supplied-password got-password)))
1786
1787 (t
1788 (list file nil nil nil))))
1789
1790 (defun rmail-insert-inbox-text (files renamep)
1791 ;; Detect a locked file now, so that we avoid moving mail
1792 ;; out of the real inbox file. (That could scare people.)
1793 (or (memq (file-locked-p buffer-file-name) '(nil t))
1794 (error "RMAIL file %s is locked"
1795 (file-name-nondirectory buffer-file-name)))
1796 (let (file tofile delete-files movemail popmail got-password password)
1797 (while files
1798 ;; Handle remote mailbox names specially; don't expand as filenames
1799 ;; in case the userid contains a directory separator.
1800 (setq file (car files))
1801 (let ((url-data (rmail-parse-url file)))
1802 (setq file (nth 0 url-data))
1803 (setq popmail (nth 1 url-data))
1804 (setq password (nth 2 url-data))
1805 (setq got-password (nth 3 url-data)))
1806
1807 (if popmail
1808 (setq renamep t)
1809 (setq file (file-truename
1810 (substitute-in-file-name (expand-file-name file)))))
1811 (setq tofile (expand-file-name
1812 ;; Generate name to move to from inbox name,
1813 ;; in case of multiple inboxes that need moving.
1814 (concat ".newmail-"
1815 (file-name-nondirectory
1816 (if (memq system-type '(windows-nt cygwin))
1817 ;; cannot have "po:" in file name
1818 (substring file 3)
1819 file)))
1820 ;; Use the directory of this rmail file
1821 ;; because it's a nuisance to use the homedir
1822 ;; if that is on a full disk and this rmail
1823 ;; file isn't.
1824 (file-name-directory
1825 (expand-file-name buffer-file-name))))
1826 ;; Always use movemail to rename the file,
1827 ;; since there can be mailboxes in various directories.
1828 (if (not popmail)
1829 (progn
1830 ;; On some systems, /usr/spool/mail/foo is a directory
1831 ;; and the actual inbox is /usr/spool/mail/foo/foo.
1832 (if (file-directory-p file)
1833 (setq file (expand-file-name (user-login-name)
1834 file)))))
1835 (cond (popmail
1836 (message "Getting mail from the remote server ..."))
1837 ((and (file-exists-p tofile)
1838 (/= 0 (nth 7 (file-attributes tofile))))
1839 (message "Getting mail from %s..." tofile))
1840 ((and (file-exists-p file)
1841 (/= 0 (nth 7 (file-attributes file))))
1842 (message "Getting mail from %s..." file)))
1843 ;; Set TOFILE if have not already done so, and
1844 ;; rename or copy the file FILE to TOFILE if and as appropriate.
1845 (cond ((not renamep)
1846 (setq tofile file))
1847 ((or (file-exists-p tofile) (and (not popmail)
1848 (not (file-exists-p file))))
1849 nil)
1850 (t
1851 (with-temp-buffer
1852 (let ((errors (current-buffer)))
1853 (buffer-disable-undo errors)
1854 (let ((args
1855 (append
1856 (list rmail-movemail-program nil errors nil)
1857 (if rmail-preserve-inbox
1858 (list "-p")
1859 nil)
1860 (if (rmail-movemail-variant-p 'mailutils)
1861 (append (list "--emacs") rmail-movemail-flags)
1862 rmail-movemail-flags)
1863 (list file tofile)
1864 (if password (list password) nil))))
1865 (apply 'call-process args))
1866 (if (not (buffer-modified-p errors))
1867 ;; No output => movemail won
1868 nil
1869 (set-buffer errors)
1870 (subst-char-in-region (point-min) (point-max)
1871 ?\n ?\ )
1872 (goto-char (point-max))
1873 (skip-chars-backward " \t")
1874 (delete-region (point) (point-max))
1875 (goto-char (point-min))
1876 (if (looking-at "movemail: ")
1877 (delete-region (point-min) (match-end 0)))
1878 (beep t)
1879 ;; If we just read the password, most likely it is
1880 ;; wrong. Otherwise, see if there is a specific
1881 ;; reason to think that the problem is a wrong passwd.
1882 (if (or got-password
1883 (re-search-forward rmail-remote-password-error
1884 nil t))
1885 (rmail-set-remote-password nil))
1886
1887 ;; If using Mailutils, remove initial error code
1888 ;; abbreviation
1889 (when (rmail-movemail-variant-p 'mailutils)
1890 (goto-char (point-min))
1891 (when (looking-at "[A-Z][A-Z0-9_]*:")
1892 (delete-region (point-min) (match-end 0))))
1893
1894 (message "movemail: %s"
1895 (buffer-substring (point-min)
1896 (point-max)))
1897
1898 (sit-for 3)
1899 nil)))))
1900
1901 ;; At this point, TOFILE contains the name to read:
1902 ;; Either the alternate name (if we renamed)
1903 ;; or the actual inbox (if not renaming).
1904 (if (file-exists-p tofile)
1905 (let ((coding-system-for-read 'no-conversion)
1906 size)
1907 (goto-char (point-max))
1908 (setq size (nth 1 (insert-file-contents tofile)))
1909 (goto-char (point-max))
1910 (or (= (preceding-char) ?\n)
1911 (zerop size)
1912 (insert ?\n))
1913 (if (not (and rmail-preserve-inbox (string= file tofile)))
1914 (setq delete-files (cons tofile delete-files)))))
1915 (message "")
1916 (setq files (cdr files)))
1917 delete-files))
1918
1919 ;; Decode the region specified by FROM and TO by CODING.
1920 ;; If CODING is nil or an invalid coding system, decode by `undecided'.
1921 (defun rmail-decode-region (from to coding)
1922 (if (or (not coding) (not (coding-system-p coding)))
1923 (setq coding 'undecided))
1924 ;; Use -dos decoding, to remove ^M characters left from base64 or
1925 ;; rogue qp-encoded text.
1926 (decode-coding-region from to
1927 (coding-system-change-eol-conversion coding 1))
1928 ;; Don't reveal the fact we used -dos decoding, as users generally
1929 ;; will not expect the RMAIL buffer to use DOS EOL format.
1930 (setq buffer-file-coding-system
1931 (setq last-coding-system-used
1932 (coding-system-change-eol-conversion coding 0))))
1933
1934 ;; the rmail-break-forwarded-messages feature is not implemented
1935 (defun rmail-convert-to-babyl-format ()
1936 (let ((count 0) start
1937 (case-fold-search nil)
1938 (buffer-undo-list t)
1939 (invalid-input-resync
1940 (function (lambda ()
1941 (message "Invalid Babyl format in inbox!")
1942 (sit-for 3)
1943 ;; Try to get back in sync with a real message.
1944 (if (re-search-forward
1945 (concat rmail-mmdf-delim1 "\\|^From") nil t)
1946 (beginning-of-line)
1947 (goto-char (point-max)))))))
1948 (goto-char (point-min))
1949 (save-restriction
1950 (while (not (eobp))
1951 (setq start (point))
1952 (cond ((looking-at "BABYL OPTIONS:") ;Babyl header
1953 (if (search-forward "\n\^_" nil t)
1954 ;; If we find the proper terminator, delete through there.
1955 (delete-region (point-min) (point))
1956 (funcall invalid-input-resync)
1957 (delete-region (point-min) (point))))
1958 ;; Babyl format message
1959 ((looking-at "\^L")
1960 (or (search-forward "\n\^_" nil t)
1961 (funcall invalid-input-resync))
1962 (setq count (1+ count))
1963 ;; Make sure there is no extra white space after the ^_
1964 ;; at the end of the message.
1965 ;; Narrowing will make sure that whatever follows the junk
1966 ;; will be treated properly.
1967 (delete-region (point)
1968 (save-excursion
1969 (skip-chars-forward " \t\n")
1970 (point)))
1971 ;; The following let* form was wrapped in a `save-excursion'
1972 ;; which in one case caused infinite looping, see:
1973 ;; http://lists.gnu.org/archive/html/emacs-devel/2008-01/msg00968.html
1974 ;; Removing that form leaves `point' at the end of the
1975 ;; region decoded by `rmail-decode-region' which should
1976 ;; be correct.
1977 (let* ((header-end
1978 (progn
1979 (save-excursion
1980 (goto-char start)
1981 (forward-line 1)
1982 (if (looking-at "0")
1983 (forward-line 1)
1984 (forward-line 2))
1985 (save-restriction
1986 (narrow-to-region (point) (point-max))
1987 (rfc822-goto-eoh)
1988 (point)))))
1989 (case-fold-search t)
1990 (quoted-printable-header-field-end
1991 (save-excursion
1992 (goto-char start)
1993 (re-search-forward
1994 "^content-transfer-encoding:\\(\n?[\t ]\\)*quoted-printable\\(\n?[\t ]\\)*"
1995 header-end t)))
1996 (base64-header-field-end
1997 (save-excursion
1998 (goto-char start)
1999 ;; Don't try to decode non-text data.
2000 (and (re-search-forward
2001 "^content-type:\\(\n?[\t ]\\)\\(text\\|message\\)/"
2002 header-end t)
2003 (goto-char start)
2004 (re-search-forward
2005 "^content-transfer-encoding:\\(\n?[\t ]\\)*base64\\(\n?[\t ]\\)*"
2006 header-end t)))))
2007 (if quoted-printable-header-field-end
2008 (save-excursion
2009 (unless
2010 (mail-unquote-printable-region header-end (point) nil t t)
2011 (message "Malformed MIME quoted-printable message"))
2012 ;; Change "quoted-printable" to "8bit",
2013 ;; to reflect the decoding we just did.
2014 (goto-char quoted-printable-header-field-end)
2015 (delete-region (point) (search-backward ":"))
2016 (insert ": 8bit")))
2017 (if base64-header-field-end
2018 (save-excursion
2019 (when
2020 (condition-case nil
2021 (progn
2022 (base64-decode-region (1+ header-end)
2023 (- (point) 2))
2024 t)
2025 (error nil))
2026 ;; Change "base64" to "8bit", to reflect the
2027 ;; decoding we just did.
2028 (goto-char base64-header-field-end)
2029 (delete-region (point) (search-backward ":"))
2030 (insert ": 8bit"))))
2031 (setq last-coding-system-used nil)
2032 (or rmail-enable-mime
2033 (not rmail-enable-multibyte)
2034 (let ((mime-charset
2035 (if (and rmail-decode-mime-charset
2036 (save-excursion
2037 (goto-char start)
2038 (search-forward "\n\n" nil t)
2039 (let ((case-fold-search t))
2040 (re-search-backward
2041 rmail-mime-charset-pattern
2042 start t))))
2043 (intern (downcase (match-string 1))))))
2044 (rmail-decode-region start (point) mime-charset))))
2045 ;; Add an X-Coding-System: header if we don't have one.
2046 (save-excursion
2047 (goto-char start)
2048 (forward-line 1)
2049 (if (looking-at "0")
2050 (forward-line 1)
2051 (forward-line 2))
2052 (or (save-restriction
2053 (narrow-to-region (point) (point-max))
2054 (rfc822-goto-eoh)
2055 (goto-char (point-min))
2056 (re-search-forward "^X-Coding-System:" nil t))
2057 (insert "X-Coding-System: "
2058 (symbol-name last-coding-system-used)
2059 "\n")))
2060 (narrow-to-region (point) (point-max))
2061 (and (= 0 (% count 10))
2062 (message "Converting to Babyl format...%d" count)))
2063 ;;*** MMDF format
2064 ((let ((case-fold-search t))
2065 (looking-at rmail-mmdf-delim1))
2066 (let ((case-fold-search t))
2067 (replace-match "\^L\n0, unseen,,\n*** EOOH ***\n")
2068 (re-search-forward rmail-mmdf-delim2 nil t)
2069 (replace-match "\^_"))
2070 (save-excursion
2071 (save-restriction
2072 (narrow-to-region start (1- (point)))
2073 (goto-char (point-min))
2074 (while (search-forward "\n\^_" nil t) ; single char "\^_"
2075 (replace-match "\n^_")))) ; 2 chars: "^" and "_"
2076 (setq last-coding-system-used nil)
2077 (or rmail-enable-mime
2078 (not rmail-enable-multibyte)
2079 (decode-coding-region start (point) 'undecided))
2080 (save-excursion
2081 (goto-char start)
2082 (forward-line 3)
2083 (insert "X-Coding-System: "
2084 (symbol-name last-coding-system-used)
2085 "\n"))
2086 (narrow-to-region (point) (point-max))
2087 (setq count (1+ count))
2088 (and (= 0 (% count 10))
2089 (message "Converting to Babyl format...%d" count)))
2090 ;;*** Mail format
2091 ((looking-at "^From ")
2092 (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
2093 (rmail-nuke-pinhead-header)
2094 ;; If this message has a Content-Length field,
2095 ;; skip to the end of the contents.
2096 (let* ((header-end (save-excursion
2097 (and (re-search-forward "\n\n" nil t)
2098 (1- (point)))))
2099 (case-fold-search t)
2100 (quoted-printable-header-field-end
2101 (save-excursion
2102 (re-search-forward
2103 "^content-transfer-encoding:\\(\n?[\t ]\\)*quoted-printable\\(\n?[\t ]\\)*"
2104 header-end t)))
2105 (base64-header-field-end
2106 (and
2107 ;; Don't decode non-text data.
2108 (save-excursion
2109 (re-search-forward
2110 "^content-type:\\(\n?[\t ]\\)\\(text\\|message\\)/"
2111 header-end t))
2112 (save-excursion
2113 (re-search-forward
2114 "^content-transfer-encoding:\\(\n?[\t ]\\)*base64\\(\n?[\t ]\\)*"
2115 header-end t))))
2116 (size
2117 ;; Get the numeric value from the Content-Length field.
2118 (save-excursion
2119 ;; Back up to end of prev line,
2120 ;; in case the Content-Length field comes first.
2121 (forward-char -1)
2122 (and (search-forward "\ncontent-length: "
2123 header-end t)
2124 (let ((beg (point))
2125 (eol (progn (end-of-line) (point))))
2126 (string-to-number (buffer-substring beg eol)))))))
2127 (and size
2128 (if (and (natnump size)
2129 (<= (+ header-end size) (point-max))
2130 ;; Make sure this would put us at a position
2131 ;; that we could continue from.
2132 (save-excursion
2133 (goto-char (+ header-end size))
2134 (skip-chars-forward "\n")
2135 (or (eobp)
2136 (and (looking-at "BABYL OPTIONS:")
2137 (search-forward "\n\^_" nil t))
2138 (and (looking-at "\^L")
2139 (search-forward "\n\^_" nil t))
2140 (let ((case-fold-search t))
2141 (looking-at rmail-mmdf-delim1))
2142 (looking-at "From "))))
2143 (goto-char (+ header-end size))
2144 (message "Ignoring invalid Content-Length field")
2145 (sit-for 1 0 t)))
2146 (if (let ((case-fold-search nil))
2147 (re-search-forward
2148 (concat "^[\^_]?\\("
2149 rmail-unix-mail-delimiter
2150 "\\|"
2151 rmail-mmdf-delim1 "\\|"
2152 "^BABYL OPTIONS:\\|"
2153 "\^L\n[01],\\)") nil t))
2154 (goto-char (match-beginning 1))
2155 (goto-char (point-max)))
2156 (setq count (1+ count))
2157 (if quoted-printable-header-field-end
2158 (save-excursion
2159 (unless
2160 (mail-unquote-printable-region header-end (point) nil t t)
2161 (message "Malformed MIME quoted-printable message"))
2162 ;; Change "quoted-printable" to "8bit",
2163 ;; to reflect the decoding we just did.
2164 (goto-char quoted-printable-header-field-end)
2165 (delete-region (point) (search-backward ":"))
2166 (insert ": 8bit")))
2167 (if base64-header-field-end
2168 (save-excursion
2169 (when
2170 (condition-case nil
2171 (progn
2172 (base64-decode-region
2173 (1+ header-end)
2174 (save-excursion
2175 ;; Prevent base64-decode-region
2176 ;; from removing newline characters.
2177 (skip-chars-backward "\n\t ")
2178 (point)))
2179 t)
2180 (error nil))
2181 ;; Change "base64" to "8bit", to reflect the
2182 ;; decoding we just did.
2183 (goto-char base64-header-field-end)
2184 (delete-region (point) (search-backward ":"))
2185 (insert ": 8bit")))))
2186
2187 (save-excursion
2188 (save-restriction
2189 (narrow-to-region start (point))
2190 (goto-char (point-min))
2191 (while (search-forward "\n\^_" nil t) ; single char
2192 (replace-match "\n^_")))) ; 2 chars: "^" and "_"
2193 ;; This is for malformed messages that don't end in newline.
2194 ;; There shouldn't be any, but some users say occasionally
2195 ;; there are some.
2196 (or (bolp) (newline))
2197 (insert ?\^_)
2198 (setq last-coding-system-used nil)
2199 (or rmail-enable-mime
2200 (not rmail-enable-multibyte)
2201 (let ((mime-charset
2202 (if (and rmail-decode-mime-charset
2203 (save-excursion
2204 (goto-char start)
2205 (search-forward "\n\n" nil t)
2206 (let ((case-fold-search t))
2207 (re-search-backward
2208 rmail-mime-charset-pattern
2209 start t))))
2210 (intern (downcase (match-string 1))))))
2211 (rmail-decode-region start (point) mime-charset)))
2212 (save-excursion
2213 (goto-char start)
2214 (forward-line 3)
2215 (insert "X-Coding-System: "
2216 (symbol-name last-coding-system-used)
2217 "\n"))
2218 (narrow-to-region (point) (point-max))
2219 (and (= 0 (% count 10))
2220 (message "Converting to Babyl format...%d" count)))
2221 ;;
2222 ;; This kludge is because some versions of sendmail.el
2223 ;; insert an extra newline at the beginning that shouldn't
2224 ;; be there. sendmail.el has been fixed, but old versions
2225 ;; may still be in use. -- rms, 7 May 1993.
2226 ((eolp) (delete-char 1))
2227 (t (error "Cannot convert to babyl format")))))
2228 (setq buffer-undo-list nil)
2229 count))
2230
2231 ;; Delete the "From ..." line, creating various other headers with
2232 ;; information from it if they don't already exist. Now puts the
2233 ;; original line into a mail-from: header line for debugging and for
2234 ;; use by the rmail-output function.
2235 (defun rmail-nuke-pinhead-header ()
2236 (save-excursion
2237 (save-restriction
2238 (let ((start (point))
2239 (end (progn
2240 (condition-case ()
2241 (search-forward "\n\n")
2242 (error
2243 (goto-char (point-max))
2244 (insert "\n\n")))
2245 (point)))
2246 has-from has-date)
2247 (narrow-to-region start end)
2248 (let ((case-fold-search t))
2249 (goto-char start)
2250 (setq has-from (search-forward "\nFrom:" nil t))
2251 (goto-char start)
2252 (setq has-date (and (search-forward "\nDate:" nil t) (point)))
2253 (goto-char start))
2254 (let ((case-fold-search nil))
2255 (if (re-search-forward (concat "^" rmail-unix-mail-delimiter) nil t)
2256 (replace-match
2257 (concat
2258 "Mail-from: \\&"
2259 ;; Keep and reformat the date if we don't
2260 ;; have a Date: field.
2261 (if has-date
2262 ""
2263 (concat
2264 "Date: \\2, \\4 \\3 \\9 \\5 "
2265
2266 ;; The timezone could be matched by group 7 or group 10.
2267 ;; If neither of them matched, assume EST, since only
2268 ;; Easterners would be so sloppy.
2269 ;; It's a shame the substitution can't use "\\10".
2270 (cond
2271 ((/= (match-beginning 7) (match-end 7)) "\\7")
2272 ((/= (match-beginning 10) (match-end 10))
2273 (buffer-substring (match-beginning 10)
2274 (match-end 10)))
2275 (t "EST"))
2276 "\n"))
2277 ;; Keep and reformat the sender if we don't
2278 ;; have a From: field.
2279 (if has-from
2280 ""
2281 "From: \\1\n"))
2282 t)))))))
2283 \f
2284 ;;;; *** Rmail Message Formatting and Header Manipulation ***
2285
2286 (defun rmail-reformat-message (beg end)
2287 (goto-char beg)
2288 (forward-line 1)
2289 (if (/= (following-char) ?0)
2290 (error "Bad format in RMAIL file"))
2291 (let ((inhibit-read-only t)
2292 (delta (- (buffer-size) end)))
2293 (delete-char 1)
2294 (insert ?1)
2295 (forward-line 1)
2296 (let ((case-fold-search t))
2297 (while (looking-at "Summary-line:\\|Mail-From:")
2298 (forward-line 1)))
2299 (if (looking-at "\\*\\*\\* EOOH \\*\\*\\*\n")
2300 (delete-region (point)
2301 (progn (forward-line 1) (point))))
2302 (let ((str (buffer-substring (point)
2303 (save-excursion (search-forward "\n\n" end 'move)
2304 (point)))))
2305 (insert str "*** EOOH ***\n")
2306 (narrow-to-region (point) (- (buffer-size) delta)))
2307 (goto-char (point-min))
2308 (if rmail-message-filter (funcall rmail-message-filter))
2309 (if (or rmail-displayed-headers rmail-ignored-headers)
2310 (rmail-clear-headers))))
2311
2312 (defun rmail-clear-headers (&optional ignored-headers)
2313 "Delete all header fields that Rmail should not show.
2314 If the optional argument IGNORED-HEADERS is non-nil,
2315 delete all header fields whose names match that regexp.
2316 Otherwise, if `rmail-displayed-headers' is non-nil,
2317 delete all header fields *except* those whose names match that regexp.
2318 Otherwise, delete all header fields whose names match `rmail-ignored-headers'
2319 unless they also match `rmail-nonignored-headers'."
2320 (when (search-forward "\n\n" nil t)
2321 (forward-char -1)
2322 (let ((case-fold-search t)
2323 (buffer-read-only nil))
2324 (if (and rmail-displayed-headers (null ignored-headers))
2325 (save-restriction
2326 (narrow-to-region (point-min) (point))
2327 (let (lim next)
2328 (goto-char (point-min))
2329 (while (and (not (eobp))
2330 (save-excursion
2331 (if (re-search-forward "\n[^ \t]" nil t)
2332 (setq lim (match-beginning 0)
2333 next (1+ lim))
2334 (setq lim nil next (point-max)))))
2335 (if (save-excursion
2336 (re-search-forward rmail-displayed-headers lim t))
2337 (goto-char next)
2338 (delete-region (point) next))))
2339 (goto-char (point-min)))
2340 (or ignored-headers (setq ignored-headers rmail-ignored-headers))
2341 (save-restriction
2342 (narrow-to-region (point-min) (point))
2343 (goto-char (point-min))
2344 (while (and ignored-headers
2345 (re-search-forward ignored-headers nil t))
2346 (beginning-of-line)
2347 (if (and rmail-nonignored-headers
2348 (looking-at rmail-nonignored-headers))
2349 (forward-line 1)
2350 (delete-region (point)
2351 (save-excursion
2352 (if (re-search-forward "\n[^ \t]" nil t)
2353 (1- (point))
2354 (point-max)))))))))))
2355
2356 (defun rmail-msg-is-pruned ()
2357 (rmail-maybe-set-message-counters)
2358 (save-restriction
2359 (narrow-to-region (rmail-msgbeg rmail-current-message) (point-max))
2360 (save-excursion
2361 (goto-char (point-min))
2362 (forward-line 1)
2363 (= (following-char) ?1))))
2364
2365 (defun rmail-msg-restore-non-pruned-header ()
2366 (let ((old-point (point))
2367 new-point
2368 new-start
2369 (inhibit-read-only t))
2370 (save-excursion
2371 (narrow-to-region (rmail-msgbeg rmail-current-message) (point-max))
2372 (goto-char (point-min))
2373 (forward-line 1)
2374 ;; Change 1 to 0.
2375 (delete-char 1)
2376 (insert ?0)
2377 ;; Insert new EOOH line at the proper place.
2378 (forward-line 1)
2379 (let ((case-fold-search t))
2380 (while (looking-at "Summary-Line:\\|Mail-From:")
2381 (forward-line 1)))
2382 (insert "*** EOOH ***\n")
2383 (setq new-start (point))
2384 ;; Delete the old reformatted header.
2385 (forward-char -1)
2386 (search-forward "\n*** EOOH ***\n")
2387 (forward-line -1)
2388 (let ((start (point)))
2389 (search-forward "\n\n")
2390 (if (and (<= start old-point)
2391 (<= old-point (point)))
2392 (setq new-point new-start))
2393 (delete-region start (point)))
2394 ;; Narrow to after the new EOOH line.
2395 (narrow-to-region new-start (point-max)))
2396 (if new-point
2397 (goto-char new-point))))
2398
2399 (defun rmail-msg-prune-header ()
2400 (let ((new-point
2401 (= (point) (point-min))))
2402 (save-excursion
2403 (narrow-to-region (rmail-msgbeg rmail-current-message) (point-max))
2404 (rmail-reformat-message (point-min) (point-max)))
2405 (if new-point
2406 (goto-char (point-min)))))
2407
2408 (defun rmail-toggle-header (&optional arg)
2409 "Show original message header if pruned header currently shown, or vice versa.
2410 With argument ARG, show the message header pruned if ARG is greater than zero;
2411 otherwise, show it in full."
2412 (interactive "P")
2413 (let* ((pruned (with-current-buffer rmail-buffer
2414 (rmail-msg-is-pruned)))
2415 (prune (if arg
2416 (> (prefix-numeric-value arg) 0)
2417 (not pruned))))
2418 (if (eq pruned prune)
2419 t
2420 (set-buffer rmail-buffer)
2421 (rmail-maybe-set-message-counters)
2422 (if rmail-enable-mime
2423 (let ((buffer-read-only nil))
2424 (if pruned
2425 (rmail-msg-restore-non-pruned-header)
2426 (rmail-msg-prune-header))
2427 (funcall rmail-show-mime-function))
2428 (let* ((buffer-read-only nil)
2429 (window (get-buffer-window (current-buffer)))
2430 (at-point-min (= (point) (point-min)))
2431 (all-headers-visible (= (window-start window) (point-min)))
2432 (on-header
2433 (save-excursion
2434 (and (not (search-backward "\n\n" nil t))
2435 (progn
2436 (end-of-line)
2437 (re-search-backward "^[-A-Za-z0-9]+:" nil t))
2438 (match-string 0))))
2439 (old-screen-line
2440 (rmail-count-screen-lines (window-start window) (point))))
2441 (if pruned
2442 (rmail-msg-restore-non-pruned-header)
2443 (rmail-msg-prune-header))
2444 (cond (at-point-min
2445 (goto-char (point-min)))
2446 (on-header
2447 (goto-char (point-min))
2448 (search-forward "\n\n")
2449 (or (re-search-backward
2450 (concat "^" (regexp-quote on-header)) nil t)
2451 (goto-char (point-min))))
2452 (t
2453 (save-selected-window
2454 (select-window window)
2455 (recenter old-screen-line)
2456 (if (and all-headers-visible
2457 (not (= (window-start) (point-min))))
2458 (recenter (- (window-height) 2))))))))
2459 (rmail-highlight-headers))))
2460
2461 (defun rmail-narrow-to-non-pruned-header ()
2462 "Narrow to the whole (original) header of the current message."
2463 (let (start end)
2464 (narrow-to-region (rmail-msgbeg rmail-current-message) (point-max))
2465 (goto-char (point-min))
2466 (forward-line 1)
2467 (if (= (following-char) ?1)
2468 (progn
2469 (forward-line 1)
2470 (setq start (point))
2471 (search-forward "*** EOOH ***\n")
2472 (setq end (match-beginning 0)))
2473 (forward-line 2)
2474 (setq start (point))
2475 (search-forward "\n\n")
2476 (setq end (1- (point))))
2477 (narrow-to-region start end)
2478 (goto-char start)))
2479
2480 ;; Lifted from repos-count-screen-lines.
2481 ;; Return number of screen lines between START and END.
2482 (defun rmail-count-screen-lines (start end)
2483 (save-excursion
2484 (save-restriction
2485 (narrow-to-region start end)
2486 (goto-char (point-min))
2487 (vertical-motion (- (point-max) (point-min))))))
2488 \f
2489 ;;;; *** Rmail Attributes and Keywords ***
2490
2491 ;; Make a string describing current message's attributes and keywords
2492 ;; and set it up as the name of a minor mode
2493 ;; so it will appear in the mode line.
2494 (defun rmail-display-labels ()
2495 (let ((blurb "") (beg (point-min-marker)) (end (point-max-marker)))
2496 (save-excursion
2497 (unwind-protect
2498 (progn
2499 (widen)
2500 (goto-char (rmail-msgbeg rmail-current-message))
2501 (forward-line 1)
2502 (if (looking-at "[01],")
2503 (progn
2504 (narrow-to-region (point) (progn (end-of-line) (point)))
2505 ;; Truly valid BABYL format requires a space before each
2506 ;; attribute or keyword name. Put them in if missing.
2507 (let (buffer-read-only)
2508 (goto-char (point-min))
2509 (while (search-forward "," nil t)
2510 (or (looking-at "[ ,]") (eobp)
2511 (insert " "))))
2512 (goto-char (point-max))
2513 (if (search-backward ",," nil 'move)
2514 (progn
2515 (if (> (point) (1+ (point-min)))
2516 (setq blurb (buffer-substring (+ 1 (point-min)) (point))))
2517 (if (> (- (point-max) (point)) 2)
2518 (setq blurb
2519 (concat blurb
2520 ";"
2521 (buffer-substring (+ (point) 3)
2522 (1- (point-max)))))))))))
2523 ;; Note: we don't use save-restriction because that does not work right
2524 ;; if changes are made outside the saved restriction
2525 ;; before that restriction is restored.
2526 (narrow-to-region beg end)
2527 (set-marker beg nil)
2528 (set-marker end nil)))
2529 (while (string-match " +," blurb)
2530 (setq blurb (concat (substring blurb 0 (match-beginning 0)) ","
2531 (substring blurb (match-end 0)))))
2532 (while (string-match ", +" blurb)
2533 (setq blurb (concat (substring blurb 0 (match-beginning 0)) ","
2534 (substring blurb (match-end 0)))))
2535 (setq mode-line-process
2536 (format " %d/%d%s"
2537 rmail-current-message rmail-total-messages blurb))
2538 ;; If rmail-enable-mime is non-nil, we may have to update
2539 ;; `mode-line-process' of rmail-view-buffer too.
2540 (if (and rmail-enable-mime
2541 (not (eq (current-buffer) rmail-view-buffer))
2542 (buffer-live-p rmail-view-buffer))
2543 (let ((mlp mode-line-process))
2544 (with-current-buffer rmail-view-buffer
2545 (setq mode-line-process mlp))))))
2546
2547 ;; Turn an attribute of a message on or off according to STATE.
2548 ;; ATTR is the name of the attribute, as a string.
2549 ;; MSGNUM is message number to change; nil means current message.
2550 (defun rmail-set-attribute (attr state &optional msgnum)
2551 (set-buffer rmail-buffer)
2552 (let ((omax (point-max-marker))
2553 (omin (point-min-marker))
2554 (buffer-read-only nil))
2555 (or msgnum (setq msgnum rmail-current-message))
2556 (if (> msgnum 0)
2557 (unwind-protect
2558 (save-excursion
2559 (widen)
2560 (goto-char (+ 3 (rmail-msgbeg msgnum)))
2561 (let ((curstate
2562 (not
2563 (null (search-backward (concat ", " attr ",")
2564 (prog1 (point) (end-of-line)) t)))))
2565 (or (eq curstate (not (not state)))
2566 (if curstate
2567 (delete-region (point) (1- (match-end 0)))
2568 (beginning-of-line)
2569 (forward-char 2)
2570 (insert " " attr ","))))
2571 (if (string= attr "deleted")
2572 (rmail-set-message-deleted-p msgnum state)))
2573 ;; Note: we don't use save-restriction because that does not work right
2574 ;; if changes are made outside the saved restriction
2575 ;; before that restriction is restored.
2576 (narrow-to-region omin omax)
2577 (set-marker omin nil)
2578 (set-marker omax nil)
2579 (if (= msgnum rmail-current-message)
2580 (rmail-display-labels))))))
2581
2582 ;; Return t if the attributes/keywords line of msg number MSG
2583 ;; contains a match for the regexp LABELS.
2584 (defun rmail-message-labels-p (msg labels)
2585 (save-excursion
2586 (save-restriction
2587 (widen)
2588 (goto-char (rmail-msgbeg msg))
2589 (forward-char 3)
2590 (re-search-backward labels (prog1 (point) (end-of-line)) t))))
2591 \f
2592 ;;;; *** Rmail Message Selection And Support ***
2593
2594 (defun rmail-msgend (n)
2595 (marker-position (aref rmail-message-vector (1+ n))))
2596
2597 (defun rmail-msgbeg (n)
2598 (marker-position (aref rmail-message-vector n)))
2599
2600 (defun rmail-widen-to-current-msgbeg (function)
2601 "Call FUNCTION with point at start of internal data of current message.
2602 Assumes that bounds were previously narrowed to display the message in Rmail.
2603 The bounds are widened enough to move point where desired, then narrowed
2604 again afterward.
2605
2606 FUNCTION may not change the visible text of the message, but it may
2607 change the invisible header text."
2608 (save-excursion
2609 (unwind-protect
2610 (progn
2611 (narrow-to-region (rmail-msgbeg rmail-current-message)
2612 (point-max))
2613 (goto-char (point-min))
2614 (funcall function))
2615 ;; Note: we don't use save-restriction because that does not work right
2616 ;; if changes are made outside the saved restriction
2617 ;; before that restriction is restored.
2618 (narrow-to-region (rmail-msgbeg rmail-current-message)
2619 (rmail-msgend rmail-current-message)))))
2620
2621 (defun rmail-forget-messages ()
2622 (unwind-protect
2623 (if (vectorp rmail-message-vector)
2624 (let* ((i 0)
2625 (v rmail-message-vector)
2626 (n (length v)))
2627 (while (< i n)
2628 (move-marker (aref v i) nil)
2629 (setq i (1+ i)))))
2630 (setq rmail-message-vector nil)
2631 (setq rmail-msgref-vector nil)
2632 (setq rmail-deleted-vector nil)))
2633
2634 (defun rmail-maybe-set-message-counters ()
2635 (if (not (and rmail-deleted-vector
2636 rmail-message-vector
2637 rmail-current-message
2638 rmail-total-messages))
2639 (rmail-set-message-counters)))
2640
2641 (defun rmail-count-new-messages (&optional nomsg)
2642 (let* ((case-fold-search nil)
2643 (total-messages 0)
2644 (messages-head nil)
2645 (deleted-head nil))
2646 (or nomsg (message "Counting new messages..."))
2647 (goto-char (point-max))
2648 ;; Put at the end of messages-head
2649 ;; the entry for message N+1, which marks
2650 ;; the end of message N. (N = number of messages).
2651 (search-backward "\n\^_")
2652 (forward-char 1)
2653 (setq messages-head (list (point-marker)))
2654 (rmail-set-message-counters-counter (point-min))
2655 (setq rmail-current-message (1+ rmail-total-messages))
2656 (setq rmail-total-messages
2657 (+ rmail-total-messages total-messages))
2658 (setq rmail-message-vector
2659 (vconcat rmail-message-vector (cdr messages-head)))
2660 (aset rmail-message-vector
2661 rmail-current-message (car messages-head))
2662 (setq rmail-deleted-vector
2663 (concat rmail-deleted-vector deleted-head))
2664 (setq rmail-summary-vector
2665 (vconcat rmail-summary-vector (make-vector total-messages nil)))
2666 (setq rmail-msgref-vector
2667 (vconcat rmail-msgref-vector (make-vector total-messages nil)))
2668 ;; Fill in the new elements of rmail-msgref-vector.
2669 (let ((i (1+ (- rmail-total-messages total-messages))))
2670 (while (<= i rmail-total-messages)
2671 (aset rmail-msgref-vector i (list i))
2672 (setq i (1+ i))))
2673 (goto-char (point-min))
2674 (or nomsg (message "Counting new messages...done (%d)" total-messages))))
2675
2676 (defun rmail-set-message-counters ()
2677 (rmail-forget-messages)
2678 (save-excursion
2679 (save-restriction
2680 (widen)
2681 (let* ((point-save (point))
2682 (total-messages 0)
2683 (messages-after-point)
2684 (case-fold-search nil)
2685 (messages-head nil)
2686 (deleted-head nil))
2687 (message "Counting messages...")
2688 (goto-char (point-max))
2689 ;; Put at the end of messages-head
2690 ;; the entry for message N+1, which marks
2691 ;; the end of message N. (N = number of messages).
2692 (search-backward "\n\^_" nil t)
2693 (if (/= (point) (point-max)) (forward-char 1))
2694 (setq messages-head (list (point-marker)))
2695 (rmail-set-message-counters-counter (min (point) point-save))
2696 (setq messages-after-point total-messages)
2697 (rmail-set-message-counters-counter)
2698 (setq rmail-total-messages total-messages)
2699 (setq rmail-current-message
2700 (min total-messages
2701 (max 1 (- total-messages messages-after-point))))
2702 (setq rmail-message-vector
2703 (apply 'vector (cons (point-min-marker) messages-head))
2704 rmail-deleted-vector (concat "0" deleted-head)
2705 rmail-summary-vector (make-vector rmail-total-messages nil)
2706 rmail-msgref-vector (make-vector (1+ rmail-total-messages) nil))
2707 (let ((i 0))
2708 (while (<= i rmail-total-messages)
2709 (aset rmail-msgref-vector i (list i))
2710 (setq i (1+ i))))
2711 (message "Counting messages...done")))))
2712
2713 (defun rmail-set-message-counters-counter (&optional stop)
2714 (let ((start (point))
2715 next)
2716 (while (search-backward "\n\^_\^L" stop t)
2717 ;; Detect messages that have been added with DOS line endings and
2718 ;; convert the line endings for such messages.
2719 (setq next (point))
2720 (if (looking-at "\n\^_\^L\r\n")
2721 (let ((buffer-read-only nil)
2722 (buffer-undo t))
2723 (message "Counting messages...(converting line endings)")
2724 (save-excursion
2725 (goto-char start)
2726 (while (search-backward "\r\n" next t)
2727 (delete-char 1)))))
2728 (setq start next)
2729 (forward-char 1)
2730 (setq messages-head (cons (point-marker) messages-head))
2731 (save-excursion
2732 (setq deleted-head
2733 (cons (if (search-backward ", deleted,"
2734 (prog1 (point)
2735 (forward-line 2))
2736 t)
2737 ?D ?\ )
2738 deleted-head)))
2739 (if (zerop (% (setq total-messages (1+ total-messages)) 20))
2740 (message "Counting messages...%d" total-messages)))))
2741
2742 (defun rmail-beginning-of-message ()
2743 "Show current message starting from the beginning."
2744 (interactive)
2745 (let ((rmail-show-message-hook
2746 (list (function (lambda ()
2747 (goto-char (point-min)))))))
2748 (rmail-show-message rmail-current-message)))
2749
2750 (defun rmail-end-of-message ()
2751 "Show bottom of current message."
2752 (interactive)
2753 (let ((rmail-show-message-hook
2754 (list (function (lambda ()
2755 (goto-char (point-max))
2756 (recenter (1- (window-height))))))))
2757 (rmail-show-message rmail-current-message)))
2758
2759 (defun rmail-unknown-mail-followup-to ()
2760 "Handle a \"Mail-Followup-To\" header field with an unknown mailing list.
2761 Ask the user whether to add that list name to `mail-mailing-lists'."
2762 (save-restriction
2763 (rmail-narrow-to-non-pruned-header)
2764 (let ((mail-followup-to (mail-fetch-field "mail-followup-to" nil t)))
2765 (when mail-followup-to
2766 (let ((addresses
2767 (split-string
2768 (mail-strip-quoted-names mail-followup-to)
2769 ",[[:space:]]+" t)))
2770 (dolist (addr addresses)
2771 (when (and (not (member addr mail-mailing-lists))
2772 (not
2773 ;; taken from rmailsum.el
2774 (string-match
2775 (or rmail-user-mail-address-regexp
2776 (concat "^\\("
2777 (regexp-quote (user-login-name))
2778 "\\($\\|@\\)\\|"
2779 (regexp-quote
2780 (or user-mail-address
2781 (concat (user-login-name) "@"
2782 (or mail-host-address
2783 (system-name)))))
2784 "\\>\\)"))
2785 addr))
2786 (y-or-n-p
2787 (format "Add `%s' to `mail-mailing-lists'? "
2788 addr)))
2789 (customize-save-variable 'mail-mailing-lists
2790 (cons addr mail-mailing-lists)))))))))
2791
2792 (defun rmail-show-message (&optional n no-summary)
2793 "Show message number N (prefix argument), counting from start of file.
2794 If summary buffer is currently displayed, update current message there also."
2795 (interactive "p")
2796 (or (eq major-mode 'rmail-mode)
2797 (switch-to-buffer rmail-buffer))
2798 (rmail-maybe-set-message-counters)
2799 (widen)
2800 (if (zerop rmail-total-messages)
2801 (progn (narrow-to-region (point-min) (1- (point-max)))
2802 (goto-char (point-min))
2803 (setq mode-line-process nil))
2804 (let (blurb coding-system)
2805 (if (not n)
2806 (setq n rmail-current-message)
2807 (cond ((<= n 0)
2808 (setq n 1
2809 rmail-current-message 1
2810 blurb "No previous message"))
2811 ((> n rmail-total-messages)
2812 (setq n rmail-total-messages
2813 rmail-current-message rmail-total-messages
2814 blurb "No following message"))
2815 (t
2816 (setq rmail-current-message n))))
2817 (let ((beg (rmail-msgbeg n)))
2818 (goto-char beg)
2819 (forward-line 1)
2820 (save-excursion
2821 (let ((end (rmail-msgend n)))
2822 (save-restriction
2823 (if (prog1 (= (following-char) ?0)
2824 (forward-line 2)
2825 ;; If there's a Summary-line in the (otherwise empty)
2826 ;; header, we didn't yet get past the EOOH line.
2827 (if (looking-at "^\\*\\*\\* EOOH \\*\\*\\*\n")
2828 (forward-line 1))
2829 (narrow-to-region (point) end))
2830 (rfc822-goto-eoh)
2831 (search-forward "\n*** EOOH ***\n" end t))
2832 (narrow-to-region beg (point))
2833 (goto-char (point-min))
2834 (if (re-search-forward "^X-Coding-System: *\\(.*\\)$" nil t)
2835 (let ((coding-system (intern (match-string 1))))
2836 (condition-case nil
2837 (progn
2838 (check-coding-system coding-system)
2839 (setq buffer-file-coding-system coding-system))
2840 (error
2841 (setq buffer-file-coding-system nil))))
2842 (setq buffer-file-coding-system nil)))))
2843 ;; Clear the "unseen" attribute when we show a message.
2844 (rmail-set-attribute "unseen" nil)
2845 (let ((end (rmail-msgend n)))
2846 ;; Reformat the header, or else find the reformatted header.
2847 (if (= (following-char) ?0)
2848 (rmail-reformat-message beg end)
2849 (search-forward "\n*** EOOH ***\n" end t)
2850 (narrow-to-region (point) end)))
2851 (goto-char (point-min))
2852 (walk-windows
2853 (function (lambda (window)
2854 (if (eq (window-buffer window) (current-buffer))
2855 (set-window-point window (point)))))
2856 nil t)
2857 (rmail-display-labels)
2858 (if (eq rmail-enable-mime t)
2859 (funcall rmail-show-mime-function)
2860 (setq rmail-view-buffer rmail-buffer))
2861 (when mail-mailing-lists
2862 (rmail-unknown-mail-followup-to))
2863 (rmail-highlight-headers)
2864 (if transient-mark-mode (deactivate-mark))
2865 (run-hooks 'rmail-show-message-hook)
2866 ;; If there is a summary buffer, try to move to this message
2867 ;; in that buffer. But don't complain if this message
2868 ;; is not mentioned in the summary.
2869 ;; Don't do this at all if we were called on behalf
2870 ;; of cursor motion in the summary buffer.
2871 (and (rmail-summary-exists) (not no-summary)
2872 (let ((curr-msg rmail-current-message))
2873 (rmail-select-summary
2874 (rmail-summary-goto-msg curr-msg t t))))
2875 (with-current-buffer rmail-buffer
2876 (rmail-auto-file))
2877 (if blurb
2878 (message blurb))))))
2879
2880 (defun rmail-redecode-body (coding &optional raw)
2881 "Decode the body of the current message using coding system CODING.
2882 This is useful with mail messages that have malformed or missing
2883 charset= headers.
2884
2885 This function assumes that the current message is already decoded
2886 and displayed in the RMAIL buffer, but the coding system used to
2887 decode it was incorrect. It then encodes the message back to its
2888 original form, and decodes it again, using the coding system CODING.
2889
2890 Optional argument RAW, if non-nil, means don't encode the message
2891 before decoding it with the new CODING. This is useful if the current
2892 message text was produced by some function which invokes `insert',
2893 since `insert' leaves unibyte character codes 128 through 255 unconverted
2894 to multibyte. One example of such a situation is when the text was
2895 produced by `base64-decode-region'.
2896
2897 Interactively, invoke the function with a prefix argument to set RAW
2898 non-nil.
2899
2900 Note that if Emacs erroneously auto-detected one of the iso-2022
2901 encodings in the message, this function might fail because the escape
2902 sequences that switch between character sets and also single-shift and
2903 locking-shift codes are impossible to recover. This function is meant
2904 to be used to fix messages encoded with 8-bit encodings, such as
2905 iso-8859, koi8-r, etc."
2906 (interactive "zCoding system for re-decoding this message: ")
2907 (when (not rmail-enable-mime)
2908 (or (eq major-mode 'rmail-mode)
2909 (switch-to-buffer rmail-buffer))
2910 (save-excursion
2911 (let ((pruned (rmail-msg-is-pruned))
2912 (raw (or raw current-prefix-arg)))
2913 (unwind-protect
2914 (let ((msgbeg (rmail-msgbeg rmail-current-message))
2915 (msgend (rmail-msgend rmail-current-message))
2916 x-coding-header)
2917 ;; We need the message headers pruned (we later restore
2918 ;; the pruned stat to what it was, see the end of
2919 ;; unwind-protect form).
2920 (or pruned
2921 (rmail-toggle-header 1))
2922 (narrow-to-region msgbeg msgend)
2923 (goto-char (point-min))
2924 (when (search-forward "\n*** EOOH ***\n" (point-max) t)
2925 (narrow-to-region msgbeg (point)))
2926 (goto-char (point-min))
2927 (if (re-search-forward "^X-Coding-System: *\\(.*\\)$" nil t)
2928 (let ((old-coding (intern (match-string 1)))
2929 (buffer-read-only nil))
2930 (check-coding-system old-coding)
2931 ;; Make sure the new coding system uses the same EOL
2932 ;; conversion, to prevent ^M characters from popping
2933 ;; up all over the place.
2934 (setq coding
2935 (coding-system-change-eol-conversion
2936 coding
2937 (coding-system-eol-type old-coding)))
2938 ;; If old-coding is `undecided', encode-coding-region
2939 ;; will not encode the text at all. Find a proper
2940 ;; non-trivial encoding to use.
2941 (if (memq (coding-system-base old-coding) '(nil undecided))
2942 (setq old-coding
2943 (car (find-coding-systems-region msgbeg msgend))))
2944 (setq x-coding-header (point-marker))
2945 (narrow-to-region msgbeg msgend)
2946 (and (null raw)
2947 ;; If old and new encoding are the same, it
2948 ;; clearly doesn't make sense to encode.
2949 (not (coding-system-equal
2950 (coding-system-base old-coding)
2951 (coding-system-base coding)))
2952 ;; If the body includes only eight-bit-*
2953 ;; characters, encoding might fail, e.g. with
2954 ;; UTF-8, and isn't needed anyway.
2955 (> (length (delq 'ascii
2956 (delq 'eight-bit-graphic
2957 (delq 'eight-bit-control
2958 (find-charset-region
2959 msgbeg msgend)))))
2960 0)
2961 (encode-coding-region (point) msgend old-coding))
2962 (decode-coding-region (point) msgend coding)
2963 (setq last-coding-system-used coding)
2964 ;; Rewrite the coding-system header according
2965 ;; to what we did.
2966 (goto-char x-coding-header)
2967 (delete-region (point)
2968 (save-excursion
2969 (beginning-of-line)
2970 (point)))
2971 (insert "X-Coding-System: "
2972 (symbol-name last-coding-system-used))
2973 (set-marker x-coding-header nil)
2974 (rmail-show-message))
2975 (error "No X-Coding-System header found")))
2976 (or pruned
2977 (rmail-toggle-header 0)))))))
2978
2979 ;; Find all occurrences of certain fields, and highlight them.
2980 (defun rmail-highlight-headers ()
2981 ;; Do this only if the system supports faces.
2982 (if (and (fboundp 'internal-find-face)
2983 rmail-highlighted-headers)
2984 (save-excursion
2985 (search-forward "\n\n" nil 'move)
2986 (save-restriction
2987 (narrow-to-region (point-min) (point))
2988 (let ((case-fold-search t)
2989 (inhibit-read-only t)
2990 ;; Highlight with boldface if that is available.
2991 ;; Otherwise use the `highlight' face.
2992 (face (or rmail-highlight-face
2993 (if (face-differs-from-default-p 'bold)
2994 'bold 'highlight)))
2995 ;; List of overlays to reuse.
2996 (overlays rmail-overlay-list))
2997 (goto-char (point-min))
2998 (while (re-search-forward rmail-highlighted-headers nil t)
2999 (skip-chars-forward " \t")
3000 (let ((beg (point))
3001 overlay)
3002 (while (progn (forward-line 1)
3003 (looking-at "[ \t]")))
3004 ;; Back up over newline, then trailing spaces or tabs
3005 (forward-char -1)
3006 (while (member (preceding-char) '(? ?\t))
3007 (forward-char -1))
3008 (if overlays
3009 ;; Reuse an overlay we already have.
3010 (progn
3011 (setq overlay (car overlays)
3012 overlays (cdr overlays))
3013 (overlay-put overlay 'face face)
3014 (move-overlay overlay beg (point)))
3015 ;; Make a new overlay and add it to
3016 ;; rmail-overlay-list.
3017 (setq overlay (make-overlay beg (point)))
3018 (overlay-put overlay 'face face)
3019 (setq rmail-overlay-list
3020 (cons overlay rmail-overlay-list))))))))))
3021
3022 (defun rmail-auto-file ()
3023 "Automatically move a message into a sub-folder based on criteria.
3024 Called when a new message is displayed."
3025 (if (or (rmail-message-labels-p rmail-current-message "filed")
3026 (not (string= (buffer-file-name)
3027 (expand-file-name rmail-file-name))))
3028 ;; Do nothing if it's already been filed.
3029 nil
3030 ;; Find out some basics (common fields)
3031 (let ((from (mail-fetch-field "from"))
3032 (subj (mail-fetch-field "subject"))
3033 (to (concat (mail-fetch-field "to") "," (mail-fetch-field "cc")))
3034 (d rmail-automatic-folder-directives)
3035 (directive-loop nil)
3036 (folder nil))
3037 (while d
3038 (setq folder (car (car d))
3039 directive-loop (cdr (car d)))
3040 (while (and (car directive-loop)
3041 (let ((f (cond
3042 ((string= (car directive-loop) "from") from)
3043 ((string= (car directive-loop) "to") to)
3044 ((string= (car directive-loop) "subject") subj)
3045 (t (mail-fetch-field (car directive-loop))))))
3046 (and f (string-match (car (cdr directive-loop)) f))))
3047 (setq directive-loop (cdr (cdr directive-loop))))
3048 ;; If there are no directives left, then it was a complete match.
3049 (if (null directive-loop)
3050 (if (null folder)
3051 (rmail-delete-forward)
3052 (if (string= "/dev/null" folder)
3053 (rmail-delete-message)
3054 (rmail-output-to-rmail-file folder 1 t)
3055 (setq d nil))))
3056 (setq d (cdr d))))))
3057
3058 (defun rmail-next-message (n)
3059 "Show following message whether deleted or not.
3060 With prefix arg N, moves forward N messages, or backward if N is negative."
3061 (interactive "p")
3062 (set-buffer rmail-buffer)
3063 (rmail-maybe-set-message-counters)
3064 (rmail-show-message (+ rmail-current-message n)))
3065
3066 (defun rmail-previous-message (n)
3067 "Show previous message whether deleted or not.
3068 With prefix arg N, moves backward N messages, or forward if N is negative."
3069 (interactive "p")
3070 (rmail-next-message (- n)))
3071
3072 (defun rmail-next-undeleted-message (n)
3073 "Show following non-deleted message.
3074 With prefix arg N, moves forward N non-deleted messages,
3075 or backward if N is negative.
3076
3077 Returns t if a new message is being shown, nil otherwise."
3078 (interactive "p")
3079 (set-buffer rmail-buffer)
3080 (rmail-maybe-set-message-counters)
3081 (let ((lastwin rmail-current-message)
3082 (current rmail-current-message))
3083 (while (and (> n 0) (< current rmail-total-messages))
3084 (setq current (1+ current))
3085 (if (not (rmail-message-deleted-p current))
3086 (setq lastwin current n (1- n))))
3087 (while (and (< n 0) (> current 1))
3088 (setq current (1- current))
3089 (if (not (rmail-message-deleted-p current))
3090 (setq lastwin current n (1+ n))))
3091 (if (/= lastwin rmail-current-message)
3092 (progn (rmail-show-message lastwin)
3093 t)
3094 (if (< n 0)
3095 (message "No previous nondeleted message"))
3096 (if (> n 0)
3097 (message "No following nondeleted message"))
3098 nil)))
3099
3100 (defun rmail-previous-undeleted-message (n)
3101 "Show previous non-deleted message.
3102 With prefix argument N, moves backward N non-deleted messages,
3103 or forward if N is negative."
3104 (interactive "p")
3105 (rmail-next-undeleted-message (- n)))
3106
3107 (defun rmail-first-message ()
3108 "Show first message in file."
3109 (interactive)
3110 (rmail-maybe-set-message-counters)
3111 (rmail-show-message 1))
3112
3113 (defun rmail-last-message ()
3114 "Show last message in file."
3115 (interactive)
3116 (rmail-maybe-set-message-counters)
3117 (rmail-show-message rmail-total-messages))
3118
3119 (defun rmail-what-message ()
3120 (let ((where (point))
3121 (low 1)
3122 (high rmail-total-messages)
3123 (mid (/ rmail-total-messages 2)))
3124 (while (> (- high low) 1)
3125 (if (>= where (rmail-msgbeg mid))
3126 (setq low mid)
3127 (setq high mid))
3128 (setq mid (+ low (/ (- high low) 2))))
3129 (if (>= where (rmail-msgbeg high)) high low)))
3130
3131 (defun rmail-message-recipients-p (msg recipients &optional primary-only)
3132 (save-restriction
3133 (goto-char (rmail-msgbeg msg))
3134 (search-forward "\n*** EOOH ***\n")
3135 (narrow-to-region (point) (progn (search-forward "\n\n") (point)))
3136 (or (string-match recipients (or (mail-fetch-field "To") ""))
3137 (string-match recipients (or (mail-fetch-field "From") ""))
3138 (if (not primary-only)
3139 (string-match recipients (or (mail-fetch-field "Cc") ""))))))
3140
3141 (defun rmail-message-regexp-p (n regexp)
3142 "Return t, if for message number N, regexp REGEXP matches in the header."
3143 (let ((beg (rmail-msgbeg n))
3144 (end (rmail-msgend n)))
3145 (goto-char beg)
3146 (forward-line 1)
3147 (save-excursion
3148 (save-restriction
3149 (if (prog1 (= (following-char) ?0)
3150 (forward-line 2)
3151 ;; If there's a Summary-line in the (otherwise empty)
3152 ;; header, we didn't yet get past the EOOH line.
3153 (when (looking-at "^\\*\\*\\* EOOH \\*\\*\\*\n")
3154 (forward-line 1))
3155 (setq beg (point))
3156 (narrow-to-region (point) end))
3157 (progn
3158 (rfc822-goto-eoh)
3159 (setq end (point)))
3160 (setq beg (point))
3161 (search-forward "\n*** EOOH ***\n" end t)
3162 (setq end (1+ (match-beginning 0)))))
3163 (goto-char beg)
3164 (if rmail-enable-mime
3165 (funcall rmail-search-mime-header-function n regexp end)
3166 (re-search-forward regexp end t)))))
3167
3168 (defun rmail-search-message (msg regexp)
3169 "Return non-nil, if for message number MSG, regexp REGEXP matches."
3170 (goto-char (rmail-msgbeg msg))
3171 (if rmail-enable-mime
3172 (funcall rmail-search-mime-message-function msg regexp)
3173 (re-search-forward regexp (rmail-msgend msg) t)))
3174
3175 (defvar rmail-search-last-regexp nil)
3176 (defun rmail-search (regexp &optional n)
3177 "Show message containing next match for REGEXP (but not the current msg).
3178 Prefix argument gives repeat count; negative argument means search
3179 backwards (through earlier messages).
3180 Interactively, empty argument means use same regexp used last time."
3181 (interactive
3182 (let* ((reversep (< (prefix-numeric-value current-prefix-arg) 0))
3183 (prompt
3184 (concat (if reversep "Reverse " "") "Rmail search (regexp"))
3185 regexp)
3186 (setq prompt
3187 (concat prompt
3188 (if rmail-search-last-regexp
3189 (concat ", default "
3190 rmail-search-last-regexp "): ")
3191 "): ")))
3192 (setq regexp (read-string prompt))
3193 (cond ((not (equal regexp ""))
3194 (setq rmail-search-last-regexp regexp))
3195 ((not rmail-search-last-regexp)
3196 (error "No previous Rmail search string")))
3197 (list rmail-search-last-regexp
3198 (prefix-numeric-value current-prefix-arg))))
3199 (or n (setq n 1))
3200 (message "%sRmail search for %s..."
3201 (if (< n 0) "Reverse " "")
3202 regexp)
3203 (set-buffer rmail-buffer)
3204 (rmail-maybe-set-message-counters)
3205 (let ((omin (point-min))
3206 (omax (point-max))
3207 (opoint (point))
3208 win
3209 (reversep (< n 0))
3210 (msg rmail-current-message))
3211 (unwind-protect
3212 (progn
3213 (widen)
3214 (while (/= n 0)
3215 ;; Check messages one by one, advancing message number up or down
3216 ;; but searching forward through each message.
3217 (if reversep
3218 (while (and (null win) (> msg 1))
3219 (setq msg (1- msg)
3220 win (rmail-search-message msg regexp)))
3221 (while (and (null win) (< msg rmail-total-messages))
3222 (setq msg (1+ msg)
3223 win (rmail-search-message msg regexp))))
3224 (setq n (+ n (if reversep 1 -1)))))
3225 (if win
3226 (progn
3227 (rmail-show-message msg)
3228 ;; Search forward (if this is a normal search) or backward
3229 ;; (if this is a reverse search) through this message to
3230 ;; position point. This search may fail because REGEXP
3231 ;; was found in the hidden portion of this message. In
3232 ;; that case, move point to the beginning of visible
3233 ;; portion.
3234 (if reversep
3235 (progn
3236 (goto-char (point-max))
3237 (re-search-backward regexp nil 'move))
3238 (goto-char (point-min))
3239 (re-search-forward regexp nil t))
3240 (message "%sRmail search for %s...done"
3241 (if reversep "Reverse " "")
3242 regexp))
3243 (goto-char opoint)
3244 (narrow-to-region omin omax)
3245 (ding)
3246 (message "Search failed: %s" regexp)))))
3247
3248 (defun rmail-search-backwards (regexp &optional n)
3249 "Show message containing previous match for REGEXP.
3250 Prefix argument gives repeat count; negative argument means search
3251 forward (through later messages).
3252 Interactively, empty argument means use same regexp used last time."
3253 (interactive
3254 (let* ((reversep (>= (prefix-numeric-value current-prefix-arg) 0))
3255 (prompt
3256 (concat (if reversep "Reverse " "") "Rmail search (regexp"))
3257 regexp)
3258 (setq prompt
3259 (concat prompt
3260 (if rmail-search-last-regexp
3261 (concat ", default "
3262 rmail-search-last-regexp "): ")
3263 "): ")))
3264 (setq regexp (read-string prompt))
3265 (cond ((not (equal regexp ""))
3266 (setq rmail-search-last-regexp regexp))
3267 ((not rmail-search-last-regexp)
3268 (error "No previous Rmail search string")))
3269 (list rmail-search-last-regexp
3270 (prefix-numeric-value current-prefix-arg))))
3271 (rmail-search regexp (- (or n 1))))
3272
3273 ;; Show the first message which has the `unseen' attribute.
3274 (defun rmail-first-unseen-message ()
3275 (rmail-maybe-set-message-counters)
3276 (let ((current 1)
3277 found)
3278 (save-restriction
3279 (widen)
3280 (while (and (not found) (<= current rmail-total-messages))
3281 (if (rmail-message-labels-p current ", ?\\(unseen\\),")
3282 (setq found current))
3283 (setq current (1+ current))))
3284 ;; Let the caller show the message.
3285 ;; (if found
3286 ;; (rmail-show-message found))
3287 found))
3288
3289 (defun rmail-current-subject ()
3290 "Return the current subject.
3291 The subject is stripped of leading and trailing whitespace, and
3292 of typical reply prefixes such as Re:."
3293 (let ((subject (or (mail-fetch-field "Subject") "")))
3294 (if (string-match "\\`[ \t]+" subject)
3295 (setq subject (substring subject (match-end 0))))
3296 (if (string-match rmail-reply-regexp subject)
3297 (setq subject (substring subject (match-end 0))))
3298 (if (string-match "[ \t]+\\'" subject)
3299 (setq subject (substring subject 0 (match-beginning 0))))
3300 subject))
3301
3302 (defun rmail-current-subject-regexp ()
3303 "Return a regular expression matching the current subject.
3304 The regular expression matches the subject header line of
3305 messages about the same subject. The subject itself is stripped
3306 of leading and trailing whitespace, of typical reply prefixes
3307 such as Re: and whitespace within the subject is replaced by a
3308 regular expression matching whitespace in general in order to
3309 take into account that subject header lines may include newlines
3310 and more whitespace. The returned regular expressions contains
3311 `rmail-reply-regexp' and ends with a newline."
3312 (let ((subject (rmail-current-subject)))
3313 ;; If Subject is long, mailers will break it into several lines at
3314 ;; arbitrary places, so replace whitespace with a regexp that will
3315 ;; match any sequence of spaces, TABs, and newlines.
3316 (setq subject (regexp-quote subject))
3317 (setq subject
3318 (replace-regexp-in-string "[ \t\n]+" "[ \t\n]+" subject t t))
3319 ;; Some mailers insert extra spaces after "Subject:", so allow any
3320 ;; amount of them.
3321 (concat "^Subject:[ \t]+"
3322 (if (string= "\\`" (substring rmail-reply-regexp 0 2))
3323 (substring rmail-reply-regexp 2)
3324 rmail-reply-regexp)
3325 subject "[ \t]*\n")))
3326
3327 (defun rmail-next-same-subject (n)
3328 "Go to the next mail message having the same subject header.
3329 With prefix argument N, do this N times.
3330 If N is negative, go backwards instead."
3331 (interactive "p")
3332 (let ((search-regexp (rmail-current-subject-regexp))
3333 (forward (> n 0))
3334 (i rmail-current-message)
3335 (case-fold-search t)
3336 found)
3337 (save-excursion
3338 (save-restriction
3339 (widen)
3340 (while (and (/= n 0)
3341 (if forward
3342 (< i rmail-total-messages)
3343 (> i 1)))
3344 (let (done)
3345 (while (and (not done)
3346 (if forward
3347 (< i rmail-total-messages)
3348 (> i 1)))
3349 (setq i (if forward (1+ i) (1- i)))
3350 (goto-char (rmail-msgbeg i))
3351 (search-forward "\n*** EOOH ***\n")
3352 (let ((beg (point)) end)
3353 (search-forward "\n\n")
3354 (setq end (point))
3355 (goto-char beg)
3356 (setq done (re-search-forward search-regexp end t))))
3357 (if done (setq found i)))
3358 (setq n (if forward (1- n) (1+ n))))))
3359 (if found
3360 (rmail-show-message found)
3361 (error "No %s message with same subject"
3362 (if forward "following" "previous")))))
3363
3364 (defun rmail-previous-same-subject (n)
3365 "Go to the previous mail message having the same subject header.
3366 With prefix argument N, do this N times.
3367 If N is negative, go forwards instead."
3368 (interactive "p")
3369 (rmail-next-same-subject (- n)))
3370 \f
3371 ;;;; *** Rmail Message Deletion Commands ***
3372
3373 (defun rmail-message-deleted-p (n)
3374 (= (aref rmail-deleted-vector n) ?D))
3375
3376 (defun rmail-set-message-deleted-p (n state)
3377 (aset rmail-deleted-vector n (if state ?D ?\ )))
3378
3379 (defun rmail-delete-message ()
3380 "Delete this message and stay on it."
3381 (interactive)
3382 (rmail-set-attribute "deleted" t)
3383 (run-hooks 'rmail-delete-message-hook))
3384
3385 (defun rmail-undelete-previous-message ()
3386 "Back up to deleted message, select it, and undelete it."
3387 (interactive)
3388 (set-buffer rmail-buffer)
3389 (let ((msg rmail-current-message))
3390 (while (and (> msg 0)
3391 (not (rmail-message-deleted-p msg)))
3392 (setq msg (1- msg)))
3393 (if (= msg 0)
3394 (error "No previous deleted message")
3395 (if (/= msg rmail-current-message)
3396 (rmail-show-message msg))
3397 (rmail-set-attribute "deleted" nil)
3398 (if (rmail-summary-exists)
3399 (save-excursion
3400 (set-buffer rmail-summary-buffer)
3401 (rmail-summary-mark-undeleted msg)))
3402 (rmail-maybe-display-summary))))
3403
3404 (defun rmail-delete-forward (&optional backward)
3405 "Delete this message and move to next nondeleted one.
3406 Deleted messages stay in the file until the \\[rmail-expunge] command is given.
3407 With prefix argument, delete and move backward.
3408
3409 Returns t if a new message is displayed after the delete, or nil otherwise."
3410 (interactive "P")
3411 (rmail-set-attribute "deleted" t)
3412 (run-hooks 'rmail-delete-message-hook)
3413 (let ((del-msg rmail-current-message))
3414 (if (rmail-summary-exists)
3415 (rmail-select-summary
3416 (rmail-summary-mark-deleted del-msg)))
3417 (prog1 (rmail-next-undeleted-message (if backward -1 1))
3418 (rmail-maybe-display-summary))))
3419
3420 (defun rmail-delete-backward ()
3421 "Delete this message and move to previous nondeleted one.
3422 Deleted messages stay in the file until the \\[rmail-expunge] command is given."
3423 (interactive)
3424 (rmail-delete-forward t))
3425
3426 ;; Compute the message number a given message would have after expunging.
3427 ;; The present number of the message is OLDNUM.
3428 ;; DELETEDVEC should be rmail-deleted-vector.
3429 ;; The value is nil for a message that would be deleted.
3430 (defun rmail-msg-number-after-expunge (deletedvec oldnum)
3431 (if (or (null oldnum) (= (aref deletedvec oldnum) ?D))
3432 nil
3433 (let ((i 0)
3434 (newnum 0))
3435 (while (< i oldnum)
3436 (if (/= (aref deletedvec i) ?D)
3437 (setq newnum (1+ newnum)))
3438 (setq i (1+ i)))
3439 newnum)))
3440
3441 (defun rmail-expunge-confirmed ()
3442 "Return t if deleted message should be expunged. If necessary, ask the user.
3443 See also user-option `rmail-confirm-expunge'."
3444 (set-buffer rmail-buffer)
3445 (or (not (stringp rmail-deleted-vector))
3446 (not (string-match "D" rmail-deleted-vector))
3447 (null rmail-confirm-expunge)
3448 (funcall rmail-confirm-expunge
3449 "Erase deleted messages from Rmail file? ")))
3450
3451 (defun rmail-only-expunge (&optional dont-show)
3452 "Actually erase all deleted messages in the file."
3453 (interactive)
3454 (set-buffer rmail-buffer)
3455 (message "Expunging deleted messages...")
3456 ;; Discard all undo records for this buffer.
3457 (or (eq buffer-undo-list t)
3458 (setq buffer-undo-list nil))
3459 (rmail-maybe-set-message-counters)
3460 (let* ((omax (- (buffer-size) (point-max)))
3461 (omin (- (buffer-size) (point-min)))
3462 (opoint (if (and (> rmail-current-message 0)
3463 (rmail-message-deleted-p rmail-current-message))
3464 0
3465 (if rmail-enable-mime
3466 (with-current-buffer rmail-view-buffer
3467 (- (point)(point-min)))
3468 (- (point) (point-min)))))
3469 (messages-head (cons (aref rmail-message-vector 0) nil))
3470 (messages-tail messages-head)
3471 ;; Don't make any undo records for the expunging.
3472 (buffer-undo-list t)
3473 (win))
3474 (unwind-protect
3475 (save-excursion
3476 (widen)
3477 (goto-char (point-min))
3478 (let ((counter 0)
3479 (number 1)
3480 (total rmail-total-messages)
3481 (new-message-number rmail-current-message)
3482 (new-summary nil)
3483 (new-msgref (list (list 0)))
3484 (rmailbuf (current-buffer))
3485 (buffer-read-only nil)
3486 (messages rmail-message-vector)
3487 (deleted rmail-deleted-vector)
3488 (summary rmail-summary-vector))
3489 (setq rmail-total-messages nil
3490 rmail-current-message nil
3491 rmail-message-vector nil
3492 rmail-deleted-vector nil
3493 rmail-summary-vector nil)
3494
3495 (while (<= number total)
3496 (if (= (aref deleted number) ?D)
3497 (progn
3498 (delete-region
3499 (marker-position (aref messages number))
3500 (marker-position (aref messages (1+ number))))
3501 (move-marker (aref messages number) nil)
3502 (if (> new-message-number counter)
3503 (setq new-message-number (1- new-message-number))))
3504 (setq counter (1+ counter))
3505 (setq messages-tail
3506 (setcdr messages-tail
3507 (cons (aref messages number) nil)))
3508 (setq new-summary
3509 (cons (if (= counter number) (aref summary (1- number)))
3510 new-summary))
3511 (setq new-msgref
3512 (cons (aref rmail-msgref-vector number)
3513 new-msgref))
3514 (setcar (car new-msgref) counter))
3515 (if (zerop (% (setq number (1+ number)) 20))
3516 (message "Expunging deleted messages...%d" number)))
3517 (setq messages-tail
3518 (setcdr messages-tail
3519 (cons (aref messages number) nil)))
3520 (setq rmail-current-message new-message-number
3521 rmail-total-messages counter
3522 rmail-message-vector (apply 'vector messages-head)
3523 rmail-deleted-vector (make-string (1+ counter) ?\ )
3524 rmail-summary-vector (vconcat (nreverse new-summary))
3525 rmail-msgref-vector (apply 'vector (nreverse new-msgref))
3526 win t)))
3527 (message "Expunging deleted messages...done")
3528 (if (not win)
3529 (narrow-to-region (- (buffer-size) omin) (- (buffer-size) omax)))
3530 (if (not dont-show)
3531 (rmail-show-message
3532 (if (zerop rmail-current-message) 1 nil)))
3533 (if rmail-enable-mime
3534 (goto-char (+ (point-min) opoint))
3535 (goto-char (+ (point) opoint))))))
3536
3537 (defun rmail-expunge ()
3538 "Erase deleted messages from Rmail file and summary buffer."
3539 (interactive)
3540 (when (rmail-expunge-confirmed)
3541 (rmail-only-expunge)
3542 (if (rmail-summary-exists)
3543 (rmail-select-summary (rmail-update-summary)))))
3544 \f
3545 ;;;; *** Rmail Mailing Commands ***
3546
3547 (defun rmail-start-mail (&optional noerase to subject in-reply-to cc
3548 replybuffer sendactions same-window others)
3549 (let (yank-action)
3550 (if replybuffer
3551 (setq yank-action (list 'insert-buffer replybuffer)))
3552 (setq others (cons (cons "cc" cc) others))
3553 (setq others (cons (cons "in-reply-to" in-reply-to) others))
3554 (if same-window
3555 (compose-mail to subject others
3556 noerase nil
3557 yank-action sendactions)
3558 (if rmail-mail-new-frame
3559 (prog1
3560 (compose-mail to subject others
3561 noerase 'switch-to-buffer-other-frame
3562 yank-action sendactions)
3563 ;; This is not a standard frame parameter;
3564 ;; nothing except sendmail.el looks at it.
3565 (modify-frame-parameters (selected-frame)
3566 '((mail-dedicated-frame . t))))
3567 (compose-mail to subject others
3568 noerase 'switch-to-buffer-other-window
3569 yank-action sendactions)))))
3570
3571 (defun rmail-mail ()
3572 "Send mail in another window.
3573 While composing the message, use \\[mail-yank-original] to yank the
3574 original message into it."
3575 (interactive)
3576 (rmail-start-mail nil nil nil nil nil rmail-view-buffer))
3577
3578 (defun rmail-continue ()
3579 "Continue composing outgoing message previously being composed."
3580 (interactive)
3581 (rmail-start-mail t))
3582
3583 (defun rmail-reply (just-sender)
3584 "Reply to the current message.
3585 Normally include CC: to all other recipients of original message;
3586 prefix argument means ignore them. While composing the reply,
3587 use \\[mail-yank-original] to yank the original message into it."
3588 (interactive "P")
3589 (let (from reply-to cc subject date to message-id references
3590 resent-to resent-cc resent-reply-to
3591 (msgnum rmail-current-message))
3592 (save-excursion
3593 (save-restriction
3594 (if rmail-enable-mime
3595 (narrow-to-region
3596 (goto-char (point-min))
3597 (if (search-forward "\n\n" nil 'move)
3598 (1+ (match-beginning 0))
3599 (point)))
3600 (widen)
3601 (goto-char (rmail-msgbeg rmail-current-message))
3602 (forward-line 1)
3603 (if (= (following-char) ?0)
3604 (narrow-to-region
3605 (progn (forward-line 2)
3606 (point))
3607 (progn (search-forward "\n\n" (rmail-msgend rmail-current-message)
3608 'move)
3609 (point)))
3610 (narrow-to-region (point)
3611 (progn (search-forward "\n*** EOOH ***\n")
3612 (beginning-of-line) (point)))))
3613 (setq from (mail-fetch-field "from")
3614 reply-to (or (mail-fetch-field "mail-reply-to" nil t)
3615 (mail-fetch-field "reply-to" nil t)
3616 from)
3617 subject (mail-fetch-field "subject")
3618 date (mail-fetch-field "date")
3619 message-id (mail-fetch-field "message-id")
3620 references (mail-fetch-field "references" nil nil t)
3621 resent-reply-to (mail-fetch-field "resent-reply-to" nil t)
3622 resent-cc (and (not just-sender)
3623 (mail-fetch-field "resent-cc" nil t))
3624 resent-to (or (mail-fetch-field "resent-to" nil t) "")
3625 ;;; resent-subject (mail-fetch-field "resent-subject")
3626 ;;; resent-date (mail-fetch-field "resent-date")
3627 ;;; resent-message-id (mail-fetch-field "resent-message-id")
3628 )
3629 (unless just-sender
3630 (if (mail-fetch-field "mail-followup-to" nil t)
3631 ;; If this header field is present, use it instead of the To and CC fields.
3632 (setq to (mail-fetch-field "mail-followup-to" nil t))
3633 (setq cc (or (mail-fetch-field "cc" nil t) "")
3634 to (or (mail-fetch-field "to" nil t) ""))))
3635
3636 ))
3637
3638 ;; Merge the resent-to and resent-cc into the to and cc.
3639 (if (and resent-to (not (equal resent-to "")))
3640 (if (not (equal to ""))
3641 (setq to (concat to ", " resent-to))
3642 (setq to resent-to)))
3643 (if (and resent-cc (not (equal resent-cc "")))
3644 (if (not (equal cc ""))
3645 (setq cc (concat cc ", " resent-cc))
3646 (setq cc resent-cc)))
3647 ;; Add `Re: ' to subject if not there already.
3648 (and (stringp subject)
3649 (setq subject
3650 (concat rmail-reply-prefix
3651 (if (let ((case-fold-search t))
3652 (string-match rmail-reply-regexp subject))
3653 (substring subject (match-end 0))
3654 subject))))
3655 (rmail-start-mail
3656 nil
3657 ;; Using mail-strip-quoted-names is undesirable with newer mailers
3658 ;; since they can handle the names unstripped.
3659 ;; I don't know whether there are other mailers that still
3660 ;; need the names to be stripped.
3661 ;;; (mail-strip-quoted-names reply-to)
3662 ;; Remove unwanted names from reply-to, since Mail-Followup-To
3663 ;; header causes all the names in it to wind up in reply-to, not
3664 ;; in cc. But if what's left is an empty list, use the original.
3665 (let* ((reply-to-list (rmail-dont-reply-to reply-to)))
3666 (if (string= reply-to-list "") reply-to reply-to-list))
3667 subject
3668 (rmail-make-in-reply-to-field from date message-id)
3669 (if just-sender
3670 nil
3671 ;; mail-strip-quoted-names is NOT necessary for rmail-dont-reply-to
3672 ;; to do its job.
3673 (let* ((cc-list (rmail-dont-reply-to
3674 (mail-strip-quoted-names
3675 (if (null cc) to (concat to ", " cc))))))
3676 (if (string= cc-list "") nil cc-list)))
3677 rmail-view-buffer
3678 (list (list 'rmail-mark-message
3679 rmail-buffer
3680 (with-current-buffer rmail-buffer
3681 (aref rmail-msgref-vector msgnum))
3682 "answered"))
3683 nil
3684 (list (cons "References" (concat (mapconcat 'identity references " ")
3685 " " message-id))))))
3686
3687 (defun rmail-mark-message (buffer msgnum-list attribute)
3688 "Give BUFFER's message number in MSGNUM-LIST the attribute ATTRIBUTE.
3689 This is use in the send-actions for message buffers.
3690 MSGNUM-LIST is a list of the form (MSGNUM)
3691 which is an element of rmail-msgref-vector."
3692 (save-excursion
3693 (set-buffer buffer)
3694 (if (car msgnum-list)
3695 (rmail-set-attribute attribute t (car msgnum-list)))))
3696
3697 (defun rmail-make-in-reply-to-field (from date message-id)
3698 (cond ((not from)
3699 (if message-id
3700 message-id
3701 nil))
3702 (mail-use-rfc822
3703 (require 'rfc822)
3704 (let ((tem (car (rfc822-addresses from))))
3705 (if message-id
3706 (if (or (not tem)
3707 (string-match
3708 (regexp-quote (if (string-match "@[^@]*\\'" tem)
3709 (substring tem 0
3710 (match-beginning 0))
3711 tem))
3712 message-id))
3713 ;; missing From, or Message-ID is sufficiently informative
3714 message-id
3715 (concat message-id " (" tem ")"))
3716 ;; Copy TEM, discarding text properties.
3717 (setq tem (copy-sequence tem))
3718 (set-text-properties 0 (length tem) nil tem)
3719 (setq tem (copy-sequence tem))
3720 ;; Use prin1 to fake RFC822 quoting
3721 (let ((field (prin1-to-string tem)))
3722 (if date
3723 (concat field "'s message of " date)
3724 field)))))
3725 ((let* ((foo "[^][\000-\037()<>@,;:\\\" ]+")
3726 (bar "[^][\000-\037()<>@,;:\\\"]+"))
3727 ;; These strings both match all non-ASCII characters.
3728 (or (string-match (concat "\\`[ \t]*\\(" bar
3729 "\\)\\(<" foo "@" foo ">\\)?[ \t]*\\'")
3730 ;; "Unix Loser <Foo@bar.edu>" => "Unix Loser"
3731 from)
3732 (string-match (concat "\\`[ \t]*<" foo "@" foo ">[ \t]*(\\("
3733 bar "\\))[ \t]*\\'")
3734 ;; "<Bugs@bar.edu>" (Losing Unix) => "Losing Unix"
3735 from)))
3736 (let ((start (match-beginning 1))
3737 (end (match-end 1)))
3738 ;; Trim whitespace which above regexp match allows
3739 (while (and (< start end)
3740 (memq (aref from start) '(?\t ?\ )))
3741 (setq start (1+ start)))
3742 (while (and (< start end)
3743 (memq (aref from (1- end)) '(?\t ?\ )))
3744 (setq end (1- end)))
3745 (let ((field (substring from start end)))
3746 (if date (setq field (concat "message from " field " on " date)))
3747 (if message-id
3748 ;; "<AA259@bar.edu> (message from Unix Loser on 1-Apr-89)"
3749 (concat message-id " (" field ")")
3750 field))))
3751 (t
3752 ;; If we can't kludge it simply, do it correctly
3753 (let ((mail-use-rfc822 t))
3754 (rmail-make-in-reply-to-field from date message-id)))))
3755 \f
3756 (defun rmail-forward (resend)
3757 "Forward the current message to another user.
3758 With prefix argument, \"resend\" the message instead of forwarding it;
3759 see the documentation of `rmail-resend'."
3760 (interactive "P")
3761 (if resend
3762 (call-interactively 'rmail-resend)
3763 (let ((forward-buffer rmail-buffer)
3764 (msgnum rmail-current-message)
3765 (subject (concat "["
3766 (let ((from (or (mail-fetch-field "From")
3767 (mail-fetch-field ">From"))))
3768 (if from
3769 (concat (mail-strip-quoted-names from) ": ")
3770 ""))
3771 (or (mail-fetch-field "Subject") "")
3772 "]")))
3773 (if (rmail-start-mail
3774 nil nil subject nil nil nil
3775 (list (list 'rmail-mark-message
3776 forward-buffer
3777 (with-current-buffer rmail-buffer
3778 (aref rmail-msgref-vector msgnum))
3779 "forwarded"))
3780 ;; If only one window, use it for the mail buffer.
3781 ;; Otherwise, use another window for the mail buffer
3782 ;; so that the Rmail buffer remains visible
3783 ;; and sending the mail will get back to it.
3784 (and (not rmail-mail-new-frame) (one-window-p t)))
3785 ;; The mail buffer is now current.
3786 (save-excursion
3787 ;; Insert after header separator--before signature if any.
3788 (goto-char (mail-text-start))
3789 (if (or rmail-enable-mime rmail-enable-mime-composing)
3790 (funcall rmail-insert-mime-forwarded-message-function
3791 forward-buffer)
3792 (insert "------- Start of forwarded message -------\n")
3793 ;; Quote lines with `- ' if they start with `-'.
3794 (let ((beg (point)) end)
3795 (setq end (point-marker))
3796 (set-marker-insertion-type end t)
3797 (insert-buffer-substring forward-buffer)
3798 (goto-char beg)
3799 (while (re-search-forward "^-" end t)
3800 (beginning-of-line)
3801 (insert "- ")
3802 (forward-line 1))
3803 (goto-char end)
3804 (skip-chars-backward "\n")
3805 (if (< (point) end)
3806 (forward-char 1))
3807 (delete-region (point) end)
3808 (set-marker end nil))
3809 (insert "------- End of forwarded message -------\n"))
3810 (push-mark))))))
3811 \f
3812 (defun rmail-resend (address &optional from comment mail-alias-file)
3813 "Resend current message to ADDRESSES.
3814 ADDRESSES should be a single address, a string consisting of several
3815 addresses separated by commas, or a list of addresses.
3816
3817 Optional FROM is the address to resend the message from, and
3818 defaults from the value of `user-mail-address'.
3819 Optional COMMENT is a string to insert as a comment in the resent message.
3820 Optional ALIAS-FILE is alternate aliases file to be used by sendmail,
3821 typically for purposes of moderating a list."
3822 (interactive "sResend to: ")
3823 (require 'sendmail)
3824 (require 'mailalias)
3825 (unless (or (eq rmail-view-buffer (current-buffer))
3826 (eq rmail-buffer (current-buffer)))
3827 (error "Not an Rmail buffer"))
3828 (if (not from) (setq from user-mail-address))
3829 (let ((tembuf (generate-new-buffer " sendmail temp"))
3830 (case-fold-search nil)
3831 (mail-personal-alias-file
3832 (or mail-alias-file mail-personal-alias-file))
3833 (mailbuf rmail-buffer))
3834 (unwind-protect
3835 (with-current-buffer tembuf
3836 ;;>> Copy message into temp buffer
3837 (if rmail-enable-mime
3838 (funcall rmail-insert-mime-resent-message-function mailbuf)
3839 (insert-buffer-substring mailbuf))
3840 (goto-char (point-min))
3841 ;; Delete any Sender field, since that's not specifiable.
3842 ; Only delete Sender fields in the actual header.
3843 (re-search-forward "^$" nil 'move)
3844 ; Using "while" here rather than "if" because some buggy mail
3845 ; software may have inserted multiple Sender fields.
3846 (while (re-search-backward "^Sender:" nil t)
3847 (let (beg)
3848 (setq beg (point))
3849 (forward-line 1)
3850 (while (looking-at "[ \t]")
3851 (forward-line 1))
3852 (delete-region beg (point))))
3853 ; Go back to the beginning of the buffer so the Resent- fields
3854 ; are inserted there.
3855 (goto-char (point-min))
3856 ;;>> Insert resent-from:
3857 (insert "Resent-From: " from "\n")
3858 (insert "Resent-Date: " (mail-rfc822-date) "\n")
3859 ;;>> Insert resent-to: and bcc if need be.
3860 (let ((before (point)))
3861 (if mail-self-blind
3862 (insert "Resent-Bcc: " (user-login-name) "\n"))
3863 (insert "Resent-To: " (if (stringp address)
3864 address
3865 (mapconcat 'identity address ",\n\t"))
3866 "\n")
3867 ;; Expand abbrevs in the recipients.
3868 (save-excursion
3869 (if (featurep 'mailabbrev)
3870 (let ((end (point-marker))
3871 (local-abbrev-table mail-abbrevs)
3872 (old-syntax-table (syntax-table)))
3873 (if (and (not (vectorp mail-abbrevs))
3874 (file-exists-p mail-personal-alias-file))
3875 (build-mail-abbrevs))
3876 (unless mail-abbrev-syntax-table
3877 (mail-abbrev-make-syntax-table))
3878 (set-syntax-table mail-abbrev-syntax-table)
3879 (goto-char before)
3880 (while (and (< (point) end)
3881 (progn (forward-word 1)
3882 (<= (point) end)))
3883 (expand-abbrev))
3884 (set-syntax-table old-syntax-table))
3885 (expand-mail-aliases before (point)))))
3886 ;;>> Set up comment, if any.
3887 (if (and (sequencep comment) (not (zerop (length comment))))
3888 (let ((before (point))
3889 after)
3890 (insert comment)
3891 (or (eolp) (insert "\n"))
3892 (setq after (point))
3893 (goto-char before)
3894 (while (< (point) after)
3895 (insert "Resent-Comment: ")
3896 (forward-line 1))))
3897 ;; Don't expand aliases in the destination fields
3898 ;; of the original message.
3899 (let (mail-aliases)
3900 (funcall send-mail-function)))
3901 (kill-buffer tembuf))
3902 (with-current-buffer rmail-buffer
3903 (rmail-set-attribute "resent" t rmail-current-message))))
3904 \f
3905 (defvar mail-unsent-separator
3906 (concat "^ *---+ +Unsent message follows +---+ *$\\|"
3907 "^ *---+ +Returned message +---+ *$\\|"
3908 "^ *---+ *Returned mail follows *---+ *$\\|"
3909 "^Start of returned message$\\|"
3910 "^---+ Below this line is a copy of the message.$\\|"
3911 "^ *---+ +Original message +---+ *$\\|"
3912 "^ *--+ +begin message +--+ *$\\|"
3913 "^ *---+ +Original message follows +---+ *$\\|"
3914 "^ *---+ +Your message follows +---+ *$\\|"
3915 "^|? *---+ +Message text follows: +---+ *|?$\\|"
3916 "^ *---+ +This is a copy of \\w+ message, including all the headers.*---+ *$")
3917 "A regexp that matches the separator before the text of a failed message.")
3918
3919 (defvar mail-mime-unsent-header "^Content-Type: message/rfc822 *$"
3920 "A regexp that matches the header of a MIME body part with a failed message.")
3921
3922 (defun rmail-retry-failure ()
3923 "Edit a mail message which is based on the contents of the current message.
3924 For a message rejected by the mail system, extract the interesting headers and
3925 the body of the original message.
3926 If the failed message is a MIME multipart message, it is searched for a
3927 body part with a header which matches the variable `mail-mime-unsent-header'.
3928 Otherwise, the variable `mail-unsent-separator' should match the string that
3929 delimits the returned original message.
3930 The variable `rmail-retry-ignored-headers' is a regular expression
3931 specifying headers which should not be copied into the new message."
3932 (interactive)
3933 (require 'mail-utils)
3934 (let ((rmail-this-buffer (current-buffer))
3935 (msgnum rmail-current-message)
3936 bounce-start bounce-end bounce-indent resending
3937 ;; Fetch any content-type header in current message
3938 ;; Must search thru the whole unpruned header.
3939 (content-type
3940 (save-excursion
3941 (save-restriction
3942 (rmail-narrow-to-non-pruned-header)
3943 (mail-fetch-field "Content-Type") ))))
3944 (save-excursion
3945 (goto-char (point-min))
3946 (let ((case-fold-search t))
3947 (if (and content-type
3948 (string-match
3949 ";[\n\t ]*boundary=\"?\\([-0-9a-z'()+_,./:=? ]+\\)\"?"
3950 content-type))
3951 ;; Handle a MIME multipart bounce message.
3952 (let ((codestring
3953 (concat "\n--"
3954 (substring content-type (match-beginning 1)
3955 (match-end 1)))))
3956 (unless (re-search-forward mail-mime-unsent-header nil t)
3957 (error "Cannot find beginning of header in failed message"))
3958 (unless (search-forward "\n\n" nil t)
3959 (error "Cannot find start of Mime data in failed message"))
3960 (setq bounce-start (point))
3961 (if (search-forward codestring nil t)
3962 (setq bounce-end (match-beginning 0))
3963 (setq bounce-end (point-max))))
3964 ;; Non-MIME bounce.
3965 (or (re-search-forward mail-unsent-separator nil t)
3966 (error "Cannot parse this as a failure message"))
3967 (skip-chars-forward "\n")
3968 ;; Support a style of failure message in which the original
3969 ;; message is indented, and included within lines saying
3970 ;; `Start of returned message' and `End of returned message'.
3971 (if (looking-at " +Received:")
3972 (progn
3973 (setq bounce-start (point))
3974 (skip-chars-forward " ")
3975 (setq bounce-indent (- (current-column)))
3976 (goto-char (point-max))
3977 (re-search-backward "^End of returned message$" nil t)
3978 (setq bounce-end (point)))
3979 ;; One message contained a few random lines before
3980 ;; the old message header. The first line of the
3981 ;; message started with two hyphens. A blank line
3982 ;; followed these random lines. The same line
3983 ;; beginning with two hyphens was possibly marking
3984 ;; the end of the message.
3985 (if (looking-at "^--")
3986 (let ((boundary (buffer-substring-no-properties
3987 (point)
3988 (progn (end-of-line) (point)))))
3989 (search-forward "\n\n")
3990 (skip-chars-forward "\n")
3991 (setq bounce-start (point))
3992 (goto-char (point-max))
3993 (search-backward (concat "\n\n" boundary) bounce-start t)
3994 (setq bounce-end (point)))
3995 (setq bounce-start (point)
3996 bounce-end (point-max)))
3997 (unless (search-forward "\n\n" nil t)
3998 (error "Cannot find end of header in failed message"))))))
3999 ;; We have found the message that bounced, within the current message.
4000 ;; Now start sending new message; default header fields from original.
4001 ;; Turn off the usual actions for initializing the message body
4002 ;; because we want to get only the text from the failure message.
4003 (let (mail-signature mail-setup-hook)
4004 (if (rmail-start-mail nil nil nil nil nil rmail-this-buffer
4005 (list (list 'rmail-mark-message
4006 rmail-this-buffer
4007 (aref rmail-msgref-vector msgnum)
4008 "retried")))
4009 ;; Insert original text as initial text of new draft message.
4010 ;; Bind inhibit-read-only since the header delimiter
4011 ;; of the previous message was probably read-only.
4012 (let ((inhibit-read-only t)
4013 rmail-displayed-headers
4014 rmail-ignored-headers)
4015 (erase-buffer)
4016 (insert-buffer-substring rmail-this-buffer
4017 bounce-start bounce-end)
4018 (goto-char (point-min))
4019 (if bounce-indent
4020 (indent-rigidly (point-min) (point-max) bounce-indent))
4021 (rmail-clear-headers rmail-retry-ignored-headers)
4022 (rmail-clear-headers "^sender:\\|^return-path:\\|^received:")
4023 (mail-sendmail-delimit-header)
4024 (save-restriction
4025 (narrow-to-region (point-min) (mail-header-end))
4026 (setq resending (mail-fetch-field "resent-to"))
4027 (if mail-self-blind
4028 (if resending
4029 (insert "Resent-Bcc: " (user-login-name) "\n")
4030 (insert "BCC: " (user-login-name) "\n"))))
4031 (goto-char (point-min))
4032 (mail-position-on-field (if resending "Resent-To" "To") t))))))
4033 \f
4034 (defun rmail-summary-exists ()
4035 "Non-nil if in an RMAIL buffer and an associated summary buffer exists.
4036 In fact, the non-nil value returned is the summary buffer itself."
4037 (and rmail-summary-buffer (buffer-name rmail-summary-buffer)
4038 rmail-summary-buffer))
4039
4040 (defun rmail-summary-displayed ()
4041 "t if in RMAIL buffer and an associated summary buffer is displayed."
4042 (and rmail-summary-buffer (get-buffer-window rmail-summary-buffer)))
4043
4044 (defcustom rmail-redisplay-summary nil
4045 "*Non-nil means Rmail should show the summary when it changes.
4046 This has an effect only if a summary buffer exists."
4047 :type 'boolean
4048 :group 'rmail-summary)
4049
4050 (defcustom rmail-summary-window-size nil
4051 "*Non-nil means specify the height for an Rmail summary window."
4052 :type '(choice (const :tag "Disabled" nil) integer)
4053 :group 'rmail-summary)
4054
4055 ;; Put the summary buffer back on the screen, if user wants that.
4056 (defun rmail-maybe-display-summary ()
4057 (let ((selected (selected-window))
4058 window)
4059 ;; If requested, make sure the summary is displayed.
4060 (and rmail-summary-buffer (buffer-name rmail-summary-buffer)
4061 rmail-redisplay-summary
4062 (if (get-buffer-window rmail-summary-buffer 0)
4063 ;; It's already in some frame; show that one.
4064 (let ((frame (window-frame
4065 (get-buffer-window rmail-summary-buffer 0))))
4066 (make-frame-visible frame)
4067 (raise-frame frame))
4068 (display-buffer rmail-summary-buffer)))
4069 ;; If requested, set the height of the summary window.
4070 (and rmail-summary-buffer (buffer-name rmail-summary-buffer)
4071 rmail-summary-window-size
4072 (setq window (get-buffer-window rmail-summary-buffer))
4073 ;; Don't try to change the size if just one window in frame.
4074 (not (eq window (frame-root-window (window-frame window))))
4075 (unwind-protect
4076 (progn
4077 (select-window window)
4078 (enlarge-window (- rmail-summary-window-size (window-height))))
4079 (select-window selected)))))
4080 \f
4081 ;;;; *** Rmail Local Fontification ***
4082
4083 (defun rmail-fontify-buffer-function ()
4084 ;; This function's symbol is bound to font-lock-fontify-buffer-function.
4085 (add-hook 'rmail-show-message-hook 'rmail-fontify-message nil t)
4086 ;; If we're already showing a message, fontify it now.
4087 (if rmail-current-message (rmail-fontify-message))
4088 ;; Prevent Font Lock mode from kicking in.
4089 (setq font-lock-fontified t))
4090
4091 (defun rmail-unfontify-buffer-function ()
4092 ;; This function's symbol is bound to font-lock-fontify-unbuffer-function.
4093 (let ((modified (buffer-modified-p))
4094 (buffer-undo-list t) (inhibit-read-only t)
4095 before-change-functions after-change-functions
4096 buffer-file-name buffer-file-truename)
4097 (save-restriction
4098 (widen)
4099 (remove-hook 'rmail-show-message-hook 'rmail-fontify-message t)
4100 (remove-text-properties (point-min) (point-max) '(rmail-fontified nil))
4101 (font-lock-default-unfontify-buffer)
4102 (and (not modified) (buffer-modified-p) (set-buffer-modified-p nil)))))
4103
4104 (defun rmail-fontify-message ()
4105 ;; Fontify the current message if it is not already fontified.
4106 (if (text-property-any (point-min) (point-max) 'rmail-fontified nil)
4107 (let ((modified (buffer-modified-p))
4108 (buffer-undo-list t) (inhibit-read-only t)
4109 before-change-functions after-change-functions
4110 buffer-file-name buffer-file-truename)
4111 (save-excursion
4112 (save-match-data
4113 (add-text-properties (point-min) (point-max) '(rmail-fontified t))
4114 (font-lock-fontify-region (point-min) (point-max))
4115 (and (not modified) (buffer-modified-p) (set-buffer-modified-p nil)))))))
4116 \f
4117 ;;; Speedbar support for RMAIL files.
4118 (eval-when-compile (require 'speedbar))
4119
4120 (defvar rmail-speedbar-match-folder-regexp "^[A-Z0-9]+\\(\\.[A-Z0-9]+\\)?$"
4121 "*This regex is used to match folder names to be displayed in speedbar.
4122 Enabling this will permit speedbar to display your folders for easy
4123 browsing, and moving of messages.")
4124
4125 (defvar rmail-speedbar-last-user nil
4126 "The last user to be displayed in the speedbar.")
4127
4128 (defvar rmail-speedbar-key-map nil
4129 "Keymap used when in rmail display mode.")
4130
4131 (defun rmail-install-speedbar-variables ()
4132 "Install those variables used by speedbar to enhance rmail."
4133 (if rmail-speedbar-key-map
4134 nil
4135 (setq rmail-speedbar-key-map (speedbar-make-specialized-keymap))
4136
4137 (define-key rmail-speedbar-key-map "e" 'speedbar-edit-line)
4138 (define-key rmail-speedbar-key-map "r" 'speedbar-edit-line)
4139 (define-key rmail-speedbar-key-map "\C-m" 'speedbar-edit-line)
4140 (define-key rmail-speedbar-key-map "M"
4141 'rmail-speedbar-move-message-to-folder-on-line)))
4142
4143 (defvar rmail-speedbar-menu-items
4144 '(["Read Folder" speedbar-edit-line t]
4145 ["Move message to folder" rmail-speedbar-move-message-to-folder-on-line
4146 (save-excursion (beginning-of-line)
4147 (looking-at "<M> "))])
4148 "Additional menu-items to add to speedbar frame.")
4149
4150 ;; Make sure our special speedbar major mode is loaded
4151 (if (featurep 'speedbar)
4152 (rmail-install-speedbar-variables)
4153 (add-hook 'speedbar-load-hook 'rmail-install-speedbar-variables))
4154
4155 (defun rmail-speedbar-buttons (buffer)
4156 "Create buttons for BUFFER containing rmail messages.
4157 Click on the address under Reply to: to reply to this person.
4158 Under Folders: Click a name to read it, or on the <M> to move the
4159 current message into that RMAIL folder."
4160 (let ((from nil))
4161 (save-excursion
4162 (set-buffer buffer)
4163 (goto-char (point-min))
4164 (if (not (re-search-forward "^Reply-To: " nil t))
4165 (if (not (re-search-forward "^From:? " nil t))
4166 (setq from t)))
4167 (if from
4168 nil
4169 (setq from (buffer-substring (point) (save-excursion
4170 (end-of-line)
4171 (point))))))
4172 (goto-char (point-min))
4173 (if (and (looking-at "Reply to:")
4174 (equal from rmail-speedbar-last-user))
4175 nil
4176 (setq rmail-speedbar-last-user from)
4177 (erase-buffer)
4178 (insert "Reply To:\n")
4179 (if (stringp from)
4180 (speedbar-insert-button from 'speedbar-directory-face 'highlight
4181 'rmail-speedbar-button 'rmail-reply))
4182 (insert "Folders:\n")
4183 (let* ((case-fold-search nil)
4184 (df (directory-files (save-excursion (set-buffer buffer)
4185 default-directory)
4186 nil rmail-speedbar-match-folder-regexp)))
4187 (while df
4188 (speedbar-insert-button "<M>" 'speedbar-button-face 'highlight
4189 'rmail-speedbar-move-message (car df))
4190 (speedbar-insert-button (car df) 'speedbar-file-face 'highlight
4191 'rmail-speedbar-find-file nil t)
4192 (setq df (cdr df)))))))
4193
4194 (defun rmail-speedbar-button (text token indent)
4195 "Execute an rmail command specified by TEXT.
4196 The command used is TOKEN. INDENT is not used."
4197 (speedbar-with-attached-buffer
4198 (funcall token t)))
4199
4200 (defun rmail-speedbar-find-file (text token indent)
4201 "Load in the rmail file TEXT.
4202 TOKEN and INDENT are not used."
4203 (speedbar-with-attached-buffer
4204 (message "Loading in RMAIL file %s..." text)
4205 (find-file text)))
4206
4207 (defun rmail-speedbar-move-message-to-folder-on-line ()
4208 "If the current line is a folder, move current message to it."
4209 (interactive)
4210 (save-excursion
4211 (beginning-of-line)
4212 (if (re-search-forward "<M> " (save-excursion (end-of-line) (point)) t)
4213 (progn
4214 (forward-char -2)
4215 (speedbar-do-function-pointer)))))
4216
4217 (defun rmail-speedbar-move-message (text token indent)
4218 "From button TEXT, copy current message to the rmail file specified by TOKEN.
4219 TEXT and INDENT are not used."
4220 (speedbar-with-attached-buffer
4221 (message "Moving message to %s" token)
4222 (rmail-output-to-rmail-file token)))
4223
4224 ; Functions for setting, getting and encoding the POP password.
4225 ; The password is encoded to prevent it from being easily accessible
4226 ; to "prying eyes." Obviously, this encoding isn't "real security,"
4227 ; nor is it meant to be.
4228
4229 ;;;###autoload
4230 (defun rmail-set-remote-password (password)
4231 "Set PASSWORD to be used for retrieving mail from a POP or IMAP server."
4232 (interactive "sPassword: ")
4233 (if password
4234 (setq rmail-encoded-remote-password
4235 (rmail-encode-string password (emacs-pid)))
4236 (setq rmail-remote-password nil)
4237 (setq rmail-encoded-remote-password nil)))
4238
4239 (defun rmail-get-remote-password (imap)
4240 "Get the password for retrieving mail from a POP or IMAP server. If none
4241 has been set, then prompt the user for one."
4242 (when (not rmail-encoded-remote-password)
4243 (if (not rmail-remote-password)
4244 (setq rmail-remote-password
4245 (read-passwd (if imap
4246 "IMAP password: "
4247 "POP password: "))))
4248 (rmail-set-remote-password rmail-remote-password)
4249 (setq rmail-remote-password nil))
4250 (rmail-encode-string rmail-encoded-remote-password (emacs-pid)))
4251
4252 (defun rmail-have-password ()
4253 (or rmail-remote-password rmail-encoded-remote-password))
4254
4255 (defun rmail-encode-string (string mask)
4256 "Encode STRING with integer MASK, by taking the exclusive OR of the
4257 lowest byte in the mask with the first character of string, the
4258 second-lowest-byte with the second character of the string, etc.,
4259 restarting at the lowest byte of the mask whenever it runs out.
4260 Returns the encoded string. Calling the function again with an
4261 encoded string (and the same mask) will decode the string."
4262 (setq mask (abs mask)) ; doesn't work if negative
4263 (let* ((string-vector (string-to-vector string)) (i 0)
4264 (len (length string-vector)) (curmask mask) charmask)
4265 (while (< i len)
4266 (if (= curmask 0)
4267 (setq curmask mask))
4268 (setq charmask (% curmask 256))
4269 (setq curmask (lsh curmask -8))
4270 (aset string-vector i (logxor charmask (aref string-vector i)))
4271 (setq i (1+ i)))
4272 (concat string-vector)))
4273
4274 ;;;; Desktop support
4275
4276 (defun rmail-restore-desktop-buffer (desktop-buffer-file-name
4277 desktop-buffer-name
4278 desktop-buffer-misc)
4279 "Restore an rmail buffer specified in a desktop file."
4280 (condition-case error
4281 (progn
4282 (rmail-input desktop-buffer-file-name)
4283 (if (eq major-mode 'rmail-mode)
4284 (current-buffer)
4285 rmail-buffer))
4286 (file-locked
4287 (kill-buffer (current-buffer))
4288 nil)))
4289
4290 (add-to-list 'desktop-buffer-mode-handlers
4291 '(rmail-mode . rmail-restore-desktop-buffer))
4292
4293 (provide 'rmail)
4294
4295 ;;; arch-tag: cff0a950-57fe-4f73-a86e-91ff75afd06c
4296 ;;; rmail.el ends here