ob-lob.el 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 ((info (org-babel-lob-get-info)))
  67. (when info
  68. (org-babel-execute-src-block nil info)
  69. t)))
  70. (defun org-babel-lob--src-info (ref)
  71. "Return internal representation for Babel data referenced as REF.
  72. REF is a string. This function looks into the current document
  73. for a Babel call or source block. If none is found, it looks
  74. after REF in the Library of Babel."
  75. (let ((name ref)
  76. (file nil))
  77. ;; Extract the remote file, if specified in the reference.
  78. (when (string-match "\\`\\(.+\\):\\(.+\\)\\'" ref)
  79. (setq file (match-string 1 ref))
  80. (setq name (match-string 2 ref)))
  81. ;; During export, look into the pristine copy of the document
  82. ;; being exported instead of the current one, which could miss
  83. ;; some data.
  84. (with-current-buffer (cond (file (find-file-noselect file t))
  85. (org-babel-exp-reference-buffer)
  86. (t (current-buffer)))
  87. (org-with-point-at 1
  88. (catch :found
  89. (let ((case-fold-search t)
  90. (regexp (org-babel-named-data-regexp-for-name name)))
  91. (while (re-search-forward regexp nil t)
  92. (let ((element (org-element-at-point)))
  93. (when (equal name (org-element-property :name element))
  94. (throw :found
  95. (pcase (org-element-type element)
  96. (`src-block (org-babel-get-src-block-info t element))
  97. (`babel-call (org-babel-lob-get-info element))
  98. ;; Non-executable data found. Since names
  99. ;; are supposed to be unique throughout
  100. ;; a document, bail out.
  101. (_ nil))))))
  102. (cdr (assoc-string ref org-babel-library-of-babel))))))))
  103. ;;;###autoload
  104. (defun org-babel-lob-get-info (&optional datum)
  105. "Return internal representation for Library of Babel function call.
  106. Consider DATUM, when provided, or element at point otherwise.
  107. Return nil when not on an appropriate location. Otherwise return
  108. a list compatible with `org-babel-get-src-block-info', which
  109. see."
  110. (let* ((context (or datum (org-element-context)))
  111. (type (org-element-type context))
  112. (reference (org-element-property :call context)))
  113. (when (memq type '(babel-call inline-babel-call))
  114. (pcase (org-babel-lob--src-info reference)
  115. (`(,language ,body ,header ,_ ,_ ,_ ,coderef)
  116. (let ((begin (org-element-property (if (eq type 'inline-babel-call)
  117. :begin
  118. :post-affiliated)
  119. context)))
  120. (list language
  121. body
  122. (apply #'org-babel-merge-params
  123. header
  124. org-babel-default-lob-header-args
  125. (append
  126. (org-with-point-at begin
  127. (org-babel-params-from-properties language))
  128. (list
  129. (org-babel-parse-header-arguments
  130. (org-element-property :inside-header context))
  131. (let ((args (org-element-property :arguments context)))
  132. (and args
  133. (mapcar (lambda (ref) (cons :var ref))
  134. (org-babel-ref-split-args args))))
  135. (org-babel-parse-header-arguments
  136. (org-element-property :end-header context)))))
  137. nil
  138. (org-element-property :name context)
  139. begin
  140. coderef)))
  141. (_ nil)))))
  142. (provide 'ob-lob)
  143. ;; Local variables:
  144. ;; generated-autoload-file: "org-loaddefs.el"
  145. ;; End:
  146. ;;; ob-lob.el ends here