ox-beamer/latex/koma-letter: Fix missing newlines in header
[org-mode.git] / contrib / lisp / ox-koma-letter.el
blob0be0be92b23cbad146026db3250cfdca01131f4d
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-closing "See you soon,"
84 "Koma-Letter's closing, as a string."
85 :group 'org-export-koma-letter
86 :type 'string)
88 (defcustom org-koma-letter-from-address "Somewhere \\ Over the rainbow."
89 "Sender's address, as a string."
90 :group 'org-export-koma-letter
91 :type 'string)
93 (defcustom org-koma-letter-opening "Dear Sir,"
94 "Letter's opening, as a string."
95 :group 'org-export-koma-letter
96 :type 'string)
98 (defcustom org-koma-letter-phone-number "00-00-00-00"
99 "Sender's phone number, as a string."
100 :group 'org-export-koma-letter
101 :type 'string)
103 (defcustom org-koma-letter-signature "\\usekomavar{fromname}"
104 "String used as the signature."
105 :group 'org-export-koma-letter
106 :type 'string)
109 ;;; Define Back-End
111 (org-export-define-derived-backend 'koma-letter 'latex
112 :options-alist
113 '((:closing "CLOSING" nil org-koma-letter-closing)
114 (:from-address "FROM_ADDRESS" nil org-koma-letter-from-address newline)
115 (:lco "LCO" nil org-koma-letter-class-option-file)
116 (:opening "OPENING" nil org-koma-letter-opening)
117 (:phone-number "PHONE_NUMBER" nil org-koma-letter-phone-number)
118 (:signature "SIGNATURE" nil nil newline)
119 (:to-address "TO_ADDRESS" nil nil newline))
120 :translate-alist '((export-block . org-koma-letter-export-block)
121 (export-snippet . org-koma-letter-export-snippet)
122 (keyword . org-koma-letter-keyword)
123 (template . org-koma-letter-template))
124 :menu-entry
125 '(?k "Export with KOMA Scrlttr2"
126 ((?K "As LaTeX buffer" org-koma-letter-export-as-latex)
127 (?k "As LaTeX file" org-koma-letter-export-to-latex)
128 (?p "As PDF file" org-koma-letter-export-to-pdf)
129 (?O "As PDF file and open"
130 (lambda (a s v b)
131 (if a (org-koma-letter-export-to-pdf t s v b)
132 (org-open-file (org-koma-letter-export-to-pdf nil s v b))))))))
135 ;;; Transcode Functions
137 ;;;; Export Block
139 (defun org-koma-letter-export-block (export-block contents info)
140 "Transcode an EXPORT-BLOCK element into KOMA Scrlttr2 code.
141 CONTENTS is nil. INFO is a plist used as a communication
142 channel."
143 (when (member (org-element-property :type export-block) '("KOMA-LETTER" "LATEX"))
144 (org-remove-indentation (org-element-property :value export-block))))
146 ;;;; Export Snippet
148 (defun org-koma-letter-export-snippet (export-snippet contents info)
149 "Transcode an EXPORT-SNIPPET object into KOMA Scrlttr2 code.
150 CONTENTS is nil. INFO is a plist used as a communication
151 channel."
152 (when (memq (org-export-snippet-backend export-snippet) '(latex koma-letter))
153 (org-element-property :value export-snippet)))
155 ;;;; Keyword
157 (defun org-koma-letter-keyword (keyword contents info)
158 "Transcode a KEYWORD element into KOMA Scrlttr2 code.
159 CONTENTS is nil. INFO is a plist used as a communication
160 channel."
161 (let ((key (org-element-property :key keyword))
162 (value (org-element-property :value keyword)))
163 ;; Handle specifically BEAMER and TOC (headlines only) keywords.
164 ;; Otherwise, fallback to `latex' back-end.
165 (if (equal key "KOMA-LETTER") value
166 (org-export-with-backend 'latex keyword contents info))))
168 ;;;; Template
170 (defun org-koma-letter-template (contents info)
171 "Return complete document string after KOMA Scrlttr2 conversion.
172 CONTENTS is the transcoded contents string. INFO is a plist
173 holding export options."
174 (concat
175 ;; Time-stamp.
176 (and (plist-get info :time-stamp-file)
177 (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
178 ;; Document class and packages.
179 (let* ((class (plist-get info :latex-class))
180 (class-options (plist-get info :latex-class-options))
181 (header (nth 1 (assoc class org-latex-classes)))
182 (document-class-string
183 (and (stringp header)
184 (if (not class-options) header
185 (replace-regexp-in-string
186 "^[ \t]*\\\\documentclass\\(\\(\\[[^]]*\\]\\)?\\)"
187 class-options header t nil 1)))))
188 (if (not document-class-string)
189 (user-error "Unknown LaTeX class `%s'")
190 (org-latex-guess-babel-language
191 (org-latex-guess-inputenc
192 (org-element-normalize-string
193 (org-splice-latex-header
194 document-class-string
195 org-latex-default-packages-alist ; Defined in org.el.
196 org-latex-packages-alist nil ; Defined in org.el.
197 (concat (org-element-normalize-string
198 (plist-get info :latex-header))
199 (plist-get info :latex-header-extra)))))
200 info)))
201 ;; Define "From" data.
202 (format "\\setkomavar{fromname}{%s}\n"
203 (org-export-data (plist-get info :author) info))
204 (format "\\setkomavar{fromaddress}{%s}\n" (plist-get info :from-address))
205 (format "\\setkomavar{signature}{%s}\n" (plist-get info :signature))
206 (format "\\setkomavar{fromemail}{%s}\n"
207 (org-export-data (plist-get info :email) info))
208 (format "\\setkomavar{fromphone}{%s}\n" (plist-get info :phone-number))
209 ;; Date.
210 (format "\\date{%s}\n" (org-export-data (org-export-get-date info) info))
211 ;; Letter Class Option File
212 (format "\\LoadLetterOption{%s}\n" (plist-get info :lco))
213 ;; Letter start.
214 "\\begin{document}\n\n"
215 (format "\\setkomavar{subject}{%s}\n\n"
216 (org-export-data (plist-get info :title) info))
217 (format "\\begin{letter}{%%\n%s}\n\n"
218 (or (plist-get info :to-address) "no address given"))
219 ;; Opening.
220 (format "\\opening{%s}\n\n" (plist-get info :opening))
221 ;; Letter body.
222 contents
223 ;; Closing.
224 (format "\n\\closing{%s}\n\n" (plist-get info :closing))
225 ;; Letter end.
226 "\\end{letter}\n\\end{document}"))
230 ;;; Commands
232 ;;;###autoload
233 (defun org-koma-letter-export-as-latex
234 (&optional async subtreep visible-only body-only ext-plist)
235 "Export current buffer as a KOMA Scrlttr2 letter.
237 If narrowing is active in the current buffer, only export its
238 narrowed part.
240 If a region is active, export that region.
242 A non-nil optional argument ASYNC means the process should happen
243 asynchronously. The resulting buffer should be accessible
244 through the `org-export-stack' interface.
246 When optional argument SUBTREEP is non-nil, export the sub-tree
247 at point, extracting information from the headline properties
248 first.
250 When optional argument VISIBLE-ONLY is non-nil, don't export
251 contents of hidden elements.
253 When optional argument BODY-ONLY is non-nil, only write code
254 between \"\\begin{letter}\" and \"\\end{letter}\".
256 EXT-PLIST, when provided, is a property list with external
257 parameters overriding Org default settings, but still inferior to
258 file-local settings.
260 Export is done in a buffer named \"*Org KOMA-LETTER Export*\". It
261 will be displayed if `org-export-show-temporary-export-buffer' is
262 non-nil."
263 (interactive)
264 (if async
265 (org-export-async-start
266 (lambda (output)
267 (with-current-buffer (get-buffer-create "*Org KOMA-LETTER Export*")
268 (erase-buffer)
269 (insert output)
270 (goto-char (point-min))
271 (LaTeX-mode)
272 (org-export-add-to-stack (current-buffer) 'koma-letter)))
273 `(org-export-as 'koma-letter ,subtreep ,visible-only ,body-only
274 ',ext-plist))
275 (let ((outbuf (org-export-to-buffer
276 'koma-letter "*Org KOMA-LETTER Export*"
277 subtreep visible-only body-only ext-plist)))
278 (with-current-buffer outbuf (LaTeX-mode))
279 (when org-export-show-temporary-export-buffer
280 (switch-to-buffer-other-window outbuf)))))
282 ;;;###autoload
283 (defun org-koma-letter-export-to-latex
284 (&optional async subtreep visible-only body-only ext-plist)
285 "Export current buffer as a KOMA Scrlttr2 letter (tex).
287 If narrowing is active in the current buffer, only export its
288 narrowed part.
290 If a region is active, export that region.
292 A non-nil optional argument ASYNC means the process should happen
293 asynchronously. The resulting file should be accessible through
294 the `org-export-stack' interface.
296 When optional argument SUBTREEP is non-nil, export the sub-tree
297 at point, extracting information from the headline properties
298 first.
300 When optional argument VISIBLE-ONLY is non-nil, don't export
301 contents of hidden elements.
303 When optional argument BODY-ONLY is non-nil, only write code
304 between \"\\begin{letter}\" and \"\\end{letter}\".
306 EXT-PLIST, when provided, is a property list with external
307 parameters overriding Org default settings, but still inferior to
308 file-local settings.
310 When optional argument PUB-DIR is set, use it as the publishing
311 directory.
313 Return output file's name."
314 (interactive)
315 (let ((outfile (org-export-output-file-name ".tex" subtreep)))
316 (if async
317 (org-export-async-start
318 (lambda (f) (org-export-add-to-stack f 'koma-letter))
319 `(expand-file-name
320 (org-export-to-file
321 'koma-letter ,outfile ,subtreep ,visible-only ,body-only
322 ',ext-plist)))
323 (org-export-to-file
324 'koma-letter outfile subtreep visible-only body-only ext-plist))))
326 ;;;###autoload
327 (defun org-koma-letter-export-to-pdf
328 (&optional async subtreep visible-only body-only ext-plist)
329 "Export current buffer as a KOMA Scrlttr2 letter (pdf).
331 If narrowing is active in the current buffer, only export its
332 narrowed part.
334 If a region is active, export that region.
336 A non-nil optional argument ASYNC means the process should happen
337 asynchronously. The resulting file should be accessible through
338 the `org-export-stack' interface.
340 When optional argument SUBTREEP is non-nil, export the sub-tree
341 at point, extracting information from the headline properties
342 first.
344 When optional argument VISIBLE-ONLY is non-nil, don't export
345 contents of hidden elements.
347 When optional argument BODY-ONLY is non-nil, only write code
348 between \"\\begin{letter}\" and \"\\end{letter}\".
350 EXT-PLIST, when provided, is a property list with external
351 parameters overriding Org default settings, but still inferior to
352 file-local settings.
354 Return PDF file's name."
355 (interactive)
356 (if async
357 (let ((outfile (org-export-output-file-name ".tex" subtreep)))
358 (org-export-async-start
359 (lambda (f) (org-export-add-to-stack f 'koma-letter))
360 `(expand-file-name
361 (org-latex-compile
362 (org-export-to-file
363 'koma-letter ,outfile ,subtreep ,visible-only ,body-only
364 ',ext-plist)))))
365 (org-latex-compile
366 (org-koma-letter-export-to-latex
367 nil subtreep visible-only body-only ext-plist))))
370 (provide 'ox-koma-letter)
371 ;;; ox-koma-letter.el ends here