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