ox-koma-letter.el 26 KB

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