emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

New optional arg to [async-]shell-command[-on-region]


From: Tino Calancha
Subject: New optional arg to [async-]shell-command[-on-region]
Date: Wed, 13 Jul 2016 23:09:17 +0900
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.2.0

I would like to propose a patch adding a new optional argument KEEP
for commands: `shell-command,' `async-shell-command' and
`shell-command-on-region'.

The new argument, when non-nil, prevent to erase the output buffer
before inserting the new output, i.e., allow the callers to
concatenate the output of several consecutive commands in the same buffer.

One example where this change is useful is Bug#22679; the fix becomes
trivial: just adjust the callers to pass a non-nil KEEP argument.


Following is the patch i propose:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

From c200d7b2eebcadc0c3108252b30fe6ffc6627b12 Mon Sep 17 00:00:00 2001
From: Tino Calancha <address@hidden>
Date: Wed, 13 Jul 2016 22:02:45 +0900
Subject: [PATCH] Allow not erasing output buffer on shell commands

* lisp/simple.el (async-shell-command)
(shell-command, shell-command-on-region): Added optional
arg KEEP.
---
 lisp/simple.el | 24 ++++++++++++++++--------
 1 file changed, 16 insertions(+), 8 deletions(-)

diff --git a/lisp/simple.el b/lisp/simple.el
index 6d7f00f..91bcb13 100644
--- a/lisp/simple.el
+++ b/lisp/simple.el
@@ -3175,7 +3175,7 @@ async-shell-command-buffer
   :group 'shell
   :version "24.3")

-(defun async-shell-command (command &optional output-buffer error-buffer)
+(defun async-shell-command (command &optional output-buffer error-buffer keep)
   "Execute string COMMAND asynchronously in background.

 Like `shell-command', but adds `&' at the end of COMMAND
@@ -3206,9 +3206,9 @@ async-shell-command
     shell-command-default-error-buffer))
   (unless (string-match "&[ \t]*\\'" command)
     (setq command (concat command " &")))
-  (shell-command command output-buffer error-buffer))
+  (shell-command command output-buffer error-buffer keep))

-(defun shell-command (command &optional output-buffer error-buffer)
+(defun shell-command (command &optional output-buffer error-buffer keep)
   "Execute string COMMAND in inferior shell; display output, if any.
 With prefix argument, insert the COMMAND's output at point.

@@ -3262,6 +3262,9 @@ shell-command
 In an interactive call, the variable `shell-command-default-error-buffer'
 specifies the value of ERROR-BUFFER.

+If the optional fourth argument KEEP is non-nil, the output buffer
+is not erased before inserting the output.
+
 In Elisp, you will often be better served by calling `call-process' or
 `start-process' directly, since it offers more control and does not impose
 the use of a shell (with its need to quote arguments)."
@@ -3379,7 +3382,7 @@ shell-command
           ;; if some text has a non-nil read-only property,
           ;; which comint sometimes adds for prompts.
           (let ((inhibit-read-only t))
-            (erase-buffer))
+            (or keep (erase-buffer)))
           (display-buffer buffer '(nil (allow-no-window . t)))
           (setq default-directory directory)
           (setq proc (start-process "Shell" buffer shell-file-name
@@ -3393,7 +3396,7 @@ shell-command
           ))
         ;; Otherwise, command is executed synchronously.
         (shell-command-on-region (point) (point) command
-                     output-buffer nil error-buffer)))))))
+                     output-buffer nil error-buffer nil nil keep)))))))

(defun display-message-or-buffer (message &optional buffer-name action frame) "Display MESSAGE in the echo area if possible, otherwise in a pop-up buffer.
@@ -3473,7 +3476,7 @@ shell-command-sentinel
 (defun shell-command-on-region (start end command
                       &optional output-buffer replace
                       error-buffer display-error-buffer
-                      region-noncontiguous-p)
+                      region-noncontiguous-p keep)
   "Execute string COMMAND in inferior shell with region as input.
 Normally display output (if any) in temp buffer `*Shell Command Output*';
 Prefix arg means replace the region with it.  Return the exit code of
@@ -3521,7 +3524,10 @@ shell-command-on-region

 Optional seventh arg DISPLAY-ERROR-BUFFER, if non-nil, means to
 display the error buffer if there were any errors.  When called
-interactively, this is t."
+interactively, this is t.
+
+Optional ninth arg KEEP, if non-nil, then the output buffer is
+not erased before inserting the output."
   (interactive (let (string)
          (unless (mark)
            (user-error "The mark is not set now, so there is no region"))
@@ -3606,7 +3612,9 @@ shell-command-on-region
                     (setq buffer-read-only nil)
                     (if (not output-buffer)
                         (setq default-directory directory))
-                    (erase-buffer)))
+                    (if keep
+                        (goto-char (point-max))
+                      (erase-buffer))))
                 (setq exit-status
                       (call-process-region start end shell-file-name nil
                                            (if error-file
--
2.8.1


In GNU Emacs 25.1.50.1 (x86_64-pc-linux-gnu, GTK+ Version 3.20.6)
 of 2016-07-13
Repository revision: a36ed9b5e95afea5716256bac24d883263aefbaf
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Please, let me know your opinion/concerns about this proposal.

Thank you,
Tino




reply via email to

[Prev in Thread] Current Thread [Next in Thread]