org-odt.el: Rework embedding of images
[org-mode/org-jambu.git] / lisp / ob-exp.el
blobc6ae85ffe40d767d07488e92f50614ae789b9c9c
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-buffer)
109 (org-babel-params-from-properties lang)
110 (if (boundp lang-headers) (eval lang-headers) nil)
111 raw-params))))
112 (setf hash (org-babel-sha1-hash info)))
113 ;; expand noweb references in the original file
114 (setf (nth 1 info)
115 (if (and (cdr (assoc :noweb (nth 2 info)))
116 (string= "yes" (cdr (assoc :noweb (nth 2 info)))))
117 (org-babel-expand-noweb-references
118 info (get-file-buffer org-current-export-file))
119 (nth 1 info)))
120 (org-babel-exp-do-export info 'block hash)))))
122 (defun org-babel-exp-inline-src-blocks (start end)
123 "Process inline source blocks between START and END for export.
124 See `org-babel-exp-src-block' for export options, currently the
125 options and are taken from `org-babel-default-inline-header-args'."
126 (interactive)
127 (save-excursion
128 (goto-char start)
129 (while (and (< (point) end)
130 (re-search-forward org-babel-inline-src-block-regexp end t))
131 (let* ((info (save-match-data (org-babel-parse-inline-src-block-match)))
132 (params (nth 2 info)))
133 (save-match-data
134 (goto-char (match-beginning 2))
135 (unless (org-babel-in-example-or-verbatim)
136 ;; expand noweb references in the original file
137 (setf (nth 1 info)
138 (if (and (cdr (assoc :noweb params))
139 (string= "yes" (cdr (assoc :noweb params))))
140 (org-babel-expand-noweb-references
141 info (get-file-buffer org-current-export-file))
142 (nth 1 info)))
143 (let ((code-replacement (save-match-data
144 (org-babel-exp-do-export info 'inline))))
145 (if code-replacement
146 (replace-match code-replacement nil nil nil 1)
147 (org-babel-examplize-region (match-beginning 1) (match-end 1))
148 (forward-char 2)))))))))
150 (defun org-exp-res/src-name-cleanup ()
151 "Clean up #+results and #+srcname lines for export.
152 This function should only be called after all block processing
153 has taken place."
154 (interactive)
155 (save-excursion
156 (goto-char (point-min))
157 (while (org-re-search-forward-unprotected
158 (concat
159 "\\("org-babel-src-name-regexp"\\|"org-babel-result-regexp"\\)")
160 nil t)
161 (delete-region
162 (progn (beginning-of-line) (point))
163 (progn (end-of-line) (+ 1 (point)))))))
165 (defun org-babel-in-example-or-verbatim ()
166 "Return true if point is in example or verbatim code.
167 Example and verbatim code include escaped portions of
168 an org-mode buffer code that should be treated as normal
169 org-mode text."
170 (or (org-in-indented-comment-line)
171 (save-match-data
172 (save-excursion
173 (goto-char (point-at-bol))
174 (looking-at "[ \t]*:[ \t]")))
175 (org-in-verbatim-emphasis)
176 (org-between-regexps-p "^[ \t]*#\\+begin_src" "^[ \t]*#\\+end_src")))
178 (defvar org-babel-default-lob-header-args)
179 (defun org-babel-exp-lob-one-liners (start end)
180 "Process Library of Babel calls between START and END for export.
181 See `org-babel-exp-src-block' for export options. Currently the
182 options are taken from `org-babel-default-header-args'."
183 (interactive)
184 (save-excursion
185 (goto-char start)
186 (while (and (< (point) end)
187 (re-search-forward org-babel-lob-one-liner-regexp nil t))
188 (unless (and (match-string 12) (org-babel-in-example-or-verbatim))
189 (let* ((lob-info (org-babel-lob-get-info))
190 (inlinep (match-string 11))
191 (inline-start (match-end 11))
192 (inline-end (match-end 0))
193 (rep (let ((lob-info (org-babel-lob-get-info)))
194 (save-match-data
195 (org-babel-exp-do-export
196 (list "emacs-lisp" "results"
197 (org-babel-merge-params
198 org-babel-default-header-args
199 org-babel-default-lob-header-args
200 (org-babel-params-from-buffer)
201 (org-babel-params-from-properties)
202 (org-babel-parse-header-arguments
203 (org-babel-clean-text-properties
204 (concat ":var results="
205 (mapconcat #'identity
206 (butlast lob-info) " ")))))
207 "" nil (car (last lob-info)))
208 'lob)))))
209 (setq end (+ end (- (length rep)
210 (- (length (match-string 0))
211 (length (or (match-string 11) ""))))))
212 (if inlinep
213 (save-excursion
214 (goto-char inline-start)
215 (delete-region inline-start inline-end)
216 (insert rep))
217 (replace-match rep t t)))))))
219 (defun org-babel-exp-do-export (info type &optional hash)
220 "Return a string with the exported content of a code block.
221 The function respects the value of the :exports header argument."
222 (flet ((silently () (let ((session (cdr (assoc :session (nth 2 info)))))
223 (when (not (and session (equal "none" session)))
224 (org-babel-exp-results info type 'silent))))
225 (clean () (unless (eq type 'inline) (org-babel-remove-result info))))
226 (case (intern (or (cdr (assoc :exports (nth 2 info))) "code"))
227 ('none (silently) (clean) "")
228 ('code (silently) (clean) (org-babel-exp-code info))
229 ('results (org-babel-exp-results info type nil hash) "")
230 ('both (org-babel-exp-results info type nil hash)
231 (org-babel-exp-code info)))))
233 (defun org-babel-exp-code (info)
234 "Return the original code block formatted for export."
235 (org-fill-template
236 "#+BEGIN_SRC %lang%flags\n%body\n#+END_SRC"
237 `(("lang" . ,(nth 0 info))
238 ("flags" . ,((lambda (f) (when f (concat " " f))) (nth 3 info)))
239 ("body" . ,(nth 1 info)))))
241 (defun org-babel-exp-results (info type &optional silent hash)
242 "Evaluate and return the results of the current code block for export.
243 Results are prepared in a manner suitable for export by org-mode.
244 This function is called by `org-babel-exp-do-export'. The code
245 block will be evaluated. Optional argument SILENT can be used to
246 inhibit insertion of results into the buffer."
247 (when (and org-export-babel-evaluate
248 (not (and hash (equal hash (org-babel-current-result-hash)))))
249 (let ((lang (nth 0 info))
250 (body (nth 1 info)))
251 ;; skip code blocks which we can't evaluate
252 (when (fboundp (intern (concat "org-babel-execute:" lang)))
253 (org-babel-eval-wipe-error-buffer)
254 (prog1 nil
255 (setf (nth 2 info)
256 (org-babel-exp-in-export-file lang
257 (org-babel-process-params
258 (org-babel-merge-params
259 (nth 2 info)
260 `((:results . ,(if silent "silent" "replace")))))))
261 (cond
262 ((equal type 'block)
263 (org-babel-execute-src-block nil info))
264 ((equal type 'inline)
265 ;; position the point on the inline source block allowing
266 ;; `org-babel-insert-result' to check that the block is
267 ;; inline
268 (re-search-backward "[ \f\t\n\r\v]" nil t)
269 (re-search-forward org-babel-inline-src-block-regexp nil t)
270 (re-search-backward "src_" nil t)
271 (org-babel-execute-src-block nil info))
272 ((equal type 'lob)
273 (save-excursion
274 (re-search-backward org-babel-lob-one-liner-regexp nil t)
275 (org-babel-execute-src-block nil info)))))))))
277 (provide 'ob-exp)
281 ;;; ob-exp.el ends here