1 ;;; org-macro.el --- Macro Replacement Code for Org -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2013-2017 Free Software Foundation, Inc.
5 ;; Author: Nicolas Goaziou <n.goaziou@gmail.com>
6 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;; Macros are expanded with `org-macro-replace-all', which relies
26 ;; internally on `org-macro-expand'.
28 ;; Default templates for expansion are stored in the buffer-local
29 ;; variable `org-macro-templates'. This variable is updated by
30 ;; `org-macro-initialize-templates', which recursively calls
31 ;; `org-macro--collect-macros' in order to read setup files.
33 ;; Argument in macros are separated with commas. Proper escaping rules
34 ;; are implemented in `org-macro-escape-arguments' and arguments can
35 ;; be extracted from a string with `org-macro-extract-arguments'.
37 ;; Along with macros defined through #+MACRO: keyword, default
38 ;; templates include the following hard-coded macros:
39 ;; {{{time(format-string)}}},
40 ;; {{{property(node-property)}}},
42 ;; {{{modification-time(format-string)}}},
43 ;; {{{n(counter,reset}}}.
45 ;; Upon exporting, "ox.el" will also provide {{{author}}}, {{{date}}},
46 ;; {{{email}}} and {{{title}}} macros.
53 (declare-function org-element-at-point
"org-element" ())
54 (declare-function org-element-context
"org-element" (&optional element
))
55 (declare-function org-element-macro-parser
"org-element" ())
56 (declare-function org-element-property
"org-element" (property element
))
57 (declare-function org-element-type
"org-element" (element))
58 (declare-function org-file-contents
"org" (file &optional noerror nocache
))
59 (declare-function org-file-url-p
"org" (file))
60 (declare-function org-in-commented-heading-p
"org" (&optional no-inheritance
))
61 (declare-function org-mode
"org" ())
62 (declare-function vc-backend
"vc-hooks" (f))
63 (declare-function vc-call
"vc-hooks" (fun file
&rest args
) t
)
64 (declare-function vc-exec-after
"vc-dispatcher" (code))
68 (defvar-local org-macro-templates nil
69 "Alist containing all macro templates in current buffer.
70 Associations are in the shape of (NAME . TEMPLATE) where NAME
71 stands for macro's name and template for its replacement value,
72 both as strings. This is an internal variable. Do not set it
73 directly, use instead:
75 #+MACRO: name template")
79 (defun org-macro--collect-macros ()
80 "Collect macro definitions in current buffer and setup files.
81 Return an alist containing all macro templates found."
82 (letrec ((collect-macros
83 (lambda (files templates
)
84 ;; Return an alist of macro templates. FILES is a list
85 ;; of setup files names read so far, used to avoid
86 ;; circular dependencies. TEMPLATES is the alist
88 (let ((case-fold-search t
))
90 (goto-char (point-min))
91 (while (re-search-forward
92 "^[ \t]*#\\+\\(MACRO\\|SETUPFILE\\):" nil t
)
93 (let ((element (org-element-at-point)))
94 (when (eq (org-element-type element
) 'keyword
)
95 (let ((val (org-element-property :value element
)))
96 (if (equal (org-element-property :key element
) "MACRO")
97 ;; Install macro in TEMPLATES.
99 "^\\(.*?\\)\\(?:\\s-+\\(.*\\)\\)?\\s-*$" val
)
100 (let* ((name (match-string 1 val
))
101 (template (or (match-string 2 val
) ""))
102 (old-cell (assoc name templates
)))
103 (if old-cell
(setcdr old-cell template
)
104 (push (cons name template
) templates
))))
106 (let* ((uri (org-unbracket-string "\"" "\"" (org-trim val
)))
107 (uri-is-url (org-file-url-p uri
))
110 (expand-file-name uri
))))
111 ;; Avoid circular dependencies.
112 (unless (member uri files
)
115 (setq default-directory
116 (file-name-directory uri
)))
118 (insert (org-file-contents uri
'noerror
))
120 (funcall collect-macros
(cons uri files
)
123 (funcall collect-macros nil nil
)))
125 (defun org-macro-initialize-templates ()
126 "Collect macro templates defined in current buffer.
127 Templates are stored in buffer-local variable
128 `org-macro-templates'. In addition to buffer-defined macros, the
129 function installs the following ones: \"property\",
130 \"time\". and, if the buffer is associated to a file,
131 \"input-file\" and \"modification-time\"."
132 (let* ((templates (org-macro--collect-macros))
135 (let ((old-template (assoc (car cell
) templates
)))
136 (if old-template
(setcdr old-template
(cdr cell
))
137 (push cell templates
))))))
138 ;; Install "property", "time" macros.
139 (mapc update-templates
140 (list (cons "property"
141 "(eval (save-excursion
143 (when (org-string-nw-p l)
145 (let ((org-link-search-must-match-exact-headline t))
146 (org-link-search l nil t))
148 (error \"Macro property failed: cannot find location %s\"
150 (org-entry-get nil \"$1\" 'selective)))")
151 (cons "time" "(eval (format-time-string \"$1\"))")))
152 ;; Install "input-file", "modification-time" macros.
153 (let ((visited-file (buffer-file-name (buffer-base-buffer))))
154 (when (and visited-file
(file-exists-p visited-file
))
155 (mapc update-templates
156 (list (cons "input-file" (file-name-nondirectory visited-file
))
157 (cons "modification-time"
158 (format "(eval (format-time-string \"$1\" (or (and (org-string-nw-p \"$2\") (org-macro--vc-modified-time %s)) '%s)))"
159 (prin1-to-string visited-file
)
161 (nth 5 (file-attributes visited-file
)))))))))
162 ;; Initialize and install "n" macro.
163 (org-macro--counter-initialize)
164 (funcall update-templates
165 (cons "n" "(eval (org-macro--counter-increment \"$1\" \"$2\"))"))
166 (setq org-macro-templates templates
)))
168 (defun org-macro-expand (macro templates
)
169 "Return expanded MACRO, as a string.
170 MACRO is an object, obtained, for example, with
171 `org-element-context'. TEMPLATES is an alist of templates used
172 for expansion. See `org-macro-templates' for a buffer-local
173 default value. Return nil if no template was found."
175 ;; Macro names are case-insensitive.
176 (cdr (assoc-string (org-element-property :key macro
) templates t
))))
178 (let ((value (replace-regexp-in-string
181 (or (nth (1- (string-to-number (substring arg
1)))
182 (org-element-property :args macro
))
183 ;; No argument: remove place-holder.
185 template nil
'literal
)))
186 ;; VALUE starts with "(eval": it is a s-exp, `eval' it.
187 (when (string-match "\\`(eval\\>" value
)
188 (setq value
(eval (read value
))))
190 (format "%s" (or value
""))))))
192 (defun org-macro-replace-all (templates &optional finalize keywords
)
193 "Replace all macros in current buffer by their expansion.
195 TEMPLATES is an alist of templates used for expansion. See
196 `org-macro-templates' for a buffer-local default value.
198 If optional arg FINALIZE is non-nil, raise an error if a macro is
199 found in the buffer with no definition in TEMPLATES.
201 Optional argument KEYWORDS, when non-nil is a list of keywords,
202 as strings, where macro expansion is allowed."
204 (goto-char (point-min))
205 (let ((properties-regexp
206 (format "\\`EXPORT_%s\\+?\\'" (regexp-opt keywords
)))
208 (while (re-search-forward "{{{[-A-Za-z0-9_]" nil t
)
209 (unless (save-match-data (org-in-commented-heading-p))
210 (let* ((datum (save-match-data (org-element-context)))
211 (type (org-element-type datum
))
214 ((eq type
'macro
) datum
)
215 ;; In parsed keywords and associated node
216 ;; properties, force macro recognition.
217 ((or (and (eq type
'keyword
)
218 (member (org-element-property :key datum
)
220 (and (eq type
'node-property
)
221 (string-match-p properties-regexp
222 (org-element-property :key
225 (goto-char (match-beginning 0))
226 (org-element-macro-parser))))))
228 (let* ((value (org-macro-expand macro templates
))
229 (begin (org-element-property :begin macro
))
230 (signature (list begin
232 (org-element-property :args macro
))))
233 ;; Avoid circular dependencies by checking if the same
234 ;; macro with the same arguments is expanded at the
235 ;; same position twice.
236 (cond ((member signature record
)
237 (error "Circular macro expansion: %s"
238 (org-element-property :key macro
)))
240 (push signature record
)
243 ;; Preserve white spaces after the macro.
244 (progn (goto-char (org-element-property :end macro
))
245 (skip-chars-backward " \t")
247 ;; Leave point before replacement in case of
248 ;; recursive expansions.
249 (save-excursion (insert value
)))
251 (error "Undefined Org macro: %s; aborting"
252 (org-element-property :key macro
))))))))))))
254 (defun org-macro-escape-arguments (&rest args
)
255 "Build macro's arguments string from ARGS.
256 ARGS are strings. Return value is a string with arguments
257 properly escaped and separated with commas. This is the opposite
258 of `org-macro-extract-arguments'."
260 (dolist (arg (reverse args
) (substring s
1))
264 (replace-regexp-in-string
267 (concat (make-string (1+ (* 2 (length (match-string 1 m
)))) ?
\\)
269 ;; If a non-terminal argument ends on backslashes, make
270 ;; sure to also escape them as they will be followed by
272 (concat arg
(and (not (equal s
""))
273 (string-match "\\\\+\\'" arg
)
274 (match-string 0 arg
)))
278 (defun org-macro-extract-arguments (s)
279 "Extract macro arguments from string S.
280 S is a string containing comma separated values properly escaped.
281 Return a list of arguments, as strings. This is the opposite of
282 `org-macro-escape-arguments'."
283 ;; Do not use `org-split-string' since empty strings are
286 (replace-regexp-in-string
289 (let ((len (length (match-string 1 str
))))
290 (concat (make-string (/ len
2) ?
\\)
291 (if (zerop (mod len
2)) "\000" ","))))
296 ;;; Helper functions and variables for internal macros
298 (defun org-macro--vc-modified-time (file)
299 (save-window-excursion
300 (when (vc-backend file
)
301 (let ((buf (get-buffer-create " *org-vc*"))
306 (vc-call print-log file buf nil nil
1)
307 (with-current-buffer buf
310 (goto-char (point-min))
311 (when (re-search-forward "Date:?[ \t]*" nil t
)
312 (let ((time (parse-time-string
314 (point) (line-end-position)))))
315 (when (cl-some #'identity time
)
316 (setq date
(apply #'encode-time time
))))))))
317 (let ((proc (get-buffer-process buf
)))
318 (while (and proc
(accept-process-output proc
.5 nil t
)))))
322 (defvar org-macro--counter-table nil
323 "Hash table containing counter value per name.")
325 (defun org-macro--counter-initialize ()
326 "Initialize `org-macro--counter-table'."
327 (setq org-macro--counter-table
(make-hash-table :test
#'equal
)))
329 (defun org-macro--counter-increment (name &optional reset
)
330 "Increment counter NAME.
331 NAME is a string identifying the counter. When non-nil, optional
332 argument RESET is a string. If it represents an integer, set the
333 counter to this number. Any other non-empty string resets the
336 (cond ((not (org-string-nw-p reset
))
337 (1+ (gethash name org-macro--counter-table
0)))
338 ((string-match-p "\\`[ \t]*[0-9]+[ \t]*\\'" reset
)
339 (string-to-number reset
))
341 org-macro--counter-table
))
345 ;;; org-macro.el ends here