org-macro: Fix macro expansion in commented trees

* lisp/org-macro.el (org-macro-replace-all): Prevent macro expansion
  in commented trees.
* testing/lisp/test-org-macro.el (test-org/macro-replace-all): Add
  tests.
* testing/lisp/test-ox.el (test-org-export/expand-macro): Remove
  tests.
This commit is contained in:
Nicolas Goaziou 2017-06-08 14:59:34 +02:00
parent da8b8f0774
commit 3cf6345b40
3 changed files with 61 additions and 50 deletions

View File

@ -191,18 +191,21 @@ as strings, where macro expansion is allowed."
(format "\\`EXPORT_%s\\+?\\'" (regexp-opt keywords))) (format "\\`EXPORT_%s\\+?\\'" (regexp-opt keywords)))
record) record)
(while (re-search-forward "{{{[-A-Za-z0-9_]" nil t) (while (re-search-forward "{{{[-A-Za-z0-9_]" nil t)
(unless (save-match-data (org-in-commented-heading-p))
(let* ((datum (save-match-data (org-element-context))) (let* ((datum (save-match-data (org-element-context)))
(type (org-element-type datum)) (type (org-element-type datum))
(macro (macro
(cond (cond
((eq type 'macro) datum) ((eq type 'macro) datum)
;; In parsed keywords and associated node properties, ;; In parsed keywords and associated node
;; force macro recognition. ;; properties, force macro recognition.
((or (and (eq type 'keyword) ((or (and (eq type 'keyword)
(member (org-element-property :key datum) keywords)) (member (org-element-property :key datum)
keywords))
(and (eq type 'node-property) (and (eq type 'node-property)
(string-match-p properties-regexp (string-match-p properties-regexp
(org-element-property :key datum)))) (org-element-property :key
datum))))
(save-excursion (save-excursion
(goto-char (match-beginning 0)) (goto-char (match-beginning 0))
(org-element-macro-parser)))))) (org-element-macro-parser))))))
@ -213,8 +216,8 @@ as strings, where macro expansion is allowed."
macro macro
(org-element-property :args macro)))) (org-element-property :args macro))))
;; Avoid circular dependencies by checking if the same ;; Avoid circular dependencies by checking if the same
;; macro with the same arguments is expanded at the same ;; macro with the same arguments is expanded at the
;; position twice. ;; same position twice.
(cond ((member signature record) (cond ((member signature record)
(error "Circular macro expansion: %s" (error "Circular macro expansion: %s"
(org-element-property :key macro))) (org-element-property :key macro)))
@ -231,7 +234,7 @@ as strings, where macro expansion is allowed."
(save-excursion (insert value))) (save-excursion (insert value)))
(finalize (finalize
(error "Undefined Org macro: %s; aborting" (error "Undefined Org macro: %s; aborting"
(org-element-property :key macro))))))))))) (org-element-property :key macro))))))))))))
(defun org-macro-escape-arguments (&rest args) (defun org-macro-escape-arguments (&rest args)
"Build macro's arguments string from ARGS. "Build macro's arguments string from ARGS.

View File

@ -117,6 +117,23 @@
(narrow-to-region (point) (point-max)) (narrow-to-region (point) (point-max))
(org-macro-initialize-templates) (org-macro-initialize-templates)
(org-macro-replace-all org-macro-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<point>{{{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<point>{{{macro}}}"
(org-macro-initialize-templates)
(org-macro-replace-all org-macro-templates)
(org-with-wide-buffer (buffer-string)))))) (org-with-wide-buffer (buffer-string))))))
(ert-deftest test-org-macro/escape-arguments () (ert-deftest test-org-macro/escape-arguments ()

View File

@ -1351,15 +1351,6 @@ Footnotes[fn:2], foot[fn:test] and [fn:inline:inline footnote]
;; Throw an error when a macro definition is missing. ;; Throw an error when a macro definition is missing.
(should-error (should-error
(org-test-with-temp-text "{{{missing}}}" (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))))) (org-export-as (org-test-default-backend)))))
(ert-deftest test-org-export/before-processing-hook () (ert-deftest test-org-export/before-processing-hook ()