The primary purpose of this laboratory is to familiarize you with basic use of the text editor Emacs. You will also learn how to compile and run a java application.
A text editor is a program that allows you to create and modify files containing ordinary text. We use a text editor to create and modify programs and data. The text editor we will use in this course is a version of Emacs called XEmacs. Emacs is a powerful editor available on many systems. It was originally developed by Richard Stallman and the Free Software Foundation. It is infinitely tailorable: any key can be made to perform any function.
The version we will use is window-based. But editor functions can be performed with mouse and menus or with the keyboard. Thus you can use XEmacs from a terminal or telnet window. Some keyboard commands are given as an appendix to this handout.
Remember that there are in general many different ways to accomplish the same task. You should spend time experimenting to determine which methods you prefer, and to become proficient in the use of the editor.
You will perform the following tasks with XEmacs under the direction of the lab instructor:
Since Emacs is configurable, the version and configuration you use may not be exactly as described below.
Run XEmacs by clicking the appropriate front panel icon as directed by your instructor (Usually the one displaying a document with an "XE" over it.) The XEmacs window will look something like the one pictured below.
When you open XEmacs, you are in an area (buffer) named *scratch*. You can key text into this buffer, but you generally will not. Typically you first either open a file or create a new one.
To create a new file, choose the Open option from the File menu, or press the Open tool button. Key the file name of the file to be created in the sub-window that appears, and press the Return key. If you see the message "[no completions; confirm]" or "[incomplete; confirm]" in the minibuffer, press the Return key again. You are confirming that you want to create a new file.
At this point, the text you have entered is stored only in a memory buffer. To write the buffer to a disk file, choose the Save or Save As... option from the File menu, or press the Save tool button.
Choosing Save As... from the File menu brings up a dialog box allowing you to specify another file name.
You can be editing several files at the same time in XEmacs. Each file you are editing has its own memory buffer. Selecting the Buffers menu will list all of the current buffers. You should see two open buffers now, *scratch* and Something.txt. When you select one of these buffers a sub menu appears. Among the choices are Switch to Buffer and Delete Buffer.
Remember, changes that you make in a buffer are not stored to the file until you save the buffer. If the buffer contains changes that have not been saved to the file, the third and fourth characters on the "mode line" will be asterisks ("*") rather than hyphens. When the buffer is consistent with the file, the mode line will start with five hyphens.
You open an existing file in the same way that you created a new files: by choosing the Open option from the File menu, or pressing the Open tool button. You can then key the name of the file to be opened in the sub-window that appears and press the Return key, or click with the middle button in the sub-window on the name of the file to be opened. You can also key the first few characters of the file name and press the Tab key.
What we generally refer to as a window is called a frame in XEmacs. There is rarely the need to run multiple instances of XEmacs. XEmacs is a complex and powerful application and requires a good deal of system resources. If you want to have more than one window open, you simply create a new frame. You can choose Open in New Frame from the File menu and open another file in the new frame, or choose New Frame from the File menu and open another frame displaying the same buffer. From the Buffers menu, you can independently display different buffers in each frame.
Close a frame as you would close any other window. For instance, you can double click the window menu button in the upper left hand corner of the window.
Click the mouse after the text to be deleted and use the Delete or Backspace key to rub out text. Alternately, cut text as described below.
Click at the beginning of the text region, and holding the left button down drag the mouse to the end of the region. Double click selects a word, triple click selects a line. The selected text is sometimes referred to a the "region."
To cut or copy, select text to be cut or copied with the mouse as described above. Then press the Cut or Copy tool button, choose the appropriate option from the Edit menu, or use the Cut or Copy keys on the keyboard. To paste, click at the position the text is to be located. Then press the Paste tool button, choose the Paste option from the Edit menu, or press the Paste key on the keyboard.
Text can also be copied by selecting it with the mouse, and pasting it with a middle mouse button click.
Also note that text can be copied from windows other than XEmacs windows.
Pressing the Undo tool button, or choosing that option from the Edit menu, "backs out" the previous change made to the buffer. Note that the Undo button can be pressed multiple times. When all changes since the most recent save have been undone, the mode line will begin with hyphens.
Choose the Search option from the Edit menu. Key in the string you want to search for. The characters will be matched automatically as they are keyed. Pressing Control-S moves to the next instance of the string. Pressing Return ends the search.
Choose Replace from the Edit menu. Key the string to be replaced followed by Return. Key the replacement string, followed by Return. The next occurrence of the string (starting from the current position) to be replaced will highlighted. Key y or Space to replace it, and move to the next occurrence; key n or Delete to move to the next occurrence without replacement. Key q or Return to end the replacement operation.
This completes our brief introduction to XEmacs. Keyboard commands appear in the appendix at the end of this lab.
We will use a terminal window to enter line commands for compilation and execution of Java programs. If necessary, review how to create terminal windows, how to create subdirectories, how to copy files, and how to navigate directories.
We will compile and run a very simple Java program, just to see how these steps are carried out.
mkdir Java
cd Java
mkdir hello2
If you are in your Java directory, you can easily copy these files with the command:
cp ~labCourse/Labs/Lab2/hello2/*.java hello2
The asterisk ("*") matches any string of characters.
ls hello2
You should see six files, named Figures.java, HelloWorldGUI.java, HelloWorldGUIStart.java, HelloWorldTUI.java, HelloWorldTUIStart.java, and Show.java. These are segments of two programs.
Next, you will compile a program: that is, run a program (the compiler) that will translate the Java source code to binary byte code. To compile, you run the Java compiler, javac, and provide it with the name of the file or files we wish to compile.
javac hello2/HelloWorldGUIStart.java
The current working directory is your java directory. Relative to this directory, the name of the file you want to compile is hello2/HelloWorldGUIStart.java.
You could also move to the hello2 subdirectory:
cd hello2
javac HelloWorldGUIStart.java
(A handy note: you can select text in the terminal window with the mouse, and then past it into the command line by clicking the middle mouse button.)
ls hello2
If you are in your hello2 directory, simply key
ls
You will notice there are eight additional files with the extension .class. These are the output from the compiler, and are the binary byte code files. Note that the compiler compiled not only the file that you specified, but also compiled three other source files as well: Figures.java, HelloWorldGUI.java, and Show.java. This is because the file that you specified, HelloWorldGUIStart.java, is dependent on these other files. The program is made up of these four source files. The compiler is generally clever enough to compile whatever is necessary.
Finally, you will run the program using the Java interpreter, java. The interpreter reads the byte code and executes it.
echo $CLASSPATH
You will see a list of directories separated by colons. Something like:
/apps/java:/home/fred/Java
Be sure that your Java directory is on this list. If it is not, consult your lab instructor.
java hello2.HelloWorldGUIStart
Note that you do not give the interpreter a file name: hello2.HelloWorldGUIStart is a class name, not a file name. (If you don't yet know what a class name is, don't worry about it. Just note that it is not a file name.)
The interpreter will look in the directories listed in the CLASSPATH for the first one containing a file named hello2/HelloWorldGUIStart.class. This is what the interpreter then executes.
If successful, you should see the following window, or an approximation thereof:
Hello World. Welcome to programming with objects
Important note: these are not representative Object Oriented programs. Their only purpose is to teach you how to compile and execute.
XEmacs recognizes Java syntax, and provides special commands for editing and compiling Java files.
You should notice Java and JDE options in the menu bar, and the mode line should say something like "(JDE Font)". Whenever you open a file with the extension .java, XEmacs automatically enters JDE mode.
You can explicitly enter JDE mode by keying " Meta-X jde-mode". (Key Meta-X by pressing the X key while holding down a Meta key. The Meta keys are the keys with diamonds on either side of the space bar. Alternatively, you can press Esc followed by X.)
The buffer will be indented to exhibit the program structure. One sure sign of a syntactic error is if the indention is not what you expect. (Of course, this will make more sense when you understand the program structure!)
javac hello2/HelloWorldBroken.java
if you are in the Java directory, or
javac HelloWorldBroken.java
if you are in the hello2 directory.
Since there are errors in the code, the compile will not succeed. You will get output something like this:
HelloWorldBroken.java:15: ';' expected
System.out.println("You fixed it!")
^
HelloWorldBroken.java:14: cannot resolve symbol
symbol : method printl (java.lang.String)
location: class java.io.PrintStream
System.out.printl("");
^
2 errors
(There are two errors. Depending on the compiler version, the compiler may detect more. One syntactic error often spawns others, since the compiler is unable to successfully parse the erroneous statement.)
The errors occur in the file HelloWorldBroken.java at lines 15 and 14.
Can you determine what the error is and where it is located by reading the error message?
Note that the window is split, and the Java compiler run in the bottom half. Again, the compile will not succeed.
`. That is,
Control-X followed by the grave. (This is the character on the upper row of the keyboard, on the same key as the tilde (~). It is not the apostrophe.)The cursor should be positioned on the second line that begins "System...."
` again, and the cursor should be positioned on the first line that begins "System...".The error is that the "n" is missing from "println". Correct this error as well.
java hello2.HelloWorldBroken
You fixed it!
printed. If you do not, contact your lab instructor.
// Fred Hosch
// CSCI 1581 Section 1
// 10 Oct 2002
// Instructor: J. Nino
lp command. Submit the listing to your lab instructor.
In the following, the notation "C-
character" means "control-
character" and "M-
character" means "meta-
character". These are generated by pressing the
character key while holding down the Control key or meta key. On Sun workstations, the lozenge keys (<>
) or the LEFT and RIGHT keys on either side of the space bar are likely to serve as meta keys. Some systems do not generate meta-characters easily. In Emacs, the character sequence "ESCAPE
character" is generally equivalent to "M-
character". Thus to key "M-a", you can press "ESC" followed by "a".
"Button-1", "Button-2", and "Button-3" refer to the left, middle, right mouse buttons respectively. "S-Button-1" means hold the Shift key while pressing the left mouse button, "C-Button-1" means hold the Control key while pressing the left mouse button, etc. To "drag" means to move the mouse while holding down one of the buttons.
To start XEmacs from the command line, key
xemacsfilename&
If you want XEmacs to run in the terminal window or the telnet window, key
xemacs -nwfilename
The option "-nw" mean "no window."
Note : You should only have one instance of Emacs running at a time. Don't crank up another Emacs process just to see another file in another window!
To leave Emacs:
C-zIn windows, this shrinks the XEmacs window to an icon. (If you're not in windows, this leaves the XEmacs session "active" as a suspended job. Reenter with the commandfg, assuming you don't subsequently suspend other jobs.)Exit XEmacsOption on the File menu. ( C-x C-cexits XEmacs, if you're not in windows.)
There is an elaborate help and info system available in Emacs. Check the Help menu (on the extreme right of the XEmacs menu bar) as well as the following.
C-h tRun Emacs tutorial.
C-h iEnter info mode - VERY USEFUL! READ THE INSTRUCTIONS AT THE TOP OF THE FIRST PAGE. C-h kShow the function of a key. (For example, C-h k C-ntells you whatC-ndoes.)C-h aShow commands matching a string. C-h fDescribe a function.
C-x C-fRead a file into Emacs. (Open option on the File menu.) C-x C-sSave current buffer without leaving Emacs. (Save option on the tool bar.) C-x sSave selected buffers. (Save Some Buffers option on the File menu.) C-x C-wWrite a buffer to a specified file. (Save As ... option on the File menu.)
The cursor shows the location at which editing commands will take effect. This location is called point. You can use keystrokes or the mouse buttons to set the point. The point is actually between two characters: it is before the character on which the cursor appears.
The point can be positioned with the left mouse button (Button-1) or moved with the following keystroke commands.
FORWARD BACK character C-forRIGHT ARROWC-borLEFT ARROWword M-fM-bline (down/up) C-porDOWN ARROWC-norUP ARROWend/beginning of line C-eC-asentence M-eM-aparagraph M-]M-[page C-x ]C-x [end/beginning of file (buffer) M->M-<
Also note the buttons (labeled Top, etc.) on the menu bar.
scroll forward C-vscroll back M-vscroll left C-x <scroll right C-x >
Delete: FORWARD BACK character C-dDel, Back Spaceword M-dM-Delline (to end) C-kM-0 C-ksentence M-kC-x Del
To operate on a region of text, set the mark at one end and the point at the other. Once the mark is set, it remains fixed until it is set again. The text between the point and mark is called the region. When the region is active, it will appear highlighted and can be operated on by various region commands.
The following are useful for setting the mark.
The region can also be defined with the mouse. Place the cursor ar one end of the region, press the left mouse button (
C-@orC-SPACESet mark here (at point). C-x C-xExchange point and mark. C-<Set mark at beginning of buffer. C->Set mark at end of buffer.
Button-1), and while holding the button down, drag the mouse to the other end of the region. (Incidentally, double clicking with the left mouse button selects the current word; triple clicking selects the current line; S-Button-1 extends the selected text to the location of the mouse cursor.)
The following keystrokes are useful to cut and paste selected text.
Cut, Copy, and Paste options are also available on the Edit menu and as toolbar options.
C-wKill (cut) active region. C-yYank (paste) back last thing killed. M-wGNU Emacs: Copy active region.
Text can also be copied from other windows into an Emacs buffer. Select the text by dragging the mouse while pressing the left button (Button-1). Use the middle button (Button-2) to paste the text into the Emacs buffer.
There are many commands that apply to regions. For example, C-x C-u will convert the region to all upper-case.
Numeric arguments can be given to a command by typing
C-u numeric-argument command
For instance, C-u 20 C-n moves the cursor down 20 lines. C-u by itself is a "4 times" multiplier. For instance, C-u C-n moves down 4 lines, C-u C-u C-n moves down 16 lines, etc.
C-gAbort partially typed or executing command. C-_orC-x C-uUndo previous change (repeated, this will "unstack" previous changes.). (Undo option on the Edit menu.) C-lRepaint screen.
C-sSearch forward. C-rSearch backward. Use C-sorC-ragain to repeat the search in either direction.RETURNExit incremental search mode. C-gAbort current search.
Valid responses in query-replace mode are:
M-%Interactively replace a text string.
SPACEReplace this one; go on to the next. ,Replace this one; don't go on to the next. DELETESkip to next without replacing. !Replace all remaining matches. ~Back up to previous match. RETURNExit query-replace mode.
An instance of XEmacs can be controlling several windows (called XEmacs frames). Each XEmacs frame (window) can be split into multiple XEmacs "windows." Each XEmacs window displays a portion of a buffer. The following commands refer to XEmacs windows within a single X window.
To create another window (XEmacs frame) controlled by the current XEmacs instantiation, choose the New Frame or Open in New Frame option on the File menu. (See also the Switch to Buffer - Other Frame option under a particular buffer in the Buffers menu.)
C-x 2Break window vertically. C-x 5Break window horizontally. C-x oSwitch cursor to other window. ESCAPE C-vScroll other window. C-x fLoad file into other window. C-x 1Delete other window. C-x 0Delete current window.
C-x 5 2 creates a new window (XEmacs frame), and C-x 5 0 deletes a frame.
The following are useful buffer commands. See the Buffers menu as well.
C-x bSwitch buffer in current window. C-x 4 bSwitch buffer in other window. C-x C-bList buffers. C-x kKill buffer. (Delete Buffer option under a particular buffer on the Buffers menu.)
A few random items. Keys in XEmacs are not "hard wired." In fact, each key stroke runs a function, and the function tied to any key can be changed dynamically. To see what function is being run by a key or key sequence, you can use C-h k.
You can also run functions explicitly by name. To do this, type M-x and then the name of the function. For instance, the command to change major mode is typically name-of-mode-mode. Keying
M-x c-mode
M-x fundamental-mode
M-x text-mode
will change mode to c-mode, fundamental-mode, or text-mode respectively. auto-fill-mode (for line wrap) is toggled on and off with the command auto-fill-mode:
M-x auto-fill-mode
TAB can be used to automatically complete command names and file names. For instance, if you key
M-x auto-f TAB
the command will automatically expand to auto-fill-mode.
XEmacs can be initialized with a file named .emacs in your home directory. For example, you can set your default mode by including a line such as
(setq default-major-mode 'text-mode)
in your .emacs file. Your instructor may provide such a file for you to copy.
As another example of key to function binding, suppose you are running Emacs remotely through a terminal server. You might have trouble getting the terminal server to pass the characters C-s and C-q through to Emacs. By default, C-s runs the function isearch-forward and C-q runs quoted-insert. You can run these functions directly (with M-x as mentioned above), or you can bind them to other keys. For instance, if you enter
M-x global-set-key RETURN C-x $ isearch-forward RETURN
then the sequence C-x $ will run the isearch-forward function.
Finally,
C-oOpen line (insert newline after point).