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 "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
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
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 (message "org-babel-exp processing...")
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
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
))
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'."
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
))
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
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
))
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."
138 (goto-char (point-min))
139 (while (org-re-search-forward-unprotected
141 "\\("org-babel-source-name-regexp
"\\|"org-babel-result-regexp
"\\)")
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
151 (or (org-in-indented-comment-line)
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'."
166 (while (and (< (point) end
)
167 (re-search-forward org-babel-lob-one-liner-regexp nil t
))
169 (let ((lob-info (org-babel-lob-get-info)))
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
)))
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
)))))
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
)
200 (org-babel-exp-results info type
))))))
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
208 (let ((lang (nth 0 info
))
210 (switches (nth 3 info
))
214 (org-remove-if-not (lambda (el) (eq :var
(car el
))) (nth 2 info
)))))
216 ('inline
(format "=%s=" body
))
219 (format "#+BEGIN_SRC %s %s\n%s%s#+END_SRC\n" lang switches body
220 (if (and body
(string-match "\n$" body
))
228 (mapconcat #'identity args
", ")))
232 (let ((call-line (and (string-match "results=" (car args
))
233 (substring (car args
) (match-end 0)))))
236 (format "\n#+HTML: <label class=\"org-src-name\">%s</label>\n"
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
))
250 ;; lets ensure that we lookup references in the original file
253 (if (and org-current-export-file
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
))))
264 ;; skip code blocks which we can't evaluate
265 (if (fboundp (intern (concat "org-babel-execute:" lang
)))
268 (let ((raw (org-babel-execute-src-block
269 nil info
'((:results .
"silent"))))
270 (result-params (split-string
271 (cdr (assoc :results params
)))))
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
))
279 ((member "code" result-params
)
280 (format "src_%s{%s}" lang raw
))
283 (if (= 0 (length raw
)) "=(no results)="
285 (format "%S" raw
)))))))
287 (org-babel-execute-src-block
288 nil info
(org-babel-merge-params
290 `((:results .
,(if silent
"silent" "replace")))))
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
298 `((:results .
,(if silent
"silent" "replace")))))
305 ;; arch-tag: 523abf4c-76d1-44ed-9f27-e3bddf34bf0f
307 ;;; ob-exp.el ends here