2002-08-15 Andrew Choi <akochoi@shaw.ca>
[bpt/emacs.git] / man / maintaining.texi
CommitLineData
f00366c2
RS
1@c This is part of the Emacs manual.
2@c Copyright (C) 1985,86,87,93,94,95,97,99,00,2001 Free Software Foundation, Inc.
3@c See file emacs.texi for copying conditions.
4@node Maintaining, Abbrevs, Building, Top
5@chapter Maintaining Programs
6@cindex Lisp editing
7@cindex C editing
8@cindex program editing
9
10 This chapter describes Emacs features for maintaining programs. The
d7ed11cd
RS
11version control features (@pxref{Version Control}) are also
12particularly useful for this purpose.
f00366c2
RS
13
14@menu
15* Change Log:: Maintaining a change history for your program.
f00366c2
RS
16* Tags:: Go direct to any function in your program in one
17 command. Tags remembers which file it is in.
18* Emerge:: A convenient way of merging two versions of a program.
19@end menu
20
21@node Change Log
22@section Change Logs
23
24@cindex change log
25@kindex C-x 4 a
26@findex add-change-log-entry-other-window
27 The Emacs command @kbd{C-x 4 a} adds a new entry to the change log
28file for the file you are editing
29(@code{add-change-log-entry-other-window}). If that file is actually
30a backup file, it makes an entry appropriate for the file's
31parent---that is useful for making log entries for functions that
32have been deleted in the current version.
33
34 A change log file contains a chronological record of when and why you
35have changed a program, consisting of a sequence of entries describing
36individual changes. Normally it is kept in a file called
37@file{ChangeLog} in the same directory as the file you are editing, or
38one of its parent directories. A single @file{ChangeLog} file can
39record changes for all the files in its directory and all its
40subdirectories.
41
1e4cb307
RS
42 You should put a copyright notice and permission notice at the
43end of the change log file. Here is an example:
44
45@example
46Copyright 1997, 1998 Free Software Foundation, Inc.
47Copying and distribution of this file, with or without modification, are
48permitted provided the copyright notice and this notice are preserved.
49@end example
50
51@noindent
52Of course, you should substitute the proper years and copyright holder.
53
f00366c2
RS
54 A change log entry starts with a header line that contains the
55current date, your name, and your email address (taken from the
56variable @code{user-mail-address}). Aside from these header lines,
57every line in the change log starts with a space or a tab. The bulk
58of the entry consists of @dfn{items}, each of which starts with a line
59starting with whitespace and a star. Here are two entries, both dated
60in May 1993, each with two items:
61
62@iftex
63@medbreak
64@end iftex
65@smallexample
661993-05-25 Richard Stallman <rms@@gnu.org>
67
68 * man.el: Rename symbols `man-*' to `Man-*'.
69 (manual-entry): Make prompt string clearer.
70
71 * simple.el (blink-matching-paren-distance):
72 Change default to 12,000.
73
741993-05-24 Richard Stallman <rms@@gnu.org>
75
76 * vc.el (minor-mode-map-alist): Don't use it if it's void.
77 (vc-cancel-version): Doc fix.
78@end smallexample
79
80 One entry can describe several changes; each change should have its
81own item. Normally there should be a blank line between items. When
82items are related (parts of the same change, in different places), group
83them by leaving no blank line between them. The second entry above
84contains two items grouped in this way.
85
86 @kbd{C-x 4 a} visits the change log file and creates a new entry
87unless the most recent entry is for today's date and your name. It
88also creates a new item for the current file. For many languages, it
89can even guess the name of the function or other object that was
90changed.
91
92@vindex add-log-keep-changes-together
93 When the option @code{add-log-keep-changes-together} is
94non-@code{nil}, @kbd{C-x 4 a} adds to any existing entry for the file
95rather than starting a new entry.
96
97@vindex change-log-version-info-enabled
98@vindex change-log-version-number-regexp-list
99@cindex file version in change log entries
100 If the value of the variable @code{change-log-version-info-enabled}
101is non-@code{nil}, @kbd{C-x 4 a} adds the file's version number to the
102change log entry. It finds the version number by searching the first
103ten percent of the file, using regular expressions from the variable
104@code{change-log-version-number-regexp-list}.
105
ed1b7d5e
RS
106@vindex add-log-always-start-new-record
107 If @code{add-log-always-start-new-record} is non-@code{nil},
108@kbd{C-x 4 a} always makes a new entry, even if the last entry
109was made by you and on the same date.
110
f00366c2
RS
111@cindex Change Log mode
112@findex change-log-mode
113 The change log file is visited in Change Log mode. In this major
114mode, each bunch of grouped items counts as one paragraph, and each
115entry is considered a page. This facilitates editing the entries.
116@kbd{C-j} and auto-fill indent each new line like the previous line;
117this is convenient for entering the contents of an entry.
118
119@findex change-log-merge
120 You can use the command @kbd{M-x change-log-merge} to merge other
121log files into a buffer in Change Log Mode, preserving the date
122ordering of entries.
123
124@findex change-log-redate
125@cindex converting change log date style
126 Versions of Emacs before 20.1 used a different format for the time of
127the change log entry:
128
129@smallexample
130Fri May 25 11:23:23 1993 Richard Stallman <rms@@gnu.org>
131@end smallexample
132
133@noindent
134The @kbd{M-x change-log-redate} command converts all the old-style
135date entries in the change log file visited in the current buffer to
136the new format, to make the file uniform in style. This is handy when
137entries are contributed by many different people, some of whom use old
138versions of Emacs.
139
140 Version control systems are another way to keep track of changes in your
141program and keep a change log. @xref{Log Buffer}.
142
e4059eed
RS
143@ignore
144@c This is commented out because the command is specific
145@c to maintenance of Emacs itself.
146
f00366c2
RS
147@node Authors
148@section @file{AUTHORS} files
149@cindex @file{AUTHORS} file
150
151 Programs which have many contributors usually include a file named
152@file{AUTHORS} in their distribution, which lists the individual
153contributions. Emacs has a special command for maintaining the
154@file{AUTHORS} file that is part of the Emacs distribution.
155
156@findex authors
157 The @kbd{M-x authors} command prompts for the name of the root of the
9cd6acef 158Emacs source directory. It then scans @file{ChangeLog} files and Lisp
f00366c2 159source files under that directory for information about authors of
d7ed11cd 160individual packages, and people who made changes in source files, and
f00366c2
RS
161puts the information it gleans into a buffer named @samp{*Authors*}.
162You can then edit the contents of that buffer and merge it with the
24cc235a 163existing @file{AUTHORS} file.
f00366c2
RS
164
165 Do not assume that this command finds all the contributors; don't
166assume that a person not listed in the output was not a contributor.
167If you merged in someone's contribution and did not put his name
168in the change log, he won't show up in @kbd{M-x authors} either.
e4059eed 169@end ignore
f00366c2
RS
170
171@node Tags
172@section Tags Tables
173@cindex tags table
174
175 A @dfn{tags table} is a description of how a multi-file program is
176broken up into files. It lists the names of the component files and the
177names and positions of the functions (or other named subunits) in each
178file. Grouping the related files makes it possible to search or replace
179through all the files with one command. Recording the function names
180and positions makes possible the @kbd{M-.} command which finds the
181definition of a function by looking up which of the files it is in.
182
183 Tags tables are stored in files called @dfn{tags table files}. The
184conventional name for a tags table file is @file{TAGS}.
185
186 Each entry in the tags table records the name of one tag, the name of the
bf8dd4e3
FP
187file that the tag is defined in (implicitly), and the position in that
188file of the tag's definition. When a file parsed by @code{etags} is
0f5401a8
FP
189generated from a different source file, like a C file generated from a
190Cweb source file, the tags of the parsed file reference the source
191file.
f00366c2
RS
192
193 Just what names from the described files are recorded in the tags table
194depends on the programming language of the described file. They
195normally include all file names, functions and subroutines, and may
196also include global variables, data types, and anything else
197convenient. Each name recorded is called a @dfn{tag}.
198
199@cindex C++ class browser, tags
200@cindex tags, C++
201@cindex class browser, C++
202@cindex Ebrowse
203 See also the Ebrowse facility, which is tailored for C++.
204@xref{Top,, Ebrowse, ebrowse, Ebrowse User's Manual}.
205
206@menu
207* Tag Syntax:: Tag syntax for various types of code and text files.
208* Create Tags Table:: Creating a tags table with @code{etags}.
209* Etags Regexps:: Create arbitrary tags using regular expressions.
210* Select Tags Table:: How to visit a tags table.
211* Find Tag:: Commands to find the definition of a specific tag.
212* Tags Search:: Using a tags table for searching and replacing.
213* List Tags:: Listing and finding tags defined in a file.
214@end menu
215
216@node Tag Syntax
217@subsection Source File Tag Syntax
218
219 Here is how tag syntax is defined for the most popular languages:
220
221@itemize @bullet
222@item
223In C code, any C function or typedef is a tag, and so are definitions of
224@code{struct}, @code{union} and @code{enum}.
225@code{#define} macro definitions and @code{enum} constants are also
226tags, unless you specify @samp{--no-defines} when making the tags table.
227Similarly, global variables are tags, unless you specify
228@samp{--no-globals}. Use of @samp{--no-globals} and @samp{--no-defines}
229can make the tags table file much smaller.
230
231You can tag function declarations and external variables in addition
232to function definitions by giving the @samp{--declarations} option to
233@code{etags}.
234
235@item
236In C++ code, in addition to all the tag constructs of C code, member
237functions are also recognized, and optionally member variables if you
238use the @samp{--members} option. Tags for variables and functions in
239classes are named @samp{@var{class}::@var{variable}} and
240@samp{@var{class}::@var{function}}. @code{operator} definitions have
241tag names like @samp{operator+}.
242
243@item
244In Java code, tags include all the constructs recognized in C++, plus
245the @code{interface}, @code{extends} and @code{implements} constructs.
246Tags for variables and functions in classes are named
247@samp{@var{class}.@var{variable}} and @samp{@var{class}.@var{function}}.
248
249@item
250In La@TeX{} text, the argument of any of the commands @code{\chapter},
251@code{\section}, @code{\subsection}, @code{\subsubsection},
e4a1e8d9
FP
252@code{\eqno}, @code{\label}, @code{\ref}, @code{\cite},
253@code{\bibitem}, @code{\part}, @code{\appendix}, @code{\entry},
254@code{\index}, @code{\def}, @code{\newcomand}, @code{\renewcommand},
255@code{\newenvironment} or @code{\renewenvironment} is a tag.@refill
f00366c2
RS
256
257Other commands can make tags as well, if you specify them in the
258environment variable @env{TEXTAGS} before invoking @code{etags}. The
259value of this environment variable should be a colon-separated list of
260command names. For example,
261
262@example
e4a1e8d9 263TEXTAGS="mycommand:myothercommand"
f00366c2
RS
264export TEXTAGS
265@end example
266
267@noindent
e4a1e8d9
FP
268specifies (using Bourne shell syntax) that the commands
269@samp{\mycommand} and @samp{\myothercommand} also define tags.
f00366c2
RS
270
271@item
272In Lisp code, any function defined with @code{defun}, any variable
273defined with @code{defvar} or @code{defconst}, and in general the first
274argument of any expression that starts with @samp{(def} in column zero, is
275a tag.
276
277@item
278In Scheme code, tags include anything defined with @code{def} or with a
279construct whose name starts with @samp{def}. They also include variables
280set with @code{set!} at top level in the file.
281@end itemize
282
283 Several other languages are also supported:
284
285@itemize @bullet
286
287@item
288In Ada code, functions, procedures, packages, tasks, and types are
289tags. Use the @samp{--packages-only} option to create tags for
290packages only.
291
292In Ada, the same name can be used for different kinds of entity
293(e.g.@:, for a procedure and for a function). Also, for things like
294packages, procedures and functions, there is the spec (i.e.@: the
295interface) and the body (i.e.@: the implementation). To make it
296easier to pick the definition you want, Ada tag name have suffixes
297indicating the type of entity:
298
299@table @samp
300@item /b
301package body.
302@item /f
303function.
304@item /k
305task.
306@item /p
307procedure.
308@item /s
309package spec.
310@item /t
311type.
312@end table
313
314 Thus, @kbd{M-x find-tag @key{RET} bidule/b @key{RET}} will go
315directly to the body of the package @code{bidule}, while @kbd{M-x
316find-tag @key{RET} bidule @key{RET}} will just search for any tag
317@code{bidule}.
318
319@item
320In assembler code, labels appearing at the beginning of a line,
321followed by a colon, are tags.
322
323@item
324In Bison or Yacc input files, each rule defines as a tag the nonterminal
325it constructs. The portions of the file that contain C code are parsed
326as C code.
327
328@item
329In Cobol code, tags are paragraph names; that is, any word starting in
330column 8 and followed by a period.
331
332@item
333In Erlang code, the tags are the functions, records, and macros defined
334in the file.
335
336@item
47d7776c 337In Fortran code, functions, subroutines and block data are tags.
f00366c2
RS
338
339@item
f175bfff
FP
340In makefiles, targets are tags; additionally, variables are tags
341unless you specify @samp{--no-globals}.
f00366c2
RS
342
343@item
344In Objective C code, tags include Objective C definitions for classes,
3e7eaab2
FP
345class categories, methods, and protocols. Tags for variables and
346functions in classes are named @samp{@var{class}::@var{variable}} and
347@samp{@var{class}::@var{function}}.
f00366c2
RS
348
349@item
350In Pascal code, the tags are the functions and procedures defined in
351the file.
352
353@item
3e7eaab2
FP
354In Perl code, the tags are the packages, subroutines and variables
355defined by the @code{package}, @code{sub}, @code{my} and @code{local}
356keywords. Use @samp{--globals} if you want to tag global variables.
357Tags for subroutines are named @samp{@var{package}::@var{sub}}. The
358name for subroutines defined in the default package is
359@samp{main::@var{sub}}.
f00366c2 360
e94a3679
FP
361@item
362In PHP code, tags are functions, classes and defines. When using the
363@samp{--members} option, vars are tags too.
364
f00366c2
RS
365@item
366In PostScript code, the tags are the functions.
367
368@item
e94a3679
FP
369In Prolog code, tags are predicates and rules at the beginning of
370line.
f00366c2
RS
371
372@item
373In Python code, @code{def} or @code{class} at the beginning of a line
374generate a tag.
375@end itemize
376
377 You can also generate tags based on regexp matching (@pxref{Etags
378Regexps}) to handle other formats and languages.
379
380@node Create Tags Table
381@subsection Creating Tags Tables
382@cindex @code{etags} program
383
384 The @code{etags} program is used to create a tags table file. It knows
385the syntax of several languages, as described in
386@iftex
387the previous section.
388@end iftex
389@ifinfo
390@ref{Tag Syntax}.
391@end ifinfo
392Here is how to run @code{etags}:
393
394@example
395etags @var{inputfiles}@dots{}
396@end example
397
398@noindent
399The @code{etags} program reads the specified files, and writes a tags
400table named @file{TAGS} in the current working directory.
401
402 If the specified files don't exist, @code{etags} looks for
403compressed versions of them and uncompresses them to read them. Under
404MS-DOS, @code{etags} also looks for file names like @file{mycode.cgz}
405if it is given @samp{mycode.c} on the command line and @file{mycode.c}
406does not exist.
407
408 @code{etags} recognizes the language used in an input file based on
409its file name and contents. You can specify the language with the
410@samp{--language=@var{name}} option, described below.
411
412 If the tags table data become outdated due to changes in the files
413described in the table, the way to update the tags table is the same
55d3737d
RS
414way it was made in the first place. If the tags table fails to record
415a tag, or records it for the wrong file, then Emacs cannot possibly
416find its definition until you update the tags table. However, if the
f00366c2 417position recorded in the tags table becomes a little bit wrong (due to
55d3737d
RS
418other editing), the only consequence is a slight delay in finding the
419tag. Even if the stored position is very far wrong, Emacs will still
420find the tag, after searching most of the file for it. Even that
421delay is hardly noticeable with today's computers.
f00366c2
RS
422
423 So you should update a tags table when you define new tags that you want
424to have listed, or when you move tag definitions from one file to another,
425or when changes become substantial. Normally there is no need to update
426the tags table after each edit, or even every day.
427
428 One tags table can virtually include another. Specify the included
429tags file name with the @samp{--include=@var{file}} option when
430creating the file that is to include it. The latter file then acts as
431if it covered all the source files specified in the included file, as
432well as the files it directly contains.
433
434 If you specify the source files with relative file names when you run
435@code{etags}, the tags file will contain file names relative to the
436directory where the tags file was initially written. This way, you can
437move an entire directory tree containing both the tags file and the
438source files, and the tags file will still refer correctly to the source
439files.
440
441 If you specify absolute file names as arguments to @code{etags}, then
442the tags file will contain absolute file names. This way, the tags file
443will still refer to the same files even if you move it, as long as the
444source files remain in the same place. Absolute file names start with
445@samp{/}, or with @samp{@var{device}:/} on MS-DOS and MS-Windows.
446
447 When you want to make a tags table from a great number of files, you
448may have problems listing them on the command line, because some systems
449have a limit on its length. The simplest way to circumvent this limit
450is to tell @code{etags} to read the file names from its standard input,
451by typing a dash in place of the file names, like this:
452
453@smallexample
454find . -name "*.[chCH]" -print | etags -
455@end smallexample
456
457 Use the option @samp{--language=@var{name}} to specify the language
458explicitly. You can intermix these options with file names; each one
459applies to the file names that follow it. Specify
460@samp{--language=auto} to tell @code{etags} to resume guessing the
461language from the file names and file contents. Specify
462@samp{--language=none} to turn off language-specific processing
463entirely; then @code{etags} recognizes tags by regexp matching alone
464(@pxref{Etags Regexps}).
465
e4a1e8d9
FP
466 The option @samp{--parse-stdin=@var{file}} is mostly useful when
467calling @code{etags} from programs. It can be used (only once) in
468place of a file name on the command line. @code{Etags} will read from
469standard input and mark the produced tags as belonging to the file
470@var{file}.
471
f00366c2
RS
472 @samp{etags --help} prints the list of the languages @code{etags}
473knows, and the file name rules for guessing the language. It also prints
474a list of all the available @code{etags} options, together with a short
475explanation.
476
477@node Etags Regexps
478@subsection Etags Regexps
479
480 The @samp{--regex} option provides a general way of recognizing tags
481based on regexp matching. You can freely intermix it with file names.
ed1b7d5e
RS
482If you specify multiple @samp{--regex} options, all of them are used
483in parallel, but each one applies only to the source files that follow
484it. The syntax is:
f00366c2
RS
485
486@smallexample
cc76b311
FP
487--regex=[@var{@{language@}}]/@var{tagregexp}/[@var{nameregexp}/]@var{modifiers}
488@end smallexample
489
ed1b7d5e
RS
490 The essential part of the option value is @var{tagregexp}, the
491regexp for matching tags. It is always used anchored, that is, it
492only matches at the beginning of a line. If you want to allow
493indented tags, use a regexp that matches initial whitespace; start it
494with @samp{[ \t]*}.
495
496 In these regular expressions, @samp{\} quotes the next character, and
497all the GCC character escape sequences are supported (@samp{\a} for
498bell, @samp{\b} for back space, @samp{\d} for delete, @samp{\e} for
499escape, @samp{\f} for formfeed, @samp{\n} for newline, @samp{\r} for
500carriage return, @samp{\t} for tab, and @samp{\v} for vertical tab).
501
502 Ideally, @var{tagregexp} should not match more characters than are
503needed to recognize what you want to tag. If the syntax requires you
504to write @var{tagregexp} so it matches more characters beyond the tag
505itself, you should add a @var{nameregexp}, to pick out just the tag.
506This will enable Emacs to find tags more accurately and to do
507completion on tag names more reliably. You can find some examples
508below.
509
510 The @var{modifiers} are a sequence of zero or more characters that
511modify the way @code{etags} does the matching. A regexp with no
512modifiers is applied sequentially to each line of the input file, in a
513case-sensitive way. The modifiers and their meanings are:
cc76b311
FP
514
515@table @samp
516@item i
ed1b7d5e 517Ignore case when matching this regexp.
cc76b311 518@item m
ed1b7d5e
RS
519Match this regular expression against the whole file, so that
520multi-line matches are possible.
cc76b311 521@item s
ed1b7d5e
RS
522Match this regular expression against the whole file, and allow
523@samp{.} in @var{tagregexp} to match newlines.
cc76b311
FP
524@end table
525
ed1b7d5e 526 The @samp{-R} option cancels all the regexps defined by preceding
f00366c2
RS
527@samp{--regex} options. It applies to the file names following it, as
528you can see from the following example:
529
530@smallexample
648ed8f4 531etags --regex=/@var{reg1}/i voo.doo --regex=/@var{reg2}/m \
f00366c2
RS
532 bar.ber -R --lang=lisp los.er
533@end smallexample
534
535@noindent
536Here @code{etags} chooses the parsing language for @file{voo.doo} and
537@file{bar.ber} according to their contents. @code{etags} also uses
538@var{reg1} to recognize additional tags in @file{voo.doo}, and both
539@var{reg1} and @var{reg2} to recognize additional tags in
648ed8f4
FP
540@file{bar.ber}. @var{reg1} is checked against each line of
541@file{voo.doo} and @file{bar.ber}, in a case-insensitive way, while
542@var{reg2} is checked against the whole @file{bar.ber} file,
543permitting multi-line matches, in a case-sensitive way. @code{etags}
ed1b7d5e
RS
544uses only the Lisp tags rules, with no user-specified regexp matching,
545to recognize tags in @file{los.er}.
f00366c2 546
b8fc5a9c
FP
547 You can restrict a @samp{--regex} option to match only files of a
548given language by using the optional prefix @var{@{language@}}.
648ed8f4
FP
549(@samp{etags --help} prints the list of languages recognized by
550@code{etags}.) This is particularly useful when storing many
551predefined regular expressions for @code{etags} in a file. The
f00366c2
RS
552following example tags the @code{DEFVAR} macros in the Emacs source
553files, for the C language only:
554
555@smallexample
556--regex='@{c@}/[ \t]*DEFVAR_[A-Z_ \t(]+"\([^"]+\)"/'
557@end smallexample
558
559@noindent
ed1b7d5e
RS
560When you have complex regular expressions, you can store the list of
561them in a file. The following option syntax instructs @code{etags} to
562read two files of regular expressions. The regular expressions
563contained in the second file are matched without regard to case.
f00366c2
RS
564
565@smallexample
ed1b7d5e 566--regex=@@@var{case-sensitive-file} --ignore-case-regex=@@@var{ignore-case-file}
f00366c2
RS
567@end smallexample
568
569@noindent
ed1b7d5e
RS
570A regex file for @code{etags} contains one regular expression per
571line. Empty lines, and lines beginning with space or tab are ignored.
572When the first character in a line is @samp{@@}, @code{etags} assumes
573that the rest of the line is the name of another file of regular
574expressions; thus, one such file can include another file. All the
575other lines are taken to be regular expressions. If the first
576non-whitespace text on the line is @samp{--}, that line is a comment.
577
578 For example, we can create a file called @samp{emacs.tags} with the
f00366c2
RS
579following contents:
580
581@smallexample
582 -- This is for GNU Emacs C source files
583@{c@}/[ \t]*DEFVAR_[A-Z_ \t(]+"\([^"]+\)"/\1/
584@end smallexample
585
586@noindent
587and then use it like this:
588
589@smallexample
590etags --regex=@@emacs.tags *.[ch] */*.[ch]
591@end smallexample
592
593 Here are some more examples. The regexps are quoted to protect them
594from shell interpretation.
595
596@itemize @bullet
597
598@item
599Tag Octave files:
600
601@smallexample
602etags --language=none \
603 --regex='/[ \t]*function.*=[ \t]*\([^ \t]*\)[ \t]*(/\1/' \
604 --regex='/###key \(.*\)/\1/' \
605 --regex='/[ \t]*global[ \t].*/' \
606 *.m
607@end smallexample
608
609@noindent
610Note that tags are not generated for scripts, so that you have to add
611a line by yourself of the form @samp{###key @var{scriptname}} if you
612want to jump to it.
613
614@item
615Tag Tcl files:
616
617@smallexample
618etags --language=none --regex='/proc[ \t]+\([^ \t]+\)/\1/' *.tcl
619@end smallexample
620
621@item
622Tag VHDL files:
623
624@smallexample
625etags --language=none \
626 --regex='/[ \t]*\(ARCHITECTURE\|CONFIGURATION\) +[^ ]* +OF/' \
627 --regex='/[ \t]*\(ATTRIBUTE\|ENTITY\|FUNCTION\|PACKAGE\
628 \( BODY\)?\|PROCEDURE\|PROCESS\|TYPE\)[ \t]+\([^ \t(]+\)/\3/'
629@end smallexample
630@end itemize
631
632@node Select Tags Table
633@subsection Selecting a Tags Table
634
635@vindex tags-file-name
636@findex visit-tags-table
637 Emacs has at any time one @dfn{selected} tags table, and all the commands
638for working with tags tables use the selected one. To select a tags table,
639type @kbd{M-x visit-tags-table}, which reads the tags table file name as an
640argument. The name @file{TAGS} in the default directory is used as the
641default file name.
642
643 All this command does is store the file name in the variable
644@code{tags-file-name}. Emacs does not actually read in the tags table
645contents until you try to use them. Setting this variable yourself is just
646as good as using @code{visit-tags-table}. The variable's initial value is
647@code{nil}; that value tells all the commands for working with tags tables
648that they must ask for a tags table file name to use.
649
650 Using @code{visit-tags-table} when a tags table is already loaded
651gives you a choice: you can add the new tags table to the current list
652of tags tables, or start a new list. The tags commands use all the tags
653tables in the current list. If you start a new list, the new tags table
654is used @emph{instead} of others. If you add the new table to the
655current list, it is used @emph{as well as} the others. When the tags
656commands scan the list of tags tables, they don't always start at the
657beginning of the list; they start with the first tags table (if any)
658that describes the current file, proceed from there to the end of the
659list, and then scan from the beginning of the list until they have
660covered all the tables in the list.
661
662@vindex tags-table-list
663 You can specify a precise list of tags tables by setting the variable
664@code{tags-table-list} to a list of strings, like this:
665
666@c keep this on two lines for formatting in smallbook
667@example
668@group
669(setq tags-table-list
670 '("~/emacs" "/usr/local/lib/emacs/src"))
671@end group
672@end example
673
674@noindent
675This tells the tags commands to look at the @file{TAGS} files in your
676@file{~/emacs} directory and in the @file{/usr/local/lib/emacs/src}
677directory. The order depends on which file you are in and which tags
678table mentions that file, as explained above.
679
680 Do not set both @code{tags-file-name} and @code{tags-table-list}.
681
682@node Find Tag
683@subsection Finding a Tag
684
685 The most important thing that a tags table enables you to do is to find
686the definition of a specific tag.
687
688@table @kbd
689@item M-.@: @var{tag} @key{RET}
690Find first definition of @var{tag} (@code{find-tag}).
691@item C-u M-.
692Find next alternate definition of last tag specified.
693@item C-u - M-.
694Go back to previous tag found.
695@item C-M-. @var{pattern} @key{RET}
696Find a tag whose name matches @var{pattern} (@code{find-tag-regexp}).
697@item C-u C-M-.
698Find the next tag whose name matches the last pattern used.
699@item C-x 4 .@: @var{tag} @key{RET}
700Find first definition of @var{tag}, but display it in another window
701(@code{find-tag-other-window}).
702@item C-x 5 .@: @var{tag} @key{RET}
703Find first definition of @var{tag}, and create a new frame to select the
704buffer (@code{find-tag-other-frame}).
705@item M-*
706Pop back to where you previously invoked @kbd{M-.} and friends.
707@end table
708
709@kindex M-.
710@findex find-tag
711 @kbd{M-.}@: (@code{find-tag}) is the command to find the definition of
712a specified tag. It searches through the tags table for that tag, as a
713string, and then uses the tags table info to determine the file that the
714definition is in and the approximate character position in the file of
715the definition. Then @code{find-tag} visits that file, moves point to
716the approximate character position, and searches ever-increasing
717distances away to find the tag definition.
718
719 If an empty argument is given (just type @key{RET}), the balanced
720expression in the buffer before or around point is used as the
721@var{tag} argument. @xref{Expressions}.
722
723 You don't need to give @kbd{M-.} the full name of the tag; a part
724will do. This is because @kbd{M-.} finds tags in the table which
725contain @var{tag} as a substring. However, it prefers an exact match
726to a substring match. To find other tags that match the same
727substring, give @code{find-tag} a numeric argument, as in @kbd{C-u
728M-.}; this does not read a tag name, but continues searching the tags
729table's text for another tag containing the same substring last used.
730If you have a real @key{META} key, @kbd{M-0 M-.}@: is an easier
731alternative to @kbd{C-u M-.}.
732
733@kindex C-x 4 .
734@findex find-tag-other-window
735@kindex C-x 5 .
736@findex find-tag-other-frame
737 Like most commands that can switch buffers, @code{find-tag} has a
738variant that displays the new buffer in another window, and one that
739makes a new frame for it. The former is @kbd{C-x 4 .}, which invokes
740the command @code{find-tag-other-window}. The latter is @kbd{C-x 5 .},
741which invokes @code{find-tag-other-frame}.
742
743 To move back to places you've found tags recently, use @kbd{C-u -
744M-.}; more generally, @kbd{M-.} with a negative numeric argument. This
745command can take you to another buffer. @kbd{C-x 4 .} with a negative
746argument finds the previous tag location in another window.
747
748@kindex M-*
749@findex pop-tag-mark
750@vindex find-tag-marker-ring-length
751 As well as going back to places you've found tags recently, you can go
752back to places @emph{from where} you found them. Use @kbd{M-*}, which
753invokes the command @code{pop-tag-mark}, for this. Typically you would
754find and study the definition of something with @kbd{M-.} and then
755return to where you were with @kbd{M-*}.
756
757 Both @kbd{C-u - M-.} and @kbd{M-*} allow you to retrace your steps to
758a depth determined by the variable @code{find-tag-marker-ring-length}.
759
760@findex find-tag-regexp
761@kindex C-M-.
762 The command @kbd{C-M-.} (@code{find-tag-regexp}) visits the tags that
763match a specified regular expression. It is just like @kbd{M-.} except
764that it does regexp matching instead of substring matching.
765
766@node Tags Search
767@subsection Searching and Replacing with Tags Tables
768@cindex search and replace in multiple files
769@cindex multiple-file search and replace
770
771 The commands in this section visit and search all the files listed in the
772selected tags table, one by one. For these commands, the tags table serves
773only to specify a sequence of files to search.
774
775@table @kbd
776@item M-x tags-search @key{RET} @var{regexp} @key{RET}
777Search for @var{regexp} through the files in the selected tags
778table.
779@item M-x tags-query-replace @key{RET} @var{regexp} @key{RET} @var{replacement} @key{RET}
780Perform a @code{query-replace-regexp} on each file in the selected tags table.
781@item M-,
782Restart one of the commands above, from the current location of point
783(@code{tags-loop-continue}).
784@end table
785
786@findex tags-search
787 @kbd{M-x tags-search} reads a regexp using the minibuffer, then
788searches for matches in all the files in the selected tags table, one
789file at a time. It displays the name of the file being searched so you
790can follow its progress. As soon as it finds an occurrence,
791@code{tags-search} returns.
792
793@kindex M-,
794@findex tags-loop-continue
795 Having found one match, you probably want to find all the rest. To find
796one more match, type @kbd{M-,} (@code{tags-loop-continue}) to resume the
797@code{tags-search}. This searches the rest of the current buffer, followed
798by the remaining files of the tags table.@refill
799
800@findex tags-query-replace
801 @kbd{M-x tags-query-replace} performs a single
802@code{query-replace-regexp} through all the files in the tags table. It
803reads a regexp to search for and a string to replace with, just like
804ordinary @kbd{M-x query-replace-regexp}. It searches much like @kbd{M-x
805tags-search}, but repeatedly, processing matches according to your
806input. @xref{Replace}, for more information on query replace.
807
808@vindex tags-case-fold-search
809@cindex case-sensitivity and tags search
810 You can control the case-sensitivity of tags search commands by
811customizing the value of the variable @code{tags-case-fold-search}. The
812default is to use the same setting as the value of
813@code{case-fold-search} (@pxref{Search Case}).
814
815 It is possible to get through all the files in the tags table with a
816single invocation of @kbd{M-x tags-query-replace}. But often it is
817useful to exit temporarily, which you can do with any input event that
818has no special query replace meaning. You can resume the query replace
819subsequently by typing @kbd{M-,}; this command resumes the last tags
820search or replace command that you did.
821
822 The commands in this section carry out much broader searches than the
823@code{find-tag} family. The @code{find-tag} commands search only for
824definitions of tags that match your substring or regexp. The commands
825@code{tags-search} and @code{tags-query-replace} find every occurrence
826of the regexp, as ordinary search commands and replace commands do in
827the current buffer.
828
829 These commands create buffers only temporarily for the files that they
830have to search (those which are not already visited in Emacs buffers).
831Buffers in which no match is found are quickly killed; the others
832continue to exist.
833
834 It may have struck you that @code{tags-search} is a lot like
835@code{grep}. You can also run @code{grep} itself as an inferior of
836Emacs and have Emacs show you the matching lines one by one. This works
837much like running a compilation; finding the source locations of the
838@code{grep} matches works like finding the compilation errors.
839@xref{Compilation}.
840
841@node List Tags
842@subsection Tags Table Inquiries
843
844@table @kbd
845@item M-x list-tags @key{RET} @var{file} @key{RET}
846Display a list of the tags defined in the program file @var{file}.
847@item M-x tags-apropos @key{RET} @var{regexp} @key{RET}
848Display a list of all tags matching @var{regexp}.
849@end table
850
851@findex list-tags
852 @kbd{M-x list-tags} reads the name of one of the files described by
853the selected tags table, and displays a list of all the tags defined in
854that file. The ``file name'' argument is really just a string to
855compare against the file names recorded in the tags table; it is read as
856a string rather than as a file name. Therefore, completion and
857defaulting are not available, and you must enter the file name the same
858way it appears in the tags table. Do not include a directory as part of
859the file name unless the file name recorded in the tags table includes a
860directory.
861
862@findex tags-apropos
863@vindex tags-apropos-verbose
864 @kbd{M-x tags-apropos} is like @code{apropos} for tags
865(@pxref{Apropos}). It finds all the tags in the selected tags table
866whose entries match @var{regexp}, and displays them. If the variable
867@code{tags-apropos-verbose} is non-@code{nil}, it displays the names
868of the tags files together with the tag names.
869
870@vindex tags-tag-face
871@vindex tags-apropos-additional-actions
872You can customize the appearance of the output with the face
873@code{tags-tag-face}. You can display additional output with @kbd{M-x
874tags-apropos} by customizing the variable
875@code{tags-apropos-additional-actions}---see its documentation for
876details.
877
878 You can also use the collection of tag names to complete a symbol
879name in the buffer. @xref{Symbol Completion}.
880
ebe19e1b 881@node Emerge
f00366c2
RS
882@section Merging Files with Emerge
883@cindex Emerge
884@cindex merging files
885
886It's not unusual for programmers to get their signals crossed and modify
887the same program in two different directions. To recover from this
888confusion, you need to merge the two versions. Emerge makes this
889easier. See also @ref{Comparing Files}, for commands to compare
b5f1c9fa 890in a more manual fashion, and @ref{Top, Ediff,, ediff, The Ediff Manual}.
f00366c2
RS
891
892@menu
893* Overview of Emerge:: How to start Emerge. Basic concepts.
894* Submodes of Emerge:: Fast mode vs. Edit mode.
895 Skip Prefers mode and Auto Advance mode.
896* State of Difference:: You do the merge by specifying state A or B
897 for each difference.
898* Merge Commands:: Commands for selecting a difference,
899 changing states of differences, etc.
900* Exiting Emerge:: What to do when you've finished the merge.
901* Combining in Emerge:: How to keep both alternatives for a difference.
902* Fine Points of Emerge:: Misc.
903@end menu
904
905@node Overview of Emerge
906@subsection Overview of Emerge
907
908To start Emerge, run one of these four commands:
909
910@table @kbd
911@item M-x emerge-files
912@findex emerge-files
913Merge two specified files.
914
915@item M-x emerge-files-with-ancestor
916@findex emerge-files-with-ancestor
917Merge two specified files, with reference to a common ancestor.
918
919@item M-x emerge-buffers
920@findex emerge-buffers
921Merge two buffers.
922
923@item M-x emerge-buffers-with-ancestor
924@findex emerge-buffers-with-ancestor
925Merge two buffers with reference to a common ancestor in a third
926buffer.
927@end table
928
929@cindex merge buffer (Emerge)
930@cindex A and B buffers (Emerge)
931 The Emerge commands compare two files or buffers, and display the
932comparison in three buffers: one for each input text (the @dfn{A buffer}
933and the @dfn{B buffer}), and one (the @dfn{merge buffer}) where merging
934takes place. The merge buffer shows the full merged text, not just the
935differences. Wherever the two input texts differ, you can choose which
936one of them to include in the merge buffer.
937
938 The Emerge commands that take input from existing buffers use only the
939accessible portions of those buffers, if they are narrowed
940(@pxref{Narrowing}).
941
942 If a common ancestor version is available, from which the two texts to
943be merged were both derived, Emerge can use it to guess which
944alternative is right. Wherever one current version agrees with the
945ancestor, Emerge presumes that the other current version is a deliberate
946change which should be kept in the merged version. Use the
947@samp{with-ancestor} commands if you want to specify a common ancestor
948text. These commands read three file or buffer names---variant A,
949variant B, and the common ancestor.
950
951 After the comparison is done and the buffers are prepared, the
952interactive merging starts. You control the merging by typing special
953@dfn{merge commands} in the merge buffer. The merge buffer shows you a
954full merged text, not just differences. For each run of differences
955between the input texts, you can choose which one of them to keep, or
956edit them both together.
957
958 The merge buffer uses a special major mode, Emerge mode, with commands
959for making these choices. But you can also edit the buffer with
960ordinary Emacs commands.
961
962 At any given time, the attention of Emerge is focused on one
963particular difference, called the @dfn{selected} difference. This
964difference is marked off in the three buffers like this:
965
966@example
967vvvvvvvvvvvvvvvvvvvv
968@var{text that differs}
969^^^^^^^^^^^^^^^^^^^^
970@end example
971
972@noindent
973Emerge numbers all the differences sequentially and the mode
974line always shows the number of the selected difference.
975
976 Normally, the merge buffer starts out with the A version of the text.
977But when the A version of a difference agrees with the common ancestor,
978then the B version is initially preferred for that difference.
979
980 Emerge leaves the merged text in the merge buffer when you exit. At
981that point, you can save it in a file with @kbd{C-x C-w}. If you give a
982numeric argument to @code{emerge-files} or
983@code{emerge-files-with-ancestor}, it reads the name of the output file
984using the minibuffer. (This is the last file name those commands read.)
985Then exiting from Emerge saves the merged text in the output file.
986
987 Normally, Emerge commands save the output buffer in its file when you
988exit. If you abort Emerge with @kbd{C-]}, the Emerge command does not
989save the output buffer, but you can save it yourself if you wish.
990
991@node Submodes of Emerge
992@subsection Submodes of Emerge
993
994 You can choose between two modes for giving merge commands: Fast mode
995and Edit mode. In Fast mode, basic merge commands are single
996characters, but ordinary Emacs commands are disabled. This is
997convenient if you use only merge commands. In Edit mode, all merge
998commands start with the prefix key @kbd{C-c C-c}, and the normal Emacs
999commands are also available. This allows editing the merge buffer, but
1000slows down Emerge operations.
1001
1002 Use @kbd{e} to switch to Edit mode, and @kbd{C-c C-c f} to switch to
1003Fast mode. The mode line indicates Edit and Fast modes with @samp{E}
1004and @samp{F}.
1005
1006 Emerge has two additional submodes that affect how particular merge
1007commands work: Auto Advance mode and Skip Prefers mode.
1008
1009 If Auto Advance mode is in effect, the @kbd{a} and @kbd{b} commands
1010advance to the next difference. This lets you go through the merge
1011faster as long as you simply choose one of the alternatives from the
1012input. The mode line indicates Auto Advance mode with @samp{A}.
1013
1014 If Skip Prefers mode is in effect, the @kbd{n} and @kbd{p} commands
1015skip over differences in states prefer-A and prefer-B (@pxref{State of
1016Difference}). Thus you see only differences for which neither version
1017is presumed ``correct.'' The mode line indicates Skip Prefers mode with
1018@samp{S}.
1019
1020@findex emerge-auto-advance-mode
1021@findex emerge-skip-prefers-mode
1022 Use the command @kbd{s a} (@code{emerge-auto-advance-mode}) to set or
1023clear Auto Advance mode. Use @kbd{s s}
1024(@code{emerge-skip-prefers-mode}) to set or clear Skip Prefers mode.
1025These commands turn on the mode with a positive argument, turns it off
1026with a negative or zero argument, and toggle the mode with no argument.
1027
1028@node State of Difference
1029@subsection State of a Difference
1030
1031 In the merge buffer, a difference is marked with lines of @samp{v} and
1032@samp{^} characters. Each difference has one of these seven states:
1033
1034@table @asis
1035@item A
1036The difference is showing the A version. The @kbd{a} command always
1037produces this state; the mode line indicates it with @samp{A}.
1038
1039@item B
1040The difference is showing the B version. The @kbd{b} command always
1041produces this state; the mode line indicates it with @samp{B}.
1042
1043@item default-A
1044@itemx default-B
1045The difference is showing the A or the B state by default, because you
1046haven't made a choice. All differences start in the default-A state
1047(and thus the merge buffer is a copy of the A buffer), except those for
1048which one alternative is ``preferred'' (see below).
1049
1050When you select a difference, its state changes from default-A or
1051default-B to plain A or B. Thus, the selected difference never has
1052state default-A or default-B, and these states are never displayed in
1053the mode line.
1054
1055The command @kbd{d a} chooses default-A as the default state, and @kbd{d
1056b} chooses default-B. This chosen default applies to all differences
1057which you haven't ever selected and for which no alternative is preferred.
1058If you are moving through the merge sequentially, the differences you
1059haven't selected are those following the selected one. Thus, while
1060moving sequentially, you can effectively make the A version the default
1061for some sections of the merge buffer and the B version the default for
1062others by using @kbd{d a} and @kbd{d b} between sections.
1063
1064@item prefer-A
1065@itemx prefer-B
1066The difference is showing the A or B state because it is
1067@dfn{preferred}. This means that you haven't made an explicit choice,
1068but one alternative seems likely to be right because the other
1069alternative agrees with the common ancestor. Thus, where the A buffer
1070agrees with the common ancestor, the B version is preferred, because
1071chances are it is the one that was actually changed.
1072
1073These two states are displayed in the mode line as @samp{A*} and @samp{B*}.
1074
1075@item combined
1076The difference is showing a combination of the A and B states, as a
1077result of the @kbd{x c} or @kbd{x C} commands.
1078
1079Once a difference is in this state, the @kbd{a} and @kbd{b} commands
1080don't do anything to it unless you give them a numeric argument.
1081
1082The mode line displays this state as @samp{comb}.
1083@end table
1084
1085@node Merge Commands
1086@subsection Merge Commands
1087
1088 Here are the Merge commands for Fast mode; in Edit mode, precede them
1089with @kbd{C-c C-c}:
1090
1091@table @kbd
1092@item p
1093Select the previous difference.
1094
1095@item n
1096Select the next difference.
1097
1098@item a
1099Choose the A version of this difference.
1100
1101@item b
1102Choose the B version of this difference.
1103
1104@item C-u @var{n} j
1105Select difference number @var{n}.
1106
1107@item .
1108Select the difference containing point. You can use this command in the
1109merge buffer or in the A or B buffer.
1110
1111@item q
1112Quit---finish the merge.
1113
1114@item C-]
1115Abort---exit merging and do not save the output.
1116
1117@item f
1118Go into Fast mode. (In Edit mode, this is actually @kbd{C-c C-c f}.)
1119
1120@item e
1121Go into Edit mode.
1122
1123@item l
1124Recenter (like @kbd{C-l}) all three windows.
1125
1126@item -
1127Specify part of a prefix numeric argument.
1128
1129@item @var{digit}
1130Also specify part of a prefix numeric argument.
1131
1132@item d a
1133Choose the A version as the default from here down in
1134the merge buffer.
1135
1136@item d b
1137Choose the B version as the default from here down in
1138the merge buffer.
1139
1140@item c a
1141Copy the A version of this difference into the kill ring.
1142
1143@item c b
1144Copy the B version of this difference into the kill ring.
1145
1146@item i a
1147Insert the A version of this difference at point.
1148
1149@item i b
1150Insert the B version of this difference at point.
1151
1152@item m
1153Put point and mark around the difference.
1154
1155@item ^
1156Scroll all three windows down (like @kbd{M-v}).
1157
1158@item v
1159Scroll all three windows up (like @kbd{C-v}).
1160
1161@item <
1162Scroll all three windows left (like @kbd{C-x <}).
1163
1164@item >
1165Scroll all three windows right (like @kbd{C-x >}).
1166
1167@item |
1168Reset horizontal scroll on all three windows.
1169
1170@item x 1
1171Shrink the merge window to one line. (Use @kbd{C-u l} to restore it
1172to full size.)
1173
1174@item x c
1175Combine the two versions of this difference (@pxref{Combining in
1176Emerge}).
1177
1178@item x f
1179Show the names of the files/buffers Emerge is operating on, in a Help
1180window. (Use @kbd{C-u l} to restore windows.)
1181
1182@item x j
1183Join this difference with the following one.
1184(@kbd{C-u x j} joins this difference with the previous one.)
1185
1186@item x s
1187Split this difference into two differences. Before you use this
1188command, position point in each of the three buffers at the place where
1189you want to split the difference.
1190
1191@item x t
1192Trim identical lines off the top and bottom of the difference.
1193Such lines occur when the A and B versions are
1194identical but differ from the ancestor version.
1195@end table
1196
1197@node Exiting Emerge
1198@subsection Exiting Emerge
1199
1200 The @kbd{q} command (@code{emerge-quit}) finishes the merge, storing
1201the results into the output file if you specified one. It restores the
1202A and B buffers to their proper contents, or kills them if they were
1203created by Emerge and you haven't changed them. It also disables the
1204Emerge commands in the merge buffer, since executing them later could
1205damage the contents of the various buffers.
1206
1207 @kbd{C-]} aborts the merge. This means exiting without writing the
1208output file. If you didn't specify an output file, then there is no
1209real difference between aborting and finishing the merge.
1210
1211 If the Emerge command was called from another Lisp program, then its
1212return value is @code{t} for successful completion, or @code{nil} if you
1213abort.
1214
1215@node Combining in Emerge
1216@subsection Combining the Two Versions
1217
1218 Sometimes you want to keep @emph{both} alternatives for a particular
1219difference. To do this, use @kbd{x c}, which edits the merge buffer
1220like this:
1221
1222@example
1223@group
1224#ifdef NEW
1225@var{version from A buffer}
1226#else /* not NEW */
1227@var{version from B buffer}
1228#endif /* not NEW */
1229@end group
1230@end example
1231
1232@noindent
1233@vindex emerge-combine-versions-template
1234While this example shows C preprocessor conditionals delimiting the two
1235alternative versions, you can specify the strings to use by setting
1236the variable @code{emerge-combine-versions-template} to a string of your
1237choice. In the string, @samp{%a} says where to put version A, and
1238@samp{%b} says where to put version B. The default setting, which
1239produces the results shown above, looks like this:
1240
1241@example
1242@group
1243"#ifdef NEW\n%a#else /* not NEW */\n%b#endif /* not NEW */\n"
1244@end group
1245@end example
1246
1247@node Fine Points of Emerge
1248@subsection Fine Points of Emerge
1249
1250 During the merge, you mustn't try to edit the A and B buffers yourself.
1251Emerge modifies them temporarily, but ultimately puts them back the way
1252they were.
1253
1254 You can have any number of merges going at once---just don't use any one
1255buffer as input to more than one merge at once, since the temporary
1256changes made in these buffers would get in each other's way.
1257
1258 Starting Emerge can take a long time because it needs to compare the
1259files fully. Emacs can't do anything else until @code{diff} finishes.
1260Perhaps in the future someone will change Emerge to do the comparison in
1261the background when the input files are large---then you could keep on
1262doing other things with Emacs until Emerge is ready to accept
1263commands.
1264
1265@vindex emerge-startup-hook
1266 After setting up the merge, Emerge runs the hook
1267@code{emerge-startup-hook} (@pxref{Hooks}).