tests: more thorough testing of inline call lines -- passing all tests
[org-mode/org-jambu.git] / lisp / ob-exp.el
blob9bfb9277175cd50a0878b8ee110d11afc1d1536c
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-excursion
170 (save-match-data
171 (goto-char (point-at-bol))
172 (looking-at "[ \t]*:[ \t]")))
173 (org-in-regexps-block-p "^[ \t]*#\\+begin_src" "^[ \t]*#\\+end_src")))
175 (defvar org-babel-default-lob-header-args)
176 (defun org-babel-exp-lob-one-liners (start end)
177 "Process Library of Babel calls between START and END for export.
178 See `org-babel-exp-src-block' for export options. Currently the
179 options are taken from `org-babel-default-header-args'."
180 (interactive)
181 (save-excursion
182 (goto-char start)
183 (while (and (< (point) end)
184 (re-search-forward org-babel-lob-one-liner-regexp nil t))
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 (rep (let ((lob-info (org-babel-lob-get-info)))
190 (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-buffer)
197 (org-babel-params-from-properties)
198 (org-babel-parse-header-arguments
199 (org-babel-clean-text-properties
200 (concat ":var results="
201 (mapconcat #'identity
202 (butlast lob-info) " ")))))
203 "" nil (car (last lob-info)))
204 'lob)))))
205 (setq end (+ end (- (length rep)
206 (- (length (match-string 0))
207 (length (or (match-string 11) ""))))))
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-exp-do-export (info type &optional hash)
216 "Return a string with the exported content of a code block.
217 The function respects the value of the :exports header argument."
218 (flet ((silently () (let ((session (cdr (assoc :session (nth 2 info)))))
219 (when (not (and session (equal "none" session)))
220 (org-babel-exp-results info type 'silent))))
221 (clean () (unless (eq type 'inline) (org-babel-remove-result info))))
222 (case (intern (or (cdr (assoc :exports (nth 2 info))) "code"))
223 ('none (silently) (clean) "")
224 ('code (silently) (clean) (org-babel-exp-code info))
225 ('results (org-babel-exp-results info type nil hash) "")
226 ('both (org-babel-exp-results info type nil hash)
227 (org-babel-exp-code info)))))
229 (defun org-babel-exp-code (info)
230 "Return the original code block formatted for export."
231 (org-fill-template
232 "#+BEGIN_SRC %lang%flags\n%body\n#+END_SRC\n"
233 `(("lang" . ,(nth 0 info))
234 ("flags" . ,((lambda (f) (when f (concat " " f))) (nth 3 info)))
235 ("body" . ,(nth 1 info)))))
237 (defun org-babel-exp-results (info type &optional silent hash)
238 "Evaluate and return the results of the current code block for export.
239 Results are prepared in a manner suitable for export by org-mode.
240 This function is called by `org-babel-exp-do-export'. The code
241 block will be evaluated. Optional argument SILENT can be used to
242 inhibit insertion of results into the buffer."
243 (when (and org-export-babel-evaluate
244 (not (and hash
245 (equal hash (org-babel-exp-in-export-file (nth 0 info)
246 (org-babel-result-hash info))))))
247 (let ((lang (nth 0 info))
248 (body (nth 1 info)))
249 ;; skip code blocks which we can't evaluate
250 (when (fboundp (intern (concat "org-babel-execute:" lang)))
251 (org-babel-eval-wipe-error-buffer)
252 (prog1 nil
253 (setf (nth 2 info)
254 (org-babel-exp-in-export-file lang
255 (org-babel-process-params
256 (org-babel-merge-params
257 (nth 2 info)
258 `((:results . ,(if silent "silent" "replace")))))))
259 (cond
260 ((or (equal type 'block) (equal type 'inline))
261 (org-babel-execute-src-block nil info))
262 ((equal type 'lob)
263 (save-excursion
264 (re-search-backward org-babel-lob-one-liner-regexp nil t)
265 (org-babel-execute-src-block nil info)))))))))
267 (provide 'ob-exp)
269 ;; arch-tag: 523abf4c-76d1-44ed-9f27-e3bddf34bf0f
271 ;;; ob-exp.el ends here