home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


Book HomeLearning the vi EditorSearch this book

10.9. Programming Assistance

elvis' programming assistance capabilities are described in this section.

10.9.1. Edit-Compile Speedup

elvis provides commands that make it easier to stay within the editor while working on a program. You can recompile a single file, rebuild your entire program, and work through compiler errors one at a time. The elvis commands are summarized in Table 10.10.

Table 10.10. elvis Program Development Commands

Command Option Function
cc[!] [args] ccprg

Run the C compiler. Useful for recompiling an individual file.

mak[e][!] [args] makeprg

Recompile everything that needs recompiling (usually via make(1)).

er[rlist][!] [file]  

Move to the next error's location.

The cc command recompiles an individual source file. You run it from the colon command line. For example, if you are editing the file hello.c, and you type :cc, elvis will compile hello.c for you.

If you supply additional arguments to the :cc command, those arguments will be passed on to the C compiler. In this case, you need to supply all the arguments, including the filename.

The :cc command works by executing the text of the ccprg option. The default value is "cc ($1?$1:$2)". elvis sets $2 to the name of the current source file, and $1 to the arguments you give to the :cc command. The value of ccprg thus uses your arguments if they are present; otherwise, it just passes the current file's name to the system cc command. (You can, of course, change ccprg to suit your taste.)

Similarly, the :make command is intended to recompile everything that needs recompiling. It does this by executing the contents of the makeprg option, which by default is "make $1". Thus, you could type :make hello to make just the hello program, or just :make to make everything.

elvis captures the output of the compile or make, and looks for things that look like filenames and line numbers. When it finds likely candidates, it treats them as such, and moves to the location of the first error. The :errlist command moves to each successive error location, in turn. elvis displays the error message text in the status line as you move to each location.

If you supply a filename argument to :errlist, elvis will load a fresh batch of error messages from that file, and move to the location of the first error.

The vi mode command * (asterisk) is equivalent to :errlist. This is more convenient to use when you have a lot of errors to step through.

Finally, one really nice feature is that elvis compensates for changes in the file. As you add or delete lines, elvis keeps track, so that when you go to the next error, you end up on the correct line, which is not necessarily the one with the same absolute line number as in the compiler's error message.

10.9.2. Syntax Highlighting

To cause elvis to do syntax highlighting, use the :display syntax command. This is a per-window command. (The other elvis display modes are described in Section 10.10.1.) elvis displays text in up to six different fonts: normal, bold, italic, underlined, emphasized, and fixed. (These can be abbreviated to a single letter.) The syntax display modes use the following options to associate fonts with various parts of the syntax:

The description of each language's comments, functions, keywords, etc., is stored in the elvis.syn file. This file comes with a number of specifications in it already. As an example, here is the syntax specification for Awk:

# Awk.  This is actually for Thompson Automation's AWK compiler, which is
# somewhat beefier than the standard AWK interpreter.
language tawk awk
extension .awk
keyword BEGIN BEGINFILE END ENDFILE INIT break continue do else for function
keyword global if in local next return while
comment #
function (
string "
regexp /
useregexp (,~
other allcaps

The format is mostly self-explanatory, and is fully documented in the elvis online documentation.

The reason elvis associates fonts with different parts of a file's syntax is its ability to print files as they're shown on the screen (see the discussion of the :lpr command in Section 10.10.1).

In addition to specifying the font to use for each kind of item, you can associate a color with each kind of font (normal, italic, and so on). This is done with the :color command.

On a non-bitmapped display such as the Linux console, all of the fonts map into the one used by the console driver. This makes it rather difficult to distinguish normal from italic, for example. However, on some displays (such as the Linux console), you can still change the color of the different fonts. If you have a Linux system with elvis, use it to edit a convenient C source file, and then issue the following commands:

:display syntax
:color normal white
:color bold yellow
:color emphasized green
:color italic cyan
:color fixed red

Your screen will change to highlight C keywords in yellow, comments in light blue, preprocessor directives in green, and character and string constants in red. We regret that we can't reproduce the effect here in print. Figure

In elvis, the syntax colors are per-window attributes. You can change the color for the italic font in one window, and it will not affect the color for the italic font in another window. This is true even if both windows are showing the same file.

Syntax coloring makes program editing much more interesting and lively. But you have to be careful in your choice of colors!



Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.