Editing Eiffel code with Emacs

Why use Emacs

As a programmer you will spend most of your time inside an editor. Editing Eiffel code, editing Makefiles, editing XML or XSLT files, editing sources in other languages. You will also spend time in the editor when writing email or posts to newsgroup. And if you write documentation you will spend even more time in the editor. Many programmers use different editors for all these environments. If there's a new language they will have to work with, they will spend time learning a new editor. Programmers typically don't spend enough time in a single editor to make customisation worthwile. And when they switch editors they have to redo all that customisation again.

But why not use a single editor that allows you to edit everything under the sun, that you will be able to use with every language? Why not become proficient in a single editor, learn to master it and use it for the rest of your life? There's only one editor that truly does everything, and that editor is Emacs.

Emacs features

Here some compelling features that Emacs has to offer the Eiffel programmer:

  1. It's available for every platform under the sun. You can take Emacs with you not only when writing sources in all kinds of languages, but also when hopping from Windows to Linux to BeOS to OS X for example.
  2. Spell-check at the touch of a finger type by pressing M-$ (on PC keyboards the M key is the Alt key).
  3. Or edit with spell checking turned on: M-x flyspell-mode. This works surprisingly well for Eiffel sources as we tend to write everything in full. Flyspell is smart enough to break a_second into two words: “a” and “second”.
  4. Dynamic feature name completion: M-/.
  5. Syntax highlighting for every computer language under the sun.
  6. Completion support (called tags).

Getting Emacs

Unix systems typicall have Emacs installed. If not, it's easy to download the sources for Emacs from the main Emacs site and compile it youself.

On Windows you can use the Cygwin layer. If you install it's X Window server, you can run Emacs in its X Window GUI mode, just like a Unix machine would.

If you prefer a more native version of Emacs on Windows, you can download these as well.

Keyboard and syntax highlighting

By default syntax highlighting isn't enabled, but it's easy to turn it on. And it demonstrates how you can customize Emacs using the new customization features.

Emacs can be customized by browsing the customization groups. You can do this interactively by choosing “Options” from the menu and next “Customize Emacs”.

But let's do it slightly faster by choosing a specific option. There are two things you probably want to do immediately:

  1. Make Emacs keyshortcuts work more like you're used to on Windows.
  2. Enable syntax highlighting.

Press C-h v (Ctrl+h followed by v). You can now type in the name of a variable. Enter ‘pc-selection-mode’. Note that you can use the Tab key to get completion for the variable name. Emacs will now show the current value of this variable and offer you the option to customize it. Click in customize with your third mouse button (or but the cursor on it and press Enter). You can now toggle the value so it is enabled. Click on “Save for future sessions” to save it permanently.

This makes Emacs behave more like a Windows application. But still Ctrl+V will note paste, Ctrl+C will not copy, etc. For this you need to enable cua-mode. I don't think this is available on Unix, and enabling it will interfere greatly with your ability to use Emacs on different OSes, but for hardcore Windows users it might be an option. CUA mode can be enabled by selecting it from the Options menu, or typing in C-x cua-mode. Afterwards C-x won't do what it normally does of course. Use Esc x instead.

We'll do the same to enable syntax highlighting. We will enable this for all modes. Press C-h v. Enter ‘global-font-lock-mode’. Again use the Tab key to avoid typing too much. Click on “Customize” again and “Toggle” it to on. Save it for all future sessions.

Installing the Eiffel mode

Emacs doesn't natively come with an Eiffel mode, but it's easy to install one. Here's how:

  1. Download Martin Schwenke's eiffel.el.
  2. Copy it to your site-lisp directory which is something like /usr/local/semacs/site-lisp/.
  3. Load the file with C-x C-f (Control+x followed by Control+f) or choose “Open File...” from the File menu and type in the path. Note you can press the Tab key to get path completion so you have to type less.
  4. From the menu click on Emacs-Lisp and select “Byte-compile And Load”.
  5. You can bind your Eiffel mode to become activated when you load a .e file. Add the following to your ~/.emacs file (or create a new one):
    (require 'eiffel)
    
    ;; the following extension are recognized as Eiffel files
    (add-to-list 'auto-mode-alist '("\\.e\\'" . eiffel-mode))
    (add-to-list 'auto-mode-alist '("\\.ge\\'" . eiffel-mode))
    
    ;; in case all your Lex/Yacc is done with Eiffel, define this
    (add-to-list 'auto-mode-alist '("\\.y\\'" . eiffel-mode))
    (add-to-list 'auto-mode-alist '("\\.l\\'" . eiffel-mode))
    
    Next time you open a file with one of the four listed extensions, the Eiffel mode will be activated.
    Note that “~” in Emacs (and Unix) is shorthand for ‘my home directory’. The .emacs file is the default file Emacs will always read and interpret (because it contains Lisp code) at startup.

gutentag

Emac's completion support is static, not dynamic. So it does not compile your code on the fly and add new features. That's a disadvantage, but an advantage as well. If your code doesn't compile anymore, Emacs will still have completion support, while ISE Eiffel and Delphi for example first need to be able to compile the code before they enable programmer support again.

You will need to download gutentag for completion support. Usage is simple. If you have a system.xace file in your directory, simply say:

gutentag system.xace

When it finishes, you will have a TAGS in that directory. You can load that in Emacs with M-x visit-tabs-table. After it has been loaded, you have some new features:

  • M-. (Alt + ‘.’ character on a PC keyboard): jump to the definition of a feature or class. Use the tab key to avoid typing in the full name.
  • You can now complete a class name or feature name by pressing M-Tab (Alt + Tab). Or calling the function complete-symbol or complete-tag.

Customizing the Eiffel mode

Tips on how to customize the Eiffel mode.

Emacs speak

Emacs has its own terminology and it is sometimes different from what we're used to nowadays. That's because Emacs predates most modern software we use.

  • Point: where the cursor is.
  • Buffer: a buffer in Emacs is a place where you edit something. A buffer can be bound to a file, or it can be a scratch buffer, never saved at all.
  • Mode: how Emacs behaves when editing a file. They come in major and minor modes. The Eiffel major mode helps you to edit Eiffel files, the nxml major mode helps you to edit XML files. The flyspell minor mode does spell-checking in the currently active major mode.
  • Region: selected area in a buffer.
  • Frame: this is what we would call a window on our modern desktops. Emacs has at least 1 frame open, but can have multiple ones. Press C-x 5 2 to open another frame, showing the current buffer.
  • Windows: each frame can be split into sections that Emacs calls windows. Press C-x 2 to split the current frame into two windows. Very useful to look at the same file, but at two different location at the same time.

Useful keybindings

Some useful keybindings:

  1. C-h m: usually lists all keybindings for the current mode and all active minor modes. Some modes also give helpful comments as “There are approx. one gazillion commands you can execute in this buffer”.
  2. C-h k: when pressed it waits for you to press a key or a combination of keys and next gives you the macro definition to which this key or key sequence is bound.

Emacs custom key bindings

You probably want to do some custom key bindings as well. The way to do that is to put some Lisp code in your ~/.emacs file. The following example shows how to bind the keys on your numeric keypad, useful for programmers who are coming from the original IBM PC area:

(global-set-key [C-kp-left] 'backward-word-nomark)
(global-set-key [C-kp-right] 'forward-word-nomark)
(global-set-key [C-kp-home] 'beginning-of-buffer-nomark)
(global-set-key [C-kp-end] 'end-of-buffer-nomark)

Emacs doesn't use any of the function keys, so these are ideal for your custom key bindings. I like to comment and uncomment code with a single key press, and that's what the following key bindings accomplish:

(global-set-key [f3] 'comment-region)
(global-set-key [f4] 'uncomment-region)
(global-set-key [f7] 'goto-line)

The binding to f7 in the example above allows you to jump to a given line.

You can also bind keys to custom Lisp code. The following example demonstrates scrolling lines up and down in your buffer using the f11 and f12 keys:

;; one line at a time scrolling
(defun scroll-n-lines-up (&optional n)
  "Scroll up N lines (1 by default)"
  (interactive "P")
  (scroll-up (prefix-numeric-value n)))

(defun scroll-n-lines-down (&optional n)
  "Scroll down N lines (1 by default)"
  (interactive "P")
  (scroll-down (prefix-numeric-value n)))

(global-set-key [f11] 'scroll-n-lines-up)
(global-set-key [f12] 'scroll-n-lines-down)

Other Emacs customizations

Tips on how to change the default font in Emacs.

Other useful Emacs modes

Some useful and very powerful other modes:

Emacs is a walrus tangoing with you

In an amusing Usenet thread, Alan Mackenzie claims that:

Emacs is decades old rather than months old, and it has been honed to gleaming efficiency in that time.

David Kastrup, of AUCTeX fame, objects:

Uh, gleaming efficiency? Sorry to disagree, but Emacs is a traveling junk yard and freak show. A junk yard which has got everything, and building materials for building everything else. It's not as much "honed" rather than having lots of people making it their home and improving their personal corner of the junk yard. People are always running around with soldering irons and swapping their favorite pieces of scrap and construction recipes. It is a gathering ground for Mad Scientists(TM) in the text processing area.

And on Alan Mackenzie's claim that he finds most other editors clumsy, David opines:

They are not necessarily clumsy. Just not accommodating. Emacs is probably the clumsiest and most dissociated piece of software ever. But it works with you, lives with you. It's a walrus tangoing with you, following your lead like a feather. If you have learnt how to properly lead and don't make it flap on your feet.