Merge branch 'maint'
[org-mode.git] / lisp / ob-lob.el
blob40033c852456224155f83fa0434ca704c3978b4c
1 ;;; ob-lob.el --- Functions Supporting the Library of Babel -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2009-2017 Free Software Foundation, Inc.
5 ;; Authors: Eric Schulte
6 ;; Dan Davison
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 <https://www.gnu.org/licenses/>.
25 ;;; Code:
26 (require 'cl-lib)
27 (require 'ob-core)
28 (require 'ob-table)
30 (declare-function org-babel-ref-split-args "ob-ref" (arg-string))
31 (declare-function org-element-at-point "org-element" ())
32 (declare-function org-element-context "org-element" (&optional element))
33 (declare-function org-element-property "org-element" (property element))
34 (declare-function org-element-type "org-element" (element))
36 (defvar org-babel-library-of-babel nil
37 "Library of source-code blocks.
38 This is an association list. Populate the library by calling
39 `org-babel-lob-ingest' on files containing source blocks.")
41 (defvar org-babel-default-lob-header-args '((:exports . "results"))
42 "Default header arguments to use when exporting Babel calls.
43 By default, a Babel call inherits its arguments from the source
44 block being called. Header arguments defined in this variable
45 take precedence over these. It is useful for properties that
46 should not be inherited from a source block.")
48 (defun org-babel-lob-ingest (&optional file)
49 "Add all named source blocks defined in FILE to `org-babel-library-of-babel'."
50 (interactive "fFile: ")
51 (let ((lob-ingest-count 0))
52 (org-babel-map-src-blocks file
53 (let* ((info (org-babel-get-src-block-info 'light))
54 (source-name (nth 4 info)))
55 (when source-name
56 (setf (nth 1 info)
57 (if (org-babel-noweb-p (nth 2 info) :eval)
58 (org-babel-expand-noweb-references info)
59 (nth 1 info)))
60 (let ((source (intern source-name)))
61 (setq org-babel-library-of-babel
62 (cons (cons source info)
63 (assq-delete-all source org-babel-library-of-babel))))
64 (cl-incf lob-ingest-count))))
65 (message "%d src block%s added to Library of Babel"
66 lob-ingest-count (if (> lob-ingest-count 1) "s" ""))
67 lob-ingest-count))
69 ;; Functions for executing lob one-liners.
71 ;;;###autoload
72 (defun org-babel-lob-execute-maybe ()
73 "Execute a Library of Babel source block, if appropriate.
74 Detect if this is context for a Library Of Babel source block and
75 if so then run the appropriate source block from the Library."
76 (interactive)
77 (let ((info (org-babel-lob-get-info)))
78 (when info
79 (org-babel-execute-src-block nil info)
80 t)))
82 (defun org-babel-lob--src-info (name)
83 "Return internal representation for Babel data named NAME.
84 NAME is a string. This function looks into the current document
85 for a Babel call or source block. If none is found, it looks
86 after NAME in the Library of Babel. Eventually, if that also
87 fails, it returns nil."
88 ;; During export, look into the pristine copy of the document being
89 ;; exported instead of the current one, which could miss some data.
90 (with-current-buffer (or org-babel-exp-reference-buffer (current-buffer))
91 (org-with-wide-buffer
92 (goto-char (point-min))
93 (catch :found
94 (let ((case-fold-search t)
95 (regexp (org-babel-named-data-regexp-for-name name)))
96 (while (re-search-forward regexp nil t)
97 (let ((element (org-element-at-point)))
98 (when (equal name (org-element-property :name element))
99 (throw :found
100 (pcase (org-element-type element)
101 (`src-block (org-babel-get-src-block-info t element))
102 (`babel-call (org-babel-lob-get-info element))
103 ;; Non-executable data found. Since names are
104 ;; supposed to be unique throughout a document,
105 ;; bail out.
106 (_ nil))))))
107 ;; No element named NAME in buffer. Try Library of Babel.
108 (cdr (assoc-string name org-babel-library-of-babel)))))))
110 ;;;###autoload
111 (defun org-babel-lob-get-info (&optional datum)
112 "Return internal representation for Library of Babel function call.
113 Consider DATUM, when provided, or element at point. Return nil
114 when not on an appropriate location. Otherwise return a list
115 compatible with `org-babel-get-src-block-info', which see."
116 (let* ((context (or datum (org-element-context)))
117 (type (org-element-type context)))
118 (when (memq type '(babel-call inline-babel-call))
119 (pcase (org-babel-lob--src-info (org-element-property :call context))
120 (`(,language ,body ,header ,_ ,_ ,_ ,coderef)
121 (let ((begin (org-element-property (if (eq type 'inline-babel-call)
122 :begin
123 :post-affiliated)
124 context)))
125 (list language
126 body
127 (apply #'org-babel-merge-params
128 header
129 org-babel-default-lob-header-args
130 (append
131 (org-with-point-at begin
132 (org-babel-params-from-properties language))
133 (list
134 (org-babel-parse-header-arguments
135 (org-element-property :inside-header context))
136 (let ((args (org-element-property :arguments context)))
137 (and args
138 (mapcar (lambda (ref) (cons :var ref))
139 (org-babel-ref-split-args args))))
140 (org-babel-parse-header-arguments
141 (org-element-property :end-header context)))))
143 (org-element-property :name context)
144 begin
145 coderef)))
146 (_ nil)))))
148 (provide 'ob-lob)
150 ;; Local variables:
151 ;; generated-autoload-file: "org-loaddefs.el"
152 ;; End:
154 ;;; ob-lob.el ends here