Auto-insert of colons in Emacs
Peter Milliken
peter.milliken at gtech.com
Wed May 17 22:38:58 EDT 2000
Hi John,
To quote a famous recent email "Why hobble your mind with a detail the
computer can figure out for itself?" :-)
Have a look at true language sensitive editing using Emacs at
http://members.xoom.com/pmilliken
I have just updated the minor mode (you will see it if you haunt
gnu.emacs.sources) and added a new template language for Python :-) So, now
you can get the computer to generate all of the syntax and you just supply
the variable names :-) This mode works with any major mode, not just Python,
there are language templates for C, Ada, Emacs-Lisp etc
I have only just started learning Python, so I don't claim the templates are
perfect but I have been using them for a couple of days now without
problems. If there is something you don't like let me know and I will fix it
(full instructions on how the templates work and how to modify them are
included in the 45+ page user manual :-)).
Any questions, please don't hesitate to call :-)
Peter
John Wiegley wrote in message ...
>
>Why hobble your mind with a detail the computer can figure out for
>itself?
>
>It's very strictly defined where a colon should go in Python, so it
>makes little sense that humans should be bothered with this detail.
>If it makes the code more readable for those who didn't write it, I
>can understand; but the author has better things to think about.
>
>Accordingly, the following snippet of Emacs code will ensure there is
>always a colon where it should be. You should never have to type them
>again, except when you want the whole statement to fit on one line.
>
>To use it, just put everything after the "----" in your .emacs file.
>
>Enjoy. This was written for users of python-mode.el
>(http://www.python.org/emacs/python-mode/)
>
>----------------------------------------------------------------------
>
>(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 (eq (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