ob-lob.el 5.6 KB

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