Documentation fixes for ox-koma-script.el
[org-mode/org-kjn.git] / contrib / lisp / ox-koma-letter.el
blob0d15956b734e5f94ed5cbfaa3cea00b2ab435fb2
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 need an appropriate association in `org-latex-classes' in order
89 ;; to use the KOMA Scrlttr2 class. By default, a sparse scrlttr2
90 ;; class is provided: "default-koma-letter". You can also add you own
91 ;; letter class. For instance:
93 ;; (add-to-list 'org-latex-classes
94 ;; '("my-letter"
95 ;; "\\documentclass\[%
96 ;; DIV=14,
97 ;; fontsize=12pt,
98 ;; parskip=half,
99 ;; subject=titled,
100 ;; backaddress=false,
101 ;; fromalign=left,
102 ;; fromemail=true,
103 ;; fromphone=true\]\{scrlttr2\}
104 ;; \[DEFAULT-PACKAGES]
105 ;; \[PACKAGES]
106 ;; \[EXTRA]"))
108 ;; Then, in your Org document, be sure to require the proper class
109 ;; with:
111 ;; #+LATEX_CLASS: my-letter
113 ;; Or by setting `org-koma-letter-default-class'.
115 ;; You may have to load (LaTeX) Babel as well, e.g., by adding
116 ;; it to `org-latex-packages-alist',
118 ;; (add-to-list 'org-latex-packages-alist '("AUTO" "babel" nil))
120 ;;; Code:
122 (require 'ox-latex)
124 ;; Install a default letter class.
125 (unless (assoc "default-koma-letter" org-latex-classes)
126 (add-to-list 'org-latex-classes
127 '("default-koma-letter" "\\documentclass[11pt]{scrlttr2}")))
130 ;;; User-Configurable Variables
132 (defgroup org-export-koma-letter nil
133 "Options for exporting to KOMA scrlttr2 class in LaTeX export."
134 :tag "Org Koma-Letter"
135 :group 'org-export)
137 (defcustom org-koma-letter-class-option-file "NF"
138 "Letter Class Option File.
139 This option can also be set with the LCO keyword."
140 :group 'org-export-koma-letter
141 :type 'string)
143 (defcustom org-koma-letter-author 'user-full-name
144 "Sender's name.
146 This variable defaults to calling the function `user-full-name'
147 which just returns the current function `user-full-name'.
148 Alternatively a string, nil or a function may be given.
149 Functions must return a string.
151 This option can also be set with the AUTHOR keyword."
152 :group 'org-export-koma-letter
153 :type '(radio (function-item user-full-name)
154 (string)
155 (function)
156 (const :tag "Do not export author" nil)))
158 (defcustom org-koma-letter-email 'org-koma-letter-email
159 "Sender's email address.
161 This variable defaults to the value `org-koma-letter-email' which
162 returns `user-mail-address'. Alternatively a string, nil or
163 a function may be given. Functions must return a string.
165 This option can also be set with the EMAIL keyword."
166 :group 'org-export-koma-letter
167 :type '(radio (function-item org-koma-letter-email)
168 (string)
169 (function)
170 (const :tag "Do not export email" nil)))
172 (defcustom org-koma-letter-from-address ""
173 "Sender's address, as a string.
174 This option can also be set with one or more FROM_ADDRESS
175 keywords."
176 :group 'org-export-koma-letter
177 :type 'string)
179 (defcustom org-koma-letter-phone-number ""
180 "Sender's phone number, as a string.
181 This option can also be set with the PHONE_NUMBER keyword."
182 :group 'org-export-koma-letter
183 :type 'string)
185 (defcustom org-koma-letter-place ""
186 "Place from which the letter is sent, as a string.
187 This option can also be set with the PLACE keyword."
188 :group 'org-export-koma-letter
189 :type 'string)
191 (defcustom org-koma-letter-opening ""
192 "Letter's opening, as a string.
194 This option can also be set with the OPENING keyword. Moreover,
195 when:
196 (1) this value is the empty string;
197 (2) there's no OPENING keyword or it is empty;
198 (3) `org-koma-letter-headline-is-opening-maybe' is non-nil;
199 (4) the letter contains a headline without a special
200 tag (e.g. \"to\" or \"ps\");
201 then the opening will be implicitly set as the headline title."
202 :group 'org-export-koma-letter
203 :type 'string)
205 (defcustom org-koma-letter-closing ""
206 "Letter's closing, as a string.
207 This option can also be set with the CLOSING keyword."
208 :group 'org-export-koma-letter
209 :type 'string)
211 (defcustom org-koma-letter-signature ""
212 "Signature, as a string.
213 This option can also be set with the SIGNATURE keyword."
214 :group 'org-export-koma-letter
215 :type 'string)
217 (defcustom org-koma-letter-prefer-special-headings nil
218 "Non-nil means prefer headlines over keywords for TO and FROM.
219 This option can also be set with the OPTIONS keyword, e.g.:
220 \"special-headings:t\"."
221 :group 'org-export-koma-letter
222 :type 'boolean)
224 (defcustom org-koma-letter-subject-format t
225 "Use the title as the subject of the letter.
227 When t, insert a subject using default options. When nil, do not
228 insert a subject at all. It can also be a list of symbols among
229 the following ones:
231 `afteropening' Subject after opening
232 `beforeopening' Subject before opening
233 `centered' Subject centered
234 `left' Subject left-justified
235 `right' Subject right-justified
236 `titled' Add title/description to subject
237 `underlined' Set subject underlined
238 `untitled' Do not add title/description to subject
240 Please refer to the KOMA-script manual (Table 4.16. in the
241 English manual of 2012-07-22).
243 This option can also be set with the OPTIONS keyword, e.g.:
244 \"subject:(underlined centered)\"."
245 :type
246 '(choice
247 (const :tag "No export" nil)
248 (const :tag "Default options" t)
249 (set :tag "Configure options"
250 (const :tag "Subject after opening" afteropening)
251 (const :tag "Subject before opening" beforeopening)
252 (const :tag "Subject centered" centered)
253 (const :tag "Subject left-justified" left)
254 (const :tag "Subject right-justified" right)
255 (const :tag "Add title or description to subject" underlined)
256 (const :tag "Set subject underlined" titled)
257 (const :tag "Do not add title or description to subject" untitled)))
258 :group 'org-export-koma-letter)
260 (defcustom org-koma-letter-use-backaddress nil
261 "Non-nil prints return address in line above to address.
262 This option can also be set with the OPTIONS keyword, e.g.:
263 \"backaddress:t\"."
264 :group 'org-export-koma-letter
265 :type 'boolean)
267 (defcustom org-koma-letter-use-foldmarks t
268 "Configure appearance of folding marks.
270 When t, activate default folding marks. When nil, do not insert
271 folding marks at all. It can also be a list of symbols among the
272 following ones:
274 `B' Activate upper horizontal mark on left paper edge
275 `b' Deactivate upper horizontal mark on left paper edge
277 `H' Activate all horizontal marks on left paper edge
278 `h' Deactivate all horizontal marks on left paper edge
280 `L' Activate left vertical mark on upper paper edge
281 `l' Deactivate left vertical mark on upper paper edge
283 `M' Activate middle horizontal mark on left paper edge
284 `m' Deactivate middle horizontal mark on left paper edge
286 `P' Activate punch or center mark on left paper edge
287 `p' Deactivate punch or center mark on left paper edge
289 `T' Activate lower horizontal mark on left paper edge
290 `t' Deactivate lower horizontal mark on left paper edge
292 `V' Activate all vertical marks on upper paper edge
293 `v' Deactivate all vertical marks on upper paper edge
295 This option can also be set with the OPTIONS keyword, e.g.:
296 \"foldmarks:(b l m t)\"."
297 :group 'org-export-koma-letter
298 :type '(choice
299 (const :tag "Activate default folding marks" t)
300 (const :tag "Deactivate folding marks" nil)
301 (set
302 :tag "Configure folding marks"
303 (const :tag "Activate upper horizontal mark on left paper edge" B)
304 (const :tag "Deactivate upper horizontal mark on left paper edge" b)
305 (const :tag "Activate all horizontal marks on left paper edge" H)
306 (const :tag "Deactivate all horizontal marks on left paper edge" h)
307 (const :tag "Activate left vertical mark on upper paper edge" L)
308 (const :tag "Deactivate left vertical mark on upper paper edge" l)
309 (const :tag "Activate middle horizontal mark on left paper edge" M)
310 (const :tag "Deactivate middle horizontal mark on left paper edge" m)
311 (const :tag "Activate punch or center mark on left paper edge" P)
312 (const :tag "Deactivate punch or center mark on left paper edge" p)
313 (const :tag "Activate lower horizontal mark on left paper edge" T)
314 (const :tag "Deactivate lower horizontal mark on left paper edge" t)
315 (const :tag "Activate all vertical marks on upper paper edge" V)
316 (const :tag "Deactivate all vertical marks on upper paper edge" v))))
318 (defcustom org-koma-letter-use-phone nil
319 "Non-nil prints sender's phone number.
320 This option can also be set with the OPTIONS keyword, e.g.:
321 \"phone:t\"."
322 :group 'org-export-koma-letter
323 :type 'boolean)
325 (defcustom org-koma-letter-use-email nil
326 "Non-nil prints sender's email address.
327 This option can also be set with the OPTIONS keyword, e.g.:
328 \"email:t\"."
329 :group 'org-export-koma-letter
330 :type 'boolean)
332 (defcustom org-koma-letter-use-place t
333 "Non-nil prints the letter's place next to the date.
334 This option can also be set with the OPTIONS keyword, e.g.:
335 \"place:nil\"."
336 :group 'org-export-koma-letter
337 :type 'boolean)
339 (defcustom org-koma-letter-default-class "default-koma-letter"
340 "Default class for `org-koma-letter'.
341 The value must be a member of `org-latex-classes'."
342 :group 'org-export-koma-letter
343 :type 'string)
345 (defcustom org-koma-letter-headline-is-opening-maybe t
346 "Non-nil means a headline may be used as an opening.
347 A headline is only used if #+OPENING is not set. See also
348 `org-koma-letter-opening'."
349 :group 'org-export-koma-letter
350 :type 'boolean)
352 (defconst org-koma-letter-special-tags-in-letter '(to from)
353 "Header tags related to the letter itself.")
355 (defconst org-koma-letter-special-tags-after-closing '(ps encl cc)
356 "Header tags to be inserted after closing.")
358 (defconst org-koma-letter-special-tags-after-letter '(after_letter)
359 "Header tags to be inserted after closing.")
361 (defvar org-koma-letter-special-contents nil
362 "Holds special content temporarily.")
366 ;;; Define Back-End
368 (org-export-define-derived-backend 'koma-letter 'latex
369 :options-alist
370 '((:latex-class "LATEX_CLASS" nil org-koma-letter-default-class t)
371 (:lco "LCO" nil org-koma-letter-class-option-file)
372 (:author "AUTHOR" nil (org-koma-letter--get-value org-koma-letter-author) t)
373 (:from-address "FROM_ADDRESS" nil nil newline)
374 (:phone-number "PHONE_NUMBER" nil org-koma-letter-phone-number)
375 (:email "EMAIL" nil (org-koma-letter--get-value org-koma-letter-email) t)
376 (:to-address "TO_ADDRESS" nil nil newline)
377 (:place "PLACE" nil org-koma-letter-place)
378 (:opening "OPENING" nil org-koma-letter-opening)
379 (:closing "CLOSING" nil org-koma-letter-closing)
380 (:signature "SIGNATURE" nil org-koma-letter-signature newline)
381 (:special-headings nil "special-headings"
382 org-koma-letter-prefer-special-headings)
383 (:special-tags nil nil (append
384 org-koma-letter-special-tags-in-letter
385 org-koma-letter-special-tags-after-closing
386 org-koma-letter-special-tags-after-letter))
387 (:with-after-closing nil "after-closing-order"
388 org-koma-letter-special-tags-after-closing)
389 (:with-after-letter nil "after-letter-order"
390 org-koma-letter-special-tags-after-letter)
391 (:with-backaddress nil "backaddress" org-koma-letter-use-backaddress)
392 (:with-email nil "email" org-koma-letter-use-email)
393 (:with-foldmarks nil "foldmarks" org-koma-letter-use-foldmarks)
394 (:with-phone nil "phone" org-koma-letter-use-phone)
395 (:with-place nil "place" org-koma-letter-use-place)
396 (:with-subject nil "subject" org-koma-letter-subject-format)
397 ;; Special properties non-nil when a setting happened in buffer.
398 ;; They are used to prioritize in-buffer settings over "lco"
399 ;; files. See `org-koma-letter-template'.
400 (:inbuffer-author "AUTHOR" nil 'koma-letter:empty)
401 (:inbuffer-email "EMAIL" nil 'koma-letter:empty)
402 (:inbuffer-phone-number "PHONE_NUMBER" nil 'koma-letter:empty)
403 (:inbuffer-place "PLACE" nil 'koma-letter:empty)
404 (:inbuffer-signature "SIGNATURE" nil 'koma-letter:empty)
405 (:inbuffer-with-backaddress nil "backaddress" 'koma-letter:empty)
406 (:inbuffer-with-email nil "email" 'koma-letter:empty)
407 (:inbuffer-with-foldmarks nil "foldmarks" 'koma-letter:empty)
408 (:inbuffer-with-phone nil "phone" 'koma-letter:empty))
409 :translate-alist '((export-block . org-koma-letter-export-block)
410 (export-snippet . org-koma-letter-export-snippet)
411 (headline . org-koma-letter-headline)
412 (keyword . org-koma-letter-keyword)
413 (template . org-koma-letter-template))
414 :menu-entry
415 '(?k "Export with KOMA Scrlttr2"
416 ((?L "As LaTeX buffer" org-koma-letter-export-as-latex)
417 (?l "As LaTeX file" org-koma-letter-export-to-latex)
418 (?p "As PDF file" org-koma-letter-export-to-pdf)
419 (?o "As PDF file and open"
420 (lambda (a s v b)
421 (if a (org-koma-letter-export-to-pdf t s v b)
422 (org-open-file (org-koma-letter-export-to-pdf nil s v b))))))))
426 ;;; Helper functions
428 (defun org-koma-letter-email ()
429 "Return the current `user-mail-address'."
430 user-mail-address)
432 ;; The following is taken from/inspired by ox-grof.el
433 ;; Thanks, Luis!
435 (defun org-koma-letter--get-tagged-contents (key)
436 "Get contents from a headline tagged with KEY.
437 The contents is stored in `org-koma-letter-special-contents'."
438 (cdr (assoc (org-koma-letter--get-value key)
439 org-koma-letter-special-contents)))
441 (defun org-koma-letter--get-value (value)
442 "Turn value into a string whenever possible.
443 Determines if VALUE is nil, a string, a function or a symbol and
444 return a string or nil."
445 (when value
446 (cond ((stringp value) value)
447 ((functionp value) (funcall value))
448 ((symbolp value) (symbol-name value))
449 (t value))))
451 (defun org-koma-letter--special-contents-as-macro
452 (keywords &optional keep-newlines no-tag)
453 "Process KEYWORDS members of `org-koma-letter-special-contents'.
454 KEYWORDS is a list of symbols. Return them as a string to be
455 formatted.
457 The function is used for inserting content of special headings
458 such as PS.
460 If KEEP-NEWLINES is t newlines will not be removed. If NO-TAG is
461 t the content in `org-koma-letter-special-contents' will not be
462 wrapped in a macro named whatever the members of KEYWORDS are
463 called."
464 (mapconcat
465 #'(lambda (keyword)
466 (let* ((name (org-koma-letter--get-value keyword))
467 (value (org-koma-letter--get-tagged-contents name)))
468 (when value
469 (if no-tag (if keep-newlines value (org-trim value))
470 (format "\\%s{%s}\n"
471 name
472 (if keep-newlines value (org-trim value)))))))
473 keywords
474 ""))
476 (defun org-koma-letter--determine-to-and-from (info key)
477 "Given INFO determine KEY for the letter.
478 KEY should be `to' or `from'.
480 `ox-koma-letter' allows two ways to specify TO and FROM. If both
481 are present return the preferred one as determined by
482 `org-koma-letter-prefer-special-headings'."
483 (let ((option (plist-get info (if (eq key 'to) :to-address :from-address)))
484 (headline (org-koma-letter--get-tagged-contents key)))
485 (replace-regexp-in-string
486 "\n" "\\\\\\\\\n"
487 (org-trim
488 (or (if (plist-get info :special-headings) (or headline option)
489 (or option headline))
490 ;; Fallback values.
491 (if (eq key 'to) "\\mbox{}" org-koma-letter-from-address))))))
495 ;;; Transcode Functions
497 ;;;; Export Block
499 (defun org-koma-letter-export-block (export-block contents info)
500 "Transcode an EXPORT-BLOCK element into KOMA Scrlttr2 code.
501 CONTENTS is nil. INFO is a plist used as a communication
502 channel."
503 (when (member (org-element-property :type export-block) '("KOMA-LETTER" "LATEX"))
504 (org-remove-indentation (org-element-property :value export-block))))
506 ;;;; Export Snippet
508 (defun org-koma-letter-export-snippet (export-snippet contents info)
509 "Transcode an EXPORT-SNIPPET object into KOMA Scrlttr2 code.
510 CONTENTS is nil. INFO is a plist used as a communication
511 channel."
512 (when (memq (org-export-snippet-backend export-snippet) '(latex koma-letter))
513 (org-element-property :value export-snippet)))
515 ;;;; Keyword
517 (defun org-koma-letter-keyword (keyword contents info)
518 "Transcode a KEYWORD element into KOMA Scrlttr2 code.
519 CONTENTS is nil. INFO is a plist used as a communication
520 channel."
521 (let ((key (org-element-property :key keyword))
522 (value (org-element-property :value keyword)))
523 ;; Handle specifically KOMA-LETTER keywords. Otherwise, fallback
524 ;; to `latex' back-end.
525 (if (equal key "KOMA-LETTER") value
526 (org-export-with-backend 'latex keyword contents info))))
528 ;; Headline
530 (defun org-koma-letter-headline (headline contents info)
531 "Transcode a HEADLINE element from Org to LaTeX.
532 CONTENTS holds the contents of the headline. INFO is a plist
533 holding contextual information.
535 Note that if a headline is tagged with a tag from
536 `org-koma-letter-special-tags' it will not be exported, but
537 stored in `org-koma-letter-special-contents' and included at the
538 appropriate place."
539 (unless (let ((tag (car (org-export-get-tags headline info))))
540 (and tag
541 (member-ignore-case
542 tag (mapcar #'symbol-name (plist-get info :special-tags)))
543 ;; Store association for later use and bail out.
544 (push (cons tag contents) org-koma-letter-special-contents)))
545 ;; Opening is not defined yet: use headline's title.
546 (when (and org-koma-letter-headline-is-opening-maybe
547 (not (org-string-nw-p (plist-get info :opening))))
548 (plist-put info :opening
549 (org-export-data (org-element-property :title headline) info)))
550 ;; In any case, insert contents in letter's body.
551 contents))
553 ;;;; Template
555 (defun org-koma-letter-template (contents info)
556 "Return complete document string after KOMA Scrlttr2 conversion.
557 CONTENTS is the transcoded contents string. INFO is a plist
558 holding export options."
559 (concat
560 ;; Time-stamp.
561 (and (plist-get info :time-stamp-file)
562 (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
563 ;; Document class and packages.
564 (let* ((class (plist-get info :latex-class))
565 (class-options (plist-get info :latex-class-options))
566 (header (nth 1 (assoc class org-latex-classes)))
567 (document-class-string
568 (and (stringp header)
569 (if (not class-options) header
570 (replace-regexp-in-string
571 "^[ \t]*\\\\documentclass\\(\\(\\[[^]]*\\]\\)?\\)"
572 class-options header t nil 1)))))
573 (if (not document-class-string)
574 (user-error "Unknown LaTeX class `%s'" class)
575 (org-latex-guess-babel-language
576 (org-latex-guess-inputenc
577 (org-element-normalize-string
578 (org-splice-latex-header
579 document-class-string
580 org-latex-default-packages-alist ; Defined in org.el.
581 org-latex-packages-alist nil ; Defined in org.el.
582 (concat (org-element-normalize-string (plist-get info :latex-header))
583 (plist-get info :latex-header-extra)))))
584 info)))
585 ;; Settings. They can come from three locations, in increasing
586 ;; order of precedence: global variables, LCO files and in-buffer
587 ;; settings. Thus, we first insert settings coming from global
588 ;; variables, then we insert LCO files, and, eventually, we insert
589 ;; settings coming from buffer keywords.
590 (org-koma-letter--build-settings 'global info)
591 (mapconcat #'(lambda (file) (format "\\LoadLetterOption{%s}\n" file))
592 (org-split-string (or (plist-get info :lco) "") " ")
594 (org-koma-letter--build-settings 'buffer info)
595 ;; From address.
596 (let ((from-address (org-koma-letter--determine-to-and-from info 'from)))
597 (and from-address (format "\\setkomavar{fromaddress}{%s}\n" from-address)))
598 ;; Date.
599 (format "\\date{%s}\n" (org-export-data (org-export-get-date info) info))
600 ;; Document start
601 "\\begin{document}\n\n"
602 ;; Subject
603 (let ((with-subject (plist-get info :with-subject)))
604 (when with-subject
605 (concat
606 (unless (eq with-subject t)
607 (format "\\KOMAoption{subject}{%s}\n"
608 (if (symbolp with-subject) with-subject
609 (mapconcat #'symbol-name with-subject ","))))
610 (let ((subject (org-export-data (plist-get info :title) info)))
611 (and (org-string-nw-p subject)
612 (format "\\setkomavar{subject}{%s}\n\n" subject))))))
613 ;; Letter start.
614 (format "\\begin{letter}{%%\n%s}\n\n"
615 (org-koma-letter--determine-to-and-from info 'to))
616 ;; Opening.
617 (format "\\opening{%s}\n\n" (plist-get info :opening))
618 ;; Letter body.
619 contents
620 ;; Closing.
621 (format "\n\\closing{%s}\n" (plist-get info :closing))
622 (org-koma-letter--special-contents-as-macro
623 (plist-get info :with-after-closing))
624 ;; Letter end.
625 "\n\\end{letter}\n"
626 (org-koma-letter--special-contents-as-macro
627 (plist-get info :with-after-letter) t t)
628 ;; Document end.
629 "\n\\end{document}"))
631 (defun org-koma-letter--build-settings (scope info)
632 "Build settings string according to type.
633 SCOPE is either `global' or `buffer'. INFO is a plist used as
634 a communication channel."
635 (let ((check-scope
636 (function
637 ;; Non-nil value when SETTING was defined in SCOPE.
638 (lambda (setting)
639 (let ((property (intern (format ":inbuffer-%s" setting))))
640 (if (eq scope 'global)
641 (eq (plist-get info property) 'koma-letter:empty)
642 (not (eq (plist-get info property) 'koma-letter:empty))))))))
643 (concat
644 ;; Name.
645 (let ((author (plist-get info :author)))
646 (and author
647 (funcall check-scope 'author)
648 (format "\\setkomavar{fromname}{%s}\n"
649 (org-export-data author info))))
650 ;; Email.
651 (let ((email (plist-get info :email)))
652 (and email
653 (funcall check-scope 'email)
654 (format "\\setkomavar{fromemail}{%s}\n" email)))
655 (and (funcall check-scope 'with-email)
656 (format "\\KOMAoption{fromemail}{%s}\n"
657 (if (plist-get info :with-email) "true" "false")))
658 ;; Phone number.
659 (let ((phone-number (plist-get info :phone-number)))
660 (and (org-string-nw-p phone-number)
661 (funcall check-scope 'phone-number)
662 (format "\\setkomavar{fromphone}{%s}\n" phone-number)))
663 (and (funcall check-scope 'with-phone)
664 (format "\\KOMAoption{fromphone}{%s}\n"
665 (if (plist-get info :with-phone) "true" "false")))
666 ;; Signature.
667 (let ((signature (plist-get info :signature)))
668 (and (org-string-nw-p signature)
669 (funcall check-scope 'signature)
670 (format "\\setkomavar{signature}{%s}\n" signature)))
671 ;; Back address.
672 (and (funcall check-scope 'with-backaddress)
673 (format "\\KOMAoption{backaddress}{%s}\n"
674 (if (plist-get info :with-backaddress) "true" "false")))
675 ;; Place.
676 (and (funcall check-scope 'place)
677 (format "\\setkomavar{place}{%s}\n"
678 (if (plist-get info :with-place) (plist-get info :place) "")))
679 ;; Folding marks.
680 (and (funcall check-scope 'with-foldmarks)
681 (let ((foldmarks (plist-get info :with-foldmarks)))
682 (cond ((consp foldmarks)
683 (format "\\KOMAoptions{foldmarks=true,foldmarks=%s}\n"
684 (mapconcat #'symbol-name foldmarks "")))
685 (foldmarks "\\KOMAoptions{foldmarks=true}\n")
686 (t "\\KOMAoptions{foldmarks=false}\n")))))))
690 ;;; Commands
692 ;;;###autoload
693 (defun org-koma-letter-export-as-latex
694 (&optional async subtreep visible-only body-only ext-plist)
695 "Export current buffer as a KOMA Scrlttr2 letter.
697 If narrowing is active in the current buffer, only export its
698 narrowed part.
700 If a region is active, export that region.
702 A non-nil optional argument ASYNC means the process should happen
703 asynchronously. The resulting buffer should be accessible
704 through the `org-export-stack' interface.
706 When optional argument SUBTREEP is non-nil, export the sub-tree
707 at point, extracting information from the headline properties
708 first.
710 When optional argument VISIBLE-ONLY is non-nil, don't export
711 contents of hidden elements.
713 When optional argument BODY-ONLY is non-nil, only write code
714 between \"\\begin{letter}\" and \"\\end{letter}\".
716 EXT-PLIST, when provided, is a proeprty list with external
717 parameters overriding Org default settings, but still inferior to
718 file-local settings.
720 Export is done in a buffer named \"*Org KOMA-LETTER Export*\". It
721 will be displayed if `org-export-show-temporary-export-buffer' is
722 non-nil."
723 (interactive)
724 (let (org-koma-letter-special-contents)
725 (org-export-to-buffer 'koma-letter "*Org KOMA-LETTER Export*"
726 async subtreep visible-only body-only ext-plist
727 (lambda () (LaTeX-mode)))))
729 ;;;###autoload
730 (defun org-koma-letter-export-to-latex
731 (&optional async subtreep visible-only body-only ext-plist)
732 "Export current buffer as a KOMA Scrlttr2 letter (tex).
734 If narrowing is active in the current buffer, only export its
735 narrowed part.
737 If a region is active, export that region.
739 A non-nil optional argument ASYNC means the process should happen
740 asynchronously. The resulting file should be accessible through
741 the `org-export-stack' interface.
743 When optional argument SUBTREEP is non-nil, export the sub-tree
744 at point, extracting information from the headline properties
745 first.
747 When optional argument VISIBLE-ONLY is non-nil, don't export
748 contents of hidden elements.
750 When optional argument BODY-ONLY is non-nil, only write code
751 between \"\\begin{letter}\" and \"\\end{letter}\".
753 EXT-PLIST, when provided, is a property list with external
754 parameters overriding Org default settings, but still inferior to
755 file-local settings.
757 When optional argument PUB-DIR is set, use it as the publishing
758 directory.
760 Return output file's name."
761 (interactive)
762 (let ((outfile (org-export-output-file-name ".tex" subtreep))
763 (org-koma-letter-special-contents))
764 (org-export-to-file 'koma-letter outfile
765 async subtreep visible-only body-only ext-plist)))
767 ;;;###autoload
768 (defun org-koma-letter-export-to-pdf
769 (&optional async subtreep visible-only body-only ext-plist)
770 "Export current buffer as a KOMA Scrlttr2 letter (pdf).
772 If narrowing is active in the current buffer, only export its
773 narrowed part.
775 If a region is active, export that region.
777 A non-nil optional argument ASYNC means the process should happen
778 asynchronously. The resulting file should be accessible through
779 the `org-export-stack' interface.
781 When optional argument SUBTREEP is non-nil, export the sub-tree
782 at point, extracting information from the headline properties
783 first.
785 When optional argument VISIBLE-ONLY is non-nil, don't export
786 contents of hidden elements.
788 When optional argument BODY-ONLY is non-nil, only write code
789 between \"\\begin{letter}\" and \"\\end{letter}\".
791 EXT-PLIST, when provided, is a property list with external
792 parameters overriding Org default settings, but still inferior to
793 file-local settings.
795 Return PDF file's name."
796 (interactive)
797 (let ((file (org-export-output-file-name ".tex" subtreep))
798 (org-koma-letter-special-contents))
799 (org-export-to-file 'koma-letter file
800 async subtreep visible-only body-only ext-plist
801 (lambda (file) (org-latex-compile file)))))
804 (provide 'ox-koma-letter)
805 ;;; ox-koma-letter.el ends here