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

From: kobarity <[email-masked]>
Date: Wed, 09 Jul 2025 23:26:55 +0900

Jakub Ječmínek wrote:
> 
> On 7/1/25 05:31, kobarity wrote:
> > How about using `delete-active-region'?  This affects the behavior of
> > `backward-delete-char-untabify'.  I have a feeling that most people
> > who set this to nil would prefer the current behavior.
> > 
> > `backward-delete-char-untabify' has the following code.
> > 
> >    (if (and (use-region-p)
> >             delete-active-region
> >             (= arg 1))
> >        ;; Delete the text of the region and deactivate the mark, in the
> >        ;; same way that `delete-backward-char' does.
> >        (with-no-warnings (delete-backward-char 1 killp))
> > 
> > I think it would be more consistent to use the same logic as this.
> 
> Do you mean something like this?
> 
> diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
> index f4f0518dbfd..92c0889da8f 100644
> --- a/lisp/progmodes/python.el
> +++ b/lisp/progmodes/python.el
> @@ -1970,7 +1970,9 @@ python-indent-dedent-line-backspace
>  Argument ARG is passed to `backward-delete-char-untabify' when
>  point is not in between the indentation."
>    (interactive "*p")
> -  (unless (python-indent-dedent-line)
> +  (when (or
> +         (and (use-region-p) delete-active-region)
> +         (not (python-indent-dedent-line)))
>      (backward-delete-char-untabify arg)))
> 
>  (put 'python-indent-dedent-line-backspace 'delete-selection 'supersede)

Mostly yes.  I would prefer to add the check for arg being 1 as well.
Doing so has the following advantages:

- Less difference from current behavior.
- The behavior can be controlled by the prefix argument.  (When
  `delete-active-region' is non-nil and the region is not empty.)
- More consistent with `backward-delete-char-untabify'.

In any case, it would be better to update the docstring.

diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
index f4f0518dbfd..867e945fabb 100644
--- a/lisp/progmodes/python.el
+++ b/lisp/progmodes/python.el
@@ -1970,7 +1970,9 @@ python-indent-dedent-line-backspace
 Argument ARG is passed to `backward-delete-char-untabify' when
 point is not in between the indentation."
   (interactive "*p")
-  (unless (python-indent-dedent-line)
+  (when (or
+         (and (use-region-p) delete-active-region (= arg 1))
+         (not (python-indent-dedent-line)))
     (backward-delete-char-untabify arg)))
 
 (put 'python-indent-dedent-line-backspace 'delete-selection 'supersede)