evaluate elisp header args at original call site
[org-mode.git] / lisp / ob-core.el
blobc5ed93998726fa9b92964750a973e08ae3f54917
1 ;;; ob-core.el --- working with code blocks in org-mode
3 ;; Copyright (C) 2009-2013 Free Software Foundation, Inc.
5 ;; Authors: Eric Schulte
6 ;; Dan Davison
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/>.
25 ;;; Code:
26 (eval-when-compile
27 (require 'cl))
28 (require 'ob-eval)
29 (require 'org-macs)
30 (require 'org-compat)
32 (defconst org-babel-exeext
33 (if (memq system-type '(windows-nt cygwin))
34 ".exe"
35 nil))
36 ;; dynamically scoped for tramp
37 (defvar org-babel-call-process-region-original nil)
38 (defvar org-src-lang-modes)
39 (defvar org-babel-library-of-babel)
40 (declare-function show-all "outline" ())
41 (declare-function org-reduce "org" (CL-FUNC CL-SEQ &rest CL-KEYS))
42 (declare-function org-mark-ring-push "org" (&optional pos buffer))
43 (declare-function tramp-compat-make-temp-file "tramp-compat"
44 (filename &optional dir-flag))
45 (declare-function tramp-dissect-file-name "tramp" (name &optional nodefault))
46 (declare-function tramp-file-name-user "tramp" (vec))
47 (declare-function tramp-file-name-host "tramp" (vec))
48 (declare-function with-parsed-tramp-file-name "tramp" (filename var &rest body))
49 (declare-function org-icompleting-read "org" (&rest args))
50 (declare-function org-edit-src-code "org-src"
51 (&optional context code edit-buffer-name quietp))
52 (declare-function org-edit-src-exit "org-src" (&optional context))
53 (declare-function org-open-at-point "org" (&optional in-emacs reference-buffer))
54 (declare-function org-save-outline-visibility "org-macs" (use-markers &rest body))
55 (declare-function org-outline-overlay-data "org" (&optional use-markers))
56 (declare-function org-set-outline-overlay-data "org" (data))
57 (declare-function org-narrow-to-subtree "org" ())
58 (declare-function org-split-string "org" (string &optional separators))
59 (declare-function org-entry-get "org"
60 (pom property &optional inherit literal-nil))
61 (declare-function org-make-options-regexp "org" (kwds &optional extra))
62 (declare-function org-do-remove-indentation "org" (&optional n))
63 (declare-function org-next-block "org" (arg &optional backward block-regexp))
64 (declare-function org-previous-block "org" (arg &optional block-regexp))
65 (declare-function org-show-context "org" (&optional key))
66 (declare-function org-at-table-p "org" (&optional table-type))
67 (declare-function org-cycle "org" (&optional arg))
68 (declare-function org-uniquify "org" (list))
69 (declare-function org-current-level "org" ())
70 (declare-function org-table-import "org-table" (file arg))
71 (declare-function org-add-hook "org-compat"
72 (hook function &optional append local))
73 (declare-function org-table-align "org-table" ())
74 (declare-function org-table-end "org-table" (&optional table-type))
75 (declare-function orgtbl-to-generic "org-table" (table params))
76 (declare-function orgtbl-to-orgtbl "org-table" (table params))
77 (declare-function org-babel-tangle-comment-links "ob-tangle" (&optional info))
78 (declare-function org-babel-lob-get-info "ob-lob" nil)
79 (declare-function org-babel-ref-split-args "ob-ref" (arg-string))
80 (declare-function org-babel-ref-parse "ob-ref" (assignment))
81 (declare-function org-babel-ref-resolve "ob-ref" (ref))
82 (declare-function org-babel-ref-goto-headline-id "ob-ref" (id))
83 (declare-function org-babel-ref-headline-body "ob-ref" ())
84 (declare-function org-babel-lob-execute-maybe "ob-lob" ())
85 (declare-function org-number-sequence "org-compat" (from &optional to inc))
86 (declare-function org-at-item-p "org-list" ())
87 (declare-function org-list-parse-list "org-list" (&optional delete))
88 (declare-function org-list-to-generic "org-list" (LIST PARAMS))
89 (declare-function org-list-struct "org-list" ())
90 (declare-function org-list-prevs-alist "org-list" (struct))
91 (declare-function org-list-get-list-end "org-list" (item struct prevs))
92 (declare-function org-remove-if "org" (predicate seq))
93 (declare-function org-completing-read "org" (&rest args))
94 (declare-function org-escape-code-in-region "org-src" (beg end))
95 (declare-function org-unescape-code-in-string "org-src" (s))
96 (declare-function org-table-to-lisp "org-table" (&optional txt))
97 (declare-function org-reverse-string "org" (string))
99 (defgroup org-babel nil
100 "Code block evaluation and management in `org-mode' documents."
101 :tag "Babel"
102 :group 'org)
104 (defcustom org-confirm-babel-evaluate t
105 "Confirm before evaluation.
106 Require confirmation before interactively evaluating code
107 blocks in Org-mode buffers. The default value of this variable
108 is t, meaning confirmation is required for any code block
109 evaluation. This variable can be set to nil to inhibit any
110 future confirmation requests. This variable can also be set to a
111 function which takes two arguments the language of the code block
112 and the body of the code block. Such a function should then
113 return a non-nil value if the user should be prompted for
114 execution or nil if no prompt is required.
116 Warning: Disabling confirmation may result in accidental
117 evaluation of potentially harmful code. It may be advisable
118 remove code block execution from C-c C-c as further protection
119 against accidental code block evaluation. The
120 `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can be used to
121 remove code block execution from the C-c C-c keybinding."
122 :group 'org-babel
123 :version "24.1"
124 :type '(choice boolean function))
125 ;; don't allow this variable to be changed through file settings
126 (put 'org-confirm-babel-evaluate 'safe-local-variable (lambda (x) (eq x t)))
128 (defcustom org-babel-no-eval-on-ctrl-c-ctrl-c nil
129 "Remove code block evaluation from the C-c C-c key binding."
130 :group 'org-babel
131 :version "24.1"
132 :type 'boolean)
134 (defcustom org-babel-results-keyword "RESULTS"
135 "Keyword used to name results generated by code blocks.
136 Should be either RESULTS or NAME however any capitalization may
137 be used."
138 :group 'org-babel
139 :version "24.4"
140 :package-version '(Org . "8.0")
141 :type 'string)
143 (defcustom org-babel-noweb-wrap-start "<<"
144 "String used to begin a noweb reference in a code block.
145 See also `org-babel-noweb-wrap-end'."
146 :group 'org-babel
147 :type 'string)
149 (defcustom org-babel-noweb-wrap-end ">>"
150 "String used to end a noweb reference in a code block.
151 See also `org-babel-noweb-wrap-start'."
152 :group 'org-babel
153 :type 'string)
155 (defcustom org-babel-inline-result-wrap "=%s="
156 "Format string used to wrap inline results.
157 This string must include a \"%s\" which will be replaced by the results."
158 :group 'org-babel
159 :type 'string)
161 (defun org-babel-noweb-wrap (&optional regexp)
162 (concat org-babel-noweb-wrap-start
163 (or regexp "\\([^ \t\n].+?[^ \t]\\|[^ \t\n]\\)")
164 org-babel-noweb-wrap-end))
166 (defvar org-babel-src-name-regexp
167 "^[ \t]*#\\+name:[ \t]*"
168 "Regular expression used to match a source name line.")
170 (defvar org-babel-multi-line-header-regexp
171 "^[ \t]*#\\+headers?:[ \t]*\\([^\n]*\\)$"
172 "Regular expression used to match multi-line header arguments.")
174 (defvar org-babel-src-name-w-name-regexp
175 (concat org-babel-src-name-regexp
176 "\\("
177 org-babel-multi-line-header-regexp
178 "\\)*"
179 "\\([^ ()\f\t\n\r\v]+\\)")
180 "Regular expression matching source name lines with a name.")
182 (defvar org-babel-src-block-regexp
183 (concat
184 ;; (1) indentation (2) lang
185 "^\\([ \t]*\\)#\\+begin_src[ \t]+\\([^ \f\t\n\r\v]+\\)[ \t]*"
186 ;; (3) switches
187 "\\([^\":\n]*\"[^\"\n*]*\"[^\":\n]*\\|[^\":\n]*\\)"
188 ;; (4) header arguments
189 "\\([^\n]*\\)\n"
190 ;; (5) body
191 "\\([^\000]*?\n\\)?[ \t]*#\\+end_src")
192 "Regexp used to identify code blocks.")
194 (defvar org-babel-inline-src-block-regexp
195 (concat
196 ;; (1) replacement target (2) lang
197 "\\(?:^\\|[^-[:alnum:]]\\)\\(src_\\([^ \f\t\n\r\v]+\\)"
198 ;; (3,4) (unused, headers)
199 "\\(\\|\\[\\(.*?\\)\\]\\)"
200 ;; (5) body
201 "{\\([^\f\n\r\v]+?\\)}\\)")
202 "Regexp used to identify inline src-blocks.")
204 (defun org-babel-get-header (params key &optional others)
205 "Select only header argument of type KEY from a list.
206 Optional argument OTHERS indicates that only the header that do
207 not match KEY should be returned."
208 (delq nil
209 (mapcar
210 (lambda (p) (when (funcall (if others #'not #'identity) (eq (car p) key)) p))
211 params)))
213 (defun org-babel-get-inline-src-block-matches()
214 "Set match data if within body of an inline source block.
215 Returns non-nil if match-data set"
216 (let ((src-at-0-p (save-excursion
217 (beginning-of-line 1)
218 (string= "src" (thing-at-point 'word))))
219 (first-line-p (= 1 (line-number-at-pos)))
220 (orig (point)))
221 (let ((search-for (cond ((and src-at-0-p first-line-p "src_"))
222 (first-line-p "[[:punct:] \t]src_")
223 (t "[[:punct:] \f\t\n\r\v]src_")))
224 (lower-limit (if first-line-p
226 (- (point-at-bol) 1))))
227 (save-excursion
228 (when (or (and src-at-0-p (bobp))
229 (and (re-search-forward "}" (point-at-eol) t)
230 (re-search-backward search-for lower-limit t)
231 (> orig (point))))
232 (when (looking-at org-babel-inline-src-block-regexp)
233 t ))))))
235 (defvar org-babel-inline-lob-one-liner-regexp)
236 (defun org-babel-get-lob-one-liner-matches()
237 "Set match data if on line of an lob one liner.
238 Returns non-nil if match-data set"
239 (save-excursion
240 (unless (= (point) (point-at-bol)) ;; move before inline block
241 (re-search-backward "[ \f\t\n\r\v]" nil t))
242 (if (looking-at org-babel-inline-lob-one-liner-regexp)
244 nil)))
246 (defun org-babel-get-src-block-info (&optional light)
247 "Get information on the current source block.
249 Optional argument LIGHT does not resolve remote variable
250 references; a process which could likely result in the execution
251 of other code blocks.
253 Returns a list
254 (language body header-arguments-alist switches name indent block-head)."
255 (let ((case-fold-search t) head info name indent)
256 ;; full code block
257 (if (setq head (org-babel-where-is-src-block-head))
258 (save-excursion
259 (goto-char head)
260 (setq info (org-babel-parse-src-block-match))
261 (setq indent (nth 5 info))
262 (setq info (butlast info))
263 (while (and (forward-line -1)
264 (looking-at org-babel-multi-line-header-regexp))
265 (setf (nth 2 info)
266 (org-babel-merge-params
267 (nth 2 info)
268 (org-babel-parse-header-arguments (match-string 1)))))
269 (when (looking-at org-babel-src-name-w-name-regexp)
270 (setq name (org-no-properties (match-string 3)))))
271 ;; inline source block
272 (when (org-babel-get-inline-src-block-matches)
273 (setq info (org-babel-parse-inline-src-block-match))))
274 ;; resolve variable references and add summary parameters
275 (when (and info (not light))
276 (setf (nth 2 info) (org-babel-process-params (nth 2 info))))
277 (when info (append info (list name indent head)))))
279 (defvar org-current-export-file) ; dynamically bound
280 (defmacro org-babel-check-confirm-evaluate (info &rest body)
281 "Evaluate BODY with special execution confirmation variables set.
283 Specifically; NOEVAL will indicate if evaluation is allowed,
284 QUERY will indicate if a user query is required, CODE-BLOCK will
285 hold the language of the code block, and BLOCK-NAME will hold the
286 name of the code block."
287 (declare (indent defun))
288 (org-with-gensyms
289 (lang block-body headers name eval eval-no export eval-no-export)
290 `(let* ((,lang (nth 0 ,info))
291 (,block-body (nth 1 ,info))
292 (,headers (nth 2 ,info))
293 (,name (nth 4 ,info))
294 (,eval (or (cdr (assoc :eval ,headers))
295 (when (assoc :noeval ,headers) "no")))
296 (,eval-no (or (equal ,eval "no")
297 (equal ,eval "never")))
298 (,export (org-bound-and-true-p org-current-export-file))
299 (,eval-no-export (and ,export (or (equal ,eval "no-export")
300 (equal ,eval "never-export"))))
301 (noeval (or ,eval-no ,eval-no-export))
302 (query (or (equal ,eval "query")
303 (and ,export (equal ,eval "query-export"))
304 (when (functionp org-confirm-babel-evaluate)
305 (funcall org-confirm-babel-evaluate
306 ,lang ,block-body))
307 org-confirm-babel-evaluate))
308 (code-block (if ,info (format " %s " ,lang) " "))
309 (block-name (if ,name (format " (%s) " ,name) " ")))
310 ,@body)))
312 (defsubst org-babel-check-evaluate (info)
313 "Check if code block INFO should be evaluated.
314 Do not query the user."
315 (org-babel-check-confirm-evaluate info
316 (not (when noeval
317 (message (format "Evaluation of this%scode-block%sis disabled."
318 code-block block-name))))))
320 ;; dynamically scoped for asynchroneous export
321 (defvar org-babel-confirm-evaluate-answer-no)
323 (defsubst org-babel-confirm-evaluate (info)
324 "Confirm evaluation of the code block INFO.
326 If the variable `org-babel-confirm-evaluate-answer-no' is bound
327 to a non-nil value, auto-answer with \"no\".
329 This query can also be suppressed by setting the value of
330 `org-confirm-babel-evaluate' to nil, in which case all future
331 interactive code block evaluations will proceed without any
332 confirmation from the user.
334 Note disabling confirmation may result in accidental evaluation
335 of potentially harmful code."
336 (org-babel-check-confirm-evaluate info
337 (not (when query
338 (unless
339 (and (not (org-bound-and-true-p
340 org-babel-confirm-evaluate-answer-no))
341 (yes-or-no-p
342 (format "Evaluate this%scode block%son your system? "
343 code-block block-name)))
344 (message (format "Evaluation of this%scode-block%sis aborted."
345 code-block block-name)))))))
347 ;;;###autoload
348 (defun org-babel-execute-safely-maybe ()
349 (unless org-babel-no-eval-on-ctrl-c-ctrl-c
350 (org-babel-execute-maybe)))
352 (add-hook 'org-ctrl-c-ctrl-c-hook 'org-babel-execute-safely-maybe)
354 ;;;###autoload
355 (defun org-babel-execute-maybe ()
356 (interactive)
357 (or (org-babel-execute-src-block-maybe)
358 (org-babel-lob-execute-maybe)))
360 (defmacro org-babel-when-in-src-block (&rest body)
361 "Execute BODY if point is in a source block and return t.
363 Otherwise do nothing and return nil."
364 `(if (or (org-babel-where-is-src-block-head)
365 (org-babel-get-inline-src-block-matches))
366 (progn
367 ,@body
369 nil))
371 (defun org-babel-execute-src-block-maybe ()
372 "Conditionally execute a source block.
373 Detect if this is context for a Babel src-block and if so
374 then run `org-babel-execute-src-block'."
375 (interactive)
376 (org-babel-when-in-src-block
377 (org-babel-eval-wipe-error-buffer)
378 (org-babel-execute-src-block current-prefix-arg)))
380 ;;;###autoload
381 (defun org-babel-view-src-block-info ()
382 "Display information on the current source block.
383 This includes header arguments, language and name, and is largely
384 a window into the `org-babel-get-src-block-info' function."
385 (interactive)
386 (let ((info (org-babel-get-src-block-info 'light))
387 (full (lambda (it) (> (length it) 0)))
388 (printf (lambda (fmt &rest args) (princ (apply #'format fmt args)))))
389 (when info
390 (with-help-window (help-buffer)
391 (let ((name (nth 4 info))
392 (lang (nth 0 info))
393 (switches (nth 3 info))
394 (header-args (nth 2 info)))
395 (when name (funcall printf "Name: %s\n" name))
396 (when lang (funcall printf "Lang: %s\n" lang))
397 (when (funcall full switches) (funcall printf "Switches: %s\n" switches))
398 (funcall printf "Header Arguments:\n")
399 (dolist (pair (sort header-args
400 (lambda (a b) (string< (symbol-name (car a))
401 (symbol-name (car b))))))
402 (when (funcall full (cdr pair))
403 (funcall printf "\t%S%s\t%s\n"
404 (car pair)
405 (if (> (length (format "%S" (car pair))) 7) "" "\t")
406 (cdr pair)))))))))
408 ;;;###autoload
409 (defun org-babel-expand-src-block-maybe ()
410 "Conditionally expand a source block.
411 Detect if this is context for a org-babel src-block and if so
412 then run `org-babel-expand-src-block'."
413 (interactive)
414 (org-babel-when-in-src-block
415 (org-babel-expand-src-block current-prefix-arg)))
417 ;;;###autoload
418 (defun org-babel-load-in-session-maybe ()
419 "Conditionally load a source block in a session.
420 Detect if this is context for a org-babel src-block and if so
421 then run `org-babel-load-in-session'."
422 (interactive)
423 (org-babel-when-in-src-block
424 (org-babel-load-in-session current-prefix-arg)))
426 (add-hook 'org-metaup-hook 'org-babel-load-in-session-maybe)
428 ;;;###autoload
429 (defun org-babel-pop-to-session-maybe ()
430 "Conditionally pop to a session.
431 Detect if this is context for a org-babel src-block and if so
432 then run `org-babel-switch-to-session'."
433 (interactive)
434 (org-babel-when-in-src-block
435 (org-babel-switch-to-session current-prefix-arg)))
437 (add-hook 'org-metadown-hook 'org-babel-pop-to-session-maybe)
439 (defconst org-babel-common-header-args-w-values
440 '((cache . ((no yes)))
441 (cmdline . :any)
442 (colnames . ((nil no yes)))
443 (comments . ((no link yes org both noweb)))
444 (dir . :any)
445 (eval . ((never query)))
446 (exports . ((code results both none)))
447 (epilogue . :any)
448 (file . :any)
449 (file-desc . :any)
450 (hlines . ((no yes)))
451 (mkdirp . ((yes no)))
452 (no-expand)
453 (noeval)
454 (noweb . ((yes no tangle no-export strip-export)))
455 (noweb-ref . :any)
456 (noweb-sep . :any)
457 (padline . ((yes no)))
458 (post . :any)
459 (prologue . :any)
460 (results . ((file list vector table scalar verbatim)
461 (raw html latex org code pp drawer)
462 (replace silent none append prepend)
463 (output value)))
464 (rownames . ((no yes)))
465 (sep . :any)
466 (session . :any)
467 (shebang . :any)
468 (tangle . ((tangle yes no :any)))
469 (tangle-mode . ((#o755 #o555 #o444 :any)))
470 (var . :any)
471 (wrap . :any)))
473 (defconst org-babel-header-arg-names
474 (mapcar #'car org-babel-common-header-args-w-values)
475 "Common header arguments used by org-babel.
476 Note that individual languages may define their own language
477 specific header arguments as well.")
479 (defvar org-babel-default-header-args
480 '((:session . "none") (:results . "replace") (:exports . "code")
481 (:cache . "no") (:noweb . "no") (:hlines . "no") (:tangle . "no")
482 (:padnewline . "yes"))
483 "Default arguments to use when evaluating a source block.")
485 (defvar org-babel-default-inline-header-args
486 '((:session . "none") (:results . "replace") (:exports . "results"))
487 "Default arguments to use when evaluating an inline source block.")
489 (defvar org-babel-data-names '("tblname" "results" "name"))
491 (defvar org-babel-result-regexp
492 (concat "^[ \t]*#\\+"
493 (regexp-opt org-babel-data-names t)
494 "\\(\\[\\("
495 ;; FIXME The string below is `org-ts-regexp'
496 "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
497 " \\)?\\([[:alnum:]]+\\)\\]\\)?\\:[ \t]*")
498 "Regular expression used to match result lines.
499 If the results are associated with a hash key then the hash will
500 be saved in the second match data.")
502 (defvar org-babel-result-w-name-regexp
503 (concat org-babel-result-regexp
504 "\\([^ ()\f\t\n\r\v]+\\)\\(\(\\(.*\\)\)\\|\\)"))
506 (defvar org-babel-min-lines-for-block-output 10
507 "The minimum number of lines for block output.
508 If number of lines of output is equal to or exceeds this
509 value, the output is placed in a #+begin_example...#+end_example
510 block. Otherwise the output is marked as literal by inserting
511 colons at the starts of the lines. This variable only takes
512 effect if the :results output option is in effect.")
514 (defvar org-babel-noweb-error-langs nil
515 "Languages for which Babel will raise literate programming errors.
516 List of languages for which errors should be raised when the
517 source code block satisfying a noweb reference in this language
518 can not be resolved.")
520 (defvar org-babel-hash-show 4
521 "Number of initial characters to show of a hidden results hash.")
523 (defvar org-babel-hash-show-time nil
524 "Non-nil means show the time the code block was evaluated in the result hash.")
526 (defvar org-babel-after-execute-hook nil
527 "Hook for functions to be called after `org-babel-execute-src-block'")
529 (defun org-babel-named-src-block-regexp-for-name (name)
530 "This generates a regexp used to match a src block named NAME."
531 (concat org-babel-src-name-regexp (regexp-quote name)
532 "[ \t(]*[\r\n]\\(?:^#.*[\r\n]\\)*"
533 (substring org-babel-src-block-regexp 1)))
535 (defun org-babel-named-data-regexp-for-name (name)
536 "This generates a regexp used to match data named NAME."
537 (concat org-babel-result-regexp (regexp-quote name) "\\([ \t]\\|$\\)"))
539 ;;; functions
540 (defvar call-process-region)
541 (defvar org-babel-current-src-block-location nil
542 "Marker pointing to the src block currently being executed.
543 This may also point to a call line or an inline code block. If
544 multiple blocks are being executed (e.g., in chained execution
545 through use of the :var header argument) this marker points to
546 the outer-most code block.")
548 ;;;###autoload
549 (defun org-babel-execute-src-block (&optional arg info params)
550 "Execute the current source code block.
551 Insert the results of execution into the buffer. Source code
552 execution and the collection and formatting of results can be
553 controlled through a variety of header arguments.
555 With prefix argument ARG, force re-execution even if an existing
556 result cached in the buffer would otherwise have been returned.
558 Optionally supply a value for INFO in the form returned by
559 `org-babel-get-src-block-info'.
561 Optionally supply a value for PARAMS which will be merged with
562 the header arguments specified at the front of the source code
563 block."
564 (interactive)
565 (let* ((org-babel-current-src-block-location
566 (or org-babel-current-src-block-location
567 (nth 6 info)
568 (org-babel-where-is-src-block-head)))
569 (info (if info
570 (copy-tree info)
571 (org-babel-get-src-block-info)))
572 (merged-params (org-babel-merge-params (nth 2 info) params)))
573 (when (org-babel-check-evaluate
574 (let ((i info)) (setf (nth 2 i) merged-params) i))
575 (let* ((params (if params
576 (org-babel-process-params merged-params)
577 (nth 2 info)))
578 (cachep (and (not arg) (cdr (assoc :cache params))
579 (string= "yes" (cdr (assoc :cache params)))))
580 (new-hash (when cachep (org-babel-sha1-hash info)))
581 (old-hash (when cachep (org-babel-current-result-hash)))
582 (cache-current-p (and (not arg) new-hash
583 (equal new-hash old-hash))))
584 (cond
585 (cache-current-p
586 (save-excursion ;; return cached result
587 (goto-char (org-babel-where-is-src-block-result nil info))
588 (end-of-line 1) (forward-char 1)
589 (let ((result (org-babel-read-result)))
590 (message (replace-regexp-in-string
591 "%" "%%" (format "%S" result))) result)))
592 ((org-babel-confirm-evaluate
593 (let ((i info)) (setf (nth 2 i) merged-params) i))
594 (let* ((lang (nth 0 info))
595 (result-params (cdr (assoc :result-params params)))
596 (body (setf (nth 1 info)
597 (if (org-babel-noweb-p params :eval)
598 (org-babel-expand-noweb-references info)
599 (nth 1 info))))
600 (dir (cdr (assoc :dir params)))
601 (default-directory
602 (or (and dir (file-name-as-directory (expand-file-name dir)))
603 default-directory))
604 (org-babel-call-process-region-original ;; for tramp handler
605 (or (org-bound-and-true-p
606 org-babel-call-process-region-original)
607 (symbol-function 'call-process-region)))
608 (indent (nth 5 info))
609 result cmd)
610 (unwind-protect
611 (let ((call-process-region
612 (lambda (&rest args)
613 (apply 'org-babel-tramp-handle-call-process-region
614 args))))
615 (let ((lang-check
616 (lambda (f)
617 (let ((f (intern (concat "org-babel-execute:" f))))
618 (when (fboundp f) f)))))
619 (setq cmd
620 (or (funcall lang-check lang)
621 (funcall lang-check
622 (symbol-name
623 (cdr (assoc lang org-src-lang-modes))))
624 (error "No org-babel-execute function for %s!"
625 lang))))
626 (message "executing %s code block%s..."
627 (capitalize lang)
628 (if (nth 4 info) (format " (%s)" (nth 4 info)) ""))
629 (if (member "none" result-params)
630 (progn
631 (funcall cmd body params)
632 (message "result silenced")
633 (setq result nil))
634 (setq result
635 ((lambda (result)
636 (if (and (eq (cdr (assoc :result-type params))
637 'value)
638 (or (member "vector" result-params)
639 (member "table" result-params))
640 (not (listp result)))
641 (list (list result)) result))
642 (funcall cmd body params)))
643 ;; if non-empty result and :file then write to :file
644 (when (cdr (assoc :file params))
645 (when result
646 (with-temp-file (cdr (assoc :file params))
647 (insert
648 (org-babel-format-result
649 result (cdr (assoc :sep (nth 2 info)))))))
650 (setq result (cdr (assoc :file params))))
651 ;; possibly perform post process provided its appropriate
652 (when (cdr (assoc :post params))
653 (let ((*this* (if (cdr (assoc :file params))
654 (org-babel-result-to-file
655 (cdr (assoc :file params))
656 (when (assoc :file-desc params)
657 (or (cdr (assoc :file-desc params))
658 result)))
659 result)))
660 (setq result (org-babel-ref-resolve
661 (cdr (assoc :post params))))
662 (when (cdr (assoc :file params))
663 (setq result-params
664 (remove "file" result-params)))))
665 (org-babel-insert-result
666 result result-params info new-hash indent lang))
667 (run-hooks 'org-babel-after-execute-hook)
668 result)
669 (setq call-process-region
670 'org-babel-call-process-region-original)))))))))
672 (defun org-babel-expand-body:generic (body params &optional var-lines)
673 "Expand BODY with PARAMS.
674 Expand a block of code with org-babel according to its header
675 arguments. This generic implementation of body expansion is
676 called for languages which have not defined their own specific
677 org-babel-expand-body:lang function."
678 (let ((pro (cdr (assoc :prologue params)))
679 (epi (cdr (assoc :epilogue params))))
680 (mapconcat #'identity
681 (append (when pro (list pro))
682 var-lines
683 (list body)
684 (when epi (list epi)))
685 "\n")))
687 ;;;###autoload
688 (defun org-babel-expand-src-block (&optional arg info params)
689 "Expand the current source code block.
690 Expand according to the source code block's header
691 arguments and pop open the results in a preview buffer."
692 (interactive)
693 (let* ((info (or info (org-babel-get-src-block-info)))
694 (lang (nth 0 info))
695 (params (setf (nth 2 info)
696 (sort (org-babel-merge-params (nth 2 info) params)
697 (lambda (el1 el2) (string< (symbol-name (car el1))
698 (symbol-name (car el2)))))))
699 (body (setf (nth 1 info)
700 (if (org-babel-noweb-p params :eval)
701 (org-babel-expand-noweb-references info) (nth 1 info))))
702 (expand-cmd (intern (concat "org-babel-expand-body:" lang)))
703 (assignments-cmd (intern (concat "org-babel-variable-assignments:"
704 lang)))
705 (expanded
706 (if (fboundp expand-cmd) (funcall expand-cmd body params)
707 (org-babel-expand-body:generic
708 body params (and (fboundp assignments-cmd)
709 (funcall assignments-cmd params))))))
710 (if (org-called-interactively-p 'any)
711 (org-edit-src-code
712 nil expanded
713 (concat "*Org-Babel Preview " (buffer-name) "[ " lang " ]*"))
714 expanded)))
716 (defun org-babel-edit-distance (s1 s2)
717 "Return the edit (levenshtein) distance between strings S1 S2."
718 (let* ((l1 (length s1))
719 (l2 (length s2))
720 (dist (vconcat (mapcar (lambda (_) (make-vector (1+ l2) nil))
721 (number-sequence 1 (1+ l1)))))
722 (in (lambda (i j) (aref (aref dist i) j))))
723 (setf (aref (aref dist 0) 0) 0)
724 (dolist (j (number-sequence 1 l2))
725 (setf (aref (aref dist 0) j) j))
726 (dolist (i (number-sequence 1 l1))
727 (setf (aref (aref dist i) 0) i)
728 (dolist (j (number-sequence 1 l2))
729 (setf (aref (aref dist i) j)
730 (min
731 (1+ (funcall in (1- i) j))
732 (1+ (funcall in i (1- j)))
733 (+ (if (equal (aref s1 (1- i)) (aref s2 (1- j))) 0 1)
734 (funcall in (1- i) (1- j)))))))
735 (funcall in l1 l2)))
737 (defun org-babel-combine-header-arg-lists (original &rest others)
738 "Combine a number of lists of header argument names and arguments."
739 (let ((results (copy-sequence original)))
740 (dolist (new-list others)
741 (dolist (arg-pair new-list)
742 (let ((header (car arg-pair))
743 (args (cdr arg-pair)))
744 (setq results
745 (cons arg-pair (org-remove-if
746 (lambda (pair) (equal header (car pair)))
747 results))))))
748 results))
750 ;;;###autoload
751 (defun org-babel-check-src-block ()
752 "Check for misspelled header arguments in the current code block."
753 (interactive)
754 ;; TODO: report malformed code block
755 ;; TODO: report incompatible combinations of header arguments
756 ;; TODO: report uninitialized variables
757 (let ((too-close 2) ;; <- control closeness to report potential match
758 (names (mapcar #'symbol-name org-babel-header-arg-names)))
759 (dolist (header (mapcar (lambda (arg) (substring (symbol-name (car arg)) 1))
760 (and (org-babel-where-is-src-block-head)
761 (org-babel-parse-header-arguments
762 (org-no-properties
763 (match-string 4))))))
764 (dolist (name names)
765 (when (and (not (string= header name))
766 (<= (org-babel-edit-distance header name) too-close)
767 (not (member header names)))
768 (error "Supplied header \"%S\" is suspiciously close to \"%S\""
769 header name))))
770 (message "No suspicious header arguments found.")))
772 ;;;###autoload
773 (defun org-babel-insert-header-arg ()
774 "Insert a header argument selecting from lists of common args and values."
775 (interactive)
776 (let* ((lang (car (org-babel-get-src-block-info 'light)))
777 (lang-headers (intern (concat "org-babel-header-args:" lang)))
778 (headers (org-babel-combine-header-arg-lists
779 org-babel-common-header-args-w-values
780 (when (boundp lang-headers) (eval lang-headers))))
781 (arg (org-icompleting-read
782 "Header Arg: "
783 (mapcar
784 (lambda (header-spec) (symbol-name (car header-spec)))
785 headers))))
786 (insert ":" arg)
787 (let ((vals (cdr (assoc (intern arg) headers))))
788 (when vals
789 (insert
791 (cond
792 ((eq vals :any)
793 (read-from-minibuffer "value: "))
794 ((listp vals)
795 (mapconcat
796 (lambda (group)
797 (let ((arg (org-icompleting-read
798 "value: "
799 (cons "default" (mapcar #'symbol-name group)))))
800 (if (and arg (not (string= "default" arg)))
801 (concat arg " ")
802 "")))
803 vals ""))))))))
805 ;; Add support for completing-read insertion of header arguments after ":"
806 (defun org-babel-header-arg-expand ()
807 "Call `org-babel-enter-header-arg-w-completion' in appropriate contexts."
808 (when (and (equal (char-before) ?\:) (org-babel-where-is-src-block-head))
809 (org-babel-enter-header-arg-w-completion (match-string 2))))
811 (defun org-babel-enter-header-arg-w-completion (&optional lang)
812 "Insert header argument appropriate for LANG with completion."
813 (let* ((lang-headers-var (intern (concat "org-babel-header-args:" lang)))
814 (lang-headers (when (boundp lang-headers-var) (eval lang-headers-var)))
815 (headers-w-values (org-babel-combine-header-arg-lists
816 org-babel-common-header-args-w-values lang-headers))
817 (headers (mapcar #'symbol-name (mapcar #'car headers-w-values)))
818 (header (org-completing-read "Header Arg: " headers))
819 (args (cdr (assoc (intern header) headers-w-values)))
820 (arg (when (and args (listp args))
821 (org-completing-read
822 (format "%s: " header)
823 (mapcar #'symbol-name (apply #'append args))))))
824 (insert (concat header " " (or arg "")))
825 (cons header arg)))
827 (add-hook 'org-tab-first-hook 'org-babel-header-arg-expand)
829 ;;;###autoload
830 (defun org-babel-load-in-session (&optional arg info)
831 "Load the body of the current source-code block.
832 Evaluate the header arguments for the source block before
833 entering the session. After loading the body this pops open the
834 session."
835 (interactive)
836 (let* ((info (or info (org-babel-get-src-block-info)))
837 (lang (nth 0 info))
838 (params (nth 2 info))
839 (body (if (not info)
840 (user-error "No src code block at point")
841 (setf (nth 1 info)
842 (if (org-babel-noweb-p params :eval)
843 (org-babel-expand-noweb-references info)
844 (nth 1 info)))))
845 (session (cdr (assoc :session params)))
846 (dir (cdr (assoc :dir params)))
847 (default-directory
848 (or (and dir (file-name-as-directory dir)) default-directory))
849 (cmd (intern (concat "org-babel-load-session:" lang))))
850 (unless (fboundp cmd)
851 (error "No org-babel-load-session function for %s!" lang))
852 (pop-to-buffer (funcall cmd session body params))
853 (end-of-line 1)))
855 ;;;###autoload
856 (defun org-babel-initiate-session (&optional arg info)
857 "Initiate session for current code block.
858 If called with a prefix argument then resolve any variable
859 references in the header arguments and assign these variables in
860 the session. Copy the body of the code block to the kill ring."
861 (interactive "P")
862 (let* ((info (or info (org-babel-get-src-block-info (not arg))))
863 (lang (nth 0 info))
864 (body (nth 1 info))
865 (params (nth 2 info))
866 (session (cdr (assoc :session params)))
867 (dir (cdr (assoc :dir params)))
868 (default-directory
869 (or (and dir (file-name-as-directory dir)) default-directory))
870 (init-cmd (intern (format "org-babel-%s-initiate-session" lang)))
871 (prep-cmd (intern (concat "org-babel-prep-session:" lang))))
872 (if (and (stringp session) (string= session "none"))
873 (error "This block is not using a session!"))
874 (unless (fboundp init-cmd)
875 (error "No org-babel-initiate-session function for %s!" lang))
876 (with-temp-buffer (insert (org-babel-trim body))
877 (copy-region-as-kill (point-min) (point-max)))
878 (when arg
879 (unless (fboundp prep-cmd)
880 (error "No org-babel-prep-session function for %s!" lang))
881 (funcall prep-cmd session params))
882 (funcall init-cmd session params)))
884 ;;;###autoload
885 (defun org-babel-switch-to-session (&optional arg info)
886 "Switch to the session of the current code block.
887 Uses `org-babel-initiate-session' to start the session. If called
888 with a prefix argument then this is passed on to
889 `org-babel-initiate-session'."
890 (interactive "P")
891 (pop-to-buffer (org-babel-initiate-session arg info))
892 (end-of-line 1))
894 (defalias 'org-babel-pop-to-session 'org-babel-switch-to-session)
896 ;;;###autoload
897 (defun org-babel-switch-to-session-with-code (&optional arg info)
898 "Switch to code buffer and display session."
899 (interactive "P")
900 (let ((swap-windows
901 (lambda ()
902 (let ((other-window-buffer (window-buffer (next-window))))
903 (set-window-buffer (next-window) (current-buffer))
904 (set-window-buffer (selected-window) other-window-buffer))
905 (other-window 1)))
906 (info (org-babel-get-src-block-info))
907 (org-src-window-setup 'reorganize-frame))
908 (save-excursion
909 (org-babel-switch-to-session arg info))
910 (org-edit-src-code)
911 (funcall swap-windows)))
913 (defmacro org-babel-do-in-edit-buffer (&rest body)
914 "Evaluate BODY in edit buffer if there is a code block at point.
915 Return t if a code block was found at point, nil otherwise."
916 `(let ((org-src-window-setup 'switch-invisibly))
917 (when (and (org-babel-where-is-src-block-head)
918 (org-edit-src-code nil nil nil))
919 (unwind-protect (progn ,@body)
920 (if (org-bound-and-true-p org-edit-src-from-org-mode)
921 (org-edit-src-exit)))
922 t)))
923 (def-edebug-spec org-babel-do-in-edit-buffer (body))
925 (defun org-babel-do-key-sequence-in-edit-buffer (key)
926 "Read key sequence and execute the command in edit buffer.
927 Enter a key sequence to be executed in the language major-mode
928 edit buffer. For example, TAB will alter the contents of the
929 Org-mode code block according to the effect of TAB in the
930 language major-mode buffer. For languages that support
931 interactive sessions, this can be used to send code from the Org
932 buffer to the session for evaluation using the native major-mode
933 evaluation mechanisms."
934 (interactive "kEnter key-sequence to execute in edit buffer: ")
935 (org-babel-do-in-edit-buffer
936 (call-interactively
937 (key-binding (or key (read-key-sequence nil))))))
939 (defvar org-bracket-link-regexp)
941 ;;;###autoload
942 (defun org-babel-open-src-block-result (&optional re-run)
943 "If `point' is on a src block then open the results of the
944 source code block, otherwise return nil. With optional prefix
945 argument RE-RUN the source-code block is evaluated even if
946 results already exist."
947 (interactive "P")
948 (let ((info (org-babel-get-src-block-info 'light)))
949 (when info
950 (save-excursion
951 ;; go to the results, if there aren't any then run the block
952 (goto-char (or (and (not re-run) (org-babel-where-is-src-block-result))
953 (progn (org-babel-execute-src-block)
954 (org-babel-where-is-src-block-result))))
955 (end-of-line 1)
956 (while (looking-at "[\n\r\t\f ]") (forward-char 1))
957 ;; open the results
958 (if (looking-at org-bracket-link-regexp)
959 ;; file results
960 (org-open-at-point)
961 (let ((r (org-babel-format-result
962 (org-babel-read-result) (cdr (assoc :sep (nth 2 info))))))
963 (pop-to-buffer (get-buffer-create "*Org-Babel Results*"))
964 (delete-region (point-min) (point-max))
965 (insert r)))
966 t))))
968 ;;;###autoload
969 (defmacro org-babel-map-src-blocks (file &rest body)
970 "Evaluate BODY forms on each source-block in FILE.
971 If FILE is nil evaluate BODY forms on source blocks in current
972 buffer. During evaluation of BODY the following local variables
973 are set relative to the currently matched code block.
975 full-block ------- string holding the entirety of the code block
976 beg-block -------- point at the beginning of the code block
977 end-block -------- point at the end of the matched code block
978 lang ------------- string holding the language of the code block
979 beg-lang --------- point at the beginning of the lang
980 end-lang --------- point at the end of the lang
981 switches --------- string holding the switches
982 beg-switches ----- point at the beginning of the switches
983 end-switches ----- point at the end of the switches
984 header-args ------ string holding the header-args
985 beg-header-args -- point at the beginning of the header-args
986 end-header-args -- point at the end of the header-args
987 body ------------- string holding the body of the code block
988 beg-body --------- point at the beginning of the body
989 end-body --------- point at the end of the body"
990 (declare (indent 1))
991 (let ((tempvar (make-symbol "file")))
992 `(let* ((,tempvar ,file)
993 (visited-p (or (null ,tempvar)
994 (get-file-buffer (expand-file-name ,tempvar))))
995 (point (point)) to-be-removed)
996 (save-window-excursion
997 (when ,tempvar (find-file ,tempvar))
998 (setq to-be-removed (current-buffer))
999 (goto-char (point-min))
1000 (while (re-search-forward org-babel-src-block-regexp nil t)
1001 (goto-char (match-beginning 0))
1002 (let ((full-block (match-string 0))
1003 (beg-block (match-beginning 0))
1004 (end-block (match-end 0))
1005 (lang (match-string 2))
1006 (beg-lang (match-beginning 2))
1007 (end-lang (match-end 2))
1008 (switches (match-string 3))
1009 (beg-switches (match-beginning 3))
1010 (end-switches (match-end 3))
1011 (header-args (match-string 4))
1012 (beg-header-args (match-beginning 4))
1013 (end-header-args (match-end 4))
1014 (body (match-string 5))
1015 (beg-body (match-beginning 5))
1016 (end-body (match-end 5)))
1017 ,@body
1018 (goto-char end-block))))
1019 (unless visited-p (kill-buffer to-be-removed))
1020 (goto-char point))))
1021 (def-edebug-spec org-babel-map-src-blocks (form body))
1023 ;;;###autoload
1024 (defmacro org-babel-map-inline-src-blocks (file &rest body)
1025 "Evaluate BODY forms on each inline source-block in FILE.
1026 If FILE is nil evaluate BODY forms on source blocks in current
1027 buffer."
1028 (declare (indent 1))
1029 (let ((tempvar (make-symbol "file")))
1030 `(let* ((,tempvar ,file)
1031 (visited-p (or (null ,tempvar)
1032 (get-file-buffer (expand-file-name ,tempvar))))
1033 (point (point)) to-be-removed)
1034 (save-window-excursion
1035 (when ,tempvar (find-file ,tempvar))
1036 (setq to-be-removed (current-buffer))
1037 (goto-char (point-min))
1038 (while (re-search-forward org-babel-inline-src-block-regexp nil t)
1039 (goto-char (match-beginning 1))
1040 (save-match-data ,@body)
1041 (goto-char (match-end 0))))
1042 (unless visited-p (kill-buffer to-be-removed))
1043 (goto-char point))))
1044 (def-edebug-spec org-babel-map-inline-src-blocks (form body))
1046 (defvar org-babel-lob-one-liner-regexp)
1048 ;;;###autoload
1049 (defmacro org-babel-map-call-lines (file &rest body)
1050 "Evaluate BODY forms on each call line in FILE.
1051 If FILE is nil evaluate BODY forms on source blocks in current
1052 buffer."
1053 (declare (indent 1))
1054 (let ((tempvar (make-symbol "file")))
1055 `(let* ((,tempvar ,file)
1056 (visited-p (or (null ,tempvar)
1057 (get-file-buffer (expand-file-name ,tempvar))))
1058 (point (point)) to-be-removed)
1059 (save-window-excursion
1060 (when ,tempvar (find-file ,tempvar))
1061 (setq to-be-removed (current-buffer))
1062 (goto-char (point-min))
1063 (while (re-search-forward org-babel-lob-one-liner-regexp nil t)
1064 (goto-char (match-beginning 1))
1065 (save-match-data ,@body)
1066 (goto-char (match-end 0))))
1067 (unless visited-p (kill-buffer to-be-removed))
1068 (goto-char point))))
1069 (def-edebug-spec org-babel-map-call-lines (form body))
1071 ;;;###autoload
1072 (defmacro org-babel-map-executables (file &rest body)
1073 (declare (indent 1))
1074 (let ((tempvar (make-symbol "file"))
1075 (rx (make-symbol "rx")))
1076 `(let* ((,tempvar ,file)
1077 (,rx (concat "\\(" org-babel-src-block-regexp
1078 "\\|" org-babel-inline-src-block-regexp
1079 "\\|" org-babel-lob-one-liner-regexp "\\)"))
1080 (visited-p (or (null ,tempvar)
1081 (get-file-buffer (expand-file-name ,tempvar))))
1082 (point (point)) to-be-removed)
1083 (save-window-excursion
1084 (when ,tempvar (find-file ,tempvar))
1085 (setq to-be-removed (current-buffer))
1086 (goto-char (point-min))
1087 (while (re-search-forward ,rx nil t)
1088 (goto-char (match-beginning 1))
1089 (when (looking-at org-babel-inline-src-block-regexp)(forward-char 1))
1090 (save-match-data ,@body)
1091 (goto-char (match-end 0))))
1092 (unless visited-p (kill-buffer to-be-removed))
1093 (goto-char point))))
1094 (def-edebug-spec org-babel-map-executables (form body))
1096 ;;;###autoload
1097 (defun org-babel-execute-buffer (&optional arg)
1098 "Execute source code blocks in a buffer.
1099 Call `org-babel-execute-src-block' on every source block in
1100 the current buffer."
1101 (interactive "P")
1102 (org-babel-eval-wipe-error-buffer)
1103 (org-save-outline-visibility t
1104 (org-babel-map-executables nil
1105 (if (looking-at org-babel-lob-one-liner-regexp)
1106 (org-babel-lob-execute-maybe)
1107 (org-babel-execute-src-block arg)))))
1109 ;;;###autoload
1110 (defun org-babel-execute-subtree (&optional arg)
1111 "Execute source code blocks in a subtree.
1112 Call `org-babel-execute-src-block' on every source block in
1113 the current subtree."
1114 (interactive "P")
1115 (save-restriction
1116 (save-excursion
1117 (org-narrow-to-subtree)
1118 (org-babel-execute-buffer arg)
1119 (widen))))
1121 ;;;###autoload
1122 (defun org-babel-sha1-hash (&optional info)
1123 "Generate an sha1 hash based on the value of info."
1124 (interactive)
1125 (let ((print-level nil)
1126 (info (or info (org-babel-get-src-block-info))))
1127 (setf (nth 2 info)
1128 (sort (copy-sequence (nth 2 info))
1129 (lambda (a b) (string< (car a) (car b)))))
1130 (let* ((rm (lambda (lst)
1131 (dolist (p '("replace" "silent" "none"
1132 "append" "prepend"))
1133 (setq lst (remove p lst)))
1134 lst))
1135 (norm (lambda (arg)
1136 (let ((v (if (and (listp (cdr arg)) (null (cddr arg)))
1137 (copy-sequence (cdr arg))
1138 (cdr arg))))
1139 (when (and v (not (and (sequencep v)
1140 (not (consp v))
1141 (= (length v) 0))))
1142 (cond
1143 ((and (listp v) ; lists are sorted
1144 (member (car arg) '(:result-params)))
1145 (sort (funcall rm v) #'string<))
1146 ((and (stringp v) ; strings are sorted
1147 (member (car arg) '(:results :exports)))
1148 (mapconcat #'identity (sort (funcall rm (split-string v))
1149 #'string<) " "))
1150 (t v)))))))
1151 ((lambda (hash)
1152 (when (org-called-interactively-p 'interactive) (message hash)) hash)
1153 (let ((it (format "%s-%s"
1154 (mapconcat
1155 #'identity
1156 (delq nil (mapcar (lambda (arg)
1157 (let ((normalized (funcall norm arg)))
1158 (when normalized
1159 (format "%S" normalized))))
1160 (nth 2 info))) ":")
1161 (nth 1 info))))
1162 (sha1 it))))))
1164 (defun org-babel-current-result-hash ()
1165 "Return the current in-buffer hash."
1166 (org-babel-where-is-src-block-result)
1167 (org-no-properties (match-string 5)))
1169 (defun org-babel-set-current-result-hash (hash)
1170 "Set the current in-buffer hash to HASH."
1171 (org-babel-where-is-src-block-result)
1172 (save-excursion (goto-char (match-beginning 3))
1173 ;; (mapc #'delete-overlay (overlays-at (point)))
1174 (replace-match hash nil nil nil 3)
1175 (org-babel-hide-hash)))
1177 (defun org-babel-hide-hash ()
1178 "Hide the hash in the current results line.
1179 Only the initial `org-babel-hash-show' characters of the hash
1180 will remain visible."
1181 (add-to-invisibility-spec '(org-babel-hide-hash . t))
1182 (save-excursion
1183 (when (and (re-search-forward org-babel-result-regexp nil t)
1184 (match-string 5))
1185 (let* ((start (match-beginning 5))
1186 (hide-start (+ org-babel-hash-show start))
1187 (end (match-end 5))
1188 (hash (match-string 5))
1189 ov1 ov2)
1190 (setq ov1 (make-overlay start hide-start))
1191 (setq ov2 (make-overlay hide-start end))
1192 (overlay-put ov2 'invisible 'org-babel-hide-hash)
1193 (overlay-put ov1 'babel-hash hash)))))
1195 (defun org-babel-hide-all-hashes ()
1196 "Hide the hash in the current buffer.
1197 Only the initial `org-babel-hash-show' characters of each hash
1198 will remain visible. This function should be called as part of
1199 the `org-mode-hook'."
1200 (save-excursion
1201 (while (and (not org-babel-hash-show-time)
1202 (re-search-forward org-babel-result-regexp nil t))
1203 (goto-char (match-beginning 0))
1204 (org-babel-hide-hash)
1205 (goto-char (match-end 0)))))
1206 (add-hook 'org-mode-hook 'org-babel-hide-all-hashes)
1208 (defun org-babel-hash-at-point (&optional point)
1209 "Return the value of the hash at POINT.
1210 The hash is also added as the last element of the kill ring.
1211 This can be called with C-c C-c."
1212 (interactive)
1213 (let ((hash (car (delq nil (mapcar
1214 (lambda (ol) (overlay-get ol 'babel-hash))
1215 (overlays-at (or point (point))))))))
1216 (when hash (kill-new hash) (message hash))))
1217 (add-hook 'org-ctrl-c-ctrl-c-hook 'org-babel-hash-at-point)
1219 (defun org-babel-result-hide-spec ()
1220 "Hide portions of results lines.
1221 Add `org-babel-hide-result' as an invisibility spec for hiding
1222 portions of results lines."
1223 (add-to-invisibility-spec '(org-babel-hide-result . t)))
1224 (add-hook 'org-mode-hook 'org-babel-result-hide-spec)
1226 (defvar org-babel-hide-result-overlays nil
1227 "Overlays hiding results.")
1229 (defun org-babel-result-hide-all ()
1230 "Fold all results in the current buffer."
1231 (interactive)
1232 (org-babel-show-result-all)
1233 (save-excursion
1234 (while (re-search-forward org-babel-result-regexp nil t)
1235 (save-excursion (goto-char (match-beginning 0))
1236 (org-babel-hide-result-toggle-maybe)))))
1238 (defun org-babel-show-result-all ()
1239 "Unfold all results in the current buffer."
1240 (mapc 'delete-overlay org-babel-hide-result-overlays)
1241 (setq org-babel-hide-result-overlays nil))
1243 ;;;###autoload
1244 (defun org-babel-hide-result-toggle-maybe ()
1245 "Toggle visibility of result at point."
1246 (interactive)
1247 (let ((case-fold-search t))
1248 (if (save-excursion
1249 (beginning-of-line 1)
1250 (looking-at org-babel-result-regexp))
1251 (progn (org-babel-hide-result-toggle)
1252 t) ;; to signal that we took action
1253 nil))) ;; to signal that we did not
1255 (defun org-babel-hide-result-toggle (&optional force)
1256 "Toggle the visibility of the current result."
1257 (interactive)
1258 (save-excursion
1259 (beginning-of-line)
1260 (if (re-search-forward org-babel-result-regexp nil t)
1261 (let ((start (progn (beginning-of-line 2) (- (point) 1)))
1262 (end (progn
1263 (while (looking-at org-babel-multi-line-header-regexp)
1264 (forward-line 1))
1265 (goto-char (- (org-babel-result-end) 1)) (point)))
1267 (if (memq t (mapcar (lambda (overlay)
1268 (eq (overlay-get overlay 'invisible)
1269 'org-babel-hide-result))
1270 (overlays-at start)))
1271 (if (or (not force) (eq force 'off))
1272 (mapc (lambda (ov)
1273 (when (member ov org-babel-hide-result-overlays)
1274 (setq org-babel-hide-result-overlays
1275 (delq ov org-babel-hide-result-overlays)))
1276 (when (eq (overlay-get ov 'invisible)
1277 'org-babel-hide-result)
1278 (delete-overlay ov)))
1279 (overlays-at start)))
1280 (setq ov (make-overlay start end))
1281 (overlay-put ov 'invisible 'org-babel-hide-result)
1282 ;; make the block accessible to isearch
1283 (overlay-put
1284 ov 'isearch-open-invisible
1285 (lambda (ov)
1286 (when (member ov org-babel-hide-result-overlays)
1287 (setq org-babel-hide-result-overlays
1288 (delq ov org-babel-hide-result-overlays)))
1289 (when (eq (overlay-get ov 'invisible)
1290 'org-babel-hide-result)
1291 (delete-overlay ov))))
1292 (push ov org-babel-hide-result-overlays)))
1293 (error "Not looking at a result line"))))
1295 ;; org-tab-after-check-for-cycling-hook
1296 (add-hook 'org-tab-first-hook 'org-babel-hide-result-toggle-maybe)
1297 ;; Remove overlays when changing major mode
1298 (add-hook 'org-mode-hook
1299 (lambda () (org-add-hook 'change-major-mode-hook
1300 'org-babel-show-result-all 'append 'local)))
1302 (defvar org-file-properties)
1303 (defun org-babel-params-from-properties (&optional lang)
1304 "Retrieve parameters specified as properties.
1305 Return a list of association lists of source block params
1306 specified in the properties of the current outline entry."
1307 (save-match-data
1308 (list
1309 ;; DEPRECATED header arguments specified as separate property at
1310 ;; point of definition
1311 (let (val sym)
1312 (org-babel-parse-multiple-vars
1313 (delq nil
1314 (mapcar
1315 (lambda (header-arg)
1316 (and (setq val (org-entry-get (point) header-arg t))
1317 (cons (intern (concat ":" header-arg))
1318 (org-babel-read val))))
1319 (mapcar
1320 #'symbol-name
1321 (mapcar
1322 #'car
1323 (org-babel-combine-header-arg-lists
1324 org-babel-common-header-args-w-values
1325 (progn
1326 (setq sym (intern (concat "org-babel-header-args:" lang)))
1327 (and (boundp sym) (eval sym))))))))))
1328 ;; header arguments specified with the header-args property at
1329 ;; point of call
1330 (org-babel-parse-header-arguments
1331 (org-entry-get org-babel-current-src-block-location
1332 "header-args" 'inherit))
1333 (when lang ;; language-specific header arguments at point of call
1334 (org-babel-parse-header-arguments
1335 (org-entry-get org-babel-current-src-block-location
1336 (concat "header-args:" lang) 'inherit))))))
1338 (defvar org-src-preserve-indentation)
1339 (defun org-babel-parse-src-block-match ()
1340 "Parse the results from a match of the `org-babel-src-block-regexp'."
1341 (let* ((block-indentation (length (match-string 1)))
1342 (lang (org-no-properties (match-string 2)))
1343 (lang-headers (intern (concat "org-babel-default-header-args:" lang)))
1344 (switches (match-string 3))
1345 (body (org-no-properties
1346 (let* ((body (match-string 5))
1347 (sub-length (- (length body) 1)))
1348 (if (and (> sub-length 0)
1349 (string= "\n" (substring body sub-length)))
1350 (substring body 0 sub-length)
1351 (or body "")))))
1352 (preserve-indentation (or org-src-preserve-indentation
1353 (save-match-data
1354 (string-match "-i\\>" switches)))))
1355 (list lang
1356 ;; get block body less properties, protective commas, and indentation
1357 (with-temp-buffer
1358 (save-match-data
1359 (insert (org-unescape-code-in-string body))
1360 (unless preserve-indentation (org-do-remove-indentation))
1361 (buffer-string)))
1362 (apply #'org-babel-merge-params
1363 org-babel-default-header-args
1364 (when (boundp lang-headers) (eval lang-headers))
1365 (append
1366 (org-babel-params-from-properties lang)
1367 (list (org-babel-parse-header-arguments
1368 (org-no-properties (or (match-string 4) ""))))))
1369 switches
1370 block-indentation)))
1372 (defun org-babel-parse-inline-src-block-match ()
1373 "Parse the results from a match of the `org-babel-inline-src-block-regexp'."
1374 (let* ((lang (org-no-properties (match-string 2)))
1375 (lang-headers (intern (concat "org-babel-default-header-args:" lang))))
1376 (list lang
1377 (org-unescape-code-in-string (org-no-properties (match-string 5)))
1378 (apply #'org-babel-merge-params
1379 org-babel-default-inline-header-args
1380 (if (boundp lang-headers) (eval lang-headers) nil)
1381 (append
1382 (org-babel-params-from-properties lang)
1383 (list (org-babel-parse-header-arguments
1384 (org-no-properties (or (match-string 4) "")))))))))
1386 (defun org-babel-balanced-split (string alts)
1387 "Split STRING on instances of ALTS.
1388 ALTS is a cons of two character options where each option may be
1389 either the numeric code of a single character or a list of
1390 character alternatives. For example to split on balanced
1391 instances of \"[ \t]:\" set ALTS to '((32 9) . 58)."
1392 (let* ((matches (lambda (ch spec) (if (listp spec) (member ch spec) (equal spec ch))))
1393 (matched (lambda (ch last)
1394 (if (consp alts)
1395 (and (funcall matches ch (cdr alts))
1396 (funcall matches last (car alts)))
1397 (funcall matches ch alts))))
1398 (balance 0) (last 0)
1399 quote partial lst)
1400 (mapc (lambda (ch) ; split on [], (), "" balanced instances of [ \t]:
1401 (setq balance (+ balance
1402 (cond ((or (equal 91 ch) (equal 40 ch)) 1)
1403 ((or (equal 93 ch) (equal 41 ch)) -1)
1404 (t 0))))
1405 (when (and (equal 34 ch) (not (equal 92 last)))
1406 (setq quote (not quote)))
1407 (setq partial (cons ch partial))
1408 (when (and (= balance 0) (not quote) (funcall matched ch last))
1409 (setq lst (cons (apply #'string (nreverse
1410 (if (consp alts)
1411 (cddr partial)
1412 (cdr partial))))
1413 lst))
1414 (setq partial nil))
1415 (setq last ch))
1416 (string-to-list string))
1417 (nreverse (cons (apply #'string (nreverse partial)) lst))))
1419 (defun org-babel-join-splits-near-ch (ch list)
1420 "Join splits where \"=\" is on either end of the split."
1421 (let ((last= (lambda (str) (= ch (aref str (1- (length str))))))
1422 (first= (lambda (str) (= ch (aref str 0)))))
1423 (reverse
1424 (org-reduce (lambda (acc el)
1425 (let ((head (car acc)))
1426 (if (and head (or (funcall last= head) (funcall first= el)))
1427 (cons (concat head el) (cdr acc))
1428 (cons el acc))))
1429 list :initial-value nil))))
1431 (defun org-babel-parse-header-arguments (arg-string)
1432 "Parse a string of header arguments returning an alist."
1433 (when (> (length arg-string) 0)
1434 (org-babel-parse-multiple-vars
1435 (delq nil
1436 (mapcar
1437 (lambda (arg)
1438 (if (string-match
1439 "\\([^ \f\t\n\r\v]+\\)[ \f\t\n\r\v]+\\([^ \f\t\n\r\v]+.*\\)"
1440 arg)
1441 (cons (intern (match-string 1 arg))
1442 (org-babel-read (org-babel-chomp (match-string 2 arg))))
1443 (cons (intern (org-babel-chomp arg)) nil)))
1444 ((lambda (raw)
1445 (cons (car raw) (mapcar (lambda (r) (concat ":" r)) (cdr raw))))
1446 (org-babel-balanced-split arg-string '((32 9) . 58))))))))
1448 (defun org-babel-parse-multiple-vars (header-arguments)
1449 "Expand multiple variable assignments behind a single :var keyword.
1451 This allows expression of multiple variables with one :var as
1452 shown below.
1454 #+PROPERTY: var foo=1, bar=2"
1455 (let (results)
1456 (mapc (lambda (pair)
1457 (if (eq (car pair) :var)
1458 (mapcar (lambda (v) (push (cons :var (org-babel-trim v)) results))
1459 (org-babel-join-splits-near-ch
1460 61 (org-babel-balanced-split (cdr pair) 32)))
1461 (push pair results)))
1462 header-arguments)
1463 (nreverse results)))
1465 (defun org-babel-process-params (params)
1466 "Expand variables in PARAMS and add summary parameters."
1467 (let* ((processed-vars (mapcar (lambda (el)
1468 (if (consp (cdr el))
1469 (cdr el)
1470 (org-babel-ref-parse (cdr el))))
1471 (org-babel-get-header params :var)))
1472 (vars-and-names (if (and (assoc :colname-names params)
1473 (assoc :rowname-names params))
1474 (list processed-vars)
1475 (org-babel-disassemble-tables
1476 processed-vars
1477 (cdr (assoc :hlines params))
1478 (cdr (assoc :colnames params))
1479 (cdr (assoc :rownames params)))))
1480 (raw-result (or (cdr (assoc :results params)) ""))
1481 (result-params (append
1482 (split-string (if (stringp raw-result)
1483 raw-result
1484 (eval raw-result)))
1485 (cdr (assoc :result-params params)))))
1486 (append
1487 (mapcar (lambda (var) (cons :var var)) (car vars-and-names))
1488 (list
1489 (cons :colname-names (or (cdr (assoc :colname-names params))
1490 (cadr vars-and-names)))
1491 (cons :rowname-names (or (cdr (assoc :rowname-names params))
1492 (caddr vars-and-names)))
1493 (cons :result-params result-params)
1494 (cons :result-type (cond ((member "output" result-params) 'output)
1495 ((member "value" result-params) 'value)
1496 (t 'value))))
1497 (org-babel-get-header params :var 'other))))
1499 ;; row and column names
1500 (defun org-babel-del-hlines (table)
1501 "Remove all 'hlines from TABLE."
1502 (remove 'hline table))
1504 (defun org-babel-get-colnames (table)
1505 "Return the column names of TABLE.
1506 Return a cons cell, the `car' of which contains the TABLE less
1507 colnames, and the `cdr' of which contains a list of the column
1508 names."
1509 (if (equal 'hline (nth 1 table))
1510 (cons (cddr table) (car table))
1511 (cons (cdr table) (car table))))
1513 (defun org-babel-get-rownames (table)
1514 "Return the row names of TABLE.
1515 Return a cons cell, the `car' of which contains the TABLE less
1516 colnames, and the `cdr' of which contains a list of the column
1517 names. Note: this function removes any hlines in TABLE."
1518 (let* ((trans (lambda (table) (apply #'mapcar* #'list table)))
1519 (width (apply 'max
1520 (mapcar (lambda (el) (if (listp el) (length el) 0)) table)))
1521 (table (funcall trans (mapcar (lambda (row)
1522 (if (not (equal row 'hline))
1524 (setq row '())
1525 (dotimes (n width)
1526 (setq row (cons 'hline row)))
1527 row))
1528 table))))
1529 (cons (mapcar (lambda (row) (if (equal (car row) 'hline) 'hline row))
1530 (funcall trans (cdr table)))
1531 (remove 'hline (car table)))))
1533 (defun org-babel-put-colnames (table colnames)
1534 "Add COLNAMES to TABLE if they exist."
1535 (if colnames (apply 'list colnames 'hline table) table))
1537 (defun org-babel-put-rownames (table rownames)
1538 "Add ROWNAMES to TABLE if they exist."
1539 (if rownames
1540 (mapcar (lambda (row)
1541 (if (listp row)
1542 (cons (or (pop rownames) "") row)
1543 row)) table)
1544 table))
1546 (defun org-babel-pick-name (names selector)
1547 "Select one out of an alist of row or column names.
1548 SELECTOR can be either a list of names in which case those names
1549 will be returned directly, or an index into the list NAMES in
1550 which case the indexed names will be return."
1551 (if (listp selector)
1552 selector
1553 (when names
1554 (if (and selector (symbolp selector) (not (equal t selector)))
1555 (cdr (assoc selector names))
1556 (if (integerp selector)
1557 (nth (- selector 1) names)
1558 (cdr (car (last names))))))))
1560 (defun org-babel-disassemble-tables (vars hlines colnames rownames)
1561 "Parse tables for further processing.
1562 Process the variables in VARS according to the HLINES,
1563 ROWNAMES and COLNAMES header arguments. Return a list consisting
1564 of the vars, cnames and rnames."
1565 (let (cnames rnames)
1566 (list
1567 (mapcar
1568 (lambda (var)
1569 (when (listp (cdr var))
1570 (when (and (not (equal colnames "no"))
1571 (or colnames (and (equal (nth 1 (cdr var)) 'hline)
1572 (not (member 'hline (cddr (cdr var)))))))
1573 (let ((both (org-babel-get-colnames (cdr var))))
1574 (setq cnames (cons (cons (car var) (cdr both))
1575 cnames))
1576 (setq var (cons (car var) (car both)))))
1577 (when (and rownames (not (equal rownames "no")))
1578 (let ((both (org-babel-get-rownames (cdr var))))
1579 (setq rnames (cons (cons (car var) (cdr both))
1580 rnames))
1581 (setq var (cons (car var) (car both)))))
1582 (when (and hlines (not (equal hlines "yes")))
1583 (setq var (cons (car var) (org-babel-del-hlines (cdr var))))))
1584 var)
1585 vars)
1586 (reverse cnames) (reverse rnames))))
1588 (defun org-babel-reassemble-table (table colnames rownames)
1589 "Add column and row names to a table.
1590 Given a TABLE and set of COLNAMES and ROWNAMES add the names
1591 to the table for reinsertion to org-mode."
1592 (if (listp table)
1593 ((lambda (table)
1594 (if (and colnames (listp (car table)) (= (length (car table))
1595 (length colnames)))
1596 (org-babel-put-colnames table colnames) table))
1597 (if (and rownames (= (length table) (length rownames)))
1598 (org-babel-put-rownames table rownames) table))
1599 table))
1601 (defun org-babel-where-is-src-block-head ()
1602 "Find where the current source block begins.
1603 Return the point at the beginning of the current source
1604 block. Specifically at the beginning of the #+BEGIN_SRC line.
1605 If the point is not on a source block then return nil."
1606 (let ((initial (point)) (case-fold-search t) top bottom)
1608 (save-excursion ;; on a source name line or a #+header line
1609 (beginning-of-line 1)
1610 (and (or (looking-at org-babel-src-name-regexp)
1611 (looking-at org-babel-multi-line-header-regexp))
1612 (progn
1613 (while (and (forward-line 1)
1614 (or (looking-at org-babel-src-name-regexp)
1615 (looking-at org-babel-multi-line-header-regexp))))
1616 (looking-at org-babel-src-block-regexp))
1617 (point)))
1618 (save-excursion ;; on a #+begin_src line
1619 (beginning-of-line 1)
1620 (and (looking-at org-babel-src-block-regexp)
1621 (point)))
1622 (save-excursion ;; inside a src block
1623 (and
1624 (re-search-backward "^[ \t]*#\\+begin_src" nil t) (setq top (point))
1625 (re-search-forward "^[ \t]*#\\+end_src" nil t) (setq bottom (point))
1626 (< top initial) (< initial bottom)
1627 (progn (goto-char top) (beginning-of-line 1)
1628 (looking-at org-babel-src-block-regexp))
1629 (point-marker))))))
1631 ;;;###autoload
1632 (defun org-babel-goto-src-block-head ()
1633 "Go to the beginning of the current code block."
1634 (interactive)
1635 ((lambda (head)
1636 (if head (goto-char head) (error "Not currently in a code block")))
1637 (org-babel-where-is-src-block-head)))
1639 ;;;###autoload
1640 (defun org-babel-goto-named-src-block (name)
1641 "Go to a named source-code block."
1642 (interactive
1643 (let ((completion-ignore-case t)
1644 (case-fold-search t)
1645 (under-point (thing-at-point 'line)))
1646 (list (org-icompleting-read
1647 "source-block name: " (org-babel-src-block-names) nil t
1648 (cond
1649 ;; noweb
1650 ((string-match (org-babel-noweb-wrap) under-point)
1651 (let ((block-name (match-string 1 under-point)))
1652 (string-match "[^(]*" block-name)
1653 (match-string 0 block-name)))
1654 ;; #+call:
1655 ((string-match org-babel-lob-one-liner-regexp under-point)
1656 (let ((source-info (car (org-babel-lob-get-info))))
1657 (if (string-match "^\\([^\\[]+?\\)\\(\\[.*\\]\\)?(" source-info)
1658 (let ((source-name (match-string 1 source-info)))
1659 source-name))))
1660 ;; #+results:
1661 ((string-match (concat "#\\+" org-babel-results-keyword
1662 "\\:\s+\\([^\\(]*\\)") under-point)
1663 (match-string 1 under-point))
1664 ;; symbol-at-point
1665 ((and (thing-at-point 'symbol))
1666 (org-babel-find-named-block (thing-at-point 'symbol))
1667 (thing-at-point 'symbol))
1668 (""))))))
1669 (let ((point (org-babel-find-named-block name)))
1670 (if point
1671 ;; taken from `org-open-at-point'
1672 (progn (org-mark-ring-push) (goto-char point) (org-show-context))
1673 (message "source-code block '%s' not found in this buffer" name))))
1675 (defun org-babel-find-named-block (name)
1676 "Find a named source-code block.
1677 Return the location of the source block identified by source
1678 NAME, or nil if no such block exists. Set match data according to
1679 org-babel-named-src-block-regexp."
1680 (save-excursion
1681 (let ((case-fold-search t)
1682 (regexp (org-babel-named-src-block-regexp-for-name name)) msg)
1683 (goto-char (point-min))
1684 (when (or (re-search-forward regexp nil t)
1685 (re-search-backward regexp nil t))
1686 (match-beginning 0)))))
1688 (defun org-babel-src-block-names (&optional file)
1689 "Returns the names of source blocks in FILE or the current buffer."
1690 (save-excursion
1691 (when file (find-file file)) (goto-char (point-min))
1692 (let ((case-fold-search t) names)
1693 (while (re-search-forward org-babel-src-name-w-name-regexp nil t)
1694 (setq names (cons (match-string 3) names)))
1695 names)))
1697 ;;;###autoload
1698 (defun org-babel-goto-named-result (name)
1699 "Go to a named result."
1700 (interactive
1701 (let ((completion-ignore-case t))
1702 (list (org-icompleting-read "source-block name: "
1703 (org-babel-result-names) nil t))))
1704 (let ((point (org-babel-find-named-result name)))
1705 (if point
1706 ;; taken from `org-open-at-point'
1707 (progn (goto-char point) (org-show-context))
1708 (message "result '%s' not found in this buffer" name))))
1710 (defun org-babel-find-named-result (name &optional point)
1711 "Find a named result.
1712 Return the location of the result named NAME in the current
1713 buffer or nil if no such result exists."
1714 (save-excursion
1715 (let ((case-fold-search t))
1716 (goto-char (or point (point-min)))
1717 (catch 'is-a-code-block
1718 (when (re-search-forward
1719 (concat org-babel-result-regexp
1720 "[ \t]" (regexp-quote name) "[ \t]*[\n\f\v\r]") nil t)
1721 (when (and (string= "name" (downcase (match-string 1)))
1722 (or (beginning-of-line 1)
1723 (looking-at org-babel-src-block-regexp)
1724 (looking-at org-babel-multi-line-header-regexp)))
1725 (throw 'is-a-code-block (org-babel-find-named-result name (point))))
1726 (beginning-of-line 0) (point))))))
1728 (defun org-babel-result-names (&optional file)
1729 "Returns the names of results in FILE or the current buffer."
1730 (save-excursion
1731 (when file (find-file file)) (goto-char (point-min))
1732 (let ((case-fold-search t) names)
1733 (while (re-search-forward org-babel-result-w-name-regexp nil t)
1734 (setq names (cons (match-string 4) names)))
1735 names)))
1737 ;;;###autoload
1738 (defun org-babel-next-src-block (&optional arg)
1739 "Jump to the next source block.
1740 With optional prefix argument ARG, jump forward ARG many source blocks."
1741 (interactive "p")
1742 (org-next-block arg nil org-babel-src-block-regexp))
1744 ;;;###autoload
1745 (defun org-babel-previous-src-block (&optional arg)
1746 "Jump to the previous source block.
1747 With optional prefix argument ARG, jump backward ARG many source blocks."
1748 (interactive "p")
1749 (org-previous-block arg org-babel-src-block-regexp))
1751 (defvar org-babel-load-languages)
1753 ;;;###autoload
1754 (defun org-babel-mark-block ()
1755 "Mark current src block."
1756 (interactive)
1757 ((lambda (head)
1758 (when head
1759 (save-excursion
1760 (goto-char head)
1761 (looking-at org-babel-src-block-regexp))
1762 (push-mark (match-end 5) nil t)
1763 (goto-char (match-beginning 5))))
1764 (org-babel-where-is-src-block-head)))
1766 (defun org-babel-demarcate-block (&optional arg)
1767 "Wrap or split the code in the region or on the point.
1768 When called from inside of a code block the current block is
1769 split. When called from outside of a code block a new code block
1770 is created. In both cases if the region is demarcated and if the
1771 region is not active then the point is demarcated."
1772 (interactive "P")
1773 (let ((info (org-babel-get-src-block-info 'light))
1774 (headers (progn (org-babel-where-is-src-block-head)
1775 (match-string 4)))
1776 (stars (concat (make-string (or (org-current-level) 1) ?*) " ")))
1777 (if info
1778 (mapc
1779 (lambda (place)
1780 (save-excursion
1781 (goto-char place)
1782 (let ((lang (nth 0 info))
1783 (indent (make-string (nth 5 info) ? )))
1784 (when (string-match "^[[:space:]]*$"
1785 (buffer-substring (point-at-bol)
1786 (point-at-eol)))
1787 (delete-region (point-at-bol) (point-at-eol)))
1788 (insert (concat
1789 (if (looking-at "^") "" "\n")
1790 indent "#+end_src\n"
1791 (if arg stars indent) "\n"
1792 indent "#+begin_src " lang
1793 (if (> (length headers) 1)
1794 (concat " " headers) headers)
1795 (if (looking-at "[\n\r]")
1797 (concat "\n" (make-string (current-column) ? )))))))
1798 (move-end-of-line 2))
1799 (sort (if (org-region-active-p) (list (mark) (point)) (list (point))) #'>))
1800 (let ((start (point))
1801 (lang (org-icompleting-read "Lang: "
1802 (mapcar (lambda (el) (symbol-name (car el)))
1803 org-babel-load-languages)))
1804 (body (delete-and-extract-region
1805 (if (org-region-active-p) (mark) (point)) (point))))
1806 (insert (concat (if (looking-at "^") "" "\n")
1807 (if arg (concat stars "\n") "")
1808 "#+begin_src " lang "\n"
1809 body
1810 (if (or (= (length body) 0)
1811 (string-match "[\r\n]$" body)) "" "\n")
1812 "#+end_src\n"))
1813 (goto-char start) (move-end-of-line 1)))))
1815 (defvar org-babel-lob-one-liner-regexp)
1816 (defun org-babel-where-is-src-block-result (&optional insert info hash indent)
1817 "Find where the current source block results begin.
1818 Return the point at the beginning of the result of the current
1819 source block. Specifically at the beginning of the results line.
1820 If no result exists for this block then create a results line
1821 following the source block."
1822 (save-excursion
1823 (let* ((case-fold-search t)
1824 (on-lob-line (save-excursion
1825 (beginning-of-line 1)
1826 (looking-at org-babel-lob-one-liner-regexp)))
1827 (inlinep (when (org-babel-get-inline-src-block-matches)
1828 (match-end 0)))
1829 (name (if on-lob-line
1830 (mapconcat #'identity (butlast (org-babel-lob-get-info))
1832 (nth 4 (or info (org-babel-get-src-block-info 'light)))))
1833 (head (unless on-lob-line (org-babel-where-is-src-block-head)))
1834 found beg end)
1835 (when head (goto-char head))
1836 (org-with-wide-buffer
1837 (setq
1838 found ;; was there a result (before we potentially insert one)
1840 inlinep
1841 (and
1842 ;; named results:
1843 ;; - return t if it is found, else return nil
1844 ;; - if it does not need to be rebuilt, then don't set end
1845 ;; - if it does need to be rebuilt then do set end
1846 name (setq beg (org-babel-find-named-result name))
1847 (prog1 beg
1848 (when (and hash (not (string= hash (match-string 5))))
1849 (goto-char beg) (setq end beg) ;; beginning of result
1850 (forward-line 1)
1851 (delete-region end (org-babel-result-end)) nil)))
1852 (and
1853 ;; unnamed results:
1854 ;; - return t if it is found, else return nil
1855 ;; - if it is found, and the hash doesn't match, delete and set end
1856 (or on-lob-line (re-search-forward "^[ \t]*#\\+end_src" nil t))
1857 (progn (end-of-line 1)
1858 (if (eobp) (insert "\n") (forward-char 1))
1859 (setq end (point))
1860 (or (and
1861 (not name)
1862 (progn ;; unnamed results line already exists
1863 (catch 'non-comment
1864 (while (re-search-forward "[^ \f\t\n\r\v]" nil t)
1865 (beginning-of-line 1)
1866 (cond
1867 ((looking-at (concat org-babel-result-regexp "\n"))
1868 (throw 'non-comment t))
1869 ((looking-at "^[ \t]*#") (end-of-line 1))
1870 (t (throw 'non-comment nil))))))
1871 (let ((this-hash (match-string 5)))
1872 (prog1 (point)
1873 ;; must remove and rebuild if hash!=old-hash
1874 (if (and hash (not (string= hash this-hash)))
1875 (prog1 nil
1876 (forward-line 1)
1877 (delete-region
1878 end (org-babel-result-end)))
1879 (setq end nil)))))))))))
1880 (if (not (and insert end)) found
1881 (goto-char end)
1882 (unless beg
1883 (if (looking-at "[\n\r]") (forward-char 1) (insert "\n")))
1884 (insert (concat
1885 (when (wholenump indent) (make-string indent ? ))
1886 "#+" org-babel-results-keyword
1887 (when hash
1888 (if org-babel-hash-show-time
1889 (concat
1890 "["(format-time-string "<%Y-%m-%d %H:%M:%S>")" "hash"]")
1891 (concat "["hash"]")))
1893 (when name (concat " " name)) "\n"))
1894 (unless beg (insert "\n") (backward-char))
1895 (beginning-of-line 0)
1896 (if hash (org-babel-hide-hash))
1897 (point)))))
1899 (defvar org-block-regexp)
1900 (defun org-babel-read-result ()
1901 "Read the result at `point' into emacs-lisp."
1902 (let ((case-fold-search t) result-string)
1903 (cond
1904 ((org-at-table-p) (org-babel-read-table))
1905 ((org-at-item-p) (org-babel-read-list))
1906 ((looking-at org-bracket-link-regexp) (org-babel-read-link))
1907 ((looking-at org-block-regexp) (org-babel-trim (match-string 4)))
1908 ((or (looking-at "^[ \t]*: ") (looking-at "^[ \t]*:$"))
1909 (setq result-string
1910 (org-babel-trim
1911 (mapconcat (lambda (line)
1912 (or (and (> (length line) 1)
1913 (string-match "^[ \t]*: ?\\(.+\\)" line)
1914 (match-string 1 line))
1915 ""))
1916 (split-string
1917 (buffer-substring
1918 (point) (org-babel-result-end)) "[\r\n]+")
1919 "\n")))
1920 (or (org-babel-number-p result-string) result-string))
1921 ((looking-at org-babel-result-regexp)
1922 (save-excursion (forward-line 1) (org-babel-read-result))))))
1924 (defun org-babel-read-table ()
1925 "Read the table at `point' into emacs-lisp."
1926 (mapcar (lambda (row)
1927 (if (and (symbolp row) (equal row 'hline)) row
1928 (mapcar (lambda (el) (org-babel-read el 'inhibit-lisp-eval)) row)))
1929 (org-table-to-lisp)))
1931 (defun org-babel-read-list ()
1932 "Read the list at `point' into emacs-lisp."
1933 (mapcar (lambda (el) (org-babel-read el 'inhibit-lisp-eval))
1934 (mapcar #'cadr (cdr (org-list-parse-list)))))
1936 (defvar org-link-types-re)
1937 (defun org-babel-read-link ()
1938 "Read the link at `point' into emacs-lisp.
1939 If the path of the link is a file path it is expanded using
1940 `expand-file-name'."
1941 (let* ((case-fold-search t)
1942 (raw (and (looking-at org-bracket-link-regexp)
1943 (org-no-properties (match-string 1))))
1944 (type (and (string-match org-link-types-re raw)
1945 (match-string 1 raw))))
1946 (cond
1947 ((not type) (expand-file-name raw))
1948 ((string= type "file")
1949 (and (string-match "file\\(.*\\):\\(.+\\)" raw)
1950 (expand-file-name (match-string 2 raw))))
1951 (t raw))))
1953 (defun org-babel-format-result (result &optional sep)
1954 "Format RESULT for writing to file."
1955 (let ((echo-res (lambda (r) (if (stringp r) r (format "%S" r)))))
1956 (if (listp result)
1957 ;; table result
1958 (orgtbl-to-generic
1959 result (list :sep (or sep "\t") :fmt echo-res))
1960 ;; scalar result
1961 (funcall echo-res result))))
1963 (defun org-babel-insert-result
1964 (result &optional result-params info hash indent lang)
1965 "Insert RESULT into the current buffer.
1966 By default RESULT is inserted after the end of the
1967 current source block. With optional argument RESULT-PARAMS
1968 controls insertion of results in the org-mode file.
1969 RESULT-PARAMS can take the following values:
1971 replace - (default option) insert results after the source block
1972 replacing any previously inserted results
1974 silent -- no results are inserted into the Org-mode buffer but
1975 the results are echoed to the minibuffer and are
1976 ingested by Emacs (a potentially time consuming
1977 process)
1979 file ---- the results are interpreted as a file path, and are
1980 inserted into the buffer using the Org-mode file syntax
1982 list ---- the results are interpreted as an Org-mode list.
1984 raw ----- results are added directly to the Org-mode file. This
1985 is a good option if you code block will output org-mode
1986 formatted text.
1988 drawer -- results are added directly to the Org-mode file as with
1989 \"raw\", but are wrapped in a RESULTS drawer, allowing
1990 them to later be replaced or removed automatically.
1992 org ----- results are added inside of a \"#+BEGIN_SRC org\" block.
1993 They are not comma-escaped when inserted, but Org syntax
1994 here will be discarded when exporting the file.
1996 html ---- results are added inside of a #+BEGIN_HTML block. This
1997 is a good option if you code block will output html
1998 formatted text.
2000 latex --- results are added inside of a #+BEGIN_LATEX block.
2001 This is a good option if you code block will output
2002 latex formatted text.
2004 code ---- the results are extracted in the syntax of the source
2005 code of the language being evaluated and are added
2006 inside of a #+BEGIN_SRC block with the source-code
2007 language set appropriately. Note this relies on the
2008 optional LANG argument."
2009 (if (stringp result)
2010 (progn
2011 (setq result (org-no-properties result))
2012 (when (member "file" result-params)
2013 (setq result (org-babel-result-to-file
2014 result (when (assoc :file-desc (nth 2 info))
2015 (or (cdr (assoc :file-desc (nth 2 info)))
2016 result))))))
2017 (unless (listp result) (setq result (format "%S" result))))
2018 (if (and result-params (member "silent" result-params))
2019 (progn
2020 (message (replace-regexp-in-string "%" "%%" (format "%S" result)))
2021 result)
2022 (save-excursion
2023 (let* ((inlinep
2024 (save-excursion
2025 (when (or (org-babel-get-inline-src-block-matches)
2026 (org-babel-get-lob-one-liner-matches))
2027 (goto-char (match-end 0))
2028 (insert (if (listp result) "\n" " "))
2029 (point))))
2030 (existing-result (unless inlinep
2031 (org-babel-where-is-src-block-result
2032 t info hash indent)))
2033 (results-switches
2034 (cdr (assoc :results_switches (nth 2 info))))
2035 (visible-beg (copy-marker (point-min)))
2036 (visible-end (copy-marker (point-max)))
2037 ;; When results exist outside of the current visible
2038 ;; region of the buffer, be sure to widen buffer to
2039 ;; update them.
2040 (outside-scope-p (and existing-result
2041 (or (> visible-beg existing-result)
2042 (<= visible-end existing-result))))
2043 beg end)
2044 (when (and (stringp result) ; ensure results end in a newline
2045 (not inlinep)
2046 (> (length result) 0)
2047 (not (or (string-equal (substring result -1) "\n")
2048 (string-equal (substring result -1) "\r"))))
2049 (setq result (concat result "\n")))
2050 (unwind-protect
2051 (progn
2052 (when outside-scope-p (widen))
2053 (if (not existing-result)
2054 (setq beg (or inlinep (point)))
2055 (goto-char existing-result)
2056 (save-excursion
2057 (re-search-forward "#" nil t)
2058 (setq indent (- (current-column) 1)))
2059 (forward-line 1)
2060 (setq beg (point))
2061 (cond
2062 ((member "replace" result-params)
2063 (delete-region (point) (org-babel-result-end)))
2064 ((member "append" result-params)
2065 (goto-char (org-babel-result-end)) (setq beg (point-marker)))
2066 ((member "prepend" result-params)))) ; already there
2067 (setq results-switches
2068 (if results-switches (concat " " results-switches) ""))
2069 (let ((wrap (lambda (start finish &optional no-escape)
2070 (goto-char end) (insert (concat finish "\n"))
2071 (goto-char beg) (insert (concat start "\n"))
2072 (unless no-escape
2073 (org-escape-code-in-region (min (point) end) end))
2074 (goto-char end) (goto-char (point-at-eol))
2075 (setq end (point-marker))))
2076 (proper-list-p (lambda (it) (and (listp it) (null (cdr (last it)))))))
2077 ;; insert results based on type
2078 (cond
2079 ;; do nothing for an empty result
2080 ((null result))
2081 ;; insert a list if preferred
2082 ((member "list" result-params)
2083 (insert
2084 (org-babel-trim
2085 (org-list-to-generic
2086 (cons 'unordered
2087 (mapcar
2088 (lambda (el) (list nil (if (stringp el) el (format "%S" el))))
2089 (if (listp result) result (split-string result "\n" t))))
2090 '(:splicep nil :istart "- " :iend "\n")))
2091 "\n"))
2092 ;; assume the result is a table if it's not a string
2093 ((funcall proper-list-p result)
2094 (goto-char beg)
2095 (insert (concat (orgtbl-to-orgtbl
2096 (if (or (eq 'hline (car result))
2097 (and (listp (car result))
2098 (listp (cdr (car result)))))
2099 result (list result))
2100 '(:fmt (lambda (cell) (format "%s" cell)))) "\n"))
2101 (goto-char beg) (when (org-at-table-p) (org-table-align)))
2102 ((and (listp result) (not (funcall proper-list-p result)))
2103 (insert (format "%s\n" result)))
2104 ((member "file" result-params)
2105 (when inlinep (goto-char inlinep))
2106 (insert result))
2107 (t (goto-char beg) (insert result)))
2108 (when (funcall proper-list-p result) (goto-char (org-table-end)))
2109 (setq end (point-marker))
2110 ;; possibly wrap result
2111 (cond
2112 ((assoc :wrap (nth 2 info))
2113 (let ((name (or (cdr (assoc :wrap (nth 2 info))) "RESULTS")))
2114 (funcall wrap (concat "#+BEGIN_" name)
2115 (concat "#+END_" (car (org-split-string name))))))
2116 ((member "html" result-params)
2117 (funcall wrap "#+BEGIN_HTML" "#+END_HTML"))
2118 ((member "latex" result-params)
2119 (funcall wrap "#+BEGIN_LaTeX" "#+END_LaTeX"))
2120 ((member "org" result-params)
2121 (goto-char beg) (if (org-at-table-p) (org-cycle))
2122 (funcall wrap "#+BEGIN_SRC org" "#+END_SRC"))
2123 ((member "code" result-params)
2124 (funcall wrap (format "#+BEGIN_SRC %s%s" (or lang "none") results-switches)
2125 "#+END_SRC"))
2126 ((member "raw" result-params)
2127 (goto-char beg) (if (org-at-table-p) (org-cycle)))
2128 ((or (member "drawer" result-params)
2129 ;; Stay backward compatible with <7.9.2
2130 (member "wrap" result-params))
2131 (goto-char beg) (if (org-at-table-p) (org-cycle))
2132 (funcall wrap ":RESULTS:" ":END:" 'no-escape))
2133 ((and (not (funcall proper-list-p result))
2134 (not (member "file" result-params)))
2135 (org-babel-examplize-region beg end results-switches)
2136 (setq end (point)))))
2137 ;; possibly indent the results to match the #+results line
2138 (when (and (not inlinep) (numberp indent) indent (> indent 0)
2139 ;; in this case `table-align' does the work for us
2140 (not (and (listp result)
2141 (member "append" result-params))))
2142 (indent-rigidly beg end indent))
2143 (if (null result)
2144 (if (member "value" result-params)
2145 (message "Code block returned no value.")
2146 (message "Code block produced no output."))
2147 (message "Code block evaluation complete.")))
2148 (when outside-scope-p (narrow-to-region visible-beg visible-end))
2149 (set-marker visible-beg nil)
2150 (set-marker visible-end nil))))))
2152 (defun org-babel-remove-result (&optional info)
2153 "Remove the result of the current source block."
2154 (interactive)
2155 (let ((location (org-babel-where-is-src-block-result nil info)) start)
2156 (when location
2157 (setq start (- location 1))
2158 (save-excursion
2159 (goto-char location) (forward-line 1)
2160 (delete-region start (org-babel-result-end))))))
2162 (defun org-babel-result-end ()
2163 "Return the point at the end of the current set of results."
2164 (save-excursion
2165 (cond
2166 ((org-at-table-p) (progn (goto-char (org-table-end)) (point)))
2167 ((org-at-item-p) (let* ((struct (org-list-struct))
2168 (prvs (org-list-prevs-alist struct)))
2169 (org-list-get-list-end (point-at-bol) struct prvs)))
2170 ((let ((case-fold-search t)) (looking-at "^\\([ \t]*\\):results:"))
2171 (progn (re-search-forward (concat "^" (match-string 1) ":END:"))
2172 (forward-char 1) (point)))
2174 (let ((case-fold-search t))
2175 (if (looking-at (concat "[ \t]*#\\+begin_\\([^ \t\n\r]+\\)"))
2176 (progn (re-search-forward (concat "[ \t]*#\\+end_" (match-string 1))
2177 nil t)
2178 (forward-char 1))
2179 (while (looking-at "[ \t]*\\(: \\|:$\\|\\[\\[\\)")
2180 (forward-line 1))))
2181 (point)))))
2183 (defun org-babel-result-to-file (result &optional description)
2184 "Convert RESULT into an `org-mode' link with optional DESCRIPTION.
2185 If the `default-directory' is different from the containing
2186 file's directory then expand relative links."
2187 (when (stringp result)
2188 (format "[[file:%s]%s]"
2189 (if (and default-directory
2190 buffer-file-name
2191 (not (string= (expand-file-name default-directory)
2192 (expand-file-name
2193 (file-name-directory buffer-file-name)))))
2194 (expand-file-name result default-directory)
2195 result)
2196 (if description (concat "[" description "]") ""))))
2198 (defvar org-babel-capitalize-examplize-region-markers nil
2199 "Make true to capitalize begin/end example markers inserted by code blocks.")
2201 (defun org-babel-examplize-region (beg end &optional results-switches)
2202 "Comment out region using the inline '==' or ': ' org example quote."
2203 (interactive "*r")
2204 (let ((chars-between (lambda (b e)
2205 (not (string-match "^[\\s]*$" (buffer-substring b e)))))
2206 (maybe-cap (lambda (str) (if org-babel-capitalize-examplize-region-markers
2207 (upcase str) str))))
2208 (if (or (funcall chars-between (save-excursion (goto-char beg) (point-at-bol)) beg)
2209 (funcall chars-between end (save-excursion (goto-char end) (point-at-eol))))
2210 (save-excursion
2211 (goto-char beg)
2212 (insert (format org-babel-inline-result-wrap
2213 (prog1 (buffer-substring beg end)
2214 (delete-region beg end)))))
2215 (let ((size (count-lines beg end)))
2216 (save-excursion
2217 (cond ((= size 0)) ; do nothing for an empty result
2218 ((< size org-babel-min-lines-for-block-output)
2219 (goto-char beg)
2220 (dotimes (n size)
2221 (beginning-of-line 1) (insert ": ") (forward-line 1)))
2223 (goto-char beg)
2224 (insert (if results-switches
2225 (format "%s%s\n"
2226 (funcall maybe-cap "#+begin_example")
2227 results-switches)
2228 (funcall maybe-cap "#+begin_example\n")))
2229 (if (markerp end) (goto-char end) (forward-char (- end beg)))
2230 (insert (funcall maybe-cap "#+end_example\n")))))))))
2232 (defun org-babel-update-block-body (new-body)
2233 "Update the body of the current code block to NEW-BODY."
2234 (if (not (org-babel-where-is-src-block-head))
2235 (error "Not in a source block")
2236 (save-match-data
2237 (replace-match (concat (org-babel-trim new-body) "\n") nil t nil 5))
2238 (indent-rigidly (match-beginning 5) (match-end 5) 2)))
2240 (defun org-babel-merge-params (&rest plists)
2241 "Combine all parameter association lists in PLISTS.
2242 Later elements of PLISTS override the values of previous elements.
2243 This takes into account some special considerations for certain
2244 parameters when merging lists."
2245 (let* ((results-exclusive-groups
2246 (mapcar (lambda (group) (mapcar #'symbol-name group))
2247 (cdr (assoc 'results org-babel-common-header-args-w-values))))
2248 (exports-exclusive-groups
2249 (mapcar (lambda (group) (mapcar #'symbol-name group))
2250 (cdr (assoc 'exports org-babel-common-header-args-w-values))))
2251 (variable-index 0)
2252 (e-merge (lambda (exclusive-groups &rest result-params)
2253 ;; maintain exclusivity of mutually exclusive parameters
2254 (let (output)
2255 (mapc (lambda (new-params)
2256 (mapc (lambda (new-param)
2257 (mapc (lambda (exclusive-group)
2258 (when (member new-param exclusive-group)
2259 (mapcar (lambda (excluded-param)
2260 (setq output
2261 (delete
2262 excluded-param
2263 output)))
2264 exclusive-group)))
2265 exclusive-groups)
2266 (setq output (org-uniquify
2267 (cons new-param output))))
2268 new-params))
2269 result-params)
2270 output)))
2271 params results exports tangle noweb cache vars shebang comments padline)
2273 (mapc
2274 (lambda (plist)
2275 (mapc
2276 (lambda (pair)
2277 (case (car pair)
2278 (:var
2279 (let ((name (if (listp (cdr pair))
2280 (cadr pair)
2281 (and (string-match "^\\([^= \f\t\n\r\v]+\\)[ \t]*="
2282 (cdr pair))
2283 (intern (match-string 1 (cdr pair)))))))
2284 (if name
2285 (setq vars
2286 (append
2287 (if (member name (mapcar #'car vars))
2288 (delq nil
2289 (mapcar
2290 (lambda (p)
2291 (unless (equal (car p) name) p))
2292 vars))
2293 vars)
2294 (list (cons name pair))))
2295 ;; if no name is given and we already have named variables
2296 ;; then assign to named variables in order
2297 (if (and vars (nth variable-index vars))
2298 (prog1 (setf (cddr (nth variable-index vars))
2299 (concat (symbol-name
2300 (car (nth variable-index vars)))
2301 "=" (cdr pair)))
2302 (incf variable-index))
2303 (error "Variable \"%s\" must be assigned a default value"
2304 (cdr pair))))))
2305 (:results
2306 (setq results (funcall e-merge results-exclusive-groups
2307 results
2308 (split-string
2309 (let ((r (cdr pair)))
2310 (if (stringp r) r (eval r)))))))
2311 (:file
2312 (when (cdr pair)
2313 (setq results (funcall e-merge results-exclusive-groups
2314 results '("file")))
2315 (unless (or (member "both" exports)
2316 (member "none" exports)
2317 (member "code" exports))
2318 (setq exports (funcall e-merge exports-exclusive-groups
2319 exports '("results"))))
2320 (setq params (cons pair (assq-delete-all (car pair) params)))))
2321 (:exports
2322 (setq exports (funcall e-merge exports-exclusive-groups
2323 exports (split-string (cdr pair)))))
2324 (:tangle ;; take the latest -- always overwrite
2325 (setq tangle (or (list (cdr pair)) tangle)))
2326 (:noweb
2327 (setq noweb (funcall e-merge
2328 '(("yes" "no" "tangle" "no-export"
2329 "strip-export" "eval"))
2330 noweb
2331 (split-string (or (cdr pair) "")))))
2332 (:cache
2333 (setq cache (funcall e-merge '(("yes" "no")) cache
2334 (split-string (or (cdr pair) "")))))
2335 (:padline
2336 (setq padline (funcall e-merge '(("yes" "no")) padline
2337 (split-string (or (cdr pair) "")))))
2338 (:shebang ;; take the latest -- always overwrite
2339 (setq shebang (or (list (cdr pair)) shebang)))
2340 (:comments
2341 (setq comments (funcall e-merge '(("yes" "no")) comments
2342 (split-string (or (cdr pair) "")))))
2343 (t ;; replace: this covers e.g. :session
2344 (setq params (cons pair (assq-delete-all (car pair) params))))))
2345 plist))
2346 plists)
2347 (setq vars (reverse vars))
2348 (while vars (setq params (cons (cons :var (cddr (pop vars))) params)))
2349 (mapc
2350 (lambda (hd)
2351 (let ((key (intern (concat ":" (symbol-name hd))))
2352 (val (eval hd)))
2353 (setf params (cons (cons key (mapconcat 'identity val " ")) params))))
2354 '(results exports tangle noweb padline cache shebang comments))
2355 params))
2357 (defvar org-babel-use-quick-and-dirty-noweb-expansion nil
2358 "Set to true to use regular expressions to expand noweb references.
2359 This results in much faster noweb reference expansion but does
2360 not properly allow code blocks to inherit the \":noweb-ref\"
2361 header argument from buffer or subtree wide properties.")
2363 (defun org-babel-noweb-p (params context)
2364 "Check if PARAMS require expansion in CONTEXT.
2365 CONTEXT may be one of :tangle, :export or :eval."
2366 (let* (intersect
2367 (intersect (lambda (as bs)
2368 (when as
2369 (if (member (car as) bs)
2370 (car as)
2371 (funcall intersect (cdr as) bs))))))
2372 (funcall intersect (case context
2373 (:tangle '("yes" "tangle" "no-export" "strip-export"))
2374 (:eval '("yes" "no-export" "strip-export" "eval"))
2375 (:export '("yes")))
2376 (split-string (or (cdr (assoc :noweb params)) "")))))
2378 (defun org-babel-expand-noweb-references (&optional info parent-buffer)
2379 "Expand Noweb references in the body of the current source code block.
2381 For example the following reference would be replaced with the
2382 body of the source-code block named 'example-block'.
2384 <<example-block>>
2386 Note that any text preceding the <<foo>> construct on a line will
2387 be interposed between the lines of the replacement text. So for
2388 example if <<foo>> is placed behind a comment, then the entire
2389 replacement text will also be commented.
2391 This function must be called from inside of the buffer containing
2392 the source-code block which holds BODY.
2394 In addition the following syntax can be used to insert the
2395 results of evaluating the source-code block named 'example-block'.
2397 <<example-block()>>
2399 Any optional arguments can be passed to example-block by placing
2400 the arguments inside the parenthesis following the convention
2401 defined by `org-babel-lob'. For example
2403 <<example-block(a=9)>>
2405 would set the value of argument \"a\" equal to \"9\". Note that
2406 these arguments are not evaluated in the current source-code
2407 block but are passed literally to the \"example-block\"."
2408 (let* ((parent-buffer (or parent-buffer (current-buffer)))
2409 (info (or info (org-babel-get-src-block-info 'light)))
2410 (lang (nth 0 info))
2411 (body (nth 1 info))
2412 (ob-nww-start org-babel-noweb-wrap-start)
2413 (ob-nww-end org-babel-noweb-wrap-end)
2414 (comment (string= "noweb" (cdr (assoc :comments (nth 2 info)))))
2415 (rx-prefix (concat "\\(" org-babel-src-name-regexp "\\|"
2416 ":noweb-ref[ \t]+" "\\)"))
2417 (new-body "")
2418 (nb-add (lambda (text) (setq new-body (concat new-body text))))
2419 (c-wrap (lambda (text)
2420 (with-temp-buffer
2421 (funcall (intern (concat lang "-mode")))
2422 (comment-region (point) (progn (insert text) (point)))
2423 (org-babel-trim (buffer-string)))))
2424 index source-name evaluate prefix blocks-in-buffer)
2425 (with-temp-buffer
2426 (org-set-local 'org-babel-noweb-wrap-start ob-nww-start)
2427 (org-set-local 'org-babel-noweb-wrap-end ob-nww-end)
2428 (insert body) (goto-char (point-min))
2429 (setq index (point))
2430 (while (and (re-search-forward (org-babel-noweb-wrap) nil t))
2431 (save-match-data (setf source-name (match-string 1)))
2432 (save-match-data (setq evaluate (string-match "\(.*\)" source-name)))
2433 (save-match-data
2434 (setq prefix
2435 (buffer-substring (match-beginning 0)
2436 (save-excursion
2437 (beginning-of-line 1) (point)))))
2438 ;; add interval to new-body (removing noweb reference)
2439 (goto-char (match-beginning 0))
2440 (funcall nb-add (buffer-substring index (point)))
2441 (goto-char (match-end 0))
2442 (setq index (point))
2443 (funcall nb-add
2444 (with-current-buffer parent-buffer
2445 (save-restriction
2446 (widen)
2447 (mapconcat ;; interpose PREFIX between every line
2448 #'identity
2449 (split-string
2450 (if evaluate
2451 (let ((raw (org-babel-ref-resolve source-name)))
2452 (if (stringp raw) raw (format "%S" raw)))
2454 ;; retrieve from the library of babel
2455 (nth 2 (assoc (intern source-name)
2456 org-babel-library-of-babel))
2457 ;; return the contents of headlines literally
2458 (save-excursion
2459 (when (org-babel-ref-goto-headline-id source-name)
2460 (org-babel-ref-headline-body)))
2461 ;; find the expansion of reference in this buffer
2462 (let ((rx (concat rx-prefix source-name "[ \t\n]"))
2463 expansion)
2464 (save-excursion
2465 (goto-char (point-min))
2466 (if org-babel-use-quick-and-dirty-noweb-expansion
2467 (while (re-search-forward rx nil t)
2468 (let* ((i (org-babel-get-src-block-info 'light))
2469 (body (org-babel-expand-noweb-references i))
2470 (sep (or (cdr (assoc :noweb-sep (nth 2 i)))
2471 "\n"))
2472 (full (if comment
2473 ((lambda (cs)
2474 (concat (funcall c-wrap (car cs)) "\n"
2475 body "\n"
2476 (funcall c-wrap (cadr cs))))
2477 (org-babel-tangle-comment-links i))
2478 body)))
2479 (setq expansion (cons sep (cons full expansion)))))
2480 (org-babel-map-src-blocks nil
2481 (let ((i (org-babel-get-src-block-info 'light)))
2482 (when (equal (or (cdr (assoc :noweb-ref (nth 2 i)))
2483 (nth 4 i))
2484 source-name)
2485 (let* ((body (org-babel-expand-noweb-references i))
2486 (sep (or (cdr (assoc :noweb-sep (nth 2 i)))
2487 "\n"))
2488 (full (if comment
2489 ((lambda (cs)
2490 (concat (funcall c-wrap (car cs)) "\n"
2491 body "\n"
2492 (funcall c-wrap (cadr cs))))
2493 (org-babel-tangle-comment-links i))
2494 body)))
2495 (setq expansion
2496 (cons sep (cons full expansion)))))))))
2497 (and expansion
2498 (mapconcat #'identity (nreverse (cdr expansion)) "")))
2499 ;; possibly raise an error if named block doesn't exist
2500 (if (member lang org-babel-noweb-error-langs)
2501 (error "%s" (concat
2502 (org-babel-noweb-wrap source-name)
2503 "could not be resolved (see "
2504 "`org-babel-noweb-error-langs')"))
2505 "")))
2506 "[\n\r]") (concat "\n" prefix))))))
2507 (funcall nb-add (buffer-substring index (point-max))))
2508 new-body))
2510 (defun org-babel-script-escape (str &optional force)
2511 "Safely convert tables into elisp lists."
2512 (let (in-single in-double out)
2513 ((lambda (escaped) (condition-case nil (org-babel-read escaped) (error escaped)))
2514 (if (or force
2515 (and (stringp str)
2516 (> (length str) 2)
2517 (or (and (string-equal "[" (substring str 0 1))
2518 (string-equal "]" (substring str -1)))
2519 (and (string-equal "{" (substring str 0 1))
2520 (string-equal "}" (substring str -1)))
2521 (and (string-equal "(" (substring str 0 1))
2522 (string-equal ")" (substring str -1))))))
2523 (org-babel-read
2524 (concat
2526 (progn
2527 (mapc
2528 (lambda (ch)
2529 (setq
2531 (case ch
2532 (91 (if (or in-double in-single) ; [
2533 (cons 91 out)
2534 (cons 40 out)))
2535 (93 (if (or in-double in-single) ; ]
2536 (cons 93 out)
2537 (cons 41 out)))
2538 (123 (if (or in-double in-single) ; {
2539 (cons 123 out)
2540 (cons 40 out)))
2541 (125 (if (or in-double in-single) ; }
2542 (cons 125 out)
2543 (cons 41 out)))
2544 (44 (if (or in-double in-single) ; ,
2545 (cons 44 out) (cons 32 out)))
2546 (39 (if in-double ; '
2547 (cons 39 out)
2548 (setq in-single (not in-single)) (cons 34 out)))
2549 (34 (if in-single ; "
2550 (append (list 34 32) out)
2551 (setq in-double (not in-double)) (cons 34 out)))
2552 (t (cons ch out)))))
2553 (string-to-list str))
2554 (apply #'string (reverse out)))))
2555 str))))
2557 (defun org-babel-read (cell &optional inhibit-lisp-eval)
2558 "Convert the string value of CELL to a number if appropriate.
2559 Otherwise if cell looks like lisp (meaning it starts with a
2560 \"(\", \"'\", \"`\" or a \"[\") then read it as lisp,
2561 otherwise return it unmodified as a string. Optional argument
2562 NO-LISP-EVAL inhibits lisp evaluation for situations in which is
2563 it not appropriate."
2564 (if (and (stringp cell) (not (equal cell "")))
2565 (or (org-babel-number-p cell)
2566 (if (and (not inhibit-lisp-eval)
2567 (or (member (substring cell 0 1) '("(" "'" "`" "["))
2568 (string= cell "*this*")))
2569 (eval (read cell))
2570 (if (string= (substring cell 0 1) "\"")
2571 (read cell)
2572 (progn (set-text-properties 0 (length cell) nil cell) cell))))
2573 cell))
2575 (defun org-babel-number-p (string)
2576 "If STRING represents a number return its value."
2577 (if (and (string-match "[0-9]+" string)
2578 (string-match "^-?[0-9]*\\.?[0-9]*$" string)
2579 (= (length (substring string (match-beginning 0)
2580 (match-end 0)))
2581 (length string)))
2582 (string-to-number string)))
2584 (defun org-babel-import-elisp-from-file (file-name &optional separator)
2585 "Read the results located at FILE-NAME into an elisp table.
2586 If the table is trivial, then return it as a scalar."
2587 (let (result)
2588 (save-window-excursion
2589 (with-temp-buffer
2590 (condition-case err
2591 (progn
2592 (org-table-import file-name separator)
2593 (delete-file file-name)
2594 (setq result (mapcar (lambda (row)
2595 (mapcar #'org-babel-string-read row))
2596 (org-table-to-lisp))))
2597 (error (message "Error reading results: %s" err) nil)))
2598 (if (null (cdr result)) ;; if result is trivial vector, then scalarize it
2599 (if (consp (car result))
2600 (if (null (cdr (car result)))
2601 (caar result)
2602 result)
2603 (car result))
2604 result))))
2606 (defun org-babel-string-read (cell)
2607 "Strip nested \"s from around strings."
2608 (org-babel-read (or (and (stringp cell)
2609 (string-match "\\\"\\(.+\\)\\\"" cell)
2610 (match-string 1 cell))
2611 cell) t))
2613 (defun org-babel-chomp (string &optional regexp)
2614 "Strip trailing spaces and carriage returns from STRING.
2615 Default regexp used is \"[ \f\t\n\r\v]\" but can be
2616 overwritten by specifying a regexp as a second argument."
2617 (let ((regexp (or regexp "[ \f\t\n\r\v]")))
2618 (while (and (> (length string) 0)
2619 (string-match regexp (substring string -1)))
2620 (setq string (substring string 0 -1)))
2621 string))
2623 (defun org-babel-trim (string &optional regexp)
2624 "Strip leading and trailing spaces and carriage returns from STRING.
2625 Like `org-babel-chomp' only it runs on both the front and back
2626 of the string."
2627 (org-babel-chomp (org-reverse-string
2628 (org-babel-chomp (org-reverse-string string) regexp))
2629 regexp))
2631 (defun org-babel-tramp-handle-call-process-region
2632 (start end program &optional delete buffer display &rest args)
2633 "Use Tramp to handle `call-process-region'.
2634 Fixes a bug in `tramp-handle-call-process-region'."
2635 (if (and (featurep 'tramp) (file-remote-p default-directory))
2636 (let ((tmpfile (tramp-compat-make-temp-file "")))
2637 (write-region start end tmpfile)
2638 (when delete (delete-region start end))
2639 (unwind-protect
2640 ;; (apply 'call-process program tmpfile buffer display args)
2641 ;; bug in tramp
2642 (apply 'process-file program tmpfile buffer display args)
2643 (delete-file tmpfile)))
2644 ;; org-babel-call-process-region-original is the original emacs
2645 ;; definition. It is in scope from the let binding in
2646 ;; org-babel-execute-src-block
2647 (apply org-babel-call-process-region-original
2648 start end program delete buffer display args)))
2650 (defun org-babel-local-file-name (file)
2651 "Return the local name component of FILE."
2652 (if (file-remote-p file)
2653 (let (localname)
2654 (with-parsed-tramp-file-name file nil
2655 localname))
2656 file))
2658 (defun org-babel-process-file-name (name &optional no-quote-p)
2659 "Prepare NAME to be used in an external process.
2660 If NAME specifies a remote location, the remote portion of the
2661 name is removed, since in that case the process will be executing
2662 remotely. The file name is then processed by `expand-file-name'.
2663 Unless second argument NO-QUOTE-P is non-nil, the file name is
2664 additionally processed by `shell-quote-argument'"
2665 ((lambda (f) (if no-quote-p f (shell-quote-argument f)))
2666 (expand-file-name (org-babel-local-file-name name))))
2668 (defvar org-babel-temporary-directory)
2669 (unless (or noninteractive (boundp 'org-babel-temporary-directory))
2670 (defvar org-babel-temporary-directory
2671 (or (and (boundp 'org-babel-temporary-directory)
2672 (file-exists-p org-babel-temporary-directory)
2673 org-babel-temporary-directory)
2674 (make-temp-file "babel-" t))
2675 "Directory to hold temporary files created to execute code blocks.
2676 Used by `org-babel-temp-file'. This directory will be removed on
2677 Emacs shutdown."))
2679 (defmacro org-babel-result-cond (result-params scalar-form &rest table-forms)
2680 "Call the code to parse raw string results according to RESULT-PARAMS."
2681 (declare (indent 1)
2682 (debug (form form &rest form)))
2683 (org-with-gensyms (params)
2684 `(let ((,params ,result-params))
2685 (unless (member "none" ,params)
2686 (if (or (member "scalar" ,params)
2687 (member "verbatim" ,params)
2688 (member "html" ,params)
2689 (member "code" ,params)
2690 (member "pp" ,params)
2691 (and (or (member "output" ,params)
2692 (member "raw" ,params)
2693 (member "org" ,params)
2694 (member "drawer" ,params))
2695 (not (member "table" ,params))))
2696 ,scalar-form
2697 ,@table-forms)))))
2698 (def-edebug-spec org-babel-result-cond (form form body))
2700 (defun org-babel-temp-file (prefix &optional suffix)
2701 "Create a temporary file in the `org-babel-temporary-directory'.
2702 Passes PREFIX and SUFFIX directly to `make-temp-file' with the
2703 value of `temporary-file-directory' temporarily set to the value
2704 of `org-babel-temporary-directory'."
2705 (if (file-remote-p default-directory)
2706 (let ((prefix
2707 (concat (file-remote-p default-directory)
2708 (expand-file-name prefix temporary-file-directory))))
2709 (make-temp-file prefix nil suffix))
2710 (let ((temporary-file-directory
2711 (or (and (boundp 'org-babel-temporary-directory)
2712 (file-exists-p org-babel-temporary-directory)
2713 org-babel-temporary-directory)
2714 temporary-file-directory)))
2715 (make-temp-file prefix nil suffix))))
2717 (defun org-babel-remove-temporary-directory ()
2718 "Remove `org-babel-temporary-directory' on Emacs shutdown."
2719 (when (and (boundp 'org-babel-temporary-directory)
2720 (file-exists-p org-babel-temporary-directory))
2721 ;; taken from `delete-directory' in files.el
2722 (condition-case nil
2723 (progn
2724 (mapc (lambda (file)
2725 ;; This test is equivalent to
2726 ;; (and (file-directory-p fn) (not (file-symlink-p fn)))
2727 ;; but more efficient
2728 (if (eq t (car (file-attributes file)))
2729 (delete-directory file)
2730 (delete-file file)))
2731 ;; We do not want to delete "." and "..".
2732 (directory-files org-babel-temporary-directory 'full
2733 "^\\([^.]\\|\\.\\([^.]\\|\\..\\)\\).*"))
2734 (delete-directory org-babel-temporary-directory))
2735 (error
2736 (message "Failed to remove temporary Org-babel directory %s"
2737 (if (boundp 'org-babel-temporary-directory)
2738 org-babel-temporary-directory
2739 "[directory not defined]"))))))
2741 (add-hook 'kill-emacs-hook 'org-babel-remove-temporary-directory)
2743 (provide 'ob-core)
2745 ;; Local variables:
2746 ;; generated-autoload-file: "org-loaddefs.el"
2747 ;; End:
2749 ;;; ob-core.el ends here