Introduction to Emacs
We suggest using the emacs editor, especially for Scheme
programming. It is very powerful and allows Scheme to be run within
the editor.
To enter emacs, at the Unix shell prompt enter emacs followed by the name of the file you would like to edit. For example, type
emacs test.ssGiving emacs a filename, such as test.ss causes the given file to be loaded for editing if it exists. Otherwise, emacs creates a buffer for composing the file. Since the file test.ss does not yet exist, you now see a blank window with the cursor at the upper left corner and a banner at the bottom displaying Emacs and some other information such as (Scheme), indicating that the editor is in its Scheme mode.
Now type
(car '(one two three))again and observe that when you close each pair of parentheses, the cursor bounces to show you the matching opening parentheses. The expression is not being entered in Scheme itself, so you will not see the result of its evaluation, but it can be sent to Scheme at any time, as we shall see. Press DEL (the delete key) and notice what happens: it erases the character to the left of the cursor.
We shall next learn to use the editing commands of Emacs. In Scheme mode emacs bounces the cursor for parenthesis matching and some other tricks that are great for Scheme programming, but not when you are typing prose (say in English). For that you want to be in text mode. We switch from the Scheme mode to text mode by pressing ESC (the escape key), releasing it, then pressing x. Note that M-x appears at the left end of the banner at the bottom of the screen. Now type text-mode (and then RET). In the future, typing such as sequence will be indicated by M-x text-mode. (M-x is read "meta-x". On some keyboards M-x may also be entered by holding down a meta or alt key while pressing x.)
Notice that now the word (Text) appears in the banner at the bottom of the screen. When you call up Emacs with a filename ending with the extension .txt, Emacs goes directly into Text mode, whereas when the filename has the extension .ss, it automatically goes into Scheme mode.
When entering emacs command names after the M-x (read "meta x") prompt, you can hit SPC (the space bar) to cause Emacs to show you all the current completions of the command you are entering. This can save you from typing the whole command.
To move forward over one word, use the command M-f. (Recall this is read "meta-f" and may be entered by pressing ESC and then f.) Next try M-b and watch the cursor move backward one word. You can abort a command or sequence of keystrokes with C-g. For example if you have typed ESC, but then decide that you want to move forward a character, enter C-g to abort the ESC keystroke, and then enter C-f. Try it.
Now enter C-e. The cursor moves to the right end of the current line. Entering C-a moves it back to the beginning of the current line. Two additional commands that you will find useful are C-v which moves ahead to the next screen and M-v, which moves back to the previous screen.
It works best to run Scheme within Emacs directly, with the command M-x run-scheme. This creates a buffer in which Scheme runs directly, and you can use Scheme just as you would outside of Emacs.
Below is a summary of the Emacs commands used in this tutorial. You will have a chance to use them in the assignment which follows the summary.
(define cat 'cheshire)When you typed the right parenthesis, it bounced back and matched the left parenthesis. In this way, you will always be able to tell which pairs of parentheses have been properly closed. At this point, let's save the Emacs buffer to the file pets.ss by entering C-x C-w. You can tell that it has been saved by noting that the two asterisks are no longer at the left end of the banner line.
To start up a Scheme process in one of the two windows, enter M-x run-scheme. We should now have our file pets.ss in one half of the screen, and Scheme in the other. This is probably the way in which you will prefer to do your work. Try switching between the Scheme window and the file window using C-x o.
We load the file pets.ss into Scheme by typing the expression (load "pets.ss") at the Scheme prompt. This causes Scheme to read and evaluate the expressions in the file just as if they had been typed in at the prompt. We call this loading the file. Now enter cat in response to the Scheme prompt. (Do not quote the symbol cat this time.) Instead of a message indicating an unbound variable, cheshire is returned. We have bound the variable cat to the value cheshire and when Scheme evaluated the atom cat, it returned its value.
Now to get back to the file pets.ss, type C-x o again. Use C-n or the down-arrow to skip a line below what has been typed, go to the beginning of the following line and then type
(define dog 'beagle)Save the buffer again using C-x C-w and return to Scheme using C-x o. In order for Scheme to know the new definition, we must load the file again. You may load the file pets.ss again by either typing the statement again, or by using the arrow keys to move up to the previous line where you loaded the file. Move the cursor to the end of the line (did you remember C-e?) and then press RET. Emacs should have copied the load expression to the last prompt, causing Scheme to evaluate the load expression again, just as if you had typed it again. Respond to the Scheme prompt with dog. We get the answer beagle. Now go back to the pets.ss buffer using C-x o.
(define couple (cons cat (cons dog '())))and observe that when you pressed RET to end the first line the second line was indented automatically. Also, as each right parenthesis was typed, the cursor bounced momentarily to the matching left parentheses. When typing expressions into Scheme directly (within Emacs), the expressions will also be indented. The TAB key may be used to re-indents the current line if the indentation is not right. To reindent every line of a definition, use the command C-x C-i with the cursor immediately after or anywhere within the definition. If there is any possibility that the parenthesis in a definition may not be right, use this command; if there is a problem with parenthesis the indentation will often look wrong.
Now save the file, return to the Scheme buffer, and load the file pets.ss. When you get the Scheme prompt, enter couple. You should get the answer (cheshire beagle). Now while in Scheme, enter
> (cons 'a couple)and you get the answer (a cheshire beagle) (What happens if you now type couple again?). Now kill the Scheme process by entering (exit). To get rid of the empty buffer on the screen, change to the other buffer with C-x o and type C-x 1 which will make the current buffer the only one on the screen.
We now return to Scheme to make a point. Type M-x run-scheme again. When you get the Scheme prompt, >, enter cat. You get an error message since the previous Scheme process was terminated and the variable cat is not bound to a value in the current process. Next enter
> (load "pets.ss")If we now enter cat, we get the answer cheshire.
> (define mylist
(cons 'one
(cons 'three
(cons 'five '()))))
If you type mylist in response to the next prompt, you
should see the list (one three five). Now press the
up-arrow key enough times to get back to the definition of
mylist. Using C-p, move the cursor up to the
end of (cons 'one. Press C-o. This opens up
a blank line below the line (cons 'one. You move to the
beginning of that blank line by pressing C-n and
pressing Tab will move the cursor under the ' in
'one. (You may also use C-j to open a new line
and move to the correct indentation automatically). Now type
(cons 'two. We move to the next line using
C-n and re-indent the line using Tab.
Pressing SPC to add spaces moves the line to the right, but
TAB or DEL will bring it back. Now move to the
next line and edit the 'five to be 'four and
indent the last line to be correct (go to that line and hit
<TAB>). You also have to add one right parenthesis at the end of
the last line to close all of the parentheses. Your screen should now
look like this:
> (define mylist
(cons 'one
(cons 'two
(cons 'three
(cons 'four '())))))
With the cursor at the end of the last line,
press <CR> to evaluate this expression. Now type
mylist and you should see the answer (one two three
four). This exercise was designed to show you how to edit in
Scheme itself instead of going into an Emacs buffer. The commands for
moving around in both are essentially the same, when Scheme is run
within Emacs. When Scheme is run outside of Emacs from the shell
prompt, it does not have any fancy editing commands.
Now move the cursor to the end of the expression, and press C-w. The lines between where the mark was set and where you pressed C-w should disappear, but they are not gone forever. They are in something called the Kill Buffer. If you now type C-y (yank from kill buffer) the lines will be restored.
Now go to the other buffer, pets.ss, and position the cursor at the bottom of the file. Press C-y again, and another copy of the expression will appear. You may copy expressions either way using this method, from one buffer to another, from Scheme and to Scheme, and within one file. This is analogous to the cut and paste operations many word processors provide.
We are now finished, and you may exit Emacs by typing C-x C-c.
Warning! Using the sample .emacs may redefine some of your old bindings. In such a situation, the best thing to do is to redefine the bindings at the end of the .emacs file.
Again, questions and are welcome. This document was put together using material from an earlier tutorial for c211.
Last Modified: Jan 12, 1995