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