avoid recursive `require' when loading semantic
[bpt/emacs.git] / lisp / mh-e / mh-letter.el
CommitLineData
dda00b2c
BW
1;;; mh-letter.el --- MH-Letter mode
2
ba318903 3;; Copyright (C) 1993, 1995, 1997, 2000-2014 Free Software Foundation,
ab422c4d 4;; Inc.
dda00b2c
BW
5
6;; Author: Bill Wohler <wohler@newt.com>
7;; Maintainer: Bill Wohler <wohler@newt.com>
8;; Keywords: mail
9;; See: mh-e.el
10
11;; This file is part of GNU Emacs.
12
5e809f55 13;; GNU Emacs is free software: you can redistribute it and/or modify
dda00b2c 14;; it under the terms of the GNU General Public License as published by
5e809f55
GM
15;; the Free Software Foundation, either version 3 of the License, or
16;; (at your option) any later version.
dda00b2c
BW
17
18;; GNU Emacs is distributed in the hope that it will be useful,
19;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21;; GNU General Public License for more details.
22
23;; You should have received a copy of the GNU General Public License
5e809f55 24;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
dda00b2c
BW
25
26;;; Commentary:
27
28;; Mode for composing and sending a draft message.
29
30;; Functions that would ordinarily be in here that are needed by
31;; mh-show.el should be placed in the Message Utilities section in
32;; mh-utils.el. That will help prevent the loading of this file until
33;; a message is actually composed.
34
35;;; Change Log:
36
37;;; Code:
38
39(require 'mh-e)
40
41(require 'gnus-util)
42
efc27af6 43;; Dynamically-created functions not found in mh-loaddefs.el.
dda00b2c 44(autoload 'mh-tool-bar-letter-buttons-init "mh-tool-bar")
efc27af6 45(autoload 'mh-tool-bar-init "mh-tool-bar")
dda00b2c
BW
46
47(autoload 'mml-insert-tag "mml")
48
49;;; Variables
50
51(defvar mh-letter-complete-function-alist
52 '((bcc . mh-alias-letter-expand-alias)
53 (cc . mh-alias-letter-expand-alias)
54 (dcc . mh-alias-letter-expand-alias)
55 (fcc . mh-folder-expand-at-point)
56 (from . mh-alias-letter-expand-alias)
57 (mail-followup-to . mh-alias-letter-expand-alias)
58 (mail-reply-to . mh-alias-letter-expand-alias)
59 (reply-to . mh-alias-letter-expand-alias)
60 (to . mh-alias-letter-expand-alias))
61 "Alist of header fields and completion functions to use.")
62
dda00b2c
BW
63(defvar mh-yank-hooks nil
64 "Obsolete hook for modifying a citation just inserted in the mail buffer.
65
66Each hook function can find the citation between point and mark.
67And each hook function should leave point and mark around the
68citation text as modified.
69
d1069532
SM
70This is a normal hook, misnamed for historical reasons.
71It is obsolete and is only used if `mail-citation-hook' is nil.")
fb9958d7 72(mh-make-obsolete-variable 'mh-yank-hooks 'mail-citation-hook "19.34")
dda00b2c
BW
73
74\f
75
76;;; Letter Menu
77
dda00b2c
BW
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]
110 )
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]))
125
126\f
127
128;;; MH-Letter Keys
129
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
24f75b10 190 "\M-\t" mh-letter-complete
dda00b2c
BW
191 "\t" mh-letter-next-header-field-or-indent
192 [backtab] mh-letter-previous-header-field)
193
194;; "C-c /" prefix is used in mh-letter-mode by pgp.el and mailcrypt.el.
195
196\f
197
198;;; MH-Letter Help Messages
199
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.
217
218This is an associative array which is used to show the most
219common commands. The key is a prefix char. The value is one or
220more strings which are concatenated together and displayed in the
221minibuffer if ? is pressed after the prefix character. The
222special key nil is used to display the non-prefixed commands.
223
224The substitutions described in `substitute-command-keys' are
225performed as well.")
226
227\f
228
229;;; MH-Letter Font Lock
230
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.")
236
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)))))
261
262(defun mh-letter-header-end ()
263 "Find the end of the message header.
264This function is to be used only for font locking. It works by
265searching 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)
d5dc8c56 270 (mh-line-beginning-position 0))
dda00b2c
BW
271 (t (point-min)))))
272
273\f
274
275;;; MH-Letter Mode
276
dda00b2c 277;; Shush compiler.
54a5db74
BW
278(mh-do-in-xemacs
279 (defvar font-lock-defaults))
dda00b2c 280
14acf2f5 281;; Ensure new buffers won't get this mode if default major-mode is nil.
dda00b2c
BW
282(put 'mh-letter-mode 'mode-class 'special)
283
284;;;###mh-autoload
285(define-derived-mode mh-letter-mode mail-mode "MH-Letter"
286 "Mode for composing letters in MH-E\\<mh-letter-mode-map>.
287
288When you have finished composing, type \\[mh-send-letter] to send
289the message using the MH mail handling system.
290
291There are two types of tags used by MH-E when composing MIME
292messages: MML and MH. The option `mh-compose-insertion' controls
293what type of tags are inserted by MH-E commands. These tags can
294be converted to MIME body parts by running \\[mh-mh-to-mime] for
295MH-style directives or \\[mh-mml-to-mime] for MML tags.
296
297Options that control this mode can be changed with
298\\[customize-group]; specify the \"mh-compose\" group.
299
300When a message is composed, the hooks `text-mode-hook',
301`mail-mode-hook', and `mh-letter-mode-hook' are run (in that
302order).
303
304\\{mh-letter-mode-map}"
305 (mh-find-path)
306 (make-local-variable 'mh-send-args)
307 (make-local-variable 'mh-annotate-char)
308 (make-local-variable 'mh-annotate-field)
309 (make-local-variable 'mh-previous-window-config)
310 (make-local-variable 'mh-sent-from-folder)
311 (make-local-variable 'mh-sent-from-msg)
312 (mh-do-in-gnu-emacs
d2464a9f
BW
313 (unless mh-letter-tool-bar-map
314 (mh-tool-bar-letter-buttons-init))
219d0838
GM
315 (if (boundp 'tool-bar-map)
316 (set (make-local-variable 'tool-bar-map) mh-letter-tool-bar-map)))
efc27af6
BW
317 (mh-do-in-xemacs
318 (mh-tool-bar-init :letter))
dda00b2c
BW
319 ;; Set the local value of mh-mail-header-separator according to what is
320 ;; present in the buffer...
321 (set (make-local-variable 'mh-mail-header-separator)
322 (save-excursion
323 (goto-char (mh-mail-header-end))
d5dc8c56 324 (buffer-substring-no-properties (point) (mh-line-end-position))))
dda00b2c
BW
325 (make-local-variable 'mail-header-separator)
326 (setq mail-header-separator mh-mail-header-separator) ;override sendmail.el
327 (mh-set-help mh-letter-mode-help-messages)
328 (setq buffer-invisibility-spec '((vanish . t) t))
329 (set (make-local-variable 'line-move-ignore-invisible) t)
330
331 ;; Enable undo since a show-mode buffer might have been reused.
332 (buffer-enable-undo)
dda00b2c
BW
333 (make-local-variable 'font-lock-defaults)
334 (cond
335 ((or (equal mh-highlight-citation-style 'font-lock)
336 (equal mh-highlight-citation-style 'gnus))
337 ;; Let's use font-lock even if gnus is used in show-mode. The reason
338 ;; is that gnus uses static text properties which are not appropriate
339 ;; for a buffer that will be edited. So the choice here is either fontify
340 ;; the citations and header...
341 (setq font-lock-defaults '(mh-letter-font-lock-keywords t)))
342 (t
343 ;; ...or the header only
344 (setq font-lock-defaults '((mh-show-font-lock-keywords) t))))
345 (easy-menu-add mh-letter-menu)
0f1f01d6
BW
346 ;; Maybe we want to use the existing Mail menu from mail-mode in
347 ;; 9.0; in the mean time, let's remove it since the redundancy will
348 ;; only produce confusion.
349 (define-key mh-letter-mode-map [menu-bar mail] 'undefined)
350 (mh-do-in-xemacs (easy-menu-remove mail-menubar-menu))
dda00b2c 351 (setq fill-column mh-letter-fill-column)
d1bb6623
SM
352 (add-hook 'completion-at-point-functions
353 'mh-letter-completion-at-point nil 'local)
dda00b2c
BW
354 ;; If text-mode-hook turned on auto-fill, tune it for messages
355 (when auto-fill-function
356 (make-local-variable 'auto-fill-function)
357 (setq auto-fill-function 'mh-auto-fill-for-letter)))
358
359\f
360
361;;; MH-Letter Commands
362
363;; Alphabetical.
364;; See also mh-comp.el and mh-mime.el.
365
366(defun mh-check-whom ()
367 "Verify recipients, showing expansion of any aliases.
368
369This command expands aliases so you can check the actual address(es)
370in the alias. A new buffer named \"*MH-E Recipients*\" is created with
371the output of \"whom\"."
372 (interactive)
373 (let ((file-name buffer-file-name))
374 (save-buffer)
375 (message "Checking recipients...")
376 (mh-in-show-buffer (mh-recipients-buffer)
377 (bury-buffer (current-buffer))
378 (erase-buffer)
379 (mh-exec-cmd-output "whom" t file-name))
380 (message "Checking recipients...done")))
381
382(defun mh-insert-letter (folder message verbatim)
383 "Insert a message.
384
385This command prompts you for the FOLDER and MESSAGE number, which
386defaults to the current message in that folder. It then inserts
387the message, indented by `mh-ins-buf-prefix' (\"> \") unless
388`mh-yank-behavior' is set to one of the supercite flavors in
389which case supercite is used to format the message. Certain
390undesirable header fields (see
391`mh-invisible-header-fields-compiled') are removed before
392insertion.
393
394If given a prefix argument VERBATIM, the header is left intact, the
395message is not indented, and \"> \" is not inserted before each line.
396This command leaves the mark before the letter and point after it."
397 (interactive
398 (let* ((folder
15855f8f 399 (mh-prompt-for-folder "Message from" mh-sent-from-folder nil))
dda00b2c 400 (default
15855f8f
BW
401 (if (equal folder mh-sent-from-folder)
402 (or mh-sent-from-msg (nth 0 (mh-translate-range folder "cur")))
dda00b2c
BW
403 (nth 0 (mh-translate-range folder "cur"))))
404 (message
405 (read-string (concat "Message number"
406 (or (and default
407 (format " (default %d): " default))
15855f8f
BW
408 ": "))
409 nil nil
410 (if (numberp default)
411 (int-to-string default)
412 default))))
dda00b2c 413 (list folder message current-prefix-arg)))
15855f8f
BW
414 (if (equal message "")
415 (error "No message number given"))
dda00b2c
BW
416 (save-restriction
417 (narrow-to-region (point) (point))
418 (let ((start (point-min)))
dda00b2c
BW
419 (insert-file-contents
420 (expand-file-name message (mh-expand-file-name folder)))
421 (when (not verbatim)
422 (mh-clean-msg-header start mh-invisible-header-fields-compiled nil)
423 (goto-char (point-max)) ;Needed for sc-cite-original
424 (push-mark) ;Needed for sc-cite-original
425 (goto-char (point-min)) ;Needed for sc-cite-original
426 (mh-insert-prefix-string mh-ins-buf-prefix)))))
427
428;;;###mh-autoload
429(defun mh-insert-signature (&optional file)
430 "Insert signature in message.
431
432This command inserts your signature at the current cursor location.
433
434By default, the text of your signature is taken from the file
435\"~/.signature\". You can read from other sources by changing the
436option `mh-signature-file-name'.
437
438A signature separator (\"-- \") will be added if the signature block
439does not contain one and `mh-signature-separator-flag' is on.
440
441The hook `mh-insert-signature-hook' is run after the signature is
442inserted. Hook functions may access the actual name of the file or the
443function used to insert the signature with `mh-signature-file-name'.
444
445The signature can also be inserted using Identities (see
446`mh-identity-list').
447
448In a program, you can pass in a signature FILE."
449 (interactive)
450 (save-excursion
451 (insert "\n")
452 (let ((mh-signature-file-name (or file mh-signature-file-name))
453 (mh-mh-p (mh-mh-directive-present-p))
454 (mh-mml-p (mh-mml-tag-present-p)))
455 (save-restriction
456 (narrow-to-region (point) (point))
457 (cond
458 ((mh-file-is-vcard-p mh-signature-file-name)
459 (if (equal mh-compose-insertion 'mml)
460 (insert "<#part type=\"text/x-vcard\" filename=\""
461 mh-signature-file-name
462 "\" disposition=inline description=VCard>\n<#/part>")
463 (insert "#text/x-vcard; name=\""
464 (file-name-nondirectory mh-signature-file-name)
465 "\" [VCard] " (expand-file-name mh-signature-file-name))))
466 (t
467 (cond
468 (mh-mh-p
469 (insert "#\n" "Content-Description: Signature\n"))
470 (mh-mml-p
471 (mml-insert-tag 'part 'type "text/plain" 'disposition "inline"
472 'description "Signature")))
473 (cond ((null mh-signature-file-name))
474 ((and (stringp mh-signature-file-name)
475 (file-readable-p mh-signature-file-name))
476 (insert-file-contents mh-signature-file-name))
477 ((functionp mh-signature-file-name)
478 (funcall mh-signature-file-name)))))
479 (save-restriction
480 (widen)
481 (run-hooks 'mh-insert-signature-hook))
482 (goto-char (point-min))
483 (when (and (not (mh-file-is-vcard-p mh-signature-file-name))
484 mh-signature-separator-flag
485 (> (point-max) (point-min))
486 (not (mh-signature-separator-p)))
487 (cond (mh-mh-p
488 (forward-line 2))
489 (mh-mml-p
490 (forward-line 1)))
491 (insert mh-signature-separator))
492 (if (not (> (point-max) (point-min)))
493 (message "No signature found")))))
494 (force-mode-line-update))
495
d1bb6623
SM
496(defun mh-letter-completion-at-point ()
497 "Return the completion data at point for MH letters.
498This provides alias and folder completion in header fields according to
499`mh-letter-complete-function-alist' and falls back on
500`mh-letter-complete-function-alist' elsewhere."
501 (let ((func (and (mh-in-header-p)
502 (cdr (assoc (mh-letter-header-field-at-point)
503 mh-letter-complete-function-alist)))))
504 (if func
505 (or (funcall func) #'ignore)
506 mh-letter-complete-function)))
507
24f75b10
BW
508;; TODO Now that completion-at-point performs the task of
509;; mh-letter-complete, perhaps mh-letter-complete along with
510;; mh-complete-word should be rewritten as a more general function for
511;; XEmacs, renamed to mh-completion-at-point, and moved to
512;; mh-compat.el.
513(defun-mh mh-letter-complete completion-at-point ()
514 "Perform completion on header field or word preceding point.
dda00b2c
BW
515
516If the field contains addresses (for example, \"To:\" or \"Cc:\")
517or folders (for example, \"Fcc:\") then this command will provide
518alias completion. In the body of the message, this command runs
519`mh-letter-complete-function' instead, which is set to
d1bb6623
SM
520`ispell-complete-word' by default."
521 (interactive)
522 (let ((data (mh-letter-completion-at-point)))
523 (cond
524 ((functionp data) (funcall data))
525 ((consp data)
526 (let ((start (nth 0 data))
527 (end (nth 1 data))
528 (table (nth 2 data)))
529 (mh-complete-word (buffer-substring-no-properties start end)
24f75b10 530 table start end))))))
dda00b2c
BW
531
532(defun mh-letter-complete-or-space (arg)
533 "Perform completion or insert space.
534
535Turn on the option `mh-compose-space-does-completion-flag' to use
536this command to perform completion in the header. Otherwise, a
537space is inserted; use a prefix argument ARG to specify more than
538one space."
539 (interactive "p")
24f75b10 540 (let ((end-of-prev (save-excursion
dda00b2c
BW
541 (goto-char (mh-beginning-of-word))
542 (mh-beginning-of-word -1))))
543 (cond ((not mh-compose-space-does-completion-flag)
544 (self-insert-command arg))
d1bb6623
SM
545 ;; FIXME: This > test is redundant now that all the completion
546 ;; functions do it anyway.
dda00b2c 547 ((> (point) end-of-prev) (self-insert-command arg))
d1bb6623
SM
548 ((let ((mh-letter-complete-function nil))
549 (mh-letter-completion-at-point))
550 (mh-letter-complete))
dda00b2c
BW
551 (t (self-insert-command arg)))))
552
553(defun mh-letter-confirm-address ()
554 "Flash alias expansion.
555
556Addresses are separated by a comma\; when you press the comma,
557this command flashes the alias expansion in the minibuffer if
558`mh-alias-flash-on-comma' is turned on."
559 (interactive)
560 (cond ((not (mh-in-header-p)) (self-insert-command 1))
561 ((eq (cdr (assoc (mh-letter-header-field-at-point)
562 mh-letter-complete-function-alist))
563 'mh-alias-letter-expand-alias)
564 (mh-alias-reload-maybe)
565 (mh-alias-minibuffer-confirm-address))
566 (t (self-insert-command 1))))
567
568(defun mh-letter-next-header-field-or-indent (arg)
569 "Cycle to next field.
570
571Within the header of the message, this command moves between
572fields that are highlighted with the face
573`mh-letter-header-field', skipping those fields listed in
574`mh-compose-skipped-header-fields'. After the last field, this
575command then moves point to the message body before cycling back
576to the first field. If point is already past the first line of
577the message body, then this command indents by calling
578`indent-relative' with the given prefix argument ARG."
579 (interactive "P")
580 (let ((header-end (save-excursion
581 (goto-char (mh-mail-header-end))
582 (forward-line)
583 (point))))
584 (if (> (point) header-end)
585 (indent-relative arg)
586 (mh-letter-next-header-field))))
587
588(defun mh-letter-previous-header-field ()
589 "Cycle to the previous header field.
590
591This command moves backwards between the fields and cycles to the
592body of the message after the first field. Unlike the command
593\\[mh-letter-next-header-field-or-indent], it will always take
594point to the last field from anywhere in the body."
595 (interactive)
596 (let ((header-end (mh-mail-header-end)))
597 (if (>= (point) header-end)
598 (goto-char header-end)
599 (mh-header-field-beginning))
600 (cond ((re-search-backward mh-letter-header-field-regexp nil t)
601 (if (mh-letter-skipped-header-field-p (match-string 1))
602 (mh-letter-previous-header-field)
603 (goto-char (match-end 0))
604 (mh-letter-skip-leading-whitespace-in-header-field)))
605 (t (goto-char header-end)
606 (forward-line)))))
607
dda00b2c
BW
608(defun mh-open-line ()
609 "Insert a newline and leave point before it.
610
611This command is similar to the command \\[open-line] in that it
612inserts a newline after point. It differs in that it also inserts
613the right number of quoting characters and spaces so that the
614next line begins in the same column as it was. This is useful
615when breaking up paragraphs in replies."
616 (interactive)
617 (let ((column (current-column))
618 (prefix (mh-current-fill-prefix)))
619 (if (> (length prefix) column)
620 (message "Sorry, point seems to be within the line prefix")
621 (newline 2)
622 (insert prefix)
623 (while (> column (current-column))
624 (insert " "))
625 (forward-line -1))))
626
627(defun mh-to-fcc (&optional folder)
628 "Move to \"Fcc:\" header field.
629
630This command will prompt you for the FOLDER name in which to file
631a copy of the draft."
632 (interactive (list (mh-prompt-for-folder
633 "Fcc"
634 (or (and mh-default-folder-for-message-function
635 (save-excursion
636 (goto-char (point-min))
637 (funcall
638 mh-default-folder-for-message-function)))
639 "")
640 t)))
0f69e911 641 (let ((last-input-event ?\C-f))
dda00b2c
BW
642 (expand-abbrev)
643 (save-excursion
644 (mh-to-field)
645 (insert (if (mh-folder-name-p folder)
646 (substring folder 1)
647 folder)))))
648
649(defvar mh-to-field-choices '(("a" . "Mail-Reply-To:")
650 ("b" . "Bcc:")
651 ("c" . "Cc:")
652 ("d" . "Dcc:")
653 ("f" . "Fcc:")
654 ("l" . "Mail-Followup-To:")
655 ("m" . "From:")
656 ("r" . "Reply-To:")
657 ("s" . "Subject:")
658 ("t" . "To:"))
659 "Alist of (final-character . field-name) choices for `mh-to-field'.")
660
661(defun mh-to-field ()
662 "Move to specified header field.
663
664The field is indicated by the previous keystroke (the last
665keystroke of the command) according to the list in the variable
666`mh-to-field-choices'.
667Create the field if it does not exist.
668Set the mark to point before moving."
669 (interactive)
670 (expand-abbrev)
0f69e911 671 (let ((target (cdr (or (assoc (char-to-string (logior last-input-event ?`))
dda00b2c
BW
672 mh-to-field-choices)
673 ;; also look for a char for version 4 compat
0f69e911 674 (assoc (logior last-input-event ?`)
dda00b2c
BW
675 mh-to-field-choices))))
676 (case-fold-search t))
677 (push-mark)
678 (cond ((mh-position-on-field target)
679 (let ((eol (point)))
680 (skip-chars-backward " \t")
681 (delete-region (point) eol))
0f69e911 682 (if (and (not (eq (logior last-input-event ?`) ?s))
dda00b2c
BW
683 (save-excursion
684 (backward-char 1)
685 (not (looking-at "[:,]"))))
686 (insert ", ")
687 (insert " ")))
688 (t
689 (if (mh-position-on-field "To:")
690 (forward-line 1))
691 (insert (format "%s \n" target))
692 (backward-char 1)))))
693
694;;;###mh-autoload
695(defun mh-yank-cur-msg ()
696 "Insert the current message into the draft buffer.
697
698It is often useful to insert a snippet of text from a letter that
699someone mailed to provide some context for your reply. This
700command does this by adding an attribution, yanking a portion of
701text from the message to which you're replying, and inserting
702`mh-ins-buf-prefix' (`> ') before each line.
703
704The attribution consists of the sender's name and email address
705followed by the content of the option
706`mh-extract-from-attribution-verb'.
707
708You can also turn on the option
709`mh-delete-yanked-msg-window-flag' to delete the window
710containing the original message after yanking it to make more
711room on your screen for your reply.
712
713You can control how the message to which you are replying is
714yanked into your reply using `mh-yank-behavior'.
715
716If this isn't enough, you can gain full control over the
717appearance of the included text by setting `mail-citation-hook'
718to a function that modifies it. For example, if you set this hook
719to `trivial-cite' (which is NOT part of Emacs), set
720`mh-yank-behavior' to \"Body and Header\" (see URL
721`http://shasta.cs.uiuc.edu/~lrclause/tc.html').
722
723Note that if `mail-citation-hook' is set, `mh-ins-buf-prefix' is
724not inserted. If the option `mh-yank-behavior' is set to one of
725the supercite flavors, the hook `mail-citation-hook' is ignored
726and `mh-ins-buf-prefix' is not inserted."
727 (interactive)
40f1ca7c
JH
728 (let ((show-buffer))
729 (if (and mh-sent-from-folder
730 (with-current-buffer mh-sent-from-folder mh-show-buffer)
731 (setq show-buffer (with-current-buffer mh-sent-from-folder
732 (get-buffer mh-show-buffer)))
733 mh-sent-from-msg)
734 (let ((to-point (point))
735 (to-buffer (current-buffer)))
736 (if mh-delete-yanked-msg-window-flag
737 (with-current-buffer mh-sent-from-folder
738 (delete-windows-on show-buffer)))
739 ;; Find displayed message
740 (with-current-buffer show-buffer
741 (let* ((from-attr (mh-extract-from-attribution))
742 (yank-region (mh-mark-active-p nil))
743 (mh-ins-str
744 (cond ((and yank-region
745 (or (eq 'supercite mh-yank-behavior)
746 (eq 'autosupercite mh-yank-behavior)
747 (eq t mh-yank-behavior)))
748 ;; supercite needs the full header
749 (concat
750 (buffer-substring (point-min) (mh-mail-header-end))
751 "\n"
752 (buffer-substring (region-beginning) (region-end))))
753 (yank-region
754 (buffer-substring (region-beginning) (region-end)))
755 ((or (eq 'body mh-yank-behavior)
756 (eq 'attribution mh-yank-behavior)
757 (eq 'autoattrib mh-yank-behavior))
758 (buffer-substring
759 (save-excursion
760 (goto-char (point-min))
761 (mh-goto-header-end 1)
762 (point))
763 (point-max)))
764 ((or (eq 'supercite mh-yank-behavior)
765 (eq 'autosupercite mh-yank-behavior)
766 (eq t mh-yank-behavior))
767 (buffer-substring (point-min) (point-max)))
768 (t
769 (buffer-substring (point) (point-max))))))
770 (with-current-buffer to-buffer
771 (save-restriction
772 (narrow-to-region to-point to-point)
773 (insert (mh-filter-out-non-text mh-ins-str))
774 (goto-char (point-max)) ;Needed for sc-cite-original
775 (push-mark) ;Needed for sc-cite-original
776 (goto-char (point-min)) ;Needed for sc-cite-original
777 (mh-insert-prefix-string mh-ins-buf-prefix)
778 (when (or (eq 'attribution mh-yank-behavior)
779 (eq 'autoattrib mh-yank-behavior))
780 (insert from-attr)
781 (mh-identity-insert-attribution-verb nil)
782 (insert "\n\n"))
783 ;; If the user has selected a region, he has already "edited" the
784 ;; text, so leave the cursor at the end of the yanked text. In
785 ;; either case, leave a mark at the opposite end of the included
786 ;; text to make it easy to jump or delete to the other end of the
787 ;; text.
788 (push-mark)
789 (goto-char (point-max))
790 (if (null yank-region)
791 (mh-exchange-point-and-mark-preserving-active-mark)))))))
792 (error "There is no current message"))))
dda00b2c
BW
793
794\f
795
796;;; Support Routines
797
798(defun mh-auto-fill-for-letter ()
799 "Perform auto-fill for message.
800Header is treated specially by inserting a tab before continuation
801lines."
802 (if (mh-in-header-p)
803 (let ((fill-prefix "\t"))
804 (do-auto-fill))
805 (do-auto-fill)))
806
807(defun mh-filter-out-non-text (string)
808 "Return STRING but without adornments such as MIME buttons and smileys."
809 (with-temp-buffer
810 ;; Insert the string to filter
811 (insert string)
812 (goto-char (point-min))
813
814 ;; Remove the MIME buttons
815 (let ((can-move-forward t)
816 (in-button nil))
817 (while can-move-forward
818 (cond ((and (not (get-text-property (point) 'mh-data))
819 in-button)
820 (delete-region (1- (point)) (point))
821 (setq in-button nil))
822 ((get-text-property (point) 'mh-data)
823 (delete-region (point)
824 (save-excursion (forward-line) (point)))
825 (setq in-button t))
826 (t (setq can-move-forward (= (forward-line) 0))))))
827
828 ;; Return the contents without properties... This gets rid of emphasis
829 ;; and smileys
830 (buffer-substring-no-properties (point-min) (point-max))))
831
832(defun mh-current-fill-prefix ()
833 "Return the `fill-prefix' on the current line as a string."
834 (save-excursion
835 (beginning-of-line)
836 ;; This assumes that the major-mode sets up adaptive-fill-regexp
837 ;; correctly such as mh-letter-mode or sendmail.el's mail-mode. But
838 ;; perhaps I should use the variable and simply inserts its value here,
839 ;; and set it locally in a let scope. --psg
840 (if (re-search-forward adaptive-fill-regexp nil t)
841 (match-string 0)
842 "")))
843
844;;;###mh-autoload
845(defun mh-letter-next-header-field ()
846 "Cycle to the next header field.
847If we are at the last header field go to the start of the message
848body."
849 (let ((header-end (mh-mail-header-end)))
850 (cond ((>= (point) header-end) (goto-char (point-min)))
851 ((< (point) (progn
852 (beginning-of-line)
853 (re-search-forward mh-letter-header-field-regexp
d5dc8c56 854 (mh-line-end-position) t)
dda00b2c
BW
855 (point)))
856 (beginning-of-line))
857 (t (end-of-line)))
858 (cond ((re-search-forward mh-letter-header-field-regexp header-end t)
859 (if (mh-letter-skipped-header-field-p (match-string 1))
860 (mh-letter-next-header-field)
861 (mh-letter-skip-leading-whitespace-in-header-field)))
862 (t (goto-char header-end)
863 (forward-line)))))
864
dda00b2c
BW
865;;;###mh-autoload
866(defun mh-position-on-field (field &optional ignored)
867 "Move to the end of the FIELD in the header.
868Move to end of entire header if FIELD not found.
df26688b 869Returns non-nil if FIELD was found.
dda00b2c
BW
870The optional second arg is for pre-version 4 compatibility and is
871IGNORED."
872 (cond ((mh-goto-header-field field)
873 (mh-header-field-end)
874 t)
875 ((mh-goto-header-end 0)
876 nil)))
877
878(defun mh-letter-header-field-at-point ()
879 "Return the header field name at point.
880A symbol is returned whose name is the string obtained by
881downcasing the field name."
882 (save-excursion
883 (end-of-line)
884 (and (re-search-backward mh-letter-header-field-regexp nil t)
885 (intern (downcase (match-string 1))))))
886
887(defun mh-folder-expand-at-point ()
888 "Do folder name completion in Fcc header field."
d1bb6623
SM
889 (let* ((beg (mh-beginning-of-word))
890 (end (save-excursion
891 (goto-char beg)
892 (mh-beginning-of-word -1))))
893 (when (>= end (point))
894 (list beg (if (fboundp 'completion-at-point) end (point))
895 #'mh-folder-completion-function))))
dda00b2c
BW
896
897;;;###mh-autoload
898(defun mh-complete-word (word choices begin end)
b7149a81 899 "Complete WORD from CHOICES.
dda00b2c
BW
900Any match found replaces the text from BEGIN to END."
901 (let ((completion (try-completion word choices))
902 (completions-buffer "*Completions*"))
903 (cond ((eq completion t)
904 (ignore-errors
905 (kill-buffer completions-buffer))
906 (message "Completed: %s" word))
907 ((null completion)
908 (ignore-errors
909 (kill-buffer completions-buffer))
910 (message "No completion for %s" word))
911 ((stringp completion)
912 (if (equal word completion)
913 (with-output-to-temp-buffer completions-buffer
d1bb6623
SM
914 (mh-display-completion-list
915 (all-completions word choices)
22bcf204 916 ;; The `common-substring' arg only works if it's a prefix.
d1bb6623
SM
917 (unless (and (functionp choices)
918 (let ((bounds
919 (funcall choices
920 word nil '(boundaries . ""))))
921 (and (eq 'boundaries (car-safe bounds))
922 (< 0 (cadr bounds)))))
923 word)))
dda00b2c
BW
924 (ignore-errors
925 (kill-buffer completions-buffer))
926 (delete-region begin end)
927 (insert completion))))))
928
929(defun mh-file-is-vcard-p (file)
930 "Return t if FILE is a .vcf vcard."
931 (let ((case-fold-search t))
932 (and (stringp file)
933 (file-exists-p file)
934 (or (and (not (mh-have-file-command))
935 (not (null (string-match "\.vcf$" file))))
936 (string-equal "text/x-vcard" (mh-file-mime-type file))))))
937
a55f450f 938;;;###mh-autoload
dda00b2c
BW
939(defun mh-letter-toggle-header-field-display-button (event)
940 "Toggle header field display at location of EVENT.
941This function does the same thing as
942`mh-letter-toggle-header-field-display' except that it is
943callable from a mouse button."
944 (interactive "e")
945 (mh-do-at-event-location event
946 (mh-letter-toggle-header-field-display nil)))
947
dda00b2c
BW
948(defun mh-extract-from-attribution ()
949 "Extract phrase or comment from From header field."
950 (save-excursion
951 (if (not (mh-goto-header-field "From: "))
952 nil
953 (skip-chars-forward " ")
954 (cond
955 ((looking-at "\"\\([^\"\n]+\\)\" \\(<.+>\\)")
956 (format "%s %s " (match-string 1)(match-string 2)))
957 ((looking-at "\\([^<\n]+<.+>\\)$")
958 (format "%s " (match-string 1)))
959 ((looking-at "\\([^ ]+@[^ ]+\\) +(\\(.+\\))$")
960 (format "%s <%s> " (match-string 2)(match-string 1)))
961 ((looking-at " *\\(.+\\)$")
962 (format "%s " (match-string 1)))))))
963
964(defun mh-insert-prefix-string (mh-ins-string)
965 "Insert prefix string before each line in buffer.
966The inserted letter is cited using `sc-cite-original' if
967`mh-yank-behavior' is one of 'supercite or 'autosupercite.
968Otherwise, simply insert MH-INS-STRING before each line."
969 (goto-char (point-min))
970 (cond ((or (eq mh-yank-behavior 'supercite)
971 (eq mh-yank-behavior 'autosupercite))
972 (sc-cite-original))
973 (mail-citation-hook
974 (run-hooks 'mail-citation-hook))
975 (mh-yank-hooks ;old hook name
976 (run-hooks 'mh-yank-hooks))
977 (t
978 (or (bolp) (forward-line 1))
979 (while (< (point) (point-max))
980 (insert mh-ins-string)
981 (forward-line 1))
982 (goto-char (point-min))))) ;leave point like sc-cite-original
983
984(provide 'mh-letter)
985
986;; Local Variables:
987;; indent-tabs-mode: nil
988;; sentence-end-double-space: nil
989;; End:
990
991;;; mh-letter.el ends here