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