Commit 5e84dcef authored by Tino Calancha's avatar Tino Calancha

call-shell-region: New defun

Suggested by Stefan Monnier in Bug#22679.
* lisp/subr.el (call-shell-region): New defun; execute a command
in an inferior shell with the buffer region as input.
* lisp/simple.el (shell-command-on-region): Use it.
* lisp/gnus/message.el (message-do-fcc): Idem.
* doc/lispref/processes.texi: Document call-shell-region in the manual.
;* etc/NEWS: Add entry for this new function.
parent 95c82efd
......@@ -492,20 +492,17 @@ inputinput@point{}
@end smallexample
For example, the @code{shell-command-on-region} command uses
@code{call-process-region} in a manner similar to this:
@code{call-shell-region} in a manner similar to this:
@smallexample
@group
(call-process-region
(call-shell-region
start end
shell-file-name ; @r{name of program}
command ; @r{shell command}
nil ; @r{do not delete region}
buffer ; @r{send output to @code{buffer}}
nil ; @r{no redisplay during output}
"-c" command) ; @r{arguments for the shell}
buffer) ; @r{send output to @code{buffer}}
@end group
@end smallexample
@c It actually uses shell-command-switch, but no need to mention that here.
@end defun
@defun call-process-shell-command command &optional infile destination display
......@@ -525,6 +522,15 @@ convention allowed passing any number of additional arguments after
supported, but strongly discouraged.
@end defun
@defun call-shell-region start end command &optional delete destination
This function sends the text from @var{start} to @var{end} as
standard input to an inferior shell running @var{command}. This function
is similar than @code{call-process-region}, with process being a shell.
The arguments @code{delete}, @code{destination} and the return value
are like in @code{call-process-region}.
Note that this funtion doesn't accept additional arguments.
@end defun
@defun shell-command-to-string command
This function executes @var{command} (a string) as a shell command,
then returns the command's output as a string.
......
......@@ -55,6 +55,10 @@ affected by this, as SGI stopped supporting IRIX in December 2013.
* Changes in Emacs 25.2
+++
** The new funtion 'call-shell-region' executes a command in an
inferior shell with the buffer region as input.
+++
** The new user option 'shell-command-not-erase-buffer' controls
if the output buffer is erased between shell commands; if non-nil,
......
......@@ -5409,9 +5409,7 @@ Otherwise, generate and save a value for `canlock-password' first."
(setq file (pop list))
(if (string-match "^[ \t]*|[ \t]*\\(.*\\)[ \t]*$" file)
;; Pipe the article to the program in question.
(call-process-region (point-min) (point-max) shell-file-name
nil nil nil shell-command-switch
(match-string 1 file))
(call-shell-region (point-min) (point-max) (match-string 1 file))
;; Save the article.
(setq file (expand-file-name file))
(unless (file-exists-p (file-name-directory file))
......
......@@ -3667,11 +3667,10 @@ interactively, this is t."
(goto-char start)
(and replace (push-mark (point) 'nomsg))
(setq exit-status
(call-process-region start end shell-file-name replace
(call-shell-region start end command replace
(if error-file
(list t error-file)
t)
nil shell-command-switch command))
t)))
;; It is rude to delete a buffer which the command is not using.
;; (let ((shell-buffer (get-buffer "*Shell Command Output*")))
;; (and shell-buffer (not (eq shell-buffer (current-buffer)))
......@@ -3709,11 +3708,10 @@ interactively, this is t."
(setq default-directory directory))
(shell-command--save-pos-or-erase)))
(setq exit-status
(call-process-region start end shell-file-name nil
(call-shell-region start end command nil
(if error-file
(list buffer error-file)
buffer)
nil shell-command-switch command)))
buffer))))
;; Report the output.
(with-current-buffer buffer
(setq mode-line-process
......
......@@ -3078,6 +3078,28 @@ Similar to `call-process-shell-command', but calls `process-file'."
infile buffer display
(if (file-remote-p default-directory) "-c" shell-command-switch)
(mapconcat 'identity (cons command args) " ")))
(defun call-shell-region (start end command &optional delete buffer)
"Send text from START to END as input to an inferior shell running COMMAND.
Delete the text if fourth arg DELETE is non-nil.
Insert output in BUFFER before point; t means current buffer; nil for
BUFFER means discard it; 0 means discard and don't wait; and `(:file
FILE)', where FILE is a file name string, means that it should be
written to that file (if the file already exists it is overwritten).
BUFFER can also have the form (REAL-BUFFER STDERR-FILE); in that case,
REAL-BUFFER says what to do with standard output, as above,
while STDERR-FILE says what to do with standard error in the child.
STDERR-FILE may be nil (discard standard error output),
t (mix it with ordinary output), or a file name string.
If BUFFER is 0, `call-shell-region' returns immediately with value nil.
Otherwise it waits for COMMAND to terminate
and returns a numeric exit status or a signal description string.
If you quit, the process is killed with SIGINT, or SIGKILL if you quit again."
(call-process-region start end
shell-file-name delete buffer nil
shell-command-switch command))
;;;; Lisp macros to do various things temporarily.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment