Emacs modes

This is where emacs really comes into its own. If you are editing HTML, emacs has a mode for HTML. If you are editing Perl code, emacs has a mode for Perl. In the same way, there are modes for all major programming languages, for shell scripts, for Makefiles, for almost anything you can think of. And these modes are highly intelligent. For instance, in the example shown in Figure 11-10, we are editing Python code. The emacs editor understands the Python syntax and colorizes the code based on its knowledge of the key words in Python. It also automatically indents the code as you type (in Python, the structure of the program is shown by the indentation; emacs helps you get the indentation right). It also helps you get the syntax right by refusing to indent a line correctly following a syntax error.

Figure 11-10: emacs editing python code

In most modes, emacs has special commands to do things that make sense in that context. For example, in XML mode, C-c / closes the currently open tag (so it will look back in the file for the last open tag, and type for you the correct closing tag).

In almost all cases, emacs loads the correct mode for the file that you are editing when it opens it. If it doesn't do so, you can select a mode with a command like M-x xml-mode.

Similarly, in HTML mode (see Figure 11-11), emacs colorizes the code in a way that helps you distinguish tags from text. There are numerous special key commands for this mode that allow you, for example, to insert common opening and closing tags with a single key combination and to call an external program to view the file.

Figure 11-11: emacs editing HTML

The modes are implemented by files of lisp code that are installed in directories under /usr/share/emacs. You can, of course, install additional modes. If you use a language for which there is no mode included in the SUSE emacs packages (fairly unlikely, but possible), you can always add it. We always have to add magicpoint mode (for editing source files for magicpoint, a nice slide display tool that uses a markup format).

The magicpoint mode that we use was written by Christoph Dalitz and comes in a file called mgp_mode_cd.el. To make this work and be automatically loaded when you open a magicpoint file (with a name such as file.mgp), you need to copy mgp_mode-cd.el to the directory /usr/share/emacs/site-lisp/ and add the following lines to the emacs startup file .gnu-emacs-custom in your home directory:

(autoload 'mgp-mode "irigp-mode-cd" "MGP mode." t) (add-to-list 'auto-mode-alist '("\\.mgp$" . mgp-mode))

As one would hope, the instructions for making this work are included as comments in the mode file itself.

Note You can (of course) write your own emacs modes. But to do so you need to become famil iar with some Lisp programming.

These comments just scratch the surface of what emacs modes can do, but they do give you a clear idea of what an intelligent editor emacs can be.

Was this article helpful?

0 0

Post a comment