1 ;;; ob-core.el --- working with code blocks in org-mode
3 ;; Copyright (C) 2009-2012 Free Software Foundation, Inc.
5 ;; Authors: Eric Schulte
7 ;; Keywords: literate programming, reproducible research
8 ;; 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/>.
32 (defconst org-babel-exeext
33 (if (memq system-type
'(windows-nt cygwin
))
36 ;; dynamically scoped for tramp
37 (defvar org-babel-call-process-region-original nil
)
38 (defvar org-src-lang-modes
)
39 (defvar org-babel-library-of-babel
)
40 (declare-function show-all
"outline" ())
41 (declare-function org-reduce
"org" (CL-FUNC CL-SEQ
&rest CL-KEYS
))
42 (declare-function org-mark-ring-push
"org" (&optional pos buffer
))
43 (declare-function tramp-compat-make-temp-file
"tramp-compat"
44 (filename &optional dir-flag
))
45 (declare-function tramp-dissect-file-name
"tramp" (name &optional nodefault
))
46 (declare-function tramp-file-name-user
"tramp" (vec))
47 (declare-function tramp-file-name-host
"tramp" (vec))
48 (declare-function with-parsed-tramp-file-name
"tramp" (filename var
&rest body
))
49 (declare-function org-icompleting-read
"org" (&rest args
))
50 (declare-function org-edit-src-code
"org-src"
51 (&optional context code edit-buffer-name quietp
))
52 (declare-function org-edit-src-exit
"org-src" (&optional context
))
53 (declare-function org-open-at-point
"org" (&optional in-emacs reference-buffer
))
54 (declare-function org-save-outline-visibility
"org-macs" (use-markers &rest body
))
55 (declare-function org-outline-overlay-data
"org" (&optional use-markers
))
56 (declare-function org-set-outline-overlay-data
"org" (data))
57 (declare-function org-narrow-to-subtree
"org" ())
58 (declare-function org-split-string
"org" (string &optional separators
))
59 (declare-function org-entry-get
"org"
60 (pom property
&optional inherit literal-nil
))
61 (declare-function org-make-options-regexp
"org" (kwds &optional extra
))
62 (declare-function org-do-remove-indentation
"org" (&optional n
))
63 (declare-function org-next-block
"org" (arg &optional backward block-regexp
))
64 (declare-function org-previous-block
"org" (arg &optional block-regexp
))
65 (declare-function org-show-context
"org" (&optional key
))
66 (declare-function org-at-table-p
"org" (&optional table-type
))
67 (declare-function org-cycle
"org" (&optional arg
))
68 (declare-function org-uniquify
"org" (list))
69 (declare-function org-current-level
"org" ())
70 (declare-function org-table-import
"org-table" (file arg
))
71 (declare-function org-add-hook
"org-compat"
72 (hook function
&optional append local
))
73 (declare-function org-table-align
"org-table" ())
74 (declare-function org-table-end
"org-table" (&optional table-type
))
75 (declare-function orgtbl-to-generic
"org-table" (table params
))
76 (declare-function orgtbl-to-orgtbl
"org-table" (table params
))
77 (declare-function org-babel-tangle-comment-links
"ob-tangle" (&optional info
))
78 (declare-function org-babel-lob-get-info
"ob-lob" nil
)
79 (declare-function org-babel-ref-split-args
"ob-ref" (arg-string))
80 (declare-function org-babel-ref-parse
"ob-ref" (assignment))
81 (declare-function org-babel-ref-resolve
"ob-ref" (ref))
82 (declare-function org-babel-ref-goto-headline-id
"ob-ref" (id))
83 (declare-function org-babel-ref-headline-body
"ob-ref" ())
84 (declare-function org-babel-lob-execute-maybe
"ob-lob" ())
85 (declare-function org-number-sequence
"org-compat" (from &optional to inc
))
86 (declare-function org-at-item-p
"org-list" ())
87 (declare-function org-list-parse-list
"org-list" (&optional delete
))
88 (declare-function org-list-to-generic
"org-list" (LIST PARAMS
))
89 (declare-function org-list-struct
"org-list" ())
90 (declare-function org-list-prevs-alist
"org-list" (struct))
91 (declare-function org-list-get-list-end
"org-list" (item struct prevs
))
92 (declare-function org-remove-if
"org" (predicate seq
))
93 (declare-function org-completing-read
"org" (&rest args
))
94 (declare-function org-escape-code-in-region
"org-src" (beg end
))
95 (declare-function org-unescape-code-in-string
"org-src" (s))
96 (declare-function org-table-to-lisp
"org-table" (&optional txt
))
97 (declare-function org-reverse-string
"org" (string))
99 (defgroup org-babel nil
100 "Code block evaluation and management in `org-mode' documents."
104 (defcustom org-confirm-babel-evaluate t
105 "Confirm before evaluation.
106 Require confirmation before interactively evaluating code
107 blocks in Org-mode buffers. The default value of this variable
108 is t, meaning confirmation is required for any code block
109 evaluation. This variable can be set to nil to inhibit any
110 future confirmation requests. This variable can also be set to a
111 function which takes two arguments the language of the code block
112 and the body of the code block. Such a function should then
113 return a non-nil value if the user should be prompted for
114 execution or nil if no prompt is required.
116 Warning: Disabling confirmation may result in accidental
117 evaluation of potentially harmful code. It may be advisable
118 remove code block execution from C-c C-c as further protection
119 against accidental code block evaluation. The
120 `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can be used to
121 remove code block execution from the C-c C-c keybinding."
124 :type
'(choice boolean function
))
125 ;; don't allow this variable to be changed through file settings
126 (put 'org-confirm-babel-evaluate
'safe-local-variable
(lambda (x) (eq x t
)))
128 (defcustom org-babel-no-eval-on-ctrl-c-ctrl-c nil
129 "Remove code block evaluation from the C-c C-c key binding."
134 (defcustom org-babel-results-keyword
"RESULTS"
135 "Keyword used to name results generated by code blocks.
136 Should be either RESULTS or NAME however any capitalization may
140 :package-version
'(Org .
"8.0")
143 (defcustom org-babel-noweb-wrap-start
"<<"
144 "String used to begin a noweb reference in a code block.
145 See also `org-babel-noweb-wrap-end'."
149 (defcustom org-babel-noweb-wrap-end
">>"
150 "String used to end a noweb reference in a code block.
151 See also `org-babel-noweb-wrap-start'."
155 (defun org-babel-noweb-wrap (&optional regexp
)
156 (concat org-babel-noweb-wrap-start
157 (or regexp
"\\([^ \t\n].+?[^ \t]\\|[^ \t\n]\\)")
158 org-babel-noweb-wrap-end
))
160 (defvar org-babel-src-name-regexp
161 "^[ \t]*#\\+name:[ \t]*"
162 "Regular expression used to match a source name line.")
164 (defvar org-babel-multi-line-header-regexp
165 "^[ \t]*#\\+headers?:[ \t]*\\([^\n]*\\)$"
166 "Regular expression used to match multi-line header arguments.")
168 (defvar org-babel-src-name-w-name-regexp
169 (concat org-babel-src-name-regexp
171 org-babel-multi-line-header-regexp
173 "\\([^ ()\f\t\n\r\v]+\\)\\(\(\\(.*\\)\)\\|\\)")
174 "Regular expression matching source name lines with a name.")
176 (defvar org-babel-src-block-regexp
178 ;; (1) indentation (2) lang
179 "^\\([ \t]*\\)#\\+begin_src[ \t]+\\([^ \f\t\n\r\v]+\\)[ \t]*"
181 "\\([^\":\n]*\"[^\"\n*]*\"[^\":\n]*\\|[^\":\n]*\\)"
182 ;; (4) header arguments
185 "\\([^\000]*?\n\\)?[ \t]*#\\+end_src")
186 "Regexp used to identify code blocks.")
188 (defvar org-babel-inline-src-block-regexp
190 ;; (1) replacement target (2) lang
191 "\\(?:^\\|[^-[:alnum:]]\\)\\(src_\\([^ \f\t\n\r\v]+\\)"
192 ;; (3,4) (unused, headers)
193 "\\(\\|\\[\\(.*?\\)\\]\\)"
195 "{\\([^\f\n\r\v]+?\\)}\\)")
196 "Regexp used to identify inline src-blocks.")
198 (defun org-babel-get-header (params key
&optional others
)
199 "Select only header argument of type KEY from a list.
200 Optional argument OTHERS indicates that only the header that do
201 not match KEY should be returned."
204 (lambda (p) (when (funcall (if others
#'not
#'identity
) (eq (car p
) key
)) p
))
207 (defun org-babel-get-inline-src-block-matches()
208 "Set match data if within body of an inline source block.
209 Returns non-nil if match-data set"
210 (let ((src-at-0-p (save-excursion
211 (beginning-of-line 1)
212 (string= "src" (thing-at-point 'word
))))
213 (first-line-p (= 1 (line-number-at-pos)))
215 (let ((search-for (cond ((and src-at-0-p first-line-p
"src_"))
216 (first-line-p "[[:punct:] \t]src_")
217 (t "[[:punct:] \f\t\n\r\v]src_")))
218 (lower-limit (if first-line-p
220 (- (point-at-bol) 1))))
222 (when (or (and src-at-0-p
(bobp))
223 (and (re-search-forward "}" (point-at-eol) t
)
224 (re-search-backward search-for lower-limit t
)
226 (when (looking-at org-babel-inline-src-block-regexp
)
229 (defvar org-babel-inline-lob-one-liner-regexp
)
230 (defun org-babel-get-lob-one-liner-matches()
231 "Set match data if on line of an lob one liner.
232 Returns non-nil if match-data set"
234 (unless (= (point) (point-at-bol)) ;; move before inline block
235 (re-search-backward "[ \f\t\n\r\v]" nil t
))
236 (if (looking-at org-babel-inline-lob-one-liner-regexp
)
240 (defun org-babel-get-src-block-info (&optional light
)
241 "Get information on the current source block.
243 Optional argument LIGHT does not resolve remote variable
244 references; a process which could likely result in the execution
245 of other code blocks.
248 (language body header-arguments-alist switches name indent)."
249 (let ((case-fold-search t
) head info name indent
)
251 (if (setq head
(org-babel-where-is-src-block-head))
254 (setq info
(org-babel-parse-src-block-match))
255 (setq indent
(car (last info
)))
256 (setq info
(butlast info
))
257 (while (and (forward-line -
1)
258 (looking-at org-babel-multi-line-header-regexp
))
260 (org-babel-merge-params
262 (org-babel-parse-header-arguments (match-string 1)))))
263 (when (looking-at org-babel-src-name-w-name-regexp
)
264 (setq name
(org-no-properties (match-string 3)))
265 (when (and (match-string 5) (> (length (match-string 5)) 0))
266 (setf (nth 2 info
) ;; merge functional-syntax vars and header-args
267 (org-babel-merge-params
269 (lambda (ref) (cons :var ref
))
271 (lambda (var) ;; check that each variable is initialized
272 (if (string-match ".+=.+" var
)
275 "variable \"%s\"%s must be assigned a default value"
276 var
(if name
(format " in block \"%s\"" name
) ""))))
277 (org-babel-ref-split-args (match-string 5))))
279 ;; inline source block
280 (when (org-babel-get-inline-src-block-matches)
281 (setq info
(org-babel-parse-inline-src-block-match))))
282 ;; resolve variable references and add summary parameters
283 (when (and info
(not light
))
284 (setf (nth 2 info
) (org-babel-process-params (nth 2 info
))))
285 (when info
(append info
(list name indent
)))))
287 (defvar org-current-export-file
) ; dynamically bound
288 (defmacro org-babel-check-confirm-evaluate
(info &rest body
)
289 "Evaluate BODY with special execution confirmation variables set.
291 Specifically; NOEVAL will indicate if evaluation is allowed,
292 QUERY will indicate if a user query is required, CODE-BLOCK will
293 hold the language of the code block, and BLOCK-NAME will hold the
294 name of the code block."
295 (declare (indent defun
))
297 (lang block-body headers name eval eval-no export eval-no-export
)
298 `(let* ((,lang
(nth 0 ,info
))
299 (,block-body
(nth 1 ,info
))
300 (,headers
(nth 2 ,info
))
301 (,name
(nth 4 ,info
))
302 (,eval
(or (cdr (assoc :eval
,headers
))
303 (when (assoc :noeval
,headers
) "no")))
304 (,eval-no
(or (equal ,eval
"no")
305 (equal ,eval
"never")))
306 (,export
(org-bound-and-true-p org-current-export-file
))
307 (,eval-no-export
(and ,export
(or (equal ,eval
"no-export")
308 (equal ,eval
"never-export"))))
309 (noeval (or ,eval-no
,eval-no-export
))
310 (query (or (equal ,eval
"query")
311 (and ,export
(equal ,eval
"query-export"))
312 (when (functionp org-confirm-babel-evaluate
)
313 (funcall org-confirm-babel-evaluate
315 org-confirm-babel-evaluate
))
316 (code-block (if ,info
(format " %s " ,lang
) " "))
317 (block-name (if ,name
(format " (%s) " ,name
) " ")))
320 (defsubst org-babel-check-evaluate
(info)
321 "Check if code block INFO should be evaluated.
322 Do not query the user."
323 (org-babel-check-confirm-evaluate info
325 (message (format "Evaluation of this%scode-block%sis disabled."
326 code-block block-name
))))))
328 ;; dynamically scoped for asynchroneous export
329 (defvar org-babel-confirm-evaluate-answer-no
)
331 (defsubst org-babel-confirm-evaluate
(info)
332 "Confirm evaluation of the code block INFO.
334 If the variable `org-babel-confirm-evaluate-answer-no´ is bound
335 to a non-nil value, auto-answer with \"no\".
337 This query can also be suppressed by setting the value of
338 `org-confirm-babel-evaluate' to nil, in which case all future
339 interactive code block evaluations will proceed without any
340 confirmation from the user.
342 Note disabling confirmation may result in accidental evaluation
343 of potentially harmful code."
344 (org-babel-check-confirm-evaluate info
347 (and (not (org-bound-and-true-p
348 org-babel-confirm-evaluate-answer-no
))
350 (format "Evaluate this%scode block%son your system? "
351 code-block block-name
)))
352 (message (format "Evaluation of this%scode-block%sis aborted."
353 code-block block-name
)))))))
356 (defun org-babel-execute-safely-maybe ()
357 (unless org-babel-no-eval-on-ctrl-c-ctrl-c
358 (org-babel-execute-maybe)))
360 (add-hook 'org-ctrl-c-ctrl-c-hook
'org-babel-execute-safely-maybe
)
363 (defun org-babel-execute-maybe ()
365 (or (org-babel-execute-src-block-maybe)
366 (org-babel-lob-execute-maybe)))
368 (defun org-babel-execute-src-block-maybe ()
369 "Conditionally execute a source block.
370 Detect if this is context for a Babel src-block and if so
371 then run `org-babel-execute-src-block'."
373 (let ((info (org-babel-get-src-block-info)))
375 (progn (org-babel-eval-wipe-error-buffer)
376 (org-babel-execute-src-block current-prefix-arg info
) t
) nil
)))
379 (defun org-babel-view-src-block-info ()
380 "Display information on the current source block.
381 This includes header arguments, language and name, and is largely
382 a window into the `org-babel-get-src-block-info' function."
384 (let ((info (org-babel-get-src-block-info 'light
))
385 (full (lambda (it) (> (length it
) 0)))
386 (printf (lambda (fmt &rest args
) (princ (apply #'format fmt args
)))))
388 (with-help-window (help-buffer)
389 (let ((name (nth 4 info
))
391 (switches (nth 3 info
))
392 (header-args (nth 2 info
)))
393 (when name
(funcall printf
"Name: %s\n" name
))
394 (when lang
(funcall printf
"Lang: %s\n" lang
))
395 (when (funcall full switches
) (funcall printf
"Switches: %s\n" switches
))
396 (funcall printf
"Header Arguments:\n")
397 (dolist (pair (sort header-args
398 (lambda (a b
) (string< (symbol-name (car a
))
399 (symbol-name (car b
))))))
400 (when (funcall full
(cdr pair
))
401 (funcall printf
"\t%S%s\t%s\n"
403 (if (> (length (format "%S" (car pair
))) 7) "" "\t")
407 (defun org-babel-expand-src-block-maybe ()
408 "Conditionally expand a source block.
409 Detect if this is context for a org-babel src-block and if so
410 then run `org-babel-expand-src-block'."
412 (let ((info (org-babel-get-src-block-info)))
414 (progn (org-babel-expand-src-block current-prefix-arg info
) t
)
418 (defun org-babel-load-in-session-maybe ()
419 "Conditionally load a source block in a session.
420 Detect if this is context for a org-babel src-block and if so
421 then run `org-babel-load-in-session'."
423 (let ((info (org-babel-get-src-block-info)))
425 (progn (org-babel-load-in-session current-prefix-arg info
) t
)
428 (add-hook 'org-metaup-hook
'org-babel-load-in-session-maybe
)
431 (defun org-babel-pop-to-session-maybe ()
432 "Conditionally pop to a session.
433 Detect if this is context for a org-babel src-block and if so
434 then run `org-babel-pop-to-session'."
436 (let ((info (org-babel-get-src-block-info)))
437 (if info
(progn (org-babel-pop-to-session current-prefix-arg info
) t
) nil
)))
439 (add-hook 'org-metadown-hook
'org-babel-pop-to-session-maybe
)
441 (defconst org-babel-common-header-args-w-values
442 '((cache .
((no yes
)))
444 (colnames .
((nil no yes
)))
445 (comments .
((no link yes org both noweb
)))
447 (eval .
((never query
)))
448 (exports .
((code results both none
)))
451 (hlines .
((no yes
)))
452 (mkdirp .
((yes no
)))
455 (noweb .
((yes no tangle no-export strip-export
)))
458 (padline .
((yes no
)))
459 (results .
((file list vector table scalar verbatim
)
460 (raw html latex org code pp drawer
)
461 (replace silent none append prepend
)
463 (rownames .
((no yes
)))
467 (tangle .
((tangle yes no
:any
)))
471 (defconst org-babel-header-arg-names
472 (mapcar #'car org-babel-common-header-args-w-values
)
473 "Common header arguments used by org-babel.
474 Note that individual languages may define their own language
475 specific header arguments as well.")
477 (defvar org-babel-default-header-args
478 '((:session .
"none") (:results .
"replace") (:exports .
"code")
479 (:cache .
"no") (:noweb .
"no") (:hlines .
"no") (:tangle .
"no")
480 (:padnewline .
"yes"))
481 "Default arguments to use when evaluating a source block.")
483 (defvar org-babel-default-inline-header-args
484 '((:session .
"none") (:results .
"replace") (:exports .
"results"))
485 "Default arguments to use when evaluating an inline source block.")
487 (defvar org-babel-data-names
'("tblname" "results" "name"))
489 (defvar org-babel-result-regexp
490 (concat "^[ \t]*#\\+"
491 (regexp-opt org-babel-data-names t
)
493 ;; FIXME The string below is `org-ts-regexp'
494 "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
495 " \\)?\\([[:alnum:]]+\\)\\]\\)?\\:[ \t]*")
496 "Regular expression used to match result lines.
497 If the results are associated with a hash key then the hash will
498 be saved in the second match data.")
500 (defvar org-babel-result-w-name-regexp
501 (concat org-babel-result-regexp
502 "\\([^ ()\f\t\n\r\v]+\\)\\(\(\\(.*\\)\)\\|\\)"))
504 (defvar org-babel-min-lines-for-block-output
10
505 "The minimum number of lines for block output.
506 If number of lines of output is equal to or exceeds this
507 value, the output is placed in a #+begin_example...#+end_example
508 block. Otherwise the output is marked as literal by inserting
509 colons at the starts of the lines. This variable only takes
510 effect if the :results output option is in effect.")
512 (defvar org-babel-noweb-error-langs nil
513 "Languages for which Babel will raise literate programming errors.
514 List of languages for which errors should be raised when the
515 source code block satisfying a noweb reference in this language
516 can not be resolved.")
518 (defvar org-babel-hash-show
4
519 "Number of initial characters to show of a hidden results hash.")
521 (defvar org-babel-hash-show-time nil
522 "When not nil show the time the code block was evaluated in the result hash.")
524 (defvar org-babel-after-execute-hook nil
525 "Hook for functions to be called after `org-babel-execute-src-block'")
527 (defun org-babel-named-src-block-regexp-for-name (name)
528 "This generates a regexp used to match a src block named NAME."
529 (concat org-babel-src-name-regexp
(regexp-quote name
)
530 "[ \t(]*[\r\n]\\(?:^#.*[\r\n]\\)*"
531 (substring org-babel-src-block-regexp
1)))
533 (defun org-babel-named-data-regexp-for-name (name)
534 "This generates a regexp used to match data named NAME."
535 (concat org-babel-result-regexp
(regexp-quote name
) "\\([ \t]\\|$\\)"))
538 (defvar call-process-region
)
541 (defun org-babel-execute-src-block (&optional arg info params
)
542 "Execute the current source code block.
543 Insert the results of execution into the buffer. Source code
544 execution and the collection and formatting of results can be
545 controlled through a variety of header arguments.
547 With prefix argument ARG, force re-execution even if an existing
548 result cached in the buffer would otherwise have been returned.
550 Optionally supply a value for INFO in the form returned by
551 `org-babel-get-src-block-info'.
553 Optionally supply a value for PARAMS which will be merged with
554 the header arguments specified at the front of the source code
557 (let* ((info (or info
(org-babel-get-src-block-info)))
558 (merged-params (org-babel-merge-params (nth 2 info
) params
)))
559 (when (org-babel-check-evaluate
560 (let ((i info
)) (setf (nth 2 i
) merged-params
) i
))
561 (let* ((params (if params
562 (org-babel-process-params merged-params
)
564 (cachep (and (not arg
) (cdr (assoc :cache params
))
565 (string= "yes" (cdr (assoc :cache params
)))))
566 (new-hash (when cachep
(org-babel-sha1-hash info
)))
567 (old-hash (when cachep
(org-babel-current-result-hash)))
568 (cache-current-p (and (not arg
) new-hash
569 (equal new-hash old-hash
))))
572 (save-excursion ;; return cached result
573 (goto-char (org-babel-where-is-src-block-result nil info
))
574 (end-of-line 1) (forward-char 1)
575 (let ((result (org-babel-read-result)))
576 (message (replace-regexp-in-string
577 "%" "%%" (format "%S" result
))) result
)))
578 ((org-babel-confirm-evaluate
579 (let ((i info
)) (setf (nth 2 i
) merged-params
) i
))
580 (let* ((lang (nth 0 info
))
581 (result-params (cdr (assoc :result-params params
)))
582 (body (setf (nth 1 info
)
583 (if (org-babel-noweb-p params
:eval
)
584 (org-babel-expand-noweb-references info
)
586 (dir (cdr (assoc :dir params
)))
588 (or (and dir
(file-name-as-directory (expand-file-name dir
)))
590 (org-babel-call-process-region-original ;; for tramp handler
591 (or (org-bound-and-true-p
592 org-babel-call-process-region-original
)
593 (symbol-function 'call-process-region
)))
594 (indent (car (last info
)))
597 (let ((call-process-region
599 (apply 'org-babel-tramp-handle-call-process-region
603 (let ((f (intern (concat "org-babel-execute:" f
))))
604 (when (fboundp f
) f
)))))
606 (or (funcall lang-check lang
)
609 (cdr (assoc lang org-src-lang-modes
))))
610 (error "No org-babel-execute function for %s!"
612 (message "executing %s code block%s..."
614 (if (nth 4 info
) (format " (%s)" (nth 4 info
)) ""))
615 (if (member "none" result-params
)
617 (funcall cmd body params
)
618 (message "result silenced"))
621 (if (and (eq (cdr (assoc :result-type params
))
623 (or (member "vector" result-params
)
624 (member "table" result-params
))
625 (not (listp result
)))
626 (list (list result
)) result
))
627 (funcall cmd body params
)))
628 ;; if non-empty result and :file then write to :file
629 (when (cdr (assoc :file params
))
631 (with-temp-file (cdr (assoc :file params
))
633 (org-babel-format-result
634 result
(cdr (assoc :sep
(nth 2 info
)))))))
635 (setq result
(cdr (assoc :file params
))))
636 (org-babel-insert-result
637 result result-params info new-hash indent lang
)
638 (run-hooks 'org-babel-after-execute-hook
)
640 (setq call-process-region
641 'org-babel-call-process-region-original
)))))))))
643 (defun org-babel-expand-body:generic
(body params
&optional var-lines
)
644 "Expand BODY with PARAMS.
645 Expand a block of code with org-babel according to its header
646 arguments. This generic implementation of body expansion is
647 called for languages which have not defined their own specific
648 org-babel-expand-body:lang function."
649 (mapconcat #'identity
(append var-lines
(list body
)) "\n"))
652 (defun org-babel-expand-src-block (&optional arg info params
)
653 "Expand the current source code block.
654 Expand according to the source code block's header
655 arguments and pop open the results in a preview buffer."
657 (let* ((info (or info
(org-babel-get-src-block-info)))
659 (params (setf (nth 2 info
)
660 (sort (org-babel-merge-params (nth 2 info
) params
)
661 (lambda (el1 el2
) (string< (symbol-name (car el1
))
662 (symbol-name (car el2
)))))))
663 (body (setf (nth 1 info
)
664 (if (org-babel-noweb-p params
:eval
)
665 (org-babel-expand-noweb-references info
) (nth 1 info
))))
666 (expand-cmd (intern (concat "org-babel-expand-body:" lang
)))
667 (assignments-cmd (intern (concat "org-babel-variable-assignments:"
670 (if (fboundp expand-cmd
) (funcall expand-cmd body params
)
671 (org-babel-expand-body:generic
672 body params
(and (fboundp assignments-cmd
)
673 (funcall assignments-cmd params
))))))
674 (if (called-interactively-p 'any
)
677 (concat "*Org-Babel Preview " (buffer-name) "[ " lang
" ]*"))
680 (defun org-babel-edit-distance (s1 s2
)
681 "Return the edit (levenshtein) distance between strings S1 S2."
682 (let* ((l1 (length s1
))
684 (dist (vconcat (mapcar (lambda (_) (make-vector (1+ l2
) nil
))
685 (number-sequence 1 (1+ l1
)))))
686 (in (lambda (i j
) (aref (aref dist i
) j
))))
687 (setf (aref (aref dist
0) 0) 0)
688 (dolist (j (number-sequence 1 l2
))
689 (setf (aref (aref dist
0) j
) j
))
690 (dolist (i (number-sequence 1 l1
))
691 (setf (aref (aref dist i
) 0) i
)
692 (dolist (j (number-sequence 1 l2
))
693 (setf (aref (aref dist i
) j
)
695 (1+ (funcall in
(1- i
) j
))
696 (1+ (funcall in i
(1- j
)))
697 (+ (if (equal (aref s1
(1- i
)) (aref s2
(1- j
))) 0 1)
698 (funcall in
(1- i
) (1- j
)))))))
701 (defun org-babel-combine-header-arg-lists (original &rest others
)
702 "Combine a number of lists of header argument names and arguments."
703 (let ((results (copy-sequence original
)))
704 (dolist (new-list others
)
705 (dolist (arg-pair new-list
)
706 (let ((header (car arg-pair
))
707 (args (cdr arg-pair
)))
709 (cons arg-pair
(org-remove-if
710 (lambda (pair) (equal header
(car pair
)))
715 (defun org-babel-check-src-block ()
716 "Check for misspelled header arguments in the current code block."
718 ;; TODO: report malformed code block
719 ;; TODO: report incompatible combinations of header arguments
720 ;; TODO: report uninitialized variables
721 (let ((too-close 2) ;; <- control closeness to report potential match
722 (names (mapcar #'symbol-name org-babel-header-arg-names
)))
723 (dolist (header (mapcar (lambda (arg) (substring (symbol-name (car arg
)) 1))
724 (and (org-babel-where-is-src-block-head)
725 (org-babel-parse-header-arguments
727 (match-string 4))))))
729 (when (and (not (string= header name
))
730 (<= (org-babel-edit-distance header name
) too-close
)
731 (not (member header names
)))
732 (error "Supplied header \"%S\" is suspiciously close to \"%S\""
734 (message "No suspicious header arguments found.")))
737 (defun org-babel-insert-header-arg ()
738 "Insert a header argument selecting from lists of common args and values."
740 (let* ((lang (car (org-babel-get-src-block-info 'light
)))
741 (lang-headers (intern (concat "org-babel-header-args:" lang
)))
742 (headers (org-babel-combine-header-arg-lists
743 org-babel-common-header-args-w-values
744 (if (boundp lang-headers
) (eval lang-headers
) nil
)))
745 (arg (org-icompleting-read
748 (lambda (header-spec) (symbol-name (car header-spec
)))
751 (let ((vals (cdr (assoc (intern arg
) headers
))))
757 (read-from-minibuffer "value: "))
761 (let ((arg (org-icompleting-read
763 (cons "default" (mapcar #'symbol-name group
)))))
764 (if (and arg
(not (string= "default" arg
)))
769 ;; Add support for completing-read insertion of header arguments after ":"
770 (defun org-babel-header-arg-expand ()
771 "Call `org-babel-enter-header-arg-w-completion' in appropriate contexts."
772 (when (and (equal (char-before) ?\
:) (org-babel-where-is-src-block-head))
773 (org-babel-enter-header-arg-w-completion (match-string 2))))
775 (defun org-babel-enter-header-arg-w-completion (&optional lang
)
776 "Insert header argument appropriate for LANG with completion."
777 (let* ((lang-headers-var (intern (concat "org-babel-header-args:" lang
)))
778 (lang-headers (when (boundp lang-headers-var
) (eval lang-headers-var
)))
779 (headers-w-values (org-babel-combine-header-arg-lists
780 org-babel-common-header-args-w-values lang-headers
))
781 (headers (mapcar #'symbol-name
(mapcar #'car headers-w-values
)))
782 (header (org-completing-read "Header Arg: " headers
))
783 (args (cdr (assoc (intern header
) headers-w-values
)))
784 (arg (when (and args
(listp args
))
786 (format "%s: " header
)
787 (mapcar #'symbol-name
(apply #'append args
))))))
788 (insert (concat header
" " (or arg
"")))
791 (add-hook 'org-tab-first-hook
'org-babel-header-arg-expand
)
794 (defun org-babel-load-in-session (&optional arg info
)
795 "Load the body of the current source-code block.
796 Evaluate the header arguments for the source block before
797 entering the session. After loading the body this pops open the
800 (let* ((info (or info
(org-babel-get-src-block-info)))
802 (params (nth 2 info
))
804 (user-error "No src code block at point")
806 (if (org-babel-noweb-p params
:eval
)
807 (org-babel-expand-noweb-references info
)
809 (session (cdr (assoc :session params
)))
810 (dir (cdr (assoc :dir params
)))
812 (or (and dir
(file-name-as-directory dir
)) default-directory
))
813 (cmd (intern (concat "org-babel-load-session:" lang
))))
814 (unless (fboundp cmd
)
815 (error "No org-babel-load-session function for %s!" lang
))
816 (pop-to-buffer (funcall cmd session body params
))
820 (defun org-babel-initiate-session (&optional arg info
)
821 "Initiate session for current code block.
822 If called with a prefix argument then resolve any variable
823 references in the header arguments and assign these variables in
824 the session. Copy the body of the code block to the kill ring."
826 (let* ((info (or info
(org-babel-get-src-block-info (not arg
))))
829 (params (nth 2 info
))
830 (session (cdr (assoc :session params
)))
831 (dir (cdr (assoc :dir params
)))
833 (or (and dir
(file-name-as-directory dir
)) default-directory
))
834 (init-cmd (intern (format "org-babel-%s-initiate-session" lang
)))
835 (prep-cmd (intern (concat "org-babel-prep-session:" lang
))))
836 (if (and (stringp session
) (string= session
"none"))
837 (error "This block is not using a session!"))
838 (unless (fboundp init-cmd
)
839 (error "No org-babel-initiate-session function for %s!" lang
))
840 (with-temp-buffer (insert (org-babel-trim body
))
841 (copy-region-as-kill (point-min) (point-max)))
843 (unless (fboundp prep-cmd
)
844 (error "No org-babel-prep-session function for %s!" lang
))
845 (funcall prep-cmd session params
))
846 (funcall init-cmd session params
)))
849 (defun org-babel-switch-to-session (&optional arg info
)
850 "Switch to the session of the current code block.
851 Uses `org-babel-initiate-session' to start the session. If called
852 with a prefix argument then this is passed on to
853 `org-babel-initiate-session'."
855 (pop-to-buffer (org-babel-initiate-session arg info
))
858 (defalias 'org-babel-pop-to-session
'org-babel-switch-to-session
)
861 (defun org-babel-switch-to-session-with-code (&optional arg info
)
862 "Switch to code buffer and display session."
866 (let ((other-window-buffer (window-buffer (next-window))))
867 (set-window-buffer (next-window) (current-buffer))
868 (set-window-buffer (selected-window) other-window-buffer
))
870 (info (org-babel-get-src-block-info))
871 (org-src-window-setup 'reorganize-frame
))
873 (org-babel-switch-to-session arg info
))
875 (funcall swap-windows
)))
877 (defmacro org-babel-do-in-edit-buffer
(&rest body
)
878 "Evaluate BODY in edit buffer if there is a code block at point.
879 Return t if a code block was found at point, nil otherwise."
880 `(let ((org-src-window-setup 'switch-invisibly
))
881 (when (and (org-babel-where-is-src-block-head)
882 (org-edit-src-code nil nil nil
))
883 (unwind-protect (progn ,@body
)
884 (if (org-bound-and-true-p org-edit-src-from-org-mode
)
885 (org-edit-src-exit)))
887 (def-edebug-spec org-babel-do-in-edit-buffer
(body))
889 (defun org-babel-do-key-sequence-in-edit-buffer (key)
890 "Read key sequence and execute the command in edit buffer.
891 Enter a key sequence to be executed in the language major-mode
892 edit buffer. For example, TAB will alter the contents of the
893 Org-mode code block according to the effect of TAB in the
894 language major-mode buffer. For languages that support
895 interactive sessions, this can be used to send code from the Org
896 buffer to the session for evaluation using the native major-mode
897 evaluation mechanisms."
898 (interactive "kEnter key-sequence to execute in edit buffer: ")
899 (org-babel-do-in-edit-buffer
901 (key-binding (or key
(read-key-sequence nil
))))))
903 (defvar org-bracket-link-regexp
)
906 (defun org-babel-open-src-block-result (&optional re-run
)
907 "If `point' is on a src block then open the results of the
908 source code block, otherwise return nil. With optional prefix
909 argument RE-RUN the source-code block is evaluated even if
910 results already exist."
912 (let ((info (org-babel-get-src-block-info)))
915 ;; go to the results, if there aren't any then run the block
916 (goto-char (or (and (not re-run
) (org-babel-where-is-src-block-result))
917 (progn (org-babel-execute-src-block)
918 (org-babel-where-is-src-block-result))))
920 (while (looking-at "[\n\r\t\f ]") (forward-char 1))
922 (if (looking-at org-bracket-link-regexp
)
925 (let ((r (org-babel-format-result
926 (org-babel-read-result) (cdr (assoc :sep
(nth 2 info
))))))
927 (pop-to-buffer (get-buffer-create "*Org-Babel Results*"))
928 (delete-region (point-min) (point-max))
933 (defmacro org-babel-map-src-blocks
(file &rest body
)
934 "Evaluate BODY forms on each source-block in FILE.
935 If FILE is nil evaluate BODY forms on source blocks in current
936 buffer. During evaluation of BODY the following local variables
937 are set relative to the currently matched code block.
939 full-block ------- string holding the entirety of the code block
940 beg-block -------- point at the beginning of the code block
941 end-block -------- point at the end of the matched code block
942 lang ------------- string holding the language of the code block
943 beg-lang --------- point at the beginning of the lang
944 end-lang --------- point at the end of the lang
945 switches --------- string holding the switches
946 beg-switches ----- point at the beginning of the switches
947 end-switches ----- point at the end of the switches
948 header-args ------ string holding the header-args
949 beg-header-args -- point at the beginning of the header-args
950 end-header-args -- point at the end of the header-args
951 body ------------- string holding the body of the code block
952 beg-body --------- point at the beginning of the body
953 end-body --------- point at the end of the body"
955 (let ((tempvar (make-symbol "file")))
956 `(let* ((,tempvar
,file
)
957 (visited-p (or (null ,tempvar
)
958 (get-file-buffer (expand-file-name ,tempvar
))))
959 (point (point)) to-be-removed
)
960 (save-window-excursion
961 (when ,tempvar
(find-file ,tempvar
))
962 (setq to-be-removed
(current-buffer))
963 (goto-char (point-min))
964 (while (re-search-forward org-babel-src-block-regexp nil t
)
965 (goto-char (match-beginning 0))
966 (let ((full-block (match-string 0))
967 (beg-block (match-beginning 0))
968 (end-block (match-end 0))
969 (lang (match-string 2))
970 (beg-lang (match-beginning 2))
971 (end-lang (match-end 2))
972 (switches (match-string 3))
973 (beg-switches (match-beginning 3))
974 (end-switches (match-end 3))
975 (header-args (match-string 4))
976 (beg-header-args (match-beginning 4))
977 (end-header-args (match-end 4))
978 (body (match-string 5))
979 (beg-body (match-beginning 5))
980 (end-body (match-end 5)))
982 (goto-char end-block
))))
983 (unless visited-p
(kill-buffer to-be-removed
))
985 (def-edebug-spec org-babel-map-src-blocks
(form body
))
988 (defmacro org-babel-map-inline-src-blocks
(file &rest body
)
989 "Evaluate BODY forms on each inline source-block in FILE.
990 If FILE is nil evaluate BODY forms on source blocks in current
993 (let ((tempvar (make-symbol "file")))
994 `(let* ((,tempvar
,file
)
995 (visited-p (or (null ,tempvar
)
996 (get-file-buffer (expand-file-name ,tempvar
))))
997 (point (point)) to-be-removed
)
998 (save-window-excursion
999 (when ,tempvar
(find-file ,tempvar
))
1000 (setq to-be-removed
(current-buffer))
1001 (goto-char (point-min))
1002 (while (re-search-forward org-babel-inline-src-block-regexp nil t
)
1003 (goto-char (match-beginning 1))
1004 (save-match-data ,@body
)
1005 (goto-char (match-end 0))))
1006 (unless visited-p
(kill-buffer to-be-removed
))
1007 (goto-char point
))))
1008 (def-edebug-spec org-babel-map-inline-src-blocks
(form body
))
1010 (defvar org-babel-lob-one-liner-regexp
)
1013 (defmacro org-babel-map-call-lines
(file &rest body
)
1014 "Evaluate BODY forms on each call line in FILE.
1015 If FILE is nil evaluate BODY forms on source blocks in current
1017 (declare (indent 1))
1018 (let ((tempvar (make-symbol "file")))
1019 `(let* ((,tempvar
,file
)
1020 (visited-p (or (null ,tempvar
)
1021 (get-file-buffer (expand-file-name ,tempvar
))))
1022 (point (point)) to-be-removed
)
1023 (save-window-excursion
1024 (when ,tempvar
(find-file ,tempvar
))
1025 (setq to-be-removed
(current-buffer))
1026 (goto-char (point-min))
1027 (while (re-search-forward org-babel-lob-one-liner-regexp nil t
)
1028 (goto-char (match-beginning 1))
1029 (save-match-data ,@body
)
1030 (goto-char (match-end 0))))
1031 (unless visited-p
(kill-buffer to-be-removed
))
1032 (goto-char point
))))
1033 (def-edebug-spec org-babel-map-call-lines
(form body
))
1036 (defmacro org-babel-map-executables
(file &rest body
)
1037 (declare (indent 1))
1038 (let ((tempvar (make-symbol "file"))
1039 (rx (make-symbol "rx")))
1040 `(let* ((,tempvar
,file
)
1041 (,rx
(concat "\\(" org-babel-src-block-regexp
1042 "\\|" org-babel-inline-src-block-regexp
1043 "\\|" org-babel-lob-one-liner-regexp
"\\)"))
1044 (visited-p (or (null ,tempvar
)
1045 (get-file-buffer (expand-file-name ,tempvar
))))
1046 (point (point)) to-be-removed
)
1047 (save-window-excursion
1048 (when ,tempvar
(find-file ,tempvar
))
1049 (setq to-be-removed
(current-buffer))
1050 (goto-char (point-min))
1051 (while (re-search-forward ,rx nil t
)
1052 (goto-char (match-beginning 1))
1053 (when (looking-at org-babel-inline-src-block-regexp
)(forward-char 1))
1054 (save-match-data ,@body
)
1055 (goto-char (match-end 0))))
1056 (unless visited-p
(kill-buffer to-be-removed
))
1057 (goto-char point
))))
1058 (def-edebug-spec org-babel-map-executables
(form body
))
1061 (defun org-babel-execute-buffer (&optional arg
)
1062 "Execute source code blocks in a buffer.
1063 Call `org-babel-execute-src-block' on every source block in
1064 the current buffer."
1066 (org-babel-eval-wipe-error-buffer)
1067 (org-save-outline-visibility t
1068 (org-babel-map-executables nil
1069 (if (looking-at org-babel-lob-one-liner-regexp
)
1070 (org-babel-lob-execute-maybe)
1071 (org-babel-execute-src-block arg
)))))
1074 (defun org-babel-execute-subtree (&optional arg
)
1075 "Execute source code blocks in a subtree.
1076 Call `org-babel-execute-src-block' on every source block in
1077 the current subtree."
1081 (org-narrow-to-subtree)
1082 (org-babel-execute-buffer arg
)
1086 (defun org-babel-sha1-hash (&optional info
)
1087 "Generate an sha1 hash based on the value of info."
1089 (let ((print-level nil
)
1090 (info (or info
(org-babel-get-src-block-info))))
1092 (sort (copy-sequence (nth 2 info
))
1093 (lambda (a b
) (string< (car a
) (car b
)))))
1094 (let* ((rm (lambda (lst)
1095 (dolist (p '("replace" "silent" "none"
1096 "append" "prepend"))
1097 (setq lst
(remove p lst
)))
1100 (let ((v (if (and (listp (cdr arg
)) (null (cddr arg
)))
1101 (copy-sequence (cdr arg
))
1103 (when (and v
(not (and (sequencep v
)
1107 ((and (listp v
) ; lists are sorted
1108 (member (car arg
) '(:result-params
)))
1109 (sort (funcall rm v
) #'string
<))
1110 ((and (stringp v
) ; strings are sorted
1111 (member (car arg
) '(:results
:exports
)))
1112 (mapconcat #'identity
(sort (funcall rm
(split-string v
))
1116 (when (org-called-interactively-p 'interactive
) (message hash
)) hash
)
1117 (let ((it (format "%s-%s"
1120 (delq nil
(mapcar (lambda (arg)
1121 (let ((normalized (funcall norm arg
)))
1123 (format "%S" normalized
))))
1128 (defun org-babel-current-result-hash ()
1129 "Return the current in-buffer hash."
1130 (org-babel-where-is-src-block-result)
1131 (org-no-properties (match-string 5)))
1133 (defun org-babel-set-current-result-hash (hash)
1134 "Set the current in-buffer hash to HASH."
1135 (org-babel-where-is-src-block-result)
1136 (save-excursion (goto-char (match-beginning 3))
1137 ;; (mapc #'delete-overlay (overlays-at (point)))
1138 (replace-match hash nil nil nil
3)
1139 (org-babel-hide-hash)))
1141 (defun org-babel-hide-hash ()
1142 "Hide the hash in the current results line.
1143 Only the initial `org-babel-hash-show' characters of the hash
1144 will remain visible."
1145 (add-to-invisibility-spec '(org-babel-hide-hash . t
))
1147 (when (and (re-search-forward org-babel-result-regexp nil t
)
1149 (let* ((start (match-beginning 5))
1150 (hide-start (+ org-babel-hash-show start
))
1152 (hash (match-string 5))
1154 (setq ov1
(make-overlay start hide-start
))
1155 (setq ov2
(make-overlay hide-start end
))
1156 (overlay-put ov2
'invisible
'org-babel-hide-hash
)
1157 (overlay-put ov1
'babel-hash hash
)))))
1159 (defun org-babel-hide-all-hashes ()
1160 "Hide the hash in the current buffer.
1161 Only the initial `org-babel-hash-show' characters of each hash
1162 will remain visible. This function should be called as part of
1163 the `org-mode-hook'."
1165 (while (and (not org-babel-hash-show-time
)
1166 (re-search-forward org-babel-result-regexp nil t
))
1167 (goto-char (match-beginning 0))
1168 (org-babel-hide-hash)
1169 (goto-char (match-end 0)))))
1170 (add-hook 'org-mode-hook
'org-babel-hide-all-hashes
)
1172 (defun org-babel-hash-at-point (&optional point
)
1173 "Return the value of the hash at POINT.
1174 The hash is also added as the last element of the kill ring.
1175 This can be called with C-c C-c."
1177 (let ((hash (car (delq nil
(mapcar
1178 (lambda (ol) (overlay-get ol
'babel-hash
))
1179 (overlays-at (or point
(point))))))))
1180 (when hash
(kill-new hash
) (message hash
))))
1181 (add-hook 'org-ctrl-c-ctrl-c-hook
'org-babel-hash-at-point
)
1183 (defun org-babel-result-hide-spec ()
1184 "Hide portions of results lines.
1185 Add `org-babel-hide-result' as an invisibility spec for hiding
1186 portions of results lines."
1187 (add-to-invisibility-spec '(org-babel-hide-result . t
)))
1188 (add-hook 'org-mode-hook
'org-babel-result-hide-spec
)
1190 (defvar org-babel-hide-result-overlays nil
1191 "Overlays hiding results.")
1193 (defun org-babel-result-hide-all ()
1194 "Fold all results in the current buffer."
1196 (org-babel-show-result-all)
1198 (while (re-search-forward org-babel-result-regexp nil t
)
1199 (save-excursion (goto-char (match-beginning 0))
1200 (org-babel-hide-result-toggle-maybe)))))
1202 (defun org-babel-show-result-all ()
1203 "Unfold all results in the current buffer."
1204 (mapc 'delete-overlay org-babel-hide-result-overlays
)
1205 (setq org-babel-hide-result-overlays nil
))
1208 (defun org-babel-hide-result-toggle-maybe ()
1209 "Toggle visibility of result at point."
1211 (let ((case-fold-search t
))
1213 (beginning-of-line 1)
1214 (looking-at org-babel-result-regexp
))
1215 (progn (org-babel-hide-result-toggle)
1216 t
) ;; to signal that we took action
1217 nil
))) ;; to signal that we did not
1219 (defun org-babel-hide-result-toggle (&optional force
)
1220 "Toggle the visibility of the current result."
1224 (if (re-search-forward org-babel-result-regexp nil t
)
1225 (let ((start (progn (beginning-of-line 2) (- (point) 1)))
1227 (while (looking-at org-babel-multi-line-header-regexp
)
1229 (goto-char (- (org-babel-result-end) 1)) (point)))
1231 (if (memq t
(mapcar (lambda (overlay)
1232 (eq (overlay-get overlay
'invisible
)
1233 'org-babel-hide-result
))
1234 (overlays-at start
)))
1235 (if (or (not force
) (eq force
'off
))
1237 (when (member ov org-babel-hide-result-overlays
)
1238 (setq org-babel-hide-result-overlays
1239 (delq ov org-babel-hide-result-overlays
)))
1240 (when (eq (overlay-get ov
'invisible
)
1241 'org-babel-hide-result
)
1242 (delete-overlay ov
)))
1243 (overlays-at start
)))
1244 (setq ov
(make-overlay start end
))
1245 (overlay-put ov
'invisible
'org-babel-hide-result
)
1246 ;; make the block accessible to isearch
1248 ov
'isearch-open-invisible
1250 (when (member ov org-babel-hide-result-overlays
)
1251 (setq org-babel-hide-result-overlays
1252 (delq ov org-babel-hide-result-overlays
)))
1253 (when (eq (overlay-get ov
'invisible
)
1254 'org-babel-hide-result
)
1255 (delete-overlay ov
))))
1256 (push ov org-babel-hide-result-overlays
)))
1257 (error "Not looking at a result line"))))
1259 ;; org-tab-after-check-for-cycling-hook
1260 (add-hook 'org-tab-first-hook
'org-babel-hide-result-toggle-maybe
)
1261 ;; Remove overlays when changing major mode
1262 (add-hook 'org-mode-hook
1263 (lambda () (org-add-hook 'change-major-mode-hook
1264 'org-babel-show-result-all
'append
'local
)))
1266 (defvar org-file-properties
)
1267 (defun org-babel-params-from-properties (&optional lang
)
1268 "Retrieve parameters specified as properties.
1269 Return an association list of any source block params which
1270 may be specified in the properties of the current outline entry."
1273 (org-babel-parse-multiple-vars
1276 (lambda (header-arg)
1277 (and (setq val
(org-entry-get (point) header-arg t
))
1278 (cons (intern (concat ":" header-arg
))
1279 (org-babel-read val
))))
1284 (org-babel-combine-header-arg-lists
1285 org-babel-common-header-args-w-values
1287 (setq sym
(intern (concat "org-babel-header-args:" lang
)))
1288 (and (boundp sym
) (eval sym
))))))))))))
1290 (defvar org-src-preserve-indentation
)
1291 (defun org-babel-parse-src-block-match ()
1292 "Parse the results from a match of the `org-babel-src-block-regexp'."
1293 (let* ((block-indentation (length (match-string 1)))
1294 (lang (org-no-properties (match-string 2)))
1295 (lang-headers (intern (concat "org-babel-default-header-args:" lang
)))
1296 (switches (match-string 3))
1297 (body (org-no-properties
1298 (let* ((body (match-string 5))
1299 (sub-length (- (length body
) 1)))
1300 (if (and (> sub-length
0)
1301 (string= "\n" (substring body sub-length
)))
1302 (substring body
0 sub-length
)
1304 (preserve-indentation (or org-src-preserve-indentation
1306 (string-match "-i\\>" switches
)))))
1308 ;; get block body less properties, protective commas, and indentation
1311 (insert (org-unescape-code-in-string body
))
1312 (unless preserve-indentation
(org-do-remove-indentation))
1314 (org-babel-merge-params
1315 org-babel-default-header-args
1316 (org-babel-params-from-properties lang
)
1317 (if (boundp lang-headers
) (eval lang-headers
) nil
)
1318 (org-babel-parse-header-arguments
1319 (org-no-properties (or (match-string 4) ""))))
1321 block-indentation
)))
1323 (defun org-babel-parse-inline-src-block-match ()
1324 "Parse the results from a match of the `org-babel-inline-src-block-regexp'."
1325 (let* ((lang (org-no-properties (match-string 2)))
1326 (lang-headers (intern (concat "org-babel-default-header-args:" lang
))))
1328 (org-unescape-code-in-string (org-no-properties (match-string 5)))
1329 (org-babel-merge-params
1330 org-babel-default-inline-header-args
1331 (org-babel-params-from-properties lang
)
1332 (if (boundp lang-headers
) (eval lang-headers
) nil
)
1333 (org-babel-parse-header-arguments
1334 (org-no-properties (or (match-string 4) "")))))))
1336 (defun org-babel-balanced-split (string alts
)
1337 "Split STRING on instances of ALTS.
1338 ALTS is a cons of two character options where each option may be
1339 either the numeric code of a single character or a list of
1340 character alternatives. For example to split on balanced
1341 instances of \"[ \t]:\" set ALTS to '((32 9) . 58)."
1342 (let* ((matches (lambda (ch spec
) (if (listp spec
) (member ch spec
) (equal spec ch
))))
1343 (matched (lambda (ch last
)
1345 (and (funcall matches ch
(cdr alts
))
1346 (funcall matches last
(car alts
)))
1347 (funcall matches ch alts
))))
1348 (balance 0) (last 0)
1350 (mapc (lambda (ch) ; split on [], (), "" balanced instances of [ \t]:
1351 (setq balance
(+ balance
1352 (cond ((or (equal 91 ch
) (equal 40 ch
)) 1)
1353 ((or (equal 93 ch
) (equal 41 ch
)) -
1)
1355 (when (and (equal 34 ch
) (not (equal 92 last
)))
1356 (setq quote
(not quote
)))
1357 (setq partial
(cons ch partial
))
1358 (when (and (= balance
0) (not quote
) (funcall matched ch last
))
1359 (setq lst
(cons (apply #'string
(nreverse
1366 (string-to-list string
))
1367 (nreverse (cons (apply #'string
(nreverse partial
)) lst
))))
1369 (defun org-babel-join-splits-near-ch (ch list
)
1370 "Join splits where \"=\" is on either end of the split."
1371 (let ((last= (lambda (str) (= ch
(aref str
(1- (length str
))))))
1372 (first= (lambda (str) (= ch
(aref str
0)))))
1374 (org-reduce (lambda (acc el
)
1375 (let ((head (car acc
)))
1376 (if (and head
(or (funcall last
= head
) (funcall first
= el
)))
1377 (cons (concat head el
) (cdr acc
))
1379 list
:initial-value nil
))))
1381 (defun org-babel-parse-header-arguments (arg-string)
1382 "Parse a string of header arguments returning an alist."
1383 (when (> (length arg-string
) 0)
1384 (org-babel-parse-multiple-vars
1389 "\\([^ \f\t\n\r\v]+\\)[ \f\t\n\r\v]+\\([^ \f\t\n\r\v]+.*\\)"
1391 (cons (intern (match-string 1 arg
))
1392 (org-babel-read (org-babel-chomp (match-string 2 arg
))))
1393 (cons (intern (org-babel-chomp arg
)) nil
)))
1395 (cons (car raw
) (mapcar (lambda (r) (concat ":" r
)) (cdr raw
))))
1396 (org-babel-balanced-split arg-string
'((32 9) .
58))))))))
1398 (defun org-babel-parse-multiple-vars (header-arguments)
1399 "Expand multiple variable assignments behind a single :var keyword.
1401 This allows expression of multiple variables with one :var as
1404 #+PROPERTY: var foo=1, bar=2"
1406 (mapc (lambda (pair)
1407 (if (eq (car pair
) :var
)
1408 (mapcar (lambda (v) (push (cons :var
(org-babel-trim v
)) results
))
1409 (org-babel-join-splits-near-ch
1410 61 (org-babel-balanced-split (cdr pair
) 32)))
1411 (push pair results
)))
1413 (nreverse results
)))
1415 (defun org-babel-process-params (params)
1416 "Expand variables in PARAMS and add summary parameters."
1417 (let* ((processed-vars (mapcar (lambda (el)
1418 (if (consp (cdr el
))
1420 (org-babel-ref-parse (cdr el
))))
1421 (org-babel-get-header params
:var
)))
1422 (vars-and-names (if (and (assoc :colname-names params
)
1423 (assoc :rowname-names params
))
1424 (list processed-vars
)
1425 (org-babel-disassemble-tables
1427 (cdr (assoc :hlines params
))
1428 (cdr (assoc :colnames params
))
1429 (cdr (assoc :rownames params
)))))
1430 (raw-result (or (cdr (assoc :results params
)) ""))
1431 (result-params (append
1432 (split-string (if (stringp raw-result
)
1435 (cdr (assoc :result-params params
)))))
1437 (mapcar (lambda (var) (cons :var var
)) (car vars-and-names
))
1439 (cons :colname-names
(or (cdr (assoc :colname-names params
))
1440 (cadr vars-and-names
)))
1441 (cons :rowname-names
(or (cdr (assoc :rowname-names params
))
1442 (caddr vars-and-names
)))
1443 (cons :result-params result-params
)
1444 (cons :result-type
(cond ((member "output" result-params
) 'output
)
1445 ((member "value" result-params
) 'value
)
1447 (org-babel-get-header params
:var
'other
))))
1449 ;; row and column names
1450 (defun org-babel-del-hlines (table)
1451 "Remove all 'hlines from TABLE."
1452 (remove 'hline table
))
1454 (defun org-babel-get-colnames (table)
1455 "Return the column names of TABLE.
1456 Return a cons cell, the `car' of which contains the TABLE less
1457 colnames, and the `cdr' of which contains a list of the column
1459 (if (equal 'hline
(nth 1 table
))
1460 (cons (cddr table
) (car table
))
1461 (cons (cdr table
) (car table
))))
1463 (defun org-babel-get-rownames (table)
1464 "Return the row names of TABLE.
1465 Return a cons cell, the `car' of which contains the TABLE less
1466 colnames, and the `cdr' of which contains a list of the column
1467 names. Note: this function removes any hlines in TABLE."
1468 (let* ((trans (lambda (table) (apply #'mapcar
* #'list table
)))
1470 (mapcar (lambda (el) (if (listp el
) (length el
) 0)) table
)))
1471 (table (funcall trans
(mapcar (lambda (row)
1472 (if (not (equal row
'hline
))
1476 (setq row
(cons 'hline row
)))
1479 (cons (mapcar (lambda (row) (if (equal (car row
) 'hline
) 'hline row
))
1480 (funcall trans
(cdr table
)))
1481 (remove 'hline
(car table
)))))
1483 (defun org-babel-put-colnames (table colnames
)
1484 "Add COLNAMES to TABLE if they exist."
1485 (if colnames
(apply 'list colnames
'hline table
) table
))
1487 (defun org-babel-put-rownames (table rownames
)
1488 "Add ROWNAMES to TABLE if they exist."
1490 (mapcar (lambda (row)
1492 (cons (or (pop rownames
) "") row
)
1496 (defun org-babel-pick-name (names selector
)
1497 "Select one out of an alist of row or column names.
1498 SELECTOR can be either a list of names in which case those names
1499 will be returned directly, or an index into the list NAMES in
1500 which case the indexed names will be return."
1501 (if (listp selector
)
1504 (if (and selector
(symbolp selector
) (not (equal t selector
)))
1505 (cdr (assoc selector names
))
1506 (if (integerp selector
)
1507 (nth (- selector
1) names
)
1508 (cdr (car (last names
))))))))
1510 (defun org-babel-disassemble-tables (vars hlines colnames rownames
)
1511 "Parse tables for further processing.
1512 Process the variables in VARS according to the HLINES,
1513 ROWNAMES and COLNAMES header arguments. Return a list consisting
1514 of the vars, cnames and rnames."
1515 (let (cnames rnames
)
1519 (when (listp (cdr var
))
1520 (when (and (not (equal colnames
"no"))
1521 (or colnames
(and (equal (nth 1 (cdr var
)) 'hline
)
1522 (not (member 'hline
(cddr (cdr var
)))))))
1523 (let ((both (org-babel-get-colnames (cdr var
))))
1524 (setq cnames
(cons (cons (car var
) (cdr both
))
1526 (setq var
(cons (car var
) (car both
)))))
1527 (when (and rownames
(not (equal rownames
"no")))
1528 (let ((both (org-babel-get-rownames (cdr var
))))
1529 (setq rnames
(cons (cons (car var
) (cdr both
))
1531 (setq var
(cons (car var
) (car both
)))))
1532 (when (and hlines
(not (equal hlines
"yes")))
1533 (setq var
(cons (car var
) (org-babel-del-hlines (cdr var
))))))
1536 (reverse cnames
) (reverse rnames
))))
1538 (defun org-babel-reassemble-table (table colnames rownames
)
1539 "Add column and row names to a table.
1540 Given a TABLE and set of COLNAMES and ROWNAMES add the names
1541 to the table for reinsertion to org-mode."
1544 (if (and colnames
(listp (car table
)) (= (length (car table
))
1546 (org-babel-put-colnames table colnames
) table
))
1547 (if (and rownames
(= (length table
) (length rownames
)))
1548 (org-babel-put-rownames table rownames
) table
))
1551 (defun org-babel-where-is-src-block-head ()
1552 "Find where the current source block begins.
1553 Return the point at the beginning of the current source
1554 block. Specifically at the beginning of the #+BEGIN_SRC line.
1555 If the point is not on a source block then return nil."
1556 (let ((initial (point)) (case-fold-search t
) top bottom
)
1558 (save-excursion ;; on a source name line or a #+header line
1559 (beginning-of-line 1)
1560 (and (or (looking-at org-babel-src-name-regexp
)
1561 (looking-at org-babel-multi-line-header-regexp
))
1563 (while (and (forward-line 1)
1564 (or (looking-at org-babel-src-name-regexp
)
1565 (looking-at org-babel-multi-line-header-regexp
))))
1566 (looking-at org-babel-src-block-regexp
))
1568 (save-excursion ;; on a #+begin_src line
1569 (beginning-of-line 1)
1570 (and (looking-at org-babel-src-block-regexp
)
1572 (save-excursion ;; inside a src block
1574 (re-search-backward "^[ \t]*#\\+begin_src" nil t
) (setq top
(point))
1575 (re-search-forward "^[ \t]*#\\+end_src" nil t
) (setq bottom
(point))
1576 (< top initial
) (< initial bottom
)
1577 (progn (goto-char top
) (beginning-of-line 1)
1578 (looking-at org-babel-src-block-regexp
))
1582 (defun org-babel-goto-src-block-head ()
1583 "Go to the beginning of the current code block."
1586 (if head
(goto-char head
) (error "Not currently in a code block")))
1587 (org-babel-where-is-src-block-head)))
1590 (defun org-babel-goto-named-src-block (name)
1591 "Go to a named source-code block."
1593 (let ((completion-ignore-case t
)
1594 (case-fold-search t
)
1595 (under-point (thing-at-point 'line
)))
1596 (list (org-icompleting-read
1597 "source-block name: " (org-babel-src-block-names) nil t
1600 ((string-match (org-babel-noweb-wrap) under-point
)
1601 (let ((block-name (match-string 1 under-point
)))
1602 (string-match "[^(]*" block-name
)
1603 (match-string 0 block-name
)))
1605 ((string-match org-babel-lob-one-liner-regexp under-point
)
1606 (let ((source-info (car (org-babel-lob-get-info))))
1607 (if (string-match "^\\([^\\[]+?\\)\\(\\[.*\\]\\)?(" source-info
)
1608 (let ((source-name (match-string 1 source-info
)))
1611 ((string-match (concat "#\\+" org-babel-results-keyword
1612 "\\:\s+\\([^\\(]*\\)") under-point
)
1613 (match-string 1 under-point
))
1615 ((and (thing-at-point 'symbol
))
1616 (org-babel-find-named-block (thing-at-point 'symbol
))
1617 (thing-at-point 'symbol
))
1619 (let ((point (org-babel-find-named-block name
)))
1621 ;; taken from `org-open-at-point'
1622 (progn (org-mark-ring-push) (goto-char point
) (org-show-context))
1623 (message "source-code block '%s' not found in this buffer" name
))))
1625 (defun org-babel-find-named-block (name)
1626 "Find a named source-code block.
1627 Return the location of the source block identified by source
1628 NAME, or nil if no such block exists. Set match data according to
1629 org-babel-named-src-block-regexp."
1631 (let ((case-fold-search t
)
1632 (regexp (org-babel-named-src-block-regexp-for-name name
)) msg
)
1633 (goto-char (point-min))
1634 (when (or (re-search-forward regexp nil t
)
1635 (re-search-backward regexp nil t
))
1636 (match-beginning 0)))))
1638 (defun org-babel-src-block-names (&optional file
)
1639 "Returns the names of source blocks in FILE or the current buffer."
1641 (when file
(find-file file
)) (goto-char (point-min))
1642 (let ((case-fold-search t
) names
)
1643 (while (re-search-forward org-babel-src-name-w-name-regexp nil t
)
1644 (setq names
(cons (match-string 3) names
)))
1648 (defun org-babel-goto-named-result (name)
1649 "Go to a named result."
1651 (let ((completion-ignore-case t
))
1652 (list (org-icompleting-read "source-block name: "
1653 (org-babel-result-names) nil t
))))
1654 (let ((point (org-babel-find-named-result name
)))
1656 ;; taken from `org-open-at-point'
1657 (progn (goto-char point
) (org-show-context))
1658 (message "result '%s' not found in this buffer" name
))))
1660 (defun org-babel-find-named-result (name &optional point
)
1661 "Find a named result.
1662 Return the location of the result named NAME in the current
1663 buffer or nil if no such result exists."
1665 (let ((case-fold-search t
))
1666 (goto-char (or point
(point-min)))
1667 (catch 'is-a-code-block
1668 (when (re-search-forward
1669 (concat org-babel-result-regexp
1670 "[ \t]" (regexp-quote name
) "[ \t]*[\n\f\v\r]") nil t
)
1671 (when (and (string= "name" (downcase (match-string 1)))
1672 (or (beginning-of-line 1)
1673 (looking-at org-babel-src-block-regexp
)
1674 (looking-at org-babel-multi-line-header-regexp
)))
1675 (throw 'is-a-code-block
(org-babel-find-named-result name
(point))))
1676 (beginning-of-line 0) (point))))))
1678 (defun org-babel-result-names (&optional file
)
1679 "Returns the names of results in FILE or the current buffer."
1681 (when file
(find-file file
)) (goto-char (point-min))
1682 (let ((case-fold-search t
) names
)
1683 (while (re-search-forward org-babel-result-w-name-regexp nil t
)
1684 (setq names
(cons (match-string 4) names
)))
1688 (defun org-babel-next-src-block (&optional arg
)
1689 "Jump to the next source block.
1690 With optional prefix argument ARG, jump forward ARG many source blocks."
1692 (org-next-block arg nil org-babel-src-block-regexp
))
1695 (defun org-babel-previous-src-block (&optional arg
)
1696 "Jump to the previous source block.
1697 With optional prefix argument ARG, jump backward ARG many source blocks."
1699 (org-previous-block arg org-babel-src-block-regexp
))
1701 (defvar org-babel-load-languages
)
1704 (defun org-babel-mark-block ()
1705 "Mark current src block."
1711 (looking-at org-babel-src-block-regexp
))
1712 (push-mark (match-end 5) nil t
)
1713 (goto-char (match-beginning 5))))
1714 (org-babel-where-is-src-block-head)))
1716 (defun org-babel-demarcate-block (&optional arg
)
1717 "Wrap or split the code in the region or on the point.
1718 When called from inside of a code block the current block is
1719 split. When called from outside of a code block a new code block
1720 is created. In both cases if the region is demarcated and if the
1721 region is not active then the point is demarcated."
1723 (let ((info (org-babel-get-src-block-info 'light
))
1724 (headers (progn (org-babel-where-is-src-block-head)
1726 (stars (concat (make-string (or (org-current-level) 1) ?
*) " ")))
1732 (let ((lang (nth 0 info
))
1733 (indent (make-string (nth 5 info
) ?
)))
1734 (when (string-match "^[[:space:]]*$"
1735 (buffer-substring (point-at-bol)
1737 (delete-region (point-at-bol) (point-at-eol)))
1739 (if (looking-at "^") "" "\n")
1740 indent
"#+end_src\n"
1741 (if arg stars indent
) "\n"
1742 indent
"#+begin_src " lang
1743 (if (> (length headers
) 1)
1744 (concat " " headers
) headers
)
1745 (if (looking-at "[\n\r]")
1747 (concat "\n" (make-string (current-column) ?
)))))))
1748 (move-end-of-line 2))
1749 (sort (if (org-region-active-p) (list (mark) (point)) (list (point))) #'>))
1750 (let ((start (point))
1751 (lang (org-icompleting-read "Lang: "
1752 (mapcar (lambda (el) (symbol-name (car el
)))
1753 org-babel-load-languages
)))
1754 (body (delete-and-extract-region
1755 (if (org-region-active-p) (mark) (point)) (point))))
1756 (insert (concat (if (looking-at "^") "" "\n")
1757 (if arg
(concat stars
"\n") "")
1758 "#+begin_src " lang
"\n"
1760 (if (or (= (length body
) 0)
1761 (string-match "[\r\n]$" body
)) "" "\n")
1763 (goto-char start
) (move-end-of-line 1)))))
1765 (defvar org-babel-lob-one-liner-regexp
)
1766 (defun org-babel-where-is-src-block-result (&optional insert info hash indent
)
1767 "Find where the current source block results begin.
1768 Return the point at the beginning of the result of the current
1769 source block. Specifically at the beginning of the results line.
1770 If no result exists for this block then create a results line
1771 following the source block."
1773 (let* ((case-fold-search t
)
1774 (on-lob-line (save-excursion
1775 (beginning-of-line 1)
1776 (looking-at org-babel-lob-one-liner-regexp
)))
1777 (inlinep (when (org-babel-get-inline-src-block-matches)
1779 (name (if on-lob-line
1780 (mapconcat #'identity
(butlast (org-babel-lob-get-info))
1782 (nth 4 (or info
(org-babel-get-src-block-info 'light
)))))
1783 (head (unless on-lob-line
(org-babel-where-is-src-block-head)))
1785 (when head
(goto-char head
))
1786 (org-with-wide-buffer
1788 found
;; was there a result (before we potentially insert one)
1793 ;; - return t if it is found, else return nil
1794 ;; - if it does not need to be rebuilt, then don't set end
1795 ;; - if it does need to be rebuilt then do set end
1796 name
(setq beg
(org-babel-find-named-result name
))
1798 (when (and hash
(not (string= hash
(match-string 5))))
1799 (goto-char beg
) (setq end beg
) ;; beginning of result
1801 (delete-region end
(org-babel-result-end)) nil
)))
1804 ;; - return t if it is found, else return nil
1805 ;; - if it is found, and the hash doesn't match, delete and set end
1806 (or on-lob-line
(re-search-forward "^[ \t]*#\\+end_src" nil t
))
1807 (progn (end-of-line 1)
1808 (if (eobp) (insert "\n") (forward-char 1))
1812 (progn ;; unnamed results line already exists
1814 (while (re-search-forward "[^ \f\t\n\r\v]" nil t
)
1815 (beginning-of-line 1)
1817 ((looking-at (concat org-babel-result-regexp
"\n"))
1818 (throw 'non-comment t
))
1819 ((looking-at "^[ \t]*#") (end-of-line 1))
1820 (t (throw 'non-comment nil
))))))
1821 (let ((this-hash (match-string 5)))
1823 ;; must remove and rebuild if hash!=old-hash
1824 (if (and hash
(not (string= hash this-hash
)))
1828 end
(org-babel-result-end)))
1829 (setq end nil
)))))))))))
1830 (if (not (and insert end
)) found
1833 (if (looking-at "[\n\r]") (forward-char 1) (insert "\n")))
1835 (when (wholenump indent
) (make-string indent ?
))
1836 "#+" org-babel-results-keyword
1838 (if org-babel-hash-show-time
1840 "["(format-time-string "<%Y-%m-%d %H:%M:%S>")" "hash
"]")
1841 (concat "["hash
"]")))
1843 (when name
(concat " " name
)) "\n"))
1844 (unless beg
(insert "\n") (backward-char))
1845 (beginning-of-line 0)
1846 (if hash
(org-babel-hide-hash))
1849 (defvar org-block-regexp
)
1850 (defun org-babel-read-result ()
1851 "Read the result at `point' into emacs-lisp."
1852 (let ((case-fold-search t
) result-string
)
1854 ((org-at-table-p) (org-babel-read-table))
1855 ((org-at-item-p) (org-babel-read-list))
1856 ((looking-at org-bracket-link-regexp
) (org-babel-read-link))
1857 ((looking-at org-block-regexp
) (org-babel-trim (match-string 4)))
1858 ((looking-at "^[ \t]*: ")
1861 (mapconcat (lambda (line)
1862 (if (and (> (length line
) 1)
1863 (string-match "^[ \t]*: \\(.+\\)" line
))
1864 (match-string 1 line
)
1868 (point) (org-babel-result-end)) "[\r\n]+")
1870 (or (org-babel-number-p result-string
) result-string
))
1871 ((looking-at org-babel-result-regexp
)
1872 (save-excursion (forward-line 1) (org-babel-read-result))))))
1874 (defun org-babel-read-table ()
1875 "Read the table at `point' into emacs-lisp."
1876 (mapcar (lambda (row)
1877 (if (and (symbolp row
) (equal row
'hline
)) row
1878 (mapcar (lambda (el) (org-babel-read el
'inhibit-lisp-eval
)) row
)))
1879 (org-table-to-lisp)))
1881 (defun org-babel-read-list ()
1882 "Read the list at `point' into emacs-lisp."
1883 (mapcar (lambda (el) (org-babel-read el
'inhibit-lisp-eval
))
1884 (mapcar #'cadr
(cdr (org-list-parse-list)))))
1886 (defvar org-link-types-re
)
1887 (defun org-babel-read-link ()
1888 "Read the link at `point' into emacs-lisp.
1889 If the path of the link is a file path it is expanded using
1890 `expand-file-name'."
1891 (let* ((case-fold-search t
)
1892 (raw (and (looking-at org-bracket-link-regexp
)
1893 (org-no-properties (match-string 1))))
1894 (type (and (string-match org-link-types-re raw
)
1895 (match-string 1 raw
))))
1897 ((not type
) (expand-file-name raw
))
1898 ((string= type
"file")
1899 (and (string-match "file\\(.*\\):\\(.+\\)" raw
)
1900 (expand-file-name (match-string 2 raw
))))
1903 (defun org-babel-format-result (result &optional sep
)
1904 "Format RESULT for writing to file."
1905 (let ((echo-res (lambda (r) (if (stringp r
) r
(format "%S" r
)))))
1909 result
(list :sep
(or sep
"\t") :fmt echo-res
))
1911 (funcall echo-res result
))))
1913 (defun org-babel-insert-result
1914 (result &optional result-params info hash indent lang
)
1915 "Insert RESULT into the current buffer.
1916 By default RESULT is inserted after the end of the
1917 current source block. With optional argument RESULT-PARAMS
1918 controls insertion of results in the org-mode file.
1919 RESULT-PARAMS can take the following values:
1921 replace - (default option) insert results after the source block
1922 replacing any previously inserted results
1924 silent -- no results are inserted into the Org-mode buffer but
1925 the results are echoed to the minibuffer and are
1926 ingested by Emacs (a potentially time consuming
1929 file ---- the results are interpreted as a file path, and are
1930 inserted into the buffer using the Org-mode file syntax
1932 list ---- the results are interpreted as an Org-mode list.
1934 raw ----- results are added directly to the Org-mode file. This
1935 is a good option if you code block will output org-mode
1938 drawer -- results are added directly to the Org-mode file as with
1939 \"raw\", but are wrapped in a RESULTS drawer, allowing
1940 them to later be replaced or removed automatically.
1942 org ----- results are added inside of a \"#+BEGIN_SRC org\" block.
1943 They are not comma-escaped when inserted, but Org syntax
1944 here will be discarded when exporting the file.
1946 html ---- results are added inside of a #+BEGIN_HTML block. This
1947 is a good option if you code block will output html
1950 latex --- results are added inside of a #+BEGIN_LATEX block.
1951 This is a good option if you code block will output
1952 latex formatted text.
1954 code ---- the results are extracted in the syntax of the source
1955 code of the language being evaluated and are added
1956 inside of a #+BEGIN_SRC block with the source-code
1957 language set appropriately. Note this relies on the
1958 optional LANG argument."
1959 (if (stringp result
)
1961 (setq result
(org-no-properties result
))
1962 (when (member "file" result-params
)
1963 (setq result
(org-babel-result-to-file
1964 result
(when (assoc :file-desc
(nth 2 info
))
1965 (or (cdr (assoc :file-desc
(nth 2 info
)))
1967 (unless (listp result
) (setq result
(format "%S" result
))))
1968 (if (and result-params
(member "silent" result-params
))
1970 (message (replace-regexp-in-string "%" "%%" (format "%S" result
)))
1975 (when (or (org-babel-get-inline-src-block-matches)
1976 (org-babel-get-lob-one-liner-matches))
1977 (goto-char (match-end 0))
1978 (insert (if (listp result
) "\n" " "))
1980 (existing-result (unless inlinep
1981 (org-babel-where-is-src-block-result
1982 t info hash indent
)))
1984 (cdr (assoc :results_switches
(nth 2 info
))))
1985 (visible-beg (copy-marker (point-min)))
1986 (visible-end (copy-marker (point-max)))
1987 ;; When results exist outside of the current visible
1988 ;; region of the buffer, be sure to widen buffer to
1990 (outside-scope-p (and existing-result
1991 (or (> visible-beg existing-result
)
1992 (<= visible-end existing-result
))))
1994 (when (and (stringp result
) ; ensure results end in a newline
1996 (> (length result
) 0)
1997 (not (or (string-equal (substring result -
1) "\n")
1998 (string-equal (substring result -
1) "\r"))))
1999 (setq result
(concat result
"\n")))
2002 (when outside-scope-p
(widen))
2003 (if (not existing-result
)
2004 (setq beg
(or inlinep
(point)))
2005 (goto-char existing-result
)
2007 (re-search-forward "#" nil t
)
2008 (setq indent
(- (current-column) 1)))
2012 ((member "replace" result-params
)
2013 (delete-region (point) (org-babel-result-end)))
2014 ((member "append" result-params
)
2015 (goto-char (org-babel-result-end)) (setq beg
(point-marker)))
2016 ((member "prepend" result-params
)))) ; already there
2017 (setq results-switches
2018 (if results-switches
(concat " " results-switches
) ""))
2019 (let ((wrap (lambda (start finish
&optional no-escape
)
2020 (goto-char end
) (insert (concat finish
"\n"))
2021 (goto-char beg
) (insert (concat start
"\n"))
2023 (org-escape-code-in-region (min (point) end
) end
))
2024 (goto-char end
) (goto-char (point-at-eol))
2025 (setq end
(point-marker))))
2026 (proper-list-p (lambda (it) (and (listp it
) (null (cdr (last it
)))))))
2027 ;; insert results based on type
2029 ;; do nothing for an empty result
2031 ;; insert a list if preferred
2032 ((member "list" result-params
)
2035 (org-list-to-generic
2038 (lambda (el) (list nil
(if (stringp el
) el
(format "%S" el
))))
2039 (if (listp result
) result
(list result
))))
2040 '(:splicep nil
:istart
"- " :iend
"\n")))
2042 ;; assume the result is a table if it's not a string
2043 ((funcall proper-list-p result
)
2045 (insert (concat (orgtbl-to-orgtbl
2046 (if (or (eq 'hline
(car result
))
2047 (and (listp (car result
))
2048 (listp (cdr (car result
)))))
2049 result
(list result
))
2050 '(:fmt
(lambda (cell) (format "%s" cell
)))) "\n"))
2051 (goto-char beg
) (when (org-at-table-p) (org-table-align)))
2052 ((and (listp result
) (not (funcall proper-list-p result
)))
2053 (insert (format "%s\n" result
)))
2054 ((member "file" result-params
)
2055 (when inlinep
(goto-char inlinep
))
2057 (t (goto-char beg
) (insert result
)))
2058 (when (funcall proper-list-p result
) (goto-char (org-table-end)))
2059 (setq end
(point-marker))
2060 ;; possibly wrap result
2062 ((assoc :wrap
(nth 2 info
))
2063 (let ((name (or (cdr (assoc :wrap
(nth 2 info
))) "RESULTS")))
2064 (funcall wrap
(concat "#+BEGIN_" name
)
2065 (concat "#+END_" (car (org-split-string name
))))))
2066 ((member "html" result-params
)
2067 (funcall wrap
"#+BEGIN_HTML" "#+END_HTML"))
2068 ((member "latex" result-params
)
2069 (funcall wrap
"#+BEGIN_LaTeX" "#+END_LaTeX"))
2070 ((member "org" result-params
)
2071 (funcall wrap
"#+BEGIN_SRC org" "#+END_SRC"))
2072 ((member "code" result-params
)
2073 (funcall wrap
(format "#+BEGIN_SRC %s%s" (or lang
"none") results-switches
)
2075 ((member "raw" result-params
)
2076 (goto-char beg
) (if (org-at-table-p) (org-cycle)))
2077 ((or (member "drawer" result-params
)
2078 ;; Stay backward compatible with <7.9.2
2079 (member "wrap" result-params
))
2080 (funcall wrap
":RESULTS:" ":END:" 'no-escape
))
2081 ((and (not (funcall proper-list-p result
))
2082 (not (member "file" result-params
)))
2083 (org-babel-examplize-region beg end results-switches
)
2084 (setq end
(point)))))
2085 ;; possibly indent the results to match the #+results line
2086 (when (and (not inlinep
) (numberp indent
) indent
(> indent
0)
2087 ;; in this case `table-align' does the work for us
2088 (not (and (listp result
)
2089 (member "append" result-params
))))
2090 (indent-rigidly beg end indent
))
2092 (if (member "value" result-params
)
2093 (message "Code block returned no value.")
2094 (message "Code block produced no output."))
2095 (message "Code block evaluation complete.")))
2096 (when outside-scope-p
(narrow-to-region visible-beg visible-end
))
2097 (set-marker visible-beg nil
)
2098 (set-marker visible-end nil
))))))
2100 (defun org-babel-remove-result (&optional info
)
2101 "Remove the result of the current source block."
2103 (let ((location (org-babel-where-is-src-block-result nil info
)) start
)
2105 (setq start
(- location
1))
2107 (goto-char location
) (forward-line 1)
2108 (delete-region start
(org-babel-result-end))))))
2110 (defun org-babel-result-end ()
2111 "Return the point at the end of the current set of results."
2114 ((org-at-table-p) (progn (goto-char (org-table-end)) (point)))
2115 ((org-at-item-p) (let* ((struct (org-list-struct))
2116 (prvs (org-list-prevs-alist struct
)))
2117 (org-list-get-list-end (point-at-bol) struct prvs
)))
2118 ((let ((case-fold-search t
)) (looking-at "^\\([ \t]*\\):results:"))
2119 (progn (re-search-forward (concat "^" (match-string 1) ":END:"))
2120 (forward-char 1) (point)))
2122 (let ((case-fold-search t
))
2123 (if (looking-at (concat "[ \t]*#\\+begin_\\([^ \t\n\r]+\\)"))
2124 (progn (re-search-forward (concat "[ \t]*#\\+end_" (match-string 1))
2127 (while (looking-at "[ \t]*\\(: \\|\\[\\[\\)")
2131 (defun org-babel-result-to-file (result &optional description
)
2132 "Convert RESULT into an `org-mode' link with optional DESCRIPTION.
2133 If the `default-directory' is different from the containing
2134 file's directory then expand relative links."
2135 (when (stringp result
)
2136 (format "[[file:%s]%s]"
2137 (if (and default-directory
2139 (not (string= (expand-file-name default-directory
)
2141 (file-name-directory buffer-file-name
)))))
2142 (expand-file-name result default-directory
)
2144 (if description
(concat "[" description
"]") ""))))
2146 (defvar org-babel-capitalize-examplize-region-markers nil
2147 "Make true to capitalize begin/end example markers inserted by code blocks.")
2149 (defun org-babel-examplize-region (beg end
&optional results-switches
)
2150 "Comment out region using the inline '==' or ': ' org example quote."
2152 (let ((chars-between (lambda (b e
)
2153 (not (string-match "^[\\s]*$" (buffer-substring b e
)))))
2154 (maybe-cap (lambda (str) (if org-babel-capitalize-examplize-region-markers
2155 (upcase str
) str
))))
2156 (if (or (funcall chars-between
(save-excursion (goto-char beg
) (point-at-bol)) beg
)
2157 (funcall chars-between end
(save-excursion (goto-char end
) (point-at-eol))))
2160 (insert (format "=%s=" (prog1 (buffer-substring beg end
)
2161 (delete-region beg end
)))))
2162 (let ((size (count-lines beg end
)))
2164 (cond ((= size
0)) ; do nothing for an empty result
2165 ((< size org-babel-min-lines-for-block-output
)
2168 (beginning-of-line 1) (insert ": ") (forward-line 1)))
2171 (insert (if results-switches
2173 (funcall maybe-cap
"#+begin_example")
2175 (funcall maybe-cap
"#+begin_example\n")))
2176 (if (markerp end
) (goto-char end
) (forward-char (- end beg
)))
2177 (insert (funcall maybe-cap
"#+end_example\n")))))))))
2179 (defun org-babel-update-block-body (new-body)
2180 "Update the body of the current code block to NEW-BODY."
2181 (if (not (org-babel-where-is-src-block-head))
2182 (error "Not in a source block")
2184 (replace-match (concat (org-babel-trim new-body
) "\n") nil t nil
5))
2185 (indent-rigidly (match-beginning 5) (match-end 5) 2)))
2187 (defun org-babel-merge-params (&rest plists
)
2188 "Combine all parameter association lists in PLISTS.
2189 Later elements of PLISTS override the values of previous elements.
2190 This takes into account some special considerations for certain
2191 parameters when merging lists."
2192 (let* ((results-exclusive-groups
2193 (mapcar (lambda (group) (mapcar #'symbol-name group
))
2194 (cdr (assoc 'results org-babel-common-header-args-w-values
))))
2195 (exports-exclusive-groups
2196 (mapcar (lambda (group) (mapcar #'symbol-name group
))
2197 (cdr (assoc 'exports org-babel-common-header-args-w-values
))))
2199 (e-merge (lambda (exclusive-groups &rest result-params
)
2200 ;; maintain exclusivity of mutually exclusive parameters
2202 (mapc (lambda (new-params)
2203 (mapc (lambda (new-param)
2204 (mapc (lambda (exclusive-group)
2205 (when (member new-param exclusive-group
)
2206 (mapcar (lambda (excluded-param)
2213 (setq output
(org-uniquify
2214 (cons new-param output
))))
2218 params results exports tangle noweb cache vars shebang comments padline
)
2226 (let ((name (if (listp (cdr pair
))
2228 (and (string-match "^\\([^= \f\t\n\r\v]+\\)[ \t]*="
2230 (intern (match-string 1 (cdr pair
)))))))
2234 (if (member name
(mapcar #'car vars
))
2238 (unless (equal (car p
) name
) p
))
2241 (list (cons name pair
))))
2242 ;; if no name is given and we already have named variables
2243 ;; then assign to named variables in order
2244 (if (and vars
(nth variable-index vars
))
2245 (prog1 (setf (cddr (nth variable-index vars
))
2246 (concat (symbol-name
2247 (car (nth variable-index vars
)))
2249 (incf variable-index
))
2250 (error "Variable \"%s\" must be assigned a default value"
2253 (setq results
(funcall e-merge results-exclusive-groups
2256 (let ((r (cdr pair
)))
2257 (if (stringp r
) r
(eval r
)))))))
2260 (setq results
(funcall e-merge results-exclusive-groups
2262 (unless (or (member "both" exports
)
2263 (member "none" exports
)
2264 (member "code" exports
))
2265 (setq exports
(funcall e-merge exports-exclusive-groups
2266 exports
'("results"))))
2267 (setq params
(cons pair
(assq-delete-all (car pair
) params
)))))
2269 (setq exports
(funcall e-merge exports-exclusive-groups
2270 exports
(split-string (cdr pair
)))))
2271 (:tangle
;; take the latest -- always overwrite
2272 (setq tangle
(or (list (cdr pair
)) tangle
)))
2274 (setq noweb
(funcall e-merge
2275 '(("yes" "no" "tangle" "no-export"
2276 "strip-export" "eval"))
2278 (split-string (or (cdr pair
) "")))))
2280 (setq cache
(funcall e-merge
'(("yes" "no")) cache
2281 (split-string (or (cdr pair
) "")))))
2283 (setq padline
(funcall e-merge
'(("yes" "no")) padline
2284 (split-string (or (cdr pair
) "")))))
2285 (:shebang
;; take the latest -- always overwrite
2286 (setq shebang
(or (list (cdr pair
)) shebang
)))
2288 (setq comments
(funcall e-merge
'(("yes" "no")) comments
2289 (split-string (or (cdr pair
) "")))))
2290 (t ;; replace: this covers e.g. :session
2291 (setq params
(cons pair
(assq-delete-all (car pair
) params
))))))
2294 (setq vars
(reverse vars
))
2295 (while vars
(setq params
(cons (cons :var
(cddr (pop vars
))) params
)))
2298 (let ((key (intern (concat ":" (symbol-name hd
))))
2300 (setf params
(cons (cons key
(mapconcat 'identity val
" ")) params
))))
2301 '(results exports tangle noweb padline cache shebang comments
))
2304 (defvar org-babel-use-quick-and-dirty-noweb-expansion nil
2305 "Set to true to use regular expressions to expand noweb references.
2306 This results in much faster noweb reference expansion but does
2307 not properly allow code blocks to inherit the \":noweb-ref\"
2308 header argument from buffer or subtree wide properties.")
2310 (defun org-babel-noweb-p (params context
)
2311 "Check if PARAMS require expansion in CONTEXT.
2312 CONTEXT may be one of :tangle, :export or :eval."
2314 (intersect (lambda (as bs
)
2316 (if (member (car as
) bs
)
2318 (funcall intersect
(cdr as
) bs
))))))
2319 (funcall intersect
(case context
2320 (:tangle
'("yes" "tangle" "no-export" "strip-export"))
2321 (:eval
'("yes" "no-export" "strip-export" "eval"))
2323 (split-string (or (cdr (assoc :noweb params
)) "")))))
2325 (defun org-babel-expand-noweb-references (&optional info parent-buffer
)
2326 "Expand Noweb references in the body of the current source code block.
2328 For example the following reference would be replaced with the
2329 body of the source-code block named 'example-block'.
2333 Note that any text preceding the <<foo>> construct on a line will
2334 be interposed between the lines of the replacement text. So for
2335 example if <<foo>> is placed behind a comment, then the entire
2336 replacement text will also be commented.
2338 This function must be called from inside of the buffer containing
2339 the source-code block which holds BODY.
2341 In addition the following syntax can be used to insert the
2342 results of evaluating the source-code block named 'example-block'.
2346 Any optional arguments can be passed to example-block by placing
2347 the arguments inside the parenthesis following the convention
2348 defined by `org-babel-lob'. For example
2350 <<example-block(a=9)>>
2352 would set the value of argument \"a\" equal to \"9\". Note that
2353 these arguments are not evaluated in the current source-code
2354 block but are passed literally to the \"example-block\"."
2355 (let* ((parent-buffer (or parent-buffer
(current-buffer)))
2356 (info (or info
(org-babel-get-src-block-info)))
2359 (ob-nww-start org-babel-noweb-wrap-start
)
2360 (ob-nww-end org-babel-noweb-wrap-end
)
2361 (comment (string= "noweb" (cdr (assoc :comments
(nth 2 info
)))))
2362 (rx-prefix (concat "\\(" org-babel-src-name-regexp
"\\|"
2363 ":noweb-ref[ \t]+" "\\)"))
2365 (nb-add (lambda (text) (setq new-body
(concat new-body text
))))
2366 (c-wrap (lambda (text)
2368 (funcall (intern (concat lang
"-mode")))
2369 (comment-region (point) (progn (insert text
) (point)))
2370 (org-babel-trim (buffer-string)))))
2371 index source-name evaluate prefix blocks-in-buffer
)
2373 (org-set-local 'org-babel-noweb-wrap-start ob-nww-start
)
2374 (org-set-local 'org-babel-noweb-wrap-end ob-nww-end
)
2375 (insert body
) (goto-char (point-min))
2376 (setq index
(point))
2377 (while (and (re-search-forward (org-babel-noweb-wrap) nil t
))
2378 (save-match-data (setf source-name
(match-string 1)))
2379 (save-match-data (setq evaluate
(string-match "\(.*\)" source-name
)))
2382 (buffer-substring (match-beginning 0)
2384 (beginning-of-line 1) (point)))))
2385 ;; add interval to new-body (removing noweb reference)
2386 (goto-char (match-beginning 0))
2387 (funcall nb-add
(buffer-substring index
(point)))
2388 (goto-char (match-end 0))
2389 (setq index
(point))
2391 (with-current-buffer parent-buffer
2394 (mapconcat ;; interpose PREFIX between every line
2398 (let ((raw (org-babel-ref-resolve source-name
)))
2399 (if (stringp raw
) raw
(format "%S" raw
)))
2401 ;; retrieve from the library of babel
2402 (nth 2 (assoc (intern source-name
)
2403 org-babel-library-of-babel
))
2404 ;; return the contents of headlines literally
2406 (when (org-babel-ref-goto-headline-id source-name
)
2407 (org-babel-ref-headline-body)))
2408 ;; find the expansion of reference in this buffer
2409 (let ((rx (concat rx-prefix source-name
"[ \t\n]"))
2412 (goto-char (point-min))
2413 (if org-babel-use-quick-and-dirty-noweb-expansion
2414 (while (re-search-forward rx nil t
)
2415 (let* ((i (org-babel-get-src-block-info 'light
))
2416 (body (org-babel-expand-noweb-references i
))
2417 (sep (or (cdr (assoc :noweb-sep
(nth 2 i
)))
2421 (concat (funcall c-wrap
(car cs
)) "\n"
2423 (funcall c-wrap
(cadr cs
))))
2424 (org-babel-tangle-comment-links i
))
2426 (setq expansion
(cons sep
(cons full expansion
)))))
2427 (org-babel-map-src-blocks nil
2428 (let ((i (org-babel-get-src-block-info 'light
)))
2429 (when (equal (or (cdr (assoc :noweb-ref
(nth 2 i
)))
2432 (let* ((body (org-babel-expand-noweb-references i
))
2433 (sep (or (cdr (assoc :noweb-sep
(nth 2 i
)))
2437 (concat (funcall c-wrap
(car cs
)) "\n"
2439 (funcall c-wrap
(cadr cs
))))
2440 (org-babel-tangle-comment-links i
))
2443 (cons sep
(cons full expansion
)))))))))
2445 (mapconcat #'identity
(nreverse (cdr expansion
)) "")))
2446 ;; possibly raise an error if named block doesn't exist
2447 (if (member lang org-babel-noweb-error-langs
)
2449 (org-babel-noweb-wrap source-name
)
2450 "could not be resolved (see "
2451 "`org-babel-noweb-error-langs')"))
2453 "[\n\r]") (concat "\n" prefix
))))))
2454 (funcall nb-add
(buffer-substring index
(point-max))))
2457 (defun org-babel-script-escape (str &optional force
)
2458 "Safely convert tables into elisp lists."
2459 (let (in-single in-double out
)
2460 ((lambda (escaped) (condition-case nil
(org-babel-read escaped
) (error escaped
)))
2464 (or (and (string-equal "[" (substring str
0 1))
2465 (string-equal "]" (substring str -
1)))
2466 (and (string-equal "{" (substring str
0 1))
2467 (string-equal "}" (substring str -
1)))
2468 (and (string-equal "(" (substring str
0 1))
2469 (string-equal ")" (substring str -
1))))))
2479 (91 (if (or in-double in-single
) ; [
2482 (93 (if (or in-double in-single
) ; ]
2485 (123 (if (or in-double in-single
) ; {
2488 (125 (if (or in-double in-single
) ; }
2491 (44 (if (or in-double in-single
) ; ,
2492 (cons 44 out
) (cons 32 out
)))
2493 (39 (if in-double
; '
2495 (setq in-single
(not in-single
)) (cons 34 out
)))
2496 (34 (if in-single
; "
2497 (append (list 34 32) out
)
2498 (setq in-double
(not in-double
)) (cons 34 out
)))
2499 (t (cons ch out
)))))
2500 (string-to-list str
))
2501 (apply #'string
(reverse out
)))))
2504 (defun org-babel-read (cell &optional inhibit-lisp-eval
)
2505 "Convert the string value of CELL to a number if appropriate.
2506 Otherwise if cell looks like lisp (meaning it starts with a
2507 \"(\", \"'\", \"`\" or a \"[\") then read it as lisp, otherwise
2508 return it unmodified as a string. Optional argument NO-LISP-EVAL
2509 inhibits lisp evaluation for situations in which is it not
2511 (if (and (stringp cell
) (not (equal cell
"")))
2512 (or (org-babel-number-p cell
)
2513 (if (and (not inhibit-lisp-eval
)
2514 (member (substring cell
0 1) '("(" "'" "`" "[")))
2516 (if (string= (substring cell
0 1) "\"")
2518 (progn (set-text-properties 0 (length cell
) nil cell
) cell
))))
2521 (defun org-babel-number-p (string)
2522 "If STRING represents a number return its value."
2523 (if (and (string-match "^-?[0-9]*\\.?[0-9]*$" string
)
2524 (= (length (substring string
(match-beginning 0)
2527 (string-to-number string
)))
2529 (defun org-babel-import-elisp-from-file (file-name &optional separator
)
2530 "Read the results located at FILE-NAME into an elisp table.
2531 If the table is trivial, then return it as a scalar."
2533 (save-window-excursion
2537 (org-table-import file-name separator
)
2538 (delete-file file-name
)
2539 (setq result
(mapcar (lambda (row)
2540 (mapcar #'org-babel-string-read row
))
2541 (org-table-to-lisp))))
2542 (error (message "Error reading results: %s" err
) nil
)))
2543 (if (null (cdr result
)) ;; if result is trivial vector, then scalarize it
2544 (if (consp (car result
))
2545 (if (null (cdr (car result
)))
2551 (defun org-babel-string-read (cell)
2552 "Strip nested \"s from around strings."
2553 (org-babel-read (or (and (stringp cell
)
2554 (string-match "\\\"\\(.+\\)\\\"" cell
)
2555 (match-string 1 cell
))
2558 (defun org-babel-chomp (string &optional regexp
)
2559 "Strip trailing spaces and carriage returns from STRING.
2560 Default regexp used is \"[ \f\t\n\r\v]\" but can be
2561 overwritten by specifying a regexp as a second argument."
2562 (let ((regexp (or regexp
"[ \f\t\n\r\v]")))
2563 (while (and (> (length string
) 0)
2564 (string-match regexp
(substring string -
1)))
2565 (setq string
(substring string
0 -
1)))
2568 (defun org-babel-trim (string &optional regexp
)
2569 "Strip leading and trailing spaces and carriage returns from STRING.
2570 Like `org-babel-chomp' only it runs on both the front and back
2572 (org-babel-chomp (org-reverse-string
2573 (org-babel-chomp (org-reverse-string string
) regexp
))
2576 (defun org-babel-tramp-handle-call-process-region
2577 (start end program
&optional delete buffer display
&rest args
)
2578 "Use Tramp to handle `call-process-region'.
2579 Fixes a bug in `tramp-handle-call-process-region'."
2580 (if (and (featurep 'tramp
) (file-remote-p default-directory
))
2581 (let ((tmpfile (tramp-compat-make-temp-file "")))
2582 (write-region start end tmpfile
)
2583 (when delete
(delete-region start end
))
2585 ;; (apply 'call-process program tmpfile buffer display args)
2587 (apply 'process-file program tmpfile buffer display args
)
2588 (delete-file tmpfile
)))
2589 ;; org-babel-call-process-region-original is the original emacs
2590 ;; definition. It is in scope from the let binding in
2591 ;; org-babel-execute-src-block
2592 (apply org-babel-call-process-region-original
2593 start end program delete buffer display args
)))
2595 (defun org-babel-local-file-name (file)
2596 "Return the local name component of FILE."
2597 (if (file-remote-p file
)
2599 (with-parsed-tramp-file-name file nil
2603 (defun org-babel-process-file-name (name &optional no-quote-p
)
2604 "Prepare NAME to be used in an external process.
2605 If NAME specifies a remote location, the remote portion of the
2606 name is removed, since in that case the process will be executing
2607 remotely. The file name is then processed by `expand-file-name'.
2608 Unless second argument NO-QUOTE-P is non-nil, the file name is
2609 additionally processed by `shell-quote-argument'"
2610 ((lambda (f) (if no-quote-p f
(shell-quote-argument f
)))
2611 (expand-file-name (org-babel-local-file-name name
))))
2613 (defvar org-babel-temporary-directory
)
2614 (unless (or noninteractive
(boundp 'org-babel-temporary-directory
))
2615 (defvar org-babel-temporary-directory
2616 (or (and (boundp 'org-babel-temporary-directory
)
2617 (file-exists-p org-babel-temporary-directory
)
2618 org-babel-temporary-directory
)
2619 (make-temp-file "babel-" t
))
2620 "Directory to hold temporary files created to execute code blocks.
2621 Used by `org-babel-temp-file'. This directory will be removed on
2624 (defmacro org-babel-result-cond
(result-params scalar-form
&rest table-forms
)
2625 "Call the code to parse raw string results according to RESULT-PARAMS."
2627 (debug (form form
&rest form
)))
2628 `(unless (member "none" ,result-params
)
2629 (if (or (member "scalar" ,result-params
)
2630 (member "verbatim" ,result-params
)
2631 (member "html" ,result-params
)
2632 (member "code" ,result-params
)
2633 (member "pp" ,result-params
)
2634 (and (member "output" ,result-params
)
2635 (not (member "table" ,result-params
))))
2639 (defun org-babel-temp-file (prefix &optional suffix
)
2640 "Create a temporary file in the `org-babel-temporary-directory'.
2641 Passes PREFIX and SUFFIX directly to `make-temp-file' with the
2642 value of `temporary-file-directory' temporarily set to the value
2643 of `org-babel-temporary-directory'."
2644 (let ((temporary-file-directory
2645 (if (file-remote-p default-directory
)
2646 (concat (file-remote-p default-directory
) "/tmp")
2647 (or (and (boundp 'org-babel-temporary-directory
)
2648 (file-exists-p org-babel-temporary-directory
)
2649 org-babel-temporary-directory
)
2650 temporary-file-directory
))))
2651 (make-temp-file prefix nil suffix
)))
2653 (defun org-babel-remove-temporary-directory ()
2654 "Remove `org-babel-temporary-directory' on Emacs shutdown."
2655 (when (and (boundp 'org-babel-temporary-directory
)
2656 (file-exists-p org-babel-temporary-directory
))
2657 ;; taken from `delete-directory' in files.el
2660 (mapc (lambda (file)
2661 ;; This test is equivalent to
2662 ;; (and (file-directory-p fn) (not (file-symlink-p fn)))
2663 ;; but more efficient
2664 (if (eq t
(car (file-attributes file
)))
2665 (delete-directory file
)
2666 (delete-file file
)))
2667 ;; We do not want to delete "." and "..".
2668 (directory-files org-babel-temporary-directory
'full
2669 "^\\([^.]\\|\\.\\([^.]\\|\\..\\)\\).*"))
2670 (delete-directory org-babel-temporary-directory
))
2672 (message "Failed to remove temporary Org-babel directory %s"
2673 (if (boundp 'org-babel-temporary-directory
)
2674 org-babel-temporary-directory
2675 "[directory not defined]"))))))
2677 (add-hook 'kill-emacs-hook
'org-babel-remove-temporary-directory
)
2682 ;; generated-autoload-file: "org-loaddefs.el"
2685 ;;; ob-core.el ends here