ob-lob.el 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. ;;; ob-lob.el --- functions supporting the Library of Babel
  2. ;; Copyright (C) 2009-2016 Free Software Foundation, Inc.
  3. ;; Authors: Eric Schulte
  4. ;; Dan Davison
  5. ;; Keywords: literate programming, reproducible research
  6. ;; Homepage: http://orgmode.org
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Code:
  19. (eval-when-compile
  20. (require 'cl))
  21. (require 'ob-core)
  22. (require 'ob-table)
  23. (declare-function org-babel-in-example-or-verbatim "ob-exp" nil)
  24. (declare-function org-element-context "org-element" (&optional element))
  25. (declare-function org-element-property "org-element" (property element))
  26. (declare-function org-element-type "org-element" (element))
  27. (defvar org-babel-library-of-babel nil
  28. "Library of source-code blocks.
  29. This is an association list. Populate the library by adding
  30. files to `org-babel-lob-files'.")
  31. (defcustom org-babel-lob-files nil
  32. "Files used to populate the `org-babel-library-of-babel'.
  33. To add files to this list use the `org-babel-lob-ingest' command."
  34. :group 'org-babel
  35. :version "24.1"
  36. :type '(repeat file))
  37. (defvar org-babel-default-lob-header-args '((:exports . "results"))
  38. "Default header arguments to use when exporting #+lob/call lines.")
  39. (defun org-babel-lob-ingest (&optional file)
  40. "Add all named source blocks defined in FILE to `org-babel-library-of-babel'."
  41. (interactive "fFile: ")
  42. (let ((lob-ingest-count 0))
  43. (org-babel-map-src-blocks file
  44. (let* ((info (org-babel-get-src-block-info 'light))
  45. (source-name (nth 4 info)))
  46. (when source-name
  47. (setq source-name (intern source-name)
  48. org-babel-library-of-babel
  49. (cons (cons source-name info)
  50. (assq-delete-all source-name org-babel-library-of-babel))
  51. lob-ingest-count (1+ lob-ingest-count)))))
  52. (message "%d src block%s added to Library of Babel"
  53. lob-ingest-count (if (> lob-ingest-count 1) "s" ""))
  54. lob-ingest-count))
  55. (defconst org-babel-block-lob-one-liner-regexp
  56. (concat
  57. "^\\([ \t]*?\\)#\\+call:[ \t]+\\([^()\n]+?\\)\\(\\[\\(.*\\)\\]\\|\\(\\)\\)"
  58. "(\\([^\n]*?\\))\\(\\[.+\\]\\|\\)[ \t]*\\(\\([^\n]*\\)\\)?")
  59. "Regexp to match non-inline calls to predefined source block functions.")
  60. (defconst org-babel-inline-lob-one-liner-regexp
  61. (concat
  62. "\\([^\n]*?\\)call_\\([^()[:space:]\n]+?\\)\\(\\[\\(.*?\\)\\]\\|\\(\\)\\)"
  63. "(\\(.*?\\))\\(\\[\\(.*?\\)\\]\\)?")
  64. "Regexp to match inline calls to predefined source block functions.")
  65. (defconst org-babel-lob-one-liner-regexp
  66. (concat "\\(" org-babel-block-lob-one-liner-regexp
  67. "\\|" org-babel-inline-lob-one-liner-regexp "\\)")
  68. "Regexp to match calls to predefined source block functions.")
  69. ;; functions for executing lob one-liners
  70. ;;;###autoload
  71. (defun org-babel-lob-execute-maybe ()
  72. "Execute a Library of Babel source block, if appropriate.
  73. Detect if this is context for a Library Of Babel source block and
  74. if so then run the appropriate source block from the Library."
  75. (interactive)
  76. (let ((info (org-babel-lob-get-info)))
  77. (when info
  78. (org-babel-lob-execute info)
  79. t)))
  80. ;;;###autoload
  81. (defun org-babel-lob-get-info (&optional datum)
  82. "Return a Library of Babel function call as a string.
  83. Return nil when not on an appropriate location. Build string
  84. from `inline-babel-call' or `babel-call' DATUM, when provided."
  85. (let ((context (or datum (org-element-context))))
  86. (when (memq (org-element-type context) '(babel-call inline-babel-call))
  87. (list (format "%s%s(%s)"
  88. (org-element-property :call context)
  89. (let ((in (org-element-property :inside-header context)))
  90. (if in (format "[%s]" in) ""))
  91. (or (org-element-property :arguments context) ""))
  92. (org-element-property :end-header context)
  93. (org-element-property :name context)))))
  94. (defvar org-babel-default-header-args:emacs-lisp) ; Defined in ob-emacs-lisp.el
  95. (defun org-babel-lob-execute (info)
  96. "Execute the lob call specified by INFO."
  97. (let* ((mkinfo (lambda (p)
  98. ;; Make plist P compatible with
  99. ;; `org-babel-get-src-block-info'.
  100. (list "emacs-lisp" "results" p nil (nth 2 info))))
  101. (pre-params
  102. (apply #'org-babel-merge-params
  103. org-babel-default-header-args
  104. org-babel-default-header-args:emacs-lisp
  105. (append
  106. (org-babel-params-from-properties)
  107. (list
  108. (org-babel-parse-header-arguments
  109. (org-no-properties
  110. (concat ":var results="
  111. (mapconcat #'identity (butlast info) " "))))))))
  112. (pre-info (funcall mkinfo pre-params))
  113. (cache-p (and (cdr (assoc :cache pre-params))
  114. (string= "yes" (cdr (assoc :cache pre-params)))))
  115. (new-hash (when cache-p
  116. (org-babel-sha1-hash
  117. ;; Do *not* pre-process params for call line
  118. ;; hash evaluation, since for a call line :var
  119. ;; extension *is* execution.
  120. (let* ((params (nth 2 pre-info))
  121. (sha1-nth2 (list
  122. (cons
  123. (cons :c-var (cdr (assoc :var params)))
  124. (assq-delete-all :var (copy-tree params)))))
  125. (sha1-info (copy-tree pre-info)))
  126. (prog1 sha1-info
  127. (setcar (cddr sha1-info) sha1-nth2))))))
  128. (old-hash (when cache-p (org-babel-current-result-hash pre-info)))
  129. (org-babel-current-src-block-location (point-marker)))
  130. (if (and cache-p (equal new-hash old-hash))
  131. (save-excursion (goto-char (org-babel-where-is-src-block-result
  132. nil pre-info))
  133. (forward-line 1)
  134. (message "%S" (org-babel-read-result)))
  135. (prog1 (let* ((proc-params (org-babel-process-params pre-params))
  136. org-confirm-babel-evaluate)
  137. (org-babel-execute-src-block nil (funcall mkinfo proc-params)))
  138. ;; update the hash
  139. (when new-hash
  140. (org-babel-set-current-result-hash new-hash pre-info))))))
  141. (provide 'ob-lob)
  142. ;; Local variables:
  143. ;; generated-autoload-file: "org-loaddefs.el"
  144. ;; End:
  145. ;;; ob-lob.el ends here