* lisp/org-icalendar.el (org-print-icalendar-entries): Add 'uid text
[org-mode/org-jambu.git] / lisp / ob-exp.el
blob6594367a6f6d4ec13dcc2c69328a92414a385901
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 (defmacro org-babel-exp-in-export-file (lang &rest body)
58 (declare (indent 1))
59 `(let* ((lang-headers (intern (concat "org-babel-default-header-args:" ,lang)))
60 (heading (nth 4 (ignore-errors (org-heading-components))))
61 (link (when org-current-export-file
62 (org-make-link-string
63 (if heading
64 (concat org-current-export-file "::" heading)
65 org-current-export-file))))
66 (export-buffer (current-buffer)) results)
67 (when link
68 ;; resolve parameters in the original file so that
69 ;; headline and file-wide parameters are included, attempt
70 ;; to go to the same heading in the original file
71 (set-buffer (get-file-buffer org-current-export-file))
72 (save-restriction
73 (condition-case nil
74 (let ((org-link-search-inhibit-query t))
75 (org-open-link-from-string link))
76 (error (when heading
77 (goto-char (point-min))
78 (re-search-forward (regexp-quote heading) nil t))))
79 (setq results ,@body))
80 (set-buffer export-buffer)
81 results)))
83 (defun org-babel-exp-src-block (body &rest headers)
84 "Process source block for export.
85 Depending on the 'export' headers argument in replace the source
86 code block with...
88 both ---- display the code and the results
90 code ---- the default, display the code inside the block but do
91 not process
93 results - just like none only the block is run on export ensuring
94 that it's results are present in the org-mode buffer
96 none ----- do not display either code or results upon export"
97 (interactive)
98 (unless noninteractive (message "org-babel-exp processing..."))
99 (save-excursion
100 (goto-char (match-beginning 0))
101 (let* ((info (org-babel-get-src-block-info 'light))
102 (lang (nth 0 info))
103 (raw-params (nth 2 info)) hash)
104 ;; bail if we couldn't get any info from the block
105 (when info
106 ;; if we're actually going to need the parameters
107 (when (member (cdr (assoc :exports (nth 2 info))) '("both" "results"))
108 (org-babel-exp-in-export-file lang
109 (setf (nth 2 info)
110 (org-babel-process-params
111 (org-babel-merge-params
112 org-babel-default-header-args
113 (org-babel-params-from-buffer)
114 (org-babel-params-from-properties lang)
115 (if (boundp lang-headers) (eval lang-headers) nil)
116 raw-params))))
117 (setf hash (org-babel-sha1-hash info)))
118 ;; expand noweb references in the original file
119 (setf (nth 1 info)
120 (if (and (cdr (assoc :noweb (nth 2 info)))
121 (string= "yes" (cdr (assoc :noweb (nth 2 info)))))
122 (org-babel-expand-noweb-references
123 info (get-file-buffer org-current-export-file))
124 (nth 1 info)))
125 (org-babel-exp-do-export info 'block hash)))))
127 (defun org-babel-exp-inline-src-blocks (start end)
128 "Process inline source blocks between START and END for export.
129 See `org-babel-exp-src-block' for export options, currently the
130 options and are taken from `org-babel-default-inline-header-args'."
131 (interactive)
132 (save-excursion
133 (goto-char start)
134 (while (and (< (point) end)
135 (re-search-forward org-babel-inline-src-block-regexp end t))
136 (let* ((info (save-match-data (org-babel-parse-inline-src-block-match)))
137 (params (nth 2 info)) code-replacement)
138 (save-match-data
139 (goto-char (match-beginning 2))
140 (when (not (org-babel-in-example-or-verbatim))
141 ;; expand noweb references in the original file
142 (setf (nth 1 info)
143 (if (and (cdr (assoc :noweb params))
144 (string= "yes" (cdr (assoc :noweb params))))
145 (org-babel-expand-noweb-references
146 info (get-file-buffer org-current-export-file))
147 (nth 1 info)))
148 (setq code-replacement (org-babel-exp-do-export info 'inline))))
149 (if code-replacement
150 (replace-match code-replacement nil nil nil 1)
151 (org-babel-examplize-region (match-beginning 1) (match-end 1))
152 (forward-char 2))))))
154 (defun org-exp-res/src-name-cleanup ()
155 "Clean up #+results and #+srcname lines for export.
156 This function should only be called after all block processing
157 has taken place."
158 (interactive)
159 (save-excursion
160 (goto-char (point-min))
161 (while (org-re-search-forward-unprotected
162 (concat
163 "\\("org-babel-src-name-regexp"\\|"org-babel-result-regexp"\\)")
164 nil t)
165 (delete-region
166 (progn (beginning-of-line) (point))
167 (progn (end-of-line) (+ 1 (point)))))))
169 (defun org-babel-in-example-or-verbatim ()
170 "Return true if point is in example or verbatim code.
171 Example and verbatim code include escaped portions of
172 an org-mode buffer code that should be treated as normal
173 org-mode text."
174 (or (org-in-indented-comment-line)
175 (save-excursion
176 (save-match-data
177 (goto-char (point-at-bol))
178 (looking-at "[ \t]*:[ \t]")))
179 (org-in-regexps-block-p "^[ \t]*#\\+begin_src" "^[ \t]*#\\+end_src")))
181 (defun org-babel-exp-lob-one-liners (start end)
182 "Process Library of Babel calls between START and END for export.
183 See `org-babel-exp-src-block' for export options. Currently the
184 options are taken from `org-babel-default-header-args'."
185 (interactive)
186 (let (replacement)
187 (save-excursion
188 (goto-char start)
189 (while (and (< (point) end)
190 (re-search-forward org-babel-lob-one-liner-regexp nil t))
191 (setq replacement
192 (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-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 (car (last lob-info)))
206 'lob))))
207 (setq end (+ end (- (length replacement) (length (match-string 0)))))
208 (if replacement (replace-match replacement t t))))))
210 (defun org-babel-exp-do-export (info type &optional hash)
211 "Return a string with the exported content of a code block.
212 The function respects the value of the :exports header argument."
213 (flet ((silently () (let ((session (cdr (assoc :session (nth 2 info)))))
214 (when (not (and session (equal "none" session)))
215 (org-babel-exp-results info type 'silent))))
216 (clean () (unless (eq type 'inline) (org-babel-remove-result info))))
217 (case (intern (or (cdr (assoc :exports (nth 2 info))) "code"))
218 ('none (silently) (clean) "")
219 ('code (silently) (clean) (org-babel-exp-code info))
220 ('results (org-babel-exp-results info type nil hash) "")
221 ('both (org-babel-exp-results info type nil hash)
222 (org-babel-exp-code info)))))
224 (defun org-babel-exp-code (info)
225 "Return the original code block formatted for export."
226 (org-fill-template
227 "#+BEGIN_SRC %lang%flags\n%body\n#+END_SRC\n"
228 `(("lang" . ,(nth 0 info))
229 ("flags" . ,((lambda (f) (when f (concat " " f))) (nth 3 info)))
230 ("body" . ,(nth 1 info)))))
232 (defun org-babel-exp-results (info type &optional silent hash)
233 "Evaluate and return the results of the current code block for export.
234 Results are prepared in a manner suitable for export by org-mode.
235 This function is called by `org-babel-exp-do-export'. The code
236 block will be evaluated. Optional argument SILENT can be used to
237 inhibit insertion of results into the buffer."
238 (when (and org-export-babel-evaluate
239 (not (and hash
240 (equal hash (org-babel-exp-in-export-file (nth 0 info)
241 (org-babel-result-hash info))))))
242 (let ((lang (nth 0 info))
243 (body (nth 1 info)))
244 ;; skip code blocks which we can't evaluate
245 (when (fboundp (intern (concat "org-babel-execute:" lang)))
246 (org-babel-eval-wipe-error-buffer)
247 (prog1 nil
248 (setf (nth 2 info)
249 (org-babel-exp-in-export-file lang
250 (org-babel-process-params
251 (org-babel-merge-params
252 (nth 2 info)
253 `((:results . ,(if silent "silent" "replace")))))))
254 (cond
255 ((or (equal type 'block) (equal type 'inline))
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)
264 ;; arch-tag: 523abf4c-76d1-44ed-9f27-e3bddf34bf0f
266 ;;; ob-exp.el ends here