From 65ebb128bc380fe4795dedd655d6f7b685249842 Mon Sep 17 00:00:00 2001 From: Nicolas Goaziou Date: Sat, 7 Jul 2018 12:45:40 +0200 Subject: [PATCH] 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. --- lisp/org-macro.el | 15 +++++++++------ lisp/ox.el | 5 ++--- testing/lisp/test-ox.el | 11 ++++++++++- 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/lisp/org-macro.el b/lisp/org-macro.el index 6e9a5cb7d..7c07e4130 100644 --- a/lisp/org-macro.el +++ b/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)))))))))))) diff --git a/lisp/ox.el b/lisp/ox.el index ac35ede47..8f0a918cf 100644 --- a/lisp/ox.el +++ b/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 diff --git a/testing/lisp/test-ox.el b/testing/lisp/test-ox.el index 1c429c83c..245cb7e21 100644 --- a/testing/lisp/test-ox.el +++ b/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'."