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