1 ;;; ob-exp.el --- Exportation of org-babel source blocks
3 ;; Copyright (C) 2009-2016 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/>.
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-commented-heading-p
"org" (&optional no-inheritance
))
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 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" ())
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))
51 (declare-function org-id-get
"org-id" (&optional pom create prefix
))
52 (declare-function org-escape-code-in-string
"org-src" (s))
54 (defcustom org-export-babel-evaluate t
55 "Switch controlling code evaluation during export.
56 When set to nil no code will be evaluated as part of the export
57 process. When set to `inline-only', only inline code blocks will
61 :type
'(choice (const :tag
"Never" nil
)
62 (const :tag
"Only inline code" inline-only
)
63 (const :tag
"Always" t
)))
64 (put 'org-export-babel-evaluate
'safe-local-variable
(lambda (x) (eq x nil
)))
66 (defvar org-link-search-inhibit-query
)
67 (defmacro org-babel-exp-in-export-file
(lang &rest body
)
69 `(let* ((lang-headers (intern (concat "org-babel-default-header-args:" ,lang
)))
70 (heading-query (or (org-id-get)
71 ;; CUSTOM_IDs don't work, maybe they are
72 ;; stripped, or maybe they resolve too
73 ;; late in `org-link-search'.
74 ;; (org-entry-get nil "CUSTOM_ID")
75 (nth 4 (ignore-errors (org-heading-components)))))
76 (export-buffer (current-buffer))
78 (when org-babel-exp-reference-buffer
79 ;; Resolve parameters in the original file so that headline and
80 ;; file-wide parameters are included, attempt to go to the same
81 ;; heading in the original file
82 (set-buffer org-babel-exp-reference-buffer
)
86 (let ((org-link-search-inhibit-query t
))
87 ;; TODO: When multiple headings have the same title,
88 ;; this returns the first, which is not always
89 ;; the right heading. Consider a better way to
90 ;; find the proper heading.
91 (org-link-search heading-query
))
92 (error (when heading-query
93 (goto-char (point-min))
94 (re-search-forward (regexp-quote heading-query
) nil t
)))))
95 (setq results
,@body
))
96 (set-buffer export-buffer
)
98 (def-edebug-spec org-babel-exp-in-export-file
(form body
))
100 (defun org-babel-exp-src-block (&rest headers
)
101 "Process source block for export.
102 Depending on the `export' headers argument, replace the source
103 code block like this:
105 both ---- display the code and the results
107 code ---- the default, display the code inside the block but do
110 results - just like none only the block is run on export ensuring
111 that its results are present in the org-mode buffer
113 none ---- do not display either code or results upon export
115 Assume point is at the beginning of block's starting line."
118 (let* ((info (org-babel-get-src-block-info 'light
))
119 (line (org-current-line))
121 (raw-params (nth 2 info
)) hash
)
122 ;; bail if we couldn't get any info from the block
123 (unless noninteractive
124 (message "org-babel-exp process %s at line %d..." lang line
))
126 ;; if we're actually going to need the parameters
127 (when (member (cdr (assoc :exports
(nth 2 info
))) '("both" "results"))
128 (org-babel-exp-in-export-file lang
130 (org-babel-process-params
131 (apply #'org-babel-merge-params
132 org-babel-default-header-args
133 (if (boundp lang-headers
) (eval lang-headers
) nil
)
134 (append (org-babel-params-from-properties lang
)
135 (list raw-params
))))))
136 (setf hash
(org-babel-sha1-hash info
)))
137 (org-babel-exp-do-export info
'block hash
)))))
139 (defcustom org-babel-exp-call-line-template
141 "Template used to export call lines.
142 This template may be customized to include the call line name
143 with any export markup. The template is filled out using
144 `org-fill-template', and the following %keys may be used.
148 An example value would be \"\\n: call: %line\" to export the call line
149 wrapped in a verbatim environment.
151 Note: the results are inserted separately after the contents of
156 (defvar org-babel-default-lob-header-args
)
157 (defun org-babel-exp-process-buffer (reference-buffer)
158 "Execute all Babel blocks in current buffer.
159 REFERENCE-BUFFER is the buffer containing a pristine copy of the
160 buffer being processed. It is used to properly resolve
161 references in source blocks, as modifications in current buffer
162 may make them unreachable."
164 (save-window-excursion
166 (let ((case-fold-search t
)
167 (org-babel-exp-reference-buffer reference-buffer
)
168 (regexp (concat org-babel-inline-src-block-regexp
"\\|"
169 org-babel-lob-one-liner-regexp
"\\|"
170 "^[ \t]*#\\+BEGIN_SRC")))
171 (goto-char (point-min))
172 (while (re-search-forward regexp nil t
)
173 (unless (save-match-data (org-in-commented-heading-p))
174 (let* ((element (save-excursion
175 ;; If match is inline, point is at its
176 ;; end. Move backward so
177 ;; `org-element-context' can get the
178 ;; object, not the following one.
180 (save-match-data (org-element-context))))
181 (type (org-element-type element
))
182 (begin (copy-marker (org-element-property :begin element
)))
185 (goto-char (org-element-property :end element
))
186 (skip-chars-backward " \r\t\n")
190 (let* ((head (match-beginning 0))
191 (info (append (org-babel-parse-inline-src-block-match)
192 (list nil nil head
)))
193 (params (nth 2 info
)))
195 (if (and (cdr (assoc :noweb params
))
196 (string= "yes" (cdr (assoc :noweb params
))))
197 (org-babel-expand-noweb-references
198 info org-babel-exp-reference-buffer
)
201 (let ((replacement (org-babel-exp-do-export info
'inline
)))
202 (if (equal replacement
"")
203 ;; Replacement code is empty: remove inline
204 ;; source block, including extra white space
205 ;; that might have been created when
206 ;; inserting results.
208 (progn (goto-char end
)
209 (skip-chars-forward " \t")
211 ;; Otherwise: remove inline src block but
212 ;; preserve following white spaces. Then
214 (delete-region begin end
)
215 (insert replacement
)))))
216 ((babel-call inline-babel-call
)
217 (let* ((lob-info (org-babel-lob-get-info))
219 (org-babel-exp-do-export
220 (list "emacs-lisp" "results"
221 (apply #'org-babel-merge-params
222 org-babel-default-header-args
223 org-babel-default-lob-header-args
225 (org-babel-params-from-properties)
227 (org-babel-parse-header-arguments
234 "" (nth 3 lob-info
) (nth 2 lob-info
))
236 (rep (org-fill-template
237 org-babel-exp-call-line-template
238 `(("line" .
,(nth 0 lob-info
))))))
239 ;; If replacement is empty, completely remove the
240 ;; object/element, including any extra white space
241 ;; that might have been created when including
246 (progn (goto-char end
)
247 (if (not (eq type
'babel-call
))
248 (progn (skip-chars-forward " \t") (point))
249 (skip-chars-forward " \r\t\n")
250 (line-beginning-position))))
251 ;; Otherwise, preserve following white
252 ;; spaces/newlines and then, insert replacement
255 (delete-region begin end
)
258 (let* ((match-start (copy-marker (match-beginning 0)))
259 (ind (org-get-indentation))
260 (lang (or (org-element-property :language element
)
262 "No language for src block: %s"
263 (or (org-element-property :name element
)
268 (org-element-property
269 :parameters element
)))
270 (and params
(org-split-string params
))))))
271 ;; Take care of matched block: compute replacement
272 ;; string. In particular, a nil REPLACEMENT means
273 ;; the block should be left as-is while an empty
274 ;; string should remove the block.
276 (progn (goto-char match-start
)
277 (org-babel-exp-src-block headers
))))
278 (cond ((not replacement
) (goto-char end
))
279 ((equal replacement
"")
281 (skip-chars-forward " \r\t\n")
283 (delete-region begin
(point)))
285 (goto-char match-start
)
286 (delete-region (point)
287 (save-excursion (goto-char end
)
288 (line-end-position)))
290 (if (or org-src-preserve-indentation
291 (org-element-property :preserve-indent
293 ;; Indent only the code block markers.
294 (save-excursion (skip-chars-backward " \r\t\n")
296 (goto-char match-start
)
297 (indent-line-to ind
))
298 ;; Indent everything.
299 (indent-rigidly match-start
(point) ind
)))))
300 (set-marker match-start nil
))))
301 (set-marker begin nil
)
302 (set-marker end nil
))))))))
304 (defun org-babel-in-example-or-verbatim ()
305 "Return true if point is in example or verbatim code.
306 Example and verbatim code include escaped portions of
307 an org-mode buffer code that should be treated as normal
311 (goto-char (point-at-bol))
312 (looking-at "[ \t]*:[ \t]")))
313 (org-in-verbatim-emphasis)
314 (org-in-block-p org-list-forbidden-blocks
)
315 (org-between-regexps-p "^[ \t]*#\\+begin_src" "^[ \t]*#\\+end_src")))
317 (defun org-babel-exp-do-export (info type
&optional hash
)
318 "Return a string with the exported content of a code block.
319 The function respects the value of the :exports header argument."
320 (let ((silently (lambda () (let ((session (cdr (assoc :session
(nth 2 info
)))))
321 (when (not (and session
(equal "none" session
)))
322 (org-babel-exp-results info type
'silent
)))))
323 (clean (lambda () (if (eq type
'inline
)
324 (org-babel-remove-inline-result)
325 (org-babel-remove-result info
)))))
326 (case (intern (or (cdr (assoc :exports
(nth 2 info
))) "code"))
327 ('none
(funcall silently
) (funcall clean
) "")
328 ('code
(funcall silently
) (funcall clean
) (org-babel-exp-code info type
))
329 ('results
(org-babel-exp-results info type nil hash
) "")
330 ('both
(org-babel-exp-results info type nil hash
)
331 (org-babel-exp-code info type
)))))
333 (defcustom org-babel-exp-code-template
334 "#+BEGIN_SRC %lang%switches%flags\n%body\n#+END_SRC"
335 "Template used to export the body of code blocks.
336 This template may be customized to include additional information
337 such as the code block name, or the values of particular header
338 arguments. The template is filled out using `org-fill-template',
339 and the following %keys may be used.
341 lang ------ the language of the code block
342 name ------ the name of the code block
343 body ------ the body of the code block
344 switches -- the switches associated to the code block
345 flags ----- the flags passed to the code block
347 In addition to the keys mentioned above, every header argument
348 defined for the code block may be used as a key and will be
349 replaced with its value."
353 (defcustom org-babel-exp-inline-code-template
354 "src_%lang[%switches%flags]{%body}"
355 "Template used to export the body of inline code blocks.
356 This template may be customized to include additional information
357 such as the code block name, or the values of particular header
358 arguments. The template is filled out using `org-fill-template',
359 and the following %keys may be used.
361 lang ------ the language of the code block
362 name ------ the name of the code block
363 body ------ the body of the code block
364 switches -- the switches associated to the code block
365 flags ----- the flags passed to the code block
367 In addition to the keys mentioned above, every header argument
368 defined for the code block may be used as a key and will be
369 replaced with its value."
373 :package-version
'(Org .
"8.3"))
375 (defun org-babel-exp-code (info type
)
376 "Return the original code block formatted for export."
378 (if (string= "strip-export" (cdr (assoc :noweb
(nth 2 info
))))
379 (replace-regexp-in-string
380 (org-babel-noweb-wrap) "" (nth 1 info
))
381 (if (org-babel-noweb-p (nth 2 info
) :export
)
382 (org-babel-expand-noweb-references
383 info org-babel-exp-reference-buffer
)
386 (if (eq type
'inline
)
387 org-babel-exp-inline-code-template
388 org-babel-exp-code-template
)
389 `(("lang" .
,(nth 0 info
))
390 ("body" .
,(org-escape-code-in-string (nth 1 info
)))
391 ("switches" .
,(let ((f (nth 3 info
)))
392 (and (org-string-nw-p f
) (concat " " f
))))
393 ("flags" .
,(let ((f (assq :flags
(nth 2 info
))))
394 (and f
(concat " " (cdr f
)))))
395 ,@(mapcar (lambda (pair)
396 (cons (substring (symbol-name (car pair
)) 1)
397 (format "%S" (cdr pair
))))
399 ("name" .
,(or (nth 4 info
) "")))))
401 (defun org-babel-exp-results (info type
&optional silent hash
)
402 "Evaluate and return the results of the current code block for export.
403 Results are prepared in a manner suitable for export by org-mode.
404 This function is called by `org-babel-exp-do-export'. The code
405 block will be evaluated. Optional argument SILENT can be used to
406 inhibit insertion of results into the buffer."
407 (when (and (or (eq org-export-babel-evaluate t
)
408 (and (eq type
'inline
)
409 (eq org-export-babel-evaluate
'inline-only
)))
410 (not (and hash
(equal hash
(org-babel-current-result-hash)))))
411 (let ((lang (nth 0 info
))
412 (body (if (org-babel-noweb-p (nth 2 info
) :eval
)
413 (org-babel-expand-noweb-references
414 info org-babel-exp-reference-buffer
)
416 (info (copy-sequence info
))
417 (org-babel-current-src-block-location (point-marker)))
418 ;; skip code blocks which we can't evaluate
419 (when (fboundp (intern (concat "org-babel-execute:" lang
)))
420 (org-babel-eval-wipe-error-buffer)
422 (setf (nth 1 info
) body
)
424 (org-babel-exp-in-export-file lang
425 (org-babel-process-params
426 (org-babel-merge-params
428 `((:results .
,(if silent
"silent" "replace")))))))
431 (org-babel-execute-src-block nil info
))
432 ((equal type
'inline
)
433 ;; position the point on the inline source block allowing
434 ;; `org-babel-insert-result' to check that the block is
436 (re-search-backward "[ \f\t\n\r\v]" nil t
)
437 (re-search-forward org-babel-inline-src-block-regexp nil t
)
438 (re-search-backward "src_" nil t
)
439 (org-babel-execute-src-block nil info
))
442 (re-search-backward org-babel-lob-one-liner-regexp nil t
)
443 (let (org-confirm-babel-evaluate)
444 (org-babel-execute-src-block nil info
))))))))))
449 ;;; ob-exp.el ends here