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
84 (defcustom org-koma-letter-from-address nil
85 "Sender's address, as a string."
86 :group
'org-export-koma-letter
89 (defcustom org-koma-letter-phone-number nil
90 "Sender's phone number, as a string."
91 :group
'org-export-koma-letter
95 (defcustom org-koma-letter-place nil
96 "Place from which the letter is sent."
97 :group
'org-export-koma-letter
100 (defcustom org-koma-letter-opening nil
101 "Letter's opening, as a string."
102 :group
'org-export-koma-letter
105 (defcustom org-koma-letter-closing nil
106 "Koma-Letter's closing, as a string."
107 :group
'org-export-koma-letter
110 (defcustom org-koma-letter-signature
"\\usekomavar{fromname}"
111 "String used as the signature."
112 :group
'org-export-koma-letter
115 (defcustom org-koma-letter-use-subject
"untitled"
116 "Use the title as the letter's subject."
117 :group
'org-export-koma-letter
120 (defcustom org-koma-letter-use-backaddress t
121 "Print return address in small line above to address."
122 :group
'org-export-koma-letter
125 (defcustom org-koma-letter-use-foldmarks
"true"
126 "Configure appearence of fold marks.
128 Accepts any valid value for the KOMA-Script `foldmarks' option.
130 Use `foldmarks:true' to activate default fold marks or
131 `foldmarks:nil' to deactivate fold marks."
132 :group
'org-export-koma-letter
135 (defcustom org-koma-letter-use-phone t
136 "Print sender's phone number."
137 :group
'org-export-koma-letter
140 (defcustom org-koma-letter-use-email t
141 "Print sender's email address."
142 :group
'org-export-koma-letter
145 (defcustom org-koma-letter-use-place t
146 "Print the letter's place next to the date."
147 :group
'org-export-koma-letter
153 (org-export-define-derived-backend 'koma-letter
'latex
155 '((:lco
"LCO" nil org-koma-letter-class-option-file
)
156 (:sender
"AUTHOR" nil user-full-name t
)
157 (:from-address
"FROM_ADDRESS" nil org-koma-letter-from-address newline
)
158 (:phone-number
"PHONE_NUMBER" nil org-koma-letter-phone-number
)
159 (:email
"EMAIL" nil user-mail-address t
)
160 (:to-address
"TO_ADDRESS" nil nil newline
)
161 (:place
"PLACE" nil org-koma-letter-place
)
162 (:opening
"OPENING" nil org-koma-letter-opening
)
163 (:closing
"CLOSING" nil org-koma-letter-closing
)
164 (:signature
"SIGNATURE" nil org-koma-letter-signature newline
)
166 (:with-backaddress nil
"backaddress" org-koma-letter-use-backaddress
)
167 (:with-foldmarks nil
"foldmarks" org-koma-letter-use-foldmarks
)
168 (:with-phone nil
"phone" org-koma-letter-use-phone
)
169 (:with-email nil
"email" org-koma-letter-use-email
)
170 (:with-place nil
"place" org-koma-letter-use-place
)
171 (:with-subject nil
"subject" org-koma-letter-use-subject
))
172 :translate-alist
'((export-block . org-koma-letter-export-block
)
173 (export-snippet . org-koma-letter-export-snippet
)
174 (keyword . org-koma-letter-keyword
)
175 (template . org-koma-letter-template
))
177 '(?k
"Export with KOMA Scrlttr2"
178 ((?L
"As LaTeX buffer" org-koma-letter-export-as-latex
)
179 (?l
"As LaTeX file" org-koma-letter-export-to-latex
)
180 (?p
"As PDF file" org-koma-letter-export-to-pdf
)
181 (?o
"As PDF file and open"
183 (if a
(org-koma-letter-export-to-pdf t s v b
)
184 (org-open-file (org-koma-letter-export-to-pdf nil s v b
))))))))
187 ;;; Transcode Functions
191 (defun org-koma-letter-export-block (export-block contents info
)
192 "Transcode an EXPORT-BLOCK element into KOMA Scrlttr2 code.
193 CONTENTS is nil. INFO is a plist used as a communication
195 (when (member (org-element-property :type export-block
) '("KOMA-LETTER" "LATEX"))
196 (org-remove-indentation (org-element-property :value export-block
))))
200 (defun org-koma-letter-export-snippet (export-snippet contents info
)
201 "Transcode an EXPORT-SNIPPET object into KOMA Scrlttr2 code.
202 CONTENTS is nil. INFO is a plist used as a communication
204 (when (memq (org-export-snippet-backend export-snippet
) '(latex koma-letter
))
205 (org-element-property :value export-snippet
)))
209 (defun org-koma-letter-keyword (keyword contents info
)
210 "Transcode a KEYWORD element into KOMA Scrlttr2 code.
211 CONTENTS is nil. INFO is a plist used as a communication
213 (let ((key (org-element-property :key keyword
))
214 (value (org-element-property :value keyword
)))
215 ;; Handle specifically BEAMER and TOC (headlines only) keywords.
216 ;; Otherwise, fallback to `latex' back-end.
217 (if (equal key
"KOMA-LETTER") value
218 (org-export-with-backend 'latex keyword contents info
))))
222 (defun org-koma-letter-template (contents info
)
223 "Return complete document string after KOMA Scrlttr2 conversion.
224 CONTENTS is the transcoded contents string. INFO is a plist
225 holding export options."
228 (and (plist-get info
:time-stamp-file
)
229 (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
230 ;; Document class and packages.
231 (let ((class (plist-get info
:latex-class
))
232 (class-options (plist-get info
:latex-class-options
)))
233 (org-element-normalize-string
234 (let* ((header (nth 1 (assoc class org-latex-classes
)))
235 (document-class-string
236 (and (stringp header
)
237 (if (not class-options
) header
238 (replace-regexp-in-string
239 "^[ \t]*\\\\documentclass\\(\\(\\[[^]]*\\]\\)?\\)"
240 class-options header t nil
1)))))
241 (if (not document-class-string
)
242 (user-error "Unknown LaTeX class `%s'")
243 (org-latex-guess-babel-language
244 (org-latex-guess-inputenc
245 (org-splice-latex-header
246 document-class-string
247 org-latex-default-packages-alist
; defined in org.el
248 org-latex-packages-alist nil
; defined in org.el
249 (concat (plist-get info
:latex-header
)
250 (plist-get info
:latex-header-extra
))))
252 (let ((lco (plist-get info
:lco
))
253 (sender (plist-get info
:sender
))
254 (from-address (plist-get info
:from-address
))
255 (phone-number (plist-get info
:phone-number
))
256 (email (plist-get info
:email
))
257 (signature (plist-get info
:signature
)))
259 ;; Letter Class Option File
261 (let ((lco-files (split-string lco
" "))
263 (dolist (lco-file lco-files lco-def
)
264 (setq lco-def
(format "%s\\LoadLetterOption{%s}\n" lco-def lco-file
)))
266 ;; Define "From" data.
267 (when sender
(format "\\setkomavar{fromname}{%s}\n"
268 (org-export-data sender info
)))
269 (when from-address
(format "\\setkomavar{fromaddress}{%s}\n" from-address
))
270 (when phone-number
(format "\\setkomavar{fromphone}{%s}\n" phone-number
))
271 (when email
(format "\\setkomavar{fromemail}{%s}\n" email
))
272 (when signature
(format "\\setkomavar{signature}{%s}\n" signature
))))
274 (format "\\date{%s}\n" (org-export-data (org-export-get-date info
) info
))
276 (let ((with-place (plist-get info
:with-place
))
277 (place (plist-get info
:place
)))
278 (when (or place
(not with-place
))
279 (format "\\setkomavar{place}{%s}\n" (if with-place place
""))))
281 (let ((with-backaddress (plist-get info
:with-backaddress
))
282 (with-foldmarks (plist-get info
:with-foldmarks
))
283 (with-phone (plist-get info
:with-phone
))
284 (with-email (plist-get info
:with-email
)))
286 (format "\\KOMAoption{backaddress}{%s}\n" (if with-backaddress
"true" "false"))
287 (format "\\KOMAoption{foldmarks}{%s}\n" (if with-foldmarks with-foldmarks
"false"))
288 (format "\\KOMAoption{fromphone}{%s}\n" (if with-phone
"true" "false"))
289 (format "\\KOMAoption{fromemail}{%s}\n" (if with-email
"true" "false"))))
291 "\\begin{document}\n\n"
293 (let ((with-subject (plist-get info
:with-subject
)))
296 (format "\\KOMAoption{subject}{%s}\n" with-subject
)
297 (format "\\setkomavar{subject}{%s}\n\n"
298 (org-export-data (plist-get info
:title
) info
)))))
300 (format "\\begin{letter}{%%\n%s}\n\n"
301 (or (plist-get info
:to-address
) "no address given"))
303 (format "\\opening{%s}\n\n" (plist-get info
:opening
))
307 (format "\n\\closing{%s}\n\n" (plist-get info
:closing
))
309 "\\end{letter}\n\\end{document}"))
316 (defun org-koma-letter-export-as-latex
317 (&optional async subtreep visible-only body-only ext-plist
)
318 "Export current buffer as a KOMA Scrlttr2 letter.
320 If narrowing is active in the current buffer, only export its
323 If a region is active, export that region.
325 A non-nil optional argument ASYNC means the process should happen
326 asynchronously. The resulting buffer should be accessible
327 through the `org-export-stack' interface.
329 When optional argument SUBTREEP is non-nil, export the sub-tree
330 at point, extracting information from the headline properties
333 When optional argument VISIBLE-ONLY is non-nil, don't export
334 contents of hidden elements.
336 When optional argument BODY-ONLY is non-nil, only write code
337 between \"\\begin{letter}\" and \"\\end{letter}\".
339 EXT-PLIST, when provided, is a property list with external
340 parameters overriding Org default settings, but still inferior to
343 Export is done in a buffer named \"*Org KOMA-LETTER Export*\". It
344 will be displayed if `org-export-show-temporary-export-buffer' is
348 (org-export-async-start
350 (with-current-buffer (get-buffer-create "*Org KOMA-LETTER Export*")
353 (goto-char (point-min))
355 (org-export-add-to-stack (current-buffer) 'koma-letter
)))
356 `(org-export-as 'koma-letter
,subtreep
,visible-only
,body-only
358 (let ((outbuf (org-export-to-buffer
359 'koma-letter
"*Org KOMA-LETTER Export*"
360 subtreep visible-only body-only ext-plist
)))
361 (with-current-buffer outbuf
(LaTeX-mode))
362 (when org-export-show-temporary-export-buffer
363 (switch-to-buffer-other-window outbuf
)))))
366 (defun org-koma-letter-export-to-latex
367 (&optional async subtreep visible-only body-only ext-plist
)
368 "Export current buffer as a KOMA Scrlttr2 letter (tex).
370 If narrowing is active in the current buffer, only export its
373 If a region is active, export that region.
375 A non-nil optional argument ASYNC means the process should happen
376 asynchronously. The resulting file should be accessible through
377 the `org-export-stack' interface.
379 When optional argument SUBTREEP is non-nil, export the sub-tree
380 at point, extracting information from the headline properties
383 When optional argument VISIBLE-ONLY is non-nil, don't export
384 contents of hidden elements.
386 When optional argument BODY-ONLY is non-nil, only write code
387 between \"\\begin{letter}\" and \"\\end{letter}\".
389 EXT-PLIST, when provided, is a property list with external
390 parameters overriding Org default settings, but still inferior to
393 When optional argument PUB-DIR is set, use it as the publishing
396 Return output file's name."
398 (let ((outfile (org-export-output-file-name ".tex" subtreep
)))
400 (org-export-async-start
401 (lambda (f) (org-export-add-to-stack f
'koma-letter
))
404 'koma-letter
,outfile
,subtreep
,visible-only
,body-only
407 'koma-letter outfile subtreep visible-only body-only ext-plist
))))
410 (defun org-koma-letter-export-to-pdf
411 (&optional async subtreep visible-only body-only ext-plist
)
412 "Export current buffer as a KOMA Scrlttr2 letter (pdf).
414 If narrowing is active in the current buffer, only export its
417 If a region is active, export that region.
419 A non-nil optional argument ASYNC means the process should happen
420 asynchronously. The resulting file should be accessible through
421 the `org-export-stack' interface.
423 When optional argument SUBTREEP is non-nil, export the sub-tree
424 at point, extracting information from the headline properties
427 When optional argument VISIBLE-ONLY is non-nil, don't export
428 contents of hidden elements.
430 When optional argument BODY-ONLY is non-nil, only write code
431 between \"\\begin{letter}\" and \"\\end{letter}\".
433 EXT-PLIST, when provided, is a property list with external
434 parameters overriding Org default settings, but still inferior to
437 Return PDF file's name."
440 (let ((outfile (org-export-output-file-name ".tex" subtreep
)))
441 (org-export-async-start
442 (lambda (f) (org-export-add-to-stack f
'koma-letter
))
446 'koma-letter
,outfile
,subtreep
,visible-only
,body-only
449 (org-koma-letter-export-to-latex
450 nil subtreep visible-only body-only ext-plist
))))
453 (provide 'ox-koma-letter
)
454 ;;; ox-koma-letter.el ends here