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