]> code.delx.au - gnu-emacs/blob - lisp/org/ob-exp.el
Merge from trunk.
[gnu-emacs] / lisp / org / ob-exp.el
1 ;;; ob-exp.el --- Exportation of org-babel source blocks
2
3 ;; Copyright (C) 2009-2011 Free Software Foundation, Inc.
4
5 ;; Author: Eric Schulte
6 ;; Dan Davison
7 ;; Keywords: literate programming, reproducible research
8 ;; Homepage: http://orgmode.org
9 ;; Version: 7.7
10
11 ;; This file is part of GNU Emacs.
12
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
17
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25
26 ;;; Code:
27 (require 'ob)
28 (require 'org-exp-blocks)
29 (eval-when-compile
30 (require 'cl))
31
32 (defvar obe-marker nil)
33 (defvar org-current-export-file)
34 (defvar org-babel-lob-one-liner-regexp)
35 (defvar org-babel-ref-split-regexp)
36 (declare-function org-babel-lob-get-info "ob-lob" ())
37 (declare-function org-babel-eval-wipe-error-buffer "ob-eval" ())
38 (add-to-list 'org-export-interblocks '(src org-babel-exp-inline-src-blocks))
39 (add-to-list 'org-export-interblocks '(lob org-babel-exp-lob-one-liners))
40 (add-hook 'org-export-blocks-postblock-hook 'org-exp-res/src-name-cleanup)
41
42 (org-export-blocks-add-block '(src org-babel-exp-src-block nil))
43
44 (defcustom org-export-babel-evaluate t
45 "Switch controlling code evaluation during export.
46 When set to nil no code will be evaluated as part of the export
47 process."
48 :group 'org-babel
49 :type 'boolean)
50 (put 'org-export-babel-evaluate 'safe-local-variable (lambda (x) (eq x nil)))
51
52 (defmacro org-babel-exp-in-export-file (lang &rest body)
53 (declare (indent 1))
54 `(let* ((lang-headers (intern (concat "org-babel-default-header-args:" ,lang)))
55 (heading (nth 4 (ignore-errors (org-heading-components))))
56 (link (when org-current-export-file
57 (org-make-link-string
58 (if heading
59 (concat org-current-export-file "::" heading)
60 org-current-export-file))))
61 (export-buffer (current-buffer)) results)
62 (when link
63 ;; resolve parameters in the original file so that
64 ;; headline and file-wide parameters are included, attempt
65 ;; to go to the same heading in the original file
66 (set-buffer (get-file-buffer org-current-export-file))
67 (save-restriction
68 (condition-case nil
69 (let ((org-link-search-inhibit-query t))
70 (org-open-link-from-string link))
71 (error (when heading
72 (goto-char (point-min))
73 (re-search-forward (regexp-quote heading) nil t))))
74 (setq results ,@body))
75 (set-buffer export-buffer)
76 results)))
77
78 (defun org-babel-exp-src-block (body &rest headers)
79 "Process source block for export.
80 Depending on the 'export' headers argument in replace the source
81 code block with...
82
83 both ---- display the code and the results
84
85 code ---- the default, display the code inside the block but do
86 not process
87
88 results - just like none only the block is run on export ensuring
89 that it's results are present in the org-mode buffer
90
91 none ----- do not display either code or results upon export"
92 (interactive)
93 (unless noninteractive (message "org-babel-exp processing..."))
94 (save-excursion
95 (goto-char (match-beginning 0))
96 (let* ((info (org-babel-get-src-block-info 'light))
97 (lang (nth 0 info))
98 (raw-params (nth 2 info)) hash)
99 ;; bail if we couldn't get any info from the block
100 (when info
101 ;; if we're actually going to need the parameters
102 (when (member (cdr (assoc :exports (nth 2 info))) '("both" "results"))
103 (org-babel-exp-in-export-file lang
104 (setf (nth 2 info)
105 (org-babel-process-params
106 (org-babel-merge-params
107 org-babel-default-header-args
108 (org-babel-params-from-buffer)
109 (org-babel-params-from-properties lang)
110 (if (boundp lang-headers) (eval lang-headers) nil)
111 raw-params))))
112 (setf hash (org-babel-sha1-hash info)))
113 ;; expand noweb references in the original file
114 (setf (nth 1 info)
115 (if (and (cdr (assoc :noweb (nth 2 info)))
116 (string= "yes" (cdr (assoc :noweb (nth 2 info)))))
117 (org-babel-expand-noweb-references
118 info (get-file-buffer org-current-export-file))
119 (nth 1 info)))
120 (org-babel-exp-do-export info 'block hash)))))
121
122 (defun org-babel-exp-inline-src-blocks (start end)
123 "Process inline source blocks between START and END for export.
124 See `org-babel-exp-src-block' for export options, currently the
125 options and are taken from `org-babel-default-inline-header-args'."
126 (interactive)
127 (save-excursion
128 (goto-char start)
129 (while (and (< (point) end)
130 (re-search-forward org-babel-inline-src-block-regexp end t))
131 (let* ((info (save-match-data (org-babel-parse-inline-src-block-match)))
132 (params (nth 2 info)) code-replacement)
133 (save-match-data
134 (goto-char (match-beginning 2))
135 (when (not (org-babel-in-example-or-verbatim))
136 ;; expand noweb references in the original file
137 (setf (nth 1 info)
138 (if (and (cdr (assoc :noweb params))
139 (string= "yes" (cdr (assoc :noweb params))))
140 (org-babel-expand-noweb-references
141 info (get-file-buffer org-current-export-file))
142 (nth 1 info)))
143 (setq code-replacement (org-babel-exp-do-export info 'inline))))
144 (if code-replacement
145 (replace-match code-replacement nil nil nil 1)
146 (org-babel-examplize-region (match-beginning 1) (match-end 1))
147 (forward-char 2))))))
148
149 (defun org-exp-res/src-name-cleanup ()
150 "Clean up #+results and #+srcname lines for export.
151 This function should only be called after all block processing
152 has taken place."
153 (interactive)
154 (save-excursion
155 (goto-char (point-min))
156 (while (org-re-search-forward-unprotected
157 (concat
158 "\\("org-babel-src-name-regexp"\\|"org-babel-result-regexp"\\)")
159 nil t)
160 (delete-region
161 (progn (beginning-of-line) (point))
162 (progn (end-of-line) (+ 1 (point)))))))
163
164 (defun org-babel-in-example-or-verbatim ()
165 "Return true if point is in example or verbatim code.
166 Example and verbatim code include escaped portions of
167 an org-mode buffer code that should be treated as normal
168 org-mode text."
169 (or (org-in-indented-comment-line)
170 (save-match-data
171 (save-excursion
172 (goto-char (point-at-bol))
173 (looking-at "[ \t]*:[ \t]")))
174 (org-in-verbatim-emphasis)
175 (org-in-regexps-block-p "^[ \t]*#\\+begin_src" "^[ \t]*#\\+end_src")))
176
177 (defvar org-babel-default-lob-header-args)
178 (defun org-babel-exp-lob-one-liners (start end)
179 "Process Library of Babel calls between START and END for export.
180 See `org-babel-exp-src-block' for export options. Currently the
181 options are taken from `org-babel-default-header-args'."
182 (interactive)
183 (save-excursion
184 (goto-char start)
185 (while (and (< (point) end)
186 (re-search-forward org-babel-lob-one-liner-regexp nil t))
187 (unless (and (match-string 12) (org-babel-in-example-or-verbatim))
188 (let* ((lob-info (org-babel-lob-get-info))
189 (inlinep (match-string 11))
190 (inline-start (match-end 11))
191 (inline-end (match-end 0))
192 (rep (let ((lob-info (org-babel-lob-get-info)))
193 (save-match-data
194 (org-babel-exp-do-export
195 (list "emacs-lisp" "results"
196 (org-babel-merge-params
197 org-babel-default-header-args
198 org-babel-default-lob-header-args
199 (org-babel-params-from-buffer)
200 (org-babel-params-from-properties)
201 (org-babel-parse-header-arguments
202 (org-babel-clean-text-properties
203 (concat ":var results="
204 (mapconcat #'identity
205 (butlast lob-info) " ")))))
206 "" nil (car (last lob-info)))
207 'lob)))))
208 (setq end (+ end (- (length rep)
209 (- (length (match-string 0))
210 (length (or (match-string 11) ""))))))
211 (if inlinep
212 (save-excursion
213 (goto-char inline-start)
214 (delete-region inline-start inline-end)
215 (insert rep))
216 (replace-match rep t t)))))))
217
218 (defun org-babel-exp-do-export (info type &optional hash)
219 "Return a string with the exported content of a code block.
220 The function respects the value of the :exports header argument."
221 (flet ((silently () (let ((session (cdr (assoc :session (nth 2 info)))))
222 (when (not (and session (equal "none" session)))
223 (org-babel-exp-results info type 'silent))))
224 (clean () (unless (eq type 'inline) (org-babel-remove-result info))))
225 (case (intern (or (cdr (assoc :exports (nth 2 info))) "code"))
226 ('none (silently) (clean) "")
227 ('code (silently) (clean) (org-babel-exp-code info))
228 ('results (org-babel-exp-results info type nil hash) "")
229 ('both (org-babel-exp-results info type nil hash)
230 (org-babel-exp-code info)))))
231
232 (defun org-babel-exp-code (info)
233 "Return the original code block formatted for export."
234 (org-fill-template
235 "#+BEGIN_SRC %lang%flags\n%body\n#+END_SRC\n"
236 `(("lang" . ,(nth 0 info))
237 ("flags" . ,((lambda (f) (when f (concat " " f))) (nth 3 info)))
238 ("body" . ,(nth 1 info)))))
239
240 (defun org-babel-exp-results (info type &optional silent hash)
241 "Evaluate and return the results of the current code block for export.
242 Results are prepared in a manner suitable for export by org-mode.
243 This function is called by `org-babel-exp-do-export'. The code
244 block will be evaluated. Optional argument SILENT can be used to
245 inhibit insertion of results into the buffer."
246 (when (and org-export-babel-evaluate
247 (not (and hash (equal hash (org-babel-current-result-hash)))))
248 (let ((lang (nth 0 info))
249 (body (nth 1 info)))
250 ;; skip code blocks which we can't evaluate
251 (when (fboundp (intern (concat "org-babel-execute:" lang)))
252 (org-babel-eval-wipe-error-buffer)
253 (prog1 nil
254 (setf (nth 2 info)
255 (org-babel-exp-in-export-file lang
256 (org-babel-process-params
257 (org-babel-merge-params
258 (nth 2 info)
259 `((:results . ,(if silent "silent" "replace")))))))
260 (cond
261 ((or (equal type 'block) (equal type 'inline))
262 (org-babel-execute-src-block nil info))
263 ((equal type 'lob)
264 (save-excursion
265 (re-search-backward org-babel-lob-one-liner-regexp nil t)
266 (org-babel-execute-src-block nil info)))))))))
267
268 (provide 'ob-exp)
269
270
271
272 ;;; ob-exp.el ends here