]> code.delx.au - gnu-emacs/blob - lisp/mail/mh-comp.el
Add defgroup; use defcustom for user vars.
[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.8 1996/01/14 07:34:30 erik 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 a complete path 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 a complete path 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 ()
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 Upon invoking mh-letter-mode, text-mode-hook and mh-letter-mode-hook are
699 invoked with no args, if those values are non-nil."
700
701 (interactive)
702 (or mh-user-path (mh-find-path))
703 (make-local-variable 'paragraph-start)
704 (setq paragraph-start (concat "^[ \t]*[-_][-_][-_]+$\\|" paragraph-start))
705 (make-local-variable 'paragraph-separate)
706 (setq paragraph-separate
707 (concat "^[ \t]*[-_][-_][-_]+$\\|" paragraph-separate))
708 (make-local-variable 'mh-send-args)
709 (make-local-variable 'mh-annotate-char)
710 (make-local-variable 'mh-annotate-field)
711 (make-local-variable 'mh-previous-window-config)
712 (make-local-variable 'mh-sent-from-folder)
713 (make-local-variable 'mh-sent-from-msg)
714 (make-local-variable 'mail-header-separator)
715 (setq mail-header-separator "--------") ;for Hyperbole
716 (use-local-map mh-letter-mode-map)
717 (setq major-mode 'mh-letter-mode)
718 (mh-set-mode-name "MH-Letter")
719 (set-syntax-table mh-letter-mode-syntax-table)
720 (run-hooks 'text-mode-hook)
721 ;; if text-mode-hook turned on auto-fill, tune it for messages
722 (cond ((and (boundp 'auto-fill-hook) auto-fill-hook) ;emacs 18
723 (make-local-variable 'auto-fill-hook)
724 (setq auto-fill-hook 'mh-auto-fill-for-letter)))
725 (cond ((and (boundp 'auto-fill-function) auto-fill-function) ;emacs 19
726 (make-local-variable 'auto-fill-function)
727 (setq auto-fill-function 'mh-auto-fill-for-letter)))
728 (run-hooks 'mh-letter-mode-hook))
729
730
731 (defun mh-auto-fill-for-letter ()
732 ;; Auto-fill in letters treats the header specially by inserting a tab
733 ;; before continuation line.
734 (if (mh-in-header-p)
735 (let ((fill-prefix "\t"))
736 (do-auto-fill))
737 (do-auto-fill)))
738
739
740 (defun mh-in-header-p ()
741 ;; Return non-nil if the point is in the header of a draft message.
742 (save-excursion
743 (let ((cur-point (point)))
744 (goto-char (point-min))
745 (re-search-forward "^-*$" nil t)
746 (< cur-point (point)))))
747
748
749 (defun mh-to-field ()
750 "Move point to the end of a specified header field.
751 The field is indicated by the previous keystroke (the last keystroke
752 of the command) according to the list in the variable mh-to-field-choices.
753 Create the field if it does not exist. Set the mark to point before moving."
754 (interactive)
755 (expand-abbrev)
756 (let ((target (cdr (or (assoc (char-to-string (logior last-input-char ?`))
757 mh-to-field-choices)
758 ;; also look for a char for version 4 compat
759 (assoc (logior last-input-char ?`) mh-to-field-choices))))
760 (case-fold-search t))
761 (push-mark)
762 (cond ((mh-position-on-field target)
763 (let ((eol (point)))
764 (skip-chars-backward " \t")
765 (delete-region (point) eol))
766 (if (and (not (eq (logior last-input-char ?`) ?s))
767 (save-excursion
768 (backward-char 1)
769 (not (looking-at "[:,]"))))
770 (insert ", ")
771 (insert " ")))
772 (t
773 (if (mh-position-on-field "To:")
774 (forward-line 1))
775 (insert (format "%s \n" target))
776 (backward-char 1)))))
777
778
779 (defun mh-to-fcc (&optional folder)
780 "Insert an Fcc: FOLDER field in the current message.
781 Prompt for the field name with a completion list of the current folders."
782 (interactive)
783 (or folder
784 (setq folder (mh-prompt-for-folder
785 "Fcc"
786 (or (and mh-default-folder-for-message-function
787 (save-excursion
788 (goto-char (point-min))
789 (funcall mh-default-folder-for-message-function)))
790 "")
791 t)))
792 (let ((last-input-char ?\C-f))
793 (expand-abbrev)
794 (save-excursion
795 (mh-to-field)
796 (insert (if (mh-folder-name-p folder)
797 (substring folder 1)
798 folder)))))
799
800
801 (defun mh-insert-signature ()
802 "Insert the file named by mh-signature-file-name at the current point."
803 (interactive)
804 (insert-file-contents mh-signature-file-name)
805 (force-mode-line-update))
806
807
808 (defun mh-check-whom ()
809 "Verify recipients of the current letter, showing expansion of any aliases."
810 (interactive)
811 (let ((file-name buffer-file-name))
812 (save-buffer)
813 (message "Checking recipients...")
814 (mh-in-show-buffer ("*Recipients*")
815 (bury-buffer (current-buffer))
816 (erase-buffer)
817 (mh-exec-cmd-output "whom" t file-name))
818 (message "Checking recipients...done")))
819
820 \f
821
822 ;;; Routines to compose and send a letter.
823
824 (defun mh-compose-and-send-mail (draft send-args
825 sent-from-folder sent-from-msg
826 to subject cc
827 annotate-char annotate-field
828 config)
829 ;; Edit and compose a draft message in buffer DRAFT and send or save it.
830 ;; SENT-FROM-FOLDER is buffer containing scan listing of current folder, or
831 ;; nil if none exists.
832 ;; SENT-FROM-MSG is the message number or sequence name or nil.
833 ;; SEND-ARGS is an optional argument passed to the send command.
834 ;; The TO, SUBJECT, and CC fields are passed to the
835 ;; mh-compose-letter-function.
836 ;; If ANNOTATE-CHAR is non-null, it is used to notate the scan listing of the
837 ;; message. In that case, the ANNOTATE-FIELD is used to build a string
838 ;; for mh-annotate-msg.
839 ;; CONFIG is the window configuration to restore after sending the letter.
840 (pop-to-buffer draft)
841 (mh-letter-mode)
842 (setq mh-sent-from-folder sent-from-folder)
843 (setq mh-sent-from-msg sent-from-msg)
844 (setq mh-send-args send-args)
845 (setq mh-annotate-char annotate-char)
846 (setq mh-annotate-field annotate-field)
847 (setq mh-previous-window-config config)
848 (setq mode-line-buffer-identification (list "{%b}"))
849 (if (and (boundp 'mh-compose-letter-function)
850 mh-compose-letter-function)
851 ;; run-hooks will not pass arguments.
852 (let ((value mh-compose-letter-function))
853 (if (and (listp value) (not (eq (car value) 'lambda)))
854 (while value
855 (funcall (car value) to subject cc)
856 (setq value (cdr value)))
857 (funcall mh-compose-letter-function to subject cc)))))
858
859
860 (defun mh-send-letter (&optional arg)
861 "Send the draft letter in the current buffer.
862 If optional prefix argument is provided, monitor delivery.
863 Run mh-before-send-letter-hook before doing anything."
864 (interactive "P")
865 (run-hooks 'mh-before-send-letter-hook)
866 (save-buffer)
867 (message "Sending...")
868 (let ((draft-buffer (current-buffer))
869 (file-name buffer-file-name)
870 (config mh-previous-window-config))
871 (cond (arg
872 (pop-to-buffer "MH mail delivery")
873 (erase-buffer)
874 (mh-exec-cmd-output mh-send-prog t "-watch" "-nopush"
875 "-nodraftfolder" mh-send-args file-name)
876 (goto-char (point-max)) ; show the interesting part
877 (recenter -1)
878 (set-buffer draft-buffer)) ; for annotation below
879 (t
880 (mh-exec-cmd-daemon mh-send-prog "-nodraftfolder" "-noverbose"
881 mh-send-args file-name)))
882 (if mh-annotate-char
883 (mh-annotate-msg mh-sent-from-msg
884 mh-sent-from-folder
885 mh-annotate-char
886 "-component" mh-annotate-field
887 "-text" (format "\"%s %s\""
888 (mh-get-header-field "To:")
889 (mh-get-header-field "Cc:"))))
890
891 (cond ((or (not arg)
892 (y-or-n-p "Kill draft buffer? "))
893 (kill-buffer draft-buffer)
894 (if config
895 (set-window-configuration config))))
896 (if arg
897 (message "Sending...done")
898 (message "Sending...backgrounded"))))
899
900
901 (defun mh-insert-letter (folder message verbatim)
902 "Insert a message into the current letter.
903 Removes the message's headers using mh-invisible-headers. Prefixes
904 each non-blank line with mh-ins-buf-prefix. Prompts for FOLDER and
905 MESSAGE. If prefix argument VERBATIM provided, do not indent and do
906 not delete headers. Leaves the mark before the letter and point after it."
907 (interactive
908 (list (mh-prompt-for-folder "Message from" mh-sent-from-folder nil)
909 (read-input (format "Message number%s: "
910 (if mh-sent-from-msg
911 (format " [%d]" mh-sent-from-msg)
912 "")))
913 current-prefix-arg))
914 (save-restriction
915 (narrow-to-region (point) (point))
916 (let ((start (point-min)))
917 (if (equal message "") (setq message (int-to-string mh-sent-from-msg)))
918 (mh-exec-lib-cmd-output "mhl" "-nobell" "-noclear"
919 (expand-file-name message
920 (mh-expand-file-name folder)))
921 (cond ((not verbatim)
922 (mh-clean-msg-header start mh-invisible-headers mh-visible-headers)
923 (set-mark start) ; since mh-clean-msg-header moves it
924 (mh-insert-prefix-string mh-ins-buf-prefix))))))
925
926
927 (defun mh-yank-cur-msg ()
928 "Insert the current message into the draft buffer.
929 Prefix each non-blank line in the message with the string in
930 `mh-ins-buf-prefix'. If a region is set in the message's buffer, then
931 only the region will be inserted. Otherwise, the entire message will
932 be inserted if `mh-yank-from-start-of-msg' is non-nil. If this variable
933 is nil, the portion of the message following the point will be yanked.
934 If `mh-delete-yanked-msg-window' is non-nil, any window displaying the
935 yanked message will be deleted."
936 (interactive)
937 (if (and mh-sent-from-folder mh-sent-from-msg)
938 (let ((to-point (point))
939 (to-buffer (current-buffer)))
940 (set-buffer mh-sent-from-folder)
941 (if mh-delete-yanked-msg-window
942 (delete-windows-on mh-show-buffer))
943 (set-buffer mh-show-buffer) ; Find displayed message
944 (let ((mh-ins-str (cond ((if (boundp 'mark-active)
945 mark-active ;Emacs 19
946 (mark)) ;Emacs 18
947 (buffer-substring (region-beginning)
948 (region-end)))
949 ((eq 'body mh-yank-from-start-of-msg)
950 (buffer-substring
951 (save-excursion
952 (goto-char (point-min))
953 (mh-goto-header-end 1)
954 (point))
955 (point-max)))
956 (mh-yank-from-start-of-msg
957 (buffer-substring (point-min) (point-max)))
958 (t
959 (buffer-substring (point) (point-max))))))
960 (set-buffer to-buffer)
961 (save-restriction
962 (narrow-to-region to-point to-point)
963 (push-mark)
964 (insert mh-ins-str)
965 (mh-insert-prefix-string mh-ins-buf-prefix)
966 (insert "\n"))))
967 (error "There is no current message")))
968
969
970 (defun mh-insert-prefix-string (mh-ins-string)
971 ;; Run mail-citation-hook to insert a prefix string before each line
972 ;; in the buffer. Generality for supercite users.
973 (set-mark (point-max))
974 (goto-char (point-min))
975 (cond (mail-citation-hook
976 (run-hooks 'mail-citation-hook))
977 (mh-yank-hooks ;old hook name
978 (run-hooks 'mh-yank-hooks))
979 (t
980 (or (bolp) (forward-line 1))
981 (let ((zmacs-regions nil)) ;so "(mark)" works in XEmacs
982 (while (< (point) (mark))
983 (insert mh-ins-string)
984 (forward-line 1))))))
985
986
987 (defun mh-fully-kill-draft ()
988 "Kill the draft message file and the draft message buffer.
989 Use \\[kill-buffer] if you don't want to delete the draft message file."
990 (interactive)
991 (if (y-or-n-p "Kill draft message? ")
992 (let ((config mh-previous-window-config))
993 (if (file-exists-p buffer-file-name)
994 (delete-file buffer-file-name))
995 (set-buffer-modified-p nil)
996 (kill-buffer (buffer-name))
997 (message "")
998 (if config
999 (set-window-configuration config)))
1000 (error "Message not killed")))
1001
1002
1003 ;;; Build the letter-mode keymap:
1004
1005 (define-key mh-letter-mode-map "\C-c\C-f\C-b" 'mh-to-field)
1006 (define-key mh-letter-mode-map "\C-c\C-f\C-c" 'mh-to-field)
1007 (define-key mh-letter-mode-map "\C-c\C-f\C-d" 'mh-to-field)
1008 (define-key mh-letter-mode-map "\C-c\C-f\C-f" 'mh-to-fcc)
1009 (define-key mh-letter-mode-map "\C-c\C-f\C-r" 'mh-to-field)
1010 (define-key mh-letter-mode-map "\C-c\C-f\C-s" 'mh-to-field)
1011 (define-key mh-letter-mode-map "\C-c\C-f\C-t" 'mh-to-field)
1012 (define-key mh-letter-mode-map "\C-c\C-fb" 'mh-to-field)
1013 (define-key mh-letter-mode-map "\C-c\C-fc" 'mh-to-field)
1014 (define-key mh-letter-mode-map "\C-c\C-fd" 'mh-to-field)
1015 (define-key mh-letter-mode-map "\C-c\C-ff" 'mh-to-fcc)
1016 (define-key mh-letter-mode-map "\C-c\C-fr" 'mh-to-field)
1017 (define-key mh-letter-mode-map "\C-c\C-fs" 'mh-to-field)
1018 (define-key mh-letter-mode-map "\C-c\C-ft" 'mh-to-field)
1019 (define-key mh-letter-mode-map "\C-c\C-i" 'mh-insert-letter)
1020 (define-key mh-letter-mode-map "\C-c\C-q" 'mh-fully-kill-draft)
1021 (define-key mh-letter-mode-map "\C-c\C-\\" 'mh-fully-kill-draft) ;if no C-q
1022 (define-key mh-letter-mode-map "\C-c\C-s" 'mh-insert-signature)
1023 (define-key mh-letter-mode-map "\C-c\C-^" 'mh-insert-signature) ;if no C-s
1024 (define-key mh-letter-mode-map "\C-c\C-w" 'mh-check-whom)
1025 (define-key mh-letter-mode-map "\C-c\C-y" 'mh-yank-cur-msg)
1026 (define-key mh-letter-mode-map "\C-c\C-c" 'mh-send-letter)
1027 (define-key mh-letter-mode-map "\C-c\C-m\C-f" 'mh-mhn-compose-forw)
1028 (define-key mh-letter-mode-map "\C-c\C-m\C-e" 'mh-mhn-compose-anon-ftp)
1029 (define-key mh-letter-mode-map "\C-c\C-m\C-t" 'mh-mhn-compose-external-compressed-tar)
1030 (define-key mh-letter-mode-map "\C-c\C-m\C-i" 'mh-mhn-compose-insertion)
1031 (define-key mh-letter-mode-map "\C-c\C-e" 'mh-edit-mhn)
1032 (define-key mh-letter-mode-map "\C-c\C-m\C-u" 'mh-revert-mhn-edit)
1033
1034 ;; "C-c /" prefix is used in mh-letter-mode by pgp.el
1035
1036 ;;; autoloads from mh-mime
1037
1038 (autoload 'mh-mhn-compose-insertion "mh-mime"
1039 "Add a directive to insert a MIME message part from a file.
1040 This is the typical way to insert non-text parts in a message.
1041 See also \\[mh-edit-mhn]." t)
1042
1043 (autoload 'mh-mhn-compose-anon-ftp "mh-mime"
1044 "Add a directive for a MIME anonymous ftp external body part.
1045 This directive tells MH to include a reference to a
1046 message/external-body part retrievable by anonymous FTP.
1047 See also \\[mh-edit-mhn]." t)
1048
1049 (autoload 'mh-mhn-compose-external-compressed-tar "mh-mime"
1050 "Add a directive to include a MIME reference to a compressed tar file.
1051 The file should be available via anonymous ftp. This directive
1052 tells MH to include a reference to a message/external-body part.
1053 See also \\[mh-edit-mhn]." t)
1054
1055 (autoload 'mh-mhn-compose-forw "mh-mime"
1056 "Add a forw directive to this message, to forward a message with MIME.
1057 This directive tells MH to include another message in this one.
1058 See also \\[mh-edit-mhn]." t)
1059
1060 (autoload 'mh-edit-mhn "mh-mime"
1061 "Format the current draft for MIME, expanding any mhn directives.
1062 Process the current draft with the mhn program, which,
1063 using directives already inserted in the draft, fills in
1064 all the MIME components and header fields.
1065 This step should be done last just before sending the message.
1066 The mhn program is part of MH version 6.8 or later.
1067 The `\\[mh-revert-mhn-edit]' command undoes this command.
1068 For assistance with creating mhn directives to insert
1069 various types of components in a message, see
1070 \\[mh-mhn-compose-insertion] (generic insertion from a file),
1071 \\[mh-mhn-compose-anon-ftp] (external reference to file via anonymous ftp),
1072 \\[mh-mhn-compose-external-compressed-tar] \
1073 \(reference to compressed tar file via anonymous ftp), and
1074 \\[mh-mhn-compose-forw] (forward message)." t)
1075
1076 (autoload 'mh-revert-mhn-edit "mh-mime"
1077 "Undoes the effect of \\[mh-edit-mhn] by reverting to the backup file.
1078 Optional non-nil argument means don't ask for confirmation." t)