ob-lob.el 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. ;;; ob-lob.el --- Functions Supporting the Library of Babel -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2009-2016 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-element-at-point "org-element" ())
  23. (declare-function org-element-context "org-element" (&optional element))
  24. (declare-function org-element-property "org-element" (property element))
  25. (declare-function org-element-type "org-element" (element))
  26. (defvar org-babel-library-of-babel nil
  27. "Library of source-code blocks.
  28. This is an association list. Populate the library by adding
  29. files to `org-babel-lob-files'.")
  30. (defcustom org-babel-lob-files nil
  31. "Files used to populate the `org-babel-library-of-babel'.
  32. To add files to this list use the `org-babel-lob-ingest' command."
  33. :group 'org-babel
  34. :version "24.1"
  35. :type '(repeat file))
  36. (defvar org-babel-default-lob-header-args
  37. '((:cache . "no")
  38. (:exports . "results")
  39. (:hlines . "no")
  40. (:noweb . "no")
  41. (:results . "replace")
  42. (:session . "none")
  43. (:tangle . "no"))
  44. "Default header arguments to use when exporting Babel calls.")
  45. (defun org-babel-lob-ingest (&optional file)
  46. "Add all named source blocks defined in FILE to `org-babel-library-of-babel'."
  47. (interactive "fFile: ")
  48. (let ((lob-ingest-count 0))
  49. (org-babel-map-src-blocks file
  50. (let* ((info (let ((org-babel-default-header-args
  51. org-babel-default-lob-header-args))
  52. (org-babel-get-src-block-info 'light)))
  53. (source-name (nth 4 info)))
  54. (when source-name
  55. (setq source-name (intern source-name)
  56. org-babel-library-of-babel
  57. (cons (cons source-name info)
  58. (assq-delete-all source-name org-babel-library-of-babel))
  59. lob-ingest-count (1+ lob-ingest-count)))))
  60. (message "%d src block%s added to Library of Babel"
  61. lob-ingest-count (if (> lob-ingest-count 1) "s" ""))
  62. lob-ingest-count))
  63. ;; Functions for executing lob one-liners.
  64. ;;;###autoload
  65. (defun org-babel-lob-execute-maybe ()
  66. "Execute a Library of Babel source block, if appropriate.
  67. Detect if this is context for a Library Of Babel source block and
  68. if so then run the appropriate source block from the Library."
  69. (interactive)
  70. (let ((info (org-babel-lob-get-info)))
  71. (when info
  72. (org-babel-execute-src-block nil info)
  73. t)))
  74. (defun org-babel-lob--src-info (name)
  75. "Return internal representation for Babel data named NAME.
  76. NAME is a string. This function looks into the current document
  77. for a Babel call or source block. If none is found, it looks
  78. after NAME in the Library of Babel. Eventually, if that also
  79. fails, it Returns nil."
  80. ;; During export, look into the pristine copy of the document being
  81. ;; exported instead of the current one, which could miss some data.
  82. (with-current-buffer (or org-babel-exp-reference-buffer (current-buffer))
  83. (org-with-wide-buffer
  84. (goto-char (point-min))
  85. (catch :found
  86. (let ((case-fold-search t)
  87. (regexp (org-babel-named-data-regexp-for-name name)))
  88. (while (re-search-forward regexp nil t)
  89. (let ((element (org-element-at-point)))
  90. (when (equal name (org-element-property :name element))
  91. (throw :found
  92. (pcase (org-element-type element)
  93. (`src-block (let ((org-babel-default-header-args
  94. org-babel-default-lob-header-args))
  95. (org-babel-get-src-block-info t element)))
  96. (`babel-call (org-babel-lob-get-info element))
  97. ;; Non-executable data found. Since names are
  98. ;; supposed to be unique throughout a document,
  99. ;; bail out.
  100. (_ nil))))))
  101. ;; No element named NAME in buffer. Try Library of Babel.
  102. (cdr (assoc-string name 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. Return nil
  107. when not on an appropriate location. Otherwise return a list
  108. compatible with `org-babel-get-src-block-info', which see."
  109. (let* ((context (or datum (org-element-context)))
  110. (type (org-element-type context)))
  111. (when (memq type '(babel-call inline-babel-call))
  112. (pcase (org-babel-lob--src-info (org-element-property :call context))
  113. (`(,language ,body ,header ,_ ,_ ,_)
  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. (append
  123. (org-with-wide-buffer
  124. (goto-char 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. (_ nil)))))
  139. (provide 'ob-lob)
  140. ;; Local variables:
  141. ;; generated-autoload-file: "org-loaddefs.el"
  142. ;; End:
  143. ;;; ob-lob.el ends here