1 ;;; emerge.el --- merge diffs under Emacs control
3 ;;; The author has placed this file in the public domain.
5 ;; This file is part of GNU Emacs.
7 ;; Author: Dale R. Worley <worley@world.std.com>
8 ;; Keywords: unix, vc, tools
10 ;; This software was created by Dale R. Worley and is
11 ;; distributed free of charge. It is placed in the public domain and
12 ;; permission is granted to anyone to use, duplicate, modify and redistribute
13 ;; it provided that this notice is attached.
15 ;; Dale R. Worley provides absolutely NO WARRANTY OF ANY KIND
16 ;; with respect to this software. The entire risk as to the quality and
17 ;; performance of this software is with the user. IN NO EVENT WILL DALE
18 ;; R. WORLEY BE LIABLE TO ANYONE FOR ANY DAMAGES ARISING OUT THE
19 ;; USE OF THIS SOFTWARE, INCLUDING, WITHOUT LIMITATION, DAMAGES RESULTING FROM
20 ;; LOST DATA OR LOST PROFITS, OR FOR ANY SPECIAL, INCIDENTAL OR CONSEQUENTIAL
27 ;; There aren't really global variables, just dynamic bindings
39 (defmacro emerge-defvar-local
(var value doc
)
40 "Defines SYMBOL as an advertised variable.
41 Performs a defvar, then executes `make-variable-buffer-local' on
42 the variable. Also sets the `permanent-local' property, so that
43 `kill-all-local-variables' (called by major-mode setting commands)
44 won't destroy Emerge control variables."
46 (defvar ,var
,value
,doc
)
47 (make-variable-buffer-local ',var
)
48 (put ',var
'permanent-local t
)))
50 ;; Add entries to minor-mode-alist so that emerge modes show correctly
51 (defvar emerge-minor-modes-list
52 '((emerge-mode " Emerge")
53 (emerge-fast-mode " F")
54 (emerge-edit-mode " E")
55 (emerge-auto-advance " A")
56 (emerge-skip-prefers " S")))
57 (if (not (assq 'emerge-mode minor-mode-alist
))
58 (setq minor-mode-alist
(append emerge-minor-modes-list
61 ;; We need to define this function so describe-mode can describe Emerge mode.
63 "Emerge mode is used by the Emerge file-merging package.
64 It is entered only through one of the functions:
66 `emerge-files-with-ancestor'
68 `emerge-buffers-with-ancestor'
69 `emerge-files-command'
70 `emerge-files-with-ancestor-command'
72 `emerge-files-with-ancestor-remote'
75 \\{emerge-basic-keymap}
76 Commands must be prefixed by \\<emerge-fast-keymap>\\[emerge-basic-keymap] in `edit' mode,
77 but can be invoked directly in `fast' mode.")
79 ;;; Emerge configuration variables
82 "Merge diffs under Emacs control."
85 ;; Commands that produce difference files
86 ;; All that can be configured is the name of the programs to execute
87 ;; (emerge-diff-program and emerge-diff3-program) and the options
88 ;; to be provided (emerge-diff-options). The order in which the file names
89 ;; are given is fixed.
90 ;; The file names are always expanded (see expand-file-name) before being
91 ;; passed to diff, thus they need not be invoked under a shell that
93 ;; The code which processes the diff/diff3 output depends on all the
94 ;; finicky details of their output, including the somewhat strange
95 ;; way they number lines of a file.
96 (defcustom emerge-diff-program
"diff"
97 "Name of the program which compares two files."
100 (defcustom emerge-diff3-program
"diff3"
101 "Name of the program which compares three files.
102 Its arguments are the ancestor file and the two variant files."
105 (defcustom emerge-diff-options
""
106 "Options to pass to `emerge-diff-program' and `emerge-diff3-program'."
109 (defcustom emerge-match-diff-line
110 (let ((x "\\([0-9]+\\)\\(\\|,\\([0-9]+\\)\\)"))
111 (concat "^" x
"\\([acd]\\)" x
"$"))
112 "Pattern to match lines produced by diff that describe differences.
113 This is as opposed to lines from the source files."
116 (defcustom emerge-diff-ok-lines-regexp
117 "^\\([0-9,]+[acd][0-9,]+$\\|[<>] \\|---\\)"
118 "Regexp that matches normal output lines from `emerge-diff-program'.
119 Lines that do not match are assumed to be error messages."
122 (defcustom emerge-diff3-ok-lines-regexp
123 "^\\([1-3]:\\|====\\| \\)"
124 "Regexp that matches normal output lines from `emerge-diff3-program'.
125 Lines that do not match are assumed to be error messages."
129 (defcustom emerge-rcs-ci-program
"ci"
130 "Name of the program that checks in RCS revisions."
133 (defcustom emerge-rcs-co-program
"co"
134 "Name of the program that checks out RCS revisions."
138 (defcustom emerge-process-local-variables nil
139 "Non-nil if Emerge should process local-variables lists in merge buffers.
140 \(You can explicitly request processing the local-variables
141 by executing `(hack-local-variables)'.)"
144 (defcustom emerge-execute-line-deletions nil
145 "If non-nil: `emerge-execute-line' makes no output if an input was deleted.
146 It concludes that an input version has been deleted when an ancestor entry
147 is present, only one A or B entry is present, and an output entry is present.
148 If nil: In such circumstances, the A or B file that is present will be
149 copied to the designated output file."
153 (defcustom emerge-before-flag
"vvvvvvvvvvvvvvvvvvvv\n"
154 "Flag placed above the highlighted block of code. Must end with newline.
155 Must be set before Emerge is loaded, or emerge-new-flags must be run
159 (defcustom emerge-after-flag
"^^^^^^^^^^^^^^^^^^^^\n"
160 "Flag placed below the highlighted block of code. Must end with newline.
161 Must be set before Emerge is loaded, or emerge-new-flags must be run
168 (defcustom emerge-startup-hook nil
169 "Hook to run in the merge buffer after the merge has been set up."
172 (defcustom emerge-select-hook nil
173 "Hook to run after a difference has been selected.
174 The variable `n' holds the (internal) number of the difference."
177 (defcustom emerge-unselect-hook nil
178 "Hook to run after a difference has been unselected.
179 The variable `n' holds the (internal) number of the difference."
183 ;; Variables to control the default directories of the arguments to
186 (defcustom emerge-default-last-directories nil
187 "If nil, default dir for filenames in emerge is `default-directory'.
188 If non-nil, filenames complete in the directory of the last argument of the
189 same type to an `emerge-files...' command."
193 (defvar emerge-last-dir-A nil
194 "Last directory for the first file of an `emerge-files...' command.")
195 (defvar emerge-last-dir-B nil
196 "Last directory for the second file of an `emerge-files...' command.")
197 (defvar emerge-last-dir-ancestor nil
198 "Last directory for the ancestor file of an `emerge-files...' command.")
199 (defvar emerge-last-dir-output nil
200 "Last directory for the output file of an `emerge-files...' command.")
201 (defvar emerge-last-revision-A nil
202 "Last RCS revision used for first file of an `emerge-revisions...' command.")
203 (defvar emerge-last-revision-B nil
204 "Last RCS revision used for second file of an `emerge-revisions...' command.")
205 (defvar emerge-last-revision-ancestor nil
206 "Last RCS revision used for ancestor file of an `emerge-revisions...' command.")
208 (defvar emerge-before-flag-length
)
209 (defvar emerge-before-flag-lines
)
210 (defvar emerge-before-flag-match
)
211 (defvar emerge-after-flag-length
)
212 (defvar emerge-after-flag-lines
)
213 (defvar emerge-after-flag-match
)
214 (defvar emerge-diff-buffer
)
215 (defvar emerge-diff-error-buffer
)
216 (defvar emerge-prefix-argument
)
217 (defvar emerge-file-out
)
218 (defvar emerge-exit-func
)
219 (defvar emerge-globalized-difference-list
)
220 (defvar emerge-globalized-number-of-differences
)
222 ;; The flags used to mark differences in the buffers.
224 ;; These function definitions need to be up here, because they are used
226 (defun emerge-new-flags ()
227 "Function to be called after `emerge-{before,after}-flag'.
228 This is called after these functions are changed to compute values that
229 depend on the flags."
230 (setq emerge-before-flag-length
(length emerge-before-flag
))
231 (setq emerge-before-flag-lines
232 (emerge-count-matches-string emerge-before-flag
"\n"))
233 (setq emerge-before-flag-match
(regexp-quote emerge-before-flag
))
234 (setq emerge-after-flag-length
(length emerge-after-flag
))
235 (setq emerge-after-flag-lines
236 (emerge-count-matches-string emerge-after-flag
"\n"))
237 (setq emerge-after-flag-match
(regexp-quote emerge-after-flag
)))
239 (defun emerge-count-matches-string (string regexp
)
240 "Return the number of matches in STRING for REGEXP."
243 (while (string-match regexp string i
)
244 (setq count
(1+ count
))
245 (setq i
(match-end 0)))
248 ;; Calculate dependent variables
251 (defcustom emerge-min-visible-lines
3
252 "Number of lines that we want to show above and below the flags when we are
253 displaying a difference."
257 (defcustom emerge-temp-file-prefix
258 (expand-file-name "emerge" temporary-file-directory
)
259 "Prefix to put on Emerge temporary file names.
260 Do not start with `~/' or `~USERNAME/'."
264 (make-obsolete-variable 'emerge-temp-file-prefix
265 "customize `temporary-file-directory' instead."
268 (defcustom emerge-temp-file-mode
384 ; u=rw only
269 "Mode for Emerge temporary files."
273 (make-obsolete-variable 'emerge-temp-file-mode
274 "it has no effect, temporary files are always private."
277 (defcustom emerge-combine-versions-template
278 "#ifdef NEW\n%b#else /* not NEW */\n%a#endif /* not NEW */\n"
279 "Template for `emerge-combine-versions' to combine the two versions.
280 The template is inserted as a string, with the following interpolations:
281 %a the A version of the difference
282 %b the B version of the difference
284 Don't forget to end the template with a newline.
285 Note that this variable can be made local to a particular merge buffer by
286 giving a prefix argument to `emerge-set-combine-versions-template'."
292 (defvar emerge-basic-keymap nil
293 "Keymap of Emerge commands.
294 Directly available in `fast' mode;
295 must be prefixed by \\<emerge-fast-keymap>\\[emerge-basic-keymap] in `edit' mode.")
297 (defvar emerge-fast-keymap nil
298 "Local keymap used in Emerge `fast' mode.
299 Makes Emerge commands directly available.")
301 (defvar emerge-options-menu
302 (make-sparse-keymap "Options"))
304 (defvar emerge-merge-menu
305 (make-sparse-keymap "Merge"))
307 (defvar emerge-move-menu
308 (make-sparse-keymap "Move"))
310 (defcustom emerge-command-prefix
"\C-c\C-c"
311 "Command prefix for Emerge commands in `edit' mode.
312 Must be set before Emerge is loaded."
316 ;; This function sets up the fixed keymaps. It is executed when the first
317 ;; Emerge is done to allow the user maximum time to set up the global keymap.
318 (defun emerge-setup-fixed-keymaps ()
319 ;; Set up the basic keymap
320 (setq emerge-basic-keymap
(make-keymap))
321 (suppress-keymap emerge-basic-keymap
) ; this sets 0..9 to digit-argument and
322 ; - to negative-argument
323 (define-key emerge-basic-keymap
"p" 'emerge-previous-difference
)
324 (define-key emerge-basic-keymap
"n" 'emerge-next-difference
)
325 (define-key emerge-basic-keymap
"a" 'emerge-select-A
)
326 (define-key emerge-basic-keymap
"b" 'emerge-select-B
)
327 (define-key emerge-basic-keymap
"j" 'emerge-jump-to-difference
)
328 (define-key emerge-basic-keymap
"." 'emerge-find-difference
)
329 (define-key emerge-basic-keymap
"q" 'emerge-quit
)
330 (define-key emerge-basic-keymap
"\C-]" 'emerge-abort
)
331 (define-key emerge-basic-keymap
"f" 'emerge-fast-mode
)
332 (define-key emerge-basic-keymap
"e" 'emerge-edit-mode
)
333 (define-key emerge-basic-keymap
"s" nil
)
334 (define-key emerge-basic-keymap
"sa" 'emerge-auto-advance
)
335 (define-key emerge-basic-keymap
"ss" 'emerge-skip-prefers
)
336 (define-key emerge-basic-keymap
"l" 'emerge-recenter
)
337 (define-key emerge-basic-keymap
"d" nil
)
338 (define-key emerge-basic-keymap
"da" 'emerge-default-A
)
339 (define-key emerge-basic-keymap
"db" 'emerge-default-B
)
340 (define-key emerge-basic-keymap
"c" nil
)
341 (define-key emerge-basic-keymap
"ca" 'emerge-copy-as-kill-A
)
342 (define-key emerge-basic-keymap
"cb" 'emerge-copy-as-kill-B
)
343 (define-key emerge-basic-keymap
"i" nil
)
344 (define-key emerge-basic-keymap
"ia" 'emerge-insert-A
)
345 (define-key emerge-basic-keymap
"ib" 'emerge-insert-B
)
346 (define-key emerge-basic-keymap
"m" 'emerge-mark-difference
)
347 (define-key emerge-basic-keymap
"v" 'emerge-scroll-up
)
348 (define-key emerge-basic-keymap
"^" 'emerge-scroll-down
)
349 (define-key emerge-basic-keymap
"<" 'emerge-scroll-left
)
350 (define-key emerge-basic-keymap
">" 'emerge-scroll-right
)
351 (define-key emerge-basic-keymap
"|" 'emerge-scroll-reset
)
352 (define-key emerge-basic-keymap
"x" nil
)
353 (define-key emerge-basic-keymap
"x1" 'emerge-one-line-window
)
354 (define-key emerge-basic-keymap
"xc" 'emerge-combine-versions
)
355 (define-key emerge-basic-keymap
"xC" 'emerge-combine-versions-register
)
356 (define-key emerge-basic-keymap
"xf" 'emerge-file-names
)
357 (define-key emerge-basic-keymap
"xj" 'emerge-join-differences
)
358 (define-key emerge-basic-keymap
"xl" 'emerge-line-numbers
)
359 (define-key emerge-basic-keymap
"xm" 'emerge-set-merge-mode
)
360 (define-key emerge-basic-keymap
"xs" 'emerge-split-difference
)
361 (define-key emerge-basic-keymap
"xt" 'emerge-trim-difference
)
362 (define-key emerge-basic-keymap
"xx" 'emerge-set-combine-versions-template
)
363 ;; Allow emerge-basic-keymap to be referenced indirectly
364 (fset 'emerge-basic-keymap emerge-basic-keymap
)
365 ;; Set up the fast mode keymap
366 (setq emerge-fast-keymap
(copy-keymap emerge-basic-keymap
))
367 ;; Allow prefixed commands to work in fast mode
368 (define-key emerge-fast-keymap emerge-command-prefix
'emerge-basic-keymap
)
369 ;; Allow emerge-fast-keymap to be referenced indirectly
370 (fset 'emerge-fast-keymap emerge-fast-keymap
)
371 ;; Suppress write-file and save-buffer
372 (define-key emerge-fast-keymap
[remap write-file
] 'emerge-query-write-file
)
373 (define-key emerge-fast-keymap
[remap save-buffer
] 'emerge-query-save-buffer
)
375 (define-key emerge-basic-keymap
[menu-bar
] (make-sparse-keymap))
377 (define-key emerge-fast-keymap
[menu-bar emerge-options
]
378 (cons "Merge-Options" emerge-options-menu
))
379 (define-key emerge-fast-keymap
[menu-bar merge
]
380 (cons "Merge" emerge-merge-menu
))
381 (define-key emerge-fast-keymap
[menu-bar move
]
382 (cons "Move" emerge-move-menu
))
384 (define-key emerge-move-menu
[emerge-scroll-reset
]
385 '("Scroll Reset" . emerge-scroll-reset
))
386 (define-key emerge-move-menu
[emerge-scroll-right
]
387 '("Scroll Right" . emerge-scroll-right
))
388 (define-key emerge-move-menu
[emerge-scroll-left
]
389 '("Scroll Left" . emerge-scroll-left
))
390 (define-key emerge-move-menu
[emerge-scroll-down
]
391 '("Scroll Down" . emerge-scroll-down
))
392 (define-key emerge-move-menu
[emerge-scroll-up
]
393 '("Scroll Up" . emerge-scroll-up
))
394 (define-key emerge-move-menu
[emerge-recenter
]
395 '("Recenter" . emerge-recenter
))
396 (define-key emerge-move-menu
[emerge-mark-difference
]
397 '("Mark Difference" . emerge-mark-difference
))
398 (define-key emerge-move-menu
[emerge-jump-to-difference
]
399 '("Jump To Difference" . emerge-jump-to-difference
))
400 (define-key emerge-move-menu
[emerge-find-difference
]
401 '("Find Difference" . emerge-find-difference
))
402 (define-key emerge-move-menu
[emerge-previous-difference
]
403 '("Previous Difference" . emerge-previous-difference
))
404 (define-key emerge-move-menu
[emerge-next-difference
]
405 '("Next Difference" . emerge-next-difference
))
408 (define-key emerge-options-menu
[emerge-one-line-window
]
409 '("One Line Window" . emerge-one-line-window
))
410 (define-key emerge-options-menu
[emerge-set-merge-mode
]
411 '("Set Merge Mode..." . emerge-set-merge-mode
))
412 (define-key emerge-options-menu
[emerge-set-combine-template
]
413 '("Set Combine Template..." . emerge-set-combine-template
))
414 (define-key emerge-options-menu
[emerge-default-B
]
415 '("Default B" . emerge-default-B
))
416 (define-key emerge-options-menu
[emerge-default-A
]
417 '("Default A" . emerge-default-A
))
418 (define-key emerge-options-menu
[emerge-skip-prefers
]
419 '(menu-item "Skip Prefers" emerge-skip-prefers
420 :button
(:toggle . emerge-skip-prefers
)))
421 (define-key emerge-options-menu
[emerge-auto-advance
]
422 '(menu-item "Auto Advance" emerge-auto-advance
423 :button
(:toggle . emerge-auto-advance
)))
424 (define-key emerge-options-menu
[emerge-edit-mode
]
425 '(menu-item "Edit Mode" emerge-edit-mode
:enable
(not emerge-edit-mode
)))
426 (define-key emerge-options-menu
[emerge-fast-mode
]
427 '(menu-item "Fast Mode" emerge-fast-mode
:enable
(not emerge-fast-mode
)))
429 (define-key emerge-merge-menu
[emerge-abort
] '("Abort" . emerge-abort
))
430 (define-key emerge-merge-menu
[emerge-quit
] '("Quit" . emerge-quit
))
431 (define-key emerge-merge-menu
[emerge-split-difference
]
432 '("Split Difference" . emerge-split-difference
))
433 (define-key emerge-merge-menu
[emerge-join-differences
]
434 '("Join Differences" . emerge-join-differences
))
435 (define-key emerge-merge-menu
[emerge-trim-difference
]
436 '("Trim Difference" . emerge-trim-difference
))
437 (define-key emerge-merge-menu
[emerge-combine-versions
]
438 '("Combine Versions" . emerge-combine-versions
))
439 (define-key emerge-merge-menu
[emerge-copy-as-kill-B
]
440 '("Copy B as Kill" . emerge-copy-as-kill-B
))
441 (define-key emerge-merge-menu
[emerge-copy-as-kill-A
]
442 '("Copy A as Kill" . emerge-copy-as-kill-A
))
443 (define-key emerge-merge-menu
[emerge-insert-B
]
444 '("Insert B" . emerge-insert-B
))
445 (define-key emerge-merge-menu
[emerge-insert-A
]
446 '("Insert A" . emerge-insert-A
))
447 (define-key emerge-merge-menu
[emerge-select-B
]
448 '("Select B" . emerge-select-B
))
449 (define-key emerge-merge-menu
[emerge-select-A
]
450 '("Select A" . emerge-select-A
)))
453 ;; Variables which control each merge. They are local to the merge buffer.
456 (emerge-defvar-local emerge-mode nil
457 "Indicator for emerge-mode.")
458 (emerge-defvar-local emerge-fast-mode nil
459 "Indicator for emerge-mode fast submode.")
460 (emerge-defvar-local emerge-edit-mode nil
461 "Indicator for emerge-mode edit submode.")
462 (emerge-defvar-local emerge-A-buffer nil
463 "The buffer in which the A variant is stored.")
464 (emerge-defvar-local emerge-B-buffer nil
465 "The buffer in which the B variant is stored.")
466 (emerge-defvar-local emerge-merge-buffer nil
467 "The buffer in which the merged file is manipulated.")
468 (emerge-defvar-local emerge-ancestor-buffer nil
469 "The buffer in which the ancestor variant is stored,
470 or nil if there is none.")
472 (defconst emerge-saved-variables
473 '((buffer-modified-p set-buffer-modified-p
)
475 buffer-auto-save-file-name
)
476 "Variables and properties of a buffer which are saved, modified and restored
478 (defconst emerge-merging-values
'(nil t nil
)
479 "Values to be assigned to emerge-saved-variables during a merge.")
481 (emerge-defvar-local emerge-A-buffer-values nil
482 "Remembers emerge-saved-variables for emerge-A-buffer.")
483 (emerge-defvar-local emerge-B-buffer-values nil
484 "Remembers emerge-saved-variables for emerge-B-buffer.")
486 (emerge-defvar-local emerge-difference-list nil
487 "Vector of differences between the variants, and markers in the buffers to
488 show where they are. Each difference is represented by a vector of seven
489 elements. The first two are markers to the beginning and end of the difference
490 section in the A buffer, the second two are markers for the B buffer, the third
491 two are markers for the merge buffer, and the last element is the \"state\" of
492 that difference in the merge buffer.
493 A section of a buffer is described by two markers, one to the beginning of
494 the first line of the section, and one to the beginning of the first line
495 after the section. (If the section is empty, both markers point to the same
496 point.) If the section is part of the selected difference, then the markers
497 are moved into the flags, so the user can edit the section without disturbing
500 A the merge buffer currently contains the A variant
501 B the merge buffer currently contains the B variant
502 default-A the merge buffer contains the A variant by default,
503 but this difference hasn't been selected yet, so
504 change-default commands can alter it
505 default-B the merge buffer contains the B variant by default,
506 but this difference hasn't been selected yet, so
507 change-default commands can alter it
508 prefer-A in a three-file merge, the A variant is the preferred
510 prefer-B in a three-file merge, the B variant is the preferred
512 (emerge-defvar-local emerge-current-difference -
1
513 "The difference that is currently selected.")
514 (emerge-defvar-local emerge-number-of-differences nil
515 "Number of differences found.")
516 (emerge-defvar-local emerge-edit-keymap nil
517 "The local keymap for the merge buffer, with the emerge commands defined in
518 it. Used to save the local keymap during fast mode, when the local keymap is
519 replaced by emerge-fast-keymap.")
520 (emerge-defvar-local emerge-old-keymap nil
521 "The original local keymap for the merge buffer.")
522 (emerge-defvar-local emerge-auto-advance nil
523 "If non-nil, emerge-select-A and emerge-select-B automatically advance to
524 the next difference.")
525 (emerge-defvar-local emerge-skip-prefers nil
526 "If non-nil, differences for which there is a preference are automatically
528 (emerge-defvar-local emerge-quit-hook nil
529 "Hooks to run in the merge buffer after the merge has been finished.
530 `emerge-prefix-argument' will hold the prefix argument of the `emerge-quit'
532 This is *not* a user option, since Emerge uses it for its own processing.")
533 (emerge-defvar-local emerge-output-description nil
534 "Describes output destination of emerge, for `emerge-file-names'.")
536 ;;; Setup functions for two-file mode.
538 (defun emerge-files-internal (file-A file-B
&optional startup-hooks quit-hooks
540 (if (not (file-readable-p file-A
))
541 (error "File `%s' does not exist or is not readable" file-A
))
542 (if (not (file-readable-p file-B
))
543 (error "File `%s' does not exist or is not readable" file-B
))
544 (let ((buffer-A (find-file-noselect file-A
))
545 (buffer-B (find-file-noselect file-B
)))
546 ;; Record the directories of the files
547 (setq emerge-last-dir-A
(file-name-directory file-A
))
548 (setq emerge-last-dir-B
(file-name-directory file-B
))
550 (setq emerge-last-dir-output
(file-name-directory output-file
)))
551 ;; Make sure the entire files are seen, and they reflect what is on disk
555 (let ((temp (file-local-copy file-A
)))
559 (cons `(lambda () (delete-file ,file-A
))
561 ;; Verify that the file matches the buffer
562 (emerge-verify-file-buffer))))
566 (let ((temp (file-local-copy file-B
)))
570 (cons `(lambda () (delete-file ,file-B
))
572 ;; Verify that the file matches the buffer
573 (emerge-verify-file-buffer))))
574 (emerge-setup buffer-A file-A buffer-B file-B startup-hooks quit-hooks
577 ;; Start up Emerge on two files
578 (defun emerge-setup (buffer-A file-A buffer-B file-B startup-hooks quit-hooks
580 (setq file-A
(expand-file-name file-A
))
581 (setq file-B
(expand-file-name file-B
))
582 (setq output-file
(and output-file
(expand-file-name output-file
)))
583 (let* ((merge-buffer-name (emerge-unique-buffer-name "*merge" "*"))
584 ;; create the merge buffer from buffer A, so it inherits buffer A's
585 ;; default directory, etc.
586 (merge-buffer (with-current-buffer
588 (get-buffer-create merge-buffer-name
))))
591 (emerge-copy-modes buffer-A
)
592 (setq buffer-read-only nil
)
595 (setq emerge-A-buffer buffer-A
)
596 (setq emerge-B-buffer buffer-B
)
597 (setq emerge-ancestor-buffer nil
)
598 (setq emerge-merge-buffer merge-buffer
)
599 (setq emerge-output-description
601 (concat "Output to file: " output-file
)
602 (concat "Output to buffer: " (buffer-name merge-buffer
))))
603 (save-excursion (insert-buffer-substring emerge-A-buffer
))
605 (setq emerge-difference-list
(emerge-make-diff-list file-A file-B
))
606 (setq emerge-number-of-differences
(length emerge-difference-list
))
607 (setq emerge-current-difference -
1)
608 (setq emerge-quit-hook quit-hooks
)
609 (emerge-remember-buffer-characteristics)
610 (emerge-handle-local-variables))
611 (emerge-setup-windows buffer-A buffer-B merge-buffer t
)
612 (with-current-buffer merge-buffer
613 (run-hooks 'startup-hooks
'emerge-startup-hook
)
614 (setq buffer-read-only t
))))
616 ;; Generate the Emerge difference list between two files
617 (defun emerge-make-diff-list (file-A file-B
)
618 (setq emerge-diff-buffer
(get-buffer-create "*emerge-diff*"))
623 (format "%s %s %s %s"
624 emerge-diff-program emerge-diff-options
625 (emerge-protect-metachars file-A
)
626 (emerge-protect-metachars file-B
))
628 (emerge-prepare-error-list emerge-diff-ok-lines-regexp
)
629 (emerge-convert-diffs-to-markers
630 emerge-A-buffer emerge-B-buffer emerge-merge-buffer
631 (emerge-extract-diffs emerge-diff-buffer
)))
633 (defun emerge-extract-diffs (diff-buffer)
637 (goto-char (point-min))
638 (while (re-search-forward emerge-match-diff-line nil t
)
639 (let* ((a-begin (string-to-number (buffer-substring (match-beginning 1)
641 (a-end (let ((b (match-beginning 3))
644 (string-to-number (buffer-substring b e
))
646 (diff-type (buffer-substring (match-beginning 4) (match-end 4)))
647 (b-begin (string-to-number (buffer-substring (match-beginning 5)
649 (b-end (let ((b (match-beginning 7))
652 (string-to-number (buffer-substring b e
))
654 ;; fix the beginning and end numbers, because diff is somewhat
655 ;; strange about how it numbers lines
656 (if (string-equal diff-type
"a")
658 (setq b-end
(1+ b-end
))
659 (setq a-begin
(1+ a-begin
))
660 (setq a-end a-begin
))
661 (if (string-equal diff-type
"d")
663 (setq a-end
(1+ a-end
))
664 (setq b-begin
(1+ b-begin
))
665 (setq b-end b-begin
))
666 ;; (string-equal diff-type "c")
668 (setq a-end
(1+ a-end
))
669 (setq b-end
(1+ b-end
)))))
670 (setq list
(cons (vector a-begin a-end
676 ;; Set up buffer of diff/diff3 error messages.
677 (defun emerge-prepare-error-list (ok-regexp)
678 (setq emerge-diff-error-buffer
(get-buffer-create "*emerge-diff-errors*"))
680 emerge-diff-error-buffer
682 (save-excursion (insert-buffer-substring emerge-diff-buffer
))
683 (delete-matching-lines ok-regexp
)))
685 ;;; Top-level and setup functions for three-file mode.
687 (defun emerge-files-with-ancestor-internal (file-A file-B file-ancestor
688 &optional startup-hooks quit-hooks
690 (if (not (file-readable-p file-A
))
691 (error "File `%s' does not exist or is not readable" file-A
))
692 (if (not (file-readable-p file-B
))
693 (error "File `%s' does not exist or is not readable" file-B
))
694 (if (not (file-readable-p file-ancestor
))
695 (error "File `%s' does not exist or is not readable" file-ancestor
))
696 (let ((buffer-A (find-file-noselect file-A
))
697 (buffer-B (find-file-noselect file-B
))
698 (buffer-ancestor (find-file-noselect file-ancestor
)))
699 ;; Record the directories of the files
700 (setq emerge-last-dir-A
(file-name-directory file-A
))
701 (setq emerge-last-dir-B
(file-name-directory file-B
))
702 (setq emerge-last-dir-ancestor
(file-name-directory file-ancestor
))
704 (setq emerge-last-dir-output
(file-name-directory output-file
)))
705 ;; Make sure the entire files are seen, and they reflect what is on disk
709 (let ((temp (file-local-copy file-A
)))
713 (cons `(lambda () (delete-file ,file-A
))
715 ;; Verify that the file matches the buffer
716 (emerge-verify-file-buffer))))
720 (let ((temp (file-local-copy file-B
)))
724 (cons `(lambda () (delete-file ,file-B
))
726 ;; Verify that the file matches the buffer
727 (emerge-verify-file-buffer))))
731 (let ((temp (file-local-copy file-ancestor
)))
733 (setq file-ancestor temp
735 (cons `(lambda () (delete-file ,file-ancestor
))
737 ;; Verify that the file matches the buffer
738 (emerge-verify-file-buffer))))
739 (emerge-setup-with-ancestor buffer-A file-A buffer-B file-B
740 buffer-ancestor file-ancestor
741 startup-hooks quit-hooks output-file
)))
743 ;; Start up Emerge on two files with an ancestor
744 (defun emerge-setup-with-ancestor (buffer-A file-A buffer-B file-B
745 buffer-ancestor file-ancestor
746 &optional startup-hooks quit-hooks
748 (setq file-A
(expand-file-name file-A
))
749 (setq file-B
(expand-file-name file-B
))
750 (setq file-ancestor
(expand-file-name file-ancestor
))
751 (setq output-file
(and output-file
(expand-file-name output-file
)))
752 (let* ((merge-buffer-name (emerge-unique-buffer-name "*merge" "*"))
753 ;; create the merge buffer from buffer A, so it inherits buffer A's
754 ;; default directory, etc.
755 (merge-buffer (with-current-buffer
757 (get-buffer-create merge-buffer-name
))))
760 (emerge-copy-modes buffer-A
)
761 (setq buffer-read-only nil
)
764 (setq emerge-A-buffer buffer-A
)
765 (setq emerge-B-buffer buffer-B
)
766 (setq emerge-ancestor-buffer buffer-ancestor
)
767 (setq emerge-merge-buffer merge-buffer
)
768 (setq emerge-output-description
770 (concat "Output to file: " output-file
)
771 (concat "Output to buffer: " (buffer-name merge-buffer
))))
772 (save-excursion (insert-buffer-substring emerge-A-buffer
))
774 (setq emerge-difference-list
775 (emerge-make-diff3-list file-A file-B file-ancestor
))
776 (setq emerge-number-of-differences
(length emerge-difference-list
))
777 (setq emerge-current-difference -
1)
778 (setq emerge-quit-hook quit-hooks
)
779 (emerge-remember-buffer-characteristics)
780 (emerge-select-prefer-Bs)
781 (emerge-handle-local-variables))
782 (emerge-setup-windows buffer-A buffer-B merge-buffer t
)
783 (with-current-buffer merge-buffer
784 (run-hooks 'startup-hooks
'emerge-startup-hook
)
785 (setq buffer-read-only t
))))
787 ;; Generate the Emerge difference list between two files with an ancestor
788 (defun emerge-make-diff3-list (file-A file-B file-ancestor
)
789 (setq emerge-diff-buffer
(get-buffer-create "*emerge-diff*"))
794 (format "%s %s %s %s %s"
795 emerge-diff3-program emerge-diff-options
796 (emerge-protect-metachars file-A
)
797 (emerge-protect-metachars file-ancestor
)
798 (emerge-protect-metachars file-B
))
800 (emerge-prepare-error-list emerge-diff3-ok-lines-regexp
)
801 (emerge-convert-diffs-to-markers
802 emerge-A-buffer emerge-B-buffer emerge-merge-buffer
803 (emerge-extract-diffs3 emerge-diff-buffer
)))
805 (defun emerge-extract-diffs3 (diff-buffer)
809 (while (re-search-forward "^====\\(.?\\)$" nil t
)
810 ;; leave point after matched line
811 (beginning-of-line 2)
812 (let ((agreement (buffer-substring (match-beginning 1) (match-end 1))))
813 ;; if the A and B files are the same, ignore the difference
814 (if (not (string-equal agreement
"2"))
817 (let (group-1 group-3 pos
)
819 (setq group-1
(emerge-get-diff3-group "1"))
821 (setq group-3
(emerge-get-diff3-group "3"))
822 (vector (car group-1
) (car (cdr group-1
))
823 (car group-3
) (car (cdr group-3
))
824 (cond ((string-equal agreement
"1") 'prefer-A
)
825 ((string-equal agreement
"3") 'prefer-B
)
830 (defun emerge-get-diff3-group (file)
831 ;; This save-excursion allows emerge-get-diff3-group to be called for the
832 ;; various groups of lines (1, 2, 3) in any order, and for the lines to
833 ;; appear in any order. The reason this is necessary is that Gnu diff3
834 ;; can produce the groups in the order 1, 2, 3 or 1, 3, 2.
837 (concat "^" file
":\\([0-9]+\\)\\(,\\([0-9]+\\)\\)?\\([ac]\\)$"))
838 (beginning-of-line 2)
839 ;; treatment depends on whether it is an "a" group or a "c" group
840 (if (string-equal (buffer-substring (match-beginning 4) (match-end 4)) "c")
842 (if (match-beginning 2)
843 ;; it has two numbers
844 (list (string-to-number
845 (buffer-substring (match-beginning 1) (match-end 1)))
846 (1+ (string-to-number
847 (buffer-substring (match-beginning 3) (match-end 3)))))
849 (let ((x (string-to-number
850 (buffer-substring (match-beginning 1) (match-end 1)))))
852 ;; it is an "a" group
853 (let ((x (1+ (string-to-number
854 (buffer-substring (match-beginning 1) (match-end 1))))))
857 ;;; Functions to start Emerge on files
860 (defun emerge-files (_arg file-A file-B file-out
&optional startup-hooks
862 "Run Emerge on two files."
865 (list current-prefix-arg
866 (setq f
(emerge-read-file-name "File A to merge" emerge-last-dir-A
868 (emerge-read-file-name "File B to merge" emerge-last-dir-B nil f t
)
869 (and current-prefix-arg
870 (emerge-read-file-name "Output file" emerge-last-dir-output
873 (add-hook 'quit-hooks
`(lambda () (emerge-files-exit ,file-out
))))
874 (emerge-files-internal
875 file-A file-B startup-hooks
880 (defun emerge-files-with-ancestor (_arg file-A file-B file-ancestor file-out
881 &optional startup-hooks quit-hooks
)
882 "Run Emerge on two files, giving another file as the ancestor."
885 (list current-prefix-arg
886 (setq f
(emerge-read-file-name "File A to merge" emerge-last-dir-A
888 (emerge-read-file-name "File B to merge" emerge-last-dir-B nil f t
)
889 (emerge-read-file-name "Ancestor file" emerge-last-dir-ancestor
891 (and current-prefix-arg
892 (emerge-read-file-name "Output file" emerge-last-dir-output
895 (add-hook 'quit-hooks
`(lambda () (emerge-files-exit ,file-out
))))
896 (emerge-files-with-ancestor-internal
897 file-A file-B file-ancestor startup-hooks
901 ;; Write the merge buffer out in place of the file the A buffer is visiting.
902 (defun emerge-files-exit (file-out)
903 ;; if merge was successful was given, save to disk
904 (if (not emerge-prefix-argument
)
905 (emerge-write-and-delete file-out
)))
907 ;;; Functions to start Emerge on buffers
910 (defun emerge-buffers (buffer-A buffer-B
&optional startup-hooks quit-hooks
)
911 "Run Emerge on two buffers."
912 (interactive "bBuffer A to merge: \nbBuffer B to merge: ")
913 (let ((emerge-file-A (emerge-make-temp-file "A"))
914 (emerge-file-B (emerge-make-temp-file "B")))
917 (write-region (point-min) (point-max) emerge-file-A nil
'no-message
))
920 (write-region (point-min) (point-max) emerge-file-B nil
'no-message
))
921 (emerge-setup (get-buffer buffer-A
) emerge-file-A
922 (get-buffer buffer-B
) emerge-file-B
924 (delete-file ,emerge-file-A
)
925 (delete-file ,emerge-file-B
))
931 (defun emerge-buffers-with-ancestor (buffer-A buffer-B buffer-ancestor
932 &optional startup-hooks
934 "Run Emerge on two buffers, giving another buffer as the ancestor."
936 "bBuffer A to merge: \nbBuffer B to merge: \nbAncestor buffer: ")
937 (let ((emerge-file-A (emerge-make-temp-file "A"))
938 (emerge-file-B (emerge-make-temp-file "B"))
939 (emerge-file-ancestor (emerge-make-temp-file "anc")))
942 (write-region (point-min) (point-max) emerge-file-A nil
'no-message
))
945 (write-region (point-min) (point-max) emerge-file-B nil
'no-message
))
948 (write-region (point-min) (point-max) emerge-file-ancestor nil
950 (emerge-setup-with-ancestor (get-buffer buffer-A
) emerge-file-A
951 (get-buffer buffer-B
) emerge-file-B
952 (get-buffer buffer-ancestor
)
955 (delete-file ,emerge-file-A
)
956 (delete-file ,emerge-file-B
)
958 ,emerge-file-ancestor
))
963 ;;; Functions to start Emerge from the command line
966 (defun emerge-files-command ()
967 (let ((file-a (nth 0 command-line-args-left
))
968 (file-b (nth 1 command-line-args-left
))
969 (file-out (nth 2 command-line-args-left
)))
970 (setq command-line-args-left
(nthcdr 3 command-line-args-left
))
971 (emerge-files-internal
973 (list `(lambda () (emerge-command-exit ,file-out
))))))
976 (defun emerge-files-with-ancestor-command ()
977 (let (file-a file-b file-anc file-out
)
978 ;; check for a -a flag, for filemerge compatibility
979 (if (string= (car command-line-args-left
) "-a")
980 ;; arguments are "-a ancestor file-a file-b file-out"
982 (setq file-a
(nth 2 command-line-args-left
))
983 (setq file-b
(nth 3 command-line-args-left
))
984 (setq file-anc
(nth 1 command-line-args-left
))
985 (setq file-out
(nth 4 command-line-args-left
))
986 (setq command-line-args-left
(nthcdr 5 command-line-args-left
)))
987 ;; arguments are "file-a file-b ancestor file-out"
988 (setq file-a
(nth 0 command-line-args-left
))
989 (setq file-b
(nth 1 command-line-args-left
))
990 (setq file-anc
(nth 2 command-line-args-left
))
991 (setq file-out
(nth 3 command-line-args-left
))
992 (setq command-line-args-left
(nthcdr 4 command-line-args-left
)))
993 (emerge-files-with-ancestor-internal
994 file-a file-b file-anc nil
995 (list `(lambda () (emerge-command-exit ,file-out
))))))
997 (defun emerge-command-exit (file-out)
998 (emerge-write-and-delete file-out
)
999 (kill-emacs (if emerge-prefix-argument
1 0)))
1001 ;;; Functions to start Emerge via remote request
1004 (defun emerge-files-remote (file-a file-b file-out
)
1005 (setq emerge-file-out file-out
)
1006 (emerge-files-internal
1008 (list `(lambda () (emerge-remote-exit ,file-out
',emerge-exit-func
)))
1010 (throw 'client-wait nil
))
1013 (defun emerge-files-with-ancestor-remote (file-a file-b file-anc file-out
)
1014 (setq emerge-file-out file-out
)
1015 (emerge-files-with-ancestor-internal
1016 file-a file-b file-anc nil
1017 (list `(lambda () (emerge-remote-exit ,file-out
',emerge-exit-func
)))
1019 (throw 'client-wait nil
))
1021 (defun emerge-remote-exit (file-out emerge-exit-func
)
1022 (emerge-write-and-delete file-out
)
1023 (kill-buffer emerge-merge-buffer
)
1024 (funcall emerge-exit-func
(if emerge-prefix-argument
1 0)))
1026 ;;; Functions to start Emerge on RCS versions
1029 (defun emerge-revisions (arg file revision-A revision-B
1030 &optional startup-hooks quit-hooks
)
1031 "Emerge two RCS revisions of a file."
1033 (list current-prefix-arg
1034 (read-file-name "File to merge: " nil nil
'confirm
)
1035 (read-string "Revision A to merge: " emerge-last-revision-A
)
1036 (read-string "Revision B to merge: " emerge-last-revision-B
)))
1037 (setq emerge-last-revision-A revision-A
1038 emerge-last-revision-B revision-B
)
1039 (emerge-revisions-internal
1040 file revision-A revision-B startup-hooks
1044 ,(format "%s %s" emerge-rcs-ci-program file
)))
1049 (defun emerge-revisions-with-ancestor (arg file revision-A
1052 startup-hooks quit-hooks
)
1053 "Emerge two RCS revisions of a file, with another revision as ancestor."
1055 (list current-prefix-arg
1056 (read-file-name "File to merge: " nil nil
'confirm
)
1057 (read-string "Revision A to merge: " emerge-last-revision-A
)
1058 (read-string "Revision B to merge: " emerge-last-revision-B
)
1059 (read-string "Ancestor: " emerge-last-revision-ancestor
)))
1060 (setq emerge-last-revision-A revision-A
1061 emerge-last-revision-B revision-B
1062 emerge-last-revision-ancestor ancestor
)
1063 (emerge-revision-with-ancestor-internal
1064 file revision-A revision-B ancestor startup-hooks
1069 ,(format "%s %s" emerge-rcs-ci-program file
)))
1073 (defun emerge-revisions-internal (file revision-A revision-B
&optional
1074 startup-hooks quit-hooks _output-file
)
1075 (let ((buffer-A (get-buffer-create (format "%s,%s" file revision-A
)))
1076 (buffer-B (get-buffer-create (format "%s,%s" file revision-B
)))
1077 (emerge-file-A (emerge-make-temp-file "A"))
1078 (emerge-file-B (emerge-make-temp-file "B")))
1079 ;; Get the revisions into buffers
1080 (with-current-buffer
1084 (format "%s -q -p%s %s" emerge-rcs-co-program revision-A file
)
1086 (write-region (point-min) (point-max) emerge-file-A nil
'no-message
)
1087 (set-buffer-modified-p nil
))
1088 (with-current-buffer
1092 (format "%s -q -p%s %s" emerge-rcs-co-program revision-B file
)
1094 (write-region (point-min) (point-max) emerge-file-B nil
'no-message
)
1095 (set-buffer-modified-p nil
))
1097 (emerge-setup buffer-A emerge-file-A
1098 buffer-B emerge-file-B
1100 (delete-file ,emerge-file-A
)
1101 (delete-file ,emerge-file-B
))
1103 (cons `(lambda () (emerge-files-exit ,file
))
1107 (defun emerge-revision-with-ancestor-internal (file revision-A revision-B
1109 &optional startup-hooks
1110 quit-hooks output-file
)
1111 (let ((buffer-A (get-buffer-create (format "%s,%s" file revision-A
)))
1112 (buffer-B (get-buffer-create (format "%s,%s" file revision-B
)))
1113 (buffer-ancestor (get-buffer-create (format "%s,%s" file ancestor
)))
1114 (emerge-file-A (emerge-make-temp-file "A"))
1115 (emerge-file-B (emerge-make-temp-file "B"))
1116 (emerge-ancestor (emerge-make-temp-file "ancestor")))
1117 ;; Get the revisions into buffers
1118 (with-current-buffer
1122 (format "%s -q -p%s %s" emerge-rcs-co-program
1125 (write-region (point-min) (point-max) emerge-file-A nil
'no-message
)
1126 (set-buffer-modified-p nil
))
1127 (with-current-buffer
1131 (format "%s -q -p%s %s" emerge-rcs-co-program revision-B file
)
1133 (write-region (point-min) (point-max) emerge-file-B nil
'no-message
)
1134 (set-buffer-modified-p nil
))
1135 (with-current-buffer
1139 (format "%s -q -p%s %s" emerge-rcs-co-program ancestor file
)
1141 (write-region (point-min) (point-max) emerge-ancestor nil
'no-message
)
1142 (set-buffer-modified-p nil
))
1144 (emerge-setup-with-ancestor
1145 buffer-A emerge-file-A buffer-B emerge-file-B
1146 buffer-ancestor emerge-ancestor
1148 (delete-file ,emerge-file-A
)
1149 (delete-file ,emerge-file-B
)
1150 (delete-file ,emerge-ancestor
))
1152 (cons `(lambda () (emerge-files-exit ,file
))
1156 ;;; Function to start Emerge based on a line in a file
1158 (defun emerge-execute-line ()
1159 "Run Emerge using files named in current text line.
1160 Looks in that line for whitespace-separated entries of these forms:
1165 to specify the files to use in Emerge.
1167 In addition, if only one of `a=file' or `b=file' is present, and `output=file'
1169 If `emerge-execute-line-deletions' is non-nil and `ancestor=file' is present,
1170 it is assumed that the file in question has been deleted, and it is
1171 not copied to the output file.
1172 Otherwise, the A or B file present is copied to the output file."
1174 (let (file-A file-B file-ancestor file-out
1175 (case-fold-search t
))
1176 ;; Stop if at end of buffer (even though we might be in a line, if
1177 ;; the line does not end with newline)
1179 (error "At end of buffer"))
1180 ;; Go to the beginning of the line
1182 ;; Skip any initial whitespace
1183 (if (looking-at "[ \t]*")
1184 (goto-char (match-end 0)))
1185 ;; Process the entire line
1187 ;; Get the next entry
1188 (if (looking-at "\\([a-z]+\\)=\\([^ \t\n]+\\)[ \t]*")
1189 ;; Break apart the tab (before =) and the filename (after =)
1190 (let ((tag (downcase
1191 (buffer-substring (match-beginning 1) (match-end 1))))
1192 (file (buffer-substring (match-beginning 2) (match-end 2))))
1193 ;; Move point after the entry
1194 (goto-char (match-end 0))
1195 ;; Store the filename in the right variable
1197 ((string-equal tag
"a")
1199 (error "This line has two `A' entries"))
1201 ((string-equal tag
"b")
1203 (error "This line has two `B' entries"))
1205 ((or (string-equal tag
"anc") (string-equal tag
"ancestor"))
1207 (error "This line has two `ancestor' entries"))
1208 (setq file-ancestor file
))
1209 ((or (string-equal tag
"out") (string-equal tag
"output"))
1211 (error "This line has two `output' entries"))
1212 (setq file-out file
))
1214 (error "Unrecognized entry"))))
1215 ;; If the match on the entry pattern failed
1216 (error "Unparsable entry")))
1217 ;; Make sure that file-A and file-B are present
1218 (if (not (or (and file-A file-B
) file-out
))
1219 (error "Must have both `A' and `B' entries"))
1220 (if (not (or file-A file-B
))
1221 (error "Must have `A' or `B' entry"))
1222 ;; Go to the beginning of the next line, so next execution will use
1223 ;; next line in buffer.
1224 (beginning-of-line 2)
1225 ;; Execute the correct command
1227 ;; Merge of two files with ancestor
1228 ((and file-A file-B file-ancestor
)
1229 (message "Merging %s and %s..." file-A file-B
)
1230 (emerge-files-with-ancestor (not (not file-out
)) file-A file-B
1231 file-ancestor file-out
1233 ;; When done, return to this buffer.
1236 (switch-to-buffer ,(current-buffer))
1237 (message "Merge done.")))))
1238 ;; Merge of two files without ancestor
1239 ((and file-A file-B
)
1240 (message "Merging %s and %s..." file-A file-B
)
1241 (emerge-files (not (not file-out
)) file-A file-B file-out
1243 ;; When done, return to this buffer.
1246 (switch-to-buffer ,(current-buffer))
1247 (message "Merge done.")))))
1248 ;; There is an output file (or there would have been an error above),
1249 ;; but only one input file.
1250 ;; The file appears to have been deleted in one version; do nothing.
1251 ((and file-ancestor emerge-execute-line-deletions
)
1252 (message "No action."))
1253 ;; The file should be copied from the version that contains it
1254 (t (let ((input-file (or file-A file-B
)))
1255 (message "Copying...")
1256 (copy-file input-file file-out
)
1257 (message "%s copied to %s." input-file file-out
))))))
1259 ;;; Sample function for creating information for emerge-execute-line
1261 (defcustom emerge-merge-directories-filename-regexp
"[^.]"
1262 "Regexp describing files to be processed by `emerge-merge-directories'."
1267 (defun emerge-merge-directories (a-dir b-dir ancestor-dir output-dir
)
1270 (read-directory-name "A directory: " nil nil
'confirm
)
1271 (read-directory-name "B directory: " nil nil
'confirm
)
1272 (read-directory-name "Ancestor directory (null for none): " nil nil
'confirm
)
1273 (read-directory-name "Output directory (null for none): " nil nil
'confirm
)))
1274 ;; Check that we're not on a line
1275 (if (not (and (bolp) (eolp)))
1276 (error "There is text on this line"))
1277 ;; Turn null strings into nil to indicate directories not used.
1278 (if (and ancestor-dir
(string-equal ancestor-dir
""))
1279 (setq ancestor-dir nil
))
1280 (if (and output-dir
(string-equal output-dir
""))
1281 (setq output-dir nil
))
1282 ;; Canonicalize the directory names
1283 (setq a-dir
(expand-file-name a-dir
))
1284 (if (not (string-equal (substring a-dir -
1) "/"))
1285 (setq a-dir
(concat a-dir
"/")))
1286 (setq b-dir
(expand-file-name b-dir
))
1287 (if (not (string-equal (substring b-dir -
1) "/"))
1288 (setq b-dir
(concat b-dir
"/")))
1291 (setq ancestor-dir
(expand-file-name ancestor-dir
))
1292 (if (not (string-equal (substring ancestor-dir -
1) "/"))
1293 (setq ancestor-dir
(concat ancestor-dir
"/")))))
1296 (setq output-dir
(expand-file-name output-dir
))
1297 (if (not (string-equal (substring output-dir -
1) "/"))
1298 (setq output-dir
(concat output-dir
"/")))))
1299 ;; Set the mark to where we start
1301 ;; Find out what files are in the directories.
1303 (directory-files a-dir nil emerge-merge-directories-filename-regexp
))
1305 (directory-files b-dir nil emerge-merge-directories-filename-regexp
))
1308 (directory-files ancestor-dir nil
1309 emerge-merge-directories-filename-regexp
)))
1310 (all-files (sort (nconc (copy-sequence a-dir-files
)
1311 (copy-sequence b-dir-files
)
1312 (copy-sequence ancestor-dir-files
))
1313 (function string-lessp
))))
1314 ;; Remove duplicates from all-files.
1315 (let ((p all-files
))
1317 (if (and (cdr p
) (string-equal (car p
) (car (cdr p
))))
1318 (setcdr p
(cdr (cdr p
)))
1320 ;; Generate the control lines for the various files.
1322 (let ((f (car all-files
)))
1323 (setq all-files
(cdr all-files
))
1324 (if (and a-dir-files
(string-equal (car a-dir-files
) f
))
1326 (insert "A=" a-dir f
"\t")
1327 (setq a-dir-files
(cdr a-dir-files
))))
1328 (if (and b-dir-files
(string-equal (car b-dir-files
) f
))
1330 (insert "B=" b-dir f
"\t")
1331 (setq b-dir-files
(cdr b-dir-files
))))
1332 (if (and ancestor-dir-files
(string-equal (car ancestor-dir-files
) f
))
1334 (insert "ancestor=" ancestor-dir f
"\t")
1335 (setq ancestor-dir-files
(cdr ancestor-dir-files
))))
1337 (insert "output=" output-dir f
"\t"))
1338 (backward-delete-char 1)
1341 ;;; Common setup routines
1343 ;; Set up the window configuration. If POS is given, set the points to
1344 ;; the beginnings of the buffers.
1345 (defun emerge-setup-windows (buffer-A buffer-B merge-buffer
&optional pos
)
1346 ;; Make sure we are not in the minibuffer window when we try to delete
1347 ;; all other windows.
1348 (if (eq (selected-window) (minibuffer-window))
1350 (delete-other-windows)
1351 (switch-to-buffer merge-buffer
)
1352 (emerge-refresh-mode-line)
1353 (split-window-below)
1354 (split-window-right)
1355 (switch-to-buffer buffer-A
)
1357 (goto-char (point-min)))
1359 (switch-to-buffer buffer-B
)
1361 (goto-char (point-min)))
1364 (goto-char (point-min)))
1365 ;; If diff/diff3 reports errors, display them rather than the merge buffer.
1366 (if (/= 0 (with-current-buffer emerge-diff-error-buffer
(buffer-size)))
1369 (message "Errors found in diff/diff3 output. Merge buffer is %s."
1370 (buffer-name emerge-merge-buffer
))
1371 (switch-to-buffer emerge-diff-error-buffer
))))
1373 ;; Set up the keymap in the merge buffer
1374 (defun emerge-set-keys ()
1375 ;; Set up fixed keymaps if necessary
1376 (if (not emerge-basic-keymap
)
1377 (emerge-setup-fixed-keymaps))
1378 ;; Save the old local map
1379 (setq emerge-old-keymap
(current-local-map))
1380 ;; Construct the edit keymap
1381 (setq emerge-edit-keymap
(if emerge-old-keymap
1382 (copy-keymap emerge-old-keymap
)
1383 (make-sparse-keymap)))
1384 ;; Install the Emerge commands
1385 (emerge-force-define-key emerge-edit-keymap emerge-command-prefix
1386 'emerge-basic-keymap
)
1387 (define-key emerge-edit-keymap
[menu-bar
] (make-sparse-keymap))
1389 ;; Create the additional menu bar items.
1390 (define-key emerge-edit-keymap
[menu-bar emerge-options
]
1391 (cons "Merge-Options" emerge-options-menu
))
1392 (define-key emerge-edit-keymap
[menu-bar merge
]
1393 (cons "Merge" emerge-merge-menu
))
1394 (define-key emerge-edit-keymap
[menu-bar move
]
1395 (cons "Move" emerge-move-menu
))
1397 ;; Suppress write-file and save-buffer
1398 (substitute-key-definition 'write-file
1399 'emerge-query-write-file
1401 (substitute-key-definition 'save-buffer
1402 'emerge-query-save-buffer
1404 (define-key emerge-edit-keymap
[remap write-file
] 'emerge-query-write-file
)
1405 (define-key emerge-edit-keymap
[remap save-buffer
] 'emerge-query-save-buffer
)
1406 (use-local-map emerge-fast-keymap
)
1407 (setq emerge-edit-mode nil
)
1408 (setq emerge-fast-mode t
))
1410 (defun emerge-remember-buffer-characteristics ()
1411 "Record certain properties of the buffers being merged.
1412 Must be called in the merge buffer. Remembers read-only, modified,
1413 auto-save, and saves them in buffer local variables. Sets the buffers
1414 read-only and turns off `auto-save-mode'.
1415 These characteristics are restored by `emerge-restore-buffer-characteristics'."
1416 ;; force auto-save, because we will turn off auto-saving in buffers for the
1419 ;; remember and alter buffer characteristics
1420 (setq emerge-A-buffer-values
1421 (with-current-buffer
1424 (emerge-save-variables emerge-saved-variables
)
1425 (emerge-restore-variables emerge-saved-variables
1426 emerge-merging-values
))))
1427 (setq emerge-B-buffer-values
1428 (with-current-buffer
1431 (emerge-save-variables emerge-saved-variables
)
1432 (emerge-restore-variables emerge-saved-variables
1433 emerge-merging-values
)))))
1435 (defun emerge-restore-buffer-characteristics ()
1436 "Restore characteristics saved by `emerge-remember-buffer-characteristics'."
1437 (let ((A-values emerge-A-buffer-values
)
1438 (B-values emerge-B-buffer-values
))
1439 (with-current-buffer emerge-A-buffer
1440 (emerge-restore-variables emerge-saved-variables
1442 (with-current-buffer emerge-B-buffer
1443 (emerge-restore-variables emerge-saved-variables
1446 ;; Move to line DESIRED-LINE assuming we are at line CURRENT-LINE.
1447 ;; Return DESIRED-LINE.
1448 (defun emerge-goto-line (desired-line current-line
)
1449 (forward-line (- desired-line current-line
))
1452 (defun emerge-convert-diffs-to-markers (A-buffer
1457 (A-point-min (with-current-buffer A-buffer
(point-min)))
1458 (offset (1- A-point-min
))
1459 (B-point-min (with-current-buffer B-buffer
(point-min)))
1460 ;; Record current line number in each buffer
1461 ;; so we don't have to count from the beginning.
1464 (with-current-buffer A-buffer
(goto-char (point-min)))
1465 (with-current-buffer B-buffer
(goto-char (point-min)))
1467 (let* ((list-element (car lineno-list
))
1474 (a-begin (aref list-element
0))
1475 (a-end (aref list-element
1))
1476 (b-begin (aref list-element
2))
1477 (b-end (aref list-element
3))
1478 (state (aref list-element
4)))
1479 ;; place markers at the appropriate places in the buffers
1480 (with-current-buffer
1482 (setq a-line
(emerge-goto-line a-begin a-line
))
1483 (setq a-begin-marker
(point-marker))
1484 (setq a-line
(emerge-goto-line a-end a-line
))
1485 (setq a-end-marker
(point-marker)))
1486 (with-current-buffer
1488 (setq b-line
(emerge-goto-line b-begin b-line
))
1489 (setq b-begin-marker
(point-marker))
1490 (setq b-line
(emerge-goto-line b-end b-line
))
1491 (setq b-end-marker
(point-marker)))
1492 (setq merge-begin-marker
(set-marker
1494 (- (marker-position a-begin-marker
)
1497 (setq merge-end-marker
(set-marker
1499 (- (marker-position a-end-marker
)
1502 ;; record all the markers for this difference
1503 (setq marker-list
(cons (vector a-begin-marker a-end-marker
1504 b-begin-marker b-end-marker
1505 merge-begin-marker merge-end-marker
1508 (setq lineno-list
(cdr lineno-list
)))
1509 ;; convert the list of difference information into a vector for
1511 (setq emerge-difference-list
(apply 'vector
(nreverse marker-list
)))))
1513 ;; If we have an ancestor, select all B variants that we prefer
1514 (defun emerge-select-prefer-Bs ()
1516 (while (< n emerge-number-of-differences
)
1517 (if (eq (aref (aref emerge-difference-list n
) 6) 'prefer-B
)
1519 (emerge-unselect-and-select-difference n t
)
1521 (aset (aref emerge-difference-list n
) 6 'prefer-B
)))
1523 (emerge-unselect-and-select-difference -
1))
1525 ;; Process the local-variables list at the end of the merged file, if
1527 (defun emerge-handle-local-variables ()
1528 (if emerge-process-local-variables
1530 (hack-local-variables)
1531 (error (message "Local-variables error in merge buffer: %s"
1532 (prin1-to-string err
))))))
1534 ;;; Common exit routines
1536 (defun emerge-write-and-delete (file-out)
1537 ;; clear screen format
1538 (delete-other-windows)
1539 ;; delete A, B, and ancestor buffers, if they haven't been changed
1540 (if (not (buffer-modified-p emerge-A-buffer
))
1541 (kill-buffer emerge-A-buffer
))
1542 (if (not (buffer-modified-p emerge-B-buffer
))
1543 (kill-buffer emerge-B-buffer
))
1544 (if (and emerge-ancestor-buffer
1545 (not (buffer-modified-p emerge-ancestor-buffer
)))
1546 (kill-buffer emerge-ancestor-buffer
))
1547 ;; Write merge buffer to file
1549 (write-file file-out
)))
1553 (defun emerge-recenter (&optional arg
)
1554 "Bring the highlighted region of all three merge buffers into view.
1555 This brings the buffers into view if they are in windows.
1556 With an argument, reestablish the default three-window display."
1558 ;; If there is an argument, rebuild the window structure
1560 (emerge-setup-windows emerge-A-buffer emerge-B-buffer
1561 emerge-merge-buffer
))
1562 ;; Redisplay whatever buffers are showing, if there is a selected difference
1563 (if (and (>= emerge-current-difference
0)
1564 (< emerge-current-difference emerge-number-of-differences
))
1565 (let* ((merge-buffer emerge-merge-buffer
)
1566 (buffer-A emerge-A-buffer
)
1567 (buffer-B emerge-B-buffer
)
1568 (window-A (get-buffer-window buffer-A
'visible
))
1569 (window-B (get-buffer-window buffer-B
'visible
))
1570 (merge-window (get-buffer-window merge-buffer
))
1572 (aref emerge-difference-list emerge-current-difference
)))
1574 (select-window window-A
)
1575 (emerge-position-region
1576 (- (aref diff-vector
0)
1577 (1- emerge-before-flag-length
))
1578 (+ (aref diff-vector
1)
1579 (1- emerge-after-flag-length
))
1580 (1+ (aref diff-vector
0)))))
1582 (select-window window-B
)
1583 (emerge-position-region
1584 (- (aref diff-vector
2)
1585 (1- emerge-before-flag-length
))
1586 (+ (aref diff-vector
3)
1587 (1- emerge-after-flag-length
))
1588 (1+ (aref diff-vector
2)))))
1589 (if merge-window
(progn
1590 (select-window merge-window
)
1591 (emerge-position-region
1592 (- (aref diff-vector
4)
1593 (1- emerge-before-flag-length
))
1594 (+ (aref diff-vector
5)
1595 (1- emerge-after-flag-length
))
1596 (1+ (aref diff-vector
4))))))))
1598 ;;; Window scrolling operations
1599 ;; These operations are designed to scroll all three windows the same amount,
1600 ;; so as to keep the text in them aligned.
1602 ;; Perform some operation on all three windows (if they are showing).
1603 ;; Catches all errors on the operation in the A and B windows, but not
1604 ;; in the merge window. Usually, errors come from scrolling off the
1605 ;; beginning or end of the buffer, and this gives a nice error message:
1606 ;; End of buffer is reported in the merge buffer, but if the scroll was
1607 ;; possible in the A or B windows, it is performed there before the error
1609 (defun emerge-operate-on-windows (operation arg
)
1610 (let* ((merge-buffer emerge-merge-buffer
)
1611 (buffer-A emerge-A-buffer
)
1612 (buffer-B emerge-B-buffer
)
1613 (window-A (get-buffer-window buffer-A
'visible
))
1614 (window-B (get-buffer-window buffer-B
'visible
))
1615 (merge-window (get-buffer-window merge-buffer
)))
1617 (select-window window-A
)
1619 (funcall operation arg
)
1622 (select-window window-B
)
1624 (funcall operation arg
)
1626 (if merge-window
(progn
1627 (select-window merge-window
)
1628 (funcall operation arg
)))))
1630 (defun emerge-scroll-up (&optional arg
)
1631 "Scroll up all three merge buffers, if they are in windows.
1632 With argument N, scroll N lines; otherwise scroll by nearly
1633 the height of the merge window.
1634 `C-u -' alone as argument scrolls half the height of the merge window."
1636 (emerge-operate-on-windows
1638 ;; calculate argument to scroll-up
1639 ;; if there is an explicit argument
1640 (if (and arg
(not (equal arg
'-
)))
1642 (prefix-numeric-value arg
)
1643 ;; if not, see if we can determine a default amount (the window height)
1644 (let ((merge-window (get-buffer-window emerge-merge-buffer
)))
1645 (if (null merge-window
)
1646 ;; no window, use nil
1648 (let ((default-amount
1649 (- (window-height merge-window
) 1 next-screen-context-lines
)))
1650 ;; the window was found
1652 ;; C-u as argument means half of default amount
1653 (/ default-amount
2)
1654 ;; no argument means default amount
1655 default-amount
)))))))
1657 (defun emerge-scroll-down (&optional arg
)
1658 "Scroll down all three merge buffers, if they are in windows.
1659 With argument N, scroll N lines; otherwise scroll by nearly
1660 the height of the merge window.
1661 `C-u -' alone as argument scrolls half the height of the merge window."
1663 (emerge-operate-on-windows
1665 ;; calculate argument to scroll-down
1666 ;; if there is an explicit argument
1667 (if (and arg
(not (equal arg
'-
)))
1669 (prefix-numeric-value arg
)
1670 ;; if not, see if we can determine a default amount (the window height)
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-height merge-window
) 1 next-screen-context-lines
)))
1677 ;; the window was found
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-left (&optional arg
)
1685 "Scroll left all three merge buffers, if they are in windows.
1686 If an argument is given, that is how many columns are scrolled, else nearly
1687 the width of the A and B windows. `C-u -' alone as argument scrolls half the
1688 width of the A and B windows."
1690 (emerge-operate-on-windows
1692 ;; calculate argument to scroll-left
1693 ;; if there is an explicit argument
1694 (if (and arg
(not (equal arg
'-
)))
1696 (prefix-numeric-value arg
)
1697 ;; if not, see if we can determine a default amount
1698 ;; (half the window width)
1699 (let ((merge-window (get-buffer-window emerge-merge-buffer
)))
1700 (if (null merge-window
)
1701 ;; no window, use nil
1703 (let ((default-amount
1704 (- (/ (window-width merge-window
) 2) 3)))
1705 ;; the window was found
1707 ;; C-u as argument means half of default amount
1708 (/ default-amount
2)
1709 ;; no argument means default amount
1710 default-amount
)))))))
1712 (defun emerge-scroll-right (&optional arg
)
1713 "Scroll right all three merge buffers, if they are in windows.
1714 If an argument is given, that is how many columns are scrolled, else nearly
1715 the width of the A and B windows. `C-u -' alone as argument scrolls half the
1716 width of the A and B windows."
1718 (emerge-operate-on-windows
1720 ;; calculate argument to scroll-right
1721 ;; if there is an explicit argument
1722 (if (and arg
(not (equal arg
'-
)))
1724 (prefix-numeric-value arg
)
1725 ;; if not, see if we can determine a default amount
1726 ;; (half the window width)
1727 (let ((merge-window (get-buffer-window emerge-merge-buffer
)))
1728 (if (null merge-window
)
1729 ;; no window, use nil
1731 (let ((default-amount
1732 (- (/ (window-width merge-window
) 2) 3)))
1733 ;; the window was found
1735 ;; C-u as argument means half of default amount
1736 (/ default-amount
2)
1737 ;; no argument means default amount
1738 default-amount
)))))))
1740 (defun emerge-scroll-reset ()
1741 "Reset horizontal scrolling in Emerge.
1742 This resets the horizontal scrolling of all three merge buffers
1743 to the left margin, if they are in windows."
1745 (emerge-operate-on-windows
1746 (lambda (x) (set-window-hscroll (selected-window) 0))
1749 ;; Attempt to show the region nicely.
1750 ;; If there are min-lines lines above and below the region, then don't do
1752 ;; If not, recenter the region to make it so.
1753 ;; If that isn't possible, remove context lines evenly from top and bottom
1754 ;; so the entire region shows.
1755 ;; If that isn't possible, show the top of the region.
1756 ;; BEG must be at the beginning of a line.
1757 (defun emerge-position-region (beg end pos
)
1758 ;; First test whether the entire region is visible with
1759 ;; emerge-min-visible-lines above and below it
1760 (if (not (and (<= (progn
1761 (move-to-window-line emerge-min-visible-lines
)
1765 (move-to-window-line
1766 (- (1+ emerge-min-visible-lines
)))
1768 ;; We failed that test, see if it fits at all
1769 ;; Meanwhile positioning it correctly in case it doesn't fit
1771 (set-window-start (selected-window) beg
)
1772 (if (pos-visible-in-window-p end
)
1773 ;; Determine the number of lines that the region occupies
1775 (while (> end
(progn
1776 (move-to-window-line lines
)
1778 (setq lines
(1+ lines
)))
1779 ;; And position the beginning on the right line
1781 (recenter (/ (1+ (- (1- (window-height))
1786 (defun emerge-next-difference ()
1787 "Advance to the next difference."
1789 (if (< emerge-current-difference emerge-number-of-differences
)
1790 (let ((n (1+ emerge-current-difference
)))
1791 (while (and emerge-skip-prefers
1792 (< n emerge-number-of-differences
)
1793 (memq (aref (aref emerge-difference-list n
) 6)
1794 '(prefer-A prefer-B
)))
1796 (let ((buffer-read-only nil
))
1797 (emerge-unselect-and-select-difference n
)))
1800 (defun emerge-previous-difference ()
1801 "Go to the previous difference."
1803 (if (> emerge-current-difference -
1)
1804 (let ((n (1- emerge-current-difference
)))
1805 (while (and emerge-skip-prefers
1807 (memq (aref (aref emerge-difference-list n
) 6)
1808 '(prefer-A prefer-B
)))
1810 (let ((buffer-read-only nil
))
1811 (emerge-unselect-and-select-difference n
)))
1812 (error "At beginning")))
1814 (defun emerge-jump-to-difference (difference-number)
1815 "Go to the N-th difference."
1817 (let ((buffer-read-only nil
))
1818 (setq difference-number
(1- difference-number
))
1819 (if (and (>= difference-number -
1)
1820 (< difference-number
(1+ emerge-number-of-differences
)))
1821 (emerge-unselect-and-select-difference difference-number
)
1822 (error "Bad difference number"))))
1824 (defun emerge-abort ()
1825 "Abort the Emerge session."
1829 (defun emerge-quit (arg)
1830 "Finish the Emerge session and exit Emerge.
1831 Prefix argument means to abort rather than successfully finish.
1832 The difference depends on how the merge was started,
1833 but usually means to not write over one of the original files, or to signal
1834 to some process which invoked Emerge a failure code.
1836 Unselects the selected difference, if any, restores the read-only and modified
1837 flags of the merged file buffers, restores the local keymap of the merge
1838 buffer, and sets off various emerge flags. Using Emerge commands in this
1839 buffer after this will cause serious problems."
1844 "Do you really want to successfully finish this merge? "
1845 "Do you really want to abort this merge? "))
1847 (emerge-really-quit arg
)))
1849 ;; Perform the quit operations.
1850 (defun emerge-really-quit (arg)
1851 (setq buffer-read-only nil
)
1852 (emerge-unselect-and-select-difference -
1)
1853 (emerge-restore-buffer-characteristics)
1854 ;; null out the difference markers so they don't slow down future editing
1857 (set-marker (aref d
0) nil
)
1858 (set-marker (aref d
1) nil
)
1859 (set-marker (aref d
2) nil
)
1860 (set-marker (aref d
3) nil
)
1861 (set-marker (aref d
4) nil
)
1862 (set-marker (aref d
5) nil
))
1863 emerge-difference-list
)
1864 ;; allow them to be garbage collected
1865 (setq emerge-difference-list nil
)
1866 ;; restore the local map
1867 (use-local-map emerge-old-keymap
)
1868 ;; turn off all the emerge modes
1869 (setq emerge-mode nil
)
1870 (setq emerge-fast-mode nil
)
1871 (setq emerge-edit-mode nil
)
1872 (setq emerge-auto-advance nil
)
1873 (setq emerge-skip-prefers nil
)
1874 ;; restore mode line
1875 (kill-local-variable 'mode-line-buffer-identification
)
1876 (let ((emerge-prefix-argument arg
))
1877 (run-hooks 'emerge-quit-hook
)))
1879 (defun emerge-select-A (&optional force
)
1880 "Select the A variant of this difference.
1881 Refuses to function if this difference has been edited, i.e., if it
1882 is neither the A nor the B variant.
1883 A prefix argument forces the variant to be selected
1884 even if the difference has been edited."
1888 (emerge-select-A-edit merge-begin merge-end A-begin A-end
)
1889 (if emerge-auto-advance
1890 (emerge-next-difference))))
1892 (lambda () (if emerge-auto-advance
1893 (emerge-next-difference)))))
1894 (emerge-select-version force operate-no-change operate operate
)))
1896 ;; Actually select the A variant
1897 (defun emerge-select-A-edit (merge-begin merge-end A-begin A-end
)
1898 (with-current-buffer
1900 (delete-region merge-begin merge-end
)
1901 (goto-char merge-begin
)
1902 (insert-buffer-substring emerge-A-buffer A-begin A-end
)
1903 (goto-char merge-begin
)
1904 (aset diff-vector
6 'A
)
1905 (emerge-refresh-mode-line)))
1907 (defun emerge-select-B (&optional force
)
1908 "Select the B variant of this difference.
1909 Refuses to function if this difference has been edited, i.e., if it
1910 is neither the A nor the B variant.
1911 A prefix argument forces the variant to be selected
1912 even if the difference has been edited."
1916 (emerge-select-B-edit merge-begin merge-end B-begin B-end
)
1917 (if emerge-auto-advance
1918 (emerge-next-difference))))
1920 (lambda () (if emerge-auto-advance
1921 (emerge-next-difference)))))
1922 (emerge-select-version force operate operate-no-change operate
)))
1924 ;; Actually select the B variant
1925 (defun emerge-select-B-edit (merge-begin merge-end B-begin B-end
)
1926 (with-current-buffer
1928 (delete-region merge-begin merge-end
)
1929 (goto-char merge-begin
)
1930 (insert-buffer-substring emerge-B-buffer B-begin B-end
)
1931 (goto-char merge-begin
)
1932 (aset diff-vector
6 'B
)
1933 (emerge-refresh-mode-line)))
1935 (defun emerge-default-A ()
1936 "Make the A variant the default from here down.
1937 This selects the A variant for all differences from here down in the buffer
1938 which are still defaulted, i.e., which the user has not selected and for
1939 which there is no preference."
1941 (let ((buffer-read-only nil
))
1942 (let ((selected-difference emerge-current-difference
)
1943 (n (max emerge-current-difference
0)))
1944 (while (< n emerge-number-of-differences
)
1945 (let ((diff-vector (aref emerge-difference-list n
)))
1946 (if (eq (aref diff-vector
6) 'default-B
)
1948 (emerge-unselect-and-select-difference n t
)
1950 (aset diff-vector
6 'default-A
))))
1952 (if (zerop (% n
10))
1953 (message "Setting default to A...%d" n
)))
1954 (emerge-unselect-and-select-difference selected-difference
)))
1955 (message "Default choice is now A"))
1957 (defun emerge-default-B ()
1958 "Make the B variant the default from here down.
1959 This selects the B variant for all differences from here down in the buffer
1960 which are still defaulted, i.e., which the user has not selected and for
1961 which there is no preference."
1963 (let ((buffer-read-only nil
))
1964 (let ((selected-difference emerge-current-difference
)
1965 (n (max emerge-current-difference
0)))
1966 (while (< n emerge-number-of-differences
)
1967 (let ((diff-vector (aref emerge-difference-list n
)))
1968 (if (eq (aref diff-vector
6) 'default-A
)
1970 (emerge-unselect-and-select-difference n t
)
1972 (aset diff-vector
6 'default-B
))))
1974 (if (zerop (% n
10))
1975 (message "Setting default to B...%d" n
)))
1976 (emerge-unselect-and-select-difference selected-difference
)))
1977 (message "Default choice is now B"))
1979 (defun emerge-fast-mode ()
1980 "Set fast mode, for Emerge.
1981 In this mode ordinary Emacs commands are disabled, and Emerge commands
1982 need not be prefixed with \\<emerge-fast-keymap>\\[emerge-basic-keymap]."
1984 (setq buffer-read-only t
)
1985 (use-local-map emerge-fast-keymap
)
1986 (setq emerge-mode t
)
1987 (setq emerge-fast-mode t
)
1988 (setq emerge-edit-mode nil
)
1989 (message "Fast mode set")
1990 (force-mode-line-update))
1992 (defun emerge-edit-mode ()
1993 "Set edit mode, for Emerge.
1994 In this mode ordinary Emacs commands are available, and Emerge commands
1995 must be prefixed with \\<emerge-fast-keymap>\\[emerge-basic-keymap]."
1997 (setq buffer-read-only nil
)
1998 (use-local-map emerge-edit-keymap
)
1999 (setq emerge-mode t
)
2000 (setq emerge-fast-mode nil
)
2001 (setq emerge-edit-mode t
)
2002 (message "Edit mode set")
2003 (force-mode-line-update))
2005 (defun emerge-auto-advance (arg)
2006 "Toggle Auto-Advance mode, for Emerge.
2007 This mode causes `emerge-select-A' and `emerge-select-B' to automatically
2008 advance to the next difference.
2009 With a positive argument, turn on Auto-Advance mode.
2010 With a negative argument, turn off Auto-Advance mode."
2012 (setq emerge-auto-advance
(if (null arg
)
2013 (not emerge-auto-advance
)
2014 (> (prefix-numeric-value arg
) 0)))
2015 (message (if emerge-auto-advance
2017 "Auto-advance cleared"))
2018 (force-mode-line-update))
2020 (defun emerge-skip-prefers (arg)
2021 "Toggle Skip-Prefers mode, for Emerge.
2022 This mode causes `emerge-next-difference' and `emerge-previous-difference'
2023 to automatically skip over differences for which there is a preference.
2024 With a positive argument, turn on Skip-Prefers mode.
2025 With a negative argument, turn off Skip-Prefers mode."
2027 (setq emerge-skip-prefers
(if (null arg
)
2028 (not emerge-skip-prefers
)
2029 (> (prefix-numeric-value arg
) 0)))
2030 (message (if emerge-skip-prefers
2032 "Skip-prefers cleared"))
2033 (force-mode-line-update))
2035 (defun emerge-copy-as-kill-A ()
2036 "Put the A variant of this difference in the kill ring."
2038 (emerge-validate-difference)
2040 (aref emerge-difference-list emerge-current-difference
))
2041 (A-begin (1+ (aref diff-vector
0)))
2042 (A-end (1- (aref diff-vector
1)))
2043 ;; so further kills don't append
2045 (with-current-buffer emerge-A-buffer
2046 (copy-region-as-kill A-begin A-end
))))
2048 (defun emerge-copy-as-kill-B ()
2049 "Put the B variant of this difference in the kill ring."
2051 (emerge-validate-difference)
2053 (aref emerge-difference-list emerge-current-difference
))
2054 (B-begin (1+ (aref diff-vector
2)))
2055 (B-end (1- (aref diff-vector
3)))
2056 ;; so further kills don't append
2058 (with-current-buffer emerge-B-buffer
2059 (copy-region-as-kill B-begin B-end
))))
2061 (defun emerge-insert-A (arg)
2062 "Insert the A variant of this difference at the point.
2063 Leaves point after text, mark before.
2064 With prefix argument, puts point before, mark after."
2066 (emerge-validate-difference)
2068 (aref emerge-difference-list emerge-current-difference
))
2069 (A-begin (1+ (aref diff-vector
0)))
2070 (A-end (1- (aref diff-vector
1)))
2072 (buffer-read-only nil
))
2073 (insert-buffer-substring emerge-A-buffer A-begin A-end
)
2077 (goto-char opoint
))))
2079 (defun emerge-insert-B (arg)
2080 "Insert the B variant of this difference at the point.
2081 Leaves point after text, mark before.
2082 With prefix argument, puts point before, mark after."
2084 (emerge-validate-difference)
2086 (aref emerge-difference-list emerge-current-difference
))
2087 (B-begin (1+ (aref diff-vector
2)))
2088 (B-end (1- (aref diff-vector
3)))
2090 (buffer-read-only nil
))
2091 (insert-buffer-substring emerge-B-buffer B-begin B-end
)
2095 (goto-char opoint
))))
2097 (defun emerge-mark-difference (arg)
2098 "Leaves the point before this difference and the mark after it.
2099 With prefix argument, puts mark before, point after."
2101 (emerge-validate-difference)
2103 (aref emerge-difference-list emerge-current-difference
))
2104 (merge-begin (1+ (aref diff-vector
4)))
2105 (merge-end (1- (aref diff-vector
5))))
2108 (goto-char merge-begin
)
2109 (set-mark merge-end
))
2110 (goto-char merge-end
)
2111 (set-mark merge-begin
))))
2113 (defun emerge-file-names ()
2114 "Show the names of the buffers or files being operated on by Emerge.
2115 Use C-u l to reset the windows afterward."
2117 (delete-other-windows)
2118 (let ((temp-buffer-show-function
2120 (split-window-below)
2121 (switch-to-buffer buf
)
2123 (with-output-to-temp-buffer "*Help*"
2124 (with-current-buffer emerge-A-buffer
2125 (if buffer-file-name
2127 (princ "File A is: ")
2128 (princ buffer-file-name
))
2130 (princ "Buffer A is: ")
2131 (princ (buffer-name))))
2133 (with-current-buffer emerge-B-buffer
2134 (if buffer-file-name
2136 (princ "File B is: ")
2137 (princ buffer-file-name
))
2139 (princ "Buffer B is: ")
2140 (princ (buffer-name))))
2142 (if emerge-ancestor-buffer
2143 (with-current-buffer emerge-ancestor-buffer
2144 (if buffer-file-name
2146 (princ "Ancestor file is: ")
2147 (princ buffer-file-name
))
2149 (princ "Ancestor buffer is: ")
2150 (princ (buffer-name))))
2152 (princ emerge-output-description
)
2153 (with-current-buffer standard-output
2156 (defun emerge-join-differences (arg)
2157 "Join the selected difference with the following one.
2158 With a prefix argument, join with the preceding one."
2160 (let ((n emerge-current-difference
))
2161 ;; adjust n to be first difference to join
2164 ;; n and n+1 are the differences to join
2165 ;; check that they are both differences
2166 (if (or (< n
0) (>= n
(1- emerge-number-of-differences
)))
2167 (error "Incorrect differences to join"))
2169 (emerge-unselect-difference emerge-current-difference
)
2170 ;; decrement total number of differences
2171 (setq emerge-number-of-differences
(1- emerge-number-of-differences
))
2172 ;; build new differences vector
2174 (new-differences (make-vector emerge-number-of-differences nil
)))
2175 (while (< i emerge-number-of-differences
)
2176 (aset new-differences i
2178 ((< i n
) (aref emerge-difference-list i
))
2179 ((> i n
) (aref emerge-difference-list
(1+ i
)))
2180 (t (let ((prev (aref emerge-difference-list i
))
2181 (next (aref emerge-difference-list
(1+ i
))))
2182 (vector (aref prev
0)
2188 (let ((ps (aref prev
6))
2193 ((and (or (eq ps
'B
) (eq ps
'prefer-B
))
2194 (or (eq ns
'B
) (eq ns
'prefer-B
)))
2198 (setq emerge-difference-list new-differences
))
2199 ;; set the current difference correctly
2200 (setq emerge-current-difference n
)
2201 ;; fix the mode line
2202 (emerge-refresh-mode-line)
2203 ;; reinsert the flags
2204 (emerge-select-difference emerge-current-difference
)
2207 (defun emerge-split-difference ()
2208 "Split the current difference where the points are in the three windows."
2210 (let ((n emerge-current-difference
))
2211 ;; check that this is a valid difference
2212 (emerge-validate-difference)
2213 ;; get the point values and old difference
2214 (let ((A-point (with-current-buffer emerge-A-buffer
2216 (B-point (with-current-buffer emerge-B-buffer
2218 (merge-point (point-marker))
2219 (old-diff (aref emerge-difference-list n
)))
2220 ;; check location of the points, give error if they aren't in the
2222 (if (or (< A-point
(aref old-diff
0))
2223 (> A-point
(aref old-diff
1)))
2224 (error "Point outside of difference in A buffer"))
2225 (if (or (< B-point
(aref old-diff
2))
2226 (> B-point
(aref old-diff
3)))
2227 (error "Point outside of difference in B buffer"))
2228 (if (or (< merge-point
(aref old-diff
4))
2229 (> merge-point
(aref old-diff
5)))
2230 (error "Point outside of difference in merge buffer"))
2232 (emerge-unselect-difference emerge-current-difference
)
2233 ;; increment total number of differences
2234 (setq emerge-number-of-differences
(1+ emerge-number-of-differences
))
2235 ;; build new differences vector
2237 (new-differences (make-vector emerge-number-of-differences nil
)))
2238 (while (< i emerge-number-of-differences
)
2239 (aset new-differences i
2242 (aref emerge-difference-list i
))
2244 (aref emerge-difference-list
(1- i
)))
2246 (vector (aref old-diff
0)
2254 (vector (copy-marker A-point
)
2256 (copy-marker B-point
)
2258 (copy-marker merge-point
)
2260 (aref old-diff
6)))))
2262 (setq emerge-difference-list new-differences
))
2263 ;; set the current difference correctly
2264 (setq emerge-current-difference n
)
2265 ;; fix the mode line
2266 (emerge-refresh-mode-line)
2267 ;; reinsert the flags
2268 (emerge-select-difference emerge-current-difference
)
2269 (emerge-recenter))))
2271 (defun emerge-trim-difference ()
2272 "Trim lines off top and bottom of difference that are the same.
2273 If lines are the same in both the A and the B versions, strip them off.
2274 \(This can happen when the A and B versions have common lines that the
2275 ancestor version does not share.)"
2277 ;; make sure we are in a real difference
2278 (emerge-validate-difference)
2280 (emerge-unselect-difference emerge-current-difference
)
2281 (let* ((diff (aref emerge-difference-list emerge-current-difference
))
2282 (top-a (marker-position (aref diff
0)))
2283 (bottom-a (marker-position (aref diff
1)))
2284 (top-b (marker-position (aref diff
2)))
2285 (bottom-b (marker-position (aref diff
3)))
2286 (top-m (marker-position (aref diff
4)))
2287 (bottom-m (marker-position (aref diff
5)))
2288 size success sa sb sm
)
2289 ;; move down the tops of the difference regions as much as possible
2290 ;; Try advancing comparing 1000 chars at a time.
2291 ;; When that fails, go 500 chars at a time, and so on.
2296 (setq size
(min size
(- bottom-a top-a
) (- bottom-b top-b
)
2297 (- bottom-m top-m
)))
2298 (setq sa
(with-current-buffer emerge-A-buffer
2299 (buffer-substring top-a
2301 (setq sb
(with-current-buffer emerge-B-buffer
2302 (buffer-substring top-b
2304 (setq sm
(buffer-substring top-m
(+ size top-m
)))
2305 (setq success
(and (> size
0) (equal sa sb
) (equal sb sm
)))
2307 (setq top-a
(+ top-a size
)
2308 top-b
(+ top-b size
)
2309 top-m
(+ top-m size
))))
2310 (setq size
(/ size
2)))
2311 ;; move up the bottoms of the difference regions as much as possible
2312 ;; Try advancing comparing 1000 chars at a time.
2313 ;; When that fails, go 500 chars at a time, and so on.
2318 (setq size
(min size
(- bottom-a top-a
) (- bottom-b top-b
)
2319 (- bottom-m top-m
)))
2320 (setq sa
(with-current-buffer emerge-A-buffer
2321 (buffer-substring (- bottom-a size
)
2323 (setq sb
(with-current-buffer emerge-B-buffer
2324 (buffer-substring (- bottom-b size
)
2326 (setq sm
(buffer-substring (- bottom-m size
) bottom-m
))
2327 (setq success
(and (> size
0) (equal sa sb
) (equal sb sm
)))
2329 (setq bottom-a
(- bottom-a size
)
2330 bottom-b
(- bottom-b size
)
2331 bottom-m
(- bottom-m size
))))
2332 (setq size
(/ size
2)))
2333 ;; {top,bottom}-{a,b,m} are now set at the new beginnings and ends
2334 ;; of the difference regions. Move them to the beginning of lines, as
2336 (with-current-buffer emerge-A-buffer
2339 (aset diff
0 (point-marker))
2340 (goto-char bottom-a
)
2341 (beginning-of-line 2)
2342 (aset diff
1 (point-marker)))
2343 (with-current-buffer emerge-B-buffer
2346 (aset diff
2 (point-marker))
2347 (goto-char bottom-b
)
2348 (beginning-of-line 2)
2349 (aset diff
3 (point-marker)))
2352 (aset diff
4 (point-marker))
2353 (goto-char bottom-m
)
2354 (beginning-of-line 2)
2355 (aset diff
5 (point-marker))
2356 ;; put the flags back in, recenter the display
2357 (emerge-select-difference emerge-current-difference
)
2360 ;; FIXME the manual advertised this as working in the A or B buffers,
2361 ;; but it does not, because all the buffer locals are nil there.
2362 ;; It would work to call it from the merge buffer and specify that one
2363 ;; wants to use the value of point in the A or B buffer.
2364 ;; But with the prefix argument already in use, there is no easy way
2365 ;; to have it ask for a buffer.
2366 (defun emerge-find-difference (arg)
2367 "Find the difference containing the current position of the point.
2368 If there is no containing difference and the prefix argument is positive,
2369 it finds the nearest following difference. A negative prefix argument finds
2370 the nearest previous difference."
2372 (cond ((eq (current-buffer) emerge-A-buffer
)
2373 (emerge-find-difference-A arg
))
2374 ((eq (current-buffer) emerge-B-buffer
)
2375 (emerge-find-difference-B arg
))
2376 (t (emerge-find-difference-merge arg
))))
2378 (defun emerge-find-difference-merge (arg)
2379 "Find the difference containing point, in the merge buffer.
2380 If there is no containing difference and the prefix argument is positive,
2381 it finds the nearest following difference. A negative prefix argument finds
2382 the nearest previous difference."
2384 ;; search for the point in the merge buffer, using the markers
2385 ;; for the beginning and end of the differences in the merge buffer
2386 (emerge-find-difference1 arg
(point) 4 5))
2388 (defun emerge-find-difference-A (arg)
2389 "Find the difference containing point, in the A buffer.
2390 This command must be executed in the merge buffer.
2391 If there is no containing difference and the prefix argument is positive,
2392 it finds the nearest following difference. A negative prefix argument finds
2393 the nearest previous difference."
2395 ;; search for the point in the A buffer, using the markers
2396 ;; for the beginning and end of the differences in the A buffer
2397 (emerge-find-difference1 arg
2398 (with-current-buffer emerge-A-buffer
(point))
2401 (defun emerge-find-difference-B (arg)
2402 "Find the difference containing point, in the B buffer.
2403 This command must be executed in the merge buffer.
2404 If there is no containing difference and the prefix argument is positive,
2405 it finds the nearest following difference. A negative prefix argument finds
2406 the nearest previous difference."
2408 ;; search for the point in the B buffer, using the markers
2409 ;; for the beginning and end of the differences in the B buffer
2410 (emerge-find-difference1 arg
2411 (with-current-buffer emerge-B-buffer
(point))
2414 (defun emerge-find-difference1 (arg location begin end
)
2416 ;; find first difference containing or after the current position
2419 (while (< n emerge-number-of-differences
)
2420 (let ((diff-vector (aref emerge-difference-list n
)))
2421 (if (<= location
(marker-position (aref diff-vector end
)))
2424 emerge-number-of-differences
))
2426 ;; whether the found difference contains the current position
2427 (and (< index emerge-number-of-differences
)
2428 (<= (marker-position (aref (aref emerge-difference-list index
)
2432 ;; numeric value of prefix argument
2433 (prefix-numeric-value arg
)))
2434 (emerge-unselect-and-select-difference
2436 ;; if the point is in a difference, select it
2438 ;; if the arg is nil and the point is not in a difference, error
2439 ((null arg
) (error "No difference contains point"))
2440 ;; if the arg is positive, select the following difference
2442 (if (< index emerge-number-of-differences
)
2444 (error "No difference contains or follows point")))
2445 ;; if the arg is negative, select the preceding difference
2449 (error "No difference contains or precedes point")))))))
2451 (defun emerge-line-numbers ()
2452 "Display the current line numbers.
2453 This function displays the line numbers of the points in the A, B, and
2457 (and (>= emerge-current-difference
0)
2458 (< emerge-current-difference emerge-number-of-differences
)))
2459 (emerge-line-diff (and valid-diff
2460 (aref emerge-difference-list
2461 emerge-current-difference
)))
2462 (merge-line (emerge-line-number-in-buf 4 5))
2463 (A-line (with-current-buffer emerge-A-buffer
2464 (emerge-line-number-in-buf 0 1)))
2465 (B-line (with-current-buffer emerge-B-buffer
2466 (emerge-line-number-in-buf 2 3))))
2467 (message "At lines: merge = %d, A = %d, B = %d"
2468 merge-line A-line B-line
)))
2470 (defvar emerge-line-diff
)
2472 (defun emerge-line-number-in-buf (begin-marker end-marker
)
2473 ;; FIXME point-min rather than 1? widen?
2474 (let ((temp (1+ (count-lines 1 (line-beginning-position)))))
2477 (if (> (point) (aref emerge-line-diff begin-marker
))
2478 (setq temp
(- temp emerge-before-flag-lines
)))
2479 (if (> (point) (aref emerge-line-diff end-marker
))
2480 (setq temp
(- temp emerge-after-flag-lines
)))))
2483 (defun emerge-set-combine-template (string &optional localize
)
2484 "Set `emerge-combine-versions-template' to STRING.
2485 This value controls how `emerge-combine-versions' combines the two versions.
2486 With prefix argument, `emerge-combine-versions-template' is made local to this
2487 merge buffer. Localization is permanent for any particular merge buffer."
2488 (interactive "s\nP")
2490 (make-local-variable 'emerge-combine-versions-template
))
2491 (setq emerge-combine-versions-template string
)
2493 (if (assq 'emerge-combine-versions-template
(buffer-local-variables))
2494 "emerge-set-combine-versions-template set locally"
2495 "emerge-set-combine-versions-template set")))
2497 (defun emerge-set-combine-versions-template (start end
&optional localize
)
2498 "Copy region into `emerge-combine-versions-template'.
2499 This controls how `emerge-combine-versions' will combine the two versions.
2500 With prefix argument, `emerge-combine-versions-template' is made local to this
2501 merge buffer. Localization is permanent for any particular merge buffer."
2502 (interactive "r\nP")
2504 (make-local-variable 'emerge-combine-versions-template
))
2505 (setq emerge-combine-versions-template
(buffer-substring start end
))
2507 (if (assq 'emerge-combine-versions-template
(buffer-local-variables))
2508 "emerge-set-combine-versions-template set locally."
2509 "emerge-set-combine-versions-template set.")))
2511 (defun emerge-combine-versions (&optional force
)
2512 "Combine versions using the template in `emerge-combine-versions-template'.
2513 Refuses to function if this difference has been edited, i.e., if it is
2514 neither the A nor the B variant.
2515 An argument forces the variant to be selected even if the difference has
2518 (emerge-combine-versions-internal emerge-combine-versions-template force
))
2520 (defun emerge-combine-versions-register (char &optional force
)
2521 "Combine the two versions using the template in register REG.
2522 See documentation of the variable `emerge-combine-versions-template'
2523 for how the template is interpreted.
2524 Refuses to function if this difference has been edited, i.e., if it is
2525 neither the A nor the B variant.
2526 An argument forces the variant to be selected even if the difference has
2529 Interactively, reads the register using `register-read-with-preview'."
2531 (register-read-with-preview "Register containing template: ")
2532 current-prefix-arg
))
2533 (let ((template (get-register char
)))
2534 (if (not (stringp template
))
2535 (error "Register does not contain text"))
2536 (emerge-combine-versions-internal template force
)))
2538 (defun emerge-combine-versions-internal (emerge-combine-template force
)
2541 (emerge-combine-versions-edit merge-begin merge-end
2542 A-begin A-end B-begin B-end
)
2543 (if emerge-auto-advance
2544 (emerge-next-difference)))))
2545 (emerge-select-version force operate operate operate
)))
2547 (defvar emerge-combine-template
)
2549 (defun emerge-combine-versions-edit (merge-begin merge-end
2550 A-begin A-end B-begin B-end
)
2551 (with-current-buffer
2553 (delete-region merge-begin merge-end
)
2554 (goto-char merge-begin
)
2556 (while (< i
(length emerge-combine-template
))
2557 (let ((c (aref emerge-combine-template i
)))
2563 (aref emerge-combine-template i
)
2566 (insert-buffer-substring emerge-A-buffer A-begin A-end
))
2568 (insert-buffer-substring emerge-B-buffer B-begin B-end
))
2575 (goto-char merge-begin
)
2576 (aset diff-vector
6 'combined
)
2577 (emerge-refresh-mode-line)))
2579 (defun emerge-set-merge-mode (mode)
2580 "Set the major mode in a merge buffer.
2581 Overrides any change that the mode might make to the mode line or local
2582 keymap. Leaves merge in fast mode."
2584 (list (intern (completing-read "New major mode for merge buffer: "
2585 obarray
'commandp t nil
))))
2587 (emerge-refresh-mode-line)
2588 (if emerge-fast-mode
2590 (emerge-edit-mode)))
2592 (defun emerge-one-line-window ()
2594 (let ((window-min-height 1))
2595 (shrink-window (- (window-height) 2))))
2597 ;;; Support routines
2599 ;; Select a difference by placing the visual flags around the appropriate
2600 ;; group of lines in the A, B, and merge buffers
2601 (defun emerge-select-difference (n)
2602 (let ((emerge-globalized-difference-list emerge-difference-list
)
2603 (emerge-globalized-number-of-differences emerge-number-of-differences
))
2604 (emerge-place-flags-in-buffer emerge-A-buffer n
0 1)
2605 (emerge-place-flags-in-buffer emerge-B-buffer n
2 3)
2606 (emerge-place-flags-in-buffer nil n
4 5))
2607 (run-hooks 'emerge-select-hook
))
2609 (defun emerge-place-flags-in-buffer (buffer difference before-index
2612 (with-current-buffer
2614 (emerge-place-flags-in-buffer1 difference before-index after-index
))
2615 (emerge-place-flags-in-buffer1 difference before-index after-index
)))
2617 (defun emerge-place-flags-in-buffer1 (difference before-index after-index
)
2618 (let ((buffer-read-only nil
))
2619 ;; insert the flag before the difference
2620 (let ((before (aref (aref emerge-globalized-difference-list difference
)
2624 ;; insert the flag itself
2625 (insert-before-markers emerge-before-flag
)
2627 ;; Put the marker(s) referring to this position 1 character before the
2628 ;; end of the flag, so it won't be damaged by the user.
2629 ;; This gets a bit tricky, as there could be a number of markers
2630 ;; that have to be moved.
2631 (set-marker before
(1- before
))
2632 (let ((n (1- difference
)) after-marker before-marker diff-list
)
2636 (setq diff-list
(aref emerge-globalized-difference-list n
)
2637 after-marker
(aref diff-list after-index
))
2638 (= after-marker here
)))
2639 (set-marker after-marker
(1- after-marker
))
2640 (setq before-marker
(aref diff-list before-index
))
2641 (if (= before-marker here
)
2642 (setq before-marker
(1- before-marker
)))
2644 ;; insert the flag after the difference
2645 (let* ((after (aref (aref emerge-globalized-difference-list difference
)
2647 (here (marker-position after
)))
2649 ;; insert the flag itself
2650 (insert emerge-after-flag
)
2651 ;; Put the marker(s) referring to this position 1 character after the
2652 ;; beginning of the flag, so it won't be damaged by the user.
2653 ;; This gets a bit tricky, as there could be a number of markers
2654 ;; that have to be moved.
2655 (set-marker after
(1+ after
))
2656 (let ((n (1+ difference
)) before-marker after-marker diff-list
)
2658 (< n emerge-globalized-number-of-differences
)
2660 (setq diff-list
(aref emerge-globalized-difference-list n
)
2661 before-marker
(aref diff-list before-index
))
2662 (= before-marker here
)))
2663 (set-marker before-marker
(1+ before-marker
))
2664 (setq after-marker
(aref diff-list after-index
))
2665 (if (= after-marker here
)
2666 (setq after-marker
(1+ after-marker
)))
2667 (setq n
(1+ n
)))))))
2669 ;; Unselect a difference by removing the visual flags in the buffers.
2670 (defun emerge-unselect-difference (n)
2671 (let ((diff-vector (aref emerge-difference-list n
)))
2672 (emerge-remove-flags-in-buffer emerge-A-buffer
2673 (aref diff-vector
0) (aref diff-vector
1))
2674 (emerge-remove-flags-in-buffer emerge-B-buffer
2675 (aref diff-vector
2) (aref diff-vector
3))
2676 (emerge-remove-flags-in-buffer emerge-merge-buffer
2677 (aref diff-vector
4) (aref diff-vector
5)))
2678 (run-hooks 'emerge-unselect-hook
))
2680 (defun emerge-remove-flags-in-buffer (buffer before after
)
2681 (with-current-buffer
2683 (let ((buffer-read-only nil
))
2684 ;; remove the flags, if they're there
2685 (goto-char (- before
(1- emerge-before-flag-length
)))
2686 (if (looking-at emerge-before-flag-match
)
2687 (delete-char emerge-before-flag-length
)
2688 ;; the flag isn't there
2690 (message "Trouble removing flag"))
2691 (goto-char (1- after
))
2692 (if (looking-at emerge-after-flag-match
)
2693 (delete-char emerge-after-flag-length
)
2694 ;; the flag isn't there
2696 (message "Trouble removing flag")))))
2698 ;; Select a difference, removing any flags that exist now.
2699 (defun emerge-unselect-and-select-difference (n &optional suppress-display
)
2700 (if (and (>= emerge-current-difference
0)
2701 (< emerge-current-difference emerge-number-of-differences
))
2702 (emerge-unselect-difference emerge-current-difference
))
2703 (if (and (>= n
0) (< n emerge-number-of-differences
))
2705 (emerge-select-difference n
)
2706 (let* ((diff-vector (aref emerge-difference-list n
))
2707 (selection-type (aref diff-vector
6)))
2708 (if (eq selection-type
'default-A
)
2709 (aset diff-vector
6 'A
)
2710 (if (eq selection-type
'default-B
)
2711 (aset diff-vector
6 'B
))))))
2712 (setq emerge-current-difference n
)
2713 (if (not suppress-display
)
2716 (emerge-refresh-mode-line))))
2718 ;; Perform tests to see whether user should be allowed to select a version
2719 ;; of this difference:
2720 ;; a valid difference has been selected; and
2721 ;; the difference text in the merge buffer is:
2722 ;; the A version (execute a-version), or
2723 ;; the B version (execute b-version), or
2724 ;; empty (execute neither-version), or
2725 ;; argument FORCE is true (execute neither-version)
2726 ;; Otherwise, signal an error.
2727 (defun emerge-select-version (force a-version b-version neither-version
)
2728 (emerge-validate-difference)
2729 (let ((buffer-read-only nil
))
2731 (aref emerge-difference-list emerge-current-difference
))
2732 (A-begin (1+ (aref diff-vector
0)))
2733 (A-end (1- (aref diff-vector
1)))
2734 (B-begin (1+ (aref diff-vector
2)))
2735 (B-end (1- (aref diff-vector
3)))
2736 (merge-begin (1+ (aref diff-vector
4)))
2737 (merge-end (1- (aref diff-vector
5))))
2738 (if (emerge-compare-buffers emerge-A-buffer A-begin A-end
2739 emerge-merge-buffer merge-begin
2742 (if (emerge-compare-buffers emerge-B-buffer B-begin B-end
2743 emerge-merge-buffer merge-begin
2746 (if (or force
(= merge-begin merge-end
))
2747 (funcall neither-version
)
2748 (error "This difference region has been edited")))))))
2750 ;; Read a file name, handling all of the various defaulting rules.
2752 (defun emerge-read-file-name (prompt alternative-default-dir default-file
2754 ;; `prompt' should not have trailing ": ", so that it can be modified
2755 ;; according to context.
2756 ;; If alternative-default-dir is non-nil, it should be used as the default
2757 ;; directory instead if default-directory, if emerge-default-last-directories
2759 ;; If default-file is set, it should be used as the default value.
2760 ;; If A-file is set, and its directory is different from
2761 ;; alternative-default-dir, and if emerge-default-last-directories is set,
2762 ;; the default file should be the last part of A-file in the default
2763 ;; directory. (Overriding default-file.)
2765 ;; If this is not the A-file argument (shown by non-nil A-file), and
2766 ;; if emerge-default-last-directories is set, and
2767 ;; the default directory exists but is not the same as the directory of the
2769 ;; then make the default file have the same name as the A-file, but in
2770 ;; the default directory.
2771 ((and emerge-default-last-directories
2773 alternative-default-dir
2774 (not (string-equal alternative-default-dir
2775 (file-name-directory A-file
))))
2776 (read-file-name (format "%s (default %s): "
2777 prompt
(file-name-nondirectory A-file
))
2778 alternative-default-dir
2779 (concat alternative-default-dir
2780 (file-name-nondirectory A-file
))
2781 (and must-match
'confirm
)))
2782 ;; If there is a default file, use it.
2784 (read-file-name (format "%s (default %s): " prompt default-file
)
2785 ;; If emerge-default-last-directories is set, use the
2786 ;; directory from the same argument of the last call of
2787 ;; Emerge as the default for this argument.
2788 (and emerge-default-last-directories
2789 alternative-default-dir
)
2790 default-file
(and must-match
'confirm
)))
2792 (read-file-name (concat prompt
": ")
2793 ;; If emerge-default-last-directories is set, use the
2794 ;; directory from the same argument of the last call of
2795 ;; Emerge as the default for this argument.
2796 (and emerge-default-last-directories
2797 alternative-default-dir
)
2798 nil
(and must-match
'confirm
)))))
2800 ;; Revise the mode line to display which difference we have selected
2802 (defun emerge-refresh-mode-line ()
2803 (setq mode-line-buffer-identification
2804 (list (format "Emerge: %%b diff %d of %d%s"
2805 (1+ emerge-current-difference
)
2806 emerge-number-of-differences
2807 (if (and (>= emerge-current-difference
0)
2808 (< emerge-current-difference
2809 emerge-number-of-differences
))
2810 (cdr (assq (aref (aref emerge-difference-list
2811 emerge-current-difference
)
2815 (prefer-A .
" - A*")
2816 (prefer-B .
" - B*")
2817 (combined .
" - comb"))))
2819 (force-mode-line-update))
2821 ;; compare two regions in two buffers for containing the same text
2822 (defun emerge-compare-buffers (buffer-x x-begin x-end buffer-y y-begin y-end
)
2823 ;; first check that the two regions are the same length
2824 (if (not (and (= (- x-end x-begin
) (- y-end y-begin
))))
2827 (while (< x-begin x-end
)
2828 ;; bite off and compare no more than 1000 characters at a time
2829 (let* ((compare-length (min (- x-end x-begin
) 1000))
2830 (x-string (with-current-buffer
2832 (buffer-substring x-begin
2833 (+ x-begin compare-length
))))
2834 (y-string (with-current-buffer
2836 (buffer-substring y-begin
2837 (+ y-begin compare-length
)))))
2838 (if (not (string-equal x-string y-string
))
2840 (setq x-begin
(+ x-begin compare-length
))
2841 (setq y-begin
(+ y-begin compare-length
)))))
2844 ;; Construct a unique buffer name.
2845 ;; The first one tried is prefixsuffix, then prefix<2>suffix,
2846 ;; prefix<3>suffix, etc.
2847 (defun emerge-unique-buffer-name (prefix suffix
)
2848 (if (null (get-buffer (concat prefix suffix
)))
2849 (concat prefix suffix
)
2851 (while (get-buffer (format "%s<%d>%s" prefix n suffix
))
2853 (format "%s<%d>%s" prefix n suffix
))))
2855 ;; Verify that we have a difference selected.
2856 (defun emerge-validate-difference ()
2857 (if (not (and (>= emerge-current-difference
0)
2858 (< emerge-current-difference emerge-number-of-differences
)))
2859 (error "No difference selected")))
2861 ;;; Functions for saving and restoring a batch of variables
2863 ;; These functions save (get the values of) and restore (set the values of)
2864 ;; a list of variables. The argument is a list of symbols (the names of
2865 ;; the variables). A list element can also be a list of two functions,
2866 ;; the first of which (when called with no arguments) gets the value, and
2867 ;; the second (when called with a value as an argument) sets the value.
2868 ;; A "function" is anything that funcall can handle as an argument.
2870 (defun emerge-save-variables (vars)
2871 (mapcar (lambda (v) (if (symbolp v
)
2876 (defun emerge-restore-variables (vars values
)
2878 (let ((var (car vars
))
2879 (value (car values
)))
2882 (funcall (car (cdr var
)) value
)))
2883 (setq vars
(cdr vars
))
2884 (setq values
(cdr values
))))
2886 ;; When the pointless option emerge-temp-file-prefix goes,
2887 ;; make this function obsolete too, and just use make-temp-file.
2888 (defun emerge-make-temp-file (prefix)
2889 "Make a private temporary file based on `emerge-temp-file-prefix'."
2890 (make-temp-file (concat emerge-temp-file-prefix prefix
)))
2892 ;;; Functions that query the user before he can write out the current buffer.
2894 (defun emerge-query-write-file ()
2895 "Ask the user whether to write out an incomplete merge.
2896 If answer is yes, call `write-file' to do so. See `emerge-query-and-call'
2897 for details of the querying process."
2899 (emerge-query-and-call 'write-file
))
2901 (defun emerge-query-save-buffer ()
2902 "Ask the user whether to save an incomplete merge.
2903 If answer is yes, call `save-buffer' to do so. See `emerge-query-and-call'
2904 for details of the querying process."
2906 (emerge-query-and-call 'save-buffer
))
2908 (defun emerge-query-and-call (command)
2909 "Ask the user whether to save or write out the incomplete merge.
2910 If answer is yes, call COMMAND interactively. During the call, the flags
2911 around the current difference are removed."
2912 (if (yes-or-no-p "Do you really write to write out this unfinished merge? ")
2913 ;; He really wants to do it -- unselect the difference for the duration
2915 (if (and (>= emerge-current-difference
0)
2916 (< emerge-current-difference emerge-number-of-differences
))
2917 (emerge-unselect-difference emerge-current-difference
))
2918 ;; call-interactively takes the value of current-prefix-arg as the
2919 ;; prefix argument value to be passed to the command. Thus, we have
2920 ;; to do nothing special to make sure the prefix argument is
2921 ;; transmitted to the command.
2922 (call-interactively command
)
2923 (if (and (>= emerge-current-difference
0)
2924 (< emerge-current-difference emerge-number-of-differences
))
2926 (emerge-select-difference emerge-current-difference
)
2927 (emerge-recenter))))
2928 ;; He's being smart and not doing it
2929 (message "Not written")))
2931 ;; Make sure the current buffer (for a file) has the same contents as the
2932 ;; file on disk, and attempt to remedy the situation if not.
2933 ;; Signal an error if we can't make them the same, or the user doesn't want
2934 ;; to do what is necessary to make them the same.
2935 (defun emerge-verify-file-buffer ()
2936 ;; First check if the file has been modified since the buffer visited it.
2937 (if (verify-visited-file-modtime (current-buffer))
2938 (if (buffer-modified-p)
2939 ;; If buffer is not obsolete and is modified, offer to save
2940 (if (yes-or-no-p (format "Save file %s? " buffer-file-name
))
2942 (error "Buffer out of sync for file %s" buffer-file-name
))
2943 ;; If buffer is not obsolete and is not modified, do nothing
2945 (if (buffer-modified-p)
2946 ;; If buffer is obsolete and is modified, give error
2947 (error "Buffer out of sync for file %s" buffer-file-name
)
2948 ;; If buffer is obsolete and is not modified, offer to revert
2949 (if (yes-or-no-p (format "Revert file %s? " buffer-file-name
))
2951 (error "Buffer out of sync for file %s" buffer-file-name
)))))
2953 ;; Utilities that might have value outside of Emerge.
2955 ;; Set up the mode in the current buffer to duplicate the mode in another
2957 (defun emerge-copy-modes (buffer)
2958 ;; Set the major mode
2959 (funcall (with-current-buffer buffer major-mode
)))
2961 ;; Define a key, even if a prefix of it is defined
2962 (defun emerge-force-define-key (keymap key definition
)
2963 "Like `define-key', but forcibly creates prefix characters as needed.
2964 If some prefix of KEY has a non-prefix definition, it is redefined."
2965 ;; Find out if a prefix of key is defined
2966 (let ((v (lookup-key keymap key
)))
2967 ;; If so, undefine it
2969 (define-key keymap
(substring key
0 v
) nil
)))
2970 ;; Now define the key
2971 (define-key keymap key definition
))
2973 ;;;;; Improvements to describe-mode, so that it describes minor modes as well
2974 ;;;;; as the major mode
2975 ;;(defun describe-mode (&optional minor)
2976 ;; "Display documentation of current major mode.
2977 ;;If optional arg MINOR is non-nil (or prefix argument is given if interactive),
2978 ;;display documentation of active minor modes as well.
2979 ;;For this to work correctly for a minor mode, the mode's indicator variable
2980 ;;\(listed in `minor-mode-alist') must also be a function whose documentation
2981 ;;describes the minor mode."
2983 ;; (with-output-to-temp-buffer "*Help*"
2984 ;; (princ mode-name)
2985 ;; (princ " Mode:\n")
2986 ;; (princ (documentation major-mode))
2987 ;; (let ((minor-modes minor-mode-alist)
2988 ;; (locals (buffer-local-variables)))
2989 ;; (while minor-modes
2990 ;; (let* ((minor-mode (car (car minor-modes)))
2991 ;; (indicator (car (cdr (car minor-modes))))
2992 ;; (local-binding (assq minor-mode locals)))
2993 ;; ;; Document a minor mode if it is listed in minor-mode-alist,
2994 ;; ;; bound locally in this buffer, non-nil, and has a function
2996 ;; (if (and local-binding
2997 ;; (cdr local-binding)
2998 ;; (fboundp minor-mode))
3000 ;; (princ (format "\n\n\n%s minor mode (indicator%s):\n"
3001 ;; minor-mode indicator))
3002 ;; (princ (documentation minor-mode)))))
3003 ;; (setq minor-modes (cdr minor-modes))))
3004 ;; (with-current-buffer standard-output
3006 ;; (help-print-return-message)))
3008 ;; This goes with the redefinition of describe-mode.
3009 ;;;; Adjust things so that keyboard macro definitions are documented correctly.
3010 ;;(fset 'defining-kbd-macro (symbol-function 'start-kbd-macro))
3012 ;; substitute-key-definition should work now.
3013 ;;;; Function to shadow a definition in a keymap with definitions in another.
3014 ;;(defun emerge-shadow-key-definition (olddef newdef keymap shadowmap)
3015 ;; "Shadow OLDDEF with NEWDEF for any keys in KEYMAP with entries in SHADOWMAP.
3016 ;;In other words, SHADOWMAP will now shadow all definitions of OLDDEF in KEYMAP
3017 ;;with NEWDEF. Does not affect keys that are already defined in SHADOWMAP,
3018 ;;including those whose definition is OLDDEF."
3019 ;; ;; loop through all keymaps accessible from keymap
3020 ;; (let ((maps (accessible-keymaps keymap)))
3022 ;; (let ((prefix (car (car maps)))
3023 ;; (map (cdr (car maps))))
3024 ;; ;; examine a keymap
3027 ;; (let ((len (length map))
3030 ;; (if (eq (aref map i) olddef)
3031 ;; ;; set the shadowing definition
3032 ;; (let ((key (concat prefix (char-to-string i))))
3033 ;; (emerge-define-key-if-possible shadowmap key newdef)))
3034 ;; (setq i (1+ i))))
3037 ;; (if (eq (cdr-safe (car-safe map)) olddef)
3038 ;; ;; set the shadowing definition
3040 ;; (concat prefix (char-to-string (car (car map))))))
3041 ;; (emerge-define-key-if-possible shadowmap key newdef)))
3042 ;; (setq map (cdr map)))))
3043 ;; (setq maps (cdr maps)))))
3045 ;; Define a key if it (or a prefix) is not already defined in the map.
3046 (defun emerge-define-key-if-possible (keymap key definition
)
3047 ;; look up the present definition of the key
3048 (let ((present (lookup-key keymap key
)))
3049 (if (integerp present
)
3050 ;; if it is "too long", look up the valid prefix
3051 (if (not (lookup-key keymap
(substring key
0 present
)))
3052 ;; if the prefix isn't defined, define it
3053 (define-key keymap key definition
))
3054 ;; if there is no present definition, define it
3056 (define-key keymap key definition
)))))
3058 ;; Ordinary substitute-key-definition should do this now.
3059 ;;(defun emerge-recursively-substitute-key-definition (olddef newdef keymap)
3060 ;; "Like `substitute-key-definition', but act recursively on subkeymaps.
3061 ;;Make sure that subordinate keymaps aren't shared with other keymaps!
3062 ;;\(`copy-keymap' will suffice.)"
3063 ;; ;; Loop through all keymaps accessible from keymap
3064 ;; (let ((maps (accessible-keymaps keymap)))
3066 ;; ;; Substitute in this keymap
3067 ;; (substitute-key-definition olddef newdef (cdr (car maps)))
3068 ;; (setq maps (cdr maps)))))
3070 ;; Show the name of the file in the buffer.
3071 (defun emerge-show-file-name ()
3072 "Displays the name of the file loaded into the current buffer.
3073 If the name won't fit on one line, the minibuffer is expanded to hold it,
3074 and the command waits for a keystroke from the user. If the keystroke is
3075 SPC, it is ignored; if it is anything else, it is processed as a command."
3077 (let ((name (buffer-file-name)))
3079 (setq name
"Buffer has no file name."))
3080 (save-window-excursion
3081 (select-window (minibuffer-window))
3086 (while (and (not (pos-visible-in-window-p))
3087 (not (window-full-height-p)))
3089 (let* ((echo-keystrokes 0)
3092 (setq unread-command-events
(list c
)))))
3095 ;; Improved auto-save file names.
3096 ;; This function fixes many problems with the standard auto-save file names:
3097 ;; Auto-save files for non-file buffers get put in the default directory
3098 ;; for the buffer, whether that makes sense or not.
3099 ;; Auto-save files for file buffers get put in the directory of the file,
3100 ;; regardless of whether we can write into it or not.
3101 ;; Auto-save files for non-file buffers don't use the process id, so if a
3102 ;; user runs more than on Emacs, they can make auto-save files that overwrite
3104 ;; To use this function, do:
3105 ;; (fset 'make-auto-save-file-name
3106 ;; (symbol-function 'emerge-make-auto-save-file-name))
3107 (defun emerge-make-auto-save-file-name ()
3108 "Return file name to use for auto-saves of current buffer.
3109 Does not consider `auto-save-visited-file-name';
3110 that is checked before calling this function.
3111 You can redefine this for customization.
3112 See also `auto-save-file-name-p'."
3113 (if buffer-file-name
3114 ;; if buffer has a file, try the format <file directory>/#<file name>#
3115 (let ((f (concat (file-name-directory buffer-file-name
)
3117 (file-name-nondirectory buffer-file-name
)
3119 (if (file-writable-p f
)
3120 ;; the file is writable, so use it
3122 ;; the file isn't writable, so use the format
3123 ;; ~/#&<file name>&<hash of directory>#
3124 (concat (getenv "HOME")
3126 (file-name-nondirectory buffer-file-name
)
3128 (emerge-hash-string-into-string
3129 (file-name-directory buffer-file-name
))
3131 ;; if buffer has no file, use the format ~/#%<buffer name>%<process id>#
3132 (expand-file-name (concat (getenv "HOME")
3134 ;; quote / into \! and \ into \\
3135 (emerge-unslashify-name (buffer-name))
3140 ;; Hash a string into five characters more-or-less suitable for use in a file
3141 ;; name. (Allowed characters are ! through ~, except /.)
3142 (defun emerge-hash-string-into-string (s)
3143 (let ((bins (vector 0 0 0 0 0))
3145 (while (< i
(length s
))
3146 (aset bins
(% i
5) (%
(+ (* (aref bins
(% i
5)) 35)
3150 (mapconcat (lambda (b)
3151 (setq b
(+ (% b
93) ?
!))
3157 ;; Quote any /s in a string by replacing them with \!.
3158 ;; Also, replace any \s by \\, to make it one-to-one.
3159 (defun emerge-unslashify-name (s)
3161 (while (string-match "[/\\]" s limit
)
3162 (setq s
(concat (substring s
0 (match-beginning 0))
3163 (if (string= (substring s
(match-beginning 0)
3168 (substring s
(match-end 0))))
3169 (setq limit
(1+ (match-end 0)))))
3172 ;; Metacharacters that have to be protected from the shell when executing
3173 ;; a diff/diff3 command.
3174 (defcustom emerge-metachars
3175 (if (memq system-type
'(ms-dos windows-nt
))
3177 "[ \t\n!\"#$&'()*;<=>?[\\^`{|~]")
3178 "Characters that must be quoted when used in a shell command line.
3179 More precisely, a [...] regexp to match any one such character."
3183 ;; Quote metacharacters (using \) when executing a diff/diff3 command.
3184 (defun emerge-protect-metachars (s)
3185 (if (memq system-type
'(ms-dos windows-nt
))
3186 (shell-quote-argument s
)
3188 (while (string-match emerge-metachars s limit
)
3189 (setq s
(concat (substring s
0 (match-beginning 0))
3191 (substring s
(match-beginning 0))))
3192 (setq limit
(1+ (match-end 0)))))
3197 ;;; emerge.el ends here