ox-koma-letter.el 28 KB

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