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