Merge branch 'maint'
[org-mode.git] / contrib / lisp / ox-koma-letter.el
blob37da54cbbc0392b3d34d412473ca79cd0b9dac54
1 ;;; ox-koma-letter.el --- KOMA Scrlttr2 Back-End for Org Export Engine
3 ;; Copyright (C) 2007-2017 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 ;; - LOCATION: see `org-koma-letter-location',
45 ;; - TO_ADDRESS: If unspecified this is set to "\mbox{}".
47 ;; TO_ADDRESS, FROM_ADDRESS, LOCATION, CLOSING, and SIGNATURE can also
48 ;; be specified using "special headings" with the special tags
49 ;; specified in `org-koma-letter-special-tags-in-letter'. LaTeX line
50 ;; breaks are not necessary for TO_ADDRESS, FROM_ADDRESS and LOCATION.
51 ;; If both a headline and a keyword specify a to or from address the
52 ;; value is determined in accordance with
53 ;; `org-koma-letter-prefer-special-headings'.
55 ;; A number of OPTIONS settings can be set to change which contents is
56 ;; exported.
57 ;; - backaddress (see `org-koma-letter-use-backaddress')
58 ;; - foldmarks (see `org-koma-letter-use-foldmarks')
59 ;; - phone (see `org-koma-letter-use-phone')
60 ;; - email (see `org-koma-letter-use-email')
61 ;; - place (see `org-koma-letter-use-place')
62 ;; - location (see `org-koma-letter-use-location')
63 ;; - subject, a list of format options
64 ;; (see `org-koma-letter-subject-format')
65 ;; - after-closing-order, a list of the ordering of headings with
66 ;; special tags after closing (see
67 ;; `org-koma-letter-special-tags-after-closing')
68 ;; - after-letter-order, as above, but after the end of the letter
69 ;; (see `org-koma-letter-special-tags-after-letter').
71 ;; The following variables works differently from the main LaTeX class
72 ;; - AUTHOR: Default to user-full-name but may be disabled.
73 ;; (See also `org-koma-letter-author'),
74 ;; - EMAIL: Same as AUTHOR. (see also `org-koma-letter-email'),
76 ;; Headlines are in general ignored. However, headlines with special
77 ;; tags can be used for specified contents like postscript (ps),
78 ;; carbon copy (cc), enclosures (encl) and code to be inserted after
79 ;; \end{letter} (after_letter). Specials tags are defined in
80 ;; `org-koma-letter-special-tags-after-closing' and
81 ;; `org-koma-letter-special-tags-after-letter'. Currently members of
82 ;; `org-koma-letter-special-tags-after-closing' used as macros and the
83 ;; content of the headline is the argument.
85 ;; Headlines with to and from may also be used rather than the keyword
86 ;; approach described above. If both a keyword and a headline with
87 ;; information is present precedence is determined by
88 ;; `org-koma-letter-prefer-special-headings'.
90 ;; You need an appropriate association in `org-latex-classes' in order
91 ;; to use the KOMA Scrlttr2 class. By default, a sparse scrlttr2
92 ;; class is provided: "default-koma-letter". You can also add you own
93 ;; letter class. For instance:
95 ;; (add-to-list 'org-latex-classes
96 ;; '("my-letter"
97 ;; "\\documentclass\[%
98 ;; DIV=14,
99 ;; fontsize=12pt,
100 ;; parskip=half,
101 ;; subject=titled,
102 ;; backaddress=false,
103 ;; fromalign=left,
104 ;; fromemail=true,
105 ;; fromphone=true\]\{scrlttr2\}
106 ;; \[DEFAULT-PACKAGES]
107 ;; \[PACKAGES]
108 ;; \[EXTRA]"))
110 ;; Then, in your Org document, be sure to require the proper class
111 ;; with:
113 ;; #+LATEX_CLASS: my-letter
115 ;; Or by setting `org-koma-letter-default-class'.
117 ;; You may have to load (LaTeX) Babel as well, e.g., by adding
118 ;; it to `org-latex-packages-alist',
120 ;; (add-to-list 'org-latex-packages-alist '("AUTO" "babel" nil))
122 ;;; Code:
124 (require 'ox-latex)
126 ;; Install a default letter class.
127 (unless (assoc "default-koma-letter" org-latex-classes)
128 (add-to-list 'org-latex-classes
129 '("default-koma-letter" "\\documentclass[11pt]{scrlttr2}")))
132 ;;; User-Configurable Variables
134 (defgroup org-export-koma-letter nil
135 "Options for exporting to KOMA scrlttr2 class in LaTeX export."
136 :tag "Org Koma-Letter"
137 :group 'org-export)
139 (defcustom org-koma-letter-class-option-file "NF"
140 "Letter Class Option File.
141 This option can also be set with the LCO keyword."
142 :group 'org-export-koma-letter
143 :type 'string)
145 (defcustom org-koma-letter-author 'user-full-name
146 "Sender's name.
148 This variable defaults to calling the function `user-full-name'
149 which just returns the current function `user-full-name'.
150 Alternatively a string, nil or a function may be given.
151 Functions must return a string.
153 This option can also be set with the AUTHOR keyword."
154 :group 'org-export-koma-letter
155 :type '(radio (function-item user-full-name)
156 (string)
157 (function)
158 (const :tag "Do not export author" nil)))
160 (defcustom org-koma-letter-email 'org-koma-letter-email
161 "Sender's email address.
163 This variable defaults to the value `org-koma-letter-email' which
164 returns `user-mail-address'. Alternatively a string, nil or
165 a function may be given. Functions must return a string.
167 This option can also be set with the EMAIL keyword."
168 :group 'org-export-koma-letter
169 :type '(radio (function-item org-koma-letter-email)
170 (string)
171 (function)
172 (const :tag "Do not export email" nil)))
174 (defcustom org-koma-letter-from-address ""
175 "Sender's address, as a string.
176 This option can also be set with one or more FROM_ADDRESS
177 keywords."
178 :group 'org-export-koma-letter
179 :type 'string)
181 (defcustom org-koma-letter-phone-number ""
182 "Sender's phone number, as a string.
183 This option can also be set with the PHONE_NUMBER keyword."
184 :group 'org-export-koma-letter
185 :type 'string)
187 (defcustom org-koma-letter-place ""
188 "Place from which the letter is sent, as a string.
189 This option can also be set with the PLACE keyword."
190 :group 'org-export-koma-letter
191 :type 'string)
193 (defcustom org-koma-letter-location ""
194 "Sender's extension field, as a string.
196 This option can also be set with the LOCATION keyword.
197 Moreover, when:
198 (1) Either `org-koma-letter-prefer-special-headings' is non-nil
199 or there is no LOCATION keyword or the LOCATION keyword is
200 empty;
201 (2) the letter contains a headline with the special
202 tag \"location\";
203 then the location will be set as the content of the location
204 special heading.
206 The location field is typically printed right of the address
207 field (See Figure 4.9. in the English manual of 2015-10-03)."
208 :group 'org-export-koma-letter
209 :type 'string)
211 (defcustom org-koma-letter-opening ""
212 "Letter's opening, as a string.
214 This option can also be set with the OPENING keyword. Moreover,
215 when:
216 (1) Either `org-koma-letter-prefer-special-headings' is non-nil
217 or the CLOSING keyword is empty
218 (2) `org-koma-letter-headline-is-opening-maybe' is non-nil;
219 (3) the letter contains a headline without a special
220 tag (e.g. \"to\" or \"ps\");
221 then the opening will be implicitly set as the untagged headline title."
222 :group 'org-export-koma-letter
223 :type 'string)
225 (defcustom org-koma-letter-closing ""
226 "Letter's closing, as a string.
227 This option can also be set with the CLOSING keyword. Moreover,
228 when:
229 (1) Either `org-koma-letter-prefer-special-headings' is non-nil
230 or the CLOSING keyword is empty;
231 (2) `org-koma-letter-headline-is-opening-maybe' is non-nil;
232 (3) the letter contains a headline with the special
233 tag \"closing\";
234 then the opening will be set as the title of the closing special
235 heading title."
236 :group 'org-export-koma-letter
237 :type 'string)
239 (defcustom org-koma-letter-signature ""
240 "Signature, as a string.
241 This option can also be set with the SIGNATURE keyword.
242 Moreover, when:
243 (1) Either `org-koma-letter-prefer-special-headings' is non-nil
244 or there is no CLOSING keyword or the CLOSING keyword is empty;
245 (2) `org-koma-letter-headline-is-opening-maybe' is non-nil;
246 (3) the letter contains a headline with the special
247 tag \"closing\";
248 then the signature will be set as the content of the
249 closing special heading.
251 Note if the content is empty the signature will not be set."
252 :group 'org-export-koma-letter
253 :type 'string)
255 (defcustom org-koma-letter-prefer-special-headings nil
256 "Non-nil means prefer headlines over keywords for TO and FROM.
257 This option can also be set with the OPTIONS keyword, e.g.:
258 \"special-headings:t\"."
259 :group 'org-export-koma-letter
260 :type 'boolean)
262 (defcustom org-koma-letter-subject-format t
263 "Non-nil means include the subject.
265 Support formatting options.
267 When t, insert a subject using default options. When nil, do not
268 insert a subject at all. It can also be a list of symbols among
269 the following ones:
271 `afteropening' Subject after opening
272 `beforeopening' Subject before opening
273 `centered' Subject centered
274 `left' Subject left-justified
275 `right' Subject right-justified
276 `titled' Add title/description to subject
277 `underlined' Set subject underlined
278 `untitled' Do not add title/description to subject
280 Please refer to the KOMA-script manual (Table 4.16. in the
281 English manual of 2012-07-22).
283 This option can also be set with the OPTIONS keyword, e.g.:
284 \"subject:(underlined centered)\"."
285 :type
286 '(choice
287 (const :tag "No export" nil)
288 (const :tag "Default options" t)
289 (set :tag "Configure options"
290 (const :tag "Subject after opening" afteropening)
291 (const :tag "Subject before opening" beforeopening)
292 (const :tag "Subject centered" centered)
293 (const :tag "Subject left-justified" left)
294 (const :tag "Subject right-justified" right)
295 (const :tag "Add title or description to subject" underlined)
296 (const :tag "Set subject underlined" titled)
297 (const :tag "Do not add title or description to subject" untitled)))
298 :group 'org-export-koma-letter)
300 (defcustom org-koma-letter-use-backaddress nil
301 "Non-nil prints return address in line above to address.
302 This option can also be set with the OPTIONS keyword, e.g.:
303 \"backaddress:t\"."
304 :group 'org-export-koma-letter
305 :type 'boolean)
307 (defcustom org-koma-letter-use-foldmarks t
308 "Configure appearance of folding marks.
310 When t, activate default folding marks. When nil, do not insert
311 folding marks at all. It can also be a list of symbols among the
312 following ones:
314 `B' Activate upper horizontal mark on left paper edge
315 `b' Deactivate upper horizontal mark on left paper edge
317 `H' Activate all horizontal marks on left paper edge
318 `h' Deactivate all horizontal marks on left paper edge
320 `L' Activate left vertical mark on upper paper edge
321 `l' Deactivate left vertical mark on upper paper edge
323 `M' Activate middle horizontal mark on left paper edge
324 `m' Deactivate middle horizontal mark on left paper edge
326 `P' Activate punch or center mark on left paper edge
327 `p' Deactivate punch or center mark on left paper edge
329 `T' Activate lower horizontal mark on left paper edge
330 `t' Deactivate lower horizontal mark on left paper edge
332 `V' Activate all vertical marks on upper paper edge
333 `v' Deactivate all vertical marks on upper paper edge
335 This option can also be set with the OPTIONS keyword, e.g.:
336 \"foldmarks:(b l m t)\"."
337 :group 'org-export-koma-letter
338 :type '(choice
339 (const :tag "Activate default folding marks" t)
340 (const :tag "Deactivate folding marks" nil)
341 (set
342 :tag "Configure folding marks"
343 (const :tag "Activate upper horizontal mark on left paper edge" B)
344 (const :tag "Deactivate upper horizontal mark on left paper edge" b)
345 (const :tag "Activate all horizontal marks on left paper edge" H)
346 (const :tag "Deactivate all horizontal marks on left paper edge" h)
347 (const :tag "Activate left vertical mark on upper paper edge" L)
348 (const :tag "Deactivate left vertical mark on upper paper edge" l)
349 (const :tag "Activate middle horizontal mark on left paper edge" M)
350 (const :tag "Deactivate middle horizontal mark on left paper edge" m)
351 (const :tag "Activate punch or center mark on left paper edge" P)
352 (const :tag "Deactivate punch or center mark on left paper edge" p)
353 (const :tag "Activate lower horizontal mark on left paper edge" T)
354 (const :tag "Deactivate lower horizontal mark on left paper edge" t)
355 (const :tag "Activate all vertical marks on upper paper edge" V)
356 (const :tag "Deactivate all vertical marks on upper paper edge" v))))
358 (defcustom org-koma-letter-use-phone nil
359 "Non-nil prints sender's phone number.
360 This option can also be set with the OPTIONS keyword, e.g.:
361 \"phone:t\"."
362 :group 'org-export-koma-letter
363 :type 'boolean)
365 (defcustom org-koma-letter-use-email nil
366 "Non-nil prints sender's email address.
367 This option can also be set with the OPTIONS keyword, e.g.:
368 \"email:t\"."
369 :group 'org-export-koma-letter
370 :type 'boolean)
372 (defcustom org-koma-letter-use-place t
373 "Non-nil prints the letter's place next to the date.
374 This option can also be set with the OPTIONS keyword, e.g.:
375 \"place:nil\"."
376 :group 'org-export-koma-letter
377 :type 'boolean)
379 (defcustom org-koma-letter-default-class "default-koma-letter"
380 "Default class for `org-koma-letter'.
381 The value must be a member of `org-latex-classes'."
382 :group 'org-export-koma-letter
383 :type 'string)
385 (defcustom org-koma-letter-headline-is-opening-maybe t
386 "Non-nil means a headline may be used as an opening and closing.
387 See also `org-koma-letter-opening' and
388 `org-koma-letter-closing'."
389 :group 'org-export-koma-letter
390 :type 'boolean)
392 (defcustom org-koma-letter-prefer-subject nil
393 "Non-nil means title should be interpreted as subject if subject is missing.
394 This option can also be set with the OPTIONS keyword,
395 e.g. \"title-subject:t\"."
396 :group 'org-export-koma-letter
397 :type 'boolean)
399 (defconst org-koma-letter-special-tags-in-letter '(to from closing location)
400 "Header tags related to the letter itself.")
402 (defconst org-koma-letter-special-tags-after-closing '(after_closing ps encl cc)
403 "Header tags to be inserted in the letter after closing.")
405 (defconst org-koma-letter-special-tags-as-macro '(ps encl cc)
406 "Header tags to be inserted as macros")
408 (defconst org-koma-letter-special-tags-after-letter '(after_letter)
409 "Header tags to be inserted after the letter.")
411 (defvar org-koma-letter-special-contents nil
412 "Holds special content temporarily.")
414 (make-obsolete-variable 'org-koma-letter-use-title
415 'org-export-with-title
416 "25.1" 'set)
419 ;;; Define Back-End
421 (org-export-define-derived-backend 'koma-letter 'latex
422 :options-alist
423 '((:latex-class "LATEX_CLASS" nil org-koma-letter-default-class t)
424 (:lco "LCO" nil org-koma-letter-class-option-file)
425 (:author "AUTHOR" nil (org-koma-letter--get-value org-koma-letter-author) parse)
426 (:author-changed-in-buffer-p "AUTHOR" nil nil t)
427 (:from-address "FROM_ADDRESS" nil org-koma-letter-from-address newline)
428 (:phone-number "PHONE_NUMBER" nil org-koma-letter-phone-number)
429 (:email "EMAIL" nil (org-koma-letter--get-value org-koma-letter-email) t)
430 (:to-address "TO_ADDRESS" nil nil newline)
431 (:place "PLACE" nil org-koma-letter-place)
432 (:location "LOCATION" nil org-koma-letter-location)
433 (:subject "SUBJECT" nil nil parse)
434 (:opening "OPENING" nil org-koma-letter-opening parse)
435 (:closing "CLOSING" nil org-koma-letter-closing parse)
436 (:signature "SIGNATURE" nil org-koma-letter-signature newline)
437 (:special-headings nil "special-headings" org-koma-letter-prefer-special-headings)
438 (:special-tags-as-macro nil nil org-koma-letter-special-tags-as-macro)
439 (:special-tags-in-letter nil nil org-koma-letter-special-tags-in-letter)
440 (:special-tags-after-closing nil "after-closing-order"
441 org-koma-letter-special-tags-after-closing)
442 (:special-tags-after-letter nil "after-letter-order"
443 org-koma-letter-special-tags-after-letter)
444 (:with-backaddress nil "backaddress" org-koma-letter-use-backaddress)
445 (:with-email nil "email" org-koma-letter-use-email)
446 (:with-foldmarks nil "foldmarks" org-koma-letter-use-foldmarks)
447 (:with-phone nil "phone" org-koma-letter-use-phone)
448 (:with-place nil "place" org-koma-letter-use-place)
449 (:with-subject nil "subject" org-koma-letter-subject-format)
450 (:with-title-as-subject nil "title-subject" org-koma-letter-prefer-subject)
451 (:with-headline-opening nil nil org-koma-letter-headline-is-opening-maybe)
452 ;; Special properties non-nil when a setting happened in buffer.
453 ;; They are used to prioritize in-buffer settings over "lco"
454 ;; files. See `org-koma-letter-template'.
455 (:inbuffer-author "AUTHOR" nil 'koma-letter:empty)
456 (:inbuffer-from "FROM" nil 'koma-letter:empty)
457 (:inbuffer-email "EMAIL" nil 'koma-letter:empty)
458 (:inbuffer-phone-number "PHONE_NUMBER" nil 'koma-letter:empty)
459 (:inbuffer-place "PLACE" nil 'koma-letter:empty)
460 (:inbuffer-location "LOCATION" nil 'koma-letter:empty)
461 (:inbuffer-signature "SIGNATURE" nil 'koma-letter:empty)
462 (:inbuffer-with-backaddress nil "backaddress" 'koma-letter:empty)
463 (:inbuffer-with-email nil "email" 'koma-letter:empty)
464 (:inbuffer-with-foldmarks nil "foldmarks" 'koma-letter:empty)
465 (:inbuffer-with-phone nil "phone" 'koma-letter:empty)
466 (:inbuffer-with-place nil "place" 'koma-letter:empty))
467 :translate-alist '((export-block . org-koma-letter-export-block)
468 (export-snippet . org-koma-letter-export-snippet)
469 (headline . org-koma-letter-headline)
470 (keyword . org-koma-letter-keyword)
471 (template . org-koma-letter-template))
472 :menu-entry
473 '(?k "Export with KOMA Scrlttr2"
474 ((?L "As LaTeX buffer" org-koma-letter-export-as-latex)
475 (?l "As LaTeX file" org-koma-letter-export-to-latex)
476 (?p "As PDF file" org-koma-letter-export-to-pdf)
477 (?o "As PDF file and open"
478 (lambda (a s v b)
479 (if a (org-koma-letter-export-to-pdf t s v b)
480 (org-open-file (org-koma-letter-export-to-pdf nil s v b))))))))
484 ;;; Helper functions
486 (defun org-koma-letter-email ()
487 "Return the current `user-mail-address'."
488 user-mail-address)
490 ;; The following is taken from/inspired by ox-grof.el
491 ;; Thanks, Luis!
493 (defun org-koma-letter--get-tagged-contents (key)
494 "Get contents from a headline tagged with KEY.
495 The contents is stored in `org-koma-letter-special-contents'."
496 (let ((value (cdr (assoc-string (org-koma-letter--get-value key)
497 org-koma-letter-special-contents))))
498 (when value (org-string-nw-p (org-trim value)))))
500 (defun org-koma-letter--get-value (value)
501 "Turn value into a string whenever possible.
502 Determines if VALUE is nil, a string, a function or a symbol and
503 return a string or nil."
504 (when value
505 (cond ((stringp value) value)
506 ((functionp value) (funcall value))
507 ((symbolp value) (symbol-name value))
508 (t value))))
510 (defun org-koma-letter--special-contents-inline (keywords info)
511 "Process KEYWORDS members of `org-koma-letter-special-contents'.
512 KEYWORDS is a list of symbols. Return them as a string to be
513 formatted.
515 The function is used for inserting content of special headings
516 such as the one tagged with PS.
518 (mapconcat
519 (lambda (keyword)
520 (let* ((name (org-koma-letter--get-value keyword))
521 (value (org-koma-letter--get-tagged-contents name))
522 (macrop (memq keyword (plist-get info :special-tags-as-macro))))
523 (cond ((not value) nil)
524 (macrop (format "\\%s{%s}\n" name value))
525 (t value))))
526 keywords
527 "\n"))
530 (defun org-koma-letter--add-latex-newlines (string)
531 "Replace regular newlines with LaTeX newlines (i.e. `\\\\')"
532 (let ((str (org-trim string)))
533 (when (org-string-nw-p str)
534 (replace-regexp-in-string "\n" "\\\\\\\\\n" str))))
538 ;;; Transcode Functions
540 ;;;; Export Block
542 (defun org-koma-letter-export-block (export-block contents info)
543 "Transcode an EXPORT-BLOCK element into KOMA Scrlttr2 code.
544 CONTENTS is nil. INFO is a plist used as a communication
545 channel."
546 (when (member (org-element-property :type export-block) '("KOMA-LETTER" "LATEX"))
547 (org-remove-indentation (org-element-property :value export-block))))
549 ;;;; Export Snippet
551 (defun org-koma-letter-export-snippet (export-snippet contents info)
552 "Transcode an EXPORT-SNIPPET object into KOMA Scrlttr2 code.
553 CONTENTS is nil. INFO is a plist used as a communication
554 channel."
555 (when (memq (org-export-snippet-backend export-snippet) '(latex koma-letter))
556 (org-element-property :value export-snippet)))
558 ;;;; Keyword
560 (defun org-koma-letter-keyword (keyword contents info)
561 "Transcode a KEYWORD element into KOMA Scrlttr2 code.
562 CONTENTS is nil. INFO is a plist used as a communication
563 channel."
564 (let ((key (org-element-property :key keyword))
565 (value (org-element-property :value keyword)))
566 ;; Handle specifically KOMA-LETTER keywords. Otherwise, fallback
567 ;; to `latex' back-end.
568 (if (equal key "KOMA-LETTER") value
569 (org-export-with-backend 'latex keyword contents info))))
571 ;; Headline
573 (defun org-koma-letter-headline (headline contents info)
574 "Transcode a HEADLINE element from Org to LaTeX.
575 CONTENTS holds the contents of the headline. INFO is a plist
576 holding contextual information.
578 Note that if a headline is tagged with a tag from
579 `org-koma-letter-special-tags' it will not be exported, but
580 stored in `org-koma-letter-special-contents' and included at the
581 appropriate place."
582 (let ((special-tag (org-koma-letter--special-tag headline info)))
583 (if (not special-tag)
584 contents
585 (push (cons special-tag contents) org-koma-letter-special-contents)
586 "")))
588 (defun org-koma-letter--special-tag (headline info)
589 "Non-nil if HEADLINE is a special headline.
590 INFO is a plist holding contextual information. Return first
591 special tag headline."
592 (let ((special-tags (append
593 (plist-get info :special-tags-in-letter)
594 (plist-get info :special-tags-after-closing)
595 (plist-get info :special-tags-after-letter))))
596 (catch 'exit
597 (dolist (tag (org-export-get-tags headline info))
598 (let ((tag (assoc-string tag special-tags)))
599 (when tag (throw 'exit tag)))))))
601 (defun org-koma-letter--keyword-or-headline (plist-key pred info)
602 "Return the correct version of opening or closing.
603 PLIST-KEY should be a key in info, typically :opening
604 or :closing. PRED is a predicate run on headline to determine
605 which title to use which takes two arguments, a headline element
606 and an info plist. INFO is a plist holding contextual
607 information. Return the preferred candidate for the exported of
608 PLIST-KEY."
609 (let* ((keyword-candidate (plist-get info plist-key))
610 (headline-candidate (when (and (plist-get info :with-headline-opening)
611 (or (plist-get info :special-headings)
612 (not keyword-candidate)))
613 (org-element-map (plist-get info :parse-tree)
614 'headline
615 (lambda (head)
616 (when (funcall pred head info)
617 (org-element-property :title head)))
618 info t))))
619 (org-export-data (or headline-candidate keyword-candidate "") info)))
621 ;;;; Template
623 (defun org-koma-letter-template (contents info)
624 "Return complete document string after KOMA Scrlttr2 conversion.
625 CONTENTS is the transcoded contents string. INFO is a plist
626 holding export options."
627 (concat
628 ;; Time-stamp.
629 (and (plist-get info :time-stamp-file)
630 (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
631 ;; LaTeX compiler
632 (org-latex--insert-compiler info)
633 ;; Document class and packages.
634 (org-latex-make-preamble info)
635 ;; Settings. They can come from three locations, in increasing
636 ;; order of precedence: global variables, LCO files and in-buffer
637 ;; settings. Thus, we first insert settings coming from global
638 ;; variables, then we insert LCO files, and, eventually, we insert
639 ;; settings coming from buffer keywords.
640 (org-koma-letter--build-settings 'global info)
641 (mapconcat #'(lambda (file) (format "\\LoadLetterOption{%s}\n" file))
642 (org-split-string (or (plist-get info :lco) "") " ")
644 (org-koma-letter--build-settings 'buffer info)
645 ;; Date.
646 (format "\\date{%s}\n" (org-export-data (org-export-get-date info) info))
647 ;; Hyperref, document start, and subject and title.
648 (let* ((with-subject (plist-get info :with-subject))
649 (with-title (plist-get info :with-title))
650 (title-as-subject (and with-subject
651 (plist-get info :with-title-as-subject)))
652 (subject* (org-string-nw-p
653 (org-export-data (plist-get info :subject) info)))
654 (title* (and with-title
655 (org-string-nw-p
656 (org-export-data (plist-get info :title) info))))
657 (subject (cond ((not with-subject) nil)
658 (title-as-subject (or subject* title*))
659 (t subject*)))
660 (title (cond ((not with-title) nil)
661 (title-as-subject (and subject* title*))
662 (t title*)))
663 (hyperref-template (plist-get info :latex-hyperref-template))
664 (spec (append (list (cons ?t (or title subject "")))
665 (org-latex--format-spec info))))
666 (concat
667 (when (and with-subject (not (eq with-subject t)))
668 (format "\\KOMAoption{subject}{%s}\n"
669 (if (symbolp with-subject) with-subject
670 (mapconcat #'symbol-name with-subject ","))))
671 ;; Hyperref.
672 (format-spec hyperref-template spec)
673 ;; Document start.
674 "\\begin{document}\n\n"
675 ;; Subject and title.
676 (when subject (format "\\setkomavar{subject}{%s}\n" subject))
677 (when title (format "\\setkomavar{title}{%s}\n" title))
678 (when (or (org-string-nw-p title) (org-string-nw-p subject)) "\n")))
679 ;; Letter start.
680 (let ((keyword-val (plist-get info :to-address))
681 (heading-val (org-koma-letter--get-tagged-contents 'to)))
682 (format "\\begin{letter}{%%\n%s}\n\n"
683 (org-koma-letter--add-latex-newlines
684 (or (if (plist-get info :special-headings)
685 (or heading-val keyword-val)
686 (or keyword-val heading-val))
687 "\\\\mbox{}"))))
688 ;; Opening.
689 (format "\\opening{%s}\n\n"
690 (org-koma-letter--keyword-or-headline
691 :opening (lambda (h i) (not (org-koma-letter--special-tag h i)))
692 info))
693 ;; Letter body.
694 contents
695 ;; Closing.
696 (format "\\closing{%s}\n"
697 (org-koma-letter--keyword-or-headline
698 :closing (lambda (h i) (eq (org-koma-letter--special-tag h i)
699 'closing))
700 info))
701 (org-koma-letter--special-contents-inline
702 (plist-get info :special-tags-after-closing) info)
703 ;; Letter end.
704 "\n\\end{letter}\n"
705 (org-koma-letter--special-contents-inline
706 (plist-get info :special-tags-after-letter) info)
707 ;; Document end.
708 "\n\\end{document}"))
710 (defun org-koma-letter--build-settings (scope info)
711 "Build settings string according to type.
712 SCOPE is either `global' or `buffer'. INFO is a plist used as
713 a communication channel."
714 (let* ((check-scope
715 (function
716 ;; Non-nil value when SETTING was defined in SCOPE.
717 (lambda (setting)
718 (let ((property (intern (format ":inbuffer-%s" setting))))
719 (if (eq scope 'global)
720 (eq (plist-get info property) 'koma-letter:empty)
721 (not (eq (plist-get info property) 'koma-letter:empty)))))))
722 (heading-or-key-value
723 (function
724 (lambda (heading key &optional scoped)
725 (let* ((heading-val
726 (org-koma-letter--get-tagged-contents heading))
727 (key-val (org-string-nw-p (plist-get info key)))
728 (scopedp (funcall check-scope (or scoped heading))))
729 (and (or (and key-val scopedp) heading-val)
730 (not (and (eq scope 'global) heading-val))
731 (if scopedp key-val heading-val)))))))
732 (concat
733 ;; Name.
734 (let ((author (plist-get info :author)))
735 (and author
736 (funcall check-scope 'author)
737 (format "\\setkomavar{fromname}{%s}\n"
738 (org-export-data author info))))
739 ;; From.
740 (let ((from (funcall heading-or-key-value 'from :from-address)))
741 (and from
742 (format "\\setkomavar{fromaddress}{%s}\n"
743 (org-koma-letter--add-latex-newlines from))))
744 ;; Email.
745 (let ((email (plist-get info :email)))
746 (and email
747 (funcall check-scope 'email)
748 (format "\\setkomavar{fromemail}{%s}\n" email)))
749 (and (funcall check-scope 'with-email)
750 (format "\\KOMAoption{fromemail}{%s}\n"
751 (if (plist-get info :with-email) "true" "false")))
752 ;; Phone number.
753 (let ((phone-number (plist-get info :phone-number)))
754 (and (org-string-nw-p phone-number)
755 (funcall check-scope 'phone-number)
756 (format "\\setkomavar{fromphone}{%s}\n" phone-number)))
757 (and (funcall check-scope 'with-phone)
758 (format "\\KOMAoption{fromphone}{%s}\n"
759 (if (plist-get info :with-phone) "true" "false")))
760 ;; Signature.
761 (let* ((heading-val
762 (and (plist-get info :with-headline-opening)
763 (org-string-nw-p
764 (org-trim
765 (org-export-data
766 (org-koma-letter--get-tagged-contents 'closing)
767 info)))))
768 (signature (org-string-nw-p (plist-get info :signature)))
769 (signature-scope (funcall check-scope 'signature)))
770 (and (or (and signature signature-scope)
771 heading-val)
772 (not (and (eq scope 'global) heading-val))
773 (format "\\setkomavar{signature}{%s}\n"
774 (if signature-scope signature heading-val))))
775 ;; Back address.
776 (and (funcall check-scope 'with-backaddress)
777 (format "\\KOMAoption{backaddress}{%s}\n"
778 (if (plist-get info :with-backaddress) "true" "false")))
779 ;; Place.
780 (let ((with-place-set (funcall check-scope 'with-place))
781 (place-set (funcall check-scope 'place)))
782 (and (or (and with-place-set place-set)
783 (and (eq scope 'buffer) (or with-place-set place-set)))
784 (format "\\setkomavar{place}{%s}\n"
785 (if (plist-get info :with-place) (plist-get info :place)
786 ""))))
787 ;; Location.
788 (let ((location (funcall heading-or-key-value 'location :location)))
789 (and location
790 (format "\\setkomavar{location}{%s}\n" location)))
791 ;; Folding marks.
792 (and (funcall check-scope 'with-foldmarks)
793 (let ((foldmarks (plist-get info :with-foldmarks)))
794 (cond ((consp foldmarks)
795 (format "\\KOMAoptions{foldmarks=true,foldmarks=%s}\n"
796 (mapconcat #'symbol-name foldmarks "")))
797 (foldmarks "\\KOMAoptions{foldmarks=true}\n")
798 (t "\\KOMAoptions{foldmarks=false}\n")))))))
802 ;;; Commands
804 ;;;###autoload
805 (defun org-koma-letter-export-as-latex
806 (&optional async subtreep visible-only body-only ext-plist)
807 "Export current buffer as a KOMA Scrlttr2 letter.
809 If narrowing is active in the current buffer, only export its
810 narrowed part.
812 If a region is active, export that region.
814 A non-nil optional argument ASYNC means the process should happen
815 asynchronously. The resulting buffer should be accessible
816 through the `org-export-stack' interface.
818 When optional argument SUBTREEP is non-nil, export the sub-tree
819 at point, extracting information from the headline properties
820 first.
822 When optional argument VISIBLE-ONLY is non-nil, don't export
823 contents of hidden elements.
825 When optional argument BODY-ONLY is non-nil, only write code
826 between \"\\begin{letter}\" and \"\\end{letter}\".
828 EXT-PLIST, when provided, is a proeprty list with external
829 parameters overriding Org default settings, but still inferior to
830 file-local settings.
832 Export is done in a buffer named \"*Org KOMA-LETTER Export*\". It
833 will be displayed if `org-export-show-temporary-export-buffer' is
834 non-nil."
835 (interactive)
836 (let (org-koma-letter-special-contents)
837 (org-export-to-buffer 'koma-letter "*Org KOMA-LETTER Export*"
838 async subtreep visible-only body-only ext-plist
839 (lambda () (LaTeX-mode)))))
841 ;;;###autoload
842 (defun org-koma-letter-export-to-latex
843 (&optional async subtreep visible-only body-only ext-plist)
844 "Export current buffer as a KOMA Scrlttr2 letter (tex).
846 If narrowing is active in the current buffer, only export its
847 narrowed part.
849 If a region is active, export that region.
851 A non-nil optional argument ASYNC means the process should happen
852 asynchronously. The resulting file should be accessible through
853 the `org-export-stack' interface.
855 When optional argument SUBTREEP is non-nil, export the sub-tree
856 at point, extracting information from the headline properties
857 first.
859 When optional argument VISIBLE-ONLY is non-nil, don't export
860 contents of hidden elements.
862 When optional argument BODY-ONLY is non-nil, only write code
863 between \"\\begin{letter}\" and \"\\end{letter}\".
865 EXT-PLIST, when provided, is a property list with external
866 parameters overriding Org default settings, but still inferior to
867 file-local settings.
869 When optional argument PUB-DIR is set, use it as the publishing
870 directory.
872 Return output file's name."
873 (interactive)
874 (let ((outfile (org-export-output-file-name ".tex" subtreep))
875 (org-koma-letter-special-contents))
876 (org-export-to-file 'koma-letter outfile
877 async subtreep visible-only body-only ext-plist)))
879 ;;;###autoload
880 (defun org-koma-letter-export-to-pdf
881 (&optional async subtreep visible-only body-only ext-plist)
882 "Export current buffer as a KOMA Scrlttr2 letter (pdf).
884 If narrowing is active in the current buffer, only export its
885 narrowed part.
887 If a region is active, export that region.
889 A non-nil optional argument ASYNC means the process should happen
890 asynchronously. The resulting file should be accessible through
891 the `org-export-stack' interface.
893 When optional argument SUBTREEP is non-nil, export the sub-tree
894 at point, extracting information from the headline properties
895 first.
897 When optional argument VISIBLE-ONLY is non-nil, don't export
898 contents of hidden elements.
900 When optional argument BODY-ONLY is non-nil, only write code
901 between \"\\begin{letter}\" and \"\\end{letter}\".
903 EXT-PLIST, when provided, is a property list with external
904 parameters overriding Org default settings, but still inferior to
905 file-local settings.
907 Return PDF file's name."
908 (interactive)
909 (let ((file (org-export-output-file-name ".tex" subtreep))
910 (org-koma-letter-special-contents))
911 (org-export-to-file 'koma-letter file
912 async subtreep visible-only body-only ext-plist
913 (lambda (file) (org-latex-compile file)))))
916 (provide 'ox-koma-letter)
917 ;;; ox-koma-letter.el ends here