babel: enhanced code block movement functions
[org-mode.git] / lisp / ob-exp.el
bloba715abb5c33b167b3492a50bb52b7f2ff64184ce
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: 0.01
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 "When exporting a source block function, this keyword will
60 appear in the exported version in the place of source name
61 line. A source block is considered to be a source block function
62 if the source name is present and is followed by a parenthesized
63 argument list. The parentheses may be empty or contain
64 whitespace. An example is the following which generates n random
65 \(uniform) numbers.
67 #+source: rand(n)
68 #+begin_src R
69 runif(n)
70 #+end_src")
72 (defvar org-babel-function-def-export-indent 4
73 "When exporting a source block function, the block contents
74 will be indented by this many characters. See
75 `org-babel-function-def-export-name' for the definition of a
76 source block function.")
78 (defun org-babel-exp-src-blocks (body &rest headers)
79 "Process src block for export. Depending on the 'export'
80 headers argument in replace the source code block with...
82 both ---- display the code and the results
84 code ---- the default, display the code inside the block but do
85 not process
87 results - just like none only the block is run on export ensuring
88 that it's results are present in the org-mode buffer
90 none ----- do not display either code or results upon export"
91 (interactive)
92 (message "org-babel-exp processing...")
93 (save-excursion
94 (goto-char (match-beginning 0))
95 (let* ((info (org-babel-get-src-block-info))
96 (params (nth 2 info)))
97 ;; expand noweb references in the original file
98 (setf (nth 1 info)
99 (if (and (cdr (assoc :noweb params))
100 (string= "yes" (cdr (assoc :noweb params))))
101 (org-babel-expand-noweb-references
102 info (get-file-buffer org-current-export-file))
103 (nth 1 info)))
104 (org-babel-exp-do-export info 'block))))
106 (defun org-babel-exp-inline-src-blocks (start end)
107 "Process inline src blocks between START and END for export.
108 See `org-babel-exp-src-blocks' for export options, currently the
109 options and are taken from `org-babel-default-inline-header-args'."
110 (interactive)
111 (save-excursion
112 (goto-char start)
113 (while (and (< (point) end)
114 (re-search-forward org-babel-inline-src-block-regexp end t))
115 (let* ((info (save-match-data (org-babel-parse-inline-src-block-match)))
116 (params (nth 2 info))
117 (replacement
118 (save-match-data
119 (if (org-babel-in-example-or-verbatim)
120 (buffer-substring (match-beginning 0) (match-end 0))
121 ;; expand noweb references in the original file
122 (setf (nth 1 info)
123 (if (and (cdr (assoc :noweb params))
124 (string= "yes" (cdr (assoc :noweb params))))
125 (org-babel-expand-noweb-references
126 info (get-file-buffer org-current-export-file))
127 (nth 1 info)))
128 (org-babel-exp-do-export info 'inline)))))
129 (setq end (+ end (- (length replacement) (length (match-string 1)))))
130 (replace-match replacement t t nil 1)))))
132 (defun org-exp-res/src-name-cleanup ()
133 "Cleanup leftover #+results and #+srcname lines as part of the
134 org export cycle. This should only be called after all block
135 processing has taken place."
136 (interactive)
137 (save-excursion
138 (goto-char (point-min))
139 (while (org-re-search-forward-unprotected
140 (concat
141 "\\("org-babel-src-name-regexp"\\|"org-babel-result-regexp"\\)")
142 nil t)
143 (delete-region
144 (progn (beginning-of-line) (point))
145 (progn (end-of-line) (+ 1 (point)))))))
147 (defun org-babel-in-example-or-verbatim ()
148 "Return true if the point is currently in an escaped portion of
149 an org-mode buffer code which should be treated as normal
150 org-mode text."
151 (or (org-in-indented-comment-line)
152 (save-excursion
153 (save-match-data
154 (goto-char (point-at-bol))
155 (looking-at "[ \t]*:[ \t]")))
156 (org-in-regexps-block-p "^[ \t]*#\\+begin_src" "^[ \t]*#\\+end_src")))
158 (defun org-babel-exp-lob-one-liners (start end)
159 "Process #+lob (Library of Babel) calls between START and END for export.
160 See `org-babel-exp-src-blocks' for export options. Currently the
161 options are taken from `org-babel-default-header-args'."
162 (interactive)
163 (let (replacement)
164 (save-excursion
165 (goto-char start)
166 (while (and (< (point) end)
167 (re-search-forward org-babel-lob-one-liner-regexp nil t))
168 (setq replacement
169 (let ((lob-info (org-babel-lob-get-info)))
170 (save-match-data
171 (org-babel-exp-do-export
172 (list "emacs-lisp" "results"
173 (org-babel-merge-params
174 org-babel-default-header-args
175 (org-babel-parse-header-arguments
176 (org-babel-clean-text-properties
177 (concat ":var results="
178 (mapconcat #'identity
179 (butlast lob-info) " ")))))
180 (car (last lob-info)))
181 'lob))))
182 (setq end (+ end (- (length replacement) (length (match-string 0)))))
183 (replace-match replacement t t)))))
185 (defun org-babel-exp-do-export (info type)
186 "Return a string containing the exported content of the current
187 code block respecting the value of the :exports header argument."
188 (flet ((silently () (let ((session (cdr (assoc :session (nth 2 info)))))
189 (when (and session
190 (not (equal "none" session))
191 (not (assoc :noeval (nth 2 info))))
192 (org-babel-exp-results info type 'silent))))
193 (clean () (org-babel-remove-result info)))
194 (case (intern (or (cdr (assoc :exports (nth 2 info))) "code"))
195 ('none (silently) (clean) "")
196 ('code (silently) (clean) (org-babel-exp-code info type))
197 ('results (org-babel-exp-results info type))
198 ('both (concat (org-babel-exp-code info type)
199 "\n\n"
200 (org-babel-exp-results info type))))))
202 (defvar backend)
203 (defun org-babel-exp-code (info type)
204 "Return the code the current code block in a manner suitable
205 for exportation by org-mode. This function is called by
206 `org-babel-exp-do-export'. The code block will not be
207 evaluated."
208 (let ((lang (nth 0 info))
209 (body (nth 1 info))
210 (switches (nth 3 info))
211 (name (nth 4 info))
212 (args (mapcar
213 #'cdr
214 (org-remove-if-not (lambda (el) (eq :var (car el))) (nth 2 info)))))
215 (case type
216 ('inline (format "=%s=" body))
217 ('block
218 (let ((str
219 (format "#+BEGIN_SRC %s %s\n%s%s#+END_SRC\n" lang switches body
220 (if (and body (string-match "\n$" body))
221 "" "\n"))))
222 (when name
223 (add-text-properties
224 0 (length str)
225 (list 'org-caption
226 (format "%s(%s)"
227 name
228 (mapconcat #'identity args ", ")))
229 str))
230 str))
231 ('lob
232 (let ((call-line (and (string-match "results=" (car args))
233 (substring (car args) (match-end 0)))))
234 (cond
235 ((eq backend 'html)
236 (format "\n#+HTML: <label class=\"org-src-name\">%s</label>\n"
237 call-line))
238 ((format ": %s\n" call-line))))))))
240 (defun org-babel-exp-results (info type &optional silent)
241 "Return the results of the current code block in a manner
242 suitable for exportation by org-mode. This function is called by
243 `org-babel-exp-do-export'. The code block will be evaluated.
244 Optional argument SILENT can be used to inhibit insertion of
245 results into the buffer."
246 (if org-export-babel-evaluate
247 (let ((lang (nth 0 info))
248 (body (nth 1 info))
249 (params
250 ;; lets ensure that we lookup references in the original file
251 (mapcar
252 (lambda (pair)
253 (if (and org-current-export-file
254 (eq (car pair) :var)
255 (string-match org-babel-ref-split-regexp (cdr pair))
256 (equal :ob-must-be-reference
257 (org-babel-ref-literal
258 (match-string 2 (cdr pair)))))
259 `(:var . ,(concat (match-string 1 (cdr pair))
260 "=" org-current-export-file
261 ":" (match-string 2 (cdr pair))))
262 pair))
263 (nth 2 info))))
264 ;; skip code blocks which we can't evaluate
265 (if (fboundp (intern (concat "org-babel-execute:" lang)))
266 (case type
267 ('inline
268 (let ((raw (org-babel-execute-src-block
269 nil info '((:results . "silent"))))
270 (result-params (split-string
271 (cdr (assoc :results params)))))
272 (unless silent
273 (cond ;; respect the value of the :results header argument
274 ((member "file" result-params)
275 (org-babel-result-to-file raw))
276 ((or (member "raw" result-params)
277 (member "org" result-params))
278 (format "%s" raw))
279 ((member "code" result-params)
280 (format "src_%s{%s}" lang raw))
282 (if (stringp raw)
283 (if (= 0 (length raw)) "=(no results)="
284 (format "%s" raw))
285 (format "%S" raw)))))))
286 ('block
287 (org-babel-execute-src-block
288 nil info (org-babel-merge-params
289 params
290 `((:results . ,(if silent "silent" "replace")))))
292 ('lob
293 (save-excursion
294 (re-search-backward org-babel-lob-one-liner-regexp nil t)
295 (org-babel-execute-src-block
296 nil info (org-babel-merge-params
297 params
298 `((:results . ,(if silent "silent" "replace")))))
299 "")))
300 ""))
301 ""))
303 (provide 'ob-exp)
305 ;; arch-tag: 523abf4c-76d1-44ed-9f27-e3bddf34bf0f
307 ;;; ob-exp.el ends here