Browse Source

ox-koma-letter: Sometimes prefer special heading

* ox-koma-letter.el (org-koma-letter--keyword-or-headline):
  New function.
  (org-koma-letter-template): Use new function.
  (org-koma-letter-opening, org-koma-letter-closing)
  (org-koma-letter-signature)
  (org-koma-letter-headline-is-opening-maybe): Update docstring.

If both org-koma-letter-headline-is-opening-maybe and
org-koma-letter-prefer-special-headings is non-nil, always prefer
special headline title for opening and closing.
Rasmus 9 years ago
parent
commit
a2d064a8b5
1 changed files with 44 additions and 33 deletions
  1. 44 33
      contrib/lisp/ox-koma-letter.el

+ 44 - 33
contrib/lisp/ox-koma-letter.el

@@ -193,12 +193,12 @@ This option can also be set with the PLACE keyword."
 
 
 This option can also be set with the OPENING keyword.  Moreover,
 This option can also be set with the OPENING keyword.  Moreover,
 when:
 when:
-  (1) this value is the empty string;
-  (2) there's no OPENING keyword or it is empty;
-  (3) `org-koma-letter-headline-is-opening-maybe' is non-nil;
-  (4) the letter contains a headline without a special
+  (1) Either `org-koma-letter-prefer-special-headings' is non-nil
+      or the CLOSING keyword is empty
+  (2) `org-koma-letter-headline-is-opening-maybe' is non-nil;
+  (3) the letter contains a headline without a special
       tag (e.g. \"to\" or \"ps\");
       tag (e.g. \"to\" or \"ps\");
-then the opening will be implicitly set as the headline title."
+then the opening will be implicitly set as the untagged headline title."
   :group 'org-export-koma-letter
   :group 'org-export-koma-letter
   :type 'string)
   :type 'string)
 
 
@@ -206,12 +206,13 @@ then the opening will be implicitly set as the headline title."
   "Letter's closing, as a string.
   "Letter's closing, as a string.
 This option can also be set with the CLOSING keyword.  Moreover,
 This option can also be set with the CLOSING keyword.  Moreover,
 when:
 when:
-  (1) there's no CLOSING keyword or it is empty;
+  (1) Either `org-koma-letter-prefer-special-headings' is non-nil
+      or the CLOSING keyword is empty;
   (2) `org-koma-letter-headline-is-opening-maybe' is non-nil;
   (2) `org-koma-letter-headline-is-opening-maybe' is non-nil;
   (3) the letter contains a headline with the special
   (3) the letter contains a headline with the special
-      tag closing;
+      tag \"closing\";
 then the opening will be set as the title of the closing special
 then the opening will be set as the title of the closing special
-heading."
+heading title."
   :group 'org-export-koma-letter
   :group 'org-export-koma-letter
   :type 'string)
   :type 'string)
 
 
@@ -219,12 +220,15 @@ heading."
   "Signature, as a string.
   "Signature, as a string.
 This option can also be set with the SIGNATURE keyword.
 This option can also be set with the SIGNATURE keyword.
 Moreover, when:
 Moreover, when:
-  (1) there's no CLOSING keyword or it is empty;
+  (1) Either `org-koma-letter-prefer-special-headings' is non-nil
+      or there is no CLOSING keyword or the CLOSING keyword is empty;
   (2) `org-koma-letter-headline-is-opening-maybe' is non-nil;
   (2) `org-koma-letter-headline-is-opening-maybe' is non-nil;
   (3) the letter contains a headline with the special
   (3) the letter contains a headline with the special
-      tag closing;
+      tag \"closing\";
 then the signature will be  set as the content of the
 then the signature will be  set as the content of the
-closing special heading."
+closing special heading.
+
+Note if the content is empty the signature will not be set."
   :group 'org-export-koma-letter
   :group 'org-export-koma-letter
   :type 'string)
   :type 'string)
 
 
@@ -359,9 +363,9 @@ The value must be a member of `org-latex-classes'."
   :type 'string)
   :type 'string)
 
 
 (defcustom org-koma-letter-headline-is-opening-maybe t
 (defcustom org-koma-letter-headline-is-opening-maybe t
-  "Non-nil means a headline may be used as an opening.
-A headline is only used if #+OPENING is not set.  See also
-`org-koma-letter-opening'."
+  "Non-nil means a headline may be used as an opening and closing.
+See also `org-koma-letter-opening' and
+`org-koma-letter-closing'."
   :group 'org-export-koma-letter
   :group 'org-export-koma-letter
   :type 'boolean)
   :type 'boolean)
 
 
@@ -581,6 +585,26 @@ special tag headline."
 	(let ((tag (assoc-string tag special-tags)))
 	(let ((tag (assoc-string tag special-tags)))
 	  (when tag (throw 'exit tag)))))))
 	  (when tag (throw 'exit tag)))))))
 
 
+(defun org-koma-letter--keyword-or-headline (plist-key pred info)
+  "Return the correct version of opening or closing.
+PLIST-KEY should be a key in info, typically :opening
+or :closing.  PRED is a predicate run on headline to determine
+which title to use which takes two arguments, a headline element
+and an info plist.  INFO is a plist holding contextual
+information.  Return the preferred candidate for the exported of
+PLIST-KEY."
+  (let* ((keyword-candidate (plist-get info plist-key))
+	 (headline-candidate (when (and (plist-get info :with-headline-opening)
+					(or (plist-get info :special-headings)
+					    (not keyword-candidate)))
+			       (org-element-map (plist-get info :parse-tree)
+				   'headline
+				 (lambda (head)
+				   (when (funcall pred head info)
+				     (org-element-property :title head)))
+				 info t))))
+    (org-export-data (or headline-candidate keyword-candidate "") info)))
+
 ;;;; Template
 ;;;; Template
 
 
 (defun org-koma-letter-template (contents info)
 (defun org-koma-letter-template (contents info)
@@ -646,29 +670,16 @@ holding export options."
 	   (org-koma-letter--determine-to-and-from info 'to))
 	   (org-koma-letter--determine-to-and-from info 'to))
    ;; Opening.
    ;; Opening.
    (format "\\opening{%s}\n\n"
    (format "\\opening{%s}\n\n"
-	   (org-export-data
-	    (or (org-string-nw-p (plist-get info :opening))
-		(when (plist-get info :with-headline-opening)
-		  (org-element-map (plist-get info :parse-tree) 'headline
-		    (lambda (head)
-		      (unless (org-koma-letter--special-tag head info)
-			(org-element-property :title head)))
-		    info t))
-		"")
+	   (org-koma-letter--keyword-or-headline
+	    :opening (lambda (h i) (not (org-koma-letter--special-tag h i)))
 	    info))
 	    info))
    ;; Letter body.
    ;; Letter body.
    contents
    contents
    ;; Closing.
    ;; Closing.
-   (format "\n\\closing{%s}\n"
-	   (org-export-data
-	    (or (org-string-nw-p (plist-get info :closing))
-		(when (plist-get info :with-headline-opening)
-		  (org-element-map (plist-get info :parse-tree) 'headline
-		    (lambda (head)
-		      (when (eq (org-koma-letter--special-tag head info)
-				'closing)
-			(org-element-property :title head)))
-		    info t)))
+   (format "\\closing{%s}\n"
+	   (org-koma-letter--keyword-or-headline
+	    :closing (lambda (h i) (eq (org-koma-letter--special-tag h i)
+				'closing))
 	    info))
 	    info))
    (org-koma-letter--special-contents-as-macro
    (org-koma-letter--special-contents-as-macro
     (plist-get info :with-after-closing))
     (plist-get info :with-after-closing))