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

From: Okamsn <[email-masked]>
Date: Sat, 26 Oct 2024 18:14:57 +0000

Stefan Monnier wrote:
>> 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.
> 
> In `electric-indent-local-mode` I faced the same question and wasn't
> completely sure but I opted somewhat arbitrarily to do the
> `kill-local-variable` whenever applicable.
> I still don't know if it's the better choice, but FWIW, nobody's
> complained about it:
> 
>        (cond
>         ((eq electric-indent-mode (default-value 'electric-indent-mode))
>          (kill-local-variable 'electric-indent-mode))
> 
>> * lisp/delsel.el (delete-selection-pre-hook): Check whether local mode
>> is active.
> 
> AFAICT you don't change `delete-selection-pre-hook` any more.
> 
>> * doc/emacs/mark.texi (Using Region): Describe
>> delete-selection-local-mode.
>> +;;;###autoload
>> +(define-minor-mode delete-selection-local-mode
>> +  "Toggle Delete Selection mode in the current buffer.
>> +
>> +See the command `delete-selection-mode'.
> 
> FWIW, for `electric-indent-local-mode` I went for something shorter:
> 
>     "Toggle `electric-indent-mode' only in this buffer."
> 
> 
> - Stefan
> 

Hello,

Please see the attached patch with the changes you suggested.

Thank you.

From 52d8ac478ef015e6875faa6e0c76dc3009fa5960 Mon Sep 17 00:00:00 2001
From: Earl Hyatt <[email-masked]>
Date: Sat, 12 Oct 2024 20:28:25 -0400
Subject: [PATCH v4] 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.

* 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      | 18 ++++++++++++++++++
 3 files changed, 32 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..40d626644b2 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 as
+needed.
 
 * Changes in Specialized Modes and Packages in Emacs 31.1
 
diff --git a/lisp/delsel.el b/lisp/delsel.el
index df99a56d7bc..18d889ab4c8 100644
--- a/lisp/delsel.el
+++ b/lisp/delsel.el
@@ -95,6 +95,24 @@ 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' only in this buffer.
+
+For compatibility with features and packages that are aware of
+`delete-selection-mode', this local mode sets the variable
+`delete-selection-mode' in the current buffer as needed."
+  :global nil :group 'editing-basics
+  :variable (buffer-local-value 'delete-selection-mode (current-buffer))
+  (cond
+   ((eq delete-selection-mode (default-value 'delete-selection-mode))
+    (kill-local-variable 'delete-selection-mode))
+   ((not (default-value 'delete-selection-mode))
+    ;; Locally enabled, but globally disabled.
+    (delete-selection-mode 1)                ; Setup the hooks.
+    (setq-default delete-selection-mode nil) ; But keep it globally disabled.
+    )))
+
 (defvar delsel--replace-text-or-position nil)
 
 ;;;###autoload
-- 
2.34.1