(mh-make-folder-mode-line): support mh-msg-count.
[bpt/emacs.git] / lisp / mail / mh-comp.el
CommitLineData
c26cf6c8 1;;; mh-comp --- mh-e functions for composing messages
9303c8db 2;; Time-stamp: <95/04/20 19:16:23 gildea>
c26cf6c8 3
847b8219 4;; Copyright (C) 1993, 1995 Free Software Foundation, Inc.
c26cf6c8 5
9b7bc076 6;; This file is part of GNU Emacs.
c26cf6c8 7
9b7bc076 8;; GNU Emacs is free software; you can redistribute it and/or modify
c26cf6c8
RS
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
9b7bc076 13;; GNU Emacs is distributed in the hope that it will be useful,
c26cf6c8
RS
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
9b7bc076 19;; along with GNU Emacs; see the file COPYING. If not, write to
c26cf6c8
RS
20;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
21
22;;; Commentary:
23
24;; Internal support for mh-e package.
25
847b8219
KH
26;;; Change Log:
27
2450bd29 28;; $Id: mh-comp.el,v 1.5 1995/04/20 23:35:49 kwzh Exp kwzh $
847b8219 29
c26cf6c8
RS
30;;; Code:
31
32(provide 'mh-comp)
33(require 'mh-utils)
34
847b8219
KH
35;;; Site customization (see also mh-utils.el):
36
37(defvar mh-send-prog "send"
38 "Name of the MH send program.
39Some 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.
43This is the case only when `send' is compiled with the BERK option.
44If MH will not allow you to redist a previously redist'd msg, set to nil.")
45
46
c26cf6c8
RS
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
c26cf6c8
RS
56(defvar mh-yank-hooks nil
57 "Obsolete hook for modifying a citation just inserted in the mail buffer.
58Each hook function can find the citation between point and mark.
59And each hook function should leave point and mark around the citation
60text as modified.
61
62This is a normal hook, misnamed for historical reasons.
63It 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.
67Each hook function can find the citation between point and mark.
68And each hook function should leave point and mark around the citation
69text as modified.
70
71If this hook is entirely empty (nil), the text of the message is inserted
72with mh-ins-buf-prefix prefixed to each line.
73
74See also the variable mh-yank-from-start-of-msg, which controls how
75much 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].
85If non-nil, yanking the current message into a draft letter deletes any
86windows 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].
90If non-nil, include the entire message. If the symbol `body', then yank the
91message minus the header. If nil, yank only the portion of the message
92following the point. If the show buffer has a region, this variable is
93ignored.")
94
847b8219
KH
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
98by \\[mh-insert-letter] or \\[mh-yank-cur-msg].")
99
c26cf6c8
RS
100(defvar mh-reply-default-reply-to nil
101 "*Sets the person or persons to whom a reply will be sent.
102If nil, prompt for recipient. If non-nil, then \\<mh-folder-mode-map>`\\[mh-reply]' will use this
847b8219
KH
103value and it should be one of \"from\", \"to\", \"cc\", or \"all\".
104The values \"cc\" and \"all\" do the same thing.")
c26cf6c8
RS
105
106(defvar mh-signature-file-name "~/.signature"
107 "*Name of file containing the user's signature.
108Inserted 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.
112The two string arguments to the format are the sender of the original
113message 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.
117Default is \"components\". If not a complete path name, the file
118is searched for first in the user's MH directory, then in the
119system MH lib directory.")
120
847b8219
KH
121(defvar mh-repl-formfile "replcomps"
122 "Name of file to be used as a skeleton for replying to messages.
123Default is \"replcomps\". If not a complete path name, the file
124is searched for first in the user's MH directory, then in the
125system MH lib directory.")
126
c26cf6c8
RS
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
847b8219 133 "Invoked when setting up a letter draft.
c26cf6c8
RS
134It 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
847b8219
KH
141 (concat "^ ----- Unsent message follows -----$" ;from sendmail V5
142 "\\|^ ----- Original message follows -----$" ;from sendmail V8
143 "\\|^------- Unsent Draft$" ;from MH itself
c26cf6c8 144 "\\|^---------- Original Message ----------$" ;from zmailer
847b8219
KH
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 )
c26cf6c8
RS
149 "Regexp specifying the beginning of the wrapper around a returned letter.
150This wrapper is generated by the mail system when rejecting a letter.")
151
152(defvar mh-new-draft-cleaned-headers
847b8219 153 "^Date:\\|^Received:\\|^Message-Id:\\|^From:\\|^Sender:\\|^Errors-To:\\|^Delivery-Date:\\|^Return-Path:"
c26cf6c8
RS
154 "Regexp of header lines to remove before offering a message as a new draft.
155Used by the \\<mh-folder-mode-map>`\\[mh-edit-again]' and `\\[mh-extract-rejected-mail]' commands.")
156
847b8219
KH
157(defvar mh-to-field-choices '(("t" . "To:") ("s" . "Subject:") ("c" . "Cc:")
158 ("b" . "Bcc:") ("f" . "Fcc:") ("r" . "From:")
159 ("d" . "Dcc:"))
c26cf6c8
RS
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.
178This function is an entry point to mh-e, the Emacs front end
847b8219
KH
179to the MH mail system.
180
181See documentation of `\\[mh-send]' for more details on composing mail."
c26cf6c8
RS
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.
847b8219 189Default is the current message.
c26cf6c8
RS
190The variable mh-new-draft-cleaned-headers specifies the headers to remove.
191See 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.
847b8219 211Default is the current message. The variable mh-new-draft-cleaned-headers
c26cf6c8
RS
212gives the headers to clean out of the original message.
213See 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)
847b8219
KH
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:")
c26cf6c8
RS
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.
236If optional prefix argument provided, then prompt for the message sequence.
237See 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)
847b8219 242 (mh-get-msg-num t))))
c26cf6c8
RS
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? "))
847b8219
KH
251 (mh-exec-cmd "forw" "-build"
252 mh-current-folder msg-or-seq)
c26cf6c8
RS
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)))))
847b8219
KH
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:")))
c26cf6c8
RS
269 (let ((forw-subject
270 (mh-forwarded-letter-subject orig-from orig-subject)))
847b8219
KH
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)
c26cf6c8 278 (mh-add-msgs-to-seq (mh-seq-to-msgs msg-or-seq) 'forwarded t))
847b8219
KH
279 (mh-compose-and-send-mail draft "" folder msg-or-seq
280 to forw-subject cc
281 mh-note-forw "Forwarded:"
282 config)))))
c26cf6c8
RS
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.
301This function is an entry point to mh-e, the Emacs front end
847b8219
KH
302to the MH mail system.
303
304See documentation of `\\[mh-send]' for more details on composing mail."
c26cf6c8
RS
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.
312Depending on how your copy of MH was compiled, you may need to change the
313setting 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"
847b8219 337 buffer-file-name
c26cf6c8 338 (expand-file-name mh-send-prog mh-progs)
847b8219 339 buffer-file-name))
c26cf6c8
RS
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)
847b8219 344 buffer-file-name)))
c26cf6c8
RS
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
847b8219
KH
352(defun mh-reply (message &optional includep)
353 "Reply to MESSAGE (default: current message).
c26cf6c8
RS
354If optional prefix argument INCLUDEP provided, then include the message
355in the reply using filter mhl.reply in your MH directory.
356Prompts for type of addresses to reply to:
357 from sender only,
358 to sender and primary recipients,
359 cc/all sender and all recipients.
847b8219
KH
360If the file named by `mh-repl-formfile' exists, it is used as a skeleton
361for the reply. See also documentation for `\\[mh-send]' function."
c26cf6c8
RS
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"
847b8219
KH
375 (if (stringp mh-repl-formfile) ;must be string, but we're paranoid
376 (list "-form" mh-repl-formfile))
377 mh-current-folder message
c26cf6c8
RS
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
847b8219
KH
392 (let ((to (mh-get-header-field "To:"))
393 (subject (mh-get-header-field "Subject:"))
394 (cc (mh-get-header-field "Cc:")))
c26cf6c8
RS
395 (goto-char (point-min))
396 (mh-goto-header-end 1)
397 (or includep
398 (mh-in-show-buffer (show-buffer)
847b8219
KH
399 (mh-display-msg message folder)))
400 (mh-add-msgs-to-seq message 'answered t)
c26cf6c8 401 (message "Composing a reply...done")
847b8219 402 (mh-compose-and-send-mail draft "" folder message to subject cc
c26cf6c8
RS
403 mh-note-repl "Replied:" config))))))
404
405
406(defun mh-send (to cc subject)
407 "Compose and send a letter.
408The file named by `mh-comp-formfile' will be used as the form.
847b8219
KH
409Do not call this function from outside mh-e; use \\[mh-smail] instead.
410
c26cf6c8
RS
411The letter is composed in mh-letter-mode; see its documentation for more
412details. If `mh-compose-letter-function' is defined, it is called on the
847b8219 413draft and passed three arguments: to, subject, and cc."
c26cf6c8
RS
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.
425Do not call this function from outside mh-e;
426use \\[mh-smail-other-window] instead.
427See 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)
847b8219
KH
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.
c26cf6c8
RS
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.
847b8219
KH
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
c26cf6c8
RS
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.
9303c8db 598 (if (re-search-forward "^-*$" nil nil)
c26cf6c8
RS
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
847b8219 611(defvar mh-sent-from-folder nil) ;Folder of msg assoc with this letter.
c26cf6c8 612
847b8219 613(defvar mh-sent-from-msg nil) ;Number of msg assoc with this letter.
c26cf6c8 614
847b8219 615(defvar mh-send-args nil) ;Extra args to pass to "send" command.
c26cf6c8 616
847b8219 617(defvar mh-annotate-char nil) ;Character to use to annotate mh-sent-from-msg.
c26cf6c8 618
847b8219 619(defvar mh-annotate-field nil) ;Field name for message annotation.
c26cf6c8
RS
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>
847b8219
KH
626When you have finished composing, type \\[mh-send-letter] to send the message
627using the MH mail handling system.
628See the documentation for \\[mh-edit-mhn] for information on composing MIME
629messages.
c26cf6c8
RS
630
631\\{mh-letter-mode-map}
632
633Variables 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
847b8219
KH
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
c26cf6c8
RS
649 mh-signature-file-name (\"~/.signature\")
650 File to be inserted into message by \\[mh-insert-signature].
651
652Upon invoking mh-letter-mode, text-mode-hook and mh-letter-mode-hook are
653invoked with no args, if those values are non-nil."
654
655 (interactive)
656 (or mh-user-path (mh-find-path))
c26cf6c8 657 (make-local-variable 'paragraph-start)
847b8219 658 (setq paragraph-start (concat "^[ \t]*[-_][-_][-_]+$\\|" paragraph-start))
c26cf6c8
RS
659 (make-local-variable 'paragraph-separate)
660 (setq paragraph-separate
847b8219 661 (concat "^[ \t]*[-_][-_][-_]+$\\|" paragraph-separate))
c26cf6c8
RS
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.
c26cf6c8 688 (if (mh-in-header-p)
9303c8db
KH
689 (let ((fill-prefix "\t"))
690 (do-auto-fill))
691 (do-auto-fill)))
c26cf6c8
RS
692
693
694(defun mh-in-header-p ()
695 ;; Return non-nil if the point is in the header of a draft message.
696 (save-excursion
697 (let ((cur-point (point)))
698 (goto-char (point-min))
699 (re-search-forward "^-*$" nil t)
700 (< cur-point (point)))))
701
702
703(defun mh-to-field ()
704 "Move point to the end of a specified header field.
705The field is indicated by the previous keystroke (the last keystroke
847b8219 706of the command) according to the list in the variable mh-to-field-choices.
c26cf6c8
RS
707Create the field if it does not exist. Set the mark to point before moving."
708 (interactive)
709 (expand-abbrev)
847b8219
KH
710 (let ((target (cdr (or (assoc (char-to-string (logior last-input-char ?`))
711 mh-to-field-choices)
712 ;; also look for a char for version 4 compat
713 (assoc (logior last-input-char ?`) mh-to-field-choices))))
c26cf6c8
RS
714 (case-fold-search t))
715 (push-mark)
716 (cond ((mh-position-on-field target)
717 (let ((eol (point)))
718 (skip-chars-backward " \t")
719 (delete-region (point) eol))
720 (if (and (not (eq (logior last-input-char ?`) ?s))
721 (save-excursion
722 (backward-char 1)
723 (not (looking-at "[:,]"))))
724 (insert ", ")
725 (insert " ")))
726 (t
727 (if (mh-position-on-field "To:")
728 (forward-line 1))
729 (insert (format "%s \n" target))
730 (backward-char 1)))))
731
732
733(defun mh-to-fcc (&optional folder)
734 "Insert an Fcc: FOLDER field in the current message.
735Prompt for the field name with a completion list of the current folders."
736 (interactive)
737 (or folder
738 (setq folder (mh-prompt-for-folder
739 "Fcc"
847b8219 740 (or (and mh-default-folder-for-message-function
c26cf6c8
RS
741 (save-excursion
742 (goto-char (point-min))
847b8219 743 (funcall mh-default-folder-for-message-function)))
c26cf6c8
RS
744 "")
745 t)))
746 (let ((last-input-char ?\C-f))
747 (expand-abbrev)
748 (save-excursion
749 (mh-to-field)
750 (insert (if (mh-folder-name-p folder)
751 (substring folder 1)
752 folder)))))
753
754
755(defun mh-insert-signature ()
756 "Insert the file named by mh-signature-file-name at the current point."
757 (interactive)
758 (insert-file-contents mh-signature-file-name)
2450bd29 759 (force-mode-line-update))
c26cf6c8
RS
760
761
762(defun mh-check-whom ()
847b8219 763 "Verify recipients of the current letter, showing expansion of any aliases."
c26cf6c8 764 (interactive)
847b8219 765 (let ((file-name buffer-file-name))
c26cf6c8
RS
766 (save-buffer)
767 (message "Checking recipients...")
768 (mh-in-show-buffer ("*Recipients*")
769 (bury-buffer (current-buffer))
770 (erase-buffer)
771 (mh-exec-cmd-output "whom" t file-name))
772 (message "Checking recipients...done")))
773
774\f
775
776;;; Routines to compose and send a letter.
777
778(defun mh-compose-and-send-mail (draft send-args
779 sent-from-folder sent-from-msg
780 to subject cc
781 annotate-char annotate-field
782 config)
783 ;; Edit and compose a draft message in buffer DRAFT and send or save it.
784 ;; SENT-FROM-FOLDER is buffer containing scan listing of current folder, or
785 ;; nil if none exists.
786 ;; SENT-FROM-MSG is the message number or sequence name or nil.
787 ;; SEND-ARGS is an optional argument passed to the send command.
788 ;; The TO, SUBJECT, and CC fields are passed to the
789 ;; mh-compose-letter-function.
790 ;; If ANNOTATE-CHAR is non-null, it is used to notate the scan listing of the
791 ;; message. In that case, the ANNOTATE-FIELD is used to build a string
792 ;; for mh-annotate-msg.
793 ;; CONFIG is the window configuration to restore after sending the letter.
794 (pop-to-buffer draft)
795 (mh-letter-mode)
796 (setq mh-sent-from-folder sent-from-folder)
797 (setq mh-sent-from-msg sent-from-msg)
798 (setq mh-send-args send-args)
799 (setq mh-annotate-char annotate-char)
800 (setq mh-annotate-field annotate-field)
801 (setq mh-previous-window-config config)
802 (setq mode-line-buffer-identification (list "{%b}"))
803 (if (and (boundp 'mh-compose-letter-function)
847b8219 804 mh-compose-letter-function)
c26cf6c8 805 ;; run-hooks will not pass arguments.
847b8219 806 (let ((value mh-compose-letter-function))
c26cf6c8
RS
807 (if (and (listp value) (not (eq (car value) 'lambda)))
808 (while value
809 (funcall (car value) to subject cc)
810 (setq value (cdr value)))
811 (funcall mh-compose-letter-function to subject cc)))))
812
813
814(defun mh-send-letter (&optional arg)
815 "Send the draft letter in the current buffer.
816If optional prefix argument is provided, monitor delivery.
817Run mh-before-send-letter-hook before doing anything."
818 (interactive "P")
819 (run-hooks 'mh-before-send-letter-hook)
820 (set-buffer-modified-p t) ; Make sure buffer is written
821 (save-buffer)
822 (message "Sending...")
823 (let ((draft-buffer (current-buffer))
847b8219 824 (file-name buffer-file-name)
c26cf6c8
RS
825 (config mh-previous-window-config))
826 (cond (arg
827 (pop-to-buffer "MH mail delivery")
828 (erase-buffer)
829 (mh-exec-cmd-output mh-send-prog t "-watch" "-nopush"
830 "-nodraftfolder" mh-send-args file-name)
831 (goto-char (point-max)) ; show the interesting part
832 (recenter -1)
833 (set-buffer draft-buffer)) ; for annotation below
834 (t
835 (mh-exec-cmd-daemon mh-send-prog "-nodraftfolder" "-noverbose"
836 mh-send-args file-name)))
837 (if mh-annotate-char
838 (mh-annotate-msg mh-sent-from-msg
839 mh-sent-from-folder
840 mh-annotate-char
841 "-component" mh-annotate-field
842 "-text" (format "\"%s %s\""
847b8219
KH
843 (mh-get-header-field "To:")
844 (mh-get-header-field "Cc:"))))
c26cf6c8
RS
845
846 (cond ((or (not arg)
847 (y-or-n-p "Kill draft buffer? "))
848 (kill-buffer draft-buffer)
849 (if config
850 (set-window-configuration config))))
851 (if arg
852 (message "Sending...done")
853 (message "Sending...backgrounded"))))
854
855
847b8219
KH
856(defun mh-insert-letter (folder message verbatim)
857 "Insert a message into the current letter.
858Removes the message's headers using mh-invisible-headers. Prefixes
859each non-blank line with mh-ins-buf-prefix. Prompts for FOLDER and
860MESSAGE. If prefix argument VERBATIM provided, do not indent and do
861not delete headers. Leaves the mark before the letter and point after it."
c26cf6c8 862 (interactive
847b8219
KH
863 (list (mh-prompt-for-folder "Message from" mh-sent-from-folder nil)
864 (read-input (format "Message number%s: "
c26cf6c8
RS
865 (if mh-sent-from-msg
866 (format " [%d]" mh-sent-from-msg)
847b8219 867 "")))
c26cf6c8
RS
868 current-prefix-arg))
869 (save-restriction
870 (narrow-to-region (point) (point))
871 (let ((start (point-min)))
847b8219 872 (if (equal message "") (setq message (int-to-string mh-sent-from-msg)))
c26cf6c8 873 (mh-exec-lib-cmd-output "mhl" "-nobell" "-noclear"
847b8219 874 (expand-file-name message
c26cf6c8 875 (mh-expand-file-name folder)))
847b8219 876 (cond ((not verbatim)
c26cf6c8
RS
877 (mh-clean-msg-header start mh-invisible-headers mh-visible-headers)
878 (set-mark start) ; since mh-clean-msg-header moves it
879 (mh-insert-prefix-string mh-ins-buf-prefix))))))
880
881
882(defun mh-yank-cur-msg ()
883 "Insert the current message into the draft buffer.
884Prefix each non-blank line in the message with the string in
885`mh-ins-buf-prefix'. If a region is set in the message's buffer, then
886only the region will be inserted. Otherwise, the entire message will
887be inserted if `mh-yank-from-start-of-msg' is non-nil. If this variable
888is nil, the portion of the message following the point will be yanked.
889If `mh-delete-yanked-msg-window' is non-nil, any window displaying the
890yanked message will be deleted."
891 (interactive)
892 (if (and mh-sent-from-folder mh-sent-from-msg)
893 (let ((to-point (point))
894 (to-buffer (current-buffer)))
895 (set-buffer mh-sent-from-folder)
896 (if mh-delete-yanked-msg-window
897 (delete-windows-on mh-show-buffer))
898 (set-buffer mh-show-buffer) ; Find displayed message
899 (let ((mh-ins-str (cond ((if (boundp 'mark-active)
847b8219
KH
900 mark-active ;Emacs 19
901 (mark)) ;Emacs 18
c26cf6c8
RS
902 (buffer-substring (region-beginning)
903 (region-end)))
904 ((eq 'body mh-yank-from-start-of-msg)
905 (buffer-substring
906 (save-excursion
907 (goto-char (point-min))
908 (mh-goto-header-end 1)
909 (point))
910 (point-max)))
911 (mh-yank-from-start-of-msg
912 (buffer-substring (point-min) (point-max)))
913 (t
914 (buffer-substring (point) (point-max))))))
915 (set-buffer to-buffer)
847b8219
KH
916 (save-restriction
917 (narrow-to-region to-point to-point)
918 (push-mark)
919 (insert mh-ins-str)
920 (mh-insert-prefix-string mh-ins-buf-prefix)
921 (insert "\n"))))
922 (error "There is no current message")))
c26cf6c8
RS
923
924
925(defun mh-insert-prefix-string (mh-ins-string)
847b8219 926 ;; Run mail-citation-hook to insert a prefix string before each line
c26cf6c8 927 ;; in the buffer. Generality for supercite users.
847b8219
KH
928 (set-mark (point-max))
929 (goto-char (point-min))
930 (cond (mail-citation-hook
931 (run-hooks 'mail-citation-hook))
932 (mh-yank-hooks ;old hook name
933 (run-hooks 'mh-yank-hooks))
934 (t
935 (or (bolp) (forward-line 1))
936 (let ((zmacs-regions nil)) ;so "(mark)" works in XEmacs
c26cf6c8
RS
937 (while (< (point) (mark))
938 (insert mh-ins-string)
939 (forward-line 1))))))
940
941
942(defun mh-fully-kill-draft ()
943 "Kill the draft message file and the draft message buffer.
944Use \\[kill-buffer] if you don't want to delete the draft message file."
945 (interactive)
946 (if (y-or-n-p "Kill draft message? ")
947 (let ((config mh-previous-window-config))
847b8219
KH
948 (if (file-exists-p buffer-file-name)
949 (delete-file buffer-file-name))
c26cf6c8
RS
950 (set-buffer-modified-p nil)
951 (kill-buffer (buffer-name))
952 (message "")
953 (if config
954 (set-window-configuration config)))
955 (error "Message not killed")))
956
847b8219 957
c26cf6c8
RS
958;;; Build the letter-mode keymap:
959
960(define-key mh-letter-mode-map "\C-c\C-f\C-b" 'mh-to-field)
961(define-key mh-letter-mode-map "\C-c\C-f\C-c" 'mh-to-field)
847b8219 962(define-key mh-letter-mode-map "\C-c\C-f\C-d" 'mh-to-field)
c26cf6c8 963(define-key mh-letter-mode-map "\C-c\C-f\C-f" 'mh-to-fcc)
847b8219 964(define-key mh-letter-mode-map "\C-c\C-f\C-r" 'mh-to-field)
c26cf6c8
RS
965(define-key mh-letter-mode-map "\C-c\C-f\C-s" 'mh-to-field)
966(define-key mh-letter-mode-map "\C-c\C-f\C-t" 'mh-to-field)
967(define-key mh-letter-mode-map "\C-c\C-fb" 'mh-to-field)
968(define-key mh-letter-mode-map "\C-c\C-fc" 'mh-to-field)
847b8219 969(define-key mh-letter-mode-map "\C-c\C-fd" 'mh-to-field)
c26cf6c8 970(define-key mh-letter-mode-map "\C-c\C-ff" 'mh-to-fcc)
847b8219 971(define-key mh-letter-mode-map "\C-c\C-fr" 'mh-to-field)
c26cf6c8
RS
972(define-key mh-letter-mode-map "\C-c\C-fs" 'mh-to-field)
973(define-key mh-letter-mode-map "\C-c\C-ft" 'mh-to-field)
847b8219 974(define-key mh-letter-mode-map "\C-c\C-i" 'mh-insert-letter)
c26cf6c8 975(define-key mh-letter-mode-map "\C-c\C-q" 'mh-fully-kill-draft)
847b8219
KH
976(define-key mh-letter-mode-map "\C-c\C-\\" 'mh-fully-kill-draft) ;if no C-q
977(define-key mh-letter-mode-map "\C-c\C-s" 'mh-insert-signature)
978(define-key mh-letter-mode-map "\C-c\C-^" 'mh-insert-signature) ;if no C-s
c26cf6c8 979(define-key mh-letter-mode-map "\C-c\C-w" 'mh-check-whom)
c26cf6c8 980(define-key mh-letter-mode-map "\C-c\C-y" 'mh-yank-cur-msg)
c26cf6c8
RS
981(define-key mh-letter-mode-map "\C-c\C-c" 'mh-send-letter)
982(define-key mh-letter-mode-map "\C-c\C-m\C-f" 'mh-mhn-compose-forw)
983(define-key mh-letter-mode-map "\C-c\C-m\C-e" 'mh-mhn-compose-anon-ftp)
984(define-key mh-letter-mode-map "\C-c\C-m\C-t" 'mh-mhn-compose-external-compressed-tar)
985(define-key mh-letter-mode-map "\C-c\C-m\C-i" 'mh-mhn-compose-insertion)
986(define-key mh-letter-mode-map "\C-c\C-e" 'mh-edit-mhn)
987(define-key mh-letter-mode-map "\C-c\C-m\C-u" 'mh-revert-mhn-edit)
988
989
990;;; autoloads from mh-mime
991
992(autoload 'mh-mhn-compose-insertion "mh-mime"
9303c8db 993 "Add a directive to insert a MIME message part from a file.
847b8219
KH
994This is the typical way to insert non-text parts in a message.
995See also \\[mh-edit-mhn]." t)
996
c26cf6c8 997(autoload 'mh-mhn-compose-anon-ftp "mh-mime"
9303c8db 998 "Add a directive for a MIME anonymous ftp external body part.
847b8219
KH
999This directive tells MH to include a reference to a
1000message/external-body part retrievable by anonymous FTP.
1001See also \\[mh-edit-mhn]." t)
1002
c26cf6c8 1003(autoload 'mh-mhn-compose-external-compressed-tar "mh-mime"
9303c8db 1004 "Add a directive to include a MIME reference to a compressed tar file.
847b8219
KH
1005The file should be available via anonymous ftp. This directive
1006tells MH to include a reference to a message/external-body part.
1007See also \\[mh-edit-mhn]." t)
1008
c26cf6c8 1009(autoload 'mh-mhn-compose-forw "mh-mime"
9303c8db 1010 "Add a forw directive to this message, to forward a message with MIME.
847b8219
KH
1011This directive tells MH to include another message in this one.
1012See also \\[mh-edit-mhn]." t)
1013
c26cf6c8 1014(autoload 'mh-edit-mhn "mh-mime"
847b8219
KH
1015 "Format the current draft for MIME, expanding any mhn directives.
1016Process the current draft with the mhn program, which,
1017using directives already inserted in the draft, fills in
c26cf6c8
RS
1018all the MIME components and header fields.
1019This step should be done last just before sending the message.
1020The mhn program is part of MH version 6.8 or later.
1021The `\\[mh-revert-mhn-edit]' command undoes this command.
847b8219 1022For assistance with creating mhn directives to insert
c26cf6c8
RS
1023various types of components in a message, see
1024\\[mh-mhn-compose-insertion] (generic insertion from a file),
1025\\[mh-mhn-compose-anon-ftp] (external reference to file via anonymous ftp),
1026\\[mh-mhn-compose-external-compressed-tar] \
1027\(reference to compressed tar file via anonymous ftp), and
1028\\[mh-mhn-compose-forw] (forward message)." t)
1029
1030(autoload 'mh-revert-mhn-edit "mh-mime"
847b8219
KH
1031 "Undoes the effect of \\[mh-edit-mhn] by reverting to the backup file.
1032Optional non-nil argument means don't ask for confirmation." t)