Subject: Re: bug#48695: [PATCH] Fix region deletion behavior in python.el

From: Jakub Ječmínek <[email-masked]>
Date: Sat, 26 Jul 2025 21:57:38 +0200

Eli Zaretskii <[email-masked]> writes:

> Ping! Jakub, did you have an opportunity to work on the updated patch?

Sorry for the delayed response.  Please see the patch below.

>From d10520e9ab362b3667bf06a798316e8afe6e8e64 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jakub=20Je=C4=8Dm=C3=ADnek?= <[email-masked]>
Date: Sun, 29 Jun 2025 23:56:13 +0200
Subject: [PATCH] Improve region deletion handling in python.el

* lisp/progmodes/python.el (python-indent-dedent-line-backspace):
Delete the text in the region if Transient Mark mode is enabled, the
mark is active, and prefix arg is 1.  (Bug#48695)

* test/lisp/progmodes/python-tests.el: Add new test
`python-indent-dedent-line-backspace-4'.
---
 etc/NEWS                            |  7 +++++++
 lisp/progmodes/python.el            |  9 ++++++---
 test/lisp/progmodes/python-tests.el | 17 +++++++++++++++++
 3 files changed, 30 insertions(+), 3 deletions(-)

diff --git a/etc/NEWS b/etc/NEWS
index 8d41efdaf64..1ee573213aa 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -1809,6 +1809,13 @@ restart Emacs.
 ---
 *** Support of 'electric-layout-mode' added.
 
+---
+*** DEL now deletes the text in the active region when point is between indentation.
+The command 'python-indent-dedent-line-backspace' (by default bound to
+'DEL') now deletes the text in the region and deactivates the mark if
+Transient Mark mode is enabled, the mark is active, and prefix argument
+is 1.
+
 ** Tmm Menubar
 
 ---
diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
index f4f0518dbfd..8b5ffae57d6 100644
--- a/lisp/progmodes/python.el
+++ b/lisp/progmodes/python.el
@@ -1967,10 +1967,13 @@ python-indent-dedent-line
 
 (defun python-indent-dedent-line-backspace (arg)
   "De-indent current line.
-Argument ARG is passed to `backward-delete-char-untabify' when
-point is not in between the indentation."
+Argument ARG is passed to `backward-delete-char-untabify' when point is
+not in between the indentation or when Transient Mark mode is enabled,
+the mark is active, and ARG is 1."
   (interactive "*p")
-  (unless (python-indent-dedent-line)
+  (when (or
+         (and (use-region-p) (= arg 1))
+         (not (python-indent-dedent-line)))
     (backward-delete-char-untabify arg)))
 
 (put 'python-indent-dedent-line-backspace 'delete-selection 'supersede)
diff --git a/test/lisp/progmodes/python-tests.el b/test/lisp/progmodes/python-tests.el
index 2c00f47d82c..d9aad7c7034 100644
--- a/test/lisp/progmodes/python-tests.el
+++ b/test/lisp/progmodes/python-tests.el
@@ -3780,6 +3780,23 @@ python-indent-dedent-line-backspace-3
                 (pos-bol) (pos-eol))
                "abcdef")))))
 
+(ert-deftest python-indent-dedent-line-backspace-4 ()
+  "Delete the text in the region instead of de-indentation.  Bug#48695."
+  (dolist (test '((1 4) (2 0)))
+    (python-tests-with-temp-buffer
+        "
+if True:
+    x ()
+    if False:
+"
+      (let ((current-prefix-arg (list (car test))))
+        (python-tests-look-at "if False:")
+        (end-of-line)
+        (set-mark-command nil)
+        (backward-word 2)
+        (call-interactively #'python-indent-dedent-line-backspace)
+        (should (= (current-indentation) (cadr test)))))))
+
 (ert-deftest python-bob-infloop-avoid ()
   "Test that strings at BOB don't confuse syntax analysis.  Bug#24905"
   (python-tests-with-temp-buffer
-- 
2.49.1