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