mh-letter.el 38 KB

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