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