*** empty log message ***
[emacs.git] / lisp / emerge.el
blob126b99c3aa2921b57d1a245206ec83f62ef2f966
1 ;;; emerge.el --- merge diffs under Emacs control
2 ;;; The author has placed this file in the public domain.
4 ;; Author: Dale R. Worley <drw@math.mit.edu>
5 ;; Version: 4
6 ;; Keywords: unix, tools
8 ;;; Commentary:
10 ; - Changes from version 3 to version 4
12 ; More configuration variables are marked as user options.
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.
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.
22 ; Added x f command to show what files or buffers are being operated on.
24 ; The merge buffer now starts read-only, which being in fast mode it
25 ; should be.
27 ; When merging buffers, Emerge writes their contents into temporary
28 ; files in the directory $TMPDIR (if it is defined), or /tmp by default.
30 ; Added x j command to join two differences.
32 ; Added x s command to split a difference into two differences.
34 ; Added emerge-version variable and function to report the version of Emerge
35 ; being run.
37 ; Added x t command to trim unchanged lines off top and bottom of
38 ; difference region.
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.
43 ; Emerge no longer tries to copy the minor modes from the A buffer to
44 ; the merge buffer, only the major mode.
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.
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.
60 ; If the diff/diff3 program reports errors, the user is notified and the
61 ; errors are displayed.
63 ; The command "0j" can be used to suppress the flags from showing in the buffers.
65 ; A discussion of the effect of the merge flags on indentation of code
66 ; has been added to the documentation.
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.
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.
75 ; Replaced calls of "ding" to calls of "error" where appropriate.
77 ; Added x m command to allow major mode of merge buffer to be changed.
79 ; Added x 1 command to shrink the merge window to one line.
81 ; Added emerge-startup-hook to allow customization.
83 ; Fixed a bug that is activated when a remote merge request is made when
84 ; the minibuffer window is selected.
86 ; - Changes from version 2 to version 3
88 ; The directory into which temporary files are written is now controlled
89 ; by a user option (emerge-temp-file-prefix).
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.
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.
97 ; The difference region of the merge buffer can be surrounded by the
98 ; point and mark with the "m" command.
100 ; The three windows can be scrolled together with the "^", "v", "<",
101 ; ">", and "|" commands.
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.
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.
114 ; The "i a" and "i b" commands now work in fast mode.
116 ; The modifications that Emerge makes to save-buffer and write-file are
117 ; described.
119 ; Emerge now handles merging narrowed buffers correctly.
121 ; Emerge now isn't fooled when the buffer visiting a file is not the
122 ; same as the file on disk.
124 ; - Starting
126 ; To start Emerge, you must run one of four commands:
128 ; emerge-files
129 ; emerge-files-with-ancestor
130 ; emerge-buffers
131 ; emerge-buffers-with-ancestor
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.
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".
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.)
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.)
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.
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.
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.
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.)
200 ; After the merge has been set up, Emerge runs the hooks in
201 ; emerge-startup-hook.
203 ; - Merging
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.
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.
226 ; The Emerge commands are:
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*
281 ; * - more details on these commands are given below
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).
287 ; - Differences and their states
289 ; A difference can have one of seven states:
291 ; A: the difference is showing the A version.
293 ; B: the difference is showing the B version.
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.
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.
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.
312 ; The state of the currently selected difference is shown in the mode
313 ; line of the merge window:
315 ; state display
317 ; A A
318 ; B B
319 ; prefer-A A*
320 ; prefer-B B*
321 ; combined comb
323 ; - Select default commands (d a and d b)
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.)
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.
339 ; - Exiting (q)
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.
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.
354 ; If Emerge was started by some other process, success/failure is
355 ; reported to the caller.
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).
364 ; - Auto-advance mode (s a)
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.
372 ; - Skip-prefers mode (s s)
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.
381 ; - Recenter (l)
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.
389 ; - Scrolling the text (^, v, <, >, and |)
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".)
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.
408 ; - Finding the difference at or near a location (x d, x a, and x b)
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.
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.
424 ; - Combining the two versions (x c, x C, and x x)
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:
430 ; #ifdef NEW
431 ; ...new version of code...
432 ; #else /* NEW */
433 ; ...old version of code...
434 ; #endif /* NEW */
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:
443 ; %a the A version of the difference
444 ; %b the B version of the difference
445 ; %% the character '%'
447 ; Thus, the template used above is
449 ; #ifdef NEW\n%b#else /* NEW */\n%a#endif /* NEW */\n
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.
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.
467 ; - Changing the major mode of the edit buffer (x m)
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.
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.
479 ; Beware that "x m" accepts any command name, not just
480 ; major-mode-setting commands.
482 ; - Writing the merge buffer manually
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").
493 ; - Running Emerge standalone
495 ; If you invoke emacs with the following arguments, you can execute
496 ; Emerge as a standalone program:
498 ; emacs -l emerge -f emerge-files-command file-a file-b file-out
500 ; emacs -l emerge -f emerge-files-with-ancestor-command
501 ; file-a file-b file-ancestor file-out
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).
508 ; - Invoking Emerge remotely
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:
514 ; (emerge-files-remote "file A" "file B" "output file")
516 ; (emerge-files-with-ancestor-remote "file A" "file B"
517 ; "ancestor file" "output file")
519 ; Returning a successful/unsuccessful return code is not yet supported
520 ; by the Emacs client/server code.
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.
527 ; - Effect of merge flags on indenting code
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.
537 ; - Autoloading
539 ; The following autoloads will make all top-level Emerge files
540 ; autoloading. Make sure that "emerge" is in a directory on load-path.
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")
559 ; ================================================================
561 ;;; Code:
563 ;;; Macros
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)))))
575 (defmacro emerge-defvar-local (var value doc)
576 "Defines SYMBOL as an advertised variable. Performs a defvar, then
577 executes make-variable-buffer-local on the variable. Also sets the
578 'preserved' property, so that kill-all-local-variables (called by major-mode
579 setting commands) won't destroy Emerge control variables."
580 (` (progn
581 (defvar (, var) (, value) (, doc))
582 (make-variable-buffer-local '(, var))
583 (put '(, var) 'preserved t))))
585 ;; Add entries to minor-mode-alist so that emerge modes show correctly
586 (setq emerge-minor-modes-list '((emerge-mode " Emerge")
587 (emerge-fast-mode " F")
588 (emerge-edit-mode " E")
589 (emerge-auto-advance " A")
590 (emerge-skip-prefers " S")))
591 (if (not (assq 'emerge-mode minor-mode-alist))
592 (setq minor-mode-alist (append emerge-minor-modes-list
593 minor-mode-alist)))
595 ;; We need to define this function so describe-mode can describe Emerge mode.
596 (defun emerge-mode ()
597 "Emerge mode is used by the Emerge file-merging package. It is entered only
598 through one of the functions:
599 emerge-files
600 emerge-files-with-ancestor
601 emerge-buffers
602 emerge-buffers-with-ancestor
603 emerge-files-command
604 emerge-files-with-ancestor-command
605 emerge-files-remote
606 emerge-files-with-ancestor-remote
608 Commands:
609 \\{emerge-basic-keymap}
610 Commands must be prefixed by \\<emerge-fast-keymap>\\[emerge-basic-keymap] in 'edit' mode, but can be invoked directly
611 in 'fast' mode.")
613 (defvar emerge-version "4"
614 "The version of Emerge.")
616 (defun emerge-version ()
617 "Return string describing the version of Emerge. When called interactively,
618 displays the version."
619 (interactive)
620 (if (interactive-p)
621 (message "Emerge version %s" (emerge-version))
622 emerge-version))
624 ;;; Emerge configuration variables
626 ;; Commands that produce difference files
627 ;; All that can be configured is the name of the programs to execute
628 ;; (emerge-diff-program and emerge-diff3-program) and the options
629 ;; to be provided (emerge-diff-options). The order in which the file names
630 ;; are given is fixed.
631 ;; The file names are always expanded (see expand-file-name) before being
632 ;; passed to diff, thus they need not be invoked under a shell that
633 ;; understands '~'.
634 ;; The code which processes the diff/diff3 output depends on all the
635 ;; finicky details of their output, including the somewhat strange
636 ;; way they number lines of a file.
637 (defvar emerge-diff-program "diff"
638 "*Name of the program which compares two files.")
639 (defvar emerge-diff3-program "diff3"
640 "*Name of the program which compares an ancestor file (first argument)
641 and two variant files (second and third arguments).")
642 (defvar emerge-diff-options ""
643 "*Options to be passed to emerge-diff/diff3-program.")
644 (defvar emerge-match-diff-line (let ((x "\\([0-9]+\\)\\(\\|,\\([0-9]+\\)\\)"))
645 (concat "^" x "\\([acd]\\)" x "$"))
646 "*Pattern to match lines produced by diff that describe differences (as
647 opposed to lines from the source files).")
648 (defvar emerge-diff-ok-lines
649 "^\\([0-9,]+[acd][0-9,]+$\\|[<>] \\|---\\)"
650 "*Regexp that matches normal output lines from emerge-diff-program .
651 Lines that do not match are assumed to be error output.")
652 (defvar emerge-diff3-ok-lines
653 "^\\([1-3]:\\|====\\| \\)"
654 "*Regexp that matches normal output lines from emerge-diff3-program .
655 Lines that do not match are assumed to be error output.")
657 ;; The flags used to mark differences in the buffers.
659 ;; These function definitions need to be up here, because they are used
660 ;; during loading.
661 (defun emerge-new-flags ()
662 "Function to be called after emerge-{before,after}-flag are changed to
663 compute values that depend on the flags."
664 (setq emerge-before-flag-length (length emerge-before-flag))
665 (setq emerge-before-flag-lines
666 (count-matches-string emerge-before-flag "\n"))
667 (setq emerge-before-flag-match (regexp-quote emerge-before-flag))
668 (setq emerge-after-flag-length (length emerge-after-flag))
669 (setq emerge-after-flag-lines
670 (count-matches-string emerge-after-flag "\n"))
671 (setq emerge-after-flag-match (regexp-quote emerge-after-flag)))
672 (defun count-matches-string (string regexp)
673 "Return the number of matches in STRING for REGEXP."
674 (let ((i 0)
675 (count 0))
676 (while (string-match regexp string i)
677 (setq count (1+ count))
678 (setq i (match-end 0)))
679 count))
681 (defvar emerge-before-flag "vvvvvvvvvvvvvvvvvvvv\n"
682 "*Flag placed above the highlighted block of code. Must end with newline.
683 Must be set before Emerge is loaded, or emerge-new-flags must be run
684 after setting.")
685 (defvar emerge-after-flag "^^^^^^^^^^^^^^^^^^^^\n"
686 "*Flag placed below the highlighted block of code. Must end with newline.
687 Must be set before Emerge is loaded, or emerge-new-flags must be run
688 after setting.")
690 ;; Calculate dependent variables
691 (emerge-new-flags)
693 (defvar emerge-min-visible-lines 3
694 "*Number of lines that we want to show above and below the flags when we are
695 displaying a difference.")
697 (defvar emerge-temp-file-prefix
698 (let ((env (getenv "TMPDIR"))
700 (setq d (if (and env (> (length env) 0))
702 "/tmp"))
703 (if (= (aref d (1- (length d))) ?/)
704 (setq d (substring d 0 -1)))
705 (concat d "/emerge"))
706 "*Prefix to put on Emerge temporary file names.
707 Do not start with '~/' or '~user-name/'.")
709 (defvar emerge-temp-file-mode 384 ; u=rw only
710 "*Mode for Emerge temporary files.")
712 (defvar emerge-combine-versions-template
713 "#ifdef NEW\n%b#else /* NEW */\n%a#endif /* NEW */\n"
714 "*Template for emerge-combine-versions to combine the two versions.
715 The template is inserted as a string, with the following interpolations:
716 %a the A version of the difference
717 %b the B version of the difference
718 %% the character '%'
719 Don't forget to end the template with a newline.
720 Note that this variable can be made local to a particular merge buffer by
721 giving a prefix argument to emerge-set-combine-versions-template .")
723 ;; Build keymaps
725 (defvar emerge-basic-keymap nil
726 "Keymap of Emerge commands.
727 Directly available in 'fast' mode;
728 must be prefixed by \\<emerge-fast-keymap>\\[emerge-basic-keymap] in 'edit' mode.")
730 (defvar emerge-fast-keymap nil
731 "Local keymap used in Emerge 'fast' mode.
732 Makes Emerge commands directly available.")
734 (defvar emerge-command-prefix "\C-c"
735 "*Command prefix for Emerge commands in 'edit' mode.
736 Must be set before Emerge is loaded.")
738 ;; This function sets up the fixed keymaps. It is executed when the first
739 ;; Emerge is done to allow the user maximum time to set up the global keymap.
740 (defun emerge-setup-fixed-keymaps ()
741 ;; Set up the basic keymap
742 (setq emerge-basic-keymap (make-keymap))
743 (suppress-keymap emerge-basic-keymap) ; this sets 0..9 to digit-argument and
744 ; - to negative-argument
745 (define-key emerge-basic-keymap "p" 'emerge-previous-difference)
746 (define-key emerge-basic-keymap "n" 'emerge-next-difference)
747 (define-key emerge-basic-keymap "a" 'emerge-select-A)
748 (define-key emerge-basic-keymap "b" 'emerge-select-B)
749 (define-key emerge-basic-keymap "j" 'emerge-jump-to-difference)
750 (define-key emerge-basic-keymap "q" 'emerge-quit)
751 (define-key emerge-basic-keymap "f" 'emerge-fast-mode)
752 (define-key emerge-basic-keymap "e" 'emerge-edit-mode)
753 (define-key emerge-basic-keymap "s" nil)
754 (define-key emerge-basic-keymap "sa" 'emerge-auto-advance)
755 (define-key emerge-basic-keymap "ss" 'emerge-skip-prefers)
756 (define-key emerge-basic-keymap "l" 'emerge-recenter)
757 (define-key emerge-basic-keymap "d" nil)
758 (define-key emerge-basic-keymap "da" 'emerge-default-A)
759 (define-key emerge-basic-keymap "db" 'emerge-default-B)
760 (define-key emerge-basic-keymap "c" nil)
761 (define-key emerge-basic-keymap "ca" 'emerge-copy-as-kill-A)
762 (define-key emerge-basic-keymap "cb" 'emerge-copy-as-kill-B)
763 (define-key emerge-basic-keymap "i" nil)
764 (define-key emerge-basic-keymap "ia" 'emerge-insert-A)
765 (define-key emerge-basic-keymap "ib" 'emerge-insert-B)
766 (define-key emerge-basic-keymap "m" 'emerge-mark-difference)
767 (define-key emerge-basic-keymap "v" 'emerge-scroll-up)
768 (define-key emerge-basic-keymap "^" 'emerge-scroll-down)
769 (define-key emerge-basic-keymap "<" 'emerge-scroll-left)
770 (define-key emerge-basic-keymap ">" 'emerge-scroll-right)
771 (define-key emerge-basic-keymap "|" 'emerge-scroll-reset)
772 (define-key emerge-basic-keymap "x" nil)
773 (define-key emerge-basic-keymap "x1" 'emerge-one-line-window)
774 (define-key emerge-basic-keymap "xa" 'emerge-find-difference-A)
775 (define-key emerge-basic-keymap "xb" 'emerge-find-difference-B)
776 (define-key emerge-basic-keymap "xc" 'emerge-combine-versions)
777 (define-key emerge-basic-keymap "xC" 'emerge-combine-versions-register)
778 (define-key emerge-basic-keymap "xd" 'emerge-find-difference)
779 (define-key emerge-basic-keymap "xf" 'emerge-file-names)
780 (define-key emerge-basic-keymap "xj" 'emerge-join-differences)
781 (define-key emerge-basic-keymap "xl" 'emerge-line-numbers)
782 (define-key emerge-basic-keymap "xm" 'emerge-set-merge-mode)
783 (define-key emerge-basic-keymap "xs" 'emerge-split-difference)
784 (define-key emerge-basic-keymap "xt" 'emerge-trim-difference)
785 (define-key emerge-basic-keymap "xx" 'emerge-set-combine-versions-template)
786 ;; Allow emerge-basic-keymap to be referenced indirectly
787 (fset 'emerge-basic-keymap emerge-basic-keymap)
788 ;; Set up the fast mode keymap
789 (setq emerge-fast-keymap (copy-keymap emerge-basic-keymap))
790 ;; Allow prefixed commands to work in fast mode
791 (define-key emerge-fast-keymap emerge-command-prefix 'emerge-basic-keymap)
792 ;; Allow emerge-fast-keymap to be referenced indirectly
793 (fset 'emerge-fast-keymap emerge-fast-keymap)
794 ;; Suppress write-file and save-buffer
795 (emerge-shadow-key-definition 'write-file 'emerge-query-write-file
796 (current-global-map) emerge-fast-keymap)
797 (emerge-shadow-key-definition 'save-buffer 'emerge-query-save-buffer
798 (current-global-map) emerge-fast-keymap))
800 ;; Variables which control each merge. They are local to the merge buffer.
802 ;; Mode variables
803 (emerge-defvar-local emerge-mode nil
804 "Indicator for emerge-mode.")
805 (emerge-defvar-local emerge-fast-mode nil
806 "Indicator for emerge-mode fast submode.")
807 (emerge-defvar-local emerge-edit-mode nil
808 "Indicator for emerge-mode edit submode.")
809 (emerge-defvar-local emerge-A-buffer nil
810 "The buffer in which the A variant is stored.")
811 (emerge-defvar-local emerge-B-buffer nil
812 "The buffer in which the B variant is stored.")
813 (emerge-defvar-local emerge-merge-buffer nil
814 "The buffer in which the merged file is manipulated.")
815 (emerge-defvar-local emerge-ancestor-buffer nil
816 "The buffer in which the ancestor variant is stored,
817 or nil if there is none.")
819 (defconst emerge-saved-variables
820 '((buffer-modified-p set-buffer-modified-p)
821 buffer-read-only
822 buffer-auto-save-file-name)
823 "Variables and properties of a buffer which are saved, modified and restored
824 during a merge.")
825 (defconst emerge-merging-values '(nil t nil)
826 "Values to be assigned to emerge-saved-variables during a merge.")
828 (emerge-defvar-local emerge-A-buffer-values nil
829 "Remembers emerge-saved-variables for emerge-A-buffer.")
830 (emerge-defvar-local emerge-B-buffer-values nil
831 "Remembers emerge-saved-variables for emerge-B-buffer.")
833 (emerge-defvar-local emerge-difference-list nil
834 "Vector of differences between the variants, and markers in the buffers to
835 show where they are. Each difference is represented by a vector of seven
836 elements. The first two are markers to the beginning and end of the difference
837 section in the A buffer, the second two are markers for the B buffer, the third
838 two are markers for the merge buffer, and the last element is the \"state\" of
839 that difference in the merge buffer.
840 A section of a buffer is described by two markers, one to the beginning of
841 the first line of the section, and one to the beginning of the first line
842 after the section. (If the section is empty, both markers point to the same
843 point.) If the section is part of the selected difference, then the markers
844 are moved into the flags, so the user can edit the section without disturbing
845 the markers.
846 The \"states\" are:
847 A the merge buffer currently contains the A variant
848 B the merge buffer currently contains the B variant
849 default-A the merge buffer contains the A variant by default,
850 but this difference hasn't been selected yet, so
851 change-default commands can alter it
852 default-B the merge buffer contains the B variant by default,
853 but this difference hasn't been selected yet, so
854 change-default commands can alter it
855 prefer-A in a three-file merge, the A variant is the prefered
856 choice
857 prefer-B in a three-file merge, the B variant is the prefered
858 choice")
859 (emerge-defvar-local emerge-current-difference -1
860 "The difference that is currently selected.")
861 (emerge-defvar-local emerge-number-of-differences nil
862 "Number of differences found.")
863 (emerge-defvar-local emerge-edit-keymap nil
864 "The local keymap for the merge buffer, with the emerge commands defined in
865 it. Used to save the local keymap during fast mode, when the local keymap is
866 replaced by emerge-fast-keymap.")
867 (emerge-defvar-local emerge-old-keymap nil
868 "The original local keymap for the merge buffer.")
869 (emerge-defvar-local emerge-auto-advance nil
870 "*If non-nil, emerge-select-A and emerge-select-B automatically advance to
871 the next difference.")
872 (emerge-defvar-local emerge-skip-prefers nil
873 "*If non-nil, differences for which there is a preference are automatically
874 skipped.")
875 (emerge-defvar-local emerge-startup-hook nil
876 "*Hooks to run in the merge buffer after the merge has been set up.")
877 (emerge-defvar-local emerge-quit-hook nil
878 "Hooks to run in the merge buffer after the merge has been finished.
879 emerge-prefix-argument will be bound to the prefix argument of the emerge-quit
880 command.
881 This is not a user option, since Emerge uses it for its own processing.")
882 (emerge-defvar-local emerge-output-description nil
883 "Describes output destination of the merge, for the use of
884 emerge-file-names.")
886 ;;; Setup functions for two-file mode.
888 (defun emerge-files-internal (file-A file-B &optional startup-hooks quit-hooks
889 output-file)
890 (let ((buffer-A (find-file-noselect file-A))
891 (buffer-B (find-file-noselect file-B)))
892 ;; Make sure the entire files are seen, and they reflect what is on disk
893 (emerge-eval-in-buffer buffer-A
894 (widen)
895 (emerge-verify-file-buffer))
896 (emerge-eval-in-buffer buffer-B
897 (widen)
898 (emerge-verify-file-buffer))
899 (emerge-setup buffer-A file-A buffer-B file-B startup-hooks quit-hooks
900 output-file)))
902 ;; Start up Emerge on two files
903 (defun emerge-setup (buffer-A file-A buffer-B file-B startup-hooks quit-hooks
904 output-file)
905 (setq file-A (expand-file-name file-A))
906 (setq file-B (expand-file-name file-B))
907 (setq output-file (and output-file (expand-file-name output-file)))
908 (let* ((merge-buffer-name (emerge-unique-buffer-name "*merge" "*"))
909 ;; create the merge buffer from buffer A, so it inherits buffer A's
910 ;; default directory, etc.
911 (merge-buffer (emerge-eval-in-buffer
912 buffer-A
913 (get-buffer-create merge-buffer-name))))
914 (emerge-eval-in-buffer
915 merge-buffer
916 (emerge-copy-modes buffer-A)
917 (setq buffer-read-only nil)
918 (auto-save-mode 1)
919 (setq emerge-mode t)
920 (setq emerge-A-buffer buffer-A)
921 (setq emerge-B-buffer buffer-B)
922 (setq emerge-ancestor-buffer nil)
923 (setq emerge-merge-buffer merge-buffer)
924 (setq emerge-output-description
925 (if output-file
926 (concat "Output to file: " output-file)
927 (concat "Output to buffer: " (buffer-name merge-buffer))))
928 (insert-buffer emerge-A-buffer)
929 (emerge-set-keys)
930 (setq emerge-difference-list (emerge-make-diff-list file-A file-B))
931 (setq emerge-number-of-differences (length emerge-difference-list))
932 (setq emerge-current-difference -1)
933 (setq emerge-quit-hooks quit-hooks)
934 (emerge-remember-buffer-characteristics))
935 (emerge-setup-windows buffer-A buffer-B merge-buffer t)
936 (emerge-eval-in-buffer merge-buffer
937 (run-hooks 'startup-hooks 'emerge-startup-hook)
938 (setq buffer-read-only t))))
940 ;; Generate the Emerge difference list between two files
941 (defun emerge-make-diff-list (file-A file-B)
942 (setq emerge-diff-buffer (get-buffer-create "*emerge-diff*"))
943 (emerge-eval-in-buffer
944 emerge-diff-buffer
945 (erase-buffer)
946 (shell-command
947 (format "%s %s %s %s"
948 emerge-diff-program emerge-diff-options file-A file-B)
950 (emerge-prepare-error-list emerge-diff-ok-lines)
951 (emerge-convert-diffs-to-markers
952 emerge-A-buffer emerge-B-buffer emerge-merge-buffer
953 (emerge-extract-diffs emerge-diff-buffer)))
955 (defun emerge-extract-diffs (diff-buffer)
956 (let (list)
957 (emerge-eval-in-buffer
958 diff-buffer
959 (goto-char (point-min))
960 (while (re-search-forward emerge-match-diff-line nil t)
961 (let* ((a-begin (string-to-int (buffer-substring (match-beginning 1)
962 (match-end 1))))
963 (a-end (let ((b (match-beginning 3))
964 (e (match-end 3)))
965 (if b
966 (string-to-int (buffer-substring b e))
967 a-begin)))
968 (diff-type (buffer-substring (match-beginning 4) (match-end 4)))
969 (b-begin (string-to-int (buffer-substring (match-beginning 5)
970 (match-end 5))))
971 (b-end (let ((b (match-beginning 7))
972 (e (match-end 7)))
973 (if b
974 (string-to-int (buffer-substring b e))
975 b-begin))))
976 ;; fix the beginning and end numbers, because diff is somewhat
977 ;; strange about how it numbers lines
978 (if (string-equal diff-type "a")
979 (progn
980 (setq b-end (1+ b-end))
981 (setq a-begin (1+ a-begin))
982 (setq a-end a-begin))
983 (if (string-equal diff-type "d")
984 (progn
985 (setq a-end (1+ a-end))
986 (setq b-begin (1+ b-begin))
987 (setq b-end b-begin))
988 ;; (string-equal diff-type "c")
989 (progn
990 (setq a-end (1+ a-end))
991 (setq b-end (1+ b-end)))))
992 (setq list (cons (vector a-begin a-end
993 b-begin b-end
994 'default-A)
995 list)))))
996 (nreverse list)))
998 ;; Set up buffer of diff/diff3 error messages.
999 (defun emerge-prepare-error-list (ok-regexp)
1000 (setq emerge-diff-error-buffer (get-buffer-create "*emerge-diff-errors*"))
1001 (emerge-eval-in-buffer
1002 emerge-diff-error-buffer
1003 (erase-buffer)
1004 (insert-buffer emerge-diff-buffer)
1005 (delete-matching-lines ok-regexp)))
1007 ;;; Top-level and setup functions for three-file mode.
1009 (defun emerge-files-with-ancestor-internal (file-A file-B file-ancestor
1010 &optional startup-hooks quit-hooks
1011 output-file)
1012 (let ((buffer-A (find-file-noselect file-A))
1013 (buffer-B (find-file-noselect file-B))
1014 (buffer-ancestor (find-file-noselect file-ancestor)))
1015 ;; Make sure the entire files are seen, and they reflect what is on disk
1016 (emerge-eval-in-buffer buffer-A
1017 (widen)
1018 (emerge-verify-file-buffer))
1019 (emerge-eval-in-buffer buffer-B
1020 (widen)
1021 (emerge-verify-file-buffer))
1022 (emerge-eval-in-buffer buffer-ancestor
1023 (widen)
1024 (emerge-verify-file-buffer))
1025 (emerge-setup-with-ancestor buffer-A file-A buffer-B file-B
1026 buffer-ancestor file-ancestor
1027 startup-hooks quit-hooks output-file)))
1029 ;; Start up Emerge on two files with an ancestor
1030 (defun emerge-setup-with-ancestor (buffer-A file-A buffer-B file-B
1031 buffer-ancestor file-ancestor
1032 &optional startup-hooks quit-hooks
1033 output-file)
1034 (setq file-A (expand-file-name file-A))
1035 (setq file-B (expand-file-name file-B))
1036 (setq file-ancestor (expand-file-name file-ancestor))
1037 (setq output-file (and output-file (expand-file-name output-file)))
1038 (let* ((merge-buffer-name (emerge-unique-buffer-name "*merge" "*"))
1039 ;; create the merge buffer from buffer A, so it inherits buffer A's
1040 ;; default directory, etc.
1041 (merge-buffer (emerge-eval-in-buffer
1042 buffer-A
1043 (get-buffer-create merge-buffer-name))))
1044 (emerge-eval-in-buffer
1045 merge-buffer
1046 (emerge-copy-modes buffer-A)
1047 (setq buffer-read-only nil)
1048 (auto-save-mode 1)
1049 (setq emerge-mode t)
1050 (setq emerge-A-buffer buffer-A)
1051 (setq emerge-B-buffer buffer-B)
1052 (setq emerge-ancestor-buffer buffer-ancestor)
1053 (setq emerge-merge-buffer merge-buffer)
1054 (setq emerge-output-description
1055 (if output-file
1056 (concat "Output to file: " output-file)
1057 (concat "Output to buffer: " (buffer-name merge-buffer))))
1058 (insert-buffer emerge-A-buffer)
1059 (emerge-set-keys)
1060 (setq emerge-difference-list
1061 (emerge-make-diff3-list file-A file-B file-ancestor))
1062 (setq emerge-number-of-differences (length emerge-difference-list))
1063 (setq emerge-current-difference -1)
1064 (setq emerge-quit-hook quit-hooks)
1065 (emerge-remember-buffer-characteristics)
1066 (emerge-select-prefer-Bs))
1067 (emerge-setup-windows buffer-A buffer-B merge-buffer t)
1068 (emerge-eval-in-buffer merge-buffer
1069 (run-hooks 'startup-hooks 'emerge-startup-hook)
1070 (setq buffer-read-only t))))
1072 ;; Generate the Emerge difference list between two files with an ancestor
1073 (defun emerge-make-diff3-list (file-A file-B file-ancestor)
1074 (setq emerge-diff-buffer (get-buffer-create "*emerge-diff*"))
1075 (emerge-eval-in-buffer
1076 emerge-diff-buffer
1077 (erase-buffer)
1078 (shell-command
1079 (format "%s %s %s %s %s"
1080 emerge-diff3-program emerge-diff-options
1081 file-ancestor file-A file-B)
1083 (emerge-prepare-error-list emerge-diff3-ok-lines)
1084 (emerge-convert-diffs-to-markers
1085 emerge-A-buffer emerge-B-buffer emerge-merge-buffer
1086 (emerge-extract-diffs3 emerge-diff-buffer)))
1088 (defun emerge-extract-diffs3 (diff-buffer)
1089 (let (list)
1090 (emerge-eval-in-buffer
1091 diff-buffer
1092 (while (re-search-forward "^====\\(.?\\)$" nil t)
1093 ;; leave point after matched line
1094 (beginning-of-line 2)
1095 (let ((agreement (buffer-substring (match-beginning 1) (match-end 1))))
1096 ;; if the A and B files are the same, ignore the difference
1097 (if (not (string-equal agreement "1"))
1098 (setq list
1099 (cons
1100 (let ((group-2 (emerge-get-diff3-group "2"))
1101 (group-3 (emerge-get-diff3-group "3")))
1102 (vector (car group-2) (car (cdr group-2))
1103 (car group-3) (car (cdr group-3))
1104 (cond ((string-equal agreement "2") 'prefer-A)
1105 ((string-equal agreement "3") 'prefer-B)
1106 (t 'default-A))))
1107 list))))))
1108 (nreverse list)))
1110 (defun emerge-get-diff3-group (file)
1111 ;; This save-excursion allows emerge-get-diff3-group to be called for the
1112 ;; various groups of lines (1, 2, 3) in any order, and for the lines to
1113 ;; appear in any order. The reason this is necessary is that Gnu diff3
1114 ;; can produce the groups in the order 1, 2, 3 or 1, 3, 2.
1115 (save-excursion
1116 (re-search-forward
1117 (concat "^" file ":\\([0-9]+\\)\\(,\\([0-9]+\\)\\)?\\([ac]\\)$"))
1118 (beginning-of-line 2)
1119 ;; treatment depends on whether it is an "a" group or a "c" group
1120 (if (string-equal (buffer-substring (match-beginning 4) (match-end 4)) "c")
1121 ;; it is a "c" group
1122 (if (match-beginning 2)
1123 ;; it has two numbers
1124 (list (string-to-int
1125 (buffer-substring (match-beginning 1) (match-end 1)))
1126 (1+ (string-to-int
1127 (buffer-substring (match-beginning 3) (match-end 3)))))
1128 ;; it has one number
1129 (let ((x (string-to-int
1130 (buffer-substring (match-beginning 1) (match-end 1)))))
1131 (list x (1+ x))))
1132 ;; it is an "a" group
1133 (let ((x (1+ (string-to-int
1134 (buffer-substring (match-beginning 1) (match-end 1))))))
1135 (list x x)))))
1137 ;;; Functions to start Emerge on files
1139 ;;;###autoload
1140 (defun emerge-files (arg file-A file-B file-out &optional startup-hooks
1141 quit-hooks)
1142 "Run Emerge on two files."
1143 (interactive
1144 (let (f)
1145 (list current-prefix-arg
1146 (setq f (read-file-name "File A to merge: " nil nil 'confirm))
1147 (read-file-name "File B to merge: " nil nil 'confirm)
1148 (and current-prefix-arg
1149 (read-file-name
1150 (format "Output file: (default %s) " f)
1151 nil f nil)))))
1152 (emerge-files-internal
1153 file-A file-B startup-hooks
1154 (if arg
1155 (cons (` (lambda () (emerge-files-exit (, file-out))))
1156 quit-hooks)
1157 quit-hooks)
1158 file-out))
1160 ;;;###autoload
1161 (defun emerge-files-with-ancestor (arg file-A file-B file-ancestor file-out
1162 &optional startup-hooks quit-hooks)
1163 "Run Emerge on two files, giving another file as the ancestor."
1164 (interactive
1165 (let (f)
1166 (list current-prefix-arg
1167 (setq f (read-file-name "File A to merge: " nil nil 'confirm))
1168 (read-file-name "File B to merge: " nil nil 'confirm)
1169 (read-file-name "Ancestor file: " nil nil 'confirm)
1170 (and current-prefix-arg
1171 (read-file-name
1172 (format "Output file: (default %s) " f)
1173 nil f nil)))))
1174 (emerge-files-with-ancestor-internal
1175 file-A file-B file-ancestor startup-hooks
1176 (if arg
1177 (cons (` (lambda () (emerge-files-exit (, file-out))))
1178 quit-hooks)
1179 quit-hooks)
1180 file-out))
1182 ;; Write the merge buffer out in place of the file the A buffer is visiting.
1183 (defun emerge-files-exit (file-out)
1184 ;; if merge was successful was given, save to disk
1185 (if (not emerge-prefix-argument)
1186 (emerge-write-and-delete file-out)))
1188 ;;; Functions to start Emerge on buffers
1190 ;;;###autoload
1191 (defun emerge-buffers (buffer-A buffer-B &optional startup-hooks quit-hooks)
1192 "Run Emerge on two buffers."
1193 (interactive "bBuffer A to merge: \nbBuffer B to merge: ")
1194 (let ((emerge-file-A (emerge-make-temp-file "A"))
1195 (emerge-file-B (emerge-make-temp-file "B")))
1196 (emerge-eval-in-buffer
1197 buffer-A
1198 (write-region (point-min) (point-max) emerge-file-A nil 'no-message))
1199 (emerge-eval-in-buffer
1200 buffer-B
1201 (write-region (point-min) (point-max) emerge-file-B nil 'no-message))
1202 (emerge-setup (get-buffer buffer-A) emerge-file-A
1203 (get-buffer buffer-B) emerge-file-B
1204 (cons (function (lambda ()
1205 (delete-file emerge-file-A)
1206 (delete-file emerge-file-B)))
1207 startup-hooks)
1208 quit-hooks
1209 nil)))
1211 ;;;###autoload
1212 (defun emerge-buffers-with-ancestor (buffer-A buffer-B buffer-ancestor
1213 &optional startup-hooks
1214 quit-hooks)
1215 "Run Emerge on two buffers, giving another buffer as the ancestor."
1216 (interactive
1217 "bBuffer A to merge: \nbBuffer B to merge: \nbAncestor buffer: ")
1218 (let ((emerge-file-A (emerge-make-temp-file "A"))
1219 (emerge-file-B (emerge-make-temp-file "B"))
1220 (emerge-file-ancestor (emerge-make-temp-file "anc")))
1221 (emerge-eval-in-buffer
1222 buffer-A
1223 (write-region (point-min) (point-max) emerge-file-A nil 'no-message))
1224 (emerge-eval-in-buffer
1225 buffer-B
1226 (write-region (point-min) (point-max) emerge-file-B nil 'no-message))
1227 (emerge-eval-in-buffer
1228 buffer-ancestor
1229 (write-region (point-min) (point-max) emerge-file-ancestor nil
1230 'no-message))
1231 (emerge-setup-with-ancestor (get-buffer buffer-A) emerge-file-A
1232 (get-buffer buffer-B) emerge-file-B
1233 (get-buffer buffer-ancestor)
1234 emerge-file-ancestor
1235 (cons (function (lambda ()
1236 (delete-file emerge-file-A)
1237 (delete-file emerge-file-B)
1238 (delete-file
1239 emerge-file-ancestor)))
1240 startup-hooks)
1241 quit-hooks
1242 nil)))
1244 ;;; Functions to start Emerge from the command line
1246 ;;;###autoload
1247 (defun emerge-files-command ()
1248 (let ((file-a (nth 0 command-line-args-left))
1249 (file-b (nth 1 command-line-args-left))
1250 (file-out (nth 2 command-line-args-left)))
1251 (setq command-line-args-left (nthcdr 3 command-line-args-left))
1252 (emerge-files-internal
1253 file-a file-b nil
1254 (list (` (lambda () (emerge-command-exit (, file-out))))))))
1256 ;;;###autoload
1257 (defun emerge-files-with-ancestor-command ()
1258 (let (file-a file-b file-anc file-out)
1259 ;; check for a -a flag, for filemerge compatibility
1260 (if (string= (car command-line-args-left) "-a")
1261 ;; arguments are "-a ancestor file-a file-b file-out"
1262 (progn
1263 (setq file-a (nth 2 command-line-args-left))
1264 (setq file-b (nth 3 command-line-args-left))
1265 (setq file-anc (nth 1 command-line-args-left))
1266 (setq file-out (nth 4 command-line-args-left))
1267 (setq command-line-args-left (nthcdr 5 command-line-args-left)))
1268 ;; arguments are "file-a file-b ancestor file-out"
1269 (setq file-a (nth 0 command-line-args-left))
1270 (setq file-b (nth 1 command-line-args-left))
1271 (setq file-anc (nth 2 command-line-args-left))
1272 (setq file-out (nth 3 command-line-args-left))
1273 (setq command-line-args-left (nthcdr 4 command-line-args-left)))
1274 (emerge-files-with-ancestor-internal
1275 file-a file-b file-anc nil
1276 (list (` (lambda () (emerge-command-exit (, file-out))))))))
1278 (defun emerge-command-exit (file-out)
1279 (emerge-write-and-delete file-out)
1280 (kill-emacs (if emerge-prefix-argument 1 0)))
1282 ;;; Functions to start Emerge via remote request
1284 ;;;###autoload
1285 (defun emerge-files-remote (file-a file-b file-out)
1286 (setq emerge-file-out file-out)
1287 (emerge-files-internal
1288 file-a file-b nil
1289 (list (` (lambda () (emerge-remote-exit (, file-out) '(, exit-func)))))
1290 file-out)
1291 (throw 'client-wait nil))
1293 ;;;###autoload
1294 (defun emerge-files-with-ancestor-remote (file-a file-b file-anc file-out)
1295 (setq emerge-file-out file-out)
1296 (emerge-files-with-ancestor-internal
1297 file-a file-b file-anc nil
1298 (list (` (lambda () (emerge-remote-exit (, file-out) '(, exit-func)))))
1299 file-out)
1300 (throw 'client-wait nil))
1302 (defun emerge-remote-exit (file-out exit-func)
1303 (emerge-write-and-delete file-out)
1304 (kill-buffer emerge-merge-buffer)
1305 (funcall exit-func (if emerge-prefix-argument 1 0)))
1307 ;;; Common setup routines
1309 ;; Set up the window configuration. If POS is given, set the points to
1310 ;; the beginnings of the buffers.
1311 (defun emerge-setup-windows (buffer-A buffer-B merge-buffer &optional pos)
1312 ;; Make sure we are not in the minibuffer window when we try to delete
1313 ;; all other windows.
1314 (if (eq (selected-window) (minibuffer-window))
1315 (other-window 1))
1316 (delete-other-windows)
1317 (switch-to-buffer merge-buffer)
1318 (emerge-refresh-mode-line)
1319 (split-window-vertically)
1320 (split-window-horizontally)
1321 (switch-to-buffer buffer-A)
1322 (if pos
1323 (goto-char (point-min)))
1324 (other-window 1)
1325 (switch-to-buffer buffer-B)
1326 (if pos
1327 (goto-char (point-min)))
1328 (other-window 1)
1329 (if pos
1330 (goto-char (point-min)))
1331 ;; If diff/diff3 reports errors, display them rather than the merge buffer.
1332 (if (/= 0 (emerge-eval-in-buffer emerge-diff-error-buffer (buffer-size)))
1333 (progn
1334 (ding)
1335 (message "Errors found in diff/diff3 output. Merge buffer is %s."
1336 (buffer-name emerge-merge-buffer))
1337 (switch-to-buffer emerge-diff-error-buffer))))
1339 ;; Set up the keymap in the merge buffer
1340 (defun emerge-set-keys ()
1341 ;; Set up fixed keymaps if necessary
1342 (if (not emerge-basic-keymap)
1343 (emerge-setup-fixed-keymaps))
1344 ;; Save the old local map
1345 (setq emerge-old-keymap (current-local-map))
1346 ;; Construct the edit keymap
1347 (setq emerge-edit-keymap (if emerge-old-keymap
1348 (copy-keymap emerge-old-keymap)
1349 (make-sparse-keymap)))
1350 ;; Install the Emerge commands
1351 (emerge-force-define-key emerge-edit-keymap emerge-command-prefix
1352 'emerge-basic-keymap)
1353 ;; Suppress write-file and save-buffer
1354 (emerge-recursively-substitute-key-definition 'write-file
1355 'emerge-query-write-file
1356 emerge-edit-keymap)
1357 (emerge-recursively-substitute-key-definition 'save-buffer
1358 'emerge-query-save-buffer
1359 emerge-edit-keymap)
1360 (emerge-shadow-key-definition 'write-file 'emerge-query-write-file
1361 (current-global-map) emerge-edit-keymap)
1362 (emerge-shadow-key-definition 'save-buffer 'emerge-query-save-buffer
1363 (current-global-map) emerge-edit-keymap)
1364 (use-local-map emerge-fast-keymap)
1365 (setq emerge-edit-mode nil)
1366 (setq emerge-fast-mode t))
1368 (defun emerge-remember-buffer-characteristics ()
1369 "Must be called in the merge buffer. Remembers certain properties of the
1370 buffers being merged (read-only, modified, auto-save), and saves them in
1371 buffer local variables. Sets the buffers read-only and turns off auto-save.
1372 These characteristics are restored by emerge-restore-buffer-characteristics."
1373 ;; force auto-save, because we will turn off auto-saving in buffers for the
1374 ;; duration
1375 (do-auto-save)
1376 ;; remember and alter buffer characteristics
1377 (setq emerge-A-buffer-values
1378 (emerge-eval-in-buffer
1379 emerge-A-buffer
1380 (prog1
1381 (emerge-save-variables emerge-saved-variables)
1382 (emerge-restore-variables emerge-saved-variables
1383 emerge-merging-values))))
1384 (setq emerge-B-buffer-values
1385 (emerge-eval-in-buffer
1386 emerge-B-buffer
1387 (prog1
1388 (emerge-save-variables emerge-saved-variables)
1389 (emerge-restore-variables emerge-saved-variables
1390 emerge-merging-values)))))
1392 (defun emerge-restore-buffer-characteristics ()
1393 "Restores the characteristics remembered by
1394 emerge-remember-buffer-characteristics."
1395 (let ((A-values emerge-A-buffer-values)
1396 (B-values emerge-B-buffer-values))
1397 (emerge-eval-in-buffer emerge-A-buffer
1398 (emerge-restore-variables emerge-saved-variables
1399 A-values))
1400 (emerge-eval-in-buffer emerge-B-buffer
1401 (emerge-restore-variables emerge-saved-variables
1402 B-values))))
1404 (defun emerge-convert-diffs-to-markers (A-buffer
1405 B-buffer
1406 merge-buffer
1407 lineno-list)
1408 (let* (marker-list
1409 (A-point-min (emerge-eval-in-buffer A-buffer (point-min)))
1410 (offset (1- A-point-min))
1411 (A-hidden-lines (emerge-eval-in-buffer
1412 A-buffer
1413 (save-restriction
1414 (widen)
1415 (count-lines 1 A-point-min))))
1416 (B-point-min (emerge-eval-in-buffer B-buffer (point-min)))
1417 (B-hidden-lines (emerge-eval-in-buffer
1418 B-buffer
1419 (save-restriction
1420 (widen)
1421 (count-lines 1 B-point-min)))))
1422 (while lineno-list
1423 (let* ((list-element (car lineno-list))
1424 a-begin-marker
1425 a-end-marker
1426 b-begin-marker
1427 b-end-marker
1428 (a-begin (aref list-element 0))
1429 (a-end (aref list-element 1))
1430 (b-begin (aref list-element 2))
1431 (b-end (aref list-element 3))
1432 (state (aref list-element 4)))
1433 ;; place markers at the appropriate places in the buffers
1434 (emerge-eval-in-buffer
1435 A-buffer
1436 (goto-line (+ a-begin A-hidden-lines))
1437 (setq a-begin-marker (point-marker))
1438 (goto-line (+ a-end A-hidden-lines))
1439 (setq a-end-marker (point-marker)))
1440 (emerge-eval-in-buffer
1441 B-buffer
1442 (goto-line (+ b-begin B-hidden-lines))
1443 (setq b-begin-marker (point-marker))
1444 (goto-line (+ b-end B-hidden-lines))
1445 (setq b-end-marker (point-marker)))
1446 (setq merge-begin-marker (set-marker
1447 (make-marker)
1448 (- (marker-position a-begin-marker)
1449 offset)
1450 merge-buffer))
1451 (setq merge-end-marker (set-marker
1452 (make-marker)
1453 (- (marker-position a-end-marker)
1454 offset)
1455 merge-buffer))
1456 ;; record all the markers for this difference
1457 (setq marker-list (cons (vector a-begin-marker a-end-marker
1458 b-begin-marker b-end-marker
1459 merge-begin-marker merge-end-marker
1460 state)
1461 marker-list)))
1462 (setq lineno-list (cdr lineno-list)))
1463 ;; convert the list of difference information into a vector for
1464 ;; fast access
1465 (setq emerge-difference-list (apply 'vector (nreverse marker-list)))))
1467 ;; If we have an ancestor, select all B variants that we prefer
1468 (defun emerge-select-prefer-Bs ()
1469 (let ((n 0))
1470 (while (< n emerge-number-of-differences)
1471 (if (eq (aref (aref emerge-difference-list n) 6) 'prefer-B)
1472 (progn
1473 (emerge-unselect-and-select-difference n t)
1474 (emerge-select-B)
1475 (aset (aref emerge-difference-list n) 6 'prefer-B)))
1476 (setq n (1+ n))))
1477 (emerge-unselect-and-select-difference -1))
1479 ;;; Common exit routines
1481 (defun emerge-write-and-delete (file-out)
1482 ;; clear screen format
1483 (delete-other-windows)
1484 ;; delete A, B, and ancestor buffers, if they haven't been changed
1485 (if (not (buffer-modified-p emerge-A-buffer))
1486 (kill-buffer emerge-A-buffer))
1487 (if (not (buffer-modified-p emerge-B-buffer))
1488 (kill-buffer emerge-B-buffer))
1489 (if (and emerge-ancestor-buffer
1490 (not (buffer-modified-p emerge-ancestor-buffer)))
1491 (kill-buffer emerge-ancestor-buffer))
1492 ;; Write merge buffer to file
1493 (write-file file-out))
1495 ;;; Commands
1497 (defun emerge-recenter (&optional arg)
1498 "Bring the highlighted region of all three merge buffers into view,
1499 if they are in windows. If an ARGUMENT is given, the default three-window
1500 display is reestablished."
1501 (interactive "P")
1502 ;; If there is an argument, rebuild the window structure
1503 (if arg
1504 (emerge-setup-windows emerge-A-buffer emerge-B-buffer
1505 emerge-merge-buffer))
1506 ;; Redisplay whatever buffers are showing, if there is a selected difference
1507 (if (and (>= emerge-current-difference 0)
1508 (< emerge-current-difference emerge-number-of-differences))
1509 (let* ((merge-buffer emerge-merge-buffer)
1510 (buffer-A emerge-A-buffer)
1511 (buffer-B emerge-B-buffer)
1512 (window-A (get-buffer-window buffer-A))
1513 (window-B (get-buffer-window buffer-B))
1514 (merge-window (get-buffer-window merge-buffer))
1515 (diff-vector
1516 (aref emerge-difference-list emerge-current-difference)))
1517 (if window-A (progn
1518 (select-window window-A)
1519 (emerge-position-region
1520 (- (aref diff-vector 0)
1521 (1- emerge-before-flag-length))
1522 (+ (aref diff-vector 1)
1523 (1- emerge-after-flag-length))
1524 (1+ (aref diff-vector 0)))))
1525 (if window-B (progn
1526 (select-window window-B)
1527 (emerge-position-region
1528 (- (aref diff-vector 2)
1529 (1- emerge-before-flag-length))
1530 (+ (aref diff-vector 3)
1531 (1- emerge-after-flag-length))
1532 (1+ (aref diff-vector 2)))))
1533 (if merge-window (progn
1534 (select-window merge-window)
1535 (emerge-position-region
1536 (- (aref diff-vector 4)
1537 (1- emerge-before-flag-length))
1538 (+ (aref diff-vector 5)
1539 (1- emerge-after-flag-length))
1540 (1+ (aref diff-vector 4))))))))
1542 ;;; Window scrolling operations
1543 ;; These operations are designed to scroll all three windows the same amount,
1544 ;; so as to keep the text in them aligned.
1546 ;; Perform some operation on all three windows (if they are showing).
1547 ;; Catches all errors on the operation in the A and B windows, but not
1548 ;; in the merge window. Usually, errors come from scrolling off the
1549 ;; beginning or end of the buffer, and this gives a nice error message:
1550 ;; End of buffer is reported in the merge buffer, but if the scroll was
1551 ;; possible in the A or B windows, it is performed there before the error
1552 ;; is reported.
1553 (defun emerge-operate-on-windows (operation arg)
1554 (let* ((merge-buffer emerge-merge-buffer)
1555 (buffer-A emerge-A-buffer)
1556 (buffer-B emerge-B-buffer)
1557 (window-A (get-buffer-window buffer-A))
1558 (window-B (get-buffer-window buffer-B))
1559 (merge-window (get-buffer-window merge-buffer)))
1560 (if window-A (progn
1561 (select-window window-A)
1562 (condition-case nil
1563 (funcall operation arg)
1564 (error))))
1565 (if window-B (progn
1566 (select-window window-B)
1567 (condition-case nil
1568 (funcall operation arg)
1569 (error))))
1570 (if merge-window (progn
1571 (select-window merge-window)
1572 (funcall operation arg)))))
1574 (defun emerge-scroll-up (&optional arg)
1575 "Scroll up all three merge buffers, if they are in windows.
1576 If an ARGUMENT is given, that is how many lines are scrolled, else nearly
1577 the size of the merge window. `C-u -' alone as argument scrolls half the
1578 size of the merge window."
1579 (interactive "P")
1580 (emerge-operate-on-windows
1581 'scroll-up
1582 ;; calculate argument to scroll-up
1583 ;; if there is an explicit argument
1584 (if (and arg (not (equal arg '-)))
1585 ;; use it
1586 (prefix-numeric-value arg)
1587 ;; if not, see if we can determine a default amount (the window height)
1588 (let ((merge-window (get-buffer-window emerge-merge-buffer)))
1589 (if (null merge-window)
1590 ;; no window, use nil
1592 (let ((default-amount
1593 (- (window-height merge-window) 1 next-screen-context-lines)))
1594 ;; the window was found
1595 (if arg
1596 ;; C-u as argument means half of default amount
1597 (/ default-amount 2)
1598 ;; no argument means default amount
1599 default-amount)))))))
1601 (defun emerge-scroll-down (&optional arg)
1602 "Scroll down all three merge buffers, if they are in windows.
1603 If an ARGUMENT is given, that is how many lines are scrolled, else nearly
1604 the size of the merge window. `C-u -' alone as argument scrolls half the
1605 size of the merge window."
1606 (interactive "P")
1607 (emerge-operate-on-windows
1608 'scroll-down
1609 ;; calculate argument to scroll-down
1610 ;; if there is an explicit argument
1611 (if (and arg (not (equal arg '-)))
1612 ;; use it
1613 (prefix-numeric-value arg)
1614 ;; if not, see if we can determine a default amount (the window height)
1615 (let ((merge-window (get-buffer-window emerge-merge-buffer)))
1616 (if (null merge-window)
1617 ;; no window, use nil
1619 (let ((default-amount
1620 (- (window-height merge-window) 1 next-screen-context-lines)))
1621 ;; the window was found
1622 (if arg
1623 ;; C-u as argument means half of default amount
1624 (/ default-amount 2)
1625 ;; no argument means default amount
1626 default-amount)))))))
1628 (defun emerge-scroll-left (&optional arg)
1629 "Scroll left all three merge buffers, if they are in windows.
1630 If an ARGUMENT is given, that is how many columns are scrolled, else nearly
1631 the width of the A and B windows. `C-u -' alone as argument scrolls half the
1632 width of the A and B windows."
1633 (interactive "P")
1634 (emerge-operate-on-windows
1635 'scroll-left
1636 ;; calculate argument to scroll-left
1637 ;; if there is an explicit argument
1638 (if (and arg (not (equal arg '-)))
1639 ;; use it
1640 (prefix-numeric-value arg)
1641 ;; if not, see if we can determine a default amount
1642 ;; (half the window width)
1643 (let ((merge-window (get-buffer-window emerge-merge-buffer)))
1644 (if (null merge-window)
1645 ;; no window, use nil
1647 (let ((default-amount
1648 (- (/ (window-width merge-window) 2) 3)))
1649 ;; the window was found
1650 (if arg
1651 ;; C-u as argument means half of default amount
1652 (/ default-amount 2)
1653 ;; no argument means default amount
1654 default-amount)))))))
1656 (defun emerge-scroll-right (&optional arg)
1657 "Scroll right all three merge buffers, if they are in windows.
1658 If an ARGUMENT is given, that is how many columns are scrolled, else nearly
1659 the width of the A and B windows. `C-u -' alone as argument scrolls half the
1660 width of the A and B windows."
1661 (interactive "P")
1662 (emerge-operate-on-windows
1663 'scroll-right
1664 ;; calculate argument to scroll-right
1665 ;; if there is an explicit argument
1666 (if (and arg (not (equal arg '-)))
1667 ;; use it
1668 (prefix-numeric-value arg)
1669 ;; if not, see if we can determine a default amount
1670 ;; (half the window width)
1671 (let ((merge-window (get-buffer-window emerge-merge-buffer)))
1672 (if (null merge-window)
1673 ;; no window, use nil
1675 (let ((default-amount
1676 (- (/ (window-width merge-window) 2) 3)))
1677 ;; the window was found
1678 (if arg
1679 ;; C-u as argument means half of default amount
1680 (/ default-amount 2)
1681 ;; no argument means default amount
1682 default-amount)))))))
1684 (defun emerge-scroll-reset ()
1685 "Reset horizontal scrolling of all three merge buffers to the left margin,
1686 if they are in windows."
1687 (interactive)
1688 (emerge-operate-on-windows
1689 (function (lambda (x) (set-window-hscroll (selected-window) 0)))
1690 nil))
1692 ;; Attempt to show the region nicely.
1693 ;; If there are min-lines lines above and below the region, then don't do
1694 ;; anything.
1695 ;; If not, recenter the region to make it so.
1696 ;; If that isn't possible, remove context lines balancedly from top and botton
1697 ;; so the entire region shows.
1698 ;; If that isn't possible, show the top of the region.
1699 ;; BEG must be at the beginning of a line.
1700 (defun emerge-position-region (beg end pos)
1701 ;; First test whether the entire region is visible with
1702 ;; emerge-min-visible-lines above and below it
1703 (if (not (and (<= (progn
1704 (move-to-window-line emerge-min-visible-lines)
1705 (point))
1706 beg)
1707 (<= end (progn
1708 (move-to-window-line
1709 (- (1+ emerge-min-visible-lines)))
1710 (point)))))
1711 ;; We failed that test, see if it fits at all
1712 ;; Meanwhile positioning it correctly in case it doesn't fit
1713 (progn
1714 (set-window-start (selected-window) beg)
1715 (setq fits (pos-visible-in-window-p end))
1716 (if fits
1717 ;; Determine the number of lines that the region occupies
1718 (let ((lines 0))
1719 (while (> end (progn
1720 (move-to-window-line lines)
1721 (point)))
1722 (setq lines (1+ lines)))
1723 ;; And position the beginning on the right line
1724 (goto-char beg)
1725 (recenter (/ (1+ (- (1- (window-height (selected-window)))
1726 lines))
1727 2))))))
1728 (goto-char pos))
1730 (defun emerge-next-difference ()
1731 "Advance to the next difference."
1732 (interactive)
1733 (if (< emerge-current-difference emerge-number-of-differences)
1734 (let ((n (1+ emerge-current-difference)))
1735 (while (and emerge-skip-prefers
1736 (< n emerge-number-of-differences)
1737 (memq (aref (aref emerge-difference-list n) 6)
1738 '(prefer-A prefer-B)))
1739 (setq n (1+ n)))
1740 (let ((buffer-read-only nil))
1741 (emerge-unselect-and-select-difference n)))
1742 (error "At end")))
1744 (defun emerge-previous-difference ()
1745 "Go to the previous difference."
1746 (interactive)
1747 (if (> emerge-current-difference -1)
1748 (let ((n (1- emerge-current-difference)))
1749 (while (and emerge-skip-prefers
1750 (> n -1)
1751 (memq (aref (aref emerge-difference-list n) 6)
1752 '(prefer-A prefer-B)))
1753 (setq n (1- n)))
1754 (let ((buffer-read-only nil))
1755 (emerge-unselect-and-select-difference n)))
1756 (error "At beginning")))
1758 (defun emerge-jump-to-difference (difference-number)
1759 "Go to the N-th difference."
1760 (interactive "p")
1761 (let ((buffer-read-only nil))
1762 (setq difference-number (1- difference-number))
1763 (if (and (>= difference-number -1)
1764 (< difference-number (1+ emerge-number-of-differences)))
1765 (emerge-unselect-and-select-difference difference-number)
1766 (error "Bad difference number"))))
1768 (defun emerge-quit (arg)
1769 "Finish an Emerge session. Prefix ARGUMENT means to abort rather than
1770 successfully finish. The difference depends on how the merge was started,
1771 but usually means to not write over one of the original files, or to signal
1772 to some process which invoked Emerge a failure code.
1774 Unselects the selected difference, if any, restores the read-only and modified
1775 flags of the merged file buffers, restores the local keymap of the merge
1776 buffer, and sets off various emerge flags. Using Emerge commands in this
1777 buffer after this will cause serious problems."
1778 (interactive "P")
1779 (if (prog1
1780 (y-or-n-p
1781 (if (not arg)
1782 "Do you really want to successfully finish this merge? "
1783 "Do you really want to abort this merge? "))
1784 (message ""))
1785 (emerge-really-quit arg)))
1787 ;; Perform the quit operations.
1788 (defun emerge-really-quit (arg)
1789 (setq buffer-read-only nil)
1790 (emerge-unselect-and-select-difference -1)
1791 (emerge-restore-buffer-characteristics)
1792 ;; null out the difference markers so they don't slow down future editing
1793 ;; operations
1794 (mapcar (function (lambda (d)
1795 (set-marker (aref d 0) nil)
1796 (set-marker (aref d 1) nil)
1797 (set-marker (aref d 2) nil)
1798 (set-marker (aref d 3) nil)
1799 (set-marker (aref d 4) nil)
1800 (set-marker (aref d 5) nil)))
1801 emerge-difference-list)
1802 ;; allow them to be garbage collected
1803 (setq emerge-difference-list nil)
1804 ;; restore the local map
1805 (use-local-map emerge-old-keymap)
1806 ;; turn off all the emerge modes
1807 (setq emerge-mode nil)
1808 (setq emerge-fast-mode nil)
1809 (setq emerge-edit-mode nil)
1810 (setq emerge-auto-advance nil)
1811 (setq emerge-skip-prefers nil)
1812 ;; restore mode line
1813 (kill-local-variable 'mode-line-buffer-identification)
1814 (let ((emerge-prefix-argument arg))
1815 (run-hooks 'emerge-quit-hook)))
1817 (defun emerge-select-A (&optional force)
1818 "Select the A variant of this difference. Refuses to function if this
1819 difference has been edited, i.e., if it is neither the A nor the B variant.
1820 An ARGUMENT forces the variant to be selected even if the difference has
1821 been edited."
1822 (interactive "P")
1823 (let ((operate
1824 (function (lambda ()
1825 (emerge-select-A-edit merge-begin merge-end A-begin A-end)
1826 (if emerge-auto-advance
1827 (emerge-next-difference)))))
1828 (operate-no-change
1829 (function (lambda ()
1830 (if emerge-auto-advance
1831 (emerge-next-difference))))))
1832 (emerge-select-version force operate-no-change operate operate)))
1834 ;; Actually select the A variant
1835 (defun emerge-select-A-edit (merge-begin merge-end A-begin A-end)
1836 (emerge-eval-in-buffer
1837 emerge-merge-buffer
1838 (delete-region merge-begin merge-end)
1839 (goto-char merge-begin)
1840 (insert-buffer-substring emerge-A-buffer A-begin A-end)
1841 (goto-char merge-begin)
1842 (aset diff-vector 6 'A)
1843 (emerge-refresh-mode-line)))
1845 (defun emerge-select-B (&optional force)
1846 "Select the B variant of this difference. Refuses to function if this
1847 difference has been edited, i.e., if it is neither the A nor the B variant.
1848 An ARGUMENT forces the variant to be selected even if the difference has
1849 been edited."
1850 (interactive "P")
1851 (let ((operate
1852 (function (lambda ()
1853 (emerge-select-B-edit merge-begin merge-end B-begin B-end)
1854 (if emerge-auto-advance
1855 (emerge-next-difference)))))
1856 (operate-no-change
1857 (function (lambda ()
1858 (if emerge-auto-advance
1859 (emerge-next-difference))))))
1860 (emerge-select-version force operate operate-no-change operate)))
1862 ;; Actually select the B variant
1863 (defun emerge-select-B-edit (merge-begin merge-end B-begin B-end)
1864 (emerge-eval-in-buffer
1865 emerge-merge-buffer
1866 (delete-region merge-begin merge-end)
1867 (goto-char merge-begin)
1868 (insert-buffer-substring emerge-B-buffer B-begin B-end)
1869 (goto-char merge-begin)
1870 (aset diff-vector 6 'B)
1871 (emerge-refresh-mode-line)))
1873 (defun emerge-default-A ()
1874 "Selects the A variant for all differences from here down in the buffer
1875 which are still defaulted, i.e., which the user has not selected and for
1876 which there is no preference."
1877 (interactive)
1878 (let ((buffer-read-only nil))
1879 (let ((selected-difference emerge-current-difference)
1880 (n (max emerge-current-difference 0)))
1881 (while (< n emerge-number-of-differences)
1882 (let ((diff-vector (aref emerge-difference-list n)))
1883 (if (eq (aref diff-vector 6) 'default-B)
1884 (progn
1885 (emerge-unselect-and-select-difference n t)
1886 (emerge-select-A)
1887 (aset diff-vector 6 'default-A))))
1888 (setq n (1+ n))
1889 (if (= (* (/ n 10) 10) n)
1890 (message "Setting default to A...%d" n)))
1891 (emerge-unselect-and-select-difference selected-difference)))
1892 (message "Default A set"))
1894 (defun emerge-default-B ()
1895 "Selects the B variant for all differences from here down in the buffer
1896 which are still defaulted, i.e., which the user has not selected and for
1897 which there is no preference."
1898 (interactive)
1899 (let ((buffer-read-only nil))
1900 (let ((selected-difference emerge-current-difference)
1901 (n (max emerge-current-difference 0)))
1902 (while (< n emerge-number-of-differences)
1903 (let ((diff-vector (aref emerge-difference-list n)))
1904 (if (eq (aref diff-vector 6) 'default-A)
1905 (progn
1906 (emerge-unselect-and-select-difference n t)
1907 (emerge-select-B)
1908 (aset diff-vector 6 'default-B))))
1909 (setq n (1+ n))
1910 (if (= (* (/ n 10) 10) n)
1911 (message "Setting default to B...%d" n)))
1912 (emerge-unselect-and-select-difference selected-difference)))
1913 (message "Default B set"))
1915 (defun emerge-fast-mode ()
1916 "Set fast mode, in which ordinary Emacs commands are disabled, and Emerge
1917 commands are need not be prefixed with \\<emerge-fast-keymap>\\[emerge-basic-keymap]."
1918 (interactive)
1919 (setq buffer-read-only t)
1920 (use-local-map emerge-fast-keymap)
1921 (setq emerge-mode t)
1922 (setq emerge-fast-mode t)
1923 (setq emerge-edit-mode nil)
1924 (message "Fast mode set")
1925 ;; force mode line redisplay
1926 (set-buffer-modified-p (buffer-modified-p)))
1928 (defun emerge-edit-mode ()
1929 "Set edit mode, in which ordinary Emacs commands are available, and Emerge
1930 commands must be prefixed with \\<emerge-fast-keymap>\\[emerge-basic-keymap]."
1931 (interactive)
1932 (setq buffer-read-only nil)
1933 (use-local-map emerge-edit-keymap)
1934 (setq emerge-mode t)
1935 (setq emerge-fast-mode nil)
1936 (setq emerge-edit-mode t)
1937 (message "Edit mode set")
1938 ;; force mode line redisplay
1939 (set-buffer-modified-p (buffer-modified-p)))
1941 (defun emerge-auto-advance (arg)
1942 "Toggle auto-advance mode, which causes emerge-select-A and
1943 emerge-select-B to automatically advance to the next difference. (See
1944 emerge-auto-advance.)
1945 If a positive ARGUMENT is given, it turns on auto-advance mode.
1946 If a negative ARGUMENT is given, it turns off auto-advance mode."
1947 (interactive "P")
1948 (setq emerge-auto-advance (if (null arg)
1949 (not emerge-auto-advance)
1950 (> (prefix-numeric-value arg) 0)))
1951 (message (if emerge-skip-prefers
1952 "Auto-advance set"
1953 "Auto-advance cleared"))
1954 ;; force mode line redisplay
1955 (set-buffer-modified-p (buffer-modified-p)))
1957 (defun emerge-skip-prefers (arg)
1958 "Toggle skip-prefers mode, which causes emerge-next-difference and
1959 emerge-previous-difference to automatically skip over differences for which
1960 there is a preference. (See emerge-skip-prefers.)
1961 If a positive ARGUMENT is given, it turns on skip-prefers mode.
1962 If a negative ARGUMENT is given, it turns off skip-prefers mode."
1963 (interactive "P")
1964 (setq emerge-skip-prefers (if (null arg)
1965 (not emerge-skip-prefers)
1966 (> (prefix-numeric-value arg) 0)))
1967 (message (if emerge-skip-prefers
1968 "Skip-prefers set"
1969 "Skip-prefers cleared"))
1970 ;; force mode line redisplay
1971 (set-buffer-modified-p (buffer-modified-p)))
1973 (defun emerge-copy-as-kill-A ()
1974 "Put the A variant of this difference in the kill ring."
1975 (interactive)
1976 (emerge-validate-difference)
1977 (let* ((diff-vector
1978 (aref emerge-difference-list emerge-current-difference))
1979 (A-begin (1+ (aref diff-vector 0)))
1980 (A-end (1- (aref diff-vector 1)))
1981 ;; so further kills don't append
1982 this-command)
1983 (save-excursion
1984 (set-buffer emerge-A-buffer)
1985 (copy-region-as-kill A-begin A-end))))
1987 (defun emerge-copy-as-kill-B ()
1988 "Put the B variant of this difference in the kill ring."
1989 (interactive)
1990 (emerge-validate-difference)
1991 (let* ((diff-vector
1992 (aref emerge-difference-list emerge-current-difference))
1993 (B-begin (1+ (aref diff-vector 2)))
1994 (B-end (1- (aref diff-vector 3)))
1995 ;; so further kills don't append
1996 this-command)
1997 (save-excursion
1998 (set-buffer emerge-B-buffer)
1999 (copy-region-as-kill B-begin B-end))))
2001 (defun emerge-insert-A (arg)
2002 "Insert the A variant of this difference at the point.
2003 Leaves point after text, mark before.
2004 With prefix argument, puts point before, mark after."
2005 (interactive "P")
2006 (emerge-validate-difference)
2007 (let* ((diff-vector
2008 (aref emerge-difference-list emerge-current-difference))
2009 (A-begin (1+ (aref diff-vector 0)))
2010 (A-end (1- (aref diff-vector 1)))
2011 (opoint (point))
2012 (buffer-read-only nil))
2013 (insert-buffer-substring emerge-A-buffer A-begin A-end)
2014 (if (not arg)
2015 (set-mark opoint)
2016 (set-mark (point))
2017 (goto-char opoint))))
2019 (defun emerge-insert-B (arg)
2020 "Insert the B variant of this difference at the point.
2021 Leaves point after text, mark before.
2022 With prefix argument, puts point before, mark after."
2023 (interactive "P")
2024 (emerge-validate-difference)
2025 (let* ((diff-vector
2026 (aref emerge-difference-list emerge-current-difference))
2027 (B-begin (1+ (aref diff-vector 2)))
2028 (B-end (1- (aref diff-vector 3)))
2029 (opoint (point))
2030 (buffer-read-only nil))
2031 (insert-buffer-substring emerge-B-buffer B-begin B-end)
2032 (if (not arg)
2033 (set-mark opoint)
2034 (set-mark (point))
2035 (goto-char opoint))))
2037 (defun emerge-mark-difference (arg)
2038 "Leaves the point before this difference and the mark after it.
2039 With prefix argument, puts mark before, point after."
2040 (interactive "P")
2041 (emerge-validate-difference)
2042 (let* ((diff-vector
2043 (aref emerge-difference-list emerge-current-difference))
2044 (merge-begin (1+ (aref diff-vector 4)))
2045 (merge-end (1- (aref diff-vector 5))))
2046 (if (not arg)
2047 (progn
2048 (goto-char merge-begin)
2049 (set-mark merge-end))
2050 (goto-char merge-end)
2051 (set-mark merge-begin))))
2053 (defun emerge-file-names ()
2054 "Show the names of the buffers or files being operated on by Emerge.
2055 Use ^U L to reset the windows afterward."
2056 (interactive)
2057 (delete-other-windows)
2058 (let ((temp-buffer-show-hook
2059 (function (lambda (buf)
2060 (split-window-vertically)
2061 (switch-to-buffer buf)
2062 (other-window 1)))))
2063 (with-output-to-temp-buffer "*Help*"
2064 (emerge-eval-in-buffer emerge-A-buffer
2065 (if buffer-file-name
2066 (progn
2067 (princ "File A is: ")
2068 (princ buffer-file-name))
2069 (progn
2070 (princ "Buffer A is: ")
2071 (princ (buffer-name))))
2072 (princ "\n"))
2073 (emerge-eval-in-buffer emerge-B-buffer
2074 (if buffer-file-name
2075 (progn
2076 (princ "File B is: ")
2077 (princ buffer-file-name))
2078 (progn
2079 (princ "Buffer B is: ")
2080 (princ (buffer-name))))
2081 (princ "\n"))
2082 (if emerge-ancestor-buffer
2083 (emerge-eval-in-buffer emerge-ancestor-buffer
2084 (if buffer-file-name
2085 (progn
2086 (princ "Ancestor file is: ")
2087 (princ buffer-file-name))
2088 (progn
2089 (princ "Ancestor buffer is: ")
2090 (princ (buffer-name))))
2091 (princ "\n")))
2092 (princ emerge-output-description))))
2094 (defun emerge-join-differences (arg)
2095 "Join the selected difference with the following one. With a prefix
2096 argument, join with the preceeding one."
2097 (interactive "P")
2098 (let ((n emerge-current-difference))
2099 ;; adjust n to be first difference to join
2100 (if arg
2101 (setq n (1- n)))
2102 ;; n and n+1 are the differences to join
2103 ;; check that they are both differences
2104 (if (or (< n 0) (>= n (1- emerge-number-of-differences)))
2105 (error "Incorrect differences to join"))
2106 ;; remove the flags
2107 (emerge-unselect-difference emerge-current-difference)
2108 ;; decrement total number of differences
2109 (setq emerge-number-of-differences (1- emerge-number-of-differences))
2110 ;; build new differences vector
2111 (let ((i 0)
2112 (new-differences (make-vector emerge-number-of-differences nil)))
2113 (while (< i emerge-number-of-differences)
2114 (aset new-differences i
2115 (cond
2116 ((< i n) (aref emerge-difference-list i))
2117 ((> i n) (aref emerge-difference-list (1+ i)))
2118 (t (let ((prev (aref emerge-difference-list i))
2119 (next (aref emerge-difference-list (1+ i))))
2120 (vector (aref prev 0)
2121 (aref next 1)
2122 (aref prev 2)
2123 (aref next 3)
2124 (aref prev 4)
2125 (aref next 5)
2126 (let ((ps (aref prev 6))
2127 (ns (aref next 6)))
2128 (cond
2129 ((eq ps ns)
2131 ((and (or (eq ps 'B) (eq ps 'prefer-B))
2132 (or (eq ns 'B) (eq ns 'prefer-B)))
2134 (t 'A))))))))
2135 (setq i (1+ i)))
2136 (setq emerge-difference-list new-differences))
2137 ;; set the current difference correctly
2138 (setq emerge-current-difference n)
2139 ;; fix the mode line
2140 (emerge-refresh-mode-line)
2141 ;; reinsert the flags
2142 (emerge-select-difference emerge-current-difference)
2143 (emerge-recenter)))
2145 (defun emerge-split-difference ()
2146 "Split the current difference where the points are in the three windows."
2147 (interactive)
2148 (let ((n emerge-current-difference))
2149 ;; check that this is a valid difference
2150 (emerge-validate-difference)
2151 ;; get the point values and old difference
2152 (let ((A-point (emerge-eval-in-buffer emerge-A-buffer
2153 (point-marker)))
2154 (B-point (emerge-eval-in-buffer emerge-B-buffer
2155 (point-marker)))
2156 (merge-point (point-marker))
2157 (old-diff (aref emerge-difference-list n)))
2158 ;; check location of the points, give error if they aren't in the
2159 ;; differences
2160 (if (or (< A-point (aref old-diff 0))
2161 (> A-point (aref old-diff 1)))
2162 (error "Point outside of difference in A buffer"))
2163 (if (or (< B-point (aref old-diff 2))
2164 (> B-point (aref old-diff 3)))
2165 (error "Point outside of difference in B buffer"))
2166 (if (or (< merge-point (aref old-diff 4))
2167 (> merge-point (aref old-diff 5)))
2168 (error "Point outside of difference in merge buffer"))
2169 ;; remove the flags
2170 (emerge-unselect-difference emerge-current-difference)
2171 ;; increment total number of differences
2172 (setq emerge-number-of-differences (1+ emerge-number-of-differences))
2173 ;; build new differences vector
2174 (let ((i 0)
2175 (new-differences (make-vector emerge-number-of-differences nil)))
2176 (while (< i emerge-number-of-differences)
2177 (aset new-differences i
2178 (cond
2179 ((< i n)
2180 (aref emerge-difference-list i))
2181 ((> i (1+ n))
2182 (aref emerge-difference-list (1- i)))
2183 ((= i n)
2184 (vector (aref old-diff 0)
2185 A-point
2186 (aref old-diff 2)
2187 B-point
2188 (aref old-diff 4)
2189 merge-point
2190 (aref old-diff 6)))
2192 (vector (copy-marker A-point)
2193 (aref old-diff 1)
2194 (copy-marker B-point)
2195 (aref old-diff 3)
2196 (copy-marker merge-point)
2197 (aref old-diff 5)
2198 (aref old-diff 6)))))
2199 (setq i (1+ i)))
2200 (setq emerge-difference-list new-differences))
2201 ;; set the current difference correctly
2202 (setq emerge-current-difference n)
2203 ;; fix the mode line
2204 (emerge-refresh-mode-line)
2205 ;; reinsert the flags
2206 (emerge-select-difference emerge-current-difference)
2207 (emerge-recenter))))
2209 (defun emerge-trim-difference ()
2210 "Trim lines off the top and bottom of a difference that are the same in
2211 both the A and B versions. (This can happen when the A and B versions
2212 have common lines that the ancestor version does not share.)"
2213 (interactive)
2214 ;; make sure we are in a real difference
2215 (emerge-validate-difference)
2216 ;; remove the flags
2217 (emerge-unselect-difference emerge-current-difference)
2218 (let* ((diff (aref emerge-difference-list emerge-current-difference))
2219 (top-a (marker-position (aref diff 0)))
2220 (bottom-a (marker-position (aref diff 1)))
2221 (top-b (marker-position (aref diff 2)))
2222 (bottom-b (marker-position (aref diff 3)))
2223 (top-m (marker-position (aref diff 4)))
2224 (bottom-m (marker-position (aref diff 5)))
2225 size success sa sb sm)
2226 ;; move down the tops of the difference regions as much as possible
2227 ;; Try advancing comparing 1000 chars at a time.
2228 ;; When that fails, go 500 chars at a time, and so on.
2229 (setq size 1000)
2230 (while (> size 0)
2231 (setq success t)
2232 (while success
2233 (setq size (min size (- bottom-a top-a) (- bottom-b top-b)
2234 (- bottom-m top-m)))
2235 (setq sa (emerge-eval-in-buffer emerge-A-buffer
2236 (buffer-substring top-a
2237 (+ size top-a))))
2238 (setq sb (emerge-eval-in-buffer emerge-B-buffer
2239 (buffer-substring top-b
2240 (+ size top-b))))
2241 (setq sm (buffer-substring top-m (+ size top-m)))
2242 (setq success (and (> size 0) (equal sa sb) (equal sb sm)))
2243 (if success
2244 (setq top-a (+ top-a size)
2245 top-b (+ top-b size)
2246 top-m (+ top-m size))))
2247 (setq size (/ size 2)))
2248 ;; move up the bottoms of the difference regions as much as possible
2249 ;; Try advancing comparing 1000 chars at a time.
2250 ;; When that fails, go 500 chars at a time, and so on.
2251 (setq size 1000)
2252 (while (> size 0)
2253 (setq success t)
2254 (while success
2255 (setq size (min size (- bottom-a top-a) (- bottom-b top-b)
2256 (- bottom-m top-m)))
2257 (setq sa (emerge-eval-in-buffer emerge-A-buffer
2258 (buffer-substring (- bottom-a size)
2259 bottom-a)))
2260 (setq sb (emerge-eval-in-buffer emerge-B-buffer
2261 (buffer-substring (- bottom-b size)
2262 bottom-b)))
2263 (setq sm (buffer-substring (- bottom-m size) bottom-m))
2264 (setq success (and (> size 0) (equal sa sb) (equal sb sm)))
2265 (if success
2266 (setq bottom-a (- bottom-a size)
2267 bottom-b (- bottom-b size)
2268 bottom-m (- bottom-m size))))
2269 (setq size (/ size 2)))
2270 ;; {top,bottom}-{a,b,m} are now set at the new beginnings and ends
2271 ;; of the difference regions. Move them to the beginning of lines, as
2272 ;; appropriate.
2273 (emerge-eval-in-buffer emerge-A-buffer
2274 (goto-char top-a)
2275 (beginning-of-line)
2276 (aset diff 0 (point-marker))
2277 (goto-char bottom-a)
2278 (beginning-of-line 2)
2279 (aset diff 1 (point-marker)))
2280 (emerge-eval-in-buffer emerge-B-buffer
2281 (goto-char top-b)
2282 (beginning-of-line)
2283 (aset diff 2 (point-marker))
2284 (goto-char bottom-b)
2285 (beginning-of-line 2)
2286 (aset diff 3 (point-marker)))
2287 (goto-char top-m)
2288 (beginning-of-line)
2289 (aset diff 4 (point-marker))
2290 (goto-char bottom-m)
2291 (beginning-of-line 2)
2292 (aset diff 5 (point-marker))
2293 ;; put the flags back in, recenter the display
2294 (emerge-select-difference emerge-current-difference)
2295 (emerge-recenter)))
2297 (defun emerge-find-difference (arg)
2298 "Find the difference containing the current position of the point.
2299 If there is no containing difference and the prefix argument is positive,
2300 it finds the nearest following difference. A negative prefix argument finds
2301 the nearest previous difference."
2302 (interactive "P")
2303 ;; search for the point in the merge buffer, using the markers
2304 ;; for the beginning and end of the differences in the merge buffer
2305 (emerge-find-difference1 arg (point) 4 5))
2307 (defun emerge-find-difference-A (arg)
2308 "Find the difference containing the current position of the point in the
2309 A buffer. (Nonetheless, this command must be executed in the merge buffer.)
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 A buffer, using the markers
2315 ;; for the beginning and end of the differences in the A buffer
2316 (emerge-find-difference1 arg
2317 (emerge-eval-in-buffer emerge-A-buffer (point))
2318 0 1))
2320 (defun emerge-find-difference-B (arg)
2321 "Find the difference containing the current position of the point in the
2322 B buffer. (Nonetheless, this command must be executed in the merge buffer.)
2323 If there is no containing difference and the prefix argument is positive,
2324 it finds the nearest following difference. A negative prefix argument finds
2325 the nearest previous difference."
2326 (interactive "P")
2327 ;; search for the point in the B buffer, using the markers
2328 ;; for the beginning and end of the differences in the B buffer
2329 (emerge-find-difference1 arg
2330 (emerge-eval-in-buffer emerge-B-buffer (point))
2331 2 3))
2333 (defun emerge-find-difference1 (arg location begin end)
2334 (let* ((index
2335 ;; find first difference containing or after the current position
2336 (catch 'search
2337 (let ((n 0))
2338 (while (< n emerge-number-of-differences)
2339 (let ((diff-vector (aref emerge-difference-list n)))
2340 (if (<= location (marker-position (aref diff-vector end)))
2341 (throw 'search n)))
2342 (setq n (1+ n))))
2343 emerge-number-of-differences))
2344 (contains
2345 ;; whether the found difference contains the current position
2346 (and (< index emerge-number-of-differences)
2347 (<= (marker-position (aref (aref emerge-difference-list index)
2348 begin))
2349 location)))
2350 (arg-value
2351 ;; numeric value of prefix argument
2352 (prefix-numeric-value arg)))
2353 (emerge-unselect-and-select-difference
2354 (cond
2355 ;; if the point is in a difference, select it
2356 (contains index)
2357 ;; if the arg is nil and the point is not in a difference, error
2358 ((null arg) (error "No difference contains point"))
2359 ;; if the arg is positive, select the following difference
2360 ((> arg-value 0)
2361 (if (< index emerge-number-of-differences)
2362 index
2363 (error "No difference contains or follows point")))
2364 ;; if the arg is negative, select the preceeding difference
2366 (if (> index 0)
2367 (1- index)
2368 (error "No difference contains or preceeds point")))))))
2370 (defun emerge-line-numbers ()
2371 "Display the current line numbers of the points in the A, B, and
2372 merge buffers."
2373 (interactive)
2374 (let* ((valid-diff
2375 (and (>= emerge-current-difference 0)
2376 (< emerge-current-difference emerge-number-of-differences)))
2377 (diff (and valid-diff
2378 (aref emerge-difference-list emerge-current-difference)))
2379 (merge-line (emerge-line-number-in-buf 4 5))
2380 (A-line (emerge-eval-in-buffer emerge-A-buffer
2381 (emerge-line-number-in-buf 0 1)))
2382 (B-line (emerge-eval-in-buffer emerge-B-buffer
2383 (emerge-line-number-in-buf 2 3))))
2384 (message "At lines: merge = %d, A = %d, B = %d"
2385 merge-line A-line B-line)))
2387 (defun emerge-line-number-in-buf (begin-marker end-marker)
2388 (let (temp)
2389 (setq temp (save-excursion
2390 (beginning-of-line)
2391 (1+ (count-lines 1 (point)))))
2392 (if valid-diff
2393 (progn
2394 (if (> (point) (aref diff begin-marker))
2395 (setq temp (- temp emerge-before-flag-lines)))
2396 (if (> (point) (aref diff end-marker))
2397 (setq temp (- temp emerge-after-flag-lines)))))
2398 temp))
2400 (defun emerge-set-combine-versions-template (start end &optional localize)
2401 "Copy region into emerge-combine-versions-template which controls how
2402 emerge-combine-versions will combine the two versions.
2403 With prefix argument, emerge-combine-versions is made local to this
2404 merge buffer. Localization is permanent for any particular merge buffer."
2405 (interactive "r\nP")
2406 (if localize
2407 (make-local-variable 'emerge-combine-versions-template))
2408 (setq emerge-combine-versions-template (buffer-substring start end))
2409 (message
2410 (if (assq 'emerge-combine-versions-template (buffer-local-variables))
2411 "emerge-set-combine-versions-template set locally."
2412 "emerge-set-combine-versions-template set.")))
2414 (defun emerge-combine-versions (&optional force)
2415 "Combine the two versions using the template in
2416 emerge-combine-versions-template.
2417 Refuses to function if this difference has been edited, i.e., if it is
2418 neither the A nor the B variant.
2419 An ARGUMENT forces the variant to be selected even if the difference has
2420 been edited."
2421 (interactive "P")
2422 (emerge-combine-versions-internal emerge-combine-versions-template force))
2424 (defun emerge-combine-versions-register (char &optional force)
2425 "Combine the two versions using the template in register REG.
2426 See documentation of the variable emerge-combine-versions-template
2427 for how the template is interpreted.
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 "cRegister containing template: \nP")
2433 (let ((template (get-register char)))
2434 (if (not (stringp template))
2435 (error "Register does not contain text"))
2436 (emerge-combine-versions-internal template force)))
2438 (defun emerge-combine-versions-internal (template force)
2439 (let ((operate
2440 (function (lambda ()
2441 (emerge-combine-versions-edit merge-begin merge-end
2442 A-begin A-end B-begin B-end)
2443 (if emerge-auto-advance
2444 (emerge-next-difference))))))
2445 (emerge-select-version force operate operate operate)))
2447 (defun emerge-combine-versions-edit (merge-begin merge-end
2448 A-begin A-end B-begin B-end)
2449 (emerge-eval-in-buffer
2450 emerge-merge-buffer
2451 (delete-region merge-begin merge-end)
2452 (goto-char merge-begin)
2453 (let ((i 0))
2454 (while (< i (length template))
2455 (let ((c (aref template i)))
2456 (if (= c ?%)
2457 (progn
2458 (setq i (1+ i))
2459 (setq c
2460 (condition-case nil
2461 (aref template i)
2462 (error ?%)))
2463 (cond ((= c ?a)
2464 (insert-buffer-substring emerge-A-buffer A-begin A-end))
2465 ((= c ?b)
2466 (insert-buffer-substring emerge-B-buffer B-begin B-end))
2467 ((= c ?%)
2468 (insert ?%)
2470 (insert c)))))
2471 (insert c)))
2472 (setq i (1+ i))))
2473 (goto-char merge-begin)
2474 (aset diff-vector 6 'combined)
2475 (emerge-refresh-mode-line)))
2477 (defun emerge-set-merge-mode (mode)
2478 "Set the major mode in a merge buffer. Overrides any change that the mode
2479 might make to the mode line or local keymap. Leaves merge in fast mode."
2480 (interactive
2481 (list (intern (completing-read "New major mode for merge buffer: "
2482 obarray 'commandp t nil))))
2483 (funcall mode)
2484 (emerge-refresh-mode-line)
2485 (if emerge-fast-mode
2486 (emerge-fast-mode)
2487 (emerge-edit-mode)))
2489 (defun emerge-one-line-window ()
2490 (interactive)
2491 (let ((window-min-height 1))
2492 (shrink-window (- (window-height) 2))))
2494 ;;; Support routines
2496 ;; Select a difference by placing the visual flags around the appropriate
2497 ;; group of lines in the A, B, and merge buffers
2498 (defun emerge-select-difference (n)
2499 (let ((diff-vector (aref emerge-difference-list n)))
2500 (emerge-place-flags-in-buffer emerge-A-buffer
2501 (aref diff-vector 0) (aref diff-vector 1))
2502 (emerge-place-flags-in-buffer emerge-B-buffer
2503 (aref diff-vector 2) (aref diff-vector 3))
2504 (emerge-place-flags-in-buffer emerge-merge-buffer
2505 (aref diff-vector 4) (aref diff-vector 5))))
2507 (defun emerge-place-flags-in-buffer (buffer before after)
2508 (if (eq buffer emerge-merge-buffer)
2509 (emerge-place-flags-in-buffer1 buffer before after)
2510 (emerge-eval-in-buffer
2511 buffer
2512 (emerge-place-flags-in-buffer1 buffer before after))))
2514 (defun emerge-place-flags-in-buffer1 (buffer before after)
2515 (let ((buffer-read-only nil))
2516 ;; insert the flags
2517 (goto-char before)
2518 (insert-before-markers emerge-before-flag)
2519 (goto-char after)
2520 (insert emerge-after-flag)
2521 ;; put the markers into the flags, so alterations above or below won't move
2522 ;; them
2523 ;; before marker is one char before the end of the before flag
2524 ;; after marker is one char after the beginning of the after flag
2525 (set-marker before (1- before))
2526 (set-marker after (1+ after))))
2528 ;; Unselect a difference by removing the visual flags in the buffers.
2529 (defun emerge-unselect-difference (n)
2530 (let ((diff-vector (aref emerge-difference-list n)))
2531 (emerge-remove-flags-in-buffer emerge-A-buffer
2532 (aref diff-vector 0) (aref diff-vector 1))
2533 (emerge-remove-flags-in-buffer emerge-B-buffer
2534 (aref diff-vector 2) (aref diff-vector 3))
2535 (emerge-remove-flags-in-buffer emerge-merge-buffer
2536 (aref diff-vector 4) (aref diff-vector 5))))
2538 (defun emerge-remove-flags-in-buffer (buffer before after)
2539 (emerge-eval-in-buffer
2540 buffer
2541 (let ((buffer-read-only nil))
2542 ;; put the markers at the beginning of the flags
2543 (set-marker before (- before (1- emerge-before-flag-length)))
2544 (set-marker after (1- after))
2545 ;; remove the flags
2546 (goto-char before)
2547 (if (looking-at emerge-before-flag-match)
2548 (delete-char emerge-before-flag-length)
2549 ;; the flag isn't there
2550 (ding)
2551 (message "Trouble removing flag."))
2552 (goto-char after)
2553 (if (looking-at emerge-after-flag-match)
2554 (delete-char emerge-after-flag-length)
2555 ;; the flag isn't there
2556 (ding)
2557 (message "Trouble removing flag.")))))
2559 ;; Select a difference, removing an flags that exist now.
2560 (defun emerge-unselect-and-select-difference (n &optional suppress-display)
2561 (if (and (>= emerge-current-difference 0)
2562 (< emerge-current-difference emerge-number-of-differences))
2563 (emerge-unselect-difference emerge-current-difference))
2564 (if (and (>= n 0) (< n emerge-number-of-differences))
2565 (progn
2566 (emerge-select-difference n)
2567 (let* ((diff-vector (aref emerge-difference-list n))
2568 (selection-type (aref diff-vector 6)))
2569 (if (eq selection-type 'default-A)
2570 (aset diff-vector 6 'A)
2571 (if (eq selection-type 'default-B)
2572 (aset diff-vector 6 'B))))))
2573 (setq emerge-current-difference n)
2574 (if (not suppress-display)
2575 (progn
2576 (emerge-recenter)
2577 (emerge-refresh-mode-line))))
2579 ;; Perform tests to see whether user should be allowed to select a version
2580 ;; of this difference:
2581 ;; a valid difference has been selected; and
2582 ;; the difference text in the merge buffer is:
2583 ;; the A version (execute a-version), or
2584 ;; the B version (execute b-version), or
2585 ;; empty (execute neither-version), or
2586 ;; argument FORCE is true (execute neither-version)
2587 ;; Otherwise, signal an error.
2588 (defun emerge-select-version (force a-version b-version neither-version)
2589 (emerge-validate-difference)
2590 (let ((buffer-read-only nil))
2591 (let* ((diff-vector
2592 (aref emerge-difference-list emerge-current-difference))
2593 (A-begin (1+ (aref diff-vector 0)))
2594 (A-end (1- (aref diff-vector 1)))
2595 (B-begin (1+ (aref diff-vector 2)))
2596 (B-end (1- (aref diff-vector 3)))
2597 (merge-begin (1+ (aref diff-vector 4)))
2598 (merge-end (1- (aref diff-vector 5))))
2599 (if (emerge-compare-buffers emerge-A-buffer A-begin A-end
2600 emerge-merge-buffer merge-begin
2601 merge-end)
2602 (funcall a-version)
2603 (if (emerge-compare-buffers emerge-B-buffer B-begin B-end
2604 emerge-merge-buffer merge-begin
2605 merge-end)
2606 (funcall b-version)
2607 (if (or force (= merge-begin merge-end))
2608 (funcall neither-version)
2609 (error "This difference region has been edited.")))))))
2611 ;; Revise the mode line to display which difference we have selected
2613 (defun emerge-refresh-mode-line ()
2614 (setq mode-line-buffer-identification
2615 (list (format "Emerge: %%b diff %d of %d%s"
2616 (1+ emerge-current-difference)
2617 emerge-number-of-differences
2618 (if (and (>= emerge-current-difference 0)
2619 (< emerge-current-difference
2620 emerge-number-of-differences))
2621 (cdr (assq (aref (aref emerge-difference-list
2622 emerge-current-difference)
2624 '((A . " - A")
2625 (B . " - B")
2626 (prefer-A . " - A*")
2627 (prefer-B . " - B*")
2628 (combined . " - comb"))))
2629 ""))))
2630 ;; Force mode-line redisplay
2631 (set-buffer-modified-p (buffer-modified-p)))
2633 ;; compare two regions in two buffers for containing the same text
2634 (defun emerge-compare-buffers (buffer-x x-begin x-end buffer-y y-begin y-end)
2635 ;; first check that the two regions are the same length
2636 (if (not (and (= (- x-end x-begin) (- y-end y-begin))))
2638 (catch 'exit
2639 (while (< x-begin x-end)
2640 ;; bite off and compare no more than 1000 characters at a time
2641 (let* ((compare-length (min (- x-end x-begin) 1000))
2642 (x-string (emerge-eval-in-buffer
2643 buffer-x
2644 (buffer-substring x-begin
2645 (+ x-begin compare-length))))
2646 (y-string (emerge-eval-in-buffer
2647 buffer-y
2648 (buffer-substring y-begin
2649 (+ y-begin compare-length)))))
2650 (if (not (string-equal x-string y-string))
2651 (throw 'exit nil)
2652 (setq x-begin (+ x-begin compare-length))
2653 (setq y-begin (+ y-begin compare-length)))))
2654 t)))
2656 ;; Construct a unique buffer name.
2657 ;; The first one tried is prefixsuffix, then prefix<2>suffix,
2658 ;; prefix<3>suffix, etc.
2659 (defun emerge-unique-buffer-name (prefix suffix)
2660 (if (null (get-buffer (concat prefix suffix)))
2661 (concat prefix suffix)
2662 (let ((n 2))
2663 (while (get-buffer (format "%s<%d>%s" prefix n suffix))
2664 (setq n (1+ n)))
2665 (format "%s<%d>%s" prefix n suffix))))
2667 ;; Verify that we have a difference selected.
2668 (defun emerge-validate-difference ()
2669 (if (not (and (>= emerge-current-difference 0)
2670 (< emerge-current-difference emerge-number-of-differences)))
2671 (error "No difference selected")))
2673 ;;; Functions for saving and restoring a batch of variables
2675 ;; These functions save (get the values of) and restore (set the values of)
2676 ;; a list of variables. The argument is a list of symbols (the names of
2677 ;; the variables). A list element can also be a list of two functions,
2678 ;; the first of which (when called with no arguments) gets the value, and
2679 ;; the second (when called with a value as an argment) sets the value.
2680 ;; A "function" is anything that funcall can handle as an argument.
2682 (defun emerge-save-variables (vars)
2683 (mapcar (function (lambda (v) (if (symbolp v)
2684 (symbol-value v)
2685 (funcall (car v)))))
2686 vars))
2688 (defun emerge-restore-variables (vars values)
2689 (while vars
2690 (let ((var (car vars))
2691 (value (car values)))
2692 (if (symbolp var)
2693 (set var value)
2694 (funcall (car (cdr var)) value)))
2695 (setq vars (cdr vars))
2696 (setq values (cdr values))))
2698 ;; Make a temporary file that only we have access to.
2699 ;; PREFIX is appended to emerge-temp-file-prefix to make the filename prefix.
2700 (defun emerge-make-temp-file (prefix)
2701 (let ((f (make-temp-name (concat emerge-temp-file-prefix prefix))))
2702 ;; create the file
2703 (write-region (point-min) (point-min) f nil 'no-message)
2704 (set-file-modes f emerge-temp-file-mode)
2707 ;;; Functions that query the user before he can write out the current buffer.
2709 (defun emerge-query-write-file ()
2710 "Query the user if he really wants to write out the incomplete merge.
2711 If he says yes, call write-file to do so. See emerge-query-and-call
2712 for details of the querying process."
2713 (interactive)
2714 (emerge-query-and-call 'write-file))
2716 (defun emerge-query-save-buffer ()
2717 "Query the user if he really wants to write out the incomplete merge.
2718 If he says yes, call save-buffer to do so. See emerge-query-and-call
2719 for details of the querying process."
2720 (interactive)
2721 (emerge-query-and-call 'save-buffer))
2723 (defun emerge-query-and-call (command)
2724 "Query the user if he really wants to write out the incomplete merge.
2725 If he says yes, call COMMAND interactively. During the call, the flags
2726 around the current difference are removed."
2727 (if (yes-or-no-p "Do you really write to write out this unfinished merge? ")
2728 ;; He really wants to do it -- unselect the difference for the duration
2729 (progn
2730 (if (and (>= emerge-current-difference 0)
2731 (< emerge-current-difference emerge-number-of-differences))
2732 (emerge-unselect-difference emerge-current-difference))
2733 ;; call-interactively takes the value of current-prefix-arg as the
2734 ;; prefix argument value to be passed to the command. Thus, we have
2735 ;; to do nothing special to make sure the prefix argument is
2736 ;; transmitted to the command.
2737 (call-interactively command)
2738 (if (and (>= emerge-current-difference 0)
2739 (< emerge-current-difference emerge-number-of-differences))
2740 (progn
2741 (emerge-select-difference emerge-current-difference)
2742 (emerge-recenter))))
2743 ;; He's being smart and not doing it
2744 (message "Not written")))
2746 ;; Make sure the current buffer (for a file) has the same contents as the
2747 ;; file on disk, and attempt to remedy the situation if not.
2748 ;; Signal an error if we can't make them the same, or the user doesn't want
2749 ;; to do what is necessary to make them the same.
2750 (defun emerge-verify-file-buffer ()
2751 ;; First check if the file has been modified since the buffer visited it.
2752 (if (verify-visited-file-modtime (current-buffer))
2753 (if (buffer-modified-p)
2754 ;; If buffer is not obsolete and is modified, offer to save
2755 (if (yes-or-no-p (format "Save file %s? " buffer-file-name))
2756 (save-buffer)
2757 (error "Buffer out of sync for file %s" buffer-file-name))
2758 ;; If buffer is not obsolete and is not modified, do nothing
2759 nil)
2760 (if (buffer-modified-p)
2761 ;; If buffer is obsolete and is modified, give error
2762 (error "Buffer out of sync for file %s" buffer-file-name)
2763 ;; If buffer is obsolete and is not modified, offer to revert
2764 (if (yes-or-no-p (format "Revert file %s? " buffer-file-name))
2765 (revert-buffer t t)
2766 (error "Buffer out of sync for file %s" buffer-file-name)))))
2768 ;; Utilities that might have value outside of Emerge.
2770 ;; Set up the mode in the current buffer to duplicate the mode in another
2771 ;; buffer.
2772 (defun emerge-copy-modes (buffer)
2773 ;; Set the major mode
2774 (funcall (emerge-eval-in-buffer buffer major-mode)))
2776 ;; Define a key, even if a prefix of it is defined
2777 (defun emerge-force-define-key (keymap key definition)
2778 "Like define-key, but is not stopped if a prefix of KEY is a defined
2779 command."
2780 ;; Find out if a prefix of key is defined
2781 (let ((v (lookup-key keymap key)))
2782 ;; If so, undefine it
2783 (if (integerp v)
2784 (define-key keymap (substring key 0 v) nil)))
2785 ;; Now define the key
2786 (define-key keymap key definition))
2788 ;;; Improvements to describe-mode, so that it describes minor modes as well
2789 ;;; as the major mode
2790 (defun describe-mode (&optional minor)
2791 "Display documentation of current major mode.
2792 If optional MINOR is non-nil (or prefix argument is given if interactive),
2793 display documentation of acive minor modes as well.
2794 For this to work correctly for a minor mode, the mode's indicator variable
2795 (listed in minor-mode-alist) must also be a function whose documentation
2796 describes the minor mode."
2797 (interactive)
2798 (with-output-to-temp-buffer "*Help*"
2799 (princ mode-name)
2800 (princ " Mode:\n")
2801 (princ (documentation major-mode))
2802 (let ((minor-modes minor-mode-alist)
2803 (locals (buffer-local-variables)))
2804 (while minor-modes
2805 (let* ((minor-mode (car (car minor-modes)))
2806 (indicator (car (cdr (car minor-modes))))
2807 (local-binding (assq minor-mode locals)))
2808 ;; Document a minor mode if it is listed in minor-mode-alist,
2809 ;; bound locally in this buffer, non-nil, and has a function
2810 ;; definition.
2811 (if (and local-binding
2812 (cdr local-binding)
2813 (fboundp minor-mode))
2814 (progn
2815 (princ (format "\n\n\n%s minor mode (indicator%s):\n"
2816 minor-mode indicator))
2817 (princ (documentation minor-mode)))))
2818 (setq minor-modes (cdr minor-modes))))
2819 (print-help-return-message)))
2821 ;; Adjust things so that keyboard macro definitions are documented correctly.
2822 (fset 'defining-kbd-macro (symbol-function 'start-kbd-macro))
2824 ;; Function to shadow a definition in a keymap with definitions in another.
2825 (defun emerge-shadow-key-definition (olddef newdef keymap shadowmap)
2826 "Shadow OLDDEF with NEWDEF for any keys in KEYMAP with entries in SHADOWMAP.
2827 In other words, SHADOWMAP will now shadow all definitions of OLDDEF in KEYMAP
2828 with NEWDEF. Does not affect keys that are already defined in SHADOWMAP,
2829 including those whose definition is OLDDEF."
2830 ;; loop through all keymaps accessible from keymap
2831 (let ((maps (accessible-keymaps keymap)))
2832 (while maps
2833 (let ((prefix (car (car maps)))
2834 (map (cdr (car maps))))
2835 ;; examine a keymap
2836 (if (arrayp map)
2837 ;; array keymap
2838 (let ((len (length map))
2839 (i 0))
2840 (while (< i len)
2841 (if (eq (aref map i) olddef)
2842 ;; set the shadowing definition
2843 (let ((key (concat prefix (char-to-string i))))
2844 (emerge-define-key-if-possible shadowmap key newdef)))
2845 (setq i (1+ i))))
2846 ;; sparse keymap
2847 (while map
2848 (if (eq (cdr-safe (car-safe map)) olddef)
2849 ;; set the shadowing definition
2850 (let ((key
2851 (concat prefix (char-to-string (car (car map))))))
2852 (emerge-define-key-if-possible shadowmap key newdef)))
2853 (setq map (cdr map)))))
2854 (setq maps (cdr maps)))))
2856 ;; Define a key if it (or a prefix) is not already defined in the map.
2857 (defun emerge-define-key-if-possible (keymap key definition)
2858 ;; look up the present definition of the key
2859 (let ((present (lookup-key keymap key)))
2860 (if (integerp present)
2861 ;; if it is "too long", look up the valid prefix
2862 (if (not (lookup-key keymap (substring key 0 present)))
2863 ;; if the prefix isn't defined, define it
2864 (define-key keymap key definition))
2865 ;; if there is no present definition, define it
2866 (if (not present)
2867 (define-key keymap key definition)))))
2869 (defun emerge-recursively-substitute-key-definition (olddef newdef keymap)
2870 "Like substitute-key-definition, but examines and substitutes in all
2871 keymaps accessible from KEYMAP. Make sure that subordinate keymaps aren't
2872 shared with other keymaps! (copy-keymap will suffice.)"
2873 ;; Loop through all keymaps accessible from keymap
2874 (let ((maps (accessible-keymaps keymap)))
2875 (while maps
2876 ;; Substitute in this keymap
2877 (substitute-key-definition olddef newdef (cdr (car maps)))
2878 (setq maps (cdr maps)))))
2880 ;; Show the name of the file in the buffer.
2881 (defun emerge-show-file-name ()
2882 "Displays the name of the file loaded into the current buffer.
2883 If the name won't fit on one line, the minibuffer is expanded to hold it,
2884 and the command waits for a keystroke from the user. If the keystroke is
2885 SPC, it is ignored; if it is anything else, it is processed as a command."
2886 (interactive)
2887 (let ((name (buffer-file-name)))
2888 (or name
2889 (setq name "Buffer has no file name."))
2890 (save-window-excursion
2891 (select-window (minibuffer-window))
2892 (erase-buffer)
2893 (insert name)
2894 (if (not (pos-visible-in-window-p))
2895 (let ((echo-keystrokes 0))
2896 (while (and (not (pos-visible-in-window-p))
2897 (> (1- (frame-height)) (window-height)))
2898 (enlarge-window 1))
2899 (let ((c (read-char)))
2900 (if (/= c 32)
2901 (setq unread-command-char c))))))))
2903 ;; Improved auto-save file names.
2904 ;; This function fixes many problems with the standard auto-save file names:
2905 ;; Auto-save files for non-file buffers get put in the default directory
2906 ;; for the buffer, whether that makes sense or not.
2907 ;; Auto-save files for file buffers get put in the directory of the file,
2908 ;; regardless of whether we can write into it or not.
2909 ;; Auto-save files for non-file buffers don't use the process id, so if a
2910 ;; user runs more than on Emacs, they can make auto-save files that overwrite
2911 ;; each other.
2912 ;; To use this function, do:
2913 ;; (fset 'make-auto-save-file-name
2914 ;; (symbol-function 'emerge-make-auto-save-file-name))
2915 (defun emerge-make-auto-save-file-name ()
2916 "Return file name to use for auto-saves of current buffer.
2917 Does not consider auto-save-visited-file-name; that is checked
2918 before calling this function.
2919 You can redefine this for customization.
2920 See also auto-save-file-name-p."
2921 (if buffer-file-name
2922 ;; if buffer has a file, try the format <file directory>/#<file name>#
2923 (let ((f (concat (file-name-directory buffer-file-name)
2925 (file-name-nondirectory buffer-file-name)
2926 "#")))
2927 (if (file-writable-p f)
2928 ;; the file is writable, so use it
2930 ;; the file isn't writable, so use the format
2931 ;; ~/#&<file name>&<hash of directory>#
2932 (concat (getenv "HOME")
2933 "/#&"
2934 (file-name-nondirectory buffer-file-name)
2936 (hash-string-into-string
2937 (file-name-directory buffer-file-name))
2938 "#")))
2939 ;; if buffer has no file, use the format ~/#%<buffer name>%<process id>#
2940 (expand-file-name (concat (getenv "HOME")
2941 "/#%"
2942 ;; quote / into \! and \ into \\
2943 (unslashify-name (buffer-name))
2945 (make-temp-name "")
2946 "#"))))
2948 ;; Hash a string into five characters more-or-less suitable for use in a file
2949 ;; name. (Allowed characters are ! through ~, except /.)
2950 (defun hash-string-into-string (s)
2951 (let ((bins (vector 0 0 0 0 0))
2952 (i 0))
2953 (while (< i (length s))
2954 (aset bins (% i 5) (% (+ (* (aref bins (% i 5)) 35)
2955 (aref s i))
2956 65536))
2957 (setq i (1+ i)))
2958 (mapconcat (function (lambda (b)
2959 (setq b (+ (% b 93) ?!))
2960 (if (>= b ?/)
2961 (setq b (1+ b)))
2962 (char-to-string b)))
2963 bins "")))
2965 ;; Quote any /s in a string by replacing them with \!.
2966 ;; Also, replace any \s by \\, to make it one-to-one.
2967 (defun unslashify-name (s)
2968 (let ((limit 0))
2969 (while (string-match "[/\\]" s limit)
2970 (setq s (concat (substring s 0 (match-beginning 0))
2971 (if (string= (substring s (match-beginning 0)
2972 (match-end 0))
2973 "/")
2974 "\\!"
2975 "\\\\")
2976 (substring s (match-end 0))))
2977 (setq limit (1+ (match-end 0)))))
2980 (provide 'emerge)
2982 ;;; emerge.el ends here