Commit | Line | Data |
---|---|---|
86fbb8ca CD |
1 | ;;; ob-exp.el --- Exportation of org-babel source blocks |
2 | ||
b73f1974 | 3 | ;; Copyright (C) 2009-2012 Free Software Foundation, Inc. |
86fbb8ca | 4 | |
c7557a0f GM |
5 | ;; Author: Eric Schulte |
6 | ;; Dan Davison | |
86fbb8ca CD |
7 | ;; Keywords: literate programming, reproducible research |
8 | ;; Homepage: http://orgmode.org | |
86fbb8ca CD |
9 | |
10 | ;; This file is part of GNU Emacs. | |
11 | ||
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. | |
16 | ||
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. | |
21 | ||
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/>. | |
24 | ||
86fbb8ca CD |
25 | ;;; Code: |
26 | (require 'ob) | |
27 | (require 'org-exp-blocks) | |
28 | (eval-when-compile | |
29 | (require 'cl)) | |
30 | ||
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" ()) | |
acedf35c | 36 | (declare-function org-babel-eval-wipe-error-buffer "ob-eval" ()) |
86fbb8ca CD |
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)) | |
86fbb8ca | 39 | |
3ab2c837 | 40 | (org-export-blocks-add-block '(src org-babel-exp-src-block nil)) |
86fbb8ca CD |
41 | |
42 | (defcustom org-export-babel-evaluate t | |
43 | "Switch controlling code evaluation during export. | |
afe98dfa | 44 | When set to nil no code will be evaluated as part of the export |
86fbb8ca CD |
45 | process." |
46 | :group 'org-babel | |
47 | :type 'boolean) | |
48 | (put 'org-export-babel-evaluate 'safe-local-variable (lambda (x) (eq x nil))) | |
49 | ||
3ab2c837 BG |
50 | (defmacro org-babel-exp-in-export-file (lang &rest body) |
51 | (declare (indent 1)) | |
52 | `(let* ((lang-headers (intern (concat "org-babel-default-header-args:" ,lang))) | |
afe98dfa CD |
53 | (heading (nth 4 (ignore-errors (org-heading-components)))) |
54 | (link (when org-current-export-file | |
55 | (org-make-link-string | |
56 | (if heading | |
57 | (concat org-current-export-file "::" heading) | |
58 | org-current-export-file)))) | |
59 | (export-buffer (current-buffer)) results) | |
60 | (when link | |
61 | ;; resolve parameters in the original file so that | |
62 | ;; headline and file-wide parameters are included, attempt | |
63 | ;; to go to the same heading in the original file | |
64 | (set-buffer (get-file-buffer org-current-export-file)) | |
65 | (save-restriction | |
66 | (condition-case nil | |
3ab2c837 BG |
67 | (let ((org-link-search-inhibit-query t)) |
68 | (org-open-link-from-string link)) | |
afe98dfa CD |
69 | (error (when heading |
70 | (goto-char (point-min)) | |
71 | (re-search-forward (regexp-quote heading) nil t)))) | |
72 | (setq results ,@body)) | |
73 | (set-buffer export-buffer) | |
74 | results))) | |
e66ba1df | 75 | (def-edebug-spec org-babel-exp-in-export-file (form body)) |
afe98dfa | 76 | |
3ab2c837 | 77 | (defun org-babel-exp-src-block (body &rest headers) |
86fbb8ca CD |
78 | "Process source block for export. |
79 | Depending on the 'export' headers argument in replace the source | |
80 | code block with... | |
81 | ||
82 | both ---- display the code and the results | |
83 | ||
84 | code ---- the default, display the code inside the block but do | |
85 | not process | |
86 | ||
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 | |
89 | ||
90 | none ----- do not display either code or results upon export" | |
91 | (interactive) | |
3ab2c837 | 92 | (unless noninteractive (message "org-babel-exp processing...")) |
86fbb8ca CD |
93 | (save-excursion |
94 | (goto-char (match-beginning 0)) | |
afe98dfa CD |
95 | (let* ((info (org-babel-get-src-block-info 'light)) |
96 | (lang (nth 0 info)) | |
3ab2c837 | 97 | (raw-params (nth 2 info)) hash) |
86fbb8ca CD |
98 | ;; bail if we couldn't get any info from the block |
99 | (when info | |
3ab2c837 BG |
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 | |
3ab2c837 BG |
107 | (org-babel-params-from-properties lang) |
108 | (if (boundp lang-headers) (eval lang-headers) nil) | |
109 | raw-params)))) | |
110 | (setf hash (org-babel-sha1-hash info))) | |
86fbb8ca CD |
111 | ;; expand noweb references in the original file |
112 | (setf (nth 1 info) | |
afe98dfa CD |
113 | (if (and (cdr (assoc :noweb (nth 2 info))) |
114 | (string= "yes" (cdr (assoc :noweb (nth 2 info))))) | |
86fbb8ca CD |
115 | (org-babel-expand-noweb-references |
116 | info (get-file-buffer org-current-export-file)) | |
afe98dfa | 117 | (nth 1 info))) |
3ab2c837 | 118 | (org-babel-exp-do-export info 'block hash))))) |
86fbb8ca CD |
119 | |
120 | (defun org-babel-exp-inline-src-blocks (start end) | |
121 | "Process inline source blocks between START and END for export. | |
3ab2c837 | 122 | See `org-babel-exp-src-block' for export options, currently the |
86fbb8ca CD |
123 | options and are taken from `org-babel-default-inline-header-args'." |
124 | (interactive) | |
125 | (save-excursion | |
126 | (goto-char start) | |
127 | (while (and (< (point) end) | |
128 | (re-search-forward org-babel-inline-src-block-regexp end t)) | |
129 | (let* ((info (save-match-data (org-babel-parse-inline-src-block-match))) | |
e66ba1df | 130 | (params (nth 2 info))) |
3ab2c837 BG |
131 | (save-match-data |
132 | (goto-char (match-beginning 2)) | |
e66ba1df | 133 | (unless (org-babel-in-example-or-verbatim) |
3ab2c837 BG |
134 | ;; expand noweb references in the original file |
135 | (setf (nth 1 info) | |
136 | (if (and (cdr (assoc :noweb params)) | |
137 | (string= "yes" (cdr (assoc :noweb params)))) | |
138 | (org-babel-expand-noweb-references | |
139 | info (get-file-buffer org-current-export-file)) | |
140 | (nth 1 info))) | |
e66ba1df BG |
141 | (let ((code-replacement (save-match-data |
142 | (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))))))))) | |
86fbb8ca CD |
147 | |
148 | (defun org-babel-in-example-or-verbatim () | |
149 | "Return true if point is in example or verbatim code. | |
150 | Example and verbatim code include escaped portions of | |
151 | an org-mode buffer code that should be treated as normal | |
152 | org-mode text." | |
e66ba1df | 153 | (or (save-match-data |
3ab2c837 | 154 | (save-excursion |
86fbb8ca CD |
155 | (goto-char (point-at-bol)) |
156 | (looking-at "[ \t]*:[ \t]"))) | |
3ab2c837 | 157 | (org-in-verbatim-emphasis) |
e66ba1df BG |
158 | (org-in-block-p org-list-forbidden-blocks) |
159 | (org-between-regexps-p "^[ \t]*#\\+begin_src" "^[ \t]*#\\+end_src"))) | |
86fbb8ca | 160 | |
3ab2c837 | 161 | (defvar org-babel-default-lob-header-args) |
86fbb8ca CD |
162 | (defun org-babel-exp-lob-one-liners (start end) |
163 | "Process Library of Babel calls between START and END for export. | |
3ab2c837 | 164 | See `org-babel-exp-src-block' for export options. Currently the |
86fbb8ca CD |
165 | options are taken from `org-babel-default-header-args'." |
166 | (interactive) | |
3ab2c837 BG |
167 | (save-excursion |
168 | (goto-char start) | |
169 | (while (and (< (point) end) | |
e66ba1df BG |
170 | (re-search-forward org-babel-lob-one-liner-regexp end t)) |
171 | (unless (org-babel-in-example-or-verbatim) | |
3ab2c837 BG |
172 | (let* ((lob-info (org-babel-lob-get-info)) |
173 | (inlinep (match-string 11)) | |
174 | (inline-start (match-end 11)) | |
175 | (inline-end (match-end 0)) | |
176 | (rep (let ((lob-info (org-babel-lob-get-info))) | |
177 | (save-match-data | |
178 | (org-babel-exp-do-export | |
179 | (list "emacs-lisp" "results" | |
180 | (org-babel-merge-params | |
181 | org-babel-default-header-args | |
182 | org-babel-default-lob-header-args | |
3ab2c837 BG |
183 | (org-babel-params-from-properties) |
184 | (org-babel-parse-header-arguments | |
185 | (org-babel-clean-text-properties | |
186 | (concat ":var results=" | |
187 | (mapconcat #'identity | |
188 | (butlast lob-info) " "))))) | |
189 | "" nil (car (last lob-info))) | |
190 | 'lob))))) | |
191 | (setq end (+ end (- (length rep) | |
192 | (- (length (match-string 0)) | |
193 | (length (or (match-string 11) "")))))) | |
194 | (if inlinep | |
195 | (save-excursion | |
196 | (goto-char inline-start) | |
197 | (delete-region inline-start inline-end) | |
198 | (insert rep)) | |
199 | (replace-match rep t t))))))) | |
86fbb8ca | 200 | |
3ab2c837 | 201 | (defun org-babel-exp-do-export (info type &optional hash) |
86fbb8ca CD |
202 | "Return a string with the exported content of a code block. |
203 | The function respects the value of the :exports header argument." | |
204 | (flet ((silently () (let ((session (cdr (assoc :session (nth 2 info))))) | |
3ab2c837 | 205 | (when (not (and session (equal "none" session))) |
86fbb8ca | 206 | (org-babel-exp-results info type 'silent)))) |
3ab2c837 | 207 | (clean () (unless (eq type 'inline) (org-babel-remove-result info)))) |
86fbb8ca | 208 | (case (intern (or (cdr (assoc :exports (nth 2 info))) "code")) |
3ab2c837 BG |
209 | ('none (silently) (clean) "") |
210 | ('code (silently) (clean) (org-babel-exp-code info)) | |
211 | ('results (org-babel-exp-results info type nil hash) "") | |
212 | ('both (org-babel-exp-results info type nil hash) | |
213 | (org-babel-exp-code info))))) | |
86fbb8ca | 214 | |
3ab2c837 BG |
215 | (defun org-babel-exp-code (info) |
216 | "Return the original code block formatted for export." | |
217 | (org-fill-template | |
e66ba1df | 218 | "#+BEGIN_SRC %lang%flags\n%body\n#+END_SRC" |
3ab2c837 BG |
219 | `(("lang" . ,(nth 0 info)) |
220 | ("flags" . ,((lambda (f) (when f (concat " " f))) (nth 3 info))) | |
221 | ("body" . ,(nth 1 info))))) | |
86fbb8ca | 222 | |
3ab2c837 | 223 | (defun org-babel-exp-results (info type &optional silent hash) |
86fbb8ca CD |
224 | "Evaluate and return the results of the current code block for export. |
225 | Results are prepared in a manner suitable for export by org-mode. | |
226 | This function is called by `org-babel-exp-do-export'. The code | |
227 | block will be evaluated. Optional argument SILENT can be used to | |
228 | inhibit insertion of results into the buffer." | |
3ab2c837 BG |
229 | (when (and org-export-babel-evaluate |
230 | (not (and hash (equal hash (org-babel-current-result-hash))))) | |
231 | (let ((lang (nth 0 info)) | |
232 | (body (nth 1 info))) | |
233 | ;; skip code blocks which we can't evaluate | |
234 | (when (fboundp (intern (concat "org-babel-execute:" lang))) | |
235 | (org-babel-eval-wipe-error-buffer) | |
236 | (prog1 nil | |
237 | (setf (nth 2 info) | |
238 | (org-babel-exp-in-export-file lang | |
239 | (org-babel-process-params | |
afe98dfa CD |
240 | (org-babel-merge-params |
241 | (nth 2 info) | |
3ab2c837 BG |
242 | `((:results . ,(if silent "silent" "replace"))))))) |
243 | (cond | |
e66ba1df BG |
244 | ((equal type 'block) |
245 | (org-babel-execute-src-block nil info)) | |
246 | ((equal type 'inline) | |
247 | ;; position the point on the inline source block allowing | |
248 | ;; `org-babel-insert-result' to check that the block is | |
249 | ;; inline | |
250 | (re-search-backward "[ \f\t\n\r\v]" nil t) | |
251 | (re-search-forward org-babel-inline-src-block-regexp nil t) | |
252 | (re-search-backward "src_" nil t) | |
3ab2c837 BG |
253 | (org-babel-execute-src-block nil info)) |
254 | ((equal type 'lob) | |
255 | (save-excursion | |
256 | (re-search-backward org-babel-lob-one-liner-regexp nil t) | |
257 | (org-babel-execute-src-block nil info))))))))) | |
86fbb8ca CD |
258 | |
259 | (provide 'ob-exp) | |
260 | ||
5b409b39 | 261 | |
86fbb8ca CD |
262 | |
263 | ;;; ob-exp.el ends here |