1 ;;; mh-comp.el --- MH-E functions for composing and sending messages
3 ;; Copyright (C) 1993, 1995, 1997, 2000-2014 Free Software Foundation,
6 ;; Author: Bill Wohler <wohler@newt.com>
7 ;; Maintainer: Bill Wohler <wohler@newt.com>
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
28 ;; This file includes the functions in the MH-Folder maps that get us
29 ;; into MH-Letter mode, as well the functions in the MH-Letter mode
30 ;; that are used to send the mail. Other that those, functions that
31 ;; are needed in mh-letter.el should be found there.
38 (require 'mh-gnus
) ;needed because mh-gnus.el not compiled
43 (autoload 'easy-menu-add
"easymenu")
44 (autoload 'mml-insert-tag
"mml")
48 ;;; Site Customization
50 (defvar mh-send-prog
"send"
51 "Name of the MH send program.
52 Some sites need to change this because of a name conflict.")
54 (defvar mh-send-uses-spost-flag nil
55 "Non-nil means \"send\" uses \"spost\" to submit messages.
57 If the value of \"postproc:\" is \"spost\", you may need to set
58 this variable to t to tell MH-E to avoid using features of
59 \"post\" that are not supported by \"spost\". You'll know that
60 you'll need to do this if sending mail fails with an error of
61 \"spost: -msgid unknown\".")
63 (defvar mh-redist-background nil
64 "If non-nil redist will be done in background like send.
65 This allows transaction log to be visible if -watch, -verbose or
72 (defvar mh-comp-formfile
"components"
73 "Name of file to be used as a skeleton for composing messages.
75 Default is \"components\".
77 If not an absolute file name, the file is searched for first in the
78 user's MH directory, then in the system MH lib directory.")
80 (defvar mh-repl-formfile
"replcomps"
81 "Name of file to be used as a skeleton for replying to messages.
83 Default is \"replcomps\".
85 If not an absolute file name, the file is searched for first in the
86 user's MH directory, then in the system MH lib directory.")
88 (defvar mh-repl-group-formfile
"replgroupcomps"
89 "Name of file to be used as a skeleton for replying to messages.
91 Default is \"replgroupcomps\".
93 This file is used to form replies to the sender and all recipients of
94 a message. Only used if `(mh-variant-p 'nmh)' is non-nil.
95 If not an absolute file name, the file is searched for first in the
96 user's MH directory, then in the system MH lib directory.")
98 (defvar mh-rejected-letter-start
101 '("Content-Type: message/rfc822" ;MIME MDN
102 "------ This is a copy of the message, including all the headers. ------";from exim
103 "--- Below this line is a copy of the message."; from qmail
104 " ----- Unsent message follows -----" ;from sendmail V5
105 " --------Unsent Message below:" ; from sendmail at BU
106 " ----- Original message follows -----" ;from sendmail V8
107 "------- Unsent Draft" ;from MH itself
108 "---------- Original Message ----------" ;from zmailer
109 " --- The unsent message follows ---" ;from AIX mail system
110 " Your message follows:" ;from MMDF-II
111 "Content-Description: Returned Content" ;1993 KJ sendmail
114 (defvar mh-new-draft-cleaned-headers
115 "^Date:\\|^Received:\\|^Message-Id:\\|^From:\\|^Sender:\\|^Errors-To:\\|^Delivery-Date:\\|^Return-Path:"
116 "Regexp of header lines to remove before offering a message as a new draft\\<mh-folder-mode-map>.
117 Used by the \\[mh-edit-again] and \\[mh-extract-rejected-mail] commands.")
119 (defvar mh-letter-mode-syntax-table
120 (let ((syntax-table (make-syntax-table text-mode-syntax-table
)))
121 (modify-syntax-entry ?%
"." syntax-table
)
123 "Syntax table used by MH-E while in MH-Letter mode.")
125 (defvar mh-regexp-in-field-syntax-table nil
126 "Specify a syntax table for `mh-regexp-in-field-p' to use.")
128 (defvar mh-fcc-syntax-table
129 (let ((syntax-table (make-syntax-table text-mode-syntax-table
)))
130 (modify-syntax-entry ?
+ "w" syntax-table
)
131 (modify-syntax-entry ?
/ "w" syntax-table
)
133 "Syntax table used by MH-E while searching an Fcc field.")
135 (defvar mh-addr-syntax-table
136 (let ((syntax-table (make-syntax-table text-mode-syntax-table
)))
137 (modify-syntax-entry ?
! "w" syntax-table
)
138 (modify-syntax-entry ?
# "w" syntax-table
)
139 (modify-syntax-entry ?$
"w" syntax-table
)
140 (modify-syntax-entry ?%
"w" syntax-table
)
141 (modify-syntax-entry ?
& "w" syntax-table
)
142 (modify-syntax-entry ?
' "w" syntax-table
)
143 (modify-syntax-entry ?
* "w" syntax-table
)
144 (modify-syntax-entry ?
+ "w" syntax-table
)
145 (modify-syntax-entry ?-
"w" syntax-table
)
146 (modify-syntax-entry ?
/ "w" syntax-table
)
147 (modify-syntax-entry ?
= "w" syntax-table
)
148 (modify-syntax-entry ??
"w" syntax-table
)
149 (modify-syntax-entry ?^
"w" syntax-table
)
150 (modify-syntax-entry ?_
"w" syntax-table
)
151 (modify-syntax-entry ?
` "w" syntax-table
)
152 (modify-syntax-entry ?
{ "w" syntax-table
)
153 (modify-syntax-entry ?|
"w" syntax-table
)
154 (modify-syntax-entry ?
} "w" syntax-table
)
155 (modify-syntax-entry ?~
"w" syntax-table
)
156 (modify-syntax-entry ?.
"w" syntax-table
)
157 (modify-syntax-entry ?
@ "w" syntax-table
)
159 "Syntax table used by MH-E while searching an address field.")
161 (defvar mh-send-args
""
162 "Extra args to pass to \"send\" command.")
164 (defvar mh-annotate-char nil
165 "Character to use to annotate `mh-sent-from-msg'.")
167 (defvar mh-annotate-field nil
168 "Field name for message annotation.")
170 (defvar mh-annotate-list nil
171 "Messages annotated, either a sequence name or a list of message numbers.
172 This variable can be used by `mh-annotate-msg-hook'.")
174 (defvar mh-insert-auto-fields-done-local nil
175 "Buffer-local variable set when `mh-insert-auto-fields' called successfully.")
176 (make-variable-buffer-local 'mh-insert-auto-fields-done-local
)
180 ;;; MH-E Entry Points
184 "Compose a message with the MH mail system.
185 See `mh-send' for more details on composing mail."
188 (call-interactively 'mh-send
))
191 (defun mh-smail-other-window ()
192 "Compose a message with the MH mail system in other window.
193 See `mh-send' for more details on composing mail."
196 (call-interactively 'mh-send-other-window
))
198 (defun mh-send-other-window (to cc subject
)
199 "Compose a message in another window.
201 See `mh-send' for more information and a description of how the
202 TO, CC, and SUBJECT arguments are used."
204 (mh-interactive-read-address "To: ")
205 (mh-interactive-read-address "Cc: ")
206 (mh-interactive-read-string "Subject: ")))
207 (let ((pop-up-windows t
))
208 (mh-send-sub to cc subject
(current-window-configuration))))
210 (defvar mh-error-if-no-draft nil
) ;raise error over using old draft
213 (defun mh-smail-batch (&optional to subject other-headers
&rest ignored
)
214 "Compose a message with the MH mail system.
216 This function does not prompt the user for any header fields, and
217 thus is suitable for use by programs that want to create a mail
218 buffer. Users should use \\[mh-smail] to compose mail.
220 Optional arguments for setting certain fields include TO,
221 SUBJECT, and OTHER-HEADERS. Additional arguments are IGNORED.
223 This function remains for Emacs 21 compatibility. New
224 applications should use `mh-user-agent-compose'."
226 (let ((mh-error-if-no-draft t
))
227 (mh-send (or to
"") "" (or subject
""))))
230 (define-mail-user-agent 'mh-e-user-agent
231 'mh-user-agent-compose
'mh-send-letter
'mh-fully-kill-draft
232 'mh-before-send-letter-hook
)
235 (defun mh-user-agent-compose (&optional to subject other-headers continue
236 switch-function yank-action
237 send-actions return-action
239 "Set up mail composition draft with the MH mail system.
240 This is the `mail-user-agent' entry point to MH-E. This function
241 conforms to the contract specified by `define-mail-user-agent'
242 which means that this function should accept the same arguments
245 The optional arguments TO and SUBJECT specify recipients and the
246 initial Subject field, respectively.
248 OTHER-HEADERS is an alist specifying additional header fields.
249 Elements look like (HEADER . VALUE) where both HEADER and VALUE
252 CONTINUE, SWITCH-FUNCTION, YANK-ACTION, SEND-ACTIONS, and
253 RETURN-ACTION and any additional arguments are IGNORED."
255 (let ((mh-error-if-no-draft t
))
256 (mh-send to
"" subject
)
258 (mh-insert-fields (concat (car (car other-headers
)) ":")
259 (cdr (car other-headers
)))
260 (setq other-headers
(cdr other-headers
)))))
264 (defvar sendmail-coding-system
))
267 (defun mh-send-letter (&optional arg
)
268 "Save draft and send message.
270 When you are all through editing a message, you send it with this
271 command. You can give a prefix argument ARG to monitor the first stage
272 of the delivery\; this output can be found in a buffer called \"*MH-E
275 The hook `mh-before-send-letter-hook' is run at the beginning of
276 this command. For example, if you want to check your spelling in
277 your message before sending, add the function `ispell-message'.
279 Unless `mh-insert-auto-fields' had previously been called
280 manually, the function `mh-insert-auto-fields' is called to
281 insert fields based upon the recipients. If fields are added, you
282 are given a chance to see and to confirm these fields before the
283 message is actually sent. You can do away with this confirmation
284 by turning off the option `mh-auto-fields-prompt-flag'.
286 In case the MH \"send\" program is installed under a different name,
287 use `mh-send-prog' to tell MH-E the name.
289 The hook `mh-annotate-msg-hook' is run after annotating the
290 message and scan line."
292 (run-hooks 'mh-before-send-letter-hook
)
293 (if (and (mh-insert-auto-fields t
)
294 mh-auto-fields-prompt-flag
295 (goto-char (point-min)))
296 (if (not (y-or-n-p "Auto fields inserted, send? "))
297 (error "Send aborted")))
298 (cond ((mh-mh-directive-present-p)
300 ((or (mh-mml-tag-present-p) (not (mh-ascii-buffer-p)))
303 (message "Sending...")
304 (let ((draft-buffer (current-buffer))
305 (file-name buffer-file-name
)
306 (config mh-previous-window-config
)
307 (coding-system-for-write
308 (if (and (local-variable-p 'buffer-file-coding-system
309 (current-buffer)) ;XEmacs needs two args
310 ;; We're not sure why, but buffer-file-coding-system
311 ;; tends to get set to undecided-unix.
312 (not (memq buffer-file-coding-system
313 '(undecided undecided-unix undecided-dos
))))
314 buffer-file-coding-system
315 (or (and (boundp 'sendmail-coding-system
) sendmail-coding-system
)
316 (and (default-boundp 'buffer-file-coding-system
)
317 (default-value 'buffer-file-coding-system
))
319 ;; Older versions of spost do not support -msgid and -mime.
320 (unless mh-send-uses-spost-flag
321 ;; Adding a Message-ID field looks good, makes it easier to search for
322 ;; message in your +outbox, and best of all doesn't break threading for
323 ;; the recipient if you reply to a message in your +outbox.
324 (setq mh-send-args
(concat "-msgid " mh-send-args
))
325 ;; The default BCC encapsulation will make a MIME message unreadable.
326 ;; With nmh use the -mime arg to prevent this.
327 (if (and (mh-variant-p 'nmh
)
328 (mh-goto-header-field "Bcc:")
329 (mh-goto-header-field "Content-Type:"))
330 (setq mh-send-args
(concat "-mime " mh-send-args
))))
332 (pop-to-buffer mh-mail-delivery-buffer
)
334 (mh-exec-cmd-output mh-send-prog t
335 "-nodraftfolder" "-watch" "-nopush"
336 (split-string mh-send-args
) file-name
)
337 (goto-char (point-max)) ; show the interesting part
339 (set-buffer draft-buffer
)) ; for annotation below
341 (mh-exec-cmd-daemon mh-send-prog nil
342 "-nodraftfolder" "-noverbose"
343 (split-string mh-send-args
) file-name
)))
345 (mh-annotate-msg mh-sent-from-msg
348 "-component" mh-annotate-field
349 "-text" (format "\"%s %s\""
350 (mh-get-header-field "To:")
351 (mh-get-header-field "Cc:"))))
354 (y-or-n-p "Kill draft buffer? "))
355 (kill-buffer draft-buffer
)
357 (set-window-configuration config
))))
359 (message "Sending...done")
360 (message "Sending...backgrounded"))))
363 (defun mh-fully-kill-draft ()
364 "Quit editing and delete draft message.
366 If for some reason you are not happy with the draft, you can use
367 this command to kill the draft buffer and delete the draft
368 message. Use the command \\[kill-buffer] if you don't want to
369 delete the draft message."
371 (if (y-or-n-p "Kill draft message? ")
372 (let ((config mh-previous-window-config
))
373 (if (file-exists-p buffer-file-name
)
374 (delete-file buffer-file-name
))
375 (set-buffer-modified-p nil
)
376 (kill-buffer (buffer-name))
379 (set-window-configuration config
)))
380 (error "Message not killed")))
384 ;;; MH-Folder Commands
389 (defun mh-edit-again (message)
390 "Edit a MESSAGE to send it again.
392 If you don't complete a draft for one reason or another, and if
393 the draft buffer is no longer available, you can pick your draft
394 up again with this command. If you don't use a draft folder, your
395 last \"draft\" file will be used. If you use draft folders,
396 you'll need to visit the draft folder with \"\\[mh-visit-folder]
397 drafts <RET>\", use \\[mh-next-undeleted-msg] to move to the
398 appropriate message, and then use \\[mh-edit-again] to prepare
399 the message for editing.
401 This command can also be used to take messages that were sent to
402 you and to send them to more people.
404 Don't use this command to re-edit a message from a Mailer-Daemon
405 who complained that your mail wasn't posted for some reason or
406 another (see `mh-extract-rejected-mail').
408 The default message is the current message.
411 (interactive (list (mh-get-msg-num t
)))
412 (let* ((from-folder mh-current-folder
)
413 (config (current-window-configuration))
415 (cond ((and mh-draft-folder
(equal from-folder mh-draft-folder
))
416 (pop-to-buffer (find-file-noselect (mh-msg-filename message
))
418 (rename-buffer (format "draft-%d" message
))
419 ;; Make buffer writable...
420 (setq buffer-read-only nil
)
421 ;; If buffer was being used to display the message reinsert
423 (when (eq major-mode
'mh-show-mode
)
425 (insert-file-contents buffer-file-name
))
428 (mh-read-draft "clean-up" (mh-msg-filename message
) nil
)))))
429 (mh-clean-msg-header (point-min) mh-new-draft-cleaned-headers nil
)
430 (mh-insert-header-separator)
431 ;; Merge in components
434 (lambda (header-field)
435 (let ((field (car header-field
))
436 (value (cdr header-field
))
437 (case-fold-search t
))
440 ((string-match field
"^To$\\|^Cc$\\|^From$")
442 ((not (mh-goto-header-field (concat field
":")))
443 ;; Header field does not exist, add it
444 (mh-goto-header-end 0)
445 (insert field
": " value
"\n"))
446 ((string-equal value
"")
447 ;; Header field already exists and no value
450 ;; Header field exists and we have a value
451 (let (address mailbox
(alias (mh-alias-expand value
)))
453 (setq address
(ietf-drums-parse-address alias
))
454 (setq mailbox
(car address
)))
455 ;; XXX - Need to parse all addresses out of field
457 (not (mh-regexp-in-field-p
458 (concat "\\b" (regexp-quote value
) "\\b") field
))
460 (not (mh-regexp-in-field-p
461 (concat "\\b" (regexp-quote mailbox
) "\\b") field
)))
462 (insert " " value
","))
464 ((string-match field
"^Fcc$")
466 (mh-modify-header-field field value
))
467 ;; Text field, that's an easy case
469 (mh-modify-header-field field value
))))))
470 (mh-components-to-list (mh-find-components)))
471 (goto-char (point-min))
473 (mh-compose-and-send-mail
474 draft
"" from-folder nil nil nil nil nil nil config
)
475 (mh-letter-mode-message)
476 (mh-letter-adjust-point)))
478 (defun mh-extract-header-field ()
479 "Extract field name and field value from the field at point.
480 Returns a list of field name and value (which may be null)."
481 (let ((end (save-excursion (mh-header-field-end)
483 (if (looking-at mh-letter-header-field-regexp
)
485 (goto-char (match-end 1))
487 (skip-chars-forward " \t")
488 (cons (match-string-no-properties 1) (buffer-substring-no-properties (point) end
))))))
491 (defun mh-components-to-list (components)
492 "Convert the COMPONENTS file to a list of field names and values."
493 (with-current-buffer (get-buffer-create mh-temp-buffer
)
495 (insert-file-contents components
)
496 (goto-char (point-min))
498 ((header-fields nil
))
499 (while (mh-in-header-p)
500 (setq header-fields
(append header-fields
(list (mh-extract-header-field))))
501 (mh-header-field-end)
507 (defun mh-extract-rejected-mail (message)
508 "Edit a MESSAGE that was returned by the mail system.
510 This command prepares the message for editing by removing the
511 Mailer-Daemon envelope and unneeded header fields. Fix whatever
512 addressing problem you had, and send the message again with
515 The default message is the current message.
518 (interactive (list (mh-get-msg-num t
)))
519 (let ((from-folder mh-current-folder
)
520 (config (current-window-configuration))
521 (draft (mh-read-draft "extraction" (mh-msg-filename message
) nil
)))
522 (goto-char (point-min))
523 (cond ((re-search-forward mh-rejected-letter-start nil t
)
524 (skip-chars-forward " \t\n")
525 (delete-region (point-min) (point))
526 (mh-clean-msg-header (point-min) mh-new-draft-cleaned-headers nil
))
528 (message "Does not appear to be a rejected letter")))
529 (mh-insert-header-separator)
530 (goto-char (point-min))
532 (mh-compose-and-send-mail draft
"" from-folder message
533 (mh-get-header-field "To:")
534 (mh-get-header-field "From:")
535 (mh-get-header-field "Cc:")
537 (mh-letter-mode-message)))
540 (defun mh-forward (to cc
&optional range
)
543 You are prompted for the TO and CC recipients. You are given a
544 draft to edit that looks like it would if you had run the MH
545 command \"forw\". You can then add some text.
547 You can forward several messages by using a RANGE. All of the
548 messages in the range are inserted into your draft. Check the
549 documentation of `mh-interactive-range' to see how RANGE is read
552 The hook `mh-forward-hook' is called on the draft.
554 See also `mh-compose-forward-as-mime-flag',
555 `mh-forward-subject-format', and `mh-send'."
556 (interactive (list (mh-interactive-read-address "To: ")
557 (mh-interactive-read-address "Cc: ")
558 (mh-interactive-range "Forward")))
559 (let* ((folder mh-current-folder
)
560 (msgs (mh-range-to-msg-list range
))
561 (config (current-window-configuration))
562 (fwd-msg-file (mh-msg-filename (car msgs
) folder
))
563 ;; forw always leaves file in "draft" since it doesn't have -draft
564 (draft-name (expand-file-name "draft" mh-user-path
))
565 (draft (cond ((or (not (file-exists-p draft-name
))
566 (y-or-n-p "The file draft exists; discard it? "))
567 (mh-exec-cmd "forw" "-build"
568 (if (and (mh-variant-p 'nmh
)
569 mh-compose-forward-as-mime-flag
)
572 (mh-coalesce-msg-list msgs
))
574 (mh-read-draft "" draft-name t
)
575 (mh-insert-fields "To:" to
"Cc:" cc
)
578 (mh-read-draft "" draft-name nil
)))))
581 (with-current-buffer (get-buffer-create mh-temp-buffer
)
583 (insert-file-contents fwd-msg-file
)
584 (setq orig-from
(mh-get-header-field "From:"))
585 (setq orig-subject
(mh-get-header-field "Subject:")))
587 (mh-forwarded-letter-subject orig-from orig-subject
)))
588 (mh-insert-fields "Subject:" forw-subject
)
589 (goto-char (point-min))
590 ;; Set the local value of mh-mail-header-separator according to what is
591 ;; present in the buffer...
592 (set (make-local-variable 'mh-mail-header-separator
)
594 (goto-char (mh-mail-header-end))
595 (buffer-substring-no-properties (point) (mh-line-end-position))))
596 (set (make-local-variable 'mail-header-separator
) mh-mail-header-separator
) ;override sendmail.el
597 ;; If using MML, translate MH-style directive
598 (if (equal mh-compose-insertion
'mml
)
600 (goto-char (mh-mail-header-end))
603 "^#forw \\[\\([^]]+\\)\\] \\(+\\S-+\\) \\(.*\\)$"
605 (let ((description (if (equal (match-string 1)
606 "forwarded messages")
607 "forwarded message %d"
609 (msgs (split-string (match-string 3)))
612 (delete-region (point) (progn (forward-line 1) (point)))
615 (mh-mml-forward-message (format description i
)
617 ;; Was inserted before us, move to end of file to preserve order
618 (goto-char (point-max)))))))
619 ;; Position just before forwarded message.
620 (if (re-search-forward "^------- Forwarded Message" nil t
)
622 (goto-char (mh-mail-header-end))
624 (delete-other-windows)
625 (mh-add-msgs-to-seq msgs
'forwarded t
)
626 (mh-compose-and-send-mail draft
"" folder msgs
628 mh-note-forw
"Forwarded:"
630 (mh-letter-mode-message)
631 (mh-letter-adjust-point)
632 (run-hooks 'mh-forward-hook
)))))
634 (defun mh-forwarded-letter-subject (from subject
)
635 "Return a Subject suitable for a forwarded message.
636 Original message has headers FROM and SUBJECT."
637 (let ((addr-start (string-match "<" from
))
638 (comment (string-match "(" from
)))
639 (cond ((and addr-start
(> addr-start
0))
640 ;; Full Name <luser@host>
641 (setq from
(substring from
0 (1- addr-start
))))
643 ;; luser@host (Full Name)
644 (setq from
(substring from
(1+ comment
) (1- (length from
)))))))
645 (format mh-forward-subject-format from subject
))
648 (defun mh-redistribute (to cc
&optional message
)
649 "Redistribute a message.
651 This command is similar in function to forwarding mail, but it
652 does not allow you to edit the message, nor does it add your name
653 to the \"From\" header field. It appears to the recipient as if
654 the message had come from the original sender. When you run this
655 command, you are prompted for the TO and CC recipients. The
656 default MESSAGE is the current message.
658 Also investigate the command \\[mh-edit-again] for another way to
659 redistribute messages.
661 See also `mh-redist-full-contents-flag'.
663 The hook `mh-annotate-msg-hook' is run after annotating the
664 message and scan line."
665 (interactive (list (mh-read-address "Redist-To: ")
666 (mh-read-address "Redist-Cc: ")
669 (setq message
(mh-get-msg-num t
)))
670 (save-window-excursion
671 (let ((folder mh-current-folder
)
672 (draft (mh-read-draft "redistribution"
673 (if mh-redist-full-contents-flag
674 (mh-msg-filename message
)
677 (mh-goto-header-end 0)
678 (insert "Resent-To: " to
"\n")
679 (if (not (equal cc
"")) (insert "Resent-cc: " cc
"\n"))
682 "^Message-Id:\\|^Received:\\|^Return-Path:\\|^Sender:\\|^Date:\\|^From:"
685 (message "Redistributing...")
686 (let ((env "mhdist=1"))
687 ;; Setup environment...
688 (setq env
(concat env
" mhaltmsg="
689 (if mh-redist-full-contents-flag
691 (mh-msg-filename message folder
))))
692 (unless mh-redist-full-contents-flag
693 (setq env
(concat env
" mhannotate=1")))
695 (if mh-redist-background
696 (mh-exec-cmd-env-daemon env mh-send-prog nil buffer-file-name
)
697 (mh-exec-cmd-error env mh-send-prog
"-push" buffer-file-name
))
699 (mh-annotate-msg message folder mh-note-dist
700 "-component" "Resent:"
701 "-text" (format "\"%s %s\"" to cc
)))
703 (message "Redistributing...done"))))
706 (defun mh-reply (message &optional reply-to includep
)
709 When you reply to a message, you are first prompted with \"Reply
710 to whom?\" (unless the optional argument REPLY-TO is provided).
711 You have several choices here.
713 Response Reply Goes To
715 from The person who sent the message. This is the
716 default, so <RET> is sufficient.
718 to Replies to the sender, plus all recipients in the
719 \"To:\" header field.
721 all cc Forms a reply to the addresses in the
722 \"Mail-Followup-To:\" header field if one
723 exists; otherwise forms a reply to the sender,
726 Depending on your answer, \"repl\" is given a different argument
727 to form your reply. Specifically, a choice of \"from\" or none at
728 all runs \"repl -nocc all\", and a choice of \"to\" runs \"repl
729 -cc to\". Finally, either \"cc\" or \"all\" runs \"repl -cc all
732 Two windows are then created. One window contains the message to
733 which you are replying in an MH-Show buffer. Your draft, in
734 MH-Letter mode (*note `mh-letter-mode'), is in the other window.
735 If the reply draft was not one that you expected, check the
736 things that affect the behavior of \"repl\" which include the
737 \"repl:\" profile component and the \"replcomps\" and
738 \"replgroupcomps\" files.
740 If you supply a prefix argument INCLUDEP, the message you are
741 replying to is inserted in your reply after having first been run
742 through \"mhl\" with the format file \"mhl.reply\".
744 Alternatively, you can customize the option `mh-yank-behavior'
745 and choose one of its \"Automatically\" variants to do the same
746 thing. If you do so, the prefix argument has no effect.
748 Another way to include the message automatically in your draft is
749 to use \"repl: -filter repl.filter\" in your MH profile.
751 If you wish to customize the header or other parts of the reply
752 draft, please see \"repl\" and \"mh-format\".
754 See also `mh-reply-show-message-flag',
755 `mh-reply-default-reply-to', and `mh-send'."
758 (let ((minibuffer-help-form
759 "from => Sender only\nto => Sender and primary recipients\ncc or all => Sender and all recipients"))
760 (or mh-reply-default-reply-to
761 (completing-read "Reply to whom (default from): "
762 '(("from") ("to") ("cc") ("all"))
766 (let* ((folder mh-current-folder
)
767 (show-buffer mh-show-buffer
)
768 (config (current-window-configuration))
769 (group-reply (or (equal reply-to
"cc") (equal reply-to
"all")))
770 (form-file (cond ((and (mh-variant-p 'nmh
'gnu-mh
) group-reply
771 (stringp mh-repl-group-formfile
))
772 mh-repl-group-formfile
)
773 ((stringp mh-repl-formfile
) mh-repl-formfile
)
775 (message "Composing a reply...")
776 (mh-exec-cmd "repl" "-build" "-noquery" "-nodraftfolder"
778 (list "-form" form-file
))
779 mh-current-folder message
780 (cond ((or (equal reply-to
"from") (equal reply-to
""))
782 ((equal reply-to
"to")
784 (group-reply (if (mh-variant-p 'nmh
'gnu-mh
)
785 '("-group" "-nocc" "me")
786 '("-cc" "all" "-nocc" "me"))))
787 (cond ((or (eq mh-yank-behavior
'autosupercite
)
788 (eq mh-yank-behavior
'autoattrib
))
790 (includep '("-filter" "mhl.reply"))
792 (let ((draft (mh-read-draft "reply"
793 (expand-file-name "reply" mh-user-path
)
795 (delete-other-windows)
798 (let ((to (mh-get-header-field "To:"))
799 (subject (mh-get-header-field "Subject:"))
800 (cc (mh-get-header-field "Cc:")))
801 (goto-char (point-min))
802 (mh-goto-header-end 1)
804 (not mh-reply-show-message-flag
)
805 (mh-in-show-buffer (show-buffer)
806 (mh-display-msg message folder
)))
807 (mh-add-msgs-to-seq message
'answered t
)
808 (message "Composing a reply...done")
809 (mh-compose-and-send-mail draft
"" folder message to subject cc
810 mh-note-repl
"Replied:" config
))
811 (when (and (or (eq 'autosupercite mh-yank-behavior
)
812 (eq 'autoattrib mh-yank-behavior
))
813 (eq (mh-show-buffer-message-number) mh-sent-from-msg
))
816 (mh-letter-mode-message))))
819 (defun mh-send (to cc subject
)
822 Your letter appears in an Emacs buffer whose mode is
823 MH-Letter (see `mh-letter-mode').
825 The arguments TO, CC, and SUBJECT can be used to prefill the
826 draft fields or suppress the prompts if `mh-compose-prompt-flag'
827 is on. They are also passed to the function set in the option
828 `mh-compose-letter-function'.
830 See also `mh-insert-x-mailer-flag' and `mh-letter-mode-hook'.
832 Outside of an MH-Folder buffer (`mh-folder-mode'), you must call
833 either \\[mh-smail] or \\[mh-smail-other-window] to compose a new
836 (mh-interactive-read-address "To: ")
837 (mh-interactive-read-address "Cc: ")
838 (mh-interactive-read-string "Subject: ")))
839 (let ((config (current-window-configuration)))
840 (delete-other-windows)
841 (mh-send-sub to cc subject config
)))
847 (defun mh-interactive-read-address (prompt)
849 If `mh-compose-prompt-flag' is non-nil, then read an address with
851 Otherwise return the empty string."
852 (if mh-compose-prompt-flag
(mh-read-address prompt
) ""))
854 (defun mh-interactive-read-string (prompt)
856 If `mh-compose-prompt-flag' is non-nil, then read a string with
858 Otherwise return the empty string."
859 (if mh-compose-prompt-flag
(read-string prompt
) ""))
862 (defun mh-show-buffer-message-number (&optional buffer
)
863 "Message number of displayed message in corresponding show buffer.
865 Return nil if show buffer not displayed.
866 If in `mh-letter-mode', don't display the message number being replied
867 to, but rather the message number of the show buffer associated with
868 our originating folder buffer.
869 Optional argument BUFFER can be used to specify the buffer."
873 (cond ((eq major-mode
'mh-show-mode
)
874 (let ((number-start (mh-search-from-end ?
/ buffer-file-name
)))
875 (string-to-number (substring buffer-file-name
876 (1+ number-start
)))))
877 ((and (eq major-mode
'mh-folder-mode
)
879 (get-buffer mh-show-buffer
))
880 (mh-show-buffer-message-number mh-show-buffer
))
881 ((and (eq major-mode
'mh-letter-mode
)
883 (get-buffer mh-sent-from-folder
))
884 (mh-show-buffer-message-number mh-sent-from-folder
))
888 (defun mh-find-components ()
889 "Return the path to the components file."
894 (expand-file-name mh-comp-formfile mh-user-path
)))
898 (expand-file-name mh-comp-formfile mh-lib
)))
901 (error "Can't find %s in %s or %s"
902 mh-comp-formfile mh-user-path mh-lib
)))))
904 (defun mh-send-sub (to cc subject config
)
905 "Do the real work of composing and sending a letter.
906 Expects the TO, CC, and SUBJECT fields as arguments.
907 CONFIG is the window configuration before sending mail."
908 (let ((folder mh-current-folder
)
909 (msg-num (mh-get-msg-num nil
)))
910 (message "Composing a message...")
911 (let ((draft (mh-read-draft
915 (mh-insert-fields "To:" to
"Subject:" subject
"Cc:" cc
)
916 (goto-char (point-max))
917 (mh-compose-and-send-mail draft
"" folder msg-num
920 (mh-letter-mode-message)
921 (mh-letter-adjust-point))))
923 (defun mh-read-draft (use initial-contents delete-contents-file
)
924 "Read draft file into a draft buffer and make that buffer the current one.
926 USE is a message used for prompting about the intended use of the
928 INITIAL-CONTENTS is filename that is read into an empty buffer, or nil
929 if buffer should not be modified. Delete the initial-contents file if
930 DELETE-CONTENTS-FILE flag is set.
931 Returns the draft folder's name.
932 If the draft folder facility is enabled in ~/.mh_profile, a new buffer
933 is used each time and saved in the draft folder. The draft file can
935 (cond (mh-draft-folder
936 (let ((orig-default-dir default-directory
)
937 (draft-file-name (mh-new-draft-name)))
938 (pop-to-buffer (generate-new-buffer
940 (file-name-nondirectory draft-file-name
))))
942 (insert-file-contents draft-file-name t
)
944 (setq default-directory orig-default-dir
)))
946 (let ((draft-name (expand-file-name "draft" mh-user-path
)))
947 (pop-to-buffer "draft") ; Create if necessary
948 (if (buffer-modified-p)
949 (if (y-or-n-p "Draft has been modified; kill anyway? ")
950 (set-buffer-modified-p nil
)
951 (error "Draft preserved")))
952 (setq buffer-file-name draft-name
)
953 (clear-visited-file-modtime)
955 (cond ((and (file-exists-p draft-name
)
956 (not (equal draft-name initial-contents
)))
957 (insert-file-contents draft-name
)
958 (delete-file draft-name
))))))
959 (cond ((and initial-contents
960 (or (zerop (buffer-size))
962 (format "A draft exists. Use for %s? " use
))
963 (if mh-error-if-no-draft
964 (error "A prior draft exists"))
967 (insert-file-contents initial-contents
)
968 (if delete-contents-file
(delete-file initial-contents
))))
971 (save-buffer)) ; Do not reuse draft name
974 (defun mh-new-draft-name ()
975 "Return the pathname of folder for draft messages."
977 (mh-exec-cmd-quiet t
"mhpath" mh-draft-folder
"new")
978 (buffer-substring (point-min) (1- (point-max)))))
980 (defun mh-insert-fields (&rest name-values
)
981 "Insert the NAME-VALUES pairs in the current buffer.
982 If the field exists, append the value to it.
983 Do not insert any pairs whose value is the empty string."
984 (let ((case-fold-search t
))
986 (let ((field-name (car name-values
))
987 (value (car (cdr name-values
))))
988 (if (not (string-match "^.*:$" field-name
))
989 (setq field-name
(concat field-name
":")))
990 (cond ((or (null value
)
993 ((mh-position-on-field field-name
)
994 (insert " " (or value
"")))
996 (insert field-name
" " value
"\n")))
997 (setq name-values
(cdr (cdr name-values
)))))))
999 (defun mh-compose-and-send-mail (draft send-args
1000 sent-from-folder sent-from-msg
1002 annotate-char annotate-field
1004 "Edit and compose a draft message in buffer DRAFT and send or save it.
1005 SEND-ARGS is the argument passed to the send command.
1006 SENT-FROM-FOLDER is buffer containing scan listing of current folder,
1007 or nil if none exists.
1008 SENT-FROM-MSG is the message number or sequence name or nil.
1009 The TO, SUBJECT, and CC fields are passed to the
1010 `mh-compose-letter-function'.
1011 If ANNOTATE-CHAR is non-null, it is used to notate the scan listing of
1012 the message. In that case, the ANNOTATE-FIELD is used to build a
1013 string for `mh-annotate-msg'.
1014 CONFIG is the window configuration to restore after sending the
1016 (pop-to-buffer draft
)
1020 (mh-insert-identity mh-identity-default t
)
1021 (mh-identity-make-menu)
1022 (mh-identity-add-menu)
1024 ;; Cleanup possibly RFC2047 encoded subject header
1025 (mh-decode-message-subject)
1027 ;; Insert extra fields.
1028 (mh-insert-x-mailer)
1031 (mh-letter-hide-all-skipped-fields)
1033 (setq mh-sent-from-folder sent-from-folder
)
1034 (setq mh-sent-from-msg sent-from-msg
)
1035 (setq mh-send-args send-args
)
1036 (setq mh-annotate-char annotate-char
)
1037 (setq mh-annotate-field annotate-field
)
1038 (setq mh-previous-window-config config
)
1039 (setq mode-line-buffer-identification
(list " {%b}"))
1041 (mh-make-local-hook 'kill-buffer-hook
)
1042 (add-hook 'kill-buffer-hook
'mh-tidy-draft-buffer nil t
)
1043 (run-hook-with-args 'mh-compose-letter-function to subject cc
))
1045 (defun mh-insert-x-mailer ()
1046 "Append an X-Mailer field to the header.
1047 The versions of MH-E, Emacs, and MH are shown."
1048 ;; Lazily initialize mh-x-mailer-string.
1049 (when (and mh-insert-x-mailer-flag
(null mh-x-mailer-string
))
1050 (setq mh-x-mailer-string
1051 (format "MH-E %s; %s; %sEmacs %s"
1052 mh-version mh-variant-in-use
1053 (if (featurep 'xemacs
) "X" "GNU ")
1054 (cond ((not (featurep 'xemacs
))
1055 (string-match "[0-9]+\\.[0-9]+\\(\\.[0-9]+\\)?"
1057 (match-string 0 emacs-version
))
1058 ((string-match "[0-9.]*\\( +\([ a-z]+[0-9]+\)\\)?"
1060 (match-string 0 emacs-version
))
1061 (t (format "%s.%s" emacs-major-version
1062 emacs-minor-version
))))))
1063 ;; Insert X-Mailer, but only if it doesn't already exist.
1065 (when (and mh-insert-x-mailer-flag
1066 (null (mh-goto-header-field "X-Mailer")))
1067 (mh-insert-fields "X-Mailer:" mh-x-mailer-string
))))
1069 (defun mh-insert-x-face ()
1070 "Append X-Face, Face or X-Image-URL field to header.
1071 If the field already exists, this function does nothing."
1072 (when (and (file-exists-p mh-x-face-file
)
1073 (file-readable-p mh-x-face-file
))
1075 (unless (or (mh-position-on-field "X-Face")
1076 (mh-position-on-field "Face")
1077 (mh-position-on-field "X-Image-URL"))
1079 (goto-char (+ (point) (cadr (insert-file-contents mh-x-face-file
))))
1080 (if (not (looking-at "^"))
1082 (unless (looking-at "\\(X-Face\\|Face\\|X-Image-URL\\): ")
1083 (insert "X-Face: "))))))
1085 (defun mh-tidy-draft-buffer ()
1086 "Run when a draft buffer is destroyed."
1087 (let ((buffer (get-buffer mh-recipients-buffer
)))
1089 (kill-buffer buffer
))))
1091 (defun mh-letter-mode-message ()
1092 "Display a help message for users of `mh-letter-mode'.
1093 This should be the last function called when composing the draft."
1094 (message "%s" (substitute-command-keys
1095 (concat "Type \\[mh-send-letter] to send message, "
1096 "\\[mh-help] for help"))))
1098 (defun mh-letter-adjust-point ()
1099 "Move cursor to first header field if are using the no prompt mode."
1100 (unless mh-compose-prompt-flag
1101 (goto-char (point-max))
1102 (mh-letter-next-header-field)))
1104 (defun mh-annotate-msg (msg folder note
&rest args
)
1105 "Mark MSG in FOLDER with character NOTE and annotate message with ARGS.
1106 MSG can be a message number, a list of message numbers, or a sequence.
1107 The hook `mh-annotate-msg-hook' is run after annotating; see its
1108 documentation for variables it can use."
1109 (apply 'mh-exec-cmd
"anno" folder
1110 (if (listp msg
) (append msg args
) (cons msg args
)))
1112 (cond ((get-buffer folder
) ; Buffer may be deleted
1114 (mh-iterate-on-range nil msg
1116 (+ mh-cmd-note mh-scan-field-destination-offset
))))))
1117 (let ((mh-current-folder folder
)
1118 ;; mh-annotate-list is a sequence name or a list of message numbers
1119 (mh-annotate-list (if (numberp msg
) (list msg
) msg
)))
1120 (run-hooks 'mh-annotate-msg-hook
)))
1122 (defun mh-insert-header-separator ()
1123 "Insert `mh-mail-header-separator', if absent."
1125 (goto-char (point-min))
1127 (if (looking-at "$")
1128 (insert mh-mail-header-separator
))))
1131 (defun mh-insert-auto-fields (&optional non-interactive
)
1132 "Insert custom fields if recipient is found in `mh-auto-fields-list'.
1134 Once the header contains one or more recipients, you may run this
1135 command to insert these fields manually. However, if you use this
1136 command, the automatic insertion when the message is sent is
1139 In a program, set buffer-local `mh-insert-auto-fields-done-local'
1140 if header fields were added. If NON-INTERACTIVE is non-nil,
1141 perform actions quietly and only if
1142 `mh-insert-auto-fields-done-local' is nil. Return t if fields
1143 added; otherwise return nil."
1145 (when (or (not non-interactive
)
1146 (not mh-insert-auto-fields-done-local
))
1148 (when (and (or (mh-goto-header-field "To:")
1149 (mh-goto-header-field "cc:")))
1150 (let ((list mh-auto-fields-list
)
1151 (fields-inserted nil
))
1153 (let ((regexp (nth 0 (car list
)))
1154 (entries (nth 1 (car list
))))
1155 (when (mh-regexp-in-field-p regexp
"To:" "cc:")
1156 (setq mh-insert-auto-fields-done-local t
)
1157 (setq fields-inserted t
)
1158 (if (not non-interactive
)
1159 (message "Fields for %s added" regexp
))
1160 (let ((entry-list entries
))
1162 (let ((field (caar entry-list
))
1163 (value (cdar entry-list
)))
1165 ((equal ":identity" field
)
1167 ;;(and (not mh-identity-local)
1168 ;; Bug 1204506. But do we need to be able
1169 ;; to set an identity manually that won't be
1170 ;; overridden by mh-insert-auto-fields?
1171 (assoc value mh-identity-list
)
1173 (mh-insert-identity value
)))
1175 (mh-modify-header-field field value
1176 (equal field
"From")))))
1177 (setq entry-list
(cdr entry-list
))))))
1178 (setq list
(cdr list
)))
1179 fields-inserted
)))))
1181 (defun mh-modify-header-field (field value
&optional overwrite-flag
)
1182 "To header FIELD add VALUE.
1183 If OVERWRITE-FLAG is non-nil then the old value, if present, is
1185 (cond ((and overwrite-flag
1186 (mh-goto-header-field (concat field
":")))
1188 (delete-region (point) (mh-line-end-position)))
1189 ((and (not overwrite-flag
)
1190 (mh-regexp-in-field-p (concat "\\b" (regexp-quote value
) "\\b") field
))
1191 ;; Already there, do nothing.
1193 ((and (not overwrite-flag
)
1194 (mh-goto-header-field (concat field
":")))
1195 (insert " " value
","))
1197 (mh-goto-header-end 0)
1198 (insert field
": " value
"\n"))))
1200 (defun mh-regexp-in-field-p (regexp &rest fields
)
1201 "Non-nil means REGEXP was found in FIELDS."
1202 (let ((old-syntax-table (syntax-table)))
1205 (let ((search-result nil
))
1207 (let* ((field (car fields
))
1209 (or mh-regexp-in-field-syntax-table
1210 (let ((case-fold-search t
))
1212 ((string-match field
"^To$\\|^[BD]?cc$\\|^From$")
1213 mh-addr-syntax-table
)
1214 ((string-match field
"^Fcc$")
1215 mh-fcc-syntax-table
)
1219 (if (and (mh-goto-header-field field
)
1220 (set-syntax-table syntax-table
)
1222 regexp
(save-excursion (mh-header-field-end)(point)) t
))
1225 (setq fields
(cdr fields
)))
1226 (set-syntax-table old-syntax-table
)))
1228 (set-syntax-table old-syntax-table
))))
1230 (defun mh-ascii-buffer-p ()
1231 "Check if current buffer is entirely composed of ASCII.
1232 The function doesn't work for XEmacs since `find-charset-region'
1233 doesn't exist there."
1234 (loop for charset in
(mh-funcall-if-exists
1235 find-charset-region
(point-min) (point-max))
1236 unless
(eq charset
'ascii
) return nil
1242 ;; indent-tabs-mode: nil
1243 ;; sentence-end-double-space: nil
1246 ;;; mh-comp.el ends here