Add 2012 to FSF copyright years for Emacs files
[bpt/emacs.git] / lisp / mh-e / mh-letter.el
CommitLineData
dda00b2c
BW
1;;; mh-letter.el --- MH-Letter mode
2
acaf905b 3;; Copyright (C) 1993, 1995, 1997, 2000-2012 Free Software Foundation, Inc.
dda00b2c
BW
4
5;; Author: Bill Wohler <wohler@newt.com>
6;; Maintainer: Bill Wohler <wohler@newt.com>
7;; Keywords: mail
8;; See: mh-e.el
9
10;; This file is part of GNU Emacs.
11
5e809f55 12;; GNU Emacs is free software: you can redistribute it and/or modify
dda00b2c 13;; it under the terms of the GNU General Public License as published by
5e809f55
GM
14;; the Free Software Foundation, either version 3 of the License, or
15;; (at your option) any later version.
dda00b2c
BW
16
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.
21
22;; You should have received a copy of the GNU General Public License
5e809f55 23;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
dda00b2c
BW
24
25;;; Commentary:
26
27;; Mode for composing and sending a draft message.
28
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.
33
34;;; Change Log:
35
36;;; Code:
37
38(require 'mh-e)
39
40(require 'gnus-util)
41
efc27af6 42;; Dynamically-created functions not found in mh-loaddefs.el.
dda00b2c 43(autoload 'mh-tool-bar-letter-buttons-init "mh-tool-bar")
efc27af6 44(autoload 'mh-tool-bar-init "mh-tool-bar")
dda00b2c
BW
45
46(autoload 'mml-insert-tag "mml")
47
48;;; Variables
49
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.")
61
dda00b2c
BW
62(defvar mh-yank-hooks nil
63 "Obsolete hook for modifying a citation just inserted in the mail buffer.
64
65Each hook function can find the citation between point and mark.
66And each hook function should leave point and mark around the
67citation text as modified.
68
69This is a normal hook, misnamed for historical reasons. It is
70semi-obsolete and is only used if `mail-citation-hook' is nil.")
71
72\f
73
74;;; Letter Menu
75
dda00b2c
BW
76(easy-menu-define
77 mh-letter-menu mh-letter-mode-map "Menu for MH-E letter mode."
78 '("Letter"
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"
86 ["Sign Message"
87 mh-mml-secure-message-sign mh-pgp-support-flag]
88 ["Encrypt Message"
89 mh-mml-secure-message-encrypt mh-pgp-support-flag]
90 ["Sign+Encrypt Message"
91 mh-mml-secure-message-signencrypt mh-pgp-support-flag]
92 ["Disable Security"
93 mh-mml-unsecure-message mh-pgp-support-flag]
94 "--"
95 "Security Method"
96 ["PGP (MIME)" (setq mh-mml-method-default "pgpmime")
97 :style radio
98 :selected (equal mh-mml-method-default "pgpmime")]
99 ["PGP" (setq mh-mml-method-default "pgp")
100 :style radio
101 :selected (equal mh-mml-method-default "pgp")]
102 ["S/MIME" (setq mh-mml-method-default "smime")
103 :style radio
104 :selected (equal mh-mml-method-default "smime")]
105 "--"
106 ["Save Method as Default"
107 (customize-save-variable 'mh-mml-method-default mh-mml-method-default) t]
108 )
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]))
123
124\f
125
126;;; MH-Letter Keys
127
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
132 "\C-c?" mh-help
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
24f75b10 188 "\M-\t" mh-letter-complete
dda00b2c
BW
189 "\t" mh-letter-next-header-field-or-indent
190 [backtab] mh-letter-previous-header-field)
191
192;; "C-c /" prefix is used in mh-letter-mode by pgp.el and mailcrypt.el.
193
194\f
195
196;;; MH-Letter Help Messages
197
198;; Group messages logically, more or less.
199(defvar mh-letter-mode-help-messages
200 '((nil
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"
205 "Insert:\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"
210 "Security:\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.
215
216This is an associative array which is used to show the most
217common commands. The key is a prefix char. The value is one or
218more strings which are concatenated together and displayed in the
219minibuffer if ? is pressed after the prefix character. The
220special key nil is used to display the non-prefixed commands.
221
222The substitutions described in `substitute-command-keys' are
223performed as well.")
224
225\f
226
227;;; MH-Letter Font Lock
228
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.")
234
235(defun mh-font-lock-field-data (limit)
236 "Find header field region between point and LIMIT."
237 (and (< (point) (mh-letter-header-end))
238 (< (point) limit)
239 (let ((end (min limit (mh-letter-header-end)))
240 (point (point))
241 data-end data-begin field)
242 (end-of-line)
243 (setq data-end (if (re-search-forward "^[^ \t]" end t)
244 (match-beginning 0)
245 end))
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))
254 (set-match-data nil)
255 nil)
256 (t (set-match-data
257 (list data-begin data-end data-begin data-end))
258 t)))))
259
260(defun mh-letter-header-end ()
261 "Find the end of the message header.
262This function is to be used only for font locking. It works by
263searching for `mh-mail-header-separator' in the buffer."
264 (save-excursion
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)
d5dc8c56 268 (mh-line-beginning-position 0))
dda00b2c
BW
269 (t (point-min)))))
270
271\f
272
273;;; MH-Letter Mode
274
dda00b2c 275;; Shush compiler.
54a5db74
BW
276(mh-do-in-xemacs
277 (defvar font-lock-defaults))
dda00b2c 278
14acf2f5 279;; Ensure new buffers won't get this mode if default major-mode is nil.
dda00b2c
BW
280(put 'mh-letter-mode 'mode-class 'special)
281
282;;;###mh-autoload
283(define-derived-mode mh-letter-mode mail-mode "MH-Letter"
284 "Mode for composing letters in MH-E\\<mh-letter-mode-map>.
285
286When you have finished composing, type \\[mh-send-letter] to send
287the message using the MH mail handling system.
288
289There are two types of tags used by MH-E when composing MIME
290messages: MML and MH. The option `mh-compose-insertion' controls
291what type of tags are inserted by MH-E commands. These tags can
292be converted to MIME body parts by running \\[mh-mh-to-mime] for
293MH-style directives or \\[mh-mml-to-mime] for MML tags.
294
295Options that control this mode can be changed with
296\\[customize-group]; specify the \"mh-compose\" group.
297
298When a message is composed, the hooks `text-mode-hook',
299`mail-mode-hook', and `mh-letter-mode-hook' are run (in that
300order).
301
302\\{mh-letter-mode-map}"
303 (mh-find-path)
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)
310 (mh-do-in-gnu-emacs
d2464a9f
BW
311 (unless mh-letter-tool-bar-map
312 (mh-tool-bar-letter-buttons-init))
219d0838
GM
313 (if (boundp 'tool-bar-map)
314 (set (make-local-variable 'tool-bar-map) mh-letter-tool-bar-map)))
efc27af6
BW
315 (mh-do-in-xemacs
316 (mh-tool-bar-init :letter))
dda00b2c
BW
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)
320 (save-excursion
321 (goto-char (mh-mail-header-end))
d5dc8c56 322 (buffer-substring-no-properties (point) (mh-line-end-position))))
dda00b2c
BW
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)
328
329 ;; Enable undo since a show-mode buffer might have been reused.
330 (buffer-enable-undo)
dda00b2c
BW
331 (make-local-variable 'font-lock-defaults)
332 (cond
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)))
340 (t
341 ;; ...or the header only
342 (setq font-lock-defaults '((mh-show-font-lock-keywords) t))))
343 (easy-menu-add mh-letter-menu)
0f1f01d6
BW
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))
dda00b2c 349 (setq fill-column mh-letter-fill-column)
d1bb6623
SM
350 (add-hook 'completion-at-point-functions
351 'mh-letter-completion-at-point nil 'local)
dda00b2c
BW
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)))
356
357\f
358
359;;; MH-Letter Commands
360
361;; Alphabetical.
362;; See also mh-comp.el and mh-mime.el.
363
364(defun mh-check-whom ()
365 "Verify recipients, showing expansion of any aliases.
366
367This command expands aliases so you can check the actual address(es)
368in the alias. A new buffer named \"*MH-E Recipients*\" is created with
369the output of \"whom\"."
370 (interactive)
371 (let ((file-name buffer-file-name))
372 (save-buffer)
373 (message "Checking recipients...")
374 (mh-in-show-buffer (mh-recipients-buffer)
375 (bury-buffer (current-buffer))
376 (erase-buffer)
377 (mh-exec-cmd-output "whom" t file-name))
378 (message "Checking recipients...done")))
379
380(defun mh-insert-letter (folder message verbatim)
381 "Insert a message.
382
383This command prompts you for the FOLDER and MESSAGE number, which
384defaults to the current message in that folder. It then inserts
385the message, indented by `mh-ins-buf-prefix' (\"> \") unless
386`mh-yank-behavior' is set to one of the supercite flavors in
387which case supercite is used to format the message. Certain
388undesirable header fields (see
389`mh-invisible-header-fields-compiled') are removed before
390insertion.
391
392If given a prefix argument VERBATIM, the header is left intact, the
393message is not indented, and \"> \" is not inserted before each line.
394This command leaves the mark before the letter and point after it."
395 (interactive
396 (let* ((folder
15855f8f 397 (mh-prompt-for-folder "Message from" mh-sent-from-folder nil))
dda00b2c 398 (default
15855f8f
BW
399 (if (equal folder mh-sent-from-folder)
400 (or mh-sent-from-msg (nth 0 (mh-translate-range folder "cur")))
dda00b2c
BW
401 (nth 0 (mh-translate-range folder "cur"))))
402 (message
403 (read-string (concat "Message number"
404 (or (and default
405 (format " (default %d): " default))
15855f8f
BW
406 ": "))
407 nil nil
408 (if (numberp default)
409 (int-to-string default)
410 default))))
dda00b2c 411 (list folder message current-prefix-arg)))
15855f8f
BW
412 (if (equal message "")
413 (error "No message number given"))
dda00b2c
BW
414 (save-restriction
415 (narrow-to-region (point) (point))
416 (let ((start (point-min)))
dda00b2c
BW
417 (insert-file-contents
418 (expand-file-name message (mh-expand-file-name folder)))
419 (when (not verbatim)
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)))))
425
426;;;###mh-autoload
427(defun mh-insert-signature (&optional file)
428 "Insert signature in message.
429
430This command inserts your signature at the current cursor location.
431
432By default, the text of your signature is taken from the file
433\"~/.signature\". You can read from other sources by changing the
434option `mh-signature-file-name'.
435
436A signature separator (\"-- \") will be added if the signature block
437does not contain one and `mh-signature-separator-flag' is on.
438
439The hook `mh-insert-signature-hook' is run after the signature is
440inserted. Hook functions may access the actual name of the file or the
441function used to insert the signature with `mh-signature-file-name'.
442
443The signature can also be inserted using Identities (see
444`mh-identity-list').
445
446In a program, you can pass in a signature FILE."
447 (interactive)
448 (save-excursion
449 (insert "\n")
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)))
453 (save-restriction
454 (narrow-to-region (point) (point))
455 (cond
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))))
464 (t
465 (cond
466 (mh-mh-p
467 (insert "#\n" "Content-Description: Signature\n"))
468 (mh-mml-p
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)))))
477 (save-restriction
478 (widen)
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)))
485 (cond (mh-mh-p
486 (forward-line 2))
487 (mh-mml-p
488 (forward-line 1)))
489 (insert mh-signature-separator))
490 (if (not (> (point-max) (point-min)))
491 (message "No signature found")))))
492 (force-mode-line-update))
493
d1bb6623
SM
494(defun mh-letter-completion-at-point ()
495 "Return the completion data at point for MH letters.
496This 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)))))
502 (if func
503 (or (funcall func) #'ignore)
504 mh-letter-complete-function)))
505
24f75b10
BW
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
510;; mh-compat.el.
511(defun-mh mh-letter-complete completion-at-point ()
512 "Perform completion on header field or word preceding point.
dda00b2c
BW
513
514If the field contains addresses (for example, \"To:\" or \"Cc:\")
515or folders (for example, \"Fcc:\") then this command will provide
516alias completion. In the body of the message, this command runs
517`mh-letter-complete-function' instead, which is set to
d1bb6623
SM
518`ispell-complete-word' by default."
519 (interactive)
520 (let ((data (mh-letter-completion-at-point)))
521 (cond
522 ((functionp data) (funcall data))
523 ((consp data)
524 (let ((start (nth 0 data))
525 (end (nth 1 data))
526 (table (nth 2 data)))
527 (mh-complete-word (buffer-substring-no-properties start end)
24f75b10 528 table start end))))))
dda00b2c
BW
529
530(defun mh-letter-complete-or-space (arg)
531 "Perform completion or insert space.
532
533Turn on the option `mh-compose-space-does-completion-flag' to use
534this command to perform completion in the header. Otherwise, a
535space is inserted; use a prefix argument ARG to specify more than
536one space."
537 (interactive "p")
24f75b10 538 (let ((end-of-prev (save-excursion
dda00b2c
BW
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))
d1bb6623
SM
543 ;; FIXME: This > test is redundant now that all the completion
544 ;; functions do it anyway.
dda00b2c 545 ((> (point) end-of-prev) (self-insert-command arg))
d1bb6623
SM
546 ((let ((mh-letter-complete-function nil))
547 (mh-letter-completion-at-point))
548 (mh-letter-complete))
dda00b2c
BW
549 (t (self-insert-command arg)))))
550
551(defun mh-letter-confirm-address ()
552 "Flash alias expansion.
553
554Addresses are separated by a comma\; when you press the comma,
555this command flashes the alias expansion in the minibuffer if
556`mh-alias-flash-on-comma' is turned on."
557 (interactive)
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))))
565
566(defun mh-letter-next-header-field-or-indent (arg)
567 "Cycle to next field.
568
569Within the header of the message, this command moves between
570fields 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
573command then moves point to the message body before cycling back
574to the first field. If point is already past the first line of
575the message body, then this command indents by calling
576`indent-relative' with the given prefix argument ARG."
577 (interactive "P")
578 (let ((header-end (save-excursion
579 (goto-char (mh-mail-header-end))
580 (forward-line)
581 (point))))
582 (if (> (point) header-end)
583 (indent-relative arg)
584 (mh-letter-next-header-field))))
585
586(defun mh-letter-previous-header-field ()
587 "Cycle to the previous header field.
588
589This command moves backwards between the fields and cycles to the
590body of the message after the first field. Unlike the command
591\\[mh-letter-next-header-field-or-indent], it will always take
592point to the last field from anywhere in the body."
593 (interactive)
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)
604 (forward-line)))))
605
dda00b2c
BW
606(defun mh-open-line ()
607 "Insert a newline and leave point before it.
608
609This command is similar to the command \\[open-line] in that it
610inserts a newline after point. It differs in that it also inserts
611the right number of quoting characters and spaces so that the
612next line begins in the same column as it was. This is useful
613when breaking up paragraphs in replies."
614 (interactive)
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")
619 (newline 2)
620 (insert prefix)
621 (while (> column (current-column))
622 (insert " "))
623 (forward-line -1))))
624
625(defun mh-to-fcc (&optional folder)
626 "Move to \"Fcc:\" header field.
627
628This command will prompt you for the FOLDER name in which to file
629a copy of the draft."
630 (interactive (list (mh-prompt-for-folder
631 "Fcc"
632 (or (and mh-default-folder-for-message-function
633 (save-excursion
634 (goto-char (point-min))
635 (funcall
636 mh-default-folder-for-message-function)))
637 "")
638 t)))
0f69e911 639 (let ((last-input-event ?\C-f))
dda00b2c
BW
640 (expand-abbrev)
641 (save-excursion
642 (mh-to-field)
643 (insert (if (mh-folder-name-p folder)
644 (substring folder 1)
645 folder)))))
646
647(defvar mh-to-field-choices '(("a" . "Mail-Reply-To:")
648 ("b" . "Bcc:")
649 ("c" . "Cc:")
650 ("d" . "Dcc:")
651 ("f" . "Fcc:")
652 ("l" . "Mail-Followup-To:")
653 ("m" . "From:")
654 ("r" . "Reply-To:")
655 ("s" . "Subject:")
656 ("t" . "To:"))
657 "Alist of (final-character . field-name) choices for `mh-to-field'.")
658
659(defun mh-to-field ()
660 "Move to specified header field.
661
662The field is indicated by the previous keystroke (the last
663keystroke of the command) according to the list in the variable
664`mh-to-field-choices'.
665Create the field if it does not exist.
666Set the mark to point before moving."
667 (interactive)
668 (expand-abbrev)
0f69e911 669 (let ((target (cdr (or (assoc (char-to-string (logior last-input-event ?`))
dda00b2c
BW
670 mh-to-field-choices)
671 ;; also look for a char for version 4 compat
0f69e911 672 (assoc (logior last-input-event ?`)
dda00b2c
BW
673 mh-to-field-choices))))
674 (case-fold-search t))
675 (push-mark)
676 (cond ((mh-position-on-field target)
677 (let ((eol (point)))
678 (skip-chars-backward " \t")
679 (delete-region (point) eol))
0f69e911 680 (if (and (not (eq (logior last-input-event ?`) ?s))
dda00b2c
BW
681 (save-excursion
682 (backward-char 1)
683 (not (looking-at "[:,]"))))
684 (insert ", ")
685 (insert " ")))
686 (t
687 (if (mh-position-on-field "To:")
688 (forward-line 1))
689 (insert (format "%s \n" target))
690 (backward-char 1)))))
691
692;;;###mh-autoload
693(defun mh-yank-cur-msg ()
694 "Insert the current message into the draft buffer.
695
696It is often useful to insert a snippet of text from a letter that
697someone mailed to provide some context for your reply. This
698command does this by adding an attribution, yanking a portion of
699text from the message to which you're replying, and inserting
700`mh-ins-buf-prefix' (`> ') before each line.
701
702The attribution consists of the sender's name and email address
703followed by the content of the option
704`mh-extract-from-attribution-verb'.
705
706You can also turn on the option
707`mh-delete-yanked-msg-window-flag' to delete the window
708containing the original message after yanking it to make more
709room on your screen for your reply.
710
711You can control how the message to which you are replying is
712yanked into your reply using `mh-yank-behavior'.
713
714If this isn't enough, you can gain full control over the
715appearance of the included text by setting `mail-citation-hook'
716to a function that modifies it. For example, if you set this hook
717to `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').
720
721Note that if `mail-citation-hook' is set, `mh-ins-buf-prefix' is
722not inserted. If the option `mh-yank-behavior' is set to one of
723the supercite flavors, the hook `mail-citation-hook' is ignored
724and `mh-ins-buf-prefix' is not inserted."
725 (interactive)
726 (if (and mh-sent-from-folder
b5553d47
SM
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))
dda00b2c
BW
730 mh-sent-from-msg)
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))
739 (mh-ins-str
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
745 (concat
746 (buffer-substring (point-min) (mh-mail-header-end))
747 "\n"
748 (buffer-substring (region-beginning) (region-end))))
749 (yank-region
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))
754 (buffer-substring
755 (save-excursion
756 (goto-char (point-min))
757 (mh-goto-header-end 1)
758 (point))
759 (point-max)))
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)))
764 (t
765 (buffer-substring (point) (point-max))))))
766 (set-buffer to-buffer)
767 (save-restriction
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))
776 (insert from-attr)
777 (mh-identity-insert-attribution-verb nil)
778 (insert "\n\n"))
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
783 ;; text.
784 (push-mark)
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")))
789
790\f
791
792;;; Support Routines
793
794(defun mh-auto-fill-for-letter ()
795 "Perform auto-fill for message.
796Header is treated specially by inserting a tab before continuation
797lines."
798 (if (mh-in-header-p)
799 (let ((fill-prefix "\t"))
800 (do-auto-fill))
801 (do-auto-fill)))
802
803(defun mh-filter-out-non-text (string)
804 "Return STRING but without adornments such as MIME buttons and smileys."
805 (with-temp-buffer
806 ;; Insert the string to filter
807 (insert string)
808 (goto-char (point-min))
809
810 ;; Remove the MIME buttons
811 (let ((can-move-forward t)
812 (in-button nil))
813 (while can-move-forward
814 (cond ((and (not (get-text-property (point) 'mh-data))
815 in-button)
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)))
821 (setq in-button t))
822 (t (setq can-move-forward (= (forward-line) 0))))))
823
824 ;; Return the contents without properties... This gets rid of emphasis
825 ;; and smileys
826 (buffer-substring-no-properties (point-min) (point-max))))
827
828(defun mh-current-fill-prefix ()
829 "Return the `fill-prefix' on the current line as a string."
830 (save-excursion
831 (beginning-of-line)
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)
837 (match-string 0)
838 "")))
839
840;;;###mh-autoload
841(defun mh-letter-next-header-field ()
842 "Cycle to the next header field.
843If we are at the last header field go to the start of the message
844body."
845 (let ((header-end (mh-mail-header-end)))
846 (cond ((>= (point) header-end) (goto-char (point-min)))
847 ((< (point) (progn
848 (beginning-of-line)
849 (re-search-forward mh-letter-header-field-regexp
d5dc8c56 850 (mh-line-end-position) t)
dda00b2c
BW
851 (point)))
852 (beginning-of-line))
853 (t (end-of-line)))
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)
859 (forward-line)))))
860
dda00b2c
BW
861;;;###mh-autoload
862(defun mh-position-on-field (field &optional ignored)
863 "Move to the end of the FIELD in the header.
864Move to end of entire header if FIELD not found.
df26688b 865Returns non-nil if FIELD was found.
dda00b2c
BW
866The optional second arg is for pre-version 4 compatibility and is
867IGNORED."
868 (cond ((mh-goto-header-field field)
869 (mh-header-field-end)
870 t)
871 ((mh-goto-header-end 0)
872 nil)))
873
874(defun mh-letter-header-field-at-point ()
875 "Return the header field name at point.
876A symbol is returned whose name is the string obtained by
877downcasing the field name."
878 (save-excursion
879 (end-of-line)
880 (and (re-search-backward mh-letter-header-field-regexp nil t)
881 (intern (downcase (match-string 1))))))
882
883(defun mh-folder-expand-at-point ()
884 "Do folder name completion in Fcc header field."
d1bb6623
SM
885 (let* ((beg (mh-beginning-of-word))
886 (end (save-excursion
887 (goto-char beg)
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))))
dda00b2c
BW
892
893;;;###mh-autoload
894(defun mh-complete-word (word choices begin end)
b7149a81 895 "Complete WORD from CHOICES.
dda00b2c
BW
896Any 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)
900 (ignore-errors
901 (kill-buffer completions-buffer))
902 (message "Completed: %s" word))
903 ((null completion)
904 (ignore-errors
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
d1bb6623
SM
910 (mh-display-completion-list
911 (all-completions word choices)
22bcf204 912 ;; The `common-substring' arg only works if it's a prefix.
d1bb6623
SM
913 (unless (and (functionp choices)
914 (let ((bounds
915 (funcall choices
916 word nil '(boundaries . ""))))
917 (and (eq 'boundaries (car-safe bounds))
918 (< 0 (cadr bounds)))))
919 word)))
dda00b2c
BW
920 (ignore-errors
921 (kill-buffer completions-buffer))
922 (delete-region begin end)
923 (insert completion))))))
924
925(defun mh-file-is-vcard-p (file)
926 "Return t if FILE is a .vcf vcard."
927 (let ((case-fold-search t))
928 (and (stringp file)
929 (file-exists-p file)
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))))))
933
a55f450f 934;;;###mh-autoload
dda00b2c
BW
935(defun mh-letter-toggle-header-field-display-button (event)
936 "Toggle header field display at location of EVENT.
937This function does the same thing as
938`mh-letter-toggle-header-field-display' except that it is
939callable from a mouse button."
940 (interactive "e")
941 (mh-do-at-event-location event
942 (mh-letter-toggle-header-field-display nil)))
943
dda00b2c
BW
944(defun mh-extract-from-attribution ()
945 "Extract phrase or comment from From header field."
946 (save-excursion
947 (if (not (mh-goto-header-field "From: "))
948 nil
949 (skip-chars-forward " ")
950 (cond
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)))))))
959
960(defun mh-insert-prefix-string (mh-ins-string)
961 "Insert prefix string before each line in buffer.
962The inserted letter is cited using `sc-cite-original' if
963`mh-yank-behavior' is one of 'supercite or 'autosupercite.
964Otherwise, 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))
968 (sc-cite-original))
969 (mail-citation-hook
970 (run-hooks 'mail-citation-hook))
971 (mh-yank-hooks ;old hook name
972 (run-hooks 'mh-yank-hooks))
973 (t
974 (or (bolp) (forward-line 1))
975 (while (< (point) (point-max))
976 (insert mh-ins-string)
977 (forward-line 1))
978 (goto-char (point-min))))) ;leave point like sc-cite-original
979
980(provide 'mh-letter)
981
982;; Local Variables:
983;; indent-tabs-mode: nil
984;; sentence-end-double-space: nil
985;; End:
986
987;;; mh-letter.el ends here