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