updated testing/README.org describing how to get started writing tests
[org-mode/org-jambu.git] / lisp / ob-exp.el
blob3004c4a953f34b9d7e835d754b37b8256fe412c0
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.01trans
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 evaluated 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* ((raw-header (match-string 3))
99 (info (org-babel-get-src-block-info))
100 (lang (nth 0 info))
101 (lang-headers
102 (intern (concat "org-babel-default-header-args:" lang)))
103 (raw-params
104 (org-babel-parse-header-arguments
105 (org-babel-clean-text-properties
106 (mapconcat #'identity (cdr (split-string raw-header)) " "))))
107 (heading (nth 4 (ignore-errors (org-heading-components))))
108 (link (when org-current-export-file
109 (org-make-link-string
110 (if heading
111 (concat org-current-export-file "::" heading)
112 org-current-export-file))))
113 (export-buffer (current-buffer)))
114 ;; bail if we couldn't get any info from the block
115 (when info
116 (when link
117 ;; resolve parameters in the original file so that headline
118 ;; and file-wide parameters are included
119 ;; attempt to go to the same heading in the original file
120 (set-buffer (get-file-buffer org-current-export-file))
121 (save-restriction
122 (condition-case nil
123 (org-open-link-from-string link)
124 (error (when heading
125 (goto-char (point-min))
126 (re-search-forward (regexp-quote heading) nil t))))
127 (setf (nth 2 info)
128 (org-babel-merge-params
129 org-babel-default-header-args
130 (org-babel-params-from-buffer)
131 (org-babel-params-from-properties lang)
132 (if (boundp lang-headers) (eval lang-headers) nil)
133 raw-params)))
134 (set-buffer export-buffer))
135 ;; expand noweb references in the original file
136 (setf (nth 1 info)
137 (if (and (cdr (assoc :noweb (nth 2 info)))
138 (string= "yes" (cdr (assoc :noweb (nth 2 info)))))
139 (org-babel-expand-noweb-references
140 info (get-file-buffer org-current-export-file))
141 (nth 1 info))))
142 (org-babel-exp-do-export info 'block))))
144 (defun org-babel-exp-inline-src-blocks (start end)
145 "Process inline source blocks between START and END for export.
146 See `org-babel-exp-src-blocks' for export options, currently the
147 options and are taken from `org-babel-default-inline-header-args'."
148 (interactive)
149 (save-excursion
150 (goto-char start)
151 (while (and (< (point) end)
152 (re-search-forward org-babel-inline-src-block-regexp end t))
153 (let* ((info (save-match-data (org-babel-parse-inline-src-block-match)))
154 (params (nth 2 info))
155 (replacement
156 (save-match-data
157 (if (org-babel-in-example-or-verbatim)
158 (buffer-substring (match-beginning 0) (match-end 0))
159 ;; expand noweb references in the original file
160 (setf (nth 1 info)
161 (if (and (cdr (assoc :noweb params))
162 (string= "yes" (cdr (assoc :noweb params))))
163 (org-babel-expand-noweb-references
164 info (get-file-buffer org-current-export-file))
165 (nth 1 info)))
166 (org-babel-exp-do-export info 'inline)))))
167 (setq end (+ end (- (length replacement) (length (match-string 1)))))
168 (replace-match replacement t t nil 1)))))
170 (defun org-exp-res/src-name-cleanup ()
171 "Clean up #+results and #+srcname lines for export.
172 This function should only be called after all block processing
173 has taken place."
174 (interactive)
175 (save-excursion
176 (goto-char (point-min))
177 (while (org-re-search-forward-unprotected
178 (concat
179 "\\("org-babel-src-name-regexp"\\|"org-babel-result-regexp"\\)")
180 nil t)
181 (delete-region
182 (progn (beginning-of-line) (point))
183 (progn (end-of-line) (+ 1 (point)))))))
185 (defun org-babel-in-example-or-verbatim ()
186 "Return true if point is in example or verbatim code.
187 Example and verbatim code include escaped portions of
188 an org-mode buffer code that should be treated as normal
189 org-mode text."
190 (or (org-in-indented-comment-line)
191 (save-excursion
192 (save-match-data
193 (goto-char (point-at-bol))
194 (looking-at "[ \t]*:[ \t]")))
195 (org-in-regexps-block-p "^[ \t]*#\\+begin_src" "^[ \t]*#\\+end_src")))
197 (defun org-babel-exp-lob-one-liners (start end)
198 "Process Library of Babel calls between START and END for export.
199 See `org-babel-exp-src-blocks' for export options. Currently the
200 options are taken from `org-babel-default-header-args'."
201 (interactive)
202 (let (replacement)
203 (save-excursion
204 (goto-char start)
205 (while (and (< (point) end)
206 (re-search-forward org-babel-lob-one-liner-regexp nil t))
207 (setq replacement
208 (let ((lob-info (org-babel-lob-get-info)))
209 (save-match-data
210 (org-babel-exp-do-export
211 (list "emacs-lisp" "results"
212 (org-babel-merge-params
213 org-babel-default-header-args
214 (org-babel-params-from-buffer)
215 (org-babel-params-from-properties)
216 (org-babel-parse-header-arguments
217 (org-babel-clean-text-properties
218 (concat ":var results="
219 (mapconcat #'identity
220 (butlast lob-info) " ")))))
221 (car (last lob-info)))
222 'lob))))
223 (setq end (+ end (- (length replacement) (length (match-string 0)))))
224 (replace-match replacement t t)))))
226 (defun org-babel-exp-do-export (info type)
227 "Return a string with the exported content of a code block.
228 The function respects the value of the :exports header argument."
229 (flet ((silently () (let ((session (cdr (assoc :session (nth 2 info)))))
230 (when (and session
231 (not (equal "none" session)))
232 (org-babel-exp-results info type 'silent))))
233 (clean () (org-babel-remove-result info)))
234 (case (intern (or (cdr (assoc :exports (nth 2 info))) "code"))
235 ('none (silently) (clean) "")
236 ('code (silently) (clean) (org-babel-exp-code info type))
237 ('results (org-babel-exp-results info type))
238 ('both (concat (org-babel-exp-code info type)
239 "\n\n"
240 (org-babel-exp-results info type))))))
242 (defvar backend)
243 (defun org-babel-exp-code (info type)
244 "Prepare and return code in the current code block for export.
245 Code is prepared in a manner suitable for export by
246 org-mode. This function is called by `org-babel-exp-do-export'.
247 The code block is not evaluated."
248 (let ((lang (nth 0 info))
249 (body (nth 1 info))
250 (switches (nth 3 info))
251 (name (nth 4 info))
252 (args (mapcar
253 #'cdr
254 (org-remove-if-not (lambda (el) (eq :var (car el))) (nth 2 info)))))
255 (case type
256 ('inline (format "=%s=" body))
257 ('block
258 (let ((str
259 (format "#+BEGIN_SRC %s %s\n%s%s#+END_SRC\n" lang switches body
260 (if (and body (string-match "\n$" body))
261 "" "\n"))))
262 (when name
263 (add-text-properties
264 0 (length str)
265 (list 'org-caption
266 (format "%s(%s)"
267 name
268 (mapconcat #'identity args ", ")))
269 str))
270 str))
271 ('lob
272 (let ((call-line (and (string-match "results=" (car args))
273 (substring (car args) (match-end 0)))))
274 (cond
275 ((eq backend 'html)
276 (format "\n#+HTML: <label class=\"org-src-name\">%s</label>\n"
277 call-line))
278 ((format ": %s\n" call-line))))))))
280 (defun org-babel-exp-results (info type &optional silent)
281 "Evaluate and return the results of the current code block for export.
282 Results are prepared in a manner suitable for export by org-mode.
283 This function is called by `org-babel-exp-do-export'. The code
284 block will be evaluated. Optional argument SILENT can be used to
285 inhibit insertion of results into the buffer."
286 (if org-export-babel-evaluate
287 (let ((lang (nth 0 info))
288 (body (nth 1 info))
289 (params
290 ;; lets ensure that we lookup references in the original file
291 (mapcar
292 (lambda (pair)
293 (if (and org-current-export-file
294 (eq (car pair) :var)
295 (string-match org-babel-ref-split-regexp (cdr pair))
296 (equal :ob-must-be-reference
297 (org-babel-ref-literal
298 (match-string 2 (cdr pair)))))
299 `(:var . ,(concat (match-string 1 (cdr pair))
300 "=" org-current-export-file
301 ":" (match-string 2 (cdr pair))))
302 pair))
303 (nth 2 info))))
304 ;; skip code blocks which we can't evaluate
305 (if (fboundp (intern (concat "org-babel-execute:" lang)))
306 (case type
307 ('inline
308 (let ((raw (org-babel-execute-src-block
309 nil info '((:results . "silent"))))
310 (result-params (split-string
311 (cdr (assoc :results params)))))
312 (unless silent
313 (cond ;; respect the value of the :results header argument
314 ((member "file" result-params)
315 (org-babel-result-to-file raw))
316 ((or (member "raw" result-params)
317 (member "org" result-params))
318 (format "%s" raw))
319 ((member "code" result-params)
320 (format "src_%s{%s}" lang raw))
322 (if (stringp raw)
323 (if (= 0 (length raw)) "=(no results)="
324 (format "%s" raw))
325 (format "%S" raw)))))))
326 ('block
327 (org-babel-execute-src-block
328 nil info (org-babel-merge-params
329 params
330 `((:results . ,(if silent "silent" "replace")))))
332 ('lob
333 (save-excursion
334 (re-search-backward org-babel-lob-one-liner-regexp nil t)
335 (org-babel-execute-src-block
336 nil info (org-babel-merge-params
337 params
338 `((:results . ,(if silent "silent" "replace")))))
339 "")))
340 ""))
341 ""))
343 (provide 'ob-exp)
345 ;; arch-tag: 523abf4c-76d1-44ed-9f27-e3bddf34bf0f
347 ;;; ob-exp.el ends here