Release 7.01e
[org-mode/org-tableheadings.git] / lisp / ob.el
blob63d83c7e418dd80965b2fa9a7c287a647c9f1f7b
1 ;;; ob.el --- working with code blocks in org-mode
3 ;; Copyright (C) 2009, 2010 Free Software Foundation, Inc.
5 ;; Author: Eric Schulte, Dan Davison
6 ;; Keywords: literate programming, reproducible research
7 ;; Homepage: http://orgmode.org
8 ;; Version: 7.01e
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;; See the online documentation for more information
28 ;;
29 ;; http://orgmode.org/worg/org-contrib/babel/
31 ;;; Code:
32 (eval-when-compile (require 'cl))
33 (require 'org-macs)
35 (defvar org-babel-call-process-region-original)
36 (declare-function show-all "outline" ())
37 (declare-function tramp-compat-make-temp-file "tramp" (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 org-icompleting-read "org" (&rest args))
42 (declare-function org-edit-src-code "org" (context code edit-buffer-name))
43 (declare-function org-open-at-point "org" (&optional in-emacs reference-buffer))
44 (declare-function org-save-outline-visibility "org" (use-markers &rest body))
45 (declare-function org-narrow-to-subtree "org" ())
46 (declare-function org-entry-get "org" (pom property &optional inherit))
47 (declare-function org-make-options-regexp "org" (kwds &optional extra))
48 (declare-function org-match-string-no-properties "org" (num &optional string))
49 (declare-function org-do-remove-indentation "org" (&optional n))
50 (declare-function org-show-context "org" (&optional key))
51 (declare-function org-at-table-p "org" (&optional table-type))
52 (declare-function org-cycle "org" (&optional arg))
53 (declare-function org-uniquify "org" (list))
54 (declare-function org-table-import "org" (file arg))
55 (declare-function org-add-hook "org-compat" (hook function &optional append local))
56 (declare-function org-table-align "org-table" ())
57 (declare-function org-table-end "org-table" (&optional table-type))
58 (declare-function orgtbl-to-generic "org-table" (table params))
59 (declare-function orgtbl-to-orgtbl "org-table" (table params))
60 (declare-function org-babel-lob-get-info "ob-lob" nil)
61 (declare-function org-babel-ref-split-args "ob-ref" (arg-string))
62 (declare-function org-babel-ref-variables "ob-ref" (params))
63 (declare-function org-babel-ref-resolve-reference "ob-ref" (ref &optional params))
65 (defgroup org-babel nil
66 "Code block evaluation and management in `org-mode' documents."
67 :tag "Babel"
68 :group 'org)
70 (defcustom org-confirm-babel-evaluate t
71 "Confirm before evaluation.
72 Require confirmation before interactively evaluating code
73 blocks in Org-mode buffers. The default value of this variable
74 is t, meaning confirmation is required for any code block
75 evaluation. This variable can be set to nil to inhibit any
76 future confirmation requests. This variable can also be set to a
77 function which takes two arguments the language of the code block
78 and the body of the code block. Such a function should then
79 return a non-nil value if the user should be prompted for
80 execution or nil if no prompt is required.
82 Warning: Disabling confirmation may result in accidental
83 evaluation of potentially harmful code. It may be advisable
84 remove code block execution from C-c C-c as further protection
85 against accidental code block evaluation. The
86 `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can be used to
87 remove code block execution from the C-c C-c keybinding."
88 :group 'org-babel
89 :type '(choice boolean function))
90 ;; don't allow this variable to be changed through file settings
91 (put 'org-confirm-babel-evaluate 'safe-local-variable (lambda (x) (eq x t)))
93 (defcustom org-babel-no-eval-on-ctrl-c-ctrl-c nil
94 "Remove code block evaluation from the C-c C-c key binding."
95 :group 'org-babel
96 :type 'boolean)
98 (defvar org-babel-src-name-regexp
99 "^[ \t]*#\\+\\(srcname\\|source\\|function\\):[ \t]*"
100 "Regular expression used to match a source name line.")
102 (defvar org-babel-src-name-w-name-regexp
103 (concat org-babel-src-name-regexp
104 "\\([^ ()\f\t\n\r\v]+\\)\\(\(\\(.*\\)\)\\|\\)")
105 "Regular expression matching source name lines with a name.")
107 (defvar org-babel-src-block-regexp
108 (concat
109 ;; (1) indentation (2) lang
110 "^\\([ \t]*\\)#\\+begin_src[ \t]+\\([^ \f\t\n\r\v]+\\)[ \t]*"
111 ;; (3) switches
112 "\\([^\":\n]*\"[^\"\n*]*\"[^\":\n]*\\|[^\":\n]*\\)"
113 ;; (4) header arguments
114 "\\([^\n]*\\)\n"
115 ;; (5) body
116 "\\([^\000]+?\n\\)[ \t]*#\\+end_src")
117 "Regexp used to identify code blocks.")
119 (defvar org-babel-inline-src-block-regexp
120 (concat
121 ;; (1) replacement target (2) lang
122 "[ \f\t\n\r\v]\\(src_\\([^ \f\t\n\r\v]+\\)"
123 ;; (3,4) (unused, headers)
124 "\\(\\|\\[\\(.*?\\)\\]\\)"
125 ;; (5) body
126 "{\\([^\f\n\r\v]+?\\)}\\)")
127 "Regexp used to identify inline src-blocks.")
129 (defun org-babel-get-src-block-info (&optional header-vars-only)
130 "Get information on the current source block.
132 Returns a list
133 (language body header-arguments-alist switches name function-args indent).
134 Unless HEADER-VARS-ONLY is non-nil, any variable
135 references provided in 'function call style' (i.e. in a
136 parenthesised argument list following the src block name) are
137 added to the header-arguments-alist."
138 (let ((case-fold-search t) head info args indent)
139 (if (setq head (org-babel-where-is-src-block-head))
140 (save-excursion
141 (goto-char head)
142 (setq info (org-babel-parse-src-block-match))
143 (setq indent (car (last info)))
144 (setq info (butlast info))
145 (forward-line -1)
146 (if (and (looking-at org-babel-src-name-w-name-regexp)
147 (match-string 2))
148 (progn
149 (setq info (append info (list (org-babel-clean-text-properties
150 (match-string 2)))))
151 ;; Note that e.g. "name()" and "name( )" result in
152 ;; ((:var . "")). We maintain that behaviour, and the
153 ;; resulting non-nil sixth element is relied upon in
154 ;; org-babel-exp-code to detect a functional-style
155 ;; block in those cases. However, "name" without any
156 ;; parentheses would result in the same thing, so we
157 ;; explicitly avoid that.
158 (if (setq args (match-string 4))
159 (setq info
160 (append info (list
161 (mapcar
162 (lambda (ref) (cons :var ref))
163 (org-babel-ref-split-args args))))))
164 (unless header-vars-only
165 (setf (nth 2 info)
166 (org-babel-merge-params (nth 5 info) (nth 2 info)))))
167 (setq info (append info (list nil nil))))
168 (append info (list indent)))
169 (if (save-excursion ;; inline source block
170 (re-search-backward "[ \f\t\n\r\v]" nil t)
171 (looking-at org-babel-inline-src-block-regexp))
172 (org-babel-parse-inline-src-block-match)
173 nil))))
175 (defun org-babel-confirm-evaluate (info)
176 "Confirm evaluation of the code block INFO.
177 This behavior can be suppressed by setting the value of
178 `org-confirm-babel-evaluate' to nil, in which case all future
179 interactive code block evaluations will proceed without any
180 confirmation from the user.
182 Note disabling confirmation may result in accidental evaluation
183 of potentially harmful code."
184 (let* ((eval (cdr (assoc :eval (nth 2 info))))
185 (query (or (equal eval "query")
186 (and (functionp org-confirm-babel-evaluate)
187 (funcall org-confirm-babel-evaluate
188 (nth 0 info) (nth 1 info)))
189 org-confirm-babel-evaluate)))
190 (when (or (equal eval "never")
191 (and query
192 (not (yes-or-no-p
193 (format "Evaluate this%scode on your system? "
194 (if info (format " %s " (nth 0 info)) " "))))))
195 (error "evaluation aborted"))))
197 ;;;###autoload
198 (defun org-babel-execute-src-block-maybe ()
199 "Conditionally execute a source block.
200 Detect if this is context for a Babel src-block and if so
201 then run `org-babel-execute-src-block'."
202 (interactive)
203 (if (not org-babel-no-eval-on-ctrl-c-ctrl-c)
204 (let ((info (org-babel-get-src-block-info)))
205 (if info
206 (progn (org-babel-execute-src-block current-prefix-arg info) t) nil))
207 nil))
208 (add-hook 'org-ctrl-c-ctrl-c-hook 'org-babel-execute-src-block-maybe)
210 ;;;###autoload
211 (defun org-babel-expand-src-block-maybe ()
212 "Conditionally expand a source block.
213 Detect if this is context for a org-babel src-block and if so
214 then run `org-babel-expand-src-block'."
215 (interactive)
216 (let ((info (org-babel-get-src-block-info)))
217 (if info
218 (progn (org-babel-expand-src-block current-prefix-arg info) t)
219 nil)))
221 ;;;###autoload
222 (defun org-babel-load-in-session-maybe ()
223 "Conditionally load a source block in a session.
224 Detect if this is context for a org-babel src-block and if so
225 then run `org-babel-load-in-session'."
226 (interactive)
227 (let ((info (org-babel-get-src-block-info)))
228 (if info
229 (progn (org-babel-load-in-session current-prefix-arg info) t)
230 nil)))
232 (add-hook 'org-metaup-hook 'org-babel-load-in-session-maybe)
234 ;;;###autoload
235 (defun org-babel-pop-to-session-maybe ()
236 "Conditionally pop to a session.
237 Detect if this is context for a org-babel src-block and if so
238 then run `org-babel-pop-to-session'."
239 (interactive)
240 (let ((info (org-babel-get-src-block-info)))
241 (if info (progn (org-babel-pop-to-session current-prefix-arg info) t) nil)))
243 (add-hook 'org-metadown-hook 'org-babel-pop-to-session-maybe)
245 (defconst org-babel-header-arg-names
246 '(cache cmdline colnames dir exports file noweb results
247 session tangle var noeval comments)
248 "Common header arguments used by org-babel.
249 Note that individual languages may define their own language
250 specific header arguments as well.")
252 (defvar org-babel-default-header-args
253 '((:session . "none") (:results . "replace") (:exports . "code")
254 (:cache . "no") (:noweb . "no") (:hlines . "no") (:tangle . "no"))
255 "Default arguments to use when evaluating a source block.")
257 (defvar org-babel-default-inline-header-args
258 '((:session . "none") (:results . "silent") (:exports . "results"))
259 "Default arguments to use when evaluating an inline source block.")
261 (defvar org-babel-current-buffer-properties)
262 (make-variable-buffer-local 'org-babel-current-buffer-properties)
264 (defvar org-babel-result-regexp
265 "^[ \t]*#\\+res\\(ults\\|name\\)\\(\\[\\([[:alnum:]]+\\)\\]\\)?\\:[ \t]*"
266 "Regular expression used to match result lines.
267 If the results are associated with a hash key then the hash will
268 be saved in the second match data.")
270 (defvar org-babel-result-w-name-regexp
271 (concat org-babel-result-regexp
272 "\\([^ ()\f\t\n\r\v]+\\)\\(\(\\(.*\\)\)\\|\\)"))
274 (defvar org-babel-min-lines-for-block-output 10
275 "The minimum number of lines for block output.
276 If number of lines of output is equal to or exceeds this
277 value, the output is placed in a #+begin_example...#+end_example
278 block. Otherwise the output is marked as literal by inserting
279 colons at the starts of the lines. This variable only takes
280 effect if the :results output option is in effect.")
282 (defvar org-babel-noweb-error-langs nil
283 "Languages for which Babel will raise literate programming errors.
284 List of languages for which errors should be raised when the
285 source code block satisfying a noweb reference in this language
286 can not be resolved.")
288 (defvar org-babel-hash-show 4
289 "Number of initial characters to show of a hidden results hash.")
291 (defvar org-babel-after-execute-hook nil
292 "Hook for functions to be called after `org-babel-execute-src-block'")
293 (defun org-babel-named-src-block-regexp-for-name (name)
294 "This generates a regexp used to match a src block named NAME."
295 (concat org-babel-src-name-regexp (regexp-quote name) "[ \t\n]*"
296 (substring org-babel-src-block-regexp 1)))
298 ;;; functions
299 (defvar call-process-region)
300 ;;;###autoload
301 (defun org-babel-execute-src-block (&optional arg info params)
302 "Execute the current source code block.
303 Insert the results of execution into the buffer. Source code
304 execution and the collection and formatting of results can be
305 controlled through a variety of header arguments.
307 Optionally supply a value for INFO in the form returned by
308 `org-babel-get-src-block-info'.
310 Optionally supply a value for PARAMS which will be merged with
311 the header arguments specified at the front of the source code
312 block."
313 (interactive)
314 (let* ((info (or info (org-babel-get-src-block-info)))
315 ;; note the `evaluation-confirmed' variable is currently not
316 ;; used, but could be used later to avoid the need for
317 ;; chaining confirmations
318 (evaluation-confirmed (org-babel-confirm-evaluate info))
319 (lang (nth 0 info))
320 (params (setf (nth 2 info)
321 (sort (org-babel-merge-params (nth 2 info) params)
322 (lambda (el1 el2) (string< (symbol-name (car el1))
323 (symbol-name (car el2)))))))
324 (new-hash
325 (if (and (cdr (assoc :cache params))
326 (string= "yes" (cdr (assoc :cache params))))
327 (org-babel-sha1-hash info)))
328 (old-hash (org-babel-result-hash info))
329 (body (setf (nth 1 info)
330 (if (and (cdr (assoc :noweb params))
331 (string= "yes" (cdr (assoc :noweb params))))
332 (org-babel-expand-noweb-references info)
333 (nth 1 info))))
334 (result-params (split-string (or (cdr (assoc :results params)) "")))
335 (result-type (cond ((member "output" result-params) 'output)
336 ((member "value" result-params) 'value)
337 (t 'value)))
338 (cmd (intern (concat "org-babel-execute:" lang)))
339 (dir (cdr (assoc :dir params)))
340 (default-directory
341 (or (and dir (file-name-as-directory dir)) default-directory))
342 (org-babel-call-process-region-original
343 (if (boundp 'org-babel-call-process-region-original) org-babel-call-process-region-original
344 (symbol-function 'call-process-region)))
345 (indent (car (last info)))
346 result)
347 (unwind-protect
348 (flet ((call-process-region (&rest args)
349 (apply 'org-babel-tramp-handle-call-process-region args)))
350 (unless (fboundp cmd)
351 (error "No org-babel-execute function for %s!" lang))
352 (if (and (not arg) new-hash (equal new-hash old-hash))
353 (save-excursion ;; return cached result
354 (goto-char (org-babel-where-is-src-block-result nil info))
355 (end-of-line 1) (forward-char 1)
356 (setq result (org-babel-read-result))
357 (message (replace-regexp-in-string "%" "%%"
358 (format "%S" result))) result)
359 (message "executing %s code block%s..."
360 (capitalize lang)
361 (if (nth 4 info) (format " (%s)" (nth 4 info)) ""))
362 (setq result (funcall cmd body params))
363 (if (eq result-type 'value)
364 (setq result (if (and (or (member "vector" result-params)
365 (member "table" result-params))
366 (not (listp result)))
367 (list (list result))
368 result)))
369 (org-babel-insert-result
370 result result-params info new-hash indent lang)
371 (run-hooks 'org-babel-after-execute-hook)
372 result))
373 (setq call-process-region 'org-babel-call-process-region-original))))
375 (defun org-babel-expand-body:generic (body params &optional processed-params)
376 "Expand BODY with PARAMS.
377 Expand a block of code with org-babel according to it's header
378 arguments. This generic implementation of body expansion is
379 called for languages which have not defined their own specific
380 org-babel-expand-body:lang function." body)
382 ;;;###autoload
383 (defun org-babel-expand-src-block (&optional arg info params)
384 "Expand the current source code block.
385 Expand according to the source code block's header
386 arguments and pop open the results in a preview buffer."
387 (interactive)
388 (let* ((info (or info (org-babel-get-src-block-info)))
389 (lang (nth 0 info))
390 (params (setf (nth 2 info)
391 (sort (org-babel-merge-params (nth 2 info) params)
392 (lambda (el1 el2) (string< (symbol-name (car el1))
393 (symbol-name (car el2)))))))
394 (body (setf (nth 1 info)
395 (if (and (cdr (assoc :noweb params))
396 (string= "yes" (cdr (assoc :noweb params))))
397 (org-babel-expand-noweb-references info) (nth 1 info))))
398 (cmd (intern (concat "org-babel-expand-body:" lang)))
399 (expanded (funcall (if (fboundp cmd) cmd 'org-babel-expand-body:generic)
400 body params)))
401 (org-edit-src-code
402 nil expanded (concat "*Org-Babel Preview " (buffer-name) "[ " lang " ]*"))))
404 ;;;###autoload
405 (defun org-babel-load-in-session (&optional arg info)
406 "Load the body of the current source-code block.
407 Evaluate the header arguments for the source block before
408 entering the session. After loading the body this pops open the
409 session."
410 (interactive)
411 (let* ((info (or info (org-babel-get-src-block-info)))
412 (lang (nth 0 info))
413 (body (nth 1 info))
414 (params (nth 2 info))
415 (session (cdr (assoc :session params)))
416 (cmd (intern (concat "org-babel-load-session:" lang))))
417 (unless (fboundp cmd)
418 (error "No org-babel-load-session function for %s!" lang))
419 (pop-to-buffer (funcall cmd session body params))
420 (end-of-line 1)))
422 ;;;###autoload
423 (defun org-babel-switch-to-session (&optional arg info)
424 "Switch to the session of the current source-code block.
425 If called with a prefix argument then evaluate the header arguments
426 for the source block before entering the session. Copy the body
427 of the source block to the kill ring."
428 (interactive)
429 (let* ((info (or info (org-babel-get-src-block-info)))
430 (lang (nth 0 info))
431 (body (nth 1 info))
432 (params (nth 2 info))
433 (session (cdr (assoc :session params)))
434 (dir (cdr (assoc :dir params)))
435 (default-directory
436 (or (and dir (file-name-as-directory dir)) default-directory))
437 (cmd (intern (format "org-babel-%s-initiate-session" lang)))
438 (cmd2 (intern (concat "org-babel-prep-session:" lang))))
439 (unless (fboundp cmd)
440 (error "No org-babel-initiate-session function for %s!" lang))
441 ;; copy body to the kill ring
442 (with-temp-buffer (insert (org-babel-trim body))
443 (copy-region-as-kill (point-min) (point-max)))
444 ;; if called with a prefix argument, then process header arguments
445 (unless (fboundp cmd2)
446 (error "No org-babel-prep-session function for %s!" lang))
447 (when arg (funcall cmd2 session params))
448 ;; just to the session using pop-to-buffer
449 (pop-to-buffer (funcall cmd session params))
450 (end-of-line 1)))
452 (defalias 'org-babel-pop-to-session 'org-babel-switch-to-session)
454 (defvar org-bracket-link-regexp)
455 ;;;###autoload
456 (defun org-babel-open-src-block-result (&optional re-run)
457 "If `point' is on a src block then open the results of the
458 source code block, otherwise return nil. With optional prefix
459 argument RE-RUN the source-code block is evaluated even if
460 results already exist."
461 (interactive "P")
462 (when (org-babel-get-src-block-info)
463 (save-excursion
464 ;; go to the results, if there aren't any then run the block
465 (goto-char (or (and (not re-run) (org-babel-where-is-src-block-result))
466 (progn (org-babel-execute-src-block)
467 (org-babel-where-is-src-block-result))))
468 (end-of-line 1)
469 (while (looking-at "[\n\r\t\f ]") (forward-char 1))
470 ;; open the results
471 (if (looking-at org-bracket-link-regexp)
472 ;; file results
473 (org-open-at-point)
474 (let ((results (org-babel-read-result)))
475 (flet ((echo-res (result)
476 (if (stringp result) result (format "%S" result))))
477 (pop-to-buffer (get-buffer-create "org-babel-results"))
478 (delete-region (point-min) (point-max))
479 (if (listp results)
480 ;; table result
481 (insert (orgtbl-to-generic results '(:sep "\t" :fmt echo-res)))
482 ;; scalar result
483 (insert (echo-res results))))))
484 t)))
486 ;;;###autoload
487 (defun org-babel-execute-buffer (&optional arg)
488 "Execute source code blocks in a buffer.
489 Call `org-babel-execute-src-block' on every source block in
490 the current buffer."
491 (interactive "P")
492 (save-excursion
493 (org-save-outline-visibility t
494 (goto-char (point-min))
495 (show-all)
496 (while (re-search-forward org-babel-src-block-regexp nil t)
497 (let ((pos-end (match-end 0)))
498 (goto-char (match-beginning 0))
499 (org-babel-execute-src-block arg)
500 (goto-char pos-end))))))
502 ;;;###autoload
503 (defun org-babel-execute-subtree (&optional arg)
504 "Execute source code blocks in a subtree.
505 Call `org-babel-execute-src-block' on every source block in
506 the current subtree."
507 (interactive "P")
508 (save-restriction
509 (save-excursion
510 (org-narrow-to-subtree)
511 (org-babel-execute-buffer)
512 (widen))))
514 ;;;###autoload
515 (defun org-babel-sha1-hash (&optional info)
516 "Generate an sha1 hash based on the value of info."
517 (interactive)
518 (let* ((info (or info (org-babel-get-src-block-info)))
519 (hash (sha1 (format "%s-%s" (mapconcat (lambda (arg) (format "%S" arg))
520 (nth 2 info) ":")
521 (nth 1 info)))))
522 (when (interactive-p) (message hash))
523 hash))
525 (defun org-babel-result-hash (&optional info)
526 "Return the in-buffer hash associated with INFO."
527 (org-babel-where-is-src-block-result nil info)
528 (org-babel-clean-text-properties (match-string 3)))
530 (defun org-babel-hide-hash ()
531 "Hide the hash in the current results line.
532 Only the initial `org-babel-hash-show' characters of the hash
533 will remain visible."
534 (add-to-invisibility-spec '(org-babel-hide-hash . t))
535 (save-excursion
536 (when (and (re-search-forward org-babel-result-regexp nil t)
537 (match-string 3))
538 (let* ((start (match-beginning 3))
539 (hide-start (+ org-babel-hash-show start))
540 (end (match-end 3))
541 (hash (match-string 3))
542 ov1 ov2)
543 (setq ov1 (make-overlay start hide-start))
544 (setq ov2 (make-overlay hide-start end))
545 (overlay-put ov2 'invisible 'org-babel-hide-hash)
546 (overlay-put ov1 'babel-hash hash)))))
548 (defun org-babel-hide-all-hashes ()
549 "Hide the hash in the current buffer.
550 Only the initial `org-babel-hash-show' characters of each hash
551 will remain visible. This function should be called as part of
552 the `org-mode-hook'."
553 (save-excursion
554 (while (re-search-forward org-babel-result-regexp nil t)
555 (goto-char (match-beginning 0))
556 (org-babel-hide-hash)
557 (goto-char (match-end 0)))))
558 (add-hook 'org-mode-hook 'org-babel-hide-all-hashes)
560 (defun org-babel-hash-at-point (&optional point)
561 "Return the value of the hash at POINT.
562 The hash is also added as the last element of the kill ring.
563 This can be called with C-c C-c."
564 (interactive)
565 (let ((hash (car (delq nil (mapcar
566 (lambda (ol) (overlay-get ol 'babel-hash))
567 (overlays-at (or point (point))))))))
568 (when hash (kill-new hash) (message hash))))
569 (add-hook 'org-ctrl-c-ctrl-c-hook 'org-babel-hash-at-point)
571 (defun org-babel-result-hide-spec ()
572 "Hide portions of results lines.
573 Add `org-babel-hide-result' as an invisibility spec for hiding
574 portions of results lines."
575 (add-to-invisibility-spec '(org-babel-hide-result . t)))
576 (add-hook 'org-mode-hook 'org-babel-result-hide-spec)
578 (defvar org-babel-hide-result-overlays nil
579 "Overlays hiding results.")
581 (defun org-babel-result-hide-all ()
582 "Fold all results in the current buffer."
583 (interactive)
584 (org-babel-show-result-all)
585 (save-excursion
586 (while (re-search-forward org-babel-result-regexp nil t)
587 (save-excursion (goto-char (match-beginning 0))
588 (org-babel-hide-result-toggle-maybe)))))
590 (defun org-babel-show-result-all ()
591 "Unfold all results in the current buffer."
592 (mapc 'delete-overlay org-babel-hide-result-overlays)
593 (setq org-babel-hide-result-overlays nil))
595 ;;;###autoload
596 (defun org-babel-hide-result-toggle-maybe ()
597 "Toggle visibility of result at point."
598 (interactive)
599 (let ((case-fold-search t))
600 (if (save-excursion
601 (beginning-of-line 1)
602 (looking-at org-babel-result-regexp))
603 (progn (org-babel-hide-result-toggle)
604 t) ;; to signal that we took action
605 nil))) ;; to signal that we did not
607 (defun org-babel-hide-result-toggle (&optional force)
608 "Toggle the visibility of the current result."
609 (interactive)
610 (save-excursion
611 (beginning-of-line)
612 (if (re-search-forward org-babel-result-regexp nil t)
613 (let ((start (progn (beginning-of-line 2) (- (point) 1)))
614 (end (progn (goto-char (- (org-babel-result-end) 1)) (point)))
616 (if (memq t (mapcar (lambda (overlay)
617 (eq (overlay-get overlay 'invisible)
618 'org-babel-hide-result))
619 (overlays-at start)))
620 (if (or (not force) (eq force 'off))
621 (mapc (lambda (ov)
622 (when (member ov org-babel-hide-result-overlays)
623 (setq org-babel-hide-result-overlays
624 (delq ov org-babel-hide-result-overlays)))
625 (when (eq (overlay-get ov 'invisible)
626 'org-babel-hide-result)
627 (delete-overlay ov)))
628 (overlays-at start)))
629 (setq ov (make-overlay start end))
630 (overlay-put ov 'invisible 'org-babel-hide-result)
631 ;; make the block accessible to isearch
632 (overlay-put
633 ov 'isearch-open-invisible
634 (lambda (ov)
635 (when (member ov org-babel-hide-result-overlays)
636 (setq org-babel-hide-result-overlays
637 (delq ov org-babel-hide-result-overlays)))
638 (when (eq (overlay-get ov 'invisible)
639 'org-babel-hide-result)
640 (delete-overlay ov))))
641 (push ov org-babel-hide-result-overlays)))
642 (error "Not looking at a result line"))))
644 ;; org-tab-after-check-for-cycling-hook
645 (add-hook 'org-tab-first-hook 'org-babel-hide-result-toggle-maybe)
646 ;; Remove overlays when changing major mode
647 (add-hook 'org-mode-hook
648 (lambda () (org-add-hook 'change-major-mode-hook
649 'org-babel-show-result-all 'append 'local)))
651 (defmacro org-babel-map-src-blocks (file &rest body)
652 "Evaluate BODY forms on each source-block in FILE."
653 (declare (indent 1))
654 `(let ((visited-p (get-file-buffer (expand-file-name ,file)))
655 to-be-removed)
656 (save-window-excursion
657 (find-file ,file)
658 (setq to-be-removed (current-buffer))
659 (goto-char (point-min))
660 (while (re-search-forward org-babel-src-block-regexp nil t)
661 (goto-char (match-beginning 0))
662 (save-match-data ,@body)
663 (goto-char (match-end 0))))
664 (unless visited-p
665 (kill-buffer to-be-removed))))
667 (defvar org-file-properties)
668 (defun org-babel-params-from-properties (&optional lang)
669 "Retrieve parameters specified as properties.
670 Return an association list of any source block params which
671 may be specified in the properties of the current outline entry."
672 (save-match-data
673 (let (val sym)
674 (delq nil
675 (mapcar
676 (lambda (header-arg)
677 (and (setq val
678 (or (condition-case nil
679 (org-entry-get (point) header-arg t)
680 (error nil))
681 (cdr (assoc header-arg org-file-properties))))
682 (cons (intern (concat ":" header-arg)) val)))
683 (mapcar
684 'symbol-name
685 (append
686 org-babel-header-arg-names
687 (progn
688 (setq sym (intern (concat "org-babel-header-arg-names:" lang)))
689 (and (boundp sym) (eval sym))))))))))
691 (defun org-babel-params-from-buffer ()
692 "Retrieve per-buffer parameters.
693 Return an association list of any source block params which
694 may be specified at the top of the current buffer."
695 (or org-babel-current-buffer-properties
696 (setq org-babel-current-buffer-properties
697 (save-match-data
698 (save-excursion
699 (save-restriction
700 (widen)
701 (goto-char (point-min))
702 (when (re-search-forward
703 (org-make-options-regexp (list "BABEL")) nil t)
704 (org-babel-parse-header-arguments
705 (org-match-string-no-properties 2)))))))))
707 (defvar org-src-preserve-indentation)
708 (defun org-babel-parse-src-block-match ()
709 "Parse the results from a match of the `org-babel-src-block-regexp'."
710 (let* ((block-indentation (length (match-string 1)))
711 (lang (org-babel-clean-text-properties (match-string 2)))
712 (lang-headers (intern (concat "org-babel-default-header-args:" lang)))
713 (switches (match-string 3))
714 (body (org-babel-clean-text-properties (match-string 5)))
715 (preserve-indentation (or org-src-preserve-indentation
716 (string-match "-i\\>" switches))))
717 (list lang
718 ;; get block body less properties, protective commas, and indentation
719 (with-temp-buffer
720 (save-match-data
721 (insert (org-babel-strip-protective-commas body))
722 (unless preserve-indentation (org-do-remove-indentation))
723 (buffer-string)))
724 (org-babel-merge-params
725 org-babel-default-header-args
726 (org-babel-params-from-buffer)
727 (org-babel-params-from-properties lang)
728 (if (boundp lang-headers) (eval lang-headers) nil)
729 (org-babel-parse-header-arguments
730 (org-babel-clean-text-properties (or (match-string 4) ""))))
731 switches
732 block-indentation)))
734 (defun org-babel-parse-inline-src-block-match ()
735 "Parse the results from a match of the `org-babel-inline-src-block-regexp'."
736 (let* ((lang (org-babel-clean-text-properties (match-string 2)))
737 (lang-headers (intern (concat "org-babel-default-header-args:" lang))))
738 (list lang
739 (org-babel-strip-protective-commas
740 (org-babel-clean-text-properties (match-string 5)))
741 (org-babel-merge-params
742 org-babel-default-inline-header-args
743 (org-babel-params-from-buffer)
744 (org-babel-params-from-properties lang)
745 (if (boundp lang-headers) (eval lang-headers) nil)
746 (org-babel-parse-header-arguments
747 (org-babel-clean-text-properties (or (match-string 4) "")))))))
749 (defun org-babel-parse-header-arguments (arg-string)
750 "Parse a string of header arguments returning an alist."
751 (if (> (length arg-string) 0)
752 (delq nil
753 (mapcar
754 (lambda (arg)
755 (if (string-match
756 "\\([^ \f\t\n\r\v]+\\)[ \f\t\n\r\v]+\\([^ \f\t\n\r\v]+.*\\)"
757 arg)
758 (cons (intern (concat ":" (match-string 1 arg)))
759 (let ((raw (org-babel-chomp (match-string 2 arg))))
760 (if (org-babel-number-p raw)
761 raw (org-babel-read raw))))
762 (cons (intern (concat ":" arg)) nil)))
763 (split-string (concat " " arg-string) "[ \f\t\n\r\v]+:" t)))))
765 (defun org-babel-process-params (params)
766 "Parse params and resolve references.
768 Return a list (session vars result-params result-type colnames rownames)."
769 (let* ((session (cdr (assoc :session params)))
770 (vars-and-names (org-babel-disassemble-tables
771 (org-babel-ref-variables params)
772 (cdr (assoc :hlines params))
773 (cdr (assoc :colnames params))
774 (cdr (assoc :rownames params))))
775 (vars (car vars-and-names))
776 (colnames (cadr vars-and-names))
777 (rownames (caddr vars-and-names))
778 (result-params (split-string (or (cdr (assoc :results params)) "")))
779 (result-type (cond ((member "output" result-params) 'output)
780 ((member "value" result-params) 'value)
781 (t 'value))))
782 (list session vars result-params result-type colnames rownames)))
784 ;; row and column names
785 (defun org-babel-del-hlines (table)
786 "Remove all 'hlines from TABLE."
787 (remove 'hline table))
789 (defun org-babel-get-colnames (table)
790 "Return the column names of TABLE.
791 Return a cons cell, the `car' of which contains the TABLE less
792 colnames, and the `cdr' of which contains a list of the column
793 names."
794 (if (equal 'hline (nth 1 table))
795 (cons (cddr table) (car table))
796 (cons (cdr table) (car table))))
798 (defun org-babel-get-rownames (table)
799 "Return the row names of TABLE.
800 Return a cons cell, the `car' of which contains the TABLE less
801 colnames, and the `cdr' of which contains a list of the column
802 names. Note: this function removes any hlines in TABLE."
803 (flet ((trans (table) (apply #'mapcar* #'list table)))
804 (let* ((width (apply 'max (mapcar (lambda (el) (if (listp el) (length el) 0)) table)))
805 (table (trans (mapcar (lambda (row)
806 (if (not (equal row 'hline))
808 (setq row '())
809 (dotimes (n width) (setq row (cons 'hline row)))
810 row))
811 table))))
812 (cons (mapcar (lambda (row) (if (equal (car row) 'hline) 'hline row))
813 (trans (cdr table)))
814 (remove 'hline (car table))))))
816 (defun org-babel-put-colnames (table colnames)
817 "Add COLNAMES to TABLE if they exist."
818 (if colnames (apply 'list colnames 'hline table) table))
820 (defun org-babel-put-rownames (table rownames)
821 "Add ROWNAMES to TABLE if they exist."
822 (if rownames
823 (mapcar (lambda (row)
824 (if (listp row)
825 (cons (or (pop rownames) "") row)
826 row)) table)
827 table))
829 (defun org-babel-pick-name (names selector)
830 "Select one out of an alist of row or column names."
831 (when names
832 (if (and selector (symbolp selector) (not (equal t selector)))
833 (cdr (assoc selector names))
834 (if (integerp selector)
835 (nth (- selector 1) names)
836 (cdr (car (last names)))))))
838 (defun org-babel-disassemble-tables (vars hlines colnames rownames)
839 "Parse tables for further processing.
840 Process the variables in VARS according to the HLINES,
841 ROWNAMES and COLNAMES header arguments. Return a list consisting
842 of the vars, cnames and rnames."
843 (let (cnames rnames)
844 (list
845 (mapcar
846 (lambda (var)
847 (when (listp (cdr var))
848 (when (and (not (equal colnames "no"))
849 (or colnames (and (equal (nth 1 (cdr var)) 'hline)
850 (not (member 'hline (cddr (cdr var)))))))
851 (let ((both (org-babel-get-colnames (cdr var))))
852 (setq cnames (cons (cons (car var) (cdr both))
853 cnames))
854 (setq var (cons (car var) (car both)))))
855 (when (and rownames (not (equal rownames "no")))
856 (let ((both (org-babel-get-rownames (cdr var))))
857 (setq rnames (cons (cons (car var) (cdr both))
858 rnames))
859 (setq var (cons (car var) (car both)))))
860 (when (and hlines (not (equal hlines "yes")))
861 (setq var (cons (car var) (org-babel-del-hlines (cdr var))))))
862 var)
863 vars)
864 cnames rnames)))
866 (defun org-babel-reassemble-table (table colnames rownames)
867 "Add column and row names to a table.
868 Given a TABLE and set of COLNAMES and ROWNAMES add the names
869 to the table for reinsertion to org-mode."
870 (if (listp table)
871 ((lambda (table)
872 (if (and colnames (listp (car table)) (= (length (car table))
873 (length colnames)))
874 (org-babel-put-colnames table colnames) table))
875 (if (and rownames (= (length table) (length rownames)))
876 (org-babel-put-rownames table rownames) table))
877 table))
879 (defun org-babel-where-is-src-block-head ()
880 "Find where the current source block begins.
881 Return the point at the beginning of the current source
882 block. Specifically at the beginning of the #+BEGIN_SRC line.
883 If the point is not on a source block then return nil."
884 (let ((initial (point)) top bottom)
886 (save-excursion ;; on a source name line
887 (beginning-of-line 1)
888 (and (looking-at org-babel-src-name-regexp) (forward-line 1)
889 (looking-at org-babel-src-block-regexp)
890 (point)))
891 (save-excursion ;; on a #+begin_src line
892 (beginning-of-line 1)
893 (and (looking-at org-babel-src-block-regexp)
894 (point)))
895 (save-excursion ;; inside a src block
896 (and
897 (re-search-backward "^[ \t]*#\\+begin_src" nil t) (setq top (point))
898 (re-search-forward "^[ \t]*#\\+end_src" nil t) (setq bottom (point))
899 (< top initial) (< initial bottom)
900 (progn (goto-char top) (beginning-of-line 1)
901 (looking-at org-babel-src-block-regexp))
902 (point))))))
904 ;;;###autoload
905 (defun org-babel-goto-named-src-block (name)
906 "Go to a named source-code block."
907 (interactive
908 (let ((completion-ignore-case t))
909 (list (org-icompleting-read "source-block name: "
910 (org-babel-src-block-names) nil t))))
911 (let ((point (org-babel-find-named-block name)))
912 (if point
913 ;; taken from `org-open-at-point'
914 (progn (goto-char point) (org-show-context))
915 (message "source-code block '%s' not found in this buffer" name))))
917 (defun org-babel-find-named-block (name)
918 "Find a named source-code block.
919 Return the location of the source block identified by source
920 NAME, or nil if no such block exists. Set match data according to
921 org-babel-named-src-block-regexp."
922 (save-excursion
923 (let ((case-fold-search t)
924 (regexp (org-babel-named-src-block-regexp-for-name name)) msg)
925 (goto-char (point-min))
926 (when (or (re-search-forward regexp nil t)
927 (re-search-backward regexp nil t))
928 (match-beginning 0)))))
930 (defun org-babel-src-block-names (&optional file)
931 "Returns the names of source blocks in FILE or the current buffer."
932 (save-excursion
933 (when file (find-file file)) (goto-char (point-min))
934 (let (names)
935 (while (re-search-forward org-babel-src-name-w-name-regexp nil t)
936 (setq names (cons (org-babel-clean-text-properties (match-string 2))
937 names)))
938 names)))
940 ;;;###autoload
941 (defun org-babel-goto-named-result (name)
942 "Go to a named result."
943 (interactive
944 (let ((completion-ignore-case t))
945 (list (org-icompleting-read "source-block name: "
946 (org-babel-result-names) nil t))))
947 (let ((point (org-babel-find-named-result name)))
948 (if point
949 ;; taken from `org-open-at-point'
950 (progn (goto-char point) (org-show-context))
951 (message "result '%s' not found in this buffer" name))))
953 (defun org-babel-find-named-result (name)
954 "Find a named result.
955 Return the location of the result named NAME in the current
956 buffer or nil if no such result exists."
957 (save-excursion
958 (goto-char (point-min))
959 (when (re-search-forward
960 (concat org-babel-result-regexp
961 "[ \t]" (regexp-quote name) "[ \t\n\f\v\r]") nil t)
962 (beginning-of-line 0) (point))))
964 (defun org-babel-result-names (&optional file)
965 "Returns the names of results in FILE or the current buffer."
966 (save-excursion
967 (when file (find-file file)) (goto-char (point-min))
968 (let (names)
969 (while (re-search-forward org-babel-result-w-name-regexp nil t)
970 (setq names (cons (org-babel-clean-text-properties (match-string 4))
971 names)))
972 names)))
974 ;;;###autoload
975 (defun org-babel-next-src-block (&optional arg)
976 "Jump to the next source block.
977 With optional prefix argument ARG, jump forward ARG many source blocks."
978 (interactive "P")
979 (when (looking-at org-babel-src-block-regexp) (forward-char 1))
980 (re-search-forward org-babel-src-block-regexp nil nil (or arg 1))
981 (goto-char (match-beginning 0)) (org-show-context))
983 ;;;###autoload
984 (defun org-babel-previous-src-block (&optional arg)
985 "Jump to the previous source block.
986 With optional prefix argument ARG, jump backward ARG many source blocks."
987 (interactive "P")
988 (re-search-backward org-babel-src-block-regexp nil nil (or arg 1))
989 (goto-char (match-beginning 0)) (org-show-context))
991 (defvar org-babel-lob-one-liner-regexp)
992 (defun org-babel-where-is-src-block-result (&optional insert info hash indent)
993 "Find where the current source block results begin.
994 Return the point at the beginning of the result of the current
995 source block. Specifically at the beginning of the results line.
996 If no result exists for this block then create a results line
997 following the source block."
998 (save-excursion
999 (let* ((on-lob-line (progn (beginning-of-line 1)
1000 (looking-at org-babel-lob-one-liner-regexp)))
1001 (name (if on-lob-line
1002 (nth 0 (org-babel-lob-get-info))
1003 (nth 4 (or info (org-babel-get-src-block-info)))))
1004 (head (unless on-lob-line (org-babel-where-is-src-block-head)))
1005 found beg end)
1006 (when head (goto-char head))
1007 (setq
1008 found ;; was there a result (before we potentially insert one)
1010 (and
1011 ;; named results:
1012 ;; - return t if it is found, else return nil
1013 ;; - if it does not need to be rebuilt, then don't set end
1014 ;; - if it does need to be rebuilt then do set end
1015 name (setq beg (org-babel-find-named-result name))
1016 (prog1 beg
1017 (when (and hash (not (string= hash (match-string 3))))
1018 (goto-char beg) (setq end beg) ;; beginning of result
1019 (forward-line 1)
1020 (delete-region end (org-babel-result-end)) nil)))
1021 (and
1022 ;; unnamed results:
1023 ;; - return t if it is found, else return nil
1024 ;; - if it is found, and the hash doesn't match, delete and set end
1025 (or on-lob-line (re-search-forward "^[ \t]*#\\+end_src" nil t))
1026 (progn (end-of-line 1)
1027 (if (eobp) (insert "\n") (forward-char 1))
1028 (setq end (point))
1029 (or (and (not name)
1030 (progn ;; unnamed results line already exists
1031 (re-search-forward "[^ \f\t\n\r\v]" nil t)
1032 (beginning-of-line 1)
1033 (looking-at
1034 (concat org-babel-result-regexp "\n")))
1035 (prog1 (point)
1036 ;; must remove and rebuild if hash!=old-hash
1037 (if (and hash (not (string= hash (match-string 3))))
1038 (prog1 nil
1039 (forward-line 1)
1040 (delete-region
1041 end (org-babel-result-end)))
1042 (setq end nil)))))))))
1043 (if (and insert end)
1044 (progn
1045 (goto-char end)
1046 (unless beg
1047 (if (looking-at "[\n\r]") (forward-char 1) (insert "\n")))
1048 (insert (concat
1049 (if indent
1050 (mapconcat
1051 (lambda (el) " ")
1052 (number-sequence 1 indent) "")
1054 "#+results"
1055 (when hash (concat "["hash"]"))
1057 (when name (concat " " name)) "\n"))
1058 (unless beg (insert "\n") (backward-char))
1059 (beginning-of-line 0)
1060 (if hash (org-babel-hide-hash))
1061 (point))
1062 found))))
1064 (defvar org-block-regexp)
1065 (defun org-babel-read-result ()
1066 "Read the result at `point' into emacs-lisp."
1067 (let ((case-fold-search t) result-string)
1068 (cond
1069 ((org-at-table-p) (org-babel-read-table))
1070 ((looking-at org-bracket-link-regexp) (org-babel-read-link))
1071 ((looking-at org-block-regexp) (org-babel-trim (match-string 4)))
1072 ((looking-at "^[ \t]*: ")
1073 (setq result-string
1074 (org-babel-trim
1075 (mapconcat (lambda (line)
1076 (if (and (> (length line) 1)
1077 (string-match "^[ \t]*: \\(.+\\)" line))
1078 (match-string 1 line)
1079 line))
1080 (split-string
1081 (buffer-substring
1082 (point) (org-babel-result-end)) "[\r\n]+")
1083 "\n")))
1084 (or (org-babel-number-p result-string) result-string))
1085 ((looking-at org-babel-result-regexp)
1086 (save-excursion (forward-line 1) (org-babel-read-result))))))
1088 (defun org-babel-read-table ()
1089 "Read the table at `point' into emacs-lisp."
1090 (mapcar (lambda (row)
1091 (if (and (symbolp row) (equal row 'hline)) row
1092 (mapcar #'org-babel-read row)))
1093 (org-table-to-lisp)))
1095 (defvar org-link-types-re)
1096 (defun org-babel-read-link ()
1097 "Read the link at `point' into emacs-lisp.
1098 If the path of the link is a file path it is expanded using
1099 `expand-file-name'."
1100 (let* ((case-fold-search t)
1101 (raw (and (looking-at org-bracket-link-regexp)
1102 (org-babel-clean-text-properties (match-string 1))))
1103 (type (and (string-match org-link-types-re raw)
1104 (match-string 1 raw))))
1105 (cond
1106 ((not type) (expand-file-name raw))
1107 ((string= type "file")
1108 (and (string-match "file\\(.*\\):\\(.+\\)" raw)
1109 (expand-file-name (match-string 2 raw))))
1110 (t raw))))
1112 (defun org-babel-insert-result
1113 (result &optional result-params info hash indent lang)
1114 "Insert RESULT into the current buffer.
1115 By default RESULT is inserted after the end of the
1116 current source block. With optional argument RESULT-PARAMS
1117 controls insertion of results in the org-mode file.
1118 RESULT-PARAMS can take the following values...
1120 replace - (default option) insert results after the source block
1121 replacing any previously inserted results
1123 silent -- no results are inserted
1125 file ---- the results are interpreted as a file path, and are
1126 inserted into the buffer using the Org-mode file syntax
1128 raw ----- results are added directly to the org-mode file. This
1129 is a good option if you code block will output org-mode
1130 formatted text.
1132 org ----- this is the same as the 'raw' option
1134 html ---- results are added inside of a #+BEGIN_HTML block. This
1135 is a good option if you code block will output html
1136 formatted text.
1138 latex --- results are added inside of a #+BEGIN_LATEX block.
1139 This is a good option if you code block will output
1140 latex formatted text.
1142 code ---- the results are extracted in the syntax of the source
1143 code of the language being evaluated and are added
1144 inside of a #+BEGIN_SRC block with the source-code
1145 language set appropriately. Note this relies on the
1146 optional LANG argument."
1147 (if (stringp result)
1148 (progn
1149 (setq result (org-babel-clean-text-properties result))
1150 (when (member "file" result-params)
1151 (setq result (org-babel-result-to-file result))))
1152 (unless (listp result) (setq result (format "%S" result))))
1153 (if (= (length result) 0)
1154 (if (member "value" result-params)
1155 (message "No result returned by source block")
1156 (message "Source block produced no output"))
1157 (if (and result-params (member "silent" result-params))
1158 (progn
1159 (message (replace-regexp-in-string "%" "%%" (format "%S" result)))
1160 result)
1161 (when (and (stringp result) ;; ensure results end in a newline
1162 (not (or (string-equal (substring result -1) "\n")
1163 (string-equal (substring result -1) "\r"))))
1164 (setq result (concat result "\n")))
1165 (save-excursion
1166 (let ((existing-result (org-babel-where-is-src-block-result
1167 t info hash indent))
1168 (results-switches
1169 (cdr (assoc :results_switches (nth 2 info))))
1170 beg end)
1171 (when existing-result
1172 (goto-char existing-result)
1173 (save-excursion
1174 (re-search-forward "#" nil t)
1175 (setq indent (- (current-column) 1)))
1176 (forward-line 1)
1177 (setq beg (point))
1178 (cond
1179 ((member "replace" result-params)
1180 (delete-region (point) (org-babel-result-end)))
1181 ((member "append" result-params)
1182 (goto-char (org-babel-result-end)) (setq beg (point)))
1183 ((member "prepend" result-params) ;; already there
1185 (setq results-switches
1186 (if results-switches (concat " " results-switches) ""))
1187 (cond
1188 ;; assume the result is a table if it's not a string
1189 ((not (stringp result))
1190 (insert (concat (orgtbl-to-orgtbl
1191 (if (or (eq 'hline (car result))
1192 (and (listp (car result))
1193 (listp (cdr (car result)))))
1194 result (list result))
1195 '(:fmt (lambda (cell) (format "%s" cell)))) "\n"))
1196 (goto-char beg) (when (org-at-table-p) (org-table-align)))
1197 ((member "file" result-params)
1198 (insert result))
1199 ((member "html" result-params)
1200 (insert (format "#+BEGIN_HTML%s\n%s#+END_HTML\n"
1201 results-switches result)))
1202 ((member "latex" result-params)
1203 (insert (format "#+BEGIN_LaTeX%s\n%s#+END_LaTeX\n"
1204 results-switches result)))
1205 ((member "code" result-params)
1206 (insert (format "#+BEGIN_SRC %s%s\n%s#+END_SRC\n"
1207 (or lang "none") results-switches result)))
1208 ((or (member "raw" result-params) (member "org" result-params))
1209 (save-excursion (insert result)) (if (org-at-table-p) (org-cycle)))
1211 (org-babel-examplize-region
1212 (point) (progn (insert result) (point)) results-switches)))
1213 ;; possibly indent the results to match the #+results line
1214 (setq end (if (listp result) (org-table-end) (point)))
1215 (when (and indent (> indent 0)
1216 ;; in this case `table-align' does the work for us
1217 (not (and (listp result)
1218 (member "append" result-params))))
1219 (indent-rigidly beg end indent))))
1220 (message "finished"))))
1222 (defun org-babel-remove-result (&optional info)
1223 "Remove the result of the current source block."
1224 (interactive)
1225 (let ((location (org-babel-where-is-src-block-result nil info)) start)
1226 (when location
1227 (save-excursion
1228 (goto-char location) (setq start (point)) (forward-line 1)
1229 (delete-region start (org-babel-result-end))))))
1231 (defun org-babel-result-end ()
1232 "Return the point at the end of the current set of results"
1233 (save-excursion
1234 (if (org-at-table-p)
1235 (progn (goto-char (org-table-end)) (point))
1236 (let ((case-fold-search t))
1237 (cond
1238 ((looking-at "[ \t]*#\\+begin_latex")
1239 (re-search-forward "[ \t]*#\\+end_latex" nil t)
1240 (forward-line 1))
1241 ((looking-at "[ \t]*#\\+begin_html")
1242 (re-search-forward "[ \t]*#\\+end_html" nil t)
1243 (forward-line 1))
1244 ((looking-at "[ \t]*#\\+begin_example")
1245 (re-search-forward "[ \t]*#\\+end_example" nil t)
1246 (forward-line 1))
1247 ((looking-at "[ \t]*#\\+begin_src")
1248 (re-search-forward "[ \t]*#\\+end_src" nil t)
1249 (forward-line 1))
1250 (t (progn (while (looking-at "[ \t]*\\(: \\|\\[\\[\\)")
1251 (forward-line 1))))))
1252 (point))))
1254 (defun org-babel-result-to-file (result)
1255 "Convert RESULT into an `org-mode' link.
1256 If the `default-directory' is different from the containing
1257 file's directory then expand relative links."
1258 (format
1259 "[[file:%s]]"
1260 (if (and default-directory
1261 buffer-file-name
1262 (not (string= (expand-file-name default-directory)
1263 (expand-file-name
1264 (file-name-directory buffer-file-name)))))
1265 (expand-file-name result default-directory)
1266 result)))
1268 (defun org-babel-examplize-region (beg end &optional results-switches)
1269 "Comment out region using the ': ' org example quote."
1270 (interactive "*r")
1271 (let ((size (count-lines beg end)))
1272 (save-excursion
1273 (cond ((= size 0)
1274 (error (concat "This should be impossible:"
1275 "a newline was appended to result if missing")))
1276 ((< size org-babel-min-lines-for-block-output)
1277 (goto-char beg)
1278 (dotimes (n size)
1279 (beginning-of-line 1) (insert ": ") (forward-line 1)))
1281 (goto-char beg)
1282 (insert (if results-switches
1283 (format "#+begin_example%s\n" results-switches)
1284 "#+begin_example\n"))
1285 (forward-char (- end beg))
1286 (insert "#+end_example\n"))))))
1288 (defun org-babel-merge-params (&rest plists)
1289 "Combine all parameter association lists in PLISTS.
1290 Later elements of PLISTS override the values of previous element.
1291 This takes into account some special considerations for certain
1292 parameters when merging lists."
1293 (let ((results-exclusive-groups
1294 '(("file" "vector" "table" "scalar" "raw" "org"
1295 "html" "latex" "code" "pp")
1296 ("replace" "silent" "append" "prepend")
1297 ("output" "value")))
1298 (exports-exclusive-groups
1299 '(("code" "results" "both" "none")))
1300 params results exports tangle noweb cache vars var ref shebang comments)
1301 (flet ((e-merge (exclusive-groups &rest result-params)
1302 ;; maintain exclusivity of mutually exclusive parameters
1303 (let (output)
1304 (mapc (lambda (new-params)
1305 (mapc (lambda (new-param)
1306 (mapc (lambda (exclusive-group)
1307 (when (member new-param exclusive-group)
1308 (mapcar (lambda (excluded-param)
1309 (setq output
1310 (delete
1311 excluded-param
1312 output)))
1313 exclusive-group)))
1314 exclusive-groups)
1315 (setq output (org-uniquify
1316 (cons new-param output))))
1317 new-params))
1318 result-params)
1319 output)))
1320 (mapc (lambda (plist)
1321 (mapc (lambda (pair)
1322 (case (car pair)
1323 (:var
1324 ;; we want only one specification per variable
1325 (when (string-match
1326 (concat "^\\([^= \f\t\n\r\v]+\\)[ \t]*="
1327 "[ \t]*\\([^\f\n\r\v]+\\)$") (cdr pair))
1328 ;; TODO: When is this not true?
1329 (setq var (intern (match-string 1 (cdr pair)))
1330 ref (match-string 2 (cdr pair))
1331 vars (cons (cons var ref)
1332 (assq-delete-all var vars)))))
1333 (:results
1334 (setq results
1335 (e-merge results-exclusive-groups
1336 results (split-string (cdr pair)))))
1337 (:file
1338 (when (cdr pair)
1339 (setq results (e-merge results-exclusive-groups
1340 results '("file")))
1341 (unless (or (member "both" exports)
1342 (member "none" exports)
1343 (member "code" exports))
1344 (setq exports (e-merge exports-exclusive-groups
1345 exports '("results"))))
1346 (setq params
1347 (cons pair
1348 (assq-delete-all (car pair) params)))))
1349 (:exports
1350 (setq exports
1351 (e-merge exports-exclusive-groups
1352 exports (split-string (cdr pair)))))
1353 (:tangle ;; take the latest -- always overwrite
1354 (setq tangle (or (list (cdr pair)) tangle)))
1355 (:noweb
1356 (setq noweb
1357 (e-merge '(("yes" "no")) noweb
1358 (split-string (or (cdr pair) "")))))
1359 (:cache
1360 (setq cache
1361 (e-merge '(("yes" "no")) cache
1362 (split-string (or (cdr pair) "")))))
1363 (:shebang ;; take the latest -- always overwrite
1364 (setq shebang (or (list (cdr pair)) shebang)))
1365 (:comments
1366 (setq comments
1367 (e-merge '(("yes" "no")) comments
1368 (split-string (or (cdr pair) "")))))
1369 (t ;; replace: this covers e.g. :session
1370 (setq params
1371 (cons pair
1372 (assq-delete-all (car pair) params))))))
1373 plist))
1374 plists))
1375 (setq vars (mapcar (lambda (pair) (format "%s=%s" (car pair) (cdr pair))) vars))
1376 (while vars (setq params (cons (cons :var (pop vars)) params)))
1377 (cons (cons :comments (mapconcat 'identity comments " "))
1378 (cons (cons :shebang (mapconcat 'identity shebang " "))
1379 (cons (cons :cache (mapconcat 'identity cache " "))
1380 (cons (cons :noweb (mapconcat 'identity noweb " "))
1381 (cons (cons :tangle (mapconcat 'identity tangle " "))
1382 (cons (cons :exports
1383 (mapconcat 'identity exports " "))
1384 (cons
1385 (cons :results
1386 (mapconcat 'identity results " "))
1387 params)))))))))
1389 (defun org-babel-expand-noweb-references (&optional info parent-buffer)
1390 "Expand Noweb references in the body of the current source code block.
1392 For example the following reference would be replaced with the
1393 body of the source-code block named 'example-block'.
1395 <<example-block>>
1397 Note that any text preceding the <<foo>> construct on a line will
1398 be interposed between the lines of the replacement text. So for
1399 example if <<foo>> is placed behind a comment, then the entire
1400 replacement text will also be commented.
1402 This function must be called from inside of the buffer containing
1403 the source-code block which holds BODY.
1405 In addition the following syntax can be used to insert the
1406 results of evaluating the source-code block named 'example-block'.
1408 <<example-block()>>
1410 Any optional arguments can be passed to example-block by placing
1411 the arguments inside the parenthesis following the convention
1412 defined by `org-babel-lob'. For example
1414 <<example-block(a=9)>>
1416 would set the value of argument \"a\" equal to \"9\". Note that
1417 these arguments are not evaluated in the current source-code
1418 block but are passed literally to the \"example-block\"."
1419 (let* ((parent-buffer (or parent-buffer (current-buffer)))
1420 (info (or info (org-babel-get-src-block-info)))
1421 (lang (nth 0 info))
1422 (body (nth 1 info))
1423 (new-body "") index source-name evaluate prefix)
1424 (flet ((nb-add (text)
1425 (setq new-body (concat new-body text))))
1426 (with-temp-buffer
1427 (insert body) (goto-char (point-min))
1428 (setq index (point))
1429 (while (and (re-search-forward "<<\\(.+?\\)>>" nil t))
1430 (save-match-data (setf source-name (match-string 1)))
1431 (save-match-data (setq evaluate (string-match "\(.*\)" source-name)))
1432 (save-match-data
1433 (setq prefix
1434 (buffer-substring (match-beginning 0)
1435 (save-excursion
1436 (beginning-of-line 1) (point)))))
1437 ;; add interval to new-body (removing noweb reference)
1438 (goto-char (match-beginning 0))
1439 (nb-add (buffer-substring index (point)))
1440 (goto-char (match-end 0))
1441 (setq index (point))
1442 (nb-add (with-current-buffer parent-buffer
1443 (mapconcat ;; interpose PREFIX between every line
1444 #'identity
1445 (split-string
1446 (if evaluate
1447 (let ((raw (org-babel-ref-resolve-reference
1448 source-name nil)))
1449 (if (stringp raw) raw (format "%S" raw)))
1450 (save-restriction
1451 (widen)
1452 (let ((point (org-babel-find-named-block
1453 source-name)))
1454 (if point
1455 (save-excursion
1456 (goto-char point)
1457 (org-babel-trim
1458 (org-babel-expand-noweb-references
1459 (org-babel-get-src-block-info))))
1460 ;; optionally raise an error if named
1461 ;; source-block doesn't exist
1462 (if (member lang org-babel-noweb-error-langs)
1463 (error "%s"
1464 (concat
1465 "<<" source-name ">> "
1466 "could not be resolved (see "
1467 "`org-babel-noweb-error-langs')"))
1468 "")))))
1469 "[\n\r]") (concat "\n" prefix)))))
1470 (nb-add (buffer-substring index (point-max)))))
1471 new-body))
1473 (defun org-babel-clean-text-properties (text)
1474 "Strip all properties from text return."
1475 (when text
1476 (set-text-properties 0 (length text) nil text) text))
1478 (defun org-babel-strip-protective-commas (body)
1479 "Strip protective commas from bodies of source blocks."
1480 (replace-regexp-in-string "^,#" "#" body))
1482 (defun org-babel-read (cell)
1483 "Convert the string value of CELL to a number if appropriate.
1484 Otherwise if cell looks like lisp (meaning it starts with a
1485 \"(\" or a \"'\") then read it as lisp, otherwise return it
1486 unmodified as a string.
1488 This is taken almost directly from `org-read-prop'."
1489 (if (and (stringp cell) (not (equal cell "")))
1490 (or (org-babel-number-p cell)
1491 (if (or (equal "(" (substring cell 0 1))
1492 (equal "'" (substring cell 0 1))
1493 (equal "`" (substring cell 0 1)))
1494 (eval (read cell))
1495 (progn (set-text-properties 0 (length cell) nil cell) cell)))
1496 cell))
1498 (defun org-babel-number-p (string)
1499 "Return t if STRING represents a number."
1500 (if (and (string-match "^-?[0-9]*\\.?[0-9]*$" string)
1501 (= (length (substring string (match-beginning 0)
1502 (match-end 0)))
1503 (length string)))
1504 (string-to-number string)))
1506 (defun org-babel-import-elisp-from-file (file-name)
1507 "Read the results located at FILE-NAME into an elisp table.
1508 If the table is trivial, then return it as a scalar."
1509 (let (result)
1510 (save-window-excursion
1511 (with-temp-buffer
1512 (condition-case nil
1513 (progn
1514 (org-table-import file-name nil)
1515 (delete-file file-name)
1516 (setq result (mapcar (lambda (row)
1517 (mapcar #'org-babel-string-read row))
1518 (org-table-to-lisp))))
1519 (error nil)))
1520 (if (null (cdr result)) ;; if result is trivial vector, then scalarize it
1521 (if (consp (car result))
1522 (if (null (cdr (car result)))
1523 (caar result)
1524 result)
1525 (car result))
1526 result))))
1528 (defun org-babel-string-read (cell)
1529 "Strip nested \"s from around strings."
1530 (org-babel-read (or (and (stringp cell)
1531 (string-match "\\\"\\(.+\\)\\\"" cell)
1532 (match-string 1 cell))
1533 cell)))
1535 (defun org-babel-reverse-string (string)
1536 "Return the reverse of STRING."
1537 (apply 'string (reverse (string-to-list string))))
1539 (defun org-babel-chomp (string &optional regexp)
1540 "Strip trailing spaces and carriage returns from STRING.
1541 Default regexp used is \"[ \f\t\n\r\v]\" but can be
1542 overwritten by specifying a regexp as a second argument."
1543 (let ((regexp (or regexp "[ \f\t\n\r\v]")))
1544 (while (and (> (length string) 0)
1545 (string-match regexp (substring string -1)))
1546 (setq string (substring string 0 -1)))
1547 string))
1549 (defun org-babel-trim (string &optional regexp)
1550 "Strip leading and trailing spaces and carriage returns from STRING.
1551 Like `org-babel-chomp' only it runs on both the front and back
1552 of the string."
1553 (org-babel-chomp (org-babel-reverse-string
1554 (org-babel-chomp (org-babel-reverse-string string) regexp))
1555 regexp))
1557 (defvar org-babel-org-babel-call-process-region-original nil)
1558 (defun org-babel-tramp-handle-call-process-region
1559 (start end program &optional delete buffer display &rest args)
1560 "Use tramp to handle call-process-region.
1561 Fixes a bug in `tramp-handle-call-process-region'."
1562 (if (and (featurep 'tramp) (file-remote-p default-directory))
1563 (let ((tmpfile (tramp-compat-make-temp-file "")))
1564 (write-region start end tmpfile)
1565 (when delete (delete-region start end))
1566 (unwind-protect
1567 ;; (apply 'call-process program tmpfile buffer display args)
1568 ;; bug in tramp
1569 (apply 'process-file program tmpfile buffer display args)
1570 (delete-file tmpfile)))
1571 ;; org-babel-call-process-region-original is the original emacs definition. It
1572 ;; is in scope from the let binding in org-babel-execute-src-block
1573 (apply org-babel-call-process-region-original
1574 start end program delete buffer display args)))
1576 (defun org-babel-maybe-remote-file (file)
1577 "Conditionally parse information on a remote connnection.
1578 If FILE specifies a remove file, then parse the information on
1579 the remote connection."
1580 (if (file-remote-p default-directory)
1581 (let* ((vec (tramp-dissect-file-name default-directory))
1582 (user (tramp-file-name-user vec))
1583 (host (tramp-file-name-host vec)))
1584 (concat "/" user (when user "@") host ":" file))
1585 file))
1587 (provide 'ob)
1589 ;; arch-tag: 01a7ebee-06c5-4ee4-a709-e660d28c0af1
1591 ;;; ob.el ends here