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

Skip to content

Commit b91b743

Browse files
committed
(py-electric-colon): new command
1 parent 0c891ce commit b91b743

1 file changed

Lines changed: 26 additions & 1 deletion

File tree

Misc/python-mode.el

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,8 @@ Currently-active file is at the head of the list.")
222222
(mapcar (function
223223
(lambda (x)
224224
(define-key py-mode-map (car x) (cdr x))))
225-
'(("\C-c\C-c" . py-execute-buffer)
225+
'((":" . py-electric-colon)
226+
("\C-c\C-c" . py-execute-buffer)
226227
("\C-c|" . py-execute-region)
227228
("\C-c!" . py-shell)
228229
("\177" . py-delete-char)
@@ -362,6 +363,30 @@ py-beep-if-tab-change\tring the bell if tab-width is changed"
362363
(run-hooks 'python-mode-hook)
363364
(run-hooks 'py-mode-hook)))
364365

366+
367+
;; electric characters
368+
(defun py-electric-colon (arg)
369+
"Insert a colon.
370+
In certain cases the line is outdented appropriately. If a numeric
371+
argument is provided, that many colons are inserted non-electrically."
372+
(interactive "P")
373+
(self-insert-command (prefix-numeric-value arg))
374+
(let (this-indent)
375+
(if (and (not arg)
376+
(save-excursion
377+
(forward-word -1)
378+
(looking-at "\\(else\\|except\\|finally\\elif\\):"))
379+
(= (setq this-indent (py-compute-indentation))
380+
(save-excursion
381+
(forward-line -1)
382+
(py-compute-indentation)))
383+
)
384+
(save-excursion
385+
(beginning-of-line)
386+
(delete-horizontal-space)
387+
(indent-to (- this-indent py-indent-offset)))
388+
)))
389+
365390

366391
;;; Functions that execute Python commands in a subprocess
367392
(defun py-shell ()

0 commit comments

Comments
 (0)