1 ;;; ox-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 ;; This program 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 ;; This program 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/>.
24 ;; This library implements a KOMA Scrlttr2 back-end, derived from the
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 `latex' back-end (see
33 ;; `org-latex-options-alist'), this back-end introduces the following
34 ;; keywords: "CLOSING" (see `org-koma-letter-closing'), "FROM_ADDRESS"
35 ;; (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-latex-classes' in order to use the KOMA Scrlttr2 class. For
43 ;; example, you can use the following code:
45 ;; (add-to-list 'org-latex-classes
47 ;; "\\documentclass\[%
55 ;; fromphone=true\]\{scrlttr2\}
56 ;; \[DEFAULT-PACKAGES]
60 ;; Then, in your Org document, be sure to require the proper class
63 ;; #+LATEX_CLASS: my-letter
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"
78 (defcustom org-koma-letter-class-option-file
"NF"
79 "Letter Class Option File."
80 :group
'org-export-koma-letter
83 (defcustom org-koma-letter-closing
"See you soon,"
84 "Koma-Letter's closing, as a string."
85 :group
'org-export-koma-letter
88 (defcustom org-koma-letter-from-address
"Somewhere \\ Over the rainbow."
89 "Sender's address, as a string."
90 :group
'org-export-koma-letter
93 (defcustom org-koma-letter-opening
"Dear Sir,"
94 "Letter's opening, as a string."
95 :group
'org-export-koma-letter
98 (defcustom org-koma-letter-phone-number
"00-00-00-00"
99 "Sender's phone number, as a string."
100 :group
'org-export-koma-letter
103 (defcustom org-koma-letter-signature
"\\usekomavar{fromname}"
104 "String used as the signature."
105 :group
'org-export-koma-letter
111 (org-export-define-derived-backend 'koma-letter
'latex
113 '((:closing
"CLOSING" nil org-koma-letter-closing
)
114 (:from-address
"FROM_ADDRESS" nil org-koma-letter-from-address newline
)
115 (:lco
"LCO" nil org-koma-letter-class-option-file
)
116 (:opening
"OPENING" nil org-koma-letter-opening
)
117 (:phone-number
"PHONE_NUMBER" nil org-koma-letter-phone-number
)
118 (:signature
"SIGNATURE" nil nil newline
)
119 (:to-address
"TO_ADDRESS" nil nil newline
))
120 :translate-alist
'((export-block . org-koma-letter-export-block
)
121 (export-snippet . org-koma-letter-export-snippet
)
122 (keyword . org-koma-letter-keyword
)
123 (template . org-koma-letter-template
))
125 '(?k
"Export with KOMA Scrlttr2"
126 ((?K
"As LaTeX buffer" org-koma-letter-export-as-latex
)
127 (?k
"As LaTeX file" org-koma-letter-export-to-latex
)
128 (?p
"As PDF file" org-koma-letter-export-to-pdf
)
129 (?O
"As PDF file and open"
131 (if a
(org-koma-letter-export-to-pdf t s v b
)
132 (org-open-file (org-koma-letter-export-to-pdf nil s v b
))))))))
135 ;;; Transcode Functions
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
143 (when (member (org-element-property :type export-block
) '("KOMA-LETTER" "LATEX"))
144 (org-remove-indentation (org-element-property :value export-block
))))
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
152 (when (memq (org-export-snippet-backend export-snippet
) '(latex koma-letter
))
153 (org-element-property :value export-snippet
)))
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
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 `latex' back-end.
165 (if (equal key
"KOMA-LETTER") value
166 (org-export-with-backend 'latex keyword contents info
))))
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."
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-latex-classes
)))
183 (document-class-string
184 (and (stringp header
)
185 (if (not class-options
) header
186 (replace-regexp-in-string
187 "^[ \t]*\\\\documentclass\\(\\(\\[[^]]*\\]\\)?\\)"
188 class-options header t nil
1)))))
189 (if (not document-class-string
)
190 (user-error "Unknown LaTeX class `%s'")
191 (org-latex-guess-babel-language
192 (org-latex-guess-inputenc
193 (org-splice-latex-header
194 document-class-string
195 org-latex-default-packages-alist
; defined in org.el
196 org-latex-packages-alist nil
; defined in org.el
197 (concat (plist-get info
:latex-header
)
198 (plist-get info
:latex-header-extra
))))
200 ;; Define "From" data.
201 (format "\\setkomavar{fromname}{%s}\n"
202 (org-export-data (plist-get info
:author
) info
))
203 (format "\\setkomavar{fromaddress}{%s}\n" (plist-get info
:from-address
))
204 (format "\\setkomavar{signature}{%s}\n" (plist-get info
:signature
))
205 (format "\\setkomavar{fromemail}{%s}\n"
206 (org-export-data (plist-get info
:email
) info
))
207 (format "\\setkomavar{fromphone}{%s}\n" (plist-get info
:phone-number
))
209 (format "\\date{%s}\n" (org-export-data (org-export-get-date info
) info
))
210 ;; Letter Class Option File
211 (format "\\LoadLetterOption{%s}\n" (plist-get info
:lco
))
213 "\\begin{document}\n\n"
214 (format "\\setkomavar{subject}{%s}\n\n"
215 (org-export-data (plist-get info
:title
) info
))
216 (format "\\begin{letter}{%%\n%s}\n\n"
217 (or (plist-get info
:to-address
) "no address given"))
219 (format "\\opening{%s}\n\n" (plist-get info
:opening
))
223 (format "\n\\closing{%s}\n\n" (plist-get info
:closing
))
225 "\\end{letter}\n\\end{document}"))
232 (defun org-koma-letter-export-as-latex
233 (&optional async subtreep visible-only body-only ext-plist
)
234 "Export current buffer as a KOMA Scrlttr2 letter.
236 If narrowing is active in the current buffer, only export its
239 If a region is active, export that region.
241 A non-nil optional argument ASYNC means the process should happen
242 asynchronously. The resulting buffer should be accessible
243 through the `org-export-stack' interface.
245 When optional argument SUBTREEP is non-nil, export the sub-tree
246 at point, extracting information from the headline properties
249 When optional argument VISIBLE-ONLY is non-nil, don't export
250 contents of hidden elements.
252 When optional argument BODY-ONLY is non-nil, only write code
253 between \"\\begin{letter}\" and \"\\end{letter}\".
255 EXT-PLIST, when provided, is a property list with external
256 parameters overriding Org default settings, but still inferior to
259 Export is done in a buffer named \"*Org KOMA-LETTER Export*\". It
260 will be displayed if `org-export-show-temporary-export-buffer' is
264 (org-export-async-start
266 (with-current-buffer (get-buffer-create "*Org KOMA-LETTER Export*")
269 (goto-char (point-min))
271 (org-export-add-to-stack (current-buffer) 'koma-letter
)))
272 `(org-export-as 'koma-letter
,subtreep
,visible-only
,body-only
274 (let ((outbuf (org-export-to-buffer
275 'koma-letter
"*Org KOMA-LETTER Export*"
276 subtreep visible-only body-only ext-plist
)))
277 (with-current-buffer outbuf
(LaTeX-mode))
278 (when org-export-show-temporary-export-buffer
279 (switch-to-buffer-other-window outbuf
)))))
282 (defun org-koma-letter-export-to-latex
283 (&optional async subtreep visible-only body-only ext-plist
)
284 "Export current buffer as a KOMA Scrlttr2 letter (tex).
286 If narrowing is active in the current buffer, only export its
289 If a region is active, export that region.
291 A non-nil optional argument ASYNC means the process should happen
292 asynchronously. The resulting file should be accessible through
293 the `org-export-stack' interface.
295 When optional argument SUBTREEP is non-nil, export the sub-tree
296 at point, extracting information from the headline properties
299 When optional argument VISIBLE-ONLY is non-nil, don't export
300 contents of hidden elements.
302 When optional argument BODY-ONLY is non-nil, only write code
303 between \"\\begin{letter}\" and \"\\end{letter}\".
305 EXT-PLIST, when provided, is a property list with external
306 parameters overriding Org default settings, but still inferior to
309 When optional argument PUB-DIR is set, use it as the publishing
312 Return output file's name."
314 (let ((outfile (org-export-output-file-name ".tex" subtreep
)))
316 (org-export-async-start
317 (lambda (f) (org-export-add-to-stack f
'koma-letter
))
320 'koma-letter
,outfile
,subtreep
,visible-only
,body-only
323 'koma-letter outfile subtreep visible-only body-only ext-plist
))))
326 (defun org-koma-letter-export-to-pdf
327 (&optional async subtreep visible-only body-only ext-plist
)
328 "Export current buffer as a KOMA Scrlttr2 letter (pdf).
330 If narrowing is active in the current buffer, only export its
333 If a region is active, export that region.
335 A non-nil optional argument ASYNC means the process should happen
336 asynchronously. The resulting file should be accessible through
337 the `org-export-stack' interface.
339 When optional argument SUBTREEP is non-nil, export the sub-tree
340 at point, extracting information from the headline properties
343 When optional argument VISIBLE-ONLY is non-nil, don't export
344 contents of hidden elements.
346 When optional argument BODY-ONLY is non-nil, only write code
347 between \"\\begin{letter}\" and \"\\end{letter}\".
349 EXT-PLIST, when provided, is a property list with external
350 parameters overriding Org default settings, but still inferior to
353 Return PDF file's name."
356 (let ((outfile (org-export-output-file-name ".tex" subtreep
)))
357 (org-export-async-start
358 (lambda (f) (org-export-add-to-stack f
'koma-letter
))
362 'koma-letter
,outfile
,subtreep
,visible-only
,body-only
365 (org-koma-letter-export-to-latex
366 nil subtreep visible-only body-only ext-plist
))))
369 (provide 'ox-koma-letter
)
370 ;;; ox-koma-letter.el ends here