From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Daniel Colascione Newsgroups: gmane.emacs.devel Subject: [PATCH] shell-command and whitespace Date: Sun, 25 Jan 2015 16:54:52 -0800 Message-ID: <54C5905C.2030501@dancol.org> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="ewgpBD8PrVKxqKEc4lXkhoWsOkxxS1JtL" X-Trace: ger.gmane.org 1422233717 25112 80.91.229.3 (26 Jan 2015 00:55:17 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Mon, 26 Jan 2015 00:55:17 +0000 (UTC) To: Emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Mon Jan 26 01:55:16 2015 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1YFXxT-0004sM-LV for ged-emacs-devel@m.gmane.org; Mon, 26 Jan 2015 01:55:15 +0100 Original-Received: from localhost ([::1]:39450 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YFXxS-00064U-Ra for ged-emacs-devel@m.gmane.org; Sun, 25 Jan 2015 19:55:14 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:51110) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YFXxO-00064E-WE for Emacs-devel@gnu.org; Sun, 25 Jan 2015 19:55:12 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1YFXxN-0008JQ-2c for Emacs-devel@gnu.org; Sun, 25 Jan 2015 19:55:10 -0500 Original-Received: from dancol.org ([2600:3c01::f03c:91ff:fedf:adf3]:56676) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YFXxM-00084v-LA for Emacs-devel@gnu.org; Sun, 25 Jan 2015 19:55:09 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=dancol.org; s=x; h=Content-Type:Subject:To:MIME-Version:From:Date:Message-ID; bh=iwjTI3qNG6IfW+P7cTsNjZUPlY9VEmm09+bO6VW299k=; b=Bd9TOf+C/E85Rs6tsz4wJ0Fuc1EHK1frh2Y8u1gSqQ/2Zc71cklLMuIHs5LTwWKgCo75fpHUd08lgilG0/5xzNtiljLWGc/oPY0uZadRp3P/Bt93aC5NpUO8ZVcRwkXCtpE0F2gIznkoijDTd12PtncfHsQowBxyrC8hUR46rnOz0b4phnuVurKxjnqnrBfSM81KKDAjDXZ1mnCiK5Q1npOYQQoDy4Ac6Ng99uc0JI8Gw//3TEqRX9W+2zxRWQlGHEoAleKT5wg2E1Js2fPmfux1r128gicw8J9Ij/AP7hkf4yvwYSEfzJlUQAW98DEj8+aSjHX9FxbXMYoMNbfR9A==; Original-Received: from [2620:10d:c0a1:1100::1000] by dancol.org with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.84) (envelope-from ) id 1YFXxD-00030A-S7 for Emacs-devel@gnu.org; Sun, 25 Jan 2015 16:55:00 -0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.4.0 X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2600:3c01::f03c:91ff:fedf:adf3 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.devel:181767 Archived-At: This is an OpenPGP/MIME signed message (RFC 4880 and 3156) --ewgpBD8PrVKxqKEc4lXkhoWsOkxxS1JtL Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable 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 --ewgpBD8PrVKxqKEc4lXkhoWsOkxxS1JtL Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v1 iQIcBAEBCAAGBQJUxZBcAAoJEN4WImmbpWBl3P0QAKhsGXGwulpkGi394wSWO8TI vHYsFTwZdBFNkcBJ9h24NHMlueKEzCU1J7DB4pBodtrRxxO346PcPuuxPonJNMWR HzARObbRNnt1BIbNnScduG+EfdGvlIbk4eMeeCaATRRnwp32zETAO7Q0eXY+WMa4 gNTFhD3zSC0pnKmrKzGzAQEUvObhh6WkPJb1hRGYLNVw8eWLBXgF6J2dm3tuhhDv lX+zFy6fLEk0FbSxl/WhD7J6ZbYrQwQpTdNDhp/buf09QwISN1n2+hE6uhwpUYVm cNYWC88fuk4TkVwUoPzp1V1FqDsVw43LnyIRGAtwciAmAXDXA9/WtZ8fqpxpRLse RRiBYOyxuBQCJvg7CpJMrQ7mV0G2NaaqekUWdPGWA/n5MHlkCGKrbXOJ0d7ommNY QuTbelLidm1gULKqoX/gN0SDbVBs/9PQO64l9eWUS++ysI7C/9rk5pBxq8jUAISm yXqoOSdEUzb/WXnRrS4ZBSwihv3qncaI1XL2Wq7MDgYtDN7t1A2W85toB905oDPy VlsthOnwtZ+Ggrnga9WUMFzRboPeunAqrAxsqI7PA32EuM4kgtq29YwqsgGaZBYk fZtwT05BbzSk4TKJt3sL5J7xb7Wl43pSGOv+mgdKoB71YzGb4GsmdnqZ/uKFvhXl sZ+ySxO5i9GM9q/S4oc7 =hIBx -----END PGP SIGNATURE----- --ewgpBD8PrVKxqKEc4lXkhoWsOkxxS1JtL--