Transcript Using Emacs

Using Emacs
CS288
Emacs for Java
• Emacs is a text editor.
• Powerful features for advanced program
editing.
• Simple to use (when you get used to it!).
• Works on all almost all platforms.
E.g works fine on Pentium 100Mhz, with
32 Meg running Windows 95,
as well as Linux, Unix, Mac-X and Windows XP.
• Very good at editing several files at same time.
UniS
Emacs for editing Java Files
Terminology
• Frame: same as a
Microsoft Window.
• Buffer: contents of
frame
• Minbuffer: Where all
command text appears
as you type.
UniS
Open File and Cancel Commands
Open a file
• click Files, Open
• minibuffer now
becomes active
• Type file name into
minibuffer.
• Note you also have to
enter the folder full
path.
• hit [Ent] (the enter key)
When it all goes wrong:
• C-g (control and g)
always cancels all
commands
UniS
Having edited a buffer, Files-> Save Buffer
saves contents of buffer back to file on disk.
Getting path from Windows Explorer.
Open a file
• can cut and paste directory path directly from Windows explorer
(or anywhere else you can get it)
• Once on clipboard start to open file in Emacs as before.
• When in minibuffer use C-y to paste clipboard contents, i.e the folder
path.
Can paste path from
address bar of explorer
UniS
Incremental Search
Search:
• C-s (control and g)
starts incremental
search.
• Type in search string in
minibuffer (which is
now active)
• Will start to search as
soon as you type.
• E.g as you type first
two letters of String,
emacs will go straight
to first St occurrence.
UniS
Incremental Search on Current Word
Search on current word:
• place cursor at start of
word you want to search
for in rest of buffer
• C-s C-w will start a search
for the current word (which
of course is the word the
cursor is at right now)
• Hit C-s to find next
occurrence of word.
• Repeat to find all
occurrences of word.
• Note C-w includes only
proper characters (e.g not
'_'. To add more words to
search hit C-w repeatedly
until all parts of word are
included.
UniS
This search:
C-s C-w C-w
(print_attributes
counts as two words)
Opening several frames on same file
Splitting buffer into two
• Files -> Make New Frame
Creates two frames with
same buffer (i.e looking at
same file).
• This is useful for seeing
the definition of a method
and places where it is
used.
• Also useful for looking at
related Java files, since
either frame can now be
used to visit other files.
• This is easy way to load
relevant Java files, one per
frame.
UniS
Emacs Warning
• Emacs has been around since the 70-s.
• It does not use standard Microsoft short cuts (like C-V for
paste).
• However commands can always be found on menu bar.
• Copy in Emacs:
Place mouse at start of area to be copied.
Hit C-x (makes an invisible mark that denotes where start of
copy will be)
Scroll down to end of text to be copied.
Hit ESC-w (escape then w. Do not hold down escape).
You now have everything from invisible mark to current
cursor point copied to clipboard.
(You can just use copy instead of the ESC-w part if you
prefer.)
• Paste in Emacs.
Either just use menu, or use C-y (y for yank).
• Undo Warning: C-z actually causes a window to mimimize!
Use undo option from menu.
UniS
Global Font Lock Mode for Java
Emacs Font Lock Mode
• Emacs understands
keywords and declarations
in Java files.
• Emacs will colour code
text in Java source files
according to their
meaning.
• By default this feature is
turned off.
• To turn it on type:
ESC-x (start mini-buffer)
global-font-lock-mode
UniS
Indenting Java Code
Java Code is unreadable
without proper indention
• Emacs will automatically
indent code.
• Place cursor at start of
region to be indented.
• Hit [ESC]-x (makes
invisible mark for start of a
region)
• Place cursor at end of
region.
• Select
Java -> Indent Region
• Note when typing source
code directly into Emacs,
simply type [TAB] to
correctly indent each line.
UniS
Auto-Completion
• Emacs will try to complete
commands for you.
• Type ESC-x (start minibuffer)
• Type in the first part of a
command
(E.g. 'glob')
• Now type [TAB] (the tab
key)
• Emacs will fill in as much
of the command as
possible
(E.g in glob case that gets
you 'global')
• Type [TAB] again to get list
of possible completions to
choose from when Emacs
cant find unique
completion.
UniS
Customizing Emacs
• First make sure the environment variable HOME is defined.
Right click my-computer icon on desktop. Choose properties, then
Advanced, then Environment Variables.
• Define HOME to be your home Folder (E.g. 'H:\' ).
UniS
Customizing Emacs
• Copy the '.emacs' (note file name starts with a dot) to the HOME
folder (e.g copy it to 'H:\' if thats what you defined HOME to be)
• The .emacs file will be read by Emacs every time it starts up and
commands there will be executed.
• See the Help menu for a details on customization.
• The course web site has a .emacs file that you can copy.
Example of very minimal .emacs file that will
make sure Java files are correctly formatted in
Emacs
(show-paren-mode 1)
(global-font-lock-mode 1)
UniS
Customizing Emacs
(show-paren-mode 1)
This line in .emacs makes
matching brackets be highlighted
when cursor is next to a bracket.
(Since Java contains many highly
nested statements this is very
useful)
UniS
IBM Eclipse
A more GUI based Java development tool is Eclipse from IBM.
This is free, but is much harder initially than Emacs.
Not yet stable.
Requires powerful PC to run comfortably.
AP Lab machines do not have enough power for it to work well.
Not supported on CS288.
http://www.eclipse.org/
UniS