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