28 November 2010

Setting up the emacs code browser

Update September 2014

Installing ECB in 24.3 emacs is much simpler than described here - see my updated post.

One of the key goals of this tutorial is to setup a file browsing pane in Emacs, similar to what Textmate offers. For Rails development this is essential as each project requires a lot of different files.

The Emacs Code Browser

Emacs has been around so long that it has plug ins for just about every use case, and browsing files is no exception. To create a Textmate style file browsing pane, all you need to do is install the Emacs code browser (ECB). This can be a bit daunting, as there are so many configuration options and settings required to get it working well. Fortunately, this tutorial will get you something pretty useful working very quickly.

Downloads

You need to download two packages to setup ecb:

  • CEDET - This is a requirement of ecb, don't worry about it, just install it.
  • ecb - The code for ecb itself.

Now you have the 2 downloads, you need to put them somewhere. On Windows, I created a directory called plugins inside my emacs directory, giving me C:\emacs-23.2\plugins

On the Mac, I created a directory called .emacs_includes/plugins in my home directory.

I am pretty sure Emacs gurus would tell me these files should go somewhere else, but this works for me and the location is not really important. Extract each of the downloads into the plugins directory.

Compiling CEDET

There are a few ways of compiling CEDET, but the most portable involves doing it inside emacs itself.

Assuming CEDET has been extracted to c:\emacs-23.2\plugins\cedet-1.0, open emacs and then open the file c:\emacs-23.2\plugins\cedet-1.0\cedet-build.el. Don't edit anything in this file, just run the following two emacs commands:

  • M-x eval-buffer
  • M-x cedet-build-in-default-emacs

(M-x means press and hold the alt key, and then press x and release both keys. Then enter the text 'eval-buffer' which will appear at the very bottom of the emacs window, then hit return). The second command will take several minutes to run and it will open a new instance of emacs while it does it. When I ran it, lots of warnings scrolled past, but I ignored them and things worked fine. Emacs also prompted to answer y or n for creating a new directory. Just answer y. When the compilation is complete, you should see a window that has 'done' at the bottom of it. At this point just close emacs as the compilation is complete.

Note that this method will not work in a unix terminal, however the solution is easy. Just ensure the correct emacs is on your path, switch into the cedet directory and enter the command make, which will get everything byte compiled.

Installing Emacs plugins generally involves putting some files in a known location, and telling Emacs about them using the .emacs file. Now you need to tell emacs about CEDET. Open your .emacs file (or _emacs on windows) and add the following (this is a Windows example, edit the paths for OS X):

; allows syntax highlighting to work
 (global-font-lock-mode 1)

;; Load CEDET.
;; This is required by ECB which will be loaded later.
;; See cedet/common/cedet.info for configuration details.
(load-file "C:/emacs-23.2/plugins/cedet-1.0/common/cedet.el")

;; Enable EDE (Project Management) features
(global-ede-mode 1)

;; * This enables the database and idle reparse engines
(semantic-load-enable-minimum-features)

;; * This enables some tools useful for coding, such as summary mode
;;   imenu support, and the semantic navigator
(semantic-load-enable-code-helpers)

After saving the .emacs file, restart emacs and hopefully it should load without displaying any config errors.

Installing ECB

Installing ecb is pretty simple compared to CEDET. All you have to do is add two more lines to your .emacs:

(add-to-list 'load-path "C:/emacs-23.2/plugins/ecb-2.40")
(load-file "C:/emacs-23.2/plugins/ecb-2.40/ecb.el")

One again restart emacs and start ecb by entering the command:

M-x ecb-activate

The Emacs window will change, adding a new section with 4 windows on the left side. In this default mode, the top window shows directories, next files, then history and finally methods in the current file.

To close ECB enter M-x ecb-deactivate

I changed this default layout to show only a combined directory and file listing and opened file history by adding the following lines to my .emacs:

(custom-set-variables
;; custom-set-variables was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
'(ecb-layout-name "left14")
'(ecb-layout-window-sizes (quote (("left14" (0.2564102564102564 . 0.6949152542372882) (0.2564102564102564 . 0.23728813559322035)))))
'(ecb-options-version "2.40"))

There really are tons of options for ecb, and I only know a handful of them. First of all, you will want to add the location of your code files so they are quickly accessible in ECB:

(ecb-source-path (quote ("d:/myRailsProject" "d:/useful scripts")))

By default, ecb opens files using the middle mouse button. If you want to use the left mouse button, add the following option:

'(ecb-primary-secondary-mouse-buttons (quote mouse-1--C-mouse-1))

I also wanted to get rid of the annoying tip-of-the-day and use ascii style directory listings:

'(ecb-tip-of-the-day nil)
'(ecb-tree-buffer-style (quote ascii-guides)))

If you go for all the options I have set, your .emacs should look like:

; allows syntax highlighting to work
 (global-font-lock-mode 1)

;; Load CEDET.
;; This is required by ECB which will be loaded later.
;; See cedet/common/cedet.info for configuration details.
(load-file "C:/emacs-23.2/plugins/cedet-1.0/common/cedet.el")

;; Enable EDE (Project Management) features
(global-ede-mode 1)

;; * This enables the database and idle reparse engines
(semantic-load-enable-minimum-features)

;; * This enables some tools useful for coding, such as summary mode
;;   imenu support, and the semantic navigator
(semantic-load-enable-code-helpers)

(add-to-list 'load-path "C:/emacs-23.2/plugins/ecb-2.40")
(load-file "C:/emacs-23.2/plugins/ecb-2.40/ecb.el")

(custom-set-variables
;; custom-set-variables was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
'(ecb-layout-name "left14")
'(ecb-layout-window-sizes (quote (("left14" (0.2564102564102564 . 0.6949152542372882) (0.2564102564102564 . 0.23728813559322035)))))
'(ecb-options-version "2.40")
'(ecb-source-path (quote ("d:/myRailsProject" "d:/useful scripts")))
'(ecb-primary-secondary-mouse-buttons (quote mouse-1--C-mouse-1))
'(ecb-tip-of-the-day nil)
'(ecb-tree-buffer-style (quote ascii-guides)))

There is a lot to configure in ECB if you wish. All the details are available in the packaged manual, which you can view by typing M-x ecb-show-help.

Having used ecb for a few year now, the only commands I ever use are:

  • Jump to the directory window CTRL-c . gd (ie type ctrl and c together, release and press '.', release and press 'g' then 'd')
  • Jump to the history window CTRL-c . gh
  • Jump to the last window you were in CTRL-c . gl
  • Jump to the first editor window CTRL-c . g1

The directory browser can be controlled without using the mouse too – just use the arrow keys and enter – give it a go!

As you can see, setting up ECB is not difficult, and its well worth it in my opinion.

blog comments powered by Disqus