ox-koma-letter.el 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013
  1. ;;; ox-koma-letter.el --- KOMA Scrlttr2 Back-End for Org Export Engine
  2. ;; Copyright (C) 2007-2018 Free Software Foundation, Inc.
  3. ;; Author: Nicolas Goaziou <n.goaziou AT gmail DOT com>
  4. ;; Alan Schmitt <alan.schmitt AT polytechnique DOT org>
  5. ;; Viktor Rosenfeld <listuser36 AT gmail DOT com>
  6. ;; Rasmus Pank Roulund <emacs AT pank DOT eu>
  7. ;; Keywords: org, wp, tex
  8. ;; This program is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; This program is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;;
  20. ;; This library implements a KOMA Scrlttr2 back-end, derived from the
  21. ;; LaTeX one.
  22. ;;
  23. ;; Depending on the desired output format, three commands are provided
  24. ;; for export: `org-koma-letter-export-as-latex' (temporary buffer),
  25. ;; `org-koma-letter-export-to-latex' ("tex" file) and
  26. ;; `org-koma-letter-export-to-pdf' ("pdf" file).
  27. ;;
  28. ;; On top of buffer keywords supported by `latex' back-end (see
  29. ;; `org-latex-options-alist'), this back-end introduces the following
  30. ;; keywords:
  31. ;; - CLOSING: see `org-koma-letter-closing',
  32. ;; - FROM_ADDRESS: see `org-koma-letter-from-address',
  33. ;; - LCO: see `org-koma-letter-class-option-file',
  34. ;; - OPENING: see `org-koma-letter-opening',
  35. ;; - PHONE_NUMBER: see `org-koma-letter-phone-number',
  36. ;; - URL: see `org-koma-letter-url',
  37. ;; - FROM_LOGO: see `org-koma-letter-from-logo',
  38. ;; - SIGNATURE: see `org-koma-letter-signature',
  39. ;; - PLACE: see `org-koma-letter-place',
  40. ;; - LOCATION: see `org-koma-letter-location',
  41. ;; - TO_ADDRESS: If unspecified this is set to "\mbox{}".
  42. ;;
  43. ;; TO_ADDRESS, FROM_ADDRESS, LOCATION, CLOSING, and SIGNATURE can also
  44. ;; be specified using "special headings" with the special tags
  45. ;; specified in `org-koma-letter-special-tags-in-letter'. LaTeX line
  46. ;; breaks are not necessary for TO_ADDRESS, FROM_ADDRESS and LOCATION.
  47. ;; If both a headline and a keyword specify a to or from address the
  48. ;; value is determined in accordance with
  49. ;; `org-koma-letter-prefer-special-headings'.
  50. ;;
  51. ;; A number of OPTIONS settings can be set to change which contents is
  52. ;; exported.
  53. ;; - backaddress (see `org-koma-letter-use-backaddress')
  54. ;; - foldmarks (see `org-koma-letter-use-foldmarks')
  55. ;; - phone (see `org-koma-letter-use-phone')
  56. ;; - url (see `org-koma-letter-use-url')
  57. ;; - from-logo (see `org-koma-letter-use-from-logo')
  58. ;; - email (see `org-koma-letter-use-email')
  59. ;; - place (see `org-koma-letter-use-place')
  60. ;; - location (see `org-koma-letter-use-location')
  61. ;; - subject, a list of format options
  62. ;; (see `org-koma-letter-subject-format')
  63. ;; - after-closing-order, a list of the ordering of headings with
  64. ;; special tags after closing (see
  65. ;; `org-koma-letter-special-tags-after-closing')
  66. ;; - after-letter-order, as above, but after the end of the letter
  67. ;; (see `org-koma-letter-special-tags-after-letter').
  68. ;;
  69. ;; The following variables works differently from the main LaTeX class
  70. ;; - AUTHOR: Default to user-full-name but may be disabled.
  71. ;; (See also `org-koma-letter-author'),
  72. ;; - EMAIL: Same as AUTHOR. (see also `org-koma-letter-email'),
  73. ;;
  74. ;; FROM_LOGO uses LaTeX markup. FROM_LOGO provides the
  75. ;; "includegraphics" command to tell LaTeX where to find the logo.
  76. ;; This command needs to know the logo's directory and file name. The
  77. ;; directory can either be relative or absolute, just as you would
  78. ;; expect. LaTeX can use three file types for the logo: PDF, JPEG, or
  79. ;; PNG. The logo can either include or exclude its extension, which
  80. ;; might surprise you. When you exclude its extension, LaTeX will
  81. ;; search the directory for the "best" quality graphics format. For
  82. ;; example if it finds both logo.pdf and logo.png then it will
  83. ;; identify the PDF as "better", and include "logo.pdf". This can be
  84. ;; useful, for example, when you are mocking up a logo in the PNG
  85. ;; raster format and then switch over to the higher quality PDF vector
  86. ;; format. When you include the file extension then LaTeX will
  87. ;; include it without searching for higher quality file types.
  88. ;; Whatever file type you choose, it will probably require a few
  89. ;; design iterations to get the best looking logo size for your
  90. ;; letter. Finally, the directory and file name are specified
  91. ;; *without* quotes. Here are some examples with commentary, in the
  92. ;; location of your letter, with a logo named "logo", to get you
  93. ;; started:
  94. ;;
  95. ;; Logo in the same directory: \includegraphics{logo}
  96. ;; or a sub-directory: \includegraphics{logos/production/logo}
  97. ;;
  98. ;; Logos specified using absolute paths on Linux or Windows:
  99. ;;
  100. ;; \includegraphics{~/correspondence/logo}
  101. ;; \includegraphics{~/correspondence/logos/production/logo}
  102. ;; \includegraphics{c:/you/correspondence/logo}
  103. ;; \includegraphics{c:/you/correspondence/logos/production/logo}
  104. ;;
  105. ;; Logos in the same directory where the "better" quality PDF will
  106. ;; be chosen over the JPG:
  107. ;;
  108. ;; \includegraphics{logo.pdf}
  109. ;; \includegraphics{logo.png}
  110. ;;
  111. ;; Headlines are in general ignored. However, headlines with special
  112. ;; tags can be used for specified contents like postscript (ps),
  113. ;; carbon copy (cc), enclosures (encl) and code to be inserted after
  114. ;; \end{letter} (after_letter). Specials tags are defined in
  115. ;; `org-koma-letter-special-tags-after-closing' and
  116. ;; `org-koma-letter-special-tags-after-letter'. Currently members of
  117. ;; `org-koma-letter-special-tags-after-closing' used as macros and the
  118. ;; content of the headline is the argument.
  119. ;;
  120. ;; Headlines with to and from may also be used rather than the keyword
  121. ;; approach described above. If both a keyword and a headline with
  122. ;; information is present precedence is determined by
  123. ;; `org-koma-letter-prefer-special-headings'.
  124. ;;
  125. ;; You need an appropriate association in `org-latex-classes' in order
  126. ;; to use the KOMA Scrlttr2 class. By default, a sparse scrlttr2
  127. ;; class is provided: "default-koma-letter". You can also add you own
  128. ;; letter class. For instance:
  129. ;;
  130. ;; (add-to-list 'org-latex-classes
  131. ;; '("my-letter"
  132. ;; "\\documentclass\[%
  133. ;; DIV=14,
  134. ;; fontsize=12pt,
  135. ;; parskip=half,
  136. ;; subject=titled,
  137. ;; backaddress=false,
  138. ;; fromalign=left,
  139. ;; fromemail=true,
  140. ;; fromphone=true\]\{scrlttr2\}
  141. ;; \[DEFAULT-PACKAGES]
  142. ;; \[PACKAGES]
  143. ;; \[EXTRA]"))
  144. ;;
  145. ;; Then, in your Org document, be sure to require the proper class
  146. ;; with:
  147. ;;
  148. ;; #+LATEX_CLASS: my-letter
  149. ;;
  150. ;; Or by setting `org-koma-letter-default-class'.
  151. ;;
  152. ;; You may have to load (LaTeX) Babel as well, e.g., by adding
  153. ;; it to `org-latex-packages-alist',
  154. ;;
  155. ;; (add-to-list 'org-latex-packages-alist '("AUTO" "babel" nil))
  156. ;;; Code:
  157. (require 'cl-lib)
  158. (require 'ox-latex)
  159. ;; Install a default letter class.
  160. (unless (assoc "default-koma-letter" org-latex-classes)
  161. (add-to-list 'org-latex-classes
  162. '("default-koma-letter" "\\documentclass[11pt]{scrlttr2}")))
  163. ;;; User-Configurable Variables
  164. (defgroup org-export-koma-letter nil
  165. "Options for exporting to KOMA scrlttr2 class in LaTeX export."
  166. :tag "Org Koma-Letter"
  167. :group 'org-export)
  168. (defcustom org-koma-letter-class-option-file "NF"
  169. "Letter Class Option File.
  170. This option can also be set with the LCO keyword."
  171. :group 'org-export-koma-letter
  172. :type 'string)
  173. (defcustom org-koma-letter-author 'user-full-name
  174. "Sender's name.
  175. This variable defaults to calling the function `user-full-name'
  176. which just returns the current function `user-full-name'.
  177. Alternatively a string, nil or a function may be given.
  178. Functions must return a string.
  179. This option can also be set with the AUTHOR keyword."
  180. :group 'org-export-koma-letter
  181. :type '(radio (function-item user-full-name)
  182. (string)
  183. (function)
  184. (const :tag "Do not export author" nil)))
  185. (defcustom org-koma-letter-email 'org-koma-letter-email
  186. "Sender's email address.
  187. This variable defaults to the value `org-koma-letter-email' which
  188. returns `user-mail-address'. Alternatively a string, nil or
  189. a function may be given. Functions must return a string.
  190. This option can also be set with the EMAIL keyword."
  191. :group 'org-export-koma-letter
  192. :type '(radio (function-item org-koma-letter-email)
  193. (string)
  194. (function)
  195. (const :tag "Do not export email" nil)))
  196. (defcustom org-koma-letter-from-address ""
  197. "Sender's address, as a string.
  198. This option can also be set with one or more FROM_ADDRESS
  199. keywords."
  200. :group 'org-export-koma-letter
  201. :type 'string)
  202. (defcustom org-koma-letter-phone-number ""
  203. "Sender's phone number, as a string.
  204. This option can also be set with the PHONE_NUMBER keyword."
  205. :group 'org-export-koma-letter
  206. :type 'string)
  207. (defcustom org-koma-letter-url ""
  208. "Sender's URL, e. g., the URL of her homepage.
  209. This option can also be set with the URL keyword."
  210. :group 'org-export-koma-letter
  211. :type 'string
  212. :safe #'stringp)
  213. (defcustom org-koma-letter-from-logo ""
  214. "Commands for inserting the sender's logo, e. g., \\includegraphics{logo}.
  215. This option can also be set with the FROM_LOGO keyword."
  216. :group 'org-export-koma-letter
  217. :type 'string
  218. :safe #'stringp)
  219. (defcustom org-koma-letter-place ""
  220. "Place from which the letter is sent, as a string.
  221. This option can also be set with the PLACE keyword."
  222. :group 'org-export-koma-letter
  223. :type 'string)
  224. (defcustom org-koma-letter-location ""
  225. "Sender's extension field, as a string.
  226. This option can also be set with the LOCATION keyword.
  227. Moreover, when:
  228. (1) Either `org-koma-letter-prefer-special-headings' is non-nil
  229. or there is no LOCATION keyword or the LOCATION keyword is
  230. empty;
  231. (2) the letter contains a headline with the special
  232. tag \"location\";
  233. then the location will be set as the content of the location
  234. special heading.
  235. The location field is typically printed right of the address
  236. field (See Figure 4.9. in the English manual of 2015-10-03)."
  237. :group 'org-export-koma-letter
  238. :type 'string)
  239. (defcustom org-koma-letter-opening ""
  240. "Letter's opening, as a string.
  241. This option can also be set with the OPENING keyword. Moreover,
  242. when:
  243. (1) Either `org-koma-letter-prefer-special-headings' is non-nil
  244. or the CLOSING keyword is empty
  245. (2) `org-koma-letter-headline-is-opening-maybe' is non-nil;
  246. (3) the letter contains a headline without a special
  247. tag (e.g. \"to\" or \"ps\");
  248. then the opening will be implicitly set as the untagged headline title."
  249. :group 'org-export-koma-letter
  250. :type 'string)
  251. (defcustom org-koma-letter-closing ""
  252. "Letter's closing, as a string.
  253. This option can also be set with the CLOSING keyword. Moreover,
  254. when:
  255. (1) Either `org-koma-letter-prefer-special-headings' is non-nil
  256. or the CLOSING keyword is empty;
  257. (2) `org-koma-letter-headline-is-opening-maybe' is non-nil;
  258. (3) the letter contains a headline with the special
  259. tag \"closing\";
  260. then the opening will be set as the title of the closing special
  261. heading title."
  262. :group 'org-export-koma-letter
  263. :type 'string)
  264. (defcustom org-koma-letter-signature ""
  265. "Signature, as a string.
  266. This option can also be set with the SIGNATURE keyword.
  267. Moreover, when:
  268. (1) Either `org-koma-letter-prefer-special-headings' is non-nil
  269. or there is no CLOSING keyword or the CLOSING keyword is empty;
  270. (2) `org-koma-letter-headline-is-opening-maybe' is non-nil;
  271. (3) the letter contains a headline with the special
  272. tag \"closing\";
  273. then the signature will be set as the content of the
  274. closing special heading.
  275. Note if the content is empty the signature will not be set."
  276. :group 'org-export-koma-letter
  277. :type 'string)
  278. (defcustom org-koma-letter-prefer-special-headings nil
  279. "Non-nil means prefer headlines over keywords for TO and FROM.
  280. This option can also be set with the OPTIONS keyword, e.g.:
  281. \"special-headings:t\"."
  282. :group 'org-export-koma-letter
  283. :type 'boolean)
  284. (defcustom org-koma-letter-subject-format t
  285. "Non-nil means include the subject.
  286. Support formatting options.
  287. When t, insert a subject using default options. When nil, do not
  288. insert a subject at all. It can also be a list of symbols among
  289. the following ones:
  290. `afteropening' Subject after opening
  291. `beforeopening' Subject before opening
  292. `centered' Subject centered
  293. `left' Subject left-justified
  294. `right' Subject right-justified
  295. `titled' Add title/description to subject
  296. `underlined' Set subject underlined
  297. `untitled' Do not add title/description to subject
  298. Please refer to the KOMA-script manual (Table 4.16. in the
  299. English manual of 2012-07-22).
  300. This option can also be set with the OPTIONS keyword, e.g.:
  301. \"subject:(underlined centered)\"."
  302. :type
  303. '(choice
  304. (const :tag "No export" nil)
  305. (const :tag "Default options" t)
  306. (set :tag "Configure options"
  307. (const :tag "Subject after opening" afteropening)
  308. (const :tag "Subject before opening" beforeopening)
  309. (const :tag "Subject centered" centered)
  310. (const :tag "Subject left-justified" left)
  311. (const :tag "Subject right-justified" right)
  312. (const :tag "Add title or description to subject" underlined)
  313. (const :tag "Set subject underlined" titled)
  314. (const :tag "Do not add title or description to subject" untitled)))
  315. :group 'org-export-koma-letter)
  316. (defcustom org-koma-letter-use-backaddress nil
  317. "Non-nil prints return address in line above to address.
  318. This option can also be set with the OPTIONS keyword, e.g.:
  319. \"backaddress:t\"."
  320. :group 'org-export-koma-letter
  321. :type 'boolean)
  322. (defcustom org-koma-letter-use-foldmarks t
  323. "Configure appearance of folding marks.
  324. When t, activate default folding marks. When nil, do not insert
  325. folding marks at all. It can also be a list of symbols among the
  326. following ones:
  327. `B' Activate upper horizontal mark on left paper edge
  328. `b' Deactivate upper horizontal mark on left paper edge
  329. `H' Activate all horizontal marks on left paper edge
  330. `h' Deactivate all horizontal marks on left paper edge
  331. `L' Activate left vertical mark on upper paper edge
  332. `l' Deactivate left vertical mark on upper paper edge
  333. `M' Activate middle horizontal mark on left paper edge
  334. `m' Deactivate middle horizontal mark on left paper edge
  335. `P' Activate punch or center mark on left paper edge
  336. `p' Deactivate punch or center mark on left paper edge
  337. `T' Activate lower horizontal mark on left paper edge
  338. `t' Deactivate lower horizontal mark on left paper edge
  339. `V' Activate all vertical marks on upper paper edge
  340. `v' Deactivate all vertical marks on upper paper edge
  341. This option can also be set with the OPTIONS keyword, e.g.:
  342. \"foldmarks:(b l m t)\"."
  343. :group 'org-export-koma-letter
  344. :type '(choice
  345. (const :tag "Activate default folding marks" t)
  346. (const :tag "Deactivate folding marks" nil)
  347. (set
  348. :tag "Configure folding marks"
  349. (const :tag "Activate upper horizontal mark on left paper edge" B)
  350. (const :tag "Deactivate upper horizontal mark on left paper edge" b)
  351. (const :tag "Activate all horizontal marks on left paper edge" H)
  352. (const :tag "Deactivate all horizontal marks on left paper edge" h)
  353. (const :tag "Activate left vertical mark on upper paper edge" L)
  354. (const :tag "Deactivate left vertical mark on upper paper edge" l)
  355. (const :tag "Activate middle horizontal mark on left paper edge" M)
  356. (const :tag "Deactivate middle horizontal mark on left paper edge" m)
  357. (const :tag "Activate punch or center mark on left paper edge" P)
  358. (const :tag "Deactivate punch or center mark on left paper edge" p)
  359. (const :tag "Activate lower horizontal mark on left paper edge" T)
  360. (const :tag "Deactivate lower horizontal mark on left paper edge" t)
  361. (const :tag "Activate all vertical marks on upper paper edge" V)
  362. (const :tag "Deactivate all vertical marks on upper paper edge" v))))
  363. (defcustom org-koma-letter-use-phone nil
  364. "Non-nil prints sender's phone number.
  365. This option can also be set with the OPTIONS keyword, e.g.:
  366. \"phone:t\"."
  367. :group 'org-export-koma-letter
  368. :type 'boolean)
  369. (defcustom org-koma-letter-use-url nil
  370. "Non-nil prints sender's URL.
  371. This option can also be set with the OPTIONS keyword, e.g.:
  372. \"url:t\"."
  373. :group 'org-export-koma-letter
  374. :type 'boolean
  375. :safe #'booleanp)
  376. (defcustom org-koma-letter-use-from-logo nil
  377. "Non-nil prints sender's FROM_LOGO.
  378. This option can also be set with the OPTIONS keyword, e.g.:
  379. \"from-logo:t\"."
  380. :group 'org-export-koma-letter
  381. :type 'boolean
  382. :safe #'booleanp)
  383. (defcustom org-koma-letter-use-email nil
  384. "Non-nil prints sender's email address.
  385. This option can also be set with the OPTIONS keyword, e.g.:
  386. \"email:t\"."
  387. :group 'org-export-koma-letter
  388. :type 'boolean)
  389. (defcustom org-koma-letter-use-place t
  390. "Non-nil prints the letter's place next to the date.
  391. This option can also be set with the OPTIONS keyword, e.g.:
  392. \"place:nil\"."
  393. :group 'org-export-koma-letter
  394. :type 'boolean)
  395. (defcustom org-koma-letter-default-class "default-koma-letter"
  396. "Default class for `org-koma-letter'.
  397. The value must be a member of `org-latex-classes'."
  398. :group 'org-export-koma-letter
  399. :type 'string)
  400. (defcustom org-koma-letter-headline-is-opening-maybe t
  401. "Non-nil means a headline may be used as an opening and closing.
  402. See also `org-koma-letter-opening' and
  403. `org-koma-letter-closing'."
  404. :group 'org-export-koma-letter
  405. :type 'boolean)
  406. (defcustom org-koma-letter-prefer-subject nil
  407. "Non-nil means title should be interpreted as subject if subject is missing.
  408. This option can also be set with the OPTIONS keyword,
  409. e.g. \"title-subject:t\"."
  410. :group 'org-export-koma-letter
  411. :type 'boolean)
  412. (defconst org-koma-letter-special-tags-in-letter '(to from closing location)
  413. "Header tags related to the letter itself.")
  414. (defconst org-koma-letter-special-tags-after-closing '(after_closing ps encl cc)
  415. "Header tags to be inserted in the letter after closing.")
  416. (defconst org-koma-letter-special-tags-as-macro '(ps encl cc)
  417. "Header tags to be inserted as macros")
  418. (defconst org-koma-letter-special-tags-after-letter '(after_letter)
  419. "Header tags to be inserted after the letter.")
  420. (defvar org-koma-letter-special-contents nil
  421. "Holds special content temporarily.")
  422. (make-obsolete-variable 'org-koma-letter-use-title
  423. 'org-export-with-title
  424. "25.1" 'set)
  425. ;;; Define Back-End
  426. (org-export-define-derived-backend 'koma-letter 'latex
  427. :options-alist
  428. '((:latex-class "LATEX_CLASS" nil org-koma-letter-default-class t)
  429. (:lco "LCO" nil org-koma-letter-class-option-file)
  430. (:author "AUTHOR" nil (org-koma-letter--get-value org-koma-letter-author) parse)
  431. (:author-changed-in-buffer-p "AUTHOR" nil nil t)
  432. (:from-address "FROM_ADDRESS" nil org-koma-letter-from-address newline)
  433. (:phone-number "PHONE_NUMBER" nil org-koma-letter-phone-number)
  434. (:url "URL" nil org-koma-letter-url)
  435. (:from-logo "FROM_LOGO" nil org-koma-letter-from-logo)
  436. (:email "EMAIL" nil (org-koma-letter--get-value org-koma-letter-email) t)
  437. (:to-address "TO_ADDRESS" nil nil newline)
  438. (:place "PLACE" nil org-koma-letter-place)
  439. (:location "LOCATION" nil org-koma-letter-location)
  440. (:subject "SUBJECT" nil nil parse)
  441. (:opening "OPENING" nil org-koma-letter-opening parse)
  442. (:closing "CLOSING" nil org-koma-letter-closing parse)
  443. (:signature "SIGNATURE" nil org-koma-letter-signature newline)
  444. (:special-headings nil "special-headings" org-koma-letter-prefer-special-headings)
  445. (:special-tags-as-macro nil nil org-koma-letter-special-tags-as-macro)
  446. (:special-tags-in-letter nil nil org-koma-letter-special-tags-in-letter)
  447. (:special-tags-after-closing nil "after-closing-order"
  448. org-koma-letter-special-tags-after-closing)
  449. (:special-tags-after-letter nil "after-letter-order"
  450. org-koma-letter-special-tags-after-letter)
  451. (:with-backaddress nil "backaddress" org-koma-letter-use-backaddress)
  452. (:with-email nil "email" org-koma-letter-use-email)
  453. (:with-foldmarks nil "foldmarks" org-koma-letter-use-foldmarks)
  454. (:with-phone nil "phone" org-koma-letter-use-phone)
  455. (:with-url nil "url" org-koma-letter-use-url)
  456. (:with-from-logo nil "from-logo" org-koma-letter-use-from-logo)
  457. (:with-place nil "place" org-koma-letter-use-place)
  458. (:with-subject nil "subject" org-koma-letter-subject-format)
  459. (:with-title-as-subject nil "title-subject" org-koma-letter-prefer-subject)
  460. (:with-headline-opening nil nil org-koma-letter-headline-is-opening-maybe)
  461. ;; Special properties non-nil when a setting happened in buffer.
  462. ;; They are used to prioritize in-buffer settings over "lco"
  463. ;; files. See `org-koma-letter-template'.
  464. (:inbuffer-author "AUTHOR" nil 'koma-letter:empty)
  465. (:inbuffer-from "FROM" nil 'koma-letter:empty)
  466. (:inbuffer-email "EMAIL" nil 'koma-letter:empty)
  467. (:inbuffer-phone-number "PHONE_NUMBER" nil 'koma-letter:empty)
  468. (:inbuffer-url "URL" nil 'koma-letter:empty)
  469. (:inbuffer-from-logo "FROM_LOGO" nil 'koma-letter:empty)
  470. (:inbuffer-place "PLACE" nil 'koma-letter:empty)
  471. (:inbuffer-location "LOCATION" nil 'koma-letter:empty)
  472. (:inbuffer-signature "SIGNATURE" nil 'koma-letter:empty)
  473. (:inbuffer-with-backaddress nil "backaddress" 'koma-letter:empty)
  474. (:inbuffer-with-email nil "email" 'koma-letter:empty)
  475. (:inbuffer-with-foldmarks nil "foldmarks" 'koma-letter:empty)
  476. (:inbuffer-with-phone nil "phone" 'koma-letter:empty)
  477. (:inbuffer-with-url nil "url" 'koma-letter:empty)
  478. (:inbuffer-with-from-logo nil "from-logo" 'koma-letter:empty)
  479. (:inbuffer-with-place nil "place" 'koma-letter:empty))
  480. :translate-alist '((export-block . org-koma-letter-export-block)
  481. (export-snippet . org-koma-letter-export-snippet)
  482. (headline . org-koma-letter-headline)
  483. (keyword . org-koma-letter-keyword)
  484. (template . org-koma-letter-template))
  485. :menu-entry
  486. '(?k "Export with KOMA Scrlttr2"
  487. ((?L "As LaTeX buffer" org-koma-letter-export-as-latex)
  488. (?l "As LaTeX file" org-koma-letter-export-to-latex)
  489. (?p "As PDF file" org-koma-letter-export-to-pdf)
  490. (?o "As PDF file and open"
  491. (lambda (a s v b)
  492. (if a (org-koma-letter-export-to-pdf t s v b)
  493. (org-open-file (org-koma-letter-export-to-pdf nil s v b))))))))
  494. ;;; Helper functions
  495. (defun org-koma-letter-email ()
  496. "Return the current `user-mail-address'."
  497. user-mail-address)
  498. ;; The following is taken from/inspired by ox-grof.el
  499. ;; Thanks, Luis!
  500. (defun org-koma-letter--get-tagged-contents (key)
  501. "Get contents from a headline tagged with KEY.
  502. The contents is stored in `org-koma-letter-special-contents'."
  503. (let ((value (cdr (assoc-string (org-koma-letter--get-value key)
  504. org-koma-letter-special-contents))))
  505. (when value (org-string-nw-p (org-trim value)))))
  506. (defun org-koma-letter--get-value (value)
  507. "Turn value into a string whenever possible.
  508. Determines if VALUE is nil, a string, a function or a symbol and
  509. return a string or nil."
  510. (when value
  511. (cond ((stringp value) value)
  512. ((functionp value) (funcall value))
  513. ((symbolp value) (symbol-name value))
  514. (t value))))
  515. (defun org-koma-letter--special-contents-inline (keywords info)
  516. "Process KEYWORDS members of `org-koma-letter-special-contents'.
  517. KEYWORDS is a list of symbols. Return them as a string to be
  518. formatted.
  519. The function is used for inserting content of special headings
  520. such as the one tagged with PS."
  521. (mapconcat
  522. (lambda (keyword)
  523. (let* ((name (org-koma-letter--get-value keyword))
  524. (value (org-koma-letter--get-tagged-contents name))
  525. (macrop (memq keyword (plist-get info :special-tags-as-macro))))
  526. (cond ((not value) nil)
  527. (macrop (format "\\%s{%s}\n" name value))
  528. (t value))))
  529. keywords
  530. "\n"))
  531. (defun org-koma-letter--add-latex-newlines (string)
  532. "Replace regular newlines with LaTeX newlines (i.e. `\\\\')"
  533. (let ((str (org-trim string)))
  534. (when (org-string-nw-p str)
  535. (replace-regexp-in-string "\n" "\\\\\\\\\n" str))))
  536. ;;; Transcode Functions
  537. ;;;; Export Block
  538. (defun org-koma-letter-export-block (export-block contents info)
  539. "Transcode an EXPORT-BLOCK element into KOMA Scrlttr2 code.
  540. CONTENTS is nil. INFO is a plist used as a communication
  541. channel."
  542. (when (member (org-element-property :type export-block)
  543. '("KOMA-LETTER" "LATEX"))
  544. (org-remove-indentation (org-element-property :value export-block))))
  545. ;;;; Export Snippet
  546. (defun org-koma-letter-export-snippet (export-snippet contents info)
  547. "Transcode an EXPORT-SNIPPET object into KOMA Scrlttr2 code.
  548. CONTENTS is nil. INFO is a plist used as a communication
  549. channel."
  550. (when (memq (org-export-snippet-backend export-snippet) '(latex koma-letter))
  551. (org-element-property :value export-snippet)))
  552. ;;;; Keyword
  553. (defun org-koma-letter-keyword (keyword contents info)
  554. "Transcode a KEYWORD element into KOMA Scrlttr2 code.
  555. CONTENTS is nil. INFO is a plist used as a communication
  556. channel."
  557. (let ((key (org-element-property :key keyword))
  558. (value (org-element-property :value keyword)))
  559. ;; Handle specifically KOMA-LETTER keywords. Otherwise, fallback
  560. ;; to `latex' back-end.
  561. (if (equal key "KOMA-LETTER") value
  562. (org-export-with-backend 'latex keyword contents info))))
  563. ;; Headline
  564. (defun org-koma-letter-headline (headline contents info)
  565. "Transcode a HEADLINE element from Org to LaTeX.
  566. CONTENTS holds the contents of the headline. INFO is a plist
  567. holding contextual information.
  568. Note that if a headline is tagged with a tag from
  569. `org-koma-letter-special-tags' it will not be exported, but
  570. stored in `org-koma-letter-special-contents' and included at the
  571. appropriate place."
  572. (let ((special-tag (org-koma-letter--special-tag headline info)))
  573. (if (not special-tag)
  574. contents
  575. (push (cons special-tag contents) org-koma-letter-special-contents)
  576. "")))
  577. (defun org-koma-letter--special-tag (headline info)
  578. "Non-nil if HEADLINE is a special headline.
  579. INFO is a plist holding contextual information. Return first
  580. special tag headline."
  581. (let ((special-tags (append
  582. (plist-get info :special-tags-in-letter)
  583. (plist-get info :special-tags-after-closing)
  584. (plist-get info :special-tags-after-letter))))
  585. (cl-some (lambda (tag) (and (assoc-string tag special-tags) tag))
  586. (org-export-get-tags headline info))))
  587. (defun org-koma-letter--keyword-or-headline (plist-key pred info)
  588. "Return the correct version of opening or closing.
  589. PLIST-KEY should be a key in info, typically :opening
  590. or :closing. PRED is a predicate run on headline to determine
  591. which title to use which takes two arguments, a headline element
  592. and an info plist. INFO is a plist holding contextual
  593. information. Return the preferred candidate for the exported of
  594. PLIST-KEY."
  595. (let* ((keyword-candidate (plist-get info plist-key))
  596. (headline-candidate (when (and (plist-get info :with-headline-opening)
  597. (or (plist-get info :special-headings)
  598. (not keyword-candidate)))
  599. (org-element-map (plist-get info :parse-tree)
  600. 'headline
  601. (lambda (h)
  602. (and (funcall pred h info)
  603. (org-element-property :title h)))
  604. info t))))
  605. (org-export-data (or headline-candidate keyword-candidate "") info)))
  606. ;;;; Template
  607. (defun org-koma-letter-template (contents info)
  608. "Return complete document string after KOMA Scrlttr2 conversion.
  609. CONTENTS is the transcoded contents string. INFO is a plist
  610. holding export options."
  611. (concat
  612. ;; Time-stamp.
  613. (and (plist-get info :time-stamp-file)
  614. (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
  615. ;; LaTeX compiler
  616. (org-latex--insert-compiler info)
  617. ;; Document class and packages.
  618. (org-latex-make-preamble info)
  619. ;; Settings. They can come from three locations, in increasing
  620. ;; order of precedence: global variables, LCO files and in-buffer
  621. ;; settings. Thus, we first insert settings coming from global
  622. ;; variables, then we insert LCO files, and, eventually, we insert
  623. ;; settings coming from buffer keywords.
  624. (org-koma-letter--build-settings 'global info)
  625. (mapconcat (lambda (file) (format "\\LoadLetterOption{%s}\n" file))
  626. (split-string (or (plist-get info :lco) ""))
  627. "")
  628. (org-koma-letter--build-settings 'buffer info)
  629. ;; Date.
  630. (format "\\date{%s}\n" (org-export-data (org-export-get-date info) info))
  631. ;; Hyperref, document start, and subject and title.
  632. (let* ((with-subject (plist-get info :with-subject))
  633. (with-title (plist-get info :with-title))
  634. (title-as-subject (and with-subject
  635. (plist-get info :with-title-as-subject)))
  636. (subject* (org-string-nw-p
  637. (org-export-data (plist-get info :subject) info)))
  638. (title* (and with-title
  639. (org-string-nw-p
  640. (org-export-data (plist-get info :title) info))))
  641. (subject (cond ((not with-subject) nil)
  642. (title-as-subject (or subject* title*))
  643. (t subject*)))
  644. (title (cond ((not with-title) nil)
  645. (title-as-subject (and subject* title*))
  646. (t title*)))
  647. (hyperref-template (plist-get info :latex-hyperref-template))
  648. (spec (append (list (cons ?t (or title subject "")))
  649. (org-latex--format-spec info))))
  650. (concat
  651. (when (and with-subject (not (eq with-subject t)))
  652. (format "\\KOMAoption{subject}{%s}\n"
  653. (if (symbolp with-subject) with-subject
  654. (mapconcat #'symbol-name with-subject ","))))
  655. ;; Hyperref.
  656. (format-spec hyperref-template spec)
  657. ;; Document start.
  658. "\\begin{document}\n\n"
  659. ;; Subject and title.
  660. (when subject (format "\\setkomavar{subject}{%s}\n" subject))
  661. (when title (format "\\setkomavar{title}{%s}\n" title))
  662. (when (or (org-string-nw-p title) (org-string-nw-p subject)) "\n")))
  663. ;; Letter start.
  664. (let ((keyword-val (plist-get info :to-address))
  665. (heading-val (org-koma-letter--get-tagged-contents 'to)))
  666. (format "\\begin{letter}{%%\n%s}\n\n"
  667. (org-koma-letter--add-latex-newlines
  668. (or (if (plist-get info :special-headings)
  669. (or heading-val keyword-val)
  670. (or keyword-val heading-val))
  671. "\\mbox{}"))))
  672. ;; Opening.
  673. (format "\\opening{%s}\n\n"
  674. (org-koma-letter--keyword-or-headline
  675. :opening
  676. (lambda (h i)
  677. (not (org-koma-letter--special-tag h i)))
  678. info))
  679. ;; Letter body.
  680. contents
  681. ;; Closing.
  682. (format "\\closing{%s}\n"
  683. (org-koma-letter--keyword-or-headline
  684. :closing
  685. (lambda (h i)
  686. (let ((special-tag (org-koma-letter--special-tag h i)))
  687. (and special-tag
  688. (string= "closing" special-tag))))
  689. info))
  690. (org-koma-letter--special-contents-inline
  691. (plist-get info :special-tags-after-closing) info)
  692. ;; Letter end.
  693. "\n\\end{letter}\n"
  694. (org-koma-letter--special-contents-inline
  695. (plist-get info :special-tags-after-letter) info)
  696. ;; Document end.
  697. "\n\\end{document}"))
  698. (defun org-koma-letter--build-settings (scope info)
  699. "Build settings string according to type.
  700. SCOPE is either `global' or `buffer'. INFO is a plist used as
  701. a communication channel."
  702. (let* ((check-scope
  703. ;; Non-nil value when SETTING was defined in SCOPE.
  704. (lambda (setting)
  705. (let ((property (intern (format ":inbuffer-%s" setting))))
  706. (if (eq scope 'global)
  707. (eq (plist-get info property) 'koma-letter:empty)
  708. (not (eq (plist-get info property) 'koma-letter:empty))))))
  709. (heading-or-key-value
  710. (lambda (heading key &optional scoped)
  711. (let* ((heading-val
  712. (org-koma-letter--get-tagged-contents heading))
  713. (key-val (org-string-nw-p (plist-get info key)))
  714. (scopedp (funcall check-scope (or scoped heading))))
  715. (and (or (and key-val scopedp) heading-val)
  716. (not (and (eq scope 'global) heading-val))
  717. (if scopedp key-val heading-val))))))
  718. (concat
  719. ;; Name.
  720. (let ((author (plist-get info :author)))
  721. (and author
  722. (funcall check-scope 'author)
  723. (format "\\setkomavar{fromname}{%s}\n"
  724. (org-export-data author info))))
  725. ;; From.
  726. (let ((from (funcall heading-or-key-value 'from :from-address)))
  727. (and from
  728. (format "\\setkomavar{fromaddress}{%s}\n"
  729. (org-koma-letter--add-latex-newlines from))))
  730. ;; Email.
  731. (let ((email (plist-get info :email)))
  732. (and email
  733. (funcall check-scope 'email)
  734. (format "\\setkomavar{fromemail}{%s}\n" email)))
  735. (and (funcall check-scope 'with-email)
  736. (format "\\KOMAoption{fromemail}{%s}\n"
  737. (if (plist-get info :with-email) "true" "false")))
  738. ;; Phone number.
  739. (let ((phone-number (plist-get info :phone-number)))
  740. (and (org-string-nw-p phone-number)
  741. (funcall check-scope 'phone-number)
  742. (format "\\setkomavar{fromphone}{%s}\n" phone-number)))
  743. (and (funcall check-scope 'with-phone)
  744. (format "\\KOMAoption{fromphone}{%s}\n"
  745. (if (plist-get info :with-phone) "true" "false")))
  746. ;; URL
  747. (let ((url (plist-get info :url)))
  748. (and (org-string-nw-p url)
  749. (funcall check-scope 'url)
  750. (format "\\setkomavar{fromurl}{%s}\n" url)))
  751. (and (funcall check-scope 'with-url)
  752. (format "\\KOMAoption{fromurl}{%s}\n"
  753. (if (plist-get info :with-url) "true" "false")))
  754. ;; From Logo
  755. (let ((from-logo (plist-get info :from-logo)))
  756. (and (org-string-nw-p from-logo)
  757. (funcall check-scope 'from-logo)
  758. (format "\\setkomavar{fromlogo}{%s}\n" from-logo)))
  759. (and (funcall check-scope 'with-from-logo)
  760. (format "\\KOMAoption{fromlogo}{%s}\n"
  761. (if (plist-get info :with-from-logo) "true" "false")))
  762. ;; Signature.
  763. (let* ((heading-val
  764. (and (plist-get info :with-headline-opening)
  765. (pcase (org-koma-letter--get-tagged-contents 'closing)
  766. ((and (pred org-string-nw-p) closing) (org-trim closing))
  767. (_ nil))))
  768. (signature (org-string-nw-p (plist-get info :signature)))
  769. (signature-scope (funcall check-scope 'signature)))
  770. (and (or (and signature signature-scope)
  771. heading-val)
  772. (not (and (eq scope 'global) heading-val))
  773. (format "\\setkomavar{signature}{%s}\n"
  774. (if signature-scope signature heading-val))))
  775. ;; Back address.
  776. (and (funcall check-scope 'with-backaddress)
  777. (format "\\KOMAoption{backaddress}{%s}\n"
  778. (if (plist-get info :with-backaddress) "true" "false")))
  779. ;; Place.
  780. (let ((with-place-set (funcall check-scope 'with-place))
  781. (place-set (funcall check-scope 'place)))
  782. (and (or (and with-place-set place-set)
  783. (and (eq scope 'buffer) (or with-place-set place-set)))
  784. (format "\\setkomavar{place}{%s}\n"
  785. (if (plist-get info :with-place) (plist-get info :place)
  786. ""))))
  787. ;; Location.
  788. (let ((location (funcall heading-or-key-value 'location :location)))
  789. (and location
  790. (format "\\setkomavar{location}{%s}\n" location)))
  791. ;; Folding marks.
  792. (and (funcall check-scope 'with-foldmarks)
  793. (let ((foldmarks (plist-get info :with-foldmarks)))
  794. (cond ((consp foldmarks)
  795. (format "\\KOMAoptions{foldmarks=true,foldmarks=%s}\n"
  796. (mapconcat #'symbol-name foldmarks "")))
  797. (foldmarks "\\KOMAoptions{foldmarks=true}\n")
  798. (t "\\KOMAoptions{foldmarks=false}\n")))))))
  799. ;;; Commands
  800. ;;;###autoload
  801. (defun org-koma-letter-export-as-latex
  802. (&optional async subtreep visible-only body-only ext-plist)
  803. "Export current buffer as a KOMA Scrlttr2 letter.
  804. If narrowing is active in the current buffer, only export its
  805. narrowed part.
  806. If a region is active, export that region.
  807. A non-nil optional argument ASYNC means the process should happen
  808. asynchronously. The resulting buffer should be accessible
  809. through the `org-export-stack' interface.
  810. When optional argument SUBTREEP is non-nil, export the sub-tree
  811. at point, extracting information from the headline properties
  812. first.
  813. When optional argument VISIBLE-ONLY is non-nil, don't export
  814. contents of hidden elements.
  815. When optional argument BODY-ONLY is non-nil, only write code
  816. between \"\\begin{letter}\" and \"\\end{letter}\".
  817. EXT-PLIST, when provided, is a property list with external
  818. parameters overriding Org default settings, but still inferior to
  819. file-local settings.
  820. Export is done in a buffer named \"*Org KOMA-LETTER Export*\". It
  821. will be displayed if `org-export-show-temporary-export-buffer' is
  822. non-nil."
  823. (interactive)
  824. (let (org-koma-letter-special-contents)
  825. (org-export-to-buffer 'koma-letter "*Org KOMA-LETTER Export*"
  826. async subtreep visible-only body-only ext-plist
  827. (lambda () (LaTeX-mode)))))
  828. ;;;###autoload
  829. (defun org-koma-letter-export-to-latex
  830. (&optional async subtreep visible-only body-only ext-plist)
  831. "Export current buffer as a KOMA Scrlttr2 letter (tex).
  832. If narrowing is active in the current buffer, only export its
  833. narrowed part.
  834. If a region is active, export that region.
  835. A non-nil optional argument ASYNC means the process should happen
  836. asynchronously. The resulting file should be accessible through
  837. the `org-export-stack' interface.
  838. When optional argument SUBTREEP is non-nil, export the sub-tree
  839. at point, extracting information from the headline properties
  840. first.
  841. When optional argument VISIBLE-ONLY is non-nil, don't export
  842. contents of hidden elements.
  843. When optional argument BODY-ONLY is non-nil, only write code
  844. between \"\\begin{letter}\" and \"\\end{letter}\".
  845. EXT-PLIST, when provided, is a property list with external
  846. parameters overriding Org default settings, but still inferior to
  847. file-local settings.
  848. When optional argument PUB-DIR is set, use it as the publishing
  849. directory.
  850. Return output file's name."
  851. (interactive)
  852. (let ((outfile (org-export-output-file-name ".tex" subtreep))
  853. (org-koma-letter-special-contents))
  854. (org-export-to-file 'koma-letter outfile
  855. async subtreep visible-only body-only ext-plist)))
  856. ;;;###autoload
  857. (defun org-koma-letter-export-to-pdf
  858. (&optional async subtreep visible-only body-only ext-plist)
  859. "Export current buffer as a KOMA Scrlttr2 letter (pdf).
  860. If narrowing is active in the current buffer, only export its
  861. narrowed part.
  862. If a region is active, export that region.
  863. A non-nil optional argument ASYNC means the process should happen
  864. asynchronously. The resulting file should be accessible through
  865. the `org-export-stack' interface.
  866. When optional argument SUBTREEP is non-nil, export the sub-tree
  867. at point, extracting information from the headline properties
  868. first.
  869. When optional argument VISIBLE-ONLY is non-nil, don't export
  870. contents of hidden elements.
  871. When optional argument BODY-ONLY is non-nil, only write code
  872. between \"\\begin{letter}\" and \"\\end{letter}\".
  873. EXT-PLIST, when provided, is a property list with external
  874. parameters overriding Org default settings, but still inferior to
  875. file-local settings.
  876. Return PDF file's name."
  877. (interactive)
  878. (let ((file (org-export-output-file-name ".tex" subtreep))
  879. (org-koma-letter-special-contents))
  880. (org-export-to-file 'koma-letter file
  881. async subtreep visible-only body-only ext-plist
  882. (lambda (file) (org-latex-compile file)))))
  883. (provide 'ox-koma-letter)
  884. ;;; ox-koma-letter.el ends here