]> code.delx.au - gnu-emacs/blob - lisp/org/org-macro.el
Add a new function `svg-embed'
[gnu-emacs] / lisp / org / org-macro.el
1 ;;; org-macro.el --- Macro Replacement Code for Org Mode
2
3 ;; Copyright (C) 2013-2016 Free Software Foundation, Inc.
4
5 ;; Author: Nicolas Goaziou <n.goaziou@gmail.com>
6 ;; Keywords: outlines, hypermedia, calendar, wp
7
8 ;; This file is part of GNU Emacs.
9
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.
14
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.
19
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/>.
22
23 ;;; Commentary:
24
25 ;; Macros are expanded with `org-macro-replace-all', which relies
26 ;; internally on `org-macro-expand'.
27
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.
32
33 ;; Along with macros defined through #+MACRO: keyword, default
34 ;; templates include the following hard-coded macros:
35 ;; {{{time(format-string)}}}, {{{property(node-property)}}},
36 ;; {{{input-file}}} and {{{modification-time(format-string)}}}.
37
38 ;; Upon exporting, "ox.el" will also provide {{{author}}}, {{{date}}},
39 ;; {{{email}}} and {{{title}}} macros.
40
41 ;;; Code:
42 (require 'org-macs)
43
44 (declare-function org-element-at-point "org-element" (&optional keep-trail))
45 (declare-function org-element-context "org-element" (&optional element))
46 (declare-function org-element-property "org-element" (property element))
47 (declare-function org-element-type "org-element" (element))
48 (declare-function org-remove-double-quotes "org" (s))
49 (declare-function org-mode "org" ())
50 (declare-function org-file-contents "org" (file &optional noerror))
51
52 ;;; Variables
53
54 (defvar org-macro-templates nil
55 "Alist containing all macro templates in current buffer.
56 Associations are in the shape of (NAME . TEMPLATE) where NAME
57 stands for macro's name and template for its replacement value,
58 both as strings. This is an internal variable. Do not set it
59 directly, use instead:
60
61 #+MACRO: name template")
62 (make-variable-buffer-local 'org-macro-templates)
63
64
65 ;;; Functions
66
67 (defun org-macro--collect-macros ()
68 "Collect macro definitions in current buffer and setup files.
69 Return an alist containing all macro templates found."
70 (let* (collect-macros ; For byte-compiler.
71 (collect-macros
72 (lambda (files templates)
73 ;; Return an alist of macro templates. FILES is a list of
74 ;; setup files names read so far, used to avoid circular
75 ;; dependencies. TEMPLATES is the alist collected so far.
76 (let ((case-fold-search t))
77 (org-with-wide-buffer
78 (goto-char (point-min))
79 (while (re-search-forward
80 "^[ \t]*#\\+\\(MACRO\\|SETUPFILE\\):" nil t)
81 (let ((element (org-element-at-point)))
82 (when (eq (org-element-type element) 'keyword)
83 (let ((val (org-element-property :value element)))
84 (if (equal (org-element-property :key element) "MACRO")
85 ;; Install macro in TEMPLATES.
86 (when (string-match
87 "^\\(.*?\\)\\(?:\\s-+\\(.*\\)\\)?\\s-*$" val)
88 (let* ((name (match-string 1 val))
89 (template (or (match-string 2 val) ""))
90 (old-cell (assoc name templates)))
91 (if old-cell (setcdr old-cell template)
92 (push (cons name template) templates))))
93 ;; Enter setup file.
94 (let ((file (expand-file-name
95 (org-remove-double-quotes val))))
96 (unless (member file files)
97 (with-temp-buffer
98 (org-mode)
99 (insert (org-file-contents file 'noerror))
100 (setq templates
101 (funcall collect-macros (cons file files)
102 templates)))))))))))
103 templates))))
104 (funcall collect-macros nil nil)))
105
106 (defun org-macro-initialize-templates ()
107 "Collect macro templates defined in current buffer.
108 Templates are stored in buffer-local variable
109 `org-macro-templates'. In addition to buffer-defined macros, the
110 function installs the following ones: \"property\",
111 \"time\". and, if the buffer is associated to a file,
112 \"input-file\" and \"modification-time\"."
113 (let* ((templates (org-macro--collect-macros))
114 (update-templates
115 (lambda (cell)
116 (let ((old-template (assoc (car cell) templates)))
117 (if old-template (setcdr old-template (cdr cell))
118 (push cell templates))))))
119 ;; Install hard-coded macros.
120 (mapc (lambda (cell) (funcall update-templates cell))
121 (list (cons "property" "(eval (org-entry-get nil \"$1\" 'selective))")
122 (cons "time" "(eval (format-time-string \"$1\"))")))
123 (let ((visited-file (buffer-file-name (buffer-base-buffer))))
124 (when (and visited-file (file-exists-p visited-file))
125 (mapc (lambda (cell) (funcall update-templates cell))
126 (list (cons "input-file" (file-name-nondirectory visited-file))
127 (cons "modification-time"
128 (format "(eval (format-time-string \"$1\" '%s))"
129 (prin1-to-string
130 (nth 5 (file-attributes visited-file)))))))))
131 (setq org-macro-templates templates)))
132
133 (defun org-macro-expand (macro templates)
134 "Return expanded MACRO, as a string.
135 MACRO is an object, obtained, for example, with
136 `org-element-context'. TEMPLATES is an alist of templates used
137 for expansion. See `org-macro-templates' for a buffer-local
138 default value. Return nil if no template was found."
139 (let ((template
140 ;; Macro names are case-insensitive.
141 (cdr (assoc-string (org-element-property :key macro) templates t))))
142 (when template
143 (let ((value (replace-regexp-in-string
144 "\\$[0-9]+"
145 (lambda (arg)
146 (or (nth (1- (string-to-number (substring arg 1)))
147 (org-element-property :args macro))
148 ;; No argument: remove place-holder.
149 ""))
150 template nil 'literal)))
151 ;; VALUE starts with "(eval": it is a s-exp, `eval' it.
152 (when (string-match "\\`(eval\\>" value)
153 (setq value (eval (read value))))
154 ;; Return string.
155 (format "%s" (or value ""))))))
156
157 (defun org-macro-replace-all (templates)
158 "Replace all macros in current buffer by their expansion.
159 TEMPLATES is an alist of templates used for expansion. See
160 `org-macro-templates' for a buffer-local default value."
161 (save-excursion
162 (goto-char (point-min))
163 (let (record)
164 (while (re-search-forward "{{{[-A-Za-z0-9_]" nil t)
165 (let ((object (org-element-context)))
166 (when (eq (org-element-type object) 'macro)
167 (let* ((value (org-macro-expand object templates))
168 (begin (org-element-property :begin object))
169 (signature (list begin
170 object
171 (org-element-property :args object))))
172 ;; Avoid circular dependencies by checking if the same
173 ;; macro with the same arguments is expanded at the same
174 ;; position twice.
175 (if (member signature record)
176 (error "Circular macro expansion: %s"
177 (org-element-property :key object))
178 (when value
179 (push signature record)
180 (delete-region
181 begin
182 ;; Preserve white spaces after the macro.
183 (progn (goto-char (org-element-property :end object))
184 (skip-chars-backward " \t")
185 (point)))
186 ;; Leave point before replacement in case of recursive
187 ;; expansions.
188 (save-excursion (insert value)))))))))))
189
190
191 (provide 'org-macro)
192 ;;; org-macro.el ends here