(mh-send-sub): Don't find components file in current directory--this
[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
dda00b2c
BW
64(defvar mh-yank-hooks nil
65 "Obsolete hook for modifying a citation just inserted in the mail buffer.
66
67Each hook function can find the citation between point and mark.
68And each hook function should leave point and mark around the
69citation text as modified.
70
71This is a normal hook, misnamed for historical reasons. It is
72semi-obsolete and is only used if `mail-citation-hook' is nil.")
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
190 "\M-\t" mh-letter-complete
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)
270 (line-beginning-position 0))
271 (t (point-min)))))
272
273\f
274
275;;; MH-Letter Mode
276
277(defvar mh-letter-buttons-init-flag nil)
278
279;; Shush compiler.
280(eval-when-compile (mh-do-in-xemacs (defvar font-lock-defaults)))
281
282;; Ensure new buffers won't get this mode if default-major-mode is nil.
283(put 'mh-letter-mode 'mode-class 'special)
284
285;;;###mh-autoload
286(define-derived-mode mh-letter-mode mail-mode "MH-Letter"
287 "Mode for composing letters in MH-E\\<mh-letter-mode-map>.
288
289When you have finished composing, type \\[mh-send-letter] to send
290the message using the MH mail handling system.
291
292There are two types of tags used by MH-E when composing MIME
293messages: MML and MH. The option `mh-compose-insertion' controls
294what type of tags are inserted by MH-E commands. These tags can
295be converted to MIME body parts by running \\[mh-mh-to-mime] for
296MH-style directives or \\[mh-mml-to-mime] for MML tags.
297
298Options that control this mode can be changed with
299\\[customize-group]; specify the \"mh-compose\" group.
300
301When a message is composed, the hooks `text-mode-hook',
302`mail-mode-hook', and `mh-letter-mode-hook' are run (in that
303order).
304
305\\{mh-letter-mode-map}"
306 (mh-find-path)
307 (make-local-variable 'mh-send-args)
308 (make-local-variable 'mh-annotate-char)
309 (make-local-variable 'mh-annotate-field)
310 (make-local-variable 'mh-previous-window-config)
311 (make-local-variable 'mh-sent-from-folder)
312 (make-local-variable 'mh-sent-from-msg)
313 (mh-do-in-gnu-emacs
314 (unless mh-letter-buttons-init-flag
315 (mh-tool-bar-letter-buttons-init)
316 (setq mh-letter-buttons-init-flag t)))
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))
322 (buffer-substring-no-properties (point) (line-end-position))))
323 (make-local-variable 'mail-header-separator)
324 (setq mail-header-separator mh-mail-header-separator) ;override sendmail.el
325 (mh-set-help mh-letter-mode-help-messages)
326 (setq buffer-invisibility-spec '((vanish . t) t))
327 (set (make-local-variable 'line-move-ignore-invisible) t)
328
329 ;; Enable undo since a show-mode buffer might have been reused.
330 (buffer-enable-undo)
331 (set (make-local-variable 'tool-bar-map) mh-letter-tool-bar-map)
332 (mh-funcall-if-exists mh-tool-bar-init :letter)
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
BW
351 (setq fill-column mh-letter-fill-column)
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
397 (mh-prompt-for-folder "Message from"
398 mh-sent-from-folder nil))
399 (default
400 (if (and (equal folder mh-sent-from-folder)
401 (numberp mh-sent-from-msg))
402 mh-sent-from-msg
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))
408 ": ")))))
409 (list folder message current-prefix-arg)))
410 (save-restriction
411 (narrow-to-region (point) (point))
412 (let ((start (point-min)))
413 (if (and (equal message "") (numberp mh-sent-from-msg))
414 (setq message (int-to-string mh-sent-from-msg)))
415 (insert-file-contents
416 (expand-file-name message (mh-expand-file-name folder)))
417 (when (not verbatim)
418 (mh-clean-msg-header start mh-invisible-header-fields-compiled nil)
419 (goto-char (point-max)) ;Needed for sc-cite-original
420 (push-mark) ;Needed for sc-cite-original
421 (goto-char (point-min)) ;Needed for sc-cite-original
422 (mh-insert-prefix-string mh-ins-buf-prefix)))))
423
424;;;###mh-autoload
425(defun mh-insert-signature (&optional file)
426 "Insert signature in message.
427
428This command inserts your signature at the current cursor location.
429
430By default, the text of your signature is taken from the file
431\"~/.signature\". You can read from other sources by changing the
432option `mh-signature-file-name'.
433
434A signature separator (\"-- \") will be added if the signature block
435does not contain one and `mh-signature-separator-flag' is on.
436
437The hook `mh-insert-signature-hook' is run after the signature is
438inserted. Hook functions may access the actual name of the file or the
439function used to insert the signature with `mh-signature-file-name'.
440
441The signature can also be inserted using Identities (see
442`mh-identity-list').
443
444In a program, you can pass in a signature FILE."
445 (interactive)
446 (save-excursion
447 (insert "\n")
448 (let ((mh-signature-file-name (or file mh-signature-file-name))
449 (mh-mh-p (mh-mh-directive-present-p))
450 (mh-mml-p (mh-mml-tag-present-p)))
451 (save-restriction
452 (narrow-to-region (point) (point))
453 (cond
454 ((mh-file-is-vcard-p mh-signature-file-name)
455 (if (equal mh-compose-insertion 'mml)
456 (insert "<#part type=\"text/x-vcard\" filename=\""
457 mh-signature-file-name
458 "\" disposition=inline description=VCard>\n<#/part>")
459 (insert "#text/x-vcard; name=\""
460 (file-name-nondirectory mh-signature-file-name)
461 "\" [VCard] " (expand-file-name mh-signature-file-name))))
462 (t
463 (cond
464 (mh-mh-p
465 (insert "#\n" "Content-Description: Signature\n"))
466 (mh-mml-p
467 (mml-insert-tag 'part 'type "text/plain" 'disposition "inline"
468 'description "Signature")))
469 (cond ((null mh-signature-file-name))
470 ((and (stringp mh-signature-file-name)
471 (file-readable-p mh-signature-file-name))
472 (insert-file-contents mh-signature-file-name))
473 ((functionp mh-signature-file-name)
474 (funcall mh-signature-file-name)))))
475 (save-restriction
476 (widen)
477 (run-hooks 'mh-insert-signature-hook))
478 (goto-char (point-min))
479 (when (and (not (mh-file-is-vcard-p mh-signature-file-name))
480 mh-signature-separator-flag
481 (> (point-max) (point-min))
482 (not (mh-signature-separator-p)))
483 (cond (mh-mh-p
484 (forward-line 2))
485 (mh-mml-p
486 (forward-line 1)))
487 (insert mh-signature-separator))
488 (if (not (> (point-max) (point-min)))
489 (message "No signature found")))))
490 (force-mode-line-update))
491
492(defun mh-letter-complete (arg)
493 "Perform completion on header field or word preceding point.
494
495If the field contains addresses (for example, \"To:\" or \"Cc:\")
496or folders (for example, \"Fcc:\") then this command will provide
497alias completion. In the body of the message, this command runs
498`mh-letter-complete-function' instead, which is set to
499`ispell-complete-word' by default. This command takes a prefix
500argument ARG that is passed to the
501`mh-letter-complete-function'."
502 (interactive "P")
503 (let ((func nil))
504 (cond ((not (mh-in-header-p))
505 (funcall mh-letter-complete-function arg))
506 ((setq func (cdr (assoc (mh-letter-header-field-at-point)
507 mh-letter-complete-function-alist)))
508 (funcall func))
509 (t (funcall mh-letter-complete-function arg)))))
510
511(defun mh-letter-complete-or-space (arg)
512 "Perform completion or insert space.
513
514Turn on the option `mh-compose-space-does-completion-flag' to use
515this command to perform completion in the header. Otherwise, a
516space is inserted; use a prefix argument ARG to specify more than
517one space."
518 (interactive "p")
519 (let ((func nil)
520 (end-of-prev (save-excursion
521 (goto-char (mh-beginning-of-word))
522 (mh-beginning-of-word -1))))
523 (cond ((not mh-compose-space-does-completion-flag)
524 (self-insert-command arg))
525 ((not (mh-in-header-p)) (self-insert-command arg))
526 ((> (point) end-of-prev) (self-insert-command arg))
527 ((setq func (cdr (assoc (mh-letter-header-field-at-point)
528 mh-letter-complete-function-alist)))
529 (funcall func))
530 (t (self-insert-command arg)))))
531
532(defun mh-letter-confirm-address ()
533 "Flash alias expansion.
534
535Addresses are separated by a comma\; when you press the comma,
536this command flashes the alias expansion in the minibuffer if
537`mh-alias-flash-on-comma' is turned on."
538 (interactive)
539 (cond ((not (mh-in-header-p)) (self-insert-command 1))
540 ((eq (cdr (assoc (mh-letter-header-field-at-point)
541 mh-letter-complete-function-alist))
542 'mh-alias-letter-expand-alias)
543 (mh-alias-reload-maybe)
544 (mh-alias-minibuffer-confirm-address))
545 (t (self-insert-command 1))))
546
547(defun mh-letter-next-header-field-or-indent (arg)
548 "Cycle to next field.
549
550Within the header of the message, this command moves between
551fields that are highlighted with the face
552`mh-letter-header-field', skipping those fields listed in
553`mh-compose-skipped-header-fields'. After the last field, this
554command then moves point to the message body before cycling back
555to the first field. If point is already past the first line of
556the message body, then this command indents by calling
557`indent-relative' with the given prefix argument ARG."
558 (interactive "P")
559 (let ((header-end (save-excursion
560 (goto-char (mh-mail-header-end))
561 (forward-line)
562 (point))))
563 (if (> (point) header-end)
564 (indent-relative arg)
565 (mh-letter-next-header-field))))
566
567(defun mh-letter-previous-header-field ()
568 "Cycle to the previous header field.
569
570This command moves backwards between the fields and cycles to the
571body of the message after the first field. Unlike the command
572\\[mh-letter-next-header-field-or-indent], it will always take
573point to the last field from anywhere in the body."
574 (interactive)
575 (let ((header-end (mh-mail-header-end)))
576 (if (>= (point) header-end)
577 (goto-char header-end)
578 (mh-header-field-beginning))
579 (cond ((re-search-backward mh-letter-header-field-regexp nil t)
580 (if (mh-letter-skipped-header-field-p (match-string 1))
581 (mh-letter-previous-header-field)
582 (goto-char (match-end 0))
583 (mh-letter-skip-leading-whitespace-in-header-field)))
584 (t (goto-char header-end)
585 (forward-line)))))
586
dda00b2c
BW
587(defun mh-open-line ()
588 "Insert a newline and leave point before it.
589
590This command is similar to the command \\[open-line] in that it
591inserts a newline after point. It differs in that it also inserts
592the right number of quoting characters and spaces so that the
593next line begins in the same column as it was. This is useful
594when breaking up paragraphs in replies."
595 (interactive)
596 (let ((column (current-column))
597 (prefix (mh-current-fill-prefix)))
598 (if (> (length prefix) column)
599 (message "Sorry, point seems to be within the line prefix")
600 (newline 2)
601 (insert prefix)
602 (while (> column (current-column))
603 (insert " "))
604 (forward-line -1))))
605
606(defun mh-to-fcc (&optional folder)
607 "Move to \"Fcc:\" header field.
608
609This command will prompt you for the FOLDER name in which to file
610a copy of the draft."
611 (interactive (list (mh-prompt-for-folder
612 "Fcc"
613 (or (and mh-default-folder-for-message-function
614 (save-excursion
615 (goto-char (point-min))
616 (funcall
617 mh-default-folder-for-message-function)))
618 "")
619 t)))
620 (let ((last-input-char ?\C-f))
621 (expand-abbrev)
622 (save-excursion
623 (mh-to-field)
624 (insert (if (mh-folder-name-p folder)
625 (substring folder 1)
626 folder)))))
627
628(defvar mh-to-field-choices '(("a" . "Mail-Reply-To:")
629 ("b" . "Bcc:")
630 ("c" . "Cc:")
631 ("d" . "Dcc:")
632 ("f" . "Fcc:")
633 ("l" . "Mail-Followup-To:")
634 ("m" . "From:")
635 ("r" . "Reply-To:")
636 ("s" . "Subject:")
637 ("t" . "To:"))
638 "Alist of (final-character . field-name) choices for `mh-to-field'.")
639
640(defun mh-to-field ()
641 "Move to specified header field.
642
643The field is indicated by the previous keystroke (the last
644keystroke of the command) according to the list in the variable
645`mh-to-field-choices'.
646Create the field if it does not exist.
647Set the mark to point before moving."
648 (interactive)
649 (expand-abbrev)
650 (let ((target (cdr (or (assoc (char-to-string (logior last-input-char ?`))
651 mh-to-field-choices)
652 ;; also look for a char for version 4 compat
653 (assoc (logior last-input-char ?`)
654 mh-to-field-choices))))
655 (case-fold-search t))
656 (push-mark)
657 (cond ((mh-position-on-field target)
658 (let ((eol (point)))
659 (skip-chars-backward " \t")
660 (delete-region (point) eol))
661 (if (and (not (eq (logior last-input-char ?`) ?s))
662 (save-excursion
663 (backward-char 1)
664 (not (looking-at "[:,]"))))
665 (insert ", ")
666 (insert " ")))
667 (t
668 (if (mh-position-on-field "To:")
669 (forward-line 1))
670 (insert (format "%s \n" target))
671 (backward-char 1)))))
672
673;;;###mh-autoload
674(defun mh-yank-cur-msg ()
675 "Insert the current message into the draft buffer.
676
677It is often useful to insert a snippet of text from a letter that
678someone mailed to provide some context for your reply. This
679command does this by adding an attribution, yanking a portion of
680text from the message to which you're replying, and inserting
681`mh-ins-buf-prefix' (`> ') before each line.
682
683The attribution consists of the sender's name and email address
684followed by the content of the option
685`mh-extract-from-attribution-verb'.
686
687You can also turn on the option
688`mh-delete-yanked-msg-window-flag' to delete the window
689containing the original message after yanking it to make more
690room on your screen for your reply.
691
692You can control how the message to which you are replying is
693yanked into your reply using `mh-yank-behavior'.
694
695If this isn't enough, you can gain full control over the
696appearance of the included text by setting `mail-citation-hook'
697to a function that modifies it. For example, if you set this hook
698to `trivial-cite' (which is NOT part of Emacs), set
699`mh-yank-behavior' to \"Body and Header\" (see URL
700`http://shasta.cs.uiuc.edu/~lrclause/tc.html').
701
702Note that if `mail-citation-hook' is set, `mh-ins-buf-prefix' is
703not inserted. If the option `mh-yank-behavior' is set to one of
704the supercite flavors, the hook `mail-citation-hook' is ignored
705and `mh-ins-buf-prefix' is not inserted."
706 (interactive)
707 (if (and mh-sent-from-folder
708 (save-excursion (set-buffer mh-sent-from-folder) mh-show-buffer)
709 (save-excursion (set-buffer mh-sent-from-folder)
710 (get-buffer mh-show-buffer))
711 mh-sent-from-msg)
712 (let ((to-point (point))
713 (to-buffer (current-buffer)))
714 (set-buffer mh-sent-from-folder)
715 (if mh-delete-yanked-msg-window-flag
716 (delete-windows-on mh-show-buffer))
717 (set-buffer mh-show-buffer) ; Find displayed message
718 (let* ((from-attr (mh-extract-from-attribution))
719 (yank-region (mh-mark-active-p nil))
720 (mh-ins-str
721 (cond ((and yank-region
722 (or (eq 'supercite mh-yank-behavior)
723 (eq 'autosupercite mh-yank-behavior)
724 (eq t mh-yank-behavior)))
725 ;; supercite needs the full header
726 (concat
727 (buffer-substring (point-min) (mh-mail-header-end))
728 "\n"
729 (buffer-substring (region-beginning) (region-end))))
730 (yank-region
731 (buffer-substring (region-beginning) (region-end)))
732 ((or (eq 'body mh-yank-behavior)
733 (eq 'attribution mh-yank-behavior)
734 (eq 'autoattrib mh-yank-behavior))
735 (buffer-substring
736 (save-excursion
737 (goto-char (point-min))
738 (mh-goto-header-end 1)
739 (point))
740 (point-max)))
741 ((or (eq 'supercite mh-yank-behavior)
742 (eq 'autosupercite mh-yank-behavior)
743 (eq t mh-yank-behavior))
744 (buffer-substring (point-min) (point-max)))
745 (t
746 (buffer-substring (point) (point-max))))))
747 (set-buffer to-buffer)
748 (save-restriction
749 (narrow-to-region to-point to-point)
750 (insert (mh-filter-out-non-text mh-ins-str))
751 (goto-char (point-max)) ;Needed for sc-cite-original
752 (push-mark) ;Needed for sc-cite-original
753 (goto-char (point-min)) ;Needed for sc-cite-original
754 (mh-insert-prefix-string mh-ins-buf-prefix)
755 (when (or (eq 'attribution mh-yank-behavior)
756 (eq 'autoattrib mh-yank-behavior))
757 (insert from-attr)
758 (mh-identity-insert-attribution-verb nil)
759 (insert "\n\n"))
760 ;; If the user has selected a region, he has already "edited" the
761 ;; text, so leave the cursor at the end of the yanked text. In
762 ;; either case, leave a mark at the opposite end of the included
763 ;; text to make it easy to jump or delete to the other end of the
764 ;; text.
765 (push-mark)
766 (goto-char (point-max))
767 (if (null yank-region)
768 (mh-exchange-point-and-mark-preserving-active-mark)))))
769 (error "There is no current message")))
770
771\f
772
773;;; Support Routines
774
775(defun mh-auto-fill-for-letter ()
776 "Perform auto-fill for message.
777Header is treated specially by inserting a tab before continuation
778lines."
779 (if (mh-in-header-p)
780 (let ((fill-prefix "\t"))
781 (do-auto-fill))
782 (do-auto-fill)))
783
784(defun mh-filter-out-non-text (string)
785 "Return STRING but without adornments such as MIME buttons and smileys."
786 (with-temp-buffer
787 ;; Insert the string to filter
788 (insert string)
789 (goto-char (point-min))
790
791 ;; Remove the MIME buttons
792 (let ((can-move-forward t)
793 (in-button nil))
794 (while can-move-forward
795 (cond ((and (not (get-text-property (point) 'mh-data))
796 in-button)
797 (delete-region (1- (point)) (point))
798 (setq in-button nil))
799 ((get-text-property (point) 'mh-data)
800 (delete-region (point)
801 (save-excursion (forward-line) (point)))
802 (setq in-button t))
803 (t (setq can-move-forward (= (forward-line) 0))))))
804
805 ;; Return the contents without properties... This gets rid of emphasis
806 ;; and smileys
807 (buffer-substring-no-properties (point-min) (point-max))))
808
809(defun mh-current-fill-prefix ()
810 "Return the `fill-prefix' on the current line as a string."
811 (save-excursion
812 (beginning-of-line)
813 ;; This assumes that the major-mode sets up adaptive-fill-regexp
814 ;; correctly such as mh-letter-mode or sendmail.el's mail-mode. But
815 ;; perhaps I should use the variable and simply inserts its value here,
816 ;; and set it locally in a let scope. --psg
817 (if (re-search-forward adaptive-fill-regexp nil t)
818 (match-string 0)
819 "")))
820
821;;;###mh-autoload
822(defun mh-letter-next-header-field ()
823 "Cycle to the next header field.
824If we are at the last header field go to the start of the message
825body."
826 (let ((header-end (mh-mail-header-end)))
827 (cond ((>= (point) header-end) (goto-char (point-min)))
828 ((< (point) (progn
829 (beginning-of-line)
830 (re-search-forward mh-letter-header-field-regexp
831 (line-end-position) t)
832 (point)))
833 (beginning-of-line))
834 (t (end-of-line)))
835 (cond ((re-search-forward mh-letter-header-field-regexp header-end t)
836 (if (mh-letter-skipped-header-field-p (match-string 1))
837 (mh-letter-next-header-field)
838 (mh-letter-skip-leading-whitespace-in-header-field)))
839 (t (goto-char header-end)
840 (forward-line)))))
841
dda00b2c
BW
842;;;###mh-autoload
843(defun mh-position-on-field (field &optional ignored)
844 "Move to the end of the FIELD in the header.
845Move to end of entire header if FIELD not found.
846Returns non-nil iff FIELD was found.
847The optional second arg is for pre-version 4 compatibility and is
848IGNORED."
849 (cond ((mh-goto-header-field field)
850 (mh-header-field-end)
851 t)
852 ((mh-goto-header-end 0)
853 nil)))
854
855(defun mh-letter-header-field-at-point ()
856 "Return the header field name at point.
857A symbol is returned whose name is the string obtained by
858downcasing the field name."
859 (save-excursion
860 (end-of-line)
861 (and (re-search-backward mh-letter-header-field-regexp nil t)
862 (intern (downcase (match-string 1))))))
863
864(defun mh-folder-expand-at-point ()
865 "Do folder name completion in Fcc header field."
866 (let* ((end (point))
867 (beg (mh-beginning-of-word))
868 (folder (buffer-substring beg end))
869 (leading-plus (and (> (length folder) 0) (equal (aref folder 0) ?+)))
870 (last-slash (mh-search-from-end ?/ folder))
871 (prefix (and last-slash (substring folder 0 last-slash)))
872 (choices (mapcar #'(lambda (x)
873 (list (cond (prefix (format "%s/%s" prefix x))
874 (leading-plus (format "+%s" x))
875 (t x))))
876 (mh-folder-completion-function folder nil t))))
877 (mh-complete-word folder choices beg end)))
878
879;;;###mh-autoload
880(defun mh-complete-word (word choices begin end)
881 "Complete WORD at from CHOICES.
882Any match found replaces the text from BEGIN to END."
883 (let ((completion (try-completion word choices))
884 (completions-buffer "*Completions*"))
885 (cond ((eq completion t)
886 (ignore-errors
887 (kill-buffer completions-buffer))
888 (message "Completed: %s" word))
889 ((null completion)
890 (ignore-errors
891 (kill-buffer completions-buffer))
892 (message "No completion for %s" word))
893 ((stringp completion)
894 (if (equal word completion)
895 (with-output-to-temp-buffer completions-buffer
896 (mh-display-completion-list (all-completions word choices)
08166ee9 897 word))
dda00b2c
BW
898 (ignore-errors
899 (kill-buffer completions-buffer))
900 (delete-region begin end)
901 (insert completion))))))
902
903(defun mh-file-is-vcard-p (file)
904 "Return t if FILE is a .vcf vcard."
905 (let ((case-fold-search t))
906 (and (stringp file)
907 (file-exists-p file)
908 (or (and (not (mh-have-file-command))
909 (not (null (string-match "\.vcf$" file))))
910 (string-equal "text/x-vcard" (mh-file-mime-type file))))))
911
a55f450f 912;;;###mh-autoload
dda00b2c
BW
913(defun mh-letter-toggle-header-field-display-button (event)
914 "Toggle header field display at location of EVENT.
915This function does the same thing as
916`mh-letter-toggle-header-field-display' except that it is
917callable from a mouse button."
918 (interactive "e")
919 (mh-do-at-event-location event
920 (mh-letter-toggle-header-field-display nil)))
921
dda00b2c
BW
922(defun mh-extract-from-attribution ()
923 "Extract phrase or comment from From header field."
924 (save-excursion
925 (if (not (mh-goto-header-field "From: "))
926 nil
927 (skip-chars-forward " ")
928 (cond
929 ((looking-at "\"\\([^\"\n]+\\)\" \\(<.+>\\)")
930 (format "%s %s " (match-string 1)(match-string 2)))
931 ((looking-at "\\([^<\n]+<.+>\\)$")
932 (format "%s " (match-string 1)))
933 ((looking-at "\\([^ ]+@[^ ]+\\) +(\\(.+\\))$")
934 (format "%s <%s> " (match-string 2)(match-string 1)))
935 ((looking-at " *\\(.+\\)$")
936 (format "%s " (match-string 1)))))))
937
938(defun mh-insert-prefix-string (mh-ins-string)
939 "Insert prefix string before each line in buffer.
940The inserted letter is cited using `sc-cite-original' if
941`mh-yank-behavior' is one of 'supercite or 'autosupercite.
942Otherwise, simply insert MH-INS-STRING before each line."
943 (goto-char (point-min))
944 (cond ((or (eq mh-yank-behavior 'supercite)
945 (eq mh-yank-behavior 'autosupercite))
946 (sc-cite-original))
947 (mail-citation-hook
948 (run-hooks 'mail-citation-hook))
949 (mh-yank-hooks ;old hook name
950 (run-hooks 'mh-yank-hooks))
951 (t
952 (or (bolp) (forward-line 1))
953 (while (< (point) (point-max))
954 (insert mh-ins-string)
955 (forward-line 1))
956 (goto-char (point-min))))) ;leave point like sc-cite-original
957
958(provide 'mh-letter)
959
960;; Local Variables:
961;; indent-tabs-mode: nil
962;; sentence-end-double-space: nil
963;; End:
964
a1ab640d 965;; arch-tag: 0548632c-aadb-4e3b-bb80-bbd62ff90bf3
dda00b2c 966;;; mh-letter.el ends here