Editing Scheme files

The XEmacs text editor

In the previous lab, you typed each command directly into the Scheme interactive interface, which immediately evaluated and printed the result. Although this way of interacting with Chez Scheme is straightforward, it has two important disadvantages: (1) The interactive interface provides only very primitive editing features. If you commit a trivial typing error, you can correct it only if you're still on the same line, and then only by erasing all the characters to the right of the error. (2) The interactive interface does not automatically save any of the commands and definitions that you type in. Hence, if you want to extend or modify a program that you worked on in a previous session, you have to type it all in again before starting the new part of the work

To get around these problems, we shall usually develop our programs with the help of another program, a text editor, the purpose of which is to collect, save, and display text and to enable the programmer to modify and correct her texts easily. Our text editor is called XEmacs. Unlike Chez Scheme, XEmacs runs in a window of its own. Often it is convenient to work back and forth between two windows, side by side on the screen -- a dtterm window with Chez Scheme running in it, so that one can submit definitions and commands to the interactive interface, and an XEmacs window in which one can conveniently write and save the text of the program one is working on.

To start up an XEmacs window, click on the pencil-and-paper icon on the front panel. XEmacs does not start instantaneously, but within fifteen seconds or so the window will appear.


Exercise 1

Go ahead and start an XEmacs window now. You don't need to pay any particular attention to the text that is initially displayed in the window. It will vanish as soon as you press a key.


XEmacs stores texts in files. A file, in computer jargon, is a body of information held on some long-term storage device with which a computer can communicate. To start a new file, initially empty, or to ask XEmacs to display an existing file, move the pointer up to the toolbar (the row of icons near the top of the XEmacs window) and click on the dogeared-paper icon at the left end, the one with the word Open underneath it. A dialogue box pops up, with the names of various editable files displayed above and a prompt at the bottom that looks like this:

Find file: ~/

Move the pointer into the dialogue box and type in the name of the file you want to create, then click on the button marked OK, at the bottom of the dialogue box. Conventionally, Scheme programs are stored in files with names ending in .ss -- first-test.ss would be a typical name for a file containing a Scheme program.


Exercise 2

Direct XEmacs to open a file named first-test.ss.


Often, it is convenient to change the position of the XEmacs window on the computer screen. To move the window, start by moving the pointer to the top of the window frame, onto the bar labelled emacs:.... Press and hold down the left mouse button while you move the mouse. An outline of the XEmacs window will follow the movements of the mouse. When this outline is where you want the XEmacs window to be, release the left mouse button, and the XEmacs window will jump to that position. A dtterm window can be moved around in the same way. Before settling in for a long editing session, move your windows around until you're satisfied with their locations.


Exercise 3

Move your XEmacs window to a different position.on the screen.


Exercise 4

If you don't yet have a dtterm window running, start one. Then move both windows around until you can see at least part of each one.


XEmacs is an extremely powerful editor containing hundreds of different operations, of which we'll need only a few:


Exercise 5

Fool around with the insertion, deletion, and cursor-movement operations in XEmacs for a little while, until you start to feel comfortable with them.


Exercise 6

Use the Cut operation to delete everything you've typed, leaving the window empty.


The most commonly used capabilities are accessible through the icons in the toolbar and the menus named on the menu bar (just above the tool bar). You can read a one-line summary of the effects of any of the toolbar icons by moving the pointer onto that icon and reading the one-line description that appears at the bottom of the XEmacs window.

If XEmacs ever starts to do something that you don't understand, or if you have attempted an operation that you want to cancel, pressing <Ctrl/G> repeatedly is usually an effective way to recover control; it cancels any partially completed operation.

To exit from XEmacs, closing the window, activate the File menu (by clicking on the word File at the left end of the menu bar) and click on the Exit XEmacs item at the end of the menu that pops up. If you try to exit from XEmacs without saving the file that you're working on, XEmacs will produce a dialogue box asking you either to save the file or to confirm that you want to discard any changes you've made in it.

Editing and saving a Scheme program


Exercise 7

Using XEmacs, type the following Scheme program into the first-test.ss file. (If you exited from XEmacs at the end of the last section, you'll have to start a new XEmacs window and open the first-test.ss file again.)

(define pi 3.14159)
(define square-root sqrt)
(define a (square-root 121))

(The last one is a definition with a procedure call inside it; the effect is to have Chez Scheme compute the square root of 121 and define a as a name for the result.)


As you are typing, note that when you type a right parenthesis, XEmacs momentarily moves the cursor back to the matching left parenthesis. Since longer Scheme definitions often contain many pairs of nested parentheses, this graphic convention makes it much easier to edit Scheme programs.

To save a text that you've typed in some file that XEmacs has opened for you, click on the Save icon on the toolbar -- the one that depicts a diskette. (Files are actually saved, not on a diskette, but on a hard disk located in the basement of the Science Building. Every MathLAN workstation is allowed to save and recover files on this hard disk.) When it has saved the text in a file, XEmacs reports the completion of the operation in a message at the bottom of the XEmacs window, like this:

Wrote /home/spelvin/frogs.ss

Exercise 8

Have XEmacs save the definitions (the ones you typed in when you did exercise 7) in the first-test.ss file.


That concludes the part of the job that the text editor can do. Now we need to persuade Chez Scheme to recover the Scheme program from the file in which it is stored and to execute it.

Loading a program into Chez Scheme

Chez Scheme provides a procedure named load which, when called, causes Chez Scheme to open a file and quickly read the program that it contains, memorizing each definition and executing each command. The load procedure takes one operand, a character string giving the name of the file in which the program is stored.

The term characters refers generically to letters, digits, punctuation marks, and such like -- the lowest-level constituents of text. A character string is a sequence containing any number of characters, ``strung together'' in a linear order. In Scheme, a character string is usually written just by placing the character sequence between double quotation marks; for instance, the string consisting of the three lower-case letters c, a, and t, in that order, is written in Scheme as "cat". (Without the quotation marks, Scheme would incorrectly interpret cat as a symbol like sqrt or pi and expect it to stand for something inside the language.)

Here, then, is what a call to the load procedure looks like. This one expresses the command ``Load the file named frogs.ss!''

(load "frogs.ss")

The parentheses enclose the procedure call and contain the name of the procedure, load, and its operand, "frogs.ss".


Exercise 9

Start Chez Scheme running in your dtterm window. (If you don't have a dtterm window running, start one.)


Exercise 10

Direct Scheme to load first-test.ss. (You won't see any response immediately, because the file has only definitions in it.)


Exercise 11

Confirm that Chez Scheme has memorized the definitions in first-test.ss by giving it the following commands, which depend on those definitions:

(+ pi 1)
(square-root 49)
(* a a)


If you get sensible responses in the last step (presumably 4.14159, 7, and 121, respectively), the definitions were read correctly. If Chez Scheme had not seen these definitions, you would have received advisory messages looking like this:

Error: variable pi is not bound.
Error: variable square-root is not bound.
Error: variable a is not bound.

This is how Chez Scheme reports that it doesn't know the meaning of a name that you've tried to use. (For instance, you might get errors like these if you forgot to have XEmacs save first-test.ss before trying to load it into Chez Scheme.)

Recording a Scheme session

Sometimes it is useful to have a transcript of a session with Chez Scheme, including not only the commands and definitions that you type into Chez Scheme but also the responses, advisory messages, and so on. For this, we need yet another computer program: submit, which copies into a file everything that is displayed in a dtterm window. One common use of submit is to prepare a log containing the source code for a program and output from one or more test runs.

To start submit in a dtterm window, think up a name for the log file that you want to create (say, frogs.log) and type

submit frogs.log

at the prompt. The file must be one that does not already exist; if you happen to pick some name that identifies a file that already exists, submit will require you to delete that file yourself or to choose a different name for your log file.

When it starts up, submit prints an advisory message that looks something like this:

Script started, file is /tmp/submit-spelvin

This means that you're now recording. The shell will produce another prompt; you can go ahead and start Chez Scheme at this point. Everything that appears on the screen will be transcribed into the log file.

When you're ready to exit from Chez Scheme, press <Ctrl/D> as usual. The shell prompt will return. Now press <Ctrl/D> again, to shut down submit. You'll get a two-line acknowledgement:

Script done, file is /tmp/submit-spelvin
submit: created log file frogs.log

The file frogs.log now contains the complete transcript. You can print it, if you like, by typing

print frogs.log

at the shell prompt. One of the laser printers in the southwest corner of the MathLAN lab will print the file.


Exercise 12

Exit from Chez Scheme, if you have not already done so. In the dtterm window, use submit to start a log file called first-test.log.


Exercise 13

Restart Chez Scheme, have it load the first-test.ss file again, and direct it to compute the result of dividing a by pi.


Exercise 14

Exit again from Chez Scheme, exit from submit, and print the log file that submit created. Go over to the printer and pick up your transcript.


Exercise 15

Exit from XEmacs (by selecting Exit XEmacs from the File menu) and log out.


This document is available on the World Wide Web as

http://www.math.grin.edu/courses/Scheme/spring-1998/editing-Scheme-files.html

created January 13, 1997
last revised June 21, 1998

Henry Walker (walker@math.grin.edu) and John David Stone (stone@math.grin.edu)