ob-lob.el 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. ;;; ob-lob.el --- functions supporting the Library of Babel
  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. (eval-when-compile
  20. (require 'cl))
  21. (require 'ob-core)
  22. (require 'ob-table)
  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 '((:exports . "results"))
  37. "Default header arguments to use when exporting #+lob/call lines.")
  38. (defun org-babel-lob-ingest (&optional file)
  39. "Add all named source blocks defined in FILE to `org-babel-library-of-babel'."
  40. (interactive "fFile: ")
  41. (let ((lob-ingest-count 0))
  42. (org-babel-map-src-blocks file
  43. (let* ((info (org-babel-get-src-block-info 'light))
  44. (source-name (nth 4 info)))
  45. (when source-name
  46. (setq source-name (intern source-name)
  47. org-babel-library-of-babel
  48. (cons (cons source-name info)
  49. (assq-delete-all source-name org-babel-library-of-babel))
  50. lob-ingest-count (1+ lob-ingest-count)))))
  51. (message "%d src block%s added to Library of Babel"
  52. lob-ingest-count (if (> lob-ingest-count 1) "s" ""))
  53. lob-ingest-count))
  54. ;; Functions for executing lob one-liners.
  55. ;;;###autoload
  56. (defun org-babel-lob-execute-maybe ()
  57. "Execute a Library of Babel source block, if appropriate.
  58. Detect if this is context for a Library Of Babel source block and
  59. if so then run the appropriate source block from the Library."
  60. (interactive)
  61. (let ((info (org-babel-lob-get-info)))
  62. (when info
  63. (org-babel-lob-execute info)
  64. t)))
  65. ;;;###autoload
  66. (defun org-babel-lob-get-info (&optional datum)
  67. "Return a Library of Babel function call as a string.
  68. Return nil when not on an appropriate location. Build string
  69. from `inline-babel-call' or `babel-call' DATUM, when provided."
  70. (let ((context (or datum (org-element-context))))
  71. (when (memq (org-element-type context) '(babel-call inline-babel-call))
  72. (list (format "%s%s(%s)"
  73. (org-element-property :call context)
  74. (let ((in (org-element-property :inside-header context)))
  75. (if in (format "[%s]" in) ""))
  76. (or (org-element-property :arguments context) ""))
  77. (org-element-property :end-header context)
  78. (org-element-property :name context)))))
  79. (defvar org-babel-default-header-args:emacs-lisp) ; Defined in ob-emacs-lisp.el
  80. (defun org-babel-lob-execute (info)
  81. "Execute the lob call specified by INFO."
  82. (let* ((mkinfo (lambda (p)
  83. ;; Make plist P compatible with
  84. ;; `org-babel-get-src-block-info'.
  85. (list "emacs-lisp" "results" p nil (nth 2 info))))
  86. (pre-params
  87. (apply #'org-babel-merge-params
  88. org-babel-default-header-args
  89. org-babel-default-header-args:emacs-lisp
  90. (append
  91. (org-babel-params-from-properties)
  92. (list
  93. (org-babel-parse-header-arguments
  94. (org-no-properties
  95. (concat ":var results="
  96. (mapconcat #'identity (butlast info) " "))))))))
  97. (pre-info (funcall mkinfo pre-params))
  98. (cache-p (and (cdr (assoc :cache pre-params))
  99. (string= "yes" (cdr (assoc :cache pre-params)))))
  100. (new-hash (when cache-p
  101. (org-babel-sha1-hash
  102. ;; Do *not* pre-process params for call line
  103. ;; hash evaluation, since for a call line :var
  104. ;; extension *is* execution.
  105. (let* ((params (nth 2 pre-info))
  106. (sha1-nth2 (list
  107. (cons
  108. (cons :c-var (cdr (assoc :var params)))
  109. (assq-delete-all :var (copy-tree params)))))
  110. (sha1-info (copy-tree pre-info)))
  111. (prog1 sha1-info
  112. (setcar (cddr sha1-info) sha1-nth2))))))
  113. (old-hash (when cache-p (org-babel-current-result-hash pre-info)))
  114. (org-babel-current-src-block-location (point-marker)))
  115. (if (and cache-p (equal new-hash old-hash))
  116. (save-excursion (goto-char (org-babel-where-is-src-block-result
  117. nil pre-info))
  118. (forward-line 1)
  119. (message "%S" (org-babel-read-result)))
  120. (prog1 (let* ((proc-params (org-babel-process-params pre-params))
  121. org-confirm-babel-evaluate)
  122. (org-babel-execute-src-block nil (funcall mkinfo proc-params)))
  123. ;; update the hash
  124. (when new-hash
  125. (org-babel-set-current-result-hash new-hash pre-info))))))
  126. (provide 'ob-lob)
  127. ;; Local variables:
  128. ;; generated-autoload-file: "org-loaddefs.el"
  129. ;; End:
  130. ;;; ob-lob.el ends here