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