remove call to bibtex-reformat which often hangs
[org-mode/org-mode-NeilSmithlineMods.git] / lisp / ob-exp.el
blob0f0cca3d94b5f02a473b1958ea1a2ed18113dee0
1 ;;; ob-exp.el --- Exportation of org-babel source blocks
3 ;; Copyright (C) 2009-2012 Free Software Foundation, Inc.
5 ;; Authors: 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 (defmacro org-babel-exp-in-export-file (lang &rest body)
51 (declare (indent 1))
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
55 (org-make-link-string
56 (if heading
57 (concat org-current-export-file "::" heading)
58 org-current-export-file))))
59 (export-buffer (current-buffer)) results)
60 (when link
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))
65 (save-restriction
66 (condition-case nil
67 (let ((org-link-search-inhibit-query t))
68 (org-open-link-from-string link))
69 (error (when heading
70 (goto-char (point-min))
71 (re-search-forward (regexp-quote heading) nil t))))
72 (setq results ,@body))
73 (set-buffer export-buffer)
74 results)))
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
80 code block with...
82 both ---- display the code and the results
84 code ---- the default, display the code inside the block but do
85 not process
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"
91 (interactive)
92 (unless noninteractive (message "org-babel-exp processing..."))
93 (save-excursion
94 (goto-char (match-beginning 0))
95 (let* ((info (org-babel-get-src-block-info 'light))
96 (lang (nth 0 info))
97 (raw-params (nth 2 info)) hash)
98 ;; bail if we couldn't get any info from the block
99 (when info
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
103 (setf (nth 2 info)
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)
109 raw-params))))
110 (setf hash (org-babel-sha1-hash info)))
111 ;; expand noweb references in the original file
112 (setf (nth 1 info)
113 (if (and (cdr (assoc :noweb (nth 2 info)))
114 (string= "yes" (cdr (assoc :noweb (nth 2 info)))))
115 (org-babel-expand-noweb-references
116 info (get-file-buffer org-current-export-file))
117 (nth 1 info)))
118 (org-babel-exp-do-export info 'block hash)))))
120 (defvar org-babel-default-lob-header-args)
121 (defun org-babel-exp-non-block-elements (start end)
122 "Process inline source and call lines between START and END for export."
123 (interactive)
124 (save-excursion
125 (goto-char start)
126 (unless (markerp end)
127 (let ((m (make-marker)))
128 (set-marker m end (current-buffer))
129 (setq end m)))
130 (let ((rx (concat "\\(" org-babel-inline-src-block-regexp
131 "\\|" org-babel-lob-one-liner-regexp "\\)")))
132 (while (and (< (point) (marker-position end))
133 (re-search-forward rx end t))
134 (if (save-excursion
135 (goto-char (match-beginning 0))
136 (looking-at org-babel-inline-src-block-regexp))
137 (progn
138 (forward-char 1)
139 (let* ((info (save-match-data
140 (org-babel-parse-inline-src-block-match)))
141 (params (nth 2 info)))
142 (save-match-data
143 (goto-char (match-beginning 2))
144 (unless (org-babel-in-example-or-verbatim)
145 ;; expand noweb references in the original file
146 (setf (nth 1 info)
147 (if (and (cdr (assoc :noweb params))
148 (string= "yes" (cdr (assoc :noweb params))))
149 (org-babel-expand-noweb-references
150 info (get-file-buffer org-current-export-file))
151 (nth 1 info)))
152 (let ((code-replacement (save-match-data
153 (org-babel-exp-do-export
154 info 'inline))))
155 (if code-replacement
156 (progn (replace-match code-replacement nil nil nil 1)
157 (delete-char 1))
158 (org-babel-examplize-region (match-beginning 1)
159 (match-end 1))
160 (forward-char 2)))))))
161 (unless (org-babel-in-example-or-verbatim)
162 (let* ((lob-info (org-babel-lob-get-info))
163 (inlinep (match-string 11))
164 (inline-start (match-end 11))
165 (inline-end (match-end 0))
166 (rep (let ((lob-info (org-babel-lob-get-info)))
167 (save-match-data
168 (org-babel-exp-do-export
169 (list "emacs-lisp" "results"
170 (org-babel-merge-params
171 org-babel-default-header-args
172 org-babel-default-lob-header-args
173 (org-babel-params-from-properties)
174 (org-babel-parse-header-arguments
175 (org-babel-clean-text-properties
176 (concat ":var results="
177 (mapconcat #'identity
178 (butlast lob-info)
179 " ")))))
180 "" nil (car (last lob-info)))
181 'lob)))))
182 (if inlinep
183 (save-excursion
184 (goto-char inline-start)
185 (delete-region inline-start inline-end)
186 (insert rep))
187 (replace-match rep t t)))))))))
189 (defun org-babel-in-example-or-verbatim ()
190 "Return true if point is in example or verbatim code.
191 Example and verbatim code include escaped portions of
192 an org-mode buffer code that should be treated as normal
193 org-mode text."
194 (or (save-match-data
195 (save-excursion
196 (goto-char (point-at-bol))
197 (looking-at "[ \t]*:[ \t]")))
198 (org-in-verbatim-emphasis)
199 (org-in-block-p org-list-forbidden-blocks)
200 (org-between-regexps-p "^[ \t]*#\\+begin_src" "^[ \t]*#\\+end_src")))
202 (defun org-babel-exp-do-export (info type &optional hash)
203 "Return a string with the exported content of a code block.
204 The function respects the value of the :exports header argument."
205 (flet ((silently () (let ((session (cdr (assoc :session (nth 2 info)))))
206 (when (not (and session (equal "none" session)))
207 (org-babel-exp-results info type 'silent))))
208 (clean () (unless (eq type 'inline) (org-babel-remove-result info))))
209 (case (intern (or (cdr (assoc :exports (nth 2 info))) "code"))
210 ('none (silently) (clean) "")
211 ('code (silently) (clean) (org-babel-exp-code info))
212 ('results (org-babel-exp-results info type nil hash) "")
213 ('both (org-babel-exp-results info type nil hash)
214 (org-babel-exp-code info)))))
216 (defun org-babel-exp-code (info)
217 "Return the original code block formatted for export."
218 (org-fill-template
219 "#+BEGIN_SRC %lang%flags\n%body\n#+END_SRC"
220 `(("lang" . ,(nth 0 info))
221 ("flags" . ,((lambda (f) (when f (concat " " f))) (nth 3 info)))
222 ("body" . ,(if (string= (nth 0 info) "org")
223 (replace-regexp-in-string "^" "," (nth 1 info))
224 (nth 1 info))))))
226 (defun org-babel-exp-results (info type &optional silent hash)
227 "Evaluate and return the results of the current code block for export.
228 Results are prepared in a manner suitable for export by org-mode.
229 This function is called by `org-babel-exp-do-export'. The code
230 block will be evaluated. Optional argument SILENT can be used to
231 inhibit insertion of results into the buffer."
232 (when (and org-export-babel-evaluate
233 (not (and hash (equal hash (org-babel-current-result-hash)))))
234 (let ((lang (nth 0 info))
235 (body (nth 1 info)))
236 ;; skip code blocks which we can't evaluate
237 (when (fboundp (intern (concat "org-babel-execute:" lang)))
238 (org-babel-eval-wipe-error-buffer)
239 (prog1 nil
240 (setf (nth 2 info)
241 (org-babel-exp-in-export-file lang
242 (org-babel-process-params
243 (org-babel-merge-params
244 (nth 2 info)
245 `((:results . ,(if silent "silent" "replace")))))))
246 (cond
247 ((equal type 'block)
248 (org-babel-execute-src-block nil info))
249 ((equal type 'inline)
250 ;; position the point on the inline source block allowing
251 ;; `org-babel-insert-result' to check that the block is
252 ;; inline
253 (re-search-backward "[ \f\t\n\r\v]" nil t)
254 (re-search-forward org-babel-inline-src-block-regexp nil t)
255 (re-search-backward "src_" nil t)
256 (org-babel-execute-src-block nil info))
257 ((equal type 'lob)
258 (save-excursion
259 (re-search-backward org-babel-lob-one-liner-regexp nil t)
260 (org-babel-execute-src-block nil info)))))))))
262 (provide 'ob-exp)
266 ;;; ob-exp.el ends here