[home]

DAT 501 [unix4]
Laboration: Introduction to Emacs

This laboration aims to aquaint you with the editor ‘emacs,’ which will be your main tool for writing programs both in SML and Java. Emacs handles all sorts of other languages as well, and is generally considered to be the most powerful all-purpose editor available. Emacs is free and runs on many platforms.

There is a newly installed version of emacs on the student machines, and I recommend to use that one (it is started by typing ‘emacs’). To make things comlicated, there are several different variants of Emacs around, and some people will recommend to use the version called ‘xEmacs.’ We won’t stop you—you can use any emacs (and indeed, any editor) you want in the course, but for this particular lab you need to use emacs. (Be aware that some people have strong opinions about with editor to use, and whether or not xEmacs is an emacs or not. Some people use textedit for their programming exercises. Some use vi. I don’t care.)

The first part of this lab asks you to follow emacs’ own interactive tutorial. After that, you have to solve a number of simple editing exercises (in emacs) as well as a programming exercise (in any editor you want). Solving the latter is the requirement for ‘godkändt.’ If you need more help, ask your lab assistant.

Tutorial

Exercises

Basic editing

SML Mode

Now we set up your emacs as an SML editor. Whenever you do changes to your emacs (like preparing it for SML), these changes go into a special hidden file in you home directory, called

~/.emacs

If you are a new student then this file probably doesn’t exist yet. You can inspect it (‘cat ~/.emacs’), but probably it’s empty or not even there yet. We’ll change that now.

Insert the contents of the file

/usr/kurs/dat501/share/emacs/site-lisp/site-start.el
into your .emacs-file. (How? By now you should know a number of ways of doing that. Otherwise ask you assistant.)

Quit and re-start emacs to force it to read the file.

Open a new file (preferably in the directory for this lab session) called my-first-sml-file.sml. Note that emacs’ mode line (at the bottom of the window) indicates that the current buffer is in SML mode, and note that there is an SML menu entry.

Type a short SML-program, for example the following

fun gcd(m,n)=
if m= 0 then n
else gcd(n mod m, m);

If everything is set up correctly, you should see ‘syntax highlighting’ in action: for example, the keywords ‘fun,’ ‘if,’ and so on are displayed in a different colour. Even better is the TAB key (the key to the left of letter ‘Q’): placing the cursor anywhere in the second line and hitting TAB should properly format that line.

Now hit ‘C-c C-s’ to start the SML subproces. The minibuffer (at the bottom) asks for the name of the SML system to use, and should recommend Moscow ML (which happens to reside in ‘/usr/local/mosml/bin/mosml’)—confirm by hitting RET. You are now running the interactive system you usually start from the terminal, but the emacs version is much more convenient. For example, you can edit your commands using the arrow keys, and you can even move up in the buffer to repeat a previously typed command.

Typing ‘C-c C-l’ in either buffer (‘my-first-sml-file.sml’ or ‘*mosml*’) will now load the gcd function you just wrote in the Moscow ML system. If you prefer menus, then you can find this command in the SML menu, under ‘Process.’ Try it, and then evaluate gcd(36,116). Show this to your assistant.

If you make an error when typing (try it by writing ‘ged’ instead of ‘gcd’ in the third line of your program) then the ML system of course complains as usual. The command ‘next-error’ will but the cursor in ‘my-first-sml-file.sml’ at the right place. This is extermely pleasant as soon as you write programs with more than just a few lines—and more than just a few errors.

You invoke ‘next-error’ either from the SML menu, or by typing ‘M-x next-error’ or, with ‘C-x `’ That is a backwards quote, or a grave accent, which is extremely convenient on English keyboards (hit the key left of Z), but extremely inconvenient on a Swedish keyboard (where you need two hands, and the SHIFT key). If you plan to do a lot of errors (and you will), why not customise your emacs a bit by adding the following line somewhere to your .emacs-file:

(global-set-key "\C-x n" 'next-error)
Now next-error is bound to ‘C-x n’. Much easier to remember for me.

Programming and documenting

Fun

Your are finished. If you are bored, try ‘M-x dunnet’. If you are frustrated, try M-x doctor.