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 (defvar org-babel-call-process-region-original
)
37 (defvar org-src-lang-modes
)
38 (defvar org-babel-library-of-babel
)
39 (declare-function show-all
"outline" ())
40 (declare-function org-reduce
"org" (CL-FUNC CL-SEQ
&rest CL-KEYS
))
41 (declare-function org-mark-ring-push
"org" (&optional pos buffer
))
42 (declare-function tramp-compat-make-temp-file
"tramp-compat"
43 (filename &optional dir-flag
))
44 (declare-function tramp-dissect-file-name
"tramp" (name &optional nodefault
))
45 (declare-function tramp-file-name-user
"tramp" (vec))
46 (declare-function tramp-file-name-host
"tramp" (vec))
47 (declare-function with-parsed-tramp-file-name
"tramp" (filename var
&rest body
))
48 (declare-function org-icompleting-read
"org" (&rest args
))
49 (declare-function org-edit-src-code
"org-src"
50 (&optional context code edit-buffer-name quietp
))
51 (declare-function org-edit-src-exit
"org-src" (&optional context
))
52 (declare-function org-open-at-point
"org" (&optional in-emacs reference-buffer
))
53 (declare-function org-save-outline-visibility
"org-macs" (use-markers &rest body
))
54 (declare-function org-outline-overlay-data
"org" (&optional use-markers
))
55 (declare-function org-set-outline-overlay-data
"org" (data))
56 (declare-function org-narrow-to-subtree
"org" ())
57 (declare-function org-split-string
"org" (string &optional separators
))
58 (declare-function org-entry-get
"org"
59 (pom property
&optional inherit literal-nil
))
60 (declare-function org-make-options-regexp
"org" (kwds &optional extra
))
61 (declare-function org-do-remove-indentation
"org" (&optional n
))
62 (declare-function org-next-block
"org" (arg &optional backward block-regexp
))
63 (declare-function org-previous-block
"org" (arg &optional block-regexp
))
64 (declare-function org-show-context
"org" (&optional key
))
65 (declare-function org-at-table-p
"org" (&optional table-type
))
66 (declare-function org-cycle
"org" (&optional arg
))
67 (declare-function org-uniquify
"org" (list))
68 (declare-function org-current-level
"org" ())
69 (declare-function org-table-import
"org-table" (file arg
))
70 (declare-function org-add-hook
"org-compat"
71 (hook function
&optional append local
))
72 (declare-function org-table-align
"org-table" ())
73 (declare-function org-table-end
"org-table" (&optional table-type
))
74 (declare-function orgtbl-to-generic
"org-table" (table params
))
75 (declare-function orgtbl-to-orgtbl
"org-table" (table params
))
76 (declare-function org-babel-tangle-comment-links
"ob-tangle" (&optional info
))
77 (declare-function org-babel-lob-get-info
"ob-lob" nil
)
78 (declare-function org-babel-ref-split-args
"ob-ref" (arg-string))
79 (declare-function org-babel-ref-parse
"ob-ref" (assignment))
80 (declare-function org-babel-ref-resolve
"ob-ref" (ref))
81 (declare-function org-babel-ref-goto-headline-id
"ob-ref" (id))
82 (declare-function org-babel-ref-headline-body
"ob-ref" ())
83 (declare-function org-babel-lob-execute-maybe
"ob-lob" ())
84 (declare-function org-number-sequence
"org-compat" (from &optional to inc
))
85 (declare-function org-at-item-p
"org-list" ())
86 (declare-function org-list-parse-list
"org-list" (&optional delete
))
87 (declare-function org-list-to-generic
"org-list" (LIST PARAMS
))
88 (declare-function org-list-struct
"org-list" ())
89 (declare-function org-list-prevs-alist
"org-list" (struct))
90 (declare-function org-list-get-list-end
"org-list" (item struct prevs
))
91 (declare-function org-remove-if
"org" (predicate seq
))
92 (declare-function org-completing-read
"org" (&rest args
))
93 (declare-function org-escape-code-in-region
"org-src" (beg end
))
94 (declare-function org-unescape-code-in-string
"org-src" (s))
95 (declare-function org-table-to-lisp
"org-table" (&optional txt
))
96 (declare-function org-reverse-string
"org" (string))
98 (defgroup org-babel nil
99 "Code block evaluation and management in `org-mode' documents."
103 (defcustom org-confirm-babel-evaluate t
104 "Confirm before evaluation.
105 Require confirmation before interactively evaluating code
106 blocks in Org-mode buffers. The default value of this variable
107 is t, meaning confirmation is required for any code block
108 evaluation. This variable can be set to nil to inhibit any
109 future confirmation requests. This variable can also be set to a
110 function which takes two arguments the language of the code block
111 and the body of the code block. Such a function should then
112 return a non-nil value if the user should be prompted for
113 execution or nil if no prompt is required.
115 Warning: Disabling confirmation may result in accidental
116 evaluation of potentially harmful code. It may be advisable
117 remove code block execution from C-c C-c as further protection
118 against accidental code block evaluation. The
119 `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can be used to
120 remove code block execution from the C-c C-c keybinding."
123 :type
'(choice boolean function
))
124 ;; don't allow this variable to be changed through file settings
125 (put 'org-confirm-babel-evaluate
'safe-local-variable
(lambda (x) (eq x t
)))
127 (defcustom org-babel-no-eval-on-ctrl-c-ctrl-c nil
128 "Remove code block evaluation from the C-c C-c key binding."
133 (defcustom org-babel-results-keyword
"RESULTS"
134 "Keyword used to name results generated by code blocks.
135 Should be either RESULTS or NAME however any capitalization may
140 (defcustom org-babel-noweb-wrap-start
"<<"
141 "String used to begin a noweb reference in a code block.
142 See also `org-babel-noweb-wrap-end'."
146 (defcustom org-babel-noweb-wrap-end
">>"
147 "String used to end a noweb reference in a code block.
148 See also `org-babel-noweb-wrap-start'."
152 (defun org-babel-noweb-wrap (&optional regexp
)
153 (concat org-babel-noweb-wrap-start
154 (or regexp
"\\([^ \t\n].+?[^ \t]\\|[^ \t\n]\\)")
155 org-babel-noweb-wrap-end
))
157 (defvar org-babel-src-name-regexp
158 "^[ \t]*#\\+name:[ \t]*"
159 "Regular expression used to match a source name line.")
161 (defvar org-babel-multi-line-header-regexp
162 "^[ \t]*#\\+headers?:[ \t]*\\([^\n]*\\)$"
163 "Regular expression used to match multi-line header arguments.")
165 (defvar org-babel-src-name-w-name-regexp
166 (concat org-babel-src-name-regexp
168 org-babel-multi-line-header-regexp
170 "\\([^ ()\f\t\n\r\v]+\\)\\(\(\\(.*\\)\)\\|\\)")
171 "Regular expression matching source name lines with a name.")
173 (defvar org-babel-src-block-regexp
175 ;; (1) indentation (2) lang
176 "^\\([ \t]*\\)#\\+begin_src[ \t]+\\([^ \f\t\n\r\v]+\\)[ \t]*"
178 "\\([^\":\n]*\"[^\"\n*]*\"[^\":\n]*\\|[^\":\n]*\\)"
179 ;; (4) header arguments
182 "\\([^\000]*?\n\\)?[ \t]*#\\+end_src")
183 "Regexp used to identify code blocks.")
185 (defvar org-babel-inline-src-block-regexp
187 ;; (1) replacement target (2) lang
188 "\\(?:^\\|[^-[:alnum:]]\\)\\(src_\\([^ \f\t\n\r\v]+\\)"
189 ;; (3,4) (unused, headers)
190 "\\(\\|\\[\\(.*?\\)\\]\\)"
192 "{\\([^\f\n\r\v]+?\\)}\\)")
193 "Regexp used to identify inline src-blocks.")
195 (defun org-babel-get-header (params key
&optional others
)
196 "Select only header argument of type KEY from a list.
197 Optional argument OTHERS indicates that only the header that do
198 not match KEY should be returned."
201 (lambda (p) (when (funcall (if others
#'not
#'identity
) (eq (car p
) key
)) p
))
204 (defun org-babel-get-inline-src-block-matches()
205 "Set match data if within body of an inline source block.
206 Returns non-nil if match-data set"
207 (let ((src-at-0-p (save-excursion
208 (beginning-of-line 1)
209 (string= "src" (thing-at-point 'word
))))
210 (first-line-p (= 1 (line-number-at-pos)))
212 (let ((search-for (cond ((and src-at-0-p first-line-p
"src_"))
213 (first-line-p "[[:punct:] \t]src_")
214 (t "[[:punct:] \f\t\n\r\v]src_")))
215 (lower-limit (if first-line-p
217 (- (point-at-bol) 1))))
219 (when (or (and src-at-0-p
(bobp))
220 (and (re-search-forward "}" (point-at-eol) t
)
221 (re-search-backward search-for lower-limit t
)
223 (when (looking-at org-babel-inline-src-block-regexp
)
226 (defvar org-babel-inline-lob-one-liner-regexp
)
227 (defun org-babel-get-lob-one-liner-matches()
228 "Set match data if on line of an lob one liner.
229 Returns non-nil if match-data set"
231 (unless (= (point) (point-at-bol)) ;; move before inline block
232 (re-search-backward "[ \f\t\n\r\v]" nil t
))
233 (if (looking-at org-babel-inline-lob-one-liner-regexp
)
237 (defun org-babel-get-src-block-info (&optional light
)
238 "Get information on the current source block.
240 Optional argument LIGHT does not resolve remote variable
241 references; a process which could likely result in the execution
242 of other code blocks.
245 (language body header-arguments-alist switches name indent)."
246 (let ((case-fold-search t
) head info name indent
)
248 (if (setq head
(org-babel-where-is-src-block-head))
251 (setq info
(org-babel-parse-src-block-match))
252 (setq indent
(car (last info
)))
253 (setq info
(butlast info
))
254 (while (and (forward-line -
1)
255 (looking-at org-babel-multi-line-header-regexp
))
257 (org-babel-merge-params
259 (org-babel-parse-header-arguments (match-string 1)))))
260 (when (looking-at org-babel-src-name-w-name-regexp
)
261 (setq name
(org-no-properties (match-string 3)))
262 (when (and (match-string 5) (> (length (match-string 5)) 0))
263 (setf (nth 2 info
) ;; merge functional-syntax vars and header-args
264 (org-babel-merge-params
266 (lambda (ref) (cons :var ref
))
268 (lambda (var) ;; check that each variable is initialized
269 (if (string-match ".+=.+" var
)
272 "variable \"%s\"%s must be assigned a default value"
273 var
(if name
(format " in block \"%s\"" name
) ""))))
274 (org-babel-ref-split-args (match-string 5))))
276 ;; inline source block
277 (when (org-babel-get-inline-src-block-matches)
278 (setq info
(org-babel-parse-inline-src-block-match))))
279 ;; resolve variable references and add summary parameters
280 (when (and info
(not light
))
281 (setf (nth 2 info
) (org-babel-process-params (nth 2 info
))))
282 (when info
(append info
(list name indent
)))))
284 (defvar org-current-export-file
) ; dynamically bound
285 (defun org-babel-confirm-evaluate (info)
286 "Confirm evaluation of the code block INFO.
287 This behavior can be suppressed by setting the value of
288 `org-confirm-babel-evaluate' to nil, in which case all future
289 interactive code block evaluations will proceed without any
290 confirmation from the user.
292 Note disabling confirmation may result in accidental evaluation
293 of potentially harmful code."
294 (let* ((eval (or (cdr (assoc :eval
(nth 2 info
)))
295 (when (assoc :noeval
(nth 2 info
)) "no")))
296 (query (cond ((equal eval
"query") t
)
297 ((and (boundp 'org-current-export-file
)
298 org-current-export-file
299 (equal eval
"query-export")) t
)
300 ((functionp org-confirm-babel-evaluate
)
301 (funcall org-confirm-babel-evaluate
302 (nth 0 info
) (nth 1 info
)))
303 (t org-confirm-babel-evaluate
))))
304 (if (or (equal eval
"never") (equal eval
"no")
305 (and (boundp 'org-current-export-file
)
306 org-current-export-file
307 (or (equal eval
"no-export")
308 (equal eval
"never-export")))
311 (format "Evaluate this%scode block%son your system? "
312 (if info
(format " %s " (nth 0 info
)) " ")
314 (format " (%s) " (nth 4 info
)) " "))))))
315 (prog1 nil
(message "Evaluation %s"
316 (if (or (equal eval
"never") (equal eval
"no")
317 (equal eval
"no-export")
318 (equal eval
"never-export"))
319 "Disabled" "Aborted")))
323 (defun org-babel-execute-safely-maybe ()
324 (unless org-babel-no-eval-on-ctrl-c-ctrl-c
325 (org-babel-execute-maybe)))
327 (add-hook 'org-ctrl-c-ctrl-c-hook
'org-babel-execute-safely-maybe
)
330 (defun org-babel-execute-maybe ()
332 (or (org-babel-execute-src-block-maybe)
333 (org-babel-lob-execute-maybe)))
335 (defun org-babel-execute-src-block-maybe ()
336 "Conditionally execute a source block.
337 Detect if this is context for a Babel src-block and if so
338 then run `org-babel-execute-src-block'."
340 (let ((info (org-babel-get-src-block-info)))
342 (progn (org-babel-eval-wipe-error-buffer)
343 (org-babel-execute-src-block current-prefix-arg info
) t
) nil
)))
346 (defun org-babel-view-src-block-info ()
347 "Display information on the current source block.
348 This includes header arguments, language and name, and is largely
349 a window into the `org-babel-get-src-block-info' function."
351 (let ((info (org-babel-get-src-block-info 'light
))
352 (full (lambda (it) (> (length it
) 0)))
353 (printf (lambda (fmt &rest args
) (princ (apply #'format fmt args
)))))
355 (with-help-window (help-buffer)
356 (let ((name (nth 4 info
))
358 (switches (nth 3 info
))
359 (header-args (nth 2 info
)))
360 (when name
(funcall printf
"Name: %s\n" name
))
361 (when lang
(funcall printf
"Lang: %s\n" lang
))
362 (when (funcall full switches
) (funcall printf
"Switches: %s\n" switches
))
363 (funcall printf
"Header Arguments:\n")
364 (dolist (pair (sort header-args
365 (lambda (a b
) (string< (symbol-name (car a
))
366 (symbol-name (car b
))))))
367 (when (funcall full
(cdr pair
))
368 (funcall printf
"\t%S%s\t%s\n"
370 (if (> (length (format "%S" (car pair
))) 7) "" "\t")
374 (defun org-babel-expand-src-block-maybe ()
375 "Conditionally expand a source block.
376 Detect if this is context for a org-babel src-block and if so
377 then run `org-babel-expand-src-block'."
379 (let ((info (org-babel-get-src-block-info)))
381 (progn (org-babel-expand-src-block current-prefix-arg info
) t
)
385 (defun org-babel-load-in-session-maybe ()
386 "Conditionally load a source block in a session.
387 Detect if this is context for a org-babel src-block and if so
388 then run `org-babel-load-in-session'."
390 (let ((info (org-babel-get-src-block-info)))
392 (progn (org-babel-load-in-session current-prefix-arg info
) t
)
395 (add-hook 'org-metaup-hook
'org-babel-load-in-session-maybe
)
398 (defun org-babel-pop-to-session-maybe ()
399 "Conditionally pop to a session.
400 Detect if this is context for a org-babel src-block and if so
401 then run `org-babel-pop-to-session'."
403 (let ((info (org-babel-get-src-block-info)))
404 (if info
(progn (org-babel-pop-to-session current-prefix-arg info
) t
) nil
)))
406 (add-hook 'org-metadown-hook
'org-babel-pop-to-session-maybe
)
408 (defconst org-babel-common-header-args-w-values
409 '((cache .
((no yes
)))
411 (colnames .
((nil no yes
)))
412 (comments .
((no link yes org both noweb
)))
414 (eval .
((never query
)))
415 (exports .
((code results both none
)))
418 (hlines .
((no yes
)))
419 (mkdirp .
((yes no
)))
422 (noweb .
((yes no tangle no-export strip-export
)))
425 (padline .
((yes no
)))
426 (results .
((file list vector table scalar verbatim
)
427 (raw html latex org code pp drawer
)
428 (replace silent none append prepend
)
430 (rownames .
((no yes
)))
434 (tangle .
((tangle yes no
:any
)))
438 (defconst org-babel-header-arg-names
439 (mapcar #'car org-babel-common-header-args-w-values
)
440 "Common header arguments used by org-babel.
441 Note that individual languages may define their own language
442 specific header arguments as well.")
444 (defvar org-babel-default-header-args
445 '((:session .
"none") (:results .
"replace") (:exports .
"code")
446 (:cache .
"no") (:noweb .
"no") (:hlines .
"no") (:tangle .
"no")
447 (:padnewline .
"yes"))
448 "Default arguments to use when evaluating a source block.")
450 (defvar org-babel-default-inline-header-args
451 '((:session .
"none") (:results .
"replace") (:exports .
"results"))
452 "Default arguments to use when evaluating an inline source block.")
454 (defvar org-babel-data-names
'("tblname" "results" "name"))
456 (defvar org-babel-result-regexp
457 (concat "^[ \t]*#\\+"
458 (regexp-opt org-babel-data-names t
)
460 ;; FIXME The string below is `org-ts-regexp'
461 "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
462 " \\)?\\([[:alnum:]]+\\)\\]\\)?\\:[ \t]*")
463 "Regular expression used to match result lines.
464 If the results are associated with a hash key then the hash will
465 be saved in the second match data.")
467 (defvar org-babel-result-w-name-regexp
468 (concat org-babel-result-regexp
469 "\\([^ ()\f\t\n\r\v]+\\)\\(\(\\(.*\\)\)\\|\\)"))
471 (defvar org-babel-min-lines-for-block-output
10
472 "The minimum number of lines for block output.
473 If number of lines of output is equal to or exceeds this
474 value, the output is placed in a #+begin_example...#+end_example
475 block. Otherwise the output is marked as literal by inserting
476 colons at the starts of the lines. This variable only takes
477 effect if the :results output option is in effect.")
479 (defvar org-babel-noweb-error-langs nil
480 "Languages for which Babel will raise literate programming errors.
481 List of languages for which errors should be raised when the
482 source code block satisfying a noweb reference in this language
483 can not be resolved.")
485 (defvar org-babel-hash-show
4
486 "Number of initial characters to show of a hidden results hash.")
488 (defvar org-babel-hash-show-time nil
489 "When not nil show the time the code block was evaluated in the result hash.")
491 (defvar org-babel-after-execute-hook nil
492 "Hook for functions to be called after `org-babel-execute-src-block'")
494 (defun org-babel-named-src-block-regexp-for-name (name)
495 "This generates a regexp used to match a src block named NAME."
496 (concat org-babel-src-name-regexp
(regexp-quote name
)
497 "[ \t(]*[\r\n]\\(?:^#.*[\r\n]\\)*"
498 (substring org-babel-src-block-regexp
1)))
500 (defun org-babel-named-data-regexp-for-name (name)
501 "This generates a regexp used to match data named NAME."
502 (concat org-babel-result-regexp
(regexp-quote name
) "\\([ \t]\\|$\\)"))
505 (defvar call-process-region
)
508 (defun org-babel-execute-src-block (&optional arg info params
)
509 "Execute the current source code block.
510 Insert the results of execution into the buffer. Source code
511 execution and the collection and formatting of results can be
512 controlled through a variety of header arguments.
514 With prefix argument ARG, force re-execution even if an existing
515 result cached in the buffer would otherwise have been returned.
517 Optionally supply a value for INFO in the form returned by
518 `org-babel-get-src-block-info'.
520 Optionally supply a value for PARAMS which will be merged with
521 the header arguments specified at the front of the source code
524 (let* ((info (or info
(org-babel-get-src-block-info)))
525 (merged-params (org-babel-merge-params (nth 2 info
) params
)))
526 (when (org-babel-confirm-evaluate
527 (let ((i info
)) (setf (nth 2 i
) merged-params
) i
))
528 (let* ((lang (nth 0 info
))
530 (org-babel-process-params merged-params
)
532 (cache-p (and (not arg
) (cdr (assoc :cache params
))
533 (string= "yes" (cdr (assoc :cache params
)))))
534 (result-params (cdr (assoc :result-params params
)))
535 (new-hash (when cache-p
(org-babel-sha1-hash info
)))
536 (old-hash (when cache-p
(org-babel-current-result-hash)))
537 (cache-current-p (and (not arg
) new-hash
(equal new-hash old-hash
)))
538 (body (setf (nth 1 info
)
539 (if (org-babel-noweb-p params
:eval
)
540 (org-babel-expand-noweb-references info
)
542 (dir (cdr (assoc :dir params
)))
544 (or (and dir
(file-name-as-directory (expand-file-name dir
)))
546 (org-babel-call-process-region-original
547 (if (boundp 'org-babel-call-process-region-original
)
548 org-babel-call-process-region-original
549 (symbol-function 'call-process-region
)))
550 (indent (car (last info
)))
553 (let ((call-process-region
555 (apply 'org-babel-tramp-handle-call-process-region args
))))
556 (let ((lang-check (lambda (f)
557 (let ((f (intern (concat "org-babel-execute:" f
))))
558 (when (fboundp f
) f
)))))
560 (or (funcall lang-check lang
)
561 (funcall lang-check
(symbol-name
562 (cdr (assoc lang org-src-lang-modes
))))
563 (error "No org-babel-execute function for %s!" lang
))))
565 (save-excursion ;; return cached result
566 (goto-char (org-babel-where-is-src-block-result nil info
))
567 (end-of-line 1) (forward-char 1)
568 (setq result
(org-babel-read-result))
569 (message (replace-regexp-in-string
570 "%" "%%" (format "%S" result
))) result
)
571 (message "executing %s code block%s..."
573 (if (nth 4 info
) (format " (%s)" (nth 4 info
)) ""))
574 (if (member "none" result-params
)
576 (funcall cmd body params
)
577 (message "result silenced"))
580 (if (and (eq (cdr (assoc :result-type params
)) 'value
)
581 (or (member "vector" result-params
)
582 (member "table" result-params
))
583 (not (listp result
)))
584 (list (list result
)) result
))
585 (funcall cmd body params
)))
586 ;; if non-empty result and :file then write to :file
587 (when (cdr (assoc :file params
))
589 (with-temp-file (cdr (assoc :file params
))
591 (org-babel-format-result
592 result
(cdr (assoc :sep
(nth 2 info
)))))))
593 (setq result
(cdr (assoc :file params
))))
594 (org-babel-insert-result
595 result result-params info new-hash indent lang
)
596 (run-hooks 'org-babel-after-execute-hook
)
599 (setq call-process-region
'org-babel-call-process-region-original
))))))
601 (defun org-babel-expand-body:generic
(body params
&optional var-lines
)
602 "Expand BODY with PARAMS.
603 Expand a block of code with org-babel according to its header
604 arguments. This generic implementation of body expansion is
605 called for languages which have not defined their own specific
606 org-babel-expand-body:lang function."
607 (mapconcat #'identity
(append var-lines
(list body
)) "\n"))
610 (defun org-babel-expand-src-block (&optional arg info params
)
611 "Expand the current source code block.
612 Expand according to the source code block's header
613 arguments and pop open the results in a preview buffer."
615 (let* ((info (or info
(org-babel-get-src-block-info)))
617 (params (setf (nth 2 info
)
618 (sort (org-babel-merge-params (nth 2 info
) params
)
619 (lambda (el1 el2
) (string< (symbol-name (car el1
))
620 (symbol-name (car el2
)))))))
621 (body (setf (nth 1 info
)
622 (if (org-babel-noweb-p params
:eval
)
623 (org-babel-expand-noweb-references info
) (nth 1 info
))))
624 (expand-cmd (intern (concat "org-babel-expand-body:" lang
)))
625 (assignments-cmd (intern (concat "org-babel-variable-assignments:"
628 (if (fboundp expand-cmd
) (funcall expand-cmd body params
)
629 (org-babel-expand-body:generic
630 body params
(and (fboundp assignments-cmd
)
631 (funcall assignments-cmd params
))))))
632 (if (called-interactively-p 'any
)
635 (concat "*Org-Babel Preview " (buffer-name) "[ " lang
" ]*"))
638 (defun org-babel-edit-distance (s1 s2
)
639 "Return the edit (levenshtein) distance between strings S1 S2."
640 (let* ((l1 (length s1
))
642 (dist (vconcat (mapcar (lambda (_) (make-vector (1+ l2
) nil
))
643 (number-sequence 1 (1+ l1
)))))
644 (in (lambda (i j
) (aref (aref dist i
) j
))))
645 (setf (aref (aref dist
0) 0) 0)
646 (dolist (j (number-sequence 1 l2
))
647 (setf (aref (aref dist
0) j
) j
))
648 (dolist (i (number-sequence 1 l1
))
649 (setf (aref (aref dist i
) 0) i
)
650 (dolist (j (number-sequence 1 l2
))
651 (setf (aref (aref dist i
) j
)
653 (1+ (funcall in
(1- i
) j
))
654 (1+ (funcall in i
(1- j
)))
655 (+ (if (equal (aref s1
(1- i
)) (aref s2
(1- j
))) 0 1)
656 (funcall in
(1- i
) (1- j
)))))))
659 (defun org-babel-combine-header-arg-lists (original &rest others
)
660 "Combine a number of lists of header argument names and arguments."
661 (let ((results (copy-sequence original
)))
662 (dolist (new-list others
)
663 (dolist (arg-pair new-list
)
664 (let ((header (car arg-pair
))
665 (args (cdr arg-pair
)))
667 (cons arg-pair
(org-remove-if
668 (lambda (pair) (equal header
(car pair
)))
673 (defun org-babel-check-src-block ()
674 "Check for misspelled header arguments in the current code block."
676 ;; TODO: report malformed code block
677 ;; TODO: report incompatible combinations of header arguments
678 ;; TODO: report uninitialized variables
679 (let ((too-close 2) ;; <- control closeness to report potential match
680 (names (mapcar #'symbol-name org-babel-header-arg-names
)))
681 (dolist (header (mapcar (lambda (arg) (substring (symbol-name (car arg
)) 1))
682 (and (org-babel-where-is-src-block-head)
683 (org-babel-parse-header-arguments
685 (match-string 4))))))
687 (when (and (not (string= header name
))
688 (<= (org-babel-edit-distance header name
) too-close
)
689 (not (member header names
)))
690 (error "Supplied header \"%S\" is suspiciously close to \"%S\""
692 (message "No suspicious header arguments found.")))
695 (defun org-babel-insert-header-arg ()
696 "Insert a header argument selecting from lists of common args and values."
698 (let* ((lang (car (org-babel-get-src-block-info 'light
)))
699 (lang-headers (intern (concat "org-babel-header-args:" lang
)))
700 (headers (org-babel-combine-header-arg-lists
701 org-babel-common-header-args-w-values
702 (if (boundp lang-headers
) (eval lang-headers
) nil
)))
703 (arg (org-icompleting-read
706 (lambda (header-spec) (symbol-name (car header-spec
)))
709 (let ((vals (cdr (assoc (intern arg
) headers
))))
715 (read-from-minibuffer "value: "))
719 (let ((arg (org-icompleting-read
721 (cons "default" (mapcar #'symbol-name group
)))))
722 (if (and arg
(not (string= "default" arg
)))
727 ;; Add support for completing-read insertion of header arguments after ":"
728 (defun org-babel-header-arg-expand ()
729 "Call `org-babel-enter-header-arg-w-completion' in appropriate contexts."
730 (when (and (equal (char-before) ?\
:) (org-babel-where-is-src-block-head))
731 (org-babel-enter-header-arg-w-completion (match-string 2))))
733 (defun org-babel-enter-header-arg-w-completion (&optional lang
)
734 "Insert header argument appropriate for LANG with completion."
735 (let* ((lang-headers-var (intern (concat "org-babel-header-args:" lang
)))
736 (lang-headers (when (boundp lang-headers-var
) (eval lang-headers-var
)))
737 (headers-w-values (org-babel-combine-header-arg-lists
738 org-babel-common-header-args-w-values lang-headers
))
739 (headers (mapcar #'symbol-name
(mapcar #'car headers-w-values
)))
740 (header (org-completing-read "Header Arg: " headers
))
741 (args (cdr (assoc (intern header
) headers-w-values
)))
742 (arg (when (and args
(listp args
))
744 (format "%s: " header
)
745 (mapcar #'symbol-name
(apply #'append args
))))))
746 (insert (concat header
" " (or arg
"")))
749 (add-hook 'org-tab-first-hook
'org-babel-header-arg-expand
)
752 (defun org-babel-load-in-session (&optional arg info
)
753 "Load the body of the current source-code block.
754 Evaluate the header arguments for the source block before
755 entering the session. After loading the body this pops open the
758 (let* ((info (or info
(org-babel-get-src-block-info)))
760 (params (nth 2 info
))
761 (body (setf (nth 1 info
)
762 (if (org-babel-noweb-p params
:eval
)
763 (org-babel-expand-noweb-references info
)
765 (session (cdr (assoc :session params
)))
766 (dir (cdr (assoc :dir params
)))
768 (or (and dir
(file-name-as-directory dir
)) default-directory
))
769 (cmd (intern (concat "org-babel-load-session:" lang
))))
770 (unless (fboundp cmd
)
771 (error "No org-babel-load-session function for %s!" lang
))
772 (pop-to-buffer (funcall cmd session body params
))
776 (defun org-babel-initiate-session (&optional arg info
)
777 "Initiate session for current code block.
778 If called with a prefix argument then resolve any variable
779 references in the header arguments and assign these variables in
780 the session. Copy the body of the code block to the kill ring."
782 (let* ((info (or info
(org-babel-get-src-block-info (not arg
))))
785 (params (nth 2 info
))
786 (session (cdr (assoc :session params
)))
787 (dir (cdr (assoc :dir params
)))
789 (or (and dir
(file-name-as-directory dir
)) default-directory
))
790 (init-cmd (intern (format "org-babel-%s-initiate-session" lang
)))
791 (prep-cmd (intern (concat "org-babel-prep-session:" lang
))))
792 (if (and (stringp session
) (string= session
"none"))
793 (error "This block is not using a session!"))
794 (unless (fboundp init-cmd
)
795 (error "No org-babel-initiate-session function for %s!" lang
))
796 (with-temp-buffer (insert (org-babel-trim body
))
797 (copy-region-as-kill (point-min) (point-max)))
799 (unless (fboundp prep-cmd
)
800 (error "No org-babel-prep-session function for %s!" lang
))
801 (funcall prep-cmd session params
))
802 (funcall init-cmd session params
)))
805 (defun org-babel-switch-to-session (&optional arg info
)
806 "Switch to the session of the current code block.
807 Uses `org-babel-initiate-session' to start the session. If called
808 with a prefix argument then this is passed on to
809 `org-babel-initiate-session'."
811 (pop-to-buffer (org-babel-initiate-session arg info
))
814 (defalias 'org-babel-pop-to-session
'org-babel-switch-to-session
)
817 (defun org-babel-switch-to-session-with-code (&optional arg info
)
818 "Switch to code buffer and display session."
822 (let ((other-window-buffer (window-buffer (next-window))))
823 (set-window-buffer (next-window) (current-buffer))
824 (set-window-buffer (selected-window) other-window-buffer
))
826 (info (org-babel-get-src-block-info))
827 (org-src-window-setup 'reorganize-frame
))
829 (org-babel-switch-to-session arg info
))
831 (funcall swap-windows
)))
833 (defmacro org-babel-do-in-edit-buffer
(&rest body
)
834 "Evaluate BODY in edit buffer if there is a code block at point.
835 Return t if a code block was found at point, nil otherwise."
836 `(let ((org-src-window-setup 'switch-invisibly
))
837 (when (and (org-babel-where-is-src-block-head)
838 (org-edit-src-code nil nil nil
))
839 (unwind-protect (progn ,@body
)
840 (if (org-bound-and-true-p org-edit-src-from-org-mode
)
841 (org-edit-src-exit)))
843 (def-edebug-spec org-babel-do-in-edit-buffer
(body))
845 (defun org-babel-do-key-sequence-in-edit-buffer (key)
846 "Read key sequence and execute the command in edit buffer.
847 Enter a key sequence to be executed in the language major-mode
848 edit buffer. For example, TAB will alter the contents of the
849 Org-mode code block according to the effect of TAB in the
850 language major-mode buffer. For languages that support
851 interactive sessions, this can be used to send code from the Org
852 buffer to the session for evaluation using the native major-mode
853 evaluation mechanisms."
854 (interactive "kEnter key-sequence to execute in edit buffer: ")
855 (org-babel-do-in-edit-buffer
857 (key-binding (or key
(read-key-sequence nil
))))))
859 (defvar org-bracket-link-regexp
)
862 (defun org-babel-open-src-block-result (&optional re-run
)
863 "If `point' is on a src block then open the results of the
864 source code block, otherwise return nil. With optional prefix
865 argument RE-RUN the source-code block is evaluated even if
866 results already exist."
868 (let ((info (org-babel-get-src-block-info)))
871 ;; go to the results, if there aren't any then run the block
872 (goto-char (or (and (not re-run
) (org-babel-where-is-src-block-result))
873 (progn (org-babel-execute-src-block)
874 (org-babel-where-is-src-block-result))))
876 (while (looking-at "[\n\r\t\f ]") (forward-char 1))
878 (if (looking-at org-bracket-link-regexp
)
881 (let ((r (org-babel-format-result
882 (org-babel-read-result) (cdr (assoc :sep
(nth 2 info
))))))
883 (pop-to-buffer (get-buffer-create "*Org-Babel Results*"))
884 (delete-region (point-min) (point-max))
889 (defmacro org-babel-map-src-blocks
(file &rest body
)
890 "Evaluate BODY forms on each source-block in FILE.
891 If FILE is nil evaluate BODY forms on source blocks in current
892 buffer. During evaluation of BODY the following local variables
893 are set relative to the currently matched code block.
895 full-block ------- string holding the entirety of the code block
896 beg-block -------- point at the beginning of the code block
897 end-block -------- point at the end of the matched code block
898 lang ------------- string holding the language of the code block
899 beg-lang --------- point at the beginning of the lang
900 end-lang --------- point at the end of the lang
901 switches --------- string holding the switches
902 beg-switches ----- point at the beginning of the switches
903 end-switches ----- point at the end of the switches
904 header-args ------ string holding the header-args
905 beg-header-args -- point at the beginning of the header-args
906 end-header-args -- point at the end of the header-args
907 body ------------- string holding the body of the code block
908 beg-body --------- point at the beginning of the body
909 end-body --------- point at the end of the body"
911 (let ((tempvar (make-symbol "file")))
912 `(let* ((,tempvar
,file
)
913 (visited-p (or (null ,tempvar
)
914 (get-file-buffer (expand-file-name ,tempvar
))))
915 (point (point)) to-be-removed
)
916 (save-window-excursion
917 (when ,tempvar
(find-file ,tempvar
))
918 (setq to-be-removed
(current-buffer))
919 (goto-char (point-min))
920 (while (re-search-forward org-babel-src-block-regexp nil t
)
921 (goto-char (match-beginning 0))
922 (let ((full-block (match-string 0))
923 (beg-block (match-beginning 0))
924 (end-block (match-end 0))
925 (lang (match-string 2))
926 (beg-lang (match-beginning 2))
927 (end-lang (match-end 2))
928 (switches (match-string 3))
929 (beg-switches (match-beginning 3))
930 (end-switches (match-end 3))
931 (header-args (match-string 4))
932 (beg-header-args (match-beginning 4))
933 (end-header-args (match-end 4))
934 (body (match-string 5))
935 (beg-body (match-beginning 5))
936 (end-body (match-end 5)))
938 (goto-char end-block
))))
939 (unless visited-p
(kill-buffer to-be-removed
))
941 (def-edebug-spec org-babel-map-src-blocks
(form body
))
944 (defmacro org-babel-map-inline-src-blocks
(file &rest body
)
945 "Evaluate BODY forms on each inline source-block in FILE.
946 If FILE is nil evaluate BODY forms on source blocks in current
949 (let ((tempvar (make-symbol "file")))
950 `(let* ((,tempvar
,file
)
951 (visited-p (or (null ,tempvar
)
952 (get-file-buffer (expand-file-name ,tempvar
))))
953 (point (point)) to-be-removed
)
954 (save-window-excursion
955 (when ,tempvar
(find-file ,tempvar
))
956 (setq to-be-removed
(current-buffer))
957 (goto-char (point-min))
958 (while (re-search-forward org-babel-inline-src-block-regexp nil t
)
959 (goto-char (match-beginning 1))
960 (save-match-data ,@body
)
961 (goto-char (match-end 0))))
962 (unless visited-p
(kill-buffer to-be-removed
))
964 (def-edebug-spec org-babel-map-inline-src-blocks
(form body
))
966 (defvar org-babel-lob-one-liner-regexp
)
969 (defmacro org-babel-map-call-lines
(file &rest body
)
970 "Evaluate BODY forms on each call line in FILE.
971 If FILE is nil evaluate BODY forms on source blocks in current
974 (let ((tempvar (make-symbol "file")))
975 `(let* ((,tempvar
,file
)
976 (visited-p (or (null ,tempvar
)
977 (get-file-buffer (expand-file-name ,tempvar
))))
978 (point (point)) to-be-removed
)
979 (save-window-excursion
980 (when ,tempvar
(find-file ,tempvar
))
981 (setq to-be-removed
(current-buffer))
982 (goto-char (point-min))
983 (while (re-search-forward org-babel-lob-one-liner-regexp nil t
)
984 (goto-char (match-beginning 1))
985 (save-match-data ,@body
)
986 (goto-char (match-end 0))))
987 (unless visited-p
(kill-buffer to-be-removed
))
989 (def-edebug-spec org-babel-map-call-lines
(form body
))
992 (defmacro org-babel-map-executables
(file &rest body
)
994 (let ((tempvar (make-symbol "file"))
995 (rx (make-symbol "rx")))
996 `(let* ((,tempvar
,file
)
997 (,rx
(concat "\\(" org-babel-src-block-regexp
998 "\\|" org-babel-inline-src-block-regexp
999 "\\|" org-babel-lob-one-liner-regexp
"\\)"))
1000 (visited-p (or (null ,tempvar
)
1001 (get-file-buffer (expand-file-name ,tempvar
))))
1002 (point (point)) to-be-removed
)
1003 (save-window-excursion
1004 (when ,tempvar
(find-file ,tempvar
))
1005 (setq to-be-removed
(current-buffer))
1006 (goto-char (point-min))
1007 (while (re-search-forward ,rx nil t
)
1008 (goto-char (match-beginning 1))
1009 (when (looking-at org-babel-inline-src-block-regexp
)(forward-char 1))
1010 (save-match-data ,@body
)
1011 (goto-char (match-end 0))))
1012 (unless visited-p
(kill-buffer to-be-removed
))
1013 (goto-char point
))))
1014 (def-edebug-spec org-babel-map-executables
(form body
))
1017 (defun org-babel-execute-buffer (&optional arg
)
1018 "Execute source code blocks in a buffer.
1019 Call `org-babel-execute-src-block' on every source block in
1020 the current buffer."
1022 (org-babel-eval-wipe-error-buffer)
1023 (org-save-outline-visibility t
1024 (org-babel-map-executables nil
1025 (if (looking-at org-babel-lob-one-liner-regexp
)
1026 (org-babel-lob-execute-maybe)
1027 (org-babel-execute-src-block arg
)))))
1030 (defun org-babel-execute-subtree (&optional arg
)
1031 "Execute source code blocks in a subtree.
1032 Call `org-babel-execute-src-block' on every source block in
1033 the current subtree."
1037 (org-narrow-to-subtree)
1038 (org-babel-execute-buffer arg
)
1042 (defun org-babel-sha1-hash (&optional info
)
1043 "Generate an sha1 hash based on the value of info."
1045 (let ((print-level nil
)
1046 (info (or info
(org-babel-get-src-block-info))))
1048 (sort (copy-sequence (nth 2 info
))
1049 (lambda (a b
) (string< (car a
) (car b
)))))
1050 (let* ((rm (lambda (lst)
1051 (dolist (p '("replace" "silent" "none"
1052 "append" "prepend"))
1053 (setq lst
(remove p lst
)))
1056 (let ((v (if (and (listp (cdr arg
)) (null (cddr arg
)))
1057 (copy-sequence (cdr arg
))
1059 (when (and v
(not (and (sequencep v
)
1063 ((and (listp v
) ; lists are sorted
1064 (member (car arg
) '(:result-params
)))
1065 (sort (funcall rm v
) #'string
<))
1066 ((and (stringp v
) ; strings are sorted
1067 (member (car arg
) '(:results
:exports
)))
1068 (mapconcat #'identity
(sort (funcall rm
(split-string v
))
1072 (when (org-called-interactively-p 'interactive
) (message hash
)) hash
)
1073 (let ((it (format "%s-%s"
1076 (delq nil
(mapcar (lambda (arg)
1077 (let ((normalized (funcall norm arg
)))
1079 (format "%S" normalized
))))
1084 (defun org-babel-current-result-hash ()
1085 "Return the current in-buffer hash."
1086 (org-babel-where-is-src-block-result)
1087 (org-no-properties (match-string 5)))
1089 (defun org-babel-set-current-result-hash (hash)
1090 "Set the current in-buffer hash to HASH."
1091 (org-babel-where-is-src-block-result)
1092 (save-excursion (goto-char (match-beginning 3))
1093 ;; (mapc #'delete-overlay (overlays-at (point)))
1094 (replace-match hash nil nil nil
3)
1095 (org-babel-hide-hash)))
1097 (defun org-babel-hide-hash ()
1098 "Hide the hash in the current results line.
1099 Only the initial `org-babel-hash-show' characters of the hash
1100 will remain visible."
1101 (add-to-invisibility-spec '(org-babel-hide-hash . t
))
1103 (when (and (re-search-forward org-babel-result-regexp nil t
)
1105 (let* ((start (match-beginning 5))
1106 (hide-start (+ org-babel-hash-show start
))
1108 (hash (match-string 5))
1110 (setq ov1
(make-overlay start hide-start
))
1111 (setq ov2
(make-overlay hide-start end
))
1112 (overlay-put ov2
'invisible
'org-babel-hide-hash
)
1113 (overlay-put ov1
'babel-hash hash
)))))
1115 (defun org-babel-hide-all-hashes ()
1116 "Hide the hash in the current buffer.
1117 Only the initial `org-babel-hash-show' characters of each hash
1118 will remain visible. This function should be called as part of
1119 the `org-mode-hook'."
1121 (while (and (not org-babel-hash-show-time
)
1122 (re-search-forward org-babel-result-regexp nil t
))
1123 (goto-char (match-beginning 0))
1124 (org-babel-hide-hash)
1125 (goto-char (match-end 0)))))
1126 (add-hook 'org-mode-hook
'org-babel-hide-all-hashes
)
1128 (defun org-babel-hash-at-point (&optional point
)
1129 "Return the value of the hash at POINT.
1130 The hash is also added as the last element of the kill ring.
1131 This can be called with C-c C-c."
1133 (let ((hash (car (delq nil
(mapcar
1134 (lambda (ol) (overlay-get ol
'babel-hash
))
1135 (overlays-at (or point
(point))))))))
1136 (when hash
(kill-new hash
) (message hash
))))
1137 (add-hook 'org-ctrl-c-ctrl-c-hook
'org-babel-hash-at-point
)
1139 (defun org-babel-result-hide-spec ()
1140 "Hide portions of results lines.
1141 Add `org-babel-hide-result' as an invisibility spec for hiding
1142 portions of results lines."
1143 (add-to-invisibility-spec '(org-babel-hide-result . t
)))
1144 (add-hook 'org-mode-hook
'org-babel-result-hide-spec
)
1146 (defvar org-babel-hide-result-overlays nil
1147 "Overlays hiding results.")
1149 (defun org-babel-result-hide-all ()
1150 "Fold all results in the current buffer."
1152 (org-babel-show-result-all)
1154 (while (re-search-forward org-babel-result-regexp nil t
)
1155 (save-excursion (goto-char (match-beginning 0))
1156 (org-babel-hide-result-toggle-maybe)))))
1158 (defun org-babel-show-result-all ()
1159 "Unfold all results in the current buffer."
1160 (mapc 'delete-overlay org-babel-hide-result-overlays
)
1161 (setq org-babel-hide-result-overlays nil
))
1164 (defun org-babel-hide-result-toggle-maybe ()
1165 "Toggle visibility of result at point."
1167 (let ((case-fold-search t
))
1169 (beginning-of-line 1)
1170 (looking-at org-babel-result-regexp
))
1171 (progn (org-babel-hide-result-toggle)
1172 t
) ;; to signal that we took action
1173 nil
))) ;; to signal that we did not
1175 (defun org-babel-hide-result-toggle (&optional force
)
1176 "Toggle the visibility of the current result."
1180 (if (re-search-forward org-babel-result-regexp nil t
)
1181 (let ((start (progn (beginning-of-line 2) (- (point) 1)))
1183 (while (looking-at org-babel-multi-line-header-regexp
)
1185 (goto-char (- (org-babel-result-end) 1)) (point)))
1187 (if (memq t
(mapcar (lambda (overlay)
1188 (eq (overlay-get overlay
'invisible
)
1189 'org-babel-hide-result
))
1190 (overlays-at start
)))
1191 (if (or (not force
) (eq force
'off
))
1193 (when (member ov org-babel-hide-result-overlays
)
1194 (setq org-babel-hide-result-overlays
1195 (delq ov org-babel-hide-result-overlays
)))
1196 (when (eq (overlay-get ov
'invisible
)
1197 'org-babel-hide-result
)
1198 (delete-overlay ov
)))
1199 (overlays-at start
)))
1200 (setq ov
(make-overlay start end
))
1201 (overlay-put ov
'invisible
'org-babel-hide-result
)
1202 ;; make the block accessible to isearch
1204 ov
'isearch-open-invisible
1206 (when (member ov org-babel-hide-result-overlays
)
1207 (setq org-babel-hide-result-overlays
1208 (delq ov org-babel-hide-result-overlays
)))
1209 (when (eq (overlay-get ov
'invisible
)
1210 'org-babel-hide-result
)
1211 (delete-overlay ov
))))
1212 (push ov org-babel-hide-result-overlays
)))
1213 (error "Not looking at a result line"))))
1215 ;; org-tab-after-check-for-cycling-hook
1216 (add-hook 'org-tab-first-hook
'org-babel-hide-result-toggle-maybe
)
1217 ;; Remove overlays when changing major mode
1218 (add-hook 'org-mode-hook
1219 (lambda () (org-add-hook 'change-major-mode-hook
1220 'org-babel-show-result-all
'append
'local
)))
1222 (defvar org-file-properties
)
1223 (defun org-babel-params-from-properties (&optional lang
)
1224 "Retrieve parameters specified as properties.
1225 Return an association list of any source block params which
1226 may be specified in the properties of the current outline entry."
1229 (org-babel-parse-multiple-vars
1232 (lambda (header-arg)
1233 (and (setq val
(org-entry-get (point) header-arg t
))
1234 (cons (intern (concat ":" header-arg
))
1235 (org-babel-read val
))))
1240 (org-babel-combine-header-arg-lists
1241 org-babel-common-header-args-w-values
1243 (setq sym
(intern (concat "org-babel-header-args:" lang
)))
1244 (and (boundp sym
) (eval sym
))))))))))))
1246 (defvar org-src-preserve-indentation
)
1247 (defun org-babel-parse-src-block-match ()
1248 "Parse the results from a match of the `org-babel-src-block-regexp'."
1249 (let* ((block-indentation (length (match-string 1)))
1250 (lang (org-no-properties (match-string 2)))
1251 (lang-headers (intern (concat "org-babel-default-header-args:" lang
)))
1252 (switches (match-string 3))
1253 (body (org-no-properties
1254 (let* ((body (match-string 5))
1255 (sub-length (- (length body
) 1)))
1256 (if (and (> sub-length
0)
1257 (string= "\n" (substring body sub-length
)))
1258 (substring body
0 sub-length
)
1260 (preserve-indentation (or org-src-preserve-indentation
1262 (string-match "-i\\>" switches
)))))
1264 ;; get block body less properties, protective commas, and indentation
1267 (insert (org-unescape-code-in-string body
))
1268 (unless preserve-indentation
(org-do-remove-indentation))
1270 (org-babel-merge-params
1271 org-babel-default-header-args
1272 (org-babel-params-from-properties lang
)
1273 (if (boundp lang-headers
) (eval lang-headers
) nil
)
1274 (org-babel-parse-header-arguments
1275 (org-no-properties (or (match-string 4) ""))))
1277 block-indentation
)))
1279 (defun org-babel-parse-inline-src-block-match ()
1280 "Parse the results from a match of the `org-babel-inline-src-block-regexp'."
1281 (let* ((lang (org-no-properties (match-string 2)))
1282 (lang-headers (intern (concat "org-babel-default-header-args:" lang
))))
1284 (org-unescape-code-in-string (org-no-properties (match-string 5)))
1285 (org-babel-merge-params
1286 org-babel-default-inline-header-args
1287 (org-babel-params-from-properties lang
)
1288 (if (boundp lang-headers
) (eval lang-headers
) nil
)
1289 (org-babel-parse-header-arguments
1290 (org-no-properties (or (match-string 4) "")))))))
1292 (defun org-babel-balanced-split (string alts
)
1293 "Split STRING on instances of ALTS.
1294 ALTS is a cons of two character options where each option may be
1295 either the numeric code of a single character or a list of
1296 character alternatives. For example to split on balanced
1297 instances of \"[ \t]:\" set ALTS to '((32 9) . 58)."
1298 (let* ((matches (lambda (ch spec
) (if (listp spec
) (member ch spec
) (equal spec ch
))))
1299 (matched (lambda (ch last
)
1301 (and (funcall matches ch
(cdr alts
))
1302 (funcall matches last
(car alts
)))
1303 (funcall matches ch alts
))))
1304 (balance 0) (last 0)
1306 (mapc (lambda (ch) ; split on [], (), "" balanced instances of [ \t]:
1307 (setq balance
(+ balance
1308 (cond ((or (equal 91 ch
) (equal 40 ch
)) 1)
1309 ((or (equal 93 ch
) (equal 41 ch
)) -
1)
1311 (when (and (equal 34 ch
) (not (equal 92 last
)))
1312 (setq quote
(not quote
)))
1313 (setq partial
(cons ch partial
))
1314 (when (and (= balance
0) (not quote
) (funcall matched ch last
))
1315 (setq lst
(cons (apply #'string
(nreverse
1322 (string-to-list string
))
1323 (nreverse (cons (apply #'string
(nreverse partial
)) lst
))))
1325 (defun org-babel-join-splits-near-ch (ch list
)
1326 "Join splits where \"=\" is on either end of the split."
1327 (let ((last= (lambda (str) (= ch
(aref str
(1- (length str
))))))
1328 (first= (lambda (str) (= ch
(aref str
0)))))
1330 (org-reduce (lambda (acc el
)
1331 (let ((head (car acc
)))
1332 (if (and head
(or (funcall last
= head
) (funcall first
= el
)))
1333 (cons (concat head el
) (cdr acc
))
1335 list
:initial-value nil
))))
1337 (defun org-babel-parse-header-arguments (arg-string)
1338 "Parse a string of header arguments returning an alist."
1339 (when (> (length arg-string
) 0)
1340 (org-babel-parse-multiple-vars
1345 "\\([^ \f\t\n\r\v]+\\)[ \f\t\n\r\v]+\\([^ \f\t\n\r\v]+.*\\)"
1347 (cons (intern (match-string 1 arg
))
1348 (org-babel-read (org-babel-chomp (match-string 2 arg
))))
1349 (cons (intern (org-babel-chomp arg
)) nil
)))
1351 (cons (car raw
) (mapcar (lambda (r) (concat ":" r
)) (cdr raw
))))
1352 (org-babel-balanced-split arg-string
'((32 9) .
58))))))))
1354 (defun org-babel-parse-multiple-vars (header-arguments)
1355 "Expand multiple variable assignments behind a single :var keyword.
1357 This allows expression of multiple variables with one :var as
1360 #+PROPERTY: var foo=1, bar=2"
1362 (mapc (lambda (pair)
1363 (if (eq (car pair
) :var
)
1364 (mapcar (lambda (v) (push (cons :var
(org-babel-trim v
)) results
))
1365 (org-babel-join-splits-near-ch
1366 61 (org-babel-balanced-split (cdr pair
) 32)))
1367 (push pair results
)))
1369 (nreverse results
)))
1371 (defun org-babel-process-params (params)
1372 "Expand variables in PARAMS and add summary parameters."
1373 (let* ((processed-vars (mapcar (lambda (el)
1374 (if (consp (cdr el
))
1376 (org-babel-ref-parse (cdr el
))))
1377 (org-babel-get-header params
:var
)))
1378 (vars-and-names (if (and (assoc :colname-names params
)
1379 (assoc :rowname-names params
))
1380 (list processed-vars
)
1381 (org-babel-disassemble-tables
1383 (cdr (assoc :hlines params
))
1384 (cdr (assoc :colnames params
))
1385 (cdr (assoc :rownames params
)))))
1386 (raw-result (or (cdr (assoc :results params
)) ""))
1387 (result-params (append
1388 (split-string (if (stringp raw-result
)
1391 (cdr (assoc :result-params params
)))))
1393 (mapcar (lambda (var) (cons :var var
)) (car vars-and-names
))
1395 (cons :colname-names
(or (cdr (assoc :colname-names params
))
1396 (cadr vars-and-names
)))
1397 (cons :rowname-names
(or (cdr (assoc :rowname-names params
))
1398 (caddr vars-and-names
)))
1399 (cons :result-params result-params
)
1400 (cons :result-type
(cond ((member "output" result-params
) 'output
)
1401 ((member "value" result-params
) 'value
)
1403 (org-babel-get-header params
:var
'other
))))
1405 ;; row and column names
1406 (defun org-babel-del-hlines (table)
1407 "Remove all 'hlines from TABLE."
1408 (remove 'hline table
))
1410 (defun org-babel-get-colnames (table)
1411 "Return the column names of TABLE.
1412 Return a cons cell, the `car' of which contains the TABLE less
1413 colnames, and the `cdr' of which contains a list of the column
1415 (if (equal 'hline
(nth 1 table
))
1416 (cons (cddr table
) (car table
))
1417 (cons (cdr table
) (car table
))))
1419 (defun org-babel-get-rownames (table)
1420 "Return the row names of TABLE.
1421 Return a cons cell, the `car' of which contains the TABLE less
1422 colnames, and the `cdr' of which contains a list of the column
1423 names. Note: this function removes any hlines in TABLE."
1424 (let* ((trans (lambda (table) (apply #'mapcar
* #'list table
)))
1426 (mapcar (lambda (el) (if (listp el
) (length el
) 0)) table
)))
1427 (table (funcall trans
(mapcar (lambda (row)
1428 (if (not (equal row
'hline
))
1432 (setq row
(cons 'hline row
)))
1435 (cons (mapcar (lambda (row) (if (equal (car row
) 'hline
) 'hline row
))
1436 (funcall trans
(cdr table
)))
1437 (remove 'hline
(car table
)))))
1439 (defun org-babel-put-colnames (table colnames
)
1440 "Add COLNAMES to TABLE if they exist."
1441 (if colnames
(apply 'list colnames
'hline table
) table
))
1443 (defun org-babel-put-rownames (table rownames
)
1444 "Add ROWNAMES to TABLE if they exist."
1446 (mapcar (lambda (row)
1448 (cons (or (pop rownames
) "") row
)
1452 (defun org-babel-pick-name (names selector
)
1453 "Select one out of an alist of row or column names.
1454 SELECTOR can be either a list of names in which case those names
1455 will be returned directly, or an index into the list NAMES in
1456 which case the indexed names will be return."
1457 (if (listp selector
)
1460 (if (and selector
(symbolp selector
) (not (equal t selector
)))
1461 (cdr (assoc selector names
))
1462 (if (integerp selector
)
1463 (nth (- selector
1) names
)
1464 (cdr (car (last names
))))))))
1466 (defun org-babel-disassemble-tables (vars hlines colnames rownames
)
1467 "Parse tables for further processing.
1468 Process the variables in VARS according to the HLINES,
1469 ROWNAMES and COLNAMES header arguments. Return a list consisting
1470 of the vars, cnames and rnames."
1471 (let (cnames rnames
)
1475 (when (listp (cdr var
))
1476 (when (and (not (equal colnames
"no"))
1477 (or colnames
(and (equal (nth 1 (cdr var
)) 'hline
)
1478 (not (member 'hline
(cddr (cdr var
)))))))
1479 (let ((both (org-babel-get-colnames (cdr var
))))
1480 (setq cnames
(cons (cons (car var
) (cdr both
))
1482 (setq var
(cons (car var
) (car both
)))))
1483 (when (and rownames
(not (equal rownames
"no")))
1484 (let ((both (org-babel-get-rownames (cdr var
))))
1485 (setq rnames
(cons (cons (car var
) (cdr both
))
1487 (setq var
(cons (car var
) (car both
)))))
1488 (when (and hlines
(not (equal hlines
"yes")))
1489 (setq var
(cons (car var
) (org-babel-del-hlines (cdr var
))))))
1492 (reverse cnames
) (reverse rnames
))))
1494 (defun org-babel-reassemble-table (table colnames rownames
)
1495 "Add column and row names to a table.
1496 Given a TABLE and set of COLNAMES and ROWNAMES add the names
1497 to the table for reinsertion to org-mode."
1500 (if (and colnames
(listp (car table
)) (= (length (car table
))
1502 (org-babel-put-colnames table colnames
) table
))
1503 (if (and rownames
(= (length table
) (length rownames
)))
1504 (org-babel-put-rownames table rownames
) table
))
1507 (defun org-babel-where-is-src-block-head ()
1508 "Find where the current source block begins.
1509 Return the point at the beginning of the current source
1510 block. Specifically at the beginning of the #+BEGIN_SRC line.
1511 If the point is not on a source block then return nil."
1512 (let ((initial (point)) (case-fold-search t
) top bottom
)
1514 (save-excursion ;; on a source name line or a #+header line
1515 (beginning-of-line 1)
1516 (and (or (looking-at org-babel-src-name-regexp
)
1517 (looking-at org-babel-multi-line-header-regexp
))
1519 (while (and (forward-line 1)
1520 (or (looking-at org-babel-src-name-regexp
)
1521 (looking-at org-babel-multi-line-header-regexp
))))
1522 (looking-at org-babel-src-block-regexp
))
1524 (save-excursion ;; on a #+begin_src line
1525 (beginning-of-line 1)
1526 (and (looking-at org-babel-src-block-regexp
)
1528 (save-excursion ;; inside a src block
1530 (re-search-backward "^[ \t]*#\\+begin_src" nil t
) (setq top
(point))
1531 (re-search-forward "^[ \t]*#\\+end_src" nil t
) (setq bottom
(point))
1532 (< top initial
) (< initial bottom
)
1533 (progn (goto-char top
) (beginning-of-line 1)
1534 (looking-at org-babel-src-block-regexp
))
1538 (defun org-babel-goto-src-block-head ()
1539 "Go to the beginning of the current code block."
1542 (if head
(goto-char head
) (error "Not currently in a code block")))
1543 (org-babel-where-is-src-block-head)))
1546 (defun org-babel-goto-named-src-block (name)
1547 "Go to a named source-code block."
1549 (let ((completion-ignore-case t
)
1550 (case-fold-search t
)
1551 (under-point (thing-at-point 'line
)))
1552 (list (org-icompleting-read
1553 "source-block name: " (org-babel-src-block-names) nil t
1556 ((string-match (org-babel-noweb-wrap) under-point
)
1557 (let ((block-name (match-string 1 under-point
)))
1558 (string-match "[^(]*" block-name
)
1559 (match-string 0 block-name
)))
1561 ((string-match org-babel-lob-one-liner-regexp under-point
)
1562 (let ((source-info (car (org-babel-lob-get-info))))
1563 (if (string-match "^\\([^\\[]+?\\)\\(\\[.*\\]\\)?(" source-info
)
1564 (let ((source-name (match-string 1 source-info
)))
1567 ((string-match (concat "#\\+" org-babel-results-keyword
1568 "\\:\s+\\([^\\(]*\\)") under-point
)
1569 (match-string 1 under-point
))
1571 ((and (thing-at-point 'symbol
))
1572 (org-babel-find-named-block (thing-at-point 'symbol
))
1573 (thing-at-point 'symbol
))
1575 (let ((point (org-babel-find-named-block name
)))
1577 ;; taken from `org-open-at-point'
1578 (progn (org-mark-ring-push) (goto-char point
) (org-show-context))
1579 (message "source-code block '%s' not found in this buffer" name
))))
1581 (defun org-babel-find-named-block (name)
1582 "Find a named source-code block.
1583 Return the location of the source block identified by source
1584 NAME, or nil if no such block exists. Set match data according to
1585 org-babel-named-src-block-regexp."
1587 (let ((case-fold-search t
)
1588 (regexp (org-babel-named-src-block-regexp-for-name name
)) msg
)
1589 (goto-char (point-min))
1590 (when (or (re-search-forward regexp nil t
)
1591 (re-search-backward regexp nil t
))
1592 (match-beginning 0)))))
1594 (defun org-babel-src-block-names (&optional file
)
1595 "Returns the names of source blocks in FILE or the current buffer."
1597 (when file
(find-file file
)) (goto-char (point-min))
1598 (let ((case-fold-search t
) names
)
1599 (while (re-search-forward org-babel-src-name-w-name-regexp nil t
)
1600 (setq names
(cons (match-string 3) names
)))
1604 (defun org-babel-goto-named-result (name)
1605 "Go to a named result."
1607 (let ((completion-ignore-case t
))
1608 (list (org-icompleting-read "source-block name: "
1609 (org-babel-result-names) nil t
))))
1610 (let ((point (org-babel-find-named-result name
)))
1612 ;; taken from `org-open-at-point'
1613 (progn (goto-char point
) (org-show-context))
1614 (message "result '%s' not found in this buffer" name
))))
1616 (defun org-babel-find-named-result (name &optional point
)
1617 "Find a named result.
1618 Return the location of the result named NAME in the current
1619 buffer or nil if no such result exists."
1621 (let ((case-fold-search t
))
1622 (goto-char (or point
(point-min)))
1623 (catch 'is-a-code-block
1624 (when (re-search-forward
1625 (concat org-babel-result-regexp
1626 "[ \t]" (regexp-quote name
) "[ \t]*[\n\f\v\r]") nil t
)
1627 (when (and (string= "name" (downcase (match-string 1)))
1628 (or (beginning-of-line 1)
1629 (looking-at org-babel-src-block-regexp
)
1630 (looking-at org-babel-multi-line-header-regexp
)))
1631 (throw 'is-a-code-block
(org-babel-find-named-result name
(point))))
1632 (beginning-of-line 0) (point))))))
1634 (defun org-babel-result-names (&optional file
)
1635 "Returns the names of results in FILE or the current buffer."
1637 (when file
(find-file file
)) (goto-char (point-min))
1638 (let ((case-fold-search t
) names
)
1639 (while (re-search-forward org-babel-result-w-name-regexp nil t
)
1640 (setq names
(cons (match-string 4) names
)))
1644 (defun org-babel-next-src-block (&optional arg
)
1645 "Jump to the next source block.
1646 With optional prefix argument ARG, jump forward ARG many source blocks."
1648 (org-next-block arg nil org-babel-src-block-regexp
))
1651 (defun org-babel-previous-src-block (&optional arg
)
1652 "Jump to the previous source block.
1653 With optional prefix argument ARG, jump backward ARG many source blocks."
1655 (org-previous-block arg org-babel-src-block-regexp
))
1657 (defvar org-babel-load-languages
)
1660 (defun org-babel-mark-block ()
1661 "Mark current src block."
1667 (looking-at org-babel-src-block-regexp
))
1668 (push-mark (match-end 5) nil t
)
1669 (goto-char (match-beginning 5))))
1670 (org-babel-where-is-src-block-head)))
1672 (defun org-babel-demarcate-block (&optional arg
)
1673 "Wrap or split the code in the region or on the point.
1674 When called from inside of a code block the current block is
1675 split. When called from outside of a code block a new code block
1676 is created. In both cases if the region is demarcated and if the
1677 region is not active then the point is demarcated."
1679 (let ((info (org-babel-get-src-block-info 'light
))
1680 (headers (progn (org-babel-where-is-src-block-head)
1682 (stars (concat (make-string (or (org-current-level) 1) ?
*) " ")))
1688 (let ((lang (nth 0 info
))
1689 (indent (make-string (nth 5 info
) ?
)))
1690 (when (string-match "^[[:space:]]*$"
1691 (buffer-substring (point-at-bol)
1693 (delete-region (point-at-bol) (point-at-eol)))
1695 (if (looking-at "^") "" "\n")
1696 indent
"#+end_src\n"
1697 (if arg stars indent
) "\n"
1698 indent
"#+begin_src " lang
1699 (if (> (length headers
) 1)
1700 (concat " " headers
) headers
)
1701 (if (looking-at "[\n\r]")
1703 (concat "\n" (make-string (current-column) ?
)))))))
1704 (move-end-of-line 2))
1705 (sort (if (org-region-active-p) (list (mark) (point)) (list (point))) #'>))
1706 (let ((start (point))
1707 (lang (org-icompleting-read "Lang: "
1708 (mapcar (lambda (el) (symbol-name (car el
)))
1709 org-babel-load-languages
)))
1710 (body (delete-and-extract-region
1711 (if (org-region-active-p) (mark) (point)) (point))))
1712 (insert (concat (if (looking-at "^") "" "\n")
1713 (if arg
(concat stars
"\n") "")
1714 "#+begin_src " lang
"\n"
1716 (if (or (= (length body
) 0)
1717 (string-match "[\r\n]$" body
)) "" "\n")
1719 (goto-char start
) (move-end-of-line 1)))))
1721 (defvar org-babel-lob-one-liner-regexp
)
1722 (defun org-babel-where-is-src-block-result (&optional insert info hash indent
)
1723 "Find where the current source block results begin.
1724 Return the point at the beginning of the result of the current
1725 source block. Specifically at the beginning of the results line.
1726 If no result exists for this block then create a results line
1727 following the source block."
1729 (let* ((case-fold-search t
)
1730 (on-lob-line (save-excursion
1731 (beginning-of-line 1)
1732 (looking-at org-babel-lob-one-liner-regexp
)))
1733 (inlinep (when (org-babel-get-inline-src-block-matches)
1735 (name (if on-lob-line
1736 (mapconcat #'identity
(butlast (org-babel-lob-get-info)) "")
1737 (nth 4 (or info
(org-babel-get-src-block-info 'light
)))))
1738 (head (unless on-lob-line
(org-babel-where-is-src-block-head)))
1740 (when head
(goto-char head
))
1741 (org-with-wide-buffer
1743 found
;; was there a result (before we potentially insert one)
1748 ;; - return t if it is found, else return nil
1749 ;; - if it does not need to be rebuilt, then don't set end
1750 ;; - if it does need to be rebuilt then do set end
1751 name
(setq beg
(org-babel-find-named-result name
))
1753 (when (and hash
(not (string= hash
(match-string 5))))
1754 (goto-char beg
) (setq end beg
) ;; beginning of result
1756 (delete-region end
(org-babel-result-end)) nil
)))
1759 ;; - return t if it is found, else return nil
1760 ;; - if it is found, and the hash doesn't match, delete and set end
1761 (or on-lob-line
(re-search-forward "^[ \t]*#\\+end_src" nil t
))
1762 (progn (end-of-line 1)
1763 (if (eobp) (insert "\n") (forward-char 1))
1766 (progn ;; unnamed results line already exists
1767 (re-search-forward "[^ \f\t\n\r\v]" nil t
)
1768 (beginning-of-line 1)
1770 (concat org-babel-result-regexp
"\n")))
1771 (let ((this-hash (match-string 5)))
1773 ;; must remove and rebuild if hash!=old-hash
1774 (if (and hash
(not (string= hash this-hash
)))
1778 end
(org-babel-result-end)))
1779 (setq end nil
)))))))))))
1780 (if (not (and insert end
)) found
1783 (if (looking-at "[\n\r]") (forward-char 1) (insert "\n")))
1785 (when (wholenump indent
) (make-string indent ?
))
1786 "#+" org-babel-results-keyword
1788 (if org-babel-hash-show-time
1790 "["(format-time-string "<%Y-%m-%d %H:%M:%S>")" "hash
"]")
1791 (concat "["hash
"]")))
1793 (when name
(concat " " name
)) "\n"))
1794 (unless beg
(insert "\n") (backward-char))
1795 (beginning-of-line 0)
1796 (if hash
(org-babel-hide-hash))
1799 (defvar org-block-regexp
)
1800 (defun org-babel-read-result ()
1801 "Read the result at `point' into emacs-lisp."
1802 (let ((case-fold-search t
) result-string
)
1804 ((org-at-table-p) (org-babel-read-table))
1805 ((org-at-item-p) (org-babel-read-list))
1806 ((looking-at org-bracket-link-regexp
) (org-babel-read-link))
1807 ((looking-at org-block-regexp
) (org-babel-trim (match-string 4)))
1808 ((looking-at "^[ \t]*: ")
1811 (mapconcat (lambda (line)
1812 (if (and (> (length line
) 1)
1813 (string-match "^[ \t]*: \\(.+\\)" line
))
1814 (match-string 1 line
)
1818 (point) (org-babel-result-end)) "[\r\n]+")
1820 (or (org-babel-number-p result-string
) result-string
))
1821 ((looking-at org-babel-result-regexp
)
1822 (save-excursion (forward-line 1) (org-babel-read-result))))))
1824 (defun org-babel-read-table ()
1825 "Read the table at `point' into emacs-lisp."
1826 (mapcar (lambda (row)
1827 (if (and (symbolp row
) (equal row
'hline
)) row
1828 (mapcar (lambda (el) (org-babel-read el
'inhibit-lisp-eval
)) row
)))
1829 (org-table-to-lisp)))
1831 (defun org-babel-read-list ()
1832 "Read the list at `point' into emacs-lisp."
1833 (mapcar (lambda (el) (org-babel-read el
'inhibit-lisp-eval
))
1834 (mapcar #'cadr
(cdr (org-list-parse-list)))))
1836 (defvar org-link-types-re
)
1837 (defun org-babel-read-link ()
1838 "Read the link at `point' into emacs-lisp.
1839 If the path of the link is a file path it is expanded using
1840 `expand-file-name'."
1841 (let* ((case-fold-search t
)
1842 (raw (and (looking-at org-bracket-link-regexp
)
1843 (org-no-properties (match-string 1))))
1844 (type (and (string-match org-link-types-re raw
)
1845 (match-string 1 raw
))))
1847 ((not type
) (expand-file-name raw
))
1848 ((string= type
"file")
1849 (and (string-match "file\\(.*\\):\\(.+\\)" raw
)
1850 (expand-file-name (match-string 2 raw
))))
1853 (defun org-babel-format-result (result &optional sep
)
1854 "Format RESULT for writing to file."
1855 (let ((echo-res (lambda (r) (if (stringp r
) r
(format "%S" r
)))))
1859 result
(list :sep
(or sep
"\t") :fmt echo-res
))
1861 (funcall echo-res result
))))
1863 (defun org-babel-insert-result
1864 (result &optional result-params info hash indent lang
)
1865 "Insert RESULT into the current buffer.
1866 By default RESULT is inserted after the end of the
1867 current source block. With optional argument RESULT-PARAMS
1868 controls insertion of results in the org-mode file.
1869 RESULT-PARAMS can take the following values:
1871 replace - (default option) insert results after the source block
1872 replacing any previously inserted results
1874 silent -- no results are inserted into the Org-mode buffer but
1875 the results are echoed to the minibuffer and are
1876 ingested by Emacs (a potentially time consuming
1879 file ---- the results are interpreted as a file path, and are
1880 inserted into the buffer using the Org-mode file syntax
1882 list ---- the results are interpreted as an Org-mode list.
1884 raw ----- results are added directly to the Org-mode file. This
1885 is a good option if you code block will output org-mode
1888 drawer -- results are added directly to the Org-mode file as with
1889 \"raw\", but are wrapped in a RESULTS drawer, allowing
1890 them to later be replaced or removed automatically.
1892 org ----- results are added inside of a \"#+BEGIN_SRC org\" block.
1893 They are not comma-escaped when inserted, but Org syntax
1894 here will be discarded when exporting the file.
1896 html ---- results are added inside of a #+BEGIN_HTML block. This
1897 is a good option if you code block will output html
1900 latex --- results are added inside of a #+BEGIN_LATEX block.
1901 This is a good option if you code block will output
1902 latex formatted text.
1904 code ---- the results are extracted in the syntax of the source
1905 code of the language being evaluated and are added
1906 inside of a #+BEGIN_SRC block with the source-code
1907 language set appropriately. Note this relies on the
1908 optional LANG argument."
1909 (if (stringp result
)
1911 (setq result
(org-no-properties result
))
1912 (when (member "file" result-params
)
1913 (setq result
(org-babel-result-to-file
1914 result
(when (assoc :file-desc
(nth 2 info
))
1915 (or (cdr (assoc :file-desc
(nth 2 info
)))
1917 (unless (listp result
) (setq result
(format "%S" result
))))
1918 (if (and result-params
(member "silent" result-params
))
1920 (message (replace-regexp-in-string "%" "%%" (format "%S" result
)))
1925 (when (or (org-babel-get-inline-src-block-matches)
1926 (org-babel-get-lob-one-liner-matches))
1927 (goto-char (match-end 0))
1928 (insert (if (listp result
) "\n" " "))
1930 (existing-result (unless inlinep
1931 (org-babel-where-is-src-block-result
1932 t info hash indent
)))
1934 (cdr (assoc :results_switches
(nth 2 info
))))
1935 (visible-beg (copy-marker (point-min)))
1936 (visible-end (copy-marker (point-max)))
1937 ;; When results exist outside of the current visible
1938 ;; region of the buffer, be sure to widen buffer to
1940 (outside-scope-p (and existing-result
1941 (or (> visible-beg existing-result
)
1942 (<= visible-end existing-result
))))
1944 (when (and (stringp result
) ; ensure results end in a newline
1946 (> (length result
) 0)
1947 (not (or (string-equal (substring result -
1) "\n")
1948 (string-equal (substring result -
1) "\r"))))
1949 (setq result
(concat result
"\n")))
1952 (when outside-scope-p
(widen))
1953 (if (not existing-result
)
1954 (setq beg
(or inlinep
(point)))
1955 (goto-char existing-result
)
1957 (re-search-forward "#" nil t
)
1958 (setq indent
(- (current-column) 1)))
1962 ((member "replace" result-params
)
1963 (delete-region (point) (org-babel-result-end)))
1964 ((member "append" result-params
)
1965 (goto-char (org-babel-result-end)) (setq beg
(point-marker)))
1966 ((member "prepend" result-params
)))) ; already there
1967 (setq results-switches
1968 (if results-switches
(concat " " results-switches
) ""))
1969 (let ((wrap (lambda (start finish
&optional no-escape
)
1970 (goto-char end
) (insert (concat finish
"\n"))
1971 (goto-char beg
) (insert (concat start
"\n"))
1973 (org-escape-code-in-region (min (point) end
) end
))
1974 (goto-char end
) (goto-char (point-at-eol))
1975 (setq end
(point-marker))))
1976 (proper-list-p (lambda (it) (and (listp it
) (null (cdr (last it
)))))))
1977 ;; insert results based on type
1979 ;; do nothing for an empty result
1981 ;; insert a list if preferred
1982 ((member "list" result-params
)
1985 (org-list-to-generic
1988 (lambda (el) (list nil
(if (stringp el
) el
(format "%S" el
))))
1989 (if (listp result
) result
(list result
))))
1990 '(:splicep nil
:istart
"- " :iend
"\n")))
1992 ;; assume the result is a table if it's not a string
1993 ((funcall proper-list-p result
)
1995 (insert (concat (orgtbl-to-orgtbl
1996 (if (or (eq 'hline
(car result
))
1997 (and (listp (car result
))
1998 (listp (cdr (car result
)))))
1999 result
(list result
))
2000 '(:fmt
(lambda (cell) (format "%s" cell
)))) "\n"))
2001 (goto-char beg
) (when (org-at-table-p) (org-table-align)))
2002 ((and (listp result
) (not (funcall proper-list-p result
)))
2003 (insert (format "%s\n" result
)))
2004 ((member "file" result-params
)
2005 (when inlinep
(goto-char inlinep
))
2007 (t (goto-char beg
) (insert result
)))
2008 (when (funcall proper-list-p result
) (goto-char (org-table-end)))
2009 (setq end
(point-marker))
2010 ;; possibly wrap result
2012 ((assoc :wrap
(nth 2 info
))
2013 (let ((name (or (cdr (assoc :wrap
(nth 2 info
))) "RESULTS")))
2014 (funcall wrap
(concat "#+BEGIN_" name
)
2015 (concat "#+END_" (car (org-split-string name
))))))
2016 ((member "html" result-params
)
2017 (funcall wrap
"#+BEGIN_HTML" "#+END_HTML"))
2018 ((member "latex" result-params
)
2019 (funcall wrap
"#+BEGIN_LaTeX" "#+END_LaTeX"))
2020 ((member "org" result-params
)
2021 (funcall wrap
"#+BEGIN_SRC org" "#+END_SRC"))
2022 ((member "code" result-params
)
2023 (funcall wrap
(format "#+BEGIN_SRC %s%s" (or lang
"none") results-switches
)
2025 ((member "raw" result-params
)
2026 (goto-char beg
) (if (org-at-table-p) (org-cycle)))
2027 ((or (member "drawer" result-params
)
2028 ;; Stay backward compatible with <7.9.2
2029 (member "wrap" result-params
))
2030 (funcall wrap
":RESULTS:" ":END:" 'no-escape
))
2031 ((and (not (funcall proper-list-p result
))
2032 (not (member "file" result-params
)))
2033 (org-babel-examplize-region beg end results-switches
)
2034 (setq end
(point)))))
2035 ;; possibly indent the results to match the #+results line
2036 (when (and (not inlinep
) (numberp indent
) indent
(> indent
0)
2037 ;; in this case `table-align' does the work for us
2038 (not (and (listp result
)
2039 (member "append" result-params
))))
2040 (indent-rigidly beg end indent
))
2042 (if (member "value" result-params
)
2043 (message "Code block returned no value.")
2044 (message "Code block produced no output."))
2045 (message "Code block evaluation complete.")))
2046 (when outside-scope-p
(narrow-to-region visible-beg visible-end
))
2047 (set-marker visible-beg nil
)
2048 (set-marker visible-end nil
))))))
2050 (defun org-babel-remove-result (&optional info
)
2051 "Remove the result of the current source block."
2053 (let ((location (org-babel-where-is-src-block-result nil info
)) start
)
2055 (setq start
(- location
1))
2057 (goto-char location
) (forward-line 1)
2058 (delete-region start
(org-babel-result-end))))))
2060 (defun org-babel-result-end ()
2061 "Return the point at the end of the current set of results."
2064 ((org-at-table-p) (progn (goto-char (org-table-end)) (point)))
2065 ((org-at-item-p) (let* ((struct (org-list-struct))
2066 (prvs (org-list-prevs-alist struct
)))
2067 (org-list-get-list-end (point-at-bol) struct prvs
)))
2068 ((let ((case-fold-search t
)) (looking-at "^\\([ \t]*\\):results:"))
2069 (progn (re-search-forward (concat "^" (match-string 1) ":END:"))
2070 (forward-char 1) (point)))
2072 (let ((case-fold-search t
))
2073 (if (looking-at (concat "[ \t]*#\\+begin_\\([^ \t\n\r]+\\)"))
2074 (progn (re-search-forward (concat "[ \t]*#\\+end_" (match-string 1))
2077 (while (looking-at "[ \t]*\\(: \\|\\[\\[\\)")
2081 (defun org-babel-result-to-file (result &optional description
)
2082 "Convert RESULT into an `org-mode' link with optional DESCRIPTION.
2083 If the `default-directory' is different from the containing
2084 file's directory then expand relative links."
2085 (when (stringp result
)
2086 (format "[[file:%s]%s]"
2087 (if (and default-directory
2089 (not (string= (expand-file-name default-directory
)
2091 (file-name-directory buffer-file-name
)))))
2092 (expand-file-name result default-directory
)
2094 (if description
(concat "[" description
"]") ""))))
2096 (defvar org-babel-capitalize-examplize-region-markers nil
2097 "Make true to capitalize begin/end example markers inserted by code blocks.")
2099 (defun org-babel-examplize-region (beg end
&optional results-switches
)
2100 "Comment out region using the inline '==' or ': ' org example quote."
2102 (let ((chars-between (lambda (b e
)
2103 (not (string-match "^[\\s]*$" (buffer-substring b e
)))))
2104 (maybe-cap (lambda (str) (if org-babel-capitalize-examplize-region-markers
2105 (upcase str
) str
))))
2106 (if (or (funcall chars-between
(save-excursion (goto-char beg
) (point-at-bol)) beg
)
2107 (funcall chars-between end
(save-excursion (goto-char end
) (point-at-eol))))
2110 (insert (format "=%s=" (prog1 (buffer-substring beg end
)
2111 (delete-region beg end
)))))
2112 (let ((size (count-lines beg end
)))
2114 (cond ((= size
0)) ; do nothing for an empty result
2115 ((< size org-babel-min-lines-for-block-output
)
2118 (beginning-of-line 1) (insert ": ") (forward-line 1)))
2121 (insert (if results-switches
2123 (funcall maybe-cap
"#+begin_example")
2125 (funcall maybe-cap
"#+begin_example\n")))
2126 (if (markerp end
) (goto-char end
) (forward-char (- end beg
)))
2127 (insert (funcall maybe-cap
"#+end_example\n")))))))))
2129 (defun org-babel-update-block-body (new-body)
2130 "Update the body of the current code block to NEW-BODY."
2131 (if (not (org-babel-where-is-src-block-head))
2132 (error "Not in a source block")
2134 (replace-match (concat (org-babel-trim new-body
) "\n") nil t nil
5))
2135 (indent-rigidly (match-beginning 5) (match-end 5) 2)))
2137 (defun org-babel-merge-params (&rest plists
)
2138 "Combine all parameter association lists in PLISTS.
2139 Later elements of PLISTS override the values of previous elements.
2140 This takes into account some special considerations for certain
2141 parameters when merging lists."
2142 (let* ((results-exclusive-groups
2143 (mapcar (lambda (group) (mapcar #'symbol-name group
))
2144 (cdr (assoc 'results org-babel-common-header-args-w-values
))))
2145 (exports-exclusive-groups
2146 (mapcar (lambda (group) (mapcar #'symbol-name group
))
2147 (cdr (assoc 'exports org-babel-common-header-args-w-values
))))
2149 (e-merge (lambda (exclusive-groups &rest result-params
)
2150 ;; maintain exclusivity of mutually exclusive parameters
2152 (mapc (lambda (new-params)
2153 (mapc (lambda (new-param)
2154 (mapc (lambda (exclusive-group)
2155 (when (member new-param exclusive-group
)
2156 (mapcar (lambda (excluded-param)
2163 (setq output
(org-uniquify
2164 (cons new-param output
))))
2168 params results exports tangle noweb cache vars shebang comments padline
)
2176 (let ((name (if (listp (cdr pair
))
2178 (and (string-match "^\\([^= \f\t\n\r\v]+\\)[ \t]*="
2180 (intern (match-string 1 (cdr pair
)))))))
2184 (if (member name
(mapcar #'car vars
))
2188 (unless (equal (car p
) name
) p
))
2191 (list (cons name pair
))))
2192 ;; if no name is given and we already have named variables
2193 ;; then assign to named variables in order
2194 (if (and vars
(nth variable-index vars
))
2195 (prog1 (setf (cddr (nth variable-index vars
))
2196 (concat (symbol-name
2197 (car (nth variable-index vars
)))
2199 (incf variable-index
))
2200 (error "Variable \"%s\" must be assigned a default value"
2203 (setq results
(funcall e-merge results-exclusive-groups
2206 (let ((r (cdr pair
)))
2207 (if (stringp r
) r
(eval r
)))))))
2210 (setq results
(funcall e-merge results-exclusive-groups
2212 (unless (or (member "both" exports
)
2213 (member "none" exports
)
2214 (member "code" exports
))
2215 (setq exports
(funcall e-merge exports-exclusive-groups
2216 exports
'("results"))))
2217 (setq params
(cons pair
(assq-delete-all (car pair
) params
)))))
2219 (setq exports
(funcall e-merge exports-exclusive-groups
2220 exports
(split-string (cdr pair
)))))
2221 (:tangle
;; take the latest -- always overwrite
2222 (setq tangle
(or (list (cdr pair
)) tangle
)))
2224 (setq noweb
(funcall e-merge
2225 '(("yes" "no" "tangle" "no-export"
2226 "strip-export" "eval"))
2228 (split-string (or (cdr pair
) "")))))
2230 (setq cache
(funcall e-merge
'(("yes" "no")) cache
2231 (split-string (or (cdr pair
) "")))))
2233 (setq padline
(funcall e-merge
'(("yes" "no")) padline
2234 (split-string (or (cdr pair
) "")))))
2235 (:shebang
;; take the latest -- always overwrite
2236 (setq shebang
(or (list (cdr pair
)) shebang
)))
2238 (setq comments
(funcall e-merge
'(("yes" "no")) comments
2239 (split-string (or (cdr pair
) "")))))
2240 (t ;; replace: this covers e.g. :session
2241 (setq params
(cons pair
(assq-delete-all (car pair
) params
))))))
2244 (setq vars
(reverse vars
))
2245 (while vars
(setq params
(cons (cons :var
(cddr (pop vars
))) params
)))
2248 (let ((key (intern (concat ":" (symbol-name hd
))))
2250 (setf params
(cons (cons key
(mapconcat 'identity val
" ")) params
))))
2251 '(results exports tangle noweb padline cache shebang comments
))
2254 (defvar org-babel-use-quick-and-dirty-noweb-expansion nil
2255 "Set to true to use regular expressions to expand noweb references.
2256 This results in much faster noweb reference expansion but does
2257 not properly allow code blocks to inherit the \":noweb-ref\"
2258 header argument from buffer or subtree wide properties.")
2260 (defun org-babel-noweb-p (params context
)
2261 "Check if PARAMS require expansion in CONTEXT.
2262 CONTEXT may be one of :tangle, :export or :eval."
2264 (intersect (lambda (as bs
)
2266 (if (member (car as
) bs
)
2268 (funcall intersect
(cdr as
) bs
))))))
2269 (funcall intersect
(case context
2270 (:tangle
'("yes" "tangle" "no-export" "strip-export"))
2271 (:eval
'("yes" "no-export" "strip-export" "eval"))
2273 (split-string (or (cdr (assoc :noweb params
)) "")))))
2275 (defun org-babel-expand-noweb-references (&optional info parent-buffer
)
2276 "Expand Noweb references in the body of the current source code block.
2278 For example the following reference would be replaced with the
2279 body of the source-code block named 'example-block'.
2283 Note that any text preceding the <<foo>> construct on a line will
2284 be interposed between the lines of the replacement text. So for
2285 example if <<foo>> is placed behind a comment, then the entire
2286 replacement text will also be commented.
2288 This function must be called from inside of the buffer containing
2289 the source-code block which holds BODY.
2291 In addition the following syntax can be used to insert the
2292 results of evaluating the source-code block named 'example-block'.
2296 Any optional arguments can be passed to example-block by placing
2297 the arguments inside the parenthesis following the convention
2298 defined by `org-babel-lob'. For example
2300 <<example-block(a=9)>>
2302 would set the value of argument \"a\" equal to \"9\". Note that
2303 these arguments are not evaluated in the current source-code
2304 block but are passed literally to the \"example-block\"."
2305 (let* ((parent-buffer (or parent-buffer
(current-buffer)))
2306 (info (or info
(org-babel-get-src-block-info)))
2309 (ob-nww-start org-babel-noweb-wrap-start
)
2310 (ob-nww-end org-babel-noweb-wrap-end
)
2311 (comment (string= "noweb" (cdr (assoc :comments
(nth 2 info
)))))
2312 (rx-prefix (concat "\\(" org-babel-src-name-regexp
"\\|"
2313 ":noweb-ref[ \t]+" "\\)"))
2315 (nb-add (lambda (text) (setq new-body
(concat new-body text
))))
2316 (c-wrap (lambda (text)
2318 (funcall (intern (concat lang
"-mode")))
2319 (comment-region (point) (progn (insert text
) (point)))
2320 (org-babel-trim (buffer-string)))))
2321 index source-name evaluate prefix blocks-in-buffer
)
2323 (org-set-local 'org-babel-noweb-wrap-start ob-nww-start
)
2324 (org-set-local 'org-babel-noweb-wrap-end ob-nww-end
)
2325 (insert body
) (goto-char (point-min))
2326 (setq index
(point))
2327 (while (and (re-search-forward (org-babel-noweb-wrap) nil t
))
2328 (save-match-data (setf source-name
(match-string 1)))
2329 (save-match-data (setq evaluate
(string-match "\(.*\)" source-name
)))
2332 (buffer-substring (match-beginning 0)
2334 (beginning-of-line 1) (point)))))
2335 ;; add interval to new-body (removing noweb reference)
2336 (goto-char (match-beginning 0))
2337 (funcall nb-add
(buffer-substring index
(point)))
2338 (goto-char (match-end 0))
2339 (setq index
(point))
2341 (with-current-buffer parent-buffer
2344 (mapconcat ;; interpose PREFIX between every line
2348 (let ((raw (org-babel-ref-resolve source-name
)))
2349 (if (stringp raw
) raw
(format "%S" raw
)))
2351 ;; retrieve from the library of babel
2352 (nth 2 (assoc (intern source-name
)
2353 org-babel-library-of-babel
))
2354 ;; return the contents of headlines literally
2356 (when (org-babel-ref-goto-headline-id source-name
)
2357 (org-babel-ref-headline-body)))
2358 ;; find the expansion of reference in this buffer
2359 (let ((rx (concat rx-prefix source-name
"[ \t\n]"))
2362 (goto-char (point-min))
2363 (if org-babel-use-quick-and-dirty-noweb-expansion
2364 (while (re-search-forward rx nil t
)
2365 (let* ((i (org-babel-get-src-block-info 'light
))
2366 (body (org-babel-expand-noweb-references i
))
2367 (sep (or (cdr (assoc :noweb-sep
(nth 2 i
)))
2371 (concat (funcall c-wrap
(car cs
)) "\n"
2373 (funcall c-wrap
(cadr cs
))))
2374 (org-babel-tangle-comment-links i
))
2376 (setq expansion
(cons sep
(cons full expansion
)))))
2377 (org-babel-map-src-blocks nil
2378 (let ((i (org-babel-get-src-block-info 'light
)))
2379 (when (equal (or (cdr (assoc :noweb-ref
(nth 2 i
)))
2382 (let* ((body (org-babel-expand-noweb-references i
))
2383 (sep (or (cdr (assoc :noweb-sep
(nth 2 i
)))
2387 (concat (funcall c-wrap
(car cs
)) "\n"
2389 (funcall c-wrap
(cadr cs
))))
2390 (org-babel-tangle-comment-links i
))
2393 (cons sep
(cons full expansion
)))))))))
2395 (mapconcat #'identity
(nreverse (cdr expansion
)) "")))
2396 ;; possibly raise an error if named block doesn't exist
2397 (if (member lang org-babel-noweb-error-langs
)
2399 (org-babel-noweb-wrap source-name
)
2400 "could not be resolved (see "
2401 "`org-babel-noweb-error-langs')"))
2403 "[\n\r]") (concat "\n" prefix
))))))
2404 (funcall nb-add
(buffer-substring index
(point-max))))
2407 (defun org-babel-script-escape (str &optional force
)
2408 "Safely convert tables into elisp lists."
2409 (let (in-single in-double out
)
2410 ((lambda (escaped) (condition-case nil
(org-babel-read escaped
) (error escaped
)))
2414 (or (and (string-equal "[" (substring str
0 1))
2415 (string-equal "]" (substring str -
1)))
2416 (and (string-equal "{" (substring str
0 1))
2417 (string-equal "}" (substring str -
1)))
2418 (and (string-equal "(" (substring str
0 1))
2419 (string-equal ")" (substring str -
1))))))
2429 (91 (if (or in-double in-single
) ; [
2432 (93 (if (or in-double in-single
) ; ]
2435 (123 (if (or in-double in-single
) ; {
2438 (125 (if (or in-double in-single
) ; }
2441 (44 (if (or in-double in-single
) ; ,
2442 (cons 44 out
) (cons 32 out
)))
2443 (39 (if in-double
; '
2445 (setq in-single
(not in-single
)) (cons 34 out
)))
2446 (34 (if in-single
; "
2447 (append (list 34 32) out
)
2448 (setq in-double
(not in-double
)) (cons 34 out
)))
2449 (t (cons ch out
)))))
2450 (string-to-list str
))
2451 (apply #'string
(reverse out
)))))
2454 (defun org-babel-read (cell &optional inhibit-lisp-eval
)
2455 "Convert the string value of CELL to a number if appropriate.
2456 Otherwise if cell looks like lisp (meaning it starts with a
2457 \"(\", \"'\", \"`\" or a \"[\") then read it as lisp, otherwise
2458 return it unmodified as a string. Optional argument NO-LISP-EVAL
2459 inhibits lisp evaluation for situations in which is it not
2461 (if (and (stringp cell
) (not (equal cell
"")))
2462 (or (org-babel-number-p cell
)
2463 (if (and (not inhibit-lisp-eval
)
2464 (member (substring cell
0 1) '("(" "'" "`" "[")))
2466 (if (string= (substring cell
0 1) "\"")
2468 (progn (set-text-properties 0 (length cell
) nil cell
) cell
))))
2471 (defun org-babel-number-p (string)
2472 "If STRING represents a number return its value."
2473 (if (and (string-match "^-?[0-9]*\\.?[0-9]*$" string
)
2474 (= (length (substring string
(match-beginning 0)
2477 (string-to-number string
)))
2479 (defun org-babel-import-elisp-from-file (file-name &optional separator
)
2480 "Read the results located at FILE-NAME into an elisp table.
2481 If the table is trivial, then return it as a scalar."
2483 (save-window-excursion
2487 (org-table-import file-name separator
)
2488 (delete-file file-name
)
2489 (setq result
(mapcar (lambda (row)
2490 (mapcar #'org-babel-string-read row
))
2491 (org-table-to-lisp))))
2492 (error (message "Error reading results: %s" err
) nil
)))
2493 (if (null (cdr result
)) ;; if result is trivial vector, then scalarize it
2494 (if (consp (car result
))
2495 (if (null (cdr (car result
)))
2501 (defun org-babel-string-read (cell)
2502 "Strip nested \"s from around strings."
2503 (org-babel-read (or (and (stringp cell
)
2504 (string-match "\\\"\\(.+\\)\\\"" cell
)
2505 (match-string 1 cell
))
2508 (defun org-babel-chomp (string &optional regexp
)
2509 "Strip trailing spaces and carriage returns from STRING.
2510 Default regexp used is \"[ \f\t\n\r\v]\" but can be
2511 overwritten by specifying a regexp as a second argument."
2512 (let ((regexp (or regexp
"[ \f\t\n\r\v]")))
2513 (while (and (> (length string
) 0)
2514 (string-match regexp
(substring string -
1)))
2515 (setq string
(substring string
0 -
1)))
2518 (defun org-babel-trim (string &optional regexp
)
2519 "Strip leading and trailing spaces and carriage returns from STRING.
2520 Like `org-babel-chomp' only it runs on both the front and back
2522 (org-babel-chomp (org-reverse-string
2523 (org-babel-chomp (org-reverse-string string
) regexp
))
2526 (defvar org-babel-org-babel-call-process-region-original nil
)
2527 (defun org-babel-tramp-handle-call-process-region
2528 (start end program
&optional delete buffer display
&rest args
)
2529 "Use Tramp to handle `call-process-region'.
2530 Fixes a bug in `tramp-handle-call-process-region'."
2531 (if (and (featurep 'tramp
) (file-remote-p default-directory
))
2532 (let ((tmpfile (tramp-compat-make-temp-file "")))
2533 (write-region start end tmpfile
)
2534 (when delete
(delete-region start end
))
2536 ;; (apply 'call-process program tmpfile buffer display args)
2538 (apply 'process-file program tmpfile buffer display args
)
2539 (delete-file tmpfile
)))
2540 ;; org-babel-call-process-region-original is the original emacs
2541 ;; definition. It is in scope from the let binding in
2542 ;; org-babel-execute-src-block
2543 (apply org-babel-call-process-region-original
2544 start end program delete buffer display args
)))
2546 (defun org-babel-local-file-name (file)
2547 "Return the local name component of FILE."
2548 (if (file-remote-p file
)
2550 (with-parsed-tramp-file-name file nil
2554 (defun org-babel-process-file-name (name &optional no-quote-p
)
2555 "Prepare NAME to be used in an external process.
2556 If NAME specifies a remote location, the remote portion of the
2557 name is removed, since in that case the process will be executing
2558 remotely. The file name is then processed by `expand-file-name'.
2559 Unless second argument NO-QUOTE-P is non-nil, the file name is
2560 additionally processed by `shell-quote-argument'"
2561 ((lambda (f) (if no-quote-p f
(shell-quote-argument f
)))
2562 (expand-file-name (org-babel-local-file-name name
))))
2564 (defvar org-babel-temporary-directory
)
2565 (unless (or noninteractive
(boundp 'org-babel-temporary-directory
))
2566 (defvar org-babel-temporary-directory
2567 (or (and (boundp 'org-babel-temporary-directory
)
2568 (file-exists-p org-babel-temporary-directory
)
2569 org-babel-temporary-directory
)
2570 (make-temp-file "babel-" t
))
2571 "Directory to hold temporary files created to execute code blocks.
2572 Used by `org-babel-temp-file'. This directory will be removed on
2575 (defmacro org-babel-result-cond
(result-params scalar-form
&rest table-forms
)
2576 "Call the code to parse raw string results according to RESULT-PARAMS."
2578 (debug (form form
&rest form
)))
2579 `(unless (member "none" ,result-params
)
2580 (if (or (member "scalar" ,result-params
)
2581 (member "verbatim" ,result-params
)
2582 (member "html" ,result-params
)
2583 (member "code" ,result-params
)
2584 (member "pp" ,result-params
)
2585 (and (member "output" ,result-params
)
2586 (not (member "table" ,result-params
))))
2590 (defun org-babel-temp-file (prefix &optional suffix
)
2591 "Create a temporary file in the `org-babel-temporary-directory'.
2592 Passes PREFIX and SUFFIX directly to `make-temp-file' with the
2593 value of `temporary-file-directory' temporarily set to the value
2594 of `org-babel-temporary-directory'."
2595 (let ((temporary-file-directory
2596 (if (file-remote-p default-directory
)
2597 (concat (file-remote-p default-directory
) "/tmp")
2598 (or (and (boundp 'org-babel-temporary-directory
)
2599 (file-exists-p org-babel-temporary-directory
)
2600 org-babel-temporary-directory
)
2601 temporary-file-directory
))))
2602 (make-temp-file prefix nil suffix
)))
2604 (defun org-babel-remove-temporary-directory ()
2605 "Remove `org-babel-temporary-directory' on Emacs shutdown."
2606 (when (and (boundp 'org-babel-temporary-directory
)
2607 (file-exists-p org-babel-temporary-directory
))
2608 ;; taken from `delete-directory' in files.el
2611 (mapc (lambda (file)
2612 ;; This test is equivalent to
2613 ;; (and (file-directory-p fn) (not (file-symlink-p fn)))
2614 ;; but more efficient
2615 (if (eq t
(car (file-attributes file
)))
2616 (delete-directory file
)
2617 (delete-file file
)))
2618 ;; We do not want to delete "." and "..".
2619 (directory-files org-babel-temporary-directory
'full
2620 "^\\([^.]\\|\\.\\([^.]\\|\\..\\)\\).*"))
2621 (delete-directory org-babel-temporary-directory
))
2623 (message "Failed to remove temporary Org-babel directory %s"
2624 (if (boundp 'org-babel-temporary-directory
)
2625 org-babel-temporary-directory
2626 "[directory not defined]"))))))
2628 (add-hook 'kill-emacs-hook
'org-babel-remove-temporary-directory
)
2633 ;; generated-autoload-file: "org-loaddefs.el"
2636 ;;; ob-core.el ends here