org-footnote: declare function for the compiler
[org-mode/org-tableheadings.git] / lisp / ob-exp.el
blob9d80d70334f1b77ca29d4af2ffce2f40bd337366
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
8 ;; Version: 7.5
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-inline-src-blocks))
38 (add-to-list 'org-export-interblocks '(lob org-babel-exp-lob-one-liners))
39 (add-hook 'org-export-blocks-postblock-hook 'org-exp-res/src-name-cleanup)
41 (org-export-blocks-add-block '(src org-babel-exp-src-block nil))
43 (defcustom org-export-babel-evaluate t
44 "Switch controlling code evaluation during export.
45 When set to nil no code will be evaluated as part of the export
46 process."
47 :group 'org-babel
48 :type 'boolean)
49 (put 'org-export-babel-evaluate 'safe-local-variable (lambda (x) (eq x nil)))
51 (defmacro org-babel-exp-in-export-file (lang &rest body)
52 (declare (indent 1))
53 `(let* ((lang-headers (intern (concat "org-babel-default-header-args:" ,lang)))
54 (heading (nth 4 (ignore-errors (org-heading-components))))
55 (link (when org-current-export-file
56 (org-make-link-string
57 (if heading
58 (concat org-current-export-file "::" heading)
59 org-current-export-file))))
60 (export-buffer (current-buffer)) results)
61 (when link
62 ;; resolve parameters in the original file so that
63 ;; headline and file-wide parameters are included, attempt
64 ;; to go to the same heading in the original file
65 (set-buffer (get-file-buffer org-current-export-file))
66 (save-restriction
67 (condition-case nil
68 (let ((org-link-search-inhibit-query t))
69 (org-open-link-from-string link))
70 (error (when heading
71 (goto-char (point-min))
72 (re-search-forward (regexp-quote heading) nil t))))
73 (setq results ,@body))
74 (set-buffer export-buffer)
75 results)))
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-buffer)
108 (org-babel-params-from-properties lang)
109 (if (boundp lang-headers) (eval lang-headers) nil)
110 raw-params))))
111 (setf hash (org-babel-sha1-hash info)))
112 ;; expand noweb references in the original file
113 (setf (nth 1 info)
114 (if (and (cdr (assoc :noweb (nth 2 info)))
115 (string= "yes" (cdr (assoc :noweb (nth 2 info)))))
116 (org-babel-expand-noweb-references
117 info (get-file-buffer org-current-export-file))
118 (nth 1 info)))
119 (org-babel-exp-do-export info 'block hash)))))
121 (defun org-babel-exp-inline-src-blocks (start end)
122 "Process inline source blocks between START and END for export.
123 See `org-babel-exp-src-block' for export options, currently the
124 options and are taken from `org-babel-default-inline-header-args'."
125 (interactive)
126 (save-excursion
127 (goto-char start)
128 (while (and (< (point) end)
129 (re-search-forward org-babel-inline-src-block-regexp end t))
130 (let* ((info (save-match-data (org-babel-parse-inline-src-block-match)))
131 (params (nth 2 info)) code-replacement)
132 (save-match-data
133 (goto-char (match-beginning 2))
134 (when (not (org-babel-in-example-or-verbatim))
135 ;; expand noweb references in the original file
136 (setf (nth 1 info)
137 (if (and (cdr (assoc :noweb params))
138 (string= "yes" (cdr (assoc :noweb params))))
139 (org-babel-expand-noweb-references
140 info (get-file-buffer org-current-export-file))
141 (nth 1 info)))
142 (setq code-replacement (org-babel-exp-do-export info 'inline))))
143 (if code-replacement
144 (replace-match code-replacement nil nil nil 1)
145 (org-babel-examplize-region (match-beginning 1) (match-end 1))
146 (forward-char 2))))))
148 (defun org-exp-res/src-name-cleanup ()
149 "Clean up #+results and #+srcname lines for export.
150 This function should only be called after all block processing
151 has taken place."
152 (interactive)
153 (save-excursion
154 (goto-char (point-min))
155 (while (org-re-search-forward-unprotected
156 (concat
157 "\\("org-babel-src-name-regexp"\\|"org-babel-result-regexp"\\)")
158 nil t)
159 (delete-region
160 (progn (beginning-of-line) (point))
161 (progn (end-of-line) (+ 1 (point)))))))
163 (defun org-babel-in-example-or-verbatim ()
164 "Return true if point is in example or verbatim code.
165 Example and verbatim code include escaped portions of
166 an org-mode buffer code that should be treated as normal
167 org-mode text."
168 (or (org-in-indented-comment-line)
169 (save-match-data
170 (save-excursion
171 (goto-char (point-at-bol))
172 (looking-at "[ \t]*:[ \t]")))
173 (org-in-verbatim-emphasis)
174 (org-in-regexps-block-p "^[ \t]*#\\+begin_src" "^[ \t]*#\\+end_src")))
176 (defvar org-babel-default-lob-header-args)
177 (defun org-babel-exp-lob-one-liners (start end)
178 "Process Library of Babel calls between START and END for export.
179 See `org-babel-exp-src-block' for export options. Currently the
180 options are taken from `org-babel-default-header-args'."
181 (interactive)
182 (save-excursion
183 (goto-char start)
184 (while (and (< (point) end)
185 (re-search-forward org-babel-lob-one-liner-regexp nil t))
186 (unless (and (match-string 12) (org-babel-in-example-or-verbatim))
187 (let* ((lob-info (org-babel-lob-get-info))
188 (inlinep (match-string 11))
189 (inline-start (match-end 11))
190 (inline-end (match-end 0))
191 (rep (let ((lob-info (org-babel-lob-get-info)))
192 (save-match-data
193 (org-babel-exp-do-export
194 (list "emacs-lisp" "results"
195 (org-babel-merge-params
196 org-babel-default-header-args
197 org-babel-default-lob-header-args
198 (org-babel-params-from-buffer)
199 (org-babel-params-from-properties)
200 (org-babel-parse-header-arguments
201 (org-babel-clean-text-properties
202 (concat ":var results="
203 (mapconcat #'identity
204 (butlast lob-info) " ")))))
205 "" nil (car (last lob-info)))
206 'lob)))))
207 (setq end (+ end (- (length rep)
208 (- (length (match-string 0))
209 (length (or (match-string 11) ""))))))
210 (if inlinep
211 (save-excursion
212 (goto-char inline-start)
213 (delete-region inline-start inline-end)
214 (insert rep))
215 (replace-match rep t t)))))))
217 (defun org-babel-exp-do-export (info type &optional hash)
218 "Return a string with the exported content of a code block.
219 The function respects the value of the :exports header argument."
220 (flet ((silently () (let ((session (cdr (assoc :session (nth 2 info)))))
221 (when (not (and session (equal "none" session)))
222 (org-babel-exp-results info type 'silent))))
223 (clean () (unless (eq type 'inline) (org-babel-remove-result info))))
224 (case (intern (or (cdr (assoc :exports (nth 2 info))) "code"))
225 ('none (silently) (clean) "")
226 ('code (silently) (clean) (org-babel-exp-code info))
227 ('results (org-babel-exp-results info type nil hash) "")
228 ('both (org-babel-exp-results info type nil hash)
229 (org-babel-exp-code info)))))
231 (defun org-babel-exp-code (info)
232 "Return the original code block formatted for export."
233 (org-fill-template
234 "#+BEGIN_SRC %lang%flags\n%body\n#+END_SRC\n"
235 `(("lang" . ,(nth 0 info))
236 ("flags" . ,((lambda (f) (when f (concat " " f))) (nth 3 info)))
237 ("body" . ,(nth 1 info)))))
239 (defun org-babel-exp-results (info type &optional silent hash)
240 "Evaluate and return the results of the current code block for export.
241 Results are prepared in a manner suitable for export by org-mode.
242 This function is called by `org-babel-exp-do-export'. The code
243 block will be evaluated. Optional argument SILENT can be used to
244 inhibit insertion of results into the buffer."
245 (when (and org-export-babel-evaluate
246 (not (and hash
247 (equal hash (org-babel-exp-in-export-file (nth 0 info)
248 (org-babel-result-hash info))))))
249 (let ((lang (nth 0 info))
250 (body (nth 1 info)))
251 ;; skip code blocks which we can't evaluate
252 (when (fboundp (intern (concat "org-babel-execute:" lang)))
253 (org-babel-eval-wipe-error-buffer)
254 (prog1 nil
255 (setf (nth 2 info)
256 (org-babel-exp-in-export-file lang
257 (org-babel-process-params
258 (org-babel-merge-params
259 (nth 2 info)
260 `((:results . ,(if silent "silent" "replace")))))))
261 (cond
262 ((or (equal type 'block) (equal type 'inline))
263 (org-babel-execute-src-block nil info))
264 ((equal type 'lob)
265 (save-excursion
266 (re-search-backward org-babel-lob-one-liner-regexp nil t)
267 (org-babel-execute-src-block nil info)))))))))
269 (provide 'ob-exp)
271 ;; arch-tag: 523abf4c-76d1-44ed-9f27-e3bddf34bf0f
273 ;;; ob-exp.el ends here