* doc/org.texi: More changes to :var table (some examples were wrong)
[org-mode.git] / lisp / ob-exp.el
blob2c82aa7882ee2d4c3ee83ab1222a2d55e97ba79b
1 ;;; ob-exp.el --- Exportation of org-babel source blocks
3 ;; Copyright (C) 2009-2011 Free Software Foundation, Inc.
5 ;; Author: 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 (declare-function org-babel-lob-get-info "ob-lob" ())
36 (declare-function org-babel-eval-wipe-error-buffer "ob-eval" ())
37 (add-to-list 'org-export-interblocks '(src org-babel-exp-inline-src-blocks))
38 (add-to-list 'org-export-interblocks '(lob org-babel-exp-lob-one-liners))
39 (add-hook 'org-export-blocks-postblock-hook 'org-exp-res/src-name-cleanup)
41 (org-export-blocks-add-block '(src org-babel-exp-src-block nil))
43 (defcustom org-export-babel-evaluate t
44 "Switch controlling code evaluation during export.
45 When set to nil no code will be evaluated as part of the export
46 process."
47 :group 'org-babel
48 :type 'boolean)
49 (put 'org-export-babel-evaluate 'safe-local-variable (lambda (x) (eq x nil)))
51 (defmacro org-babel-exp-in-export-file (lang &rest body)
52 (declare (indent 1))
53 `(let* ((lang-headers (intern (concat "org-babel-default-header-args:" ,lang)))
54 (heading (nth 4 (ignore-errors (org-heading-components))))
55 (link (when org-current-export-file
56 (org-make-link-string
57 (if heading
58 (concat org-current-export-file "::" heading)
59 org-current-export-file))))
60 (export-buffer (current-buffer)) results)
61 (when link
62 ;; resolve parameters in the original file so that
63 ;; headline and file-wide parameters are included, attempt
64 ;; to go to the same heading in the original file
65 (set-buffer (get-file-buffer org-current-export-file))
66 (save-restriction
67 (condition-case nil
68 (let ((org-link-search-inhibit-query t))
69 (org-open-link-from-string link))
70 (error (when heading
71 (goto-char (point-min))
72 (re-search-forward (regexp-quote heading) nil t))))
73 (setq results ,@body))
74 (set-buffer export-buffer)
75 results)))
76 (def-edebug-spec org-babel-exp-in-export-file (form body))
78 (defun org-babel-exp-src-block (body &rest headers)
79 "Process source block for export.
80 Depending on the 'export' headers argument in replace the source
81 code block with...
83 both ---- display the code and the results
85 code ---- the default, display the code inside the block but do
86 not process
88 results - just like none only the block is run on export ensuring
89 that it's results are present in the org-mode buffer
91 none ----- do not display either code or results upon export"
92 (interactive)
93 (unless noninteractive (message "org-babel-exp processing..."))
94 (save-excursion
95 (goto-char (match-beginning 0))
96 (let* ((info (org-babel-get-src-block-info 'light))
97 (lang (nth 0 info))
98 (raw-params (nth 2 info)) hash)
99 ;; bail if we couldn't get any info from the block
100 (when info
101 ;; if we're actually going to need the parameters
102 (when (member (cdr (assoc :exports (nth 2 info))) '("both" "results"))
103 (org-babel-exp-in-export-file lang
104 (setf (nth 2 info)
105 (org-babel-process-params
106 (org-babel-merge-params
107 org-babel-default-header-args
108 (org-babel-params-from-properties lang)
109 (if (boundp lang-headers) (eval lang-headers) nil)
110 raw-params))))
111 (setf hash (org-babel-sha1-hash info)))
112 ;; expand noweb references in the original file
113 (setf (nth 1 info)
114 (if (and (cdr (assoc :noweb (nth 2 info)))
115 (string= "yes" (cdr (assoc :noweb (nth 2 info)))))
116 (org-babel-expand-noweb-references
117 info (get-file-buffer org-current-export-file))
118 (nth 1 info)))
119 (org-babel-exp-do-export info 'block hash)))))
121 (defun org-babel-exp-inline-src-blocks (start end)
122 "Process inline source blocks between START and END for export.
123 See `org-babel-exp-src-block' for export options, currently the
124 options and are taken from `org-babel-default-inline-header-args'."
125 (interactive)
126 (save-excursion
127 (goto-char start)
128 (while (and (< (point) end)
129 (re-search-forward org-babel-inline-src-block-regexp end t))
130 (let* ((info (save-match-data (org-babel-parse-inline-src-block-match)))
131 (params (nth 2 info)))
132 (save-match-data
133 (goto-char (match-beginning 2))
134 (unless (org-babel-in-example-or-verbatim)
135 ;; expand noweb references in the original file
136 (setf (nth 1 info)
137 (if (and (cdr (assoc :noweb params))
138 (string= "yes" (cdr (assoc :noweb params))))
139 (org-babel-expand-noweb-references
140 info (get-file-buffer org-current-export-file))
141 (nth 1 info)))
142 (let ((code-replacement (save-match-data
143 (org-babel-exp-do-export info 'inline))))
144 (if code-replacement
145 (replace-match code-replacement nil nil nil 1)
146 (org-babel-examplize-region (match-beginning 1) (match-end 1))
147 (forward-char 2)))))))))
149 (defun org-exp-res/src-name-cleanup ()
150 "Clean up #+results and #+name lines for export.
151 This function should only be called after all block processing
152 has taken place."
153 (interactive)
154 (save-excursion
155 (goto-char (point-min))
156 (while (org-re-search-forward-unprotected
157 (concat
158 "\\("org-babel-src-name-regexp"\\|"org-babel-result-regexp"\\)")
159 nil t)
160 (delete-region
161 (progn (beginning-of-line) (point))
162 (progn (end-of-line) (+ 1 (point)))))))
164 (defun org-babel-in-example-or-verbatim ()
165 "Return true if point is in example or verbatim code.
166 Example and verbatim code include escaped portions of
167 an org-mode buffer code that should be treated as normal
168 org-mode text."
169 (or (save-match-data
170 (save-excursion
171 (goto-char (point-at-bol))
172 (looking-at "[ \t]*:[ \t]")))
173 (org-in-verbatim-emphasis)
174 (org-in-block-p org-list-forbidden-blocks)
175 (org-between-regexps-p "^[ \t]*#\\+begin_src" "^[ \t]*#\\+end_src")))
177 (defvar org-babel-default-lob-header-args)
178 (defun org-babel-exp-lob-one-liners (start end)
179 "Process Library of Babel calls between START and END for export.
180 See `org-babel-exp-src-block' for export options. Currently the
181 options are taken from `org-babel-default-header-args'."
182 (interactive)
183 (save-excursion
184 (goto-char start)
185 (while (and (< (point) end)
186 (re-search-forward org-babel-lob-one-liner-regexp nil t))
187 (unless (and (match-string 12) (org-babel-in-example-or-verbatim))
188 (let* ((lob-info (org-babel-lob-get-info))
189 (inlinep (match-string 11))
190 (inline-start (match-end 11))
191 (inline-end (match-end 0))
192 (rep (let ((lob-info (org-babel-lob-get-info)))
193 (save-match-data
194 (org-babel-exp-do-export
195 (list "emacs-lisp" "results"
196 (org-babel-merge-params
197 org-babel-default-header-args
198 org-babel-default-lob-header-args
199 (org-babel-params-from-properties)
200 (org-babel-parse-header-arguments
201 (org-babel-clean-text-properties
202 (concat ":var results="
203 (mapconcat #'identity
204 (butlast lob-info) " ")))))
205 "" nil (car (last lob-info)))
206 'lob)))))
207 (setq end (+ end (- (length rep)
208 (- (length (match-string 0))
209 (length (or (match-string 11) ""))))))
210 (if inlinep
211 (save-excursion
212 (goto-char inline-start)
213 (delete-region inline-start inline-end)
214 (insert rep))
215 (replace-match rep t t)))))))
217 (defun org-babel-exp-do-export (info type &optional hash)
218 "Return a string with the exported content of a code block.
219 The function respects the value of the :exports header argument."
220 (flet ((silently () (let ((session (cdr (assoc :session (nth 2 info)))))
221 (when (not (and session (equal "none" session)))
222 (org-babel-exp-results info type 'silent))))
223 (clean () (unless (eq type 'inline) (org-babel-remove-result info))))
224 (case (intern (or (cdr (assoc :exports (nth 2 info))) "code"))
225 ('none (silently) (clean) "")
226 ('code (silently) (clean) (org-babel-exp-code info))
227 ('results (org-babel-exp-results info type nil hash) "")
228 ('both (org-babel-exp-results info type nil hash)
229 (org-babel-exp-code info)))))
231 (defun org-babel-exp-code (info)
232 "Return the original code block formatted for export."
233 (org-fill-template
234 "#+BEGIN_SRC %lang%flags\n%body\n#+END_SRC"
235 `(("lang" . ,(nth 0 info))
236 ("flags" . ,((lambda (f) (when f (concat " " f))) (nth 3 info)))
237 ("body" . ,(nth 1 info)))))
239 (defun org-babel-exp-results (info type &optional silent hash)
240 "Evaluate and return the results of the current code block for export.
241 Results are prepared in a manner suitable for export by org-mode.
242 This function is called by `org-babel-exp-do-export'. The code
243 block will be evaluated. Optional argument SILENT can be used to
244 inhibit insertion of results into the buffer."
245 (when (and org-export-babel-evaluate
246 (not (and hash (equal hash (org-babel-current-result-hash)))))
247 (let ((lang (nth 0 info))
248 (body (nth 1 info)))
249 ;; skip code blocks which we can't evaluate
250 (when (fboundp (intern (concat "org-babel-execute:" lang)))
251 (org-babel-eval-wipe-error-buffer)
252 (prog1 nil
253 (setf (nth 2 info)
254 (org-babel-exp-in-export-file lang
255 (org-babel-process-params
256 (org-babel-merge-params
257 (nth 2 info)
258 `((:results . ,(if silent "silent" "replace")))))))
259 (cond
260 ((equal type 'block)
261 (org-babel-execute-src-block nil info))
262 ((equal type 'inline)
263 ;; position the point on the inline source block allowing
264 ;; `org-babel-insert-result' to check that the block is
265 ;; inline
266 (re-search-backward "[ \f\t\n\r\v]" nil t)
267 (re-search-forward org-babel-inline-src-block-regexp nil t)
268 (re-search-backward "src_" nil t)
269 (org-babel-execute-src-block nil info))
270 ((equal type 'lob)
271 (save-excursion
272 (re-search-backward org-babel-lob-one-liner-regexp nil t)
273 (org-babel-execute-src-block nil info)))))))))
275 (provide 'ob-exp)
279 ;;; ob-exp.el ends here