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