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
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 ;; See the online documentation for more information
29 ;; http://orgmode.org/worg/org-contrib/babel/
33 (require 'org-exp-blocks
)
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
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
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
85 both ---- display the code and the results
87 code ---- the default, display the code inside the block but do
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"
95 (message "org-babel-exp processing...")
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
102 ;; expand noweb references in the original file
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
))
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'."
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
))
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
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
))
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
143 (goto-char (point-min))
144 (while (org-re-search-forward-unprotected
146 "\\("org-babel-src-name-regexp
"\\|"org-babel-result-regexp
"\\)")
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
157 (or (org-in-indented-comment-line)
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'."
172 (while (and (< (point) end
)
173 (re-search-forward org-babel-lob-one-liner-regexp nil t
))
175 (let ((lob-info (org-babel-lob-get-info)))
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
)))
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
)))))
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
)
206 (org-babel-exp-results info type
))))))
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
))
216 (switches (nth 3 info
))
220 (org-remove-if-not (lambda (el) (eq :var
(car el
))) (nth 2 info
)))))
222 ('inline
(format "=%s=" body
))
225 (format "#+BEGIN_SRC %s %s\n%s%s#+END_SRC\n" lang switches body
226 (if (and body
(string-match "\n$" body
))
234 (mapconcat #'identity args
", ")))
238 (let ((call-line (and (string-match "results=" (car args
))
239 (substring (car args
) (match-end 0)))))
242 (format "\n#+HTML: <label class=\"org-src-name\">%s</label>\n"
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
))
256 ;; lets ensure that we lookup references in the original file
259 (if (and org-current-export-file
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
))))
270 ;; skip code blocks which we can't evaluate
271 (if (fboundp (intern (concat "org-babel-execute:" lang
)))
274 (let ((raw (org-babel-execute-src-block
275 nil info
'((:results .
"silent"))))
276 (result-params (split-string
277 (cdr (assoc :results params
)))))
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
))
285 ((member "code" result-params
)
286 (format "src_%s{%s}" lang raw
))
289 (if (= 0 (length raw
)) "=(no results)="
291 (format "%S" raw
)))))))
293 (org-babel-execute-src-block
294 nil info
(org-babel-merge-params
296 `((:results .
,(if silent
"silent" "replace")))))
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
304 `((:results .
,(if silent
"silent" "replace")))))
311 ;; arch-tag: 523abf4c-76d1-44ed-9f27-e3bddf34bf0f
313 ;;; ob-exp.el ends here