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