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