unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Philip Kaludercic <philipk@posteo.net>
To: Sean Whitton <spwhitton@spwhitton.name>
Cc: rms@gnu.org, 69097@debbugs.gnu.org, juri@linkov.net,
	stefankangas@gmail.com, Eli Zaretskii <eliz@gnu.org>,
	acorallo@gnu.org
Subject: bug#69097: [PATCH] Add 'kill-region-or-word' command
Date: Sat, 14 Sep 2024 13:39:43 +0000	[thread overview]
Message-ID: <87ttei2w2o.fsf@posteo.net> (raw)
In-Reply-To: <87r09m8ogh.fsf@zephyr.silentflame.com> (Sean Whitton's message of "Sat, 14 Sep 2024 12:27:42 +0100")

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

Sean Whitton <spwhitton@spwhitton.name> writes:

> Hello,
>
> Thanks for the re-review, Eli.  I've pushed my patch.
>
>> This should also say explicitly that users who turn off Transient Mark
>> mode will need to either activate the region ("C-x C-x" etc.) or
>> invoke the new word-rubout commands by hand, since otherwise the
>> region is never active for them.
>
> This seems like Philip might want to think a bit about what it is more
> helpful to say, so I'll hold off on editing and pushing his patch
> myself.

How about


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Allow-kill-region-kill-the-last-word-when-there-is-n.patch --]
[-- Type: text/x-diff, Size: 4527 bytes --]

From 2fb36ab68eb177e232b9ee4b0d5ea105b9aa9f16 Mon Sep 17 00:00:00 2001
From: Philip Kaludercic <philipk@posteo.net>
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       |  9 +++++++++
 lisp/simple.el | 41 ++++++++++++++++++++++++++++++++++-------
 2 files changed, 43 insertions(+), 7 deletions(-)

diff --git a/etc/NEWS b/etc/NEWS
index add438e8b6a..f01c6e2b3cd 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -129,6 +129,15 @@ Unix-words are words separated by whitespace regardless of the buffer's
 syntax table.  In a Unix terminal or shell, C-w kills by Unix-word.
 The new commands 'unix-word-rubout' and 'unix-filename-rubout' allow
 you to bind keys to operate more similarly to the terminal.
+
+---
+** New user option 'kill-word-dwim'.
+This option, if non-nil, modifies the fall-back behaviour of
+'kill-region' if no region is active, and will kill the last word
+instead of raising an error.  Note that if you have disabled Transient
+Mark mode you might prefer to use 'unix-word-rubout', as this feature
+relies on there being an active region.
+
 \f
 * Changes in Specialized Modes and Packages in Emacs 31.1
 
diff --git a/lisp/simple.el b/lisp/simple.el
index bbb13c1b471..f05ded36c22 100644
--- a/lisp/simple.el
+++ b/lisp/simple.el
@@ -5817,6 +5817,19 @@ 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 the behavior `kill-region' will not
+change.  If set to `emacs-word', then kill 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 like with
+`unix-word-rubout'."
+  :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 "31.1")
+
 (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 +5856,35 @@ 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)
+                   (cond
+                    ((and kill-region-dwim (not (use-region-p)))
+                     (list beg end kill-region-dwim))
+                    ((not (or beg end))
                      (user-error "The mark is not set now, so there is no region"))
-                   (list beg end 'region))))
+                    ((list beg end 'region))))))
+
   (condition-case nil
-      (let ((string (if region
-                        (funcall region-extract-function 'delete)
-                      (filter-buffer-substring beg end 'delete))))
+      (let ((string (cond
+                     ((memq region '(unix-word emacs-word))
+                      (let ((end (point)))
+                        (save-excursion
+                          (if (eq region 'emacs-word)
+                              (forward-word -1)
+                            (forward-unix-word -1))
+                          (filter-buffer-substring (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.45.2


[-- Attachment #3: Type: text/plain, Size: 113 bytes --]


And I think that you can already push your change, and I can follow on
later.

-- 
	Philip Kaludercic on siskin

  reply	other threads:[~2024-09-14 13:39 UTC|newest]

Thread overview: 90+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-13  9:55 bug#69097: [PATCH] Add 'kill-region-or-word' command Philip Kaludercic
2024-02-17  3:53 ` Richard Stallman
     [not found]   ` <87ttm7gi9i.fsf@posteo.net>
2024-02-19  3:44     ` Richard Stallman
     [not found]       ` <87sf1obkw9.fsf@posteo.net>
2024-02-23  3:04         ` Richard Stallman
     [not found]           ` <871q93rzv8.fsf@posteo.net>
2024-02-25  3:16             ` Richard Stallman
     [not found]               ` <87frxgn73g.fsf@posteo.net>
2024-02-27  3:12                 ` Richard Stallman
2024-05-03  7:37 ` Philip Kaludercic
2024-05-03 10:40   ` Eli Zaretskii
2024-05-03 10:48     ` Philip Kaludercic
2024-05-03 10:59       ` Eli Zaretskii
2024-05-03 11:04         ` Eli Zaretskii
2024-05-03 17:32           ` Philip Kaludercic
2024-05-03 18:01             ` Eli Zaretskii
2024-05-03 19:41               ` Philip Kaludercic
2024-05-04  6:20                 ` Eli Zaretskii
2024-05-05  6:53                 ` Juri Linkov
2024-05-05  9:04                   ` Eli Zaretskii
2024-05-05 16:29                     ` Juri Linkov
2024-05-05 16:54                       ` Philip Kaludercic
2024-05-05 16:59                         ` Juri Linkov
2024-05-05 17:08                           ` Drew Adams via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-05-05 18:27                           ` Philip Kaludercic
2024-05-05 17:05                         ` Drew Adams via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-05-05 17:13                           ` Eli Zaretskii
2024-05-05 17:53                             ` Drew Adams via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-05-06  0:21                       ` Sean Whitton via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-05-06 16:46                     ` Sean Whitton via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-05-06 16:51                       ` Sean Whitton via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-05-06 17:55                         ` Eli Zaretskii
2024-05-07  8:47                           ` Sean Whitton via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-05-07  8:47                             ` Sean Whitton via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-09-01 20:06                           ` Philip Kaludercic
2024-09-02  6:36                             ` Sean Whitton
2024-09-02 11:18                             ` Eli Zaretskii
2024-09-02 18:30                               ` Stefan Kangas
2024-09-02 18:45                                 ` Sean Whitton
2024-09-02 20:39                               ` Philip Kaludercic
2024-09-02 20:42                                 ` Sean Whitton
2024-09-02 20:45                                   ` Philip Kaludercic
2024-09-02 20:46                                   ` Sean Whitton
2024-09-02 21:12                                     ` Philip Kaludercic
2024-09-03 12:21                                       ` Eli Zaretskii
2024-09-03 13:53                                         ` Robert Pluim
2024-09-03 14:27                                           ` Eli Zaretskii
2024-09-03 14:55                                             ` Robert Pluim
2024-09-03 15:32                                               ` Eli Zaretskii
2024-09-03 16:32                                         ` Philip Kaludercic
2024-09-03 16:47                                           ` Robert Pluim
2024-09-04 11:08                                           ` Eli Zaretskii
2024-09-04 14:07                                           ` Sean Whitton
2024-09-04 14:21                                             ` Eli Zaretskii
2024-09-05  9:39                                             ` Philip Kaludercic
2024-09-05  9:52                                               ` Eli Zaretskii
2024-09-05 10:02                                                 ` Philip Kaludercic
2024-09-05 10:19                                                   ` Eli Zaretskii
2024-09-05 10:23                                                     ` Philip Kaludercic
2024-09-05 13:27                                               ` Sean Whitton
2024-09-05 14:38                                                 ` Philip Kaludercic
2024-09-06 10:36                                                   ` Sean Whitton
2024-09-06 11:06                                                     ` Sean Whitton
2024-09-06 11:30                                                     ` Eli Zaretskii
2024-09-06 13:54                                                       ` Sean Whitton
2024-09-06 16:32                                                         ` Philip Kaludercic
2024-09-07  9:52                                                         ` Eli Zaretskii
2024-09-07 10:10                                                           ` Philip Kaludercic
2024-09-07 21:08                                                           ` Sean Whitton
2024-09-07 21:17                                                             ` Sean Whitton
2024-09-09 17:54                                                               ` Sean Whitton
2024-09-09 19:03                                                                 ` Eli Zaretskii
2024-09-09 19:48                                                                   ` Sean Whitton
2024-09-09 19:23                                                                 ` Philip Kaludercic
2024-09-14  9:12                                                                   ` Eli Zaretskii
2024-09-14 11:27                                                                     ` Sean Whitton
2024-09-14 13:39                                                                       ` Philip Kaludercic [this message]
2024-09-14 14:05                                                                         ` Eli Zaretskii
2024-09-14 14:13                                                                           ` Philip Kaludercic
2024-09-14 14:24                                                                             ` Eli Zaretskii
2024-09-14 16:10                                                                             ` Sean Whitton
2024-09-14 16:26                                                                               ` German Pacenza
2024-09-14 16:36                                                                                 ` Sean Whitton
2024-09-14 21:19                                                                               ` Philip Kaludercic
2024-09-14 21:22                                                                                 ` Sean Whitton
2024-09-17 17:58                                                                             ` Juri Linkov
2024-09-18 16:08                                                                               ` Juri Linkov
2024-09-19  7:19                                                                               ` Philip Kaludercic
2024-09-20  6:50                                                                                 ` Juri Linkov
2024-09-20 16:57                                                                                   ` Philip Kaludercic
2024-09-08 10:38                                                             ` Philip Kaludercic
2024-05-05 16:47                   ` Drew Adams via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-05-03 16:20         ` Philip Kaludercic

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=87ttei2w2o.fsf@posteo.net \
    --to=philipk@posteo.net \
    --cc=69097@debbugs.gnu.org \
    --cc=acorallo@gnu.org \
    --cc=eliz@gnu.org \
    --cc=juri@linkov.net \
    --cc=rms@gnu.org \
    --cc=spwhitton@spwhitton.name \
    --cc=stefankangas@gmail.com \
    /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).