Merge branch 'maint'
[org-mode/org-tableheadings.git] / contrib / lisp / ox-koma-letter.el
blob96c056965bde95d5a187b3ced622a46b38644a6d
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-sender nil
84 "Sender's name, as a string."
85 :group 'org-export-koma-letter
86 :type 'string)
88 (defcustom org-koma-letter-from-address nil
89 "Sender's address, as a string."
90 :group 'org-export-koma-letter
91 :type 'string)
93 (defcustom org-koma-letter-phone-number nil
94 "Sender's phone number, as a string."
95 :group 'org-export-koma-letter
96 :type 'string)
98 (defcustom org-koma-letter-email nil
99 "Sender's email, as a string."
100 :group 'org-export-koma-letter
101 :type 'string)
103 (defcustom org-koma-letter-place nil
104 "Place from which the letter is sent."
105 :group 'org-export-koma-letter
106 :type 'string)
108 (defcustom org-koma-letter-opening nil
109 "Letter's opening, as a string."
110 :group 'org-export-koma-letter
111 :type 'string)
113 (defcustom org-koma-letter-closing nil
114 "Koma-Letter's closing, as a string."
115 :group 'org-export-koma-letter
116 :type 'string)
118 (defcustom org-koma-letter-signature "\\usekomavar{fromname}"
119 "String used as the signature."
120 :group 'org-export-koma-letter
121 :type 'string)
123 (defcustom org-koma-letter-use-subject "untitled"
124 "Use the title as the letter's subject."
125 :group 'org-export-koma-letter
126 :type 'string)
128 (defcustom org-koma-letter-use-backaddress t
129 "Print return address in small line above to address."
130 :group 'org-export-koma-letter
131 :type 'boolean)
133 (defcustom org-koma-letter-use-foldmarks "true"
134 "Configure appearence of fold marks.
136 Accepts any valid value for the KOMA-Script `foldmarks' option.
138 Use `foldmarks:true' to activate default fold marks or
139 `foldmarks:nil' to deactivate fold marks."
140 :group 'org-export-koma-letter
141 :type 'string)
143 (defcustom org-koma-letter-use-phone t
144 "Print sender's phone number."
145 :group 'org-export-koma-letter
146 :type 'boolean)
148 (defcustom org-koma-letter-use-email t
149 "Print sender's email address."
150 :group 'org-export-koma-letter
151 :type 'boolean)
153 (defcustom org-koma-letter-use-place t
154 "Print the letter's place next to the date."
155 :group 'org-export-koma-letter
156 :type 'boolean)
159 ;;; Define Back-End
161 (org-export-define-derived-backend 'koma-letter 'latex
162 :options-alist
163 '((:lco "LCO" nil org-koma-letter-class-option-file)
164 (:sender "SENDER" nil org-koma-letter-sender newline)
165 (:from-address "FROM_ADDRESS" nil org-koma-letter-from-address newline)
166 (:phone-number "PHONE_NUMBER" nil org-koma-letter-phone-number)
167 (:email "EMAIL" nil org-koma-letter-email)
168 (:to-address "TO_ADDRESS" nil nil newline)
169 (:place "PLACE" nil org-koma-letter-place)
170 (:opening "OPENING" nil org-koma-letter-opening)
171 (:closing "CLOSING" nil org-koma-letter-closing)
172 (:signature "SIGNATURE" nil org-koma-letter-signature newline)
174 (:with-backaddress nil "backaddress" org-koma-letter-use-backaddress)
175 (:with-foldmarks nil "foldmarks" org-koma-letter-use-foldmarks)
176 (:with-phone nil "phone" org-koma-letter-use-phone)
177 (:with-email nil "email" org-koma-letter-use-email)
178 (:with-place nil "place" org-koma-letter-use-place)
179 (:with-subject nil "subject" org-koma-letter-use-subject))
180 :translate-alist '((export-block . org-koma-letter-export-block)
181 (export-snippet . org-koma-letter-export-snippet)
182 (keyword . org-koma-letter-keyword)
183 (template . org-koma-letter-template))
184 :menu-entry
185 '(?k "Export with KOMA Scrlttr2"
186 ((?L "As LaTeX buffer" org-koma-letter-export-as-latex)
187 (?l "As LaTeX file" org-koma-letter-export-to-latex)
188 (?p "As PDF file" org-koma-letter-export-to-pdf)
189 (?o "As PDF file and open"
190 (lambda (a s v b)
191 (if a (org-koma-letter-export-to-pdf t s v b)
192 (org-open-file (org-koma-letter-export-to-pdf nil s v b))))))))
195 ;;; Transcode Functions
197 ;;;; Export Block
199 (defun org-koma-letter-export-block (export-block contents info)
200 "Transcode an EXPORT-BLOCK element into KOMA Scrlttr2 code.
201 CONTENTS is nil. INFO is a plist used as a communication
202 channel."
203 (when (member (org-element-property :type export-block) '("KOMA-LETTER" "LATEX"))
204 (org-remove-indentation (org-element-property :value export-block))))
206 ;;;; Export Snippet
208 (defun org-koma-letter-export-snippet (export-snippet contents info)
209 "Transcode an EXPORT-SNIPPET object into KOMA Scrlttr2 code.
210 CONTENTS is nil. INFO is a plist used as a communication
211 channel."
212 (when (memq (org-export-snippet-backend export-snippet) '(latex koma-letter))
213 (org-element-property :value export-snippet)))
215 ;;;; Keyword
217 (defun org-koma-letter-keyword (keyword contents info)
218 "Transcode a KEYWORD element into KOMA Scrlttr2 code.
219 CONTENTS is nil. INFO is a plist used as a communication
220 channel."
221 (let ((key (org-element-property :key keyword))
222 (value (org-element-property :value keyword)))
223 ;; Handle specifically BEAMER and TOC (headlines only) keywords.
224 ;; Otherwise, fallback to `latex' back-end.
225 (if (equal key "KOMA-LETTER") value
226 (org-export-with-backend 'latex keyword contents info))))
228 ;;;; Template
230 (defun org-koma-letter-template (contents info)
231 "Return complete document string after KOMA Scrlttr2 conversion.
232 CONTENTS is the transcoded contents string. INFO is a plist
233 holding export options."
234 (concat
235 ;; Time-stamp.
236 (and (plist-get info :time-stamp-file)
237 (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
238 ;; Document class and packages.
239 (let ((class (plist-get info :latex-class))
240 (class-options (plist-get info :latex-class-options)))
241 (org-element-normalize-string
242 (let* ((header (nth 1 (assoc class org-latex-classes)))
243 (document-class-string
244 (and (stringp header)
245 (if (not class-options) header
246 (replace-regexp-in-string
247 "^[ \t]*\\\\documentclass\\(\\(\\[[^]]*\\]\\)?\\)"
248 class-options header t nil 1)))))
249 (if (not document-class-string)
250 (user-error "Unknown LaTeX class `%s'")
251 (org-latex-guess-babel-language
252 (org-latex-guess-inputenc
253 (org-splice-latex-header
254 document-class-string
255 org-latex-default-packages-alist ; defined in org.el
256 org-latex-packages-alist nil ; defined in org.el
257 (concat (plist-get info :latex-header)
258 (plist-get info :latex-header-extra))))
259 info)))))
260 (let ((lco (plist-get info :lco))
261 (sender (plist-get info :sender))
262 (from-address (plist-get info :from-address))
263 (phone-number (plist-get info :phone-number))
264 (email (plist-get info :email))
265 (signature (plist-get info :signature)))
266 (concat
267 ;; Letter Class Option File
268 (when lco
269 (let ((lco-files (split-string lco " "))
270 (lco-def ""))
271 (dolist (lco-file lco-files lco-def)
272 (setq lco-def (format "%s\\LoadLetterOption{%s}\n" lco-def lco-file)))
273 lco-def))
274 ;; Define "From" data.
275 (when sender (format "\\setkomavar{fromname}{%s}\n" sender))
276 (when from-address (format "\\setkomavar{fromaddress}{%s}\n" from-address))
277 (when phone-number (format "\\setkomavar{fromphone}{%s}\n" phone-number))
278 (when email (format "\\setkomavar{fromemail}{%s}\n" email))
279 (when signature (format "\\setkomavar{signature}{%s}\n" signature))))
280 ;; Date.
281 (format "\\date{%s}\n" (org-export-data (org-export-get-date info) info))
282 ;; Place
283 (let ((with-place (plist-get info :with-place))
284 (place (plist-get info :place)))
285 (when (or place (not with-place))
286 (format "\\setkomavar{place}{%s}\n" (if with-place place ""))))
287 ;; KOMA options
288 (let ((with-backaddress (plist-get info :with-backaddress))
289 (with-foldmarks (plist-get info :with-foldmarks))
290 (with-phone (plist-get info :with-phone))
291 (with-email (plist-get info :with-email)))
292 (concat
293 (format "\\KOMAoption{backaddress}{%s}\n" (if with-backaddress "true" "false"))
294 (format "\\KOMAoption{foldmarks}{%s}\n" (if with-foldmarks with-foldmarks "false"))
295 (format "\\KOMAoption{fromphone}{%s}\n" (if with-phone "true" "false"))
296 (format "\\KOMAoption{fromemail}{%s}\n" (if with-email "true" "false"))))
297 ;; Document start
298 "\\begin{document}\n\n"
299 ;; Subject
300 (let ((with-subject (plist-get info :with-subject)))
301 (when with-subject
302 (concat
303 (format "\\KOMAoption{subject}{%s}\n" with-subject)
304 (format "\\setkomavar{subject}{%s}\n\n"
305 (org-export-data (plist-get info :title) info)))))
306 ;; Letter start
307 (format "\\begin{letter}{%%\n%s}\n\n"
308 (or (plist-get info :to-address) "no address given"))
309 ;; Opening.
310 (format "\\opening{%s}\n\n" (plist-get info :opening))
311 ;; Letter body.
312 contents
313 ;; Closing.
314 (format "\n\\closing{%s}\n\n" (plist-get info :closing))
315 ;; Letter end.
316 "\\end{letter}\n\\end{document}"))
320 ;;; Commands
322 ;;;###autoload
323 (defun org-koma-letter-export-as-latex
324 (&optional async subtreep visible-only body-only ext-plist)
325 "Export current buffer as a KOMA Scrlttr2 letter.
327 If narrowing is active in the current buffer, only export its
328 narrowed part.
330 If a region is active, export that region.
332 A non-nil optional argument ASYNC means the process should happen
333 asynchronously. The resulting buffer should be accessible
334 through the `org-export-stack' interface.
336 When optional argument SUBTREEP is non-nil, export the sub-tree
337 at point, extracting information from the headline properties
338 first.
340 When optional argument VISIBLE-ONLY is non-nil, don't export
341 contents of hidden elements.
343 When optional argument BODY-ONLY is non-nil, only write code
344 between \"\\begin{letter}\" and \"\\end{letter}\".
346 EXT-PLIST, when provided, is a property list with external
347 parameters overriding Org default settings, but still inferior to
348 file-local settings.
350 Export is done in a buffer named \"*Org KOMA-LETTER Export*\". It
351 will be displayed if `org-export-show-temporary-export-buffer' is
352 non-nil."
353 (interactive)
354 (if async
355 (org-export-async-start
356 (lambda (output)
357 (with-current-buffer (get-buffer-create "*Org KOMA-LETTER Export*")
358 (erase-buffer)
359 (insert output)
360 (goto-char (point-min))
361 (LaTeX-mode)
362 (org-export-add-to-stack (current-buffer) 'koma-letter)))
363 `(org-export-as 'koma-letter ,subtreep ,visible-only ,body-only
364 ',ext-plist))
365 (let ((outbuf (org-export-to-buffer
366 'koma-letter "*Org KOMA-LETTER Export*"
367 subtreep visible-only body-only ext-plist)))
368 (with-current-buffer outbuf (LaTeX-mode))
369 (when org-export-show-temporary-export-buffer
370 (switch-to-buffer-other-window outbuf)))))
372 ;;;###autoload
373 (defun org-koma-letter-export-to-latex
374 (&optional async subtreep visible-only body-only ext-plist)
375 "Export current buffer as a KOMA Scrlttr2 letter (tex).
377 If narrowing is active in the current buffer, only export its
378 narrowed part.
380 If a region is active, export that region.
382 A non-nil optional argument ASYNC means the process should happen
383 asynchronously. The resulting file should be accessible through
384 the `org-export-stack' interface.
386 When optional argument SUBTREEP is non-nil, export the sub-tree
387 at point, extracting information from the headline properties
388 first.
390 When optional argument VISIBLE-ONLY is non-nil, don't export
391 contents of hidden elements.
393 When optional argument BODY-ONLY is non-nil, only write code
394 between \"\\begin{letter}\" and \"\\end{letter}\".
396 EXT-PLIST, when provided, is a property list with external
397 parameters overriding Org default settings, but still inferior to
398 file-local settings.
400 When optional argument PUB-DIR is set, use it as the publishing
401 directory.
403 Return output file's name."
404 (interactive)
405 (let ((outfile (org-export-output-file-name ".tex" subtreep)))
406 (if async
407 (org-export-async-start
408 (lambda (f) (org-export-add-to-stack f 'koma-letter))
409 `(expand-file-name
410 (org-export-to-file
411 'koma-letter ,outfile ,subtreep ,visible-only ,body-only
412 ',ext-plist)))
413 (org-export-to-file
414 'koma-letter outfile subtreep visible-only body-only ext-plist))))
416 ;;;###autoload
417 (defun org-koma-letter-export-to-pdf
418 (&optional async subtreep visible-only body-only ext-plist)
419 "Export current buffer as a KOMA Scrlttr2 letter (pdf).
421 If narrowing is active in the current buffer, only export its
422 narrowed part.
424 If a region is active, export that region.
426 A non-nil optional argument ASYNC means the process should happen
427 asynchronously. The resulting file should be accessible through
428 the `org-export-stack' interface.
430 When optional argument SUBTREEP is non-nil, export the sub-tree
431 at point, extracting information from the headline properties
432 first.
434 When optional argument VISIBLE-ONLY is non-nil, don't export
435 contents of hidden elements.
437 When optional argument BODY-ONLY is non-nil, only write code
438 between \"\\begin{letter}\" and \"\\end{letter}\".
440 EXT-PLIST, when provided, is a property list with external
441 parameters overriding Org default settings, but still inferior to
442 file-local settings.
444 Return PDF file's name."
445 (interactive)
446 (if async
447 (let ((outfile (org-export-output-file-name ".tex" subtreep)))
448 (org-export-async-start
449 (lambda (f) (org-export-add-to-stack f 'koma-letter))
450 `(expand-file-name
451 (org-latex-compile
452 (org-export-to-file
453 'koma-letter ,outfile ,subtreep ,visible-only ,body-only
454 ',ext-plist)))))
455 (org-latex-compile
456 (org-koma-letter-export-to-latex
457 nil subtreep visible-only body-only ext-plist))))
460 (provide 'ox-koma-letter)
461 ;;; ox-koma-letter.el ends here