ox-koma-letter.el 30 KB

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