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