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 | |
dfd98937 | 5 | ;; Authors: Eric Schulte |
c7557a0f | 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" ()) |
153ae947 | 37 | (add-to-list 'org-export-interblocks '(src org-babel-exp-non-block-elements)) |
86fbb8ca | 38 | |
3ab2c837 | 39 | (org-export-blocks-add-block '(src org-babel-exp-src-block nil)) |
86fbb8ca CD |
40 | |
41 | (defcustom org-export-babel-evaluate t | |
42 | "Switch controlling code evaluation during export. | |
afe98dfa | 43 | When set to nil no code will be evaluated as part of the export |
86fbb8ca CD |
44 | process." |
45 | :group 'org-babel | |
46 | :type 'boolean) | |
47 | (put 'org-export-babel-evaluate 'safe-local-variable (lambda (x) (eq x nil))) | |
48 | ||
3ab2c837 BG |
49 | (defmacro org-babel-exp-in-export-file (lang &rest body) |
50 | (declare (indent 1)) | |
51 | `(let* ((lang-headers (intern (concat "org-babel-default-header-args:" ,lang))) | |
afe98dfa CD |
52 | (heading (nth 4 (ignore-errors (org-heading-components)))) |
53 | (link (when org-current-export-file | |
54 | (org-make-link-string | |
55 | (if heading | |
56 | (concat org-current-export-file "::" heading) | |
57 | org-current-export-file)))) | |
58 | (export-buffer (current-buffer)) results) | |
59 | (when link | |
60 | ;; resolve parameters in the original file so that | |
61 | ;; headline and file-wide parameters are included, attempt | |
62 | ;; to go to the same heading in the original file | |
63 | (set-buffer (get-file-buffer org-current-export-file)) | |
64 | (save-restriction | |
65 | (condition-case nil | |
3ab2c837 BG |
66 | (let ((org-link-search-inhibit-query t)) |
67 | (org-open-link-from-string link)) | |
afe98dfa CD |
68 | (error (when heading |
69 | (goto-char (point-min)) | |
70 | (re-search-forward (regexp-quote heading) nil t)))) | |
71 | (setq results ,@body)) | |
72 | (set-buffer export-buffer) | |
73 | results))) | |
e66ba1df | 74 | (def-edebug-spec org-babel-exp-in-export-file (form body)) |
afe98dfa | 75 | |
3ab2c837 | 76 | (defun org-babel-exp-src-block (body &rest headers) |
86fbb8ca CD |
77 | "Process source block for export. |
78 | Depending on the 'export' headers argument in replace the source | |
79 | code block with... | |
80 | ||
81 | both ---- display the code and the results | |
82 | ||
83 | code ---- the default, display the code inside the block but do | |
84 | not process | |
85 | ||
86 | results - just like none only the block is run on export ensuring | |
87 | that it's results are present in the org-mode buffer | |
88 | ||
89 | none ----- do not display either code or results upon export" | |
90 | (interactive) | |
3ab2c837 | 91 | (unless noninteractive (message "org-babel-exp processing...")) |
86fbb8ca CD |
92 | (save-excursion |
93 | (goto-char (match-beginning 0)) | |
afe98dfa CD |
94 | (let* ((info (org-babel-get-src-block-info 'light)) |
95 | (lang (nth 0 info)) | |
3ab2c837 | 96 | (raw-params (nth 2 info)) hash) |
86fbb8ca CD |
97 | ;; bail if we couldn't get any info from the block |
98 | (when info | |
3ab2c837 BG |
99 | ;; if we're actually going to need the parameters |
100 | (when (member (cdr (assoc :exports (nth 2 info))) '("both" "results")) | |
101 | (org-babel-exp-in-export-file lang | |
102 | (setf (nth 2 info) | |
103 | (org-babel-process-params | |
104 | (org-babel-merge-params | |
105 | org-babel-default-header-args | |
3ab2c837 BG |
106 | (org-babel-params-from-properties lang) |
107 | (if (boundp lang-headers) (eval lang-headers) nil) | |
108 | raw-params)))) | |
109 | (setf hash (org-babel-sha1-hash info))) | |
86fbb8ca CD |
110 | ;; expand noweb references in the original file |
111 | (setf (nth 1 info) | |
afe98dfa CD |
112 | (if (and (cdr (assoc :noweb (nth 2 info))) |
113 | (string= "yes" (cdr (assoc :noweb (nth 2 info))))) | |
86fbb8ca CD |
114 | (org-babel-expand-noweb-references |
115 | info (get-file-buffer org-current-export-file)) | |
afe98dfa | 116 | (nth 1 info))) |
3ab2c837 | 117 | (org-babel-exp-do-export info 'block hash))))) |
86fbb8ca | 118 | |
153ae947 BG |
119 | (defvar org-babel-default-lob-header-args) |
120 | (defun org-babel-exp-non-block-elements (start end) | |
121 | "Process inline source and call lines between START and END for export." | |
86fbb8ca CD |
122 | (interactive) |
123 | (save-excursion | |
124 | (goto-char start) | |
153ae947 BG |
125 | (unless (markerp end) |
126 | (let ((m (make-marker))) | |
127 | (set-marker m end (current-buffer)) | |
128 | (setq end m))) | |
129 | (let ((rx (concat "\\(" org-babel-inline-src-block-regexp | |
130 | "\\|" org-babel-lob-one-liner-regexp "\\)"))) | |
131 | (while (and (< (point) (marker-position end)) | |
132 | (re-search-forward rx end t)) | |
133 | (if (save-excursion | |
134 | (goto-char (match-beginning 0)) | |
135 | (looking-at org-babel-inline-src-block-regexp)) | |
136 | (progn | |
137 | (forward-char 1) | |
138 | (let* ((info (save-match-data | |
139 | (org-babel-parse-inline-src-block-match))) | |
140 | (params (nth 2 info))) | |
141 | (save-match-data | |
142 | (goto-char (match-beginning 2)) | |
143 | (unless (org-babel-in-example-or-verbatim) | |
144 | ;; expand noweb references in the original file | |
145 | (setf (nth 1 info) | |
146 | (if (and (cdr (assoc :noweb params)) | |
147 | (string= "yes" (cdr (assoc :noweb params)))) | |
148 | (org-babel-expand-noweb-references | |
149 | info (get-file-buffer org-current-export-file)) | |
150 | (nth 1 info))) | |
151 | (let ((code-replacement (save-match-data | |
152 | (org-babel-exp-do-export | |
153 | info 'inline)))) | |
154 | (if code-replacement | |
155 | (progn (replace-match code-replacement nil nil nil 1) | |
156 | (delete-char 1)) | |
157 | (org-babel-examplize-region (match-beginning 1) | |
158 | (match-end 1)) | |
159 | (forward-char 2))))))) | |
e66ba1df | 160 | (unless (org-babel-in-example-or-verbatim) |
153ae947 BG |
161 | (let* ((lob-info (org-babel-lob-get-info)) |
162 | (inlinep (match-string 11)) | |
163 | (inline-start (match-end 11)) | |
164 | (inline-end (match-end 0)) | |
165 | (rep (let ((lob-info (org-babel-lob-get-info))) | |
166 | (save-match-data | |
167 | (org-babel-exp-do-export | |
168 | (list "emacs-lisp" "results" | |
169 | (org-babel-merge-params | |
170 | org-babel-default-header-args | |
171 | org-babel-default-lob-header-args | |
172 | (org-babel-params-from-properties) | |
173 | (org-babel-parse-header-arguments | |
174 | (org-babel-clean-text-properties | |
175 | (concat ":var results=" | |
176 | (mapconcat #'identity | |
177 | (butlast lob-info) | |
178 | " "))))) | |
179 | "" nil (car (last lob-info))) | |
180 | 'lob))))) | |
181 | (if inlinep | |
182 | (save-excursion | |
183 | (goto-char inline-start) | |
184 | (delete-region inline-start inline-end) | |
185 | (insert rep)) | |
186 | (replace-match rep t t))))))))) | |
86fbb8ca CD |
187 | |
188 | (defun org-babel-in-example-or-verbatim () | |
189 | "Return true if point is in example or verbatim code. | |
190 | Example and verbatim code include escaped portions of | |
191 | an org-mode buffer code that should be treated as normal | |
192 | org-mode text." | |
e66ba1df | 193 | (or (save-match-data |
3ab2c837 | 194 | (save-excursion |
86fbb8ca CD |
195 | (goto-char (point-at-bol)) |
196 | (looking-at "[ \t]*:[ \t]"))) | |
3ab2c837 | 197 | (org-in-verbatim-emphasis) |
e66ba1df BG |
198 | (org-in-block-p org-list-forbidden-blocks) |
199 | (org-between-regexps-p "^[ \t]*#\\+begin_src" "^[ \t]*#\\+end_src"))) | |
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))) | |
153ae947 BG |
221 | ("body" . ,(if (string= (nth 0 info) "org") |
222 | (replace-regexp-in-string "^" "," (nth 1 info)) | |
223 | (nth 1 info)))))) | |
86fbb8ca | 224 | |
3ab2c837 | 225 | (defun org-babel-exp-results (info type &optional silent hash) |
86fbb8ca CD |
226 | "Evaluate and return the results of the current code block for export. |
227 | Results are prepared in a manner suitable for export by org-mode. | |
228 | This function is called by `org-babel-exp-do-export'. The code | |
229 | block will be evaluated. Optional argument SILENT can be used to | |
230 | inhibit insertion of results into the buffer." | |
3ab2c837 BG |
231 | (when (and org-export-babel-evaluate |
232 | (not (and hash (equal hash (org-babel-current-result-hash))))) | |
233 | (let ((lang (nth 0 info)) | |
234 | (body (nth 1 info))) | |
235 | ;; skip code blocks which we can't evaluate | |
236 | (when (fboundp (intern (concat "org-babel-execute:" lang))) | |
237 | (org-babel-eval-wipe-error-buffer) | |
238 | (prog1 nil | |
239 | (setf (nth 2 info) | |
240 | (org-babel-exp-in-export-file lang | |
241 | (org-babel-process-params | |
afe98dfa CD |
242 | (org-babel-merge-params |
243 | (nth 2 info) | |
3ab2c837 BG |
244 | `((:results . ,(if silent "silent" "replace"))))))) |
245 | (cond | |
e66ba1df BG |
246 | ((equal type 'block) |
247 | (org-babel-execute-src-block nil info)) | |
248 | ((equal type 'inline) | |
249 | ;; position the point on the inline source block allowing | |
250 | ;; `org-babel-insert-result' to check that the block is | |
251 | ;; inline | |
252 | (re-search-backward "[ \f\t\n\r\v]" nil t) | |
253 | (re-search-forward org-babel-inline-src-block-regexp nil t) | |
254 | (re-search-backward "src_" nil t) | |
3ab2c837 BG |
255 | (org-babel-execute-src-block nil info)) |
256 | ((equal type 'lob) | |
257 | (save-excursion | |
258 | (re-search-backward org-babel-lob-one-liner-regexp nil t) | |
259 | (org-babel-execute-src-block nil info))))))))) | |
86fbb8ca CD |
260 | |
261 | (provide 'ob-exp) | |
262 | ||
5b409b39 | 263 | |
86fbb8ca CD |
264 | |
265 | ;;; ob-exp.el ends here |