]> code.delx.au - gnu-emacs/blob - lisp/mh-e/mh-comp.el
Revision: miles@gnu.org--gnu-2005/emacs--unicode--0--patch-91
[gnu-emacs] / lisp / mh-e / mh-comp.el
1 ;;; mh-comp.el --- MH-E functions for composing messages
2
3 ;; Copyright (C) 1993, 1995, 1997,
4 ;; 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
5
6 ;; Author: Bill Wohler <wohler@newt.com>
7 ;; Maintainer: Bill Wohler <wohler@newt.com>
8 ;; Keywords: mail
9 ;; See: mh-e.el
10
11 ;; This file is part of GNU Emacs.
12
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; any later version.
17
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26 ;; Boston, MA 02110-1301, USA.
27
28 ;;; Commentary:
29
30 ;; Internal support for MH-E package.
31
32 ;;; Change Log:
33
34 ;;; Code:
35
36 (eval-when-compile (require 'mh-acros))
37 (mh-require-cl)
38 (require 'mh-e)
39 (require 'gnus-util)
40 (require 'easymenu)
41 (require 'mh-gnus)
42 (eval-when (compile load eval)
43 (ignore-errors (require 'mailabbrev)))
44
45 ;; Shush the byte-compiler
46 (defvar adaptive-fill-first-line-regexp)
47 (defvar font-lock-defaults)
48 (defvar mark-active)
49 (defvar sendmail-coding-system)
50 (defvar mh-identity-list)
51 (defvar mh-identity-default)
52 (defvar mh-mml-mode-default)
53 (defvar mh-identity-menu)
54
55 ;;; Autoloads
56 (autoload 'mail-mode-fill-paragraph "sendmail")
57 (autoload 'mm-handle-displayed-p "mm-decode")
58
59 (autoload 'sc-cite-original "sc"
60 "Workhorse citing function which performs the initial citation.
61 This is callable from the various mail and news readers' reply
62 function according to the agreed upon standard. See `sc-describe'
63 for more details. `sc-cite-original' does not do any yanking of the
64 original message but it does require a few things:
65
66 1) The reply buffer is the current buffer.
67
68 2) The original message has been yanked and inserted into the
69 reply buffer.
70
71 3) Verbose mail headers from the original message have been
72 inserted into the reply buffer directly before the text of the
73 original message.
74
75 4) Point is at the beginning of the verbose headers.
76
77 5) Mark is at the end of the body of text to be cited.
78
79 For Emacs 19's, the region need not be active (and typically isn't
80 when this function is called. Also, the hook `sc-pre-hook' is run
81 before, and `sc-post-hook' is run after the guts of this function.")
82
83 ;;; Site customization (see also mh-utils.el):
84
85 (defvar mh-send-prog "send"
86 "Name of the MH send program.
87 Some sites need to change this because of a name conflict.")
88
89 (defvar mh-redist-full-contents nil
90 "Non-nil if the `dist' command needs whole letter for redistribution.
91 This is the case only when `send' is compiled with the BERK option.
92 If MH will not allow you to redist a previously redist'd msg, set to nil.")
93
94 (defvar mh-redist-background nil
95 "If non-nil redist will be done in background like send.
96 This allows transaction log to be visible if -watch, -verbose or -snoop are
97 used.")
98
99 ;;; Scan Line Formats
100
101 (defvar mh-note-repl ?-
102 "Messages that have been replied to are marked by this character.")
103
104 (defvar mh-note-forw ?F
105 "Messages that have been forwarded are marked by this character.")
106
107 (defvar mh-note-dist ?R
108 "Messages that have been redistributed are marked by this character.")
109
110 (defvar mh-yank-hooks nil
111 "Obsolete hook for modifying a citation just inserted in the mail buffer.
112 Each hook function can find the citation between point and mark.
113 And each hook function should leave point and mark around the citation
114 text as modified.
115
116 This is a normal hook, misnamed for historical reasons.
117 It is semi-obsolete and is only used if `mail-citation-hook' is nil.")
118
119 (defvar mh-comp-formfile "components"
120 "Name of file to be used as a skeleton for composing messages.
121 Default is \"components\". If not an absolute file name, the file
122 is searched for first in the user's MH directory, then in the
123 system MH lib directory.")
124
125 (defvar mh-repl-formfile "replcomps"
126 "Name of file to be used as a skeleton for replying to messages.
127 Default is \"replcomps\". If not an absolute file name, the file
128 is searched for first in the user's MH directory, then in the
129 system MH lib directory.")
130
131 (defvar mh-repl-group-formfile "replgroupcomps"
132 "Name of file to be used as a skeleton for replying to messages.
133 This file is used to form replies to the sender and all recipients of a
134 message. Only used if `(mh-variant-p 'nmh)' is non-nil.
135 Default is \"replgroupcomps\".
136 If not an absolute file name, the file is searched for first in the user's MH
137 directory, then in the system MH lib directory.")
138
139 (defvar mh-rejected-letter-start
140 (format "^%s$"
141 (regexp-opt
142 '("Content-Type: message/rfc822" ;MIME MDN
143 "------ This is a copy of the message, including all the headers. ------";from exim
144 "--- Below this line is a copy of the message."; from qmail
145 " ----- Unsent message follows -----" ;from sendmail V5
146 " --------Unsent Message below:" ; from sendmail at BU
147 " ----- Original message follows -----" ;from sendmail V8
148 "------- Unsent Draft" ;from MH itself
149 "---------- Original Message ----------" ;from zmailer
150 " --- The unsent message follows ---" ;from AIX mail system
151 " Your message follows:" ;from MMDF-II
152 "Content-Description: Returned Content" ;1993 KJ sendmail
153 ))))
154
155 (defvar mh-new-draft-cleaned-headers
156 "^Date:\\|^Received:\\|^Message-Id:\\|^From:\\|^Sender:\\|^Errors-To:\\|^Delivery-Date:\\|^Return-Path:"
157 "Regexp of header lines to remove before offering a message as a new draft.
158 Used by the \\<mh-folder-mode-map>`\\[mh-edit-again]' and `\\[mh-extract-rejected-mail]' commands.")
159
160 (defvar mh-to-field-choices '(("t" . "To:") ("s" . "Subject:") ("c" . "Cc:")
161 ("b" . "Bcc:") ("f" . "Fcc:") ("r" . "From:")
162 ("d" . "Dcc:"))
163 "Alist of (final-character . field-name) choices for `mh-to-field'.")
164
165 (defvar mh-letter-mode-map (copy-keymap text-mode-map)
166 "Keymap for composing mail.")
167
168 (defvar mh-letter-mode-syntax-table nil
169 "Syntax table used by MH-E while in MH-Letter mode.")
170
171 (if mh-letter-mode-syntax-table
172 ()
173 (setq mh-letter-mode-syntax-table
174 (make-syntax-table text-mode-syntax-table))
175 (modify-syntax-entry ?% "." mh-letter-mode-syntax-table))
176
177 (defvar mh-sent-from-folder nil
178 "Folder of msg assoc with this letter.")
179
180 (defvar mh-sent-from-msg nil
181 "Number of msg assoc with this letter.")
182
183 (defvar mh-send-args nil
184 "Extra args to pass to \"send\" command.")
185
186 (defvar mh-annotate-char nil
187 "Character to use to annotate `mh-sent-from-msg'.")
188
189 (defvar mh-annotate-field nil
190 "Field name for message annotation.")
191
192 (defvar mh-insert-auto-fields-done-local nil
193 "Buffer-local variable set when `mh-insert-auto-fields' called successfully.")
194 (make-variable-buffer-local 'mh-insert-auto-fields-done-local)
195
196 ;;;###autoload
197 (defun mh-smail ()
198 "Compose and send mail with the MH mail system.
199 This function is an entry point to MH-E, the Emacs interface to the MH mail
200 system.
201
202 See `mh-send' for more details on composing mail."
203 (interactive)
204 (mh-find-path)
205 (call-interactively 'mh-send))
206
207 (defvar mh-error-if-no-draft nil) ;raise error over using old draft
208
209 ;;;###autoload
210 (defun mh-smail-batch (&optional to subject other-headers &rest ignored)
211 "Set up a mail composition draft with the MH mail system.
212 This function is an entry point to MH-E, the Emacs interface to the MH mail
213 system. This function does not prompt the user for any header fields, and thus
214 is suitable for use by programs that want to create a mail buffer. Users
215 should use `mh-smail' to compose mail.
216
217 Optional arguments for setting certain fields include TO, SUBJECT, and
218 OTHER-HEADERS. Additional arguments are IGNORED."
219 (mh-find-path)
220 (let ((mh-error-if-no-draft t))
221 (mh-send (or to "") "" (or subject ""))))
222
223 ;; XEmacs needs this:
224 ;;;###autoload
225 (defun mh-user-agent-compose (&optional to subject other-headers continue
226 switch-function yank-action
227 send-actions)
228 "Set up mail composition draft with the MH mail system.
229 This is `mail-user-agent' entry point to MH-E.
230
231 The optional arguments TO and SUBJECT specify recipients and the
232 initial Subject field, respectively.
233
234 OTHER-HEADERS is an alist specifying additional
235 header fields. Elements look like (HEADER . VALUE) where both
236 HEADER and VALUE are strings.
237
238 CONTINUE, SWITCH-FUNCTION, YANK-ACTION and SEND-ACTIONS are ignored."
239 (mh-find-path)
240 (let ((mh-error-if-no-draft t))
241 (mh-send to "" subject)
242 (while other-headers
243 (mh-insert-fields (concat (car (car other-headers)) ":")
244 (cdr (car other-headers)))
245 (setq other-headers (cdr other-headers)))))
246
247 ;;;###mh-autoload
248 (defun mh-edit-again (msg)
249 "Clean up a draft or a message MSG previously sent and make it resendable.
250 Default is the current message.
251 The variable `mh-new-draft-cleaned-headers' specifies the headers to remove.
252
253 See also `mh-send'."
254 (interactive (list (mh-get-msg-num t)))
255 (let* ((from-folder mh-current-folder)
256 (config (current-window-configuration))
257 (draft
258 (cond ((and mh-draft-folder (equal from-folder mh-draft-folder))
259 (pop-to-buffer (find-file-noselect (mh-msg-filename msg)) t)
260 (rename-buffer (format "draft-%d" msg))
261 ;; Make buffer writable...
262 (setq buffer-read-only nil)
263 ;; If buffer was being used to display the message reinsert
264 ;; from file...
265 (when (eq major-mode 'mh-show-mode)
266 (erase-buffer)
267 (insert-file-contents buffer-file-name))
268 (buffer-name))
269 (t
270 (mh-read-draft "clean-up" (mh-msg-filename msg) nil)))))
271 (mh-clean-msg-header (point-min) mh-new-draft-cleaned-headers nil)
272 (mh-insert-header-separator)
273 (goto-char (point-min))
274 (save-buffer)
275 (mh-compose-and-send-mail draft "" from-folder nil nil nil nil nil nil
276 config)
277 (mh-letter-mode-message)
278 (mh-letter-adjust-point)))
279
280 ;;;###mh-autoload
281 (defun mh-extract-rejected-mail (msg)
282 "Extract message MSG returned by the mail system and make it resendable.
283 Default is the current message. The variable `mh-new-draft-cleaned-headers'
284 gives the headers to clean out of the original message.
285
286 See also `mh-send'."
287 (interactive (list (mh-get-msg-num t)))
288 (let ((from-folder mh-current-folder)
289 (config (current-window-configuration))
290 (draft (mh-read-draft "extraction" (mh-msg-filename msg) nil)))
291 (goto-char (point-min))
292 (cond ((re-search-forward mh-rejected-letter-start nil t)
293 (skip-chars-forward " \t\n")
294 (delete-region (point-min) (point))
295 (mh-clean-msg-header (point-min) mh-new-draft-cleaned-headers nil))
296 (t
297 (message "Does not appear to be a rejected letter")))
298 (mh-insert-header-separator)
299 (goto-char (point-min))
300 (save-buffer)
301 (mh-compose-and-send-mail draft "" from-folder msg
302 (mh-get-header-field "To:")
303 (mh-get-header-field "From:")
304 (mh-get-header-field "Cc:")
305 nil nil config)
306 (mh-letter-mode-message)))
307
308 ;;;###mh-autoload
309 (defun mh-forward (to cc &optional range)
310 "Forward messages to the recipients TO and CC.
311 Use optional RANGE argument to specify a message or sequence to forward.
312 Default is the displayed message.
313
314 Check the documentation of `mh-interactive-range' to see how RANGE is read in
315 interactive use.
316
317 See also `mh-send'."
318 (interactive (list (mh-interactive-read-address "To: ")
319 (mh-interactive-read-address "Cc: ")
320 (mh-interactive-range "Forward")))
321 (let* ((folder mh-current-folder)
322 (msgs (mh-range-to-msg-list range))
323 (config (current-window-configuration))
324 (fwd-msg-file (mh-msg-filename (car msgs) folder))
325 ;; forw always leaves file in "draft" since it doesn't have -draft
326 (draft-name (expand-file-name "draft" mh-user-path))
327 (draft (cond ((or (not (file-exists-p draft-name))
328 (y-or-n-p "The file 'draft' exists. Discard it? "))
329 (mh-exec-cmd "forw" "-build"
330 (if (and (mh-variant-p 'nmh)
331 mh-compose-forward-as-mime-flag)
332 "-mime")
333 mh-current-folder
334 (mh-coalesce-msg-list msgs))
335 (prog1
336 (mh-read-draft "" draft-name t)
337 (mh-insert-fields "To:" to "Cc:" cc)
338 (save-buffer)))
339 (t
340 (mh-read-draft "" draft-name nil)))))
341 (let (orig-from
342 orig-subject)
343 (save-excursion
344 (set-buffer (get-buffer-create mh-temp-buffer))
345 (erase-buffer)
346 (insert-file-contents fwd-msg-file)
347 (setq orig-from (mh-get-header-field "From:"))
348 (setq orig-subject (mh-get-header-field "Subject:")))
349 (let ((forw-subject
350 (mh-forwarded-letter-subject orig-from orig-subject)))
351 (mh-insert-fields "Subject:" forw-subject)
352 (goto-char (point-min))
353 ;; If using MML, translate mhn
354 (if (equal mh-compose-insertion 'gnus)
355 (save-excursion
356 (goto-char (mh-mail-header-end))
357 (while
358 (re-search-forward
359 "^#forw \\[\\([^]]+\\)\\] \\(+\\S-+\\) \\(.*\\)$"
360 (point-max) t)
361 (let ((description (if (equal (match-string 1)
362 "forwarded messages")
363 "forwarded message %d"
364 (match-string 1)))
365 (msgs (split-string (match-string 3)))
366 (i 0))
367 (beginning-of-line)
368 (delete-region (point) (progn (forward-line 1) (point)))
369 (dolist (msg msgs)
370 (setq i (1+ i))
371 (mh-mml-forward-message (format description i)
372 folder msg))))))
373 ;; Postition just before forwarded message
374 (if (re-search-forward "^------- Forwarded Message" nil t)
375 (forward-line -1)
376 (goto-char (mh-mail-header-end))
377 (forward-line 1))
378 (delete-other-windows)
379 (mh-add-msgs-to-seq msgs 'forwarded t)
380 (mh-compose-and-send-mail draft "" folder msgs
381 to forw-subject cc
382 mh-note-forw "Forwarded:"
383 config)
384 (mh-letter-mode-message)
385 (mh-letter-adjust-point)
386 (run-hooks 'mh-forward-hook)))))
387
388 (defun mh-forwarded-letter-subject (from subject)
389 "Return a Subject suitable for a forwarded message.
390 Original message has headers FROM and SUBJECT."
391 (let ((addr-start (string-match "<" from))
392 (comment (string-match "(" from)))
393 (cond ((and addr-start (> addr-start 0))
394 ;; Full Name <luser@host>
395 (setq from (substring from 0 (1- addr-start))))
396 (comment
397 ;; luser@host (Full Name)
398 (setq from (substring from (1+ comment) (1- (length from)))))))
399 (format mh-forward-subject-format from subject))
400
401 ;;;###autoload
402 (defun mh-smail-other-window ()
403 "Compose and send mail in other window with the MH mail system.
404 This function is an entry point to MH-E, the Emacs interface to the MH mail
405 system.
406
407 See `mh-send' for more details on composing mail."
408 (interactive)
409 (mh-find-path)
410 (call-interactively 'mh-send-other-window))
411
412 ;;;###mh-autoload
413 (defun mh-redistribute (to cc &optional msg)
414 "Redistribute displayed message to recipients TO and CC.
415 Use optional argument MSG to redistribute another message.
416 Depending on how your copy of MH was compiled, you may need to change the
417 setting of the variable `mh-redist-full-contents'. See its documentation."
418 (interactive (list (mh-read-address "Redist-To: ")
419 (mh-read-address "Redist-Cc: ")
420 (mh-get-msg-num t)))
421 (or msg
422 (setq msg (mh-get-msg-num t)))
423 (save-window-excursion
424 (let ((folder mh-current-folder)
425 (draft (mh-read-draft "redistribution"
426 (if mh-redist-full-contents
427 (mh-msg-filename msg)
428 nil)
429 nil)))
430 (mh-goto-header-end 0)
431 (insert "Resent-To: " to "\n")
432 (if (not (equal cc "")) (insert "Resent-cc: " cc "\n"))
433 (mh-clean-msg-header
434 (point-min)
435 "^Message-Id:\\|^Received:\\|^Return-Path:\\|^Sender:\\|^Date:\\|^From:"
436 nil)
437 (save-buffer)
438 (message "Redistributing...")
439 (let ((env "mhdist=1"))
440 ;; Setup environment...
441 (setq env (concat env " mhaltmsg=" (if mh-redist-full-contents
442 buffer-file-name
443 (mh-msg-filename msg folder))))
444 (unless mh-redist-full-contents
445 (setq env (concat env " mhannotate=1")))
446 ;; Redistribute...
447 (if mh-redist-background
448 (mh-exec-cmd-env-daemon env mh-send-prog nil buffer-file-name)
449 (mh-exec-cmd-error env mh-send-prog "-push" buffer-file-name))
450 ;; Annotate...
451 (mh-annotate-msg msg folder mh-note-dist
452 "-component" "Resent:"
453 "-text" (format "\"%s %s\"" to cc)))
454 (kill-buffer draft)
455 (message "Redistributing...done"))))
456
457 (defun mh-show-buffer-message-number (&optional buffer)
458 "Message number of displayed message in corresponding show buffer.
459 Return nil if show buffer not displayed.
460 If in `mh-letter-mode', don't display the message number being replied to,
461 but rather the message number of the show buffer associated with our
462 originating folder buffer.
463 Optional argument BUFFER can be used to specify the buffer."
464 (save-excursion
465 (if buffer
466 (set-buffer buffer))
467 (cond ((eq major-mode 'mh-show-mode)
468 (let ((number-start (mh-search-from-end ?/ buffer-file-name)))
469 (car (read-from-string (substring buffer-file-name
470 (1+ number-start))))))
471 ((and (eq major-mode 'mh-folder-mode)
472 mh-show-buffer
473 (get-buffer mh-show-buffer))
474 (mh-show-buffer-message-number mh-show-buffer))
475 ((and (eq major-mode 'mh-letter-mode)
476 mh-sent-from-folder
477 (get-buffer mh-sent-from-folder))
478 (mh-show-buffer-message-number mh-sent-from-folder))
479 (t
480 nil))))
481
482 ;;;###mh-autoload
483 (defun mh-reply (message &optional reply-to includep)
484 "Reply to MESSAGE.
485 Default is the displayed message.
486 If the optional argument REPLY-TO is not given, prompts for type of addresses
487 to reply to:
488 from sender only,
489 to sender and primary recipients,
490 cc/all sender and all recipients.
491 If optional prefix argument INCLUDEP provided, then include the message
492 in the reply using filter `mhl.reply' in your MH directory.
493 If the file named by `mh-repl-formfile' exists, it is used as a skeleton for
494 the reply. If REPLY-TO is cc or all and you're using either the nmh or GNU
495 mailutils variants and the file names by `mh-repl-group-formfile' exists, it
496 is used instead.
497
498 See also `mh-send'."
499 (interactive (list
500 (mh-get-msg-num t)
501 (let ((minibuffer-help-form
502 "from => Sender only\nto => Sender and primary recipients\ncc or all => Sender and all recipients"))
503 (or mh-reply-default-reply-to
504 (completing-read "Reply to whom: [from] "
505 '(("from") ("to") ("cc") ("all"))
506 nil
507 t)))
508 current-prefix-arg))
509 (let* ((folder mh-current-folder)
510 (show-buffer mh-show-buffer)
511 (config (current-window-configuration))
512 (group-reply (or (equal reply-to "cc") (equal reply-to "all")))
513 (form-file (cond ((and (mh-variant-p 'nmh 'mu-mh) group-reply
514 (stringp mh-repl-group-formfile))
515 mh-repl-group-formfile)
516 ((stringp mh-repl-formfile) mh-repl-formfile)
517 (t nil))))
518 (message "Composing a reply...")
519 (mh-exec-cmd "repl" "-build" "-noquery" "-nodraftfolder"
520 (if form-file
521 (list "-form" form-file))
522 mh-current-folder message
523 (cond ((or (equal reply-to "from") (equal reply-to ""))
524 '("-nocc" "all"))
525 ((equal reply-to "to")
526 '("-cc" "to"))
527 (group-reply (if (mh-variant-p 'nmh 'mu-mh)
528 '("-group" "-nocc" "me")
529 '("-cc" "all" "-nocc" "me"))))
530 (cond ((or (eq mh-yank-from-start-of-msg 'autosupercite)
531 (eq mh-yank-from-start-of-msg 'autoattrib))
532 '("-noformat"))
533 (includep '("-filter" "mhl.reply"))
534 (t '())))
535 (let ((draft (mh-read-draft "reply"
536 (expand-file-name "reply" mh-user-path)
537 t)))
538 (delete-other-windows)
539 (save-buffer)
540
541 (let ((to (mh-get-header-field "To:"))
542 (subject (mh-get-header-field "Subject:"))
543 (cc (mh-get-header-field "Cc:")))
544 (goto-char (point-min))
545 (mh-goto-header-end 1)
546 (or includep
547 (not mh-reply-show-message-flag)
548 (mh-in-show-buffer (show-buffer)
549 (mh-display-msg message folder)))
550 (mh-add-msgs-to-seq message 'answered t)
551 (message "Composing a reply...done")
552 (mh-compose-and-send-mail draft "" folder message to subject cc
553 mh-note-repl "Replied:" config))
554 (when (and (or (eq 'autosupercite mh-yank-from-start-of-msg)
555 (eq 'autoattrib mh-yank-from-start-of-msg))
556 (eq (mh-show-buffer-message-number) mh-sent-from-msg))
557 (undo-boundary)
558 (mh-yank-cur-msg))
559 (mh-letter-mode-message))))
560
561 ;;;###mh-autoload
562 (defun mh-send (to cc subject)
563 "Compose and send a letter.
564 Do not call this function from outside MH-E; use \\[mh-smail] instead.
565
566 The file named by `mh-comp-formfile' will be used as the form.
567 The letter is composed in `mh-letter-mode'; see its documentation for more
568 details.
569 If `mh-compose-letter-function' is defined, it is called on the draft and
570 passed three arguments: TO, CC, and SUBJECT."
571 (interactive (list
572 (mh-interactive-read-address "To: ")
573 (mh-interactive-read-address "Cc: ")
574 (mh-interactive-read-string "Subject: ")))
575 (let ((config (current-window-configuration)))
576 (delete-other-windows)
577 (mh-send-sub to cc subject config)))
578
579 ;;;###mh-autoload
580 (defun mh-send-other-window (to cc subject)
581 "Compose and send a letter in another window.
582 Do not call this function from outside MH-E; use \\[mh-smail-other-window]
583 instead.
584
585 The file named by `mh-comp-formfile' will be used as the form.
586 The letter is composed in `mh-letter-mode'; see its documentation for more
587 details.
588 If `mh-compose-letter-function' is defined, it is called on the draft and
589 passed three arguments: TO, CC, and SUBJECT."
590 (interactive (list
591 (mh-interactive-read-address "To: ")
592 (mh-interactive-read-address "Cc: ")
593 (mh-interactive-read-string "Subject: ")))
594 (let ((pop-up-windows t))
595 (mh-send-sub to cc subject (current-window-configuration))))
596
597 (defun mh-send-sub (to cc subject config)
598 "Do the real work of composing and sending a letter.
599 Expects the TO, CC, and SUBJECT fields as arguments.
600 CONFIG is the window configuration before sending mail."
601 (let ((folder mh-current-folder)
602 (msg-num (mh-get-msg-num nil)))
603 (message "Composing a message...")
604 (let ((draft (mh-read-draft
605 "message"
606 (let (components)
607 (cond
608 ((file-exists-p
609 (setq components
610 (expand-file-name mh-comp-formfile mh-user-path)))
611 components)
612 ((file-exists-p
613 (setq components
614 (expand-file-name mh-comp-formfile mh-lib)))
615 components)
616 ((file-exists-p
617 (setq components
618 (expand-file-name mh-comp-formfile
619 ;; What is this mh-etc ?? -sm
620 ;; This is dead code, so
621 ;; remove it.
622 ;(and (boundp 'mh-etc) mh-etc)
623 )))
624 components)
625 (t
626 (error "Can't find components file \"%s\""
627 components))))
628 nil)))
629 (mh-insert-fields "To:" to "Subject:" subject "Cc:" cc)
630 (goto-char (point-max))
631 (mh-compose-and-send-mail draft "" folder msg-num
632 to subject cc
633 nil nil config)
634 (mh-letter-mode-message)
635 (mh-letter-adjust-point))))
636
637 (defun mh-read-draft (use initial-contents delete-contents-file)
638 "Read draft file into a draft buffer and make that buffer the current one.
639 USE is a message used for prompting about the intended use of the message.
640 INITIAL-CONTENTS is filename that is read into an empty buffer, or nil
641 if buffer should not be modified. Delete the initial-contents file if
642 DELETE-CONTENTS-FILE flag is set.
643 Returns the draft folder's name.
644 If the draft folder facility is enabled in ~/.mh_profile, a new buffer is
645 used each time and saved in the draft folder. The draft file can then be
646 reused."
647 (cond (mh-draft-folder
648 (let ((orig-default-dir default-directory)
649 (draft-file-name (mh-new-draft-name)))
650 (pop-to-buffer (generate-new-buffer
651 (format "draft-%s"
652 (file-name-nondirectory draft-file-name))))
653 (condition-case ()
654 (insert-file-contents draft-file-name t)
655 (file-error))
656 (setq default-directory orig-default-dir)))
657 (t
658 (let ((draft-name (expand-file-name "draft" mh-user-path)))
659 (pop-to-buffer "draft") ; Create if necessary
660 (if (buffer-modified-p)
661 (if (y-or-n-p "Draft has been modified; kill anyway? ")
662 (set-buffer-modified-p nil)
663 (error "Draft preserved")))
664 (setq buffer-file-name draft-name)
665 (clear-visited-file-modtime)
666 (unlock-buffer)
667 (cond ((and (file-exists-p draft-name)
668 (not (equal draft-name initial-contents)))
669 (insert-file-contents draft-name)
670 (delete-file draft-name))))))
671 (cond ((and initial-contents
672 (or (zerop (buffer-size))
673 (if (y-or-n-p
674 (format "A draft exists. Use for %s? " use))
675 (if mh-error-if-no-draft
676 (error "A prior draft exists"))
677 t)))
678 (erase-buffer)
679 (insert-file-contents initial-contents)
680 (if delete-contents-file (delete-file initial-contents))))
681 (auto-save-mode 1)
682 (if mh-draft-folder
683 (save-buffer)) ; Do not reuse draft name
684 (buffer-name))
685
686 (defun mh-new-draft-name ()
687 "Return the pathname of folder for draft messages."
688 (save-excursion
689 (mh-exec-cmd-quiet t "mhpath" mh-draft-folder "new")
690 (buffer-substring (point-min) (1- (point-max)))))
691
692 (defun mh-annotate-msg (msg buffer note &rest args)
693 "Mark MSG in BUFFER with character NOTE and annotate message with ARGS.
694 MSG can be a message number, a list of message numbers, or a sequence."
695 (apply 'mh-exec-cmd "anno" buffer
696 (if (listp msg) (append msg args) (cons msg args)))
697 (save-excursion
698 (cond ((get-buffer buffer) ; Buffer may be deleted
699 (set-buffer buffer)
700 (mh-iterate-on-range nil msg
701 (mh-notate nil note (1+ mh-cmd-note)))))))
702
703 (defun mh-insert-fields (&rest name-values)
704 "Insert the NAME-VALUES pairs in the current buffer.
705 If the field exists, append the value to it.
706 Do not insert any pairs whose value is the empty string."
707 (let ((case-fold-search t))
708 (while name-values
709 (let ((field-name (car name-values))
710 (value (car (cdr name-values))))
711 (if (not (string-match "^.*:$" field-name))
712 (setq field-name (concat field-name ":")))
713 (cond ((equal value "")
714 nil)
715 ((mh-position-on-field field-name)
716 (insert " " (or value "")))
717 (t
718 (insert field-name " " value "\n")))
719 (setq name-values (cdr (cdr name-values)))))))
720
721 (defun mh-position-on-field (field &optional ignored)
722 "Move to the end of the FIELD in the header.
723 Move to end of entire header if FIELD not found.
724 Returns non-nil iff FIELD was found.
725 The optional second arg is for pre-version 4 compatibility and is IGNORED."
726 (cond ((mh-goto-header-field field)
727 (mh-header-field-end)
728 t)
729 ((mh-goto-header-end 0)
730 nil)))
731
732 ;;;###mh-autoload
733 (defun mh-get-header-field (field)
734 "Find and return the body of FIELD in the mail header.
735 Returns the empty string if the field is not in the header of the
736 current buffer."
737 (if (mh-goto-header-field field)
738 (progn
739 (skip-chars-forward " \t") ;strip leading white space in body
740 (let ((start (point)))
741 (mh-header-field-end)
742 (buffer-substring-no-properties start (point))))
743 ""))
744
745 (fset 'mh-get-field 'mh-get-header-field) ;MH-E 4 compatibility
746
747 (defun mh-goto-header-field (field)
748 "Move to FIELD in the message header.
749 Move to the end of the FIELD name, which should end in a colon.
750 Returns t if found, nil if not."
751 (goto-char (point-min))
752 (let ((case-fold-search t)
753 (headers-end (save-excursion
754 (mh-goto-header-end 0)
755 (point))))
756 (re-search-forward (format "^%s" field) headers-end t)))
757
758 (defun mh-goto-header-end (arg)
759 "Move the cursor ARG lines after the header."
760 (if (re-search-forward "^-*$" nil nil)
761 (forward-line arg)))
762
763 (defun mh-extract-from-header-value ()
764 "Extract From: string from header."
765 (save-excursion
766 (if (not (mh-goto-header-field "From:"))
767 nil
768 (skip-chars-forward " \t")
769 (buffer-substring-no-properties
770 (point) (progn (mh-header-field-end)(point))))))
771
772 \f
773
774 ;;; Mode for composing and sending a draft message.
775
776 (put 'mh-letter-mode 'mode-class 'special)
777
778 ;;; Menu extracted from mh-menubar.el V1.1 (31 July 2001)
779 (eval-when-compile (defvar mh-letter-menu nil))
780 (easy-menu-define
781 mh-letter-menu mh-letter-mode-map "Menu for MH-E letter mode."
782 '("Letter"
783 ["Send This Draft" mh-send-letter t]
784 ["Split Current Line" mh-open-line t]
785 ["Check Recipient" mh-check-whom t]
786 ["Yank Current Message" mh-yank-cur-msg t]
787 ["Insert a Message..." mh-insert-letter t]
788 ["Insert Signature" mh-insert-signature t]
789 ("Encrypt/Sign Message"
790 ["Sign Message"
791 mh-mml-secure-message-sign mh-gnus-pgp-support-flag]
792 ["Encrypt Message"
793 mh-mml-secure-message-encrypt mh-gnus-pgp-support-flag]
794 ["Sign+Encrypt Message"
795 mh-mml-secure-message-signencrypt mh-gnus-pgp-support-flag]
796 ["Disable Security"
797 mh-mml-unsecure-message mh-gnus-pgp-support-flag]
798 "--"
799 "Security Method"
800 ["PGP (MIME)" (setq mh-mml-method-default "pgpmime")
801 :style radio
802 :selected (equal mh-mml-method-default "pgpmime")]
803 ["PGP" (setq mh-mml-method-default "pgp")
804 :style radio
805 :selected (equal mh-mml-method-default "pgp")]
806 ["S/MIME" (setq mh-mml-method-default "smime")
807 :style radio
808 :selected (equal mh-mml-method-default "smime")]
809 "--"
810 ["Save Method as Default"
811 (customize-save-variable 'mh-mml-method-default mh-mml-method-default) t]
812 )
813 ["Compose Insertion (MIME)..." mh-compose-insertion t]
814 ["Compose Compressed tar (MIME)..."
815 mh-mhn-compose-external-compressed-tar t]
816 ["Compose Get File (MIME)..." mh-mhn-compose-anon-ftp t]
817 ["Compose Forward (MIME)..." mh-compose-forward t]
818 ;; The next two will have to be merged. But I also need to make sure the
819 ;; user can't mix directives of both types.
820 ["Pull in All Compositions (mhn)"
821 mh-edit-mhn (mh-mhn-directive-present-p)]
822 ["Pull in All Compositions (gnus)"
823 mh-mml-to-mime (mh-mml-directive-present-p)]
824 ["Revert to Non-MIME Edit (mhn)"
825 mh-revert-mhn-edit (equal mh-compose-insertion 'mhn)]
826 ["Kill This Draft" mh-fully-kill-draft t]))
827
828 ;;; Help Messages
829 ;;; Group messages logically, more or less.
830 (defvar mh-letter-mode-help-messages
831 '((nil
832 "Send letter: \\[mh-send-letter]"
833 "\t\tOpen line: \\[mh-open-line]\n"
834 "Kill letter: \\[mh-fully-kill-draft]"
835 "\t\tInsert:\n"
836 "Check recipients: \\[mh-check-whom]"
837 "\t\t Current message: \\[mh-yank-cur-msg]\n"
838 "\t\t Attachment: \\[mh-compose-insertion]\n"
839 "\t\t Message to forward: \\[mh-compose-forward]\n"
840 " "
841 "Security:"
842 "\t\t Encrypt message: \\[mh-mml-secure-message-encrypt]"
843 "\t\t Sign+Encrypt message: \\[mh-mml-secure-message-signencrypt]"
844 "\t\t Sign message: \\[mh-mml-secure-message-sign]\n"
845 " "
846 "\t\t Signature: \\[mh-insert-signature]"))
847 "Key binding cheat sheet.
848
849 This is an associative array which is used to show the most common commands.
850 The key is a prefix char. The value is one or more strings which are
851 concatenated together and displayed in the minibuffer if ? is pressed after
852 the prefix character. The special key nil is used to display the
853 non-prefixed commands.
854
855 The substitutions described in `substitute-command-keys' are performed as
856 well.")
857
858 ;;;###mh-autoload
859 (defun mh-fill-paragraph-function (arg)
860 "Fill paragraph at or after point.
861 Prefix ARG means justify as well. This function enables `fill-paragraph' to
862 work better in MH-Letter mode."
863 (interactive "P")
864 (let ((fill-paragraph-function) (fill-prefix))
865 (if (mh-in-header-p)
866 (mail-mode-fill-paragraph arg)
867 (fill-paragraph arg))))
868
869 ;; Avoid compiler warnings in XEmacs and Emacs 20
870 (eval-when-compile
871 (defvar tool-bar-mode)
872 (defvar tool-bar-map))
873
874 ;;;###autoload
875 (define-derived-mode mh-letter-mode text-mode "MH-Letter"
876 "Mode for composing letters in MH-E.\\<mh-letter-mode-map>
877
878 When you have finished composing, type \\[mh-send-letter] to send the message
879 using the MH mail handling system.
880
881 There are two types of MIME directives used by MH-E: Gnus and MH. The option
882 `mh-compose-insertion' controls what type of directives are inserted by MH-E
883 commands. These directives can be converted to MIME body parts by running
884 \\[mh-edit-mhn] for mhn directives or \\[mh-mml-to-mime] for Gnus directives.
885 This step is mandatory if these directives are added manually. If the
886 directives are inserted with MH-E commands such as \\[mh-compose-insertion],
887 the directives are expanded automatically when the letter is sent.
888
889 Options that control this mode can be changed with
890 \\[customize-group]; specify the \"mh-compose\" group.
891
892 When a message is composed, the hooks `text-mode-hook' and
893 `mh-letter-mode-hook' are run.
894
895 \\{mh-letter-mode-map}"
896 (mh-find-path)
897 (make-local-variable 'mh-send-args)
898 (make-local-variable 'mh-annotate-char)
899 (make-local-variable 'mh-annotate-field)
900 (make-local-variable 'mh-previous-window-config)
901 (make-local-variable 'mh-sent-from-folder)
902 (make-local-variable 'mh-sent-from-msg)
903 ;; Set the local value of mh-mail-header-separator according to what is
904 ;; present in the buffer...
905 (set (make-local-variable 'mh-mail-header-separator)
906 (save-excursion
907 (goto-char (mh-mail-header-end))
908 (buffer-substring-no-properties (point) (line-end-position))))
909 (make-local-variable 'mail-header-separator)
910 (setq mail-header-separator mh-mail-header-separator) ;override sendmail.el
911 (make-local-variable 'mh-help-messages)
912 (setq mh-help-messages mh-letter-mode-help-messages)
913 (setq buffer-invisibility-spec '((vanish . t) t))
914 (set (make-local-variable 'line-move-ignore-invisible) t)
915
916 ;; From sendmail.el for proper paragraph fill
917 ;; sendmail.el also sets a normal-auto-fill-function (not done here)
918 (make-local-variable 'paragraph-separate)
919 (make-local-variable 'paragraph-start)
920 (make-local-variable 'fill-paragraph-function)
921 (setq fill-paragraph-function 'mh-fill-paragraph-function)
922 (make-local-variable 'adaptive-fill-regexp)
923 (setq adaptive-fill-regexp
924 (concat adaptive-fill-regexp
925 "\\|[ \t]*[-[:alnum:]]*>+[ \t]*"))
926 (make-local-variable 'adaptive-fill-first-line-regexp)
927 (setq adaptive-fill-first-line-regexp
928 (concat adaptive-fill-first-line-regexp
929 "\\|[ \t]*[-[:alnum:]]*>+[ \t]*"))
930 ;; `-- ' precedes the signature. `-----' appears at the start of the
931 ;; lines that delimit forwarded messages.
932 ;; Lines containing just >= 3 dashes, perhaps after whitespace,
933 ;; are also sometimes used and should be separators.
934 (setq paragraph-start (concat (regexp-quote mail-header-separator)
935 "\\|\t*\\([-|#;>* ]\\|(?[0-9]+[.)]\\)+$"
936 "\\|[ \t]*[[:alnum:]]*>+[ \t]*$\\|[ \t]*$\\|"
937 "-- $\\|---+$\\|"
938 page-delimiter))
939 (setq paragraph-separate paragraph-start)
940 ;; --- End of code from sendmail.el ---
941
942 ;; Enable undo since a show-mode buffer might have been reused.
943 (buffer-enable-undo)
944 (set (make-local-variable 'tool-bar-map) mh-letter-tool-bar-map)
945 (mh-funcall-if-exists mh-toolbar-init :letter)
946 (make-local-variable 'font-lock-defaults)
947 (cond
948 ((or (equal mh-highlight-citation-p 'font-lock)
949 (equal mh-highlight-citation-p 'gnus))
950 ;; Let's use font-lock even if gnus is used in show-mode. The reason
951 ;; is that gnus uses static text properties which are not appropriate
952 ;; for a buffer that will be edited. So the choice here is either fontify
953 ;; the citations and header...
954 (setq font-lock-defaults '(mh-letter-font-lock-keywords t)))
955 (t
956 ;; ...or the header only
957 (setq font-lock-defaults '(mh-show-font-lock-keywords t))))
958 (easy-menu-add mh-letter-menu)
959 (setq fill-column mh-letter-fill-column)
960 ;; If text-mode-hook turned on auto-fill, tune it for messages
961 (when auto-fill-function
962 (make-local-variable 'auto-fill-function)
963 (setq auto-fill-function 'mh-auto-fill-for-letter)))
964
965 (defun mh-font-lock-field-data (limit)
966 "Find header field region between point and LIMIT."
967 (and (< (point) (mh-letter-header-end))
968 (< (point) limit)
969 (let ((end (min limit (mh-letter-header-end)))
970 (point (point))
971 data-end data-begin field)
972 (end-of-line)
973 (setq data-end (if (re-search-forward "^[^ \t]" end t)
974 (match-beginning 0)
975 end))
976 (goto-char (1- data-end))
977 (if (not (re-search-backward "\\(^[^ \t][^:]*\\):[ \t]*" nil t))
978 (setq data-begin (point-min))
979 (setq data-begin (match-end 0))
980 (setq field (match-string 1)))
981 (setq data-begin (max point data-begin))
982 (goto-char (if (equal point data-end) (1+ data-end) data-end))
983 (cond ((and field (mh-letter-skipped-header-field-p field))
984 (set-match-data nil)
985 nil)
986 (t (set-match-data
987 (list data-begin data-end data-begin data-end))
988 t)))))
989
990 (defun mh-letter-header-end ()
991 "Find the end of the message header.
992 This function is to be used only for font locking. It works by searching for
993 `mh-mail-header-separator' in the buffer."
994 (save-excursion
995 (goto-char (point-min))
996 (cond ((equal mh-mail-header-separator "") (point-min))
997 ((search-forward (format "\n%s\n" mh-mail-header-separator) nil t)
998 (line-beginning-position 0))
999 (t (point-min)))))
1000
1001 (defun mh-auto-fill-for-letter ()
1002 "Perform auto-fill for message.
1003 Header is treated specially by inserting a tab before continuation lines."
1004 (if (mh-in-header-p)
1005 (let ((fill-prefix "\t"))
1006 (do-auto-fill))
1007 (do-auto-fill)))
1008
1009 (defun mh-insert-header-separator ()
1010 "Insert `mh-mail-header-separator', if absent."
1011 (save-excursion
1012 (goto-char (point-min))
1013 (rfc822-goto-eoh)
1014 (if (looking-at "$")
1015 (insert mh-mail-header-separator))))
1016
1017 ;;;###mh-autoload
1018 (defun mh-to-field ()
1019 "Move point to the end of a specified header field.
1020 The field is indicated by the previous keystroke (the last keystroke
1021 of the command) according to the list in the variable `mh-to-field-choices'.
1022 Create the field if it does not exist. Set the mark to point before moving."
1023 (interactive)
1024 (expand-abbrev)
1025 (let ((target (cdr (or (assoc (char-to-string (logior last-input-char ?`))
1026 mh-to-field-choices)
1027 ;; also look for a char for version 4 compat
1028 (assoc (logior last-input-char ?`)
1029 mh-to-field-choices))))
1030 (case-fold-search t))
1031 (push-mark)
1032 (cond ((mh-position-on-field target)
1033 (let ((eol (point)))
1034 (skip-chars-backward " \t")
1035 (delete-region (point) eol))
1036 (if (and (not (eq (logior last-input-char ?`) ?s))
1037 (save-excursion
1038 (backward-char 1)
1039 (not (looking-at "[:,]"))))
1040 (insert ", ")
1041 (insert " ")))
1042 (t
1043 (if (mh-position-on-field "To:")
1044 (forward-line 1))
1045 (insert (format "%s \n" target))
1046 (backward-char 1)))))
1047
1048 ;;;###mh-autoload
1049 (defun mh-to-fcc (&optional folder)
1050 "Insert an Fcc: FOLDER field in the current message.
1051 Prompt for the field name with a completion list of the current folders."
1052 (interactive)
1053 (or folder
1054 (setq folder (mh-prompt-for-folder
1055 "Fcc"
1056 (or (and mh-default-folder-for-message-function
1057 (save-excursion
1058 (goto-char (point-min))
1059 (funcall
1060 mh-default-folder-for-message-function)))
1061 "")
1062 t)))
1063 (let ((last-input-char ?\C-f))
1064 (expand-abbrev)
1065 (save-excursion
1066 (mh-to-field)
1067 (insert (if (mh-folder-name-p folder)
1068 (substring folder 1)
1069 folder)))))
1070
1071 (defun mh-file-is-vcard-p (file)
1072 "Return t if FILE is a .vcf vcard."
1073 (let ((case-fold-search t))
1074 (and (stringp file)
1075 (file-exists-p file)
1076 (or (and (not (mh-have-file-command))
1077 (not (null (string-match "\.vcf$" file))))
1078 (and (mh-have-file-command)
1079 (string-equal "text/x-vcard" (mh-file-mime-type file)))))))
1080
1081 ;;;###mh-autoload
1082 (defun mh-insert-signature (&optional file)
1083 "Insert the signature specified by `mh-signature-file-name' or FILE at point.
1084 A signature separator (`-- ') will be added if the signature block does not
1085 contain one and `mh-signature-separator-flag' is on.
1086 The value of `mh-letter-insert-signature-hook' is a list of functions to be
1087 called, with no arguments, after the signature is inserted.
1088 The signature can also be inserted with `mh-identity-list'."
1089 (interactive)
1090 (save-excursion
1091 (insert "\n")
1092 (let ((mh-signature-file-name (or file mh-signature-file-name))
1093 (mh-mhn-p (mh-mhn-directive-present-p))
1094 (mh-mml-p (mh-mml-directive-present-p)))
1095 (save-restriction
1096 (narrow-to-region (point) (point))
1097 (cond
1098 ((mh-file-is-vcard-p mh-signature-file-name)
1099 (if (equal mh-compose-insertion 'gnus)
1100 (insert "<#part type=\"text/x-vcard\" filename=\""
1101 mh-signature-file-name
1102 "\" disposition=inline description=VCard>\n<#/part>")
1103 (insert "#text/x-vcard; name=\""
1104 (file-name-nondirectory mh-signature-file-name)
1105 "\" [VCard] " (expand-file-name mh-signature-file-name))))
1106 (t
1107 (cond
1108 (mh-mhn-p
1109 (insert "#\n" "Content-Description: Signature\n"))
1110 (mh-mml-p
1111 (mml-insert-tag 'part 'type "text/plain" 'disposition "inline"
1112 'description "Signature")))
1113 (cond ((null mh-signature-file-name))
1114 ((and (stringp mh-signature-file-name)
1115 (file-readable-p mh-signature-file-name))
1116 (insert-file-contents mh-signature-file-name))
1117 ((functionp mh-signature-file-name)
1118 (funcall mh-signature-file-name)))))
1119 (save-restriction
1120 (widen)
1121 (run-hooks 'mh-letter-insert-signature-hook))
1122 (goto-char (point-min))
1123 (when (and (not (mh-file-is-vcard-p mh-signature-file-name))
1124 mh-signature-separator-flag
1125 (> (point-max) (point-min))
1126 (not (mh-signature-separator-p)))
1127 (cond (mh-mhn-p
1128 (forward-line 2))
1129 (mh-mml-p
1130 (forward-line 1)))
1131 (insert mh-signature-separator))
1132 (if (not (> (point-max) (point-min)))
1133 (message "No signature found")))))
1134 (force-mode-line-update))
1135
1136 ;;;###mh-autoload
1137 (defun mh-check-whom ()
1138 "Verify recipients of the current letter, showing expansion of any aliases."
1139 (interactive)
1140 (let ((file-name buffer-file-name))
1141 (save-buffer)
1142 (message "Checking recipients...")
1143 (mh-in-show-buffer (mh-recipients-buffer)
1144 (bury-buffer (current-buffer))
1145 (erase-buffer)
1146 (mh-exec-cmd-output "whom" t file-name))
1147 (message "Checking recipients...done")))
1148
1149 (defun mh-tidy-draft-buffer ()
1150 "Run when a draft buffer is destroyed."
1151 (let ((buffer (get-buffer mh-recipients-buffer)))
1152 (if buffer
1153 (kill-buffer buffer))))
1154
1155 \f
1156
1157 ;;; Routines to compose and send a letter.
1158
1159 (defun mh-insert-x-face ()
1160 "Append X-Face, Face or X-Image-URL field to header.
1161 If the field already exists, this function does nothing."
1162 (when (and (file-exists-p mh-x-face-file)
1163 (file-readable-p mh-x-face-file))
1164 (save-excursion
1165 (unless (or (mh-position-on-field "X-Face")
1166 (mh-position-on-field "Face")
1167 (mh-position-on-field "X-Image-URL"))
1168 (save-excursion
1169 (goto-char (+ (point) (cadr (insert-file-contents mh-x-face-file))))
1170 (if (not (looking-at "^"))
1171 (insert "\n")))
1172 (unless (looking-at "\\(X-Face\\|Face\\|X-Image-URL\\): ")
1173 (insert "X-Face: "))))))
1174
1175 (defvar mh-x-mailer-string nil
1176 "*String containing the contents of the X-Mailer header field.
1177 If nil, this variable is initialized to show the version of MH-E, Emacs, and
1178 MH the first time a message is composed.")
1179
1180 (defun mh-insert-x-mailer ()
1181 "Append an X-Mailer field to the header.
1182 The versions of MH-E, Emacs, and MH are shown."
1183 ;; Lazily initialize mh-x-mailer-string.
1184 (when (and mh-insert-x-mailer-flag (null mh-x-mailer-string))
1185 (setq mh-x-mailer-string
1186 (format "MH-E %s; %s; %sEmacs %s"
1187 mh-version mh-variant-in-use
1188 (if mh-xemacs-flag "X" "GNU ")
1189 (cond ((not mh-xemacs-flag) emacs-version)
1190 ((string-match "[0-9.]*\\( +\([ a-z]+[0-9]+\)\\)?"
1191 emacs-version)
1192 (match-string 0 emacs-version))
1193 (t (format "%s.%s" emacs-major-version
1194 emacs-minor-version))))))
1195 ;; Insert X-Mailer, but only if it doesn't already exist.
1196 (save-excursion
1197 (when (and mh-insert-x-mailer-flag
1198 (null (mh-goto-header-field "X-Mailer")))
1199 (mh-insert-fields "X-Mailer:" mh-x-mailer-string))))
1200
1201 (defun mh-regexp-in-field-p (regexp &rest fields)
1202 "Non-nil means REGEXP was found in FIELDS."
1203 (save-excursion
1204 (let ((search-result nil)
1205 (field))
1206 (while fields
1207 (setq field (car fields))
1208 (if (and (mh-goto-header-field field)
1209 (re-search-forward
1210 regexp (save-excursion (mh-header-field-end)(point)) t))
1211 (setq fields nil
1212 search-result t)
1213 (setq fields (cdr fields))))
1214 search-result)))
1215
1216 ;;;###mh-autoload
1217 (defun mh-insert-auto-fields (&optional non-interactive)
1218 "Insert custom fields if To or Cc match `mh-auto-fields-list'.
1219 Sets buffer-local `mh-insert-auto-fields-done-local' when done and inserted
1220 something. If NON-INTERACTIVE is non-nil, do not be verbose and only
1221 attempt matches if `mh-insert-auto-fields-done-local' is nil.
1222
1223 An `identity' entry is skipped if one was already entered manually.
1224
1225 Return t if fields added; otherwise return nil."
1226 (interactive)
1227 (when (or (not non-interactive)
1228 (not mh-insert-auto-fields-done-local))
1229 (save-excursion
1230 (when (and (or (mh-goto-header-field "To:")
1231 (mh-goto-header-field "cc:")))
1232 (let ((list mh-auto-fields-list)
1233 (fields-inserted nil))
1234 (while list
1235 (let ((regexp (nth 0 (car list)))
1236 (entries (nth 1 (car list))))
1237 (when (mh-regexp-in-field-p regexp "To:" "cc:")
1238 (setq mh-insert-auto-fields-done-local t)
1239 (setq fields-inserted t)
1240 (if (not non-interactive)
1241 (message "Fields for %s added" regexp))
1242 (let ((entry-list entries))
1243 (while entry-list
1244 (let ((field (caar entry-list))
1245 (value (cdar entry-list)))
1246 (cond
1247 ((equal ":identity" field)
1248 (when ;;(and (not mh-identity-local)
1249 ;; Bug 1204506. But do we need to be able
1250 ;; to set an identity manually that won't be
1251 ;; overridden by mh-insert-auto-fields?
1252 (assoc value mh-identity-list)
1253 ;;)
1254 (mh-insert-identity value)))
1255 (t
1256 (mh-modify-header-field field value
1257 (equal field "From")))))
1258 (setq entry-list (cdr entry-list))))))
1259 (setq list (cdr list)))
1260 fields-inserted)))))
1261
1262 (defun mh-modify-header-field (field value &optional overwrite-flag)
1263 "To header FIELD add VALUE.
1264 If OVERWRITE-FLAG is non-nil then the old value, if present, is discarded."
1265 (cond ((and overwrite-flag
1266 (mh-goto-header-field (concat field ":")))
1267 (insert " " value)
1268 (delete-region (point) (line-end-position)))
1269 ((and (not overwrite-flag)
1270 (mh-regexp-in-field-p (concat "\\b" value "\\b") field))
1271 ;; Already there, do nothing.
1272 )
1273 ((and (not overwrite-flag)
1274 (mh-goto-header-field (concat field ":")))
1275 (insert " " value ","))
1276 (t
1277 (mh-goto-header-end 0)
1278 (insert field ": " value "\n"))))
1279
1280 (defun mh-compose-and-send-mail (draft send-args
1281 sent-from-folder sent-from-msg
1282 to subject cc
1283 annotate-char annotate-field
1284 config)
1285 "Edit and compose a draft message in buffer DRAFT and send or save it.
1286 SEND-ARGS is the argument passed to the send command.
1287 SENT-FROM-FOLDER is buffer containing scan listing of current folder, or
1288 nil if none exists.
1289 SENT-FROM-MSG is the message number or sequence name or nil.
1290 The TO, SUBJECT, and CC fields are passed to the
1291 `mh-compose-letter-function'.
1292 If ANNOTATE-CHAR is non-null, it is used to notate the scan listing of the
1293 message. In that case, the ANNOTATE-FIELD is used to build a string
1294 for `mh-annotate-msg'.
1295 CONFIG is the window configuration to restore after sending the letter."
1296 (pop-to-buffer draft)
1297 (mh-letter-mode)
1298
1299 ;; Insert identity.
1300 (if (and (boundp 'mh-identity-default)
1301 mh-identity-default
1302 (not mh-identity-local))
1303 (mh-insert-identity mh-identity-default))
1304 (mh-identity-make-menu)
1305 (easy-menu-add mh-identity-menu)
1306
1307 ;; Insert extra fields.
1308 (mh-insert-x-mailer)
1309 (mh-insert-x-face)
1310
1311 (mh-letter-hide-all-skipped-fields)
1312
1313 (setq mh-sent-from-folder sent-from-folder)
1314 (setq mh-sent-from-msg sent-from-msg)
1315 (setq mh-send-args send-args)
1316 (setq mh-annotate-char annotate-char)
1317 (setq mh-annotate-field annotate-field)
1318 (setq mh-previous-window-config config)
1319 (setq mode-line-buffer-identification (list " {%b}"))
1320 (mh-logo-display)
1321 (mh-make-local-hook 'kill-buffer-hook)
1322 (add-hook 'kill-buffer-hook 'mh-tidy-draft-buffer nil t)
1323 (if (and (boundp 'mh-compose-letter-function)
1324 mh-compose-letter-function)
1325 ;; run-hooks will not pass arguments.
1326 (let ((value mh-compose-letter-function))
1327 (if (and (listp value) (not (eq (car value) 'lambda)))
1328 (while value
1329 (funcall (car value) to subject cc)
1330 (setq value (cdr value)))
1331 (funcall mh-compose-letter-function to subject cc)))))
1332
1333 (defun mh-letter-mode-message ()
1334 "Display a help message for users of `mh-letter-mode'.
1335 This should be the last function called when composing the draft."
1336 (message "%s" (substitute-command-keys
1337 (concat "Type \\[mh-send-letter] to send message, "
1338 "\\[mh-help] for help"))))
1339
1340 (defun mh-ascii-buffer-p ()
1341 "Check if current buffer is entirely composed of ASCII.
1342 The function doesn't work for XEmacs since `find-charset-region' doesn't exist
1343 there."
1344 (loop for charset in (mh-funcall-if-exists
1345 find-charset-region (point-min) (point-max))
1346 unless (eq charset 'ascii) return nil
1347 finally return t))
1348
1349 ;;;###mh-autoload
1350 (defun mh-send-letter (&optional arg)
1351 "Send the draft letter in the current buffer.
1352 If optional prefix argument ARG is provided, monitor delivery.
1353 The value of `mh-before-send-letter-hook' is a list of functions to be called,
1354 with no arguments, before doing anything.
1355 Run `\\[mh-edit-mhn]' if mhn directives are present; otherwise
1356 run `\\[mh-mml-to-mime]' if mml directives are present."
1357 (interactive "P")
1358 (run-hooks 'mh-before-send-letter-hook)
1359 (if (and (mh-insert-auto-fields t)
1360 mh-auto-fields-prompt-flag
1361 (goto-char (point-min)))
1362 (if (not (y-or-n-p "Auto fields inserted, send? "))
1363 (error "Send aborted")))
1364 (cond ((mh-mhn-directive-present-p)
1365 (mh-edit-mhn))
1366 ((or (mh-mml-directive-present-p) (not (mh-ascii-buffer-p)))
1367 (mh-mml-to-mime)))
1368 (save-buffer)
1369 (message "Sending...")
1370 (let ((draft-buffer (current-buffer))
1371 (file-name buffer-file-name)
1372 (config mh-previous-window-config)
1373 (coding-system-for-write
1374 (if (and (local-variable-p 'buffer-file-coding-system
1375 (current-buffer)) ;XEmacs needs two args
1376 ;; We're not sure why, but buffer-file-coding-system
1377 ;; tends to get set to undecided-unix.
1378 (not (memq buffer-file-coding-system
1379 '(undecided undecided-unix undecided-dos))))
1380 buffer-file-coding-system
1381 (or (and (boundp 'sendmail-coding-system) sendmail-coding-system)
1382 (and (boundp 'default-buffer-file-coding-system )
1383 default-buffer-file-coding-system)
1384 'iso-latin-1))))
1385 ;; The default BCC encapsulation will make a MIME message unreadable.
1386 ;; With nmh use the -mime arg to prevent this.
1387 (if (and (mh-variant-p 'nmh)
1388 (mh-goto-header-field "Bcc:")
1389 (mh-goto-header-field "Content-Type:"))
1390 (setq mh-send-args (format "-mime %s" mh-send-args)))
1391 (cond (arg
1392 (pop-to-buffer mh-mail-delivery-buffer)
1393 (erase-buffer)
1394 (mh-exec-cmd-output mh-send-prog t "-watch" "-nopush"
1395 "-nodraftfolder" mh-send-args file-name)
1396 (goto-char (point-max)) ; show the interesting part
1397 (recenter -1)
1398 (set-buffer draft-buffer)) ; for annotation below
1399 (t
1400 (mh-exec-cmd-daemon mh-send-prog nil "-nodraftfolder" "-noverbose"
1401 mh-send-args file-name)))
1402 (if mh-annotate-char
1403 (mh-annotate-msg mh-sent-from-msg
1404 mh-sent-from-folder
1405 mh-annotate-char
1406 "-component" mh-annotate-field
1407 "-text" (format "\"%s %s\""
1408 (mh-get-header-field "To:")
1409 (mh-get-header-field "Cc:"))))
1410
1411 (cond ((or (not arg)
1412 (y-or-n-p "Kill draft buffer? "))
1413 (kill-buffer draft-buffer)
1414 (if config
1415 (set-window-configuration config))))
1416 (if arg
1417 (message "Sending...done")
1418 (message "Sending...backgrounded"))))
1419
1420 ;;;###mh-autoload
1421 (defun mh-insert-letter (folder message verbatim)
1422 "Insert a message into the current letter.
1423 Removes the header fields according to the variable
1424 `mh-invisible-header-fields-compiled'.
1425 Prefixes each non-blank line with `mh-ins-buf-prefix', unless
1426 `mh-yank-from-start-of-msg' is set for supercite in which case supercite is
1427 used to format the message.
1428 Prompts for FOLDER and MESSAGE. If prefix argument VERBATIM provided, do
1429 not indent and do not delete headers. Leaves the mark before the letter
1430 and point after it."
1431 (interactive
1432 (list (mh-prompt-for-folder "Message from" mh-sent-from-folder nil)
1433 (read-string (concat "Message number"
1434 (if (numberp mh-sent-from-msg)
1435 (format " (default %d): " mh-sent-from-msg)
1436 ": ")))
1437 current-prefix-arg))
1438 (save-restriction
1439 (narrow-to-region (point) (point))
1440 (let ((start (point-min)))
1441 (if (and (equal message "") (numberp mh-sent-from-msg))
1442 (setq message (int-to-string mh-sent-from-msg)))
1443 (insert-file-contents
1444 (expand-file-name message (mh-expand-file-name folder)))
1445 (when (not verbatim)
1446 (mh-clean-msg-header start mh-invisible-header-fields-compiled nil)
1447 (goto-char (point-max)) ;Needed for sc-cite-original
1448 (push-mark) ;Needed for sc-cite-original
1449 (goto-char (point-min)) ;Needed for sc-cite-original
1450 (mh-insert-prefix-string mh-ins-buf-prefix)))))
1451
1452 (defun mh-extract-from-attribution ()
1453 "Extract phrase or comment from From header field."
1454 (save-excursion
1455 (if (not (mh-goto-header-field "From: "))
1456 nil
1457 (skip-chars-forward " ")
1458 (cond
1459 ((looking-at "\"\\([^\"\n]+\\)\" \\(<.+>\\)")
1460 (format "%s %s " (match-string 1)(match-string 2)))
1461 ((looking-at "\\([^<\n]+<.+>\\)$")
1462 (format "%s " (match-string 1)))
1463 ((looking-at "\\([^ ]+@[^ ]+\\) +(\\(.+\\))$")
1464 (format "%s <%s> " (match-string 2)(match-string 1)))
1465 ((looking-at " *\\(.+\\)$")
1466 (format "%s " (match-string 1)))))))
1467
1468 ;;;###mh-autoload
1469 (defun mh-yank-cur-msg ()
1470 "Insert the current message into the draft buffer.
1471 Prefix each non-blank line in the message with the string in
1472 `mh-ins-buf-prefix'. If a region is set in the message's buffer, then
1473 only the region will be inserted. Otherwise, the entire message will
1474 be inserted if `mh-yank-from-start-of-msg' is non-nil. If this variable
1475 is nil, the portion of the message following the point will be yanked.
1476 If `mh-delete-yanked-msg-window-flag' is non-nil, any window displaying the
1477 yanked message will be deleted."
1478 (interactive)
1479 (if (and mh-sent-from-folder
1480 (save-excursion (set-buffer mh-sent-from-folder) mh-show-buffer)
1481 (save-excursion (set-buffer mh-sent-from-folder)
1482 (get-buffer mh-show-buffer))
1483 mh-sent-from-msg)
1484 (let ((to-point (point))
1485 (to-buffer (current-buffer)))
1486 (set-buffer mh-sent-from-folder)
1487 (if mh-delete-yanked-msg-window-flag
1488 (delete-windows-on mh-show-buffer))
1489 (set-buffer mh-show-buffer) ; Find displayed message
1490 (let* ((from-attr (mh-extract-from-attribution))
1491 (yank-region (mh-mark-active-p nil))
1492 (mh-ins-str
1493 (cond ((and yank-region
1494 (or (eq 'supercite mh-yank-from-start-of-msg)
1495 (eq 'autosupercite mh-yank-from-start-of-msg)
1496 (eq t mh-yank-from-start-of-msg)))
1497 ;; supercite needs the full header
1498 (concat
1499 (buffer-substring (point-min) (mh-mail-header-end))
1500 "\n"
1501 (buffer-substring (region-beginning) (region-end))))
1502 (yank-region
1503 (buffer-substring (region-beginning) (region-end)))
1504 ((or (eq 'body mh-yank-from-start-of-msg)
1505 (eq 'attribution
1506 mh-yank-from-start-of-msg)
1507 (eq 'autoattrib
1508 mh-yank-from-start-of-msg))
1509 (buffer-substring
1510 (save-excursion
1511 (goto-char (point-min))
1512 (mh-goto-header-end 1)
1513 (point))
1514 (point-max)))
1515 ((or (eq 'supercite mh-yank-from-start-of-msg)
1516 (eq 'autosupercite mh-yank-from-start-of-msg)
1517 (eq t mh-yank-from-start-of-msg))
1518 (buffer-substring (point-min) (point-max)))
1519 (t
1520 (buffer-substring (point) (point-max))))))
1521 (set-buffer to-buffer)
1522 (save-restriction
1523 (narrow-to-region to-point to-point)
1524 (insert (mh-filter-out-non-text mh-ins-str))
1525 (goto-char (point-max)) ;Needed for sc-cite-original
1526 (push-mark) ;Needed for sc-cite-original
1527 (goto-char (point-min)) ;Needed for sc-cite-original
1528 (mh-insert-prefix-string mh-ins-buf-prefix)
1529 (when (or (eq 'attribution mh-yank-from-start-of-msg)
1530 (eq 'autoattrib mh-yank-from-start-of-msg))
1531 (insert from-attr)
1532 (mh-identity-insert-attribution-verb nil)
1533 (insert "\n\n"))
1534 ;; If the user has selected a region, he has already "edited" the
1535 ;; text, so leave the cursor at the end of the yanked text. In
1536 ;; either case, leave a mark at the opposite end of the included
1537 ;; text to make it easy to jump or delete to the other end of the
1538 ;; text.
1539 (push-mark)
1540 (goto-char (point-max))
1541 (if (null yank-region)
1542 (mh-exchange-point-and-mark-preserving-active-mark)))))
1543 (error "There is no current message")))
1544
1545 (defun mh-filter-out-non-text (string)
1546 "Return STRING but without adornments such as MIME buttons and smileys."
1547 (with-temp-buffer
1548 ;; Insert the string to filter
1549 (insert string)
1550 (goto-char (point-min))
1551
1552 ;; Remove the MIME buttons
1553 (let ((can-move-forward t)
1554 (in-button nil))
1555 (while can-move-forward
1556 (cond ((and (not (get-text-property (point) 'mh-data))
1557 in-button)
1558 (delete-region (1- (point)) (point))
1559 (setq in-button nil))
1560 ((get-text-property (point) 'mh-data)
1561 (delete-region (point)
1562 (save-excursion (forward-line) (point)))
1563 (setq in-button t))
1564 (t (setq can-move-forward (= (forward-line) 0))))))
1565
1566 ;; Return the contents without properties... This gets rid of emphasis
1567 ;; and smileys
1568 (buffer-substring-no-properties (point-min) (point-max))))
1569
1570 (defun mh-insert-prefix-string (mh-ins-string)
1571 "Insert prefix string before each line in buffer.
1572 The inserted letter is cited using `sc-cite-original' if
1573 `mh-yank-from-start-of-msg' is one of 'supercite or 'autosupercite. Otherwise,
1574 simply insert MH-INS-STRING before each line."
1575 (goto-char (point-min))
1576 (cond ((or (eq mh-yank-from-start-of-msg 'supercite)
1577 (eq mh-yank-from-start-of-msg 'autosupercite))
1578 (sc-cite-original))
1579 (mail-citation-hook
1580 (run-hooks 'mail-citation-hook))
1581 (mh-yank-hooks ;old hook name
1582 (run-hooks 'mh-yank-hooks))
1583 (t
1584 (or (bolp) (forward-line 1))
1585 (while (< (point) (point-max))
1586 (insert mh-ins-string)
1587 (forward-line 1))
1588 (goto-char (point-min))))) ;leave point like sc-cite-original
1589
1590 ;;;###mh-autoload
1591 (defun mh-fully-kill-draft ()
1592 "Kill the draft message file and the draft message buffer.
1593 Use \\[kill-buffer] if you don't want to delete the draft message file."
1594 (interactive)
1595 (if (y-or-n-p "Kill draft message? ")
1596 (let ((config mh-previous-window-config))
1597 (if (file-exists-p buffer-file-name)
1598 (delete-file buffer-file-name))
1599 (set-buffer-modified-p nil)
1600 (kill-buffer (buffer-name))
1601 (message "")
1602 (if config
1603 (set-window-configuration config)))
1604 (error "Message not killed")))
1605
1606 (defun mh-current-fill-prefix ()
1607 "Return the `fill-prefix' on the current line as a string."
1608 (save-excursion
1609 (beginning-of-line)
1610 ;; This assumes that the major-mode sets up adaptive-fill-regexp
1611 ;; correctly such as mh-letter-mode or sendmail.el's mail-mode. But
1612 ;; perhaps I should use the variable and simply inserts its value here,
1613 ;; and set it locally in a let scope. --psg
1614 (if (re-search-forward adaptive-fill-regexp nil t)
1615 (match-string 0)
1616 "")))
1617
1618 ;;;###mh-autoload
1619 (defun mh-open-line ()
1620 "Insert a newline and leave point after it.
1621 In addition, insert newline and quoting characters before text after point.
1622 This is useful in breaking up paragraphs in replies."
1623 (interactive)
1624 (let ((column (current-column))
1625 (prefix (mh-current-fill-prefix)))
1626 (if (> (length prefix) column)
1627 (message "Sorry, point seems to be within the line prefix")
1628 (newline 2)
1629 (insert prefix)
1630 (while (> column (current-column))
1631 (insert " "))
1632 (forward-line -1))))
1633
1634 (mh-do-in-xemacs (defvar mail-abbrevs))
1635
1636 ;;;###mh-autoload
1637 (defun mh-complete-word (word choices begin end)
1638 "Complete WORD at from CHOICES.
1639 Any match found replaces the text from BEGIN to END."
1640 (let ((completion (try-completion word choices))
1641 (completions-buffer "*Completions*"))
1642 (cond ((eq completion t)
1643 (ignore-errors
1644 (kill-buffer completions-buffer))
1645 (message "Completed: %s" word))
1646 ((null completion)
1647 (ignore-errors
1648 (kill-buffer completions-buffer))
1649 (message "No completion for `%s'" word))
1650 ((stringp completion)
1651 (if (equal word completion)
1652 (with-output-to-temp-buffer completions-buffer
1653 (display-completion-list (all-completions word choices)))
1654 (ignore-errors
1655 (kill-buffer completions-buffer))
1656 (delete-region begin end)
1657 (insert completion))))))
1658
1659 ;;;###mh-autoload
1660 (defun mh-beginning-of-word (&optional n)
1661 "Return position of the N th word backwards."
1662 (unless n (setq n 1))
1663 (let ((syntax-table (syntax-table)))
1664 (unwind-protect
1665 (save-excursion
1666 (mh-mail-abbrev-make-syntax-table)
1667 (set-syntax-table mail-abbrev-syntax-table)
1668 (backward-word n)
1669 (point))
1670 (set-syntax-table syntax-table))))
1671
1672 (defun mh-folder-expand-at-point ()
1673 "Do folder name completion in Fcc header field."
1674 (let* ((end (point))
1675 (beg (mh-beginning-of-word))
1676 (folder (buffer-substring beg end))
1677 (leading-plus (and (> (length folder) 0) (equal (aref folder 0) ?+)))
1678 (last-slash (mh-search-from-end ?/ folder))
1679 (prefix (and last-slash (substring folder 0 last-slash)))
1680 (choices (mapcar #'(lambda (x)
1681 (list (cond (prefix (format "%s/%s" prefix x))
1682 (leading-plus (format "+%s" x))
1683 (t x))))
1684 (mh-folder-completion-function folder nil t))))
1685 (mh-complete-word folder choices beg end)))
1686
1687 (defvar mh-letter-complete-function-alist
1688 '((cc . mh-alias-letter-expand-alias)
1689 (bcc . mh-alias-letter-expand-alias)
1690 (dcc . mh-alias-letter-expand-alias)
1691 (fcc . mh-folder-expand-at-point)
1692 (from . mh-alias-letter-expand-alias)
1693 (mail-followup-to . mh-alias-letter-expand-alias)
1694 (reply-to . mh-alias-letter-expand-alias)
1695 (to . mh-alias-letter-expand-alias))
1696 "Alist of header fields and completion functions to use.")
1697
1698 (defun mh-letter-complete (arg)
1699 "Perform completion on header field or word preceding point.
1700 If the field contains addresses (for example, `To:' or `Cc:') or folders (for
1701 example, `Fcc:') then this function will provide alias completion. Elsewhere,
1702 this function runs `mh-letter-complete-function' instead and passes the prefix
1703 ARG, if present."
1704 (interactive "P")
1705 (let ((func nil))
1706 (cond ((not (mh-in-header-p))
1707 (funcall mh-letter-complete-function arg))
1708 ((setq func (cdr (assoc (mh-letter-header-field-at-point)
1709 mh-letter-complete-function-alist)))
1710 (funcall func))
1711 (t (funcall mh-letter-complete-function arg)))))
1712
1713 (defun mh-letter-complete-or-space (arg)
1714 "Perform completion or insert space.
1715 If `mh-compose-space-does-completion-flag' is nil (the default) a space is
1716 inserted.
1717
1718 Otherwise, if point is in the message header and the preceding character is
1719 not whitespace then do completion. Otherwise insert a space character.
1720
1721 ARG is the number of spaces inserted."
1722 (interactive "p")
1723 (let ((func nil)
1724 (end-of-prev (save-excursion
1725 (goto-char (mh-beginning-of-word))
1726 (mh-beginning-of-word -1))))
1727 (cond ((not mh-compose-space-does-completion-flag)
1728 (self-insert-command arg))
1729 ((not (mh-in-header-p)) (self-insert-command arg))
1730 ((> (point) end-of-prev) (self-insert-command arg))
1731 ((setq func (cdr (assoc (mh-letter-header-field-at-point)
1732 mh-letter-complete-function-alist)))
1733 (funcall func))
1734 (t (self-insert-command arg)))))
1735
1736 (defun mh-letter-confirm-address ()
1737 "Flash alias expansion if `mh-alias-flash-on-comma' is non-nil."
1738 (interactive)
1739 (cond ((not (mh-in-header-p)) (self-insert-command 1))
1740 ((eq (cdr (assoc (mh-letter-header-field-at-point)
1741 mh-letter-complete-function-alist))
1742 'mh-alias-letter-expand-alias)
1743 (mh-alias-reload-maybe)
1744 (mh-alias-minibuffer-confirm-address))
1745 (t (self-insert-command 1))))
1746
1747 (defvar mh-letter-header-field-regexp "^\\([A-Za-z][A-Za-z0-9-]*\\):")
1748
1749 (defun mh-letter-header-field-at-point ()
1750 "Return the header field name at point.
1751 A symbol is returned whose name is the string obtained by downcasing the field
1752 name."
1753 (save-excursion
1754 (end-of-line)
1755 (and (re-search-backward mh-letter-header-field-regexp nil t)
1756 (intern (downcase (match-string 1))))))
1757
1758 ;;;###mh-autoload
1759 (defun mh-letter-next-header-field-or-indent (arg)
1760 "Move to next field or indent depending on point.
1761 In the message header, go to the next field. Elsewhere call
1762 `indent-relative' as usual with optional prefix ARG."
1763 (interactive "P")
1764 (let ((header-end (save-excursion
1765 (goto-char (mh-mail-header-end))
1766 (forward-line)
1767 (point))))
1768 (if (> (point) header-end)
1769 (indent-relative arg)
1770 (mh-letter-next-header-field))))
1771
1772 (defun mh-letter-next-header-field ()
1773 "Cycle to the next header field.
1774 If we are at the last header field go to the start of the message body."
1775 (let ((header-end (mh-mail-header-end)))
1776 (cond ((>= (point) header-end) (goto-char (point-min)))
1777 ((< (point) (progn
1778 (beginning-of-line)
1779 (re-search-forward mh-letter-header-field-regexp
1780 (line-end-position) t)
1781 (point)))
1782 (beginning-of-line))
1783 (t (end-of-line)))
1784 (cond ((re-search-forward mh-letter-header-field-regexp header-end t)
1785 (if (mh-letter-skipped-header-field-p (match-string 1))
1786 (mh-letter-next-header-field)
1787 (mh-letter-skip-leading-whitespace-in-header-field)))
1788 (t (goto-char header-end)
1789 (forward-line)))))
1790
1791 ;;;###mh-autoload
1792 (defun mh-letter-previous-header-field ()
1793 "Cycle to the previous header field.
1794 If we are at the first header field go to the start of the message body."
1795 (interactive)
1796 (let ((header-end (mh-mail-header-end)))
1797 (if (>= (point) header-end)
1798 (goto-char header-end)
1799 (mh-header-field-beginning))
1800 (cond ((re-search-backward mh-letter-header-field-regexp nil t)
1801 (if (mh-letter-skipped-header-field-p (match-string 1))
1802 (mh-letter-previous-header-field)
1803 (goto-char (match-end 0))
1804 (mh-letter-skip-leading-whitespace-in-header-field)))
1805 (t (goto-char header-end)
1806 (forward-line)))))
1807
1808 (defun mh-letter-skipped-header-field-p (field)
1809 "Check if FIELD is to be skipped."
1810 (let ((field (downcase field)))
1811 (loop for x in mh-compose-skipped-header-fields
1812 when (equal (downcase x) field) return t
1813 finally return nil)))
1814
1815 (defun mh-letter-skip-leading-whitespace-in-header-field ()
1816 "Skip leading whitespace in a header field.
1817 If the header field doesn't have at least one space after the colon then a
1818 space character is added."
1819 (let ((need-space t))
1820 (while (memq (char-after) '(?\t ?\ ))
1821 (forward-char)
1822 (setq need-space nil))
1823 (when need-space (insert " "))))
1824
1825 (defvar mh-hidden-header-keymap
1826 (let ((map (make-sparse-keymap)))
1827 (mh-do-in-gnu-emacs
1828 (define-key map [mouse-2] 'mh-letter-toggle-header-field-display-button))
1829 (mh-do-in-xemacs
1830 (define-key map '(button2)
1831 'mh-letter-toggle-header-field-display-button))
1832 map))
1833
1834 (defun mh-letter-toggle-header-field-display-button (event)
1835 "Toggle header field display at location of EVENT.
1836 This function does the same thing as `mh-letter-toggle-header-field-display'
1837 except that it is callable from a mouse button."
1838 (interactive "e")
1839 (mh-do-at-event-location event
1840 (mh-letter-toggle-header-field-display nil)))
1841
1842 (defun mh-letter-toggle-header-field-display (arg)
1843 "Toggle display of header field at point.
1844 If the header is long or spread over multiple lines then hiding it will show
1845 the first few characters and replace the rest with an ellipsis.
1846
1847 If ARG is negative then header is hidden, if positive it is displayed. If ARG
1848 is the symbol `long' then keep at most the first 4 lines."
1849 (interactive (list nil))
1850 (when (and (mh-in-header-p)
1851 (progn
1852 (end-of-line)
1853 (re-search-backward mh-letter-header-field-regexp nil t)))
1854 (let ((buffer-read-only nil)
1855 (modified-flag (buffer-modified-p))
1856 (begin (point))
1857 end)
1858 (end-of-line)
1859 (setq end (1- (if (re-search-forward "^[^ \t]" nil t)
1860 (match-beginning 0)
1861 (point-max))))
1862 (goto-char begin)
1863 ;; Make it clickable...
1864 (add-text-properties begin end `(keymap ,mh-hidden-header-keymap
1865 mouse-face highlight))
1866 (unwind-protect
1867 (cond ((or (and (not arg)
1868 (text-property-any begin end 'invisible 'vanish))
1869 (and (numberp arg) (>= arg 0))
1870 (and (eq arg 'long) (> (line-beginning-position 5) end)))
1871 (remove-text-properties begin end '(invisible nil))
1872 (search-forward ":" (line-end-position) t)
1873 (mh-letter-skip-leading-whitespace-in-header-field))
1874 ((eq arg 'long)
1875 (end-of-line 4)
1876 (mh-letter-truncate-header-field end)
1877 (beginning-of-line))
1878 (t (end-of-line)
1879 (mh-letter-truncate-header-field end)
1880 (beginning-of-line)))
1881 (set-buffer-modified-p modified-flag)))))
1882
1883 (defun mh-letter-truncate-header-field (end)
1884 "Replace text from current line till END with an ellipsis.
1885 If the current line is too long truncate a part of it as well."
1886 (let ((max-len (min (window-width) 62)))
1887 (when (> (+ (current-column) 4) max-len)
1888 (backward-char (- (+ (current-column) 5) max-len)))
1889 (when (> end (point))
1890 (add-text-properties (point) end '(invisible vanish)))))
1891
1892 (defun mh-letter-hide-all-skipped-fields ()
1893 "Hide all skipped fields."
1894 (save-excursion
1895 (goto-char (point-min))
1896 (save-restriction
1897 (narrow-to-region (point) (mh-mail-header-end))
1898 (while (re-search-forward mh-letter-header-field-regexp nil t)
1899 (if (mh-letter-skipped-header-field-p (match-string 1))
1900 (mh-letter-toggle-header-field-display -1)
1901 (mh-letter-toggle-header-field-display 'long))
1902 (beginning-of-line 2)))))
1903
1904 (defun mh-interactive-read-address (prompt)
1905 "Read an address.
1906 If `mh-compose-prompt-flag' is non-nil, then read an address with PROMPT.
1907 Otherwise return the empty string."
1908 (if mh-compose-prompt-flag (mh-read-address prompt) ""))
1909
1910 (defun mh-interactive-read-string (prompt)
1911 "Read a string.
1912 If `mh-compose-prompt-flag' is non-nil, then read a string with PROMPT.
1913 Otherwise return the empty string."
1914 (if mh-compose-prompt-flag (read-string prompt) ""))
1915
1916 (defun mh-letter-adjust-point ()
1917 "Move cursor to first header field if are using the no prompt mode."
1918 (unless mh-compose-prompt-flag
1919 (goto-char (point-max))
1920 (mh-letter-next-header-field)))
1921
1922 ;;; Build the letter-mode keymap:
1923 ;;; If this changes, modify mh-letter-mode-help-messages accordingly, above.
1924 (gnus-define-keys mh-letter-mode-map
1925 " " mh-letter-complete-or-space
1926 "," mh-letter-confirm-address
1927 "\C-c?" mh-help
1928 "\C-c\C-\\" mh-fully-kill-draft ;if no C-q
1929 "\C-c\C-^" mh-insert-signature ;if no C-s
1930 "\C-c\C-c" mh-send-letter
1931 "\C-c\C-d" mh-insert-identity
1932 "\C-c\C-e" mh-edit-mhn
1933 "\C-c\C-f\C-b" mh-to-field
1934 "\C-c\C-f\C-c" mh-to-field
1935 "\C-c\C-f\C-d" mh-to-field
1936 "\C-c\C-f\C-f" mh-to-fcc
1937 "\C-c\C-f\C-r" mh-to-field
1938 "\C-c\C-f\C-s" mh-to-field
1939 "\C-c\C-f\C-t" mh-to-field
1940 "\C-c\C-fb" mh-to-field
1941 "\C-c\C-fc" mh-to-field
1942 "\C-c\C-fd" mh-to-field
1943 "\C-c\C-ff" mh-to-fcc
1944 "\C-c\C-fr" mh-to-field
1945 "\C-c\C-fs" mh-to-field
1946 "\C-c\C-ft" mh-to-field
1947 "\C-c\C-i" mh-insert-letter
1948 "\C-c\C-m\C-e" mh-mml-secure-message-encrypt
1949 "\C-c\C-m\C-f" mh-compose-forward
1950 "\C-c\C-m\C-g" mh-mhn-compose-anon-ftp
1951 "\C-c\C-m\C-i" mh-compose-insertion
1952 "\C-c\C-m\C-m" mh-mml-to-mime
1953 "\C-c\C-m\C-n" mh-mml-unsecure-message
1954 "\C-c\C-m\C-s" mh-mml-secure-message-sign
1955 "\C-c\C-m\C-t" mh-mhn-compose-external-compressed-tar
1956 "\C-c\C-m\C-u" mh-revert-mhn-edit
1957 "\C-c\C-m\C-x" mh-mhn-compose-external-type
1958 "\C-c\C-mee" mh-mml-secure-message-encrypt
1959 "\C-c\C-mes" mh-mml-secure-message-signencrypt
1960 "\C-c\C-mf" mh-compose-forward
1961 "\C-c\C-mg" mh-mhn-compose-anon-ftp
1962 "\C-c\C-mi" mh-compose-insertion
1963 "\C-c\C-mm" mh-mml-to-mime
1964 "\C-c\C-mn" mh-mml-unsecure-message
1965 "\C-c\C-mse" mh-mml-secure-message-signencrypt
1966 "\C-c\C-mss" mh-mml-secure-message-sign
1967 "\C-c\C-mt" mh-mhn-compose-external-compressed-tar
1968 "\C-c\C-mu" mh-revert-mhn-edit
1969 "\C-c\C-mx" mh-mhn-compose-external-type
1970 "\C-c\C-o" mh-open-line
1971 "\C-c\C-q" mh-fully-kill-draft
1972 "\C-c\C-s" mh-insert-signature
1973 "\C-c\C-t" mh-letter-toggle-header-field-display
1974 "\C-c\C-w" mh-check-whom
1975 "\C-c\C-y" mh-yank-cur-msg
1976 "\C-c\M-d" mh-insert-auto-fields
1977 "\M-\t" mh-letter-complete
1978 "\t" mh-letter-next-header-field-or-indent
1979 [backtab] mh-letter-previous-header-field)
1980
1981 ;; "C-c /" prefix is used in mh-letter-mode by pgp.el and mailcrypt.el.
1982
1983 (provide 'mh-comp)
1984
1985 ;;; Local Variables:
1986 ;;; indent-tabs-mode: nil
1987 ;;; sentence-end-double-space: nil
1988 ;;; End:
1989
1990 ;;; arch-tag: 62865511-e610-4923-b0b5-f45a8ab70a34
1991 ;;; mh-comp.el ends here