Guile and Emacs together


Note: This is not strictly necessary. You can edit your Scheme files in any editor, and run them in Guile by loading that file. This does, however, make things very nice when working with larger non-trivial Scheme code.

Setup


To run Guile within Emacs, follow these directions:

Find out if you already have a .emacs file, and back it up if you don't want to lose it.

 cd 
 ls -l .emacs 
 cp .emacs .emacs-backup 

Make a directory called elisp in your home directory. This is the conventional place to put certain Emacs personalization files.

 mkdir elisp 

Copy this file (named guile-interaction-mode.el) into that directory. A convenient way to do that would be to copy it from my directory:

 cd elisp 
 cp ~jkh/elisp/guile-interaction-mode.el . 
 ls -l guile* 
 cd .. 

Now change your .emacs file so that it contains the following Emacs-Lisp code: (load "~/elisp/guile-interaction-mode.el") One way to accomplish this is to edit your existing .emacs file, and insert this at the end. Or if you don't have a .emacs file, you can get one here. (This file has the proper load command in place.) Yet a third option would be to copy mine from my directory:

 cp ~jkh/.emacs . 

The goal is for you to have a .emacs file that loads your ~/elisp/guile-interaction-mode.el file when you run Emacs.

As a final setup note, the UNS people here in the department have set up an example .emacs file located at /uns/examples/tutorial.dot.emacs. This file may contain things that customize Emacs in a way that you would like. If you choose to use this file, you could insert the Emacs-Lisp code (load "/uns/examples/tutorial.dot.emacs") at the beginning of your .emacs file. Or you could just paste the parts you think are interesting into your .emacs file. Just make sure that the guile-interaction-mode.el file is loaded last, so Guile interacts properly.


Usage

Summary: This is a sample run-through that creates a simple Scheme file and interacts with it using Guile.


Open Emacs in a new window. If you're using TeraTerm or putty, you will probably want to specify the -nw flag.

 emacs & (OR, ALTERNATIVELY) emacs -nw &  

In Emacs, open a new Scheme file named tutorial.scm.

 C-x C-f tutorial.scm 

Enter this Scheme code into the editor. Notice that when you go to a new line, Emacs automatically indents your Scheme code. You can also press TAB to indent an existing line.

(define (third l) 
(car (cdr (cdr l))))

Save the file to disk.

 C-x C-s 

Now we are going to open Guile within Emacs, so we can interact with it and test out our new code. The first step is to split Emacs into two-buffer mode. The new buffer will show the same buffer (tutorial.scm), and will be in the lower half of Emacs.

 C-x 2 

You will notice that the cursor is in the upper buffer, and that you can switch the cursor between buffers using the mouse. From the keyboard, you can switch the cursor between buffers using

 C-x o 

Practice switching the cursor between the buffers, editing, and saving the file. Then, with the cursor in the bottom buffer, open Guile within the lower buffer with this key sequence: (Remember, M means Meta, which is usually Escape, but sometimes Alt)

 M-x run-scheme 

The lower buffer should now have a guile> prompt. You have now opened an interactive Emacs buffer, with Guile running as a sub-process. You will want to load your tutorial.scm file into Guile. There are three methods of doing this. Method 1: Put the cursor in the Guile buffer. At the guile> prompt, type

 (load "tutorial.scm") 

Method 2: Ask Emacs to load the file for you. This is done with the command

 C-c C-l 

Both of these methods loaded the whole file tutorial.scm (and all the methods within it) into the Guile session. If you only want to evaluate individual S-expressions within this session, use Method 3: Put the cursor in the buffer where your Scheme code is. (C-x o) Move your cursor so that it is just after the last closing parenthesis of the S-expression you're interested in evaluating. With the cursor in place, type

 C-x C-e 

This command asked Emacs to send the preceding S-expression to the Guile session, for evaluation. This is a useful way to quickly write a piece of code and then test it. For example, in your tutorial.scm buffer, type in this code, and then evaluate it by placing the cursor just after it, and use the C-x C-e method of evaluating it in Guile.

 (third '(1 2 3 4 5)) 

The S-expression should have been sent to Guile for evaluation, and the number 3 should have appeared in the Guile buffer. Try

 (third '(1 2)) 

This should fail since (car '()) doesn't work. Of course, you can also enter S-expressions in the Guile buffer, where it interacts in the usual fashion. One difference between operating Guile standalone versus under emacs is asking it to repeat the last S-expression. In Emacs, under the Guile interactive buffer, this is accomplished through the command

 M-p 

Repeatedly pressing M-p will cycle through the most recent S-expressions you have entered in the Guile interactive buffer.

One thing to remember is that the code in turorial.scm will be around after the Emacs session, while code entered directly to Guile will not (unless of course you cut and paste it.) When you are satisfied, put the cursor in the tutorial.scm buffer, save your work and exit Emacs.

 C-x C-s

C-x C-c