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
(python-indent-dedent-line-backspace-4): Add new test.
This commit is contained in:
Jakub Ječmínek 2025-06-29 23:56:13 +02:00 committed by Eli Zaretskii
parent 2846dd8842
commit e52ed1b5d1
3 changed files with 31 additions and 3 deletions

View file

@ -1822,6 +1822,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
---

View file

@ -1967,10 +1967,13 @@ indentation levels from right to left."
(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)

View file

@ -3780,6 +3780,24 @@ if x:
(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)
(transient-mark-mode)
(set-mark (point))
(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