1 ;;; ob-exp.el --- Exportation of Babel Source Blocks -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2009-2018 Free Software Foundation, Inc.
5 ;; Authors: Eric Schulte
7 ;; Keywords: literate programming, reproducible research
8 ;; Homepage: https://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 <https://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-in-commented-heading-p
"org" (&optional no-inheritance
))
38 (defvar org-src-preserve-indentation
)
40 (defcustom org-export-use-babel t
41 "Switch controlling code evaluation and header processing during export.
42 When set to nil no code will be evaluated as part of the export
43 process and no header arguments will be obeyed. Users who wish
44 to avoid evaluating code on export should use the header argument
45 `:eval never-export'."
48 :type
'(choice (const :tag
"Never" nil
)
49 (const :tag
"Always" t
))
53 (defmacro org-babel-exp--at-source
(&rest body
)
54 "Evaluate BODY at the source of the Babel block at point.
55 Source is located in `org-babel-exp-reference-buffer'. The value
56 returned is the value of the last form in BODY. Assume that
57 point is at the beginning of the Babel block."
58 (declare (indent 1) (debug body
))
59 `(let ((source (get-text-property (point) 'org-reference
)))
60 ;; Source blocks created during export process (e.g., by other
61 ;; source blocks) are not referenced. In this case, do not move
63 (with-current-buffer (if source org-babel-exp-reference-buffer
66 (when source
(goto-char source
))
69 (defun org-babel-exp-src-block ()
70 "Process source block for export.
71 Depending on the \":export\" header argument, replace the source
74 both ---- display the code and the results
76 code ---- the default, display the code inside the block but do
79 results - just like none only the block is run on export ensuring
80 that its results are present in the Org mode buffer
82 none ---- do not display either code or results upon export
84 Assume point is at block opening line."
87 (let* ((info (org-babel-get-src-block-info))
89 (raw-params (nth 2 info
))
91 ;; bail if we couldn't get any info from the block
92 (unless noninteractive
93 (message "org-babel-exp process %s at position %d..."
95 (line-beginning-position)))
97 ;; if we're actually going to need the parameters
98 (when (member (cdr (assq :exports
(nth 2 info
))) '("both" "results"))
99 (let ((lang-headers (intern (concat "org-babel-default-header-args:"
101 (org-babel-exp--at-source
103 (org-babel-process-params
104 (apply #'org-babel-merge-params
105 org-babel-default-header-args
106 (and (boundp lang-headers
)
107 (symbol-value lang-headers
))
108 (append (org-babel-params-from-properties lang
)
109 (list raw-params
)))))))
110 (setf hash
(org-babel-sha1-hash info
:export
)))
111 (org-babel-exp-do-export info
'block hash
)))))
113 (defcustom org-babel-exp-call-line-template
115 "Template used to export call lines.
116 This template may be customized to include the call line name
117 with any export markup. The template is filled out using
118 `org-fill-template', and the following %keys may be used.
122 An example value would be \"\\n: call: %line\" to export the call line
123 wrapped in a verbatim environment.
125 Note: the results are inserted separately after the contents of
130 (defun org-babel-exp-process-buffer ()
131 "Execute all Babel blocks in current buffer."
133 (when org-export-use-babel
134 (save-window-excursion
135 (let ((case-fold-search t
)
136 (regexp "\\(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 source block
213 ;; but preserve following white spaces.
214 ;; Then insert value.
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 (current-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
) :export
)
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