Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4f009fb

Browse files
committed
(py-no-outdent-re): new constant
(py-indent-line, py-electric-colon): watch for compound statements one line after another.
1 parent b5e0ecb commit 4f009fb

1 file changed

Lines changed: 39 additions & 18 deletions

File tree

Misc/python-mode.el

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -310,13 +310,25 @@ Currently-active file is at the head of the list.")
310310
(defconst py-outdent-re
311311
(concat "\\(" (mapconcat 'identity
312312
'("else:"
313-
"except\\s +.*:"
313+
"except\\(\\s +.*\\)?:"
314314
"finally:"
315315
"elif\\s +.*:")
316316
"\\|")
317317
"\\)")
318318
"Regexp matching clauses to be outdented one level.")
319319

320+
(defconst py-no-outdent-re
321+
(concat "\\(" (mapconcat 'identity
322+
'("try\\s +.*:"
323+
"except\\(\\s +.*\\)?:"
324+
"while\\s +.*:"
325+
"for\\s +.*:"
326+
"if\\s +.*:"
327+
"elif\\s +.*:")
328+
"\\|")
329+
"\\)")
330+
"Regexp matching lines to not outdent after.")
331+
320332

321333
;;;###autoload
322334
(defun python-mode ()
@@ -397,20 +409,27 @@ In certain cases the line is outdented appropriately. If a numeric
397409
argument is provided, that many colons are inserted non-electrically."
398410
(interactive "P")
399411
(self-insert-command (prefix-numeric-value arg))
400-
(let (this-indent)
401-
(if (and (not arg)
402-
(save-excursion
403-
(back-to-indentation)
404-
(looking-at py-outdent-re))
405-
(= (setq this-indent (py-compute-indentation))
406-
(save-excursion
407-
(forward-line -1)
408-
(py-compute-indentation)))
409-
)
410-
(save-excursion
411-
(beginning-of-line)
412-
(delete-horizontal-space)
413-
(indent-to (- this-indent py-indent-offset)))
412+
(save-excursion
413+
(let ((here (point))
414+
(outdent 0)
415+
(indent (py-compute-indentation)))
416+
(if (and (not arg)
417+
(progn
418+
(back-to-indentation)
419+
(looking-at py-outdent-re))
420+
(prog2
421+
(backward-to-indentation 1)
422+
(not (looking-at py-no-outdent-re))
423+
(goto-char here))
424+
(= indent (progn
425+
(forward-line -1)
426+
(py-compute-indentation)))
427+
)
428+
(setq outdent py-indent-offset))
429+
(goto-char here)
430+
(beginning-of-line)
431+
(delete-horizontal-space)
432+
(indent-to (- indent outdent))
414433
)))
415434

416435

@@ -630,10 +649,12 @@ needed so that only a single column position is deleted."
630649
(let* ((ci (current-indentation))
631650
(move-to-indentation-p (<= (current-column) ci))
632651
(need (py-compute-indentation)))
633-
;; watch for outdents
652+
;; see if we need to outdent
634653
(if (save-excursion
635-
(back-to-indentation)
636-
(looking-at py-outdent-re))
654+
(and (progn (back-to-indentation)
655+
(looking-at py-outdent-re))
656+
(progn (backward-to-indentation 1)
657+
(not (looking-at py-no-outdent-re)))))
637658
(setq need (- need py-indent-offset)))
638659
(if (/= ci need)
639660
(save-excursion

0 commit comments

Comments
 (0)