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)))
record)
(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)))
(type (org-element-type datum))
(macro
(cond
((eq type 'macro) datum)
;; In parsed keywords and associated node properties,
;; force macro recognition.
;; In parsed keywords and associated node
;; properties, force macro recognition.
((or (and (eq type 'keyword)
(member (org-element-property :key datum) keywords))
(member (org-element-property :key datum)
keywords))
(and (eq type 'node-property)
(string-match-p properties-regexp
(org-element-property :key datum))))
(org-element-property :key
datum))))
(save-excursion
(goto-char (match-beginning 0))
(org-element-macro-parser))))))
@ -213,8 +216,8 @@ as strings, where macro expansion is allowed."
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.
;; 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)))
@ -231,7 +234,7 @@ as strings, where macro expansion is allowed."
(save-excursion (insert value)))
(finalize
(error "Undefined Org macro: %s; aborting"
(org-element-property :key macro)))))))))))
(org-element-property :key macro))))))))))))
(defun org-macro-escape-arguments (&rest args)
"Build macro's arguments string from ARGS.

View file

@ -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<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))))))
(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.
(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 ()