2013-02-16 13:03:59 +00:00
|
|
|
;;; org-macro.el --- Macro Replacement Code for Org Mode
|
|
|
|
|
2015-02-16 00:40:07 +00:00
|
|
|
;; Copyright (C) 2013-2015 Free Software Foundation, Inc.
|
2013-02-16 13:03:59 +00:00
|
|
|
|
|
|
|
;; Author: Nicolas Goaziou <n.goaziou@gmail.com>
|
|
|
|
;; Keywords: outlines, hypermedia, calendar, wp
|
|
|
|
|
Backport changes from Emacs revs 115081 and 115082
2013-11-12 Stefan Monnier <monnier@iro.umontreal.ca>
Address some byte-compiler warnings.
* ob-abc.el (org-babel-expand-body:abc): Use dolist.
(org-babel-execute:abc): Fix regexp quoting.
* ob-calc.el (org--var-syms): Rename from `var-syms'.
* ob-lilypond.el (ly-compile-lilyfile): Remove redundant let-binding.
* ob-table.el (sbe): Move debug declaration.
* org-clock.el (org--msg-extra): Rename from `msg-extra'.
* org.el (org-version): Avoid var name starting with _.
(org-inhibit-startup, org-called-with-limited-levels)
(org-link-search-inhibit-query, org-time-was-given)
(org-end-time-was-given, org-def, org-defdecode, org-with-time):
* org-colview.el (org-agenda-overriding-columns-format):
* org-agenda.el (org-agenda-multi, org-depend-tag-blocked)
(org-agenda-show-log-scoped):
* ob-python.el (py-which-bufname, python-shell-buffer-name):
* ob-haskell.el (org-export-copy-to-kill-ring):
* ob-exp.el (org-link-search-inhibit-query):
* ob-R.el (ess-eval-visibly-p):
* ob-core.el (org-src-window-setup): Declare before use.
(org-babel-expand-noweb-references): Remove unused `blocks-in-buffer'.
* ox-odt.el (org-odt-hfy-face-to-css):
* org-src.el (org-src-associate-babel-session, org-src-get-lang-mode):
* org-bibtex.el (org-bibtex-get, org-bibtex-ask, org-bibtex)
(org-bibtex-check):
* ob-tangle.el (org-babel-tangle, org-babel-spec-to-string)
(org-babel-tangle-single-block, org-babel-tangle-comment-links):
* ob-table.el (sbe):
* ob-sqlite.el (org-babel-sqlite-expand-vars):
* ob-sql.el (org-babel-sql-expand-vars):
* ob-shen.el (org-babel-execute:shen):
* ob-sh.el (org-babel-execute:sh, org-babel-sh-evaluate):
* ob-scala.el (org-babel-scala-evaluate):
* ob-ruby.el (org-babel-ruby-table-or-string)
(org-babel-ruby-evaluate):
* ob-python.el (org-babel-python-table-or-string)
(org-babel-python-evaluate-external-process)
(org-babel-python-evaluate-session):
* ob-picolisp.el (org-babel-execute:picolisp):
* ob-perl.el (org-babel-perl-evaluate):
* ob-maxima.el (org-babel-execute:maxima):
* ob-lisp.el (org-babel-execute:lisp):
* ob-java.el (org-babel-execute:java):
* ob-io.el (org-babel-io-evaluate):
* ob-haskell.el (org-babel-execute:haskell):
* ob-fortran.el (org-babel-execute:fortran):
* ob-exp.el (org-babel-exp-code):
* ob-emacs-lisp.el (org-babel-execute:emacs-lisp):
* ob-ditaa.el (org-babel-execute:ditaa):
* ob-core.el (org-babel-execute-src-block, org-babel-sha1-hash)
(org-babel-parse-header-arguments, org-babel-reassemble-table)
(org-babel-goto-src-block-head, org-babel-mark-block)
(org-babel-expand-noweb-references, org-babel-script-escape)
(org-babel-process-file-name):
* ob-clojure.el (org-babel-execute:clojure):
* ob-calc.el (org-babel-execute:calc):
* ob-awk.el (org-babel-execute:awk):
* ob-abc.el (org-babel-execute:abc):
* ob-R.el (org-babel-expand-body:R):
* ob-C.el (org-babel-C-execute): Avoid deprecated ((lambda) ...).
2013-11-12 Glenn Morris <rgm@gnu.org>
* ox-html.el (org-html-scripts): Add 2013 to copyright years.
(org-html-infojs-template): Copyright holder to FSF.
2013-11-12 19:57:31 +00:00
|
|
|
;; This file is part of GNU Emacs.
|
|
|
|
|
2013-02-23 08:56:24 +00:00
|
|
|
;; GNU Emacs is free software: you can redistribute it and/or modify
|
2013-02-16 13:03:59 +00:00
|
|
|
;; it under the terms of the GNU General Public License as published by
|
|
|
|
;; the Free Software Foundation, either version 3 of the License, or
|
|
|
|
;; (at your option) any later version.
|
|
|
|
|
2013-02-23 08:56:24 +00:00
|
|
|
;; GNU Emacs is distributed in the hope that it will be useful,
|
2013-02-16 13:03:59 +00:00
|
|
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
;; GNU General Public License for more details.
|
|
|
|
|
|
|
|
;; You should have received a copy of the GNU General Public License
|
2013-02-23 08:56:24 +00:00
|
|
|
;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
|
2013-02-16 13:03:59 +00:00
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
|
|
|
;; Macros are expanded with `org-macro-replace-all', which relies
|
|
|
|
;; internally on `org-macro-expand'.
|
|
|
|
|
|
|
|
;; Default templates for expansion are stored in the buffer-local
|
|
|
|
;; variable `org-macro-templates'. This variable is updated by
|
|
|
|
;; `org-macro-initialize-templates', which recursively calls
|
|
|
|
;; `org-macro--collect-macros' in order to read setup files.
|
|
|
|
|
2015-01-20 23:25:47 +00:00
|
|
|
;; Argument in macros are separated with commas. Proper escaping rules
|
|
|
|
;; are implemented in `org-macro-escape-arguments' and arguments can
|
|
|
|
;; be extracted from a string with `org-macro-extract-arguments'.
|
|
|
|
|
2013-02-16 13:03:59 +00:00
|
|
|
;; Along with macros defined through #+MACRO: keyword, default
|
|
|
|
;; templates include the following hard-coded macros:
|
|
|
|
;; {{{time(format-string)}}}, {{{property(node-property)}}},
|
|
|
|
;; {{{input-file}}} and {{{modification-time(format-string)}}}.
|
|
|
|
|
|
|
|
;; Upon exporting, "ox.el" will also provide {{{author}}}, {{{date}}},
|
|
|
|
;; {{{email}}} and {{{title}}} macros.
|
|
|
|
|
|
|
|
;;; Code:
|
2013-06-01 09:21:48 +00:00
|
|
|
(require 'org-macs)
|
2015-05-30 12:28:35 +00:00
|
|
|
(require 'org-compat)
|
2013-02-16 13:03:59 +00:00
|
|
|
|
2013-11-24 23:24:54 +00:00
|
|
|
(declare-function org-element-at-point "org-element" ())
|
2013-02-16 13:03:59 +00:00
|
|
|
(declare-function org-element-context "org-element" (&optional element))
|
2015-05-30 12:28:35 +00:00
|
|
|
(declare-function org-element-map "org-element"
|
|
|
|
(data types fun &optional info first-match no-recursion
|
|
|
|
with-affiliated))
|
|
|
|
(declare-function org-element-parse-buffer "org-element"
|
|
|
|
(&optional granularity visible-only))
|
2013-02-16 13:03:59 +00:00
|
|
|
(declare-function org-element-property "org-element" (property element))
|
|
|
|
(declare-function org-element-type "org-element" (element))
|
2013-02-19 19:32:09 +00:00
|
|
|
(declare-function org-file-contents "org" (file &optional noerror))
|
2015-05-30 12:28:35 +00:00
|
|
|
(declare-function org-mode "org" ())
|
|
|
|
(declare-function org-remove-double-quotes "org" (s))
|
2013-02-19 19:32:09 +00:00
|
|
|
(declare-function org-with-wide-buffer "org-macs" (&rest body))
|
2013-02-16 13:03:59 +00:00
|
|
|
|
|
|
|
;;; Variables
|
|
|
|
|
|
|
|
(defvar org-macro-templates nil
|
|
|
|
"Alist containing all macro templates in current buffer.
|
|
|
|
Associations are in the shape of (NAME . TEMPLATE) where NAME
|
|
|
|
stands for macro's name and template for its replacement value,
|
|
|
|
both as strings. This is an internal variable. Do not set it
|
|
|
|
directly, use instead:
|
|
|
|
|
|
|
|
#+MACRO: name template")
|
|
|
|
(make-variable-buffer-local 'org-macro-templates)
|
|
|
|
|
|
|
|
|
|
|
|
;;; Functions
|
|
|
|
|
2013-04-01 13:44:15 +00:00
|
|
|
(defun org-macro--collect-macros ()
|
2013-02-16 13:03:59 +00:00
|
|
|
"Collect macro definitions in current buffer and setup files.
|
2013-04-01 13:44:15 +00:00
|
|
|
Return an alist containing all macro templates found."
|
|
|
|
(let* (collect-macros ; For byte-compiler.
|
|
|
|
(collect-macros
|
|
|
|
(lambda (files templates)
|
|
|
|
;; Return an alist of macro templates. FILES is a list of
|
|
|
|
;; setup files names read so far, used to avoid circular
|
|
|
|
;; dependencies. TEMPLATES is the alist collected so far.
|
|
|
|
(let ((case-fold-search t))
|
|
|
|
(org-with-wide-buffer
|
|
|
|
(goto-char (point-min))
|
|
|
|
(while (re-search-forward
|
|
|
|
"^[ \t]*#\\+\\(MACRO\\|SETUPFILE\\):" nil t)
|
2015-03-28 08:54:37 +00:00
|
|
|
(let ((element (org-element-at-point)))
|
|
|
|
(when (eq (org-element-type element) 'keyword)
|
|
|
|
(let ((val (org-element-property :value element)))
|
|
|
|
(if (equal (org-element-property :key element) "MACRO")
|
|
|
|
;; Install macro in TEMPLATES.
|
|
|
|
(when (string-match
|
|
|
|
"^\\(.*?\\)\\(?:\\s-+\\(.*\\)\\)?\\s-*$" val)
|
|
|
|
(let* ((name (match-string 1 val))
|
|
|
|
(template (or (match-string 2 val) ""))
|
|
|
|
(old-cell (assoc name templates)))
|
|
|
|
(if old-cell (setcdr old-cell template)
|
|
|
|
(push (cons name template) templates))))
|
|
|
|
;; Enter setup file.
|
|
|
|
(let ((file (expand-file-name
|
|
|
|
(org-remove-double-quotes val))))
|
|
|
|
(unless (member file files)
|
|
|
|
(with-temp-buffer
|
2015-09-20 05:28:53 +00:00
|
|
|
(setq default-directory
|
|
|
|
(file-name-directory file))
|
2015-03-28 08:54:37 +00:00
|
|
|
(org-mode)
|
|
|
|
(insert (org-file-contents file 'noerror))
|
|
|
|
(setq templates
|
|
|
|
(funcall collect-macros (cons file files)
|
|
|
|
templates)))))))))))
|
2013-04-01 13:44:15 +00:00
|
|
|
templates))))
|
|
|
|
(funcall collect-macros nil nil)))
|
2013-02-16 13:03:59 +00:00
|
|
|
|
|
|
|
(defun org-macro-initialize-templates ()
|
|
|
|
"Collect macro templates defined in current buffer.
|
|
|
|
Templates are stored in buffer-local variable
|
|
|
|
`org-macro-templates'. In addition to buffer-defined macros, the
|
|
|
|
function installs the following ones: \"property\",
|
|
|
|
\"time\". and, if the buffer is associated to a file,
|
|
|
|
\"input-file\" and \"modification-time\"."
|
2013-04-01 13:44:15 +00:00
|
|
|
(let* ((templates (org-macro--collect-macros))
|
2013-02-16 13:03:59 +00:00
|
|
|
(update-templates
|
|
|
|
(lambda (cell)
|
|
|
|
(let ((old-template (assoc (car cell) templates)))
|
|
|
|
(if old-template (setcdr old-template (cdr cell))
|
|
|
|
(push cell templates))))))
|
|
|
|
;; Install hard-coded macros.
|
2015-03-24 08:44:27 +00:00
|
|
|
(mapc update-templates
|
2015-05-10 09:48:13 +00:00
|
|
|
(list (cons "property"
|
|
|
|
"(eval (save-excursion
|
|
|
|
(let ((l \"$2\"))
|
|
|
|
(when (org-string-nw-p l)
|
|
|
|
(condition-case _
|
|
|
|
(let ((org-link-search-must-match-exact-headline t))
|
2015-08-04 12:08:33 +00:00
|
|
|
(org-link-search l nil t))
|
2015-05-10 09:48:13 +00:00
|
|
|
(error
|
|
|
|
(error \"Macro property failed: cannot find location %s\"
|
|
|
|
l)))))
|
|
|
|
(org-entry-get nil \"$1\" 'selective)))")
|
2013-02-16 13:03:59 +00:00
|
|
|
(cons "time" "(eval (format-time-string \"$1\"))")))
|
|
|
|
(let ((visited-file (buffer-file-name (buffer-base-buffer))))
|
|
|
|
(when (and visited-file (file-exists-p visited-file))
|
2015-03-24 08:44:27 +00:00
|
|
|
(mapc update-templates
|
2013-02-16 13:03:59 +00:00
|
|
|
(list (cons "input-file" (file-name-nondirectory visited-file))
|
|
|
|
(cons "modification-time"
|
|
|
|
(format "(eval (format-time-string \"$1\" '%s))"
|
|
|
|
(prin1-to-string
|
|
|
|
(nth 5 (file-attributes visited-file)))))))))
|
|
|
|
(setq org-macro-templates templates)))
|
|
|
|
|
|
|
|
(defun org-macro-expand (macro templates)
|
|
|
|
"Return expanded MACRO, as a string.
|
|
|
|
MACRO is an object, obtained, for example, with
|
|
|
|
`org-element-context'. TEMPLATES is an alist of templates used
|
|
|
|
for expansion. See `org-macro-templates' for a buffer-local
|
|
|
|
default value. Return nil if no template was found."
|
|
|
|
(let ((template
|
|
|
|
;; Macro names are case-insensitive.
|
|
|
|
(cdr (assoc-string (org-element-property :key macro) templates t))))
|
|
|
|
(when template
|
|
|
|
(let ((value (replace-regexp-in-string
|
|
|
|
"\\$[0-9]+"
|
|
|
|
(lambda (arg)
|
|
|
|
(or (nth (1- (string-to-number (substring arg 1)))
|
|
|
|
(org-element-property :args macro))
|
|
|
|
;; No argument: remove place-holder.
|
|
|
|
""))
|
2013-03-17 09:20:10 +00:00
|
|
|
template nil 'literal)))
|
2013-02-16 13:03:59 +00:00
|
|
|
;; VALUE starts with "(eval": it is a s-exp, `eval' it.
|
|
|
|
(when (string-match "\\`(eval\\>" value)
|
|
|
|
(setq value (eval (read value))))
|
|
|
|
;; Return string.
|
|
|
|
(format "%s" (or value ""))))))
|
|
|
|
|
2015-04-05 11:40:44 +00:00
|
|
|
(defun org-macro-replace-all (templates &optional finalize keywords)
|
2013-02-16 13:03:59 +00:00
|
|
|
"Replace all macros in current buffer by their expansion.
|
2014-09-28 03:50:23 +00:00
|
|
|
|
2013-02-16 13:03:59 +00:00
|
|
|
TEMPLATES is an alist of templates used for expansion. See
|
2014-09-28 03:50:23 +00:00
|
|
|
`org-macro-templates' for a buffer-local default value.
|
|
|
|
|
|
|
|
If optional arg FINALIZE is non-nil, raise an error if a macro is
|
2015-05-10 08:05:46 +00:00
|
|
|
found in the buffer with no definition in TEMPLATES.
|
|
|
|
|
|
|
|
Optional argument KEYWORDS, when non-nil is a list of keywords,
|
|
|
|
as strings, where macro expansion is allowed."
|
2013-02-16 13:03:59 +00:00
|
|
|
(save-excursion
|
|
|
|
(goto-char (point-min))
|
2015-04-05 11:40:44 +00:00
|
|
|
(let ((properties-regexp
|
|
|
|
(format "\\`EXPORT_%s\\+?\\'" (regexp-opt keywords)))
|
|
|
|
record)
|
2013-02-16 13:03:59 +00:00
|
|
|
(while (re-search-forward "{{{[-A-Za-z0-9_]" nil t)
|
2015-04-05 11:40:44 +00:00
|
|
|
(let* ((datum (save-match-data (org-element-context)))
|
|
|
|
(type (org-element-type datum))
|
|
|
|
(macro
|
|
|
|
(cond
|
|
|
|
((eq type 'macro) datum)
|
|
|
|
;; In parsed keywords and associated node properties,
|
|
|
|
;; force macro recognition.
|
|
|
|
((or (and (eq type 'keyword)
|
|
|
|
(member (org-element-property :key datum) keywords))
|
|
|
|
(and (eq type 'node-property)
|
|
|
|
(org-string-match-p
|
|
|
|
properties-regexp
|
|
|
|
(org-element-property :key datum))))
|
|
|
|
(save-restriction
|
|
|
|
(narrow-to-region (match-beginning 0) (line-end-position))
|
|
|
|
(org-element-map (org-element-parse-buffer) 'macro
|
|
|
|
#'identity nil t))))))
|
|
|
|
(when macro
|
|
|
|
(let* ((value (org-macro-expand macro templates))
|
|
|
|
(begin (org-element-property :begin macro))
|
2013-02-16 13:03:59 +00:00
|
|
|
(signature (list begin
|
2015-04-05 11:40:44 +00:00
|
|
|
macro
|
|
|
|
(org-element-property :args macro))))
|
2013-02-16 13:03:59 +00:00
|
|
|
;; Avoid circular dependencies by checking if the same
|
|
|
|
;; macro with the same arguments is expanded at the same
|
|
|
|
;; position twice.
|
2015-04-05 11:40:44 +00:00
|
|
|
(cond ((member signature record)
|
|
|
|
(error "Circular macro expansion: %s"
|
|
|
|
(org-element-property :key macro)))
|
|
|
|
(value
|
|
|
|
(push signature record)
|
|
|
|
(delete-region
|
|
|
|
begin
|
|
|
|
;; Preserve white spaces after the macro.
|
|
|
|
(progn (goto-char (org-element-property :end macro))
|
|
|
|
(skip-chars-backward " \t")
|
|
|
|
(point)))
|
|
|
|
;; Leave point before replacement in case of
|
|
|
|
;; recursive expansions.
|
|
|
|
(save-excursion (insert value)))
|
|
|
|
(finalize
|
|
|
|
(error "Undefined Org macro: %s; aborting"
|
|
|
|
(org-element-property :key macro)))))))))))
|
2013-02-16 13:03:59 +00:00
|
|
|
|
2015-01-20 23:25:47 +00:00
|
|
|
(defun org-macro-escape-arguments (&rest args)
|
|
|
|
"Build macro's arguments string from ARGS.
|
|
|
|
ARGS are strings. Return value is a string with arguments
|
|
|
|
properly escaped and separated with commas. This is the opposite
|
|
|
|
of `org-macro-extract-arguments'."
|
|
|
|
(let ((s ""))
|
|
|
|
(dolist (arg (reverse args) (substring s 1))
|
|
|
|
(setq s
|
|
|
|
(concat
|
|
|
|
","
|
|
|
|
(replace-regexp-in-string
|
|
|
|
"\\(\\\\*\\),"
|
|
|
|
(lambda (m)
|
|
|
|
(concat (make-string (1+ (* 2 (length (match-string 1 m)))) ?\\)
|
|
|
|
","))
|
|
|
|
;; If a non-terminal argument ends on backslashes, make
|
|
|
|
;; sure to also escape them as they will be followed by
|
|
|
|
;; a comma.
|
|
|
|
(concat arg (and (not (equal s ""))
|
|
|
|
(string-match "\\\\+\\'" arg)
|
|
|
|
(match-string 0 arg)))
|
|
|
|
nil t)
|
|
|
|
s)))))
|
|
|
|
|
|
|
|
(defun org-macro-extract-arguments (s)
|
|
|
|
"Extract macro arguments from string S.
|
|
|
|
S is a string containing comma separated values properly escaped.
|
|
|
|
Return a list of arguments, as strings. This is the opposite of
|
|
|
|
`org-macro-escape-arguments'."
|
|
|
|
;; Do not use `org-split-string' since empty strings are
|
|
|
|
;; meaningful here.
|
|
|
|
(split-string
|
|
|
|
(replace-regexp-in-string
|
|
|
|
"\\(\\\\*\\),"
|
|
|
|
(lambda (str)
|
|
|
|
(let ((len (length (match-string 1 str))))
|
|
|
|
(concat (make-string (/ len 2) ?\\)
|
|
|
|
(if (zerop (mod len 2)) "\000" ","))))
|
|
|
|
s nil t)
|
|
|
|
"\000"))
|
|
|
|
|
2013-02-16 13:03:59 +00:00
|
|
|
|
|
|
|
(provide 'org-macro)
|
|
|
|
;;; org-macro.el ends here
|