ob-lob.el 5.9 KB

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