emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
blob 069d45830b233e8a3207952595436b93be844c2c 14409 bytes (raw)
name: contrib/lisp/ox-koma-letter.el 	 # note: path name is non-authoritative(*)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
 
;;; ox-koma-letter.el --- KOMA Scrlttr2 Back-End for Org Export Engine

;; Copyright (C) 2007-2012  Free Software Foundation, Inc.

;; Author: Nicolas Goaziou <n.goaziou AT gmail DOT com>
;;         Alan Schmitt <alan.schmitt AT polytechnique DOT org>
;; Keywords: org, wp, tex

;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.

;;; Commentary:
;;
;; This library implements a KOMA Scrlttr2 back-end, derived from the
;; LaTeX one.
;;
;; Depending on the desired output format, three commands are provided
;; for export: `org-koma-letter-export-as-latex' (temporary buffer),
;; `org-koma-letter-export-to-latex' ("tex" file) and
;; `org-koma-letter-export-to-pdf' ("pdf" file).
;;
;; On top of buffer keywords supported by `latex' back-end (see
;; `org-latex-options-alist'), this back-end introduces the following
;; keywords: "CLOSING" (see `org-koma-letter-closing'), "FROM_ADDRESS"
;; (see `org-koma-letter-from-address'), "LCO" (see
;; `org-koma-letter-class-option-file'), "OPENING" (see
;; `org-koma-letter-opening'), "PHONE_NUMBER" (see
;; `org-koma-letter-phone-number'), "SIGNATURE" (see
;; `org-koma-letter-signature') and "TO_ADDRESS".
;;
;; You will need to add an appropriate association in
;; `org-latex-classes' in order to use the KOMA Scrlttr2 class.  For
;; example, you can use the following code:
;;
;;   (add-to-list 'org-latex-classes
;;                '("my-letter"
;;                  "\\documentclass\[%
;;   DIV=14,
;;   fontsize=12pt,
;;   parskip=half,
;;   subject=titled,
;;   backaddress=false,
;;   fromalign=left,
;;   fromemail=true,
;;   fromphone=true\]\{scrlttr2\}
;;   \[DEFAULT-PACKAGES]
;;   \[PACKAGES]
;;   \[EXTRA]"))
;;
;; Then, in your Org document, be sure to require the proper class
;; with :
;;
;;    #+LATEX_CLASS: my-letter


;;; Code:

(require 'ox-latex)

\f
;;; User-Configurable Variables

(defgroup org-export-koma-letter nil
  "Options for exporting to KOMA scrlttr2 class in LaTeX export."
  :tag "Org Koma-Letter"
  :group 'org-export
  :version "24.2")

(defcustom org-koma-letter-class-option-file "NF"
  "Letter Class Option File."
  :group 'org-export-koma-letter
  :type 'string)

(defcustom org-koma-letter-closing "See you soon,"
  "Koma-Letter's closing, as a string."
  :group 'org-export-koma-letter
  :type 'string)

(defcustom org-koma-letter-from-address "Somewhere \\ Over the rainbow."
  "Sender's address, as a string."
  :group 'org-export-koma-letter
  :type 'string)

(defcustom org-koma-letter-from-bank ""
  "Sender's bank account, as a string."
  :group 'org-export-koma-letter
  :type 'string)

(defcustom org-koma-letter-from-fax ""
  "Sender's fax number, as a string."
  :group 'org-export-koma-letter
  :type 'string)

(defcustom org-koma-letter-from-url ""
  "Sender's url, as a string."
  :group 'org-export-koma-letter
  :type 'string)



(defcustom org-koma-letter-opening "Dear Sir,"
  "Letter's opening, as a string."
  :group 'org-export-koma-letter
  :type 'string)

(defcustom org-koma-letter-phone-number "00-00-00-00"
  "Sender's phone number, as a string."
  :group 'org-export-koma-letter
  :type 'string)

(defcustom org-koma-letter-signature "\\usekomavar{fromname}"
  "String used as the signature."
  :group 'org-export-koma-letter
  :type 'string)


;;; Define Back-End

(org-export-define-derived-backend koma-letter latex
  :options-alist
  ((:after-closing "AFTER_CLOSING" nil "" newline)
   (:after-letter "AFTER_LETTER" nil "" newline)
   (:closing "CLOSING" nil org-koma-letter-closing)
   (:from-address "FROM_ADDRESS" nil org-koma-letter-from-address newline)
   (:from-bank "FROM_BANK" nil org-koma-letter-from-bank t)
   (:from-fax "FROM_FAX" nil org-koma-letter-from-fax t)
   (:from-url "FROM_FAX" nil org-koma-letter-from-url t)
   (:invoice "INVOICE" nil "" t)
   (:my-ref "MY_REF" nil "" t)
   (:your-ref "YOUR_REF" nil "" t)
   (:lco "LCO" nil org-koma-letter-class-option-file)
   (:opening "OPENING" nil org-koma-letter-opening)
   (:phone-number "PHONE_NUMBER" nil org-koma-letter-phone-number)
   (:signature "SIGNATURE" nil user-full-name newline)
   (:to-address "TO_ADDRESS" nil nil newline)
   )
  :translate-alist ((export-block . org-koma-letter-export-block)
                    (export-snippet . org-koma-letter-export-snippet)
                    (keyword . org-koma-letter-keyword)
                    (template . org-koma-letter-template))
  :menu-entry
  (?k "Export with KOMA Scrlttr2"
      ((?L "As LaTeX buffer" org-koma-letter-export-as-latex)
       (?l "As LaTeX file" org-koma-letter-export-to-latex)
       (?p "As PDF file" org-koma-letter-export-to-pdf)
       (?o "As PDF file and open"
           (lambda (a s v b)
             (if a (org-koma-letter-export-to-pdf t s v b)
	       (org-open-file (org-koma-letter-export-to-pdf nil s v b))))))))


;;; Transcode Functions

;;;; Export Block

(defun org-koma-letter-export-block (export-block contents info)
  "Transcode an EXPORT-BLOCK element into KOMA Scrlttr2 code.
CONTENTS is nil.  INFO is a plist used as a communication
channel."
  (when (member (org-element-property :type export-block) '("KOMA-LETTER" "LATEX"))
    (org-remove-indentation (org-element-property :value export-block))))

;;;; Export Snippet

(defun org-koma-letter-export-snippet (export-snippet contents info)
  "Transcode an EXPORT-SNIPPET object into KOMA Scrlttr2 code.
CONTENTS is nil.  INFO is a plist used as a communication
channel."
  (when (memq (org-export-snippet-backend export-snippet) '(latex koma-letter))
    (org-element-property :value export-snippet)))

;;;; Keyword

(defun org-koma-letter-keyword (keyword contents info)
  "Transcode a KEYWORD element into KOMA Scrlttr2 code.
CONTENTS is nil.  INFO is a plist used as a communication
channel."
  (let ((key (org-element-property :key keyword))
        (value (org-element-property :value keyword)))
    ;; Handle specifically BEAMER and TOC (headlines only) keywords.
    ;; Otherwise, fallback to `latex' back-end.
    (if (equal key "KOMA-LETTER") value
      (org-export-with-backend 'latex keyword contents info))))

;;;; Template

(defun org-koma-letter-template (contents info)
  "Return complete document string after KOMA Scrlttr2 conversion.
CONTENTS is the transcoded contents string.  INFO is a plist
holding export options."
  (concat
   ;; Time-stamp.
   (and (plist-get info :time-stamp-file)
        (format-time-string "%% Created %Y-%m-%d %a %H:%M\n"))
   ;; Document class and packages.
   (let ((class (plist-get info :latex-class))
         (class-options (plist-get info :latex-class-options)))
     (org-element-normalize-string
      (let* ((header (nth 1 (assoc class org-latex-classes)))
             (document-class-string
              (and (stringp header)
                   (if (not class-options) header
		     (replace-regexp-in-string
		      "^[ \t]*\\\\documentclass\\(\\(\\[[^]]*\\]\\)?\\)"
		      class-options header t nil 1)))))
        (if (not document-class-string)
	    (user-error "Unknown LaTeX class `%s'")
          (org-latex-guess-babel-language
           (org-latex-guess-inputenc
            (org-splice-latex-header
             document-class-string
             org-latex-default-packages-alist ; defined in org.el
             org-latex-packages-alist nil     ; defined in org.el
             (plist-get info :latex-header-extra)))
           info)))))
   ;; Define "From" data.
   (format "\\setkomavar{fromname}{%s}\n"
           (org-export-data (plist-get info :author) info))
   (format "\\setkomavar{fromaddress}{%s}\n" (plist-get info :from-address))
   (format "\\setkomavar{signature}{%s}\n" (plist-get info :signature))
   (format "\\setkomavar{fromemail}{%s}\n"
           (org-export-data (plist-get info :email) info))
   (format "\\setkomavar{frombank}{%s}\n"
           (org-export-data (plist-get info :from-bank) info))
   (format "\\setkomavar{fromphone}{%s}\n" (plist-get info :phone-number))
   (format "\\setkomavar{frombank}{%s}\n" (plist-get info :from-bank))
   (format "\\setkomavar{fromfax}{%s}\n" (plist-get info :from-fax))
   (format "\\setkomavar{fromurl}{%s}\n" (plist-get info :from-url))
   (format "\\setkomavar{myref}{%s}\n" (plist-get info :my-ref))
   (format "\\setkomavar{yourref}{%s}\n" (plist-get info :your-ref))
   (format "\\setkomavar{invoice}{%s}\n" (plist-get info :invoice))
   ;; Date.
   (format "\\date{%s}\n" (org-export-data (plist-get info :date) info))
   ;; Letter Class Option File
   (format "\\LoadLetterOption{%s}\n" (plist-get info :lco))
   ;; Letter start.
   "\\begin{document}\n\n"
   (format "\\setkomavar{subject}{%s}\n\n"
           (org-export-data (plist-get info :title) info))
   (format "\\begin{letter}{%%\n%s}\n\n"
	   (or (plist-get info :to-address) "no address given"))
   ;; Opening.
   (format "\\opening{%s}\n\n" (plist-get info :opening))
   ;; Letter body.
   contents
   ;; Closing.
   (format "\n\\closing{%s}\n\n" (plist-get info :closing))
   (format "\n%s\n" (plist-get info :after-closing))
   ;; Letter end.
   "\\end{letter}\n\n"
   (format "%s" (plist-get info :after-letter))
   "\\end{document}"))


;;; Commands

;;;###autoload
(defun org-koma-letter-export-as-latex
  (&optional async subtreep visible-only body-only ext-plist)
  "Export current buffer as a KOMA Scrlttr2 letter.

If narrowing is active in the current buffer, only export its
narrowed part.

If a region is active, export that region.

A non-nil optional argument ASYNC means the process should happen
asynchronously.  The resulting buffer should be accessible
through the `org-export-stack' interface.

When optional argument SUBTREEP is non-nil, export the sub-tree
at point, extracting information from the headline properties
first.

When optional argument VISIBLE-ONLY is non-nil, don't export
contents of hidden elements.

When optional argument BODY-ONLY is non-nil, only write code
between \"\\begin{letter}\" and \"\\end{letter}\".

EXT-PLIST, when provided, is a property list with external
parameters overriding Org default settings, but still inferior to
file-local settings.

Export is done in a buffer named \"*Org KOMA-LETTER Export*\".  It
will be displayed if `org-export-show-temporary-export-buffer' is
non-nil."
  (interactive)
  (if async
      (org-export-async-start
	  (lambda (output)
	    (with-current-buffer (get-buffer-create "*Org KOMA-LETTER Export*")
	      (erase-buffer)
	      (insert output)
	      (goto-char (point-min))
	      (LaTeX-mode)
	      (org-export-add-to-stack (current-buffer) 'koma-letter)))
	`(org-export-as 'koma-letter ,subtreep ,visible-only ,body-only
			',ext-plist))
    (let ((outbuf (org-export-to-buffer
		   'koma-letter "*Org KOMA-LETTER Export*"
		   subtreep visible-only body-only ext-plist)))
      (with-current-buffer outbuf (LaTeX-mode))
      (when org-export-show-temporary-export-buffer
	(switch-to-buffer-other-window outbuf)))))

;;;###autoload
(defun org-koma-letter-export-to-latex
  (&optional async subtreep visible-only body-only ext-plist)
  "Export current buffer as a KOMA Scrlttr2 letter (tex).

If narrowing is active in the current buffer, only export its
narrowed part.

If a region is active, export that region.

A non-nil optional argument ASYNC means the process should happen
asynchronously.  The resulting file should be accessible through
the `org-export-stack' interface.

When optional argument SUBTREEP is non-nil, export the sub-tree
at point, extracting information from the headline properties
first.

When optional argument VISIBLE-ONLY is non-nil, don't export
contents of hidden elements.

When optional argument BODY-ONLY is non-nil, only write code
between \"\\begin{letter}\" and \"\\end{letter}\".

EXT-PLIST, when provided, is a property list with external
parameters overriding Org default settings, but still inferior to
file-local settings.

When optional argument PUB-DIR is set, use it as the publishing
directory.

Return output file's name."
  (interactive)
  (let ((outfile (org-export-output-file-name ".tex" subtreep)))
    (if async
	(org-export-async-start
	    (lambda (f) (org-export-add-to-stack f 'koma-letter))
	  `(expand-file-name
	    (org-export-to-file
	     'koma-letter ,outfile ,subtreep ,visible-only ,body-only
	     ',ext-plist)))
      (org-export-to-file
       'koma-letter outfile subtreep visible-only body-only ext-plist))))

;;;###autoload
(defun org-koma-letter-export-to-pdf
  (&optional async subtreep visible-only body-only ext-plist)
  "Export current buffer as a KOMA Scrlttr2 letter (pdf).

If narrowing is active in the current buffer, only export its
narrowed part.

If a region is active, export that region.

A non-nil optional argument ASYNC means the process should happen
asynchronously.  The resulting file should be accessible through
the `org-export-stack' interface.

When optional argument SUBTREEP is non-nil, export the sub-tree
at point, extracting information from the headline properties
first.

When optional argument VISIBLE-ONLY is non-nil, don't export
contents of hidden elements.

When optional argument BODY-ONLY is non-nil, only write code
between \"\\begin{letter}\" and \"\\end{letter}\".

EXT-PLIST, when provided, is a property list with external
parameters overriding Org default settings, but still inferior to
file-local settings.

Return PDF file's name."
  (interactive)
  (if async
      (let ((outfile (org-export-output-file-name ".tex" subtreep)))
	(org-export-async-start
	    (lambda (f) (org-export-add-to-stack f 'koma-letter))
	  `(expand-file-name
	    (org-latex-compile
	     (org-export-to-file
	      'koma-letter ,outfile ,subtreep ,visible-only ,body-only
	      ',ext-plist)))))
    (org-latex-compile
     (org-koma-letter-export-to-latex
      nil subtreep visible-only body-only ext-plist))))


(provide 'ox-koma-letter)
;;; ox-koma-letter.el ends here

debug log:

solving 069d458 ...
found 069d458 in https://yhetil.org/orgmode/87vc9gkund.fsf@pank.eu/
found 07633e3 in https://git.savannah.gnu.org/cgit/emacs/org-mode.git
preparing index
index prepared:
100644 07633e3718abc666d4ac97f21a929310bcd17531	contrib/lisp/ox-koma-letter.el

applying [1/1] https://yhetil.org/orgmode/87vc9gkund.fsf@pank.eu/
diff --git a/contrib/lisp/ox-koma-letter.el b/contrib/lisp/ox-koma-letter.el
index 07633e3..069d458 100644

Checking patch contrib/lisp/ox-koma-letter.el...
Applied patch contrib/lisp/ox-koma-letter.el cleanly.

index at:
100644 069d45830b233e8a3207952595436b93be844c2c	contrib/lisp/ox-koma-letter.el

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).