Editing a File

One of a series of tutorials about Scheme in general
and the Wraith Scheme interpreter in particular.

Copyright © 2011 Jay Reynolds Freeman, all rights reserved.
Personal Web Site: http://JayReynoldsFreeman.com
EMail: Jay_Reynolds_Freeman@mac.com.

Programmers spend a whole lot of time in what often seems to be an endless loop of writing code, testing it, and fixing bugs. A programmer's first reaction to Wraith Scheme is likely to be, "How can I do that with this program? Wraith Scheme doesn't have a built-in editor!"

The answer is that Wraith Scheme is set up to use any editor you like, and what's more, it does so very efficiently, without you having to do a lot of moving back and forth between your favorite editor and Wraith Scheme. Here's how.

My favorite programming editor is the venerable EMacs, and I hope you will forgive me for choosing it. (In case you didn't know, innumerable fights and flame wars have grown out of "discussions" about which programming editor is best. There are even flame wars about how to capitalize "EMacs", so I won't even try to tell you why I do it that way.) Anyway, here is part of a screen shot showing my basic write-and-test-code setup -- I have EMacs running in a Macintosh "Terminal" window in the foreground, with Wraith Scheme behind it.

Editing Setup

I have already written some Scheme code in the window, and saved it out to my desktop as "Test.s". The code is supposed to define a new procedure, named "inc", that takes one argument, "n", and returns n plus one. Unfortunately, there are a couple of problems with what I have written, but it is 4 AM and I am way too tired to notice them, so I press on regardless.

The next thing I want to do is load my file into Wraith Scheme so that I can check out my code. The first time I do that, I have to go through the usual Macintosh file-selection stuff, but -- getting ahead of myself -- I only have to go through the whole rigamarole once. I bring Wraith Scheme to the foreground, pull down the "Wraith Scheme" menu, and select the "Load File" item.

Menu down, and selected.

The usual select-a-file panel pops up, and I navigate to my file.

File selection dialog.

When I press the "Open" button, Wraith Scheme tries to load the file, and behold, there is a problem!

Oops #1.

My parentheses weren't balanced. Shameful!! So I go back into EMacs, and add the missing parenthesis, and save the file. What is important now is that I don't need to use the "Load File" menu item and the select-what-file panel any more. Look at the Basic Buttons Drawer, which is attached to the left side of the Wraith Scheme main window. A couple of buttons in it have become enabled, that were not enabled before I had loaded a file. I press the "Reload Last File" button, which is where the arrow cursor is in the next image ...

Load Last File.

... and as shown below, Wraith Scheme reloads my file, as modified.

Second load.

The load was successful: The first error message, left over from the first try, is still there, but there are no new error messages. Furthermore, Wraith Scheme has printed out the results of executing the Scheme statements in the file; the "define" statement returned the name of the procedure that I am trying to define, "inc", and that name is there, as the next to the last line typed in the Main Display Panel. So far, so good.

(Incidentally, the "#t" in the very last line of the Main Display Panel is the result of the "Reload Last File" operation itself.)

Unfortunately, when I test my new "inc" function, it doesn't work.

Oops #2.

I tried to increment 41, but instead of getting 42, I got an error message about an "m" being undefined. The problem is that I mistyped "m" for "n" in the part of my procedure where I am adding one to the argument -- the expression there is "(+ m 1)", and it ought to be "(+ n 1)". I must be really tired. It is still a long time before sunrise, and I am probably going to make more errors, and I don't want to waste time pressing the "Reload Last File" button over and over, so I will now press the "Automatically Reload Last File After You Save Changes" button.

When Updated.

That button stays down after you have pressed it. (You can press it again to make it release.) This button is an accessory to the "Reload Last File" button: "Automatically Reload Last File After You Save Changes" is exactly what I want to have happen. Now I can go back into EMacs, change the errant "m" to "n", and the moment I save the changed file, Wraith Scheme will notice the change and load the new version automatically.

It is difficult to show that happening without a video, but here is the result. I have changed "m" to "n" and saved the modified file. Wraith Scheme has reloaded it, as you can see from the new printout of "inc" as the (new) next to the last line in the Main Display Panel.

When Updated (down).

It's kind of fun watching this happen: You might have your edit window in the foreground and the Wraith Scheme window partially visible behind it. When you save a file, you can watch Wraith Scheme out of the corner of your eye, and see it scroll the Main Display panel as it does the reload.

The new version of "inc" now works. I can now go on modifying my file all night, and every time I save out changes, Wraith Scheme will reload the file for me, without my even having to leave the editor window. If I am really lazy, perhaps I can even include some test procedures in my file of code, that will test the procedures that I am trying to write. With that arrangement, I will only have to glance at the Wraith Scheme window after each automatic load, to look at the results of the tests and see whether my procedures are working correctly.

Just for kicks, here are the two windows after I have added a new procedure, "inc-twice", to the file, and saved it. Wraith Scheme has done another reload, and the names of both procedures are printed out near the bottom of the Main Display Panel.

Two procedures.

Once again, the main points of this tutorial are that

In my opinion, these capabilities are a major feature of Wraith Scheme. I hope you will try them out, and that you like and use them as much as I do.

All right, all right, as a reward for reading this far, I will go ahead and tell you what EMacs stands for, and explain why I capitalize it the way I do. "EMacs" stands for either "Edit Macros" or "the Editor of project MAC", and no one seems to be sure which. I learned about "Edit Macros" before I had ever heard of project MAC, so I am in the habit of capitalizing it as if it stood for those two words. Project MAC was an MIT computer project several decades ago, that had a lot to do with artificial intelligence. "MAC" stood for either "Machine-Aided Cognition" or "Man And Computer", and that ambiguity was intentional, because people thought it was cool.

And yes, computer types really do get into flame wars about silly things like how to capitalize "EMacs". Don't ask me how I know.

-- Jay Reynolds Freeman (Jay_Reynolds_Freeman@mac.com)

Wraith Face