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
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/>.
27 ;; See the online documentation for more information
29 ;; http://orgmode.org/worg/org-contrib/babel/
34 (require 'org
)) ; org-save-outline-visibility macro
37 (defvar org-babel-call-process-region-original
)
38 (declare-function show-all
"outline" ())
39 (declare-function tramp-compat-make-temp-file
"tramp-compat"
40 (filename &optional dir-flag
))
41 (declare-function tramp-dissect-file-name
"tramp" (name &optional nodefault
))
42 (declare-function tramp-file-name-user
"tramp" (vec))
43 (declare-function tramp-file-name-host
"tramp" (vec))
44 (declare-function org-icompleting-read
"org" (&rest args
))
45 (declare-function org-edit-src-code
"org-src"
46 (&optional context code edit-buffer-name
))
47 (declare-function org-open-at-point
"org" (&optional in-emacs reference-buffer
))
48 (declare-function org-narrow-to-subtree
"org" ())
49 (declare-function org-entry-get
"org" (pom property
&optional inherit literal-nil
))
50 (declare-function org-make-options-regexp
"org" (kwds &optional extra
))
51 (declare-function org-do-remove-indentation
"org" (&optional n
))
52 (declare-function org-show-context
"org" (&optional key
))
53 (declare-function org-at-table-p
"org" (&optional table-type
))
54 (declare-function org-cycle
"org" (&optional arg
))
55 (declare-function org-uniquify
"org" (list))
56 (declare-function org-table-import
"org-table" (file arg
))
57 (declare-function org-add-hook
"org-compat" (hook function
&optional append local
))
58 (declare-function org-table-align
"org-table" ())
59 (declare-function org-table-end
"org-table" (&optional table-type
))
60 (declare-function orgtbl-to-generic
"org-table" (table params
))
61 (declare-function orgtbl-to-orgtbl
"org-table" (table params
))
62 (declare-function org-babel-lob-get-info
"ob-lob" nil
)
63 (declare-function org-babel-ref-split-args
"ob-ref" (arg-string))
64 (declare-function org-babel-ref-variables
"ob-ref" (params))
65 (declare-function org-babel-ref-resolve-reference
"ob-ref" (ref &optional params
))
67 (defgroup org-babel nil
68 "Code block evaluation and management in `org-mode' documents."
72 (defcustom org-confirm-babel-evaluate t
73 "Confirm before evaluation.
74 Require confirmation before interactively evaluating code
75 blocks in Org-mode buffers. The default value of this variable
76 is t, meaning confirmation is required for any code block
77 evaluation. This variable can be set to nil to inhibit any
78 future confirmation requests. This variable can also be set to a
79 function which takes two arguments the language of the code block
80 and the body of the code block. Such a function should then
81 return a non-nil value if the user should be prompted for
82 execution or nil if no prompt is required.
84 Warning: Disabling confirmation may result in accidental
85 evaluation of potentially harmful code. It may be advisable
86 remove code block execution from C-c C-c as further protection
87 against accidental code block evaluation. The
88 `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can be used to
89 remove code block execution from the C-c C-c keybinding."
91 :type
'(choice boolean function
))
92 ;; don't allow this variable to be changed through file settings
93 (put 'org-confirm-babel-evaluate
'safe-local-variable
(lambda (x) (eq x t
)))
95 (defcustom org-babel-no-eval-on-ctrl-c-ctrl-c nil
96 "Remove code block evaluation from the C-c C-c key binding."
100 (defvar org-babel-src-name-regexp
101 "^[ \t]*#\\+\\(srcname\\|source\\|function\\):[ \t]*"
102 "Regular expression used to match a source name line.")
104 (defvar org-babel-src-name-w-name-regexp
105 (concat org-babel-src-name-regexp
106 "\\([^ ()\f\t\n\r\v]+\\)\\(\(\\(.*\\)\)\\|\\)")
107 "Regular expression matching source name lines with a name.")
109 (defvar org-babel-src-block-regexp
111 ;; (1) indentation (2) lang
112 "^\\([ \t]*\\)#\\+begin_src[ \t]+\\([^ \f\t\n\r\v]+\\)[ \t]*"
114 "\\([^\":\n]*\"[^\"\n*]*\"[^\":\n]*\\|[^\":\n]*\\)"
115 ;; (4) header arguments
118 "\\([^\000]+?\n\\)[ \t]*#\\+end_src")
119 "Regexp used to identify code blocks.")
121 (defvar org-babel-inline-src-block-regexp
123 ;; (1) replacement target (2) lang
124 "[ \f\t\n\r\v]\\(src_\\([^ \f\t\n\r\v]+\\)"
125 ;; (3,4) (unused, headers)
126 "\\(\\|\\[\\(.*?\\)\\]\\)"
128 "{\\([^\f\n\r\v]+?\\)}\\)")
129 "Regexp used to identify inline src-blocks.")
131 (defun org-babel-get-src-block-info (&optional header-vars-only
)
132 "Get information on the current source block.
135 (language body header-arguments-alist switches name function-args indent).
136 Unless HEADER-VARS-ONLY is non-nil, any variable
137 references provided in 'function call style' (i.e. in a
138 parenthesised argument list following the src block name) are
139 added to the header-arguments-alist."
140 (let ((case-fold-search t
) head info args indent
)
141 (if (setq head
(org-babel-where-is-src-block-head))
144 (setq info
(org-babel-parse-src-block-match))
145 (setq indent
(car (last info
)))
146 (setq info
(butlast info
))
148 (if (and (looking-at org-babel-src-name-w-name-regexp
)
151 (setq info
(append info
(list (org-babel-clean-text-properties
153 ;; Note that e.g. "name()" and "name( )" result in
154 ;; ((:var . "")). We maintain that behaviour, and the
155 ;; resulting non-nil sixth element is relied upon in
156 ;; org-babel-exp-code to detect a functional-style
157 ;; block in those cases. However, "name" without any
158 ;; parentheses would result in the same thing, so we
159 ;; explicitly avoid that.
160 (if (setq args
(match-string 4))
164 (lambda (ref) (cons :var ref
))
165 (org-babel-ref-split-args args
))))))
166 (unless header-vars-only
168 (org-babel-merge-params (nth 5 info
) (nth 2 info
)))))
169 (setq info
(append info
(list nil nil
))))
170 (append info
(list indent
)))
171 (if (save-excursion ;; inline source block
172 (re-search-backward "[ \f\t\n\r\v]" nil t
)
173 (looking-at org-babel-inline-src-block-regexp
))
174 (org-babel-parse-inline-src-block-match)
177 (defun org-babel-confirm-evaluate (info)
178 "Confirm evaluation of the code block INFO.
179 This behavior can be suppressed by setting the value of
180 `org-confirm-babel-evaluate' to nil, in which case all future
181 interactive code block evaluations will proceed without any
182 confirmation from the user.
184 Note disabling confirmation may result in accidental evaluation
185 of potentially harmful code."
186 (let* ((eval (cdr (assoc :eval
(nth 2 info
))))
187 (query (or (equal eval
"query")
188 (and (functionp org-confirm-babel-evaluate
)
189 (funcall org-confirm-babel-evaluate
190 (nth 0 info
) (nth 1 info
)))
191 org-confirm-babel-evaluate
)))
192 (when (or (equal eval
"never")
195 (format "Evaluate this%scode on your system? "
196 (if info
(format " %s " (nth 0 info
)) " "))))))
197 (error "evaluation aborted"))))
200 (defun org-babel-execute-src-block-maybe ()
201 "Conditionally execute a source block.
202 Detect if this is context for a Babel src-block and if so
203 then run `org-babel-execute-src-block'."
205 (if (not org-babel-no-eval-on-ctrl-c-ctrl-c
)
206 (let ((info (org-babel-get-src-block-info)))
208 (progn (org-babel-execute-src-block current-prefix-arg info
) t
) nil
))
210 (add-hook 'org-ctrl-c-ctrl-c-hook
'org-babel-execute-src-block-maybe
)
213 (defun org-babel-expand-src-block-maybe ()
214 "Conditionally expand a source block.
215 Detect if this is context for a org-babel src-block and if so
216 then run `org-babel-expand-src-block'."
218 (let ((info (org-babel-get-src-block-info)))
220 (progn (org-babel-expand-src-block current-prefix-arg info
) t
)
224 (defun org-babel-load-in-session-maybe ()
225 "Conditionally load a source block in a session.
226 Detect if this is context for a org-babel src-block and if so
227 then run `org-babel-load-in-session'."
229 (let ((info (org-babel-get-src-block-info)))
231 (progn (org-babel-load-in-session current-prefix-arg info
) t
)
234 (add-hook 'org-metaup-hook
'org-babel-load-in-session-maybe
)
237 (defun org-babel-pop-to-session-maybe ()
238 "Conditionally pop to a session.
239 Detect if this is context for a org-babel src-block and if so
240 then run `org-babel-pop-to-session'."
242 (let ((info (org-babel-get-src-block-info)))
243 (if info
(progn (org-babel-pop-to-session current-prefix-arg info
) t
) nil
)))
245 (add-hook 'org-metadown-hook
'org-babel-pop-to-session-maybe
)
247 (defconst org-babel-header-arg-names
248 '(cache cmdline colnames dir exports file noweb results
249 session tangle var noeval comments
)
250 "Common header arguments used by org-babel.
251 Note that individual languages may define their own language
252 specific header arguments as well.")
254 (defvar org-babel-default-header-args
255 '((:session .
"none") (:results .
"replace") (:exports .
"code")
256 (:cache .
"no") (:noweb .
"no") (:hlines .
"no") (:tangle .
"no"))
257 "Default arguments to use when evaluating a source block.")
259 (defvar org-babel-default-inline-header-args
260 '((:session .
"none") (:results .
"silent") (:exports .
"results"))
261 "Default arguments to use when evaluating an inline source block.")
263 (defvar org-babel-current-buffer-properties
)
264 (make-variable-buffer-local 'org-babel-current-buffer-properties
)
266 (defvar org-babel-result-regexp
267 "^[ \t]*#\\+res\\(ults\\|name\\)\\(\\[\\([[:alnum:]]+\\)\\]\\)?\\:[ \t]*"
268 "Regular expression used to match result lines.
269 If the results are associated with a hash key then the hash will
270 be saved in the second match data.")
272 (defvar org-babel-result-w-name-regexp
273 (concat org-babel-result-regexp
274 "\\([^ ()\f\t\n\r\v]+\\)\\(\(\\(.*\\)\)\\|\\)"))
276 (defvar org-babel-min-lines-for-block-output
10
277 "The minimum number of lines for block output.
278 If number of lines of output is equal to or exceeds this
279 value, the output is placed in a #+begin_example...#+end_example
280 block. Otherwise the output is marked as literal by inserting
281 colons at the starts of the lines. This variable only takes
282 effect if the :results output option is in effect.")
284 (defvar org-babel-noweb-error-langs nil
285 "Languages for which Babel will raise literate programming errors.
286 List of languages for which errors should be raised when the
287 source code block satisfying a noweb reference in this language
288 can not be resolved.")
290 (defvar org-babel-hash-show
4
291 "Number of initial characters to show of a hidden results hash.")
293 (defvar org-babel-after-execute-hook nil
294 "Hook for functions to be called after `org-babel-execute-src-block'")
295 (defun org-babel-named-src-block-regexp-for-name (name)
296 "This generates a regexp used to match a src block named NAME."
297 (concat org-babel-src-name-regexp
(regexp-quote name
) "[ \t\n]*"
298 (substring org-babel-src-block-regexp
1)))
301 (defvar call-process-region
)
303 (defun org-babel-execute-src-block (&optional arg info params
)
304 "Execute the current source code block.
305 Insert the results of execution into the buffer. Source code
306 execution and the collection and formatting of results can be
307 controlled through a variety of header arguments.
309 Optionally supply a value for INFO in the form returned by
310 `org-babel-get-src-block-info'.
312 Optionally supply a value for PARAMS which will be merged with
313 the header arguments specified at the front of the source code
316 (let* ((info (or info
(org-babel-get-src-block-info)))
317 ;; note the `evaluation-confirmed' variable is currently not
318 ;; used, but could be used later to avoid the need for
319 ;; chaining confirmations
320 (evaluation-confirmed (org-babel-confirm-evaluate info
))
322 (params (setf (nth 2 info
)
323 (sort (org-babel-merge-params (nth 2 info
) params
)
324 (lambda (el1 el2
) (string< (symbol-name (car el1
))
325 (symbol-name (car el2
)))))))
327 (if (and (cdr (assoc :cache params
))
328 (string= "yes" (cdr (assoc :cache params
))))
329 (org-babel-sha1-hash info
)))
330 (old-hash (org-babel-result-hash info
))
331 (body (setf (nth 1 info
)
332 (if (and (cdr (assoc :noweb params
))
333 (string= "yes" (cdr (assoc :noweb params
))))
334 (org-babel-expand-noweb-references info
)
336 (result-params (split-string (or (cdr (assoc :results params
)) "")))
337 (result-type (cond ((member "output" result-params
) 'output
)
338 ((member "value" result-params
) 'value
)
340 (cmd (intern (concat "org-babel-execute:" lang
)))
341 (dir (cdr (assoc :dir params
)))
343 (or (and dir
(file-name-as-directory dir
)) default-directory
))
344 (org-babel-call-process-region-original
345 (if (boundp 'org-babel-call-process-region-original
) org-babel-call-process-region-original
346 (symbol-function 'call-process-region
)))
347 (indent (car (last info
)))
350 (flet ((call-process-region (&rest args
)
351 (apply 'org-babel-tramp-handle-call-process-region args
)))
352 (unless (fboundp cmd
)
353 (error "No org-babel-execute function for %s!" lang
))
354 (if (and (not arg
) new-hash
(equal new-hash old-hash
))
355 (save-excursion ;; return cached result
356 (goto-char (org-babel-where-is-src-block-result nil info
))
357 (end-of-line 1) (forward-char 1)
358 (setq result
(org-babel-read-result))
359 (message (replace-regexp-in-string "%" "%%"
360 (format "%S" result
))) result
)
361 (message "executing %s code block%s..."
363 (if (nth 4 info
) (format " (%s)" (nth 4 info
)) ""))
364 (setq result
(funcall cmd body params
))
365 (if (eq result-type
'value
)
366 (setq result
(if (and (or (member "vector" result-params
)
367 (member "table" result-params
))
368 (not (listp result
)))
371 (org-babel-insert-result
372 result result-params info new-hash indent lang
)
373 (run-hooks 'org-babel-after-execute-hook
)
375 (setq call-process-region
'org-babel-call-process-region-original
))))
377 (defun org-babel-expand-body:generic
(body params
&optional processed-params
)
378 "Expand BODY with PARAMS.
379 Expand a block of code with org-babel according to it's header
380 arguments. This generic implementation of body expansion is
381 called for languages which have not defined their own specific
382 org-babel-expand-body:lang function." body
)
385 (defun org-babel-expand-src-block (&optional arg info params
)
386 "Expand the current source code block.
387 Expand according to the source code block's header
388 arguments and pop open the results in a preview buffer."
390 (let* ((info (or info
(org-babel-get-src-block-info)))
392 (params (setf (nth 2 info
)
393 (sort (org-babel-merge-params (nth 2 info
) params
)
394 (lambda (el1 el2
) (string< (symbol-name (car el1
))
395 (symbol-name (car el2
)))))))
396 (body (setf (nth 1 info
)
397 (if (and (cdr (assoc :noweb params
))
398 (string= "yes" (cdr (assoc :noweb params
))))
399 (org-babel-expand-noweb-references info
) (nth 1 info
))))
400 (cmd (intern (concat "org-babel-expand-body:" lang
)))
401 (expanded (funcall (if (fboundp cmd
) cmd
'org-babel-expand-body
:generic
)
404 nil expanded
(concat "*Org-Babel Preview " (buffer-name) "[ " lang
" ]*"))))
407 (defun org-babel-load-in-session (&optional arg info
)
408 "Load the body of the current source-code block.
409 Evaluate the header arguments for the source block before
410 entering the session. After loading the body this pops open the
413 (let* ((info (or info
(org-babel-get-src-block-info)))
416 (params (nth 2 info
))
417 (session (cdr (assoc :session params
)))
418 (cmd (intern (concat "org-babel-load-session:" lang
))))
419 (unless (fboundp cmd
)
420 (error "No org-babel-load-session function for %s!" lang
))
421 (pop-to-buffer (funcall cmd session body params
))
425 (defun org-babel-switch-to-session (&optional arg info
)
426 "Switch to the session of the current source-code block.
427 If called with a prefix argument then evaluate the header arguments
428 for the source block before entering the session. Copy the body
429 of the source block to the kill ring."
431 (let* ((info (or info
(org-babel-get-src-block-info)))
434 (params (nth 2 info
))
435 (session (cdr (assoc :session params
)))
436 (dir (cdr (assoc :dir params
)))
438 (or (and dir
(file-name-as-directory dir
)) default-directory
))
439 (cmd (intern (format "org-babel-%s-initiate-session" lang
)))
440 (cmd2 (intern (concat "org-babel-prep-session:" lang
))))
441 (unless (fboundp cmd
)
442 (error "No org-babel-initiate-session function for %s!" lang
))
443 ;; copy body to the kill ring
444 (with-temp-buffer (insert (org-babel-trim body
))
445 (copy-region-as-kill (point-min) (point-max)))
446 ;; if called with a prefix argument, then process header arguments
447 (unless (fboundp cmd2
)
448 (error "No org-babel-prep-session function for %s!" lang
))
449 (when arg
(funcall cmd2 session params
))
450 ;; just to the session using pop-to-buffer
451 (pop-to-buffer (funcall cmd session params
))
454 (defalias 'org-babel-pop-to-session
'org-babel-switch-to-session
)
456 (defvar org-bracket-link-regexp
)
458 (defun org-babel-open-src-block-result (&optional re-run
)
459 "If `point' is on a src block then open the results of the
460 source code block, otherwise return nil. With optional prefix
461 argument RE-RUN the source-code block is evaluated even if
462 results already exist."
464 (when (org-babel-get-src-block-info)
466 ;; go to the results, if there aren't any then run the block
467 (goto-char (or (and (not re-run
) (org-babel-where-is-src-block-result))
468 (progn (org-babel-execute-src-block)
469 (org-babel-where-is-src-block-result))))
471 (while (looking-at "[\n\r\t\f ]") (forward-char 1))
473 (if (looking-at org-bracket-link-regexp
)
476 (let ((results (org-babel-read-result)))
477 (flet ((echo-res (result)
478 (if (stringp result
) result
(format "%S" result
))))
479 (pop-to-buffer (get-buffer-create "org-babel-results"))
480 (delete-region (point-min) (point-max))
483 (insert (orgtbl-to-generic results
'(:sep
"\t" :fmt echo-res
)))
485 (insert (echo-res results
))))))
489 (defun org-babel-execute-buffer (&optional arg
)
490 "Execute source code blocks in a buffer.
491 Call `org-babel-execute-src-block' on every source block in
495 (org-save-outline-visibility t
496 (goto-char (point-min))
498 (while (re-search-forward org-babel-src-block-regexp nil t
)
499 (let ((pos-end (match-end 0)))
500 (goto-char (match-beginning 0))
501 (org-babel-execute-src-block arg
)
502 (goto-char pos-end
))))))
505 (defun org-babel-execute-subtree (&optional arg
)
506 "Execute source code blocks in a subtree.
507 Call `org-babel-execute-src-block' on every source block in
508 the current subtree."
512 (org-narrow-to-subtree)
513 (org-babel-execute-buffer)
517 (defun org-babel-sha1-hash (&optional info
)
518 "Generate an sha1 hash based on the value of info."
520 (let* ((info (or info
(org-babel-get-src-block-info)))
521 (hash (sha1 (format "%s-%s" (mapconcat (lambda (arg) (format "%S" arg
))
524 (when (interactive-p) (message hash
))
527 (defun org-babel-result-hash (&optional info
)
528 "Return the in-buffer hash associated with INFO."
529 (org-babel-where-is-src-block-result nil info
)
530 (org-babel-clean-text-properties (match-string 3)))
532 (defun org-babel-hide-hash ()
533 "Hide the hash in the current results line.
534 Only the initial `org-babel-hash-show' characters of the hash
535 will remain visible."
536 (add-to-invisibility-spec '(org-babel-hide-hash . t
))
538 (when (and (re-search-forward org-babel-result-regexp nil t
)
540 (let* ((start (match-beginning 3))
541 (hide-start (+ org-babel-hash-show start
))
543 (hash (match-string 3))
545 (setq ov1
(make-overlay start hide-start
))
546 (setq ov2
(make-overlay hide-start end
))
547 (overlay-put ov2
'invisible
'org-babel-hide-hash
)
548 (overlay-put ov1
'babel-hash hash
)))))
550 (defun org-babel-hide-all-hashes ()
551 "Hide the hash in the current buffer.
552 Only the initial `org-babel-hash-show' characters of each hash
553 will remain visible. This function should be called as part of
554 the `org-mode-hook'."
556 (while (re-search-forward org-babel-result-regexp nil t
)
557 (goto-char (match-beginning 0))
558 (org-babel-hide-hash)
559 (goto-char (match-end 0)))))
560 (add-hook 'org-mode-hook
'org-babel-hide-all-hashes
)
562 (defun org-babel-hash-at-point (&optional point
)
563 "Return the value of the hash at POINT.
564 The hash is also added as the last element of the kill ring.
565 This can be called with C-c C-c."
567 (let ((hash (car (delq nil
(mapcar
568 (lambda (ol) (overlay-get ol
'babel-hash
))
569 (overlays-at (or point
(point))))))))
570 (when hash
(kill-new hash
) (message hash
))))
571 (add-hook 'org-ctrl-c-ctrl-c-hook
'org-babel-hash-at-point
)
573 (defun org-babel-result-hide-spec ()
574 "Hide portions of results lines.
575 Add `org-babel-hide-result' as an invisibility spec for hiding
576 portions of results lines."
577 (add-to-invisibility-spec '(org-babel-hide-result . t
)))
578 (add-hook 'org-mode-hook
'org-babel-result-hide-spec
)
580 (defvar org-babel-hide-result-overlays nil
581 "Overlays hiding results.")
583 (defun org-babel-result-hide-all ()
584 "Fold all results in the current buffer."
586 (org-babel-show-result-all)
588 (while (re-search-forward org-babel-result-regexp nil t
)
589 (save-excursion (goto-char (match-beginning 0))
590 (org-babel-hide-result-toggle-maybe)))))
592 (defun org-babel-show-result-all ()
593 "Unfold all results in the current buffer."
594 (mapc 'delete-overlay org-babel-hide-result-overlays
)
595 (setq org-babel-hide-result-overlays nil
))
598 (defun org-babel-hide-result-toggle-maybe ()
599 "Toggle visibility of result at point."
601 (let ((case-fold-search t
))
603 (beginning-of-line 1)
604 (looking-at org-babel-result-regexp
))
605 (progn (org-babel-hide-result-toggle)
606 t
) ;; to signal that we took action
607 nil
))) ;; to signal that we did not
609 (defun org-babel-hide-result-toggle (&optional force
)
610 "Toggle the visibility of the current result."
614 (if (re-search-forward org-babel-result-regexp nil t
)
615 (let ((start (progn (beginning-of-line 2) (- (point) 1)))
616 (end (progn (goto-char (- (org-babel-result-end) 1)) (point)))
618 (if (memq t
(mapcar (lambda (overlay)
619 (eq (overlay-get overlay
'invisible
)
620 'org-babel-hide-result
))
621 (overlays-at start
)))
622 (if (or (not force
) (eq force
'off
))
624 (when (member ov org-babel-hide-result-overlays
)
625 (setq org-babel-hide-result-overlays
626 (delq ov org-babel-hide-result-overlays
)))
627 (when (eq (overlay-get ov
'invisible
)
628 'org-babel-hide-result
)
629 (delete-overlay ov
)))
630 (overlays-at start
)))
631 (setq ov
(make-overlay start end
))
632 (overlay-put ov
'invisible
'org-babel-hide-result
)
633 ;; make the block accessible to isearch
635 ov
'isearch-open-invisible
637 (when (member ov org-babel-hide-result-overlays
)
638 (setq org-babel-hide-result-overlays
639 (delq ov org-babel-hide-result-overlays
)))
640 (when (eq (overlay-get ov
'invisible
)
641 'org-babel-hide-result
)
642 (delete-overlay ov
))))
643 (push ov org-babel-hide-result-overlays
)))
644 (error "Not looking at a result line"))))
646 ;; org-tab-after-check-for-cycling-hook
647 (add-hook 'org-tab-first-hook
'org-babel-hide-result-toggle-maybe
)
648 ;; Remove overlays when changing major mode
649 (add-hook 'org-mode-hook
650 (lambda () (org-add-hook 'change-major-mode-hook
651 'org-babel-show-result-all
'append
'local
)))
653 (defmacro org-babel-map-src-blocks
(file &rest body
)
654 "Evaluate BODY forms on each source-block in FILE."
656 `(let ((visited-p (get-file-buffer (expand-file-name ,file
)))
658 (save-window-excursion
660 (setq to-be-removed
(current-buffer))
661 (goto-char (point-min))
662 (while (re-search-forward org-babel-src-block-regexp nil t
)
663 (goto-char (match-beginning 0))
664 (save-match-data ,@body
)
665 (goto-char (match-end 0))))
667 (kill-buffer to-be-removed
))))
669 (defvar org-file-properties
)
670 (defun org-babel-params-from-properties (&optional lang
)
671 "Retrieve parameters specified as properties.
672 Return an association list of any source block params which
673 may be specified in the properties of the current outline entry."
680 (or (condition-case nil
681 (org-entry-get (point) header-arg t
)
683 (cdr (assoc header-arg org-file-properties
))))
684 (cons (intern (concat ":" header-arg
)) val
)))
688 org-babel-header-arg-names
690 (setq sym
(intern (concat "org-babel-header-arg-names:" lang
)))
691 (and (boundp sym
) (eval sym
))))))))))
693 (defun org-babel-params-from-buffer ()
694 "Retrieve per-buffer parameters.
695 Return an association list of any source block params which
696 may be specified at the top of the current buffer."
697 (or org-babel-current-buffer-properties
698 (setq org-babel-current-buffer-properties
703 (goto-char (point-min))
704 (when (re-search-forward
705 (org-make-options-regexp (list "BABEL")) nil t
)
706 (org-babel-parse-header-arguments
707 (org-match-string-no-properties 2)))))))))
709 (defvar org-src-preserve-indentation
)
710 (defun org-babel-parse-src-block-match ()
711 "Parse the results from a match of the `org-babel-src-block-regexp'."
712 (let* ((block-indentation (length (match-string 1)))
713 (lang (org-babel-clean-text-properties (match-string 2)))
714 (lang-headers (intern (concat "org-babel-default-header-args:" lang
)))
715 (switches (match-string 3))
716 (body (org-babel-clean-text-properties (match-string 5)))
717 (preserve-indentation (or org-src-preserve-indentation
718 (string-match "-i\\>" switches
))))
720 ;; get block body less properties, protective commas, and indentation
723 (insert (org-babel-strip-protective-commas body
))
724 (unless preserve-indentation
(org-do-remove-indentation))
726 (org-babel-merge-params
727 org-babel-default-header-args
728 (org-babel-params-from-buffer)
729 (org-babel-params-from-properties lang
)
730 (if (boundp lang-headers
) (eval lang-headers
) nil
)
731 (org-babel-parse-header-arguments
732 (org-babel-clean-text-properties (or (match-string 4) ""))))
736 (defun org-babel-parse-inline-src-block-match ()
737 "Parse the results from a match of the `org-babel-inline-src-block-regexp'."
738 (let* ((lang (org-babel-clean-text-properties (match-string 2)))
739 (lang-headers (intern (concat "org-babel-default-header-args:" lang
))))
741 (org-babel-strip-protective-commas
742 (org-babel-clean-text-properties (match-string 5)))
743 (org-babel-merge-params
744 org-babel-default-inline-header-args
745 (org-babel-params-from-buffer)
746 (org-babel-params-from-properties lang
)
747 (if (boundp lang-headers
) (eval lang-headers
) nil
)
748 (org-babel-parse-header-arguments
749 (org-babel-clean-text-properties (or (match-string 4) "")))))))
751 (defun org-babel-parse-header-arguments (arg-string)
752 "Parse a string of header arguments returning an alist."
753 (if (> (length arg-string
) 0)
758 "\\([^ \f\t\n\r\v]+\\)[ \f\t\n\r\v]+\\([^ \f\t\n\r\v]+.*\\)"
760 (cons (intern (concat ":" (match-string 1 arg
)))
761 (let ((raw (org-babel-chomp (match-string 2 arg
))))
762 (if (org-babel-number-p raw
)
763 raw
(org-babel-read raw
))))
764 (cons (intern (concat ":" arg
)) nil
)))
765 (split-string (concat " " arg-string
) "[ \f\t\n\r\v]+:" t
)))))
767 (defun org-babel-process-params (params)
768 "Parse params and resolve references.
770 Return a list (session vars result-params result-type colnames rownames)."
771 (let* ((session (cdr (assoc :session params
)))
772 (vars-and-names (org-babel-disassemble-tables
773 (org-babel-ref-variables params
)
774 (cdr (assoc :hlines params
))
775 (cdr (assoc :colnames params
))
776 (cdr (assoc :rownames params
))))
777 (vars (car vars-and-names
))
778 (colnames (cadr vars-and-names
))
779 (rownames (caddr vars-and-names
))
780 (result-params (split-string (or (cdr (assoc :results params
)) "")))
781 (result-type (cond ((member "output" result-params
) 'output
)
782 ((member "value" result-params
) 'value
)
784 (list session vars result-params result-type colnames rownames
)))
786 ;; row and column names
787 (defun org-babel-del-hlines (table)
788 "Remove all 'hlines from TABLE."
789 (remove 'hline table
))
791 (defun org-babel-get-colnames (table)
792 "Return the column names of TABLE.
793 Return a cons cell, the `car' of which contains the TABLE less
794 colnames, and the `cdr' of which contains a list of the column
796 (if (equal 'hline
(nth 1 table
))
797 (cons (cddr table
) (car table
))
798 (cons (cdr table
) (car table
))))
800 (defun org-babel-get-rownames (table)
801 "Return the row names of TABLE.
802 Return a cons cell, the `car' of which contains the TABLE less
803 colnames, and the `cdr' of which contains a list of the column
804 names. Note: this function removes any hlines in TABLE."
805 (flet ((trans (table) (apply #'mapcar
* #'list table
)))
806 (let* ((width (apply 'max
(mapcar (lambda (el) (if (listp el
) (length el
) 0)) table
)))
807 (table (trans (mapcar (lambda (row)
808 (if (not (equal row
'hline
))
811 (dotimes (n width
) (setq row
(cons 'hline row
)))
814 (cons (mapcar (lambda (row) (if (equal (car row
) 'hline
) 'hline row
))
816 (remove 'hline
(car table
))))))
818 (defun org-babel-put-colnames (table colnames
)
819 "Add COLNAMES to TABLE if they exist."
820 (if colnames
(apply 'list colnames
'hline table
) table
))
822 (defun org-babel-put-rownames (table rownames
)
823 "Add ROWNAMES to TABLE if they exist."
825 (mapcar (lambda (row)
827 (cons (or (pop rownames
) "") row
)
831 (defun org-babel-pick-name (names selector
)
832 "Select one out of an alist of row or column names."
834 (if (and selector
(symbolp selector
) (not (equal t selector
)))
835 (cdr (assoc selector names
))
836 (if (integerp selector
)
837 (nth (- selector
1) names
)
838 (cdr (car (last names
)))))))
840 (defun org-babel-disassemble-tables (vars hlines colnames rownames
)
841 "Parse tables for further processing.
842 Process the variables in VARS according to the HLINES,
843 ROWNAMES and COLNAMES header arguments. Return a list consisting
844 of the vars, cnames and rnames."
849 (when (listp (cdr var
))
850 (when (and (not (equal colnames
"no"))
851 (or colnames
(and (equal (nth 1 (cdr var
)) 'hline
)
852 (not (member 'hline
(cddr (cdr var
)))))))
853 (let ((both (org-babel-get-colnames (cdr var
))))
854 (setq cnames
(cons (cons (car var
) (cdr both
))
856 (setq var
(cons (car var
) (car both
)))))
857 (when (and rownames
(not (equal rownames
"no")))
858 (let ((both (org-babel-get-rownames (cdr var
))))
859 (setq rnames
(cons (cons (car var
) (cdr both
))
861 (setq var
(cons (car var
) (car both
)))))
862 (when (and hlines
(not (equal hlines
"yes")))
863 (setq var
(cons (car var
) (org-babel-del-hlines (cdr var
))))))
868 (defun org-babel-reassemble-table (table colnames rownames
)
869 "Add column and row names to a table.
870 Given a TABLE and set of COLNAMES and ROWNAMES add the names
871 to the table for reinsertion to org-mode."
874 (if (and colnames
(listp (car table
)) (= (length (car table
))
876 (org-babel-put-colnames table colnames
) table
))
877 (if (and rownames
(= (length table
) (length rownames
)))
878 (org-babel-put-rownames table rownames
) table
))
881 (defun org-babel-where-is-src-block-head ()
882 "Find where the current source block begins.
883 Return the point at the beginning of the current source
884 block. Specifically at the beginning of the #+BEGIN_SRC line.
885 If the point is not on a source block then return nil."
886 (let ((initial (point)) top bottom
)
888 (save-excursion ;; on a source name line
889 (beginning-of-line 1)
890 (and (looking-at org-babel-src-name-regexp
) (forward-line 1)
891 (looking-at org-babel-src-block-regexp
)
893 (save-excursion ;; on a #+begin_src line
894 (beginning-of-line 1)
895 (and (looking-at org-babel-src-block-regexp
)
897 (save-excursion ;; inside a src block
899 (re-search-backward "^[ \t]*#\\+begin_src" nil t
) (setq top
(point))
900 (re-search-forward "^[ \t]*#\\+end_src" nil t
) (setq bottom
(point))
901 (< top initial
) (< initial bottom
)
902 (progn (goto-char top
) (beginning-of-line 1)
903 (looking-at org-babel-src-block-regexp
))
907 (defun org-babel-goto-named-src-block (name)
908 "Go to a named source-code block."
910 (let ((completion-ignore-case t
))
911 (list (org-icompleting-read "source-block name: "
912 (org-babel-src-block-names) nil t
))))
913 (let ((point (org-babel-find-named-block name
)))
915 ;; taken from `org-open-at-point'
916 (progn (goto-char point
) (org-show-context))
917 (message "source-code block '%s' not found in this buffer" name
))))
919 (defun org-babel-find-named-block (name)
920 "Find a named source-code block.
921 Return the location of the source block identified by source
922 NAME, or nil if no such block exists. Set match data according to
923 org-babel-named-src-block-regexp."
925 (let ((case-fold-search t
)
926 (regexp (org-babel-named-src-block-regexp-for-name name
)) msg
)
927 (goto-char (point-min))
928 (when (or (re-search-forward regexp nil t
)
929 (re-search-backward regexp nil t
))
930 (match-beginning 0)))))
932 (defun org-babel-src-block-names (&optional file
)
933 "Returns the names of source blocks in FILE or the current buffer."
935 (when file
(find-file file
)) (goto-char (point-min))
937 (while (re-search-forward org-babel-src-name-w-name-regexp nil t
)
938 (setq names
(cons (org-babel-clean-text-properties (match-string 2))
943 (defun org-babel-goto-named-result (name)
944 "Go to a named result."
946 (let ((completion-ignore-case t
))
947 (list (org-icompleting-read "source-block name: "
948 (org-babel-result-names) nil t
))))
949 (let ((point (org-babel-find-named-result name
)))
951 ;; taken from `org-open-at-point'
952 (progn (goto-char point
) (org-show-context))
953 (message "result '%s' not found in this buffer" name
))))
955 (defun org-babel-find-named-result (name)
956 "Find a named result.
957 Return the location of the result named NAME in the current
958 buffer or nil if no such result exists."
960 (goto-char (point-min))
961 (when (re-search-forward
962 (concat org-babel-result-regexp
963 "[ \t]" (regexp-quote name
) "[ \t\n\f\v\r]") nil t
)
964 (beginning-of-line 0) (point))))
966 (defun org-babel-result-names (&optional file
)
967 "Returns the names of results in FILE or the current buffer."
969 (when file
(find-file file
)) (goto-char (point-min))
971 (while (re-search-forward org-babel-result-w-name-regexp nil t
)
972 (setq names
(cons (org-babel-clean-text-properties (match-string 4))
977 (defun org-babel-next-src-block (&optional arg
)
978 "Jump to the next source block.
979 With optional prefix argument ARG, jump forward ARG many source blocks."
981 (when (looking-at org-babel-src-block-regexp
) (forward-char 1))
982 (re-search-forward org-babel-src-block-regexp nil nil
(or arg
1))
983 (goto-char (match-beginning 0)) (org-show-context))
986 (defun org-babel-previous-src-block (&optional arg
)
987 "Jump to the previous source block.
988 With optional prefix argument ARG, jump backward ARG many source blocks."
990 (re-search-backward org-babel-src-block-regexp nil nil
(or arg
1))
991 (goto-char (match-beginning 0)) (org-show-context))
993 (defvar org-babel-lob-one-liner-regexp
)
994 (defun org-babel-where-is-src-block-result (&optional insert info hash indent
)
995 "Find where the current source block results begin.
996 Return the point at the beginning of the result of the current
997 source block. Specifically at the beginning of the results line.
998 If no result exists for this block then create a results line
999 following the source block."
1001 (let* ((on-lob-line (progn (beginning-of-line 1)
1002 (looking-at org-babel-lob-one-liner-regexp
)))
1003 (name (if on-lob-line
1004 (nth 0 (org-babel-lob-get-info))
1005 (nth 4 (or info
(org-babel-get-src-block-info)))))
1006 (head (unless on-lob-line
(org-babel-where-is-src-block-head)))
1008 (when head
(goto-char head
))
1010 found
;; was there a result (before we potentially insert one)
1014 ;; - return t if it is found, else return nil
1015 ;; - if it does not need to be rebuilt, then don't set end
1016 ;; - if it does need to be rebuilt then do set end
1017 name
(setq beg
(org-babel-find-named-result name
))
1019 (when (and hash
(not (string= hash
(match-string 3))))
1020 (goto-char beg
) (setq end beg
) ;; beginning of result
1022 (delete-region end
(org-babel-result-end)) nil
)))
1025 ;; - return t if it is found, else return nil
1026 ;; - if it is found, and the hash doesn't match, delete and set end
1027 (or on-lob-line
(re-search-forward "^[ \t]*#\\+end_src" nil t
))
1028 (progn (end-of-line 1)
1029 (if (eobp) (insert "\n") (forward-char 1))
1032 (progn ;; unnamed results line already exists
1033 (re-search-forward "[^ \f\t\n\r\v]" nil t
)
1034 (beginning-of-line 1)
1036 (concat org-babel-result-regexp
"\n")))
1038 ;; must remove and rebuild if hash!=old-hash
1039 (if (and hash
(not (string= hash
(match-string 3))))
1043 end
(org-babel-result-end)))
1044 (setq end nil
)))))))))
1045 (if (and insert end
)
1049 (if (looking-at "[\n\r]") (forward-char 1) (insert "\n")))
1054 (number-sequence 1 indent
) "")
1057 (when hash
(concat "["hash
"]"))
1059 (when name
(concat " " name
)) "\n"))
1060 (unless beg
(insert "\n") (backward-char))
1061 (beginning-of-line 0)
1062 (if hash
(org-babel-hide-hash))
1066 (defvar org-block-regexp
)
1067 (defun org-babel-read-result ()
1068 "Read the result at `point' into emacs-lisp."
1069 (let ((case-fold-search t
) result-string
)
1071 ((org-at-table-p) (org-babel-read-table))
1072 ((looking-at org-bracket-link-regexp
) (org-babel-read-link))
1073 ((looking-at org-block-regexp
) (org-babel-trim (match-string 4)))
1074 ((looking-at "^[ \t]*: ")
1077 (mapconcat (lambda (line)
1078 (if (and (> (length line
) 1)
1079 (string-match "^[ \t]*: \\(.+\\)" line
))
1080 (match-string 1 line
)
1084 (point) (org-babel-result-end)) "[\r\n]+")
1086 (or (org-babel-number-p result-string
) result-string
))
1087 ((looking-at org-babel-result-regexp
)
1088 (save-excursion (forward-line 1) (org-babel-read-result))))))
1090 (defun org-babel-read-table ()
1091 "Read the table at `point' into emacs-lisp."
1092 (mapcar (lambda (row)
1093 (if (and (symbolp row
) (equal row
'hline
)) row
1094 (mapcar #'org-babel-read row
)))
1095 (org-table-to-lisp)))
1097 (defvar org-link-types-re
)
1098 (defun org-babel-read-link ()
1099 "Read the link at `point' into emacs-lisp.
1100 If the path of the link is a file path it is expanded using
1101 `expand-file-name'."
1102 (let* ((case-fold-search t
)
1103 (raw (and (looking-at org-bracket-link-regexp
)
1104 (org-babel-clean-text-properties (match-string 1))))
1105 (type (and (string-match org-link-types-re raw
)
1106 (match-string 1 raw
))))
1108 ((not type
) (expand-file-name raw
))
1109 ((string= type
"file")
1110 (and (string-match "file\\(.*\\):\\(.+\\)" raw
)
1111 (expand-file-name (match-string 2 raw
))))
1114 (defun org-babel-insert-result
1115 (result &optional result-params info hash indent lang
)
1116 "Insert RESULT into the current buffer.
1117 By default RESULT is inserted after the end of the
1118 current source block. With optional argument RESULT-PARAMS
1119 controls insertion of results in the org-mode file.
1120 RESULT-PARAMS can take the following values...
1122 replace - (default option) insert results after the source block
1123 replacing any previously inserted results
1125 silent -- no results are inserted
1127 file ---- the results are interpreted as a file path, and are
1128 inserted into the buffer using the Org-mode file syntax
1130 raw ----- results are added directly to the org-mode file. This
1131 is a good option if you code block will output org-mode
1134 org ----- this is the same as the 'raw' option
1136 html ---- results are added inside of a #+BEGIN_HTML block. This
1137 is a good option if you code block will output html
1140 latex --- results are added inside of a #+BEGIN_LATEX block.
1141 This is a good option if you code block will output
1142 latex formatted text.
1144 code ---- the results are extracted in the syntax of the source
1145 code of the language being evaluated and are added
1146 inside of a #+BEGIN_SRC block with the source-code
1147 language set appropriately. Note this relies on the
1148 optional LANG argument."
1149 (if (stringp result
)
1151 (setq result
(org-babel-clean-text-properties result
))
1152 (when (member "file" result-params
)
1153 (setq result
(org-babel-result-to-file result
))))
1154 (unless (listp result
) (setq result
(format "%S" result
))))
1155 (if (= (length result
) 0)
1156 (if (member "value" result-params
)
1157 (message "No result returned by source block")
1158 (message "Source block produced no output"))
1159 (if (and result-params
(member "silent" result-params
))
1161 (message (replace-regexp-in-string "%" "%%" (format "%S" result
)))
1163 (when (and (stringp result
) ;; ensure results end in a newline
1164 (not (or (string-equal (substring result -
1) "\n")
1165 (string-equal (substring result -
1) "\r"))))
1166 (setq result
(concat result
"\n")))
1168 (let ((existing-result (org-babel-where-is-src-block-result
1169 t info hash indent
))
1171 (cdr (assoc :results_switches
(nth 2 info
))))
1173 (when existing-result
1174 (goto-char existing-result
)
1176 (re-search-forward "#" nil t
)
1177 (setq indent
(- (current-column) 1)))
1181 ((member "replace" result-params
)
1182 (delete-region (point) (org-babel-result-end)))
1183 ((member "append" result-params
)
1184 (goto-char (org-babel-result-end)) (setq beg
(point)))
1185 ((member "prepend" result-params
) ;; already there
1187 (setq results-switches
1188 (if results-switches
(concat " " results-switches
) ""))
1190 ;; assume the result is a table if it's not a string
1191 ((not (stringp result
))
1192 (insert (concat (orgtbl-to-orgtbl
1193 (if (or (eq 'hline
(car result
))
1194 (and (listp (car result
))
1195 (listp (cdr (car result
)))))
1196 result
(list result
))
1197 '(:fmt
(lambda (cell) (format "%s" cell
)))) "\n"))
1198 (goto-char beg
) (when (org-at-table-p) (org-table-align)))
1199 ((member "file" result-params
)
1201 ((member "html" result-params
)
1202 (insert (format "#+BEGIN_HTML%s\n%s#+END_HTML\n"
1203 results-switches result
)))
1204 ((member "latex" result-params
)
1205 (insert (format "#+BEGIN_LaTeX%s\n%s#+END_LaTeX\n"
1206 results-switches result
)))
1207 ((member "code" result-params
)
1208 (insert (format "#+BEGIN_SRC %s%s\n%s#+END_SRC\n"
1209 (or lang
"none") results-switches result
)))
1210 ((or (member "raw" result-params
) (member "org" result-params
))
1211 (save-excursion (insert result
)) (if (org-at-table-p) (org-cycle)))
1213 (org-babel-examplize-region
1214 (point) (progn (insert result
) (point)) results-switches
)))
1215 ;; possibly indent the results to match the #+results line
1216 (setq end
(if (listp result
) (org-table-end) (point)))
1217 (when (and indent
(> indent
0)
1218 ;; in this case `table-align' does the work for us
1219 (not (and (listp result
)
1220 (member "append" result-params
))))
1221 (indent-rigidly beg end indent
))))
1222 (message "finished"))))
1224 (defun org-babel-remove-result (&optional info
)
1225 "Remove the result of the current source block."
1227 (let ((location (org-babel-where-is-src-block-result nil info
)) start
)
1230 (goto-char location
) (setq start
(point)) (forward-line 1)
1231 (delete-region start
(org-babel-result-end))))))
1233 (defun org-babel-result-end ()
1234 "Return the point at the end of the current set of results"
1236 (if (org-at-table-p)
1237 (progn (goto-char (org-table-end)) (point))
1238 (let ((case-fold-search t
))
1240 ((looking-at "[ \t]*#\\+begin_latex")
1241 (re-search-forward "[ \t]*#\\+end_latex" nil t
)
1243 ((looking-at "[ \t]*#\\+begin_html")
1244 (re-search-forward "[ \t]*#\\+end_html" nil t
)
1246 ((looking-at "[ \t]*#\\+begin_example")
1247 (re-search-forward "[ \t]*#\\+end_example" nil t
)
1249 ((looking-at "[ \t]*#\\+begin_src")
1250 (re-search-forward "[ \t]*#\\+end_src" nil t
)
1252 (t (progn (while (looking-at "[ \t]*\\(: \\|\\[\\[\\)")
1253 (forward-line 1))))))
1256 (defun org-babel-result-to-file (result)
1257 "Convert RESULT into an `org-mode' link.
1258 If the `default-directory' is different from the containing
1259 file's directory then expand relative links."
1262 (if (and default-directory
1264 (not (string= (expand-file-name default-directory
)
1266 (file-name-directory buffer-file-name
)))))
1267 (expand-file-name result default-directory
)
1270 (defun org-babel-examplize-region (beg end
&optional results-switches
)
1271 "Comment out region using the ': ' org example quote."
1273 (let ((size (count-lines beg end
)))
1276 (error (concat "This should be impossible:"
1277 "a newline was appended to result if missing")))
1278 ((< size org-babel-min-lines-for-block-output
)
1281 (beginning-of-line 1) (insert ": ") (forward-line 1)))
1284 (insert (if results-switches
1285 (format "#+begin_example%s\n" results-switches
)
1286 "#+begin_example\n"))
1287 (forward-char (- end beg
))
1288 (insert "#+end_example\n"))))))
1290 (defun org-babel-merge-params (&rest plists
)
1291 "Combine all parameter association lists in PLISTS.
1292 Later elements of PLISTS override the values of previous element.
1293 This takes into account some special considerations for certain
1294 parameters when merging lists."
1295 (let ((results-exclusive-groups
1296 '(("file" "vector" "table" "scalar" "raw" "org"
1297 "html" "latex" "code" "pp")
1298 ("replace" "silent" "append" "prepend")
1299 ("output" "value")))
1300 (exports-exclusive-groups
1301 '(("code" "results" "both" "none")))
1302 params results exports tangle noweb cache vars var ref shebang comments
)
1303 (flet ((e-merge (exclusive-groups &rest result-params
)
1304 ;; maintain exclusivity of mutually exclusive parameters
1306 (mapc (lambda (new-params)
1307 (mapc (lambda (new-param)
1308 (mapc (lambda (exclusive-group)
1309 (when (member new-param exclusive-group
)
1310 (mapcar (lambda (excluded-param)
1317 (setq output
(org-uniquify
1318 (cons new-param output
))))
1322 (mapc (lambda (plist)
1323 (mapc (lambda (pair)
1326 ;; we want only one specification per variable
1328 (concat "^\\([^= \f\t\n\r\v]+\\)[ \t]*="
1329 "[ \t]*\\([^\f\n\r\v]+\\)$") (cdr pair
))
1330 ;; TODO: When is this not true?
1331 (setq var
(intern (match-string 1 (cdr pair
)))
1332 ref
(match-string 2 (cdr pair
))
1333 vars
(cons (cons var ref
)
1334 (assq-delete-all var vars
)))))
1337 (e-merge results-exclusive-groups
1338 results
(split-string (cdr pair
)))))
1341 (setq results
(e-merge results-exclusive-groups
1343 (unless (or (member "both" exports
)
1344 (member "none" exports
)
1345 (member "code" exports
))
1346 (setq exports
(e-merge exports-exclusive-groups
1347 exports
'("results"))))
1350 (assq-delete-all (car pair
) params
)))))
1353 (e-merge exports-exclusive-groups
1354 exports
(split-string (cdr pair
)))))
1355 (:tangle
;; take the latest -- always overwrite
1356 (setq tangle
(or (list (cdr pair
)) tangle
)))
1359 (e-merge '(("yes" "no")) noweb
1360 (split-string (or (cdr pair
) "")))))
1363 (e-merge '(("yes" "no")) cache
1364 (split-string (or (cdr pair
) "")))))
1365 (:shebang
;; take the latest -- always overwrite
1366 (setq shebang
(or (list (cdr pair
)) shebang
)))
1369 (e-merge '(("yes" "no")) comments
1370 (split-string (or (cdr pair
) "")))))
1371 (t ;; replace: this covers e.g. :session
1374 (assq-delete-all (car pair
) params
))))))
1377 (setq vars
(mapcar (lambda (pair) (format "%s=%s" (car pair
) (cdr pair
))) vars
))
1378 (while vars
(setq params
(cons (cons :var
(pop vars
)) params
)))
1379 (cons (cons :comments
(mapconcat 'identity comments
" "))
1380 (cons (cons :shebang
(mapconcat 'identity shebang
" "))
1381 (cons (cons :cache
(mapconcat 'identity cache
" "))
1382 (cons (cons :noweb
(mapconcat 'identity noweb
" "))
1383 (cons (cons :tangle
(mapconcat 'identity tangle
" "))
1384 (cons (cons :exports
1385 (mapconcat 'identity exports
" "))
1388 (mapconcat 'identity results
" "))
1391 (defun org-babel-expand-noweb-references (&optional info parent-buffer
)
1392 "Expand Noweb references in the body of the current source code block.
1394 For example the following reference would be replaced with the
1395 body of the source-code block named 'example-block'.
1399 Note that any text preceding the <<foo>> construct on a line will
1400 be interposed between the lines of the replacement text. So for
1401 example if <<foo>> is placed behind a comment, then the entire
1402 replacement text will also be commented.
1404 This function must be called from inside of the buffer containing
1405 the source-code block which holds BODY.
1407 In addition the following syntax can be used to insert the
1408 results of evaluating the source-code block named 'example-block'.
1412 Any optional arguments can be passed to example-block by placing
1413 the arguments inside the parenthesis following the convention
1414 defined by `org-babel-lob'. For example
1416 <<example-block(a=9)>>
1418 would set the value of argument \"a\" equal to \"9\". Note that
1419 these arguments are not evaluated in the current source-code
1420 block but are passed literally to the \"example-block\"."
1421 (let* ((parent-buffer (or parent-buffer
(current-buffer)))
1422 (info (or info
(org-babel-get-src-block-info)))
1425 (new-body "") index source-name evaluate prefix
)
1426 (flet ((nb-add (text)
1427 (setq new-body
(concat new-body text
))))
1429 (insert body
) (goto-char (point-min))
1430 (setq index
(point))
1431 (while (and (re-search-forward "<<\\(.+?\\)>>" nil t
))
1432 (save-match-data (setf source-name
(match-string 1)))
1433 (save-match-data (setq evaluate
(string-match "\(.*\)" source-name
)))
1436 (buffer-substring (match-beginning 0)
1438 (beginning-of-line 1) (point)))))
1439 ;; add interval to new-body (removing noweb reference)
1440 (goto-char (match-beginning 0))
1441 (nb-add (buffer-substring index
(point)))
1442 (goto-char (match-end 0))
1443 (setq index
(point))
1444 (nb-add (with-current-buffer parent-buffer
1445 (mapconcat ;; interpose PREFIX between every line
1449 (let ((raw (org-babel-ref-resolve-reference
1451 (if (stringp raw
) raw
(format "%S" raw
)))
1454 (let ((point (org-babel-find-named-block
1460 (org-babel-expand-noweb-references
1461 (org-babel-get-src-block-info))))
1462 ;; optionally raise an error if named
1463 ;; source-block doesn't exist
1464 (if (member lang org-babel-noweb-error-langs
)
1467 "<<" source-name
">> "
1468 "could not be resolved (see "
1469 "`org-babel-noweb-error-langs')"))
1471 "[\n\r]") (concat "\n" prefix
)))))
1472 (nb-add (buffer-substring index
(point-max)))))
1475 (defun org-babel-clean-text-properties (text)
1476 "Strip all properties from text return."
1478 (set-text-properties 0 (length text
) nil text
) text
))
1480 (defun org-babel-strip-protective-commas (body)
1481 "Strip protective commas from bodies of source blocks."
1482 (replace-regexp-in-string "^,#" "#" body
))
1484 (defun org-babel-read (cell)
1485 "Convert the string value of CELL to a number if appropriate.
1486 Otherwise if cell looks like lisp (meaning it starts with a
1487 \"(\" or a \"'\") then read it as lisp, otherwise return it
1488 unmodified as a string.
1490 This is taken almost directly from `org-read-prop'."
1491 (if (and (stringp cell
) (not (equal cell
"")))
1492 (or (org-babel-number-p cell
)
1493 (if (or (equal "(" (substring cell
0 1))
1494 (equal "'" (substring cell
0 1))
1495 (equal "`" (substring cell
0 1)))
1497 (progn (set-text-properties 0 (length cell
) nil cell
) cell
)))
1500 (defun org-babel-number-p (string)
1501 "Return t if STRING represents a number."
1502 (if (and (string-match "^-?[0-9]*\\.?[0-9]*$" string
)
1503 (= (length (substring string
(match-beginning 0)
1506 (string-to-number string
)))
1508 (defun org-babel-import-elisp-from-file (file-name)
1509 "Read the results located at FILE-NAME into an elisp table.
1510 If the table is trivial, then return it as a scalar."
1512 (save-window-excursion
1516 (org-table-import file-name nil
)
1517 (delete-file file-name
)
1518 (setq result
(mapcar (lambda (row)
1519 (mapcar #'org-babel-string-read row
))
1520 (org-table-to-lisp))))
1522 (if (null (cdr result
)) ;; if result is trivial vector, then scalarize it
1523 (if (consp (car result
))
1524 (if (null (cdr (car result
)))
1530 (defun org-babel-string-read (cell)
1531 "Strip nested \"s from around strings."
1532 (org-babel-read (or (and (stringp cell
)
1533 (string-match "\\\"\\(.+\\)\\\"" cell
)
1534 (match-string 1 cell
))
1537 (defun org-babel-reverse-string (string)
1538 "Return the reverse of STRING."
1539 (apply 'string
(reverse (string-to-list string
))))
1541 (defun org-babel-chomp (string &optional regexp
)
1542 "Strip trailing spaces and carriage returns from STRING.
1543 Default regexp used is \"[ \f\t\n\r\v]\" but can be
1544 overwritten by specifying a regexp as a second argument."
1545 (let ((regexp (or regexp
"[ \f\t\n\r\v]")))
1546 (while (and (> (length string
) 0)
1547 (string-match regexp
(substring string -
1)))
1548 (setq string
(substring string
0 -
1)))
1551 (defun org-babel-trim (string &optional regexp
)
1552 "Strip leading and trailing spaces and carriage returns from STRING.
1553 Like `org-babel-chomp' only it runs on both the front and back
1555 (org-babel-chomp (org-babel-reverse-string
1556 (org-babel-chomp (org-babel-reverse-string string
) regexp
))
1559 (defvar org-babel-org-babel-call-process-region-original nil
)
1560 (defun org-babel-tramp-handle-call-process-region
1561 (start end program
&optional delete buffer display
&rest args
)
1562 "Use tramp to handle call-process-region.
1563 Fixes a bug in `tramp-handle-call-process-region'."
1564 (if (and (featurep 'tramp
) (file-remote-p default-directory
))
1565 (let ((tmpfile (tramp-compat-make-temp-file "")))
1566 (write-region start end tmpfile
)
1567 (when delete
(delete-region start end
))
1569 ;; (apply 'call-process program tmpfile buffer display args)
1571 (apply 'process-file program tmpfile buffer display args
)
1572 (delete-file tmpfile
)))
1573 ;; org-babel-call-process-region-original is the original emacs definition. It
1574 ;; is in scope from the let binding in org-babel-execute-src-block
1575 (apply org-babel-call-process-region-original
1576 start end program delete buffer display args
)))
1578 (defun org-babel-maybe-remote-file (file)
1579 "Conditionally parse information on a remote connnection.
1580 If FILE specifies a remove file, then parse the information on
1581 the remote connection."
1582 (if (file-remote-p default-directory
)
1583 (let* ((vec (tramp-dissect-file-name default-directory
))
1584 (user (tramp-file-name-user vec
))
1585 (host (tramp-file-name-host vec
)))
1586 (concat "/" user
(when user
"@") host
":" file
))
1591 ;; arch-tag: 01a7ebee-06c5-4ee4-a709-e660d28c0af1