From 92d716b5e2adeaa01b8206c59b263512232119ba Mon Sep 17 00:00:00 2001 From: Rasmus Date: Sun, 24 Nov 2013 19:23:02 +0100 Subject: [PATCH 2/3] ox-koma-letter: test with auxiliary variables * ox-koma-letter.el (org-koma-letter-aux-suffix): New variable (org-koma-letter-unset-string): New variable. (koma-letter-option-alist): Removed x-changed-in-buffer-p variables. (org-koma-letter--make-aux-variables): Function to add aux variables. (org-koma-letter--aux-name): Function returns an aux name. (org-koma-letter--set-in-buffer): Function returns t if variable is changed in buffer. (org-koma-letter-template): Use new test for changed in buffer. An aux variable is used to test wheter a variable has been changed in buffer. This commits makes this work consistently. --- contrib/lisp/ox-koma-letter.el | 76 +++++++++++++++++++++++++++++++++--------- 1 file changed, 60 insertions(+), 16 deletions(-) diff --git a/contrib/lisp/ox-koma-letter.el b/contrib/lisp/ox-koma-letter.el index e4d6f7a..2705d86 100644 --- a/contrib/lisp/ox-koma-letter.el +++ b/contrib/lisp/ox-koma-letter.el @@ -66,6 +66,10 @@ ;; - after-letter-order, as above, but after the end of the letter ;; (see `org-koma-letter-special-tags-after-letter'). ;; +;; Note, some formatting depends on whether the variables has been set +;; in the buffer. For instance, the email is inserted after LCO +;; files, if changed in the letter. +;; ;; The following variables works differently from the main LaTeX class ;; - "AUTHOR": default to user-full-name but may be disabled. (see org-koma-letter-author), ;; - "EMAIL": same as AUTHOR, (see org-koma-letter-email), @@ -93,7 +97,7 @@ ;; ;; to your init file. This will add a sparse scrlttr2 class and ;; set it as the default `org-koma-latex-default-class'. You can also -;; add you own letter class. For instace: +;; add you own letter class. For instance: ;; ;; (add-to-list 'org-latex-classes ;; '("my-letter" @@ -179,6 +183,8 @@ function may be given. Functions must return a string." :type 'string) (defcustom org-koma-letter-opening nil + ;; TODO: We should probably get rid of requirement (1) and just + ;; check if the value is set in the buffer. "Letter's opening, as a string. If (1) this value is nil; (2) the letter is started with a @@ -291,6 +297,11 @@ A headline is only used if #+OPENING is not set. See also (defvar org-koma-letter-special-contents nil "Holds special content temporarily.") +(defconst org-koma-letter-unset-string "unset" + "Value for unset auxiliary variables in :option-alist.") + +(defconst org-koma-letter-aux-suffix "-aux" + "Suffix for auxiliary variables used to test whether a variable has been set.") ;;; Define Back-End @@ -302,11 +313,9 @@ A headline is only used if #+OPENING is not set. See also org-koma-letter-default-class org-latex-default-class) t) (:author "AUTHOR" nil (org-koma-letter--get-value org-koma-letter-author) t) - (:author-changed-in-buffer-p "AUTHOR" nil nil t) (:from-address "FROM_ADDRESS" nil nil newline) (:phone-number "PHONE_NUMBER" nil org-koma-letter-phone-number) (:email "EMAIL" nil (org-koma-letter--get-value org-koma-letter-email) t) - (:email-changed-in-buffer-p "EMAIL" nil nil t) (:to-address "TO_ADDRESS" nil nil newline) (:place "PLACE" nil org-koma-letter-place) (:opening "OPENING" nil org-koma-letter-opening) @@ -322,14 +331,10 @@ A headline is only used if #+OPENING is not set. See also org-koma-letter-special-tags-after-closing) (:with-after-letter nil "after-letter-order" org-koma-letter-special-tags-after-letter) - (:with-backaddress nil "backaddress" org-koma-letter-use-backaddress) - (:with-backaddress-changed-in-buffer-p nil "backaddress" nil) + (:with-backaddress nil "backaddress" org-koma-letter-use-backaddress ) (:with-foldmarks nil "foldmarks" org-koma-letter-use-foldmarks) - (:with-foldmarks-changed-in-buffer-p nil "foldmarks" "foldmarks-not-set") (:with-phone nil "phone" org-koma-letter-use-phone) - (:with-phone-changed-in-buffer-p nil "phone" nil) (:with-email nil "email" org-koma-letter-use-email) - (:with-email-changed-in-buffer-p nil "email" nil) (:with-place nil "place" org-koma-letter-use-place) (:with-subject nil "subject" org-koma-letter-subject-format)) :translate-alist '((export-block . org-koma-letter-export-block) @@ -347,6 +352,10 @@ A headline is only used if #+OPENING is not set. See also (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)))))))) + +;; Make auxiliary values to check whether variables have been set +(org-koma-letter--make-aux-variables 'koma-letter) + ;;; Initialize class function @@ -362,6 +371,23 @@ A headline is only used if #+OPENING is not set. See also ;;; Helper functions +(defun org-koma-letter--make-aux-variables (class) + "Append the option list of CLASS with aux values that test + whether they have been set in buffer." + ;; TODO: Is this the proper way to archive this? + (let* ((options (org-export-backend-options (org-export-get-backend class))) + (defined (mapcar 'first options)) + (aux (mapcar (lambda (l) + (let ((name (first l)) + (r1 (subseq l 1 3))) + (unless (member (org-koma-letter--aux-name name) defined) + (append (list (org-koma-letter--aux-name name)) r1 + (list 'org-koma-letter-unset-string t))))) + options))) + (setf (org-export-backend-options (org-export-get-backend class)) + (append options (delq nil aux))))) + + (defun org-koma-letter-email () "Return the current `user-mail-address'." user-mail-address) @@ -448,6 +474,21 @@ are present return the preferred one as determined by "\n" "\\\\\\\\\n" (org-koma-letter--normalize-string adr))))) +(defun org-koma-letter--aux-name (option) + "Return the auxiliary name of OPTION" + (let ((aux org-koma-letter-aux-suffix)) + (intern (concat (replace-regexp-in-string aux "" (symbol-name option)) "-aux")))) + +(defun org-koma-letter--set-in-buffer (info option) + "Test whether an :option-alist value is set. Return t or nil + +Values are \"primary\" values such as :email." + (let* ((opt (org-koma-letter--aux-name option)) + (value (if (plist-member info opt) (plist-get info opt) + (warn (format "Checked whether %s was set. But no auxiliary value exists. Returning nil" + option))))) + (not (equal (org-export-data value info) org-koma-letter-unset-string)))) + ;;; Transcode Functions ;;;; Export Block @@ -549,11 +590,11 @@ holding export options." info))) (let ((lco (plist-get info :lco)) (author (plist-get info :author)) - (author-set (plist-get info :author-changed-in-buffer-p)) + (author-set (org-koma-letter--set-in-buffer info :author)) (from-address (org-koma-letter--determine-to-and-from info 'from)) (phone-number (plist-get info :phone-number)) (email (plist-get info :email)) - (email-set (plist-get info :email-changed-in-buffer-p)) + (email-set (org-koma-letter--set-in-buffer info :email)) (signature (plist-get info :signature))) (concat ;; author or email not set in file: may be overridden by lco @@ -586,16 +627,19 @@ holding export options." (format "\\setkomavar{place}{%s}\n" (if with-place place "")))) ;; KOMA options (let ((with-backaddress (plist-get info :with-backaddress)) - (with-backaddress-set (plist-get info :with-backaddress-changed-in-buffer-p)) + (with-backaddress-set (org-koma-letter--set-in-buffer info :with-backaddress)) (with-foldmarks (plist-get info :with-foldmarks)) - (with-foldmarks-set - (not (string-equal (plist-get info :with-foldmarks-changed-in-buffer-p) - "foldmarks-not-set"))) + (with-foldmarks-set (org-koma-letter--set-in-buffer info :with-foldmarks)) (with-phone (plist-get info :with-phone)) - (with-phone-set (plist-get info :with-phone-changed-in-buffer-p)) + (with-phone-set (org-koma-letter--set-in-buffer info :with-phone)) + ;; TODO: email can now be set in two different places. This + ;; is handled in an inelegant manner. (with-email (plist-get info :with-email)) - (with-email-set (plist-get info :with-email-changed-in-buffer-p))) + (with-email-set (org-koma-letter--set-in-buffer info :with-email))) (concat + ;; TODO: with the (with . .)-clauses the default values are + ;; basically obsolete as these variables are only set when + ;; changed in buffer... (when with-backaddress-set (format "\\KOMAoption{backaddress}{%s}\n" (if with-backaddress "true" "false"))) (when with-foldmarks-set -- 1.8.4.2