ox-koma-letter.el 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013
  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. ;; Maintainer: Marco Wahl <marcowahlsoft@gmail.com>
  8. ;; Keywords: org, wp, tex
  9. ;; This file is part of GNU Emacs.
  10. ;; This program is free software: you can redistribute it and/or modify
  11. ;; it under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation, either version 3 of the License, or
  13. ;; (at your option) any later version.
  14. ;; This program is distributed in the hope that it will be useful,
  15. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ;; GNU General Public License for more details.
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
  20. ;;; Commentary:
  21. ;;
  22. ;; This library implements a KOMA Scrlttr2 back-end, derived from the
  23. ;; LaTeX one.
  24. ;;
  25. ;; Depending on the desired output format, three commands are provided
  26. ;; for export: `org-koma-letter-export-as-latex' (temporary buffer),
  27. ;; `org-koma-letter-export-to-latex' ("tex" file) and
  28. ;; `org-koma-letter-export-to-pdf' ("pdf" file).
  29. ;;
  30. ;; On top of buffer keywords supported by `latex' back-end (see
  31. ;; `org-latex-options-alist'), this back-end introduces the following
  32. ;; keywords:
  33. ;; - CLOSING: see `org-koma-letter-closing',
  34. ;; - FROM_ADDRESS: see `org-koma-letter-from-address',
  35. ;; - LCO: see `org-koma-letter-class-option-file',
  36. ;; - OPENING: see `org-koma-letter-opening',
  37. ;; - PHONE_NUMBER: see `org-koma-letter-phone-number',
  38. ;; - URL: see `org-koma-letter-url',
  39. ;; - FROM_LOGO: see `org-koma-letter-from-logo',
  40. ;; - SIGNATURE: see `org-koma-letter-signature',
  41. ;; - PLACE: see `org-koma-letter-place',
  42. ;; - LOCATION: see `org-koma-letter-location',
  43. ;; - TO_ADDRESS: If unspecified this is set to "\mbox{}".
  44. ;;
  45. ;; TO_ADDRESS, FROM_ADDRESS, LOCATION, CLOSING, and SIGNATURE can also
  46. ;; be specified using "special headings" with the special tags
  47. ;; specified in `org-koma-letter-special-tags-in-letter'. LaTeX line
  48. ;; breaks are not necessary for TO_ADDRESS, FROM_ADDRESS and LOCATION.
  49. ;; If both a headline and a keyword specify a to or from address the
  50. ;; value is determined in accordance with
  51. ;; `org-koma-letter-prefer-special-headings'.
  52. ;;
  53. ;; A number of OPTIONS settings can be set to change which contents is
  54. ;; exported.
  55. ;; - backaddress (see `org-koma-letter-use-backaddress')
  56. ;; - foldmarks (see `org-koma-letter-use-foldmarks')
  57. ;; - phone (see `org-koma-letter-use-phone')
  58. ;; - url (see `org-koma-letter-use-url')
  59. ;; - from-logo (see `org-koma-letter-use-from-logo')
  60. ;; - email (see `org-koma-letter-use-email')
  61. ;; - place (see `org-koma-letter-use-place')
  62. ;; - location (see `org-koma-letter-use-location')
  63. ;; - subject, a list of format options
  64. ;; (see `org-koma-letter-subject-format')
  65. ;; - after-closing-order, a list of the ordering of headings with
  66. ;; special tags after closing (see
  67. ;; `org-koma-letter-special-tags-after-closing')
  68. ;; - after-letter-order, as above, but after the end of the letter
  69. ;; (see `org-koma-letter-special-tags-after-letter').
  70. ;;
  71. ;; The following variables works differently from the main LaTeX class
  72. ;; - AUTHOR: Default to user-full-name but may be disabled.
  73. ;; (See also `org-koma-letter-author'.)
  74. ;; - EMAIL: Same as AUTHOR. (See also `org-koma-letter-email'.)
  75. ;;
  76. ;; FROM_LOGO uses LaTeX markup. FROM_LOGO provides the
  77. ;; "includegraphics" command to tell LaTeX where to find the logo.
  78. ;; This command needs to know the logo's directory and file name. The
  79. ;; directory can either be relative or absolute, just as you would
  80. ;; expect. LaTeX can use three file types for the logo: PDF, JPEG, or
  81. ;; PNG. The logo can either include or exclude its extension, which
  82. ;; might surprise you. When you exclude its extension, LaTeX will
  83. ;; search the directory for the "best" quality graphics format. For
  84. ;; example if it finds both logo.pdf and logo.png then it will
  85. ;; identify the PDF as "better", and include "logo.pdf". This can be
  86. ;; useful, for example, when you are mocking up a logo in the PNG
  87. ;; raster format and then switch over to the higher quality PDF vector
  88. ;; format. When you include the file extension then LaTeX will
  89. ;; include it without searching for higher quality file types.
  90. ;; Whatever file type you choose, it will probably require a few
  91. ;; design iterations to get the best looking logo size for your
  92. ;; letter. Finally, the directory and file name are specified
  93. ;; *without* quotes. Here are some examples with commentary, in the
  94. ;; location of your letter, with a logo named "logo", to get you
  95. ;; started:
  96. ;;
  97. ;; Logo in the same directory: \includegraphics{logo}
  98. ;; or a sub-directory: \includegraphics{logos/production/logo}
  99. ;;
  100. ;; Logos specified using absolute paths on Linux or Windows:
  101. ;;
  102. ;; \includegraphics{~/correspondence/logo}
  103. ;; \includegraphics{~/correspondence/logos/production/logo}
  104. ;; \includegraphics{c:/you/correspondence/logo}
  105. ;; \includegraphics{c:/you/correspondence/logos/production/logo}
  106. ;;
  107. ;; Logos in the same directory where the "better" quality PDF will
  108. ;; be chosen over the JPG:
  109. ;;
  110. ;; \includegraphics{logo.pdf}
  111. ;; \includegraphics{logo.png}
  112. ;;
  113. ;; Headlines are in general ignored. However, headlines with special
  114. ;; tags can be used for specified contents like postscript (ps),
  115. ;; carbon copy (cc), enclosures (encl) and code to be inserted after
  116. ;; \end{letter} (after_letter). Specials tags are defined in
  117. ;; `org-koma-letter-special-tags-after-closing' and
  118. ;; `org-koma-letter-special-tags-after-letter'. Currently members of
  119. ;; `org-koma-letter-special-tags-after-closing' used as macros and the
  120. ;; content of the headline is the argument.
  121. ;;
  122. ;; Headlines with to and from may also be used rather than the keyword
  123. ;; approach described above. If both a keyword and a headline with
  124. ;; information is present precedence is determined by
  125. ;; `org-koma-letter-prefer-special-headings'.
  126. ;;
  127. ;; You need an appropriate association in `org-latex-classes' in order
  128. ;; to use the KOMA Scrlttr2 class. By default, a sparse scrlttr2
  129. ;; class is provided: "default-koma-letter". You can also add you own
  130. ;; letter class. For instance:
  131. ;;
  132. ;; (add-to-list 'org-latex-classes
  133. ;; '("my-letter"
  134. ;; "\\documentclass\[%
  135. ;; DIV=14,
  136. ;; fontsize=12pt,
  137. ;; parskip=half,
  138. ;; subject=titled,
  139. ;; backaddress=false,
  140. ;; fromalign=left,
  141. ;; fromemail=true,
  142. ;; fromphone=true\]\{scrlttr2\}
  143. ;; \[DEFAULT-PACKAGES]
  144. ;; \[PACKAGES]
  145. ;; \[EXTRA]"))
  146. ;;
  147. ;; Then, in your Org document, be sure to require the proper class
  148. ;; with:
  149. ;;
  150. ;; #+LATEX_CLASS: my-letter
  151. ;;
  152. ;; Or by setting `org-koma-letter-default-class'.
  153. ;;
  154. ;; You may have to load (LaTeX) Babel as well, e.g., by adding
  155. ;; it to `org-latex-packages-alist',
  156. ;;
  157. ;; (add-to-list 'org-latex-packages-alist '("AUTO" "babel" nil))
  158. ;;; Code:
  159. (require 'cl-lib)
  160. (require 'ox-latex)
  161. ;; Install a default letter class.
  162. (unless (assoc "default-koma-letter" org-latex-classes)
  163. (add-to-list 'org-latex-classes
  164. '("default-koma-letter" "\\documentclass[11pt]{scrlttr2}")))
  165. ;;; User-Configurable Variables
  166. (defgroup org-export-koma-letter nil
  167. "Options for exporting to KOMA scrlttr2 class in LaTeX export."
  168. :tag "Org Koma-Letter"
  169. :group 'org-export)
  170. (defcustom org-koma-letter-class-option-file "NF"
  171. "Letter Class Option File.
  172. This option can also be set with the LCO keyword."
  173. :group 'org-export-koma-letter
  174. :type 'string)
  175. (defcustom org-koma-letter-author 'user-full-name
  176. "Sender's name.
  177. This variable defaults to calling the function `user-full-name'
  178. which just returns the current function `user-full-name'.
  179. Alternatively a string, nil or a function may be given.
  180. Functions must return a string.
  181. This option can also be set with the AUTHOR keyword."
  182. :group 'org-export-koma-letter
  183. :type '(radio (function-item user-full-name)
  184. (string)
  185. (function)
  186. (const :tag "Do not export author" nil)))
  187. (defcustom org-koma-letter-email 'org-koma-letter-email
  188. "Sender's email address.
  189. This variable defaults to the value `org-koma-letter-email' which
  190. returns `user-mail-address'. Alternatively a string, nil or
  191. a function may be given. Functions must return a string.
  192. This option can also be set with the EMAIL keyword."
  193. :group 'org-export-koma-letter
  194. :type '(radio (function-item org-koma-letter-email)
  195. (string)
  196. (function)
  197. (const :tag "Do not export email" nil)))
  198. (defcustom org-koma-letter-from-address ""
  199. "Sender's address, as a string.
  200. This option can also be set with one or more FROM_ADDRESS
  201. keywords."
  202. :group 'org-export-koma-letter
  203. :type 'string)
  204. (defcustom org-koma-letter-phone-number ""
  205. "Sender's phone number, as a string.
  206. This option can also be set with the PHONE_NUMBER keyword."
  207. :group 'org-export-koma-letter
  208. :type 'string)
  209. (defcustom org-koma-letter-url ""
  210. "Sender's URL, e. g., the URL of her homepage.
  211. This option can also be set with the URL keyword."
  212. :group 'org-export-koma-letter
  213. :type 'string
  214. :safe #'stringp)
  215. (defcustom org-koma-letter-from-logo ""
  216. "Commands for inserting the sender's logo, e. g., \\includegraphics{logo}.
  217. This option can also be set with the FROM_LOGO keyword."
  218. :group 'org-export-koma-letter
  219. :type 'string
  220. :safe #'stringp)
  221. (defcustom org-koma-letter-place ""
  222. "Place from which the letter is sent, as a string.
  223. This option can also be set with the PLACE keyword."
  224. :group 'org-export-koma-letter
  225. :type 'string)
  226. (defcustom org-koma-letter-location ""
  227. "Sender's extension field, as a string.
  228. This option can also be set with the LOCATION keyword.
  229. Moreover, when:
  230. (1) Either `org-koma-letter-prefer-special-headings' is non-nil
  231. or there is no LOCATION keyword or the LOCATION keyword is
  232. empty;
  233. (2) the letter contains a headline with the special
  234. tag \"location\";
  235. then the location will be set as the content of the location
  236. special heading.
  237. The location field is typically printed right of the address
  238. field (See Figure 4.9. in the English manual of 2015-10-03)."
  239. :group 'org-export-koma-letter
  240. :type 'string)
  241. (defcustom org-koma-letter-opening ""
  242. "Letter's opening, as a string.
  243. This option can also be set with the OPENING keyword. Moreover,
  244. when:
  245. (1) Either `org-koma-letter-prefer-special-headings' is non-nil
  246. or the CLOSING keyword is empty
  247. (2) `org-koma-letter-headline-is-opening-maybe' is non-nil;
  248. (3) the letter contains a headline without a special
  249. tag (e.g. \"to\" or \"ps\");
  250. then the opening will be implicitly set as the untagged headline title."
  251. :group 'org-export-koma-letter
  252. :type 'string)
  253. (defcustom org-koma-letter-closing ""
  254. "Letter's closing, as a string.
  255. This option can also be set with the CLOSING keyword. Moreover,
  256. when:
  257. (1) Either `org-koma-letter-prefer-special-headings' is non-nil
  258. or the CLOSING keyword is empty;
  259. (2) `org-koma-letter-headline-is-opening-maybe' is non-nil;
  260. (3) the letter contains a headline with the special
  261. tag \"closing\";
  262. then the opening will be set as the title of the closing special
  263. heading title."
  264. :group 'org-export-koma-letter
  265. :type 'string)
  266. (defcustom org-koma-letter-signature ""
  267. "Signature, as a string.
  268. This option can also be set with the SIGNATURE keyword.
  269. Moreover, when:
  270. (1) Either `org-koma-letter-prefer-special-headings' is non-nil
  271. or there is no CLOSING keyword or the CLOSING keyword is empty;
  272. (2) `org-koma-letter-headline-is-opening-maybe' is non-nil;
  273. (3) the letter contains a headline with the special
  274. tag \"closing\";
  275. then the signature will be set as the content of the
  276. closing special heading.
  277. Note if the content is empty the signature will not be set."
  278. :group 'org-export-koma-letter
  279. :type 'string)
  280. (defcustom org-koma-letter-prefer-special-headings nil
  281. "Non-nil means prefer headlines over keywords for TO and FROM.
  282. This option can also be set with the OPTIONS keyword, e.g.:
  283. \"special-headings:t\"."
  284. :group 'org-export-koma-letter
  285. :type 'boolean)
  286. (defcustom org-koma-letter-subject-format t
  287. "Non-nil means include the subject.
  288. Support formatting options.
  289. When t, insert a subject using default options. When nil, do not
  290. insert a subject at all. It can also be a list of symbols among
  291. the following ones:
  292. `afteropening' Subject after opening
  293. `beforeopening' Subject before opening
  294. `centered' Subject centered
  295. `left' Subject left-justified
  296. `right' Subject right-justified
  297. `titled' Add title/description to subject
  298. `underlined' Set subject underlined
  299. `untitled' Do not add title/description to subject
  300. Please refer to the KOMA-script manual (Table 4.16. in the
  301. English manual of 2012-07-22).
  302. This option can also be set with the OPTIONS keyword, e.g.:
  303. \"subject:(underlined centered)\"."
  304. :type
  305. '(choice
  306. (const :tag "No export" nil)
  307. (const :tag "Default options" t)
  308. (set :tag "Configure options"
  309. (const :tag "Subject after opening" afteropening)
  310. (const :tag "Subject before opening" beforeopening)
  311. (const :tag "Subject centered" centered)
  312. (const :tag "Subject left-justified" left)
  313. (const :tag "Subject right-justified" right)
  314. (const :tag "Add title or description to subject" underlined)
  315. (const :tag "Set subject underlined" titled)
  316. (const :tag "Do not add title or description to subject" untitled)))
  317. :group 'org-export-koma-letter)
  318. (defcustom org-koma-letter-use-backaddress nil
  319. "Non-nil prints return address in line above to address.
  320. This option can also be set with the OPTIONS keyword, e.g.:
  321. \"backaddress:t\"."
  322. :group 'org-export-koma-letter
  323. :type 'boolean)
  324. (defcustom org-koma-letter-use-foldmarks t
  325. "Configure appearance of folding marks.
  326. When t, activate default folding marks. When nil, do not insert
  327. folding marks at all. It can also be a list of symbols among the
  328. following ones:
  329. `B' Activate upper horizontal mark on left paper edge
  330. `b' Deactivate upper horizontal mark on left paper edge
  331. `H' Activate all horizontal marks on left paper edge
  332. `h' Deactivate all horizontal marks on left paper edge
  333. `L' Activate left vertical mark on upper paper edge
  334. `l' Deactivate left vertical mark on upper paper edge
  335. `M' Activate middle horizontal mark on left paper edge
  336. `m' Deactivate middle horizontal mark on left paper edge
  337. `P' Activate punch or center mark on left paper edge
  338. `p' Deactivate punch or center mark on left paper edge
  339. `T' Activate lower horizontal mark on left paper edge
  340. `t' Deactivate lower horizontal mark on left paper edge
  341. `V' Activate all vertical marks on upper paper edge
  342. `v' Deactivate all vertical marks on upper paper edge
  343. This option can also be set with the OPTIONS keyword, e.g.:
  344. \"foldmarks:(b l m t)\"."
  345. :group 'org-export-koma-letter
  346. :type '(choice
  347. (const :tag "Activate default folding marks" t)
  348. (const :tag "Deactivate folding marks" nil)
  349. (set
  350. :tag "Configure folding marks"
  351. (const :tag "Activate upper horizontal mark on left paper edge" B)
  352. (const :tag "Deactivate upper horizontal mark on left paper edge" b)
  353. (const :tag "Activate all horizontal marks on left paper edge" H)
  354. (const :tag "Deactivate all horizontal marks on left paper edge" h)
  355. (const :tag "Activate left vertical mark on upper paper edge" L)
  356. (const :tag "Deactivate left vertical mark on upper paper edge" l)
  357. (const :tag "Activate middle horizontal mark on left paper edge" M)
  358. (const :tag "Deactivate middle horizontal mark on left paper edge" m)
  359. (const :tag "Activate punch or center mark on left paper edge" P)
  360. (const :tag "Deactivate punch or center mark on left paper edge" p)
  361. (const :tag "Activate lower horizontal mark on left paper edge" T)
  362. (const :tag "Deactivate lower horizontal mark on left paper edge" t)
  363. (const :tag "Activate all vertical marks on upper paper edge" V)
  364. (const :tag "Deactivate all vertical marks on upper paper edge" v))))
  365. (defcustom org-koma-letter-use-phone nil
  366. "Non-nil prints sender's phone number.
  367. This option can also be set with the OPTIONS keyword, e.g.:
  368. \"phone:t\"."
  369. :group 'org-export-koma-letter
  370. :type 'boolean)
  371. (defcustom org-koma-letter-use-url nil
  372. "Non-nil prints sender's URL.
  373. This option can also be set with the OPTIONS keyword, e.g.:
  374. \"url:t\"."
  375. :group 'org-export-koma-letter
  376. :type 'boolean
  377. :safe #'booleanp)
  378. (defcustom org-koma-letter-use-from-logo nil
  379. "Non-nil prints sender's FROM_LOGO.
  380. This option can also be set with the OPTIONS keyword, e.g.:
  381. \"from-logo:t\"."
  382. :group 'org-export-koma-letter
  383. :type 'boolean
  384. :safe #'booleanp)
  385. (defcustom org-koma-letter-use-email nil
  386. "Non-nil prints sender's email address.
  387. This option can also be set with the OPTIONS keyword, e.g.:
  388. \"email:t\"."
  389. :group 'org-export-koma-letter
  390. :type 'boolean)
  391. (defcustom org-koma-letter-use-place t
  392. "Non-nil prints the letter's place next to the date.
  393. This option can also be set with the OPTIONS keyword, e.g.:
  394. \"place:nil\"."
  395. :group 'org-export-koma-letter
  396. :type 'boolean)
  397. (defcustom org-koma-letter-default-class "default-koma-letter"
  398. "Default class for `org-koma-letter'.
  399. The value must be a member of `org-latex-classes'."
  400. :group 'org-export-koma-letter
  401. :type 'string)
  402. (defcustom org-koma-letter-headline-is-opening-maybe t
  403. "Non-nil means a headline may be used as an opening and closing.
  404. See also `org-koma-letter-opening' and
  405. `org-koma-letter-closing'."
  406. :group 'org-export-koma-letter
  407. :type 'boolean)
  408. (defcustom org-koma-letter-prefer-subject nil
  409. "Non-nil means title should be interpreted as subject if subject is missing.
  410. This option can also be set with the OPTIONS keyword,
  411. e.g. \"title-subject:t\"."
  412. :group 'org-export-koma-letter
  413. :type 'boolean)
  414. (defconst org-koma-letter-special-tags-in-letter '(to from closing location)
  415. "Header tags related to the letter itself.")
  416. (defconst org-koma-letter-special-tags-after-closing '(after_closing ps encl cc)
  417. "Header tags to be inserted in the letter after closing.")
  418. (defconst org-koma-letter-special-tags-as-macro '(ps encl cc)
  419. "Header tags to be inserted as macros.")
  420. (defconst org-koma-letter-special-tags-after-letter '(after_letter)
  421. "Header tags to be inserted after the letter.")
  422. (defvar org-koma-letter-special-contents nil
  423. "Holds special content temporarily.")
  424. ;;; Define Back-End
  425. (org-export-define-derived-backend 'koma-letter 'latex
  426. :options-alist
  427. '((:latex-class "LATEX_CLASS" nil org-koma-letter-default-class t)
  428. (:lco "LCO" nil org-koma-letter-class-option-file)
  429. (:author "AUTHOR" nil (org-koma-letter--get-value org-koma-letter-author) parse)
  430. (:author-changed-in-buffer-p "AUTHOR" nil nil t)
  431. (:from-address "FROM_ADDRESS" nil org-koma-letter-from-address newline)
  432. (:phone-number "PHONE_NUMBER" nil org-koma-letter-phone-number)
  433. (:url "URL" nil org-koma-letter-url)
  434. (:from-logo "FROM_LOGO" nil org-koma-letter-from-logo)
  435. (:email "EMAIL" nil (org-koma-letter--get-value org-koma-letter-email) t)
  436. (:to-address "TO_ADDRESS" nil nil newline)
  437. (:place "PLACE" nil org-koma-letter-place)
  438. (:location "LOCATION" nil org-koma-letter-location)
  439. (:subject "SUBJECT" nil nil parse)
  440. (:opening "OPENING" nil org-koma-letter-opening parse)
  441. (:closing "CLOSING" nil org-koma-letter-closing parse)
  442. (:signature "SIGNATURE" nil org-koma-letter-signature newline)
  443. (:special-headings nil "special-headings" org-koma-letter-prefer-special-headings)
  444. (:special-tags-as-macro nil nil org-koma-letter-special-tags-as-macro)
  445. (:special-tags-in-letter nil nil org-koma-letter-special-tags-in-letter)
  446. (:special-tags-after-closing nil "after-closing-order"
  447. org-koma-letter-special-tags-after-closing)
  448. (:special-tags-after-letter nil "after-letter-order"
  449. org-koma-letter-special-tags-after-letter)
  450. (:with-backaddress nil "backaddress" org-koma-letter-use-backaddress)
  451. (:with-email nil "email" org-koma-letter-use-email)
  452. (:with-foldmarks nil "foldmarks" org-koma-letter-use-foldmarks)
  453. (:with-phone nil "phone" org-koma-letter-use-phone)
  454. (:with-url nil "url" org-koma-letter-use-url)
  455. (:with-from-logo nil "from-logo" org-koma-letter-use-from-logo)
  456. (:with-place nil "place" org-koma-letter-use-place)
  457. (:with-subject nil "subject" org-koma-letter-subject-format)
  458. (:with-title-as-subject nil "title-subject" org-koma-letter-prefer-subject)
  459. (:with-headline-opening nil nil org-koma-letter-headline-is-opening-maybe)
  460. ;; Special properties non-nil when a setting happened in buffer.
  461. ;; They are used to prioritize in-buffer settings over "lco"
  462. ;; files. See `org-koma-letter-template'.
  463. (:inbuffer-author "AUTHOR" nil 'koma-letter:empty)
  464. (:inbuffer-from "FROM" nil 'koma-letter:empty)
  465. (:inbuffer-email "EMAIL" nil 'koma-letter:empty)
  466. (:inbuffer-phone-number "PHONE_NUMBER" nil 'koma-letter:empty)
  467. (:inbuffer-url "URL" nil 'koma-letter:empty)
  468. (:inbuffer-from-logo "FROM_LOGO" nil 'koma-letter:empty)
  469. (:inbuffer-place "PLACE" nil 'koma-letter:empty)
  470. (:inbuffer-location "LOCATION" nil 'koma-letter:empty)
  471. (:inbuffer-signature "SIGNATURE" nil 'koma-letter:empty)
  472. (:inbuffer-with-backaddress nil "backaddress" 'koma-letter:empty)
  473. (:inbuffer-with-email nil "email" 'koma-letter:empty)
  474. (:inbuffer-with-foldmarks nil "foldmarks" 'koma-letter:empty)
  475. (:inbuffer-with-phone nil "phone" 'koma-letter:empty)
  476. (:inbuffer-with-url nil "url" 'koma-letter:empty)
  477. (:inbuffer-with-from-logo nil "from-logo" 'koma-letter:empty)
  478. (:inbuffer-with-place nil "place" 'koma-letter:empty))
  479. :translate-alist '((export-block . org-koma-letter-export-block)
  480. (export-snippet . org-koma-letter-export-snippet)
  481. (headline . org-koma-letter-headline)
  482. (keyword . org-koma-letter-keyword)
  483. (template . org-koma-letter-template))
  484. :menu-entry
  485. '(?k "Export with KOMA Scrlttr2"
  486. ((?L "As LaTeX buffer" org-koma-letter-export-as-latex)
  487. (?l "As LaTeX file" org-koma-letter-export-to-latex)
  488. (?p "As PDF file" org-koma-letter-export-to-pdf)
  489. (?o "As PDF file and open"
  490. (lambda (a s v b)
  491. (if a (org-koma-letter-export-to-pdf t s v b)
  492. (org-open-file (org-koma-letter-export-to-pdf nil s v b))))))))
  493. ;;; Helper functions
  494. (defun org-koma-letter-email ()
  495. "Return the current `user-mail-address'."
  496. user-mail-address)
  497. ;; The following is taken from/inspired by ox-grof.el
  498. ;; Thanks, Luis!
  499. (defun org-koma-letter--get-tagged-contents (key)
  500. "Get contents from a headline tagged with KEY.
  501. The contents is stored in `org-koma-letter-special-contents'."
  502. (let ((value (cdr (assoc-string (org-koma-letter--get-value key)
  503. org-koma-letter-special-contents))))
  504. (when value (org-string-nw-p (org-trim value)))))
  505. (defun org-koma-letter--get-value (value)
  506. "Turn value into a string whenever possible.
  507. Determines if VALUE is nil, a string, a function or a symbol and
  508. return a string or nil."
  509. (when value
  510. (cond ((stringp value) value)
  511. ((functionp value) (funcall value))
  512. ((symbolp value) (symbol-name value))
  513. (t value))))
  514. (defun org-koma-letter--special-contents-inline (keywords info)
  515. "Process KEYWORDS members of `org-koma-letter-special-contents'.
  516. KEYWORDS is a list of symbols. Return them as a string to be
  517. formatted.
  518. The function is used for inserting content of special headings
  519. such as the one tagged with PS."
  520. (mapconcat
  521. (lambda (keyword)
  522. (let* ((name (org-koma-letter--get-value keyword))
  523. (value (org-koma-letter--get-tagged-contents name))
  524. (macrop (memq keyword (plist-get info :special-tags-as-macro))))
  525. (cond ((not value) nil)
  526. (macrop (format "\\%s{%s}\n" name value))
  527. (t value))))
  528. keywords
  529. "\n"))
  530. (defun org-koma-letter--add-latex-newlines (string)
  531. "Replace regular newlines with LaTeX newlines (i.e. `\\\\')."
  532. (let ((str (org-trim string)))
  533. (when (org-string-nw-p str)
  534. (replace-regexp-in-string "\n" "\\\\\\\\\n" str))))
  535. ;;; Transcode Functions
  536. ;;;; Export Block
  537. (defun org-koma-letter-export-block (export-block contents info)
  538. "Transcode an EXPORT-BLOCK element into KOMA Scrlttr2 code.
  539. CONTENTS is nil. INFO is a plist used as a communication
  540. channel."
  541. (when (member (org-element-property :type export-block)
  542. '("KOMA-LETTER" "LATEX"))
  543. (org-remove-indentation (org-element-property :value export-block))))
  544. ;;;; Export Snippet
  545. (defun org-koma-letter-export-snippet (export-snippet contents info)
  546. "Transcode an EXPORT-SNIPPET object into KOMA Scrlttr2 code.
  547. CONTENTS is nil. INFO is a plist used as a communication
  548. channel."
  549. (when (memq (org-export-snippet-backend export-snippet) '(latex koma-letter))
  550. (org-element-property :value export-snippet)))
  551. ;;;; Keyword
  552. (defun org-koma-letter-keyword (keyword contents info)
  553. "Transcode a KEYWORD element into KOMA Scrlttr2 code.
  554. CONTENTS is nil. INFO is a plist used as a communication
  555. channel."
  556. (let ((key (org-element-property :key keyword))
  557. (value (org-element-property :value keyword)))
  558. ;; Handle specifically KOMA-LETTER keywords. Otherwise, fallback
  559. ;; to `latex' back-end.
  560. (if (equal key "KOMA-LETTER") value
  561. (org-export-with-backend 'latex keyword contents info))))
  562. ;; Headline
  563. (defun org-koma-letter-headline (headline contents info)
  564. "Transcode a HEADLINE element from Org to LaTeX.
  565. CONTENTS holds the contents of the headline. INFO is a plist
  566. holding contextual information.
  567. Note that if a headline is tagged with a tag from
  568. `org-koma-letter-special-tags' it will not be exported, but
  569. stored in `org-koma-letter-special-contents' and included at the
  570. appropriate place."
  571. (let ((special-tag (org-koma-letter--special-tag headline info)))
  572. (if (not special-tag)
  573. contents
  574. (push (cons special-tag contents) org-koma-letter-special-contents)
  575. "")))
  576. (defun org-koma-letter--special-tag (headline info)
  577. "Non-nil if HEADLINE is a special headline.
  578. INFO is a plist holding contextual information. Return first
  579. special tag headline."
  580. (let ((special-tags (append
  581. (plist-get info :special-tags-in-letter)
  582. (plist-get info :special-tags-after-closing)
  583. (plist-get info :special-tags-after-letter))))
  584. (cl-some (lambda (tag) (and (assoc-string tag special-tags) tag))
  585. (org-export-get-tags headline info))))
  586. (defun org-koma-letter--keyword-or-headline (plist-key pred info)
  587. "Return the correct version of opening or closing.
  588. PLIST-KEY should be a key in info, typically :opening
  589. or :closing. PRED is a predicate run on headline to determine
  590. which title to use which takes two arguments, a headline element
  591. and an info plist. INFO is a plist holding contextual
  592. information. Return the preferred candidate for the exported of
  593. PLIST-KEY."
  594. (let* ((keyword-candidate (plist-get info plist-key))
  595. (headline-candidate (when (and (plist-get info :with-headline-opening)
  596. (or (plist-get info :special-headings)
  597. (not keyword-candidate)))
  598. (org-element-map (plist-get info :parse-tree)
  599. 'headline
  600. (lambda (h)
  601. (and (funcall pred h info)
  602. (org-element-property :title h)))
  603. info t))))
  604. (org-export-data (or headline-candidate keyword-candidate "") info)))
  605. ;;;; Template
  606. (defun org-koma-letter-template (contents info)
  607. "Return complete document string after KOMA Scrlttr2 conversion.
  608. CONTENTS is the transcoded contents string. INFO is a plist
  609. holding export options."
  610. (concat
  611. ;; Time-stamp.
  612. (and (plist-get info :time-stamp-file)
  613. (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
  614. ;; LaTeX compiler
  615. (org-latex--insert-compiler info)
  616. ;; Document class and packages.
  617. (org-latex-make-preamble info)
  618. ;; Settings. They can come from three locations, in increasing
  619. ;; order of precedence: global variables, LCO files and in-buffer
  620. ;; settings. Thus, we first insert settings coming from global
  621. ;; variables, then we insert LCO files, and, eventually, we insert
  622. ;; settings coming from buffer keywords.
  623. (org-koma-letter--build-settings 'global info)
  624. (mapconcat (lambda (file) (format "\\LoadLetterOption{%s}\n" file))
  625. (split-string (or (plist-get info :lco) ""))
  626. "")
  627. (org-koma-letter--build-settings 'buffer info)
  628. ;; Date.
  629. (format "\\date{%s}\n" (org-export-data (org-export-get-date info) info))
  630. ;; Hyperref, document start, and subject and title.
  631. (let* ((with-subject (plist-get info :with-subject))
  632. (with-title (plist-get info :with-title))
  633. (title-as-subject (and with-subject
  634. (plist-get info :with-title-as-subject)))
  635. (subject* (org-string-nw-p
  636. (org-export-data (plist-get info :subject) info)))
  637. (title* (and with-title
  638. (org-string-nw-p
  639. (org-export-data (plist-get info :title) info))))
  640. (subject (cond ((not with-subject) nil)
  641. (title-as-subject (or subject* title*))
  642. (t subject*)))
  643. (title (cond ((not with-title) nil)
  644. (title-as-subject (and subject* title*))
  645. (t title*)))
  646. (hyperref-template (plist-get info :latex-hyperref-template))
  647. (spec (append (list (cons ?t (or title subject "")))
  648. (org-latex--format-spec info))))
  649. (concat
  650. (when (and with-subject (not (eq with-subject t)))
  651. (format "\\KOMAoption{subject}{%s}\n"
  652. (if (symbolp with-subject) with-subject
  653. (mapconcat #'symbol-name with-subject ","))))
  654. ;; Hyperref.
  655. (and (stringp hyperref-template)
  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