1 ;;; ob-exp.el --- Exportation of org-babel source blocks
3 ;; Copyright (C) 2009-2012 Free Software Foundation, Inc.
5 ;; Authors: Eric Schulte
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/>.
30 (defvar obe-marker nil
)
31 (defvar org-current-export-file
)
32 (defvar org-babel-lob-one-liner-regexp
)
33 (defvar org-babel-ref-split-regexp
)
34 (defvar org-list-forbidden-blocks
)
36 (declare-function org-babel-lob-get-info
"ob-lob" ())
37 (declare-function org-babel-eval-wipe-error-buffer
"ob-eval" ())
38 (declare-function org-between-regexps-p
"org"
39 (start-re end-re
&optional lim-up lim-down
))
40 (declare-function org-get-indentation
"org" (&optional line
))
41 (declare-function org-heading-components
"org" ())
42 (declare-function org-in-block-p
"org" (names))
43 (declare-function org-in-verbatim-emphasis
"org" ())
44 (declare-function org-link-search
"org" (s &optional type avoid-pos stealth
))
45 (declare-function org-fill-template
"org" (template alist
))
46 (declare-function org-split-string
"org" (string &optional separators
))
47 (declare-function org-element-at-point
"org-element" (&optional keep-trail
))
48 (declare-function org-element-context
"org-element" ())
49 (declare-function org-element-property
"org-element" (property element
))
50 (declare-function org-element-type
"org-element" (element))
53 (defcustom org-export-babel-evaluate t
54 "Switch controlling code evaluation during export.
55 When set to nil no code will be evaluated as part of the export
60 (put 'org-export-babel-evaluate
'safe-local-variable
(lambda (x) (eq x nil
)))
62 (defun org-babel-exp-get-export-buffer ()
63 "Return the current export buffer if possible."
65 ((bufferp org-current-export-file
) org-current-export-file
)
66 (org-current-export-file (get-file-buffer org-current-export-file
))
68 (error "Requested export buffer when `org-current-export-file' is nil"))))
70 (defmacro org-babel-exp-in-export-file
(lang &rest body
)
72 `(let* ((lang-headers (intern (concat "org-babel-default-header-args:" ,lang
)))
73 (heading (nth 4 (ignore-errors (org-heading-components))))
74 (export-buffer (current-buffer))
75 (original-buffer (org-babel-exp-get-export-buffer)) results
)
77 ;; resolve parameters in the original file so that
78 ;; headline and file-wide parameters are included, attempt
79 ;; to go to the same heading in the original file
80 (set-buffer original-buffer
)
84 (let ((org-link-search-inhibit-query t
))
85 (org-link-search heading
))
87 (goto-char (point-min))
88 (re-search-forward (regexp-quote heading
) nil t
)))))
89 (setq results
,@body
))
90 (set-buffer export-buffer
)
92 (def-edebug-spec org-babel-exp-in-export-file
(form body
))
94 (defun org-babel-exp-src-block (&rest headers
)
95 "Process source block for export.
96 Depending on the 'export' headers argument in replace the source
99 both ---- display the code and the results
101 code ---- the default, display the code inside the block but do
104 results - just like none only the block is run on export ensuring
105 that it's results are present in the org-mode buffer
107 none ----- do not display either code or results upon export
109 Assume point is at the beginning of block's starting line."
111 (unless noninteractive
(message "org-babel-exp processing..."))
113 (let* ((info (org-babel-get-src-block-info 'light
))
115 (raw-params (nth 2 info
)) hash
)
116 ;; bail if we couldn't get any info from the block
118 ;; if we're actually going to need the parameters
119 (when (member (cdr (assoc :exports
(nth 2 info
))) '("both" "results"))
120 (org-babel-exp-in-export-file lang
122 (org-babel-process-params
123 (org-babel-merge-params
124 org-babel-default-header-args
125 (org-babel-params-from-properties lang
)
126 (if (boundp lang-headers
) (eval lang-headers
) nil
)
128 (setf hash
(org-babel-sha1-hash info
)))
129 (org-babel-exp-do-export info
'block hash
)))))
131 (defcustom org-babel-exp-call-line-template
133 "Template used to export call lines.
134 This template may be customized to include the call line name
135 with any export markup. The template is filled out using
136 `org-fill-template', and the following %keys may be used.
140 An example value would be \"\\n: call: %line\" to export the call line
141 wrapped in a verbatim environment.
143 Note: the results are inserted separately after the contents of
148 (defvar org-babel-default-lob-header-args
)
149 (defun org-babel-exp-non-block-elements (start end
)
150 "Process inline source and call lines between START and END for export."
154 (unless (markerp end
)
155 (let ((m (make-marker)))
156 (set-marker m end
(current-buffer))
158 (let ((rx (concat "\\(?:" org-babel-inline-src-block-regexp
159 "\\|" org-babel-lob-one-liner-regexp
"\\)")))
160 (while (re-search-forward rx end t
)
162 (let* ((element (save-match-data (org-element-context)))
163 (type (org-element-type element
)))
164 (when (memq type
'(babel-call inline-babel-call inline-src-block
))
165 (let ((beg-el (org-element-property :begin element
))
166 (end-el (org-element-property :end element
)))
169 (let* ((info (org-babel-parse-inline-src-block-match))
170 (params (nth 2 info
)))
172 (if (and (cdr (assoc :noweb params
))
173 (string= "yes" (cdr (assoc :noweb params
))))
174 (org-babel-expand-noweb-references
175 info
(org-babel-exp-get-export-buffer))
178 (let ((replacement (org-babel-exp-do-export info
'inline
)))
179 (if (equal replacement
"")
180 ;; Replacement code is empty: completely
181 ;; remove inline src block, including extra
182 ;; white space that might have been created
183 ;; when inserting results.
184 (delete-region beg-el
185 (progn (goto-char end-el
)
186 (skip-chars-forward " \t")
188 ;; Otherwise: remove inline src block but
189 ;; preserve following white spaces. Then
191 (delete-region beg-el
192 (progn (goto-char end-el
)
193 (skip-chars-backward " \t")
195 (insert replacement
)))))
196 ((babel-call inline-babel-call
)
197 (let* ((lob-info (org-babel-lob-get-info))
199 (org-babel-exp-do-export
200 (list "emacs-lisp" "results"
201 (org-babel-merge-params
202 org-babel-default-header-args
203 org-babel-default-lob-header-args
204 (org-babel-params-from-properties)
205 (org-babel-parse-header-arguments
207 (concat ":var results="
211 "" nil
(car (last lob-info
)))
213 (rep (org-fill-template
214 org-babel-exp-call-line-template
215 `(("line" .
,(nth 0 lob-info
))))))
216 ;; If replacement is empty, completely remove the
217 ;; object/element, including any extra white space
218 ;; that might have been created when including
223 (progn (goto-char end-el
)
224 (if (not (eq type
'babel-call
))
225 (progn (skip-chars-forward " \t") (point))
226 (skip-chars-forward " \r\t\n")
227 (line-beginning-position))))
228 ;; Otherwise, preserve following white
229 ;; spaces/newlines and then, insert replacement
232 (delete-region beg-el
233 (progn (goto-char end-el
)
234 (skip-chars-backward " \r\t\n")
236 (insert rep
)))))))))))))
238 (defvar org-src-preserve-indentation
) ; From org-src.el
239 (defun org-export-blocks-preprocess ()
240 "Execute all blocks in visible part of buffer."
242 (save-window-excursion
243 (let ((case-fold-search t
)
246 (while (re-search-forward "^[ \t]*#\\+BEGIN_SRC" nil t
)
247 (let ((element (save-match-data (org-element-at-point))))
248 (when (eq (org-element-type element
) 'src-block
)
249 (let* ((match-start (copy-marker (match-beginning 0)))
250 (begin (copy-marker (org-element-property :begin element
)))
251 ;; Make sure we don't remove any blank lines after
252 ;; the block when replacing it.
253 (block-end (save-excursion
254 (goto-char (org-element-property :end element
))
255 (skip-chars-backward " \r\t\n")
256 (copy-marker (line-end-position))))
257 (ind (org-get-indentation))
260 (org-element-property :language element
)
261 (let ((params (org-element-property :parameters element
)))
262 (and params
(org-split-string params
"[ \t]+")))))
264 (or org-src-preserve-indentation
265 (org-element-property :preserve-indent element
))))
266 ;; Execute all non-block elements between POS and
268 (org-babel-exp-non-block-elements pos begin
)
269 ;; Take care of matched block: compute replacement
270 ;; string. In particular, a nil REPLACEMENT means the
271 ;; block should be left as-is while an empty string
272 ;; should remove the block.
273 (let ((replacement (progn (goto-char match-start
)
274 (org-babel-exp-src-block headers
))))
275 (cond ((not replacement
) (goto-char block-end
))
276 ((equal replacement
"")
278 (progn (goto-char block-end
)
279 (skip-chars-forward " \r\t\n")
281 (line-beginning-position)))))
283 (goto-char match-start
)
284 (delete-region (point) block-end
)
287 ;; Indent only the code block markers.
288 (save-excursion (skip-chars-backward " \r\t\n")
290 (goto-char match-start
)
291 (indent-line-to ind
))
292 ;; Indent everything.
293 (indent-code-rigidly match-start
(point) ind
)))))
294 (setq pos
(line-beginning-position))
296 (set-marker match-start nil
)
297 (set-marker begin nil
)
298 (set-marker block-end nil
)))))
299 ;; Eventually execute all non-block Babel elements between last
300 ;; src-block and end of buffer.
301 (org-babel-exp-non-block-elements pos
(point-max)))))
303 (defun org-babel-in-example-or-verbatim ()
304 "Return true if point is in example or verbatim code.
305 Example and verbatim code include escaped portions of
306 an org-mode buffer code that should be treated as normal
310 (goto-char (point-at-bol))
311 (looking-at "[ \t]*:[ \t]")))
312 (org-in-verbatim-emphasis)
313 (org-in-block-p org-list-forbidden-blocks
)
314 (org-between-regexps-p "^[ \t]*#\\+begin_src" "^[ \t]*#\\+end_src")))
316 (defun org-babel-exp-do-export (info type
&optional hash
)
317 "Return a string with the exported content of a code block.
318 The function respects the value of the :exports header argument."
319 (let ((silently (lambda () (let ((session (cdr (assoc :session
(nth 2 info
)))))
320 (when (not (and session
(equal "none" session
)))
321 (org-babel-exp-results info type
'silent
)))))
322 (clean (lambda () (unless (eq type
'inline
) (org-babel-remove-result info
)))))
323 (case (intern (or (cdr (assoc :exports
(nth 2 info
))) "code"))
324 ('none
(funcall silently
) (funcall clean
) "")
325 ('code
(funcall silently
) (funcall clean
) (org-babel-exp-code info
))
326 ('results
(org-babel-exp-results info type nil hash
) "")
327 ('both
(org-babel-exp-results info type nil hash
)
328 (org-babel-exp-code info
)))))
330 (defcustom org-babel-exp-code-template
331 "#+BEGIN_SRC %lang%flags\n%body\n#+END_SRC"
332 "Template used to export the body of code blocks.
333 This template may be customized to include additional information
334 such as the code block name, or the values of particular header
335 arguments. The template is filled out using `org-fill-template',
336 and the following %keys may be used.
338 lang ------ the language of the code block
339 name ------ the name of the code block
340 body ------ the body of the code block
341 flags ----- the flags passed to the code block
343 In addition to the keys mentioned above, every header argument
344 defined for the code block may be used as a key and will be
345 replaced with its value."
349 (defun org-babel-exp-code (info)
350 "Return the original code block formatted for export."
352 (if (string= "strip-export" (cdr (assoc :noweb
(nth 2 info
))))
353 (replace-regexp-in-string
354 (org-babel-noweb-wrap) "" (nth 1 info
))
355 (if (org-babel-noweb-p (nth 2 info
) :export
)
356 (org-babel-expand-noweb-references
357 info
(org-babel-exp-get-export-buffer))
360 org-babel-exp-code-template
361 `(("lang" .
,(nth 0 info
))
362 ("body" .
,(nth 1 info
))
363 ,@(mapcar (lambda (pair)
364 (cons (substring (symbol-name (car pair
)) 1)
365 (format "%S" (cdr pair
))))
367 ("flags" .
,((lambda (f) (when f
(concat " " f
))) (nth 3 info
)))
368 ("name" .
,(or (nth 4 info
) "")))))
370 (defun org-babel-exp-results (info type
&optional silent hash
)
371 "Evaluate and return the results of the current code block for export.
372 Results are prepared in a manner suitable for export by org-mode.
373 This function is called by `org-babel-exp-do-export'. The code
374 block will be evaluated. Optional argument SILENT can be used to
375 inhibit insertion of results into the buffer."
376 (when (and org-export-babel-evaluate
377 (not (and hash
(equal hash
(org-babel-current-result-hash)))))
378 (let ((lang (nth 0 info
))
379 (body (if (org-babel-noweb-p (nth 2 info
) :eval
)
380 (org-babel-expand-noweb-references
381 info
(org-babel-exp-get-export-buffer))
383 (info (copy-sequence info
)))
384 ;; skip code blocks which we can't evaluate
385 (when (fboundp (intern (concat "org-babel-execute:" lang
)))
386 (org-babel-eval-wipe-error-buffer)
388 (setf (nth 1 info
) body
)
390 (org-babel-exp-in-export-file lang
391 (org-babel-process-params
392 (org-babel-merge-params
394 `((:results .
,(if silent
"silent" "replace")))))))
397 (org-babel-execute-src-block nil info
))
398 ((equal type
'inline
)
399 ;; position the point on the inline source block allowing
400 ;; `org-babel-insert-result' to check that the block is
402 (re-search-backward "[ \f\t\n\r\v]" nil t
)
403 (re-search-forward org-babel-inline-src-block-regexp nil t
)
404 (re-search-backward "src_" nil t
)
405 (org-babel-execute-src-block nil info
))
408 (re-search-backward org-babel-lob-one-liner-regexp nil t
)
409 (org-babel-execute-src-block nil info
)))))))))
414 ;;; ob-exp.el ends here