ox-koma-letter.el 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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-closing "See you soon,"
  70. "Koma-Letter's closing, as a string."
  71. :group 'org-export-koma-letter
  72. :type 'string)
  73. (defcustom org-koma-letter-from-address "Somewhere \\ Over the rainbow."
  74. "Sender's address, as a string."
  75. :group 'org-export-koma-letter
  76. :type 'string)
  77. (defcustom org-koma-letter-opening "Dear Sir,"
  78. "Letter's opening, as a string."
  79. :group 'org-export-koma-letter
  80. :type 'string)
  81. (defcustom org-koma-letter-phone-number "00-00-00-00"
  82. "Sender's phone number, as a string."
  83. :group 'org-export-koma-letter
  84. :type 'string)
  85. (defcustom org-koma-letter-signature "\\usekomavar{fromname}"
  86. "String used as the signature."
  87. :group 'org-export-koma-letter
  88. :type 'string)
  89. ;;; Define Back-End
  90. (org-export-define-derived-backend koma-letter latex
  91. :options-alist
  92. ((:closing "CLOSING" nil org-koma-letter-closing)
  93. (:from-address "FROM_ADDRESS" nil org-koma-letter-from-address newline)
  94. (:lco "LCO" nil org-koma-letter-class-option-file)
  95. (:opening "OPENING" nil org-koma-letter-opening)
  96. (:phone-number "PHONE_NUMBER" nil org-koma-letter-phone-number)
  97. (:signature "SIGNATURE" nil nil newline)
  98. (:to-address "TO_ADDRESS" nil nil newline))
  99. :translate-alist ((export-block . org-koma-letter-export-block)
  100. (export-snippet . org-koma-letter-export-snippet)
  101. (keyword . org-koma-letter-keyword)
  102. (template . org-koma-letter-template))
  103. :menu-entry
  104. (?k "Export with KOMA Scrlttr2"
  105. ((?K "As LaTeX buffer" org-koma-letter-export-as-latex)
  106. (?k "As LaTeX file" org-koma-letter-export-to-latex)
  107. (?p "As PDF file" org-koma-letter-export-to-pdf)
  108. (?O "As PDF file and open"
  109. (lambda (a s v b)
  110. (if a (org-koma-letter-export-to-pdf t s v b)
  111. (org-open-file (org-koma-letter-export-to-pdf nil s v b))))))))
  112. ;;; Transcode Functions
  113. ;;;; Export Block
  114. (defun org-koma-letter-export-block (export-block contents info)
  115. "Transcode an EXPORT-BLOCK element into KOMA Scrlttr2 code.
  116. CONTENTS is nil. INFO is a plist used as a communication
  117. channel."
  118. (when (member (org-element-property :type export-block) '("KOMA-LETTER" "LATEX"))
  119. (org-remove-indentation (org-element-property :value export-block))))
  120. ;;;; Export Snippet
  121. (defun org-koma-letter-export-snippet (export-snippet contents info)
  122. "Transcode an EXPORT-SNIPPET object into KOMA Scrlttr2 code.
  123. CONTENTS is nil. INFO is a plist used as a communication
  124. channel."
  125. (when (memq (org-export-snippet-backend export-snippet) '(latex koma-letter))
  126. (org-element-property :value export-snippet)))
  127. ;;;; Keyword
  128. (defun org-koma-letter-keyword (keyword contents info)
  129. "Transcode a KEYWORD element into KOMA Scrlttr2 code.
  130. CONTENTS is nil. INFO is a plist used as a communication
  131. channel."
  132. (let ((key (org-element-property :key keyword))
  133. (value (org-element-property :value keyword)))
  134. ;; Handle specifically BEAMER and TOC (headlines only) keywords.
  135. ;; Otherwise, fallback to `latex' back-end.
  136. (if (equal key "KOMA-LETTER") value
  137. (org-export-with-backend 'latex keyword contents info))))
  138. ;;;; Template
  139. (defun org-koma-letter-template (contents info)
  140. "Return complete document string after KOMA Scrlttr2 conversion.
  141. CONTENTS is the transcoded contents string. INFO is a plist
  142. holding export options."
  143. (concat
  144. ;; Time-stamp.
  145. (and (plist-get info :time-stamp-file)
  146. (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
  147. ;; Document class and packages.
  148. (let ((class (plist-get info :latex-class))
  149. (class-options (plist-get info :latex-class-options)))
  150. (org-element-normalize-string
  151. (let* ((header (nth 1 (assoc class org-latex-classes)))
  152. (document-class-string
  153. (and (stringp header)
  154. (if (not class-options) header
  155. (replace-regexp-in-string
  156. "^[ \t]*\\\\documentclass\\(\\(\\[[^]]*\\]\\)?\\)"
  157. class-options header t nil 1)))))
  158. (if (not document-class-string)
  159. (user-error "Unknown LaTeX class `%s'")
  160. (org-latex-guess-babel-language
  161. (org-latex-guess-inputenc
  162. (org-splice-latex-header
  163. document-class-string
  164. org-latex-default-packages-alist ; defined in org.el
  165. org-latex-packages-alist nil ; defined in org.el
  166. (concat (plist-get info :latex-header)
  167. (plist-get info :latex-header-extra))))
  168. info)))))
  169. ;; Define "From" data.
  170. (format "\\setkomavar{fromname}{%s}\n"
  171. (org-export-data (plist-get info :author) info))
  172. (format "\\setkomavar{fromaddress}{%s}\n" (plist-get info :from-address))
  173. (format "\\setkomavar{signature}{%s}\n" (plist-get info :signature))
  174. (format "\\setkomavar{fromemail}{%s}\n"
  175. (org-export-data (plist-get info :email) info))
  176. (format "\\setkomavar{fromphone}{%s}\n" (plist-get info :phone-number))
  177. ;; Date.
  178. (format "\\date{%s}\n" (org-export-data (plist-get info :date) info))
  179. ;; Letter Class Option File
  180. (format "\\LoadLetterOption{%s}\n" (plist-get info :lco))
  181. ;; Letter start.
  182. "\\begin{document}\n\n"
  183. (format "\\setkomavar{subject}{%s}\n\n"
  184. (org-export-data (plist-get info :title) info))
  185. (format "\\begin{letter}{%%\n%s}\n\n"
  186. (or (plist-get info :to-address) "no address given"))
  187. ;; Opening.
  188. (format "\\opening{%s}\n\n" (plist-get info :opening))
  189. ;; Letter body.
  190. contents
  191. ;; Closing.
  192. (format "\n\\closing{%s}\n\n" (plist-get info :closing))
  193. ;; Letter end.
  194. "\\end{letter}\n\\end{document}"))
  195. ;;; Commands
  196. ;;;###autoload
  197. (defun org-koma-letter-export-as-latex
  198. (&optional async subtreep visible-only body-only ext-plist)
  199. "Export current buffer as a KOMA Scrlttr2 letter.
  200. If narrowing is active in the current buffer, only export its
  201. narrowed part.
  202. If a region is active, export that region.
  203. A non-nil optional argument ASYNC means the process should happen
  204. asynchronously. The resulting buffer should be accessible
  205. through the `org-export-stack' interface.
  206. When optional argument SUBTREEP is non-nil, export the sub-tree
  207. at point, extracting information from the headline properties
  208. first.
  209. When optional argument VISIBLE-ONLY is non-nil, don't export
  210. contents of hidden elements.
  211. When optional argument BODY-ONLY is non-nil, only write code
  212. between \"\\begin{letter}\" and \"\\end{letter}\".
  213. EXT-PLIST, when provided, is a property list with external
  214. parameters overriding Org default settings, but still inferior to
  215. file-local settings.
  216. Export is done in a buffer named \"*Org KOMA-LETTER Export*\". It
  217. will be displayed if `org-export-show-temporary-export-buffer' is
  218. non-nil."
  219. (interactive)
  220. (if async
  221. (org-export-async-start
  222. (lambda (output)
  223. (with-current-buffer (get-buffer-create "*Org KOMA-LETTER Export*")
  224. (erase-buffer)
  225. (insert output)
  226. (goto-char (point-min))
  227. (LaTeX-mode)
  228. (org-export-add-to-stack (current-buffer) 'koma-letter)))
  229. `(org-export-as 'koma-letter ,subtreep ,visible-only ,body-only
  230. ',ext-plist))
  231. (let ((outbuf (org-export-to-buffer
  232. 'koma-letter "*Org KOMA-LETTER Export*"
  233. subtreep visible-only body-only ext-plist)))
  234. (with-current-buffer outbuf (LaTeX-mode))
  235. (when org-export-show-temporary-export-buffer
  236. (switch-to-buffer-other-window outbuf)))))
  237. ;;;###autoload
  238. (defun org-koma-letter-export-to-latex
  239. (&optional async subtreep visible-only body-only ext-plist)
  240. "Export current buffer as a KOMA Scrlttr2 letter (tex).
  241. If narrowing is active in the current buffer, only export its
  242. narrowed part.
  243. If a region is active, export that region.
  244. A non-nil optional argument ASYNC means the process should happen
  245. asynchronously. The resulting file should be accessible through
  246. the `org-export-stack' interface.
  247. When optional argument SUBTREEP is non-nil, export the sub-tree
  248. at point, extracting information from the headline properties
  249. first.
  250. When optional argument VISIBLE-ONLY is non-nil, don't export
  251. contents of hidden elements.
  252. When optional argument BODY-ONLY is non-nil, only write code
  253. between \"\\begin{letter}\" and \"\\end{letter}\".
  254. EXT-PLIST, when provided, is a property list with external
  255. parameters overriding Org default settings, but still inferior to
  256. file-local settings.
  257. When optional argument PUB-DIR is set, use it as the publishing
  258. directory.
  259. Return output file's name."
  260. (interactive)
  261. (let ((outfile (org-export-output-file-name ".tex" subtreep)))
  262. (if async
  263. (org-export-async-start
  264. (lambda (f) (org-export-add-to-stack f 'koma-letter))
  265. `(expand-file-name
  266. (org-export-to-file
  267. 'koma-letter ,outfile ,subtreep ,visible-only ,body-only
  268. ',ext-plist)))
  269. (org-export-to-file
  270. 'koma-letter outfile subtreep visible-only body-only ext-plist))))
  271. ;;;###autoload
  272. (defun org-koma-letter-export-to-pdf
  273. (&optional async subtreep visible-only body-only ext-plist)
  274. "Export current buffer as a KOMA Scrlttr2 letter (pdf).
  275. If narrowing is active in the current buffer, only export its
  276. narrowed part.
  277. If a region is active, export that region.
  278. A non-nil optional argument ASYNC means the process should happen
  279. asynchronously. The resulting file should be accessible through
  280. the `org-export-stack' interface.
  281. When optional argument SUBTREEP is non-nil, export the sub-tree
  282. at point, extracting information from the headline properties
  283. first.
  284. When optional argument VISIBLE-ONLY is non-nil, don't export
  285. contents of hidden elements.
  286. When optional argument BODY-ONLY is non-nil, only write code
  287. between \"\\begin{letter}\" and \"\\end{letter}\".
  288. EXT-PLIST, when provided, is a property list with external
  289. parameters overriding Org default settings, but still inferior to
  290. file-local settings.
  291. Return PDF file's name."
  292. (interactive)
  293. (if async
  294. (let ((outfile (org-export-output-file-name ".tex" subtreep)))
  295. (org-export-async-start
  296. (lambda (f) (org-export-add-to-stack f 'koma-letter))
  297. `(expand-file-name
  298. (org-latex-compile
  299. (org-export-to-file
  300. 'koma-letter ,outfile ,subtreep ,visible-only ,body-only
  301. ',ext-plist)))))
  302. (org-latex-compile
  303. (org-koma-letter-export-to-latex
  304. nil subtreep visible-only body-only ext-plist))))
  305. (provide 'ox-koma-letter)
  306. ;;; ox-koma-letter.el ends here