org-koma-letter.el 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. ;;; org-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. ;; GNU Emacs 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. ;; GNU Emacs 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 `e-latex' back-end (see
  27. ;; `org-e-latex-options-alist'), this back-end introduces the
  28. ;; following keywords: "CLOSING" (see `org-koma-letter-closing'),
  29. ;; "FROM_ADDRESS" (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-e-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-e-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 'org-e-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. :version "24.2")
  66. (defcustom org-koma-letter-class-option-file "NF"
  67. "Letter Class Option File."
  68. :group 'org-export-koma-letter
  69. :type 'string)
  70. (defcustom org-koma-letter-closing "See you soon,"
  71. "Koma-Letter's closing, as a string."
  72. :group 'org-export-koma-letter
  73. :type 'string)
  74. (defcustom org-koma-letter-from-address "Somewhere \\ Over the rainbow."
  75. "Sender's address, as a string."
  76. :group 'org-export-koma-letter
  77. :type 'string)
  78. (defcustom org-koma-letter-opening "Dear Sir,"
  79. "Letter's opening, as a string."
  80. :group 'org-export-koma-letter
  81. :type 'string)
  82. (defcustom org-koma-letter-phone-number "00-00-00-00"
  83. "Sender's phone number, as a string."
  84. :group 'org-export-koma-letter
  85. :type 'string)
  86. (defcustom org-koma-letter-signature "\\usekomavar{fromname}"
  87. "String used as the signature."
  88. :group 'org-export-koma-letter
  89. :type 'string)
  90. ;;; Define Back-End
  91. (org-export-define-derived-backend koma-letter e-latex
  92. :options-alist
  93. ((:closing "CLOSING" nil org-koma-letter-closing)
  94. (:from-address "FROM_ADDRESS" nil org-koma-letter-from-address newline)
  95. (:lco "LCO" nil org-koma-letter-class-option-file)
  96. (:opening "OPENING" nil org-koma-letter-opening)
  97. (:phone-number "PHONE_NUMBER" nil org-koma-letter-phone-number)
  98. (:signature "SIGNATURE" nil nil newline)
  99. (:to-address "TO_ADDRESS" nil nil newline))
  100. :translate-alist ((export-block . org-koma-letter-export-block)
  101. (export-snippet . org-koma-letter-export-snippet)
  102. (keyword . org-koma-letter-keyword)
  103. (template . org-koma-letter-template))
  104. :menu-entry
  105. (?k "Export with KOMA Scrlttr2"
  106. ((?K "As TEX buffer" org-koma-letter-export-as-latex)
  107. (?k "As TEX file" org-koma-letter-export-to-latex)
  108. (?p "As PDF file" org-koma-letter-export-to-pdf)
  109. (?O "As PDF file and open"
  110. (lambda (s v b)
  111. (org-open-file (org-koma-letter-export-to-pdf 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) '(e-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 `e-latex' back-end.
  136. (if (equal key "KOMA-LETTER") value
  137. (org-export-with-backend 'e-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-e-latex-classes)))
  152. (document-class-string
  153. (and (stringp header)
  154. (if class-options
  155. (replace-regexp-in-string
  156. "^[ \t]*\\\\documentclass\\(\\[.*?\\]\\)"
  157. class-options header t nil 1)
  158. header))))
  159. (when document-class-string
  160. (org-e-latex--guess-babel-language
  161. (org-e-latex--guess-inputenc
  162. (org-splice-latex-header
  163. document-class-string
  164. org-export-latex-default-packages-alist ; defined in org.el
  165. org-export-latex-packages-alist nil ; defined in org.el
  166. (plist-get info :latex-header-extra)))
  167. info)))))
  168. ;; Define "From" data.
  169. (format "\\setkomavar{fromname}{%s}\n"
  170. (org-export-data (plist-get info :author) info))
  171. (format "\\setkomavar{fromaddress}{%s}\n" (plist-get info :from-address))
  172. (format "\\setkomavar{signature}{%s}\n" (plist-get info :signature))
  173. (format "\\setkomavar{fromemail}{%s}\n"
  174. (org-export-data (plist-get info :email) info))
  175. (format "\\setkomavar{fromphone}{%s}\n" (plist-get info :phone-number))
  176. ;; Date.
  177. (format "\\date{%s}\n" (org-export-data (plist-get info :date) info))
  178. ;; Letter Class Option File
  179. (format "\\LoadLetterOption{%s}\n" (plist-get info :lco))
  180. ;; Letter start.
  181. "\\begin{document}\n\n"
  182. (format "\\setkomavar{subject}{%s}\n\n"
  183. (org-export-data (plist-get info :title) info))
  184. (format "\\begin{letter}{%%\n%s}\n\n" (or (plist-get info :to-address) "no address given"))
  185. ;; Opening.
  186. (format "\\opening{%s}\n\n" (plist-get info :opening))
  187. ;; Letter body.
  188. contents
  189. ;; Closing.
  190. (format "\n\\closing{%s}\n\n" (plist-get info :closing))
  191. ;; Letter end.
  192. "\\end{letter}\n\\end{document}"))
  193. ;;; Commands
  194. ;;;###autoload
  195. (defun org-koma-letter-export-as-latex
  196. (&optional subtreep visible-only body-only ext-plist)
  197. "Export current buffer as a KOMA Scrlttr2 letter.
  198. If narrowing is active in the current buffer, only export its
  199. narrowed part.
  200. If a region is active, export that region.
  201. When optional argument SUBTREEP is non-nil, export the sub-tree
  202. at point, extracting information from the headline properties
  203. first.
  204. When optional argument VISIBLE-ONLY is non-nil, don't export
  205. contents of hidden elements.
  206. When optional argument BODY-ONLY is non-nil, only write code
  207. between \"\\begin{letter}\" and \"\\end{letter}\".
  208. EXT-PLIST, when provided, is a property list with external
  209. parameters overriding Org default settings, but still inferior to
  210. file-local settings.
  211. Export is done in a buffer named \"*Org KOMA-LETTER Export*\". It
  212. will be displayed if `org-export-show-temporary-export-buffer' is
  213. non-nil."
  214. (interactive)
  215. (let ((outbuf (org-export-to-buffer
  216. 'koma-letter "*Org KOMA-LETTER Export*"
  217. subtreep visible-only body-only ext-plist)))
  218. (with-current-buffer outbuf (LaTeX-mode))
  219. (when org-export-show-temporary-export-buffer
  220. (switch-to-buffer-other-window outbuf))))
  221. ;;;###autoload
  222. (defun org-koma-letter-export-to-latex
  223. (&optional subtreep visible-only body-only ext-plist pub-dir)
  224. "Export current buffer as a KOMA Scrlttr2 letter (tex).
  225. If narrowing is active in the current buffer, only export its
  226. narrowed part.
  227. If a region is active, export that region.
  228. When optional argument SUBTREEP is non-nil, export the sub-tree
  229. at point, extracting information from the headline properties
  230. first.
  231. When optional argument VISIBLE-ONLY is non-nil, don't export
  232. contents of hidden elements.
  233. When optional argument BODY-ONLY is non-nil, only write code
  234. between \"\\begin{letter}\" and \"\\end{letter}\".
  235. EXT-PLIST, when provided, is a property list with external
  236. parameters overriding Org default settings, but still inferior to
  237. file-local settings.
  238. When optional argument PUB-DIR is set, use it as the publishing
  239. directory.
  240. Return output file's name."
  241. (interactive)
  242. (let ((outfile (org-export-output-file-name ".tex" subtreep pub-dir)))
  243. (org-export-to-file
  244. 'koma-letter outfile subtreep visible-only body-only ext-plist)))
  245. ;;;###autoload
  246. (defun org-koma-letter-export-to-pdf
  247. (&optional subtreep visible-only body-only ext-plist pub-dir)
  248. "Export current buffer as a KOMA Scrlttr2 letter (pdf).
  249. If narrowing is active in the current buffer, only export its
  250. narrowed part.
  251. If a region is active, export that region.
  252. When optional argument SUBTREEP is non-nil, export the sub-tree
  253. at point, extracting information from the headline properties
  254. first.
  255. When optional argument VISIBLE-ONLY is non-nil, don't export
  256. contents of hidden elements.
  257. When optional argument BODY-ONLY is non-nil, only write code
  258. between \"\\begin{letter}\" and \"\\end{letter}\".
  259. EXT-PLIST, when provided, is a property list with external
  260. parameters overriding Org default settings, but still inferior to
  261. file-local settings.
  262. When optional argument PUB-DIR is set, use it as the publishing
  263. directory.
  264. Return PDF file's name."
  265. (interactive)
  266. (org-e-latex-compile
  267. (org-koma-letter-export-to-latex
  268. subtreep visible-only body-only ext-plist pub-dir)))
  269. (provide 'org-koma-letter)
  270. ;;; org-koma-letter.el ends here