summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNicolas Goaziou <mail@nicolasgoaziou.fr>2019-04-29 20:42:45 +0200
committerNicolas Goaziou <mail@nicolasgoaziou.fr>2019-04-29 20:48:00 +0200
commit727c3f442b7af5fb45cfcb1d589aa64524ea5a1e (patch)
treebd0b0ed18a372293caad4aa577fab3abced0789c
parent14132a356a241dbea95e4d63364bf9dbb90b669e (diff)
downloadorg-mode-727c3f442b7af5fb45cfcb1d589aa64524ea5a1e.tar.gz
org-macro: Exit early when looking for keywords
* lisp/org-macro.el (org-macro--find-keyword-value): Change signature. * testing/lisp/test-org-macro.el (test-org-macro/keyword): Remove a test.
-rw-r--r--lisp/org-macro.el25
-rw-r--r--testing/lisp/test-org-macro.el10
2 files changed, 14 insertions, 21 deletions
diff --git a/lisp/org-macro.el b/lisp/org-macro.el
index d7ec953..e1241c3 100644
--- a/lisp/org-macro.el
+++ b/lisp/org-macro.el
@@ -133,7 +133,7 @@ The two arguments are used in recursive calls."
(cons uri files) templates)))))))))))
(let ((macros `(("author" . ,(org-macro--find-keyword-value "AUTHOR"))
("email" . ,(org-macro--find-keyword-value "EMAIL"))
- ("title" . ,(org-macro--find-keyword-value "TITLE"))
+ ("title" . ,(org-macro--find-keyword-value "TITLE" t))
("date" . ,(org-macro--find-date)))))
(pcase-dolist (`(,name . ,value) macros)
(setq templates (org-macro--set-template name value templates))))
@@ -326,21 +326,24 @@ by `org-link-search', or the empty string."
(error "Macro property failed: cannot find location %s" location))))
(org-entry-get nil property 'selective)))
-(defun org-macro--find-keyword-value (name)
+(defun org-macro--find-keyword-value (name &optional collect)
"Find value for keyword NAME in current buffer.
-KEYWORD is a string. Return value associated to the keywords
-named after NAME, as a string, or nil."
+Return value associated to the keywords named after NAME, as
+a string, or nil. When optional argument COLLECT is non-nil,
+concatenate values, separated with a space, from various keywords
+in the buffer."
(org-with-point-at 1
(let ((regexp (format "^[ \t]*#\\+%s:" (regexp-quote name)))
(case-fold-search t)
(result nil))
- (while (re-search-forward regexp nil t)
- (let ((element (org-element-at-point)))
- (when (eq 'keyword (org-element-type element))
- (setq result (concat result
- " "
- (org-element-property :value element))))))
- (and result (org-trim result)))))
+ (catch :exit
+ (while (re-search-forward regexp nil t)
+ (let ((element (org-element-at-point)))
+ (when (eq 'keyword (org-element-type element))
+ (let ((value (org-element-property :value element)))
+ (if (not collect) (throw :exit value)
+ (setq result (concat result " " value)))))))
+ (and result (org-trim result))))))
(defun org-macro--find-date ()
"Find value for DATE in current buffer.
diff --git a/testing/lisp/test-org-macro.el b/testing/lisp/test-org-macro.el
index f5ab633..28bc712 100644
--- a/testing/lisp/test-org-macro.el
+++ b/testing/lisp/test-org-macro.el
@@ -302,16 +302,6 @@
(org-macro-initialize-templates)
(org-macro-replace-all org-macro-templates)
(buffer-substring-no-properties
- (line-beginning-position) (point-max)))))
- ;; Replace macro with keyword's value.
- (should
- (equal
- "value value2"
- (org-test-with-temp-text
- "#+keyword: value\n#+keyword: value2\n<point>{{{keyword(KEYWORD)}}}"
- (org-macro-initialize-templates)
- (org-macro-replace-all org-macro-templates)
- (buffer-substring-no-properties
(line-beginning-position) (point-max))))))
(ert-deftest test-org-macro/escape-arguments ()