From mboxrd@z Thu Jan 1 00:00:00 1970 From: Nicolas Goaziou Subject: Re: HowTo: Letter template method using yasnippet Date: Sat, 07 Jul 2012 00:35:32 +0200 Message-ID: <87zk7cplfv.fsf@gmail.com> References: <8762a2l6f7.fsf@googlemail.com> <2012-07-06T20-54-34@devnull.Karl-Voit.at> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Return-path: Received: from eggs.gnu.org ([208.118.235.92]:50380) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SnHAs-00053h-61 for emacs-orgmode@gnu.org; Fri, 06 Jul 2012 18:38:55 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1SnHAq-00029q-2G for emacs-orgmode@gnu.org; Fri, 06 Jul 2012 18:38:53 -0400 Received: from mail-wg0-f49.google.com ([74.125.82.49]:57090) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SnHAp-000238-Kz for emacs-orgmode@gnu.org; Fri, 06 Jul 2012 18:38:51 -0400 Received: by wgbds1 with SMTP id ds1so7004717wgb.30 for ; Fri, 06 Jul 2012 15:38:48 -0700 (PDT) In-Reply-To: <2012-07-06T20-54-34@devnull.Karl-Voit.at> (Karl Voit's message of "Fri, 6 Jul 2012 21:08:24 +0200") List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: news1142@Karl-Voit.at Cc: emacs-orgmode@gnu.org --=-=-= Content-Type: text/plain Hello, Based on your work, I've put up the attached back-end: org-koma-letter.el. Quoting its documentation: --8<---------------cut here---------------start------------->8--- 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 `e-latex' back-end (see `org-e-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-e-latex-classes' in order to use the KOMA Scrlttr2 class. For example, you can use the following code: (add-to-list 'org-e-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]" ("\\section\{%s\}" . "\\section*\{%s\}") ("\\subsection\{%s\}" . "\\subsection*\{%s\}") ("\\subsubsection\{%s\}" . "\\subsubsection*\{%s\}"))) Then, in your Org document, be sure to require the proper class with : #+LATEX_CLASS: my-letter In this back-end, headlines are ignored during export. --8<---------------cut here---------------end--------------->8--- You can test it on the following Org document: --8<---------------cut here---------------start------------->8--- #+TITLE: About Org and letters #+AUTHOR: Nicolas Goaziou #+TO_ADDRESS: Someone \\ #+TO_ADDRESS: in some town #+SIGNATURE: ngz #+LATEX_CLASS: my-letter Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enimad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. --8<---------------cut here---------------end--------------->8--- It's far from being complete. See it as a proof of concept. Feel free to upgrade it. Regards, -- Nicolas Goaziou --=-=-= Content-Type: application/emacs-lisp Content-Disposition: attachment; filename=org-koma-letter.el Content-Transfer-Encoding: quoted-printable ;;; org-koma-letter.el --- KOMA Scrlttr2 Back-End for Org Export Engine ;; Copyright (C) 2007-2012 Free Software Foundation, Inc. ;; Author: Nicolas Goaziou ;; 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 . ;;; 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 `e-latex' back-end (see ;; `org-e-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-e-latex-classes' in order to use the KOMA Scrlttr2 class. For ;; example, you can use the following code: ;; ;; (add-to-list 'org-e-latex-classes ;; '("my-letter" ;; "\\documentclass\[% ;; DIV=3D14, ;; fontsize=3D12pt, ;; parskip=3Dhalf, ;; subject=3Dtitled, ;; backaddress=3Dfalse, ;; fromalign=3Dleft, ;; fromemail=3Dtrue, ;; fromphone=3Dtrue\]\{scrlttr2\} ;; \[DEFAULT-PACKAGES] ;; \[PACKAGES] ;; \[EXTRA]")) ;; ;; Then, in your Org document, be sure to require the proper class ;; with : ;; ;; #+LATEX_CLASS: my-letter ;; ;; In this back-end, headlines are ignored during export. ;;; Code: (require 'org-e-latex) ;;; 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-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 e-latex :options-alist ((:closing "CLOSING" nil org-koma-letter-closing) (:from-address "FROM_ADDRESS" nil org-koma-letter-from-address newline) (: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 nil newline) (:to-address "TO_ADDRESS" nil nil newline)) :translate-alist ((export-block . org-koma-letter-export-block) (export-snippet . org-koma-letter-export-snippet) (headline . org-koma-letter-headline) (keyword . org-koma-letter-keyword) (template . org-koma-letter-template))) ;;; 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) '(e-latex koma-le= tter)) (org-element-property :value export-snippet))) ;;;; Headline (defun org-koma-letter-headline (headline contents info) "Transcode a HEADLINE element into KOMA Scrlttr2 code. CONTENTS is nil. INFO is a plist used as a communication channel." contents) ;;;; 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 `e-latex' back-end. (if (equal key "KOMA-LETTER") value (funcall (cdr (assq 'keyword org-e-latex-translate-alist)) 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-e-latex-classes))) (document-class-string (and (stringp header) (if class-options (replace-regexp-in-string "^[ \t]*\\\\documentclass\\(\\[.*?\\]\\)" class-options header t nil 1) header)))) (when document-class-string (org-e-latex--guess-babel-language (org-e-latex--guess-inputenc (org-splice-latex-header document-class-string org-export-latex-default-packages-alist ; defined in org.el org-export-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{fromphone}{%s}\n" (plist-get info :phone-number)) ;; Date. (format "\\date{%s}\n" (org-export-data (plist-get info :date) info)) ;; 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" (plist-get info :to-address)) ;; Letter Class Option File (format "\\LoadLetterOption{%s}" (plist-get info :lco)) ;; Opening. (format "\\opening{%s}\n\n" (plist-get info :opening)) ;; Letter body. contents ;; Closing. (format "\n\\closing{%s}\n\n" (plist-get info :closing)) ;; Letter end. "\\end{letter}\n\\end{document}")) ;;; Commands ;;;###autoload (defun org-koma-letter-export-as-latex (&optional 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. 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) (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 subtreep visible-only body-only ext-plist pub-dir) "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. 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 pub-dir))) (org-export-to-file 'koma-letter outfile subtreep visible-only body-only ext-plist))) ;;;###autoload (defun org-koma-letter-export-to-pdf (&optional subtreep visible-only body-only ext-plist pub-dir) "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. 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 PDF file's name." (interactive) (org-e-latex-compile (org-koma-letter-export-to-latex subtreep visible-only body-only ext-plist pub-dir))) (provide 'org-koma-letter) ;;; org-koma-letter.el ends here --=-=-=--