ob-lob.el 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. ;;; ob-lob.el --- functions supporting the Library of Babel
  2. ;; Copyright (C) 2009-2015 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-babel-in-example-or-verbatim "ob-exp" nil)
  24. (defvar org-babel-library-of-babel nil
  25. "Library of source-code blocks.
  26. This is an association list. Populate the library by adding
  27. files to `org-babel-lob-files'.")
  28. (defcustom org-babel-lob-files nil
  29. "Files used to populate the `org-babel-library-of-babel'.
  30. To add files to this list use the `org-babel-lob-ingest' command."
  31. :group 'org-babel
  32. :version "24.1"
  33. :type '(repeat file))
  34. (defvar org-babel-default-lob-header-args '((:exports . "results"))
  35. "Default header arguments to use when exporting #+lob/call lines.")
  36. (defun org-babel-lob-ingest (&optional file)
  37. "Add all named source blocks defined in FILE to `org-babel-library-of-babel'."
  38. (interactive "fFile: ")
  39. (let ((lob-ingest-count 0))
  40. (org-babel-map-src-blocks file
  41. (let* ((info (org-babel-get-src-block-info 'light))
  42. (source-name (nth 4 info)))
  43. (when source-name
  44. (setq source-name (intern source-name)
  45. org-babel-library-of-babel
  46. (cons (cons source-name info)
  47. (assq-delete-all source-name org-babel-library-of-babel))
  48. lob-ingest-count (1+ lob-ingest-count)))))
  49. (message "%d src block%s added to Library of Babel"
  50. lob-ingest-count (if (> lob-ingest-count 1) "s" ""))
  51. lob-ingest-count))
  52. (defconst org-babel-block-lob-one-liner-regexp
  53. (concat
  54. "^\\([ \t]*?\\)#\\+call:[ \t]+\\([^()\n]+?\\)\\(\\[\\(.*\\)\\]\\|\\(\\)\\)"
  55. "(\\([^\n]*?\\))\\(\\[.+\\]\\|\\)[ \t]*\\(\\([^\n]*\\)\\)?")
  56. "Regexp to match non-inline calls to predefined source block functions.")
  57. (defconst org-babel-inline-lob-one-liner-regexp
  58. (concat
  59. "\\([^\n]*?\\)call_\\([^()[:space:]\n]+?\\)\\(\\[\\(.*?\\)\\]\\|\\(\\)\\)"
  60. "(\\(.*?\\))\\(\\[\\(.*?\\)\\]\\)?")
  61. "Regexp to match inline calls to predefined source block functions.")
  62. (defconst org-babel-lob-one-liner-regexp
  63. (concat "\\(" org-babel-block-lob-one-liner-regexp
  64. "\\|" org-babel-inline-lob-one-liner-regexp "\\)")
  65. "Regexp to match calls to predefined source block functions.")
  66. ;; functions for executing lob one-liners
  67. ;;;###autoload
  68. (defun org-babel-lob-execute-maybe ()
  69. "Execute a Library of Babel source block, if appropriate.
  70. Detect if this is context for a Library Of Babel source block and
  71. if so then run the appropriate source block from the Library."
  72. (interactive)
  73. (let ((info (org-babel-lob-get-info)))
  74. (if (and (nth 0 info) (not (org-babel-in-example-or-verbatim)))
  75. (progn (org-babel-lob-execute info) t)
  76. nil)))
  77. ;;;###autoload
  78. (defun org-babel-lob-get-info ()
  79. "Return a Library of Babel function call as a string."
  80. (let ((case-fold-search t)
  81. (nonempty (lambda (a b)
  82. (let ((it (match-string a)))
  83. (if (= (length it) 0) (match-string b) it)))))
  84. (save-excursion
  85. (beginning-of-line 1)
  86. (when (looking-at org-babel-lob-one-liner-regexp)
  87. (append
  88. (mapcar #'org-no-properties
  89. (list
  90. (format "%s%s(%s)%s"
  91. (funcall nonempty 3 12)
  92. (if (not (= 0 (length (funcall nonempty 5 14))))
  93. (concat "[" (funcall nonempty 5 14) "]") "")
  94. (or (funcall nonempty 7 16) "")
  95. (or (funcall nonempty 8 19) ""))
  96. (funcall nonempty 9 18)))
  97. (list (length (if (= (length (match-string 12)) 0)
  98. (match-string 2) (match-string 11)))
  99. (save-excursion
  100. (forward-line -1)
  101. (save-match-data
  102. (and (looking-at (concat org-babel-src-name-regexp
  103. "\\([^\n]*\\)$"))
  104. (org-no-properties (match-string 1)))))))))))
  105. (defvar org-babel-default-header-args:emacs-lisp) ; Defined in ob-emacs-lisp.el
  106. (defun org-babel-lob-execute (info)
  107. "Execute the lob call specified by INFO."
  108. (let* ((mkinfo (lambda (p)
  109. (list "emacs-lisp" "results" p nil
  110. (nth 3 info) ;; name
  111. (nth 2 info))))
  112. (pre-params (apply #'org-babel-merge-params
  113. org-babel-default-header-args
  114. org-babel-default-header-args:emacs-lisp
  115. (append
  116. (org-babel-params-from-properties)
  117. (list
  118. (org-babel-parse-header-arguments
  119. (org-no-properties
  120. (concat
  121. ":var results="
  122. (mapconcat #'identity (butlast info 2)
  123. " "))))))))
  124. (pre-info (funcall mkinfo pre-params))
  125. (cache-p (and (cdr (assoc :cache pre-params))
  126. (string= "yes" (cdr (assoc :cache pre-params)))))
  127. (new-hash (when cache-p
  128. (org-babel-sha1-hash
  129. ;; Do *not* pre-process params for call line
  130. ;; hash evaluation, since for a call line :var
  131. ;; extension *is* execution.
  132. (let* ((params (nth 2 pre-info))
  133. (sha1-nth2 (list
  134. (cons
  135. (cons :c-var (cdr (assoc :var params)))
  136. (assq-delete-all :var (copy-tree params)))))
  137. (sha1-info (copy-tree pre-info)))
  138. (prog1 sha1-info
  139. (setcar (cddr sha1-info) sha1-nth2))))))
  140. (old-hash (when cache-p (org-babel-current-result-hash pre-info)))
  141. (org-babel-current-src-block-location (point-marker)))
  142. (if (and cache-p (equal new-hash old-hash))
  143. (save-excursion (goto-char (org-babel-where-is-src-block-result
  144. nil pre-info))
  145. (forward-line 1)
  146. (message "%S" (org-babel-read-result)))
  147. (prog1 (let* ((proc-params (org-babel-process-params pre-params))
  148. org-confirm-babel-evaluate)
  149. (org-babel-execute-src-block nil (funcall mkinfo proc-params)))
  150. ;; update the hash
  151. (when new-hash
  152. (org-babel-set-current-result-hash new-hash pre-info))))))
  153. (provide 'ob-lob)
  154. ;; Local variables:
  155. ;; generated-autoload-file: "org-loaddefs.el"
  156. ;; End:
  157. ;;; ob-lob.el ends here