Convert consecutive FSF copyright years to ranges.
[bpt/emacs.git] / doc / emacs / emerge-xtra.texi
CommitLineData
6f585e44 1@c This is part of the Emacs manual.
73b0cd50 2@c Copyright (C) 2004-2011
8838673e 3@c Free Software Foundation, Inc.
6f585e44
EZ
4@c See file emacs.texi for copying conditions.
5@c
c5184807
EZ
6@c This file is included either in emacs-xtra.texi (when producing the
7@c printed version) or in the main Emacs manual (for the on-line version).
8@node Emerge
9@section Merging Files with Emerge
10@cindex Emerge
11@cindex merging files
12
13 It's not unusual for programmers to get their signals crossed and
14modify the same program in two different directions. To recover from
15this confusion, you need to merge the two versions. Emerge makes this
c4945d6a
EZ
16easier. For other ways to compare files, see
17@iftex
18@ref{Comparing Files,,, emacs, the Emacs Manual},
19@end iftex
20@ifnottex
21@ref{Comparing Files},
22@end ifnottex
23and @ref{Top, Ediff,, ediff, The Ediff Manual}.
c5184807
EZ
24
25@menu
8838673e
GM
26* Overview of Emerge:: How to start Emerge. Basic concepts.
27* Submodes of Emerge:: Fast mode vs. Edit mode.
28 Skip Prefers mode and Auto Advance mode.
29* State of Difference:: You do the merge by specifying state A or B
30 for each difference.
31* Merge Commands:: Commands for selecting a difference,
32 changing states of differences, etc.
33* Exiting Emerge:: What to do when you've finished the merge.
34* Combining in Emerge:: How to keep both alternatives for a difference.
35* Fine Points of Emerge:: Miscellaneous issues.
c5184807
EZ
36@end menu
37
38@node Overview of Emerge
39@subsection Overview of Emerge
40
41 To start Emerge, run one of these four commands:
42
43@table @kbd
44@item M-x emerge-files
45@findex emerge-files
46Merge two specified files.
47
48@item M-x emerge-files-with-ancestor
49@findex emerge-files-with-ancestor
50Merge two specified files, with reference to a common ancestor.
51
52@item M-x emerge-buffers
53@findex emerge-buffers
54Merge two buffers.
55
56@item M-x emerge-buffers-with-ancestor
57@findex emerge-buffers-with-ancestor
58Merge two buffers with reference to a common ancestor in a third
59buffer.
60@end table
61
62@cindex merge buffer (Emerge)
63@cindex A and B buffers (Emerge)
64 The Emerge commands compare two files or buffers, and display the
65comparison in three buffers: one for each input text (the @dfn{A buffer}
66and the @dfn{B buffer}), and one (the @dfn{merge buffer}) where merging
67takes place. The merge buffer shows the full merged text, not just the
68differences. Wherever the two input texts differ, you can choose which
69one of them to include in the merge buffer.
70
71 The Emerge commands that take input from existing buffers use only
72the accessible portions of those buffers, if they are narrowed.
c4945d6a 73@iftex
c5184807 74@xref{Narrowing,,, emacs, the Emacs Manual}.
c4945d6a
EZ
75@end iftex
76@ifnottex
77@xref{Narrowing}.
78@end ifnottex
c5184807
EZ
79
80
81 If a common ancestor version is available, from which the two texts to
82be merged were both derived, Emerge can use it to guess which
83alternative is right. Wherever one current version agrees with the
84ancestor, Emerge presumes that the other current version is a deliberate
85change which should be kept in the merged version. Use the
86@samp{with-ancestor} commands if you want to specify a common ancestor
87text. These commands read three file or buffer names---variant A,
88variant B, and the common ancestor.
89
90 After the comparison is done and the buffers are prepared, the
91interactive merging starts. You control the merging by typing special
92@dfn{merge commands} in the merge buffer (@pxref{Merge Commands}).
93For each run of differences between the input texts, you can choose
94which one of them to keep, or edit them both together.
95
96 The merge buffer uses a special major mode, Emerge mode, with commands
97for making these choices. But you can also edit the buffer with
98ordinary Emacs commands.
99
100 At any given time, the attention of Emerge is focused on one
101particular difference, called the @dfn{selected} difference. This
102difference is marked off in the three buffers like this:
103
104@example
105vvvvvvvvvvvvvvvvvvvv
106@var{text that differs}
107^^^^^^^^^^^^^^^^^^^^
108@end example
109
110@noindent
111Emerge numbers all the differences sequentially and the mode
112line always shows the number of the selected difference.
113
114 Normally, the merge buffer starts out with the A version of the text.
115But when the A version of a difference agrees with the common ancestor,
116then the B version is initially preferred for that difference.
117
118 Emerge leaves the merged text in the merge buffer when you exit. At
119that point, you can save it in a file with @kbd{C-x C-w}. If you give a
120numeric argument to @code{emerge-files} or
121@code{emerge-files-with-ancestor}, it reads the name of the output file
122using the minibuffer. (This is the last file name those commands read.)
123Then exiting from Emerge saves the merged text in the output file.
124
125 Normally, Emerge commands save the output buffer in its file when you
126exit. If you abort Emerge with @kbd{C-]}, the Emerge command does not
127save the output buffer, but you can save it yourself if you wish.
128
129@node Submodes of Emerge
130@subsection Submodes of Emerge
131
132 You can choose between two modes for giving merge commands: Fast mode
133and Edit mode. In Fast mode, basic merge commands are single
134characters, but ordinary Emacs commands are disabled. This is
135convenient if you use only merge commands. In Edit mode, all merge
136commands start with the prefix key @kbd{C-c C-c}, and the normal Emacs
137commands are also available. This allows editing the merge buffer, but
138slows down Emerge operations.
139
140 Use @kbd{e} to switch to Edit mode, and @kbd{C-c C-c f} to switch to
141Fast mode. The mode line indicates Edit and Fast modes with @samp{E}
142and @samp{F}.
143
144 Emerge has two additional submodes that affect how particular merge
145commands work: Auto Advance mode and Skip Prefers mode.
146
147 If Auto Advance mode is in effect, the @kbd{a} and @kbd{b} commands
148advance to the next difference. This lets you go through the merge
149faster as long as you simply choose one of the alternatives from the
150input. The mode line indicates Auto Advance mode with @samp{A}.
151
152 If Skip Prefers mode is in effect, the @kbd{n} and @kbd{p} commands
d07e72d3
GM
153skip over differences in states ``prefer-A'' and ``prefer-B''
154(@pxref{State of Difference}). Thus you see only differences for
155which neither version is presumed ``correct.'' The mode line
156indicates Skip Prefers mode with @samp{S}. This mode is only relevant
157when there is an ancestor.
c5184807
EZ
158
159@findex emerge-auto-advance-mode
160@findex emerge-skip-prefers-mode
161 Use the command @kbd{s a} (@code{emerge-auto-advance-mode}) to set or
162clear Auto Advance mode. Use @kbd{s s}
163(@code{emerge-skip-prefers-mode}) to set or clear Skip Prefers mode.
d07e72d3 164These commands turn on the mode with a positive argument, turn it off
c5184807
EZ
165with a negative or zero argument, and toggle the mode with no argument.
166
167@node State of Difference
168@subsection State of a Difference
169
170 In the merge buffer, a difference is marked with lines of @samp{v} and
171@samp{^} characters. Each difference has one of these seven states:
172
173@table @asis
174@item A
175The difference is showing the A version. The @kbd{a} command always
176produces this state; the mode line indicates it with @samp{A}.
177
178@item B
179The difference is showing the B version. The @kbd{b} command always
180produces this state; the mode line indicates it with @samp{B}.
181
182@item default-A
183@itemx default-B
184The difference is showing the A or the B state by default, because you
185haven't made a choice. All differences start in the default-A state
186(and thus the merge buffer is a copy of the A buffer), except those for
187which one alternative is ``preferred'' (see below).
188
189When you select a difference, its state changes from default-A or
190default-B to plain A or B. Thus, the selected difference never has
191state default-A or default-B, and these states are never displayed in
192the mode line.
193
194The command @kbd{d a} chooses default-A as the default state, and @kbd{d
195b} chooses default-B. This chosen default applies to all differences
d07e72d3 196that you have never selected and for which no alternative is preferred.
c5184807
EZ
197If you are moving through the merge sequentially, the differences you
198haven't selected are those following the selected one. Thus, while
199moving sequentially, you can effectively make the A version the default
200for some sections of the merge buffer and the B version the default for
201others by using @kbd{d a} and @kbd{d b} between sections.
202
203@item prefer-A
204@itemx prefer-B
205The difference is showing the A or B state because it is
206@dfn{preferred}. This means that you haven't made an explicit choice,
207but one alternative seems likely to be right because the other
208alternative agrees with the common ancestor. Thus, where the A buffer
209agrees with the common ancestor, the B version is preferred, because
210chances are it is the one that was actually changed.
211
212These two states are displayed in the mode line as @samp{A*} and @samp{B*}.
213
214@item combined
215The difference is showing a combination of the A and B states, as a
216result of the @kbd{x c} or @kbd{x C} commands.
217
218Once a difference is in this state, the @kbd{a} and @kbd{b} commands
219don't do anything to it unless you give them a numeric argument.
220
221The mode line displays this state as @samp{comb}.
222@end table
223
224@node Merge Commands
225@subsection Merge Commands
226
227 Here are the Merge commands for Fast mode; in Edit mode, precede them
228with @kbd{C-c C-c}:
229
230@table @kbd
231@item p
232Select the previous difference.
233
234@item n
235Select the next difference.
236
237@item a
238Choose the A version of this difference.
239
240@item b
241Choose the B version of this difference.
242
243@item C-u @var{n} j
244Select difference number @var{n}.
245
246@item .
d07e72d3
GM
247Select the difference containing point.
248@c [Does not work in the A or B buffer?]
249@c You can use this command in the merge buffer or in the A or B buffer.
c5184807
EZ
250
251@item q
252Quit---finish the merge.
253
254@item C-]
255Abort---exit merging and do not save the output.
256
257@item f
258Go into Fast mode. (In Edit mode, this is actually @kbd{C-c C-c f}.)
259
260@item e
261Go into Edit mode.
262
263@item l
d07e72d3
GM
264Recenter (like @kbd{C-l}) all three windows. With an argument,
265reestablish the default three-window display.
c5184807
EZ
266
267@item -
268Specify part of a prefix numeric argument.
269
270@item @var{digit}
271Also specify part of a prefix numeric argument.
272
273@item d a
274Choose the A version as the default from here down in
275the merge buffer.
276
277@item d b
278Choose the B version as the default from here down in
279the merge buffer.
280
281@item c a
282Copy the A version of this difference into the kill ring.
283
284@item c b
285Copy the B version of this difference into the kill ring.
286
287@item i a
288Insert the A version of this difference at point.
289
290@item i b
291Insert the B version of this difference at point.
292
293@item m
294Put point and mark around the difference.
295
296@item ^
297Scroll all three windows down (like @kbd{M-v}).
298
299@item v
300Scroll all three windows up (like @kbd{C-v}).
301
302@item <
303Scroll all three windows left (like @kbd{C-x <}).
304
305@item >
306Scroll all three windows right (like @kbd{C-x >}).
307
308@item |
309Reset horizontal scroll on all three windows.
310
311@item x 1
312Shrink the merge window to one line. (Use @kbd{C-u l} to restore it
313to full size.)
314
315@item x c
316Combine the two versions of this difference (@pxref{Combining in
317Emerge}).
318
319@item x f
320Show the names of the files/buffers Emerge is operating on, in a Help
321window. (Use @kbd{C-u l} to restore windows.)
322
323@item x j
324Join this difference with the following one.
325(@kbd{C-u x j} joins this difference with the previous one.)
326
327@item x s
328Split this difference into two differences. Before you use this
329command, position point in each of the three buffers at the place where
330you want to split the difference.
331
332@item x t
333Trim identical lines off the top and bottom of the difference.
334Such lines occur when the A and B versions are
335identical but differ from the ancestor version.
336@end table
337
338@node Exiting Emerge
339@subsection Exiting Emerge
340
341 The @kbd{q} command (@code{emerge-quit}) finishes the merge, storing
342the results into the output file if you specified one. It restores the
343A and B buffers to their proper contents, or kills them if they were
344created by Emerge and you haven't changed them. It also disables the
345Emerge commands in the merge buffer, since executing them later could
346damage the contents of the various buffers.
347
348 @kbd{C-]} aborts the merge. This means exiting without writing the
349output file. If you didn't specify an output file, then there is no
350real difference between aborting and finishing the merge.
351
352 If the Emerge command was called from another Lisp program, then its
353return value is @code{t} for successful completion, or @code{nil} if you
354abort.
355
356@node Combining in Emerge
357@subsection Combining the Two Versions
358
359 Sometimes you want to keep @emph{both} alternatives for a particular
360difference. To do this, use @kbd{x c}, which edits the merge buffer
361like this:
362
363@example
364@group
365#ifdef NEW
366@var{version from A buffer}
367#else /* not NEW */
368@var{version from B buffer}
369#endif /* not NEW */
370@end group
371@end example
372
373@noindent
374@vindex emerge-combine-versions-template
375While this example shows C preprocessor conditionals delimiting the two
376alternative versions, you can specify the strings to use by setting
377the variable @code{emerge-combine-versions-template} to a string of your
378choice. In the string, @samp{%a} says where to put version A, and
379@samp{%b} says where to put version B. The default setting, which
380produces the results shown above, looks like this:
381
382@example
383@group
384"#ifdef NEW\n%a#else /* not NEW */\n%b#endif /* not NEW */\n"
385@end group
386@end example
387
388@node Fine Points of Emerge
389@subsection Fine Points of Emerge
390
391 During the merge, you mustn't try to edit the A and B buffers yourself.
392Emerge modifies them temporarily, but ultimately puts them back the way
393they were.
394
395 You can have any number of merges going at once---just don't use any one
396buffer as input to more than one merge at once, since the temporary
397changes made in these buffers would get in each other's way.
398
399 Starting Emerge can take a long time because it needs to compare the
400files fully. Emacs can't do anything else until @code{diff} finishes.
401Perhaps in the future someone will change Emerge to do the comparison in
402the background when the input files are large---then you could keep on
403doing other things with Emacs until Emerge is ready to accept
404commands.
405
406@vindex emerge-startup-hook
407 After setting up the merge, Emerge runs the hook
c4945d6a
EZ
408@code{emerge-startup-hook}.
409@iftex
410@xref{Hooks,,, emacs, the Emacs Manual}.
411@end iftex
412@ifnottex
413@xref{Hooks}.
414@end ifnottex