ob-lob.el 5.8 KB

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