Auto-insert of colons in Emacs

John Wiegley jwiegley at inprise.com
Wed May 17 15:07:17 EDT 2000


One correction needed: for lines that end in , or \, which should not
be followed by a colon, since it implies a continuation onto the next
line

(eval-after-load "python-mode"
  '(progn
     (defvar python-keywords-wanting-colon
       '("def" "class" "if" "while" "else"
	 "try" "except" "finally" "for" "lambda"))

     (defvar python-kwc-regexp nil)

     (require 'advice)
     (defadvice py-newline-and-indent (before always-insert-colons activate)
       "Always make sure that colons appear in the appropriate place."
       (unless (memq (char-before) '(?: ?, ?\\))
	 (let ((here (point))
	       insert-colon already-has-it)
	   (save-excursion
	     (beginning-of-line)
	     (save-excursion
	       (if (search-forward ":" here t)
		   (setq already-has-it t)))
	     (unless already-has-it
	       (unless python-kwc-regexp
		 (require 'regexp-opt)
		 (setq python-kwc-regexp
		       (concat "\\s-*\\<"
			       (regexp-opt python-keywords-wanting-colon t)
			       "\\>")))
	       (if (looking-at python-kwc-regexp)
		   (setq insert-colon t))))
	   (if insert-colon
	       (let ((last-command-char ?:))
		 (py-electric-colon nil))))))))



More information about the Python-list mailing list