unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Tino Calancha <tino.calancha@gmail.com>
To: Stefan Monnier <monnier@iro.umontreal.ca>
Cc: 22679@debbugs.gnu.org, tino.calancha@gmail.com
Subject: bug#22679: 25.0.91; ibuffer-do-shell-command-pipe truncate output
Date: Fri, 27 Jan 2017 15:26:32 +0900	[thread overview]
Message-ID: <87o9ytuig7.fsf@calancha-pc> (raw)
In-Reply-To: <jwv4m6ahzjh.fsf-monnier+emacsbugs@gnu.org> (Stefan Monnier's message of "Wed, 24 Aug 2016 13:05:56 -0400")

Stefan Monnier <monnier@iro.umontreal.ca> writes:

>> -  (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*")))
>> +    (when (or (null file) (not (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 file)
>> +                                nil out-buf nil)))
>
> 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.

How do you think?

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
From d8b9c18d1693e30a07b2559aae89459a4fce0a4a Mon Sep 17 00:00:00 2001
From: Tino Calancha <tino.calancha@gmail.com>
Date: Fri, 27 Jan 2017 15:22:41 +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 905a263a86340ad739b9e8816d36f596ae00b65b Mon Sep 17 00:00:00 2001
From: Tino Calancha <tino.calancha@gmail.com>
Date: Fri, 27 Jan 2017 15:22:53 +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 BODY.
* lisp/ibuf-ext.el (ibuffer--maybe-erase-shell-cmd-output):
New defsubst; 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  | 9 ++++++++-
 lisp/ibuf-macs.el | 8 +++++++-
 2 files changed, 15 insertions(+), 2 deletions(-)

diff --git a/lisp/ibuf-ext.el b/lisp/ibuf-ext.el
index 00cbf051d2..04a3d84f58 100644
--- a/lisp/ibuf-ext.el
+++ b/lisp/ibuf-ext.el
@@ -506,11 +506,18 @@ ibuffer-backward-filter-group
     (ibuffer-backward-filter-group 1))
   (ibuffer-forward-line 0))
 
+(defsubst ibuffer--maybe-erase-shell-cmd-output (&optional buffer)
+  (let ((buf (or buffer (get-buffer "*Shell Command Output*"))))
+    (when (and (buffer-live-p buf)
+               (not shell-command-dont-erase-buffer))
+      (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 +540,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 +559,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..1ee157aac7 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))
@@ -233,11 +237,13 @@ ibuffer-save-marks
 				 'ibuffer-deletion-char)
 				(_
 				 'ibuffer-marked-char))))
+         ,before ; pre-operation form.
 	 ,(let* ((finish (append
 			  '(progn)
 			  (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
-- 
2.11.0

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
In GNU Emacs 26.0.50.1 (x86_64-pc-linux-gnu, GTK+ Version 3.22.6)
 of 2017-01-27
Repository revision: 7cb7a582f44db94292709d35f4f5474f891f03b0





  parent reply	other threads:[~2017-01-27  6:26 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-02-15 13:21 bug#22679: 25.0.91; ibuffer-do-shell-command-pipe truncate output Tino Calancha
2016-06-10  5:02 ` Glenn Morris
     [not found]   ` <CAMn5WmYDL0CrDppLc_Vs+EM5CkA_wfdb1z+QCmNj_=v6PiYM-g@mail.gmail.com>
2016-06-10  9:08     ` Tino Calancha
2016-07-05 15:58       ` Glenn Morris
2016-07-05 16:27         ` Tino Calancha
2016-07-09 17:28           ` Glenn Morris
2016-07-13 15:27             ` Stefan Monnier
2016-08-19  8:33               ` Tino Calancha
2016-08-19 13:52                 ` Stefan Monnier
2016-08-20  3:28                   ` Tino Calancha
2016-08-20 10:28                   ` Tino Calancha
2016-08-20 12:46                     ` Stefan Monnier
2016-08-21 14:37                       ` Tino Calancha
2016-08-22 16:06                         ` Stefan Monnier
2016-08-23 15:08                           ` Tino Calancha
2016-08-24 17:05                             ` Stefan Monnier
2016-08-25  9:39                               ` Tino Calancha
2016-08-25 12:36                                 ` Stefan Monnier
2016-08-25 13:26                                   ` Tino Calancha
2017-01-27  6:26                               ` Tino Calancha [this message]
2017-02-03  4:25                                 ` Tino Calancha
2017-02-09  9:24                                   ` Tino Calancha
2016-06-11  3:48   ` C. Calancha

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87o9ytuig7.fsf@calancha-pc \
    --to=tino.calancha@gmail.com \
    --cc=22679@debbugs.gnu.org \
    --cc=monnier@iro.umontreal.ca \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).