Fix handling of author and email in koma exporter.
[org-mode/org-tableheadings.git] / contrib / lisp / ox-koma-letter.el
blob7f5053092e2f14f64e6aaac4fb3792f7d32b8733
1 ;;; ox-koma-letter.el --- KOMA Scrlttr2 Back-End for Org Export Engine
3 ;; Copyright (C) 2007-2012 Free Software Foundation, Inc.
5 ;; Author: Nicolas Goaziou <n.goaziou AT gmail DOT com>
6 ;; Alan Schmitt <alan.schmitt AT polytechnique DOT org>
7 ;; Keywords: org, wp, tex
9 ;; This program is free software: you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
14 ;; This program is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
22 ;;; Commentary:
24 ;; This library implements a KOMA Scrlttr2 back-end, derived from the
25 ;; LaTeX one.
27 ;; Depending on the desired output format, three commands are provided
28 ;; for export: `org-koma-letter-export-as-latex' (temporary buffer),
29 ;; `org-koma-letter-export-to-latex' ("tex" file) and
30 ;; `org-koma-letter-export-to-pdf' ("pdf" file).
32 ;; On top of buffer keywords supported by `latex' back-end (see
33 ;; `org-latex-options-alist'), this back-end introduces the following
34 ;; keywords: "CLOSING" (see `org-koma-letter-closing'), "FROM_ADDRESS"
35 ;; (see `org-koma-letter-from-address'), "LCO" (see
36 ;; `org-koma-letter-class-option-file'), "OPENING" (see
37 ;; `org-koma-letter-opening'), "PHONE_NUMBER" (see
38 ;; `org-koma-letter-phone-number'), "SIGNATURE" (see
39 ;; `org-koma-letter-signature') and "TO_ADDRESS".
41 ;; You will need to add an appropriate association in
42 ;; `org-latex-classes' in order to use the KOMA Scrlttr2 class. For
43 ;; example, you can use the following code:
45 ;; (add-to-list 'org-latex-classes
46 ;; '("my-letter"
47 ;; "\\documentclass\[%
48 ;; DIV=14,
49 ;; fontsize=12pt,
50 ;; parskip=half,
51 ;; subject=titled,
52 ;; backaddress=false,
53 ;; fromalign=left,
54 ;; fromemail=true,
55 ;; fromphone=true\]\{scrlttr2\}
56 ;; \[DEFAULT-PACKAGES]
57 ;; \[PACKAGES]
58 ;; \[EXTRA]"))
60 ;; Then, in your Org document, be sure to require the proper class
61 ;; with :
63 ;; #+LATEX_CLASS: my-letter
66 ;;; Code:
68 (require 'ox-latex)
71 ;;; User-Configurable Variables
73 (defgroup org-export-koma-letter nil
74 "Options for exporting to KOMA scrlttr2 class in LaTeX export."
75 :tag "Org Koma-Letter"
76 :group 'org-export)
78 (defcustom org-koma-letter-class-option-file "NF"
79 "Letter Class Option File."
80 :group 'org-export-koma-letter
81 :type 'string)
83 (defcustom org-koma-letter-author 'user-full-name
84 "The sender's name.
86 This variable defaults to calling the function `user-full-name'
87 which just returns the current `user-full-name'. Alternatively a
88 string, nil or a function may be given. Functions must return a
89 string."
90 :group 'org-export-koma-letter
91 :type '(radio (function-item user-full-name)
92 (string)
93 (function)
94 (const nil)))
96 (defcustom org-koma-letter-email 'org-koma-letter-email
97 "The sender's email address.
99 This variable defaults to the value `org-koma-letter-email' which
100 returns `user-mail-address'. Alternatively a string, nil or a
101 function may be given. Functions must return a string."
102 :group 'org-export-koma-letter
103 :type '(radio (function-item org-koma-letter-email)
104 (string)
105 (function)
106 (const nil)))
108 (defcustom org-koma-letter-from-address nil
109 "Sender's address, as a string."
110 :group 'org-export-koma-letter
111 :type 'string)
113 (defcustom org-koma-letter-phone-number nil
114 "Sender's phone number, as a string."
115 :group 'org-export-koma-letter
116 :type 'string)
119 (defcustom org-koma-letter-place nil
120 "Place from which the letter is sent."
121 :group 'org-export-koma-letter
122 :type 'string)
124 (defcustom org-koma-letter-opening nil
125 "Letter's opening, as a string."
126 :group 'org-export-koma-letter
127 :type 'string)
129 (defcustom org-koma-letter-closing nil
130 "Koma-Letter's closing, as a string."
131 :group 'org-export-koma-letter
132 :type 'string)
134 (defcustom org-koma-letter-signature "\\usekomavar{fromname}"
135 "String used as the signature."
136 :group 'org-export-koma-letter
137 :type 'string)
139 (defcustom org-koma-letter-use-subject "untitled"
140 "Use the title as the letter's subject."
141 :group 'org-export-koma-letter
142 :type 'string)
144 (defcustom org-koma-letter-use-backaddress t
145 "Print return address in small line above to address."
146 :group 'org-export-koma-letter
147 :type 'boolean)
149 (defcustom org-koma-letter-use-foldmarks "true"
150 "Configure appearence of fold marks.
152 Accepts any valid value for the KOMA-Script `foldmarks' option.
154 Use `foldmarks:true' to activate default fold marks or
155 `foldmarks:nil' to deactivate fold marks."
156 :group 'org-export-koma-letter
157 :type 'string)
159 (defcustom org-koma-letter-use-phone t
160 "Print sender's phone number."
161 :group 'org-export-koma-letter
162 :type 'boolean)
164 (defcustom org-koma-letter-use-email t
165 "Print sender's email address."
166 :group 'org-export-koma-letter
167 :type 'boolean)
169 (defcustom org-koma-letter-use-place t
170 "Print the letter's place next to the date."
171 :group 'org-export-koma-letter
172 :type 'boolean)
175 ;;; Define Back-End
177 (org-export-define-derived-backend 'koma-letter 'latex
178 :options-alist
179 '((:lco "LCO" nil org-koma-letter-class-option-file)
180 (:author "AUTHOR" nil (org-koma-letter--get-custom org-koma-letter-author) t)
181 (:from-address "FROM_ADDRESS" nil org-koma-letter-from-address newline)
182 (:phone-number "PHONE_NUMBER" nil org-koma-letter-phone-number)
183 (:email "EMAIL" nil (org-koma-letter--get-custom org-koma-letter-email) t)
184 (:to-address "TO_ADDRESS" nil nil newline)
185 (:place "PLACE" nil org-koma-letter-place)
186 (:opening "OPENING" nil org-koma-letter-opening)
187 (:closing "CLOSING" nil org-koma-letter-closing)
188 (:signature "SIGNATURE" nil org-koma-letter-signature newline)
190 (:with-backaddress nil "backaddress" org-koma-letter-use-backaddress)
191 (:with-foldmarks nil "foldmarks" org-koma-letter-use-foldmarks)
192 (:with-phone nil "phone" org-koma-letter-use-phone)
193 (:with-email nil "email" org-koma-letter-use-email)
194 (:with-place nil "place" org-koma-letter-use-place)
195 (:with-subject nil "subject" org-koma-letter-use-subject))
196 :translate-alist '((export-block . org-koma-letter-export-block)
197 (export-snippet . org-koma-letter-export-snippet)
198 (keyword . org-koma-letter-keyword)
199 (template . org-koma-letter-template))
200 :menu-entry
201 '(?k "Export with KOMA Scrlttr2"
202 ((?L "As LaTeX buffer" org-koma-letter-export-as-latex)
203 (?l "As LaTeX file" org-koma-letter-export-to-latex)
204 (?p "As PDF file" org-koma-letter-export-to-pdf)
205 (?o "As PDF file and open"
206 (lambda (a s v b)
207 (if a (org-koma-letter-export-to-pdf t s v b)
208 (org-open-file (org-koma-letter-export-to-pdf nil s v b))))))))
211 ;;; Helper functions
213 (defun org-koma-letter-email ()
214 "Return the current `user-mail-address'"
215 user-mail-address)
218 (defun org-koma-letter--get-custom (value)
219 "Determines whether a value is nil, a string or a
220 function (a symobl). If it is a function it it evaluates it."
221 (when value
222 (cond ((stringp value) value)
223 ((functionp value) (funcall value))
224 ((symbolp value) (symbol-name value)))))
227 ;;; Transcode Functions
229 ;;;; Export Block
231 (defun org-koma-letter-export-block (export-block contents info)
232 "Transcode an EXPORT-BLOCK element into KOMA Scrlttr2 code.
233 CONTENTS is nil. INFO is a plist used as a communication
234 channel."
235 (when (member (org-element-property :type export-block) '("KOMA-LETTER" "LATEX"))
236 (org-remove-indentation (org-element-property :value export-block))))
238 ;;;; Export Snippet
240 (defun org-koma-letter-export-snippet (export-snippet contents info)
241 "Transcode an EXPORT-SNIPPET object into KOMA Scrlttr2 code.
242 CONTENTS is nil. INFO is a plist used as a communication
243 channel."
244 (when (memq (org-export-snippet-backend export-snippet) '(latex koma-letter))
245 (org-element-property :value export-snippet)))
247 ;;;; Keyword
249 (defun org-koma-letter-keyword (keyword contents info)
250 "Transcode a KEYWORD element into KOMA Scrlttr2 code.
251 CONTENTS is nil. INFO is a plist used as a communication
252 channel."
253 (let ((key (org-element-property :key keyword))
254 (value (org-element-property :value keyword)))
255 ;; Handle specifically BEAMER and TOC (headlines only) keywords.
256 ;; Otherwise, fallback to `latex' back-end.
257 (if (equal key "KOMA-LETTER") value
258 (org-export-with-backend 'latex keyword contents info))))
260 ;;;; Template
262 (defun org-koma-letter-template (contents info)
263 "Return complete document string after KOMA Scrlttr2 conversion.
264 CONTENTS is the transcoded contents string. INFO is a plist
265 holding export options."
266 (concat
267 ;; Time-stamp.
268 (and (plist-get info :time-stamp-file)
269 (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
270 ;; Document class and packages.
271 (let ((class (plist-get info :latex-class))
272 (class-options (plist-get info :latex-class-options)))
273 (org-element-normalize-string
274 (let* ((header (nth 1 (assoc class org-latex-classes)))
275 (document-class-string
276 (and (stringp header)
277 (if (not class-options) header
278 (replace-regexp-in-string
279 "^[ \t]*\\\\documentclass\\(\\(\\[[^]]*\\]\\)?\\)"
280 class-options header t nil 1)))))
281 (if (not document-class-string)
282 (user-error "Unknown LaTeX class `%s'")
283 (org-latex-guess-babel-language
284 (org-latex-guess-inputenc
285 (org-splice-latex-header
286 document-class-string
287 org-latex-default-packages-alist ; defined in org.el
288 org-latex-packages-alist nil ; defined in org.el
289 (concat (plist-get info :latex-header)
290 (plist-get info :latex-header-extra))))
291 info)))))
292 (let ((lco (plist-get info :lco))
293 (author (plist-get info :author))
294 (from-address (plist-get info :from-address))
295 (phone-number (plist-get info :phone-number))
296 (email (plist-get info :email))
297 (signature (plist-get info :signature)))
298 (concat
299 ;; Letter Class Option File
300 (when lco
301 (let ((lco-files (split-string lco " "))
302 (lco-def ""))
303 (dolist (lco-file lco-files lco-def)
304 (setq lco-def (format "%s\\LoadLetterOption{%s}\n" lco-def lco-file)))
305 lco-def))
306 ;; Define "From" data.
307 (when author (format "\\setkomavar{fromname}{%s}\n"
308 (org-export-data author info)))
309 (when from-address (format "\\setkomavar{fromaddress}{%s}\n" from-address))
310 (when phone-number (format "\\setkomavar{fromphone}{%s}\n" phone-number))
311 (when email (format "\\setkomavar{fromemail}{%s}\n" email))
312 (when signature (format "\\setkomavar{signature}{%s}\n" signature))))
313 ;; Date.
314 (format "\\date{%s}\n" (org-export-data (org-export-get-date info) info))
315 ;; Place
316 (let ((with-place (plist-get info :with-place))
317 (place (plist-get info :place)))
318 (when (or place (not with-place))
319 (format "\\setkomavar{place}{%s}\n" (if with-place place ""))))
320 ;; KOMA options
321 (let ((with-backaddress (plist-get info :with-backaddress))
322 (with-foldmarks (plist-get info :with-foldmarks))
323 (with-phone (plist-get info :with-phone))
324 (with-email (plist-get info :with-email)))
325 (concat
326 (format "\\KOMAoption{backaddress}{%s}\n" (if with-backaddress "true" "false"))
327 (format "\\KOMAoption{foldmarks}{%s}\n" (if with-foldmarks with-foldmarks "false"))
328 (format "\\KOMAoption{fromphone}{%s}\n" (if with-phone "true" "false"))
329 (format "\\KOMAoption{fromemail}{%s}\n" (if with-email "true" "false"))))
330 ;; Document start
331 "\\begin{document}\n\n"
332 ;; Subject
333 (let ((with-subject (plist-get info :with-subject)))
334 (when with-subject
335 (concat
336 (format "\\KOMAoption{subject}{%s}\n" with-subject)
337 (format "\\setkomavar{subject}{%s}\n\n"
338 (org-export-data (plist-get info :title) info)))))
339 ;; Letter start
340 (format "\\begin{letter}{%%\n%s}\n\n"
341 (or (plist-get info :to-address) "no address given"))
342 ;; Opening.
343 (format "\\opening{%s}\n\n" (plist-get info :opening))
344 ;; Letter body.
345 contents
346 ;; Closing.
347 (format "\n\\closing{%s}\n\n" (plist-get info :closing))
348 ;; Letter end.
349 "\\end{letter}\n\\end{document}"))
353 ;;; Commands
355 ;;;###autoload
356 (defun org-koma-letter-export-as-latex
357 (&optional async subtreep visible-only body-only ext-plist)
358 "Export current buffer as a KOMA Scrlttr2 letter.
360 If narrowing is active in the current buffer, only export its
361 narrowed part.
363 If a region is active, export that region.
365 A non-nil optional argument ASYNC means the process should happen
366 asynchronously. The resulting buffer should be accessible
367 through the `org-export-stack' interface.
369 When optional argument SUBTREEP is non-nil, export the sub-tree
370 at point, extracting information from the headline properties
371 first.
373 When optional argument VISIBLE-ONLY is non-nil, don't export
374 contents of hidden elements.
376 When optional argument BODY-ONLY is non-nil, only write code
377 between \"\\begin{letter}\" and \"\\end{letter}\".
379 EXT-PLIST, when provided, is a property list with external
380 parameters overriding Org default settings, but still inferior to
381 file-local settings.
383 Export is done in a buffer named \"*Org KOMA-LETTER Export*\". It
384 will be displayed if `org-export-show-temporary-export-buffer' is
385 non-nil."
386 (interactive)
387 (if async
388 (org-export-async-start
389 (lambda (output)
390 (with-current-buffer (get-buffer-create "*Org KOMA-LETTER Export*")
391 (erase-buffer)
392 (insert output)
393 (goto-char (point-min))
394 (LaTeX-mode)
395 (org-export-add-to-stack (current-buffer) 'koma-letter)))
396 `(org-export-as 'koma-letter ,subtreep ,visible-only ,body-only
397 ',ext-plist))
398 (let ((outbuf (org-export-to-buffer
399 'koma-letter "*Org KOMA-LETTER Export*"
400 subtreep visible-only body-only ext-plist)))
401 (with-current-buffer outbuf (LaTeX-mode))
402 (when org-export-show-temporary-export-buffer
403 (switch-to-buffer-other-window outbuf)))))
405 ;;;###autoload
406 (defun org-koma-letter-export-to-latex
407 (&optional async subtreep visible-only body-only ext-plist)
408 "Export current buffer as a KOMA Scrlttr2 letter (tex).
410 If narrowing is active in the current buffer, only export its
411 narrowed part.
413 If a region is active, export that region.
415 A non-nil optional argument ASYNC means the process should happen
416 asynchronously. The resulting file should be accessible through
417 the `org-export-stack' interface.
419 When optional argument SUBTREEP is non-nil, export the sub-tree
420 at point, extracting information from the headline properties
421 first.
423 When optional argument VISIBLE-ONLY is non-nil, don't export
424 contents of hidden elements.
426 When optional argument BODY-ONLY is non-nil, only write code
427 between \"\\begin{letter}\" and \"\\end{letter}\".
429 EXT-PLIST, when provided, is a property list with external
430 parameters overriding Org default settings, but still inferior to
431 file-local settings.
433 When optional argument PUB-DIR is set, use it as the publishing
434 directory.
436 Return output file's name."
437 (interactive)
438 (let ((outfile (org-export-output-file-name ".tex" subtreep)))
439 (if async
440 (org-export-async-start
441 (lambda (f) (org-export-add-to-stack f 'koma-letter))
442 `(expand-file-name
443 (org-export-to-file
444 'koma-letter ,outfile ,subtreep ,visible-only ,body-only
445 ',ext-plist)))
446 (org-export-to-file
447 'koma-letter outfile subtreep visible-only body-only ext-plist))))
449 ;;;###autoload
450 (defun org-koma-letter-export-to-pdf
451 (&optional async subtreep visible-only body-only ext-plist)
452 "Export current buffer as a KOMA Scrlttr2 letter (pdf).
454 If narrowing is active in the current buffer, only export its
455 narrowed part.
457 If a region is active, export that region.
459 A non-nil optional argument ASYNC means the process should happen
460 asynchronously. The resulting file should be accessible through
461 the `org-export-stack' interface.
463 When optional argument SUBTREEP is non-nil, export the sub-tree
464 at point, extracting information from the headline properties
465 first.
467 When optional argument VISIBLE-ONLY is non-nil, don't export
468 contents of hidden elements.
470 When optional argument BODY-ONLY is non-nil, only write code
471 between \"\\begin{letter}\" and \"\\end{letter}\".
473 EXT-PLIST, when provided, is a property list with external
474 parameters overriding Org default settings, but still inferior to
475 file-local settings.
477 Return PDF file's name."
478 (interactive)
479 (if async
480 (let ((outfile (org-export-output-file-name ".tex" subtreep)))
481 (org-export-async-start
482 (lambda (f) (org-export-add-to-stack f 'koma-letter))
483 `(expand-file-name
484 (org-latex-compile
485 (org-export-to-file
486 'koma-letter ,outfile ,subtreep ,visible-only ,body-only
487 ',ext-plist)))))
488 (org-latex-compile
489 (org-koma-letter-export-to-latex
490 nil subtreep visible-only body-only ext-plist))))
493 (provide 'ox-koma-letter)
494 ;;; ox-koma-letter.el ends here