all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* bug#55692: 29.0.50; delete-selection-mode: Replace region only if set using the mouse
@ 2022-05-28 17:33 Visuwesh
  2022-05-28 19:10 ` Eli Zaretskii
  0 siblings, 1 reply; 13+ messages in thread
From: Visuwesh @ 2022-05-28 17:33 UTC (permalink / raw)
  To: 55692

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

Tags: patch

Motivation: I often find myself annoyed by `delete-selection-mode' when
using the keyboard since the region in Emacs does not translate to text
selections in other applications.  On the contrary, I find myself
wanting the behaviour of `delete-selection-mode' for regions set using
the mouse.  Attached patch adds an user option to do that.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-delete-selection-mode-Add-user-option-to-delete-mous.patch --]
[-- Type: text/x-patch, Size: 2062 bytes --]

From 3db630457b46bb25514a8041eeb80f35e73ef40c Mon Sep 17 00:00:00 2001
From: Visuwesh <visuweshm@gmail.com>
Date: Sat, 28 May 2022 22:52:19 +0530
Subject: [PATCH] delete-selection-mode: Add user option to delete mouse
 regions only

* lisp/delsel.el (delete-selection-mouse-selection-only): Add new user option.
(delete-selection-pre-hook): Respect it.
* etc/NEWS: Announce the new user option.
---
 etc/NEWS       | 4 ++++
 lisp/delsel.el | 9 +++++++++
 2 files changed, 13 insertions(+)

diff --git a/etc/NEWS b/etc/NEWS
index 3065fa85d3..100be767b3 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -265,6 +265,10 @@ startup.  Previously, these functions ignored
 * Changes in Emacs 29.1
 
 +++
+** New user option 'delete-selection-mouse-selection-only'.
+When non-nil, 'delete-selection-mode' will only delete the active
+region if it was set using the mouse.
+
 ** New user option 'switch-to-prev-buffer-skip-regexp'.
 This should be a regexp or a list of regexps; buffers whose names
 match those regexps will be ignored by 'switch-to-prev-buffer' and
diff --git a/lisp/delsel.el b/lisp/delsel.el
index f5fe7cf793..a011be9ede 100644
--- a/lisp/delsel.el
+++ b/lisp/delsel.el
@@ -64,6 +64,12 @@ delete-selection-save-to-register
   "If non-nil, deleted region text is stored in this register.
 Value must be the register (key) to use.")
 
+(defcustom delete-selection-mouse-selection-only nil
+  "If non-nil, delete the region only if set using the mouse."
+  :version "29.1"
+  :group 'editing-basics
+  :type 'boolean)
+
 ;;;###autoload
 (defalias 'pending-delete-mode 'delete-selection-mode)
 
@@ -251,6 +257,9 @@ delete-selection-pre-hook
 have this property won't delete the selection.
 See `delete-selection-helper'."
   (when (and delete-selection-mode (use-region-p)
+             (if delete-selection-mouse-selection-only
+                 (mouse-region-match)
+               t)
 	     (not buffer-read-only))
     (delete-selection-helper (and (symbolp this-command)
                                   (get this-command 'delete-selection)))))
-- 
2.33.1


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

* bug#55692: 29.0.50; delete-selection-mode: Replace region only if set using the mouse
  2022-05-28 17:33 bug#55692: 29.0.50; delete-selection-mode: Replace region only if set using the mouse Visuwesh
@ 2022-05-28 19:10 ` Eli Zaretskii
  2022-05-29  6:17   ` Visuwesh
  0 siblings, 1 reply; 13+ messages in thread
From: Eli Zaretskii @ 2022-05-28 19:10 UTC (permalink / raw)
  To: Visuwesh; +Cc: 55692

> From: Visuwesh <visuweshm@gmail.com>
> Date: Sat, 28 May 2022 23:03:51 +0530
> 
> Motivation: I often find myself annoyed by `delete-selection-mode' when
> using the keyboard since the region in Emacs does not translate to text
> selections in other applications.  On the contrary, I find myself
> wanting the behaviour of `delete-selection-mode' for regions set using
> the mouse.  Attached patch adds an user option to do that.

IMO, we shouldn't distinguish between the mouse and shift-selection
methods of defining the region.  So this feature, if accepted, should
also treat both methods of defining the region the same.





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

* bug#55692: 29.0.50; delete-selection-mode: Replace region only if set using the mouse
  2022-05-28 19:10 ` Eli Zaretskii
@ 2022-05-29  6:17   ` Visuwesh
  2022-05-29  6:52     ` Eli Zaretskii
  0 siblings, 1 reply; 13+ messages in thread
From: Visuwesh @ 2022-05-29  6:17 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: 55692

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

[சனி மே 28, 2022] Eli Zaretskii wrote:

>> From: Visuwesh <visuweshm@gmail.com>
>> Date: Sat, 28 May 2022 23:03:51 +0530
>> 
>> Motivation: I often find myself annoyed by `delete-selection-mode' when
>> using the keyboard since the region in Emacs does not translate to text
>> selections in other applications.  On the contrary, I find myself
>> wanting the behaviour of `delete-selection-mode' for regions set using
>> the mouse.  Attached patch adds an user option to do that.
>
> IMO, we shouldn't distinguish between the mouse and shift-selection
> methods of defining the region.  So this feature, if accepted, should
> also treat both methods of defining the region the same.

Considering shift-selection would be convenient as well.  Please check
attached patch.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-delete-selection-mode-Add-user-option-to-delete-temp.patch --]
[-- Type: text/x-patch, Size: 3412 bytes --]

From 49e09eadf09b1848ce3dbe109e557384c99ce94d Mon Sep 17 00:00:00 2001
From: Visuwesh <visuweshm@gmail.com>
Date: Sun, 29 May 2022 11:45:19 +0530
Subject: [PATCH] delete-selection-mode: Add user option to delete temporary
 regions only

* lisp/delsel.el (delete-selection-temporary-regions-only): Add new
user option.
(delete-selection-pre-hook): Respect it.
* doc/emacs/mark.texi (Using Region): Document the new user option.
* etc/NEWS: Announce the new user option. (bug#55692)
---
 doc/emacs/mark.texi |  7 +++++--
 etc/NEWS            |  5 +++++
 lisp/delsel.el      | 12 ++++++++++++
 3 files changed, 22 insertions(+), 2 deletions(-)

diff --git a/doc/emacs/mark.texi b/doc/emacs/mark.texi
index 91c44d527b..1b53c2c478 100644
--- a/doc/emacs/mark.texi
+++ b/doc/emacs/mark.texi
@@ -291,13 +291,16 @@ Using Region
 @cindex Delete Selection mode
 @cindex mode, Delete Selection
 @findex delete-selection-mode
+@vindex delete-selection-temporary-regions-only
   By default, text insertion occurs normally even if the mark is
 active---for example, typing @kbd{a} inserts the character @samp{a},
 then deactivates the mark.  Delete Selection mode, a minor mode,
 modifies this behavior: if you enable that mode, then inserting text
 while the mark is active causes the text in the region to be deleted
-first.  To toggle Delete Selection mode on or off, type @kbd{M-x
-delete-selection-mode}.
+first.  If you want to replace only temporary regions, set by
+mouse-dragging or shift-selection, then change the variable
+@code{delete-selection-temporary-regions-only} to @code{t}.  To toggle
+Delete Selection mode on or off, type @kbd{M-x delete-selection-mode}.

 @node Mark Ring
 @section The Mark Ring
diff --git a/etc/NEWS b/etc/NEWS
index 85a0ee44b9..3016c62db0 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -271,6 +271,11 @@ startup.  Previously, these functions ignored
 \f
 * Changes in Emacs 29.1

++++
+** New user option 'delete-selection-temporary-regions-only'.
+When non-nil, 'delete-selection-mode' will only delete the temporary
+regions (usually set by mouse-dragging or shift-selection).
+
 +++
 ** New user option 'switch-to-prev-buffer-skip-regexp'.
 This should be a regexp or a list of regexps; buffers whose names
diff --git a/lisp/delsel.el b/lisp/delsel.el
index f5fe7cf793..7ee41ff2a7 100644
--- a/lisp/delsel.el
+++ b/lisp/delsel.el
@@ -64,6 +64,14 @@ delete-selection-save-to-register
   "If non-nil, deleted region text is stored in this register.
 Value must be the register (key) to use.")

+(defcustom delete-selection-temporary-regions-only nil
+  "Whether to delete only temporary regions.
+When non-nil, typed text only replaces temporary regions (usually
+set by mouse-dragging or shift-selection)."
+  :version "29.1"
+  :group 'editing-basics
+  :type 'boolean)
+
 ;;;###autoload
 (defalias 'pending-delete-mode 'delete-selection-mode)

@@ -251,6 +259,10 @@ delete-selection-pre-hook
 have this property won't delete the selection.
 See `delete-selection-helper'."
   (when (and delete-selection-mode (use-region-p)
+             (if delete-selection-temporary-regions-only
+                 (and (consp transient-mark-mode)
+                      (eq (car transient-mark-mode) 'only))
+               t)
 	     (not buffer-read-only))
     (delete-selection-helper (and (symbolp this-command)
                                   (get this-command 'delete-selection)))))
--
2.33.1

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

* bug#55692: 29.0.50; delete-selection-mode: Replace region only if set using the mouse
  2022-05-29  6:17   ` Visuwesh
@ 2022-05-29  6:52     ` Eli Zaretskii
  2022-05-29  8:10       ` Visuwesh
  0 siblings, 1 reply; 13+ messages in thread
From: Eli Zaretskii @ 2022-05-29  6:52 UTC (permalink / raw)
  To: Visuwesh; +Cc: 55692

> From: Visuwesh <visuweshm@gmail.com>
> Cc: 55692@debbugs.gnu.org
> Date: Sun, 29 May 2022 11:47:32 +0530
> 
> [சனி மே 28, 2022] Eli Zaretskii wrote:
> 
> > IMO, we shouldn't distinguish between the mouse and shift-selection
> > methods of defining the region.  So this feature, if accepted, should
> > also treat both methods of defining the region the same.
> 
> Considering shift-selection would be convenient as well.  Please check
> attached patch.

Thanks.

> +@vindex delete-selection-temporary-regions-only

I'd drop the "-only" part: it is not necessary, and makes the variable
name longer.

>    By default, text insertion occurs normally even if the mark is
>  active---for example, typing @kbd{a} inserts the character @samp{a},
>  then deactivates the mark.  Delete Selection mode, a minor mode,
>  modifies this behavior: if you enable that mode, then inserting text
>  while the mark is active causes the text in the region to be deleted
> -first.  To toggle Delete Selection mode on or off, type @kbd{M-x
> -delete-selection-mode}.
> +first.  If you want to replace only temporary regions, set by
> +mouse-dragging or shift-selection, then change the variable

There should be cross-references here to where these methods are
described in the manual.

> @@ -251,6 +259,10 @@ delete-selection-pre-hook
>  have this property won't delete the selection.
>  See `delete-selection-helper'."
>    (when (and delete-selection-mode (use-region-p)
> +             (if delete-selection-temporary-regions-only
> +                 (and (consp transient-mark-mode)
> +                      (eq (car transient-mark-mode) 'only))
> +               t)

I wonder whether we should also optionally allow replacing text in
regions activated by "C-u C-x C-x" when transient-mark-mode is off.
Those cause transient-mark-mode to have the value 'lambda', not
'(only...)'.  So maybe the new defcustom should be a tristate, not a
boolean?

Also, can we have the above condition in a more elegant form?  In
general, whenever you have something like

   (if SOMETHING foo t)

it begs to be rewritten so the "t" part is not needed, because 'if'
itself already returns a boolean value.





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

* bug#55692: 29.0.50; delete-selection-mode: Replace region only if set using the mouse
  2022-05-29  6:52     ` Eli Zaretskii
@ 2022-05-29  8:10       ` Visuwesh
  2022-05-29  9:04         ` Eli Zaretskii
  0 siblings, 1 reply; 13+ messages in thread
From: Visuwesh @ 2022-05-29  8:10 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: 55692

[ஞாயிறு மே 29, 2022] Eli Zaretskii wrote:

>> From: Visuwesh <visuweshm@gmail.com>
>> Cc: 55692@debbugs.gnu.org
>> Date: Sun, 29 May 2022 11:47:32 +0530
>> 
>> [சனி மே 28, 2022] Eli Zaretskii wrote:
>> 
>> > IMO, we shouldn't distinguish between the mouse and shift-selection
>> > methods of defining the region.  So this feature, if accepted, should
>> > also treat both methods of defining the region the same.
>> 
>> Considering shift-selection would be convenient as well.  Please check
>> attached patch.
>
> Thanks.
>
>> +@vindex delete-selection-temporary-regions-only
>
> I'd drop the "-only" part: it is not necessary, and makes the variable
> name longer.
>

Okay, will do.

>>    By default, text insertion occurs normally even if the mark is
>>  active---for example, typing @kbd{a} inserts the character @samp{a},
>>  then deactivates the mark.  Delete Selection mode, a minor mode,
>>  modifies this behavior: if you enable that mode, then inserting text
>>  while the mark is active causes the text in the region to be deleted
>> -first.  To toggle Delete Selection mode on or off, type @kbd{M-x
>> -delete-selection-mode}.
>> +first.  If you want to replace only temporary regions, set by
>> +mouse-dragging or shift-selection, then change the variable
>
> There should be cross-references here to where these methods are
> described in the manual.
>

These methods as in "mouse-dragging" or "shift-selection".  I'm afraid I
don't fully understand what you mean.

>> @@ -251,6 +259,10 @@ delete-selection-pre-hook
>>  have this property won't delete the selection.
>>  See `delete-selection-helper'."
>>    (when (and delete-selection-mode (use-region-p)
>> +             (if delete-selection-temporary-regions-only
>> +                 (and (consp transient-mark-mode)
>> +                      (eq (car transient-mark-mode) 'only))
>> +               t)
>
> I wonder whether we should also optionally allow replacing text in
> regions activated by "C-u C-x C-x" when transient-mark-mode is off.
> Those cause transient-mark-mode to have the value 'lambda', not
> '(only...)'.  So maybe the new defcustom should be a tristate, not a
> boolean?
>

Sure, that could be useful.  But I have transient-mark-mode turned on
always so I can't comment much on it.  However, what would the third
state be?  When the defcustom is t, we could have it check for '(only
. ...)' and the value 'lambda', no?

> Also, can we have the above condition in a more elegant form?  In
> general, whenever you have something like
>
>    (if SOMETHING foo t)
>
> it begs to be rewritten so the "t" part is not needed, because 'if'
> itself already returns a boolean value.

Sure.  I will factor out the condition into a separate function.  Is
that more elegant?

I will send an updated patch a bit later.  Thanks.





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

* bug#55692: 29.0.50; delete-selection-mode: Replace region only if set using the mouse
  2022-05-29  8:10       ` Visuwesh
@ 2022-05-29  9:04         ` Eli Zaretskii
  2022-05-29 14:53           ` Visuwesh
  0 siblings, 1 reply; 13+ messages in thread
From: Eli Zaretskii @ 2022-05-29  9:04 UTC (permalink / raw)
  To: Visuwesh; +Cc: 55692

> From: Visuwesh <visuweshm@gmail.com>
> Cc: 55692@debbugs.gnu.org
> Date: Sun, 29 May 2022 13:40:20 +0530
> 
> >>    By default, text insertion occurs normally even if the mark is
> >>  active---for example, typing @kbd{a} inserts the character @samp{a},
> >>  then deactivates the mark.  Delete Selection mode, a minor mode,
> >>  modifies this behavior: if you enable that mode, then inserting text
> >>  while the mark is active causes the text in the region to be deleted
> >> -first.  To toggle Delete Selection mode on or off, type @kbd{M-x
> >> -delete-selection-mode}.
> >> +first.  If you want to replace only temporary regions, set by
> >> +mouse-dragging or shift-selection, then change the variable
> >
> > There should be cross-references here to where these methods are
> > described in the manual.
> >
> 
> These methods as in "mouse-dragging" or "shift-selection".  I'm afraid I
> don't fully understand what you mean.

I mean to add @xref or @pxref to the places in the manual where mouse
selection and shift-selection are described.  Those places are,
respectively, the nodes "Setting Mark" and "Shift Selection".

> >> @@ -251,6 +259,10 @@ delete-selection-pre-hook
> >>  have this property won't delete the selection.
> >>  See `delete-selection-helper'."
> >>    (when (and delete-selection-mode (use-region-p)
> >> +             (if delete-selection-temporary-regions-only
> >> +                 (and (consp transient-mark-mode)
> >> +                      (eq (car transient-mark-mode) 'only))
> >> +               t)
> >
> > I wonder whether we should also optionally allow replacing text in
> > regions activated by "C-u C-x C-x" when transient-mark-mode is off.
> > Those cause transient-mark-mode to have the value 'lambda', not
> > '(only...)'.  So maybe the new defcustom should be a tristate, not a
> > boolean?
> >
> 
> Sure, that could be useful.  But I have transient-mark-mode turned on
> always so I can't comment much on it.  However, what would the third
> state be?

Some symbol.

> When the defcustom is t, we could have it check for '(only . ...)'
> and the value 'lambda', no?

Yes, we could have:

  . nil - always replace the region
  . t   - replace region from mouse, shift-selection, and "C-u C-x C-x"
  . selection - only replace  region from mouse and shift-selection

> > Also, can we have the above condition in a more elegant form?  In
> > general, whenever you have something like
> >
> >    (if SOMETHING foo t)
> >
> > it begs to be rewritten so the "t" part is not needed, because 'if'
> > itself already returns a boolean value.
> 
> Sure.  I will factor out the condition into a separate function.  Is
> that more elegant?

No, that's overkill.  I meant that if you need an 'if' to return t or
something else, you could instead make it return nil or something
else, as in

   (if SOMETHING foo)

and reverse the condition.

But if you still don't understand, just forget this comment.





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

* bug#55692: 29.0.50; delete-selection-mode: Replace region only if set using the mouse
  2022-05-29  9:04         ` Eli Zaretskii
@ 2022-05-29 14:53           ` Visuwesh
  2022-05-29 17:10             ` Eli Zaretskii
  0 siblings, 1 reply; 13+ messages in thread
From: Visuwesh @ 2022-05-29 14:53 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: 55692

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

[ஞாயிறு மே 29, 2022] Eli Zaretskii wrote:

>> From: Visuwesh <visuweshm@gmail.com>
>> Cc: 55692@debbugs.gnu.org
>> Date: Sun, 29 May 2022 13:40:20 +0530
>> 
>> >>    By default, text insertion occurs normally even if the mark is
>> >>  active---for example, typing @kbd{a} inserts the character @samp{a},
>> >>  then deactivates the mark.  Delete Selection mode, a minor mode,
>> >>  modifies this behavior: if you enable that mode, then inserting text
>> >>  while the mark is active causes the text in the region to be deleted
>> >> -first.  To toggle Delete Selection mode on or off, type @kbd{M-x
>> >> -delete-selection-mode}.
>> >> +first.  If you want to replace only temporary regions, set by
>> >> +mouse-dragging or shift-selection, then change the variable
>> >
>> > There should be cross-references here to where these methods are
>> > described in the manual.
>> >
>> 
>> These methods as in "mouse-dragging" or "shift-selection".  I'm afraid I
>> don't fully understand what you mean.
>
> I mean to add @xref or @pxref to the places in the manual where mouse
> selection and shift-selection are described.  Those places are,
> respectively, the nodes "Setting Mark" and "Shift Selection".
>

Done.

>> >> @@ -251,6 +259,10 @@ delete-selection-pre-hook
>> >>  have this property won't delete the selection.
>> >>  See `delete-selection-helper'."
>> >>    (when (and delete-selection-mode (use-region-p)
>> >> +             (if delete-selection-temporary-regions-only
>> >> +                 (and (consp transient-mark-mode)
>> >> +                      (eq (car transient-mark-mode) 'only))
>> >> +               t)
>> >
>> > I wonder whether we should also optionally allow replacing text in
>> > regions activated by "C-u C-x C-x" when transient-mark-mode is off.
>> > Those cause transient-mark-mode to have the value 'lambda', not
>> > '(only...)'.  So maybe the new defcustom should be a tristate, not a
>> > boolean?
>> >
>> 
>> Sure, that could be useful.  But I have transient-mark-mode turned on
>> always so I can't comment much on it.  However, what would the third
>> state be?
>
> Some symbol.
>
>> When the defcustom is t, we could have it check for '(only . ...)'
>> and the value 'lambda', no?
>
> Yes, we could have:
>
>   . nil - always replace the region
>   . t   - replace region from mouse, shift-selection, and "C-u C-x C-x"
>   . selection - only replace  region from mouse and shift-selection
>

Done.

>> > Also, can we have the above condition in a more elegant form?  In
>> > general, whenever you have something like
>> >
>> >    (if SOMETHING foo t)
>> >
>> > it begs to be rewritten so the "t" part is not needed, because 'if'
>> > itself already returns a boolean value.
>> 
>> Sure.  I will factor out the condition into a separate function.  Is
>> that more elegant?
>
> No, that's overkill.  I meant that if you need an 'if' to return t or
> something else, you could instead make it return nil or something
> else, as in
>
>    (if SOMETHING foo)
>
> and reverse the condition.
>
> But if you still don't understand, just forget this comment.

I don't think I fully understood what you meant but I took a shot at it
anyway.  Please review updated patch.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-delete-selection-mode-Add-user-option-to-delete-temp.patch --]
[-- Type: text/x-patch, Size: 4202 bytes --]

From 4649ff74c384fa22edb19234928df6322efd6099 Mon Sep 17 00:00:00 2001
From: Visuwesh <visuweshm@gmail.com>
Date: Sun, 29 May 2022 11:45:19 +0530
Subject: [PATCH] delete-selection-mode: Add user option to delete temporary
 regions only

* lisp/delsel.el (delete-selection-temporary-region): Add new
user option.
(delete-selection-pre-hook): Respect it.
* doc/emacs/mark.texi (Using Region): Document the new user option.
* etc/NEWS: Announce the new user option. (bug#55692)
---
 doc/emacs/mark.texi | 11 +++++++++--
 etc/NEWS            |  5 +++++
 lisp/delsel.el      | 18 ++++++++++++++++++
 3 files changed, 32 insertions(+), 2 deletions(-)

diff --git a/doc/emacs/mark.texi b/doc/emacs/mark.texi
index 91c44d527b..397f941b2d 100644
--- a/doc/emacs/mark.texi
+++ b/doc/emacs/mark.texi
@@ -291,13 +291,20 @@ Using Region
 @cindex Delete Selection mode
 @cindex mode, Delete Selection
 @findex delete-selection-mode
+@vindex delete-selection-temporary-region
   By default, text insertion occurs normally even if the mark is
 active---for example, typing @kbd{a} inserts the character @samp{a},
 then deactivates the mark.  Delete Selection mode, a minor mode,
 modifies this behavior: if you enable that mode, then inserting text
 while the mark is active causes the text in the region to be deleted
-first.  To toggle Delete Selection mode on or off, type @kbd{M-x
-delete-selection-mode}.
+first.  If you want to replace only temporary regions, set by
+mouse-dragging or shift-selection (@pxref{Setting Mark}) or @kbd{C-u
+C-x C-x}, then change the variable
+@code{delete-selection-temporary-region} to @code{t}.  But if you do
+not want to consider the region made active by @kbd{C-u C-x C-x}
+(@pxref{Disabled Transient Mark}), then set the variable to
+@code{selection}.  To toggle Delete Selection mode on or off, type
+@kbd{M-x delete-selection-mode}.
 
 @node Mark Ring
 @section The Mark Ring
diff --git a/etc/NEWS b/etc/NEWS
index 85a0ee44b9..43a0e1aaab 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -271,6 +271,11 @@ startup.  Previously, these functions ignored
 \f
 * Changes in Emacs 29.1
 
++++
+** New user option 'delete-selection-temporary-region'.
+When non-nil, 'delete-selection-mode' will only delete the temporary
+regions (usually set by mouse-dragging or shift-selection).
+
 +++
 ** New user option 'switch-to-prev-buffer-skip-regexp'.
 This should be a regexp or a list of regexps; buffers whose names
diff --git a/lisp/delsel.el b/lisp/delsel.el
index f5fe7cf793..8691f4720b 100644
--- a/lisp/delsel.el
+++ b/lisp/delsel.el
@@ -64,6 +64,19 @@ delete-selection-save-to-register
   "If non-nil, deleted region text is stored in this register.
 Value must be the register (key) to use.")
 
+(defcustom delete-selection-temporary-region nil
+  "Whether to delete only temporary regions.
+When non-nil, typed text replaces only the regions set by
+mouse-dragging, shift-selection, and \"\\[universal-argument] \\[exchange-point-and-mark]\" when
+`transient-mark-mode' is turned off.  If the value is the symbol
+`selection', then replace only the regions set by mouse-dragging
+and shift-selection."
+  :version "29.1"
+  :group 'editing-basics
+  :type '(choice (const :tag "Replace all regions" nil)
+                 (const :tag "Replace region from mouse, shift-selection, and \"C-u C-x C-x\"" t)
+                 (const :tag "Replace region from mouse and shift-selection" selection)))
+
 ;;;###autoload
 (defalias 'pending-delete-mode 'delete-selection-mode)
 
@@ -251,6 +264,11 @@ delete-selection-pre-hook
 have this property won't delete the selection.
 See `delete-selection-helper'."
   (when (and delete-selection-mode (use-region-p)
+             (not (when delete-selection-temporary-region
+                    (not (or (and (consp transient-mark-mode)
+                                  (eq (car transient-mark-mode) 'only))
+                             (and (not (eq delete-selection-temporary-region 'selection))
+                                  (eq transient-mark-mode 'lambda))))))
 	     (not buffer-read-only))
     (delete-selection-helper (and (symbolp this-command)
                                   (get this-command 'delete-selection)))))
-- 
2.33.1


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

* bug#55692: 29.0.50; delete-selection-mode: Replace region only if set using the mouse
  2022-05-29 14:53           ` Visuwesh
@ 2022-05-29 17:10             ` Eli Zaretskii
  2022-05-31 10:44               ` Visuwesh
  0 siblings, 1 reply; 13+ messages in thread
From: Eli Zaretskii @ 2022-05-29 17:10 UTC (permalink / raw)
  To: Visuwesh; +Cc: 55692

> From: Visuwesh <visuweshm@gmail.com>
> Cc: 55692@debbugs.gnu.org
> Date: Sun, 29 May 2022 20:23:49 +0530
> 
> >> Sure.  I will factor out the condition into a separate function.  Is
> >> that more elegant?
> >
> > No, that's overkill.  I meant that if you need an 'if' to return t or
> > something else, you could instead make it return nil or something
> > else, as in
> >
> >    (if SOMETHING foo)
> >
> > and reverse the condition.
> >
> > But if you still don't understand, just forget this comment.
> 
> I don't think I fully understood what you meant but I took a shot at it
> anyway.  Please review updated patch.

Well, that uses double negation, which IME is a sign that it can be
clarified.  Is the following condition correct?

  (when (and delete-selection-mode
             (use-region-p)
	     (not buffer-read-only)
	     (or (null delete-selection-temporary-region)
		 (and (eq delete-selection-temporary-region 'selection)
		      (consp transient-mark-mode)
		      (eq (car transient-mark-mode) 'only))
	         (and delete-selection-temporary-region
	              (or (eq transient-mark-mode 'lambda)
		          (and (consp transient-mark-mode)
		               (eq (car transient-mark-mode) 'only))))))

If not, why not?

> -first.  To toggle Delete Selection mode on or off, type @kbd{M-x
> -delete-selection-mode}.
> +first.  If you want to replace only temporary regions, set by
> +mouse-dragging or shift-selection (@pxref{Setting Mark}) or @kbd{C-u
> +C-x C-x}, then change the variable
> +@code{delete-selection-temporary-region} to @code{t}.  But if you do
> +not want to consider the region made active by @kbd{C-u C-x C-x}
> +(@pxref{Disabled Transient Mark}), then set the variable to
> +@code{selection}.  To toggle Delete Selection mode on or off, type
> +@kbd{M-x delete-selection-mode}.

This text is now too complex and thus confusing.  (You can only keep
using that style for boolean options.)  I would rephrase:

  Normally, inserting text deletes any active region, replacing it
  with the text you insert.  However, you can tune this behavior by
  customizing the @code{delete-selection-temporary-region} option.
  Its default value is @code{nil}, but you can set it to @code{t}, in
  which case only temporarily-active regions will be replaced: those
  which are set by dragging the mouse (@pxref{Setting Mark}) or by
  shift-selection (@pxref{Shift Selection}), as well as by @kbd{C-u
  C-x C-x} when Transient Mark mode is disabled.  You can further tune
  the behavior by setting @code{delete-selection-temporary-region} to
  @code{selection}: then temporary regions activated by @kbd{C-u C-x
  C-x} won't be replaced, only the ones activated by dragging the
  mouse or shift-selection.

OK?





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

* bug#55692: 29.0.50; delete-selection-mode: Replace region only if set using the mouse
  2022-05-29 17:10             ` Eli Zaretskii
@ 2022-05-31 10:44               ` Visuwesh
  2022-05-31 14:33                 ` Drew Adams
  2022-06-01 16:04                 ` Eli Zaretskii
  0 siblings, 2 replies; 13+ messages in thread
From: Visuwesh @ 2022-05-31 10:44 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: 55692

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

[ஞாயிறு மே 29, 2022] Eli Zaretskii wrote:

>> From: Visuwesh <visuweshm@gmail.com>
>> Cc: 55692@debbugs.gnu.org
>> Date: Sun, 29 May 2022 20:23:49 +0530
>> 
>> >> Sure.  I will factor out the condition into a separate function.  Is
>> >> that more elegant?
>> >
>> > No, that's overkill.  I meant that if you need an 'if' to return t or
>> > something else, you could instead make it return nil or something
>> > else, as in
>> >
>> >    (if SOMETHING foo)
>> >
>> > and reverse the condition.
>> >
>> > But if you still don't understand, just forget this comment.
>> 
>> I don't think I fully understood what you meant but I took a shot at it
>> anyway.  Please review updated patch.
>
> Well, that uses double negation, which IME is a sign that it can be
> clarified.  Is the following condition correct?
>
> [...]
>
> If not, why not?
>

For some reason, it completely crossed my mind that I could check for
the null value of delete-selection-temporary-region explicitly, thanks
for pointing out.  The condition is now,

      (when (and delete-selection-mode (use-region-p)
                 (not buffer-read-only)
                 (or (null delete-selection-temporary-region)
                     (and delete-selection-temporary-region
                          (consp transient-mark-mode)
                          (eq (car transient-mark-mode) 'only))
                     (and (not (eq delete-selection-temporary-region 'selection))
                          (eq transient-mark-mode 'lambda))))


>> -first.  To toggle Delete Selection mode on or off, type @kbd{M-x
>> -delete-selection-mode}.
>> +first.  If you want to replace only temporary regions, set by
>> +mouse-dragging or shift-selection (@pxref{Setting Mark}) or @kbd{C-u
>> +C-x C-x}, then change the variable
>> +@code{delete-selection-temporary-region} to @code{t}.  But if you do
>> +not want to consider the region made active by @kbd{C-u C-x C-x}
>> +(@pxref{Disabled Transient Mark}), then set the variable to
>> +@code{selection}.  To toggle Delete Selection mode on or off, type
>> +@kbd{M-x delete-selection-mode}.
>
> This text is now too complex and thus confusing.  (You can only keep
> using that style for boolean options.)  I would rephrase:
>
>   Normally, inserting text deletes any active region, replacing it
>   with the text you insert.  However, you can tune this behavior by
>   customizing the @code{delete-selection-temporary-region} option.
>   Its default value is @code{nil}, but you can set it to @code{t}, in
>   which case only temporarily-active regions will be replaced: those
>   which are set by dragging the mouse (@pxref{Setting Mark}) or by
>   shift-selection (@pxref{Shift Selection}), as well as by @kbd{C-u
>   C-x C-x} when Transient Mark mode is disabled.  You can further tune
>   the behavior by setting @code{delete-selection-temporary-region} to
>   @code{selection}: then temporary regions activated by @kbd{C-u C-x
>   C-x} won't be replaced, only the ones activated by dragging the
>   mouse or shift-selection.
>
> OK?

Indeed, that is more clear.  Thanks for teaching.

The paragraph now says,

       By default, text insertion occurs normally even if the mark is
    active—for example, typing ‘a’ inserts the character ‘a’, then
    deactivates the mark.  Delete Selection mode, a minor mode, modifies
    this behavior: if you enable that mode, then inserting text while the
    mark is active causes the text in the region to be deleted first.
    However, you can tune this behavior by customizing the
    ‘delete-selection-temporary-region’ option.  Its default value is ‘nil’,
    but you can set it to ‘t’, in which case only temporarily-active regions
    will be replaced: those which are set by dragging the mouse (*note
    Setting Mark) or by shift-selection (*note Shift Selection), as well
    as by ‘C-u C-x C-x’ when Transient Mark Mode is disabled.  You can
    further tune the behavior by setting ‘delete-selection-temporary-region’
    to ‘selection’: then temporary regions by ‘C-u C-x C-x’ won’t be
    replaced, only the ones activated by dragging the mouse or
    shift-selection.  To toggle Delete Selection mode on or off, type ‘M-x
    delete-selection-mode’.

Is this fine?

Improved patch attached.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-delete-selection-mode-Add-user-option-to-delete-temp.patch --]
[-- Type: text/x-patch, Size: 4486 bytes --]

From 6bdf6dbf4525ef6db2e04897b4d6d23488ac2efe Mon Sep 17 00:00:00 2001
From: Visuwesh <visuweshm@gmail.com>
Date: Sun, 29 May 2022 11:45:19 +0530
Subject: [PATCH] delete-selection-mode: Add user option to delete temporary
 regions only

* lisp/delsel.el (delete-selection-temporary-region): Add new
user option.
(delete-selection-pre-hook): Respect it.
* doc/emacs/mark.texi (Using Region): Document the new user option.
* etc/NEWS: Announce the new user option. (bug#55692)
---
 doc/emacs/mark.texi | 13 ++++++++++++-
 etc/NEWS            |  5 +++++
 lisp/delsel.el      | 21 ++++++++++++++++++++-
 3 files changed, 37 insertions(+), 2 deletions(-)

diff --git a/doc/emacs/mark.texi b/doc/emacs/mark.texi
index 91c44d527b..ad25ed6a8a 100644
--- a/doc/emacs/mark.texi
+++ b/doc/emacs/mark.texi
@@ -291,12 +291,23 @@ Using Region
 @cindex Delete Selection mode
 @cindex mode, Delete Selection
 @findex delete-selection-mode
+@vindex delete-selection-temporary-region
   By default, text insertion occurs normally even if the mark is
 active---for example, typing @kbd{a} inserts the character @samp{a},
 then deactivates the mark.  Delete Selection mode, a minor mode,
 modifies this behavior: if you enable that mode, then inserting text
 while the mark is active causes the text in the region to be deleted
-first.  To toggle Delete Selection mode on or off, type @kbd{M-x
+first.  However, you can tune this behavior by customizing the
+@code{delete-selection-temporary-region} option.  Its default value is
+@code{nil}, but you can set it to @code{t}, in which case only
+temporarily-active regions will be replaced: those which are set by
+dragging the mouse (@pxref{Setting Mark}) or by shift-selection
+(@pxref{Shift Selection}), as well as by @kbd{C-u C-x C-x} when
+Transient Mark Mode is disabled.  You can further tune the behavior by
+setting @code{delete-selection-temporary-region} to @code{selection}:
+then temporary regions by @kbd{C-u C-x C-x} won't be replaced, only
+the ones activated by dragging the mouse or shift-selection.  To
+toggle Delete Selection mode on or off, type @kbd{M-x
 delete-selection-mode}.
 
 @node Mark Ring
diff --git a/etc/NEWS b/etc/NEWS
index 85a0ee44b9..43a0e1aaab 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -271,6 +271,11 @@ startup.  Previously, these functions ignored
 \f
 * Changes in Emacs 29.1
 
++++
+** New user option 'delete-selection-temporary-region'.
+When non-nil, 'delete-selection-mode' will only delete the temporary
+regions (usually set by mouse-dragging or shift-selection).
+
 +++
 ** New user option 'switch-to-prev-buffer-skip-regexp'.
 This should be a regexp or a list of regexps; buffers whose names
diff --git a/lisp/delsel.el b/lisp/delsel.el
index f5fe7cf793..c9d3cf269b 100644
--- a/lisp/delsel.el
+++ b/lisp/delsel.el
@@ -64,6 +64,19 @@ delete-selection-save-to-register
   "If non-nil, deleted region text is stored in this register.
 Value must be the register (key) to use.")
 
+(defcustom delete-selection-temporary-region nil
+  "Whether to delete only temporary regions.
+When non-nil, typed text replaces only the regions set by
+mouse-dragging, shift-selection, and \"\\[universal-argument] \\[exchange-point-and-mark]\" when
+`transient-mark-mode' is turned off.  If the value is the symbol
+`selection', then replace only the regions set by mouse-dragging
+and shift-selection."
+  :version "29.1"
+  :group 'editing-basics
+  :type '(choice (const :tag "Replace all regions" nil)
+                 (const :tag "Replace region from mouse, shift-selection, and \"C-u C-x C-x\"" t)
+                 (const :tag "Replace region from mouse and shift-selection" selection)))
+
 ;;;###autoload
 (defalias 'pending-delete-mode 'delete-selection-mode)
 
@@ -251,7 +264,13 @@ delete-selection-pre-hook
 have this property won't delete the selection.
 See `delete-selection-helper'."
   (when (and delete-selection-mode (use-region-p)
-	     (not buffer-read-only))
+	     (not buffer-read-only)
+             (or (null delete-selection-temporary-region)
+                 (and delete-selection-temporary-region
+                      (consp transient-mark-mode)
+                      (eq (car transient-mark-mode) 'only))
+                 (and (not (eq delete-selection-temporary-region 'selection))
+                      (eq transient-mark-mode 'lambda))))
     (delete-selection-helper (and (symbolp this-command)
                                   (get this-command 'delete-selection)))))
 
-- 
2.33.1


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

* bug#55692: 29.0.50; delete-selection-mode: Replace region only if set using the mouse
  2022-05-31 10:44               ` Visuwesh
@ 2022-05-31 14:33                 ` Drew Adams
  2022-06-01 16:04                 ` Eli Zaretskii
  1 sibling, 0 replies; 13+ messages in thread
From: Drew Adams @ 2022-05-31 14:33 UTC (permalink / raw)
  To: Visuwesh, Eli Zaretskii; +Cc: 55692@debbugs.gnu.org

This has long been the text in the manual: 

>        By default, text insertion occurs normally even if the mark is
>     active—for example, typing ‘a’ inserts the character ‘a’, then
>     deactivates the mark.  Delete Selection mode, a minor mode,
>     modifies
>     this behavior: if you enable that mode, then inserting text while
>     the mark is active causes the text in the region to be deleted first.

But this, or similar, is apparently being added now:

>     However, you can tune this behavior by customizing the
>     ‘delete-selection-temporary-region’ option.  Its default value is
>     ‘nil’, but you can set it to ‘t’, in which case only temporarily-active
>     regions
>     will be replaced: those which are set by dragging the mouse (*note
>     Setting Mark) or by shift-selection (*note Shift Selection), as
>     well as by ‘C-u C-x C-x’ when Transient Mark Mode is disabled.  You can
>     further tune the behavior by setting ‘delete-selection-temporary-
>     region’
>     to ‘selection’: then temporary regions by ‘C-u C-x C-x’ won’t be
>     replaced, only the ones activated by dragging the mouse or
>     shift-selection.  To toggle Delete Selection mode on or off, type
>     ‘M-x delete-selection-mode’.

FWIW, I disagree with this doc change.

If you're going to do something like that (let
alone say _more_ about this minor new feature than
about `delete-selection-mode' itself), then at
least tell users about the _real_ way to customize
d-s-mode - the part that's _basic_ to its design
and purpose.

I'm talking about the fact that d-s-mode does _not_
just replace selected text - even by _default_ for
some commands.

The design of d-s-mode - its features that are
useful for both end users and Lisp coders - isn't
even hinted at in the doc.

What is that design?  The mode behaves differently
for different commands.  Both by default and by
easy-peasy customization.

The doc says nothing about this, and yet it's so
simple, general, elegant, and powerful.  Just put
the kind of action you want on a command symbol
as property `delete-selection'.  There are basic,
predefined actions, and you can define any other
action you like.

From the delsel.el Commentary - which is the real
doc, IMO:

;; Commands which will delete the selection need a 'delete-selection
;; property on their symbols; commands which insert text but don't
;; have this property won't delete the selection.  It can be one of
;; the values:
;;
;;  `yank'
;;      For commands which do a yank; ensures the region about to be
;;      deleted isn't immediately yanked back, which would make the
;;      command a no-op.
;;  `supersede'
;;      Delete the active region and ignore the current command,
;;      i.e. the command will just delete the region.  This is for
;;      commands that normally delete small amounts of text, like
;;      a single character -- they will instead delete the whole
;;      active region.
;;  `kill'
;;      `kill-region' is used on the selection, rather than
;;      `delete-region'.  (Text selected with the mouse will typically
;;      be yankable anyhow.)
;;  t
;;      The normal case: delete the active region prior to executing
;;      the command which will insert replacement text.
;;  FUNCTION
;;      For commands which need to dynamically determine this behavior.
;;      FUNCTION should take no argument and return one of the above
;;      values, or nil.  In the latter case, FUNCTION should itself
;;      do with the active region whatever is appropriate."

Note the first sentence, BTW.  You don't get _any_
`delete-selection-mode' behavior at all for a
command unless it has property `delete-selection'.

Yes, most insertion of regular text falls into the
default case.  And yes, that's the main idea behind
the mode.  But that's not a reason for the manual
to speak _only_ of that behavior.  d-s-mode doesn't
have as its aim only to replicate behavior that you
might be used to outside Emacs.

If you're going to start mentioning non-replacement
behavior, please prioritize the basic design of
d-s-mode, not the changes proposed in this thread.

Mention the latter if you like, but please don't
overwhelm the description of d-s-mode itself, and
(especially) please prioritize its essential means
of customization - the basic design.

Emphasize what's important.  The proposed new
feature isn't what's important about being able to
"tune this behavior".  Not IMO.

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

* bug#55692: 29.0.50; delete-selection-mode: Replace region only if set using the mouse
  2022-05-31 10:44               ` Visuwesh
  2022-05-31 14:33                 ` Drew Adams
@ 2022-06-01 16:04                 ` Eli Zaretskii
  2022-06-01 17:03                   ` Visuwesh
  1 sibling, 1 reply; 13+ messages in thread
From: Eli Zaretskii @ 2022-06-01 16:04 UTC (permalink / raw)
  To: Visuwesh; +Cc: 55692

> From: Visuwesh <visuweshm@gmail.com>
> Cc: 55692@debbugs.gnu.org
> Date: Tue, 31 May 2022 16:14:55 +0530
> 
> Improved patch attached.

Thanks, installed.





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

* bug#55692: 29.0.50; delete-selection-mode: Replace region only if set using the mouse
  2022-06-01 16:04                 ` Eli Zaretskii
@ 2022-06-01 17:03                   ` Visuwesh
  2022-09-06 10:59                     ` Lars Ingebrigtsen
  0 siblings, 1 reply; 13+ messages in thread
From: Visuwesh @ 2022-06-01 17:03 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: 55692

[Wednesday June 01, 2022] Eli Zaretskii wrote:

>> From: Visuwesh <visuweshm@gmail.com>
>> Cc: 55692@debbugs.gnu.org
>> Date: Tue, 31 May 2022 16:14:55 +0530
>> 
>> Improved patch attached.
>
> Thanks, installed.

Great, thanks.





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

* bug#55692: 29.0.50; delete-selection-mode: Replace region only if set using the mouse
  2022-06-01 17:03                   ` Visuwesh
@ 2022-09-06 10:59                     ` Lars Ingebrigtsen
  0 siblings, 0 replies; 13+ messages in thread
From: Lars Ingebrigtsen @ 2022-09-06 10:59 UTC (permalink / raw)
  To: Visuwesh; +Cc: Eli Zaretskii, 55692

Visuwesh <visuweshm@gmail.com> writes:

>>> Improved patch attached.
>>
>> Thanks, installed.
>
> Great, thanks.

But the bug report was left open, so I'm closing it now.  (I quickly
skimmed the bug report to see if there was anything else to be done
here, but didn't see anything.  If this is a mistake, please respond to
the debbugs address and we'll reopen.)





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

end of thread, other threads:[~2022-09-06 10:59 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-05-28 17:33 bug#55692: 29.0.50; delete-selection-mode: Replace region only if set using the mouse Visuwesh
2022-05-28 19:10 ` Eli Zaretskii
2022-05-29  6:17   ` Visuwesh
2022-05-29  6:52     ` Eli Zaretskii
2022-05-29  8:10       ` Visuwesh
2022-05-29  9:04         ` Eli Zaretskii
2022-05-29 14:53           ` Visuwesh
2022-05-29 17:10             ` Eli Zaretskii
2022-05-31 10:44               ` Visuwesh
2022-05-31 14:33                 ` Drew Adams
2022-06-01 16:04                 ` Eli Zaretskii
2022-06-01 17:03                   ` Visuwesh
2022-09-06 10:59                     ` Lars Ingebrigtsen

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.