1 ;;; ox-koma-letter.el --- KOMA Scrlttr2 Back-End for Org Export Engine
3 ;; Copyright (C) 2007-2018 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/>.
26 ;; This library implements a KOMA Scrlttr2 back-end, derived from the
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
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 ;; - FROM_LOGO: see `org-koma-letter-from-logo',
44 ;; - SIGNATURE: see `org-koma-letter-signature',
45 ;; - PLACE: see `org-koma-letter-place',
46 ;; - LOCATION: see `org-koma-letter-location',
47 ;; - TO_ADDRESS: If unspecified this is set to "\mbox{}".
49 ;; TO_ADDRESS, FROM_ADDRESS, LOCATION, CLOSING, and SIGNATURE can also
50 ;; be specified using "special headings" with the special tags
51 ;; specified in `org-koma-letter-special-tags-in-letter'. LaTeX line
52 ;; breaks are not necessary for TO_ADDRESS, FROM_ADDRESS and LOCATION.
53 ;; If both a headline and a keyword specify a to or from address the
54 ;; value is determined in accordance with
55 ;; `org-koma-letter-prefer-special-headings'.
57 ;; A number of OPTIONS settings can be set to change which contents is
59 ;; - backaddress (see `org-koma-letter-use-backaddress')
60 ;; - foldmarks (see `org-koma-letter-use-foldmarks')
61 ;; - phone (see `org-koma-letter-use-phone')
62 ;; - url (see `org-koma-letter-use-url')
63 ;; - from-logo (see `org-koma-letter-use-from-logo')
64 ;; - email (see `org-koma-letter-use-email')
65 ;; - place (see `org-koma-letter-use-place')
66 ;; - location (see `org-koma-letter-use-location')
67 ;; - subject, a list of format options
68 ;; (see `org-koma-letter-subject-format')
69 ;; - after-closing-order, a list of the ordering of headings with
70 ;; special tags after closing (see
71 ;; `org-koma-letter-special-tags-after-closing')
72 ;; - after-letter-order, as above, but after the end of the letter
73 ;; (see `org-koma-letter-special-tags-after-letter').
75 ;; The following variables works differently from the main LaTeX class
76 ;; - AUTHOR: Default to user-full-name but may be disabled.
77 ;; (See also `org-koma-letter-author'),
78 ;; - EMAIL: Same as AUTHOR. (see also `org-koma-letter-email'),
80 ;; FROM_LOGO uses LaTeX markup. FROM_LOGO provides the
81 ;; "includegraphics" command to tell LaTeX where to find the logo.
82 ;; This command needs to know the logo's directory and file name. The
83 ;; directory can either be relative or absolute, just as you would
84 ;; expect. LaTeX can use three file types for the logo: PDF, JPEG, or
85 ;; PNG. The logo can either include or exclude its extension, which
86 ;; might surprise you. When you exclude its extension, LaTeX will
87 ;; search the directory for the "best" quality graphics format. For
88 ;; example if it finds both logo.pdf and logo.png then it will
89 ;; identify the PDF as "better", and include "logo.pdf". This can be
90 ;; useful, for example, when you are mocking up a logo in the PNG
91 ;; raster format and then switch over to the higher quality PDF vector
92 ;; format. When you include the file extension then LaTeX will
93 ;; include it without searching for higher quality file types.
94 ;; Whatever file type you choose, it will probably require a few
95 ;; design iterations to get the best looking logo size for your
96 ;; letter. Finally, the directory and file name are specified
97 ;; *without* quotes. Here are some examples with commentary, in the
98 ;; location of your letter, with a logo named "logo", to get you
101 ;; Logo in the same directory: \includegraphics{logo}
102 ;; or a sub-directory: \includegraphics{logos/production/logo}
104 ;; Logos specified using absolute paths on Linux or Windows:
106 ;; \includegraphics{~/correspondence/logo}
107 ;; \includegraphics{~/correspondence/logos/production/logo}
108 ;; \includegraphics{c:/you/correspondence/logo}
109 ;; \includegraphics{c:/you/correspondence/logos/production/logo}
111 ;; Logos in the same directory where the "better" quality PDF will
112 ;; be chosen over the JPG:
114 ;; \includegraphics{logo.pdf}
115 ;; \includegraphics{logo.png}
117 ;; Headlines are in general ignored. However, headlines with special
118 ;; tags can be used for specified contents like postscript (ps),
119 ;; carbon copy (cc), enclosures (encl) and code to be inserted after
120 ;; \end{letter} (after_letter). Specials tags are defined in
121 ;; `org-koma-letter-special-tags-after-closing' and
122 ;; `org-koma-letter-special-tags-after-letter'. Currently members of
123 ;; `org-koma-letter-special-tags-after-closing' used as macros and the
124 ;; content of the headline is the argument.
126 ;; Headlines with to and from may also be used rather than the keyword
127 ;; approach described above. If both a keyword and a headline with
128 ;; information is present precedence is determined by
129 ;; `org-koma-letter-prefer-special-headings'.
131 ;; You need an appropriate association in `org-latex-classes' in order
132 ;; to use the KOMA Scrlttr2 class. By default, a sparse scrlttr2
133 ;; class is provided: "default-koma-letter". You can also add you own
134 ;; letter class. For instance:
136 ;; (add-to-list 'org-latex-classes
138 ;; "\\documentclass\[%
143 ;; backaddress=false,
146 ;; fromphone=true\]\{scrlttr2\}
147 ;; \[DEFAULT-PACKAGES]
151 ;; Then, in your Org document, be sure to require the proper class
154 ;; #+LATEX_CLASS: my-letter
156 ;; Or by setting `org-koma-letter-default-class'.
158 ;; You may have to load (LaTeX) Babel as well, e.g., by adding
159 ;; it to `org-latex-packages-alist',
161 ;; (add-to-list 'org-latex-packages-alist '("AUTO" "babel" nil))
168 ;; Install a default letter class.
169 (unless (assoc "default-koma-letter" org-latex-classes
)
170 (add-to-list 'org-latex-classes
171 '("default-koma-letter" "\\documentclass[11pt]{scrlttr2}")))
174 ;;; User-Configurable Variables
176 (defgroup org-export-koma-letter nil
177 "Options for exporting to KOMA scrlttr2 class in LaTeX export."
178 :tag
"Org Koma-Letter"
181 (defcustom org-koma-letter-class-option-file
"NF"
182 "Letter Class Option File.
183 This option can also be set with the LCO keyword."
184 :group
'org-export-koma-letter
187 (defcustom org-koma-letter-author
'user-full-name
190 This variable defaults to calling the function `user-full-name'
191 which just returns the current function `user-full-name'.
192 Alternatively a string, nil or a function may be given.
193 Functions must return a string.
195 This option can also be set with the AUTHOR keyword."
196 :group
'org-export-koma-letter
197 :type
'(radio (function-item user-full-name
)
200 (const :tag
"Do not export author" nil
)))
202 (defcustom org-koma-letter-email
'org-koma-letter-email
203 "Sender's email address.
205 This variable defaults to the value `org-koma-letter-email' which
206 returns `user-mail-address'. Alternatively a string, nil or
207 a function may be given. Functions must return a string.
209 This option can also be set with the EMAIL keyword."
210 :group
'org-export-koma-letter
211 :type
'(radio (function-item org-koma-letter-email
)
214 (const :tag
"Do not export email" nil
)))
216 (defcustom org-koma-letter-from-address
""
217 "Sender's address, as a string.
218 This option can also be set with one or more FROM_ADDRESS
220 :group
'org-export-koma-letter
223 (defcustom org-koma-letter-phone-number
""
224 "Sender's phone number, as a string.
225 This option can also be set with the PHONE_NUMBER keyword."
226 :group
'org-export-koma-letter
229 (defcustom org-koma-letter-url
""
230 "Sender's URL, e. g., the URL of her homepage.
231 This option can also be set with the URL keyword."
232 :group
'org-export-koma-letter
236 (defcustom org-koma-letter-from-logo
""
237 "Commands for inserting the sender's logo, e. g., \\includegraphics{logo}.
238 This option can also be set with the FROM_LOGO keyword."
239 :group
'org-export-koma-letter
243 (defcustom org-koma-letter-place
""
244 "Place from which the letter is sent, as a string.
245 This option can also be set with the PLACE keyword."
246 :group
'org-export-koma-letter
249 (defcustom org-koma-letter-location
""
250 "Sender's extension field, as a string.
252 This option can also be set with the LOCATION keyword.
254 (1) Either `org-koma-letter-prefer-special-headings' is non-nil
255 or there is no LOCATION keyword or the LOCATION keyword is
257 (2) the letter contains a headline with the special
259 then the location will be set as the content of the location
262 The location field is typically printed right of the address
263 field (See Figure 4.9. in the English manual of 2015-10-03)."
264 :group
'org-export-koma-letter
267 (defcustom org-koma-letter-opening
""
268 "Letter's opening, as a string.
270 This option can also be set with the OPENING keyword. Moreover,
272 (1) Either `org-koma-letter-prefer-special-headings' is non-nil
273 or the CLOSING keyword is empty
274 (2) `org-koma-letter-headline-is-opening-maybe' is non-nil;
275 (3) the letter contains a headline without a special
276 tag (e.g. \"to\" or \"ps\");
277 then the opening will be implicitly set as the untagged headline title."
278 :group
'org-export-koma-letter
281 (defcustom org-koma-letter-closing
""
282 "Letter's closing, as a string.
283 This option can also be set with the CLOSING keyword. Moreover,
285 (1) Either `org-koma-letter-prefer-special-headings' is non-nil
286 or the CLOSING keyword is empty;
287 (2) `org-koma-letter-headline-is-opening-maybe' is non-nil;
288 (3) the letter contains a headline with the special
290 then the opening will be set as the title of the closing special
292 :group
'org-export-koma-letter
295 (defcustom org-koma-letter-signature
""
296 "Signature, as a string.
297 This option can also be set with the SIGNATURE keyword.
299 (1) Either `org-koma-letter-prefer-special-headings' is non-nil
300 or there is no CLOSING keyword or the CLOSING keyword is empty;
301 (2) `org-koma-letter-headline-is-opening-maybe' is non-nil;
302 (3) the letter contains a headline with the special
304 then the signature will be set as the content of the
305 closing special heading.
307 Note if the content is empty the signature will not be set."
308 :group
'org-export-koma-letter
311 (defcustom org-koma-letter-prefer-special-headings nil
312 "Non-nil means prefer headlines over keywords for TO and FROM.
313 This option can also be set with the OPTIONS keyword, e.g.:
314 \"special-headings:t\"."
315 :group
'org-export-koma-letter
318 (defcustom org-koma-letter-subject-format t
319 "Non-nil means include the subject.
321 Support formatting options.
323 When t, insert a subject using default options. When nil, do not
324 insert a subject at all. It can also be a list of symbols among
327 `afteropening' Subject after opening
328 `beforeopening' Subject before opening
329 `centered' Subject centered
330 `left' Subject left-justified
331 `right' Subject right-justified
332 `titled' Add title/description to subject
333 `underlined' Set subject underlined
334 `untitled' Do not add title/description to subject
336 Please refer to the KOMA-script manual (Table 4.16. in the
337 English manual of 2012-07-22).
339 This option can also be set with the OPTIONS keyword, e.g.:
340 \"subject:(underlined centered)\"."
343 (const :tag
"No export" nil
)
344 (const :tag
"Default options" t
)
345 (set :tag
"Configure options"
346 (const :tag
"Subject after opening" afteropening
)
347 (const :tag
"Subject before opening" beforeopening
)
348 (const :tag
"Subject centered" centered
)
349 (const :tag
"Subject left-justified" left
)
350 (const :tag
"Subject right-justified" right
)
351 (const :tag
"Add title or description to subject" underlined
)
352 (const :tag
"Set subject underlined" titled
)
353 (const :tag
"Do not add title or description to subject" untitled
)))
354 :group
'org-export-koma-letter
)
356 (defcustom org-koma-letter-use-backaddress nil
357 "Non-nil prints return address in line above to address.
358 This option can also be set with the OPTIONS keyword, e.g.:
360 :group
'org-export-koma-letter
363 (defcustom org-koma-letter-use-foldmarks t
364 "Configure appearance of folding marks.
366 When t, activate default folding marks. When nil, do not insert
367 folding marks at all. It can also be a list of symbols among the
370 `B' Activate upper horizontal mark on left paper edge
371 `b' Deactivate upper horizontal mark on left paper edge
373 `H' Activate all horizontal marks on left paper edge
374 `h' Deactivate all horizontal marks on left paper edge
376 `L' Activate left vertical mark on upper paper edge
377 `l' Deactivate left vertical mark on upper paper edge
379 `M' Activate middle horizontal mark on left paper edge
380 `m' Deactivate middle horizontal mark on left paper edge
382 `P' Activate punch or center mark on left paper edge
383 `p' Deactivate punch or center mark on left paper edge
385 `T' Activate lower horizontal mark on left paper edge
386 `t' Deactivate lower horizontal mark on left paper edge
388 `V' Activate all vertical marks on upper paper edge
389 `v' Deactivate all vertical marks on upper paper edge
391 This option can also be set with the OPTIONS keyword, e.g.:
392 \"foldmarks:(b l m t)\"."
393 :group
'org-export-koma-letter
395 (const :tag
"Activate default folding marks" t
)
396 (const :tag
"Deactivate folding marks" nil
)
398 :tag
"Configure folding marks"
399 (const :tag
"Activate upper horizontal mark on left paper edge" B
)
400 (const :tag
"Deactivate upper horizontal mark on left paper edge" b
)
401 (const :tag
"Activate all horizontal marks on left paper edge" H
)
402 (const :tag
"Deactivate all horizontal marks on left paper edge" h
)
403 (const :tag
"Activate left vertical mark on upper paper edge" L
)
404 (const :tag
"Deactivate left vertical mark on upper paper edge" l
)
405 (const :tag
"Activate middle horizontal mark on left paper edge" M
)
406 (const :tag
"Deactivate middle horizontal mark on left paper edge" m
)
407 (const :tag
"Activate punch or center mark on left paper edge" P
)
408 (const :tag
"Deactivate punch or center mark on left paper edge" p
)
409 (const :tag
"Activate lower horizontal mark on left paper edge" T
)
410 (const :tag
"Deactivate lower horizontal mark on left paper edge" t
)
411 (const :tag
"Activate all vertical marks on upper paper edge" V
)
412 (const :tag
"Deactivate all vertical marks on upper paper edge" v
))))
414 (defcustom org-koma-letter-use-phone nil
415 "Non-nil prints sender's phone number.
416 This option can also be set with the OPTIONS keyword, e.g.:
418 :group
'org-export-koma-letter
421 (defcustom org-koma-letter-use-url nil
422 "Non-nil prints sender's URL.
423 This option can also be set with the OPTIONS keyword, e.g.:
425 :group
'org-export-koma-letter
429 (defcustom org-koma-letter-use-from-logo nil
430 "Non-nil prints sender's FROM_LOGO.
431 This option can also be set with the OPTIONS keyword, e.g.:
433 :group
'org-export-koma-letter
437 (defcustom org-koma-letter-use-email nil
438 "Non-nil prints sender's email address.
439 This option can also be set with the OPTIONS keyword, e.g.:
441 :group
'org-export-koma-letter
444 (defcustom org-koma-letter-use-place t
445 "Non-nil prints the letter's place next to the date.
446 This option can also be set with the OPTIONS keyword, e.g.:
448 :group
'org-export-koma-letter
451 (defcustom org-koma-letter-default-class
"default-koma-letter"
452 "Default class for `org-koma-letter'.
453 The value must be a member of `org-latex-classes'."
454 :group
'org-export-koma-letter
457 (defcustom org-koma-letter-headline-is-opening-maybe t
458 "Non-nil means a headline may be used as an opening and closing.
459 See also `org-koma-letter-opening' and
460 `org-koma-letter-closing'."
461 :group
'org-export-koma-letter
464 (defcustom org-koma-letter-prefer-subject nil
465 "Non-nil means title should be interpreted as subject if subject is missing.
466 This option can also be set with the OPTIONS keyword,
467 e.g. \"title-subject:t\"."
468 :group
'org-export-koma-letter
471 (defconst org-koma-letter-special-tags-in-letter
'(to from closing location
)
472 "Header tags related to the letter itself.")
474 (defconst org-koma-letter-special-tags-after-closing
'(after_closing ps encl cc
)
475 "Header tags to be inserted in the letter after closing.")
477 (defconst org-koma-letter-special-tags-as-macro
'(ps encl cc
)
478 "Header tags to be inserted as macros")
480 (defconst org-koma-letter-special-tags-after-letter
'(after_letter)
481 "Header tags to be inserted after the letter.")
483 (defvar org-koma-letter-special-contents nil
484 "Holds special content temporarily.")
486 (make-obsolete-variable 'org-koma-letter-use-title
487 'org-export-with-title
493 (org-export-define-derived-backend 'koma-letter
'latex
495 '((:latex-class
"LATEX_CLASS" nil org-koma-letter-default-class t
)
496 (:lco
"LCO" nil org-koma-letter-class-option-file
)
497 (:author
"AUTHOR" nil
(org-koma-letter--get-value org-koma-letter-author
) parse
)
498 (:author-changed-in-buffer-p
"AUTHOR" nil nil t
)
499 (:from-address
"FROM_ADDRESS" nil org-koma-letter-from-address newline
)
500 (:phone-number
"PHONE_NUMBER" nil org-koma-letter-phone-number
)
501 (:url
"URL" nil org-koma-letter-url
)
502 (:from-logo
"FROM_LOGO" nil org-koma-letter-from-logo
)
503 (:email
"EMAIL" nil
(org-koma-letter--get-value org-koma-letter-email
) t
)
504 (:to-address
"TO_ADDRESS" nil nil newline
)
505 (:place
"PLACE" nil org-koma-letter-place
)
506 (:location
"LOCATION" nil org-koma-letter-location
)
507 (:subject
"SUBJECT" nil nil parse
)
508 (:opening
"OPENING" nil org-koma-letter-opening parse
)
509 (:closing
"CLOSING" nil org-koma-letter-closing parse
)
510 (:signature
"SIGNATURE" nil org-koma-letter-signature newline
)
511 (:special-headings nil
"special-headings" org-koma-letter-prefer-special-headings
)
512 (:special-tags-as-macro nil nil org-koma-letter-special-tags-as-macro
)
513 (:special-tags-in-letter nil nil org-koma-letter-special-tags-in-letter
)
514 (:special-tags-after-closing nil
"after-closing-order"
515 org-koma-letter-special-tags-after-closing
)
516 (:special-tags-after-letter nil
"after-letter-order"
517 org-koma-letter-special-tags-after-letter
)
518 (:with-backaddress nil
"backaddress" org-koma-letter-use-backaddress
)
519 (:with-email nil
"email" org-koma-letter-use-email
)
520 (:with-foldmarks nil
"foldmarks" org-koma-letter-use-foldmarks
)
521 (:with-phone nil
"phone" org-koma-letter-use-phone
)
522 (:with-url nil
"url" org-koma-letter-use-url
)
523 (:with-from-logo nil
"from-logo" org-koma-letter-use-from-logo
)
524 (:with-place nil
"place" org-koma-letter-use-place
)
525 (:with-subject nil
"subject" org-koma-letter-subject-format
)
526 (:with-title-as-subject nil
"title-subject" org-koma-letter-prefer-subject
)
527 (:with-headline-opening nil nil org-koma-letter-headline-is-opening-maybe
)
528 ;; Special properties non-nil when a setting happened in buffer.
529 ;; They are used to prioritize in-buffer settings over "lco"
530 ;; files. See `org-koma-letter-template'.
531 (:inbuffer-author
"AUTHOR" nil
'koma-letter
:empty
)
532 (:inbuffer-from
"FROM" nil
'koma-letter
:empty
)
533 (:inbuffer-email
"EMAIL" nil
'koma-letter
:empty
)
534 (:inbuffer-phone-number
"PHONE_NUMBER" nil
'koma-letter
:empty
)
535 (:inbuffer-url
"URL" nil
'koma-letter
:empty
)
536 (:inbuffer-from-logo
"FROM_LOGO" nil
'koma-letter
:empty
)
537 (:inbuffer-place
"PLACE" nil
'koma-letter
:empty
)
538 (:inbuffer-location
"LOCATION" nil
'koma-letter
:empty
)
539 (:inbuffer-signature
"SIGNATURE" nil
'koma-letter
:empty
)
540 (:inbuffer-with-backaddress nil
"backaddress" 'koma-letter
:empty
)
541 (:inbuffer-with-email nil
"email" 'koma-letter
:empty
)
542 (:inbuffer-with-foldmarks nil
"foldmarks" 'koma-letter
:empty
)
543 (:inbuffer-with-phone nil
"phone" 'koma-letter
:empty
)
544 (:inbuffer-with-url nil
"url" 'koma-letter
:empty
)
545 (:inbuffer-with-from-logo nil
"from-logo" 'koma-letter
:empty
)
546 (:inbuffer-with-place nil
"place" 'koma-letter
:empty
))
547 :translate-alist
'((export-block . org-koma-letter-export-block
)
548 (export-snippet . org-koma-letter-export-snippet
)
549 (headline . org-koma-letter-headline
)
550 (keyword . org-koma-letter-keyword
)
551 (template . org-koma-letter-template
))
553 '(?k
"Export with KOMA Scrlttr2"
554 ((?L
"As LaTeX buffer" org-koma-letter-export-as-latex
)
555 (?l
"As LaTeX file" org-koma-letter-export-to-latex
)
556 (?p
"As PDF file" org-koma-letter-export-to-pdf
)
557 (?o
"As PDF file and open"
559 (if a
(org-koma-letter-export-to-pdf t s v b
)
560 (org-open-file (org-koma-letter-export-to-pdf nil s v b
))))))))
566 (defun org-koma-letter-email ()
567 "Return the current `user-mail-address'."
570 ;; The following is taken from/inspired by ox-grof.el
573 (defun org-koma-letter--get-tagged-contents (key)
574 "Get contents from a headline tagged with KEY.
575 The contents is stored in `org-koma-letter-special-contents'."
576 (let ((value (cdr (assoc-string (org-koma-letter--get-value key
)
577 org-koma-letter-special-contents
))))
578 (when value
(org-string-nw-p (org-trim value
)))))
580 (defun org-koma-letter--get-value (value)
581 "Turn value into a string whenever possible.
582 Determines if VALUE is nil, a string, a function or a symbol and
583 return a string or nil."
585 (cond ((stringp value
) value
)
586 ((functionp value
) (funcall value
))
587 ((symbolp value
) (symbol-name value
))
590 (defun org-koma-letter--special-contents-inline (keywords info
)
591 "Process KEYWORDS members of `org-koma-letter-special-contents'.
593 KEYWORDS is a list of symbols. Return them as a string to be
596 The function is used for inserting content of special headings
597 such as the one tagged with PS."
600 (let* ((name (org-koma-letter--get-value keyword
))
601 (value (org-koma-letter--get-tagged-contents name
))
602 (macrop (memq keyword
(plist-get info
:special-tags-as-macro
))))
603 (cond ((not value
) nil
)
604 (macrop (format "\\%s{%s}\n" name value
))
610 (defun org-koma-letter--add-latex-newlines (string)
611 "Replace regular newlines with LaTeX newlines (i.e. `\\\\')"
612 (let ((str (org-trim string
)))
613 (when (org-string-nw-p str
)
614 (replace-regexp-in-string "\n" "\\\\\\\\\n" str
))))
618 ;;; Transcode Functions
622 (defun org-koma-letter-export-block (export-block contents info
)
623 "Transcode an EXPORT-BLOCK element into KOMA Scrlttr2 code.
624 CONTENTS is nil. INFO is a plist used as a communication
626 (when (member (org-element-property :type export-block
)
627 '("KOMA-LETTER" "LATEX"))
628 (org-remove-indentation (org-element-property :value export-block
))))
632 (defun org-koma-letter-export-snippet (export-snippet contents info
)
633 "Transcode an EXPORT-SNIPPET object into KOMA Scrlttr2 code.
634 CONTENTS is nil. INFO is a plist used as a communication
636 (when (memq (org-export-snippet-backend export-snippet
) '(latex koma-letter
))
637 (org-element-property :value export-snippet
)))
641 (defun org-koma-letter-keyword (keyword contents info
)
642 "Transcode a KEYWORD element into KOMA Scrlttr2 code.
643 CONTENTS is nil. INFO is a plist used as a communication
645 (let ((key (org-element-property :key keyword
))
646 (value (org-element-property :value keyword
)))
647 ;; Handle specifically KOMA-LETTER keywords. Otherwise, fallback
648 ;; to `latex' back-end.
649 (if (equal key
"KOMA-LETTER") value
650 (org-export-with-backend 'latex keyword contents info
))))
654 (defun org-koma-letter-headline (headline contents info
)
655 "Transcode a HEADLINE element from Org to LaTeX.
656 CONTENTS holds the contents of the headline. INFO is a plist
657 holding contextual information.
659 Note that if a headline is tagged with a tag from
660 `org-koma-letter-special-tags' it will not be exported, but
661 stored in `org-koma-letter-special-contents' and included at the
663 (let ((special-tag (org-koma-letter--special-tag headline info
)))
664 (if (not special-tag
)
666 (push (cons special-tag contents
) org-koma-letter-special-contents
)
669 (defun org-koma-letter--special-tag (headline info
)
670 "Non-nil if HEADLINE is a special headline.
671 INFO is a plist holding contextual information. Return first
672 special tag headline."
673 (let ((special-tags (append
674 (plist-get info
:special-tags-in-letter
)
675 (plist-get info
:special-tags-after-closing
)
676 (plist-get info
:special-tags-after-letter
))))
677 (cl-some (lambda (tag) (and (assoc-string tag special-tags
) tag
))
678 (org-export-get-tags headline info
))))
680 (defun org-koma-letter--keyword-or-headline (plist-key pred info
)
681 "Return the correct version of opening or closing.
682 PLIST-KEY should be a key in info, typically :opening
683 or :closing. PRED is a predicate run on headline to determine
684 which title to use which takes two arguments, a headline element
685 and an info plist. INFO is a plist holding contextual
686 information. Return the preferred candidate for the exported of
688 (let* ((keyword-candidate (plist-get info plist-key
))
689 (headline-candidate (when (and (plist-get info
:with-headline-opening
)
690 (or (plist-get info
:special-headings
)
691 (not keyword-candidate
)))
692 (org-element-map (plist-get info
:parse-tree
)
695 (and (funcall pred h info
)
696 (org-element-property :title h
)))
698 (org-export-data (or headline-candidate keyword-candidate
"") info
)))
702 (defun org-koma-letter-template (contents info
)
703 "Return complete document string after KOMA Scrlttr2 conversion.
704 CONTENTS is the transcoded contents string. INFO is a plist
705 holding export options."
708 (and (plist-get info
:time-stamp-file
)
709 (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
711 (org-latex--insert-compiler info
)
712 ;; Document class and packages.
713 (org-latex-make-preamble info
)
714 ;; Settings. They can come from three locations, in increasing
715 ;; order of precedence: global variables, LCO files and in-buffer
716 ;; settings. Thus, we first insert settings coming from global
717 ;; variables, then we insert LCO files, and, eventually, we insert
718 ;; settings coming from buffer keywords.
719 (org-koma-letter--build-settings 'global info
)
720 (mapconcat (lambda (file) (format "\\LoadLetterOption{%s}\n" file
))
721 (split-string (or (plist-get info
:lco
) ""))
723 (org-koma-letter--build-settings 'buffer info
)
725 (format "\\date{%s}\n" (org-export-data (org-export-get-date info
) info
))
726 ;; Hyperref, document start, and subject and title.
727 (let* ((with-subject (plist-get info
:with-subject
))
728 (with-title (plist-get info
:with-title
))
729 (title-as-subject (and with-subject
730 (plist-get info
:with-title-as-subject
)))
731 (subject* (org-string-nw-p
732 (org-export-data (plist-get info
:subject
) info
)))
733 (title* (and with-title
735 (org-export-data (plist-get info
:title
) info
))))
736 (subject (cond ((not with-subject
) nil
)
737 (title-as-subject (or subject
* title
*))
739 (title (cond ((not with-title
) nil
)
740 (title-as-subject (and subject
* title
*))
742 (hyperref-template (plist-get info
:latex-hyperref-template
))
743 (spec (append (list (cons ?t
(or title subject
"")))
744 (org-latex--format-spec info
))))
746 (when (and with-subject
(not (eq with-subject t
)))
747 (format "\\KOMAoption{subject}{%s}\n"
748 (if (symbolp with-subject
) with-subject
749 (mapconcat #'symbol-name with-subject
","))))
751 (format-spec hyperref-template spec
)
753 "\\begin{document}\n\n"
754 ;; Subject and title.
755 (when subject
(format "\\setkomavar{subject}{%s}\n" subject
))
756 (when title
(format "\\setkomavar{title}{%s}\n" title
))
757 (when (or (org-string-nw-p title
) (org-string-nw-p subject
)) "\n")))
759 (let ((keyword-val (plist-get info
:to-address
))
760 (heading-val (org-koma-letter--get-tagged-contents 'to
)))
761 (format "\\begin{letter}{%%\n%s}\n\n"
762 (org-koma-letter--add-latex-newlines
763 (or (if (plist-get info
:special-headings
)
764 (or heading-val keyword-val
)
765 (or keyword-val heading-val
))
768 (format "\\opening{%s}\n\n"
769 (org-koma-letter--keyword-or-headline
772 (not (org-koma-letter--special-tag h i
)))
777 (format "\\closing{%s}\n"
778 (org-koma-letter--keyword-or-headline
781 (let ((special-tag (org-koma-letter--special-tag h i
)))
783 (string= "closing" special-tag
))))
785 (org-koma-letter--special-contents-inline
786 (plist-get info
:special-tags-after-closing
) info
)
789 (org-koma-letter--special-contents-inline
790 (plist-get info
:special-tags-after-letter
) info
)
792 "\n\\end{document}"))
794 (defun org-koma-letter--build-settings (scope info
)
795 "Build settings string according to type.
796 SCOPE is either `global' or `buffer'. INFO is a plist used as
797 a communication channel."
799 ;; Non-nil value when SETTING was defined in SCOPE.
801 (let ((property (intern (format ":inbuffer-%s" setting
))))
802 (if (eq scope
'global
)
803 (eq (plist-get info property
) 'koma-letter
:empty
)
804 (not (eq (plist-get info property
) 'koma-letter
:empty
))))))
805 (heading-or-key-value
806 (lambda (heading key
&optional scoped
)
808 (org-koma-letter--get-tagged-contents heading
))
809 (key-val (org-string-nw-p (plist-get info key
)))
810 (scopedp (funcall check-scope
(or scoped heading
))))
811 (and (or (and key-val scopedp
) heading-val
)
812 (not (and (eq scope
'global
) heading-val
))
813 (if scopedp key-val heading-val
))))))
816 (let ((author (plist-get info
:author
)))
818 (funcall check-scope
'author
)
819 (format "\\setkomavar{fromname}{%s}\n"
820 (org-export-data author info
))))
822 (let ((from (funcall heading-or-key-value
'from
:from-address
)))
824 (format "\\setkomavar{fromaddress}{%s}\n"
825 (org-koma-letter--add-latex-newlines from
))))
827 (let ((email (plist-get info
:email
)))
829 (funcall check-scope
'email
)
830 (format "\\setkomavar{fromemail}{%s}\n" email
)))
831 (and (funcall check-scope
'with-email
)
832 (format "\\KOMAoption{fromemail}{%s}\n"
833 (if (plist-get info
:with-email
) "true" "false")))
835 (let ((phone-number (plist-get info
:phone-number
)))
836 (and (org-string-nw-p phone-number
)
837 (funcall check-scope
'phone-number
)
838 (format "\\setkomavar{fromphone}{%s}\n" phone-number
)))
839 (and (funcall check-scope
'with-phone
)
840 (format "\\KOMAoption{fromphone}{%s}\n"
841 (if (plist-get info
:with-phone
) "true" "false")))
843 (let ((url (plist-get info
:url
)))
844 (and (org-string-nw-p url
)
845 (funcall check-scope
'url
)
846 (format "\\setkomavar{fromurl}{%s}\n" url
)))
847 (and (funcall check-scope
'with-url
)
848 (format "\\KOMAoption{fromurl}{%s}\n"
849 (if (plist-get info
:with-url
) "true" "false")))
851 (let ((from-logo (plist-get info
:from-logo
)))
852 (and (org-string-nw-p from-logo
)
853 (funcall check-scope
'from-logo
)
854 (format "\\setkomavar{fromlogo}{%s}\n" from-logo
)))
855 (and (funcall check-scope
'with-from-logo
)
856 (format "\\KOMAoption{fromlogo}{%s}\n"
857 (if (plist-get info
:with-from-logo
) "true" "false")))
860 (and (plist-get info
:with-headline-opening
)
861 (pcase (org-koma-letter--get-tagged-contents 'closing
)
862 ((and (pred org-string-nw-p
) closing
) (org-trim closing
))
864 (signature (org-string-nw-p (plist-get info
:signature
)))
865 (signature-scope (funcall check-scope
'signature
)))
866 (and (or (and signature signature-scope
)
868 (not (and (eq scope
'global
) heading-val
))
869 (format "\\setkomavar{signature}{%s}\n"
870 (if signature-scope signature heading-val
))))
872 (and (funcall check-scope
'with-backaddress
)
873 (format "\\KOMAoption{backaddress}{%s}\n"
874 (if (plist-get info
:with-backaddress
) "true" "false")))
876 (let ((with-place-set (funcall check-scope
'with-place
))
877 (place-set (funcall check-scope
'place
)))
878 (and (or (and with-place-set place-set
)
879 (and (eq scope
'buffer
) (or with-place-set place-set
)))
880 (format "\\setkomavar{place}{%s}\n"
881 (if (plist-get info
:with-place
) (plist-get info
:place
)
884 (let ((location (funcall heading-or-key-value
'location
:location
)))
886 (format "\\setkomavar{location}{%s}\n" location
)))
888 (and (funcall check-scope
'with-foldmarks
)
889 (let ((foldmarks (plist-get info
:with-foldmarks
)))
890 (cond ((consp foldmarks
)
891 (format "\\KOMAoptions{foldmarks=true,foldmarks=%s}\n"
892 (mapconcat #'symbol-name foldmarks
"")))
893 (foldmarks "\\KOMAoptions{foldmarks=true}\n")
894 (t "\\KOMAoptions{foldmarks=false}\n")))))))
901 (defun org-koma-letter-export-as-latex
902 (&optional async subtreep visible-only body-only ext-plist
)
903 "Export current buffer as a KOMA Scrlttr2 letter.
905 If narrowing is active in the current buffer, only export its
908 If a region is active, export that region.
910 A non-nil optional argument ASYNC means the process should happen
911 asynchronously. The resulting buffer should be accessible
912 through the `org-export-stack' interface.
914 When optional argument SUBTREEP is non-nil, export the sub-tree
915 at point, extracting information from the headline properties
918 When optional argument VISIBLE-ONLY is non-nil, don't export
919 contents of hidden elements.
921 When optional argument BODY-ONLY is non-nil, only write code
922 between \"\\begin{letter}\" and \"\\end{letter}\".
924 EXT-PLIST, when provided, is a proeprty list with external
925 parameters overriding Org default settings, but still inferior to
928 Export is done in a buffer named \"*Org KOMA-LETTER Export*\". It
929 will be displayed if `org-export-show-temporary-export-buffer' is
932 (let (org-koma-letter-special-contents)
933 (org-export-to-buffer 'koma-letter
"*Org KOMA-LETTER Export*"
934 async subtreep visible-only body-only ext-plist
935 (lambda () (LaTeX-mode)))))
938 (defun org-koma-letter-export-to-latex
939 (&optional async subtreep visible-only body-only ext-plist
)
940 "Export current buffer as a KOMA Scrlttr2 letter (tex).
942 If narrowing is active in the current buffer, only export its
945 If a region is active, export that region.
947 A non-nil optional argument ASYNC means the process should happen
948 asynchronously. The resulting file should be accessible through
949 the `org-export-stack' interface.
951 When optional argument SUBTREEP is non-nil, export the sub-tree
952 at point, extracting information from the headline properties
955 When optional argument VISIBLE-ONLY is non-nil, don't export
956 contents of hidden elements.
958 When optional argument BODY-ONLY is non-nil, only write code
959 between \"\\begin{letter}\" and \"\\end{letter}\".
961 EXT-PLIST, when provided, is a property list with external
962 parameters overriding Org default settings, but still inferior to
965 When optional argument PUB-DIR is set, use it as the publishing
968 Return output file's name."
970 (let ((outfile (org-export-output-file-name ".tex" subtreep
))
971 (org-koma-letter-special-contents))
972 (org-export-to-file 'koma-letter outfile
973 async subtreep visible-only body-only ext-plist
)))
976 (defun org-koma-letter-export-to-pdf
977 (&optional async subtreep visible-only body-only ext-plist
)
978 "Export current buffer as a KOMA Scrlttr2 letter (pdf).
980 If narrowing is active in the current buffer, only export its
983 If a region is active, export that region.
985 A non-nil optional argument ASYNC means the process should happen
986 asynchronously. The resulting file should be accessible through
987 the `org-export-stack' interface.
989 When optional argument SUBTREEP is non-nil, export the sub-tree
990 at point, extracting information from the headline properties
993 When optional argument VISIBLE-ONLY is non-nil, don't export
994 contents of hidden elements.
996 When optional argument BODY-ONLY is non-nil, only write code
997 between \"\\begin{letter}\" and \"\\end{letter}\".
999 EXT-PLIST, when provided, is a property list with external
1000 parameters overriding Org default settings, but still inferior to
1001 file-local settings.
1003 Return PDF file's name."
1005 (let ((file (org-export-output-file-name ".tex" subtreep
))
1006 (org-koma-letter-special-contents))
1007 (org-export-to-file 'koma-letter file
1008 async subtreep visible-only body-only ext-plist
1009 (lambda (file) (org-latex-compile file
)))))
1012 (provide 'ox-koma-letter
)
1013 ;;; ox-koma-letter.el ends here