bug-gnu-emacs
[Top][All Lists]
Advanced

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

bug#22679: 25.0.91; ibuffer-do-shell-command-pipe truncate output


From: Tino Calancha
Subject: bug#22679: 25.0.91; ibuffer-do-shell-command-pipe truncate output
Date: Fri, 03 Feb 2017 13:25:25 +0900
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/26.0.50 (gnu/linux)

Tino Calancha <address@hidden> writes:

>> Doesn't look simple enough, tho: you dropped the shell-quote-argument.
> OK, i will use it.
> The following patch is divided in two parts.
> 1) First one fix this bug: avoid truncation of the output, i.e., the
>    output from all processed buffers is kept.
>
> 2) Erase *Shell Command Output* before `ibuffer-do-shell-command-pipe',
>    `ibuffer-do-shell-command-pipe-replace' and
>    `ibuffer-do-shell-command-file'
>    if shell-command-dont-erase-buffer is nil.
I have updated 2): BEFORE form must be executed once, right before the
operation;  previous patch run it inside the loop.

Following is the updated patch.  I'd like to push it into master
branch in a few days if there is no objections.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
>From f01155c426e83fd69ec0b9ecdd697bc973b78197 Mon Sep 17 00:00:00 2001
From: Tino Calancha <address@hidden>
Date: Fri, 3 Feb 2017 12:37:25 +0900
Subject: [PATCH 1/2] Ibuffer: Don't truncate shell command output

* lisp/ibuf-ext.el (ibuffer-do-shell-command-pipe)
(ibuffer-do-shell-command-pipe-replace)
Use 'call-shell-region' (Bug#22679).
(ibuffer-do-shell-command-file): Use call-process-shell-command.
If FILE, the file that the buffer object is visiting,
exists and the buffer is up-to-date, then use
FILE instead of creating a temporary file (Bug#22679).
---
 lisp/ibuf-ext.el | 38 +++++++++++++++++++++++---------------
 1 file changed, 23 insertions(+), 15 deletions(-)

diff --git a/lisp/ibuf-ext.el b/lisp/ibuf-ext.el
index 058eaecb36..00cbf051d2 100644
--- a/lisp/ibuf-ext.el
+++ b/lisp/ibuf-ext.el
@@ -512,8 +512,10 @@ shell-command-pipe
   (:interactive "sPipe to shell command: "
    :opstring "Shell command executed on"
    :modifier-p nil)
-  (shell-command-on-region
-   (point-min) (point-max) command))
+  (let ((out-buf (get-buffer-create "*Shell Command Output*")))
+    (with-current-buffer out-buf (goto-char (point-max)))
+    (call-shell-region (point-min) (point-max)
+                       command nil out-buf)))
 
 ;;;###autoload (autoload 'ibuffer-do-shell-command-pipe-replace "ibuf-ext")
 (define-ibuffer-op shell-command-pipe-replace (command)
@@ -523,9 +525,8 @@ shell-command-pipe-replace
    :active-opstring "replace buffer contents in"
    :dangerous t
    :modifier-p t)
-  (with-current-buffer buf
-    (shell-command-on-region (point-min) (point-max)
-                            command nil t)))
+  (call-shell-region (point-min) (point-max)
+                     command 'delete buf))
 
 ;;;###autoload (autoload 'ibuffer-do-shell-command-file "ibuf-ext")
 (define-ibuffer-op shell-command-file (command)
@@ -533,16 +534,23 @@ shell-command-file
   (:interactive "sShell command on buffer's file: "
    :opstring "Shell command executed on"
    :modifier-p nil)
-  (shell-command (concat command " "
-                        (shell-quote-argument
-                         (or buffer-file-name
-                             (let ((file
-                                    (make-temp-file
-                                     (substring
-                                      (buffer-name) 0
-                                      (min 10 (length (buffer-name)))))))
-                               (write-region nil nil file nil 0)
-                               file))))))
+  (let ((file (and (not (buffer-modified-p))
+                   buffer-file-name))
+        (out-buf (get-buffer-create "*Shell Command Output*")))
+    (unless (and file (file-exists-p file))
+      (setq file
+            (make-temp-file
+             (substring
+              (buffer-name) 0
+              (min 10 (length (buffer-name))))))
+      (write-region nil nil file nil 0))
+    (with-current-buffer out-buf (goto-char (point-max)))
+    (call-process-shell-command
+     (format "%s %s"
+             command
+             (shell-quote-argument file))
+     nil out-buf nil)))
+
 
 ;;;###autoload (autoload 'ibuffer-do-eval "ibuf-ext")
 (define-ibuffer-op eval (form)
-- 
2.11.0

>From 196c1dc5984c3b0f6842201ef86bc34b71d4a440 Mon Sep 17 00:00:00 2001
From: Tino Calancha <address@hidden>
Date: Fri, 3 Feb 2017 13:10:08 +0900
Subject: [PATCH 2/2] Ibuffer: Erase output buffer before shell commands

* lisp/ibuf-macs.el (define-ibuffer-op): Add keyword arguments
BEFORE and AFTER; they are forms to run before/after the operation.
* lisp/ibuf-ext.el (ibuffer--maybe-erase-shell-cmd-output):
New defun; if shell-command-dont-erase-buffer is nil, then
erase shell command output buffer.
(ibuffer-do-shell-command-pipe, ibuffer-do-shell-command-file): Use it.
---
 lisp/ibuf-ext.el  | 10 +++++++++-
 lisp/ibuf-macs.el | 10 ++++++++--
 2 files changed, 17 insertions(+), 3 deletions(-)

diff --git a/lisp/ibuf-ext.el b/lisp/ibuf-ext.el
index 00cbf051d2..2a68f777d9 100644
--- a/lisp/ibuf-ext.el
+++ b/lisp/ibuf-ext.el
@@ -506,11 +506,19 @@ ibuffer-backward-filter-group
     (ibuffer-backward-filter-group 1))
   (ibuffer-forward-line 0))
 
+(defun ibuffer--maybe-erase-shell-cmd-output ()
+  (let ((buf (get-buffer "*Shell Command Output*")))
+    (when (and (buffer-live-p buf)
+               (not shell-command-dont-erase-buffer)
+               (not (zerop (buffer-size buf))))
+      (with-current-buffer buf (erase-buffer)))))
+
 ;;;###autoload (autoload 'ibuffer-do-shell-command-pipe "ibuf-ext")
 (define-ibuffer-op shell-command-pipe (command)
   "Pipe the contents of each marked buffer to shell command COMMAND."
   (:interactive "sPipe to shell command: "
    :opstring "Shell command executed on"
+   :before (ibuffer--maybe-erase-shell-cmd-output)
    :modifier-p nil)
   (let ((out-buf (get-buffer-create "*Shell Command Output*")))
     (with-current-buffer out-buf (goto-char (point-max)))
@@ -533,6 +541,7 @@ shell-command-file
   "Run shell command COMMAND separately on files of marked buffers."
   (:interactive "sShell command on buffer's file: "
    :opstring "Shell command executed on"
+   :before (ibuffer--maybe-erase-shell-cmd-output)
    :modifier-p nil)
   (let ((file (and (not (buffer-modified-p))
                    buffer-file-name))
@@ -551,7 +560,6 @@ shell-command-file
              (shell-quote-argument file))
      nil out-buf nil)))
 
-
 ;;;###autoload (autoload 'ibuffer-do-eval "ibuf-ext")
 (define-ibuffer-op eval (form)
   "Evaluate FORM in each of the buffers.
diff --git a/lisp/ibuf-macs.el b/lisp/ibuf-macs.el
index 05e568efeb..8457599899 100644
--- a/lisp/ibuf-macs.el
+++ b/lisp/ibuf-macs.el
@@ -169,6 +169,8 @@ ibuffer-save-marks
                                  dangerous
                                  (opstring "operated on")
                                  (active-opstring "Operate on")
+                                  before
+                                  after
                                  complex)
                                 &rest body)
   "Generate a function which operates on a buffer.
@@ -198,6 +200,8 @@ ibuffer-save-marks
 ACTIVE-OPSTRING is a string which will be displayed to the user in a
 confirmation message, in the form:
  \"Really ACTIVE-OPSTRING x buffers?\"
+BEFORE is a form to evaluate before start the operation.
+AFTER is a form to evaluate once the operation is complete.
 COMPLEX means this function is special; if COMPLEX is nil BODY
 evaluates once for each marked buffer, MBUF, with MBUF current
 and saving the point.  If COMPLEX is non-nil, BODY evaluates
@@ -206,7 +210,7 @@ ibuffer-save-marks
 marked buffer.  BODY is evaluated with `buf' bound to the
 buffer object.
 
-\(fn OP ARGS DOCUMENTATION (&key INTERACTIVE MARK MODIFIER-P DANGEROUS 
OPSTRING ACTIVE-OPSTRING COMPLEX) &rest BODY)"
+\(fn OP ARGS DOCUMENTATION (&key INTERACTIVE MARK MODIFIER-P DANGEROUS 
OPSTRING ACTIVE-OPSTRING BEFORE AFTER COMPLEX) &rest BODY)"
   (declare (indent 2) (doc-string 3))
   `(progn
      (defun ,(intern (concat (if (string-match "^ibuffer-do" (symbol-name op))
@@ -238,6 +242,7 @@ ibuffer-save-marks
                          (if (eq modifier-p t)
                              '((setq ibuffer-did-modification t))
                            ())
+                          (and after `(,after)) ; post-operation form.
                          `((ibuffer-redisplay t)
                            (message ,(concat "Operation finished; " opstring " 
%s buffers") count))))
                 (inner-body (if complex
@@ -247,7 +252,8 @@ ibuffer-save-marks
                                    (save-excursion
                                      ,@body))
                                  t)))
-                (body `(let ((count
+                (body `(let ((_before ,before) ; pre-operation form.
+                               (count
                               (,(pcase mark
                                   (:deletion
                                    'ibuffer-map-deletion-lines)
-- 
2.11.0

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
In GNU Emacs 26.0.50.1 (x86_64-pc-linux-gnu, GTK+ Version 3.22.6)
 of 2017-02-02
Repository revision: ce88155d83ba84e84321ed69a39c82f40117dd1f





reply via email to

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