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