Subject: bug#29098: 26.0.90; [PATCH] 2 patches: initial-buffer-choice and python-indent-dedent-line-backspace

From: Alex <[email-masked]>
Date: Tue, 31 Oct 2017 23:24:48 -0600

I have 2 small patches that I think should go to emacs-26, and I'm not
sure if they deserve their own individual reports. Is this alright, or
should I split them up in the future?

Anyway, these alleviate/solve some issues that I've seen people report.
The first patch provides more information to an error message, and the
second deals with pressing backspace in a python-mode buffer with an
active region.

When in a python-mode buffer with the contents:

def foo():
    print('bar')

with an active region and point at the "p", then pressing backspace
de-indents the line instead of deleting it. This is at odds with the
behaviour in other modes, where a similar action deletes the line, at
least with ‘delete-active-region’ non-nil.

I thought about making this behaviour conditional using
‘delete-active-region’, but that might be a bit inconsistent.

>From 922fe8f466ad039f65fb423af6636bfd1695f039 Mon Sep 17 00:00:00 2001
From: Alexander Gramiak <[email-masked]>
Date: Tue, 31 Oct 2017 21:10:52 -0600
Subject: [PATCH 1/2] Improve error messages regarding initial-buffer-choice

* lisp/startup.el (command-line-1) <initial-buffer-choice>: Make the
messages conform to Emacs conventions, and show the invalid return
value in the message.
---
 lisp/startup.el | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/lisp/startup.el b/lisp/startup.el
index 4b538d130e..cf88c40e92 100644
--- a/lisp/startup.el
+++ b/lisp/startup.el
@@ -2506,9 +2506,9 @@ command-line-1
                    ((eq initial-buffer-choice t)
                     (get-buffer-create "*scratch*"))
                    (t
-                    (error "initial-buffer-choice must be a string, a function, or t.")))))
+                    (error "`initial-buffer-choice' must be a string, a function, or t")))))
         (unless (buffer-live-p buf)
-          (error "initial-buffer-choice is not a live buffer."))
+          (error "Value returned by `initial-buffer-choice' is not a live buffer: %S" buf))
         (setq displayable-buffers (cons buf displayable-buffers))))
 
     ;; Display the first two buffers in `displayable-buffers'.  If
-- 
2.14.2

>From 50bd5bd058b71031f84c4d5a4d511353c671d66d Mon Sep 17 00:00:00 2001
From: Alexander Gramiak <[email-masked]>
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