summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNicolas Goaziou <mail@nicolasgoaziou.fr>2017-06-08 14:59:34 +0200
committerNicolas Goaziou <mail@nicolasgoaziou.fr>2017-06-08 14:59:34 +0200
commit3cf6345b408803486f72c524654439401e716493 (patch)
treea3234b194c214e40f5285c0f5a69696cb784f393
parentda8b8f0774d378ad477ccd0b9c059dc246e90fde (diff)
downloadorg-mode-3cf6345b408803486f72c524654439401e716493.tar.gz
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.
-rw-r--r--lisp/org-macro.el85
-rw-r--r--testing/lisp/test-org-macro.el17
-rw-r--r--testing/lisp/test-ox.el9
3 files changed, 61 insertions, 50 deletions
diff --git a/lisp/org-macro.el b/lisp/org-macro.el
index 8927b3a..5a48908 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 bc493b3..b277c09 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<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 ()
diff --git a/testing/lisp/test-ox.el b/testing/lisp/test-ox.el
index b91b80d..d696601 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 ()