org-macro.el 7.3 KB

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