Release 7.01e
[org-mode/org-jambu.git] / lisp / ob-exp.el
blobf414194ae5abd2c80be7027a132f3f02b62ca4b8
1 ;;; ob-exp.el --- Exportation of org-babel source blocks
3 ;; Copyright (C) 2009, 2010 Free Software Foundation, Inc.
5 ;; Author: Eric Schulte, Dan Davison
6 ;; Keywords: literate programming, reproducible research
7 ;; Homepage: http://orgmode.org
8 ;; Version: 7.01e
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 ;;; Commentary:
27 ;; See the online documentation for more information
28 ;;
29 ;; http://orgmode.org/worg/org-contrib/babel/
31 ;;; Code:
32 (require 'ob)
33 (require 'org-exp-blocks)
34 (eval-when-compile
35 (require 'cl))
37 (defvar obe-marker nil)
38 (defvar org-current-export-file)
39 (defvar org-babel-lob-one-liner-regexp)
40 (defvar org-babel-ref-split-regexp)
41 (declare-function org-babel-lob-get-info "ob-lob" ())
42 (declare-function org-babel-ref-literal "ob-ref" (ref))
44 (add-to-list 'org-export-interblocks '(src org-babel-exp-inline-src-blocks))
45 (add-to-list 'org-export-interblocks '(lob org-babel-exp-lob-one-liners))
46 (add-hook 'org-export-blocks-postblock-hook 'org-exp-res/src-name-cleanup)
48 (org-export-blocks-add-block '(src org-babel-exp-src-blocks nil))
50 (defcustom org-export-babel-evaluate t
51 "Switch controlling code evaluation during export.
52 When set to nil no code will be exported as part of the export
53 process."
54 :group 'org-babel
55 :type 'boolean)
56 (put 'org-export-babel-evaluate 'safe-local-variable (lambda (x) (eq x nil)))
58 (defvar org-babel-function-def-export-keyword "function"
59 "The keyword to substitute for the source name line on export.
60 When exporting a source block function, this keyword will
61 appear in the exported version in the place of source name
62 line. A source block is considered to be a source block function
63 if the source name is present and is followed by a parenthesized
64 argument list. The parentheses may be empty or contain
65 whitespace. An example is the following which generates n random
66 \(uniform) numbers.
68 #+source: rand(n)
69 #+begin_src R
70 runif(n)
71 #+end_src")
73 (defvar org-babel-function-def-export-indent 4
74 "Number of characters to indent a source block on export.
75 When exporting a source block function, the block contents will
76 be indented by this many characters. See
77 `org-babel-function-def-export-name' for the definition of a
78 source block function.")
80 (defun org-babel-exp-src-blocks (body &rest headers)
81 "Process source block for export.
82 Depending on the 'export' headers argument in replace the source
83 code block with...
85 both ---- display the code and the results
87 code ---- the default, display the code inside the block but do
88 not process
90 results - just like none only the block is run on export ensuring
91 that it's results are present in the org-mode buffer
93 none ----- do not display either code or results upon export"
94 (interactive)
95 (message "org-babel-exp processing...")
96 (save-excursion
97 (goto-char (match-beginning 0))
98 (let* ((info (org-babel-get-src-block-info))
99 (params (nth 2 info)))
100 ;; bail if we couldn't get any info from the block
101 (when info
102 ;; expand noweb references in the original file
103 (setf (nth 1 info)
104 (if (and (cdr (assoc :noweb params))
105 (string= "yes" (cdr (assoc :noweb params))))
106 (org-babel-expand-noweb-references
107 info (get-file-buffer org-current-export-file))
108 (nth 1 info))))
109 (org-babel-exp-do-export info 'block))))
111 (defun org-babel-exp-inline-src-blocks (start end)
112 "Process inline source blocks between START and END for export.
113 See `org-babel-exp-src-blocks' for export options, currently the
114 options and are taken from `org-babel-default-inline-header-args'."
115 (interactive)
116 (save-excursion
117 (goto-char start)
118 (while (and (< (point) end)
119 (re-search-forward org-babel-inline-src-block-regexp end t))
120 (let* ((info (save-match-data (org-babel-parse-inline-src-block-match)))
121 (params (nth 2 info))
122 (replacement
123 (save-match-data
124 (if (org-babel-in-example-or-verbatim)
125 (buffer-substring (match-beginning 0) (match-end 0))
126 ;; expand noweb references in the original file
127 (setf (nth 1 info)
128 (if (and (cdr (assoc :noweb params))
129 (string= "yes" (cdr (assoc :noweb params))))
130 (org-babel-expand-noweb-references
131 info (get-file-buffer org-current-export-file))
132 (nth 1 info)))
133 (org-babel-exp-do-export info 'inline)))))
134 (setq end (+ end (- (length replacement) (length (match-string 1)))))
135 (replace-match replacement t t nil 1)))))
137 (defun org-exp-res/src-name-cleanup ()
138 "Clean up #+results and #+srcname lines for export.
139 This function should only be called after all block processing
140 has taken place."
141 (interactive)
142 (save-excursion
143 (goto-char (point-min))
144 (while (org-re-search-forward-unprotected
145 (concat
146 "\\("org-babel-src-name-regexp"\\|"org-babel-result-regexp"\\)")
147 nil t)
148 (delete-region
149 (progn (beginning-of-line) (point))
150 (progn (end-of-line) (+ 1 (point)))))))
152 (defun org-babel-in-example-or-verbatim ()
153 "Return true if point is in example or verbatim code.
154 Example and verbatim code include escaped portions of
155 an org-mode buffer code that should be treated as normal
156 org-mode text."
157 (or (org-in-indented-comment-line)
158 (save-excursion
159 (save-match-data
160 (goto-char (point-at-bol))
161 (looking-at "[ \t]*:[ \t]")))
162 (org-in-regexps-block-p "^[ \t]*#\\+begin_src" "^[ \t]*#\\+end_src")))
164 (defun org-babel-exp-lob-one-liners (start end)
165 "Process Library of Babel calls between START and END for export.
166 See `org-babel-exp-src-blocks' for export options. Currently the
167 options are taken from `org-babel-default-header-args'."
168 (interactive)
169 (let (replacement)
170 (save-excursion
171 (goto-char start)
172 (while (and (< (point) end)
173 (re-search-forward org-babel-lob-one-liner-regexp nil t))
174 (setq replacement
175 (let ((lob-info (org-babel-lob-get-info)))
176 (save-match-data
177 (org-babel-exp-do-export
178 (list "emacs-lisp" "results"
179 (org-babel-merge-params
180 org-babel-default-header-args
181 (org-babel-parse-header-arguments
182 (org-babel-clean-text-properties
183 (concat ":var results="
184 (mapconcat #'identity
185 (butlast lob-info) " ")))))
186 (car (last lob-info)))
187 'lob))))
188 (setq end (+ end (- (length replacement) (length (match-string 0)))))
189 (replace-match replacement t t)))))
191 (defun org-babel-exp-do-export (info type)
192 "Return a string with the exported content of a code block.
193 The function respects the value of the :exports header argument."
194 (flet ((silently () (let ((session (cdr (assoc :session (nth 2 info)))))
195 (when (and session
196 (not (equal "none" session))
197 (not (assoc :noeval (nth 2 info))))
198 (org-babel-exp-results info type 'silent))))
199 (clean () (org-babel-remove-result info)))
200 (case (intern (or (cdr (assoc :exports (nth 2 info))) "code"))
201 ('none (silently) (clean) "")
202 ('code (silently) (clean) (org-babel-exp-code info type))
203 ('results (org-babel-exp-results info type))
204 ('both (concat (org-babel-exp-code info type)
205 "\n\n"
206 (org-babel-exp-results info type))))))
208 (defvar backend)
209 (defun org-babel-exp-code (info type)
210 "Prepare and return code in the current code block for export.
211 Code is prepared in a manner suitable for exportat by
212 org-mode. This function is called by `org-babel-exp-do-export'.
213 The code block is not evaluated."
214 (let ((lang (nth 0 info))
215 (body (nth 1 info))
216 (switches (nth 3 info))
217 (name (nth 4 info))
218 (args (mapcar
219 #'cdr
220 (org-remove-if-not (lambda (el) (eq :var (car el))) (nth 2 info)))))
221 (case type
222 ('inline (format "=%s=" body))
223 ('block
224 (let ((str
225 (format "#+BEGIN_SRC %s %s\n%s%s#+END_SRC\n" lang switches body
226 (if (and body (string-match "\n$" body))
227 "" "\n"))))
228 (when name
229 (add-text-properties
230 0 (length str)
231 (list 'org-caption
232 (format "%s(%s)"
233 name
234 (mapconcat #'identity args ", ")))
235 str))
236 str))
237 ('lob
238 (let ((call-line (and (string-match "results=" (car args))
239 (substring (car args) (match-end 0)))))
240 (cond
241 ((eq backend 'html)
242 (format "\n#+HTML: <label class=\"org-src-name\">%s</label>\n"
243 call-line))
244 ((format ": %s\n" call-line))))))))
246 (defun org-babel-exp-results (info type &optional silent)
247 "Evaluate and return the results of the current code block for export.
248 Results are prepared in a manner suitable for export by org-mode.
249 This function is called by `org-babel-exp-do-export'. The code
250 block will be evaluated. Optional argument SILENT can be used to
251 inhibit insertion of results into the buffer."
252 (if org-export-babel-evaluate
253 (let ((lang (nth 0 info))
254 (body (nth 1 info))
255 (params
256 ;; lets ensure that we lookup references in the original file
257 (mapcar
258 (lambda (pair)
259 (if (and org-current-export-file
260 (eq (car pair) :var)
261 (string-match org-babel-ref-split-regexp (cdr pair))
262 (equal :ob-must-be-reference
263 (org-babel-ref-literal
264 (match-string 2 (cdr pair)))))
265 `(:var . ,(concat (match-string 1 (cdr pair))
266 "=" org-current-export-file
267 ":" (match-string 2 (cdr pair))))
268 pair))
269 (nth 2 info))))
270 ;; skip code blocks which we can't evaluate
271 (if (fboundp (intern (concat "org-babel-execute:" lang)))
272 (case type
273 ('inline
274 (let ((raw (org-babel-execute-src-block
275 nil info '((:results . "silent"))))
276 (result-params (split-string
277 (cdr (assoc :results params)))))
278 (unless silent
279 (cond ;; respect the value of the :results header argument
280 ((member "file" result-params)
281 (org-babel-result-to-file raw))
282 ((or (member "raw" result-params)
283 (member "org" result-params))
284 (format "%s" raw))
285 ((member "code" result-params)
286 (format "src_%s{%s}" lang raw))
288 (if (stringp raw)
289 (if (= 0 (length raw)) "=(no results)="
290 (format "%s" raw))
291 (format "%S" raw)))))))
292 ('block
293 (org-babel-execute-src-block
294 nil info (org-babel-merge-params
295 params
296 `((:results . ,(if silent "silent" "replace")))))
298 ('lob
299 (save-excursion
300 (re-search-backward org-babel-lob-one-liner-regexp nil t)
301 (org-babel-execute-src-block
302 nil info (org-babel-merge-params
303 params
304 `((:results . ,(if silent "silent" "replace")))))
305 "")))
306 ""))
307 ""))
309 (provide 'ob-exp)
311 ;; arch-tag: 523abf4c-76d1-44ed-9f27-e3bddf34bf0f
313 ;;; ob-exp.el ends here