Function: org-babel--shell-command-on-region

org-babel--shell-command-on-region is a byte-compiled function defined in ob-eval.el.gz.

Signature

(org-babel--shell-command-on-region COMMAND ERROR-BUFFER)

Documentation

Execute COMMAND in an inferior shell with region as input.

Stripped down version of shell-command-on-region for internal use in Babel only. This lets us work around errors in the original function in various versions of Emacs. This expects the query to be run to be in the current temp buffer. This is written into input-file. ERROR-BUFFER is the name of the file which org-babel-eval has created to use for any error messages that are returned.

Source Code

;; Defined in /usr/src/emacs/lisp/org/ob-eval.el.gz
(defun org-babel--shell-command-on-region (command error-buffer)
  "Execute COMMAND in an inferior shell with region as input.
Stripped down version of `shell-command-on-region' for internal use in
Babel only.  This lets us work around errors in the original function
in various versions of Emacs.  This expects the query to be run to be
in the current temp buffer.  This is written into
input-file.  ERROR-BUFFER is the name of the file which
`org-babel-eval' has created to use for any error messages that are
returned."

  (let ((input-file (org-babel-temp-file "ob-input-"))
	(error-file (if error-buffer (org-babel-temp-file "ob-error-") nil))
	(shell-file-name (org-babel--get-shell-file-name))
	exit-status)
    ;; we always call this with 'replace, remove conditional
    ;; Replace specified region with output from command.
    (org-babel--write-temp-buffer-input-file input-file)
    (setq exit-status
	  (process-file shell-file-name input-file
			(if error-file
			    (list t error-file)
			  t)
			nil shell-command-switch command))

    (when (and input-file (file-exists-p input-file)
	       ;; bind org-babel--debug-input around the call to keep
	       ;; the temporary input files available for inspection
	       (not (when (boundp 'org-babel--debug-input)
		      org-babel--debug-input)))
      (delete-file input-file))

    (when (and error-file (file-exists-p error-file))
      (when (< 0 (file-attribute-size (file-attributes error-file)))
	(with-current-buffer (get-buffer-create error-buffer)
	  (let ((pos-from-end (- (point-max) (point))))
	    (or (bobp)
		(insert "\f\n"))
	    ;; Do no formatting while reading error file,
	    ;; because that can run a shell command, and we
	    ;; don't want that to cause an infinite recursion.
	    (format-insert-file error-file nil)
	    ;; Put point after the inserted errors.
	    (goto-char (- (point-max) pos-from-end)))
	  (current-buffer)))
      (delete-file error-file))
    exit-status))