Timestamp fixes for undo.
[emacs.git] / lisp / org / ob-exp.el
blob37a9f71cf591beae0a44df9c9dd1dbdb52a4f629
1 ;;; ob-exp.el --- Exportation of org-babel source blocks
3 ;; Copyright (C) 2009-2013 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)
27 (require 'org-exp-blocks)
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-heading-components "org" ())
39 (declare-function org-link-search "org" (s &optional type avoid-pos stealth))
40 (declare-function org-fill-template "org" (template alist))
41 (declare-function org-in-verbatim-emphasis "org" ())
42 (declare-function org-in-block-p "org" (names))
43 (declare-function org-between-regexps-p "org" (start-re end-re &optional lim-up lim-down))
45 (add-to-list 'org-export-interblocks '(src org-babel-exp-non-block-elements))
46 (org-export-blocks-add-block '(src org-babel-exp-src-block nil))
48 (defcustom org-export-babel-evaluate t
49 "Switch controlling code evaluation during export.
50 When set to nil no code will be evaluated as part of the export
51 process."
52 :group 'org-babel
53 :version "24.1"
54 :type 'boolean)
55 (put 'org-export-babel-evaluate 'safe-local-variable (lambda (x) (eq x nil)))
57 (defun org-babel-exp-get-export-buffer ()
58 "Return the current export buffer if possible."
59 (cond
60 ((bufferp org-current-export-file) org-current-export-file)
61 (org-current-export-file (get-file-buffer org-current-export-file))
62 ('otherwise
63 (error "Requested export buffer when `org-current-export-file' is nil"))))
65 (defmacro org-babel-exp-in-export-file (lang &rest body)
66 (declare (indent 1))
67 `(let* ((lang-headers (intern (concat "org-babel-default-header-args:" ,lang)))
68 (heading (nth 4 (ignore-errors (org-heading-components))))
69 (export-buffer (current-buffer))
70 (original-buffer (org-babel-exp-get-export-buffer)) results)
71 (when original-buffer
72 ;; resolve parameters in the original file so that
73 ;; headline and file-wide parameters are included, attempt
74 ;; to go to the same heading in the original file
75 (set-buffer original-buffer)
76 (save-restriction
77 (when heading
78 (condition-case nil
79 (let ((org-link-search-inhibit-query t))
80 (org-link-search heading))
81 (error (when heading
82 (goto-char (point-min))
83 (re-search-forward (regexp-quote heading) nil t)))))
84 (setq results ,@body))
85 (set-buffer export-buffer)
86 results)))
87 (def-edebug-spec org-babel-exp-in-export-file (form body))
89 (defun org-babel-exp-src-block (body &rest headers)
90 "Process source block for export.
91 Depending on the 'export' headers argument in replace the source
92 code block with...
94 both ---- display the code and the results
96 code ---- the default, display the code inside the block but do
97 not process
99 results - just like none only the block is run on export ensuring
100 that it's results are present in the org-mode buffer
102 none ----- do not display either code or results upon export"
103 (interactive)
104 (unless noninteractive (message "org-babel-exp processing..."))
105 (save-excursion
106 (goto-char (match-beginning 0))
107 (let* ((info (org-babel-get-src-block-info 'light))
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 (when info
112 ;; if we're actually going to need the parameters
113 (when (member (cdr (assoc :exports (nth 2 info))) '("both" "results"))
114 (org-babel-exp-in-export-file lang
115 (setf (nth 2 info)
116 (org-babel-process-params
117 (org-babel-merge-params
118 org-babel-default-header-args
119 (org-babel-params-from-properties lang)
120 (if (boundp lang-headers) (eval lang-headers) nil)
121 raw-params))))
122 (setf hash (org-babel-sha1-hash info)))
123 (org-babel-exp-do-export info 'block hash)))))
125 (defcustom org-babel-exp-call-line-template
127 "Template used to export call lines.
128 This template may be customized to include the call line name
129 with any export markup. The template is filled out using
130 `org-fill-template', and the following %keys may be used.
132 line --- call line
134 An example value would be \"\\n: call: %line\" to export the call line
135 wrapped in a verbatim environment.
137 Note: the results are inserted separately after the contents of
138 this template."
139 :group 'org-babel
140 :type 'string)
142 (defvar org-babel-default-lob-header-args)
143 (defun org-babel-exp-non-block-elements (start end)
144 "Process inline source and call lines between START and END for export."
145 (interactive)
146 (save-excursion
147 (goto-char start)
148 (unless (markerp end)
149 (let ((m (make-marker)))
150 (set-marker m end (current-buffer))
151 (setq end m)))
152 (let ((rx (concat "\\(" org-babel-inline-src-block-regexp
153 "\\|" org-babel-lob-one-liner-regexp "\\)")))
154 (while (and (< (point) (marker-position end))
155 (re-search-forward rx end t))
156 (if (save-excursion
157 (goto-char (match-beginning 0))
158 (looking-at org-babel-inline-src-block-regexp))
159 (progn
160 (forward-char 1)
161 (let* ((info (save-match-data
162 (org-babel-parse-inline-src-block-match)))
163 (params (nth 2 info)))
164 (save-match-data
165 (goto-char (match-beginning 2))
166 (unless (org-babel-in-example-or-verbatim)
167 ;; expand noweb references in the original file
168 (setf (nth 1 info)
169 (if (and (cdr (assoc :noweb params))
170 (string= "yes" (cdr (assoc :noweb params))))
171 (org-babel-expand-noweb-references
172 info (org-babel-exp-get-export-buffer))
173 (nth 1 info)))
174 (let ((code-replacement (save-match-data
175 (org-babel-exp-do-export
176 info 'inline))))
177 (if code-replacement
178 (progn (replace-match code-replacement nil nil nil 1)
179 (delete-char 1))
180 (org-babel-examplize-region (match-beginning 1)
181 (match-end 1))
182 (forward-char 2)))))))
183 (unless (org-babel-in-example-or-verbatim)
184 (let* ((lob-info (org-babel-lob-get-info))
185 (inlinep (match-string 11))
186 (inline-start (match-end 11))
187 (inline-end (match-end 0))
188 (results (save-match-data
189 (org-babel-exp-do-export
190 (list "emacs-lisp" "results"
191 (org-babel-merge-params
192 org-babel-default-header-args
193 org-babel-default-lob-header-args
194 (org-babel-params-from-properties)
195 (org-babel-parse-header-arguments
196 (org-no-properties
197 (concat ":var results="
198 (mapconcat #'identity
199 (butlast lob-info)
200 " ")))))
201 "" nil (car (last lob-info)))
202 'lob)))
203 (rep (org-fill-template
204 org-babel-exp-call-line-template
205 `(("line" . ,(nth 0 lob-info))))))
206 (if inlinep
207 (save-excursion
208 (goto-char inline-start)
209 (delete-region inline-start inline-end)
210 (insert rep))
211 (replace-match rep t t)))))))))
213 (defun org-babel-in-example-or-verbatim ()
214 "Return true if point is in example or verbatim code.
215 Example and verbatim code include escaped portions of
216 an org-mode buffer code that should be treated as normal
217 org-mode text."
218 (or (save-match-data
219 (save-excursion
220 (goto-char (point-at-bol))
221 (looking-at "[ \t]*:[ \t]")))
222 (org-in-verbatim-emphasis)
223 (org-in-block-p org-list-forbidden-blocks)
224 (org-between-regexps-p "^[ \t]*#\\+begin_src" "^[ \t]*#\\+end_src")))
226 (defun org-babel-exp-do-export (info type &optional hash)
227 "Return a string with the exported content of a code block.
228 The function respects the value of the :exports header argument."
229 (let ((silently (lambda () (let ((session (cdr (assoc :session (nth 2 info)))))
230 (when (not (and session (equal "none" session)))
231 (org-babel-exp-results info type 'silent)))))
232 (clean (lambda () (unless (eq type 'inline) (org-babel-remove-result info)))))
233 (case (intern (or (cdr (assoc :exports (nth 2 info))) "code"))
234 ('none (funcall silently) (funcall clean) "")
235 ('code (funcall silently) (funcall clean) (org-babel-exp-code info))
236 ('results (org-babel-exp-results info type nil hash) "")
237 ('both (org-babel-exp-results info type nil hash)
238 (org-babel-exp-code info)))))
240 (defcustom org-babel-exp-code-template
241 "#+BEGIN_SRC %lang%flags\n%body\n#+END_SRC"
242 "Template used to export the body of code blocks.
243 This template may be customized to include additional information
244 such as the code block name, or the values of particular header
245 arguments. The template is filled out using `org-fill-template',
246 and the following %keys may be used.
248 lang ------ the language of the code block
249 name ------ the name of the code block
250 body ------ the body of the code block
251 flags ----- the flags passed to the code block
253 In addition to the keys mentioned above, every header argument
254 defined for the code block may be used as a key and will be
255 replaced with its value."
256 :group 'org-babel
257 :type 'string)
259 (defun org-babel-exp-code (info)
260 "Return the original code block formatted for export."
261 (setf (nth 1 info)
262 (if (string= "strip-export" (cdr (assoc :noweb (nth 2 info))))
263 (replace-regexp-in-string
264 (org-babel-noweb-wrap) "" (nth 1 info))
265 (if (org-babel-noweb-p (nth 2 info) :export)
266 (org-babel-expand-noweb-references
267 info (org-babel-exp-get-export-buffer))
268 (nth 1 info))))
269 (org-fill-template
270 org-babel-exp-code-template
271 `(("lang" . ,(nth 0 info))
272 ("body" . ,(if (string= (nth 0 info) "org")
273 (replace-regexp-in-string "^" "," (nth 1 info))
274 (nth 1 info)))
275 ,@(mapcar (lambda (pair)
276 (cons (substring (symbol-name (car pair)) 1)
277 (format "%S" (cdr pair))))
278 (nth 2 info))
279 ("flags" . ,((lambda (f) (when f (concat " " f))) (nth 3 info)))
280 ("name" . ,(or (nth 4 info) "")))))
282 (defun org-babel-exp-results (info type &optional silent hash)
283 "Evaluate and return the results of the current code block for export.
284 Results are prepared in a manner suitable for export by org-mode.
285 This function is called by `org-babel-exp-do-export'. The code
286 block will be evaluated. Optional argument SILENT can be used to
287 inhibit insertion of results into the buffer."
288 (when (and org-export-babel-evaluate
289 (not (and hash (equal hash (org-babel-current-result-hash)))))
290 (let ((lang (nth 0 info))
291 (body (if (org-babel-noweb-p (nth 2 info) :eval)
292 (org-babel-expand-noweb-references
293 info (org-babel-exp-get-export-buffer))
294 (nth 1 info)))
295 (info (copy-sequence info)))
296 ;; skip code blocks which we can't evaluate
297 (when (fboundp (intern (concat "org-babel-execute:" lang)))
298 (org-babel-eval-wipe-error-buffer)
299 (prog1 nil
300 (setf (nth 1 info) body)
301 (setf (nth 2 info)
302 (org-babel-exp-in-export-file lang
303 (org-babel-process-params
304 (org-babel-merge-params
305 (nth 2 info)
306 `((:results . ,(if silent "silent" "replace")))))))
307 (cond
308 ((equal type 'block)
309 (org-babel-execute-src-block nil info))
310 ((equal type 'inline)
311 ;; position the point on the inline source block allowing
312 ;; `org-babel-insert-result' to check that the block is
313 ;; inline
314 (re-search-backward "[ \f\t\n\r\v]" nil t)
315 (re-search-forward org-babel-inline-src-block-regexp nil t)
316 (re-search-backward "src_" nil t)
317 (org-babel-execute-src-block nil info))
318 ((equal type 'lob)
319 (save-excursion
320 (re-search-backward org-babel-lob-one-liner-regexp nil t)
321 (org-babel-execute-src-block nil info)))))))))
323 (provide 'ob-exp)
327 ;;; ob-exp.el ends here