testing: allow to select tests
[org-mode/org-tableheadings.git] / lisp / ob-exp.el
blob2f63590608cabcda8a7fe0a96213adb44cce2b5e
1 ;;; ob-exp.el --- Exportation of org-babel source blocks
3 ;; Copyright (C) 2009-2013 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 (eval-when-compile
28 (require 'cl))
30 (defvar org-current-export-file)
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 "org"
38 (start-re end-re &optional lim-up lim-down))
39 (declare-function org-get-indentation "org" (&optional line))
40 (declare-function org-heading-components "org" ())
41 (declare-function org-in-block-p "org" (names))
42 (declare-function org-in-verbatim-emphasis "org" ())
43 (declare-function org-link-search "org" (s &optional type 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" (&optional keep-trail))
47 (declare-function org-element-context "org-element" ())
48 (declare-function org-element-property "org-element" (property element))
49 (declare-function org-element-type "org-element" (element))
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 (defun org-babel-exp-get-export-buffer ()
65 "Return the current export buffer if possible."
66 (cond
67 ((bufferp org-current-export-file) org-current-export-file)
68 (org-current-export-file (get-file-buffer org-current-export-file))
69 ('otherwise
70 (error "Requested export buffer when `org-current-export-file' is nil"))))
72 (defvar org-link-search-inhibit-query)
74 (defmacro org-babel-exp-in-export-file (lang &rest body)
75 (declare (indent 1))
76 `(let* ((lang-headers (intern (concat "org-babel-default-header-args:" ,lang)))
77 (heading (nth 4 (ignore-errors (org-heading-components))))
78 (export-buffer (current-buffer))
79 (original-buffer (org-babel-exp-get-export-buffer)) results)
80 (when original-buffer
81 ;; resolve parameters in the original file so that
82 ;; headline and file-wide parameters are included, attempt
83 ;; to go to the same heading in the original file
84 (set-buffer original-buffer)
85 (save-restriction
86 (when heading
87 (condition-case nil
88 (let ((org-link-search-inhibit-query t))
89 (org-link-search heading))
90 (error (when heading
91 (goto-char (point-min))
92 (re-search-forward (regexp-quote heading) 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 it's 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 (line (org-current-line))
118 (lang (nth 0 info))
119 (raw-params (nth 2 info)) hash)
120 ;; bail if we couldn't get any info from the block
121 (unless noninteractive
122 (message "org-babel-exp process %s at line %d..." lang line))
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-non-block-elements (start end)
156 "Process inline source and call lines between START and END for export."
157 (interactive)
158 (save-excursion
159 (goto-char start)
160 (unless (markerp end)
161 (let ((m (make-marker)))
162 (set-marker m end (current-buffer))
163 (setq end m)))
164 (let ((rx (concat "\\(?:" org-babel-inline-src-block-regexp
165 "\\|" org-babel-lob-one-liner-regexp "\\)")))
166 (while (re-search-forward rx end t)
167 (save-excursion
168 (let* ((element (save-excursion
169 ;; If match is inline, point is at its
170 ;; end. Move backward so
171 ;; `org-element-context' can get the
172 ;; object, not the following one.
173 (backward-char)
174 (save-match-data (org-element-context))))
175 (type (org-element-type element)))
176 (when (memq type '(babel-call inline-babel-call inline-src-block))
177 (let ((beg-el (org-element-property :begin element))
178 (end-el (org-element-property :end element)))
179 (case type
180 (inline-src-block
181 (let* ((info (org-babel-parse-inline-src-block-match))
182 (params (nth 2 info)))
183 (setf (nth 1 info)
184 (if (and (cdr (assoc :noweb params))
185 (string= "yes" (cdr (assoc :noweb params))))
186 (org-babel-expand-noweb-references
187 info (org-babel-exp-get-export-buffer))
188 (nth 1 info)))
189 (goto-char beg-el)
190 (let ((replacement (org-babel-exp-do-export info 'inline)))
191 (if (equal replacement "")
192 ;; Replacement code is empty: completely
193 ;; remove inline src block, including extra
194 ;; white space that might have been created
195 ;; when inserting results.
196 (delete-region beg-el
197 (progn (goto-char end-el)
198 (skip-chars-forward " \t")
199 (point)))
200 ;; Otherwise: remove inline src block but
201 ;; preserve following white spaces. Then
202 ;; insert value.
203 (delete-region beg-el
204 (progn (goto-char end-el)
205 (skip-chars-backward " \t")
206 (point)))
207 (insert replacement)))))
208 ((babel-call inline-babel-call)
209 (let* ((lob-info (org-babel-lob-get-info))
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 3 lob-info) (nth 2 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 space
233 ;; that might have been created when including
234 ;; results.
235 (if (equal rep "")
236 (delete-region
237 beg-el
238 (progn (goto-char end-el)
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 following white
244 ;; spaces/newlines and then, insert replacement
245 ;; string.
246 (goto-char beg-el)
247 (delete-region beg-el
248 (progn (goto-char end-el)
249 (skip-chars-backward " \r\t\n")
250 (point)))
251 (insert rep)))))))))))))
253 (defvar org-src-preserve-indentation) ; From org-src.el
254 (defun org-babel-exp-process-buffer ()
255 "Execute all blocks in visible part of buffer."
256 (interactive)
257 (save-window-excursion
258 (let ((case-fold-search t)
259 (pos (point-min)))
260 (goto-char pos)
261 (while (re-search-forward "^[ \t]*#\\+BEGIN_SRC" nil t)
262 (let ((element (save-match-data (org-element-at-point))))
263 (when (eq (org-element-type element) 'src-block)
264 (let* ((match-start (copy-marker (match-beginning 0)))
265 (begin (copy-marker (org-element-property :begin element)))
266 ;; Make sure we don't remove any blank lines after
267 ;; the block when replacing it.
268 (block-end (save-excursion
269 (goto-char (org-element-property :end element))
270 (skip-chars-backward " \r\t\n")
271 (copy-marker (line-end-position))))
272 (ind (org-get-indentation))
273 (headers
274 (cons
275 (org-element-property :language element)
276 (let ((params (org-element-property :parameters element)))
277 (and params (org-split-string params "[ \t]+"))))))
278 ;; Execute all non-block elements between POS and
279 ;; current block.
280 (org-babel-exp-non-block-elements pos begin)
281 ;; Take care of matched block: compute replacement
282 ;; string. In particular, a nil REPLACEMENT means the
283 ;; block should be left as-is while an empty string
284 ;; should remove the block.
285 (let ((replacement (progn (goto-char match-start)
286 (org-babel-exp-src-block headers))))
287 (cond ((not replacement) (goto-char block-end))
288 ((equal replacement "")
289 (delete-region begin
290 (progn (goto-char block-end)
291 (skip-chars-forward " \r\t\n")
292 (if (eobp) (point)
293 (line-beginning-position)))))
295 (goto-char match-start)
296 (delete-region (point) block-end)
297 (insert replacement)
298 (if (org-element-property :preserve-indent element)
299 ;; Indent only the code block markers.
300 (save-excursion (skip-chars-backward " \r\t\n")
301 (indent-line-to ind)
302 (goto-char match-start)
303 (indent-line-to ind))
304 ;; Indent everything.
305 (indent-rigidly match-start (point) ind)))))
306 (setq pos (line-beginning-position))
307 ;; Cleanup markers.
308 (set-marker match-start nil)
309 (set-marker begin nil)
310 (set-marker block-end nil)))))
311 ;; Eventually execute all non-block Babel elements between last
312 ;; src-block and end of buffer.
313 (org-babel-exp-non-block-elements pos (point-max)))))
315 (defun org-babel-in-example-or-verbatim ()
316 "Return true if point is in example or verbatim code.
317 Example and verbatim code include escaped portions of
318 an org-mode buffer code that should be treated as normal
319 org-mode text."
320 (or (save-match-data
321 (save-excursion
322 (goto-char (point-at-bol))
323 (looking-at "[ \t]*:[ \t]")))
324 (org-in-verbatim-emphasis)
325 (org-in-block-p org-list-forbidden-blocks)
326 (org-between-regexps-p "^[ \t]*#\\+begin_src" "^[ \t]*#\\+end_src")))
328 (defun org-babel-exp-do-export (info type &optional hash)
329 "Return a string with the exported content of a code block.
330 The function respects the value of the :exports header argument."
331 (let ((silently (lambda () (let ((session (cdr (assoc :session (nth 2 info)))))
332 (when (not (and session (equal "none" session)))
333 (org-babel-exp-results info type 'silent)))))
334 (clean (lambda () (unless (eq type 'inline) (org-babel-remove-result info)))))
335 (case (intern (or (cdr (assoc :exports (nth 2 info))) "code"))
336 ('none (funcall silently) (funcall clean) "")
337 ('code (funcall silently) (funcall clean) (org-babel-exp-code info))
338 ('results (org-babel-exp-results info type nil hash) "")
339 ('both (org-babel-exp-results info type nil hash)
340 (org-babel-exp-code info)))))
342 (defcustom org-babel-exp-code-template
343 "#+BEGIN_SRC %lang%flags\n%body\n#+END_SRC"
344 "Template used to export the body of code blocks.
345 This template may be customized to include additional information
346 such as the code block name, or the values of particular header
347 arguments. The template is filled out using `org-fill-template',
348 and the following %keys may be used.
350 lang ------ the language of the code block
351 name ------ the name of the code block
352 body ------ the body of the code block
353 flags ----- the flags passed to the code block
355 In addition to the keys mentioned above, every header argument
356 defined for the code block may be used as a key and will be
357 replaced with its value."
358 :group 'org-babel
359 :type 'string)
361 (defun org-babel-exp-code (info)
362 "Return the original code block formatted for export."
363 (setf (nth 1 info)
364 (if (string= "strip-export" (cdr (assoc :noweb (nth 2 info))))
365 (replace-regexp-in-string
366 (org-babel-noweb-wrap) "" (nth 1 info))
367 (if (org-babel-noweb-p (nth 2 info) :export)
368 (org-babel-expand-noweb-references
369 info (org-babel-exp-get-export-buffer))
370 (nth 1 info))))
371 (org-fill-template
372 org-babel-exp-code-template
373 `(("lang" . ,(nth 0 info))
374 ("body" . ,(org-escape-code-in-string (nth 1 info)))
375 ,@(mapcar (lambda (pair)
376 (cons (substring (symbol-name (car pair)) 1)
377 (format "%S" (cdr pair))))
378 (nth 2 info))
379 ("flags" . ,(let ((f (nth 3 info))) (when f (concat " " f))))
380 ("name" . ,(or (nth 4 info) "")))))
382 (defun org-babel-exp-results (info type &optional silent hash)
383 "Evaluate and return the results of the current code block for export.
384 Results are prepared in a manner suitable for export by org-mode.
385 This function is called by `org-babel-exp-do-export'. The code
386 block will be evaluated. Optional argument SILENT can be used to
387 inhibit insertion of results into the buffer."
388 (when (and (or (eq org-export-babel-evaluate t)
389 (and (eq type 'inline)
390 (eq org-export-babel-evaluate 'inline-only)))
391 (not (and hash (equal hash (org-babel-current-result-hash)))))
392 (let ((lang (nth 0 info))
393 (body (if (org-babel-noweb-p (nth 2 info) :eval)
394 (org-babel-expand-noweb-references
395 info (org-babel-exp-get-export-buffer))
396 (nth 1 info)))
397 (info (copy-sequence info))
398 (org-babel-current-src-block-location (point-marker)))
399 ;; skip code blocks which we can't evaluate
400 (when (fboundp (intern (concat "org-babel-execute:" lang)))
401 (org-babel-eval-wipe-error-buffer)
402 (prog1 nil
403 (setf (nth 1 info) body)
404 (setf (nth 2 info)
405 (org-babel-exp-in-export-file lang
406 (org-babel-process-params
407 (org-babel-merge-params
408 (nth 2 info)
409 `((:results . ,(if silent "silent" "replace")))))))
410 (cond
411 ((equal type 'block)
412 (org-babel-execute-src-block nil info))
413 ((equal type 'inline)
414 ;; position the point on the inline source block allowing
415 ;; `org-babel-insert-result' to check that the block is
416 ;; inline
417 (re-search-backward "[ \f\t\n\r\v]" nil t)
418 (re-search-forward org-babel-inline-src-block-regexp nil t)
419 (re-search-backward "src_" nil t)
420 (org-babel-execute-src-block nil info))
421 ((equal type 'lob)
422 (save-excursion
423 (re-search-backward org-babel-lob-one-liner-regexp nil t)
424 (let (org-confirm-babel-evaluate)
425 (org-babel-execute-src-block nil info))))))))))
428 (provide 'ob-exp)
430 ;;; ob-exp.el ends here