ox-koma-letter.el 35 KB

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