ox-koma-letter.el 33 KB

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