test-ob-exp: fix failing test
[org-mode/org-tableheadings.git] / lisp / ob-exp.el
blob761c9f17ad02a7021bf908676ccc99597596bdeb
1 ;;; ob-exp.el --- Exportation of org-babel source blocks
3 ;; Copyright (C) 2009-2014 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-current-export-file)
32 (defvar org-babel-lob-one-liner-regexp)
33 (defvar org-babel-ref-split-regexp)
34 (defvar org-list-forbidden-blocks)
36 (declare-function org-babel-lob-get-info "ob-lob" ())
37 (declare-function org-babel-eval-wipe-error-buffer "ob-eval" ())
38 (declare-function org-between-regexps-p "org"
39 (start-re end-re &optional lim-up lim-down))
40 (declare-function org-get-indentation "org" (&optional line))
41 (declare-function org-heading-components "org" ())
42 (declare-function org-in-block-p "org" (names))
43 (declare-function org-in-verbatim-emphasis "org" ())
44 (declare-function org-link-search "org" (s &optional type avoid-pos stealth))
45 (declare-function org-fill-template "org" (template alist))
46 (declare-function org-split-string "org" (string &optional separators))
47 (declare-function org-element-at-point "org-element" (&optional keep-trail))
48 (declare-function org-element-context "org-element" ())
49 (declare-function org-element-property "org-element" (property element))
50 (declare-function org-element-type "org-element" (element))
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 (defun org-babel-exp-get-export-buffer ()
66 "Return the current export buffer if possible."
67 (cond
68 ((bufferp org-current-export-file) org-current-export-file)
69 (org-current-export-file (get-file-buffer org-current-export-file))
70 ('otherwise
71 (error "Requested export buffer when `org-current-export-file' is nil"))))
73 (defvar org-link-search-inhibit-query)
75 (defmacro org-babel-exp-in-export-file (lang &rest body)
76 (declare (indent 1))
77 `(let* ((lang-headers (intern (concat "org-babel-default-header-args:" ,lang)))
78 (heading (nth 4 (ignore-errors (org-heading-components))))
79 (export-buffer (current-buffer))
80 (original-buffer (org-babel-exp-get-export-buffer)) results)
81 (when original-buffer
82 ;; resolve parameters in the original file so that
83 ;; headline and file-wide parameters are included, attempt
84 ;; to go to the same heading in the original file
85 (set-buffer original-buffer)
86 (save-restriction
87 (when heading
88 (condition-case nil
89 (let ((org-link-search-inhibit-query t))
90 (org-link-search heading))
91 (error (when heading
92 (goto-char (point-min))
93 (re-search-forward (regexp-quote heading) 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 it's 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 (unless noninteractive (message "org-babel-exp processing..."))
117 (save-excursion
118 (let* ((info (org-babel-get-src-block-info 'light))
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 (when info
123 ;; if we're actually going to need the parameters
124 (when (member (cdr (assoc :exports (nth 2 info))) '("both" "results"))
125 (org-babel-exp-in-export-file lang
126 (setf (nth 2 info)
127 (org-babel-process-params
128 (apply #'org-babel-merge-params
129 org-babel-default-header-args
130 (if (boundp lang-headers) (eval lang-headers) nil)
131 (append (org-babel-params-from-properties lang)
132 (list raw-params))))))
133 (setf hash (org-babel-sha1-hash info)))
134 (org-babel-exp-do-export info 'block hash)))))
136 (defcustom org-babel-exp-call-line-template
138 "Template used to export call lines.
139 This template may be customized to include the call line name
140 with any export markup. The template is filled out using
141 `org-fill-template', and the following %keys may be used.
143 line --- call line
145 An example value would be \"\\n: call: %line\" to export the call line
146 wrapped in a verbatim environment.
148 Note: the results are inserted separately after the contents of
149 this template."
150 :group 'org-babel
151 :type 'string)
153 (defvar org-babel-default-lob-header-args)
154 (defun org-babel-exp-process-buffer ()
155 "Execute all Babel blocks in current buffer."
156 (interactive)
157 (save-window-excursion
158 (save-excursion
159 (let ((case-fold-search t)
160 (regexp (concat org-babel-inline-src-block-regexp "\\|"
161 org-babel-lob-one-liner-regexp "\\|"
162 "^[ \t]*#\\+BEGIN_SRC")))
163 (goto-char (point-min))
164 (while (re-search-forward regexp nil t)
165 (let* ((element (save-excursion
166 ;; If match is inline, point is at its
167 ;; end. Move backward so
168 ;; `org-element-context' can get the
169 ;; object, not the following one.
170 (backward-char)
171 (save-match-data (org-element-context))))
172 (type (org-element-type element))
173 (begin (copy-marker (org-element-property :begin element)))
174 (end (copy-marker
175 (save-excursion
176 (goto-char (org-element-property :end element))
177 (skip-chars-backward " \r\t\n")
178 (point)))))
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 begin)
190 (let ((replacement (org-babel-exp-do-export info 'inline)))
191 (if (equal replacement "")
192 ;; Replacement code is empty: remove inline src
193 ;; block, including extra white space that
194 ;; might have been created when inserting
195 ;; results.
196 (delete-region begin
197 (progn (goto-char end)
198 (skip-chars-forward " \t")
199 (point)))
200 ;; Otherwise: remove inline src block but
201 ;; preserve following white spaces. Then insert
202 ;; value.
203 (delete-region begin end)
204 (insert replacement)))))
205 ((babel-call inline-babel-call)
206 (let* ((lob-info (org-babel-lob-get-info))
207 (results
208 (org-babel-exp-do-export
209 (list "emacs-lisp" "results"
210 (apply #'org-babel-merge-params
211 org-babel-default-header-args
212 org-babel-default-lob-header-args
213 (append
214 (org-babel-params-from-properties)
215 (list
216 (org-babel-parse-header-arguments
217 (org-no-properties
218 (concat
219 ":var results="
220 (mapconcat 'identity
221 (butlast lob-info 2)
222 " ")))))))
223 "" (nth 3 lob-info) (nth 2 lob-info))
224 'lob))
225 (rep (org-fill-template
226 org-babel-exp-call-line-template
227 `(("line" . ,(nth 0 lob-info))))))
228 ;; If replacement is empty, completely remove the
229 ;; object/element, including any extra white space
230 ;; that might have been created when including
231 ;; results.
232 (if (equal rep "")
233 (delete-region
234 begin
235 (progn (goto-char end)
236 (if (not (eq type 'babel-call))
237 (progn (skip-chars-forward " \t") (point))
238 (skip-chars-forward " \r\t\n")
239 (line-beginning-position))))
240 ;; Otherwise, preserve following white
241 ;; spaces/newlines and then, insert replacement
242 ;; string.
243 (goto-char begin)
244 (delete-region begin end)
245 (insert rep))))
246 (src-block
247 (let* ((match-start (copy-marker (match-beginning 0)))
248 (ind (org-get-indentation))
249 (headers
250 (cons
251 (org-element-property :language element)
252 (let ((params (org-element-property :parameters
253 element)))
254 (and params (org-split-string params "[ \t]+"))))))
255 ;; Take care of matched block: compute replacement
256 ;; string. In particular, a nil REPLACEMENT means
257 ;; the block should be left as-is while an empty
258 ;; string should remove the block.
259 (let ((replacement (progn (goto-char match-start)
260 (org-babel-exp-src-block headers))))
261 (cond ((not replacement) (goto-char end))
262 ((equal replacement "")
263 (goto-char end)
264 (skip-chars-forward " \r\t\n")
265 (beginning-of-line)
266 (delete-region begin (point)))
268 (goto-char match-start)
269 (delete-region (point)
270 (save-excursion (goto-char end)
271 (line-end-position)))
272 (insert replacement)
273 (if (or org-src-preserve-indentation
274 (org-element-property :preserve-indent
275 element))
276 ;; Indent only the code block markers.
277 (save-excursion (skip-chars-backward " \r\t\n")
278 (indent-line-to ind)
279 (goto-char match-start)
280 (indent-line-to ind))
281 ;; Indent everything.
282 (indent-rigidly match-start (point) ind)))))
283 (set-marker match-start nil))))
284 (set-marker begin nil)
285 (set-marker end nil)))))))
287 (defun org-babel-in-example-or-verbatim ()
288 "Return true if point is in example or verbatim code.
289 Example and verbatim code include escaped portions of
290 an org-mode buffer code that should be treated as normal
291 org-mode text."
292 (or (save-match-data
293 (save-excursion
294 (goto-char (point-at-bol))
295 (looking-at "[ \t]*:[ \t]")))
296 (org-in-verbatim-emphasis)
297 (org-in-block-p org-list-forbidden-blocks)
298 (org-between-regexps-p "^[ \t]*#\\+begin_src" "^[ \t]*#\\+end_src")))
300 (defun org-babel-exp-do-export (info type &optional hash)
301 "Return a string with the exported content of a code block.
302 The function respects the value of the :exports header argument."
303 (let ((silently (lambda () (let ((session (cdr (assoc :session (nth 2 info)))))
304 (when (not (and session (equal "none" session)))
305 (org-babel-exp-results info type 'silent)))))
306 (clean (lambda () (unless (eq type 'inline) (org-babel-remove-result info)))))
307 (case (intern (or (cdr (assoc :exports (nth 2 info))) "code"))
308 ('none (funcall silently) (funcall clean) "")
309 ('code (funcall silently) (funcall clean) (org-babel-exp-code info))
310 ('results (org-babel-exp-results info type nil hash) "")
311 ('both (org-babel-exp-results info type nil hash)
312 (org-babel-exp-code info)))))
314 (defcustom org-babel-exp-code-template
315 "#+BEGIN_SRC %lang%switches%flags\n%body\n#+END_SRC"
316 "Template used to export the body of code blocks.
317 This template may be customized to include additional information
318 such as the code block name, or the values of particular header
319 arguments. The template is filled out using `org-fill-template',
320 and the following %keys may be used.
322 lang ------ the language of the code block
323 name ------ the name of the code block
324 body ------ the body of the code block
325 switches -- the switches associated to the code block
326 flags ----- the flags passed to the code block
328 In addition to the keys mentioned above, every header argument
329 defined for the code block may be used as a key and will be
330 replaced with its value."
331 :group 'org-babel
332 :type 'string)
334 (defun org-babel-exp-code (info)
335 "Return the original code block formatted for export."
336 (setf (nth 1 info)
337 (if (string= "strip-export" (cdr (assoc :noweb (nth 2 info))))
338 (replace-regexp-in-string
339 (org-babel-noweb-wrap) "" (nth 1 info))
340 (if (org-babel-noweb-p (nth 2 info) :export)
341 (org-babel-expand-noweb-references
342 info (org-babel-exp-get-export-buffer))
343 (nth 1 info))))
344 (org-fill-template
345 org-babel-exp-code-template
346 `(("lang" . ,(nth 0 info))
347 ("body" . ,(org-escape-code-in-string (nth 1 info)))
348 ("switches" . ,(let ((f (nth 3 info)))
349 (and (org-string-nw-p f) (concat " " f))))
350 ("flags" . ,(let ((f (assq :flags (nth 2 info))))
351 (and f (concat " " (cdr f)))))
352 ,@(mapcar (lambda (pair)
353 (cons (substring (symbol-name (car pair)) 1)
354 (format "%S" (cdr pair))))
355 (nth 2 info))
356 ("name" . ,(or (nth 4 info) "")))))
358 (defun org-babel-exp-results (info type &optional silent hash)
359 "Evaluate and return the results of the current code block for export.
360 Results are prepared in a manner suitable for export by org-mode.
361 This function is called by `org-babel-exp-do-export'. The code
362 block will be evaluated. Optional argument SILENT can be used to
363 inhibit insertion of results into the buffer."
364 (when (and (or (eq org-export-babel-evaluate t)
365 (and (eq type 'inline)
366 (eq org-export-babel-evaluate 'inline-only)))
367 (not (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-get-export-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 (cond
387 ((equal type 'block)
388 (org-babel-execute-src-block nil info))
389 ((equal type 'inline)
390 ;; position the point on the inline source block allowing
391 ;; `org-babel-insert-result' to check that the block is
392 ;; inline
393 (re-search-backward "[ \f\t\n\r\v]" nil t)
394 (re-search-forward org-babel-inline-src-block-regexp nil t)
395 (re-search-backward "src_" nil t)
396 (org-babel-execute-src-block nil info))
397 ((equal type 'lob)
398 (save-excursion
399 (re-search-backward org-babel-lob-one-liner-regexp nil t)
400 (let (org-confirm-babel-evaluate)
401 (org-babel-execute-src-block nil info))))))))))
404 (provide 'ob-exp)
406 ;;; ob-exp.el ends here