From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Philip Kaludercic Newsgroups: gmane.emacs.bugs Subject: bug#69097: [PATCH] Add 'kill-region-or-word' command Date: Tue, 03 Sep 2024 16:32:46 +0000 Message-ID: <877cbshf4h.fsf@posteo.net> References: <871q9g8z8k.fsf@posteo.net> <87wmob73ds.fsf@posteo.net> <861q6jji0i.fsf@gnu.org> <87o79n6uj6.fsf@posteo.net> <86v83vi2kr.fsf@gnu.org> <86seyzi2by.fsf@gnu.org> <87cyq26bt8.fsf@posteo.net> <864jbeixkt.fsf@gnu.org> <874jbe65u0.fsf@posteo.net> <8634qwkbtc.fsf@mail.linkov.net> <86h6fceij2.fsf@gnu.org> <87edaeevmr.fsf@zephyr.silentflame.com> <87a5l2evdn.fsf@zephyr.silentflame.com> <86ikzqbzac.fsf@gnu.org> <87seujf8bb.fsf@posteo.net> <867cbu9uce.fsf@gnu.org> <87cyllg57r.fsf@posteo.net> <87zfopbxdk.fsf@zephyr.silentflame.com> <87seuhbx87.fsf@zephyr.silentflame.com> <87zfopep5q.fsf@posteo.net> <86r0a16i71.fsf@gnu.org> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="4912"; mail-complaints-to="usenet@ciao.gmane.io" Cc: rms@gnu.org, 69097@debbugs.gnu.org, juri@linkov.net, stefankangas@gmail.com, acorallo@gnu.org, spwhitton@spwhitton.name To: Eli Zaretskii Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Tue Sep 03 18:34:31 2024 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1slWUB-000152-85 for geb-bug-gnu-emacs@m.gmane-mx.org; Tue, 03 Sep 2024 18:34:31 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1slWTn-0004Vz-DF; Tue, 03 Sep 2024 12:34:08 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1slWTh-00045a-0T for bug-gnu-emacs@gnu.org; Tue, 03 Sep 2024 12:34:01 -0400 Original-Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1slWTg-0006Lc-53 for bug-gnu-emacs@gnu.org; Tue, 03 Sep 2024 12:34:00 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=debbugs.gnu.org; s=debbugs-gnu-org; h=MIME-Version:Date:References:In-Reply-To:From:To:Subject; bh=AYdSX71+EGVDFVDLkGZVIHNeHfTQssaxg6ot16eAaaU=; b=qj0jad9yoJb+XoutxOhXK1PD7u1nYaMZIi90LMN42MkBWD+7UJm8b8Imp/gffcnfPmVYVOaaKUEM7Sfe9wIex9hDmsOxH3mfJjriKR05djIvLAULkd/uZ5zSqJMt6Q+wPGH5XvXbi05cWkxle6EzAFiG3k1bAQ+SaNgSq+iuDWuRAW5/Vmq/i/dI4TOomhmyNCrQiyKEfG8WEi6yX5/M+nYNtGwgHdU/AkATb5fc2yEDlZipjZgAT4GqDmqGsg0Op1WPaF8Wgi8f9qvuCfUKcC05nZI62PsJuAd9aSJMXSnbKsvdxBcqR445gym6l+BTn4kiwr+DcTuht3nEweVZdg==; Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1slWUg-0001DE-IV for bug-gnu-emacs@gnu.org; Tue, 03 Sep 2024 12:35:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Philip Kaludercic Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Tue, 03 Sep 2024 16:35:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 69097 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch Original-Received: via spool by 69097-submit@debbugs.gnu.org id=B69097.17253812434518 (code B ref 69097); Tue, 03 Sep 2024 16:35:02 +0000 Original-Received: (at 69097) by debbugs.gnu.org; 3 Sep 2024 16:34:03 +0000 Original-Received: from localhost ([127.0.0.1]:60666 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1slWTi-0001Ao-RI for submit@debbugs.gnu.org; Tue, 03 Sep 2024 12:34:03 -0400 Original-Received: from mout01.posteo.de ([185.67.36.65]:35957) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1slWTf-0001AD-Au for 69097@debbugs.gnu.org; Tue, 03 Sep 2024 12:34:01 -0400 Original-Received: from submission (posteo.de [185.67.36.169]) by mout01.posteo.de (Postfix) with ESMTPS id 70101240027 for <69097@debbugs.gnu.org>; Tue, 3 Sep 2024 18:32:49 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=posteo.net; s=2017; t=1725381169; bh=Lg5gr8Sk6UH1rPBZZgBk33rnH1N7w94OZs8uTbMJjEg=; h=From:To:Cc:Subject:Autocrypt:OpenPGP:Date:Message-ID:MIME-Version: Content-Type:From; b=DgDQuDEPNLqHJ6xmjhmJXwg1w9bAnyPptxPfZH4+sNKHdM/q0PvjhTeQ4OpoAATtk AzKfOSB60HBdX9Btl5x3YhoCiuJRYWILuF8dIuvNPLCW5bXcYoJOL5bGjlnPYMcjzF +Z89YxujnRKBXdC7ppH016kE0KhZEo3Sal+MoJYgZZ7DQXTUvH+UtTsNWtbKV3MsOd /IH38/15POqXFgTel8+srzEmJVQZjoBwbYJyccWzwktTAIA9Lft8JoJUD0HFafRVbK CjAjRhy5vN8Zfr9I9w+UsBGLhTvJuWqDzAdd/Gfn1FLEKl/MrgTKY9eL0QZJmcsZ0K DYP/k0yrgdBQQ== Original-Received: from customer (localhost [127.0.0.1]) by submission (posteo.de) with ESMTPSA id 4Wyrl30fnSz9rxG; Tue, 3 Sep 2024 18:32:46 +0200 (CEST) In-Reply-To: <86r0a16i71.fsf@gnu.org> (Eli Zaretskii's message of "Tue, 03 Sep 2024 15:21:54 +0300") Autocrypt: addr=philipk@posteo.net; keydata= mDMEZBBQQhYJKwYBBAHaRw8BAQdAHJuofBrfqFh12uQu0Yi7mrl525F28eTmwUDflFNmdui0QlBo aWxpcCBLYWx1ZGVyY2ljIChnZW5lcmF0ZWQgYnkgYXV0b2NyeXB0LmVsKSA8cGhpbGlwa0Bwb3N0 ZW8ubmV0PoiWBBMWCAA+FiEEDg7HY17ghYlni8XN8xYDWXahwukFAmQQUEICGwMFCQHhM4AFCwkI BwIGFQoJCAsCBBYCAwECHgECF4AACgkQ8xYDWXahwulikAEA77hloUiSrXgFkUVJhlKBpLCHUjA0 mWZ9j9w5d08+jVwBAK6c4iGP7j+/PhbkxaEKa4V3MzIl7zJkcNNjHCXmvFcEuDgEZBBQQhIKKwYB BAGXVQEFAQEHQI5NLiLRjZy3OfSt1dhCmFyn+fN/QKELUYQetiaoe+MMAwEIB4h+BBgWCAAmFiEE Dg7HY17ghYlni8XN8xYDWXahwukFAmQQUEICGwwFCQHhM4AACgkQ8xYDWXahwukm+wEA8cml4JpK NeAu65rg+auKrPOP6TP/4YWRCTIvuYDm0joBALw98AMz7/qMHvSCeU/hw9PL6u6R2EScxtpKnWof z4oM OpenPGP: id=philipk@posteo.net; url="https://keys.openpgp.org/vks/v1/by-email/philipk@posteo.net"; preference=signencrypt X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list 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-mx.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:291157 Archived-At: --=-=-= Content-Type: text/plain Eli Zaretskii writes: >> From: Philip Kaludercic >> Cc: Eli Zaretskii , Stefan Kangas , >> Andrea Corallo , juri@linkov.net, rms@gnu.org, >> 69097@debbugs.gnu.org >> Date: Mon, 02 Sep 2024 21:12:01 +0000 >> >> I had misremembered the last state of this patch. It is easier to just >> have a tristate option. Here is the updated proposal: > > Thanks. > >> +(defcustom kill-word-if-no-region nil > > I would call this 'kill-region-dwim' instead. Can do. >> + "Behaviour when `kill-region' is invoked without an active region. >> +If set to nil (default), then an error occurs and nothing is killed. If >> +set to `emacs-word', then kill a the last word as defined by the current >> +major mode. If set to `unix-word', then kill the last word in the style >> +of a shell like Bash, disregarding the major mode." >> + :type '(choice (const :tag "Kill a word like `backward-kill-word'" emacs-word) >> + (const :tag "Kill a word like Bash would" unix-word) >> + (const :tag "Do not kill anything" nil)) >> + :group 'killing) > > :version tag is missing. Whoops, added it. >> -Lisp programs should use this function for killing text. >> - (To delete text, use `delete-region'.) >> -Supply two arguments, character positions BEG and END indicating the >> - stretch of text to be killed. If the optional argument REGION is >> - non-nil, the function ignores BEG and END, and kills the current >> - region instead. Interactively, REGION is always non-nil, and so >> - this command always kills the current region." >> +Lisp programs should use this function for killing text. (To delete >> +text, use `delete-region'.) Supply two arguments, character positions >> +BEG and END indicating the stretch of text to be killed. If the >> +optional argument REGION is non-nil, the function ignores BEG and END, >> +and kills the current region instead. If REGION has the special value > > Not sure why you decided to reformat this part. Its formatting was > not random. I think this was just an accidental M-q. > This also needs a NEWS entry. I've added the NEWS entry from the last iteration of the patch (now actually as a patch, not just a diff): --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=0001-Allow-kill-region-kill-the-last-word-when-there-is-n.patch >From 981331cfe0757b21f529be02d848a3ef7bcc4295 Mon Sep 17 00:00:00 2001 From: Philip Kaludercic Date: Tue, 3 Sep 2024 18:29:56 +0200 Subject: [PATCH] Allow 'kill-region' kill the last word when there is no region * etc/NEWS: Document the new user option. * lisp/simple.el (kill-region-dwim): Add new option. (kill-region): Respect 'kill-region-dwim'. (Bug#69097) --- etc/NEWS | 6 ++++++ lisp/simple.el | 50 ++++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 48 insertions(+), 8 deletions(-) diff --git a/etc/NEWS b/etc/NEWS index 1e66f084117..7fadf52a6cf 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -123,6 +123,12 @@ When using 'visual-wrap-prefix-mode' in buffers with variable-pitch fonts, the wrapped text will now be lined up correctly so that it's exactly below the text after the prefix on the first line. +--- +** New user option 'kill-word-if-no-region'. +This option will modify the fall-back behaviour of 'kill-region' if no +region is active, and will kill the last word instead of raising an +error. + * Changes in Specialized Modes and Packages in Emacs 31.1 diff --git a/lisp/simple.el b/lisp/simple.el index eedc5768fe2..3b4453c7a8f 100644 --- a/lisp/simple.el +++ b/lisp/simple.el @@ -5817,6 +5817,17 @@ kill-read-only-ok :type 'boolean :group 'killing) +(defcustom kill-region-dwim nil + "Behaviour when `kill-region' is invoked without an active region. +If set to nil (default), then an error occurs and nothing is killed. If +set to `emacs-word', then kill a the last word as defined by the current +major mode. If set to `unix-word', then kill the last word in the style +of a shell like Bash, disregarding the major mode." + :type '(choice (const :tag "Kill a word like `backward-kill-word'" emacs-word) + (const :tag "Kill a word like Bash would" unix-word) + (const :tag "Do not kill anything" nil)) + :group 'killing) + (defun kill-region (beg end &optional region) "Kill (\"cut\") text between point and mark. This deletes the text from the buffer and saves it in the kill ring. @@ -5843,21 +5854,44 @@ kill-region (To delete text, use `delete-region'.) Supply two arguments, character positions BEG and END indicating the stretch of text to be killed. If the optional argument REGION is - non-nil, the function ignores BEG and END, and kills the current + `region', the function ignores BEG and END, and kills the current region instead. Interactively, REGION is always non-nil, and so - this command always kills the current region." + this command always kills the current region. It is possible to + override this behaviour by customising the user option + `kill-region-dwim'." ;; Pass mark first, then point, because the order matters when ;; calling `kill-append'. (interactive (progn (let ((beg (mark)) (end (point))) - (unless (and beg end) - (user-error "The mark is not set now, so there is no region")) - (list beg end 'region)))) + (cond + ((and beg end (use-region-p)) + (list beg end 'region)) + (kill-region-dwim + (list beg end kill-region-dwim)) + ((user-error "The mark is not set now, so there is no region")))))) + (condition-case nil - (let ((string (if region - (funcall region-extract-function 'delete) - (filter-buffer-substring beg end 'delete)))) + (let ((string (cond + ((eq region 'emacs-word) + (let ((end (point))) + (save-excursion + (forward-word -1) + (filter-buffer-substring (point) end 'delete)))) + ((eq region 'unix-word) + (let ((end (point))) + (save-excursion + (skip-chars-backward "[:space:]") + (skip-chars-backward "^[:space:]") + (filter-buffer-substring + (if (get-char-property (point) 'read-only) + (next-single-char-property-change + (point) 'read-only nil end) + (point)) + end 'delete)))) + (region + (funcall region-extract-function 'delete)) + ((filter-buffer-substring beg end 'delete))))) (when string ;STRING is nil if BEG = END ;; Add that string to the kill ring, one way or another. (if (eq last-command 'kill-region) -- 2.46.0 --=-=-= Content-Type: text/plain Robert Pluim writes: >>>>>> On Tue, 03 Sep 2024 15:21:54 +0300, Eli Zaretskii said: > > >> From: Philip Kaludercic > >> Cc: Eli Zaretskii , Stefan Kangas , > >> Andrea Corallo , juri@linkov.net, rms@gnu.org, > >> 69097@debbugs.gnu.org > >> Date: Mon, 02 Sep 2024 21:12:01 +0000 > >> > >> I had misremembered the last state of this patch. It is easier to just > >> have a tristate option. Here is the updated proposal: > > Eli> Thanks. > > >> +(defcustom kill-word-if-no-region nil > > Eli> I would call this 'kill-region-dwim' instead. > > >> + "Behaviour when `kill-region' is invoked without an active region. > >> +If set to nil (default), then an error occurs and nothing is killed. If > >> +set to `emacs-word', then kill a the last word as defined by the current > >> +major mode. If set to `unix-word', then kill the last word in the style > >> +of a shell like Bash, disregarding the major mode." > >> + :type '(choice (const :tag "Kill a word like `backward-kill-word'" emacs-word) > >> + (const :tag "Kill a word like Bash would" unix-word) > >> + (const :tag "Do not kill anything" nil)) > >> + :group 'killing) > > Eli> :version tag is missing. > > Is it worth allowing a user-specified function? That would be possible as well, but to make it manageable with the current approach the function would have to be one that moves the point. So we could rewrite the patch with two default options `backward-word' and `unix-backward-word' (which we would have to add), but I am not sure it would have much use. Perhaps killing the current word, instead of just killing to the beginning? > Robert -- Philip Kaludercic on peregrine --=-=-=--