Using Emacs and STk

Setting up

In order to use STk from Emacs, you should place the text

   (load-file "~cslab/.emacs")
in ~/.emacs, your Emacs init file. You can do this by typing:
   echo '(load-file "~cslab/.emacs")' > ~/.emacs

Starting Emacs

To start Emacs, just invoke emacs. If your $PATH is not set properly, invoke ~cslab/emacs-19.34/bin/emacs. When Emacs starts it will pop up a window:

You should read the blurb it contains. The version you are running should be on the top line: make sure it is 19.34. Now look where it says ``Type C-h t for a tutorial on using Emacs.'' That sounds like a good idea, doesn't it? The tutorial will work best if you resize the Emacs window to 80x24 so one window-full of text matches one page of the tutorial.

If you don't use the tutorial quite yet you may be able to muddle along using:

Emacs commands

A one-page emacs reference card is available from the instructors, or here in postscript form. But for the extraordinarily lazy, here are a few key ones:
C-x C-f
find file
C-x 4 C-f
find file and split window
C-x 4 b
split window with another buffer
C-x C-s
save file
M-x run scheme
start scheme
indent current line (relative to prev)
C-c C-e
send command around cursor to scheme
C-x C-c
quit emacs

Starting STk

To start scheme, use the run-scheme command in Emacs.

To spell it out, go ESC x r u n - s c h RET. Your Emacs should look like this:

If there are some error messages now, or when you load turtle.stk as described below, you are probably not loading the site-local initialization file we set up. If you have your own initialization file, then ours won't get loaded. You can either

   rm ~/init.stk
or add (load "~cslab/Tk-3.1.1/lib/stk/3.1.1/STk/init.stk") to your ~/init.stk.

Using STk

You can type stuff in the *scheme* buffer: type
  (+ 1 2) RETURN
and your screen should look like this:

Now type C-x 4 C-f foo.stk RET to make a new file, and insert the text

(+ 1 (+ 2 (+ 3 (+ 4 (+ 5 (+ 6 (+ 7 (+ 8 (+ 9 10)))))))))
into it. Save the file with C-x C-s. Your emacs should now look like

Now put the cursor in the middle of that expression and type C-c C-e. Your emacs should look like this

Note the answer: 55 !

The stk window

You're probably wondering what the window positioned on the right hand side of Emacs is. It's for graphics.

For now, we're going to use ``turtle graphics,'' a cute way of doing graphics invented by Seymore Papert at the MIT AI lab. To get the turtle graphics package into stk you have a load the turtle package, via

    (load "~cslab/CS257/turtle.stk")
to the stk prompt, or C-c C-l ~cslab/CS257/turtle.stk

Now you have access to some new procedures. You can figure out what they do from their names and by playing around.

    (move distance)
    (moveto x y)
    (turn degrees)
    (turnto degree)


    (colour num) ; default is 0; num between 0 and 15;
		 ; different stipple patterns

    (width num) ; default is 0; 10 is about an inch wide

    (write "text")

    (screen-dump "")

If you can't stand the British spelling, do this:

    (define color colour)

Good advice

Comment liberally

Comment your code liberally, if you feel like it. In scheme comments run from a ; to the end of the line. By convention
;;; Triple semicolons are used for
;;; block comments on the left margin,
;;; for instance in file headers and
;;; before a definition to explain what
;;; the thing about to be defined will
;;; do.

(define (fact n)
   ;; Double semicolons are used for comments
   ;; that should be indented the same amount
   ;; as the following line of code.
   (if (= n 0)
       1                       ; single semicolons are on the right
       (* n (fact (- n 1)))))  ; like this.
Emacs knows this convention. M-; move to the comment on the current line, creating one if necessary.

Don't bash your head against a wall

Get a little help from your classmates, you'll all enjoy it.

Fool around with Scheme

Get this stuff figured out now and it won't get in your way later. Emacs is the best editor ever for writing computer programs, in almost any language. Work through its tutorial, later in your career you won't be sorry.

And be sure to turn in your problem set via e-mail to before it's due (5pm Tues Sep 2).

An ideal problem set handin can be fun to look at.

Barak Pearlmutter <>