ox-koma-letter.el 31 KB

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