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 ;; Viktor Rosenfeld <listuser36 AT gmail DOT com>
8 ;; Rasmus Pank Roulund <emacs AT pank DOT eu>
9 ;; Keywords: org, wp, tex
11 ;; This program is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; This program is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;; This library implements a KOMA Scrlttr2 back-end, derived from the
29 ;; Depending on the desired output format, three commands are provided
30 ;; for export: `org-koma-letter-export-as-latex' (temporary buffer),
31 ;; `org-koma-letter-export-to-latex' ("tex" file) and
32 ;; `org-koma-letter-export-to-pdf' ("pdf" file).
34 ;; On top of buffer keywords supported by `latex' back-end (see
35 ;; `org-latex-options-alist'), this back-end introduces the following
37 ;; - "CLOSING" (see `org-koma-letter-closing'),
38 ;; - "FROM_ADDRESS" (see `org-koma-letter-from-address'),
39 ;; - "LCO" (see `org-koma-letter-class-option-file'),
40 ;; - "OPENING" (see `org-koma-letter-opening'),
41 ;; - "PHONE_NUMBER" (see `org-koma-letter-phone-number'),
42 ;; - "SIGNATURE" (see `org-koma-letter-signature')
43 ;; - "PLACE" (see `org-koma-letter-place')
44 ;; - and "TO_ADDRESS".
46 ;; A number of OPTIONS settings can be set to change which contents is
48 ;; - backaddress (see `org-koma-letter-use-backaddress')
49 ;; - foldmarks (see `org-koma-letter-use-foldmarks')
50 ;; - phone (see `org-koma-letter-use-phone')
51 ;; - email (see `org-koma-letter-use-email')
52 ;; - place (see `org-koma-letter-use-place')
53 ;; - subject, a list of format options
54 ;; (see `org-koma-letter-subject-format')
55 ;; - after-closing-order, a list of the ordering of headings with
56 ;; special tags after closing (see
57 ;; `org-koma-letter-special-tags-after-closing') -
58 ;; after-letter-order, as above, but after the end of the letter
59 ;; (see `org-koma-letter-special-tags-after-letter').
61 ;; The following variables works differently from the main LaTeX class
62 ;; - "AUTHOR": default to user-full-name but may be disabled. (see org-koma-letter-author),
63 ;; - "EMAIL": same as AUTHOR, (see org-koma-letter-email),
65 ;; Headlines are in general ignored. However, headlines with special
66 ;; tags can be used for specified contents like postscript (ps),
67 ;; carbon copy (cc), enclosures (encl) and code to be inserted after
68 ;; \end{letter} (after_letter). Specials tags are defined in
69 ;; `org-koma-letter-special-tags-after-closing' and
70 ;; `org-koma-letter-special-tags-after-letter'. Currently members of
71 ;; `org-koma-letter-special-tags-after-closing' used as macros and the
72 ;; content of the headline is the argument.
74 ;; You will need to add an appropriate association in
75 ;; `org-latex-classes' in order to use the KOMA Scrlttr2 class. For
76 ;; example, you can use the following code:
78 ;; (add-to-list 'org-latex-classes
80 ;; "\\documentclass\[%
88 ;; fromphone=true\]\{scrlttr2\}
89 ;; \[DEFAULT-PACKAGES]
93 ;; Then, in your Org document, be sure to require the proper class
96 ;; #+LATEX_CLASS: my-letter
104 ;;; User-Configurable Variables
106 (defgroup org-export-koma-letter nil
107 "Options for exporting to KOMA scrlttr2 class in LaTeX export."
108 :tag
"Org Koma-Letter"
111 (defcustom org-koma-letter-class-option-file
"NF"
112 "Letter Class Option File."
113 :group
'org-export-koma-letter
116 (defcustom org-koma-letter-author
'user-full-name
119 This variable defaults to calling the function `user-full-name'
120 which just returns the current `user-full-name'. Alternatively a
121 string, nil or a function may be given. Functions must return a
123 :group
'org-export-koma-letter
124 :type
'(radio (function-item user-full-name
)
127 (const :tag
"Do not export author" nil
)))
129 (defcustom org-koma-letter-email
'org-koma-letter-email
130 "The sender's email address.
132 This variable defaults to the value `org-koma-letter-email' which
133 returns `user-mail-address'. Alternatively a string, nil or a
134 function may be given. Functions must return a string."
135 :group
'org-export-koma-letter
136 :type
'(radio (function-item org-koma-letter-email
)
139 (const :tag
"Do not export email" nil
)))
141 (defcustom org-koma-letter-from-address nil
142 "Sender's address, as a string."
143 :group
'org-export-koma-letter
146 (defcustom org-koma-letter-phone-number nil
147 "Sender's phone number, as a string."
148 :group
'org-export-koma-letter
151 (defcustom org-koma-letter-place nil
152 "Place from which the letter is sent."
153 :group
'org-export-koma-letter
156 (defcustom org-koma-letter-opening nil
157 "Letter's opening, as a string."
158 :group
'org-export-koma-letter
161 (defcustom org-koma-letter-closing nil
162 "Koma-Letter's closing, as a string."
163 :group
'org-export-koma-letter
166 (defcustom org-koma-letter-signature nil
167 "String used as the signature."
168 :group
'org-export-koma-letter
171 (defcustom org-koma-letter-subject-format t
172 "Use the title as the subject of the letter. At the time of
173 writing the following values are allowed:
175 - afteropening: subject after opening
176 - beforeopening: subject before opening
177 - centered: subject centered
178 - left:subject left-justified
179 - right: subject right-justified
180 - titled: add title/description to subject
181 - underlined: set subject underlined (see note in text please)
182 - untitled: do not add title/description to subject.
183 - No-export: do no insert a subject even if present.
185 Please refer to the KOMA-script manual (Table 4.16. in the
186 English manual of 2012-07-22)"
188 (const :tag
"No export" nil
)
189 (const :tag
"Default options" t
)
190 (set :tag
"selection"
191 (const 'afteropening
)
192 (const 'beforeopening
)
200 :group
'org-export-koma-letter
)
204 (defcustom org-koma-letter-use-backaddress t
205 "Print return address in small line above to address."
206 :group
'org-export-koma-letter
209 (defcustom org-koma-letter-use-foldmarks
"true"
210 "Configure appearence of fold marks.
212 Accepts any valid value for the KOMA-Script `foldmarks' option.
214 Use `foldmarks:true' to activate default fold marks or
215 `foldmarks:nil' to deactivate fold marks."
216 :group
'org-export-koma-letter
219 (defcustom org-koma-letter-use-phone t
220 "Print sender's phone number."
221 :group
'org-export-koma-letter
224 (defcustom org-koma-letter-use-email t
225 "Print sender's email address."
226 :group
'org-export-koma-letter
229 (defcustom org-koma-letter-use-place t
230 "Print the letter's place next to the date."
231 :group
'org-export-koma-letter
236 (defconst org-koma-letter-special-tags-after-closing
'(ps encl cc
)
237 "Header tags to be inserted after closing")
239 (defconst org-koma-letter-special-tags-after-letter
'(after_letter)
240 "Header tags to be inserted after closing")
242 (defvar org-koma-letter-special-contents nil
243 "holds special content temporarily.")
249 (org-export-define-derived-backend 'koma-letter
'latex
251 '((:lco
"LCO" nil org-koma-letter-class-option-file
)
252 (:author
"AUTHOR" nil
(org-koma-letter--get-custom org-koma-letter-author
) t
)
253 (:from-address
"FROM_ADDRESS" nil org-koma-letter-from-address newline
)
254 (:phone-number
"PHONE_NUMBER" nil org-koma-letter-phone-number
)
255 (:email
"EMAIL" nil
(org-koma-letter--get-custom org-koma-letter-email
) t
)
256 (:to-address
"TO_ADDRESS" nil nil newline
)
257 (:place
"PLACE" nil org-koma-letter-place
)
258 (:opening
"OPENING" nil org-koma-letter-opening
)
259 (:closing
"CLOSING" nil org-koma-letter-closing
)
260 (:signature
"SIGNATURE" nil org-koma-letter-signature newline
)
261 (:special-tags nil nil
262 (append org-koma-letter-special-tags-after-closing
263 org-koma-letter-special-tags-after-letter
))
264 (:with-after-closing nil
"after-closing-order"
265 org-koma-letter-special-tags-after-closing
)
266 (:with-after-letter nil
"after-letter-order"
267 org-koma-letter-special-tags-after-letter
)
268 (:with-backaddress nil
"backaddress" org-koma-letter-use-backaddress
)
269 (:with-foldmarks nil
"foldmarks" org-koma-letter-use-foldmarks
)
270 (:with-phone nil
"phone" org-koma-letter-use-phone
)
271 (:with-email nil
"email" org-koma-letter-use-email
)
272 (:with-place nil
"place" org-koma-letter-use-place
)
273 (:with-subject nil
"subject" org-koma-letter-subject-format
))
274 :translate-alist
'((export-block . org-koma-letter-export-block
)
275 (export-snippet . org-koma-letter-export-snippet
)
276 (headline . org-koma-letter-headline
)
277 (keyword . org-koma-letter-keyword
)
278 (template . org-koma-letter-template
))
280 '(?k
"Export with KOMA Scrlttr2"
281 ((?L
"As LaTeX buffer" org-koma-letter-export-as-latex
)
282 (?l
"As LaTeX file" org-koma-letter-export-to-latex
)
283 (?p
"As PDF file" org-koma-letter-export-to-pdf
)
284 (?o
"As PDF file and open"
286 (if a
(org-koma-letter-export-to-pdf t s v b
)
287 (org-open-file (org-koma-letter-export-to-pdf nil s v b
))))))))
292 (defun org-koma-letter-email ()
293 "Return the current `user-mail-address'"
296 ;; The following is taken from/inspired by ox-grof.el
299 (defun org-koma-letter--get-tagged-contents (key)
300 "Get tagged content from `org-koma-letter-special-contents'"
301 (cdr (assoc (org-koma-letter--get-custom key
)
302 org-koma-letter-special-contents
)))
304 (defun org-koma-letter--get-custom (value)
305 "Determines whether a value is nil, a string or a
306 function (a symobl). If it is a function it it evaluates it."
308 (cond ((stringp value
) value
)
309 ((functionp value
) (funcall value
))
310 ((symbolp value
) (symbol-name value
))
314 (defun org-koma-letter--prepare-special-contents-as-macro (a-list &optional keep-newlines no-tag
)
315 "Finds all the components of `org-koma-letter-special-contents'
316 corresponding to members of the `a-list' and return them as a
317 string to be formatted. The function is used for inserting
318 content of speciall headings such as PS.
320 If keep-newlines is t newlines will not be removed. If no-tag is
321 is t the content in `org-koma-letter-special-contents' will not
322 be wrapped in a macro named whatever the members of a-list are called.
325 (dolist (ac* a-list output
)
327 ((ac (org-koma-letter--get-custom ac
*))
328 (x (org-koma-letter--get-tagged-contents ac
)))
333 ;; sometimes LaTeX complains about newlines
334 ;; at the end or beginning of macros. Remove them.
335 (org-koma-letter--format-string-as-macro
336 (if keep-newlines x
(org-koma-letter--remove-offending-new-lines x
))
337 (unless no-tag ac
)))))))))
339 (defun org-koma-letter--format-string-as-macro (string &optional macro
)
340 "If a macro is given format as string as \"\\macro{string}\" else as \"string\""
342 (format "\\%s{%s}" macro string
)
343 (format "%s" string
)))
345 (defun org-koma-letter--remove-offending-new-lines (string)
346 "Remove new lines in the begging and end of `string'"
347 (replace-regexp-in-string "\\`[ \n\t]+\\|[\n\t ]*\\'" "" string
))
349 ;;; Transcode Functions
353 (defun org-koma-letter-export-block (export-block contents info
)
354 "Transcode an EXPORT-BLOCK element into KOMA Scrlttr2 code.
355 CONTENTS is nil. INFO is a plist used as a communication
357 (when (member (org-element-property :type export-block
) '("KOMA-LETTER" "LATEX"))
358 (org-remove-indentation (org-element-property :value export-block
))))
362 (defun org-koma-letter-export-snippet (export-snippet contents info
)
363 "Transcode an EXPORT-SNIPPET object into KOMA Scrlttr2 code.
364 CONTENTS is nil. INFO is a plist used as a communication
366 (when (memq (org-export-snippet-backend export-snippet
) '(latex koma-letter
))
367 (org-element-property :value export-snippet
)))
371 (defun org-koma-letter-keyword (keyword contents info
)
372 "Transcode a KEYWORD element into KOMA Scrlttr2 code.
373 CONTENTS is nil. INFO is a plist used as a communication
375 (let ((key (org-element-property :key keyword
))
376 (value (org-element-property :value keyword
)))
377 ;; Handle specifically BEAMER and TOC (headlines only) keywords.
378 ;; Otherwise, fallback to `latex' back-end.
379 (if (equal key
"KOMA-LETTER") value
380 (org-export-with-backend 'latex keyword contents info
))))
385 (defun org-koma-letter-headline (headline contents info
)
386 "Transcode a HEADLINE element from Org to LaTeX.
387 CONTENTS holds the contents of the headline. INFO is a plist
388 holding contextual information.
390 Note that if a headline is tagged with a tag from
391 `org-koma-letter-special-tags' it will not be exported, but
392 stored in `org-koma-letter-special-contents' and included at the
395 ((tags (org-export-get-tags headline info
))
398 (car (member-ignore-case
400 (mapcar 'symbol-name
(plist-get info
:special-tags
)))))))
403 (push (cons tag contents
)
404 org-koma-letter-special-contents
)
411 (defun org-koma-letter-template (contents info
)
412 "Return complete document string after KOMA Scrlttr2 conversion.
413 CONTENTS is the transcoded contents string. INFO is a plist
414 holding export options."
415 ;; FIXME: instead of setq'ing org-koma-letter-special-contents and
416 ;; callying varioues stuff it might be nice to put a big let* around the templace
417 ;; as in org-groff...
420 (and (plist-get info
:time-stamp-file
)
421 (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
422 ;; Document class and packages.
423 (let ((class (plist-get info
:latex-class
))
424 (class-options (plist-get info
:latex-class-options
)))
425 (org-element-normalize-string
426 (let* ((header (nth 1 (assoc class org-latex-classes
)))
427 (document-class-string
428 (and (stringp header
)
429 (if (not class-options
) header
430 (replace-regexp-in-string
431 "^[ \t]*\\\\documentclass\\(\\(\\[[^]]*\\]\\)?\\)"
432 class-options header t nil
1)))))
433 (if (not document-class-string
)
434 (user-error "Unknown LaTeX class `%s'")
435 (org-latex-guess-babel-language
436 (org-latex-guess-inputenc
437 (org-splice-latex-header
438 document-class-string
439 org-latex-default-packages-alist
; defined in org.el
440 org-latex-packages-alist nil
; defined in org.el
441 (concat (plist-get info
:latex-header
)
442 (plist-get info
:latex-header-extra
))))
444 (let ((lco (plist-get info
:lco
))
445 (author (plist-get info
:author
))
446 (from-address (plist-get info
:from-address
))
447 (phone-number (plist-get info
:phone-number
))
448 (email (plist-get info
:email
))
449 (signature (plist-get info
:signature
)))
451 ;; Letter Class Option File
453 (let ((lco-files (split-string lco
" "))
455 (dolist (lco-file lco-files lco-def
)
456 (setq lco-def
(format "%s\\LoadLetterOption{%s}\n" lco-def lco-file
)))
458 ;; Define "From" data.
459 (when author
(format "\\setkomavar{fromname}{%s}\n"
460 (org-export-data author info
)))
461 (when from-address
(format "\\setkomavar{fromaddress}{%s}\n" from-address
))
462 (when phone-number
(format "\\setkomavar{fromphone}{%s}\n" phone-number
))
463 (when email
(format "\\setkomavar{fromemail}{%s}\n" email
))
464 (when signature
(format "\\setkomavar{signature}{%s}\n" signature
))))
466 (format "\\date{%s}\n" (org-export-data (org-export-get-date info
) info
))
468 (let ((with-place (plist-get info
:with-place
))
469 (place (plist-get info
:place
)))
470 (when (or place
(not with-place
))
471 (format "\\setkomavar{place}{%s}\n" (if with-place place
""))))
473 (let ((with-backaddress (plist-get info
:with-backaddress
))
474 (with-foldmarks (plist-get info
:with-foldmarks
))
475 (with-phone (plist-get info
:with-phone
))
476 (with-email (plist-get info
:with-email
)))
478 (format "\\KOMAoption{backaddress}{%s}\n" (if with-backaddress
"true" "false"))
479 (format "\\KOMAoption{foldmarks}{%s}\n" (if with-foldmarks with-foldmarks
"false"))
480 (format "\\KOMAoption{fromphone}{%s}\n" (if with-phone
"true" "false"))
481 (format "\\KOMAoption{fromemail}{%s}\n" (if with-email
"true" "false"))))
483 "\\begin{document}\n\n"
485 (let* ((with-subject (plist-get info
:with-subject
))
486 (subject-format (cond ((member with-subject
'("true" "t" t
)) nil
)
487 ((stringp with-subject
) (list with-subject
))
488 ((symbolp with-subject
)
489 (list (symbol-name with-subject
)))
491 (subject (org-export-data (plist-get info
:title
) info
))
492 (l (length subject-format
))
495 (when (and with-subject subject-format
)
497 "\\KOMAoption{subject}{"
500 (setq y
(concat (if (> x
0) "%s," "%s") y
)))
501 subject-format
) "}\n"))
502 (when (and subject with-subject
)
503 (format "\\setkomavar{subject}{%s}\n\n" subject
))))
505 (format "\\begin{letter}{%%\n%s}\n\n"
506 (or (plist-get info
:to-address
) "no address given"))
508 (format "\\opening{%s}\n\n" (plist-get info
:opening
))
512 (format "\n\\closing{%s}\n" (plist-get info
:closing
))
513 (org-koma-letter--prepare-special-contents-as-macro
514 (plist-get info
:with-after-closing
))
517 (org-koma-letter--prepare-special-contents-as-macro
518 (plist-get info
:with-after-letter
) t t
)
527 (defun org-koma-letter-export-as-latex
528 (&optional async subtreep visible-only body-only ext-plist
)
529 "Export current buffer as a KOMA Scrlttr2 letter.
531 If narrowing is active in the current buffer, only export its
534 If a region is active, export that region.
536 A non-nil optional argument ASYNC means the process should happen
537 asynchronously. The resulting buffer should be accessible
538 through the `org-export-stack' interface.
540 When optional argument SUBTREEP is non-nil, export the sub-tree
541 at point, extracting information from the headline properties
544 When optional argument VISIBLE-ONLY is non-nil, don't export
545 contents of hidden elements.
547 When optional argument BODY-ONLY is non-nil, only write code
548 between \"\\begin{letter}\" and \"\\end{letter}\".
550 EXT-PLIST, when provided, is a proeprty list with external
551 parameters overriding Org default settings, but still inferior to
554 Export is done in a buffer named \"*Org KOMA-LETTER Export*\". It
555 will be displayed if `org-export-show-temporary-export-buffer' is
558 (let (org-koma-letter-special-contents)
560 (org-export-async-start
562 (with-current-buffer (get-buffer-create "*Org KOMA-LETTER Export*")
565 (goto-char (point-min))
567 (org-export-add-to-stack (current-buffer) 'koma-letter
)))
568 `(org-export-as 'koma-letter
,subtreep
,visible-only
,body-only
570 (let ((outbuf (org-export-to-buffer
571 'koma-letter
"*Org KOMA-LETTER Export*"
572 subtreep visible-only body-only ext-plist
)))
573 (with-current-buffer outbuf
(LaTeX-mode))
574 (when org-export-show-temporary-export-buffer
575 (switch-to-buffer-other-window outbuf
))))))
578 (defun org-koma-letter-export-to-latex
579 (&optional async subtreep visible-only body-only ext-plist
)
580 "Export current buffer as a KOMA Scrlttr2 letter (tex).
582 If narrowing is active in the current buffer, only export its
585 If a region is active, export that region.
587 A non-nil optional argument ASYNC means the process should happen
588 asynchronously. The resulting file should be accessible through
589 the `org-export-stack' interface.
591 When optional argument SUBTREEP is non-nil, export the sub-tree
592 at point, extracting information from the headline properties
595 When optional argument VISIBLE-ONLY is non-nil, don't export
596 contents of hidden elements.
598 When optional argument BODY-ONLY is non-nil, only write code
599 between \"\\begin{letter}\" and \"\\end{letter}\".
601 EXT-PLIST, when provided, is a property list with external
602 parameters overriding Org default settings, but still inferior to
605 When optional argument PUB-DIR is set, use it as the publishing
608 Return output file's name."
610 (let ((outfile (org-export-output-file-name ".tex" subtreep
))
611 org-koma-letter-special-contents
)
613 (org-export-async-start
614 (lambda (f) (org-export-add-to-stack f
'koma-letter
))
617 'koma-letter
,outfile
,subtreep
,visible-only
,body-only
620 'koma-letter outfile subtreep visible-only body-only ext-plist
))))
623 (defun org-koma-letter-export-to-pdf
624 (&optional async subtreep visible-only body-only ext-plist
)
625 "Export current buffer as a KOMA Scrlttr2 letter (pdf).
627 If narrowing is active in the current buffer, only export its
630 If a region is active, export that region.
632 A non-nil optional argument ASYNC means the process should happen
633 asynchronously. The resulting file should be accessible through
634 the `org-export-stack' interface.
636 When optional argument SUBTREEP is non-nil, export the sub-tree
637 at point, extracting information from the headline properties
640 When optional argument VISIBLE-ONLY is non-nil, don't export
641 contents of hidden elements.
643 When optional argument BODY-ONLY is non-nil, only write code
644 between \"\\begin{letter}\" and \"\\end{letter}\".
646 EXT-PLIST, when provided, is a property list with external
647 parameters overriding Org default settings, but still inferior to
650 Return PDF file's name."
653 (let ((outfile (org-export-output-file-name ".tex" subtreep
)))
654 (org-export-async-start
655 (lambda (f) (org-export-add-to-stack f
'koma-letter
))
659 'koma-letter
,outfile
,subtreep
,visible-only
,body-only
662 (org-koma-letter-export-to-latex
663 nil subtreep visible-only body-only ext-plist
))))
666 (provide 'ox-koma-letter
)
667 ;;; ox-koma-letter.el ends here