ox-koma-letter: Improve defcustom docstrings
[org-mode.git] / contrib / lisp / ox-koma-letter.el
blob6ccc11d059f3b4e7bdd844863f57188b60dc71a5
1 ;;; ox-koma-letter.el --- KOMA Scrlttr2 Back-End for Org Export Engine
3 ;; Copyright (C) 2007-2012, 2014 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 ;; - and "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. (see org-koma-letter-author),
71 ;; - "EMAIL": same as AUTHOR, (see org-koma-letter-email),
73 ;; Headlines are in general ignored. However, headlines with special
74 ;; tags can be used for specified contents like postscript (ps),
75 ;; carbon copy (cc), enclosures (encl) and code to be inserted after
76 ;; \end{letter} (after_letter). Specials tags are defined in
77 ;; `org-koma-letter-special-tags-after-closing' and
78 ;; `org-koma-letter-special-tags-after-letter'. Currently members of
79 ;; `org-koma-letter-special-tags-after-closing' used as macros and the
80 ;; content of the headline is the argument.
82 ;; Headlines with two and from may also be used rather than the
83 ;; keyword approach described above. If both a keyword and a headline
84 ;; with information is present precedence is determined by
85 ;; `org-koma-letter-prefer-special-headings'.
87 ;; You need an appropriate association in `org-latex-classes' in order
88 ;; to use the KOMA Scrlttr2 class. By default, a sparse scrlttr2
89 ;; class is provided: "default-koma-letter". You can also add you own
90 ;; letter class. For instance:
92 ;; (add-to-list 'org-latex-classes
93 ;; '("my-letter"
94 ;; "\\documentclass\[%
95 ;; DIV=14,
96 ;; fontsize=12pt,
97 ;; parskip=half,
98 ;; subject=titled,
99 ;; backaddress=false,
100 ;; fromalign=left,
101 ;; fromemail=true,
102 ;; fromphone=true\]\{scrlttr2\}
103 ;; \[DEFAULT-PACKAGES]
104 ;; \[PACKAGES]
105 ;; \[EXTRA]"))
107 ;; Then, in your Org document, be sure to require the proper class
108 ;; with :
110 ;; #+LATEX_CLASS: my-letter
112 ;; Or by setting `org-koma-letter-default-class'.
114 ;;; Code:
116 (require 'ox-latex)
118 ;; Install a default letter class.
119 (unless (assoc "default-koma-letter" org-latex-classes)
120 (add-to-list 'org-latex-classes
121 '("default-koma-letter" "\\documentclass[11pt]{scrlttr2}")))
124 ;;; User-Configurable Variables
126 (defgroup org-export-koma-letter nil
127 "Options for exporting to KOMA scrlttr2 class in LaTeX export."
128 :tag "Org Koma-Letter"
129 :group 'org-export)
131 (defcustom org-koma-letter-class-option-file "NF"
132 "Letter Class Option File.
133 This option can also be set with the LCO keyword."
134 :group 'org-export-koma-letter
135 :type 'string)
137 (defcustom org-koma-letter-author 'user-full-name
138 "Sender's name.
140 This variable defaults to calling the function `user-full-name'
141 which just returns the current function `user-full-name'.
142 Alternatively a string, nil or a function may be given.
143 Functions must return a string.
145 This option can also be set with the AUTHOR keyword."
146 :group 'org-export-koma-letter
147 :type '(radio (function-item user-full-name)
148 (string)
149 (function)
150 (const :tag "Do not export author" nil)))
152 (defcustom org-koma-letter-email 'org-koma-letter-email
153 "Sender's email address.
155 This variable defaults to the value `org-koma-letter-email' which
156 returns `user-mail-address'. Alternatively a string, nil or
157 a function may be given. Functions must return a string.
159 This option can also be set with the EMAIL keyword."
160 :group 'org-export-koma-letter
161 :type '(radio (function-item org-koma-letter-email)
162 (string)
163 (function)
164 (const :tag "Do not export email" nil)))
166 (defcustom org-koma-letter-from-address ""
167 "Sender's address, as a string.
168 This option can also be set with one or more FROM_ADDRESS
169 keywords."
170 :group 'org-export-koma-letter
171 :type 'string)
173 (defcustom org-koma-letter-phone-number ""
174 "Sender's phone number, as a string.
175 This option can also be set with the PHONE_NUMBER keyword."
176 :group 'org-export-koma-letter
177 :type 'string)
179 (defcustom org-koma-letter-place ""
180 "Place from which the letter is sent, as a string.
181 This option can also be set with the PLACE keyword."
182 :group 'org-export-koma-letter
183 :type 'string)
185 (defcustom org-koma-letter-opening ""
186 "Letter's opening, as a string.
188 This option can also be set with the OPENING keyword. Moreover,
189 when:
190 (1) this value is the empty string;
191 (2) there's no OPENING keyword or it is empty;
192 (3) `org-koma-letter-headline-is-opening-maybe' is non-nil;
193 (4) the letter contains a headline without a special
194 tag (e.g. \"to\" or \"ps\");
195 then the opening will be implicitly set as the headline title."
196 :group 'org-export-koma-letter
197 :type 'string)
199 (defcustom org-koma-letter-closing ""
200 "Letter's closing, as a string.
201 This option can also be set with the CLOSING keyword."
202 :group 'org-export-koma-letter
203 :type 'string)
205 (defcustom org-koma-letter-signature ""
206 "Signature, as a string.
207 This option can also be set with the SIGNATURE keyword."
208 :group 'org-export-koma-letter
209 :type 'string)
211 (defcustom org-koma-letter-prefer-special-headings nil
212 "Non-nil means prefer headlines over keywords for TO and FROM.
213 This option can also be set with the OPTIONS keyword, e.g.:
214 \"special-headings:t\"."
215 :group 'org-export-koma-letter
216 :type 'boolean)
218 (defcustom org-koma-letter-subject-format t
219 "Use the title as the subject of the letter.
221 When t, insert a subject using default options. When nil, do not
222 insert a subject at all. It can also be a list of symbols among
223 the following ones:
225 `afteropening' Subject after opening
226 `beforeopening' Subject before opening
227 `centered' Subject centered
228 `left' Subject left-justified
229 `right' Subject right-justified
230 `titled' Add title/description to subject
231 `underlined' Set subject underlined
232 `untitled' Do not add title/description to subject
234 Please refer to the KOMA-script manual (Table 4.16. in the
235 English manual of 2012-07-22).
237 This option can also be set with the OPTIONS keyword, e.g.:
238 \"subject:(underlined centered)\"."
239 :type
240 '(choice
241 (const :tag "No export" nil)
242 (const :tag "Default options" t)
243 (set :tag "Configure options"
244 (const :tag "Subject after opening" afteropening)
245 (const :tag "Subject before opening" beforeopening)
246 (const :tag "Subject centered" centered)
247 (const :tag "Subject left-justified" left)
248 (const :tag "Subject right-justified" right)
249 (const :tag "Add title or description to subject" underlined)
250 (const :tag "Set subject underlined" titled)
251 (const :tag "Do not add title or description to subject" untitled)))
252 :group 'org-export-koma-letter)
254 (defcustom org-koma-letter-use-backaddress nil
255 "Non-nil prints return address in small line above to address.
256 This option can also be set with the OPTIONS keyword, e.g.:
257 \"backaddress:t\"."
258 :group 'org-export-koma-letter
259 :type 'boolean)
261 (defcustom org-koma-letter-use-foldmarks t
262 "Configure appearance of folding marks.
264 When t, activate default folding marks. When nil, do not insert
265 folding marks at all. It can also be a list of symbols among the
266 following ones:
268 `B' Activate upper horizontal mark on left paper edge
269 `b' Deactivate upper horizontal mark on left paper edge
271 `H' Activate all horizontal marks on left paper edge
272 `h' Deactivate all horizontal marks on left paper edge
274 `L' Activate left vertical mark on upper paper edge
275 `l' Deactivate left vertical mark on upper paper edge
277 `M' Activate middle horizontal mark on left paper edge
278 `m' Deactivate middle horizontal mark on left paper edge
280 `P' Activate punch or center mark on left paper edge
281 `p' Deactivate punch or center mark on left paper edge
283 `T' Activate lower horizontal mark on left paper edge
284 `t' Deactivate lower horizontal mark on left paper edge
286 `V' Activate all vertical marks on upper paper edge
287 `v' Deactivate all vertical marks on upper paper edge
289 This option can also be set with the OPTIONS keyword, e.g.:
290 \"foldmarks:(b l m t)\"."
291 :group 'org-export-koma-letter
292 :type '(choice
293 (const :tag "Activate default folding marks" t)
294 (const :tag "Deactivate folding marks" nil)
295 (set
296 :tag "Configure folding marks"
297 (const :tag "Activate upper horizontal mark on left paper edge" B)
298 (const :tag "Deactivate upper horizontal mark on left paper edge" b)
299 (const :tag "Activate all horizontal marks on left paper edge" H)
300 (const :tag "Deactivate all horizontal marks on left paper edge" h)
301 (const :tag "Activate left vertical mark on upper paper edge" L)
302 (const :tag "Deactivate left vertical mark on upper paper edge" l)
303 (const :tag "Activate middle horizontal mark on left paper edge" M)
304 (const :tag "Deactivate middle horizontal mark on left paper edge" m)
305 (const :tag "Activate punch or center mark on left paper edge" P)
306 (const :tag "Deactivate punch or center mark on left paper edge" p)
307 (const :tag "Activate lower horizontal mark on left paper edge" T)
308 (const :tag "Deactivate lower horizontal mark on left paper edge" t)
309 (const :tag "Activate all vertical marks on upper paper edge" V)
310 (const :tag "Deactivate all vertical marks on upper paper edge" v))))
312 (defcustom org-koma-letter-use-phone nil
313 "Non-nil prints sender's phone number.
314 This option can also be set with the OPTIONS keyword, e.g.:
315 \"phone:t\"."
316 :group 'org-export-koma-letter
317 :type 'boolean)
319 (defcustom org-koma-letter-use-email nil
320 "Non-nil prints sender's email address.
321 This option can also be set with the OPTIONS keyword, e.g.:
322 \"email:t\"."
323 :group 'org-export-koma-letter
324 :type 'boolean)
326 (defcustom org-koma-letter-use-place t
327 "Non-nil prints the letter's place next to the date.
328 This option can also be set with the OPTIONS keyword, e.g.:
329 \"place:nil\"."
330 :group 'org-export-koma-letter
331 :type 'boolean)
333 (defcustom org-koma-letter-default-class "default-koma-letter"
334 "Default class for `org-koma-letter'.
335 The value must be a member of `org-latex-classes'."
336 :group 'org-export-koma-letter
337 :type 'string)
339 (defcustom org-koma-letter-headline-is-opening-maybe t
340 "Non-nil means a headline may be used as an opening.
341 A headline is only used if #+OPENING is not set. See also
342 `org-koma-letter-opening'."
343 :group 'org-export-koma-letter
344 :type 'boolean)
346 (defconst org-koma-letter-special-tags-in-letter '(to from)
347 "Header tags related to the letter itself.")
349 (defconst org-koma-letter-special-tags-after-closing '(ps encl cc)
350 "Header tags to be inserted after closing.")
352 (defconst org-koma-letter-special-tags-after-letter '(after_letter)
353 "Header tags to be inserted after closing.")
355 (defvar org-koma-letter-special-contents nil
356 "Holds special content temporarily.")
360 ;;; Define Back-End
362 (org-export-define-derived-backend 'koma-letter 'latex
363 :options-alist
364 '((:author "AUTHOR" nil (org-koma-letter--get-value org-koma-letter-author) t)
365 (:closing "CLOSING" nil org-koma-letter-closing)
366 (:email "EMAIL" nil (org-koma-letter--get-value org-koma-letter-email) t)
367 (:from-address "FROM_ADDRESS" nil nil newline)
368 (:latex-class "LATEX_CLASS" nil org-koma-letter-default-class t)
369 (:lco "LCO" nil org-koma-letter-class-option-file)
370 (:opening "OPENING" nil org-koma-letter-opening)
371 (:phone-number "PHONE_NUMBER" nil org-koma-letter-phone-number)
372 (:place "PLACE" nil org-koma-letter-place)
373 (:signature "SIGNATURE" nil org-koma-letter-signature newline)
374 (:to-address "TO_ADDRESS" nil nil newline)
375 (:special-headings nil "special-headings"
376 org-koma-letter-prefer-special-headings)
377 (:special-tags nil nil (append
378 org-koma-letter-special-tags-in-letter
379 org-koma-letter-special-tags-after-closing
380 org-koma-letter-special-tags-after-letter))
381 (:with-after-closing nil "after-closing-order"
382 org-koma-letter-special-tags-after-closing)
383 (:with-after-letter nil "after-letter-order"
384 org-koma-letter-special-tags-after-letter)
385 (:with-backaddress nil "backaddress" org-koma-letter-use-backaddress)
386 (:with-email nil "email" org-koma-letter-use-email)
387 (:with-foldmarks nil "foldmarks" org-koma-letter-use-foldmarks)
388 (:with-phone nil "phone" org-koma-letter-use-phone)
389 (:with-place nil "place" org-koma-letter-use-place)
390 (:with-subject nil "subject" org-koma-letter-subject-format)
391 ;; Special properties non-nil when a setting happened in buffer.
392 ;; They are used to prioritize in-buffer settings over "lco"
393 ;; files. See `org-koma-letter-template'.
394 (:inbuffer-author "AUTHOR" nil 'koma-letter:empty)
395 (:inbuffer-email "EMAIL" nil 'koma-letter:empty)
396 (:inbuffer-phone-number "PHONE_NUMBER" nil 'koma-letter:empty)
397 (:inbuffer-place "PLACE" nil 'koma-letter:empty)
398 (:inbuffer-signature "SIGNATURE" nil 'koma-letter:empty)
399 (:inbuffer-with-backaddress nil "backaddress" 'koma-letter:empty)
400 (:inbuffer-with-email nil "email" 'koma-letter:empty)
401 (:inbuffer-with-foldmarks nil "foldmarks" 'koma-letter:empty)
402 (:inbuffer-with-phone nil "phone" 'koma-letter:empty))
403 :translate-alist '((export-block . org-koma-letter-export-block)
404 (export-snippet . org-koma-letter-export-snippet)
405 (headline . org-koma-letter-headline)
406 (keyword . org-koma-letter-keyword)
407 (template . org-koma-letter-template))
408 :menu-entry
409 '(?k "Export with KOMA Scrlttr2"
410 ((?L "As LaTeX buffer" org-koma-letter-export-as-latex)
411 (?l "As LaTeX file" org-koma-letter-export-to-latex)
412 (?p "As PDF file" org-koma-letter-export-to-pdf)
413 (?o "As PDF file and open"
414 (lambda (a s v b)
415 (if a (org-koma-letter-export-to-pdf t s v b)
416 (org-open-file (org-koma-letter-export-to-pdf nil s v b))))))))
420 ;;; Helper functions
422 (defun org-koma-letter-email ()
423 "Return the current `user-mail-address'."
424 user-mail-address)
426 ;; The following is taken from/inspired by ox-grof.el
427 ;; Thanks, Luis!
429 (defun org-koma-letter--get-tagged-contents (key)
430 "Get contents from a headline tagged with KEY.
431 The contents is stored in `org-koma-letter-special-contents'."
432 (cdr (assoc (org-koma-letter--get-value key)
433 org-koma-letter-special-contents)))
435 (defun org-koma-letter--get-value (value)
436 "Turn value into a string whenever possible.
437 Determines if VALUE is nil, a string, a function or a symbol and
438 return a string or nil."
439 (when value
440 (cond ((stringp value) value)
441 ((functionp value) (funcall value))
442 ((symbolp value) (symbol-name value))
443 (t value))))
445 (defun org-koma-letter--special-contents-as-macro
446 (keywords &optional keep-newlines no-tag)
447 "Process KEYWORDS members of `org-koma-letter-special-contents'.
448 KEYWORDS is a list of symbols. Return them as a string to be
449 formatted.
451 The function is used for inserting content of special headings
452 such as PS.
454 If KEEP-NEWLINES is t newlines will not be removed. If NO-TAG is
455 t the content in `org-koma-letter-special-contents' will not be
456 wrapped in a macro named whatever the members of KEYWORDS are
457 called."
458 (mapconcat
459 #'(lambda (keyword)
460 (let* ((name (org-koma-letter--get-value keyword))
461 (value (org-koma-letter--get-tagged-contents name)))
462 (when value
463 (if no-tag (if keep-newlines value (org-trim value))
464 (format "\\%s{%s}\n"
465 name
466 (if keep-newlines value (org-trim value)))))))
467 keywords
468 ""))
470 (defun org-koma-letter--determine-to-and-from (info key)
471 "Given INFO determine KEY for the letter.
472 KEY should be `to' or `from'.
474 `ox-koma-letter' allows two ways to specify TO and FROM. If both
475 are present return the preferred one as determined by
476 `org-koma-letter-prefer-special-headings'."
477 (let ((option (plist-get info (if (eq key 'to) :to-address :from-address)))
478 (headline (org-koma-letter--get-tagged-contents key)))
479 (replace-regexp-in-string
480 "\n" "\\\\\\\\\n"
481 (org-trim
482 (or (if (plist-get info :special-headings) (or headline option)
483 (or option headline))
484 ;; Fallback values.
485 (if (eq key 'to) "\\mbox{}" org-koma-letter-from-address))))))
489 ;;; Transcode Functions
491 ;;;; Export Block
493 (defun org-koma-letter-export-block (export-block contents info)
494 "Transcode an EXPORT-BLOCK element into KOMA Scrlttr2 code.
495 CONTENTS is nil. INFO is a plist used as a communication
496 channel."
497 (when (member (org-element-property :type export-block) '("KOMA-LETTER" "LATEX"))
498 (org-remove-indentation (org-element-property :value export-block))))
500 ;;;; Export Snippet
502 (defun org-koma-letter-export-snippet (export-snippet contents info)
503 "Transcode an EXPORT-SNIPPET object into KOMA Scrlttr2 code.
504 CONTENTS is nil. INFO is a plist used as a communication
505 channel."
506 (when (memq (org-export-snippet-backend export-snippet) '(latex koma-letter))
507 (org-element-property :value export-snippet)))
509 ;;;; Keyword
511 (defun org-koma-letter-keyword (keyword contents info)
512 "Transcode a KEYWORD element into KOMA Scrlttr2 code.
513 CONTENTS is nil. INFO is a plist used as a communication
514 channel."
515 (let ((key (org-element-property :key keyword))
516 (value (org-element-property :value keyword)))
517 ;; Handle specifically KOMA-LETTER keywords. Otherwise, fallback
518 ;; to `latex' back-end.
519 (if (equal key "KOMA-LETTER") value
520 (org-export-with-backend 'latex keyword contents info))))
522 ;; Headline
524 (defun org-koma-letter-headline (headline contents info)
525 "Transcode a HEADLINE element from Org to LaTeX.
526 CONTENTS holds the contents of the headline. INFO is a plist
527 holding contextual information.
529 Note that if a headline is tagged with a tag from
530 `org-koma-letter-special-tags' it will not be exported, but
531 stored in `org-koma-letter-special-contents' and included at the
532 appropriate place."
533 (unless (let ((tag (car (org-export-get-tags headline info))))
534 (and tag
535 (member-ignore-case
536 tag (mapcar #'symbol-name (plist-get info :special-tags)))
537 ;; Store association for later use and bail out.
538 (push (cons tag contents) org-koma-letter-special-contents)))
539 ;; Opening is not defined yet: use headline's title.
540 (when (and org-koma-letter-headline-is-opening-maybe
541 (not (org-string-nw-p (plist-get info :opening))))
542 (plist-put info :opening
543 (org-export-data (org-element-property :title headline) info)))
544 ;; In any case, insert contents in letter's body.
545 contents))
547 ;;;; Template
549 (defun org-koma-letter-template (contents info)
550 "Return complete document string after KOMA Scrlttr2 conversion.
551 CONTENTS is the transcoded contents string. INFO is a plist
552 holding export options."
553 (concat
554 ;; Time-stamp.
555 (and (plist-get info :time-stamp-file)
556 (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
557 ;; Document class and packages.
558 (let* ((class (plist-get info :latex-class))
559 (class-options (plist-get info :latex-class-options))
560 (header (nth 1 (assoc class org-latex-classes)))
561 (document-class-string
562 (and (stringp header)
563 (if (not class-options) header
564 (replace-regexp-in-string
565 "^[ \t]*\\\\documentclass\\(\\(\\[[^]]*\\]\\)?\\)"
566 class-options header t nil 1)))))
567 (if (not document-class-string)
568 (user-error "Unknown LaTeX class `%s'" class)
569 (org-latex-guess-babel-language
570 (org-latex-guess-inputenc
571 (org-element-normalize-string
572 (org-splice-latex-header
573 document-class-string
574 org-latex-default-packages-alist ; Defined in org.el.
575 org-latex-packages-alist nil ; Defined in org.el.
576 (concat (org-element-normalize-string (plist-get info :latex-header))
577 (plist-get info :latex-header-extra)))))
578 info)))
579 ;; Settings. They can come from three locations, in increasing
580 ;; order of precedence: global variables, LCO files and in-buffer
581 ;; settings. Thus, we first insert settings coming from global
582 ;; variables, then we insert LCO files, and, eventually, we insert
583 ;; settings coming from buffer keywords.
584 (org-koma-letter--build-settings 'global info)
585 (mapconcat #'(lambda (file) (format "\\LoadLetterOption{%s}\n" file))
586 (org-split-string (or (plist-get info :lco) "") " ")
588 (org-koma-letter--build-settings 'buffer info)
589 ;; From address.
590 (let ((from-address (org-koma-letter--determine-to-and-from info 'from)))
591 (and from-address (format "\\setkomavar{fromaddress}{%s}\n" from-address)))
592 ;; Date.
593 (format "\\date{%s}\n" (org-export-data (org-export-get-date info) info))
594 ;; Document start
595 "\\begin{document}\n\n"
596 ;; Subject
597 (let ((with-subject (plist-get info :with-subject)))
598 (when with-subject
599 (concat
600 (unless (eq with-subject t)
601 (format "\\KOMAoption{subject}{%s}\n"
602 (if (symbolp with-subject) with-subject
603 (mapconcat #'symbol-name with-subject ","))))
604 (let ((subject (org-export-data (plist-get info :title) info)))
605 (and (org-string-nw-p subject)
606 (format "\\setkomavar{subject}{%s}\n\n" subject))))))
607 ;; Letter start.
608 (format "\\begin{letter}{%%\n%s}\n\n"
609 (org-koma-letter--determine-to-and-from info 'to))
610 ;; Opening.
611 (format "\\opening{%s}\n\n" (plist-get info :opening))
612 ;; Letter body.
613 contents
614 ;; Closing.
615 (format "\n\\closing{%s}\n" (plist-get info :closing))
616 (org-koma-letter--special-contents-as-macro
617 (plist-get info :with-after-closing))
618 ;; Letter end.
619 "\n\\end{letter}\n"
620 (org-koma-letter--special-contents-as-macro
621 (plist-get info :with-after-letter) t t)
622 ;; Document end.
623 "\n\\end{document}"))
625 (defun org-koma-letter--build-settings (scope info)
626 "Build settings string according to type.
627 SCOPE is either `global' or `buffer'. INFO is a plist used as
628 a communication channel."
629 (let ((check-scope
630 (function
631 ;; Non-nil value when SETTING was defined in SCOPE.
632 (lambda (setting)
633 (let ((property (intern (format ":inbuffer-%s" setting))))
634 (if (eq scope 'global)
635 (eq (plist-get info property) 'koma-letter:empty)
636 (not (eq (plist-get info property) 'koma-letter:empty))))))))
637 (concat
638 ;; Name.
639 (let ((author (plist-get info :author)))
640 (and author
641 (funcall check-scope 'author)
642 (format "\\setkomavar{fromname}{%s}\n"
643 (org-export-data author info))))
644 ;; Email.
645 (let ((email (plist-get info :email)))
646 (and email
647 (funcall check-scope 'email)
648 (format "\\setkomavar{fromemail}{%s}\n" email)))
649 (and (funcall check-scope 'with-email)
650 (format "\\KOMAoption{fromemail}{%s}\n"
651 (if (plist-get info :with-email) "true" "false")))
652 ;; Phone number.
653 (let ((phone-number (plist-get info :phone-number)))
654 (and (org-string-nw-p phone-number)
655 (funcall check-scope 'phone-number)
656 (format "\\setkomavar{fromphone}{%s}\n" phone-number)))
657 (and (funcall check-scope 'with-phone)
658 (format "\\KOMAoption{fromphone}{%s}\n"
659 (if (plist-get info :with-phone) "true" "false")))
660 ;; Signature.
661 (let ((signature (plist-get info :signature)))
662 (and (org-string-nw-p signature)
663 (funcall check-scope 'signature)
664 (format "\\setkomavar{signature}{%s}\n" signature)))
665 ;; Back address.
666 (and (funcall check-scope 'with-backaddress)
667 (format "\\KOMAoption{backaddress}{%s}\n"
668 (if (plist-get info :with-backaddress) "true" "false")))
669 ;; Place.
670 (and (funcall check-scope 'place)
671 (format "\\setkomavar{place}{%s}\n"
672 (if (plist-get info :with-place) (plist-get info :place) "")))
673 ;; Folding marks.
674 (and (funcall check-scope 'with-foldmarks)
675 (let ((foldmarks (plist-get info :with-foldmarks)))
676 (cond ((consp foldmarks)
677 (format "\\KOMAoptions{foldmarks=true,foldmarks=%s}\n"
678 (mapconcat #'symbol-name foldmarks "")))
679 (foldmarks "\\KOMAoptions{foldmarks=true}\n")
680 (t "\\KOMAoptions{foldmarks=false}\n")))))))
684 ;;; Commands
686 ;;;###autoload
687 (defun org-koma-letter-export-as-latex
688 (&optional async subtreep visible-only body-only ext-plist)
689 "Export current buffer as a KOMA Scrlttr2 letter.
691 If narrowing is active in the current buffer, only export its
692 narrowed part.
694 If a region is active, export that region.
696 A non-nil optional argument ASYNC means the process should happen
697 asynchronously. The resulting buffer should be accessible
698 through the `org-export-stack' interface.
700 When optional argument SUBTREEP is non-nil, export the sub-tree
701 at point, extracting information from the headline properties
702 first.
704 When optional argument VISIBLE-ONLY is non-nil, don't export
705 contents of hidden elements.
707 When optional argument BODY-ONLY is non-nil, only write code
708 between \"\\begin{letter}\" and \"\\end{letter}\".
710 EXT-PLIST, when provided, is a proeprty list with external
711 parameters overriding Org default settings, but still inferior to
712 file-local settings.
714 Export is done in a buffer named \"*Org KOMA-LETTER Export*\". It
715 will be displayed if `org-export-show-temporary-export-buffer' is
716 non-nil."
717 (interactive)
718 (let (org-koma-letter-special-contents)
719 (org-export-to-buffer 'koma-letter "*Org KOMA-LETTER Export*"
720 async subtreep visible-only body-only ext-plist
721 (lambda () (LaTeX-mode)))))
723 ;;;###autoload
724 (defun org-koma-letter-export-to-latex
725 (&optional async subtreep visible-only body-only ext-plist)
726 "Export current buffer as a KOMA Scrlttr2 letter (tex).
728 If narrowing is active in the current buffer, only export its
729 narrowed part.
731 If a region is active, export that region.
733 A non-nil optional argument ASYNC means the process should happen
734 asynchronously. The resulting file should be accessible through
735 the `org-export-stack' interface.
737 When optional argument SUBTREEP is non-nil, export the sub-tree
738 at point, extracting information from the headline properties
739 first.
741 When optional argument VISIBLE-ONLY is non-nil, don't export
742 contents of hidden elements.
744 When optional argument BODY-ONLY is non-nil, only write code
745 between \"\\begin{letter}\" and \"\\end{letter}\".
747 EXT-PLIST, when provided, is a property list with external
748 parameters overriding Org default settings, but still inferior to
749 file-local settings.
751 When optional argument PUB-DIR is set, use it as the publishing
752 directory.
754 Return output file's name."
755 (interactive)
756 (let ((outfile (org-export-output-file-name ".tex" subtreep))
757 (org-koma-letter-special-contents))
758 (org-export-to-file 'koma-letter outfile
759 async subtreep visible-only body-only ext-plist)))
761 ;;;###autoload
762 (defun org-koma-letter-export-to-pdf
763 (&optional async subtreep visible-only body-only ext-plist)
764 "Export current buffer as a KOMA Scrlttr2 letter (pdf).
766 If narrowing is active in the current buffer, only export its
767 narrowed part.
769 If a region is active, export that region.
771 A non-nil optional argument ASYNC means the process should happen
772 asynchronously. The resulting file should be accessible through
773 the `org-export-stack' interface.
775 When optional argument SUBTREEP is non-nil, export the sub-tree
776 at point, extracting information from the headline properties
777 first.
779 When optional argument VISIBLE-ONLY is non-nil, don't export
780 contents of hidden elements.
782 When optional argument BODY-ONLY is non-nil, only write code
783 between \"\\begin{letter}\" and \"\\end{letter}\".
785 EXT-PLIST, when provided, is a property list with external
786 parameters overriding Org default settings, but still inferior to
787 file-local settings.
789 Return PDF file's name."
790 (interactive)
791 (let ((file (org-export-output-file-name ".tex" subtreep))
792 (org-koma-letter-special-contents))
793 (org-export-to-file 'koma-letter file
794 async subtreep visible-only body-only ext-plist
795 (lambda (file) (org-latex-compile file)))))
798 (provide 'ox-koma-letter)
799 ;;; ox-koma-letter.el ends here