Merge branch 'maint'
[org-mode.git] / lisp / ob-exp.el
blob220a3c36f5614d0bb7e48c218ac79b04eb1144c0
1 ;;; ob-exp.el --- Exportation of org-babel source blocks
3 ;; Copyright (C) 2009-2014 Free Software Foundation, Inc.
5 ;; Authors: Eric Schulte
6 ;; Dan Davison
7 ;; Keywords: literate programming, reproducible research
8 ;; Homepage: http://orgmode.org
10 ;; This file is part of GNU Emacs.
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.
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.
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/>.
25 ;;; Code:
26 (require 'ob-core)
27 (eval-when-compile
28 (require 'cl))
30 (defvar org-babel-lob-one-liner-regexp)
31 (defvar org-babel-ref-split-regexp)
32 (defvar org-list-forbidden-blocks)
34 (declare-function org-babel-lob-get-info "ob-lob" ())
35 (declare-function org-babel-eval-wipe-error-buffer "ob-eval" ())
36 (declare-function org-between-regexps-p "org"
37 (start-re end-re &optional lim-up lim-down))
38 (declare-function org-get-indentation "org" (&optional line))
39 (declare-function org-heading-components "org" ())
40 (declare-function org-in-commented-heading-p "org" (&optional no-inheritance))
41 (declare-function org-in-block-p "org" (names))
42 (declare-function org-in-verbatim-emphasis "org" ())
43 (declare-function org-link-search "org" (s &optional type avoid-pos stealth))
44 (declare-function org-fill-template "org" (template alist))
45 (declare-function org-split-string "org" (string &optional separators))
46 (declare-function org-element-at-point "org-element" ())
47 (declare-function org-element-context "org-element" ())
48 (declare-function org-element-property "org-element" (property element))
49 (declare-function org-element-type "org-element" (element))
50 (declare-function org-escape-code-in-string "org-src" (s))
52 (defcustom org-export-babel-evaluate t
53 "Switch controlling code evaluation during export.
54 When set to nil no code will be evaluated as part of the export
55 process. When set to 'inline-only, only inline code blocks will
56 be executed."
57 :group 'org-babel
58 :version "24.1"
59 :type '(choice (const :tag "Never" nil)
60 (const :tag "Only inline code" inline-only)
61 (const :tag "Always" t)))
62 (put 'org-export-babel-evaluate 'safe-local-variable (lambda (x) (eq x nil)))
64 (defvar org-link-search-inhibit-query)
65 (defmacro org-babel-exp-in-export-file (lang &rest body)
66 (declare (indent 1))
67 `(let* ((lang-headers (intern (concat "org-babel-default-header-args:" ,lang)))
68 (heading (nth 4 (ignore-errors (org-heading-components))))
69 (export-buffer (current-buffer))
70 results)
71 (when org-babel-exp-reference-buffer
72 ;; Resolve parameters in the original file so that headline and
73 ;; file-wide parameters are included, attempt to go to the same
74 ;; heading in the original file
75 (set-buffer org-babel-exp-reference-buffer)
76 (save-restriction
77 (when heading
78 (condition-case nil
79 (let ((org-link-search-inhibit-query t))
80 (org-link-search heading))
81 (error (when heading
82 (goto-char (point-min))
83 (re-search-forward (regexp-quote heading) nil t)))))
84 (setq results ,@body))
85 (set-buffer export-buffer)
86 results)))
87 (def-edebug-spec org-babel-exp-in-export-file (form body))
89 (defun org-babel-exp-src-block (&rest headers)
90 "Process source block for export.
91 Depending on the 'export' headers argument, replace the source
92 code block like this:
94 both ---- display the code and the results
96 code ---- the default, display the code inside the block but do
97 not process
99 results - just like none only the block is run on export ensuring
100 that it's results are present in the org-mode buffer
102 none ---- do not display either code or results upon export
104 Assume point is at the beginning of block's starting line."
105 (interactive)
106 (save-excursion
107 (let* ((info (org-babel-get-src-block-info 'light))
108 (line (org-current-line))
109 (lang (nth 0 info))
110 (raw-params (nth 2 info)) hash)
111 ;; bail if we couldn't get any info from the block
112 (unless noninteractive
113 (message "org-babel-exp process %s at line %d..." lang line))
114 (when info
115 ;; if we're actually going to need the parameters
116 (when (member (cdr (assoc :exports (nth 2 info))) '("both" "results"))
117 (org-babel-exp-in-export-file lang
118 (setf (nth 2 info)
119 (org-babel-process-params
120 (apply #'org-babel-merge-params
121 org-babel-default-header-args
122 (if (boundp lang-headers) (eval lang-headers) nil)
123 (append (org-babel-params-from-properties lang)
124 (list raw-params))))))
125 (setf hash (org-babel-sha1-hash info)))
126 (org-babel-exp-do-export info 'block hash)))))
128 (defcustom org-babel-exp-call-line-template
130 "Template used to export call lines.
131 This template may be customized to include the call line name
132 with any export markup. The template is filled out using
133 `org-fill-template', and the following %keys may be used.
135 line --- call line
137 An example value would be \"\\n: call: %line\" to export the call line
138 wrapped in a verbatim environment.
140 Note: the results are inserted separately after the contents of
141 this template."
142 :group 'org-babel
143 :type 'string)
145 (defvar org-babel-default-lob-header-args)
146 (defun org-babel-exp-process-buffer (reference-buffer)
147 "Execute all Babel blocks in current buffer.
148 REFERENCE-BUFFER is the buffer containing a pristine copy of the
149 buffer being processed. It is used to properly resolve
150 references in source blocks, as modifications in current buffer
151 may make them unreachable."
152 (interactive)
153 (save-window-excursion
154 (save-excursion
155 (let ((case-fold-search t)
156 (org-babel-exp-reference-buffer reference-buffer)
157 (regexp (concat org-babel-inline-src-block-regexp "\\|"
158 org-babel-lob-one-liner-regexp "\\|"
159 "^[ \t]*#\\+BEGIN_SRC")))
160 (goto-char (point-min))
161 (while (re-search-forward regexp nil t)
162 (unless (save-match-data (org-in-commented-heading-p))
163 (let* ((element (save-excursion
164 ;; If match is inline, point is at its
165 ;; end. Move backward so
166 ;; `org-element-context' can get the
167 ;; object, not the following one.
168 (backward-char)
169 (save-match-data (org-element-context))))
170 (type (org-element-type element))
171 (begin (copy-marker (org-element-property :begin element)))
172 (end (copy-marker
173 (save-excursion
174 (goto-char (org-element-property :end element))
175 (skip-chars-backward " \r\t\n")
176 (point)))))
177 (case type
178 (inline-src-block
179 (let* ((info (org-babel-parse-inline-src-block-match))
180 (params (nth 2 info)))
181 (setf (nth 1 info)
182 (if (and (cdr (assoc :noweb params))
183 (string= "yes" (cdr (assoc :noweb params))))
184 (org-babel-expand-noweb-references
185 info org-babel-exp-reference-buffer)
186 (nth 1 info)))
187 (goto-char begin)
188 (let ((replacement (org-babel-exp-do-export info 'inline)))
189 (if (equal replacement "")
190 ;; Replacement code is empty: remove inline
191 ;; source block, including extra white space
192 ;; that might have been created when
193 ;; inserting results.
194 (delete-region begin
195 (progn (goto-char end)
196 (skip-chars-forward " \t")
197 (point)))
198 ;; Otherwise: remove inline src block but
199 ;; preserve following white spaces. Then
200 ;; insert value.
201 (delete-region begin end)
202 (insert replacement)))))
203 ((babel-call inline-babel-call)
204 (let* ((lob-info (org-babel-lob-get-info))
205 (results
206 (org-babel-exp-do-export
207 (list "emacs-lisp" "results"
208 (apply #'org-babel-merge-params
209 org-babel-default-header-args
210 org-babel-default-lob-header-args
211 (append
212 (org-babel-params-from-properties)
213 (list
214 (org-babel-parse-header-arguments
215 (org-no-properties
216 (concat
217 ":var results="
218 (mapconcat 'identity
219 (butlast lob-info 2)
220 " ")))))))
221 "" (nth 3 lob-info) (nth 2 lob-info))
222 'lob))
223 (rep (org-fill-template
224 org-babel-exp-call-line-template
225 `(("line" . ,(nth 0 lob-info))))))
226 ;; If replacement is empty, completely remove the
227 ;; object/element, including any extra white space
228 ;; that might have been created when including
229 ;; results.
230 (if (equal rep "")
231 (delete-region
232 begin
233 (progn (goto-char end)
234 (if (not (eq type 'babel-call))
235 (progn (skip-chars-forward " \t") (point))
236 (skip-chars-forward " \r\t\n")
237 (line-beginning-position))))
238 ;; Otherwise, preserve following white
239 ;; spaces/newlines and then, insert replacement
240 ;; string.
241 (goto-char begin)
242 (delete-region begin end)
243 (insert rep))))
244 (src-block
245 (let* ((match-start (copy-marker (match-beginning 0)))
246 (ind (org-get-indentation))
247 (headers
248 (cons
249 (org-element-property :language element)
250 (let ((params (org-element-property :parameters
251 element)))
252 (and params (org-split-string params "[ \t]+"))))))
253 ;; Take care of matched block: compute replacement
254 ;; string. In particular, a nil REPLACEMENT means
255 ;; the block should be left as-is while an empty
256 ;; string should remove the block.
257 (let ((replacement
258 (progn (goto-char match-start)
259 (org-babel-exp-src-block headers))))
260 (cond ((not replacement) (goto-char end))
261 ((equal replacement "")
262 (goto-char end)
263 (skip-chars-forward " \r\t\n")
264 (beginning-of-line)
265 (delete-region begin (point)))
267 (goto-char match-start)
268 (delete-region (point)
269 (save-excursion (goto-char end)
270 (line-end-position)))
271 (insert replacement)
272 (if (or org-src-preserve-indentation
273 (org-element-property :preserve-indent
274 element))
275 ;; Indent only the code block markers.
276 (save-excursion (skip-chars-backward " \r\t\n")
277 (indent-line-to ind)
278 (goto-char match-start)
279 (indent-line-to ind))
280 ;; Indent everything.
281 (indent-rigidly match-start (point) ind)))))
282 (set-marker match-start nil))))
283 (set-marker begin nil)
284 (set-marker end nil))))))))
286 (defun org-babel-in-example-or-verbatim ()
287 "Return true if point is in example or verbatim code.
288 Example and verbatim code include escaped portions of
289 an org-mode buffer code that should be treated as normal
290 org-mode text."
291 (or (save-match-data
292 (save-excursion
293 (goto-char (point-at-bol))
294 (looking-at "[ \t]*:[ \t]")))
295 (org-in-verbatim-emphasis)
296 (org-in-block-p org-list-forbidden-blocks)
297 (org-between-regexps-p "^[ \t]*#\\+begin_src" "^[ \t]*#\\+end_src")))
299 (defun org-babel-exp-do-export (info type &optional hash)
300 "Return a string with the exported content of a code block.
301 The function respects the value of the :exports header argument."
302 (let ((silently (lambda () (let ((session (cdr (assoc :session (nth 2 info)))))
303 (when (not (and session (equal "none" session)))
304 (org-babel-exp-results info type 'silent)))))
305 (clean (lambda () (unless (eq type 'inline) (org-babel-remove-result info)))))
306 (case (intern (or (cdr (assoc :exports (nth 2 info))) "code"))
307 ('none (funcall silently) (funcall clean) "")
308 ('code (funcall silently) (funcall clean) (org-babel-exp-code info))
309 ('results (org-babel-exp-results info type nil hash) "")
310 ('both (org-babel-exp-results info type nil hash)
311 (org-babel-exp-code info)))))
313 (defcustom org-babel-exp-code-template
314 "#+BEGIN_SRC %lang%switches%flags\n%body\n#+END_SRC"
315 "Template used to export the body of code blocks.
316 This template may be customized to include additional information
317 such as the code block name, or the values of particular header
318 arguments. The template is filled out using `org-fill-template',
319 and the following %keys may be used.
321 lang ------ the language of the code block
322 name ------ the name of the code block
323 body ------ the body of the code block
324 switches -- the switches associated to the code block
325 flags ----- the flags passed to the code block
327 In addition to the keys mentioned above, every header argument
328 defined for the code block may be used as a key and will be
329 replaced with its value."
330 :group 'org-babel
331 :type 'string)
333 (defun org-babel-exp-code (info)
334 "Return the original code block formatted for export."
335 (setf (nth 1 info)
336 (if (string= "strip-export" (cdr (assoc :noweb (nth 2 info))))
337 (replace-regexp-in-string
338 (org-babel-noweb-wrap) "" (nth 1 info))
339 (if (org-babel-noweb-p (nth 2 info) :export)
340 (org-babel-expand-noweb-references
341 info org-babel-exp-reference-buffer)
342 (nth 1 info))))
343 (org-fill-template
344 org-babel-exp-code-template
345 `(("lang" . ,(nth 0 info))
346 ("body" . ,(org-escape-code-in-string (nth 1 info)))
347 ("switches" . ,(let ((f (nth 3 info)))
348 (and (org-string-nw-p f) (concat " " f))))
349 ("flags" . ,(let ((f (assq :flags (nth 2 info))))
350 (and f (concat " " (cdr f)))))
351 ,@(mapcar (lambda (pair)
352 (cons (substring (symbol-name (car pair)) 1)
353 (format "%S" (cdr pair))))
354 (nth 2 info))
355 ("name" . ,(or (nth 4 info) "")))))
357 (defun org-babel-exp-results (info type &optional silent hash)
358 "Evaluate and return the results of the current code block for export.
359 Results are prepared in a manner suitable for export by org-mode.
360 This function is called by `org-babel-exp-do-export'. The code
361 block will be evaluated. Optional argument SILENT can be used to
362 inhibit insertion of results into the buffer."
363 (when (and (or (eq org-export-babel-evaluate t)
364 (and (eq type 'inline)
365 (eq org-export-babel-evaluate 'inline-only)))
366 (not (and hash (equal hash (org-babel-current-result-hash)))))
367 (let ((lang (nth 0 info))
368 (body (if (org-babel-noweb-p (nth 2 info) :eval)
369 (org-babel-expand-noweb-references
370 info org-babel-exp-reference-buffer)
371 (nth 1 info)))
372 (info (copy-sequence info))
373 (org-babel-current-src-block-location (point-marker)))
374 ;; skip code blocks which we can't evaluate
375 (when (fboundp (intern (concat "org-babel-execute:" lang)))
376 (org-babel-eval-wipe-error-buffer)
377 (prog1 nil
378 (setf (nth 1 info) body)
379 (setf (nth 2 info)
380 (org-babel-exp-in-export-file lang
381 (org-babel-process-params
382 (org-babel-merge-params
383 (nth 2 info)
384 `((:results . ,(if silent "silent" "replace")))))))
385 (cond
386 ((equal type 'block)
387 (org-babel-execute-src-block nil info))
388 ((equal type 'inline)
389 ;; position the point on the inline source block allowing
390 ;; `org-babel-insert-result' to check that the block is
391 ;; inline
392 (re-search-backward "[ \f\t\n\r\v]" nil t)
393 (re-search-forward org-babel-inline-src-block-regexp nil t)
394 (re-search-backward "src_" nil t)
395 (org-babel-execute-src-block nil info))
396 ((equal type 'lob)
397 (save-excursion
398 (re-search-backward org-babel-lob-one-liner-regexp nil t)
399 (let (org-confirm-babel-evaluate)
400 (org-babel-execute-src-block nil info))))))))))
403 (provide 'ob-exp)
405 ;;; ob-exp.el ends here