summaryrefslogtreecommitdiff
path: root/lisp/org/org-macro.el
diff options
context:
space:
mode:
Diffstat (limited to 'lisp/org/org-macro.el')
-rw-r--r--lisp/org/org-macro.el58
1 files changed, 17 insertions, 41 deletions
diff --git a/lisp/org/org-macro.el b/lisp/org/org-macro.el
index a1b987a8e26..5ddfae4e1f6 100644
--- a/lisp/org/org-macro.el
+++ b/lisp/org/org-macro.el
@@ -50,6 +50,7 @@
(require 'org-macs)
(require 'org-compat)
+(declare-function org-collect-keywords "org" (keywords &optional unique directory))
(declare-function org-element-at-point "org-element" ())
(declare-function org-element-context "org-element" (&optional element))
(declare-function org-element-copy "org-element" (datum))
@@ -88,49 +89,24 @@ directly, use instead:
VALUE is the template of the macro. The new value override the
previous one, unless VALUE is nil. TEMPLATES is the list of
templates. Return the updated list."
- (when value
- (let ((old-definition (assoc name templates)))
- (if old-definition
- (setcdr old-definition value)
- (push (cons name value) templates))))
+ (let ((old-definition (assoc name templates)))
+ (cond ((and value old-definition) (setcdr old-definition value))
+ (old-definition)
+ (t (push (cons name (or value "")) templates))))
templates)
-(defun org-macro--collect-macros (&optional files templates)
+(defun org-macro--collect-macros ()
"Collect macro definitions in current buffer and setup files.
-Return an alist containing all macro templates found.
-
-FILES is a list of setup files names read so far, used to avoid
-circular dependencies. TEMPLATES is the alist collected so far.
-The two arguments are used in recursive calls."
- (let ((case-fold-search t))
- (org-with-point-at 1
- (while (re-search-forward "^[ \t]*#\\+\\(MACRO\\|SETUPFILE\\):" nil t)
- (let ((element (org-element-at-point)))
- (when (eq (org-element-type element) 'keyword)
- (let ((val (org-element-property :value element)))
- (if (equal "MACRO" (org-element-property :key element))
- ;; Install macro in TEMPLATES.
- (when (string-match "^\\(\\S-+\\)[ \t]*" val)
- (let ((name (match-string 1 val))
- (value (substring val (match-end 0))))
- (setq templates
- (org-macro--set-template name value templates))))
- ;; Enter setup file.
- (let* ((uri (org-strip-quotes val))
- (uri-is-url (org-file-url-p uri))
- (uri (if uri-is-url
- uri
- (expand-file-name uri))))
- ;; Avoid circular dependencies.
- (unless (member uri files)
- (with-temp-buffer
- (unless uri-is-url
- (setq default-directory (file-name-directory uri)))
- (org-mode)
- (insert (org-file-contents uri 'noerror))
- (setq templates
- (org-macro--collect-macros
- (cons uri files) templates)))))))))))
+Return an alist containing all macro templates found."
+ (let ((templates nil))
+ (pcase (org-collect-keywords '("MACRO"))
+ (`(("MACRO" . ,values))
+ (dolist (value values)
+ (when (string-match "^\\(\\S-+\\)[ \t]*" value)
+ (let ((name (match-string 1 value))
+ (definition (substring value (match-end 0))))
+ (setq templates
+ (org-macro--set-template name definition templates)))))))
(let ((macros `(("author" . ,(org-macro--find-keyword-value "AUTHOR"))
("email" . ,(org-macro--find-keyword-value "EMAIL"))
("title" . ,(org-macro--find-keyword-value "TITLE" t))
@@ -417,6 +393,6 @@ Any other non-empty string resets the counter to 1."
(t 1))
org-macro--counter-table)))
-
(provide 'org-macro)
+
;;; org-macro.el ends here