tests: more thorough testing of inline call lines -- passing all tests
[org-mode/org-jambu.git] / lisp / ob-lob.el
blob2fa2524f03f41807524720101c2713bc0a9816a4
1 ;;; ob-lob.el --- functions supporting the Library of Babel
3 ;; Copyright (C) 2009, 2010 Free Software Foundation, Inc.
5 ;; Author: Eric Schulte, Dan Davison
6 ;; Keywords: literate programming, reproducible research
7 ;; Homepage: http://orgmode.org
8 ;; Version: 7.5
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 'ob)
27 (require 'ob-table)
29 (defvar org-babel-library-of-babel nil
30 "Library of source-code blocks.
31 This is an association list. Populate the library by adding
32 files to `org-babel-lob-files'.")
34 (defcustom org-babel-lob-files '()
35 "Files used to populate the `org-babel-library-of-babel'.
36 To add files to this list use the `org-babel-lob-ingest' command."
37 :group 'org-babel
38 :type 'list)
40 (defvar org-babel-default-lob-header-args '((:exports . "results"))
41 "Default header arguments to use when exporting #+lob/call lines.")
43 ;;;###autoload
44 (defun org-babel-lob-ingest (&optional file)
45 "Add all named source-blocks defined in FILE to
46 `org-babel-library-of-babel'."
47 (interactive "fFile: ")
48 (let ((lob-ingest-count 0))
49 (org-babel-map-src-blocks file
50 (let* ((info (org-babel-get-src-block-info 'light))
51 (source-name (nth 4 info)))
52 (when source-name
53 (setq source-name (intern source-name)
54 org-babel-library-of-babel
55 (cons (cons source-name info)
56 (assq-delete-all source-name org-babel-library-of-babel))
57 lob-ingest-count (1+ lob-ingest-count)))))
58 (message "%d src block%s added to Library of Babel"
59 lob-ingest-count (if (> lob-ingest-count 1) "s" ""))
60 lob-ingest-count))
62 (defconst org-babel-lob-call-aliases '("lob" "call")
63 "Aliases to call a source block function.
64 If you change the value of this variable then your files may
65 become unusable by other org-babel users, and vice versa.")
67 (defconst org-babel-block-lob-one-liner-regexp
68 (concat
69 "^\\([ \t]*\\)#\\+\\(?:"
70 (mapconcat #'regexp-quote org-babel-lob-call-aliases "\\|")
71 "\\):[ \t]+\\([^\(\)\n]+?\\)\\(\\[\\(.*\\)\\]\\|\\(\\)\\)"
72 "\(\\([^\n]*\\)\)\\(\\[.+\\]\\|\\)[ \t]*\\(\\([^\n]*\\)\\)?")
73 "Regexp to match non-inline calls to predefined source block functions.")
75 (defconst org-babel-inline-lob-one-liner-regexp
76 (concat
77 "\\([^\n]*\\)\\(?:"
78 (mapconcat #'regexp-quote org-babel-lob-call-aliases "\\|")
79 "\\)_\\([^\(\)\n]+?\\)\\(\\[\\(.*\\)\\]\\|\\(\\)\\)"
80 "\(\\([^\n]*\\)\)\\(\\[\\(.*?\\)\\]\\)?")
81 "Regexp to match inline calls to predefined source block functions.")
83 (defconst org-babel-lob-one-liner-regexp
84 (concat "\\(" org-babel-block-lob-one-liner-regexp
85 "\\|" org-babel-inline-lob-one-liner-regexp "\\)")
86 "Regexp to match calls to predefined source block functions.")
88 ;; functions for executing lob one-liners
89 ;;;###autoload
90 (defun org-babel-lob-execute-maybe ()
91 "Execute a Library of Babel source block, if appropriate.
92 Detect if this is context for a Library Of Babel source block and
93 if so then run the appropriate source block from the Library."
94 (interactive)
95 (let ((info (org-babel-lob-get-info)))
96 (if (nth 0 info) (progn (org-babel-lob-execute info) t) nil)))
98 ;;;###autoload
99 (defun org-babel-lob-get-info ()
100 "Return a Library of Babel function call as a string."
101 (flet ((nonempty (a b)
102 (let ((it (match-string a)))
103 (if (= (length it) 0) (match-string b) it))))
104 (let ((case-fold-search t))
105 (save-excursion
106 (beginning-of-line 1)
107 (when (looking-at org-babel-lob-one-liner-regexp)
108 (append
109 (mapcar #'org-babel-clean-text-properties
110 (list
111 (format "%s%s(%s)%s"
112 (nonempty 3 12)
113 (if (not (= 0 (length (nonempty 5 13))))
114 (concat "[" (nonempty 5 13) "]") "")
115 (or (nonempty 7 16) "")
116 (or (nonempty 8 19) ""))
117 (nonempty 9 18)))
118 (list (length (nonempty 1 11)))))))))
120 (defun org-babel-lob-execute (info)
121 "Execute the lob call specified by INFO."
122 (let ((params (org-babel-process-params
123 (org-babel-merge-params
124 org-babel-default-header-args
125 (org-babel-params-from-buffer)
126 (org-babel-params-from-properties)
127 (org-babel-parse-header-arguments
128 (org-babel-clean-text-properties
129 (concat ":var results="
130 (mapconcat #'identity (butlast info) " "))))))))
131 (org-babel-execute-src-block
132 nil (list "emacs-lisp" "results" params nil nil (nth 2 info)))))
134 (provide 'ob-lob)
136 ;; arch-tag: ce0712c9-2147-4019-ba3f-42341b8b474b
138 ;;; ob-lob.el ends here