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