From ef79c8cf3d63f510107536901925c05e37740275 Mon Sep 17 00:00:00 2001 From: Thierry Volpiatto Date: Sun, 9 Mar 2025 16:08:41 +0100 Subject: [PATCH] New user variable `exchange-point-and-mark-highlight-region` When set to nil, this modifies `exchange-point-and-mark' so that it doesn't activate mark when it is not already active. * etc/NEWS: Introduce new variable exchange-point-and-mark-highlight-region. * lisp/simple.el (exchange-point-and-mark-highlight-region): New. (exchange-point-and-mark): Use it. --- etc/NEWS | 5 +++++ lisp/simple.el | 23 ++++++++++++++++++----- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/etc/NEWS b/etc/NEWS index 926d426eaae..01c522e1516 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -473,6 +473,11 @@ 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. +** New user variable 'exchange-point-and-mark-highlight-region'. +When set to nil, this modifies `exchange-point-and-mark' so that it doesn't +activate mark when it is not already active. +The default value is 't' which changes nothing to previous behavior. + * Changes in Specialized Modes and Packages in Emacs 31.1 diff --git a/lisp/simple.el b/lisp/simple.el index 32479f3c092..1b7df13a2ac 100644 --- a/lisp/simple.el +++ b/lisp/simple.el @@ -7456,10 +7456,20 @@ Does not set point. Does nothing if mark ring is empty." (pop mark-ring)) (deactivate-mark)) +(defcustom exchange-point-and-mark-highlight-region t + "Activate region when exchanging point and mark. + +When set to nil, this modifies `exchange-point-and-mark' so that it doesn't +activate mark when it is not already active." + :type 'boolean + :group 'editing-basics + :version 31.1) + (defun exchange-point-and-mark (&optional arg) "Put the mark where point is now, and point where the mark is now. This command works even when the mark is not active, -and it reactivates the mark. +and it reactivates the mark unless +`exchange-point-and-mark-highlight-region' is nil. If Transient Mark mode is on, a prefix ARG deactivates the mark if it is active, and otherwise avoids reactivating it. If @@ -7467,15 +7477,18 @@ Transient Mark mode is off, a prefix ARG enables Transient Mark mode temporarily." (interactive "P") (let ((omark (mark t)) + (region-was-active (region-active-p)) (temp-highlight (eq (car-safe transient-mark-mode) 'only))) (if (null omark) (user-error "No mark set in this buffer")) (set-mark (point)) (goto-char omark) - (or temp-highlight - (cond ((xor arg (not (region-active-p))) - (deactivate-mark)) - (t (activate-mark)))) + (cond (temp-highlight) + ((xor arg (if exchange-point-and-mark-highlight-region + (not (region-active-p)) + (not region-was-active))) + (deactivate-mark)) + (t (activate-mark))) nil)) (defcustom shift-select-mode t