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 (defconst org-ts-regexp
"<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
40 "Regular expression for fast time stamp matching.")
41 (declare-function show-all
"outline" ())
42 (declare-function org-reduce
"org" (CL-FUNC CL-SEQ
&rest CL-KEYS
))
43 (declare-function org-mark-ring-push
"org" (&optional pos buffer
))
44 (declare-function tramp-compat-make-temp-file
"tramp-compat"
45 (filename &optional dir-flag
))
46 (declare-function tramp-dissect-file-name
"tramp" (name &optional nodefault
))
47 (declare-function tramp-file-name-user
"tramp" (vec))
48 (declare-function tramp-file-name-host
"tramp" (vec))
49 (declare-function with-parsed-tramp-file-name
"tramp" (filename var
&rest body
))
50 (declare-function org-icompleting-read
"org" (&rest args
))
51 (declare-function org-edit-src-code
"org-src"
52 (&optional context code edit-buffer-name quietp
))
53 (declare-function org-edit-src-exit
"org-src" (&optional context
))
54 (declare-function org-open-at-point
"org" (&optional in-emacs reference-buffer
))
55 (declare-function org-save-outline-visibility
"org-macs" (use-markers &rest body
))
56 (declare-function org-outline-overlay-data
"org" (&optional use-markers
))
57 (declare-function org-set-outline-overlay-data
"org" (data))
58 (declare-function org-narrow-to-subtree
"org" ())
59 (declare-function org-split-string
"org" (string &optional separators
))
60 (declare-function org-entry-get
"org"
61 (pom property
&optional inherit literal-nil
))
62 (declare-function org-make-options-regexp
"org" (kwds &optional extra
))
63 (declare-function org-do-remove-indentation
"org" (&optional n
))
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
))
97 (defgroup org-babel nil
98 "Code block evaluation and management in `org-mode' documents."
102 (defcustom org-confirm-babel-evaluate t
103 "Confirm before evaluation.
104 Require confirmation before interactively evaluating code
105 blocks in Org-mode buffers. The default value of this variable
106 is t, meaning confirmation is required for any code block
107 evaluation. This variable can be set to nil to inhibit any
108 future confirmation requests. This variable can also be set to a
109 function which takes two arguments the language of the code block
110 and the body of the code block. Such a function should then
111 return a non-nil value if the user should be prompted for
112 execution or nil if no prompt is required.
114 Warning: Disabling confirmation may result in accidental
115 evaluation of potentially harmful code. It may be advisable
116 remove code block execution from C-c C-c as further protection
117 against accidental code block evaluation. The
118 `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can be used to
119 remove code block execution from the C-c C-c keybinding."
122 :type
'(choice boolean function
))
123 ;; don't allow this variable to be changed through file settings
124 (put 'org-confirm-babel-evaluate
'safe-local-variable
(lambda (x) (eq x t
)))
126 (defcustom org-babel-no-eval-on-ctrl-c-ctrl-c nil
127 "Remove code block evaluation from the C-c C-c key binding."
132 (defcustom org-babel-results-keyword
"RESULTS"
133 "Keyword used to name results generated by code blocks.
134 Should be either RESULTS or NAME however any capitalization may
139 (defcustom org-babel-noweb-wrap-start
"<<"
140 "String used to begin a noweb reference in a code block.
141 See also `org-babel-noweb-wrap-end'."
145 (defcustom org-babel-noweb-wrap-end
">>"
146 "String used to end a noweb reference in a code block.
147 See also `org-babel-noweb-wrap-start'."
151 (defun org-babel-noweb-wrap (&optional regexp
)
152 (concat org-babel-noweb-wrap-start
153 (or regexp
"\\([^ \t\n].+?[^ \t]\\|[^ \t\n]\\)")
154 org-babel-noweb-wrap-end
))
156 (defvar org-babel-src-name-regexp
157 "^[ \t]*#\\+name:[ \t]*"
158 "Regular expression used to match a source name line.")
160 (defvar org-babel-multi-line-header-regexp
161 "^[ \t]*#\\+headers?:[ \t]*\\([^\n]*\\)$"
162 "Regular expression used to match multi-line header arguments.")
164 (defvar org-babel-src-name-w-name-regexp
165 (concat org-babel-src-name-regexp
167 org-babel-multi-line-header-regexp
169 "\\([^ ()\f\t\n\r\v]+\\)\\(\(\\(.*\\)\)\\|\\)")
170 "Regular expression matching source name lines with a name.")
172 (defvar org-babel-src-block-regexp
174 ;; (1) indentation (2) lang
175 "^\\([ \t]*\\)#\\+begin_src[ \t]+\\([^ \f\t\n\r\v]+\\)[ \t]*"
177 "\\([^\":\n]*\"[^\"\n*]*\"[^\":\n]*\\|[^\":\n]*\\)"
178 ;; (4) header arguments
181 "\\([^\000]*?\n\\)?[ \t]*#\\+end_src")
182 "Regexp used to identify code blocks.")
184 (defvar org-babel-inline-src-block-regexp
186 ;; (1) replacement target (2) lang
187 "\\(?:^\\|[^-[:alnum:]]\\)\\(src_\\([^ \f\t\n\r\v]+\\)"
188 ;; (3,4) (unused, headers)
189 "\\(\\|\\[\\(.*?\\)\\]\\)"
191 "{\\([^\f\n\r\v]+?\\)}\\)")
192 "Regexp used to identify inline src-blocks.")
194 (defun org-babel-get-header (params key
&optional others
)
195 "Select only header argument of type KEY from a list.
196 Optional argument OTHERS indicates that only the header that do
197 not match KEY should be returned."
200 (lambda (p) (when (funcall (if others
#'not
#'identity
) (eq (car p
) key
)) p
))
203 (defun org-babel-get-inline-src-block-matches()
204 "Set match data if within body of an inline source block.
205 Returns non-nil if match-data set"
206 (let ((src-at-0-p (save-excursion
207 (beginning-of-line 1)
208 (string= "src" (thing-at-point 'word
))))
209 (first-line-p (= 1 (line-number-at-pos)))
211 (let ((search-for (cond ((and src-at-0-p first-line-p
"src_"))
212 (first-line-p "[[:punct:] \t]src_")
213 (t "[[:punct:] \f\t\n\r\v]src_")))
214 (lower-limit (if first-line-p
216 (- (point-at-bol) 1))))
218 (when (or (and src-at-0-p
(bobp))
219 (and (re-search-forward "}" (point-at-eol) t
)
220 (re-search-backward search-for lower-limit t
)
222 (when (looking-at org-babel-inline-src-block-regexp
)
225 (defvar org-babel-inline-lob-one-liner-regexp
)
226 (defun org-babel-get-lob-one-liner-matches()
227 "Set match data if on line of an lob one liner.
228 Returns non-nil if match-data set"
230 (unless (= (point) (point-at-bol)) ;; move before inline block
231 (re-search-backward "[ \f\t\n\r\v]" nil t
))
232 (if (looking-at org-babel-inline-lob-one-liner-regexp
)
236 (defun org-babel-get-src-block-info (&optional light
)
237 "Get information on the current source block.
239 Optional argument LIGHT does not resolve remote variable
240 references; a process which could likely result in the execution
241 of other code blocks.
244 (language body header-arguments-alist switches name indent)."
245 (let ((case-fold-search t
) head info name indent
)
247 (if (setq head
(org-babel-where-is-src-block-head))
250 (setq info
(org-babel-parse-src-block-match))
251 (setq indent
(car (last info
)))
252 (setq info
(butlast info
))
253 (while (and (forward-line -
1)
254 (looking-at org-babel-multi-line-header-regexp
))
256 (org-babel-merge-params
258 (org-babel-parse-header-arguments (match-string 1)))))
259 (when (looking-at org-babel-src-name-w-name-regexp
)
260 (setq name
(org-no-properties (match-string 3)))
261 (when (and (match-string 5) (> (length (match-string 5)) 0))
262 (setf (nth 2 info
) ;; merge functional-syntax vars and header-args
263 (org-babel-merge-params
265 (lambda (ref) (cons :var ref
))
267 (lambda (var) ;; check that each variable is initialized
268 (if (string-match ".+=.+" var
)
271 "variable \"%s\"%s must be assigned a default value"
272 var
(if name
(format " in block \"%s\"" name
) ""))))
273 (org-babel-ref-split-args (match-string 5))))
275 ;; inline source block
276 (when (org-babel-get-inline-src-block-matches)
277 (setq info
(org-babel-parse-inline-src-block-match))))
278 ;; resolve variable references and add summary parameters
279 (when (and info
(not light
))
280 (setf (nth 2 info
) (org-babel-process-params (nth 2 info
))))
281 (when info
(append info
(list name indent
)))))
283 (defvar org-current-export-file
) ; dynamically bound
284 (defun org-babel-confirm-evaluate (info)
285 "Confirm evaluation of the code block INFO.
286 This behavior can be suppressed by setting the value of
287 `org-confirm-babel-evaluate' to nil, in which case all future
288 interactive code block evaluations will proceed without any
289 confirmation from the user.
291 Note disabling confirmation may result in accidental evaluation
292 of potentially harmful code."
293 (let* ((eval (or (cdr (assoc :eval
(nth 2 info
)))
294 (when (assoc :noeval
(nth 2 info
)) "no")))
295 (query (cond ((equal eval
"query") t
)
296 ((and (boundp 'org-current-export-file
)
297 org-current-export-file
298 (equal eval
"query-export")) t
)
299 ((functionp org-confirm-babel-evaluate
)
300 (funcall org-confirm-babel-evaluate
301 (nth 0 info
) (nth 1 info
)))
302 (t org-confirm-babel-evaluate
))))
303 (if (or (equal eval
"never") (equal eval
"no")
304 (and (boundp 'org-current-export-file
)
305 org-current-export-file
306 (or (equal eval
"no-export")
307 (equal eval
"never-export")))
310 (format "Evaluate this%scode block%son your system? "
311 (if info
(format " %s " (nth 0 info
)) " ")
313 (format " (%s) " (nth 4 info
)) " "))))))
314 (prog1 nil
(message "Evaluation %s"
315 (if (or (equal eval
"never") (equal eval
"no")
316 (equal eval
"no-export")
317 (equal eval
"never-export"))
318 "Disabled" "Aborted")))
322 (defun org-babel-execute-safely-maybe ()
323 (unless org-babel-no-eval-on-ctrl-c-ctrl-c
324 (org-babel-execute-maybe)))
326 (add-hook 'org-ctrl-c-ctrl-c-hook
'org-babel-execute-safely-maybe
)
329 (defun org-babel-execute-maybe ()
331 (or (org-babel-execute-src-block-maybe)
332 (org-babel-lob-execute-maybe)))
334 (defun org-babel-execute-src-block-maybe ()
335 "Conditionally execute a source block.
336 Detect if this is context for a Babel src-block and if so
337 then run `org-babel-execute-src-block'."
339 (let ((info (org-babel-get-src-block-info)))
341 (progn (org-babel-eval-wipe-error-buffer)
342 (org-babel-execute-src-block current-prefix-arg info
) t
) nil
)))
345 (defun org-babel-view-src-block-info ()
346 "Display information on the current source block.
347 This includes header arguments, language and name, and is largely
348 a window into the `org-babel-get-src-block-info' function."
350 (let ((info (org-babel-get-src-block-info 'light
))
351 (full (lambda (it) (> (length it
) 0)))
352 (printf (lambda (fmt &rest args
) (princ (apply #'format fmt args
)))))
354 (with-help-window (help-buffer)
355 (let ((name (nth 4 info
))
357 (switches (nth 3 info
))
358 (header-args (nth 2 info
)))
359 (when name
(funcall printf
"Name: %s\n" name
))
360 (when lang
(funcall printf
"Lang: %s\n" lang
))
361 (when (funcall full switches
) (funcall printf
"Switches: %s\n" switches
))
362 (funcall printf
"Header Arguments:\n")
363 (dolist (pair (sort header-args
364 (lambda (a b
) (string< (symbol-name (car a
))
365 (symbol-name (car b
))))))
366 (when (funcall full
(cdr pair
))
367 (funcall printf
"\t%S%s\t%s\n"
369 (if (> (length (format "%S" (car pair
))) 7) "" "\t")
373 (defun org-babel-expand-src-block-maybe ()
374 "Conditionally expand a source block.
375 Detect if this is context for a org-babel src-block and if so
376 then run `org-babel-expand-src-block'."
378 (let ((info (org-babel-get-src-block-info)))
380 (progn (org-babel-expand-src-block current-prefix-arg info
) t
)
384 (defun org-babel-load-in-session-maybe ()
385 "Conditionally load a source block in a session.
386 Detect if this is context for a org-babel src-block and if so
387 then run `org-babel-load-in-session'."
389 (let ((info (org-babel-get-src-block-info)))
391 (progn (org-babel-load-in-session current-prefix-arg info
) t
)
394 (add-hook 'org-metaup-hook
'org-babel-load-in-session-maybe
)
397 (defun org-babel-pop-to-session-maybe ()
398 "Conditionally pop to a session.
399 Detect if this is context for a org-babel src-block and if so
400 then run `org-babel-pop-to-session'."
402 (let ((info (org-babel-get-src-block-info)))
403 (if info
(progn (org-babel-pop-to-session current-prefix-arg info
) t
) nil
)))
405 (add-hook 'org-metadown-hook
'org-babel-pop-to-session-maybe
)
407 (defconst org-babel-common-header-args-w-values
408 '((cache .
((no yes
)))
410 (colnames .
((nil no yes
)))
411 (comments .
((no link yes org both noweb
)))
413 (eval .
((never query
)))
414 (exports .
((code results both none
)))
417 (hlines .
((no yes
)))
418 (mkdirp .
((yes no
)))
421 (noweb .
((yes no tangle no-export strip-export
)))
424 (padline .
((yes no
)))
425 (results .
((file list vector table scalar verbatim
)
426 (raw html latex org code pp drawer
)
427 (replace silent none append prepend
)
429 (rownames .
((no yes
)))
433 (tangle .
((tangle yes no
:any
)))
437 (defconst org-babel-header-arg-names
438 (mapcar #'car org-babel-common-header-args-w-values
)
439 "Common header arguments used by org-babel.
440 Note that individual languages may define their own language
441 specific header arguments as well.")
443 (defvar org-babel-default-header-args
444 '((:session .
"none") (:results .
"replace") (:exports .
"code")
445 (:cache .
"no") (:noweb .
"no") (:hlines .
"no") (:tangle .
"no")
446 (:padnewline .
"yes"))
447 "Default arguments to use when evaluating a source block.")
449 (defvar org-babel-default-inline-header-args
450 '((:session .
"none") (:results .
"replace") (:exports .
"results"))
451 "Default arguments to use when evaluating an inline source block.")
453 (defvar org-babel-data-names
'("tblname" "results" "name"))
455 (defvar org-babel-result-regexp
456 (concat "^[ \t]*#\\+"
457 (regexp-opt org-babel-data-names t
)
458 "\\(\\[\\([[:alnum:]]+\\)\\]\\|\\[\\[\\([[:alnum:]]+\\)\\]\\["
460 "\\]\\]\\)?\\:[ \t]*")
461 "Regular expression used to match result lines.
462 If the results are associated with a hash key then the hash will
463 be saved in the second match data.")
465 (defvar org-babel-result-w-name-regexp
466 (concat org-babel-result-regexp
467 "\\([^ ()\f\t\n\r\v]+\\)\\(\(\\(.*\\)\)\\|\\)"))
469 (defvar org-babel-min-lines-for-block-output
10
470 "The minimum number of lines for block output.
471 If number of lines of output is equal to or exceeds this
472 value, the output is placed in a #+begin_example...#+end_example
473 block. Otherwise the output is marked as literal by inserting
474 colons at the starts of the lines. This variable only takes
475 effect if the :results output option is in effect.")
477 (defvar org-babel-noweb-error-langs nil
478 "Languages for which Babel will raise literate programming errors.
479 List of languages for which errors should be raised when the
480 source code block satisfying a noweb reference in this language
481 can not be resolved.")
483 (defvar org-babel-hash-show
4
484 "Number of initial characters to show of a hidden results hash.")
486 (defvar org-babel-hash-show-time nil
487 "When not nil show the time the code block was evaluated in the result hash.")
489 (defvar org-babel-after-execute-hook nil
490 "Hook for functions to be called after `org-babel-execute-src-block'")
492 (defun org-babel-named-src-block-regexp-for-name (name)
493 "This generates a regexp used to match a src block named NAME."
494 (concat org-babel-src-name-regexp
(regexp-quote name
)
495 "[ \t(]*[\r\n]\\(?:^#.*[\r\n]\\)*"
496 (substring org-babel-src-block-regexp
1)))
498 (defun org-babel-named-data-regexp-for-name (name)
499 "This generates a regexp used to match data named NAME."
500 (concat org-babel-result-regexp
(regexp-quote name
) "\\([ \t]\\|$\\)"))
503 (defvar call-process-region
)
506 (defun org-babel-execute-src-block (&optional arg info params
)
507 "Execute the current source code block.
508 Insert the results of execution into the buffer. Source code
509 execution and the collection and formatting of results can be
510 controlled through a variety of header arguments.
512 With prefix argument ARG, force re-execution even if an existing
513 result cached in the buffer would otherwise have been returned.
515 Optionally supply a value for INFO in the form returned by
516 `org-babel-get-src-block-info'.
518 Optionally supply a value for PARAMS which will be merged with
519 the header arguments specified at the front of the source code
522 (let ((info (or info
(org-babel-get-src-block-info))))
523 (when (org-babel-confirm-evaluate
525 (setf (nth 2 i
) (org-babel-merge-params (nth 2 info
) params
))
527 (let* ((lang (nth 0 info
))
529 (org-babel-process-params
530 (org-babel-merge-params (nth 2 info
) params
))
532 (cache?
(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?
(org-babel-sha1-hash info
)))
536 (old-hash (when cache?
(org-babel-current-result-hash)))
537 (body (setf (nth 1 info
)
538 (if (org-babel-noweb-p params
:eval
)
539 (org-babel-expand-noweb-references info
)
541 (dir (cdr (assoc :dir params
)))
543 (or (and dir
(file-name-as-directory (expand-file-name dir
)))
545 (org-babel-call-process-region-original
546 (if (boundp 'org-babel-call-process-region-original
)
547 org-babel-call-process-region-original
548 (symbol-function 'call-process-region
)))
549 (indent (car (last info
)))
552 (let ((call-process-region
554 (apply 'org-babel-tramp-handle-call-process-region args
))))
555 (let ((lang-check (lambda (f)
556 (let ((f (intern (concat "org-babel-execute:" f
))))
557 (when (fboundp f
) f
)))))
559 (or (funcall lang-check lang
)
560 (funcall lang-check
(symbol-name
561 (cdr (assoc lang org-src-lang-modes
))))
562 (error "No org-babel-execute function for %s!" lang
))))
563 (if (and (not arg
) new-hash
(equal new-hash old-hash
))
564 (save-excursion ;; return cached result
565 (goto-char (org-babel-where-is-src-block-result nil info
))
566 (end-of-line 1) (forward-char 1)
567 (setq result
(org-babel-read-result))
568 (message (replace-regexp-in-string
569 "%" "%%" (format "%S" result
))) result
)
570 (message "executing %s code block%s..."
572 (if (nth 4 info
) (format " (%s)" (nth 4 info
)) ""))
573 (if (member "none" result-params
)
575 (funcall cmd body params
)
576 (message "result silenced"))
579 (if (and (eq (cdr (assoc :result-type params
)) 'value
)
580 (or (member "vector" result-params
)
581 (member "table" result-params
))
582 (not (listp result
)))
583 (list (list result
)) result
))
584 (funcall cmd body params
)))
585 ;; if non-empty result and :file then write to :file
586 (when (cdr (assoc :file params
))
588 (with-temp-file (cdr (assoc :file params
))
590 (org-babel-format-result
591 result
(cdr (assoc :sep
(nth 2 info
)))))))
592 (setq result
(cdr (assoc :file params
))))
593 (org-babel-insert-result
594 result result-params info new-hash indent lang
)
595 (run-hooks 'org-babel-after-execute-hook
)
598 (setq call-process-region
'org-babel-call-process-region-original
))))))
600 (defun org-babel-expand-body:generic
(body params
&optional var-lines
)
601 "Expand BODY with PARAMS.
602 Expand a block of code with org-babel according to its header
603 arguments. This generic implementation of body expansion is
604 called for languages which have not defined their own specific
605 org-babel-expand-body:lang function."
606 (mapconcat #'identity
(append var-lines
(list body
)) "\n"))
609 (defun org-babel-expand-src-block (&optional arg info params
)
610 "Expand the current source code block.
611 Expand according to the source code block's header
612 arguments and pop open the results in a preview buffer."
614 (let* ((info (or info
(org-babel-get-src-block-info)))
616 (params (setf (nth 2 info
)
617 (sort (org-babel-merge-params (nth 2 info
) params
)
618 (lambda (el1 el2
) (string< (symbol-name (car el1
))
619 (symbol-name (car el2
)))))))
620 (body (setf (nth 1 info
)
621 (if (org-babel-noweb-p params
:eval
)
622 (org-babel-expand-noweb-references info
) (nth 1 info
))))
623 (expand-cmd (intern (concat "org-babel-expand-body:" lang
)))
624 (assignments-cmd (intern (concat "org-babel-variable-assignments:"
627 (if (fboundp expand-cmd
) (funcall expand-cmd body params
)
628 (org-babel-expand-body:generic
629 body params
(and (fboundp assignments-cmd
)
630 (funcall assignments-cmd params
))))))
632 nil expanded
(concat "*Org-Babel Preview " (buffer-name) "[ " lang
" ]*"))))
634 (defun org-babel-edit-distance (s1 s2
)
635 "Return the edit (levenshtein) distance between strings S1 S2."
636 (let* ((l1 (length s1
))
638 (dist (vconcat (mapcar (lambda (_) (make-vector (1+ l2
) nil
))
639 (number-sequence 1 (1+ l1
)))))
640 (in (lambda (i j
) (aref (aref dist i
) j
))))
641 (setf (aref (aref dist
0) 0) 0)
642 (dolist (j (number-sequence 1 l2
))
643 (setf (aref (aref dist
0) j
) j
))
644 (dolist (i (number-sequence 1 l1
))
645 (setf (aref (aref dist i
) 0) i
)
646 (dolist (j (number-sequence 1 l2
))
647 (setf (aref (aref dist i
) j
)
649 (1+ (funcall in
(1- i
) j
))
650 (1+ (funcall in i
(1- j
)))
651 (+ (if (equal (aref s1
(1- i
)) (aref s2
(1- j
))) 0 1)
652 (funcall in
(1- i
) (1- j
)))))))
655 (defun org-babel-combine-header-arg-lists (original &rest others
)
656 "Combine a number of lists of header argument names and arguments."
657 (let ((results (copy-sequence original
)))
658 (dolist (new-list others
)
659 (dolist (arg-pair new-list
)
660 (let ((header (car arg-pair
))
661 (args (cdr arg-pair
)))
663 (cons arg-pair
(org-remove-if
664 (lambda (pair) (equal header
(car pair
)))
669 (defun org-babel-check-src-block ()
670 "Check for misspelled header arguments in the current code block."
672 ;; TODO: report malformed code block
673 ;; TODO: report incompatible combinations of header arguments
674 ;; TODO: report uninitialized variables
675 (let ((too-close 2) ;; <- control closeness to report potential match
676 (names (mapcar #'symbol-name org-babel-header-arg-names
)))
677 (dolist (header (mapcar (lambda (arg) (substring (symbol-name (car arg
)) 1))
678 (and (org-babel-where-is-src-block-head)
679 (org-babel-parse-header-arguments
681 (match-string 4))))))
683 (when (and (not (string= header name
))
684 (<= (org-babel-edit-distance header name
) too-close
)
685 (not (member header names
)))
686 (error "Supplied header \"%S\" is suspiciously close to \"%S\""
688 (message "No suspicious header arguments found.")))
691 (defun org-babel-insert-header-arg ()
692 "Insert a header argument selecting from lists of common args and values."
694 (let* ((lang (car (org-babel-get-src-block-info 'light
)))
695 (lang-headers (intern (concat "org-babel-header-args:" lang
)))
696 (headers (org-babel-combine-header-arg-lists
697 org-babel-common-header-args-w-values
698 (if (boundp lang-headers
) (eval lang-headers
) nil
)))
699 (arg (org-icompleting-read
702 (lambda (header-spec) (symbol-name (car header-spec
)))
705 (let ((vals (cdr (assoc (intern arg
) headers
))))
711 (read-from-minibuffer "value: "))
715 (let ((arg (org-icompleting-read
717 (cons "default" (mapcar #'symbol-name group
)))))
718 (if (and arg
(not (string= "default" arg
)))
723 ;; Add support for completing-read insertion of header arguments after ":"
724 (defun org-babel-header-arg-expand ()
725 "Call `org-babel-enter-header-arg-w-completion' in appropriate contexts."
726 (when (and (equal (char-before) ?\
:) (org-babel-where-is-src-block-head))
727 (org-babel-enter-header-arg-w-completion (match-string 2))))
729 (defun org-babel-enter-header-arg-w-completion (&optional lang
)
730 "Insert header argument appropriate for LANG with completion."
731 (let* ((lang-headers-var (intern (concat "org-babel-header-args:" lang
)))
732 (lang-headers (when (boundp lang-headers-var
) (eval lang-headers-var
)))
733 (headers-w-values (org-babel-combine-header-arg-lists
734 org-babel-common-header-args-w-values lang-headers
))
735 (headers (mapcar #'symbol-name
(mapcar #'car headers-w-values
)))
736 (header (org-completing-read "Header Arg: " headers
))
737 (args (cdr (assoc (intern header
) headers-w-values
)))
738 (arg (when (and args
(listp args
))
740 (format "%s: " header
)
741 (mapcar #'symbol-name
(apply #'append args
))))))
742 (insert (concat header
" " (or arg
"")))
745 (add-hook 'org-tab-first-hook
'org-babel-header-arg-expand
)
748 (defun org-babel-load-in-session (&optional arg info
)
749 "Load the body of the current source-code block.
750 Evaluate the header arguments for the source block before
751 entering the session. After loading the body this pops open the
754 (let* ((info (or info
(org-babel-get-src-block-info)))
756 (params (nth 2 info
))
757 (body (setf (nth 1 info
)
758 (if (org-babel-noweb-p params
:eval
)
759 (org-babel-expand-noweb-references info
)
761 (session (cdr (assoc :session params
)))
762 (dir (cdr (assoc :dir params
)))
764 (or (and dir
(file-name-as-directory dir
)) default-directory
))
765 (cmd (intern (concat "org-babel-load-session:" lang
))))
766 (unless (fboundp cmd
)
767 (error "No org-babel-load-session function for %s!" lang
))
768 (pop-to-buffer (funcall cmd session body params
))
772 (defun org-babel-initiate-session (&optional arg info
)
773 "Initiate session for current code block.
774 If called with a prefix argument then resolve any variable
775 references in the header arguments and assign these variables in
776 the session. Copy the body of the code block to the kill ring."
778 (let* ((info (or info
(org-babel-get-src-block-info (not arg
))))
781 (params (nth 2 info
))
782 (session (cdr (assoc :session params
)))
783 (dir (cdr (assoc :dir params
)))
785 (or (and dir
(file-name-as-directory dir
)) default-directory
))
786 (init-cmd (intern (format "org-babel-%s-initiate-session" lang
)))
787 (prep-cmd (intern (concat "org-babel-prep-session:" lang
))))
788 (if (and (stringp session
) (string= session
"none"))
789 (error "This block is not using a session!"))
790 (unless (fboundp init-cmd
)
791 (error "No org-babel-initiate-session function for %s!" lang
))
792 (with-temp-buffer (insert (org-babel-trim body
))
793 (copy-region-as-kill (point-min) (point-max)))
795 (unless (fboundp prep-cmd
)
796 (error "No org-babel-prep-session function for %s!" lang
))
797 (funcall prep-cmd session params
))
798 (funcall init-cmd session params
)))
801 (defun org-babel-switch-to-session (&optional arg info
)
802 "Switch to the session of the current code block.
803 Uses `org-babel-initiate-session' to start the session. If called
804 with a prefix argument then this is passed on to
805 `org-babel-initiate-session'."
807 (pop-to-buffer (org-babel-initiate-session arg info
))
810 (defalias 'org-babel-pop-to-session
'org-babel-switch-to-session
)
813 (defun org-babel-switch-to-session-with-code (&optional arg info
)
814 "Switch to code buffer and display session."
818 (let ((other-window-buffer (window-buffer (next-window))))
819 (set-window-buffer (next-window) (current-buffer))
820 (set-window-buffer (selected-window) other-window-buffer
))
822 (info (org-babel-get-src-block-info))
823 (org-src-window-setup 'reorganize-frame
))
825 (org-babel-switch-to-session arg info
))
827 (funcall swap-windows
)))
829 (defmacro org-babel-do-in-edit-buffer
(&rest body
)
830 "Evaluate BODY in edit buffer if there is a code block at point.
831 Return t if a code block was found at point, nil otherwise."
832 `(let ((org-src-window-setup 'switch-invisibly
))
833 (when (and (org-babel-where-is-src-block-head)
834 (org-edit-src-code nil nil nil
))
835 (unwind-protect (progn ,@body
)
836 (if (org-bound-and-true-p org-edit-src-from-org-mode
)
837 (org-edit-src-exit)))
839 (def-edebug-spec org-babel-do-in-edit-buffer
(body))
841 (defun org-babel-do-key-sequence-in-edit-buffer (key)
842 "Read key sequence and execute the command in edit buffer.
843 Enter a key sequence to be executed in the language major-mode
844 edit buffer. For example, TAB will alter the contents of the
845 Org-mode code block according to the effect of TAB in the
846 language major-mode buffer. For languages that support
847 interactive sessions, this can be used to send code from the Org
848 buffer to the session for evaluation using the native major-mode
849 evaluation mechanisms."
850 (interactive "kEnter key-sequence to execute in edit buffer: ")
851 (org-babel-do-in-edit-buffer
853 (key-binding (or key
(read-key-sequence nil
))))))
855 (defvar org-bracket-link-regexp
)
858 (defun org-babel-open-src-block-result (&optional re-run
)
859 "If `point' is on a src block then open the results of the
860 source code block, otherwise return nil. With optional prefix
861 argument RE-RUN the source-code block is evaluated even if
862 results already exist."
864 (let ((info (org-babel-get-src-block-info)))
867 ;; go to the results, if there aren't any then run the block
868 (goto-char (or (and (not re-run
) (org-babel-where-is-src-block-result))
869 (progn (org-babel-execute-src-block)
870 (org-babel-where-is-src-block-result))))
872 (while (looking-at "[\n\r\t\f ]") (forward-char 1))
874 (if (looking-at org-bracket-link-regexp
)
877 (let ((r (org-babel-format-result
878 (org-babel-read-result) (cdr (assoc :sep
(nth 2 info
))))))
879 (pop-to-buffer (get-buffer-create "*Org-Babel Results*"))
880 (delete-region (point-min) (point-max))
885 (defmacro org-babel-map-src-blocks
(file &rest body
)
886 "Evaluate BODY forms on each source-block in FILE.
887 If FILE is nil evaluate BODY forms on source blocks in current
888 buffer. During evaluation of BODY the following local variables
889 are set relative to the currently matched code block.
891 full-block ------- string holding the entirety of the code block
892 beg-block -------- point at the beginning of the code block
893 end-block -------- point at the end of the matched code block
894 lang ------------- string holding the language of the code block
895 beg-lang --------- point at the beginning of the lang
896 end-lang --------- point at the end of the lang
897 switches --------- string holding the switches
898 beg-switches ----- point at the beginning of the switches
899 end-switches ----- point at the end of the switches
900 header-args ------ string holding the header-args
901 beg-header-args -- point at the beginning of the header-args
902 end-header-args -- point at the end of the header-args
903 body ------------- string holding the body of the code block
904 beg-body --------- point at the beginning of the body
905 end-body --------- point at the end of the body"
907 (let ((tempvar (make-symbol "file")))
908 `(let* ((,tempvar
,file
)
909 (visited-p (or (null ,tempvar
)
910 (get-file-buffer (expand-file-name ,tempvar
))))
911 (point (point)) to-be-removed
)
912 (save-window-excursion
913 (when ,tempvar
(find-file ,tempvar
))
914 (setq to-be-removed
(current-buffer))
915 (goto-char (point-min))
916 (while (re-search-forward org-babel-src-block-regexp nil t
)
917 (goto-char (match-beginning 0))
918 (let ((full-block (match-string 0))
919 (beg-block (match-beginning 0))
920 (end-block (match-end 0))
921 (lang (match-string 2))
922 (beg-lang (match-beginning 2))
923 (end-lang (match-end 2))
924 (switches (match-string 3))
925 (beg-switches (match-beginning 3))
926 (end-switches (match-end 3))
927 (header-args (match-string 4))
928 (beg-header-args (match-beginning 4))
929 (end-header-args (match-end 4))
930 (body (match-string 5))
931 (beg-body (match-beginning 5))
932 (end-body (match-end 5)))
934 (goto-char end-block
))))
935 (unless visited-p
(kill-buffer to-be-removed
))
937 (def-edebug-spec org-babel-map-src-blocks
(form body
))
940 (defmacro org-babel-map-inline-src-blocks
(file &rest body
)
941 "Evaluate BODY forms on each inline source-block in FILE.
942 If FILE is nil evaluate BODY forms on source blocks in current
945 (let ((tempvar (make-symbol "file")))
946 `(let* ((,tempvar
,file
)
947 (visited-p (or (null ,tempvar
)
948 (get-file-buffer (expand-file-name ,tempvar
))))
949 (point (point)) to-be-removed
)
950 (save-window-excursion
951 (when ,tempvar
(find-file ,tempvar
))
952 (setq to-be-removed
(current-buffer))
953 (goto-char (point-min))
954 (while (re-search-forward org-babel-inline-src-block-regexp nil t
)
955 (goto-char (match-beginning 1))
956 (save-match-data ,@body
)
957 (goto-char (match-end 0))))
958 (unless visited-p
(kill-buffer to-be-removed
))
960 (def-edebug-spec org-babel-map-inline-src-blocks
(form body
))
962 (defvar org-babel-lob-one-liner-regexp
)
965 (defmacro org-babel-map-call-lines
(file &rest body
)
966 "Evaluate BODY forms on each call line in FILE.
967 If FILE is nil evaluate BODY forms on source blocks in current
970 (let ((tempvar (make-symbol "file")))
971 `(let* ((,tempvar
,file
)
972 (visited-p (or (null ,tempvar
)
973 (get-file-buffer (expand-file-name ,tempvar
))))
974 (point (point)) to-be-removed
)
975 (save-window-excursion
976 (when ,tempvar
(find-file ,tempvar
))
977 (setq to-be-removed
(current-buffer))
978 (goto-char (point-min))
979 (while (re-search-forward org-babel-lob-one-liner-regexp nil t
)
980 (goto-char (match-beginning 1))
981 (save-match-data ,@body
)
982 (goto-char (match-end 0))))
983 (unless visited-p
(kill-buffer to-be-removed
))
985 (def-edebug-spec org-babel-map-call-lines
(form body
))
988 (defmacro org-babel-map-executables
(file &rest body
)
990 (let ((tempvar (make-symbol "file"))
991 (rx (make-symbol "rx")))
992 `(let* ((,tempvar
,file
)
993 (,rx
(concat "\\(" org-babel-src-block-regexp
994 "\\|" org-babel-inline-src-block-regexp
995 "\\|" org-babel-lob-one-liner-regexp
"\\)"))
996 (visited-p (or (null ,tempvar
)
997 (get-file-buffer (expand-file-name ,tempvar
))))
998 (point (point)) to-be-removed
)
999 (save-window-excursion
1000 (when ,tempvar
(find-file ,tempvar
))
1001 (setq to-be-removed
(current-buffer))
1002 (goto-char (point-min))
1003 (while (re-search-forward ,rx nil t
)
1004 (goto-char (match-beginning 1))
1005 (when (looking-at org-babel-inline-src-block-regexp
)(forward-char 1))
1006 (save-match-data ,@body
)
1007 (goto-char (match-end 0))))
1008 (unless visited-p
(kill-buffer to-be-removed
))
1009 (goto-char point
))))
1010 (def-edebug-spec org-babel-map-executables
(form body
))
1013 (defun org-babel-execute-buffer (&optional arg
)
1014 "Execute source code blocks in a buffer.
1015 Call `org-babel-execute-src-block' on every source block in
1016 the current buffer."
1018 (org-babel-eval-wipe-error-buffer)
1019 (org-save-outline-visibility t
1020 (org-babel-map-executables nil
1021 (if (looking-at org-babel-lob-one-liner-regexp
)
1022 (org-babel-lob-execute-maybe)
1023 (org-babel-execute-src-block arg
)))))
1026 (defun org-babel-execute-subtree (&optional arg
)
1027 "Execute source code blocks in a subtree.
1028 Call `org-babel-execute-src-block' on every source block in
1029 the current subtree."
1033 (org-narrow-to-subtree)
1034 (org-babel-execute-buffer arg
)
1038 (defun org-babel-sha1-hash (&optional info
)
1039 "Generate an sha1 hash based on the value of info."
1041 (let ((print-level nil
)
1042 (info (or info
(org-babel-get-src-block-info))))
1044 (sort (copy-sequence (nth 2 info
))
1045 (lambda (a b
) (string< (car a
) (car b
)))))
1046 (let* ((rm (lambda (lst)
1047 (dolist (p '("replace" "silent" "none"
1048 "append" "prepend"))
1049 (setq lst
(remove p lst
)))
1052 (let ((v (if (and (listp (cdr arg
)) (null (cddr arg
)))
1053 (copy-sequence (cdr arg
))
1055 (when (and v
(not (and (sequencep v
)
1059 ((and (listp v
) ; lists are sorted
1060 (member (car arg
) '(:result-params
)))
1061 (sort (funcall rm v
) #'string
<))
1062 ((and (stringp v
) ; strings are sorted
1063 (member (car arg
) '(:results
:exports
)))
1064 (mapconcat #'identity
(sort (funcall rm
(split-string v
))
1068 (when (org-called-interactively-p 'interactive
) (message hash
)) hash
)
1069 (let ((it (format "%s-%s"
1072 (delq nil
(mapcar (lambda (arg)
1073 (let ((normalized (funcall norm arg
)))
1075 (format "%S" normalized
))))
1080 (defun org-babel-current-result-hash ()
1081 "Return the current in-buffer hash."
1082 (org-babel-where-is-src-block-result)
1083 (org-no-properties (or (match-string 3) (match-string 4))))
1085 (defun org-babel-set-current-result-hash (hash)
1086 "Set the current in-buffer hash to HASH."
1087 (org-babel-where-is-src-block-result)
1088 (save-excursion (goto-char (match-beginning 3))
1089 ;; (mapc #'delete-overlay (overlays-at (point)))
1090 (replace-match hash nil nil nil
3)
1091 (org-babel-hide-hash)))
1093 (defun org-babel-hide-hash ()
1094 "Hide the hash in the current results line.
1095 Only the initial `org-babel-hash-show' characters of the hash
1096 will remain visible."
1097 (add-to-invisibility-spec '(org-babel-hide-hash . t
))
1099 (when (and (re-search-forward org-babel-result-regexp nil t
)
1101 (let* ((start (match-beginning 3))
1102 (hide-start (+ org-babel-hash-show start
))
1104 (hash (match-string 3))
1106 (setq ov1
(make-overlay start hide-start
))
1107 (setq ov2
(make-overlay hide-start end
))
1108 (overlay-put ov2
'invisible
'org-babel-hide-hash
)
1109 (overlay-put ov1
'babel-hash hash
)))))
1111 (defun org-babel-hide-all-hashes ()
1112 "Hide the hash in the current buffer.
1113 Only the initial `org-babel-hash-show' characters of each hash
1114 will remain visible. This function should be called as part of
1115 the `org-mode-hook'."
1117 (while (and (not org-babel-hash-show-time
)
1118 (re-search-forward org-babel-result-regexp nil t
))
1119 (goto-char (match-beginning 0))
1120 (org-babel-hide-hash)
1121 (goto-char (match-end 0)))))
1122 (add-hook 'org-mode-hook
'org-babel-hide-all-hashes
)
1124 (defun org-babel-hash-at-point (&optional point
)
1125 "Return the value of the hash at POINT.
1126 The hash is also added as the last element of the kill ring.
1127 This can be called with C-c C-c."
1129 (let ((hash (car (delq nil
(mapcar
1130 (lambda (ol) (overlay-get ol
'babel-hash
))
1131 (overlays-at (or point
(point))))))))
1132 (when hash
(kill-new hash
) (message hash
))))
1133 (add-hook 'org-ctrl-c-ctrl-c-hook
'org-babel-hash-at-point
)
1135 (defun org-babel-result-hide-spec ()
1136 "Hide portions of results lines.
1137 Add `org-babel-hide-result' as an invisibility spec for hiding
1138 portions of results lines."
1139 (add-to-invisibility-spec '(org-babel-hide-result . t
)))
1140 (add-hook 'org-mode-hook
'org-babel-result-hide-spec
)
1142 (defvar org-babel-hide-result-overlays nil
1143 "Overlays hiding results.")
1145 (defun org-babel-result-hide-all ()
1146 "Fold all results in the current buffer."
1148 (org-babel-show-result-all)
1150 (while (re-search-forward org-babel-result-regexp nil t
)
1151 (save-excursion (goto-char (match-beginning 0))
1152 (org-babel-hide-result-toggle-maybe)))))
1154 (defun org-babel-show-result-all ()
1155 "Unfold all results in the current buffer."
1156 (mapc 'delete-overlay org-babel-hide-result-overlays
)
1157 (setq org-babel-hide-result-overlays nil
))
1160 (defun org-babel-hide-result-toggle-maybe ()
1161 "Toggle visibility of result at point."
1163 (let ((case-fold-search t
))
1165 (beginning-of-line 1)
1166 (looking-at org-babel-result-regexp
))
1167 (progn (org-babel-hide-result-toggle)
1168 t
) ;; to signal that we took action
1169 nil
))) ;; to signal that we did not
1171 (defun org-babel-hide-result-toggle (&optional force
)
1172 "Toggle the visibility of the current result."
1176 (if (re-search-forward org-babel-result-regexp nil t
)
1177 (let ((start (progn (beginning-of-line 2) (- (point) 1)))
1179 (while (looking-at org-babel-multi-line-header-regexp
)
1181 (goto-char (- (org-babel-result-end) 1)) (point)))
1183 (if (memq t
(mapcar (lambda (overlay)
1184 (eq (overlay-get overlay
'invisible
)
1185 'org-babel-hide-result
))
1186 (overlays-at start
)))
1187 (if (or (not force
) (eq force
'off
))
1189 (when (member ov org-babel-hide-result-overlays
)
1190 (setq org-babel-hide-result-overlays
1191 (delq ov org-babel-hide-result-overlays
)))
1192 (when (eq (overlay-get ov
'invisible
)
1193 'org-babel-hide-result
)
1194 (delete-overlay ov
)))
1195 (overlays-at start
)))
1196 (setq ov
(make-overlay start end
))
1197 (overlay-put ov
'invisible
'org-babel-hide-result
)
1198 ;; make the block accessible to isearch
1200 ov
'isearch-open-invisible
1202 (when (member ov org-babel-hide-result-overlays
)
1203 (setq org-babel-hide-result-overlays
1204 (delq ov org-babel-hide-result-overlays
)))
1205 (when (eq (overlay-get ov
'invisible
)
1206 'org-babel-hide-result
)
1207 (delete-overlay ov
))))
1208 (push ov org-babel-hide-result-overlays
)))
1209 (error "Not looking at a result line"))))
1211 ;; org-tab-after-check-for-cycling-hook
1212 (add-hook 'org-tab-first-hook
'org-babel-hide-result-toggle-maybe
)
1213 ;; Remove overlays when changing major mode
1214 (add-hook 'org-mode-hook
1215 (lambda () (org-add-hook 'change-major-mode-hook
1216 'org-babel-show-result-all
'append
'local
)))
1218 (defvar org-file-properties
)
1219 (defun org-babel-params-from-properties (&optional lang
)
1220 "Retrieve parameters specified as properties.
1221 Return an association list of any source block params which
1222 may be specified in the properties of the current outline entry."
1225 (org-babel-parse-multiple-vars
1228 (lambda (header-arg)
1229 (and (setq val
(org-entry-get (point) header-arg t
))
1230 (cons (intern (concat ":" header-arg
))
1231 (org-babel-read val
))))
1236 (org-babel-combine-header-arg-lists
1237 org-babel-common-header-args-w-values
1239 (setq sym
(intern (concat "org-babel-header-args:" lang
)))
1240 (and (boundp sym
) (eval sym
))))))))))))
1242 (defvar org-src-preserve-indentation
)
1243 (defun org-babel-parse-src-block-match ()
1244 "Parse the results from a match of the `org-babel-src-block-regexp'."
1245 (let* ((block-indentation (length (match-string 1)))
1246 (lang (org-no-properties (match-string 2)))
1247 (lang-headers (intern (concat "org-babel-default-header-args:" lang
)))
1248 (switches (match-string 3))
1249 (body (org-no-properties
1250 (let* ((body (match-string 5))
1251 (sub-length (- (length body
) 1)))
1252 (if (and (> sub-length
0)
1253 (string= "\n" (substring body sub-length
)))
1254 (substring body
0 sub-length
)
1256 (preserve-indentation (or org-src-preserve-indentation
1258 (string-match "-i\\>" switches
)))))
1260 ;; get block body less properties, protective commas, and indentation
1263 (insert (org-unescape-code-in-string body
))
1264 (unless preserve-indentation
(org-do-remove-indentation))
1266 (org-babel-merge-params
1267 org-babel-default-header-args
1268 (org-babel-params-from-properties lang
)
1269 (if (boundp lang-headers
) (eval lang-headers
) nil
)
1270 (org-babel-parse-header-arguments
1271 (org-no-properties (or (match-string 4) ""))))
1273 block-indentation
)))
1275 (defun org-babel-parse-inline-src-block-match ()
1276 "Parse the results from a match of the `org-babel-inline-src-block-regexp'."
1277 (let* ((lang (org-no-properties (match-string 2)))
1278 (lang-headers (intern (concat "org-babel-default-header-args:" lang
))))
1280 (org-unescape-code-in-string (org-no-properties (match-string 5)))
1281 (org-babel-merge-params
1282 org-babel-default-inline-header-args
1283 (org-babel-params-from-properties lang
)
1284 (if (boundp lang-headers
) (eval lang-headers
) nil
)
1285 (org-babel-parse-header-arguments
1286 (org-no-properties (or (match-string 4) "")))))))
1288 (defun org-babel-balanced-split (string alts
)
1289 "Split STRING on instances of ALTS.
1290 ALTS is a cons of two character options where each option may be
1291 either the numeric code of a single character or a list of
1292 character alternatives. For example to split on balanced
1293 instances of \"[ \t]:\" set ALTS to '((32 9) . 58)."
1294 (let* ((matches (lambda (ch spec
) (if (listp spec
) (member ch spec
) (equal spec ch
))))
1295 (matched (lambda (ch last
)
1297 (and (funcall matches ch
(cdr alts
))
1298 (funcall matches last
(car alts
)))
1299 (funcall matches ch alts
))))
1300 (balance 0) (last 0)
1302 (mapc (lambda (ch) ; split on [], (), "" balanced instances of [ \t]:
1303 (setq balance
(+ balance
1304 (cond ((or (equal 91 ch
) (equal 40 ch
)) 1)
1305 ((or (equal 93 ch
) (equal 41 ch
)) -
1)
1307 (when (and (equal 34 ch
) (not (equal 92 last
)))
1308 (setq quote
(not quote
)))
1309 (setq partial
(cons ch partial
))
1310 (when (and (= balance
0) (not quote
) (funcall matched ch last
))
1311 (setq lst
(cons (apply #'string
(nreverse
1318 (string-to-list string
))
1319 (nreverse (cons (apply #'string
(nreverse partial
)) lst
))))
1321 (defun org-babel-join-splits-near-ch (ch list
)
1322 "Join splits where \"=\" is on either end of the split."
1323 (let ((last= (lambda (str) (= ch
(aref str
(1- (length str
))))))
1324 (first= (lambda (str) (= ch
(aref str
0)))))
1326 (org-reduce (lambda (acc el
)
1327 (let ((head (car acc
)))
1328 (if (and head
(or (funcall last
= head
) (funcall first
= el
)))
1329 (cons (concat head el
) (cdr acc
))
1331 list
:initial-value nil
))))
1333 (defun org-babel-parse-header-arguments (arg-string)
1334 "Parse a string of header arguments returning an alist."
1335 (when (> (length arg-string
) 0)
1336 (org-babel-parse-multiple-vars
1341 "\\([^ \f\t\n\r\v]+\\)[ \f\t\n\r\v]+\\([^ \f\t\n\r\v]+.*\\)"
1343 (cons (intern (match-string 1 arg
))
1344 (org-babel-read (org-babel-chomp (match-string 2 arg
))))
1345 (cons (intern (org-babel-chomp arg
)) nil
)))
1347 (cons (car raw
) (mapcar (lambda (r) (concat ":" r
)) (cdr raw
))))
1348 (org-babel-balanced-split arg-string
'((32 9) .
58))))))))
1350 (defun org-babel-parse-multiple-vars (header-arguments)
1351 "Expand multiple variable assignments behind a single :var keyword.
1353 This allows expression of multiple variables with one :var as
1356 #+PROPERTY: var foo=1, bar=2"
1358 (mapc (lambda (pair)
1359 (if (eq (car pair
) :var
)
1360 (mapcar (lambda (v) (push (cons :var
(org-babel-trim v
)) results
))
1361 (org-babel-join-splits-near-ch
1362 61 (org-babel-balanced-split (cdr pair
) 32)))
1363 (push pair results
)))
1365 (nreverse results
)))
1367 (defun org-babel-process-params (params)
1368 "Expand variables in PARAMS and add summary parameters."
1369 (let* ((processed-vars (mapcar (lambda (el)
1370 (if (consp (cdr el
))
1372 (org-babel-ref-parse (cdr el
))))
1373 (org-babel-get-header params
:var
)))
1374 (vars-and-names (if (and (assoc :colname-names params
)
1375 (assoc :rowname-names params
))
1376 (list processed-vars
)
1377 (org-babel-disassemble-tables
1379 (cdr (assoc :hlines params
))
1380 (cdr (assoc :colnames params
))
1381 (cdr (assoc :rownames params
)))))
1382 (raw-result (or (cdr (assoc :results params
)) ""))
1383 (result-params (append
1384 (split-string (if (stringp raw-result
)
1387 (cdr (assoc :result-params params
)))))
1389 (mapcar (lambda (var) (cons :var var
)) (car vars-and-names
))
1391 (cons :colname-names
(or (cdr (assoc :colname-names params
))
1392 (cadr vars-and-names
)))
1393 (cons :rowname-names
(or (cdr (assoc :rowname-names params
))
1394 (caddr vars-and-names
)))
1395 (cons :result-params result-params
)
1396 (cons :result-type
(cond ((member "output" result-params
) 'output
)
1397 ((member "value" result-params
) 'value
)
1399 (org-babel-get-header params
:var
'other
))))
1401 ;; row and column names
1402 (defun org-babel-del-hlines (table)
1403 "Remove all 'hlines from TABLE."
1404 (remove 'hline table
))
1406 (defun org-babel-get-colnames (table)
1407 "Return the column names of TABLE.
1408 Return a cons cell, the `car' of which contains the TABLE less
1409 colnames, and the `cdr' of which contains a list of the column
1411 (if (equal 'hline
(nth 1 table
))
1412 (cons (cddr table
) (car table
))
1413 (cons (cdr table
) (car table
))))
1415 (defun org-babel-get-rownames (table)
1416 "Return the row names of TABLE.
1417 Return a cons cell, the `car' of which contains the TABLE less
1418 colnames, and the `cdr' of which contains a list of the column
1419 names. Note: this function removes any hlines in TABLE."
1420 (let* ((trans (lambda (table) (apply #'mapcar
* #'list table
)))
1422 (mapcar (lambda (el) (if (listp el
) (length el
) 0)) table
)))
1423 (table (funcall trans
(mapcar (lambda (row)
1424 (if (not (equal row
'hline
))
1428 (setq row
(cons 'hline row
)))
1431 (cons (mapcar (lambda (row) (if (equal (car row
) 'hline
) 'hline row
))
1432 (funcall trans
(cdr table
)))
1433 (remove 'hline
(car table
)))))
1435 (defun org-babel-put-colnames (table colnames
)
1436 "Add COLNAMES to TABLE if they exist."
1437 (if colnames
(apply 'list colnames
'hline table
) table
))
1439 (defun org-babel-put-rownames (table rownames
)
1440 "Add ROWNAMES to TABLE if they exist."
1442 (mapcar (lambda (row)
1444 (cons (or (pop rownames
) "") row
)
1448 (defun org-babel-pick-name (names selector
)
1449 "Select one out of an alist of row or column names.
1450 SELECTOR can be either a list of names in which case those names
1451 will be returned directly, or an index into the list NAMES in
1452 which case the indexed names will be return."
1453 (if (listp selector
)
1456 (if (and selector
(symbolp selector
) (not (equal t selector
)))
1457 (cdr (assoc selector names
))
1458 (if (integerp selector
)
1459 (nth (- selector
1) names
)
1460 (cdr (car (last names
))))))))
1462 (defun org-babel-disassemble-tables (vars hlines colnames rownames
)
1463 "Parse tables for further processing.
1464 Process the variables in VARS according to the HLINES,
1465 ROWNAMES and COLNAMES header arguments. Return a list consisting
1466 of the vars, cnames and rnames."
1467 (let (cnames rnames
)
1471 (when (listp (cdr var
))
1472 (when (and (not (equal colnames
"no"))
1473 (or colnames
(and (equal (nth 1 (cdr var
)) 'hline
)
1474 (not (member 'hline
(cddr (cdr var
)))))))
1475 (let ((both (org-babel-get-colnames (cdr var
))))
1476 (setq cnames
(cons (cons (car var
) (cdr both
))
1478 (setq var
(cons (car var
) (car both
)))))
1479 (when (and rownames
(not (equal rownames
"no")))
1480 (let ((both (org-babel-get-rownames (cdr var
))))
1481 (setq rnames
(cons (cons (car var
) (cdr both
))
1483 (setq var
(cons (car var
) (car both
)))))
1484 (when (and hlines
(not (equal hlines
"yes")))
1485 (setq var
(cons (car var
) (org-babel-del-hlines (cdr var
))))))
1488 (reverse cnames
) (reverse rnames
))))
1490 (defun org-babel-reassemble-table (table colnames rownames
)
1491 "Add column and row names to a table.
1492 Given a TABLE and set of COLNAMES and ROWNAMES add the names
1493 to the table for reinsertion to org-mode."
1496 (if (and colnames
(listp (car table
)) (= (length (car table
))
1498 (org-babel-put-colnames table colnames
) table
))
1499 (if (and rownames
(= (length table
) (length rownames
)))
1500 (org-babel-put-rownames table rownames
) table
))
1503 (defun org-babel-where-is-src-block-head ()
1504 "Find where the current source block begins.
1505 Return the point at the beginning of the current source
1506 block. Specifically at the beginning of the #+BEGIN_SRC line.
1507 If the point is not on a source block then return nil."
1508 (let ((initial (point)) (case-fold-search t
) top bottom
)
1510 (save-excursion ;; on a source name line or a #+header line
1511 (beginning-of-line 1)
1512 (and (or (looking-at org-babel-src-name-regexp
)
1513 (looking-at org-babel-multi-line-header-regexp
))
1515 (while (and (forward-line 1)
1516 (or (looking-at org-babel-src-name-regexp
)
1517 (looking-at org-babel-multi-line-header-regexp
))))
1518 (looking-at org-babel-src-block-regexp
))
1520 (save-excursion ;; on a #+begin_src line
1521 (beginning-of-line 1)
1522 (and (looking-at org-babel-src-block-regexp
)
1524 (save-excursion ;; inside a src block
1526 (re-search-backward "^[ \t]*#\\+begin_src" nil t
) (setq top
(point))
1527 (re-search-forward "^[ \t]*#\\+end_src" nil t
) (setq bottom
(point))
1528 (< top initial
) (< initial bottom
)
1529 (progn (goto-char top
) (beginning-of-line 1)
1530 (looking-at org-babel-src-block-regexp
))
1534 (defun org-babel-goto-src-block-head ()
1535 "Go to the beginning of the current code block."
1538 (if head
(goto-char head
) (error "Not currently in a code block")))
1539 (org-babel-where-is-src-block-head)))
1542 (defun org-babel-goto-named-src-block (name)
1543 "Go to a named source-code block."
1545 (let ((completion-ignore-case t
)
1546 (case-fold-search t
)
1547 (under-point (thing-at-point 'line
)))
1548 (list (org-icompleting-read
1549 "source-block name: " (org-babel-src-block-names) nil t
1552 ((string-match (org-babel-noweb-wrap) under-point
)
1553 (let ((block-name (match-string 1 under-point
)))
1554 (string-match "[^(]*" block-name
)
1555 (match-string 0 block-name
)))
1557 ((string-match org-babel-lob-one-liner-regexp under-point
)
1558 (let ((source-info (car (org-babel-lob-get-info))))
1559 (if (string-match "^\\([^\\[]+?\\)\\(\\[.*\\]\\)?(" source-info
)
1560 (let ((source-name (match-string 1 source-info
)))
1563 ((string-match (concat "#\\+" org-babel-results-keyword
1564 "\\:\s+\\([^\\(]*\\)") under-point
)
1565 (match-string 1 under-point
))
1567 ((and (thing-at-point 'symbol
))
1568 (org-babel-find-named-block (thing-at-point 'symbol
))
1569 (thing-at-point 'symbol
))
1571 (let ((point (org-babel-find-named-block name
)))
1573 ;; taken from `org-open-at-point'
1574 (progn (org-mark-ring-push) (goto-char point
) (org-show-context))
1575 (message "source-code block '%s' not found in this buffer" name
))))
1577 (defun org-babel-find-named-block (name)
1578 "Find a named source-code block.
1579 Return the location of the source block identified by source
1580 NAME, or nil if no such block exists. Set match data according to
1581 org-babel-named-src-block-regexp."
1583 (let ((case-fold-search t
)
1584 (regexp (org-babel-named-src-block-regexp-for-name name
)) msg
)
1585 (goto-char (point-min))
1586 (when (or (re-search-forward regexp nil t
)
1587 (re-search-backward regexp nil t
))
1588 (match-beginning 0)))))
1590 (defun org-babel-src-block-names (&optional file
)
1591 "Returns the names of source blocks in FILE or the current buffer."
1593 (when file
(find-file file
)) (goto-char (point-min))
1594 (let ((case-fold-search t
) names
)
1595 (while (re-search-forward org-babel-src-name-w-name-regexp nil t
)
1596 (setq names
(cons (match-string 3) names
)))
1600 (defun org-babel-goto-named-result (name)
1601 "Go to a named result."
1603 (let ((completion-ignore-case t
))
1604 (list (org-icompleting-read "source-block name: "
1605 (org-babel-result-names) nil t
))))
1606 (let ((point (org-babel-find-named-result name
)))
1608 ;; taken from `org-open-at-point'
1609 (progn (goto-char point
) (org-show-context))
1610 (message "result '%s' not found in this buffer" name
))))
1612 (defun org-babel-find-named-result (name &optional point
)
1613 "Find a named result.
1614 Return the location of the result named NAME in the current
1615 buffer or nil if no such result exists."
1617 (let ((case-fold-search t
))
1618 (goto-char (or point
(point-min)))
1619 (catch 'is-a-code-block
1620 (when (re-search-forward
1621 (concat org-babel-result-regexp
1622 "[ \t]" (regexp-quote name
) "[ \t]*[\n\f\v\r]") nil t
)
1623 (when (and (string= "name" (downcase (match-string 1)))
1624 (or (beginning-of-line 1)
1625 (looking-at org-babel-src-block-regexp
)
1626 (looking-at org-babel-multi-line-header-regexp
)))
1627 (throw 'is-a-code-block
(org-babel-find-named-result name
(point))))
1628 (beginning-of-line 0) (point))))))
1630 (defun org-babel-result-names (&optional file
)
1631 "Returns the names of results in FILE or the current buffer."
1633 (when file
(find-file file
)) (goto-char (point-min))
1634 (let ((case-fold-search t
) names
)
1635 (while (re-search-forward org-babel-result-w-name-regexp nil t
)
1636 (setq names
(cons (match-string 4) names
)))
1640 (defun org-babel-next-src-block (&optional arg
)
1641 "Jump to the next source block.
1642 With optional prefix argument ARG, jump forward ARG many source blocks."
1644 (when (looking-at org-babel-src-block-regexp
) (forward-char 1))
1646 (re-search-forward org-babel-src-block-regexp nil nil
(or arg
1))
1647 (error (error "No further code blocks")))
1648 (goto-char (match-beginning 0)) (org-show-context))
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."
1656 (re-search-backward org-babel-src-block-regexp nil nil
(or arg
1))
1657 (error (error "No previous code blocks")))
1658 (goto-char (match-beginning 0)) (org-show-context))
1660 (defvar org-babel-load-languages
)
1663 (defun org-babel-mark-block ()
1664 "Mark current src block."
1670 (looking-at org-babel-src-block-regexp
))
1671 (push-mark (match-end 5) nil t
)
1672 (goto-char (match-beginning 5))))
1673 (org-babel-where-is-src-block-head)))
1675 (defun org-babel-demarcate-block (&optional arg
)
1676 "Wrap or split the code in the region or on the point.
1677 When called from inside of a code block the current block is
1678 split. When called from outside of a code block a new code block
1679 is created. In both cases if the region is demarcated and if the
1680 region is not active then the point is demarcated."
1682 (let ((info (org-babel-get-src-block-info 'light
))
1683 (headers (progn (org-babel-where-is-src-block-head)
1685 (stars (concat (make-string (or (org-current-level) 1) ?
*) " ")))
1691 (let ((lang (nth 0 info
))
1692 (indent (make-string (nth 5 info
) ?
)))
1693 (when (string-match "^[[:space:]]*$"
1694 (buffer-substring (point-at-bol)
1696 (delete-region (point-at-bol) (point-at-eol)))
1698 (if (looking-at "^") "" "\n")
1699 indent
"#+end_src\n"
1700 (if arg stars indent
) "\n"
1701 indent
"#+begin_src " lang
1702 (if (> (length headers
) 1)
1703 (concat " " headers
) headers
)
1704 (if (looking-at "[\n\r]")
1706 (concat "\n" (make-string (current-column) ?
)))))))
1707 (move-end-of-line 2))
1708 (sort (if (org-region-active-p) (list (mark) (point)) (list (point))) #'>))
1709 (let ((start (point))
1710 (lang (org-icompleting-read "Lang: "
1711 (mapcar (lambda (el) (symbol-name (car el
)))
1712 org-babel-load-languages
)))
1713 (body (delete-and-extract-region
1714 (if (org-region-active-p) (mark) (point)) (point))))
1715 (insert (concat (if (looking-at "^") "" "\n")
1716 (if arg
(concat stars
"\n") "")
1717 "#+begin_src " lang
"\n"
1719 (if (or (= (length body
) 0)
1720 (string-match "[\r\n]$" body
)) "" "\n")
1722 (goto-char start
) (move-end-of-line 1)))))
1724 (defvar org-babel-lob-one-liner-regexp
)
1725 (defun org-babel-where-is-src-block-result (&optional insert info hash indent
)
1726 "Find where the current source block results begin.
1727 Return the point at the beginning of the result of the current
1728 source block. Specifically at the beginning of the results line.
1729 If no result exists for this block then create a results line
1730 following the source block."
1732 (let* ((case-fold-search t
)
1733 (on-lob-line (save-excursion
1734 (beginning-of-line 1)
1735 (looking-at org-babel-lob-one-liner-regexp
)))
1736 (inlinep (when (org-babel-get-inline-src-block-matches)
1738 (name (if on-lob-line
1739 (mapconcat #'identity
(butlast (org-babel-lob-get-info)) "")
1740 (nth 4 (or info
(org-babel-get-src-block-info 'light
)))))
1741 (head (unless on-lob-line
(org-babel-where-is-src-block-head)))
1743 (when head
(goto-char head
))
1744 (org-with-wide-buffer
1746 found
;; was there a result (before we potentially insert one)
1751 ;; - return t if it is found, else return nil
1752 ;; - if it does not need to be rebuilt, then don't set end
1753 ;; - if it does need to be rebuilt then do set end
1754 name
(setq beg
(org-babel-find-named-result name
))
1756 (when (and hash
(not (string= hash
(or (match-string 3)
1757 (match-string 4)))))
1758 (goto-char beg
) (setq end beg
) ;; beginning of result
1760 (delete-region end
(org-babel-result-end)) nil
)))
1763 ;; - return t if it is found, else return nil
1764 ;; - if it is found, and the hash doesn't match, delete and set end
1765 (or on-lob-line
(re-search-forward "^[ \t]*#\\+end_src" nil t
))
1766 (progn (end-of-line 1)
1767 (if (eobp) (insert "\n") (forward-char 1))
1770 (progn ;; unnamed results line already exists
1771 (re-search-forward "[^ \f\t\n\r\v]" nil t
)
1772 (beginning-of-line 1)
1774 (concat org-babel-result-regexp
"\n")))
1775 (let ((this-hash (or ;; from org-babel-result-regexp
1779 ;; must remove and rebuild if hash!=old-hash
1780 (if (and hash
(not (string= hash this-hash
)))
1784 end
(org-babel-result-end)))
1785 (setq end nil
)))))))))))
1786 (if (not (and insert end
)) found
1789 (if (looking-at "[\n\r]") (forward-char 1) (insert "\n")))
1791 (when (wholenump indent
) (make-string indent ?
))
1792 "#+" org-babel-results-keyword
1794 (if org-babel-hash-show-time
1797 (format-time-string "<%Y-%m-%d %H:%M:%S>")
1799 (concat "["hash
"]")))
1801 (when name
(concat " " name
)) "\n"))
1802 (unless beg
(insert "\n") (backward-char))
1803 (beginning-of-line 0)
1804 (when (and hash
(not org-babel-hash-show-time
)) (org-babel-hide-hash))
1807 (defvar org-block-regexp
)
1808 (defun org-babel-read-result ()
1809 "Read the result at `point' into emacs-lisp."
1810 (let ((case-fold-search t
) result-string
)
1812 ((org-at-table-p) (org-babel-read-table))
1813 ((org-at-item-p) (org-babel-read-list))
1814 ((looking-at org-bracket-link-regexp
) (org-babel-read-link))
1815 ((looking-at org-block-regexp
) (org-babel-trim (match-string 4)))
1816 ((looking-at "^[ \t]*: ")
1819 (mapconcat (lambda (line)
1820 (if (and (> (length line
) 1)
1821 (string-match "^[ \t]*: \\(.+\\)" line
))
1822 (match-string 1 line
)
1826 (point) (org-babel-result-end)) "[\r\n]+")
1828 (or (org-babel-number-p result-string
) result-string
))
1829 ((looking-at org-babel-result-regexp
)
1830 (save-excursion (forward-line 1) (org-babel-read-result))))))
1832 (defun org-babel-read-table ()
1833 "Read the table at `point' into emacs-lisp."
1834 (mapcar (lambda (row)
1835 (if (and (symbolp row
) (equal row
'hline
)) row
1836 (mapcar (lambda (el) (org-babel-read el
'inhibit-lisp-eval
)) row
)))
1837 (org-table-to-lisp)))
1839 (defun org-babel-read-list ()
1840 "Read the list at `point' into emacs-lisp."
1841 (mapcar (lambda (el) (org-babel-read el
'inhibit-lisp-eval
))
1842 (mapcar #'cadr
(cdr (org-list-parse-list)))))
1844 (defvar org-link-types-re
)
1845 (defun org-babel-read-link ()
1846 "Read the link at `point' into emacs-lisp.
1847 If the path of the link is a file path it is expanded using
1848 `expand-file-name'."
1849 (let* ((case-fold-search t
)
1850 (raw (and (looking-at org-bracket-link-regexp
)
1851 (org-no-properties (match-string 1))))
1852 (type (and (string-match org-link-types-re raw
)
1853 (match-string 1 raw
))))
1855 ((not type
) (expand-file-name raw
))
1856 ((string= type
"file")
1857 (and (string-match "file\\(.*\\):\\(.+\\)" raw
)
1858 (expand-file-name (match-string 2 raw
))))
1861 (defun org-babel-format-result (result &optional sep
)
1862 "Format RESULT for writing to file."
1863 (let ((echo-res (lambda (r) (if (stringp r
) r
(format "%S" r
)))))
1867 result
(list :sep
(or sep
"\t") :fmt echo-res
))
1869 (funcall echo-res result
))))
1871 (defun org-babel-insert-result
1872 (result &optional result-params info hash indent lang
)
1873 "Insert RESULT into the current buffer.
1874 By default RESULT is inserted after the end of the
1875 current source block. With optional argument RESULT-PARAMS
1876 controls insertion of results in the org-mode file.
1877 RESULT-PARAMS can take the following values:
1879 replace - (default option) insert results after the source block
1880 replacing any previously inserted results
1882 silent -- no results are inserted into the Org-mode buffer but
1883 the results are echoed to the minibuffer and are
1884 ingested by Emacs (a potentially time consuming
1887 file ---- the results are interpreted as a file path, and are
1888 inserted into the buffer using the Org-mode file syntax
1890 list ---- the results are interpreted as an Org-mode list.
1892 raw ----- results are added directly to the Org-mode file. This
1893 is a good option if you code block will output org-mode
1896 drawer -- results are added directly to the Org-mode file as with
1897 \"raw\", but are wrapped in a RESULTS drawer, allowing
1898 them to later be replaced or removed automatically.
1900 org ----- results are added inside of a \"#+BEGIN_SRC org\" block.
1901 They are not comma-escaped when inserted, but Org syntax
1902 here will be discarded when exporting the file.
1904 html ---- results are added inside of a #+BEGIN_HTML block. This
1905 is a good option if you code block will output html
1908 latex --- results are added inside of a #+BEGIN_LATEX block.
1909 This is a good option if you code block will output
1910 latex formatted text.
1912 code ---- the results are extracted in the syntax of the source
1913 code of the language being evaluated and are added
1914 inside of a #+BEGIN_SRC block with the source-code
1915 language set appropriately. Note this relies on the
1916 optional LANG argument."
1917 (if (stringp result
)
1919 (setq result
(org-no-properties result
))
1920 (when (member "file" result-params
)
1921 (setq result
(org-babel-result-to-file
1922 result
(when (assoc :file-desc
(nth 2 info
))
1923 (or (cdr (assoc :file-desc
(nth 2 info
)))
1925 (unless (listp result
) (setq result
(format "%S" result
))))
1926 (if (and result-params
(member "silent" result-params
))
1928 (message (replace-regexp-in-string "%" "%%" (format "%S" result
)))
1933 (when (or (org-babel-get-inline-src-block-matches)
1934 (org-babel-get-lob-one-liner-matches))
1935 (goto-char (match-end 0))
1936 (insert (if (listp result
) "\n" " "))
1938 (existing-result (unless inlinep
1939 (org-babel-where-is-src-block-result
1940 t info hash indent
)))
1942 (cdr (assoc :results_switches
(nth 2 info
))))
1943 (visible-beg (copy-marker (point-min)))
1944 (visible-end (copy-marker (point-max)))
1945 ;; When results exist outside of the current visible
1946 ;; region of the buffer, be sure to widen buffer to
1948 (outside-scope-p (and existing-result
1949 (or (> visible-beg existing-result
)
1950 (<= visible-end existing-result
))))
1952 (when (and (stringp result
) ; ensure results end in a newline
1954 (> (length result
) 0)
1955 (not (or (string-equal (substring result -
1) "\n")
1956 (string-equal (substring result -
1) "\r"))))
1957 (setq result
(concat result
"\n")))
1960 (when outside-scope-p
(widen))
1961 (if (not existing-result
)
1962 (setq beg
(or inlinep
(point)))
1963 (goto-char existing-result
)
1965 (re-search-forward "#" nil t
)
1966 (setq indent
(- (current-column) 1)))
1970 ((member "replace" result-params
)
1971 (delete-region (point) (org-babel-result-end)))
1972 ((member "append" result-params
)
1973 (goto-char (org-babel-result-end)) (setq beg
(point-marker)))
1974 ((member "prepend" result-params
)))) ; already there
1975 (setq results-switches
1976 (if results-switches
(concat " " results-switches
) ""))
1977 (let ((wrap (lambda (start finish
&optional no-escape
)
1978 (goto-char end
) (insert (concat finish
"\n"))
1979 (goto-char beg
) (insert (concat start
"\n"))
1981 (org-escape-code-in-region (point) end
))
1982 (goto-char end
) (goto-char (point-at-eol))
1983 (setq end
(point-marker))))
1984 (proper-list-p (lambda (it) (and (listp it
) (null (cdr (last it
)))))))
1985 ;; insert results based on type
1987 ;; do nothing for an empty result
1989 ;; insert a list if preferred
1990 ((member "list" result-params
)
1993 (org-list-to-generic
1996 (lambda (el) (list nil
(if (stringp el
) el
(format "%S" el
))))
1997 (if (listp result
) result
(list result
))))
1998 '(:splicep nil
:istart
"- " :iend
"\n")))
2000 ;; assume the result is a table if it's not a string
2001 ((funcall proper-list-p result
)
2003 (insert (concat (orgtbl-to-orgtbl
2004 (if (or (eq 'hline
(car result
))
2005 (and (listp (car result
))
2006 (listp (cdr (car result
)))))
2007 result
(list result
))
2008 '(:fmt
(lambda (cell) (format "%s" cell
)))) "\n"))
2009 (goto-char beg
) (when (org-at-table-p) (org-table-align)))
2010 ((and (listp result
) (not (funcall proper-list-p result
)))
2011 (insert (format "%s\n" result
)))
2012 ((member "file" result-params
)
2013 (when inlinep
(goto-char inlinep
))
2015 (t (goto-char beg
) (insert result
)))
2016 (when (funcall proper-list-p result
) (goto-char (org-table-end)))
2017 (setq end
(point-marker))
2018 ;; possibly wrap result
2020 ((assoc :wrap
(nth 2 info
))
2021 (let ((name (or (cdr (assoc :wrap
(nth 2 info
))) "RESULTS")))
2022 (funcall wrap
(concat "#+BEGIN_" name
)
2023 (concat "#+END_" (car (org-split-string name
))))))
2024 ((member "html" result-params
)
2025 (funcall wrap
"#+BEGIN_HTML" "#+END_HTML"))
2026 ((member "latex" result-params
)
2027 (funcall wrap
"#+BEGIN_LaTeX" "#+END_LaTeX"))
2028 ((member "org" result-params
)
2029 (funcall wrap
"#+BEGIN_SRC org" "#+END_SRC"))
2030 ((member "code" result-params
)
2031 (funcall wrap
(format "#+BEGIN_SRC %s%s" (or lang
"none") results-switches
)
2033 ((member "raw" result-params
)
2034 (goto-char beg
) (if (org-at-table-p) (org-cycle)))
2035 ((or (member "drawer" result-params
)
2036 ;; Stay backward compatible with <7.9.2
2037 (member "wrap" result-params
))
2038 (funcall wrap
":RESULTS:" ":END:" 'no-escape
))
2039 ((and (not (funcall proper-list-p result
))
2040 (not (member "file" result-params
)))
2041 (org-babel-examplize-region beg end results-switches
)
2042 (setq end
(point)))))
2043 ;; possibly indent the results to match the #+results line
2044 (when (and (not inlinep
) (numberp indent
) indent
(> indent
0)
2045 ;; in this case `table-align' does the work for us
2046 (not (and (listp result
)
2047 (member "append" result-params
))))
2048 (indent-rigidly beg end indent
))
2050 (if (member "value" result-params
)
2051 (message "Code block returned no value.")
2052 (message "Code block produced no output."))
2053 (message "Code block evaluation complete.")))
2054 (when outside-scope-p
(narrow-to-region visible-beg visible-end
))
2055 (set-marker visible-beg nil
)
2056 (set-marker visible-end nil
))))))
2058 (defun org-babel-remove-result (&optional info
)
2059 "Remove the result of the current source block."
2061 (let ((location (org-babel-where-is-src-block-result nil info
)) start
)
2063 (setq start
(- location
1))
2065 (goto-char location
) (forward-line 1)
2066 (delete-region start
(org-babel-result-end))))))
2068 (defun org-babel-result-end ()
2069 "Return the point at the end of the current set of results."
2072 ((org-at-table-p) (progn (goto-char (org-table-end)) (point)))
2073 ((org-at-item-p) (let* ((struct (org-list-struct))
2074 (prvs (org-list-prevs-alist struct
)))
2075 (org-list-get-list-end (point-at-bol) struct prvs
)))
2076 ((let ((case-fold-search t
)) (looking-at "^\\([ \t]*\\):results:"))
2077 (progn (re-search-forward (concat "^" (match-string 1) ":END:"))
2078 (forward-char 1) (point)))
2080 (let ((case-fold-search t
))
2081 (if (looking-at (concat "[ \t]*#\\+begin_\\([^ \t\n\r]+\\)"))
2082 (progn (re-search-forward (concat "[ \t]*#\\+end_" (match-string 1))
2085 (while (looking-at "[ \t]*\\(: \\|\\[\\[\\)")
2089 (defun org-babel-result-to-file (result &optional description
)
2090 "Convert RESULT into an `org-mode' link with optional DESCRIPTION.
2091 If the `default-directory' is different from the containing
2092 file's directory then expand relative links."
2093 (when (stringp result
)
2094 (format "[[file:%s]%s]"
2095 (if (and default-directory
2097 (not (string= (expand-file-name default-directory
)
2099 (file-name-directory buffer-file-name
)))))
2100 (expand-file-name result default-directory
)
2102 (if description
(concat "[" description
"]") ""))))
2104 (defvar org-babel-capitalize-examplize-region-markers nil
2105 "Make true to capitalize begin/end example markers inserted by code blocks.")
2107 (defun org-babel-examplize-region (beg end
&optional results-switches
)
2108 "Comment out region using the inline '==' or ': ' org example quote."
2110 (let ((chars-between (lambda (b e
)
2111 (not (string-match "^[\\s]*$" (buffer-substring b e
)))))
2112 (maybe-cap (lambda (str) (if org-babel-capitalize-examplize-region-markers
2113 (upcase str
) str
))))
2114 (if (or (funcall chars-between
(save-excursion (goto-char beg
) (point-at-bol)) beg
)
2115 (funcall chars-between end
(save-excursion (goto-char end
) (point-at-eol))))
2118 (insert (format "=%s=" (prog1 (buffer-substring beg end
)
2119 (delete-region beg end
)))))
2120 (let ((size (count-lines beg end
)))
2122 (cond ((= size
0)) ; do nothing for an empty result
2123 ((< size org-babel-min-lines-for-block-output
)
2126 (beginning-of-line 1) (insert ": ") (forward-line 1)))
2129 (insert (if results-switches
2131 (funcall maybe-cap
"#+begin_example")
2133 (funcall maybe-cap
"#+begin_example\n")))
2134 (if (markerp end
) (goto-char end
) (forward-char (- end beg
)))
2135 (insert (funcall maybe-cap
"#+end_example\n")))))))))
2137 (defun org-babel-update-block-body (new-body)
2138 "Update the body of the current code block to NEW-BODY."
2139 (if (not (org-babel-where-is-src-block-head))
2140 (error "Not in a source block")
2142 (replace-match (concat (org-babel-trim new-body
) "\n") nil t nil
5))
2143 (indent-rigidly (match-beginning 5) (match-end 5) 2)))
2145 (defun org-babel-merge-params (&rest plists
)
2146 "Combine all parameter association lists in PLISTS.
2147 Later elements of PLISTS override the values of previous elements.
2148 This takes into account some special considerations for certain
2149 parameters when merging lists."
2150 (let* ((results-exclusive-groups
2151 (mapcar (lambda (group) (mapcar #'symbol-name group
))
2152 (cdr (assoc 'results org-babel-common-header-args-w-values
))))
2153 (exports-exclusive-groups
2154 (mapcar (lambda (group) (mapcar #'symbol-name group
))
2155 (cdr (assoc 'exports org-babel-common-header-args-w-values
))))
2157 (e-merge (lambda (exclusive-groups &rest result-params
)
2158 ;; maintain exclusivity of mutually exclusive parameters
2160 (mapc (lambda (new-params)
2161 (mapc (lambda (new-param)
2162 (mapc (lambda (exclusive-group)
2163 (when (member new-param exclusive-group
)
2164 (mapcar (lambda (excluded-param)
2171 (setq output
(org-uniquify
2172 (cons new-param output
))))
2176 params results exports tangle noweb cache vars shebang comments padline
)
2184 (let ((name (if (listp (cdr pair
))
2186 (and (string-match "^\\([^= \f\t\n\r\v]+\\)[ \t]*="
2188 (intern (match-string 1 (cdr pair
)))))))
2192 (if (member name
(mapcar #'car vars
))
2196 (unless (equal (car p
) name
) p
))
2199 (list (cons name pair
))))
2200 ;; if no name is given and we already have named variables
2201 ;; then assign to named variables in order
2202 (if (and vars
(nth variable-index vars
))
2203 (prog1 (setf (cddr (nth variable-index vars
))
2204 (concat (symbol-name
2205 (car (nth variable-index vars
)))
2207 (incf variable-index
))
2208 (error "Variable \"%s\" must be assigned a default value"
2211 (setq results
(funcall e-merge results-exclusive-groups
2214 (let ((r (cdr pair
)))
2215 (if (stringp r
) r
(eval r
)))))))
2218 (setq results
(funcall e-merge results-exclusive-groups
2220 (unless (or (member "both" exports
)
2221 (member "none" exports
)
2222 (member "code" exports
))
2223 (setq exports
(funcall e-merge exports-exclusive-groups
2224 exports
'("results"))))
2225 (setq params
(cons pair
(assq-delete-all (car pair
) params
)))))
2227 (setq exports
(funcall e-merge exports-exclusive-groups
2228 exports
(split-string (cdr pair
)))))
2229 (:tangle
;; take the latest -- always overwrite
2230 (setq tangle
(or (list (cdr pair
)) tangle
)))
2232 (setq noweb
(funcall e-merge
2233 '(("yes" "no" "tangle" "no-export"
2234 "strip-export" "eval"))
2236 (split-string (or (cdr pair
) "")))))
2238 (setq cache
(funcall e-merge
'(("yes" "no")) cache
2239 (split-string (or (cdr pair
) "")))))
2241 (setq padline
(funcall e-merge
'(("yes" "no")) padline
2242 (split-string (or (cdr pair
) "")))))
2243 (:shebang
;; take the latest -- always overwrite
2244 (setq shebang
(or (list (cdr pair
)) shebang
)))
2246 (setq comments
(funcall e-merge
'(("yes" "no")) comments
2247 (split-string (or (cdr pair
) "")))))
2248 (t ;; replace: this covers e.g. :session
2249 (setq params
(cons pair
(assq-delete-all (car pair
) params
))))))
2252 (setq vars
(reverse vars
))
2253 (while vars
(setq params
(cons (cons :var
(cddr (pop vars
))) params
)))
2256 (let ((key (intern (concat ":" (symbol-name hd
))))
2258 (setf params
(cons (cons key
(mapconcat 'identity val
" ")) params
))))
2259 '(results exports tangle noweb padline cache shebang comments
))
2262 (defvar org-babel-use-quick-and-dirty-noweb-expansion nil
2263 "Set to true to use regular expressions to expand noweb references.
2264 This results in much faster noweb reference expansion but does
2265 not properly allow code blocks to inherit the \":noweb-ref\"
2266 header argument from buffer or subtree wide properties.")
2268 (defun org-babel-noweb-p (params context
)
2269 "Check if PARAMS require expansion in CONTEXT.
2270 CONTEXT may be one of :tangle, :export or :eval."
2272 (intersect (lambda (as bs
)
2274 (if (member (car as
) bs
)
2276 (funcall intersect
(cdr as
) bs
))))))
2277 (funcall intersect
(case context
2278 (:tangle
'("yes" "tangle" "no-export" "strip-export"))
2279 (:eval
'("yes" "no-export" "strip-export" "eval"))
2281 (split-string (or (cdr (assoc :noweb params
)) "")))))
2283 (defun org-babel-expand-noweb-references (&optional info parent-buffer
)
2284 "Expand Noweb references in the body of the current source code block.
2286 For example the following reference would be replaced with the
2287 body of the source-code block named 'example-block'.
2291 Note that any text preceding the <<foo>> construct on a line will
2292 be interposed between the lines of the replacement text. So for
2293 example if <<foo>> is placed behind a comment, then the entire
2294 replacement text will also be commented.
2296 This function must be called from inside of the buffer containing
2297 the source-code block which holds BODY.
2299 In addition the following syntax can be used to insert the
2300 results of evaluating the source-code block named 'example-block'.
2304 Any optional arguments can be passed to example-block by placing
2305 the arguments inside the parenthesis following the convention
2306 defined by `org-babel-lob'. For example
2308 <<example-block(a=9)>>
2310 would set the value of argument \"a\" equal to \"9\". Note that
2311 these arguments are not evaluated in the current source-code
2312 block but are passed literally to the \"example-block\"."
2313 (let* ((parent-buffer (or parent-buffer
(current-buffer)))
2314 (info (or info
(org-babel-get-src-block-info)))
2317 (ob-nww-start org-babel-noweb-wrap-start
)
2318 (ob-nww-end org-babel-noweb-wrap-end
)
2319 (comment (string= "noweb" (cdr (assoc :comments
(nth 2 info
)))))
2320 (rx-prefix (concat "\\(" org-babel-src-name-regexp
"\\|"
2321 ":noweb-ref[ \t]+" "\\)"))
2323 (nb-add (lambda (text) (setq new-body
(concat new-body text
))))
2324 (c-wrap (lambda (text)
2326 (funcall (intern (concat lang
"-mode")))
2327 (comment-region (point) (progn (insert text
) (point)))
2328 (org-babel-trim (buffer-string)))))
2329 index source-name evaluate prefix blocks-in-buffer
)
2331 (org-set-local 'org-babel-noweb-wrap-start ob-nww-start
)
2332 (org-set-local 'org-babel-noweb-wrap-end ob-nww-end
)
2333 (insert body
) (goto-char (point-min))
2334 (setq index
(point))
2335 (while (and (re-search-forward (org-babel-noweb-wrap) nil t
))
2336 (save-match-data (setf source-name
(match-string 1)))
2337 (save-match-data (setq evaluate
(string-match "\(.*\)" source-name
)))
2340 (buffer-substring (match-beginning 0)
2342 (beginning-of-line 1) (point)))))
2343 ;; add interval to new-body (removing noweb reference)
2344 (goto-char (match-beginning 0))
2345 (funcall nb-add
(buffer-substring index
(point)))
2346 (goto-char (match-end 0))
2347 (setq index
(point))
2349 (with-current-buffer parent-buffer
2352 (mapconcat ;; interpose PREFIX between every line
2356 (let ((raw (org-babel-ref-resolve source-name
)))
2357 (if (stringp raw
) raw
(format "%S" raw
)))
2359 ;; retrieve from the library of babel
2360 (nth 2 (assoc (intern source-name
)
2361 org-babel-library-of-babel
))
2362 ;; return the contents of headlines literally
2364 (when (org-babel-ref-goto-headline-id source-name
)
2365 (org-babel-ref-headline-body)))
2366 ;; find the expansion of reference in this buffer
2367 (let ((rx (concat rx-prefix source-name
"[ \t\n]"))
2370 (goto-char (point-min))
2371 (if org-babel-use-quick-and-dirty-noweb-expansion
2372 (while (re-search-forward rx nil t
)
2373 (let* ((i (org-babel-get-src-block-info 'light
))
2374 (body (org-babel-expand-noweb-references i
))
2375 (sep (or (cdr (assoc :noweb-sep
(nth 2 i
)))
2379 (concat (funcall c-wrap
(car cs
)) "\n"
2381 (funcall c-wrap
(cadr cs
))))
2382 (org-babel-tangle-comment-links i
))
2384 (setq expansion
(cons sep
(cons full expansion
)))))
2385 (org-babel-map-src-blocks nil
2386 (let ((i (org-babel-get-src-block-info 'light
)))
2387 (when (equal (or (cdr (assoc :noweb-ref
(nth 2 i
)))
2390 (let* ((body (org-babel-expand-noweb-references i
))
2391 (sep (or (cdr (assoc :noweb-sep
(nth 2 i
)))
2395 (concat (funcall c-wrap
(car cs
)) "\n"
2397 (funcall c-wrap
(cadr cs
))))
2398 (org-babel-tangle-comment-links i
))
2401 (cons sep
(cons full expansion
)))))))))
2403 (mapconcat #'identity
(nreverse (cdr expansion
)) "")))
2404 ;; possibly raise an error if named block doesn't exist
2405 (if (member lang org-babel-noweb-error-langs
)
2407 (org-babel-noweb-wrap source-name
)
2408 "could not be resolved (see "
2409 "`org-babel-noweb-error-langs')"))
2411 "[\n\r]") (concat "\n" prefix
))))))
2412 (funcall nb-add
(buffer-substring index
(point-max))))
2415 (defun org-babel-script-escape (str &optional force
)
2416 "Safely convert tables into elisp lists."
2417 (let (in-single in-double out
)
2418 ((lambda (escaped) (condition-case nil
(org-babel-read escaped
) (error escaped
)))
2422 (or (and (string-equal "[" (substring str
0 1))
2423 (string-equal "]" (substring str -
1)))
2424 (and (string-equal "{" (substring str
0 1))
2425 (string-equal "}" (substring str -
1)))
2426 (and (string-equal "(" (substring str
0 1))
2427 (string-equal ")" (substring str -
1))))))
2437 (91 (if (or in-double in-single
) ; [
2440 (93 (if (or in-double in-single
) ; ]
2443 (123 (if (or in-double in-single
) ; {
2446 (125 (if (or in-double in-single
) ; }
2449 (44 (if (or in-double in-single
) ; ,
2450 (cons 44 out
) (cons 32 out
)))
2451 (39 (if in-double
; '
2453 (setq in-single
(not in-single
)) (cons 34 out
)))
2454 (34 (if in-single
; "
2455 (append (list 34 32) out
)
2456 (setq in-double
(not in-double
)) (cons 34 out
)))
2457 (t (cons ch out
)))))
2458 (string-to-list str
))
2459 (apply #'string
(reverse out
)))))
2462 (defun org-babel-read (cell &optional inhibit-lisp-eval
)
2463 "Convert the string value of CELL to a number if appropriate.
2464 Otherwise if cell looks like lisp (meaning it starts with a
2465 \"(\", \"'\", \"`\" or a \"[\") then read it as lisp, otherwise
2466 return it unmodified as a string. Optional argument NO-LISP-EVAL
2467 inhibits lisp evaluation for situations in which is it not
2469 (if (and (stringp cell
) (not (equal cell
"")))
2470 (or (org-babel-number-p cell
)
2471 (if (and (not inhibit-lisp-eval
)
2472 (member (substring cell
0 1) '("(" "'" "`" "[")))
2474 (if (string= (substring cell
0 1) "\"")
2476 (progn (set-text-properties 0 (length cell
) nil cell
) cell
))))
2479 (defun org-babel-number-p (string)
2480 "If STRING represents a number return its value."
2481 (if (and (string-match "^-?[0-9]*\\.?[0-9]*$" string
)
2482 (= (length (substring string
(match-beginning 0)
2485 (string-to-number string
)))
2487 (defun org-babel-import-elisp-from-file (file-name &optional separator
)
2488 "Read the results located at FILE-NAME into an elisp table.
2489 If the table is trivial, then return it as a scalar."
2491 (save-window-excursion
2495 (org-table-import file-name separator
)
2496 (delete-file file-name
)
2497 (setq result
(mapcar (lambda (row)
2498 (mapcar #'org-babel-string-read row
))
2499 (org-table-to-lisp))))
2500 (error (message "Error reading results: %s" err
) nil
)))
2501 (if (null (cdr result
)) ;; if result is trivial vector, then scalarize it
2502 (if (consp (car result
))
2503 (if (null (cdr (car result
)))
2509 (defun org-babel-string-read (cell)
2510 "Strip nested \"s from around strings."
2511 (org-babel-read (or (and (stringp cell
)
2512 (string-match "\\\"\\(.+\\)\\\"" cell
)
2513 (match-string 1 cell
))
2516 (defun org-babel-reverse-string (string)
2517 "Return the reverse of STRING."
2518 (apply 'string
(reverse (string-to-list string
))))
2520 (defun org-babel-chomp (string &optional regexp
)
2521 "Strip trailing spaces and carriage returns from STRING.
2522 Default regexp used is \"[ \f\t\n\r\v]\" but can be
2523 overwritten by specifying a regexp as a second argument."
2524 (let ((regexp (or regexp
"[ \f\t\n\r\v]")))
2525 (while (and (> (length string
) 0)
2526 (string-match regexp
(substring string -
1)))
2527 (setq string
(substring string
0 -
1)))
2530 (defun org-babel-trim (string &optional regexp
)
2531 "Strip leading and trailing spaces and carriage returns from STRING.
2532 Like `org-babel-chomp' only it runs on both the front and back
2534 (org-babel-chomp (org-babel-reverse-string
2535 (org-babel-chomp (org-babel-reverse-string string
) regexp
))
2538 (defvar org-babel-org-babel-call-process-region-original nil
)
2539 (defun org-babel-tramp-handle-call-process-region
2540 (start end program
&optional delete buffer display
&rest args
)
2541 "Use Tramp to handle `call-process-region'.
2542 Fixes a bug in `tramp-handle-call-process-region'."
2543 (if (and (featurep 'tramp
) (file-remote-p default-directory
))
2544 (let ((tmpfile (tramp-compat-make-temp-file "")))
2545 (write-region start end tmpfile
)
2546 (when delete
(delete-region start end
))
2548 ;; (apply 'call-process program tmpfile buffer display args)
2550 (apply 'process-file program tmpfile buffer display args
)
2551 (delete-file tmpfile
)))
2552 ;; org-babel-call-process-region-original is the original emacs
2553 ;; definition. It is in scope from the let binding in
2554 ;; org-babel-execute-src-block
2555 (apply org-babel-call-process-region-original
2556 start end program delete buffer display args
)))
2558 (defun org-babel-local-file-name (file)
2559 "Return the local name component of FILE."
2560 (if (file-remote-p file
)
2562 (with-parsed-tramp-file-name file nil
2566 (defun org-babel-process-file-name (name &optional no-quote-p
)
2567 "Prepare NAME to be used in an external process.
2568 If NAME specifies a remote location, the remote portion of the
2569 name is removed, since in that case the process will be executing
2570 remotely. The file name is then processed by `expand-file-name'.
2571 Unless second argument NO-QUOTE-P is non-nil, the file name is
2572 additionally processed by `shell-quote-argument'"
2573 ((lambda (f) (if no-quote-p f
(shell-quote-argument f
)))
2574 (expand-file-name (org-babel-local-file-name name
))))
2576 (defvar org-babel-temporary-directory
)
2577 (unless (or noninteractive
(boundp 'org-babel-temporary-directory
))
2578 (defvar org-babel-temporary-directory
2579 (or (and (boundp 'org-babel-temporary-directory
)
2580 (file-exists-p org-babel-temporary-directory
)
2581 org-babel-temporary-directory
)
2582 (make-temp-file "babel-" t
))
2583 "Directory to hold temporary files created to execute code blocks.
2584 Used by `org-babel-temp-file'. This directory will be removed on
2587 (defmacro org-babel-result-cond
(result-params scalar-form
&rest table-forms
)
2588 "Call the code to parse raw string results according to RESULT-PARAMS."
2589 (declare (indent 1))
2590 `(unless (member "none" ,result-params
)
2591 (if (or (member "scalar" ,result-params
)
2592 (member "verbatim" ,result-params
)
2593 (member "html" ,result-params
)
2594 (member "code" ,result-params
)
2595 (member "pp" ,result-params
)
2596 (and (member "output" ,result-params
)
2597 (not (member "table" ,result-params
))))
2601 (defun org-babel-temp-file (prefix &optional suffix
)
2602 "Create a temporary file in the `org-babel-temporary-directory'.
2603 Passes PREFIX and SUFFIX directly to `make-temp-file' with the
2604 value of `temporary-file-directory' temporarily set to the value
2605 of `org-babel-temporary-directory'."
2606 (let ((temporary-file-directory
2607 (if (file-remote-p default-directory
)
2608 (concat (file-remote-p default-directory
) "/tmp")
2609 (or (and (boundp 'org-babel-temporary-directory
)
2610 (file-exists-p org-babel-temporary-directory
)
2611 org-babel-temporary-directory
)
2612 temporary-file-directory
))))
2613 (make-temp-file prefix nil suffix
)))
2615 (defun org-babel-remove-temporary-directory ()
2616 "Remove `org-babel-temporary-directory' on Emacs shutdown."
2617 (when (and (boundp 'org-babel-temporary-directory
)
2618 (file-exists-p org-babel-temporary-directory
))
2619 ;; taken from `delete-directory' in files.el
2622 (mapc (lambda (file)
2623 ;; This test is equivalent to
2624 ;; (and (file-directory-p fn) (not (file-symlink-p fn)))
2625 ;; but more efficient
2626 (if (eq t
(car (file-attributes file
)))
2627 (delete-directory file
)
2628 (delete-file file
)))
2629 ;; We do not want to delete "." and "..".
2630 (directory-files org-babel-temporary-directory
'full
2631 "^\\([^.]\\|\\.\\([^.]\\|\\..\\)\\).*"))
2632 (delete-directory org-babel-temporary-directory
))
2634 (message "Failed to remove temporary Org-babel directory %s"
2635 (if (boundp 'org-babel-temporary-directory
)
2636 org-babel-temporary-directory
2637 "[directory not defined]"))))))
2639 (add-hook 'kill-emacs-hook
'org-babel-remove-temporary-directory
)
2644 ;; generated-autoload-file: "org-loaddefs.el"
2647 ;;; ob-core.el ends here