remove when-let which isn't a real Emacs macro
[org-mode.git] / lisp / ob-exp.el
blob09e85caf237b4e0a7fafed48bd88e873c89c9a0d
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 ;;; Commentary:
27 ;; See the online documentation for more information
28 ;;
29 ;; http://orgmode.org/worg/org-contrib/babel/
31 ;;; Code:
32 (require 'ob)
33 (require 'org-exp-blocks)
34 (eval-when-compile
35 (require 'cl))
37 (defvar obe-marker nil)
38 (defvar org-current-export-file)
39 (defvar org-babel-lob-one-liner-regexp)
40 (defvar org-babel-ref-split-regexp)
41 (declare-function org-babel-lob-get-info "ob-lob" ())
42 (declare-function org-babel-eval-wipe-error-buffer "ob-eval" ())
43 (add-to-list 'org-export-interblocks '(src org-babel-exp-inline-src-blocks))
44 (add-to-list 'org-export-interblocks '(lob org-babel-exp-lob-one-liners))
45 (add-hook 'org-export-blocks-postblock-hook 'org-exp-res/src-name-cleanup)
47 (org-export-blocks-add-block '(src org-babel-exp-src-block nil))
49 (defcustom org-export-babel-evaluate t
50 "Switch controlling code evaluation during export.
51 When set to nil no code will be evaluated as part of the export
52 process."
53 :group 'org-babel
54 :type 'boolean)
55 (put 'org-export-babel-evaluate 'safe-local-variable (lambda (x) (eq x nil)))
57 (defvar org-babel-function-def-export-keyword "function"
58 "The keyword to substitute for the source name line on export.
59 When exporting a source block function, this keyword will
60 appear in the exported version in the place of source name
61 line. A source block is considered to be a source block function
62 if the source name is present and is followed by a parenthesized
63 argument list. The parentheses may be empty or contain
64 whitespace. An example is the following which generates n random
65 \(uniform) numbers.
67 #+source: rand(n)
68 #+begin_src R
69 runif(n)
70 #+end_src")
72 (defvar org-babel-function-def-export-indent 4
73 "Number of characters to indent a source block on export.
74 When exporting a source block function, the block contents will
75 be indented by this many characters. See
76 `org-babel-function-def-export-name' for the definition of a
77 source block function.")
79 (defmacro org-babel-exp-in-export-file (lang &rest body)
80 (declare (indent 1))
81 `(let* ((lang-headers (intern (concat "org-babel-default-header-args:" ,lang)))
82 (heading (nth 4 (ignore-errors (org-heading-components))))
83 (link (when org-current-export-file
84 (org-make-link-string
85 (if heading
86 (concat org-current-export-file "::" heading)
87 org-current-export-file))))
88 (export-buffer (current-buffer)) results)
89 (when link
90 ;; resolve parameters in the original file so that
91 ;; headline and file-wide parameters are included, attempt
92 ;; to go to the same heading in the original file
93 (set-buffer (get-file-buffer org-current-export-file))
94 (save-restriction
95 (condition-case nil
96 (let ((org-link-search-inhibit-query t))
97 (org-open-link-from-string link))
98 (error (when heading
99 (goto-char (point-min))
100 (re-search-forward (regexp-quote heading) nil t))))
101 (setq results ,@body))
102 (set-buffer export-buffer)
103 results)))
105 (defun org-babel-exp-src-block (body &rest headers)
106 "Process source block for export.
107 Depending on the 'export' headers argument in replace the source
108 code block with...
110 both ---- display the code and the results
112 code ---- the default, display the code inside the block but do
113 not process
115 results - just like none only the block is run on export ensuring
116 that it's results are present in the org-mode buffer
118 none ----- do not display either code or results upon export"
119 (interactive)
120 (message "org-babel-exp processing...")
121 (save-excursion
122 (goto-char (match-beginning 0))
123 (let* ((info (org-babel-get-src-block-info 'light))
124 (lang (nth 0 info))
125 (raw-params (nth 2 info)) hash)
126 ;; bail if we couldn't get any info from the block
127 (when info
128 ;; if we're actually going to need the parameters
129 (when (member (cdr (assoc :exports (nth 2 info))) '("both" "results"))
130 (org-babel-exp-in-export-file lang
131 (setf (nth 2 info)
132 (org-babel-process-params
133 (org-babel-merge-params
134 org-babel-default-header-args
135 (org-babel-params-from-buffer)
136 (org-babel-params-from-properties lang)
137 (if (boundp lang-headers) (eval lang-headers) nil)
138 raw-params))))
139 (setf hash (org-babel-sha1-hash info)))
140 ;; expand noweb references in the original file
141 (setf (nth 1 info)
142 (if (and (cdr (assoc :noweb (nth 2 info)))
143 (string= "yes" (cdr (assoc :noweb (nth 2 info)))))
144 (org-babel-expand-noweb-references
145 info (get-file-buffer org-current-export-file))
146 (nth 1 info)))
147 (org-babel-exp-do-export info 'block hash)))))
149 (defun org-babel-exp-inline-src-blocks (start end)
150 "Process inline source blocks between START and END for export.
151 See `org-babel-exp-src-block' for export options, currently the
152 options and are taken from `org-babel-default-inline-header-args'."
153 (interactive)
154 (save-excursion
155 (goto-char start)
156 (while (and (< (point) end)
157 (re-search-forward org-babel-inline-src-block-regexp end t))
158 (let* ((info (save-match-data (org-babel-parse-inline-src-block-match)))
159 (params (nth 2 info)) code-replacement)
160 (save-match-data
161 (goto-char (match-beginning 2))
162 (when (not (org-babel-in-example-or-verbatim))
163 ;; expand noweb references in the original file
164 (setf (nth 1 info)
165 (if (and (cdr (assoc :noweb params))
166 (string= "yes" (cdr (assoc :noweb params))))
167 (org-babel-expand-noweb-references
168 info (get-file-buffer org-current-export-file))
169 (nth 1 info)))
170 (setq code-replacement (org-babel-exp-do-export info 'inline))))
171 (if code-replacement
172 (replace-match code-replacement nil nil nil 1)
173 (org-babel-examplize-region (match-beginning 1) (match-end 1))
174 (forward-char 2))))))
176 (defun org-exp-res/src-name-cleanup ()
177 "Clean up #+results and #+srcname lines for export.
178 This function should only be called after all block processing
179 has taken place."
180 (interactive)
181 (save-excursion
182 (goto-char (point-min))
183 (while (org-re-search-forward-unprotected
184 (concat
185 "\\("org-babel-src-name-regexp"\\|"org-babel-result-regexp"\\)")
186 nil t)
187 (delete-region
188 (progn (beginning-of-line) (point))
189 (progn (end-of-line) (+ 1 (point)))))))
191 (defun org-babel-in-example-or-verbatim ()
192 "Return true if point is in example or verbatim code.
193 Example and verbatim code include escaped portions of
194 an org-mode buffer code that should be treated as normal
195 org-mode text."
196 (or (org-in-indented-comment-line)
197 (save-excursion
198 (save-match-data
199 (goto-char (point-at-bol))
200 (looking-at "[ \t]*:[ \t]")))
201 (org-in-regexps-block-p "^[ \t]*#\\+begin_src" "^[ \t]*#\\+end_src")))
203 (defun org-babel-exp-lob-one-liners (start end)
204 "Process Library of Babel calls between START and END for export.
205 See `org-babel-exp-src-block' for export options. Currently the
206 options are taken from `org-babel-default-header-args'."
207 (interactive)
208 (let (replacement)
209 (save-excursion
210 (goto-char start)
211 (while (and (< (point) end)
212 (re-search-forward org-babel-lob-one-liner-regexp nil t))
213 (setq replacement
214 (let ((lob-info (org-babel-lob-get-info)))
215 (save-match-data
216 (org-babel-exp-do-export
217 (list "emacs-lisp" "results"
218 (org-babel-merge-params
219 org-babel-default-header-args
220 (org-babel-params-from-buffer)
221 (org-babel-params-from-properties)
222 (org-babel-parse-header-arguments
223 (org-babel-clean-text-properties
224 (concat ":var results="
225 (mapconcat #'identity
226 (butlast lob-info) " ")))))
227 (car (last lob-info)))
228 'lob))))
229 (setq end (+ end (- (length replacement) (length (match-string 0)))))
230 (if replacement (replace-match replacement t t))))))
232 (defun org-babel-exp-do-export (info type &optional hash)
233 "Return a string with the exported content of a code block.
234 The function respects the value of the :exports header argument."
235 (flet ((silently () (let ((session (cdr (assoc :session (nth 2 info)))))
236 (when (not (and session (equal "none" session)))
237 (org-babel-exp-results info type 'silent))))
238 (clean () (unless (eq type 'inline) (org-babel-remove-result info))))
239 (case (intern (or (cdr (assoc :exports (nth 2 info))) "code"))
240 ('none (silently) (clean) "")
241 ('code (silently) (clean) (org-babel-exp-code info))
242 ('results (org-babel-exp-results info type nil hash) "")
243 ('both (org-babel-exp-results info type nil hash)
244 (org-babel-exp-code info)))))
246 (defun org-babel-exp-code (info)
247 "Return the original code block formatted for export."
248 (org-fill-template
249 "#+BEGIN_SRC %lang%flags\n%body\n#+END_SRC\n"
250 `(("lang" . ,(nth 0 info))
251 ("flags" . ,((lambda (f) (when f (concat " " f))) (nth 3 info)))
252 ("body" . ,(nth 1 info)))))
254 (defun org-babel-exp-results (info type &optional silent hash)
255 "Evaluate and return the results of the current code block for export.
256 Results are prepared in a manner suitable for export by org-mode.
257 This function is called by `org-babel-exp-do-export'. The code
258 block will be evaluated. Optional argument SILENT can be used to
259 inhibit insertion of results into the buffer."
260 (when (and org-export-babel-evaluate
261 (not (and hash
262 (equal hash (org-babel-exp-in-export-file (nth 0 info)
263 (org-babel-result-hash info))))))
264 (let ((lang (nth 0 info))
265 (body (nth 1 info)))
266 ;; skip code blocks which we can't evaluate
267 (when (fboundp (intern (concat "org-babel-execute:" lang)))
268 (org-babel-eval-wipe-error-buffer)
269 (prog1 nil
270 (setf (nth 2 info)
271 (org-babel-exp-in-export-file lang
272 (org-babel-process-params
273 (org-babel-merge-params
274 (nth 2 info)
275 `((:results . ,(if silent "silent" "replace")))))))
276 (cond
277 ((or (equal type 'block) (equal type 'inline))
278 (org-babel-execute-src-block nil info))
279 ((equal type 'lob)
280 (save-excursion
281 (re-search-backward org-babel-lob-one-liner-regexp nil t)
282 (org-babel-execute-src-block nil info)))))))))
284 (provide 'ob-exp)
286 ;; arch-tag: 523abf4c-76d1-44ed-9f27-e3bddf34bf0f
288 ;;; ob-exp.el ends here