ox-koma-letter.el (koma-letter): Use `org-koma-letter-from-address'
[org-mode.git] / contrib / lisp / ox-koma-letter.el
blob1755fbea9c98968e03199208a9b8ffa7e4e6836a
1 ;;; ox-koma-letter.el --- KOMA Scrlttr2 Back-End for Org Export Engine
3 ;; Copyright (C) 2007-2012, 2014 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/>.
24 ;;; Commentary:
26 ;; This library implements a KOMA Scrlttr2 back-end, derived from the
27 ;; LaTeX one.
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
36 ;; keywords:
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 ;; - TO_ADDRESS: If unspecified this is set to "\mbox{}".
46 ;; TO_ADDRESS and FROM_ADDRESS can also be specified using heading
47 ;; with the special tags specified in
48 ;; `org-koma-letter-special-tags-in-letter', namely "to" and "from".
49 ;; LaTeX line breaks are not necessary if using these headings. If
50 ;; both a headline and a keyword specify a to or from address the
51 ;; value is determined in accordance with
52 ;; `org-koma-letter-prefer-special-headings'.
54 ;; A number of OPTIONS settings can be set to change which contents is
55 ;; exported.
56 ;; - backaddress (see `org-koma-letter-use-backaddress')
57 ;; - foldmarks (see `org-koma-letter-use-foldmarks')
58 ;; - phone (see `org-koma-letter-use-phone')
59 ;; - email (see `org-koma-letter-use-email')
60 ;; - place (see `org-koma-letter-use-place')
61 ;; - subject, a list of format options
62 ;; (see `org-koma-letter-subject-format')
63 ;; - after-closing-order, a list of the ordering of headings with
64 ;; special tags after closing (see
65 ;; `org-koma-letter-special-tags-after-closing')
66 ;; - after-letter-order, as above, but after the end of the letter
67 ;; (see `org-koma-letter-special-tags-after-letter').
69 ;; The following variables works differently from the main LaTeX class
70 ;; - AUTHOR: Default to user-full-name but may be disabled.
71 ;; (See also `org-koma-letter-author'),
72 ;; - EMAIL: Same as AUTHOR. (see also `org-koma-letter-email'),
74 ;; Headlines are in general ignored. However, headlines with special
75 ;; tags can be used for specified contents like postscript (ps),
76 ;; carbon copy (cc), enclosures (encl) and code to be inserted after
77 ;; \end{letter} (after_letter). Specials tags are defined in
78 ;; `org-koma-letter-special-tags-after-closing' and
79 ;; `org-koma-letter-special-tags-after-letter'. Currently members of
80 ;; `org-koma-letter-special-tags-after-closing' used as macros and the
81 ;; content of the headline is the argument.
83 ;; Headlines with two and from may also be used rather than the
84 ;; keyword approach described above. If both a keyword and a headline
85 ;; with information is present precedence is determined by
86 ;; `org-koma-letter-prefer-special-headings'.
88 ;; You will need to add an appropriate association in
89 ;; `org-latex-classes' in order to use the KOMA Scrlttr2 class.
90 ;; The easiest way to do this is by adding
92 ;; (eval-after-load "ox-koma-letter"
93 ;; '(org-koma-letter-plug-into-ox))
95 ;; to your init file. This will add a sparse scrlttr2 class and
96 ;; set it as the default `org-koma-latex-default-class'. You can also
97 ;; add you own letter class. For instace:
99 ;; (add-to-list 'org-latex-classes
100 ;; '("my-letter"
101 ;; "\\documentclass\[%
102 ;; DIV=14,
103 ;; fontsize=12pt,
104 ;; parskip=half,
105 ;; subject=titled,
106 ;; backaddress=false,
107 ;; fromalign=left,
108 ;; fromemail=true,
109 ;; fromphone=true\]\{scrlttr2\}
110 ;; \[DEFAULT-PACKAGES]
111 ;; \[PACKAGES]
112 ;; \[EXTRA]"))
114 ;; Then, in your Org document, be sure to require the proper class
115 ;; with:
117 ;; #+LATEX_CLASS: my-letter
119 ;; Or by setting `org-koma-letter-default-class'.
121 ;; You may have to load (LaTeX) Babel as well, e.g., by adding
122 ;; it to `org-latex-packages-alist',
124 ;; (add-to-list 'org-latex-packages-alist '("AUTO" "babel" nil))
126 ;;; Code:
128 (require 'ox-latex)
131 ;;; User-Configurable Variables
133 (defgroup org-export-koma-letter nil
134 "Options for exporting to KOMA scrlttr2 class in LaTeX export."
135 :tag "Org Koma-Letter"
136 :group 'org-export)
138 (defcustom org-koma-letter-class-option-file "NF"
139 "Letter Class Option File."
140 :group 'org-export-koma-letter
141 :type 'string)
143 (defcustom org-koma-letter-author 'user-full-name
144 "The sender's name.
146 This variable defaults to calling the function `user-full-name'
147 which just returns the current function `user-full-name'. Alternatively a
148 string, nil or a function may be given. Functions must return a
149 string."
150 :group 'org-export-koma-letter
151 :type '(radio (function-item user-full-name)
152 (string)
153 (function)
154 (const :tag "Do not export author" nil)))
156 (defcustom org-koma-letter-email 'org-koma-letter-email
157 "The sender's email address.
159 This variable defaults to the value `org-koma-letter-email' which
160 returns `user-mail-address'. Alternatively a string, nil or a
161 function may be given. Functions must return a string."
162 :group 'org-export-koma-letter
163 :type '(radio (function-item org-koma-letter-email)
164 (string)
165 (function)
166 (const :tag "Do not export email" nil)))
168 (defcustom org-koma-letter-from-address nil
169 "Sender's address, as a string."
170 :group 'org-export-koma-letter
171 :type 'string)
173 (defcustom org-koma-letter-phone-number nil
174 "Sender's phone number, as a string."
175 :group 'org-export-koma-letter
176 :type 'string)
178 (defcustom org-koma-letter-place nil
179 "Place from which the letter is sent."
180 :group 'org-export-koma-letter
181 :type 'string)
183 (defcustom org-koma-letter-opening nil
184 "Letter's opening, as a string.
186 If (1) this value is nil; (2) the letter is started with a
187 headline; and (3) `org-koma-letter-headline-is-opening-maybe' is
188 t the value opening will be implicit set as the headline title."
189 :group 'org-export-koma-letter
190 :type 'string)
192 (defcustom org-koma-letter-closing nil
193 "Koma-Letter's closing, as a string."
194 :group 'org-export-koma-letter
195 :type 'string)
197 (defcustom org-koma-letter-prefer-special-headings nil
198 "If TO and/or FROM is specified using both a heading and a keyword the heading value will be preferred if the variable is t."
199 :group 'org-export-koma-letter
200 :type 'boolean)
202 (defcustom org-koma-letter-signature nil
203 "String used as the signature."
204 :group 'org-export-koma-letter
205 :type 'string)
207 (defcustom org-koma-letter-subject-format t
208 "Use the title as the subject of the letter.
210 At this time the following values are allowed:
212 - afteropening: subject after opening.
213 - beforeopening: subject before opening.
214 - centered: subject centered.
215 - left:subject left-justified.
216 - right: subject right-justified.
217 - titled: add title/description to subject.
218 - underlined: set subject underlined.
219 - untitled: do not add title/description to subject.
220 - No-export: do no insert a subject even if present.
222 Please refer to the KOMA-script manual (Table 4.16. in the
223 English manual of 2012-07-22)."
224 :type '(radio
225 (const :tag "No export" nil)
226 (const :tag "Default options" t)
227 (set :tag "selection"
228 (const 'afteropening)
229 (const 'beforeopening)
230 (const 'centered)
231 (const 'left)
232 (const 'right)
233 (const 'underlined)
234 (const 'titled)
235 (const 'untitled))
236 (string))
237 :group 'org-export-koma-letter)
239 (defcustom org-koma-letter-use-backaddress nil
240 "Print return address in line above to address."
241 :group 'org-export-koma-letter
242 :type 'boolean)
244 (defcustom org-koma-letter-use-foldmarks "true"
245 "Configure appearence of fold marks.
247 Accepts any valid value for the KOMA-Script `foldmarks' option.
249 Use `foldmarks:true' to activate default fold marks or
250 `foldmarks:nil' to deactivate fold marks."
251 :group 'org-export-koma-letter
252 :type 'string)
254 (defcustom org-koma-letter-use-phone nil
255 "Print sender's phone number."
256 :group 'org-export-koma-letter
257 :type 'boolean)
259 (defcustom org-koma-letter-use-email nil
260 "Print sender's email address."
261 :group 'org-export-koma-letter
262 :type 'boolean)
264 (defcustom org-koma-letter-use-place t
265 "Print the letter's place next to the date."
266 :group 'org-export-koma-letter
267 :type 'boolean)
269 (defcustom org-koma-letter-default-class nil
270 "Default class for `org-koma-letter'.
272 The value must be a member of `org-latex-classes'."
273 :group 'org-export-koma-letter
274 :type 'string)
276 (defcustom org-koma-letter-headline-is-opening-maybe t
277 "Whether a headline may be used as an opening.
278 A headline is only used if #+OPENING is not set. See also
279 `org-koma-letter-opening'."
280 :group 'org-export-koma-letter
281 :type 'boolean)
283 (defconst org-koma-letter-special-tags-in-letter '(to from)
284 "Header tags related to the letter itself.")
286 (defconst org-koma-letter-special-tags-after-closing '(ps encl cc)
287 "Header tags to be inserted after closing.")
289 (defconst org-koma-letter-special-tags-after-letter '(after_letter)
290 "Header tags to be inserted after closing.")
292 (defvar org-koma-letter-special-contents nil
293 "Holds special content temporarily.")
297 ;;; Define Back-End
299 (org-export-define-derived-backend 'koma-letter 'latex
300 :options-alist
301 '((:lco "LCO" nil org-koma-letter-class-option-file)
302 (:latex-class "LATEX_CLASS" nil (if org-koma-letter-default-class
303 org-koma-letter-default-class
304 org-latex-default-class) t)
305 (:author "AUTHOR" nil (org-koma-letter--get-value org-koma-letter-author) t)
306 (:author-changed-in-buffer-p "AUTHOR" nil nil t)
307 (:from-address "FROM_ADDRESS" nil org-koma-letter-from-address newline)
308 (:phone-number "PHONE_NUMBER" nil org-koma-letter-phone-number)
309 (:email "EMAIL" nil (org-koma-letter--get-value org-koma-letter-email) t)
310 (:email-changed-in-buffer-p "EMAIL" nil nil t)
311 (:to-address "TO_ADDRESS" nil nil newline)
312 (:place "PLACE" nil org-koma-letter-place)
313 (:opening "OPENING" nil org-koma-letter-opening)
314 (:closing "CLOSING" nil org-koma-letter-closing)
315 (:signature "SIGNATURE" nil org-koma-letter-signature newline)
316 (:special-tags nil nil (append
317 org-koma-letter-special-tags-in-letter
318 org-koma-letter-special-tags-after-closing
319 org-koma-letter-special-tags-after-letter))
320 (:special-headings nil "special-headings"
321 org-koma-letter-prefer-special-headings)
322 (:with-after-closing nil "after-closing-order"
323 org-koma-letter-special-tags-after-closing)
324 (:with-after-letter nil "after-letter-order"
325 org-koma-letter-special-tags-after-letter)
326 (:with-backaddress nil "backaddress" org-koma-letter-use-backaddress)
327 (:with-backaddress-changed-in-buffer-p nil "backaddress" nil)
328 (:with-foldmarks nil "foldmarks" org-koma-letter-use-foldmarks)
329 (:with-foldmarks-changed-in-buffer-p nil "foldmarks" "foldmarks-not-set")
330 (:with-phone nil "phone" org-koma-letter-use-phone)
331 (:with-phone-changed-in-buffer-p nil "phone" nil)
332 (:with-email nil "email" org-koma-letter-use-email)
333 (:with-email-changed-in-buffer-p nil "email" nil)
334 (:with-place nil "place" org-koma-letter-use-place)
335 (:with-subject nil "subject" org-koma-letter-subject-format))
336 :translate-alist '((export-block . org-koma-letter-export-block)
337 (export-snippet . org-koma-letter-export-snippet)
338 (headline . org-koma-letter-headline)
339 (keyword . org-koma-letter-keyword)
340 (template . org-koma-letter-template))
341 :menu-entry
342 '(?k "Export with KOMA Scrlttr2"
343 ((?L "As LaTeX buffer" org-koma-letter-export-as-latex)
344 (?l "As LaTeX file" org-koma-letter-export-to-latex)
345 (?p "As PDF file" org-koma-letter-export-to-pdf)
346 (?o "As PDF file and open"
347 (lambda (a s v b)
348 (if a (org-koma-letter-export-to-pdf t s v b)
349 (org-open-file (org-koma-letter-export-to-pdf nil s v b))))))))
352 ;;; Initialize class function
354 (defun org-koma-letter-plug-into-ox ()
355 "Add a sparse `default-koma-letter' to `org-latex-classes' and set `org-koma-letter-default-class' to `default-koma-letter'."
356 (let ((class "default-koma-letter"))
357 (eval-after-load "ox-latex"
358 `(unless (member ,class 'org-latex-classes)
359 (add-to-list 'org-latex-classes
360 `(,class
361 "\\documentclass[11pt]{scrlttr2}") ())
362 (setq org-koma-letter-default-class class)))))
364 ;;; Helper functions
366 (defun org-koma-letter-email ()
367 "Return the current `user-mail-address'."
368 user-mail-address)
370 ;; The following is taken from/inspired by ox-grof.el
371 ;; Thanks, Luis!
373 (defun org-koma-letter--get-tagged-contents (key)
374 "Get contents from a headline tagged with KEY.
375 Technically, the contents is stored in `org-koma-letter-special-contents'."
376 (cdr (assoc (org-koma-letter--get-value key)
377 org-koma-letter-special-contents)))
379 (defun org-koma-letter--get-value (value)
380 "Determines if VALUE is nil, a string, a function or a symbol and return a string or nil."
381 (when value
382 (cond ((stringp value) value)
383 ((functionp value) (funcall value))
384 ((symbolp value) (symbol-name value))
385 (t value))))
388 (defun org-koma-letter--special-contents-as-macro (a-list &optional keep-newlines no-tag)
389 "Find members of `org-koma-letter-special-contents' corresponding to A-LIST.
390 Return them as a string to be formatted.
392 The function is used for inserting content of speciall headings
393 such as PS.
395 If KEEP-NEWLINES is t newlines will not be removed. If NO-TAG is
396 is t the content in `org-koma-letter-special-contents' will not
397 be wrapped in a macro named whatever the members of A-LIST are
398 called."
399 (let (output)
400 (dolist (ac* a-list output)
401 (let*
402 ((ac (org-koma-letter--get-value ac*))
403 (x (org-koma-letter--get-tagged-contents ac)))
404 (when x
405 (setq output
406 (concat
407 output "\n"
408 ;; sometimes LaTeX complains about newlines
409 ;; at the end or beginning of macros. Remove them.
410 (org-koma-letter--format-string-as-macro
411 (if keep-newlines x (org-koma-letter--normalize-string x))
412 (unless no-tag ac)))))))))
414 (defun org-koma-letter--format-string-as-macro (string &optional macro)
415 "Format STRING as \"\\macro{string}\" if MACRO is given else as \"string\"."
416 (if macro
417 (format "\\%s{%s}" macro string)
418 (format "%s" string)))
420 (defun org-koma-letter--normalize-string (string)
421 "Remove new lines in the beginning and end of `STRING'."
422 (replace-regexp-in-string "\\`[ \n\t]+\\|[\n\t ]*\\'" "" string))
424 (defun org-koma-letter--determine-to-and-from (info key)
425 "Given INFO determine KEY for the letter.
426 KEY should be `to' or `from'.
428 `ox-koma-letter' allows two ways to specify to and from. If both
429 are present return the preferred one as determined by
430 `org-koma-letter-prefer-special-headings'."
431 (let* ((plist-alist '((from . :from-address)
432 (to . :to-address)))
433 (default-alist `((from ,org-koma-letter-from-address)
434 (to "\\mbox{}")))
435 (option-value (plist-get info (cdr-safe (assoc key plist-alist))))
436 (head-value (org-koma-letter--get-tagged-contents key))
437 (order (append
438 (funcall
439 (if (plist-get info :special-headings)
440 'reverse 'identity)
441 `(,option-value ,head-value))
442 (cdr-safe (assoc key default-alist))))
444 (adr (dolist (x order tmp)
445 (when (and (not tmp) x)
446 (setq tmp x)))))
447 (when adr
448 (replace-regexp-in-string
449 "\n" "\\\\\\\\\n"
450 (org-koma-letter--normalize-string adr)))))
452 ;;; Transcode Functions
454 ;;;; Export Block
456 (defun org-koma-letter-export-block (export-block contents info)
457 "Transcode an EXPORT-BLOCK element into KOMA Scrlttr2 code.
458 CONTENTS is nil. INFO is a plist used as a communication
459 channel."
460 (when (member (org-element-property :type export-block) '("KOMA-LETTER" "LATEX"))
461 (org-remove-indentation (org-element-property :value export-block))))
463 ;;;; Export Snippet
465 (defun org-koma-letter-export-snippet (export-snippet contents info)
466 "Transcode an EXPORT-SNIPPET object into KOMA Scrlttr2 code.
467 CONTENTS is nil. INFO is a plist used as a communication
468 channel."
469 (when (memq (org-export-snippet-backend export-snippet) '(latex koma-letter))
470 (org-element-property :value export-snippet)))
472 ;;;; Keyword
474 (defun org-koma-letter-keyword (keyword contents info)
475 "Transcode a KEYWORD element into KOMA Scrlttr2 code.
476 CONTENTS is nil. INFO is a plist used as a communication
477 channel."
478 (let ((key (org-element-property :key keyword))
479 (value (org-element-property :value keyword)))
480 ;; Handle specifically BEAMER and TOC (headlines only) keywords.
481 ;; Otherwise, fallback to `latex' back-end.
482 (if (equal key "KOMA-LETTER") value
483 (org-export-with-backend 'latex keyword contents info))))
486 ;; Headline
488 (defun org-koma-letter-headline (headline contents info)
489 "Transcode a HEADLINE element from Org to LaTeX.
490 CONTENTS holds the contents of the headline. INFO is a plist
491 holding contextual information.
493 Note that if a headline is tagged with a tag from
494 `org-koma-letter-special-tags' it will not be exported, but
495 stored in `org-koma-letter-special-contents' and included at the
496 appropriate place."
497 (let*
498 ((tags (org-export-get-tags headline info))
499 (tag* (car tags))
500 (tag (when tag*
501 (car (member-ignore-case
502 tag*
503 (mapcar 'symbol-name (plist-get info :special-tags)))))))
504 (if tag
505 (progn
506 (push (cons tag contents)
507 org-koma-letter-special-contents)
508 nil)
509 (unless (or (plist-get info :opening)
510 (not org-koma-letter-headline-is-opening-maybe))
511 (plist-put info :opening
512 (org-export-data (org-element-property :title headline) info)))
513 contents)))
516 ;;;; Template
518 (defun org-koma-letter-template (contents info)
519 "Return complete document string after KOMA Scrlttr2 conversion.
520 CONTENTS is the transcoded contents string. INFO is a plist
521 holding export options."
522 ;; FIXME: instead of setq'ing org-koma-letter-special-contents and
523 ;; callying varioues stuff it might be nice to put a big let* around the templace
524 ;; as in org-groff...
525 (concat
526 ;; Time-stamp.
527 (and (plist-get info :time-stamp-file)
528 (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
529 ;; Document class and packages.
530 (let* ((class (plist-get info :latex-class))
531 (class-options (plist-get info :latex-class-options))
532 (header (nth 1 (assoc class org-latex-classes)))
533 (document-class-string
534 (and (stringp header)
535 (if (not class-options) header
536 (replace-regexp-in-string
537 "^[ \t]*\\\\documentclass\\(\\(\\[[^]]*\\]\\)?\\)"
538 class-options header t nil 1)))))
539 (if (not document-class-string)
540 (user-error "Unknown LaTeX class `%s'" class)
541 (org-latex-guess-babel-language
542 (org-latex-guess-inputenc
543 (org-element-normalize-string
544 (org-splice-latex-header
545 document-class-string
546 org-latex-default-packages-alist ; Defined in org.el.
547 org-latex-packages-alist nil ; Defined in org.el.
548 (concat (org-element-normalize-string (plist-get info :latex-header))
549 (plist-get info :latex-header-extra)))))
550 info)))
551 (let ((lco (plist-get info :lco))
552 (author (plist-get info :author))
553 (author-set (plist-get info :author-changed-in-buffer-p))
554 (from-address (org-koma-letter--determine-to-and-from info 'from))
555 (phone-number (plist-get info :phone-number))
556 (email (plist-get info :email))
557 (email-set (plist-get info :email-changed-in-buffer-p))
558 (signature (plist-get info :signature)))
559 (concat
560 ;; author or email not set in file: may be overridden by lco
561 (unless author-set
562 (when author (format "\\setkomavar{fromname}{%s}\n"
563 (org-export-data author info))))
564 (unless email-set
565 (when email (format "\\setkomavar{fromemail}{%s}\n" email)))
566 ;; Letter Class Option File
567 (when lco
568 (let ((lco-files (split-string lco " "))
569 (lco-def ""))
570 (dolist (lco-file lco-files lco-def)
571 (setq lco-def (format "%s\\LoadLetterOption{%s}\n" lco-def lco-file)))
572 lco-def))
573 ;; Define "From" data.
574 (when (and author author-set) (format "\\setkomavar{fromname}{%s}\n"
575 (org-export-data author info)))
576 (when from-address (format "\\setkomavar{fromaddress}{%s}\n" from-address))
577 (when phone-number
578 (format "\\setkomavar{fromphone}{%s}\n" phone-number))
579 (when (and email email-set) (format "\\setkomavar{fromemail}{%s}\n" email))
580 (when signature (format "\\setkomavar{signature}{%s}\n" signature))))
581 ;; Date.
582 (format "\\date{%s}\n" (org-export-data (org-export-get-date info) info))
583 ;; Place
584 (let ((with-place (plist-get info :with-place))
585 (place (plist-get info :place)))
586 (when (or place (not with-place))
587 (format "\\setkomavar{place}{%s}\n" (if with-place place ""))))
588 ;; KOMA options
589 (let ((with-backaddress (plist-get info :with-backaddress))
590 (with-backaddress-set (plist-get info :with-backaddress-changed-in-buffer-p))
591 (with-foldmarks (plist-get info :with-foldmarks))
592 (with-foldmarks-set
593 (not (string-equal (plist-get info :with-foldmarks-changed-in-buffer-p)
594 "foldmarks-not-set")))
595 (with-phone (plist-get info :with-phone))
596 (with-phone-set (plist-get info :with-phone-changed-in-buffer-p))
597 (with-email (plist-get info :with-email))
598 (with-email-set (plist-get info :with-email-changed-in-buffer-p)))
599 (concat
600 (when with-backaddress-set
601 (format "\\KOMAoption{backaddress}{%s}\n" (if with-backaddress "true" "false")))
602 (when with-foldmarks-set
603 (format "\\KOMAoption{foldmarks}{%s}\n" (if with-foldmarks with-foldmarks "false")))
604 (when with-phone-set
605 (format "\\KOMAoption{fromphone}{%s}\n" (if with-phone "true" "false")))
606 (when with-email-set
607 (format "\\KOMAoption{fromemail}{%s}\n" (if with-email "true" "false")))))
608 ;; Document start
609 "\\begin{document}\n\n"
610 ;; Subject
611 (let* ((with-subject (plist-get info :with-subject))
612 (subject-format (cond ((member with-subject '("true" "t" t)) nil)
613 ((stringp with-subject) (list with-subject))
614 ((symbolp with-subject)
615 (list (symbol-name with-subject)))
616 (t with-subject)))
617 (subject (org-export-data (plist-get info :title) info))
618 (l (length subject-format))
619 (y ""))
620 (concat
621 (when (and with-subject subject-format)
622 (concat
623 "\\KOMAoption{subject}{"
624 (apply 'format
625 (dotimes (x l y)
626 (setq y (concat (if (> x 0) "%s," "%s") y)))
627 subject-format) "}\n"))
628 (when (and subject with-subject)
629 (format "\\setkomavar{subject}{%s}\n\n" subject))))
630 ;; Letter start
631 (format "\\begin{letter}{%%\n%s}\n\n"
632 (org-koma-letter--determine-to-and-from info 'to))
633 ;; Opening.
634 (format "\\opening{%s}\n\n" (or (plist-get info :opening) ""))
635 ;; Letter body.
636 contents
637 ;; Closing.
638 (format "\n\\closing{%s}\n" (or (plist-get info :closing) ""))
639 (org-koma-letter--special-contents-as-macro
640 (plist-get info :with-after-closing))
641 ;; Letter end.
642 "\n\\end{letter}\n"
643 (org-koma-letter--special-contents-as-macro
644 (plist-get info :with-after-letter) t t)
645 ;; Document end.
646 "\n\\end{document}"
650 ;;; Commands
652 ;;;###autoload
653 (defun org-koma-letter-export-as-latex
654 (&optional async subtreep visible-only body-only ext-plist)
655 "Export current buffer as a KOMA Scrlttr2 letter.
657 If narrowing is active in the current buffer, only export its
658 narrowed part.
660 If a region is active, export that region.
662 A non-nil optional argument ASYNC means the process should happen
663 asynchronously. The resulting buffer should be accessible
664 through the `org-export-stack' interface.
666 When optional argument SUBTREEP is non-nil, export the sub-tree
667 at point, extracting information from the headline properties
668 first.
670 When optional argument VISIBLE-ONLY is non-nil, don't export
671 contents of hidden elements.
673 When optional argument BODY-ONLY is non-nil, only write code
674 between \"\\begin{letter}\" and \"\\end{letter}\".
676 EXT-PLIST, when provided, is a proeprty list with external
677 parameters overriding Org default settings, but still inferior to
678 file-local settings.
680 Export is done in a buffer named \"*Org KOMA-LETTER Export*\". It
681 will be displayed if `org-export-show-temporary-export-buffer' is
682 non-nil."
683 (interactive)
684 (let (org-koma-letter-special-contents)
685 (org-export-to-buffer 'koma-letter "*Org KOMA-LETTER Export*"
686 async subtreep visible-only body-only ext-plist
687 (lambda () (LaTeX-mode)))))
689 ;;;###autoload
690 (defun org-koma-letter-export-to-latex
691 (&optional async subtreep visible-only body-only ext-plist)
692 "Export current buffer as a KOMA Scrlttr2 letter (tex).
694 If narrowing is active in the current buffer, only export its
695 narrowed part.
697 If a region is active, export that region.
699 A non-nil optional argument ASYNC means the process should happen
700 asynchronously. The resulting file should be accessible through
701 the `org-export-stack' interface.
703 When optional argument SUBTREEP is non-nil, export the sub-tree
704 at point, extracting information from the headline properties
705 first.
707 When optional argument VISIBLE-ONLY is non-nil, don't export
708 contents of hidden elements.
710 When optional argument BODY-ONLY is non-nil, only write code
711 between \"\\begin{letter}\" and \"\\end{letter}\".
713 EXT-PLIST, when provided, is a property list with external
714 parameters overriding Org default settings, but still inferior to
715 file-local settings.
717 When optional argument PUB-DIR is set, use it as the publishing
718 directory.
720 Return output file's name."
721 (interactive)
722 (let ((outfile (org-export-output-file-name ".tex" subtreep))
723 (org-koma-letter-special-contents))
724 (org-export-to-file 'koma-letter outfile
725 async subtreep visible-only body-only ext-plist)))
727 ;;;###autoload
728 (defun org-koma-letter-export-to-pdf
729 (&optional async subtreep visible-only body-only ext-plist)
730 "Export current buffer as a KOMA Scrlttr2 letter (pdf).
732 If narrowing is active in the current buffer, only export its
733 narrowed part.
735 If a region is active, export that region.
737 A non-nil optional argument ASYNC means the process should happen
738 asynchronously. The resulting file should be accessible through
739 the `org-export-stack' interface.
741 When optional argument SUBTREEP is non-nil, export the sub-tree
742 at point, extracting information from the headline properties
743 first.
745 When optional argument VISIBLE-ONLY is non-nil, don't export
746 contents of hidden elements.
748 When optional argument BODY-ONLY is non-nil, only write code
749 between \"\\begin{letter}\" and \"\\end{letter}\".
751 EXT-PLIST, when provided, is a property list with external
752 parameters overriding Org default settings, but still inferior to
753 file-local settings.
755 Return PDF file's name."
756 (interactive)
757 (let ((file (org-export-output-file-name ".tex" subtreep))
758 (org-koma-letter-special-contents))
759 (org-export-to-file 'koma-letter file
760 async subtreep visible-only body-only ext-plist
761 (lambda (file) (org-latex-compile file)))))
764 (provide 'ox-koma-letter)
765 ;;; ox-koma-letter.el ends here