1 ;;; org-macro.el --- Macro Replacement Code for Org Mode
3 ;; Copyright (C) 2013 Free Software Foundation, Inc.
5 ;; Author: Nicolas Goaziou <n.goaziou@gmail.com>
6 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; GNU Emacs is free software: you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation, either version 3 of the License, or
11 ;; (at your option) any later version.
13 ;; GNU Emacs is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;; Macros are expanded with `org-macro-replace-all', which relies
24 ;; internally on `org-macro-expand'.
26 ;; Default templates for expansion are stored in the buffer-local
27 ;; variable `org-macro-templates'. This variable is updated by
28 ;; `org-macro-initialize-templates', which recursively calls
29 ;; `org-macro--collect-macros' in order to read setup files.
31 ;; Along with macros defined through #+MACRO: keyword, default
32 ;; templates include the following hard-coded macros:
33 ;; {{{time(format-string)}}}, {{{property(node-property)}}},
34 ;; {{{input-file}}} and {{{modification-time(format-string)}}}.
36 ;; Upon exporting, "ox.el" will also provide {{{author}}}, {{{date}}},
37 ;; {{{email}}} and {{{title}}} macros.
42 (declare-function org-element-at-point
"org-element" (&optional keep-trail
))
43 (declare-function org-element-context
"org-element" (&optional element
))
44 (declare-function org-element-property
"org-element" (property element
))
45 (declare-function org-element-type
"org-element" (element))
46 (declare-function org-remove-double-quotes
"org" (s))
47 (declare-function org-mode
"org" ())
48 (declare-function org-file-contents
"org" (file &optional noerror
))
49 (declare-function org-with-wide-buffer
"org-macs" (&rest body
))
53 (defvar org-macro-templates nil
54 "Alist containing all macro templates in current buffer.
55 Associations are in the shape of (NAME . TEMPLATE) where NAME
56 stands for macro's name and template for its replacement value,
57 both as strings. This is an internal variable. Do not set it
58 directly, use instead:
60 #+MACRO: name template")
61 (make-variable-buffer-local 'org-macro-templates
)
66 (defun org-macro--collect-macros ()
67 "Collect macro definitions in current buffer and setup files.
68 Return an alist containing all macro templates found."
69 (let* (collect-macros ; For byte-compiler.
71 (lambda (files templates
)
72 ;; Return an alist of macro templates. FILES is a list of
73 ;; setup files names read so far, used to avoid circular
74 ;; dependencies. TEMPLATES is the alist collected so far.
75 (let ((case-fold-search t
))
77 (goto-char (point-min))
78 (while (re-search-forward
79 "^[ \t]*#\\+\\(MACRO\\|SETUPFILE\\):" nil t
)
80 (let ((element (org-element-at-point)))
81 (when (eq (org-element-type element
) 'keyword
)
82 (let ((val (org-element-property :value element
)))
83 (if (equal (org-element-property :key element
) "MACRO")
84 ;; Install macro in TEMPLATES.
86 "^\\(.*?\\)\\(?:\\s-+\\(.*\\)\\)?\\s-*$" val
)
87 (let* ((name (match-string 1 val
))
88 (template (or (match-string 2 val
) ""))
89 (old-cell (assoc name templates
)))
90 (if old-cell
(setcdr old-cell template
)
91 (push (cons name template
) templates
))))
93 (let ((file (expand-file-name
94 (org-remove-double-quotes val
))))
95 (unless (member file files
)
98 (insert (org-file-contents file
'noerror
))
100 (funcall collect-macros
(cons file files
)
103 (funcall collect-macros nil nil
)))
105 (defun org-macro-initialize-templates ()
106 "Collect macro templates defined in current buffer.
107 Templates are stored in buffer-local variable
108 `org-macro-templates'. In addition to buffer-defined macros, the
109 function installs the following ones: \"property\",
110 \"time\". and, if the buffer is associated to a file,
111 \"input-file\" and \"modification-time\"."
112 (let* ((templates (org-macro--collect-macros))
115 (let ((old-template (assoc (car cell
) templates
)))
116 (if old-template
(setcdr old-template
(cdr cell
))
117 (push cell templates
))))))
118 ;; Install hard-coded macros.
119 (mapc (lambda (cell) (funcall update-templates cell
))
120 (list (cons "property" "(eval (org-entry-get nil \"$1\" 'selective))")
121 (cons "time" "(eval (format-time-string \"$1\"))")))
122 (let ((visited-file (buffer-file-name (buffer-base-buffer))))
123 (when (and visited-file
(file-exists-p visited-file
))
124 (mapc (lambda (cell) (funcall update-templates cell
))
125 (list (cons "input-file" (file-name-nondirectory visited-file
))
126 (cons "modification-time"
127 (format "(eval (format-time-string \"$1\" '%s))"
129 (nth 5 (file-attributes visited-file
)))))))))
130 (setq org-macro-templates templates
)))
132 (defun org-macro-expand (macro templates
)
133 "Return expanded MACRO, as a string.
134 MACRO is an object, obtained, for example, with
135 `org-element-context'. TEMPLATES is an alist of templates used
136 for expansion. See `org-macro-templates' for a buffer-local
137 default value. Return nil if no template was found."
139 ;; Macro names are case-insensitive.
140 (cdr (assoc-string (org-element-property :key macro
) templates t
))))
142 (let ((value (replace-regexp-in-string
145 (or (nth (1- (string-to-number (substring arg
1)))
146 (org-element-property :args macro
))
147 ;; No argument: remove place-holder.
149 template nil
'literal
)))
150 ;; VALUE starts with "(eval": it is a s-exp, `eval' it.
151 (when (string-match "\\`(eval\\>" value
)
152 (setq value
(eval (read value
))))
154 (format "%s" (or value
""))))))
156 (defun org-macro-replace-all (templates)
157 "Replace all macros in current buffer by their expansion.
158 TEMPLATES is an alist of templates used for expansion. See
159 `org-macro-templates' for a buffer-local default value."
161 (goto-char (point-min))
163 (while (re-search-forward "{{{[-A-Za-z0-9_]" nil t
)
164 (let ((object (org-element-context)))
165 (when (eq (org-element-type object
) 'macro
)
166 (let* ((value (org-macro-expand object templates
))
167 (begin (org-element-property :begin object
))
168 (signature (list begin
170 (org-element-property :args object
))))
171 ;; Avoid circular dependencies by checking if the same
172 ;; macro with the same arguments is expanded at the same
174 (if (member signature record
)
175 (error "Circular macro expansion: %s"
176 (org-element-property :key object
))
178 (push signature record
)
181 ;; Preserve white spaces after the macro.
182 (progn (goto-char (org-element-property :end object
))
183 (skip-chars-backward " \t")
185 ;; Leave point before replacement in case of recursive
187 (save-excursion (insert value
)))))))))))
191 ;;; org-macro.el ends here