ob-lob.el 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. ;;; ob-lob.el --- Functions Supporting the Library of Babel -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2009-2022 Free Software Foundation, Inc.
  3. ;; Authors: Eric Schulte
  4. ;; Dan Davison
  5. ;; Keywords: literate programming, reproducible research
  6. ;; URL: https://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 <https://www.gnu.org/licenses/>.
  18. ;;; Code:
  19. (require 'org-macs)
  20. (org-assert-version)
  21. (require 'cl-lib)
  22. (require 'ob-core)
  23. (require 'ob-table)
  24. (declare-function org-babel-ref-split-args "ob-ref" (arg-string))
  25. (declare-function org-element-at-point "org-element" (&optional pom cached-only))
  26. (declare-function org-element-context "org-element" (&optional element))
  27. (declare-function org-element-property "org-element" (property element))
  28. (declare-function org-element-type "org-element" (element))
  29. (defvar org-babel-library-of-babel nil
  30. "Library of source-code blocks.
  31. This is an association list. Populate the library by calling
  32. `org-babel-lob-ingest' on files containing source blocks.")
  33. (defvar org-babel-default-lob-header-args '((:exports . "results"))
  34. "Default header arguments to use when exporting Babel calls.
  35. By default, a Babel call inherits its arguments from the source
  36. block being called. Header arguments defined in this variable
  37. take precedence over these. It is useful for properties that
  38. should not be inherited from a source block.")
  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. (setf (nth 1 info)
  48. (if (org-babel-noweb-p (nth 2 info) :eval)
  49. (org-babel-expand-noweb-references info)
  50. (nth 1 info)))
  51. (let ((source (intern source-name)))
  52. (setq org-babel-library-of-babel
  53. (cons (cons source info)
  54. (assq-delete-all source org-babel-library-of-babel))))
  55. (cl-incf lob-ingest-count))))
  56. (message "%d source block%s added to Library of Babel"
  57. lob-ingest-count (if (> lob-ingest-count 1) "s" ""))
  58. lob-ingest-count))
  59. ;; Functions for executing lob one-liners.
  60. ;;;###autoload
  61. (defun org-babel-lob-execute-maybe ()
  62. "Execute a Library of Babel source block, if appropriate.
  63. Detect if this is context for a Library Of Babel source block and
  64. if so then run the appropriate source block from the Library."
  65. (interactive)
  66. (let* ((datum (org-element-context))
  67. (info (org-babel-lob-get-info datum)))
  68. (when info
  69. (org-babel-execute-src-block nil info nil (org-element-type datum))
  70. t)))
  71. (defun org-babel-lob--src-info (ref)
  72. "Return internal representation for Babel data referenced as REF.
  73. REF is a string. This function looks into the current document
  74. for a Babel call or source block. If none is found, it looks
  75. after REF in the Library of Babel."
  76. (let ((name ref)
  77. (file nil))
  78. ;; Extract the remote file, if specified in the reference.
  79. (when (string-match "\\`\\(.+\\):\\(.+\\)\\'" ref)
  80. (setq file (match-string 1 ref))
  81. (setq name (match-string 2 ref)))
  82. ;; During export, look into the pristine copy of the document
  83. ;; being exported instead of the current one, which could miss
  84. ;; some data.
  85. (with-current-buffer (cond (file (find-file-noselect file t))
  86. (org-babel-exp-reference-buffer)
  87. (t (current-buffer)))
  88. (org-with-point-at 1
  89. (catch :found
  90. (let ((case-fold-search t)
  91. (regexp (org-babel-named-data-regexp-for-name name)))
  92. (while (re-search-forward regexp nil t)
  93. (let ((element (org-element-at-point)))
  94. (when (equal name (org-element-property :name element))
  95. (throw :found
  96. (pcase (org-element-type element)
  97. (`src-block (org-babel-get-src-block-info t element))
  98. (`babel-call (org-babel-lob-get-info element))
  99. ;; Non-executable data found. Since names
  100. ;; are supposed to be unique throughout
  101. ;; a document, bail out.
  102. (_ nil))))))
  103. (cdr (assoc-string ref org-babel-library-of-babel))))))))
  104. ;;;###autoload
  105. (defun org-babel-lob-get-info (&optional datum)
  106. "Return internal representation for Library of Babel function call.
  107. Consider DATUM, when provided, or element at point otherwise.
  108. Return nil when not on an appropriate location. Otherwise return
  109. a list compatible with `org-babel-get-src-block-info', which
  110. see."
  111. (let* ((context (or datum (org-element-context)))
  112. (type (org-element-type context))
  113. (reference (org-element-property :call context)))
  114. (when (memq type '(babel-call inline-babel-call))
  115. (pcase (org-babel-lob--src-info reference)
  116. (`(,language ,body ,header ,_ ,_ ,_ ,coderef)
  117. (let ((begin (org-element-property (if (eq type 'inline-babel-call)
  118. :begin
  119. :post-affiliated)
  120. context)))
  121. (list language
  122. body
  123. (apply #'org-babel-merge-params
  124. header
  125. org-babel-default-lob-header-args
  126. (append
  127. (org-with-point-at begin
  128. (org-babel-params-from-properties language))
  129. (list
  130. (org-babel-parse-header-arguments
  131. (org-element-property :inside-header context))
  132. (let ((args (org-element-property :arguments context)))
  133. (and args
  134. (mapcar (lambda (ref) (cons :var ref))
  135. (org-babel-ref-split-args args))))
  136. (org-babel-parse-header-arguments
  137. (org-element-property :end-header context)))))
  138. nil
  139. (org-element-property :name context)
  140. begin
  141. coderef)))
  142. (_ nil)))))
  143. (provide 'ob-lob)
  144. ;; Local variables:
  145. ;; generated-autoload-file: "org-loaddefs.el"
  146. ;; End:
  147. ;;; ob-lob.el ends here