* ob-vbnet.el: Org-babel functions for VB.Net evaluation
[org-mode/org-tableheadings.git] / lisp / ob-lob.el
blobd1f6172e00435b419d119caa554f9c458eb771f3
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
42 '((:cache . "no")
43 (:exports . "results")
44 (:hlines . "no")
45 (:noweb . "no")
46 (:results . "replace")
47 (:session . "none")
48 (:tangle . "no"))
49 "Default header arguments to use when exporting Babel calls.")
51 (defun org-babel-lob-ingest (&optional file)
52 "Add all named source blocks defined in FILE to `org-babel-library-of-babel'."
53 (interactive "fFile: ")
54 (let ((lob-ingest-count 0))
55 (org-babel-map-src-blocks file
56 (let* ((info (let ((org-babel-default-header-args
57 org-babel-default-lob-header-args))
58 (org-babel-get-src-block-info 'light)))
59 (source-name (nth 4 info)))
60 (when source-name
61 (setq source-name (intern source-name)
62 org-babel-library-of-babel
63 (cons (cons source-name info)
64 (assq-delete-all source-name org-babel-library-of-babel))
65 lob-ingest-count (1+ lob-ingest-count)))))
66 (message "%d src block%s added to Library of Babel"
67 lob-ingest-count (if (> lob-ingest-count 1) "s" ""))
68 lob-ingest-count))
70 ;; Functions for executing lob one-liners.
72 ;;;###autoload
73 (defun org-babel-lob-execute-maybe ()
74 "Execute a Library of Babel source block, if appropriate.
75 Detect if this is context for a Library Of Babel source block and
76 if so then run the appropriate source block from the Library."
77 (interactive)
78 (let ((info (org-babel-lob-get-info)))
79 (when info
80 (org-babel-execute-src-block nil info)
81 t)))
83 (defun org-babel-lob--src-info (name)
84 "Return internal representation for Babel data named NAME.
85 NAME is a string. This function looks into the current document
86 for a Babel call or source block. If none is found, it looks
87 after NAME in the Library of Babel. Eventually, if that also
88 fails, it returns nil."
89 ;; During export, look into the pristine copy of the document being
90 ;; exported instead of the current one, which could miss some data.
91 (with-current-buffer (or org-babel-exp-reference-buffer (current-buffer))
92 (org-with-wide-buffer
93 (goto-char (point-min))
94 (catch :found
95 (let ((case-fold-search t)
96 (regexp (org-babel-named-data-regexp-for-name name)))
97 (while (re-search-forward regexp nil t)
98 (let ((element (org-element-at-point)))
99 (when (equal name (org-element-property :name element))
100 (throw :found
101 (pcase (org-element-type element)
102 (`src-block (let ((org-babel-default-header-args
103 org-babel-default-lob-header-args))
104 (org-babel-get-src-block-info t element)))
105 (`babel-call (org-babel-lob-get-info element))
106 ;; Non-executable data found. Since names are
107 ;; supposed to be unique throughout a document,
108 ;; bail out.
109 (_ nil))))))
110 ;; No element named NAME in buffer. Try Library of Babel.
111 (cdr (assoc-string name org-babel-library-of-babel)))))))
113 ;;;###autoload
114 (defun org-babel-lob-get-info (&optional datum)
115 "Return internal representation for Library of Babel function call.
116 Consider DATUM, when provided, or element at point. Return nil
117 when not on an appropriate location. Otherwise return a list
118 compatible with `org-babel-get-src-block-info', which see."
119 (let* ((context (or datum (org-element-context)))
120 (type (org-element-type context)))
121 (when (memq type '(babel-call inline-babel-call))
122 (pcase (org-babel-lob--src-info (org-element-property :call context))
123 (`(,language ,body ,header ,_ ,_ ,_)
124 (let ((begin (org-element-property (if (eq type 'inline-babel-call)
125 :begin
126 :post-affiliated)
127 context)))
128 (list language
129 body
130 (apply #'org-babel-merge-params
131 header
132 (append
133 (org-with-wide-buffer
134 (goto-char begin)
135 (org-babel-params-from-properties language))
136 (list
137 (org-babel-parse-header-arguments
138 (org-element-property :inside-header context))
139 (let ((args (org-element-property :arguments context)))
140 (and args
141 (mapcar (lambda (ref) (cons :var ref))
142 (org-babel-ref-split-args args))))
143 (org-babel-parse-header-arguments
144 (org-element-property :end-header context)))))
146 (org-element-property :name context)
147 begin)))
148 (_ nil)))))
150 (provide 'ob-lob)
152 ;; Local variables:
153 ;; generated-autoload-file: "org-loaddefs.el"
154 ;; End:
156 ;;; ob-lob.el ends here