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