ob-lob.el 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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-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 adding
  30. files to `org-babel-lob-files'.")
  31. (defcustom org-babel-lob-files nil
  32. "Files used to populate the `org-babel-library-of-babel'.
  33. To add files to this list use the `org-babel-lob-ingest' command."
  34. :group 'org-babel
  35. :version "24.1"
  36. :type '(repeat file))
  37. (defvar org-babel-default-lob-header-args
  38. '((:cache . "no")
  39. (:exports . "results")
  40. (:hlines . "no")
  41. (:noweb . "no")
  42. (:results . "replace")
  43. (:session . "none")
  44. (:tangle . "no"))
  45. "Default header arguments to use when exporting Babel calls.")
  46. (defun org-babel-lob-ingest (&optional file)
  47. "Add all named source blocks defined in FILE to `org-babel-library-of-babel'."
  48. (interactive "fFile: ")
  49. (let ((lob-ingest-count 0))
  50. (org-babel-map-src-blocks file
  51. (let* ((info (let ((org-babel-default-header-args
  52. org-babel-default-lob-header-args))
  53. (org-babel-get-src-block-info 'light)))
  54. (source-name (nth 4 info)))
  55. (when source-name
  56. (setq source-name (intern source-name)
  57. org-babel-library-of-babel
  58. (cons (cons source-name info)
  59. (assq-delete-all source-name org-babel-library-of-babel))
  60. lob-ingest-count (1+ lob-ingest-count)))))
  61. (message "%d src block%s added to Library of Babel"
  62. lob-ingest-count (if (> lob-ingest-count 1) "s" ""))
  63. lob-ingest-count))
  64. ;; Functions for executing lob one-liners.
  65. ;;;###autoload
  66. (defun org-babel-lob-execute-maybe ()
  67. "Execute a Library of Babel source block, if appropriate.
  68. Detect if this is context for a Library Of Babel source block and
  69. if so then run the appropriate source block from the Library."
  70. (interactive)
  71. (let ((info (org-babel-lob-get-info)))
  72. (when info
  73. (org-babel-execute-src-block nil info)
  74. t)))
  75. (defun org-babel-lob--src-info (name)
  76. "Return internal representation for Babel data named NAME.
  77. NAME is a string. This function looks into the current document
  78. for a Babel call or source block. If none is found, it looks
  79. after NAME in the Library of Babel. Eventually, if that also
  80. fails, it Returns nil."
  81. ;; During export, look into the pristine copy of the document being
  82. ;; exported instead of the current one, which could miss some data.
  83. (with-current-buffer (or org-babel-exp-reference-buffer (current-buffer))
  84. (org-with-wide-buffer
  85. (goto-char (point-min))
  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 (let ((org-babel-default-header-args
  95. org-babel-default-lob-header-args))
  96. (org-babel-get-src-block-info t element)))
  97. (`babel-call (org-babel-lob-get-info element))
  98. ;; Non-executable data found. Since names are
  99. ;; supposed to be unique throughout a document,
  100. ;; bail out.
  101. (_ nil))))))
  102. ;; No element named NAME in buffer. Try Library of Babel.
  103. (cdr (assoc-string name org-babel-library-of-babel)))))))
  104. ;;;###autoload
  105. (defun org-babel-lob-get-info (&optional datum)
  106. "Return internal representation for Library of Babel function call.
  107. Consider DATUM, when provided, or element at point. Return nil
  108. when not on an appropriate location. Otherwise return a list
  109. compatible with `org-babel-get-src-block-info', which see."
  110. (let* ((context (or datum (org-element-context)))
  111. (type (org-element-type context)))
  112. (when (memq type '(babel-call inline-babel-call))
  113. (pcase (org-babel-lob--src-info (org-element-property :call context))
  114. (`(,language ,body ,header ,_ ,_ ,_)
  115. (let ((begin (org-element-property (if (eq type 'inline-babel-call)
  116. :begin
  117. :post-affiliated)
  118. context)))
  119. (list language
  120. body
  121. (apply #'org-babel-merge-params
  122. header
  123. (append
  124. (org-with-wide-buffer
  125. (goto-char begin)
  126. (org-babel-params-from-properties language))
  127. (list
  128. (org-babel-parse-header-arguments
  129. (org-element-property :inside-header context))
  130. (let ((args (org-element-property :arguments context)))
  131. (and args
  132. (mapcar (lambda (ref) (cons :var ref))
  133. (org-babel-ref-split-args args))))
  134. (org-babel-parse-header-arguments
  135. (org-element-property :end-header context)))))
  136. nil
  137. (org-element-property :name context)
  138. begin)))
  139. (_ nil)))))
  140. (provide 'ob-lob)
  141. ;; Local variables:
  142. ;; generated-autoload-file: "org-loaddefs.el"
  143. ;; End:
  144. ;;; ob-lob.el ends here