ox-koma-letter.el 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  1. ;;; ox-koma-letter.el --- KOMA Scrlttr2 Back-End for Org Export Engine
  2. ;; Copyright (C) 2007-2012 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 very 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. "The sender's name.
  126. This variable defaults to calling the function `user-full-name'
  127. which just returns the current `user-full-name'. Alternatively a
  128. string, nil or a function may be given. Functions must return a
  129. 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. "The 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 a
  139. 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 nil
  146. "Sender's address, as a string."
  147. :group 'org-export-koma-letter
  148. :type 'string)
  149. (defcustom org-koma-letter-phone-number nil
  150. "Sender's phone number, as a string."
  151. :group 'org-export-koma-letter
  152. :type 'string)
  153. (defcustom org-koma-letter-place nil
  154. "Place from which the letter is sent."
  155. :group 'org-export-koma-letter
  156. :type 'string)
  157. (defcustom org-koma-letter-opening nil
  158. "Letter's opening, as a string."
  159. :group 'org-export-koma-letter
  160. :type 'string)
  161. (defcustom org-koma-letter-closing nil
  162. "Koma-Letter's closing, as a string."
  163. :group 'org-export-koma-letter
  164. :type 'string)
  165. (defcustom org-koma-letter-prefer-special-headings nil
  166. "If both a TO or FROM is specified two places should the
  167. heading version be preferred?"
  168. :group 'org-export-koma-letter
  169. :type 'boolean)
  170. (defcustom org-koma-letter-signature nil
  171. "String used as the signature."
  172. :group 'org-export-koma-letter
  173. :type 'string)
  174. (defcustom org-koma-letter-subject-format t
  175. "Use the title as the subject of the letter. At the time of
  176. writing the following values are allowed:
  177. - afteropening: subject after opening
  178. - beforeopening: subject before opening
  179. - centered: subject centered
  180. - left:subject left-justified
  181. - right: subject right-justified
  182. - titled: add title/description to subject
  183. - underlined: set subject underlined (see note in text please)
  184. - untitled: do not add title/description to subject.
  185. - No-export: do no insert a subject even if present.
  186. Please refer to the KOMA-script manual (Table 4.16. in the
  187. English manual of 2012-07-22)"
  188. :type '(radio
  189. (const :tag "No export" nil)
  190. (const :tag "Default options" t)
  191. (set :tag "selection"
  192. (const 'afteropening)
  193. (const 'beforeopening)
  194. (const 'centered)
  195. (const 'left)
  196. (const 'right)
  197. (const 'underlined)
  198. (const 'titled)
  199. (const 'untitled))
  200. (string))
  201. :group 'org-export-koma-letter)
  202. (defcustom org-koma-letter-use-backaddress t
  203. "Print return address in small line above to address."
  204. :group 'org-export-koma-letter
  205. :type 'boolean)
  206. (defcustom org-koma-letter-use-foldmarks "true"
  207. "Configure appearence of fold marks.
  208. Accepts any valid value for the KOMA-Script `foldmarks' option.
  209. Use `foldmarks:true' to activate default fold marks or
  210. `foldmarks:nil' to deactivate fold marks."
  211. :group 'org-export-koma-letter
  212. :type 'string)
  213. (defcustom org-koma-letter-use-phone t
  214. "Print sender's phone number."
  215. :group 'org-export-koma-letter
  216. :type 'boolean)
  217. (defcustom org-koma-letter-use-email t
  218. "Print sender's email address."
  219. :group 'org-export-koma-letter
  220. :type 'boolean)
  221. (defcustom org-koma-letter-use-place t
  222. "Print the letter's place next to the date."
  223. :group 'org-export-koma-letter
  224. :type 'boolean)
  225. (defcustom org-koma-letter-default-class nil
  226. "Default class for `org-koma-letter'. Must be a member of
  227. `org-latex-classes'"
  228. :group 'org-export-koma-letter
  229. :type 'string)
  230. (defconst org-koma-letter-special-tags-in-letter '(to from)
  231. "header tags related to the letter itself")
  232. (defconst org-koma-letter-special-tags-after-closing '(ps encl cc)
  233. "Header tags to be inserted after closing")
  234. (defconst org-koma-letter-special-tags-after-letter '(after_letter)
  235. "Header tags to be inserted after closing")
  236. (defvar org-koma-letter-special-contents nil
  237. "holds special content temporarily.")
  238. ;;; Define Back-End
  239. (org-export-define-derived-backend 'koma-letter 'latex
  240. :options-alist
  241. '((:lco "LCO" nil org-koma-letter-class-option-file)
  242. (:latex-class "LATEX_CLASS" nil (if org-koma-letter-default-class
  243. org-koma-letter-default-class
  244. org-latex-default-class) t)
  245. (:author "AUTHOR" nil (org-koma-letter--get-custom org-koma-letter-author) t)
  246. (:from-address "FROM_ADDRESS" nil nil newline)
  247. (:phone-number "PHONE_NUMBER" nil org-koma-letter-phone-number)
  248. (:email "EMAIL" nil (org-koma-letter--get-custom org-koma-letter-email) t)
  249. (:to-address "TO_ADDRESS" nil nil newline)
  250. (:place "PLACE" nil org-koma-letter-place)
  251. (:opening "OPENING" nil org-koma-letter-opening)
  252. (:closing "CLOSING" nil org-koma-letter-closing)
  253. (:signature "SIGNATURE" nil org-koma-letter-signature newline)
  254. (:special-tags nil nil (append
  255. org-koma-letter-special-tags-in-letter
  256. org-koma-letter-special-tags-after-closing
  257. org-koma-letter-special-tags-after-letter))
  258. (:special-headings nil "special-headings"
  259. org-koma-letter-prefer-special-headings)
  260. (:with-after-closing nil "after-closing-order"
  261. org-koma-letter-special-tags-after-closing)
  262. (:with-after-letter nil "after-letter-order"
  263. org-koma-letter-special-tags-after-letter)
  264. (:with-backaddress nil "backaddress" org-koma-letter-use-backaddress)
  265. (:with-foldmarks nil "foldmarks" org-koma-letter-use-foldmarks)
  266. (:with-phone nil "phone" org-koma-letter-use-phone)
  267. (:with-email nil "email" org-koma-letter-use-email)
  268. (:with-place nil "place" org-koma-letter-use-place)
  269. (:with-subject nil "subject" org-koma-letter-subject-format))
  270. :translate-alist '((export-block . org-koma-letter-export-block)
  271. (export-snippet . org-koma-letter-export-snippet)
  272. (headline . org-koma-letter-headline)
  273. (keyword . org-koma-letter-keyword)
  274. (template . org-koma-letter-template))
  275. :menu-entry
  276. '(?k "Export with KOMA Scrlttr2"
  277. ((?L "As LaTeX buffer" org-koma-letter-export-as-latex)
  278. (?l "As LaTeX file" org-koma-letter-export-to-latex)
  279. (?p "As PDF file" org-koma-letter-export-to-pdf)
  280. (?o "As PDF file and open"
  281. (lambda (a s v b)
  282. (if a (org-koma-letter-export-to-pdf t s v b)
  283. (org-open-file (org-koma-letter-export-to-pdf nil s v b))))))))
  284. ;;; Initialize class function
  285. (defun org-koma-letter-plug-into-ox ()
  286. "Add a sparse `default-koma-letter' to `org-latex-classes' and set
  287. `org-koma-letter-default-class' to `default-koma-letter'"
  288. (let ((class "default-koma-letter"))
  289. (eval-after-load "ox-latex"
  290. '(unless (member ,class 'org-latex-classes)
  291. (add-to-list 'org-latex-classes
  292. `(,class
  293. "\\documentclass[11pt]{scrlttr2}") ())
  294. (setq org-koma-letter-default-class class)))))
  295. ;;; Helper functions
  296. (defun org-koma-letter-email ()
  297. "Return the current `user-mail-address'"
  298. user-mail-address)
  299. ;; The following is taken from/inspired by ox-grof.el
  300. ;; Thanks, Luis!
  301. (defun org-koma-letter--get-tagged-contents (key)
  302. "Get tagged content from `org-koma-letter-special-contents'"
  303. (cdr (assoc (org-koma-letter--get-custom key)
  304. org-koma-letter-special-contents)))
  305. (defun org-koma-letter--get-custom (value)
  306. "Determines whether a value is nil, a string or a
  307. function (a symobl). If it is a function it it evaluates it."
  308. (when value
  309. (cond ((stringp value) value)
  310. ((functionp value) (funcall value))
  311. ((symbolp value) (symbol-name value))
  312. (t value))))
  313. (defun org-koma-letter--prepare-special-contents-as-macro (a-list &optional keep-newlines no-tag)
  314. "Finds all the components of `org-koma-letter-special-contents'
  315. corresponding to members of the `a-list' and return them as a
  316. string to be formatted. The function is used for inserting
  317. content of speciall headings such as PS.
  318. If keep-newlines is t newlines will not be removed. If no-tag is
  319. is t the content in `org-koma-letter-special-contents' will not
  320. be wrapped in a macro named whatever the members of a-list are called.
  321. "
  322. (let (output)
  323. (dolist (ac* a-list output)
  324. (let*
  325. ((ac (org-koma-letter--get-custom ac*))
  326. (x (org-koma-letter--get-tagged-contents ac)))
  327. (when x
  328. (setq output
  329. (concat
  330. output "\n"
  331. ;; sometimes LaTeX complains about newlines
  332. ;; at the end or beginning of macros. Remove them.
  333. (org-koma-letter--format-string-as-macro
  334. (if keep-newlines x (org-koma-letter--remove-offending-new-lines x))
  335. (unless no-tag ac)))))))))
  336. (defun org-koma-letter--format-string-as-macro (string &optional macro)
  337. "If a macro is given format as string as \"\\macro{string}\" else as \"string\""
  338. (if macro
  339. (format "\\%s{%s}" macro string)
  340. (format "%s" string)))
  341. (defun org-koma-letter--remove-offending-new-lines (string)
  342. "Remove new lines in the begging and end of `string'"
  343. (replace-regexp-in-string "\\`[ \n\t]+\\|[\n\t ]*\\'" "" string))
  344. (defun org-koma-letter--determine-special-value (info key)
  345. "Determine who the letter is to and whom it is from.
  346. oxkoma-letter allows two ways to specify these things. If both
  347. are present return the preferred one as determined by
  348. `org-koma-letter-prefer-special-headings'."
  349. (let* ((plist-alist '((from . :from-address)
  350. (to . :to-address)))
  351. (default-alist `((from ,org-koma-letter-from-address)
  352. (to "\\mbox{}")))
  353. (option-value (plist-get info (cdr-safe (assoc key plist-alist))))
  354. (head-value (org-koma-letter--get-tagged-contents key))
  355. (order (append
  356. (funcall
  357. (if (plist-get info :special-headings)
  358. 'reverse 'identity)
  359. `(,option-value ,head-value))
  360. (cdr-safe (assoc key default-alist))))
  361. tmp
  362. (adr (dolist (x order tmp)
  363. (when (and (not tmp) x)
  364. (setq tmp x)))))
  365. (when adr
  366. (replace-regexp-in-string
  367. "\n" "\\\\\\\\\n"
  368. (org-koma-letter--remove-offending-new-lines adr)))))
  369. ;;; Transcode Functions
  370. ;;;; Export Block
  371. (defun org-koma-letter-export-block (export-block contents info)
  372. "Transcode an EXPORT-BLOCK element into KOMA Scrlttr2 code.
  373. CONTENTS is nil. INFO is a plist used as a communication
  374. channel."
  375. (when (member (org-element-property :type export-block) '("KOMA-LETTER" "LATEX"))
  376. (org-remove-indentation (org-element-property :value export-block))))
  377. ;;;; Export Snippet
  378. (defun org-koma-letter-export-snippet (export-snippet contents info)
  379. "Transcode an EXPORT-SNIPPET object into KOMA Scrlttr2 code.
  380. CONTENTS is nil. INFO is a plist used as a communication
  381. channel."
  382. (when (memq (org-export-snippet-backend export-snippet) '(latex koma-letter))
  383. (org-element-property :value export-snippet)))
  384. ;;;; Keyword
  385. (defun org-koma-letter-keyword (keyword contents info)
  386. "Transcode a KEYWORD element into KOMA Scrlttr2 code.
  387. CONTENTS is nil. INFO is a plist used as a communication
  388. channel."
  389. (let ((key (org-element-property :key keyword))
  390. (value (org-element-property :value keyword)))
  391. ;; Handle specifically BEAMER and TOC (headlines only) keywords.
  392. ;; Otherwise, fallback to `latex' back-end.
  393. (if (equal key "KOMA-LETTER") value
  394. (org-export-with-backend 'latex keyword contents info))))
  395. ;; Headline
  396. (defun org-koma-letter-headline (headline contents info)
  397. "Transcode a HEADLINE element from Org to LaTeX.
  398. CONTENTS holds the contents of the headline. INFO is a plist
  399. holding contextual information.
  400. Note that if a headline is tagged with a tag from
  401. `org-koma-letter-special-tags' it will not be exported, but
  402. stored in `org-koma-letter-special-contents' and included at the
  403. appropriate place."
  404. (let*
  405. ((tags (org-export-get-tags headline info))
  406. (tag* (car tags))
  407. (tag (when tag*
  408. (car (member-ignore-case
  409. tag*
  410. (mapcar 'symbol-name (plist-get info :special-tags)))))))
  411. (if tag
  412. (progn
  413. (push (cons tag contents)
  414. org-koma-letter-special-contents)
  415. nil)
  416. contents)))
  417. ;;;; Template
  418. (defun org-koma-letter-template (contents info)
  419. "Return complete document string after KOMA Scrlttr2 conversion.
  420. CONTENTS is the transcoded contents string. INFO is a plist
  421. holding export options."
  422. ;; FIXME: instead of setq'ing org-koma-letter-special-contents and
  423. ;; callying varioues stuff it might be nice to put a big let* around the templace
  424. ;; as in org-groff...
  425. (concat
  426. ;; Time-stamp.
  427. (and (plist-get info :time-stamp-file)
  428. (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
  429. ;; Document class and packages.
  430. (let ((class (plist-get info :latex-class))
  431. (class-options (plist-get info :latex-class-options)))
  432. (org-element-normalize-string
  433. (let* ((header (nth 1 (assoc class org-latex-classes)))
  434. (document-class-string
  435. (and (stringp header)
  436. (if (not class-options) header
  437. (replace-regexp-in-string
  438. "^[ \t]*\\\\documentclass\\(\\(\\[[^]]*\\]\\)?\\)"
  439. class-options header t nil 1)))))
  440. (if (not document-class-string)
  441. (user-error "Unknown LaTeX class `%s'" class)
  442. (org-latex-guess-babel-language
  443. (org-latex-guess-inputenc
  444. (org-splice-latex-header
  445. document-class-string
  446. org-latex-default-packages-alist ; defined in org.el
  447. org-latex-packages-alist nil ; defined in org.el
  448. (concat (plist-get info :latex-header)
  449. (plist-get info :latex-header-extra))))
  450. info)))))
  451. (let ((lco (plist-get info :lco))
  452. (author (plist-get info :author))
  453. (from-address (org-koma-letter--determine-special-value info 'from))
  454. (phone-number (plist-get info :phone-number))
  455. (email (plist-get info :email))
  456. (signature (plist-get info :signature)))
  457. (concat
  458. ;; Letter Class Option File
  459. (when lco
  460. (let ((lco-files (split-string lco " "))
  461. (lco-def ""))
  462. (dolist (lco-file lco-files lco-def)
  463. (setq lco-def (format "%s\\LoadLetterOption{%s}\n" lco-def lco-file)))
  464. lco-def))
  465. ;; Define "From" data.
  466. (when author (format "\\setkomavar{fromname}{%s}\n"
  467. (org-export-data author info)))
  468. (when from-address (format "\\setkomavar{fromaddress}{%s}\n" from-address))
  469. (when phone-number (format "\\setkomavar{fromphone}{%s}\n" phone-number))
  470. (when email (format "\\setkomavar{fromemail}{%s}\n" email))
  471. (when signature (format "\\setkomavar{signature}{%s}\n" signature))))
  472. ;; Date.
  473. (format "\\date{%s}\n" (org-export-data (org-export-get-date info) info))
  474. ;; Place
  475. (let ((with-place (plist-get info :with-place))
  476. (place (plist-get info :place)))
  477. (when (or place (not with-place))
  478. (format "\\setkomavar{place}{%s}\n" (if with-place place ""))))
  479. ;; KOMA options
  480. (let ((with-backaddress (plist-get info :with-backaddress))
  481. (with-foldmarks (plist-get info :with-foldmarks))
  482. (with-phone (plist-get info :with-phone))
  483. (with-email (plist-get info :with-email)))
  484. (concat
  485. (format "\\KOMAoption{backaddress}{%s}\n" (if with-backaddress "true" "false"))
  486. (format "\\KOMAoption{foldmarks}{%s}\n" (if with-foldmarks with-foldmarks "false"))
  487. (format "\\KOMAoption{fromphone}{%s}\n" (if with-phone "true" "false"))
  488. (format "\\KOMAoption{fromemail}{%s}\n" (if with-email "true" "false"))))
  489. ;; Document start
  490. "\\begin{document}\n\n"
  491. ;; Subject
  492. (let* ((with-subject (plist-get info :with-subject))
  493. (subject-format (cond ((member with-subject '("true" "t" t)) nil)
  494. ((stringp with-subject) (list with-subject))
  495. ((symbolp with-subject)
  496. (list (symbol-name with-subject)))
  497. (t with-subject)))
  498. (subject (org-export-data (plist-get info :title) info))
  499. (l (length subject-format))
  500. (y ""))
  501. (concat
  502. (when (and with-subject subject-format)
  503. (concat
  504. "\\KOMAoption{subject}{"
  505. (apply 'format
  506. (dotimes (x l y)
  507. (setq y (concat (if (> x 0) "%s," "%s") y)))
  508. subject-format) "}\n"))
  509. (when (and subject with-subject)
  510. (format "\\setkomavar{subject}{%s}\n\n" subject))))
  511. ;; Letter start
  512. (format "\\begin{letter}{%%\n%s}\n\n"
  513. (org-koma-letter--determine-special-value info 'to))
  514. ;; Opening.
  515. (format "\\opening{%s}\n\n" (plist-get info :opening))
  516. ;; Letter body.
  517. contents
  518. ;; Closing.
  519. (format "\n\\closing{%s}\n" (plist-get info :closing))
  520. (org-koma-letter--prepare-special-contents-as-macro
  521. (plist-get info :with-after-closing))
  522. ;; Letter end.
  523. "\n\\end{letter}\n"
  524. (org-koma-letter--prepare-special-contents-as-macro
  525. (plist-get info :with-after-letter) t t)
  526. ;; Document end.
  527. "\n\\end{document}"
  528. ))
  529. ;;; Commands
  530. ;;;###autoload
  531. (defun org-koma-letter-export-as-latex
  532. (&optional async subtreep visible-only body-only ext-plist)
  533. "Export current buffer as a KOMA Scrlttr2 letter.
  534. If narrowing is active in the current buffer, only export its
  535. narrowed part.
  536. If a region is active, export that region.
  537. A non-nil optional argument ASYNC means the process should happen
  538. asynchronously. The resulting buffer should be accessible
  539. through the `org-export-stack' interface.
  540. When optional argument SUBTREEP is non-nil, export the sub-tree
  541. at point, extracting information from the headline properties
  542. first.
  543. When optional argument VISIBLE-ONLY is non-nil, don't export
  544. contents of hidden elements.
  545. When optional argument BODY-ONLY is non-nil, only write code
  546. between \"\\begin{letter}\" and \"\\end{letter}\".
  547. EXT-PLIST, when provided, is a proeprty list with external
  548. parameters overriding Org default settings, but still inferior to
  549. file-local settings.
  550. Export is done in a buffer named \"*Org KOMA-LETTER Export*\". It
  551. will be displayed if `org-export-show-temporary-export-buffer' is
  552. non-nil."
  553. (interactive)
  554. (let (org-koma-letter-special-contents)
  555. (if async
  556. (org-export-async-start
  557. (lambda (output)
  558. (with-current-buffer (get-buffer-create "*Org KOMA-LETTER Export*")
  559. (erase-buffer)
  560. (insert output)
  561. (goto-char (point-min))
  562. (LaTeX-mode)
  563. (org-export-add-to-stack (current-buffer) 'koma-letter)))
  564. `(org-export-as 'koma-letter ,subtreep ,visible-only ,body-only
  565. ',ext-plist))
  566. (let ((outbuf (org-export-to-buffer
  567. 'koma-letter "*Org KOMA-LETTER Export*"
  568. subtreep visible-only body-only ext-plist)))
  569. (with-current-buffer outbuf (LaTeX-mode))
  570. (when org-export-show-temporary-export-buffer
  571. (switch-to-buffer-other-window outbuf))))))
  572. ;;;###autoload
  573. (defun org-koma-letter-export-to-latex
  574. (&optional async subtreep visible-only body-only ext-plist)
  575. "Export current buffer as a KOMA Scrlttr2 letter (tex).
  576. If narrowing is active in the current buffer, only export its
  577. narrowed part.
  578. If a region is active, export that region.
  579. A non-nil optional argument ASYNC means the process should happen
  580. asynchronously. The resulting file should be accessible through
  581. the `org-export-stack' interface.
  582. When optional argument SUBTREEP is non-nil, export the sub-tree
  583. at point, extracting information from the headline properties
  584. first.
  585. When optional argument VISIBLE-ONLY is non-nil, don't export
  586. contents of hidden elements.
  587. When optional argument BODY-ONLY is non-nil, only write code
  588. between \"\\begin{letter}\" and \"\\end{letter}\".
  589. EXT-PLIST, when provided, is a property list with external
  590. parameters overriding Org default settings, but still inferior to
  591. file-local settings.
  592. When optional argument PUB-DIR is set, use it as the publishing
  593. directory.
  594. Return output file's name."
  595. (interactive)
  596. (let ((outfile (org-export-output-file-name ".tex" subtreep))
  597. org-koma-letter-special-contents)
  598. (if async
  599. (org-export-async-start
  600. (lambda (f) (org-export-add-to-stack f 'koma-letter))
  601. `(expand-file-name
  602. (org-export-to-file
  603. 'koma-letter ,outfile ,subtreep ,visible-only ,body-only
  604. ',ext-plist)))
  605. (org-export-to-file
  606. 'koma-letter outfile subtreep visible-only body-only ext-plist))))
  607. ;;;###autoload
  608. (defun org-koma-letter-export-to-pdf
  609. (&optional async subtreep visible-only body-only ext-plist)
  610. "Export current buffer as a KOMA Scrlttr2 letter (pdf).
  611. If narrowing is active in the current buffer, only export its
  612. narrowed part.
  613. If a region is active, export that region.
  614. A non-nil optional argument ASYNC means the process should happen
  615. asynchronously. The resulting file should be accessible through
  616. the `org-export-stack' interface.
  617. When optional argument SUBTREEP is non-nil, export the sub-tree
  618. at point, extracting information from the headline properties
  619. first.
  620. When optional argument VISIBLE-ONLY is non-nil, don't export
  621. contents of hidden elements.
  622. When optional argument BODY-ONLY is non-nil, only write code
  623. between \"\\begin{letter}\" and \"\\end{letter}\".
  624. EXT-PLIST, when provided, is a property list with external
  625. parameters overriding Org default settings, but still inferior to
  626. file-local settings.
  627. Return PDF file's name."
  628. (interactive)
  629. (if async
  630. (let ((outfile (org-export-output-file-name ".tex" subtreep)))
  631. (org-export-async-start
  632. (lambda (f) (org-export-add-to-stack f 'koma-letter))
  633. `(expand-file-name
  634. (org-latex-compile
  635. (org-export-to-file
  636. 'koma-letter ,outfile ,subtreep ,visible-only ,body-only
  637. ',ext-plist)))))
  638. (org-latex-compile
  639. (org-koma-letter-export-to-latex
  640. nil subtreep visible-only body-only ext-plist))))
  641. (provide 'ox-koma-letter)
  642. ;;; ox-koma-letter.el ends here