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,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.

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