ox-koma-letter.el 16 KB

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