ob-lob.el 5.7 KB

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