1 ;;; mh-comp.el --- MH-E functions for composing and sending messages
3 ;; Copyright (C) 1993, 1995, 1997, 2000-2016 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))
414 (components-file (mh-bare-components))
416 (cond ((and mh-draft-folder
(equal from-folder mh-draft-folder
))
417 (pop-to-buffer (find-file-noselect (mh-msg-filename message
))
419 (rename-buffer (format "draft-%d" message
))
420 ;; Make buffer writable...
421 (setq buffer-read-only nil
)
422 ;; If buffer was being used to display the message reinsert
424 (when (eq major-mode
'mh-show-mode
)
426 (insert-file-contents buffer-file-name
))
429 (mh-read-draft "clean-up" (mh-msg-filename message
) nil
)))))
430 (mh-clean-msg-header (point-min) mh-new-draft-cleaned-headers nil
)
431 (mh-insert-header-separator)
432 ;; Merge in components
435 (lambda (header-field)
436 (let ((field (car header-field
))
437 (value (cdr header-field
))
438 (case-fold-search t
))
441 ((string-match field
"^To$\\|^Cc$\\|^From$")
443 ((not (mh-goto-header-field (concat field
":")))
444 ;; Header field does not exist, add it
445 (mh-goto-header-end 0)
446 (insert field
": " value
"\n"))
447 ((string-equal value
"")
448 ;; Header field already exists and no value
451 ;; Header field exists and we have a value
452 (let (address mailbox
(alias (mh-alias-expand value
)))
454 (setq address
(ietf-drums-parse-address alias
))
455 (setq mailbox
(car address
)))
456 ;; XXX - Need to parse all addresses out of field
458 (not (mh-regexp-in-field-p
459 (concat "\\b" (regexp-quote value
) "\\b") field
))
461 (not (mh-regexp-in-field-p
462 (concat "\\b" (regexp-quote mailbox
) "\\b") field
)))
463 (insert " " value
","))
465 ((string-match field
"^Fcc$")
467 (mh-modify-header-field field value
))
468 ;; Text field, that's an easy case
470 (mh-modify-header-field field value
))))))
471 (mh-components-to-list components-file
))
472 (delete-file components-file
)
473 (goto-char (point-min))
475 (mh-compose-and-send-mail
476 draft
"" from-folder nil nil nil nil nil nil config
)
477 (mh-letter-mode-message)
478 (mh-letter-adjust-point)))
480 (defun mh-extract-header-field ()
481 "Extract field name and field value from the field at point.
482 Returns a list of field name and value (which may be null)."
483 (let ((end (save-excursion (mh-header-field-end)
485 (if (looking-at mh-letter-header-field-regexp
)
487 (goto-char (match-end 1))
489 (skip-chars-forward " \t")
490 (cons (match-string-no-properties 1) (buffer-substring-no-properties (point) end
))))))
493 (defun mh-components-to-list (components)
494 "Convert the COMPONENTS file to a list of field names and values."
495 (with-current-buffer (get-buffer-create mh-temp-buffer
)
497 (insert-file-contents components
)
498 (goto-char (point-min))
500 ((header-fields nil
))
501 (while (mh-in-header-p)
502 (setq header-fields
(append header-fields
(list (mh-extract-header-field))))
503 (mh-header-field-end)
509 (defun mh-extract-rejected-mail (message)
510 "Edit a MESSAGE that was returned by the mail system.
512 This command prepares the message for editing by removing the
513 Mailer-Daemon envelope and unneeded header fields. Fix whatever
514 addressing problem you had, and send the message again with
517 The default message is the current message.
520 (interactive (list (mh-get-msg-num t
)))
521 (let ((from-folder mh-current-folder
)
522 (config (current-window-configuration))
523 (draft (mh-read-draft "extraction" (mh-msg-filename message
) nil
)))
524 (goto-char (point-min))
525 (cond ((re-search-forward mh-rejected-letter-start nil t
)
526 (skip-chars-forward " \t\n")
527 (delete-region (point-min) (point))
528 (mh-clean-msg-header (point-min) mh-new-draft-cleaned-headers nil
))
530 (message "Does not appear to be a rejected letter")))
531 (mh-insert-header-separator)
532 (goto-char (point-min))
534 (mh-compose-and-send-mail draft
"" from-folder message
535 (mh-get-header-field "To:")
536 (mh-get-header-field "From:")
537 (mh-get-header-field "Cc:")
539 (mh-letter-mode-message)))
542 (defun mh-forward (to cc
&optional range
)
545 You are prompted for the TO and CC recipients. You are given a
546 draft to edit that looks like it would if you had run the MH
547 command \"forw\". You can then add some text.
549 You can forward several messages by using a RANGE. All of the
550 messages in the range are inserted into your draft. Check the
551 documentation of `mh-interactive-range' to see how RANGE is read
554 The hook `mh-forward-hook' is called on the draft.
556 See also `mh-compose-forward-as-mime-flag',
557 `mh-forward-subject-format', and `mh-send'."
558 (interactive (list (mh-interactive-read-address "To: ")
559 (mh-interactive-read-address "Cc: ")
560 (mh-interactive-range "Forward")))
561 (let* ((folder mh-current-folder
)
562 (msgs (mh-range-to-msg-list range
))
563 (config (current-window-configuration))
564 (fwd-msg-file (mh-msg-filename (car msgs
) folder
))
565 ;; forw always leaves file in "draft" since it doesn't have -draft
566 (draft-name (expand-file-name "draft" mh-user-path
))
567 (draft (cond ((or (not (file-exists-p draft-name
))
568 (y-or-n-p "The file draft exists; discard it? "))
569 (mh-exec-cmd "forw" "-build"
570 (if (and (mh-variant-p 'nmh
)
571 mh-compose-forward-as-mime-flag
)
574 (mh-coalesce-msg-list msgs
))
576 (mh-read-draft "" draft-name t
)
577 (mh-insert-fields "To:" to
"Cc:" cc
)
580 (mh-read-draft "" draft-name nil
)))))
583 (with-current-buffer (get-buffer-create mh-temp-buffer
)
585 (insert-file-contents fwd-msg-file
)
586 (setq orig-from
(mh-get-header-field "From:"))
587 (setq orig-subject
(mh-get-header-field "Subject:")))
589 (mh-forwarded-letter-subject orig-from orig-subject
)))
590 (mh-insert-fields "Subject:" forw-subject
)
591 (goto-char (point-min))
592 ;; Set the local value of mh-mail-header-separator according to what is
593 ;; present in the buffer...
594 (set (make-local-variable 'mh-mail-header-separator
)
596 (goto-char (mh-mail-header-end))
597 (buffer-substring-no-properties (point) (mh-line-end-position))))
598 (set (make-local-variable 'mail-header-separator
) mh-mail-header-separator
) ;override sendmail.el
599 ;; If using MML, translate MH-style directive
600 (if (equal mh-compose-insertion
'mml
)
602 (goto-char (mh-mail-header-end))
605 "^#forw \\[\\([^]]+\\)\\] \\(+\\S-+\\) \\(.*\\)$"
607 (let ((description (if (equal (match-string 1)
608 "forwarded messages")
609 "forwarded message %d"
611 (msgs (split-string (match-string 3)))
614 (delete-region (point) (progn (forward-line 1) (point)))
617 (mh-mml-forward-message (format description i
)
619 ;; Was inserted before us, move to end of file to preserve order
620 (goto-char (point-max)))))))
621 ;; Position just before forwarded message.
622 (if (re-search-forward "^------- Forwarded Message" nil t
)
624 (goto-char (mh-mail-header-end))
626 (delete-other-windows)
627 (mh-add-msgs-to-seq msgs
'forwarded t
)
628 (mh-compose-and-send-mail draft
"" folder msgs
630 mh-note-forw
"Forwarded:"
632 (mh-letter-mode-message)
633 (mh-letter-adjust-point)
634 (run-hooks 'mh-forward-hook
)))))
636 (defun mh-forwarded-letter-subject (from subject
)
637 "Return a Subject suitable for a forwarded message.
638 Original message has headers FROM and SUBJECT."
639 (let ((addr-start (string-match "<" from
))
640 (comment (string-match "(" from
)))
641 (cond ((and addr-start
(> addr-start
0))
642 ;; Full Name <luser@host>
643 (setq from
(substring from
0 (1- addr-start
))))
645 ;; luser@host (Full Name)
646 (setq from
(substring from
(1+ comment
) (1- (length from
)))))))
647 (format mh-forward-subject-format from subject
))
650 (defun mh-redistribute (to cc
&optional message
)
651 "Redistribute a message.
653 This command is similar in function to forwarding mail, but it
654 does not allow you to edit the message, nor does it add your name
655 to the \"From\" header field. It appears to the recipient as if
656 the message had come from the original sender. When you run this
657 command, you are prompted for the TO and CC recipients. The
658 default MESSAGE is the current message.
660 Also investigate the command \\[mh-edit-again] for another way to
661 redistribute messages.
663 See also `mh-redist-full-contents-flag'.
665 The hook `mh-annotate-msg-hook' is run after annotating the
666 message and scan line."
667 (interactive (list (mh-read-address "Redist-To: ")
668 (mh-read-address "Redist-Cc: ")
671 (setq message
(mh-get-msg-num t
)))
672 (save-window-excursion
673 (let ((folder mh-current-folder
)
674 (draft (mh-read-draft "redistribution"
675 (if mh-redist-full-contents-flag
676 (mh-msg-filename message
)
679 (mh-goto-header-end 0)
680 (insert "Resent-To: " to
"\n")
681 (if (not (equal cc
"")) (insert "Resent-cc: " cc
"\n"))
684 "^Message-Id:\\|^Received:\\|^Return-Path:\\|^Sender:\\|^Date:\\|^From:"
687 (message "Redistributing...")
688 (let ((env "mhdist=1"))
689 ;; Setup environment...
690 (setq env
(concat env
" mhaltmsg="
691 (if mh-redist-full-contents-flag
693 (mh-msg-filename message folder
))))
694 (unless mh-redist-full-contents-flag
695 (setq env
(concat env
" mhannotate=1")))
697 (if mh-redist-background
698 (mh-exec-cmd-env-daemon env mh-send-prog nil buffer-file-name
)
699 (mh-exec-cmd-error env mh-send-prog
"-push" buffer-file-name
))
701 (mh-annotate-msg message folder mh-note-dist
702 "-component" "Resent:"
703 "-text" (format "\"%s %s\"" to cc
)))
705 (message "Redistributing...done"))))
708 (defun mh-reply (message &optional reply-to includep
)
711 When you reply to a message, you are first prompted with \"Reply
712 to whom?\" (unless the optional argument REPLY-TO is provided).
713 You have several choices here.
715 Response Reply Goes To
717 from The person who sent the message. This is the
718 default, so <RET> is sufficient.
720 to Replies to the sender, plus all recipients in the
721 \"To:\" header field.
723 all cc Forms a reply to the addresses in the
724 \"Mail-Followup-To:\" header field if one
725 exists; otherwise forms a reply to the sender,
728 Depending on your answer, \"repl\" is given a different argument
729 to form your reply. Specifically, a choice of \"from\" or none at
730 all runs \"repl -nocc all\", and a choice of \"to\" runs \"repl
731 -cc to\". Finally, either \"cc\" or \"all\" runs \"repl -cc all
734 Two windows are then created. One window contains the message to
735 which you are replying in an MH-Show buffer. Your draft, in
736 MH-Letter mode (*note `mh-letter-mode'), is in the other window.
737 If the reply draft was not one that you expected, check the
738 things that affect the behavior of \"repl\" which include the
739 \"repl:\" profile component and the \"replcomps\" and
740 \"replgroupcomps\" files.
742 If you supply a prefix argument INCLUDEP, the message you are
743 replying to is inserted in your reply after having first been run
744 through \"mhl\" with the format file \"mhl.reply\".
746 Alternatively, you can customize the option `mh-yank-behavior'
747 and choose one of its \"Automatically\" variants to do the same
748 thing. If you do so, the prefix argument has no effect.
750 Another way to include the message automatically in your draft is
751 to use \"repl: -filter repl.filter\" in your MH profile.
753 If you wish to customize the header or other parts of the reply
754 draft, please see \"repl\" and \"mh-format\".
756 See also `mh-reply-show-message-flag',
757 `mh-reply-default-reply-to', and `mh-send'."
760 (let ((minibuffer-help-form
761 "from => Sender only\nto => Sender and primary recipients\ncc or all => Sender and all recipients"))
762 (or mh-reply-default-reply-to
763 (completing-read "Reply to whom (default from): "
764 '(("from") ("to") ("cc") ("all"))
768 (let* ((folder mh-current-folder
)
769 (show-buffer mh-show-buffer
)
770 (config (current-window-configuration))
771 (group-reply (or (equal reply-to
"cc") (equal reply-to
"all")))
772 (form-file (cond ((and (mh-variant-p 'nmh
'gnu-mh
) group-reply
773 (stringp mh-repl-group-formfile
))
774 mh-repl-group-formfile
)
775 ((stringp mh-repl-formfile
) mh-repl-formfile
)
777 (message "Composing a reply...")
778 (mh-exec-cmd "repl" "-build" "-noquery" "-nodraftfolder"
780 (list "-form" form-file
))
781 mh-current-folder message
782 (cond ((or (equal reply-to
"from") (equal reply-to
""))
784 ((equal reply-to
"to")
786 (group-reply (if (mh-variant-p 'nmh
'gnu-mh
)
787 '("-group" "-nocc" "me")
788 '("-cc" "all" "-nocc" "me"))))
789 (cond ((or (eq mh-yank-behavior
'autosupercite
)
790 (eq mh-yank-behavior
'autoattrib
))
792 (includep '("-filter" "mhl.reply"))
794 (let ((draft (mh-read-draft "reply"
795 (expand-file-name "reply" mh-user-path
)
797 (delete-other-windows)
800 (let ((to (mh-get-header-field "To:"))
801 (subject (mh-get-header-field "Subject:"))
802 (cc (mh-get-header-field "Cc:")))
803 (goto-char (point-min))
804 (mh-goto-header-end 1)
806 (not mh-reply-show-message-flag
)
807 (mh-in-show-buffer (show-buffer)
808 (mh-display-msg message folder
)))
809 (mh-add-msgs-to-seq message
'answered t
)
810 (message "Composing a reply...done")
811 (mh-compose-and-send-mail draft
"" folder message to subject cc
812 mh-note-repl
"Replied:" config
))
813 (when (and (or (eq 'autosupercite mh-yank-behavior
)
814 (eq 'autoattrib mh-yank-behavior
))
815 (eq (mh-show-buffer-message-number) mh-sent-from-msg
))
818 (mh-letter-mode-message))))
821 (defun mh-send (to cc subject
)
824 Your letter appears in an Emacs buffer whose mode is
825 MH-Letter (see `mh-letter-mode').
827 The arguments TO, CC, and SUBJECT can be used to prefill the
828 draft fields or suppress the prompts if `mh-compose-prompt-flag'
829 is on. They are also passed to the function set in the option
830 `mh-compose-letter-function'.
832 See also `mh-insert-x-mailer-flag' and `mh-letter-mode-hook'.
834 Outside of an MH-Folder buffer (`mh-folder-mode'), you must call
835 either \\[mh-smail] or \\[mh-smail-other-window] to compose a new
838 (mh-interactive-read-address "To: ")
839 (mh-interactive-read-address "Cc: ")
840 (mh-interactive-read-string "Subject: ")))
841 (let ((config (current-window-configuration)))
842 (delete-other-windows)
843 (mh-send-sub to cc subject config
)))
849 (defun mh-interactive-read-address (prompt)
851 If `mh-compose-prompt-flag' is non-nil, then read an address with
853 Otherwise return the empty string."
854 (if mh-compose-prompt-flag
(mh-read-address prompt
) ""))
856 (defun mh-interactive-read-string (prompt)
858 If `mh-compose-prompt-flag' is non-nil, then read a string with
860 Otherwise return the empty string."
861 (if mh-compose-prompt-flag
(read-string prompt
) ""))
864 (defun mh-show-buffer-message-number (&optional buffer
)
865 "Message number of displayed message in corresponding show buffer.
867 Return nil if show buffer not displayed.
868 If in `mh-letter-mode', don't display the message number being replied
869 to, but rather the message number of the show buffer associated with
870 our originating folder buffer.
871 Optional argument BUFFER can be used to specify the buffer."
875 (cond ((eq major-mode
'mh-show-mode
)
876 (let ((number-start (mh-search-from-end ?
/ buffer-file-name
)))
877 (string-to-number (substring buffer-file-name
878 (1+ number-start
)))))
879 ((and (eq major-mode
'mh-folder-mode
)
881 (get-buffer mh-show-buffer
))
882 (mh-show-buffer-message-number mh-show-buffer
))
883 ((and (eq major-mode
'mh-letter-mode
)
885 (get-buffer mh-sent-from-folder
))
886 (mh-show-buffer-message-number mh-sent-from-folder
))
890 (defun mh-send-sub (to cc subject config
)
891 "Do the real work of composing and sending a letter.
892 Expects the TO, CC, and SUBJECT fields as arguments.
893 CONFIG is the window configuration before sending mail."
894 (let ((folder mh-current-folder
)
895 (msg-num (mh-get-msg-num nil
)))
896 (message "Composing a message...")
897 (let ((draft (mh-read-draft
901 (mh-insert-fields "To:" to
"Subject:" subject
"Cc:" cc
)
902 (goto-char (point-max))
903 (mh-compose-and-send-mail draft
"" folder msg-num
906 (mh-letter-mode-message)
907 (mh-letter-adjust-point))))
909 (defun mh-bare-components ()
910 "Generate a temporary, clean components file and return its path."
911 ;; Let comp(1) create the skeleton for us. This is particularly
912 ;; important with nmh-1.5, because its default "components" needs
913 ;; some processing before it can be used. Unfortunately, comp(1)
914 ;; doesn't have a -build option. So, to avoid the possibility of
915 ;; clobbering an existing draft, create a temporary directory and
916 ;; use it as the drafts folder. Then copy the skeleton to a regular
917 ;; temp file, and return the regular temp file.
919 (temp-folder (mm-make-temp-file
920 (concat mh-user-path
"draftfolder.") t
)))
921 (mh-exec-cmd "comp" "-nowhatnowproc"
922 "-draftfolder" (format "+%s"
923 (file-name-nondirectory temp-folder
))
924 (if (stringp mh-comp-formfile
)
925 (list "-form" mh-comp-formfile
)))
926 (setq new
(mm-make-temp-file "comp."))
927 (rename-file (concat temp-folder
"/" "1") new t
)
928 (delete-file (concat temp-folder
"/" ".mh_sequences"))
929 (delete-directory temp-folder
)
932 (defun mh-read-draft (use initial-contents delete-contents-file
)
933 "Read draft file into a draft buffer and make that buffer the current one.
935 USE is a message used for prompting about the intended use of the
937 INITIAL-CONTENTS is filename that is read into an empty buffer, or nil
938 if buffer should not be modified. Delete the initial-contents file if
939 DELETE-CONTENTS-FILE flag is set.
940 Returns the draft folder's name.
941 If the draft folder facility is enabled in ~/.mh_profile, a new buffer
942 is used each time and saved in the draft folder. The draft file can
944 (cond (mh-draft-folder
945 (let ((orig-default-dir default-directory
)
946 (draft-file-name (mh-new-draft-name)))
947 (pop-to-buffer (generate-new-buffer
949 (file-name-nondirectory draft-file-name
))))
951 (insert-file-contents draft-file-name t
)
953 (setq default-directory orig-default-dir
)))
955 (let ((draft-name (expand-file-name "draft" mh-user-path
)))
956 (pop-to-buffer "draft") ; Create if necessary
957 (if (buffer-modified-p)
958 (if (y-or-n-p "Draft has been modified; kill anyway? ")
959 (set-buffer-modified-p nil
)
960 (error "Draft preserved")))
961 (setq buffer-file-name draft-name
)
962 (clear-visited-file-modtime)
964 (cond ((and (file-exists-p draft-name
)
965 (not (equal draft-name initial-contents
)))
966 (insert-file-contents draft-name
)
967 (delete-file draft-name
))))))
968 (cond ((and initial-contents
969 (or (zerop (buffer-size))
971 (format "A draft exists. Use for %s? " use
))
972 (if mh-error-if-no-draft
973 (error "A prior draft exists"))
976 (insert-file-contents initial-contents
)
977 (if delete-contents-file
(delete-file initial-contents
))))
980 (save-buffer)) ; Do not reuse draft name
983 (defun mh-new-draft-name ()
984 "Return the pathname of folder for draft messages."
986 (mh-exec-cmd-quiet t
"mhpath" mh-draft-folder
"new")
987 (buffer-substring (point-min) (1- (point-max)))))
989 (defun mh-insert-fields (&rest name-values
)
990 "Insert the NAME-VALUES pairs in the current buffer.
991 If the field exists, append the value to it.
992 Do not insert any pairs whose value is the empty string."
993 (let ((case-fold-search t
))
995 (let ((field-name (car name-values
))
996 (value (car (cdr name-values
))))
997 (if (not (string-match "^.*:$" field-name
))
998 (setq field-name
(concat field-name
":")))
999 (cond ((or (null value
)
1002 ((mh-position-on-field field-name
)
1003 (insert " " (or value
"")))
1005 (insert field-name
" " value
"\n")))
1006 (setq name-values
(cdr (cdr name-values
)))))))
1008 (defun mh-compose-and-send-mail (draft send-args
1009 sent-from-folder sent-from-msg
1011 annotate-char annotate-field
1013 "Edit and compose a draft message in buffer DRAFT and send or save it.
1014 SEND-ARGS is the argument passed to the send command.
1015 SENT-FROM-FOLDER is buffer containing scan listing of current folder,
1016 or nil if none exists.
1017 SENT-FROM-MSG is the message number or sequence name or nil.
1018 The TO, SUBJECT, and CC fields are passed to the
1019 `mh-compose-letter-function'.
1020 If ANNOTATE-CHAR is non-null, it is used to notate the scan listing of
1021 the message. In that case, the ANNOTATE-FIELD is used to build a
1022 string for `mh-annotate-msg'.
1023 CONFIG is the window configuration to restore after sending the
1025 (pop-to-buffer draft
)
1029 (mh-insert-identity mh-identity-default t
)
1030 (mh-identity-make-menu)
1031 (mh-identity-add-menu)
1033 ;; Cleanup possibly RFC2047 encoded subject header
1034 (mh-decode-message-subject)
1036 ;; Insert extra fields.
1037 (mh-insert-x-mailer)
1040 (mh-letter-hide-all-skipped-fields)
1042 (setq mh-sent-from-folder sent-from-folder
)
1043 (setq mh-sent-from-msg sent-from-msg
)
1044 (setq mh-send-args send-args
)
1045 (setq mh-annotate-char annotate-char
)
1046 (setq mh-annotate-field annotate-field
)
1047 (setq mh-previous-window-config config
)
1048 (setq mode-line-buffer-identification
(list " {%b}"))
1050 (mh-make-local-hook 'kill-buffer-hook
)
1051 (add-hook 'kill-buffer-hook
'mh-tidy-draft-buffer nil t
)
1052 (run-hook-with-args 'mh-compose-letter-function to subject cc
))
1054 (defun mh-insert-x-mailer ()
1055 "Append an X-Mailer field to the header.
1056 The versions of MH-E, Emacs, and MH are shown."
1057 ;; Lazily initialize mh-x-mailer-string.
1058 (when (and mh-insert-x-mailer-flag
(null mh-x-mailer-string
))
1059 (setq mh-x-mailer-string
1060 (format "MH-E %s; %s; %sEmacs %s"
1061 mh-version mh-variant-in-use
1062 (if (featurep 'xemacs
) "X" "GNU ")
1063 (cond ((not (featurep 'xemacs
))
1064 (string-match "[0-9]+\\.[0-9]+\\(\\.[0-9]+\\)?"
1066 (match-string 0 emacs-version
))
1067 ((string-match "[0-9.]*\\( +([ a-z]+[0-9]+)\\)?"
1069 (match-string 0 emacs-version
))
1070 (t (format "%s.%s" emacs-major-version
1071 emacs-minor-version
))))))
1072 ;; Insert X-Mailer, but only if it doesn't already exist.
1074 (when (and mh-insert-x-mailer-flag
1075 (null (mh-goto-header-field "X-Mailer")))
1076 (mh-insert-fields "X-Mailer:" mh-x-mailer-string
))))
1078 (defun mh-insert-x-face ()
1079 "Append X-Face, Face or X-Image-URL field to header.
1080 If the field already exists, this function does nothing."
1081 (when (and (stringp mh-x-face-file
)
1082 (file-exists-p mh-x-face-file
)
1083 (file-readable-p mh-x-face-file
))
1085 (unless (or (mh-position-on-field "X-Face")
1086 (mh-position-on-field "Face")
1087 (mh-position-on-field "X-Image-URL"))
1089 (goto-char (+ (point) (cadr (insert-file-contents mh-x-face-file
))))
1090 (if (not (looking-at "^"))
1092 (unless (looking-at "\\(X-Face\\|Face\\|X-Image-URL\\): ")
1093 (insert "X-Face: "))))))
1095 (defun mh-tidy-draft-buffer ()
1096 "Run when a draft buffer is destroyed."
1097 (let ((buffer (get-buffer mh-recipients-buffer
)))
1099 (kill-buffer buffer
))))
1101 (defun mh-letter-mode-message ()
1102 "Display a help message for users of `mh-letter-mode'.
1103 This should be the last function called when composing the draft."
1104 (message "%s" (substitute-command-keys
1105 (concat "Type \\[mh-send-letter] to send message, "
1106 "\\[mh-help] for help"))))
1108 (defun mh-letter-adjust-point ()
1109 "Move cursor to first header field if are using the no prompt mode."
1110 (unless mh-compose-prompt-flag
1111 (goto-char (point-max))
1112 (mh-letter-next-header-field)))
1114 (defun mh-annotate-msg (msg folder note
&rest args
)
1115 "Mark MSG in FOLDER with character NOTE and annotate message with ARGS.
1116 MSG can be a message number, a list of message numbers, or a sequence.
1117 The hook `mh-annotate-msg-hook' is run after annotating; see its
1118 documentation for variables it can use."
1119 (apply 'mh-exec-cmd
"anno" folder
1120 (if (listp msg
) (append msg args
) (cons msg args
)))
1122 (cond ((get-buffer folder
) ; Buffer may be deleted
1124 (mh-iterate-on-range nil msg
1126 (+ mh-cmd-note mh-scan-field-destination-offset
))))))
1127 (let ((mh-current-folder folder
)
1128 ;; mh-annotate-list is a sequence name or a list of message numbers
1129 (mh-annotate-list (if (numberp msg
) (list msg
) msg
)))
1130 (run-hooks 'mh-annotate-msg-hook
)))
1132 (defun mh-insert-header-separator ()
1133 "Insert `mh-mail-header-separator', if absent."
1135 (goto-char (point-min))
1137 (if (looking-at "$")
1138 (insert mh-mail-header-separator
))))
1141 (defun mh-insert-auto-fields (&optional non-interactive
)
1142 "Insert custom fields if recipient is found in `mh-auto-fields-list'.
1144 Once the header contains one or more recipients, you may run this
1145 command to insert these fields manually. However, if you use this
1146 command, the automatic insertion when the message is sent is
1149 In a program, set buffer-local `mh-insert-auto-fields-done-local'
1150 if header fields were added. If NON-INTERACTIVE is non-nil,
1151 perform actions quietly and only if
1152 `mh-insert-auto-fields-done-local' is nil. Return t if fields
1153 added; otherwise return nil."
1155 (when (or (not non-interactive
)
1156 (not mh-insert-auto-fields-done-local
))
1158 (when (and (or (mh-goto-header-field "To:")
1159 (mh-goto-header-field "cc:")))
1160 (let ((list mh-auto-fields-list
)
1161 (fields-inserted nil
))
1163 (let ((regexp (nth 0 (car list
)))
1164 (entries (nth 1 (car list
))))
1165 (when (mh-regexp-in-field-p regexp
"To:" "cc:")
1166 (setq mh-insert-auto-fields-done-local t
)
1167 (setq fields-inserted t
)
1168 (if (not non-interactive
)
1169 (message "Fields for %s added" regexp
))
1170 (let ((entry-list entries
))
1172 (let ((field (caar entry-list
))
1173 (value (cdar entry-list
)))
1175 ((equal ":identity" field
)
1177 ;;(and (not mh-identity-local)
1178 ;; Bug 1204506. But do we need to be able
1179 ;; to set an identity manually that won't be
1180 ;; overridden by mh-insert-auto-fields?
1181 (assoc value mh-identity-list
)
1183 (mh-insert-identity value
)))
1185 (mh-modify-header-field field value
1186 (equal field
"From")))))
1187 (setq entry-list
(cdr entry-list
))))))
1188 (setq list
(cdr list
)))
1189 fields-inserted
)))))
1191 (defun mh-modify-header-field (field value
&optional overwrite-flag
)
1192 "To header FIELD add VALUE.
1193 If OVERWRITE-FLAG is non-nil then the old value, if present, is
1195 (cond ((and overwrite-flag
1196 (mh-goto-header-field (concat field
":")))
1198 (delete-region (point) (mh-line-end-position)))
1199 ((and (not overwrite-flag
)
1200 (mh-regexp-in-field-p (concat "\\b" (regexp-quote value
) "\\b") field
))
1201 ;; Already there, do nothing.
1203 ((and (not overwrite-flag
)
1204 (mh-goto-header-field (concat field
":")))
1205 (insert " " value
","))
1207 (mh-goto-header-end 0)
1208 (insert field
": " value
"\n"))))
1210 (defun mh-regexp-in-field-p (regexp &rest fields
)
1211 "Non-nil means REGEXP was found in FIELDS."
1212 (let ((old-syntax-table (syntax-table)))
1215 (let ((search-result nil
))
1217 (let* ((field (car fields
))
1219 (or mh-regexp-in-field-syntax-table
1220 (let ((case-fold-search t
))
1222 ((string-match field
"^To$\\|^[BD]?cc$\\|^From$")
1223 mh-addr-syntax-table
)
1224 ((string-match field
"^Fcc$")
1225 mh-fcc-syntax-table
)
1229 (if (and (mh-goto-header-field field
)
1230 (set-syntax-table syntax-table
)
1232 regexp
(save-excursion (mh-header-field-end)(point)) t
))
1235 (setq fields
(cdr fields
)))
1236 (set-syntax-table old-syntax-table
)))
1238 (set-syntax-table old-syntax-table
))))
1240 (defun mh-ascii-buffer-p ()
1241 "Check if current buffer is entirely composed of ASCII.
1242 The function doesn't work for XEmacs since `find-charset-region'
1243 doesn't exist there."
1244 (loop for charset in
(mh-funcall-if-exists
1245 find-charset-region
(point-min) (point-max))
1246 unless
(eq charset
'ascii
) return nil
1252 ;; indent-tabs-mode: nil
1253 ;; sentence-end-double-space: nil
1256 ;;; mh-comp.el ends here