To use etags, you must first invoke the separate etagsprogram in your current directory to create the tag table. Its arguments are the files for which you want tag information. The usual way to invoke it is etags *.[ch], that is, building a tag table from all files ending in .c or .h . (That's for you C programmers; other languages would use their appropriate extensions, of course.) You can run etagsfrom shell mode or with the command M-!(for shell-command). The output of etagsis the file TAGS , which is the tag table. When you are writing code, you can update your tag table to reflect new files and function definitions by invoking etagsagain.
After you have created the tag table, you need to make it known to Emacs. To do this, type M-x visit-tags-table Enter. This prompts you for the name of the tag table file; the default is TAGS in the current directory, as you would expect. After you execute this step, you can use the various Emacs tags commands.
The most important tag command is M-. (for find-tag). This command prompts you for a string to use in searching the tag table for a function whose name contains the string. Supply the search string, and Emacs visits the file containing the matching function name in the current window and goes to the first line of the function's definition. A variation of M-. is C-x 4. (for find-tag-other-window), which uses another window instead of replacing the text in your current window.
A nice feature of M-. is that it picks up the word the cursor is on and uses it as the default search string. For example, if your cursor is anywhere on the string my_function, M-. uses my_functionas the default. Thus, when you are looking at a C statement that calls a function, you can type M-. to see the code for that function.
If you have multiple functions with the same name, M-. finds the function in the file whose name comes first in alphabetical order. To find the others, you can use the command M-, (for tags-loop-continue) to find the next one (or complain if there are no more). This feature is especially useful if your directory contains more than one program, that is, if there is more than one function called main . M-, also has other uses, as we will see.
You can use the tag table to search for more than just function definitions. The command M-x tags-search Enterprompts for a regular expression; it searches through all files listed in the tag table (such as, all .c and .h files) for any occurrence of the regular expression, whether it is a function name or not. This capability is similar to the grepfacility discussed earlier in this chapter. After you have invoked tags-search, you can find additional matches by typing M-,.
There is also an analogous query-replace capability. The command M-x tags-query-replace Enterdoes a regular expression query-replace (see Chapter 3 Chapter 3. Search and Replace The commands we discussed in the first two chapters are enough to get you started, but they're certainly not enough to do any serious editing. If you're using Emacs for anything longer than a few paragraphs, you'll want the support this chapter describes. In this chapter, we cover the various ways that Emacs lets you search for and replace text. Emacs provides the traditional search and replace facilities you would expect in any editor; it also provides several important variants, including incremental searches, regular expression searches, and query-replace. We also cover spell-checking here, because it is a type of replacement (errors are sought and replaced with corrections). Finally, we cover word abbreviation mode; this feature is a type of automatic replacement that can be a real timesaver.
) on all files listed in the tag table. As with the regular query-replace-regexpcommand, if you precede tags-query-replacewith a prefix argument (i.e., C-u M-x tags-query-replace Enter), Emacs replaces only matches that are whole words. This feature is useful, for example, if you want to replace occurrences of printfwithout disturbing occurrences of fprintf. If you exit a tags-query-replacewith Escor C-g, you can resume it later by typing M-,.
The command M-x tags-aproposrounds out the search facilities of etags. If you give it a regular expression argument, it opens a *Tags List*
buffer that contains a list of all tags in the tag table (including names of files as well as functions) that match the regular expression. For example, if you want to find out the names of output routines in a multiple-file C program, you could invoke tags-aproposwith the argument printor write.
Finally, you can type M-x list-tags Enterto list all the tags in the table—that is, all the functions—for a given C file. Supply the filename at the prompt, and you get a *Tags List*
buffer showing the names of functions defined in that file along with their return types (if any). Note that if you move your cursor to this list, you can use M-. to look at the actual code for the function. M-. picks up the word the cursor is on as the default function name, so you can just move the cursor to the name of the function you want to see and press M-. followed by Enterto see it.
9.2.5 Fonts and Font-lock Mode
There's one last common feature to mention. The use of fonts to help present code is very popular—so popular, in fact, that it is now universal. Unlike the indentation and formatting supported by the various language modes, nothing in the code itself changes. But when you're in font-lock mode, your program certainly looks different.
You can turn on this feature for any language mode with M-x font-lock-modeto see for yourself. Keywords get a particular color; comments get a different color and are often italicized; strings and literals get yet another color. It can aid quick browsing of code. Many people come to depend on it much the way they rely on proper indentation. If you become one of those people, you'll want to make it the default for all language sessions. You can add the following line to your .emacs file to achieve this aim:
;; Turn on font-locking globally
(global-font-lock-mode t)
The colors and styles used are customizable if you don't like the defaults. M-x list-faces-displayproduces a list of the named faces Emacs knows about. You'll see something similar to the screen shown in Figure 9-1.
Figure 9-1. Fonts available for customization in Emacs
Of course, in real life, the colors and bold and whatnot should be more pronounced. You'll also see quite a few more faces. You can modify any of those faces with either M-x modify-face(a simple prompted "wizard" approach) or M-x customize-face(the big fancy interactive approach). You can also add lines to your .emacs file for your favorite customizations. Here's an example:
'(font-lock-comment-face
((((class color) (background light))
Читать дальше