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