org-macro.el 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. ;;; org-macro.el --- Macro Replacement Code for Org Mode
  2. ;; Copyright (C) 2013-2014 Free Software Foundation, Inc.
  3. ;; Author: Nicolas Goaziou <n.goaziou@gmail.com>
  4. ;; Keywords: outlines, hypermedia, calendar, wp
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;; Macros are expanded with `org-macro-replace-all', which relies
  18. ;; internally on `org-macro-expand'.
  19. ;; Default templates for expansion are stored in the buffer-local
  20. ;; variable `org-macro-templates'. This variable is updated by
  21. ;; `org-macro-initialize-templates', which recursively calls
  22. ;; `org-macro--collect-macros' in order to read setup files.
  23. ;; Along with macros defined through #+MACRO: keyword, default
  24. ;; templates include the following hard-coded macros:
  25. ;; {{{time(format-string)}}}, {{{property(node-property)}}},
  26. ;; {{{input-file}}} and {{{modification-time(format-string)}}}.
  27. ;; Upon exporting, "ox.el" will also provide {{{author}}}, {{{date}}},
  28. ;; {{{email}}} and {{{title}}} macros.
  29. ;;; Code:
  30. (require 'org-macs)
  31. (declare-function org-element-at-point "org-element" (&optional keep-trail))
  32. (declare-function org-element-context "org-element" (&optional element))
  33. (declare-function org-element-property "org-element" (property element))
  34. (declare-function org-element-type "org-element" (element))
  35. (declare-function org-remove-double-quotes "org" (s))
  36. (declare-function org-mode "org" ())
  37. (declare-function org-file-contents "org" (file &optional noerror))
  38. (declare-function org-with-wide-buffer "org-macs" (&rest body))
  39. ;;; Variables
  40. (defvar org-macro-templates nil
  41. "Alist containing all macro templates in current buffer.
  42. Associations are in the shape of (NAME . TEMPLATE) where NAME
  43. stands for macro's name and template for its replacement value,
  44. both as strings. This is an internal variable. Do not set it
  45. directly, use instead:
  46. #+MACRO: name template")
  47. (make-variable-buffer-local 'org-macro-templates)
  48. ;;; Functions
  49. (defun org-macro--collect-macros ()
  50. "Collect macro definitions in current buffer and setup files.
  51. Return an alist containing all macro templates found."
  52. (let* (collect-macros ; For byte-compiler.
  53. (collect-macros
  54. (lambda (files templates)
  55. ;; Return an alist of macro templates. FILES is a list of
  56. ;; setup files names read so far, used to avoid circular
  57. ;; dependencies. TEMPLATES is the alist collected so far.
  58. (let ((case-fold-search t))
  59. (org-with-wide-buffer
  60. (goto-char (point-min))
  61. (while (re-search-forward
  62. "^[ \t]*#\\+\\(MACRO\\|SETUPFILE\\):" nil t)
  63. (let ((element (org-element-at-point)))
  64. (when (eq (org-element-type element) 'keyword)
  65. (let ((val (org-element-property :value element)))
  66. (if (equal (org-element-property :key element) "MACRO")
  67. ;; Install macro in TEMPLATES.
  68. (when (string-match
  69. "^\\(.*?\\)\\(?:\\s-+\\(.*\\)\\)?\\s-*$" val)
  70. (let* ((name (match-string 1 val))
  71. (template (or (match-string 2 val) ""))
  72. (old-cell (assoc name templates)))
  73. (if old-cell (setcdr old-cell template)
  74. (push (cons name template) templates))))
  75. ;; Enter setup file.
  76. (let ((file (expand-file-name
  77. (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. (funcall collect-macros (cons file files)
  84. templates)))))))))))
  85. templates))))
  86. (funcall collect-macros nil nil)))
  87. (defun org-macro-initialize-templates ()
  88. "Collect macro templates defined in current buffer.
  89. Templates are stored in buffer-local variable
  90. `org-macro-templates'. In addition to buffer-defined macros, the
  91. function installs the following ones: \"property\",
  92. \"time\". and, if the buffer is associated to a file,
  93. \"input-file\" and \"modification-time\"."
  94. (let* ((templates (org-macro--collect-macros))
  95. (update-templates
  96. (lambda (cell)
  97. (let ((old-template (assoc (car cell) templates)))
  98. (if old-template (setcdr old-template (cdr cell))
  99. (push cell templates))))))
  100. ;; Install hard-coded macros.
  101. (mapc (lambda (cell) (funcall update-templates cell))
  102. (list (cons "property" "(eval (org-entry-get nil \"$1\" 'selective))")
  103. (cons "time" "(eval (format-time-string \"$1\"))")))
  104. (let ((visited-file (buffer-file-name (buffer-base-buffer))))
  105. (when (and visited-file (file-exists-p visited-file))
  106. (mapc (lambda (cell) (funcall update-templates cell))
  107. (list (cons "input-file" (file-name-nondirectory visited-file))
  108. (cons "modification-time"
  109. (format "(eval (format-time-string \"$1\" '%s))"
  110. (prin1-to-string
  111. (nth 5 (file-attributes visited-file)))))))))
  112. (setq org-macro-templates templates)))
  113. (defun org-macro-expand (macro templates)
  114. "Return expanded MACRO, as a string.
  115. MACRO is an object, obtained, for example, with
  116. `org-element-context'. TEMPLATES is an alist of templates used
  117. for expansion. See `org-macro-templates' for a buffer-local
  118. default value. Return nil if no template was found."
  119. (let ((template
  120. ;; Macro names are case-insensitive.
  121. (cdr (assoc-string (org-element-property :key macro) templates t))))
  122. (when template
  123. (let ((value (replace-regexp-in-string
  124. "\\$[0-9]+"
  125. (lambda (arg)
  126. (or (nth (1- (string-to-number (substring arg 1)))
  127. (org-element-property :args macro))
  128. ;; No argument: remove place-holder.
  129. ""))
  130. template nil 'literal)))
  131. ;; VALUE starts with "(eval": it is a s-exp, `eval' it.
  132. (when (string-match "\\`(eval\\>" value)
  133. (setq value (eval (read value))))
  134. ;; Return string.
  135. (format "%s" (or value ""))))))
  136. (defun org-macro-replace-all (templates)
  137. "Replace all macros in current buffer by their expansion.
  138. TEMPLATES is an alist of templates used for expansion. See
  139. `org-macro-templates' for a buffer-local default value."
  140. (save-excursion
  141. (goto-char (point-min))
  142. (let (record)
  143. (while (re-search-forward "{{{[-A-Za-z0-9_]" nil t)
  144. (let ((object (org-element-context)))
  145. (when (eq (org-element-type object) 'macro)
  146. (let* ((value (org-macro-expand object templates))
  147. (begin (org-element-property :begin object))
  148. (signature (list begin
  149. object
  150. (org-element-property :args object))))
  151. ;; Avoid circular dependencies by checking if the same
  152. ;; macro with the same arguments is expanded at the same
  153. ;; position twice.
  154. (if (member signature record)
  155. (error "Circular macro expansion: %s"
  156. (org-element-property :key object))
  157. (when value
  158. (push signature record)
  159. (delete-region
  160. begin
  161. ;; Preserve white spaces after the macro.
  162. (progn (goto-char (org-element-property :end object))
  163. (skip-chars-backward " \t")
  164. (point)))
  165. ;; Leave point before replacement in case of recursive
  166. ;; expansions.
  167. (save-excursion (insert value)))))))))))
  168. (provide 'org-macro)
  169. ;;; org-macro.el ends here