unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Juri Linkov <juri@linkov.net>
To: asparagus@comcast.net
Cc: 20626@debbugs.gnu.org
Subject: bug#20626: Wishlist: M-x shell-command-on-rectangle-region
Date: Tue, 23 Jun 2015 01:37:53 +0300	[thread overview]
Message-ID: <874mlzweri.fsf@mail.linkov.net> (raw)
In-Reply-To: <87fv5s4pm0.fsf@mail.linkov.net> (Juri Linkov's message of "Tue,  16 Jun 2015 00:45:27 +0300")

> Sorry, I was wrong.  I realized now that query-replace has quite
> a different requirement.  query-replace needs rectangular boundaries
> to limit the search for replacements, whereas shell-command-on-region
> should extract the rectangular region as strings and replace it with
> the result of the command.  Here is a working prototype that demonstrates
> its possible implementation:
>
> (define-advice shell-command-on-region
>     (:around (orig-fun start end command
>                        &optional output-buffer replace
>                        error-buffer display-error-buffer))
>   (if (and (boundp 'rectangle-mark-mode) rectangle-mark-mode)
>       (let ((input (mapconcat 'identity (delete-extract-rectangle start end) "\n"))
>             output)
>         (with-temp-buffer
>           (insert input)
>           (call-process-region (point-min) (point-max)
>                                shell-file-name t t
>                                nil shell-command-switch
>                                command)
>           (setq output (split-string (buffer-string) "\n")))
>         (goto-char start)
>         (insert-rectangle output))
>     (funcall orig-fun start end command
>              output-buffer replace
>              error-buffer display-error-buffer)))
>
> This is another case to take into account when designing the interface,
> i.e. in this case the list of boundaries in the arg START is not necessary,
> and I have no idea how to avoid `(if (and (boundp 'rectangle-mark-mode)
> rectangle-mark-mode))'

For example, the command ‘kill-ring-save’ has the signature

  kill-ring-save (beg end &optional region)

with an additional boolean arg for the region.

And ‘rectangle--extract-region’ contains a condition
that checks for ‘rectangle-mark-mode’.

Combining these two prerequisites we could do a similar thing in
‘shell-command-on-region’ by adding a new arg and using it in the
function body:

diff --git a/lisp/simple.el b/lisp/simple.el
index 1868077..d022504 100644
--- a/lisp/simple.el
+++ b/lisp/simple.el
@@ -3274,7 +3278,8 @@
 
 (defun shell-command-on-region (start end command
 				      &optional output-buffer replace
-				      error-buffer display-error-buffer)
+				      error-buffer display-error-buffer
+				      region)
   "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
@@ -3337,7 +3342,8 @@ (defun shell-command-on-region (start end command
 		       current-prefix-arg
 		       current-prefix-arg
 		       shell-command-default-error-buffer
-		       t)))
+		       t
+		       rectangle-mark-mode)))
   (let ((error-file
 	 (if error-buffer
 	     (make-temp-file
@@ -3346,6 +3352,18 @@ (defun shell-command-on-region (start end command
 				    temporary-file-directory)))
 	   nil))
 	exit-status)
+    (if region
+        (let ((input (mapconcat 'identity (delete-extract-rectangle start end) "\n"))
+              output)
+          (with-temp-buffer
+            (insert input)
+            (call-process-region (point-min) (point-max)
+                                 shell-file-name t t
+                                 nil shell-command-switch
+                                 command)
+            (setq output (split-string (buffer-string) "\n")))
+          (goto-char start)
+          (insert-rectangle output))
     (if (or replace
 	    (and output-buffer
 		 (not (or (bufferp output-buffer) (stringp output-buffer)))))
@@ -3435,7 +3453,7 @@ (defun shell-command-on-region (start end command
 			      exit-status output))))
 	    ;; Don't kill: there might be useful info in the undo-log.
 	    ;; (kill-buffer buffer)
-	    ))))
+	    )))))
 
     (when (and error-file (file-exists-p error-file))
       (if (< 0 (nth 7 (file-attributes error-file)))





  reply	other threads:[~2015-06-22 22:37 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <1119163948.10099018.1432245074823.JavaMail.zimbra@comcast.net>
2015-05-21 22:04 ` bug#20626: Wishlist: M-x shell-command-on-rectangle-region asparagus
2015-05-21 22:47   ` Juri Linkov
2015-06-15 21:45     ` Juri Linkov
2015-06-22 22:37       ` Juri Linkov [this message]
2015-06-23  2:02         ` Stefan Monnier
2015-06-23 22:59           ` Juri Linkov
2015-06-23 23:50             ` Stefan Monnier
2015-06-24 22:27               ` Juri Linkov
2015-06-25  3:45                 ` Stefan Monnier
2015-06-25 22:30               ` Juri Linkov
2015-06-26  1:53                 ` Stefan Monnier
2015-06-30 20:44                   ` Juri Linkov

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=874mlzweri.fsf@mail.linkov.net \
    --to=juri@linkov.net \
    --cc=20626@debbugs.gnu.org \
    --cc=asparagus@comcast.net \
    /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).