Merge branch 'maint'
[org-mode/org-tableheadings.git] / lisp / ob-exp.el
blob769b7f0218f5b9ade705dec3768df2fece597df4
1 ;;; ob-exp.el --- Exportation of org-babel source blocks
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)
27 (require 'org-src)
28 (eval-when-compile
29 (require 'cl))
31 (defvar org-babel-ref-split-regexp)
33 (declare-function org-babel-lob-get-info "ob-lob" (&optional datum))
34 (declare-function org-babel-eval-wipe-error-buffer "ob-eval" ())
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))
38 (declare-function org-link-search "org" (s &optional avoid-pos stealth))
39 (declare-function org-fill-template "org" (template alist))
40 (declare-function org-split-string "org" (string &optional separators))
41 (declare-function org-element-at-point "org-element" ())
42 (declare-function org-element-context "org-element" (&optional element))
43 (declare-function org-element-property "org-element" (property element))
44 (declare-function org-element-type "org-element" (element))
45 (declare-function org-id-get "org-id" (&optional pom create prefix))
47 (defcustom org-export-babel-evaluate t
48 "Switch controlling code evaluation during export.
49 When set to nil no code will be evaluated as part of the export
50 process. When set to `inline-only', only inline code blocks will
51 be executed."
52 :group 'org-babel
53 :version "24.1"
54 :type '(choice (const :tag "Never" nil)
55 (const :tag "Only inline code" inline-only)
56 (const :tag "Always" t)))
57 (put 'org-export-babel-evaluate 'safe-local-variable (lambda (x) (eq x nil)))
59 (defvar org-link-search-inhibit-query)
60 (defmacro org-babel-exp-in-export-file (lang &rest body)
61 (declare (indent 1))
62 `(let* ((lang-headers (intern (concat "org-babel-default-header-args:" ,lang)))
63 (heading-query (or (org-id-get)
64 ;; CUSTOM_IDs don't work, maybe they are
65 ;; stripped, or maybe they resolve too
66 ;; late in `org-link-search'.
67 ;; (org-entry-get nil "CUSTOM_ID")
68 (nth 4 (ignore-errors (org-heading-components)))))
69 (export-buffer (current-buffer))
70 results)
71 (when org-babel-exp-reference-buffer
72 ;; Resolve parameters in the original file so that headline and
73 ;; file-wide parameters are included, attempt to go to the same
74 ;; heading in the original file
75 (set-buffer org-babel-exp-reference-buffer)
76 (save-restriction
77 (when heading-query
78 (condition-case nil
79 (let ((org-link-search-inhibit-query t))
80 ;; TODO: When multiple headings have the same title,
81 ;; this returns the first, which is not always
82 ;; the right heading. Consider a better way to
83 ;; find the proper heading.
84 (org-link-search heading-query))
85 (error (when heading-query
86 (goto-char (point-min))
87 (re-search-forward (regexp-quote heading-query) nil t)))))
88 (setq results ,@body))
89 (set-buffer export-buffer)
90 results)))
91 (def-edebug-spec org-babel-exp-in-export-file (form body))
93 (defun org-babel-exp-src-block (&rest headers)
94 "Process source block for export.
95 Depending on the `export' headers argument, replace the source
96 code block like this:
98 both ---- display the code and the results
100 code ---- the default, display the code inside the block but do
101 not process
103 results - just like none only the block is run on export ensuring
104 that its results are present in the org-mode buffer
106 none ---- do not display either code or results upon export
108 Assume point is at the beginning of block's starting line."
109 (interactive)
110 (save-excursion
111 (let* ((info (org-babel-get-src-block-info 'light))
112 (lang (nth 0 info))
113 (raw-params (nth 2 info)) hash)
114 ;; bail if we couldn't get any info from the block
115 (unless noninteractive
116 (message "org-babel-exp process %s at position %d..."
117 lang (line-beginning-position)))
118 (when info
119 ;; if we're actually going to need the parameters
120 (when (member (cdr (assoc :exports (nth 2 info))) '("both" "results"))
121 (org-babel-exp-in-export-file lang
122 (setf (nth 2 info)
123 (org-babel-process-params
124 (apply #'org-babel-merge-params
125 org-babel-default-header-args
126 (if (boundp lang-headers) (eval lang-headers) nil)
127 (append (org-babel-params-from-properties lang)
128 (list raw-params))))))
129 (setf hash (org-babel-sha1-hash info)))
130 (org-babel-exp-do-export info 'block hash)))))
132 (defcustom org-babel-exp-call-line-template
134 "Template used to export call lines.
135 This template may be customized to include the call line name
136 with any export markup. The template is filled out using
137 `org-fill-template', and the following %keys may be used.
139 line --- call line
141 An example value would be \"\\n: call: %line\" to export the call line
142 wrapped in a verbatim environment.
144 Note: the results are inserted separately after the contents of
145 this template."
146 :group 'org-babel
147 :type 'string)
149 (defvar org-babel-default-lob-header-args)
150 (defun org-babel-exp-process-buffer (reference-buffer)
151 "Execute all Babel blocks in current buffer.
152 REFERENCE-BUFFER is the buffer containing a pristine copy of the
153 buffer being processed. It is used to properly resolve
154 references in source blocks, as modifications in current buffer
155 may make them unreachable."
156 (interactive)
157 (when org-export-babel-evaluate
158 (save-window-excursion
159 (save-excursion
160 (let ((case-fold-search t)
161 (org-babel-exp-reference-buffer reference-buffer)
162 (regexp
163 (if (eq org-export-babel-evaluate 'inline-only)
164 "\\(call\\|src\\)_"
165 "\\(call\\|src\\)_\\|^[ \t]*#\\+\\(BEGIN_SRC\\|CALL:\\)")))
166 (goto-char (point-min))
167 (while (re-search-forward regexp nil t)
168 (unless (save-match-data (org-in-commented-heading-p))
169 (let* ((element (save-match-data (org-element-context)))
170 (type (org-element-type element))
171 (begin (copy-marker (org-element-property :begin element)))
172 (end (copy-marker
173 (save-excursion
174 (goto-char (org-element-property :end element))
175 (skip-chars-backward " \r\t\n")
176 (point)))))
177 (case type
178 (inline-src-block
179 (let* ((info (org-babel-get-src-block-info nil element))
180 (params (nth 2 info)))
181 (setf (nth 1 info)
182 (if (and (cdr (assoc :noweb params))
183 (string= "yes" (cdr (assoc :noweb params))))
184 (org-babel-expand-noweb-references
185 info org-babel-exp-reference-buffer)
186 (nth 1 info)))
187 (goto-char begin)
188 (let ((replacement (org-babel-exp-do-export info 'inline)))
189 (if (equal replacement "")
190 ;; Replacement code is empty: remove inline
191 ;; source block, including extra white
192 ;; space that might have been created when
193 ;; inserting results.
194 (delete-region begin
195 (progn (goto-char end)
196 (skip-chars-forward " \t")
197 (point)))
198 ;; Otherwise: remove inline src block but
199 ;; preserve following white spaces. Then
200 ;; insert value.
201 (delete-region begin end)
202 (insert replacement)))))
203 ((babel-call inline-babel-call)
204 (let ((results (org-babel-exp-do-export
205 (org-babel-lob-get-info element)
206 'lob))
207 (rep
208 (org-fill-template
209 org-babel-exp-call-line-template
210 `(("line" .
211 ,(org-element-property :value element))))))
212 ;; If replacement is empty, completely remove the
213 ;; object/element, including any extra white
214 ;; space that might have been created when
215 ;; including results.
216 (if (equal rep "")
217 (delete-region
218 begin
219 (progn (goto-char end)
220 (if (not (eq type 'babel-call))
221 (progn (skip-chars-forward " \t") (point))
222 (skip-chars-forward " \r\t\n")
223 (line-beginning-position))))
224 ;; Otherwise, preserve trailing spaces/newlines
225 ;; and then, insert replacement string.
226 (goto-char begin)
227 (delete-region begin end)
228 (insert rep))))
229 (src-block
230 (let* ((match-start (copy-marker (match-beginning 0)))
231 (ind (org-get-indentation))
232 (lang (or (org-element-property :language element)
233 (user-error
234 "No language for src block: %s"
235 (or (org-element-property :name element)
236 "(unnamed)"))))
237 (headers
238 (cons lang
239 (let ((params
240 (org-element-property
241 :parameters element)))
242 (and params (org-split-string params))))))
243 ;; Take care of matched block: compute
244 ;; replacement string. In particular, a nil
245 ;; REPLACEMENT means the block is left as-is
246 ;; while an empty string removes the block.
247 (let ((replacement
248 (progn (goto-char match-start)
249 (org-babel-exp-src-block headers))))
250 (cond ((not replacement) (goto-char end))
251 ((equal replacement "")
252 (goto-char end)
253 (skip-chars-forward " \r\t\n")
254 (beginning-of-line)
255 (delete-region begin (point)))
257 (goto-char match-start)
258 (delete-region (point)
259 (save-excursion (goto-char end)
260 (line-end-position)))
261 (insert replacement)
262 (if (or org-src-preserve-indentation
263 (org-element-property :preserve-indent
264 element))
265 ;; Indent only code block markers.
266 (save-excursion (skip-chars-backward " \r\t\n")
267 (indent-line-to ind)
268 (goto-char match-start)
269 (indent-line-to ind))
270 ;; Indent everything.
271 (indent-rigidly match-start (point) ind)))))
272 (set-marker match-start nil))))
273 (set-marker begin nil)
274 (set-marker end nil)))))))))
276 (defun org-babel-exp-do-export (info type &optional hash)
277 "Return a string with the exported content of a code block.
278 The function respects the value of the :exports header argument."
279 (let ((silently (lambda () (let ((session (cdr (assoc :session (nth 2 info)))))
280 (unless (equal "none" session)
281 (org-babel-exp-results info type 'silent)))))
282 (clean (lambda () (if (eq type 'inline)
283 (org-babel-remove-inline-result)
284 (org-babel-remove-result info)))))
285 (pcase (or (cdr (assq :exports (nth 2 info))) "code")
286 ("none" (funcall silently) (funcall clean) "")
287 ("code" (funcall silently) (funcall clean) (org-babel-exp-code info type))
288 ("results" (org-babel-exp-results info type nil hash) "")
289 ("both"
290 (org-babel-exp-results info type nil hash)
291 (org-babel-exp-code info type)))))
293 (defcustom org-babel-exp-code-template
294 "#+BEGIN_SRC %lang%switches%flags\n%body\n#+END_SRC"
295 "Template used to export the body of code blocks.
296 This template may be customized to include additional information
297 such as the code block name, or the values of particular header
298 arguments. The template is filled out using `org-fill-template',
299 and the following %keys may be used.
301 lang ------ the language of the code block
302 name ------ the name of the code block
303 body ------ the body of the code block
304 switches -- the switches associated to the code block
305 flags ----- the flags passed to the code block
307 In addition to the keys mentioned above, every header argument
308 defined for the code block may be used as a key and will be
309 replaced with its value."
310 :group 'org-babel
311 :type 'string)
313 (defcustom org-babel-exp-inline-code-template
314 "src_%lang[%switches%flags]{%body}"
315 "Template used to export the body of inline code blocks.
316 This template may be customized to include additional information
317 such as the code block name, or the values of particular header
318 arguments. The template is filled out using `org-fill-template',
319 and the following %keys may be used.
321 lang ------ the language of the code block
322 name ------ the name of the code block
323 body ------ the body of the code block
324 switches -- the switches associated to the code block
325 flags ----- the flags passed to the code block
327 In addition to the keys mentioned above, every header argument
328 defined for the code block may be used as a key and will be
329 replaced with its value."
330 :group 'org-babel
331 :type 'string
332 :version "25.1"
333 :package-version '(Org . "8.3"))
335 (defun org-babel-exp-code (info type)
336 "Return the original code block formatted for export."
337 (setf (nth 1 info)
338 (if (string= "strip-export" (cdr (assoc :noweb (nth 2 info))))
339 (replace-regexp-in-string
340 (org-babel-noweb-wrap) "" (nth 1 info))
341 (if (org-babel-noweb-p (nth 2 info) :export)
342 (org-babel-expand-noweb-references
343 info org-babel-exp-reference-buffer)
344 (nth 1 info))))
345 (org-fill-template
346 (if (eq type 'inline)
347 org-babel-exp-inline-code-template
348 org-babel-exp-code-template)
349 `(("lang" . ,(nth 0 info))
350 ("body" . ,(nth 1 info))
351 ("switches" . ,(let ((f (nth 3 info)))
352 (and (org-string-nw-p f) (concat " " f))))
353 ("flags" . ,(let ((f (assq :flags (nth 2 info))))
354 (and f (concat " " (cdr f)))))
355 ,@(mapcar (lambda (pair)
356 (cons (substring (symbol-name (car pair)) 1)
357 (format "%S" (cdr pair))))
358 (nth 2 info))
359 ("name" . ,(or (nth 4 info) "")))))
361 (defun org-babel-exp-results (info type &optional silent hash)
362 "Evaluate and return the results of the current code block for export.
363 Results are prepared in a manner suitable for export by Org mode.
364 This function is called by `org-babel-exp-do-export'. The code
365 block will be evaluated. Optional argument SILENT can be used to
366 inhibit insertion of results into the buffer."
367 (unless (and hash (equal hash (org-babel-current-result-hash)))
368 (let ((lang (nth 0 info))
369 (body (if (org-babel-noweb-p (nth 2 info) :eval)
370 (org-babel-expand-noweb-references
371 info org-babel-exp-reference-buffer)
372 (nth 1 info)))
373 (info (copy-sequence info))
374 (org-babel-current-src-block-location (point-marker)))
375 ;; Skip code blocks which we can't evaluate.
376 (when (fboundp (intern (concat "org-babel-execute:" lang)))
377 (org-babel-eval-wipe-error-buffer)
378 (prog1 nil
379 (setf (nth 1 info) body)
380 (setf (nth 2 info)
381 (org-babel-exp-in-export-file lang
382 (org-babel-process-params
383 (org-babel-merge-params
384 (nth 2 info)
385 `((:results . ,(if silent "silent" "replace")))))))
386 (pcase type
387 (`block (org-babel-execute-src-block nil info))
388 (`inline
389 ;; Position the point on the inline source block
390 ;; allowing `org-babel-insert-result' to check that the
391 ;; block is inline.
392 (goto-char (nth 5 info))
393 (org-babel-execute-src-block nil info))
394 (`lob
395 (save-excursion
396 (goto-char (nth 5 info))
397 (let (org-confirm-babel-evaluate)
398 (org-babel-execute-src-block nil info))))))))))
401 (provide 'ob-exp)
403 ;;; ob-exp.el ends here