1 ;;; ob-lob.el --- functions supporting the Library of Babel
3 ;; Copyright (C) 2009-2011 Free Software Foundation, Inc.
5 ;; Author: Eric Schulte
7 ;; Keywords: literate programming, reproducible research
8 ;; Homepage: http://orgmode.org
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
32 (defvar org-babel-library-of-babel nil
33 "Library of source-code blocks.
34 This is an association list. Populate the library by adding
35 files to `org-babel-lob-files'.")
37 (defcustom org-babel-lob-files
'()
38 "Files used to populate the `org-babel-library-of-babel'.
39 To add files to this list use the `org-babel-lob-ingest' command."
43 (defvar org-babel-default-lob-header-args
'((:exports .
"results"))
44 "Default header arguments to use when exporting #+lob/call lines.")
47 (defun org-babel-lob-ingest (&optional file
)
48 "Add all named source-blocks defined in FILE to
49 `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
)))
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" ""))
65 (defconst org-babel-lob-call-aliases
'("lob" "call")
66 "Aliases to call a source block function.
67 If you change the value of this variable then your files may
68 become unusable by other org-babel users, and vice versa.")
70 (defconst org-babel-block-lob-one-liner-regexp
72 "^\\([ \t]*\\)#\\+\\(?:"
73 (mapconcat #'regexp-quote org-babel-lob-call-aliases
"\\|")
74 "\\):[ \t]+\\([^\(\)\n]+?\\)\\(\\[\\(.*\\)\\]\\|\\(\\)\\)"
75 "\(\\([^\n]*\\)\)\\(\\[.+\\]\\|\\)[ \t]*\\(\\([^\n]*\\)\\)?")
76 "Regexp to match non-inline calls to predefined source block functions.")
78 (defconst org-babel-inline-lob-one-liner-regexp
81 (mapconcat #'regexp-quote org-babel-lob-call-aliases
"\\|")
82 "\\)_\\([^\(\)\n]+?\\)\\(\\[\\(.*\\)\\]\\|\\(\\)\\)"
83 "\(\\([^\n]*\\)\)\\(\\[\\(.*?\\)\\]\\)?")
84 "Regexp to match inline calls to predefined source block functions.")
86 (defconst org-babel-lob-one-liner-regexp
87 (concat "\\(" org-babel-block-lob-one-liner-regexp
88 "\\|" org-babel-inline-lob-one-liner-regexp
"\\)")
89 "Regexp to match calls to predefined source block functions.")
91 ;; functions for executing lob one-liners
93 (defun org-babel-lob-execute-maybe ()
94 "Execute a Library of Babel source block, if appropriate.
95 Detect if this is context for a Library Of Babel source block and
96 if so then run the appropriate source block from the Library."
98 (let ((info (org-babel-lob-get-info)))
99 (if (nth 0 info
) (progn (org-babel-lob-execute info
) t
) nil
)))
102 (defun org-babel-lob-get-info ()
103 "Return a Library of Babel function call as a string."
104 (flet ((nonempty (a b
)
105 (let ((it (match-string a
)))
106 (if (= (length it
) 0) (match-string b
) it
))))
107 (let ((case-fold-search t
))
109 (beginning-of-line 1)
110 (when (looking-at org-babel-lob-one-liner-regexp
)
112 (mapcar #'org-babel-clean-text-properties
116 (if (not (= 0 (length (nonempty 5 13))))
117 (concat "[" (nonempty 5 13) "]") "")
118 (or (nonempty 7 16) "")
119 (or (nonempty 8 19) ""))
121 (list (length (if (= (length (match-string 12)) 0)
122 (match-string 2) (match-string 11))))))))))
124 (defun org-babel-lob-execute (info)
125 "Execute the lob call specified by INFO."
126 (let ((params (org-babel-process-params
127 (org-babel-merge-params
128 org-babel-default-header-args
129 (org-babel-params-from-buffer)
130 (org-babel-params-from-properties)
131 (org-babel-parse-header-arguments
132 (org-babel-clean-text-properties
133 (concat ":var results="
134 (mapconcat #'identity
(butlast info
) " "))))))))
135 (org-babel-execute-src-block
136 nil
(list "emacs-lisp" "results" params nil nil
(nth 2 info
)))))
142 ;;; ob-lob.el ends here