Fix typos in ChangeLogs.
[bpt/emacs.git] / lisp / org / ob-exp.el
CommitLineData
86fbb8ca
CD
1;;; ob-exp.el --- Exportation of org-babel source blocks
2
ab422c4d 3;; Copyright (C) 2009-2013 Free Software Foundation, Inc.
86fbb8ca 4
dfd98937 5;; Authors: Eric Schulte
c7557a0f 6;; Dan Davison
86fbb8ca
CD
7;; Keywords: literate programming, reproducible research
8;; Homepage: http://orgmode.org
86fbb8ca
CD
9
10;; This file is part of GNU Emacs.
11
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.
16
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.
21
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/>.
24
86fbb8ca
CD
25;;; Code:
26(require 'ob)
27(require 'org-exp-blocks)
28(eval-when-compile
29 (require 'cl))
30
86fbb8ca
CD
31(defvar org-current-export-file)
32(defvar org-babel-lob-one-liner-regexp)
33(defvar org-babel-ref-split-regexp)
8223b1d2
BG
34(defvar org-list-forbidden-blocks)
35
86fbb8ca 36(declare-function org-babel-lob-get-info "ob-lob" ())
acedf35c 37(declare-function org-babel-eval-wipe-error-buffer "ob-eval" ())
8223b1d2
BG
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))
86fbb8ca 44
8223b1d2 45(add-to-list 'org-export-interblocks '(src org-babel-exp-non-block-elements))
3ab2c837 46(org-export-blocks-add-block '(src org-babel-exp-src-block nil))
86fbb8ca
CD
47
48(defcustom org-export-babel-evaluate t
49 "Switch controlling code evaluation during export.
afe98dfa 50When set to nil no code will be evaluated as part of the export
86fbb8ca
CD
51process."
52 :group 'org-babel
372d7b21 53 :version "24.1"
86fbb8ca
CD
54 :type 'boolean)
55(put 'org-export-babel-evaluate 'safe-local-variable (lambda (x) (eq x nil)))
56
8223b1d2
BG
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"))))
64
3ab2c837
BG
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)))
afe98dfa 68 (heading (nth 4 (ignore-errors (org-heading-components))))
8223b1d2
BG
69 (export-buffer (current-buffer))
70 (original-buffer (org-babel-exp-get-export-buffer)) results)
71 (when original-buffer
afe98dfa
CD
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
8223b1d2 75 (set-buffer original-buffer)
afe98dfa 76 (save-restriction
8223b1d2
BG
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)))))
afe98dfa
CD
84 (setq results ,@body))
85 (set-buffer export-buffer)
86 results)))
e66ba1df 87(def-edebug-spec org-babel-exp-in-export-file (form body))
afe98dfa 88
3ab2c837 89(defun org-babel-exp-src-block (body &rest headers)
86fbb8ca
CD
90 "Process source block for export.
91Depending on the 'export' headers argument in replace the source
92code block with...
93
94both ---- display the code and the results
95
96code ---- the default, display the code inside the block but do
97 not process
98
99results - just like none only the block is run on export ensuring
100 that it's results are present in the org-mode buffer
101
102none ----- do not display either code or results upon export"
103 (interactive)
3ab2c837 104 (unless noninteractive (message "org-babel-exp processing..."))
86fbb8ca
CD
105 (save-excursion
106 (goto-char (match-beginning 0))
afe98dfa
CD
107 (let* ((info (org-babel-get-src-block-info 'light))
108 (lang (nth 0 info))
3ab2c837 109 (raw-params (nth 2 info)) hash)
86fbb8ca
CD
110 ;; bail if we couldn't get any info from the block
111 (when info
3ab2c837
BG
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
3ab2c837
BG
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)))
3ab2c837 123 (org-babel-exp-do-export info 'block hash)))))
86fbb8ca 124
8223b1d2
BG
125(defcustom org-babel-exp-call-line-template
126 ""
127 "Template used to export call lines.
128This template may be customized to include the call line name
129with any export markup. The template is filled out using
130`org-fill-template', and the following %keys may be used.
131
132 line --- call line
133
134An example value would be \"\\n: call: %line\" to export the call line
135wrapped in a verbatim environment.
136
137Note: the results are inserted separately after the contents of
138this template."
139 :group 'org-babel
140 :type 'string)
141
153ae947
BG
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."
86fbb8ca
CD
145 (interactive)
146 (save-excursion
147 (goto-char start)
153ae947
BG
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
8223b1d2 172 info (org-babel-exp-get-export-buffer))
153ae947
BG
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)))))))
e66ba1df 183 (unless (org-babel-in-example-or-verbatim)
153ae947
BG
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))
8223b1d2
BG
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))))))
153ae947
BG
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)))))))))
86fbb8ca
CD
212
213(defun org-babel-in-example-or-verbatim ()
214 "Return true if point is in example or verbatim code.
215Example and verbatim code include escaped portions of
216an org-mode buffer code that should be treated as normal
217org-mode text."
e66ba1df 218 (or (save-match-data
3ab2c837 219 (save-excursion
86fbb8ca
CD
220 (goto-char (point-at-bol))
221 (looking-at "[ \t]*:[ \t]")))
3ab2c837 222 (org-in-verbatim-emphasis)
e66ba1df
BG
223 (org-in-block-p org-list-forbidden-blocks)
224 (org-between-regexps-p "^[ \t]*#\\+begin_src" "^[ \t]*#\\+end_src")))
86fbb8ca 225
3ab2c837 226(defun org-babel-exp-do-export (info type &optional hash)
86fbb8ca
CD
227 "Return a string with the exported content of a code block.
228The function respects the value of the :exports header argument."
8223b1d2
BG
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)))))
86fbb8ca 233 (case (intern (or (cdr (assoc :exports (nth 2 info))) "code"))
8223b1d2
BG
234 ('none (funcall silently) (funcall clean) "")
235 ('code (funcall silently) (funcall clean) (org-babel-exp-code info))
3ab2c837
BG
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)))))
86fbb8ca 239
8223b1d2
BG
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.
243This template may be customized to include additional information
244such as the code block name, or the values of particular header
245arguments. The template is filled out using `org-fill-template',
246and the following %keys may be used.
247
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
252
253In addition to the keys mentioned above, every header argument
254defined for the code block may be used as a key and will be
255replaced with its value."
256 :group 'org-babel
257 :type 'string)
258
3ab2c837
BG
259(defun org-babel-exp-code (info)
260 "Return the original code block formatted for export."
8223b1d2
BG
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))))
3ab2c837 269 (org-fill-template
8223b1d2 270 org-babel-exp-code-template
3ab2c837 271 `(("lang" . ,(nth 0 info))
153ae947
BG
272 ("body" . ,(if (string= (nth 0 info) "org")
273 (replace-regexp-in-string "^" "," (nth 1 info))
8223b1d2
BG
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) "")))))
86fbb8ca 281
3ab2c837 282(defun org-babel-exp-results (info type &optional silent hash)
86fbb8ca
CD
283 "Evaluate and return the results of the current code block for export.
284Results are prepared in a manner suitable for export by org-mode.
285This function is called by `org-babel-exp-do-export'. The code
286block will be evaluated. Optional argument SILENT can be used to
287inhibit insertion of results into the buffer."
3ab2c837
BG
288 (when (and org-export-babel-evaluate
289 (not (and hash (equal hash (org-babel-current-result-hash)))))
290 (let ((lang (nth 0 info))
8223b1d2
BG
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)))
3ab2c837
BG
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
8223b1d2 300 (setf (nth 1 info) body)
3ab2c837
BG
301 (setf (nth 2 info)
302 (org-babel-exp-in-export-file lang
303 (org-babel-process-params
afe98dfa
CD
304 (org-babel-merge-params
305 (nth 2 info)
3ab2c837
BG
306 `((:results . ,(if silent "silent" "replace")))))))
307 (cond
e66ba1df
BG
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)
3ab2c837
BG
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)))))))))
86fbb8ca
CD
322
323(provide 'ob-exp)
324
5b409b39 325
86fbb8ca
CD
326
327;;; ob-exp.el ends here