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
"Sincerely yours,"
84 "Koma-Letter's closing, as a string."
85 :group
'org-export-koma-letter
88 (defcustom org-koma-letter-from-address nil
89 "Sender's address, as a string."
90 :group
'org-export-koma-letter
93 (defcustom org-koma-letter-opening
"Dear Madam or Sir,"
94 "Letter's opening, as a string."
95 :group
'org-export-koma-letter
98 (defcustom org-koma-letter-phone-number nil
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
108 (defcustom org-koma-letter-sender nil
109 "Sender's name, as a string."
110 :group
'org-export-koma-letter
113 (defcustom org-koma-letter-email nil
114 "Sender's email, as a string."
115 :group
'org-export-koma-letter
121 (org-export-define-derived-backend 'koma-letter
'latex
123 '((:lco
"LCO" nil org-koma-letter-class-option-file
)
124 (:sender
"SENDER" nil org-koma-letter-sender newline
)
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 org-koma-letter-email
)
128 (:to-address
"TO_ADDRESS" nil nil newline
)
129 (:opening
"OPENING" nil org-koma-letter-opening
)
130 (:closing
"CLOSING" nil org-koma-letter-closing
)
131 (:signature
"SIGNATURE" nil org-koma-letter-signature newline
))
132 :translate-alist
'((export-block . org-koma-letter-export-block
)
133 (export-snippet . org-koma-letter-export-snippet
)
134 (keyword . org-koma-letter-keyword
)
135 (template . org-koma-letter-template
))
137 '(?k
"Export with KOMA Scrlttr2"
138 ((?L
"As LaTeX buffer" org-koma-letter-export-as-latex
)
139 (?l
"As LaTeX file" org-koma-letter-export-to-latex
)
140 (?p
"As PDF file" org-koma-letter-export-to-pdf
)
141 (?o
"As PDF file and open"
143 (if a
(org-koma-letter-export-to-pdf t s v b
)
144 (org-open-file (org-koma-letter-export-to-pdf nil s v b
))))))))
147 ;;; Transcode Functions
151 (defun org-koma-letter-export-block (export-block contents info
)
152 "Transcode an EXPORT-BLOCK element into KOMA Scrlttr2 code.
153 CONTENTS is nil. INFO is a plist used as a communication
155 (when (member (org-element-property :type export-block
) '("KOMA-LETTER" "LATEX"))
156 (org-remove-indentation (org-element-property :value export-block
))))
160 (defun org-koma-letter-export-snippet (export-snippet contents info
)
161 "Transcode an EXPORT-SNIPPET object into KOMA Scrlttr2 code.
162 CONTENTS is nil. INFO is a plist used as a communication
164 (when (memq (org-export-snippet-backend export-snippet
) '(latex koma-letter
))
165 (org-element-property :value export-snippet
)))
169 (defun org-koma-letter-keyword (keyword contents info
)
170 "Transcode a KEYWORD element into KOMA Scrlttr2 code.
171 CONTENTS is nil. INFO is a plist used as a communication
173 (let ((key (org-element-property :key keyword
))
174 (value (org-element-property :value keyword
)))
175 ;; Handle specifically BEAMER and TOC (headlines only) keywords.
176 ;; Otherwise, fallback to `latex' back-end.
177 (if (equal key
"KOMA-LETTER") value
178 (org-export-with-backend 'latex keyword contents info
))))
182 (defun org-koma-letter-template (contents info
)
183 "Return complete document string after KOMA Scrlttr2 conversion.
184 CONTENTS is the transcoded contents string. INFO is a plist
185 holding export options."
188 (and (plist-get info
:time-stamp-file
)
189 (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
190 ;; Document class and packages.
191 (let ((class (plist-get info
:latex-class
))
192 (class-options (plist-get info
:latex-class-options
)))
193 (org-element-normalize-string
194 (let* ((header (nth 1 (assoc class org-latex-classes
)))
195 (document-class-string
196 (and (stringp header
)
197 (if (not class-options
) header
198 (replace-regexp-in-string
199 "^[ \t]*\\\\documentclass\\(\\(\\[[^]]*\\]\\)?\\)"
200 class-options header t nil
1)))))
201 (if (not document-class-string
)
202 (user-error "Unknown LaTeX class `%s'")
203 (org-latex-guess-babel-language
204 (org-latex-guess-inputenc
205 (org-splice-latex-header
206 document-class-string
207 org-latex-default-packages-alist
; defined in org.el
208 org-latex-packages-alist nil
; defined in org.el
209 (concat (plist-get info
:latex-header
)
210 (plist-get info
:latex-header-extra
))))
212 (let ((lco (plist-get info
:lco
))
213 (sender (plist-get info
:sender
))
214 (from-address (plist-get info
:from-address
))
215 (phone-number (plist-get info
:phone-number
))
216 (email (plist-get info
:email
))
217 (signature (plist-get info
:signature
)))
219 ;; Letter Class Option File
220 (when lco
(format "\\LoadLetterOption{%s}\n" lco
))
221 ;; Define "From" data.
222 (when sender
(format "\\setkomavar{fromname}{%s}\n" sender
))
223 (when from-address
(format "\\setkomavar{fromaddress}{%s}\n" from-address
))
224 (when phone-number
(format "\\setkomavar{fromphone}{%s}\n" phone-number
))
225 (when email
(format "\\setkomavar{fromemail}{%s}\n" email
))
226 (when signature
(format "\\setkomavar{signature}{%s}\n" signature
))))
228 (format "\\date{%s}\n" (org-export-data (org-export-get-date info
) info
))
230 "\\begin{document}\n\n"
231 (format "\\setkomavar{subject}{%s}\n\n"
232 (org-export-data (plist-get info
:title
) info
))
233 (format "\\begin{letter}{%%\n%s}\n\n"
234 (or (plist-get info
:to-address
) "no address given"))
236 (format "\\opening{%s}\n\n" (plist-get info
:opening
))
240 (format "\n\\closing{%s}\n\n" (plist-get info
:closing
))
242 "\\end{letter}\n\\end{document}"))
249 (defun org-koma-letter-export-as-latex
250 (&optional async subtreep visible-only body-only ext-plist
)
251 "Export current buffer as a KOMA Scrlttr2 letter.
253 If narrowing is active in the current buffer, only export its
256 If a region is active, export that region.
258 A non-nil optional argument ASYNC means the process should happen
259 asynchronously. The resulting buffer should be accessible
260 through the `org-export-stack' interface.
262 When optional argument SUBTREEP is non-nil, export the sub-tree
263 at point, extracting information from the headline properties
266 When optional argument VISIBLE-ONLY is non-nil, don't export
267 contents of hidden elements.
269 When optional argument BODY-ONLY is non-nil, only write code
270 between \"\\begin{letter}\" and \"\\end{letter}\".
272 EXT-PLIST, when provided, is a property list with external
273 parameters overriding Org default settings, but still inferior to
276 Export is done in a buffer named \"*Org KOMA-LETTER Export*\". It
277 will be displayed if `org-export-show-temporary-export-buffer' is
281 (org-export-async-start
283 (with-current-buffer (get-buffer-create "*Org KOMA-LETTER Export*")
286 (goto-char (point-min))
288 (org-export-add-to-stack (current-buffer) 'koma-letter
)))
289 `(org-export-as 'koma-letter
,subtreep
,visible-only
,body-only
291 (let ((outbuf (org-export-to-buffer
292 'koma-letter
"*Org KOMA-LETTER Export*"
293 subtreep visible-only body-only ext-plist
)))
294 (with-current-buffer outbuf
(LaTeX-mode))
295 (when org-export-show-temporary-export-buffer
296 (switch-to-buffer-other-window outbuf
)))))
299 (defun org-koma-letter-export-to-latex
300 (&optional async subtreep visible-only body-only ext-plist
)
301 "Export current buffer as a KOMA Scrlttr2 letter (tex).
303 If narrowing is active in the current buffer, only export its
306 If a region is active, export that region.
308 A non-nil optional argument ASYNC means the process should happen
309 asynchronously. The resulting file should be accessible through
310 the `org-export-stack' interface.
312 When optional argument SUBTREEP is non-nil, export the sub-tree
313 at point, extracting information from the headline properties
316 When optional argument VISIBLE-ONLY is non-nil, don't export
317 contents of hidden elements.
319 When optional argument BODY-ONLY is non-nil, only write code
320 between \"\\begin{letter}\" and \"\\end{letter}\".
322 EXT-PLIST, when provided, is a property list with external
323 parameters overriding Org default settings, but still inferior to
326 When optional argument PUB-DIR is set, use it as the publishing
329 Return output file's name."
331 (let ((outfile (org-export-output-file-name ".tex" subtreep
)))
333 (org-export-async-start
334 (lambda (f) (org-export-add-to-stack f
'koma-letter
))
337 'koma-letter
,outfile
,subtreep
,visible-only
,body-only
340 'koma-letter outfile subtreep visible-only body-only ext-plist
))))
343 (defun org-koma-letter-export-to-pdf
344 (&optional async subtreep visible-only body-only ext-plist
)
345 "Export current buffer as a KOMA Scrlttr2 letter (pdf).
347 If narrowing is active in the current buffer, only export its
350 If a region is active, export that region.
352 A non-nil optional argument ASYNC means the process should happen
353 asynchronously. The resulting file should be accessible through
354 the `org-export-stack' interface.
356 When optional argument SUBTREEP is non-nil, export the sub-tree
357 at point, extracting information from the headline properties
360 When optional argument VISIBLE-ONLY is non-nil, don't export
361 contents of hidden elements.
363 When optional argument BODY-ONLY is non-nil, only write code
364 between \"\\begin{letter}\" and \"\\end{letter}\".
366 EXT-PLIST, when provided, is a property list with external
367 parameters overriding Org default settings, but still inferior to
370 Return PDF file's name."
373 (let ((outfile (org-export-output-file-name ".tex" subtreep
)))
374 (org-export-async-start
375 (lambda (f) (org-export-add-to-stack f
'koma-letter
))
379 'koma-letter
,outfile
,subtreep
,visible-only
,body-only
382 (org-koma-letter-export-to-latex
383 nil subtreep visible-only body-only ext-plist
))))
386 (provide 'ox-koma-letter
)
387 ;;; ox-koma-letter.el ends here