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