Emacs Autocomplete

Introduction

In this article I'll explain how I got autocompletion to work in emacs. Here's what it looks like:
emacs autocompletion
I know that this isn't a new idea. But most of the methods I've seen only give text completion in the mini-buffer whereas I like graphical completion. Here I found one way of doing that but it was somewhat buggy (for me at least) and only started to complete when the tab button was pressed.
After playing around a bit I managed to get graphical autocompletion while typing '.' after an object (in python mode) and after any three letters. The three letter completion is surprisingly useful. It searches the current buffer for words with the same beginning and gives you a drop down list. This is helpful for anything from variables to function names. The interesting part about the drop down list is that it's pure text, so it even works in a terminal as you can see in the image above.

How it works

First of all, we have to install rope, ropemacs and ropemode for python autocompletion. Since there are some issues with mixing together the various versions of these packages, I put together a tarball with the versions that worked for me. I'll be assuming you're using it in the following code. In addition, I'm assuming that your emacs configuration file is ~/.emacs and that your emacs configuration folder is ~/.emacs.d.
 
tar -xjf autocomplete.tar.bz2 #extract the tarball 
cd autocomplete/rope
sudo python setup.py install #install rope 
cd ../ropemacs
sudo python setup.py install #install ropemacs and ropemode 
Next we need to install Pymacs and put the autocomplete files in the emacs configuration directory:
 
cd ../Pymacs-0.23 
sudo python setup.py install #install pymacs 
cp pymacs.el ~/.emacs.d
cp ../auto-complete.el ../auto-complete-config.el ~/.emacs.d
And finally, we have to put the following code into the .emacs file:
 
(add-to-list 'load-path "~/.emacs.d")
(require 'auto-complete)
(global-auto-complete-mode t)
 
(require 'auto-complete-config)
(ac-ropemacs-initialize)
(global-auto-complete-mode t)
(define-key ac-complete-mode-map "\t" 'ac-expand)
(define-key ac-complete-mode-map "\r" 'ac-complete)
(define-key ac-complete-mode-map "\M-n" 'ac-next)
(define-key ac-complete-mode-map "\M-p" 'ac-previous)
(setq ac-auto-start 3)
(setq ac-dwim t)
(set-default 'ac-sources '(ac-source-abbrev ac-source-words-in-buffer))
(setq ac-modes
         (append ac-modes
                 '(eshell-mode
                         )))

Some final tweaks

By now, you should have the automcompletion working. But there are a few more things we can to do. In it's default configuration, autocomplete will only start automatically for the modes that are defined in the ac-modes list which you can find in the autocomplete.el file. In order to add modes to the default, just add them to the list. In addition, you might want to change the color theme of the drop down list. You can do that by adding the following three lines to the .emacs file:
 
(set-face-background 'ac-menu-face "lightgray")
(set-face-underline 'ac-menu-face "darkgray")
(set-face-background 'ac-selection-face "steelblue")
Obviously, you can change the colors to whatever you want. And one final hack. Whenever you open a python file for the first time in an emacs session and press the '.' key, rope is activated and it will ask you for a location for the rope project file. For some reason, when I press RET and choose the default I get an error. If, on the other hand, I first go back to the main buffer (C-x o), and then switch back to the minibuffer and hit RET (C-x o RET), then it works.