From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Tino Calancha Newsgroups: gmane.emacs.bugs Subject: bug#22679: 25.0.91; ibuffer-do-shell-command-pipe truncate output Date: Fri, 27 Jan 2017 15:26:32 +0900 Message-ID: <87o9ytuig7.fsf@calancha-pc> References: <7oa66k9es.fsf@fencepost.gnu.org> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: blaine.gmane.org 1485498438 16969 195.159.176.226 (27 Jan 2017 06:27:18 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Fri, 27 Jan 2017 06:27:18 +0000 (UTC) User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.0.50 (gnu/linux) Cc: 22679@debbugs.gnu.org, tino.calancha@gmail.com To: Stefan Monnier Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Fri Jan 27 07:27:14 2017 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cX003-0003LZ-34 for geb-bug-gnu-emacs@m.gmane.org; Fri, 27 Jan 2017 07:27:07 +0100 Original-Received: from localhost ([::1]:42983 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cX008-0000LQ-GV for geb-bug-gnu-emacs@m.gmane.org; Fri, 27 Jan 2017 01:27:12 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:53468) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cX001-0000LD-OW for bug-gnu-emacs@gnu.org; Fri, 27 Jan 2017 01:27:07 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cWzzy-0003ZY-Fz for bug-gnu-emacs@gnu.org; Fri, 27 Jan 2017 01:27:05 -0500 Original-Received: from debbugs.gnu.org ([208.118.235.43]:48665) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1cWzzy-0003ZS-Bf for bug-gnu-emacs@gnu.org; Fri, 27 Jan 2017 01:27:02 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1cWzzy-0001gS-2I for bug-gnu-emacs@gnu.org; Fri, 27 Jan 2017 01:27:02 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Tino Calancha Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Fri, 27 Jan 2017 06:27:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 22679 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch Original-Received: via spool by 22679-submit@debbugs.gnu.org id=B22679.14854984076452 (code B ref 22679); Fri, 27 Jan 2017 06:27:02 +0000 Original-Received: (at 22679) by debbugs.gnu.org; 27 Jan 2017 06:26:47 +0000 Original-Received: from localhost ([127.0.0.1]:46864 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cWzzi-0001fz-Gg for submit@debbugs.gnu.org; Fri, 27 Jan 2017 01:26:46 -0500 Original-Received: from mail-pg0-f67.google.com ([74.125.83.67]:35142) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cWzzg-0001fm-U7 for 22679@debbugs.gnu.org; Fri, 27 Jan 2017 01:26:45 -0500 Original-Received: by mail-pg0-f67.google.com with SMTP id 204so24243810pge.2 for <22679@debbugs.gnu.org>; Thu, 26 Jan 2017 22:26:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:references:date:in-reply-to:message-id :user-agent:mime-version; bh=jj2KCOatyhGxASe4hYAVWzUr2nV8BQDD9jb5VqsVCM8=; b=uDduN/BH7UDA2MClUnJfwRFIw+K1RWH8T8WsPYMrPOqmkhSpeX8OpthHH/NVezTWEE b6luR83I+eRG/83d0hKW7knYP1baersjWJ8xPBcQRkpUWML6WhmnJ2rV/1nkaSuEDb7e Ua3EiafDt/iHq6vvjW/NdDkCkcZnoU1XUezKGQ0yHOzL79iAB5Stymke4HMpTNpvFvIG PMtmUr78pMV0+9AiEFBs0RrhK8e45ZB6gyTEJ3wriGfHxAuG58Ick3H25d9n8BKJT2tl yUBVD2o+xeTzQmPvTUqI7vWDxyJjCLFnMBG90Moo78UGXpaA+vO04rbNy1PIAqUL/DNj 4y9Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:references:date:in-reply-to :message-id:user-agent:mime-version; bh=jj2KCOatyhGxASe4hYAVWzUr2nV8BQDD9jb5VqsVCM8=; b=lyKe82mE/mXeprFaN7+FWAHLCTjGWx58TOCOVXP5a8k8CL9iS/i/gJZ9r3LxcUR0ca kLCSnIbFk+ftw4XvfYuo7r/imtgYt5MNjDQaG7x9ids3sMW4Dvo+FQ7cYf7nkUhH3i2r R8Gac7c4Hrz1LfFIYXT9iN75Y6Ev7jPgUFWiWK4z/QNcN75D7gFdGctRlCvfLnfWRgN2 jKqxGT9jKcOWeqDsohTvxiSsfadv8EN2YozZpuTxPKVznBNvDXll8VyGCDN6RWJZCsjA CkQOFU1+4WKSlbrvleVaF6GtLPaqJDAx2c7qEn76SKHkva55t4e+khkMIXNZC+yfyXj5 uSGA== X-Gm-Message-State: AIkVDXKKlL56AlKy40po3GAYeHdtDQD0mxVEuvg8GG6D1fbO81reG0l9NrIakR6s4+Y0dw== X-Received: by 10.84.136.7 with SMTP id 7mr10053400plk.100.1485498399176; Thu, 26 Jan 2017 22:26:39 -0800 (PST) Original-Received: from calancha-pc (33.94.100.220.dy.bbexcite.jp. [220.100.94.33]) by smtp.gmail.com with ESMTPSA id w25sm7803093pge.9.2017.01.26.22.26.36 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 26 Jan 2017 22:26:38 -0800 (PST) In-Reply-To: (Stefan Monnier's message of "Wed, 24 Aug 2016 13:05:56 -0400") X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 208.118.235.43 X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.org gmane.emacs.bugs:128640 Archived-At: Stefan Monnier 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 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 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