ox-koma-letter.el: No default opening and closing line.
[org-mode/org-kjn.git] / contrib / lisp / ox-koma-letter.el
blob37ff9039a031439227a28ecddada82bf831ca239
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 t
134 "Print foldmarks."
135 :group 'org-export-koma-letter
136 :type 'boolean)
138 (defcustom org-koma-letter-use-phone t
139 "Print sender's phone number."
140 :group 'org-export-koma-letter
141 :type 'boolean)
143 (defcustom org-koma-letter-use-email t
144 "Print sender's email address."
145 :group 'org-export-koma-letter
146 :type 'boolean)
148 (defcustom org-koma-letter-use-place t
149 "Print the letter's place next to the date."
150 :group 'org-export-koma-letter
151 :type 'boolean)
154 ;;; Define Back-End
156 (org-export-define-derived-backend 'koma-letter 'latex
157 :options-alist
158 '((:lco "LCO" nil org-koma-letter-class-option-file)
159 (:sender "SENDER" nil org-koma-letter-sender newline)
160 (:from-address "FROM_ADDRESS" nil org-koma-letter-from-address newline)
161 (:phone-number "PHONE_NUMBER" nil org-koma-letter-phone-number)
162 (:email "EMAIL" nil org-koma-letter-email)
163 (:to-address "TO_ADDRESS" nil nil newline)
164 (:place "PLACE" nil org-koma-letter-place)
165 (:opening "OPENING" nil org-koma-letter-opening)
166 (:closing "CLOSING" nil org-koma-letter-closing)
167 (:signature "SIGNATURE" nil org-koma-letter-signature newline)
169 (:with-backaddress nil "backaddress" org-koma-letter-use-backaddress)
170 (:with-foldmarks nil "foldmarks" org-koma-letter-use-foldmarks)
171 (:with-phone nil "phone" org-koma-letter-use-phone)
172 (:with-email nil "email" org-koma-letter-use-email)
173 (:with-place nil "place" org-koma-letter-use-place)
174 (:with-subject nil "subject" org-koma-letter-use-subject))
175 :translate-alist '((export-block . org-koma-letter-export-block)
176 (export-snippet . org-koma-letter-export-snippet)
177 (keyword . org-koma-letter-keyword)
178 (template . org-koma-letter-template))
179 :menu-entry
180 '(?k "Export with KOMA Scrlttr2"
181 ((?L "As LaTeX buffer" org-koma-letter-export-as-latex)
182 (?l "As LaTeX file" org-koma-letter-export-to-latex)
183 (?p "As PDF file" org-koma-letter-export-to-pdf)
184 (?o "As PDF file and open"
185 (lambda (a s v b)
186 (if a (org-koma-letter-export-to-pdf t s v b)
187 (org-open-file (org-koma-letter-export-to-pdf nil s v b))))))))
190 ;;; Transcode Functions
192 ;;;; Export Block
194 (defun org-koma-letter-export-block (export-block contents info)
195 "Transcode an EXPORT-BLOCK element into KOMA Scrlttr2 code.
196 CONTENTS is nil. INFO is a plist used as a communication
197 channel."
198 (when (member (org-element-property :type export-block) '("KOMA-LETTER" "LATEX"))
199 (org-remove-indentation (org-element-property :value export-block))))
201 ;;;; Export Snippet
203 (defun org-koma-letter-export-snippet (export-snippet contents info)
204 "Transcode an EXPORT-SNIPPET object into KOMA Scrlttr2 code.
205 CONTENTS is nil. INFO is a plist used as a communication
206 channel."
207 (when (memq (org-export-snippet-backend export-snippet) '(latex koma-letter))
208 (org-element-property :value export-snippet)))
210 ;;;; Keyword
212 (defun org-koma-letter-keyword (keyword contents info)
213 "Transcode a KEYWORD element into KOMA Scrlttr2 code.
214 CONTENTS is nil. INFO is a plist used as a communication
215 channel."
216 (let ((key (org-element-property :key keyword))
217 (value (org-element-property :value keyword)))
218 ;; Handle specifically BEAMER and TOC (headlines only) keywords.
219 ;; Otherwise, fallback to `latex' back-end.
220 (if (equal key "KOMA-LETTER") value
221 (org-export-with-backend 'latex keyword contents info))))
223 ;;;; Template
225 (defun org-koma-letter-template (contents info)
226 "Return complete document string after KOMA Scrlttr2 conversion.
227 CONTENTS is the transcoded contents string. INFO is a plist
228 holding export options."
229 (concat
230 ;; Time-stamp.
231 (and (plist-get info :time-stamp-file)
232 (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
233 ;; Document class and packages.
234 (let ((class (plist-get info :latex-class))
235 (class-options (plist-get info :latex-class-options)))
236 (org-element-normalize-string
237 (let* ((header (nth 1 (assoc class org-latex-classes)))
238 (document-class-string
239 (and (stringp header)
240 (if (not class-options) header
241 (replace-regexp-in-string
242 "^[ \t]*\\\\documentclass\\(\\(\\[[^]]*\\]\\)?\\)"
243 class-options header t nil 1)))))
244 (if (not document-class-string)
245 (user-error "Unknown LaTeX class `%s'")
246 (org-latex-guess-babel-language
247 (org-latex-guess-inputenc
248 (org-splice-latex-header
249 document-class-string
250 org-latex-default-packages-alist ; defined in org.el
251 org-latex-packages-alist nil ; defined in org.el
252 (concat (plist-get info :latex-header)
253 (plist-get info :latex-header-extra))))
254 info)))))
255 (let ((lco (plist-get info :lco))
256 (sender (plist-get info :sender))
257 (from-address (plist-get info :from-address))
258 (phone-number (plist-get info :phone-number))
259 (email (plist-get info :email))
260 (signature (plist-get info :signature)))
261 (concat
262 ;; Letter Class Option File
263 (when lco
264 (let ((lco-files (split-string lco " "))
265 (lco-def ""))
266 (dolist (lco-file lco-files lco-def)
267 (setq lco-def (format "%s\\LoadLetterOption{%s}\n" lco-def lco-file)))
268 lco-def))
269 ;; Define "From" data.
270 (when sender (format "\\setkomavar{fromname}{%s}\n" sender))
271 (when from-address (format "\\setkomavar{fromaddress}{%s}\n" from-address))
272 (when phone-number (format "\\setkomavar{fromphone}{%s}\n" phone-number))
273 (when email (format "\\setkomavar{fromemail}{%s}\n" email))
274 (when signature (format "\\setkomavar{signature}{%s}\n" signature))))
275 ;; Date.
276 (format "\\date{%s}\n" (org-export-data (org-export-get-date info) info))
277 ;; Place
278 (let ((with-place (plist-get info :with-place))
279 (place (plist-get info :place)))
280 (when (or place (not with-place))
281 (format "\\setkomavar{place}{%s}\n" (if with-place place ""))))
282 ;; KOMA options
283 (let ((with-backaddress (plist-get info :with-backaddress))
284 (with-foldmarks (plist-get info :with-foldmarks))
285 (with-phone (plist-get info :with-phone))
286 (with-email (plist-get info :with-email)))
287 (concat
288 (format "\\KOMAoption{backaddress}{%s}\n" (if with-backaddress "true" "false"))
289 (format "\\KOMAoption{foldmarks}{%s}\n" (if with-foldmarks "true" "false"))
290 (format "\\KOMAoption{fromphone}{%s}\n" (if with-phone "true" "false"))
291 (format "\\KOMAoption{fromemail}{%s}\n" (if with-email "true" "false"))))
292 ;; Document start
293 "\\begin{document}\n\n"
294 ;; Subject
295 (let ((with-subject (plist-get info :with-subject)))
296 (when with-subject
297 (concat
298 (format "\\KOMAoption{subject}{%s}\n" with-subject)
299 (format "\\setkomavar{subject}{%s}\n\n"
300 (org-export-data (plist-get info :title) info)))))
301 ;; Letter start
302 (format "\\begin{letter}{%%\n%s}\n\n"
303 (or (plist-get info :to-address) "no address given"))
304 ;; Opening.
305 (format "\\opening{%s}\n\n" (plist-get info :opening))
306 ;; Letter body.
307 contents
308 ;; Closing.
309 (format "\n\\closing{%s}\n\n" (plist-get info :closing))
310 ;; Letter end.
311 "\\end{letter}\n\\end{document}"))
315 ;;; Commands
317 ;;;###autoload
318 (defun org-koma-letter-export-as-latex
319 (&optional async subtreep visible-only body-only ext-plist)
320 "Export current buffer as a KOMA Scrlttr2 letter.
322 If narrowing is active in the current buffer, only export its
323 narrowed part.
325 If a region is active, export that region.
327 A non-nil optional argument ASYNC means the process should happen
328 asynchronously. The resulting buffer should be accessible
329 through the `org-export-stack' interface.
331 When optional argument SUBTREEP is non-nil, export the sub-tree
332 at point, extracting information from the headline properties
333 first.
335 When optional argument VISIBLE-ONLY is non-nil, don't export
336 contents of hidden elements.
338 When optional argument BODY-ONLY is non-nil, only write code
339 between \"\\begin{letter}\" and \"\\end{letter}\".
341 EXT-PLIST, when provided, is a property list with external
342 parameters overriding Org default settings, but still inferior to
343 file-local settings.
345 Export is done in a buffer named \"*Org KOMA-LETTER Export*\". It
346 will be displayed if `org-export-show-temporary-export-buffer' is
347 non-nil."
348 (interactive)
349 (if async
350 (org-export-async-start
351 (lambda (output)
352 (with-current-buffer (get-buffer-create "*Org KOMA-LETTER Export*")
353 (erase-buffer)
354 (insert output)
355 (goto-char (point-min))
356 (LaTeX-mode)
357 (org-export-add-to-stack (current-buffer) 'koma-letter)))
358 `(org-export-as 'koma-letter ,subtreep ,visible-only ,body-only
359 ',ext-plist))
360 (let ((outbuf (org-export-to-buffer
361 'koma-letter "*Org KOMA-LETTER Export*"
362 subtreep visible-only body-only ext-plist)))
363 (with-current-buffer outbuf (LaTeX-mode))
364 (when org-export-show-temporary-export-buffer
365 (switch-to-buffer-other-window outbuf)))))
367 ;;;###autoload
368 (defun org-koma-letter-export-to-latex
369 (&optional async subtreep visible-only body-only ext-plist)
370 "Export current buffer as a KOMA Scrlttr2 letter (tex).
372 If narrowing is active in the current buffer, only export its
373 narrowed part.
375 If a region is active, export that region.
377 A non-nil optional argument ASYNC means the process should happen
378 asynchronously. The resulting file should be accessible through
379 the `org-export-stack' interface.
381 When optional argument SUBTREEP is non-nil, export the sub-tree
382 at point, extracting information from the headline properties
383 first.
385 When optional argument VISIBLE-ONLY is non-nil, don't export
386 contents of hidden elements.
388 When optional argument BODY-ONLY is non-nil, only write code
389 between \"\\begin{letter}\" and \"\\end{letter}\".
391 EXT-PLIST, when provided, is a property list with external
392 parameters overriding Org default settings, but still inferior to
393 file-local settings.
395 When optional argument PUB-DIR is set, use it as the publishing
396 directory.
398 Return output file's name."
399 (interactive)
400 (let ((outfile (org-export-output-file-name ".tex" subtreep)))
401 (if async
402 (org-export-async-start
403 (lambda (f) (org-export-add-to-stack f 'koma-letter))
404 `(expand-file-name
405 (org-export-to-file
406 'koma-letter ,outfile ,subtreep ,visible-only ,body-only
407 ',ext-plist)))
408 (org-export-to-file
409 'koma-letter outfile subtreep visible-only body-only ext-plist))))
411 ;;;###autoload
412 (defun org-koma-letter-export-to-pdf
413 (&optional async subtreep visible-only body-only ext-plist)
414 "Export current buffer as a KOMA Scrlttr2 letter (pdf).
416 If narrowing is active in the current buffer, only export its
417 narrowed part.
419 If a region is active, export that region.
421 A non-nil optional argument ASYNC means the process should happen
422 asynchronously. The resulting file should be accessible through
423 the `org-export-stack' interface.
425 When optional argument SUBTREEP is non-nil, export the sub-tree
426 at point, extracting information from the headline properties
427 first.
429 When optional argument VISIBLE-ONLY is non-nil, don't export
430 contents of hidden elements.
432 When optional argument BODY-ONLY is non-nil, only write code
433 between \"\\begin{letter}\" and \"\\end{letter}\".
435 EXT-PLIST, when provided, is a property list with external
436 parameters overriding Org default settings, but still inferior to
437 file-local settings.
439 Return PDF file's name."
440 (interactive)
441 (if async
442 (let ((outfile (org-export-output-file-name ".tex" subtreep)))
443 (org-export-async-start
444 (lambda (f) (org-export-add-to-stack f 'koma-letter))
445 `(expand-file-name
446 (org-latex-compile
447 (org-export-to-file
448 'koma-letter ,outfile ,subtreep ,visible-only ,body-only
449 ',ext-plist)))))
450 (org-latex-compile
451 (org-koma-letter-export-to-latex
452 nil subtreep visible-only body-only ext-plist))))
455 (provide 'ox-koma-letter)
456 ;;; ox-koma-letter.el ends here