8.5. Enhanced TagsThe "Exuberant ctags" program is a ctags clone that is considerably more capable than UNIX ctags. It produces an extended tags file format that makes tag searching and matching a more flexible and capable process. We describe it first, since it is supported by several of the vi clones. This section also describes tag stacks: the ability to save multiple locations visited with the :tag or ^] commands. All of the clones provide tag stacking. 8.5.1. Exuberant ctagsThe "Exuberant ctags" program was written by Darren Hiebert. Its home page is http://home.hiwaay.net/~darren/ctags/. As of this writing, the current version is 2.0.3. The following list of the program's features is adapted from the README file in the ctags distribution:
Exuberant ctags produces tags files in the form described in the next subsection. 8.5.2. The New tags FormatTraditionally, a tags file has three tab-separated fields: the tag name (typically an identifier), the source file containing the tag, and an indication of where to find the identifier. This indication is either a simple line number, or a nomagic search pattern enclosed either in slashes or question marks. Furthermore, the tags file is always sorted. This is the format generated by the UNIX ctags program. In fact, many versions of vi allowed any command in the search pattern field (a rather gaping security hole). Furthermore, due to an undocumented implementation quirk, if the line ended with a semicolon and then a double-quote (;"), anything following those two characters would be ignored. (The double-quote starts a comment, as it does in .exrc files.) The new format is backwards-compatible with the traditional one. The first three fields are the same: tag, filename, and search pattern. Exuberant ctags only generates search patterns, not arbitrary commands. Extended attributes are placed after a separating ;". Each attribute is separated from the next by a tab character, and consists of two colon-separated subfields. The first subfield is a keyword describing the attribute, the second is the actual value. Table 8.2 lists the supported keywords. Table 8.2. Extended ctags Keywords
If the field does not contain a colon, it is assumed to be of type kind. Here are some examples: ARRAYMAXED awk.h 427;" d AVG_CHAIN_MAX array.c 38;" d file: array.c array.c 1;" F ARRAYMAXED is a C #define macro defined in awk.h. AVG_CHAIN_MAX is also a C macro but it is used only in array.c. The third line is a bit different: it is a tag for the actual source file! This is generated with the -i F option to Exuberant ctags, and allows you to give the command :tag array.c. More usefully, you can put the cursor over a filename and use the ^] command to go to that file. Within the value part of each attribute, the characters backslash, tab, carriage return and newline should be encoded as \\, \t, \r, and \n, respectively. Extended tags files may have some number of initial tags that begin with !_TAG_. These tags usually sort to the front of the file, and are useful for identifying which program created the file. Here is what Exuberant ctags generates: !_TAG_FILE_FORMAT 2 /extended format; ..../ !_TAG_FILE_SORTED 1 /0=unsorted, 1=sorted/ !_TAG_PROGRAM_AUTHOR Darren Hiebert /darren@hiebert.com/ !_TAG_PROGRAM_NAME Exuberant Ctags // !_TAG_PROGRAM_URL http://home.hiwaay.net/~darren/ctags /.../ !_TAG_PROGRAM_VERSION 2.0.3 /with C++ support/ Editors may take advantage of these special tags to implement special features. For example, vim pays attention to the !_TAG_FILE_SORTED tag and will use a binary search to search the tags file instead of a linear search if the file is indeed sorted. If you use tags files, we recommend that you get and install Exuberant ctags. 8.5.3. Tag StacksThe :tag ex command and the ^] vi mode command provide a limited means of finding identifiers, based on the information provided in a tags file. Each of the clones extends this ability by maintaining a stack of tag locations. Each time you issue the :tag ex command, or use the ^] vi mode command, the editor saves the current location before searching for the specified tag. You may then return to a saved location using (usually) the ^T command or an ex command. Solaris vi tag stacking and an example are presented below. The way each clone handles tag stacking is described in each editor's respective chapter. 8.5.3.1. Solaris viSurprisingly enough, the Solaris 2.6 version of vi supports tag stacking. Perhaps not so surprisingly, this feature is completely undocumented in the Solaris ex(1) and vi(1) manual pages. For completeness, we summarize Solaris vi tag stacking in Table 8.3, Table 8.4, and Table 8.5. Tag stacking in Solaris vi is quite simple.[43]
Table 8.3. Solaris vi Tag CommandsTable 8.4. Solaris vi Command Mode Tag CommandsTable 8.5. Solaris vi Options for Tag Management
To give you a feel for using tag stacks, we present a short example, using Exuberant ctags and vim. Suppose you are working with a program that uses the GNU getopt_long function, and that you need to understand more about it. GNU getopt consists of three files, getopt.h, getopt.c, and getopt1.c. First, you create the tags file, then you start by editing the main program, found in main.c: $ ctags *.[ch] $ ls Makefile getopt.c getopt.h getopt1.c main.c tags $ vim main.c
It turns out that getopt_long is a "wrapper" function for _getopt_internal. You place the cursor on _getopt_internal and do another tag search. Typing ^T three times would move you back to main.c, where you started. The tag facilities make it easy to move around as you edit source code. Copyright © 2003 O'Reilly & Associates. All rights reserved. |
||||||
|