ox-koma-letter.el 32 KB

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