ox-koma-letter.el 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010
  1. ;;; ox-koma-letter.el --- KOMA Scrlttr2 Back-End for Org Export Engine
  2. ;; Copyright (C) 2007-2021 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. ;;; Define Back-End
  423. (org-export-define-derived-backend 'koma-letter 'latex
  424. :options-alist
  425. '((:latex-class "LATEX_CLASS" nil org-koma-letter-default-class t)
  426. (:lco "LCO" nil org-koma-letter-class-option-file)
  427. (:author "AUTHOR" nil (org-koma-letter--get-value org-koma-letter-author) parse)
  428. (:author-changed-in-buffer-p "AUTHOR" nil nil t)
  429. (:from-address "FROM_ADDRESS" nil org-koma-letter-from-address newline)
  430. (:phone-number "PHONE_NUMBER" nil org-koma-letter-phone-number)
  431. (:url "URL" nil org-koma-letter-url)
  432. (:from-logo "FROM_LOGO" nil org-koma-letter-from-logo)
  433. (:email "EMAIL" nil (org-koma-letter--get-value org-koma-letter-email) t)
  434. (:to-address "TO_ADDRESS" nil nil newline)
  435. (:place "PLACE" nil org-koma-letter-place)
  436. (:location "LOCATION" nil org-koma-letter-location)
  437. (:subject "SUBJECT" nil nil parse)
  438. (:opening "OPENING" nil org-koma-letter-opening parse)
  439. (:closing "CLOSING" nil org-koma-letter-closing parse)
  440. (:signature "SIGNATURE" nil org-koma-letter-signature newline)
  441. (:special-headings nil "special-headings" org-koma-letter-prefer-special-headings)
  442. (:special-tags-as-macro nil nil org-koma-letter-special-tags-as-macro)
  443. (:special-tags-in-letter nil nil org-koma-letter-special-tags-in-letter)
  444. (:special-tags-after-closing nil "after-closing-order"
  445. org-koma-letter-special-tags-after-closing)
  446. (:special-tags-after-letter nil "after-letter-order"
  447. org-koma-letter-special-tags-after-letter)
  448. (:with-backaddress nil "backaddress" org-koma-letter-use-backaddress)
  449. (:with-email nil "email" org-koma-letter-use-email)
  450. (:with-foldmarks nil "foldmarks" org-koma-letter-use-foldmarks)
  451. (:with-phone nil "phone" org-koma-letter-use-phone)
  452. (:with-url nil "url" org-koma-letter-use-url)
  453. (:with-from-logo nil "from-logo" org-koma-letter-use-from-logo)
  454. (:with-place nil "place" org-koma-letter-use-place)
  455. (:with-subject nil "subject" org-koma-letter-subject-format)
  456. (:with-title-as-subject nil "title-subject" org-koma-letter-prefer-subject)
  457. (:with-headline-opening nil nil org-koma-letter-headline-is-opening-maybe)
  458. ;; Special properties non-nil when a setting happened in buffer.
  459. ;; They are used to prioritize in-buffer settings over "lco"
  460. ;; files. See `org-koma-letter-template'.
  461. (:inbuffer-author "AUTHOR" nil 'koma-letter:empty)
  462. (:inbuffer-from "FROM" nil 'koma-letter:empty)
  463. (:inbuffer-email "EMAIL" nil 'koma-letter:empty)
  464. (:inbuffer-phone-number "PHONE_NUMBER" nil 'koma-letter:empty)
  465. (:inbuffer-url "URL" nil 'koma-letter:empty)
  466. (:inbuffer-from-logo "FROM_LOGO" nil 'koma-letter:empty)
  467. (:inbuffer-place "PLACE" nil 'koma-letter:empty)
  468. (:inbuffer-location "LOCATION" nil 'koma-letter:empty)
  469. (:inbuffer-signature "SIGNATURE" nil 'koma-letter:empty)
  470. (:inbuffer-with-backaddress nil "backaddress" 'koma-letter:empty)
  471. (:inbuffer-with-email nil "email" 'koma-letter:empty)
  472. (:inbuffer-with-foldmarks nil "foldmarks" 'koma-letter:empty)
  473. (:inbuffer-with-phone nil "phone" 'koma-letter:empty)
  474. (:inbuffer-with-url nil "url" 'koma-letter:empty)
  475. (:inbuffer-with-from-logo nil "from-logo" 'koma-letter:empty)
  476. (:inbuffer-with-place nil "place" 'koma-letter:empty))
  477. :translate-alist '((export-block . org-koma-letter-export-block)
  478. (export-snippet . org-koma-letter-export-snippet)
  479. (headline . org-koma-letter-headline)
  480. (keyword . org-koma-letter-keyword)
  481. (template . org-koma-letter-template))
  482. :menu-entry
  483. '(?k "Export with KOMA Scrlttr2"
  484. ((?L "As LaTeX buffer" org-koma-letter-export-as-latex)
  485. (?l "As LaTeX file" org-koma-letter-export-to-latex)
  486. (?p "As PDF file" org-koma-letter-export-to-pdf)
  487. (?o "As PDF file and open"
  488. (lambda (a s v b)
  489. (if a (org-koma-letter-export-to-pdf t s v b)
  490. (org-open-file (org-koma-letter-export-to-pdf nil s v b))))))))
  491. ;;; Helper functions
  492. (defun org-koma-letter-email ()
  493. "Return the current `user-mail-address'."
  494. user-mail-address)
  495. ;; The following is taken from/inspired by ox-grof.el
  496. ;; Thanks, Luis!
  497. (defun org-koma-letter--get-tagged-contents (key)
  498. "Get contents from a headline tagged with KEY.
  499. The contents is stored in `org-koma-letter-special-contents'."
  500. (let ((value (cdr (assoc-string (org-koma-letter--get-value key)
  501. org-koma-letter-special-contents))))
  502. (when value (org-string-nw-p (org-trim value)))))
  503. (defun org-koma-letter--get-value (value)
  504. "Turn value into a string whenever possible.
  505. Determines if VALUE is nil, a string, a function or a symbol and
  506. return a string or nil."
  507. (when value
  508. (cond ((stringp value) value)
  509. ((functionp value) (funcall value))
  510. ((symbolp value) (symbol-name value))
  511. (t value))))
  512. (defun org-koma-letter--special-contents-inline (keywords info)
  513. "Process KEYWORDS members of `org-koma-letter-special-contents'.
  514. KEYWORDS is a list of symbols. Return them as a string to be
  515. formatted.
  516. The function is used for inserting content of special headings
  517. such as the one tagged with PS."
  518. (mapconcat
  519. (lambda (keyword)
  520. (let* ((name (org-koma-letter--get-value keyword))
  521. (value (org-koma-letter--get-tagged-contents name))
  522. (macrop (memq keyword (plist-get info :special-tags-as-macro))))
  523. (cond ((not value) nil)
  524. (macrop (format "\\%s{%s}\n" name value))
  525. (t value))))
  526. keywords
  527. "\n"))
  528. (defun org-koma-letter--add-latex-newlines (string)
  529. "Replace regular newlines with LaTeX newlines (i.e. `\\\\')"
  530. (let ((str (org-trim string)))
  531. (when (org-string-nw-p str)
  532. (replace-regexp-in-string "\n" "\\\\\\\\\n" str))))
  533. ;;; Transcode Functions
  534. ;;;; Export Block
  535. (defun org-koma-letter-export-block (export-block contents info)
  536. "Transcode an EXPORT-BLOCK element into KOMA Scrlttr2 code.
  537. CONTENTS is nil. INFO is a plist used as a communication
  538. channel."
  539. (when (member (org-element-property :type export-block)
  540. '("KOMA-LETTER" "LATEX"))
  541. (org-remove-indentation (org-element-property :value export-block))))
  542. ;;;; Export Snippet
  543. (defun org-koma-letter-export-snippet (export-snippet contents info)
  544. "Transcode an EXPORT-SNIPPET object into KOMA Scrlttr2 code.
  545. CONTENTS is nil. INFO is a plist used as a communication
  546. channel."
  547. (when (memq (org-export-snippet-backend export-snippet) '(latex koma-letter))
  548. (org-element-property :value export-snippet)))
  549. ;;;; Keyword
  550. (defun org-koma-letter-keyword (keyword contents info)
  551. "Transcode a KEYWORD element into KOMA Scrlttr2 code.
  552. CONTENTS is nil. INFO is a plist used as a communication
  553. channel."
  554. (let ((key (org-element-property :key keyword))
  555. (value (org-element-property :value keyword)))
  556. ;; Handle specifically KOMA-LETTER keywords. Otherwise, fallback
  557. ;; to `latex' back-end.
  558. (if (equal key "KOMA-LETTER") value
  559. (org-export-with-backend 'latex keyword contents info))))
  560. ;; Headline
  561. (defun org-koma-letter-headline (headline contents info)
  562. "Transcode a HEADLINE element from Org to LaTeX.
  563. CONTENTS holds the contents of the headline. INFO is a plist
  564. holding contextual information.
  565. Note that if a headline is tagged with a tag from
  566. `org-koma-letter-special-tags' it will not be exported, but
  567. stored in `org-koma-letter-special-contents' and included at the
  568. appropriate place."
  569. (let ((special-tag (org-koma-letter--special-tag headline info)))
  570. (if (not special-tag)
  571. contents
  572. (push (cons special-tag contents) org-koma-letter-special-contents)
  573. "")))
  574. (defun org-koma-letter--special-tag (headline info)
  575. "Non-nil if HEADLINE is a special headline.
  576. INFO is a plist holding contextual information. Return first
  577. special tag headline."
  578. (let ((special-tags (append
  579. (plist-get info :special-tags-in-letter)
  580. (plist-get info :special-tags-after-closing)
  581. (plist-get info :special-tags-after-letter))))
  582. (cl-some (lambda (tag) (and (assoc-string tag special-tags) tag))
  583. (org-export-get-tags headline info))))
  584. (defun org-koma-letter--keyword-or-headline (plist-key pred info)
  585. "Return the correct version of opening or closing.
  586. PLIST-KEY should be a key in info, typically :opening
  587. or :closing. PRED is a predicate run on headline to determine
  588. which title to use which takes two arguments, a headline element
  589. and an info plist. INFO is a plist holding contextual
  590. information. Return the preferred candidate for the exported of
  591. PLIST-KEY."
  592. (let* ((keyword-candidate (plist-get info plist-key))
  593. (headline-candidate (when (and (plist-get info :with-headline-opening)
  594. (or (plist-get info :special-headings)
  595. (not keyword-candidate)))
  596. (org-element-map (plist-get info :parse-tree)
  597. 'headline
  598. (lambda (h)
  599. (and (funcall pred h info)
  600. (org-element-property :title h)))
  601. info t))))
  602. (org-export-data (or headline-candidate keyword-candidate "") info)))
  603. ;;;; Template
  604. (defun org-koma-letter-template (contents info)
  605. "Return complete document string after KOMA Scrlttr2 conversion.
  606. CONTENTS is the transcoded contents string. INFO is a plist
  607. holding export options."
  608. (concat
  609. ;; Time-stamp.
  610. (and (plist-get info :time-stamp-file)
  611. (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
  612. ;; LaTeX compiler
  613. (org-latex--insert-compiler info)
  614. ;; Document class and packages.
  615. (org-latex-make-preamble info)
  616. ;; Settings. They can come from three locations, in increasing
  617. ;; order of precedence: global variables, LCO files and in-buffer
  618. ;; settings. Thus, we first insert settings coming from global
  619. ;; variables, then we insert LCO files, and, eventually, we insert
  620. ;; settings coming from buffer keywords.
  621. (org-koma-letter--build-settings 'global info)
  622. (mapconcat (lambda (file) (format "\\LoadLetterOption{%s}\n" file))
  623. (split-string (or (plist-get info :lco) ""))
  624. "")
  625. (org-koma-letter--build-settings 'buffer info)
  626. ;; Date.
  627. (format "\\date{%s}\n" (org-export-data (org-export-get-date info) info))
  628. ;; Hyperref, document start, and subject and title.
  629. (let* ((with-subject (plist-get info :with-subject))
  630. (with-title (plist-get info :with-title))
  631. (title-as-subject (and with-subject
  632. (plist-get info :with-title-as-subject)))
  633. (subject* (org-string-nw-p
  634. (org-export-data (plist-get info :subject) info)))
  635. (title* (and with-title
  636. (org-string-nw-p
  637. (org-export-data (plist-get info :title) info))))
  638. (subject (cond ((not with-subject) nil)
  639. (title-as-subject (or subject* title*))
  640. (t subject*)))
  641. (title (cond ((not with-title) nil)
  642. (title-as-subject (and subject* title*))
  643. (t title*)))
  644. (hyperref-template (plist-get info :latex-hyperref-template))
  645. (spec (append (list (cons ?t (or title subject "")))
  646. (org-latex--format-spec info))))
  647. (concat
  648. (when (and with-subject (not (eq with-subject t)))
  649. (format "\\KOMAoption{subject}{%s}\n"
  650. (if (symbolp with-subject) with-subject
  651. (mapconcat #'symbol-name with-subject ","))))
  652. ;; Hyperref.
  653. (and (stringp hyperref-template)
  654. (format-spec hyperref-template spec))
  655. ;; Document start.
  656. "\\begin{document}\n\n"
  657. ;; Subject and title.
  658. (when subject (format "\\setkomavar{subject}{%s}\n" subject))
  659. (when title (format "\\setkomavar{title}{%s}\n" title))
  660. (when (or (org-string-nw-p title) (org-string-nw-p subject)) "\n")))
  661. ;; Letter start.
  662. (let ((keyword-val (plist-get info :to-address))
  663. (heading-val (org-koma-letter--get-tagged-contents 'to)))
  664. (format "\\begin{letter}{%%\n%s}\n\n"
  665. (org-koma-letter--add-latex-newlines
  666. (or (if (plist-get info :special-headings)
  667. (or heading-val keyword-val)
  668. (or keyword-val heading-val))
  669. "\\mbox{}"))))
  670. ;; Opening.
  671. (format "\\opening{%s}\n\n"
  672. (org-koma-letter--keyword-or-headline
  673. :opening
  674. (lambda (h i)
  675. (not (org-koma-letter--special-tag h i)))
  676. info))
  677. ;; Letter body.
  678. contents
  679. ;; Closing.
  680. (format "\\closing{%s}\n"
  681. (org-koma-letter--keyword-or-headline
  682. :closing
  683. (lambda (h i)
  684. (let ((special-tag (org-koma-letter--special-tag h i)))
  685. (and special-tag
  686. (string= "closing" special-tag))))
  687. info))
  688. (org-koma-letter--special-contents-inline
  689. (plist-get info :special-tags-after-closing) info)
  690. ;; Letter end.
  691. "\n\\end{letter}\n"
  692. (org-koma-letter--special-contents-inline
  693. (plist-get info :special-tags-after-letter) info)
  694. ;; Document end.
  695. "\n\\end{document}"))
  696. (defun org-koma-letter--build-settings (scope info)
  697. "Build settings string according to type.
  698. SCOPE is either `global' or `buffer'. INFO is a plist used as
  699. a communication channel."
  700. (let* ((check-scope
  701. ;; Non-nil value when SETTING was defined in SCOPE.
  702. (lambda (setting)
  703. (let ((property (intern (format ":inbuffer-%s" setting))))
  704. (if (eq scope 'global)
  705. (eq (plist-get info property) 'koma-letter:empty)
  706. (not (eq (plist-get info property) 'koma-letter:empty))))))
  707. (heading-or-key-value
  708. (lambda (heading key &optional scoped)
  709. (let* ((heading-val
  710. (org-koma-letter--get-tagged-contents heading))
  711. (key-val (org-string-nw-p (plist-get info key)))
  712. (scopedp (funcall check-scope (or scoped heading))))
  713. (and (or (and key-val scopedp) heading-val)
  714. (not (and (eq scope 'global) heading-val))
  715. (if scopedp key-val heading-val))))))
  716. (concat
  717. ;; Name.
  718. (let ((author (plist-get info :author)))
  719. (and author
  720. (funcall check-scope 'author)
  721. (format "\\setkomavar{fromname}{%s}\n"
  722. (org-export-data author info))))
  723. ;; From.
  724. (let ((from (funcall heading-or-key-value 'from :from-address)))
  725. (and from
  726. (format "\\setkomavar{fromaddress}{%s}\n"
  727. (org-koma-letter--add-latex-newlines from))))
  728. ;; Email.
  729. (let ((email (plist-get info :email)))
  730. (and email
  731. (funcall check-scope 'email)
  732. (format "\\setkomavar{fromemail}{%s}\n" email)))
  733. (and (funcall check-scope 'with-email)
  734. (format "\\KOMAoption{fromemail}{%s}\n"
  735. (if (plist-get info :with-email) "true" "false")))
  736. ;; Phone number.
  737. (let ((phone-number (plist-get info :phone-number)))
  738. (and (org-string-nw-p phone-number)
  739. (funcall check-scope 'phone-number)
  740. (format "\\setkomavar{fromphone}{%s}\n" phone-number)))
  741. (and (funcall check-scope 'with-phone)
  742. (format "\\KOMAoption{fromphone}{%s}\n"
  743. (if (plist-get info :with-phone) "true" "false")))
  744. ;; URL
  745. (let ((url (plist-get info :url)))
  746. (and (org-string-nw-p url)
  747. (funcall check-scope 'url)
  748. (format "\\setkomavar{fromurl}{%s}\n" url)))
  749. (and (funcall check-scope 'with-url)
  750. (format "\\KOMAoption{fromurl}{%s}\n"
  751. (if (plist-get info :with-url) "true" "false")))
  752. ;; From Logo
  753. (let ((from-logo (plist-get info :from-logo)))
  754. (and (org-string-nw-p from-logo)
  755. (funcall check-scope 'from-logo)
  756. (format "\\setkomavar{fromlogo}{%s}\n" from-logo)))
  757. (and (funcall check-scope 'with-from-logo)
  758. (format "\\KOMAoption{fromlogo}{%s}\n"
  759. (if (plist-get info :with-from-logo) "true" "false")))
  760. ;; Signature.
  761. (let* ((heading-val
  762. (and (plist-get info :with-headline-opening)
  763. (pcase (org-koma-letter--get-tagged-contents 'closing)
  764. ((and (pred org-string-nw-p) closing) (org-trim closing))
  765. (_ nil))))
  766. (signature (org-string-nw-p (plist-get info :signature)))
  767. (signature-scope (funcall check-scope 'signature)))
  768. (and (or (and signature signature-scope)
  769. heading-val)
  770. (not (and (eq scope 'global) heading-val))
  771. (format "\\setkomavar{signature}{%s}\n"
  772. (if signature-scope signature heading-val))))
  773. ;; Back address.
  774. (and (funcall check-scope 'with-backaddress)
  775. (format "\\KOMAoption{backaddress}{%s}\n"
  776. (if (plist-get info :with-backaddress) "true" "false")))
  777. ;; Place.
  778. (let ((with-place-set (funcall check-scope 'with-place))
  779. (place-set (funcall check-scope 'place)))
  780. (and (or (and with-place-set place-set)
  781. (and (eq scope 'buffer) (or with-place-set place-set)))
  782. (format "\\setkomavar{place}{%s}\n"
  783. (if (plist-get info :with-place) (plist-get info :place)
  784. ""))))
  785. ;; Location.
  786. (let ((location (funcall heading-or-key-value 'location :location)))
  787. (and location
  788. (format "\\setkomavar{location}{%s}\n" location)))
  789. ;; Folding marks.
  790. (and (funcall check-scope 'with-foldmarks)
  791. (let ((foldmarks (plist-get info :with-foldmarks)))
  792. (cond ((consp foldmarks)
  793. (format "\\KOMAoptions{foldmarks=true,foldmarks=%s}\n"
  794. (mapconcat #'symbol-name foldmarks "")))
  795. (foldmarks "\\KOMAoptions{foldmarks=true}\n")
  796. (t "\\KOMAoptions{foldmarks=false}\n")))))))
  797. ;;; Commands
  798. ;;;###autoload
  799. (defun org-koma-letter-export-as-latex
  800. (&optional async subtreep visible-only body-only ext-plist)
  801. "Export current buffer as a KOMA Scrlttr2 letter.
  802. If narrowing is active in the current buffer, only export its
  803. narrowed part.
  804. If a region is active, export that region.
  805. A non-nil optional argument ASYNC means the process should happen
  806. asynchronously. The resulting buffer should be accessible
  807. through the `org-export-stack' interface.
  808. When optional argument SUBTREEP is non-nil, export the sub-tree
  809. at point, extracting information from the headline properties
  810. first.
  811. When optional argument VISIBLE-ONLY is non-nil, don't export
  812. contents of hidden elements.
  813. When optional argument BODY-ONLY is non-nil, only write code
  814. between \"\\begin{letter}\" and \"\\end{letter}\".
  815. EXT-PLIST, when provided, is a property list with external
  816. parameters overriding Org default settings, but still inferior to
  817. file-local settings.
  818. Export is done in a buffer named \"*Org KOMA-LETTER Export*\". It
  819. will be displayed if `org-export-show-temporary-export-buffer' is
  820. non-nil."
  821. (interactive)
  822. (let (org-koma-letter-special-contents)
  823. (org-export-to-buffer 'koma-letter "*Org KOMA-LETTER Export*"
  824. async subtreep visible-only body-only ext-plist
  825. (lambda () (LaTeX-mode)))))
  826. ;;;###autoload
  827. (defun org-koma-letter-export-to-latex
  828. (&optional async subtreep visible-only body-only ext-plist)
  829. "Export current buffer as a KOMA Scrlttr2 letter (tex).
  830. If narrowing is active in the current buffer, only export its
  831. narrowed part.
  832. If a region is active, export that region.
  833. A non-nil optional argument ASYNC means the process should happen
  834. asynchronously. The resulting file should be accessible through
  835. the `org-export-stack' interface.
  836. When optional argument SUBTREEP is non-nil, export the sub-tree
  837. at point, extracting information from the headline properties
  838. first.
  839. When optional argument VISIBLE-ONLY is non-nil, don't export
  840. contents of hidden elements.
  841. When optional argument BODY-ONLY is non-nil, only write code
  842. between \"\\begin{letter}\" and \"\\end{letter}\".
  843. EXT-PLIST, when provided, is a property list with external
  844. parameters overriding Org default settings, but still inferior to
  845. file-local settings.
  846. When optional argument PUB-DIR is set, use it as the publishing
  847. directory.
  848. Return output file's name."
  849. (interactive)
  850. (let ((outfile (org-export-output-file-name ".tex" subtreep))
  851. (org-koma-letter-special-contents))
  852. (org-export-to-file 'koma-letter outfile
  853. async subtreep visible-only body-only ext-plist)))
  854. ;;;###autoload
  855. (defun org-koma-letter-export-to-pdf
  856. (&optional async subtreep visible-only body-only ext-plist)
  857. "Export current buffer as a KOMA Scrlttr2 letter (pdf).
  858. If narrowing is active in the current buffer, only export its
  859. narrowed part.
  860. If a region is active, export that region.
  861. A non-nil optional argument ASYNC means the process should happen
  862. asynchronously. The resulting file should be accessible through
  863. the `org-export-stack' interface.
  864. When optional argument SUBTREEP is non-nil, export the sub-tree
  865. at point, extracting information from the headline properties
  866. first.
  867. When optional argument VISIBLE-ONLY is non-nil, don't export
  868. contents of hidden elements.
  869. When optional argument BODY-ONLY is non-nil, only write code
  870. between \"\\begin{letter}\" and \"\\end{letter}\".
  871. EXT-PLIST, when provided, is a property list with external
  872. parameters overriding Org default settings, but still inferior to
  873. file-local settings.
  874. Return PDF file's name."
  875. (interactive)
  876. (let ((file (org-export-output-file-name ".tex" subtreep))
  877. (org-koma-letter-special-contents))
  878. (org-export-to-file 'koma-letter file
  879. async subtreep visible-only body-only ext-plist
  880. (lambda (file) (org-latex-compile file)))))
  881. (provide 'ox-koma-letter)
  882. ;;; ox-koma-letter.el ends here