Lisp development with GNU Emacs

Just out of interest I decided last week to revisit Lisp.  Usually I have no problem learning a new computer programming language, but when trying Lisp as part of an Open University post-graduate computing course about ten years ago, I didn’t really get on with it.  It wasn’t that I couldn’t do it.  I passed the module, after all, although not with a great mark.  It was more that I didn’t ‘get’ it.

So this time, I’m following the book: Practical Common Lisp by Peter Seibel, details of which (and a free electronic copy) you can find on the gigamonkeys site.  I think it’s a great tutorial, by the way, and will definitely be buying a copy of the book, if only to show my appreciation.

Development environment

Setting up a development environment is really important whatever language you’re using, whether it’s for the purpose of learning the language or for serious development.  In this case, the book recommends using Lispbox, which is just a packaging up of Emacs, SLIME, QuickLisp and a couple of other things.  And I guess that this is fine for Windows, but most Linux distributions have all these things (or their equivalents) in their repositories, so I decided to install the bits individually and see where it got me.  The following describes how I set up these elements on my Ubuntu 12.04 laptop.

Lisp

Simply install Lisp from the distribution’s repository.  I don’t know of any distribution that doesn’t include it.  The package name may be different for different distributions, so use whatever search facilities your distribution or your package manager offers and search for something like ‘clisp’ or ‘common lisp’ or just ‘lisp’.  You should be able to find the right package.

When it’s installed you can check it’s all working as it should do by opening a terminal window and typing:

$ clisp

This will start the LISP read-eval-print loop (REPL).  To exit just type:

(quit)
CLISP REPL

CLISP REPL

Emacs

Many developers swear by Emacs: those that don’t swear by VI, I guess.  However I’ve always found it a bit of a challenge.  It’s not hard, but there are a lot of commands to learn.  What’s more if you’re using the GTK+ or the GNU version, many commands are accessible from the menus using a mouse.  So flogging your way through the tutorial meticulously trying out commands you’ll probably never use can be a boring and disheartening experience.

I did try to follow the tutorial; and not for the first time.  This time I got about sixty percent of the way through; which is better than my previous attempts.  Fortunately there are a lot of crib sheets available on the web such as the one from refcards.com.  At least this frees up some of your personal memory for useful stuff, and probably tells you all you need to know from the tutorial, but in a much more succinct form.

GNU Emacs can be installed from the standard repository on every Linux distribution I know about, and can be left configured pretty much as it is after a default install.  The only thing I decided to do was to put the scroll bar on the right hand side of the window.  GNU Emacs follows the old X-Window convention of having the scroll bar on the left: fine if you’re left-handed, but I’m not.

To move the scroll bar to the right hand side you need to check whether you have a .emacs file in your home directory.  If you don’t, you need to create one.  Once the file is created, simply add the following to the file:

(custom-set-variables
      '(scroll-bar-mode (quote right)))

If the .emacs file already existed on your installation and if it already contained a custom-set-variables form, you could add the ‘(scroll-bar-mode (quote right)) to it.

Looks like Lisp, doesn’t it?  That’s because it is.

SLIME

SLIME is the Superior Lisp Interaction mode for Emacs.  It allows you to create Lisp files and debug them without having to swap between your text editor and a terminal window running clisp.

Once again, just install it from your distribution’s repository.  If your distribution doesn’t have SLIME in the repository, you can download it from the project’s web site and install it following the installation instructions.

To get going with SLIME, start up Emacs and then issue the command:

M-x slime

(Where, following the usual Emacs conventions, M-x means to hold down the Meta key and type ‘x’.  The meta key will be either [Esc] or [Alt].  It’s [Esc] on my laptop).

You should see the Lisp REPL displayed now within Emacs:

SLIME in Emacs

SLIME in Emacs

 Look through any Window

In Emacs, you enter your Lisp code into a ‘buffer’.  By default, this buffer will occupy all the space in the main Emacs window.  And when you activate SLIME it, too will occupy the entire Emacs window as shown in the image above.  Swapping between these two is not very convenient.  Luckily, Emacs can display two windows side-by-side.  Simply issue the following Emacs command:

C-x 3

(The command C-x 2 splits the window horizontally.  ‘C’ in these commands refers to the [Ctrl] key, and is another standard Emacs documentation convention.)

You can now have SLIME in one window and your code in another:

Emacs split screen

Emacs split screen

Development in Emacs with SLIME

Workspace

Usually, your code and data files are not in the currenct working directory as SLIME sees things.  To find your current working directory you can do the following in SLIME:

CL-USER> ,

That single comma (which you should follow with [RETURN] if necessary) will make SLIME prompt you for a command to perform.  Type:

pwd

followed by [RETURN], and the current working directly will by displayed.

On my system, the [RETURN] character is not required for SLIME to prompt for the command name.

To change the current working directory to your workspace where all your files for your current application are located, type a comma again, followed by [RETURN] if necessary, at the SLIME prompt and then type in the path to your workspace, followed by [RETURN].  For example type:

~/proyectos-de-lisp/database

(or whatever the appropriate path is to your own workspace for your project)

Developing

When you’ve set up your development environment as described above, the code you are developing and the REPL that is open under SLIME are linked.  If you compile and save your Lisp code:

C-c C-k

the newly-compiled file will be loaded automatically:

Compiled file loaded

Compiled file loaded

Note that this loads the compiled Lisp file.  You can, of course, just load the source code:

(load "path/to/code/codefile.lisp")

I found that this could sometimes be more reliable if the compiled file could not be loaded for some reason.  If you changed your current working directory to your workspace as described above, the load command can simply be:

(load "codefile.lisp")

Bootnote

Lisp is starting to make more sense now, thanks to a sensible development environment and, naturally, the tutorial.  If other helpful things come to light, I’ll try to remember to include them in a post.

This entry was posted in Uncategorized. Bookmark the permalink.