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