unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Joel Rosdahl <joel@rosdahl.net>
To: 22663@debbugs.gnu.org
Subject: bug#22663: python-mode: Fix electric indent bug after dedenting colon
Date: Sun, 12 Feb 2017 16:11:41 +0100	[thread overview]
Message-ID: <CAK8D=pgYxrbZN4uk-3z3NRWhXMcfyH0ZLrtae_txPCPngj++kA@mail.gmail.com> (raw)
In-Reply-To: <877fi7z041.fsf@golux.woodcraft.me.uk>


[-- Attachment #1.1: Type: text/plain, Size: 488 bytes --]

This fixes bug #22663 ("25.0.91; python.el electric-indent misbehaviour
with 'except:'"). Note that the bug is not restricted to "except:" but
affects all statements that are dedented by an electric colon.

For instance, entering a colon after "else" in

    if do:
        something()
        else
    outside

results in

    if do:
        something()
    else:
        outside

before the bug fix but

    if do:
        something()
    else:
    outside

after the bug fix.

-- Joel

[-- Attachment #1.2: Type: text/html, Size: 895 bytes --]

[-- Attachment #2: 0001-python-mode-Fix-electric-indent-bug-after-dedenting-.patch --]
[-- Type: text/x-patch, Size: 2802 bytes --]

From 555fb47d0c3061346c2792e97206a0d64dd1e578 Mon Sep 17 00:00:00 2001
From: Joel Rosdahl <joel@rosdahl.net>
Date: Sun, 12 Feb 2017 16:10:47 +0100
Subject: [PATCH] python-mode: Fix electric indent bug after dedenting colon

This fixes bug #22663 ("25.0.91; python.el electric-indent misbehaviour
with 'except:'"). Note that the bug is not restricted to "except:" but
affects all statements that are dedented by an electric colon.

For instance, entering a colon after "else" in

    if do:
        something()
        else
    outside

results in

    if do:
        something()
    else:
        outside

before the bug fix but

    if do:
        something()
    else:
    outside

after the bug fix.
---
 lisp/progmodes/python.el            | 19 ++++++++++---------
 test/lisp/progmodes/python-tests.el |  3 +++
 2 files changed, 13 insertions(+), 9 deletions(-)

diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
index 90b5e4e..467ac08 100644
--- a/lisp/progmodes/python.el
+++ b/lisp/progmodes/python.el
@@ -1286,16 +1286,17 @@ python-indent-post-self-insert-function
            (not (equal ?: (char-before (1- (point)))))
            (not (python-syntax-comment-or-string-p)))
       ;; Just re-indent dedenters
-      (let ((dedenter-pos (python-info-dedenter-statement-p))
-            (current-pos (point)))
+      (let ((dedenter-pos (python-info-dedenter-statement-p)))
         (when dedenter-pos
-          (save-excursion
-            (goto-char dedenter-pos)
-            (python-indent-line)
-            (unless (= (line-number-at-pos dedenter-pos)
-                       (line-number-at-pos current-pos))
-              ;; Reindent region if this is a multiline statement
-              (python-indent-region dedenter-pos current-pos)))))))))
+          (let ((start (copy-marker dedenter-pos))
+                (end (point-marker)))
+            (save-excursion
+              (goto-char start)
+              (python-indent-line)
+              (unless (= (line-number-at-pos start)
+                         (line-number-at-pos end))
+                ;; Reindent region if this is a multiline statement
+                (python-indent-region start end))))))))))
 
 \f
 ;;; Mark
diff --git a/test/lisp/progmodes/python-tests.el b/test/lisp/progmodes/python-tests.el
index 1e6b867..e911921 100644
--- a/test/lisp/progmodes/python-tests.el
+++ b/test/lisp/progmodes/python-tests.el
@@ -1130,10 +1130,13 @@ python-tests-visible-string
 if do:
     something()
     else
+outside
 "
    (python-tests-look-at "else")
    (goto-char (line-end-position))
    (python-tests-self-insert ":")
+   (should (= (current-indentation) 0))
+   (python-tests-look-at "outside")
    (should (= (current-indentation) 0))))
 
 (ert-deftest python-indent-electric-colon-3 ()
-- 
2.7.4


  reply	other threads:[~2017-02-12 15:11 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-02-14 17:13 bug#22663: 25.0.91; python.el electric-indent misbehaviour with 'except:' Matthew Woodcraft
2017-02-12 15:11 ` Joel Rosdahl [this message]
2018-12-27 17:38 ` bug#22663: python-mode: Fix electric indent bug after dedenting colon Joel Rosdahl
2019-01-05  9:05   ` Eli Zaretskii

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAK8D=pgYxrbZN4uk-3z3NRWhXMcfyH0ZLrtae_txPCPngj++kA@mail.gmail.com' \
    --to=joel@rosdahl.net \
    --cc=22663@debbugs.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).