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