ox-koma-letter.el 25 KB

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