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: Sat, 21 Jan 2017 18:08:48 -0800
User-agent: mu4e 0.9.19; emacs 25.1.91.8

On 2017-01-21 Sat 09:34 GMT-0800, Matthew Woodcraft <address@hidden> wrote:

>
> I have tested this patch and it does fix the case I posted.
>
> But it misbehaves if I add a blank line before the 'else':
>
> def foo()
>     if aaa:
>         if bbb:
>             x = 1
>         y = 1
>
>     else
>
> Now when I add the colon after the 'else', electric-indent moves the
> 'else' to align with the 'def'.
>
>
> I agree that python-info-dedenter-opening-block-positions is the right
> thing to fix.
>
> I think it's sufficient to look at the last nonblank line above the
> current line, rather than all lines between the candidate opening block
> and the current line.
>
> -M-

Thanks for your suggestion, Matthew. I've now fixed it in the new patch
as attached.

From b9b742e15174de6abe5134427964fe4e0f852461 Mon Sep 17 00:00:00 2001
From: Hong Xu <address@hidden>
Date: Mon, 12 Dec 2016 17:55:25 -0800
Subject: [PATCH] python-mode: Fix detection for opening blocks.

        * 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 | 20 ++++++++++++++++++--
 1 file changed, 18 insertions(+), 2 deletions(-)

diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
index 306402d8e3b4..ae9b0890cfc2 100644
--- a/lisp/progmodes/python.el
+++ b/lisp/progmodes/python.el
@@ -4659,7 +4659,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")))
@@ -4675,7 +4676,22 @@ 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' (except for
+                           ;; blank lines) 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
+                                                 (or (> (current-indentation) 
indentation)
+                                                     (save-match-data
+                                                       
(python-info-current-line-empty-p)))))
+                                 (forward-line))
+                               no-back-indent)))
                   (setq collected-indentations
                         (cons indentation collected-indentations))
                   (when (member (match-string-no-properties 0)
-- 
2.1.4

Attachment: signature.asc
Description: PGP signature


reply via email to

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