ob-lob.el 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. ;;; ob-lob.el --- Functions Supporting the Library of Babel -*- lexical-binding: t; -*-
  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. (require 'cl-lib)
  20. (require 'ob-core)
  21. (require 'ob-table)
  22. (declare-function org-element-context "org-element" (&optional element))
  23. (declare-function org-element-property "org-element" (property element))
  24. (declare-function org-element-type "org-element" (element))
  25. (defvar org-babel-library-of-babel nil
  26. "Library of source-code blocks.
  27. This is an association list. Populate the library by adding
  28. files to `org-babel-lob-files'.")
  29. (defcustom org-babel-lob-files nil
  30. "Files used to populate the `org-babel-library-of-babel'.
  31. To add files to this list use the `org-babel-lob-ingest' command."
  32. :group 'org-babel
  33. :version "24.1"
  34. :type '(repeat file))
  35. (defvar org-babel-default-lob-header-args '((:exports . "results"))
  36. "Default header arguments to use when exporting #+lob/call lines.")
  37. (defun org-babel-lob-ingest (&optional file)
  38. "Add all named source blocks defined in FILE to `org-babel-library-of-babel'."
  39. (interactive "fFile: ")
  40. (let ((lob-ingest-count 0))
  41. (org-babel-map-src-blocks file
  42. (let* ((info (org-babel-get-src-block-info 'light))
  43. (source-name (nth 4 info)))
  44. (when source-name
  45. (setq source-name (intern source-name)
  46. org-babel-library-of-babel
  47. (cons (cons source-name info)
  48. (assq-delete-all source-name org-babel-library-of-babel))
  49. lob-ingest-count (1+ lob-ingest-count)))))
  50. (message "%d src block%s added to Library of Babel"
  51. lob-ingest-count (if (> lob-ingest-count 1) "s" ""))
  52. lob-ingest-count))
  53. ;; Functions for executing lob one-liners.
  54. ;;;###autoload
  55. (defun org-babel-lob-execute-maybe ()
  56. "Execute a Library of Babel source block, if appropriate.
  57. Detect if this is context for a Library Of Babel source block and
  58. if so then run the appropriate source block from the Library."
  59. (interactive)
  60. (let ((info (org-babel-lob-get-info)))
  61. (when info
  62. (org-babel-lob-execute info)
  63. t)))
  64. ;;;###autoload
  65. (defun org-babel-lob-get-info (&optional datum)
  66. "Return a Library of Babel function call as a string.
  67. Return nil when not on an appropriate location. Build string
  68. from `inline-babel-call' or `babel-call' DATUM, when provided."
  69. (let* ((context (or datum (org-element-context)))
  70. (type (org-element-type context)))
  71. (when (memq type '(babel-call inline-babel-call))
  72. (list (format "%s%s(%s)"
  73. (org-element-property :call context)
  74. (let ((in (org-element-property :inside-header context)))
  75. (if in (format "[%s]" in) ""))
  76. (or (org-element-property :arguments context) ""))
  77. (org-element-property :end-header context)
  78. (org-element-property :name context)
  79. (org-element-property
  80. (if (eq type 'babel-call) :post-affiliated :begin)
  81. datum)))))
  82. (defvar org-babel-default-header-args:emacs-lisp) ; Defined in ob-emacs-lisp.el
  83. (defun org-babel-lob-execute (info)
  84. "Execute the lob call specified by INFO."
  85. (let* ((mkinfo (lambda (p)
  86. ;; Make plist P compatible with
  87. ;; `org-babel-get-src-block-info'.
  88. (list
  89. "emacs-lisp" "results" p nil (nth 2 info) (nth 3 info))))
  90. (pre-params
  91. (apply #'org-babel-merge-params
  92. org-babel-default-header-args
  93. org-babel-default-header-args:emacs-lisp
  94. (append
  95. (org-babel-params-from-properties)
  96. (list
  97. (org-babel-parse-header-arguments
  98. (org-no-properties
  99. (concat ":var results="
  100. (mapconcat #'identity (butlast info 2) " "))))))))
  101. (pre-info (funcall mkinfo pre-params))
  102. (cache-p (and (cdr (assoc :cache pre-params))
  103. (string= "yes" (cdr (assoc :cache pre-params)))))
  104. (new-hash (when cache-p
  105. (org-babel-sha1-hash
  106. ;; Do *not* pre-process params for call line
  107. ;; hash evaluation, since for a call line :var
  108. ;; extension *is* execution.
  109. (let* ((params (nth 2 pre-info))
  110. (sha1-nth2 (list
  111. (cons
  112. (cons :c-var (cdr (assoc :var params)))
  113. (assq-delete-all :var (copy-tree params)))))
  114. (sha1-info (copy-tree pre-info)))
  115. (prog1 sha1-info
  116. (setcar (cddr sha1-info) sha1-nth2))))))
  117. (old-hash (when cache-p (org-babel-current-result-hash pre-info)))
  118. (org-babel-current-src-block-location (point-marker)))
  119. (if (and cache-p (equal new-hash old-hash))
  120. (save-excursion (goto-char (org-babel-where-is-src-block-result
  121. nil pre-info))
  122. (forward-line 1)
  123. (message "%S" (org-babel-read-result)))
  124. (prog1 (let* ((proc-params (org-babel-process-params pre-params))
  125. org-confirm-babel-evaluate)
  126. (org-babel-execute-src-block nil (funcall mkinfo proc-params)))
  127. ;; update the hash
  128. (when new-hash
  129. (org-babel-set-current-result-hash new-hash pre-info))))))
  130. (provide 'ob-lob)
  131. ;; Local variables:
  132. ;; generated-autoload-file: "org-loaddefs.el"
  133. ;; End:
  134. ;;; ob-lob.el ends here