mutt-post.el 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397
  1. ; $Id: post.el,v 2.4 2004/07/23 23:13:17 rreid Exp rreid $
  2. ;; post.el --- Use (X?)Emacs(client) as an external editor for mail and news.
  3. ;;; Authors: Eric Kidd <eric.kidd@pobox.com>,
  4. ;;; Dave Pearson <davep@davep.org>,
  5. ;;; Rob Reid <reid@astro.utoronto.ca>,
  6. ;;; Roland Rosenfeld <roland@spinnaker.de>
  7. ;; This is free software distributed under the GPL, yadda, yadda, yadda.
  8. ;; It has no warranty. See the GNU General Public License for more
  9. ;; information. Send us your feature requests and patches, and we'll try
  10. ;; to integrate everything.
  11. ;;; Maintainer: Rob Reid <reid@astro.utoronto.ca>
  12. ;;; Keywords: mail
  13. ;;; Commentary:
  14. ;; This is a major mode for use with Mutt, the spiffy *nix mailreader du jour
  15. ;; (See http://www.mutt.org/), slrn, the spiffy *nix newsreader du jour, or
  16. ;; whatever you can get it to work with. To use this mode, add the following
  17. ;; line to the .emacs file in your home directory:
  18. ;;
  19. ;; (load "/your/local/path/to/this/file/post")
  20. ;;
  21. ;; Note that you can omit the ".el" from the file name when calling load.
  22. ;;
  23. ;; If you want to make it available to all your users, type \C-h v
  24. ;; load-path RET, pick an appropriate directory for post.el, and modify
  25. ;; your sitewide default.el to (require 'post).
  26. ;;
  27. ;; You may find the latest version of this mode at
  28. ;; http://astro.utoronto.ca/~reid/mutt/
  29. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  30. ;;
  31. ;; BUGS:
  32. ;;
  33. ;; Rob: I predict that some buffers (*Original*<2>, *Composing*<2>?)
  34. ;; will be left behind if you edit more than one message at a time.
  35. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  36. ;;;
  37. ;;; Thanks
  38. ;;;
  39. ;;; Dave Pearson: Code, feature ideas, Mutt experience. Many thanks!
  40. ;;; Louis Theran: Encouragement to make Mutt mode work like Emacs MUAs.
  41. ;;; Ronald: Enlightening gripes about what Emacs should do, but doesn't.
  42. ;;; Robert Napier: Bug reports about font-lock mode, fancy wrapping.
  43. ;;; Kevin Rodgers: Answered RR's question on gnu.emacs.help on
  44. ;;; overwriting server-process-filter's annoying message at startup.
  45. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  46. ;;;
  47. ;;; Revision History
  48. ;;;
  49. ;;; $Log: post.el,v $
  50. ;;; Revision 2.401 2004/07/23 16:27:29 rreid
  51. ;;; Fixed post-delete-quoted-signatures to not remove sneaky things like quoted
  52. ;;; double dash arrows. Thanks go to Felix Klee for a clear bug report.
  53. ;;;
  54. ;;; Revision 2.4 2002/04/22 22:04:29 reid
  55. ;;; Tweaked post-emoticon-pattern yet again. Made cl mandatory for all
  56. ;;; versions of emacs. (Thanks to Eric Dorland and Mike Schiraldi for bug
  57. ;;; reports.) Fixed post-unquote-region. (Thanks to Mike Schiraldi for the
  58. ;;; bug report.)
  59. ;;;
  60. ;;; Revision 2.3 2002/04/21 20:13:55 reid
  61. ;;; Improved post-emoticon-pattern.
  62. ;;;
  63. ;;; Revision 2.2 2002/04/20 04:12:54 reid
  64. ;;; Improved post-emoticon-pattern.
  65. ;;;
  66. ;;; Revision 2.1 2002/04/20 03:17:48 reid
  67. ;;; - A major (but not total) synchronization with Dave Pearson's post-mode.
  68. ;;; header-set-followup-to and header-set-organization should work now.
  69. ;;; - Syntax highlighting now works for quoted email addresses and URLs.
  70. ;;; - *bold* words are now highlighted.
  71. ;;; - Emoticons can now be highlighted, and the default regexp,
  72. ;;; post-emoticon-pattern, might be too enthusiastic for your taste. In case
  73. ;;; you're curious, I verified that gnus' smiley-ems.el works with post, but I
  74. ;;; decided that it wasn't ideal.
  75. ;;; - post-url-text-pattern changed to post-url-pattern and made more enthusiastic.
  76. ;;;
  77. ;;; revision 1.95 2002/04/10 00:06:26 reid
  78. ;;; Fixed the regexp in post-kill-signature to not delete everything between
  79. ;;; mutt's standard forwarding lines. post-kill-signature is called indirectly
  80. ;;; by many functions.
  81. ;;;
  82. ;;; Revision 1.9 2002/04/04 22:24:31 reid
  83. ;;; Applied a patch (not quite verbatim) from The Anarcat
  84. ;;; <anarcat@anarcat.dyndns.org> to make the entity separating siglets in
  85. ;;; `post-variable-signature-source' a regexp, `post-signature-sep-regexp'. The
  86. ;;; default works either either the old post file format or strfiled (fortune)
  87. ;;; files.
  88. ;;;;
  89. ;;; Changed default `post-random-signature-command' to `fortune
  90. ;;; ~/.mutt/sigs.fortune'.
  91. ;;;
  92. ;;; `post-random-signature-command' should now NOT supply a fixed sig portion!
  93. ;;;
  94. ;;; (post-el-random-signature) supplied by The Anarcat to do random sig
  95. ;;; selection purely within Emacs Lisp.
  96. ;;;
  97. ;;; Revision 1.8 2002/02/06 22:24:31 eric
  98. ;;; clean up
  99. ;;;
  100. ;;; Revision 1.7.2 2002/02/06 22:17:01 eric
  101. ;;; tweak regexps, make font-lock-comment-face be post-signature-text-face
  102. ;;;
  103. ;;; Revision 1.7.1 2002/02/06 21:58:58 eric
  104. ;;; tweak regexp, change some types to regexp
  105. ;;;
  106. ;;; Revision 1.7.0 2002/02/06 21:36:56 eric
  107. ;;; hilight signatures, urls and emails
  108. ;;;
  109. ;;; Revision 1.6.3.10 1999/10/11 00:29:41 roland
  110. ;;; Corrected color quoting again: Now allows ">" in the middle of
  111. ;;; a line which is quoted twice.
  112. ;;;
  113. ;;; Revision 1.6.3.9 1999/10/08 10:43:18 roland
  114. ;;; Add third level of quoting faces.
  115. ;;; Allow super-cite name prefixes before quote signs.
  116. ;;;
  117. ;;; Revision 1.6.3.8 1999/10/08 08:39:00 roland
  118. ;;; post-font-lock-keywords now detects lines with only "> "in it
  119. ;;; correctly (merged following line into it before).
  120. ;;;
  121. ;;; Revision 1.6.3.7 1999/10/04 10:07:48 roland
  122. ;;; Add post-quote-region and post-unquote-region commands to quote and
  123. ;;; unquote a region (one level).
  124. ;;;
  125. ;;; Revision 1.6.3.6 1999/09/03 23:13:55 reid
  126. ;;; Valeriy E. Ushakov <uwe@ptc.spbu.ru> pointed out that (GNU) Emacs <20 has
  127. ;;; fewer (optional) arguments to (read-string) than what I was using to
  128. ;;; inherit the input method. I didn't find a way off the top of my head
  129. ;;; to redefine (read-string) without causing an infinite loop, so I have
  130. ;;; substituted a macro (string-read prompt) which does the right thing,
  131. ;;; so please use it instead of read-string.
  132. ;;;
  133. ;;; Revision 1.6.3.5 1999/08/29 19:58:49 reid
  134. ;;; Changed default post-mail-message to handle hostnames with digits.
  135. ;;; Thanks to Brian D. Winters <brianw@alumni.caltech.edu>.
  136. ;;;
  137. ;;; Revision 1.6.3.4 1999/03/20 03:02:05 reid
  138. ;;; Made post compatible with emacs as far back as 19.28.1, probably
  139. ;;; farther.
  140. ;;;
  141. ;;; Revision 1.6.3.3 1999/03/16 03:14:07 reid
  142. ;;; Cleaned up post-select-signature-select-sig-from-file code.
  143. ;;;
  144. ;;; Revision 1.6.3.2 1999/03/16 03:05:12 reid
  145. ;;; Fixed alist updating.
  146. ;;;
  147. ;;; Revision 1.6.3.1 1999/03/13 02:23:48 reid
  148. ;;; Added defface to the list of things that get defined if customize
  149. ;;; hasn't already done it. Thanks to Melissa Binde for the bug report.
  150. ;;;
  151. ;;; Modified post-body-says-attach to use a regexp,
  152. ;;; post-attachment-regexp, so that something like "\(attach\|anbringen\)"
  153. ;;; can be used by bilingual people like Roland.
  154. ;;;
  155. ;;; Revision 1.6.2.1 1999/03/12 10:16:11 roland
  156. ;;; Added missing () to post-insert-to-auto-mode-alist-on-load.
  157. ;;;
  158. ;;; Revision 1.6.2 1999/03/11 15:51 Dave Pearson
  159. ;;; header-position-on-value fixed to return (point), and
  160. ;;; defcustom macro provided for Emacs 19 users.
  161. ;;;
  162. ;;; Revision 1.6.1.2 1999/03/06 11:24:43 roland
  163. ;;; Added post-insert-to-auto-mode-alist-on-load.
  164. ;;;
  165. ;;; Revision 1.6.1.1 1999/03/06 11:02:27 roland
  166. ;;; Customized renaming of buffer.
  167. ;;; Removed different handling for mail, news, news-reply.
  168. ;;; Fixed problems with easy-menu under XEmacs.
  169. ;;;
  170. ;;; Revision 1.6.0 1999/03/04 18:04 Rob Reid
  171. ;;; Returned post-signature-pattern to using "--" instead of "-- "
  172. ;;; because some senders have broken MTAs (as Eric reminded me) and
  173. ;;; some users don't use procmail to compensate. This time all of the
  174. ;;; functions dealing with signatures have been smartened up to avoid
  175. ;;; false matches. Unfortunately that means they don't use
  176. ;;; post-signature-pattern in its raw form.
  177. ;;;
  178. ;;; Added post-backup-original so that Dave's post-copy-original can
  179. ;;; be used.
  180. ;;;
  181. ;;; Kevin Rodgers explained how to put this in .emacs to fix the
  182. ;;; server-process-filter's annoying message problem:
  183. ;;;
  184. ;;; Revision 1.1 1999/03/04 18:02:30 reid
  185. ;;; Initial revision
  186. ;;;
  187. ;;; %%%%%%%%%%%% Put in .emacs %%%%%%%%%%%
  188. ;;;
  189. ;;; ;;; Email
  190. ;;; (server-start)
  191. ;;; (load "/home/reid/.mutt/post")
  192. ;;; (defadvice server-process-filter (after post-mode-message first activate)
  193. ;;; "If the buffer is in post mode, overwrite the server-edit
  194. ;;; message with a post-save-current-buffer-and-exit message."
  195. ;;; (if (eq major-mode 'post-mode)
  196. ;;; (message
  197. ;;; (substitute-command-keys "Type \\[describe-mode] for help composing; \\[post-save-current-buffer-and-exit] when done."))))
  198. ;;; ; This is also needed to see the magic message. Set to a higher
  199. ;;; ; number if you have a faster computer or read slower than me.
  200. ;;; '(font-lock-verbose 1000)
  201. ;;; ; (setq server-temp-file-regexp "mutt-")
  202. ;;; (add-hook 'server-switch-hook
  203. ;;; (function (lambda()
  204. ;;; (cond ((string-match "Post" mode-name)
  205. ;;; (post-goto-body))))))
  206. ;;;
  207. ;;; %%%%%%%%% We now return to our regular commentary %%%%%%%%%
  208. ;;;
  209. ;;; Eric Kidd asked that the name of Headers mode be changed so that
  210. ;;; it doesn't conflict with mutt-mode's Headers, so I changed it to
  211. ;;; just Header (no s).
  212. ;;;
  213. ;;; Revision 1.5? 1999/02/27 17:30 Rob Reid
  214. ;;; I had a go at combining Dave Pearson's post mode with Eric Kidd's
  215. ;;; Mutt mode. Since Dave Pearson's post mode explicitly handles news as
  216. ;;; well as email, and this should be useful for more than just mutt,
  217. ;;; I'm calling it post mode. I also added functions for picking
  218. ;;; random signatures, selecting a signature from a file, and
  219. ;;; intelligently (IMHO) prompting the user for an attachment when
  220. ;;; necessary. Changed mutt-save-buffer-and-exit to work better with
  221. ;;; emacsclient, and some of the key bindings. post-signature-pattern
  222. ;;; now defaults to use "-- " instead of "--", and I have far less
  223. ;;; trouble this way (I use procmail to clean up braindead "--"s.). I
  224. ;;; don't know why Eric warned against trailing whitespace.
  225. ;;;
  226. ;;; Revision 1.4 1998/04/11 00:05:46 emk
  227. ;;; Fixed font-lock bug. Also made mutt-mode a little more careful about
  228. ;;; saving various bits of Emacs state when moving around the buffer.
  229. ;;;
  230. ;;; Revision 1.3 1998/03/25 00:37:36 emk
  231. ;;; Added support for menus and font-lock mode, plus a few bug fixes.
  232. ;;;
  233. ;;; Revision 1.2 1998/03/24 13:19:46 emk
  234. ;;; Major overhaul--more commands, a minor mode for header editing, and other
  235. ;;; desirable features. Attaching files seems to be broken, though.
  236. ;;;
  237. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  238. ;;;
  239. ;;; Required Packages
  240. (require 'cl)
  241. (require 'derived)
  242. (require 'easymenu)
  243. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  244. ;;;
  245. ;;; Customization Support
  246. ;;;
  247. ;;; Set up our customizable features. You can edit these (and lots of other
  248. ;;; fun stuff) by typing M-x customize RET. The Post preferences can be
  249. ;;; found under the [Applications] [Mail] category.
  250. ;; Make post mode a bit more compatible with older (i.e. <20) versions of emacs.
  251. ;;; Code:
  252. (eval-and-compile
  253. ;; Dumb down read-string if necessary.
  254. ;; The number of optional arguments for read-string seems to increase
  255. ;; sharply with (emacs-version). Since old versions of emacs are a large
  256. ;; source of bug reports it might be worth writing (or looking for)
  257. ;; (bug-report reid@astro.utoronto.ca) which emails me the result of
  258. ;; (emacs-version) along with a user supplied description of the problem.
  259. ;; GNU Emacs 19.28.1 only has INITIAL-STRING as an optional argument.
  260. ;; 19.34.1 has (read-string PROMPT &optional INITIAL-INPUT HISTORY). 20.2.1
  261. ;; has (read-string PROMPT &optional INITIAL-INPUT HISTORY DEFAULT-VALUE
  262. ;; INHERIT-INPUT-METHOD).
  263. ;; Since I haven't found a way of redefining read-string without causing an
  264. ;; infinite loop, please use (string-read prompt).
  265. (if (< (string-to-number (substring (emacs-version)
  266. (string-match "[0-9]+\.[0-9]"
  267. (emacs-version) 5))) 20)
  268. (defmacro string-read (prompt) (` (read-string (, prompt))))
  269. (defmacro string-read (prompt)
  270. (` (read-string (, prompt) nil nil nil t))))
  271. ;; XEmacs gnuserv uses slightly different functions than the GNU Emacs
  272. ;; server, and some people are still wasting time and CPU cycles by starting
  273. ;; up a new emacs each time.
  274. (cond ((fboundp 'server-edit)
  275. (fset 'post-finish 'server-edit))
  276. ((fboundp 'gnuserv-kill-buffer-function)
  277. (fset 'post-finish 'gnuserv-kill-buffer-function))
  278. (t
  279. (fset 'post-finish 'save-buffers-kill-emacs)))
  280. ;; If customize isn't available just use defvar instead.
  281. (unless (fboundp 'defgroup)
  282. (defmacro defgroup (&rest rest) nil)
  283. (defmacro defcustom (symbol init docstring &rest rest)
  284. ; The "extra" braces and whitespace are for emacs < 19.29.
  285. (` (defvar (, symbol) (, init) (, docstring))))
  286. (defmacro defface (&rest args) nil))
  287. (unless (fboundp 'buffer-substring-no-properties)
  288. (fset 'buffer-substring-no-properties 'buffer-substring)))
  289. (defgroup post nil
  290. "Composing e-mail messages with Post.
  291. Emacs can run as an external editor for Mutt, the spiffy Unix mail reader
  292. du jour, or slrn, the spiffy Unix news reader du jour. You can get
  293. Mutt from http://www.mutt.org/."
  294. :group 'mail)
  295. (defcustom post-uses-fill-mode t
  296. "*Specifies whether Post should automatically wrap lines.
  297. Set this to t to enable line wrapping, and nil to disable line
  298. wrapping. Note that if a paragraph gets messed up (the line wrapper
  299. is very primitive), you can type \\[fill-paragraph] to rewrap the paragraph."
  300. :type 'boolean
  301. :group 'post)
  302. (defcustom post-mail-message "mutt-[a-z0-9]+-[0-9]+-[0-9]+\\'"
  303. "*Regular expression which matches your mailer's temporary files."
  304. :type 'string
  305. :group 'post)
  306. (defcustom post-news-posting "\\.\\(followup\\|letter\\|article\\)$"
  307. "*Regular expression which matches your news reader's composition files."
  308. :type 'string
  309. :group 'post)
  310. (defcustom post-backup-original nil
  311. "*Controls whether a pristine backup of the original is kept for reference."
  312. :type 'boolean
  313. :group 'post)
  314. (defcustom post-signature-pattern "\\(--\\|Cheers,\\| \\)"
  315. "*Pattern signifying the beginning of signatures.
  316. It should not contain trailing whitespace unless you know what you're doing."
  317. :type 'regexp
  318. :group 'post)
  319. (defcustom post-signature-sep-regexp "^\\(%\\|^L\\|--\\)?\n"
  320. "*Regular expression delimiting signatures in the signature file.
  321. This allows the use of classic fortune files as signature files.
  322. This should normally contain a newline."
  323. :type 'regexp
  324. :group 'post)
  325. (defcustom post-signature-source-is-file t
  326. "*Toggles the signature source type between file and directory."
  327. :type 'boolean
  328. :group 'post)
  329. (defcustom post-variable-signature-source "~/.mutt/sigs.fortune"
  330. "*Location of the variable part of your signature.
  331. Post uses this to locate signatures. It can be either a directory
  332. with one item per file or a file with items separated by blank lines."
  333. :type 'string
  334. :group 'post)
  335. (defcustom post-fixed-signature-source "~/.fixedsig"
  336. "*File with the fixed part of your signature."
  337. :type 'string
  338. :group 'post)
  339. (defcustom post-signature-directory "~/.sigs/"
  340. "*The directory that contains your collection of signature files."
  341. :type 'string
  342. :group 'post)
  343. (defcustom post-signature-wildcard "sig*"
  344. "*Wildcard for finding signature files in your signature directory."
  345. :type 'string
  346. :group 'post)
  347. (defcustom post-random-signature-command "fortune ~/.mutt/sigs.fortune"
  348. "*Command to run to get a random signature.
  349. Examples are available at http://astro.utoronto.ca/~reid/mutt/"
  350. :type 'string
  351. :group 'post)
  352. (defcustom post-kill-quoted-sig t
  353. "Specifies whether `post-mode' should automatically kill quoted signatures."
  354. :type 'boolean
  355. :group 'post)
  356. (defcustom post-jump-header t
  357. "Specifies wheather `post-mode' should jump to the body."
  358. :type 'boolean
  359. :group 'post)
  360. (defcustom post-force-pwd-to-home t
  361. "Specifies whether `post-mode' should cd to your home directory."
  362. :type 'boolean
  363. :group 'post)
  364. (defcustom post-email-address (concat (user-login-name) "@" mail-host-address)
  365. "*Your email address."
  366. :type 'string
  367. :group 'post)
  368. (defcustom post-should-prompt-for-attachment 'Smart
  369. "*Controls whether an attachment will be prompted for before saving
  370. the message and exiting. 'Smart' will prompt only if the body
  371. contains post-attachment-regexp."
  372. :type '(choice (const Never)
  373. (const Smart)
  374. (const Always))
  375. :group 'post)
  376. (defcustom post-attachment-regexp "attach"
  377. "*This is what post looks for in the body if
  378. post-should-prompt-for-attachment is 'Smart'."
  379. :type 'regexp
  380. :group 'post)
  381. (defcustom post-news-poster-regexp "^On .*<.*>.*wrote:$"
  382. "Regular expression used to locate the attribution line of a news posting."
  383. :type 'regexp
  384. :group 'post)
  385. (defcustom post-rename-buffer t
  386. "Specify whether `post-mode' should rename the buffer to *Composing*."
  387. :type 'boolean
  388. :group 'post)
  389. (defcustom post-insert-to-auto-mode-alist-on-load t
  390. "Automatically insert `post-mode' with `post-mail-message' to `auto-mode-alist'."
  391. :type 'boolean
  392. :group 'post)
  393. (defcustom post-mode-hook nil
  394. "List of hooks to be executed on entry to `post-mode'."
  395. :group 'post)
  396. (defcustom post-quote-start "> "
  397. "Pattern which is added (or removed) at the beginning of the line by
  398. comment-region"
  399. :group 'post)
  400. (defcustom post-email-address-pattern
  401. "[A-Za-z0-9_][-A-Za-z0-9._]*@[-A-Za-z0-9._]*[A-Za-z0-9]"
  402. "Pattern to detect email addresses."
  403. :type 'regexp
  404. :group 'post)
  405. (defcustom post-url-pattern
  406. '("\\<\\(\\(https?\\|news\\|mailto\\|ftp\\|gopher\\):\\|\\(www\\|ftp\\)\\.\\)[-~A-Za-z0-9._/%$+?#]+[A-Za-z0-9/#]" "<URL:[^ ]+>")
  407. "Pattern to detect URL addresses."
  408. :type '(repeat regexp)
  409. :group 'post)
  410. (defcustom post-bold-pattern '("\\*\\w+\\*")
  411. "*List of regular expressions that define bold text."
  412. :type '(repeat regexp)
  413. :group 'post)
  414. (defcustom post-underline-pattern '("_\\w+_")
  415. "*List of regular expressions that define underlined text."
  416. :type '(repeat regexp)
  417. :group 'post)
  418. (defcustom post-emoticon-pattern '("[0O(<{}]?[;:8B|][.,]?[-+^*o0O][{<>/\|]?[][)>(<|/\P][)>]?"
  419. "\\s [(<]?[][)>(<|/\][}<>|]?[-+^*oO0][,.]?[:8][0O>]?"
  420. "\\s [;:][][P)\/(]" "\\s [][)(P\/][:;]"
  421. "<[Gg]>" "<[BbSs][Gg]>")
  422. "*List of regular expressions that define a emoticon."
  423. :type '(repeat regexp)
  424. :group 'post)
  425. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  426. ;;;
  427. ;;; Customizable Faces
  428. ;;; If you find a more attractive color scheme for dark backgrounds, please
  429. ;;; email it to reid@astro.utoronto.
  430. (defgroup post-faces nil
  431. "Typefaces used for composing messages with Post."
  432. :group 'post
  433. :group 'faces)
  434. (defface post-header-keyword-face
  435. '((((class color)
  436. (background light))
  437. (:foreground "Navy" :bold t))
  438. (((class color)
  439. (background dark))
  440. (:foreground "LightBlue" :bold t))
  441. (t
  442. (:bold t)))
  443. "Face used for displaying keywords (e.g. \"From:\") in header."
  444. :group 'post-faces)
  445. (defface post-header-value-face
  446. '((((class color)
  447. (background light))
  448. (:foreground "MidnightBlue"))
  449. (((class color)
  450. (background dark))
  451. (:foreground "LightSteelBlue")))
  452. "Face used for displaying the values of header."
  453. :group 'post-faces)
  454. (defface post-quoted-text-face
  455. '((((class color)
  456. (background light))
  457. (:foreground "Sienna" :italic t))
  458. (((class color)
  459. (background dark))
  460. (:foreground "Wheat" :italic t))
  461. (t
  462. (:bold t :italic t)))
  463. "Face used for displaying text which has been quoted (e.g. \">foo\")."
  464. :group 'post-faces)
  465. (defface post-double-quoted-text-face
  466. '((((class color)
  467. (background light))
  468. (:foreground "Firebrick" :italic t))
  469. (((class color)
  470. (background dark))
  471. (:foreground "Tan" :italic t))
  472. (t
  473. (:italic t)))
  474. "Face used for text which has been quoted twice (e.g. \">>foo\")."
  475. :group 'post-faces)
  476. (defface post-multiply-quoted-text-face
  477. '((((class color)
  478. (background light))
  479. (:foreground "goldenrod" :italic t))
  480. (((class color)
  481. (background dark))
  482. (:foreground "tan3" :italic t))
  483. (t
  484. (:italic t)))
  485. "Face used for text which has been quoted more than twice (e.g. \">>>foo\")."
  486. :group 'post-faces)
  487. (defface post-signature-text-face
  488. '((((class color)
  489. (background light))
  490. (:foreground "red3"))
  491. (((class color)
  492. (background dark))
  493. (:foreground "red1"))
  494. (t
  495. (:bold t)))
  496. "Face used for text that is part of a signature"
  497. :group 'post-faces)
  498. (defface post-email-address-text-face
  499. '((((class color)
  500. (background light))
  501. (:foreground "green3"))
  502. (((class color)
  503. (background dark))
  504. (:foreground "green1"))
  505. (t
  506. (:italic t)))
  507. "Face used for email addresses"
  508. :group 'post-faces)
  509. (defface post-url-face
  510. '((((class color)
  511. (background light))
  512. (:foreground "green3" :bold t))
  513. (((class color)
  514. (background dark))
  515. (:foreground "green1" :bold t))
  516. (t
  517. (:italic t)))
  518. "Face used for URL addresses"
  519. :group 'post-faces)
  520. (defface post-emoticon-face
  521. '((((class color)
  522. (background light))
  523. (:foreground "black" :background "yellow" :bold t))
  524. (((class color)
  525. (background dark))
  526. (:foreground "black" :background "yellow" :bold t))
  527. (t
  528. (:bold t)))
  529. "Face used for text matched by post-emoticon-pattern."
  530. :group 'post-faces)
  531. (defface post-bold-face
  532. '((((class color)
  533. (background light))
  534. (:bold t))
  535. (((class color)
  536. (background dark))
  537. (:bold t))
  538. (t
  539. (:bold t)))
  540. "Face used for text matching post-bold-pattern."
  541. :group 'post-faces)
  542. (defface post-underline-face
  543. '((((class color)
  544. (background light))
  545. (:underline t))
  546. (((class color)
  547. (background dark))
  548. (:underline t))
  549. (t
  550. (:underline t)))
  551. "Face used for text matching post-underline-pattern."
  552. :group 'post-faces)
  553. ; Note: some faces are added later!
  554. (defvar post-font-lock-keywords
  555. `(("^\\([A-Z][-A-Za-z0-9.]+:\\)\\(.*\\)$"
  556. (1 'post-header-keyword-face)
  557. (2 'post-header-value-face))
  558. ("^[ \t\f]*\\(>[ \t\f]*\\)\\([-a-zA-Z]*>[ \t\f]*\\)\\([-a-zA-Z]*>.*\\)$"
  559. (1 'post-quoted-text-face)
  560. (2 'post-double-quoted-text-face)
  561. (3 'post-multiply-quoted-text-face))
  562. ("^[ \t\f]*\\(>[ \t\f]*\\)\\([-a-zA-Z]*>.*\\)$"
  563. (1 'post-quoted-text-face)
  564. (2 'post-double-quoted-text-face))
  565. ("^[ \t\f]*\\(>[ \t\f]*[^ \t\f\n>].*\\)$"
  566. (1 'post-quoted-text-face))
  567. ("^[ \t\f]*\\(>[ \t\f]*\\)$"
  568. (1 'post-quoted-text-face))
  569. (,post-email-address-pattern
  570. (0 'post-email-address-text-face)))
  571. "Highlighting rules for message mode.")
  572. ;;; Declare global mode variables.
  573. (defconst post-font-lock-syntactic-keywords
  574. `((,(concat "^" post-signature-pattern "[ \t\f]*$") 0 '(11))))
  575. (defun post-font-lock-syntactic-face-function (state)
  576. "Function for font locking syntactic faces.
  577. Argument STATE ."
  578. post-signature-text-face)
  579. (defvar post-buf nil
  580. "Name of the composing buffer.")
  581. (defvar post-select-signature-mode-map nil
  582. "Local keymap for the select-signature buffer.")
  583. (defvar post-select-signature-last-buffer nil
  584. "Pointer to the calling buffer.")
  585. (defvar post-select-signature-last-point nil
  586. "Where we were in the calling buffer.")
  587. (defvar post-has-attachment nil
  588. "Whether the message has an attachment.")
  589. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  590. ;;;
  591. ;;; Interactive Commands
  592. (defun post-save-current-buffer-and-exit ()
  593. "Save the current buffer and exit Emacs."
  594. (interactive)
  595. ;; Should the user be prompted for an attachment?
  596. (cond (post-has-attachment)
  597. ((equal post-should-prompt-for-attachment 'Never))
  598. ((or (equal post-should-prompt-for-attachment 'Always)
  599. (post-body-says-attach))
  600. (post-prompt-for-attachment)))
  601. (basic-save-buffer)
  602. (if post-backup-original
  603. (kill-buffer "*Original*"))
  604. (post-finish)
  605. ;; Added by Rob Reid 10/13/1998 to prevent accumulating *Composing* buffers
  606. ;; when using (emacs|gnu)client. Helped by Eric Marsden's Eliza example in
  607. ;; http://www.ssc.com/lg/issue29/marsden.html
  608. (kill-buffer post-buf))
  609. (defun post-goto-body ()
  610. "Go to the beginning of the message body."
  611. (interactive)
  612. (goto-char (point-min))
  613. ;; If the message has header, slide downward.
  614. (and header-mode (save-match-data (re-search-forward "^$" nil t))
  615. (next-line 1)))
  616. (defun post-goto-signature ()
  617. "Go to the beginning of the message signature."
  618. (interactive)
  619. (goto-char (point-max))
  620. (and (save-match-data
  621. (re-search-backward (concat "^" post-signature-pattern
  622. "[ \t\f]*$")
  623. nil t))))
  624. (defun post-delete-quoted-signatures ()
  625. "Delete quoted signatures from buffer."
  626. (interactive)
  627. (goto-char (point-min))
  628. (flush-lines (concat "^\\([ \t\f]*>[ \t\f>]*\\)"
  629. post-signature-pattern
  630. "[ \t\f]*\\(\n\\1.*\\)+")))
  631. (defun post-kill-signature ()
  632. "Kill the signature from the buffer.
  633. Returns the point value for where the signature was or, if there isn't a
  634. signature, the point value of the end of the buffer"
  635. (interactive)
  636. (save-excursion
  637. (goto-char (point-min))
  638. ; The .=*+|#@!~$%&()_- is to compensate for people who put ASCII art on the
  639. ; same line as the sigdashes, and the $ at the end prevents this from deleting
  640. ; everything between mutt's standard forwarding lines.
  641. (cond ((search-forward-regexp (concat "^" post-signature-pattern
  642. "[ \t\f.=*+|#@!~$%&()_-]*$") nil t)
  643. (beginning-of-line)
  644. (kill-region (point) (point-max)))
  645. (t
  646. (goto-char (point-max))))
  647. (point)))
  648. (defun post-delete-old-citations ()
  649. "Delete citations more than one level deep from buffer."
  650. (interactive)
  651. (goto-char (point-min))
  652. (flush-lines "^[ \t\f]*>[ \t\f]*>[ \t\f>]*"))
  653. ;;; Functions for messing with the body
  654. (defun post-make-region-bold (start end)
  655. "Apply mutt's nroff style bold to a region of text.
  656. Argument START start of region.
  657. Argument END end of region."
  658. (interactive "r")
  659. (while (< start end)
  660. (goto-char start)
  661. (insert (buffer-substring-no-properties start (1+ start)))
  662. (insert (char-to-string 8))
  663. (setq start (+ start 3))
  664. (setq end (+ end 2))))
  665. (defun post-make-region-underlined (start end)
  666. "Apply mutt's nroff style underline to a region of text.
  667. Argument START start of region.
  668. Argument END end of region."
  669. (interactive "r")
  670. (while (< start end)
  671. (goto-char start)
  672. (insert "_")
  673. (insert (char-to-string 8))
  674. (setq start (+ start 3))
  675. (setq end (+ end 2))))
  676. (defun post-quote-region (beg end)
  677. "Quote a region using the `post-quote-start' variable.
  678. Argument BEG Beginning of region to be quoted.
  679. Argument END End of region to be quoted."
  680. (interactive "r")
  681. (comment-region beg end))
  682. (defun post-unquote-region (beg end)
  683. "Un-quote a region one level using the `post-quote-start' variable.
  684. Argument BEG Beginning of region to be quoted.
  685. Argument END End of region to be quoted."
  686. (interactive "r")
  687. (uncomment-region beg end))
  688. ; From Dave Pearson, July 15, 2000
  689. (defun* split-quoted-paragraph (&optional (quote-string "> "))
  690. "Split a quoted paragraph at point, keeping the quote."
  691. (interactive)
  692. (if (save-excursion
  693. (beginning-of-line)
  694. (looking-at (regexp-quote quote-string)))
  695. (progn
  696. (let ((spaces (- (point)
  697. (save-excursion
  698. (beginning-of-line)
  699. (point))
  700. (length quote-string))))
  701. (save-excursion
  702. (insert (format "\n\n%s%s" quote-string (make-string spaces ? ))))))
  703. (error "Can't see a quoted paragraph here")))
  704. (defun post-random-signature ()
  705. "Randomize the signature.
  706. Set it to whatever `post-random-signature-command' spits out followed by the
  707. content of `post-fixed-signature-source', if available, or a nasty reminder if
  708. it is not."
  709. (interactive)
  710. (save-excursion
  711. (goto-char (post-kill-signature))
  712. (insert "-- \n")
  713. (shell-command post-random-signature-command t)
  714. (goto-char (point-max))
  715. (if (file-readable-p post-fixed-signature-source)
  716. (insert-file-contents post-fixed-signature-source)
  717. (insert "I really need a `post-fixed-signature-source'!\n"))))
  718. (defun post-el-random-signature ()
  719. "Choose a random signature from `post-variable-signature-source'.
  720. the signatures in `post-variable-signature-source' must be separated by
  721. `post-signature-sep-regexp'."
  722. (interactive)
  723. (let ((sig nil))
  724. (save-excursion
  725. (set-buffer (generate-new-buffer "*Post-Select-Signature*"))
  726. (insert-file post-variable-signature-source)
  727. (beginning-of-buffer)
  728. ;; we have 2 lists of marks since seperators are of arbitrary lenght
  729. (let ((marks-st (list (point-min)))
  730. (marks-end (list))
  731. (count 0)) ;nth counts from zero and random is [0,N)
  732. (while (search-forward-regexp post-signature-sep-regexp nil "a")
  733. (setq marks-st (cons (match-end 0) marks-st)
  734. marks-end (cons (match-beginning 0) marks-end)
  735. count (1+ count)))
  736. (setq marks-end (cons (point-max) marks-end))
  737. (let ((r (random (1+ count))))
  738. (setq sig (buffer-substring-no-properties
  739. (nth r marks-st) (nth r marks-end))))
  740. (kill-buffer (current-buffer)))
  741. (goto-char (post-kill-signature))
  742. (insert-string "-- \n")
  743. (insert sig)
  744. (if (file-readable-p post-fixed-signature-source)
  745. (insert-file-contents post-fixed-signature-source)
  746. (insert "I really need a `post-fixed-signature-source'!\n")))))
  747. (defun post-select-signature-from-file ()
  748. "*Interactively select a signature from `post-variable-signature-source'."
  749. (interactive)
  750. (setq post-select-signature-last-buffer (current-buffer))
  751. (setq post-select-signature-last-point (point))
  752. (pop-to-buffer "*Post-Select-Signature*")
  753. (insert-file post-variable-signature-source)
  754. (use-local-map post-select-signature-mode-map))
  755. (defun post-select-signature-select-sig-from-file ()
  756. "*Chooses the signature the cursor is in from `post-variable-signature-source'."
  757. (interactive)
  758. ;; These 2 lines select whatever siglet the cursor is sitting in,
  759. ;; making it nifty to C-s "word" then C-m (or whatever this is
  760. ;; bound to).
  761. (let ((sig-start (point))
  762. (sig-end (point)))
  763. (cond ((setq sig-start (search-backward-regexp post-signature-sep-regexp
  764. nil "a"))
  765. (forward-line 1)
  766. (setq sig-start (point))))
  767. (if (search-forward-regexp post-signature-sep-regexp nil "a")
  768. (setq sig-end (match-beginning 0))
  769. (setq sig-end (point-max)))
  770. (let ((sig (buffer-substring-no-properties sig-start sig-end)))
  771. (switch-to-buffer post-select-signature-last-buffer)
  772. (goto-char (post-kill-signature))
  773. (insert-string "-- \n")
  774. (insert sig))
  775. (if (file-readable-p post-fixed-signature-source)
  776. (insert-file-contents post-fixed-signature-source))
  777. (post-select-signature-quit)))
  778. (defun post-select-signature-from-dir ()
  779. "Select a new signature for an email/post in the current buffer."
  780. (interactive)
  781. (setq post-select-signature-last-buffer (current-buffer))
  782. (setq post-select-signature-last-point (point))
  783. (pop-to-buffer "*Post-Select-Signature*")
  784. (list-directory (concat post-signature-directory
  785. post-signature-wildcard) t)
  786. (pop-to-buffer "*Directory*")
  787. (next-line 1)
  788. (copy-to-buffer "*Post-Select-Signature*" (point) (point-max))
  789. (kill-buffer "*Directory*")
  790. (pop-to-buffer "*Post-Select-Signature*")
  791. (use-local-map post-select-signature-mode-map)
  792. (toggle-read-only t))
  793. (defun post-select-signature-select-sig-from-dir ()
  794. "Set the signature in the calling buffer to the one under the cursor."
  795. (interactive)
  796. (let ((sig-start nil)
  797. (sig-to-load nil))
  798. (end-of-line)
  799. (search-backward " ")
  800. (forward-char)
  801. (setq sig-start (point))
  802. (end-of-line)
  803. (setq sig-to-load (buffer-substring-no-properties sig-start (point)))
  804. (switch-to-buffer post-select-signature-last-buffer)
  805. (goto-char (post-kill-signature))
  806. (insert-string "-- \n")
  807. (insert-file (concat post-signature-directory sig-to-load))
  808. (message "Signature set to %s%s" post-signature-directory sig-to-load)
  809. (post-select-signature-quit)))
  810. (defun post-select-signature-quit ()
  811. "Kill the *Post-Select-Signature* frame."
  812. (interactive)
  813. (kill-buffer "*Post-Select-Signature*")
  814. (switch-to-buffer post-select-signature-last-buffer)
  815. (goto-char post-select-signature-last-point)
  816. (delete-other-windows))
  817. ;;; Non-interactive functions
  818. (defun post-ask-for-address-with-default (header)
  819. "Prompt for an email address, showing default.
  820. Argument HEADER the header type."
  821. (let ((default (if (= (length (post-get-header-value header)) 0)
  822. post-email-address
  823. (post-get-header-value header))))
  824. (read-string (concat header ": ") default)))
  825. ; From davep@davep.org. RR hasn't tested it.
  826. (defun post-get-header-value (header)
  827. "Get the value of a specific mail HEADER."
  828. (save-excursion
  829. (let ((value "")
  830. (start-of-value nil))
  831. (setf (point) (point-min))
  832. (when (post-find-header-line header)
  833. (setq start-of-value (point))
  834. (end-of-line)
  835. (setq value (buffer-substring-no-properties start-of-value (point))))
  836. value)))
  837. ;;; From davep@davep.org. RR hasn't tested it.
  838. (defun post-find-header-line (header)
  839. "Find a HEADER line in the header."
  840. (let ((old-point (point))
  841. (end-of-header nil)
  842. (found-point nil))
  843. (setf (point) (point-min))
  844. (search-forward-regexp "^$" nil t)
  845. (setq end-of-header (point))
  846. (setf (point) (point-min))
  847. (cond ((search-forward-regexp (concat "^" header ": ") nil t)
  848. (cond ((< (point) end-of-header)
  849. (setq found-point (point)))
  850. (t
  851. (setf (point) old-point))))
  852. (t
  853. (setf (point) old-point)))
  854. found-point))
  855. ;;; Function to make a backup buffer for viewing the original.
  856. (defun post-copy-original ()
  857. "Make a copy of the `post-mode' buffer before any editing by the user.
  858. This way they can refer back to this buffer during a compose session."
  859. (copy-to-buffer (get-buffer-create "*Original*")
  860. (point-min) (point-max)))
  861. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  862. ;;;
  863. ;;; The Heart of Darkness
  864. ;;;
  865. ;;; The old post mode (i.e. Dave Pearson's) derived from mail-mode. I
  866. ;;; prefer deriving from text mode like mutt mode did. - RR
  867. (define-derived-mode post-mode text-mode "Post"
  868. "Major mode for composing email or news with an external agent.
  869. To customize it, type \\[customize] and select [Applications] [Mail] [Post].
  870. When you finish editing this message, type \\[post-save-current-buffer-and-exit] to save and exit Emacs.
  871. \\{post-mode-map}"
  872. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  873. ;;;
  874. ;;; Neat things to do right off the bat.
  875. (auto-fill-mode (if post-uses-fill-mode 1 0))
  876. (if post-backup-original (post-copy-original))
  877. ;; Make Emacs smarter about wrapping citations and paragraphs.
  878. ;; We probably can't handle Supercited messages, though.
  879. (make-local-variable 'paragraph-start)
  880. (make-local-variable 'paragraph-separate)
  881. (setq paragraph-start
  882. "\\([ \t\n\f]+[^ \t\n\f>]\\|[ \t\f>]*$\\)"
  883. paragraph-separate
  884. "[ \t\f>]*$")
  885. ;; XEmacs needs easy-menu-add, Emacs does not care
  886. (easy-menu-add post-mode-menu)
  887. ;; If headers were passed, activate the necessary commands.
  888. (when (looking-at "^[-A-Za-z0-9]+:")
  889. (header-mode 1))
  890. ;; Our temporary file lives in /tmp. Yuck! Compensate appropriately.
  891. (make-local-variable 'backup-inhibited)
  892. (setq backup-inhibited t)
  893. (if (boundp 'font-lock-defaults)
  894. (make-local-variable 'font-lock-defaults))
  895. (flet ((add-syntax-highlight (face regexps)
  896. (set face face)
  897. (nconc post-font-lock-keywords
  898. (loop for regexp in regexps
  899. collect (list regexp (list 0 face 't))))))
  900. ; collect (list regexp `(,0 ',face))))))
  901. (add-syntax-highlight 'post-emoticon-face post-emoticon-pattern)
  902. (add-syntax-highlight 'post-bold-face post-bold-pattern)
  903. (add-syntax-highlight 'post-underline-face post-underline-pattern)
  904. (add-syntax-highlight 'post-url-face post-url-pattern))
  905. (setq font-lock-defaults
  906. '(post-font-lock-keywords nil nil nil nil
  907. (font-lock-syntactic-keywords
  908. . post-font-lock-syntactic-keywords)
  909. (font-lock-comment-face
  910. ; . 'post-signature-text-face)))
  911. . post-signature-text-face)))
  912. ;; Force pwd to home directory if so required.
  913. (cond (post-force-pwd-to-home
  914. (cd "~")))
  915. ;; Kill quoted sig if so required.
  916. (cond (post-kill-quoted-sig
  917. (post-delete-quoted-signatures)
  918. (not-modified)))
  919. ;; Remap signature selection functions according to whether the
  920. ;; signatures are stored in a file or directory.
  921. (if post-signature-source-is-file
  922. (progn
  923. (defalias 'post-select-signature 'post-select-signature-from-file)
  924. (defalias 'post-select-signature-select-sig
  925. 'post-select-signature-select-sig-from-file))
  926. (progn
  927. (defalias 'post-select-signature 'post-select-signature-from-dir)
  928. (defalias 'post-select-signature-select-sig
  929. 'post-select-signature-select-sig-from-dir)))
  930. ;; Define mutt/slrn specific key bindings.
  931. (define-key (current-local-map) "\C-c\C-b" 'post-make-region-bold)
  932. (define-key (current-local-map) "\C-c\C-u" 'post-make-region-underlined)
  933. (define-key (current-local-map) "\C-c\C-q" 'post-quote-region)
  934. (define-key (current-local-map) "\C-c\C-d\C-q" 'post-unquote-region)
  935. (define-key (current-local-map) "\C-c\C-a" 'post-attach-file)
  936. (define-key (current-local-map) "\C-c\C-p" 'post-set-return-receipt-to)
  937. ;; Give the buffer a handy name.
  938. (if post-rename-buffer
  939. (setq post-buf (rename-buffer "*Composing*" t)))
  940. ;; If this is a news posting, check the length of the References field.
  941. (if (post-references-p)
  942. (header-check-references))
  943. ;; Define the quote signs as comments to make comment-region usable.
  944. (make-local-variable 'comment-start)
  945. (setq comment-start post-quote-start)
  946. ;; Run any hooks.
  947. (run-hooks 'post-mode-hook)
  948. ;; Jump past header if so required.
  949. (cond (post-jump-header
  950. (post-goto-body)))
  951. (unless (fboundp 'server-process-filter)
  952. (message (substitute-command-keys
  953. "Type \\[describe-mode] for help composing; \\[post-save-current-buffer-and-exit] when done."))))
  954. (defun post-references-p ()
  955. "Is there a References header in this buffer?"
  956. (save-excursion
  957. (goto-char (point-min))
  958. (looking-at "^References: ")))
  959. (defun post-body-says-attach ()
  960. "Check if attach appears in the body."
  961. (post-goto-body)
  962. ;; Aargh it's annoying that how-many returns a string,
  963. ;; "13 occurences" instead of a number, 13.
  964. (let ((total-attach (string-to-int (how-many post-attachment-regexp))))
  965. ;; And this mess is just to catch the unlikely false alarm of
  966. ;; "attach" being in the signature, but not in the body.
  967. (if (> total-attach 0)
  968. (progn (post-goto-signature)
  969. (> total-attach (string-to-int (how-many
  970. post-attachment-regexp)))))))
  971. (defun post-prompt-for-attachment ()
  972. "Prompt for an attachment."
  973. (if (y-or-n-p "Do you want to attach anything? ")
  974. (let ((file (read-file-name "Attach file: " nil nil t nil))
  975. (description (string-read "Description: ")))
  976. (header-attach-file file description))))
  977. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  978. ;;;
  979. ;;; Post Header Mode
  980. (defvar header-mode nil)
  981. (defun header-mode (&optional arg)
  982. "Commands for editing the header of an e-mail or news message.
  983. \\{header-mode-map}
  984. Optional argument ARG ."
  985. (interactive "P")
  986. (make-local-variable 'header-mode)
  987. (setq header-mode
  988. (if (null arg)
  989. (not header-mode)
  990. (> (prefix-numeric-value arg) 0)))
  991. (setq post-has-attachment nil)
  992. ;; XEmacs needs easy-menu-add, Emacs does not care
  993. (easy-menu-add header-mode-menu)
  994. (force-mode-line-update))
  995. (defvar header-mode-map (make-sparse-keymap)
  996. "Keymap used for editing RFC822 header.")
  997. (defun header-position-on-value ()
  998. "Go to the start of the value part of a header."
  999. (beginning-of-line)
  1000. (skip-chars-forward "-A-Za-z0-9:")
  1001. ;; XXX - Should make sure we stay on line.
  1002. (forward-char)
  1003. (point))
  1004. (defun header-goto-field (field)
  1005. "Go to FIELD of a header."
  1006. (let ((case-fold-search t))
  1007. (goto-char (point-min))
  1008. (save-match-data
  1009. (when (re-search-forward (concat "^\\($\\|" field ": \\)"))
  1010. (if (looking-at "^$")
  1011. (progn
  1012. (insert-string field ": \n")
  1013. (forward-char -1))
  1014. (header-position-on-value))))))
  1015. (defmacro define-header-goto (name header)
  1016. "Define functions called NAME to go to HEADER."
  1017. `(defun ,name ()
  1018. ,(concat "Position the cursor on the " header ": header.")
  1019. (interactive)
  1020. (header-goto-field ,header)))
  1021. (define-header-goto header-goto-to "To")
  1022. (define-header-goto header-goto-cc "Cc")
  1023. (define-header-goto header-goto-fcc "Fcc")
  1024. (define-header-goto header-goto-summary "Summary")
  1025. (define-header-goto header-goto-keywords "Keywords")
  1026. (define-header-goto header-goto-subject "Subject")
  1027. (define-header-goto header-goto-bcc "Bcc")
  1028. (define-header-goto header-goto-reply-to "Reply-To")
  1029. (define-header-goto header-goto-from "From")
  1030. (define-header-goto header-goto-organization "Organization")
  1031. (defun header-attach-file (file description)
  1032. "Attach a FILE to the current message (works with Mutt).
  1033. Argument DESCRIPTION MIME description."
  1034. (interactive "fAttach file: \nsDescription: ")
  1035. (when (> (length file) 0)
  1036. (save-excursion
  1037. (save-match-data
  1038. (save-restriction
  1039. (widen)
  1040. (goto-char (point-min))
  1041. (search-forward-regexp "^$")
  1042. (insert-string (concat "Attach: " (file-truename file) " "
  1043. description "\n"))
  1044. (message (concat "Attached '" file "'."))
  1045. (setq post-has-attachment t))))))
  1046. (or (assq 'header-mode minor-mode-alist)
  1047. (setq minor-mode-alist
  1048. (cons '(header-mode " Header") minor-mode-alist)))
  1049. (or (assq 'header-mode minor-mode-map-alist)
  1050. (setq minor-mode-map-alist
  1051. (cons (cons 'header-mode header-mode-map)
  1052. minor-mode-map-alist)))
  1053. (defun header-set-return-receipt-to (address)
  1054. "Insert a Return-Receipt-To header into an email.
  1055. Argument ADDRESS email address return receipts should be sent to."
  1056. (interactive (list (post-ask-for-address-with-default "Return-Receipt-To")))
  1057. (save-excursion
  1058. (header-set-value "Return-Receipt-To" address)))
  1059. (defun post-news-posting-p ()
  1060. "Does the buffer look like a news posting?"
  1061. (save-excursion
  1062. (setf (point) (point-min))
  1063. (looking-at "^Newsgroups: ")))
  1064. (defun header-set-followup-to (to)
  1065. "Set the Followup-To: header.
  1066. Argument TO Where followups should go."
  1067. (interactive (list (header-ask-for-value "Followup-To"
  1068. (header-ask-for-value
  1069. "Newsgroups"))))
  1070. (cond ((post-news-posting-p)
  1071. (save-excursion
  1072. (header-set-value "Followup-To" to)))
  1073. (t
  1074. (error
  1075. "Followup-To is for Usenet. Maybe you want Reply-To or Mail-Followup-To"))))
  1076. (defun header-set-organization (org)
  1077. "Set the Organization: header.
  1078. Argument ORG Should be SMERSH."
  1079. (interactive (list (header-ask-for-value "Organization")))
  1080. (save-excursion
  1081. (header-set-value "Organization" org)))
  1082. (defun header-check-references ()
  1083. "Place the cursor at the start of the References: if they are too long."
  1084. (interactive)
  1085. (cond ((> (header-references-length) 500) ; 500 to be on the safe side.
  1086. (beep) ; Catch my attention.
  1087. (goto-char (point-min))
  1088. (search-forward-regexp "^References: " nil t))))
  1089. (defun header-references-length (&optional show)
  1090. "Get (and optionally display) the length of the references header.
  1091. Optional argument SHOW Whether or not to display the length."
  1092. (interactive)
  1093. (let* ((header "References")
  1094. (refs (header-get-value header))
  1095. (len (+ (length header) (length refs) 2)))
  1096. (if (or (interactive-p) show)
  1097. (message "References header is %d characters in length." len))
  1098. len))
  1099. (defun header-delete-reference ()
  1100. "Delete the first reference in the references header."
  1101. (interactive)
  1102. (save-excursion
  1103. (let ((ref-location (header-goto-field "References")))
  1104. (cond (ref-location
  1105. (let ((ref-start (goto-char ref-location)))
  1106. (cond ((search-forward ">" nil t)
  1107. (forward-char 1)
  1108. (delete-region ref-start (point))
  1109. (header-references-length t)))))))))
  1110. ;; Noninteractive functions.
  1111. (defun header-ask-for-value (header &optional default)
  1112. "Ask for a HEADER value, defaulting to the current value if one is present.
  1113. Optional argument DEFAULT ."
  1114. (let ((new-value (post-get-header-value header)))
  1115. (and (= (length new-value) 0)
  1116. default
  1117. (setq new-value default))
  1118. (read-string (concat header ": ") new-value)))
  1119. (defun header-get-value (header)
  1120. "Get the value of a specific mail HEADER."
  1121. (save-excursion
  1122. (let ((value "")
  1123. (start-of-value nil))
  1124. (goto-char (point-min))
  1125. (cond ((post-find-header-line header)
  1126. (setq start-of-value (point))
  1127. (end-of-line)
  1128. (setq value (buffer-substring-no-properties
  1129. start-of-value (point)))))
  1130. value)))
  1131. (defun header-set-value (header value)
  1132. "Set VALUE of a HEADER (replacing any existing value)."
  1133. (let ((kill-ring kill-ring))
  1134. (setf (point) (point-min))
  1135. (cond ((post-find-header-line header)
  1136. (beginning-of-line)
  1137. (kill-line)
  1138. (insert-string (concat header ": " value)))
  1139. (t
  1140. (header-append-value header value))))
  1141. (message "%s set to %s" header value))
  1142. (defun header-append-value (header value)
  1143. "Add a HEADER and set it's VALUE (if header exists, will add multiple headers)."
  1144. (goto-char (point-min))
  1145. (search-forward-regexp "^$" nil t)
  1146. (insert-string (concat header ": " value "\n")))
  1147. ;;; Setup the mode map for the select-signature buffer.
  1148. (if post-select-signature-mode-map nil
  1149. (setq post-select-signature-mode-map (make-sparse-keymap))
  1150. (define-key post-select-signature-mode-map "\C-m"
  1151. 'post-select-signature-select-sig)
  1152. (define-key post-select-signature-mode-map " "
  1153. 'post-select-signature-select-sig)
  1154. (define-key post-select-signature-mode-map "q" 'post-select-signature-quit)
  1155. (define-key post-select-signature-mode-map "\C-g"
  1156. 'post-select-signature-quit))
  1157. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1158. ;;;
  1159. ;;; Key Bindings
  1160. (define-key post-mode-map "\C-c\C-c" 'post-save-current-buffer-and-exit)
  1161. (define-key post-mode-map "\C-c\C-d\C-s" 'post-delete-quoted-signatures)
  1162. (define-key post-mode-map "\C-c\C-d\C-c" 'post-delete-old-citations)
  1163. (define-key post-mode-map "\C-c\C-t" 'post-goto-body)
  1164. (define-key post-mode-map "\C-c\C-e" 'post-goto-signature)
  1165. (define-key post-mode-map "\C-c\C-r" 'post-random-signature)
  1166. (define-key post-mode-map "\C-c\C-b" 'post-make-region-bold)
  1167. (define-key post-mode-map "\C-c\C-u" 'post-make-region-underlined)
  1168. (define-key post-mode-map "\C-c\C-q" 'post-quote-region)
  1169. (define-key post-mode-map "\C-c\C-d\C-q" 'post-unquote-region)
  1170. (define-key post-mode-map "\C-c\C-s" 'post-select-signature)
  1171. (define-key header-mode-map "\C-c\C-f\C-t" 'header-goto-to)
  1172. (define-key header-mode-map "\C-c\C-f\C-c" 'header-goto-cc)
  1173. (define-key header-mode-map "\C-c\C-f\C-w" 'header-goto-fcc)
  1174. (define-key header-mode-map "\C-c\C-f\C-u" 'header-goto-summary)
  1175. (define-key header-mode-map "\C-c\C-f\C-k" 'header-goto-keywords)
  1176. (define-key header-mode-map "\C-c\C-f\C-s" 'header-goto-subject)
  1177. (define-key header-mode-map "\C-c\C-f\C-b" 'header-goto-bcc)
  1178. (define-key header-mode-map "\C-c\C-f\C-r" 'header-goto-reply-to)
  1179. (define-key header-mode-map "\C-c\C-f\C-f" 'header-goto-from)
  1180. (define-key header-mode-map "\C-c\C-f\C-o" 'header-goto-organization)
  1181. (define-key header-mode-map "\C-c\C-ff" 'header-set-followup-to)
  1182. (define-key header-mode-map "\C-c\C-a" 'header-attach-file)
  1183. (define-key header-mode-map "\C-c\C-fd" 'header-delete-reference)
  1184. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1185. ;;;
  1186. ;;; Menus
  1187. (easy-menu-define
  1188. post-mode-menu post-mode-map "Post Message Composition Commands."
  1189. '("Post"
  1190. ["Delete quoted signatures" post-delete-quoted-signatures t]
  1191. ["Delete doubly quoted text" post-delete-old-citations t]
  1192. "----"
  1193. ["Go to body of message" post-goto-body t]
  1194. ["Go to signature of message" post-goto-signature t]
  1195. ["Get new random signature" post-random-signature t]
  1196. ["Select new signature" post-select-signature t]
  1197. "----"
  1198. ["Embolden region" post-make-region-bold t]
  1199. ["Underline region" post-make-region-underlined t]
  1200. "----"
  1201. ["Quote region" post-quote-region t]
  1202. ["Unquote region" post-unquote-region t]
  1203. "----"
  1204. ["Save message and return from Post" post-save-current-buffer-and-exit t]))
  1205. (easy-menu-define
  1206. header-mode-menu header-mode-map "Header Editing Commands."
  1207. '("Header"
  1208. ["Attach File..." header-attach-file t]
  1209. "----"
  1210. ["Edit From Header" header-goto-from t]
  1211. ["Edit Subject Header" header-goto-subject t]
  1212. ["Edit To Header" header-goto-to t]
  1213. ["Edit Cc Header" header-goto-cc t]
  1214. ["Edit Bcc Header" header-goto-bcc t]
  1215. ["Edit Fcc Header" header-goto-fcc t]
  1216. ["Edit Reply-To Header" header-goto-reply-to t]
  1217. ["Edit Summary Header" header-goto-summary t]
  1218. ["Edit Keywords Header" header-goto-keywords t]
  1219. ["Edit Organization Header" header-goto-organization t]))
  1220. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1221. ;;;
  1222. ;;; Finish Installing Post Mode
  1223. (when post-insert-to-auto-mode-alist-on-load
  1224. (unless (assq post-mail-message auto-mode-alist)
  1225. (setq auto-mode-alist
  1226. (cons (cons post-mail-message 'post-mode)
  1227. auto-mode-alist)))
  1228. (unless (assq post-news-posting auto-mode-alist)
  1229. (setq auto-mode-alist
  1230. (cons (cons post-news-posting 'post-mode)
  1231. auto-mode-alist))))
  1232. (provide 'post)
  1233. ;;; post.el ends here