]> code.delx.au - gnu-emacs/blob - lisp/mail/sendmail.el
9269d7f7d836e80986d346b5435f91b6c06c3c64
[gnu-emacs] / lisp / mail / sendmail.el
1 ;;; sendmail.el --- mail sending commands for Emacs
2
3 ;; Copyright (C) 1985-1986, 1992-1996, 1998, 2000-2015 Free Software
4 ;; Foundation, Inc.
5
6 ;; Maintainer: emacs-devel@gnu.org
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 3 of the License, or
14 ;; (at your option) 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. If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25
26 ;; This mode provides mail-sending facilities from within Emacs. It is
27 ;; documented in the Emacs user's manual.
28
29 ;;; Code:
30 (require 'mail-utils)
31
32 (require 'rfc2047)
33
34 (defgroup sendmail nil
35 "Mail sending commands for Emacs."
36 :prefix "mail-"
37 :group 'mail)
38
39 (defcustom mail-setup-with-from t
40 "Non-nil means insert `From:' field when setting up the message."
41 :type 'boolean
42 :group 'sendmail
43 :version "22.1")
44
45 (defcustom sendmail-program
46 (or (executable-find "sendmail")
47 (cond
48 ((file-exists-p "/usr/sbin/sendmail") "/usr/sbin/sendmail")
49 ((file-exists-p "/usr/lib/sendmail") "/usr/lib/sendmail")
50 ((file-exists-p "/usr/ucblib/sendmail") "/usr/ucblib/sendmail")
51 (t "sendmail")))
52 "Program used to send messages."
53 :version "24.1" ; add executable-find, remove fakemail
54 :group 'mail
55 :type 'file)
56
57 ;;;###autoload
58 (defcustom mail-from-style 'default
59 "Specifies how \"From:\" fields look.
60
61 If nil, they contain just the return address like:
62 king@grassland.com
63 If `parens', they look like:
64 king@grassland.com (Elvis Parsley)
65 If `angles', they look like:
66 Elvis Parsley <king@grassland.com>
67
68 Otherwise, most addresses look like `angles', but they look like
69 `parens' if `angles' would need quoting and `parens' would not."
70 ;; The value `system-default' is now deprecated.
71 :type '(choice (const :tag "simple" nil)
72 (const parens)
73 (const angles)
74 (const default))
75 :version "20.3"
76 :group 'sendmail)
77
78 ;;;###autoload
79 (defcustom mail-specify-envelope-from nil
80 "If non-nil, specify the envelope-from address when sending mail.
81 The value used to specify it is whatever is found in
82 the variable `mail-envelope-from', with `user-mail-address' as fallback.
83
84 On most systems, specifying the envelope-from address is a
85 privileged operation. This variable affects sendmail and
86 smtpmail -- if you use feedmail to send mail, see instead the
87 variable `feedmail-deduce-envelope-from'."
88 :version "21.1"
89 :type 'boolean
90 :group 'sendmail)
91
92 (defcustom mail-envelope-from nil
93 "If non-nil, designate the envelope-from address when sending mail.
94 This only has an effect if `mail-specify-envelope-from' is non-nil.
95 The value should be either a string, or the symbol `header' (in
96 which case the contents of the \"From\" header of the message
97 being sent is used), or nil (in which case the value of
98 `user-mail-address' is used)."
99 :version "21.1"
100 :type '(choice (string :tag "From-name")
101 (const :tag "Use From: header from message" header)
102 (const :tag "Use `user-mail-address'" nil))
103 :group 'sendmail)
104
105 ;;;###autoload
106 (defcustom mail-self-blind nil
107 "Non-nil means insert BCC to self in messages to be sent.
108 This is done when the message is initialized,
109 so you can remove or alter the BCC field to override the default."
110 :type 'boolean
111 :group 'sendmail)
112
113 ;;;###autoload
114 (defcustom mail-interactive t
115 ;; We used to use a default of nil rather than t, but nowadays it is very
116 ;; common for sendmail to be misconfigured, so one cannot rely on the
117 ;; bounce message to be delivered anywhere, least of all to the
118 ;; user's mailbox.
119 "Non-nil means when sending a message wait for and display errors.
120 Otherwise, let mailer send back a message to report errors."
121 :type 'boolean
122 :version "23.1" ; changed from nil to t
123 :group 'sendmail)
124
125 (defcustom mail-yank-ignored-headers
126 (concat "^"
127 (regexp-opt '("via" "mail-from" "origin" "status" "remailed"
128 "received" "message-id" "summary-line" "to" "subject"
129 "in-reply-to" "return-path" "mail-reply-to"
130 ;; Should really be rmail-attribute-header and
131 ;; rmail-keyword-header, but this file does not
132 ;; require rmail (at run time).
133 "x-rmail-attributes" "x-rmail-keywords"
134 "mail-followup-to") "\\(?:")
135 ":")
136 "Delete these headers from old message when it's inserted in a reply."
137 :type 'regexp
138 :group 'sendmail
139 :version "23.1")
140
141 ;; Useful to set in site-init.el
142 ;;;###autoload
143 (defcustom send-mail-function
144 ;; Assume smtpmail is the preferred choice if it's already configured.
145 (if (and (boundp 'smtpmail-smtp-server)
146 smtpmail-smtp-server)
147 'smtpmail-send-it 'sendmail-query-once)
148 "Function to call to send the current buffer as mail.
149 The headers should be delimited by a line which is
150 not a valid RFC822 header or continuation line,
151 that matches the variable `mail-header-separator'.
152 This is used by the default mail-sending commands. See also
153 `message-send-mail-function' for use with the Message package."
154 :type '(radio (function-item sendmail-send-it :tag "Use Sendmail package")
155 (function-item sendmail-query-once :tag "Query the user")
156 (function-item smtpmail-send-it :tag "Use SMTPmail package")
157 (function-item feedmail-send-it :tag "Use Feedmail package")
158 (function-item mailclient-send-it :tag "Use Mailclient package")
159 function)
160 :version "24.1"
161 :group 'sendmail)
162
163 ;;;###autoload
164 (defcustom mail-header-separator (purecopy "--text follows this line--")
165 "Line used to separate headers from text in messages being composed."
166 :type 'string
167 :group 'sendmail)
168
169 ;; Set up mail-header-separator for use as a category text property.
170 (put 'mail-header-separator 'rear-nonsticky '(category))
171 ;; This was a nice idea, for preventing accidental modification of
172 ;; the separator. But I found it also prevented or obstructed
173 ;; certain deliberate operations, such as copying the separator line
174 ;; up to the top to send myself a copy of an already sent outgoing message
175 ;; and other things. So I turned it off. --rms.
176 ;;(put 'mail-header-separator 'read-only t)
177
178 ;;;###autoload
179 (defcustom mail-archive-file-name nil
180 "Name of file to write all outgoing messages in, or nil for none.
181 This is normally an mbox file, but for backwards compatibility may also
182 be a Babyl file."
183 :type '(choice file (const nil))
184 :group 'sendmail)
185
186 ;;;###autoload
187 (defcustom mail-default-reply-to nil
188 "Address to insert as default Reply-to field of outgoing messages.
189 If nil, it will be initialized from the REPLYTO environment variable
190 when you first send mail."
191 :type '(choice (const nil) string)
192 :group 'sendmail)
193
194 (defcustom mail-alias-file nil
195 "If non-nil, the name of a file to use instead of the sendmail default.
196 This file defines aliases to be expanded by the mailer; this is a different
197 feature from that of defining aliases in `.mailrc' to be expanded in Emacs.
198 This variable has no effect unless your system uses sendmail as its mailer.
199 The default file is defined in sendmail's configuration file, e.g.
200 `/etc/aliases'."
201 :type '(choice (const :tag "Sendmail default" nil) file)
202 :group 'sendmail)
203
204 ;;;###autoload
205 (defcustom mail-personal-alias-file (purecopy "~/.mailrc")
206 "If non-nil, the name of the user's personal mail alias file.
207 This file typically should be in same format as the `.mailrc' file used by
208 the `Mail' or `mailx' program.
209 This file need not actually exist."
210 :type '(choice (const nil) file)
211 :group 'sendmail)
212
213 ;;;###autoload
214 (defcustom mail-setup-hook nil
215 "Normal hook, run each time a new outgoing message is initialized."
216 :type 'hook
217 :options '(fortune-to-signature spook mail-abbrevs-setup)
218 :group 'sendmail)
219
220 ;;;###autoload
221 (defvar mail-aliases t
222 "Alist of mail address aliases,
223 or t meaning should be initialized from your mail aliases file.
224 \(The file's name is normally `~/.mailrc', but `mail-personal-alias-file'
225 can specify a different file name.)
226 The alias definitions in the file have this form:
227 alias ALIAS MEANING")
228
229 (defvar mail-alias-modtime nil
230 "The modification time of your mail alias file when it was last examined.")
231
232 ;;;###autoload
233 (defcustom mail-yank-prefix "> "
234 "Prefix insert on lines of yanked message being replied to.
235 If this is nil, use indentation, as specified by `mail-indentation-spaces'."
236 :type '(choice (const nil) string)
237 :group 'sendmail)
238
239 ;;;###autoload
240 (defcustom mail-indentation-spaces 3
241 "Number of spaces to insert at the beginning of each cited line.
242 Used by `mail-yank-original' via `mail-indent-citation'."
243 :type 'integer
244 :group 'sendmail)
245
246 (defvar mail-yank-hooks nil
247 "Obsolete hook for modifying a citation just inserted in the mail buffer.
248 Each hook function can find the citation between (point) and (mark t).
249 And each hook function should leave point and mark around the citation
250 text as modified.
251 This is a normal hook, misnamed for historical reasons.
252 It is obsolete and mail agents should no longer use it.")
253 (make-obsolete-variable 'mail-yank-hooks 'mail-citation-hook "19.34")
254
255 ;;;###autoload
256 (defcustom mail-citation-hook nil
257 "Hook for modifying a citation just inserted in the mail buffer.
258 Each hook function can find the citation between (point) and (mark t),
259 and should leave point and mark around the citation text as modified.
260 The hook functions can find the header of the cited message
261 in the variable `mail-citation-header', whether or not this is included
262 in the cited portion of the message.
263
264 If this hook is entirely empty (nil), a default action is taken
265 instead of no action."
266 :type 'hook
267 :group 'sendmail)
268
269 (defvar mail-citation-header nil
270 "While running `mail-citation-hook', this variable holds the message header.
271 This enables the hook functions to see the whole message header
272 regardless of what part of it (if any) is included in the cited text.")
273
274 ;;;###autoload
275 (defcustom mail-citation-prefix-regexp
276 (purecopy "\\([ \t]*\\(\\w\\|[_.]\\)+>+\\|[ \t]*[]>|]\\)+")
277 "Regular expression to match a citation prefix plus whitespace.
278 It should match whatever sort of citation prefixes you want to handle,
279 with whitespace before and after; it should also match just whitespace.
280 The default value matches citations like `foo-bar>' plus whitespace."
281 :type 'regexp
282 :group 'sendmail
283 :version "24.1")
284
285 (defvar mail-abbrevs-loaded nil)
286 (defvar mail-mode-map
287 (let ((map (make-sparse-keymap)))
288 (define-key map "\M-\t" 'completion-at-point)
289 (define-key map "\C-c?" 'describe-mode)
290 (define-key map "\C-c\C-f\C-t" 'mail-to)
291 (define-key map "\C-c\C-f\C-b" 'mail-bcc)
292 (define-key map "\C-c\C-f\C-f" 'mail-fcc)
293 (define-key map "\C-c\C-f\C-c" 'mail-cc)
294 (define-key map "\C-c\C-f\C-s" 'mail-subject)
295 (define-key map "\C-c\C-f\C-r" 'mail-reply-to)
296 (define-key map "\C-c\C-f\C-a" 'mail-mail-reply-to) ; author
297 (define-key map "\C-c\C-f\C-l" 'mail-mail-followup-to) ; list
298 (define-key map "\C-c\C-t" 'mail-text)
299 (define-key map "\C-c\C-y" 'mail-yank-original)
300 (define-key map "\C-c\C-r" 'mail-yank-region)
301 (define-key map [remap split-line] 'mail-split-line)
302 (define-key map "\C-c\C-q" 'mail-fill-yanked-message)
303 (define-key map "\C-c\C-w" 'mail-signature)
304 (define-key map "\C-c\C-c" 'mail-send-and-exit)
305 (define-key map "\C-c\C-s" 'mail-send)
306 (define-key map "\C-c\C-i" 'mail-insert-file)
307 ;; FIXME add this? "b" = bury buffer. It's in the menu-bar.
308 ;;; (define-key map "\C-c\C-b" 'mail-dont-send)
309
310 (define-key map [menu-bar mail]
311 (cons "Mail" (make-sparse-keymap "Mail")))
312
313 (define-key map [menu-bar mail attachment]
314 '("Attach File" . mail-add-attachment))
315
316 (define-key map [menu-bar mail fill]
317 '("Fill Citation" . mail-fill-yanked-message))
318
319 (define-key map [menu-bar mail yank]
320 '(menu-item "Cite Original" mail-yank-original :enable mail-reply-action))
321
322 (define-key map [menu-bar mail signature]
323 '("Insert Signature" . mail-signature))
324
325 (define-key map [menu-bar mail mail-sep]
326 '("--"))
327
328 (define-key map [menu-bar mail cancel]
329 '("Cancel" . mail-dont-send))
330
331 (define-key map [menu-bar mail send-stay]
332 '("Send, Keep Editing" . mail-send))
333
334 (define-key map [menu-bar mail send]
335 '("Send Message" . mail-send-and-exit))
336
337 (define-key map [menu-bar headers]
338 (cons "Headers" (make-sparse-keymap "Move to Header")))
339
340 (define-key map [menu-bar headers text]
341 '("Text" . mail-text))
342
343 (define-key map [menu-bar headers expand-aliases]
344 '("Expand Aliases" . expand-mail-aliases))
345
346 (define-key map [menu-bar headers mail-reply-to]
347 '("Mail-Reply-To" . mail-mail-reply-to))
348
349 (define-key map [menu-bar headers mail-followup-to]
350 '("Mail-Followup-To" . mail-mail-followup-to))
351
352 (define-key map [menu-bar headers reply-to]
353 '("Reply-To" . mail-reply-to))
354
355 (define-key map [menu-bar headers bcc]
356 '("Bcc" . mail-bcc))
357
358 (define-key map [menu-bar headers fcc]
359 '("Fcc" . mail-fcc))
360
361 (define-key map [menu-bar headers cc]
362 '("Cc" . mail-cc))
363
364 (define-key map [menu-bar headers subject]
365 '("Subject" . mail-subject))
366
367 (define-key map [menu-bar headers to]
368 '("To" . mail-to))
369
370 map))
371
372 (autoload 'build-mail-aliases "mailalias"
373 "Read mail aliases from personal aliases file and set `mail-aliases'.
374 By default, this is the file specified by `mail-personal-alias-file'." t)
375
376 ;;;###autoload
377 (defcustom mail-signature t
378 "Text inserted at end of mail buffer when a message is initialized.
379 If t, it means to insert the contents of the file `mail-signature-file'.
380 If a string, that string is inserted.
381 (To make a proper signature, the string should begin with \\n\\n-- \\n,
382 which is the standard way to delimit a signature in a message.)
383 Otherwise, it should be an expression; it is evaluated
384 and should insert whatever you want to insert."
385 :type '(choice (const :tag "None" nil)
386 (const :tag "Use `.signature' file" t)
387 (string :tag "String to insert")
388 (sexp :tag "Expression to evaluate"))
389 :group 'sendmail)
390 (put 'mail-signature 'risky-local-variable t)
391
392 ;;;###autoload
393 (defcustom mail-signature-file (purecopy "~/.signature")
394 "File containing the text inserted at end of mail buffer."
395 :type 'file
396 :group 'sendmail)
397
398 ;;;###autoload
399 (defcustom mail-default-directory (purecopy "~/")
400 "Value of `default-directory' for Mail mode buffers.
401 This directory is used for auto-save files of Mail mode buffers.
402
403 Note that Message mode does not use this variable; it auto-saves
404 in `message-auto-save-directory'."
405 :type '(directory :tag "Directory")
406 :group 'sendmail
407 :version "22.1")
408
409 (defvar mail-reply-action nil)
410 (defvar mail-send-actions nil
411 "A list of actions to be performed upon successful sending of a message.")
412 (defvar mail-return-action nil)
413
414 ;;;###autoload
415 (defcustom mail-default-headers nil
416 "A string containing header lines, to be inserted in outgoing messages.
417 It can contain newlines, and should end in one. It is inserted
418 before you edit the message, so you can edit or delete the lines."
419 :type '(choice (const nil) string)
420 :group 'sendmail)
421
422 (defcustom mail-bury-selects-summary t
423 "If non-nil, try to show Rmail summary buffer after returning from mail.
424 The functions \\[mail-send-on-exit] or \\[mail-dont-send] select
425 the Rmail summary buffer before returning, if it exists and this variable
426 is non-nil."
427 :type 'boolean
428 :group 'sendmail)
429
430 (defcustom mail-send-nonascii 'mime
431 "Specify whether to allow sending non-ASCII characters in mail.
432 If t, that means do allow it. nil means don't allow it.
433 `query' means ask the user each time.
434 `mime' means add an appropriate MIME header if none already present.
435 The default is `mime'.
436 Including non-ASCII characters in a mail message can be problematical
437 for the recipient, who may not know how to decode them properly."
438 :type '(choice (const t) (const nil) (const query) (const mime))
439 :group 'sendmail)
440
441 (defcustom mail-use-dsn nil
442 "Ask MTA for notification of failed, delayed or successful delivery.
443 Note that only some MTAs (currently only recent versions of Sendmail)
444 support Delivery Status Notification."
445 :group 'sendmail
446 :type '(repeat (radio (const :tag "Failure" failure)
447 (const :tag "Delay" delay)
448 (const :tag "Success" success)))
449 :version "22.1")
450
451 ;; Note: could use /usr/ucb/mail instead of sendmail;
452 ;; options -t, and -v if not interactive.
453 (defvar mail-mailer-swallows-blank-line nil
454 "Set this non-nil if the system's mailer runs the header and body together.
455 The actual value should be an expression to evaluate that returns
456 non-nil if the problem will actually occur.
457 \(As far as we know, this is not an issue on any system still supported
458 by Emacs.)")
459
460 (put 'mail-mailer-swallows-blank-line 'risky-local-variable t) ; gets evalled
461 (make-obsolete-variable 'mail-mailer-swallows-blank-line
462 "no need to set this on any modern system."
463 "24.1" 'set)
464
465 (defvar mail-mode-syntax-table
466 ;; define-derived-mode will make it inherit from text-mode-syntax-table.
467 (let ((st (make-syntax-table)))
468 ;; FIXME this is probably very obsolete now ("percent hack").
469 ;; sending.texi used to say:
470 ;; Mail mode defines the character `%' as a word separator; this
471 ;; is helpful for using the word commands to edit mail addresses.
472 (modify-syntax-entry ?% ". " st)
473 st)
474 "Syntax table used while in `mail-mode'.")
475
476 (defvar mail-font-lock-keywords
477 (eval-when-compile
478 (let* ((cite-chars "[>|}]")
479 (cite-prefix "[:alpha:]")
480 (cite-suffix (concat cite-prefix "0-9_.@-`'\"")))
481 (list '("^\\(To\\|Newsgroups\\):" . font-lock-function-name-face)
482 '("^\\(B?CC\\|Reply-to\\|Mail-\\(reply\\|followup\\)-to\\):" . font-lock-keyword-face)
483 '("^\\(Subject:\\)[ \t]*\\(.+\\)?"
484 (1 font-lock-comment-face)
485 ;; (2 font-lock-type-face nil t)
486 )
487 ;; Use EVAL to delay in case `mail-header-separator' gets changed.
488 '(eval .
489 (let ((separator (if (zerop (length mail-header-separator))
490 " \\`\\' "
491 (regexp-quote mail-header-separator))))
492 (cons (concat "^" separator "$") 'font-lock-warning-face)))
493 ;; Use MATCH-ANCHORED to effectively anchor the regexp left side.
494 `(,cite-chars
495 (,(concat "\\=[ \t]*"
496 "\\(\\(\\([" cite-prefix "]+[" cite-suffix "]*\\)?"
497 "\\(" cite-chars "[ \t]*\\)\\)+\\)"
498 "\\(.*\\)")
499 (beginning-of-line) (end-of-line)
500 (1 font-lock-comment-delimiter-face nil t)
501 (5 font-lock-comment-face nil t)))
502 '("^\\(X-[A-Za-z0-9-]+\\|In-reply-to\\):.*\\(\n[ \t]+.*\\)*$"
503 . font-lock-string-face))))
504 "Additional expressions to highlight in Mail mode.")
505
506 \f
507 ;;;###autoload
508 (defun sendmail-query-once ()
509 "Query for `send-mail-function' and send mail with it.
510 This also saves the value of `send-mail-function' via Customize."
511 ;; If send-mail-function is already setup, we're incorrectly called
512 ;; a second time, probably because someone's using an old value
513 ;; of send-mail-function.
514 (when (eq send-mail-function 'sendmail-query-once)
515 (sendmail-query-user-about-smtp))
516 (funcall send-mail-function))
517
518 (defun sendmail-query-user-about-smtp ()
519 (let* ((options `(("mail client" . mailclient-send-it)
520 ,@(when (and sendmail-program
521 (executable-find sendmail-program))
522 '(("transport" . sendmail-send-it)))
523 ("smtp" . smtpmail-send-it)))
524 (choice
525 ;; Query the user.
526 (with-temp-buffer
527 (rename-buffer "*Emacs Mail Setup Help*" t)
528 (insert (substitute-command-keys "\
529 Emacs is about to send an email message, but it has not been
530 configured for sending email. To tell Emacs how to send email:
531
532 - Type `")
533 (propertize "mail client" 'face 'bold)
534 (substitute-command-keys "\
535 ' to start your default email client and
536 pass it the message text.\n\n"))
537 (and sendmail-program
538 (executable-find sendmail-program)
539 (insert (substitute-command-keys "\
540 - Type `")
541 (propertize "transport" 'face 'bold)
542 (substitute-command-keys "\
543 ' to invoke the system's mail transport agent
544 (the `")
545 sendmail-program
546 (substitute-command-keys "' program).\n\n")))
547 (insert (substitute-command-keys "\
548 - Type `")
549 (propertize "smtp" 'face 'bold)
550 (substitute-command-keys "' to send mail directly to an \"outgoing mail\" server.
551 (Emacs may prompt you for SMTP settings).
552
553 Emacs will record your selection and will use it thereafter.
554 To change it later, customize the option `send-mail-function'.\n"))
555 (goto-char (point-min))
556 (display-buffer (current-buffer))
557 (let ((completion-ignore-case t))
558 (completing-read "Send mail via: "
559 options nil 'require-match)))))
560 (customize-save-variable 'send-mail-function
561 (cdr (assoc-string choice options t)))))
562 \f
563 (defun sendmail-sync-aliases ()
564 (when mail-personal-alias-file
565 (let ((modtime (nth 5 (file-attributes mail-personal-alias-file))))
566 (or (equal mail-alias-modtime modtime)
567 (setq mail-alias-modtime modtime
568 mail-aliases t)))))
569
570 \f
571 ;;;###autoload
572 (define-mail-user-agent 'sendmail-user-agent
573 'sendmail-user-agent-compose
574 'mail-send-and-exit)
575
576 ;;;###autoload
577 (defun sendmail-user-agent-compose (&optional to subject other-headers
578 continue switch-function yank-action
579 send-actions return-action
580 &rest ignored)
581 (if switch-function
582 (funcall switch-function "*mail*"))
583 (let ((cc (cdr (assoc-string "cc" other-headers t)))
584 (in-reply-to (cdr (assoc-string "in-reply-to" other-headers t)))
585 (body (cdr (assoc-string "body" other-headers t))))
586 (or (mail continue to subject in-reply-to cc yank-action
587 send-actions return-action)
588 continue
589 (error "Message aborted"))
590 (save-excursion
591 (rfc822-goto-eoh)
592 (while other-headers
593 (unless (member-ignore-case (car (car other-headers))
594 '("in-reply-to" "cc" "body"))
595 (insert (car (car other-headers)) ": "
596 (cdr (car other-headers))
597 (if use-hard-newlines hard-newline "\n")))
598 (setq other-headers (cdr other-headers)))
599 (when body
600 (forward-line 1)
601 (insert body))
602 t)))
603
604 (defun mail-setup (to subject in-reply-to cc replybuffer
605 actions return-action)
606 (or mail-default-reply-to
607 (setq mail-default-reply-to (getenv "REPLYTO")))
608 (sendmail-sync-aliases)
609 (when (eq mail-aliases t)
610 (setq mail-aliases nil)
611 (and mail-personal-alias-file
612 (file-exists-p mail-personal-alias-file)
613 (build-mail-aliases)))
614 ;; Don't leave this around from a previous message.
615 (kill-local-variable 'buffer-file-coding-system)
616 ;; This doesn't work for enable-multibyte-characters.
617 ;; (kill-local-variable 'enable-multibyte-characters)
618 (set-buffer-multibyte (default-value 'enable-multibyte-characters))
619 (if current-input-method
620 (deactivate-input-method))
621
622 ;; Local variables for Mail mode.
623 (setq mail-send-actions actions)
624 (setq mail-reply-action replybuffer)
625 (setq mail-return-action return-action)
626
627 (goto-char (point-min))
628 (if mail-setup-with-from
629 (mail-insert-from-field))
630 (insert "To: ")
631 (save-excursion
632 (if to
633 ;; Here removed code to extract names from within <...>
634 ;; on the assumption that mail-strip-quoted-names
635 ;; has been called and has done so.
636 (let ((fill-prefix "\t")
637 (address-start (point)))
638 (insert to "\n")
639 (fill-region-as-paragraph address-start (point-max))
640 (goto-char (point-max))
641 (unless (bolp)
642 (newline)))
643 (newline))
644 (if cc
645 (let ((fill-prefix "\t")
646 (address-start (progn (insert "CC: ") (point))))
647 (insert cc "\n")
648 (fill-region-as-paragraph address-start (point-max))
649 (goto-char (point-max))
650 (unless (bolp)
651 (newline))))
652 (if in-reply-to
653 (let ((fill-prefix "\t")
654 (fill-column 78)
655 (address-start (point)))
656 (insert "In-reply-to: " in-reply-to "\n")
657 (fill-region-as-paragraph address-start (point-max))
658 (goto-char (point-max))
659 (unless (bolp)
660 (newline))))
661 (insert "Subject: " (or subject "") "\n")
662 (if mail-default-headers
663 (insert mail-default-headers))
664 (if mail-default-reply-to
665 (insert "Reply-to: " mail-default-reply-to "\n"))
666 (if mail-self-blind
667 (insert "BCC: " user-mail-address "\n"))
668 (if mail-archive-file-name
669 (insert "FCC: " mail-archive-file-name "\n"))
670 (put-text-property (point)
671 (progn
672 (insert mail-header-separator "\n")
673 (1- (point)))
674 'category 'mail-header-separator)
675 ;; Insert the signature. But remember the beginning of the message.
676 (if to (setq to (point)))
677 (if mail-signature (mail-signature t))
678 (goto-char (point-max))
679 (or (bolp) (newline)))
680 (if to (goto-char to))
681 (or to subject in-reply-to
682 (set-buffer-modified-p nil))
683 (run-hooks 'mail-setup-hook))
684 \f
685 (defcustom mail-mode-hook nil
686 "Hook run by Mail mode.
687 When composing a mail, this runs immediately after creating, or
688 switching to, the `*mail*' buffer. See also `mail-setup-hook'."
689 :group 'sendmail
690 :type 'hook
691 :options '(footnote-mode))
692
693 (defvar mail-mode-abbrev-table text-mode-abbrev-table)
694 (defvar mail-encode-mml)
695 ;;;###autoload
696 (define-derived-mode mail-mode text-mode "Mail"
697 "Major mode for editing mail to be sent.
698 Like Text Mode but with these additional commands:
699
700 \\[mail-send] mail-send (send the message)
701 \\[mail-send-and-exit] mail-send-and-exit (send the message and exit)
702
703 Here are commands that move to a header field (and create it if there isn't):
704 \\[mail-to] move to To: \\[mail-subject] move to Subj:
705 \\[mail-bcc] move to BCC: \\[mail-cc] move to CC:
706 \\[mail-fcc] move to FCC: \\[mail-reply-to] move to Reply-To:
707 \\[mail-mail-reply-to] move to Mail-Reply-To:
708 \\[mail-mail-followup-to] move to Mail-Followup-To:
709 \\[mail-text] move to message text.
710 \\[mail-signature] mail-signature (insert `mail-signature-file' file).
711 \\[mail-yank-original] mail-yank-original (insert current message, in Rmail).
712 \\[mail-fill-yanked-message] mail-fill-yanked-message (fill what was yanked).
713 \\[mail-insert-file] insert a text file into the message.
714 \\[mail-add-attachment] attach to the message a file as binary attachment.
715 Turning on Mail mode runs the normal hooks `text-mode-hook' and
716 `mail-mode-hook' (in that order)."
717 (make-local-variable 'mail-reply-action)
718 (make-local-variable 'mail-send-actions)
719 (make-local-variable 'mail-return-action)
720 (make-local-variable 'mail-encode-mml)
721 (setq mail-encode-mml nil)
722 (setq buffer-offer-save t)
723 (make-local-variable 'font-lock-defaults)
724 (setq font-lock-defaults '(mail-font-lock-keywords t t))
725 (make-local-variable 'paragraph-separate)
726 (make-local-variable 'normal-auto-fill-function)
727 (setq normal-auto-fill-function 'mail-mode-auto-fill)
728 (make-local-variable 'fill-paragraph-function)
729 (setq fill-paragraph-function 'mail-mode-fill-paragraph)
730 ;; Allow using comment commands to add/remove quoting (this only does
731 ;; anything if mail-yank-prefix is set to a non-nil value).
732 (set (make-local-variable 'comment-start) mail-yank-prefix)
733 (if mail-yank-prefix
734 (set (make-local-variable 'comment-start-skip)
735 (concat "^" (regexp-quote mail-yank-prefix) "[ \t]*")))
736 (make-local-variable 'adaptive-fill-regexp)
737 ;; Also update the paragraph-separate entry if you change this.
738 (setq adaptive-fill-regexp
739 (concat "[ \t]*[-[:alnum:]]+>+[ \t]*\\|"
740 adaptive-fill-regexp))
741 (make-local-variable 'adaptive-fill-first-line-regexp)
742 (setq adaptive-fill-first-line-regexp
743 (concat "[ \t]*[-[:alnum:]]*>+[ \t]*\\|"
744 adaptive-fill-first-line-regexp))
745 (add-hook 'completion-at-point-functions #'mail-completion-at-point-function
746 nil 'local)
747 ;; `-- ' precedes the signature. `-----' appears at the start of the
748 ;; lines that delimit forwarded messages.
749 ;; Lines containing just >= 3 dashes, perhaps after whitespace,
750 ;; are also sometimes used and should be separators.
751 (setq paragraph-separate
752 (concat (regexp-quote mail-header-separator)
753 ;; This is based on adaptive-fill-regexp (presumably
754 ;; the idea is to allow navigation etc of cited paragraphs).
755 "$\\|\t*[-–!|#%;>*·•‣⁃◦ ]+$"
756 "\\|[ \t]*[-[:alnum:]]*>+[ \t]*$\\|[ \t]*$\\|"
757 "--\\( \\|-+\\)$\\|"
758 page-delimiter)))
759
760
761 (defun mail-header-end ()
762 "Return the buffer location of the end of headers, as a number."
763 (save-restriction
764 (widen)
765 (save-excursion
766 (rfc822-goto-eoh)
767 (point))))
768
769 (defun mail-text-start ()
770 "Return the buffer location of the start of text, as a number."
771 (save-restriction
772 (widen)
773 (save-excursion
774 (rfc822-goto-eoh)
775 (forward-line 1)
776 (point))))
777
778 (defun mail-sendmail-delimit-header ()
779 "Set up whatever header delimiter convention sendmail will use.
780 Concretely: replace the first blank line in the header with the separator."
781 (rfc822-goto-eoh)
782 (insert mail-header-separator)
783 (point))
784
785 (defun mail-sendmail-undelimit-header ()
786 "Remove header separator to put the message in correct form for sendmail.
787 Leave point at the start of the delimiter line."
788 (rfc822-goto-eoh)
789 (delete-region (point) (progn (end-of-line) (point))))
790
791 (defun mail-mode-auto-fill ()
792 "Carry out Auto Fill for Mail mode.
793 If within the headers, this makes the new lines into continuation lines."
794 (if (< (point) (mail-header-end))
795 (let ((old-line-start (line-beginning-position)))
796 (if (do-auto-fill)
797 (save-excursion
798 (beginning-of-line)
799 (while (not (eq (point) old-line-start))
800 ;; Use insert-before-markers in case we're inserting
801 ;; before the saved value of point (which is common).
802 (insert-before-markers " ")
803 (forward-line -1))
804 t)))
805 (do-auto-fill)))
806
807 (defun mail-mode-fill-paragraph (arg)
808 ;; Do something special only if within the headers.
809 (if (< (point) (mail-header-end))
810 (let (beg end fieldname)
811 (when (prog1 (re-search-backward "^[-a-zA-Z]+:" nil 'yes)
812 (setq beg (point)))
813 (setq fieldname
814 (downcase (buffer-substring beg (1- (match-end 0))))))
815 (forward-line 1)
816 ;; Find continuation lines and get rid of their continuation markers.
817 (while (looking-at "[ \t]")
818 (delete-horizontal-space)
819 (forward-line 1))
820 (setq end (point-marker))
821 (goto-char beg)
822 ;; If this field contains addresses,
823 ;; make sure we can fill after each address.
824 (if (member fieldname
825 '("to" "cc" "bcc" "from" "reply-to"
826 "mail-reply-to" "mail-followup-to"
827 "resent-to" "resent-cc" "resent-bcc"
828 "resent-from" "resent-reply-to"))
829 (while (search-forward "," end t)
830 (or (looking-at "[ \t]")
831 (insert " "))))
832 (fill-region-as-paragraph beg end arg)
833 ;; Mark all lines except the first as continuations.
834 (goto-char beg)
835 (forward-line 1)
836 (while (< (point) end)
837 (insert " ")
838 (forward-line 1))
839 (move-marker end nil)
840 t)))
841 \f
842 ;; User-level commands for sending.
843
844 (defun mail-send-and-exit (&optional arg)
845 "Send message like `mail-send', then, if no errors, exit from mail buffer.
846 Prefix arg means don't delete this window."
847 (interactive "P")
848 (mail-send)
849 (mail-bury arg))
850
851 (defun mail-dont-send (&optional arg)
852 "Don't send the message you have been editing.
853 Prefix arg means don't delete this window."
854 (interactive "P")
855 (mail-bury arg))
856
857 (defun mail-bury (&optional arg)
858 "Bury this mail buffer."
859 (let ((newbuf (other-buffer (current-buffer)))
860 (return-action mail-return-action))
861 (bury-buffer (current-buffer))
862 ;; If there is an Rmail buffer, return to it nicely
863 ;; even if this message was not started by an Rmail command.
864 (unless return-action
865 (dolist (buffer (buffer-list))
866 (if (and (eq (buffer-local-value 'major-mode buffer) 'rmail-mode)
867 (null return-action)
868 ;; Don't match message-viewer buffer.
869 (not (string-match "\\` " (buffer-name buffer))))
870 (setq return-action `(rmail-mail-return ,buffer)))))
871 (if (and (null arg) return-action)
872 (apply (car return-action) (cdr return-action))
873 (switch-to-buffer newbuf))))
874
875 (defcustom mail-send-hook nil
876 "Hook run just before sending a message."
877 :type 'hook
878 :options '(flyspell-mode-off)
879 :group 'sendmail)
880
881 ;;;###autoload
882 (defcustom mail-mailing-lists nil
883 "List of mailing list addresses the user is subscribed to.
884 The variable is used to trigger insertion of the \"Mail-Followup-To\"
885 header when sending a message to a mailing list."
886 :type '(repeat string)
887 :group 'sendmail)
888
889 (declare-function mml-to-mime "mml" ())
890
891 (defun mail-send ()
892 "Send the message in the current buffer.
893 If `mail-interactive' is non-nil, wait for success indication
894 or error messages, and inform user.
895 Otherwise any failure is reported in a message back to
896 the user from the mailer."
897 (interactive)
898 (if (if buffer-file-name
899 (y-or-n-p "Send buffer contents as mail message? ")
900 (or (buffer-modified-p)
901 (y-or-n-p "Message already sent; resend? ")))
902 (let ((inhibit-read-only t)
903 (opoint (point))
904 (ml (when mail-mailing-lists
905 ;; The surrounding regexp assumes the use of
906 ;; `mail-strip-quoted-names' on addresses before matching
907 ;; Cannot deal with full RFC 822 freedom, but that is
908 ;; unlikely to be problematic.
909 (concat "\\(?:[[:space:];,]\\|\\`\\)"
910 (regexp-opt mail-mailing-lists t)
911 "\\(?:[[:space:];,]\\|\\'\\)"))))
912 (mail-combine-fields "To")
913 (mail-combine-fields "CC")
914 ;; If there are mailing lists defined
915 (when ml
916 (save-excursion
917 (let* ((to (mail-fetch-field "to" nil t))
918 (cc (mail-fetch-field "cc" nil t))
919 (new-header-values ; To: and Cc:
920 (mail-strip-quoted-names
921 (concat to (when cc (concat ", " cc))))))
922 ;; If message goes to known mailing list ...
923 (when (string-match ml new-header-values)
924 ;; Add Mail-Followup-To if none yet
925 (unless (mail-fetch-field "mail-followup-to")
926 (goto-char (mail-header-end))
927 (insert "Mail-Followup-To: "
928 (let ((l))
929 (mapc
930 ;; remove duplicates
931 (lambda (e)
932 (unless (member e l)
933 (push e l)))
934 (split-string new-header-values
935 ",[[:space:]]+" t))
936 (mapconcat 'identity l ", "))
937 "\n"))
938 ;; Add Mail-Reply-To if none yet
939 (unless (mail-fetch-field "mail-reply-to")
940 (goto-char (mail-header-end))
941 (insert "Mail-Reply-To: "
942 (or (mail-fetch-field "reply-to")
943 user-mail-address)
944 "\n"))))))
945 (unless (memq mail-send-nonascii '(t mime))
946 (goto-char (point-min))
947 (skip-chars-forward "\0-\177")
948 (or (= (point) (point-max))
949 (if (eq mail-send-nonascii 'query)
950 (or (y-or-n-p "Message contains non-ASCII characters; send anyway? ")
951 (error "Aborted"))
952 (error "Message contains non-ASCII characters"))))
953 ;; Complain about any invalid line.
954 (goto-char (point-min))
955 (re-search-forward (regexp-quote mail-header-separator) (point-max) t)
956 (let ((header-end (or (match-beginning 0) (point-max))))
957 (goto-char (point-min))
958 (while (< (point) header-end)
959 (unless (looking-at "[ \t]\\|.*:\\|$")
960 (push-mark opoint)
961 (error "Invalid header line (maybe a continuation line lacks initial whitespace)"))
962 (forward-line 1)))
963 (goto-char opoint)
964 (when mail-encode-mml
965 (mml-to-mime)
966 (setq mail-encode-mml nil))
967 (run-hooks 'mail-send-hook)
968 (message "Sending...")
969 (funcall send-mail-function)
970 ;; Now perform actions on successful sending.
971 (while mail-send-actions
972 (condition-case nil
973 (apply (car (car mail-send-actions))
974 (cdr (car mail-send-actions)))
975 (error))
976 (setq mail-send-actions (cdr mail-send-actions)))
977 (message "Sending...done")
978 ;; If buffer has no file, mark it as unmodified and delete auto-save.
979 (if (not buffer-file-name)
980 (progn
981 (set-buffer-modified-p nil)
982 (delete-auto-save-file-if-necessary t))))))
983
984 (defun mail-envelope-from ()
985 "Return the envelope mail address to use when sending mail.
986 This function uses `mail-envelope-from'."
987 (if (eq mail-envelope-from 'header)
988 (nth 1 (mail-extract-address-components
989 (mail-fetch-field "From")))
990 mail-envelope-from))
991 \f
992 ;; This does the real work of sending a message via sendmail.
993 ;; It is called via the variable send-mail-function.
994
995 ;;;###autoload
996 (defvar sendmail-coding-system nil
997 "Coding system for encoding the outgoing mail.
998 This has higher priority than the default `buffer-file-coding-system'
999 and `default-sendmail-coding-system',
1000 but lower priority than the local value of `buffer-file-coding-system'.
1001 See also the function `select-message-coding-system'.")
1002
1003 ;;;###autoload
1004 (defvar default-sendmail-coding-system 'iso-latin-1
1005 "Default coding system for encoding the outgoing mail.
1006 This variable is used only when `sendmail-coding-system' is nil.
1007
1008 This variable is set/changed by the command `set-language-environment'.
1009 User should not set this variable manually,
1010 instead use `sendmail-coding-system' to get a constant encoding
1011 of outgoing mails regardless of the current language environment.
1012 See also the function `select-message-coding-system'.")
1013
1014 (defun mail-insert-from-field ()
1015 "Insert the \"From:\" field of a mail header.
1016 The style of the field is determined by the variable `mail-from-style'.
1017 This function does not perform RFC2047 encoding."
1018 (let* ((login user-mail-address)
1019 (fullname (user-full-name))
1020 (quote-fullname nil))
1021 (if (string-match "[^\0-\177]" fullname)
1022 (setq quote-fullname t))
1023 (cond ((null mail-from-style)
1024 (insert "From: " login "\n"))
1025 ;; This is deprecated.
1026 ((eq mail-from-style 'system-default)
1027 nil)
1028 ((or (eq mail-from-style 'angles)
1029 (and (not (eq mail-from-style 'parens))
1030 ;; Use angles if no quoting is needed, or if
1031 ;; parens would need quoting too.
1032 (or (not (string-match "[^- !#-'*+/-9=?A-Z^-~]" fullname))
1033 (let ((tmp (concat fullname nil)))
1034 (while (string-match "([^()]*)" tmp)
1035 (aset tmp (match-beginning 0) ?-)
1036 (aset tmp (1- (match-end 0)) ?-))
1037 (string-match "[\\()]" tmp)))))
1038 (insert "From: " fullname)
1039 (let ((fullname-start (+ (point-min) 6))
1040 (fullname-end (point-marker)))
1041 (goto-char fullname-start)
1042 ;; Look for a character that cannot appear unquoted
1043 ;; according to RFC 822.
1044 (if (or (re-search-forward "[^- !#-'*+/-9=?A-Z^-~]"
1045 fullname-end 1)
1046 quote-fullname)
1047 (progn
1048 ;; Quote fullname, escaping specials.
1049 (goto-char fullname-start)
1050 (insert "\"")
1051 (while (re-search-forward "[\"\\]"
1052 fullname-end 1)
1053 (replace-match "\\\\\\&" t))
1054 (insert "\""))))
1055 (insert " <" login ">\n"))
1056 ;; 'parens or default
1057 (t
1058 (insert "From: " login " (")
1059 (let ((fullname-start (point)))
1060 (if quote-fullname
1061 (insert "\""))
1062 (insert fullname)
1063 (if quote-fullname
1064 (insert "\""))
1065 (let ((fullname-end (point-marker)))
1066 (goto-char fullname-start)
1067 ;; RFC 822 says \ and nonmatching parentheses
1068 ;; must be escaped in comments.
1069 ;; Escape every instance of ()\ ...
1070 (while (re-search-forward "[()\\]" fullname-end 1)
1071 (replace-match "\\\\\\&" t))
1072 ;; ... then undo escaping of matching parentheses,
1073 ;; including matching nested parentheses.
1074 (goto-char fullname-start)
1075 (while (re-search-forward
1076 "\\(\\=\\|[^\\]\\(\\\\\\\\\\)*\\)\\\\(\\(\\([^\\]\\|\\\\\\\\\\)*\\)\\\\)"
1077 fullname-end 1)
1078 (replace-match "\\1(\\3)" t)
1079 (goto-char fullname-start))))
1080 (insert ")\n")))))
1081
1082 (defun mail-combine-fields (field)
1083 "Offer to combine all FIELD fields in buffer into one FIELD field.
1084 If this finds multiple FIELD fields, it asks the user whether
1085 to combine them into one, and does so if the user says y."
1086 (let ((search-pattern (format "^%s[ \t]*:" field))
1087 first-to-end
1088 query-asked
1089 query-answer
1090 (old-point (point))
1091 (old-max (point-max)))
1092 (save-excursion
1093 (save-restriction
1094 (goto-char (point-min))
1095 (narrow-to-region (point-min) (mail-header-end))
1096 ;; Find the first FIELD field and record where it ends.
1097 (when (re-search-forward search-pattern nil t)
1098 (forward-line 1)
1099 (re-search-forward "^[^ \t]" nil t)
1100 (beginning-of-line)
1101 (setq first-to-end (point-marker))
1102 (set-marker-insertion-type first-to-end t)
1103 ;; Find each following FIELD field
1104 ;; and combine it with the first FIELD field.
1105 (while (re-search-forward search-pattern nil t)
1106 ;; For the second FIELD field, ask user to
1107 ;; approve combining them.
1108 ;; But if the user refuse to combine them, signal error.
1109 (unless query-asked
1110 (save-restriction
1111 ;; This is just so the screen doesn't change.
1112 (narrow-to-region (point-min) old-max)
1113 (goto-char old-point)
1114 (setq query-asked t)
1115 (if (y-or-n-p (format "Message contains multiple %s fields. Combine? " field))
1116 (setq query-answer t))))
1117 (when query-answer
1118 (let ((this-to-start (line-beginning-position))
1119 this-to-end
1120 this-to)
1121 (forward-line 1)
1122 (re-search-forward "^[^ \t]" nil t)
1123 (beginning-of-line)
1124 (setq this-to-end (point))
1125 ;; Get the text of this FIELD field.
1126 (setq this-to (buffer-substring this-to-start this-to-end))
1127 ;; Delete it.
1128 (delete-region this-to-start this-to-end)
1129 (save-excursion
1130 ;; Put a comma after the first FIELD field.
1131 (goto-char first-to-end)
1132 (forward-char -1)
1133 (insert ",")
1134 ;; Copy this one after it.
1135 (goto-char first-to-end)
1136 (save-excursion
1137 (insert this-to))
1138 ;; Replace the FIELD: with spaces.
1139 (looking-at search-pattern)
1140 ;; Try to preserve alignment of contents of the field
1141 (let ((prefix-length (length (match-string 0))))
1142 (replace-match " ")
1143 (dotimes (i (1- prefix-length))
1144 (insert " ")))))))
1145 (set-marker first-to-end nil))))))
1146
1147 (defun mail-encode-header (beg end)
1148 "Encode the mail header between BEG and END according to RFC2047.
1149 Return non-nil if and only if some part of the header is encoded."
1150 (save-restriction
1151 (narrow-to-region beg end)
1152 (let* ((selected (select-message-coding-system))
1153 (mm-coding-system-priorities
1154 (if (and selected (coding-system-get selected :mime-charset))
1155 (cons selected mm-coding-system-priorities)
1156 mm-coding-system-priorities))
1157 (tick (buffer-chars-modified-tick))
1158 ;; Many mailers, including Gnus, passes a message of which
1159 ;; the header is already encoded, so this is necessary to
1160 ;; prevent it from being encoded again.
1161 (rfc2047-encode-encoded-words nil))
1162 (rfc2047-encode-message-header)
1163 (= tick (buffer-chars-modified-tick)))))
1164
1165 ;; Normally you will not need to modify these options unless you are
1166 ;; using some non-genuine substitute for sendmail which does not
1167 ;; implement each and every option that the original supports.
1168 ;; E.g., ssmtp does not support "-odb", so, if your site uses it,
1169 ;; you will need to modify `sendmail-error-reporting-non-interactive'
1170 ;; in your site-init.el.
1171 (defvar sendmail-error-reporting-interactive
1172 ;; These mean "report errors to terminal" and "deliver interactively"
1173 '("-oep" "-odi"))
1174 (defvar sendmail-error-reporting-non-interactive
1175 ;; These mean "report errors by mail" and "deliver in background".
1176 '("-oem" "-odb"))
1177
1178 (defun sendmail-send-it ()
1179 "Send the current mail buffer using the Sendmail package.
1180 This is a suitable value for `send-mail-function'. It sends using the
1181 external program defined by `sendmail-program'."
1182 (require 'mail-utils)
1183 (let ((errbuf (if mail-interactive
1184 (generate-new-buffer " sendmail errors")
1185 0))
1186 (error nil)
1187 (tembuf (generate-new-buffer " sendmail temp"))
1188 (multibyte enable-multibyte-characters)
1189 (case-fold-search nil)
1190 (selected-coding (select-message-coding-system))
1191 resend-to-addresses
1192 delimline
1193 fcc-was-found
1194 (mailbuf (current-buffer))
1195 ;; Examine these variables now, so that
1196 ;; local binding in the mail buffer will take effect.
1197 (envelope-from
1198 (and mail-specify-envelope-from
1199 (or (mail-envelope-from) user-mail-address))))
1200 (unwind-protect
1201 (with-current-buffer tembuf
1202 (erase-buffer)
1203 (unless multibyte
1204 (set-buffer-multibyte nil))
1205 (insert-buffer-substring mailbuf)
1206 (set-buffer-file-coding-system selected-coding)
1207 (goto-char (point-max))
1208 ;; require one newline at the end.
1209 (or (= (preceding-char) ?\n)
1210 (insert ?\n))
1211 ;; Change header-delimiter to be what sendmail expects.
1212 (goto-char (mail-header-end))
1213 (delete-region (point) (progn (end-of-line) (point)))
1214 (setq delimline (point-marker))
1215 (sendmail-sync-aliases)
1216 (if mail-aliases
1217 (expand-mail-aliases (point-min) delimline))
1218 (goto-char (point-min))
1219 ;; Ignore any blank lines in the header
1220 (while (and (re-search-forward "\n\n\n*" delimline t)
1221 (< (point) delimline))
1222 (replace-match "\n"))
1223 (goto-char (point-min))
1224 ;; Look for Resent- headers. They require sending
1225 ;; the message specially.
1226 (let ((case-fold-search t))
1227 (goto-char (point-min))
1228 (while (re-search-forward "^Resent-\\(to\\|cc\\|bcc\\):" delimline t)
1229 ;; Put a list of such addresses in resend-to-addresses.
1230 (setq resend-to-addresses
1231 (save-restriction
1232 (narrow-to-region (point)
1233 (save-excursion
1234 (forward-line 1)
1235 (while (looking-at "^[ \t]")
1236 (forward-line 1))
1237 (point)))
1238 (append (mail-parse-comma-list)
1239 resend-to-addresses)))
1240 ;; Delete Resent-BCC ourselves
1241 (if (save-excursion (beginning-of-line)
1242 (looking-at "resent-bcc"))
1243 (delete-region (line-beginning-position)
1244 (line-beginning-position 2))))
1245 ;; Apparently this causes a duplicate Sender.
1246 ;; ;; If the From is different than current user, insert Sender.
1247 ;; (goto-char (point-min))
1248 ;; (and (re-search-forward "^From:" delimline t)
1249 ;; (progn
1250 ;; (require 'mail-utils)
1251 ;; (not (string-equal
1252 ;; (mail-strip-quoted-names
1253 ;; (save-restriction
1254 ;; (narrow-to-region (point-min) delimline)
1255 ;; (mail-fetch-field "From")))
1256 ;; (user-login-name))))
1257 ;; (progn
1258 ;; (forward-line 1)
1259 ;; (insert "Sender: " (user-login-name) "\n")))
1260 ;; Don't send out a blank subject line
1261 (goto-char (point-min))
1262 (if (re-search-forward "^Subject:\\([ \t]*\n\\)+\\b" delimline t)
1263 (replace-match "")
1264 ;; This one matches a Subject just before the header delimiter.
1265 (if (and (re-search-forward "^Subject:\\([ \t]*\n\\)+" delimline t)
1266 (= (match-end 0) delimline))
1267 (replace-match "")))
1268 ;; Put the "From:" field in unless for some odd reason
1269 ;; they put one in themselves.
1270 (goto-char (point-min))
1271 (if (not (re-search-forward "^From:" delimline t))
1272 (mail-insert-from-field))
1273 ;; Possibly add a MIME header for the current coding system
1274 (let (charset where-content-type)
1275 (goto-char (point-min))
1276 (setq where-content-type
1277 (re-search-forward "^Content-type:" delimline t))
1278 (goto-char (point-min))
1279 (and (eq mail-send-nonascii 'mime)
1280 (not (re-search-forward "^MIME-version:" delimline t))
1281 (progn (skip-chars-forward "\0-\177")
1282 (/= (point) (point-max)))
1283 selected-coding
1284 (setq charset
1285 (coding-system-get selected-coding :mime-charset))
1286 (progn
1287 (goto-char delimline)
1288 (insert "MIME-version: 1.0\n"
1289 "Content-type: text/plain; charset="
1290 (symbol-name charset)
1291 "\nContent-Transfer-Encoding: 8bit\n")
1292 ;; The character set we will actually use
1293 ;; should override any specified in the message itself.
1294 (when where-content-type
1295 (goto-char where-content-type)
1296 (delete-region (point-at-bol)
1297 (progn (forward-line 1) (point)))))))
1298 ;; Insert an extra newline if we need it to work around
1299 ;; Sun's bug that swallows newlines.
1300 (goto-char (1+ delimline))
1301 (if (eval mail-mailer-swallows-blank-line)
1302 (newline))
1303 ;; Find and handle any FCC fields.
1304 (goto-char (point-min))
1305 (if (re-search-forward "^FCC:" delimline t)
1306 (progn
1307 (setq fcc-was-found t)
1308 (mail-do-fcc delimline)))
1309 (if mail-interactive
1310 (with-current-buffer errbuf
1311 (erase-buffer))))
1312 ;; Encode the header according to RFC2047.
1313 (mail-encode-header (point-min) delimline)
1314 (goto-char (point-min))
1315 (if (let ((case-fold-search t))
1316 (or resend-to-addresses
1317 (re-search-forward "^To:\\|^cc:\\|^bcc:"
1318 delimline t)))
1319 (let* ((default-directory "/")
1320 (coding-system-for-write selected-coding)
1321 (args
1322 (append (list (point-min) (point-max)
1323 sendmail-program
1324 nil errbuf nil "-oi")
1325 (and envelope-from
1326 (list "-f" envelope-from))
1327 ;; ;; Don't say "from root" if running under su.
1328 ;; (and (equal (user-real-login-name) "root")
1329 ;; (list "-f" (user-login-name)))
1330 (and mail-alias-file
1331 (list (concat "-oA" mail-alias-file)))
1332 (if mail-interactive
1333 sendmail-error-reporting-interactive
1334 sendmail-error-reporting-non-interactive)
1335 ;; Get the addresses from the message
1336 ;; unless this is a resend.
1337 ;; We must not do that for a resend
1338 ;; because we would find the original addresses.
1339 ;; For a resend, include the specific addresses.
1340 (or resend-to-addresses
1341 '("-t")
1342 )
1343 (if mail-use-dsn
1344 (list "-N" (mapconcat 'symbol-name
1345 mail-use-dsn ",")))
1346 )
1347 )
1348 (exit-value (apply 'call-process-region args)))
1349 (cond ((or (null exit-value) (eq 0 exit-value)))
1350 ((numberp exit-value)
1351 (setq error t)
1352 (error "Sending...failed with exit value %d" exit-value))
1353 ((stringp exit-value)
1354 (setq error t)
1355 (error "Sending...terminated by signal: %s" exit-value))
1356 (t
1357 (setq error t)
1358 (error "SENDMAIL-SEND-IT -- fall through: %S" exit-value))))
1359 (or fcc-was-found
1360 (error "No recipients")))
1361 (if mail-interactive
1362 (with-current-buffer errbuf
1363 (goto-char (point-min))
1364 (while (re-search-forward "\n\n* *" nil t)
1365 (replace-match "; "))
1366 (unless (zerop (buffer-size))
1367 (setq error t)
1368 (error "Sending...failed to %s"
1369 (buffer-substring (point-min) (point-max)))))))
1370 (kill-buffer tembuf)
1371 (when (buffer-live-p errbuf)
1372 (if error
1373 (switch-to-buffer-other-window errbuf)
1374 (kill-buffer errbuf))))))
1375
1376 (autoload 'rmail-output-to-rmail-buffer "rmailout")
1377
1378 (defun mail-do-fcc (header-end)
1379 "Find and act on any FCC: headers in the current message before HEADER-END.
1380 If a buffer is visiting the FCC file, append to it before
1381 offering to save it, if it was modified initially. If this is an
1382 Rmail buffer, update Rmail as needed. If there is no buffer,
1383 just append to the file, in Babyl format if necessary."
1384 (unless (markerp header-end)
1385 (error "Value of `header-end' must be a marker"))
1386 (let (fcc-list
1387 (mailbuf (current-buffer))
1388 (time (current-time)))
1389 (save-excursion
1390 (goto-char (point-min))
1391 (let ((case-fold-search t))
1392 (while (re-search-forward "^FCC:[ \t]*" header-end t)
1393 (push (buffer-substring (point)
1394 (progn
1395 (end-of-line)
1396 (skip-chars-backward " \t")
1397 (point)))
1398 fcc-list)
1399 (delete-region (match-beginning 0)
1400 (progn (forward-line 1) (point)))))
1401 (with-temp-buffer
1402 ;; This initial newline is not written out if we create a new
1403 ;; file (see below).
1404 (insert "\nFrom " (user-login-name) " " (current-time-string time) "\n")
1405 ;; Insert the time zone before the year.
1406 (forward-char -1)
1407 (forward-word -1)
1408 (require 'mail-utils)
1409 (insert (mail-rfc822-time-zone time) " ")
1410 (goto-char (point-max))
1411 (insert-buffer-substring mailbuf)
1412 ;; Make sure messages are separated.
1413 (goto-char (point-max))
1414 (insert ?\n)
1415 (goto-char 2)
1416 ;; ``Quote'' "^From " as ">From "
1417 ;; (note that this isn't really quoting, as there is no requirement
1418 ;; that "^[>]+From " be quoted in the same transparent way.)
1419 (let ((case-fold-search nil))
1420 (while (search-forward "\nFrom " nil t)
1421 (forward-char -5)
1422 (insert ?>)))
1423 (dolist (fcc fcc-list)
1424 (let* ((buffer (find-buffer-visiting fcc))
1425 (curbuf (current-buffer))
1426 dont-write-the-file
1427 buffer-matches-file
1428 (beg (point-min)) ; the initial blank line
1429 (end (point-max))
1430 ;; After the ^From line.
1431 (beg2 (save-excursion (goto-char (point-min))
1432 (forward-line 2) (point))))
1433 (if buffer
1434 ;; File is present in a buffer => append to that buffer.
1435 (with-current-buffer buffer
1436 (setq buffer-matches-file
1437 (and (not (buffer-modified-p))
1438 (verify-visited-file-modtime buffer)))
1439 (let ((msg (bound-and-true-p rmail-current-message))
1440 (buffer-read-only nil))
1441 ;; If MSG is non-nil, buffer is in Rmail mode.
1442 (if msg
1443 (let ((buff (generate-new-buffer " *mail-do-fcc")))
1444 (unwind-protect
1445 (progn
1446 (with-current-buffer buff
1447 (insert-buffer-substring curbuf (1+ beg) end))
1448 (rmail-output-to-rmail-buffer buff msg))
1449 (kill-buffer buff)))
1450 ;; Output file not in Rmail mode => just insert
1451 ;; at the end.
1452 (save-restriction
1453 (widen)
1454 (goto-char (point-max))
1455 (insert-buffer-substring curbuf beg end)))
1456 ;; Offer to save the buffer if it was modified
1457 ;; before we started.
1458 (unless buffer-matches-file
1459 (if (y-or-n-p (format "Save file %s? " fcc))
1460 (save-buffer))
1461 (setq dont-write-the-file t)))))
1462 ;; Append to the file directly, unless we've already taken
1463 ;; care of it.
1464 (unless dont-write-the-file
1465 (if (and (file-exists-p fcc)
1466 (mail-file-babyl-p fcc))
1467 ;; If the file is a Babyl file, convert the message to
1468 ;; Babyl format. Even though Rmail no longer uses
1469 ;; Babyl, this code can remain for the time being, on
1470 ;; the off-chance one FCCs to a Babyl file that has
1471 ;; not yet been converted to mbox.
1472 (let ((coding-system-for-write
1473 (or rmail-file-coding-system 'emacs-mule)))
1474 (with-temp-buffer
1475 (insert "\C-l\n0, unseen,,\n*** EOOH ***\nDate: "
1476 (mail-rfc822-date) "\n")
1477 (insert-buffer-substring curbuf beg2 end)
1478 (insert "\n\C-_")
1479 (write-region (point-min) (point-max) fcc t)))
1480 ;; Ensure there is a blank line between messages, but
1481 ;; not at the very start of the file.
1482 (write-region (if (file-exists-p fcc)
1483 (point-min)
1484 (1+ (point-min)))
1485 (point-max) fcc t)))
1486 (and buffer (not dont-write-the-file)
1487 (with-current-buffer buffer
1488 (set-visited-file-modtime)))))))))
1489
1490 (defun mail-sent-via ()
1491 "Make a Sent-via header line from each To or CC header line."
1492 (declare (obsolete "nobody can remember what it is for." "24.1"))
1493 (interactive)
1494 (save-excursion
1495 ;; put a marker at the end of the header
1496 (let ((end (copy-marker (mail-header-end)))
1497 (case-fold-search t))
1498 (goto-char (point-min))
1499 ;; search for the To: lines and make Sent-via: lines from them
1500 ;; search for the next To: line
1501 (while (re-search-forward "^\\(to\\|cc\\):" end t)
1502 ;; Grab this line plus all its continuations, sans the `to:'.
1503 (let ((to-line
1504 (buffer-substring (point)
1505 (progn
1506 (if (re-search-forward "^[^ \t\n]" end t)
1507 (backward-char 1)
1508 (goto-char end))
1509 (point)))))
1510 ;; Insert a copy, with altered header field name.
1511 (insert-before-markers "Sent-via:" to-line))))))
1512 \f
1513 (defun mail-to ()
1514 "Move point to end of To field, creating it if necessary."
1515 (interactive)
1516 (expand-abbrev)
1517 (mail-position-on-field "To"))
1518
1519 (defun mail-subject ()
1520 "Move point to end of Subject field, creating it if necessary."
1521 (interactive)
1522 (expand-abbrev)
1523 (mail-position-on-field "Subject"))
1524
1525 (defun mail-cc ()
1526 "Move point to end of CC field, creating it if necessary."
1527 (interactive)
1528 (expand-abbrev)
1529 (or (mail-position-on-field "cc" t)
1530 (progn (mail-position-on-field "to")
1531 (insert "\nCC: "))))
1532
1533 (defun mail-bcc ()
1534 "Move point to end of BCC field, creating it if necessary."
1535 (interactive)
1536 (expand-abbrev)
1537 (or (mail-position-on-field "bcc" t)
1538 (progn (mail-position-on-field "to")
1539 (insert "\nBCC: "))))
1540
1541 (defun mail-fcc (folder)
1542 "Add a new FCC field, with file name completion."
1543 (interactive "FFolder carbon copy: ")
1544 (expand-abbrev)
1545 (or (mail-position-on-field "fcc" t) ;Put new field after exiting FCC.
1546 (mail-position-on-field "to"))
1547 (insert "\nFCC: " folder))
1548
1549 (defun mail-reply-to ()
1550 "Move point to end of Reply-To field, creating it if necessary."
1551 (interactive)
1552 (expand-abbrev)
1553 (mail-position-on-field "Reply-To"))
1554
1555 (defun mail-mail-reply-to ()
1556 "Move point to end of Mail-Reply-To field, creating it if necessary."
1557 (interactive)
1558 (expand-abbrev)
1559 (or (mail-position-on-field "mail-reply-to" t)
1560 (progn (mail-position-on-field "to")
1561 (insert "\nMail-Reply-To: "))))
1562
1563 (defun mail-mail-followup-to ()
1564 "Move point to end of Mail-Followup-To field, creating it if necessary."
1565 (interactive)
1566 (expand-abbrev)
1567 (or (mail-position-on-field "mail-followup-to" t)
1568 (progn (mail-position-on-field "to")
1569 (insert "\nMail-Followup-To: "))))
1570
1571 (defun mail-position-on-field (field &optional soft)
1572 "Move to the end of the contents of header field FIELD.
1573 If there is no such header, insert one, unless SOFT is non-nil.
1574 If there are multiple FIELD fields, this goes to the first.
1575 Returns non-nil if FIELD was originally present."
1576 (let (end
1577 (case-fold-search t))
1578 (setq end (mail-header-end))
1579 (goto-char (point-min))
1580 (if (re-search-forward (concat "^" (regexp-quote field) ":") end t)
1581 (progn
1582 (re-search-forward "^[^ \t]" nil 'move)
1583 (beginning-of-line)
1584 (skip-chars-backward "\n")
1585 t)
1586 (or soft
1587 (progn (goto-char end)
1588 (insert field ": \n")
1589 (skip-chars-backward "\n")))
1590 nil)))
1591
1592 (defun mail-text ()
1593 "Move point to beginning of text field."
1594 (interactive)
1595 (expand-abbrev)
1596 (goto-char (mail-text-start)))
1597 \f
1598 (defun mail-signature (&optional atpoint)
1599 "Sign letter with signature.
1600 If the variable `mail-signature' is a string, inserts it.
1601 If it is t or nil, inserts the contents of the file `mail-signature-file'.
1602 Otherwise, evals `mail-signature'.
1603 Prefix argument ATPOINT means insert at point rather than the end."
1604 (interactive "*P")
1605 ;; Test for an unreadable file here, before we delete trailing
1606 ;; whitespace, so that we don't modify the buffer needlessly.
1607 (if (and (memq mail-signature '(t nil))
1608 (not (file-readable-p mail-signature-file)))
1609 (if (called-interactively-p 'interactive)
1610 (message "The signature file `%s' could not be read"
1611 mail-signature-file))
1612 (save-excursion
1613 (unless atpoint
1614 (goto-char (point-max))
1615 ;; Delete trailing whitespace and blank lines.
1616 (skip-chars-backward " \t\n")
1617 (end-of-line)
1618 (delete-region (point) (point-max)))
1619 (cond ((stringp mail-signature)
1620 (insert mail-signature))
1621 ((memq mail-signature '(t nil))
1622 (insert "\n\n-- \n")
1623 (insert-file-contents (expand-file-name mail-signature-file)))
1624 (t
1625 ;; FIXME add condition-case error handling?
1626 (eval mail-signature))))))
1627
1628 (defun mail-fill-yanked-message (&optional justifyp)
1629 "Fill the paragraphs of a message yanked into this one.
1630 Numeric argument means justify as well."
1631 (interactive "P")
1632 (save-excursion
1633 (goto-char (mail-text-start))
1634 (fill-individual-paragraphs (point)
1635 (point-max)
1636 justifyp
1637 mail-citation-prefix-regexp)))
1638
1639 (defun mail-indent-citation ()
1640 "Modify text just inserted from a message to be cited.
1641 The inserted text should be the region.
1642 When this function returns, the region is again around the modified text.
1643
1644 Normally, indent each nonblank line `mail-indentation-spaces' spaces.
1645 However, if `mail-yank-prefix' is non-nil, insert that prefix on each line."
1646 (mail-yank-clear-headers (region-beginning) (region-end))
1647 (if (null mail-yank-prefix)
1648 (indent-rigidly (region-beginning) (region-end)
1649 mail-indentation-spaces)
1650 (save-excursion
1651 (let ((end (set-marker (make-marker) (region-end))))
1652 (goto-char (region-beginning))
1653 (while (< (point) end)
1654 (insert mail-yank-prefix)
1655 (forward-line 1))))))
1656
1657 (defun mail-yank-original (arg)
1658 "Insert the message being replied to, if any (in Rmail).
1659 Puts point after the text and mark before.
1660 Normally, indents each nonblank line ARG spaces (default 3).
1661 However, if `mail-yank-prefix' is non-nil, insert that prefix on each line.
1662
1663 Just \\[universal-argument] as argument means don't indent, insert no prefix,
1664 and don't delete any header fields."
1665 (interactive "P")
1666 (if mail-reply-action
1667 (let ((start (point))
1668 (original mail-reply-action)
1669 (omark (mark t)))
1670 (and (consp original) (eq (car original) 'insert-buffer)
1671 (setq original (nth 1 original)))
1672 (if (consp original)
1673 (progn
1674 ;; Call yank function, and set the mark if it doesn't.
1675 (apply (car original) (cdr original))
1676 (if (eq omark (mark t))
1677 (push-mark (point))))
1678 ;; If the original message is in another window in the same
1679 ;; frame, delete that window to save space.
1680 (delete-windows-on original t)
1681 (with-no-warnings
1682 ;; We really want this to set mark.
1683 (insert-buffer original)
1684 ;; If they yank the original text, the encoding of the
1685 ;; original message is a better default than
1686 ;; the default buffer-file-coding-system.
1687 (and (coding-system-equal
1688 (default-value 'buffer-file-coding-system)
1689 buffer-file-coding-system)
1690 (setq buffer-file-coding-system
1691 (coding-system-change-text-conversion
1692 buffer-file-coding-system
1693 (coding-system-base
1694 (with-current-buffer original
1695 buffer-file-coding-system))))))
1696 (set-text-properties (point) (mark t) nil))
1697 (if (consp arg)
1698 nil
1699 (goto-char start)
1700 (let ((mail-indentation-spaces (if arg (prefix-numeric-value arg)
1701 mail-indentation-spaces))
1702 ;; Avoid error in Transient Mark mode
1703 ;; on account of mark's being inactive.
1704 (mark-even-if-inactive t))
1705 (cond (mail-citation-hook
1706 ;; Bind mail-citation-header to the inserted
1707 ;; message's header.
1708 (let ((mail-citation-header
1709 (buffer-substring-no-properties
1710 start
1711 (save-excursion
1712 (save-restriction
1713 (narrow-to-region start (point-max))
1714 (goto-char start)
1715 (rfc822-goto-eoh)
1716 (point))))))
1717 (run-hooks 'mail-citation-hook)))
1718 (mail-yank-hooks
1719 (run-hooks 'mail-yank-hooks))
1720 (t
1721 (mail-indent-citation)))))
1722 ;; This is like exchange-point-and-mark, but doesn't activate the mark.
1723 ;; It is cleaner to avoid activation, even though the command
1724 ;; loop would deactivate the mark because we inserted text.
1725 (goto-char (prog1 (mark t)
1726 (set-marker (mark-marker) (point) (current-buffer))))
1727 (if (not (eolp)) (insert ?\n)))))
1728
1729 (defun mail-yank-clear-headers (start end)
1730 (if (< end start)
1731 (let (temp)
1732 (setq temp start start end end temp)))
1733 (if mail-yank-ignored-headers
1734 (save-excursion
1735 (goto-char start)
1736 (if (search-forward "\n\n" end t)
1737 (save-restriction
1738 (narrow-to-region start (point))
1739 (goto-char start)
1740 (while (let ((case-fold-search t))
1741 (re-search-forward mail-yank-ignored-headers nil t))
1742 (beginning-of-line)
1743 (delete-region (point)
1744 (progn (re-search-forward "\n[^ \t]")
1745 (forward-char -1)
1746 (point)))))))))
1747
1748 (defun mail-yank-region (arg)
1749 "Insert the selected region from the message being replied to.
1750 Puts point after the text and mark before.
1751 Normally, indents each nonblank line ARG spaces (default 3).
1752 However, if `mail-yank-prefix' is non-nil, insert that prefix on each line.
1753
1754 Just \\[universal-argument] as argument means don't indent, insert no prefix,
1755 and don't delete any header fields."
1756 (interactive "P")
1757 (and (consp mail-reply-action)
1758 (memq (car mail-reply-action)
1759 '(rmail-yank-current-message insert-buffer))
1760 (with-current-buffer (nth 1 mail-reply-action)
1761 (or (mark t)
1762 (error "No mark set: %S" (current-buffer))))
1763 (let ((buffer (nth 1 mail-reply-action))
1764 (start (point))
1765 ;; Avoid error in Transient Mark mode
1766 ;; on account of mark's being inactive.
1767 (mark-even-if-inactive t))
1768 ;; Insert the citation text.
1769 (insert (with-current-buffer buffer
1770 (buffer-substring-no-properties (point) (mark))))
1771 (push-mark start)
1772 ;; Indent or otherwise annotate the citation text.
1773 (if (consp arg)
1774 nil
1775 (let ((mail-indentation-spaces (if arg (prefix-numeric-value arg)
1776 mail-indentation-spaces)))
1777 (if mail-citation-hook
1778 ;; Bind mail-citation-header to the original message's header.
1779 (let ((mail-citation-header
1780 (with-current-buffer buffer
1781 (buffer-substring-no-properties
1782 (point-min)
1783 (save-excursion
1784 (goto-char (point-min))
1785 (rfc822-goto-eoh)
1786 (point))))))
1787 (run-hooks 'mail-citation-hook))
1788 (if mail-yank-hooks
1789 (run-hooks 'mail-yank-hooks)
1790 (mail-indent-citation))))))))
1791
1792 (defun mail-split-line ()
1793 "Split current line, moving portion beyond point vertically down.
1794 If the current line has `mail-yank-prefix', insert it on the new line."
1795 (interactive "*")
1796 (split-line mail-yank-prefix))
1797
1798 \f
1799 (defun mail-insert-file (&optional file)
1800 "Insert a file at the end of the buffer, with separator lines around it."
1801 (interactive "fAttach file: ")
1802 (save-excursion
1803 (goto-char (point-max))
1804 (or (bolp) (newline))
1805 (newline)
1806 (let ((start (point))
1807 middle)
1808 (insert (format "===File %s===" file))
1809 (insert-char ?= (max 0 (- 60 (current-column))))
1810 (newline)
1811 (setq middle (point))
1812 (insert "============================================================\n")
1813 (push-mark)
1814 (goto-char middle)
1815 (insert-file-contents file)
1816 (or (bolp) (newline))
1817 (goto-char start))))
1818
1819 (define-obsolete-function-alias 'mail-attach-file 'mail-insert-file "24.1")
1820
1821 (declare-function mml-attach-file "mml"
1822 (file &optional type description disposition))
1823 (declare-function mm-default-file-encoding "mm-encode" (file))
1824
1825 (defun mail-add-attachment (file)
1826 "Add FILE as a MIME attachment to the end of the mail message being composed."
1827 (interactive "fAttach file: ")
1828 (mml-attach-file file
1829 (or (mm-default-file-encoding file)
1830 "application/octet-stream") nil)
1831 (setq mail-encode-mml t))
1832
1833 \f
1834 ;; Put these commands last, to reduce chance of lossage from quitting
1835 ;; in middle of loading the file.
1836
1837 ;;;###autoload
1838 (defun mail (&optional noerase to subject in-reply-to cc replybuffer
1839 actions return-action)
1840 "Edit a message to be sent. Prefix arg means resume editing (don't erase).
1841 When this function returns, the buffer `*mail*' is selected.
1842 The value is t if the message was newly initialized; otherwise, nil.
1843
1844 Optionally, the signature file `mail-signature-file' can be inserted at the
1845 end; see the variable `mail-signature'.
1846
1847 \\<mail-mode-map>
1848 While editing message, type \\[mail-send-and-exit] to send the message and exit.
1849
1850 Various special commands starting with C-c are available in sendmail mode
1851 to move to message header fields:
1852 \\{mail-mode-map}
1853
1854 If `mail-self-blind' is non-nil, a BCC to yourself is inserted
1855 when the message is initialized.
1856
1857 If `mail-default-reply-to' is non-nil, it should be an address (a string);
1858 a Reply-to: field with that address is inserted.
1859
1860 If `mail-archive-file-name' is non-nil, an FCC field with that file name
1861 is inserted.
1862
1863 The normal hook `mail-setup-hook' is run after the message is
1864 initialized. It can add more default fields to the message.
1865
1866 The first argument, NOERASE, determines what to do when there is
1867 an existing modified `*mail*' buffer. If NOERASE is nil, the
1868 existing mail buffer is used, and the user is prompted whether to
1869 keep the old contents or to erase them. If NOERASE has the value
1870 `new', a new mail buffer will be created instead of using the old
1871 one. Any other non-nil value means to always select the old
1872 buffer without erasing the contents.
1873
1874 The second through fifth arguments,
1875 TO, SUBJECT, IN-REPLY-TO and CC, specify if non-nil
1876 the initial contents of those header fields.
1877 These arguments should not have final newlines.
1878 The sixth argument REPLYBUFFER is a buffer which contains an
1879 original message being replied to, or else an action
1880 of the form (FUNCTION . ARGS) which says how to insert the original.
1881 Or it can be nil, if not replying to anything.
1882 The seventh argument ACTIONS is a list of actions to take
1883 if/when the message is sent. Each action looks like (FUNCTION . ARGS);
1884 when the message is sent, we apply FUNCTION to ARGS.
1885 This is how Rmail arranges to mark messages `answered'."
1886 (interactive "P")
1887 (if (eq noerase 'new)
1888 (pop-to-buffer-same-window (generate-new-buffer "*mail*"))
1889 (and noerase
1890 (not (get-buffer "*mail*"))
1891 (setq noerase nil))
1892 (pop-to-buffer-same-window "*mail*"))
1893
1894 ;; Avoid danger that the auto-save file can't be written.
1895 (let ((dir (expand-file-name
1896 (file-name-as-directory mail-default-directory))))
1897 (if (file-exists-p dir)
1898 (setq default-directory dir)))
1899 ;; Only call auto-save-mode if necessary, to avoid changing auto-save file.
1900 (if (or (and auto-save-default (not buffer-auto-save-file-name))
1901 (and (not auto-save-default) buffer-auto-save-file-name))
1902 (auto-save-mode auto-save-default))
1903 (mail-mode)
1904 ;; Disconnect the buffer from its visited file
1905 ;; (in case the user has actually visited a file *mail*).
1906 ;; (set-visited-file-name nil)
1907 (let (initialized)
1908 (and (not (and noerase
1909 (not (eq noerase 'new))))
1910 (if buffer-file-name
1911 (if (buffer-modified-p)
1912 (when (y-or-n-p "Buffer has unsaved changes; reinitialize it and discard them? ")
1913 (if (y-or-n-p "Disconnect buffer from visited file? ")
1914 (set-visited-file-name nil))
1915 t)
1916 (when (y-or-n-p "Reinitialize buffer, and disconnect it from the visited file? ")
1917 (set-visited-file-name nil)
1918 t))
1919 ;; A non-file-visiting buffer.
1920 (if (buffer-modified-p)
1921 (y-or-n-p "Unsent message being composed; erase it? ")
1922 t))
1923 (let ((inhibit-read-only t))
1924 (erase-buffer)
1925 (mail-setup to subject in-reply-to cc replybuffer actions
1926 return-action)
1927 (setq initialized t)))
1928 (if (and buffer-auto-save-file-name
1929 (file-exists-p buffer-auto-save-file-name))
1930 (message "Auto save file for draft message exists; consider M-x mail-recover"))
1931 initialized))
1932
1933 (declare-function dired-view-file "dired" ())
1934 (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
1935
1936 (defun mail-recover-1 ()
1937 "Pop up a list of auto-saved draft messages so you can recover one of them."
1938 (interactive)
1939 (let ((file-name (make-auto-save-file-name))
1940 (ls-lisp-support-shell-wildcards t)
1941 non-random-len wildcard)
1942 ;; Remove the random part from the auto-save-file-name, and
1943 ;; create a wildcard which matches possible candidates.
1944 ;; Note: this knows that make-auto-save-file-name appends
1945 ;; "#<RANDOM-STUFF>#" to the buffer name, where RANDOM-STUFF
1946 ;; is the result of (make-temp-name "").
1947 (setq non-random-len
1948 (- (length file-name) (length (make-temp-name "")) 1))
1949 (setq wildcard (concat (substring file-name 0 non-random-len) "*"))
1950 (if (null (file-expand-wildcards wildcard))
1951 (message "There are no auto-saved drafts to recover")
1952 ;; Bind dired-trivial-filenames to t because all auto-save file
1953 ;; names are normally ``trivial'', so Dired will set point after
1954 ;; all the files, at buffer bottom. We want it on the first
1955 ;; file instead.
1956 ;; Require dired so that dired-trivial-filenames does not get
1957 ;; unbound on exit from the let.
1958 (require 'dired)
1959 (let ((dired-trivial-filenames t))
1960 (dired-other-window wildcard (concat dired-listing-switches " -t")))
1961 (rename-buffer "*Auto-saved Drafts*" t)
1962 (save-excursion
1963 (goto-char (point-min))
1964 (or (looking-at " Move to the draft file you want to recover,")
1965 (let ((inhibit-read-only t))
1966 ;; Each line starts with a space so that Font Lock mode
1967 ;; won't highlight the first character.
1968 (insert "\
1969 Move to the draft file you want to recover, then type C-c C-c
1970 to recover text of message whose composition was interrupted.
1971 To browse text of a draft, type v on the draft file's line.
1972
1973 You can also delete some of these files;
1974 type d on a line to mark that file for deletion.
1975
1976 List of possible auto-save files for recovery:
1977
1978 "))))
1979 (use-local-map
1980 (let ((map (make-sparse-keymap)))
1981 (set-keymap-parent map (current-local-map))
1982 map))
1983 (define-key (current-local-map) "v"
1984 (lambda ()
1985 (interactive)
1986 (let ((coding-system-for-read 'utf-8-emacs-unix))
1987 (dired-view-file))))
1988 (define-key (current-local-map) "\C-c\C-c"
1989 (lambda ()
1990 (interactive)
1991 (let ((fname (dired-get-filename))
1992 ;; Auto-saved files are written in the internal
1993 ;; representation, so they should be read accordingly.
1994 (coding-system-for-read 'utf-8-emacs-unix))
1995 (switch-to-buffer-other-window "*mail*")
1996 (let ((buffer-read-only nil))
1997 (erase-buffer)
1998 (insert-file-contents fname nil)
1999 ;; insert-file-contents will set buffer-file-coding-system
2000 ;; to utf-8-emacs, which is probably not what they want to
2001 ;; use for sending the message. But we don't know what
2002 ;; was its value before the buffer was killed or Emacs
2003 ;; crashed. We therefore reset buffer-file-coding-system
2004 ;; to the default value, so that either the default does
2005 ;; TRT, or the user will get prompted for the right
2006 ;; encoding when they send the message.
2007 (setq buffer-file-coding-system
2008 (default-value 'buffer-file-coding-system)))))))))
2009
2010 (declare-function dired-move-to-filename "dired" (&optional raise-error eol))
2011 (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
2012 (declare-function dired-view-file "dired" ())
2013
2014 (defun mail-recover ()
2015 "Recover interrupted mail composition from auto-save files.
2016
2017 If the mail buffer has a current valid auto-save file,
2018 the command recovers that file. Otherwise, it displays a
2019 buffer showing the existing auto-saved draft messages;
2020 you can move to one of them and type C-c C-c to recover that one."
2021 (interactive)
2022 ;; In case they invoke us from some random buffer...
2023 (switch-to-buffer "*mail*")
2024 ;; If *mail* didn't exist, set its directory, so that auto-saved
2025 ;; drafts will be found.
2026 (let ((dir (expand-file-name
2027 (file-name-as-directory mail-default-directory))))
2028 (if (file-exists-p dir)
2029 (setq default-directory dir)))
2030 (or (eq major-mode 'mail-mode)
2031 (mail-mode))
2032 (let ((file-name buffer-auto-save-file-name))
2033 (cond ((and file-name (file-exists-p file-name))
2034 (let ((dispbuf
2035 ;; This used to invoke `ls' via call-process, but
2036 ;; dired-noselect is more portable to systems where
2037 ;; `ls' is not a standard program (it will use
2038 ;; ls-lisp instead).
2039 (dired-noselect file-name
2040 (concat dired-listing-switches " -t"))))
2041 (save-selected-window
2042 (switch-to-buffer-other-window dispbuf)
2043 (goto-char (point-min))
2044 (forward-line 2)
2045 (dired-move-to-filename)
2046 (setq dispbuf (rename-buffer "*Directory*" t)))
2047 (if (not (yes-or-no-p
2048 (format "Recover mail draft from auto save file %s? "
2049 file-name)))
2050 (error "mail-recover canceled")
2051 (let ((buffer-read-only nil)
2052 (buffer-coding buffer-file-coding-system)
2053 ;; Auto-save files are written in internal
2054 ;; representation of non-ASCII characters.
2055 (coding-system-for-read 'utf-8-emacs-unix))
2056 (erase-buffer)
2057 (insert-file-contents file-name nil)
2058 (setq buffer-file-coding-system buffer-coding)))))
2059 (t (mail-recover-1)))))
2060
2061 ;;;###autoload
2062 (defun mail-other-window (&optional noerase to subject in-reply-to cc replybuffer sendactions)
2063 "Like `mail' command, but display mail buffer in another window."
2064 (interactive "P")
2065 (switch-to-buffer-other-window "*mail*")
2066 (mail noerase to subject in-reply-to cc replybuffer sendactions))
2067
2068 ;;;###autoload
2069 (defun mail-other-frame (&optional noerase to subject in-reply-to cc replybuffer sendactions)
2070 "Like `mail' command, but display mail buffer in another frame."
2071 (interactive "P")
2072 (switch-to-buffer-other-frame "*mail*")
2073 (mail noerase to subject in-reply-to cc replybuffer sendactions))
2074
2075 ;; Do not add anything but external entries on this page.
2076
2077 (provide 'sendmail)
2078
2079 ;; Local Variables:
2080 ;; byte-compile-dynamic: t
2081 ;; End:
2082
2083 ;;; sendmail.el ends here