Commit | Line | Data |
---|---|---|
86fbb8ca CD |
1 | ;;; ob-exp.el --- Exportation of org-babel source blocks |
2 | ||
ab422c4d | 3 | ;; Copyright (C) 2009-2013 Free Software Foundation, Inc. |
86fbb8ca | 4 | |
dfd98937 | 5 | ;; Authors: Eric Schulte |
c7557a0f | 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 CD |
25 | ;;; Code: |
26 | (require 'ob) | |
27 | (require 'org-exp-blocks) | |
28 | (eval-when-compile | |
29 | (require 'cl)) | |
30 | ||
31 | (defvar obe-marker nil) | |
32 | (defvar org-current-export-file) | |
33 | (defvar org-babel-lob-one-liner-regexp) | |
34 | (defvar org-babel-ref-split-regexp) | |
8223b1d2 BG |
35 | (defvar org-list-forbidden-blocks) |
36 | ||
86fbb8ca | 37 | (declare-function org-babel-lob-get-info "ob-lob" ()) |
acedf35c | 38 | (declare-function org-babel-eval-wipe-error-buffer "ob-eval" ()) |
8223b1d2 BG |
39 | (declare-function org-heading-components "org" ()) |
40 | (declare-function org-link-search "org" (s &optional type avoid-pos stealth)) | |
41 | (declare-function org-fill-template "org" (template alist)) | |
42 | (declare-function org-in-verbatim-emphasis "org" ()) | |
43 | (declare-function org-in-block-p "org" (names)) | |
44 | (declare-function org-between-regexps-p "org" (start-re end-re &optional lim-up lim-down)) | |
86fbb8ca | 45 | |
8223b1d2 | 46 | (add-to-list 'org-export-interblocks '(src org-babel-exp-non-block-elements)) |
3ab2c837 | 47 | (org-export-blocks-add-block '(src org-babel-exp-src-block nil)) |
86fbb8ca CD |
48 | |
49 | (defcustom org-export-babel-evaluate t | |
50 | "Switch controlling code evaluation during export. | |
afe98dfa | 51 | When set to nil no code will be evaluated as part of the export |
86fbb8ca CD |
52 | process." |
53 | :group 'org-babel | |
372d7b21 | 54 | :version "24.1" |
86fbb8ca CD |
55 | :type 'boolean) |
56 | (put 'org-export-babel-evaluate 'safe-local-variable (lambda (x) (eq x nil))) | |
57 | ||
8223b1d2 BG |
58 | (defun org-babel-exp-get-export-buffer () |
59 | "Return the current export buffer if possible." | |
60 | (cond | |
61 | ((bufferp org-current-export-file) org-current-export-file) | |
62 | (org-current-export-file (get-file-buffer org-current-export-file)) | |
63 | ('otherwise | |
64 | (error "Requested export buffer when `org-current-export-file' is nil")))) | |
65 | ||
3ab2c837 BG |
66 | (defmacro org-babel-exp-in-export-file (lang &rest body) |
67 | (declare (indent 1)) | |
68 | `(let* ((lang-headers (intern (concat "org-babel-default-header-args:" ,lang))) | |
afe98dfa | 69 | (heading (nth 4 (ignore-errors (org-heading-components)))) |
8223b1d2 BG |
70 | (export-buffer (current-buffer)) |
71 | (original-buffer (org-babel-exp-get-export-buffer)) results) | |
72 | (when original-buffer | |
afe98dfa CD |
73 | ;; resolve parameters in the original file so that |
74 | ;; headline and file-wide parameters are included, attempt | |
75 | ;; to go to the same heading in the original file | |
8223b1d2 | 76 | (set-buffer original-buffer) |
afe98dfa | 77 | (save-restriction |
8223b1d2 BG |
78 | (when heading |
79 | (condition-case nil | |
80 | (let ((org-link-search-inhibit-query t)) | |
81 | (org-link-search heading)) | |
82 | (error (when heading | |
83 | (goto-char (point-min)) | |
84 | (re-search-forward (regexp-quote heading) nil t))))) | |
afe98dfa CD |
85 | (setq results ,@body)) |
86 | (set-buffer export-buffer) | |
87 | results))) | |
e66ba1df | 88 | (def-edebug-spec org-babel-exp-in-export-file (form body)) |
afe98dfa | 89 | |
3ab2c837 | 90 | (defun org-babel-exp-src-block (body &rest headers) |
86fbb8ca CD |
91 | "Process source block for export. |
92 | Depending on the 'export' headers argument in replace the source | |
93 | code block with... | |
94 | ||
95 | both ---- display the code and the results | |
96 | ||
97 | code ---- the default, display the code inside the block but do | |
98 | not process | |
99 | ||
100 | results - just like none only the block is run on export ensuring | |
101 | that it's results are present in the org-mode buffer | |
102 | ||
103 | none ----- do not display either code or results upon export" | |
104 | (interactive) | |
3ab2c837 | 105 | (unless noninteractive (message "org-babel-exp processing...")) |
86fbb8ca CD |
106 | (save-excursion |
107 | (goto-char (match-beginning 0)) | |
afe98dfa CD |
108 | (let* ((info (org-babel-get-src-block-info 'light)) |
109 | (lang (nth 0 info)) | |
3ab2c837 | 110 | (raw-params (nth 2 info)) hash) |
86fbb8ca CD |
111 | ;; bail if we couldn't get any info from the block |
112 | (when info | |
3ab2c837 BG |
113 | ;; if we're actually going to need the parameters |
114 | (when (member (cdr (assoc :exports (nth 2 info))) '("both" "results")) | |
115 | (org-babel-exp-in-export-file lang | |
116 | (setf (nth 2 info) | |
117 | (org-babel-process-params | |
118 | (org-babel-merge-params | |
119 | org-babel-default-header-args | |
3ab2c837 BG |
120 | (org-babel-params-from-properties lang) |
121 | (if (boundp lang-headers) (eval lang-headers) nil) | |
122 | raw-params)))) | |
123 | (setf hash (org-babel-sha1-hash info))) | |
3ab2c837 | 124 | (org-babel-exp-do-export info 'block hash))))) |
86fbb8ca | 125 | |
8223b1d2 BG |
126 | (defcustom org-babel-exp-call-line-template |
127 | "" | |
128 | "Template used to export call lines. | |
129 | This template may be customized to include the call line name | |
130 | with any export markup. The template is filled out using | |
131 | `org-fill-template', and the following %keys may be used. | |
132 | ||
133 | line --- call line | |
134 | ||
135 | An example value would be \"\\n: call: %line\" to export the call line | |
136 | wrapped in a verbatim environment. | |
137 | ||
138 | Note: the results are inserted separately after the contents of | |
139 | this template." | |
140 | :group 'org-babel | |
141 | :type 'string) | |
142 | ||
153ae947 BG |
143 | (defvar org-babel-default-lob-header-args) |
144 | (defun org-babel-exp-non-block-elements (start end) | |
145 | "Process inline source and call lines between START and END for export." | |
86fbb8ca CD |
146 | (interactive) |
147 | (save-excursion | |
148 | (goto-char start) | |
153ae947 BG |
149 | (unless (markerp end) |
150 | (let ((m (make-marker))) | |
151 | (set-marker m end (current-buffer)) | |
152 | (setq end m))) | |
153 | (let ((rx (concat "\\(" org-babel-inline-src-block-regexp | |
154 | "\\|" org-babel-lob-one-liner-regexp "\\)"))) | |
155 | (while (and (< (point) (marker-position end)) | |
156 | (re-search-forward rx end t)) | |
157 | (if (save-excursion | |
158 | (goto-char (match-beginning 0)) | |
159 | (looking-at org-babel-inline-src-block-regexp)) | |
160 | (progn | |
161 | (forward-char 1) | |
162 | (let* ((info (save-match-data | |
163 | (org-babel-parse-inline-src-block-match))) | |
164 | (params (nth 2 info))) | |
165 | (save-match-data | |
166 | (goto-char (match-beginning 2)) | |
167 | (unless (org-babel-in-example-or-verbatim) | |
168 | ;; expand noweb references in the original file | |
169 | (setf (nth 1 info) | |
170 | (if (and (cdr (assoc :noweb params)) | |
171 | (string= "yes" (cdr (assoc :noweb params)))) | |
172 | (org-babel-expand-noweb-references | |
8223b1d2 | 173 | info (org-babel-exp-get-export-buffer)) |
153ae947 BG |
174 | (nth 1 info))) |
175 | (let ((code-replacement (save-match-data | |
176 | (org-babel-exp-do-export | |
177 | info 'inline)))) | |
178 | (if code-replacement | |
179 | (progn (replace-match code-replacement nil nil nil 1) | |
180 | (delete-char 1)) | |
181 | (org-babel-examplize-region (match-beginning 1) | |
182 | (match-end 1)) | |
183 | (forward-char 2))))))) | |
e66ba1df | 184 | (unless (org-babel-in-example-or-verbatim) |
153ae947 BG |
185 | (let* ((lob-info (org-babel-lob-get-info)) |
186 | (inlinep (match-string 11)) | |
187 | (inline-start (match-end 11)) | |
188 | (inline-end (match-end 0)) | |
8223b1d2 BG |
189 | (results (save-match-data |
190 | (org-babel-exp-do-export | |
191 | (list "emacs-lisp" "results" | |
192 | (org-babel-merge-params | |
193 | org-babel-default-header-args | |
194 | org-babel-default-lob-header-args | |
195 | (org-babel-params-from-properties) | |
196 | (org-babel-parse-header-arguments | |
197 | (org-no-properties | |
198 | (concat ":var results=" | |
199 | (mapconcat #'identity | |
200 | (butlast lob-info) | |
201 | " "))))) | |
202 | "" nil (car (last lob-info))) | |
203 | 'lob))) | |
204 | (rep (org-fill-template | |
205 | org-babel-exp-call-line-template | |
206 | `(("line" . ,(nth 0 lob-info)))))) | |
153ae947 BG |
207 | (if inlinep |
208 | (save-excursion | |
209 | (goto-char inline-start) | |
210 | (delete-region inline-start inline-end) | |
211 | (insert rep)) | |
212 | (replace-match rep t t))))))))) | |
86fbb8ca CD |
213 | |
214 | (defun org-babel-in-example-or-verbatim () | |
215 | "Return true if point is in example or verbatim code. | |
216 | Example and verbatim code include escaped portions of | |
217 | an org-mode buffer code that should be treated as normal | |
218 | org-mode text." | |
e66ba1df | 219 | (or (save-match-data |
3ab2c837 | 220 | (save-excursion |
86fbb8ca CD |
221 | (goto-char (point-at-bol)) |
222 | (looking-at "[ \t]*:[ \t]"))) | |
3ab2c837 | 223 | (org-in-verbatim-emphasis) |
e66ba1df BG |
224 | (org-in-block-p org-list-forbidden-blocks) |
225 | (org-between-regexps-p "^[ \t]*#\\+begin_src" "^[ \t]*#\\+end_src"))) | |
86fbb8ca | 226 | |
3ab2c837 | 227 | (defun org-babel-exp-do-export (info type &optional hash) |
86fbb8ca CD |
228 | "Return a string with the exported content of a code block. |
229 | The function respects the value of the :exports header argument." | |
8223b1d2 BG |
230 | (let ((silently (lambda () (let ((session (cdr (assoc :session (nth 2 info))))) |
231 | (when (not (and session (equal "none" session))) | |
232 | (org-babel-exp-results info type 'silent))))) | |
233 | (clean (lambda () (unless (eq type 'inline) (org-babel-remove-result info))))) | |
86fbb8ca | 234 | (case (intern (or (cdr (assoc :exports (nth 2 info))) "code")) |
8223b1d2 BG |
235 | ('none (funcall silently) (funcall clean) "") |
236 | ('code (funcall silently) (funcall clean) (org-babel-exp-code info)) | |
3ab2c837 BG |
237 | ('results (org-babel-exp-results info type nil hash) "") |
238 | ('both (org-babel-exp-results info type nil hash) | |
239 | (org-babel-exp-code info))))) | |
86fbb8ca | 240 | |
8223b1d2 BG |
241 | (defcustom org-babel-exp-code-template |
242 | "#+BEGIN_SRC %lang%flags\n%body\n#+END_SRC" | |
243 | "Template used to export the body of code blocks. | |
244 | This template may be customized to include additional information | |
245 | such as the code block name, or the values of particular header | |
246 | arguments. The template is filled out using `org-fill-template', | |
247 | and the following %keys may be used. | |
248 | ||
249 | lang ------ the language of the code block | |
250 | name ------ the name of the code block | |
251 | body ------ the body of the code block | |
252 | flags ----- the flags passed to the code block | |
253 | ||
254 | In addition to the keys mentioned above, every header argument | |
255 | defined for the code block may be used as a key and will be | |
256 | replaced with its value." | |
257 | :group 'org-babel | |
258 | :type 'string) | |
259 | ||
3ab2c837 BG |
260 | (defun org-babel-exp-code (info) |
261 | "Return the original code block formatted for export." | |
8223b1d2 BG |
262 | (setf (nth 1 info) |
263 | (if (string= "strip-export" (cdr (assoc :noweb (nth 2 info)))) | |
264 | (replace-regexp-in-string | |
265 | (org-babel-noweb-wrap) "" (nth 1 info)) | |
266 | (if (org-babel-noweb-p (nth 2 info) :export) | |
267 | (org-babel-expand-noweb-references | |
268 | info (org-babel-exp-get-export-buffer)) | |
269 | (nth 1 info)))) | |
3ab2c837 | 270 | (org-fill-template |
8223b1d2 | 271 | org-babel-exp-code-template |
3ab2c837 | 272 | `(("lang" . ,(nth 0 info)) |
153ae947 BG |
273 | ("body" . ,(if (string= (nth 0 info) "org") |
274 | (replace-regexp-in-string "^" "," (nth 1 info)) | |
8223b1d2 BG |
275 | (nth 1 info))) |
276 | ,@(mapcar (lambda (pair) | |
277 | (cons (substring (symbol-name (car pair)) 1) | |
278 | (format "%S" (cdr pair)))) | |
279 | (nth 2 info)) | |
280 | ("flags" . ,((lambda (f) (when f (concat " " f))) (nth 3 info))) | |
281 | ("name" . ,(or (nth 4 info) ""))))) | |
86fbb8ca | 282 | |
3ab2c837 | 283 | (defun org-babel-exp-results (info type &optional silent hash) |
86fbb8ca CD |
284 | "Evaluate and return the results of the current code block for export. |
285 | Results are prepared in a manner suitable for export by org-mode. | |
286 | This function is called by `org-babel-exp-do-export'. The code | |
287 | block will be evaluated. Optional argument SILENT can be used to | |
288 | inhibit insertion of results into the buffer." | |
3ab2c837 BG |
289 | (when (and org-export-babel-evaluate |
290 | (not (and hash (equal hash (org-babel-current-result-hash))))) | |
291 | (let ((lang (nth 0 info)) | |
8223b1d2 BG |
292 | (body (if (org-babel-noweb-p (nth 2 info) :eval) |
293 | (org-babel-expand-noweb-references | |
294 | info (org-babel-exp-get-export-buffer)) | |
295 | (nth 1 info))) | |
296 | (info (copy-sequence info))) | |
3ab2c837 BG |
297 | ;; skip code blocks which we can't evaluate |
298 | (when (fboundp (intern (concat "org-babel-execute:" lang))) | |
299 | (org-babel-eval-wipe-error-buffer) | |
300 | (prog1 nil | |
8223b1d2 | 301 | (setf (nth 1 info) body) |
3ab2c837 BG |
302 | (setf (nth 2 info) |
303 | (org-babel-exp-in-export-file lang | |
304 | (org-babel-process-params | |
afe98dfa CD |
305 | (org-babel-merge-params |
306 | (nth 2 info) | |
3ab2c837 BG |
307 | `((:results . ,(if silent "silent" "replace"))))))) |
308 | (cond | |
e66ba1df BG |
309 | ((equal type 'block) |
310 | (org-babel-execute-src-block nil info)) | |
311 | ((equal type 'inline) | |
312 | ;; position the point on the inline source block allowing | |
313 | ;; `org-babel-insert-result' to check that the block is | |
314 | ;; inline | |
315 | (re-search-backward "[ \f\t\n\r\v]" nil t) | |
316 | (re-search-forward org-babel-inline-src-block-regexp nil t) | |
317 | (re-search-backward "src_" nil t) | |
3ab2c837 BG |
318 | (org-babel-execute-src-block nil info)) |
319 | ((equal type 'lob) | |
320 | (save-excursion | |
321 | (re-search-backward org-babel-lob-one-liner-regexp nil t) | |
322 | (org-babel-execute-src-block nil info))))))))) | |
86fbb8ca CD |
323 | |
324 | (provide 'ob-exp) | |
325 | ||
5b409b39 | 326 | |
86fbb8ca CD |
327 | |
328 | ;;; ob-exp.el ends here |