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