1 ;;; mh-letter.el --- MH-Letter mode
3 ;; Copyright (C) 1993, 1995, 1997, 2000-2011 Free Software Foundation, Inc.
5 ;; Author: Bill Wohler <wohler@newt.com>
6 ;; Maintainer: Bill Wohler <wohler@newt.com>
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;; Mode for composing and sending a draft message.
29 ;; Functions that would ordinarily be in here that are needed by
30 ;; mh-show.el should be placed in the Message Utilities section in
31 ;; mh-utils.el. That will help prevent the loading of this file until
32 ;; a message is actually composed.
42 ;; Dynamically-created functions not found in mh-loaddefs.el.
43 (autoload 'mh-tool-bar-letter-buttons-init
"mh-tool-bar")
44 (autoload 'mh-tool-bar-init
"mh-tool-bar")
46 (autoload 'mml-insert-tag
"mml")
50 (defvar mh-letter-complete-function-alist
51 '((bcc . mh-alias-letter-expand-alias
)
52 (cc . mh-alias-letter-expand-alias
)
53 (dcc . mh-alias-letter-expand-alias
)
54 (fcc . mh-folder-expand-at-point
)
55 (from . mh-alias-letter-expand-alias
)
56 (mail-followup-to . mh-alias-letter-expand-alias
)
57 (mail-reply-to . mh-alias-letter-expand-alias
)
58 (reply-to . mh-alias-letter-expand-alias
)
59 (to . mh-alias-letter-expand-alias
))
60 "Alist of header fields and completion functions to use.")
62 (defvar mh-yank-hooks nil
63 "Obsolete hook for modifying a citation just inserted in the mail buffer.
65 Each hook function can find the citation between point and mark.
66 And each hook function should leave point and mark around the
67 citation text as modified.
69 This is a normal hook, misnamed for historical reasons. It is
70 semi-obsolete and is only used if `mail-citation-hook' is nil.")
77 mh-letter-menu mh-letter-mode-map
"Menu for MH-E letter mode."
79 ["Send This Draft" mh-send-letter t
]
80 ["Split Current Line" mh-open-line t
]
81 ["Check Recipient" mh-check-whom t
]
82 ["Yank Current Message" mh-yank-cur-msg t
]
83 ["Insert a Message..." mh-insert-letter t
]
84 ["Insert Signature" mh-insert-signature t
]
85 ("Encrypt/Sign Message"
87 mh-mml-secure-message-sign mh-pgp-support-flag
]
89 mh-mml-secure-message-encrypt mh-pgp-support-flag
]
90 ["Sign+Encrypt Message"
91 mh-mml-secure-message-signencrypt mh-pgp-support-flag
]
93 mh-mml-unsecure-message mh-pgp-support-flag
]
96 ["PGP (MIME)" (setq mh-mml-method-default
"pgpmime")
98 :selected
(equal mh-mml-method-default
"pgpmime")]
99 ["PGP" (setq mh-mml-method-default
"pgp")
101 :selected
(equal mh-mml-method-default
"pgp")]
102 ["S/MIME" (setq mh-mml-method-default
"smime")
104 :selected
(equal mh-mml-method-default
"smime")]
106 ["Save Method as Default"
107 (customize-save-variable 'mh-mml-method-default mh-mml-method-default
) t
]
109 ["Compose Insertion..." mh-compose-insertion t
]
110 ["Compose Compressed tar (MH)..."
111 mh-mh-compose-external-compressed-tar t
]
112 ["Compose Get File (MH)..." mh-mh-compose-anon-ftp t
]
113 ["Compose Forward..." mh-compose-forward t
]
114 ;; The next two will have to be merged. But I also need to make sure the
115 ;; user can't mix tags of both types.
116 ["Pull in All Compositions (MH)"
117 mh-mh-to-mime
(mh-mh-directive-present-p)]
118 ["Pull in All Compositions (MML)"
119 mh-mml-to-mime
(mh-mml-tag-present-p)]
120 ["Revert to Non-MIME Edit (MH)"
121 mh-mh-to-mime-undo
(equal mh-compose-insertion
'mh
)]
122 ["Kill This Draft" mh-fully-kill-draft t
]))
128 ;; If this changes, modify mh-letter-mode-help-messages accordingly, above.
129 (gnus-define-keys mh-letter-mode-map
130 " " mh-letter-complete-or-space
131 "," mh-letter-confirm-address
133 "\C-c\C-\\" mh-fully-kill-draft
;if no C-q
134 "\C-c\C-^" mh-insert-signature
;if no C-s
135 "\C-c\C-c" mh-send-letter
136 "\C-c\C-d" mh-insert-identity
137 "\C-c\C-e" mh-mh-to-mime
138 "\C-c\C-f\C-a" mh-to-field
139 "\C-c\C-f\C-b" mh-to-field
140 "\C-c\C-f\C-c" mh-to-field
141 "\C-c\C-f\C-d" mh-to-field
142 "\C-c\C-f\C-f" mh-to-fcc
143 "\C-c\C-f\C-l" mh-to-field
144 "\C-c\C-f\C-m" mh-to-field
145 "\C-c\C-f\C-r" mh-to-field
146 "\C-c\C-f\C-s" mh-to-field
147 "\C-c\C-f\C-t" mh-to-field
148 "\C-c\C-fa" mh-to-field
149 "\C-c\C-fb" mh-to-field
150 "\C-c\C-fc" mh-to-field
151 "\C-c\C-fd" mh-to-field
152 "\C-c\C-ff" mh-to-fcc
153 "\C-c\C-fl" mh-to-field
154 "\C-c\C-fm" mh-to-field
155 "\C-c\C-fr" mh-to-field
156 "\C-c\C-fs" mh-to-field
157 "\C-c\C-ft" mh-to-field
158 "\C-c\C-i" mh-insert-letter
159 "\C-c\C-m\C-e" mh-mml-secure-message-encrypt
160 "\C-c\C-m\C-f" mh-compose-forward
161 "\C-c\C-m\C-g" mh-mh-compose-anon-ftp
162 "\C-c\C-m\C-i" mh-compose-insertion
163 "\C-c\C-m\C-m" mh-mml-to-mime
164 "\C-c\C-m\C-n" mh-mml-unsecure-message
165 "\C-c\C-m\C-s" mh-mml-secure-message-sign
166 "\C-c\C-m\C-t" mh-mh-compose-external-compressed-tar
167 "\C-c\C-m\C-u" mh-mh-to-mime-undo
168 "\C-c\C-m\C-x" mh-mh-compose-external-type
169 "\C-c\C-mee" mh-mml-secure-message-encrypt
170 "\C-c\C-mes" mh-mml-secure-message-signencrypt
171 "\C-c\C-mf" mh-compose-forward
172 "\C-c\C-mg" mh-mh-compose-anon-ftp
173 "\C-c\C-mi" mh-compose-insertion
174 "\C-c\C-mm" mh-mml-to-mime
175 "\C-c\C-mn" mh-mml-unsecure-message
176 "\C-c\C-mse" mh-mml-secure-message-signencrypt
177 "\C-c\C-mss" mh-mml-secure-message-sign
178 "\C-c\C-mt" mh-mh-compose-external-compressed-tar
179 "\C-c\C-mu" mh-mh-to-mime-undo
180 "\C-c\C-mx" mh-mh-compose-external-type
181 "\C-c\C-o" mh-open-line
182 "\C-c\C-q" mh-fully-kill-draft
183 "\C-c\C-s" mh-insert-signature
184 "\C-c\C-t" mh-letter-toggle-header-field-display
185 "\C-c\C-w" mh-check-whom
186 "\C-c\C-y" mh-yank-cur-msg
187 "\C-c\M-d" mh-insert-auto-fields
188 "\M-\t" mh-letter-complete
189 "\t" mh-letter-next-header-field-or-indent
190 [backtab] mh-letter-previous-header-field)
192 ;; "C-c /" prefix is used in mh-letter-mode by pgp.el and mailcrypt.el.
196 ;;; MH-Letter Help Messages
198 ;; Group messages logically, more or less.
199 (defvar mh-letter-mode-help-messages
201 "Send letter: \\[mh-send-letter] "
202 "Open line: \\[mh-open-line]\n"
203 "Kill letter: \\[mh-fully-kill-draft] "
204 "Check recipients: \\[mh-check-whom]\n\n"
206 " Current message: \\[mh-yank-cur-msg]\n"
207 " Attachment: \\[mh-compose-insertion]\n"
208 " Message to forward: \\[mh-compose-forward]\n"
209 " Signature: \\[mh-insert-signature]\n\n"
211 " Encrypt message: \\[mh-mml-secure-message-encrypt]\n"
212 " Sign message: \\[mh-mml-secure-message-sign]\n"
213 " Sign+Encrypt message: \\[mh-mml-secure-message-signencrypt]"))
214 "Key binding cheat sheet.
216 This is an associative array which is used to show the most
217 common commands. The key is a prefix char. The value is one or
218 more strings which are concatenated together and displayed in the
219 minibuffer if ? is pressed after the prefix character. The
220 special key nil is used to display the non-prefixed commands.
222 The substitutions described in `substitute-command-keys' are
227 ;;; MH-Letter Font Lock
229 (defvar mh-letter-font-lock-keywords
230 `(,@(mh-show-font-lock-keywords-with-cite)
231 (mh-font-lock-field-data
232 (1 'mh-letter-header-field prepend t)))
233 "Additional expressions to highlight in MH-Letter buffers.")
235 (defun mh-font-lock-field-data (limit)
236 "Find header field region between point and LIMIT."
237 (and (< (point) (mh-letter-header-end))
239 (let ((end (min limit (mh-letter-header-end)))
241 data-end data-begin field)
243 (setq data-end (if (re-search-forward "^[^ \t]" end t)
246 (goto-char (1- data-end))
247 (if (not (re-search-backward "\\(^[^ \t][^:]*\\):[ \t]*" nil t))
248 (setq data-begin (point-min))
249 (setq data-begin (match-end 0))
250 (setq field (match-string 1)))
251 (setq data-begin (max point data-begin))
252 (goto-char (if (equal point data-end) (1+ data-end) data-end))
253 (cond ((and field (mh-letter-skipped-header-field-p field))
257 (list data-begin data-end data-begin data-end))
260 (defun mh-letter-header-end ()
261 "Find the end of the message header.
262 This function is to be used only for font locking. It works by
263 searching for `mh-mail-header-separator' in the buffer."
265 (goto-char (point-min))
266 (cond ((equal mh-mail-header-separator "") (point-min))
267 ((search-forward (format "\n%s\n" mh-mail-header-separator) nil t)
268 (mh-line-beginning-position 0))
277 (defvar font-lock-defaults))
279 ;; Ensure new buffers won't get this mode if default major-mode is nil.
280 (put 'mh-letter-mode 'mode-class 'special)
283 (define-derived-mode mh-letter-mode mail-mode "MH-Letter"
284 "Mode for composing letters in MH-E\\<mh-letter-mode-map>.
286 When you have finished composing, type \\[mh-send-letter] to send
287 the message using the MH mail handling system.
289 There are two types of tags used by MH-E when composing MIME
290 messages: MML and MH. The option `mh-compose-insertion' controls
291 what type of tags are inserted by MH-E commands. These tags can
292 be converted to MIME body parts by running \\[mh-mh-to-mime] for
293 MH-style directives or \\[mh-mml-to-mime] for MML tags.
295 Options that control this mode can be changed with
296 \\[customize-group]; specify the \"mh-compose\" group.
298 When a message is composed, the hooks `text-mode-hook',
299 `mail-mode-hook', and `mh-letter-mode-hook' are run (in that
302 \\{mh-letter-mode-map}"
304 (make-local-variable 'mh-send-args)
305 (make-local-variable 'mh-annotate-char)
306 (make-local-variable 'mh-annotate-field)
307 (make-local-variable 'mh-previous-window-config)
308 (make-local-variable 'mh-sent-from-folder)
309 (make-local-variable 'mh-sent-from-msg)
311 (unless mh-letter-tool-bar-map
312 (mh-tool-bar-letter-buttons-init))
313 (if (boundp 'tool-bar-map)
314 (set (make-local-variable 'tool-bar-map) mh-letter-tool-bar-map)))
316 (mh-tool-bar-init :letter))
317 ;; Set the local value of mh-mail-header-separator according to what is
318 ;; present in the buffer...
319 (set (make-local-variable 'mh-mail-header-separator)
321 (goto-char (mh-mail-header-end))
322 (buffer-substring-no-properties (point) (mh-line-end-position))))
323 (make-local-variable 'mail-header-separator)
324 (setq mail-header-separator mh-mail-header-separator) ;override sendmail.el
325 (mh-set-help mh-letter-mode-help-messages)
326 (setq buffer-invisibility-spec '((vanish . t) t))
327 (set (make-local-variable 'line-move-ignore-invisible) t)
329 ;; Enable undo since a show-mode buffer might have been reused.
331 (make-local-variable 'font-lock-defaults)
333 ((or (equal mh-highlight-citation-style 'font-lock)
334 (equal mh-highlight-citation-style 'gnus))
335 ;; Let's use font-lock even if gnus is used in show-mode. The reason
336 ;; is that gnus uses static text properties which are not appropriate
337 ;; for a buffer that will be edited. So the choice here is either fontify
338 ;; the citations and header...
339 (setq font-lock-defaults '(mh-letter-font-lock-keywords t)))
341 ;; ...or the header only
342 (setq font-lock-defaults '((mh-show-font-lock-keywords) t))))
343 (easy-menu-add mh-letter-menu)
344 ;; Maybe we want to use the existing Mail menu from mail-mode in
345 ;; 9.0; in the mean time, let's remove it since the redundancy will
346 ;; only produce confusion.
347 (define-key mh-letter-mode-map [menu-bar mail] 'undefined)
348 (mh-do-in-xemacs (easy-menu-remove mail-menubar-menu))
349 (setq fill-column mh-letter-fill-column)
350 (add-hook 'completion-at-point-functions
351 'mh-letter-completion-at-point nil 'local)
352 ;; If text-mode-hook turned on auto-fill, tune it for messages
353 (when auto-fill-function
354 (make-local-variable 'auto-fill-function)
355 (setq auto-fill-function 'mh-auto-fill-for-letter)))
359 ;;; MH-Letter Commands
362 ;; See also mh-comp.el and mh-mime.el.
364 (defun mh-check-whom ()
365 "Verify recipients, showing expansion of any aliases.
367 This command expands aliases so you can check the actual address(es)
368 in the alias. A new buffer named \"*MH-E Recipients*\" is created with
369 the output of \"whom\"."
371 (let ((file-name buffer-file-name))
373 (message "Checking recipients...")
374 (mh-in-show-buffer (mh-recipients-buffer)
375 (bury-buffer (current-buffer))
377 (mh-exec-cmd-output "whom" t file-name))
378 (message "Checking recipients...done")))
380 (defun mh-insert-letter (folder message verbatim)
383 This command prompts you for the FOLDER and MESSAGE number, which
384 defaults to the current message in that folder. It then inserts
385 the message, indented by `mh-ins-buf-prefix' (\"> \") unless
386 `mh-yank-behavior' is set to one of the supercite flavors in
387 which case supercite is used to format the message. Certain
388 undesirable header fields (see
389 `mh-invisible-header-fields-compiled') are removed before
392 If given a prefix argument VERBATIM, the header is left intact, the
393 message is not indented, and \"> \" is not inserted before each line.
394 This command leaves the mark before the letter and point after it."
397 (mh-prompt-for-folder "Message from" mh-sent-from-folder nil))
399 (if (equal folder mh-sent-from-folder)
400 (or mh-sent-from-msg (nth 0 (mh-translate-range folder "cur")))
401 (nth 0 (mh-translate-range folder "cur"))))
403 (read-string (concat "Message number"
405 (format " (default %d): " default))
408 (if (numberp default)
409 (int-to-string default)
411 (list folder message current-prefix-arg)))
412 (if (equal message "")
413 (error "No message number given"))
415 (narrow-to-region (point) (point))
416 (let ((start (point-min)))
417 (insert-file-contents
418 (expand-file-name message (mh-expand-file-name folder)))
420 (mh-clean-msg-header start mh-invisible-header-fields-compiled nil)
421 (goto-char (point-max)) ;Needed for sc-cite-original
422 (push-mark) ;Needed for sc-cite-original
423 (goto-char (point-min)) ;Needed for sc-cite-original
424 (mh-insert-prefix-string mh-ins-buf-prefix)))))
427 (defun mh-insert-signature (&optional file)
428 "Insert signature in message.
430 This command inserts your signature at the current cursor location.
432 By default, the text of your signature is taken from the file
433 \"~/.signature\". You can read from other sources by changing the
434 option `mh-signature-file-name'.
436 A signature separator (\"-- \") will be added if the signature block
437 does not contain one and `mh-signature-separator-flag' is on.
439 The hook `mh-insert-signature-hook' is run after the signature is
440 inserted. Hook functions may access the actual name of the file or the
441 function used to insert the signature with `mh-signature-file-name'.
443 The signature can also be inserted using Identities (see
446 In a program, you can pass in a signature FILE."
450 (let ((mh-signature-file-name (or file mh-signature-file-name))
451 (mh-mh-p (mh-mh-directive-present-p))
452 (mh-mml-p (mh-mml-tag-present-p)))
454 (narrow-to-region (point) (point))
456 ((mh-file-is-vcard-p mh-signature-file-name)
457 (if (equal mh-compose-insertion 'mml)
458 (insert "<#part type=\"text/x-vcard\" filename=\""
459 mh-signature-file-name
460 "\" disposition=inline description=VCard>\n<#/part>")
461 (insert "#text/x-vcard; name=\""
462 (file-name-nondirectory mh-signature-file-name)
463 "\" [VCard] " (expand-file-name mh-signature-file-name))))
467 (insert "#\n" "Content-Description: Signature\n"))
469 (mml-insert-tag 'part 'type "text/plain" 'disposition "inline"
470 'description "Signature")))
471 (cond ((null mh-signature-file-name))
472 ((and (stringp mh-signature-file-name)
473 (file-readable-p mh-signature-file-name))
474 (insert-file-contents mh-signature-file-name))
475 ((functionp mh-signature-file-name)
476 (funcall mh-signature-file-name)))))
479 (run-hooks 'mh-insert-signature-hook))
480 (goto-char (point-min))
481 (when (and (not (mh-file-is-vcard-p mh-signature-file-name))
482 mh-signature-separator-flag
483 (> (point-max) (point-min))
484 (not (mh-signature-separator-p)))
489 (insert mh-signature-separator))
490 (if (not (> (point-max) (point-min)))
491 (message "No signature found")))))
492 (force-mode-line-update))
494 (defun mh-letter-completion-at-point ()
495 "Return the completion data at point for MH letters.
496 This provides alias and folder completion in header fields according to
497 `mh-letter-complete-function-alist' and falls back on
498 `mh-letter-complete-function-alist' elsewhere."
499 (let ((func (and (mh-in-header-p)
500 (cdr (assoc (mh-letter-header-field-at-point)
501 mh-letter-complete-function-alist)))))
503 (or (funcall func) #'ignore)
504 mh-letter-complete-function)))
506 ;; TODO Now that completion-at-point performs the task of
507 ;; mh-letter-complete, perhaps mh-letter-complete along with
508 ;; mh-complete-word should be rewritten as a more general function for
509 ;; XEmacs, renamed to mh-completion-at-point, and moved to
511 (defun-mh mh-letter-complete completion-at-point ()
512 "Perform completion on header field or word preceding point.
514 If the field contains addresses (for example, \"To:\" or \"Cc:\")
515 or folders (for example, \"Fcc:\") then this command will provide
516 alias completion. In the body of the message, this command runs
517 `mh-letter-complete-function' instead, which is set to
518 `ispell-complete-word' by default."
520 (let ((data (mh-letter-completion-at-point)))
522 ((functionp data) (funcall data))
524 (let ((start (nth 0 data))
526 (table (nth 2 data)))
527 (mh-complete-word (buffer-substring-no-properties start end)
528 table start end))))))
530 (defun mh-letter-complete-or-space (arg)
531 "Perform completion or insert space.
533 Turn on the option `mh-compose-space-does-completion-flag' to use
534 this command to perform completion in the header. Otherwise, a
535 space is inserted; use a prefix argument ARG to specify more than
538 (let ((end-of-prev (save-excursion
539 (goto-char (mh-beginning-of-word))
540 (mh-beginning-of-word -1))))
541 (cond ((not mh-compose-space-does-completion-flag)
542 (self-insert-command arg))
543 ;; FIXME: This > test is redundant now that all the completion
544 ;; functions do it anyway.
545 ((> (point) end-of-prev) (self-insert-command arg))
546 ((let ((mh-letter-complete-function nil))
547 (mh-letter-completion-at-point))
548 (mh-letter-complete))
549 (t (self-insert-command arg)))))
551 (defun mh-letter-confirm-address ()
552 "Flash alias expansion.
554 Addresses are separated by a comma\; when you press the comma,
555 this command flashes the alias expansion in the minibuffer if
556 `mh-alias-flash-on-comma' is turned on."
558 (cond ((not (mh-in-header-p)) (self-insert-command 1))
559 ((eq (cdr (assoc (mh-letter-header-field-at-point)
560 mh-letter-complete-function-alist))
561 'mh-alias-letter-expand-alias)
562 (mh-alias-reload-maybe)
563 (mh-alias-minibuffer-confirm-address))
564 (t (self-insert-command 1))))
566 (defun mh-letter-next-header-field-or-indent (arg)
567 "Cycle to next field.
569 Within the header of the message, this command moves between
570 fields that are highlighted with the face
571 `mh-letter-header-field', skipping those fields listed in
572 `mh-compose-skipped-header-fields'. After the last field, this
573 command then moves point to the message body before cycling back
574 to the first field. If point is already past the first line of
575 the message body, then this command indents by calling
576 `indent-relative' with the given prefix argument ARG."
578 (let ((header-end (save-excursion
579 (goto-char (mh-mail-header-end))
582 (if (> (point) header-end)
583 (indent-relative arg)
584 (mh-letter-next-header-field))))
586 (defun mh-letter-previous-header-field ()
587 "Cycle to the previous header field.
589 This command moves backwards between the fields and cycles to the
590 body of the message after the first field. Unlike the command
591 \\[mh-letter-next-header-field-or-indent], it will always take
592 point to the last field from anywhere in the body."
594 (let ((header-end (mh-mail-header-end)))
595 (if (>= (point) header-end)
596 (goto-char header-end)
597 (mh-header-field-beginning))
598 (cond ((re-search-backward mh-letter-header-field-regexp nil t)
599 (if (mh-letter-skipped-header-field-p (match-string 1))
600 (mh-letter-previous-header-field)
601 (goto-char (match-end 0))
602 (mh-letter-skip-leading-whitespace-in-header-field)))
603 (t (goto-char header-end)
606 (defun mh-open-line ()
607 "Insert a newline and leave point before it.
609 This command is similar to the command \\[open-line] in that it
610 inserts a newline after point. It differs in that it also inserts
611 the right number of quoting characters and spaces so that the
612 next line begins in the same column as it was. This is useful
613 when breaking up paragraphs in replies."
615 (let ((column (current-column))
616 (prefix (mh-current-fill-prefix)))
617 (if (> (length prefix) column)
618 (message "Sorry, point seems to be within the line prefix")
621 (while (> column (current-column))
625 (defun mh-to-fcc (&optional folder)
626 "Move to \"Fcc:\" header field.
628 This command will prompt you for the FOLDER name in which to file
629 a copy of the draft."
630 (interactive (list (mh-prompt-for-folder
632 (or (and mh-default-folder-for-message-function
634 (goto-char (point-min))
636 mh-default-folder-for-message-function)))
639 (let ((last-input-event ?\C-f))
643 (insert (if (mh-folder-name-p folder)
647 (defvar mh-to-field-choices '(("a" . "Mail-Reply-To:")
652 ("l" . "Mail-Followup-To:")
657 "Alist of (final-character . field-name) choices for `mh-to-field'.")
659 (defun mh-to-field ()
660 "Move to specified header field.
662 The field is indicated by the previous keystroke (the last
663 keystroke of the command) according to the list in the variable
664 `mh-to-field-choices'.
665 Create the field if it does not exist.
666 Set the mark to point before moving."
669 (let ((target (cdr (or (assoc (char-to-string (logior last-input-event ?`))
671 ;; also look for a char for version 4 compat
672 (assoc (logior last-input-event ?`)
673 mh-to-field-choices))))
674 (case-fold-search t))
676 (cond ((mh-position-on-field target)
678 (skip-chars-backward " \t")
679 (delete-region (point) eol))
680 (if (and (not (eq (logior last-input-event ?`) ?s))
683 (not (looking-at "[:,]"))))
687 (if (mh-position-on-field "To:")
689 (insert (format "%s \n" target))
690 (backward-char 1)))))
693 (defun mh-yank-cur-msg ()
694 "Insert the current message into the draft buffer.
696 It is often useful to insert a snippet of text from a letter that
697 someone mailed to provide some context for your reply. This
698 command does this by adding an attribution, yanking a portion of
699 text from the message to which you're replying, and inserting
700 `mh-ins-buf-prefix' (`> ') before each line.
702 The attribution consists of the sender's name and email address
703 followed by the content of the option
704 `mh-extract-from-attribution-verb'.
706 You can also turn on the option
707 `mh-delete-yanked-msg-window-flag' to delete the window
708 containing the original message after yanking it to make more
709 room on your screen for your reply.
711 You can control how the message to which you are replying is
712 yanked into your reply using `mh-yank-behavior'.
714 If this isn't enough, you can gain full control over the
715 appearance of the included text by setting `mail-citation-hook'
716 to a function that modifies it. For example, if you set this hook
717 to `trivial-cite' (which is NOT part of Emacs), set
718 `mh-yank-behavior' to \"Body and Header\" (see URL
719 `http://shasta.cs.uiuc.edu/~lrclause/tc.html').
721 Note that if `mail-citation-hook' is set, `mh-ins-buf-prefix' is
722 not inserted. If the option `mh-yank-behavior' is set to one of
723 the supercite flavors, the hook `mail-citation-hook' is ignored
724 and `mh-ins-buf-prefix' is not inserted."
726 (if (and mh-sent-from-folder
727 (with-current-buffer mh-sent-from-folder mh-show-buffer)
728 (with-current-buffer mh-sent-from-folder
729 (get-buffer mh-show-buffer))
731 (let ((to-point (point))
732 (to-buffer (current-buffer)))
733 (set-buffer mh-sent-from-folder)
734 (if mh-delete-yanked-msg-window-flag
735 (delete-windows-on mh-show-buffer))
736 (set-buffer mh-show-buffer) ; Find displayed message
737 (let* ((from-attr (mh-extract-from-attribution))
738 (yank-region (mh-mark-active-p nil))
740 (cond ((and yank-region
741 (or (eq 'supercite mh-yank-behavior)
742 (eq 'autosupercite mh-yank-behavior)
743 (eq t mh-yank-behavior)))
744 ;; supercite needs the full header
746 (buffer-substring (point-min) (mh-mail-header-end))
748 (buffer-substring (region-beginning) (region-end))))
750 (buffer-substring (region-beginning) (region-end)))
751 ((or (eq 'body mh-yank-behavior)
752 (eq 'attribution mh-yank-behavior)
753 (eq 'autoattrib mh-yank-behavior))
756 (goto-char (point-min))
757 (mh-goto-header-end 1)
760 ((or (eq 'supercite mh-yank-behavior)
761 (eq 'autosupercite mh-yank-behavior)
762 (eq t mh-yank-behavior))
763 (buffer-substring (point-min) (point-max)))
765 (buffer-substring (point) (point-max))))))
766 (set-buffer to-buffer)
768 (narrow-to-region to-point to-point)
769 (insert (mh-filter-out-non-text mh-ins-str))
770 (goto-char (point-max)) ;Needed for sc-cite-original
771 (push-mark) ;Needed for sc-cite-original
772 (goto-char (point-min)) ;Needed for sc-cite-original
773 (mh-insert-prefix-string mh-ins-buf-prefix)
774 (when (or (eq 'attribution mh-yank-behavior)
775 (eq 'autoattrib mh-yank-behavior))
777 (mh-identity-insert-attribution-verb nil)
779 ;; If the user has selected a region, he has already "edited" the
780 ;; text, so leave the cursor at the end of the yanked text. In
781 ;; either case, leave a mark at the opposite end of the included
782 ;; text to make it easy to jump or delete to the other end of the
785 (goto-char (point-max))
786 (if (null yank-region)
787 (mh-exchange-point-and-mark-preserving-active-mark)))))
788 (error "There is no current message")))
794 (defun mh-auto-fill-for-letter ()
795 "Perform auto-fill for message.
796 Header is treated specially by inserting a tab before continuation
799 (let ((fill-prefix "\t"))
803 (defun mh-filter-out-non-text (string)
804 "Return STRING but without adornments such as MIME buttons and smileys."
806 ;; Insert the string to filter
808 (goto-char (point-min))
810 ;; Remove the MIME buttons
811 (let ((can-move-forward t)
813 (while can-move-forward
814 (cond ((and (not (get-text-property (point) 'mh-data))
816 (delete-region (1- (point)) (point))
817 (setq in-button nil))
818 ((get-text-property (point) 'mh-data)
819 (delete-region (point)
820 (save-excursion (forward-line) (point)))
822 (t (setq can-move-forward (= (forward-line) 0))))))
824 ;; Return the contents without properties... This gets rid of emphasis
826 (buffer-substring-no-properties (point-min) (point-max))))
828 (defun mh-current-fill-prefix ()
829 "Return the `fill-prefix' on the current line as a string."
832 ;; This assumes that the major-mode sets up adaptive-fill-regexp
833 ;; correctly such as mh-letter-mode or sendmail.el's mail-mode. But
834 ;; perhaps I should use the variable and simply inserts its value here,
835 ;; and set it locally in a let scope. --psg
836 (if (re-search-forward adaptive-fill-regexp nil t)
841 (defun mh-letter-next-header-field ()
842 "Cycle to the next header field.
843 If we are at the last header field go to the start of the message
845 (let ((header-end (mh-mail-header-end)))
846 (cond ((>= (point) header-end) (goto-char (point-min)))
849 (re-search-forward mh-letter-header-field-regexp
850 (mh-line-end-position) t)
854 (cond ((re-search-forward mh-letter-header-field-regexp header-end t)
855 (if (mh-letter-skipped-header-field-p (match-string 1))
856 (mh-letter-next-header-field)
857 (mh-letter-skip-leading-whitespace-in-header-field)))
858 (t (goto-char header-end)
862 (defun mh-position-on-field (field &optional ignored)
863 "Move to the end of the FIELD in the header.
864 Move to end of entire header if FIELD not found.
865 Returns non-nil if FIELD was found.
866 The optional second arg is for pre-version 4 compatibility and is
868 (cond ((mh-goto-header-field field)
869 (mh-header-field-end)
871 ((mh-goto-header-end 0)
874 (defun mh-letter-header-field-at-point ()
875 "Return the header field name at point.
876 A symbol is returned whose name is the string obtained by
877 downcasing the field name."
880 (and (re-search-backward mh-letter-header-field-regexp nil t)
881 (intern (downcase (match-string 1))))))
883 (defun mh-folder-expand-at-point ()
884 "Do folder name completion in Fcc header field."
885 (let* ((beg (mh-beginning-of-word))
888 (mh-beginning-of-word -1))))
889 (when (>= end (point))
890 (list beg (if (fboundp 'completion-at-point) end (point))
891 #'mh-folder-completion-function))))
894 (defun mh-complete-word (word choices begin end)
895 "Complete WORD from CHOICES.
896 Any match found replaces the text from BEGIN to END."
897 (let ((completion (try-completion word choices))
898 (completions-buffer "*Completions*"))
899 (cond ((eq completion t)
901 (kill-buffer completions-buffer))
902 (message "Completed: %s" word))
905 (kill-buffer completions-buffer))
906 (message "No completion for %s" word))
907 ((stringp completion)
908 (if (equal word completion)
909 (with-output-to-temp-buffer completions-buffer
910 (mh-display-completion-list
911 (all-completions word choices)
912 ;; The `common-subtring' arg only works if it's a prefix.
913 (unless (and (functionp choices)
916 word nil '(boundaries . ""))))
917 (and (eq 'boundaries (car-safe bounds))
918 (< 0 (cadr bounds)))))
921 (kill-buffer completions-buffer))
922 (delete-region begin end)
923 (insert completion))))))
925 (defun mh-file-is-vcard-p (file)
926 "Return t if FILE is a .vcf vcard."
927 (let ((case-fold-search t))
930 (or (and (not (mh-have-file-command))
931 (not (null (string-match "\.vcf$" file))))
932 (string-equal "text/x-vcard" (mh-file-mime-type file))))))
935 (defun mh-letter-toggle-header-field-display-button (event)
936 "Toggle header field display at location of EVENT.
937 This function does the same thing as
938 `mh-letter-toggle-header-field-display' except that it is
939 callable from a mouse button."
941 (mh-do-at-event-location event
942 (mh-letter-toggle-header-field-display nil)))
944 (defun mh-extract-from-attribution ()
945 "Extract phrase or comment from From header field."
947 (if (not (mh-goto-header-field "From: "))
949 (skip-chars-forward " ")
951 ((looking-at "\"\\([^\"\n]+\\)\" \\(<.+>\\)")
952 (format "%s %s " (match-string 1)(match-string 2)))
953 ((looking-at "\\([^<\n]+<.+>\\)$")
954 (format "%s " (match-string 1)))
955 ((looking-at "\\([^ ]+@[^ ]+\\) +(\\(.+\\))$")
956 (format "%s <%s> " (match-string 2)(match-string 1)))
957 ((looking-at " *\\(.+\\)$")
958 (format "%s " (match-string 1)))))))
960 (defun mh-insert-prefix-string (mh-ins-string)
961 "Insert prefix string before each line in buffer.
962 The inserted letter is cited using `sc-cite-original' if
963 `mh-yank-behavior' is one of 'supercite or 'autosupercite.
964 Otherwise, simply insert MH-INS-STRING before each line."
965 (goto-char (point-min))
966 (cond ((or (eq mh-yank-behavior 'supercite)
967 (eq mh-yank-behavior 'autosupercite))
970 (run-hooks 'mail-citation-hook))
971 (mh-yank-hooks ;old hook name
972 (run-hooks 'mh-yank-hooks))
974 (or (bolp) (forward-line 1))
975 (while (< (point) (point-max))
976 (insert mh-ins-string)
978 (goto-char (point-min))))) ;leave point like sc-cite-original
983 ;; indent-tabs-mode: nil
984 ;; sentence-end-double-space: nil
987 ;;; mh-letter.el ends here