bug-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

bug#22661: 25.0.91; python.el electric-indent misbehaviour with 'else:'


From: Hong Xu
Subject: bug#22661: 25.0.91; python.el electric-indent misbehaviour with 'else:' and nested 'if:'
Date: Mon, 12 Dec 2016 18:01:06 -0800
User-agent: mu4e 0.9.18; emacs 25.1.90.9

> If I have the following code and add the colon after the 'else',
> python.el's electric-indent moves the 'else' to align with the inner
> 'if'.

> def foo()
>     if aaa:
>         if bbb:
>             x = 1
>         y = 1
>     else

I've attached a patch to fix this issue: It is caused by incorrect
detection of opening blocks.

From: Hong Xu <address@hidden>
Date: Mon, 12 Dec 2016 17:55:25 -0800
Subject: [PATCH] python-mode: Fix detection for opening blocks (bug# 22661).

        * python.el (python-info-dedenter-opening-block-positions): There
        can't be any back-indented lines between an opening block and the
        current line.
---
 lisp/progmodes/python.el | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)

diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
index d0d4a7f766ef..8d3bd3194151 100644
--- a/lisp/progmodes/python.el
+++ b/lisp/progmodes/python.el
@@ -4660,7 +4660,8 @@ python-info-dedenter-opening-block-positions
     (let ((dedenter-pos (python-info-dedenter-statement-p)))
       (when dedenter-pos
         (goto-char dedenter-pos)
-        (let* ((pairs '(("elif" "elif" "if")
+        (let* ((cur-line (line-beginning-position))
+               (pairs '(("elif" "elif" "if")
                         ("else" "if" "elif" "except" "for" "while")
                         ("except" "except" "try")
                         ("finally" "else" "except" "try")))
@@ -4676,7 +4677,18 @@ python-info-dedenter-opening-block-positions
               (let ((indentation (current-indentation)))
                 (when (and (not (memq indentation collected-indentations))
                            (or (not collected-indentations)
-                               (< indentation (apply #'min 
collected-indentations))))
+                               (< indentation (apply #'min 
collected-indentations)))
+                           ;; There must be no line with indentation smaller 
than
+                           ;; `indentation' between the found opening block 
and the
+                           ;; current line, otherwise it is not an opening 
block.
+                           (save-excursion
+                             (forward-line)
+                             (let ((no-back-indent t))
+                               (while (and (< (point) cur-line)
+                                           (setq no-back-indent
+                                                 (> (current-indentation) 
indentation)))
+                                 (forward-line))
+                               no-back-indent)))
                   (setq collected-indentations
                         (cons indentation collected-indentations))
                   (when (member (match-string-no-properties 0)

Attachment: signature.asc
Description: PGP signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]