unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* [PATCH] shell-command and whitespace
@ 2015-01-26  0:54 Daniel Colascione
  2015-01-26  6:29 ` Achim Gratz
  0 siblings, 1 reply; 5+ messages in thread
From: Daniel Colascione @ 2015-01-26  0:54 UTC (permalink / raw)
  To: Emacs-devel

[-- Attachment #1: Type: text/plain, Size: 3521 bytes --]

C-u M-! inserts the output of a shell command in the current buffer.
That's great, but this output includes the terminating newline that
almost all shell commands produce, and I almost never want that newline,
or any other whitespace.

How about this patch, which makes shell-command strip whitespace by
default when called interactively?

diff --git a/lisp/simple.el b/lisp/simple.el
index 25293ed..918b9bd 100644
--- a/lisp/simple.el
+++ b/lisp/simple.el
@@ -2973,7 +2973,8 @@ shell (with its need to quote arguments)."

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

 If COMMAND ends in `&', execute it asynchronously.
 The output appears in the buffer `*Async Shell Command*'.
@@ -2997,11 +2998,16 @@ Noninteractive callers can specify coding
systems by binding
 The optional second argument OUTPUT-BUFFER, if non-nil,
 says to put the output in some other buffer.
 If OUTPUT-BUFFER is a buffer or buffer name, put the output there.
-If OUTPUT-BUFFER is not a buffer and not nil,
-insert output in current buffer.  (This cannot be done asynchronously.)
 In either case, the buffer is first erased, and the output is
 inserted after point (leaving mark after it).

+If OUTPUT-BUFFER is not a buffer and not nil, insert output in
+current buffer.  (This cannot be done asynchronously.)  In this
+case, the output buffer is not erased.  Additionally, if
+OUTPUT-BUFFER is `:strip', leading and trailing whitespace is
+removed before inserting the shell command output in the current
+buffer.
+
 If the command terminates without error, but generates output,
 and you did not specify \"insert it in the current buffer\",
 the output can be displayed in the echo area or in its buffer.
@@ -3036,7 +3042,7 @@ the use of a shell (with its need to quote
arguments)."
 				((eq major-mode 'dired-mode)
 				 (dired-get-filename nil t)))))
 			  (and filename (file-relative-name filename))))
-    current-prefix-arg
+    (if current-prefix-arg :strip)
     shell-command-default-error-buffer))
   ;; Look for a handler in case default-directory is a remote file name.
   (let ((handler
@@ -3083,10 +3089,21 @@ the use of a shell (with its need to quote
arguments)."
 	    ;; This is like exchange-point-and-mark, but doesn't
 	    ;; activate the mark.  It is cleaner to avoid activation,
 	    ;; even though the command loop would deactivate the mark
-	    ;; because we inserted text.
+	    ;; because we inserted text.
 	    (goto-char (prog1 (mark t)
 			 (set-marker (mark-marker) (point)
-				     (current-buffer)))))
+				     (current-buffer))))
+            (when (eq output-buffer :strip)
+              (save-excursion
+                (save-restriction
+                  (narrow-to-region (min (point) (mark t))
+                                    (max (point) (mark t)))
+                  (goto-char (point-min))
+                  (skip-chars-forward " \v\t\n\r")
+                  (delete-region (point-min) (point))
+                  (goto-char (point-max))
+                  (skip-chars-backward " \v\t\n\r")
+                  (delete-region (point) (point-max))))))
 	;; Output goes in a separate buffer.
 	;; Preserve the match data in case called from a program.
 	(save-match-data


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 819 bytes --]

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] shell-command and whitespace
  2015-01-26  0:54 [PATCH] shell-command and whitespace Daniel Colascione
@ 2015-01-26  6:29 ` Achim Gratz
  2015-02-16 21:40   ` Daniel Colascione
  0 siblings, 1 reply; 5+ messages in thread
From: Achim Gratz @ 2015-01-26  6:29 UTC (permalink / raw)
  To: emacs-devel

Daniel Colascione writes:
> C-u M-! inserts the output of a shell command in the current buffer.
> That's great, but this output includes the terminating newline that
> almost all shell commands produce, and I almost never want that newline,
> or any other whitespace.
>
> How about this patch, which makes shell-command strip whitespace by
> default when called interactively?

How about you keep the behaviour as is and have double prefix strip the
whitespace instead?


Regards,
Achim.
-- 
+<[Q+ Matrix-12 WAVE#46+305 Neuron microQkb Andromeda XTk Blofeld]>+

SD adaptations for KORG EX-800 and Poly-800MkII V0.9:
http://Synth.Stromeko.net/Downloads.html#KorgSDada




^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] shell-command and whitespace
  2015-01-26  6:29 ` Achim Gratz
@ 2015-02-16 21:40   ` Daniel Colascione
  2015-02-18  8:45     ` Achim Gratz
  0 siblings, 1 reply; 5+ messages in thread
From: Daniel Colascione @ 2015-02-16 21:40 UTC (permalink / raw)
  To: Achim Gratz, emacs-devel

[-- Attachment #1: Type: text/plain, Size: 649 bytes --]

On 01/25/2015 10:29 PM, Achim Gratz wrote:
> Daniel Colascione writes:
>> C-u M-! inserts the output of a shell command in the current buffer.
>> That's great, but this output includes the terminating newline that
>> almost all shell commands produce, and I almost never want that newline,
>> or any other whitespace.
>>
>> How about this patch, which makes shell-command strip whitespace by
>> default when called interactively?
> 
> How about you keep the behaviour as is and have double prefix strip the
> whitespace instead?

Most of the time, I want to strip whitespace, and whitespace stripping
is how $() works in the shell.


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 819 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] shell-command and whitespace
  2015-02-16 21:40   ` Daniel Colascione
@ 2015-02-18  8:45     ` Achim Gratz
  2015-02-18 17:04       ` Juri Linkov
  0 siblings, 1 reply; 5+ messages in thread
From: Achim Gratz @ 2015-02-18  8:45 UTC (permalink / raw)
  To: emacs-devel

Daniel Colascione writes:
>>> How about this patch, which makes shell-command strip whitespace by
>>> default when called interactively?
>> 
>> How about you keep the behaviour as is and have double prefix strip the
>> whitespace instead?
>
> Most of the time, I want to strip whitespace, and whitespace stripping
> is how $() works in the shell.

That may well be, but I mostly don't want this command to strip
whitespace as has been the default for many years.  So instead of
redefining what it does I again suggest you add that functionality.
From a usability point of view double prefix is almost as good as a
single prefix and you're free to provide customization to reverse the
meaning of the two.  But please keep the default what it was.


Regards,
Achim.
-- 
+<[Q+ Matrix-12 WAVE#46+305 Neuron microQkb Andromeda XTk Blofeld]>+

SD adaptations for KORG EX-800 and Poly-800MkII V0.9:
http://Synth.Stromeko.net/Downloads.html#KorgSDada




^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] shell-command and whitespace
  2015-02-18  8:45     ` Achim Gratz
@ 2015-02-18 17:04       ` Juri Linkov
  0 siblings, 0 replies; 5+ messages in thread
From: Juri Linkov @ 2015-02-18 17:04 UTC (permalink / raw)
  To: Achim Gratz; +Cc: emacs-devel

>>>> How about this patch, which makes shell-command strip whitespace by
>>>> default when called interactively?
>>>
>>> How about you keep the behaviour as is and have double prefix strip the
>>> whitespace instead?
>>
>> Most of the time, I want to strip whitespace, and whitespace stripping
>> is how $() works in the shell.
>
> That may well be, but I mostly don't want this command to strip
> whitespace as has been the default for many years.  So instead of
> redefining what it does I again suggest you add that functionality.
> From a usability point of view double prefix is almost as good as a
> single prefix and you're free to provide customization to reverse the
> meaning of the two.  But please keep the default what it was.

I noticed that most of the time, the final newline needs to be
removed when output is a single line, but needs to be preserved
when output is multi-line.



^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2015-02-18 17:04 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-01-26  0:54 [PATCH] shell-command and whitespace Daniel Colascione
2015-01-26  6:29 ` Achim Gratz
2015-02-16 21:40   ` Daniel Colascione
2015-02-18  8:45     ` Achim Gratz
2015-02-18 17:04       ` Juri Linkov

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).