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