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