|
|
DATA STRUCTURES AND ALGORITHMS (230.223)
Emacs and Java Editing - A Reference and TutorialThis sheet describes some of the main features found in GNUemacs, and its sibling
xemacs. Since the two are very similar, we will just
refer to emacs except where pointing out differences.
In the following:
Basic editing on an X-terminalThere's really not much to say here, since Emacs on an X-terminal has drop down menus with all the usual sorts of commands, and the mouse work is just standard X - the same as you'd find in other X applications and shell windows. The basics of opening a file was covered in Getting Started on GNU/Linux. As an example for use in
this exercise, open a new file called
Note: If you started Now type in the following Java program, exactly as it appears here. (Note that this is intentionally badly formatted.)
public class Pentium {
public static void main(String args[]) {
double x,y,z;
x = 4195835.0;
y = 3145727.0;
z = x - (x / y) * y;
System.out.println("Result = " + z);
}
}
If you are unfamiliar with using the mouse in X, the only things you really need to know are:
Note that X works "outside" of the application or program you're working in. For example, try copying the word "Pentium" from your program in the Emacs window to the command line of one of your xterm windows. Once you've finished save your changes using [Files|Save]. Buffers and Edit modesOne of the great things about Emacs is that it has different modes that tailor the available commands for different types of files. To see the modes that you have available choose [Help|Commands and Keys|Apropos...] (this searches for commands that contain a particular string or regular expression), then enter "mode" in the "mini-buffer" (the line at the bottom of the screen). A new window or buffer will appear with a list of all the commands with the word "mode" in them. You can scroll this to see the various modes available.Emacs allows you to have a large number of buffers (for example, many files) active at once. You can look at the list of buffers and switch between them by choosing [Buffers] and then the name of the file or buffer. Therefore it is never necessary to run more than one copy of emacs. In fact it is not advised, because you may end up with older and newer copies of the same file in different emacs processes. Try changing between a few of the buffers listed now to see what they contain. Then select the Hyper Apropos buffer again (the help window you just created) and delete it using [Files|Delete Buffer]. You can change the mode by entering one of the mode commands, for example: M-x pascal-modeor M-x html-modeThis is rarely needed, however, as Emacs usually picks the appropriate mode from the filename extension. To demonstrate this, have a look at the current mode which is shown in parentheses in the status line at the bottom of the window for various buffers. Now select your Pentium.java buffer. Observe that the mode changes to JDE - the Java Development Environment. Emacs knows from the extension that this is (likely to be) a Java program, and enters JDE-mode accordingly. Notice that the headings of the drop-down menus at the top of the window also change to include new heading [Java] and [JDE].
Note: The version of XEmacs in the labs has been set to load
JDE mode automatically by commands in a file called
Java modeModes relating to programming languages have special commands for editing programs and often for executing and testing parts of programs. We will demonstrate this with a couple of useful editing features for writing Java programs.First, Java will do automatic tabbing for you. Try entering the following piece of (bogus) code, but at the beginning of each line, press "Tab" rather than spaces.
class nothing () {
while (true) do {
while (false) do {
Notice that the tab distance changes on each line to the appropriate
amount of indentation. (The "appropriate amount of indentation" is
defined in the document
Java Programming Conventions.)
As you may have noticed already, Emacs will do bracket matching - when you type a closing bracket the cursor will skip briefly back to the opening bracket it matched. Like the indentation, this is useful for picking up errors early. Returning to the above piece of code, move the cursor to the next line (after while (false)...) and press "Tab". The cursor will move under the "i" ready for a new line. Now press "}" and you will notice two things - firstly the cursor moves the "}" back two places so that it lines up with the matching "while" statement, secondly the cursor jumps back momentarily to the matching "}". Try this again on the next two lines. Now delete the second line (while (true)...) and move the cursor to the following line (that is, while (false)...). If you now press "Tab" Emacs will adjust the indentation of that line to account for the change. You can also "Tab" a region of text in one go by marking (or highlighting) the region and pressing "Control", "Meta" (or "Alt"), and backslash "\" all at the same time. Now go back and reformat the Pentium class correctly.
Emacs also knows about the commenting commands in various modes. For
example, press "Return" at the end of the first line (" Emacs can be used to comment out regions of text. This can be particularly useful for debugging programs. As an example select the whole of the "nothing" class with the left mouse button, then choose [Java|Comment Out Region] (or [C++|Comment Out Region] if using an older version) and you should see the selected text commented out. Then save your file. You can get help any mode that you are using by choosing [Help|Describe Mode]. Coloured highlighting of programsIn addition to the programming aids provided above,emacs will also highlight your code, using different
colours for comments, strings, keywords, function names and so
on. You can toggle the colouring on and off with the command:
M-x font-lock-mode font-lock-mode is called a minor mode and operates
on top of the current major mode (for example
java-mode). It can also be used with other modes, for
example those for editing text for the typesetting language LaTeX,
or those for editing html documents such as this one.
Note that in JDE mode colouring happens automatically - there is no need to toggle font-lock-mode.
[Help|Commands and Keys|Mode] (or JDE - The java development environmentYour computer has been set up so that whenever you edit a java program (with filename extension.java) XEmacs will enter JDE
mode. In addition to all the facilities described above, JDE
allows you to compile and run java programs without ever leaving
XEmacs.
To demonstrate this return to your You can return to a single window (the one the cursor is in) at any time by choosing [File|Unsplit]. JDE provides many other facilities for developing Java programs such as inserting code templates and code elements. Feel free to experiment with these when you have time. In particular, try out the Speedbar [JDE|Speedbar]. This is useful when you have many classes and want to quickly find classes, methods and variables. Shortcuts and editing in non-X environmentsThe pop-down menus are the easiest way to start using Emacs, but it is much faster to use the keyboard commands - particularly if you have learnt to touch type. It is also worth learning the main keyboard commands so that:
You might like to start learning the short-hand commands quickly by using them instead of the menus, or just look up commands that you find you use regularly. For practice, use the short-hand commands to open your
bindkey -eIf the line contains bindkey -v change the
-v to -e. (This tells the shell to accept
Emacs commands.) Then save the file.
Note that "control" commands are actually shortcuts for longer commands. For example C-x C-scan also be executed as M-x save-bufferand C-x C-ccan be executed as M-x save-buffer-kill-emacs. The remainder of this tutorial will refer to the short-hand commands. Buffers, Windows and ShellsIf you're working from a non-X machine in a terminal window you may only have one window (for example if you've dialed into the University). For this reason Emacs provides its own system of multiple buffers and windows. Multiple buffers allow a number of "files" to be stored in memory and makes it possible, for example, to cut and paste between files without continually opening and closing them. The multiple windows allow this to be done more easily.Another case where the use of buffers is vital is if you want to run other Unix commands without closing down Emacs. You can do this by opening a shell in a bufffer with the command M-x shell. This means, for example, that you can try compiling and running your Java programs without ever leaving Emacs. Commands for buffers and windows can be found under [Buffers] and [Files] or in the Quick-reference Guide. Error recoveryIt is a good idea to do a C-x s before leaving your machine for any length of time - this ensures you have no modified but unsaved buffers in case the machine crashes. Emacs crash recovery is quite good, however, as modifications are auto-saved from time to time in a "#" file. They can be restored using M-x recover-file.In case you accidently obliterate your file, Emacs also keeps a backup whenever you open a file - the backup has the same name as the file but with a tilde ("~") appended to the end. Finally, if you accidently press the wrong command and do something weird to your file, you can get it back the way it was using M-x undo. Emacs commands in other applicationsBecause Emacs is so popular, a number of other applications have adopted the Emacs commands. This includes Netscape, Z-shell and Ghostview, all of which are used on this course, as well as Fig/Xfig.
Before trying the commands in this section, make sure you have updated
your As an example of using Emacs commands elsewhere, start a new shell by opening an xterm window. By default this will be a Z-shell. If you've changed the default shell, enter $ zshto start a Z-shell. Now give an ls command to check that the current directory contains your file Pentium.java (if it doesn't, change into the directory where you saved it), compile your Java program (the "manual way" this time) into byte code: $ javac Pentium.javaand run it: $ java Pentium Lets say you've edited your Pentium class and want to compile and run it again. Rather than typing in the javac command again, press C-p ("previous") twice and you'll see the command line step back though the commands to the correct one. Similarly C-n ("next") will take you forward through the command history. Now lets say you've executed a few other commands since running Java (enter a few now like ls and pwd) and you want to recompile your program. Press C-r ("reverse search") and type javac. As you type this you'll see the command line skip back firstly to the command beginning with java, then as you type the letter "c", to the command beginning with javac. Finally, assume that in the meantime you've edited your Java program and given it the new name Pentium2.java. You can edit the command line using the usual Emacs commands - C-f ("forward"), C-b ("backward"), C-d ("delete") and so on. These techniques are particularly useful when you are using long Unix commands with lots of options or pipes. Some other useful things to know...
On-line TutorialThere is an Emacs tutorial included within Emacs. This runs through the commonly used commands while letting you try them out on a sample file. The tutorial can be run by entering C-h t.Emacs for all tastes!One of the reasons Emacs is so popular is that it is extensible - users can write their own commands, modes, and so on in the Emacs lisp language that Emacs is written in. (In fact Emacs is Turing-complete! [See Discrete Structures.]) Many of the modes are in fact contributed by individual users. This is part of the philosphy of promoting innovation, software development and free distribution, which is taken by the Free Software Foundation, makers of GNU software including Emacs.
Emacs is also highly customisable. In XEmacs this can be done through
the menus. As an example, while the grey background is easy on the
retina, some people find it a bit dull, so we'll change it. Choose
[Options|Customize|Face...], then press Enter to accept all faces.
Now scroll down to "Default" and click on the triangle to see what is
currently stored. Hit the "Background" checkbutton, then in the grey
field type "lightyellow". Now put the cursor over "[State]" and press
the middle mouse button (or left and right together). On the menu that
pops up choose [Set for Current Session]. You should see the
background change. You can try other colours the same way. Once you
are happy with the colour choose [Save for Future Sessions] from the
middle button menu, and XEmacs will write your choice to your
Advanced users can also customize Emacs by programming directly in Lisp. If you'd like to try this see the document Programming in Emacs Lisp: An Introduction. Further informationYou may find some of the following links of interest.
|
|
Copyright © 2000,
Cara MacNish Department of Computer Science, University of Western Australia. |
![]() |