sendmail.el 73 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009
  1. ;;; sendmail.el --- mail sending commands for Emacs
  2. ;; Copyright (C) 1985-1986, 1992-1996, 1998, 2000-2012
  3. ;; Free Software Foundation, Inc.
  4. ;; Maintainer: FSF
  5. ;; Keywords: mail
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;; This mode provides mail-sending facilities from within Emacs. It is
  19. ;; documented in the Emacs user's manual.
  20. ;;; Code:
  21. (require 'mail-utils)
  22. (require 'rfc2047)
  23. (defgroup sendmail nil
  24. "Mail sending commands for Emacs."
  25. :prefix "mail-"
  26. :group 'mail)
  27. (defcustom mail-setup-with-from t
  28. "Non-nil means insert `From:' field when setting up the message."
  29. :type 'boolean
  30. :group 'sendmail
  31. :version "22.1")
  32. (defcustom sendmail-program
  33. (or (executable-find "sendmail")
  34. (cond
  35. ((file-exists-p "/usr/sbin/sendmail") "/usr/sbin/sendmail")
  36. ((file-exists-p "/usr/lib/sendmail") "/usr/lib/sendmail")
  37. ((file-exists-p "/usr/ucblib/sendmail") "/usr/ucblib/sendmail")
  38. (t "sendmail")))
  39. "Program used to send messages."
  40. :version "24.1" ; add executable-find, remove fakemail
  41. :group 'mail
  42. :type 'file)
  43. ;;;###autoload
  44. (defcustom mail-from-style 'default
  45. "Specifies how \"From:\" fields look.
  46. If `nil', they contain just the return address like:
  47. king@grassland.com
  48. If `parens', they look like:
  49. king@grassland.com (Elvis Parsley)
  50. If `angles', they look like:
  51. Elvis Parsley <king@grassland.com>
  52. Otherwise, most addresses look like `angles', but they look like
  53. `parens' if `angles' would need quoting and `parens' would not."
  54. ;; The value `system-default' is now deprecated.
  55. :type '(choice (const :tag "simple" nil)
  56. (const parens)
  57. (const angles)
  58. (const default))
  59. :version "20.3"
  60. :group 'sendmail)
  61. ;;;###autoload
  62. (defcustom mail-specify-envelope-from nil
  63. "If non-nil, specify the envelope-from address when sending mail.
  64. The value used to specify it is whatever is found in
  65. the variable `mail-envelope-from', with `user-mail-address' as fallback.
  66. On most systems, specifying the envelope-from address is a
  67. privileged operation. This variable affects sendmail and
  68. smtpmail -- if you use feedmail to send mail, see instead the
  69. variable `feedmail-deduce-envelope-from'."
  70. :version "21.1"
  71. :type 'boolean
  72. :group 'sendmail)
  73. (defcustom mail-envelope-from nil
  74. "If non-nil, designate the envelope-from address when sending mail.
  75. This only has an effect if `mail-specify-envelope-from' is non-nil.
  76. The value should be either a string, or the symbol `header' (in
  77. which case the contents of the \"From\" header of the message
  78. being sent is used), or nil (in which case the value of
  79. `user-mail-address' is used)."
  80. :version "21.1"
  81. :type '(choice (string :tag "From-name")
  82. (const :tag "Use From: header from message" header)
  83. (const :tag "Use `user-mail-address'" nil))
  84. :group 'sendmail)
  85. ;;;###autoload
  86. (defcustom mail-self-blind nil
  87. "Non-nil means insert BCC to self in messages to be sent.
  88. This is done when the message is initialized,
  89. so you can remove or alter the BCC field to override the default."
  90. :type 'boolean
  91. :group 'sendmail)
  92. ;;;###autoload
  93. (defcustom mail-interactive t
  94. ;; We used to use a default of nil rather than t, but nowadays it is very
  95. ;; common for sendmail to be misconfigured, so one cannot rely on the
  96. ;; bounce message to be delivered anywhere, least of all to the
  97. ;; user's mailbox.
  98. "Non-nil means when sending a message wait for and display errors.
  99. Otherwise, let mailer send back a message to report errors."
  100. :type 'boolean
  101. :version "23.1" ; changed from nil to t
  102. :group 'sendmail)
  103. (defcustom mail-yank-ignored-headers
  104. (concat "^"
  105. (regexp-opt '("via" "mail-from" "origin" "status" "remailed"
  106. "received" "message-id" "summary-line" "to" "subject"
  107. "in-reply-to" "return-path" "mail-reply-to"
  108. ;; Should really be rmail-attribute-header and
  109. ;; rmail-keyword-header, but this file does not
  110. ;; require rmail (at run time).
  111. "x-rmail-attributes" "x-rmail-keywords"
  112. "mail-followup-to") "\\(?:")
  113. ":")
  114. "Delete these headers from old message when it's inserted in a reply."
  115. :type 'regexp
  116. :group 'sendmail
  117. :version "23.1")
  118. ;; Useful to set in site-init.el
  119. ;;;###autoload
  120. (defcustom send-mail-function
  121. ;; Assume smtpmail is the preferred choice if it's already configured.
  122. (if (and (boundp 'smtpmail-smtp-server)
  123. smtpmail-smtp-server)
  124. 'smtpmail-send-it 'sendmail-query-once)
  125. "Function to call to send the current buffer as mail.
  126. The headers should be delimited by a line which is
  127. not a valid RFC822 header or continuation line,
  128. that matches the variable `mail-header-separator'.
  129. This is used by the default mail-sending commands. See also
  130. `message-send-mail-function' for use with the Message package."
  131. :type '(radio (function-item sendmail-send-it :tag "Use Sendmail package")
  132. (function-item sendmail-query-once :tag "Query the user")
  133. (function-item smtpmail-send-it :tag "Use SMTPmail package")
  134. (function-item feedmail-send-it :tag "Use Feedmail package")
  135. (function-item mailclient-send-it :tag "Use Mailclient package")
  136. function)
  137. :version "24.1"
  138. :group 'sendmail)
  139. ;;;###autoload
  140. (defcustom mail-header-separator (purecopy "--text follows this line--")
  141. "Line used to separate headers from text in messages being composed."
  142. :type 'string
  143. :group 'sendmail)
  144. ;; Set up mail-header-separator for use as a category text property.
  145. (put 'mail-header-separator 'rear-nonsticky '(category))
  146. ;; This was a nice idea, for preventing accidental modification of
  147. ;; the separator. But I found it also prevented or obstructed
  148. ;; certain deliberate operations, such as copying the separator line
  149. ;; up to the top to send myself a copy of an already sent outgoing message
  150. ;; and other things. So I turned it off. --rms.
  151. ;;(put 'mail-header-separator 'read-only t)
  152. ;;;###autoload
  153. (defcustom mail-archive-file-name nil
  154. "Name of file to write all outgoing messages in, or nil for none.
  155. This is normally an mbox file, but for backwards compatibility may also
  156. be a Babyl file."
  157. :type '(choice file (const nil))
  158. :group 'sendmail)
  159. ;;;###autoload
  160. (defcustom mail-default-reply-to nil
  161. "Address to insert as default Reply-to field of outgoing messages.
  162. If nil, it will be initialized from the REPLYTO environment variable
  163. when you first send mail."
  164. :type '(choice (const nil) string)
  165. :group 'sendmail)
  166. (defcustom mail-alias-file nil
  167. "If non-nil, the name of a file to use instead of the sendmail default.
  168. This file defines aliases to be expanded by the mailer; this is a different
  169. feature from that of defining aliases in `.mailrc' to be expanded in Emacs.
  170. This variable has no effect unless your system uses sendmail as its mailer.
  171. The default file is defined in sendmail's configuration file, e.g.
  172. `/etc/aliases'."
  173. :type '(choice (const :tag "Sendmail default" nil) file)
  174. :group 'sendmail)
  175. ;;;###autoload
  176. (defcustom mail-personal-alias-file (purecopy "~/.mailrc")
  177. "If non-nil, the name of the user's personal mail alias file.
  178. This file typically should be in same format as the `.mailrc' file used by
  179. the `Mail' or `mailx' program.
  180. This file need not actually exist."
  181. :type '(choice (const nil) file)
  182. :group 'sendmail)
  183. ;;;###autoload
  184. (defcustom mail-setup-hook nil
  185. "Normal hook, run each time a new outgoing message is initialized."
  186. :type 'hook
  187. :options '(fortune-to-signature spook mail-abbrevs-setup)
  188. :group 'sendmail)
  189. ;;;###autoload
  190. (defvar mail-aliases t
  191. "Alist of mail address aliases,
  192. or t meaning should be initialized from your mail aliases file.
  193. \(The file's name is normally `~/.mailrc', but `mail-personal-alias-file'
  194. can specify a different file name.)
  195. The alias definitions in the file have this form:
  196. alias ALIAS MEANING")
  197. (defvar mail-alias-modtime nil
  198. "The modification time of your mail alias file when it was last examined.")
  199. ;;;###autoload
  200. (defcustom mail-yank-prefix "> "
  201. "Prefix insert on lines of yanked message being replied to.
  202. If this is nil, use indentation, as specified by `mail-indentation-spaces'."
  203. :type '(choice (const nil) string)
  204. :group 'sendmail)
  205. ;;;###autoload
  206. (defcustom mail-indentation-spaces 3
  207. "Number of spaces to insert at the beginning of each cited line.
  208. Used by `mail-yank-original' via `mail-indent-citation'."
  209. :type 'integer
  210. :group 'sendmail)
  211. ;; FIXME make it really obsolete.
  212. (defvar mail-yank-hooks nil
  213. "Obsolete hook for modifying a citation just inserted in the mail buffer.
  214. Each hook function can find the citation between (point) and (mark t).
  215. And each hook function should leave point and mark around the citation
  216. text as modified.
  217. This is a normal hook, misnamed for historical reasons.
  218. It is semi-obsolete and mail agents should no longer use it.")
  219. ;;;###autoload
  220. (defcustom mail-citation-hook nil
  221. "Hook for modifying a citation just inserted in the mail buffer.
  222. Each hook function can find the citation between (point) and (mark t),
  223. and should leave point and mark around the citation text as modified.
  224. The hook functions can find the header of the cited message
  225. in the variable `mail-citation-header', whether or not this is included
  226. in the cited portion of the message.
  227. If this hook is entirely empty (nil), a default action is taken
  228. instead of no action."
  229. :type 'hook
  230. :group 'sendmail)
  231. (defvar mail-citation-header nil
  232. "While running `mail-citation-hook', this variable holds the message header.
  233. This enables the hook functions to see the whole message header
  234. regardless of what part of it (if any) is included in the cited text.")
  235. ;;;###autoload
  236. (defcustom mail-citation-prefix-regexp
  237. (purecopy "\\([ \t]*\\(\\w\\|[_.]\\)+>+\\|[ \t]*[]>|]\\)+")
  238. "Regular expression to match a citation prefix plus whitespace.
  239. It should match whatever sort of citation prefixes you want to handle,
  240. with whitespace before and after; it should also match just whitespace.
  241. The default value matches citations like `foo-bar>' plus whitespace."
  242. :type 'regexp
  243. :group 'sendmail
  244. :version "24.1")
  245. (defvar mail-abbrevs-loaded nil)
  246. (defvar mail-mode-map
  247. (let ((map (make-sparse-keymap)))
  248. (define-key map "\M-\t" 'completion-at-point)
  249. (define-key map "\C-c?" 'describe-mode)
  250. (define-key map "\C-c\C-f\C-t" 'mail-to)
  251. (define-key map "\C-c\C-f\C-b" 'mail-bcc)
  252. (define-key map "\C-c\C-f\C-f" 'mail-fcc)
  253. (define-key map "\C-c\C-f\C-c" 'mail-cc)
  254. (define-key map "\C-c\C-f\C-s" 'mail-subject)
  255. (define-key map "\C-c\C-f\C-r" 'mail-reply-to)
  256. (define-key map "\C-c\C-f\C-a" 'mail-mail-reply-to) ; author
  257. (define-key map "\C-c\C-f\C-l" 'mail-mail-followup-to) ; list
  258. (define-key map "\C-c\C-t" 'mail-text)
  259. (define-key map "\C-c\C-y" 'mail-yank-original)
  260. (define-key map "\C-c\C-r" 'mail-yank-region)
  261. (define-key map [remap split-line] 'mail-split-line)
  262. (define-key map "\C-c\C-q" 'mail-fill-yanked-message)
  263. (define-key map "\C-c\C-w" 'mail-signature)
  264. (define-key map "\C-c\C-c" 'mail-send-and-exit)
  265. (define-key map "\C-c\C-s" 'mail-send)
  266. (define-key map "\C-c\C-i" 'mail-insert-file)
  267. ;; FIXME add this? "b" = bury buffer. It's in the menu-bar.
  268. ;;; (define-key map "\C-c\C-b" 'mail-dont-send)
  269. (define-key map [menu-bar mail]
  270. (cons "Mail" (make-sparse-keymap "Mail")))
  271. (define-key map [menu-bar mail attachment]
  272. '("Attach File" . mail-add-attachment))
  273. (define-key map [menu-bar mail fill]
  274. '("Fill Citation" . mail-fill-yanked-message))
  275. (define-key map [menu-bar mail yank]
  276. '(menu-item "Cite Original" mail-yank-original :enable mail-reply-action))
  277. (define-key map [menu-bar mail signature]
  278. '("Insert Signature" . mail-signature))
  279. (define-key map [menu-bar mail mail-sep]
  280. '("--"))
  281. (define-key map [menu-bar mail cancel]
  282. '("Cancel" . mail-dont-send))
  283. (define-key map [menu-bar mail send-stay]
  284. '("Send, Keep Editing" . mail-send))
  285. (define-key map [menu-bar mail send]
  286. '("Send Message" . mail-send-and-exit))
  287. (define-key map [menu-bar headers]
  288. (cons "Headers" (make-sparse-keymap "Move to Header")))
  289. (define-key map [menu-bar headers text]
  290. '("Text" . mail-text))
  291. (define-key map [menu-bar headers expand-aliases]
  292. '("Expand Aliases" . expand-mail-aliases))
  293. (define-key map [menu-bar headers mail-reply-to]
  294. '("Mail-Reply-To" . mail-mail-reply-to))
  295. (define-key map [menu-bar headers mail-followup-to]
  296. '("Mail-Followup-To" . mail-mail-followup-to))
  297. (define-key map [menu-bar headers reply-to]
  298. '("Reply-To" . mail-reply-to))
  299. (define-key map [menu-bar headers bcc]
  300. '("Bcc" . mail-bcc))
  301. (define-key map [menu-bar headers fcc]
  302. '("Fcc" . mail-fcc))
  303. (define-key map [menu-bar headers cc]
  304. '("Cc" . mail-cc))
  305. (define-key map [menu-bar headers subject]
  306. '("Subject" . mail-subject))
  307. (define-key map [menu-bar headers to]
  308. '("To" . mail-to))
  309. map))
  310. (autoload 'build-mail-aliases "mailalias"
  311. "Read mail aliases from personal aliases file and set `mail-aliases'.
  312. By default, this is the file specified by `mail-personal-alias-file'." t)
  313. ;;;###autoload
  314. (defcustom mail-signature t
  315. "Text inserted at end of mail buffer when a message is initialized.
  316. If t, it means to insert the contents of the file `mail-signature-file'.
  317. If a string, that string is inserted.
  318. (To make a proper signature, the string should begin with \\n\\n-- \\n,
  319. which is the standard way to delimit a signature in a message.)
  320. Otherwise, it should be an expression; it is evaluated
  321. and should insert whatever you want to insert."
  322. :type '(choice (const :tag "None" nil)
  323. (const :tag "Use `.signature' file" t)
  324. (string :tag "String to insert")
  325. (sexp :tag "Expression to evaluate"))
  326. :group 'sendmail)
  327. (put 'mail-signature 'risky-local-variable t)
  328. ;;;###autoload
  329. (defcustom mail-signature-file (purecopy "~/.signature")
  330. "File containing the text inserted at end of mail buffer."
  331. :type 'file
  332. :group 'sendmail)
  333. ;;;###autoload
  334. (defcustom mail-default-directory (purecopy "~/")
  335. "Value of `default-directory' for Mail mode buffers.
  336. This directory is used for auto-save files of Mail mode buffers.
  337. Note that Message mode does not use this variable; it auto-saves
  338. in `message-auto-save-directory'."
  339. :type '(directory :tag "Directory")
  340. :group 'sendmail
  341. :version "22.1")
  342. (defvar mail-reply-action nil)
  343. (defvar mail-send-actions nil
  344. "A list of actions to be performed upon successful sending of a message.")
  345. (defvar mail-return-action nil)
  346. ;;;###autoload
  347. (defcustom mail-default-headers nil
  348. "A string containing header lines, to be inserted in outgoing messages.
  349. It can contain newlines, and should end in one. It is inserted
  350. before you edit the message, so you can edit or delete the lines."
  351. :type '(choice (const nil) string)
  352. :group 'sendmail)
  353. (defcustom mail-bury-selects-summary t
  354. "If non-nil, try to show Rmail summary buffer after returning from mail.
  355. The functions \\[mail-send-on-exit] or \\[mail-dont-send] select
  356. the Rmail summary buffer before returning, if it exists and this variable
  357. is non-nil."
  358. :type 'boolean
  359. :group 'sendmail)
  360. (defcustom mail-send-nonascii 'mime
  361. "Specify whether to allow sending non-ASCII characters in mail.
  362. If t, that means do allow it. nil means don't allow it.
  363. `query' means ask the user each time.
  364. `mime' means add an appropriate MIME header if none already present.
  365. The default is `mime'.
  366. Including non-ASCII characters in a mail message can be problematical
  367. for the recipient, who may not know how to decode them properly."
  368. :type '(choice (const t) (const nil) (const query) (const mime))
  369. :group 'sendmail)
  370. (defcustom mail-use-dsn nil
  371. "Ask MTA for notification of failed, delayed or successful delivery.
  372. Note that only some MTAs (currently only recent versions of Sendmail)
  373. support Delivery Status Notification."
  374. :group 'sendmail
  375. :type '(repeat (radio (const :tag "Failure" failure)
  376. (const :tag "Delay" delay)
  377. (const :tag "Success" success)))
  378. :version "22.1")
  379. ;; Note: could use /usr/ucb/mail instead of sendmail;
  380. ;; options -t, and -v if not interactive.
  381. (defvar mail-mailer-swallows-blank-line nil
  382. "Set this non-nil if the system's mailer runs the header and body together.
  383. The actual value should be an expression to evaluate that returns
  384. non-nil if the problem will actually occur.
  385. \(As far as we know, this is not an issue on any system still supported
  386. by Emacs.)")
  387. (put 'mail-mailer-swallows-blank-line 'risky-local-variable t) ; gets evalled
  388. (make-obsolete-variable 'mail-mailer-swallows-blank-line
  389. "no need to set this on any modern system."
  390. "24.1" 'set)
  391. (defvar mail-mode-syntax-table
  392. ;; define-derived-mode will make it inherit from text-mode-syntax-table.
  393. (let ((st (make-syntax-table)))
  394. ;; FIXME this is probably very obsolete now ("percent hack").
  395. ;; sending.texi used to say:
  396. ;; Mail mode defines the character `%' as a word separator; this
  397. ;; is helpful for using the word commands to edit mail addresses.
  398. (modify-syntax-entry ?% ". " st)
  399. st)
  400. "Syntax table used while in `mail-mode'.")
  401. (defvar mail-font-lock-keywords
  402. (eval-when-compile
  403. (let* ((cite-chars "[>|}]")
  404. (cite-prefix "[:alpha:]")
  405. (cite-suffix (concat cite-prefix "0-9_.@-`'\"")))
  406. (list '("^\\(To\\|Newsgroups\\):" . font-lock-function-name-face)
  407. '("^\\(B?CC\\|Reply-to\\|Mail-\\(reply\\|followup\\)-to\\):" . font-lock-keyword-face)
  408. '("^\\(Subject:\\)[ \t]*\\(.+\\)?"
  409. (1 font-lock-comment-face)
  410. ;; (2 font-lock-type-face nil t)
  411. )
  412. ;; Use EVAL to delay in case `mail-header-separator' gets changed.
  413. '(eval .
  414. (let ((separator (if (zerop (length mail-header-separator))
  415. " \\`\\' "
  416. (regexp-quote mail-header-separator))))
  417. (cons (concat "^" separator "$") 'font-lock-warning-face)))
  418. ;; Use MATCH-ANCHORED to effectively anchor the regexp left side.
  419. `(,cite-chars
  420. (,(concat "\\=[ \t]*"
  421. "\\(\\(\\([" cite-prefix "]+[" cite-suffix "]*\\)?"
  422. "\\(" cite-chars "[ \t]*\\)\\)+\\)"
  423. "\\(.*\\)")
  424. (beginning-of-line) (end-of-line)
  425. (1 font-lock-comment-delimiter-face nil t)
  426. (5 font-lock-comment-face nil t)))
  427. '("^\\(X-[A-Za-z0-9-]+\\|In-reply-to\\):.*\\(\n[ \t]+.*\\)*$"
  428. . font-lock-string-face))))
  429. "Additional expressions to highlight in Mail mode.")
  430. ;;;###autoload
  431. (defun sendmail-query-once ()
  432. "Query for `send-mail-function' and send mail with it.
  433. This also saves the value of `send-mail-function' via Customize."
  434. ;; If send-mail-function is already setup, we're incorrectly called
  435. ;; a second time, probably because someone's using an old value
  436. ;; of send-mail-function.
  437. (when (eq send-mail-function 'sendmail-query-once)
  438. (sendmail-query-user-about-smtp))
  439. (funcall send-mail-function))
  440. (defun sendmail-query-user-about-smtp ()
  441. (let* ((options `(("mail client" . mailclient-send-it)
  442. ,@(when (and sendmail-program
  443. (executable-find sendmail-program))
  444. '(("transport" . sendmail-send-it)))
  445. ("smtp" . smtpmail-send-it)))
  446. (choice
  447. ;; Query the user.
  448. (with-temp-buffer
  449. (rename-buffer "*Emacs Mail Setup Help*" t)
  450. (insert "\
  451. Emacs is about to send an email message, but it has not been
  452. configured for sending email. To tell Emacs how to send email:
  453. - Type `"
  454. (propertize "mail client" 'face 'bold)
  455. "' to start your default email client and
  456. pass it the message text.\n\n")
  457. (and sendmail-program
  458. (executable-find sendmail-program)
  459. (insert "\
  460. - Type `"
  461. (propertize "transport" 'face 'bold)
  462. "' to invoke the system's mail transport agent
  463. (the `"
  464. sendmail-program
  465. "' program).\n\n"))
  466. (insert "\
  467. - Type `"
  468. (propertize "smtp" 'face 'bold)
  469. "' to send mail directly to an \"outgoing mail\" server.
  470. (Emacs may prompt you for SMTP settings).
  471. Emacs will record your selection and will use it thereafter.
  472. To change it later, customize the option `send-mail-function'.\n")
  473. (goto-char (point-min))
  474. (display-buffer (current-buffer))
  475. (let ((completion-ignore-case t))
  476. (completing-read "Send mail via: "
  477. options nil 'require-match)))))
  478. (customize-save-variable 'send-mail-function
  479. (cdr (assoc-string choice options t)))))
  480. (defun sendmail-sync-aliases ()
  481. (when mail-personal-alias-file
  482. (let ((modtime (nth 5 (file-attributes mail-personal-alias-file))))
  483. (or (equal mail-alias-modtime modtime)
  484. (setq mail-alias-modtime modtime
  485. mail-aliases t)))))
  486. ;;;###autoload
  487. (define-mail-user-agent 'sendmail-user-agent
  488. 'sendmail-user-agent-compose
  489. 'mail-send-and-exit)
  490. ;;;###autoload
  491. (defun sendmail-user-agent-compose (&optional to subject other-headers
  492. continue switch-function yank-action
  493. send-actions return-action
  494. &rest ignored)
  495. (if switch-function
  496. (funcall switch-function "*mail*"))
  497. (let ((cc (cdr (assoc-string "cc" other-headers t)))
  498. (in-reply-to (cdr (assoc-string "in-reply-to" other-headers t)))
  499. (body (cdr (assoc-string "body" other-headers t))))
  500. (or (mail continue to subject in-reply-to cc yank-action
  501. send-actions return-action)
  502. continue
  503. (error "Message aborted"))
  504. (save-excursion
  505. (rfc822-goto-eoh)
  506. (while other-headers
  507. (unless (member-ignore-case (car (car other-headers))
  508. '("in-reply-to" "cc" "body"))
  509. (insert (car (car other-headers)) ": "
  510. (cdr (car other-headers))
  511. (if use-hard-newlines hard-newline "\n")))
  512. (setq other-headers (cdr other-headers)))
  513. (when body
  514. (forward-line 1)
  515. (insert body))
  516. t)))
  517. (defun mail-setup (to subject in-reply-to cc replybuffer
  518. actions return-action)
  519. (or mail-default-reply-to
  520. (setq mail-default-reply-to (getenv "REPLYTO")))
  521. (sendmail-sync-aliases)
  522. (when (eq mail-aliases t)
  523. (setq mail-aliases nil)
  524. (and mail-personal-alias-file
  525. (file-exists-p mail-personal-alias-file)
  526. (build-mail-aliases)))
  527. ;; Don't leave this around from a previous message.
  528. (kill-local-variable 'buffer-file-coding-system)
  529. ;; This doesn't work for enable-multibyte-characters.
  530. ;; (kill-local-variable 'enable-multibyte-characters)
  531. (set-buffer-multibyte (default-value 'enable-multibyte-characters))
  532. (if current-input-method
  533. (inactivate-input-method))
  534. ;; Local variables for Mail mode.
  535. (setq mail-send-actions actions)
  536. (setq mail-reply-action replybuffer)
  537. (setq mail-return-action return-action)
  538. (goto-char (point-min))
  539. (if mail-setup-with-from
  540. (mail-insert-from-field))
  541. (insert "To: ")
  542. (save-excursion
  543. (if to
  544. ;; Here removed code to extract names from within <...>
  545. ;; on the assumption that mail-strip-quoted-names
  546. ;; has been called and has done so.
  547. (let ((fill-prefix "\t")
  548. (address-start (point)))
  549. (insert to "\n")
  550. (fill-region-as-paragraph address-start (point-max))
  551. (goto-char (point-max))
  552. (unless (bolp)
  553. (newline)))
  554. (newline))
  555. (if cc
  556. (let ((fill-prefix "\t")
  557. (address-start (progn (insert "CC: ") (point))))
  558. (insert cc "\n")
  559. (fill-region-as-paragraph address-start (point-max))
  560. (goto-char (point-max))
  561. (unless (bolp)
  562. (newline))))
  563. (if in-reply-to
  564. (let ((fill-prefix "\t")
  565. (fill-column 78)
  566. (address-start (point)))
  567. (insert "In-reply-to: " in-reply-to "\n")
  568. (fill-region-as-paragraph address-start (point-max))
  569. (goto-char (point-max))
  570. (unless (bolp)
  571. (newline))))
  572. (insert "Subject: " (or subject "") "\n")
  573. (if mail-default-headers
  574. (insert mail-default-headers))
  575. (if mail-default-reply-to
  576. (insert "Reply-to: " mail-default-reply-to "\n"))
  577. (if mail-self-blind
  578. (insert "BCC: " user-mail-address "\n"))
  579. (if mail-archive-file-name
  580. (insert "FCC: " mail-archive-file-name "\n"))
  581. (put-text-property (point)
  582. (progn
  583. (insert mail-header-separator "\n")
  584. (1- (point)))
  585. 'category 'mail-header-separator)
  586. ;; Insert the signature. But remember the beginning of the message.
  587. (if to (setq to (point)))
  588. (if mail-signature (mail-signature t))
  589. (goto-char (point-max))
  590. (or (bolp) (newline)))
  591. (if to (goto-char to))
  592. (or to subject in-reply-to
  593. (set-buffer-modified-p nil))
  594. (run-hooks 'mail-setup-hook))
  595. (defcustom mail-mode-hook nil
  596. "Hook run by Mail mode.
  597. When composing a mail, this runs immediately after creating, or
  598. switching to, the `*mail*' buffer. See also `mail-setup-hook'."
  599. :group 'sendmail
  600. :type 'hook
  601. :options '(footnote-mode))
  602. (defvar mail-mode-abbrev-table text-mode-abbrev-table)
  603. (defvar mail-encode-mml)
  604. ;;;###autoload
  605. (define-derived-mode mail-mode text-mode "Mail"
  606. "Major mode for editing mail to be sent.
  607. Like Text Mode but with these additional commands:
  608. \\[mail-send] mail-send (send the message)
  609. \\[mail-send-and-exit] mail-send-and-exit (send the message and exit)
  610. Here are commands that move to a header field (and create it if there isn't):
  611. \\[mail-to] move to To: \\[mail-subject] move to Subj:
  612. \\[mail-bcc] move to BCC: \\[mail-cc] move to CC:
  613. \\[mail-fcc] move to FCC: \\[mail-reply-to] move to Reply-To:
  614. \\[mail-mail-reply-to] move to Mail-Reply-To:
  615. \\[mail-mail-followup-to] move to Mail-Followup-To:
  616. \\[mail-text] move to message text.
  617. \\[mail-signature] mail-signature (insert `mail-signature-file' file).
  618. \\[mail-yank-original] mail-yank-original (insert current message, in Rmail).
  619. \\[mail-fill-yanked-message] mail-fill-yanked-message (fill what was yanked).
  620. \\[mail-insert-file] insert a text file into the message.
  621. \\[mail-add-attachment] attach to the message a file as binary attachment.
  622. Turning on Mail mode runs the normal hooks `text-mode-hook' and
  623. `mail-mode-hook' (in that order)."
  624. (make-local-variable 'mail-reply-action)
  625. (make-local-variable 'mail-send-actions)
  626. (make-local-variable 'mail-return-action)
  627. (make-local-variable 'mail-encode-mml)
  628. (setq mail-encode-mml nil)
  629. (setq buffer-offer-save t)
  630. (make-local-variable 'font-lock-defaults)
  631. (setq font-lock-defaults '(mail-font-lock-keywords t t))
  632. (make-local-variable 'paragraph-separate)
  633. (make-local-variable 'normal-auto-fill-function)
  634. (setq normal-auto-fill-function 'mail-mode-auto-fill)
  635. (make-local-variable 'fill-paragraph-function)
  636. (setq fill-paragraph-function 'mail-mode-fill-paragraph)
  637. ;; Allow using comment commands to add/remove quoting (this only does
  638. ;; anything if mail-yank-prefix is set to a non-nil value).
  639. (set (make-local-variable 'comment-start) mail-yank-prefix)
  640. (if mail-yank-prefix
  641. (set (make-local-variable 'comment-start-skip)
  642. (concat "^" (regexp-quote mail-yank-prefix) "[ \t]*")))
  643. (make-local-variable 'adaptive-fill-regexp)
  644. ;; Also update the paragraph-separate entry if you change this.
  645. (setq adaptive-fill-regexp
  646. (concat "[ \t]*[-[:alnum:]]+>+[ \t]*\\|"
  647. adaptive-fill-regexp))
  648. (make-local-variable 'adaptive-fill-first-line-regexp)
  649. (setq adaptive-fill-first-line-regexp
  650. (concat "[ \t]*[-[:alnum:]]*>+[ \t]*\\|"
  651. adaptive-fill-first-line-regexp))
  652. (add-hook 'completion-at-point-functions #'mail-completion-at-point-function
  653. nil 'local)
  654. ;; `-- ' precedes the signature. `-----' appears at the start of the
  655. ;; lines that delimit forwarded messages.
  656. ;; Lines containing just >= 3 dashes, perhaps after whitespace,
  657. ;; are also sometimes used and should be separators.
  658. (setq paragraph-separate
  659. (concat (regexp-quote mail-header-separator)
  660. ;; This is based on adaptive-fill-regexp (presumably
  661. ;; the idea is to allow navigation etc of cited paragraphs).
  662. "$\\|\t*[-–!|#%;>*·•‣⁃◦ ]+$"
  663. "\\|[ \t]*[-[:alnum:]]*>+[ \t]*$\\|[ \t]*$\\|"
  664. "--\\( \\|-+\\)$\\|"
  665. page-delimiter)))
  666. (defun mail-header-end ()
  667. "Return the buffer location of the end of headers, as a number."
  668. (save-restriction
  669. (widen)
  670. (save-excursion
  671. (rfc822-goto-eoh)
  672. (point))))
  673. (defun mail-text-start ()
  674. "Return the buffer location of the start of text, as a number."
  675. (save-restriction
  676. (widen)
  677. (save-excursion
  678. (rfc822-goto-eoh)
  679. (forward-line 1)
  680. (point))))
  681. (defun mail-sendmail-delimit-header ()
  682. "Set up whatever header delimiter convention sendmail will use.
  683. Concretely: replace the first blank line in the header with the separator."
  684. (rfc822-goto-eoh)
  685. (insert mail-header-separator)
  686. (point))
  687. (defun mail-sendmail-undelimit-header ()
  688. "Remove header separator to put the message in correct form for sendmail.
  689. Leave point at the start of the delimiter line."
  690. (rfc822-goto-eoh)
  691. (delete-region (point) (progn (end-of-line) (point))))
  692. (defun mail-mode-auto-fill ()
  693. "Carry out Auto Fill for Mail mode.
  694. If within the headers, this makes the new lines into continuation lines."
  695. (if (< (point) (mail-header-end))
  696. (let ((old-line-start (line-beginning-position)))
  697. (if (do-auto-fill)
  698. (save-excursion
  699. (beginning-of-line)
  700. (while (not (eq (point) old-line-start))
  701. ;; Use insert-before-markers in case we're inserting
  702. ;; before the saved value of point (which is common).
  703. (insert-before-markers " ")
  704. (forward-line -1))
  705. t)))
  706. (do-auto-fill)))
  707. (defun mail-mode-fill-paragraph (arg)
  708. ;; Do something special only if within the headers.
  709. (if (< (point) (mail-header-end))
  710. (let (beg end fieldname)
  711. (when (prog1 (re-search-backward "^[-a-zA-Z]+:" nil 'yes)
  712. (setq beg (point)))
  713. (setq fieldname
  714. (downcase (buffer-substring beg (1- (match-end 0))))))
  715. (forward-line 1)
  716. ;; Find continuation lines and get rid of their continuation markers.
  717. (while (looking-at "[ \t]")
  718. (delete-horizontal-space)
  719. (forward-line 1))
  720. (setq end (point-marker))
  721. (goto-char beg)
  722. ;; If this field contains addresses,
  723. ;; make sure we can fill after each address.
  724. (if (member fieldname
  725. '("to" "cc" "bcc" "from" "reply-to"
  726. "mail-reply-to" "mail-followup-to"
  727. "resent-to" "resent-cc" "resent-bcc"
  728. "resent-from" "resent-reply-to"))
  729. (while (search-forward "," end t)
  730. (or (looking-at "[ \t]")
  731. (insert " "))))
  732. (fill-region-as-paragraph beg end arg)
  733. ;; Mark all lines except the first as continuations.
  734. (goto-char beg)
  735. (forward-line 1)
  736. (while (< (point) end)
  737. (insert " ")
  738. (forward-line 1))
  739. (move-marker end nil)
  740. t)))
  741. ;; User-level commands for sending.
  742. (defun mail-send-and-exit (&optional arg)
  743. "Send message like `mail-send', then, if no errors, exit from mail buffer.
  744. Prefix arg means don't delete this window."
  745. (interactive "P")
  746. (mail-send)
  747. (mail-bury arg))
  748. (defun mail-dont-send (&optional arg)
  749. "Don't send the message you have been editing.
  750. Prefix arg means don't delete this window."
  751. (interactive "P")
  752. (mail-bury arg))
  753. (defun mail-bury (&optional arg)
  754. "Bury this mail buffer."
  755. (let ((newbuf (other-buffer (current-buffer)))
  756. (return-action mail-return-action)
  757. some-rmail)
  758. (bury-buffer (current-buffer))
  759. ;; If there is an Rmail buffer, return to it nicely
  760. ;; even if this message was not started by an Rmail command.
  761. (unless return-action
  762. (dolist (buffer (buffer-list))
  763. (if (and (eq (buffer-local-value 'major-mode buffer) 'rmail-mode)
  764. (null return-action)
  765. ;; Don't match message-viewer buffer.
  766. (not (string-match "\\` " (buffer-name buffer))))
  767. (setq return-action `(rmail-mail-return ,buffer)))))
  768. (if (and (null arg) return-action)
  769. (apply (car return-action) (cdr return-action))
  770. (switch-to-buffer newbuf))))
  771. (defcustom mail-send-hook nil
  772. "Hook run just before sending a message."
  773. :type 'hook
  774. :options '(flyspell-mode-off)
  775. :group 'sendmail)
  776. ;;;###autoload
  777. (defcustom mail-mailing-lists nil
  778. "List of mailing list addresses the user is subscribed to.
  779. The variable is used to trigger insertion of the \"Mail-Followup-To\"
  780. header when sending a message to a mailing list."
  781. :type '(repeat string)
  782. :group 'sendmail)
  783. (declare-function mml-to-mime "mml" ())
  784. (defun mail-send ()
  785. "Send the message in the current buffer.
  786. If `mail-interactive' is non-nil, wait for success indication
  787. or error messages, and inform user.
  788. Otherwise any failure is reported in a message back to
  789. the user from the mailer."
  790. (interactive)
  791. (if (if buffer-file-name
  792. (y-or-n-p "Send buffer contents as mail message? ")
  793. (or (buffer-modified-p)
  794. (y-or-n-p "Message already sent; resend? ")))
  795. (let ((inhibit-read-only t)
  796. (opoint (point))
  797. (ml (when mail-mailing-lists
  798. ;; The surrounding regexp assumes the use of
  799. ;; `mail-strip-quoted-names' on addresses before matching
  800. ;; Cannot deal with full RFC 822 freedom, but that is
  801. ;; unlikely to be problematic.
  802. (concat "\\(?:[[:space:];,]\\|\\`\\)"
  803. (regexp-opt mail-mailing-lists t)
  804. "\\(?:[[:space:];,]\\|\\'\\)"))))
  805. ;; If there are mailing lists defined
  806. (when ml
  807. (save-excursion
  808. (let* ((to (mail-fetch-field "to" nil t))
  809. (cc (mail-fetch-field "cc" nil t))
  810. (new-header-values ; To: and Cc:
  811. (mail-strip-quoted-names
  812. (concat to (when cc (concat ", " cc))))))
  813. ;; If message goes to known mailing list ...
  814. (when (string-match ml new-header-values)
  815. ;; Add Mail-Followup-To if none yet
  816. (unless (mail-fetch-field "mail-followup-to")
  817. (goto-char (mail-header-end))
  818. (insert "Mail-Followup-To: "
  819. (let ((l))
  820. (mapc
  821. ;; remove duplicates
  822. (lambda (e)
  823. (unless (member e l)
  824. (push e l)))
  825. (split-string new-header-values
  826. ",[[:space:]]+" t))
  827. (mapconcat 'identity l ", "))
  828. "\n"))
  829. ;; Add Mail-Reply-To if none yet
  830. (unless (mail-fetch-field "mail-reply-to")
  831. (goto-char (mail-header-end))
  832. (insert "Mail-Reply-To: "
  833. (or (mail-fetch-field "reply-to")
  834. user-mail-address)
  835. "\n"))))))
  836. (unless (memq mail-send-nonascii '(t mime))
  837. (goto-char (point-min))
  838. (skip-chars-forward "\0-\177")
  839. (or (= (point) (point-max))
  840. (if (eq mail-send-nonascii 'query)
  841. (or (y-or-n-p "Message contains non-ASCII characters; send anyway? ")
  842. (error "Aborted"))
  843. (error "Message contains non-ASCII characters"))))
  844. ;; Complain about any invalid line.
  845. (goto-char (point-min))
  846. (re-search-forward (regexp-quote mail-header-separator) (point-max) t)
  847. (let ((header-end (or (match-beginning 0) (point-max))))
  848. (goto-char (point-min))
  849. (while (< (point) header-end)
  850. (unless (looking-at "[ \t]\\|.*:\\|$")
  851. (push-mark opoint)
  852. (error "Invalid header line (maybe a continuation line lacks initial whitespace)"))
  853. (forward-line 1)))
  854. (goto-char opoint)
  855. (when mail-encode-mml
  856. (mml-to-mime)
  857. (setq mail-encode-mml nil))
  858. (run-hooks 'mail-send-hook)
  859. (message "Sending...")
  860. (funcall send-mail-function)
  861. ;; Now perform actions on successful sending.
  862. (while mail-send-actions
  863. (condition-case nil
  864. (apply (car (car mail-send-actions))
  865. (cdr (car mail-send-actions)))
  866. (error))
  867. (setq mail-send-actions (cdr mail-send-actions)))
  868. (message "Sending...done")
  869. ;; If buffer has no file, mark it as unmodified and delete auto-save.
  870. (if (not buffer-file-name)
  871. (progn
  872. (set-buffer-modified-p nil)
  873. (delete-auto-save-file-if-necessary t))))))
  874. (defun mail-envelope-from ()
  875. "Return the envelope mail address to use when sending mail.
  876. This function uses `mail-envelope-from'."
  877. (if (eq mail-envelope-from 'header)
  878. (nth 1 (mail-extract-address-components
  879. (mail-fetch-field "From")))
  880. mail-envelope-from))
  881. ;; This does the real work of sending a message via sendmail.
  882. ;; It is called via the variable send-mail-function.
  883. ;;;###autoload
  884. (defvar sendmail-coding-system nil
  885. "*Coding system for encoding the outgoing mail.
  886. This has higher priority than the default `buffer-file-coding-system'
  887. and `default-sendmail-coding-system',
  888. but lower priority than the local value of `buffer-file-coding-system'.
  889. See also the function `select-message-coding-system'.")
  890. ;;;###autoload
  891. (defvar default-sendmail-coding-system 'iso-latin-1
  892. "Default coding system for encoding the outgoing mail.
  893. This variable is used only when `sendmail-coding-system' is nil.
  894. This variable is set/changed by the command `set-language-environment'.
  895. User should not set this variable manually,
  896. instead use `sendmail-coding-system' to get a constant encoding
  897. of outgoing mails regardless of the current language environment.
  898. See also the function `select-message-coding-system'.")
  899. (defun mail-insert-from-field ()
  900. "Insert the \"From:\" field of a mail header.
  901. The style of the field is determined by the variable `mail-from-style'.
  902. This function does not perform RFC2047 encoding."
  903. (let* ((login user-mail-address)
  904. (fullname (user-full-name))
  905. (quote-fullname nil))
  906. (if (string-match "[^\0-\177]" fullname)
  907. (setq quote-fullname t))
  908. (cond ((null mail-from-style)
  909. (insert "From: " login "\n"))
  910. ;; This is deprecated.
  911. ((eq mail-from-style 'system-default)
  912. nil)
  913. ((or (eq mail-from-style 'angles)
  914. (and (not (eq mail-from-style 'parens))
  915. ;; Use angles if no quoting is needed, or if
  916. ;; parens would need quoting too.
  917. (or (not (string-match "[^- !#-'*+/-9=?A-Z^-~]" fullname))
  918. (let ((tmp (concat fullname nil)))
  919. (while (string-match "([^()]*)" tmp)
  920. (aset tmp (match-beginning 0) ?-)
  921. (aset tmp (1- (match-end 0)) ?-))
  922. (string-match "[\\()]" tmp)))))
  923. (insert "From: " fullname)
  924. (let ((fullname-start (+ (point-min) 6))
  925. (fullname-end (point-marker)))
  926. (goto-char fullname-start)
  927. ;; Look for a character that cannot appear unquoted
  928. ;; according to RFC 822.
  929. (if (or (re-search-forward "[^- !#-'*+/-9=?A-Z^-~]"
  930. fullname-end 1)
  931. quote-fullname)
  932. (progn
  933. ;; Quote fullname, escaping specials.
  934. (goto-char fullname-start)
  935. (insert "\"")
  936. (while (re-search-forward "[\"\\]"
  937. fullname-end 1)
  938. (replace-match "\\\\\\&" t))
  939. (insert "\""))))
  940. (insert " <" login ">\n"))
  941. ;; 'parens or default
  942. (t
  943. (insert "From: " login " (")
  944. (let ((fullname-start (point)))
  945. (if quote-fullname
  946. (insert "\""))
  947. (insert fullname)
  948. (if quote-fullname
  949. (insert "\""))
  950. (let ((fullname-end (point-marker)))
  951. (goto-char fullname-start)
  952. ;; RFC 822 says \ and nonmatching parentheses
  953. ;; must be escaped in comments.
  954. ;; Escape every instance of ()\ ...
  955. (while (re-search-forward "[()\\]" fullname-end 1)
  956. (replace-match "\\\\\\&" t))
  957. ;; ... then undo escaping of matching parentheses,
  958. ;; including matching nested parentheses.
  959. (goto-char fullname-start)
  960. (while (re-search-forward
  961. "\\(\\=\\|[^\\]\\(\\\\\\\\\\)*\\)\\\\(\\(\\([^\\]\\|\\\\\\\\\\)*\\)\\\\)"
  962. fullname-end 1)
  963. (replace-match "\\1(\\3)" t)
  964. (goto-char fullname-start))))
  965. (insert ")\n")))))
  966. (defun mail-encode-header (beg end)
  967. "Encode the mail header between BEG and END according to RFC2047.
  968. Return non-nil if and only if some part of the header is encoded."
  969. (save-restriction
  970. (narrow-to-region beg end)
  971. (let* ((selected (select-message-coding-system))
  972. (mm-coding-system-priorities
  973. (if (and selected (coding-system-get selected :mime-charset))
  974. (cons selected mm-coding-system-priorities)
  975. mm-coding-system-priorities))
  976. (tick (buffer-chars-modified-tick))
  977. ;; Many mailers, including Gnus, passes a message of which
  978. ;; the header is already encoded, so this is necessary to
  979. ;; prevent it from being encoded again.
  980. (rfc2047-encode-encoded-words nil))
  981. (rfc2047-encode-message-header)
  982. (= tick (buffer-chars-modified-tick)))))
  983. ;; Normally you will not need to modify these options unless you are
  984. ;; using some non-genuine substitute for sendmail which does not
  985. ;; implement each and every option that the original supports.
  986. ;; E.g., ssmtp does not support "-odb", so, if your site uses it,
  987. ;; you will need to modify `sendmail-error-reporting-non-interactive'
  988. ;; in your site-init.el.
  989. (defvar sendmail-error-reporting-interactive
  990. ;; These mean "report errors to terminal" and "deliver interactively"
  991. '("-oep" "-odi"))
  992. (defvar sendmail-error-reporting-non-interactive
  993. ;; These mean "report errors by mail" and "deliver in background".
  994. '("-oem" "-odb"))
  995. (defun sendmail-send-it ()
  996. "Send the current mail buffer using the Sendmail package.
  997. This is a suitable value for `send-mail-function'. It sends using the
  998. external program defined by `sendmail-program'."
  999. (require 'mail-utils)
  1000. (let ((errbuf (if mail-interactive
  1001. (generate-new-buffer " sendmail errors")
  1002. 0))
  1003. (tembuf (generate-new-buffer " sendmail temp"))
  1004. (multibyte enable-multibyte-characters)
  1005. (case-fold-search nil)
  1006. (selected-coding (select-message-coding-system))
  1007. resend-to-addresses
  1008. delimline
  1009. fcc-was-found
  1010. (mailbuf (current-buffer))
  1011. ;; Examine these variables now, so that
  1012. ;; local binding in the mail buffer will take effect.
  1013. (envelope-from
  1014. (and mail-specify-envelope-from
  1015. (or (mail-envelope-from) user-mail-address))))
  1016. (unwind-protect
  1017. (with-current-buffer tembuf
  1018. (erase-buffer)
  1019. (unless multibyte
  1020. (set-buffer-multibyte nil))
  1021. (insert-buffer-substring mailbuf)
  1022. (set-buffer-file-coding-system selected-coding)
  1023. (goto-char (point-max))
  1024. ;; require one newline at the end.
  1025. (or (= (preceding-char) ?\n)
  1026. (insert ?\n))
  1027. ;; Change header-delimiter to be what sendmail expects.
  1028. (goto-char (mail-header-end))
  1029. (delete-region (point) (progn (end-of-line) (point)))
  1030. (setq delimline (point-marker))
  1031. (sendmail-sync-aliases)
  1032. (if mail-aliases
  1033. (expand-mail-aliases (point-min) delimline))
  1034. (goto-char (point-min))
  1035. ;; Ignore any blank lines in the header
  1036. (while (and (re-search-forward "\n\n\n*" delimline t)
  1037. (< (point) delimline))
  1038. (replace-match "\n"))
  1039. (goto-char (point-min))
  1040. ;; Look for Resent- headers. They require sending
  1041. ;; the message specially.
  1042. (let ((case-fold-search t))
  1043. (goto-char (point-min))
  1044. (while (re-search-forward "^Resent-\\(to\\|cc\\|bcc\\):" delimline t)
  1045. ;; Put a list of such addresses in resend-to-addresses.
  1046. (setq resend-to-addresses
  1047. (save-restriction
  1048. (narrow-to-region (point)
  1049. (save-excursion
  1050. (forward-line 1)
  1051. (while (looking-at "^[ \t]")
  1052. (forward-line 1))
  1053. (point)))
  1054. (append (mail-parse-comma-list)
  1055. resend-to-addresses)))
  1056. ;; Delete Resent-BCC ourselves
  1057. (if (save-excursion (beginning-of-line)
  1058. (looking-at "resent-bcc"))
  1059. (delete-region (line-beginning-position)
  1060. (line-beginning-position 2))))
  1061. ;; Apparently this causes a duplicate Sender.
  1062. ;; ;; If the From is different than current user, insert Sender.
  1063. ;; (goto-char (point-min))
  1064. ;; (and (re-search-forward "^From:" delimline t)
  1065. ;; (progn
  1066. ;; (require 'mail-utils)
  1067. ;; (not (string-equal
  1068. ;; (mail-strip-quoted-names
  1069. ;; (save-restriction
  1070. ;; (narrow-to-region (point-min) delimline)
  1071. ;; (mail-fetch-field "From")))
  1072. ;; (user-login-name))))
  1073. ;; (progn
  1074. ;; (forward-line 1)
  1075. ;; (insert "Sender: " (user-login-name) "\n")))
  1076. ;; Don't send out a blank subject line
  1077. (goto-char (point-min))
  1078. (if (re-search-forward "^Subject:\\([ \t]*\n\\)+\\b" delimline t)
  1079. (replace-match "")
  1080. ;; This one matches a Subject just before the header delimiter.
  1081. (if (and (re-search-forward "^Subject:\\([ \t]*\n\\)+" delimline t)
  1082. (= (match-end 0) delimline))
  1083. (replace-match "")))
  1084. ;; Put the "From:" field in unless for some odd reason
  1085. ;; they put one in themselves.
  1086. (goto-char (point-min))
  1087. (if (not (re-search-forward "^From:" delimline t))
  1088. (mail-insert-from-field))
  1089. ;; Possibly add a MIME header for the current coding system
  1090. (let (charset where-content-type)
  1091. (goto-char (point-min))
  1092. (setq where-content-type
  1093. (re-search-forward "^Content-type:" delimline t))
  1094. (goto-char (point-min))
  1095. (and (eq mail-send-nonascii 'mime)
  1096. (not (re-search-forward "^MIME-version:" delimline t))
  1097. (progn (skip-chars-forward "\0-\177")
  1098. (/= (point) (point-max)))
  1099. selected-coding
  1100. (setq charset
  1101. (coding-system-get selected-coding :mime-charset))
  1102. (progn
  1103. (goto-char delimline)
  1104. (insert "MIME-version: 1.0\n"
  1105. "Content-type: text/plain; charset="
  1106. (symbol-name charset)
  1107. "\nContent-Transfer-Encoding: 8bit\n")
  1108. ;; The character set we will actually use
  1109. ;; should override any specified in the message itself.
  1110. (when where-content-type
  1111. (goto-char where-content-type)
  1112. (delete-region (point-at-bol)
  1113. (progn (forward-line 1) (point)))))))
  1114. ;; Insert an extra newline if we need it to work around
  1115. ;; Sun's bug that swallows newlines.
  1116. (goto-char (1+ delimline))
  1117. (if (eval mail-mailer-swallows-blank-line)
  1118. (newline))
  1119. ;; Find and handle any FCC fields.
  1120. (goto-char (point-min))
  1121. (if (re-search-forward "^FCC:" delimline t)
  1122. (progn
  1123. (setq fcc-was-found t)
  1124. (mail-do-fcc delimline)))
  1125. (if mail-interactive
  1126. (with-current-buffer errbuf
  1127. (erase-buffer))))
  1128. ;; Encode the header according to RFC2047.
  1129. (mail-encode-header (point-min) delimline)
  1130. (goto-char (point-min))
  1131. (if (let ((case-fold-search t))
  1132. (or resend-to-addresses
  1133. (re-search-forward "^To:\\|^cc:\\|^bcc:"
  1134. delimline t)))
  1135. (let* ((default-directory "/")
  1136. (coding-system-for-write selected-coding)
  1137. (args
  1138. (append (list (point-min) (point-max)
  1139. sendmail-program
  1140. nil errbuf nil "-oi")
  1141. (and envelope-from
  1142. (list "-f" envelope-from))
  1143. ;; ;; Don't say "from root" if running under su.
  1144. ;; (and (equal (user-real-login-name) "root")
  1145. ;; (list "-f" (user-login-name)))
  1146. (and mail-alias-file
  1147. (list (concat "-oA" mail-alias-file)))
  1148. (if mail-interactive
  1149. sendmail-error-reporting-interactive
  1150. sendmail-error-reporting-non-interactive)
  1151. ;; Get the addresses from the message
  1152. ;; unless this is a resend.
  1153. ;; We must not do that for a resend
  1154. ;; because we would find the original addresses.
  1155. ;; For a resend, include the specific addresses.
  1156. (or resend-to-addresses
  1157. '("-t")
  1158. )
  1159. (if mail-use-dsn
  1160. (list "-N" (mapconcat 'symbol-name
  1161. mail-use-dsn ",")))
  1162. )
  1163. )
  1164. (exit-value (apply 'call-process-region args)))
  1165. (cond ((or (null exit-value) (eq 0 exit-value)))
  1166. ((numberp exit-value)
  1167. (error "Sending...failed with exit value %d" exit-value))
  1168. ((stringp exit-value)
  1169. (error "Sending...terminated by signal: %s" exit-value))
  1170. (t
  1171. (error "SENDMAIL-SEND-IT -- fall through: %S" exit-value))))
  1172. (or fcc-was-found
  1173. (error "No recipients")))
  1174. (if mail-interactive
  1175. (with-current-buffer errbuf
  1176. (goto-char (point-min))
  1177. (while (re-search-forward "\n\n* *" nil t)
  1178. (replace-match "; "))
  1179. (if (not (zerop (buffer-size)))
  1180. (error "Sending...failed to %s"
  1181. (buffer-substring (point-min) (point-max)))))))
  1182. (kill-buffer tembuf)
  1183. (if (bufferp errbuf)
  1184. (kill-buffer errbuf)))))
  1185. (autoload 'rmail-output-to-rmail-buffer "rmailout")
  1186. (defun mail-do-fcc (header-end)
  1187. "Find and act on any FCC: headers in the current message before HEADER-END.
  1188. If a buffer is visiting the FCC file, append to it before
  1189. offering to save it, if it was modified initially. If this is an
  1190. Rmail buffer, update Rmail as needed. If there is no buffer,
  1191. just append to the file, in Babyl format if necessary."
  1192. (unless (markerp header-end)
  1193. (error "Value of `header-end' must be a marker"))
  1194. (let (fcc-list
  1195. (mailbuf (current-buffer))
  1196. (time (current-time)))
  1197. (save-excursion
  1198. (goto-char (point-min))
  1199. (let ((case-fold-search t))
  1200. (while (re-search-forward "^FCC:[ \t]*" header-end t)
  1201. (push (buffer-substring (point)
  1202. (progn
  1203. (end-of-line)
  1204. (skip-chars-backward " \t")
  1205. (point)))
  1206. fcc-list)
  1207. (delete-region (match-beginning 0)
  1208. (progn (forward-line 1) (point)))))
  1209. (with-temp-buffer
  1210. ;; This initial newline is not written out if we create a new
  1211. ;; file (see below).
  1212. (insert "\nFrom " (user-login-name) " " (current-time-string time) "\n")
  1213. ;; Insert the time zone before the year.
  1214. (forward-char -1)
  1215. (forward-word -1)
  1216. (require 'mail-utils)
  1217. (insert (mail-rfc822-time-zone time) " ")
  1218. (goto-char (point-max))
  1219. (insert-buffer-substring mailbuf)
  1220. ;; Make sure messages are separated.
  1221. (goto-char (point-max))
  1222. (insert ?\n)
  1223. (goto-char 2)
  1224. ;; ``Quote'' "^From " as ">From "
  1225. ;; (note that this isn't really quoting, as there is no requirement
  1226. ;; that "^[>]+From " be quoted in the same transparent way.)
  1227. (let ((case-fold-search nil))
  1228. (while (search-forward "\nFrom " nil t)
  1229. (forward-char -5)
  1230. (insert ?>)))
  1231. (dolist (fcc fcc-list)
  1232. (let* ((buffer (find-buffer-visiting fcc))
  1233. (curbuf (current-buffer))
  1234. dont-write-the-file
  1235. buffer-matches-file
  1236. (beg (point-min)) ; the initial blank line
  1237. (end (point-max))
  1238. ;; After the ^From line.
  1239. (beg2 (save-excursion (goto-char (point-min))
  1240. (forward-line 2) (point))))
  1241. (if buffer
  1242. ;; File is present in a buffer => append to that buffer.
  1243. (with-current-buffer buffer
  1244. (setq buffer-matches-file
  1245. (and (not (buffer-modified-p))
  1246. (verify-visited-file-modtime buffer)))
  1247. (let ((msg (bound-and-true-p rmail-current-message))
  1248. (buffer-read-only nil))
  1249. ;; If MSG is non-nil, buffer is in Rmail mode.
  1250. (if msg
  1251. (let ((buff (generate-new-buffer " *mail-do-fcc")))
  1252. (unwind-protect
  1253. (progn
  1254. (with-current-buffer buff
  1255. (insert-buffer-substring curbuf (1+ beg) end))
  1256. (rmail-output-to-rmail-buffer buff msg))
  1257. (kill-buffer buff)))
  1258. ;; Output file not in Rmail mode => just insert
  1259. ;; at the end.
  1260. (save-restriction
  1261. (widen)
  1262. (goto-char (point-max))
  1263. (insert-buffer-substring curbuf beg end)))
  1264. ;; Offer to save the buffer if it was modified
  1265. ;; before we started.
  1266. (unless buffer-matches-file
  1267. (if (y-or-n-p (format "Save file %s? " fcc))
  1268. (save-buffer))
  1269. (setq dont-write-the-file t)))))
  1270. ;; Append to the file directly, unless we've already taken
  1271. ;; care of it.
  1272. (unless dont-write-the-file
  1273. (if (and (file-exists-p fcc)
  1274. (mail-file-babyl-p fcc))
  1275. ;; If the file is a Babyl file, convert the message to
  1276. ;; Babyl format. Even though Rmail no longer uses
  1277. ;; Babyl, this code can remain for the time being, on
  1278. ;; the off-chance one FCCs to a Babyl file that has
  1279. ;; not yet been converted to mbox.
  1280. (let ((coding-system-for-write
  1281. (or rmail-file-coding-system 'emacs-mule)))
  1282. (with-temp-buffer
  1283. (insert "\C-l\n0, unseen,,\n*** EOOH ***\nDate: "
  1284. (mail-rfc822-date) "\n")
  1285. (insert-buffer-substring curbuf beg2 end)
  1286. (insert "\n\C-_")
  1287. (write-region (point-min) (point-max) fcc t)))
  1288. ;; Ensure there is a blank line between messages, but
  1289. ;; not at the very start of the file.
  1290. (write-region (if (file-exists-p fcc)
  1291. (point-min)
  1292. (1+ (point-min)))
  1293. (point-max) fcc t)))
  1294. (and buffer (not dont-write-the-file)
  1295. (with-current-buffer buffer
  1296. (set-visited-file-modtime)))))))))
  1297. (defun mail-sent-via ()
  1298. "Make a Sent-via header line from each To or CC header line."
  1299. (interactive)
  1300. (save-excursion
  1301. ;; put a marker at the end of the header
  1302. (let ((end (copy-marker (mail-header-end)))
  1303. (case-fold-search t))
  1304. (goto-char (point-min))
  1305. ;; search for the To: lines and make Sent-via: lines from them
  1306. ;; search for the next To: line
  1307. (while (re-search-forward "^\\(to\\|cc\\):" end t)
  1308. ;; Grab this line plus all its continuations, sans the `to:'.
  1309. (let ((to-line
  1310. (buffer-substring (point)
  1311. (progn
  1312. (if (re-search-forward "^[^ \t\n]" end t)
  1313. (backward-char 1)
  1314. (goto-char end))
  1315. (point)))))
  1316. ;; Insert a copy, with altered header field name.
  1317. (insert-before-markers "Sent-via:" to-line))))))
  1318. (make-obsolete 'mail-sent-via "nobody can remember what it is for." "24.1")
  1319. (defun mail-to ()
  1320. "Move point to end of To field, creating it if necessary."
  1321. (interactive)
  1322. (expand-abbrev)
  1323. (mail-position-on-field "To"))
  1324. (defun mail-subject ()
  1325. "Move point to end of Subject field, creating it if necessary."
  1326. (interactive)
  1327. (expand-abbrev)
  1328. (mail-position-on-field "Subject"))
  1329. (defun mail-cc ()
  1330. "Move point to end of CC field, creating it if necessary."
  1331. (interactive)
  1332. (expand-abbrev)
  1333. (or (mail-position-on-field "cc" t)
  1334. (progn (mail-position-on-field "to")
  1335. (insert "\nCC: "))))
  1336. (defun mail-bcc ()
  1337. "Move point to end of BCC field, creating it if necessary."
  1338. (interactive)
  1339. (expand-abbrev)
  1340. (or (mail-position-on-field "bcc" t)
  1341. (progn (mail-position-on-field "to")
  1342. (insert "\nBCC: "))))
  1343. (defun mail-fcc (folder)
  1344. "Add a new FCC field, with file name completion."
  1345. (interactive "FFolder carbon copy: ")
  1346. (expand-abbrev)
  1347. (or (mail-position-on-field "fcc" t) ;Put new field after exiting FCC.
  1348. (mail-position-on-field "to"))
  1349. (insert "\nFCC: " folder))
  1350. (defun mail-reply-to ()
  1351. "Move point to end of Reply-To field, creating it if necessary."
  1352. (interactive)
  1353. (expand-abbrev)
  1354. (mail-position-on-field "Reply-To"))
  1355. (defun mail-mail-reply-to ()
  1356. "Move point to end of Mail-Reply-To field, creating it if necessary."
  1357. (interactive)
  1358. (expand-abbrev)
  1359. (or (mail-position-on-field "mail-reply-to" t)
  1360. (progn (mail-position-on-field "to")
  1361. (insert "\nMail-Reply-To: "))))
  1362. (defun mail-mail-followup-to ()
  1363. "Move point to end of Mail-Followup-To field, creating it if necessary."
  1364. (interactive)
  1365. (expand-abbrev)
  1366. (or (mail-position-on-field "mail-followup-to" t)
  1367. (progn (mail-position-on-field "to")
  1368. (insert "\nMail-Followup-To: "))))
  1369. (defun mail-position-on-field (field &optional soft)
  1370. (let (end
  1371. (case-fold-search t))
  1372. (setq end (mail-header-end))
  1373. (goto-char (point-min))
  1374. (if (re-search-forward (concat "^" (regexp-quote field) ":") end t)
  1375. (progn
  1376. (re-search-forward "^[^ \t]" nil 'move)
  1377. (beginning-of-line)
  1378. (skip-chars-backward "\n")
  1379. t)
  1380. (or soft
  1381. (progn (goto-char end)
  1382. (insert field ": \n")
  1383. (skip-chars-backward "\n")))
  1384. nil)))
  1385. (defun mail-text ()
  1386. "Move point to beginning of text field."
  1387. (interactive)
  1388. (expand-abbrev)
  1389. (goto-char (mail-text-start)))
  1390. (defun mail-signature (&optional atpoint)
  1391. "Sign letter with signature.
  1392. If the variable `mail-signature' is a string, inserts it.
  1393. If it is t or nil, inserts the contents of the file `mail-signature-file'.
  1394. Otherwise, evals `mail-signature'.
  1395. Prefix argument ATPOINT means insert at point rather than the end."
  1396. (interactive "*P")
  1397. ;; Test for an unreadable file here, before we delete trailing
  1398. ;; whitespace, so that we don't modify the buffer needlessly.
  1399. (if (and (memq mail-signature '(t nil))
  1400. (not (file-readable-p mail-signature-file)))
  1401. (if (called-interactively-p 'interactive)
  1402. (message "The signature file `%s' could not be read"
  1403. mail-signature-file))
  1404. (save-excursion
  1405. (unless atpoint
  1406. (goto-char (point-max))
  1407. ;; Delete trailing whitespace and blank lines.
  1408. (skip-chars-backward " \t\n")
  1409. (end-of-line)
  1410. (delete-region (point) (point-max)))
  1411. (cond ((stringp mail-signature)
  1412. (insert mail-signature))
  1413. ((memq mail-signature '(t nil))
  1414. (insert "\n\n-- \n")
  1415. (insert-file-contents (expand-file-name mail-signature-file)))
  1416. (t
  1417. ;; FIXME add condition-case error handling?
  1418. (eval mail-signature))))))
  1419. (defun mail-fill-yanked-message (&optional justifyp)
  1420. "Fill the paragraphs of a message yanked into this one.
  1421. Numeric argument means justify as well."
  1422. (interactive "P")
  1423. (save-excursion
  1424. (goto-char (mail-text-start))
  1425. (fill-individual-paragraphs (point)
  1426. (point-max)
  1427. justifyp
  1428. mail-citation-prefix-regexp)))
  1429. (defun mail-indent-citation ()
  1430. "Modify text just inserted from a message to be cited.
  1431. The inserted text should be the region.
  1432. When this function returns, the region is again around the modified text.
  1433. Normally, indent each nonblank line `mail-indentation-spaces' spaces.
  1434. However, if `mail-yank-prefix' is non-nil, insert that prefix on each line."
  1435. (mail-yank-clear-headers (region-beginning) (region-end))
  1436. (if (null mail-yank-prefix)
  1437. (indent-rigidly (region-beginning) (region-end)
  1438. mail-indentation-spaces)
  1439. (save-excursion
  1440. (let ((end (set-marker (make-marker) (region-end))))
  1441. (goto-char (region-beginning))
  1442. (while (< (point) end)
  1443. (insert mail-yank-prefix)
  1444. (forward-line 1))))))
  1445. (defun mail-yank-original (arg)
  1446. "Insert the message being replied to, if any (in Rmail).
  1447. Puts point after the text and mark before.
  1448. Normally, indents each nonblank line ARG spaces (default 3).
  1449. However, if `mail-yank-prefix' is non-nil, insert that prefix on each line.
  1450. Just \\[universal-argument] as argument means don't indent, insert no prefix,
  1451. and don't delete any header fields."
  1452. (interactive "P")
  1453. (if mail-reply-action
  1454. (let ((start (point))
  1455. (original mail-reply-action)
  1456. (omark (mark t)))
  1457. (and (consp original) (eq (car original) 'insert-buffer)
  1458. (setq original (nth 1 original)))
  1459. (if (consp original)
  1460. (progn
  1461. ;; Call yank function, and set the mark if it doesn't.
  1462. (apply (car original) (cdr original))
  1463. (if (eq omark (mark t))
  1464. (push-mark (point))))
  1465. ;; If the original message is in another window in the same
  1466. ;; frame, delete that window to save space.
  1467. (delete-windows-on original t)
  1468. (with-no-warnings
  1469. ;; We really want this to set mark.
  1470. (insert-buffer original)
  1471. ;; If they yank the original text, the encoding of the
  1472. ;; original message is a better default than
  1473. ;; the default buffer-file-coding-system.
  1474. (and (coding-system-equal
  1475. (default-value 'buffer-file-coding-system)
  1476. buffer-file-coding-system)
  1477. (setq buffer-file-coding-system
  1478. (coding-system-change-text-conversion
  1479. buffer-file-coding-system
  1480. (coding-system-base
  1481. (with-current-buffer original
  1482. buffer-file-coding-system))))))
  1483. (set-text-properties (point) (mark t) nil))
  1484. (if (consp arg)
  1485. nil
  1486. (goto-char start)
  1487. (let ((mail-indentation-spaces (if arg (prefix-numeric-value arg)
  1488. mail-indentation-spaces))
  1489. ;; Avoid error in Transient Mark mode
  1490. ;; on account of mark's being inactive.
  1491. (mark-even-if-inactive t))
  1492. (cond (mail-citation-hook
  1493. ;; Bind mail-citation-header to the inserted
  1494. ;; message's header.
  1495. (let ((mail-citation-header
  1496. (buffer-substring-no-properties
  1497. start
  1498. (save-excursion
  1499. (save-restriction
  1500. (narrow-to-region start (point-max))
  1501. (goto-char start)
  1502. (rfc822-goto-eoh)
  1503. (point))))))
  1504. (run-hooks 'mail-citation-hook)))
  1505. (mail-yank-hooks
  1506. (run-hooks 'mail-yank-hooks))
  1507. (t
  1508. (mail-indent-citation)))))
  1509. ;; This is like exchange-point-and-mark, but doesn't activate the mark.
  1510. ;; It is cleaner to avoid activation, even though the command
  1511. ;; loop would deactivate the mark because we inserted text.
  1512. (goto-char (prog1 (mark t)
  1513. (set-marker (mark-marker) (point) (current-buffer))))
  1514. (if (not (eolp)) (insert ?\n)))))
  1515. (defun mail-yank-clear-headers (start end)
  1516. (if (< end start)
  1517. (let (temp)
  1518. (setq temp start start end end temp)))
  1519. (if mail-yank-ignored-headers
  1520. (save-excursion
  1521. (goto-char start)
  1522. (if (search-forward "\n\n" end t)
  1523. (save-restriction
  1524. (narrow-to-region start (point))
  1525. (goto-char start)
  1526. (while (let ((case-fold-search t))
  1527. (re-search-forward mail-yank-ignored-headers nil t))
  1528. (beginning-of-line)
  1529. (delete-region (point)
  1530. (progn (re-search-forward "\n[^ \t]")
  1531. (forward-char -1)
  1532. (point)))))))))
  1533. (defun mail-yank-region (arg)
  1534. "Insert the selected region from the message being replied to.
  1535. Puts point after the text and mark before.
  1536. Normally, indents each nonblank line ARG spaces (default 3).
  1537. However, if `mail-yank-prefix' is non-nil, insert that prefix on each line.
  1538. Just \\[universal-argument] as argument means don't indent, insert no prefix,
  1539. and don't delete any header fields."
  1540. (interactive "P")
  1541. (and (consp mail-reply-action)
  1542. (memq (car mail-reply-action)
  1543. '(rmail-yank-current-message insert-buffer))
  1544. (with-current-buffer (nth 1 mail-reply-action)
  1545. (or (mark t)
  1546. (error "No mark set: %S" (current-buffer))))
  1547. (let ((buffer (nth 1 mail-reply-action))
  1548. (start (point))
  1549. ;; Avoid error in Transient Mark mode
  1550. ;; on account of mark's being inactive.
  1551. (mark-even-if-inactive t))
  1552. ;; Insert the citation text.
  1553. (insert (with-current-buffer buffer
  1554. (buffer-substring-no-properties (point) (mark))))
  1555. (push-mark start)
  1556. ;; Indent or otherwise annotate the citation text.
  1557. (if (consp arg)
  1558. nil
  1559. (let ((mail-indentation-spaces (if arg (prefix-numeric-value arg)
  1560. mail-indentation-spaces)))
  1561. (if mail-citation-hook
  1562. ;; Bind mail-citation-header to the original message's header.
  1563. (let ((mail-citation-header
  1564. (with-current-buffer buffer
  1565. (buffer-substring-no-properties
  1566. (point-min)
  1567. (save-excursion
  1568. (goto-char (point-min))
  1569. (rfc822-goto-eoh)
  1570. (point))))))
  1571. (run-hooks 'mail-citation-hook))
  1572. (if mail-yank-hooks
  1573. (run-hooks 'mail-yank-hooks)
  1574. (mail-indent-citation))))))))
  1575. (defun mail-split-line ()
  1576. "Split current line, moving portion beyond point vertically down.
  1577. If the current line has `mail-yank-prefix', insert it on the new line."
  1578. (interactive "*")
  1579. (split-line mail-yank-prefix))
  1580. (defun mail-insert-file (&optional file)
  1581. "Insert a file at the end of the buffer, with separator lines around it."
  1582. (interactive "fAttach file: ")
  1583. (save-excursion
  1584. (goto-char (point-max))
  1585. (or (bolp) (newline))
  1586. (newline)
  1587. (let ((start (point))
  1588. middle)
  1589. (insert (format "===File %s===" file))
  1590. (insert-char ?= (max 0 (- 60 (current-column))))
  1591. (newline)
  1592. (setq middle (point))
  1593. (insert "============================================================\n")
  1594. (push-mark)
  1595. (goto-char middle)
  1596. (insert-file-contents file)
  1597. (or (bolp) (newline))
  1598. (goto-char start))))
  1599. (define-obsolete-function-alias 'mail-attach-file 'mail-insert-file "24.1")
  1600. (declare-function mml-attach-file "mml"
  1601. (file &optional type description disposition))
  1602. (declare-function mm-default-file-encoding "mm-encode" (file))
  1603. (defun mail-add-attachment (file)
  1604. "Add FILE as a MIME attachment to the end of the mail message being composed."
  1605. (interactive "fAttach file: ")
  1606. (mml-attach-file file
  1607. (or (mm-default-file-encoding file)
  1608. "application/octet-stream") nil)
  1609. (setq mail-encode-mml t))
  1610. ;; Put these commands last, to reduce chance of lossage from quitting
  1611. ;; in middle of loading the file.
  1612. ;;;###autoload
  1613. (defun mail (&optional noerase to subject in-reply-to cc replybuffer
  1614. actions return-action)
  1615. "Edit a message to be sent. Prefix arg means resume editing (don't erase).
  1616. When this function returns, the buffer `*mail*' is selected.
  1617. The value is t if the message was newly initialized; otherwise, nil.
  1618. Optionally, the signature file `mail-signature-file' can be inserted at the
  1619. end; see the variable `mail-signature'.
  1620. \\<mail-mode-map>
  1621. While editing message, type \\[mail-send-and-exit] to send the message and exit.
  1622. Various special commands starting with C-c are available in sendmail mode
  1623. to move to message header fields:
  1624. \\{mail-mode-map}
  1625. If `mail-self-blind' is non-nil, a BCC to yourself is inserted
  1626. when the message is initialized.
  1627. If `mail-default-reply-to' is non-nil, it should be an address (a string);
  1628. a Reply-to: field with that address is inserted.
  1629. If `mail-archive-file-name' is non-nil, an FCC field with that file name
  1630. is inserted.
  1631. The normal hook `mail-setup-hook' is run after the message is
  1632. initialized. It can add more default fields to the message.
  1633. The first argument, NOERASE, determines what to do when there is
  1634. an existing modified `*mail*' buffer. If NOERASE is nil, the
  1635. existing mail buffer is used, and the user is prompted whether to
  1636. keep the old contents or to erase them. If NOERASE has the value
  1637. `new', a new mail buffer will be created instead of using the old
  1638. one. Any other non-nil value means to always select the old
  1639. buffer without erasing the contents.
  1640. The second through fifth arguments,
  1641. TO, SUBJECT, IN-REPLY-TO and CC, specify if non-nil
  1642. the initial contents of those header fields.
  1643. These arguments should not have final newlines.
  1644. The sixth argument REPLYBUFFER is a buffer which contains an
  1645. original message being replied to, or else an action
  1646. of the form (FUNCTION . ARGS) which says how to insert the original.
  1647. Or it can be nil, if not replying to anything.
  1648. The seventh argument ACTIONS is a list of actions to take
  1649. if/when the message is sent. Each action looks like (FUNCTION . ARGS);
  1650. when the message is sent, we apply FUNCTION to ARGS.
  1651. This is how Rmail arranges to mark messages `answered'."
  1652. (interactive "P")
  1653. (if (eq noerase 'new)
  1654. (pop-to-buffer-same-window (generate-new-buffer "*mail*"))
  1655. (and noerase
  1656. (not (get-buffer "*mail*"))
  1657. (setq noerase nil))
  1658. (pop-to-buffer-same-window "*mail*"))
  1659. ;; Avoid danger that the auto-save file can't be written.
  1660. (let ((dir (expand-file-name
  1661. (file-name-as-directory mail-default-directory))))
  1662. (if (file-exists-p dir)
  1663. (setq default-directory dir)))
  1664. ;; Only call auto-save-mode if necessary, to avoid changing auto-save file.
  1665. (if (or (and auto-save-default (not buffer-auto-save-file-name))
  1666. (and (not auto-save-default) buffer-auto-save-file-name))
  1667. (auto-save-mode auto-save-default))
  1668. (mail-mode)
  1669. ;; Disconnect the buffer from its visited file
  1670. ;; (in case the user has actually visited a file *mail*).
  1671. ;; (set-visited-file-name nil)
  1672. (let (initialized)
  1673. (and (not (and noerase
  1674. (not (eq noerase 'new))))
  1675. (if buffer-file-name
  1676. (if (buffer-modified-p)
  1677. (when (y-or-n-p "Buffer has unsaved changes; reinitialize it and discard them? ")
  1678. (if (y-or-n-p "Disconnect buffer from visited file? ")
  1679. (set-visited-file-name nil))
  1680. t)
  1681. (when (y-or-n-p "Reinitialize buffer, and disconnect it from the visited file? ")
  1682. (set-visited-file-name nil)
  1683. t))
  1684. ;; A non-file-visiting buffer.
  1685. (if (buffer-modified-p)
  1686. (y-or-n-p "Unsent message being composed; erase it? ")
  1687. t))
  1688. (let ((inhibit-read-only t))
  1689. (erase-buffer)
  1690. (mail-setup to subject in-reply-to cc replybuffer actions
  1691. return-action)
  1692. (setq initialized t)))
  1693. (if (and buffer-auto-save-file-name
  1694. (file-exists-p buffer-auto-save-file-name))
  1695. (message "Auto save file for draft message exists; consider M-x mail-recover"))
  1696. initialized))
  1697. (declare-function dired-view-file "dired" ())
  1698. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  1699. (defun mail-recover-1 ()
  1700. "Pop up a list of auto-saved draft messages so you can recover one of them."
  1701. (interactive)
  1702. (let ((file-name (make-auto-save-file-name))
  1703. (ls-lisp-support-shell-wildcards t)
  1704. non-random-len wildcard)
  1705. ;; Remove the random part from the auto-save-file-name, and
  1706. ;; create a wildcard which matches possible candidates.
  1707. ;; Note: this knows that make-auto-save-file-name appends
  1708. ;; "#<RANDOM-STUFF>#" to the buffer name, where RANDOM-STUFF
  1709. ;; is the result of (make-temp-name "").
  1710. (setq non-random-len
  1711. (- (length file-name) (length (make-temp-name "")) 1))
  1712. (setq wildcard (concat (substring file-name 0 non-random-len) "*"))
  1713. (if (null (file-expand-wildcards wildcard))
  1714. (message "There are no auto-saved drafts to recover")
  1715. ;; Bind dired-trivial-filenames to t because all auto-save file
  1716. ;; names are normally ``trivial'', so Dired will set point after
  1717. ;; all the files, at buffer bottom. We want it on the first
  1718. ;; file instead.
  1719. ;; Require dired so that dired-trivial-filenames does not get
  1720. ;; unbound on exit from the let.
  1721. (require 'dired)
  1722. (let ((dired-trivial-filenames t))
  1723. (dired-other-window wildcard (concat dired-listing-switches " -t")))
  1724. (rename-buffer "*Auto-saved Drafts*" t)
  1725. (save-excursion
  1726. (goto-char (point-min))
  1727. (or (looking-at " Move to the draft file you want to recover,")
  1728. (let ((inhibit-read-only t))
  1729. ;; Each line starts with a space so that Font Lock mode
  1730. ;; won't highlight the first character.
  1731. (insert "\
  1732. Move to the draft file you want to recover, then type C-c C-c
  1733. to recover text of message whose composition was interrupted.
  1734. To browse text of a draft, type v on the draft file's line.
  1735. You can also delete some of these files;
  1736. type d on a line to mark that file for deletion.
  1737. List of possible auto-save files for recovery:
  1738. "))))
  1739. (use-local-map
  1740. (let ((map (make-sparse-keymap)))
  1741. (set-keymap-parent map (current-local-map))
  1742. map))
  1743. (define-key (current-local-map) "v"
  1744. (lambda ()
  1745. (interactive)
  1746. (let ((coding-system-for-read 'utf-8-emacs-unix))
  1747. (dired-view-file))))
  1748. (define-key (current-local-map) "\C-c\C-c"
  1749. (lambda ()
  1750. (interactive)
  1751. (let ((fname (dired-get-filename))
  1752. ;; Auto-saved files are written in the internal
  1753. ;; representation, so they should be read accordingly.
  1754. (coding-system-for-read 'utf-8-emacs-unix))
  1755. (switch-to-buffer-other-window "*mail*")
  1756. (let ((buffer-read-only nil))
  1757. (erase-buffer)
  1758. (insert-file-contents fname nil)
  1759. ;; insert-file-contents will set buffer-file-coding-system
  1760. ;; to utf-8-emacs, which is probably not what they want to
  1761. ;; use for sending the message. But we don't know what
  1762. ;; was its value before the buffer was killed or Emacs
  1763. ;; crashed. We therefore reset buffer-file-coding-system
  1764. ;; to the default value, so that either the default does
  1765. ;; TRT, or the user will get prompted for the right
  1766. ;; encoding when they send the message.
  1767. (setq buffer-file-coding-system
  1768. (default-value 'buffer-file-coding-system)))))))))
  1769. (declare-function dired-move-to-filename "dired" (&optional raise-error eol))
  1770. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  1771. (declare-function dired-view-file "dired" ())
  1772. (defun mail-recover ()
  1773. "Recover interrupted mail composition from auto-save files.
  1774. If the mail buffer has a current valid auto-save file,
  1775. the command recovers that file. Otherwise, it displays a
  1776. buffer showing the existing auto-saved draft messages;
  1777. you can move to one of them and type C-c C-c to recover that one."
  1778. (interactive)
  1779. ;; In case they invoke us from some random buffer...
  1780. (switch-to-buffer "*mail*")
  1781. ;; If *mail* didn't exist, set its directory, so that auto-saved
  1782. ;; drafts will be found.
  1783. (let ((dir (expand-file-name
  1784. (file-name-as-directory mail-default-directory))))
  1785. (if (file-exists-p dir)
  1786. (setq default-directory dir)))
  1787. (or (eq major-mode 'mail-mode)
  1788. (mail-mode))
  1789. (let ((file-name buffer-auto-save-file-name))
  1790. (cond ((and file-name (file-exists-p file-name))
  1791. (let ((dispbuf
  1792. ;; This used to invoke `ls' via call-process, but
  1793. ;; dired-noselect is more portable to systems where
  1794. ;; `ls' is not a standard program (it will use
  1795. ;; ls-lisp instead).
  1796. (dired-noselect file-name
  1797. (concat dired-listing-switches " -t"))))
  1798. (save-selected-window
  1799. (switch-to-buffer-other-window dispbuf)
  1800. (goto-char (point-min))
  1801. (forward-line 2)
  1802. (dired-move-to-filename)
  1803. (setq dispbuf (rename-buffer "*Directory*" t)))
  1804. (if (not (yes-or-no-p
  1805. (format "Recover mail draft from auto save file %s? "
  1806. file-name)))
  1807. (error "mail-recover cancelled")
  1808. (let ((buffer-read-only nil)
  1809. (buffer-coding buffer-file-coding-system)
  1810. ;; Auto-save files are written in internal
  1811. ;; representation of non-ASCII characters.
  1812. (coding-system-for-read 'utf-8-emacs-unix))
  1813. (erase-buffer)
  1814. (insert-file-contents file-name nil)
  1815. (setq buffer-file-coding-system buffer-coding)))))
  1816. (t (mail-recover-1)))))
  1817. ;;;###autoload
  1818. (defun mail-other-window (&optional noerase to subject in-reply-to cc replybuffer sendactions)
  1819. "Like `mail' command, but display mail buffer in another window."
  1820. (interactive "P")
  1821. (switch-to-buffer-other-window "*mail*")
  1822. (mail noerase to subject in-reply-to cc replybuffer sendactions))
  1823. ;;;###autoload
  1824. (defun mail-other-frame (&optional noerase to subject in-reply-to cc replybuffer sendactions)
  1825. "Like `mail' command, but display mail buffer in another frame."
  1826. (interactive "P")
  1827. (switch-to-buffer-other-frame "*mail*")
  1828. (mail noerase to subject in-reply-to cc replybuffer sendactions))
  1829. ;; Do not add anything but external entries on this page.
  1830. (provide 'sendmail)
  1831. ;; Local Variables:
  1832. ;; byte-compile-dynamic: t
  1833. ;; coding: utf-8
  1834. ;; End:
  1835. ;;; sendmail.el ends here