org-info: Use lexical binding
[org-mode/org-tableheadings.git] / lisp / ob-exp.el
blobfbddf10d0d7b799bc316649e3f1642e2958282b0
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-export-copy-buffer "ox" ())
34 (declare-function org-fill-template "org" (template alist))
35 (declare-function org-get-indentation "org" (&optional line))
36 (declare-function org-heading-components "org" ())
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. When set to `inline-only', only inline code blocks will
45 be executed."
46 :group 'org-babel
47 :version "24.1"
48 :type '(choice (const :tag "Never" nil)
49 (const :tag "Only inline code" inline-only)
50 (const :tag "Always" t)))
51 (put 'org-export-babel-evaluate 'safe-local-variable #'null)
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 (with-current-buffer org-babel-exp-reference-buffer
61 (org-with-wide-buffer
62 (goto-char source)
63 ,@body))))
65 (defun org-babel-exp-src-block ()
66 "Process source block for export.
67 Depending on the \":export\" header argument, replace the source
68 code block like this:
70 both ---- display the code and the results
72 code ---- the default, display the code inside the block but do
73 not process
75 results - just like none only the block is run on export ensuring
76 that its results are present in the Org mode buffer
78 none ---- do not display either code or results upon export
80 Assume point is at block opening line."
81 (interactive)
82 (save-excursion
83 (let* ((info (org-babel-get-src-block-info 'light))
84 (lang (nth 0 info))
85 (raw-params (nth 2 info))
86 hash)
87 ;; bail if we couldn't get any info from the block
88 (unless noninteractive
89 (message "org-babel-exp process %s at position %d..."
90 lang
91 (line-beginning-position)))
92 (when info
93 ;; if we're actually going to need the parameters
94 (when (member (cdr (assq :exports (nth 2 info))) '("both" "results"))
95 (let ((lang-headers (intern (concat "org-babel-default-header-args:"
96 lang))))
97 (org-babel-exp--at-source
98 (setf (nth 2 info)
99 (org-babel-process-params
100 (apply #'org-babel-merge-params
101 org-babel-default-header-args
102 (and (boundp lang-headers) (eval lang-headers t))
103 (append (org-babel-params-from-properties lang)
104 (list raw-params)))))))
105 (setf hash (org-babel-sha1-hash info)))
106 (org-babel-exp-do-export info 'block hash)))))
108 (defcustom org-babel-exp-call-line-template
110 "Template used to export call lines.
111 This template may be customized to include the call line name
112 with any export markup. The template is filled out using
113 `org-fill-template', and the following %keys may be used.
115 line --- call line
117 An example value would be \"\\n: call: %line\" to export the call line
118 wrapped in a verbatim environment.
120 Note: the results are inserted separately after the contents of
121 this template."
122 :group 'org-babel
123 :type 'string)
125 (defun org-babel-exp-process-buffer ()
126 "Execute all Babel blocks in current buffer."
127 (interactive)
128 (when org-export-babel-evaluate
129 (save-window-excursion
130 (let ((case-fold-search t)
131 (regexp (if (eq org-export-babel-evaluate 'inline-only)
132 "\\(call\\|src\\)_"
133 "\\(call\\|src\\)_\\|^[ \t]*#\\+\\(BEGIN_SRC\\|CALL:\\)"))
134 ;; Get a pristine copy of current buffer so Babel
135 ;; references are properly resolved and source block
136 ;; context is preserved.
137 (org-babel-exp-reference-buffer (org-export-copy-buffer)))
138 (unwind-protect
139 (save-excursion
140 ;; First attach to every source block their original
141 ;; position, so that they can be retrieved within
142 ;; `org-babel-exp-reference-buffer', even after heavy
143 ;; modifications on current buffer.
145 ;; False positives are harmless, so we don't check if
146 ;; we're really at some Babel object. Moreover,
147 ;; `line-end-position' ensures that we propertize
148 ;; a noticeable part of the object, without affecting
149 ;; multiple objects on the same line.
150 (goto-char (point-min))
151 (while (re-search-forward regexp nil t)
152 (let ((s (match-beginning 0)))
153 (put-text-property s (line-end-position) 'org-reference s)))
154 ;; Evaluate from top to bottom every Babel block
155 ;; encountered.
156 (goto-char (point-min))
157 (while (re-search-forward regexp nil t)
158 (unless (save-match-data (org-in-commented-heading-p))
159 (let* ((element (save-match-data (org-element-context)))
160 (type (org-element-type element))
161 (begin
162 (copy-marker (org-element-property :begin element)))
163 (end
164 (copy-marker
165 (save-excursion
166 (goto-char (org-element-property :end element))
167 (skip-chars-backward " \r\t\n")
168 (point)))))
169 (pcase type
170 (`inline-src-block
171 (let* ((info
172 (org-babel-get-src-block-info nil element))
173 (params (nth 2 info)))
174 (setf (nth 1 info)
175 (if (and (cdr (assoc :noweb params))
176 (string= "yes"
177 (cdr (assq :noweb params))))
178 (org-babel-expand-noweb-references
179 info org-babel-exp-reference-buffer)
180 (nth 1 info)))
181 (goto-char begin)
182 (let ((replacement
183 (org-babel-exp-do-export info 'inline)))
184 (if (equal replacement "")
185 ;; Replacement code is empty: remove
186 ;; inline source block, including extra
187 ;; white space that might have been
188 ;; created when inserting results.
189 (delete-region begin
190 (progn (goto-char end)
191 (skip-chars-forward " \t")
192 (point)))
193 ;; Otherwise: remove inline src block but
194 ;; preserve following white spaces. Then
195 ;; insert value.
196 (delete-region begin end)
197 (insert replacement)))))
198 ((or `babel-call `inline-babel-call)
199 (org-babel-exp-do-export (org-babel-lob-get-info element)
200 'lob)
201 (let ((rep
202 (org-fill-template
203 org-babel-exp-call-line-template
204 `(("line" .
205 ,(org-element-property :value element))))))
206 ;; If replacement is empty, completely remove
207 ;; the object/element, including any extra
208 ;; white space that might have been created
209 ;; when including results.
210 (if (equal rep "")
211 (delete-region
212 begin
213 (progn (goto-char end)
214 (if (not (eq type 'babel-call))
215 (progn (skip-chars-forward " \t")
216 (point))
217 (skip-chars-forward " \r\t\n")
218 (line-beginning-position))))
219 ;; Otherwise, preserve trailing
220 ;; spaces/newlines and then, insert
221 ;; replacement string.
222 (goto-char begin)
223 (delete-region begin end)
224 (insert rep))))
225 (`src-block
226 (let ((match-start (copy-marker (match-beginning 0)))
227 (ind (org-get-indentation)))
228 ;; Take care of matched block: compute
229 ;; replacement string. In particular, a nil
230 ;; REPLACEMENT means the block is left as-is
231 ;; while an empty string removes the block.
232 (let ((replacement
233 (progn (goto-char match-start)
234 (org-babel-exp-src-block))))
235 (cond ((not replacement) (goto-char end))
236 ((equal replacement "")
237 (goto-char end)
238 (skip-chars-forward " \r\t\n")
239 (beginning-of-line)
240 (delete-region begin (point)))
242 (goto-char match-start)
243 (delete-region (point)
244 (save-excursion
245 (goto-char end)
246 (line-end-position)))
247 (insert replacement)
248 (if (or org-src-preserve-indentation
249 (org-element-property
250 :preserve-indent element))
251 ;; Indent only code block
252 ;; markers.
253 (save-excursion
254 (skip-chars-backward " \r\t\n")
255 (indent-line-to ind)
256 (goto-char match-start)
257 (indent-line-to ind))
258 ;; Indent everything.
259 (indent-rigidly
260 match-start (point) ind)))))
261 (set-marker match-start nil))))
262 (set-marker begin nil)
263 (set-marker end nil)))))
264 (kill-buffer org-babel-exp-reference-buffer)
265 (remove-text-properties (point-min) (point-max) '(org-reference)))))))
267 (defun org-babel-exp-do-export (info type &optional hash)
268 "Return a string with the exported content of a code block.
269 The function respects the value of the :exports header argument."
270 (let ((silently (lambda () (let ((session (cdr (assoc :session (nth 2 info)))))
271 (unless (equal "none" session)
272 (org-babel-exp-results info type 'silent)))))
273 (clean (lambda () (if (eq type 'inline)
274 (org-babel-remove-inline-result)
275 (org-babel-remove-result info)))))
276 (pcase (or (cdr (assq :exports (nth 2 info))) "code")
277 ("none" (funcall silently) (funcall clean) "")
278 ("code" (funcall silently) (funcall clean) (org-babel-exp-code info type))
279 ("results" (org-babel-exp-results info type nil hash) "")
280 ("both"
281 (org-babel-exp-results info type nil hash)
282 (org-babel-exp-code info type)))))
284 (defcustom org-babel-exp-code-template
285 "#+BEGIN_SRC %lang%switches%flags\n%body\n#+END_SRC"
286 "Template used to export the body of code blocks.
287 This template may be customized to include additional information
288 such as the code block name, or the values of particular header
289 arguments. The template is filled out using `org-fill-template',
290 and the following %keys may be used.
292 lang ------ the language of the code block
293 name ------ the name of the code block
294 body ------ the body of the code block
295 switches -- the switches associated to the code block
296 flags ----- the flags passed to the code block
298 In addition to the keys mentioned above, every header argument
299 defined for the code block may be used as a key and will be
300 replaced with its value."
301 :group 'org-babel
302 :type 'string)
304 (defcustom org-babel-exp-inline-code-template
305 "src_%lang[%switches%flags]{%body}"
306 "Template used to export the body of inline code blocks.
307 This template may be customized to include additional information
308 such as the code block name, or the values of particular header
309 arguments. The template is filled out using `org-fill-template',
310 and the following %keys may be used.
312 lang ------ the language of the code block
313 name ------ the name of the code block
314 body ------ the body of the code block
315 switches -- the switches associated to the code block
316 flags ----- the flags passed to the code block
318 In addition to the keys mentioned above, every header argument
319 defined for the code block may be used as a key and will be
320 replaced with its value."
321 :group 'org-babel
322 :type 'string
323 :version "25.1"
324 :package-version '(Org . "8.3"))
326 (defun org-babel-exp-code (info type)
327 "Return the original code block formatted for export."
328 (setf (nth 1 info)
329 (if (string= "strip-export" (cdr (assoc :noweb (nth 2 info))))
330 (replace-regexp-in-string
331 (org-babel-noweb-wrap) "" (nth 1 info))
332 (if (org-babel-noweb-p (nth 2 info) :export)
333 (org-babel-expand-noweb-references
334 info org-babel-exp-reference-buffer)
335 (nth 1 info))))
336 (org-fill-template
337 (if (eq type 'inline)
338 org-babel-exp-inline-code-template
339 org-babel-exp-code-template)
340 `(("lang" . ,(nth 0 info))
341 ("body" . ,(nth 1 info))
342 ("switches" . ,(let ((f (nth 3 info)))
343 (and (org-string-nw-p f) (concat " " f))))
344 ("flags" . ,(let ((f (assq :flags (nth 2 info))))
345 (and f (concat " " (cdr f)))))
346 ,@(mapcar (lambda (pair)
347 (cons (substring (symbol-name (car pair)) 1)
348 (format "%S" (cdr pair))))
349 (nth 2 info))
350 ("name" . ,(or (nth 4 info) "")))))
352 (defun org-babel-exp-results (info type &optional silent hash)
353 "Evaluate and return the results of the current code block for export.
354 Results are prepared in a manner suitable for export by Org mode.
355 This function is called by `org-babel-exp-do-export'. The code
356 block will be evaluated. Optional argument SILENT can be used to
357 inhibit insertion of results into the buffer."
358 (unless (and hash (equal hash (org-babel-current-result-hash)))
359 (let ((lang (nth 0 info))
360 (body (if (org-babel-noweb-p (nth 2 info) :eval)
361 (org-babel-expand-noweb-references
362 info org-babel-exp-reference-buffer)
363 (nth 1 info)))
364 (info (copy-sequence info))
365 (org-babel-current-src-block-location (point-marker)))
366 ;; Skip code blocks which we can't evaluate.
367 (when (fboundp (intern (concat "org-babel-execute:" lang)))
368 (org-babel-eval-wipe-error-buffer)
369 (setf (nth 1 info) body)
370 (setf (nth 2 info)
371 (org-babel-exp--at-source
372 (org-babel-process-params
373 (org-babel-merge-params
374 (nth 2 info)
375 `((:results . ,(if silent "silent" "replace")))))))
376 (pcase type
377 (`block (org-babel-execute-src-block nil info))
378 (`inline
379 ;; Position the point on the inline source block
380 ;; allowing `org-babel-insert-result' to check that the
381 ;; block is inline.
382 (goto-char (nth 5 info))
383 (org-babel-execute-src-block nil info))
384 (`lob
385 (save-excursion
386 (goto-char (nth 5 info))
387 (let (org-confirm-babel-evaluate)
388 (org-babel-execute-src-block nil info)))))))))
391 (provide 'ob-exp)
393 ;;; ob-exp.el ends here