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