1 ;;; ob-exp.el --- Exportation of org-babel source blocks
3 ;; Copyright (C) 2009-2012 Free Software Foundation, Inc.
5 ;; Author: Eric Schulte
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/>.
27 (require 'org-exp-blocks
)
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
))
40 (org-export-blocks-add-block '(src org-babel-exp-src-block nil
))
42 (defcustom org-export-babel-evaluate t
43 "Switch controlling code evaluation during export.
44 When set to nil no code will be evaluated as part of the export
48 (put 'org-export-babel-evaluate
'safe-local-variable
(lambda (x) (eq x nil
)))
50 (defmacro org-babel-exp-in-export-file
(lang &rest body
)
52 `(let* ((lang-headers (intern (concat "org-babel-default-header-args:" ,lang
)))
53 (heading (nth 4 (ignore-errors (org-heading-components))))
54 (link (when org-current-export-file
57 (concat org-current-export-file
"::" heading
)
58 org-current-export-file
))))
59 (export-buffer (current-buffer)) results
)
61 ;; resolve parameters in the original file so that
62 ;; headline and file-wide parameters are included, attempt
63 ;; to go to the same heading in the original file
64 (set-buffer (get-file-buffer org-current-export-file
))
67 (let ((org-link-search-inhibit-query t
))
68 (org-open-link-from-string link
))
70 (goto-char (point-min))
71 (re-search-forward (regexp-quote heading
) nil t
))))
72 (setq results
,@body
))
73 (set-buffer export-buffer
)
75 (def-edebug-spec org-babel-exp-in-export-file
(form body
))
77 (defun org-babel-exp-src-block (body &rest headers
)
78 "Process source block for export.
79 Depending on the 'export' headers argument in replace the source
82 both ---- display the code and the results
84 code ---- the default, display the code inside the block but do
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"
92 (unless noninteractive
(message "org-babel-exp processing..."))
94 (goto-char (match-beginning 0))
95 (let* ((info (org-babel-get-src-block-info 'light
))
97 (raw-params (nth 2 info
)) hash
)
98 ;; bail if we couldn't get any info from the block
100 ;; if we're actually going to need the parameters
101 (when (member (cdr (assoc :exports
(nth 2 info
))) '("both" "results"))
102 (org-babel-exp-in-export-file lang
104 (org-babel-process-params
105 (org-babel-merge-params
106 org-babel-default-header-args
107 (org-babel-params-from-properties lang
)
108 (if (boundp lang-headers
) (eval lang-headers
) nil
)
110 (setf hash
(org-babel-sha1-hash info
)))
111 ;; expand noweb references in the original file
113 (if (org-babel-noweb-p (nth 2 info
) :export
)
114 (org-babel-expand-noweb-references
115 info
(get-file-buffer org-current-export-file
))
117 (org-babel-exp-do-export info
'block hash
)))))
119 (defun org-babel-exp-inline-src-blocks (start end
)
120 "Process inline source blocks between START and END for export.
121 See `org-babel-exp-src-block' for export options, currently the
122 options and are taken from `org-babel-default-inline-header-args'."
126 (while (and (< (point) end
)
127 (re-search-forward org-babel-inline-src-block-regexp end t
))
128 (let* ((info (save-match-data (org-babel-parse-inline-src-block-match)))
129 (params (nth 2 info
)))
131 (goto-char (match-beginning 2))
132 (unless (org-babel-in-example-or-verbatim)
133 ;; expand noweb references in the original file
135 (if (org-babel-noweb-p params
:export
)
136 (org-babel-expand-noweb-references
137 info
(get-file-buffer org-current-export-file
))
139 (let ((code-replacement (save-match-data
140 (org-babel-exp-do-export info
'inline
))))
142 (replace-match code-replacement nil nil nil
1)
143 (org-babel-examplize-region (match-beginning 1) (match-end 1))
144 (forward-char 2)))))))))
146 (defun org-babel-in-example-or-verbatim ()
147 "Return true if point is in example or verbatim code.
148 Example and verbatim code include escaped portions of
149 an org-mode buffer code that should be treated as normal
153 (goto-char (point-at-bol))
154 (looking-at "[ \t]*:[ \t]")))
155 (org-in-verbatim-emphasis)
156 (org-in-block-p org-list-forbidden-blocks
)
157 (org-between-regexps-p "^[ \t]*#\\+begin_src" "^[ \t]*#\\+end_src")))
159 (defvar org-babel-default-lob-header-args
)
160 (defun org-babel-exp-lob-one-liners (start end
)
161 "Process Library of Babel calls between START and END for export.
162 See `org-babel-exp-src-block' for export options. Currently the
163 options are taken from `org-babel-default-header-args'."
167 (unless (markerp end
)
168 (let ((m (make-marker)))
169 (set-marker m end
(current-buffer))
171 (while (and (< (point) (marker-position end
))
172 (re-search-forward org-babel-lob-one-liner-regexp end t
))
173 (unless (org-babel-in-example-or-verbatim)
174 (let* ((lob-info (org-babel-lob-get-info))
175 (inlinep (match-string 11))
176 (inline-start (match-end 11))
177 (inline-end (match-end 0))
178 (rep (let ((lob-info (org-babel-lob-get-info)))
180 (org-babel-exp-do-export
181 (list "emacs-lisp" "results"
182 (org-babel-merge-params
183 org-babel-default-header-args
184 org-babel-default-lob-header-args
185 (org-babel-params-from-properties)
186 (org-babel-parse-header-arguments
187 (org-babel-clean-text-properties
188 (concat ":var results="
189 (mapconcat #'identity
190 (butlast lob-info
) " ")))))
191 "" nil
(car (last lob-info
)))
195 (goto-char inline-start
)
196 (delete-region inline-start inline-end
)
198 (replace-match rep t t
)))))))
200 (defun org-babel-exp-do-export (info type
&optional hash
)
201 "Return a string with the exported content of a code block.
202 The function respects the value of the :exports header argument."
203 (flet ((silently () (let ((session (cdr (assoc :session
(nth 2 info
)))))
204 (when (not (and session
(equal "none" session
)))
205 (org-babel-exp-results info type
'silent
))))
206 (clean () (unless (eq type
'inline
) (org-babel-remove-result info
))))
207 (case (intern (or (cdr (assoc :exports
(nth 2 info
))) "code"))
208 ('none
(silently) (clean) "")
209 ('code
(silently) (clean) (org-babel-exp-code info
))
210 ('results
(org-babel-exp-results info type nil hash
) "")
211 ('both
(org-babel-exp-results info type nil hash
)
212 (org-babel-exp-code info
)))))
214 (defcustom org-babel-exp-code-template
215 "#+BEGIN_SRC %lang%flags\n%body\n#+END_SRC"
216 "Template used to export the body of code blocks.
217 This template may be customized to include additional information
218 such as the code block name, or the values of particular header
219 arguments. The template is filled out using `org-fill-template',
220 and the following %keys may be used.
222 lang ------ the language of the code block
223 name ------ the name of the code block
224 body ------ the body of the code block
225 flags ----- the flags passed to the code block
227 In addition to the keys mentioned above, every header argument
228 defined for the code block may be used as a key and will be
229 replaced with its value."
233 (defun org-babel-exp-code (info)
234 "Return the original code block formatted for export."
236 org-babel-exp-code-template
237 `(("lang" .
,(nth 0 info
))
238 ("body" .
,(nth 1 info
))
239 ,@(mapcar (lambda (pair)
240 (cons (substring (symbol-name (car pair
)) 1)
241 (format "%S" (cdr pair
))))
243 ("flags" .
,((lambda (f) (when f
(concat " " f
))) (nth 3 info
)))
244 ("name" .
,(or (nth 4 info
) "")))))
246 (defun org-babel-exp-results (info type
&optional silent hash
)
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 (when (and org-export-babel-evaluate
253 (not (and hash
(equal hash
(org-babel-current-result-hash)))))
254 (let ((lang (nth 0 info
))
256 (info (copy-sequence info
)))
257 ;; skip code blocks which we can't evaluate
258 (when (fboundp (intern (concat "org-babel-execute:" lang
)))
259 (org-babel-eval-wipe-error-buffer)
262 (org-babel-exp-in-export-file lang
263 (org-babel-process-params
264 (org-babel-merge-params
266 `((:results .
,(if silent
"silent" "replace")))))))
269 (org-babel-execute-src-block nil info
))
270 ((equal type
'inline
)
271 ;; position the point on the inline source block allowing
272 ;; `org-babel-insert-result' to check that the block is
274 (re-search-backward "[ \f\t\n\r\v]" nil t
)
275 (re-search-forward org-babel-inline-src-block-regexp nil t
)
276 (re-search-backward "src_" nil t
)
277 (org-babel-execute-src-block nil info
))
280 (re-search-backward org-babel-lob-one-liner-regexp nil t
)
281 (org-babel-execute-src-block nil info
)))))))))
287 ;;; ob-exp.el ends here