ox-koma-letter: Fixed bug in filling 'location' field
[org-mode.git] / contrib / lisp / ox-koma-letter.el
blob2ef9025eb69f11499e248a99e25da6225e58dbf0
1 ;;; ox-koma-letter.el --- KOMA Scrlttr2 Back-End for Org Export Engine
3 ;; Copyright (C) 2007-2016 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-use-location t
380 "Non-nil prints the contents of the letter's extension below the header.
381 This option can also be set with the OPTIONS keyword, e.g.:
382 \"location:nil\"."
383 :group 'org-export-koma-letter
384 :type 'boolean)
386 (defcustom org-koma-letter-default-class "default-koma-letter"
387 "Default class for `org-koma-letter'.
388 The value must be a member of `org-latex-classes'."
389 :group 'org-export-koma-letter
390 :type 'string)
392 (defcustom org-koma-letter-headline-is-opening-maybe t
393 "Non-nil means a headline may be used as an opening and closing.
394 See also `org-koma-letter-opening' and
395 `org-koma-letter-closing'."
396 :group 'org-export-koma-letter
397 :type 'boolean)
399 (defcustom org-koma-letter-prefer-subject nil
400 "Non-nil means title should be interpreted as subject if subject is missing.
401 This option can also be set with the OPTIONS keyword,
402 e.g. \"title-subject:t\"."
403 :group 'org-export-koma-letter
404 :type 'boolean)
406 (defconst org-koma-letter-special-tags-in-letter '(to from closing location)
407 "Header tags related to the letter itself.")
409 (defconst org-koma-letter-special-tags-after-closing '(after_closing ps encl cc)
410 "Header tags to be inserted in the letter after closing.")
412 (defconst org-koma-letter-special-tags-as-macro '(ps encl cc)
413 "Header tags to be inserted as macros")
415 (defconst org-koma-letter-special-tags-after-letter '(after_letter)
416 "Header tags to be inserted after the letter.")
418 (defvar org-koma-letter-special-contents nil
419 "Holds special content temporarily.")
421 (make-obsolete-variable 'org-koma-letter-use-title
422 'org-export-with-title
423 "25.1" 'set)
426 ;;; Define Back-End
428 (org-export-define-derived-backend 'koma-letter 'latex
429 :options-alist
430 '((:latex-class "LATEX_CLASS" nil org-koma-letter-default-class t)
431 (:lco "LCO" nil org-koma-letter-class-option-file)
432 (:author "AUTHOR" nil (org-koma-letter--get-value org-koma-letter-author) parse)
433 (:author-changed-in-buffer-p "AUTHOR" nil nil t)
434 (:from-address "FROM_ADDRESS" nil org-koma-letter-from-address newline)
435 (:phone-number "PHONE_NUMBER" nil org-koma-letter-phone-number)
436 (:email "EMAIL" nil (org-koma-letter--get-value org-koma-letter-email) t)
437 (:to-address "TO_ADDRESS" nil nil newline)
438 (:place "PLACE" nil org-koma-letter-place)
439 (:location "LOCATION" nil org-koma-letter-location)
440 (:subject "SUBJECT" nil nil parse)
441 (:opening "OPENING" nil org-koma-letter-opening parse)
442 (:closing "CLOSING" nil org-koma-letter-closing parse)
443 (:signature "SIGNATURE" nil org-koma-letter-signature newline)
444 (:special-headings nil "special-headings" org-koma-letter-prefer-special-headings)
445 (:special-tags-as-macro nil nil org-koma-letter-special-tags-as-macro)
446 (:special-tags-in-letter nil nil org-koma-letter-special-tags-in-letter)
447 (:special-tags-after-closing nil "after-closing-order"
448 org-koma-letter-special-tags-after-closing)
449 (:special-tags-after-letter nil "after-letter-order"
450 org-koma-letter-special-tags-after-letter)
451 (:with-backaddress nil "backaddress" org-koma-letter-use-backaddress)
452 (:with-email nil "email" org-koma-letter-use-email)
453 (:with-foldmarks nil "foldmarks" org-koma-letter-use-foldmarks)
454 (:with-phone nil "phone" org-koma-letter-use-phone)
455 (:with-place nil "place" org-koma-letter-use-place)
456 (:with-location nil "location" org-koma-letter-use-location)
457 (:with-subject nil "subject" org-koma-letter-subject-format)
458 (:with-title-as-subject nil "title-subject" org-koma-letter-prefer-subject)
459 (:with-headline-opening nil nil org-koma-letter-headline-is-opening-maybe)
460 ;; Special properties non-nil when a setting happened in buffer.
461 ;; They are used to prioritize in-buffer settings over "lco"
462 ;; files. See `org-koma-letter-template'.
463 (:inbuffer-author "AUTHOR" nil 'koma-letter:empty)
464 (:inbuffer-from "FROM" nil 'koma-letter:empty)
465 (:inbuffer-email "EMAIL" nil 'koma-letter:empty)
466 (:inbuffer-phone-number "PHONE_NUMBER" nil 'koma-letter:empty)
467 (:inbuffer-place "PLACE" nil 'koma-letter:empty)
468 (:inbuffer-location "LOCATION" nil 'koma-letter:empty)
469 (:inbuffer-signature "SIGNATURE" nil 'koma-letter:empty)
470 (:inbuffer-with-backaddress nil "backaddress" 'koma-letter:empty)
471 (:inbuffer-with-email nil "email" 'koma-letter:empty)
472 (:inbuffer-with-foldmarks nil "foldmarks" 'koma-letter:empty)
473 (:inbuffer-with-phone nil "phone" 'koma-letter:empty)
474 (:inbuffer-with-place nil "place" 'koma-letter:empty)
475 (:inbuffer-with-location nil "location" 'koma-letter:empty))
476 :translate-alist '((export-block . org-koma-letter-export-block)
477 (export-snippet . org-koma-letter-export-snippet)
478 (headline . org-koma-letter-headline)
479 (keyword . org-koma-letter-keyword)
480 (template . org-koma-letter-template))
481 :menu-entry
482 '(?k "Export with KOMA Scrlttr2"
483 ((?L "As LaTeX buffer" org-koma-letter-export-as-latex)
484 (?l "As LaTeX file" org-koma-letter-export-to-latex)
485 (?p "As PDF file" org-koma-letter-export-to-pdf)
486 (?o "As PDF file and open"
487 (lambda (a s v b)
488 (if a (org-koma-letter-export-to-pdf t s v b)
489 (org-open-file (org-koma-letter-export-to-pdf nil s v b))))))))
493 ;;; Helper functions
495 (defun org-koma-letter-email ()
496 "Return the current `user-mail-address'."
497 user-mail-address)
499 ;; The following is taken from/inspired by ox-grof.el
500 ;; Thanks, Luis!
502 (defun org-koma-letter--get-tagged-contents (key)
503 "Get contents from a headline tagged with KEY.
504 The contents is stored in `org-koma-letter-special-contents'."
505 (let ((value (cdr (assoc-string (org-koma-letter--get-value key)
506 org-koma-letter-special-contents))))
507 (when value (org-string-nw-p (org-trim value)))))
509 (defun org-koma-letter--get-value (value)
510 "Turn value into a string whenever possible.
511 Determines if VALUE is nil, a string, a function or a symbol and
512 return a string or nil."
513 (when value
514 (cond ((stringp value) value)
515 ((functionp value) (funcall value))
516 ((symbolp value) (symbol-name value))
517 (t value))))
519 (defun org-koma-letter--special-contents-inline (keywords info)
520 "Process KEYWORDS members of `org-koma-letter-special-contents'.
521 KEYWORDS is a list of symbols. Return them as a string to be
522 formatted.
524 The function is used for inserting content of special headings
525 such as the one tagged with PS.
527 (mapconcat
528 (lambda (keyword)
529 (let* ((name (org-koma-letter--get-value keyword))
530 (value (org-koma-letter--get-tagged-contents name))
531 (macrop (memq keyword (plist-get info :special-tags-as-macro))))
532 (cond ((not value) nil)
533 (macrop (format "\\%s{%s}\n" name value))
534 (t value))))
535 keywords
536 "\n"))
539 (defun org-koma-letter--add-latex-newlines (string)
540 "Replace regular newlines with LaTeX newlines (i.e. `\\\\')"
541 (let ((str (org-trim string)))
542 (when (org-string-nw-p str)
543 (replace-regexp-in-string "\n" "\\\\\\\\\n" str))))
547 ;;; Transcode Functions
549 ;;;; Export Block
551 (defun org-koma-letter-export-block (export-block contents info)
552 "Transcode an EXPORT-BLOCK element into KOMA Scrlttr2 code.
553 CONTENTS is nil. INFO is a plist used as a communication
554 channel."
555 (when (member (org-element-property :type export-block) '("KOMA-LETTER" "LATEX"))
556 (org-remove-indentation (org-element-property :value export-block))))
558 ;;;; Export Snippet
560 (defun org-koma-letter-export-snippet (export-snippet contents info)
561 "Transcode an EXPORT-SNIPPET object into KOMA Scrlttr2 code.
562 CONTENTS is nil. INFO is a plist used as a communication
563 channel."
564 (when (memq (org-export-snippet-backend export-snippet) '(latex koma-letter))
565 (org-element-property :value export-snippet)))
567 ;;;; Keyword
569 (defun org-koma-letter-keyword (keyword contents info)
570 "Transcode a KEYWORD element into KOMA Scrlttr2 code.
571 CONTENTS is nil. INFO is a plist used as a communication
572 channel."
573 (let ((key (org-element-property :key keyword))
574 (value (org-element-property :value keyword)))
575 ;; Handle specifically KOMA-LETTER keywords. Otherwise, fallback
576 ;; to `latex' back-end.
577 (if (equal key "KOMA-LETTER") value
578 (org-export-with-backend 'latex keyword contents info))))
580 ;; Headline
582 (defun org-koma-letter-headline (headline contents info)
583 "Transcode a HEADLINE element from Org to LaTeX.
584 CONTENTS holds the contents of the headline. INFO is a plist
585 holding contextual information.
587 Note that if a headline is tagged with a tag from
588 `org-koma-letter-special-tags' it will not be exported, but
589 stored in `org-koma-letter-special-contents' and included at the
590 appropriate place."
591 (let ((special-tag (org-koma-letter--special-tag headline info)))
592 (if (not special-tag)
593 contents
594 (push (cons special-tag contents) org-koma-letter-special-contents)
595 "")))
597 (defun org-koma-letter--special-tag (headline info)
598 "Non-nil if HEADLINE is a special headline.
599 INFO is a plist holding contextual information. Return first
600 special tag headline."
601 (let ((special-tags (append
602 (plist-get info :special-tags-in-letter)
603 (plist-get info :special-tags-after-closing)
604 (plist-get info :special-tags-after-letter))))
605 (catch 'exit
606 (dolist (tag (org-export-get-tags headline info))
607 (let ((tag (assoc-string tag special-tags)))
608 (when tag (throw 'exit tag)))))))
610 (defun org-koma-letter--keyword-or-headline (plist-key pred info)
611 "Return the correct version of opening or closing.
612 PLIST-KEY should be a key in info, typically :opening
613 or :closing. PRED is a predicate run on headline to determine
614 which title to use which takes two arguments, a headline element
615 and an info plist. INFO is a plist holding contextual
616 information. Return the preferred candidate for the exported of
617 PLIST-KEY."
618 (let* ((keyword-candidate (plist-get info plist-key))
619 (headline-candidate (when (and (plist-get info :with-headline-opening)
620 (or (plist-get info :special-headings)
621 (not keyword-candidate)))
622 (org-element-map (plist-get info :parse-tree)
623 'headline
624 (lambda (head)
625 (when (funcall pred head info)
626 (org-element-property :title head)))
627 info t))))
628 (org-export-data (or headline-candidate keyword-candidate "") info)))
630 ;;;; Template
632 (defun org-koma-letter-template (contents info)
633 "Return complete document string after KOMA Scrlttr2 conversion.
634 CONTENTS is the transcoded contents string. INFO is a plist
635 holding export options."
636 (concat
637 ;; Time-stamp.
638 (and (plist-get info :time-stamp-file)
639 (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
640 ;; LaTeX compiler
641 (org-latex--insert-compiler info)
642 ;; Document class and packages.
643 (org-latex--make-preamble info)
644 ;; Settings. They can come from three locations, in increasing
645 ;; order of precedence: global variables, LCO files and in-buffer
646 ;; settings. Thus, we first insert settings coming from global
647 ;; variables, then we insert LCO files, and, eventually, we insert
648 ;; settings coming from buffer keywords.
649 (org-koma-letter--build-settings 'global info)
650 (mapconcat #'(lambda (file) (format "\\LoadLetterOption{%s}\n" file))
651 (org-split-string (or (plist-get info :lco) "") " ")
653 (org-koma-letter--build-settings 'buffer info)
654 ;; Date.
655 (format "\\date{%s}\n" (org-export-data (org-export-get-date info) info))
656 ;; Hyperref, document start, and subject and title.
657 (let* ((with-subject (plist-get info :with-subject))
658 (with-title (plist-get info :with-title))
659 (title-as-subject (and with-subject
660 (plist-get info :with-title-as-subject)))
661 (subject* (org-string-nw-p
662 (org-export-data (plist-get info :subject) info)))
663 (title* (and with-title
664 (org-string-nw-p
665 (org-export-data (plist-get info :title) info))))
666 (subject (cond ((not with-subject) nil)
667 (title-as-subject (or subject* title*))
668 (t subject*)))
669 (title (cond ((not with-title) nil)
670 (title-as-subject (and subject* title*))
671 (t title*)))
672 (hyperref-template (plist-get info :latex-hyperref-template))
673 (spec (append (list (cons ?t (or title subject "")))
674 (org-latex--format-spec info))))
675 (concat
676 (when (and with-subject (not (eq with-subject t)))
677 (format "\\KOMAoption{subject}{%s}\n"
678 (if (symbolp with-subject) with-subject
679 (mapconcat #'symbol-name with-subject ","))))
680 ;; Hyperref.
681 (format-spec hyperref-template spec)
682 ;; Document start.
683 "\\begin{document}\n\n"
684 ;; Subject and title.
685 (when subject (format "\\setkomavar{subject}{%s}\n" subject))
686 (when title (format "\\setkomavar{title}{%s}\n" title))
687 (when (or (org-string-nw-p title) (org-string-nw-p subject)) "\n")))
688 ;; Letter start.
689 (let ((keyword-val (plist-get info :to-address))
690 (heading-val (org-koma-letter--get-tagged-contents 'to)))
691 (format "\\begin{letter}{%%\n%s}\n\n"
692 (org-koma-letter--add-latex-newlines
693 (or (if (plist-get info :special-headings)
694 (or heading-val keyword-val)
695 (or keyword-val heading-val))
696 "\\\\mbox{}"))))
697 ;; Opening.
698 (format "\\opening{%s}\n\n"
699 (org-koma-letter--keyword-or-headline
700 :opening (lambda (h i) (not (org-koma-letter--special-tag h i)))
701 info))
702 ;; Letter body.
703 contents
704 ;; Closing.
705 (format "\\closing{%s}\n"
706 (org-koma-letter--keyword-or-headline
707 :closing (lambda (h i) (eq (org-koma-letter--special-tag h i)
708 'closing))
709 info))
710 (org-koma-letter--special-contents-inline
711 (plist-get info :special-tags-after-closing) info)
712 ;; Letter end.
713 "\n\\end{letter}\n"
714 (org-koma-letter--special-contents-inline
715 (plist-get info :special-tags-after-letter) info)
716 ;; Document end.
717 "\n\\end{document}"))
719 (defun org-koma-letter--build-settings (scope info)
720 "Build settings string according to type.
721 SCOPE is either `global' or `buffer'. INFO is a plist used as
722 a communication channel."
723 (let* ((check-scope
724 (function
725 ;; Non-nil value when SETTING was defined in SCOPE.
726 (lambda (setting)
727 (let ((property (intern (format ":inbuffer-%s" setting))))
728 (if (eq scope 'global)
729 (eq (plist-get info property) 'koma-letter:empty)
730 (not (eq (plist-get info property) 'koma-letter:empty)))))))
731 (heading-or-key-value
732 (function
733 (lambda (heading key &optional scoped)
734 (let* ((heading-val
735 (org-koma-letter--get-tagged-contents heading))
736 (key-val (org-string-nw-p (plist-get info key)))
737 (scopedp (funcall check-scope (or scoped heading))))
738 (and (or (and key-val scopedp) heading-val)
739 (not (and (eq scope 'global) heading-val))
740 (if scopedp key-val heading-val)))))))
741 (concat
742 ;; Name.
743 (let ((author (plist-get info :author)))
744 (and author
745 (funcall check-scope 'author)
746 (format "\\setkomavar{fromname}{%s}\n"
747 (org-export-data author info))))
748 ;; From.
749 (let ((from (funcall heading-or-key-value 'from :from-address)))
750 (and from
751 (format "\\setkomavar{fromaddress}{%s}\n"
752 (org-koma-letter--add-latex-newlines from))))
753 ;; Email.
754 (let ((email (plist-get info :email)))
755 (and email
756 (funcall check-scope 'email)
757 (format "\\setkomavar{fromemail}{%s}\n" email)))
758 (and (funcall check-scope 'with-email)
759 (format "\\KOMAoption{fromemail}{%s}\n"
760 (if (plist-get info :with-email) "true" "false")))
761 ;; Phone number.
762 (let ((phone-number (plist-get info :phone-number)))
763 (and (org-string-nw-p phone-number)
764 (funcall check-scope 'phone-number)
765 (format "\\setkomavar{fromphone}{%s}\n" phone-number)))
766 (and (funcall check-scope 'with-phone)
767 (format "\\KOMAoption{fromphone}{%s}\n"
768 (if (plist-get info :with-phone) "true" "false")))
769 ;; Signature.
770 (let* ((head-opening (plist-get info :with-headline-opening))
771 (signature (funcall heading-or-key-value
772 (if head-opening 'closing nil)
773 :signature
774 (if head-opening 'signature nil))))
775 (and signature
776 (format "\\setkomavar{signature}{%s}\n" signature)))
777 ;; Back address.
778 (and (funcall check-scope 'with-backaddress)
779 (format "\\KOMAoption{backaddress}{%s}\n"
780 (if (plist-get info :with-backaddress) "true" "false")))
781 ;; Place.
782 (let ((place-scoped (funcall check-scope 'with-place))
783 (place-set (funcall check-scope 'place)))
784 (when (or (and place-scoped place-set)
785 (and (eq scope 'buffer)
786 (or place-scoped place-set)))
787 (format "\\setkomavar{place}{%s}\n"
788 (if (plist-get info :with-place) (plist-get info :place)
789 ""))))
790 ;; Location.
791 (let ((heading-val
792 (org-koma-letter--add-latex-newlines
793 (org-export-data
794 (org-koma-letter--get-tagged-contents 'location)
795 info)))
796 (with-location-set (funcall check-scope 'with-location))
797 (location-set (funcall check-scope 'location))
798 (location (plist-get info :location)))
799 (when (or (and with-location-set (or location-set heading-val))
800 (and (eq scope 'buffer) (or with-location-set location-set heading-val)))
801 (format "\\setkomavar{location}{%s}\n"
802 (if (plist-get info :with-location)
803 (if (plist-get info :special-headings) (or heading-val location "")
804 (or heading-val location ""))
805 ""))))
806 ;; Folding marks.
807 (and (funcall check-scope 'with-foldmarks)
808 (let ((foldmarks (plist-get info :with-foldmarks)))
809 (cond ((consp foldmarks)
810 (format "\\KOMAoptions{foldmarks=true,foldmarks=%s}\n"
811 (mapconcat #'symbol-name foldmarks "")))
812 (foldmarks "\\KOMAoptions{foldmarks=true}\n")
813 (t "\\KOMAoptions{foldmarks=false}\n")))))))
817 ;;; Commands
819 ;;;###autoload
820 (defun org-koma-letter-export-as-latex
821 (&optional async subtreep visible-only body-only ext-plist)
822 "Export current buffer as a KOMA Scrlttr2 letter.
824 If narrowing is active in the current buffer, only export its
825 narrowed part.
827 If a region is active, export that region.
829 A non-nil optional argument ASYNC means the process should happen
830 asynchronously. The resulting buffer should be accessible
831 through the `org-export-stack' interface.
833 When optional argument SUBTREEP is non-nil, export the sub-tree
834 at point, extracting information from the headline properties
835 first.
837 When optional argument VISIBLE-ONLY is non-nil, don't export
838 contents of hidden elements.
840 When optional argument BODY-ONLY is non-nil, only write code
841 between \"\\begin{letter}\" and \"\\end{letter}\".
843 EXT-PLIST, when provided, is a proeprty list with external
844 parameters overriding Org default settings, but still inferior to
845 file-local settings.
847 Export is done in a buffer named \"*Org KOMA-LETTER Export*\". It
848 will be displayed if `org-export-show-temporary-export-buffer' is
849 non-nil."
850 (interactive)
851 (let (org-koma-letter-special-contents)
852 (org-export-to-buffer 'koma-letter "*Org KOMA-LETTER Export*"
853 async subtreep visible-only body-only ext-plist
854 (lambda () (LaTeX-mode)))))
856 ;;;###autoload
857 (defun org-koma-letter-export-to-latex
858 (&optional async subtreep visible-only body-only ext-plist)
859 "Export current buffer as a KOMA Scrlttr2 letter (tex).
861 If narrowing is active in the current buffer, only export its
862 narrowed part.
864 If a region is active, export that region.
866 A non-nil optional argument ASYNC means the process should happen
867 asynchronously. The resulting file should be accessible through
868 the `org-export-stack' interface.
870 When optional argument SUBTREEP is non-nil, export the sub-tree
871 at point, extracting information from the headline properties
872 first.
874 When optional argument VISIBLE-ONLY is non-nil, don't export
875 contents of hidden elements.
877 When optional argument BODY-ONLY is non-nil, only write code
878 between \"\\begin{letter}\" and \"\\end{letter}\".
880 EXT-PLIST, when provided, is a property list with external
881 parameters overriding Org default settings, but still inferior to
882 file-local settings.
884 When optional argument PUB-DIR is set, use it as the publishing
885 directory.
887 Return output file's name."
888 (interactive)
889 (let ((outfile (org-export-output-file-name ".tex" subtreep))
890 (org-koma-letter-special-contents))
891 (org-export-to-file 'koma-letter outfile
892 async subtreep visible-only body-only ext-plist)))
894 ;;;###autoload
895 (defun org-koma-letter-export-to-pdf
896 (&optional async subtreep visible-only body-only ext-plist)
897 "Export current buffer as a KOMA Scrlttr2 letter (pdf).
899 If narrowing is active in the current buffer, only export its
900 narrowed part.
902 If a region is active, export that region.
904 A non-nil optional argument ASYNC means the process should happen
905 asynchronously. The resulting file should be accessible through
906 the `org-export-stack' interface.
908 When optional argument SUBTREEP is non-nil, export the sub-tree
909 at point, extracting information from the headline properties
910 first.
912 When optional argument VISIBLE-ONLY is non-nil, don't export
913 contents of hidden elements.
915 When optional argument BODY-ONLY is non-nil, only write code
916 between \"\\begin{letter}\" and \"\\end{letter}\".
918 EXT-PLIST, when provided, is a property list with external
919 parameters overriding Org default settings, but still inferior to
920 file-local settings.
922 Return PDF file's name."
923 (interactive)
924 (let ((file (org-export-output-file-name ".tex" subtreep))
925 (org-koma-letter-special-contents))
926 (org-export-to-file 'koma-letter file
927 async subtreep visible-only body-only ext-plist
928 (lambda (file) (org-latex-compile file)))))
931 (provide 'ox-koma-letter)
932 ;;; ox-koma-letter.el ends here