diff --git a/lisp/org-macro.el b/lisp/org-macro.el index 8927b3a80..5a4890875 100644 --- a/lisp/org-macro.el +++ b/lisp/org-macro.el @@ -191,47 +191,50 @@ as strings, where macro expansion is allowed." (format "\\`EXPORT_%s\\+?\\'" (regexp-opt keywords))) record) (while (re-search-forward "{{{[-A-Za-z0-9_]" nil t) - (let* ((datum (save-match-data (org-element-context))) - (type (org-element-type datum)) - (macro - (cond - ((eq type 'macro) datum) - ;; In parsed keywords and associated node properties, - ;; force macro recognition. - ((or (and (eq type 'keyword) - (member (org-element-property :key datum) keywords)) - (and (eq type 'node-property) - (string-match-p properties-regexp - (org-element-property :key datum)))) - (save-excursion - (goto-char (match-beginning 0)) - (org-element-macro-parser)))))) - (when macro - (let* ((value (org-macro-expand macro templates)) - (begin (org-element-property :begin macro)) - (signature (list begin - macro - (org-element-property :args macro)))) - ;; Avoid circular dependencies by checking if the same - ;; 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))) - (value - (push signature record) - (delete-region - begin - ;; Preserve white spaces after the macro. - (progn (goto-char (org-element-property :end macro)) - (skip-chars-backward " \t") - (point))) - ;; Leave point before replacement in case of - ;; recursive expansions. - (save-excursion (insert value))) - (finalize - (error "Undefined Org macro: %s; aborting" - (org-element-property :key macro))))))))))) + (unless (save-match-data (org-in-commented-heading-p)) + (let* ((datum (save-match-data (org-element-context))) + (type (org-element-type datum)) + (macro + (cond + ((eq type 'macro) datum) + ;; In parsed keywords and associated node + ;; properties, force macro recognition. + ((or (and (eq type 'keyword) + (member (org-element-property :key datum) + keywords)) + (and (eq type 'node-property) + (string-match-p properties-regexp + (org-element-property :key + datum)))) + (save-excursion + (goto-char (match-beginning 0)) + (org-element-macro-parser)))))) + (when macro + (let* ((value (org-macro-expand macro templates)) + (begin (org-element-property :begin macro)) + (signature (list begin + macro + (org-element-property :args macro)))) + ;; Avoid circular dependencies by checking if the same + ;; 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))) + (value + (push signature record) + (delete-region + begin + ;; Preserve white spaces after the macro. + (progn (goto-char (org-element-property :end macro)) + (skip-chars-backward " \t") + (point))) + ;; Leave point before replacement in case of + ;; recursive expansions. + (save-excursion (insert value))) + (finalize + (error "Undefined Org macro: %s; aborting" + (org-element-property :key macro)))))))))))) (defun org-macro-escape-arguments (&rest args) "Build macro's arguments string from ARGS. diff --git a/testing/lisp/test-org-macro.el b/testing/lisp/test-org-macro.el index bc493b3d1..b277c097b 100644 --- a/testing/lisp/test-org-macro.el +++ b/testing/lisp/test-org-macro.el @@ -117,6 +117,23 @@ (narrow-to-region (point) (point-max)) (org-macro-initialize-templates) (org-macro-replace-all org-macro-templates) + (org-with-wide-buffer (buffer-string))))) + ;; Macros in a commented tree are not expanded. + (should + (string-match-p + "{{{macro}}}" + (org-test-with-temp-text + "#+MACRO: macro expansion\n* COMMENT H\n{{{macro}}}" + (org-macro-initialize-templates) + (org-macro-replace-all org-macro-templates) + (org-with-wide-buffer (buffer-string))))) + (should + (string-match-p + "{{{macro}}}" + (org-test-with-temp-text + "#+MACRO: macro expansion\n* COMMENT H1\n** H2\n{{{macro}}}" + (org-macro-initialize-templates) + (org-macro-replace-all org-macro-templates) (org-with-wide-buffer (buffer-string)))))) (ert-deftest test-org-macro/escape-arguments () diff --git a/testing/lisp/test-ox.el b/testing/lisp/test-ox.el index b91b80d63..d69660178 100644 --- a/testing/lisp/test-ox.el +++ b/testing/lisp/test-ox.el @@ -1351,15 +1351,6 @@ Footnotes[fn:2], foot[fn:test] and [fn:inline:inline footnote] ;; Throw an error when a macro definition is missing. (should-error (org-test-with-temp-text "{{{missing}}}" - (org-export-as (org-test-default-backend)))) - ;; Macros defined in commented subtrees are ignored. - (should-error - (org-test-with-temp-text - "* COMMENT H\n#+MACRO: macro1\n* H2\nvalue\n{{{macro1}}}" - (org-export-as (org-test-default-backend)))) - (should-error - (org-test-with-temp-text - "* COMMENT H\n** H2\n#+MACRO: macro1\n* H3\nvalue\n{{{macro1}}}" (org-export-as (org-test-default-backend))))) (ert-deftest test-org-export/before-processing-hook ()