summaryrefslogtreecommitdiff
path: root/lisp/progmodes/python.el
diff options
context:
space:
mode:
authorHong Xu <hong@topbug.net>2016-12-12 17:55:25 -0800
committerNoam Postavsky <npostavs@gmail.com>2017-01-26 20:14:19 -0500
commit7cb7a582f44db94292709d35f4f5474f891f03b0 (patch)
treee0ab66d0b9409565128b65e01061dbd061f48502 /lisp/progmodes/python.el
parent3485c1693efb709cfc15023d9b38978278731a32 (diff)
downloademacs-7cb7a582f44db94292709d35f4f5474f891f03b0.tar.gz
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. * python-tests.el (python-indent-electric-colon-4): Add an indent test case where there is one-more indented previous opening block.
Diffstat (limited to 'lisp/progmodes/python.el')
-rw-r--r--lisp/progmodes/python.el20
1 files changed, 18 insertions, 2 deletions
diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
index d8262dd0a75..90b5e4e0dc6 100644
--- a/lisp/progmodes/python.el
+++ b/lisp/progmodes/python.el
@@ -4693,7 +4693,8 @@ likely an invalid python file."
(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")))
@@ -4709,7 +4710,22 @@ likely an invalid python file."
(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))
+ (save-match-data
+ (while (and (< (point) cur-line)
+ (setq no-back-indent
+ (or (> (current-indentation) indentation)
+ (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)