1 ;;; ob-lob.el --- functions supporting the Library of Babel
3 ;; Copyright (C) 2009-2016 Free Software Foundation, Inc.
5 ;; Authors: Eric Schulte
7 ;; Keywords: literate programming, reproducible research
8 ;; Homepage: http://orgmode.org
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
31 (declare-function org-element-context
"org-element" (&optional element
))
32 (declare-function org-element-property
"org-element" (property element
))
33 (declare-function org-element-type
"org-element" (element))
35 (defvar org-babel-library-of-babel nil
36 "Library of source-code blocks.
37 This is an association list. Populate the library by adding
38 files to `org-babel-lob-files'.")
40 (defcustom org-babel-lob-files nil
41 "Files used to populate the `org-babel-library-of-babel'.
42 To add files to this list use the `org-babel-lob-ingest' command."
47 (defvar org-babel-default-lob-header-args
'((:exports .
"results"))
48 "Default header arguments to use when exporting #+lob/call lines.")
50 (defun org-babel-lob-ingest (&optional file
)
51 "Add all named source blocks defined in FILE to `org-babel-library-of-babel'."
52 (interactive "fFile: ")
53 (let ((lob-ingest-count 0))
54 (org-babel-map-src-blocks file
55 (let* ((info (org-babel-get-src-block-info 'light
))
56 (source-name (nth 4 info
)))
58 (setq source-name
(intern source-name
)
59 org-babel-library-of-babel
60 (cons (cons source-name info
)
61 (assq-delete-all source-name org-babel-library-of-babel
))
62 lob-ingest-count
(1+ lob-ingest-count
)))))
63 (message "%d src block%s added to Library of Babel"
64 lob-ingest-count
(if (> lob-ingest-count
1) "s" ""))
67 ;; Functions for executing lob one-liners.
70 (defun org-babel-lob-execute-maybe ()
71 "Execute a Library of Babel source block, if appropriate.
72 Detect if this is context for a Library Of Babel source block and
73 if so then run the appropriate source block from the Library."
75 (let ((info (org-babel-lob-get-info)))
77 (org-babel-lob-execute info
)
81 (defun org-babel-lob-get-info (&optional datum
)
82 "Return a Library of Babel function call as a string.
83 Return nil when not on an appropriate location. Build string
84 from `inline-babel-call' or `babel-call' DATUM, when provided."
85 (let* ((context (or datum
(org-element-context)))
86 (type (org-element-type context
)))
87 (when (memq type
'(babel-call inline-babel-call
))
88 (list (format "%s%s(%s)"
89 (org-element-property :call context
)
90 (let ((in (org-element-property :inside-header context
)))
91 (if in
(format "[%s]" in
) ""))
92 (or (org-element-property :arguments context
) ""))
93 (org-element-property :end-header context
)
94 (org-element-property :name context
)
96 (if (eq type
'babel-call
) :post-affiliated
:begin
)
99 (defvar org-babel-default-header-args
:emacs-lisp
) ; Defined in ob-emacs-lisp.el
100 (defun org-babel-lob-execute (info)
101 "Execute the lob call specified by INFO."
102 (let* ((mkinfo (lambda (p)
103 ;; Make plist P compatible with
104 ;; `org-babel-get-src-block-info'.
106 "emacs-lisp" "results" p nil
(nth 2 info
) (nth 3 info
))))
108 (apply #'org-babel-merge-params
109 org-babel-default-header-args
110 org-babel-default-header-args
:emacs-lisp
112 (org-babel-params-from-properties)
114 (org-babel-parse-header-arguments
116 (concat ":var results="
117 (mapconcat #'identity
(butlast info
2) " "))))))))
118 (pre-info (funcall mkinfo pre-params
))
119 (cache-p (and (cdr (assoc :cache pre-params
))
120 (string= "yes" (cdr (assoc :cache pre-params
)))))
121 (new-hash (when cache-p
123 ;; Do *not* pre-process params for call line
124 ;; hash evaluation, since for a call line :var
125 ;; extension *is* execution.
126 (let* ((params (nth 2 pre-info
))
129 (cons :c-var
(cdr (assoc :var params
)))
130 (assq-delete-all :var
(copy-tree params
)))))
131 (sha1-info (copy-tree pre-info
)))
133 (setcar (cddr sha1-info
) sha1-nth2
))))))
134 (old-hash (when cache-p
(org-babel-current-result-hash pre-info
)))
135 (org-babel-current-src-block-location (point-marker)))
136 (if (and cache-p
(equal new-hash old-hash
))
137 (save-excursion (goto-char (org-babel-where-is-src-block-result
140 (message "%S" (org-babel-read-result)))
141 (prog1 (let* ((proc-params (org-babel-process-params pre-params
))
142 org-confirm-babel-evaluate
)
143 (org-babel-execute-src-block nil
(funcall mkinfo proc-params
)))
146 (org-babel-set-current-result-hash new-hash pre-info
))))))
151 ;; generated-autoload-file: "org-loaddefs.el"
154 ;;; ob-lob.el ends here