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