ox-koma-letter.el 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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. ;; Keywords: org, wp, tex
  6. ;; This program is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; This program is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;;
  18. ;; This library implements a KOMA Scrlttr2 back-end, derived from the
  19. ;; LaTeX one.
  20. ;;
  21. ;; Depending on the desired output format, three commands are provided
  22. ;; for export: `org-koma-letter-export-as-latex' (temporary buffer),
  23. ;; `org-koma-letter-export-to-latex' ("tex" file) and
  24. ;; `org-koma-letter-export-to-pdf' ("pdf" file).
  25. ;;
  26. ;; On top of buffer keywords supported by `latex' back-end (see
  27. ;; `org-latex-options-alist'), this back-end introduces the following
  28. ;; keywords: "CLOSING" (see `org-koma-letter-closing'), "FROM_ADDRESS"
  29. ;; (see `org-koma-letter-from-address'), "LCO" (see
  30. ;; `org-koma-letter-class-option-file'), "OPENING" (see
  31. ;; `org-koma-letter-opening'), "PHONE_NUMBER" (see
  32. ;; `org-koma-letter-phone-number'), "SIGNATURE" (see
  33. ;; `org-koma-letter-signature') and "TO_ADDRESS".
  34. ;;
  35. ;; You will need to add an appropriate association in
  36. ;; `org-latex-classes' in order to use the KOMA Scrlttr2 class. For
  37. ;; example, you can use the following code:
  38. ;;
  39. ;; (add-to-list 'org-latex-classes
  40. ;; '("my-letter"
  41. ;; "\\documentclass\[%
  42. ;; DIV=14,
  43. ;; fontsize=12pt,
  44. ;; parskip=half,
  45. ;; subject=titled,
  46. ;; backaddress=false,
  47. ;; fromalign=left,
  48. ;; fromemail=true,
  49. ;; fromphone=true\]\{scrlttr2\}
  50. ;; \[DEFAULT-PACKAGES]
  51. ;; \[PACKAGES]
  52. ;; \[EXTRA]"))
  53. ;;
  54. ;; Then, in your Org document, be sure to require the proper class
  55. ;; with :
  56. ;;
  57. ;; #+LATEX_CLASS: my-letter
  58. ;;; Code:
  59. (require 'ox-latex)
  60. ;;; User-Configurable Variables
  61. (defgroup org-export-koma-letter nil
  62. "Options for exporting to KOMA scrlttr2 class in LaTeX export."
  63. :tag "Org Koma-Letter"
  64. :group 'org-export)
  65. (defcustom org-koma-letter-class-option-file "NF"
  66. "Letter Class Option File."
  67. :group 'org-export-koma-letter
  68. :type 'string)
  69. (defcustom org-koma-letter-sender nil
  70. "Sender's name, as a string."
  71. :group 'org-export-koma-letter
  72. :type 'string)
  73. (defcustom org-koma-letter-from-address nil
  74. "Sender's address, as a string."
  75. :group 'org-export-koma-letter
  76. :type 'string)
  77. (defcustom org-koma-letter-phone-number nil
  78. "Sender's phone number, as a string."
  79. :group 'org-export-koma-letter
  80. :type 'string)
  81. (defcustom org-koma-letter-email nil
  82. "Sender's email, as a string."
  83. :group 'org-export-koma-letter
  84. :type 'string)
  85. (defcustom org-koma-letter-place nil
  86. "Place from which the letter is sent."
  87. :group 'org-export-koma-letter
  88. :type 'string)
  89. (defcustom org-koma-letter-opening nil
  90. "Letter's opening, as a string."
  91. :group 'org-export-koma-letter
  92. :type 'string)
  93. (defcustom org-koma-letter-closing nil
  94. "Koma-Letter's closing, as a string."
  95. :group 'org-export-koma-letter
  96. :type 'string)
  97. (defcustom org-koma-letter-signature "\\usekomavar{fromname}"
  98. "String used as the signature."
  99. :group 'org-export-koma-letter
  100. :type 'string)
  101. (defcustom org-koma-letter-use-subject "untitled"
  102. "Use the title as the letter's subject."
  103. :group 'org-export-koma-letter
  104. :type 'string)
  105. (defcustom org-koma-letter-use-backaddress t
  106. "Print return address in small line above to address."
  107. :group 'org-export-koma-letter
  108. :type 'boolean)
  109. (defcustom org-koma-letter-use-foldmarks "true"
  110. "Configure appearence of fold marks.
  111. Accepts any valid value for the KOMA-Script `foldmarks' option.
  112. Use `foldmarks:true' to activate default fold marks or
  113. `foldmarks:nil' to deactivate fold marks."
  114. :group 'org-export-koma-letter
  115. :type 'string)
  116. (defcustom org-koma-letter-use-phone t
  117. "Print sender's phone number."
  118. :group 'org-export-koma-letter
  119. :type 'boolean)
  120. (defcustom org-koma-letter-use-email t
  121. "Print sender's email address."
  122. :group 'org-export-koma-letter
  123. :type 'boolean)
  124. (defcustom org-koma-letter-use-place t
  125. "Print the letter's place next to the date."
  126. :group 'org-export-koma-letter
  127. :type 'boolean)
  128. ;;; Define Back-End
  129. (org-export-define-derived-backend 'koma-letter 'latex
  130. :options-alist
  131. '((:lco "LCO" nil org-koma-letter-class-option-file)
  132. (:sender "SENDER" nil org-koma-letter-sender newline)
  133. (:from-address "FROM_ADDRESS" nil org-koma-letter-from-address newline)
  134. (:phone-number "PHONE_NUMBER" nil org-koma-letter-phone-number)
  135. (:email "EMAIL" nil org-koma-letter-email)
  136. (:to-address "TO_ADDRESS" nil nil newline)
  137. (:place "PLACE" nil org-koma-letter-place)
  138. (:opening "OPENING" nil org-koma-letter-opening)
  139. (:closing "CLOSING" nil org-koma-letter-closing)
  140. (:signature "SIGNATURE" nil org-koma-letter-signature newline)
  141. (:with-backaddress nil "backaddress" org-koma-letter-use-backaddress)
  142. (:with-foldmarks nil "foldmarks" org-koma-letter-use-foldmarks)
  143. (:with-phone nil "phone" org-koma-letter-use-phone)
  144. (:with-email nil "email" org-koma-letter-use-email)
  145. (:with-place nil "place" org-koma-letter-use-place)
  146. (:with-subject nil "subject" org-koma-letter-use-subject))
  147. :translate-alist '((export-block . org-koma-letter-export-block)
  148. (export-snippet . org-koma-letter-export-snippet)
  149. (keyword . org-koma-letter-keyword)
  150. (template . org-koma-letter-template))
  151. :menu-entry
  152. '(?k "Export with KOMA Scrlttr2"
  153. ((?L "As LaTeX buffer" org-koma-letter-export-as-latex)
  154. (?l "As LaTeX file" org-koma-letter-export-to-latex)
  155. (?p "As PDF file" org-koma-letter-export-to-pdf)
  156. (?o "As PDF file and open"
  157. (lambda (a s v b)
  158. (if a (org-koma-letter-export-to-pdf t s v b)
  159. (org-open-file (org-koma-letter-export-to-pdf nil s v b))))))))
  160. ;;; Transcode Functions
  161. ;;;; Export Block
  162. (defun org-koma-letter-export-block (export-block contents info)
  163. "Transcode an EXPORT-BLOCK element into KOMA Scrlttr2 code.
  164. CONTENTS is nil. INFO is a plist used as a communication
  165. channel."
  166. (when (member (org-element-property :type export-block) '("KOMA-LETTER" "LATEX"))
  167. (org-remove-indentation (org-element-property :value export-block))))
  168. ;;;; Export Snippet
  169. (defun org-koma-letter-export-snippet (export-snippet contents info)
  170. "Transcode an EXPORT-SNIPPET object into KOMA Scrlttr2 code.
  171. CONTENTS is nil. INFO is a plist used as a communication
  172. channel."
  173. (when (memq (org-export-snippet-backend export-snippet) '(latex koma-letter))
  174. (org-element-property :value export-snippet)))
  175. ;;;; Keyword
  176. (defun org-koma-letter-keyword (keyword contents info)
  177. "Transcode a KEYWORD element into KOMA Scrlttr2 code.
  178. CONTENTS is nil. INFO is a plist used as a communication
  179. channel."
  180. (let ((key (org-element-property :key keyword))
  181. (value (org-element-property :value keyword)))
  182. ;; Handle specifically BEAMER and TOC (headlines only) keywords.
  183. ;; Otherwise, fallback to `latex' back-end.
  184. (if (equal key "KOMA-LETTER") value
  185. (org-export-with-backend 'latex keyword contents info))))
  186. ;;;; Template
  187. (defun org-koma-letter-template (contents info)
  188. "Return complete document string after KOMA Scrlttr2 conversion.
  189. CONTENTS is the transcoded contents string. INFO is a plist
  190. holding export options."
  191. (concat
  192. ;; Time-stamp.
  193. (and (plist-get info :time-stamp-file)
  194. (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
  195. ;; Document class and packages.
  196. (let ((class (plist-get info :latex-class))
  197. (class-options (plist-get info :latex-class-options)))
  198. (org-element-normalize-string
  199. (let* ((header (nth 1 (assoc class org-latex-classes)))
  200. (document-class-string
  201. (and (stringp header)
  202. (if (not class-options) header
  203. (replace-regexp-in-string
  204. "^[ \t]*\\\\documentclass\\(\\(\\[[^]]*\\]\\)?\\)"
  205. class-options header t nil 1)))))
  206. (if (not document-class-string)
  207. (user-error "Unknown LaTeX class `%s'")
  208. (org-latex-guess-babel-language
  209. (org-latex-guess-inputenc
  210. (org-splice-latex-header
  211. document-class-string
  212. org-latex-default-packages-alist ; defined in org.el
  213. org-latex-packages-alist nil ; defined in org.el
  214. (concat (plist-get info :latex-header)
  215. (plist-get info :latex-header-extra))))
  216. info)))))
  217. (let ((lco (plist-get info :lco))
  218. (sender (plist-get info :sender))
  219. (from-address (plist-get info :from-address))
  220. (phone-number (plist-get info :phone-number))
  221. (email (plist-get info :email))
  222. (signature (plist-get info :signature)))
  223. (concat
  224. ;; Letter Class Option File
  225. (when lco
  226. (let ((lco-files (split-string lco " "))
  227. (lco-def ""))
  228. (dolist (lco-file lco-files lco-def)
  229. (setq lco-def (format "%s\\LoadLetterOption{%s}\n" lco-def lco-file)))
  230. lco-def))
  231. ;; Define "From" data.
  232. (when sender (format "\\setkomavar{fromname}{%s}\n" sender))
  233. (when from-address (format "\\setkomavar{fromaddress}{%s}\n" from-address))
  234. (when phone-number (format "\\setkomavar{fromphone}{%s}\n" phone-number))
  235. (when email (format "\\setkomavar{fromemail}{%s}\n" email))
  236. (when signature (format "\\setkomavar{signature}{%s}\n" signature))))
  237. ;; Date.
  238. (format "\\date{%s}\n" (org-export-data (org-export-get-date info) info))
  239. ;; Place
  240. (let ((with-place (plist-get info :with-place))
  241. (place (plist-get info :place)))
  242. (when (or place (not with-place))
  243. (format "\\setkomavar{place}{%s}\n" (if with-place place ""))))
  244. ;; KOMA options
  245. (let ((with-backaddress (plist-get info :with-backaddress))
  246. (with-foldmarks (plist-get info :with-foldmarks))
  247. (with-phone (plist-get info :with-phone))
  248. (with-email (plist-get info :with-email)))
  249. (concat
  250. (format "\\KOMAoption{backaddress}{%s}\n" (if with-backaddress "true" "false"))
  251. (format "\\KOMAoption{foldmarks}{%s}\n" (if with-foldmarks with-foldmarks "false"))
  252. (format "\\KOMAoption{fromphone}{%s}\n" (if with-phone "true" "false"))
  253. (format "\\KOMAoption{fromemail}{%s}\n" (if with-email "true" "false"))))
  254. ;; Document start
  255. "\\begin{document}\n\n"
  256. ;; Subject
  257. (let ((with-subject (plist-get info :with-subject)))
  258. (when with-subject
  259. (concat
  260. (format "\\KOMAoption{subject}{%s}\n" with-subject)
  261. (format "\\setkomavar{subject}{%s}\n\n"
  262. (org-export-data (plist-get info :title) info)))))
  263. ;; Letter start
  264. (format "\\begin{letter}{%%\n%s}\n\n"
  265. (or (plist-get info :to-address) "no address given"))
  266. ;; Opening.
  267. (format "\\opening{%s}\n\n" (plist-get info :opening))
  268. ;; Letter body.
  269. contents
  270. ;; Closing.
  271. (format "\n\\closing{%s}\n\n" (plist-get info :closing))
  272. ;; Letter end.
  273. "\\end{letter}\n\\end{document}"))
  274. ;;; Commands
  275. ;;;###autoload
  276. (defun org-koma-letter-export-as-latex
  277. (&optional async subtreep visible-only body-only ext-plist)
  278. "Export current buffer as a KOMA Scrlttr2 letter.
  279. If narrowing is active in the current buffer, only export its
  280. narrowed part.
  281. If a region is active, export that region.
  282. A non-nil optional argument ASYNC means the process should happen
  283. asynchronously. The resulting buffer should be accessible
  284. through the `org-export-stack' interface.
  285. When optional argument SUBTREEP is non-nil, export the sub-tree
  286. at point, extracting information from the headline properties
  287. first.
  288. When optional argument VISIBLE-ONLY is non-nil, don't export
  289. contents of hidden elements.
  290. When optional argument BODY-ONLY is non-nil, only write code
  291. between \"\\begin{letter}\" and \"\\end{letter}\".
  292. EXT-PLIST, when provided, is a property list with external
  293. parameters overriding Org default settings, but still inferior to
  294. file-local settings.
  295. Export is done in a buffer named \"*Org KOMA-LETTER Export*\". It
  296. will be displayed if `org-export-show-temporary-export-buffer' is
  297. non-nil."
  298. (interactive)
  299. (if async
  300. (org-export-async-start
  301. (lambda (output)
  302. (with-current-buffer (get-buffer-create "*Org KOMA-LETTER Export*")
  303. (erase-buffer)
  304. (insert output)
  305. (goto-char (point-min))
  306. (LaTeX-mode)
  307. (org-export-add-to-stack (current-buffer) 'koma-letter)))
  308. `(org-export-as 'koma-letter ,subtreep ,visible-only ,body-only
  309. ',ext-plist))
  310. (let ((outbuf (org-export-to-buffer
  311. 'koma-letter "*Org KOMA-LETTER Export*"
  312. subtreep visible-only body-only ext-plist)))
  313. (with-current-buffer outbuf (LaTeX-mode))
  314. (when org-export-show-temporary-export-buffer
  315. (switch-to-buffer-other-window outbuf)))))
  316. ;;;###autoload
  317. (defun org-koma-letter-export-to-latex
  318. (&optional async subtreep visible-only body-only ext-plist)
  319. "Export current buffer as a KOMA Scrlttr2 letter (tex).
  320. If narrowing is active in the current buffer, only export its
  321. narrowed part.
  322. If a region is active, export that region.
  323. A non-nil optional argument ASYNC means the process should happen
  324. asynchronously. The resulting file should be accessible through
  325. the `org-export-stack' interface.
  326. When optional argument SUBTREEP is non-nil, export the sub-tree
  327. at point, extracting information from the headline properties
  328. first.
  329. When optional argument VISIBLE-ONLY is non-nil, don't export
  330. contents of hidden elements.
  331. When optional argument BODY-ONLY is non-nil, only write code
  332. between \"\\begin{letter}\" and \"\\end{letter}\".
  333. EXT-PLIST, when provided, is a property list with external
  334. parameters overriding Org default settings, but still inferior to
  335. file-local settings.
  336. When optional argument PUB-DIR is set, use it as the publishing
  337. directory.
  338. Return output file's name."
  339. (interactive)
  340. (let ((outfile (org-export-output-file-name ".tex" subtreep)))
  341. (if async
  342. (org-export-async-start
  343. (lambda (f) (org-export-add-to-stack f 'koma-letter))
  344. `(expand-file-name
  345. (org-export-to-file
  346. 'koma-letter ,outfile ,subtreep ,visible-only ,body-only
  347. ',ext-plist)))
  348. (org-export-to-file
  349. 'koma-letter outfile subtreep visible-only body-only ext-plist))))
  350. ;;;###autoload
  351. (defun org-koma-letter-export-to-pdf
  352. (&optional async subtreep visible-only body-only ext-plist)
  353. "Export current buffer as a KOMA Scrlttr2 letter (pdf).
  354. If narrowing is active in the current buffer, only export its
  355. narrowed part.
  356. If a region is active, export that region.
  357. A non-nil optional argument ASYNC means the process should happen
  358. asynchronously. The resulting file should be accessible through
  359. the `org-export-stack' interface.
  360. When optional argument SUBTREEP is non-nil, export the sub-tree
  361. at point, extracting information from the headline properties
  362. first.
  363. When optional argument VISIBLE-ONLY is non-nil, don't export
  364. contents of hidden elements.
  365. When optional argument BODY-ONLY is non-nil, only write code
  366. between \"\\begin{letter}\" and \"\\end{letter}\".
  367. EXT-PLIST, when provided, is a property list with external
  368. parameters overriding Org default settings, but still inferior to
  369. file-local settings.
  370. Return PDF file's name."
  371. (interactive)
  372. (if async
  373. (let ((outfile (org-export-output-file-name ".tex" subtreep)))
  374. (org-export-async-start
  375. (lambda (f) (org-export-add-to-stack f 'koma-letter))
  376. `(expand-file-name
  377. (org-latex-compile
  378. (org-export-to-file
  379. 'koma-letter ,outfile ,subtreep ,visible-only ,body-only
  380. ',ext-plist)))))
  381. (org-latex-compile
  382. (org-koma-letter-export-to-latex
  383. nil subtreep visible-only body-only ext-plist))))
  384. (provide 'ox-koma-letter)
  385. ;;; ox-koma-letter.el ends here