diff Misc/python-mode.el @ 2535:875173f3f31c legacy-trunk

(py-electric-colon): new command
author Barry Warsaw <barry@python.org>
date Tue, 14 Mar 1995 15:55:20 +0000
parents 6001448fab89
children a14fc2d04c06
line wrap: on
line diff
--- a/Misc/python-mode.el	Tue Mar 14 15:09:05 1995 +0000
+++ b/Misc/python-mode.el	Tue Mar 14 15:55:20 1995 +0000
@@ -222,7 +222,8 @@
   (mapcar (function
 	   (lambda (x)
 	     (define-key py-mode-map (car x) (cdr x))))
-	  '(("\C-c\C-c"  . py-execute-buffer)
+	  '((":"         . py-electric-colon)
+	    ("\C-c\C-c"  . py-execute-buffer)
 	    ("\C-c|"	 . py-execute-region)
 	    ("\C-c!"	 . py-shell)
 	    ("\177"	 . py-delete-char)
@@ -363,6 +364,30 @@
     (run-hooks 'py-mode-hook)))
 
 
+;; electric characters
+(defun py-electric-colon (arg)
+  "Insert a colon.
+In certain cases the line is outdented appropriately.  If a numeric
+argument is provided, that many colons are inserted non-electrically."
+  (interactive "P")
+  (self-insert-command (prefix-numeric-value arg))
+  (let (this-indent)
+    (if (and (not arg)
+	     (save-excursion
+	       (forward-word -1)
+	       (looking-at "\\(else\\|except\\|finally\\elif\\):"))
+	     (= (setq this-indent (py-compute-indentation))
+		(save-excursion
+		  (forward-line -1)
+		  (py-compute-indentation)))
+	     )
+	(save-excursion
+	  (beginning-of-line)
+	  (delete-horizontal-space)
+	  (indent-to (- this-indent py-indent-offset)))
+      )))
+
+
 ;;; Functions that execute Python commands in a subprocess
 (defun py-shell ()
   "Start an interactive Python interpreter in another window.