org-macro.el 7.5 KB

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