123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408 |
- (require 'cl-lib)
- (require 'org-macs)
- (require 'org-compat)
- (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))
- (declare-function org-element-macro-parser "org-element" ())
- (declare-function org-element-parse-secondary-string "org-element" (string restriction &optional parent))
- (declare-function org-element-property "org-element" (property element))
- (declare-function org-element-restriction "org-element" (element))
- (declare-function org-element-type "org-element" (element))
- (declare-function org-entry-get "org" (pom property &optional inherit literal-nil))
- (declare-function org-file-contents "org" (file &optional noerror nocache))
- (declare-function org-file-url-p "org" (file))
- (declare-function org-in-commented-heading-p "org" (&optional no-inheritance))
- (declare-function org-link-search "org" (s &optional avoid-pos stealth))
- (declare-function org-mode "org" ())
- (declare-function vc-backend "vc-hooks" (f))
- (declare-function vc-call "vc-hooks" (fun file &rest args) t)
- (declare-function vc-exec-after "vc-dispatcher" (code))
- (defvar org-link-search-must-match-exact-headline)
- (defvar-local org-macro-templates nil
- "Alist containing all macro templates in current buffer.
- Associations are in the shape of (NAME . TEMPLATE) where NAME
- stands for macro's name and template for its replacement value,
- both as strings. This is an internal variable. Do not set it
- directly, use instead:
- #+MACRO: name template")
- (defun org-macro--collect-macros ()
- "Collect macro definitions in current buffer and setup files.
- Return an alist containing all macro templates found."
- (letrec ((collect-macros
- (lambda (files templates)
-
-
-
-
- (let ((case-fold-search t))
- (org-with-wide-buffer
- (goto-char (point-min))
- (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 (org-element-property :key element) "MACRO")
-
- (when (string-match
- "^\\(.*?\\)\\(?:\\s-+\\(.*\\)\\)?\\s-*$" val)
- (let* ((name (match-string 1 val))
- (template (or (match-string 2 val) ""))
- (old-cell (assoc name templates)))
- (if old-cell (setcdr old-cell template)
- (push (cons name template) templates))))
-
- (let* ((uri (org-strip-quotes (org-trim val)))
- (uri-is-url (org-file-url-p uri))
- (uri (if uri-is-url
- uri
- (expand-file-name uri))))
-
- (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
- (funcall collect-macros (cons uri files)
- templates)))))))))))
- templates))))
- (funcall collect-macros nil nil)))
- (defun org-macro-initialize-templates ()
- "Collect macro templates defined in current buffer.
- Templates are stored in buffer-local variable
- `org-macro-templates'.
- In addition to buffer-defined macros, the function installs the
- following ones: \"n\", \"author\", \"email\", \"keyword\",
- \"time\", \"property\", and, if the buffer is associated to
- a file, \"input-file\" and \"modification-time\"."
- (org-macro--counter-initialize)
- (setq org-macro-templates
- (nconc
-
- (org-macro--collect-macros)
-
- (let ((visited-file (buffer-file-name (buffer-base-buffer))))
- (and visited-file
- (file-exists-p visited-file)
- (list
- `("input-file" . ,(file-name-nondirectory visited-file))
- `("modification-time" .
- ,(format "(eval
- \(format-time-string $1
- (or (and (org-string-nw-p $2)
- (org-macro--vc-modified-time %s))
- '%s)))"
- (prin1-to-string visited-file)
- (prin1-to-string
- (file-attribute-modification-time
- (file-attributes visited-file))))))))
-
- (list
- '("n" . "(eval (org-macro--counter-increment $1 $2))")
- `("author" . ,(org-macro--find-keyword-value "AUTHOR"))
- `("email" . ,(org-macro--find-keyword-value "EMAIL"))
- '("keyword" . "(eval (org-macro--find-keyword-value $1))")
- '("time" . "(eval (format-time-string $1))")
- `("title" . ,(org-macro--find-keyword-value "TITLE"))
- '("property" . "(eval (org-macro--get-property $1 $2))")
- `("date" .
- ,(let* ((value (org-macro--find-keyword-value "DATE"))
- (date (org-element-parse-secondary-string
- value (org-element-restriction 'keyword))))
- (if (and (consp date)
- (not (cdr date))
- (eq 'timestamp (org-element-type (car date))))
- (format "(eval (if (org-string-nw-p $1) %s %S))"
- (format "(org-timestamp-format '%S $1)"
- (org-element-copy (car date)))
- value)
- value)))))))
- (defun org-macro-expand (macro templates)
- "Return expanded MACRO, as a string.
- MACRO is an object, obtained, for example, with
- `org-element-context'. TEMPLATES is an alist of templates used
- for expansion. See `org-macro-templates' for a buffer-local
- default value. Return nil if no template was found."
- (let ((template
-
- (cdr (assoc-string (org-element-property :key macro) templates t))))
- (when template
- (let* ((eval? (string-match-p "\\`(eval\\>" template))
- (value
- (replace-regexp-in-string
- "\\$[0-9]+"
- (lambda (m)
- (let ((arg (or (nth (1- (string-to-number (substring m 1)))
- (org-element-property :args macro))
-
- "")))
-
- (if eval? (format "%S" arg) arg)))
- template nil 'literal)))
- (when eval?
- (setq value (eval (condition-case nil (read value)
- (error (debug))))))
-
- (format "%s" (or value ""))))))
- (defun org-macro-replace-all (templates &optional keywords)
- "Replace all macros in current buffer by their expansion.
- TEMPLATES is an alist of templates used for expansion. See
- `org-macro-templates' for a buffer-local default value.
- Optional argument KEYWORDS, when non-nil is a list of keywords,
- as strings, where macro expansion is allowed.
- Return an error if a macro in the buffer cannot be associated to
- a definition in TEMPLATES."
- (org-with-wide-buffer
- (goto-char (point-min))
- (let ((properties-regexp (format "\\`EXPORT_%s\\+?\\'"
- (regexp-opt keywords)))
- record)
- (while (re-search-forward "{{{[-A-Za-z0-9_]" nil t)
- (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)
-
-
- ((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* ((key (org-element-property :key macro))
- (value (org-macro-expand macro templates))
- (begin (org-element-property :begin macro))
- (signature (list begin
- macro
- (org-element-property :args macro))))
-
-
-
- (cond ((member signature record)
- (error "Circular macro expansion: %s" key))
- (value
- (push signature record)
- (delete-region
- begin
-
- (progn (goto-char (org-element-property :end macro))
- (skip-chars-backward " \t")
- (point)))
-
-
- (save-excursion (insert value)))
-
-
-
- ((equal key "results"))
- (t
- (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.
- ARGS are strings. Return value is a string with arguments
- properly escaped and separated with commas. This is the opposite
- of `org-macro-extract-arguments'."
- (let ((s ""))
- (dolist (arg (reverse args) (substring s 1))
- (setq s
- (concat
- ","
- (replace-regexp-in-string
- "\\(\\\\*\\),"
- (lambda (m)
- (concat (make-string (1+ (* 2 (length (match-string 1 m)))) ?\\)
- ","))
-
-
-
- (concat arg (and (not (equal s ""))
- (string-match "\\\\+\\'" arg)
- (match-string 0 arg)))
- nil t)
- s)))))
- (defun org-macro-extract-arguments (s)
- "Extract macro arguments from string S.
- S is a string containing comma separated values properly escaped.
- Return a list of arguments, as strings. This is the opposite of
- `org-macro-escape-arguments'."
-
-
- (split-string
- (replace-regexp-in-string
- "\\(\\\\*\\),"
- (lambda (str)
- (let ((len (length (match-string 1 str))))
- (concat (make-string (/ len 2) ?\\)
- (if (zerop (mod len 2)) "\000" ","))))
- s nil t)
- "\000"))
- (defun org-macro--get-property (property location)
- "Find PROPERTY's value at LOCATION.
- PROPERTY is a string. LOCATION is a search string, as expected
- by `org-link-search', or the empty string."
- (save-excursion
- (when (org-string-nw-p location)
- (condition-case _
- (let ((org-link-search-must-match-exact-headline t))
- (org-link-search location nil t))
- (error
- (error "Macro property failed: cannot find location %s" location))))
- (org-entry-get nil property 'selective)))
- (defun org-macro--find-keyword-value (name)
- "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."
- (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)))))
- (defun org-macro--vc-modified-time (file)
- (save-window-excursion
- (when (vc-backend file)
- (let ((buf (get-buffer-create " *org-vc*"))
- (case-fold-search t)
- date)
- (unwind-protect
- (progn
- (vc-call print-log file buf nil nil 1)
- (with-current-buffer buf
- (vc-exec-after
- (lambda ()
- (goto-char (point-min))
- (when (re-search-forward "Date:?[ \t]*" nil t)
- (let ((time (parse-time-string
- (buffer-substring
- (point) (line-end-position)))))
- (when (cl-some #'identity time)
- (setq date (apply #'encode-time time))))))))
- (let ((proc (get-buffer-process buf)))
- (while (and proc (accept-process-output proc .5 nil t)))))
- (kill-buffer buf))
- date))))
- (defvar org-macro--counter-table nil
- "Hash table containing counter value per name.")
- (defun org-macro--counter-initialize ()
- "Initialize `org-macro--counter-table'."
- (setq org-macro--counter-table (make-hash-table :test #'equal)))
- (defun org-macro--counter-increment (name &optional action)
- "Increment counter NAME.
- NAME is a string identifying the counter.
- When non-nil, optional argument ACTION is a string.
- If the string is \"-\", keep the NAME counter at its current
- value, i.e. do not increment.
- If the string represents an integer, set the counter to this number.
- Any other non-empty string resets the counter to 1."
- (let ((name-trimmed (org-trim name))
- (action-trimmed (when (org-string-nw-p action)
- (org-trim action))))
- (puthash name-trimmed
- (cond ((not (org-string-nw-p action-trimmed))
- (1+ (gethash name-trimmed org-macro--counter-table 0)))
- ((string= "-" action-trimmed)
- (gethash name-trimmed org-macro--counter-table 1))
- ((string-match-p "\\`[0-9]+\\'" action-trimmed)
- (string-to-number action-trimmed))
- (t 1))
- org-macro--counter-table)))
- (provide 'org-macro)
|