Search This Blog

Sunday, February 21, 2010

make ipython work better with emacs

ipython is an excellent python shell to be used with emacs. ipython.el is the emacs extension which communicates with ipython on behalf of emacs.

When ipython is activated in emacs, we can complete functions, attributes, variables or something else with the TAB key, if there are more than one possible completions, a new buffer  will created to show them. However, the new buffer can not automatically disappear just as in slime does, so I make some modifications to ipython.el.

1. create a new file named tempbuffer.el with the following elisp source code as content:

(defvar saved nil)

(defun restore-after-temp-buffer ()
  (remove-hook 'pre-command-hook 'restore-after-temp-buffer)
  (when saved
    (progn (set-window-configuration saved)
           (setf saved nil))))

(defun delay-restoring-after-temp-buffer ()
  (add-hook 'pre-command-hook 'restore-after-temp-buffer))

(defmacro with-output-to-restorable-temp-buffer (bufname &rest body)
  `(progn (setf saved (current-window-configuration))
          (with-output-to-temp-buffer ,bufname
            ,@body)
          (delay-restoring-after-temp-buffer)))

(provide 'tempbuffer)

2. put tempbuffer.el into somewhere emacs can find.

3. add (require 'tempbuffer) to ipython.el and replace with-output-to-tempbuffer with with-output-to-restorable-temp-buffer.

after done this, the new buffer showing possible completions will close as soon as we type something in emacs.

No comments: