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