]> code.delx.au - gnu-emacs/blob - lisp/mail/mh-comp.el
(mail-signature): Allow expression as value.
[gnu-emacs] / lisp / mail / mh-comp.el
1 ;;; mh-comp --- mh-e functions for composing messages
2 ;; Time-stamp: <95/08/19 17:48:59 gildea>
3
4 ;; Copyright (C) 1993, 1995, 1997 Free Software Foundation, Inc.
5
6 ;; This file is part of mh-e, part of GNU Emacs.
7
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)
11 ;; any later version.
12
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.
17
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 the
20 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 ;; Boston, MA 02111-1307, USA.
22
23 ;;; Commentary:
24
25 ;; Internal support for mh-e package.
26
27 ;;; Change Log:
28
29 ;; $Id: mh-comp.el,v 1.11 1997/09/15 19:45:16 rms Exp rms $
30
31 ;;; Code:
32
33 (provide 'mh-comp)
34 (require 'mh-utils)
35
36 ;;; Site customization (see also mh-utils.el):
37
38 (defgroup mh-compose nil
39 "Mh-e functions for composing messages"
40 :prefix "mh-"
41 :group 'mh)
42
43
44 (defvar mh-send-prog "send"
45 "Name of the MH send program.
46 Some sites need to change this because of a name conflict.")
47
48 (defvar mh-redist-full-contents nil
49 "Non-nil if the `dist' command needs whole letter for redistribution.
50 This is the case only when `send' is compiled with the BERK option.
51 If MH will not allow you to redist a previously redist'd msg, set to nil.")
52
53
54 (defvar mh-note-repl "-"
55 "String whose first character is used to notate replied to messages.")
56
57 (defvar mh-note-forw "F"
58 "String whose first character is used to notate forwarded messages.")
59
60 (defvar mh-note-dist "R"
61 "String whose first character is used to notate redistributed messages.")
62
63 (defvar mh-yank-hooks nil
64 "Obsolete hook for modifying a citation just inserted in the mail buffer.
65 Each hook function can find the citation between point and mark.
66 And each hook function should leave point and mark around the citation
67 text as modified.
68
69 This is a normal hook, misnamed for historical reasons.
70 It is semi-obsolete and is only used if `mail-citation-hook' is nil.")
71
72 (defvar mail-citation-hook nil
73 "*Hook for modifying a citation just inserted in the mail buffer.
74 Each hook function can find the citation between point and mark.
75 And each hook function should leave point and mark around the citation
76 text as modified.
77
78 If this hook is entirely empty (nil), the text of the message is inserted
79 with `mh-ins-buf-prefix' prefixed to each line.
80
81 See also the variable `mh-yank-from-start-of-msg', which controls how
82 much of the message passed to the hook.")
83
84 ;;; Copied from sendmail.el for Hyperbole
85 (defvar mail-header-separator "--------"
86 "*Line used by MH to separate headers from text in messages being composed.")
87
88 ;;; Personal preferences:
89
90 (defcustom mh-delete-yanked-msg-window nil
91 "*Controls window display when a message is yanked by \\<mh-letter-mode-map>\\[mh-yank-cur-msg].
92 If non-nil, yanking the current message into a draft letter deletes any
93 windows displaying the message."
94 :type 'boolean
95 :group 'mh-compose)
96
97 (defcustom mh-yank-from-start-of-msg t
98 "*Controls which part of a message is yanked by \\<mh-letter-mode-map>\\[mh-yank-cur-msg].
99 If non-nil, include the entire message. If the symbol `body', then yank the
100 message minus the header. If nil, yank only the portion of the message
101 following the point. If the show buffer has a region, this variable is
102 ignored."
103 :type '(choice (const t) (const nil) (const body))
104 :group 'mh-compose)
105
106 (defcustom mh-ins-buf-prefix "> "
107 "*String to put before each non-blank line of a yanked or inserted message.
108 \\<mh-letter-mode-map>Used when the message is inserted into an outgoing letter
109 by \\[mh-insert-letter] or \\[mh-yank-cur-msg]."
110 :type 'string
111 :group 'mh-compose)
112
113 (defcustom mh-reply-default-reply-to nil
114 "*Sets the person or persons to whom a reply will be sent.
115 If nil, prompt for recipient. If non-nil, then \\<mh-folder-mode-map>`\\[mh-reply]' will use this
116 value and it should be one of \"from\", \"to\", \"cc\", or \"all\".
117 The values \"cc\" and \"all\" do the same thing."
118 :type '(choice (const :tag "Prompt" nil)
119 (const "from") (const "to")
120 (const "cc") (const "all"))
121 :group 'mh-compose)
122
123 (defcustom mh-signature-file-name "~/.signature"
124 "*Name of file containing the user's signature.
125 Inserted into message by \\<mh-letter-mode-map>\\[mh-insert-signature]."
126 :type 'file
127 :group 'mh-compose)
128
129 (defcustom mh-forward-subject-format "%s: %s"
130 "*Format to generate the Subject: line contents for a forwarded message.
131 The two string arguments to the format are the sender of the original
132 message and the original subject line."
133 :type 'string
134 :group 'mh-compose)
135
136 (defvar mh-comp-formfile "components"
137 "Name of file to be used as a skeleton for composing messages.
138 Default is \"components\". If not an absolute file name, the file
139 is searched for first in the user's MH directory, then in the
140 system MH lib directory.")
141
142 (defvar mh-repl-formfile "replcomps"
143 "Name of file to be used as a skeleton for replying to messages.
144 Default is \"replcomps\". If not an absolute file name, the file
145 is searched for first in the user's MH directory, then in the
146 system MH lib directory.")
147
148 ;;; Hooks:
149
150 (defcustom mh-letter-mode-hook nil
151 "Invoked in `mh-letter-mode' on a new letter."
152 :type 'hook
153 :group 'mh-compose)
154
155 (defcustom mh-compose-letter-function nil
156 "Invoked when setting up a letter draft.
157 It is passed three arguments: TO recipients, SUBJECT, and CC recipients."
158 :type 'function
159 :group 'mh-compose)
160
161 (defcustom mh-before-send-letter-hook nil
162 "Invoked at the beginning of the \\<mh-letter-mode-map>\\[mh-send-letter] command."
163 :type 'hook
164 :group 'mh-compose)
165
166
167 (defvar mh-rejected-letter-start
168 (concat "^ ----- Unsent message follows -----$" ;from sendmail V5
169 "\\|^ ----- Original message follows -----$" ;from sendmail V8
170 "\\|^------- Unsent Draft$" ;from MH itself
171 "\\|^---------- Original Message ----------$" ;from zmailer
172 "\\|^ --- The unsent message follows ---$" ;from AIX mail system
173 "\\|^ Your message follows:$" ;from MMDF-II
174 "\\|^Content-Description: Returned Content$" ;1993 KJ sendmail
175 )
176 "Regexp specifying the beginning of the wrapper around a returned letter.
177 This wrapper is generated by the mail system when rejecting a letter.")
178
179 (defvar mh-new-draft-cleaned-headers
180 "^Date:\\|^Received:\\|^Message-Id:\\|^From:\\|^Sender:\\|^Errors-To:\\|^Delivery-Date:\\|^Return-Path:"
181 "Regexp of header lines to remove before offering a message as a new draft.
182 Used by the \\<mh-folder-mode-map>`\\[mh-edit-again]' and `\\[mh-extract-rejected-mail]' commands.")
183
184 (defvar mh-to-field-choices '(("t" . "To:") ("s" . "Subject:") ("c" . "Cc:")
185 ("b" . "Bcc:") ("f" . "Fcc:") ("r" . "From:")
186 ("d" . "Dcc:"))
187 "Alist of (final-character . field-name) choices for mh-to-field.")
188
189 (defvar mh-letter-mode-map (copy-keymap text-mode-map)
190 "Keymap for composing mail.")
191
192 (defvar mh-letter-mode-syntax-table nil
193 "Syntax table used by mh-e while in MH-Letter mode.")
194
195 (if mh-letter-mode-syntax-table
196 ()
197 (setq mh-letter-mode-syntax-table
198 (make-syntax-table text-mode-syntax-table))
199 (modify-syntax-entry ?% "." mh-letter-mode-syntax-table))
200
201
202 ;;;###autoload
203 (defun mh-smail ()
204 "Compose and send mail with the MH mail system.
205 This function is an entry point to mh-e, the Emacs front end
206 to the MH mail system.
207
208 See documentation of `\\[mh-send]' for more details on composing mail."
209 (interactive)
210 (mh-find-path)
211 (call-interactively 'mh-send))
212
213
214 (defvar mh-error-if-no-draft nil) ;raise error over using old draft
215
216
217 ;;;###autoload
218 (defun mh-smail-batch (&rest ignored)
219 "Set up a mail composition draft with the MH mail system.
220 This function is an entry point to mh-e, the Emacs front end
221 to the MH mail system. This function does not prompt the user
222 for any header fields, and thus is suitable for use by programs
223 that want to create a mail buffer.
224 Users should use `\\[mh-smail]' to compose mail."
225 (mh-find-path)
226 (let ((mh-error-if-no-draft t))
227 (mh-send "" "" "")))
228
229
230 (defun mh-edit-again (msg)
231 "Clean-up a draft or a message previously sent and make it resendable.
232 Default is the current message.
233 The variable mh-new-draft-cleaned-headers specifies the headers to remove.
234 See also documentation for `\\[mh-send]' function."
235 (interactive (list (mh-get-msg-num t)))
236 (let* ((from-folder mh-current-folder)
237 (config (current-window-configuration))
238 (draft
239 (cond ((and mh-draft-folder (equal from-folder mh-draft-folder))
240 (pop-to-buffer (find-file-noselect (mh-msg-filename msg)) t)
241 (rename-buffer (format "draft-%d" msg))
242 (buffer-name))
243 (t
244 (mh-read-draft "clean-up" (mh-msg-filename msg) nil)))))
245 (mh-clean-msg-header (point-min) mh-new-draft-cleaned-headers nil)
246 (goto-char (point-min))
247 (save-buffer)
248 (mh-compose-and-send-mail draft "" from-folder nil nil nil nil nil nil
249 config)))
250
251
252 (defun mh-extract-rejected-mail (msg)
253 "Extract a letter returned by the mail system and make it resendable.
254 Default is the current message. The variable `mh-new-draft-cleaned-headers'
255 gives the headers to clean out of the original message.
256 See also documentation for `\\[mh-send]' function."
257 (interactive (list (mh-get-msg-num t)))
258 (let ((from-folder mh-current-folder)
259 (config (current-window-configuration))
260 (draft (mh-read-draft "extraction" (mh-msg-filename msg) nil)))
261 (goto-char (point-min))
262 (cond ((re-search-forward mh-rejected-letter-start nil t)
263 (skip-chars-forward " \t\n")
264 (delete-region (point-min) (point))
265 (mh-clean-msg-header (point-min) mh-new-draft-cleaned-headers nil))
266 (t
267 (message "Does not appear to be a rejected letter.")))
268 (goto-char (point-min))
269 (save-buffer)
270 (mh-compose-and-send-mail draft "" from-folder msg
271 (mh-get-header-field "To:")
272 (mh-get-header-field "From:")
273 (mh-get-header-field "Cc:")
274 nil nil config)))
275
276
277 (defun mh-forward (to cc &optional msg-or-seq)
278 "Forward a message or message sequence. Defaults to displayed message.
279 If optional prefix argument provided, then prompt for the message sequence.
280 See also documentation for `\\[mh-send]' function."
281 (interactive (list (mh-read-address "To: ")
282 (mh-read-address "Cc: ")
283 (if current-prefix-arg
284 (mh-read-seq-default "Forward" t)
285 (mh-get-msg-num t))))
286 (or msg-or-seq
287 (setq msg-or-seq (mh-get-msg-num t)))
288 (let* ((folder mh-current-folder)
289 (config (current-window-configuration))
290 ;; forw always leaves file in "draft" since it doesn't have -draft
291 (draft-name (expand-file-name "draft" mh-user-path))
292 (draft (cond ((or (not (file-exists-p draft-name))
293 (y-or-n-p "The file 'draft' exists. Discard it? "))
294 (mh-exec-cmd "forw" "-build"
295 mh-current-folder msg-or-seq)
296 (prog1
297 (mh-read-draft "" draft-name t)
298 (mh-insert-fields "To:" to "Cc:" cc)
299 (save-buffer)))
300 (t
301 (mh-read-draft "" draft-name nil)))))
302 (let (orig-from
303 orig-subject)
304 (goto-char (point-min))
305 (re-search-forward "^------- Forwarded Message")
306 (forward-line 1)
307 (skip-chars-forward " \t\n")
308 (save-restriction
309 (narrow-to-region (point) (point-max))
310 (setq orig-from (mh-get-header-field "From:"))
311 (setq orig-subject (mh-get-header-field "Subject:")))
312 (let ((forw-subject
313 (mh-forwarded-letter-subject orig-from orig-subject)))
314 (mh-insert-fields "Subject:" forw-subject)
315 (goto-char (point-min))
316 (re-search-forward "^------- Forwarded Message")
317 (forward-line -1)
318 (delete-other-windows)
319 (if (numberp msg-or-seq)
320 (mh-add-msgs-to-seq msg-or-seq 'forwarded t)
321 (mh-add-msgs-to-seq (mh-seq-to-msgs msg-or-seq) 'forwarded t))
322 (mh-compose-and-send-mail draft "" folder msg-or-seq
323 to forw-subject cc
324 mh-note-forw "Forwarded:"
325 config)))))
326
327 (defun mh-forwarded-letter-subject (from subject)
328 ;; Return a Subject suitable for a forwarded message.
329 ;; Original message has headers FROM and SUBJECT.
330 (let ((addr-start (string-match "<" from))
331 (comment (string-match "(" from)))
332 (cond ((and addr-start (> addr-start 0))
333 ;; Full Name <luser@host>
334 (setq from (substring from 0 (1- addr-start))))
335 (comment
336 ;; luser@host (Full Name)
337 (setq from (substring from (1+ comment) (1- (length from)))))))
338 (format mh-forward-subject-format from subject))
339
340
341 ;;;###autoload
342 (defun mh-smail-other-window ()
343 "Compose and send mail in other window with the MH mail system.
344 This function is an entry point to mh-e, the Emacs front end
345 to the MH mail system.
346
347 See documentation of `\\[mh-send]' for more details on composing mail."
348 (interactive)
349 (mh-find-path)
350 (call-interactively 'mh-send-other-window))
351
352
353 (defun mh-redistribute (to cc &optional msg)
354 "Redistribute a letter.
355 Depending on how your copy of MH was compiled, you may need to change the
356 setting of the variable `mh-redist-full-contents'. See its documentation."
357 (interactive (list (mh-read-address "Redist-To: ")
358 (mh-read-address "Redist-Cc: ")
359 (mh-get-msg-num t)))
360 (or msg
361 (setq msg (mh-get-msg-num t)))
362 (save-window-excursion
363 (let ((folder mh-current-folder)
364 (draft (mh-read-draft "redistribution"
365 (if mh-redist-full-contents
366 (mh-msg-filename msg)
367 nil)
368 nil)))
369 (mh-goto-header-end 0)
370 (insert "Resent-To: " to "\n")
371 (if (not (equal cc "")) (insert "Resent-cc: " cc "\n"))
372 (mh-clean-msg-header (point-min)
373 "^Message-Id:\\|^Received:\\|^Return-Path:\\|^Sender:\\|^Date:\\|^From:"
374 nil)
375 (save-buffer)
376 (message "Redistributing...")
377 (if mh-redist-full-contents
378 (call-process "/bin/sh" nil 0 nil "-c"
379 (format "mhdist=1 mhaltmsg=%s %s -push %s"
380 buffer-file-name
381 (expand-file-name mh-send-prog mh-progs)
382 buffer-file-name))
383 (call-process "/bin/sh" nil 0 nil "-c"
384 (format "mhdist=1 mhaltmsg=%s mhannotate=1 %s -push %s"
385 (mh-msg-filename msg folder)
386 (expand-file-name mh-send-prog mh-progs)
387 buffer-file-name)))
388 (mh-annotate-msg msg folder mh-note-dist
389 "-component" "Resent:"
390 "-text" (format "\"%s %s\"" to cc))
391 (kill-buffer draft)
392 (message "Redistributing...done"))))
393
394
395 (defun mh-reply (message &optional includep)
396 "Reply to MESSAGE (default: current message).
397 If optional prefix argument INCLUDEP provided, then include the message
398 in the reply using filter `mhl.reply' in your MH directory.
399 Prompts for type of addresses to reply to:
400 from sender only,
401 to sender and primary recipients,
402 cc/all sender and all recipients.
403 If the file named by `mh-repl-formfile' exists, it is used as a skeleton
404 for the reply. See also documentation for `\\[mh-send]' function."
405 (interactive (list (mh-get-msg-num t) current-prefix-arg))
406 (let ((minibuffer-help-form
407 "from => Sender only\nto => Sender and primary recipients\ncc or all => Sender and all recipients"))
408 (let ((reply-to (or mh-reply-default-reply-to
409 (completing-read "Reply to whom: "
410 '(("from") ("to") ("cc") ("all"))
411 nil
412 t)))
413 (folder mh-current-folder)
414 (show-buffer mh-show-buffer)
415 (config (current-window-configuration)))
416 (message "Composing a reply...")
417 (mh-exec-cmd "repl" "-build" "-noquery" "-nodraftfolder"
418 (if (stringp mh-repl-formfile) ;must be string, but we're paranoid
419 (list "-form" mh-repl-formfile))
420 mh-current-folder message
421 (cond ((or (equal reply-to "from") (equal reply-to ""))
422 '("-nocc" "all"))
423 ((equal reply-to "to")
424 '("-cc" "to"))
425 ((or (equal reply-to "cc") (equal reply-to "all"))
426 '("-cc" "all" "-nocc" "me")))
427 (if includep
428 '("-filter" "mhl.reply")))
429 (let ((draft (mh-read-draft "reply"
430 (expand-file-name "reply" mh-user-path)
431 t)))
432 (delete-other-windows)
433 (save-buffer)
434
435 (let ((to (mh-get-header-field "To:"))
436 (subject (mh-get-header-field "Subject:"))
437 (cc (mh-get-header-field "Cc:")))
438 (goto-char (point-min))
439 (mh-goto-header-end 1)
440 (or includep
441 (mh-in-show-buffer (show-buffer)
442 (mh-display-msg message folder)))
443 (mh-add-msgs-to-seq message 'answered t)
444 (message "Composing a reply...done")
445 (mh-compose-and-send-mail draft "" folder message to subject cc
446 mh-note-repl "Replied:" config))))))
447
448
449 (defun mh-send (to cc subject)
450 "Compose and send a letter.
451 The file named by `mh-comp-formfile' will be used as the form.
452 Do not call this function from outside mh-e; use \\[mh-smail] instead.
453
454 The letter is composed in mh-letter-mode; see its documentation for more
455 details. If `mh-compose-letter-function' is defined, it is called on the
456 draft and passed three arguments: TO, SUBJECT, and CC."
457 (interactive (list
458 (mh-read-address "To: ")
459 (mh-read-address "Cc: ")
460 (read-string "Subject: ")))
461 (let ((config (current-window-configuration)))
462 (delete-other-windows)
463 (mh-send-sub to cc subject config)))
464
465
466 (defun mh-send-other-window (to cc subject)
467 "Compose and send a letter in another window.
468 Do not call this function from outside mh-e;
469 use \\[mh-smail-other-window] instead.
470 See also documentation for `\\[mh-send]' function."
471 (interactive (list
472 (mh-read-address "To: ")
473 (mh-read-address "Cc: ")
474 (read-string "Subject: ")))
475 (let ((pop-up-windows t))
476 (mh-send-sub to cc subject (current-window-configuration))))
477
478
479 (defun mh-send-sub (to cc subject config)
480 ;; Do the real work of composing and sending a letter.
481 ;; Expects the TO, CC, and SUBJECT fields as arguments.
482 ;; CONFIG is the window configuration before sending mail.
483 (let ((folder mh-current-folder)
484 (msg-num (mh-get-msg-num nil)))
485 (message "Composing a message...")
486 (let ((draft (mh-read-draft
487 "message"
488 (let (components)
489 (cond
490 ((file-exists-p
491 (setq components
492 (expand-file-name mh-comp-formfile mh-user-path)))
493 components)
494 ((file-exists-p
495 (setq components
496 (expand-file-name mh-comp-formfile mh-lib)))
497 components)
498 (t
499 (error (format "Can't find components file \"%s\""
500 components)))))
501 nil)))
502 (mh-insert-fields "To:" to "Subject:" subject "Cc:" cc)
503 (goto-char (point-max))
504 (message "Composing a message...done")
505 (mh-compose-and-send-mail draft "" folder msg-num
506 to subject cc
507 nil nil config))))
508
509
510 (defun mh-read-draft (use initial-contents delete-contents-file)
511 ;; Read draft file into a draft buffer and make that buffer the current one.
512 ;; USE is a message used for prompting about the intended use of the message.
513 ;; INITIAL-CONTENTS is filename that is read into an empty buffer, or NIL
514 ;; if buffer should not be modified. Delete the initial-contents file if
515 ;; DELETE-CONTENTS-FILE flag is set.
516 ;; Returns the draft folder's name.
517 ;; If the draft folder facility is enabled in ~/.mh_profile, a new buffer is
518 ;; used each time and saved in the draft folder. The draft file can then be
519 ;; reused.
520 (cond (mh-draft-folder
521 (let ((orig-default-dir default-directory)
522 (draft-file-name (mh-new-draft-name)))
523 (pop-to-buffer (generate-new-buffer
524 (format "draft-%s"
525 (file-name-nondirectory draft-file-name))))
526 (condition-case ()
527 (insert-file-contents draft-file-name t)
528 (file-error))
529 (setq default-directory orig-default-dir)))
530 (t
531 (let ((draft-name (expand-file-name "draft" mh-user-path)))
532 (pop-to-buffer "draft") ; Create if necessary
533 (if (buffer-modified-p)
534 (if (y-or-n-p "Draft has been modified; kill anyway? ")
535 (set-buffer-modified-p nil)
536 (error "Draft preserved")))
537 (setq buffer-file-name draft-name)
538 (clear-visited-file-modtime)
539 (unlock-buffer)
540 (cond ((and (file-exists-p draft-name)
541 (not (equal draft-name initial-contents)))
542 (insert-file-contents draft-name)
543 (delete-file draft-name))))))
544 (cond ((and initial-contents
545 (or (zerop (buffer-size))
546 (if (y-or-n-p
547 (format "A draft exists. Use for %s? " use))
548 (if mh-error-if-no-draft
549 (error "A prior draft exists."))
550 t)))
551 (erase-buffer)
552 (insert-file-contents initial-contents)
553 (if delete-contents-file (delete-file initial-contents))))
554 (auto-save-mode 1)
555 (if mh-draft-folder
556 (save-buffer)) ; Do not reuse draft name
557 (buffer-name))
558
559
560 (defun mh-new-draft-name ()
561 ;; Returns the pathname of folder for draft messages.
562 (save-excursion
563 (mh-exec-cmd-quiet t "mhpath" mh-draft-folder "new")
564 (buffer-substring (point-min) (1- (point-max)))))
565
566
567 (defun mh-annotate-msg (msg buffer note &rest args)
568 ;; Mark the MESSAGE in BUFFER listing with the character NOTE and annotate
569 ;; the saved message with ARGS.
570 (apply 'mh-exec-cmd "anno" buffer msg args)
571 (save-excursion
572 (cond ((get-buffer buffer) ; Buffer may be deleted
573 (set-buffer buffer)
574 (if (symbolp msg)
575 (mh-notate-seq msg note (1+ mh-cmd-note))
576 (mh-notate msg note (1+ mh-cmd-note)))))))
577
578
579 (defun mh-insert-fields (&rest name-values)
580 ;; Insert the NAME-VALUE pairs in the current buffer.
581 ;; If field NAME exists, append VALUE to it.
582 ;; Do not insert any pairs whose value is the empty string.
583 (let ((case-fold-search t))
584 (while name-values
585 (let ((field-name (car name-values))
586 (value (car (cdr name-values))))
587 (cond ((equal value "")
588 nil)
589 ((mh-position-on-field field-name)
590 (insert " " value))
591 (t
592 (insert field-name " " value "\n")))
593 (setq name-values (cdr (cdr name-values)))))))
594
595
596 (defun mh-position-on-field (field &optional ignore)
597 ;; Move to the end of the FIELD in the header.
598 ;; Move to end of entire header if FIELD not found.
599 ;; Returns non-nil iff FIELD was found.
600 ;; The optional second arg is for pre-version 4 compatibility.
601 (if (mh-goto-header-field field)
602 (progn
603 (mh-header-field-end)
604 t)))
605
606
607 (defun mh-get-header-field (field)
608 ;; Find and return the body of FIELD in the mail header.
609 ;; Returns the empty string if the field is not in the header of the
610 ;; current buffer.
611 (if (mh-goto-header-field field)
612 (progn
613 (skip-chars-forward " \t") ;strip leading white space in body
614 (let ((start (point)))
615 (mh-header-field-end)
616 (buffer-substring start (point))))
617 ""))
618
619 (fset 'mh-get-field 'mh-get-header-field) ;mh-e 4 compatibility
620
621 (defun mh-goto-header-field (field)
622 ;; Move to FIELD in the message header.
623 ;; Move to the end of the FIELD name, which should end in a colon.
624 ;; Returns T if found, NIL if not.
625 (goto-char (point-min))
626 (let ((case-fold-search t)
627 (headers-end (save-excursion
628 (mh-goto-header-end 0)
629 (point))))
630 (re-search-forward (format "^%s" field) headers-end t)))
631
632 (defun mh-header-field-end ()
633 ;; Move to the end of the current header field.
634 ;; Handles RFC 822 continuation lines.
635 (forward-line 1)
636 (while (looking-at "^[ \t]")
637 (forward-line 1))
638 (backward-char 1)) ;to end of previous line
639
640
641 (defun mh-goto-header-end (arg)
642 ;; Find the end of the message header in the current buffer and position
643 ;; the cursor at the ARG'th newline after the header.
644 (if (re-search-forward "^-*$" nil nil)
645 (forward-line arg)))
646
647
648 (defun mh-read-address (prompt)
649 ;; Read a To: or Cc: address, prompting in the minibuffer with PROMPT.
650 ;; May someday do completion on aliases.
651 (read-string prompt))
652
653 \f
654
655 ;;; Mode for composing and sending a draft message.
656
657 (defvar mh-sent-from-folder nil) ;Folder of msg assoc with this letter.
658
659 (defvar mh-sent-from-msg nil) ;Number of msg assoc with this letter.
660
661 (defvar mh-send-args nil) ;Extra args to pass to "send" command.
662
663 (defvar mh-annotate-char nil) ;Character to use to annotate mh-sent-from-msg.
664
665 (defvar mh-annotate-field nil) ;Field name for message annotation.
666
667 (put 'mh-letter-mode 'mode-class 'special)
668
669 ;;;###autoload
670 (defun mh-letter-mode ()
671 "Mode for composing letters in mh-e.\\<mh-letter-mode-map>
672 When you have finished composing, type \\[mh-send-letter] to send the message
673 using the MH mail handling system.
674 See the documentation for \\[mh-edit-mhn] for information on composing MIME
675 messages.
676
677 \\{mh-letter-mode-map}
678
679 Variables controlling this mode (defaults in parentheses):
680
681 mh-delete-yanked-msg-window (nil)
682 If non-nil, \\[mh-yank-cur-msg] will delete any windows displaying
683 the yanked message.
684
685 mh-yank-from-start-of-msg (t)
686 If non-nil, \\[mh-yank-cur-msg] will include the entire message.
687 If `body', just yank the body (no header).
688 If nil, only the portion of the message following the point will be yanked.
689 If there is a region, this variable is ignored.
690
691 mh-ins-buf-prefix (\"> \")
692 String to insert before each non-blank line of a message as it is
693 inserted in a draft letter.
694
695 mh-signature-file-name (\"~/.signature\")
696 File to be inserted into message by \\[mh-insert-signature].
697
698 This command runs the normal hooks `text-mode-hook' and `mh-letter-mode-hook'."
699
700 (interactive)
701 (or mh-user-path (mh-find-path))
702 (make-local-variable 'paragraph-start)
703 (setq paragraph-start (concat "^[ \t]*[-_][-_][-_]+$\\|" paragraph-start))
704 (make-local-variable 'paragraph-separate)
705 (setq paragraph-separate
706 (concat "^[ \t]*[-_][-_][-_]+$\\|" paragraph-separate))
707 (make-local-variable 'mh-send-args)
708 (make-local-variable 'mh-annotate-char)
709 (make-local-variable 'mh-annotate-field)
710 (make-local-variable 'mh-previous-window-config)
711 (make-local-variable 'mh-sent-from-folder)
712 (make-local-variable 'mh-sent-from-msg)
713 (make-local-variable 'mail-header-separator)
714 (setq mail-header-separator "--------") ;for Hyperbole
715 (use-local-map mh-letter-mode-map)
716 (setq major-mode 'mh-letter-mode)
717 (mh-set-mode-name "MH-Letter")
718 (set-syntax-table mh-letter-mode-syntax-table)
719 (run-hooks 'text-mode-hook)
720 ;; if text-mode-hook turned on auto-fill, tune it for messages
721 (cond ((and (boundp 'auto-fill-hook) auto-fill-hook) ;emacs 18
722 (make-local-variable 'auto-fill-hook)
723 (setq auto-fill-hook 'mh-auto-fill-for-letter)))
724 (cond ((and (boundp 'auto-fill-function) auto-fill-function) ;emacs 19
725 (make-local-variable 'auto-fill-function)
726 (setq auto-fill-function 'mh-auto-fill-for-letter)))
727 (run-hooks 'mh-letter-mode-hook))
728
729
730 (defun mh-auto-fill-for-letter ()
731 ;; Auto-fill in letters treats the header specially by inserting a tab
732 ;; before continuation line.
733 (if (mh-in-header-p)
734 (let ((fill-prefix "\t"))
735 (do-auto-fill))
736 (do-auto-fill)))
737
738
739 (defun mh-in-header-p ()
740 ;; Return non-nil if the point is in the header of a draft message.
741 (save-excursion
742 (let ((cur-point (point)))
743 (goto-char (point-min))
744 (re-search-forward "^-*$" nil t)
745 (< cur-point (point)))))
746
747
748 (defun mh-to-field ()
749 "Move point to the end of a specified header field.
750 The field is indicated by the previous keystroke (the last keystroke
751 of the command) according to the list in the variable `mh-to-field-choices'.
752 Create the field if it does not exist. Set the mark to point before moving."
753 (interactive)
754 (expand-abbrev)
755 (let ((target (cdr (or (assoc (char-to-string (logior last-input-char ?`))
756 mh-to-field-choices)
757 ;; also look for a char for version 4 compat
758 (assoc (logior last-input-char ?`) mh-to-field-choices))))
759 (case-fold-search t))
760 (push-mark)
761 (cond ((mh-position-on-field target)
762 (let ((eol (point)))
763 (skip-chars-backward " \t")
764 (delete-region (point) eol))
765 (if (and (not (eq (logior last-input-char ?`) ?s))
766 (save-excursion
767 (backward-char 1)
768 (not (looking-at "[:,]"))))
769 (insert ", ")
770 (insert " ")))
771 (t
772 (if (mh-position-on-field "To:")
773 (forward-line 1))
774 (insert (format "%s \n" target))
775 (backward-char 1)))))
776
777
778 (defun mh-to-fcc (&optional folder)
779 "Insert an Fcc: FOLDER field in the current message.
780 Prompt for the field name with a completion list of the current folders."
781 (interactive)
782 (or folder
783 (setq folder (mh-prompt-for-folder
784 "Fcc"
785 (or (and mh-default-folder-for-message-function
786 (save-excursion
787 (goto-char (point-min))
788 (funcall mh-default-folder-for-message-function)))
789 "")
790 t)))
791 (let ((last-input-char ?\C-f))
792 (expand-abbrev)
793 (save-excursion
794 (mh-to-field)
795 (insert (if (mh-folder-name-p folder)
796 (substring folder 1)
797 folder)))))
798
799
800 (defun mh-insert-signature ()
801 "Insert the file named by `mh-signature-file-name' at point."
802 (interactive)
803 (insert-file-contents mh-signature-file-name)
804 (force-mode-line-update))
805
806
807 (defun mh-check-whom ()
808 "Verify recipients of the current letter, showing expansion of any aliases."
809 (interactive)
810 (let ((file-name buffer-file-name))
811 (save-buffer)
812 (message "Checking recipients...")
813 (mh-in-show-buffer ("*Recipients*")
814 (bury-buffer (current-buffer))
815 (erase-buffer)
816 (mh-exec-cmd-output "whom" t file-name))
817 (message "Checking recipients...done")))
818
819 \f
820
821 ;;; Routines to compose and send a letter.
822
823 (defun mh-compose-and-send-mail (draft send-args
824 sent-from-folder sent-from-msg
825 to subject cc
826 annotate-char annotate-field
827 config)
828 ;; Edit and compose a draft message in buffer DRAFT and send or save it.
829 ;; SENT-FROM-FOLDER is buffer containing scan listing of current folder, or
830 ;; nil if none exists.
831 ;; SENT-FROM-MSG is the message number or sequence name or nil.
832 ;; SEND-ARGS is an optional argument passed to the send command.
833 ;; The TO, SUBJECT, and CC fields are passed to the
834 ;; mh-compose-letter-function.
835 ;; If ANNOTATE-CHAR is non-null, it is used to notate the scan listing of the
836 ;; message. In that case, the ANNOTATE-FIELD is used to build a string
837 ;; for mh-annotate-msg.
838 ;; CONFIG is the window configuration to restore after sending the letter.
839 (pop-to-buffer draft)
840 (mh-letter-mode)
841 (setq mh-sent-from-folder sent-from-folder)
842 (setq mh-sent-from-msg sent-from-msg)
843 (setq mh-send-args send-args)
844 (setq mh-annotate-char annotate-char)
845 (setq mh-annotate-field annotate-field)
846 (setq mh-previous-window-config config)
847 (setq mode-line-buffer-identification (list "{%b}"))
848 (if (and (boundp 'mh-compose-letter-function)
849 mh-compose-letter-function)
850 ;; run-hooks will not pass arguments.
851 (let ((value mh-compose-letter-function))
852 (if (and (listp value) (not (eq (car value) 'lambda)))
853 (while value
854 (funcall (car value) to subject cc)
855 (setq value (cdr value)))
856 (funcall mh-compose-letter-function to subject cc)))))
857
858
859 (defun mh-send-letter (&optional arg)
860 "Send the draft letter in the current buffer.
861 If optional prefix argument is provided, monitor delivery.
862 Run `mh-before-send-letter-hook' before actually doing anything."
863 (interactive "P")
864 (run-hooks 'mh-before-send-letter-hook)
865 (save-buffer)
866 (message "Sending...")
867 (let ((draft-buffer (current-buffer))
868 (file-name buffer-file-name)
869 (config mh-previous-window-config)
870 (coding-system-for-write
871 (if (and (local-variable-p 'buffer-file-coding-system)
872 ;; We're not sure why, but buffer-file-coding-system
873 ;; tends to get set to undecided-unix.
874 (not (memq buffer-file-coding-system
875 '(undecided undecided-unix undecided-dos))))
876 buffer-file-coding-system
877 (or sendmail-coding-system
878 default-buffer-file-coding-system
879 'iso-latin-1))))
880 (cond (arg
881 (pop-to-buffer "MH mail delivery")
882 (erase-buffer)
883 (mh-exec-cmd-output mh-send-prog t "-watch" "-nopush"
884 "-nodraftfolder" mh-send-args file-name)
885 (goto-char (point-max)) ; show the interesting part
886 (recenter -1)
887 (set-buffer draft-buffer)) ; for annotation below
888 (t
889 (mh-exec-cmd-daemon mh-send-prog "-nodraftfolder" "-noverbose"
890 mh-send-args file-name)))
891 (if mh-annotate-char
892 (mh-annotate-msg mh-sent-from-msg
893 mh-sent-from-folder
894 mh-annotate-char
895 "-component" mh-annotate-field
896 "-text" (format "\"%s %s\""
897 (mh-get-header-field "To:")
898 (mh-get-header-field "Cc:"))))
899
900 (cond ((or (not arg)
901 (y-or-n-p "Kill draft buffer? "))
902 (kill-buffer draft-buffer)
903 (if config
904 (set-window-configuration config))))
905 (if arg
906 (message "Sending...done")
907 (message "Sending...backgrounded"))))
908
909
910 (defun mh-insert-letter (folder message verbatim)
911 "Insert a message into the current letter.
912 Removes the message's headers using `mh-invisible-headers'. Prefixes
913 each non-blank line with `mh-ins-buf-prefix'. Prompts for FOLDER and
914 MESSAGE. If prefix argument VERBATIM provided, do not indent and do
915 not delete headers. Leaves the mark before the letter and point after it."
916 (interactive
917 (list (mh-prompt-for-folder "Message from" mh-sent-from-folder nil)
918 (read-input (format "Message number%s: "
919 (if mh-sent-from-msg
920 (format " [%d]" mh-sent-from-msg)
921 "")))
922 current-prefix-arg))
923 (save-restriction
924 (narrow-to-region (point) (point))
925 (let ((start (point-min)))
926 (if (equal message "") (setq message (int-to-string mh-sent-from-msg)))
927 (mh-exec-lib-cmd-output "mhl" "-nobell" "-noclear"
928 (expand-file-name message
929 (mh-expand-file-name folder)))
930 (cond ((not verbatim)
931 (mh-clean-msg-header start mh-invisible-headers mh-visible-headers)
932 (set-mark start) ; since mh-clean-msg-header moves it
933 (mh-insert-prefix-string mh-ins-buf-prefix))))))
934
935
936 (defun mh-yank-cur-msg ()
937 "Insert the current message into the draft buffer.
938 Prefix each non-blank line in the message with the string in
939 `mh-ins-buf-prefix'. If a region is set in the message's buffer, then
940 only the region will be inserted. Otherwise, the entire message will
941 be inserted if `mh-yank-from-start-of-msg' is non-nil. If this variable
942 is nil, the portion of the message following the point will be yanked.
943 If `mh-delete-yanked-msg-window' is non-nil, any window displaying the
944 yanked message will be deleted."
945 (interactive)
946 (if (and mh-sent-from-folder mh-sent-from-msg)
947 (let ((to-point (point))
948 (to-buffer (current-buffer)))
949 (set-buffer mh-sent-from-folder)
950 (if mh-delete-yanked-msg-window
951 (delete-windows-on mh-show-buffer))
952 (set-buffer mh-show-buffer) ; Find displayed message
953 (let ((mh-ins-str (cond ((if (boundp 'mark-active)
954 mark-active ;Emacs 19
955 (mark)) ;Emacs 18
956 (buffer-substring (region-beginning)
957 (region-end)))
958 ((eq 'body mh-yank-from-start-of-msg)
959 (buffer-substring
960 (save-excursion
961 (goto-char (point-min))
962 (mh-goto-header-end 1)
963 (point))
964 (point-max)))
965 (mh-yank-from-start-of-msg
966 (buffer-substring (point-min) (point-max)))
967 (t
968 (buffer-substring (point) (point-max))))))
969 (set-buffer to-buffer)
970 (save-restriction
971 (narrow-to-region to-point to-point)
972 (push-mark)
973 (insert mh-ins-str)
974 (mh-insert-prefix-string mh-ins-buf-prefix)
975 (insert "\n"))))
976 (error "There is no current message")))
977
978
979 (defun mh-insert-prefix-string (mh-ins-string)
980 ;; Run mail-citation-hook to insert a prefix string before each line
981 ;; in the buffer. Generality for supercite users.
982 (set-mark (point-max))
983 (goto-char (point-min))
984 (cond (mail-citation-hook
985 (run-hooks 'mail-citation-hook))
986 (mh-yank-hooks ;old hook name
987 (run-hooks 'mh-yank-hooks))
988 (t
989 (or (bolp) (forward-line 1))
990 (let ((zmacs-regions nil)) ;so "(mark)" works in XEmacs
991 (while (< (point) (mark))
992 (insert mh-ins-string)
993 (forward-line 1))))))
994
995
996 (defun mh-fully-kill-draft ()
997 "Kill the draft message file and the draft message buffer.
998 Use \\[kill-buffer] if you don't want to delete the draft message file."
999 (interactive)
1000 (if (y-or-n-p "Kill draft message? ")
1001 (let ((config mh-previous-window-config))
1002 (if (file-exists-p buffer-file-name)
1003 (delete-file buffer-file-name))
1004 (set-buffer-modified-p nil)
1005 (kill-buffer (buffer-name))
1006 (message "")
1007 (if config
1008 (set-window-configuration config)))
1009 (error "Message not killed")))
1010
1011
1012 ;;; Build the letter-mode keymap:
1013
1014 (define-key mh-letter-mode-map "\C-c\C-f\C-b" 'mh-to-field)
1015 (define-key mh-letter-mode-map "\C-c\C-f\C-c" 'mh-to-field)
1016 (define-key mh-letter-mode-map "\C-c\C-f\C-d" 'mh-to-field)
1017 (define-key mh-letter-mode-map "\C-c\C-f\C-f" 'mh-to-fcc)
1018 (define-key mh-letter-mode-map "\C-c\C-f\C-r" 'mh-to-field)
1019 (define-key mh-letter-mode-map "\C-c\C-f\C-s" 'mh-to-field)
1020 (define-key mh-letter-mode-map "\C-c\C-f\C-t" 'mh-to-field)
1021 (define-key mh-letter-mode-map "\C-c\C-fb" 'mh-to-field)
1022 (define-key mh-letter-mode-map "\C-c\C-fc" 'mh-to-field)
1023 (define-key mh-letter-mode-map "\C-c\C-fd" 'mh-to-field)
1024 (define-key mh-letter-mode-map "\C-c\C-ff" 'mh-to-fcc)
1025 (define-key mh-letter-mode-map "\C-c\C-fr" 'mh-to-field)
1026 (define-key mh-letter-mode-map "\C-c\C-fs" 'mh-to-field)
1027 (define-key mh-letter-mode-map "\C-c\C-ft" 'mh-to-field)
1028 (define-key mh-letter-mode-map "\C-c\C-i" 'mh-insert-letter)
1029 (define-key mh-letter-mode-map "\C-c\C-q" 'mh-fully-kill-draft)
1030 (define-key mh-letter-mode-map "\C-c\C-\\" 'mh-fully-kill-draft) ;if no C-q
1031 (define-key mh-letter-mode-map "\C-c\C-s" 'mh-insert-signature)
1032 (define-key mh-letter-mode-map "\C-c\C-^" 'mh-insert-signature) ;if no C-s
1033 (define-key mh-letter-mode-map "\C-c\C-w" 'mh-check-whom)
1034 (define-key mh-letter-mode-map "\C-c\C-y" 'mh-yank-cur-msg)
1035 (define-key mh-letter-mode-map "\C-c\C-c" 'mh-send-letter)
1036 (define-key mh-letter-mode-map "\C-c\C-m\C-f" 'mh-mhn-compose-forw)
1037 (define-key mh-letter-mode-map "\C-c\C-m\C-e" 'mh-mhn-compose-anon-ftp)
1038 (define-key mh-letter-mode-map "\C-c\C-m\C-t" 'mh-mhn-compose-external-compressed-tar)
1039 (define-key mh-letter-mode-map "\C-c\C-m\C-i" 'mh-mhn-compose-insertion)
1040 (define-key mh-letter-mode-map "\C-c\C-e" 'mh-edit-mhn)
1041 (define-key mh-letter-mode-map "\C-c\C-m\C-u" 'mh-revert-mhn-edit)
1042
1043 ;; "C-c /" prefix is used in mh-letter-mode by pgp.el
1044
1045 ;;; autoloads from mh-mime
1046
1047 (autoload 'mh-mhn-compose-insertion "mh-mime"
1048 "Add a directive to insert a MIME message part from a file.
1049 This is the typical way to insert non-text parts in a message.
1050 See also \\[mh-edit-mhn]." t)
1051
1052 (autoload 'mh-mhn-compose-anon-ftp "mh-mime"
1053 "Add a directive for a MIME anonymous ftp external body part.
1054 This directive tells MH to include a reference to a
1055 message/external-body part retrievable by anonymous FTP.
1056 See also \\[mh-edit-mhn]." t)
1057
1058 (autoload 'mh-mhn-compose-external-compressed-tar "mh-mime"
1059 "Add a directive to include a MIME reference to a compressed tar file.
1060 The file should be available via anonymous ftp. This directive
1061 tells MH to include a reference to a message/external-body part.
1062 See also \\[mh-edit-mhn]." t)
1063
1064 (autoload 'mh-mhn-compose-forw "mh-mime"
1065 "Add a forw directive to this message, to forward a message with MIME.
1066 This directive tells MH to include another message in this one.
1067 See also \\[mh-edit-mhn]." t)
1068
1069 (autoload 'mh-edit-mhn "mh-mime"
1070 "Format the current draft for MIME, expanding any mhn directives.
1071 Process the current draft with the mhn program, which,
1072 using directives already inserted in the draft, fills in
1073 all the MIME components and header fields.
1074 This step should be done last just before sending the message.
1075 The mhn program is part of MH version 6.8 or later.
1076 The \\[mh-revert-mhn-edit] command undoes this command.
1077 For assistance with creating mhn directives to insert
1078 various types of components in a message, see
1079 \\[mh-mhn-compose-insertion] (generic insertion from a file),
1080 \\[mh-mhn-compose-anon-ftp] (external reference to file via anonymous ftp),
1081 \\[mh-mhn-compose-external-compressed-tar] \
1082 \(reference to compressed tar file via anonymous ftp), and
1083 \\[mh-mhn-compose-forw] (forward message)." t)
1084
1085 (autoload 'mh-revert-mhn-edit "mh-mime"
1086 "Undoes the effect of \\[mh-edit-mhn] by reverting to the backup file.
1087 Optional non-nil argument means don't ask for confirmation." t)