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