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