export back-ends: Remove useless pub-dir argument from export commands
[org-mode.git] / contrib / lisp / org-koma-letter.el
blobd82b78e7e6a669b8b8e7cee58e082e3aa25f51a1
1 ;;; org-koma-letter.el --- KOMA Scrlttr2 Back-End for Org Export Engine
3 ;; Copyright (C) 2007-2012 Free Software Foundation, Inc.
5 ;; Author: Nicolas Goaziou <n.goaziou AT gmail DOT com>
6 ;; Alan Schmitt <alan.schmitt AT polytechnique DOT org>
7 ;; Keywords: org, wp, tex
9 ;; GNU Emacs is free software: you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
22 ;;; Commentary:
24 ;; This library implements a KOMA Scrlttr2 back-end, derived from the
25 ;; LaTeX one.
27 ;; Depending on the desired output format, three commands are provided
28 ;; for export: `org-koma-letter-export-as-latex' (temporary buffer),
29 ;; `org-koma-letter-export-to-latex' ("tex" file) and
30 ;; `org-koma-letter-export-to-pdf' ("pdf" file).
32 ;; On top of buffer keywords supported by `e-latex' back-end (see
33 ;; `org-e-latex-options-alist'), this back-end introduces the
34 ;; following keywords: "CLOSING" (see `org-koma-letter-closing'),
35 ;; "FROM_ADDRESS" (see `org-koma-letter-from-address'), "LCO" (see
36 ;; `org-koma-letter-class-option-file'), "OPENING" (see
37 ;; `org-koma-letter-opening'), "PHONE_NUMBER" (see
38 ;; `org-koma-letter-phone-number'), "SIGNATURE" (see
39 ;; `org-koma-letter-signature') and "TO_ADDRESS".
41 ;; You will need to add an appropriate association in
42 ;; `org-e-latex-classes' in order to use the KOMA Scrlttr2 class. For
43 ;; example, you can use the following code:
45 ;; (add-to-list 'org-e-latex-classes
46 ;; '("my-letter"
47 ;; "\\documentclass\[%
48 ;; DIV=14,
49 ;; fontsize=12pt,
50 ;; parskip=half,
51 ;; subject=titled,
52 ;; backaddress=false,
53 ;; fromalign=left,
54 ;; fromemail=true,
55 ;; fromphone=true\]\{scrlttr2\}
56 ;; \[DEFAULT-PACKAGES]
57 ;; \[PACKAGES]
58 ;; \[EXTRA]"))
60 ;; Then, in your Org document, be sure to require the proper class
61 ;; with :
63 ;; #+LATEX_CLASS: my-letter
66 ;;; Code:
68 (require 'org-e-latex)
71 ;;; User-Configurable Variables
73 (defgroup org-export-koma-letter nil
74 "Options for exporting to KOMA scrlttr2 class in LaTeX export."
75 :tag "Org Koma-Letter"
76 :group 'org-export
77 :version "24.2")
79 (defcustom org-koma-letter-class-option-file "NF"
80 "Letter Class Option File."
81 :group 'org-export-koma-letter
82 :type 'string)
84 (defcustom org-koma-letter-closing "See you soon,"
85 "Koma-Letter's closing, as a string."
86 :group 'org-export-koma-letter
87 :type 'string)
89 (defcustom org-koma-letter-from-address "Somewhere \\ Over the rainbow."
90 "Sender's address, as a string."
91 :group 'org-export-koma-letter
92 :type 'string)
94 (defcustom org-koma-letter-opening "Dear Sir,"
95 "Letter's opening, as a string."
96 :group 'org-export-koma-letter
97 :type 'string)
99 (defcustom org-koma-letter-phone-number "00-00-00-00"
100 "Sender's phone number, as a string."
101 :group 'org-export-koma-letter
102 :type 'string)
104 (defcustom org-koma-letter-signature "\\usekomavar{fromname}"
105 "String used as the signature."
106 :group 'org-export-koma-letter
107 :type 'string)
110 ;;; Define Back-End
112 (org-export-define-derived-backend koma-letter e-latex
113 :options-alist
114 ((:closing "CLOSING" nil org-koma-letter-closing)
115 (:from-address "FROM_ADDRESS" nil org-koma-letter-from-address newline)
116 (:lco "LCO" nil org-koma-letter-class-option-file)
117 (:opening "OPENING" nil org-koma-letter-opening)
118 (:phone-number "PHONE_NUMBER" nil org-koma-letter-phone-number)
119 (:signature "SIGNATURE" nil nil newline)
120 (:to-address "TO_ADDRESS" nil nil newline))
121 :translate-alist ((export-block . org-koma-letter-export-block)
122 (export-snippet . org-koma-letter-export-snippet)
123 (keyword . org-koma-letter-keyword)
124 (template . org-koma-letter-template))
125 :menu-entry
126 (?k "Export with KOMA Scrlttr2"
127 ((?K "As TEX buffer" org-koma-letter-export-as-latex)
128 (?k "As TEX file" org-koma-letter-export-to-latex)
129 (?p "As PDF file" org-koma-letter-export-to-pdf)
130 (?O "As PDF file and open"
131 (lambda (s v b)
132 (org-open-file (org-koma-letter-export-to-pdf s v b)))))))
135 ;;; Transcode Functions
137 ;;;; Export Block
139 (defun org-koma-letter-export-block (export-block contents info)
140 "Transcode an EXPORT-BLOCK element into KOMA Scrlttr2 code.
141 CONTENTS is nil. INFO is a plist used as a communication
142 channel."
143 (when (member (org-element-property :type export-block) '("KOMA-LETTER" "LATEX"))
144 (org-remove-indentation (org-element-property :value export-block))))
146 ;;;; Export Snippet
148 (defun org-koma-letter-export-snippet (export-snippet contents info)
149 "Transcode an EXPORT-SNIPPET object into KOMA Scrlttr2 code.
150 CONTENTS is nil. INFO is a plist used as a communication
151 channel."
152 (when (memq (org-export-snippet-backend export-snippet) '(e-latex koma-letter))
153 (org-element-property :value export-snippet)))
155 ;;;; Keyword
157 (defun org-koma-letter-keyword (keyword contents info)
158 "Transcode a KEYWORD element into KOMA Scrlttr2 code.
159 CONTENTS is nil. INFO is a plist used as a communication
160 channel."
161 (let ((key (org-element-property :key keyword))
162 (value (org-element-property :value keyword)))
163 ;; Handle specifically BEAMER and TOC (headlines only) keywords.
164 ;; Otherwise, fallback to `e-latex' back-end.
165 (if (equal key "KOMA-LETTER") value
166 (org-export-with-backend 'e-latex keyword contents info))))
168 ;;;; Template
170 (defun org-koma-letter-template (contents info)
171 "Return complete document string after KOMA Scrlttr2 conversion.
172 CONTENTS is the transcoded contents string. INFO is a plist
173 holding export options."
174 (concat
175 ;; Time-stamp.
176 (and (plist-get info :time-stamp-file)
177 (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
178 ;; Document class and packages.
179 (let ((class (plist-get info :latex-class))
180 (class-options (plist-get info :latex-class-options)))
181 (org-element-normalize-string
182 (let* ((header (nth 1 (assoc class org-e-latex-classes)))
183 (document-class-string
184 (and (stringp header)
185 (if class-options
186 (replace-regexp-in-string
187 "^[ \t]*\\\\documentclass\\(\\[.*?\\]\\)"
188 class-options header t nil 1)
189 header))))
190 (when document-class-string
191 (org-e-latex--guess-babel-language
192 (org-e-latex--guess-inputenc
193 (org-splice-latex-header
194 document-class-string
195 org-export-latex-default-packages-alist ; defined in org.el
196 org-export-latex-packages-alist nil ; defined in org.el
197 (plist-get info :latex-header-extra)))
198 info)))))
199 ;; Define "From" data.
200 (format "\\setkomavar{fromname}{%s}\n"
201 (org-export-data (plist-get info :author) info))
202 (format "\\setkomavar{fromaddress}{%s}\n" (plist-get info :from-address))
203 (format "\\setkomavar{signature}{%s}\n" (plist-get info :signature))
204 (format "\\setkomavar{fromemail}{%s}\n"
205 (org-export-data (plist-get info :email) info))
206 (format "\\setkomavar{fromphone}{%s}\n" (plist-get info :phone-number))
207 ;; Date.
208 (format "\\date{%s}\n" (org-export-data (plist-get info :date) info))
209 ;; Letter Class Option File
210 (format "\\LoadLetterOption{%s}\n" (plist-get info :lco))
211 ;; Letter start.
212 "\\begin{document}\n\n"
213 (format "\\setkomavar{subject}{%s}\n\n"
214 (org-export-data (plist-get info :title) info))
215 (format "\\begin{letter}{%%\n%s}\n\n" (or (plist-get info :to-address) "no address given"))
216 ;; Opening.
217 (format "\\opening{%s}\n\n" (plist-get info :opening))
218 ;; Letter body.
219 contents
220 ;; Closing.
221 (format "\n\\closing{%s}\n\n" (plist-get info :closing))
222 ;; Letter end.
223 "\\end{letter}\n\\end{document}"))
227 ;;; Commands
229 ;;;###autoload
230 (defun org-koma-letter-export-as-latex
231 (&optional subtreep visible-only body-only ext-plist)
232 "Export current buffer as a KOMA Scrlttr2 letter.
234 If narrowing is active in the current buffer, only export its
235 narrowed part.
237 If a region is active, export that region.
239 When optional argument SUBTREEP is non-nil, export the sub-tree
240 at point, extracting information from the headline properties
241 first.
243 When optional argument VISIBLE-ONLY is non-nil, don't export
244 contents of hidden elements.
246 When optional argument BODY-ONLY is non-nil, only write code
247 between \"\\begin{letter}\" and \"\\end{letter}\".
249 EXT-PLIST, when provided, is a property list with external
250 parameters overriding Org default settings, but still inferior to
251 file-local settings.
253 Export is done in a buffer named \"*Org KOMA-LETTER Export*\". It
254 will be displayed if `org-export-show-temporary-export-buffer' is
255 non-nil."
256 (interactive)
257 (let ((outbuf (org-export-to-buffer
258 'koma-letter "*Org KOMA-LETTER Export*"
259 subtreep visible-only body-only ext-plist)))
260 (with-current-buffer outbuf (LaTeX-mode))
261 (when org-export-show-temporary-export-buffer
262 (switch-to-buffer-other-window outbuf))))
264 ;;;###autoload
265 (defun org-koma-letter-export-to-latex
266 (&optional subtreep visible-only body-only ext-plist)
267 "Export current buffer as a KOMA Scrlttr2 letter (tex).
269 If narrowing is active in the current buffer, only export its
270 narrowed part.
272 If a region is active, export that region.
274 When optional argument SUBTREEP is non-nil, export the sub-tree
275 at point, extracting information from the headline properties
276 first.
278 When optional argument VISIBLE-ONLY is non-nil, don't export
279 contents of hidden elements.
281 When optional argument BODY-ONLY is non-nil, only write code
282 between \"\\begin{letter}\" and \"\\end{letter}\".
284 EXT-PLIST, when provided, is a property list with external
285 parameters overriding Org default settings, but still inferior to
286 file-local settings.
288 When optional argument PUB-DIR is set, use it as the publishing
289 directory.
291 Return output file's name."
292 (interactive)
293 (let ((outfile (org-export-output-file-name ".tex" subtreep)))
294 (org-export-to-file
295 'koma-letter outfile subtreep visible-only body-only ext-plist)))
297 ;;;###autoload
298 (defun org-koma-letter-export-to-pdf
299 (&optional subtreep visible-only body-only ext-plist)
300 "Export current buffer as a KOMA Scrlttr2 letter (pdf).
302 If narrowing is active in the current buffer, only export its
303 narrowed part.
305 If a region is active, export that region.
307 When optional argument SUBTREEP is non-nil, export the sub-tree
308 at point, extracting information from the headline properties
309 first.
311 When optional argument VISIBLE-ONLY is non-nil, don't export
312 contents of hidden elements.
314 When optional argument BODY-ONLY is non-nil, only write code
315 between \"\\begin{letter}\" and \"\\end{letter}\".
317 EXT-PLIST, when provided, is a property list with external
318 parameters overriding Org default settings, but still inferior to
319 file-local settings.
321 Return PDF file's name."
322 (interactive)
323 (org-e-latex-compile
324 (org-koma-letter-export-to-latex subtreep visible-only body-only ext-plist)))
326 (provide 'org-koma-letter)
327 ;;; org-koma-letter.el ends here