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-author
'user-full-name
86 This variable defaults to calling the function `user-full-name'
87 which just returns the current `user-full-name'. Alternatively a
88 string, nil or a function may be given. Functions must return a
90 :group
'org-export-koma-letter
91 :type
'(radio (function-item user-full-name
)
96 (defcustom org-koma-letter-email
'org-koma-letter-email
97 "The sender's email address.
99 This variable defaults to the value `org-koma-letter-email' which
100 returns `user-mail-address'. Alternatively a string, nil or a
101 function may be given. Functions must return a string."
102 :group
'org-export-koma-letter
103 :type
'(radio (function-item org-koma-letter-email
)
108 (defcustom org-koma-letter-from-address nil
109 "Sender's address, as a string."
110 :group
'org-export-koma-letter
113 (defcustom org-koma-letter-phone-number nil
114 "Sender's phone number, as a string."
115 :group
'org-export-koma-letter
119 (defcustom org-koma-letter-place nil
120 "Place from which the letter is sent."
121 :group
'org-export-koma-letter
124 (defcustom org-koma-letter-opening nil
125 "Letter's opening, as a string."
126 :group
'org-export-koma-letter
129 (defcustom org-koma-letter-closing nil
130 "Koma-Letter's closing, as a string."
131 :group
'org-export-koma-letter
134 (defcustom org-koma-letter-signature
"\\usekomavar{fromname}"
135 "String used as the signature."
136 :group
'org-export-koma-letter
139 (defcustom org-koma-letter-use-subject
"untitled"
140 "Use the title as the letter's subject."
141 :group
'org-export-koma-letter
144 (defcustom org-koma-letter-use-backaddress t
145 "Print return address in small line above to address."
146 :group
'org-export-koma-letter
149 (defcustom org-koma-letter-use-foldmarks
"true"
150 "Configure appearence of fold marks.
152 Accepts any valid value for the KOMA-Script `foldmarks' option.
154 Use `foldmarks:true' to activate default fold marks or
155 `foldmarks:nil' to deactivate fold marks."
156 :group
'org-export-koma-letter
159 (defcustom org-koma-letter-use-phone t
160 "Print sender's phone number."
161 :group
'org-export-koma-letter
164 (defcustom org-koma-letter-use-email t
165 "Print sender's email address."
166 :group
'org-export-koma-letter
169 (defcustom org-koma-letter-use-place t
170 "Print the letter's place next to the date."
171 :group
'org-export-koma-letter
177 (org-export-define-derived-backend 'koma-letter
'latex
179 '((:lco
"LCO" nil org-koma-letter-class-option-file
)
180 (:author
"AUTHOR" nil
(org-koma-letter--get-custom org-koma-letter-author
) t
)
181 (:from-address
"FROM_ADDRESS" nil org-koma-letter-from-address newline
)
182 (:phone-number
"PHONE_NUMBER" nil org-koma-letter-phone-number
)
183 (:email
"EMAIL" nil
(org-koma-letter--get-custom org-koma-letter-email
) t
)
184 (:to-address
"TO_ADDRESS" nil nil newline
)
185 (:place
"PLACE" nil org-koma-letter-place
)
186 (:opening
"OPENING" nil org-koma-letter-opening
)
187 (:closing
"CLOSING" nil org-koma-letter-closing
)
188 (:signature
"SIGNATURE" nil org-koma-letter-signature newline
)
190 (:with-backaddress nil
"backaddress" org-koma-letter-use-backaddress
)
191 (:with-foldmarks nil
"foldmarks" org-koma-letter-use-foldmarks
)
192 (:with-phone nil
"phone" org-koma-letter-use-phone
)
193 (:with-email nil
"email" org-koma-letter-use-email
)
194 (:with-place nil
"place" org-koma-letter-use-place
)
195 (:with-subject nil
"subject" org-koma-letter-use-subject
))
196 :translate-alist
'((export-block . org-koma-letter-export-block
)
197 (export-snippet . org-koma-letter-export-snippet
)
198 (keyword . org-koma-letter-keyword
)
199 (template . org-koma-letter-template
))
201 '(?k
"Export with KOMA Scrlttr2"
202 ((?L
"As LaTeX buffer" org-koma-letter-export-as-latex
)
203 (?l
"As LaTeX file" org-koma-letter-export-to-latex
)
204 (?p
"As PDF file" org-koma-letter-export-to-pdf
)
205 (?o
"As PDF file and open"
207 (if a
(org-koma-letter-export-to-pdf t s v b
)
208 (org-open-file (org-koma-letter-export-to-pdf nil s v b
))))))))
213 (defun org-koma-letter-email ()
214 "Return the current `user-mail-address'"
218 (defun org-koma-letter--get-custom (value)
219 "Determines whether a value is nil, a string or a
220 function (a symobl). If it is a function it it evaluates it."
222 (cond ((stringp value
) value
)
223 ((functionp value
) (funcall value
))
224 ((symbolp value
) (symbol-name value
)))))
227 ;;; Transcode Functions
231 (defun org-koma-letter-export-block (export-block contents info
)
232 "Transcode an EXPORT-BLOCK element into KOMA Scrlttr2 code.
233 CONTENTS is nil. INFO is a plist used as a communication
235 (when (member (org-element-property :type export-block
) '("KOMA-LETTER" "LATEX"))
236 (org-remove-indentation (org-element-property :value export-block
))))
240 (defun org-koma-letter-export-snippet (export-snippet contents info
)
241 "Transcode an EXPORT-SNIPPET object into KOMA Scrlttr2 code.
242 CONTENTS is nil. INFO is a plist used as a communication
244 (when (memq (org-export-snippet-backend export-snippet
) '(latex koma-letter
))
245 (org-element-property :value export-snippet
)))
249 (defun org-koma-letter-keyword (keyword contents info
)
250 "Transcode a KEYWORD element into KOMA Scrlttr2 code.
251 CONTENTS is nil. INFO is a plist used as a communication
253 (let ((key (org-element-property :key keyword
))
254 (value (org-element-property :value keyword
)))
255 ;; Handle specifically BEAMER and TOC (headlines only) keywords.
256 ;; Otherwise, fallback to `latex' back-end.
257 (if (equal key
"KOMA-LETTER") value
258 (org-export-with-backend 'latex keyword contents info
))))
262 (defun org-koma-letter-template (contents info
)
263 "Return complete document string after KOMA Scrlttr2 conversion.
264 CONTENTS is the transcoded contents string. INFO is a plist
265 holding export options."
268 (and (plist-get info
:time-stamp-file
)
269 (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
270 ;; Document class and packages.
271 (let ((class (plist-get info
:latex-class
))
272 (class-options (plist-get info
:latex-class-options
)))
273 (org-element-normalize-string
274 (let* ((header (nth 1 (assoc class org-latex-classes
)))
275 (document-class-string
276 (and (stringp header
)
277 (if (not class-options
) header
278 (replace-regexp-in-string
279 "^[ \t]*\\\\documentclass\\(\\(\\[[^]]*\\]\\)?\\)"
280 class-options header t nil
1)))))
281 (if (not document-class-string
)
282 (user-error "Unknown LaTeX class `%s'")
283 (org-latex-guess-babel-language
284 (org-latex-guess-inputenc
285 (org-splice-latex-header
286 document-class-string
287 org-latex-default-packages-alist
; defined in org.el
288 org-latex-packages-alist nil
; defined in org.el
289 (concat (plist-get info
:latex-header
)
290 (plist-get info
:latex-header-extra
))))
292 (let ((lco (plist-get info
:lco
))
293 (author (plist-get info
:author
))
294 (from-address (plist-get info
:from-address
))
295 (phone-number (plist-get info
:phone-number
))
296 (email (plist-get info
:email
))
297 (signature (plist-get info
:signature
)))
299 ;; Letter Class Option File
301 (let ((lco-files (split-string lco
" "))
303 (dolist (lco-file lco-files lco-def
)
304 (setq lco-def
(format "%s\\LoadLetterOption{%s}\n" lco-def lco-file
)))
306 ;; Define "From" data.
307 (when author
(format "\\setkomavar{fromname}{%s}\n"
308 (org-export-data author info
)))
309 (when from-address
(format "\\setkomavar{fromaddress}{%s}\n" from-address
))
310 (when phone-number
(format "\\setkomavar{fromphone}{%s}\n" phone-number
))
311 (when email
(format "\\setkomavar{fromemail}{%s}\n" email
))
312 (when signature
(format "\\setkomavar{signature}{%s}\n" signature
))))
314 (format "\\date{%s}\n" (org-export-data (org-export-get-date info
) info
))
316 (let ((with-place (plist-get info
:with-place
))
317 (place (plist-get info
:place
)))
318 (when (or place
(not with-place
))
319 (format "\\setkomavar{place}{%s}\n" (if with-place place
""))))
321 (let ((with-backaddress (plist-get info
:with-backaddress
))
322 (with-foldmarks (plist-get info
:with-foldmarks
))
323 (with-phone (plist-get info
:with-phone
))
324 (with-email (plist-get info
:with-email
)))
326 (format "\\KOMAoption{backaddress}{%s}\n" (if with-backaddress
"true" "false"))
327 (format "\\KOMAoption{foldmarks}{%s}\n" (if with-foldmarks with-foldmarks
"false"))
328 (format "\\KOMAoption{fromphone}{%s}\n" (if with-phone
"true" "false"))
329 (format "\\KOMAoption{fromemail}{%s}\n" (if with-email
"true" "false"))))
331 "\\begin{document}\n\n"
333 (let ((with-subject (plist-get info
:with-subject
)))
336 (format "\\KOMAoption{subject}{%s}\n" with-subject
)
337 (format "\\setkomavar{subject}{%s}\n\n"
338 (org-export-data (plist-get info
:title
) info
)))))
340 (format "\\begin{letter}{%%\n%s}\n\n"
341 (or (plist-get info
:to-address
) "no address given"))
343 (format "\\opening{%s}\n\n" (plist-get info
:opening
))
347 (format "\n\\closing{%s}\n\n" (plist-get info
:closing
))
349 "\\end{letter}\n\\end{document}"))
356 (defun org-koma-letter-export-as-latex
357 (&optional async subtreep visible-only body-only ext-plist
)
358 "Export current buffer as a KOMA Scrlttr2 letter.
360 If narrowing is active in the current buffer, only export its
363 If a region is active, export that region.
365 A non-nil optional argument ASYNC means the process should happen
366 asynchronously. The resulting buffer should be accessible
367 through the `org-export-stack' interface.
369 When optional argument SUBTREEP is non-nil, export the sub-tree
370 at point, extracting information from the headline properties
373 When optional argument VISIBLE-ONLY is non-nil, don't export
374 contents of hidden elements.
376 When optional argument BODY-ONLY is non-nil, only write code
377 between \"\\begin{letter}\" and \"\\end{letter}\".
379 EXT-PLIST, when provided, is a property list with external
380 parameters overriding Org default settings, but still inferior to
383 Export is done in a buffer named \"*Org KOMA-LETTER Export*\". It
384 will be displayed if `org-export-show-temporary-export-buffer' is
388 (org-export-async-start
390 (with-current-buffer (get-buffer-create "*Org KOMA-LETTER Export*")
393 (goto-char (point-min))
395 (org-export-add-to-stack (current-buffer) 'koma-letter
)))
396 `(org-export-as 'koma-letter
,subtreep
,visible-only
,body-only
398 (let ((outbuf (org-export-to-buffer
399 'koma-letter
"*Org KOMA-LETTER Export*"
400 subtreep visible-only body-only ext-plist
)))
401 (with-current-buffer outbuf
(LaTeX-mode))
402 (when org-export-show-temporary-export-buffer
403 (switch-to-buffer-other-window outbuf
)))))
406 (defun org-koma-letter-export-to-latex
407 (&optional async subtreep visible-only body-only ext-plist
)
408 "Export current buffer as a KOMA Scrlttr2 letter (tex).
410 If narrowing is active in the current buffer, only export its
413 If a region is active, export that region.
415 A non-nil optional argument ASYNC means the process should happen
416 asynchronously. The resulting file should be accessible through
417 the `org-export-stack' interface.
419 When optional argument SUBTREEP is non-nil, export the sub-tree
420 at point, extracting information from the headline properties
423 When optional argument VISIBLE-ONLY is non-nil, don't export
424 contents of hidden elements.
426 When optional argument BODY-ONLY is non-nil, only write code
427 between \"\\begin{letter}\" and \"\\end{letter}\".
429 EXT-PLIST, when provided, is a property list with external
430 parameters overriding Org default settings, but still inferior to
433 When optional argument PUB-DIR is set, use it as the publishing
436 Return output file's name."
438 (let ((outfile (org-export-output-file-name ".tex" subtreep
)))
440 (org-export-async-start
441 (lambda (f) (org-export-add-to-stack f
'koma-letter
))
444 'koma-letter
,outfile
,subtreep
,visible-only
,body-only
447 'koma-letter outfile subtreep visible-only body-only ext-plist
))))
450 (defun org-koma-letter-export-to-pdf
451 (&optional async subtreep visible-only body-only ext-plist
)
452 "Export current buffer as a KOMA Scrlttr2 letter (pdf).
454 If narrowing is active in the current buffer, only export its
457 If a region is active, export that region.
459 A non-nil optional argument ASYNC means the process should happen
460 asynchronously. The resulting file should be accessible through
461 the `org-export-stack' interface.
463 When optional argument SUBTREEP is non-nil, export the sub-tree
464 at point, extracting information from the headline properties
467 When optional argument VISIBLE-ONLY is non-nil, don't export
468 contents of hidden elements.
470 When optional argument BODY-ONLY is non-nil, only write code
471 between \"\\begin{letter}\" and \"\\end{letter}\".
473 EXT-PLIST, when provided, is a property list with external
474 parameters overriding Org default settings, but still inferior to
477 Return PDF file's name."
480 (let ((outfile (org-export-output-file-name ".tex" subtreep
)))
481 (org-export-async-start
482 (lambda (f) (org-export-add-to-stack f
'koma-letter
))
486 'koma-letter
,outfile
,subtreep
,visible-only
,body-only
489 (org-koma-letter-export-to-latex
490 nil subtreep visible-only body-only ext-plist
))))
493 (provide 'ox-koma-letter
)
494 ;;; ox-koma-letter.el ends here