Merge branch 'maint'
[org-mode.git] / lisp / org-macro.el
blobfa74d8341b51ad3510cd2200a1ceb74d41d71985
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:
40 (require 'org-macs)
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))
51 ;;; Variables
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)
64 ;;; Functions
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.
70 (collect-macros
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))
76 (org-with-wide-buffer
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.
85 (when (string-match
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))))
92 ;; Enter setup file.
93 (let ((file (expand-file-name
94 (org-remove-double-quotes val))))
95 (unless (member file files)
96 (with-temp-buffer
97 (org-mode)
98 (insert (org-file-contents file 'noerror))
99 (setq templates
100 (funcall collect-macros (cons file files)
101 templates)))))))))))
102 templates))))
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))
113 (update-templates
114 (lambda (cell)
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))"
128 (prin1-to-string
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."
138 (let ((template
139 ;; Macro names are case-insensitive.
140 (cdr (assoc-string (org-element-property :key macro) templates t))))
141 (when template
142 (let ((value (replace-regexp-in-string
143 "\\$[0-9]+"
144 (lambda (arg)
145 (or (nth (1- (string-to-number (substring arg 1)))
146 (org-element-property :args macro))
147 ;; No argument: remove place-holder.
148 ""))
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))))
153 ;; Return string.
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."
160 (save-excursion
161 (goto-char (point-min))
162 (let (record)
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
169 object
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
173 ;; position twice.
174 (if (member signature record)
175 (error "Circular macro expansion: %s"
176 (org-element-property :key object))
177 (when value
178 (push signature record)
179 (delete-region
180 begin
181 ;; Preserve white spaces after the macro.
182 (progn (goto-char (org-element-property :end object))
183 (skip-chars-backward " \t")
184 (point)))
185 ;; Leave point before replacement in case of recursive
186 ;; expansions.
187 (save-excursion (insert value)))))))))))
190 (provide 'org-macro)
191 ;;; org-macro.el ends here