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