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