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