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