1 ;;; mh-comp --- mh-e functions for composing messages
2 ;; Time-stamp: <95/04/20 19:16:23 gildea>
4 ;; Copyright (C) 1993, 1995 Free Software Foundation, Inc.
6 ;; This file is part of GNU Emacs.
8 ;; GNU Emacs is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; GNU Emacs is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with GNU Emacs; see the file COPYING. If not, write to
20 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
24 ;; Internal support for mh-e package.
28 ;; $Id: mh-comp.el,v 1.4 1995/04/10 00:19:27 kwzh Exp kwzh $
35 ;;; Site customization (see also mh-utils.el):
37 (defvar mh-send-prog "send"
38 "Name of the MH send program.
39 Some sites need to change this because of a name conflict.")
41 (defvar mh-redist-full-contents nil
42 "Non-nil if the `dist' command needs whole letter for redistribution.
43 This is the case only when `send' is compiled with the BERK option.
44 If MH will not allow you to redist a previously redist'd msg, set to nil.")
47 (defvar mh-note-repl "-"
48 "String whose first character is used to notate replied to messages.")
50 (defvar mh-note-forw "F"
51 "String whose first character is used to notate forwarded messages.")
53 (defvar mh-note-dist "R"
54 "String whose first character is used to notate redistributed messages.")
56 (defvar mh-yank-hooks nil
57 "Obsolete hook for modifying a citation just inserted in the mail buffer.
58 Each hook function can find the citation between point and mark.
59 And each hook function should leave point and mark around the citation
62 This is a normal hook, misnamed for historical reasons.
63 It is semi-obsolete and is only used if mail-citation-hook is nil.")
65 (defvar mail-citation-hook nil
66 "*Hook for modifying a citation just inserted in the mail buffer.
67 Each hook function can find the citation between point and mark.
68 And each hook function should leave point and mark around the citation
71 If this hook is entirely empty (nil), the text of the message is inserted
72 with mh-ins-buf-prefix prefixed to each line.
74 See also the variable mh-yank-from-start-of-msg, which controls how
75 much of the message passed to the hook.")
77 ;;; Copied from sendmail.el for Hyperbole
78 (defvar mail-header-separator "--------"
79 "*Line used by MH to separate headers from text in messages being composed.")
81 ;;; Personal preferences:
83 (defvar mh-delete-yanked-msg-window nil
84 "*Controls window display when a message is yanked by \\<mh-letter-mode-map>\\[mh-yank-cur-msg].
85 If non-nil, yanking the current message into a draft letter deletes any
86 windows displaying the message.")
88 (defvar mh-yank-from-start-of-msg t
89 "*Controls which part of a message is yanked by \\<mh-letter-mode-map>\\[mh-yank-cur-msg].
90 If non-nil, include the entire message. If the symbol `body', then yank the
91 message minus the header. If nil, yank only the portion of the message
92 following the point. If the show buffer has a region, this variable is
95 (defvar mh-ins-buf-prefix "> "
96 "*String to put before each non-blank line of a yanked or inserted message.
97 \\<mh-letter-mode-map>Used when the message is inserted into an outgoing letter
98 by \\[mh-insert-letter] or \\[mh-yank-cur-msg].")
100 (defvar mh-reply-default-reply-to nil
101 "*Sets the person or persons to whom a reply will be sent.
102 If nil, prompt for recipient. If non-nil, then \\<mh-folder-mode-map>`\\[mh-reply]' will use this
103 value and it should be one of \"from\", \"to\", \"cc\", or \"all\".
104 The values \"cc\" and \"all\" do the same thing.")
106 (defvar mh-signature-file-name "~/.signature"
107 "*Name of file containing the user's signature.
108 Inserted into message by \\<mh-letter-mode-map>\\[mh-insert-signature].")
110 (defvar mh-forward-subject-format "%s: %s"
111 "*Format to generate the Subject: line contents for a forwarded message.
112 The two string arguments to the format are the sender of the original
113 message and the original subject line.")
115 (defvar mh-comp-formfile "components"
116 "Name of file to be used as a skeleton for composing messages.
117 Default is \"components\". If not a complete path name, the file
118 is searched for first in the user's MH directory, then in the
119 system MH lib directory.")
121 (defvar mh-repl-formfile "replcomps"
122 "Name of file to be used as a skeleton for replying to messages.
123 Default is \"replcomps\". If not a complete path name, the file
124 is searched for first in the user's MH directory, then in the
125 system MH lib directory.")
129 (defvar mh-letter-mode-hook nil
130 "Invoked in `mh-letter-mode' on a new letter.")
132 (defvar mh-compose-letter-function nil
133 "Invoked when setting up a letter draft.
134 It is passed three arguments: TO recipients, SUBJECT, and CC recipients.")
136 (defvar mh-before-send-letter-hook nil
137 "Invoked at the beginning of the \\<mh-letter-mode-map>\\[mh-send-letter] command.")
140 (defvar mh-rejected-letter-start
141 (concat "^ ----- Unsent message follows -----$" ;from sendmail V5
142 "\\|^ ----- Original message follows -----$" ;from sendmail V8
143 "\\|^------- Unsent Draft$" ;from MH itself
144 "\\|^---------- Original Message ----------$" ;from zmailer
145 "\\|^ --- The unsent message follows ---$" ;from AIX mail system
146 "\\|^ Your message follows:$" ;from MMDF-II
147 "\\|^Content-Description: Returned Content$" ;1993 KJ sendmail
149 "Regexp specifying the beginning of the wrapper around a returned letter.
150 This wrapper is generated by the mail system when rejecting a letter.")
152 (defvar mh-new-draft-cleaned-headers
153 "^Date:\\|^Received:\\|^Message-Id:\\|^From:\\|^Sender:\\|^Errors-To:\\|^Delivery-Date:\\|^Return-Path:"
154 "Regexp of header lines to remove before offering a message as a new draft.
155 Used by the \\<mh-folder-mode-map>`\\[mh-edit-again]' and `\\[mh-extract-rejected-mail]' commands.")
157 (defvar mh-to-field-choices '(("t" . "To:") ("s" . "Subject:") ("c" . "Cc:")
158 ("b" . "Bcc:") ("f" . "Fcc:") ("r" . "From:")
160 "Alist of (final-character . field-name) choices for mh-to-field.")
162 (defvar mh-letter-mode-map (copy-keymap text-mode-map)
163 "Keymap for composing mail.")
165 (defvar mh-letter-mode-syntax-table nil
166 "Syntax table used by mh-e while in MH-Letter mode.")
168 (if mh-letter-mode-syntax-table
170 (setq mh-letter-mode-syntax-table
171 (make-syntax-table text-mode-syntax-table))
172 (modify-syntax-entry ?% "." mh-letter-mode-syntax-table))
177 "Compose and send mail with the MH mail system.
178 This function is an entry point to mh-e, the Emacs front end
179 to the MH mail system.
181 See documentation of `\\[mh-send]' for more details on composing mail."
184 (call-interactively 'mh-send))
187 (defun mh-edit-again (msg)
188 "Clean-up a draft or a message previously sent and make it resendable.
189 Default is the current message.
190 The variable mh-new-draft-cleaned-headers specifies the headers to remove.
191 See also documentation for `\\[mh-send]' function."
192 (interactive (list (mh-get-msg-num t)))
193 (let* ((from-folder mh-current-folder)
194 (config (current-window-configuration))
196 (cond ((and mh-draft-folder (equal from-folder mh-draft-folder))
197 (pop-to-buffer (find-file-noselect (mh-msg-filename msg)) t)
198 (rename-buffer (format "draft-%d" msg))
201 (mh-read-draft "clean-up" (mh-msg-filename msg) nil)))))
202 (mh-clean-msg-header (point-min) mh-new-draft-cleaned-headers nil)
203 (goto-char (point-min))
204 (set-buffer-modified-p nil)
205 (mh-compose-and-send-mail draft "" from-folder nil nil nil nil nil nil
209 (defun mh-extract-rejected-mail (msg)
210 "Extract a letter returned by the mail system and make it resendable.
211 Default is the current message. The variable mh-new-draft-cleaned-headers
212 gives the headers to clean out of the original message.
213 See also documentation for `\\[mh-send]' function."
214 (interactive (list (mh-get-msg-num t)))
215 (let ((from-folder mh-current-folder)
216 (config (current-window-configuration))
217 (draft (mh-read-draft "extraction" (mh-msg-filename msg) nil)))
218 (goto-char (point-min))
219 (cond ((re-search-forward mh-rejected-letter-start nil t)
220 (skip-chars-forward " \t\n")
221 (delete-region (point-min) (point))
222 (mh-clean-msg-header (point-min) mh-new-draft-cleaned-headers nil))
224 (message "Does not appear to be a rejected letter.")))
225 (goto-char (point-min))
226 (set-buffer-modified-p nil)
227 (mh-compose-and-send-mail draft "" from-folder msg
228 (mh-get-header-field "To:")
229 (mh-get-header-field "From:")
230 (mh-get-header-field "Cc:")
234 (defun mh-forward (to cc &optional msg-or-seq)
235 "Forward a message or message sequence. Defaults to displayed message.
236 If optional prefix argument provided, then prompt for the message sequence.
237 See also documentation for `\\[mh-send]' function."
238 (interactive (list (mh-read-address "To: ")
239 (mh-read-address "Cc: ")
240 (if current-prefix-arg
241 (mh-read-seq-default "Forward" t)
242 (mh-get-msg-num t))))
244 (setq msg-or-seq (mh-get-msg-num t)))
245 (let* ((folder mh-current-folder)
246 (config (current-window-configuration))
247 ;; forw always leaves file in "draft" since it doesn't have -draft
248 (draft-name (expand-file-name "draft" mh-user-path))
249 (draft (cond ((or (not (file-exists-p draft-name))
250 (y-or-n-p "The file 'draft' exists. Discard it? "))
251 (mh-exec-cmd "forw" "-build"
252 mh-current-folder msg-or-seq)
254 (mh-read-draft "" draft-name t)
255 (mh-insert-fields "To:" to "Cc:" cc)
256 (set-buffer-modified-p nil)))
258 (mh-read-draft "" draft-name nil)))))
261 (goto-char (point-min))
262 (re-search-forward "^------- Forwarded Message")
264 (skip-chars-forward " \t\n")
266 (narrow-to-region (point) (point-max))
267 (setq orig-from (mh-get-header-field "From:"))
268 (setq orig-subject (mh-get-header-field "Subject:")))
270 (mh-forwarded-letter-subject orig-from orig-subject)))
271 (mh-insert-fields "Subject:" forw-subject)
272 (goto-char (point-min))
273 (re-search-forward "^------- Forwarded Message")
275 (delete-other-windows)
276 (if (numberp msg-or-seq)
277 (mh-add-msgs-to-seq msg-or-seq 'forwarded t)
278 (mh-add-msgs-to-seq (mh-seq-to-msgs msg-or-seq) 'forwarded t))
279 (mh-compose-and-send-mail draft "" folder msg-or-seq
281 mh-note-forw "Forwarded:"
284 (defun mh-forwarded-letter-subject (from subject)
285 ;; Return a Subject suitable for a forwarded message.
286 ;; Original message has headers FROM and SUBJECT.
287 (let ((addr-start (string-match "<" from))
288 (comment (string-match "(" from)))
289 (cond ((and addr-start (> addr-start 0))
290 ;; Full Name <luser@host>
291 (setq from (substring from 0 (1- addr-start))))
293 ;; luser@host (Full Name)
294 (setq from (substring from (1+ comment) (1- (length from)))))))
295 (format mh-forward-subject-format from subject))
299 (defun mh-smail-other-window ()
300 "Compose and send mail in other window with the MH mail system.
301 This function is an entry point to mh-e, the Emacs front end
302 to the MH mail system.
304 See documentation of `\\[mh-send]' for more details on composing mail."
307 (call-interactively 'mh-send-other-window))
310 (defun mh-redistribute (to cc &optional msg)
311 "Redistribute a letter.
312 Depending on how your copy of MH was compiled, you may need to change the
313 setting of the variable mh-redist-full-contents. See its documentation."
314 (interactive (list (mh-read-address "Redist-To: ")
315 (mh-read-address "Redist-Cc: ")
318 (setq msg (mh-get-msg-num t)))
319 (save-window-excursion
320 (let ((folder mh-current-folder)
321 (draft (mh-read-draft "redistribution"
322 (if mh-redist-full-contents
323 (mh-msg-filename msg)
326 (mh-goto-header-end 0)
327 (insert "Resent-To: " to "\n")
328 (if (not (equal cc "")) (insert "Resent-cc: " cc "\n"))
329 (mh-clean-msg-header (point-min)
330 "^Message-Id:\\|^Received:\\|^Return-Path:\\|^Sender:\\|^Date:\\|^From:"
333 (message "Redistributing...")
334 (if mh-redist-full-contents
335 (call-process "/bin/sh" nil 0 nil "-c"
336 (format "mhdist=1 mhaltmsg=%s %s -push %s"
338 (expand-file-name mh-send-prog mh-progs)
340 (call-process "/bin/sh" nil 0 nil "-c"
341 (format "mhdist=1 mhaltmsg=%s mhannotate=1 %s -push %s"
342 (mh-msg-filename msg folder)
343 (expand-file-name mh-send-prog mh-progs)
345 (mh-annotate-msg msg folder mh-note-dist
346 "-component" "Resent:"
347 "-text" (format "\"%s %s\"" to cc))
349 (message "Redistributing...done"))))
352 (defun mh-reply (message &optional includep)
353 "Reply to MESSAGE (default: current message).
354 If optional prefix argument INCLUDEP provided, then include the message
355 in the reply using filter mhl.reply in your MH directory.
356 Prompts for type of addresses to reply to:
358 to sender and primary recipients,
359 cc/all sender and all recipients.
360 If the file named by `mh-repl-formfile' exists, it is used as a skeleton
361 for the reply. See also documentation for `\\[mh-send]' function."
362 (interactive (list (mh-get-msg-num t) current-prefix-arg))
363 (let ((minibuffer-help-form
364 "from => Sender only\nto => Sender and primary recipients\ncc or all => Sender and all recipients"))
365 (let ((reply-to (or mh-reply-default-reply-to
366 (completing-read "Reply to whom: "
367 '(("from") ("to") ("cc") ("all"))
370 (folder mh-current-folder)
371 (show-buffer mh-show-buffer)
372 (config (current-window-configuration)))
373 (message "Composing a reply...")
374 (mh-exec-cmd "repl" "-build" "-noquery" "-nodraftfolder"
375 (if (stringp mh-repl-formfile) ;must be string, but we're paranoid
376 (list "-form" mh-repl-formfile))
377 mh-current-folder message
378 (cond ((or (equal reply-to "from") (equal reply-to ""))
380 ((equal reply-to "to")
382 ((or (equal reply-to "cc") (equal reply-to "all"))
383 '("-cc" "all" "-nocc" "me")))
385 '("-filter" "mhl.reply")))
386 (let ((draft (mh-read-draft "reply"
387 (expand-file-name "reply" mh-user-path)
389 (delete-other-windows)
390 (set-buffer-modified-p nil)
392 (let ((to (mh-get-header-field "To:"))
393 (subject (mh-get-header-field "Subject:"))
394 (cc (mh-get-header-field "Cc:")))
395 (goto-char (point-min))
396 (mh-goto-header-end 1)
398 (mh-in-show-buffer (show-buffer)
399 (mh-display-msg message folder)))
400 (mh-add-msgs-to-seq message 'answered t)
401 (message "Composing a reply...done")
402 (mh-compose-and-send-mail draft "" folder message to subject cc
403 mh-note-repl "Replied:" config))))))
406 (defun mh-send (to cc subject)
407 "Compose and send a letter.
408 The file named by `mh-comp-formfile' will be used as the form.
409 Do not call this function from outside mh-e; use \\[mh-smail] instead.
411 The letter is composed in mh-letter-mode; see its documentation for more
412 details. If `mh-compose-letter-function' is defined, it is called on the
413 draft and passed three arguments: to, subject, and cc."
415 (mh-read-address "To: ")
416 (mh-read-address "Cc: ")
417 (read-string "Subject: ")))
418 (let ((config (current-window-configuration)))
419 (delete-other-windows)
420 (mh-send-sub to cc subject config)))
423 (defun mh-send-other-window (to cc subject)
424 "Compose and send a letter in another window.
425 Do not call this function from outside mh-e;
426 use \\[mh-smail-other-window] instead.
427 See also documentation for `\\[mh-send]' function."
429 (mh-read-address "To: ")
430 (mh-read-address "Cc: ")
431 (read-string "Subject: ")))
432 (let ((pop-up-windows t))
433 (mh-send-sub to cc subject (current-window-configuration))))
436 (defun mh-send-sub (to cc subject config)
437 ;; Do the real work of composing and sending a letter.
438 ;; Expects the TO, CC, and SUBJECT fields as arguments.
439 ;; CONFIG is the window configuration before sending mail.
440 (let ((folder mh-current-folder)
441 (msg-num (mh-get-msg-num nil)))
442 (message "Composing a message...")
443 (let ((draft (mh-read-draft
449 (expand-file-name mh-comp-formfile mh-user-path)))
453 (expand-file-name mh-comp-formfile mh-lib)))
456 (error (format "Can't find components file \"%s\""
459 (mh-insert-fields "To:" to "Subject:" subject "Cc:" cc)
460 (goto-char (point-max))
461 (message "Composing a message...done")
462 (mh-compose-and-send-mail draft "" folder msg-num
467 (defun mh-read-draft (use initial-contents delete-contents-file)
468 ;; Read draft file into a draft buffer and make that buffer the current one.
469 ;; USE is a message used for prompting about the intended use of the message.
470 ;; INITIAL-CONTENTS is filename that is read into an empty buffer, or NIL
471 ;; if buffer should not be modified. Delete the initial-contents file if
472 ;; DELETE-CONTENTS-FILE flag is set.
473 ;; Returns the draft folder's name.
474 ;; If the draft folder facility is enabled in ~/.mh_profile, a new buffer is
475 ;; used each time and saved in the draft folder. The draft file can then be
477 (cond (mh-draft-folder
478 (let ((orig-default-dir default-directory)
479 (draft-file-name (mh-new-draft-name)))
480 (pop-to-buffer (generate-new-buffer
482 (file-name-nondirectory draft-file-name))))
484 (insert-file-contents draft-file-name t)
486 (setq default-directory orig-default-dir)))
488 (let ((draft-name (expand-file-name "draft" mh-user-path)))
489 (pop-to-buffer "draft") ; Create if necessary
490 (if (buffer-modified-p)
491 (if (y-or-n-p "Draft has been modified; kill anyway? ")
492 (set-buffer-modified-p nil)
493 (error "Draft preserved")))
494 (setq buffer-file-name draft-name)
495 (clear-visited-file-modtime)
497 (cond ((and (file-exists-p draft-name)
498 (not (equal draft-name initial-contents)))
499 (insert-file-contents draft-name)
500 (delete-file draft-name))))))
501 (cond ((and initial-contents
502 (or (zerop (buffer-size))
504 (format "A draft exists. Use for %s? " use)))))
506 (insert-file-contents initial-contents)
507 (if delete-contents-file (delete-file initial-contents))))
510 (save-buffer)) ; Do not reuse draft name
514 (defun mh-new-draft-name ()
515 ;; Returns the pathname of folder for draft messages.
517 (mh-exec-cmd-quiet t "mhpath" mh-draft-folder "new")
518 (buffer-substring (point-min) (1- (point-max)))))
521 (defun mh-annotate-msg (msg buffer note &rest args)
522 ;; Mark the MESSAGE in BUFFER listing with the character NOTE and annotate
523 ;; the saved message with ARGS.
524 (apply 'mh-exec-cmd "anno" buffer msg args)
526 (cond ((get-buffer buffer) ; Buffer may be deleted
529 (mh-notate-seq msg note (1+ mh-cmd-note))
530 (mh-notate msg note (1+ mh-cmd-note)))))))
533 (defun mh-insert-fields (&rest name-values)
534 ;; Insert the NAME-VALUE pairs in the current buffer.
535 ;; If field NAME exists, append VALUE to it.
536 ;; Do not insert any pairs whose value is the empty string.
537 (let ((case-fold-search t))
539 (let ((field-name (car name-values))
540 (value (car (cdr name-values))))
541 (cond ((equal value "")
543 ((mh-position-on-field field-name)
546 (insert field-name " " value "\n")))
547 (setq name-values (cdr (cdr name-values)))))))
550 (defun mh-position-on-field (field &optional ignore)
551 ;; Move to the end of the FIELD in the header.
552 ;; Move to end of entire header if FIELD not found.
553 ;; Returns non-nil iff FIELD was found.
554 ;; The optional second arg is for pre-version 4 compatibility.
555 (if (mh-goto-header-field field)
557 (mh-header-field-end)
561 (defun mh-get-header-field (field)
562 ;; Find and return the body of FIELD in the mail header.
563 ;; Returns the empty string if the field is not in the header of the
565 (if (mh-goto-header-field field)
567 (skip-chars-forward " \t") ;strip leading white space in body
568 (let ((start (point)))
569 (mh-header-field-end)
570 (buffer-substring start (point))))
573 (fset 'mh-get-field 'mh-get-header-field) ;mh-e 4 compatibility
575 (defun mh-goto-header-field (field)
576 ;; Move to FIELD in the message header.
577 ;; Move to the end of the FIELD name, which should end in a colon.
578 ;; Returns T if found, NIL if not.
579 (goto-char (point-min))
580 (let ((case-fold-search t)
581 (headers-end (save-excursion
582 (mh-goto-header-end 0)
584 (re-search-forward (format "^%s" field) headers-end t)))
586 (defun mh-header-field-end ()
587 ;; Move to the end of the current header field.
588 ;; Handles RFC 822 continuation lines.
590 (while (looking-at "^[ \t]")
592 (backward-char 1)) ;to end of previous line
595 (defun mh-goto-header-end (arg)
596 ;; Find the end of the message header in the current buffer and position
597 ;; the cursor at the ARG'th newline after the header.
598 (if (re-search-forward "^-*$" nil nil)
602 (defun mh-read-address (prompt)
603 ;; Read a To: or Cc: address, prompting in the minibuffer with PROMPT.
604 ;; May someday do completion on aliases.
605 (read-string prompt))
609 ;;; Mode for composing and sending a draft message.
611 (defvar mh-sent-from-folder nil) ;Folder of msg assoc with this letter.
613 (defvar mh-sent-from-msg nil) ;Number of msg assoc with this letter.
615 (defvar mh-send-args nil) ;Extra args to pass to "send" command.
617 (defvar mh-annotate-char nil) ;Character to use to annotate mh-sent-from-msg.
619 (defvar mh-annotate-field nil) ;Field name for message annotation.
621 (put 'mh-letter-mode 'mode-class 'special)
624 (defun mh-letter-mode ()
625 "Mode for composing letters in mh-e.\\<mh-letter-mode-map>
626 When you have finished composing, type \\[mh-send-letter] to send the message
627 using the MH mail handling system.
628 See the documentation for \\[mh-edit-mhn] for information on composing MIME
631 \\{mh-letter-mode-map}
633 Variables controlling this mode (defaults in parentheses):
635 mh-delete-yanked-msg-window (nil)
636 If non-nil, \\[mh-yank-cur-msg] will delete any windows displaying
639 mh-yank-from-start-of-msg (t)
640 If non-nil, \\[mh-yank-cur-msg] will include the entire message.
641 If `body', just yank the body (no header).
642 If nil, only the portion of the message following the point will be yanked.
643 If there is a region, this variable is ignored.
645 mh-ins-buf-prefix (\"> \")
646 String to insert before each non-blank line of a message as it is
647 inserted in a draft letter.
649 mh-signature-file-name (\"~/.signature\")
650 File to be inserted into message by \\[mh-insert-signature].
652 Upon invoking mh-letter-mode, text-mode-hook and mh-letter-mode-hook are
653 invoked with no args, if those values are non-nil."
656 (or mh-user-path (mh-find-path))
657 (make-local-variable 'paragraph-start)
658 (setq paragraph-start (concat "^[ \t]*[-_][-_][-_]+$\\|" paragraph-start))
659 (make-local-variable 'paragraph-separate)
660 (setq paragraph-separate
661 (concat "^[ \t]*[-_][-_][-_]+$\\|" paragraph-separate))
662 (make-local-variable 'mh-send-args)
663 (make-local-variable 'mh-annotate-char)
664 (make-local-variable 'mh-annotate-field)
665 (make-local-variable 'mh-previous-window-config)
666 (make-local-variable 'mh-sent-from-folder)
667 (make-local-variable 'mh-sent-from-msg)
668 (make-local-variable 'mail-header-separator)
669 (setq mail-header-separator "--------") ;for Hyperbole
670 (use-local-map mh-letter-mode-map)
671 (setq major-mode 'mh-letter-mode)
672 (mh-set-mode-name "MH-Letter")
673 (set-syntax-table mh-letter-mode-syntax-table)
674 (run-hooks 'text-mode-hook)
675 ;; if text-mode-hook turned on auto-fill, tune it for messages
676 (cond ((and (boundp 'auto-fill-hook) auto-fill-hook) ;emacs 18
677 (make-local-variable 'auto-fill-hook)
678 (setq auto-fill-hook 'mh-auto-fill-for-letter)))
679 (cond ((and (boundp 'auto-fill-function) auto-fill-function) ;emacs 19
680 (make-local-variable 'auto-fill-function)
681 (setq auto-fill-function 'mh-auto-fill-for-letter)))
682 (run-hooks 'mh-letter-mode-hook))
685 (defun mh-auto-fill-for-letter ()
686 ;; Auto-fill in letters treats the header specially by inserting a tab
687 ;; before continuation line.
689 (let ((fill-prefix "\t"))
694 (defun mh-in-header-p ()
695 ;; Return non-nil if the point is in the header of a draft message.
697 (let ((cur-point (point)))
698 (goto-char (point-min))
699 (re-search-forward "^-*$" nil t)
700 (< cur-point (point)))))
703 (defun mh-to-field ()
704 "Move point to the end of a specified header field.
705 The field is indicated by the previous keystroke (the last keystroke
706 of the command) according to the list in the variable mh-to-field-choices.
707 Create the field if it does not exist. Set the mark to point before moving."
710 (let ((target (cdr (or (assoc (char-to-string (logior last-input-char ?`))
712 ;; also look for a char for version 4 compat
713 (assoc (logior last-input-char ?`) mh-to-field-choices))))
714 (case-fold-search t))
716 (cond ((mh-position-on-field target)
718 (skip-chars-backward " \t")
719 (delete-region (point) eol))
720 (if (and (not (eq (logior last-input-char ?`) ?s))
723 (not (looking-at "[:,]"))))
727 (if (mh-position-on-field "To:")
729 (insert (format "%s \n" target))
730 (backward-char 1)))))
733 (defun mh-to-fcc (&optional folder)
734 "Insert an Fcc: FOLDER field in the current message.
735 Prompt for the field name with a completion list of the current folders."
738 (setq folder (mh-prompt-for-folder
740 (or (and mh-default-folder-for-message-function
742 (goto-char (point-min))
743 (funcall mh-default-folder-for-message-function)))
746 (let ((last-input-char ?\C-f))
750 (insert (if (mh-folder-name-p folder)
755 (defun mh-insert-signature ()
756 "Insert the file named by mh-signature-file-name at the current point."
758 (insert-file-contents mh-signature-file-name)
759 (set-buffer-modified-p (buffer-modified-p))) ; force mode line update
762 (defun mh-check-whom ()
763 "Verify recipients of the current letter, showing expansion of any aliases."
765 (let ((file-name buffer-file-name))
767 (message "Checking recipients...")
768 (mh-in-show-buffer ("*Recipients*")
769 (bury-buffer (current-buffer))
771 (mh-exec-cmd-output "whom" t file-name))
772 (message "Checking recipients...done")))
776 ;;; Routines to compose and send a letter.
778 (defun mh-compose-and-send-mail (draft send-args
779 sent-from-folder sent-from-msg
781 annotate-char annotate-field
783 ;; Edit and compose a draft message in buffer DRAFT and send or save it.
784 ;; SENT-FROM-FOLDER is buffer containing scan listing of current folder, or
785 ;; nil if none exists.
786 ;; SENT-FROM-MSG is the message number or sequence name or nil.
787 ;; SEND-ARGS is an optional argument passed to the send command.
788 ;; The TO, SUBJECT, and CC fields are passed to the
789 ;; mh-compose-letter-function.
790 ;; If ANNOTATE-CHAR is non-null, it is used to notate the scan listing of the
791 ;; message. In that case, the ANNOTATE-FIELD is used to build a string
792 ;; for mh-annotate-msg.
793 ;; CONFIG is the window configuration to restore after sending the letter.
794 (pop-to-buffer draft)
796 (setq mh-sent-from-folder sent-from-folder)
797 (setq mh-sent-from-msg sent-from-msg)
798 (setq mh-send-args send-args)
799 (setq mh-annotate-char annotate-char)
800 (setq mh-annotate-field annotate-field)
801 (setq mh-previous-window-config config)
802 (setq mode-line-buffer-identification (list "{%b}"))
803 (if (and (boundp 'mh-compose-letter-function)
804 mh-compose-letter-function)
805 ;; run-hooks will not pass arguments.
806 (let ((value mh-compose-letter-function))
807 (if (and (listp value) (not (eq (car value) 'lambda)))
809 (funcall (car value) to subject cc)
810 (setq value (cdr value)))
811 (funcall mh-compose-letter-function to subject cc)))))
814 (defun mh-send-letter (&optional arg)
815 "Send the draft letter in the current buffer.
816 If optional prefix argument is provided, monitor delivery.
817 Run mh-before-send-letter-hook before doing anything."
819 (run-hooks 'mh-before-send-letter-hook)
820 (set-buffer-modified-p t) ; Make sure buffer is written
822 (message "Sending...")
823 (let ((draft-buffer (current-buffer))
824 (file-name buffer-file-name)
825 (config mh-previous-window-config))
827 (pop-to-buffer "MH mail delivery")
829 (mh-exec-cmd-output mh-send-prog t "-watch" "-nopush"
830 "-nodraftfolder" mh-send-args file-name)
831 (goto-char (point-max)) ; show the interesting part
833 (set-buffer draft-buffer)) ; for annotation below
835 (mh-exec-cmd-daemon mh-send-prog "-nodraftfolder" "-noverbose"
836 mh-send-args file-name)))
838 (mh-annotate-msg mh-sent-from-msg
841 "-component" mh-annotate-field
842 "-text" (format "\"%s %s\""
843 (mh-get-header-field "To:")
844 (mh-get-header-field "Cc:"))))
847 (y-or-n-p "Kill draft buffer? "))
848 (kill-buffer draft-buffer)
850 (set-window-configuration config))))
852 (message "Sending...done")
853 (message "Sending...backgrounded"))))
856 (defun mh-insert-letter (folder message verbatim)
857 "Insert a message into the current letter.
858 Removes the message's headers using mh-invisible-headers. Prefixes
859 each non-blank line with mh-ins-buf-prefix. Prompts for FOLDER and
860 MESSAGE. If prefix argument VERBATIM provided, do not indent and do
861 not delete headers. Leaves the mark before the letter and point after it."
863 (list (mh-prompt-for-folder "Message from" mh-sent-from-folder nil)
864 (read-input (format "Message number%s: "
866 (format " [%d]" mh-sent-from-msg)
870 (narrow-to-region (point) (point))
871 (let ((start (point-min)))
872 (if (equal message "") (setq message (int-to-string mh-sent-from-msg)))
873 (mh-exec-lib-cmd-output "mhl" "-nobell" "-noclear"
874 (expand-file-name message
875 (mh-expand-file-name folder)))
876 (cond ((not verbatim)
877 (mh-clean-msg-header start mh-invisible-headers mh-visible-headers)
878 (set-mark start) ; since mh-clean-msg-header moves it
879 (mh-insert-prefix-string mh-ins-buf-prefix))))))
882 (defun mh-yank-cur-msg ()
883 "Insert the current message into the draft buffer.
884 Prefix each non-blank line in the message with the string in
885 `mh-ins-buf-prefix'. If a region is set in the message's buffer, then
886 only the region will be inserted. Otherwise, the entire message will
887 be inserted if `mh-yank-from-start-of-msg' is non-nil. If this variable
888 is nil, the portion of the message following the point will be yanked.
889 If `mh-delete-yanked-msg-window' is non-nil, any window displaying the
890 yanked message will be deleted."
892 (if (and mh-sent-from-folder mh-sent-from-msg)
893 (let ((to-point (point))
894 (to-buffer (current-buffer)))
895 (set-buffer mh-sent-from-folder)
896 (if mh-delete-yanked-msg-window
897 (delete-windows-on mh-show-buffer))
898 (set-buffer mh-show-buffer) ; Find displayed message
899 (let ((mh-ins-str (cond ((if (boundp 'mark-active)
900 mark-active ;Emacs 19
902 (buffer-substring (region-beginning)
904 ((eq 'body mh-yank-from-start-of-msg)
907 (goto-char (point-min))
908 (mh-goto-header-end 1)
911 (mh-yank-from-start-of-msg
912 (buffer-substring (point-min) (point-max)))
914 (buffer-substring (point) (point-max))))))
915 (set-buffer to-buffer)
917 (narrow-to-region to-point to-point)
920 (mh-insert-prefix-string mh-ins-buf-prefix)
922 (error "There is no current message")))
925 (defun mh-insert-prefix-string (mh-ins-string)
926 ;; Run mail-citation-hook to insert a prefix string before each line
927 ;; in the buffer. Generality for supercite users.
928 (set-mark (point-max))
929 (goto-char (point-min))
930 (cond (mail-citation-hook
931 (run-hooks 'mail-citation-hook))
932 (mh-yank-hooks ;old hook name
933 (run-hooks 'mh-yank-hooks))
935 (or (bolp) (forward-line 1))
936 (let ((zmacs-regions nil)) ;so "(mark)" works in XEmacs
937 (while (< (point) (mark))
938 (insert mh-ins-string)
939 (forward-line 1))))))
942 (defun mh-fully-kill-draft ()
943 "Kill the draft message file and the draft message buffer.
944 Use \\[kill-buffer] if you don't want to delete the draft message file."
946 (if (y-or-n-p "Kill draft message? ")
947 (let ((config mh-previous-window-config))
948 (if (file-exists-p buffer-file-name)
949 (delete-file buffer-file-name))
950 (set-buffer-modified-p nil)
951 (kill-buffer (buffer-name))
954 (set-window-configuration config)))
955 (error "Message not killed")))
958 ;;; Build the letter-mode keymap:
960 (define-key mh-letter-mode-map "\C-c\C-f\C-b" 'mh-to-field)
961 (define-key mh-letter-mode-map "\C-c\C-f\C-c" 'mh-to-field)
962 (define-key mh-letter-mode-map "\C-c\C-f\C-d" 'mh-to-field)
963 (define-key mh-letter-mode-map "\C-c\C-f\C-f" 'mh-to-fcc)
964 (define-key mh-letter-mode-map "\C-c\C-f\C-r" 'mh-to-field)
965 (define-key mh-letter-mode-map "\C-c\C-f\C-s" 'mh-to-field)
966 (define-key mh-letter-mode-map "\C-c\C-f\C-t" 'mh-to-field)
967 (define-key mh-letter-mode-map "\C-c\C-fb" 'mh-to-field)
968 (define-key mh-letter-mode-map "\C-c\C-fc" 'mh-to-field)
969 (define-key mh-letter-mode-map "\C-c\C-fd" 'mh-to-field)
970 (define-key mh-letter-mode-map "\C-c\C-ff" 'mh-to-fcc)
971 (define-key mh-letter-mode-map "\C-c\C-fr" 'mh-to-field)
972 (define-key mh-letter-mode-map "\C-c\C-fs" 'mh-to-field)
973 (define-key mh-letter-mode-map "\C-c\C-ft" 'mh-to-field)
974 (define-key mh-letter-mode-map "\C-c\C-i" 'mh-insert-letter)
975 (define-key mh-letter-mode-map "\C-c\C-q" 'mh-fully-kill-draft)
976 (define-key mh-letter-mode-map "\C-c\C-\\" 'mh-fully-kill-draft) ;if no C-q
977 (define-key mh-letter-mode-map "\C-c\C-s" 'mh-insert-signature)
978 (define-key mh-letter-mode-map "\C-c\C-^" 'mh-insert-signature) ;if no C-s
979 (define-key mh-letter-mode-map "\C-c\C-w" 'mh-check-whom)
980 (define-key mh-letter-mode-map "\C-c\C-y" 'mh-yank-cur-msg)
981 (define-key mh-letter-mode-map "\C-c\C-c" 'mh-send-letter)
982 (define-key mh-letter-mode-map "\C-c\C-m\C-f" 'mh-mhn-compose-forw)
983 (define-key mh-letter-mode-map "\C-c\C-m\C-e" 'mh-mhn-compose-anon-ftp)
984 (define-key mh-letter-mode-map "\C-c\C-m\C-t" 'mh-mhn-compose-external-compressed-tar)
985 (define-key mh-letter-mode-map "\C-c\C-m\C-i" 'mh-mhn-compose-insertion)
986 (define-key mh-letter-mode-map "\C-c\C-e" 'mh-edit-mhn)
987 (define-key mh-letter-mode-map "\C-c\C-m\C-u" 'mh-revert-mhn-edit)
990 ;;; autoloads from mh-mime
992 (autoload 'mh-mhn-compose-insertion "mh-mime"
993 "Add a directive to insert a MIME message part from a file.
994 This is the typical way to insert non-text parts in a message.
995 See also \\[mh-edit-mhn]." t)
997 (autoload 'mh-mhn-compose-anon-ftp "mh-mime"
998 "Add a directive for a MIME anonymous ftp external body part.
999 This directive tells MH to include a reference to a
1000 message/external-body part retrievable by anonymous FTP.
1001 See also \\[mh-edit-mhn]." t)
1003 (autoload 'mh-mhn-compose-external-compressed-tar "mh-mime"
1004 "Add a directive to include a MIME reference to a compressed tar file.
1005 The file should be available via anonymous ftp. This directive
1006 tells MH to include a reference to a message/external-body part.
1007 See also \\[mh-edit-mhn]." t)
1009 (autoload 'mh-mhn-compose-forw "mh-mime"
1010 "Add a forw directive to this message, to forward a message with MIME.
1011 This directive tells MH to include another message in this one.
1012 See also \\[mh-edit-mhn]." t)
1014 (autoload 'mh-edit-mhn "mh-mime"
1015 "Format the current draft for MIME, expanding any mhn directives.
1016 Process the current draft with the mhn program, which,
1017 using directives already inserted in the draft, fills in
1018 all the MIME components and header fields.
1019 This step should be done last just before sending the message.
1020 The mhn program is part of MH version 6.8 or later.
1021 The `\\[mh-revert-mhn-edit]' command undoes this command.
1022 For assistance with creating mhn directives to insert
1023 various types of components in a message, see
1024 \\[mh-mhn-compose-insertion] (generic insertion from a file),
1025 \\[mh-mhn-compose-anon-ftp] (external reference to file via anonymous ftp),
1026 \\[mh-mhn-compose-external-compressed-tar] \
1027 \(reference to compressed tar file via anonymous ftp), and
1028 \\[mh-mhn-compose-forw] (forward message)." t)
1030 (autoload 'mh-revert-mhn-edit "mh-mime"
1031 "Undoes the effect of \\[mh-edit-mhn] by reverting to the backup file.
1032 Optional non-nil argument means don't ask for confirmation." t)