* bytecomp.el: Declare unread-command-char an obsolete variable.
[bpt/emacs.git] / lisp / emerge.el
1 ;;; emerge.el --- merge diffs under Emacs control
2 ;;; The author has placed this file in the public domain.
3
4 ;; Author: Dale R. Worley <drw@math.mit.edu>
5 ;; Version: 4
6 ;; Keywords: unix, tools
7
8 ;;; Commentary:
9
10 ; - Changes from version 3 to version 4
11 ;
12 ; More configuration variables are marked as user options.
13 ;
14 ; Code is included for an improved version of make-auto-save-file-name
15 ; which eliminates many problems with the default version. See the
16 ; documentation of emerge-make-auto-save-file-name to see how to
17 ; activate it.
18 ;
19 ; Emerge now works with Gnu diff3, which can produce the groups of lines
20 ; from the various files in the order 1, 2, 3 or 1, 3, 2.
21 ;
22 ; Added x f command to show what files or buffers are being operated on.
23 ;
24 ; The merge buffer now starts read-only, which being in fast mode it
25 ; should be.
26 ;
27 ; When merging buffers, Emerge writes their contents into temporary
28 ; files in the directory $TMPDIR (if it is defined), or /tmp by default.
29 ;
30 ; Added x j command to join two differences.
31 ;
32 ; Added x s command to split a difference into two differences.
33 ;
34 ; Added emerge-version variable and function to report the version of Emerge
35 ; being run.
36 ;
37 ; Added x t command to trim unchanged lines off top and bottom of
38 ; difference region.
39 ;
40 ; Added x d, x a, and x b commands to locate the differences at or near
41 ; a given location in one of the buffers.
42 ;
43 ; Emerge no longer tries to copy the minor modes from the A buffer to
44 ; the merge buffer, only the major mode.
45 ;
46 ; The programs executed to find the differences between versions of the file
47 ; are no longer controlled by emerge-diff/diff3-command, but rather by:
48 ; emerge-diff-program
49 ; Variable: *Name of the program which compares two files.
50 ; emerge-diff3-program
51 ; Variable: *Name of the program which compares an ancestor file
52 ; (first argument) and two variant files (second and third arguments).
53 ; emerge-diff-options
54 ; Variable: *Options to be passed to emerge-diff/diff3-program.
55 ;
56 ; The names of the files are expanded (see expand-file-name) before being
57 ; passed to emerge-diff/diff3-program, so diff need not invoked under a shell
58 ; that understands '~', for instance.
59 ;
60 ; If the diff/diff3 program reports errors, the user is notified and the
61 ; errors are displayed.
62 ;
63 ; The command "0j" can be used to suppress the flags from showing in the buffers.
64 ;
65 ; A discussion of the effect of the merge flags on indentation of code
66 ; has been added to the documentation.
67 ;
68 ; If kill-fix.el is loaded, Emerge control variables new have their
69 ; 'preserved' property set, so setting the major mode in the merge
70 ; buffer doesn't destroy Emerge's state.
71 ;
72 ; Added x c, x C, and x x commands to allow the A and B versions to be
73 ; combined into #ifdef - #endif forms.
74 ;
75 ; Replaced calls of "ding" to calls of "error" where appropriate.
76 ;
77 ; Added x m command to allow major mode of merge buffer to be changed.
78 ;
79 ; Added x 1 command to shrink the merge window to one line.
80 ;
81 ; Added emerge-startup-hook to allow customization.
82 ;
83 ; Fixed a bug that is activated when a remote merge request is made when
84 ; the minibuffer window is selected.
85 ;
86 ; - Changes from version 2 to version 3
87 ;
88 ; The directory into which temporary files are written is now controlled
89 ; by a user option (emerge-temp-file-prefix).
90 ;
91 ; The A and B versions of the difference can be loaded into the kill
92 ; ring with the "c a" and "c b" commands.
93 ;
94 ; The A and B versions of the difference can be inserted into the merge
95 ; buffer with the "i a" and "i b" commands.
96 ;
97 ; The difference region of the merge buffer can be surrounded by the
98 ; point and mark with the "m" command.
99 ;
100 ; The three windows can be scrolled together with the "^", "v", "<",
101 ; ">", and "|" commands.
102 ;
103 ; The "s s" and "s a" commands report the state of the option in the
104 ; echo area. Similarly, the "f" and "e" commands report what they do in
105 ; the echo area.
106 ;
107 ; The "q" command has been revamped, and its behavior is now controlled
108 ; by the manner in which Emerge is started. In particular, if you wish
109 ; to write the merge buffer into a file upon exiting, invoke
110 ; emerge-files[-with-ancestor] with a prefix argument, and it will
111 ; prompt you for the file name. Then exiting will write the merge
112 ; buffer to the file, unless "q" is given a prefix argument.
113 ;
114 ; The "i a" and "i b" commands now work in fast mode.
115 ;
116 ; The modifications that Emerge makes to save-buffer and write-file are
117 ; described.
118 ;
119 ; Emerge now handles merging narrowed buffers correctly.
120 ;
121 ; Emerge now isn't fooled when the buffer visiting a file is not the
122 ; same as the file on disk.
123 ;
124 ; - Starting
125 ;
126 ; To start Emerge, you must run one of four commands:
127 ;
128 ; emerge-files
129 ; emerge-files-with-ancestor
130 ; emerge-buffers
131 ; emerge-buffers-with-ancestor
132 ;
133 ; The "files" versions prompt you for two file names (the "A" and "B"
134 ; files), the "buffers" versions prompt you for two buffer names (the
135 ; "A" and "B" buffers). Emerge then runs a "diff" of the two entities
136 ; (emerge-buffers writes the buffers into temporary files for input to
137 ; diff) and digests the output to form a list of the differences between
138 ; the two files. Then three buffers are set up: two containing the
139 ; entities (emerge-files does a find-file (C-x C-f) on the files to get
140 ; them into buffers), and one, the "merge buffer", which contains the
141 ; working copy of the merged file that you are constructing. The three
142 ; buffers are put up in a nice three-window display, showing the A and B
143 ; buffers in the upper half and the merge buffer in the lower half.
144 ;
145 ; The versions of the command that say "with-ancestor" ask for a third
146 ; name, that of an entity which is a common ancestor from which the
147 ; versions being merged were derived. These commands use "diff3" to
148 ; compare all three versions. If one version of a difference agrees
149 ; with the ancestor, then it is presumed that the other version is the
150 ; "correct" version, and is said to be "preferred".
151 ;
152 ; (Note that if you use emerge-files, Emerge attempts to make sure that
153 ; file on disk and the file in the buffer are the same. If the file on
154 ; disk has been changed, Emerge offers to revert the buffer. If the
155 ; buffer has been modified, Emerge offers to save the buffer. If the
156 ; user declines the offer, or if the file on disk and the buffer have
157 ; both been modified, Emerge aborts with an error message. Emerge is
158 ; careful to widen the buffers containing the files if they have been
159 ; narrowed. If you use emerge-buffers, the buffers are not widened --
160 ; only the visible portion is used.)
161 ;
162 ; During the merge, the A and B buffers are read-only, so you don't
163 ; damage them. (This is because the A and B versions of the differences
164 ; are extracted from these buffers.) When you quit the merge, the
165 ; read-only/read-write status and modified flag on the A and B buffers
166 ; are restored. In addition, auto-saving of the A and B buffers is
167 ; suppressed during the merge. This is because Emerge modifies the A
168 ; and B buffers to point out the text of the differences, and it would
169 ; be useless to save these changes. (Just before suppressing
170 ; auto-saving, Emerge forces an auto-save.)
171 ;
172 ; If you give a prefix argument to emerge-files or
173 ; emerge-files-with-ancestor, it prompts you for another file name,
174 ; which is the file into which the merged file is to be written when you
175 ; exit Emerge. The output file name defaults to the A file name. If
176 ; you successfully quit Emerge, the merge buffer will be written to the
177 ; output file, and the buffers for the A, B, and ancestor buffers will
178 ; be deleted (if they exist and are not modified). If you abort Emerge,
179 ; the merge buffer will not be written and the buffers will not be
180 ; deleted.
181 ;
182 ; You can have any number of merges going at once -- just don't use any
183 ; one buffer as input to more than one merge at once, since that will
184 ; cause the read-only/modified/auto-save status save-and-restore to
185 ; screw up.
186 ;
187 ; Beware that when Emerge starts up, it does a diff or diff3 of the
188 ; files, which can take many minutes for long files with many
189 ; differences. Emacs can't do anything else until diff finishes.
190 ;
191 ; If diff or diff3 produces error messages, Emerge will beep and display
192 ; the error messages instead of the merge buffer. There will be a
193 ; message in the echo area giving the name of the merge buffer. Note
194 ; that this is really just an informational message -- you still have
195 ; switch to the merge buffer and abort the merge to restore the
196 ; conditions before you ran Emerge. (Emerge considers any output line
197 ; that does not match the regexp emerge-diff/diff3-ok-lines to be an
198 ; error message.)
199 ;
200 ; After the merge has been set up, Emerge runs the hooks in
201 ; emerge-startup-hook.
202 ;
203 ; - Merging
204 ;
205 ; Once you have started the merge, you manipulate the merge buffer with
206 ; special commands issued in the merge buffer. You may also edit the
207 ; buffer with ordinary Emacs commands. Emerge keeps track of each
208 ; difference between the A and B buffers and the corresponding section
209 ; of the merge buffer. Initially, all differences show the A version,
210 ; except those for which B is preferred (because A agrees with the
211 ; ancestor), which show the B version. Emerge always has its attention
212 ; focused on one particular difference, which is marked off in the three
213 ; buffers by "vvvvvvvvvvvvvvvvvvvv" above and "^^^^^^^^^^^^^^^^^^^^"
214 ; below. The number of the difference is shown in the mode line.
215 ;
216 ; A merge buffer can be in two modes: "fast" mode and "edit" mode. In
217 ; fast mode, emerge commands are single characters, and ordinary Emacs
218 ; commands are disabled. This makes Emerge operations fast, but
219 ; prevents you from doing more than selecing the A or the B version of
220 ; differences. In edit mode, all emerge commands must be prefixed with
221 ; C-c, and all (non-conflicting) Emacs commands are available. This
222 ; allows editing the merge buffer, but slows down Emerge operations.
223 ; Edit and fast modes are indicated by "F" and "E" in the minor modes in
224 ; the mode line.
225 ;
226 ; The Emerge commands are:
227 ;
228 ; p go to the previous difference
229 ; n go to the next difference
230 ; a select the A version of this difference
231 ; b select the B version of this difference
232 ; j go to a particular difference (prefix argument
233 ; specifies which difference) (0j suppresses display of
234 ; the flags)
235 ; q quit - finish the merge*
236 ; f go into fast mode
237 ; e go into edit mode
238 ; s a set/clear auto-advance mode*
239 ; s s set/clear skip-prefers mode*
240 ; l recenter (C-l) all three windows*
241 ; - and 0 through 9
242 ; prefix numeric arguments
243 ; d a select the A version as the default from here down in
244 ; the merge buffer*
245 ; d b select the B version as the default from here down in
246 ; the merge buffer*
247 ; c a copy the A version of the difference into the kill
248 ; ring
249 ; c b copy the B version of the difference into the kill
250 ; ring
251 ; i a insert the A version of the difference at the point
252 ; i b insert the B version of the difference at the point
253 ; m put the point and mark around the difference region
254 ; ^ scroll-down (like M-v) the three windows*
255 ; v scroll-up (like C-v) the three windows*
256 ; < scroll-left (like C-x <) the three windows*
257 ; > scroll-right (like C-x >) the three windows*
258 ; | reset horizontal scroll on the three windows*
259 ; x 1 shrink the merge window to one line (use C-u l to restore it
260 ; to full size)
261 ; x a find the difference containing a location in the A buffer*
262 ; x b find the difference containing a location in the B buffer*
263 ; x c combine the two versions of this difference*
264 ; x C combine the two versions of this difference, using a
265 ; register's value as the template*
266 ; x d find the difference containing a location in the merge buffer*
267 ; x f show the files/buffers Emerge is operating on in Help window
268 ; (use C-u l to restore windows)
269 ; x j join this difference with the following one
270 ; (C-u x j joins this difference with the previous one)
271 ; x l show line numbers of points in A, B, and merge buffers
272 ; x m change major mode of merge buffer*
273 ; x s split this difference into two differences
274 ; (first position the point in all three buffers to the places
275 ; to split the difference)
276 ; x t trim identical lines off top and bottom of difference
277 ; (such lines occur when the A and B versions are
278 ; identical but differ from the ancestor version)
279 ; x x set the template for the x c command*
280 ;
281 ; * - more details on these commands are given below
282 ;
283 ; emerge-version is a variable giving the version number of Emerge. It
284 ; is also a function which displays emerge-version (when called
285 ; interactively) or returns it (when called from a program).
286 ;
287 ; - Differences and their states
288 ;
289 ; A difference can have one of seven states:
290 ;
291 ; A: the difference is showing the A version.
292 ;
293 ; B: the difference is showing the B version.
294 ;
295 ; default-A and default-B: the difference is showing the A or B state,
296 ; but has never been selected by the user. All differences start in the
297 ; default-A state (and thus the merge buffer is a copy of the A buffer),
298 ; except those for which one buffer or another is preferred. When the
299 ; user selects the difference, it changes to the A or B state.
300 ;
301 ; prefer-A and prefer-B: the difference is showing the A or B state. In
302 ; addition, the other buffer (that is, for prefer-A, the B buffer; for
303 ; prefer-B, the A buffer) agrees with the ancestor buffer. Thus,
304 ; presumably, the displayed version is the correct one. The "a" and "b"
305 ; commands override these states, and turn them into the A and B states.
306 ;
307 ; combined: the difference is showing a combination of the A and B
308 ; states that was constructed by the "x c" or "x C" commands. Since
309 ; this state is neither the A or B states, the "a" and "b" commands
310 ; won't alter the difference unless they are given a prefix argument.
311 ;
312 ; The state of the currently selected difference is shown in the mode
313 ; line of the merge window:
314 ;
315 ; state display
316 ;
317 ; A A
318 ; B B
319 ; prefer-A A*
320 ; prefer-B B*
321 ; combined comb
322 ;
323 ; - Select default commands (d a and d b)
324 ;
325 ; The d a and d b commands change all default-A's to default-B's (or
326 ; vice-versa) from the selected difference on down to the end of the
327 ; file to default-A or default-B, respectively. (Since a difference
328 ; that has been selected can not have state default-A or default-B, it
329 ; will never be affected by d a or d b. This leads to the unexpected
330 ; result that d a or d b never affects the difference selected at the
331 ; moment, but prevents differences that you have already looked at from
332 ; changing unexpectedly.)
333 ;
334 ; If you work your way down from the top of the file, using d a and d b
335 ; at judicious points, you can effectivly make the A version the default
336 ; for some sections of the merge buffer and the B version the default
337 ; for others.
338 ;
339 ; - Exiting (q)
340 ;
341 ; The quit command finishes the merge session by restoring the state of
342 ; the A and B buffers and removing the markers around the currently
343 ; selected difference. It also disables the Emerge commands in the
344 ; merge buffer, since executing them later could damage the contents of
345 ; the various buffers.
346 ;
347 ; The action of "q" depends on how Emerge was started and whether "q"
348 ; was given a prefix argument. If there was no prefix argument, it is
349 ; considered a "successful" finish. If there was a prefix argument, it
350 ; is considered an "unsuccessful" finish. In either case, you are asked
351 ; to cofirm the exit, and the confirmation message tells which sort of
352 ; exit you are confirming.
353 ;
354 ; If Emerge was started by some other process, success/failure is
355 ; reported to the caller.
356 ;
357 ; If Emerge was started with emerge-files or emerge-files-with-ancestor,
358 ; if a prefix argument was given to that command, then you specified a
359 ; file into which the merge is to be written. A successful exit writes
360 ; the merge into the output file and then kills the A, B, and ancestor
361 ; buffers (so they aren't lying around to confuse you, since they
362 ; probably all have similar names).
363 ;
364 ; - Auto-advance mode (s a)
365 ;
366 ; If auto-advance mode is set, the "a" and "b" commands perform an "n"
367 ; (select next difference) afterward. When auto-advance mode is set,
368 ; it is indicated by "A" in the minor modes in the mode line.
369 ; "s a" with a positive argument sets auto-advance, with a non-positive
370 ; argument clears it, and with no argument toggles it.
371 ;
372 ; - Skip-prefers mode (s s)
373 ;
374 ; If skip-prefers mode is set, the "n" and "p" commands skip over
375 ; differences with states prefer-A and prefer-B. Thus you will only see
376 ; differences for which one version isn't presumed "correct". When
377 ; skip-prefers mode is set, it is indicated by "S" in the minor modes in
378 ; the mode line. "s s" with a positive argument sets auto-advance, with
379 ; a non-positive argument clears it, and with no argument toggles it.
380 ;
381 ; - Recenter (l)
382 ;
383 ; The Emerge "l" command causes the selected difference to be brought
384 ; into view in the three windows, or at least, whichever of the three
385 ; merge buffers are visible at the moment. If a prefix argument is
386 ; given, then the original three-window display is set up before the
387 ; difference texts are shown.
388 ;
389 ; - Scrolling the text (^, v, <, >, and |)
390 ;
391 ; Emerge has several commands which scroll all three windows by the same
392 ; amount, thus allowing you to easily compare the versions of the text.
393 ; The commands are "^" (scroll-up), "v" (scroll-down), "<"
394 ; (scroll-left), ">" (scroll-right), and "|" (reset horizontal
395 ; scrolling). (Remember that Emacs names scrolling commands by the
396 ; motion of the text with respect to the window, so C-v is called
397 ; "scroll-up".)
398 ;
399 ; If these commands (except "|") are given an argument, that is the
400 ; number of lines or characters by which the windows are scrolled.
401 ; Otherwise, the amount of motion is computed based on the dimensions of
402 ; the merge buffer window -- the height of the merge buffer window
403 ; (minus next-frame-context-lines), or half the width of the merge
404 ; buffer window. (The A and B version windows are assumed to be as high
405 ; as the merge window, but half as wide.) If the argument is just `C-u
406 ; -', then the scrolling is half the default amount.
407 ;
408 ; - Finding the difference at or near a location (x d, x a, and x b)
409 ;
410 ; The "x d" command selects the difference containing the current point
411 ; in the merge buffer. If there is no difference containing the point,
412 ; an error is given. An argument can be given to the command to change
413 ; this behavior: if the argument is positive (e.g., C-u), the next
414 ; following difference is selected; if the argument is negative (e.g.,
415 ; C-u -), the previous difference is selected.
416 ;
417 ; The "x a" and "x b" commands select the difference containing the
418 ; current point in the A and B buffers, respectively. Otherwise, they
419 ; act like the "x d" command. Note that although the point used in the
420 ; commands is not the merge buffer point, the commands can only be
421 ; issued in the merge buffer, because it is the only buffer with the
422 ; Emerge keymap.
423 ;
424 ; - Combining the two versions (x c, x C, and x x)
425 ;
426 ; Sometimes one wants to combine the two versions of a difference. For
427 ; instance, when merging two versions of a program, one wants to make
428 ; something like this:
429 ;
430 ; #ifdef NEW
431 ; ...new version of code...
432 ; #else /* NEW */
433 ; ...old version of code...
434 ; #endif /* NEW */
435 ;
436 ; The "x c" command will make such a combined version. (Note that any
437 ; combined version is not the same as either the A or B versions, and so
438 ; the "a" and "b" commands will refuse to alter it unless they are given
439 ; a prefix argument.) The combination is made under control of a
440 ; template, which is a character string with the following
441 ; interpolations:
442 ;
443 ; %a the A version of the difference
444 ; %b the B version of the difference
445 ; %% the character '%'
446 ;
447 ; Thus, the template used above is
448 ;
449 ; #ifdef NEW\n%b#else /* NEW */\n%a#endif /* NEW */\n
450 ;
451 ; (using \n here to represent newlines). The template is stored in the
452 ; variable emerge-combine-versions-template, and its initial value is
453 ; the one given above. The template can be set (from the current
454 ; region) by the "x x" command. (Be careful to get the newlines in the
455 ; template in the right places!) ("x x" was chosen by analogy with "C-x
456 ; x".) ("x x" is only available in the merge buffer, of course.
457 ; Elsewhere, M-x emerge-set-combine-versions-template can be used.) If
458 ; "x x" is given a prefix argument, emerge-combine-versions-template is
459 ; localized in the merge buffer before its value is set, so the "x x"
460 ; command's effect (and the effect of any later "x x" command in the
461 ; merge buffer) is only on the merge buffer.
462 ;
463 ; The "x C" command is like "x c", but it prompts for a character
464 ; which is the register whose value is to be used as the template.
465 ; This allows one to use multiple templates conveniently.
466 ;
467 ; - Changing the major mode of the edit buffer (x m)
468 ;
469 ; The "x m" command prompts for the name of a major-mode-setting command
470 ; and executes it. Ordinarily, major-mode-setting commands change the
471 ; mode line and local keymap, so the "x m" command then resets the
472 ; Emerge mode line and the fast or edit mode local keymap, as
473 ; appropriate.
474 ;
475 ; If you have already changed the major mode of the merge buffer and
476 ; lost the Emerge keymap, you can use M-x emerge-set-merge-mode to
477 ; execute this command.
478 ;
479 ; Beware that "x m" accepts any command name, not just
480 ; major-mode-setting commands.
481 ;
482 ; - Writing the merge buffer manually
483 ;
484 ; Emerge places a wrapper (emerge-query-and-call) on the key bindings of
485 ; save-buffer (usually "C-x C-s") and write-file (usually "C-x C-w"), in
486 ; order to protect the user from writing out the merge before it is
487 ; finished. Emerge-query-and-call asks the user if he is sure he wants
488 ; to write out the incomplete merge. If he answers yes, the buffer is
489 ; written out. The flags are suppressed while the write is being done.
490 ; As a result of this, the displayed portions of the buffers are
491 ; recentered (equivalent to "l").
492 ;
493 ; - Running Emerge standalone
494 ;
495 ; If you invoke emacs with the following arguments, you can execute
496 ; Emerge as a standalone program:
497 ;
498 ; emacs -l emerge -f emerge-files-command file-a file-b file-out
499 ;
500 ; emacs -l emerge -f emerge-files-with-ancestor-command
501 ; file-a file-b file-ancestor file-out
502 ;
503 ; When the user gives the "q" (quit) command, Emerge will write out the
504 ; merge buffer in file-out and terminate Emacs. If a prefix argument is
505 ; given, Emacs will terminate with an unsuccessful return code (1), if
506 ; not, it will terminate with a successful return code (0).
507 ;
508 ; - Invoking Emerge remotely
509 ;
510 ; If you use the Emacs client/server code that supports remote
511 ; execution, then you can invoke Emerge remotely by executing one of the
512 ; Lisp calls:
513 ;
514 ; (emerge-files-remote "file A" "file B" "output file")
515 ;
516 ; (emerge-files-with-ancestor-remote "file A" "file B"
517 ; "ancestor file" "output file")
518 ;
519 ; Returning a successful/unsuccessful return code is not yet supported
520 ; by the Emacs client/server code.
521 ;
522 ; Beware that in systems of networked workstations, even though all user
523 ; directories are shared between all the workstations, the /tmp
524 ; directory on each workstation is not shared, so writing files into
525 ; /tmp and then remotely invoking Emerge is not likely to work.
526 ;
527 ; - Effect of merge flags on indenting code
528 ;
529 ; The presence of the flags confuses the indentation code of C and
530 ; Emacs-Lisp modes. Starting the flag strings
531 ; (emerge-{before,after}-flag) with '#' (for C) or ';' (for Lisp)
532 ; prevents the indentation code from noticing the flags. Remember to
533 ; change the flag strings before loading Emerge, or to execute
534 ; emerge-new-flags after changing them. But never change the flag
535 ; strings while a merge is being performed.
536 ;
537 ; - Autoloading
538 ;
539 ; The following autoloads will make all top-level Emerge files
540 ; autoloading. Make sure that "emerge" is in a directory on load-path.
541 ;
542 ; (autoload 'emerge-files "emerge"
543 ; "Run Emerge on two files."
544 ; t)
545 ; (autoload 'emerge-files-with-ancestor "emerge"
546 ; "Run Emerge on two files, giving another file as the ancestor."
547 ; t)
548 ; (autoload 'emerge-buffers "emerge"
549 ; "Run Emerge on two buffers."
550 ; t)
551 ; (autoload 'emerge-buffers-with-ancestor "emerge"
552 ; "Run Emerge on two buffers, giving another buffer as the ancestor."
553 ; t)
554 ; (autoload 'emerge-files-command "emerge")
555 ; (autoload 'emerge-files-with-ancestor-command "emerge")
556 ; (autoload 'emerge-files-remote "emerge")
557 ; (autoload 'emerge-files-with-ancestor-remote "emerge")
558 ;
559 ; ================================================================
560
561 ;;; Code:
562
563 ;;; Macros
564
565 (defmacro emerge-eval-in-buffer (buffer &rest forms)
566 "Macro to switch to BUFFER, evaluate FORMS, returns to original buffer.
567 Differs from `save-excursion' in that it doesn't save the point and mark."
568 (` (let ((StartBuffer (current-buffer)))
569 (unwind-protect
570 (progn
571 (set-buffer (, buffer))
572 (,@ forms))
573 (set-buffer StartBuffer)))))
574
575 (defmacro emerge-defvar-local (var value doc)
576 "Defines SYMBOL as an advertised variable.
577 Performs a defvar, then executes `make-variable-buffer-local' on
578 the variable. Also sets the 'preserved' property, so that
579 `kill-all-local-variables' (called by major-mode setting commands)
580 won't destroy Emerge control variables."
581 (` (progn
582 (defvar (, var) (, value) (, doc))
583 (make-variable-buffer-local '(, var))
584 (put '(, var) 'preserved t))))
585
586 ;; Add entries to minor-mode-alist so that emerge modes show correctly
587 (setq emerge-minor-modes-list '((emerge-mode " Emerge")
588 (emerge-fast-mode " F")
589 (emerge-edit-mode " E")
590 (emerge-auto-advance " A")
591 (emerge-skip-prefers " S")))
592 (if (not (assq 'emerge-mode minor-mode-alist))
593 (setq minor-mode-alist (append emerge-minor-modes-list
594 minor-mode-alist)))
595
596 ;; We need to define this function so describe-mode can describe Emerge mode.
597 (defun emerge-mode ()
598 "Emerge mode is used by the Emerge file-merging package.
599 It is entered only through one of the functions:
600 `emerge-files'
601 `emerge-files-with-ancestor'
602 `emerge-buffers'
603 `emerge-buffers-with-ancestor'
604 `emerge-files-command'
605 `emerge-files-with-ancestor-command'
606 `emerge-files-remote'
607 `emerge-files-with-ancestor-remote'
608
609 Commands:
610 \\{emerge-basic-keymap}
611 Commands must be prefixed by \\<emerge-fast-keymap>\\[emerge-basic-keymap] in 'edit' mode, but can be invoked directly
612 in 'fast' mode.")
613
614 (defvar emerge-version "4"
615 "The version of Emerge.")
616
617 (defun emerge-version ()
618 "Return string describing the version of Emerge.
619 When called interactively, displays the version."
620 (interactive)
621 (if (interactive-p)
622 (message "Emerge version %s" (emerge-version))
623 emerge-version))
624
625 ;;; Emerge configuration variables
626
627 ;; Commands that produce difference files
628 ;; All that can be configured is the name of the programs to execute
629 ;; (emerge-diff-program and emerge-diff3-program) and the options
630 ;; to be provided (emerge-diff-options). The order in which the file names
631 ;; are given is fixed.
632 ;; The file names are always expanded (see expand-file-name) before being
633 ;; passed to diff, thus they need not be invoked under a shell that
634 ;; understands '~'.
635 ;; The code which processes the diff/diff3 output depends on all the
636 ;; finicky details of their output, including the somewhat strange
637 ;; way they number lines of a file.
638 (defvar emerge-diff-program "diff"
639 "*Name of the program which compares two files.")
640 (defvar emerge-diff3-program "diff3"
641 "*Name of the program which compares an ancestor file (first argument)
642 and two variant files (second and third arguments).")
643 (defvar emerge-diff-options ""
644 "*Options to be passed to emerge-diff/diff3-program.")
645 (defvar emerge-match-diff-line (let ((x "\\([0-9]+\\)\\(\\|,\\([0-9]+\\)\\)"))
646 (concat "^" x "\\([acd]\\)" x "$"))
647 "*Pattern to match lines produced by diff that describe differences (as
648 opposed to lines from the source files).")
649 (defvar emerge-diff-ok-lines
650 "^\\([0-9,]+[acd][0-9,]+$\\|[<>] \\|---\\)"
651 "*Regexp that matches normal output lines from emerge-diff-program .
652 Lines that do not match are assumed to be error output.")
653 (defvar emerge-diff3-ok-lines
654 "^\\([1-3]:\\|====\\| \\)"
655 "*Regexp that matches normal output lines from emerge-diff3-program .
656 Lines that do not match are assumed to be error output.")
657
658 ;; The flags used to mark differences in the buffers.
659
660 ;; These function definitions need to be up here, because they are used
661 ;; during loading.
662 (defun emerge-new-flags ()
663 "Function to be called after `emerge-{before,after}-flag'.
664 This is called after these functions are changed to compute values that
665 depend on the flags."
666 (setq emerge-before-flag-length (length emerge-before-flag))
667 (setq emerge-before-flag-lines
668 (count-matches-string emerge-before-flag "\n"))
669 (setq emerge-before-flag-match (regexp-quote emerge-before-flag))
670 (setq emerge-after-flag-length (length emerge-after-flag))
671 (setq emerge-after-flag-lines
672 (count-matches-string emerge-after-flag "\n"))
673 (setq emerge-after-flag-match (regexp-quote emerge-after-flag)))
674 (defun count-matches-string (string regexp)
675 "Return the number of matches in STRING for REGEXP."
676 (let ((i 0)
677 (count 0))
678 (while (string-match regexp string i)
679 (setq count (1+ count))
680 (setq i (match-end 0)))
681 count))
682
683 (defvar emerge-before-flag "vvvvvvvvvvvvvvvvvvvv\n"
684 "*Flag placed above the highlighted block of code. Must end with newline.
685 Must be set before Emerge is loaded, or emerge-new-flags must be run
686 after setting.")
687 (defvar emerge-after-flag "^^^^^^^^^^^^^^^^^^^^\n"
688 "*Flag placed below the highlighted block of code. Must end with newline.
689 Must be set before Emerge is loaded, or emerge-new-flags must be run
690 after setting.")
691
692 ;; Calculate dependent variables
693 (emerge-new-flags)
694
695 (defvar emerge-min-visible-lines 3
696 "*Number of lines that we want to show above and below the flags when we are
697 displaying a difference.")
698
699 (defvar emerge-temp-file-prefix
700 (let ((env (getenv "TMPDIR"))
701 d)
702 (setq d (if (and env (> (length env) 0))
703 env
704 "/tmp"))
705 (if (= (aref d (1- (length d))) ?/)
706 (setq d (substring d 0 -1)))
707 (concat d "/emerge"))
708 "*Prefix to put on Emerge temporary file names.
709 Do not start with '~/' or '~user-name/'.")
710
711 (defvar emerge-temp-file-mode 384 ; u=rw only
712 "*Mode for Emerge temporary files.")
713
714 (defvar emerge-combine-versions-template
715 "#ifdef NEW\n%b#else /* NEW */\n%a#endif /* NEW */\n"
716 "*Template for emerge-combine-versions to combine the two versions.
717 The template is inserted as a string, with the following interpolations:
718 %a the A version of the difference
719 %b the B version of the difference
720 %% the character '%'
721 Don't forget to end the template with a newline.
722 Note that this variable can be made local to a particular merge buffer by
723 giving a prefix argument to emerge-set-combine-versions-template .")
724
725 ;; Build keymaps
726
727 (defvar emerge-basic-keymap nil
728 "Keymap of Emerge commands.
729 Directly available in 'fast' mode;
730 must be prefixed by \\<emerge-fast-keymap>\\[emerge-basic-keymap] in 'edit' mode.")
731
732 (defvar emerge-fast-keymap nil
733 "Local keymap used in Emerge 'fast' mode.
734 Makes Emerge commands directly available.")
735
736 (defvar emerge-command-prefix "\C-c"
737 "*Command prefix for Emerge commands in 'edit' mode.
738 Must be set before Emerge is loaded.")
739
740 ;; This function sets up the fixed keymaps. It is executed when the first
741 ;; Emerge is done to allow the user maximum time to set up the global keymap.
742 (defun emerge-setup-fixed-keymaps ()
743 ;; Set up the basic keymap
744 (setq emerge-basic-keymap (make-keymap))
745 (suppress-keymap emerge-basic-keymap) ; this sets 0..9 to digit-argument and
746 ; - to negative-argument
747 (define-key emerge-basic-keymap "p" 'emerge-previous-difference)
748 (define-key emerge-basic-keymap "n" 'emerge-next-difference)
749 (define-key emerge-basic-keymap "a" 'emerge-select-A)
750 (define-key emerge-basic-keymap "b" 'emerge-select-B)
751 (define-key emerge-basic-keymap "j" 'emerge-jump-to-difference)
752 (define-key emerge-basic-keymap "q" 'emerge-quit)
753 (define-key emerge-basic-keymap "f" 'emerge-fast-mode)
754 (define-key emerge-basic-keymap "e" 'emerge-edit-mode)
755 (define-key emerge-basic-keymap "s" nil)
756 (define-key emerge-basic-keymap "sa" 'emerge-auto-advance)
757 (define-key emerge-basic-keymap "ss" 'emerge-skip-prefers)
758 (define-key emerge-basic-keymap "l" 'emerge-recenter)
759 (define-key emerge-basic-keymap "d" nil)
760 (define-key emerge-basic-keymap "da" 'emerge-default-A)
761 (define-key emerge-basic-keymap "db" 'emerge-default-B)
762 (define-key emerge-basic-keymap "c" nil)
763 (define-key emerge-basic-keymap "ca" 'emerge-copy-as-kill-A)
764 (define-key emerge-basic-keymap "cb" 'emerge-copy-as-kill-B)
765 (define-key emerge-basic-keymap "i" nil)
766 (define-key emerge-basic-keymap "ia" 'emerge-insert-A)
767 (define-key emerge-basic-keymap "ib" 'emerge-insert-B)
768 (define-key emerge-basic-keymap "m" 'emerge-mark-difference)
769 (define-key emerge-basic-keymap "v" 'emerge-scroll-up)
770 (define-key emerge-basic-keymap "^" 'emerge-scroll-down)
771 (define-key emerge-basic-keymap "<" 'emerge-scroll-left)
772 (define-key emerge-basic-keymap ">" 'emerge-scroll-right)
773 (define-key emerge-basic-keymap "|" 'emerge-scroll-reset)
774 (define-key emerge-basic-keymap "x" nil)
775 (define-key emerge-basic-keymap "x1" 'emerge-one-line-window)
776 (define-key emerge-basic-keymap "xa" 'emerge-find-difference-A)
777 (define-key emerge-basic-keymap "xb" 'emerge-find-difference-B)
778 (define-key emerge-basic-keymap "xc" 'emerge-combine-versions)
779 (define-key emerge-basic-keymap "xC" 'emerge-combine-versions-register)
780 (define-key emerge-basic-keymap "xd" 'emerge-find-difference)
781 (define-key emerge-basic-keymap "xf" 'emerge-file-names)
782 (define-key emerge-basic-keymap "xj" 'emerge-join-differences)
783 (define-key emerge-basic-keymap "xl" 'emerge-line-numbers)
784 (define-key emerge-basic-keymap "xm" 'emerge-set-merge-mode)
785 (define-key emerge-basic-keymap "xs" 'emerge-split-difference)
786 (define-key emerge-basic-keymap "xt" 'emerge-trim-difference)
787 (define-key emerge-basic-keymap "xx" 'emerge-set-combine-versions-template)
788 ;; Allow emerge-basic-keymap to be referenced indirectly
789 (fset 'emerge-basic-keymap emerge-basic-keymap)
790 ;; Set up the fast mode keymap
791 (setq emerge-fast-keymap (copy-keymap emerge-basic-keymap))
792 ;; Allow prefixed commands to work in fast mode
793 (define-key emerge-fast-keymap emerge-command-prefix 'emerge-basic-keymap)
794 ;; Allow emerge-fast-keymap to be referenced indirectly
795 (fset 'emerge-fast-keymap emerge-fast-keymap)
796 ;; Suppress write-file and save-buffer
797 (emerge-shadow-key-definition 'write-file 'emerge-query-write-file
798 (current-global-map) emerge-fast-keymap)
799 (emerge-shadow-key-definition 'save-buffer 'emerge-query-save-buffer
800 (current-global-map) emerge-fast-keymap))
801
802 ;; Variables which control each merge. They are local to the merge buffer.
803
804 ;; Mode variables
805 (emerge-defvar-local emerge-mode nil
806 "Indicator for emerge-mode.")
807 (emerge-defvar-local emerge-fast-mode nil
808 "Indicator for emerge-mode fast submode.")
809 (emerge-defvar-local emerge-edit-mode nil
810 "Indicator for emerge-mode edit submode.")
811 (emerge-defvar-local emerge-A-buffer nil
812 "The buffer in which the A variant is stored.")
813 (emerge-defvar-local emerge-B-buffer nil
814 "The buffer in which the B variant is stored.")
815 (emerge-defvar-local emerge-merge-buffer nil
816 "The buffer in which the merged file is manipulated.")
817 (emerge-defvar-local emerge-ancestor-buffer nil
818 "The buffer in which the ancestor variant is stored,
819 or nil if there is none.")
820
821 (defconst emerge-saved-variables
822 '((buffer-modified-p set-buffer-modified-p)
823 buffer-read-only
824 buffer-auto-save-file-name)
825 "Variables and properties of a buffer which are saved, modified and restored
826 during a merge.")
827 (defconst emerge-merging-values '(nil t nil)
828 "Values to be assigned to emerge-saved-variables during a merge.")
829
830 (emerge-defvar-local emerge-A-buffer-values nil
831 "Remembers emerge-saved-variables for emerge-A-buffer.")
832 (emerge-defvar-local emerge-B-buffer-values nil
833 "Remembers emerge-saved-variables for emerge-B-buffer.")
834
835 (emerge-defvar-local emerge-difference-list nil
836 "Vector of differences between the variants, and markers in the buffers to
837 show where they are. Each difference is represented by a vector of seven
838 elements. The first two are markers to the beginning and end of the difference
839 section in the A buffer, the second two are markers for the B buffer, the third
840 two are markers for the merge buffer, and the last element is the \"state\" of
841 that difference in the merge buffer.
842 A section of a buffer is described by two markers, one to the beginning of
843 the first line of the section, and one to the beginning of the first line
844 after the section. (If the section is empty, both markers point to the same
845 point.) If the section is part of the selected difference, then the markers
846 are moved into the flags, so the user can edit the section without disturbing
847 the markers.
848 The \"states\" are:
849 A the merge buffer currently contains the A variant
850 B the merge buffer currently contains the B variant
851 default-A the merge buffer contains the A variant by default,
852 but this difference hasn't been selected yet, so
853 change-default commands can alter it
854 default-B the merge buffer contains the B variant by default,
855 but this difference hasn't been selected yet, so
856 change-default commands can alter it
857 prefer-A in a three-file merge, the A variant is the prefered
858 choice
859 prefer-B in a three-file merge, the B variant is the prefered
860 choice")
861 (emerge-defvar-local emerge-current-difference -1
862 "The difference that is currently selected.")
863 (emerge-defvar-local emerge-number-of-differences nil
864 "Number of differences found.")
865 (emerge-defvar-local emerge-edit-keymap nil
866 "The local keymap for the merge buffer, with the emerge commands defined in
867 it. Used to save the local keymap during fast mode, when the local keymap is
868 replaced by emerge-fast-keymap.")
869 (emerge-defvar-local emerge-old-keymap nil
870 "The original local keymap for the merge buffer.")
871 (emerge-defvar-local emerge-auto-advance nil
872 "*If non-nil, emerge-select-A and emerge-select-B automatically advance to
873 the next difference.")
874 (emerge-defvar-local emerge-skip-prefers nil
875 "*If non-nil, differences for which there is a preference are automatically
876 skipped.")
877 (emerge-defvar-local emerge-startup-hook nil
878 "*Hooks to run in the merge buffer after the merge has been set up.")
879 (emerge-defvar-local emerge-quit-hook nil
880 "Hooks to run in the merge buffer after the merge has been finished.
881 emerge-prefix-argument will be bound to the prefix argument of the emerge-quit
882 command.
883 This is not a user option, since Emerge uses it for its own processing.")
884 (emerge-defvar-local emerge-output-description nil
885 "Describes output destination merge, for the use of `emerge-file-names'.")
886
887 ;;; Setup functions for two-file mode.
888
889 (defun emerge-files-internal (file-A file-B &optional startup-hooks quit-hooks
890 output-file)
891 (let ((buffer-A (find-file-noselect file-A))
892 (buffer-B (find-file-noselect file-B)))
893 ;; Make sure the entire files are seen, and they reflect what is on disk
894 (emerge-eval-in-buffer buffer-A
895 (widen)
896 (emerge-verify-file-buffer))
897 (emerge-eval-in-buffer buffer-B
898 (widen)
899 (emerge-verify-file-buffer))
900 (emerge-setup buffer-A file-A buffer-B file-B startup-hooks quit-hooks
901 output-file)))
902
903 ;; Start up Emerge on two files
904 (defun emerge-setup (buffer-A file-A buffer-B file-B startup-hooks quit-hooks
905 output-file)
906 (setq file-A (expand-file-name file-A))
907 (setq file-B (expand-file-name file-B))
908 (setq output-file (and output-file (expand-file-name output-file)))
909 (let* ((merge-buffer-name (emerge-unique-buffer-name "*merge" "*"))
910 ;; create the merge buffer from buffer A, so it inherits buffer A's
911 ;; default directory, etc.
912 (merge-buffer (emerge-eval-in-buffer
913 buffer-A
914 (get-buffer-create merge-buffer-name))))
915 (emerge-eval-in-buffer
916 merge-buffer
917 (emerge-copy-modes buffer-A)
918 (setq buffer-read-only nil)
919 (auto-save-mode 1)
920 (setq emerge-mode t)
921 (setq emerge-A-buffer buffer-A)
922 (setq emerge-B-buffer buffer-B)
923 (setq emerge-ancestor-buffer nil)
924 (setq emerge-merge-buffer merge-buffer)
925 (setq emerge-output-description
926 (if output-file
927 (concat "Output to file: " output-file)
928 (concat "Output to buffer: " (buffer-name merge-buffer))))
929 (insert-buffer emerge-A-buffer)
930 (emerge-set-keys)
931 (setq emerge-difference-list (emerge-make-diff-list file-A file-B))
932 (setq emerge-number-of-differences (length emerge-difference-list))
933 (setq emerge-current-difference -1)
934 (setq emerge-quit-hooks quit-hooks)
935 (emerge-remember-buffer-characteristics))
936 (emerge-setup-windows buffer-A buffer-B merge-buffer t)
937 (emerge-eval-in-buffer merge-buffer
938 (run-hooks 'startup-hooks 'emerge-startup-hook)
939 (setq buffer-read-only t))))
940
941 ;; Generate the Emerge difference list between two files
942 (defun emerge-make-diff-list (file-A file-B)
943 (setq emerge-diff-buffer (get-buffer-create "*emerge-diff*"))
944 (emerge-eval-in-buffer
945 emerge-diff-buffer
946 (erase-buffer)
947 (shell-command
948 (format "%s %s %s %s"
949 emerge-diff-program emerge-diff-options file-A file-B)
950 t))
951 (emerge-prepare-error-list emerge-diff-ok-lines)
952 (emerge-convert-diffs-to-markers
953 emerge-A-buffer emerge-B-buffer emerge-merge-buffer
954 (emerge-extract-diffs emerge-diff-buffer)))
955
956 (defun emerge-extract-diffs (diff-buffer)
957 (let (list)
958 (emerge-eval-in-buffer
959 diff-buffer
960 (goto-char (point-min))
961 (while (re-search-forward emerge-match-diff-line nil t)
962 (let* ((a-begin (string-to-int (buffer-substring (match-beginning 1)
963 (match-end 1))))
964 (a-end (let ((b (match-beginning 3))
965 (e (match-end 3)))
966 (if b
967 (string-to-int (buffer-substring b e))
968 a-begin)))
969 (diff-type (buffer-substring (match-beginning 4) (match-end 4)))
970 (b-begin (string-to-int (buffer-substring (match-beginning 5)
971 (match-end 5))))
972 (b-end (let ((b (match-beginning 7))
973 (e (match-end 7)))
974 (if b
975 (string-to-int (buffer-substring b e))
976 b-begin))))
977 ;; fix the beginning and end numbers, because diff is somewhat
978 ;; strange about how it numbers lines
979 (if (string-equal diff-type "a")
980 (progn
981 (setq b-end (1+ b-end))
982 (setq a-begin (1+ a-begin))
983 (setq a-end a-begin))
984 (if (string-equal diff-type "d")
985 (progn
986 (setq a-end (1+ a-end))
987 (setq b-begin (1+ b-begin))
988 (setq b-end b-begin))
989 ;; (string-equal diff-type "c")
990 (progn
991 (setq a-end (1+ a-end))
992 (setq b-end (1+ b-end)))))
993 (setq list (cons (vector a-begin a-end
994 b-begin b-end
995 'default-A)
996 list)))))
997 (nreverse list)))
998
999 ;; Set up buffer of diff/diff3 error messages.
1000 (defun emerge-prepare-error-list (ok-regexp)
1001 (setq emerge-diff-error-buffer (get-buffer-create "*emerge-diff-errors*"))
1002 (emerge-eval-in-buffer
1003 emerge-diff-error-buffer
1004 (erase-buffer)
1005 (insert-buffer emerge-diff-buffer)
1006 (delete-matching-lines ok-regexp)))
1007
1008 ;;; Top-level and setup functions for three-file mode.
1009
1010 (defun emerge-files-with-ancestor-internal (file-A file-B file-ancestor
1011 &optional startup-hooks quit-hooks
1012 output-file)
1013 (let ((buffer-A (find-file-noselect file-A))
1014 (buffer-B (find-file-noselect file-B))
1015 (buffer-ancestor (find-file-noselect file-ancestor)))
1016 ;; Make sure the entire files are seen, and they reflect what is on disk
1017 (emerge-eval-in-buffer buffer-A
1018 (widen)
1019 (emerge-verify-file-buffer))
1020 (emerge-eval-in-buffer buffer-B
1021 (widen)
1022 (emerge-verify-file-buffer))
1023 (emerge-eval-in-buffer buffer-ancestor
1024 (widen)
1025 (emerge-verify-file-buffer))
1026 (emerge-setup-with-ancestor buffer-A file-A buffer-B file-B
1027 buffer-ancestor file-ancestor
1028 startup-hooks quit-hooks output-file)))
1029
1030 ;; Start up Emerge on two files with an ancestor
1031 (defun emerge-setup-with-ancestor (buffer-A file-A buffer-B file-B
1032 buffer-ancestor file-ancestor
1033 &optional startup-hooks quit-hooks
1034 output-file)
1035 (setq file-A (expand-file-name file-A))
1036 (setq file-B (expand-file-name file-B))
1037 (setq file-ancestor (expand-file-name file-ancestor))
1038 (setq output-file (and output-file (expand-file-name output-file)))
1039 (let* ((merge-buffer-name (emerge-unique-buffer-name "*merge" "*"))
1040 ;; create the merge buffer from buffer A, so it inherits buffer A's
1041 ;; default directory, etc.
1042 (merge-buffer (emerge-eval-in-buffer
1043 buffer-A
1044 (get-buffer-create merge-buffer-name))))
1045 (emerge-eval-in-buffer
1046 merge-buffer
1047 (emerge-copy-modes buffer-A)
1048 (setq buffer-read-only nil)
1049 (auto-save-mode 1)
1050 (setq emerge-mode t)
1051 (setq emerge-A-buffer buffer-A)
1052 (setq emerge-B-buffer buffer-B)
1053 (setq emerge-ancestor-buffer buffer-ancestor)
1054 (setq emerge-merge-buffer merge-buffer)
1055 (setq emerge-output-description
1056 (if output-file
1057 (concat "Output to file: " output-file)
1058 (concat "Output to buffer: " (buffer-name merge-buffer))))
1059 (insert-buffer emerge-A-buffer)
1060 (emerge-set-keys)
1061 (setq emerge-difference-list
1062 (emerge-make-diff3-list file-A file-B file-ancestor))
1063 (setq emerge-number-of-differences (length emerge-difference-list))
1064 (setq emerge-current-difference -1)
1065 (setq emerge-quit-hook quit-hooks)
1066 (emerge-remember-buffer-characteristics)
1067 (emerge-select-prefer-Bs))
1068 (emerge-setup-windows buffer-A buffer-B merge-buffer t)
1069 (emerge-eval-in-buffer merge-buffer
1070 (run-hooks 'startup-hooks 'emerge-startup-hook)
1071 (setq buffer-read-only t))))
1072
1073 ;; Generate the Emerge difference list between two files with an ancestor
1074 (defun emerge-make-diff3-list (file-A file-B file-ancestor)
1075 (setq emerge-diff-buffer (get-buffer-create "*emerge-diff*"))
1076 (emerge-eval-in-buffer
1077 emerge-diff-buffer
1078 (erase-buffer)
1079 (shell-command
1080 (format "%s %s %s %s %s"
1081 emerge-diff3-program emerge-diff-options
1082 file-ancestor file-A file-B)
1083 t))
1084 (emerge-prepare-error-list emerge-diff3-ok-lines)
1085 (emerge-convert-diffs-to-markers
1086 emerge-A-buffer emerge-B-buffer emerge-merge-buffer
1087 (emerge-extract-diffs3 emerge-diff-buffer)))
1088
1089 (defun emerge-extract-diffs3 (diff-buffer)
1090 (let (list)
1091 (emerge-eval-in-buffer
1092 diff-buffer
1093 (while (re-search-forward "^====\\(.?\\)$" nil t)
1094 ;; leave point after matched line
1095 (beginning-of-line 2)
1096 (let ((agreement (buffer-substring (match-beginning 1) (match-end 1))))
1097 ;; if the A and B files are the same, ignore the difference
1098 (if (not (string-equal agreement "1"))
1099 (setq list
1100 (cons
1101 (let ((group-2 (emerge-get-diff3-group "2"))
1102 (group-3 (emerge-get-diff3-group "3")))
1103 (vector (car group-2) (car (cdr group-2))
1104 (car group-3) (car (cdr group-3))
1105 (cond ((string-equal agreement "2") 'prefer-A)
1106 ((string-equal agreement "3") 'prefer-B)
1107 (t 'default-A))))
1108 list))))))
1109 (nreverse list)))
1110
1111 (defun emerge-get-diff3-group (file)
1112 ;; This save-excursion allows emerge-get-diff3-group to be called for the
1113 ;; various groups of lines (1, 2, 3) in any order, and for the lines to
1114 ;; appear in any order. The reason this is necessary is that Gnu diff3
1115 ;; can produce the groups in the order 1, 2, 3 or 1, 3, 2.
1116 (save-excursion
1117 (re-search-forward
1118 (concat "^" file ":\\([0-9]+\\)\\(,\\([0-9]+\\)\\)?\\([ac]\\)$"))
1119 (beginning-of-line 2)
1120 ;; treatment depends on whether it is an "a" group or a "c" group
1121 (if (string-equal (buffer-substring (match-beginning 4) (match-end 4)) "c")
1122 ;; it is a "c" group
1123 (if (match-beginning 2)
1124 ;; it has two numbers
1125 (list (string-to-int
1126 (buffer-substring (match-beginning 1) (match-end 1)))
1127 (1+ (string-to-int
1128 (buffer-substring (match-beginning 3) (match-end 3)))))
1129 ;; it has one number
1130 (let ((x (string-to-int
1131 (buffer-substring (match-beginning 1) (match-end 1)))))
1132 (list x (1+ x))))
1133 ;; it is an "a" group
1134 (let ((x (1+ (string-to-int
1135 (buffer-substring (match-beginning 1) (match-end 1))))))
1136 (list x x)))))
1137
1138 ;;; Functions to start Emerge on files
1139
1140 ;;;###autoload
1141 (defun emerge-files (arg file-A file-B file-out &optional startup-hooks
1142 quit-hooks)
1143 "Run Emerge on two files."
1144 (interactive
1145 (let (f)
1146 (list current-prefix-arg
1147 (setq f (read-file-name "File A to merge: " nil nil 'confirm))
1148 (read-file-name "File B to merge: " nil nil 'confirm)
1149 (and current-prefix-arg
1150 (read-file-name
1151 (format "Output file: (default %s) " f)
1152 nil f nil)))))
1153 (emerge-files-internal
1154 file-A file-B startup-hooks
1155 (if arg
1156 (cons (` (lambda () (emerge-files-exit (, file-out))))
1157 quit-hooks)
1158 quit-hooks)
1159 file-out))
1160
1161 ;;;###autoload
1162 (defun emerge-files-with-ancestor (arg file-A file-B file-ancestor file-out
1163 &optional startup-hooks quit-hooks)
1164 "Run Emerge on two files, giving another file as the ancestor."
1165 (interactive
1166 (let (f)
1167 (list current-prefix-arg
1168 (setq f (read-file-name "File A to merge: " nil nil 'confirm))
1169 (read-file-name "File B to merge: " nil nil 'confirm)
1170 (read-file-name "Ancestor file: " nil nil 'confirm)
1171 (and current-prefix-arg
1172 (read-file-name
1173 (format "Output file: (default %s) " f)
1174 nil f nil)))))
1175 (emerge-files-with-ancestor-internal
1176 file-A file-B file-ancestor startup-hooks
1177 (if arg
1178 (cons (` (lambda () (emerge-files-exit (, file-out))))
1179 quit-hooks)
1180 quit-hooks)
1181 file-out))
1182
1183 ;; Write the merge buffer out in place of the file the A buffer is visiting.
1184 (defun emerge-files-exit (file-out)
1185 ;; if merge was successful was given, save to disk
1186 (if (not emerge-prefix-argument)
1187 (emerge-write-and-delete file-out)))
1188
1189 ;;; Functions to start Emerge on buffers
1190
1191 ;;;###autoload
1192 (defun emerge-buffers (buffer-A buffer-B &optional startup-hooks quit-hooks)
1193 "Run Emerge on two buffers."
1194 (interactive "bBuffer A to merge: \nbBuffer B to merge: ")
1195 (let ((emerge-file-A (emerge-make-temp-file "A"))
1196 (emerge-file-B (emerge-make-temp-file "B")))
1197 (emerge-eval-in-buffer
1198 buffer-A
1199 (write-region (point-min) (point-max) emerge-file-A nil 'no-message))
1200 (emerge-eval-in-buffer
1201 buffer-B
1202 (write-region (point-min) (point-max) emerge-file-B nil 'no-message))
1203 (emerge-setup (get-buffer buffer-A) emerge-file-A
1204 (get-buffer buffer-B) emerge-file-B
1205 (cons (function (lambda ()
1206 (delete-file emerge-file-A)
1207 (delete-file emerge-file-B)))
1208 startup-hooks)
1209 quit-hooks
1210 nil)))
1211
1212 ;;;###autoload
1213 (defun emerge-buffers-with-ancestor (buffer-A buffer-B buffer-ancestor
1214 &optional startup-hooks
1215 quit-hooks)
1216 "Run Emerge on two buffers, giving another buffer as the ancestor."
1217 (interactive
1218 "bBuffer A to merge: \nbBuffer B to merge: \nbAncestor buffer: ")
1219 (let ((emerge-file-A (emerge-make-temp-file "A"))
1220 (emerge-file-B (emerge-make-temp-file "B"))
1221 (emerge-file-ancestor (emerge-make-temp-file "anc")))
1222 (emerge-eval-in-buffer
1223 buffer-A
1224 (write-region (point-min) (point-max) emerge-file-A nil 'no-message))
1225 (emerge-eval-in-buffer
1226 buffer-B
1227 (write-region (point-min) (point-max) emerge-file-B nil 'no-message))
1228 (emerge-eval-in-buffer
1229 buffer-ancestor
1230 (write-region (point-min) (point-max) emerge-file-ancestor nil
1231 'no-message))
1232 (emerge-setup-with-ancestor (get-buffer buffer-A) emerge-file-A
1233 (get-buffer buffer-B) emerge-file-B
1234 (get-buffer buffer-ancestor)
1235 emerge-file-ancestor
1236 (cons (function (lambda ()
1237 (delete-file emerge-file-A)
1238 (delete-file emerge-file-B)
1239 (delete-file
1240 emerge-file-ancestor)))
1241 startup-hooks)
1242 quit-hooks
1243 nil)))
1244
1245 ;;; Functions to start Emerge from the command line
1246
1247 ;;;###autoload
1248 (defun emerge-files-command ()
1249 (let ((file-a (nth 0 command-line-args-left))
1250 (file-b (nth 1 command-line-args-left))
1251 (file-out (nth 2 command-line-args-left)))
1252 (setq command-line-args-left (nthcdr 3 command-line-args-left))
1253 (emerge-files-internal
1254 file-a file-b nil
1255 (list (` (lambda () (emerge-command-exit (, file-out))))))))
1256
1257 ;;;###autoload
1258 (defun emerge-files-with-ancestor-command ()
1259 (let (file-a file-b file-anc file-out)
1260 ;; check for a -a flag, for filemerge compatibility
1261 (if (string= (car command-line-args-left) "-a")
1262 ;; arguments are "-a ancestor file-a file-b file-out"
1263 (progn
1264 (setq file-a (nth 2 command-line-args-left))
1265 (setq file-b (nth 3 command-line-args-left))
1266 (setq file-anc (nth 1 command-line-args-left))
1267 (setq file-out (nth 4 command-line-args-left))
1268 (setq command-line-args-left (nthcdr 5 command-line-args-left)))
1269 ;; arguments are "file-a file-b ancestor file-out"
1270 (setq file-a (nth 0 command-line-args-left))
1271 (setq file-b (nth 1 command-line-args-left))
1272 (setq file-anc (nth 2 command-line-args-left))
1273 (setq file-out (nth 3 command-line-args-left))
1274 (setq command-line-args-left (nthcdr 4 command-line-args-left)))
1275 (emerge-files-with-ancestor-internal
1276 file-a file-b file-anc nil
1277 (list (` (lambda () (emerge-command-exit (, file-out))))))))
1278
1279 (defun emerge-command-exit (file-out)
1280 (emerge-write-and-delete file-out)
1281 (kill-emacs (if emerge-prefix-argument 1 0)))
1282
1283 ;;; Functions to start Emerge via remote request
1284
1285 ;;;###autoload
1286 (defun emerge-files-remote (file-a file-b file-out)
1287 (setq emerge-file-out file-out)
1288 (emerge-files-internal
1289 file-a file-b nil
1290 (list (` (lambda () (emerge-remote-exit (, file-out) '(, exit-func)))))
1291 file-out)
1292 (throw 'client-wait nil))
1293
1294 ;;;###autoload
1295 (defun emerge-files-with-ancestor-remote (file-a file-b file-anc file-out)
1296 (setq emerge-file-out file-out)
1297 (emerge-files-with-ancestor-internal
1298 file-a file-b file-anc nil
1299 (list (` (lambda () (emerge-remote-exit (, file-out) '(, exit-func)))))
1300 file-out)
1301 (throw 'client-wait nil))
1302
1303 (defun emerge-remote-exit (file-out exit-func)
1304 (emerge-write-and-delete file-out)
1305 (kill-buffer emerge-merge-buffer)
1306 (funcall exit-func (if emerge-prefix-argument 1 0)))
1307
1308 ;;; Common setup routines
1309
1310 ;; Set up the window configuration. If POS is given, set the points to
1311 ;; the beginnings of the buffers.
1312 (defun emerge-setup-windows (buffer-A buffer-B merge-buffer &optional pos)
1313 ;; Make sure we are not in the minibuffer window when we try to delete
1314 ;; all other windows.
1315 (if (eq (selected-window) (minibuffer-window))
1316 (other-window 1))
1317 (delete-other-windows)
1318 (switch-to-buffer merge-buffer)
1319 (emerge-refresh-mode-line)
1320 (split-window-vertically)
1321 (split-window-horizontally)
1322 (switch-to-buffer buffer-A)
1323 (if pos
1324 (goto-char (point-min)))
1325 (other-window 1)
1326 (switch-to-buffer buffer-B)
1327 (if pos
1328 (goto-char (point-min)))
1329 (other-window 1)
1330 (if pos
1331 (goto-char (point-min)))
1332 ;; If diff/diff3 reports errors, display them rather than the merge buffer.
1333 (if (/= 0 (emerge-eval-in-buffer emerge-diff-error-buffer (buffer-size)))
1334 (progn
1335 (ding)
1336 (message "Errors found in diff/diff3 output. Merge buffer is %s."
1337 (buffer-name emerge-merge-buffer))
1338 (switch-to-buffer emerge-diff-error-buffer))))
1339
1340 ;; Set up the keymap in the merge buffer
1341 (defun emerge-set-keys ()
1342 ;; Set up fixed keymaps if necessary
1343 (if (not emerge-basic-keymap)
1344 (emerge-setup-fixed-keymaps))
1345 ;; Save the old local map
1346 (setq emerge-old-keymap (current-local-map))
1347 ;; Construct the edit keymap
1348 (setq emerge-edit-keymap (if emerge-old-keymap
1349 (copy-keymap emerge-old-keymap)
1350 (make-sparse-keymap)))
1351 ;; Install the Emerge commands
1352 (emerge-force-define-key emerge-edit-keymap emerge-command-prefix
1353 'emerge-basic-keymap)
1354 ;; Suppress write-file and save-buffer
1355 (emerge-recursively-substitute-key-definition 'write-file
1356 'emerge-query-write-file
1357 emerge-edit-keymap)
1358 (emerge-recursively-substitute-key-definition 'save-buffer
1359 'emerge-query-save-buffer
1360 emerge-edit-keymap)
1361 (emerge-shadow-key-definition 'write-file 'emerge-query-write-file
1362 (current-global-map) emerge-edit-keymap)
1363 (emerge-shadow-key-definition 'save-buffer 'emerge-query-save-buffer
1364 (current-global-map) emerge-edit-keymap)
1365 (use-local-map emerge-fast-keymap)
1366 (setq emerge-edit-mode nil)
1367 (setq emerge-fast-mode t))
1368
1369 (defun emerge-remember-buffer-characteristics ()
1370 "Remembers certain properties of the buffers being merged.
1371 Must be called in the merge buffer. Remembers read-only, modified,
1372 auto-save, and saves them in buffer local variables. Sets the buffers
1373 read-only and turns off `auto-save-mode'.
1374 These characteristics are restored by emerge-restore-buffer-characteristics."
1375 ;; force auto-save, because we will turn off auto-saving in buffers for the
1376 ;; duration
1377 (do-auto-save)
1378 ;; remember and alter buffer characteristics
1379 (setq emerge-A-buffer-values
1380 (emerge-eval-in-buffer
1381 emerge-A-buffer
1382 (prog1
1383 (emerge-save-variables emerge-saved-variables)
1384 (emerge-restore-variables emerge-saved-variables
1385 emerge-merging-values))))
1386 (setq emerge-B-buffer-values
1387 (emerge-eval-in-buffer
1388 emerge-B-buffer
1389 (prog1
1390 (emerge-save-variables emerge-saved-variables)
1391 (emerge-restore-variables emerge-saved-variables
1392 emerge-merging-values)))))
1393
1394 (defun emerge-restore-buffer-characteristics ()
1395 "Restores the characteristics remembered by
1396 emerge-remember-buffer-characteristics."
1397 (let ((A-values emerge-A-buffer-values)
1398 (B-values emerge-B-buffer-values))
1399 (emerge-eval-in-buffer emerge-A-buffer
1400 (emerge-restore-variables emerge-saved-variables
1401 A-values))
1402 (emerge-eval-in-buffer emerge-B-buffer
1403 (emerge-restore-variables emerge-saved-variables
1404 B-values))))
1405
1406 (defun emerge-convert-diffs-to-markers (A-buffer
1407 B-buffer
1408 merge-buffer
1409 lineno-list)
1410 (let* (marker-list
1411 (A-point-min (emerge-eval-in-buffer A-buffer (point-min)))
1412 (offset (1- A-point-min))
1413 (A-hidden-lines (emerge-eval-in-buffer
1414 A-buffer
1415 (save-restriction
1416 (widen)
1417 (count-lines 1 A-point-min))))
1418 (B-point-min (emerge-eval-in-buffer B-buffer (point-min)))
1419 (B-hidden-lines (emerge-eval-in-buffer
1420 B-buffer
1421 (save-restriction
1422 (widen)
1423 (count-lines 1 B-point-min)))))
1424 (while lineno-list
1425 (let* ((list-element (car lineno-list))
1426 a-begin-marker
1427 a-end-marker
1428 b-begin-marker
1429 b-end-marker
1430 (a-begin (aref list-element 0))
1431 (a-end (aref list-element 1))
1432 (b-begin (aref list-element 2))
1433 (b-end (aref list-element 3))
1434 (state (aref list-element 4)))
1435 ;; place markers at the appropriate places in the buffers
1436 (emerge-eval-in-buffer
1437 A-buffer
1438 (goto-line (+ a-begin A-hidden-lines))
1439 (setq a-begin-marker (point-marker))
1440 (goto-line (+ a-end A-hidden-lines))
1441 (setq a-end-marker (point-marker)))
1442 (emerge-eval-in-buffer
1443 B-buffer
1444 (goto-line (+ b-begin B-hidden-lines))
1445 (setq b-begin-marker (point-marker))
1446 (goto-line (+ b-end B-hidden-lines))
1447 (setq b-end-marker (point-marker)))
1448 (setq merge-begin-marker (set-marker
1449 (make-marker)
1450 (- (marker-position a-begin-marker)
1451 offset)
1452 merge-buffer))
1453 (setq merge-end-marker (set-marker
1454 (make-marker)
1455 (- (marker-position a-end-marker)
1456 offset)
1457 merge-buffer))
1458 ;; record all the markers for this difference
1459 (setq marker-list (cons (vector a-begin-marker a-end-marker
1460 b-begin-marker b-end-marker
1461 merge-begin-marker merge-end-marker
1462 state)
1463 marker-list)))
1464 (setq lineno-list (cdr lineno-list)))
1465 ;; convert the list of difference information into a vector for
1466 ;; fast access
1467 (setq emerge-difference-list (apply 'vector (nreverse marker-list)))))
1468
1469 ;; If we have an ancestor, select all B variants that we prefer
1470 (defun emerge-select-prefer-Bs ()
1471 (let ((n 0))
1472 (while (< n emerge-number-of-differences)
1473 (if (eq (aref (aref emerge-difference-list n) 6) 'prefer-B)
1474 (progn
1475 (emerge-unselect-and-select-difference n t)
1476 (emerge-select-B)
1477 (aset (aref emerge-difference-list n) 6 'prefer-B)))
1478 (setq n (1+ n))))
1479 (emerge-unselect-and-select-difference -1))
1480
1481 ;;; Common exit routines
1482
1483 (defun emerge-write-and-delete (file-out)
1484 ;; clear screen format
1485 (delete-other-windows)
1486 ;; delete A, B, and ancestor buffers, if they haven't been changed
1487 (if (not (buffer-modified-p emerge-A-buffer))
1488 (kill-buffer emerge-A-buffer))
1489 (if (not (buffer-modified-p emerge-B-buffer))
1490 (kill-buffer emerge-B-buffer))
1491 (if (and emerge-ancestor-buffer
1492 (not (buffer-modified-p emerge-ancestor-buffer)))
1493 (kill-buffer emerge-ancestor-buffer))
1494 ;; Write merge buffer to file
1495 (write-file file-out))
1496
1497 ;;; Commands
1498
1499 (defun emerge-recenter (&optional arg)
1500 "Bring the highlighted region of all three merge buffers into view.
1501 This brings the buffers into view if they are in windows.
1502 If an ARGUMENT is given, the default three-window display is reestablished."
1503 (interactive "P")
1504 ;; If there is an argument, rebuild the window structure
1505 (if arg
1506 (emerge-setup-windows emerge-A-buffer emerge-B-buffer
1507 emerge-merge-buffer))
1508 ;; Redisplay whatever buffers are showing, if there is a selected difference
1509 (if (and (>= emerge-current-difference 0)
1510 (< emerge-current-difference emerge-number-of-differences))
1511 (let* ((merge-buffer emerge-merge-buffer)
1512 (buffer-A emerge-A-buffer)
1513 (buffer-B emerge-B-buffer)
1514 (window-A (get-buffer-window buffer-A))
1515 (window-B (get-buffer-window buffer-B))
1516 (merge-window (get-buffer-window merge-buffer))
1517 (diff-vector
1518 (aref emerge-difference-list emerge-current-difference)))
1519 (if window-A (progn
1520 (select-window window-A)
1521 (emerge-position-region
1522 (- (aref diff-vector 0)
1523 (1- emerge-before-flag-length))
1524 (+ (aref diff-vector 1)
1525 (1- emerge-after-flag-length))
1526 (1+ (aref diff-vector 0)))))
1527 (if window-B (progn
1528 (select-window window-B)
1529 (emerge-position-region
1530 (- (aref diff-vector 2)
1531 (1- emerge-before-flag-length))
1532 (+ (aref diff-vector 3)
1533 (1- emerge-after-flag-length))
1534 (1+ (aref diff-vector 2)))))
1535 (if merge-window (progn
1536 (select-window merge-window)
1537 (emerge-position-region
1538 (- (aref diff-vector 4)
1539 (1- emerge-before-flag-length))
1540 (+ (aref diff-vector 5)
1541 (1- emerge-after-flag-length))
1542 (1+ (aref diff-vector 4))))))))
1543
1544 ;;; Window scrolling operations
1545 ;; These operations are designed to scroll all three windows the same amount,
1546 ;; so as to keep the text in them aligned.
1547
1548 ;; Perform some operation on all three windows (if they are showing).
1549 ;; Catches all errors on the operation in the A and B windows, but not
1550 ;; in the merge window. Usually, errors come from scrolling off the
1551 ;; beginning or end of the buffer, and this gives a nice error message:
1552 ;; End of buffer is reported in the merge buffer, but if the scroll was
1553 ;; possible in the A or B windows, it is performed there before the error
1554 ;; is reported.
1555 (defun emerge-operate-on-windows (operation arg)
1556 (let* ((merge-buffer emerge-merge-buffer)
1557 (buffer-A emerge-A-buffer)
1558 (buffer-B emerge-B-buffer)
1559 (window-A (get-buffer-window buffer-A))
1560 (window-B (get-buffer-window buffer-B))
1561 (merge-window (get-buffer-window merge-buffer)))
1562 (if window-A (progn
1563 (select-window window-A)
1564 (condition-case nil
1565 (funcall operation arg)
1566 (error))))
1567 (if window-B (progn
1568 (select-window window-B)
1569 (condition-case nil
1570 (funcall operation arg)
1571 (error))))
1572 (if merge-window (progn
1573 (select-window merge-window)
1574 (funcall operation arg)))))
1575
1576 (defun emerge-scroll-up (&optional arg)
1577 "Scroll up all three merge buffers, if they are in windows.
1578 If an ARGUMENT is given, that is how many lines are scrolled, else nearly
1579 the size of the merge window. `C-u -' alone as argument scrolls half the
1580 size of the merge window."
1581 (interactive "P")
1582 (emerge-operate-on-windows
1583 'scroll-up
1584 ;; calculate argument to scroll-up
1585 ;; if there is an explicit argument
1586 (if (and arg (not (equal arg '-)))
1587 ;; use it
1588 (prefix-numeric-value arg)
1589 ;; if not, see if we can determine a default amount (the window height)
1590 (let ((merge-window (get-buffer-window emerge-merge-buffer)))
1591 (if (null merge-window)
1592 ;; no window, use nil
1593 nil
1594 (let ((default-amount
1595 (- (window-height merge-window) 1 next-screen-context-lines)))
1596 ;; the window was found
1597 (if arg
1598 ;; C-u as argument means half of default amount
1599 (/ default-amount 2)
1600 ;; no argument means default amount
1601 default-amount)))))))
1602
1603 (defun emerge-scroll-down (&optional arg)
1604 "Scroll down all three merge buffers, if they are in windows.
1605 If an ARGUMENT is given, that is how many lines are scrolled, else nearly
1606 the size of the merge window. `C-u -' alone as argument scrolls half the
1607 size of the merge window."
1608 (interactive "P")
1609 (emerge-operate-on-windows
1610 'scroll-down
1611 ;; calculate argument to scroll-down
1612 ;; if there is an explicit argument
1613 (if (and arg (not (equal arg '-)))
1614 ;; use it
1615 (prefix-numeric-value arg)
1616 ;; if not, see if we can determine a default amount (the window height)
1617 (let ((merge-window (get-buffer-window emerge-merge-buffer)))
1618 (if (null merge-window)
1619 ;; no window, use nil
1620 nil
1621 (let ((default-amount
1622 (- (window-height merge-window) 1 next-screen-context-lines)))
1623 ;; the window was found
1624 (if arg
1625 ;; C-u as argument means half of default amount
1626 (/ default-amount 2)
1627 ;; no argument means default amount
1628 default-amount)))))))
1629
1630 (defun emerge-scroll-left (&optional arg)
1631 "Scroll left all three merge buffers, if they are in windows.
1632 If an ARGUMENT is given, that is how many columns are scrolled, else nearly
1633 the width of the A and B windows. C-u - alone as argument scrolls half the
1634 width of the A and B windows."
1635 (interactive "P")
1636 (emerge-operate-on-windows
1637 'scroll-left
1638 ;; calculate argument to scroll-left
1639 ;; if there is an explicit argument
1640 (if (and arg (not (equal arg '-)))
1641 ;; use it
1642 (prefix-numeric-value arg)
1643 ;; if not, see if we can determine a default amount
1644 ;; (half the window width)
1645 (let ((merge-window (get-buffer-window emerge-merge-buffer)))
1646 (if (null merge-window)
1647 ;; no window, use nil
1648 nil
1649 (let ((default-amount
1650 (- (/ (window-width merge-window) 2) 3)))
1651 ;; the window was found
1652 (if arg
1653 ;; C-u as argument means half of default amount
1654 (/ default-amount 2)
1655 ;; no argument means default amount
1656 default-amount)))))))
1657
1658 (defun emerge-scroll-right (&optional arg)
1659 "Scroll right all three merge buffers, if they are in windows.
1660 If an ARGUMENT is given, that is how many columns are scrolled, else nearly
1661 the width of the A and B windows. C-u - alone as argument scrolls half the
1662 width of the A and B windows."
1663 (interactive "P")
1664 (emerge-operate-on-windows
1665 'scroll-right
1666 ;; calculate argument to scroll-right
1667 ;; if there is an explicit argument
1668 (if (and arg (not (equal arg '-)))
1669 ;; use it
1670 (prefix-numeric-value arg)
1671 ;; if not, see if we can determine a default amount
1672 ;; (half the window width)
1673 (let ((merge-window (get-buffer-window emerge-merge-buffer)))
1674 (if (null merge-window)
1675 ;; no window, use nil
1676 nil
1677 (let ((default-amount
1678 (- (/ (window-width merge-window) 2) 3)))
1679 ;; the window was found
1680 (if arg
1681 ;; C-u as argument means half of default amount
1682 (/ default-amount 2)
1683 ;; no argument means default amount
1684 default-amount)))))))
1685
1686 (defun emerge-scroll-reset ()
1687 "Reset horizontal scrolling.
1688 This resets the horizontal scrolling of all three merge buffers
1689 to the left margin, if they are in windows."
1690 (interactive)
1691 (emerge-operate-on-windows
1692 (function (lambda (x) (set-window-hscroll (selected-window) 0)))
1693 nil))
1694
1695 ;; Attempt to show the region nicely.
1696 ;; If there are min-lines lines above and below the region, then don't do
1697 ;; anything.
1698 ;; If not, recenter the region to make it so.
1699 ;; If that isn't possible, remove context lines balancedly from top and botton
1700 ;; so the entire region shows.
1701 ;; If that isn't possible, show the top of the region.
1702 ;; BEG must be at the beginning of a line.
1703 (defun emerge-position-region (beg end pos)
1704 ;; First test whether the entire region is visible with
1705 ;; emerge-min-visible-lines above and below it
1706 (if (not (and (<= (progn
1707 (move-to-window-line emerge-min-visible-lines)
1708 (point))
1709 beg)
1710 (<= end (progn
1711 (move-to-window-line
1712 (- (1+ emerge-min-visible-lines)))
1713 (point)))))
1714 ;; We failed that test, see if it fits at all
1715 ;; Meanwhile positioning it correctly in case it doesn't fit
1716 (progn
1717 (set-window-start (selected-window) beg)
1718 (setq fits (pos-visible-in-window-p end))
1719 (if fits
1720 ;; Determine the number of lines that the region occupies
1721 (let ((lines 0))
1722 (while (> end (progn
1723 (move-to-window-line lines)
1724 (point)))
1725 (setq lines (1+ lines)))
1726 ;; And position the beginning on the right line
1727 (goto-char beg)
1728 (recenter (/ (1+ (- (1- (window-height (selected-window)))
1729 lines))
1730 2))))))
1731 (goto-char pos))
1732
1733 (defun emerge-next-difference ()
1734 "Advance to the next difference."
1735 (interactive)
1736 (if (< emerge-current-difference emerge-number-of-differences)
1737 (let ((n (1+ emerge-current-difference)))
1738 (while (and emerge-skip-prefers
1739 (< n emerge-number-of-differences)
1740 (memq (aref (aref emerge-difference-list n) 6)
1741 '(prefer-A prefer-B)))
1742 (setq n (1+ n)))
1743 (let ((buffer-read-only nil))
1744 (emerge-unselect-and-select-difference n)))
1745 (error "At end")))
1746
1747 (defun emerge-previous-difference ()
1748 "Go to the previous difference."
1749 (interactive)
1750 (if (> emerge-current-difference -1)
1751 (let ((n (1- emerge-current-difference)))
1752 (while (and emerge-skip-prefers
1753 (> n -1)
1754 (memq (aref (aref emerge-difference-list n) 6)
1755 '(prefer-A prefer-B)))
1756 (setq n (1- n)))
1757 (let ((buffer-read-only nil))
1758 (emerge-unselect-and-select-difference n)))
1759 (error "At beginning")))
1760
1761 (defun emerge-jump-to-difference (difference-number)
1762 "Go to the N-th difference."
1763 (interactive "p")
1764 (let ((buffer-read-only nil))
1765 (setq difference-number (1- difference-number))
1766 (if (and (>= difference-number -1)
1767 (< difference-number (1+ emerge-number-of-differences)))
1768 (emerge-unselect-and-select-difference difference-number)
1769 (error "Bad difference number"))))
1770
1771 (defun emerge-quit (arg)
1772 "Finish an Emerge session.
1773 Prefix argument means to abort rather than successfully finish.
1774 The difference depends on how the merge was started,
1775 but usually means to not write over one of the original files, or to signal
1776 to some process which invoked Emerge a failure code.
1777
1778 Unselects the selected difference, if any, restores the read-only and modified
1779 flags of the merged file buffers, restores the local keymap of the merge
1780 buffer, and sets off various emerge flags. Using Emerge commands in this
1781 buffer after this will cause serious problems."
1782 (interactive "P")
1783 (if (prog1
1784 (y-or-n-p
1785 (if (not arg)
1786 "Do you really want to successfully finish this merge? "
1787 "Do you really want to abort this merge? "))
1788 (message ""))
1789 (emerge-really-quit arg)))
1790
1791 ;; Perform the quit operations.
1792 (defun emerge-really-quit (arg)
1793 (setq buffer-read-only nil)
1794 (emerge-unselect-and-select-difference -1)
1795 (emerge-restore-buffer-characteristics)
1796 ;; null out the difference markers so they don't slow down future editing
1797 ;; operations
1798 (mapcar (function (lambda (d)
1799 (set-marker (aref d 0) nil)
1800 (set-marker (aref d 1) nil)
1801 (set-marker (aref d 2) nil)
1802 (set-marker (aref d 3) nil)
1803 (set-marker (aref d 4) nil)
1804 (set-marker (aref d 5) nil)))
1805 emerge-difference-list)
1806 ;; allow them to be garbage collected
1807 (setq emerge-difference-list nil)
1808 ;; restore the local map
1809 (use-local-map emerge-old-keymap)
1810 ;; turn off all the emerge modes
1811 (setq emerge-mode nil)
1812 (setq emerge-fast-mode nil)
1813 (setq emerge-edit-mode nil)
1814 (setq emerge-auto-advance nil)
1815 (setq emerge-skip-prefers nil)
1816 ;; restore mode line
1817 (kill-local-variable 'mode-line-buffer-identification)
1818 (let ((emerge-prefix-argument arg))
1819 (run-hooks 'emerge-quit-hook)))
1820
1821 (defun emerge-select-A (&optional force)
1822 "Select the A variant of this difference.
1823 Refuses to function if this difference has been edited, i.e., if it
1824 is neither the A nor the B variant.
1825 An ARGUMENT forces the variant to be selected even if the difference has
1826 been edited."
1827 (interactive "P")
1828 (let ((operate
1829 (function (lambda ()
1830 (emerge-select-A-edit merge-begin merge-end A-begin A-end)
1831 (if emerge-auto-advance
1832 (emerge-next-difference)))))
1833 (operate-no-change
1834 (function (lambda ()
1835 (if emerge-auto-advance
1836 (emerge-next-difference))))))
1837 (emerge-select-version force operate-no-change operate operate)))
1838
1839 ;; Actually select the A variant
1840 (defun emerge-select-A-edit (merge-begin merge-end A-begin A-end)
1841 (emerge-eval-in-buffer
1842 emerge-merge-buffer
1843 (delete-region merge-begin merge-end)
1844 (goto-char merge-begin)
1845 (insert-buffer-substring emerge-A-buffer A-begin A-end)
1846 (goto-char merge-begin)
1847 (aset diff-vector 6 'A)
1848 (emerge-refresh-mode-line)))
1849
1850 (defun emerge-select-B (&optional force)
1851 "Select the B variant of this difference.
1852 Refuses to function if this difference has been edited, i.e., if it
1853 is neither the A nor the B variant. An ARGUMENT forces the variant to be selected even if the difference has
1854 been edited."
1855 (interactive "P")
1856 (let ((operate
1857 (function (lambda ()
1858 (emerge-select-B-edit merge-begin merge-end B-begin B-end)
1859 (if emerge-auto-advance
1860 (emerge-next-difference)))))
1861 (operate-no-change
1862 (function (lambda ()
1863 (if emerge-auto-advance
1864 (emerge-next-difference))))))
1865 (emerge-select-version force operate operate-no-change operate)))
1866
1867 ;; Actually select the B variant
1868 (defun emerge-select-B-edit (merge-begin merge-end B-begin B-end)
1869 (emerge-eval-in-buffer
1870 emerge-merge-buffer
1871 (delete-region merge-begin merge-end)
1872 (goto-char merge-begin)
1873 (insert-buffer-substring emerge-B-buffer B-begin B-end)
1874 (goto-char merge-begin)
1875 (aset diff-vector 6 'B)
1876 (emerge-refresh-mode-line)))
1877
1878 (defun emerge-default-A ()
1879 "Selects the A variant.
1880 This selects the A variant for all differences from here down in the buffer
1881 which are still defaulted, i.e., which the user has not selected and for
1882 which there is no preference."
1883 (interactive)
1884 (let ((buffer-read-only nil))
1885 (let ((selected-difference emerge-current-difference)
1886 (n (max emerge-current-difference 0)))
1887 (while (< n emerge-number-of-differences)
1888 (let ((diff-vector (aref emerge-difference-list n)))
1889 (if (eq (aref diff-vector 6) 'default-B)
1890 (progn
1891 (emerge-unselect-and-select-difference n t)
1892 (emerge-select-A)
1893 (aset diff-vector 6 'default-A))))
1894 (setq n (1+ n))
1895 (if (= (* (/ n 10) 10) n)
1896 (message "Setting default to A...%d" n)))
1897 (emerge-unselect-and-select-difference selected-difference)))
1898 (message "Default A set"))
1899
1900 (defun emerge-default-B ()
1901 "Selects the B variant.
1902 This selects the B variant for all differences from here down in the buffer
1903 which are still defaulted, i.e., which the user has not selected and for
1904 which there is no preference."
1905 (interactive)
1906 (let ((buffer-read-only nil))
1907 (let ((selected-difference emerge-current-difference)
1908 (n (max emerge-current-difference 0)))
1909 (while (< n emerge-number-of-differences)
1910 (let ((diff-vector (aref emerge-difference-list n)))
1911 (if (eq (aref diff-vector 6) 'default-A)
1912 (progn
1913 (emerge-unselect-and-select-difference n t)
1914 (emerge-select-B)
1915 (aset diff-vector 6 'default-B))))
1916 (setq n (1+ n))
1917 (if (= (* (/ n 10) 10) n)
1918 (message "Setting default to B...%d" n)))
1919 (emerge-unselect-and-select-difference selected-difference)))
1920 (message "Default B set"))
1921
1922 (defun emerge-fast-mode ()
1923 "Set fast mode.
1924 In this mode ordinary Emacs commands are disabled, and Emerge commands
1925 are need not be prefixed with \\<emerge-fast-keymap>\\[emerge-basic-keymap]."
1926 (interactive)
1927 (setq buffer-read-only t)
1928 (use-local-map emerge-fast-keymap)
1929 (setq emerge-mode t)
1930 (setq emerge-fast-mode t)
1931 (setq emerge-edit-mode nil)
1932 (message "Fast mode set")
1933 ;; force mode line redisplay
1934 (set-buffer-modified-p (buffer-modified-p)))
1935
1936 (defun emerge-edit-mode ()
1937 "Set edit mode.
1938 In this mode ordinary Emacs commands are available, and Emerge commands
1939 must be prefixed with \\<emerge-fast-keymap>\\[emerge-basic-keymap]."
1940 (interactive)
1941 (setq buffer-read-only nil)
1942 (use-local-map emerge-edit-keymap)
1943 (setq emerge-mode t)
1944 (setq emerge-fast-mode nil)
1945 (setq emerge-edit-mode t)
1946 (message "Edit mode set")
1947 ;; force mode line redisplay
1948 (set-buffer-modified-p (buffer-modified-p)))
1949
1950 (defun emerge-auto-advance (arg)
1951 "Toggle auto-advance mode.
1952 This mode causes `emerge-select-A' and `emerge-select-B' to automatically
1953 advance to the next difference. (See `emerge-auto-advance'.)
1954 If a positive ARGUMENT is given, it turns on `auto-advance-mode'.
1955 If a negative ARGUMENT is given, it turns off `auto-advance-mode'."
1956 (interactive "P")
1957 (setq emerge-auto-advance (if (null arg)
1958 (not emerge-auto-advance)
1959 (> (prefix-numeric-value arg) 0)))
1960 (message (if emerge-skip-prefers
1961 "Auto-advance set"
1962 "Auto-advance cleared"))
1963 ;; force mode line redisplay
1964 (set-buffer-modified-p (buffer-modified-p)))
1965
1966 (defun emerge-skip-prefers (arg)
1967 "Toggle skip-prefers mode.
1968 This mode causes `emerge-next-difference' and `emerge-previous-difference'
1969 to automatically skip over differences for which there is a preference.
1970 (See `emerge-skip-prefers'.) If a positive ARG is given, it turns on
1971 `skip-prefers' mode.
1972 If a negative ARG is given, it turns off `skip-prefers' mode."
1973 (interactive "P")
1974 (setq emerge-skip-prefers (if (null arg)
1975 (not emerge-skip-prefers)
1976 (> (prefix-numeric-value arg) 0)))
1977 (message (if emerge-skip-prefers
1978 "Skip-prefers set"
1979 "Skip-prefers cleared"))
1980 ;; force mode line redisplay
1981 (set-buffer-modified-p (buffer-modified-p)))
1982
1983 (defun emerge-copy-as-kill-A ()
1984 "Put the A variant of this difference in the kill ring."
1985 (interactive)
1986 (emerge-validate-difference)
1987 (let* ((diff-vector
1988 (aref emerge-difference-list emerge-current-difference))
1989 (A-begin (1+ (aref diff-vector 0)))
1990 (A-end (1- (aref diff-vector 1)))
1991 ;; so further kills don't append
1992 this-command)
1993 (save-excursion
1994 (set-buffer emerge-A-buffer)
1995 (copy-region-as-kill A-begin A-end))))
1996
1997 (defun emerge-copy-as-kill-B ()
1998 "Put the B variant of this difference in the kill ring."
1999 (interactive)
2000 (emerge-validate-difference)
2001 (let* ((diff-vector
2002 (aref emerge-difference-list emerge-current-difference))
2003 (B-begin (1+ (aref diff-vector 2)))
2004 (B-end (1- (aref diff-vector 3)))
2005 ;; so further kills don't append
2006 this-command)
2007 (save-excursion
2008 (set-buffer emerge-B-buffer)
2009 (copy-region-as-kill B-begin B-end))))
2010
2011 (defun emerge-insert-A (arg)
2012 "Insert the A variant of this difference at the point.
2013 Leaves point after text, mark before.
2014 With prefix argument, puts point before, mark after."
2015 (interactive "P")
2016 (emerge-validate-difference)
2017 (let* ((diff-vector
2018 (aref emerge-difference-list emerge-current-difference))
2019 (A-begin (1+ (aref diff-vector 0)))
2020 (A-end (1- (aref diff-vector 1)))
2021 (opoint (point))
2022 (buffer-read-only nil))
2023 (insert-buffer-substring emerge-A-buffer A-begin A-end)
2024 (if (not arg)
2025 (set-mark opoint)
2026 (set-mark (point))
2027 (goto-char opoint))))
2028
2029 (defun emerge-insert-B (arg)
2030 "Insert the B variant of this difference at the point.
2031 Leaves point after text, mark before.
2032 With prefix argument, puts point before, mark after."
2033 (interactive "P")
2034 (emerge-validate-difference)
2035 (let* ((diff-vector
2036 (aref emerge-difference-list emerge-current-difference))
2037 (B-begin (1+ (aref diff-vector 2)))
2038 (B-end (1- (aref diff-vector 3)))
2039 (opoint (point))
2040 (buffer-read-only nil))
2041 (insert-buffer-substring emerge-B-buffer B-begin B-end)
2042 (if (not arg)
2043 (set-mark opoint)
2044 (set-mark (point))
2045 (goto-char opoint))))
2046
2047 (defun emerge-mark-difference (arg)
2048 "Leaves the point before this difference and the mark after it.
2049 With prefix argument, puts mark before, point after."
2050 (interactive "P")
2051 (emerge-validate-difference)
2052 (let* ((diff-vector
2053 (aref emerge-difference-list emerge-current-difference))
2054 (merge-begin (1+ (aref diff-vector 4)))
2055 (merge-end (1- (aref diff-vector 5))))
2056 (if (not arg)
2057 (progn
2058 (goto-char merge-begin)
2059 (set-mark merge-end))
2060 (goto-char merge-end)
2061 (set-mark merge-begin))))
2062
2063 (defun emerge-file-names ()
2064 "Show the names of the buffers or files being operated on by Emerge.
2065 Use C-u l to reset the windows afterward."
2066 (interactive)
2067 (delete-other-windows)
2068 (let ((temp-buffer-show-function
2069 (function (lambda (buf)
2070 (split-window-vertically)
2071 (switch-to-buffer buf)
2072 (other-window 1)))))
2073 (with-output-to-temp-buffer "*Help*"
2074 (emerge-eval-in-buffer emerge-A-buffer
2075 (if buffer-file-name
2076 (progn
2077 (princ "File A is: ")
2078 (princ buffer-file-name))
2079 (progn
2080 (princ "Buffer A is: ")
2081 (princ (buffer-name))))
2082 (princ "\n"))
2083 (emerge-eval-in-buffer emerge-B-buffer
2084 (if buffer-file-name
2085 (progn
2086 (princ "File B is: ")
2087 (princ buffer-file-name))
2088 (progn
2089 (princ "Buffer B is: ")
2090 (princ (buffer-name))))
2091 (princ "\n"))
2092 (if emerge-ancestor-buffer
2093 (emerge-eval-in-buffer emerge-ancestor-buffer
2094 (if buffer-file-name
2095 (progn
2096 (princ "Ancestor file is: ")
2097 (princ buffer-file-name))
2098 (progn
2099 (princ "Ancestor buffer is: ")
2100 (princ (buffer-name))))
2101 (princ "\n")))
2102 (princ emerge-output-description))))
2103
2104 (defun emerge-join-differences (arg)
2105 "Join the selected difference with the following one.
2106 With a prefix argument, join with the preceeding one."
2107 (interactive "P")
2108 (let ((n emerge-current-difference))
2109 ;; adjust n to be first difference to join
2110 (if arg
2111 (setq n (1- n)))
2112 ;; n and n+1 are the differences to join
2113 ;; check that they are both differences
2114 (if (or (< n 0) (>= n (1- emerge-number-of-differences)))
2115 (error "Incorrect differences to join"))
2116 ;; remove the flags
2117 (emerge-unselect-difference emerge-current-difference)
2118 ;; decrement total number of differences
2119 (setq emerge-number-of-differences (1- emerge-number-of-differences))
2120 ;; build new differences vector
2121 (let ((i 0)
2122 (new-differences (make-vector emerge-number-of-differences nil)))
2123 (while (< i emerge-number-of-differences)
2124 (aset new-differences i
2125 (cond
2126 ((< i n) (aref emerge-difference-list i))
2127 ((> i n) (aref emerge-difference-list (1+ i)))
2128 (t (let ((prev (aref emerge-difference-list i))
2129 (next (aref emerge-difference-list (1+ i))))
2130 (vector (aref prev 0)
2131 (aref next 1)
2132 (aref prev 2)
2133 (aref next 3)
2134 (aref prev 4)
2135 (aref next 5)
2136 (let ((ps (aref prev 6))
2137 (ns (aref next 6)))
2138 (cond
2139 ((eq ps ns)
2140 ps)
2141 ((and (or (eq ps 'B) (eq ps 'prefer-B))
2142 (or (eq ns 'B) (eq ns 'prefer-B)))
2143 'B)
2144 (t 'A))))))))
2145 (setq i (1+ i)))
2146 (setq emerge-difference-list new-differences))
2147 ;; set the current difference correctly
2148 (setq emerge-current-difference n)
2149 ;; fix the mode line
2150 (emerge-refresh-mode-line)
2151 ;; reinsert the flags
2152 (emerge-select-difference emerge-current-difference)
2153 (emerge-recenter)))
2154
2155 (defun emerge-split-difference ()
2156 "Split the current difference where the points are in the three windows."
2157 (interactive)
2158 (let ((n emerge-current-difference))
2159 ;; check that this is a valid difference
2160 (emerge-validate-difference)
2161 ;; get the point values and old difference
2162 (let ((A-point (emerge-eval-in-buffer emerge-A-buffer
2163 (point-marker)))
2164 (B-point (emerge-eval-in-buffer emerge-B-buffer
2165 (point-marker)))
2166 (merge-point (point-marker))
2167 (old-diff (aref emerge-difference-list n)))
2168 ;; check location of the points, give error if they aren't in the
2169 ;; differences
2170 (if (or (< A-point (aref old-diff 0))
2171 (> A-point (aref old-diff 1)))
2172 (error "Point outside of difference in A buffer"))
2173 (if (or (< B-point (aref old-diff 2))
2174 (> B-point (aref old-diff 3)))
2175 (error "Point outside of difference in B buffer"))
2176 (if (or (< merge-point (aref old-diff 4))
2177 (> merge-point (aref old-diff 5)))
2178 (error "Point outside of difference in merge buffer"))
2179 ;; remove the flags
2180 (emerge-unselect-difference emerge-current-difference)
2181 ;; increment total number of differences
2182 (setq emerge-number-of-differences (1+ emerge-number-of-differences))
2183 ;; build new differences vector
2184 (let ((i 0)
2185 (new-differences (make-vector emerge-number-of-differences nil)))
2186 (while (< i emerge-number-of-differences)
2187 (aset new-differences i
2188 (cond
2189 ((< i n)
2190 (aref emerge-difference-list i))
2191 ((> i (1+ n))
2192 (aref emerge-difference-list (1- i)))
2193 ((= i n)
2194 (vector (aref old-diff 0)
2195 A-point
2196 (aref old-diff 2)
2197 B-point
2198 (aref old-diff 4)
2199 merge-point
2200 (aref old-diff 6)))
2201 (t
2202 (vector (copy-marker A-point)
2203 (aref old-diff 1)
2204 (copy-marker B-point)
2205 (aref old-diff 3)
2206 (copy-marker merge-point)
2207 (aref old-diff 5)
2208 (aref old-diff 6)))))
2209 (setq i (1+ i)))
2210 (setq emerge-difference-list new-differences))
2211 ;; set the current difference correctly
2212 (setq emerge-current-difference n)
2213 ;; fix the mode line
2214 (emerge-refresh-mode-line)
2215 ;; reinsert the flags
2216 (emerge-select-difference emerge-current-difference)
2217 (emerge-recenter))))
2218
2219 (defun emerge-trim-difference ()
2220 "Trim lines off top and bottom of difference that are the same.
2221 If lines are the same in both the A and the B versions, strip them off.
2222 (This can happen when the A and B versions have common lines that the
2223 ancestor version does not share.)"
2224 (interactive)
2225 ;; make sure we are in a real difference
2226 (emerge-validate-difference)
2227 ;; remove the flags
2228 (emerge-unselect-difference emerge-current-difference)
2229 (let* ((diff (aref emerge-difference-list emerge-current-difference))
2230 (top-a (marker-position (aref diff 0)))
2231 (bottom-a (marker-position (aref diff 1)))
2232 (top-b (marker-position (aref diff 2)))
2233 (bottom-b (marker-position (aref diff 3)))
2234 (top-m (marker-position (aref diff 4)))
2235 (bottom-m (marker-position (aref diff 5)))
2236 size success sa sb sm)
2237 ;; move down the tops of the difference regions as much as possible
2238 ;; Try advancing comparing 1000 chars at a time.
2239 ;; When that fails, go 500 chars at a time, and so on.
2240 (setq size 1000)
2241 (while (> size 0)
2242 (setq success t)
2243 (while success
2244 (setq size (min size (- bottom-a top-a) (- bottom-b top-b)
2245 (- bottom-m top-m)))
2246 (setq sa (emerge-eval-in-buffer emerge-A-buffer
2247 (buffer-substring top-a
2248 (+ size top-a))))
2249 (setq sb (emerge-eval-in-buffer emerge-B-buffer
2250 (buffer-substring top-b
2251 (+ size top-b))))
2252 (setq sm (buffer-substring top-m (+ size top-m)))
2253 (setq success (and (> size 0) (equal sa sb) (equal sb sm)))
2254 (if success
2255 (setq top-a (+ top-a size)
2256 top-b (+ top-b size)
2257 top-m (+ top-m size))))
2258 (setq size (/ size 2)))
2259 ;; move up the bottoms of the difference regions as much as possible
2260 ;; Try advancing comparing 1000 chars at a time.
2261 ;; When that fails, go 500 chars at a time, and so on.
2262 (setq size 1000)
2263 (while (> size 0)
2264 (setq success t)
2265 (while success
2266 (setq size (min size (- bottom-a top-a) (- bottom-b top-b)
2267 (- bottom-m top-m)))
2268 (setq sa (emerge-eval-in-buffer emerge-A-buffer
2269 (buffer-substring (- bottom-a size)
2270 bottom-a)))
2271 (setq sb (emerge-eval-in-buffer emerge-B-buffer
2272 (buffer-substring (- bottom-b size)
2273 bottom-b)))
2274 (setq sm (buffer-substring (- bottom-m size) bottom-m))
2275 (setq success (and (> size 0) (equal sa sb) (equal sb sm)))
2276 (if success
2277 (setq bottom-a (- bottom-a size)
2278 bottom-b (- bottom-b size)
2279 bottom-m (- bottom-m size))))
2280 (setq size (/ size 2)))
2281 ;; {top,bottom}-{a,b,m} are now set at the new beginnings and ends
2282 ;; of the difference regions. Move them to the beginning of lines, as
2283 ;; appropriate.
2284 (emerge-eval-in-buffer emerge-A-buffer
2285 (goto-char top-a)
2286 (beginning-of-line)
2287 (aset diff 0 (point-marker))
2288 (goto-char bottom-a)
2289 (beginning-of-line 2)
2290 (aset diff 1 (point-marker)))
2291 (emerge-eval-in-buffer emerge-B-buffer
2292 (goto-char top-b)
2293 (beginning-of-line)
2294 (aset diff 2 (point-marker))
2295 (goto-char bottom-b)
2296 (beginning-of-line 2)
2297 (aset diff 3 (point-marker)))
2298 (goto-char top-m)
2299 (beginning-of-line)
2300 (aset diff 4 (point-marker))
2301 (goto-char bottom-m)
2302 (beginning-of-line 2)
2303 (aset diff 5 (point-marker))
2304 ;; put the flags back in, recenter the display
2305 (emerge-select-difference emerge-current-difference)
2306 (emerge-recenter)))
2307
2308 (defun emerge-find-difference (arg)
2309 "Find the difference containing the current position of the point.
2310 If there is no containing difference and the prefix argument is positive,
2311 it finds the nearest following difference. A negative prefix argument finds
2312 the nearest previous difference."
2313 (interactive "P")
2314 ;; search for the point in the merge buffer, using the markers
2315 ;; for the beginning and end of the differences in the merge buffer
2316 (emerge-find-difference1 arg (point) 4 5))
2317
2318 (defun emerge-find-difference-A (arg)
2319 "Find the difference containing the position of the point in the A buffer.
2320 This command must be executed in the merge buffer.
2321 If there is no containing difference and the prefix argument is positive,
2322 it finds the nearest following difference. A negative prefix argument finds
2323 the nearest previous difference."
2324 (interactive "P")
2325 ;; search for the point in the A buffer, using the markers
2326 ;; for the beginning and end of the differences in the A buffer
2327 (emerge-find-difference1 arg
2328 (emerge-eval-in-buffer emerge-A-buffer (point))
2329 0 1))
2330
2331 (defun emerge-find-difference-B (arg)
2332 "Find the difference containing the position of the point in the B buffer.
2333 This command must be executed in the merge buffer.
2334 If there is no containing difference and the prefix argument is positive,
2335 it finds the nearest following difference. A negative prefix argument finds
2336 the nearest previous difference."
2337 (interactive "P")
2338 ;; search for the point in the B buffer, using the markers
2339 ;; for the beginning and end of the differences in the B buffer
2340 (emerge-find-difference1 arg
2341 (emerge-eval-in-buffer emerge-B-buffer (point))
2342 2 3))
2343
2344 (defun emerge-find-difference1 (arg location begin end)
2345 (let* ((index
2346 ;; find first difference containing or after the current position
2347 (catch 'search
2348 (let ((n 0))
2349 (while (< n emerge-number-of-differences)
2350 (let ((diff-vector (aref emerge-difference-list n)))
2351 (if (<= location (marker-position (aref diff-vector end)))
2352 (throw 'search n)))
2353 (setq n (1+ n))))
2354 emerge-number-of-differences))
2355 (contains
2356 ;; whether the found difference contains the current position
2357 (and (< index emerge-number-of-differences)
2358 (<= (marker-position (aref (aref emerge-difference-list index)
2359 begin))
2360 location)))
2361 (arg-value
2362 ;; numeric value of prefix argument
2363 (prefix-numeric-value arg)))
2364 (emerge-unselect-and-select-difference
2365 (cond
2366 ;; if the point is in a difference, select it
2367 (contains index)
2368 ;; if the arg is nil and the point is not in a difference, error
2369 ((null arg) (error "No difference contains point"))
2370 ;; if the arg is positive, select the following difference
2371 ((> arg-value 0)
2372 (if (< index emerge-number-of-differences)
2373 index
2374 (error "No difference contains or follows point")))
2375 ;; if the arg is negative, select the preceeding difference
2376 (t
2377 (if (> index 0)
2378 (1- index)
2379 (error "No difference contains or preceeds point")))))))
2380
2381 (defun emerge-line-numbers ()
2382 "Display the current line numbers.
2383 This function displays the line numbers of the points in the A, B, and
2384 merge buffers."
2385 (interactive)
2386 (let* ((valid-diff
2387 (and (>= emerge-current-difference 0)
2388 (< emerge-current-difference emerge-number-of-differences)))
2389 (diff (and valid-diff
2390 (aref emerge-difference-list emerge-current-difference)))
2391 (merge-line (emerge-line-number-in-buf 4 5))
2392 (A-line (emerge-eval-in-buffer emerge-A-buffer
2393 (emerge-line-number-in-buf 0 1)))
2394 (B-line (emerge-eval-in-buffer emerge-B-buffer
2395 (emerge-line-number-in-buf 2 3))))
2396 (message "At lines: merge = %d, A = %d, B = %d"
2397 merge-line A-line B-line)))
2398
2399 (defun emerge-line-number-in-buf (begin-marker end-marker)
2400 (let (temp)
2401 (setq temp (save-excursion
2402 (beginning-of-line)
2403 (1+ (count-lines 1 (point)))))
2404 (if valid-diff
2405 (progn
2406 (if (> (point) (aref diff begin-marker))
2407 (setq temp (- temp emerge-before-flag-lines)))
2408 (if (> (point) (aref diff end-marker))
2409 (setq temp (- temp emerge-after-flag-lines)))))
2410 temp))
2411
2412 (defun emerge-set-combine-versions-template (start end &optional localize)
2413 "Copy region into `emerge-combine-versions-template'.
2414 This controls how `emerge-combine-versions' will combine the two versions.
2415 With prefix argument, `emerge-combine-versions' is made local to this
2416 merge buffer. Localization is permanent for any particular merge buffer."
2417 (interactive "r\nP")
2418 (if localize
2419 (make-local-variable 'emerge-combine-versions-template))
2420 (setq emerge-combine-versions-template (buffer-substring start end))
2421 (message
2422 (if (assq 'emerge-combine-versions-template (buffer-local-variables))
2423 "emerge-set-combine-versions-template set locally."
2424 "emerge-set-combine-versions-template set.")))
2425
2426 (defun emerge-combine-versions (&optional force)
2427 "Combine versions using the template in `emerge-combine-versions-template'.
2428 Refuses to function if this difference has been edited, i.e., if it is
2429 neither the A nor the B variant.
2430 An argument forces the variant to be selected even if the difference has
2431 been edited."
2432 (interactive "P")
2433 (emerge-combine-versions-internal emerge-combine-versions-template force))
2434
2435 (defun emerge-combine-versions-register (char &optional force)
2436 "Combine the two versions using the template in register REG.
2437 See documentation of the variable `emerge-combine-versions-template'
2438 for how the template is interpreted.
2439 Refuses to function if this difference has been edited, i.e., if it is
2440 neither the A nor the B variant.
2441 An argument forces the variant to be selected even if the difference has
2442 been edited."
2443 (interactive "cRegister containing template: \nP")
2444 (let ((template (get-register char)))
2445 (if (not (stringp template))
2446 (error "Register does not contain text"))
2447 (emerge-combine-versions-internal template force)))
2448
2449 (defun emerge-combine-versions-internal (template force)
2450 (let ((operate
2451 (function (lambda ()
2452 (emerge-combine-versions-edit merge-begin merge-end
2453 A-begin A-end B-begin B-end)
2454 (if emerge-auto-advance
2455 (emerge-next-difference))))))
2456 (emerge-select-version force operate operate operate)))
2457
2458 (defun emerge-combine-versions-edit (merge-begin merge-end
2459 A-begin A-end B-begin B-end)
2460 (emerge-eval-in-buffer
2461 emerge-merge-buffer
2462 (delete-region merge-begin merge-end)
2463 (goto-char merge-begin)
2464 (let ((i 0))
2465 (while (< i (length template))
2466 (let ((c (aref template i)))
2467 (if (= c ?%)
2468 (progn
2469 (setq i (1+ i))
2470 (setq c
2471 (condition-case nil
2472 (aref template i)
2473 (error ?%)))
2474 (cond ((= c ?a)
2475 (insert-buffer-substring emerge-A-buffer A-begin A-end))
2476 ((= c ?b)
2477 (insert-buffer-substring emerge-B-buffer B-begin B-end))
2478 ((= c ?%)
2479 (insert ?%))
2480 (t
2481 (insert c))))
2482 (insert c)))
2483 (setq i (1+ i))))
2484 (goto-char merge-begin)
2485 (aset diff-vector 6 'combined)
2486 (emerge-refresh-mode-line)))
2487
2488 (defun emerge-set-merge-mode (mode)
2489 "Set the major mode in a merge buffer.
2490 Overrides any change that the mode might make to the mode line or local
2491 keymap. Leaves merge in fast mode."
2492 (interactive
2493 (list (intern (completing-read "New major mode for merge buffer: "
2494 obarray 'commandp t nil))))
2495 (funcall mode)
2496 (emerge-refresh-mode-line)
2497 (if emerge-fast-mode
2498 (emerge-fast-mode)
2499 (emerge-edit-mode)))
2500
2501 (defun emerge-one-line-window ()
2502 (interactive)
2503 (let ((window-min-height 1))
2504 (shrink-window (- (window-height) 2))))
2505
2506 ;;; Support routines
2507
2508 ;; Select a difference by placing the visual flags around the appropriate
2509 ;; group of lines in the A, B, and merge buffers
2510 (defun emerge-select-difference (n)
2511 (let ((diff-vector (aref emerge-difference-list n)))
2512 (emerge-place-flags-in-buffer emerge-A-buffer
2513 (aref diff-vector 0) (aref diff-vector 1))
2514 (emerge-place-flags-in-buffer emerge-B-buffer
2515 (aref diff-vector 2) (aref diff-vector 3))
2516 (emerge-place-flags-in-buffer emerge-merge-buffer
2517 (aref diff-vector 4) (aref diff-vector 5))))
2518
2519 (defun emerge-place-flags-in-buffer (buffer before after)
2520 (if (eq buffer emerge-merge-buffer)
2521 (emerge-place-flags-in-buffer1 buffer before after)
2522 (emerge-eval-in-buffer
2523 buffer
2524 (emerge-place-flags-in-buffer1 buffer before after))))
2525
2526 (defun emerge-place-flags-in-buffer1 (buffer before after)
2527 (let ((buffer-read-only nil))
2528 ;; insert the flags
2529 (goto-char before)
2530 (insert-before-markers emerge-before-flag)
2531 (goto-char after)
2532 (insert emerge-after-flag)
2533 ;; put the markers into the flags, so alterations above or below won't move
2534 ;; them
2535 ;; before marker is one char before the end of the before flag
2536 ;; after marker is one char after the beginning of the after flag
2537 (set-marker before (1- before))
2538 (set-marker after (1+ after))))
2539
2540 ;; Unselect a difference by removing the visual flags in the buffers.
2541 (defun emerge-unselect-difference (n)
2542 (let ((diff-vector (aref emerge-difference-list n)))
2543 (emerge-remove-flags-in-buffer emerge-A-buffer
2544 (aref diff-vector 0) (aref diff-vector 1))
2545 (emerge-remove-flags-in-buffer emerge-B-buffer
2546 (aref diff-vector 2) (aref diff-vector 3))
2547 (emerge-remove-flags-in-buffer emerge-merge-buffer
2548 (aref diff-vector 4) (aref diff-vector 5))))
2549
2550 (defun emerge-remove-flags-in-buffer (buffer before after)
2551 (emerge-eval-in-buffer
2552 buffer
2553 (let ((buffer-read-only nil))
2554 ;; put the markers at the beginning of the flags
2555 (set-marker before (- before (1- emerge-before-flag-length)))
2556 (set-marker after (1- after))
2557 ;; remove the flags
2558 (goto-char before)
2559 (if (looking-at emerge-before-flag-match)
2560 (delete-char emerge-before-flag-length)
2561 ;; the flag isn't there
2562 (ding)
2563 (message "Trouble removing flag."))
2564 (goto-char after)
2565 (if (looking-at emerge-after-flag-match)
2566 (delete-char emerge-after-flag-length)
2567 ;; the flag isn't there
2568 (ding)
2569 (message "Trouble removing flag.")))))
2570
2571 ;; Select a difference, removing an flags that exist now.
2572 (defun emerge-unselect-and-select-difference (n &optional suppress-display)
2573 (if (and (>= emerge-current-difference 0)
2574 (< emerge-current-difference emerge-number-of-differences))
2575 (emerge-unselect-difference emerge-current-difference))
2576 (if (and (>= n 0) (< n emerge-number-of-differences))
2577 (progn
2578 (emerge-select-difference n)
2579 (let* ((diff-vector (aref emerge-difference-list n))
2580 (selection-type (aref diff-vector 6)))
2581 (if (eq selection-type 'default-A)
2582 (aset diff-vector 6 'A)
2583 (if (eq selection-type 'default-B)
2584 (aset diff-vector 6 'B))))))
2585 (setq emerge-current-difference n)
2586 (if (not suppress-display)
2587 (progn
2588 (emerge-recenter)
2589 (emerge-refresh-mode-line))))
2590
2591 ;; Perform tests to see whether user should be allowed to select a version
2592 ;; of this difference:
2593 ;; a valid difference has been selected; and
2594 ;; the difference text in the merge buffer is:
2595 ;; the A version (execute a-version), or
2596 ;; the B version (execute b-version), or
2597 ;; empty (execute neither-version), or
2598 ;; argument FORCE is true (execute neither-version)
2599 ;; Otherwise, signal an error.
2600 (defun emerge-select-version (force a-version b-version neither-version)
2601 (emerge-validate-difference)
2602 (let ((buffer-read-only nil))
2603 (let* ((diff-vector
2604 (aref emerge-difference-list emerge-current-difference))
2605 (A-begin (1+ (aref diff-vector 0)))
2606 (A-end (1- (aref diff-vector 1)))
2607 (B-begin (1+ (aref diff-vector 2)))
2608 (B-end (1- (aref diff-vector 3)))
2609 (merge-begin (1+ (aref diff-vector 4)))
2610 (merge-end (1- (aref diff-vector 5))))
2611 (if (emerge-compare-buffers emerge-A-buffer A-begin A-end
2612 emerge-merge-buffer merge-begin
2613 merge-end)
2614 (funcall a-version)
2615 (if (emerge-compare-buffers emerge-B-buffer B-begin B-end
2616 emerge-merge-buffer merge-begin
2617 merge-end)
2618 (funcall b-version)
2619 (if (or force (= merge-begin merge-end))
2620 (funcall neither-version)
2621 (error "This difference region has been edited.")))))))
2622
2623 ;; Revise the mode line to display which difference we have selected
2624
2625 (defun emerge-refresh-mode-line ()
2626 (setq mode-line-buffer-identification
2627 (list (format "Emerge: %%b diff %d of %d%s"
2628 (1+ emerge-current-difference)
2629 emerge-number-of-differences
2630 (if (and (>= emerge-current-difference 0)
2631 (< emerge-current-difference
2632 emerge-number-of-differences))
2633 (cdr (assq (aref (aref emerge-difference-list
2634 emerge-current-difference)
2635 6)
2636 '((A . " - A")
2637 (B . " - B")
2638 (prefer-A . " - A*")
2639 (prefer-B . " - B*")
2640 (combined . " - comb"))))
2641 ""))))
2642 ;; Force mode-line redisplay
2643 (set-buffer-modified-p (buffer-modified-p)))
2644
2645 ;; compare two regions in two buffers for containing the same text
2646 (defun emerge-compare-buffers (buffer-x x-begin x-end buffer-y y-begin y-end)
2647 ;; first check that the two regions are the same length
2648 (if (not (and (= (- x-end x-begin) (- y-end y-begin))))
2649 nil
2650 (catch 'exit
2651 (while (< x-begin x-end)
2652 ;; bite off and compare no more than 1000 characters at a time
2653 (let* ((compare-length (min (- x-end x-begin) 1000))
2654 (x-string (emerge-eval-in-buffer
2655 buffer-x
2656 (buffer-substring x-begin
2657 (+ x-begin compare-length))))
2658 (y-string (emerge-eval-in-buffer
2659 buffer-y
2660 (buffer-substring y-begin
2661 (+ y-begin compare-length)))))
2662 (if (not (string-equal x-string y-string))
2663 (throw 'exit nil)
2664 (setq x-begin (+ x-begin compare-length))
2665 (setq y-begin (+ y-begin compare-length)))))
2666 t)))
2667
2668 ;; Construct a unique buffer name.
2669 ;; The first one tried is prefixsuffix, then prefix<2>suffix,
2670 ;; prefix<3>suffix, etc.
2671 (defun emerge-unique-buffer-name (prefix suffix)
2672 (if (null (get-buffer (concat prefix suffix)))
2673 (concat prefix suffix)
2674 (let ((n 2))
2675 (while (get-buffer (format "%s<%d>%s" prefix n suffix))
2676 (setq n (1+ n)))
2677 (format "%s<%d>%s" prefix n suffix))))
2678
2679 ;; Verify that we have a difference selected.
2680 (defun emerge-validate-difference ()
2681 (if (not (and (>= emerge-current-difference 0)
2682 (< emerge-current-difference emerge-number-of-differences)))
2683 (error "No difference selected")))
2684
2685 ;;; Functions for saving and restoring a batch of variables
2686
2687 ;; These functions save (get the values of) and restore (set the values of)
2688 ;; a list of variables. The argument is a list of symbols (the names of
2689 ;; the variables). A list element can also be a list of two functions,
2690 ;; the first of which (when called with no arguments) gets the value, and
2691 ;; the second (when called with a value as an argment) sets the value.
2692 ;; A "function" is anything that funcall can handle as an argument.
2693
2694 (defun emerge-save-variables (vars)
2695 (mapcar (function (lambda (v) (if (symbolp v)
2696 (symbol-value v)
2697 (funcall (car v)))))
2698 vars))
2699
2700 (defun emerge-restore-variables (vars values)
2701 (while vars
2702 (let ((var (car vars))
2703 (value (car values)))
2704 (if (symbolp var)
2705 (set var value)
2706 (funcall (car (cdr var)) value)))
2707 (setq vars (cdr vars))
2708 (setq values (cdr values))))
2709
2710 ;; Make a temporary file that only we have access to.
2711 ;; PREFIX is appended to emerge-temp-file-prefix to make the filename prefix.
2712 (defun emerge-make-temp-file (prefix)
2713 (let ((f (make-temp-name (concat emerge-temp-file-prefix prefix))))
2714 ;; create the file
2715 (write-region (point-min) (point-min) f nil 'no-message)
2716 (set-file-modes f emerge-temp-file-mode)
2717 f))
2718
2719 ;;; Functions that query the user before he can write out the current buffer.
2720
2721 (defun emerge-query-write-file ()
2722 "Query the user if he really wants to write out the incomplete merge.
2723 If he says yes, call `write-file' to do so. See `emerge-query-and-call'
2724 for details of the querying process."
2725 (interactive)
2726 (emerge-query-and-call 'write-file))
2727
2728 (defun emerge-query-save-buffer ()
2729 "Query the user if he really wants to write out the incomplete merge.
2730 If he says yes, call `save-buffer' to do so. See `emerge-query-and-call'
2731 for details of the querying process."
2732 (interactive)
2733 (emerge-query-and-call 'save-buffer))
2734
2735 (defun emerge-query-and-call (command)
2736 "Query the user if he really wants to write out the incomplete merge.
2737 If he says yes, call COMMAND interactively. During the call, the flags
2738 around the current difference are removed."
2739 (if (yes-or-no-p "Do you really write to write out this unfinished merge? ")
2740 ;; He really wants to do it -- unselect the difference for the duration
2741 (progn
2742 (if (and (>= emerge-current-difference 0)
2743 (< emerge-current-difference emerge-number-of-differences))
2744 (emerge-unselect-difference emerge-current-difference))
2745 ;; call-interactively takes the value of current-prefix-arg as the
2746 ;; prefix argument value to be passed to the command. Thus, we have
2747 ;; to do nothing special to make sure the prefix argument is
2748 ;; transmitted to the command.
2749 (call-interactively command)
2750 (if (and (>= emerge-current-difference 0)
2751 (< emerge-current-difference emerge-number-of-differences))
2752 (progn
2753 (emerge-select-difference emerge-current-difference)
2754 (emerge-recenter))))
2755 ;; He's being smart and not doing it
2756 (message "Not written")))
2757
2758 ;; Make sure the current buffer (for a file) has the same contents as the
2759 ;; file on disk, and attempt to remedy the situation if not.
2760 ;; Signal an error if we can't make them the same, or the user doesn't want
2761 ;; to do what is necessary to make them the same.
2762 (defun emerge-verify-file-buffer ()
2763 ;; First check if the file has been modified since the buffer visited it.
2764 (if (verify-visited-file-modtime (current-buffer))
2765 (if (buffer-modified-p)
2766 ;; If buffer is not obsolete and is modified, offer to save
2767 (if (yes-or-no-p (format "Save file %s? " buffer-file-name))
2768 (save-buffer)
2769 (error "Buffer out of sync for file %s" buffer-file-name))
2770 ;; If buffer is not obsolete and is not modified, do nothing
2771 nil)
2772 (if (buffer-modified-p)
2773 ;; If buffer is obsolete and is modified, give error
2774 (error "Buffer out of sync for file %s" buffer-file-name)
2775 ;; If buffer is obsolete and is not modified, offer to revert
2776 (if (yes-or-no-p (format "Revert file %s? " buffer-file-name))
2777 (revert-buffer t t)
2778 (error "Buffer out of sync for file %s" buffer-file-name)))))
2779 \f
2780 ;; Utilities that might have value outside of Emerge.
2781
2782 ;; Set up the mode in the current buffer to duplicate the mode in another
2783 ;; buffer.
2784 (defun emerge-copy-modes (buffer)
2785 ;; Set the major mode
2786 (funcall (emerge-eval-in-buffer buffer major-mode)))
2787
2788 ;; Define a key, even if a prefix of it is defined
2789 (defun emerge-force-define-key (keymap key definition)
2790 "Like `define-key', but isn't stopped if a prefix of KEY is a defined
2791 command."
2792 ;; Find out if a prefix of key is defined
2793 (let ((v (lookup-key keymap key)))
2794 ;; If so, undefine it
2795 (if (integerp v)
2796 (define-key keymap (substring key 0 v) nil)))
2797 ;; Now define the key
2798 (define-key keymap key definition))
2799
2800 ;;; Improvements to describe-mode, so that it describes minor modes as well
2801 ;;; as the major mode
2802 (defun describe-mode (&optional minor)
2803 "Display documentation of current major mode.
2804 If optional MINOR is non-nil (or prefix argument is given if interactive),
2805 display documentation of acive minor modes as well.
2806 For this to work correctly for a minor mode, the mode's indicator variable
2807 (listed in `minor-mode-alist') must also be a function whose documentation
2808 describes the minor mode."
2809 (interactive)
2810 (with-output-to-temp-buffer "*Help*"
2811 (princ mode-name)
2812 (princ " Mode:\n")
2813 (princ (documentation major-mode))
2814 (let ((minor-modes minor-mode-alist)
2815 (locals (buffer-local-variables)))
2816 (while minor-modes
2817 (let* ((minor-mode (car (car minor-modes)))
2818 (indicator (car (cdr (car minor-modes))))
2819 (local-binding (assq minor-mode locals)))
2820 ;; Document a minor mode if it is listed in minor-mode-alist,
2821 ;; bound locally in this buffer, non-nil, and has a function
2822 ;; definition.
2823 (if (and local-binding
2824 (cdr local-binding)
2825 (fboundp minor-mode))
2826 (progn
2827 (princ (format "\n\n\n%s minor mode (indicator%s):\n"
2828 minor-mode indicator))
2829 (princ (documentation minor-mode)))))
2830 (setq minor-modes (cdr minor-modes))))
2831 (print-help-return-message)))
2832
2833 ;; Adjust things so that keyboard macro definitions are documented correctly.
2834 (fset 'defining-kbd-macro (symbol-function 'start-kbd-macro))
2835
2836 ;; Function to shadow a definition in a keymap with definitions in another.
2837 (defun emerge-shadow-key-definition (olddef newdef keymap shadowmap)
2838 "Shadow OLDDEF with NEWDEF for any keys in KEYMAP with entries in SHADOWMAP.
2839 In other words, SHADOWMAP will now shadow all definitions of OLDDEF in KEYMAP
2840 with NEWDEF. Does not affect keys that are already defined in SHADOWMAP,
2841 including those whose definition is OLDDEF."
2842 ;; loop through all keymaps accessible from keymap
2843 (let ((maps (accessible-keymaps keymap)))
2844 (while maps
2845 (let ((prefix (car (car maps)))
2846 (map (cdr (car maps))))
2847 ;; examine a keymap
2848 (if (arrayp map)
2849 ;; array keymap
2850 (let ((len (length map))
2851 (i 0))
2852 (while (< i len)
2853 (if (eq (aref map i) olddef)
2854 ;; set the shadowing definition
2855 (let ((key (concat prefix (char-to-string i))))
2856 (emerge-define-key-if-possible shadowmap key newdef)))
2857 (setq i (1+ i))))
2858 ;; sparse keymap
2859 (while map
2860 (if (eq (cdr-safe (car-safe map)) olddef)
2861 ;; set the shadowing definition
2862 (let ((key
2863 (concat prefix (char-to-string (car (car map))))))
2864 (emerge-define-key-if-possible shadowmap key newdef)))
2865 (setq map (cdr map)))))
2866 (setq maps (cdr maps)))))
2867
2868 ;; Define a key if it (or a prefix) is not already defined in the map.
2869 (defun emerge-define-key-if-possible (keymap key definition)
2870 ;; look up the present definition of the key
2871 (let ((present (lookup-key keymap key)))
2872 (if (integerp present)
2873 ;; if it is "too long", look up the valid prefix
2874 (if (not (lookup-key keymap (substring key 0 present)))
2875 ;; if the prefix isn't defined, define it
2876 (define-key keymap key definition))
2877 ;; if there is no present definition, define it
2878 (if (not present)
2879 (define-key keymap key definition)))))
2880
2881 (defun emerge-recursively-substitute-key-definition (olddef newdef keymap)
2882 "Like `substitute-key-definition', but examines and substitutes in all
2883 keymaps accessible from KEYMAP. Make sure that subordinate keymaps aren't
2884 shared with other keymaps! (`copy-keymap' will suffice.)"
2885 ;; Loop through all keymaps accessible from keymap
2886 (let ((maps (accessible-keymaps keymap)))
2887 (while maps
2888 ;; Substitute in this keymap
2889 (substitute-key-definition olddef newdef (cdr (car maps)))
2890 (setq maps (cdr maps)))))
2891
2892 ;; Show the name of the file in the buffer.
2893 (defun emerge-show-file-name ()
2894 "Displays the name of the file loaded into the current buffer.
2895 If the name won't fit on one line, the minibuffer is expanded to hold it,
2896 and the command waits for a keystroke from the user. If the keystroke is
2897 SPC, it is ignored; if it is anything else, it is processed as a command."
2898 (interactive)
2899 (let ((name (buffer-file-name)))
2900 (or name
2901 (setq name "Buffer has no file name."))
2902 (save-window-excursion
2903 (select-window (minibuffer-window))
2904 (erase-buffer)
2905 (insert name)
2906 (if (not (pos-visible-in-window-p))
2907 (let ((echo-keystrokes 0))
2908 (while (and (not (pos-visible-in-window-p))
2909 (> (1- (frame-height)) (window-height)))
2910 (enlarge-window 1))
2911 (let ((c (read-char)))
2912 (if (/= c 32)
2913 (setq unread-command-event c))))))))
2914
2915 ;; Improved auto-save file names.
2916 ;; This function fixes many problems with the standard auto-save file names:
2917 ;; Auto-save files for non-file buffers get put in the default directory
2918 ;; for the buffer, whether that makes sense or not.
2919 ;; Auto-save files for file buffers get put in the directory of the file,
2920 ;; regardless of whether we can write into it or not.
2921 ;; Auto-save files for non-file buffers don't use the process id, so if a
2922 ;; user runs more than on Emacs, they can make auto-save files that overwrite
2923 ;; each other.
2924 ;; To use this function, do:
2925 ;; (fset 'make-auto-save-file-name
2926 ;; (symbol-function 'emerge-make-auto-save-file-name))
2927 (defun emerge-make-auto-save-file-name ()
2928 "Return file name to use for auto-saves of current buffer.
2929 Does not consider auto-save-visited-file-name; that is checked
2930 before calling this function.
2931 You can redefine this for customization.
2932 See also auto-save-file-name-p."
2933 (if buffer-file-name
2934 ;; if buffer has a file, try the format <file directory>/#<file name>#
2935 (let ((f (concat (file-name-directory buffer-file-name)
2936 "#"
2937 (file-name-nondirectory buffer-file-name)
2938 "#")))
2939 (if (file-writable-p f)
2940 ;; the file is writable, so use it
2941 f
2942 ;; the file isn't writable, so use the format
2943 ;; ~/#&<file name>&<hash of directory>#
2944 (concat (getenv "HOME")
2945 "/#&"
2946 (file-name-nondirectory buffer-file-name)
2947 "&"
2948 (hash-string-into-string
2949 (file-name-directory buffer-file-name))
2950 "#")))
2951 ;; if buffer has no file, use the format ~/#%<buffer name>%<process id>#
2952 (expand-file-name (concat (getenv "HOME")
2953 "/#%"
2954 ;; quote / into \! and \ into \\
2955 (unslashify-name (buffer-name))
2956 "%"
2957 (make-temp-name "")
2958 "#"))))
2959
2960 ;; Hash a string into five characters more-or-less suitable for use in a file
2961 ;; name. (Allowed characters are ! through ~, except /.)
2962 (defun hash-string-into-string (s)
2963 (let ((bins (vector 0 0 0 0 0))
2964 (i 0))
2965 (while (< i (length s))
2966 (aset bins (% i 5) (% (+ (* (aref bins (% i 5)) 35)
2967 (aref s i))
2968 65536))
2969 (setq i (1+ i)))
2970 (mapconcat (function (lambda (b)
2971 (setq b (+ (% b 93) ?!))
2972 (if (>= b ?/)
2973 (setq b (1+ b)))
2974 (char-to-string b)))
2975 bins "")))
2976
2977 ;; Quote any /s in a string by replacing them with \!.
2978 ;; Also, replace any \s by \\, to make it one-to-one.
2979 (defun unslashify-name (s)
2980 (let ((limit 0))
2981 (while (string-match "[/\\]" s limit)
2982 (setq s (concat (substring s 0 (match-beginning 0))
2983 (if (string= (substring s (match-beginning 0)
2984 (match-end 0))
2985 "/")
2986 "\\!"
2987 "\\\\")
2988 (substring s (match-end 0))))
2989 (setq limit (1+ (match-end 0)))))
2990 s)
2991
2992 (provide 'emerge)
2993
2994 ;;; emerge.el ends here