Add version tag "24.1" for options introduced since Emacs 23.4 (and <= 24.1)
[org-mode/org-kjn.git] / lisp / ob-exp.el
blob06d193a1a17ce1054086c1a6e1386de3a3989593
1 ;;; ob-exp.el --- Exportation of org-babel source blocks
3 ;; Copyright (C) 2009-2012 Free Software Foundation, Inc.
5 ;; Author: Eric Schulte
6 ;; Dan Davison
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/>.
25 ;;; Code:
26 (require 'ob)
27 (require 'org-exp-blocks)
28 (eval-when-compile
29 (require 'cl))
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-non-block-elements))
39 (org-export-blocks-add-block '(src org-babel-exp-src-block nil))
41 (defcustom org-export-babel-evaluate t
42 "Switch controlling code evaluation during export.
43 When set to nil no code will be evaluated as part of the export
44 process."
45 :group 'org-babel
46 :version "24.1"
47 :type 'boolean)
48 (put 'org-export-babel-evaluate 'safe-local-variable (lambda (x) (eq x nil)))
50 (defun org-babel-exp-get-export-buffer ()
51 "Return the current export buffer if possible."
52 (cond
53 ((bufferp org-current-export-file) org-current-export-file)
54 (org-current-export-file (get-file-buffer org-current-export-file))
55 ('otherwise
56 (error "Requested export buffer when `org-current-export-file' is nil"))))
58 (defmacro org-babel-exp-in-export-file (lang &rest body)
59 (declare (indent 1))
60 `(let* ((lang-headers (intern (concat "org-babel-default-header-args:" ,lang)))
61 (heading (nth 4 (ignore-errors (org-heading-components))))
62 (export-buffer (current-buffer))
63 (original-buffer (org-babel-exp-get-export-buffer)) results)
64 (when original-buffer
65 ;; resolve parameters in the original file so that
66 ;; headline and file-wide parameters are included, attempt
67 ;; to go to the same heading in the original file
68 (set-buffer original-buffer)
69 (save-restriction
70 (when heading
71 (condition-case nil
72 (let ((org-link-search-inhibit-query t))
73 (org-link-search heading))
74 (error (when heading
75 (goto-char (point-min))
76 (re-search-forward (regexp-quote heading) nil t)))))
77 (setq results ,@body))
78 (set-buffer export-buffer)
79 results)))
80 (def-edebug-spec org-babel-exp-in-export-file (form body))
82 (defun org-babel-exp-src-block (body &rest headers)
83 "Process source block for export.
84 Depending on the 'export' headers argument in replace the source
85 code block with...
87 both ---- display the code and the results
89 code ---- the default, display the code inside the block but do
90 not process
92 results - just like none only the block is run on export ensuring
93 that it's results are present in the org-mode buffer
95 none ----- do not display either code or results upon export"
96 (interactive)
97 (unless noninteractive (message "org-babel-exp processing..."))
98 (save-excursion
99 (goto-char (match-beginning 0))
100 (let* ((info (org-babel-get-src-block-info 'light))
101 (lang (nth 0 info))
102 (raw-params (nth 2 info)) hash)
103 ;; bail if we couldn't get any info from the block
104 (when info
105 ;; if we're actually going to need the parameters
106 (when (member (cdr (assoc :exports (nth 2 info))) '("both" "results"))
107 (org-babel-exp-in-export-file lang
108 (setf (nth 2 info)
109 (org-babel-process-params
110 (org-babel-merge-params
111 org-babel-default-header-args
112 (org-babel-params-from-properties lang)
113 (if (boundp lang-headers) (eval lang-headers) nil)
114 raw-params))))
115 (setf hash (org-babel-sha1-hash info)))
116 ;; expand noweb references in the original file
117 (setf (nth 1 info)
118 (if (string= "strip-export" (cdr (assoc :noweb (nth 2 info))))
119 (replace-regexp-in-string
120 (org-babel-noweb-wrap) "" (nth 1 info))
121 (if (org-babel-noweb-p (nth 2 info) :export)
122 (org-babel-expand-noweb-references
123 info (org-babel-exp-get-export-buffer))
124 (nth 1 info))))
125 (org-babel-exp-do-export info 'block hash)))))
127 (defcustom org-babel-exp-call-line-template
129 "Template used to export call lines.
130 This template may be customized to include the call line name
131 with any export markup. The template is filled out using
132 `org-fill-template', and the following %keys may be used.
134 line --- call line
136 An example value would be \"\\n: call: %line\" to export the call line
137 wrapped in a verbatim environment.
139 Note: the results are inserted separately after the contents of
140 this template."
141 :group 'org-babel
142 :type 'string)
144 (defvar org-babel-default-lob-header-args)
145 (defun org-babel-exp-non-block-elements (start end)
146 "Process inline source and call lines between START and END for export."
147 (interactive)
148 (save-excursion
149 (goto-char start)
150 (unless (markerp end)
151 (let ((m (make-marker)))
152 (set-marker m end (current-buffer))
153 (setq end m)))
154 (let ((rx (concat "\\(" org-babel-inline-src-block-regexp
155 "\\|" org-babel-lob-one-liner-regexp "\\)")))
156 (while (and (< (point) (marker-position end))
157 (re-search-forward rx end t))
158 (if (save-excursion
159 (goto-char (match-beginning 0))
160 (looking-at org-babel-inline-src-block-regexp))
161 (progn
162 (forward-char 1)
163 (let* ((info (save-match-data
164 (org-babel-parse-inline-src-block-match)))
165 (params (nth 2 info)))
166 (save-match-data
167 (goto-char (match-beginning 2))
168 (unless (org-babel-in-example-or-verbatim)
169 ;; expand noweb references in the original file
170 (setf (nth 1 info)
171 (if (and (cdr (assoc :noweb params))
172 (string= "yes" (cdr (assoc :noweb params))))
173 (org-babel-expand-noweb-references
174 info (org-babel-exp-get-export-buffer))
175 (nth 1 info)))
176 (let ((code-replacement (save-match-data
177 (org-babel-exp-do-export
178 info 'inline))))
179 (if code-replacement
180 (progn (replace-match code-replacement nil nil nil 1)
181 (delete-char 1))
182 (org-babel-examplize-region (match-beginning 1)
183 (match-end 1))
184 (forward-char 2)))))))
185 (unless (org-babel-in-example-or-verbatim)
186 (let* ((lob-info (org-babel-lob-get-info))
187 (inlinep (match-string 11))
188 (inline-start (match-end 11))
189 (inline-end (match-end 0))
190 (results (save-match-data
191 (org-babel-exp-do-export
192 (list "emacs-lisp" "results"
193 (org-babel-merge-params
194 org-babel-default-header-args
195 org-babel-default-lob-header-args
196 (org-babel-params-from-properties)
197 (org-babel-parse-header-arguments
198 (org-babel-clean-text-properties
199 (concat ":var results="
200 (mapconcat #'identity
201 (butlast lob-info)
202 " ")))))
203 "" nil (car (last lob-info)))
204 'lob)))
205 (rep (org-fill-template
206 org-babel-exp-call-line-template
207 `(("line" . ,(nth 0 lob-info))))))
208 (if inlinep
209 (save-excursion
210 (goto-char inline-start)
211 (delete-region inline-start inline-end)
212 (insert rep))
213 (replace-match rep t t)))))))))
215 (defun org-babel-in-example-or-verbatim ()
216 "Return true if point is in example or verbatim code.
217 Example and verbatim code include escaped portions of
218 an org-mode buffer code that should be treated as normal
219 org-mode text."
220 (or (save-match-data
221 (save-excursion
222 (goto-char (point-at-bol))
223 (looking-at "[ \t]*:[ \t]")))
224 (org-in-verbatim-emphasis)
225 (org-in-block-p org-list-forbidden-blocks)
226 (org-between-regexps-p "^[ \t]*#\\+begin_src" "^[ \t]*#\\+end_src")))
228 (defun org-babel-exp-do-export (info type &optional hash)
229 "Return a string with the exported content of a code block.
230 The function respects the value of the :exports header argument."
231 (flet ((silently () (let ((session (cdr (assoc :session (nth 2 info)))))
232 (when (not (and session (equal "none" session)))
233 (org-babel-exp-results info type 'silent))))
234 (clean () (unless (eq type 'inline) (org-babel-remove-result info))))
235 (case (intern (or (cdr (assoc :exports (nth 2 info))) "code"))
236 ('none (silently) (clean) "")
237 ('code (silently) (clean) (org-babel-exp-code info))
238 ('results (org-babel-exp-results info type nil hash) "")
239 ('both (org-babel-exp-results info type nil hash)
240 (org-babel-exp-code info)))))
242 (defcustom org-babel-exp-code-template
243 "#+BEGIN_SRC %lang%flags\n%body\n#+END_SRC"
244 "Template used to export the body of code blocks.
245 This template may be customized to include additional information
246 such as the code block name, or the values of particular header
247 arguments. The template is filled out using `org-fill-template',
248 and the following %keys may be used.
250 lang ------ the language of the code block
251 name ------ the name of the code block
252 body ------ the body of the code block
253 flags ----- the flags passed to the code block
255 In addition to the keys mentioned above, every header argument
256 defined for the code block may be used as a key and will be
257 replaced with its value."
258 :group 'org-babel
259 :type 'string)
261 (defun org-babel-exp-code (info)
262 "Return the original code block formatted for export."
263 (org-fill-template
264 org-babel-exp-code-template
265 `(("lang" . ,(nth 0 info))
266 ("body" . ,(nth 1 info))
267 ,@(mapcar (lambda (pair)
268 (cons (substring (symbol-name (car pair)) 1)
269 (format "%S" (cdr pair))))
270 (nth 2 info))
271 ("flags" . ,((lambda (f) (when f (concat " " f))) (nth 3 info)))
272 ("name" . ,(or (nth 4 info) "")))))
274 (defun org-babel-exp-results (info type &optional silent hash)
275 "Evaluate and return the results of the current code block for export.
276 Results are prepared in a manner suitable for export by org-mode.
277 This function is called by `org-babel-exp-do-export'. The code
278 block will be evaluated. Optional argument SILENT can be used to
279 inhibit insertion of results into the buffer."
280 (when (and org-export-babel-evaluate
281 (not (and hash (equal hash (org-babel-current-result-hash)))))
282 (let ((lang (nth 0 info))
283 (body (nth 1 info))
284 (info (copy-sequence info)))
285 ;; skip code blocks which we can't evaluate
286 (when (fboundp (intern (concat "org-babel-execute:" lang)))
287 (org-babel-eval-wipe-error-buffer)
288 (prog1 nil
289 (setf (nth 2 info)
290 (org-babel-exp-in-export-file lang
291 (org-babel-process-params
292 (org-babel-merge-params
293 (nth 2 info)
294 `((:results . ,(if silent "silent" "replace")))))))
295 (cond
296 ((equal type 'block)
297 (org-babel-execute-src-block nil info))
298 ((equal type 'inline)
299 ;; position the point on the inline source block allowing
300 ;; `org-babel-insert-result' to check that the block is
301 ;; inline
302 (re-search-backward "[ \f\t\n\r\v]" nil t)
303 (re-search-forward org-babel-inline-src-block-regexp nil t)
304 (re-search-backward "src_" nil t)
305 (org-babel-execute-src-block nil info))
306 ((equal type 'lob)
307 (save-excursion
308 (re-search-backward org-babel-lob-one-liner-regexp nil t)
309 (org-babel-execute-src-block nil info)))))))))
311 (provide 'ob-exp)
315 ;;; ob-exp.el ends here