>From 50bd5bd058b71031f84c4d5a4d511353c671d66d Mon Sep 17 00:00:00 2001 From: Alexander Gramiak Date: Tue, 31 Oct 2017 22:36:16 -0600 Subject: [PATCH 2/2] Delete active region instead of de-indenting in python-mode This conforms to the behaviour of `backward-delete-char-untabify' when there's an active region. 183f9296f1 fixes this behaviour for `delete-selection-mode' only. * lisp/progmodes/python.el (python-indent-dedent-line): Restructure and check for an active region conditionally using a new optional argument. (python-indent-dedent-line-backspace): Use the new argument to `python-indent-dedent-line'. * test/lisp/progmodes/python-tests.el: Add test. --- lisp/progmodes/python.el | 19 +++++++++++-------- test/lisp/progmodes/python-tests.el | 17 +++++++++++++++++ 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el index 895117b9ee..56faab057d 100644 --- a/lisp/progmodes/python.el +++ b/lisp/progmodes/python.el @@ -1150,21 +1150,24 @@ python-indent-line-function (and (memq this-command python-indent-trigger-commands) (eq last-command this-command)))) -(defun python-indent-dedent-line () - "De-indent current line." +(defun python-indent-dedent-line (&optional region-inhibits) + "De-indent current line. +If REGION-INHIBITS is non-nil, then don't de-indent when the +region is active as determined by `use-region-p'." (interactive "*") - (when (and (not (bolp)) - (not (python-syntax-comment-or-string-p)) - (= (current-indentation) (current-column))) - (python-indent-line t) - t)) + (unless (or (bolp) + (and region-inhibits (use-region-p)) + (python-syntax-comment-or-string-p) + (/= (current-indentation) (current-column))) + (python-indent-line t) + t)) (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." (interactive "*p") - (unless (python-indent-dedent-line) + (unless (python-indent-dedent-line t) (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 a59885637e..1b9cd23d6b 100644 --- a/test/lisp/progmodes/python-tests.el +++ b/test/lisp/progmodes/python-tests.el @@ -2475,6 +2475,23 @@ python-tests-visible-string (line-beginning-position) (line-end-position)) "abcdef"))))) +(ert-deftest python-indent-dedent-line-backspace-4 () + "Check that an active region is deleted instead of the +line being de-indented." + (python-tests-with-temp-buffer + " +def foo(): + print('bar')" + (let ((transient-mark-mode t)) + (goto-char (point-max)) + (set-mark (point)) + (back-to-indentation) + (call-interactively #'python-indent-dedent-line-backspace) + (should + (string= (buffer-substring-no-properties + (line-beginning-position) (line-end-position)) + " "))))) + (ert-deftest python-bob-infloop-avoid () "Test that strings at BOB don't confuse syntax analysis. Bug#24905" (python-tests-with-temp-buffer -- 2.14.2