Browse Source

org-macro: Fix "results" macro

* lisp/org-macro.el (org-macro-initialize-templates): Do not
  initialize the special "results" macro.
(org-macro-replace-all): Do not raise an error if "results" macro has
no associated template yet.
* lisp/ox.el (org-export-as): Update code comments.
* testing/lisp/test-ox.el (test-org-export/expand-macro): Add test.
Nicolas Goaziou 6 years ago
parent
commit
65ebb128bc
3 changed files with 21 additions and 10 deletions
  1. 9 6
      lisp/org-macro.el
  2. 2 3
      lisp/ox.el
  3. 10 1
      testing/lisp/test-ox.el

+ 9 - 6
lisp/org-macro.el

@@ -133,8 +133,8 @@ Templates are stored in buffer-local variable
 
 In addition to buffer-defined macros, the function installs the
 following ones: \"n\", \"author\", \"email\", \"keyword\",
-\"results\", \"time\", \"property\", and, if the buffer is
-associated to a file, \"input-file\" and \"modification-time\"."
+\"time\", \"property\", and, if the buffer is associated to
+a file, \"input-file\" and \"modification-time\"."
   (org-macro--counter-initialize)	;for "n" macro
   (setq org-macro-templates
 	(nconc
@@ -161,7 +161,6 @@ associated to a file, \"input-file\" and \"modification-time\"."
 	  `("author" . ,(org-macro--find-keyword-value "AUTHOR"))
 	  `("email" . ,(org-macro--find-keyword-value "EMAIL"))
 	  '("keyword" . "(eval (org-macro--find-keyword-value $1))")
-	  '("results" . "$1")
 	  '("time" . "(eval (format-time-string $1))")
 	  `("title" . ,(org-macro--find-keyword-value "TITLE"))
 	  '("property" . "(eval (org-macro--get-property $1 $2))")
@@ -240,7 +239,8 @@ a definition in TEMPLATES."
 		     (goto-char (match-beginning 0))
 		     (org-element-macro-parser))))))
 	   (when macro
-	     (let* ((value (org-macro-expand macro templates))
+	     (let* ((key (org-element-property :key macro))
+		    (value (org-macro-expand macro templates))
 		    (begin (org-element-property :begin macro))
 		    (signature (list begin
 				     macro
@@ -249,8 +249,7 @@ a definition in TEMPLATES."
 	       ;; macro with the same arguments is expanded at the
 	       ;; same position twice.
 	       (cond ((member signature record)
-		      (error "Circular macro expansion: %s"
-			     (org-element-property :key macro)))
+		      (error "Circular macro expansion: %s" key))
 		     (value
 		      (push signature record)
 		      (delete-region
@@ -262,6 +261,10 @@ a definition in TEMPLATES."
 		      ;; Leave point before replacement in case of
 		      ;; recursive expansions.
 		      (save-excursion (insert value)))
+		     ;; Special "results" macro: if it is not defined,
+		     ;; simply leave it as-is.  It will be expanded in
+		     ;; a second phase.
+		     ((equal key "results"))
 		     (t
 		      (error "Undefined Org macro: %s; aborting"
 			     (org-element-property :key macro))))))))))))

+ 2 - 3
lisp/ox.el

@@ -3043,15 +3043,14 @@ Return code as a string."
 	 ;; Run first hook with current back-end's name as argument.
 	 (run-hook-with-args 'org-export-before-processing-hook
 			     (org-export-backend-name backend))
-	 ;; Include files, delete comments and expand macros.  Refresh
-	 ;; buffer properties and radio targets after these
-	 ;; potentially invasive changes.
 	 (org-export-expand-include-keyword)
 	 (org-export--delete-comment-trees)
 	 (org-macro-initialize-templates)
 	 (org-macro-replace-all (append org-macro-templates
 					org-export-global-macros)
 				parsed-keywords)
+	 ;; Refresh buffer properties and radio targets after previous
+	 ;; potentially invasive changes.
 	 (org-set-regexps-and-options)
 	 (org-update-radio-target-regexp)
 	 ;;  Possibly execute Babel code.  Re-run a macro expansion

+ 10 - 1
testing/lisp/test-ox.el

@@ -1595,7 +1595,16 @@ Footnotes[fn:2], foot[fn:test] and [fn:inline:inline footnote]
 	  (org-test-with-temp-text "src_emacs-lisp{(+ 1 1)}"
 	    (let ((org-export-use-babel t)
 		  (org-babel-inline-result-wrap "=%s="))
-	      (org-export-as (org-test-default-backend)))))))
+	      (org-export-as (org-test-default-backend))))))
+  ;; If inline source block is already associated to a "results"
+  ;; macro, do not duplicate it.
+  (should
+   (equal "src_emacs-lisp{(+ 1 1)} {{{results(=2=)}}}"
+	  (org-test-with-temp-text "src_emacs-lisp{(+ 1 1)} {{{results(=2=)}}}"
+	    (let ((org-export-use-babel t)
+		  (org-babel-inline-result-wrap "=%s="))
+	      (org-export-as (org-test-default-backend)))
+	    (buffer-string)))))
 
 (ert-deftest test-org-export/before-processing-hook ()
   "Test `org-export-before-processing-hook'."