Bill Clementson's Blog

Bits and pieces (mostly Lisp-related) that I collect from the ether.

May 2007
Sun Mon Tue Wed Thu Fri Sat
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
Apr  Jun

Distel = Emacs erlang-mode++

Monday, May 28, 2007

There are a number of different editor options for people wanting to develop Erlang code (see here, here, here); however, most people seem to use Emacs (which suits me just fine, because that's what I use most of the time anyhow). For Emacs users, the standard Erlang Mode for Emacs (which is included as part of the Erlang distribution) is what most people use. It has a lot of the functionality that you need when working with Erlang code and a lot of the "traditional" things that are normally found in an Emacs major mode for a programming language. It has support for things such as:

However, coming from a Lisp background, I'm used to being able to interact directly with the underlying Lisp implementation, and I missed not having that available when working with Erlang code. Fortunately, Luke Gorrie created Distel - an Emacs Lisp + Erlang package that fills this gap (I've mentioned Distel in previous blog posts here, here, here). Although Distel is a lot more than just an extension of Erlang Mode for Emacs, I'll focus on just the erlang-extended-mode aspects of it here. Since Luke also did a lot of the work on SLIME, any Lisper who uses SLIME will notice a lot of similarities between SLIME and Distel. Distel is an extension to erlang-mode in the same sense that SLIME is an extension to lisp-mode (and it has some of the same types of extensions).

First of all, a few tips about installing Distel. If you Google for Distel, you'll find it in a number of different places. The "canonical" home page is on Luke's site at http://fresh.homeunix.net/~luke/distel/ (and the online documentation is still there); but, the actual source code has been hosted at different times in two separate Sourceforge projects and on the Erlang CEAN site as well. However, the current development repository for Distel is at the distel project on Google Code. To download it, you'll need to check it out from the Subversion repository using the following command:
svn checkout http://distel.googlecode.com/svn/trunk/ distel
Once you've checked it out, you'll need to follow the INSTALL instructions to build things and then update your .emacs file. Here's what I have in my .emacs file to setup Erlang mode and Distel:
;; This is needed for Erlang mode setup
(setq erlang-root-dir "/usr/local/lib/erlang")
(setq load-path (cons "/usr/local/lib/erlang/lib/tools-2.5.1/emacs" load-path))
(setq exec-path (cons "/usr/local/lib/erlang/bin" exec-path))
(require 'erlang-start)
;; This is needed for Distel setup (let ((distel-dir "/Users/bc/Projects/distel/elisp")) (unless (member distel-dir load-path) ;; Add distel-dir to the end of load-path (setq load-path (append load-path (list distel-dir)))))
(require 'distel) (distel-setup)
In addition, I add the following customizations:
;; Some Erlang customizations
(add-hook 'erlang-mode-hook
	  (lambda ()
	    ;; when starting an Erlang shell in Emacs, default in the node name
	    (setq inferior-erlang-machine-options '("-sname" "emacs"))
	    ;; add Erlang functions to an imenu menu
	    (imenu-add-to-menubar "imenu")))
;; A number of the erlang-extended-mode key bindings are useful in the shell too (defconst distel-shell-keys '(("\C-\M-i" erl-complete) ("\M-?" erl-complete) ("\M-." erl-find-source-under-point) ("\M-," erl-find-source-unwind) ("\M-*" erl-find-source-unwind) ) "Additional keys to bind when in Erlang shell.")
(add-hook 'erlang-shell-mode-hook (lambda () ;; add some Distel bindings to the Erlang shell (dolist (spec distel-shell-keys) (define-key erlang-shell-mode-map (car spec) (cadr spec)))))
Since Distel is designed to communicate with a running Erlang node (make certain that you have an Erlang cookie set up properly!), the first thing I generally do in an Erlang source buffer is start up Erlang (if it's not already running). Usually, if I'm just playing around with something, I'll start up an Erlang Shell in Emacs with "C-c C-z" (erlang-shell-display) and connect to that; otherwise, I'll connect to an instance that I've started up outside of Emacs (either locally or remotely). In my .emacs file, I've got the inferior-erlang-machine-options variable set to "-sname emacs" so that the default node name for an Erlang shell started in Emacs is "emacs@bc". So, once I've got an Erlang node going, I'll connect Distel to it by pressing "C-c C-d n" (erl-choose-nodename) and specify the node I want to connect to ("emacs" if I'm connecting to the Erlang shell in Emacs) - that establishes which Erlang node all Distel commands will go to. (Note: For someone familiar with using SLIME with Common Lisp, this is analogous to doing a slime-connect in SLIME.)

Once you've setup things correctly and opened an Erlang source member, if you look at the "Erlang" menu (that was created by erlang-mode), you'll see that Distel has added some additional commands to that menu. Although Distel provides some functionality not covered by the menu options, the menu options will give you a good idea of a lot of the supplemental functionality over and above what is provided by erlang-mode:

Menu

Here are some of the things that Distel provides: If you program in Erlang and use Emacs, then I think it's really worthwhile to have a look at Distel. The best first document to read on Distel is the User Manual (pdf or

ps.gz), which describes how to use Distel and what commands are available. For more in-depth information, read the Reference Manual (pdf or ps.gz). In addition to those two documents, Luke's 2002 Erlang User Conference paper Distel: Distributed Emacs Lisp (pdf or ps.gz), describes Distel's motivation and workings in detail. Lastly, the Distel mailing list (also available via gmane) can be a good source of information.

emacs Copyright © 2009 by Bill Clementson