Editing in SDK and Unix


Emacs

Creating and editing your program can be accomplished using a tool that allows you to work with files containing text (i.e., characters) and is thus called a text editor. The text editor used throughout this manual is named xemacs, a freely available text editor that is a collaborative effort of a number of companies and universities. Xemacs is a descendent of the original emacs text editor from The Free Software Foundation, and remains largely compatible with it; however xemacs provides a much nicer user interface for writing programs, including a menu bar providing pushbuttons for common editing commands, and a color-editing mode that shows keywords in one color, variable names in another color, comments in another color, and so on.

Our first program will input a number and then multiply it by 2, 4, and 8. We will thus name program Mult.java, and name byte code will be named Mult.class (Java source programs always end in .Java and object code always ends in .class.) We can use xemacs to create the source program by entering:

   % xemacs Mult.java &

(If you are using a terminal or telnet instead of the X-Window environment, omit the ampersand (&) from the end.)

Since the file Mult.java does not exist in the working directory, xemacs will create a new, blank editing window (called a buffer) named Mult.java into which you can type a program. (If a file named Mult.java existed in the working directory, then xemacs would create a blank buffer, open that file and read the file into the buffer). Note that xemacs displays the name of the buffer status bar near the bottom of the window, and that the name of a buffer is the name of the file it is displaying.

Entering the Program

Within the Mult.java buffer, we are going to enter (and personalize) the following Java source program:

/* Mult.java demonstrates basic I/O in Java.
 *
 * Author: Jane Doe.
 * Date: 2/29/99.
 * Purpose: Lab 1 in CS-1 at the University of Gallifrey.
 *
 * Specification:
 *   Input(keyboard): aNumber, an integer;
 *   Output(screen): 2x, 4x and 8x aNumber.
 ***********************************************************************/

import ann.easyio.*;

public class Mult
{
	
	static Keyboard theKeyboard = new Keyboard();
	static Screen theScreen = new Screen();

	public static void main(String args[]) 
     {

   		// 0. print a message explaining the purpose of the program.
		theScreen.println("\nThis program inputs a integer"
			+ "\n\tand displays 2x, 4x, and 8x its value.");

		// 1a. ask the user to enter an integer.
		theScreen.print("\nPlease enter an integer: ");
		// 1b.declare an integer container to hold the input number
		int aNumber;
		// 1c. input an integer, storing it in variable aNumber.
		aNumber = theKeyboard.readInt();

		// 2. output 2x, 4x and 8x aNumber.
		theScreen.print("\n\nTwice " + aNumber + " is "
			+ 2*aNumber
			+ ",\n\tand four times is "
			+ 4*aNumber
			+ ",\n\tand eight times is "
			+ 8*aNumber
			+ "\n\n");
	}
}

If you are working from a terminal or via telnet, you will have to type in this program; but if you are using the X-Window environment, you can instead copy-and-paste the text above from your browser's window into the xemacs window. Let's learn how to do this next.

In the X-Window system, a block of text can be selected by dragging (holding down the left mouse button while moving the mouse) from the beginning to the end of the desired text.

Most X environments support a copy-paste short-cut:

  1. drag the mouse over the text you want to copy, selecting it;
  2. position the cursor where you want to paste by pointing the mouse there and clicking the left mouse button; and
  3. paste by pressing the middle mouse button.

If your environment does not support this short-cut, try clicking the right-mouse button with text selected, and a menu should appear providing cut, copy and paste menu choices.

Using one of these methods, copy the program shown above into the xemacs Mult.java buffer.

 

Personalizing the Program

As given above, Mult.java is the work of a fictitious person (Jane Doe) on a fictitious date (February 29, 1999), in a fictitious course (CS-1) at a fictitious university (the University of Gallifrey). Edit the program's opening comment (the part between the /* and */ symbols) as appropriate to make it your work on the current date, in your course at your university.

If you are using xemacs in the X-Windows environment, you can use the mouse to position the cursor at an arbitrary point by pointing at that point and clicking the left mouse button.

The editor command C-d (typing the Control and d keys simultaneously) can be used to delete the character beneath the cursor, and C-k can be used to delete from the cursor to the end of the line.

If you are running xemacs outside of the X-Windows environment, you can move the cursor by the editor commands:

Alternatively, you can use the arrow keys on your keyboard to move the cursor. If you mistype something, it can be erased using the delete (or backspace) key.

If you make a mistake, you can always undo the effects of any xemacs command, either by using the undo command: C-x u (type Control and x simultaneously, then type the u key by itself), or by using the Undo button on the xemacs menu bar.

The undo command can be used to undo the effects of an xemacs command that has completed. However some xemacs commands take a significant length of time, and you may wish to get out of the command while it is still being performed. For this, you can use the get-out command: C-g, which we find to be useful in a wide variety of situations.

 

Saving Your Work.

When the Mult.java buffer contains the Mult.java program, you can store this program in a file by entering the editor save files command C-x s (hold down the Control and x keys simultaneously; then type the s key separately). Xemacs will ask you to confirm the operation (in the mini-buffer at the bottom of the window):

   Save file .../labs/0/Mult.java? (y, n, !, ., q or C-h)

To save the contents of the buffer in a file named Mult.java, answer 'yes' by typing y:

   Save file .../labs/0/Mult.java? (y, n, !, ., q or C-h) y

Xemacs will then confirm the save operation by displaying the Wrote file ... message shown above. Since we have saved Mult.java in the subdirectory ~/labs/0, we can visualize our situation as follows:


 

Emacs Quick Reference

Back to the Lab


Back to the Table of Contents

Back to the Introduction


Copyright 2000 by Prentice Hall. All rights reserved.