Commit | Line | Data |
---|---|---|
86fbb8ca CD |
1 | ;;; ob-exp.el --- Exportation of org-babel source blocks |
2 | ||
73b0cd50 | 3 | ;; Copyright (C) 2009-2011 Free Software Foundation, Inc. |
86fbb8ca CD |
4 | |
5 | ;; Author: Eric Schulte, Dan Davison | |
6 | ;; Keywords: literate programming, reproducible research | |
7 | ;; Homepage: http://orgmode.org | |
acedf35c | 8 | ;; Version: 7.4 |
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 | ||
25 | ;;; Commentary: | |
26 | ||
27 | ;; See the online documentation for more information | |
28 | ;; | |
29 | ;; http://orgmode.org/worg/org-contrib/babel/ | |
30 | ||
31 | ;;; Code: | |
32 | (require 'ob) | |
33 | (require 'org-exp-blocks) | |
34 | (eval-when-compile | |
35 | (require 'cl)) | |
36 | ||
37 | (defvar obe-marker nil) | |
38 | (defvar org-current-export-file) | |
39 | (defvar org-babel-lob-one-liner-regexp) | |
40 | (defvar org-babel-ref-split-regexp) | |
41 | (declare-function org-babel-lob-get-info "ob-lob" ()) | |
acedf35c | 42 | (declare-function org-babel-eval-wipe-error-buffer "ob-eval" ()) |
86fbb8ca CD |
43 | (add-to-list 'org-export-interblocks '(src org-babel-exp-inline-src-blocks)) |
44 | (add-to-list 'org-export-interblocks '(lob org-babel-exp-lob-one-liners)) | |
45 | (add-hook 'org-export-blocks-postblock-hook 'org-exp-res/src-name-cleanup) | |
46 | ||
47 | (org-export-blocks-add-block '(src org-babel-exp-src-blocks nil)) | |
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 | |
54 | :type 'boolean) | |
55 | (put 'org-export-babel-evaluate 'safe-local-variable (lambda (x) (eq x nil))) | |
56 | ||
57 | (defvar org-babel-function-def-export-keyword "function" | |
58 | "The keyword to substitute for the source name line on export. | |
59 | When exporting a source block function, this keyword will | |
60 | appear in the exported version in the place of source name | |
61 | line. A source block is considered to be a source block function | |
62 | if the source name is present and is followed by a parenthesized | |
63 | argument list. The parentheses may be empty or contain | |
64 | whitespace. An example is the following which generates n random | |
65 | \(uniform) numbers. | |
66 | ||
67 | #+source: rand(n) | |
68 | #+begin_src R | |
69 | runif(n) | |
70 | #+end_src") | |
71 | ||
72 | (defvar org-babel-function-def-export-indent 4 | |
73 | "Number of characters to indent a source block on export. | |
74 | When exporting a source block function, the block contents will | |
75 | be indented by this many characters. See | |
76 | `org-babel-function-def-export-name' for the definition of a | |
77 | source block function.") | |
78 | ||
afe98dfa CD |
79 | (defmacro org-babel-exp-in-export-file (&rest body) |
80 | `(let* ((lang-headers (intern (concat "org-babel-default-header-args:" lang))) | |
81 | (heading (nth 4 (ignore-errors (org-heading-components)))) | |
82 | (link (when org-current-export-file | |
83 | (org-make-link-string | |
84 | (if heading | |
85 | (concat org-current-export-file "::" heading) | |
86 | org-current-export-file)))) | |
87 | (export-buffer (current-buffer)) results) | |
88 | (when link | |
89 | ;; resolve parameters in the original file so that | |
90 | ;; headline and file-wide parameters are included, attempt | |
91 | ;; to go to the same heading in the original file | |
92 | (set-buffer (get-file-buffer org-current-export-file)) | |
93 | (save-restriction | |
94 | (condition-case nil | |
95 | (org-open-link-from-string link) | |
96 | (error (when heading | |
97 | (goto-char (point-min)) | |
98 | (re-search-forward (regexp-quote heading) nil t)))) | |
99 | (setq results ,@body)) | |
100 | (set-buffer export-buffer) | |
101 | results))) | |
102 | ||
86fbb8ca CD |
103 | (defun org-babel-exp-src-blocks (body &rest headers) |
104 | "Process source block for export. | |
105 | Depending on the 'export' headers argument in replace the source | |
106 | code block with... | |
107 | ||
108 | both ---- display the code and the results | |
109 | ||
110 | code ---- the default, display the code inside the block but do | |
111 | not process | |
112 | ||
113 | results - just like none only the block is run on export ensuring | |
114 | that it's results are present in the org-mode buffer | |
115 | ||
116 | none ----- do not display either code or results upon export" | |
117 | (interactive) | |
118 | (message "org-babel-exp processing...") | |
119 | (save-excursion | |
120 | (goto-char (match-beginning 0)) | |
afe98dfa CD |
121 | (let* ((info (org-babel-get-src-block-info 'light)) |
122 | (lang (nth 0 info)) | |
123 | (raw-params (nth 2 info))) | |
86fbb8ca CD |
124 | ;; bail if we couldn't get any info from the block |
125 | (when info | |
afe98dfa CD |
126 | (org-babel-exp-in-export-file |
127 | (setf (nth 2 info) | |
128 | (org-babel-merge-params | |
129 | org-babel-default-header-args | |
130 | (org-babel-params-from-buffer) | |
131 | (org-babel-params-from-properties lang) | |
132 | (if (boundp lang-headers) (eval lang-headers) nil) | |
133 | raw-params))) | |
86fbb8ca CD |
134 | ;; expand noweb references in the original file |
135 | (setf (nth 1 info) | |
afe98dfa CD |
136 | (if (and (cdr (assoc :noweb (nth 2 info))) |
137 | (string= "yes" (cdr (assoc :noweb (nth 2 info))))) | |
86fbb8ca CD |
138 | (org-babel-expand-noweb-references |
139 | info (get-file-buffer org-current-export-file)) | |
afe98dfa CD |
140 | (nth 1 info))) |
141 | (org-babel-exp-do-export info 'block))))) | |
86fbb8ca CD |
142 | |
143 | (defun org-babel-exp-inline-src-blocks (start end) | |
144 | "Process inline source blocks between START and END for export. | |
145 | See `org-babel-exp-src-blocks' for export options, currently the | |
146 | options and are taken from `org-babel-default-inline-header-args'." | |
147 | (interactive) | |
148 | (save-excursion | |
149 | (goto-char start) | |
150 | (while (and (< (point) end) | |
151 | (re-search-forward org-babel-inline-src-block-regexp end t)) | |
152 | (let* ((info (save-match-data (org-babel-parse-inline-src-block-match))) | |
153 | (params (nth 2 info)) | |
154 | (replacement | |
155 | (save-match-data | |
156 | (if (org-babel-in-example-or-verbatim) | |
157 | (buffer-substring (match-beginning 0) (match-end 0)) | |
158 | ;; expand noweb references in the original file | |
159 | (setf (nth 1 info) | |
160 | (if (and (cdr (assoc :noweb params)) | |
161 | (string= "yes" (cdr (assoc :noweb params)))) | |
162 | (org-babel-expand-noweb-references | |
163 | info (get-file-buffer org-current-export-file)) | |
164 | (nth 1 info))) | |
165 | (org-babel-exp-do-export info 'inline))))) | |
166 | (setq end (+ end (- (length replacement) (length (match-string 1))))) | |
167 | (replace-match replacement t t nil 1))))) | |
168 | ||
169 | (defun org-exp-res/src-name-cleanup () | |
170 | "Clean up #+results and #+srcname lines for export. | |
171 | This function should only be called after all block processing | |
172 | has taken place." | |
173 | (interactive) | |
174 | (save-excursion | |
175 | (goto-char (point-min)) | |
176 | (while (org-re-search-forward-unprotected | |
177 | (concat | |
178 | "\\("org-babel-src-name-regexp"\\|"org-babel-result-regexp"\\)") | |
179 | nil t) | |
180 | (delete-region | |
181 | (progn (beginning-of-line) (point)) | |
182 | (progn (end-of-line) (+ 1 (point))))))) | |
183 | ||
184 | (defun org-babel-in-example-or-verbatim () | |
185 | "Return true if point is in example or verbatim code. | |
186 | Example and verbatim code include escaped portions of | |
187 | an org-mode buffer code that should be treated as normal | |
188 | org-mode text." | |
189 | (or (org-in-indented-comment-line) | |
190 | (save-excursion | |
191 | (save-match-data | |
192 | (goto-char (point-at-bol)) | |
193 | (looking-at "[ \t]*:[ \t]"))) | |
194 | (org-in-regexps-block-p "^[ \t]*#\\+begin_src" "^[ \t]*#\\+end_src"))) | |
195 | ||
196 | (defun org-babel-exp-lob-one-liners (start end) | |
197 | "Process Library of Babel calls between START and END for export. | |
198 | See `org-babel-exp-src-blocks' for export options. Currently the | |
199 | options are taken from `org-babel-default-header-args'." | |
200 | (interactive) | |
201 | (let (replacement) | |
202 | (save-excursion | |
203 | (goto-char start) | |
204 | (while (and (< (point) end) | |
205 | (re-search-forward org-babel-lob-one-liner-regexp nil t)) | |
206 | (setq replacement | |
207 | (let ((lob-info (org-babel-lob-get-info))) | |
208 | (save-match-data | |
209 | (org-babel-exp-do-export | |
210 | (list "emacs-lisp" "results" | |
211 | (org-babel-merge-params | |
212 | org-babel-default-header-args | |
afe98dfa CD |
213 | (org-babel-params-from-buffer) |
214 | (org-babel-params-from-properties) | |
86fbb8ca CD |
215 | (org-babel-parse-header-arguments |
216 | (org-babel-clean-text-properties | |
217 | (concat ":var results=" | |
218 | (mapconcat #'identity | |
219 | (butlast lob-info) " "))))) | |
220 | (car (last lob-info))) | |
221 | 'lob)))) | |
222 | (setq end (+ end (- (length replacement) (length (match-string 0))))) | |
223 | (replace-match replacement t t))))) | |
224 | ||
225 | (defun org-babel-exp-do-export (info type) | |
226 | "Return a string with the exported content of a code block. | |
227 | The function respects the value of the :exports header argument." | |
228 | (flet ((silently () (let ((session (cdr (assoc :session (nth 2 info))))) | |
229 | (when (and session | |
afe98dfa | 230 | (not (equal "none" session))) |
86fbb8ca CD |
231 | (org-babel-exp-results info type 'silent)))) |
232 | (clean () (org-babel-remove-result info))) | |
233 | (case (intern (or (cdr (assoc :exports (nth 2 info))) "code")) | |
234 | ('none (silently) (clean) "") | |
235 | ('code (silently) (clean) (org-babel-exp-code info type)) | |
236 | ('results (org-babel-exp-results info type)) | |
237 | ('both (concat (org-babel-exp-code info type) | |
238 | "\n\n" | |
239 | (org-babel-exp-results info type)))))) | |
240 | ||
241 | (defvar backend) | |
242 | (defun org-babel-exp-code (info type) | |
243 | "Prepare and return code in the current code block for export. | |
afe98dfa | 244 | Code is prepared in a manner suitable for export by |
86fbb8ca CD |
245 | org-mode. This function is called by `org-babel-exp-do-export'. |
246 | The code block is not evaluated." | |
247 | (let ((lang (nth 0 info)) | |
248 | (body (nth 1 info)) | |
249 | (switches (nth 3 info)) | |
250 | (name (nth 4 info)) | |
afe98dfa | 251 | (args (mapcar #'cdr (org-babel-get-header (nth 2 info) :var)))) |
86fbb8ca CD |
252 | (case type |
253 | ('inline (format "=%s=" body)) | |
254 | ('block | |
255 | (let ((str | |
256 | (format "#+BEGIN_SRC %s %s\n%s%s#+END_SRC\n" lang switches body | |
257 | (if (and body (string-match "\n$" body)) | |
258 | "" "\n")))) | |
259 | (when name | |
260 | (add-text-properties | |
261 | 0 (length str) | |
262 | (list 'org-caption | |
263 | (format "%s(%s)" | |
264 | name | |
265 | (mapconcat #'identity args ", "))) | |
266 | str)) | |
267 | str)) | |
268 | ('lob | |
269 | (let ((call-line (and (string-match "results=" (car args)) | |
270 | (substring (car args) (match-end 0))))) | |
271 | (cond | |
272 | ((eq backend 'html) | |
273 | (format "\n#+HTML: <label class=\"org-src-name\">%s</label>\n" | |
274 | call-line)) | |
275 | ((format ": %s\n" call-line)))))))) | |
276 | ||
277 | (defun org-babel-exp-results (info type &optional silent) | |
278 | "Evaluate and return the results of the current code block for export. | |
279 | Results are prepared in a manner suitable for export by org-mode. | |
280 | This function is called by `org-babel-exp-do-export'. The code | |
281 | block will be evaluated. Optional argument SILENT can be used to | |
282 | inhibit insertion of results into the buffer." | |
afe98dfa CD |
283 | (or |
284 | (when org-export-babel-evaluate | |
285 | (let ((lang (nth 0 info)) | |
286 | (body (nth 1 info))) | |
287 | (setf (nth 2 info) (org-babel-exp-in-export-file | |
288 | (org-babel-process-params (nth 2 info)))) | |
289 | ;; skip code blocks which we can't evaluate | |
290 | (when (fboundp (intern (concat "org-babel-execute:" lang))) | |
acedf35c | 291 | (org-babel-eval-wipe-error-buffer) |
afe98dfa CD |
292 | (if (equal type 'inline) |
293 | (let ((raw (org-babel-execute-src-block | |
294 | nil info '((:results . "silent")))) | |
295 | (result-params (split-string | |
296 | (cdr (assoc :results (nth 2 info)))))) | |
297 | (unless silent | |
298 | (cond ;; respect the value of the :results header argument | |
299 | ((member "file" result-params) | |
300 | (org-babel-result-to-file raw)) | |
301 | ((or (member "raw" result-params) | |
302 | (member "org" result-params)) | |
303 | (format "%s" raw)) | |
304 | ((member "code" result-params) | |
305 | (format "src_%s{%s}" lang raw)) | |
306 | (t | |
307 | (if (stringp raw) | |
308 | (if (= 0 (length raw)) "=(no results)=" | |
309 | (format "%s" raw)) | |
310 | (format "%S" raw)))))) | |
311 | (prog1 nil | |
312 | (setf (nth 2 info) | |
313 | (org-babel-merge-params | |
314 | (nth 2 info) | |
315 | `((:results . ,(if silent "silent" "replace"))))) | |
316 | (cond | |
317 | ((equal type 'block) (org-babel-execute-src-block nil info)) | |
318 | ((equal type 'lob) | |
86fbb8ca CD |
319 | (save-excursion |
320 | (re-search-backward org-babel-lob-one-liner-regexp nil t) | |
afe98dfa CD |
321 | (org-babel-execute-src-block nil info))))))))) |
322 | "")) | |
86fbb8ca CD |
323 | |
324 | (provide 'ob-exp) | |
325 | ||
86fbb8ca CD |
326 | |
327 | ;;; ob-exp.el ends here |