Merge branch 'maint'
[org-mode/org-tableheadings.git] / lisp / ob-exp.el
blob6aebcd5ee4740eb5fda79507f9f86ea93b4ecf67
1 ;;; ob-exp.el --- Exportation of Babel Source Blocks -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2009-2016 Free Software Foundation, Inc.
5 ;; Authors: Eric Schulte
6 ;; Dan Davison
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/>.
25 ;;; Code:
26 (require 'ob-core)
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 argumentss 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'."
48 :group 'org-babel
49 :version "24.1"
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
63 (org-with-wide-buffer
64 (goto-char source)
65 ,@body))))
67 (defun org-babel-exp-src-block ()
68 "Process source block for export.
69 Depending on the \":export\" header argument, replace the source
70 code block like this:
72 both ---- display the code and the results
74 code ---- the default, display the code inside the block but do
75 not process
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."
83 (interactive)
84 (save-excursion
85 (let* ((info (org-babel-get-src-block-info 'light))
86 (lang (nth 0 info))
87 (raw-params (nth 2 info))
88 hash)
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..."
92 lang
93 (line-beginning-position)))
94 (when info
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:"
98 lang))))
99 (org-babel-exp--at-source
100 (setf (nth 2 info)
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.
118 line --- call line
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
124 this template."
125 :group 'org-babel
126 :type 'string)
128 (defun org-babel-exp-process-buffer ()
129 "Execute all Babel blocks in current buffer."
130 (interactive)
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)
135 "\\(call\\|src\\)_"
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)))
141 (unwind-protect
142 (save-excursion
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
158 ;; encountered.
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* ((element (save-match-data (org-element-context)))
163 (type (org-element-type element))
164 (begin
165 (copy-marker (org-element-property :begin element)))
166 (end
167 (copy-marker
168 (save-excursion
169 (goto-char (org-element-property :end element))
170 (skip-chars-backward " \r\t\n")
171 (point)))))
172 (pcase type
173 (`inline-src-block
174 (let* ((info
175 (org-babel-get-src-block-info nil element))
176 (params (nth 2 info)))
177 (setf (nth 1 info)
178 (if (and (cdr (assq :noweb params))
179 (string= "yes"
180 (cdr (assq :noweb params))))
181 (org-babel-expand-noweb-references
182 info org-babel-exp-reference-buffer)
183 (nth 1 info)))
184 (goto-char begin)
185 (let ((replacement
186 (org-babel-exp-do-export info 'inline)))
187 (if (equal replacement "")
188 ;; Replacement code is empty: remove
189 ;; inline source block, including extra
190 ;; white space that might have been
191 ;; created when inserting results.
192 (delete-region begin
193 (progn (goto-char end)
194 (skip-chars-forward " \t")
195 (point)))
196 ;; Otherwise: remove inline src block but
197 ;; preserve following white spaces. Then
198 ;; insert value.
199 (delete-region begin end)
200 (insert replacement)))))
201 ((or `babel-call `inline-babel-call)
202 (org-babel-exp-do-export (org-babel-lob-get-info element)
203 'lob)
204 (let ((rep
205 (org-fill-template
206 org-babel-exp-call-line-template
207 `(("line" .
208 ,(org-element-property :value element))))))
209 ;; If replacement is empty, completely remove
210 ;; the object/element, including any extra
211 ;; white space that might have been created
212 ;; when including results.
213 (if (equal rep "")
214 (delete-region
215 begin
216 (progn (goto-char end)
217 (if (not (eq type 'babel-call))
218 (progn (skip-chars-forward " \t")
219 (point))
220 (skip-chars-forward " \r\t\n")
221 (line-beginning-position))))
222 ;; Otherwise, preserve trailing
223 ;; spaces/newlines and then, insert
224 ;; replacement string.
225 (goto-char begin)
226 (delete-region begin end)
227 (insert rep))))
228 (`src-block
229 (let ((match-start (copy-marker (match-beginning 0)))
230 (ind (org-get-indentation)))
231 ;; Take care of matched block: compute
232 ;; replacement string. In particular, a nil
233 ;; REPLACEMENT means the block is left as-is
234 ;; while an empty string removes the block.
235 (let ((replacement
236 (progn (goto-char match-start)
237 (org-babel-exp-src-block))))
238 (cond ((not replacement) (goto-char end))
239 ((equal replacement "")
240 (goto-char end)
241 (skip-chars-forward " \r\t\n")
242 (beginning-of-line)
243 (delete-region begin (point)))
245 (goto-char match-start)
246 (delete-region (point)
247 (save-excursion
248 (goto-char end)
249 (line-end-position)))
250 (insert replacement)
251 (if (or org-src-preserve-indentation
252 (org-element-property
253 :preserve-indent element))
254 ;; Indent only code block
255 ;; markers.
256 (save-excursion
257 (skip-chars-backward " \r\t\n")
258 (indent-line-to ind)
259 (goto-char match-start)
260 (indent-line-to ind))
261 ;; Indent everything.
262 (indent-rigidly
263 match-start (point) ind)))))
264 (set-marker match-start nil))))
265 (set-marker begin nil)
266 (set-marker end nil)))))
267 (kill-buffer org-babel-exp-reference-buffer)
268 (remove-text-properties (point-min) (point-max) '(org-reference)))))))
270 (defun org-babel-exp-do-export (info type &optional hash)
271 "Return a string with the exported content of a code block.
272 The function respects the value of the :exports header argument."
273 (let ((silently (lambda () (let ((session (cdr (assq :session (nth 2 info)))))
274 (unless (equal "none" session)
275 (org-babel-exp-results info type 'silent)))))
276 (clean (lambda () (if (eq type 'inline)
277 (org-babel-remove-inline-result)
278 (org-babel-remove-result info)))))
279 (pcase (or (cdr (assq :exports (nth 2 info))) "code")
280 ("none" (funcall silently) (funcall clean) "")
281 ("code" (funcall silently) (funcall clean) (org-babel-exp-code info type))
282 ("results" (org-babel-exp-results info type nil hash) "")
283 ("both"
284 (org-babel-exp-results info type nil hash)
285 (org-babel-exp-code info type)))))
287 (defcustom org-babel-exp-code-template
288 "#+BEGIN_SRC %lang%switches%flags\n%body\n#+END_SRC"
289 "Template used to export the body of code blocks.
290 This template may be customized to include additional information
291 such as the code block name, or the values of particular header
292 arguments. The template is filled out using `org-fill-template',
293 and the following %keys may be used.
295 lang ------ the language of the code block
296 name ------ the name of the code block
297 body ------ the body of the code block
298 switches -- the switches associated to the code block
299 flags ----- the flags passed to the code block
301 In addition to the keys mentioned above, every header argument
302 defined for the code block may be used as a key and will be
303 replaced with its value."
304 :group 'org-babel
305 :type 'string)
307 (defcustom org-babel-exp-inline-code-template
308 "src_%lang[%switches%flags]{%body}"
309 "Template used to export the body of inline code blocks.
310 This template may be customized to include additional information
311 such as the code block name, or the values of particular header
312 arguments. The template is filled out using `org-fill-template',
313 and the following %keys may be used.
315 lang ------ the language of the code block
316 name ------ the name of the code block
317 body ------ the body of the code block
318 switches -- the switches associated to the code block
319 flags ----- the flags passed to the code block
321 In addition to the keys mentioned above, every header argument
322 defined for the code block may be used as a key and will be
323 replaced with its value."
324 :group 'org-babel
325 :type 'string
326 :version "25.2"
327 :package-version '(Org . "8.3"))
329 (defun org-babel-exp-code (info type)
330 "Return the original code block formatted for export."
331 (setf (nth 1 info)
332 (if (string= "strip-export" (cdr (assq :noweb (nth 2 info))))
333 (replace-regexp-in-string
334 (org-babel-noweb-wrap) "" (nth 1 info))
335 (if (org-babel-noweb-p (nth 2 info) :export)
336 (org-babel-expand-noweb-references
337 info org-babel-exp-reference-buffer)
338 (nth 1 info))))
339 (org-fill-template
340 (if (eq type 'inline)
341 org-babel-exp-inline-code-template
342 org-babel-exp-code-template)
343 `(("lang" . ,(nth 0 info))
344 ("body" . ,(org-escape-code-in-string (nth 1 info)))
345 ("switches" . ,(let ((f (nth 3 info)))
346 (and (org-string-nw-p f) (concat " " f))))
347 ("flags" . ,(let ((f (assq :flags (nth 2 info))))
348 (and f (concat " " (cdr f)))))
349 ,@(mapcar (lambda (pair)
350 (cons (substring (symbol-name (car pair)) 1)
351 (format "%S" (cdr pair))))
352 (nth 2 info))
353 ("name" . ,(or (nth 4 info) "")))))
355 (defun org-babel-exp-results (info type &optional silent hash)
356 "Evaluate and return the results of the current code block for export.
357 Results are prepared in a manner suitable for export by Org mode.
358 This function is called by `org-babel-exp-do-export'. The code
359 block will be evaluated. Optional argument SILENT can be used to
360 inhibit insertion of results into the buffer."
361 (unless (and hash (equal hash (org-babel-current-result-hash)))
362 (let ((lang (nth 0 info))
363 (body (if (org-babel-noweb-p (nth 2 info) :eval)
364 (org-babel-expand-noweb-references
365 info org-babel-exp-reference-buffer)
366 (nth 1 info)))
367 (info (copy-sequence info))
368 (org-babel-current-src-block-location (point-marker)))
369 ;; Skip code blocks which we can't evaluate.
370 (when (fboundp (intern (concat "org-babel-execute:" lang)))
371 (org-babel-eval-wipe-error-buffer)
372 (setf (nth 1 info) body)
373 (setf (nth 2 info)
374 (org-babel-exp--at-source
375 (org-babel-process-params
376 (org-babel-merge-params
377 (nth 2 info)
378 `((:results . ,(if silent "silent" "replace")))))))
379 (pcase type
380 (`block (org-babel-execute-src-block nil info))
381 (`inline
382 ;; Position the point on the inline source block
383 ;; allowing `org-babel-insert-result' to check that the
384 ;; block is inline.
385 (goto-char (nth 5 info))
386 (org-babel-execute-src-block nil info))
387 (`lob
388 (save-excursion
389 (goto-char (nth 5 info))
390 (let (org-confirm-babel-evaluate)
391 (org-babel-execute-src-block nil info)))))))))
394 (provide 'ob-exp)
396 ;;; ob-exp.el ends here