Subject: [PATCH] Add `delete-selection-local-mode'.

From: Okamsn <[email-masked]>
Date: Sun, 13 Oct 2024 01:25:28 +0000

Hello,

This patch adds a local version of `delete-selection-mode`. I use a 
modal editing package for which I would like to have 
`delete-selection-mode` enabled depending on the modal state that is 
active in the buffer. This doesn't currently work because 
`delete-selection-mode` applies to all buffers.

For compatibility, I made the local mode set the value of the variable 
`delete-selection-mode` buffer locally. I see that some features, such 
as the built-in `table.el`, check that variable to adjust their 
behavior. Do you think that is a good way to maintain compatibility?

Thank you.

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

* lisp/delsel.el (delete-selection-local-mode): Add local version
of delete-selection-mode.

* lisp/delsel.el (delete-selection-pre-hook): Check whether local mode
is active.
---
 lisp/delsel.el | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)

diff --git a/lisp/delsel.el b/lisp/delsel.el
index df99a56d7bc..b4cc6745603 100644
--- a/lisp/delsel.el
+++ b/lisp/delsel.el
@@ -95,6 +95,22 @@ 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 also sets the
+variable `delete-selection-mode' in the current buffer."
+  :global nil :group 'editing-basics
+  (remove-hook 'pre-command-hook 'delete-selection-pre-hook t)
+  (setq-local delete-selection-mode nil)
+  (when delete-selection-local-mode
+    (add-hook 'pre-command-hook 'delete-selection-pre-hook t)
+    (setq-local delete-selection-mode t)))
+
 (defvar delsel--replace-text-or-position nil)
 
 ;;;###autoload
@@ -264,7 +280,8 @@ delete-selection-pre-hook
 property on their symbol; commands which insert text but don't
 have this property won't delete the selection.
 See `delete-selection-helper'."
-  (when (and delete-selection-mode (use-region-p)
+  (when (and (or delete-selection-mode delete-selection-local-mode)
+             (use-region-p)
 	     (not buffer-read-only)
              (or (null delete-selection-temporary-region)
                  (and delete-selection-temporary-region
-- 
2.34.1