From 848e5d6f4e81791288fa163215dcb1065e151d13 Mon Sep 17 00:00:00 2001 From: Earl Hyatt 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'. 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'. (cherry picked from commit e0b21b6c4de4d7f0b7ad9ae112755435f501835e) --- doc/emacs/mark.texi | 4 +++- etc/NEWS | 7 +++++++ lisp/delsel.el | 18 ++++++++++++++++++ 3 files changed, 28 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 @@ instead signal an error if the mark is inactive. @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 @@ 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}. +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 b16d415aad1..8526a80da5e 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -156,6 +156,13 @@ 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'. +This mode sets 'delete-selection-mode' buffer-locally. 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. + * Changes in Specialized Modes and Packages in Emacs 31.1 diff --git a/lisp/delsel.el b/lisp/delsel.el index 8a7002eb965..130fdfbe851 100644 --- a/lisp/delsel.el +++ b/lisp/delsel.el @@ -95,6 +95,24 @@ information on adapting behavior of commands in 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.39.5