1 ;;; ob-exp.el --- Exportation of org-babel source blocks
3 ;; Copyright (C) 2009-2014 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 org-current-export-file
)
31 (defvar org-babel-lob-one-liner-regexp
)
32 (defvar org-babel-ref-split-regexp
)
33 (defvar org-list-forbidden-blocks
)
35 (declare-function org-babel-lob-get-info
"ob-lob" ())
36 (declare-function org-babel-eval-wipe-error-buffer
"ob-eval" ())
37 (declare-function org-between-regexps-p
"org"
38 (start-re end-re
&optional lim-up lim-down
))
39 (declare-function org-get-indentation
"org" (&optional line
))
40 (declare-function org-heading-components
"org" ())
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
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 (defun org-babel-exp-get-export-buffer ()
65 "Return the current export buffer if possible."
67 ((bufferp org-current-export-file
) org-current-export-file
)
68 (org-current-export-file (get-file-buffer org-current-export-file
))
70 (error "Requested export buffer when `org-current-export-file' is nil"))))
72 (defvar org-link-search-inhibit-query
)
74 (defmacro org-babel-exp-in-export-file
(lang &rest body
)
76 `(let* ((lang-headers (intern (concat "org-babel-default-header-args:" ,lang
)))
77 (heading (nth 4 (ignore-errors (org-heading-components))))
78 (export-buffer (current-buffer))
79 (original-buffer (org-babel-exp-get-export-buffer)) results
)
81 ;; resolve parameters in the original file so that
82 ;; headline and file-wide parameters are included, attempt
83 ;; to go to the same heading in the original file
84 (set-buffer original-buffer
)
88 (let ((org-link-search-inhibit-query t
))
89 (org-link-search heading
))
91 (goto-char (point-min))
92 (re-search-forward (regexp-quote heading
) nil t
)))))
93 (setq results
,@body
))
94 (set-buffer export-buffer
)
96 (def-edebug-spec org-babel-exp-in-export-file
(form body
))
98 (defun org-babel-exp-src-block (&rest headers
)
99 "Process source block for export.
100 Depending on the 'export' headers argument, replace the source
101 code block like this:
103 both ---- display the code and the results
105 code ---- the default, display the code inside the block but do
108 results - just like none only the block is run on export ensuring
109 that it's results are present in the org-mode buffer
111 none ---- do not display either code or results upon export
113 Assume point is at the beginning of block's starting line."
116 (let* ((info (org-babel-get-src-block-info 'light
))
117 (line (org-current-line))
119 (raw-params (nth 2 info
)) hash
)
120 ;; bail if we couldn't get any info from the block
121 (unless noninteractive
122 (message "org-babel-exp process %s at line %d..." lang line
))
124 ;; if we're actually going to need the parameters
125 (when (member (cdr (assoc :exports
(nth 2 info
))) '("both" "results"))
126 (org-babel-exp-in-export-file lang
128 (org-babel-process-params
129 (apply #'org-babel-merge-params
130 org-babel-default-header-args
131 (if (boundp lang-headers
) (eval lang-headers
) nil
)
132 (append (org-babel-params-from-properties lang
)
133 (list raw-params
))))))
134 (setf hash
(org-babel-sha1-hash info
)))
135 (org-babel-exp-do-export info
'block hash
)))))
137 (defcustom org-babel-exp-call-line-template
139 "Template used to export call lines.
140 This template may be customized to include the call line name
141 with any export markup. The template is filled out using
142 `org-fill-template', and the following %keys may be used.
146 An example value would be \"\\n: call: %line\" to export the call line
147 wrapped in a verbatim environment.
149 Note: the results are inserted separately after the contents of
154 (defvar org-babel-default-lob-header-args
)
155 (defun org-babel-exp-process-buffer ()
156 "Execute all Babel blocks in current buffer."
158 (save-window-excursion
160 (let ((case-fold-search t
)
161 (regexp (concat org-babel-inline-src-block-regexp
"\\|"
162 org-babel-lob-one-liner-regexp
"\\|"
163 "^[ \t]*#\\+BEGIN_SRC")))
164 (goto-char (point-min))
165 (while (re-search-forward regexp nil t
)
166 (let* ((element (save-excursion
167 ;; If match is inline, point is at its
168 ;; end. Move backward so
169 ;; `org-element-context' can get the
170 ;; object, not the following one.
172 (save-match-data (org-element-context))))
173 (type (org-element-type element
))
174 (begin (copy-marker (org-element-property :begin element
)))
177 (goto-char (org-element-property :end element
))
178 (skip-chars-backward " \r\t\n")
182 (let* ((info (org-babel-parse-inline-src-block-match))
183 (params (nth 2 info
)))
185 (if (and (cdr (assoc :noweb params
))
186 (string= "yes" (cdr (assoc :noweb params
))))
187 (org-babel-expand-noweb-references
188 info
(org-babel-exp-get-export-buffer))
191 (let ((replacement (org-babel-exp-do-export info
'inline
)))
192 (if (equal replacement
"")
193 ;; Replacement code is empty: remove inline src
194 ;; block, including extra white space that
195 ;; might have been created when inserting
198 (progn (goto-char end
)
199 (skip-chars-forward " \t")
201 ;; Otherwise: remove inline src block but
202 ;; preserve following white spaces. Then insert
204 (delete-region begin end
)
205 (insert replacement
)))))
206 ((babel-call inline-babel-call
)
207 (let* ((lob-info (org-babel-lob-get-info))
209 (org-babel-exp-do-export
210 (list "emacs-lisp" "results"
211 (apply #'org-babel-merge-params
212 org-babel-default-header-args
213 org-babel-default-lob-header-args
215 (org-babel-params-from-properties)
217 (org-babel-parse-header-arguments
224 "" (nth 3 lob-info
) (nth 2 lob-info
))
226 (rep (org-fill-template
227 org-babel-exp-call-line-template
228 `(("line" .
,(nth 0 lob-info
))))))
229 ;; If replacement is empty, completely remove the
230 ;; object/element, including any extra white space
231 ;; that might have been created when including
236 (progn (goto-char end
)
237 (if (not (eq type
'babel-call
))
238 (progn (skip-chars-forward " \t") (point))
239 (skip-chars-forward " \r\t\n")
240 (line-beginning-position))))
241 ;; Otherwise, preserve following white
242 ;; spaces/newlines and then, insert replacement
245 (delete-region begin end
)
248 (let* ((match-start (copy-marker (match-beginning 0)))
249 (ind (org-get-indentation))
252 (org-element-property :language element
)
253 (let ((params (org-element-property :parameters
255 (and params
(org-split-string params
"[ \t]+"))))))
256 ;; Take care of matched block: compute replacement
257 ;; string. In particular, a nil REPLACEMENT means
258 ;; the block should be left as-is while an empty
259 ;; string should remove the block.
260 (let ((replacement (progn (goto-char match-start
)
261 (org-babel-exp-src-block headers
))))
262 (cond ((not replacement
) (goto-char end
))
263 ((equal replacement
"")
265 (skip-chars-forward " \r\t\n")
267 (delete-region begin
(point)))
269 (goto-char match-start
)
270 (delete-region (point)
271 (save-excursion (goto-char end
)
272 (line-end-position)))
274 (if (or org-src-preserve-indentation
275 (org-element-property :preserve-indent
277 ;; Indent only the code block markers.
278 (save-excursion (skip-chars-backward " \r\t\n")
280 (goto-char match-start
)
281 (indent-line-to ind
))
282 ;; Indent everything.
283 (indent-rigidly match-start
(point) ind
)))))
284 (set-marker match-start nil
))))
285 (set-marker begin nil
)
286 (set-marker end nil
)))))))
288 (defun org-babel-in-example-or-verbatim ()
289 "Return true if point is in example or verbatim code.
290 Example and verbatim code include escaped portions of
291 an org-mode buffer code that should be treated as normal
295 (goto-char (point-at-bol))
296 (looking-at "[ \t]*:[ \t]")))
297 (org-in-verbatim-emphasis)
298 (org-in-block-p org-list-forbidden-blocks
)
299 (org-between-regexps-p "^[ \t]*#\\+begin_src" "^[ \t]*#\\+end_src")))
301 (defun org-babel-exp-do-export (info type
&optional hash
)
302 "Return a string with the exported content of a code block.
303 The function respects the value of the :exports header argument."
304 (let ((silently (lambda () (let ((session (cdr (assoc :session
(nth 2 info
)))))
305 (when (not (and session
(equal "none" session
)))
306 (org-babel-exp-results info type
'silent
)))))
307 (clean (lambda () (unless (eq type
'inline
) (org-babel-remove-result info
)))))
308 (case (intern (or (cdr (assoc :exports
(nth 2 info
))) "code"))
309 ('none
(funcall silently
) (funcall clean
) "")
310 ('code
(funcall silently
) (funcall clean
) (org-babel-exp-code info
))
311 ('results
(org-babel-exp-results info type nil hash
) "")
312 ('both
(org-babel-exp-results info type nil hash
)
313 (org-babel-exp-code info
)))))
315 (defcustom org-babel-exp-code-template
316 "#+BEGIN_SRC %lang%switches%flags\n%body\n#+END_SRC"
317 "Template used to export the body of code blocks.
318 This template may be customized to include additional information
319 such as the code block name, or the values of particular header
320 arguments. The template is filled out using `org-fill-template',
321 and the following %keys may be used.
323 lang ------ the language of the code block
324 name ------ the name of the code block
325 body ------ the body of the code block
326 switches -- the switches associated to the code block
327 flags ----- the flags passed to the code block
329 In addition to the keys mentioned above, every header argument
330 defined for the code block may be used as a key and will be
331 replaced with its value."
335 (defun org-babel-exp-code (info)
336 "Return the original code block formatted for export."
338 (if (string= "strip-export" (cdr (assoc :noweb
(nth 2 info
))))
339 (replace-regexp-in-string
340 (org-babel-noweb-wrap) "" (nth 1 info
))
341 (if (org-babel-noweb-p (nth 2 info
) :export
)
342 (org-babel-expand-noweb-references
343 info
(org-babel-exp-get-export-buffer))
346 org-babel-exp-code-template
347 `(("lang" .
,(nth 0 info
))
348 ("body" .
,(org-escape-code-in-string (nth 1 info
)))
349 ("switches" .
,(let ((f (nth 3 info
)))
350 (and (org-string-nw-p f
) (concat " " f
))))
351 ("flags" .
,(let ((f (assq :flags
(nth 2 info
))))
352 (and f
(concat " " (cdr f
)))))
353 ,@(mapcar (lambda (pair)
354 (cons (substring (symbol-name (car pair
)) 1)
355 (format "%S" (cdr pair
))))
357 ("name" .
,(or (nth 4 info
) "")))))
359 (defun org-babel-exp-results (info type
&optional silent hash
)
360 "Evaluate and return the results of the current code block for export.
361 Results are prepared in a manner suitable for export by org-mode.
362 This function is called by `org-babel-exp-do-export'. The code
363 block will be evaluated. Optional argument SILENT can be used to
364 inhibit insertion of results into the buffer."
365 (when (and (or (eq org-export-babel-evaluate t
)
366 (and (eq type
'inline
)
367 (eq org-export-babel-evaluate
'inline-only
)))
368 (not (and hash
(equal hash
(org-babel-current-result-hash)))))
369 (let ((lang (nth 0 info
))
370 (body (if (org-babel-noweb-p (nth 2 info
) :eval
)
371 (org-babel-expand-noweb-references
372 info
(org-babel-exp-get-export-buffer))
374 (info (copy-sequence info
))
375 (org-babel-current-src-block-location (point-marker)))
376 ;; skip code blocks which we can't evaluate
377 (when (fboundp (intern (concat "org-babel-execute:" lang
)))
378 (org-babel-eval-wipe-error-buffer)
380 (setf (nth 1 info
) body
)
382 (org-babel-exp-in-export-file lang
383 (org-babel-process-params
384 (org-babel-merge-params
386 `((:results .
,(if silent
"silent" "replace")))))))
389 (org-babel-execute-src-block nil info
))
390 ((equal type
'inline
)
391 ;; position the point on the inline source block allowing
392 ;; `org-babel-insert-result' to check that the block is
394 (re-search-backward "[ \f\t\n\r\v]" nil t
)
395 (re-search-forward org-babel-inline-src-block-regexp nil t
)
396 (re-search-backward "src_" nil t
)
397 (org-babel-execute-src-block nil info
))
400 (re-search-backward org-babel-lob-one-liner-regexp nil t
)
401 (let (org-confirm-babel-evaluate)
402 (org-babel-execute-src-block nil info
))))))))))
407 ;;; ob-exp.el ends here