[FRPythoneers] Emacs & Python (was Re: VB/Python article....)

Clementson, Bill Bill_Clementson at jdedwards.com
Tue Jun 5 11:19:12 MDT 2001

> >  > I've set up some custom
> >  > emacs code to automatically jump to modules and/or view pydoc
> >  > documentation,
> > 
> > Ooh, that sounds good. Would you be willing to share that code?
> Sure, I'll write up a summary of my python-specific emacs 
> setup & send it
> out.

Here is my setup. I would be interested in hearing what emacs customizations
other people have been doing for python development. 

My emacs version is 20.7.1 and I use the same setup on Windows (NT & 98) and
Linux (2.4). I have a lot of setup for other languages and functionality
that I won't show here - this is just the python stuff.

First of all, I have the following python-specific functionality from
3rd-party elisp contributions:

#1. Python indenting, syntax support, etc. via python-mode.el 
#2. Python debugger support via pdb.el & pdbtrack.el (shows line in source
as you step thru code)
#3. Python pydoc support via pydoc.el 
#4. Generic OO browser via oo-browser from beopen.com

These modules are loaded into a directory in the emacs loadpath and have the
usual setup.

In addition to the above, I have added the following:

#5. Function key f11 loads the module (using fff.el) that the cursor is on
by searching the Python sys.path (using pythonpath.py below) 
#6. Function key f1 brings up the pydoc help for the module/keyword that the
cursor is on
#7. Function key Shift-f1 brings up the Windows Help file pyshelf.chm
containing all the python documentation as well as the pydoc documentation
(this is a convenient searchable format but is a MSWindows-only function)

Ok, here is the code itself:

The following Python code (pythonpath.py) is in my PYTHONPATH and called
from the elisp code (for #5):

--------------------- cut here --------------------------------------------
#!/usr/bin/env python
Produces Lisp list of Python paths (based on pydoc_lisp).

from string import join
import sys

def main():
    paths = _get_paths_lisp()
    # Print the alist so the Lisp reader can evaluate it.
    print paths

def _get_paths_lisp():
    paths = [e.replace('\\', '/') for e in sys.path[:]]
        paths[paths.index('')] = '.'
    except ValueError:
    return '("%s")\n' % join(paths, '" "')

if __name__ == '__main__':

--------------------- cut here --------------------------------------------

The following code is in my emacs startup:

--------------------- cut here --------------------------------------------

;;;; Python Mode

(setq auto-mode-alist
      (cons '("\\.py$" . python-mode) auto-mode-alist))
(setq interpreter-mode-alist
      (cons '("python" . python-mode)
(autoload 'python-mode "python-mode" "Python editing mode." t)

(require 'pydoc)

(autoload 'thing-at-point "thingatpt" "mouse enable object" t)

(defvar python-path nil
  "Python path.")

(defun pythonpath-initialize()
  (let (output-buf)
      ;; Start a Python interpreter if not already running.
      (pydoc-wait-for-output (current-buffer) 20.0)
      (setq output-buf
	     "import pythonpath
      (setq python-path (pydoc-lisp-read-result output-buf)))
    (if (member python-path '(nil None Traceback))
	  (setq python-path nil)
	  (pop-to-buffer output-buf)
	  (error "Initialization failed, Python did not output Lisp python
path lists"))
      (pydoc-kill-async-output output-buf (pydoc-async-output-p))
      (message ""))))

(defun find-python-module ()
  "Load the module at the cursor."
  (require 'fff)
  (if (null python-path)
  (fff-find-file-in-path (concat (thing-at-point 'symbol) ".py")

(defun my-pydoc-call ()
  "Find python documentation on symbol at point"
  (if (null pydoc-alist)
  (pydoc-call "help" (format "'%s'" (thing-at-point 'symbol))
(thing-at-point 'symbol)))

(defun my-pyshelf-call ()
  "Open a browser window showing the pyshelf documentation"
   ((string= "w32" window-system)
    (shell-command "C:/WINNT/hh.exe C:/docs/pyshelf/pyshelf.chm"))))

(defun my-python-mode-hook ()
  (define-key py-mode-map [f1] 'my-pydoc-call)
  (define-key py-mode-map [(shift f1)] 'my-pyshelf-call)
  (define-key py-mode-map [f11] 'find-python-module))

(add-hook 'python-mode-hook 'my-python-mode-hook)

;;;; OO Browser

(load "br-start")
(global-set-key [(alt y)] 'oo-browser)

--------------------- cut here --------------------------------------------

More information about the FRPythoneers mailing list