Use ‘eq’ instead of ‘equal’ for symbols
[org-mode.git] / lisp / ob-lob.el
blob1c4123bd1a99bd7ebe5af92eb163d5d79ee38f6c
1 ;;; ob-lob.el --- Functions Supporting the Library of Babel -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2009-2016 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 <http://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 (setq source-name (intern source-name)
57 org-babel-library-of-babel
58 (cons (cons source-name info)
59 (assq-delete-all source-name org-babel-library-of-babel))
60 lob-ingest-count (1+ lob-ingest-count)))))
61 (message "%d src block%s added to Library of Babel"
62 lob-ingest-count (if (> lob-ingest-count 1) "s" ""))
63 lob-ingest-count))
65 ;; 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 (when info
75 (org-babel-execute-src-block nil info)
76 t)))
78 (defun org-babel-lob--src-info (name)
79 "Return internal representation for Babel data named NAME.
80 NAME is a string. This function looks into the current document
81 for a Babel call or source block. If none is found, it looks
82 after NAME in the Library of Babel. Eventually, if that also
83 fails, it returns nil."
84 ;; During export, look into the pristine copy of the document being
85 ;; exported instead of the current one, which could miss some data.
86 (with-current-buffer (or org-babel-exp-reference-buffer (current-buffer))
87 (org-with-wide-buffer
88 (goto-char (point-min))
89 (catch :found
90 (let ((case-fold-search t)
91 (regexp (org-babel-named-data-regexp-for-name name)))
92 (while (re-search-forward regexp nil t)
93 (let ((element (org-element-at-point)))
94 (when (equal name (org-element-property :name element))
95 (throw :found
96 (pcase (org-element-type element)
97 (`src-block (org-babel-get-src-block-info t element))
98 (`babel-call (org-babel-lob-get-info element))
99 ;; Non-executable data found. Since names are
100 ;; supposed to be unique throughout a document,
101 ;; bail out.
102 (_ nil))))))
103 ;; No element named NAME in buffer. Try Library of Babel.
104 (cdr (assoc-string name org-babel-library-of-babel)))))))
106 ;;;###autoload
107 (defun org-babel-lob-get-info (&optional datum)
108 "Return internal representation for Library of Babel function call.
109 Consider DATUM, when provided, or element at point. Return nil
110 when not on an appropriate location. Otherwise return a list
111 compatible with `org-babel-get-src-block-info', which see."
112 (let* ((context (or datum (org-element-context)))
113 (type (org-element-type context)))
114 (when (memq type '(babel-call inline-babel-call))
115 (pcase (org-babel-lob--src-info (org-element-property :call context))
116 (`(,language ,body ,header ,_ ,_ ,_ ,coderef)
117 (let ((begin (org-element-property (if (eq type 'inline-babel-call)
118 :begin
119 :post-affiliated)
120 context)))
121 (list language
122 body
123 (apply #'org-babel-merge-params
124 header
125 org-babel-default-lob-header-args
126 (append
127 (org-with-wide-buffer
128 (goto-char begin)
129 (org-babel-params-from-properties language))
130 (list
131 (org-babel-parse-header-arguments
132 (org-element-property :inside-header context))
133 (let ((args (org-element-property :arguments context)))
134 (and args
135 (mapcar (lambda (ref) (cons :var ref))
136 (org-babel-ref-split-args args))))
137 (org-babel-parse-header-arguments
138 (org-element-property :end-header context)))))
140 (org-element-property :name context)
141 begin
142 coderef)))
143 (_ nil)))))
145 (provide 'ob-lob)
147 ;; Local variables:
148 ;; generated-autoload-file: "org-loaddefs.el"
149 ;; End:
151 ;;; ob-lob.el ends here