1 ;;; ob-exp.el --- Exportation of Babel Source Blocks -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2009-2017 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/>.
28 (declare-function org-babel-lob-get-info
"ob-lob" (&optional datum
))
29 (declare-function org-element-at-point
"org-element" ())
30 (declare-function org-element-context
"org-element" (&optional element
))
31 (declare-function org-element-property
"org-element" (property element
))
32 (declare-function org-element-type
"org-element" (element))
33 (declare-function org-escape-code-in-string
"org-src" (s))
34 (declare-function org-export-copy-buffer
"ox" ())
35 (declare-function org-fill-template
"org" (template alist
))
36 (declare-function org-get-indentation
"org" (&optional line
))
37 (declare-function org-in-commented-heading-p
"org" (&optional no-inheritance
))
39 (defvar org-src-preserve-indentation
)
41 (defcustom org-export-babel-evaluate t
42 "Switch controlling code evaluation during export.
43 When set to nil no code will be evaluated as part of the export
44 process and no header arguments will be obeyed. When set to
45 `inline-only', only inline code blocks will be executed. Users
46 who wish to avoid evaluating code on export should use the header
47 argument `:eval never-export'."
50 :type
'(choice (const :tag
"Never" nil
)
51 (const :tag
"Only inline code" inline-only
)
52 (const :tag
"Always" t
)))
53 (put 'org-export-babel-evaluate
'safe-local-variable
#'null
)
55 (defmacro org-babel-exp--at-source
(&rest body
)
56 "Evaluate BODY at the source of the Babel block at point.
57 Source is located in `org-babel-exp-reference-buffer'. The value
58 returned is the value of the last form in BODY. Assume that
59 point is at the beginning of the Babel block."
60 (declare (indent 1) (debug body
))
61 `(let ((source (get-text-property (point) 'org-reference
)))
62 (with-current-buffer org-babel-exp-reference-buffer
67 (defun org-babel-exp-src-block ()
68 "Process source block for export.
69 Depending on the \":export\" header argument, replace the source
72 both ---- display the code and the results
74 code ---- the default, display the code inside the block but do
77 results - just like none only the block is run on export ensuring
78 that its results are present in the Org mode buffer
80 none ---- do not display either code or results upon export
82 Assume point is at block opening line."
85 (let* ((info (org-babel-get-src-block-info 'light
))
87 (raw-params (nth 2 info
))
89 ;; bail if we couldn't get any info from the block
90 (unless noninteractive
91 (message "org-babel-exp process %s at position %d..."
93 (line-beginning-position)))
95 ;; if we're actually going to need the parameters
96 (when (member (cdr (assq :exports
(nth 2 info
))) '("both" "results"))
97 (let ((lang-headers (intern (concat "org-babel-default-header-args:"
99 (org-babel-exp--at-source
101 (org-babel-process-params
102 (apply #'org-babel-merge-params
103 org-babel-default-header-args
104 (and (boundp lang-headers
)
105 (symbol-value lang-headers
))
106 (append (org-babel-params-from-properties lang
)
107 (list raw-params
)))))))
108 (setf hash
(org-babel-sha1-hash info
)))
109 (org-babel-exp-do-export info
'block hash
)))))
111 (defcustom org-babel-exp-call-line-template
113 "Template used to export call lines.
114 This template may be customized to include the call line name
115 with any export markup. The template is filled out using
116 `org-fill-template', and the following %keys may be used.
120 An example value would be \"\\n: call: %line\" to export the call line
121 wrapped in a verbatim environment.
123 Note: the results are inserted separately after the contents of
128 (defun org-babel-exp-process-buffer ()
129 "Execute all Babel blocks in current buffer."
131 (when org-export-babel-evaluate
132 (save-window-excursion
133 (let ((case-fold-search t
)
134 (regexp (if (eq org-export-babel-evaluate
'inline-only
)
136 "\\(call\\|src\\)_\\|^[ \t]*#\\+\\(BEGIN_SRC\\|CALL:\\)"))
137 ;; Get a pristine copy of current buffer so Babel
138 ;; references are properly resolved and source block
139 ;; context is preserved.
140 (org-babel-exp-reference-buffer (org-export-copy-buffer)))
143 ;; First attach to every source block their original
144 ;; position, so that they can be retrieved within
145 ;; `org-babel-exp-reference-buffer', even after heavy
146 ;; modifications on current buffer.
148 ;; False positives are harmless, so we don't check if
149 ;; we're really at some Babel object. Moreover,
150 ;; `line-end-position' ensures that we propertize
151 ;; a noticeable part of the object, without affecting
152 ;; multiple objects on the same line.
153 (goto-char (point-min))
154 (while (re-search-forward regexp nil t
)
155 (let ((s (match-beginning 0)))
156 (put-text-property s
(line-end-position) 'org-reference s
)))
157 ;; Evaluate from top to bottom every Babel block
159 (goto-char (point-min))
160 (while (re-search-forward regexp nil t
)
161 (unless (save-match-data (org-in-commented-heading-p))
162 (let* ((object?
(match-end 1))
163 (element (save-match-data
164 (if object?
(org-element-context)
165 ;; No deep inspection if we're
166 ;; just looking for an element.
167 (org-element-at-point))))
169 (pcase (org-element-type element
)
170 ;; Discard block elements if we're looking
171 ;; for inline objects. False results
172 ;; happen when, e.g., "call_" syntax is
173 ;; located within affiliated keywords:
177 ((and (or `babel-call
`src-block
) (guard object?
))
181 (copy-marker (org-element-property :begin element
)))
185 (goto-char (org-element-property :end element
))
186 (skip-chars-backward " \r\t\n")
191 (org-babel-get-src-block-info nil element
))
192 (params (nth 2 info
)))
194 (if (and (cdr (assq :noweb params
))
196 (cdr (assq :noweb params
))))
197 (org-babel-expand-noweb-references
198 info org-babel-exp-reference-buffer
)
202 (org-babel-exp-do-export info
'inline
)))
203 (if (equal replacement
"")
204 ;; Replacement code is empty: remove
205 ;; inline source block, including extra
206 ;; white space that might have been
207 ;; created when inserting results.
209 (progn (goto-char end
)
210 (skip-chars-forward " \t")
212 ;; Otherwise: remove inline src block but
213 ;; preserve following white spaces. Then
215 (delete-region begin end
)
216 (insert replacement
)))))
217 ((or `babel-call
`inline-babel-call
)
218 (org-babel-exp-do-export (org-babel-lob-get-info element
)
222 org-babel-exp-call-line-template
224 ,(org-element-property :value element
))))))
225 ;; If replacement is empty, completely remove
226 ;; the object/element, including any extra
227 ;; white space that might have been created
228 ;; when including results.
232 (progn (goto-char end
)
233 (if (not (eq type
'babel-call
))
234 (progn (skip-chars-forward " \t")
236 (skip-chars-forward " \r\t\n")
237 (line-beginning-position))))
238 ;; Otherwise, preserve trailing
239 ;; spaces/newlines and then, insert
240 ;; replacement string.
242 (delete-region begin end
)
245 (let ((match-start (copy-marker (match-beginning 0)))
246 (ind (org-get-indentation)))
247 ;; Take care of matched block: compute
248 ;; replacement string. In particular, a nil
249 ;; REPLACEMENT means the block is left as-is
250 ;; while an empty string removes the block.
252 (progn (goto-char match-start
)
253 (org-babel-exp-src-block))))
254 (cond ((not replacement
) (goto-char end
))
255 ((equal replacement
"")
257 (skip-chars-forward " \r\t\n")
259 (delete-region begin
(point)))
261 (goto-char match-start
)
262 (delete-region (point)
265 (line-end-position)))
267 (if (or org-src-preserve-indentation
268 (org-element-property
269 :preserve-indent element
))
270 ;; Indent only code block
273 (skip-chars-backward " \r\t\n")
275 (goto-char match-start
)
276 (indent-line-to ind
))
277 ;; Indent everything.
279 match-start
(point) ind
)))))
280 (set-marker match-start nil
))))
281 (set-marker begin nil
)
282 (set-marker end nil
)))))
283 (kill-buffer org-babel-exp-reference-buffer
)
284 (remove-text-properties (point-min) (point-max) '(org-reference)))))))
286 (defun org-babel-exp-do-export (info type
&optional hash
)
287 "Return a string with the exported content of a code block.
288 The function respects the value of the :exports header argument."
289 (let ((silently (lambda () (let ((session (cdr (assq :session
(nth 2 info
)))))
290 (unless (equal "none" session
)
291 (org-babel-exp-results info type
'silent
)))))
292 (clean (lambda () (if (eq type
'inline
)
293 (org-babel-remove-inline-result)
294 (org-babel-remove-result info
)))))
295 (pcase (or (cdr (assq :exports
(nth 2 info
))) "code")
296 ("none" (funcall silently
) (funcall clean
) "")
297 ("code" (funcall silently
) (funcall clean
) (org-babel-exp-code info type
))
298 ("results" (org-babel-exp-results info type nil hash
) "")
300 (org-babel-exp-results info type nil hash
)
301 (org-babel-exp-code info type
)))))
303 (defcustom org-babel-exp-code-template
304 "#+BEGIN_SRC %lang%switches%flags\n%body\n#+END_SRC"
305 "Template used to export the body of code blocks.
306 This template may be customized to include additional information
307 such as the code block name, or the values of particular header
308 arguments. The template is filled out using `org-fill-template',
309 and the following %keys may be used.
311 lang ------ the language of the code block
312 name ------ the name of the code block
313 body ------ the body of the code block
314 switches -- the switches associated to the code block
315 flags ----- the flags passed to the code block
317 In addition to the keys mentioned above, every header argument
318 defined for the code block may be used as a key and will be
319 replaced with its value."
323 (defcustom org-babel-exp-inline-code-template
324 "src_%lang[%switches%flags]{%body}"
325 "Template used to export the body of inline code blocks.
326 This template may be customized to include additional information
327 such as the code block name, or the values of particular header
328 arguments. The template is filled out using `org-fill-template',
329 and the following %keys may be used.
331 lang ------ the language of the code block
332 name ------ the name of the code block
333 body ------ the body of the code block
334 switches -- the switches associated to the code block
335 flags ----- the flags passed to the code block
337 In addition to the keys mentioned above, every header argument
338 defined for the code block may be used as a key and will be
339 replaced with its value."
343 :package-version
'(Org .
"8.3"))
345 (defun org-babel-exp-code (info type
)
346 "Return the original code block formatted for export."
348 (if (string= "strip-export" (cdr (assq :noweb
(nth 2 info
))))
349 (replace-regexp-in-string
350 (org-babel-noweb-wrap) "" (nth 1 info
))
351 (if (org-babel-noweb-p (nth 2 info
) :export
)
352 (org-babel-expand-noweb-references
353 info org-babel-exp-reference-buffer
)
356 (if (eq type
'inline
)
357 org-babel-exp-inline-code-template
358 org-babel-exp-code-template
)
359 `(("lang" .
,(nth 0 info
))
360 ("body" .
,(org-escape-code-in-string (nth 1 info
)))
361 ("switches" .
,(let ((f (nth 3 info
)))
362 (and (org-string-nw-p f
) (concat " " f
))))
363 ("flags" .
,(let ((f (assq :flags
(nth 2 info
))))
364 (and f
(concat " " (cdr f
)))))
365 ,@(mapcar (lambda (pair)
366 (cons (substring (symbol-name (car pair
)) 1)
367 (format "%S" (cdr pair
))))
369 ("name" .
,(or (nth 4 info
) "")))))
371 (defun org-babel-exp-results (info type
&optional silent hash
)
372 "Evaluate and return the results of the current code block for export.
373 Results are prepared in a manner suitable for export by Org mode.
374 This function is called by `org-babel-exp-do-export'. The code
375 block will be evaluated. Optional argument SILENT can be used to
376 inhibit insertion of results into the buffer."
377 (unless (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-reference-buffer
)
383 (info (copy-sequence info
))
384 (org-babel-current-src-block-location (point-marker)))
385 ;; Skip code blocks which we can't evaluate.
386 (when (fboundp (intern (concat "org-babel-execute:" lang
)))
387 (org-babel-eval-wipe-error-buffer)
388 (setf (nth 1 info
) body
)
390 (org-babel-exp--at-source
391 (org-babel-process-params
392 (org-babel-merge-params
394 `((:results .
,(if silent
"silent" "replace")))))))
396 (`block
(org-babel-execute-src-block nil info
))
398 ;; Position the point on the inline source block
399 ;; allowing `org-babel-insert-result' to check that the
401 (goto-char (nth 5 info
))
402 (org-babel-execute-src-block nil info
))
405 (goto-char (nth 5 info
))
406 (let (org-confirm-babel-evaluate)
407 (org-babel-execute-src-block nil info
)))))))))
412 ;;; ob-exp.el ends here