* gnus/nnheader.el (nnheader-find-file-noselect):
[emacs.git] / lisp / mh-e / mh-letter.el
blob051a98de89a6853846b2dc8a79cc779b3c7c2851
1 ;;; mh-letter.el --- MH-Letter mode
3 ;; Copyright (C) 1993, 1995, 1997,
4 ;; 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
5 ;; Free Software Foundation, Inc.
7 ;; Author: Bill Wohler <wohler@newt.com>
8 ;; Maintainer: Bill Wohler <wohler@newt.com>
9 ;; Keywords: mail
10 ;; See: mh-e.el
12 ;; This file is part of GNU Emacs.
14 ;; GNU Emacs is free software: you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation, either version 3 of the License, or
17 ;; (at your option) any later version.
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;;; Commentary:
29 ;; Mode for composing and sending a draft message.
31 ;; Functions that would ordinarily be in here that are needed by
32 ;; mh-show.el should be placed in the Message Utilities section in
33 ;; mh-utils.el. That will help prevent the loading of this file until
34 ;; a message is actually composed.
36 ;;; Change Log:
38 ;;; Code:
40 (require 'mh-e)
42 (require 'gnus-util)
44 ;; Dynamically-created functions not found in mh-loaddefs.el.
45 (autoload 'mh-tool-bar-letter-buttons-init "mh-tool-bar")
46 (autoload 'mh-tool-bar-init "mh-tool-bar")
48 (autoload 'mml-insert-tag "mml")
50 ;;; Variables
52 (defvar mh-letter-complete-function-alist
53 '((bcc . mh-alias-letter-expand-alias)
54 (cc . mh-alias-letter-expand-alias)
55 (dcc . mh-alias-letter-expand-alias)
56 (fcc . mh-folder-expand-at-point)
57 (from . mh-alias-letter-expand-alias)
58 (mail-followup-to . mh-alias-letter-expand-alias)
59 (mail-reply-to . mh-alias-letter-expand-alias)
60 (reply-to . mh-alias-letter-expand-alias)
61 (to . mh-alias-letter-expand-alias))
62 "Alist of header fields and completion functions to use.")
64 (defvar mh-yank-hooks nil
65 "Obsolete hook for modifying a citation just inserted in the mail buffer.
67 Each hook function can find the citation between point and mark.
68 And each hook function should leave point and mark around the
69 citation text as modified.
71 This is a normal hook, misnamed for historical reasons. It is
72 semi-obsolete and is only used if `mail-citation-hook' is nil.")
76 ;;; Letter Menu
78 (easy-menu-define
79 mh-letter-menu mh-letter-mode-map "Menu for MH-E letter mode."
80 '("Letter"
81 ["Send This Draft" mh-send-letter t]
82 ["Split Current Line" mh-open-line t]
83 ["Check Recipient" mh-check-whom t]
84 ["Yank Current Message" mh-yank-cur-msg t]
85 ["Insert a Message..." mh-insert-letter t]
86 ["Insert Signature" mh-insert-signature t]
87 ("Encrypt/Sign Message"
88 ["Sign Message"
89 mh-mml-secure-message-sign mh-pgp-support-flag]
90 ["Encrypt Message"
91 mh-mml-secure-message-encrypt mh-pgp-support-flag]
92 ["Sign+Encrypt Message"
93 mh-mml-secure-message-signencrypt mh-pgp-support-flag]
94 ["Disable Security"
95 mh-mml-unsecure-message mh-pgp-support-flag]
96 "--"
97 "Security Method"
98 ["PGP (MIME)" (setq mh-mml-method-default "pgpmime")
99 :style radio
100 :selected (equal mh-mml-method-default "pgpmime")]
101 ["PGP" (setq mh-mml-method-default "pgp")
102 :style radio
103 :selected (equal mh-mml-method-default "pgp")]
104 ["S/MIME" (setq mh-mml-method-default "smime")
105 :style radio
106 :selected (equal mh-mml-method-default "smime")]
107 "--"
108 ["Save Method as Default"
109 (customize-save-variable 'mh-mml-method-default mh-mml-method-default) t]
111 ["Compose Insertion..." mh-compose-insertion t]
112 ["Compose Compressed tar (MH)..."
113 mh-mh-compose-external-compressed-tar t]
114 ["Compose Get File (MH)..." mh-mh-compose-anon-ftp t]
115 ["Compose Forward..." mh-compose-forward t]
116 ;; The next two will have to be merged. But I also need to make sure the
117 ;; user can't mix tags of both types.
118 ["Pull in All Compositions (MH)"
119 mh-mh-to-mime (mh-mh-directive-present-p)]
120 ["Pull in All Compositions (MML)"
121 mh-mml-to-mime (mh-mml-tag-present-p)]
122 ["Revert to Non-MIME Edit (MH)"
123 mh-mh-to-mime-undo (equal mh-compose-insertion 'mh)]
124 ["Kill This Draft" mh-fully-kill-draft t]))
128 ;;; MH-Letter Keys
130 ;; If this changes, modify mh-letter-mode-help-messages accordingly, above.
131 (gnus-define-keys mh-letter-mode-map
132 " " mh-letter-complete-or-space
133 "," mh-letter-confirm-address
134 "\C-c?" mh-help
135 "\C-c\C-\\" mh-fully-kill-draft ;if no C-q
136 "\C-c\C-^" mh-insert-signature ;if no C-s
137 "\C-c\C-c" mh-send-letter
138 "\C-c\C-d" mh-insert-identity
139 "\C-c\C-e" mh-mh-to-mime
140 "\C-c\C-f\C-a" mh-to-field
141 "\C-c\C-f\C-b" mh-to-field
142 "\C-c\C-f\C-c" mh-to-field
143 "\C-c\C-f\C-d" mh-to-field
144 "\C-c\C-f\C-f" mh-to-fcc
145 "\C-c\C-f\C-l" mh-to-field
146 "\C-c\C-f\C-m" mh-to-field
147 "\C-c\C-f\C-r" mh-to-field
148 "\C-c\C-f\C-s" mh-to-field
149 "\C-c\C-f\C-t" mh-to-field
150 "\C-c\C-fa" mh-to-field
151 "\C-c\C-fb" mh-to-field
152 "\C-c\C-fc" mh-to-field
153 "\C-c\C-fd" mh-to-field
154 "\C-c\C-ff" mh-to-fcc
155 "\C-c\C-fl" mh-to-field
156 "\C-c\C-fm" mh-to-field
157 "\C-c\C-fr" mh-to-field
158 "\C-c\C-fs" mh-to-field
159 "\C-c\C-ft" mh-to-field
160 "\C-c\C-i" mh-insert-letter
161 "\C-c\C-m\C-e" mh-mml-secure-message-encrypt
162 "\C-c\C-m\C-f" mh-compose-forward
163 "\C-c\C-m\C-g" mh-mh-compose-anon-ftp
164 "\C-c\C-m\C-i" mh-compose-insertion
165 "\C-c\C-m\C-m" mh-mml-to-mime
166 "\C-c\C-m\C-n" mh-mml-unsecure-message
167 "\C-c\C-m\C-s" mh-mml-secure-message-sign
168 "\C-c\C-m\C-t" mh-mh-compose-external-compressed-tar
169 "\C-c\C-m\C-u" mh-mh-to-mime-undo
170 "\C-c\C-m\C-x" mh-mh-compose-external-type
171 "\C-c\C-mee" mh-mml-secure-message-encrypt
172 "\C-c\C-mes" mh-mml-secure-message-signencrypt
173 "\C-c\C-mf" mh-compose-forward
174 "\C-c\C-mg" mh-mh-compose-anon-ftp
175 "\C-c\C-mi" mh-compose-insertion
176 "\C-c\C-mm" mh-mml-to-mime
177 "\C-c\C-mn" mh-mml-unsecure-message
178 "\C-c\C-mse" mh-mml-secure-message-signencrypt
179 "\C-c\C-mss" mh-mml-secure-message-sign
180 "\C-c\C-mt" mh-mh-compose-external-compressed-tar
181 "\C-c\C-mu" mh-mh-to-mime-undo
182 "\C-c\C-mx" mh-mh-compose-external-type
183 "\C-c\C-o" mh-open-line
184 "\C-c\C-q" mh-fully-kill-draft
185 "\C-c\C-s" mh-insert-signature
186 "\C-c\C-t" mh-letter-toggle-header-field-display
187 "\C-c\C-w" mh-check-whom
188 "\C-c\C-y" mh-yank-cur-msg
189 "\C-c\M-d" mh-insert-auto-fields
190 "\M-\t" mh-letter-complete
191 "\t" mh-letter-next-header-field-or-indent
192 [backtab] mh-letter-previous-header-field)
194 ;; "C-c /" prefix is used in mh-letter-mode by pgp.el and mailcrypt.el.
198 ;;; MH-Letter Help Messages
200 ;; Group messages logically, more or less.
201 (defvar mh-letter-mode-help-messages
202 '((nil
203 "Send letter: \\[mh-send-letter] "
204 "Open line: \\[mh-open-line]\n"
205 "Kill letter: \\[mh-fully-kill-draft] "
206 "Check recipients: \\[mh-check-whom]\n\n"
207 "Insert:\n"
208 " Current message: \\[mh-yank-cur-msg]\n"
209 " Attachment: \\[mh-compose-insertion]\n"
210 " Message to forward: \\[mh-compose-forward]\n"
211 " Signature: \\[mh-insert-signature]\n\n"
212 "Security:\n"
213 " Encrypt message: \\[mh-mml-secure-message-encrypt]\n"
214 " Sign message: \\[mh-mml-secure-message-sign]\n"
215 " Sign+Encrypt message: \\[mh-mml-secure-message-signencrypt]"))
216 "Key binding cheat sheet.
218 This is an associative array which is used to show the most
219 common commands. The key is a prefix char. The value is one or
220 more strings which are concatenated together and displayed in the
221 minibuffer if ? is pressed after the prefix character. The
222 special key nil is used to display the non-prefixed commands.
224 The substitutions described in `substitute-command-keys' are
225 performed as well.")
229 ;;; MH-Letter Font Lock
231 (defvar mh-letter-font-lock-keywords
232 `(,@(mh-show-font-lock-keywords-with-cite)
233 (mh-font-lock-field-data
234 (1 'mh-letter-header-field prepend t)))
235 "Additional expressions to highlight in MH-Letter buffers.")
237 (defun mh-font-lock-field-data (limit)
238 "Find header field region between point and LIMIT."
239 (and (< (point) (mh-letter-header-end))
240 (< (point) limit)
241 (let ((end (min limit (mh-letter-header-end)))
242 (point (point))
243 data-end data-begin field)
244 (end-of-line)
245 (setq data-end (if (re-search-forward "^[^ \t]" end t)
246 (match-beginning 0)
247 end))
248 (goto-char (1- data-end))
249 (if (not (re-search-backward "\\(^[^ \t][^:]*\\):[ \t]*" nil t))
250 (setq data-begin (point-min))
251 (setq data-begin (match-end 0))
252 (setq field (match-string 1)))
253 (setq data-begin (max point data-begin))
254 (goto-char (if (equal point data-end) (1+ data-end) data-end))
255 (cond ((and field (mh-letter-skipped-header-field-p field))
256 (set-match-data nil)
257 nil)
258 (t (set-match-data
259 (list data-begin data-end data-begin data-end))
260 t)))))
262 (defun mh-letter-header-end ()
263 "Find the end of the message header.
264 This function is to be used only for font locking. It works by
265 searching for `mh-mail-header-separator' in the buffer."
266 (save-excursion
267 (goto-char (point-min))
268 (cond ((equal mh-mail-header-separator "") (point-min))
269 ((search-forward (format "\n%s\n" mh-mail-header-separator) nil t)
270 (mh-line-beginning-position 0))
271 (t (point-min)))))
275 ;;; MH-Letter Mode
277 ;; Shush compiler.
278 (defvar font-lock-defaults) ; XEmacs
280 ;; Ensure new buffers won't get this mode if default major-mode is nil.
281 (put 'mh-letter-mode 'mode-class 'special)
283 ;;;###mh-autoload
284 (define-derived-mode mh-letter-mode mail-mode "MH-Letter"
285 "Mode for composing letters in MH-E\\<mh-letter-mode-map>.
287 When you have finished composing, type \\[mh-send-letter] to send
288 the message using the MH mail handling system.
290 There are two types of tags used by MH-E when composing MIME
291 messages: MML and MH. The option `mh-compose-insertion' controls
292 what type of tags are inserted by MH-E commands. These tags can
293 be converted to MIME body parts by running \\[mh-mh-to-mime] for
294 MH-style directives or \\[mh-mml-to-mime] for MML tags.
296 Options that control this mode can be changed with
297 \\[customize-group]; specify the \"mh-compose\" group.
299 When a message is composed, the hooks `text-mode-hook',
300 `mail-mode-hook', and `mh-letter-mode-hook' are run (in that
301 order).
303 \\{mh-letter-mode-map}"
304 (mh-find-path)
305 (make-local-variable 'mh-send-args)
306 (make-local-variable 'mh-annotate-char)
307 (make-local-variable 'mh-annotate-field)
308 (make-local-variable 'mh-previous-window-config)
309 (make-local-variable 'mh-sent-from-folder)
310 (make-local-variable 'mh-sent-from-msg)
311 (mh-do-in-gnu-emacs
312 (unless mh-letter-tool-bar-map
313 (mh-tool-bar-letter-buttons-init))
314 (if (boundp 'tool-bar-map)
315 (set (make-local-variable 'tool-bar-map) mh-letter-tool-bar-map)))
316 (mh-do-in-xemacs
317 (mh-tool-bar-init :letter))
318 ;; Set the local value of mh-mail-header-separator according to what is
319 ;; present in the buffer...
320 (set (make-local-variable 'mh-mail-header-separator)
321 (save-excursion
322 (goto-char (mh-mail-header-end))
323 (buffer-substring-no-properties (point) (mh-line-end-position))))
324 (make-local-variable 'mail-header-separator)
325 (setq mail-header-separator mh-mail-header-separator) ;override sendmail.el
326 (mh-set-help mh-letter-mode-help-messages)
327 (setq buffer-invisibility-spec '((vanish . t) t))
328 (set (make-local-variable 'line-move-ignore-invisible) t)
330 ;; Enable undo since a show-mode buffer might have been reused.
331 (buffer-enable-undo)
332 (make-local-variable 'font-lock-defaults)
333 (cond
334 ((or (equal mh-highlight-citation-style 'font-lock)
335 (equal mh-highlight-citation-style 'gnus))
336 ;; Let's use font-lock even if gnus is used in show-mode. The reason
337 ;; is that gnus uses static text properties which are not appropriate
338 ;; for a buffer that will be edited. So the choice here is either fontify
339 ;; the citations and header...
340 (setq font-lock-defaults '(mh-letter-font-lock-keywords t)))
342 ;; ...or the header only
343 (setq font-lock-defaults '((mh-show-font-lock-keywords) t))))
344 (easy-menu-add mh-letter-menu)
345 ;; Maybe we want to use the existing Mail menu from mail-mode in
346 ;; 9.0; in the mean time, let's remove it since the redundancy will
347 ;; only produce confusion.
348 (define-key mh-letter-mode-map [menu-bar mail] 'undefined)
349 (mh-do-in-xemacs (easy-menu-remove mail-menubar-menu))
350 (setq fill-column mh-letter-fill-column)
351 ;; If text-mode-hook turned on auto-fill, tune it for messages
352 (when auto-fill-function
353 (make-local-variable 'auto-fill-function)
354 (setq auto-fill-function 'mh-auto-fill-for-letter)))
358 ;;; MH-Letter Commands
360 ;; Alphabetical.
361 ;; See also mh-comp.el and mh-mime.el.
363 (defun mh-check-whom ()
364 "Verify recipients, showing expansion of any aliases.
366 This command expands aliases so you can check the actual address(es)
367 in the alias. A new buffer named \"*MH-E Recipients*\" is created with
368 the output of \"whom\"."
369 (interactive)
370 (let ((file-name buffer-file-name))
371 (save-buffer)
372 (message "Checking recipients...")
373 (mh-in-show-buffer (mh-recipients-buffer)
374 (bury-buffer (current-buffer))
375 (erase-buffer)
376 (mh-exec-cmd-output "whom" t file-name))
377 (message "Checking recipients...done")))
379 (defun mh-insert-letter (folder message verbatim)
380 "Insert a message.
382 This command prompts you for the FOLDER and MESSAGE number, which
383 defaults to the current message in that folder. It then inserts
384 the message, indented by `mh-ins-buf-prefix' (\"> \") unless
385 `mh-yank-behavior' is set to one of the supercite flavors in
386 which case supercite is used to format the message. Certain
387 undesirable header fields (see
388 `mh-invisible-header-fields-compiled') are removed before
389 insertion.
391 If given a prefix argument VERBATIM, the header is left intact, the
392 message is not indented, and \"> \" is not inserted before each line.
393 This command leaves the mark before the letter and point after it."
394 (interactive
395 (let* ((folder
396 (mh-prompt-for-folder "Message from" mh-sent-from-folder nil))
397 (default
398 (if (equal folder mh-sent-from-folder)
399 (or mh-sent-from-msg (nth 0 (mh-translate-range folder "cur")))
400 (nth 0 (mh-translate-range folder "cur"))))
401 (message
402 (read-string (concat "Message number"
403 (or (and default
404 (format " (default %d): " default))
405 ": "))
406 nil nil
407 (if (numberp default)
408 (int-to-string default)
409 default))))
410 (list folder message current-prefix-arg)))
411 (if (equal message "")
412 (error "No message number given"))
413 (save-restriction
414 (narrow-to-region (point) (point))
415 (let ((start (point-min)))
416 (insert-file-contents
417 (expand-file-name message (mh-expand-file-name folder)))
418 (when (not verbatim)
419 (mh-clean-msg-header start mh-invisible-header-fields-compiled nil)
420 (goto-char (point-max)) ;Needed for sc-cite-original
421 (push-mark) ;Needed for sc-cite-original
422 (goto-char (point-min)) ;Needed for sc-cite-original
423 (mh-insert-prefix-string mh-ins-buf-prefix)))))
425 ;;;###mh-autoload
426 (defun mh-insert-signature (&optional file)
427 "Insert signature in message.
429 This command inserts your signature at the current cursor location.
431 By default, the text of your signature is taken from the file
432 \"~/.signature\". You can read from other sources by changing the
433 option `mh-signature-file-name'.
435 A signature separator (\"-- \") will be added if the signature block
436 does not contain one and `mh-signature-separator-flag' is on.
438 The hook `mh-insert-signature-hook' is run after the signature is
439 inserted. Hook functions may access the actual name of the file or the
440 function used to insert the signature with `mh-signature-file-name'.
442 The signature can also be inserted using Identities (see
443 `mh-identity-list').
445 In a program, you can pass in a signature FILE."
446 (interactive)
447 (save-excursion
448 (insert "\n")
449 (let ((mh-signature-file-name (or file mh-signature-file-name))
450 (mh-mh-p (mh-mh-directive-present-p))
451 (mh-mml-p (mh-mml-tag-present-p)))
452 (save-restriction
453 (narrow-to-region (point) (point))
454 (cond
455 ((mh-file-is-vcard-p mh-signature-file-name)
456 (if (equal mh-compose-insertion 'mml)
457 (insert "<#part type=\"text/x-vcard\" filename=\""
458 mh-signature-file-name
459 "\" disposition=inline description=VCard>\n<#/part>")
460 (insert "#text/x-vcard; name=\""
461 (file-name-nondirectory mh-signature-file-name)
462 "\" [VCard] " (expand-file-name mh-signature-file-name))))
464 (cond
465 (mh-mh-p
466 (insert "#\n" "Content-Description: Signature\n"))
467 (mh-mml-p
468 (mml-insert-tag 'part 'type "text/plain" 'disposition "inline"
469 'description "Signature")))
470 (cond ((null mh-signature-file-name))
471 ((and (stringp mh-signature-file-name)
472 (file-readable-p mh-signature-file-name))
473 (insert-file-contents mh-signature-file-name))
474 ((functionp mh-signature-file-name)
475 (funcall mh-signature-file-name)))))
476 (save-restriction
477 (widen)
478 (run-hooks 'mh-insert-signature-hook))
479 (goto-char (point-min))
480 (when (and (not (mh-file-is-vcard-p mh-signature-file-name))
481 mh-signature-separator-flag
482 (> (point-max) (point-min))
483 (not (mh-signature-separator-p)))
484 (cond (mh-mh-p
485 (forward-line 2))
486 (mh-mml-p
487 (forward-line 1)))
488 (insert mh-signature-separator))
489 (if (not (> (point-max) (point-min)))
490 (message "No signature found")))))
491 (force-mode-line-update))
493 (defun mh-letter-complete (arg)
494 "Perform completion on header field or word preceding point.
496 If the field contains addresses (for example, \"To:\" or \"Cc:\")
497 or folders (for example, \"Fcc:\") then this command will provide
498 alias completion. In the body of the message, this command runs
499 `mh-letter-complete-function' instead, which is set to
500 `ispell-complete-word' by default. This command takes a prefix
501 argument ARG that is passed to the
502 `mh-letter-complete-function'."
503 (interactive "P")
504 (let ((func nil))
505 (cond ((not (mh-in-header-p))
506 (funcall mh-letter-complete-function arg))
507 ((setq func (cdr (assoc (mh-letter-header-field-at-point)
508 mh-letter-complete-function-alist)))
509 (funcall func))
510 (t (funcall mh-letter-complete-function arg)))))
512 (defun mh-letter-complete-or-space (arg)
513 "Perform completion or insert space.
515 Turn on the option `mh-compose-space-does-completion-flag' to use
516 this command to perform completion in the header. Otherwise, a
517 space is inserted; use a prefix argument ARG to specify more than
518 one space."
519 (interactive "p")
520 (let ((func nil)
521 (end-of-prev (save-excursion
522 (goto-char (mh-beginning-of-word))
523 (mh-beginning-of-word -1))))
524 (cond ((not mh-compose-space-does-completion-flag)
525 (self-insert-command arg))
526 ((not (mh-in-header-p)) (self-insert-command arg))
527 ((> (point) end-of-prev) (self-insert-command arg))
528 ((setq func (cdr (assoc (mh-letter-header-field-at-point)
529 mh-letter-complete-function-alist)))
530 (funcall func))
531 (t (self-insert-command arg)))))
533 (defun mh-letter-confirm-address ()
534 "Flash alias expansion.
536 Addresses are separated by a comma\; when you press the comma,
537 this command flashes the alias expansion in the minibuffer if
538 `mh-alias-flash-on-comma' is turned on."
539 (interactive)
540 (cond ((not (mh-in-header-p)) (self-insert-command 1))
541 ((eq (cdr (assoc (mh-letter-header-field-at-point)
542 mh-letter-complete-function-alist))
543 'mh-alias-letter-expand-alias)
544 (mh-alias-reload-maybe)
545 (mh-alias-minibuffer-confirm-address))
546 (t (self-insert-command 1))))
548 (defun mh-letter-next-header-field-or-indent (arg)
549 "Cycle to next field.
551 Within the header of the message, this command moves between
552 fields that are highlighted with the face
553 `mh-letter-header-field', skipping those fields listed in
554 `mh-compose-skipped-header-fields'. After the last field, this
555 command then moves point to the message body before cycling back
556 to the first field. If point is already past the first line of
557 the message body, then this command indents by calling
558 `indent-relative' with the given prefix argument ARG."
559 (interactive "P")
560 (let ((header-end (save-excursion
561 (goto-char (mh-mail-header-end))
562 (forward-line)
563 (point))))
564 (if (> (point) header-end)
565 (indent-relative arg)
566 (mh-letter-next-header-field))))
568 (defun mh-letter-previous-header-field ()
569 "Cycle to the previous header field.
571 This command moves backwards between the fields and cycles to the
572 body of the message after the first field. Unlike the command
573 \\[mh-letter-next-header-field-or-indent], it will always take
574 point to the last field from anywhere in the body."
575 (interactive)
576 (let ((header-end (mh-mail-header-end)))
577 (if (>= (point) header-end)
578 (goto-char header-end)
579 (mh-header-field-beginning))
580 (cond ((re-search-backward mh-letter-header-field-regexp nil t)
581 (if (mh-letter-skipped-header-field-p (match-string 1))
582 (mh-letter-previous-header-field)
583 (goto-char (match-end 0))
584 (mh-letter-skip-leading-whitespace-in-header-field)))
585 (t (goto-char header-end)
586 (forward-line)))))
588 (defun mh-open-line ()
589 "Insert a newline and leave point before it.
591 This command is similar to the command \\[open-line] in that it
592 inserts a newline after point. It differs in that it also inserts
593 the right number of quoting characters and spaces so that the
594 next line begins in the same column as it was. This is useful
595 when breaking up paragraphs in replies."
596 (interactive)
597 (let ((column (current-column))
598 (prefix (mh-current-fill-prefix)))
599 (if (> (length prefix) column)
600 (message "Sorry, point seems to be within the line prefix")
601 (newline 2)
602 (insert prefix)
603 (while (> column (current-column))
604 (insert " "))
605 (forward-line -1))))
607 (defun mh-to-fcc (&optional folder)
608 "Move to \"Fcc:\" header field.
610 This command will prompt you for the FOLDER name in which to file
611 a copy of the draft."
612 (interactive (list (mh-prompt-for-folder
613 "Fcc"
614 (or (and mh-default-folder-for-message-function
615 (save-excursion
616 (goto-char (point-min))
617 (funcall
618 mh-default-folder-for-message-function)))
620 t)))
621 (let ((last-input-event ?\C-f))
622 (expand-abbrev)
623 (save-excursion
624 (mh-to-field)
625 (insert (if (mh-folder-name-p folder)
626 (substring folder 1)
627 folder)))))
629 (defvar mh-to-field-choices '(("a" . "Mail-Reply-To:")
630 ("b" . "Bcc:")
631 ("c" . "Cc:")
632 ("d" . "Dcc:")
633 ("f" . "Fcc:")
634 ("l" . "Mail-Followup-To:")
635 ("m" . "From:")
636 ("r" . "Reply-To:")
637 ("s" . "Subject:")
638 ("t" . "To:"))
639 "Alist of (final-character . field-name) choices for `mh-to-field'.")
641 (defun mh-to-field ()
642 "Move to specified header field.
644 The field is indicated by the previous keystroke (the last
645 keystroke of the command) according to the list in the variable
646 `mh-to-field-choices'.
647 Create the field if it does not exist.
648 Set the mark to point before moving."
649 (interactive)
650 (expand-abbrev)
651 (let ((target (cdr (or (assoc (char-to-string (logior last-input-event ?`))
652 mh-to-field-choices)
653 ;; also look for a char for version 4 compat
654 (assoc (logior last-input-event ?`)
655 mh-to-field-choices))))
656 (case-fold-search t))
657 (push-mark)
658 (cond ((mh-position-on-field target)
659 (let ((eol (point)))
660 (skip-chars-backward " \t")
661 (delete-region (point) eol))
662 (if (and (not (eq (logior last-input-event ?`) ?s))
663 (save-excursion
664 (backward-char 1)
665 (not (looking-at "[:,]"))))
666 (insert ", ")
667 (insert " ")))
669 (if (mh-position-on-field "To:")
670 (forward-line 1))
671 (insert (format "%s \n" target))
672 (backward-char 1)))))
674 ;;;###mh-autoload
675 (defun mh-yank-cur-msg ()
676 "Insert the current message into the draft buffer.
678 It is often useful to insert a snippet of text from a letter that
679 someone mailed to provide some context for your reply. This
680 command does this by adding an attribution, yanking a portion of
681 text from the message to which you're replying, and inserting
682 `mh-ins-buf-prefix' (`> ') before each line.
684 The attribution consists of the sender's name and email address
685 followed by the content of the option
686 `mh-extract-from-attribution-verb'.
688 You can also turn on the option
689 `mh-delete-yanked-msg-window-flag' to delete the window
690 containing the original message after yanking it to make more
691 room on your screen for your reply.
693 You can control how the message to which you are replying is
694 yanked into your reply using `mh-yank-behavior'.
696 If this isn't enough, you can gain full control over the
697 appearance of the included text by setting `mail-citation-hook'
698 to a function that modifies it. For example, if you set this hook
699 to `trivial-cite' (which is NOT part of Emacs), set
700 `mh-yank-behavior' to \"Body and Header\" (see URL
701 `http://shasta.cs.uiuc.edu/~lrclause/tc.html').
703 Note that if `mail-citation-hook' is set, `mh-ins-buf-prefix' is
704 not inserted. If the option `mh-yank-behavior' is set to one of
705 the supercite flavors, the hook `mail-citation-hook' is ignored
706 and `mh-ins-buf-prefix' is not inserted."
707 (interactive)
708 (if (and mh-sent-from-folder
709 (save-excursion (set-buffer mh-sent-from-folder) mh-show-buffer)
710 (save-excursion (set-buffer mh-sent-from-folder)
711 (get-buffer mh-show-buffer))
712 mh-sent-from-msg)
713 (let ((to-point (point))
714 (to-buffer (current-buffer)))
715 (set-buffer mh-sent-from-folder)
716 (if mh-delete-yanked-msg-window-flag
717 (delete-windows-on mh-show-buffer))
718 (set-buffer mh-show-buffer) ; Find displayed message
719 (let* ((from-attr (mh-extract-from-attribution))
720 (yank-region (mh-mark-active-p nil))
721 (mh-ins-str
722 (cond ((and yank-region
723 (or (eq 'supercite mh-yank-behavior)
724 (eq 'autosupercite mh-yank-behavior)
725 (eq t mh-yank-behavior)))
726 ;; supercite needs the full header
727 (concat
728 (buffer-substring (point-min) (mh-mail-header-end))
729 "\n"
730 (buffer-substring (region-beginning) (region-end))))
731 (yank-region
732 (buffer-substring (region-beginning) (region-end)))
733 ((or (eq 'body mh-yank-behavior)
734 (eq 'attribution mh-yank-behavior)
735 (eq 'autoattrib mh-yank-behavior))
736 (buffer-substring
737 (save-excursion
738 (goto-char (point-min))
739 (mh-goto-header-end 1)
740 (point))
741 (point-max)))
742 ((or (eq 'supercite mh-yank-behavior)
743 (eq 'autosupercite mh-yank-behavior)
744 (eq t mh-yank-behavior))
745 (buffer-substring (point-min) (point-max)))
747 (buffer-substring (point) (point-max))))))
748 (set-buffer to-buffer)
749 (save-restriction
750 (narrow-to-region to-point to-point)
751 (insert (mh-filter-out-non-text mh-ins-str))
752 (goto-char (point-max)) ;Needed for sc-cite-original
753 (push-mark) ;Needed for sc-cite-original
754 (goto-char (point-min)) ;Needed for sc-cite-original
755 (mh-insert-prefix-string mh-ins-buf-prefix)
756 (when (or (eq 'attribution mh-yank-behavior)
757 (eq 'autoattrib mh-yank-behavior))
758 (insert from-attr)
759 (mh-identity-insert-attribution-verb nil)
760 (insert "\n\n"))
761 ;; If the user has selected a region, he has already "edited" the
762 ;; text, so leave the cursor at the end of the yanked text. In
763 ;; either case, leave a mark at the opposite end of the included
764 ;; text to make it easy to jump or delete to the other end of the
765 ;; text.
766 (push-mark)
767 (goto-char (point-max))
768 (if (null yank-region)
769 (mh-exchange-point-and-mark-preserving-active-mark)))))
770 (error "There is no current message")))
774 ;;; Support Routines
776 (defun mh-auto-fill-for-letter ()
777 "Perform auto-fill for message.
778 Header is treated specially by inserting a tab before continuation
779 lines."
780 (if (mh-in-header-p)
781 (let ((fill-prefix "\t"))
782 (do-auto-fill))
783 (do-auto-fill)))
785 (defun mh-filter-out-non-text (string)
786 "Return STRING but without adornments such as MIME buttons and smileys."
787 (with-temp-buffer
788 ;; Insert the string to filter
789 (insert string)
790 (goto-char (point-min))
792 ;; Remove the MIME buttons
793 (let ((can-move-forward t)
794 (in-button nil))
795 (while can-move-forward
796 (cond ((and (not (get-text-property (point) 'mh-data))
797 in-button)
798 (delete-region (1- (point)) (point))
799 (setq in-button nil))
800 ((get-text-property (point) 'mh-data)
801 (delete-region (point)
802 (save-excursion (forward-line) (point)))
803 (setq in-button t))
804 (t (setq can-move-forward (= (forward-line) 0))))))
806 ;; Return the contents without properties... This gets rid of emphasis
807 ;; and smileys
808 (buffer-substring-no-properties (point-min) (point-max))))
810 (defun mh-current-fill-prefix ()
811 "Return the `fill-prefix' on the current line as a string."
812 (save-excursion
813 (beginning-of-line)
814 ;; This assumes that the major-mode sets up adaptive-fill-regexp
815 ;; correctly such as mh-letter-mode or sendmail.el's mail-mode. But
816 ;; perhaps I should use the variable and simply inserts its value here,
817 ;; and set it locally in a let scope. --psg
818 (if (re-search-forward adaptive-fill-regexp nil t)
819 (match-string 0)
820 "")))
822 ;;;###mh-autoload
823 (defun mh-letter-next-header-field ()
824 "Cycle to the next header field.
825 If we are at the last header field go to the start of the message
826 body."
827 (let ((header-end (mh-mail-header-end)))
828 (cond ((>= (point) header-end) (goto-char (point-min)))
829 ((< (point) (progn
830 (beginning-of-line)
831 (re-search-forward mh-letter-header-field-regexp
832 (mh-line-end-position) t)
833 (point)))
834 (beginning-of-line))
835 (t (end-of-line)))
836 (cond ((re-search-forward mh-letter-header-field-regexp header-end t)
837 (if (mh-letter-skipped-header-field-p (match-string 1))
838 (mh-letter-next-header-field)
839 (mh-letter-skip-leading-whitespace-in-header-field)))
840 (t (goto-char header-end)
841 (forward-line)))))
843 ;;;###mh-autoload
844 (defun mh-position-on-field (field &optional ignored)
845 "Move to the end of the FIELD in the header.
846 Move to end of entire header if FIELD not found.
847 Returns non-nil if FIELD was found.
848 The optional second arg is for pre-version 4 compatibility and is
849 IGNORED."
850 (cond ((mh-goto-header-field field)
851 (mh-header-field-end)
853 ((mh-goto-header-end 0)
854 nil)))
856 (defun mh-letter-header-field-at-point ()
857 "Return the header field name at point.
858 A symbol is returned whose name is the string obtained by
859 downcasing the field name."
860 (save-excursion
861 (end-of-line)
862 (and (re-search-backward mh-letter-header-field-regexp nil t)
863 (intern (downcase (match-string 1))))))
865 (defun mh-folder-expand-at-point ()
866 "Do folder name completion in Fcc header field."
867 (let* ((end (point))
868 (beg (mh-beginning-of-word))
869 (folder (buffer-substring-no-properties beg end))
870 (leading-plus (and (> (length folder) 0) (equal (aref folder 0) ?+)))
871 (choices (mapcar (lambda (x) (list x))
872 (mh-folder-completion-function folder nil t))))
873 (unless leading-plus
874 (setq folder (concat "+" folder)))
875 (mh-complete-word folder choices beg end)))
877 ;;;###mh-autoload
878 (defun mh-complete-word (word choices begin end)
879 "Complete WORD from CHOICES.
880 Any match found replaces the text from BEGIN to END."
881 (let ((completion (try-completion word choices))
882 (completions-buffer "*Completions*"))
883 (cond ((eq completion t)
884 (ignore-errors
885 (kill-buffer completions-buffer))
886 (message "Completed: %s" word))
887 ((null completion)
888 (ignore-errors
889 (kill-buffer completions-buffer))
890 (message "No completion for %s" word))
891 ((stringp completion)
892 (if (equal word completion)
893 (with-output-to-temp-buffer completions-buffer
894 (mh-display-completion-list (all-completions word choices)
895 word))
896 (ignore-errors
897 (kill-buffer completions-buffer))
898 (delete-region begin end)
899 (insert completion))))))
901 (defun mh-file-is-vcard-p (file)
902 "Return t if FILE is a .vcf vcard."
903 (let ((case-fold-search t))
904 (and (stringp file)
905 (file-exists-p file)
906 (or (and (not (mh-have-file-command))
907 (not (null (string-match "\.vcf$" file))))
908 (string-equal "text/x-vcard" (mh-file-mime-type file))))))
910 ;;;###mh-autoload
911 (defun mh-letter-toggle-header-field-display-button (event)
912 "Toggle header field display at location of EVENT.
913 This function does the same thing as
914 `mh-letter-toggle-header-field-display' except that it is
915 callable from a mouse button."
916 (interactive "e")
917 (mh-do-at-event-location event
918 (mh-letter-toggle-header-field-display nil)))
920 (defun mh-extract-from-attribution ()
921 "Extract phrase or comment from From header field."
922 (save-excursion
923 (if (not (mh-goto-header-field "From: "))
925 (skip-chars-forward " ")
926 (cond
927 ((looking-at "\"\\([^\"\n]+\\)\" \\(<.+>\\)")
928 (format "%s %s " (match-string 1)(match-string 2)))
929 ((looking-at "\\([^<\n]+<.+>\\)$")
930 (format "%s " (match-string 1)))
931 ((looking-at "\\([^ ]+@[^ ]+\\) +(\\(.+\\))$")
932 (format "%s <%s> " (match-string 2)(match-string 1)))
933 ((looking-at " *\\(.+\\)$")
934 (format "%s " (match-string 1)))))))
936 (defun mh-insert-prefix-string (mh-ins-string)
937 "Insert prefix string before each line in buffer.
938 The inserted letter is cited using `sc-cite-original' if
939 `mh-yank-behavior' is one of 'supercite or 'autosupercite.
940 Otherwise, simply insert MH-INS-STRING before each line."
941 (goto-char (point-min))
942 (cond ((or (eq mh-yank-behavior 'supercite)
943 (eq mh-yank-behavior 'autosupercite))
944 (sc-cite-original))
945 (mail-citation-hook
946 (run-hooks 'mail-citation-hook))
947 (mh-yank-hooks ;old hook name
948 (run-hooks 'mh-yank-hooks))
950 (or (bolp) (forward-line 1))
951 (while (< (point) (point-max))
952 (insert mh-ins-string)
953 (forward-line 1))
954 (goto-char (point-min))))) ;leave point like sc-cite-original
956 (provide 'mh-letter)
958 ;; Local Variables:
959 ;; indent-tabs-mode: nil
960 ;; sentence-end-double-space: nil
961 ;; End:
963 ;; arch-tag: 0548632c-aadb-4e3b-bb80-bbd62ff90bf3
964 ;;; mh-letter.el ends here