Browse Source

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.
Nicolas Goaziou 5 years ago
parent
commit
727c3f442b
2 changed files with 14 additions and 21 deletions
  1. 14 11
      lisp/org-macro.el
  2. 0 10
      testing/lisp/test-org-macro.el

+ 14 - 11
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.

+ 0 - 10
testing/lisp/test-org-macro.el

@@ -301,16 +301,6 @@
 	"#+keyword: value\n<point>{{{keyword(KEYWORD)}}}"
       (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))))))