TODO in ob-exp.el
[org-mode/org-tableheadings.git] / lisp / ob-exp.el
blob783892d92d3fa3a2dadf763aced6307a6edd9d0c
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-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 (nth 4 (ignore-errors (org-heading-components))))
70 (export-buffer (current-buffer))
71 results)
72 (when org-babel-exp-reference-buffer
73 ;; Resolve parameters in the original file so that headline and
74 ;; file-wide parameters are included, attempt to go to the same
75 ;; heading in the original file
76 (set-buffer org-babel-exp-reference-buffer)
77 (save-restriction
78 (when heading
79 (condition-case nil
80 (let ((org-link-search-inhibit-query t))
81 ;; TODO: When multiple headings have the same title,
82 ;; this returns the first, which is not always
83 ;; the right heading. Consider a better way to
84 ;; find the proper heading.
85 (org-link-search heading))
86 (error (when heading
87 (goto-char (point-min))
88 (re-search-forward (regexp-quote heading) nil t)))))
89 (setq results ,@body))
90 (set-buffer export-buffer)
91 results)))
92 (def-edebug-spec org-babel-exp-in-export-file (form body))
94 (defun org-babel-exp-src-block (&rest headers)
95 "Process source block for export.
96 Depending on the 'export' headers argument, replace the source
97 code block like this:
99 both ---- display the code and the results
101 code ---- the default, display the code inside the block but do
102 not process
104 results - just like none only the block is run on export ensuring
105 that it's results are present in the org-mode buffer
107 none ---- do not display either code or results upon export
109 Assume point is at the beginning of block's starting line."
110 (interactive)
111 (save-excursion
112 (let* ((info (org-babel-get-src-block-info 'light))
113 (line (org-current-line))
114 (lang (nth 0 info))
115 (raw-params (nth 2 info)) hash)
116 ;; bail if we couldn't get any info from the block
117 (unless noninteractive
118 (message "org-babel-exp process %s at line %d..." lang line))
119 (when info
120 ;; if we're actually going to need the parameters
121 (when (member (cdr (assoc :exports (nth 2 info))) '("both" "results"))
122 (org-babel-exp-in-export-file lang
123 (setf (nth 2 info)
124 (org-babel-process-params
125 (apply #'org-babel-merge-params
126 org-babel-default-header-args
127 (if (boundp lang-headers) (eval lang-headers) nil)
128 (append (org-babel-params-from-properties lang)
129 (list raw-params))))))
130 (setf hash (org-babel-sha1-hash info)))
131 (org-babel-exp-do-export info 'block hash)))))
133 (defcustom org-babel-exp-call-line-template
135 "Template used to export call lines.
136 This template may be customized to include the call line name
137 with any export markup. The template is filled out using
138 `org-fill-template', and the following %keys may be used.
140 line --- call line
142 An example value would be \"\\n: call: %line\" to export the call line
143 wrapped in a verbatim environment.
145 Note: the results are inserted separately after the contents of
146 this template."
147 :group 'org-babel
148 :type 'string)
150 (defvar org-babel-default-lob-header-args)
151 (defun org-babel-exp-process-buffer (reference-buffer)
152 "Execute all Babel blocks in current buffer.
153 REFERENCE-BUFFER is the buffer containing a pristine copy of the
154 buffer being processed. It is used to properly resolve
155 references in source blocks, as modifications in current buffer
156 may make them unreachable."
157 (interactive)
158 (save-window-excursion
159 (save-excursion
160 (let ((case-fold-search t)
161 (org-babel-exp-reference-buffer reference-buffer)
162 (regexp (concat org-babel-inline-src-block-regexp "\\|"
163 org-babel-lob-one-liner-regexp "\\|"
164 "^[ \t]*#\\+BEGIN_SRC")))
165 (goto-char (point-min))
166 (while (re-search-forward regexp nil t)
167 (unless (save-match-data (org-in-commented-heading-p))
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 (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-parse-inline-src-block-match))
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 space
197 ;; 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))
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 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 following white
244 ;; spaces/newlines and then, insert replacement
245 ;; string.
246 (goto-char begin)
247 (delete-region begin end)
248 (insert rep))))
249 (src-block
250 (let* ((match-start (copy-marker (match-beginning 0)))
251 (ind (org-get-indentation))
252 (headers
253 (cons
254 (org-element-property :language element)
255 (let ((params (org-element-property :parameters
256 element)))
257 (and params (org-split-string params "[ \t]+"))))))
258 ;; Take care of matched block: compute replacement
259 ;; string. In particular, a nil REPLACEMENT means
260 ;; the block should be left as-is while an empty
261 ;; string should remove the block.
262 (let ((replacement
263 (progn (goto-char match-start)
264 (org-babel-exp-src-block headers))))
265 (cond ((not replacement) (goto-char end))
266 ((equal replacement "")
267 (goto-char end)
268 (skip-chars-forward " \r\t\n")
269 (beginning-of-line)
270 (delete-region begin (point)))
272 (goto-char match-start)
273 (delete-region (point)
274 (save-excursion (goto-char end)
275 (line-end-position)))
276 (insert replacement)
277 (if (or org-src-preserve-indentation
278 (org-element-property :preserve-indent
279 element))
280 ;; Indent only the code block markers.
281 (save-excursion (skip-chars-backward " \r\t\n")
282 (indent-line-to ind)
283 (goto-char match-start)
284 (indent-line-to ind))
285 ;; Indent everything.
286 (indent-rigidly match-start (point) ind)))))
287 (set-marker match-start nil))))
288 (set-marker begin nil)
289 (set-marker end nil))))))))
291 (defun org-babel-in-example-or-verbatim ()
292 "Return true if point is in example or verbatim code.
293 Example and verbatim code include escaped portions of
294 an org-mode buffer code that should be treated as normal
295 org-mode text."
296 (or (save-match-data
297 (save-excursion
298 (goto-char (point-at-bol))
299 (looking-at "[ \t]*:[ \t]")))
300 (org-in-verbatim-emphasis)
301 (org-in-block-p org-list-forbidden-blocks)
302 (org-between-regexps-p "^[ \t]*#\\+begin_src" "^[ \t]*#\\+end_src")))
304 (defun org-babel-exp-do-export (info type &optional hash)
305 "Return a string with the exported content of a code block.
306 The function respects the value of the :exports header argument."
307 (let ((silently (lambda () (let ((session (cdr (assoc :session (nth 2 info)))))
308 (when (not (and session (equal "none" session)))
309 (org-babel-exp-results info type 'silent)))))
310 (clean (lambda () (unless (eq type 'inline) (org-babel-remove-result info)))))
311 (case (intern (or (cdr (assoc :exports (nth 2 info))) "code"))
312 ('none (funcall silently) (funcall clean) "")
313 ('code (funcall silently) (funcall clean) (org-babel-exp-code info))
314 ('results (org-babel-exp-results info type nil hash) "")
315 ('both (org-babel-exp-results info type nil hash)
316 (org-babel-exp-code info)))))
318 (defcustom org-babel-exp-code-template
319 "#+BEGIN_SRC %lang%switches%flags\n%body\n#+END_SRC"
320 "Template used to export the body of code blocks.
321 This template may be customized to include additional information
322 such as the code block name, or the values of particular header
323 arguments. The template is filled out using `org-fill-template',
324 and the following %keys may be used.
326 lang ------ the language of the code block
327 name ------ the name of the code block
328 body ------ the body of the code block
329 switches -- the switches associated to the code block
330 flags ----- the flags passed to the code block
332 In addition to the keys mentioned above, every header argument
333 defined for the code block may be used as a key and will be
334 replaced with its value."
335 :group 'org-babel
336 :type 'string)
338 (defun org-babel-exp-code (info)
339 "Return the original code block formatted for export."
340 (setf (nth 1 info)
341 (if (string= "strip-export" (cdr (assoc :noweb (nth 2 info))))
342 (replace-regexp-in-string
343 (org-babel-noweb-wrap) "" (nth 1 info))
344 (if (org-babel-noweb-p (nth 2 info) :export)
345 (org-babel-expand-noweb-references
346 info org-babel-exp-reference-buffer)
347 (nth 1 info))))
348 (org-fill-template
349 org-babel-exp-code-template
350 `(("lang" . ,(nth 0 info))
351 ("body" . ,(org-escape-code-in-string (nth 1 info)))
352 ("switches" . ,(let ((f (nth 3 info)))
353 (and (org-string-nw-p f) (concat " " f))))
354 ("flags" . ,(let ((f (assq :flags (nth 2 info))))
355 (and f (concat " " (cdr f)))))
356 ,@(mapcar (lambda (pair)
357 (cons (substring (symbol-name (car pair)) 1)
358 (format "%S" (cdr pair))))
359 (nth 2 info))
360 ("name" . ,(or (nth 4 info) "")))))
362 (defun org-babel-exp-results (info type &optional silent hash)
363 "Evaluate and return the results of the current code block for export.
364 Results are prepared in a manner suitable for export by org-mode.
365 This function is called by `org-babel-exp-do-export'. The code
366 block will be evaluated. Optional argument SILENT can be used to
367 inhibit insertion of results into the buffer."
368 (when (and (or (eq org-export-babel-evaluate t)
369 (and (eq type 'inline)
370 (eq org-export-babel-evaluate 'inline-only)))
371 (not (and hash (equal hash (org-babel-current-result-hash)))))
372 (let ((lang (nth 0 info))
373 (body (if (org-babel-noweb-p (nth 2 info) :eval)
374 (org-babel-expand-noweb-references
375 info org-babel-exp-reference-buffer)
376 (nth 1 info)))
377 (info (copy-sequence info))
378 (org-babel-current-src-block-location (point-marker)))
379 ;; skip code blocks which we can't evaluate
380 (when (fboundp (intern (concat "org-babel-execute:" lang)))
381 (org-babel-eval-wipe-error-buffer)
382 (prog1 nil
383 (setf (nth 1 info) body)
384 (setf (nth 2 info)
385 (org-babel-exp-in-export-file lang
386 (org-babel-process-params
387 (org-babel-merge-params
388 (nth 2 info)
389 `((:results . ,(if silent "silent" "replace")))))))
390 (cond
391 ((equal type 'block)
392 (org-babel-execute-src-block nil info))
393 ((equal type 'inline)
394 ;; position the point on the inline source block allowing
395 ;; `org-babel-insert-result' to check that the block is
396 ;; inline
397 (re-search-backward "[ \f\t\n\r\v]" nil t)
398 (re-search-forward org-babel-inline-src-block-regexp nil t)
399 (re-search-backward "src_" nil t)
400 (org-babel-execute-src-block nil info))
401 ((equal type 'lob)
402 (save-excursion
403 (re-search-backward org-babel-lob-one-liner-regexp nil t)
404 (let (org-confirm-babel-evaluate)
405 (org-babel-execute-src-block nil info))))))))))
408 (provide 'ob-exp)
410 ;;; ob-exp.el ends here