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