Use the normal overlay API, not Org's one
[org-mode.git] / contrib / babel / lisp / org-babel.el
blobb348503bab3222181ae7c80f1ba95fd065799084
1 ;;; org-babel.el --- facilitating communication between programming languages and people
3 ;; Copyright (C) 2009 Eric Schulte, Dan Davison
5 ;; Author: Eric Schulte, Dan Davison
6 ;; Keywords: literate programming, reproducible research
7 ;; Homepage: http://orgmode.org
8 ;; Version: 0.01
10 ;;; License:
12 ;; This program 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, or (at your option)
15 ;; any later version.
17 ;; This program 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; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
27 ;;; Commentary:
29 ;; See org-babel.org in the parent directory for more information
31 ;;; Code:
32 (require 'org)
34 (defun org-babel-execute-src-block-maybe ()
35 "Detect if this is context for a org-babel src-block and if so
36 then run `org-babel-execute-src-block'."
37 (interactive)
38 (let ((info (org-babel-get-src-block-info)))
39 (if info (progn (org-babel-execute-src-block current-prefix-arg info) t) nil)))
41 (add-hook 'org-ctrl-c-ctrl-c-hook 'org-babel-execute-src-block-maybe)
43 (defadvice org-edit-special (around org-babel-prep-session-for-edit activate)
44 "Prepare the current source block's session according to it's
45 header arguments before editing in an org-src buffer. This
46 function is called when `org-edit-special' is called with a
47 prefix argument from inside of a source-code block."
48 (when current-prefix-arg
49 (let* ((info (org-babel-get-src-block-info))
50 (lang (first info))
51 (params (third info))
52 (session (cdr (assoc :session params))))
53 (when (and info session) ;; if we are in a source-code block which has a session
54 (funcall (intern (concat "org-babel-prep-session:" lang)) session params))))
55 ad-do-it)
57 (defadvice org-open-at-point (around org-babel-open-at-point activate)
58 "If `point' is on a source code block, then open that block's
59 results with `org-babel-open-src-block-results', otherwise defer
60 to `org-open-at-point'."
61 (interactive "P")
62 (or (call-interactively #'org-babel-open-src-block-result) ad-do-it))
64 (defun org-babel-load-in-session-maybe ()
65 "Detect if this is context for a org-babel src-block and if so
66 then run `org-babel-load-in-session'."
67 (interactive)
68 (let ((info (org-babel-get-src-block-info)))
69 (if info (progn (org-babel-load-in-session current-prefix-arg info) t) nil)))
71 (add-hook 'org-metaup-hook 'org-babel-load-in-session-maybe)
73 (defun org-babel-pop-to-session-maybe ()
74 "Detect if this is context for a org-babel src-block and if so
75 then run `org-babel-pop-to-session'."
76 (interactive)
77 (let ((info (org-babel-get-src-block-info)))
78 (if info (progn (org-babel-pop-to-session current-prefix-arg info) t) nil)))
80 (add-hook 'org-metadown-hook 'org-babel-pop-to-session-maybe)
82 (defconst org-babel-header-arg-names
83 '(cache cmdline colnames dir exports file noweb results session tangle var)
84 "Common header arguments used by org-babel. Note that
85 individual languages may define their own language specific
86 header arguments as well.")
88 (defvar org-babel-default-header-args
89 '((:session . "none") (:results . "replace") (:exports . "code") (:cache . "no") (:noweb . "no"))
90 "Default arguments to use when evaluating a source block.")
92 (defvar org-babel-default-inline-header-args
93 '((:session . "none") (:results . "silent") (:exports . "results"))
94 "Default arguments to use when evaluating an inline source block.")
96 (defvar org-babel-src-block-regexp nil
97 "Regexp used to test when inside of a org-babel src-block")
99 (defvar org-babel-inline-src-block-regexp nil
100 "Regexp used to test when on an inline org-babel src-block")
102 (defvar org-babel-result-regexp
103 "^[ \t]*#\\+res\\(ults\\|name\\)\\(\\[\\([[:alnum:]]+\\)\\]\\)?\\:"
104 "Regular expression used to match result lines. If the
105 results are associated with a hash key then the hash will be
106 saved in the second match data.")
108 (defvar org-babel-source-name-regexp
109 "^[ \t]*#\\+\\(srcname\\|source\\|function\\):[ \t]*"
110 "Regular expression used to match a source name line.")
112 (defvar org-babel-min-lines-for-block-output 10
113 "If number of lines of output is equal to or exceeds this
114 value, the output is placed in a #+begin_example...#+end_example
115 block. Otherwise the output is marked as literal by inserting
116 colons at the starts of the lines. This variable only takes
117 effect if the :results output option is in effect.")
119 (defvar org-babel-noweb-error-langs nil
120 "List of language for which errors should be raised when the
121 source code block satisfying a noweb reference in this language
122 can not be resolved.")
124 (defvar org-babel-hash-show 4
125 "Number of initial characters to show of a hidden results hash.")
127 (defvar org-babel-after-execute-hook nil
128 "Hook for functions to be called after `org-babel-execute-src-block'")
129 (defun org-babel-named-src-block-regexp-for-name (name)
130 "Regexp used to match named src block."
131 (concat org-babel-source-name-regexp (regexp-quote name) "[ \t\n]*"
132 (substring org-babel-src-block-regexp 1)))
134 (defun org-babel-set-interpreters (var value)
135 (set-default var value)
136 (setq org-babel-src-block-regexp
137 (concat "^[ \t]*#\\+begin_src[ \t]+\\(" ;; (1) lang
138 (mapconcat 'regexp-quote value "\\|")
139 "\\)[ \t]*"
140 "\\([^\":\n]*\"[^\"\n*]*\"[^\":\n]*\\|[^\":\n]*\\)" ;; (2) switches
141 "\\([^\n]*\\)\n" ;; (3) header arguments
142 "\\([^\000]+?\n\\)[ \t]*#\\+end_src"));; (4) body
143 (setq org-babel-inline-src-block-regexp
144 (concat "[ \f\t\n\r\v]\\(src_" ;; (1) replacement target
145 "\\(" ;; (2) lang
146 (mapconcat 'regexp-quote value "\\|")
147 "\\)"
148 "\\(\\|\\[\\(.*\\)\\]\\)" ;; (3,4) (unused, headers)
149 "{\\([^\f\n\r\v]+\\)}" ;; (5) body
150 "\\)")))
152 (defun org-babel-add-interpreter (interpreter)
153 "Add INTERPRETER to `org-babel-interpreters' and update
154 `org-babel-src-block-regexp' appropriately."
155 (unless (member interpreter org-babel-interpreters)
156 (setq org-babel-interpreters
157 (sort (cons interpreter org-babel-interpreters)
158 (lambda (left right)
159 (> (length left) (length right)))))
160 (org-babel-set-interpreters 'org-babel-interpreters org-babel-interpreters)))
162 (defcustom org-babel-interpreters '()
163 "Interpreters allows for evaluation tags.
164 This is a list of program names (as strings) that can evaluate code and
165 insert the output into an Org-mode buffer. Valid choices are
167 R Evaluate R code
168 emacs-lisp Evaluate Emacs Lisp code and display the result
169 sh Pass command to the shell and display the result
170 perl The perl interpreter
171 python The python interpreter
172 ruby The ruby interpreter
174 The source block regexp `org-babel-src-block-regexp' is updated
175 when a new interpreter is added to this list through the
176 customize interface. To add interpreters to this variable from
177 lisp code use the `org-babel-add-interpreter' function."
178 :group 'org-babel
179 :set 'org-babel-set-interpreters
180 :type '(set :greedy t
181 (const "R")
182 (const "emacs-lisp")
183 (const "sh")
184 (const "perl")
185 (const "python")
186 (const "ruby")))
188 ;;; functions
189 (defun org-babel-execute-src-block (&optional arg info params)
190 "Execute the current source code block, and insert the results
191 into the buffer. Source code execution and the collection and
192 formatting of results can be controlled through a variety of
193 header arguments.
195 Optionally supply a value for INFO in the form returned by
196 `org-babel-get-src-block-info'.
198 Optionally supply a value for PARAMS which will be merged with
199 the header arguments specified at the front of the source code
200 block."
201 (interactive)
202 ;; (message "supplied params=%S" params) ;; debugging
203 (let* ((info (or info (org-babel-get-src-block-info)))
204 (lang (first info))
205 (params (setf (third info)
206 (sort (org-babel-merge-params (third info) params)
207 (lambda (el1 el2) (string< (symbol-name (car el1))
208 (symbol-name (car el2)))))))
209 (new-hash (if (and (cdr (assoc :cache params))
210 (string= "yes" (cdr (assoc :cache params)))) (org-babel-sha1-hash info)))
211 (old-hash (org-babel-result-hash info))
212 (body (setf (second info)
213 (if (and (cdr (assoc :noweb params))
214 (string= "yes" (cdr (assoc :noweb params))))
215 (org-babel-expand-noweb-references info) (second info))))
217 (result-params (split-string (or (cdr (assoc :results params)) "")))
218 (result-type (cond ((member "output" result-params) 'output)
219 ((member "value" result-params) 'value)
220 (t 'value)))
221 (cmd (intern (concat "org-babel-execute:" lang)))
222 (dir (cdr (assoc :dir params)))
223 (default-directory
224 (or (and dir (file-name-as-directory dir)) default-directory))
225 (call-process-region-original
226 (if (boundp 'call-process-region-original) call-process-region-original
227 (symbol-function 'call-process-region)))
228 result)
229 (unwind-protect
230 (flet ((call-process-region (&rest args)
231 (apply 'org-babel-tramp-handle-call-process-region args)))
232 (unless (member lang org-babel-interpreters)
233 (error "Language is not in `org-babel-interpreters': %s" lang))
234 (if (and (not arg) new-hash (equal new-hash old-hash))
235 (save-excursion ;; return cached result
236 (goto-char (org-babel-where-is-src-block-result nil info))
237 (move-end-of-line 1) (forward-char 1)
238 (setq result (org-babel-read-result))
239 (message (replace-regexp-in-string "%" "%%" (format "%S" result))) result)
240 (setq result (funcall cmd body params))
241 (if (eq result-type 'value)
242 (setq result (if (and (or (member "vector" result-params)
243 (member "table" result-params))
244 (not (listp result)))
245 (list (list result))
246 result)))
247 (org-babel-insert-result result result-params info new-hash)
248 (run-hooks 'org-babel-after-execute-hook)
249 result))
250 (setq call-process-region 'call-process-region-original))))
252 (defun org-babel-load-in-session (&optional arg info)
253 "Load the body of the current source-code block. Evaluate the
254 header arguments for the source block before entering the
255 session. After loading the body this pops open the session."
256 (interactive)
257 (let* ((info (or info (org-babel-get-src-block-info)))
258 (lang (first info))
259 (body (second info))
260 (params (third info))
261 (session (cdr (assoc :session params))))
262 (unless (member lang org-babel-interpreters)
263 (error "Language is not in `org-babel-interpreters': %s" lang))
264 ;; if called with a prefix argument, then process header arguments
265 (pop-to-buffer (funcall (intern (concat "org-babel-load-session:" lang)) session body params))
266 (move-end-of-line 1)))
268 (defun org-babel-switch-to-session (&optional arg info)
269 "Switch to the session of the current source-code block.
270 If called with a prefix argument then evaluate the header arguments
271 for the source block before entering the session. Copy the body
272 of the source block to the kill ring."
273 (interactive)
274 (let* ((info (or info (org-babel-get-src-block-info)))
275 (lang (first info))
276 (body (second info))
277 (params (third info))
278 (session (cdr (assoc :session params)))
279 (dir (cdr (assoc :dir params)))
280 (default-directory
281 (or (and dir (file-name-as-directory dir)) default-directory)))
282 (unless (member lang org-babel-interpreters)
283 (error "Language is not in `org-babel-interpreters': %s" lang))
284 ;; copy body to the kill ring
285 (with-temp-buffer (insert (org-babel-trim body)) (copy-region-as-kill (point-min) (point-max)))
286 ;; if called with a prefix argument, then process header arguments
287 (if arg (funcall (intern (concat "org-babel-prep-session:" lang)) session params))
288 ;; just to the session using pop-to-buffer
289 (pop-to-buffer (funcall (intern (format "org-babel-%s-initiate-session" lang)) session params))
290 (move-end-of-line 1)))
292 (defalias 'org-babel-pop-to-session 'org-babel-switch-to-session)
294 (defun org-babel-open-src-block-result (&optional re-run)
295 "If `point' is on a src block then open the results of the
296 source code block, otherwise return nil. With optional prefix
297 argument RE-RUN the source-code block is evaluated even if
298 results already exist."
299 (interactive "P")
300 (when (org-babel-get-src-block-info)
301 (save-excursion
302 ;; go to the results, if there aren't any then run the block
303 (goto-char (or (and (not re-run) (org-babel-where-is-src-block-result))
304 (progn (org-babel-execute-src-block)
305 (org-babel-where-is-src-block-result))))
306 (move-end-of-line 1) (forward-char 1)
307 ;; open the results
308 (if (looking-at org-bracket-link-regexp)
309 ;; file results
310 (org-open-at-point)
311 (let ((results (org-babel-read-result)))
312 (flet ((echo-res (result)
313 (if (stringp result) result (format "%S" result))))
314 (pop-to-buffer (get-buffer-create "org-babel-results"))
315 (delete-region (point-min) (point-max))
316 (if (listp results)
317 ;; table result
318 (insert (orgtbl-to-generic results '(:sep "\t" :fmt echo-res)))
319 ;; scalar result
320 (insert (echo-res results))))))
321 t)))
323 (defun org-babel-execute-buffer (&optional arg)
324 "Call `org-babel-execute-src-block' on every source block in
325 the current buffer."
326 (interactive "P")
327 (save-excursion
328 (goto-char (point-min))
329 (while (re-search-forward org-babel-src-block-regexp nil t)
330 (let ((pos-end (match-end 0)))
331 (goto-char (match-beginning 0))
332 (org-babel-execute-src-block arg)
333 (goto-char pos-end)))))
335 (defun org-babel-execute-subtree (&optional arg)
336 "Call `org-babel-execute-src-block' on every source block in
337 the current subtree."
338 (interactive "P")
339 (save-excursion
340 (org-narrow-to-subtree)
341 (org-babel-execute-buffer)
342 (widen)))
344 (defun org-babel-get-src-block-info (&optional header-vars-only)
345 "Get information of the current source block.
346 Returns a list
347 (language body header-arguments-alist switches name function-args).
348 Unless HEADER-VARS-ONLY is non-nil, any variable
349 references provided in 'function call style' (i.e. in a
350 parenthesised argument list following the src block name) are
351 added to the header-arguments-alist."
352 (let ((case-fold-search t) head info args)
353 (if (setq head (org-babel-where-is-src-block-head))
354 (save-excursion
355 (goto-char head)
356 (setq info (org-babel-parse-src-block-match))
357 (forward-line -1)
358 (when (looking-at (concat org-babel-source-name-regexp
359 "\\([^ ()\f\t\n\r\v]+\\)\\(\(\\(.*\\)\)\\|\\)"))
360 (setq info (append info (list (org-babel-clean-text-properties (match-string 2)))))
361 ;; Note that e.g. "name()" and "name( )" result in ((:var . "")).
362 ;; We maintain that behaviour, and the resulting non-nil sixth
363 ;; element is relied upon in org-babel-exp-code to detect a functional-style
364 ;; block in those cases. However, "name" without any
365 ;; parentheses would result in the same thing, so we
366 ;; explicitly avoid that.
367 (if (setq args (match-string 4))
368 (setq info (append info (list (mapcar (lambda (ref) (cons :var ref))
369 (org-babel-ref-split-args args))))))
370 (unless header-vars-only
371 (setf (third info)
372 (org-babel-merge-params (sixth info) (third info)))))
373 info)
374 (if (save-excursion ;; inline source block
375 (re-search-backward "[ \f\t\n\r\v]" nil t)
376 (looking-at org-babel-inline-src-block-regexp))
377 (org-babel-parse-inline-src-block-match)
378 nil)))) ;; indicate that no source block was found
380 (defun org-babel-sha1-hash (&optional info)
381 (interactive)
382 (let* ((info (or info (org-babel-get-src-block-info)))
383 (hash (sha1 (format "%s-%s" (mapconcat (lambda (arg) (format "%S" arg))
384 (third info) ":")
385 (second info)))))
386 (when (interactive-p) (message hash))
387 hash))
389 (defun org-babel-result-hash (&optional info)
390 (org-babel-where-is-src-block-result nil info)
391 (org-babel-clean-text-properties (match-string 3)))
393 (defun org-babel-hide-hash ()
394 "Hide the hash in the current results line. Only the initial
395 `org-babel-hash-show' characters of the hash will remain
396 visible."
397 (org-add-to-invisibility-spec '(org-babel-hide-hash . t))
398 (save-excursion
399 (when (and (re-search-forward org-babel-result-regexp nil t)
400 (match-string 3))
401 (let* ((start (match-beginning 3))
402 (hide-start (+ org-babel-hash-show start))
403 (end (match-end 3))
404 (hash (match-string 3))
405 ov1 ov2)
406 (setq ov1 (make-overlay start hide-start))
407 (setq ov2 (make-overlay hide-start end))
408 (overlay-put ov2 'invisible 'org-babel-hide-hash)
409 (overlay-put ov1 'babel-hash hash)))))
411 (defun org-babel-hide-all-hashes ()
412 "Hide the hash in the current buffer. Only the initial
413 `org-babel-hash-show' characters of each hash will remain
414 visible. This function should be called as part of the
415 `org-mode-hook'."
416 (save-excursion
417 (while (re-search-forward org-babel-result-regexp nil t)
418 (goto-char (match-beginning 0))
419 (org-babel-hide-hash)
420 (goto-char (match-end 0)))))
421 (add-hook 'org-mode-hook 'org-babel-hide-all-hashes)
423 (defun org-babel-hash-at-point (&optional point)
424 "Return the value of the hash at `point'. The hash is also
425 added as the last element of the kill ring. This can be called
426 with C-c C-c."
427 (interactive)
428 (let ((hash (car (delq nil (mapcar
429 (lambda (ol) (overlay-get ol 'babel-hash))
430 (org-overlays-at (or point (point))))))))
431 (when hash (kill-new hash) (message hash))))
432 (add-hook 'org-ctrl-c-ctrl-c-hook 'org-babel-hash-at-point)
434 (defun org-babel-result-hide-spec ()
435 (org-add-to-invisibility-spec '(org-babel-hide-result . t)))
436 (add-hook 'org-mode-hook 'org-babel-result-hide-spec)
438 (defvar org-babel-hide-result-overlays nil
439 "Overlays hiding results.")
441 (defun org-babel-result-hide-all ()
442 "Fold all results in the current buffer."
443 (interactive)
444 (org-babel-show-result-all)
445 (save-excursion
446 (while (re-search-forward org-babel-result-regexp nil t)
447 (save-excursion (goto-char (match-beginning 0))
448 (org-babel-hide-result-toggle-maybe)))))
450 (defun org-babel-show-result-all ()
451 "Unfold all results in the current buffer."
452 (mapc 'delete-overlay org-babel-hide-result-overlays)
453 (setq org-babel-hide-result-overlays nil))
455 (defun org-babel-hide-result-toggle-maybe ()
456 "Toggle visibility of result at point."
457 (interactive)
458 (let ((case-fold-search t))
459 (if (save-excursion
460 (beginning-of-line 1)
461 (looking-at org-babel-result-regexp))
462 (progn (org-babel-hide-result-toggle)
463 t) ;; to signal that we took action
464 nil))) ;; to signal that we did not
466 (defun org-babel-hide-result-toggle (&optional force)
467 "Toggle the visibility of the current result."
468 (interactive)
469 (save-excursion
470 (beginning-of-line)
471 (if (re-search-forward org-babel-result-regexp nil t)
472 (let ((start (progn (beginning-of-line 2) (- (point) 1)))
473 (end (progn (goto-char (- (org-babel-result-end) 1)) (point)))
475 (if (memq t (mapcar (lambda (overlay)
476 (eq (overlay-get overlay 'invisible)
477 'org-babel-hide-result))
478 (org-overlays-at start)))
479 (if (or (not force) (eq force 'off))
480 (mapc (lambda (ov)
481 (when (member ov org-babel-hide-result-overlays)
482 (setq org-babel-hide-result-overlays
483 (delq ov org-babel-hide-result-overlays)))
484 (when (eq (overlay-get ov 'invisible)
485 'org-babel-hide-result)
486 (delete-overlay ov)))
487 (org-overlays-at start)))
488 (setq ov (make-overlay start end))
489 (overlay-put ov 'invisible 'org-babel-hide-result)
490 ;; make the block accessible to isearch
491 (overlay-put
492 ov 'isearch-open-invisible
493 (lambda (ov)
494 (when (member ov org-babel-hide-result-overlays)
495 (setq org-babel-hide-result-overlays
496 (delq ov org-babel-hide-result-overlays)))
497 (when (eq (overlay-get ov 'invisible)
498 'org-babel-hide-result)
499 (delete-overlay ov))))
500 (push ov org-babel-hide-result-overlays)))
501 (error "Not looking at a result line"))))
503 ;; org-tab-after-check-for-cycling-hook
504 (add-hook 'org-tab-first-hook 'org-babel-hide-result-toggle-maybe)
505 ;; Remove overlays when changing major mode
506 (add-hook 'org-mode-hook
507 (lambda () (org-add-hook 'change-major-mode-hook
508 'org-babel-show-result-all 'append 'local)))
510 (defmacro org-babel-map-source-blocks (file &rest body)
511 "Evaluate BODY forms on each source-block in FILE."
512 (declare (indent 1))
513 `(let ((visited-p (get-buffer (file-name-nondirectory ,file))))
514 (save-window-excursion
515 (find-file ,file) (goto-char (point-min))
516 (while (re-search-forward org-babel-src-block-regexp nil t)
517 (goto-char (match-beginning 0))
518 (save-match-data ,@body)
519 (goto-char (match-end 0))))
520 (unless visited-p (kill-buffer (file-name-nondirectory file)))))
522 (defun org-babel-params-from-properties ()
523 "Return an association list of any source block params which
524 may be specified in the properties of the current outline entry."
525 (save-match-data
526 (delq nil
527 (mapcar
528 (lambda (header-arg)
529 (let ((val (or (condition-case nil
530 (org-entry-get (point) header-arg t)
531 (error nil))
532 (cdr (assoc header-arg org-file-properties)))))
533 (when val
534 ;; (message "prop %s=%s" header-arg val) ;; debugging
535 (cons (intern (concat ":" header-arg)) val))))
536 (mapcar 'symbol-name org-babel-header-arg-names)))))
538 (defun org-babel-parse-src-block-match ()
539 (let* ((lang (org-babel-clean-text-properties (match-string 1)))
540 (lang-headers (intern (concat "org-babel-default-header-args:" lang)))
541 (switches (match-string 2))
542 (body (org-babel-clean-text-properties (match-string 4)))
543 (preserve-indentation (or org-src-preserve-indentation
544 (string-match "-i\\>" switches))))
545 (list lang
546 ;; get src block body removing properties, protective commas, and indentation
547 (with-temp-buffer
548 (save-match-data
549 (insert (org-babel-strip-protective-commas body))
550 (unless preserve-indentation (org-do-remove-indentation))
551 (buffer-string)))
552 (org-babel-merge-params
553 org-babel-default-header-args
554 (org-babel-params-from-properties)
555 (if (boundp lang-headers) (eval lang-headers) nil)
556 (org-babel-parse-header-arguments (org-babel-clean-text-properties (or (match-string 3) ""))))
557 switches)))
559 (defun org-babel-parse-inline-src-block-match ()
560 (let* ((lang (org-babel-clean-text-properties (match-string 2)))
561 (lang-headers (intern (concat "org-babel-default-header-args:" lang))))
562 (list lang
563 (org-babel-strip-protective-commas (org-babel-clean-text-properties (match-string 5)))
564 (org-babel-merge-params
565 org-babel-default-inline-header-args
566 (org-babel-params-from-properties)
567 (if (boundp lang-headers) (eval lang-headers) nil)
568 (org-babel-parse-header-arguments (org-babel-clean-text-properties (or (match-string 4) "")))))))
570 (defun org-babel-parse-header-arguments (arg-string)
571 "Parse a string of header arguments returning an alist."
572 (if (> (length arg-string) 0)
573 (delq nil
574 (mapcar
575 (lambda (arg)
576 (if (string-match "\\([^ \f\t\n\r\v]+\\)[ \f\t\n\r\v]+\\([^ \f\t\n\r\v]+.*\\)" arg)
577 (cons (intern (concat ":" (match-string 1 arg)))
578 (let ((raw (org-babel-chomp (match-string 2 arg))))
579 (if (org-babel-number-p raw) raw (org-babel-read raw))))
580 (cons (intern (concat ":" arg)) nil)))
581 (split-string (concat " " arg-string) "[ \f\t\n\r\v]+:" t)))))
583 (defun org-babel-process-params (params)
584 "Parse params and resolve references.
586 Return a list (session vars result-params result-type)."
587 (let* ((session (cdr (assoc :session params)))
588 (vars (org-babel-ref-variables params))
589 (result-params (split-string (or (cdr (assoc :results params)) "")))
590 (result-type (cond ((member "output" result-params) 'output)
591 ((member "value" result-params) 'value)
592 (t 'value))))
593 (list session vars result-params result-type)))
595 (defun org-babel-where-is-src-block-head ()
596 "Return the point at the beginning of the current source
597 block. Specifically at the beginning of the #+BEGIN_SRC line.
598 If the point is not on a source block then return nil."
599 (let ((initial (point)) top bottom)
601 (save-excursion ;; on a source name line
602 (beginning-of-line 1)
603 (and (looking-at org-babel-source-name-regexp) (forward-line 1)
604 (looking-at org-babel-src-block-regexp)
605 (point)))
606 (save-excursion ;; on a #+begin_src line
607 (beginning-of-line 1)
608 (and (looking-at org-babel-src-block-regexp)
609 (point)))
610 (save-excursion ;; inside a src block
611 (and
612 (re-search-backward "^[ \t]*#\\+begin_src" nil t) (setq top (point))
613 (re-search-forward "^[ \t]*#\\+end_src" nil t) (setq bottom (point))
614 (< top initial) (< initial bottom)
615 (goto-char top) (move-beginning-of-line 1)
616 (looking-at org-babel-src-block-regexp)
617 (point))))))
619 (defun org-babel-goto-named-source-block (&optional name)
620 "Go to a named source-code block."
621 (interactive "ssource-block name: ")
622 (let ((point (org-babel-find-named-block name)))
623 (if point
624 ;; taken from `org-open-at-point'
625 (progn (goto-char point) (org-show-context))
626 (message "source-code block '%s' not found in this buffer" name))))
628 (defun org-babel-find-named-block (name)
629 "Find a named source-code block.
630 Return the location of the source block identified by source
631 NAME, or nil if no such block exists. Set match data according to
632 org-babel-named-src-block-regexp."
633 (save-excursion
634 (let ((case-fold-search t)
635 (regexp (org-babel-named-src-block-regexp-for-name name)) msg)
636 (goto-char (point-min))
637 (when (or (re-search-forward regexp nil t)
638 (re-search-backward regexp nil t))
639 (match-beginning 0)))))
641 (defun org-babel-find-named-result (name)
642 "Return the location of the result named NAME in the current
643 buffer or nil if no such result exists."
644 (save-excursion
645 (goto-char (point-min))
646 (when (re-search-forward
647 (concat org-babel-result-regexp "[ \t]" (regexp-quote name) "[ \t\n\f\v\r]") nil t)
648 (move-beginning-of-line 0) (point))))
650 (defun org-babel-where-is-src-block-result (&optional insert info hash)
651 "Return the point at the beginning of the result of the current
652 source block. Specifically at the beginning of the results line.
653 If no result exists for this block then create a results line
654 following the source block."
655 (save-excursion
656 (let* ((on-lob-line (progn (beginning-of-line 1)
657 (looking-at org-babel-lob-one-liner-regexp)))
658 (name (if on-lob-line (first (org-babel-lob-get-info))
659 (fifth (or info (org-babel-get-src-block-info)))))
660 (head (unless on-lob-line (org-babel-where-is-src-block-head))) end)
661 (when head (goto-char head))
662 (or (and name (org-babel-find-named-result name))
663 (and (or on-lob-line (re-search-forward "^[ \t]*#\\+end_src" nil t))
664 (progn (move-end-of-line 1)
665 (if (eobp) (insert "\n") (forward-char 1))
666 (setq end (point))
667 (or (and (not name)
668 (progn ;; unnamed results line already exists
669 (re-search-forward "[^ \f\t\n\r\v]" nil t)
670 (move-beginning-of-line 1)
671 (looking-at (concat org-babel-result-regexp "\n"))))
672 ;; or (with optional insert) back up and make one ourselves
673 (when insert
674 (goto-char end)
675 (if (looking-at "[\n\r]") (forward-char 1) (insert "\n"))
676 (insert (concat "#+results" (if hash (concat "["hash"]"))
677 ":"(if name (concat " " name)) "\n"))
678 (move-beginning-of-line 0)
679 (if hash (org-babel-hide-hash)) t)))
680 (point))))))
682 (defun org-babel-read-result ()
683 "Read the result at `point' into emacs-lisp."
684 (let ((case-fold-search t) result-string)
685 (cond
686 ((org-at-table-p) (org-babel-read-table))
687 ((looking-at org-bracket-link-regexp) (org-babel-read-link))
688 ((looking-at org-block-regexp) (org-babel-trim (match-string 4)))
689 ((looking-at ": ")
690 (setq result-string
691 (org-babel-trim
692 (mapconcat (lambda (line) (if (and (> (length line) 1)
693 (string= ": " (substring line 0 2)))
694 (substring line 2)
695 line))
696 (split-string
697 (buffer-substring (point) (org-babel-result-end)) "[\r\n]+")
698 "\n")))
699 (or (org-babel-number-p result-string) result-string))
700 ((looking-at org-babel-result-regexp)
701 (save-excursion (forward-line 1) (org-babel-read-result))))))
703 (defun org-babel-read-table ()
704 "Read the table at `point' into emacs-lisp."
705 (mapcar (lambda (row)
706 (if (and (symbolp row) (equal row 'hline)) row
707 (mapcar #'org-babel-read row)))
708 (org-table-to-lisp)))
710 (defun org-babel-read-link ()
711 "Read the link at `point' into emacs-lisp. If the path of the
712 link is a file path it is expanded using `expand-file-name'."
713 (let* ((case-fold-search t)
714 (raw (and (looking-at org-bracket-link-regexp)
715 (org-babel-clean-text-properties (match-string 1))))
716 (type (and (string-match org-link-types-re raw)
717 (match-string 1 raw))))
718 (cond
719 ((not type) (expand-file-name raw))
720 ((string= type "file")
721 (and (string-match "file\\(.*\\):\\(.+\\)" raw)
722 (expand-file-name (match-string 2 raw))))
723 (t raw))))
725 (defun org-babel-insert-result (result &optional result-params info hash)
726 "Insert RESULT into the current buffer after the end of the
727 current source block. With optional argument RESULT-PARAMS
728 controls insertion of results in the org-mode file.
729 RESULT-PARAMS can take the following values...
731 replace - (default option) insert results after the source block
732 replacing any previously inserted results
734 silent -- no results are inserted
736 file ---- the results are interpreted as a file path, and are
737 inserted into the buffer using the Org-mode file syntax
739 raw ----- results are added directly to the org-mode file. This
740 is a good option if you code block will output org-mode
741 formatted text.
743 org ----- this is the same as the 'raw' option
745 html ---- results are added inside of a #+BEGIN_HTML block. This
746 is a good option if you code block will output html
747 formatted text.
749 latex --- results are added inside of a #+BEGIN_LATEX block.
750 This is a good option if you code block will output
751 latex formatted text.
753 code ---- the results are extracted in the syntax of the source
754 code of the language being evaluated and are added
755 inside of a #+BEGIN_SRC block with the source-code
756 language set appropriately."
757 (if (stringp result)
758 (progn
759 (setq result (org-babel-clean-text-properties result))
760 (when (member "file" result-params)
761 (setq result (org-babel-result-to-file result))))
762 (unless (listp result) (setq result (format "%S" result))))
763 (if (and result-params (member "replace" result-params)
764 (not (member "silent" result-params)))
765 (org-babel-remove-result info))
766 (if (= (length result) 0)
767 (if (member "value" result-params)
768 (message "No result returned by source block")
769 (message "Source block produced no output"))
770 (if (and result-params (member "silent" result-params))
771 (progn (message (replace-regexp-in-string "%" "%%" (format "%S" result)))
772 result)
773 (when (and (stringp result) ;; ensure results end in a newline
774 (not (or (string-equal (substring result -1) "\n")
775 (string-equal (substring result -1) "\r"))))
776 (setq result (concat result "\n")))
777 (save-excursion
778 (let ((existing-result (org-babel-where-is-src-block-result t info hash))
779 (results-switches (cdr (assoc :results_switches (third info)))) beg)
780 (when existing-result (goto-char existing-result) (forward-line 1))
781 (setq results-switches
782 (if results-switches (concat " " results-switches) ""))
783 (cond
784 ;; assume the result is a table if it's not a string
785 ((not (stringp result))
786 (setq beg (point))
787 (insert (concat (orgtbl-to-orgtbl
788 (if (or (eq 'hline (car result))
789 (and (listp (car result))
790 (listp (cdr (car result)))))
791 result (list result))
792 '(:fmt (lambda (cell) (format "%s" cell)))) "\n"))
793 (goto-char beg) (org-cycle))
794 ((member "file" result-params)
795 (insert result))
796 ((member "html" result-params)
797 (insert (format "#+BEGIN_HTML%s\n%s#+END_HTML\n" results-switches result)))
798 ((member "latex" result-params)
799 (insert (format "#+BEGIN_LaTeX%s\n%s#+END_LaTeX\n" results-switches result)))
800 ((member "code" result-params)
801 (insert (format "#+BEGIN_SRC %s%s\n%s#+END_SRC\n" lang results-switches result)))
802 ((or (member "raw" result-params) (member "org" result-params))
803 (save-excursion (insert result)) (if (org-at-table-p) (org-cycle)))
805 (org-babel-examplize-region
806 (point) (progn (insert result) (point)) results-switches)))))
807 (message "finished"))))
809 (defun org-babel-result-to-org-string (result)
810 "Return RESULT as a string in org-mode format. This function
811 relies on `org-babel-insert-result'."
812 (with-temp-buffer (org-babel-insert-result result) (buffer-string)))
814 (defun org-babel-remove-result (&optional info)
815 "Remove the result of the current source block."
816 (interactive)
817 (let ((location (org-babel-where-is-src-block-result nil info)) start)
818 (when location
819 (save-excursion
820 (goto-char location) (setq start (point)) (forward-line 1)
821 (delete-region start (org-babel-result-end))))))
823 (defun org-babel-result-end ()
824 "Return the point at the end of the current set of results"
825 (save-excursion
826 (if (org-at-table-p)
827 (progn (goto-char (org-table-end)) (point))
828 (let ((case-fold-search t))
829 (cond
830 ((looking-at "#\\+begin_latex")
831 (search-forward "#+end_latex" nil t)
832 (forward-line 1))
833 ((looking-at "#\\+begin_html")
834 (search-forward "#+end_html" nil t)
835 (forward-line 1))
836 ((looking-at "#\\+begin_example")
837 (search-forward "#+end_example" nil t)
838 (forward-line 1))
839 ((looking-at "#\\+begin_src")
840 (search-forward "#+end_src" nil t)
841 (forward-line 1))
842 (t (progn (while (looking-at "\\(: \\|\\[\\[\\)")
843 (forward-line 1))))))
844 (point))))
846 (defun org-babel-result-to-file (result)
847 "Convert RESULT into an `org-mode' link. If the
848 `default-directory' is different from the containing file's
849 directory then expand relative links."
850 (format
851 "[[file:%s]]"
852 (if (and default-directory
853 buffer-file-name
854 (not (string= (expand-file-name default-directory)
855 (expand-file-name (file-name-directory buffer-file-name)))))
856 (expand-file-name result default-directory)
857 result)))
859 (defun org-babel-examplize-region (beg end &optional results-switches)
860 "Comment out region using the ': ' org example quote."
861 (interactive "*r")
862 (let ((size (abs (- (line-number-at-pos end)
863 (line-number-at-pos beg)))))
864 (save-excursion
865 (cond ((= size 0)
866 (error "This should be impossible: a newline was appended to result if missing"))
867 ((< size org-babel-min-lines-for-block-output)
868 (goto-char beg)
869 (dotimes (n size)
870 (move-beginning-of-line 1) (insert ": ") (forward-line 1)))
872 (goto-char beg)
873 (insert (if results-switches
874 (format "#+begin_example%s\n" results-switches)
875 "#+begin_example\n"))
876 (forward-char (- end beg))
877 (insert "#+end_example\n"))))))
879 (defun org-babel-merge-params (&rest plists)
880 "Combine all parameter association lists in PLISTS. Later
881 elements of PLISTS override the values of previous element. This
882 takes into account some special considerations for certain
883 parameters when merging lists."
884 (let ((results-exclusive-groups
885 '(("file" "vector" "table" "scalar" "raw" "org" "html" "latex" "code" "pp")
886 ("replace" "silent")
887 ("output" "value")))
888 (exports-exclusive-groups
889 '(("code" "results" "both" "none")))
890 params results exports tangle noweb cache vars var ref shebang comments)
891 (flet ((e-merge (exclusive-groups &rest result-params)
892 ;; maintain exclusivity of mutually exclusive parameters
893 (let (output)
894 (mapc (lambda (new-params)
895 (mapc (lambda (new-param)
896 (mapc (lambda (exclusive-group)
897 (when (member new-param exclusive-group)
898 (mapcar (lambda (excluded-param)
899 (setq output (delete excluded-param output)))
900 exclusive-group)))
901 exclusive-groups)
902 (setq output (org-uniquify (cons new-param output))))
903 new-params))
904 result-params)
905 output)))
906 (mapc (lambda (plist)
907 (mapc (lambda (pair)
908 (case (car pair)
909 (:var
910 ;; we want only one specification per variable
911 (when (string-match "^\\([^= \f\t\n\r\v]+\\)[ \t]*=[ \t]*\\([^\f\n\r\v]+\\)$" (cdr pair))
912 ;; TODO: When is this not true?
913 (setq var (intern (match-string 1 (cdr pair)))
914 ref (match-string 2 (cdr pair))
915 vars (cons (cons var ref) (assq-delete-all var vars)))))
916 (:results
917 (setq results
918 (e-merge results-exclusive-groups results (split-string (cdr pair)))))
919 (:file
920 (when (cdr pair)
921 (setq results (e-merge results-exclusive-groups results '("file")))
922 (unless (or (member "both" exports)
923 (member "none" exports)
924 (member "code" exports))
925 (setq exports (e-merge exports-exclusive-groups exports '("results"))))
926 (setq params (cons pair (assq-delete-all (car pair) params)))))
927 (:exports
928 (setq exports (e-merge exports-exclusive-groups
929 exports (split-string (cdr pair)))))
930 (:tangle ;; take the latest -- always overwrite
931 (setq tangle (or (list (cdr pair)) tangle)))
932 (:noweb
933 (setq noweb (e-merge '(("yes" "no"))
934 noweb (split-string (or (cdr pair) "")))))
935 (:cache
936 (setq cache (e-merge '(("yes" "no"))
937 cache (split-string (or (cdr pair) "")))))
938 (:shebang ;; take the latest -- always overwrite
939 (setq shebang (or (list (cdr pair)) shebang)))
940 (:comments
941 (setq comments (e-merge '(("yes" "no"))
942 comments (split-string (or (cdr pair) "")))))
943 (t ;; replace: this covers e.g. :session
944 (setq params (cons pair (assq-delete-all (car pair) params))))))
945 plist))
946 plists))
947 (setq vars (mapcar (lambda (pair) (format "%s=%s" (car pair) (cdr pair))) vars))
948 (while vars (setq params (cons (cons :var (pop vars)) params)))
949 (cons (cons :comments (mapconcat 'identity comments " "))
950 (cons (cons :shebang (mapconcat 'identity shebang " "))
951 (cons (cons :cache (mapconcat 'identity cache " "))
952 (cons (cons :noweb (mapconcat 'identity noweb " "))
953 (cons (cons :tangle (mapconcat 'identity tangle " "))
954 (cons (cons :exports (mapconcat 'identity exports " "))
955 (cons (cons :results (mapconcat 'identity results " "))
956 params)))))))))
958 (defun org-babel-expand-noweb-references (&optional info parent-buffer)
959 "This function expands Noweb style references in the body of
960 the current source-code block. For example the following
961 reference would be replaced with the body of the source-code
962 block named 'example-block'.
964 <<example-block>>
966 Note that any text preceding the <<foo>> construct on a line will
967 be interposed between the lines of the replacement text. So for
968 example if <<foo>> is placed behind a comment, then the entire
969 replacement text will also be commented.
971 This function must be called from inside of the buffer containing
972 the source-code block which holds BODY.
974 In addition the following syntax can be used to insert the
975 results of evaluating the source-code block named 'example-block'.
977 <<example-block()>>
979 Any optional arguments can be passed to example-block by placing
980 the arguments inside the parenthesis following the convention
981 defined by `org-babel-lob'. For example
983 <<example-block(a=9)>>
985 would set the value of argument \"a\" equal to \"9\". Note that
986 these arguments are not evaluated in the current source-code
987 block but are passed literally to the \"example-block\"."
988 (let* ((parent-buffer (or parent-buffer (current-buffer)))
989 (info (or info (org-babel-get-src-block-info)))
990 (lang (first info))
991 (body (second info))
992 (new-body "") index source-name evaluate prefix)
993 (flet ((nb-add (text)
994 (setq new-body (concat new-body text))))
995 (with-temp-buffer
996 (insert body) (goto-char (point-min))
997 (setq index (point))
998 (while (and (re-search-forward "<<\\(.+?\\)>>" nil t))
999 (save-match-data (setf source-name (match-string 1)))
1000 (save-match-data (setq evaluate (string-match "\(.*\)" source-name)))
1001 (save-match-data
1002 (setq prefix (buffer-substring (match-beginning 0)
1003 (save-excursion
1004 (move-beginning-of-line 1) (point)))))
1005 ;; add interval to new-body (removing noweb reference)
1006 (goto-char (match-beginning 0))
1007 (nb-add (buffer-substring index (point)))
1008 (goto-char (match-end 0))
1009 (setq index (point))
1010 (nb-add (save-excursion
1011 (set-buffer parent-buffer)
1012 (mapconcat ;; interpose `prefix' between every line
1013 #'identity
1014 (split-string
1015 (if evaluate
1016 (let ((raw (org-babel-ref-resolve-reference
1017 source-name nil)))
1018 (if (stringp raw) raw (format "%S" raw)))
1019 (let ((point (org-babel-find-named-block source-name)))
1020 (if point
1021 (save-excursion
1022 (goto-char point)
1023 (org-babel-trim (org-babel-expand-noweb-references
1024 (org-babel-get-src-block-info))))
1025 ;; optionally raise an error if named
1026 ;; source-block doesn't exist
1027 (if (member lang org-babel-noweb-error-langs)
1028 (error
1029 "<<%s>> could not be resolved (see `org-babel-noweb-error-langs')"
1030 source-name)
1031 "")))) "[\n\r]") (concat "\n" prefix)))))
1032 (nb-add (buffer-substring index (point-max)))))
1033 new-body))
1035 (defun org-babel-error-notify (exit-code stderr)
1036 (message (format "Shell command exited with code %d" exit-code))
1037 (let ((buf (get-buffer-create "*Org-Babel Error Output*")))
1038 (with-current-buffer buf
1039 (goto-char (point-max))
1040 (save-excursion (insert stderr)))
1041 (display-buffer buf)))
1043 (defun org-babel-clean-text-properties (text)
1044 "Strip all properties from text return."
1045 (set-text-properties 0 (length text) nil text) text)
1047 (defun org-babel-strip-protective-commas (body)
1048 "Strip protective commas from bodies of source blocks."
1049 (replace-regexp-in-string "^,#" "#" body))
1051 (defun org-babel-read (cell)
1052 "Convert the string value of CELL to a number if appropriate.
1053 Otherwise if cell looks like lisp (meaning it starts with a
1054 \"(\" or a \"'\") then read it as lisp, otherwise return it
1055 unmodified as a string.
1057 This is taken almost directly from `org-read-prop'."
1058 (if (and (stringp cell) (not (equal cell "")))
1059 (or (org-babel-number-p cell)
1060 (if (or (equal "(" (substring cell 0 1))
1061 (equal "'" (substring cell 0 1))
1062 (equal "`" (substring cell 0 1)))
1063 (eval (read cell))
1064 (progn (set-text-properties 0 (length cell) nil cell) cell)))
1065 cell))
1067 (defun org-babel-number-p (string)
1068 "Return t if STRING represents a number"
1069 (if (and (string-match "^-?[[:digit:]]*\\.?[[:digit:]]*$" string)
1070 (= (match-end 0) (length string)))
1071 (string-to-number string)))
1073 (defun org-babel-import-elisp-from-file (file-name)
1074 "Read the results located at FILE-NAME into an elisp table. If
1075 the table is trivial, then return it as a scalar."
1076 (let (result)
1077 (save-window-excursion
1078 (with-temp-buffer
1079 (condition-case nil
1080 (progn
1081 (org-table-import file-name nil)
1082 (delete-file file-name)
1083 (setq result (mapcar (lambda (row)
1084 (mapcar #'org-babel-string-read row))
1085 (org-table-to-lisp))))
1086 (error nil)))
1087 (if (null (cdr result)) ;; if result is trivial vector, then scalarize it
1088 (if (consp (car result))
1089 (if (null (cdr (car result)))
1090 (caar result)
1091 result)
1092 (car result))
1093 result))))
1095 (defun org-babel-string-read (cell)
1096 "Strip nested \"s from around strings."
1097 (org-babel-read (or (and (stringp cell)
1098 (string-match "\\\"\\(.+\\)\\\"" cell)
1099 (match-string 1 cell))
1100 cell)))
1102 (defun org-babel-reverse-string (string)
1103 (apply 'string (reverse (string-to-list string))))
1105 (defun org-babel-chomp (string &optional regexp)
1106 "Remove any trailing space or carriage returns characters from
1107 STRING. Default regexp used is \"[ \f\t\n\r\v]\" but can be
1108 overwritten by specifying a regexp as a second argument."
1109 (let ((regexp (or regexp "[ \f\t\n\r\v]")))
1110 (while (and (> (length string) 0) (string-match regexp (substring string -1)))
1111 (setq string (substring string 0 -1)))
1112 string))
1114 (defun org-babel-trim (string &optional regexp)
1115 "Like `org-babel-chomp' only it runs on both the front and back of the string"
1116 (org-babel-chomp (org-babel-reverse-string
1117 (org-babel-chomp (org-babel-reverse-string string) regexp)) regexp))
1119 (defun org-babel-tramp-handle-call-process-region
1120 (start end program &optional delete buffer display &rest args)
1121 "Use tramp to handle call-process-region.
1122 Fixes a bug in `tramp-handle-call-process-region'."
1123 (if (and (featurep 'tramp) (file-remote-p default-directory))
1124 (let ((tmpfile (tramp-compat-make-temp-file "")))
1125 (write-region start end tmpfile)
1126 (when delete (delete-region start end))
1127 (unwind-protect
1128 ;; (apply 'call-process program tmpfile buffer display args) ;; bug in tramp
1129 (apply 'process-file program tmpfile buffer display args)
1130 (delete-file tmpfile)))
1131 ;; call-process-region-original is the original emacs definition. It
1132 ;; is in scope from the let binding in org-babel-execute-src-block
1133 (apply call-process-region-original start end program delete buffer display args)))
1135 (defun org-babel-maybe-remote-file (file)
1136 (if (file-remote-p default-directory)
1137 (let* ((vec (tramp-dissect-file-name default-directory))
1138 (user (tramp-file-name-user vec))
1139 (host (tramp-file-name-host vec)))
1140 (concat "/" user (when user "@") host ":" file))
1141 file))
1143 (defun org-babel-shell-command-on-region (start end command
1144 &optional output-buffer replace
1145 error-buffer display-error-buffer)
1146 "Execute string COMMAND in inferior shell with region as input.
1148 Fixes bugs in the emacs 23.1.1 version of `shell-command-on-region'
1150 Normally display output (if any) in temp buffer `*Shell Command Output*';
1151 Prefix arg means replace the region with it. Return the exit code of
1152 COMMAND.
1154 To specify a coding system for converting non-ASCII characters
1155 in the input and output to the shell command, use \\[universal-coding-system-argument]
1156 before this command. By default, the input (from the current buffer)
1157 is encoded in the same coding system that will be used to save the file,
1158 `buffer-file-coding-system'. If the output is going to replace the region,
1159 then it is decoded from that same coding system.
1161 The noninteractive arguments are START, END, COMMAND,
1162 OUTPUT-BUFFER, REPLACE, ERROR-BUFFER, and DISPLAY-ERROR-BUFFER.
1163 Noninteractive callers can specify coding systems by binding
1164 `coding-system-for-read' and `coding-system-for-write'.
1166 If the command generates output, the output may be displayed
1167 in the echo area or in a buffer.
1168 If the output is short enough to display in the echo area
1169 \(determined by the variable `max-mini-window-height' if
1170 `resize-mini-windows' is non-nil), it is shown there. Otherwise
1171 it is displayed in the buffer `*Shell Command Output*'. The output
1172 is available in that buffer in both cases.
1174 If there is output and an error, a message about the error
1175 appears at the end of the output.
1177 If there is no output, or if output is inserted in the current buffer,
1178 then `*Shell Command Output*' is deleted.
1180 If the optional fourth argument OUTPUT-BUFFER is non-nil,
1181 that says to put the output in some other buffer.
1182 If OUTPUT-BUFFER is a buffer or buffer name, put the output there.
1183 If OUTPUT-BUFFER is not a buffer and not nil,
1184 insert output in the current buffer.
1185 In either case, the output is inserted after point (leaving mark after it).
1187 If REPLACE, the optional fifth argument, is non-nil, that means insert
1188 the output in place of text from START to END, putting point and mark
1189 around it.
1191 If optional sixth argument ERROR-BUFFER is non-nil, it is a buffer
1192 or buffer name to which to direct the command's standard error output.
1193 If it is nil, error output is mingled with regular output.
1194 If DISPLAY-ERROR-BUFFER is non-nil, display the error buffer if there
1195 were any errors. (This is always t, interactively.)
1196 In an interactive call, the variable `shell-command-default-error-buffer'
1197 specifies the value of ERROR-BUFFER."
1198 (interactive (let (string)
1199 (unless (mark)
1200 (error "The mark is not set now, so there is no region"))
1201 ;; Do this before calling region-beginning
1202 ;; and region-end, in case subprocess output
1203 ;; relocates them while we are in the minibuffer.
1204 (setq string (read-shell-command "Shell command on region: "))
1205 ;; call-interactively recognizes region-beginning and
1206 ;; region-end specially, leaving them in the history.
1207 (list (region-beginning) (region-end)
1208 string
1209 current-prefix-arg
1210 current-prefix-arg
1211 shell-command-default-error-buffer
1212 t)))
1213 (let ((error-file
1214 (if error-buffer
1215 (make-temp-file
1216 (expand-file-name "scor"
1217 (or small-temporary-file-directory
1218 temporary-file-directory)))
1219 nil))
1220 exit-status)
1221 (if (or replace
1222 (and output-buffer
1223 (not (or (bufferp output-buffer) (stringp output-buffer)))))
1224 ;; Replace specified region with output from command.
1225 (let ((swap (and replace (< start end))))
1226 ;; Don't muck with mark unless REPLACE says we should.
1227 (goto-char start)
1228 (and replace (push-mark (point) 'nomsg))
1229 (setq exit-status
1230 (call-process-region start end shell-file-name t
1231 (if error-file
1232 (list output-buffer error-file)
1234 nil shell-command-switch command))
1235 ;; It is rude to delete a buffer which the command is not using.
1236 ;; (let ((shell-buffer (get-buffer "*Shell Command Output*")))
1237 ;; (and shell-buffer (not (eq shell-buffer (current-buffer)))
1238 ;; (kill-buffer shell-buffer)))
1239 ;; Don't muck with mark unless REPLACE says we should.
1240 (and replace swap (exchange-point-and-mark)))
1241 ;; No prefix argument: put the output in a temp buffer,
1242 ;; replacing its entire contents.
1243 (let ((buffer (get-buffer-create
1244 (or output-buffer "*Shell Command Output*"))))
1245 (unwind-protect
1246 (if (eq buffer (current-buffer))
1247 ;; If the input is the same buffer as the output,
1248 ;; delete everything but the specified region,
1249 ;; then replace that region with the output.
1250 (progn (setq buffer-read-only nil)
1251 (delete-region (max start end) (point-max))
1252 (delete-region (point-min) (min start end))
1253 (setq exit-status
1254 (call-process-region (point-min) (point-max)
1255 shell-file-name t
1256 (if error-file
1257 (list t error-file)
1259 nil shell-command-switch
1260 command)))
1261 ;; Clear the output buffer, then run the command with
1262 ;; output there.
1263 (let ((directory default-directory))
1264 (save-excursion
1265 (set-buffer buffer)
1266 (setq buffer-read-only nil)
1267 (if (not output-buffer)
1268 (setq default-directory directory))
1269 (erase-buffer)))
1270 (setq exit-status
1271 (call-process-region start end shell-file-name nil
1272 (if error-file
1273 (list buffer error-file)
1274 buffer)
1275 nil shell-command-switch command)))
1276 ;; Report the output.
1277 (with-current-buffer buffer
1278 (setq mode-line-process
1279 (cond ((null exit-status)
1280 " - Error")
1281 ((stringp exit-status)
1282 (format " - Signal [%s]" exit-status))
1283 ((not (equal 0 exit-status))
1284 (format " - Exit [%d]" exit-status)))))
1285 (if (with-current-buffer buffer (> (point-max) (point-min)))
1286 ;; There's some output, display it
1287 (display-message-or-buffer buffer)
1288 ;; No output; error?
1289 (let ((output
1290 (if (and error-file
1291 (< 0 (nth 7 (file-attributes error-file))))
1292 "some error output"
1293 "no output")))
1294 (cond ((null exit-status)
1295 (message "(Shell command failed with error)"))
1296 ((equal 0 exit-status)
1297 (message "(Shell command succeeded with %s)"
1298 output))
1299 ((stringp exit-status)
1300 (message "(Shell command killed by signal %s)"
1301 exit-status))
1303 (message "(Shell command failed with code %d and %s)"
1304 exit-status output))))
1305 ;; Don't kill: there might be useful info in the undo-log.
1306 ;; (kill-buffer buffer)
1307 ))))
1309 (when (and error-file (file-exists-p error-file))
1310 (if (< 0 (nth 7 (file-attributes error-file)))
1311 (with-current-buffer (get-buffer-create error-buffer)
1312 (let ((pos-from-end (- (point-max) (point))))
1313 (or (bobp)
1314 (insert "\f\n"))
1315 ;; Do no formatting while reading error file,
1316 ;; because that can run a shell command, and we
1317 ;; don't want that to cause an infinite recursion.
1318 (format-insert-file error-file nil)
1319 ;; Put point after the inserted errors.
1320 (goto-char (- (point-max) pos-from-end)))
1321 (and display-error-buffer
1322 (display-buffer (current-buffer)))))
1323 (delete-file error-file))
1324 exit-status))
1327 (provide 'org-babel)
1328 ;;; org-babel.el ends here