Commit | Line | Data |
---|---|---|
86fbb8ca CD |
1 | ;;; ob-lob.el --- functions supporting the Library of Babel |
2 | ||
ab422c4d | 3 | ;; Copyright (C) 2009-2013 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. | |
35 | This is an association list. Populate the library by adding | |
36 | files to `org-babel-lob-files'.") | |
37 | ||
38 | (defcustom org-babel-lob-files '() | |
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 | |
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 | 48 | (defun org-babel-lob-ingest (&optional file) |
afe98dfa CD |
49 | "Add all named source-blocks defined in FILE to |
50 | `org-babel-library-of-babel'." | |
3ab2c837 | 51 | (interactive "fFile: ") |
afe98dfa CD |
52 | (let ((lob-ingest-count 0)) |
53 | (org-babel-map-src-blocks file | |
54 | (let* ((info (org-babel-get-src-block-info 'light)) | |
55 | (source-name (nth 4 info))) | |
56 | (when source-name | |
57 | (setq source-name (intern source-name) | |
58 | org-babel-library-of-babel | |
59 | (cons (cons source-name info) | |
60 | (assq-delete-all source-name org-babel-library-of-babel)) | |
61 | lob-ingest-count (1+ lob-ingest-count))))) | |
62 | (message "%d src block%s added to Library of Babel" | |
63 | lob-ingest-count (if (> lob-ingest-count 1) "s" "")) | |
64 | lob-ingest-count)) | |
86fbb8ca | 65 | |
3ab2c837 | 66 | (defconst org-babel-block-lob-one-liner-regexp |
afe98dfa | 67 | (concat |
153ae947 BG |
68 | "^\\([ \t]*?\\)#\\+call:[ \t]+\\([^\(\)\n]+?\\)\\(\\[\\(.*\\)\\]\\|\\(\\)\\)" |
69 | "\(\\([^\n]*?\\)\)\\(\\[.+\\]\\|\\)[ \t]*\\(\\([^\n]*\\)\\)?") | |
3ab2c837 BG |
70 | "Regexp to match non-inline calls to predefined source block functions.") |
71 | ||
72 | (defconst org-babel-inline-lob-one-liner-regexp | |
73 | (concat | |
153ae947 BG |
74 | "\\([^\n]*?\\)call_\\([^\(\)\n]+?\\)\\(\\[\\(.*?\\)\\]\\|\\(\\)\\)" |
75 | "\(\\([^\n]*?\\)\)\\(\\[\\(.*?\\)\\]\\)?") | |
3ab2c837 BG |
76 | "Regexp to match inline calls to predefined source block functions.") |
77 | ||
78 | (defconst org-babel-lob-one-liner-regexp | |
79 | (concat "\\(" org-babel-block-lob-one-liner-regexp | |
80 | "\\|" org-babel-inline-lob-one-liner-regexp "\\)") | |
86fbb8ca CD |
81 | "Regexp to match calls to predefined source block functions.") |
82 | ||
83 | ;; functions for executing lob one-liners | |
e66ba1df | 84 | |
86fbb8ca CD |
85 | ;;;###autoload |
86 | (defun org-babel-lob-execute-maybe () | |
87 | "Execute a Library of Babel source block, if appropriate. | |
88 | Detect if this is context for a Library Of Babel source block and | |
89 | if so then run the appropriate source block from the Library." | |
90 | (interactive) | |
91 | (let ((info (org-babel-lob-get-info))) | |
e66ba1df BG |
92 | (if (and (nth 0 info) (not (org-babel-in-example-or-verbatim))) |
93 | (progn (org-babel-lob-execute info) t) | |
94 | nil))) | |
86fbb8ca | 95 | |
86fbb8ca CD |
96 | ;;;###autoload |
97 | (defun org-babel-lob-get-info () | |
afe98dfa | 98 | "Return a Library of Babel function call as a string." |
8223b1d2 BG |
99 | (let ((case-fold-search t) |
100 | (nonempty (lambda (a b) | |
101 | (let ((it (match-string a))) | |
102 | (if (= (length it) 0) (match-string b) it))))) | |
103 | (save-excursion | |
104 | (beginning-of-line 1) | |
105 | (when (looking-at org-babel-lob-one-liner-regexp) | |
106 | (append | |
107 | (mapcar #'org-no-properties | |
108 | (list | |
109 | (format "%s%s(%s)%s" | |
110 | (funcall nonempty 3 12) | |
111 | (if (not (= 0 (length (funcall nonempty 5 14)))) | |
112 | (concat "[" (funcall nonempty 5 14) "]") "") | |
113 | (or (funcall nonempty 7 16) "") | |
114 | (or (funcall nonempty 8 19) "")) | |
115 | (funcall nonempty 9 18))) | |
116 | (list (length (if (= (length (match-string 12)) 0) | |
117 | (match-string 2) (match-string 11))))))))) | |
14e1337f | 118 | |
86fbb8ca CD |
119 | (defun org-babel-lob-execute (info) |
120 | "Execute the lob call specified by INFO." | |
8223b1d2 BG |
121 | (let* ((mkinfo (lambda (p) (list "emacs-lisp" "results" p nil nil (nth 2 info)))) |
122 | (pre-params (org-babel-merge-params | |
123 | org-babel-default-header-args | |
124 | (org-babel-params-from-properties) | |
125 | (org-babel-parse-header-arguments | |
126 | (org-no-properties | |
127 | (concat ":var results=" | |
128 | (mapconcat #'identity (butlast info) " ")))))) | |
129 | (pre-info (funcall mkinfo pre-params)) | |
130 | (cache? (and (cdr (assoc :cache pre-params)) | |
131 | (string= "yes" (cdr (assoc :cache pre-params))))) | |
132 | (new-hash (when cache? (org-babel-sha1-hash pre-info))) | |
133 | (old-hash (when cache? (org-babel-current-result-hash)))) | |
134 | (if (and cache? (equal new-hash old-hash)) | |
135 | (save-excursion (goto-char (org-babel-where-is-src-block-result)) | |
136 | (forward-line 1) | |
137 | (message "%S" (org-babel-read-result))) | |
138 | (prog1 (org-babel-execute-src-block | |
139 | nil (funcall mkinfo (org-babel-process-params pre-params))) | |
140 | ;; update the hash | |
141 | (when new-hash (org-babel-set-current-result-hash new-hash)))))) | |
86fbb8ca CD |
142 | |
143 | (provide 'ob-lob) | |
144 | ||
bdebdb64 BG |
145 | ;; Local variables: |
146 | ;; generated-autoload-file: "org-loaddefs.el" | |
147 | ;; End: | |
86fbb8ca CD |
148 | |
149 | ;;; ob-lob.el ends here |