ob-lob.el 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. ;;; ob-lob.el --- functions supporting the Library of Babel
  2. ;; Copyright (C) 2009-2015 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. (if (and (nth 0 info) (not (org-babel-in-example-or-verbatim)))
  78. (progn (org-babel-lob-execute info) t)
  79. nil)))
  80. ;;;###autoload
  81. (defun org-babel-lob-get-info ()
  82. "Return a Library of Babel function call as a string."
  83. (when (save-excursion
  84. (beginning-of-line)
  85. (let ((case-fold-search t))
  86. (looking-at org-babel-lob-one-liner-regexp)))
  87. (let ((context (save-match-data (org-element-context))))
  88. (when (memq (org-element-type context) '(babel-call inline-babel-call))
  89. (list (format "%s%s(%s)"
  90. (org-element-property :call context)
  91. (let ((in (org-element-property :inside-header context)))
  92. (if in (format "[%s]" in) ""))
  93. (or (org-element-property :arguments context) ""))
  94. (org-element-property :end-header context)
  95. (length (if (= (length (match-string 12)) 0)
  96. (match-string-no-properties 2)
  97. (match-string-no-properties 11)))
  98. (org-element-property :name context))))))
  99. (defvar org-babel-default-header-args:emacs-lisp) ; Defined in ob-emacs-lisp.el
  100. (defun org-babel-lob-execute (info)
  101. "Execute the lob call specified by INFO."
  102. (let* ((mkinfo (lambda (p)
  103. (list "emacs-lisp" "results" p nil
  104. (nth 3 info) ;; name
  105. (nth 2 info))))
  106. (pre-params (apply #'org-babel-merge-params
  107. org-babel-default-header-args
  108. org-babel-default-header-args:emacs-lisp
  109. (append
  110. (org-babel-params-from-properties)
  111. (list
  112. (org-babel-parse-header-arguments
  113. (org-no-properties
  114. (concat
  115. ":var results="
  116. (mapconcat #'identity (butlast info 2)
  117. " "))))))))
  118. (pre-info (funcall mkinfo pre-params))
  119. (cache-p (and (cdr (assoc :cache pre-params))
  120. (string= "yes" (cdr (assoc :cache pre-params)))))
  121. (new-hash (when cache-p
  122. (org-babel-sha1-hash
  123. ;; Do *not* pre-process params for call line
  124. ;; hash evaluation, since for a call line :var
  125. ;; extension *is* execution.
  126. (let* ((params (nth 2 pre-info))
  127. (sha1-nth2 (list
  128. (cons
  129. (cons :c-var (cdr (assoc :var params)))
  130. (assq-delete-all :var (copy-tree params)))))
  131. (sha1-info (copy-tree pre-info)))
  132. (prog1 sha1-info
  133. (setcar (cddr sha1-info) sha1-nth2))))))
  134. (old-hash (when cache-p (org-babel-current-result-hash pre-info)))
  135. (org-babel-current-src-block-location (point-marker)))
  136. (if (and cache-p (equal new-hash old-hash))
  137. (save-excursion (goto-char (org-babel-where-is-src-block-result
  138. nil pre-info))
  139. (forward-line 1)
  140. (message "%S" (org-babel-read-result)))
  141. (prog1 (let* ((proc-params (org-babel-process-params pre-params))
  142. org-confirm-babel-evaluate)
  143. (org-babel-execute-src-block nil (funcall mkinfo proc-params)))
  144. ;; update the hash
  145. (when new-hash
  146. (org-babel-set-current-result-hash new-hash pre-info))))))
  147. (provide 'ob-lob)
  148. ;; Local variables:
  149. ;; generated-autoload-file: "org-loaddefs.el"
  150. ;; End:
  151. ;;; ob-lob.el ends here