org-macro.el 7.6 KB

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