Add org-link-edit.el to contrib
[org-mode.git] / contrib / lisp / ox-koma-letter.el
blob8ba380c836558ccfac7055534d22f8e0773c106a
1 ;;; ox-koma-letter.el --- KOMA Scrlttr2 Back-End for Org Export Engine
3 ;; Copyright (C) 2007-2015 Free Software Foundation, Inc.
5 ;; Author: Nicolas Goaziou <n.goaziou AT gmail DOT com>
6 ;; Alan Schmitt <alan.schmitt AT polytechnique DOT org>
7 ;; Viktor Rosenfeld <listuser36 AT gmail DOT com>
8 ;; Rasmus Pank Roulund <emacs AT pank DOT eu>
9 ;; Keywords: org, wp, tex
11 ;; This program is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; This program is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; This library implements a KOMA Scrlttr2 back-end, derived from the
27 ;; LaTeX one.
29 ;; Depending on the desired output format, three commands are provided
30 ;; for export: `org-koma-letter-export-as-latex' (temporary buffer),
31 ;; `org-koma-letter-export-to-latex' ("tex" file) and
32 ;; `org-koma-letter-export-to-pdf' ("pdf" file).
34 ;; On top of buffer keywords supported by `latex' back-end (see
35 ;; `org-latex-options-alist'), this back-end introduces the following
36 ;; keywords:
37 ;; - CLOSING: see `org-koma-letter-closing',
38 ;; - FROM_ADDRESS: see `org-koma-letter-from-address',
39 ;; - LCO: see `org-koma-letter-class-option-file',
40 ;; - OPENING: see `org-koma-letter-opening',
41 ;; - PHONE_NUMBER: see `org-koma-letter-phone-number',
42 ;; - SIGNATURE: see `org-koma-letter-signature',
43 ;; - PLACE: see `org-koma-letter-place',
44 ;; - TO_ADDRESS: If unspecified this is set to "\mbox{}".
46 ;; TO_ADDRESS and FROM_ADDRESS can also be specified using heading
47 ;; with the special tags specified in
48 ;; `org-koma-letter-special-tags-in-letter', namely "to" and "from".
49 ;; LaTeX line breaks are not necessary if using these headings. If
50 ;; both a headline and a keyword specify a to or from address the
51 ;; value is determined in accordance with
52 ;; `org-koma-letter-prefer-special-headings'.
54 ;; A number of OPTIONS settings can be set to change which contents is
55 ;; exported.
56 ;; - backaddress (see `org-koma-letter-use-backaddress')
57 ;; - foldmarks (see `org-koma-letter-use-foldmarks')
58 ;; - phone (see `org-koma-letter-use-phone')
59 ;; - email (see `org-koma-letter-use-email')
60 ;; - place (see `org-koma-letter-use-place')
61 ;; - subject, a list of format options
62 ;; (see `org-koma-letter-subject-format')
63 ;; - after-closing-order, a list of the ordering of headings with
64 ;; special tags after closing (see
65 ;; `org-koma-letter-special-tags-after-closing')
66 ;; - after-letter-order, as above, but after the end of the letter
67 ;; (see `org-koma-letter-special-tags-after-letter').
69 ;; The following variables works differently from the main LaTeX class
70 ;; - AUTHOR: Default to user-full-name but may be disabled.
71 ;; (See also `org-koma-letter-author'),
72 ;; - EMAIL: Same as AUTHOR. (see also `org-koma-letter-email'),
74 ;; Headlines are in general ignored. However, headlines with special
75 ;; tags can be used for specified contents like postscript (ps),
76 ;; carbon copy (cc), enclosures (encl) and code to be inserted after
77 ;; \end{letter} (after_letter). Specials tags are defined in
78 ;; `org-koma-letter-special-tags-after-closing' and
79 ;; `org-koma-letter-special-tags-after-letter'. Currently members of
80 ;; `org-koma-letter-special-tags-after-closing' used as macros and the
81 ;; content of the headline is the argument.
83 ;; Headlines with two and from may also be used rather than the
84 ;; keyword approach described above. If both a keyword and a headline
85 ;; with information is present precedence is determined by
86 ;; `org-koma-letter-prefer-special-headings'.
88 ;; You need an appropriate association in `org-latex-classes' in order
89 ;; to use the KOMA Scrlttr2 class. By default, a sparse scrlttr2
90 ;; class is provided: "default-koma-letter". You can also add you own
91 ;; letter class. For instance:
93 ;; (add-to-list 'org-latex-classes
94 ;; '("my-letter"
95 ;; "\\documentclass\[%
96 ;; DIV=14,
97 ;; fontsize=12pt,
98 ;; parskip=half,
99 ;; subject=titled,
100 ;; backaddress=false,
101 ;; fromalign=left,
102 ;; fromemail=true,
103 ;; fromphone=true\]\{scrlttr2\}
104 ;; \[DEFAULT-PACKAGES]
105 ;; \[PACKAGES]
106 ;; \[EXTRA]"))
108 ;; Then, in your Org document, be sure to require the proper class
109 ;; with:
111 ;; #+LATEX_CLASS: my-letter
113 ;; Or by setting `org-koma-letter-default-class'.
115 ;; You may have to load (LaTeX) Babel as well, e.g., by adding
116 ;; it to `org-latex-packages-alist',
118 ;; (add-to-list 'org-latex-packages-alist '("AUTO" "babel" nil))
120 ;;; Code:
122 (require 'ox-latex)
124 ;; Install a default letter class.
125 (unless (assoc "default-koma-letter" org-latex-classes)
126 (add-to-list 'org-latex-classes
127 '("default-koma-letter" "\\documentclass[11pt]{scrlttr2}")))
130 ;;; User-Configurable Variables
132 (defgroup org-export-koma-letter nil
133 "Options for exporting to KOMA scrlttr2 class in LaTeX export."
134 :tag "Org Koma-Letter"
135 :group 'org-export)
137 (defcustom org-koma-letter-class-option-file "NF"
138 "Letter Class Option File.
139 This option can also be set with the LCO keyword."
140 :group 'org-export-koma-letter
141 :type 'string)
143 (defcustom org-koma-letter-author 'user-full-name
144 "Sender's name.
146 This variable defaults to calling the function `user-full-name'
147 which just returns the current function `user-full-name'.
148 Alternatively a string, nil or a function may be given.
149 Functions must return a string.
151 This option can also be set with the AUTHOR keyword."
152 :group 'org-export-koma-letter
153 :type '(radio (function-item user-full-name)
154 (string)
155 (function)
156 (const :tag "Do not export author" nil)))
158 (defcustom org-koma-letter-email 'org-koma-letter-email
159 "Sender's email address.
161 This variable defaults to the value `org-koma-letter-email' which
162 returns `user-mail-address'. Alternatively a string, nil or
163 a function may be given. Functions must return a string.
165 This option can also be set with the EMAIL keyword."
166 :group 'org-export-koma-letter
167 :type '(radio (function-item org-koma-letter-email)
168 (string)
169 (function)
170 (const :tag "Do not export email" nil)))
172 (defcustom org-koma-letter-from-address ""
173 "Sender's address, as a string.
174 This option can also be set with one or more FROM_ADDRESS
175 keywords."
176 :group 'org-export-koma-letter
177 :type 'string)
179 (defcustom org-koma-letter-phone-number ""
180 "Sender's phone number, as a string.
181 This option can also be set with the PHONE_NUMBER keyword."
182 :group 'org-export-koma-letter
183 :type 'string)
185 (defcustom org-koma-letter-place ""
186 "Place from which the letter is sent, as a string.
187 This option can also be set with the PLACE keyword."
188 :group 'org-export-koma-letter
189 :type 'string)
191 (defcustom org-koma-letter-opening ""
192 "Letter's opening, as a string.
194 This option can also be set with the OPENING keyword. Moreover,
195 when:
196 (1) this value is the empty string;
197 (2) there's no OPENING keyword or it is empty;
198 (3) `org-koma-letter-headline-is-opening-maybe' is non-nil;
199 (4) the letter contains a headline without a special
200 tag (e.g. \"to\" or \"ps\");
201 then the opening will be implicitly set as the headline title."
202 :group 'org-export-koma-letter
203 :type 'string)
205 (defcustom org-koma-letter-closing ""
206 "Letter's closing, as a string.
207 This option can also be set with the CLOSING keyword. Moreover,
208 when:
209 (1) there's no CLOSING keyword or it is empty;
210 (2) `org-koma-letter-headline-is-opening-maybe' is non-nil;
211 (3) the letter contains a headline with the special
212 tag closing;
213 then the opening will be set as the title of the closing special
214 heading."
215 :group 'org-export-koma-letter
216 :type 'string)
218 (defcustom org-koma-letter-signature ""
219 "Signature, as a string.
220 This option can also be set with the SIGNATURE keyword.
221 Moreover, when:
222 (1) there's no CLOSING keyword or it is empty;
223 (2) `org-koma-letter-headline-is-opening-maybe' is non-nil;
224 (3) the letter contains a headline with the special
225 tag closing;
226 then the signature will be set as the content of the
227 closing special heading."
228 :group 'org-export-koma-letter
229 :type 'string)
231 (defcustom org-koma-letter-prefer-special-headings nil
232 "Non-nil means prefer headlines over keywords for TO and FROM.
233 This option can also be set with the OPTIONS keyword, e.g.:
234 \"special-headings:t\"."
235 :group 'org-export-koma-letter
236 :type 'boolean)
238 (defcustom org-koma-letter-subject-format t
239 "Non-nil means include the subject.
241 Support formatting options.
243 When t, insert a subject using default options. When nil, do not
244 insert a subject at all. It can also be a list of symbols among
245 the following ones:
247 `afteropening' Subject after opening
248 `beforeopening' Subject before opening
249 `centered' Subject centered
250 `left' Subject left-justified
251 `right' Subject right-justified
252 `titled' Add title/description to subject
253 `underlined' Set subject underlined
254 `untitled' Do not add title/description to subject
256 Please refer to the KOMA-script manual (Table 4.16. in the
257 English manual of 2012-07-22).
259 This option can also be set with the OPTIONS keyword, e.g.:
260 \"subject:(underlined centered)\"."
261 :type
262 '(choice
263 (const :tag "No export" nil)
264 (const :tag "Default options" t)
265 (set :tag "Configure options"
266 (const :tag "Subject after opening" afteropening)
267 (const :tag "Subject before opening" beforeopening)
268 (const :tag "Subject centered" centered)
269 (const :tag "Subject left-justified" left)
270 (const :tag "Subject right-justified" right)
271 (const :tag "Add title or description to subject" underlined)
272 (const :tag "Set subject underlined" titled)
273 (const :tag "Do not add title or description to subject" untitled)))
274 :group 'org-export-koma-letter)
276 (defcustom org-koma-letter-use-backaddress nil
277 "Non-nil prints return address in line above to address.
278 This option can also be set with the OPTIONS keyword, e.g.:
279 \"backaddress:t\"."
280 :group 'org-export-koma-letter
281 :type 'boolean)
283 (defcustom org-koma-letter-use-foldmarks t
284 "Configure appearance of folding marks.
286 When t, activate default folding marks. When nil, do not insert
287 folding marks at all. It can also be a list of symbols among the
288 following ones:
290 `B' Activate upper horizontal mark on left paper edge
291 `b' Deactivate upper horizontal mark on left paper edge
293 `H' Activate all horizontal marks on left paper edge
294 `h' Deactivate all horizontal marks on left paper edge
296 `L' Activate left vertical mark on upper paper edge
297 `l' Deactivate left vertical mark on upper paper edge
299 `M' Activate middle horizontal mark on left paper edge
300 `m' Deactivate middle horizontal mark on left paper edge
302 `P' Activate punch or center mark on left paper edge
303 `p' Deactivate punch or center mark on left paper edge
305 `T' Activate lower horizontal mark on left paper edge
306 `t' Deactivate lower horizontal mark on left paper edge
308 `V' Activate all vertical marks on upper paper edge
309 `v' Deactivate all vertical marks on upper paper edge
311 This option can also be set with the OPTIONS keyword, e.g.:
312 \"foldmarks:(b l m t)\"."
313 :group 'org-export-koma-letter
314 :type '(choice
315 (const :tag "Activate default folding marks" t)
316 (const :tag "Deactivate folding marks" nil)
317 (set
318 :tag "Configure folding marks"
319 (const :tag "Activate upper horizontal mark on left paper edge" B)
320 (const :tag "Deactivate upper horizontal mark on left paper edge" b)
321 (const :tag "Activate all horizontal marks on left paper edge" H)
322 (const :tag "Deactivate all horizontal marks on left paper edge" h)
323 (const :tag "Activate left vertical mark on upper paper edge" L)
324 (const :tag "Deactivate left vertical mark on upper paper edge" l)
325 (const :tag "Activate middle horizontal mark on left paper edge" M)
326 (const :tag "Deactivate middle horizontal mark on left paper edge" m)
327 (const :tag "Activate punch or center mark on left paper edge" P)
328 (const :tag "Deactivate punch or center mark on left paper edge" p)
329 (const :tag "Activate lower horizontal mark on left paper edge" T)
330 (const :tag "Deactivate lower horizontal mark on left paper edge" t)
331 (const :tag "Activate all vertical marks on upper paper edge" V)
332 (const :tag "Deactivate all vertical marks on upper paper edge" v))))
334 (defcustom org-koma-letter-use-phone nil
335 "Non-nil prints sender's phone number.
336 This option can also be set with the OPTIONS keyword, e.g.:
337 \"phone:t\"."
338 :group 'org-export-koma-letter
339 :type 'boolean)
341 (defcustom org-koma-letter-use-email nil
342 "Non-nil prints sender's email address.
343 This option can also be set with the OPTIONS keyword, e.g.:
344 \"email:t\"."
345 :group 'org-export-koma-letter
346 :type 'boolean)
348 (defcustom org-koma-letter-use-place t
349 "Non-nil prints the letter's place next to the date.
350 This option can also be set with the OPTIONS keyword, e.g.:
351 \"place:nil\"."
352 :group 'org-export-koma-letter
353 :type 'boolean)
355 (defcustom org-koma-letter-default-class "default-koma-letter"
356 "Default class for `org-koma-letter'.
357 The value must be a member of `org-latex-classes'."
358 :group 'org-export-koma-letter
359 :type 'string)
361 (defcustom org-koma-letter-headline-is-opening-maybe t
362 "Non-nil means a headline may be used as an opening.
363 A headline is only used if #+OPENING is not set. See also
364 `org-koma-letter-opening'."
365 :group 'org-export-koma-letter
366 :type 'boolean)
368 (defcustom org-koma-letter-prefer-subject nil
369 "Non-nil means title should be interpreted as subject if subject is missing.
370 This option can also be set with the OPTIONS keyword,
371 e.g. \"title-subject:t\"."
372 :group 'org-export-koma-letter
373 :type 'boolean)
375 (defconst org-koma-letter-special-tags-in-letter '(to from closing)
376 "Header tags related to the letter itself.")
378 (defconst org-koma-letter-special-tags-after-closing '(ps encl cc)
379 "Header tags to be inserted after closing.")
381 (defconst org-koma-letter-special-tags-after-letter '(after_letter)
382 "Header tags to be inserted after closing.")
384 (defvar org-koma-letter-special-contents nil
385 "Holds special content temporarily.")
387 (make-obsolete-variable 'org-koma-letter-use-title
388 'org-export-with-title
389 "25.1" 'set)
392 ;;; Define Back-End
394 (org-export-define-derived-backend 'koma-letter 'latex
395 :options-alist
396 '((:latex-class "LATEX_CLASS" nil org-koma-letter-default-class t)
397 (:lco "LCO" nil org-koma-letter-class-option-file)
398 (:author "AUTHOR" nil (org-koma-letter--get-value org-koma-letter-author) parse)
399 (:author-changed-in-buffer-p "AUTHOR" nil nil t)
400 (:from-address "FROM_ADDRESS" nil org-koma-letter-from-address newline)
401 (:phone-number "PHONE_NUMBER" nil org-koma-letter-phone-number)
402 (:email "EMAIL" nil (org-koma-letter--get-value org-koma-letter-email) t)
403 (:to-address "TO_ADDRESS" nil nil newline)
404 (:place "PLACE" nil org-koma-letter-place)
405 (:subject "SUBJECT" nil nil parse)
406 (:opening "OPENING" nil org-koma-letter-opening parse)
407 (:closing "CLOSING" nil org-koma-letter-closing parse)
408 (:signature "SIGNATURE" nil org-koma-letter-signature newline)
409 (:special-headings nil "special-headings"
410 org-koma-letter-prefer-special-headings)
411 (:special-tags nil nil (append
412 org-koma-letter-special-tags-in-letter
413 org-koma-letter-special-tags-after-closing
414 org-koma-letter-special-tags-after-letter))
415 (:with-after-closing nil "after-closing-order"
416 org-koma-letter-special-tags-after-closing)
417 (:with-after-letter nil "after-letter-order"
418 org-koma-letter-special-tags-after-letter)
419 (:with-backaddress nil "backaddress" org-koma-letter-use-backaddress)
420 (:with-email nil "email" org-koma-letter-use-email)
421 (:with-foldmarks nil "foldmarks" org-koma-letter-use-foldmarks)
422 (:with-phone nil "phone" org-koma-letter-use-phone)
423 (:with-place nil "place" org-koma-letter-use-place)
424 (:with-subject nil "subject" org-koma-letter-subject-format)
425 (:with-title-as-subject nil "title-subject" org-koma-letter-prefer-subject)
426 (:with-headline-opening nil nil org-koma-letter-headline-is-opening-maybe)
427 ;; Special properties non-nil when a setting happened in buffer.
428 ;; They are used to prioritize in-buffer settings over "lco"
429 ;; files. See `org-koma-letter-template'.
430 (:inbuffer-author "AUTHOR" nil 'koma-letter:empty)
431 (:inbuffer-email "EMAIL" nil 'koma-letter:empty)
432 (:inbuffer-phone-number "PHONE_NUMBER" nil 'koma-letter:empty)
433 (:inbuffer-place "PLACE" nil 'koma-letter:empty)
434 (:inbuffer-signature "SIGNATURE" nil 'koma-letter:empty)
435 (:inbuffer-with-backaddress nil "backaddress" 'koma-letter:empty)
436 (:inbuffer-with-email nil "email" 'koma-letter:empty)
437 (:inbuffer-with-foldmarks nil "foldmarks" 'koma-letter:empty)
438 (:inbuffer-with-phone nil "phone" 'koma-letter:empty))
439 :translate-alist '((export-block . org-koma-letter-export-block)
440 (export-snippet . org-koma-letter-export-snippet)
441 (headline . org-koma-letter-headline)
442 (keyword . org-koma-letter-keyword)
443 (template . org-koma-letter-template))
444 :menu-entry
445 '(?k "Export with KOMA Scrlttr2"
446 ((?L "As LaTeX buffer" org-koma-letter-export-as-latex)
447 (?l "As LaTeX file" org-koma-letter-export-to-latex)
448 (?p "As PDF file" org-koma-letter-export-to-pdf)
449 (?o "As PDF file and open"
450 (lambda (a s v b)
451 (if a (org-koma-letter-export-to-pdf t s v b)
452 (org-open-file (org-koma-letter-export-to-pdf nil s v b))))))))
456 ;;; Helper functions
458 (defun org-koma-letter-email ()
459 "Return the current `user-mail-address'."
460 user-mail-address)
462 ;; The following is taken from/inspired by ox-grof.el
463 ;; Thanks, Luis!
465 (defun org-koma-letter--get-tagged-contents (key)
466 "Get contents from a headline tagged with KEY.
467 The contents is stored in `org-koma-letter-special-contents'."
468 (cdr (assoc-string (org-koma-letter--get-value key)
469 org-koma-letter-special-contents)))
471 (defun org-koma-letter--get-value (value)
472 "Turn value into a string whenever possible.
473 Determines if VALUE is nil, a string, a function or a symbol and
474 return a string or nil."
475 (when value
476 (cond ((stringp value) value)
477 ((functionp value) (funcall value))
478 ((symbolp value) (symbol-name value))
479 (t value))))
481 (defun org-koma-letter--special-contents-as-macro
482 (keywords &optional keep-newlines no-tag)
483 "Process KEYWORDS members of `org-koma-letter-special-contents'.
484 KEYWORDS is a list of symbols. Return them as a string to be
485 formatted.
487 The function is used for inserting content of special headings
488 such as PS.
490 If KEEP-NEWLINES is non-nil leading and trailing newlines are not
491 removed. If NO-TAG is non-nil the content in
492 `org-koma-letter-special-contents' are not wrapped in a macro
493 named whatever the members of KEYWORDS are called."
494 (mapconcat
495 (lambda (keyword)
496 (let* ((name (org-koma-letter--get-value keyword))
497 (value (org-koma-letter--get-tagged-contents name)))
498 (cond ((not value) nil)
499 (no-tag (if keep-newlines value (org-trim value)))
500 (t (format "\\%s{%s}\n"
501 name
502 (if keep-newlines value (org-trim value)))))))
503 keywords
504 ""))
506 (defun org-koma-letter--determine-to-and-from (info key)
507 "Given INFO determine KEY for the letter.
508 KEY should be `to' or `from'.
510 `ox-koma-letter' allows two ways to specify TO and FROM. If both
511 are present return the preferred one as determined by
512 `org-koma-letter-prefer-special-headings'."
513 (let ((option (org-string-nw-p
514 (plist-get info (if (eq key 'to) :to-address :from-address))))
515 (headline (org-koma-letter--get-tagged-contents key)))
516 (replace-regexp-in-string
517 "\n" "\\\\\\\\\n"
518 (org-trim
519 (if (plist-get info :special-headings) (or headline option "")
520 (or option headline ""))))))
524 ;;; Transcode Functions
526 ;;;; Export Block
528 (defun org-koma-letter-export-block (export-block contents info)
529 "Transcode an EXPORT-BLOCK element into KOMA Scrlttr2 code.
530 CONTENTS is nil. INFO is a plist used as a communication
531 channel."
532 (when (member (org-element-property :type export-block) '("KOMA-LETTER" "LATEX"))
533 (org-remove-indentation (org-element-property :value export-block))))
535 ;;;; Export Snippet
537 (defun org-koma-letter-export-snippet (export-snippet contents info)
538 "Transcode an EXPORT-SNIPPET object into KOMA Scrlttr2 code.
539 CONTENTS is nil. INFO is a plist used as a communication
540 channel."
541 (when (memq (org-export-snippet-backend export-snippet) '(latex koma-letter))
542 (org-element-property :value export-snippet)))
544 ;;;; Keyword
546 (defun org-koma-letter-keyword (keyword contents info)
547 "Transcode a KEYWORD element into KOMA Scrlttr2 code.
548 CONTENTS is nil. INFO is a plist used as a communication
549 channel."
550 (let ((key (org-element-property :key keyword))
551 (value (org-element-property :value keyword)))
552 ;; Handle specifically KOMA-LETTER keywords. Otherwise, fallback
553 ;; to `latex' back-end.
554 (if (equal key "KOMA-LETTER") value
555 (org-export-with-backend 'latex keyword contents info))))
557 ;; Headline
559 (defun org-koma-letter-headline (headline contents info)
560 "Transcode a HEADLINE element from Org to LaTeX.
561 CONTENTS holds the contents of the headline. INFO is a plist
562 holding contextual information.
564 Note that if a headline is tagged with a tag from
565 `org-koma-letter-special-tags' it will not be exported, but
566 stored in `org-koma-letter-special-contents' and included at the
567 appropriate place."
568 (let ((special-tag (org-koma-letter--special-tag headline info)))
569 (if (not special-tag)
570 contents
571 (push (cons special-tag contents) org-koma-letter-special-contents)
572 "")))
574 (defun org-koma-letter--special-tag (headline info)
575 "Non-nil if HEADLINE is a special headline.
576 INFO is a plist holding contextual information. Return first
577 special tag headline."
578 (let ((special-tags (plist-get info :special-tags)))
579 (catch 'exit
580 (dolist (tag (org-export-get-tags headline info))
581 (let ((tag (assoc-string tag special-tags)))
582 (when tag (throw 'exit tag)))))))
584 ;;;; Template
586 (defun org-koma-letter-template (contents info)
587 "Return complete document string after KOMA Scrlttr2 conversion.
588 CONTENTS is the transcoded contents string. INFO is a plist
589 holding export options."
590 (concat
591 ;; Time-stamp.
592 (and (plist-get info :time-stamp-file)
593 (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
594 ;; Document class and packages.
595 (org-latex--make-header info)
596 ;; Settings. They can come from three locations, in increasing
597 ;; order of precedence: global variables, LCO files and in-buffer
598 ;; settings. Thus, we first insert settings coming from global
599 ;; variables, then we insert LCO files, and, eventually, we insert
600 ;; settings coming from buffer keywords.
601 (org-koma-letter--build-settings 'global info)
602 (mapconcat #'(lambda (file) (format "\\LoadLetterOption{%s}\n" file))
603 (org-split-string (or (plist-get info :lco) "") " ")
605 (org-koma-letter--build-settings 'buffer info)
606 ;; From address.
607 (let ((from-address (org-koma-letter--determine-to-and-from info 'from)))
608 (when (org-string-nw-p from-address)
609 (format "\\setkomavar{fromaddress}{%s}\n" from-address)))
610 ;; Date.
611 (format "\\date{%s}\n" (org-export-data (org-export-get-date info) info))
612 ;; Hyperref, document start, and subject and title.
613 (let* ((with-subject (plist-get info :with-subject))
614 (with-title (plist-get info :with-title))
615 (title-as-subject (and with-subject
616 (plist-get info :with-title-as-subject)))
617 (subject* (org-string-nw-p
618 (org-export-data (plist-get info :subject) info)))
619 (title* (and with-title
620 (org-string-nw-p
621 (org-export-data (plist-get info :title) info))))
622 (subject (cond ((not with-subject) nil)
623 (title-as-subject (or subject* title*))
624 (t subject*)))
625 (title (cond ((not with-title) nil)
626 (title-as-subject (and subject* title*))
627 (t title*)))
628 (hyperref-template (plist-get info :latex-hyperref-template))
629 (spec (append (list (cons ?t (or title subject "")))
630 (org-latex--format-spec info))))
631 (concat
632 (when (and with-subject (not (eq with-subject t)))
633 (format "\\KOMAoption{subject}{%s}\n"
634 (if (symbolp with-subject) with-subject
635 (mapconcat #'symbol-name with-subject ","))))
636 ;; Hyperref.
637 (format-spec hyperref-template spec)
638 ;; Document start.
639 "\\begin{document}\n\n"
640 ;; Subject and title.
641 (when subject (format "\\setkomavar{subject}{%s}\n" subject))
642 (when title (format "\\setkomavar{title}{%s}\n" title))
643 (when (or (org-string-nw-p title) (org-string-nw-p subject)) "\n")))
644 ;; Letter start.
645 (format "\\begin{letter}{%%\n%s}\n\n"
646 (org-koma-letter--determine-to-and-from info 'to))
647 ;; Opening.
648 (format "\\opening{%s}\n\n"
649 (org-export-data
650 (or (org-string-nw-p (plist-get info :opening))
651 (when (plist-get info :with-headline-opening)
652 (org-element-map (plist-get info :parse-tree) 'headline
653 (lambda (head)
654 (unless (org-koma-letter--special-tag head info)
655 (org-element-property :title head)))
656 info t))
658 info))
659 ;; Letter body.
660 contents
661 ;; Closing.
662 (format "\n\\closing{%s}\n"
663 (org-export-data
664 (or (org-string-nw-p (plist-get info :closing))
665 (when (plist-get info :with-headline-opening)
666 (org-element-map (plist-get info :parse-tree) 'headline
667 (lambda (head)
668 (when (eq (org-koma-letter--special-tag head info)
669 'closing)
670 (org-element-property :title head)))
671 info t)))
672 info))
673 (org-koma-letter--special-contents-as-macro
674 (plist-get info :with-after-closing))
675 ;; Letter end.
676 "\n\\end{letter}\n"
677 (org-koma-letter--special-contents-as-macro
678 (plist-get info :with-after-letter) t t)
679 ;; Document end.
680 "\n\\end{document}"))
682 (defun org-koma-letter--build-settings (scope info)
683 "Build settings string according to type.
684 SCOPE is either `global' or `buffer'. INFO is a plist used as
685 a communication channel."
686 (let ((check-scope
687 (function
688 ;; Non-nil value when SETTING was defined in SCOPE.
689 (lambda (setting)
690 (let ((property (intern (format ":inbuffer-%s" setting))))
691 (if (eq scope 'global)
692 (eq (plist-get info property) 'koma-letter:empty)
693 (not (eq (plist-get info property) 'koma-letter:empty))))))))
694 (concat
695 ;; Name.
696 (let ((author (plist-get info :author)))
697 (and author
698 (funcall check-scope 'author)
699 (format "\\setkomavar{fromname}{%s}\n"
700 (org-export-data author info))))
701 ;; Email.
702 (let ((email (plist-get info :email)))
703 (and email
704 (funcall check-scope 'email)
705 (format "\\setkomavar{fromemail}{%s}\n" email)))
706 (and (funcall check-scope 'with-email)
707 (format "\\KOMAoption{fromemail}{%s}\n"
708 (if (plist-get info :with-email) "true" "false")))
709 ;; Phone number.
710 (let ((phone-number (plist-get info :phone-number)))
711 (and (org-string-nw-p phone-number)
712 (funcall check-scope 'phone-number)
713 (format "\\setkomavar{fromphone}{%s}\n" phone-number)))
714 (and (funcall check-scope 'with-phone)
715 (format "\\KOMAoption{fromphone}{%s}\n"
716 (if (plist-get info :with-phone) "true" "false")))
717 ;; Signature.
718 (let* ((heading-val
719 (and (plist-get info :with-headline-opening)
720 (org-string-nw-p
721 (org-trim
722 (org-export-data
723 (org-koma-letter--get-tagged-contents 'closing)
724 info)))))
725 (signature (org-string-nw-p (plist-get info :signature)))
726 (signature-scope (funcall check-scope 'signature)))
727 (and (or (and signature signature-scope)
728 heading-val)
729 (not (and (eq scope 'global) heading-val))
730 (format "\\setkomavar{signature}{%s}\n"
731 (if signature-scope signature heading-val))))
732 ;; Back address.
733 (and (funcall check-scope 'with-backaddress)
734 (format "\\KOMAoption{backaddress}{%s}\n"
735 (if (plist-get info :with-backaddress) "true" "false")))
736 ;; Place.
737 (and (funcall check-scope 'place)
738 (format "\\setkomavar{place}{%s}\n"
739 (if (plist-get info :with-place) (plist-get info :place) "")))
740 ;; Folding marks.
741 (and (funcall check-scope 'with-foldmarks)
742 (let ((foldmarks (plist-get info :with-foldmarks)))
743 (cond ((consp foldmarks)
744 (format "\\KOMAoptions{foldmarks=true,foldmarks=%s}\n"
745 (mapconcat #'symbol-name foldmarks "")))
746 (foldmarks "\\KOMAoptions{foldmarks=true}\n")
747 (t "\\KOMAoptions{foldmarks=false}\n")))))))
751 ;;; Commands
753 ;;;###autoload
754 (defun org-koma-letter-export-as-latex
755 (&optional async subtreep visible-only body-only ext-plist)
756 "Export current buffer as a KOMA Scrlttr2 letter.
758 If narrowing is active in the current buffer, only export its
759 narrowed part.
761 If a region is active, export that region.
763 A non-nil optional argument ASYNC means the process should happen
764 asynchronously. The resulting buffer should be accessible
765 through the `org-export-stack' interface.
767 When optional argument SUBTREEP is non-nil, export the sub-tree
768 at point, extracting information from the headline properties
769 first.
771 When optional argument VISIBLE-ONLY is non-nil, don't export
772 contents of hidden elements.
774 When optional argument BODY-ONLY is non-nil, only write code
775 between \"\\begin{letter}\" and \"\\end{letter}\".
777 EXT-PLIST, when provided, is a proeprty list with external
778 parameters overriding Org default settings, but still inferior to
779 file-local settings.
781 Export is done in a buffer named \"*Org KOMA-LETTER Export*\". It
782 will be displayed if `org-export-show-temporary-export-buffer' is
783 non-nil."
784 (interactive)
785 (let (org-koma-letter-special-contents)
786 (org-export-to-buffer 'koma-letter "*Org KOMA-LETTER Export*"
787 async subtreep visible-only body-only ext-plist
788 (lambda () (LaTeX-mode)))))
790 ;;;###autoload
791 (defun org-koma-letter-export-to-latex
792 (&optional async subtreep visible-only body-only ext-plist)
793 "Export current buffer as a KOMA Scrlttr2 letter (tex).
795 If narrowing is active in the current buffer, only export its
796 narrowed part.
798 If a region is active, export that region.
800 A non-nil optional argument ASYNC means the process should happen
801 asynchronously. The resulting file should be accessible through
802 the `org-export-stack' interface.
804 When optional argument SUBTREEP is non-nil, export the sub-tree
805 at point, extracting information from the headline properties
806 first.
808 When optional argument VISIBLE-ONLY is non-nil, don't export
809 contents of hidden elements.
811 When optional argument BODY-ONLY is non-nil, only write code
812 between \"\\begin{letter}\" and \"\\end{letter}\".
814 EXT-PLIST, when provided, is a property list with external
815 parameters overriding Org default settings, but still inferior to
816 file-local settings.
818 When optional argument PUB-DIR is set, use it as the publishing
819 directory.
821 Return output file's name."
822 (interactive)
823 (let ((outfile (org-export-output-file-name ".tex" subtreep))
824 (org-koma-letter-special-contents))
825 (org-export-to-file 'koma-letter outfile
826 async subtreep visible-only body-only ext-plist)))
828 ;;;###autoload
829 (defun org-koma-letter-export-to-pdf
830 (&optional async subtreep visible-only body-only ext-plist)
831 "Export current buffer as a KOMA Scrlttr2 letter (pdf).
833 If narrowing is active in the current buffer, only export its
834 narrowed part.
836 If a region is active, export that region.
838 A non-nil optional argument ASYNC means the process should happen
839 asynchronously. The resulting file should be accessible through
840 the `org-export-stack' interface.
842 When optional argument SUBTREEP is non-nil, export the sub-tree
843 at point, extracting information from the headline properties
844 first.
846 When optional argument VISIBLE-ONLY is non-nil, don't export
847 contents of hidden elements.
849 When optional argument BODY-ONLY is non-nil, only write code
850 between \"\\begin{letter}\" and \"\\end{letter}\".
852 EXT-PLIST, when provided, is a property list with external
853 parameters overriding Org default settings, but still inferior to
854 file-local settings.
856 Return PDF file's name."
857 (interactive)
858 (let ((file (org-export-output-file-name ".tex" subtreep))
859 (org-koma-letter-special-contents))
860 (org-export-to-file 'koma-letter file
861 async subtreep visible-only body-only ext-plist
862 (lambda (file) (org-latex-compile file)))))
865 (provide 'ox-koma-letter)
866 ;;; ox-koma-letter.el ends here