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/
37 (defvar org-babel-call-process-region-original
)
38 (defvar org-src-lang-modes
)
39 (defvar org-babel-library-of-babel
)
40 (declare-function show-all
"outline" ())
41 (declare-function tramp-compat-make-temp-file
"tramp-compat"
42 (filename &optional dir-flag
))
43 (declare-function tramp-dissect-file-name
"tramp" (name &optional nodefault
))
44 (declare-function tramp-file-name-user
"tramp" (vec))
45 (declare-function tramp-file-name-host
"tramp" (vec))
46 (declare-function with-parsed-tramp-file-name
"tramp" (filename var
&rest body
))
47 (declare-function org-icompleting-read
"org" (&rest args
))
48 (declare-function org-edit-src-code
"org-src"
49 (&optional context code edit-buffer-name quietp
))
50 (declare-function org-edit-src-exit
"org-src" (&optional context
))
51 (declare-function org-open-at-point
"org" (&optional in-emacs reference-buffer
))
52 (declare-function org-save-outline-visibility
"org" (use-markers &rest body
))
53 (declare-function org-outline-overlay-data
"org" (&optional use-markers
))
54 (declare-function org-set-outline-overlay-data
"org" (data))
55 (declare-function org-narrow-to-subtree
"org" ())
56 (declare-function org-entry-get
"org"
57 (pom property
&optional inherit literal-nil
))
58 (declare-function org-make-options-regexp
"org" (kwds &optional extra
))
59 (declare-function org-do-remove-indentation
"org" (&optional n
))
60 (declare-function org-show-context
"org" (&optional key
))
61 (declare-function org-at-table-p
"org" (&optional table-type
))
62 (declare-function org-cycle
"org" (&optional arg
))
63 (declare-function org-uniquify
"org" (list))
64 (declare-function org-current-level
"org" ())
65 (declare-function org-table-import
"org-table" (file arg
))
66 (declare-function org-add-hook
"org-compat"
67 (hook function
&optional append local
))
68 (declare-function org-table-align
"org-table" ())
69 (declare-function org-table-end
"org-table" (&optional table-type
))
70 (declare-function orgtbl-to-generic
"org-table" (table params
))
71 (declare-function orgtbl-to-orgtbl
"org-table" (table params
))
72 (declare-function org-babel-tangle-comment-links
"ob-tangle" (&optional info
))
73 (declare-function org-babel-lob-get-info
"ob-lob" nil
)
74 (declare-function org-babel-ref-split-args
"ob-ref" (arg-string))
75 (declare-function org-babel-ref-parse
"ob-ref" (assignment))
76 (declare-function org-babel-ref-resolve
"ob-ref" (ref))
77 (declare-function org-babel-lob-execute-maybe
"ob-lob" ())
78 (declare-function org-number-sequence
"org-compat" (from &optional to inc
))
79 (declare-function org-at-item-p
"org-list" ())
80 (declare-function org-list-parse-list
"org-list" (&optional delete
))
81 (declare-function org-list-to-generic
"org-list" (LIST PARAMS
))
82 (declare-function org-list-struct
"org-list" ())
83 (declare-function org-list-prevs-alist
"org-list" (struct))
84 (declare-function org-list-get-list-end
"org-list" (item struct prevs
))
86 (defgroup org-babel nil
87 "Code block evaluation and management in `org-mode' documents."
91 (defcustom org-confirm-babel-evaluate t
92 "Confirm before evaluation.
93 Require confirmation before interactively evaluating code
94 blocks in Org-mode buffers. The default value of this variable
95 is t, meaning confirmation is required for any code block
96 evaluation. This variable can be set to nil to inhibit any
97 future confirmation requests. This variable can also be set to a
98 function which takes two arguments the language of the code block
99 and the body of the code block. Such a function should then
100 return a non-nil value if the user should be prompted for
101 execution or nil if no prompt is required.
103 Warning: Disabling confirmation may result in accidental
104 evaluation of potentially harmful code. It may be advisable
105 remove code block execution from C-c C-c as further protection
106 against accidental code block evaluation. The
107 `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can be used to
108 remove code block execution from the C-c C-c keybinding."
110 :type
'(choice boolean function
))
111 ;; don't allow this variable to be changed through file settings
112 (put 'org-confirm-babel-evaluate
'safe-local-variable
(lambda (x) (eq x t
)))
114 (defcustom org-babel-no-eval-on-ctrl-c-ctrl-c nil
115 "Remove code block evaluation from the C-c C-c key binding."
119 (defvar org-babel-src-name-regexp
120 "^[ \t]*#\\+\\(srcname\\|source\\|function\\):[ \t]*"
121 "Regular expression used to match a source name line.")
123 (defvar org-babel-multi-line-header-regexp
124 "^[ \t]*#\\+headers?:[ \t]*\\([^\n]*\\)$"
125 "Regular expression used to match multi-line header arguments.")
127 (defvar org-babel-src-name-w-name-regexp
128 (concat org-babel-src-name-regexp
130 org-babel-multi-line-header-regexp
132 "\\([^ ()\f\t\n\r\v]+\\)\\(\(\\(.*\\)\)\\|\\)")
133 "Regular expression matching source name lines with a name.")
135 (defvar org-babel-src-block-regexp
137 ;; (1) indentation (2) lang
138 "^\\([ \t]*\\)#\\+begin_src[ \t]+\\([^ \f\t\n\r\v]+\\)[ \t]*"
140 "\\([^\":\n]*\"[^\"\n*]*\"[^\":\n]*\\|[^\":\n]*\\)"
141 ;; (4) header arguments
144 "\\([^\000]*?\\)[ \t]*#\\+end_src")
145 "Regexp used to identify code blocks.")
147 (defvar org-babel-inline-src-block-regexp
149 ;; (1) replacement target (2) lang
150 "[^-[:alnum:]]\\(src_\\([^ \f\t\n\r\v]+\\)"
151 ;; (3,4) (unused, headers)
152 "\\(\\|\\[\\(.*?\\)\\]\\)"
154 "{\\([^\f\n\r\v]+?\\)}\\)")
155 "Regexp used to identify inline src-blocks.")
157 (defun org-babel-get-header (params key
&optional others
)
158 "Select only header argument of type KEY from a list.
159 Optional argument OTHERS indicates that only the header that do
160 not match KEY should be returned."
163 (lambda (p) (when (funcall (if others
#'not
#'identity
) (eq (car p
) key
)) p
))
166 (defun org-babel-get-src-block-info (&optional light
)
167 "Get information on the current source block.
169 Optional argument LIGHT does not resolve remote variable
170 references; a process which could likely result in the execution
171 of other code blocks.
174 (language body header-arguments-alist switches name indent)."
175 (let ((case-fold-search t
) head info name indent
)
177 (if (setq head
(org-babel-where-is-src-block-head))
180 (setq info
(org-babel-parse-src-block-match))
181 (setq indent
(car (last info
)))
182 (setq info
(butlast info
))
183 (while (and (forward-line -
1)
184 (looking-at org-babel-multi-line-header-regexp
))
186 (org-babel-merge-params
188 (org-babel-parse-header-arguments (match-string 1)))))
189 (when (looking-at org-babel-src-name-w-name-regexp
)
190 (setq name
(org-babel-clean-text-properties (match-string 4)))
191 (when (match-string 6)
192 (setf (nth 2 info
) ;; merge functional-syntax vars and header-args
193 (org-babel-merge-params
194 (mapcar (lambda (ref) (cons :var ref
))
195 (org-babel-ref-split-args (match-string 6)))
197 ;; inline source block
198 (when (save-excursion (re-search-backward "[ \f\t\n\r\v]" nil t
)
199 (looking-at org-babel-inline-src-block-regexp
))
200 (setq info
(org-babel-parse-inline-src-block-match))))
201 ;; resolve variable references and add summary parameters
202 (when (and info
(not light
))
203 (setf (nth 2 info
) (org-babel-process-params (nth 2 info
))))
204 (when info
(append info
(list name indent
)))))
206 (defun org-babel-confirm-evaluate (info)
207 "Confirm evaluation of the code block INFO.
208 This behavior can be suppressed by setting the value of
209 `org-confirm-babel-evaluate' to nil, in which case all future
210 interactive code block evaluations will proceed without any
211 confirmation from the user.
213 Note disabling confirmation may result in accidental evaluation
214 of potentially harmful code."
215 (let* ((eval (or (cdr (assoc :eval
(nth 2 info
)))
216 (when (assoc :noeval
(nth 2 info
)) "no")))
217 (query (cond ((equal eval
"query") t
)
218 ((functionp org-confirm-babel-evaluate
)
219 (funcall org-confirm-babel-evaluate
220 (nth 0 info
) (nth 1 info
)))
221 (t org-confirm-babel-evaluate
))))
222 (if (or (equal eval
"never") (equal eval
"no")
225 (format "Evaluate this%scode block%son your system? "
226 (if info
(format " %s " (nth 0 info
)) " ")
228 (format " (%s) " (nth 4 info
)) " "))))))
229 (prog1 nil
(message "Evaluation %s"
230 (if (or (equal eval
"never") (equal eval
"no"))
231 "Disabled" "Aborted")))
235 (defun org-babel-execute-safely-maybe ()
236 (unless org-babel-no-eval-on-ctrl-c-ctrl-c
237 (org-babel-execute-maybe)))
239 (add-hook 'org-ctrl-c-ctrl-c-hook
'org-babel-execute-safely-maybe
)
242 (defun org-babel-execute-maybe ()
244 (or (org-babel-execute-src-block-maybe)
245 (org-babel-lob-execute-maybe)))
247 (defun org-babel-execute-src-block-maybe ()
248 "Conditionally execute a source block.
249 Detect if this is context for a Babel src-block and if so
250 then run `org-babel-execute-src-block'."
252 (let ((info (org-babel-get-src-block-info)))
254 (progn (org-babel-eval-wipe-error-buffer)
255 (org-babel-execute-src-block current-prefix-arg info
) t
) nil
)))
258 (defun org-babel-expand-src-block-maybe ()
259 "Conditionally expand a source block.
260 Detect if this is context for a org-babel src-block and if so
261 then run `org-babel-expand-src-block'."
263 (let ((info (org-babel-get-src-block-info)))
265 (progn (org-babel-expand-src-block current-prefix-arg info
) t
)
269 (defun org-babel-load-in-session-maybe ()
270 "Conditionally load a source block in a session.
271 Detect if this is context for a org-babel src-block and if so
272 then run `org-babel-load-in-session'."
274 (let ((info (org-babel-get-src-block-info)))
276 (progn (org-babel-load-in-session current-prefix-arg info
) t
)
279 (add-hook 'org-metaup-hook
'org-babel-load-in-session-maybe
)
282 (defun org-babel-pop-to-session-maybe ()
283 "Conditionally pop to a session.
284 Detect if this is context for a org-babel src-block and if so
285 then run `org-babel-pop-to-session'."
287 (let ((info (org-babel-get-src-block-info)))
288 (if info
(progn (org-babel-pop-to-session current-prefix-arg info
) t
) nil
)))
290 (add-hook 'org-metadown-hook
'org-babel-pop-to-session-maybe
)
292 (defconst org-babel-header-arg-names
293 '(cache cmdline colnames dir exports file noweb results
294 session tangle var eval noeval comments no-expand shebang padline
)
295 "Common header arguments used by org-babel.
296 Note that individual languages may define their own language
297 specific header arguments as well.")
299 (defvar org-babel-default-header-args
300 '((:session .
"none") (:results .
"replace") (:exports .
"code")
301 (:cache .
"no") (:noweb .
"no") (:hlines .
"no") (:tangle .
"no")
302 (:padnewline .
"yes"))
303 "Default arguments to use when evaluating a source block.")
305 (defvar org-babel-default-inline-header-args
306 '((:session .
"none") (:results .
"replace") (:exports .
"results"))
307 "Default arguments to use when evaluating an inline source block.")
309 (defvar org-babel-data-names
'("TBLNAME" "RESNAME" "RESULTS" "DATA"))
311 (defvar org-babel-result-regexp
312 (concat "^[ \t]*#\\+"
313 (regexp-opt org-babel-data-names t
)
314 "\\(\\[\\([[:alnum:]]+\\)\\]\\)?\\:[ \t]*")
315 "Regular expression used to match result lines.
316 If the results are associated with a hash key then the hash will
317 be saved in the second match data.")
319 (defvar org-babel-result-w-name-regexp
320 (concat org-babel-result-regexp
321 "\\([^ ()\f\t\n\r\v]+\\)\\(\(\\(.*\\)\)\\|\\)"))
323 (defvar org-babel-min-lines-for-block-output
10
324 "The minimum number of lines for block output.
325 If number of lines of output is equal to or exceeds this
326 value, the output is placed in a #+begin_example...#+end_example
327 block. Otherwise the output is marked as literal by inserting
328 colons at the starts of the lines. This variable only takes
329 effect if the :results output option is in effect.")
331 (defvar org-babel-noweb-error-langs nil
332 "Languages for which Babel will raise literate programming errors.
333 List of languages for which errors should be raised when the
334 source code block satisfying a noweb reference in this language
335 can not be resolved.")
337 (defvar org-babel-hash-show
4
338 "Number of initial characters to show of a hidden results hash.")
340 (defvar org-babel-after-execute-hook nil
341 "Hook for functions to be called after `org-babel-execute-src-block'")
342 (defun org-babel-named-src-block-regexp-for-name (name)
343 "This generates a regexp used to match a src block named NAME."
344 (concat org-babel-src-name-regexp
(regexp-quote name
) "[ \t\n]*"
345 (substring org-babel-src-block-regexp
1)))
348 (defvar call-process-region
)
351 (defun org-babel-execute-src-block (&optional arg info params
)
352 "Execute the current source code block.
353 Insert the results of execution into the buffer. Source code
354 execution and the collection and formatting of results can be
355 controlled through a variety of header arguments.
357 With prefix argument ARG, force re-execution even if a an
358 existing result cached in the buffer would otherwise have been
361 Optionally supply a value for INFO in the form returned by
362 `org-babel-get-src-block-info'.
364 Optionally supply a value for PARAMS which will be merged with
365 the header arguments specified at the front of the source code
368 (let ((info (or info
(org-babel-get-src-block-info))))
369 (when (org-babel-confirm-evaluate info
)
370 (let* ((lang (nth 0 info
))
372 (org-babel-process-params
373 (org-babel-merge-params (nth 2 info
) params
))
375 (cache?
(and (not arg
) (cdr (assoc :cache params
))
376 (string= "yes" (cdr (assoc :cache params
)))))
377 (result-params (cdr (assoc :result-params params
)))
378 (new-hash (when cache?
(org-babel-sha1-hash info
)))
379 (old-hash (when cache?
(org-babel-result-hash info
)))
380 (body (setf (nth 1 info
)
381 (let ((noweb (cdr (assoc :noweb params
))))
383 (or (string= "yes" noweb
)
384 (string= "tangle" noweb
)))
385 (org-babel-expand-noweb-references info
)
387 (dir (cdr (assoc :dir params
)))
389 (or (and dir
(file-name-as-directory dir
)) default-directory
))
390 (org-babel-call-process-region-original
391 (if (boundp 'org-babel-call-process-region-original
)
392 org-babel-call-process-region-original
393 (symbol-function 'call-process-region
)))
394 (indent (car (last info
)))
397 (flet ((call-process-region (&rest args
)
398 (apply 'org-babel-tramp-handle-call-process-region args
)))
399 (flet ((lang-check (f)
400 (let ((f (intern (concat "org-babel-execute:" f
))))
401 (when (fboundp f
) f
))))
403 (or (lang-check lang
)
404 (lang-check (symbol-name
405 (cdr (assoc lang org-src-lang-modes
))))
406 (error "No org-babel-execute function for %s!" lang
))))
407 (if (and (not arg
) new-hash
(equal new-hash old-hash
))
408 (save-excursion ;; return cached result
409 (goto-char (org-babel-where-is-src-block-result nil info
))
410 (end-of-line 1) (forward-char 1)
411 (setq result
(org-babel-read-result))
412 (message (replace-regexp-in-string
413 "%" "%%" (format "%S" result
))) result
)
414 (message "executing %s code block%s..."
416 (if (nth 4 info
) (format " (%s)" (nth 4 info
)) ""))
419 (if (and (eq (cdr (assoc :result-type params
)) 'value
)
420 (or (member "vector" result-params
)
421 (member "table" result-params
))
422 (not (listp result
)))
423 (list (list result
)) result
))
424 (funcall cmd body params
)))
425 ;; if non-empty result and :file then write to :file
426 (when (cdr (assoc :file params
))
428 (with-temp-file (cdr (assoc :file params
))
430 (org-babel-format-result
431 result
(cdr (assoc :sep
(nth 2 info
)))))))
432 (setq result
(cdr (assoc :file params
))))
433 (org-babel-insert-result
434 result result-params info new-hash indent lang
)
435 (run-hooks 'org-babel-after-execute-hook
)
437 (setq call-process-region
'org-babel-call-process-region-original
))))))
439 (defun org-babel-expand-body:generic
(body params
&optional var-lines
)
440 "Expand BODY with PARAMS.
441 Expand a block of code with org-babel according to it's header
442 arguments. This generic implementation of body expansion is
443 called for languages which have not defined their own specific
444 org-babel-expand-body:lang function."
445 (mapconcat #'identity
(append var-lines
(list body
)) "\n"))
448 (defun org-babel-expand-src-block (&optional arg info params
)
449 "Expand the current source code block.
450 Expand according to the source code block's header
451 arguments and pop open the results in a preview buffer."
453 (let* ((info (or info
(org-babel-get-src-block-info)))
455 (params (setf (nth 2 info
)
456 (sort (org-babel-merge-params (nth 2 info
) params
)
457 (lambda (el1 el2
) (string< (symbol-name (car el1
))
458 (symbol-name (car el2
)))))))
459 (body (setf (nth 1 info
)
460 (if (and (cdr (assoc :noweb params
))
461 (string= "yes" (cdr (assoc :noweb params
))))
462 (org-babel-expand-noweb-references info
) (nth 1 info
))))
463 (expand-cmd (intern (concat "org-babel-expand-body:" lang
)))
464 (assignments-cmd (intern (concat "org-babel-variable-assignments:"
467 (if (fboundp expand-cmd
) (funcall expand-cmd body params
)
468 (org-babel-expand-body:generic
469 body params
(and (fboundp assignments-cmd
)
470 (funcall assignments-cmd params
))))))
472 nil expanded
(concat "*Org-Babel Preview " (buffer-name) "[ " lang
" ]*"))))
474 (defun org-babel-edit-distance (s1 s2
)
475 "Return the edit (levenshtein) distance between strings S1 S2."
476 (let* ((l1 (length s1
))
478 (dist (map 'vector
(lambda (_) (make-vector (1+ l2
) nil
))
479 (number-sequence 1 (1+ l1
)))))
480 (flet ((in (i j
) (aref (aref dist i
) j
))
481 (mmin (&rest lst
) (apply #'min
(remove nil lst
))))
482 (setf (aref (aref dist
0) 0) 0)
483 (dolist (i (number-sequence 1 l1
))
484 (dolist (j (number-sequence 1 l2
))
485 (setf (aref (aref dist i
) j
)
486 (+ (if (equal (aref s1
(1- i
)) (aref s2
(1- j
))) 0 1)
487 (mmin (in (1- i
) j
) (in i
(1- j
)) (in (1- i
) (1- j
)))))))
491 (defun org-babel-check-src-block ()
492 "Check for misspelled header arguments in the current code block."
494 ;; TODO: report malformed code block
495 ;; TODO: report incompatible combinations of header arguments
496 (let ((too-close 2)) ;; <- control closeness to report potential match
497 (dolist (header (mapcar (lambda (arg) (substring (symbol-name (car arg
)) 1))
498 (and (org-babel-where-is-src-block-head)
499 (org-babel-parse-header-arguments
500 (org-babel-clean-text-properties
501 (match-string 4))))))
502 (dolist (name (mapcar #'symbol-name org-babel-header-arg-names
))
503 (when (and (not (string= header name
))
504 (<= (org-babel-edit-distance header name
) too-close
))
505 (error "supplied header \"%S\" is suspiciously close to \"%S\""
507 (message "No suspicious header arguments found.")))
510 (defun org-babel-load-in-session (&optional arg info
)
511 "Load the body of the current source-code block.
512 Evaluate the header arguments for the source block before
513 entering the session. After loading the body this pops open the
516 (let* ((info (or info
(org-babel-get-src-block-info)))
518 (params (nth 2 info
))
519 (body (setf (nth 1 info
)
520 (if (and (cdr (assoc :noweb params
))
521 (string= "yes" (cdr (assoc :noweb params
))))
522 (org-babel-expand-noweb-references info
)
524 (session (cdr (assoc :session params
)))
525 (dir (cdr (assoc :dir params
)))
527 (or (and dir
(file-name-as-directory dir
)) default-directory
))
528 (cmd (intern (concat "org-babel-load-session:" lang
))))
529 (unless (fboundp cmd
)
530 (error "No org-babel-load-session function for %s!" lang
))
531 (pop-to-buffer (funcall cmd session body params
))
535 (defun org-babel-initiate-session (&optional arg info
)
536 "Initiate session for current code block.
537 If called with a prefix argument then resolve any variable
538 references in the header arguments and assign these variables in
539 the session. Copy the body of the code block to the kill ring."
541 (let* ((info (or info
(org-babel-get-src-block-info (not arg
))))
544 (params (nth 2 info
))
545 (session (cdr (assoc :session params
)))
546 (dir (cdr (assoc :dir params
)))
548 (or (and dir
(file-name-as-directory dir
)) default-directory
))
549 (init-cmd (intern (format "org-babel-%s-initiate-session" lang
)))
550 (prep-cmd (intern (concat "org-babel-prep-session:" lang
))))
551 (if (and (stringp session
) (string= session
"none"))
552 (error "This block is not using a session!"))
553 (unless (fboundp init-cmd
)
554 (error "No org-babel-initiate-session function for %s!" lang
))
555 (with-temp-buffer (insert (org-babel-trim body
))
556 (copy-region-as-kill (point-min) (point-max)))
558 (unless (fboundp prep-cmd
)
559 (error "No org-babel-prep-session function for %s!" lang
))
560 (funcall prep-cmd session params
))
561 (funcall init-cmd session params
)))
564 (defun org-babel-switch-to-session (&optional arg info
)
565 "Switch to the session of the current code block.
566 Uses `org-babel-initiate-session' to start the session. If called
567 with a prefix argument then this is passed on to
568 `org-babel-initiate-session'."
570 (pop-to-buffer (org-babel-initiate-session arg info
))
573 (defalias 'org-babel-pop-to-session
'org-babel-switch-to-session
)
576 (defun org-babel-switch-to-session-with-code (&optional arg info
)
577 "Switch to code buffer and display session."
581 (let ((other-window-buffer (window-buffer (next-window))))
582 (set-window-buffer (next-window) (current-buffer))
583 (set-window-buffer (selected-window) other-window-buffer
))
585 (let ((info (org-babel-get-src-block-info))
586 (org-src-window-setup 'reorganize-frame
))
588 (org-babel-switch-to-session arg info
))
592 (defmacro org-babel-do-in-edit-buffer
(&rest body
)
593 "Evaluate BODY in edit buffer if there is a code block at point.
594 Return t if a code block was found at point, nil otherwise."
595 `(let ((org-src-window-setup 'switch-invisibly
))
596 (when (and (org-babel-where-is-src-block-head)
597 (org-edit-src-code nil nil nil
))
598 (unwind-protect (progn ,@body
)
599 (if (org-bound-and-true-p org-edit-src-from-org-mode
)
600 (org-edit-src-exit)))
603 (defun org-babel-do-key-sequence-in-edit-buffer (key)
604 "Read key sequence and execute the command in edit buffer.
605 Enter a key sequence to be executed in the language major-mode
606 edit buffer. For example, TAB will alter the contents of the
607 Org-mode code block according to the effect of TAB in the
608 language major-mode buffer. For languages that support
609 interactive sessions, this can be used to send code from the Org
610 buffer to the session for evaluation using the native major-mode
611 evaluation mechanisms."
612 (interactive "kEnter key-sequence to execute in edit buffer: ")
613 (org-babel-do-in-edit-buffer
615 (key-binding (or key
(read-key-sequence nil
))))))
617 (defvar org-bracket-link-regexp
)
619 (defun org-babel-open-src-block-result (&optional re-run
)
620 "If `point' is on a src block then open the results of the
621 source code block, otherwise return nil. With optional prefix
622 argument RE-RUN the source-code block is evaluated even if
623 results already exist."
625 (let ((info (org-babel-get-src-block-info)))
628 ;; go to the results, if there aren't any then run the block
629 (goto-char (or (and (not re-run
) (org-babel-where-is-src-block-result))
630 (progn (org-babel-execute-src-block)
631 (org-babel-where-is-src-block-result))))
633 (while (looking-at "[\n\r\t\f ]") (forward-char 1))
635 (if (looking-at org-bracket-link-regexp
)
638 (let ((r (org-babel-format-result
639 (org-babel-read-result) (cdr (assoc :sep
(nth 2 info
))))))
640 (pop-to-buffer (get-buffer-create "*Org-Babel Results*"))
641 (delete-region (point-min) (point-max))
646 (defmacro org-babel-map-src-blocks
(file &rest body
)
647 "Evaluate BODY forms on each source-block in FILE.
648 If FILE is nil evaluate BODY forms on source blocks in current
649 buffer. During evaluation of BODY the following local variables
650 are set relative to the currently matched code block.
652 full-block ------- string holding the entirety of the code block
653 beg-block -------- point at the beginning of the code block
654 end-block -------- point at the end of the matched code block
655 lang ------------- string holding the language of the code block
656 beg-lang --------- point at the beginning of the lang
657 end-lang --------- point at the end of the lang
658 switches --------- string holding the switches
659 beg-switches ----- point at the beginning of the switches
660 end-switches ----- point at the end of the switches
661 header-args ------ string holding the header-args
662 beg-header-args -- point at the beginning of the header-args
663 end-header-args -- point at the end of the header-args
664 body ------------- string holding the body of the code block
665 beg-body --------- point at the beginning of the body
666 end-body --------- point at the end of the body"
668 (let ((tempvar (make-symbol "file")))
669 `(let* ((,tempvar
,file
)
670 (visited-p (or (null ,tempvar
)
671 (get-file-buffer (expand-file-name ,tempvar
))))
672 (point (point)) to-be-removed
)
673 (save-window-excursion
674 (when ,tempvar
(find-file ,tempvar
))
675 (setq to-be-removed
(current-buffer))
676 (goto-char (point-min))
677 (while (re-search-forward org-babel-src-block-regexp nil t
)
678 (goto-char (match-beginning 0))
679 (let ((full-block (match-string 0))
680 (beg-block (match-beginning 0))
681 (end-block (match-end 0))
682 (lang (match-string 2))
683 (beg-lang (match-beginning 2))
684 (end-lang (match-end 2))
685 (switches (match-string 3))
686 (beg-switches (match-beginning 3))
687 (end-switches (match-end 3))
688 (header-args (match-string 4))
689 (beg-header-args (match-beginning 4))
690 (end-header-args (match-end 4))
691 (body (match-string 5))
692 (beg-body (match-beginning 5))
693 (end-body (match-end 5)))
695 (goto-char end-block
))))
696 (unless visited-p
(kill-buffer to-be-removed
))
700 (defmacro org-babel-map-inline-src-blocks
(file &rest body
)
701 "Evaluate BODY forms on each inline source-block in FILE.
702 If FILE is nil evaluate BODY forms on source blocks in current
705 (let ((tempvar (make-symbol "file")))
706 `(let* ((,tempvar
,file
)
707 (visited-p (or (null ,tempvar
)
708 (get-file-buffer (expand-file-name ,tempvar
))))
709 (point (point)) to-be-removed
)
710 (save-window-excursion
711 (when ,tempvar
(find-file ,tempvar
))
712 (setq to-be-removed
(current-buffer))
713 (goto-char (point-min))
714 (while (re-search-forward org-babel-inline-src-block-regexp nil t
)
715 (goto-char (match-beginning 1))
716 (save-match-data ,@body
)
717 (goto-char (match-end 0))))
718 (unless visited-p
(kill-buffer to-be-removed
))
722 (defun org-babel-execute-buffer (&optional arg
)
723 "Execute source code blocks in a buffer.
724 Call `org-babel-execute-src-block' on every source block in
727 (org-babel-eval-wipe-error-buffer)
728 (org-save-outline-visibility t
729 (org-babel-map-src-blocks nil
730 (org-babel-execute-src-block arg
))
731 (org-babel-map-inline-src-blocks nil
732 (org-babel-execute-src-block arg
))))
735 (defun org-babel-execute-subtree (&optional arg
)
736 "Execute source code blocks in a subtree.
737 Call `org-babel-execute-src-block' on every source block in
738 the current subtree."
742 (org-narrow-to-subtree)
743 (org-babel-execute-buffer arg
)
747 (defun org-babel-sha1-hash (&optional info
)
748 "Generate an sha1 hash based on the value of info."
750 (let ((print-level nil
)
751 (info (or info
(org-babel-get-src-block-info))))
753 (sort (copy-sequence (nth 2 info
))
754 (lambda (a b
) (string< (car a
) (car b
)))))
755 ((lambda (hash) (when (called-interactively-p 'interactive
) (message hash
)) hash
)
756 (sha1 (format "%s-%s"
763 (when (and v
(not (and (sequencep v
)
766 ((lambda (el) (format "%S" el
))
768 ((and (listp v
) ; lists are sorted
769 (member (car arg
) '(:result-params
)))
770 (sort (copy-sequence v
) #'string
<))
771 ((and (stringp v
) ; strings are sorted
772 (member (car arg
) '(:results
:exports
)))
773 (mapconcat #'identity
(sort (split-string v
)
775 (t v
)))))) ; atomic are left untouched
779 (defun org-babel-result-hash (&optional info
)
780 "Return the in-buffer hash associated with INFO."
781 (org-babel-where-is-src-block-result nil info
)
782 (org-babel-clean-text-properties (match-string 3)))
784 (defun org-babel-hide-hash ()
785 "Hide the hash in the current results line.
786 Only the initial `org-babel-hash-show' characters of the hash
787 will remain visible."
788 (add-to-invisibility-spec '(org-babel-hide-hash . t
))
790 (when (and (re-search-forward org-babel-result-regexp nil t
)
792 (let* ((start (match-beginning 3))
793 (hide-start (+ org-babel-hash-show start
))
795 (hash (match-string 3))
797 (setq ov1
(make-overlay start hide-start
))
798 (setq ov2
(make-overlay hide-start end
))
799 (overlay-put ov2
'invisible
'org-babel-hide-hash
)
800 (overlay-put ov1
'babel-hash hash
)))))
802 (defun org-babel-hide-all-hashes ()
803 "Hide the hash in the current buffer.
804 Only the initial `org-babel-hash-show' characters of each hash
805 will remain visible. This function should be called as part of
806 the `org-mode-hook'."
808 (while (re-search-forward org-babel-result-regexp nil t
)
809 (goto-char (match-beginning 0))
810 (org-babel-hide-hash)
811 (goto-char (match-end 0)))))
812 (add-hook 'org-mode-hook
'org-babel-hide-all-hashes
)
814 (defun org-babel-hash-at-point (&optional point
)
815 "Return the value of the hash at POINT.
816 The hash is also added as the last element of the kill ring.
817 This can be called with C-c C-c."
819 (let ((hash (car (delq nil
(mapcar
820 (lambda (ol) (overlay-get ol
'babel-hash
))
821 (overlays-at (or point
(point))))))))
822 (when hash
(kill-new hash
) (message hash
))))
823 (add-hook 'org-ctrl-c-ctrl-c-hook
'org-babel-hash-at-point
)
825 (defun org-babel-result-hide-spec ()
826 "Hide portions of results lines.
827 Add `org-babel-hide-result' as an invisibility spec for hiding
828 portions of results lines."
829 (add-to-invisibility-spec '(org-babel-hide-result . t
)))
830 (add-hook 'org-mode-hook
'org-babel-result-hide-spec
)
832 (defvar org-babel-hide-result-overlays nil
833 "Overlays hiding results.")
835 (defun org-babel-result-hide-all ()
836 "Fold all results in the current buffer."
838 (org-babel-show-result-all)
840 (while (re-search-forward org-babel-result-regexp nil t
)
841 (save-excursion (goto-char (match-beginning 0))
842 (org-babel-hide-result-toggle-maybe)))))
844 (defun org-babel-show-result-all ()
845 "Unfold all results in the current buffer."
846 (mapc 'delete-overlay org-babel-hide-result-overlays
)
847 (setq org-babel-hide-result-overlays nil
))
850 (defun org-babel-hide-result-toggle-maybe ()
851 "Toggle visibility of result at point."
853 (let ((case-fold-search t
))
855 (beginning-of-line 1)
856 (looking-at org-babel-result-regexp
))
857 (progn (org-babel-hide-result-toggle)
858 t
) ;; to signal that we took action
859 nil
))) ;; to signal that we did not
861 (defun org-babel-hide-result-toggle (&optional force
)
862 "Toggle the visibility of the current result."
866 (if (re-search-forward org-babel-result-regexp nil t
)
867 (let ((start (progn (beginning-of-line 2) (- (point) 1)))
868 (end (progn (goto-char (- (org-babel-result-end) 1)) (point)))
870 (if (memq t
(mapcar (lambda (overlay)
871 (eq (overlay-get overlay
'invisible
)
872 'org-babel-hide-result
))
873 (overlays-at start
)))
874 (if (or (not force
) (eq force
'off
))
876 (when (member ov org-babel-hide-result-overlays
)
877 (setq org-babel-hide-result-overlays
878 (delq ov org-babel-hide-result-overlays
)))
879 (when (eq (overlay-get ov
'invisible
)
880 'org-babel-hide-result
)
881 (delete-overlay ov
)))
882 (overlays-at start
)))
883 (setq ov
(make-overlay start end
))
884 (overlay-put ov
'invisible
'org-babel-hide-result
)
885 ;; make the block accessible to isearch
887 ov
'isearch-open-invisible
889 (when (member ov org-babel-hide-result-overlays
)
890 (setq org-babel-hide-result-overlays
891 (delq ov org-babel-hide-result-overlays
)))
892 (when (eq (overlay-get ov
'invisible
)
893 'org-babel-hide-result
)
894 (delete-overlay ov
))))
895 (push ov org-babel-hide-result-overlays
)))
896 (error "Not looking at a result line"))))
898 ;; org-tab-after-check-for-cycling-hook
899 (add-hook 'org-tab-first-hook
'org-babel-hide-result-toggle-maybe
)
900 ;; Remove overlays when changing major mode
901 (add-hook 'org-mode-hook
902 (lambda () (org-add-hook 'change-major-mode-hook
903 'org-babel-show-result-all
'append
'local
)))
905 (defvar org-file-properties
)
906 (defun org-babel-params-from-properties (&optional lang
)
907 "Retrieve parameters specified as properties.
908 Return an association list of any source block params which
909 may be specified in the properties of the current outline entry."
916 (or (org-entry-get (point) header-arg t
)
917 (org-entry-get (point) (concat ":" header-arg
) t
)))
918 (cons (intern (concat ":" header-arg
))
919 (org-babel-read val
))))
923 org-babel-header-arg-names
925 (setq sym
(intern (concat "org-babel-header-arg-names:" lang
)))
926 (and (boundp sym
) (eval sym
))))))))))
928 (defun org-babel-params-from-buffer ()
929 "Retrieve per-buffer parameters.
930 Return an association list of any source block params which
931 may be specified in the current buffer."
932 (let (local-properties)
937 (goto-char (point-min))
938 (while (re-search-forward
939 (org-make-options-regexp (list "BABEL" "PROPERTIES")) nil t
)
940 (setq local-properties
941 (org-babel-merge-params
943 (org-babel-parse-header-arguments
944 (org-match-string-no-properties 2)))))
945 local-properties
)))))
947 (defvar org-src-preserve-indentation
)
948 (defun org-babel-parse-src-block-match ()
949 "Parse the results from a match of the `org-babel-src-block-regexp'."
950 (let* ((block-indentation (length (match-string 1)))
951 (lang (org-babel-clean-text-properties (match-string 2)))
952 (lang-headers (intern (concat "org-babel-default-header-args:" lang
)))
953 (switches (match-string 3))
954 (body (org-babel-clean-text-properties (match-string 5)))
955 (preserve-indentation (or org-src-preserve-indentation
956 (string-match "-i\\>" switches
))))
958 ;; get block body less properties, protective commas, and indentation
961 (insert (org-babel-strip-protective-commas body
))
962 (unless preserve-indentation
(org-do-remove-indentation))
964 (org-babel-merge-params
965 org-babel-default-header-args
966 (org-babel-params-from-buffer)
967 (org-babel-params-from-properties lang
)
968 (if (boundp lang-headers
) (eval lang-headers
) nil
)
969 (org-babel-parse-header-arguments
970 (org-babel-clean-text-properties (or (match-string 4) ""))))
974 (defun org-babel-parse-inline-src-block-match ()
975 "Parse the results from a match of the `org-babel-inline-src-block-regexp'."
976 (let* ((lang (org-babel-clean-text-properties (match-string 2)))
977 (lang-headers (intern (concat "org-babel-default-header-args:" lang
))))
979 (org-babel-strip-protective-commas
980 (org-babel-clean-text-properties (match-string 5)))
981 (org-babel-merge-params
982 org-babel-default-inline-header-args
983 (org-babel-params-from-buffer)
984 (org-babel-params-from-properties lang
)
985 (if (boundp lang-headers
) (eval lang-headers
) nil
)
986 (org-babel-parse-header-arguments
987 (org-babel-clean-text-properties (or (match-string 4) "")))))))
989 (defun org-babel-parse-header-arguments (arg-string)
990 "Parse a string of header arguments returning an alist."
991 (when (> (length arg-string
) 0)
996 "\\([^ \f\t\n\r\v]+\\)[ \f\t\n\r\v]+\\([^ \f\t\n\r\v]+.*\\)"
998 (cons (intern (match-string 1 arg
))
999 (org-babel-read (org-babel-chomp (match-string 2 arg
))))
1000 (cons (intern (org-babel-chomp arg
)) nil
)))
1001 (let ((balance 0) (partial nil
) (lst nil
) (last 0))
1002 (mapc (lambda (ch) ; split on [] balanced instances of [ \t]:
1003 (setq balance
(+ balance
1004 (cond ((equal 91 ch
) 1)
1007 (setq partial
(cons ch partial
))
1008 (when (and (= ch
58) (= balance
0)
1009 (or (= last
32) (= last
9)))
1010 (setq lst
(cons (apply #'string
(nreverse (cddr partial
)))
1012 (setq partial
(list ch
)))
1014 (string-to-list arg-string
))
1015 (nreverse (cons (apply #'string
(nreverse partial
)) lst
)))))))
1017 (defun org-babel-process-params (params)
1018 "Expand variables in PARAMS and add summary parameters."
1019 (let* ((vars-and-names (org-babel-disassemble-tables
1020 (mapcar (lambda (el)
1021 (if (consp (cdr el
))
1022 (cdr el
) (org-babel-ref-parse (cdr el
))))
1023 (org-babel-get-header params
:var
))
1024 (cdr (assoc :hlines params
))
1025 (cdr (assoc :colnames params
))
1026 (cdr (assoc :rownames params
))))
1027 (raw-result (or (cdr (assoc :results params
)) ""))
1028 (result-params (append
1029 (split-string (if (stringp raw-result
)
1032 (cdr (assoc :result-params params
)))))
1034 (mapcar (lambda (var) (cons :var var
)) (car vars-and-names
))
1036 (cons :colname-names
(or (cdr (assoc :colname-names params
))
1037 (cadr vars-and-names
)))
1038 (cons :rowname-names
(or (cdr (assoc :rowname-names params
))
1039 (caddr vars-and-names
)))
1040 (cons :result-params result-params
)
1041 (cons :result-type
(cond ((member "output" result-params
) 'output
)
1042 ((member "value" result-params
) 'value
)
1044 (org-babel-get-header params
:var
'other
))))
1046 ;; row and column names
1047 (defun org-babel-del-hlines (table)
1048 "Remove all 'hlines from TABLE."
1049 (remove 'hline table
))
1051 (defun org-babel-get-colnames (table)
1052 "Return the column names of TABLE.
1053 Return a cons cell, the `car' of which contains the TABLE less
1054 colnames, and the `cdr' of which contains a list of the column
1056 (if (equal 'hline
(nth 1 table
))
1057 (cons (cddr table
) (car table
))
1058 (cons (cdr table
) (car table
))))
1060 (defun org-babel-get-rownames (table)
1061 "Return the row names of TABLE.
1062 Return a cons cell, the `car' of which contains the TABLE less
1063 colnames, and the `cdr' of which contains a list of the column
1064 names. Note: this function removes any hlines in TABLE."
1065 (flet ((trans (table) (apply #'mapcar
* #'list table
)))
1066 (let* ((width (apply 'max
1067 (mapcar (lambda (el) (if (listp el
) (length el
) 0)) table
)))
1068 (table (trans (mapcar (lambda (row)
1069 (if (not (equal row
'hline
))
1073 (setq row
(cons 'hline row
)))
1076 (cons (mapcar (lambda (row) (if (equal (car row
) 'hline
) 'hline row
))
1077 (trans (cdr table
)))
1078 (remove 'hline
(car table
))))))
1080 (defun org-babel-put-colnames (table colnames
)
1081 "Add COLNAMES to TABLE if they exist."
1082 (if colnames
(apply 'list colnames
'hline table
) table
))
1084 (defun org-babel-put-rownames (table rownames
)
1085 "Add ROWNAMES to TABLE if they exist."
1087 (mapcar (lambda (row)
1089 (cons (or (pop rownames
) "") row
)
1093 (defun org-babel-pick-name (names selector
)
1094 "Select one out of an alist of row or column names.
1095 SELECTOR can be either a list of names in which case those names
1096 will be returned directly, or an index into the list NAMES in
1097 which case the indexed names will be return."
1098 (if (listp selector
)
1101 (if (and selector
(symbolp selector
) (not (equal t selector
)))
1102 (cdr (assoc selector names
))
1103 (if (integerp selector
)
1104 (nth (- selector
1) names
)
1105 (cdr (car (last names
))))))))
1107 (defun org-babel-disassemble-tables (vars hlines colnames rownames
)
1108 "Parse tables for further processing.
1109 Process the variables in VARS according to the HLINES,
1110 ROWNAMES and COLNAMES header arguments. Return a list consisting
1111 of the vars, cnames and rnames."
1112 (let (cnames rnames
)
1116 (when (listp (cdr var
))
1117 (when (and (not (equal colnames
"no"))
1118 (or colnames
(and (equal (nth 1 (cdr var
)) 'hline
)
1119 (not (member 'hline
(cddr (cdr var
)))))))
1120 (let ((both (org-babel-get-colnames (cdr var
))))
1121 (setq cnames
(cons (cons (car var
) (cdr both
))
1123 (setq var
(cons (car var
) (car both
)))))
1124 (when (and rownames
(not (equal rownames
"no")))
1125 (let ((both (org-babel-get-rownames (cdr var
))))
1126 (setq rnames
(cons (cons (car var
) (cdr both
))
1128 (setq var
(cons (car var
) (car both
)))))
1129 (when (and hlines
(not (equal hlines
"yes")))
1130 (setq var
(cons (car var
) (org-babel-del-hlines (cdr var
))))))
1135 (defun org-babel-reassemble-table (table colnames rownames
)
1136 "Add column and row names to a table.
1137 Given a TABLE and set of COLNAMES and ROWNAMES add the names
1138 to the table for reinsertion to org-mode."
1141 (if (and colnames
(listp (car table
)) (= (length (car table
))
1143 (org-babel-put-colnames table colnames
) table
))
1144 (if (and rownames
(= (length table
) (length rownames
)))
1145 (org-babel-put-rownames table rownames
) table
))
1148 (defun org-babel-where-is-src-block-head ()
1149 "Find where the current source block begins.
1150 Return the point at the beginning of the current source
1151 block. Specifically at the beginning of the #+BEGIN_SRC line.
1152 If the point is not on a source block then return nil."
1153 (let ((initial (point)) top bottom
)
1155 (save-excursion ;; on a source name line or a #+header line
1156 (beginning-of-line 1)
1157 (and (or (looking-at org-babel-src-name-regexp
)
1158 (looking-at org-babel-multi-line-header-regexp
))
1160 (while (and (forward-line 1)
1161 (looking-at org-babel-multi-line-header-regexp
)))
1162 (looking-at org-babel-src-block-regexp
))
1164 (save-excursion ;; on a #+begin_src line
1165 (beginning-of-line 1)
1166 (and (looking-at org-babel-src-block-regexp
)
1168 (save-excursion ;; inside a src block
1170 (re-search-backward "^[ \t]*#\\+begin_src" nil t
) (setq top
(point))
1171 (re-search-forward "^[ \t]*#\\+end_src" nil t
) (setq bottom
(point))
1172 (< top initial
) (< initial bottom
)
1173 (progn (goto-char top
) (beginning-of-line 1)
1174 (looking-at org-babel-src-block-regexp
))
1178 (defun org-babel-goto-src-block-head ()
1179 "Go to the beginning of the current code block."
1182 (if head
(goto-char head
) (error "not currently in a code block")))
1183 (org-babel-where-is-src-block-head)))
1186 (defun org-babel-goto-named-src-block (name)
1187 "Go to a named source-code block."
1189 (let ((completion-ignore-case t
))
1190 (list (org-icompleting-read "source-block name: "
1191 (org-babel-src-block-names) nil t
))))
1192 (let ((point (org-babel-find-named-block name
)))
1194 ;; taken from `org-open-at-point'
1195 (progn (goto-char point
) (org-show-context))
1196 (message "source-code block '%s' not found in this buffer" name
))))
1198 (defun org-babel-find-named-block (name)
1199 "Find a named source-code block.
1200 Return the location of the source block identified by source
1201 NAME, or nil if no such block exists. Set match data according to
1202 org-babel-named-src-block-regexp."
1204 (let ((case-fold-search t
)
1205 (regexp (org-babel-named-src-block-regexp-for-name name
)) msg
)
1206 (goto-char (point-min))
1207 (when (or (re-search-forward regexp nil t
)
1208 (re-search-backward regexp nil t
))
1209 (match-beginning 0)))))
1211 (defun org-babel-src-block-names (&optional file
)
1212 "Returns the names of source blocks in FILE or the current buffer."
1214 (when file
(find-file file
)) (goto-char (point-min))
1216 (while (re-search-forward org-babel-src-name-w-name-regexp nil t
)
1217 (setq names
(cons (match-string 4) names
)))
1221 (defun org-babel-goto-named-result (name)
1222 "Go to a named result."
1224 (let ((completion-ignore-case t
))
1225 (list (org-icompleting-read "source-block name: "
1226 (org-babel-result-names) nil t
))))
1227 (let ((point (org-babel-find-named-result name
)))
1229 ;; taken from `org-open-at-point'
1230 (progn (goto-char point
) (org-show-context))
1231 (message "result '%s' not found in this buffer" name
))))
1233 (defun org-babel-find-named-result (name)
1234 "Find a named result.
1235 Return the location of the result named NAME in the current
1236 buffer or nil if no such result exists."
1238 (goto-char (point-min))
1239 (when (re-search-forward
1240 (concat org-babel-result-regexp
1241 "[ \t]" (regexp-quote name
) "[ \t\n\f\v\r]") nil t
)
1242 (beginning-of-line 0) (point))))
1244 (defun org-babel-result-names (&optional file
)
1245 "Returns the names of results in FILE or the current buffer."
1247 (when file
(find-file file
)) (goto-char (point-min))
1249 (while (re-search-forward org-babel-result-w-name-regexp nil t
)
1250 (setq names
(cons (match-string 4) names
)))
1254 (defun org-babel-next-src-block (&optional arg
)
1255 "Jump to the next source block.
1256 With optional prefix argument ARG, jump forward ARG many source blocks."
1258 (when (looking-at org-babel-src-block-regexp
) (forward-char 1))
1260 (re-search-forward org-babel-src-block-regexp nil nil
(or arg
1))
1261 (error (error "No further code blocks")))
1262 (goto-char (match-beginning 0)) (org-show-context))
1265 (defun org-babel-previous-src-block (&optional arg
)
1266 "Jump to the previous source block.
1267 With optional prefix argument ARG, jump backward ARG many source blocks."
1270 (re-search-backward org-babel-src-block-regexp nil nil
(or arg
1))
1271 (error (error "No previous code blocks")))
1272 (goto-char (match-beginning 0)) (org-show-context))
1274 (defvar org-babel-load-languages
)
1277 (defun org-babel-mark-block ()
1278 "Mark current src block"
1284 (looking-at org-babel-src-block-regexp
))
1285 (push-mark (match-end 5) nil t
)
1286 (goto-char (match-beginning 5))))
1287 (org-babel-where-is-src-block-head)))
1289 (defun org-babel-demarcate-block (&optional arg
)
1290 "Wrap or split the code in the region or on the point.
1291 When called from inside of a code block the current block is
1292 split. When called from outside of a code block a new code block
1293 is created. In both cases if the region is demarcated and if the
1294 region is not active then the point is demarcated."
1296 (let ((info (org-babel-get-src-block-info 'light
))
1297 (stars (concat (make-string (or (org-current-level) 1) ?
*) " ")))
1303 (let ((lang (nth 0 info
))
1304 (indent (make-string (nth 5 info
) ?
)))
1305 (when (string-match "^[[:space:]]*$"
1306 (buffer-substring (point-at-bol)
1308 (delete-region (point-at-bol) (point-at-eol)))
1309 (insert (concat (if (looking-at "^") "" "\n")
1310 indent
"#+end_src\n"
1311 (if arg stars indent
) "\n"
1312 indent
"#+begin_src " lang
1313 (if (looking-at "[\n\r]") "" "\n")))))
1314 (move-end-of-line 2))
1315 (sort (if (region-active-p) (list (mark) (point)) (list (point))) #'>))
1316 (let ((start (point))
1317 (lang (org-icompleting-read "Lang: "
1318 (mapcar (lambda (el) (symbol-name (car el
)))
1319 org-babel-load-languages
)))
1320 (body (delete-and-extract-region
1321 (if (region-active-p) (mark) (point)) (point))))
1322 (insert (concat (if (looking-at "^") "" "\n")
1323 (if arg
(concat stars
"\n") "")
1324 "#+begin_src " lang
"\n"
1326 (if (or (= (length body
) 0)
1327 (string-match "[\r\n]$" body
)) "" "\n")
1329 (goto-char start
) (move-end-of-line 1)))))
1331 (defvar org-babel-lob-one-liner-regexp
)
1332 (defun org-babel-where-is-src-block-result (&optional insert info hash indent
)
1333 "Find where the current source block results begin.
1334 Return the point at the beginning of the result of the current
1335 source block. Specifically at the beginning of the results line.
1336 If no result exists for this block then create a results line
1337 following the source block."
1339 (let* ((on-lob-line (save-excursion
1340 (beginning-of-line 1)
1341 (looking-at org-babel-lob-one-liner-regexp
)))
1342 (inlinep (save-excursion
1343 (re-search-backward "[ \f\t\n\r\v]" nil t
)
1344 (when (looking-at org-babel-inline-src-block-regexp
)
1346 (name (if on-lob-line
1347 (nth 0 (org-babel-lob-get-info))
1348 (nth 4 (or info
(org-babel-get-src-block-info)))))
1349 (head (unless on-lob-line
(org-babel-where-is-src-block-head)))
1351 (when head
(goto-char head
))
1353 found
;; was there a result (before we potentially insert one)
1358 ;; - return t if it is found, else return nil
1359 ;; - if it does not need to be rebuilt, then don't set end
1360 ;; - if it does need to be rebuilt then do set end
1361 name
(setq beg
(org-babel-find-named-result name
))
1363 (when (and hash
(not (string= hash
(match-string 3))))
1364 (goto-char beg
) (setq end beg
) ;; beginning of result
1366 (delete-region end
(org-babel-result-end)) nil
)))
1369 ;; - return t if it is found, else return nil
1370 ;; - if it is found, and the hash doesn't match, delete and set end
1371 (or on-lob-line
(re-search-forward "^[ \t]*#\\+end_src" nil t
))
1372 (progn (end-of-line 1)
1373 (if (eobp) (insert "\n") (forward-char 1))
1376 (progn ;; unnamed results line already exists
1377 (re-search-forward "[^ \f\t\n\r\v]" nil t
)
1378 (beginning-of-line 1)
1380 (concat org-babel-result-regexp
"\n")))
1382 ;; must remove and rebuild if hash!=old-hash
1383 (if (and hash
(not (string= hash
(match-string 3))))
1387 end
(org-babel-result-end)))
1388 (setq end nil
)))))))))
1389 (if (and insert end
)
1393 (if (looking-at "[\n\r]") (forward-char 1) (insert "\n")))
1398 (org-number-sequence 1 indent
) "")
1401 (when hash
(concat "["hash
"]"))
1403 (when name
(concat " " name
)) "\n"))
1404 (unless beg
(insert "\n") (backward-char))
1405 (beginning-of-line 0)
1406 (if hash
(org-babel-hide-hash))
1410 (defvar org-block-regexp
)
1411 (defun org-babel-read-result ()
1412 "Read the result at `point' into emacs-lisp."
1413 (let ((case-fold-search t
) result-string
)
1415 ((org-at-table-p) (org-babel-read-table))
1416 ((org-at-item-p) (org-babel-read-list))
1417 ((looking-at org-bracket-link-regexp
) (org-babel-read-link))
1418 ((looking-at org-block-regexp
) (org-babel-trim (match-string 4)))
1419 ((looking-at "^[ \t]*: ")
1422 (mapconcat (lambda (line)
1423 (if (and (> (length line
) 1)
1424 (string-match "^[ \t]*: \\(.+\\)" line
))
1425 (match-string 1 line
)
1429 (point) (org-babel-result-end)) "[\r\n]+")
1431 (or (org-babel-number-p result-string
) result-string
))
1432 ((looking-at org-babel-result-regexp
)
1433 (save-excursion (forward-line 1) (org-babel-read-result))))))
1435 (defun org-babel-read-table ()
1436 "Read the table at `point' into emacs-lisp."
1437 (mapcar (lambda (row)
1438 (if (and (symbolp row
) (equal row
'hline
)) row
1439 (mapcar (lambda (el) (org-babel-read el
'inhibit-lisp-eval
)) row
)))
1440 (org-table-to-lisp)))
1442 (defun org-babel-read-list ()
1443 "Read the list at `point' into emacs-lisp."
1444 (mapcar (lambda (el) (org-babel-read el
'inhibit-lisp-eval
))
1445 (mapcar #'cadr
(cdr (org-list-parse-list)))))
1447 (defvar org-link-types-re
)
1448 (defun org-babel-read-link ()
1449 "Read the link at `point' into emacs-lisp.
1450 If the path of the link is a file path it is expanded using
1451 `expand-file-name'."
1452 (let* ((case-fold-search t
)
1453 (raw (and (looking-at org-bracket-link-regexp
)
1454 (org-babel-clean-text-properties (match-string 1))))
1455 (type (and (string-match org-link-types-re raw
)
1456 (match-string 1 raw
))))
1458 ((not type
) (expand-file-name raw
))
1459 ((string= type
"file")
1460 (and (string-match "file\\(.*\\):\\(.+\\)" raw
)
1461 (expand-file-name (match-string 2 raw
))))
1464 (defun org-babel-format-result (result &optional sep
)
1465 "Format RESULT for writing to file."
1466 (flet ((echo-res (result)
1467 (if (stringp result
) result
(format "%S" result
))))
1476 (echo-res result
))))
1478 (defun org-babel-insert-result
1479 (result &optional result-params info hash indent lang
)
1480 "Insert RESULT into the current buffer.
1481 By default RESULT is inserted after the end of the
1482 current source block. With optional argument RESULT-PARAMS
1483 controls insertion of results in the org-mode file.
1484 RESULT-PARAMS can take the following values...
1486 replace - (default option) insert results after the source block
1487 replacing any previously inserted results
1489 silent -- no results are inserted
1491 file ---- the results are interpreted as a file path, and are
1492 inserted into the buffer using the Org-mode file syntax
1494 list ---- the results are interpreted as an Org-mode list.
1496 raw ----- results are added directly to the Org-mode file. This
1497 is a good option if you code block will output org-mode
1500 org ----- similar in effect to raw, only the results are wrapped
1501 in an org code block. Similar to the raw option, on
1502 export the results will be interpreted as org-formatted
1503 text, however by wrapping the results in an org code
1504 block they can be replaced upon re-execution of the
1507 html ---- results are added inside of a #+BEGIN_HTML block. This
1508 is a good option if you code block will output html
1511 latex --- results are added inside of a #+BEGIN_LATEX block.
1512 This is a good option if you code block will output
1513 latex formatted text.
1515 code ---- the results are extracted in the syntax of the source
1516 code of the language being evaluated and are added
1517 inside of a #+BEGIN_SRC block with the source-code
1518 language set appropriately. Note this relies on the
1519 optional LANG argument."
1520 (if (stringp result
)
1522 (setq result
(org-babel-clean-text-properties result
))
1523 (when (member "file" result-params
)
1524 (setq result
(org-babel-result-to-file result
))))
1525 (unless (listp result
) (setq result
(format "%S" result
))))
1526 (if (and result-params
(member "silent" result-params
))
1528 (message (replace-regexp-in-string "%" "%%" (format "%S" result
)))
1531 (let* ((inlinep (save-excursion
1532 (re-search-backward "[ \f\t\n\r\v]" nil t
)
1533 (when (looking-at org-babel-inline-src-block-regexp
)
1534 (goto-char (match-end 0))
1535 (insert (if (listp result
) "\n" " "))
1537 (existing-result (unless inlinep
1538 (org-babel-where-is-src-block-result
1539 t info hash indent
)))
1541 (cdr (assoc :results_switches
(nth 2 info
))))
1543 (when (and (stringp result
) ; ensure results end in a newline
1545 (> (length result
) 0)
1546 (not (or (string-equal (substring result -
1) "\n")
1547 (string-equal (substring result -
1) "\r"))))
1548 (setq result
(concat result
"\n")))
1549 (if (not existing-result
)
1550 (setq beg
(or inlinep
(point)))
1551 (goto-char existing-result
)
1553 (re-search-forward "#" nil t
)
1554 (setq indent
(- (current-column) 1)))
1558 ((member "replace" result-params
)
1559 (delete-region (point) (org-babel-result-end)))
1560 ((member "append" result-params
)
1561 (goto-char (org-babel-result-end)) (setq beg
(point-marker)))
1562 ((member "prepend" result-params
)))) ; already there
1563 (setq results-switches
1564 (if results-switches
(concat " " results-switches
) ""))
1565 ;; insert results based on type
1567 ;; do nothing for an empty result
1568 ((= (length result
) 0))
1569 ;; insert a list if preferred
1570 ((member "list" result-params
)
1573 (org-list-to-generic
1576 (lambda (el) (list nil
(if (stringp el
) el
(format "%S" el
))))
1577 (if (listp result
) result
(list result
))))
1578 '(:splicep nil
:istart
"- " :iend
"\n")))
1580 ;; assume the result is a table if it's not a string
1581 ((not (stringp result
))
1583 (insert (concat (orgtbl-to-orgtbl
1584 (if (or (eq 'hline
(car result
))
1585 (and (listp (car result
))
1586 (listp (cdr (car result
)))))
1587 result
(list result
))
1588 '(:fmt
(lambda (cell) (format "%s" cell
)))) "\n"))
1589 (goto-char beg
) (when (org-at-table-p) (org-table-align)))
1590 ((member "file" result-params
)
1592 (t (goto-char beg
) (insert result
)))
1593 (when (listp result
) (goto-char (org-table-end)))
1594 (setq end
(point-marker))
1595 ;; possibly wrap result
1596 (flet ((wrap (start finish
)
1597 (goto-char beg
) (insert (concat start
"\n"))
1598 (goto-char end
) (insert (concat finish
"\n"))
1599 (setq end
(point-marker))))
1601 ((member "html" result-params
)
1602 (wrap "#+BEGIN_HTML" "#+END_HTML"))
1603 ((member "latex" result-params
)
1604 (wrap "#+BEGIN_LaTeX" "#+END_LaTeX"))
1605 ((member "code" result-params
)
1606 (wrap (format "#+BEGIN_SRC %s%s" (or lang
"none") results-switches
)
1608 ((member "org" result-params
)
1609 (wrap "#+BEGIN_ORG" "#+END_ORG"))
1610 ((member "raw" result-params
)
1611 (goto-char beg
) (if (org-at-table-p) (org-cycle)))
1612 ((member "wrap" result-params
)
1613 (when (and (stringp result
) (not (member "file" result-params
)))
1614 (org-babel-examplize-region beg end results-switches
))
1615 (wrap "#+BEGIN_RESULT" "#+END_RESULT"))
1616 ((and (stringp result
) (not (member "file" result-params
)))
1617 (org-babel-examplize-region beg end results-switches
)
1618 (setq end
(point)))))
1619 ;; possibly indent the results to match the #+results line
1620 (when (and (not inlinep
) (numberp indent
) indent
(> indent
0)
1621 ;; in this case `table-align' does the work for us
1622 (not (and (listp result
)
1623 (member "append" result-params
))))
1624 (indent-rigidly beg end indent
))))
1625 (if (= (length result
) 0)
1626 (if (member "value" result-params
)
1627 (message "Code block returned no value.")
1628 (message "Code block produced no output."))
1629 (message "Code block evaluation complete."))))
1631 (defun org-babel-remove-result (&optional info
)
1632 "Remove the result of the current source block."
1634 (let ((location (org-babel-where-is-src-block-result nil info
)) start
)
1637 (goto-char location
) (setq start
(point)) (forward-line 1)
1638 (delete-region start
(org-babel-result-end))))))
1640 (defun org-babel-result-end ()
1641 "Return the point at the end of the current set of results"
1644 ((org-at-table-p) (progn (goto-char (org-table-end)) (point)))
1645 ((org-at-item-p) (let* ((struct (org-list-struct))
1646 (prvs (org-list-prevs-alist struct
)))
1647 (org-list-get-list-end (point-at-bol) struct prvs
)))
1649 (let ((case-fold-search t
)
1650 (blocks-re (regexp-opt
1651 (list "latex" "html" "example" "src" "result" "org"))))
1652 (if (looking-at (concat "[ \t]*#\\+begin_" blocks-re
))
1653 (progn (re-search-forward (concat "[ \t]*#\\+end_" blocks-re
) nil t
)
1655 (while (looking-at "[ \t]*\\(: \\|\\[\\[\\)")
1659 (defun org-babel-result-to-file (result)
1660 "Convert RESULT into an `org-mode' link.
1661 If the `default-directory' is different from the containing
1662 file's directory then expand relative links."
1663 (flet ((cond-exp (file)
1664 (if (and default-directory
1666 (not (string= (expand-file-name default-directory
)
1668 (file-name-directory buffer-file-name
)))))
1669 (expand-file-name file default-directory
)
1671 (if (stringp result
)
1672 (format "[[file:%s]]" (cond-exp result
))
1673 (when (and (listp result
) (= 2 (length result
))
1674 (stringp (car result
)) (stringp (cadr result
)))
1675 (format "[[file:%s][%s]]" (car result
) (cadr result
))))))
1677 (defun org-babel-examplize-region (beg end
&optional results-switches
)
1678 "Comment out region using the inline '==' or ': ' org example quote."
1680 (flet ((chars-between (b e
) (string-match "[\\S]" (buffer-substring b e
))))
1681 (if (or (chars-between (save-excursion (goto-char beg
) (point-at-bol)) beg
)
1682 (chars-between end
(save-excursion (goto-char end
) (point-at-eol))))
1685 (insert (format "=%s=" (prog1 (buffer-substring beg end
)
1686 (delete-region beg end
)))))
1687 (let ((size (count-lines beg end
)))
1689 (cond ((= size
0)) ; do nothing for an empty result
1690 ((< size org-babel-min-lines-for-block-output
)
1693 (beginning-of-line 1) (insert ": ") (forward-line 1)))
1696 (insert (if results-switches
1697 (format "#+begin_example%s\n" results-switches
)
1698 "#+begin_example\n"))
1699 (if (markerp end
) (goto-char end
) (forward-char (- end beg
)))
1700 (insert "#+end_example\n"))))))))
1702 (defun org-babel-update-block-body (new-body)
1703 "Update the body of the current code block to NEW-BODY."
1704 (if (not (org-babel-where-is-src-block-head))
1705 (error "not in source block")
1707 (replace-match (concat (org-babel-trim new-body
) "\n") nil t nil
5))
1708 (indent-rigidly (match-beginning 5) (match-end 5) 2)))
1710 (defun org-babel-merge-params (&rest plists
)
1711 "Combine all parameter association lists in PLISTS.
1712 Later elements of PLISTS override the values of previous elements.
1713 This takes into account some special considerations for certain
1714 parameters when merging lists."
1715 (let ((results-exclusive-groups
1716 '(("file" "list" "vector" "table" "scalar" "raw" "org"
1717 "html" "latex" "code" "pp" "wrap")
1718 ("replace" "silent" "append" "prepend")
1719 ("output" "value")))
1720 (exports-exclusive-groups
1721 '(("code" "results" "both" "none")))
1722 params results exports tangle noweb cache vars shebang comments padline
)
1723 (flet ((e-merge (exclusive-groups &rest result-params
)
1724 ;; maintain exclusivity of mutually exclusive parameters
1726 (mapc (lambda (new-params)
1727 (mapc (lambda (new-param)
1728 (mapc (lambda (exclusive-group)
1729 (when (member new-param exclusive-group
)
1730 (mapcar (lambda (excluded-param)
1737 (setq output
(org-uniquify
1738 (cons new-param output
))))
1748 (let ((name (if (listp (cdr pair
))
1750 (and (string-match "^\\([^= \f\t\n\r\v]+\\)[ \t]*="
1752 (intern (match-string 1 (cdr pair
)))))))
1755 (cons (cons name pair
)
1756 (if (member name
(mapcar #'car vars
))
1759 (lambda (p) (unless (equal (car p
) name
) p
))
1763 (setq results
(e-merge results-exclusive-groups
1766 (let ((r (cdr pair
)))
1767 (if (stringp r
) r
(eval r
)))))))
1770 (setq results
(e-merge results-exclusive-groups
1772 (unless (or (member "both" exports
)
1773 (member "none" exports
)
1774 (member "code" exports
))
1775 (setq exports
(e-merge exports-exclusive-groups
1776 exports
'("results"))))
1777 (setq params
(cons pair
(assq-delete-all (car pair
) params
)))))
1779 (setq exports
(e-merge exports-exclusive-groups
1780 exports
(split-string (cdr pair
)))))
1781 (:tangle
;; take the latest -- always overwrite
1782 (setq tangle
(or (list (cdr pair
)) tangle
)))
1784 (setq noweb
(e-merge '(("yes" "no" "tangle")) noweb
1785 (split-string (or (cdr pair
) "")))))
1787 (setq cache
(e-merge '(("yes" "no")) cache
1788 (split-string (or (cdr pair
) "")))))
1790 (setq padline
(e-merge '(("yes" "no")) padline
1791 (split-string (or (cdr pair
) "")))))
1792 (:shebang
;; take the latest -- always overwrite
1793 (setq shebang
(or (list (cdr pair
)) shebang
)))
1795 (setq comments
(e-merge '(("yes" "no")) comments
1796 (split-string (or (cdr pair
) "")))))
1797 (t ;; replace: this covers e.g. :session
1798 (setq params
(cons pair
(assq-delete-all (car pair
) params
))))))
1801 (while vars
(setq params
(cons (cons :var
(cddr (pop vars
))) params
)))
1804 (let ((key (intern (concat ":" (symbol-name hd
))))
1806 (setf params
(cons (cons key
(mapconcat 'identity val
" ")) params
))))
1807 '(results exports tangle noweb padline cache shebang comments
))
1810 (defun org-babel-expand-noweb-references (&optional info parent-buffer
)
1811 "Expand Noweb references in the body of the current source code block.
1813 For example the following reference would be replaced with the
1814 body of the source-code block named 'example-block'.
1818 Note that any text preceding the <<foo>> construct on a line will
1819 be interposed between the lines of the replacement text. So for
1820 example if <<foo>> is placed behind a comment, then the entire
1821 replacement text will also be commented.
1823 This function must be called from inside of the buffer containing
1824 the source-code block which holds BODY.
1826 In addition the following syntax can be used to insert the
1827 results of evaluating the source-code block named 'example-block'.
1831 Any optional arguments can be passed to example-block by placing
1832 the arguments inside the parenthesis following the convention
1833 defined by `org-babel-lob'. For example
1835 <<example-block(a=9)>>
1837 would set the value of argument \"a\" equal to \"9\". Note that
1838 these arguments are not evaluated in the current source-code
1839 block but are passed literally to the \"example-block\"."
1840 (let* ((parent-buffer (or parent-buffer
(current-buffer)))
1841 (info (or info
(org-babel-get-src-block-info)))
1844 (comment (string= "noweb" (cdr (assoc :comments
(nth 2 info
)))))
1845 (new-body "") index source-name evaluate prefix
)
1846 (flet ((nb-add (text) (setq new-body
(concat new-body text
)))
1849 (funcall (intern (concat lang
"-mode")))
1850 (comment-region (point) (progn (insert text
) (point)))
1851 (org-babel-trim (buffer-string)))))
1853 (insert body
) (goto-char (point-min))
1854 (setq index
(point))
1855 (while (and (re-search-forward "<<\\(.+?\\)>>" nil t
))
1856 (save-match-data (setf source-name
(match-string 1)))
1857 (save-match-data (setq evaluate
(string-match "\(.*\)" source-name
)))
1860 (buffer-substring (match-beginning 0)
1862 (beginning-of-line 1) (point)))))
1863 ;; add interval to new-body (removing noweb reference)
1864 (goto-char (match-beginning 0))
1865 (nb-add (buffer-substring index
(point)))
1866 (goto-char (match-end 0))
1867 (setq index
(point))
1869 (with-current-buffer parent-buffer
1870 (mapconcat ;; interpose PREFIX between every line
1874 (let ((raw (org-babel-ref-resolve source-name
)))
1875 (if (stringp raw
) raw
(format "%S" raw
)))
1876 (or (nth 2 (assoc (intern source-name
)
1877 org-babel-library-of-babel
))
1880 (let ((point (org-babel-find-named-block
1885 ;; possibly wrap body in comments
1886 (let* ((i (org-babel-get-src-block-info 'light
))
1887 (body (org-babel-trim
1888 (org-babel-expand-noweb-references
1891 ((lambda (cs) (concat (c-wrap (car cs
)) "\n"
1893 "\n" (c-wrap (cadr cs
))))
1894 (org-babel-tangle-comment-links i
))
1896 ;; optionally raise an error if named
1897 ;; source-block doesn't exist
1898 (if (member lang org-babel-noweb-error-langs
)
1901 "<<" source-name
">> "
1902 "could not be resolved (see "
1903 "`org-babel-noweb-error-langs')"))
1905 "[\n\r]") (concat "\n" prefix
)))))
1906 (nb-add (buffer-substring index
(point-max)))))
1909 (defun org-babel-clean-text-properties (text)
1910 "Strip all properties from text return."
1912 (set-text-properties 0 (length text
) nil text
) text
))
1914 (defun org-babel-strip-protective-commas (body)
1915 "Strip protective commas from bodies of source blocks."
1916 (replace-regexp-in-string "^,#" "#" body
))
1918 (defun org-babel-script-escape (str)
1919 "Safely convert tables into elisp lists."
1920 (let (in-single in-double out
)
1921 ((lambda (escaped) (condition-case nil
(org-babel-read escaped
) (error escaped
)))
1922 (if (and (stringp str
)
1924 (string-equal "[" (substring str
0 1))
1925 (string-equal "]" (substring str -
1)))
1935 (91 (if (or in-double in-single
) ; [
1938 (93 (if (or in-double in-single
) ; ]
1941 (44 (if (or in-double in-single
) ; ,
1942 (cons 44 out
) (cons 32 out
)))
1943 (39 (if in-double
; '
1945 (setq in-single
(not in-single
)) (cons 34 out
)))
1946 (34 (if in-single
; "
1947 (append (list 34 32) out
)
1948 (setq in-double
(not in-double
)) (cons 34 out
)))
1949 (t (cons ch out
)))))
1950 (string-to-list str
))
1951 (apply #'string
(reverse out
)))))
1954 (defun org-babel-read (cell &optional inhibit-lisp-eval
)
1955 "Convert the string value of CELL to a number if appropriate.
1956 Otherwise if cell looks like lisp (meaning it starts with a
1957 \"(\", \"'\", \"`\" or a \"[\") then read it as lisp, otherwise
1958 return it unmodified as a string. Optional argument NO-LISP-EVAL
1959 inhibits lisp evaluation for situations in which is it not
1961 (if (and (stringp cell
) (not (equal cell
"")))
1962 (or (org-babel-number-p cell
)
1963 (if (and (not inhibit-lisp-eval
)
1964 (member (substring cell
0 1) '("(" "'" "`" "[")))
1966 (if (string= (substring cell
0 1) "\"")
1968 (progn (set-text-properties 0 (length cell
) nil cell
) cell
))))
1971 (defun org-babel-number-p (string)
1972 "If STRING represents a number return it's value."
1973 (if (and (string-match "^-?[0-9]*\\.?[0-9]*$" string
)
1974 (= (length (substring string
(match-beginning 0)
1977 (string-to-number string
)))
1979 (defun org-babel-import-elisp-from-file (file-name &optional separator
)
1980 "Read the results located at FILE-NAME into an elisp table.
1981 If the table is trivial, then return it as a scalar."
1983 (save-window-excursion
1987 (org-table-import file-name separator
)
1988 (delete-file file-name
)
1989 (setq result
(mapcar (lambda (row)
1990 (mapcar #'org-babel-string-read row
))
1991 (org-table-to-lisp))))
1993 (if (null (cdr result
)) ;; if result is trivial vector, then scalarize it
1994 (if (consp (car result
))
1995 (if (null (cdr (car result
)))
2001 (defun org-babel-string-read (cell)
2002 "Strip nested \"s from around strings."
2003 (org-babel-read (or (and (stringp cell
)
2004 (string-match "\\\"\\(.+\\)\\\"" cell
)
2005 (match-string 1 cell
))
2008 (defun org-babel-reverse-string (string)
2009 "Return the reverse of STRING."
2010 (apply 'string
(reverse (string-to-list string
))))
2012 (defun org-babel-chomp (string &optional regexp
)
2013 "Strip trailing spaces and carriage returns from STRING.
2014 Default regexp used is \"[ \f\t\n\r\v]\" but can be
2015 overwritten by specifying a regexp as a second argument."
2016 (let ((regexp (or regexp
"[ \f\t\n\r\v]")))
2017 (while (and (> (length string
) 0)
2018 (string-match regexp
(substring string -
1)))
2019 (setq string
(substring string
0 -
1)))
2022 (defun org-babel-trim (string &optional regexp
)
2023 "Strip leading and trailing spaces and carriage returns from STRING.
2024 Like `org-babel-chomp' only it runs on both the front and back
2026 (org-babel-chomp (org-babel-reverse-string
2027 (org-babel-chomp (org-babel-reverse-string string
) regexp
))
2030 (defvar org-babel-org-babel-call-process-region-original nil
)
2031 (defun org-babel-tramp-handle-call-process-region
2032 (start end program
&optional delete buffer display
&rest args
)
2033 "Use tramp to handle call-process-region.
2034 Fixes a bug in `tramp-handle-call-process-region'."
2035 (if (and (featurep 'tramp
) (file-remote-p default-directory
))
2036 (let ((tmpfile (tramp-compat-make-temp-file "")))
2037 (write-region start end tmpfile
)
2038 (when delete
(delete-region start end
))
2040 ;; (apply 'call-process program tmpfile buffer display args)
2042 (apply 'process-file program tmpfile buffer display args
)
2043 (delete-file tmpfile
)))
2044 ;; org-babel-call-process-region-original is the original emacs
2045 ;; definition. It is in scope from the let binding in
2046 ;; org-babel-execute-src-block
2047 (apply org-babel-call-process-region-original
2048 start end program delete buffer display args
)))
2050 (defun org-babel-local-file-name (file)
2051 "Return the local name component of FILE."
2052 (if (file-remote-p file
)
2054 (with-parsed-tramp-file-name file nil
2058 (defun org-babel-process-file-name (name &optional no-quote-p
)
2059 "Prepare NAME to be used in an external process.
2060 If NAME specifies a remote location, the remote portion of the
2061 name is removed, since in that case the process will be executing
2062 remotely. The file name is then processed by
2063 `expand-file-name'. Unless second argument NO-QUOTE-P is non-nil,
2064 the file name is additionally processed by
2065 `shell-quote-argument'"
2066 ((lambda (f) (if no-quote-p f
(shell-quote-argument f
)))
2067 (expand-file-name (org-babel-local-file-name name
))))
2069 (defvar org-babel-temporary-directory
)
2070 (unless (or noninteractive
(boundp 'org-babel-temporary-directory
))
2071 (defvar org-babel-temporary-directory
2072 (or (and (boundp 'org-babel-temporary-directory
)
2073 (file-exists-p org-babel-temporary-directory
)
2074 org-babel-temporary-directory
)
2075 (make-temp-file "babel-" t
))
2076 "Directory to hold temporary files created to execute code blocks.
2077 Used by `org-babel-temp-file'. This directory will be removed on
2080 (defun org-babel-temp-file (prefix &optional suffix
)
2081 "Create a temporary file in the `org-babel-temporary-directory'.
2082 Passes PREFIX and SUFFIX directly to `make-temp-file' with the
2083 value of `temporary-file-directory' temporarily set to the value
2084 of `org-babel-temporary-directory'."
2085 (if (file-remote-p default-directory
)
2087 (concat (file-remote-p default-directory
)
2089 prefix temporary-file-directory
)
2091 (let ((temporary-file-directory
2092 (or (and (boundp 'org-babel-temporary-directory
)
2093 (file-exists-p org-babel-temporary-directory
)
2094 org-babel-temporary-directory
)
2095 temporary-file-directory
)))
2096 (make-temp-file prefix nil suffix
))))
2098 (defun org-babel-remove-temporary-directory ()
2099 "Remove `org-babel-temporary-directory' on Emacs shutdown."
2100 (when (and (boundp 'org-babel-temporary-directory
)
2101 (file-exists-p org-babel-temporary-directory
))
2102 ;; taken from `delete-directory' in files.el
2105 (mapc (lambda (file)
2106 ;; This test is equivalent to
2107 ;; (and (file-directory-p fn) (not (file-symlink-p fn)))
2108 ;; but more efficient
2109 (if (eq t
(car (file-attributes file
)))
2110 (delete-directory file
)
2111 (delete-file file
)))
2112 ;; We do not want to delete "." and "..".
2113 (directory-files org-babel-temporary-directory
'full
2114 "^\\([^.]\\|\\.\\([^.]\\|\\..\\)\\).*"))
2115 (delete-directory org-babel-temporary-directory
))
2117 (message "Failed to remove temporary Org-babel directory %s"
2118 (if (boundp 'org-babel-temporary-directory
)
2119 org-babel-temporary-directory
2120 "[directory not defined]"))))))
2122 (add-hook 'kill-emacs-hook
'org-babel-remove-temporary-directory
)
2126 ;; arch-tag: 01a7ebee-06c5-4ee4-a709-e660d28c0af1