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