ox-koma-letter.el 16 KB

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