org-macro.el 7.1 KB

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