Commit | Line | Data |
---|---|---|
86fbb8ca CD |
1 | ;;; ob.el --- working with code blocks in org-mode |
2 | ||
ab422c4d | 3 | ;; Copyright (C) 2009-2013 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 | 25 | ;;; Code: |
eeb4145e | 26 | (eval-when-compile |
e720ae53 | 27 | (require 'cl)) |
acedf35c | 28 | (require 'ob-eval) |
86fbb8ca | 29 | (require 'org-macs) |
8223b1d2 | 30 | (require 'org-compat) |
86fbb8ca | 31 | |
8223b1d2 BG |
32 | (defconst org-babel-exeext |
33 | (if (memq system-type '(windows-nt cygwin)) | |
34 | ".exe" | |
35 | nil)) | |
86fbb8ca | 36 | (defvar org-babel-call-process-region-original) |
3ab2c837 BG |
37 | (defvar org-src-lang-modes) |
38 | (defvar org-babel-library-of-babel) | |
86fbb8ca | 39 | (declare-function show-all "outline" ()) |
e66ba1df | 40 | (declare-function org-reduce "org" (CL-FUNC CL-SEQ &rest CL-KEYS)) |
8223b1d2 | 41 | (declare-function org-mark-ring-push "org" (&optional pos buffer)) |
f1eee0b6 GM |
42 | (declare-function tramp-compat-make-temp-file "tramp-compat" |
43 | (filename &optional dir-flag)) | |
86fbb8ca CD |
44 | (declare-function tramp-dissect-file-name "tramp" (name &optional nodefault)) |
45 | (declare-function tramp-file-name-user "tramp" (vec)) | |
46 | (declare-function tramp-file-name-host "tramp" (vec)) | |
afe98dfa | 47 | (declare-function with-parsed-tramp-file-name "tramp" (filename var &rest body)) |
86fbb8ca | 48 | (declare-function org-icompleting-read "org" (&rest args)) |
acedf35c | 49 | (declare-function org-edit-src-code "org-src" |
afe98dfa CD |
50 | (&optional context code edit-buffer-name quietp)) |
51 | (declare-function org-edit-src-exit "org-src" (&optional context)) | |
86fbb8ca | 52 | (declare-function org-open-at-point "org" (&optional in-emacs reference-buffer)) |
8223b1d2 | 53 | (declare-function org-save-outline-visibility "org-macs" (use-markers &rest body)) |
afe98dfa CD |
54 | (declare-function org-outline-overlay-data "org" (&optional use-markers)) |
55 | (declare-function org-set-outline-overlay-data "org" (data)) | |
86fbb8ca | 56 | (declare-function org-narrow-to-subtree "org" ()) |
afe98dfa CD |
57 | (declare-function org-entry-get "org" |
58 | (pom property &optional inherit literal-nil)) | |
86fbb8ca | 59 | (declare-function org-make-options-regexp "org" (kwds &optional extra)) |
86fbb8ca CD |
60 | (declare-function org-do-remove-indentation "org" (&optional n)) |
61 | (declare-function org-show-context "org" (&optional key)) | |
62 | (declare-function org-at-table-p "org" (&optional table-type)) | |
63 | (declare-function org-cycle "org" (&optional arg)) | |
64 | (declare-function org-uniquify "org" (list)) | |
afe98dfa | 65 | (declare-function org-current-level "org" ()) |
f1eee0b6 | 66 | (declare-function org-table-import "org-table" (file arg)) |
afe98dfa CD |
67 | (declare-function org-add-hook "org-compat" |
68 | (hook function &optional append local)) | |
86fbb8ca CD |
69 | (declare-function org-table-align "org-table" ()) |
70 | (declare-function org-table-end "org-table" (&optional table-type)) | |
71 | (declare-function orgtbl-to-generic "org-table" (table params)) | |
72 | (declare-function orgtbl-to-orgtbl "org-table" (table params)) | |
3ab2c837 | 73 | (declare-function org-babel-tangle-comment-links "ob-tangle" (&optional info)) |
86fbb8ca CD |
74 | (declare-function org-babel-lob-get-info "ob-lob" nil) |
75 | (declare-function org-babel-ref-split-args "ob-ref" (arg-string)) | |
afe98dfa CD |
76 | (declare-function org-babel-ref-parse "ob-ref" (assignment)) |
77 | (declare-function org-babel-ref-resolve "ob-ref" (ref)) | |
3ab2c837 BG |
78 | (declare-function org-babel-ref-goto-headline-id "ob-ref" (id)) |
79 | (declare-function org-babel-ref-headline-body "ob-ref" ()) | |
afe98dfa CD |
80 | (declare-function org-babel-lob-execute-maybe "ob-lob" ()) |
81 | (declare-function org-number-sequence "org-compat" (from &optional to inc)) | |
3ab2c837 | 82 | (declare-function org-at-item-p "org-list" ()) |
acedf35c CD |
83 | (declare-function org-list-parse-list "org-list" (&optional delete)) |
84 | (declare-function org-list-to-generic "org-list" (LIST PARAMS)) | |
3ab2c837 BG |
85 | (declare-function org-list-struct "org-list" ()) |
86 | (declare-function org-list-prevs-alist "org-list" (struct)) | |
87 | (declare-function org-list-get-list-end "org-list" (item struct prevs)) | |
8223b1d2 BG |
88 | (declare-function org-remove-if "org" (predicate seq)) |
89 | (declare-function org-completing-read "org" (&rest args)) | |
bdebdb64 BG |
90 | (declare-function org-escape-code-in-region "org-src" (beg end)) |
91 | (declare-function org-unescape-code-in-string "org-src" (s)) | |
92 | (declare-function org-table-to-lisp "org-table" (&optional txt)) | |
86fbb8ca CD |
93 | |
94 | (defgroup org-babel nil | |
95 | "Code block evaluation and management in `org-mode' documents." | |
96 | :tag "Babel" | |
97 | :group 'org) | |
98 | ||
99 | (defcustom org-confirm-babel-evaluate t | |
100 | "Confirm before evaluation. | |
101 | Require confirmation before interactively evaluating code | |
102 | blocks in Org-mode buffers. The default value of this variable | |
103 | is t, meaning confirmation is required for any code block | |
104 | evaluation. This variable can be set to nil to inhibit any | |
105 | future confirmation requests. This variable can also be set to a | |
106 | function which takes two arguments the language of the code block | |
107 | and the body of the code block. Such a function should then | |
108 | return a non-nil value if the user should be prompted for | |
109 | execution or nil if no prompt is required. | |
110 | ||
111 | Warning: Disabling confirmation may result in accidental | |
112 | evaluation of potentially harmful code. It may be advisable | |
113 | remove code block execution from C-c C-c as further protection | |
114 | against accidental code block evaluation. The | |
115 | `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can be used to | |
116 | remove code block execution from the C-c C-c keybinding." | |
8223b1d2 BG |
117 | :group 'org-babel |
118 | :version "24.1" | |
119 | :type '(choice boolean function)) | |
86fbb8ca CD |
120 | ;; don't allow this variable to be changed through file settings |
121 | (put 'org-confirm-babel-evaluate 'safe-local-variable (lambda (x) (eq x t))) | |
122 | ||
123 | (defcustom org-babel-no-eval-on-ctrl-c-ctrl-c nil | |
124 | "Remove code block evaluation from the C-c C-c key binding." | |
125 | :group 'org-babel | |
372d7b21 | 126 | :version "24.1" |
86fbb8ca CD |
127 | :type 'boolean) |
128 | ||
153ae947 BG |
129 | (defcustom org-babel-results-keyword "RESULTS" |
130 | "Keyword used to name results generated by code blocks. | |
131 | Should be either RESULTS or NAME however any capitalization may | |
132 | be used." | |
133 | :group 'org-babel | |
134 | :type 'string) | |
135 | ||
8223b1d2 BG |
136 | (defcustom org-babel-noweb-wrap-start "<<" |
137 | "String used to begin a noweb reference in a code block. | |
138 | See also `org-babel-noweb-wrap-end'." | |
139 | :group 'org-babel | |
140 | :type 'string) | |
141 | ||
142 | (defcustom org-babel-noweb-wrap-end ">>" | |
143 | "String used to end a noweb reference in a code block. | |
144 | See also `org-babel-noweb-wrap-start'." | |
145 | :group 'org-babel | |
146 | :type 'string) | |
147 | ||
148 | (defun org-babel-noweb-wrap (&optional regexp) | |
149 | (concat org-babel-noweb-wrap-start | |
150 | (or regexp "\\([^ \t\n].+?[^ \t]\\|[^ \t\n]\\)") | |
151 | org-babel-noweb-wrap-end)) | |
152 | ||
86fbb8ca | 153 | (defvar org-babel-src-name-regexp |
e66ba1df | 154 | "^[ \t]*#\\+name:[ \t]*" |
86fbb8ca CD |
155 | "Regular expression used to match a source name line.") |
156 | ||
afe98dfa CD |
157 | (defvar org-babel-multi-line-header-regexp |
158 | "^[ \t]*#\\+headers?:[ \t]*\\([^\n]*\\)$" | |
159 | "Regular expression used to match multi-line header arguments.") | |
160 | ||
86fbb8ca CD |
161 | (defvar org-babel-src-name-w-name-regexp |
162 | (concat org-babel-src-name-regexp | |
afe98dfa CD |
163 | "\\(" |
164 | org-babel-multi-line-header-regexp | |
165 | "\\)*" | |
86fbb8ca CD |
166 | "\\([^ ()\f\t\n\r\v]+\\)\\(\(\\(.*\\)\)\\|\\)") |
167 | "Regular expression matching source name lines with a name.") | |
168 | ||
169 | (defvar org-babel-src-block-regexp | |
170 | (concat | |
3ab2c837 | 171 | ;; (1) indentation (2) lang |
86fbb8ca CD |
172 | "^\\([ \t]*\\)#\\+begin_src[ \t]+\\([^ \f\t\n\r\v]+\\)[ \t]*" |
173 | ;; (3) switches | |
174 | "\\([^\":\n]*\"[^\"\n*]*\"[^\":\n]*\\|[^\":\n]*\\)" | |
175 | ;; (4) header arguments | |
176 | "\\([^\n]*\\)\n" | |
177 | ;; (5) body | |
9a70f03d | 178 | "\\([^\000]*?\n\\)?[ \t]*#\\+end_src") |
86fbb8ca CD |
179 | "Regexp used to identify code blocks.") |
180 | ||
181 | (defvar org-babel-inline-src-block-regexp | |
182 | (concat | |
183 | ;; (1) replacement target (2) lang | |
e66ba1df | 184 | "\\(?:^\\|[^-[:alnum:]]\\)\\(src_\\([^ \f\t\n\r\v]+\\)" |
86fbb8ca CD |
185 | ;; (3,4) (unused, headers) |
186 | "\\(\\|\\[\\(.*?\\)\\]\\)" | |
187 | ;; (5) body | |
188 | "{\\([^\f\n\r\v]+?\\)}\\)") | |
189 | "Regexp used to identify inline src-blocks.") | |
190 | ||
afe98dfa CD |
191 | (defun org-babel-get-header (params key &optional others) |
192 | "Select only header argument of type KEY from a list. | |
193 | Optional argument OTHERS indicates that only the header that do | |
194 | not match KEY should be returned." | |
195 | (delq nil | |
196 | (mapcar | |
197 | (lambda (p) (when (funcall (if others #'not #'identity) (eq (car p) key)) p)) | |
198 | params))) | |
199 | ||
e66ba1df BG |
200 | (defun org-babel-get-inline-src-block-matches() |
201 | "Set match data if within body of an inline source block. | |
202 | Returns non-nil if match-data set" | |
203 | (let ((src-at-0-p (save-excursion | |
204 | (beginning-of-line 1) | |
205 | (string= "src" (thing-at-point 'word)))) | |
206 | (first-line-p (= 1 (line-number-at-pos))) | |
207 | (orig (point))) | |
208 | (let ((search-for (cond ((and src-at-0-p first-line-p "src_")) | |
153ae947 BG |
209 | (first-line-p "[[:punct:] \t]src_") |
210 | (t "[[:punct:] \f\t\n\r\v]src_"))) | |
e66ba1df BG |
211 | (lower-limit (if first-line-p |
212 | nil | |
213 | (- (point-at-bol) 1)))) | |
214 | (save-excursion | |
215 | (when (or (and src-at-0-p (bobp)) | |
216 | (and (re-search-forward "}" (point-at-eol) t) | |
217 | (re-search-backward search-for lower-limit t) | |
218 | (> orig (point)))) | |
219 | (when (looking-at org-babel-inline-src-block-regexp) | |
220 | t )))))) | |
221 | ||
222 | (defvar org-babel-inline-lob-one-liner-regexp) | |
223 | (defun org-babel-get-lob-one-liner-matches() | |
224 | "Set match data if on line of an lob one liner. | |
225 | Returns non-nil if match-data set" | |
226 | (save-excursion | |
227 | (unless (= (point) (point-at-bol)) ;; move before inline block | |
228 | (re-search-backward "[ \f\t\n\r\v]" nil t)) | |
229 | (if (looking-at org-babel-inline-lob-one-liner-regexp) | |
230 | t | |
231 | nil))) | |
232 | ||
afe98dfa | 233 | (defun org-babel-get-src-block-info (&optional light) |
86fbb8ca CD |
234 | "Get information on the current source block. |
235 | ||
afe98dfa CD |
236 | Optional argument LIGHT does not resolve remote variable |
237 | references; a process which could likely result in the execution | |
238 | of other code blocks. | |
239 | ||
86fbb8ca | 240 | Returns a list |
afe98dfa CD |
241 | (language body header-arguments-alist switches name indent)." |
242 | (let ((case-fold-search t) head info name indent) | |
243 | ;; full code block | |
86fbb8ca | 244 | (if (setq head (org-babel-where-is-src-block-head)) |
afe98dfa | 245 | (save-excursion |
86fbb8ca CD |
246 | (goto-char head) |
247 | (setq info (org-babel-parse-src-block-match)) | |
248 | (setq indent (car (last info))) | |
249 | (setq info (butlast info)) | |
afe98dfa CD |
250 | (while (and (forward-line -1) |
251 | (looking-at org-babel-multi-line-header-regexp)) | |
252 | (setf (nth 2 info) | |
253 | (org-babel-merge-params | |
3ab2c837 BG |
254 | (nth 2 info) |
255 | (org-babel-parse-header-arguments (match-string 1))))) | |
afe98dfa | 256 | (when (looking-at org-babel-src-name-w-name-regexp) |
8223b1d2 | 257 | (setq name (org-no-properties (match-string 3))) |
e66ba1df | 258 | (when (and (match-string 5) (> (length (match-string 5)) 0)) |
afe98dfa CD |
259 | (setf (nth 2 info) ;; merge functional-syntax vars and header-args |
260 | (org-babel-merge-params | |
e66ba1df BG |
261 | (mapcar |
262 | (lambda (ref) (cons :var ref)) | |
263 | (mapcar | |
264 | (lambda (var) ;; check that each variable is initialized | |
265 | (if (string-match ".+=.+" var) | |
266 | var | |
267 | (error | |
268 | "variable \"%s\"%s must be assigned a default value" | |
269 | var (if name (format " in block \"%s\"" name) "")))) | |
270 | (org-babel-ref-split-args (match-string 5)))) | |
afe98dfa CD |
271 | (nth 2 info)))))) |
272 | ;; inline source block | |
e66ba1df | 273 | (when (org-babel-get-inline-src-block-matches) |
afe98dfa CD |
274 | (setq info (org-babel-parse-inline-src-block-match)))) |
275 | ;; resolve variable references and add summary parameters | |
276 | (when (and info (not light)) | |
277 | (setf (nth 2 info) (org-babel-process-params (nth 2 info)))) | |
278 | (when info (append info (list name indent))))) | |
86fbb8ca | 279 | |
e66ba1df | 280 | (defvar org-current-export-file) ; dynamically bound |
86fbb8ca CD |
281 | (defun org-babel-confirm-evaluate (info) |
282 | "Confirm evaluation of the code block INFO. | |
283 | This behavior can be suppressed by setting the value of | |
284 | `org-confirm-babel-evaluate' to nil, in which case all future | |
285 | interactive code block evaluations will proceed without any | |
286 | confirmation from the user. | |
287 | ||
288 | Note disabling confirmation may result in accidental evaluation | |
289 | of potentially harmful code." | |
afe98dfa CD |
290 | (let* ((eval (or (cdr (assoc :eval (nth 2 info))) |
291 | (when (assoc :noeval (nth 2 info)) "no"))) | |
3ab2c837 | 292 | (query (cond ((equal eval "query") t) |
8223b1d2 BG |
293 | ((and (boundp 'org-current-export-file) |
294 | org-current-export-file | |
e66ba1df | 295 | (equal eval "query-export")) t) |
3ab2c837 BG |
296 | ((functionp org-confirm-babel-evaluate) |
297 | (funcall org-confirm-babel-evaluate | |
298 | (nth 0 info) (nth 1 info))) | |
299 | (t org-confirm-babel-evaluate)))) | |
afe98dfa | 300 | (if (or (equal eval "never") (equal eval "no") |
8223b1d2 BG |
301 | (and (boundp 'org-current-export-file) |
302 | org-current-export-file | |
303 | (or (equal eval "no-export") | |
304 | (equal eval "never-export"))) | |
afe98dfa CD |
305 | (and query |
306 | (not (yes-or-no-p | |
acedf35c CD |
307 | (format "Evaluate this%scode block%son your system? " |
308 | (if info (format " %s " (nth 0 info)) " ") | |
309 | (if (nth 4 info) | |
310 | (format " (%s) " (nth 4 info)) " ")))))) | |
311 | (prog1 nil (message "Evaluation %s" | |
e66ba1df BG |
312 | (if (or (equal eval "never") (equal eval "no") |
313 | (equal eval "no-export") | |
314 | (equal eval "never-export")) | |
acedf35c | 315 | "Disabled" "Aborted"))) |
afe98dfa | 316 | t))) |
86fbb8ca CD |
317 | |
318 | ;;;###autoload | |
afe98dfa CD |
319 | (defun org-babel-execute-safely-maybe () |
320 | (unless org-babel-no-eval-on-ctrl-c-ctrl-c | |
321 | (org-babel-execute-maybe))) | |
322 | ||
323 | (add-hook 'org-ctrl-c-ctrl-c-hook 'org-babel-execute-safely-maybe) | |
324 | ||
325 | ;;;###autoload | |
326 | (defun org-babel-execute-maybe () | |
327 | (interactive) | |
328 | (or (org-babel-execute-src-block-maybe) | |
329 | (org-babel-lob-execute-maybe))) | |
330 | ||
86fbb8ca CD |
331 | (defun org-babel-execute-src-block-maybe () |
332 | "Conditionally execute a source block. | |
333 | Detect if this is context for a Babel src-block and if so | |
334 | then run `org-babel-execute-src-block'." | |
335 | (interactive) | |
afe98dfa CD |
336 | (let ((info (org-babel-get-src-block-info))) |
337 | (if info | |
acedf35c CD |
338 | (progn (org-babel-eval-wipe-error-buffer) |
339 | (org-babel-execute-src-block current-prefix-arg info) t) nil))) | |
86fbb8ca | 340 | |
3ab2c837 BG |
341 | ;;;###autoload |
342 | (defun org-babel-view-src-block-info () | |
343 | "Display information on the current source block. | |
344 | This includes header arguments, language and name, and is largely | |
345 | a window into the `org-babel-get-src-block-info' function." | |
346 | (interactive) | |
8223b1d2 BG |
347 | (let ((info (org-babel-get-src-block-info 'light)) |
348 | (full (lambda (it) (> (length it) 0))) | |
349 | (printf (lambda (fmt &rest args) (princ (apply #'format fmt args))))) | |
350 | (when info | |
351 | (with-help-window (help-buffer) | |
352 | (let ((name (nth 4 info)) | |
353 | (lang (nth 0 info)) | |
354 | (switches (nth 3 info)) | |
355 | (header-args (nth 2 info))) | |
356 | (when name (funcall printf "Name: %s\n" name)) | |
357 | (when lang (funcall printf "Lang: %s\n" lang)) | |
358 | (when (funcall full switches) (funcall printf "Switches: %s\n" switches)) | |
359 | (funcall printf "Header Arguments:\n") | |
360 | (dolist (pair (sort header-args | |
361 | (lambda (a b) (string< (symbol-name (car a)) | |
362 | (symbol-name (car b)))))) | |
363 | (when (funcall full (cdr pair)) | |
364 | (funcall printf "\t%S%s\t%s\n" | |
365 | (car pair) | |
366 | (if (> (length (format "%S" (car pair))) 7) "" "\t") | |
367 | (cdr pair))))))))) | |
3ab2c837 | 368 | |
86fbb8ca CD |
369 | ;;;###autoload |
370 | (defun org-babel-expand-src-block-maybe () | |
371 | "Conditionally expand a source block. | |
372 | Detect if this is context for a org-babel src-block and if so | |
373 | then run `org-babel-expand-src-block'." | |
374 | (interactive) | |
375 | (let ((info (org-babel-get-src-block-info))) | |
376 | (if info | |
377 | (progn (org-babel-expand-src-block current-prefix-arg info) t) | |
378 | nil))) | |
379 | ||
380 | ;;;###autoload | |
381 | (defun org-babel-load-in-session-maybe () | |
382 | "Conditionally load a source block in a session. | |
383 | Detect if this is context for a org-babel src-block and if so | |
384 | then run `org-babel-load-in-session'." | |
385 | (interactive) | |
386 | (let ((info (org-babel-get-src-block-info))) | |
387 | (if info | |
388 | (progn (org-babel-load-in-session current-prefix-arg info) t) | |
389 | nil))) | |
390 | ||
391 | (add-hook 'org-metaup-hook 'org-babel-load-in-session-maybe) | |
392 | ||
393 | ;;;###autoload | |
394 | (defun org-babel-pop-to-session-maybe () | |
395 | "Conditionally pop to a session. | |
396 | Detect if this is context for a org-babel src-block and if so | |
397 | then run `org-babel-pop-to-session'." | |
398 | (interactive) | |
399 | (let ((info (org-babel-get-src-block-info))) | |
400 | (if info (progn (org-babel-pop-to-session current-prefix-arg info) t) nil))) | |
401 | ||
402 | (add-hook 'org-metadown-hook 'org-babel-pop-to-session-maybe) | |
403 | ||
e66ba1df BG |
404 | (defconst org-babel-common-header-args-w-values |
405 | '((cache . ((no yes))) | |
406 | (cmdline . :any) | |
407 | (colnames . ((nil no yes))) | |
408 | (comments . ((no link yes org both noweb))) | |
409 | (dir . :any) | |
410 | (eval . ((never query))) | |
411 | (exports . ((code results both none))) | |
412 | (file . :any) | |
8223b1d2 | 413 | (file-desc . :any) |
e66ba1df BG |
414 | (hlines . ((no yes))) |
415 | (mkdirp . ((yes no))) | |
416 | (no-expand) | |
417 | (noeval) | |
8223b1d2 | 418 | (noweb . ((yes no tangle no-export strip-export))) |
e66ba1df | 419 | (noweb-ref . :any) |
153ae947 | 420 | (noweb-sep . :any) |
e66ba1df BG |
421 | (padline . ((yes no))) |
422 | (results . ((file list vector table scalar verbatim) | |
8223b1d2 BG |
423 | (raw html latex org code pp drawer) |
424 | (replace silent append prepend) | |
425 | (output value))) | |
e66ba1df BG |
426 | (rownames . ((no yes))) |
427 | (sep . :any) | |
428 | (session . :any) | |
429 | (shebang . :any) | |
430 | (tangle . ((tangle yes no :any))) | |
8223b1d2 BG |
431 | (var . :any) |
432 | (wrap . :any))) | |
e66ba1df | 433 | |
86fbb8ca | 434 | (defconst org-babel-header-arg-names |
e66ba1df | 435 | (mapcar #'car org-babel-common-header-args-w-values) |
86fbb8ca CD |
436 | "Common header arguments used by org-babel. |
437 | Note that individual languages may define their own language | |
438 | specific header arguments as well.") | |
439 | ||
440 | (defvar org-babel-default-header-args | |
441 | '((:session . "none") (:results . "replace") (:exports . "code") | |
3ab2c837 BG |
442 | (:cache . "no") (:noweb . "no") (:hlines . "no") (:tangle . "no") |
443 | (:padnewline . "yes")) | |
86fbb8ca CD |
444 | "Default arguments to use when evaluating a source block.") |
445 | ||
446 | (defvar org-babel-default-inline-header-args | |
3ab2c837 | 447 | '((:session . "none") (:results . "replace") (:exports . "results")) |
86fbb8ca CD |
448 | "Default arguments to use when evaluating an inline source block.") |
449 | ||
8223b1d2 | 450 | (defvar org-babel-data-names '("tblname" "results" "name")) |
86fbb8ca CD |
451 | |
452 | (defvar org-babel-result-regexp | |
3ab2c837 BG |
453 | (concat "^[ \t]*#\\+" |
454 | (regexp-opt org-babel-data-names t) | |
455 | "\\(\\[\\([[:alnum:]]+\\)\\]\\)?\\:[ \t]*") | |
86fbb8ca CD |
456 | "Regular expression used to match result lines. |
457 | If the results are associated with a hash key then the hash will | |
458 | be saved in the second match data.") | |
459 | ||
460 | (defvar org-babel-result-w-name-regexp | |
461 | (concat org-babel-result-regexp | |
462 | "\\([^ ()\f\t\n\r\v]+\\)\\(\(\\(.*\\)\)\\|\\)")) | |
463 | ||
464 | (defvar org-babel-min-lines-for-block-output 10 | |
465 | "The minimum number of lines for block output. | |
466 | If number of lines of output is equal to or exceeds this | |
467 | value, the output is placed in a #+begin_example...#+end_example | |
8223b1d2 BG |
468 | block. Otherwise the output is marked as literal by inserting |
469 | colons at the starts of the lines. This variable only takes | |
86fbb8ca CD |
470 | effect if the :results output option is in effect.") |
471 | ||
472 | (defvar org-babel-noweb-error-langs nil | |
473 | "Languages for which Babel will raise literate programming errors. | |
474 | List of languages for which errors should be raised when the | |
475 | source code block satisfying a noweb reference in this language | |
476 | can not be resolved.") | |
477 | ||
478 | (defvar org-babel-hash-show 4 | |
479 | "Number of initial characters to show of a hidden results hash.") | |
480 | ||
481 | (defvar org-babel-after-execute-hook nil | |
482 | "Hook for functions to be called after `org-babel-execute-src-block'") | |
e66ba1df | 483 | |
86fbb8ca CD |
484 | (defun org-babel-named-src-block-regexp-for-name (name) |
485 | "This generates a regexp used to match a src block named NAME." | |
e66ba1df | 486 | (concat org-babel-src-name-regexp (regexp-quote name) |
8223b1d2 | 487 | "[ \t(]*[\r\n]\\(?:^#.*[\r\n]\\)*" |
86fbb8ca CD |
488 | (substring org-babel-src-block-regexp 1))) |
489 | ||
e66ba1df BG |
490 | (defun org-babel-named-data-regexp-for-name (name) |
491 | "This generates a regexp used to match data named NAME." | |
492 | (concat org-babel-result-regexp (regexp-quote name) "\\([ \t]\\|$\\)")) | |
493 | ||
86fbb8ca CD |
494 | ;;; functions |
495 | (defvar call-process-region) | |
afe98dfa | 496 | |
bdebdb64 | 497 | ;;;###autoload |
86fbb8ca CD |
498 | (defun org-babel-execute-src-block (&optional arg info params) |
499 | "Execute the current source code block. | |
500 | Insert the results of execution into the buffer. Source code | |
501 | execution and the collection and formatting of results can be | |
502 | controlled through a variety of header arguments. | |
503 | ||
e66ba1df BG |
504 | With prefix argument ARG, force re-execution even if an existing |
505 | result cached in the buffer would otherwise have been returned. | |
afe98dfa | 506 | |
86fbb8ca CD |
507 | Optionally supply a value for INFO in the form returned by |
508 | `org-babel-get-src-block-info'. | |
509 | ||
510 | Optionally supply a value for PARAMS which will be merged with | |
511 | the header arguments specified at the front of the source code | |
512 | block." | |
513 | (interactive) | |
afe98dfa | 514 | (let ((info (or info (org-babel-get-src-block-info)))) |
153ae947 BG |
515 | (when (org-babel-confirm-evaluate |
516 | (let ((i info)) | |
517 | (setf (nth 2 i) (org-babel-merge-params (nth 2 info) params)) | |
518 | i)) | |
afe98dfa CD |
519 | (let* ((lang (nth 0 info)) |
520 | (params (if params | |
521 | (org-babel-process-params | |
522 | (org-babel-merge-params (nth 2 info) params)) | |
523 | (nth 2 info))) | |
524 | (cache? (and (not arg) (cdr (assoc :cache params)) | |
525 | (string= "yes" (cdr (assoc :cache params))))) | |
526 | (result-params (cdr (assoc :result-params params))) | |
527 | (new-hash (when cache? (org-babel-sha1-hash info))) | |
3ab2c837 | 528 | (old-hash (when cache? (org-babel-current-result-hash))) |
afe98dfa | 529 | (body (setf (nth 1 info) |
8223b1d2 BG |
530 | (if (org-babel-noweb-p params :eval) |
531 | (org-babel-expand-noweb-references info) | |
532 | (nth 1 info)))) | |
afe98dfa CD |
533 | (dir (cdr (assoc :dir params))) |
534 | (default-directory | |
8223b1d2 BG |
535 | (or (and dir (file-name-as-directory (expand-file-name dir))) |
536 | default-directory)) | |
afe98dfa CD |
537 | (org-babel-call-process-region-original |
538 | (if (boundp 'org-babel-call-process-region-original) | |
539 | org-babel-call-process-region-original | |
540 | (symbol-function 'call-process-region))) | |
541 | (indent (car (last info))) | |
3ab2c837 | 542 | result cmd) |
afe98dfa | 543 | (unwind-protect |
8223b1d2 BG |
544 | (let ((call-process-region |
545 | (lambda (&rest args) | |
546 | (apply 'org-babel-tramp-handle-call-process-region args)))) | |
547 | (let ((lang-check (lambda (f) | |
548 | (let ((f (intern (concat "org-babel-execute:" f)))) | |
549 | (when (fboundp f) f))))) | |
3ab2c837 | 550 | (setq cmd |
8223b1d2 BG |
551 | (or (funcall lang-check lang) |
552 | (funcall lang-check (symbol-name | |
553 | (cdr (assoc lang org-src-lang-modes)))) | |
3ab2c837 | 554 | (error "No org-babel-execute function for %s!" lang)))) |
afe98dfa CD |
555 | (if (and (not arg) new-hash (equal new-hash old-hash)) |
556 | (save-excursion ;; return cached result | |
557 | (goto-char (org-babel-where-is-src-block-result nil info)) | |
558 | (end-of-line 1) (forward-char 1) | |
559 | (setq result (org-babel-read-result)) | |
560 | (message (replace-regexp-in-string | |
561 | "%" "%%" (format "%S" result))) result) | |
562 | (message "executing %s code block%s..." | |
563 | (capitalize lang) | |
564 | (if (nth 4 info) (format " (%s)" (nth 4 info)) "")) | |
565 | (setq result | |
566 | ((lambda (result) | |
3ab2c837 BG |
567 | (if (and (eq (cdr (assoc :result-type params)) 'value) |
568 | (or (member "vector" result-params) | |
569 | (member "table" result-params)) | |
570 | (not (listp result))) | |
571 | (list (list result)) result)) | |
afe98dfa | 572 | (funcall cmd body params))) |
3ab2c837 BG |
573 | ;; if non-empty result and :file then write to :file |
574 | (when (cdr (assoc :file params)) | |
575 | (when result | |
576 | (with-temp-file (cdr (assoc :file params)) | |
577 | (insert | |
578 | (org-babel-format-result | |
579 | result (cdr (assoc :sep (nth 2 info))))))) | |
580 | (setq result (cdr (assoc :file params)))) | |
afe98dfa CD |
581 | (org-babel-insert-result |
582 | result result-params info new-hash indent lang) | |
583 | (run-hooks 'org-babel-after-execute-hook) | |
584 | result)) | |
585 | (setq call-process-region 'org-babel-call-process-region-original)))))) | |
586 | ||
587 | (defun org-babel-expand-body:generic (body params &optional var-lines) | |
86fbb8ca | 588 | "Expand BODY with PARAMS. |
a98edce9 | 589 | Expand a block of code with org-babel according to its header |
86fbb8ca CD |
590 | arguments. This generic implementation of body expansion is |
591 | called for languages which have not defined their own specific | |
afe98dfa CD |
592 | org-babel-expand-body:lang function." |
593 | (mapconcat #'identity (append var-lines (list body)) "\n")) | |
86fbb8ca CD |
594 | |
595 | ;;;###autoload | |
596 | (defun org-babel-expand-src-block (&optional arg info params) | |
597 | "Expand the current source code block. | |
598 | Expand according to the source code block's header | |
599 | arguments and pop open the results in a preview buffer." | |
600 | (interactive) | |
601 | (let* ((info (or info (org-babel-get-src-block-info))) | |
602 | (lang (nth 0 info)) | |
603 | (params (setf (nth 2 info) | |
604 | (sort (org-babel-merge-params (nth 2 info) params) | |
605 | (lambda (el1 el2) (string< (symbol-name (car el1)) | |
8223b1d2 | 606 | (symbol-name (car el2))))))) |
86fbb8ca | 607 | (body (setf (nth 1 info) |
8223b1d2 | 608 | (if (org-babel-noweb-p params :eval) |
86fbb8ca | 609 | (org-babel-expand-noweb-references info) (nth 1 info)))) |
afe98dfa | 610 | (expand-cmd (intern (concat "org-babel-expand-body:" lang))) |
3ab2c837 BG |
611 | (assignments-cmd (intern (concat "org-babel-variable-assignments:" |
612 | lang))) | |
afe98dfa CD |
613 | (expanded |
614 | (if (fboundp expand-cmd) (funcall expand-cmd body params) | |
615 | (org-babel-expand-body:generic | |
3ab2c837 BG |
616 | body params (and (fboundp assignments-cmd) |
617 | (funcall assignments-cmd params)))))) | |
86fbb8ca CD |
618 | (org-edit-src-code |
619 | nil expanded (concat "*Org-Babel Preview " (buffer-name) "[ " lang " ]*")))) | |
620 | ||
3ab2c837 BG |
621 | (defun org-babel-edit-distance (s1 s2) |
622 | "Return the edit (levenshtein) distance between strings S1 S2." | |
623 | (let* ((l1 (length s1)) | |
624 | (l2 (length s2)) | |
8223b1d2 BG |
625 | (dist (vconcat (mapcar (lambda (_) (make-vector (1+ l2) nil)) |
626 | (number-sequence 1 (1+ l1))))) | |
627 | (in (lambda (i j) (aref (aref dist i) j))) | |
628 | (mmin (lambda (&rest lst) (apply #'min (remove nil lst))))) | |
629 | (setf (aref (aref dist 0) 0) 0) | |
630 | (dolist (i (number-sequence 1 l1)) | |
631 | (dolist (j (number-sequence 1 l2)) | |
632 | (setf (aref (aref dist i) j) | |
633 | (+ (if (equal (aref s1 (1- i)) (aref s2 (1- j))) 0 1) | |
634 | (funcall mmin (funcall in (1- i) j) | |
635 | (funcall in i (1- j)) | |
636 | (funcall in (1- i) (1- j))))))) | |
637 | (funcall in l1 l2))) | |
638 | ||
639 | (defun org-babel-combine-header-arg-lists (original &rest others) | |
640 | "Combine a number of lists of header argument names and arguments." | |
641 | (let ((results (copy-sequence original))) | |
642 | (dolist (new-list others) | |
643 | (dolist (arg-pair new-list) | |
644 | (let ((header (car arg-pair)) | |
645 | (args (cdr arg-pair))) | |
646 | (setq results | |
647 | (cons arg-pair (org-remove-if | |
648 | (lambda (pair) (equal header (car pair))) | |
649 | results)))))) | |
650 | results)) | |
3ab2c837 BG |
651 | |
652 | ;;;###autoload | |
653 | (defun org-babel-check-src-block () | |
654 | "Check for misspelled header arguments in the current code block." | |
655 | (interactive) | |
656 | ;; TODO: report malformed code block | |
657 | ;; TODO: report incompatible combinations of header arguments | |
e66ba1df | 658 | ;; TODO: report uninitialized variables |
153ae947 BG |
659 | (let ((too-close 2) ;; <- control closeness to report potential match |
660 | (names (mapcar #'symbol-name org-babel-header-arg-names))) | |
3ab2c837 BG |
661 | (dolist (header (mapcar (lambda (arg) (substring (symbol-name (car arg)) 1)) |
662 | (and (org-babel-where-is-src-block-head) | |
663 | (org-babel-parse-header-arguments | |
8223b1d2 | 664 | (org-no-properties |
3ab2c837 | 665 | (match-string 4)))))) |
153ae947 | 666 | (dolist (name names) |
3ab2c837 | 667 | (when (and (not (string= header name)) |
153ae947 BG |
668 | (<= (org-babel-edit-distance header name) too-close) |
669 | (not (member header names))) | |
8223b1d2 | 670 | (error "Supplied header \"%S\" is suspiciously close to \"%S\"" |
3ab2c837 BG |
671 | header name)))) |
672 | (message "No suspicious header arguments found."))) | |
673 | ||
e66ba1df BG |
674 | ;;;###autoload |
675 | (defun org-babel-insert-header-arg () | |
676 | "Insert a header argument selecting from lists of common args and values." | |
677 | (interactive) | |
678 | (let* ((lang (car (org-babel-get-src-block-info 'light))) | |
8223b1d2 BG |
679 | (lang-headers (intern (concat "org-babel-header-args:" lang))) |
680 | (headers (org-babel-combine-header-arg-lists | |
681 | org-babel-common-header-args-w-values | |
682 | (if (boundp lang-headers) (eval lang-headers) nil))) | |
e66ba1df | 683 | (arg (org-icompleting-read |
8223b1d2 BG |
684 | "Header Arg: " |
685 | (mapcar | |
686 | (lambda (header-spec) (symbol-name (car header-spec))) | |
687 | headers)))) | |
e66ba1df BG |
688 | (insert ":" arg) |
689 | (let ((vals (cdr (assoc (intern arg) headers)))) | |
690 | (when vals | |
691 | (insert | |
692 | " " | |
693 | (cond | |
694 | ((eq vals :any) | |
695 | (read-from-minibuffer "value: ")) | |
696 | ((listp vals) | |
697 | (mapconcat | |
698 | (lambda (group) | |
699 | (let ((arg (org-icompleting-read | |
700 | "value: " | |
701 | (cons "default" (mapcar #'symbol-name group))))) | |
702 | (if (and arg (not (string= "default" arg))) | |
703 | (concat arg " ") | |
704 | ""))) | |
705 | vals "")))))))) | |
706 | ||
8223b1d2 BG |
707 | ;; Add support for completing-read insertion of header arguments after ":" |
708 | (defun org-babel-header-arg-expand () | |
709 | "Call `org-babel-enter-header-arg-w-completion' in appropriate contexts." | |
710 | (when (and (equal (char-before) ?\:) (org-babel-where-is-src-block-head)) | |
711 | (org-babel-enter-header-arg-w-completion (match-string 2)))) | |
712 | ||
713 | (defun org-babel-enter-header-arg-w-completion (&optional lang) | |
714 | "Insert header argument appropriate for LANG with completion." | |
715 | (let* ((lang-headers-var (intern (concat "org-babel-header-args:" lang))) | |
716 | (lang-headers (when (boundp lang-headers-var) (eval lang-headers-var))) | |
717 | (headers-w-values (org-babel-combine-header-arg-lists | |
718 | org-babel-common-header-args-w-values lang-headers)) | |
719 | (headers (mapcar #'symbol-name (mapcar #'car headers-w-values))) | |
720 | (header (org-completing-read "Header Arg: " headers)) | |
721 | (args (cdr (assoc (intern header) headers-w-values))) | |
722 | (arg (when (and args (listp args)) | |
723 | (org-completing-read | |
724 | (format "%s: " header) | |
725 | (mapcar #'symbol-name (apply #'append args)))))) | |
726 | (insert (concat header " " (or arg ""))) | |
727 | (cons header arg))) | |
728 | ||
729 | (add-hook 'org-tab-first-hook 'org-babel-header-arg-expand) | |
730 | ||
86fbb8ca CD |
731 | ;;;###autoload |
732 | (defun org-babel-load-in-session (&optional arg info) | |
733 | "Load the body of the current source-code block. | |
734 | Evaluate the header arguments for the source block before | |
735 | entering the session. After loading the body this pops open the | |
736 | session." | |
737 | (interactive) | |
738 | (let* ((info (or info (org-babel-get-src-block-info))) | |
739 | (lang (nth 0 info)) | |
86fbb8ca | 740 | (params (nth 2 info)) |
afe98dfa | 741 | (body (setf (nth 1 info) |
8223b1d2 | 742 | (if (org-babel-noweb-p params :eval) |
afe98dfa CD |
743 | (org-babel-expand-noweb-references info) |
744 | (nth 1 info)))) | |
86fbb8ca | 745 | (session (cdr (assoc :session params))) |
afe98dfa CD |
746 | (dir (cdr (assoc :dir params))) |
747 | (default-directory | |
748 | (or (and dir (file-name-as-directory dir)) default-directory)) | |
86fbb8ca CD |
749 | (cmd (intern (concat "org-babel-load-session:" lang)))) |
750 | (unless (fboundp cmd) | |
751 | (error "No org-babel-load-session function for %s!" lang)) | |
752 | (pop-to-buffer (funcall cmd session body params)) | |
753 | (end-of-line 1))) | |
754 | ||
755 | ;;;###autoload | |
afe98dfa CD |
756 | (defun org-babel-initiate-session (&optional arg info) |
757 | "Initiate session for current code block. | |
758 | If called with a prefix argument then resolve any variable | |
759 | references in the header arguments and assign these variables in | |
8223b1d2 | 760 | the session. Copy the body of the code block to the kill ring." |
afe98dfa CD |
761 | (interactive "P") |
762 | (let* ((info (or info (org-babel-get-src-block-info (not arg)))) | |
86fbb8ca CD |
763 | (lang (nth 0 info)) |
764 | (body (nth 1 info)) | |
765 | (params (nth 2 info)) | |
766 | (session (cdr (assoc :session params))) | |
767 | (dir (cdr (assoc :dir params))) | |
768 | (default-directory | |
769 | (or (and dir (file-name-as-directory dir)) default-directory)) | |
afe98dfa CD |
770 | (init-cmd (intern (format "org-babel-%s-initiate-session" lang))) |
771 | (prep-cmd (intern (concat "org-babel-prep-session:" lang)))) | |
772 | (if (and (stringp session) (string= session "none")) | |
773 | (error "This block is not using a session!")) | |
774 | (unless (fboundp init-cmd) | |
86fbb8ca | 775 | (error "No org-babel-initiate-session function for %s!" lang)) |
86fbb8ca CD |
776 | (with-temp-buffer (insert (org-babel-trim body)) |
777 | (copy-region-as-kill (point-min) (point-max))) | |
afe98dfa CD |
778 | (when arg |
779 | (unless (fboundp prep-cmd) | |
780 | (error "No org-babel-prep-session function for %s!" lang)) | |
781 | (funcall prep-cmd session params)) | |
782 | (funcall init-cmd session params))) | |
783 | ||
784 | ;;;###autoload | |
785 | (defun org-babel-switch-to-session (&optional arg info) | |
786 | "Switch to the session of the current code block. | |
8223b1d2 | 787 | Uses `org-babel-initiate-session' to start the session. If called |
afe98dfa CD |
788 | with a prefix argument then this is passed on to |
789 | `org-babel-initiate-session'." | |
790 | (interactive "P") | |
791 | (pop-to-buffer (org-babel-initiate-session arg info)) | |
792 | (end-of-line 1)) | |
86fbb8ca CD |
793 | |
794 | (defalias 'org-babel-pop-to-session 'org-babel-switch-to-session) | |
795 | ||
afe98dfa CD |
796 | ;;;###autoload |
797 | (defun org-babel-switch-to-session-with-code (&optional arg info) | |
798 | "Switch to code buffer and display session." | |
799 | (interactive "P") | |
8223b1d2 BG |
800 | (let ((swap-windows |
801 | (lambda () | |
802 | (let ((other-window-buffer (window-buffer (next-window)))) | |
803 | (set-window-buffer (next-window) (current-buffer)) | |
804 | (set-window-buffer (selected-window) other-window-buffer)) | |
805 | (other-window 1))) | |
806 | (info (org-babel-get-src-block-info)) | |
807 | (org-src-window-setup 'reorganize-frame)) | |
808 | (save-excursion | |
809 | (org-babel-switch-to-session arg info)) | |
810 | (org-edit-src-code) | |
811 | (funcall swap-windows))) | |
afe98dfa CD |
812 | |
813 | (defmacro org-babel-do-in-edit-buffer (&rest body) | |
814 | "Evaluate BODY in edit buffer if there is a code block at point. | |
815 | Return t if a code block was found at point, nil otherwise." | |
816 | `(let ((org-src-window-setup 'switch-invisibly)) | |
817 | (when (and (org-babel-where-is-src-block-head) | |
3ab2c837 | 818 | (org-edit-src-code nil nil nil)) |
afe98dfa CD |
819 | (unwind-protect (progn ,@body) |
820 | (if (org-bound-and-true-p org-edit-src-from-org-mode) | |
821 | (org-edit-src-exit))) | |
822 | t))) | |
e66ba1df | 823 | (def-edebug-spec org-babel-do-in-edit-buffer (body)) |
afe98dfa CD |
824 | |
825 | (defun org-babel-do-key-sequence-in-edit-buffer (key) | |
826 | "Read key sequence and execute the command in edit buffer. | |
827 | Enter a key sequence to be executed in the language major-mode | |
8223b1d2 | 828 | edit buffer. For example, TAB will alter the contents of the |
afe98dfa | 829 | Org-mode code block according to the effect of TAB in the |
8223b1d2 | 830 | language major-mode buffer. For languages that support |
afe98dfa CD |
831 | interactive sessions, this can be used to send code from the Org |
832 | buffer to the session for evaluation using the native major-mode | |
833 | evaluation mechanisms." | |
834 | (interactive "kEnter key-sequence to execute in edit buffer: ") | |
835 | (org-babel-do-in-edit-buffer | |
836 | (call-interactively | |
837 | (key-binding (or key (read-key-sequence nil)))))) | |
838 | ||
86fbb8ca | 839 | (defvar org-bracket-link-regexp) |
bdebdb64 | 840 | |
86fbb8ca CD |
841 | ;;;###autoload |
842 | (defun org-babel-open-src-block-result (&optional re-run) | |
843 | "If `point' is on a src block then open the results of the | |
844 | source code block, otherwise return nil. With optional prefix | |
845 | argument RE-RUN the source-code block is evaluated even if | |
846 | results already exist." | |
847 | (interactive "P") | |
3ab2c837 BG |
848 | (let ((info (org-babel-get-src-block-info))) |
849 | (when info | |
850 | (save-excursion | |
851 | ;; go to the results, if there aren't any then run the block | |
852 | (goto-char (or (and (not re-run) (org-babel-where-is-src-block-result)) | |
853 | (progn (org-babel-execute-src-block) | |
854 | (org-babel-where-is-src-block-result)))) | |
855 | (end-of-line 1) | |
856 | (while (looking-at "[\n\r\t\f ]") (forward-char 1)) | |
857 | ;; open the results | |
858 | (if (looking-at org-bracket-link-regexp) | |
859 | ;; file results | |
860 | (org-open-at-point) | |
861 | (let ((r (org-babel-format-result | |
862 | (org-babel-read-result) (cdr (assoc :sep (nth 2 info)))))) | |
863 | (pop-to-buffer (get-buffer-create "*Org-Babel Results*")) | |
864 | (delete-region (point-min) (point-max)) | |
865 | (insert r))) | |
866 | t)))) | |
86fbb8ca | 867 | |
acedf35c CD |
868 | ;;;###autoload |
869 | (defmacro org-babel-map-src-blocks (file &rest body) | |
870 | "Evaluate BODY forms on each source-block in FILE. | |
871 | If FILE is nil evaluate BODY forms on source blocks in current | |
872 | buffer. During evaluation of BODY the following local variables | |
873 | are set relative to the currently matched code block. | |
874 | ||
875 | full-block ------- string holding the entirety of the code block | |
876 | beg-block -------- point at the beginning of the code block | |
877 | end-block -------- point at the end of the matched code block | |
878 | lang ------------- string holding the language of the code block | |
879 | beg-lang --------- point at the beginning of the lang | |
880 | end-lang --------- point at the end of the lang | |
881 | switches --------- string holding the switches | |
882 | beg-switches ----- point at the beginning of the switches | |
883 | end-switches ----- point at the end of the switches | |
884 | header-args ------ string holding the header-args | |
885 | beg-header-args -- point at the beginning of the header-args | |
886 | end-header-args -- point at the end of the header-args | |
887 | body ------------- string holding the body of the code block | |
888 | beg-body --------- point at the beginning of the body | |
889 | end-body --------- point at the end of the body" | |
890 | (declare (indent 1)) | |
891 | (let ((tempvar (make-symbol "file"))) | |
892 | `(let* ((,tempvar ,file) | |
893 | (visited-p (or (null ,tempvar) | |
894 | (get-file-buffer (expand-file-name ,tempvar)))) | |
895 | (point (point)) to-be-removed) | |
896 | (save-window-excursion | |
897 | (when ,tempvar (find-file ,tempvar)) | |
898 | (setq to-be-removed (current-buffer)) | |
899 | (goto-char (point-min)) | |
900 | (while (re-search-forward org-babel-src-block-regexp nil t) | |
901 | (goto-char (match-beginning 0)) | |
902 | (let ((full-block (match-string 0)) | |
903 | (beg-block (match-beginning 0)) | |
904 | (end-block (match-end 0)) | |
905 | (lang (match-string 2)) | |
906 | (beg-lang (match-beginning 2)) | |
907 | (end-lang (match-end 2)) | |
908 | (switches (match-string 3)) | |
909 | (beg-switches (match-beginning 3)) | |
910 | (end-switches (match-end 3)) | |
911 | (header-args (match-string 4)) | |
912 | (beg-header-args (match-beginning 4)) | |
913 | (end-header-args (match-end 4)) | |
914 | (body (match-string 5)) | |
915 | (beg-body (match-beginning 5)) | |
916 | (end-body (match-end 5))) | |
917 | ,@body | |
918 | (goto-char end-block)))) | |
919 | (unless visited-p (kill-buffer to-be-removed)) | |
920 | (goto-char point)))) | |
e66ba1df | 921 | (def-edebug-spec org-babel-map-src-blocks (form body)) |
acedf35c | 922 | |
3ab2c837 BG |
923 | ;;;###autoload |
924 | (defmacro org-babel-map-inline-src-blocks (file &rest body) | |
925 | "Evaluate BODY forms on each inline source-block in FILE. | |
926 | If FILE is nil evaluate BODY forms on source blocks in current | |
927 | buffer." | |
928 | (declare (indent 1)) | |
929 | (let ((tempvar (make-symbol "file"))) | |
930 | `(let* ((,tempvar ,file) | |
931 | (visited-p (or (null ,tempvar) | |
932 | (get-file-buffer (expand-file-name ,tempvar)))) | |
933 | (point (point)) to-be-removed) | |
934 | (save-window-excursion | |
935 | (when ,tempvar (find-file ,tempvar)) | |
936 | (setq to-be-removed (current-buffer)) | |
937 | (goto-char (point-min)) | |
938 | (while (re-search-forward org-babel-inline-src-block-regexp nil t) | |
939 | (goto-char (match-beginning 1)) | |
940 | (save-match-data ,@body) | |
941 | (goto-char (match-end 0)))) | |
942 | (unless visited-p (kill-buffer to-be-removed)) | |
943 | (goto-char point)))) | |
e66ba1df BG |
944 | (def-edebug-spec org-babel-map-inline-src-blocks (form body)) |
945 | ||
946 | (defvar org-babel-lob-one-liner-regexp) | |
bdebdb64 | 947 | |
e66ba1df BG |
948 | ;;;###autoload |
949 | (defmacro org-babel-map-call-lines (file &rest body) | |
950 | "Evaluate BODY forms on each call line in FILE. | |
951 | If FILE is nil evaluate BODY forms on source blocks in current | |
952 | buffer." | |
953 | (declare (indent 1)) | |
954 | (let ((tempvar (make-symbol "file"))) | |
955 | `(let* ((,tempvar ,file) | |
956 | (visited-p (or (null ,tempvar) | |
957 | (get-file-buffer (expand-file-name ,tempvar)))) | |
958 | (point (point)) to-be-removed) | |
959 | (save-window-excursion | |
960 | (when ,tempvar (find-file ,tempvar)) | |
961 | (setq to-be-removed (current-buffer)) | |
962 | (goto-char (point-min)) | |
963 | (while (re-search-forward org-babel-lob-one-liner-regexp nil t) | |
964 | (goto-char (match-beginning 1)) | |
965 | (save-match-data ,@body) | |
966 | (goto-char (match-end 0)))) | |
967 | (unless visited-p (kill-buffer to-be-removed)) | |
968 | (goto-char point)))) | |
969 | (def-edebug-spec org-babel-map-call-lines (form body)) | |
3ab2c837 | 970 | |
153ae947 BG |
971 | ;;;###autoload |
972 | (defmacro org-babel-map-executables (file &rest body) | |
973 | (declare (indent 1)) | |
974 | (let ((tempvar (make-symbol "file")) | |
975 | (rx (make-symbol "rx"))) | |
976 | `(let* ((,tempvar ,file) | |
977 | (,rx (concat "\\(" org-babel-src-block-regexp | |
978 | "\\|" org-babel-inline-src-block-regexp | |
979 | "\\|" org-babel-lob-one-liner-regexp "\\)")) | |
980 | (visited-p (or (null ,tempvar) | |
981 | (get-file-buffer (expand-file-name ,tempvar)))) | |
982 | (point (point)) to-be-removed) | |
983 | (save-window-excursion | |
984 | (when ,tempvar (find-file ,tempvar)) | |
985 | (setq to-be-removed (current-buffer)) | |
986 | (goto-char (point-min)) | |
987 | (while (re-search-forward ,rx nil t) | |
988 | (goto-char (match-beginning 1)) | |
989 | (when (looking-at org-babel-inline-src-block-regexp)(forward-char 1)) | |
990 | (save-match-data ,@body) | |
991 | (goto-char (match-end 0)))) | |
992 | (unless visited-p (kill-buffer to-be-removed)) | |
993 | (goto-char point)))) | |
994 | (def-edebug-spec org-babel-map-executables (form body)) | |
995 | ||
86fbb8ca CD |
996 | ;;;###autoload |
997 | (defun org-babel-execute-buffer (&optional arg) | |
998 | "Execute source code blocks in a buffer. | |
999 | Call `org-babel-execute-src-block' on every source block in | |
1000 | the current buffer." | |
1001 | (interactive "P") | |
3ab2c837 | 1002 | (org-babel-eval-wipe-error-buffer) |
afe98dfa | 1003 | (org-save-outline-visibility t |
153ae947 BG |
1004 | (org-babel-map-executables nil |
1005 | (if (looking-at org-babel-lob-one-liner-regexp) | |
1006 | (org-babel-lob-execute-maybe) | |
1007 | (org-babel-execute-src-block arg))))) | |
86fbb8ca CD |
1008 | |
1009 | ;;;###autoload | |
1010 | (defun org-babel-execute-subtree (&optional arg) | |
1011 | "Execute source code blocks in a subtree. | |
1012 | Call `org-babel-execute-src-block' on every source block in | |
1013 | the current subtree." | |
1014 | (interactive "P") | |
1015 | (save-restriction | |
1016 | (save-excursion | |
1017 | (org-narrow-to-subtree) | |
afe98dfa | 1018 | (org-babel-execute-buffer arg) |
86fbb8ca CD |
1019 | (widen)))) |
1020 | ||
1021 | ;;;###autoload | |
1022 | (defun org-babel-sha1-hash (&optional info) | |
1023 | "Generate an sha1 hash based on the value of info." | |
1024 | (interactive) | |
afe98dfa CD |
1025 | (let ((print-level nil) |
1026 | (info (or info (org-babel-get-src-block-info)))) | |
1027 | (setf (nth 2 info) | |
1028 | (sort (copy-sequence (nth 2 info)) | |
1029 | (lambda (a b) (string< (car a) (car b))))) | |
8223b1d2 | 1030 | (let* ((rm (lambda (lst) |
3ab2c837 BG |
1031 | (dolist (p '("replace" "silent" "append" "prepend")) |
1032 | (setq lst (remove p lst))) | |
8223b1d2 BG |
1033 | lst)) |
1034 | (norm (lambda (arg) | |
3ab2c837 | 1035 | (let ((v (if (and (listp (cdr arg)) (null (cddr arg))) |
e66ba1df | 1036 | (copy-sequence (cdr arg)) |
3ab2c837 BG |
1037 | (cdr arg)))) |
1038 | (when (and v (not (and (sequencep v) | |
1039 | (not (consp v)) | |
1040 | (= (length v) 0)))) | |
1041 | (cond | |
1042 | ((and (listp v) ; lists are sorted | |
1043 | (member (car arg) '(:result-params))) | |
8223b1d2 | 1044 | (sort (funcall rm v) #'string<)) |
3ab2c837 BG |
1045 | ((and (stringp v) ; strings are sorted |
1046 | (member (car arg) '(:results :exports))) | |
8223b1d2 | 1047 | (mapconcat #'identity (sort (funcall rm (split-string v)) |
3ab2c837 | 1048 | #'string<) " ")) |
8223b1d2 | 1049 | (t v))))))) |
3ab2c837 BG |
1050 | ((lambda (hash) |
1051 | (when (org-called-interactively-p 'interactive) (message hash)) hash) | |
1052 | (let ((it (format "%s-%s" | |
afe98dfa CD |
1053 | (mapconcat |
1054 | #'identity | |
3ab2c837 | 1055 | (delq nil (mapcar (lambda (arg) |
8223b1d2 | 1056 | (let ((normalized (funcall norm arg))) |
3ab2c837 BG |
1057 | (when normalized |
1058 | (format "%S" normalized)))) | |
1059 | (nth 2 info))) ":") | |
1060 | (nth 1 info)))) | |
1061 | (sha1 it)))))) | |
1062 | ||
1063 | (defun org-babel-current-result-hash () | |
8223b1d2 BG |
1064 | "Return the current in-buffer hash." |
1065 | (org-babel-where-is-src-block-result) | |
1066 | (org-no-properties (match-string 3))) | |
1067 | ||
1068 | (defun org-babel-set-current-result-hash (hash) | |
1069 | "Set the current in-buffer hash to HASH." | |
3ab2c837 | 1070 | (org-babel-where-is-src-block-result) |
8223b1d2 BG |
1071 | (save-excursion (goto-char (match-beginning 3)) |
1072 | ;; (mapc #'delete-overlay (overlays-at (point))) | |
1073 | (replace-match hash nil nil nil 3) | |
1074 | (org-babel-hide-hash))) | |
86fbb8ca CD |
1075 | |
1076 | (defun org-babel-hide-hash () | |
1077 | "Hide the hash in the current results line. | |
1078 | Only the initial `org-babel-hash-show' characters of the hash | |
1079 | will remain visible." | |
1080 | (add-to-invisibility-spec '(org-babel-hide-hash . t)) | |
1081 | (save-excursion | |
1082 | (when (and (re-search-forward org-babel-result-regexp nil t) | |
1083 | (match-string 3)) | |
1084 | (let* ((start (match-beginning 3)) | |
1085 | (hide-start (+ org-babel-hash-show start)) | |
1086 | (end (match-end 3)) | |
1087 | (hash (match-string 3)) | |
1088 | ov1 ov2) | |
1089 | (setq ov1 (make-overlay start hide-start)) | |
1090 | (setq ov2 (make-overlay hide-start end)) | |
1091 | (overlay-put ov2 'invisible 'org-babel-hide-hash) | |
1092 | (overlay-put ov1 'babel-hash hash))))) | |
1093 | ||
1094 | (defun org-babel-hide-all-hashes () | |
1095 | "Hide the hash in the current buffer. | |
1096 | Only the initial `org-babel-hash-show' characters of each hash | |
1097 | will remain visible. This function should be called as part of | |
1098 | the `org-mode-hook'." | |
1099 | (save-excursion | |
1100 | (while (re-search-forward org-babel-result-regexp nil t) | |
1101 | (goto-char (match-beginning 0)) | |
1102 | (org-babel-hide-hash) | |
1103 | (goto-char (match-end 0))))) | |
1104 | (add-hook 'org-mode-hook 'org-babel-hide-all-hashes) | |
1105 | ||
1106 | (defun org-babel-hash-at-point (&optional point) | |
1107 | "Return the value of the hash at POINT. | |
1108 | The hash is also added as the last element of the kill ring. | |
1109 | This can be called with C-c C-c." | |
1110 | (interactive) | |
1111 | (let ((hash (car (delq nil (mapcar | |
1112 | (lambda (ol) (overlay-get ol 'babel-hash)) | |
1113 | (overlays-at (or point (point)))))))) | |
1114 | (when hash (kill-new hash) (message hash)))) | |
1115 | (add-hook 'org-ctrl-c-ctrl-c-hook 'org-babel-hash-at-point) | |
1116 | ||
153ae947 BG |
1117 | (defun org-babel-result-hide-spec () |
1118 | "Hide portions of results lines. | |
1119 | Add `org-babel-hide-result' as an invisibility spec for hiding | |
1120 | portions of results lines." | |
1121 | (add-to-invisibility-spec '(org-babel-hide-result . t))) | |
1122 | (add-hook 'org-mode-hook 'org-babel-result-hide-spec) | |
1123 | ||
1124 | (defvar org-babel-hide-result-overlays nil | |
1125 | "Overlays hiding results.") | |
1126 | ||
1127 | (defun org-babel-result-hide-all () | |
1128 | "Fold all results in the current buffer." | |
1129 | (interactive) | |
1130 | (org-babel-show-result-all) | |
1131 | (save-excursion | |
1132 | (while (re-search-forward org-babel-result-regexp nil t) | |
1133 | (save-excursion (goto-char (match-beginning 0)) | |
1134 | (org-babel-hide-result-toggle-maybe))))) | |
1135 | ||
1136 | (defun org-babel-show-result-all () | |
1137 | "Unfold all results in the current buffer." | |
1138 | (mapc 'delete-overlay org-babel-hide-result-overlays) | |
1139 | (setq org-babel-hide-result-overlays nil)) | |
1140 | ||
1141 | ;;;###autoload | |
1142 | (defun org-babel-hide-result-toggle-maybe () | |
1143 | "Toggle visibility of result at point." | |
1144 | (interactive) | |
1145 | (let ((case-fold-search t)) | |
1146 | (if (save-excursion | |
1147 | (beginning-of-line 1) | |
1148 | (looking-at org-babel-result-regexp)) | |
1149 | (progn (org-babel-hide-result-toggle) | |
1150 | t) ;; to signal that we took action | |
1151 | nil))) ;; to signal that we did not | |
1152 | ||
1153 | (defun org-babel-hide-result-toggle (&optional force) | |
1154 | "Toggle the visibility of the current result." | |
1155 | (interactive) | |
1156 | (save-excursion | |
1157 | (beginning-of-line) | |
1158 | (if (re-search-forward org-babel-result-regexp nil t) | |
1159 | (let ((start (progn (beginning-of-line 2) (- (point) 1))) | |
1160 | (end (progn | |
1161 | (while (looking-at org-babel-multi-line-header-regexp) | |
1162 | (forward-line 1)) | |
1163 | (goto-char (- (org-babel-result-end) 1)) (point))) | |
1164 | ov) | |
1165 | (if (memq t (mapcar (lambda (overlay) | |
1166 | (eq (overlay-get overlay 'invisible) | |
1167 | 'org-babel-hide-result)) | |
1168 | (overlays-at start))) | |
1169 | (if (or (not force) (eq force 'off)) | |
1170 | (mapc (lambda (ov) | |
1171 | (when (member ov org-babel-hide-result-overlays) | |
1172 | (setq org-babel-hide-result-overlays | |
1173 | (delq ov org-babel-hide-result-overlays))) | |
1174 | (when (eq (overlay-get ov 'invisible) | |
1175 | 'org-babel-hide-result) | |
1176 | (delete-overlay ov))) | |
1177 | (overlays-at start))) | |
1178 | (setq ov (make-overlay start end)) | |
1179 | (overlay-put ov 'invisible 'org-babel-hide-result) | |
1180 | ;; make the block accessible to isearch | |
1181 | (overlay-put | |
1182 | ov 'isearch-open-invisible | |
1183 | (lambda (ov) | |
1184 | (when (member ov org-babel-hide-result-overlays) | |
1185 | (setq org-babel-hide-result-overlays | |
1186 | (delq ov org-babel-hide-result-overlays))) | |
1187 | (when (eq (overlay-get ov 'invisible) | |
1188 | 'org-babel-hide-result) | |
1189 | (delete-overlay ov)))) | |
1190 | (push ov org-babel-hide-result-overlays))) | |
1191 | (error "Not looking at a result line")))) | |
1192 | ||
1193 | ;; org-tab-after-check-for-cycling-hook | |
1194 | (add-hook 'org-tab-first-hook 'org-babel-hide-result-toggle-maybe) | |
1195 | ;; Remove overlays when changing major mode | |
1196 | (add-hook 'org-mode-hook | |
1197 | (lambda () (org-add-hook 'change-major-mode-hook | |
1198 | 'org-babel-show-result-all 'append 'local))) | |
1199 | ||
86fbb8ca CD |
1200 | (defvar org-file-properties) |
1201 | (defun org-babel-params-from-properties (&optional lang) | |
1202 | "Retrieve parameters specified as properties. | |
1203 | Return an association list of any source block params which | |
1204 | may be specified in the properties of the current outline entry." | |
1205 | (save-match-data | |
1206 | (let (val sym) | |
e66ba1df BG |
1207 | (org-babel-parse-multiple-vars |
1208 | (delq nil | |
86fbb8ca | 1209 | (mapcar |
e66ba1df BG |
1210 | (lambda (header-arg) |
1211 | (and (setq val (org-entry-get (point) header-arg t)) | |
1212 | (cons (intern (concat ":" header-arg)) | |
1213 | (org-babel-read val)))) | |
1214 | (mapcar | |
8223b1d2 BG |
1215 | #'symbol-name |
1216 | (mapcar | |
1217 | #'car | |
1218 | (org-babel-combine-header-arg-lists | |
1219 | org-babel-common-header-args-w-values | |
1220 | (progn | |
1221 | (setq sym (intern (concat "org-babel-header-args:" lang))) | |
1222 | (and (boundp sym) (eval sym)))))))))))) | |
86fbb8ca CD |
1223 | |
1224 | (defvar org-src-preserve-indentation) | |
1225 | (defun org-babel-parse-src-block-match () | |
1226 | "Parse the results from a match of the `org-babel-src-block-regexp'." | |
1227 | (let* ((block-indentation (length (match-string 1))) | |
8223b1d2 | 1228 | (lang (org-no-properties (match-string 2))) |
86fbb8ca CD |
1229 | (lang-headers (intern (concat "org-babel-default-header-args:" lang))) |
1230 | (switches (match-string 3)) | |
8223b1d2 | 1231 | (body (org-no-properties |
3ab2c837 BG |
1232 | (let* ((body (match-string 5)) |
1233 | (sub-length (- (length body) 1))) | |
e66ba1df BG |
1234 | (if (and (> sub-length 0) |
1235 | (string= "\n" (substring body sub-length))) | |
3ab2c837 | 1236 | (substring body 0 sub-length) |
e66ba1df | 1237 | (or body ""))))) |
86fbb8ca | 1238 | (preserve-indentation (or org-src-preserve-indentation |
eb2adf0a BG |
1239 | (save-match-data |
1240 | (string-match "-i\\>" switches))))) | |
86fbb8ca CD |
1241 | (list lang |
1242 | ;; get block body less properties, protective commas, and indentation | |
1243 | (with-temp-buffer | |
1244 | (save-match-data | |
bdebdb64 | 1245 | (insert (org-unescape-code-in-string body)) |
86fbb8ca CD |
1246 | (unless preserve-indentation (org-do-remove-indentation)) |
1247 | (buffer-string))) | |
1248 | (org-babel-merge-params | |
1249 | org-babel-default-header-args | |
86fbb8ca CD |
1250 | (org-babel-params-from-properties lang) |
1251 | (if (boundp lang-headers) (eval lang-headers) nil) | |
1252 | (org-babel-parse-header-arguments | |
8223b1d2 | 1253 | (org-no-properties (or (match-string 4) "")))) |
86fbb8ca CD |
1254 | switches |
1255 | block-indentation))) | |
1256 | ||
1257 | (defun org-babel-parse-inline-src-block-match () | |
1258 | "Parse the results from a match of the `org-babel-inline-src-block-regexp'." | |
8223b1d2 | 1259 | (let* ((lang (org-no-properties (match-string 2))) |
86fbb8ca CD |
1260 | (lang-headers (intern (concat "org-babel-default-header-args:" lang)))) |
1261 | (list lang | |
bdebdb64 | 1262 | (org-unescape-code-in-string (org-no-properties (match-string 5))) |
86fbb8ca CD |
1263 | (org-babel-merge-params |
1264 | org-babel-default-inline-header-args | |
86fbb8ca CD |
1265 | (org-babel-params-from-properties lang) |
1266 | (if (boundp lang-headers) (eval lang-headers) nil) | |
1267 | (org-babel-parse-header-arguments | |
8223b1d2 | 1268 | (org-no-properties (or (match-string 4) ""))))))) |
86fbb8ca | 1269 | |
e66ba1df BG |
1270 | (defun org-babel-balanced-split (string alts) |
1271 | "Split STRING on instances of ALTS. | |
1272 | ALTS is a cons of two character options where each option may be | |
1273 | either the numeric code of a single character or a list of | |
1274 | character alternatives. For example to split on balanced | |
1275 | instances of \"[ \t]:\" set ALTS to '((32 9) . 58)." | |
8223b1d2 BG |
1276 | (let* ((matches (lambda (ch spec) (if (listp spec) (member ch spec) (equal spec ch)))) |
1277 | (matched (lambda (ch last) | |
1278 | (if (consp alts) | |
1279 | (and (funcall matches ch (cdr alts)) | |
1280 | (funcall matches last (car alts))) | |
1281 | (funcall matches ch alts)))) | |
1282 | (balance 0) (last 0) | |
1283 | quote partial lst) | |
1284 | (mapc (lambda (ch) ; split on [], (), "" balanced instances of [ \t]: | |
1285 | (setq balance (+ balance | |
1286 | (cond ((or (equal 91 ch) (equal 40 ch)) 1) | |
1287 | ((or (equal 93 ch) (equal 41 ch)) -1) | |
1288 | (t 0)))) | |
1289 | (when (and (equal 34 ch) (not (equal 92 last))) | |
1290 | (setq quote (not quote))) | |
1291 | (setq partial (cons ch partial)) | |
1292 | (when (and (= balance 0) (not quote) (funcall matched ch last)) | |
1293 | (setq lst (cons (apply #'string (nreverse | |
1294 | (if (consp alts) | |
1295 | (cddr partial) | |
1296 | (cdr partial)))) | |
1297 | lst)) | |
1298 | (setq partial nil)) | |
1299 | (setq last ch)) | |
1300 | (string-to-list string)) | |
1301 | (nreverse (cons (apply #'string (nreverse partial)) lst)))) | |
e66ba1df BG |
1302 | |
1303 | (defun org-babel-join-splits-near-ch (ch list) | |
1304 | "Join splits where \"=\" is on either end of the split." | |
8223b1d2 BG |
1305 | (let ((last= (lambda (str) (= ch (aref str (1- (length str)))))) |
1306 | (first= (lambda (str) (= ch (aref str 0))))) | |
e66ba1df BG |
1307 | (reverse |
1308 | (org-reduce (lambda (acc el) | |
8223b1d2 BG |
1309 | (let ((head (car acc))) |
1310 | (if (and head (or (funcall last= head) (funcall first= el))) | |
1311 | (cons (concat head el) (cdr acc)) | |
1312 | (cons el acc)))) | |
1313 | list :initial-value nil)))) | |
e66ba1df | 1314 | |
86fbb8ca CD |
1315 | (defun org-babel-parse-header-arguments (arg-string) |
1316 | "Parse a string of header arguments returning an alist." | |
acedf35c | 1317 | (when (> (length arg-string) 0) |
e66ba1df BG |
1318 | (org-babel-parse-multiple-vars |
1319 | (delq nil | |
1320 | (mapcar | |
1321 | (lambda (arg) | |
1322 | (if (string-match | |
1323 | "\\([^ \f\t\n\r\v]+\\)[ \f\t\n\r\v]+\\([^ \f\t\n\r\v]+.*\\)" | |
1324 | arg) | |
1325 | (cons (intern (match-string 1 arg)) | |
1326 | (org-babel-read (org-babel-chomp (match-string 2 arg)))) | |
1327 | (cons (intern (org-babel-chomp arg)) nil))) | |
1328 | ((lambda (raw) | |
1329 | (cons (car raw) (mapcar (lambda (r) (concat ":" r)) (cdr raw)))) | |
1330 | (org-babel-balanced-split arg-string '((32 9) . 58)))))))) | |
1331 | ||
1332 | (defun org-babel-parse-multiple-vars (header-arguments) | |
1333 | "Expand multiple variable assignments behind a single :var keyword. | |
1334 | ||
1335 | This allows expression of multiple variables with one :var as | |
1336 | shown below. | |
1337 | ||
1338 | #+PROPERTY: var foo=1, bar=2" | |
1339 | (let (results) | |
1340 | (mapc (lambda (pair) | |
1341 | (if (eq (car pair) :var) | |
1342 | (mapcar (lambda (v) (push (cons :var (org-babel-trim v)) results)) | |
1343 | (org-babel-join-splits-near-ch | |
1344 | 61 (org-babel-balanced-split (cdr pair) 32))) | |
1345 | (push pair results))) | |
1346 | header-arguments) | |
1347 | (nreverse results))) | |
86fbb8ca CD |
1348 | |
1349 | (defun org-babel-process-params (params) | |
afe98dfa | 1350 | "Expand variables in PARAMS and add summary parameters." |
e66ba1df BG |
1351 | (let* ((processed-vars (mapcar (lambda (el) |
1352 | (if (consp (cdr el)) | |
1353 | (cdr el) | |
1354 | (org-babel-ref-parse (cdr el)))) | |
1355 | (org-babel-get-header params :var))) | |
1356 | (vars-and-names (if (and (assoc :colname-names params) | |
1357 | (assoc :rowname-names params)) | |
1358 | (list processed-vars) | |
1359 | (org-babel-disassemble-tables | |
1360 | processed-vars | |
1361 | (cdr (assoc :hlines params)) | |
1362 | (cdr (assoc :colnames params)) | |
1363 | (cdr (assoc :rownames params))))) | |
3ab2c837 | 1364 | (raw-result (or (cdr (assoc :results params)) "")) |
afe98dfa | 1365 | (result-params (append |
3ab2c837 BG |
1366 | (split-string (if (stringp raw-result) |
1367 | raw-result | |
1368 | (eval raw-result))) | |
afe98dfa CD |
1369 | (cdr (assoc :result-params params))))) |
1370 | (append | |
1371 | (mapcar (lambda (var) (cons :var var)) (car vars-and-names)) | |
1372 | (list | |
3ab2c837 BG |
1373 | (cons :colname-names (or (cdr (assoc :colname-names params)) |
1374 | (cadr vars-and-names))) | |
1375 | (cons :rowname-names (or (cdr (assoc :rowname-names params)) | |
1376 | (caddr vars-and-names))) | |
afe98dfa CD |
1377 | (cons :result-params result-params) |
1378 | (cons :result-type (cond ((member "output" result-params) 'output) | |
1379 | ((member "value" result-params) 'value) | |
1380 | (t 'value)))) | |
1381 | (org-babel-get-header params :var 'other)))) | |
86fbb8ca CD |
1382 | |
1383 | ;; row and column names | |
1384 | (defun org-babel-del-hlines (table) | |
1385 | "Remove all 'hlines from TABLE." | |
1386 | (remove 'hline table)) | |
1387 | ||
1388 | (defun org-babel-get-colnames (table) | |
1389 | "Return the column names of TABLE. | |
1390 | Return a cons cell, the `car' of which contains the TABLE less | |
1391 | colnames, and the `cdr' of which contains a list of the column | |
1392 | names." | |
1393 | (if (equal 'hline (nth 1 table)) | |
1394 | (cons (cddr table) (car table)) | |
1395 | (cons (cdr table) (car table)))) | |
1396 | ||
1397 | (defun org-babel-get-rownames (table) | |
1398 | "Return the row names of TABLE. | |
1399 | Return a cons cell, the `car' of which contains the TABLE less | |
1400 | colnames, and the `cdr' of which contains a list of the column | |
1401 | names. Note: this function removes any hlines in TABLE." | |
8223b1d2 BG |
1402 | (let* ((trans (lambda (table) (apply #'mapcar* #'list table))) |
1403 | (width (apply 'max | |
1404 | (mapcar (lambda (el) (if (listp el) (length el) 0)) table))) | |
1405 | (table (funcall trans (mapcar (lambda (row) | |
1406 | (if (not (equal row 'hline)) | |
1407 | row | |
1408 | (setq row '()) | |
1409 | (dotimes (n width) | |
1410 | (setq row (cons 'hline row))) | |
1411 | row)) | |
1412 | table)))) | |
1413 | (cons (mapcar (lambda (row) (if (equal (car row) 'hline) 'hline row)) | |
1414 | (funcall trans (cdr table))) | |
1415 | (remove 'hline (car table))))) | |
86fbb8ca CD |
1416 | |
1417 | (defun org-babel-put-colnames (table colnames) | |
1418 | "Add COLNAMES to TABLE if they exist." | |
1419 | (if colnames (apply 'list colnames 'hline table) table)) | |
1420 | ||
1421 | (defun org-babel-put-rownames (table rownames) | |
1422 | "Add ROWNAMES to TABLE if they exist." | |
1423 | (if rownames | |
1424 | (mapcar (lambda (row) | |
1425 | (if (listp row) | |
1426 | (cons (or (pop rownames) "") row) | |
1427 | row)) table) | |
1428 | table)) | |
1429 | ||
1430 | (defun org-babel-pick-name (names selector) | |
afe98dfa CD |
1431 | "Select one out of an alist of row or column names. |
1432 | SELECTOR can be either a list of names in which case those names | |
1433 | will be returned directly, or an index into the list NAMES in | |
1434 | which case the indexed names will be return." | |
1435 | (if (listp selector) | |
1436 | selector | |
1437 | (when names | |
1438 | (if (and selector (symbolp selector) (not (equal t selector))) | |
1439 | (cdr (assoc selector names)) | |
1440 | (if (integerp selector) | |
1441 | (nth (- selector 1) names) | |
1442 | (cdr (car (last names)))))))) | |
86fbb8ca CD |
1443 | |
1444 | (defun org-babel-disassemble-tables (vars hlines colnames rownames) | |
1445 | "Parse tables for further processing. | |
1446 | Process the variables in VARS according to the HLINES, | |
1447 | ROWNAMES and COLNAMES header arguments. Return a list consisting | |
1448 | of the vars, cnames and rnames." | |
1449 | (let (cnames rnames) | |
1450 | (list | |
1451 | (mapcar | |
1452 | (lambda (var) | |
1453 | (when (listp (cdr var)) | |
1454 | (when (and (not (equal colnames "no")) | |
1455 | (or colnames (and (equal (nth 1 (cdr var)) 'hline) | |
1456 | (not (member 'hline (cddr (cdr var))))))) | |
1457 | (let ((both (org-babel-get-colnames (cdr var)))) | |
1458 | (setq cnames (cons (cons (car var) (cdr both)) | |
1459 | cnames)) | |
1460 | (setq var (cons (car var) (car both))))) | |
1461 | (when (and rownames (not (equal rownames "no"))) | |
1462 | (let ((both (org-babel-get-rownames (cdr var)))) | |
1463 | (setq rnames (cons (cons (car var) (cdr both)) | |
1464 | rnames)) | |
1465 | (setq var (cons (car var) (car both))))) | |
1466 | (when (and hlines (not (equal hlines "yes"))) | |
1467 | (setq var (cons (car var) (org-babel-del-hlines (cdr var)))))) | |
1468 | var) | |
1469 | vars) | |
e66ba1df | 1470 | (reverse cnames) (reverse rnames)))) |
86fbb8ca CD |
1471 | |
1472 | (defun org-babel-reassemble-table (table colnames rownames) | |
1473 | "Add column and row names to a table. | |
1474 | Given a TABLE and set of COLNAMES and ROWNAMES add the names | |
1475 | to the table for reinsertion to org-mode." | |
1476 | (if (listp table) | |
1477 | ((lambda (table) | |
1478 | (if (and colnames (listp (car table)) (= (length (car table)) | |
1479 | (length colnames))) | |
1480 | (org-babel-put-colnames table colnames) table)) | |
1481 | (if (and rownames (= (length table) (length rownames))) | |
1482 | (org-babel-put-rownames table rownames) table)) | |
1483 | table)) | |
1484 | ||
1485 | (defun org-babel-where-is-src-block-head () | |
1486 | "Find where the current source block begins. | |
1487 | Return the point at the beginning of the current source | |
1488 | block. Specifically at the beginning of the #+BEGIN_SRC line. | |
1489 | If the point is not on a source block then return nil." | |
8223b1d2 | 1490 | (let ((initial (point)) (case-fold-search t) top bottom) |
86fbb8ca | 1491 | (or |
3ab2c837 | 1492 | (save-excursion ;; on a source name line or a #+header line |
86fbb8ca | 1493 | (beginning-of-line 1) |
3ab2c837 BG |
1494 | (and (or (looking-at org-babel-src-name-regexp) |
1495 | (looking-at org-babel-multi-line-header-regexp)) | |
1496 | (progn | |
1497 | (while (and (forward-line 1) | |
8223b1d2 BG |
1498 | (or (looking-at org-babel-src-name-regexp) |
1499 | (looking-at org-babel-multi-line-header-regexp)))) | |
3ab2c837 | 1500 | (looking-at org-babel-src-block-regexp)) |
86fbb8ca CD |
1501 | (point))) |
1502 | (save-excursion ;; on a #+begin_src line | |
1503 | (beginning-of-line 1) | |
1504 | (and (looking-at org-babel-src-block-regexp) | |
1505 | (point))) | |
1506 | (save-excursion ;; inside a src block | |
1507 | (and | |
1508 | (re-search-backward "^[ \t]*#\\+begin_src" nil t) (setq top (point)) | |
1509 | (re-search-forward "^[ \t]*#\\+end_src" nil t) (setq bottom (point)) | |
1510 | (< top initial) (< initial bottom) | |
1511 | (progn (goto-char top) (beginning-of-line 1) | |
1512 | (looking-at org-babel-src-block-regexp)) | |
1513 | (point)))))) | |
1514 | ||
afe98dfa CD |
1515 | ;;;###autoload |
1516 | (defun org-babel-goto-src-block-head () | |
1517 | "Go to the beginning of the current code block." | |
1518 | (interactive) | |
1519 | ((lambda (head) | |
8223b1d2 | 1520 | (if head (goto-char head) (error "Not currently in a code block"))) |
afe98dfa CD |
1521 | (org-babel-where-is-src-block-head))) |
1522 | ||
86fbb8ca CD |
1523 | ;;;###autoload |
1524 | (defun org-babel-goto-named-src-block (name) | |
1525 | "Go to a named source-code block." | |
1526 | (interactive | |
8223b1d2 BG |
1527 | (let ((completion-ignore-case t) |
1528 | (case-fold-search t) | |
1529 | (under-point (thing-at-point 'line))) | |
1530 | (list (org-icompleting-read | |
1531 | "source-block name: " (org-babel-src-block-names) nil t | |
1532 | (cond | |
1533 | ;; noweb | |
1534 | ((string-match (org-babel-noweb-wrap) under-point) | |
1535 | (let ((block-name (match-string 1 under-point))) | |
1536 | (string-match "[^(]*" block-name) | |
1537 | (match-string 0 block-name))) | |
1538 | ;; #+call: | |
1539 | ((string-match org-babel-lob-one-liner-regexp under-point) | |
1540 | (let ((source-info (car (org-babel-lob-get-info)))) | |
1541 | (if (string-match "^\\([^\\[]+?\\)\\(\\[.*\\]\\)?(" source-info) | |
1542 | (let ((source-name (match-string 1 source-info))) | |
1543 | source-name)))) | |
1544 | ;; #+results: | |
1545 | ((string-match (concat "#\\+" org-babel-results-keyword | |
1546 | "\\:\s+\\([^\\(]*\\)") under-point) | |
1547 | (match-string 1 under-point)) | |
1548 | ;; symbol-at-point | |
1549 | ((and (thing-at-point 'symbol)) | |
1550 | (org-babel-find-named-block (thing-at-point 'symbol)) | |
1551 | (thing-at-point 'symbol)) | |
1552 | ("")))))) | |
86fbb8ca CD |
1553 | (let ((point (org-babel-find-named-block name))) |
1554 | (if point | |
1555 | ;; taken from `org-open-at-point' | |
8223b1d2 | 1556 | (progn (org-mark-ring-push) (goto-char point) (org-show-context)) |
86fbb8ca CD |
1557 | (message "source-code block '%s' not found in this buffer" name)))) |
1558 | ||
1559 | (defun org-babel-find-named-block (name) | |
1560 | "Find a named source-code block. | |
1561 | Return the location of the source block identified by source | |
8223b1d2 | 1562 | NAME, or nil if no such block exists. Set match data according to |
86fbb8ca CD |
1563 | org-babel-named-src-block-regexp." |
1564 | (save-excursion | |
1565 | (let ((case-fold-search t) | |
1566 | (regexp (org-babel-named-src-block-regexp-for-name name)) msg) | |
1567 | (goto-char (point-min)) | |
1568 | (when (or (re-search-forward regexp nil t) | |
e66ba1df | 1569 | (re-search-backward regexp nil t)) |
86fbb8ca CD |
1570 | (match-beginning 0))))) |
1571 | ||
1572 | (defun org-babel-src-block-names (&optional file) | |
1573 | "Returns the names of source blocks in FILE or the current buffer." | |
1574 | (save-excursion | |
1575 | (when file (find-file file)) (goto-char (point-min)) | |
8223b1d2 | 1576 | (let ((case-fold-search t) names) |
86fbb8ca | 1577 | (while (re-search-forward org-babel-src-name-w-name-regexp nil t) |
e66ba1df | 1578 | (setq names (cons (match-string 3) names))) |
86fbb8ca CD |
1579 | names))) |
1580 | ||
1581 | ;;;###autoload | |
1582 | (defun org-babel-goto-named-result (name) | |
1583 | "Go to a named result." | |
1584 | (interactive | |
1585 | (let ((completion-ignore-case t)) | |
1586 | (list (org-icompleting-read "source-block name: " | |
1587 | (org-babel-result-names) nil t)))) | |
1588 | (let ((point (org-babel-find-named-result name))) | |
1589 | (if point | |
1590 | ;; taken from `org-open-at-point' | |
1591 | (progn (goto-char point) (org-show-context)) | |
1592 | (message "result '%s' not found in this buffer" name)))) | |
1593 | ||
e66ba1df | 1594 | (defun org-babel-find-named-result (name &optional point) |
86fbb8ca CD |
1595 | "Find a named result. |
1596 | Return the location of the result named NAME in the current | |
1597 | buffer or nil if no such result exists." | |
1598 | (save-excursion | |
8223b1d2 BG |
1599 | (let ((case-fold-search t)) |
1600 | (goto-char (or point (point-min))) | |
1601 | (catch 'is-a-code-block | |
1602 | (when (re-search-forward | |
1603 | (concat org-babel-result-regexp | |
1604 | "[ \t]" (regexp-quote name) "[ \t]*[\n\f\v\r]") nil t) | |
1605 | (when (and (string= "name" (downcase (match-string 1))) | |
1606 | (or (beginning-of-line 1) | |
1607 | (looking-at org-babel-src-block-regexp) | |
1608 | (looking-at org-babel-multi-line-header-regexp))) | |
1609 | (throw 'is-a-code-block (org-babel-find-named-result name (point)))) | |
1610 | (beginning-of-line 0) (point)))))) | |
86fbb8ca CD |
1611 | |
1612 | (defun org-babel-result-names (&optional file) | |
1613 | "Returns the names of results in FILE or the current buffer." | |
1614 | (save-excursion | |
1615 | (when file (find-file file)) (goto-char (point-min)) | |
8223b1d2 | 1616 | (let ((case-fold-search t) names) |
86fbb8ca | 1617 | (while (re-search-forward org-babel-result-w-name-regexp nil t) |
3ab2c837 | 1618 | (setq names (cons (match-string 4) names))) |
86fbb8ca CD |
1619 | names))) |
1620 | ||
1621 | ;;;###autoload | |
1622 | (defun org-babel-next-src-block (&optional arg) | |
1623 | "Jump to the next source block. | |
1624 | With optional prefix argument ARG, jump forward ARG many source blocks." | |
1625 | (interactive "P") | |
1626 | (when (looking-at org-babel-src-block-regexp) (forward-char 1)) | |
afe98dfa CD |
1627 | (condition-case nil |
1628 | (re-search-forward org-babel-src-block-regexp nil nil (or arg 1)) | |
1629 | (error (error "No further code blocks"))) | |
86fbb8ca CD |
1630 | (goto-char (match-beginning 0)) (org-show-context)) |
1631 | ||
1632 | ;;;###autoload | |
1633 | (defun org-babel-previous-src-block (&optional arg) | |
1634 | "Jump to the previous source block. | |
1635 | With optional prefix argument ARG, jump backward ARG many source blocks." | |
1636 | (interactive "P") | |
afe98dfa CD |
1637 | (condition-case nil |
1638 | (re-search-backward org-babel-src-block-regexp nil nil (or arg 1)) | |
1639 | (error (error "No previous code blocks"))) | |
86fbb8ca CD |
1640 | (goto-char (match-beginning 0)) (org-show-context)) |
1641 | ||
afe98dfa CD |
1642 | (defvar org-babel-load-languages) |
1643 | ||
1644 | ;;;###autoload | |
1645 | (defun org-babel-mark-block () | |
8223b1d2 | 1646 | "Mark current src block." |
afe98dfa CD |
1647 | (interactive) |
1648 | ((lambda (head) | |
1649 | (when head | |
1650 | (save-excursion | |
1651 | (goto-char head) | |
1652 | (looking-at org-babel-src-block-regexp)) | |
1653 | (push-mark (match-end 5) nil t) | |
1654 | (goto-char (match-beginning 5)))) | |
1655 | (org-babel-where-is-src-block-head))) | |
1656 | ||
1657 | (defun org-babel-demarcate-block (&optional arg) | |
1658 | "Wrap or split the code in the region or on the point. | |
1659 | When called from inside of a code block the current block is | |
1660 | split. When called from outside of a code block a new code block | |
1661 | is created. In both cases if the region is demarcated and if the | |
1662 | region is not active then the point is demarcated." | |
1663 | (interactive "P") | |
1664 | (let ((info (org-babel-get-src-block-info 'light)) | |
e66ba1df BG |
1665 | (headers (progn (org-babel-where-is-src-block-head) |
1666 | (match-string 4))) | |
afe98dfa CD |
1667 | (stars (concat (make-string (or (org-current-level) 1) ?*) " "))) |
1668 | (if info | |
1669 | (mapc | |
1670 | (lambda (place) | |
1671 | (save-excursion | |
1672 | (goto-char place) | |
1673 | (let ((lang (nth 0 info)) | |
1674 | (indent (make-string (nth 5 info) ? ))) | |
1675 | (when (string-match "^[[:space:]]*$" | |
1676 | (buffer-substring (point-at-bol) | |
1677 | (point-at-eol))) | |
1678 | (delete-region (point-at-bol) (point-at-eol))) | |
e66ba1df BG |
1679 | (insert (concat |
1680 | (if (looking-at "^") "" "\n") | |
1681 | indent "#+end_src\n" | |
1682 | (if arg stars indent) "\n" | |
1683 | indent "#+begin_src " lang | |
1684 | (if (> (length headers) 1) | |
1685 | (concat " " headers) headers) | |
1686 | (if (looking-at "[\n\r]") | |
1687 | "" | |
1688 | (concat "\n" (make-string (current-column) ? ))))))) | |
afe98dfa | 1689 | (move-end-of-line 2)) |
8223b1d2 | 1690 | (sort (if (org-region-active-p) (list (mark) (point)) (list (point))) #'>)) |
afe98dfa CD |
1691 | (let ((start (point)) |
1692 | (lang (org-icompleting-read "Lang: " | |
1693 | (mapcar (lambda (el) (symbol-name (car el))) | |
1694 | org-babel-load-languages))) | |
1695 | (body (delete-and-extract-region | |
8223b1d2 | 1696 | (if (org-region-active-p) (mark) (point)) (point)))) |
afe98dfa CD |
1697 | (insert (concat (if (looking-at "^") "" "\n") |
1698 | (if arg (concat stars "\n") "") | |
1699 | "#+begin_src " lang "\n" | |
1700 | body | |
1701 | (if (or (= (length body) 0) | |
1702 | (string-match "[\r\n]$" body)) "" "\n") | |
1703 | "#+end_src\n")) | |
1704 | (goto-char start) (move-end-of-line 1))))) | |
1705 | ||
86fbb8ca CD |
1706 | (defvar org-babel-lob-one-liner-regexp) |
1707 | (defun org-babel-where-is-src-block-result (&optional insert info hash indent) | |
1708 | "Find where the current source block results begin. | |
1709 | Return the point at the beginning of the result of the current | |
1710 | source block. Specifically at the beginning of the results line. | |
1711 | If no result exists for this block then create a results line | |
1712 | following the source block." | |
1713 | (save-excursion | |
8223b1d2 BG |
1714 | (let* ((case-fold-search t) |
1715 | (on-lob-line (save-excursion | |
3ab2c837 BG |
1716 | (beginning-of-line 1) |
1717 | (looking-at org-babel-lob-one-liner-regexp))) | |
e66ba1df | 1718 | (inlinep (when (org-babel-get-inline-src-block-matches) |
8223b1d2 | 1719 | (match-end 0))) |
86fbb8ca | 1720 | (name (if on-lob-line |
153ae947 | 1721 | (mapconcat #'identity (butlast (org-babel-lob-get-info)) "") |
e66ba1df | 1722 | (nth 4 (or info (org-babel-get-src-block-info 'light))))) |
86fbb8ca CD |
1723 | (head (unless on-lob-line (org-babel-where-is-src-block-head))) |
1724 | found beg end) | |
1725 | (when head (goto-char head)) | |
1726 | (setq | |
1727 | found ;; was there a result (before we potentially insert one) | |
1728 | (or | |
3ab2c837 | 1729 | inlinep |
86fbb8ca CD |
1730 | (and |
1731 | ;; named results: | |
1732 | ;; - return t if it is found, else return nil | |
1733 | ;; - if it does not need to be rebuilt, then don't set end | |
1734 | ;; - if it does need to be rebuilt then do set end | |
1735 | name (setq beg (org-babel-find-named-result name)) | |
1736 | (prog1 beg | |
1737 | (when (and hash (not (string= hash (match-string 3)))) | |
1738 | (goto-char beg) (setq end beg) ;; beginning of result | |
1739 | (forward-line 1) | |
1740 | (delete-region end (org-babel-result-end)) nil))) | |
1741 | (and | |
1742 | ;; unnamed results: | |
1743 | ;; - return t if it is found, else return nil | |
1744 | ;; - if it is found, and the hash doesn't match, delete and set end | |
1745 | (or on-lob-line (re-search-forward "^[ \t]*#\\+end_src" nil t)) | |
1746 | (progn (end-of-line 1) | |
1747 | (if (eobp) (insert "\n") (forward-char 1)) | |
1748 | (setq end (point)) | |
1749 | (or (and (not name) | |
1750 | (progn ;; unnamed results line already exists | |
1751 | (re-search-forward "[^ \f\t\n\r\v]" nil t) | |
1752 | (beginning-of-line 1) | |
1753 | (looking-at | |
1754 | (concat org-babel-result-regexp "\n"))) | |
1755 | (prog1 (point) | |
1756 | ;; must remove and rebuild if hash!=old-hash | |
1757 | (if (and hash (not (string= hash (match-string 3)))) | |
1758 | (prog1 nil | |
1759 | (forward-line 1) | |
1760 | (delete-region | |
1761 | end (org-babel-result-end))) | |
1762 | (setq end nil))))))))) | |
1763 | (if (and insert end) | |
1764 | (progn | |
1765 | (goto-char end) | |
1766 | (unless beg | |
1767 | (if (looking-at "[\n\r]") (forward-char 1) (insert "\n"))) | |
1768 | (insert (concat | |
1769 | (if indent | |
1770 | (mapconcat | |
1771 | (lambda (el) " ") | |
afe98dfa | 1772 | (org-number-sequence 1 indent) "") |
86fbb8ca | 1773 | "") |
153ae947 | 1774 | "#+" org-babel-results-keyword |
86fbb8ca CD |
1775 | (when hash (concat "["hash"]")) |
1776 | ":" | |
1777 | (when name (concat " " name)) "\n")) | |
1778 | (unless beg (insert "\n") (backward-char)) | |
1779 | (beginning-of-line 0) | |
1780 | (if hash (org-babel-hide-hash)) | |
1781 | (point)) | |
1782 | found)))) | |
1783 | ||
1784 | (defvar org-block-regexp) | |
1785 | (defun org-babel-read-result () | |
1786 | "Read the result at `point' into emacs-lisp." | |
1787 | (let ((case-fold-search t) result-string) | |
1788 | (cond | |
1789 | ((org-at-table-p) (org-babel-read-table)) | |
3ab2c837 | 1790 | ((org-at-item-p) (org-babel-read-list)) |
86fbb8ca CD |
1791 | ((looking-at org-bracket-link-regexp) (org-babel-read-link)) |
1792 | ((looking-at org-block-regexp) (org-babel-trim (match-string 4))) | |
1793 | ((looking-at "^[ \t]*: ") | |
1794 | (setq result-string | |
1795 | (org-babel-trim | |
1796 | (mapconcat (lambda (line) | |
1797 | (if (and (> (length line) 1) | |
1798 | (string-match "^[ \t]*: \\(.+\\)" line)) | |
1799 | (match-string 1 line) | |
1800 | line)) | |
1801 | (split-string | |
1802 | (buffer-substring | |
1803 | (point) (org-babel-result-end)) "[\r\n]+") | |
1804 | "\n"))) | |
1805 | (or (org-babel-number-p result-string) result-string)) | |
1806 | ((looking-at org-babel-result-regexp) | |
1807 | (save-excursion (forward-line 1) (org-babel-read-result)))))) | |
1808 | ||
1809 | (defun org-babel-read-table () | |
1810 | "Read the table at `point' into emacs-lisp." | |
1811 | (mapcar (lambda (row) | |
1812 | (if (and (symbolp row) (equal row 'hline)) row | |
3ab2c837 | 1813 | (mapcar (lambda (el) (org-babel-read el 'inhibit-lisp-eval)) row))) |
86fbb8ca CD |
1814 | (org-table-to-lisp))) |
1815 | ||
acedf35c CD |
1816 | (defun org-babel-read-list () |
1817 | "Read the list at `point' into emacs-lisp." | |
3ab2c837 BG |
1818 | (mapcar (lambda (el) (org-babel-read el 'inhibit-lisp-eval)) |
1819 | (mapcar #'cadr (cdr (org-list-parse-list))))) | |
acedf35c | 1820 | |
86fbb8ca CD |
1821 | (defvar org-link-types-re) |
1822 | (defun org-babel-read-link () | |
1823 | "Read the link at `point' into emacs-lisp. | |
1824 | If the path of the link is a file path it is expanded using | |
1825 | `expand-file-name'." | |
1826 | (let* ((case-fold-search t) | |
1827 | (raw (and (looking-at org-bracket-link-regexp) | |
8223b1d2 | 1828 | (org-no-properties (match-string 1)))) |
86fbb8ca CD |
1829 | (type (and (string-match org-link-types-re raw) |
1830 | (match-string 1 raw)))) | |
1831 | (cond | |
1832 | ((not type) (expand-file-name raw)) | |
1833 | ((string= type "file") | |
1834 | (and (string-match "file\\(.*\\):\\(.+\\)" raw) | |
1835 | (expand-file-name (match-string 2 raw)))) | |
1836 | (t raw)))) | |
1837 | ||
3ab2c837 BG |
1838 | (defun org-babel-format-result (result &optional sep) |
1839 | "Format RESULT for writing to file." | |
8223b1d2 | 1840 | (let ((echo-res (lambda (r) (if (stringp r) r (format "%S" r))))) |
3ab2c837 BG |
1841 | (if (listp result) |
1842 | ;; table result | |
1843 | (orgtbl-to-generic | |
8223b1d2 | 1844 | result (list :sep (or sep "\t") :fmt echo-res)) |
3ab2c837 | 1845 | ;; scalar result |
8223b1d2 | 1846 | (funcall echo-res result)))) |
3ab2c837 | 1847 | |
86fbb8ca CD |
1848 | (defun org-babel-insert-result |
1849 | (result &optional result-params info hash indent lang) | |
1850 | "Insert RESULT into the current buffer. | |
1851 | By default RESULT is inserted after the end of the | |
1852 | current source block. With optional argument RESULT-PARAMS | |
1853 | controls insertion of results in the org-mode file. | |
8223b1d2 | 1854 | RESULT-PARAMS can take the following values: |
86fbb8ca CD |
1855 | |
1856 | replace - (default option) insert results after the source block | |
1857 | replacing any previously inserted results | |
1858 | ||
1859 | silent -- no results are inserted | |
1860 | ||
1861 | file ---- the results are interpreted as a file path, and are | |
1862 | inserted into the buffer using the Org-mode file syntax | |
1863 | ||
acedf35c CD |
1864 | list ---- the results are interpreted as an Org-mode list. |
1865 | ||
1866 | raw ----- results are added directly to the Org-mode file. This | |
86fbb8ca CD |
1867 | is a good option if you code block will output org-mode |
1868 | formatted text. | |
1869 | ||
8223b1d2 | 1870 | drawer -- results are added directly to the Org-mode file as with |
e66ba1df BG |
1871 | \"raw\", but are wrapped in a RESULTS drawer, allowing |
1872 | them to later be replaced or removed automatically. | |
1873 | ||
8223b1d2 BG |
1874 | org ----- results are added inside of a \"#+BEGIN_SRC org\" block. |
1875 | They are not comma-escaped when inserted, but Org syntax | |
1876 | here will be discarded when exporting the file. | |
86fbb8ca CD |
1877 | |
1878 | html ---- results are added inside of a #+BEGIN_HTML block. This | |
1879 | is a good option if you code block will output html | |
1880 | formatted text. | |
1881 | ||
1882 | latex --- results are added inside of a #+BEGIN_LATEX block. | |
1883 | This is a good option if you code block will output | |
1884 | latex formatted text. | |
1885 | ||
1886 | code ---- the results are extracted in the syntax of the source | |
1887 | code of the language being evaluated and are added | |
1888 | inside of a #+BEGIN_SRC block with the source-code | |
1889 | language set appropriately. Note this relies on the | |
1890 | optional LANG argument." | |
1891 | (if (stringp result) | |
1892 | (progn | |
8223b1d2 | 1893 | (setq result (org-no-properties result)) |
86fbb8ca | 1894 | (when (member "file" result-params) |
8223b1d2 BG |
1895 | (setq result (org-babel-result-to-file |
1896 | result (when (assoc :file-desc (nth 2 info)) | |
1897 | (or (cdr (assoc :file-desc (nth 2 info))) | |
1898 | result)))))) | |
86fbb8ca | 1899 | (unless (listp result) (setq result (format "%S" result)))) |
afe98dfa CD |
1900 | (if (and result-params (member "silent" result-params)) |
1901 | (progn | |
1902 | (message (replace-regexp-in-string "%" "%%" (format "%S" result))) | |
1903 | result) | |
afe98dfa | 1904 | (save-excursion |
3ab2c837 BG |
1905 | (let* ((inlinep |
1906 | (save-excursion | |
e66ba1df BG |
1907 | (when (or (org-babel-get-inline-src-block-matches) |
1908 | (org-babel-get-lob-one-liner-matches)) | |
3ab2c837 BG |
1909 | (goto-char (match-end 0)) |
1910 | (insert (if (listp result) "\n" " ")) | |
1911 | (point)))) | |
1912 | (existing-result (unless inlinep | |
1913 | (org-babel-where-is-src-block-result | |
1914 | t info hash indent))) | |
1915 | (results-switches | |
1916 | (cdr (assoc :results_switches (nth 2 info)))) | |
1917 | beg end) | |
1918 | (when (and (stringp result) ; ensure results end in a newline | |
1919 | (not inlinep) | |
1920 | (> (length result) 0) | |
1921 | (not (or (string-equal (substring result -1) "\n") | |
1922 | (string-equal (substring result -1) "\r")))) | |
1923 | (setq result (concat result "\n"))) | |
afe98dfa | 1924 | (if (not existing-result) |
3ab2c837 | 1925 | (setq beg (or inlinep (point))) |
afe98dfa CD |
1926 | (goto-char existing-result) |
1927 | (save-excursion | |
1928 | (re-search-forward "#" nil t) | |
1929 | (setq indent (- (current-column) 1))) | |
1930 | (forward-line 1) | |
1931 | (setq beg (point)) | |
86fbb8ca | 1932 | (cond |
afe98dfa CD |
1933 | ((member "replace" result-params) |
1934 | (delete-region (point) (org-babel-result-end))) | |
1935 | ((member "append" result-params) | |
acedf35c CD |
1936 | (goto-char (org-babel-result-end)) (setq beg (point-marker))) |
1937 | ((member "prepend" result-params)))) ; already there | |
afe98dfa CD |
1938 | (setq results-switches |
1939 | (if results-switches (concat " " results-switches) "")) | |
bdebdb64 | 1940 | (let ((wrap (lambda (start finish) |
8223b1d2 BG |
1941 | (goto-char end) (insert (concat finish "\n")) |
1942 | (goto-char beg) (insert (concat start "\n")) | |
bdebdb64 | 1943 | (org-escape-code-in-region (point) end) |
8223b1d2 BG |
1944 | (goto-char end) (goto-char (point-at-eol)) |
1945 | (setq end (point-marker)))) | |
1946 | (proper-list-p (lambda (it) (and (listp it) (null (cdr (last it))))))) | |
e66ba1df BG |
1947 | ;; insert results based on type |
1948 | (cond | |
1949 | ;; do nothing for an empty result | |
1950 | ((null result)) | |
1951 | ;; insert a list if preferred | |
1952 | ((member "list" result-params) | |
1953 | (insert | |
1954 | (org-babel-trim | |
1955 | (org-list-to-generic | |
1956 | (cons 'unordered | |
1957 | (mapcar | |
1958 | (lambda (el) (list nil (if (stringp el) el (format "%S" el)))) | |
1959 | (if (listp result) result (list result)))) | |
1960 | '(:splicep nil :istart "- " :iend "\n"))) | |
1961 | "\n")) | |
1962 | ;; assume the result is a table if it's not a string | |
8223b1d2 | 1963 | ((funcall proper-list-p result) |
e66ba1df BG |
1964 | (goto-char beg) |
1965 | (insert (concat (orgtbl-to-orgtbl | |
1966 | (if (or (eq 'hline (car result)) | |
1967 | (and (listp (car result)) | |
1968 | (listp (cdr (car result))))) | |
1969 | result (list result)) | |
1970 | '(:fmt (lambda (cell) (format "%s" cell)))) "\n")) | |
1971 | (goto-char beg) (when (org-at-table-p) (org-table-align))) | |
8223b1d2 | 1972 | ((and (listp result) (not (funcall proper-list-p result))) |
e66ba1df BG |
1973 | (insert (format "%s\n" result))) |
1974 | ((member "file" result-params) | |
1975 | (when inlinep (goto-char inlinep)) | |
1976 | (insert result)) | |
1977 | (t (goto-char beg) (insert result))) | |
8223b1d2 | 1978 | (when (funcall proper-list-p result) (goto-char (org-table-end))) |
e66ba1df BG |
1979 | (setq end (point-marker)) |
1980 | ;; possibly wrap result | |
acedf35c | 1981 | (cond |
8223b1d2 BG |
1982 | ((assoc :wrap (nth 2 info)) |
1983 | (let ((name (or (cdr (assoc :wrap (nth 2 info))) "RESULTS"))) | |
1984 | (funcall wrap (concat "#+BEGIN_" name) (concat "#+END_" name)))) | |
acedf35c | 1985 | ((member "html" result-params) |
8223b1d2 | 1986 | (funcall wrap "#+BEGIN_HTML" "#+END_HTML")) |
acedf35c | 1987 | ((member "latex" result-params) |
8223b1d2 | 1988 | (funcall wrap "#+BEGIN_LaTeX" "#+END_LaTeX")) |
acedf35c | 1989 | ((member "org" result-params) |
bdebdb64 | 1990 | (funcall wrap "#+BEGIN_SRC org" "#+END_SRC")) |
8223b1d2 BG |
1991 | ((member "code" result-params) |
1992 | (funcall wrap (format "#+BEGIN_SRC %s%s" (or lang "none") results-switches) | |
1993 | "#+END_SRC")) | |
acedf35c CD |
1994 | ((member "raw" result-params) |
1995 | (goto-char beg) (if (org-at-table-p) (org-cycle))) | |
8223b1d2 BG |
1996 | ((or (member "drawer" result-params) |
1997 | ;; Stay backward compatible with <7.9.2 | |
1998 | (member "wrap" result-params)) | |
1999 | (funcall wrap ":RESULTS:" ":END:")) | |
2000 | ((and (not (funcall proper-list-p result)) | |
e66ba1df | 2001 | (not (member "file" result-params))) |
acedf35c CD |
2002 | (org-babel-examplize-region beg end results-switches) |
2003 | (setq end (point))))) | |
afe98dfa | 2004 | ;; possibly indent the results to match the #+results line |
3ab2c837 | 2005 | (when (and (not inlinep) (numberp indent) indent (> indent 0) |
afe98dfa CD |
2006 | ;; in this case `table-align' does the work for us |
2007 | (not (and (listp result) | |
2008 | (member "append" result-params)))) | |
2009 | (indent-rigidly beg end indent)))) | |
e66ba1df | 2010 | (if (null result) |
afe98dfa | 2011 | (if (member "value" result-params) |
acedf35c CD |
2012 | (message "Code block returned no value.") |
2013 | (message "Code block produced no output.")) | |
2014 | (message "Code block evaluation complete.")))) | |
86fbb8ca CD |
2015 | |
2016 | (defun org-babel-remove-result (&optional info) | |
2017 | "Remove the result of the current source block." | |
2018 | (interactive) | |
2019 | (let ((location (org-babel-where-is-src-block-result nil info)) start) | |
2020 | (when location | |
e66ba1df | 2021 | (setq start (- location 1)) |
86fbb8ca | 2022 | (save-excursion |
e66ba1df | 2023 | (goto-char location) (forward-line 1) |
86fbb8ca CD |
2024 | (delete-region start (org-babel-result-end)))))) |
2025 | ||
2026 | (defun org-babel-result-end () | |
8223b1d2 | 2027 | "Return the point at the end of the current set of results." |
86fbb8ca | 2028 | (save-excursion |
acedf35c CD |
2029 | (cond |
2030 | ((org-at-table-p) (progn (goto-char (org-table-end)) (point))) | |
3ab2c837 BG |
2031 | ((org-at-item-p) (let* ((struct (org-list-struct)) |
2032 | (prvs (org-list-prevs-alist struct))) | |
2033 | (org-list-get-list-end (point-at-bol) struct prvs))) | |
8223b1d2 | 2034 | ((let ((case-fold-search t)) (looking-at "^\\([ \t]*\\):results:")) |
153ae947 BG |
2035 | (progn (re-search-forward (concat "^" (match-string 1) ":END:")) |
2036 | (forward-char 1) (point))) | |
acedf35c | 2037 | (t |
8223b1d2 BG |
2038 | (let ((case-fold-search t)) |
2039 | (if (looking-at (concat "[ \t]*#\\+begin_\\([^ \t\n\r]+\\)")) | |
2040 | (progn (re-search-forward (concat "[ \t]*#\\+end_" (match-string 1)) | |
2041 | nil t) | |
3ab2c837 | 2042 | (forward-char 1)) |
acedf35c CD |
2043 | (while (looking-at "[ \t]*\\(: \\|\\[\\[\\)") |
2044 | (forward-line 1)))) | |
2045 | (point))))) | |
86fbb8ca | 2046 | |
8223b1d2 BG |
2047 | (defun org-babel-result-to-file (result &optional description) |
2048 | "Convert RESULT into an `org-mode' link with optional DESCRIPTION. | |
86fbb8ca CD |
2049 | If the `default-directory' is different from the containing |
2050 | file's directory then expand relative links." | |
8223b1d2 BG |
2051 | (when (stringp result) |
2052 | (format "[[file:%s]%s]" | |
2053 | (if (and default-directory | |
2054 | buffer-file-name | |
2055 | (not (string= (expand-file-name default-directory) | |
2056 | (expand-file-name | |
2057 | (file-name-directory buffer-file-name))))) | |
2058 | (expand-file-name result default-directory) | |
2059 | result) | |
2060 | (if description (concat "[" description "]") "")))) | |
86fbb8ca | 2061 | |
2a88ee23 BG |
2062 | (defvar org-babel-capitalize-examplize-region-markers nil |
2063 | "Make true to capitalize begin/end example markers inserted by code blocks.") | |
2064 | ||
86fbb8ca | 2065 | (defun org-babel-examplize-region (beg end &optional results-switches) |
3ab2c837 | 2066 | "Comment out region using the inline '==' or ': ' org example quote." |
86fbb8ca | 2067 | (interactive "*r") |
8223b1d2 BG |
2068 | (let ((chars-between (lambda (b e) |
2069 | (not (string-match "^[\\s]*$" (buffer-substring b e))))) | |
2070 | (maybe-cap (lambda (str) (if org-babel-capitalize-examplize-region-markers | |
2071 | (upcase str) str)))) | |
2072 | (if (or (funcall chars-between (save-excursion (goto-char beg) (point-at-bol)) beg) | |
2073 | (funcall chars-between end (save-excursion (goto-char end) (point-at-eol)))) | |
3ab2c837 BG |
2074 | (save-excursion |
2075 | (goto-char beg) | |
2076 | (insert (format "=%s=" (prog1 (buffer-substring beg end) | |
2077 | (delete-region beg end))))) | |
2078 | (let ((size (count-lines beg end))) | |
2079 | (save-excursion | |
2080 | (cond ((= size 0)) ; do nothing for an empty result | |
2081 | ((< size org-babel-min-lines-for-block-output) | |
2082 | (goto-char beg) | |
2083 | (dotimes (n size) | |
2084 | (beginning-of-line 1) (insert ": ") (forward-line 1))) | |
2085 | (t | |
2086 | (goto-char beg) | |
2087 | (insert (if results-switches | |
2a88ee23 | 2088 | (format "%s%s\n" |
8223b1d2 | 2089 | (funcall maybe-cap "#+begin_example") |
2a88ee23 | 2090 | results-switches) |
8223b1d2 | 2091 | (funcall maybe-cap "#+begin_example\n"))) |
3ab2c837 | 2092 | (if (markerp end) (goto-char end) (forward-char (- end beg))) |
8223b1d2 | 2093 | (insert (funcall maybe-cap "#+end_example\n"))))))))) |
86fbb8ca | 2094 | |
afe98dfa CD |
2095 | (defun org-babel-update-block-body (new-body) |
2096 | "Update the body of the current code block to NEW-BODY." | |
2097 | (if (not (org-babel-where-is-src-block-head)) | |
8223b1d2 | 2098 | (error "Not in a source block") |
afe98dfa | 2099 | (save-match-data |
3ab2c837 | 2100 | (replace-match (concat (org-babel-trim new-body) "\n") nil t nil 5)) |
afe98dfa CD |
2101 | (indent-rigidly (match-beginning 5) (match-end 5) 2))) |
2102 | ||
86fbb8ca CD |
2103 | (defun org-babel-merge-params (&rest plists) |
2104 | "Combine all parameter association lists in PLISTS. | |
3ab2c837 | 2105 | Later elements of PLISTS override the values of previous elements. |
86fbb8ca CD |
2106 | This takes into account some special considerations for certain |
2107 | parameters when merging lists." | |
8223b1d2 BG |
2108 | (let* ((results-exclusive-groups |
2109 | (mapcar (lambda (group) (mapcar #'symbol-name group)) | |
2110 | (cdr (assoc 'results org-babel-common-header-args-w-values)))) | |
2111 | (exports-exclusive-groups | |
2112 | (mapcar (lambda (group) (mapcar #'symbol-name group)) | |
2113 | (cdr (assoc 'exports org-babel-common-header-args-w-values)))) | |
2114 | (variable-index 0) | |
2115 | (e-merge (lambda (exclusive-groups &rest result-params) | |
2116 | ;; maintain exclusivity of mutually exclusive parameters | |
2117 | (let (output) | |
2118 | (mapc (lambda (new-params) | |
2119 | (mapc (lambda (new-param) | |
2120 | (mapc (lambda (exclusive-group) | |
2121 | (when (member new-param exclusive-group) | |
2122 | (mapcar (lambda (excluded-param) | |
2123 | (setq output | |
2124 | (delete | |
2125 | excluded-param | |
2126 | output))) | |
2127 | exclusive-group))) | |
2128 | exclusive-groups) | |
2129 | (setq output (org-uniquify | |
2130 | (cons new-param output)))) | |
2131 | new-params)) | |
2132 | result-params) | |
2133 | output))) | |
2134 | params results exports tangle noweb cache vars shebang comments padline) | |
2135 | ||
2136 | (mapc | |
2137 | (lambda (plist) | |
2138 | (mapc | |
2139 | (lambda (pair) | |
2140 | (case (car pair) | |
2141 | (:var | |
2142 | (let ((name (if (listp (cdr pair)) | |
2143 | (cadr pair) | |
2144 | (and (string-match "^\\([^= \f\t\n\r\v]+\\)[ \t]*=" | |
2145 | (cdr pair)) | |
2146 | (intern (match-string 1 (cdr pair))))))) | |
2147 | (if name | |
2148 | (setq vars | |
2149 | (append | |
2150 | (if (member name (mapcar #'car vars)) | |
2151 | (delq nil | |
2152 | (mapcar | |
2153 | (lambda (p) | |
2154 | (unless (equal (car p) name) p)) | |
2155 | vars)) | |
2156 | vars) | |
2157 | (list (cons name pair)))) | |
2158 | ;; if no name is given and we already have named variables | |
2159 | ;; then assign to named variables in order | |
2160 | (if (and vars (nth variable-index vars)) | |
2161 | (prog1 (setf (cddr (nth variable-index vars)) | |
2162 | (concat (symbol-name | |
2163 | (car (nth variable-index vars))) | |
2164 | "=" (cdr pair))) | |
2165 | (incf variable-index)) | |
2166 | (error "Variable \"%s\" must be assigned a default value" | |
2167 | (cdr pair)))))) | |
2168 | (:results | |
2169 | (setq results (funcall e-merge results-exclusive-groups | |
2170 | results | |
2171 | (split-string | |
2172 | (let ((r (cdr pair))) | |
2173 | (if (stringp r) r (eval r))))))) | |
2174 | (:file | |
2175 | (when (cdr pair) | |
2176 | (setq results (funcall e-merge results-exclusive-groups | |
2177 | results '("file"))) | |
2178 | (unless (or (member "both" exports) | |
2179 | (member "none" exports) | |
2180 | (member "code" exports)) | |
2181 | (setq exports (funcall e-merge exports-exclusive-groups | |
2182 | exports '("results")))) | |
2183 | (setq params (cons pair (assq-delete-all (car pair) params))))) | |
2184 | (:exports | |
2185 | (setq exports (funcall e-merge exports-exclusive-groups | |
2186 | exports (split-string (cdr pair))))) | |
2187 | (:tangle ;; take the latest -- always overwrite | |
2188 | (setq tangle (or (list (cdr pair)) tangle))) | |
2189 | (:noweb | |
2190 | (setq noweb (funcall e-merge | |
2191 | '(("yes" "no" "tangle" "no-export" | |
2192 | "strip-export" "eval")) | |
2193 | noweb | |
2194 | (split-string (or (cdr pair) ""))))) | |
2195 | (:cache | |
2196 | (setq cache (funcall e-merge '(("yes" "no")) cache | |
2197 | (split-string (or (cdr pair) ""))))) | |
2198 | (:padline | |
2199 | (setq padline (funcall e-merge '(("yes" "no")) padline | |
afe98dfa | 2200 | (split-string (or (cdr pair) ""))))) |
8223b1d2 BG |
2201 | (:shebang ;; take the latest -- always overwrite |
2202 | (setq shebang (or (list (cdr pair)) shebang))) | |
2203 | (:comments | |
2204 | (setq comments (funcall e-merge '(("yes" "no")) comments | |
2205 | (split-string (or (cdr pair) ""))))) | |
2206 | (t ;; replace: this covers e.g. :session | |
2207 | (setq params (cons pair (assq-delete-all (car pair) params)))))) | |
2208 | plist)) | |
2209 | plists) | |
3ab2c837 | 2210 | (setq vars (reverse vars)) |
afe98dfa | 2211 | (while vars (setq params (cons (cons :var (cddr (pop vars))) params))) |
3ab2c837 BG |
2212 | (mapc |
2213 | (lambda (hd) | |
2214 | (let ((key (intern (concat ":" (symbol-name hd)))) | |
2215 | (val (eval hd))) | |
2216 | (setf params (cons (cons key (mapconcat 'identity val " ")) params)))) | |
2217 | '(results exports tangle noweb padline cache shebang comments)) | |
2218 | params)) | |
86fbb8ca | 2219 | |
e66ba1df BG |
2220 | (defvar *org-babel-use-quick-and-dirty-noweb-expansion* nil |
2221 | "Set to true to use regular expressions to expand noweb references. | |
2222 | This results in much faster noweb reference expansion but does | |
2223 | not properly allow code blocks to inherit the \":noweb-ref\" | |
2224 | header argument from buffer or subtree wide properties.") | |
2225 | ||
8223b1d2 BG |
2226 | (defun org-babel-noweb-p (params context) |
2227 | "Check if PARAMS require expansion in CONTEXT. | |
2228 | CONTEXT may be one of :tangle, :export or :eval." | |
2229 | (let* (intersect | |
2230 | (intersect (lambda (as bs) | |
2231 | (when as | |
2232 | (if (member (car as) bs) | |
2233 | (car as) | |
2234 | (funcall intersect (cdr as) bs)))))) | |
2235 | (funcall intersect (case context | |
2236 | (:tangle '("yes" "tangle" "no-export" "strip-export")) | |
2237 | (:eval '("yes" "no-export" "strip-export" "eval")) | |
2238 | (:export '("yes"))) | |
2239 | (split-string (or (cdr (assoc :noweb params)) ""))))) | |
2240 | ||
86fbb8ca CD |
2241 | (defun org-babel-expand-noweb-references (&optional info parent-buffer) |
2242 | "Expand Noweb references in the body of the current source code block. | |
2243 | ||
2244 | For example the following reference would be replaced with the | |
2245 | body of the source-code block named 'example-block'. | |
2246 | ||
2247 | <<example-block>> | |
2248 | ||
2249 | Note that any text preceding the <<foo>> construct on a line will | |
2250 | be interposed between the lines of the replacement text. So for | |
2251 | example if <<foo>> is placed behind a comment, then the entire | |
2252 | replacement text will also be commented. | |
2253 | ||
2254 | This function must be called from inside of the buffer containing | |
2255 | the source-code block which holds BODY. | |
2256 | ||
2257 | In addition the following syntax can be used to insert the | |
2258 | results of evaluating the source-code block named 'example-block'. | |
2259 | ||
2260 | <<example-block()>> | |
2261 | ||
2262 | Any optional arguments can be passed to example-block by placing | |
2263 | the arguments inside the parenthesis following the convention | |
2264 | defined by `org-babel-lob'. For example | |
2265 | ||
2266 | <<example-block(a=9)>> | |
2267 | ||
2268 | would set the value of argument \"a\" equal to \"9\". Note that | |
2269 | these arguments are not evaluated in the current source-code | |
2270 | block but are passed literally to the \"example-block\"." | |
2271 | (let* ((parent-buffer (or parent-buffer (current-buffer))) | |
2272 | (info (or info (org-babel-get-src-block-info))) | |
2273 | (lang (nth 0 info)) | |
2274 | (body (nth 1 info)) | |
8223b1d2 BG |
2275 | (ob-nww-start org-babel-noweb-wrap-start) |
2276 | (ob-nww-end org-babel-noweb-wrap-end) | |
3ab2c837 | 2277 | (comment (string= "noweb" (cdr (assoc :comments (nth 2 info))))) |
e66ba1df BG |
2278 | (rx-prefix (concat "\\(" org-babel-src-name-regexp "\\|" |
2279 | ":noweb-ref[ \t]+" "\\)")) | |
8223b1d2 BG |
2280 | (new-body "") |
2281 | (nb-add (lambda (text) (setq new-body (concat new-body text)))) | |
2282 | (c-wrap (lambda (text) | |
3ab2c837 BG |
2283 | (with-temp-buffer |
2284 | (funcall (intern (concat lang "-mode"))) | |
2285 | (comment-region (point) (progn (insert text) (point))) | |
e66ba1df | 2286 | (org-babel-trim (buffer-string))))) |
8223b1d2 BG |
2287 | index source-name evaluate prefix blocks-in-buffer) |
2288 | (with-temp-buffer | |
2289 | (org-set-local 'org-babel-noweb-wrap-start ob-nww-start) | |
2290 | (org-set-local 'org-babel-noweb-wrap-end ob-nww-end) | |
2291 | (insert body) (goto-char (point-min)) | |
2292 | (setq index (point)) | |
2293 | (while (and (re-search-forward (org-babel-noweb-wrap) nil t)) | |
2294 | (save-match-data (setf source-name (match-string 1))) | |
2295 | (save-match-data (setq evaluate (string-match "\(.*\)" source-name))) | |
2296 | (save-match-data | |
2297 | (setq prefix | |
2298 | (buffer-substring (match-beginning 0) | |
2299 | (save-excursion | |
2300 | (beginning-of-line 1) (point))))) | |
2301 | ;; add interval to new-body (removing noweb reference) | |
2302 | (goto-char (match-beginning 0)) | |
2303 | (funcall nb-add (buffer-substring index (point))) | |
2304 | (goto-char (match-end 0)) | |
2305 | (setq index (point)) | |
2306 | (funcall nb-add | |
2307 | (with-current-buffer parent-buffer | |
2308 | (save-restriction | |
2309 | (widen) | |
2310 | (mapconcat ;; interpose PREFIX between every line | |
2311 | #'identity | |
2312 | (split-string | |
2313 | (if evaluate | |
2314 | (let ((raw (org-babel-ref-resolve source-name))) | |
2315 | (if (stringp raw) raw (format "%S" raw))) | |
2316 | (or | |
2317 | ;; retrieve from the library of babel | |
2318 | (nth 2 (assoc (intern source-name) | |
2319 | org-babel-library-of-babel)) | |
2320 | ;; return the contents of headlines literally | |
2321 | (save-excursion | |
2322 | (when (org-babel-ref-goto-headline-id source-name) | |
2323 | (org-babel-ref-headline-body))) | |
2324 | ;; find the expansion of reference in this buffer | |
2325 | (let ((rx (concat rx-prefix source-name "[ \t\n]")) | |
2326 | expansion) | |
2327 | (save-excursion | |
2328 | (goto-char (point-min)) | |
2329 | (if *org-babel-use-quick-and-dirty-noweb-expansion* | |
2330 | (while (re-search-forward rx nil t) | |
2331 | (let* ((i (org-babel-get-src-block-info 'light)) | |
2332 | (body (org-babel-expand-noweb-references i)) | |
2333 | (sep (or (cdr (assoc :noweb-sep (nth 2 i))) | |
2334 | "\n")) | |
2335 | (full (if comment | |
2336 | ((lambda (cs) | |
2337 | (concat (funcall c-wrap (car cs)) "\n" | |
2338 | body "\n" | |
2339 | (funcall c-wrap (cadr cs)))) | |
2340 | (org-babel-tangle-comment-links i)) | |
2341 | body))) | |
2342 | (setq expansion (cons sep (cons full expansion))))) | |
2343 | (org-babel-map-src-blocks nil | |
2344 | (let ((i (org-babel-get-src-block-info 'light))) | |
2345 | (when (equal (or (cdr (assoc :noweb-ref (nth 2 i))) | |
2346 | (nth 4 i)) | |
2347 | source-name) | |
2348 | (let* ((body (org-babel-expand-noweb-references i)) | |
2349 | (sep (or (cdr (assoc :noweb-sep (nth 2 i))) | |
2350 | "\n")) | |
2351 | (full (if comment | |
2352 | ((lambda (cs) | |
2353 | (concat (funcall c-wrap (car cs)) "\n" | |
2354 | body "\n" | |
2355 | (funcall c-wrap (cadr cs)))) | |
2356 | (org-babel-tangle-comment-links i)) | |
2357 | body))) | |
2358 | (setq expansion | |
2359 | (cons sep (cons full expansion))))))))) | |
2360 | (and expansion | |
2361 | (mapconcat #'identity (nreverse (cdr expansion)) ""))) | |
2362 | ;; possibly raise an error if named block doesn't exist | |
2363 | (if (member lang org-babel-noweb-error-langs) | |
2364 | (error "%s" (concat | |
2365 | (org-babel-noweb-wrap source-name) | |
2366 | "could not be resolved (see " | |
2367 | "`org-babel-noweb-error-langs')")) | |
2368 | ""))) | |
2369 | "[\n\r]") (concat "\n" prefix)))))) | |
2370 | (funcall nb-add (buffer-substring index (point-max)))) | |
86fbb8ca CD |
2371 | new-body)) |
2372 | ||
3ab2c837 | 2373 | (defun org-babel-script-escape (str &optional force) |
acedf35c CD |
2374 | "Safely convert tables into elisp lists." |
2375 | (let (in-single in-double out) | |
3ab2c837 BG |
2376 | ((lambda (escaped) (condition-case nil (org-babel-read escaped) (error escaped))) |
2377 | (if (or force | |
2378 | (and (stringp str) | |
2379 | (> (length str) 2) | |
2380 | (or (and (string-equal "[" (substring str 0 1)) | |
2381 | (string-equal "]" (substring str -1))) | |
2382 | (and (string-equal "{" (substring str 0 1)) | |
2383 | (string-equal "}" (substring str -1))) | |
2384 | (and (string-equal "(" (substring str 0 1)) | |
2385 | (string-equal ")" (substring str -1)))))) | |
acedf35c CD |
2386 | (org-babel-read |
2387 | (concat | |
2388 | "'" | |
2389 | (progn | |
2390 | (mapc | |
2391 | (lambda (ch) | |
2392 | (setq | |
2393 | out | |
2394 | (case ch | |
2395 | (91 (if (or in-double in-single) ; [ | |
2396 | (cons 91 out) | |
2397 | (cons 40 out))) | |
2398 | (93 (if (or in-double in-single) ; ] | |
2399 | (cons 93 out) | |
2400 | (cons 41 out))) | |
3ab2c837 BG |
2401 | (123 (if (or in-double in-single) ; { |
2402 | (cons 123 out) | |
2403 | (cons 40 out))) | |
2404 | (125 (if (or in-double in-single) ; } | |
2405 | (cons 125 out) | |
2406 | (cons 41 out))) | |
2407 | (44 (if (or in-double in-single) ; , | |
2408 | (cons 44 out) (cons 32 out))) | |
acedf35c CD |
2409 | (39 (if in-double ; ' |
2410 | (cons 39 out) | |
2411 | (setq in-single (not in-single)) (cons 34 out))) | |
2412 | (34 (if in-single ; " | |
2413 | (append (list 34 32) out) | |
2414 | (setq in-double (not in-double)) (cons 34 out))) | |
2415 | (t (cons ch out))))) | |
2416 | (string-to-list str)) | |
2417 | (apply #'string (reverse out))))) | |
2418 | str)))) | |
2419 | ||
3ab2c837 | 2420 | (defun org-babel-read (cell &optional inhibit-lisp-eval) |
86fbb8ca CD |
2421 | "Convert the string value of CELL to a number if appropriate. |
2422 | Otherwise if cell looks like lisp (meaning it starts with a | |
3ab2c837 BG |
2423 | \"(\", \"'\", \"`\" or a \"[\") then read it as lisp, otherwise |
2424 | return it unmodified as a string. Optional argument NO-LISP-EVAL | |
2425 | inhibits lisp evaluation for situations in which is it not | |
2426 | appropriate." | |
86fbb8ca CD |
2427 | (if (and (stringp cell) (not (equal cell ""))) |
2428 | (or (org-babel-number-p cell) | |
3ab2c837 BG |
2429 | (if (and (not inhibit-lisp-eval) |
2430 | (member (substring cell 0 1) '("(" "'" "`" "["))) | |
86fbb8ca | 2431 | (eval (read cell)) |
3ab2c837 BG |
2432 | (if (string= (substring cell 0 1) "\"") |
2433 | (read cell) | |
2434 | (progn (set-text-properties 0 (length cell) nil cell) cell)))) | |
86fbb8ca CD |
2435 | cell)) |
2436 | ||
2437 | (defun org-babel-number-p (string) | |
a98edce9 | 2438 | "If STRING represents a number return its value." |
86fbb8ca CD |
2439 | (if (and (string-match "^-?[0-9]*\\.?[0-9]*$" string) |
2440 | (= (length (substring string (match-beginning 0) | |
2441 | (match-end 0))) | |
2442 | (length string))) | |
2443 | (string-to-number string))) | |
2444 | ||
afe98dfa | 2445 | (defun org-babel-import-elisp-from-file (file-name &optional separator) |
86fbb8ca CD |
2446 | "Read the results located at FILE-NAME into an elisp table. |
2447 | If the table is trivial, then return it as a scalar." | |
2448 | (let (result) | |
2449 | (save-window-excursion | |
2450 | (with-temp-buffer | |
8223b1d2 | 2451 | (condition-case err |
86fbb8ca | 2452 | (progn |
afe98dfa | 2453 | (org-table-import file-name separator) |
86fbb8ca CD |
2454 | (delete-file file-name) |
2455 | (setq result (mapcar (lambda (row) | |
2456 | (mapcar #'org-babel-string-read row)) | |
2457 | (org-table-to-lisp)))) | |
8223b1d2 | 2458 | (error (message "Error reading results: %s" err) nil))) |
86fbb8ca CD |
2459 | (if (null (cdr result)) ;; if result is trivial vector, then scalarize it |
2460 | (if (consp (car result)) | |
2461 | (if (null (cdr (car result))) | |
2462 | (caar result) | |
2463 | result) | |
2464 | (car result)) | |
2465 | result)))) | |
2466 | ||
2467 | (defun org-babel-string-read (cell) | |
2468 | "Strip nested \"s from around strings." | |
2469 | (org-babel-read (or (and (stringp cell) | |
2470 | (string-match "\\\"\\(.+\\)\\\"" cell) | |
2471 | (match-string 1 cell)) | |
8223b1d2 | 2472 | cell) t)) |
86fbb8ca CD |
2473 | |
2474 | (defun org-babel-reverse-string (string) | |
2475 | "Return the reverse of STRING." | |
2476 | (apply 'string (reverse (string-to-list string)))) | |
2477 | ||
2478 | (defun org-babel-chomp (string &optional regexp) | |
2479 | "Strip trailing spaces and carriage returns from STRING. | |
2480 | Default regexp used is \"[ \f\t\n\r\v]\" but can be | |
2481 | overwritten by specifying a regexp as a second argument." | |
2482 | (let ((regexp (or regexp "[ \f\t\n\r\v]"))) | |
2483 | (while (and (> (length string) 0) | |
2484 | (string-match regexp (substring string -1))) | |
2485 | (setq string (substring string 0 -1))) | |
2486 | string)) | |
2487 | ||
2488 | (defun org-babel-trim (string &optional regexp) | |
2489 | "Strip leading and trailing spaces and carriage returns from STRING. | |
2490 | Like `org-babel-chomp' only it runs on both the front and back | |
2491 | of the string." | |
2492 | (org-babel-chomp (org-babel-reverse-string | |
2493 | (org-babel-chomp (org-babel-reverse-string string) regexp)) | |
2494 | regexp)) | |
2495 | ||
2496 | (defvar org-babel-org-babel-call-process-region-original nil) | |
2497 | (defun org-babel-tramp-handle-call-process-region | |
2498 | (start end program &optional delete buffer display &rest args) | |
8223b1d2 | 2499 | "Use Tramp to handle `call-process-region'. |
86fbb8ca CD |
2500 | Fixes a bug in `tramp-handle-call-process-region'." |
2501 | (if (and (featurep 'tramp) (file-remote-p default-directory)) | |
2502 | (let ((tmpfile (tramp-compat-make-temp-file ""))) | |
2503 | (write-region start end tmpfile) | |
2504 | (when delete (delete-region start end)) | |
2505 | (unwind-protect | |
2506 | ;; (apply 'call-process program tmpfile buffer display args) | |
2507 | ;; bug in tramp | |
2508 | (apply 'process-file program tmpfile buffer display args) | |
2509 | (delete-file tmpfile))) | |
afe98dfa | 2510 | ;; org-babel-call-process-region-original is the original emacs |
8223b1d2 | 2511 | ;; definition. It is in scope from the let binding in |
afe98dfa | 2512 | ;; org-babel-execute-src-block |
86fbb8ca CD |
2513 | (apply org-babel-call-process-region-original |
2514 | start end program delete buffer display args))) | |
2515 | ||
afe98dfa CD |
2516 | (defun org-babel-local-file-name (file) |
2517 | "Return the local name component of FILE." | |
2518 | (if (file-remote-p file) | |
2519 | (let (localname) | |
2520 | (with-parsed-tramp-file-name file nil | |
8223b1d2 | 2521 | localname)) |
86fbb8ca CD |
2522 | file)) |
2523 | ||
afe98dfa CD |
2524 | (defun org-babel-process-file-name (name &optional no-quote-p) |
2525 | "Prepare NAME to be used in an external process. | |
2526 | If NAME specifies a remote location, the remote portion of the | |
2527 | name is removed, since in that case the process will be executing | |
8223b1d2 BG |
2528 | remotely. The file name is then processed by `expand-file-name'. |
2529 | Unless second argument NO-QUOTE-P is non-nil, the file name is | |
2530 | additionally processed by `shell-quote-argument'" | |
afe98dfa CD |
2531 | ((lambda (f) (if no-quote-p f (shell-quote-argument f))) |
2532 | (expand-file-name (org-babel-local-file-name name)))) | |
2533 | ||
2534 | (defvar org-babel-temporary-directory) | |
2535 | (unless (or noninteractive (boundp 'org-babel-temporary-directory)) | |
2536 | (defvar org-babel-temporary-directory | |
2537 | (or (and (boundp 'org-babel-temporary-directory) | |
2538 | (file-exists-p org-babel-temporary-directory) | |
2539 | org-babel-temporary-directory) | |
2540 | (make-temp-file "babel-" t)) | |
2541 | "Directory to hold temporary files created to execute code blocks. | |
2542 | Used by `org-babel-temp-file'. This directory will be removed on | |
2543 | Emacs shutdown.")) | |
2544 | ||
2545 | (defun org-babel-temp-file (prefix &optional suffix) | |
2546 | "Create a temporary file in the `org-babel-temporary-directory'. | |
2547 | Passes PREFIX and SUFFIX directly to `make-temp-file' with the | |
2548 | value of `temporary-file-directory' temporarily set to the value | |
2549 | of `org-babel-temporary-directory'." | |
006faa41 MA |
2550 | (let ((temporary-file-directory |
2551 | (if (file-remote-p default-directory) | |
2552 | (concat (file-remote-p default-directory) "/tmp") | |
3ab2c837 BG |
2553 | (or (and (boundp 'org-babel-temporary-directory) |
2554 | (file-exists-p org-babel-temporary-directory) | |
afe98dfa | 2555 | org-babel-temporary-directory) |
006faa41 MA |
2556 | temporary-file-directory)))) |
2557 | (make-temp-file prefix nil suffix))) | |
afe98dfa CD |
2558 | |
2559 | (defun org-babel-remove-temporary-directory () | |
2560 | "Remove `org-babel-temporary-directory' on Emacs shutdown." | |
2561 | (when (and (boundp 'org-babel-temporary-directory) | |
2562 | (file-exists-p org-babel-temporary-directory)) | |
2563 | ;; taken from `delete-directory' in files.el | |
acedf35c CD |
2564 | (condition-case nil |
2565 | (progn | |
2566 | (mapc (lambda (file) | |
2567 | ;; This test is equivalent to | |
2568 | ;; (and (file-directory-p fn) (not (file-symlink-p fn))) | |
2569 | ;; but more efficient | |
2570 | (if (eq t (car (file-attributes file))) | |
2571 | (delete-directory file) | |
2572 | (delete-file file))) | |
2573 | ;; We do not want to delete "." and "..". | |
2574 | (directory-files org-babel-temporary-directory 'full | |
2575 | "^\\([^.]\\|\\.\\([^.]\\|\\..\\)\\).*")) | |
2576 | (delete-directory org-babel-temporary-directory)) | |
2577 | (error | |
2578 | (message "Failed to remove temporary Org-babel directory %s" | |
3ab2c837 BG |
2579 | (if (boundp 'org-babel-temporary-directory) |
2580 | org-babel-temporary-directory | |
2581 | "[directory not defined]")))))) | |
afe98dfa CD |
2582 | |
2583 | (add-hook 'kill-emacs-hook 'org-babel-remove-temporary-directory) | |
2584 | ||
86fbb8ca CD |
2585 | (provide 'ob) |
2586 | ||
bdebdb64 BG |
2587 | ;; Local variables: |
2588 | ;; generated-autoload-file: "org-loaddefs.el" | |
2589 | ;; End: | |
86fbb8ca CD |
2590 | |
2591 | ;;; ob.el ends here |