old-www/HOWTO/Emacs-Beginner-HOWTO-3.html

297 lines
12 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML>
<HEAD>
<META NAME="GENERATOR" CONTENT="SGML-Tools 1.0.9">
<TITLE>Emacs Beginner's HOWTO: Emacs Modes</TITLE>
<LINK HREF="Emacs-Beginner-HOWTO-4.html" REL=next>
<LINK HREF="Emacs-Beginner-HOWTO-2.html" REL=previous>
<LINK HREF="Emacs-Beginner-HOWTO.html#toc3" REL=contents>
</HEAD>
<BODY>
<A HREF="Emacs-Beginner-HOWTO-4.html">Next</A>
<A HREF="Emacs-Beginner-HOWTO-2.html">Previous</A>
<A HREF="Emacs-Beginner-HOWTO.html#toc3">Contents</A>
<HR>
<H2><A NAME="s3">3. Emacs Modes</A></H2>
<P>Emacs modes are different behaviors and features which you can turn
on or off (or customize, of course) for use in different
circumstances. Modes are what make one editor (Emacs) equally useful
for writing documentation, programming in a variety of languages (C,
C++, Perl, Python, Java, and many more), creating a home page, sending
E-Mail, reading Usenet news, keeping track of your appointments, and
even playing games.
<P>
<P>Emacs modes are simply libraries of Lisp code that extend, modify,
or enhance Emacs is some way.
<P>
<H2><A NAME="ss3.1">3.1 Major vs. Minor Modes</A>
</H2>
<P>There are fundamentally two types of modes available: Major and
Minor. The distinction isn't the easiest thing to grasp until you've
worked with a few of them off and on, but let's give it a shot.
<P>
<P>Only one major mode can be active at a given time. Many minor modes
can be active at a given time. Major modes tend to be language or
task-specific, while minor modes are smaller and less specific
utilities that cut across many tasks.
<P>
<P>Sounds kind of abstract, so let's try an example. There's a mode
that I use quite often when I'm writing plain old text files. It's
called <CODE>text-mode</CODE>. This mode was designed for writing free
form text like a README file. It understands how to identify words and
paragraphs and generally makes sure that it does what I expect when I
use the normal navigation keystrokes.
<P>
<P>When I'm writing text for human consumption, I typically want it
to look good. It should be properly word-wrapped to a reasonable value
and so on. To enable word wrapping I just turn on the
<CODE>auto-fill</CODE> minor mode. This mode tries to do the Right Thing
when I'm typing along and hit the end of the line. The fact that it is
a minor mode means that it can work with several different major
modes. My notion of the ``Right Thing'' to do when I hit the end of
the line is different when I'm in <CODE>text-mode</CODE> than it is when
I'm in <CODE>java-mode</CODE> for example. I don't want my Java code to be
word-wrapped as if was English text. But I <EM>do</EM> want the blocks
of comments in my Java code to be word wrapped! <CODE>auto-fill</CODE>
mode is smart enough to figure that out.
<P>
<P>The authors of various Emacs modes have done a great job of making
sure that things that should work as minor modes are minor modes.
<P>
<P>If you look back at that ASCII sketch of an Emacs screen, you'll
notice that the mode line identifies the mode(s) that Emacs is in. In
that case it was in a mode called ``Lisp Interaction'' which is the
default mode. It's really only useful if you're going to be writing
Lisp code. (But since most of Emacs is written in Lisp, why not?)
<P>
<H2><A NAME="ss3.2">3.2 Programming Modes</A>
</H2>
<P>First and foremost, Emacs was designed by a programmer for
programmers. There are high-quality modes available for almost every
popular programming language you can think of (and even some not so
popular ones). I only briefly describe a few of them here.
<P>
<P>Most programming modes share some common characteristics. Usually,
they'll do some or all of the following:
<P>
<UL>
<LI>Provide color-syntax highlighting for the language.
</LI>
<LI>Provide automatic indentation and code formatting for the
language.
</LI>
<LI>Provide context (language) sensitive help.
</LI>
<LI>Automatically interface with your debugger.
</LI>
<LI>Add language-specific menus to the menu bar.
</LI>
</UL>
<P>
<P>In addition, there are some non-language specific modes that help
out with tasks that are common to programming in many
languages. Things like interfacing to your version control software,
automatically adding comments to your code, creating Makefiles,
updating Change Logs and so on.
<P>
<P>When you add all these modes together and consider the maturity and
stability of the Emacs code, it compares quite nicely to commercially
marketed Integrated Development Environments (IDEs) for languages like
C++ and Java. And, of course, it's free.
<P>
<H3>C/C++/Java</H3>
<P>Because the syntax of C, C++, and Java are quite similar, there is
one Emacs mode which handles all three languages (as well as
Objective-C and IDL). It's a very mature and complete package and it
included in the Emacs distribution. This mode is called either
<CODE>cc-mode</CODE> or <CODE>CC Mode</CODE>.
<P>
<P>For more details or to download a newer version, visit
<A HREF="http://www.python.org/emacs/">http://www.python.org/emacs/</A>.
<P>
<H3>Perl</H3>
<P>There are actually two modes for editing Perl code in Emacs. The
first is called <CODE>perl-mode</CODE> (as you would expect) and the
second is <CODE>cperl-mode</CODE>. I don't have a good grasp of this
history and why there are two modes (the docs don't say), but it would
appear that <CODE>perl-mode</CODE> was the original mode for editing Perl
code in Emacs. It seems to have fewer features than
<CODE>cperl-mode</CODE> and is lacking the ability to recognize some of
Perl's fancier language constructs.
<P>
<P>Personally, I use and recommend <CODE>cperl-mode</CODE> which seems to
be quite actively maintained and has just about every feature I could
ever want. You can find the latest release here:
<A HREF="ftp://ftp.math.ohio-state.edu/pub/users/ilya/emacs">ftp://ftp.math.ohio-state.edu/pub/users/ilya/emacs</A>.
<P>
<P>But don't take my word for it. Try them both and pick the one that
best meets your needs.
<P>
<H3>Python</H3>
<P>Python (another very popular scripting language) has an Emacs mode
available for it as well. As far as I can tell, it is <EM>not</EM>
distributed with GNU Emacs but it distributed with XEmacs. It works
quite well in both editors, though.
<P>
<P>You can get <CODE>python-mode</CODE> from the official Python web site
<A HREF="http://www.python.org/emacs/python-mode/">http://www.python.org/emacs/python-mode/</A>.
<P>
<H3>Others</H3>
<P>There are many many other editing modes available to help out
programmers. Such modes help out with things like:
<P>
<UL>
<LI>Shell Scripts (Bash, sh, ksh, csh, ...)</LI>
<LI>Awk, Sed, Tcl, ...</LI>
<LI>Makefiles</LI>
<LI>Change Logs</LI>
<LI>Documentation</LI>
<LI>Debugging
</LI>
</UL>
<P>
<P>And much more. See the last section of this document for more
information on finding other modes and add-ins.
<P>
<H2><A NAME="ss3.3">3.3 Authoring</A>
</H2>
<P>Fancy Emacs modes are <EM>not</EM> limited to just those who write
code. Folks writing documentation (of any sorts) can also benefit from
a wide selection of Emacs modes.
<P>
<H3>Spell-Checking (<CODE>ispell</CODE> mode)</H3>
<P>Authors of many types of documents need to spell-check once in a
while. If you have <B>GNU ispell</B> installed, you can type <CODE>M-x
ispell</CODE> and spell-check the current buffer. If ispell finds words
that it doesn't know, it prompts you with a list of possible
replacements and lets you select one (or none) of them. It's
functionally equivalent to the spell-checkers in many popular non-free
software packages.
<P>
<H3>HTML (<CODE>html-helper</CODE> mode)</H3>
<P>If you find yourself writing HTML files once in a while (or even a
lot), you might want to try out <CODE>html-helper-mode</CODE>. It is
available from
<A HREF="http://www.santafe.edu/~nelson/tools/">http://www.santafe.edu/~nelson/tools/</A> as is the documentation
and related stuff.
<P>
<P>As its name suggests, <CODE>html-helper-mode</CODE> provides lots of
things to help out those folks who still write HTML by hand--the old
fashioned way.
<P>
<H3>TeX (<CODE>tex-mode</CODE>)</H3>
<P>When you're writing documents in TeX, it's often helpful to get
Emacs to add some color and highlight the backslashes, braces and
other characters. <CODE>tex-mode</CODE> takes care of that for you.
<P>
<P>Though I don't write much directly in TeX anymore, when I did this
mode proved to be quite helpful in making my TeX source a bit more
readable.
<P>
<H3>SGML (<CODE>sgml-mode</CODE>)</H3>
<P>The document you're now reading was written in SGML (and probably
converted to the format you're reading it in). <CODE>sgml-mode</CODE>
provides all the basics for SGML documents: validation, highlighting,
forward-tag, backward-tag, and much more. It is a standard part of
Emacs.
<P>
<H2><A NAME="ss3.4">3.4 Other Modes</A>
</H2>
<P>Of course, there are lots of other handy modes to make life
easier. Here's just a sampling of the popular ones:
<P>
<H3>Version Control (<CODE>vc</CODE> mode)</H3>
<P><CODE>vc</CODE> mode interfaces with most of the popular version
control back-ends (RCS, SCCS, CVS) to make it very easy to check files
in and out, manage releases and so on. It is a standard part of Emacs
and is documented in the Emacs documentation.
<P>
<H3>Shell Mode</H3>
<P>Why switch to another X window or virtual console just to run a few
shell commands? Do it from within Emacs and save yourself the
trouble. <CODE>:-)</CODE>
<P>
<P><CODE>M-x shell</CODE> will launch a shell within an Emacs buffer. You
can do most things with this buffer that you could do with a normal
shell prompt (except for running full screen programs like <CODE>vi</CODE>
or <CODE>pine</CODE>) because Emacs is talking to your real shell behind
the scenes.
<P>
<P>This is a standard part of Emacs, too, so you'll find it documented
in the Emacs docs.
<P>
<H3>Telnet and FTP</H3>
<P>Why switch to another X window or virtual console just to run telnet
or FTP? Do it from within Emacs and save yourself the
trouble. (Notice the pattern yet?)
<P>
<P>Just like running a shell inside of Emacs, you can telnet and
ftp. Try <CODE>M-x telnet</CODE> or <CODE>M-x ftp</CODE> to experience it for
yourself. See the documentation for all the gory details.
<P>
<H3>Man</H3>
<P>Why switch to another X window or virtual console just to read a
manual page? Do it from within Emacs and save yourself the
trouble. (I promise. I'll stop.)
<P>
<P>Just like running a shell inside of Emacs, you can read manual
pages. Try <CODE>M-x man</CODE> to experience it for yourself. See the
documentation for more.
<P>
<H3>Ange-FTP</H3>
<P>To quote the <CODE>ange-ftp</CODE> documentation:
<P>
<BLOCKQUOTE>
This package attempts to make accessing files and directories using
FTP from within GNU Emacs as simple and transparent as possible. A
subset of the common file-handling routines are extended to interact
with FTP.
</BLOCKQUOTE>
<P>
<P>That means you can treat files on remote machines as if there were
local. So if you need to edit a file on a different computer, just
tell Emacs to open it (using a slightly different path syntax) and it
takes care of all the details of logging in and retrieving the
file. Then, when you save the file via <CODE>C-x C-s</CODE>,
<CODE>ange-ftp</CODE> intercepts the save and writes the file back to the
remote machine.
<P>
<P>The slightly different path syntax goes like this... A file named
``myfile'', in a ``user'''s directory, on a machine named
``my.host.org'' can be opened by opening (<CODE>C-x f</CODE>) the file:
<P>
<BLOCKQUOTE>
<CODE>/user@my.host.org:~user/myfile</CODE>
</BLOCKQUOTE>
<P>
<P>This, also, is a standard part of the Emacs distribution so you can
find it documented in the Emacs documentation.
<P>
<P>Thanks to Etienne Grossmann (
<A HREF="mailto:etienne@anonimo.isr.ist.utl.pt">etienne@anonimo.isr.ist.utl.pt</A>) for the example above.
<P>
<HR>
<A HREF="Emacs-Beginner-HOWTO-4.html">Next</A>
<A HREF="Emacs-Beginner-HOWTO-2.html">Previous</A>
<A HREF="Emacs-Beginner-HOWTO.html#toc3">Contents</A>
</BODY>
</HTML>