Subject: Re: bug#73782: [PATCH] Add `delete-selection-local-mode'.

From: Okamsn <[email-masked]>
Date: Thu, 24 Oct 2024 03:24:14 +0000

Stefan Monnier wrote:
>> +(define-minor-mode delete-selection-local-mode
>> +  "Toggle Delete Selection mode in the current buffer.
>> +
>> +See the command `delete-selection-mode'.
>> +
>> +For compatibility with features and packages that are aware of
>> +`delete-selection-mode', this mode also sets the
>> +variable `delete-selection-mode' in the current buffer."
>> +  :global nil :group 'editing-basics
> 
> Have you tried to use the `:variable (buffer-local-value
> 'delete-selection-mode)` instead of introducing a parallel
> `delete-selection-local-mode` variable?
> 
> 
>          Stefan
> 

I had not thought to try that. Attached is a patch which uses 
`buffer-local-value` as a `setf`-able place. I had to include the third 
argument `(current-buffer)`.

Do you think that disabling the local mode should remove the 
buffer-local version of `delete-selection-mode` via 
`kill-local-variable`? It seems that once the variable is made buffer 
local, the global mode can't take effect in the buffer, even when the 
local mode is disabled.

If you do think that the local version of the variable should be 
removed, do you think that it should be removed selectively or always?

Thank you.

From 900131edb9337bed3b624a80295c2e95cb75e24d Mon Sep 17 00:00:00 2001
From: Earl Hyatt <[email-masked]>
Date: Sat, 12 Oct 2024 20:28:25 -0400
Subject: [PATCH v3] Add delete-selection-local-mode.

* lisp/delsel.el (delete-selection-local-mode): Add local version of
delete-selection-mode.  The local mode sets the value of the variable
'delete-selection-mode' to maintain compatibility with packages and
features that consider the existing mode.

* lisp/delsel.el (delete-selection-pre-hook): Check whether local mode
is active.

* doc/emacs/mark.texi (Using Region): Describe
delete-selection-local-mode.

* etc/NEWS: Describe delete-selection-local-mode.
---
 doc/emacs/mark.texi |  4 +++-
 etc/NEWS            | 11 +++++++++++
 lisp/delsel.el      | 15 +++++++++++++++
 3 files changed, 29 insertions(+), 1 deletion(-)

diff --git a/doc/emacs/mark.texi b/doc/emacs/mark.texi
index 0d705769f55..83261d36495 100644
--- a/doc/emacs/mark.texi
+++ b/doc/emacs/mark.texi
@@ -306,6 +306,7 @@ Using Region
 @cindex Delete Selection mode
 @cindex mode, Delete Selection
 @findex delete-selection-mode
+@findex delete-selection-local-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},
@@ -323,7 +324,8 @@ Using Region
 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}.
+delete-selection-mode}.  To toggle Delete Selection mode on or off
+in the current buffer only, type @kbd{M-x delete-selection-local-mode}.
 
 @node Mark Ring
 @section The Mark Ring
diff --git a/etc/NEWS b/etc/NEWS
index daaae54d7d3..ea36f3002e9 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -204,6 +204,17 @@ a prefix argument when inserting one of the delimiters.
 Typing M-~ while saving some buffers means not to save the buffer and
 also to mark it as unmodified.  This is an alternative way to mark a
 buffer as unmodified which doesn't require switching to that buffer.
+
+** New minor mode 'delete-selection-local-mode'.
+'delete-selection-local-mode' is a buffer-local version of the existing
+'delete-selection-mode'.  This can be useful for enabling or disabling
+the features of 'delete-selection-mode' based on the state of the
+buffer, such as for the different states of modal editing packages.
+
+For compatibility with features and packages that are aware of the
+existing 'delete-selection-mode', 'delete-selection-local-mode' will set
+the value of the variable 'delete-selection-mode' buffer locally when
+enabled or disabled.
 
 * Changes in Specialized Modes and Packages in Emacs 31.1
 
diff --git a/lisp/delsel.el b/lisp/delsel.el
index df99a56d7bc..5a492999585 100644
--- a/lisp/delsel.el
+++ b/lisp/delsel.el
@@ -95,6 +95,21 @@ delete-selection-mode
       (remove-hook 'pre-command-hook 'delete-selection-pre-hook)
     (add-hook 'pre-command-hook 'delete-selection-pre-hook)))
 
+;;;###autoload
+(define-minor-mode delete-selection-local-mode
+  "Toggle Delete Selection mode in the current buffer.
+
+See the command `delete-selection-mode'.
+
+For compatibility with features and packages that are aware of
+`delete-selection-mode', this mode sets the variable
+`delete-selection-mode' in the current buffer."
+  :global nil :group 'editing-basics
+  :variable (buffer-local-value 'delete-selection-mode (current-buffer))
+  (remove-hook 'pre-command-hook 'delete-selection-pre-hook t)
+  (when delete-selection-mode
+    (add-hook 'pre-command-hook 'delete-selection-pre-hook t)))
+
 (defvar delsel--replace-text-or-position nil)
 
 ;;;###autoload
-- 
2.34.1