Running Scheme in Emacs
After the first lab some of you were introduced to the Emacs text
editor (or its relative Xemacs). Emacs is a very versatile editor and
we'll be learning one of its capabilities in this short tutorial: how
to run a Scheme interpreter inside of Emacs.
Disclaimer: I've tried the modifications described here
personally on Emacs versions 21 (2nd floor lab) and 20 (4th floor
lab). I can't guarantee that it'll work on other versions, most
notably this may not work for XEmacs. If you want help,
visit me in office hours and I'll do my best!
Quick Setup
Here is the short list of instruction's for those of you who want
to get started in a hurry. An explanation of each step follows below.
- Open up emacs (or any other editor) in you home directory.
- Open up the file ".emacs" and add the following line:
(set-variable (quote scheme-program-name) "stk")
- Save the file. You only need to do steps 1-3 once. If you were
editing the file in Emacs, restart Emacs.
- Start up Emacs and type the following sequence of keys:
- M-x (which means hold the "Alt" key and hit "x"). At the bottom
of the emacs window "M-x" should appear.
- Type the command "run-scheme".
- A new buffer will open up with stk started inside of it.
What did I just do??
Here's a bit more detail about what just went happened.
- Emacs, like many other Unix programs, reads initialization and
customization information from a ".blah" file. Since the program is
name "Emacs" the initialization file is called ".emacs". These files
are in your home directory. They don't show up when a you do a "ls"
(for directory listing) most of the time because of the period at the
beginning of the name. The period (or dot) makes the files hidden on
Unix. To see all of them, type the command "ls -a" for a listing of
"all" files in directory, including hidden files.
- Emacs is built in a very interesting manner. The text editor is
actually built up using a variant of the Lisp programming language
called ELisp (for "Emacs Lisp" presumably). When you add the line
(set-variable (quote scheme-program-name) "stk")
to your
.emacs file, the line is read and the variable "scheme-program-name"
is set to "stk". Notice the structure of the command: it has
parenthesis just like Scheme, and the operator-first syntax of
Scheme. This is because Scheme is also a variant of Lisp. The "(quote
blah)" form will be something we talk about later in the class so keep
it in the back of your mind.
- Once you have added the above line to your ".emacs" file, you have
changed the program that Emacs will attempt to run for Scheme to "stk"
(I believe the default is MIT Scheme, another implementation of the
language).
- Emacs grew up back when there were no mice and great GUIs
(Graphical User Interfaces). This means all of the commands in Emacs
can be accessed from using only the keyboard. While this requires you
to learn a bit more about the editor to use it effectively, it ends up
being much faster in the long run than constantly using menus.
There are several ways to enter commands in Emacs. A number of them
simply involve using a sequence like "C-blah" which means hold the
control key then press the key blah. C-k will "kill" or cut all the
text from the cursor or point to the end of the line. C-y will "yank"
or past it back. Another way to enter commands is by typing "M-x"
which means hold the alt key (referred to as the "meta" key on most
Unix machines) and press the x key. Emacs then waits for you to enter
the name of a command. The one we're interested in at the moment is
"run-scheme" which will run scheme inside of Emacs.
- Once the "run-scheme" command has been entered a new buffer will
open up with scheme running inside of it. Buffers are the way Emacs
allows you to edit more than one file at once. Every time you open up
a new file it is opened in a new buffer. So if you open up a second
file, the first one is still in Emacs. You can look at it by selecting
it from the "Buffers" menu in the menu bar (there are other ways, but
that's easiest to begin with). The name of the buffer created by the
"run-scheme" command is (naturally) "*scheme*". When in this buffer,
the things that you type are interpreted as Scheme commands so after
typing something like "(+ 2 (* 3 5))" and hitting enter, the
interpreter will spit back "17".
- The nice thing about running Scheme inside Emacs is that you have
all the features of the editor and some nice extras inside of
Emacs. First off you'll notice your arrow keys work. You can cut and
past text or save the entire results of what you've been running (by
simply saving the buffer to a file name - this is much nicer than
using the Unix "script" command because you don't get all those funny
"^H" symbols when you type something wrong). Also, there are some nice
things Emacs provides, like history commands:
- Type "M-p" (hold alt, press p) to get the previous
command you typed into the interpreter..
- Type "M-n" to get the next command after typing one or more
"M-p"s.
How to indent Scheme code from within Emacs
Indenting Scheme code properly is very important to improve readability,
If you make corrections to your programs and you want to reindent
them in one step (as opposed to doing it line-by-line) here is
what you should do:
- click mouse at beginning of first line to re-indent and drag to last
line to re-indent, holding down mouse key
- on the keyboard type: <ESC> <Ctrl>-\ (an escape key
followed by control-backslash)
All in all, running Scheme inside of emacs is a bit nicer than the
command prompt. Additionally, you'll begin to learn about the Emacs
editor and some of the great things it can do to make your life
easier.
Cheers!
Chris Kauffman