Avoid the use of ((lambda ...) ...) in lexical-binding code.
[bpt/emacs.git] / lisp / org / ob-lob.el
CommitLineData
86fbb8ca
CD
1;;; ob-lob.el --- functions supporting the Library of Babel
2
b73f1974 3;; Copyright (C) 2009-2012 Free Software Foundation, Inc.
86fbb8ca 4
dfd98937
BG
5;; Authors: Eric Schulte
6;; Dan Davison
86fbb8ca
CD
7;; Keywords: literate programming, reproducible research
8;; Homepage: http://orgmode.org
86fbb8ca
CD
9
10;; This file is part of GNU Emacs.
11
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.
16
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.
21
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/>.
24
86fbb8ca 25;;; Code:
3ab2c837
BG
26(eval-when-compile
27 (require 'cl))
86fbb8ca
CD
28(require 'ob)
29(require 'ob-table)
30
e66ba1df
BG
31(declare-function org-babel-in-example-or-verbatim "ob-exp" nil)
32
86fbb8ca
CD
33(defvar org-babel-library-of-babel nil
34 "Library of source-code blocks.
35This is an association list. Populate the library by adding
36files to `org-babel-lob-files'.")
37
38(defcustom org-babel-lob-files '()
39 "Files used to populate the `org-babel-library-of-babel'.
40To add files to this list use the `org-babel-lob-ingest' command."
41 :group 'org-babel
372d7b21 42 :version "24.1"
86fbb8ca
CD
43 :type 'list)
44
3ab2c837
BG
45(defvar org-babel-default-lob-header-args '((:exports . "results"))
46 "Default header arguments to use when exporting #+lob/call lines.")
47
86fbb8ca
CD
48;;;###autoload
49(defun org-babel-lob-ingest (&optional file)
afe98dfa
CD
50 "Add all named source-blocks defined in FILE to
51`org-babel-library-of-babel'."
3ab2c837 52 (interactive "fFile: ")
afe98dfa
CD
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)))
57 (when source-name
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" ""))
65 lob-ingest-count))
86fbb8ca 66
3ab2c837 67(defconst org-babel-block-lob-one-liner-regexp
afe98dfa 68 (concat
153ae947
BG
69 "^\\([ \t]*?\\)#\\+call:[ \t]+\\([^\(\)\n]+?\\)\\(\\[\\(.*\\)\\]\\|\\(\\)\\)"
70 "\(\\([^\n]*?\\)\)\\(\\[.+\\]\\|\\)[ \t]*\\(\\([^\n]*\\)\\)?")
3ab2c837
BG
71 "Regexp to match non-inline calls to predefined source block functions.")
72
73(defconst org-babel-inline-lob-one-liner-regexp
74 (concat
153ae947
BG
75 "\\([^\n]*?\\)call_\\([^\(\)\n]+?\\)\\(\\[\\(.*?\\)\\]\\|\\(\\)\\)"
76 "\(\\([^\n]*?\\)\)\\(\\[\\(.*?\\)\\]\\)?")
3ab2c837
BG
77 "Regexp to match inline calls to predefined source block functions.")
78
79(defconst org-babel-lob-one-liner-regexp
80 (concat "\\(" org-babel-block-lob-one-liner-regexp
81 "\\|" org-babel-inline-lob-one-liner-regexp "\\)")
86fbb8ca
CD
82 "Regexp to match calls to predefined source block functions.")
83
84;; functions for executing lob one-liners
e66ba1df 85
86fbb8ca
CD
86;;;###autoload
87(defun org-babel-lob-execute-maybe ()
88 "Execute a Library of Babel source block, if appropriate.
89Detect if this is context for a Library Of Babel source block and
90if so then run the appropriate source block from the Library."
91 (interactive)
92 (let ((info (org-babel-lob-get-info)))
e66ba1df
BG
93 (if (and (nth 0 info) (not (org-babel-in-example-or-verbatim)))
94 (progn (org-babel-lob-execute info) t)
95 nil)))
86fbb8ca 96
86fbb8ca
CD
97;;;###autoload
98(defun org-babel-lob-get-info ()
afe98dfa 99 "Return a Library of Babel function call as a string."
3ab2c837
BG
100 (flet ((nonempty (a b)
101 (let ((it (match-string a)))
102 (if (= (length it) 0) (match-string b) it))))
103 (let ((case-fold-search t))
104 (save-excursion
105 (beginning-of-line 1)
106 (when (looking-at org-babel-lob-one-liner-regexp)
107 (append
14e1337f 108 (mapcar #'org-babel-clean-text-properties
afe98dfa 109 (list
acedf35c 110 (format "%s%s(%s)%s"
3ab2c837 111 (nonempty 3 12)
153ae947
BG
112 (if (not (= 0 (length (nonempty 5 14))))
113 (concat "[" (nonempty 5 14) "]") "")
3ab2c837
BG
114 (or (nonempty 7 16) "")
115 (or (nonempty 8 19) ""))
116 (nonempty 9 18)))
117 (list (length (if (= (length (match-string 12)) 0)
118 (match-string 2) (match-string 11))))))))))
14e1337f 119
86fbb8ca
CD
120(defun org-babel-lob-execute (info)
121 "Execute the lob call specified by INFO."
afe98dfa
CD
122 (let ((params (org-babel-process-params
123 (org-babel-merge-params
124 org-babel-default-header-args
afe98dfa
CD
125 (org-babel-params-from-properties)
126 (org-babel-parse-header-arguments
127 (org-babel-clean-text-properties
128 (concat ":var results="
129 (mapconcat #'identity (butlast info) " "))))))))
86fbb8ca
CD
130 (org-babel-execute-src-block
131 nil (list "emacs-lisp" "results" params nil nil (nth 2 info)))))
132
133(provide 'ob-lob)
134
5b409b39 135
86fbb8ca
CD
136
137;;; ob-lob.el ends here