Monday, 30 June 2008

Adding Python Mode to Emacs

To associate newly-loaded Python files (and the interpreter name) with the awesome new Python mode, we need to update:
  • auto-mode A List (matches the filename with a MajorMode when file is first opened)
  • interpreter-mode A List (associates interpreter name with a MajorMode)
(setq auto-mode-alist
(cons '("\\.py$" . python-mode) auto-mode-alist))
(setq interpreter-mode-alist
(cons '("python" . python-mode)
interpreter-mode-alist))
(autoload 'python-mode "python-mode" "Python editing mode." t)

python-mode.el does some pretty cool things like:
  • set the default number of spaces for a tab in Python (where indentation is clearly important)
(defcustom py-indent-offset 4
"*Amount of offset per level of indentation.
`\\[py-guess-indent-offset]' can usually guess a good value when
you're editing someone else's Python code."
:type 'integer
:group 'python)

  • Parse classes and functions in the source file and display in the IM-Python menu item
On Windows the Python mode code is normally stored in C:\Program Files\emacs-versionX\modes\python-mode-versionY\. The latest version can be swiped from sourceforge.net/projects/python-mode.

No comments: