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 (defcustom emerge-temp-file-mode
384 ; u=rw only
265 "Mode for Emerge temporary files."
269 (defcustom emerge-combine-versions-template
270 "#ifdef NEW\n%b#else /* not NEW */\n%a#endif /* not NEW */\n"
271 "Template for `emerge-combine-versions' to combine the two versions.
272 The template is inserted as a string, with the following interpolations:
273 %a the A version of the difference
274 %b the B version of the difference
276 Don't forget to end the template with a newline.
277 Note that this variable can be made local to a particular merge buffer by
278 giving a prefix argument to `emerge-set-combine-versions-template'."
284 (defvar emerge-basic-keymap nil
285 "Keymap of Emerge commands.
286 Directly available in `fast' mode;
287 must be prefixed by \\<emerge-fast-keymap>\\[emerge-basic-keymap] in `edit' mode.")
289 (defvar emerge-fast-keymap nil
290 "Local keymap used in Emerge `fast' mode.
291 Makes Emerge commands directly available.")
293 (defvar emerge-options-menu
294 (make-sparse-keymap "Options"))
296 (defvar emerge-merge-menu
297 (make-sparse-keymap "Merge"))
299 (defvar emerge-move-menu
300 (make-sparse-keymap "Move"))
302 (defcustom emerge-command-prefix
"\C-c\C-c"
303 "Command prefix for Emerge commands in `edit' mode.
304 Must be set before Emerge is loaded."
308 ;; This function sets up the fixed keymaps. It is executed when the first
309 ;; Emerge is done to allow the user maximum time to set up the global keymap.
310 (defun emerge-setup-fixed-keymaps ()
311 ;; Set up the basic keymap
312 (setq emerge-basic-keymap
(make-keymap))
313 (suppress-keymap emerge-basic-keymap
) ; this sets 0..9 to digit-argument and
314 ; - to negative-argument
315 (define-key emerge-basic-keymap
"p" 'emerge-previous-difference
)
316 (define-key emerge-basic-keymap
"n" 'emerge-next-difference
)
317 (define-key emerge-basic-keymap
"a" 'emerge-select-A
)
318 (define-key emerge-basic-keymap
"b" 'emerge-select-B
)
319 (define-key emerge-basic-keymap
"j" 'emerge-jump-to-difference
)
320 (define-key emerge-basic-keymap
"." 'emerge-find-difference
)
321 (define-key emerge-basic-keymap
"q" 'emerge-quit
)
322 (define-key emerge-basic-keymap
"\C-]" 'emerge-abort
)
323 (define-key emerge-basic-keymap
"f" 'emerge-fast-mode
)
324 (define-key emerge-basic-keymap
"e" 'emerge-edit-mode
)
325 (define-key emerge-basic-keymap
"s" nil
)
326 (define-key emerge-basic-keymap
"sa" 'emerge-auto-advance
)
327 (define-key emerge-basic-keymap
"ss" 'emerge-skip-prefers
)
328 (define-key emerge-basic-keymap
"l" 'emerge-recenter
)
329 (define-key emerge-basic-keymap
"d" nil
)
330 (define-key emerge-basic-keymap
"da" 'emerge-default-A
)
331 (define-key emerge-basic-keymap
"db" 'emerge-default-B
)
332 (define-key emerge-basic-keymap
"c" nil
)
333 (define-key emerge-basic-keymap
"ca" 'emerge-copy-as-kill-A
)
334 (define-key emerge-basic-keymap
"cb" 'emerge-copy-as-kill-B
)
335 (define-key emerge-basic-keymap
"i" nil
)
336 (define-key emerge-basic-keymap
"ia" 'emerge-insert-A
)
337 (define-key emerge-basic-keymap
"ib" 'emerge-insert-B
)
338 (define-key emerge-basic-keymap
"m" 'emerge-mark-difference
)
339 (define-key emerge-basic-keymap
"v" 'emerge-scroll-up
)
340 (define-key emerge-basic-keymap
"^" 'emerge-scroll-down
)
341 (define-key emerge-basic-keymap
"<" 'emerge-scroll-left
)
342 (define-key emerge-basic-keymap
">" 'emerge-scroll-right
)
343 (define-key emerge-basic-keymap
"|" 'emerge-scroll-reset
)
344 (define-key emerge-basic-keymap
"x" nil
)
345 (define-key emerge-basic-keymap
"x1" 'emerge-one-line-window
)
346 (define-key emerge-basic-keymap
"xc" 'emerge-combine-versions
)
347 (define-key emerge-basic-keymap
"xC" 'emerge-combine-versions-register
)
348 (define-key emerge-basic-keymap
"xf" 'emerge-file-names
)
349 (define-key emerge-basic-keymap
"xj" 'emerge-join-differences
)
350 (define-key emerge-basic-keymap
"xl" 'emerge-line-numbers
)
351 (define-key emerge-basic-keymap
"xm" 'emerge-set-merge-mode
)
352 (define-key emerge-basic-keymap
"xs" 'emerge-split-difference
)
353 (define-key emerge-basic-keymap
"xt" 'emerge-trim-difference
)
354 (define-key emerge-basic-keymap
"xx" 'emerge-set-combine-versions-template
)
355 ;; Allow emerge-basic-keymap to be referenced indirectly
356 (fset 'emerge-basic-keymap emerge-basic-keymap
)
357 ;; Set up the fast mode keymap
358 (setq emerge-fast-keymap
(copy-keymap emerge-basic-keymap
))
359 ;; Allow prefixed commands to work in fast mode
360 (define-key emerge-fast-keymap emerge-command-prefix
'emerge-basic-keymap
)
361 ;; Allow emerge-fast-keymap to be referenced indirectly
362 (fset 'emerge-fast-keymap emerge-fast-keymap
)
363 ;; Suppress write-file and save-buffer
364 (define-key emerge-fast-keymap
[remap write-file
] 'emerge-query-write-file
)
365 (define-key emerge-fast-keymap
[remap save-buffer
] 'emerge-query-save-buffer
)
367 (define-key emerge-basic-keymap
[menu-bar
] (make-sparse-keymap))
369 (define-key emerge-fast-keymap
[menu-bar emerge-options
]
370 (cons "Merge-Options" emerge-options-menu
))
371 (define-key emerge-fast-keymap
[menu-bar merge
]
372 (cons "Merge" emerge-merge-menu
))
373 (define-key emerge-fast-keymap
[menu-bar move
]
374 (cons "Move" emerge-move-menu
))
376 (define-key emerge-move-menu
[emerge-scroll-reset
]
377 '("Scroll Reset" . emerge-scroll-reset
))
378 (define-key emerge-move-menu
[emerge-scroll-right
]
379 '("Scroll Right" . emerge-scroll-right
))
380 (define-key emerge-move-menu
[emerge-scroll-left
]
381 '("Scroll Left" . emerge-scroll-left
))
382 (define-key emerge-move-menu
[emerge-scroll-down
]
383 '("Scroll Down" . emerge-scroll-down
))
384 (define-key emerge-move-menu
[emerge-scroll-up
]
385 '("Scroll Up" . emerge-scroll-up
))
386 (define-key emerge-move-menu
[emerge-recenter
]
387 '("Recenter" . emerge-recenter
))
388 (define-key emerge-move-menu
[emerge-mark-difference
]
389 '("Mark Difference" . emerge-mark-difference
))
390 (define-key emerge-move-menu
[emerge-jump-to-difference
]
391 '("Jump To Difference" . emerge-jump-to-difference
))
392 (define-key emerge-move-menu
[emerge-find-difference
]
393 '("Find Difference" . emerge-find-difference
))
394 (define-key emerge-move-menu
[emerge-previous-difference
]
395 '("Previous Difference" . emerge-previous-difference
))
396 (define-key emerge-move-menu
[emerge-next-difference
]
397 '("Next Difference" . emerge-next-difference
))
400 (define-key emerge-options-menu
[emerge-one-line-window
]
401 '("One Line Window" . emerge-one-line-window
))
402 (define-key emerge-options-menu
[emerge-set-merge-mode
]
403 '("Set Merge Mode..." . emerge-set-merge-mode
))
404 (define-key emerge-options-menu
[emerge-set-combine-template
]
405 '("Set Combine Template..." . emerge-set-combine-template
))
406 (define-key emerge-options-menu
[emerge-default-B
]
407 '("Default B" . emerge-default-B
))
408 (define-key emerge-options-menu
[emerge-default-A
]
409 '("Default A" . emerge-default-A
))
410 (define-key emerge-options-menu
[emerge-skip-prefers
]
411 '(menu-item "Skip Prefers" emerge-skip-prefers
412 :button
(:toggle . emerge-skip-prefers
)))
413 (define-key emerge-options-menu
[emerge-auto-advance
]
414 '(menu-item "Auto Advance" emerge-auto-advance
415 :button
(:toggle . emerge-auto-advance
)))
416 (define-key emerge-options-menu
[emerge-edit-mode
]
417 '(menu-item "Edit Mode" emerge-edit-mode
:enable
(not emerge-edit-mode
)))
418 (define-key emerge-options-menu
[emerge-fast-mode
]
419 '(menu-item "Fast Mode" emerge-fast-mode
:enable
(not emerge-fast-mode
)))
421 (define-key emerge-merge-menu
[emerge-abort
] '("Abort" . emerge-abort
))
422 (define-key emerge-merge-menu
[emerge-quit
] '("Quit" . emerge-quit
))
423 (define-key emerge-merge-menu
[emerge-split-difference
]
424 '("Split Difference" . emerge-split-difference
))
425 (define-key emerge-merge-menu
[emerge-join-differences
]
426 '("Join Differences" . emerge-join-differences
))
427 (define-key emerge-merge-menu
[emerge-trim-difference
]
428 '("Trim Difference" . emerge-trim-difference
))
429 (define-key emerge-merge-menu
[emerge-combine-versions
]
430 '("Combine Versions" . emerge-combine-versions
))
431 (define-key emerge-merge-menu
[emerge-copy-as-kill-B
]
432 '("Copy B as Kill" . emerge-copy-as-kill-B
))
433 (define-key emerge-merge-menu
[emerge-copy-as-kill-A
]
434 '("Copy A as Kill" . emerge-copy-as-kill-A
))
435 (define-key emerge-merge-menu
[emerge-insert-B
]
436 '("Insert B" . emerge-insert-B
))
437 (define-key emerge-merge-menu
[emerge-insert-A
]
438 '("Insert A" . emerge-insert-A
))
439 (define-key emerge-merge-menu
[emerge-select-B
]
440 '("Select B" . emerge-select-B
))
441 (define-key emerge-merge-menu
[emerge-select-A
]
442 '("Select A" . emerge-select-A
)))
445 ;; Variables which control each merge. They are local to the merge buffer.
448 (emerge-defvar-local emerge-mode nil
449 "Indicator for emerge-mode.")
450 (emerge-defvar-local emerge-fast-mode nil
451 "Indicator for emerge-mode fast submode.")
452 (emerge-defvar-local emerge-edit-mode nil
453 "Indicator for emerge-mode edit submode.")
454 (emerge-defvar-local emerge-A-buffer nil
455 "The buffer in which the A variant is stored.")
456 (emerge-defvar-local emerge-B-buffer nil
457 "The buffer in which the B variant is stored.")
458 (emerge-defvar-local emerge-merge-buffer nil
459 "The buffer in which the merged file is manipulated.")
460 (emerge-defvar-local emerge-ancestor-buffer nil
461 "The buffer in which the ancestor variant is stored,
462 or nil if there is none.")
464 (defconst emerge-saved-variables
465 '((buffer-modified-p set-buffer-modified-p
)
467 buffer-auto-save-file-name
)
468 "Variables and properties of a buffer which are saved, modified and restored
470 (defconst emerge-merging-values
'(nil t nil
)
471 "Values to be assigned to emerge-saved-variables during a merge.")
473 (emerge-defvar-local emerge-A-buffer-values nil
474 "Remembers emerge-saved-variables for emerge-A-buffer.")
475 (emerge-defvar-local emerge-B-buffer-values nil
476 "Remembers emerge-saved-variables for emerge-B-buffer.")
478 (emerge-defvar-local emerge-difference-list nil
479 "Vector of differences between the variants, and markers in the buffers to
480 show where they are. Each difference is represented by a vector of seven
481 elements. The first two are markers to the beginning and end of the difference
482 section in the A buffer, the second two are markers for the B buffer, the third
483 two are markers for the merge buffer, and the last element is the \"state\" of
484 that difference in the merge buffer.
485 A section of a buffer is described by two markers, one to the beginning of
486 the first line of the section, and one to the beginning of the first line
487 after the section. (If the section is empty, both markers point to the same
488 point.) If the section is part of the selected difference, then the markers
489 are moved into the flags, so the user can edit the section without disturbing
492 A the merge buffer currently contains the A variant
493 B the merge buffer currently contains the B variant
494 default-A the merge buffer contains the A variant by default,
495 but this difference hasn't been selected yet, so
496 change-default commands can alter it
497 default-B the merge buffer contains the B variant by default,
498 but this difference hasn't been selected yet, so
499 change-default commands can alter it
500 prefer-A in a three-file merge, the A variant is the preferred
502 prefer-B in a three-file merge, the B variant is the preferred
504 (emerge-defvar-local emerge-current-difference -
1
505 "The difference that is currently selected.")
506 (emerge-defvar-local emerge-number-of-differences nil
507 "Number of differences found.")
508 (emerge-defvar-local emerge-edit-keymap nil
509 "The local keymap for the merge buffer, with the emerge commands defined in
510 it. Used to save the local keymap during fast mode, when the local keymap is
511 replaced by emerge-fast-keymap.")
512 (emerge-defvar-local emerge-old-keymap nil
513 "The original local keymap for the merge buffer.")
514 (emerge-defvar-local emerge-auto-advance nil
515 "If non-nil, emerge-select-A and emerge-select-B automatically advance to
516 the next difference.")
517 (emerge-defvar-local emerge-skip-prefers nil
518 "If non-nil, differences for which there is a preference are automatically
520 (emerge-defvar-local emerge-quit-hook nil
521 "Hooks to run in the merge buffer after the merge has been finished.
522 `emerge-prefix-argument' will hold the prefix argument of the `emerge-quit'
524 This is *not* a user option, since Emerge uses it for its own processing.")
525 (emerge-defvar-local emerge-output-description nil
526 "Describes output destination of emerge, for `emerge-file-names'.")
528 ;;; Setup functions for two-file mode.
530 (defun emerge-files-internal (file-A file-B
&optional startup-hooks quit-hooks
532 (if (not (file-readable-p file-A
))
533 (error "File `%s' does not exist or is not readable" file-A
))
534 (if (not (file-readable-p file-B
))
535 (error "File `%s' does not exist or is not readable" file-B
))
536 (let ((buffer-A (find-file-noselect file-A
))
537 (buffer-B (find-file-noselect file-B
)))
538 ;; Record the directories of the files
539 (setq emerge-last-dir-A
(file-name-directory file-A
))
540 (setq emerge-last-dir-B
(file-name-directory file-B
))
542 (setq emerge-last-dir-output
(file-name-directory output-file
)))
543 ;; Make sure the entire files are seen, and they reflect what is on disk
547 (let ((temp (file-local-copy file-A
)))
551 (cons `(lambda () (delete-file ,file-A
))
553 ;; Verify that the file matches the buffer
554 (emerge-verify-file-buffer))))
558 (let ((temp (file-local-copy file-B
)))
562 (cons `(lambda () (delete-file ,file-B
))
564 ;; Verify that the file matches the buffer
565 (emerge-verify-file-buffer))))
566 (emerge-setup buffer-A file-A buffer-B file-B startup-hooks quit-hooks
569 ;; Start up Emerge on two files
570 (defun emerge-setup (buffer-A file-A buffer-B file-B startup-hooks quit-hooks
572 (setq file-A
(expand-file-name file-A
))
573 (setq file-B
(expand-file-name file-B
))
574 (setq output-file
(and output-file
(expand-file-name output-file
)))
575 (let* ((merge-buffer-name (emerge-unique-buffer-name "*merge" "*"))
576 ;; create the merge buffer from buffer A, so it inherits buffer A's
577 ;; default directory, etc.
578 (merge-buffer (with-current-buffer
580 (get-buffer-create merge-buffer-name
))))
583 (emerge-copy-modes buffer-A
)
584 (setq buffer-read-only nil
)
587 (setq emerge-A-buffer buffer-A
)
588 (setq emerge-B-buffer buffer-B
)
589 (setq emerge-ancestor-buffer nil
)
590 (setq emerge-merge-buffer merge-buffer
)
591 (setq emerge-output-description
593 (concat "Output to file: " output-file
)
594 (concat "Output to buffer: " (buffer-name merge-buffer
))))
595 (save-excursion (insert-buffer-substring emerge-A-buffer
))
597 (setq emerge-difference-list
(emerge-make-diff-list file-A file-B
))
598 (setq emerge-number-of-differences
(length emerge-difference-list
))
599 (setq emerge-current-difference -
1)
600 (setq emerge-quit-hook quit-hooks
)
601 (emerge-remember-buffer-characteristics)
602 (emerge-handle-local-variables))
603 (emerge-setup-windows buffer-A buffer-B merge-buffer t
)
604 (with-current-buffer merge-buffer
605 (run-hooks 'startup-hooks
'emerge-startup-hook
)
606 (setq buffer-read-only t
))))
608 ;; Generate the Emerge difference list between two files
609 (defun emerge-make-diff-list (file-A file-B
)
610 (setq emerge-diff-buffer
(get-buffer-create "*emerge-diff*"))
615 (format "%s %s %s %s"
616 emerge-diff-program emerge-diff-options
617 (emerge-protect-metachars file-A
)
618 (emerge-protect-metachars file-B
))
620 (emerge-prepare-error-list emerge-diff-ok-lines-regexp
)
621 (emerge-convert-diffs-to-markers
622 emerge-A-buffer emerge-B-buffer emerge-merge-buffer
623 (emerge-extract-diffs emerge-diff-buffer
)))
625 (defun emerge-extract-diffs (diff-buffer)
629 (goto-char (point-min))
630 (while (re-search-forward emerge-match-diff-line nil t
)
631 (let* ((a-begin (string-to-number (buffer-substring (match-beginning 1)
633 (a-end (let ((b (match-beginning 3))
636 (string-to-number (buffer-substring b e
))
638 (diff-type (buffer-substring (match-beginning 4) (match-end 4)))
639 (b-begin (string-to-number (buffer-substring (match-beginning 5)
641 (b-end (let ((b (match-beginning 7))
644 (string-to-number (buffer-substring b e
))
646 ;; fix the beginning and end numbers, because diff is somewhat
647 ;; strange about how it numbers lines
648 (if (string-equal diff-type
"a")
650 (setq b-end
(1+ b-end
))
651 (setq a-begin
(1+ a-begin
))
652 (setq a-end a-begin
))
653 (if (string-equal diff-type
"d")
655 (setq a-end
(1+ a-end
))
656 (setq b-begin
(1+ b-begin
))
657 (setq b-end b-begin
))
658 ;; (string-equal diff-type "c")
660 (setq a-end
(1+ a-end
))
661 (setq b-end
(1+ b-end
)))))
662 (setq list
(cons (vector a-begin a-end
668 ;; Set up buffer of diff/diff3 error messages.
669 (defun emerge-prepare-error-list (ok-regexp)
670 (setq emerge-diff-error-buffer
(get-buffer-create "*emerge-diff-errors*"))
672 emerge-diff-error-buffer
674 (save-excursion (insert-buffer-substring emerge-diff-buffer
))
675 (delete-matching-lines ok-regexp
)))
677 ;;; Top-level and setup functions for three-file mode.
679 (defun emerge-files-with-ancestor-internal (file-A file-B file-ancestor
680 &optional startup-hooks quit-hooks
682 (if (not (file-readable-p file-A
))
683 (error "File `%s' does not exist or is not readable" file-A
))
684 (if (not (file-readable-p file-B
))
685 (error "File `%s' does not exist or is not readable" file-B
))
686 (if (not (file-readable-p file-ancestor
))
687 (error "File `%s' does not exist or is not readable" file-ancestor
))
688 (let ((buffer-A (find-file-noselect file-A
))
689 (buffer-B (find-file-noselect file-B
))
690 (buffer-ancestor (find-file-noselect file-ancestor
)))
691 ;; Record the directories of the files
692 (setq emerge-last-dir-A
(file-name-directory file-A
))
693 (setq emerge-last-dir-B
(file-name-directory file-B
))
694 (setq emerge-last-dir-ancestor
(file-name-directory file-ancestor
))
696 (setq emerge-last-dir-output
(file-name-directory output-file
)))
697 ;; Make sure the entire files are seen, and they reflect what is on disk
701 (let ((temp (file-local-copy file-A
)))
705 (cons `(lambda () (delete-file ,file-A
))
707 ;; Verify that the file matches the buffer
708 (emerge-verify-file-buffer))))
712 (let ((temp (file-local-copy file-B
)))
716 (cons `(lambda () (delete-file ,file-B
))
718 ;; Verify that the file matches the buffer
719 (emerge-verify-file-buffer))))
723 (let ((temp (file-local-copy file-ancestor
)))
725 (setq file-ancestor temp
727 (cons `(lambda () (delete-file ,file-ancestor
))
729 ;; Verify that the file matches the buffer
730 (emerge-verify-file-buffer))))
731 (emerge-setup-with-ancestor buffer-A file-A buffer-B file-B
732 buffer-ancestor file-ancestor
733 startup-hooks quit-hooks output-file
)))
735 ;; Start up Emerge on two files with an ancestor
736 (defun emerge-setup-with-ancestor (buffer-A file-A buffer-B file-B
737 buffer-ancestor file-ancestor
738 &optional startup-hooks quit-hooks
740 (setq file-A
(expand-file-name file-A
))
741 (setq file-B
(expand-file-name file-B
))
742 (setq file-ancestor
(expand-file-name file-ancestor
))
743 (setq output-file
(and output-file
(expand-file-name output-file
)))
744 (let* ((merge-buffer-name (emerge-unique-buffer-name "*merge" "*"))
745 ;; create the merge buffer from buffer A, so it inherits buffer A's
746 ;; default directory, etc.
747 (merge-buffer (with-current-buffer
749 (get-buffer-create merge-buffer-name
))))
752 (emerge-copy-modes buffer-A
)
753 (setq buffer-read-only nil
)
756 (setq emerge-A-buffer buffer-A
)
757 (setq emerge-B-buffer buffer-B
)
758 (setq emerge-ancestor-buffer buffer-ancestor
)
759 (setq emerge-merge-buffer merge-buffer
)
760 (setq emerge-output-description
762 (concat "Output to file: " output-file
)
763 (concat "Output to buffer: " (buffer-name merge-buffer
))))
764 (save-excursion (insert-buffer-substring emerge-A-buffer
))
766 (setq emerge-difference-list
767 (emerge-make-diff3-list file-A file-B file-ancestor
))
768 (setq emerge-number-of-differences
(length emerge-difference-list
))
769 (setq emerge-current-difference -
1)
770 (setq emerge-quit-hook quit-hooks
)
771 (emerge-remember-buffer-characteristics)
772 (emerge-select-prefer-Bs)
773 (emerge-handle-local-variables))
774 (emerge-setup-windows buffer-A buffer-B merge-buffer t
)
775 (with-current-buffer merge-buffer
776 (run-hooks 'startup-hooks
'emerge-startup-hook
)
777 (setq buffer-read-only t
))))
779 ;; Generate the Emerge difference list between two files with an ancestor
780 (defun emerge-make-diff3-list (file-A file-B file-ancestor
)
781 (setq emerge-diff-buffer
(get-buffer-create "*emerge-diff*"))
786 (format "%s %s %s %s %s"
787 emerge-diff3-program emerge-diff-options
788 (emerge-protect-metachars file-A
)
789 (emerge-protect-metachars file-ancestor
)
790 (emerge-protect-metachars file-B
))
792 (emerge-prepare-error-list emerge-diff3-ok-lines-regexp
)
793 (emerge-convert-diffs-to-markers
794 emerge-A-buffer emerge-B-buffer emerge-merge-buffer
795 (emerge-extract-diffs3 emerge-diff-buffer
)))
797 (defun emerge-extract-diffs3 (diff-buffer)
801 (while (re-search-forward "^====\\(.?\\)$" nil t
)
802 ;; leave point after matched line
803 (beginning-of-line 2)
804 (let ((agreement (buffer-substring (match-beginning 1) (match-end 1))))
805 ;; if the A and B files are the same, ignore the difference
806 (if (not (string-equal agreement
"2"))
809 (let (group-1 group-3 pos
)
811 (setq group-1
(emerge-get-diff3-group "1"))
813 (setq group-3
(emerge-get-diff3-group "3"))
814 (vector (car group-1
) (car (cdr group-1
))
815 (car group-3
) (car (cdr group-3
))
816 (cond ((string-equal agreement
"1") 'prefer-A
)
817 ((string-equal agreement
"3") 'prefer-B
)
822 (defun emerge-get-diff3-group (file)
823 ;; This save-excursion allows emerge-get-diff3-group to be called for the
824 ;; various groups of lines (1, 2, 3) in any order, and for the lines to
825 ;; appear in any order. The reason this is necessary is that Gnu diff3
826 ;; can produce the groups in the order 1, 2, 3 or 1, 3, 2.
829 (concat "^" file
":\\([0-9]+\\)\\(,\\([0-9]+\\)\\)?\\([ac]\\)$"))
830 (beginning-of-line 2)
831 ;; treatment depends on whether it is an "a" group or a "c" group
832 (if (string-equal (buffer-substring (match-beginning 4) (match-end 4)) "c")
834 (if (match-beginning 2)
835 ;; it has two numbers
836 (list (string-to-number
837 (buffer-substring (match-beginning 1) (match-end 1)))
838 (1+ (string-to-number
839 (buffer-substring (match-beginning 3) (match-end 3)))))
841 (let ((x (string-to-number
842 (buffer-substring (match-beginning 1) (match-end 1)))))
844 ;; it is an "a" group
845 (let ((x (1+ (string-to-number
846 (buffer-substring (match-beginning 1) (match-end 1))))))
849 ;;; Functions to start Emerge on files
852 (defun emerge-files (_arg file-A file-B file-out
&optional startup-hooks
854 "Run Emerge on two files."
857 (list current-prefix-arg
858 (setq f
(emerge-read-file-name "File A to merge" emerge-last-dir-A
860 (emerge-read-file-name "File B to merge" emerge-last-dir-B nil f t
)
861 (and current-prefix-arg
862 (emerge-read-file-name "Output file" emerge-last-dir-output
865 (add-hook 'quit-hooks
`(lambda () (emerge-files-exit ,file-out
))))
866 (emerge-files-internal
867 file-A file-B startup-hooks
872 (defun emerge-files-with-ancestor (_arg file-A file-B file-ancestor file-out
873 &optional startup-hooks quit-hooks
)
874 "Run Emerge on two files, giving another file as the ancestor."
877 (list current-prefix-arg
878 (setq f
(emerge-read-file-name "File A to merge" emerge-last-dir-A
880 (emerge-read-file-name "File B to merge" emerge-last-dir-B nil f t
)
881 (emerge-read-file-name "Ancestor file" emerge-last-dir-ancestor
883 (and current-prefix-arg
884 (emerge-read-file-name "Output file" emerge-last-dir-output
887 (add-hook 'quit-hooks
`(lambda () (emerge-files-exit ,file-out
))))
888 (emerge-files-with-ancestor-internal
889 file-A file-B file-ancestor startup-hooks
893 ;; Write the merge buffer out in place of the file the A buffer is visiting.
894 (defun emerge-files-exit (file-out)
895 ;; if merge was successful was given, save to disk
896 (if (not emerge-prefix-argument
)
897 (emerge-write-and-delete file-out
)))
899 ;;; Functions to start Emerge on buffers
902 (defun emerge-buffers (buffer-A buffer-B
&optional startup-hooks quit-hooks
)
903 "Run Emerge on two buffers."
904 (interactive "bBuffer A to merge: \nbBuffer B to merge: ")
905 (let ((emerge-file-A (emerge-make-temp-file "A"))
906 (emerge-file-B (emerge-make-temp-file "B")))
909 (write-region (point-min) (point-max) emerge-file-A nil
'no-message
))
912 (write-region (point-min) (point-max) emerge-file-B nil
'no-message
))
913 (emerge-setup (get-buffer buffer-A
) emerge-file-A
914 (get-buffer buffer-B
) emerge-file-B
916 (delete-file ,emerge-file-A
)
917 (delete-file ,emerge-file-B
))
923 (defun emerge-buffers-with-ancestor (buffer-A buffer-B buffer-ancestor
924 &optional startup-hooks
926 "Run Emerge on two buffers, giving another buffer as the ancestor."
928 "bBuffer A to merge: \nbBuffer B to merge: \nbAncestor buffer: ")
929 (let ((emerge-file-A (emerge-make-temp-file "A"))
930 (emerge-file-B (emerge-make-temp-file "B"))
931 (emerge-file-ancestor (emerge-make-temp-file "anc")))
934 (write-region (point-min) (point-max) emerge-file-A nil
'no-message
))
937 (write-region (point-min) (point-max) emerge-file-B nil
'no-message
))
940 (write-region (point-min) (point-max) emerge-file-ancestor nil
942 (emerge-setup-with-ancestor (get-buffer buffer-A
) emerge-file-A
943 (get-buffer buffer-B
) emerge-file-B
944 (get-buffer buffer-ancestor
)
947 (delete-file ,emerge-file-A
)
948 (delete-file ,emerge-file-B
)
950 ,emerge-file-ancestor
))
955 ;;; Functions to start Emerge from the command line
958 (defun emerge-files-command ()
959 (let ((file-a (nth 0 command-line-args-left
))
960 (file-b (nth 1 command-line-args-left
))
961 (file-out (nth 2 command-line-args-left
)))
962 (setq command-line-args-left
(nthcdr 3 command-line-args-left
))
963 (emerge-files-internal
965 (list `(lambda () (emerge-command-exit ,file-out
))))))
968 (defun emerge-files-with-ancestor-command ()
969 (let (file-a file-b file-anc file-out
)
970 ;; check for a -a flag, for filemerge compatibility
971 (if (string= (car command-line-args-left
) "-a")
972 ;; arguments are "-a ancestor file-a file-b file-out"
974 (setq file-a
(nth 2 command-line-args-left
))
975 (setq file-b
(nth 3 command-line-args-left
))
976 (setq file-anc
(nth 1 command-line-args-left
))
977 (setq file-out
(nth 4 command-line-args-left
))
978 (setq command-line-args-left
(nthcdr 5 command-line-args-left
)))
979 ;; arguments are "file-a file-b ancestor file-out"
980 (setq file-a
(nth 0 command-line-args-left
))
981 (setq file-b
(nth 1 command-line-args-left
))
982 (setq file-anc
(nth 2 command-line-args-left
))
983 (setq file-out
(nth 3 command-line-args-left
))
984 (setq command-line-args-left
(nthcdr 4 command-line-args-left
)))
985 (emerge-files-with-ancestor-internal
986 file-a file-b file-anc nil
987 (list `(lambda () (emerge-command-exit ,file-out
))))))
989 (defun emerge-command-exit (file-out)
990 (emerge-write-and-delete file-out
)
991 (kill-emacs (if emerge-prefix-argument
1 0)))
993 ;;; Functions to start Emerge via remote request
996 (defun emerge-files-remote (file-a file-b file-out
)
997 (setq emerge-file-out file-out
)
998 (emerge-files-internal
1000 (list `(lambda () (emerge-remote-exit ,file-out
',emerge-exit-func
)))
1002 (throw 'client-wait nil
))
1005 (defun emerge-files-with-ancestor-remote (file-a file-b file-anc file-out
)
1006 (setq emerge-file-out file-out
)
1007 (emerge-files-with-ancestor-internal
1008 file-a file-b file-anc nil
1009 (list `(lambda () (emerge-remote-exit ,file-out
',emerge-exit-func
)))
1011 (throw 'client-wait nil
))
1013 (defun emerge-remote-exit (file-out emerge-exit-func
)
1014 (emerge-write-and-delete file-out
)
1015 (kill-buffer emerge-merge-buffer
)
1016 (funcall emerge-exit-func
(if emerge-prefix-argument
1 0)))
1018 ;;; Functions to start Emerge on RCS versions
1021 (defun emerge-revisions (arg file revision-A revision-B
1022 &optional startup-hooks quit-hooks
)
1023 "Emerge two RCS revisions of a file."
1025 (list current-prefix-arg
1026 (read-file-name "File to merge: " nil nil
'confirm
)
1027 (read-string "Revision A to merge: " emerge-last-revision-A
)
1028 (read-string "Revision B to merge: " emerge-last-revision-B
)))
1029 (setq emerge-last-revision-A revision-A
1030 emerge-last-revision-B revision-B
)
1031 (emerge-revisions-internal
1032 file revision-A revision-B startup-hooks
1036 ,(format "%s %s" emerge-rcs-ci-program file
)))
1041 (defun emerge-revisions-with-ancestor (arg file revision-A
1044 startup-hooks quit-hooks
)
1045 "Emerge two RCS revisions of a file, with another revision as ancestor."
1047 (list current-prefix-arg
1048 (read-file-name "File to merge: " nil nil
'confirm
)
1049 (read-string "Revision A to merge: " emerge-last-revision-A
)
1050 (read-string "Revision B to merge: " emerge-last-revision-B
)
1051 (read-string "Ancestor: " emerge-last-revision-ancestor
)))
1052 (setq emerge-last-revision-A revision-A
1053 emerge-last-revision-B revision-B
1054 emerge-last-revision-ancestor ancestor
)
1055 (emerge-revision-with-ancestor-internal
1056 file revision-A revision-B ancestor startup-hooks
1061 ,(format "%s %s" emerge-rcs-ci-program file
)))
1065 (defun emerge-revisions-internal (file revision-A revision-B
&optional
1066 startup-hooks quit-hooks _output-file
)
1067 (let ((buffer-A (get-buffer-create (format "%s,%s" file revision-A
)))
1068 (buffer-B (get-buffer-create (format "%s,%s" file revision-B
)))
1069 (emerge-file-A (emerge-make-temp-file "A"))
1070 (emerge-file-B (emerge-make-temp-file "B")))
1071 ;; Get the revisions into buffers
1072 (with-current-buffer
1076 (format "%s -q -p%s %s" emerge-rcs-co-program revision-A file
)
1078 (write-region (point-min) (point-max) emerge-file-A nil
'no-message
)
1079 (set-buffer-modified-p nil
))
1080 (with-current-buffer
1084 (format "%s -q -p%s %s" emerge-rcs-co-program revision-B file
)
1086 (write-region (point-min) (point-max) emerge-file-B nil
'no-message
)
1087 (set-buffer-modified-p nil
))
1089 (emerge-setup buffer-A emerge-file-A
1090 buffer-B emerge-file-B
1092 (delete-file ,emerge-file-A
)
1093 (delete-file ,emerge-file-B
))
1095 (cons `(lambda () (emerge-files-exit ,file
))
1099 (defun emerge-revision-with-ancestor-internal (file revision-A revision-B
1101 &optional startup-hooks
1102 quit-hooks output-file
)
1103 (let ((buffer-A (get-buffer-create (format "%s,%s" file revision-A
)))
1104 (buffer-B (get-buffer-create (format "%s,%s" file revision-B
)))
1105 (buffer-ancestor (get-buffer-create (format "%s,%s" file ancestor
)))
1106 (emerge-file-A (emerge-make-temp-file "A"))
1107 (emerge-file-B (emerge-make-temp-file "B"))
1108 (emerge-ancestor (emerge-make-temp-file "ancestor")))
1109 ;; Get the revisions into buffers
1110 (with-current-buffer
1114 (format "%s -q -p%s %s" emerge-rcs-co-program
1117 (write-region (point-min) (point-max) emerge-file-A nil
'no-message
)
1118 (set-buffer-modified-p nil
))
1119 (with-current-buffer
1123 (format "%s -q -p%s %s" emerge-rcs-co-program revision-B file
)
1125 (write-region (point-min) (point-max) emerge-file-B nil
'no-message
)
1126 (set-buffer-modified-p nil
))
1127 (with-current-buffer
1131 (format "%s -q -p%s %s" emerge-rcs-co-program ancestor file
)
1133 (write-region (point-min) (point-max) emerge-ancestor nil
'no-message
)
1134 (set-buffer-modified-p nil
))
1136 (emerge-setup-with-ancestor
1137 buffer-A emerge-file-A buffer-B emerge-file-B
1138 buffer-ancestor emerge-ancestor
1140 (delete-file ,emerge-file-A
)
1141 (delete-file ,emerge-file-B
)
1142 (delete-file ,emerge-ancestor
))
1144 (cons `(lambda () (emerge-files-exit ,file
))
1148 ;;; Function to start Emerge based on a line in a file
1150 (defun emerge-execute-line ()
1151 "Run Emerge using files named in current text line.
1152 Looks in that line for whitespace-separated entries of these forms:
1157 to specify the files to use in Emerge.
1159 In addition, if only one of `a=file' or `b=file' is present, and `output=file'
1161 If `emerge-execute-line-deletions' is non-nil and `ancestor=file' is present,
1162 it is assumed that the file in question has been deleted, and it is
1163 not copied to the output file.
1164 Otherwise, the A or B file present is copied to the output file."
1166 (let (file-A file-B file-ancestor file-out
1167 (case-fold-search t
))
1168 ;; Stop if at end of buffer (even though we might be in a line, if
1169 ;; the line does not end with newline)
1171 (error "At end of buffer"))
1172 ;; Go to the beginning of the line
1174 ;; Skip any initial whitespace
1175 (if (looking-at "[ \t]*")
1176 (goto-char (match-end 0)))
1177 ;; Process the entire line
1179 ;; Get the next entry
1180 (if (looking-at "\\([a-z]+\\)=\\([^ \t\n]+\\)[ \t]*")
1181 ;; Break apart the tab (before =) and the filename (after =)
1182 (let ((tag (downcase
1183 (buffer-substring (match-beginning 1) (match-end 1))))
1184 (file (buffer-substring (match-beginning 2) (match-end 2))))
1185 ;; Move point after the entry
1186 (goto-char (match-end 0))
1187 ;; Store the filename in the right variable
1189 ((string-equal tag
"a")
1191 (error "This line has two `A' entries"))
1193 ((string-equal tag
"b")
1195 (error "This line has two `B' entries"))
1197 ((or (string-equal tag
"anc") (string-equal tag
"ancestor"))
1199 (error "This line has two `ancestor' entries"))
1200 (setq file-ancestor file
))
1201 ((or (string-equal tag
"out") (string-equal tag
"output"))
1203 (error "This line has two `output' entries"))
1204 (setq file-out file
))
1206 (error "Unrecognized entry"))))
1207 ;; If the match on the entry pattern failed
1208 (error "Unparsable entry")))
1209 ;; Make sure that file-A and file-B are present
1210 (if (not (or (and file-A file-B
) file-out
))
1211 (error "Must have both `A' and `B' entries"))
1212 (if (not (or file-A file-B
))
1213 (error "Must have `A' or `B' entry"))
1214 ;; Go to the beginning of the next line, so next execution will use
1215 ;; next line in buffer.
1216 (beginning-of-line 2)
1217 ;; Execute the correct command
1219 ;; Merge of two files with ancestor
1220 ((and file-A file-B file-ancestor
)
1221 (message "Merging %s and %s..." file-A file-B
)
1222 (emerge-files-with-ancestor (not (not file-out
)) file-A file-B
1223 file-ancestor file-out
1225 ;; When done, return to this buffer.
1228 (switch-to-buffer ,(current-buffer))
1229 (message "Merge done.")))))
1230 ;; Merge of two files without ancestor
1231 ((and file-A file-B
)
1232 (message "Merging %s and %s..." file-A file-B
)
1233 (emerge-files (not (not file-out
)) file-A file-B file-out
1235 ;; When done, return to this buffer.
1238 (switch-to-buffer ,(current-buffer))
1239 (message "Merge done.")))))
1240 ;; There is an output file (or there would have been an error above),
1241 ;; but only one input file.
1242 ;; The file appears to have been deleted in one version; do nothing.
1243 ((and file-ancestor emerge-execute-line-deletions
)
1244 (message "No action."))
1245 ;; The file should be copied from the version that contains it
1246 (t (let ((input-file (or file-A file-B
)))
1247 (message "Copying...")
1248 (copy-file input-file file-out
)
1249 (message "%s copied to %s." input-file file-out
))))))
1251 ;;; Sample function for creating information for emerge-execute-line
1253 (defcustom emerge-merge-directories-filename-regexp
"[^.]"
1254 "Regexp describing files to be processed by `emerge-merge-directories'."
1259 (defun emerge-merge-directories (a-dir b-dir ancestor-dir output-dir
)
1262 (read-directory-name "A directory: " nil nil
'confirm
)
1263 (read-directory-name "B directory: " nil nil
'confirm
)
1264 (read-directory-name "Ancestor directory (null for none): " nil nil
'confirm
)
1265 (read-directory-name "Output directory (null for none): " nil nil
'confirm
)))
1266 ;; Check that we're not on a line
1267 (if (not (and (bolp) (eolp)))
1268 (error "There is text on this line"))
1269 ;; Turn null strings into nil to indicate directories not used.
1270 (if (and ancestor-dir
(string-equal ancestor-dir
""))
1271 (setq ancestor-dir nil
))
1272 (if (and output-dir
(string-equal output-dir
""))
1273 (setq output-dir nil
))
1274 ;; Canonicalize the directory names
1275 (setq a-dir
(expand-file-name a-dir
))
1276 (if (not (string-equal (substring a-dir -
1) "/"))
1277 (setq a-dir
(concat a-dir
"/")))
1278 (setq b-dir
(expand-file-name b-dir
))
1279 (if (not (string-equal (substring b-dir -
1) "/"))
1280 (setq b-dir
(concat b-dir
"/")))
1283 (setq ancestor-dir
(expand-file-name ancestor-dir
))
1284 (if (not (string-equal (substring ancestor-dir -
1) "/"))
1285 (setq ancestor-dir
(concat ancestor-dir
"/")))))
1288 (setq output-dir
(expand-file-name output-dir
))
1289 (if (not (string-equal (substring output-dir -
1) "/"))
1290 (setq output-dir
(concat output-dir
"/")))))
1291 ;; Set the mark to where we start
1293 ;; Find out what files are in the directories.
1295 (directory-files a-dir nil emerge-merge-directories-filename-regexp
))
1297 (directory-files b-dir nil emerge-merge-directories-filename-regexp
))
1300 (directory-files ancestor-dir nil
1301 emerge-merge-directories-filename-regexp
)))
1302 (all-files (sort (nconc (copy-sequence a-dir-files
)
1303 (copy-sequence b-dir-files
)
1304 (copy-sequence ancestor-dir-files
))
1305 (function string-lessp
))))
1306 ;; Remove duplicates from all-files.
1307 (let ((p all-files
))
1309 (if (and (cdr p
) (string-equal (car p
) (car (cdr p
))))
1310 (setcdr p
(cdr (cdr p
)))
1312 ;; Generate the control lines for the various files.
1314 (let ((f (car all-files
)))
1315 (setq all-files
(cdr all-files
))
1316 (if (and a-dir-files
(string-equal (car a-dir-files
) f
))
1318 (insert "A=" a-dir f
"\t")
1319 (setq a-dir-files
(cdr a-dir-files
))))
1320 (if (and b-dir-files
(string-equal (car b-dir-files
) f
))
1322 (insert "B=" b-dir f
"\t")
1323 (setq b-dir-files
(cdr b-dir-files
))))
1324 (if (and ancestor-dir-files
(string-equal (car ancestor-dir-files
) f
))
1326 (insert "ancestor=" ancestor-dir f
"\t")
1327 (setq ancestor-dir-files
(cdr ancestor-dir-files
))))
1329 (insert "output=" output-dir f
"\t"))
1330 (backward-delete-char 1)
1333 ;;; Common setup routines
1335 ;; Set up the window configuration. If POS is given, set the points to
1336 ;; the beginnings of the buffers.
1337 (defun emerge-setup-windows (buffer-A buffer-B merge-buffer
&optional pos
)
1338 ;; Make sure we are not in the minibuffer window when we try to delete
1339 ;; all other windows.
1340 (if (eq (selected-window) (minibuffer-window))
1342 (delete-other-windows)
1343 (switch-to-buffer merge-buffer
)
1344 (emerge-refresh-mode-line)
1345 (split-window-below)
1346 (split-window-right)
1347 (switch-to-buffer buffer-A
)
1349 (goto-char (point-min)))
1351 (switch-to-buffer buffer-B
)
1353 (goto-char (point-min)))
1356 (goto-char (point-min)))
1357 ;; If diff/diff3 reports errors, display them rather than the merge buffer.
1358 (if (/= 0 (with-current-buffer emerge-diff-error-buffer
(buffer-size)))
1361 (message "Errors found in diff/diff3 output. Merge buffer is %s."
1362 (buffer-name emerge-merge-buffer
))
1363 (switch-to-buffer emerge-diff-error-buffer
))))
1365 ;; Set up the keymap in the merge buffer
1366 (defun emerge-set-keys ()
1367 ;; Set up fixed keymaps if necessary
1368 (if (not emerge-basic-keymap
)
1369 (emerge-setup-fixed-keymaps))
1370 ;; Save the old local map
1371 (setq emerge-old-keymap
(current-local-map))
1372 ;; Construct the edit keymap
1373 (setq emerge-edit-keymap
(if emerge-old-keymap
1374 (copy-keymap emerge-old-keymap
)
1375 (make-sparse-keymap)))
1376 ;; Install the Emerge commands
1377 (emerge-force-define-key emerge-edit-keymap emerge-command-prefix
1378 'emerge-basic-keymap
)
1379 (define-key emerge-edit-keymap
[menu-bar
] (make-sparse-keymap))
1381 ;; Create the additional menu bar items.
1382 (define-key emerge-edit-keymap
[menu-bar emerge-options
]
1383 (cons "Merge-Options" emerge-options-menu
))
1384 (define-key emerge-edit-keymap
[menu-bar merge
]
1385 (cons "Merge" emerge-merge-menu
))
1386 (define-key emerge-edit-keymap
[menu-bar move
]
1387 (cons "Move" emerge-move-menu
))
1389 ;; Suppress write-file and save-buffer
1390 (substitute-key-definition 'write-file
1391 'emerge-query-write-file
1393 (substitute-key-definition 'save-buffer
1394 'emerge-query-save-buffer
1396 (define-key emerge-edit-keymap
[remap write-file
] 'emerge-query-write-file
)
1397 (define-key emerge-edit-keymap
[remap save-buffer
] 'emerge-query-save-buffer
)
1398 (use-local-map emerge-fast-keymap
)
1399 (setq emerge-edit-mode nil
)
1400 (setq emerge-fast-mode t
))
1402 (defun emerge-remember-buffer-characteristics ()
1403 "Record certain properties of the buffers being merged.
1404 Must be called in the merge buffer. Remembers read-only, modified,
1405 auto-save, and saves them in buffer local variables. Sets the buffers
1406 read-only and turns off `auto-save-mode'.
1407 These characteristics are restored by `emerge-restore-buffer-characteristics'."
1408 ;; force auto-save, because we will turn off auto-saving in buffers for the
1411 ;; remember and alter buffer characteristics
1412 (setq emerge-A-buffer-values
1413 (with-current-buffer
1416 (emerge-save-variables emerge-saved-variables
)
1417 (emerge-restore-variables emerge-saved-variables
1418 emerge-merging-values
))))
1419 (setq emerge-B-buffer-values
1420 (with-current-buffer
1423 (emerge-save-variables emerge-saved-variables
)
1424 (emerge-restore-variables emerge-saved-variables
1425 emerge-merging-values
)))))
1427 (defun emerge-restore-buffer-characteristics ()
1428 "Restore characteristics saved by `emerge-remember-buffer-characteristics'."
1429 (let ((A-values emerge-A-buffer-values
)
1430 (B-values emerge-B-buffer-values
))
1431 (with-current-buffer emerge-A-buffer
1432 (emerge-restore-variables emerge-saved-variables
1434 (with-current-buffer emerge-B-buffer
1435 (emerge-restore-variables emerge-saved-variables
1438 ;; Move to line DESIRED-LINE assuming we are at line CURRENT-LINE.
1439 ;; Return DESIRED-LINE.
1440 (defun emerge-goto-line (desired-line current-line
)
1441 (forward-line (- desired-line current-line
))
1444 (defun emerge-convert-diffs-to-markers (A-buffer
1449 (A-point-min (with-current-buffer A-buffer
(point-min)))
1450 (offset (1- A-point-min
))
1451 (B-point-min (with-current-buffer B-buffer
(point-min)))
1452 ;; Record current line number in each buffer
1453 ;; so we don't have to count from the beginning.
1456 (with-current-buffer A-buffer
(goto-char (point-min)))
1457 (with-current-buffer B-buffer
(goto-char (point-min)))
1459 (let* ((list-element (car lineno-list
))
1466 (a-begin (aref list-element
0))
1467 (a-end (aref list-element
1))
1468 (b-begin (aref list-element
2))
1469 (b-end (aref list-element
3))
1470 (state (aref list-element
4)))
1471 ;; place markers at the appropriate places in the buffers
1472 (with-current-buffer
1474 (setq a-line
(emerge-goto-line a-begin a-line
))
1475 (setq a-begin-marker
(point-marker))
1476 (setq a-line
(emerge-goto-line a-end a-line
))
1477 (setq a-end-marker
(point-marker)))
1478 (with-current-buffer
1480 (setq b-line
(emerge-goto-line b-begin b-line
))
1481 (setq b-begin-marker
(point-marker))
1482 (setq b-line
(emerge-goto-line b-end b-line
))
1483 (setq b-end-marker
(point-marker)))
1484 (setq merge-begin-marker
(set-marker
1486 (- (marker-position a-begin-marker
)
1489 (setq merge-end-marker
(set-marker
1491 (- (marker-position a-end-marker
)
1494 ;; record all the markers for this difference
1495 (setq marker-list
(cons (vector a-begin-marker a-end-marker
1496 b-begin-marker b-end-marker
1497 merge-begin-marker merge-end-marker
1500 (setq lineno-list
(cdr lineno-list
)))
1501 ;; convert the list of difference information into a vector for
1503 (setq emerge-difference-list
(apply 'vector
(nreverse marker-list
)))))
1505 ;; If we have an ancestor, select all B variants that we prefer
1506 (defun emerge-select-prefer-Bs ()
1508 (while (< n emerge-number-of-differences
)
1509 (if (eq (aref (aref emerge-difference-list n
) 6) 'prefer-B
)
1511 (emerge-unselect-and-select-difference n t
)
1513 (aset (aref emerge-difference-list n
) 6 'prefer-B
)))
1515 (emerge-unselect-and-select-difference -
1))
1517 ;; Process the local-variables list at the end of the merged file, if
1519 (defun emerge-handle-local-variables ()
1520 (if emerge-process-local-variables
1522 (hack-local-variables)
1523 (error (message "Local-variables error in merge buffer: %s"
1524 (prin1-to-string err
))))))
1526 ;;; Common exit routines
1528 (defun emerge-write-and-delete (file-out)
1529 ;; clear screen format
1530 (delete-other-windows)
1531 ;; delete A, B, and ancestor buffers, if they haven't been changed
1532 (if (not (buffer-modified-p emerge-A-buffer
))
1533 (kill-buffer emerge-A-buffer
))
1534 (if (not (buffer-modified-p emerge-B-buffer
))
1535 (kill-buffer emerge-B-buffer
))
1536 (if (and emerge-ancestor-buffer
1537 (not (buffer-modified-p emerge-ancestor-buffer
)))
1538 (kill-buffer emerge-ancestor-buffer
))
1539 ;; Write merge buffer to file
1541 (write-file file-out
)))
1545 (defun emerge-recenter (&optional arg
)
1546 "Bring the highlighted region of all three merge buffers into view.
1547 This brings the buffers into view if they are in windows.
1548 With an argument, reestablish the default three-window display."
1550 ;; If there is an argument, rebuild the window structure
1552 (emerge-setup-windows emerge-A-buffer emerge-B-buffer
1553 emerge-merge-buffer
))
1554 ;; Redisplay whatever buffers are showing, if there is a selected difference
1555 (if (and (>= emerge-current-difference
0)
1556 (< emerge-current-difference emerge-number-of-differences
))
1557 (let* ((merge-buffer emerge-merge-buffer
)
1558 (buffer-A emerge-A-buffer
)
1559 (buffer-B emerge-B-buffer
)
1560 (window-A (get-buffer-window buffer-A
'visible
))
1561 (window-B (get-buffer-window buffer-B
'visible
))
1562 (merge-window (get-buffer-window merge-buffer
))
1564 (aref emerge-difference-list emerge-current-difference
)))
1566 (select-window window-A
)
1567 (emerge-position-region
1568 (- (aref diff-vector
0)
1569 (1- emerge-before-flag-length
))
1570 (+ (aref diff-vector
1)
1571 (1- emerge-after-flag-length
))
1572 (1+ (aref diff-vector
0)))))
1574 (select-window window-B
)
1575 (emerge-position-region
1576 (- (aref diff-vector
2)
1577 (1- emerge-before-flag-length
))
1578 (+ (aref diff-vector
3)
1579 (1- emerge-after-flag-length
))
1580 (1+ (aref diff-vector
2)))))
1581 (if merge-window
(progn
1582 (select-window merge-window
)
1583 (emerge-position-region
1584 (- (aref diff-vector
4)
1585 (1- emerge-before-flag-length
))
1586 (+ (aref diff-vector
5)
1587 (1- emerge-after-flag-length
))
1588 (1+ (aref diff-vector
4))))))))
1590 ;;; Window scrolling operations
1591 ;; These operations are designed to scroll all three windows the same amount,
1592 ;; so as to keep the text in them aligned.
1594 ;; Perform some operation on all three windows (if they are showing).
1595 ;; Catches all errors on the operation in the A and B windows, but not
1596 ;; in the merge window. Usually, errors come from scrolling off the
1597 ;; beginning or end of the buffer, and this gives a nice error message:
1598 ;; End of buffer is reported in the merge buffer, but if the scroll was
1599 ;; possible in the A or B windows, it is performed there before the error
1601 (defun emerge-operate-on-windows (operation arg
)
1602 (let* ((merge-buffer emerge-merge-buffer
)
1603 (buffer-A emerge-A-buffer
)
1604 (buffer-B emerge-B-buffer
)
1605 (window-A (get-buffer-window buffer-A
'visible
))
1606 (window-B (get-buffer-window buffer-B
'visible
))
1607 (merge-window (get-buffer-window merge-buffer
)))
1609 (select-window window-A
)
1611 (funcall operation arg
)
1614 (select-window window-B
)
1616 (funcall operation arg
)
1618 (if merge-window
(progn
1619 (select-window merge-window
)
1620 (funcall operation arg
)))))
1622 (defun emerge-scroll-up (&optional arg
)
1623 "Scroll up all three merge buffers, if they are in windows.
1624 With argument N, scroll N lines; otherwise scroll by nearly
1625 the height of the merge window.
1626 `C-u -' alone as argument scrolls half the height of the merge window."
1628 (emerge-operate-on-windows
1630 ;; calculate argument to scroll-up
1631 ;; if there is an explicit argument
1632 (if (and arg
(not (equal arg
'-
)))
1634 (prefix-numeric-value arg
)
1635 ;; if not, see if we can determine a default amount (the window height)
1636 (let ((merge-window (get-buffer-window emerge-merge-buffer
)))
1637 (if (null merge-window
)
1638 ;; no window, use nil
1640 (let ((default-amount
1641 (- (window-height merge-window
) 1 next-screen-context-lines
)))
1642 ;; the window was found
1644 ;; C-u as argument means half of default amount
1645 (/ default-amount
2)
1646 ;; no argument means default amount
1647 default-amount
)))))))
1649 (defun emerge-scroll-down (&optional arg
)
1650 "Scroll down all three merge buffers, if they are in windows.
1651 With argument N, scroll N lines; otherwise scroll by nearly
1652 the height of the merge window.
1653 `C-u -' alone as argument scrolls half the height of the merge window."
1655 (emerge-operate-on-windows
1657 ;; calculate argument to scroll-down
1658 ;; if there is an explicit argument
1659 (if (and arg
(not (equal arg
'-
)))
1661 (prefix-numeric-value arg
)
1662 ;; if not, see if we can determine a default amount (the window height)
1663 (let ((merge-window (get-buffer-window emerge-merge-buffer
)))
1664 (if (null merge-window
)
1665 ;; no window, use nil
1667 (let ((default-amount
1668 (- (window-height merge-window
) 1 next-screen-context-lines
)))
1669 ;; the window was found
1671 ;; C-u as argument means half of default amount
1672 (/ default-amount
2)
1673 ;; no argument means default amount
1674 default-amount
)))))))
1676 (defun emerge-scroll-left (&optional arg
)
1677 "Scroll left all three merge buffers, if they are in windows.
1678 If an argument is given, that is how many columns are scrolled, else nearly
1679 the width of the A and B windows. `C-u -' alone as argument scrolls half the
1680 width of the A and B windows."
1682 (emerge-operate-on-windows
1684 ;; calculate argument to scroll-left
1685 ;; if there is an explicit argument
1686 (if (and arg
(not (equal arg
'-
)))
1688 (prefix-numeric-value arg
)
1689 ;; if not, see if we can determine a default amount
1690 ;; (half the window width)
1691 (let ((merge-window (get-buffer-window emerge-merge-buffer
)))
1692 (if (null merge-window
)
1693 ;; no window, use nil
1695 (let ((default-amount
1696 (- (/ (window-width merge-window
) 2) 3)))
1697 ;; the window was found
1699 ;; C-u as argument means half of default amount
1700 (/ default-amount
2)
1701 ;; no argument means default amount
1702 default-amount
)))))))
1704 (defun emerge-scroll-right (&optional arg
)
1705 "Scroll right all three merge buffers, if they are in windows.
1706 If an argument is given, that is how many columns are scrolled, else nearly
1707 the width of the A and B windows. `C-u -' alone as argument scrolls half the
1708 width of the A and B windows."
1710 (emerge-operate-on-windows
1712 ;; calculate argument to scroll-right
1713 ;; if there is an explicit argument
1714 (if (and arg
(not (equal arg
'-
)))
1716 (prefix-numeric-value arg
)
1717 ;; if not, see if we can determine a default amount
1718 ;; (half the window width)
1719 (let ((merge-window (get-buffer-window emerge-merge-buffer
)))
1720 (if (null merge-window
)
1721 ;; no window, use nil
1723 (let ((default-amount
1724 (- (/ (window-width merge-window
) 2) 3)))
1725 ;; the window was found
1727 ;; C-u as argument means half of default amount
1728 (/ default-amount
2)
1729 ;; no argument means default amount
1730 default-amount
)))))))
1732 (defun emerge-scroll-reset ()
1733 "Reset horizontal scrolling in Emerge.
1734 This resets the horizontal scrolling of all three merge buffers
1735 to the left margin, if they are in windows."
1737 (emerge-operate-on-windows
1738 (lambda (x) (set-window-hscroll (selected-window) 0))
1741 ;; Attempt to show the region nicely.
1742 ;; If there are min-lines lines above and below the region, then don't do
1744 ;; If not, recenter the region to make it so.
1745 ;; If that isn't possible, remove context lines evenly from top and bottom
1746 ;; so the entire region shows.
1747 ;; If that isn't possible, show the top of the region.
1748 ;; BEG must be at the beginning of a line.
1749 (defun emerge-position-region (beg end pos
)
1750 ;; First test whether the entire region is visible with
1751 ;; emerge-min-visible-lines above and below it
1752 (if (not (and (<= (progn
1753 (move-to-window-line emerge-min-visible-lines
)
1757 (move-to-window-line
1758 (- (1+ emerge-min-visible-lines
)))
1760 ;; We failed that test, see if it fits at all
1761 ;; Meanwhile positioning it correctly in case it doesn't fit
1763 (set-window-start (selected-window) beg
)
1764 (if (pos-visible-in-window-p end
)
1765 ;; Determine the number of lines that the region occupies
1767 (while (> end
(progn
1768 (move-to-window-line lines
)
1770 (setq lines
(1+ lines
)))
1771 ;; And position the beginning on the right line
1773 (recenter (/ (1+ (- (1- (window-height))
1778 (defun emerge-next-difference ()
1779 "Advance to the next difference."
1781 (if (< emerge-current-difference emerge-number-of-differences
)
1782 (let ((n (1+ emerge-current-difference
)))
1783 (while (and emerge-skip-prefers
1784 (< n emerge-number-of-differences
)
1785 (memq (aref (aref emerge-difference-list n
) 6)
1786 '(prefer-A prefer-B
)))
1788 (let ((buffer-read-only nil
))
1789 (emerge-unselect-and-select-difference n
)))
1792 (defun emerge-previous-difference ()
1793 "Go to the previous difference."
1795 (if (> emerge-current-difference -
1)
1796 (let ((n (1- emerge-current-difference
)))
1797 (while (and emerge-skip-prefers
1799 (memq (aref (aref emerge-difference-list n
) 6)
1800 '(prefer-A prefer-B
)))
1802 (let ((buffer-read-only nil
))
1803 (emerge-unselect-and-select-difference n
)))
1804 (error "At beginning")))
1806 (defun emerge-jump-to-difference (difference-number)
1807 "Go to the N-th difference."
1809 (let ((buffer-read-only nil
))
1810 (setq difference-number
(1- difference-number
))
1811 (if (and (>= difference-number -
1)
1812 (< difference-number
(1+ emerge-number-of-differences
)))
1813 (emerge-unselect-and-select-difference difference-number
)
1814 (error "Bad difference number"))))
1816 (defun emerge-abort ()
1817 "Abort the Emerge session."
1821 (defun emerge-quit (arg)
1822 "Finish the Emerge session and exit Emerge.
1823 Prefix argument means to abort rather than successfully finish.
1824 The difference depends on how the merge was started,
1825 but usually means to not write over one of the original files, or to signal
1826 to some process which invoked Emerge a failure code.
1828 Unselects the selected difference, if any, restores the read-only and modified
1829 flags of the merged file buffers, restores the local keymap of the merge
1830 buffer, and sets off various emerge flags. Using Emerge commands in this
1831 buffer after this will cause serious problems."
1836 "Do you really want to successfully finish this merge? "
1837 "Do you really want to abort this merge? "))
1839 (emerge-really-quit arg
)))
1841 ;; Perform the quit operations.
1842 (defun emerge-really-quit (arg)
1843 (setq buffer-read-only nil
)
1844 (emerge-unselect-and-select-difference -
1)
1845 (emerge-restore-buffer-characteristics)
1846 ;; null out the difference markers so they don't slow down future editing
1849 (set-marker (aref d
0) nil
)
1850 (set-marker (aref d
1) nil
)
1851 (set-marker (aref d
2) nil
)
1852 (set-marker (aref d
3) nil
)
1853 (set-marker (aref d
4) nil
)
1854 (set-marker (aref d
5) nil
))
1855 emerge-difference-list
)
1856 ;; allow them to be garbage collected
1857 (setq emerge-difference-list nil
)
1858 ;; restore the local map
1859 (use-local-map emerge-old-keymap
)
1860 ;; turn off all the emerge modes
1861 (setq emerge-mode nil
)
1862 (setq emerge-fast-mode nil
)
1863 (setq emerge-edit-mode nil
)
1864 (setq emerge-auto-advance nil
)
1865 (setq emerge-skip-prefers nil
)
1866 ;; restore mode line
1867 (kill-local-variable 'mode-line-buffer-identification
)
1868 (let ((emerge-prefix-argument arg
))
1869 (run-hooks 'emerge-quit-hook
)))
1871 (defun emerge-select-A (&optional force
)
1872 "Select the A variant of this difference.
1873 Refuses to function if this difference has been edited, i.e., if it
1874 is neither the A nor the B variant.
1875 A prefix argument forces the variant to be selected
1876 even if the difference has been edited."
1880 (emerge-select-A-edit merge-begin merge-end A-begin A-end
)
1881 (if emerge-auto-advance
1882 (emerge-next-difference))))
1884 (lambda () (if emerge-auto-advance
1885 (emerge-next-difference)))))
1886 (emerge-select-version force operate-no-change operate operate
)))
1888 ;; Actually select the A variant
1889 (defun emerge-select-A-edit (merge-begin merge-end A-begin A-end
)
1890 (with-current-buffer
1892 (delete-region merge-begin merge-end
)
1893 (goto-char merge-begin
)
1894 (insert-buffer-substring emerge-A-buffer A-begin A-end
)
1895 (goto-char merge-begin
)
1896 (aset diff-vector
6 'A
)
1897 (emerge-refresh-mode-line)))
1899 (defun emerge-select-B (&optional force
)
1900 "Select the B variant of this difference.
1901 Refuses to function if this difference has been edited, i.e., if it
1902 is neither the A nor the B variant.
1903 A prefix argument forces the variant to be selected
1904 even if the difference has been edited."
1908 (emerge-select-B-edit merge-begin merge-end B-begin B-end
)
1909 (if emerge-auto-advance
1910 (emerge-next-difference))))
1912 (lambda () (if emerge-auto-advance
1913 (emerge-next-difference)))))
1914 (emerge-select-version force operate operate-no-change operate
)))
1916 ;; Actually select the B variant
1917 (defun emerge-select-B-edit (merge-begin merge-end B-begin B-end
)
1918 (with-current-buffer
1920 (delete-region merge-begin merge-end
)
1921 (goto-char merge-begin
)
1922 (insert-buffer-substring emerge-B-buffer B-begin B-end
)
1923 (goto-char merge-begin
)
1924 (aset diff-vector
6 'B
)
1925 (emerge-refresh-mode-line)))
1927 (defun emerge-default-A ()
1928 "Make the A variant the default from here down.
1929 This selects the A variant for all differences from here down in the buffer
1930 which are still defaulted, i.e., which the user has not selected and for
1931 which there is no preference."
1933 (let ((buffer-read-only nil
))
1934 (let ((selected-difference emerge-current-difference
)
1935 (n (max emerge-current-difference
0)))
1936 (while (< n emerge-number-of-differences
)
1937 (let ((diff-vector (aref emerge-difference-list n
)))
1938 (if (eq (aref diff-vector
6) 'default-B
)
1940 (emerge-unselect-and-select-difference n t
)
1942 (aset diff-vector
6 'default-A
))))
1944 (if (zerop (% n
10))
1945 (message "Setting default to A...%d" n
)))
1946 (emerge-unselect-and-select-difference selected-difference
)))
1947 (message "Default choice is now A"))
1949 (defun emerge-default-B ()
1950 "Make the B variant the default from here down.
1951 This selects the B variant for all differences from here down in the buffer
1952 which are still defaulted, i.e., which the user has not selected and for
1953 which there is no preference."
1955 (let ((buffer-read-only nil
))
1956 (let ((selected-difference emerge-current-difference
)
1957 (n (max emerge-current-difference
0)))
1958 (while (< n emerge-number-of-differences
)
1959 (let ((diff-vector (aref emerge-difference-list n
)))
1960 (if (eq (aref diff-vector
6) 'default-A
)
1962 (emerge-unselect-and-select-difference n t
)
1964 (aset diff-vector
6 'default-B
))))
1966 (if (zerop (% n
10))
1967 (message "Setting default to B...%d" n
)))
1968 (emerge-unselect-and-select-difference selected-difference
)))
1969 (message "Default choice is now B"))
1971 (defun emerge-fast-mode ()
1972 "Set fast mode, for Emerge.
1973 In this mode ordinary Emacs commands are disabled, and Emerge commands
1974 need not be prefixed with \\<emerge-fast-keymap>\\[emerge-basic-keymap]."
1976 (setq buffer-read-only t
)
1977 (use-local-map emerge-fast-keymap
)
1978 (setq emerge-mode t
)
1979 (setq emerge-fast-mode t
)
1980 (setq emerge-edit-mode nil
)
1981 (message "Fast mode set")
1982 (force-mode-line-update))
1984 (defun emerge-edit-mode ()
1985 "Set edit mode, for Emerge.
1986 In this mode ordinary Emacs commands are available, and Emerge commands
1987 must be prefixed with \\<emerge-fast-keymap>\\[emerge-basic-keymap]."
1989 (setq buffer-read-only nil
)
1990 (use-local-map emerge-edit-keymap
)
1991 (setq emerge-mode t
)
1992 (setq emerge-fast-mode nil
)
1993 (setq emerge-edit-mode t
)
1994 (message "Edit mode set")
1995 (force-mode-line-update))
1997 (defun emerge-auto-advance (arg)
1998 "Toggle Auto-Advance mode, for Emerge.
1999 This mode causes `emerge-select-A' and `emerge-select-B' to automatically
2000 advance to the next difference.
2001 With a positive argument, turn on Auto-Advance mode.
2002 With a negative argument, turn off Auto-Advance mode."
2004 (setq emerge-auto-advance
(if (null arg
)
2005 (not emerge-auto-advance
)
2006 (> (prefix-numeric-value arg
) 0)))
2007 (message (if emerge-auto-advance
2009 "Auto-advance cleared"))
2010 (force-mode-line-update))
2012 (defun emerge-skip-prefers (arg)
2013 "Toggle Skip-Prefers mode, for Emerge.
2014 This mode causes `emerge-next-difference' and `emerge-previous-difference'
2015 to automatically skip over differences for which there is a preference.
2016 With a positive argument, turn on Skip-Prefers mode.
2017 With a negative argument, turn off Skip-Prefers mode."
2019 (setq emerge-skip-prefers
(if (null arg
)
2020 (not emerge-skip-prefers
)
2021 (> (prefix-numeric-value arg
) 0)))
2022 (message (if emerge-skip-prefers
2024 "Skip-prefers cleared"))
2025 (force-mode-line-update))
2027 (defun emerge-copy-as-kill-A ()
2028 "Put the A variant of this difference in the kill ring."
2030 (emerge-validate-difference)
2032 (aref emerge-difference-list emerge-current-difference
))
2033 (A-begin (1+ (aref diff-vector
0)))
2034 (A-end (1- (aref diff-vector
1)))
2035 ;; so further kills don't append
2037 (with-current-buffer emerge-A-buffer
2038 (copy-region-as-kill A-begin A-end
))))
2040 (defun emerge-copy-as-kill-B ()
2041 "Put the B variant of this difference in the kill ring."
2043 (emerge-validate-difference)
2045 (aref emerge-difference-list emerge-current-difference
))
2046 (B-begin (1+ (aref diff-vector
2)))
2047 (B-end (1- (aref diff-vector
3)))
2048 ;; so further kills don't append
2050 (with-current-buffer emerge-B-buffer
2051 (copy-region-as-kill B-begin B-end
))))
2053 (defun emerge-insert-A (arg)
2054 "Insert the A variant of this difference at the point.
2055 Leaves point after text, mark before.
2056 With prefix argument, puts point before, mark after."
2058 (emerge-validate-difference)
2060 (aref emerge-difference-list emerge-current-difference
))
2061 (A-begin (1+ (aref diff-vector
0)))
2062 (A-end (1- (aref diff-vector
1)))
2064 (buffer-read-only nil
))
2065 (insert-buffer-substring emerge-A-buffer A-begin A-end
)
2069 (goto-char opoint
))))
2071 (defun emerge-insert-B (arg)
2072 "Insert the B variant of this difference at the point.
2073 Leaves point after text, mark before.
2074 With prefix argument, puts point before, mark after."
2076 (emerge-validate-difference)
2078 (aref emerge-difference-list emerge-current-difference
))
2079 (B-begin (1+ (aref diff-vector
2)))
2080 (B-end (1- (aref diff-vector
3)))
2082 (buffer-read-only nil
))
2083 (insert-buffer-substring emerge-B-buffer B-begin B-end
)
2087 (goto-char opoint
))))
2089 (defun emerge-mark-difference (arg)
2090 "Leaves the point before this difference and the mark after it.
2091 With prefix argument, puts mark before, point after."
2093 (emerge-validate-difference)
2095 (aref emerge-difference-list emerge-current-difference
))
2096 (merge-begin (1+ (aref diff-vector
4)))
2097 (merge-end (1- (aref diff-vector
5))))
2100 (goto-char merge-begin
)
2101 (set-mark merge-end
))
2102 (goto-char merge-end
)
2103 (set-mark merge-begin
))))
2105 (defun emerge-file-names ()
2106 "Show the names of the buffers or files being operated on by Emerge.
2107 Use C-u l to reset the windows afterward."
2109 (delete-other-windows)
2110 (let ((temp-buffer-show-function
2112 (split-window-below)
2113 (switch-to-buffer buf
)
2115 (with-output-to-temp-buffer "*Help*"
2116 (with-current-buffer emerge-A-buffer
2117 (if buffer-file-name
2119 (princ "File A is: ")
2120 (princ buffer-file-name
))
2122 (princ "Buffer A is: ")
2123 (princ (buffer-name))))
2125 (with-current-buffer emerge-B-buffer
2126 (if buffer-file-name
2128 (princ "File B is: ")
2129 (princ buffer-file-name
))
2131 (princ "Buffer B is: ")
2132 (princ (buffer-name))))
2134 (if emerge-ancestor-buffer
2135 (with-current-buffer emerge-ancestor-buffer
2136 (if buffer-file-name
2138 (princ "Ancestor file is: ")
2139 (princ buffer-file-name
))
2141 (princ "Ancestor buffer is: ")
2142 (princ (buffer-name))))
2144 (princ emerge-output-description
)
2145 (with-current-buffer standard-output
2148 (defun emerge-join-differences (arg)
2149 "Join the selected difference with the following one.
2150 With a prefix argument, join with the preceding one."
2152 (let ((n emerge-current-difference
))
2153 ;; adjust n to be first difference to join
2156 ;; n and n+1 are the differences to join
2157 ;; check that they are both differences
2158 (if (or (< n
0) (>= n
(1- emerge-number-of-differences
)))
2159 (error "Incorrect differences to join"))
2161 (emerge-unselect-difference emerge-current-difference
)
2162 ;; decrement total number of differences
2163 (setq emerge-number-of-differences
(1- emerge-number-of-differences
))
2164 ;; build new differences vector
2166 (new-differences (make-vector emerge-number-of-differences nil
)))
2167 (while (< i emerge-number-of-differences
)
2168 (aset new-differences i
2170 ((< i n
) (aref emerge-difference-list i
))
2171 ((> i n
) (aref emerge-difference-list
(1+ i
)))
2172 (t (let ((prev (aref emerge-difference-list i
))
2173 (next (aref emerge-difference-list
(1+ i
))))
2174 (vector (aref prev
0)
2180 (let ((ps (aref prev
6))
2185 ((and (or (eq ps
'B
) (eq ps
'prefer-B
))
2186 (or (eq ns
'B
) (eq ns
'prefer-B
)))
2190 (setq emerge-difference-list new-differences
))
2191 ;; set the current difference correctly
2192 (setq emerge-current-difference n
)
2193 ;; fix the mode line
2194 (emerge-refresh-mode-line)
2195 ;; reinsert the flags
2196 (emerge-select-difference emerge-current-difference
)
2199 (defun emerge-split-difference ()
2200 "Split the current difference where the points are in the three windows."
2202 (let ((n emerge-current-difference
))
2203 ;; check that this is a valid difference
2204 (emerge-validate-difference)
2205 ;; get the point values and old difference
2206 (let ((A-point (with-current-buffer emerge-A-buffer
2208 (B-point (with-current-buffer emerge-B-buffer
2210 (merge-point (point-marker))
2211 (old-diff (aref emerge-difference-list n
)))
2212 ;; check location of the points, give error if they aren't in the
2214 (if (or (< A-point
(aref old-diff
0))
2215 (> A-point
(aref old-diff
1)))
2216 (error "Point outside of difference in A buffer"))
2217 (if (or (< B-point
(aref old-diff
2))
2218 (> B-point
(aref old-diff
3)))
2219 (error "Point outside of difference in B buffer"))
2220 (if (or (< merge-point
(aref old-diff
4))
2221 (> merge-point
(aref old-diff
5)))
2222 (error "Point outside of difference in merge buffer"))
2224 (emerge-unselect-difference emerge-current-difference
)
2225 ;; increment total number of differences
2226 (setq emerge-number-of-differences
(1+ emerge-number-of-differences
))
2227 ;; build new differences vector
2229 (new-differences (make-vector emerge-number-of-differences nil
)))
2230 (while (< i emerge-number-of-differences
)
2231 (aset new-differences i
2234 (aref emerge-difference-list i
))
2236 (aref emerge-difference-list
(1- i
)))
2238 (vector (aref old-diff
0)
2246 (vector (copy-marker A-point
)
2248 (copy-marker B-point
)
2250 (copy-marker merge-point
)
2252 (aref old-diff
6)))))
2254 (setq emerge-difference-list new-differences
))
2255 ;; set the current difference correctly
2256 (setq emerge-current-difference n
)
2257 ;; fix the mode line
2258 (emerge-refresh-mode-line)
2259 ;; reinsert the flags
2260 (emerge-select-difference emerge-current-difference
)
2261 (emerge-recenter))))
2263 (defun emerge-trim-difference ()
2264 "Trim lines off top and bottom of difference that are the same.
2265 If lines are the same in both the A and the B versions, strip them off.
2266 \(This can happen when the A and B versions have common lines that the
2267 ancestor version does not share.)"
2269 ;; make sure we are in a real difference
2270 (emerge-validate-difference)
2272 (emerge-unselect-difference emerge-current-difference
)
2273 (let* ((diff (aref emerge-difference-list emerge-current-difference
))
2274 (top-a (marker-position (aref diff
0)))
2275 (bottom-a (marker-position (aref diff
1)))
2276 (top-b (marker-position (aref diff
2)))
2277 (bottom-b (marker-position (aref diff
3)))
2278 (top-m (marker-position (aref diff
4)))
2279 (bottom-m (marker-position (aref diff
5)))
2280 size success sa sb sm
)
2281 ;; move down the tops of the difference regions as much as possible
2282 ;; Try advancing comparing 1000 chars at a time.
2283 ;; When that fails, go 500 chars at a time, and so on.
2288 (setq size
(min size
(- bottom-a top-a
) (- bottom-b top-b
)
2289 (- bottom-m top-m
)))
2290 (setq sa
(with-current-buffer emerge-A-buffer
2291 (buffer-substring top-a
2293 (setq sb
(with-current-buffer emerge-B-buffer
2294 (buffer-substring top-b
2296 (setq sm
(buffer-substring top-m
(+ size top-m
)))
2297 (setq success
(and (> size
0) (equal sa sb
) (equal sb sm
)))
2299 (setq top-a
(+ top-a size
)
2300 top-b
(+ top-b size
)
2301 top-m
(+ top-m size
))))
2302 (setq size
(/ size
2)))
2303 ;; move up the bottoms of the difference regions as much as possible
2304 ;; Try advancing comparing 1000 chars at a time.
2305 ;; When that fails, go 500 chars at a time, and so on.
2310 (setq size
(min size
(- bottom-a top-a
) (- bottom-b top-b
)
2311 (- bottom-m top-m
)))
2312 (setq sa
(with-current-buffer emerge-A-buffer
2313 (buffer-substring (- bottom-a size
)
2315 (setq sb
(with-current-buffer emerge-B-buffer
2316 (buffer-substring (- bottom-b size
)
2318 (setq sm
(buffer-substring (- bottom-m size
) bottom-m
))
2319 (setq success
(and (> size
0) (equal sa sb
) (equal sb sm
)))
2321 (setq bottom-a
(- bottom-a size
)
2322 bottom-b
(- bottom-b size
)
2323 bottom-m
(- bottom-m size
))))
2324 (setq size
(/ size
2)))
2325 ;; {top,bottom}-{a,b,m} are now set at the new beginnings and ends
2326 ;; of the difference regions. Move them to the beginning of lines, as
2328 (with-current-buffer emerge-A-buffer
2331 (aset diff
0 (point-marker))
2332 (goto-char bottom-a
)
2333 (beginning-of-line 2)
2334 (aset diff
1 (point-marker)))
2335 (with-current-buffer emerge-B-buffer
2338 (aset diff
2 (point-marker))
2339 (goto-char bottom-b
)
2340 (beginning-of-line 2)
2341 (aset diff
3 (point-marker)))
2344 (aset diff
4 (point-marker))
2345 (goto-char bottom-m
)
2346 (beginning-of-line 2)
2347 (aset diff
5 (point-marker))
2348 ;; put the flags back in, recenter the display
2349 (emerge-select-difference emerge-current-difference
)
2352 ;; FIXME the manual advertised this as working in the A or B buffers,
2353 ;; but it does not, because all the buffer locals are nil there.
2354 ;; It would work to call it from the merge buffer and specify that one
2355 ;; wants to use the value of point in the A or B buffer.
2356 ;; But with the prefix argument already in use, there is no easy way
2357 ;; to have it ask for a buffer.
2358 (defun emerge-find-difference (arg)
2359 "Find the difference containing the current position of the point.
2360 If there is no containing difference and the prefix argument is positive,
2361 it finds the nearest following difference. A negative prefix argument finds
2362 the nearest previous difference."
2364 (cond ((eq (current-buffer) emerge-A-buffer
)
2365 (emerge-find-difference-A arg
))
2366 ((eq (current-buffer) emerge-B-buffer
)
2367 (emerge-find-difference-B arg
))
2368 (t (emerge-find-difference-merge arg
))))
2370 (defun emerge-find-difference-merge (arg)
2371 "Find the difference containing point, in the merge buffer.
2372 If there is no containing difference and the prefix argument is positive,
2373 it finds the nearest following difference. A negative prefix argument finds
2374 the nearest previous difference."
2376 ;; search for the point in the merge buffer, using the markers
2377 ;; for the beginning and end of the differences in the merge buffer
2378 (emerge-find-difference1 arg
(point) 4 5))
2380 (defun emerge-find-difference-A (arg)
2381 "Find the difference containing point, in the A buffer.
2382 This command must be executed in the merge buffer.
2383 If there is no containing difference and the prefix argument is positive,
2384 it finds the nearest following difference. A negative prefix argument finds
2385 the nearest previous difference."
2387 ;; search for the point in the A buffer, using the markers
2388 ;; for the beginning and end of the differences in the A buffer
2389 (emerge-find-difference1 arg
2390 (with-current-buffer emerge-A-buffer
(point))
2393 (defun emerge-find-difference-B (arg)
2394 "Find the difference containing point, in the B buffer.
2395 This command must be executed in the merge buffer.
2396 If there is no containing difference and the prefix argument is positive,
2397 it finds the nearest following difference. A negative prefix argument finds
2398 the nearest previous difference."
2400 ;; search for the point in the B buffer, using the markers
2401 ;; for the beginning and end of the differences in the B buffer
2402 (emerge-find-difference1 arg
2403 (with-current-buffer emerge-B-buffer
(point))
2406 (defun emerge-find-difference1 (arg location begin end
)
2408 ;; find first difference containing or after the current position
2411 (while (< n emerge-number-of-differences
)
2412 (let ((diff-vector (aref emerge-difference-list n
)))
2413 (if (<= location
(marker-position (aref diff-vector end
)))
2416 emerge-number-of-differences
))
2418 ;; whether the found difference contains the current position
2419 (and (< index emerge-number-of-differences
)
2420 (<= (marker-position (aref (aref emerge-difference-list index
)
2424 ;; numeric value of prefix argument
2425 (prefix-numeric-value arg
)))
2426 (emerge-unselect-and-select-difference
2428 ;; if the point is in a difference, select it
2430 ;; if the arg is nil and the point is not in a difference, error
2431 ((null arg
) (error "No difference contains point"))
2432 ;; if the arg is positive, select the following difference
2434 (if (< index emerge-number-of-differences
)
2436 (error "No difference contains or follows point")))
2437 ;; if the arg is negative, select the preceding difference
2441 (error "No difference contains or precedes point")))))))
2443 (defun emerge-line-numbers ()
2444 "Display the current line numbers.
2445 This function displays the line numbers of the points in the A, B, and
2449 (and (>= emerge-current-difference
0)
2450 (< emerge-current-difference emerge-number-of-differences
)))
2451 (emerge-line-diff (and valid-diff
2452 (aref emerge-difference-list
2453 emerge-current-difference
)))
2454 (merge-line (emerge-line-number-in-buf 4 5))
2455 (A-line (with-current-buffer emerge-A-buffer
2456 (emerge-line-number-in-buf 0 1)))
2457 (B-line (with-current-buffer emerge-B-buffer
2458 (emerge-line-number-in-buf 2 3))))
2459 (message "At lines: merge = %d, A = %d, B = %d"
2460 merge-line A-line B-line
)))
2462 (defvar emerge-line-diff
)
2464 (defun emerge-line-number-in-buf (begin-marker end-marker
)
2465 ;; FIXME point-min rather than 1? widen?
2466 (let ((temp (1+ (count-lines 1 (line-beginning-position)))))
2469 (if (> (point) (aref emerge-line-diff begin-marker
))
2470 (setq temp
(- temp emerge-before-flag-lines
)))
2471 (if (> (point) (aref emerge-line-diff end-marker
))
2472 (setq temp
(- temp emerge-after-flag-lines
)))))
2475 (defun emerge-set-combine-template (string &optional localize
)
2476 "Set `emerge-combine-versions-template' to STRING.
2477 This value controls how `emerge-combine-versions' combines the two versions.
2478 With prefix argument, `emerge-combine-versions-template' is made local to this
2479 merge buffer. Localization is permanent for any particular merge buffer."
2480 (interactive "s\nP")
2482 (make-local-variable 'emerge-combine-versions-template
))
2483 (setq emerge-combine-versions-template string
)
2485 (if (assq 'emerge-combine-versions-template
(buffer-local-variables))
2486 "emerge-set-combine-versions-template set locally"
2487 "emerge-set-combine-versions-template set")))
2489 (defun emerge-set-combine-versions-template (start end
&optional localize
)
2490 "Copy region into `emerge-combine-versions-template'.
2491 This controls how `emerge-combine-versions' will combine the two versions.
2492 With prefix argument, `emerge-combine-versions-template' is made local to this
2493 merge buffer. Localization is permanent for any particular merge buffer."
2494 (interactive "r\nP")
2496 (make-local-variable 'emerge-combine-versions-template
))
2497 (setq emerge-combine-versions-template
(buffer-substring start end
))
2499 (if (assq 'emerge-combine-versions-template
(buffer-local-variables))
2500 "emerge-set-combine-versions-template set locally."
2501 "emerge-set-combine-versions-template set.")))
2503 (defun emerge-combine-versions (&optional force
)
2504 "Combine versions using the template in `emerge-combine-versions-template'.
2505 Refuses to function if this difference has been edited, i.e., if it is
2506 neither the A nor the B variant.
2507 An argument forces the variant to be selected even if the difference has
2510 (emerge-combine-versions-internal emerge-combine-versions-template force
))
2512 (defun emerge-combine-versions-register (char &optional force
)
2513 "Combine the two versions using the template in register REG.
2514 See documentation of the variable `emerge-combine-versions-template'
2515 for how the template is interpreted.
2516 Refuses to function if this difference has been edited, i.e., if it is
2517 neither the A nor the B variant.
2518 An argument forces the variant to be selected even if the difference has
2520 (interactive "cRegister containing template: \nP")
2521 (let ((template (get-register char
)))
2522 (if (not (stringp template
))
2523 (error "Register does not contain text"))
2524 (emerge-combine-versions-internal template force
)))
2526 (defun emerge-combine-versions-internal (emerge-combine-template force
)
2529 (emerge-combine-versions-edit merge-begin merge-end
2530 A-begin A-end B-begin B-end
)
2531 (if emerge-auto-advance
2532 (emerge-next-difference)))))
2533 (emerge-select-version force operate operate operate
)))
2535 (defvar emerge-combine-template
)
2537 (defun emerge-combine-versions-edit (merge-begin merge-end
2538 A-begin A-end B-begin B-end
)
2539 (with-current-buffer
2541 (delete-region merge-begin merge-end
)
2542 (goto-char merge-begin
)
2544 (while (< i
(length emerge-combine-template
))
2545 (let ((c (aref emerge-combine-template i
)))
2551 (aref emerge-combine-template i
)
2554 (insert-buffer-substring emerge-A-buffer A-begin A-end
))
2556 (insert-buffer-substring emerge-B-buffer B-begin B-end
))
2563 (goto-char merge-begin
)
2564 (aset diff-vector
6 'combined
)
2565 (emerge-refresh-mode-line)))
2567 (defun emerge-set-merge-mode (mode)
2568 "Set the major mode in a merge buffer.
2569 Overrides any change that the mode might make to the mode line or local
2570 keymap. Leaves merge in fast mode."
2572 (list (intern (completing-read "New major mode for merge buffer: "
2573 obarray
'commandp t nil
))))
2575 (emerge-refresh-mode-line)
2576 (if emerge-fast-mode
2578 (emerge-edit-mode)))
2580 (defun emerge-one-line-window ()
2582 (let ((window-min-height 1))
2583 (shrink-window (- (window-height) 2))))
2585 ;;; Support routines
2587 ;; Select a difference by placing the visual flags around the appropriate
2588 ;; group of lines in the A, B, and merge buffers
2589 (defun emerge-select-difference (n)
2590 (let ((emerge-globalized-difference-list emerge-difference-list
)
2591 (emerge-globalized-number-of-differences emerge-number-of-differences
))
2592 (emerge-place-flags-in-buffer emerge-A-buffer n
0 1)
2593 (emerge-place-flags-in-buffer emerge-B-buffer n
2 3)
2594 (emerge-place-flags-in-buffer nil n
4 5))
2595 (run-hooks 'emerge-select-hook
))
2597 (defun emerge-place-flags-in-buffer (buffer difference before-index
2600 (with-current-buffer
2602 (emerge-place-flags-in-buffer1 difference before-index after-index
))
2603 (emerge-place-flags-in-buffer1 difference before-index after-index
)))
2605 (defun emerge-place-flags-in-buffer1 (difference before-index after-index
)
2606 (let ((buffer-read-only nil
))
2607 ;; insert the flag before the difference
2608 (let ((before (aref (aref emerge-globalized-difference-list difference
)
2612 ;; insert the flag itself
2613 (insert-before-markers emerge-before-flag
)
2615 ;; Put the marker(s) referring to this position 1 character before the
2616 ;; end of the flag, so it won't be damaged by the user.
2617 ;; This gets a bit tricky, as there could be a number of markers
2618 ;; that have to be moved.
2619 (set-marker before
(1- before
))
2620 (let ((n (1- difference
)) after-marker before-marker diff-list
)
2624 (setq diff-list
(aref emerge-globalized-difference-list n
)
2625 after-marker
(aref diff-list after-index
))
2626 (= after-marker here
)))
2627 (set-marker after-marker
(1- after-marker
))
2628 (setq before-marker
(aref diff-list before-index
))
2629 (if (= before-marker here
)
2630 (setq before-marker
(1- before-marker
)))
2632 ;; insert the flag after the difference
2633 (let* ((after (aref (aref emerge-globalized-difference-list difference
)
2635 (here (marker-position after
)))
2637 ;; insert the flag itself
2638 (insert emerge-after-flag
)
2639 ;; Put the marker(s) referring to this position 1 character after the
2640 ;; beginning of the flag, so it won't be damaged by the user.
2641 ;; This gets a bit tricky, as there could be a number of markers
2642 ;; that have to be moved.
2643 (set-marker after
(1+ after
))
2644 (let ((n (1+ difference
)) before-marker after-marker diff-list
)
2646 (< n emerge-globalized-number-of-differences
)
2648 (setq diff-list
(aref emerge-globalized-difference-list n
)
2649 before-marker
(aref diff-list before-index
))
2650 (= before-marker here
)))
2651 (set-marker before-marker
(1+ before-marker
))
2652 (setq after-marker
(aref diff-list after-index
))
2653 (if (= after-marker here
)
2654 (setq after-marker
(1+ after-marker
)))
2655 (setq n
(1+ n
)))))))
2657 ;; Unselect a difference by removing the visual flags in the buffers.
2658 (defun emerge-unselect-difference (n)
2659 (let ((diff-vector (aref emerge-difference-list n
)))
2660 (emerge-remove-flags-in-buffer emerge-A-buffer
2661 (aref diff-vector
0) (aref diff-vector
1))
2662 (emerge-remove-flags-in-buffer emerge-B-buffer
2663 (aref diff-vector
2) (aref diff-vector
3))
2664 (emerge-remove-flags-in-buffer emerge-merge-buffer
2665 (aref diff-vector
4) (aref diff-vector
5)))
2666 (run-hooks 'emerge-unselect-hook
))
2668 (defun emerge-remove-flags-in-buffer (buffer before after
)
2669 (with-current-buffer
2671 (let ((buffer-read-only nil
))
2672 ;; remove the flags, if they're there
2673 (goto-char (- before
(1- emerge-before-flag-length
)))
2674 (if (looking-at emerge-before-flag-match
)
2675 (delete-char emerge-before-flag-length
)
2676 ;; the flag isn't there
2678 (message "Trouble removing flag"))
2679 (goto-char (1- after
))
2680 (if (looking-at emerge-after-flag-match
)
2681 (delete-char emerge-after-flag-length
)
2682 ;; the flag isn't there
2684 (message "Trouble removing flag")))))
2686 ;; Select a difference, removing any flags that exist now.
2687 (defun emerge-unselect-and-select-difference (n &optional suppress-display
)
2688 (if (and (>= emerge-current-difference
0)
2689 (< emerge-current-difference emerge-number-of-differences
))
2690 (emerge-unselect-difference emerge-current-difference
))
2691 (if (and (>= n
0) (< n emerge-number-of-differences
))
2693 (emerge-select-difference n
)
2694 (let* ((diff-vector (aref emerge-difference-list n
))
2695 (selection-type (aref diff-vector
6)))
2696 (if (eq selection-type
'default-A
)
2697 (aset diff-vector
6 'A
)
2698 (if (eq selection-type
'default-B
)
2699 (aset diff-vector
6 'B
))))))
2700 (setq emerge-current-difference n
)
2701 (if (not suppress-display
)
2704 (emerge-refresh-mode-line))))
2706 ;; Perform tests to see whether user should be allowed to select a version
2707 ;; of this difference:
2708 ;; a valid difference has been selected; and
2709 ;; the difference text in the merge buffer is:
2710 ;; the A version (execute a-version), or
2711 ;; the B version (execute b-version), or
2712 ;; empty (execute neither-version), or
2713 ;; argument FORCE is true (execute neither-version)
2714 ;; Otherwise, signal an error.
2715 (defun emerge-select-version (force a-version b-version neither-version
)
2716 (emerge-validate-difference)
2717 (let ((buffer-read-only nil
))
2719 (aref emerge-difference-list emerge-current-difference
))
2720 (A-begin (1+ (aref diff-vector
0)))
2721 (A-end (1- (aref diff-vector
1)))
2722 (B-begin (1+ (aref diff-vector
2)))
2723 (B-end (1- (aref diff-vector
3)))
2724 (merge-begin (1+ (aref diff-vector
4)))
2725 (merge-end (1- (aref diff-vector
5))))
2726 (if (emerge-compare-buffers emerge-A-buffer A-begin A-end
2727 emerge-merge-buffer merge-begin
2730 (if (emerge-compare-buffers emerge-B-buffer B-begin B-end
2731 emerge-merge-buffer merge-begin
2734 (if (or force
(= merge-begin merge-end
))
2735 (funcall neither-version
)
2736 (error "This difference region has been edited")))))))
2738 ;; Read a file name, handling all of the various defaulting rules.
2740 (defun emerge-read-file-name (prompt alternative-default-dir default-file
2742 ;; `prompt' should not have trailing ": ", so that it can be modified
2743 ;; according to context.
2744 ;; If alternative-default-dir is non-nil, it should be used as the default
2745 ;; directory instead if default-directory, if emerge-default-last-directories
2747 ;; If default-file is set, it should be used as the default value.
2748 ;; If A-file is set, and its directory is different from
2749 ;; alternative-default-dir, and if emerge-default-last-directories is set,
2750 ;; the default file should be the last part of A-file in the default
2751 ;; directory. (Overriding default-file.)
2753 ;; If this is not the A-file argument (shown by non-nil A-file), and
2754 ;; if emerge-default-last-directories is set, and
2755 ;; the default directory exists but is not the same as the directory of the
2757 ;; then make the default file have the same name as the A-file, but in
2758 ;; the default directory.
2759 ((and emerge-default-last-directories
2761 alternative-default-dir
2762 (not (string-equal alternative-default-dir
2763 (file-name-directory A-file
))))
2764 (read-file-name (format "%s (default %s): "
2765 prompt
(file-name-nondirectory A-file
))
2766 alternative-default-dir
2767 (concat alternative-default-dir
2768 (file-name-nondirectory A-file
))
2769 (and must-match
'confirm
)))
2770 ;; If there is a default file, use it.
2772 (read-file-name (format "%s (default %s): " prompt default-file
)
2773 ;; If emerge-default-last-directories is set, use the
2774 ;; directory from the same argument of the last call of
2775 ;; Emerge as the default for this argument.
2776 (and emerge-default-last-directories
2777 alternative-default-dir
)
2778 default-file
(and must-match
'confirm
)))
2780 (read-file-name (concat prompt
": ")
2781 ;; If emerge-default-last-directories is set, use the
2782 ;; directory from the same argument of the last call of
2783 ;; Emerge as the default for this argument.
2784 (and emerge-default-last-directories
2785 alternative-default-dir
)
2786 nil
(and must-match
'confirm
)))))
2788 ;; Revise the mode line to display which difference we have selected
2790 (defun emerge-refresh-mode-line ()
2791 (setq mode-line-buffer-identification
2792 (list (format "Emerge: %%b diff %d of %d%s"
2793 (1+ emerge-current-difference
)
2794 emerge-number-of-differences
2795 (if (and (>= emerge-current-difference
0)
2796 (< emerge-current-difference
2797 emerge-number-of-differences
))
2798 (cdr (assq (aref (aref emerge-difference-list
2799 emerge-current-difference
)
2803 (prefer-A .
" - A*")
2804 (prefer-B .
" - B*")
2805 (combined .
" - comb"))))
2807 (force-mode-line-update))
2809 ;; compare two regions in two buffers for containing the same text
2810 (defun emerge-compare-buffers (buffer-x x-begin x-end buffer-y y-begin y-end
)
2811 ;; first check that the two regions are the same length
2812 (if (not (and (= (- x-end x-begin
) (- y-end y-begin
))))
2815 (while (< x-begin x-end
)
2816 ;; bite off and compare no more than 1000 characters at a time
2817 (let* ((compare-length (min (- x-end x-begin
) 1000))
2818 (x-string (with-current-buffer
2820 (buffer-substring x-begin
2821 (+ x-begin compare-length
))))
2822 (y-string (with-current-buffer
2824 (buffer-substring y-begin
2825 (+ y-begin compare-length
)))))
2826 (if (not (string-equal x-string y-string
))
2828 (setq x-begin
(+ x-begin compare-length
))
2829 (setq y-begin
(+ y-begin compare-length
)))))
2832 ;; Construct a unique buffer name.
2833 ;; The first one tried is prefixsuffix, then prefix<2>suffix,
2834 ;; prefix<3>suffix, etc.
2835 (defun emerge-unique-buffer-name (prefix suffix
)
2836 (if (null (get-buffer (concat prefix suffix
)))
2837 (concat prefix suffix
)
2839 (while (get-buffer (format "%s<%d>%s" prefix n suffix
))
2841 (format "%s<%d>%s" prefix n suffix
))))
2843 ;; Verify that we have a difference selected.
2844 (defun emerge-validate-difference ()
2845 (if (not (and (>= emerge-current-difference
0)
2846 (< emerge-current-difference emerge-number-of-differences
)))
2847 (error "No difference selected")))
2849 ;;; Functions for saving and restoring a batch of variables
2851 ;; These functions save (get the values of) and restore (set the values of)
2852 ;; a list of variables. The argument is a list of symbols (the names of
2853 ;; the variables). A list element can also be a list of two functions,
2854 ;; the first of which (when called with no arguments) gets the value, and
2855 ;; the second (when called with a value as an argument) sets the value.
2856 ;; A "function" is anything that funcall can handle as an argument.
2858 (defun emerge-save-variables (vars)
2859 (mapcar (lambda (v) (if (symbolp v
)
2864 (defun emerge-restore-variables (vars values
)
2866 (let ((var (car vars
))
2867 (value (car values
)))
2870 (funcall (car (cdr var
)) value
)))
2871 (setq vars
(cdr vars
))
2872 (setq values
(cdr values
))))
2874 ;; Make a temporary file that only we have access to.
2875 ;; PREFIX is appended to emerge-temp-file-prefix to make the filename prefix.
2876 (defun emerge-make-temp-file (prefix)
2877 (let (f (old-modes (default-file-modes)))
2880 (set-default-file-modes emerge-temp-file-mode
)
2881 (setq f
(make-temp-file (concat emerge-temp-file-prefix prefix
))))
2882 (set-default-file-modes old-modes
))
2885 ;;; Functions that query the user before he can write out the current buffer.
2887 (defun emerge-query-write-file ()
2888 "Ask the user whether to write out an incomplete merge.
2889 If answer is yes, call `write-file' to do so. See `emerge-query-and-call'
2890 for details of the querying process."
2892 (emerge-query-and-call 'write-file
))
2894 (defun emerge-query-save-buffer ()
2895 "Ask the user whether to save an incomplete merge.
2896 If answer is yes, call `save-buffer' to do so. See `emerge-query-and-call'
2897 for details of the querying process."
2899 (emerge-query-and-call 'save-buffer
))
2901 (defun emerge-query-and-call (command)
2902 "Ask the user whether to save or write out the incomplete merge.
2903 If answer is yes, call COMMAND interactively. During the call, the flags
2904 around the current difference are removed."
2905 (if (yes-or-no-p "Do you really write to write out this unfinished merge? ")
2906 ;; He really wants to do it -- unselect the difference for the duration
2908 (if (and (>= emerge-current-difference
0)
2909 (< emerge-current-difference emerge-number-of-differences
))
2910 (emerge-unselect-difference emerge-current-difference
))
2911 ;; call-interactively takes the value of current-prefix-arg as the
2912 ;; prefix argument value to be passed to the command. Thus, we have
2913 ;; to do nothing special to make sure the prefix argument is
2914 ;; transmitted to the command.
2915 (call-interactively command
)
2916 (if (and (>= emerge-current-difference
0)
2917 (< emerge-current-difference emerge-number-of-differences
))
2919 (emerge-select-difference emerge-current-difference
)
2920 (emerge-recenter))))
2921 ;; He's being smart and not doing it
2922 (message "Not written")))
2924 ;; Make sure the current buffer (for a file) has the same contents as the
2925 ;; file on disk, and attempt to remedy the situation if not.
2926 ;; Signal an error if we can't make them the same, or the user doesn't want
2927 ;; to do what is necessary to make them the same.
2928 (defun emerge-verify-file-buffer ()
2929 ;; First check if the file has been modified since the buffer visited it.
2930 (if (verify-visited-file-modtime (current-buffer))
2931 (if (buffer-modified-p)
2932 ;; If buffer is not obsolete and is modified, offer to save
2933 (if (yes-or-no-p (format "Save file %s? " buffer-file-name
))
2935 (error "Buffer out of sync for file %s" buffer-file-name
))
2936 ;; If buffer is not obsolete and is not modified, do nothing
2938 (if (buffer-modified-p)
2939 ;; If buffer is obsolete and is modified, give error
2940 (error "Buffer out of sync for file %s" buffer-file-name
)
2941 ;; If buffer is obsolete and is not modified, offer to revert
2942 (if (yes-or-no-p (format "Revert file %s? " buffer-file-name
))
2944 (error "Buffer out of sync for file %s" buffer-file-name
)))))
2946 ;; Utilities that might have value outside of Emerge.
2948 ;; Set up the mode in the current buffer to duplicate the mode in another
2950 (defun emerge-copy-modes (buffer)
2951 ;; Set the major mode
2952 (funcall (with-current-buffer buffer major-mode
)))
2954 ;; Define a key, even if a prefix of it is defined
2955 (defun emerge-force-define-key (keymap key definition
)
2956 "Like `define-key', but forcibly creates prefix characters as needed.
2957 If some prefix of KEY has a non-prefix definition, it is redefined."
2958 ;; Find out if a prefix of key is defined
2959 (let ((v (lookup-key keymap key
)))
2960 ;; If so, undefine it
2962 (define-key keymap
(substring key
0 v
) nil
)))
2963 ;; Now define the key
2964 (define-key keymap key definition
))
2966 ;;;;; Improvements to describe-mode, so that it describes minor modes as well
2967 ;;;;; as the major mode
2968 ;;(defun describe-mode (&optional minor)
2969 ;; "Display documentation of current major mode.
2970 ;;If optional arg MINOR is non-nil (or prefix argument is given if interactive),
2971 ;;display documentation of active minor modes as well.
2972 ;;For this to work correctly for a minor mode, the mode's indicator variable
2973 ;;\(listed in `minor-mode-alist') must also be a function whose documentation
2974 ;;describes the minor mode."
2976 ;; (with-output-to-temp-buffer "*Help*"
2977 ;; (princ mode-name)
2978 ;; (princ " Mode:\n")
2979 ;; (princ (documentation major-mode))
2980 ;; (let ((minor-modes minor-mode-alist)
2981 ;; (locals (buffer-local-variables)))
2982 ;; (while minor-modes
2983 ;; (let* ((minor-mode (car (car minor-modes)))
2984 ;; (indicator (car (cdr (car minor-modes))))
2985 ;; (local-binding (assq minor-mode locals)))
2986 ;; ;; Document a minor mode if it is listed in minor-mode-alist,
2987 ;; ;; bound locally in this buffer, non-nil, and has a function
2989 ;; (if (and local-binding
2990 ;; (cdr local-binding)
2991 ;; (fboundp minor-mode))
2993 ;; (princ (format "\n\n\n%s minor mode (indicator%s):\n"
2994 ;; minor-mode indicator))
2995 ;; (princ (documentation minor-mode)))))
2996 ;; (setq minor-modes (cdr minor-modes))))
2997 ;; (with-current-buffer standard-output
2999 ;; (help-print-return-message)))
3001 ;; This goes with the redefinition of describe-mode.
3002 ;;;; Adjust things so that keyboard macro definitions are documented correctly.
3003 ;;(fset 'defining-kbd-macro (symbol-function 'start-kbd-macro))
3005 ;; substitute-key-definition should work now.
3006 ;;;; Function to shadow a definition in a keymap with definitions in another.
3007 ;;(defun emerge-shadow-key-definition (olddef newdef keymap shadowmap)
3008 ;; "Shadow OLDDEF with NEWDEF for any keys in KEYMAP with entries in SHADOWMAP.
3009 ;;In other words, SHADOWMAP will now shadow all definitions of OLDDEF in KEYMAP
3010 ;;with NEWDEF. Does not affect keys that are already defined in SHADOWMAP,
3011 ;;including those whose definition is OLDDEF."
3012 ;; ;; loop through all keymaps accessible from keymap
3013 ;; (let ((maps (accessible-keymaps keymap)))
3015 ;; (let ((prefix (car (car maps)))
3016 ;; (map (cdr (car maps))))
3017 ;; ;; examine a keymap
3020 ;; (let ((len (length map))
3023 ;; (if (eq (aref map i) olddef)
3024 ;; ;; set the shadowing definition
3025 ;; (let ((key (concat prefix (char-to-string i))))
3026 ;; (emerge-define-key-if-possible shadowmap key newdef)))
3027 ;; (setq i (1+ i))))
3030 ;; (if (eq (cdr-safe (car-safe map)) olddef)
3031 ;; ;; set the shadowing definition
3033 ;; (concat prefix (char-to-string (car (car map))))))
3034 ;; (emerge-define-key-if-possible shadowmap key newdef)))
3035 ;; (setq map (cdr map)))))
3036 ;; (setq maps (cdr maps)))))
3038 ;; Define a key if it (or a prefix) is not already defined in the map.
3039 (defun emerge-define-key-if-possible (keymap key definition
)
3040 ;; look up the present definition of the key
3041 (let ((present (lookup-key keymap key
)))
3042 (if (integerp present
)
3043 ;; if it is "too long", look up the valid prefix
3044 (if (not (lookup-key keymap
(substring key
0 present
)))
3045 ;; if the prefix isn't defined, define it
3046 (define-key keymap key definition
))
3047 ;; if there is no present definition, define it
3049 (define-key keymap key definition
)))))
3051 ;; Ordinary substitute-key-definition should do this now.
3052 ;;(defun emerge-recursively-substitute-key-definition (olddef newdef keymap)
3053 ;; "Like `substitute-key-definition', but act recursively on subkeymaps.
3054 ;;Make sure that subordinate keymaps aren't shared with other keymaps!
3055 ;;\(`copy-keymap' will suffice.)"
3056 ;; ;; Loop through all keymaps accessible from keymap
3057 ;; (let ((maps (accessible-keymaps keymap)))
3059 ;; ;; Substitute in this keymap
3060 ;; (substitute-key-definition olddef newdef (cdr (car maps)))
3061 ;; (setq maps (cdr maps)))))
3063 ;; Show the name of the file in the buffer.
3064 (defun emerge-show-file-name ()
3065 "Displays the name of the file loaded into the current buffer.
3066 If the name won't fit on one line, the minibuffer is expanded to hold it,
3067 and the command waits for a keystroke from the user. If the keystroke is
3068 SPC, it is ignored; if it is anything else, it is processed as a command."
3070 (let ((name (buffer-file-name)))
3072 (setq name
"Buffer has no file name."))
3073 (save-window-excursion
3074 (select-window (minibuffer-window))
3079 (while (and (not (pos-visible-in-window-p))
3080 (not (window-full-height-p)))
3082 (let* ((echo-keystrokes 0)
3085 (setq unread-command-events
(list c
)))))
3088 ;; Improved auto-save file names.
3089 ;; This function fixes many problems with the standard auto-save file names:
3090 ;; Auto-save files for non-file buffers get put in the default directory
3091 ;; for the buffer, whether that makes sense or not.
3092 ;; Auto-save files for file buffers get put in the directory of the file,
3093 ;; regardless of whether we can write into it or not.
3094 ;; Auto-save files for non-file buffers don't use the process id, so if a
3095 ;; user runs more than on Emacs, they can make auto-save files that overwrite
3097 ;; To use this function, do:
3098 ;; (fset 'make-auto-save-file-name
3099 ;; (symbol-function 'emerge-make-auto-save-file-name))
3100 (defun emerge-make-auto-save-file-name ()
3101 "Return file name to use for auto-saves of current buffer.
3102 Does not consider `auto-save-visited-file-name';
3103 that is checked before calling this function.
3104 You can redefine this for customization.
3105 See also `auto-save-file-name-p'."
3106 (if buffer-file-name
3107 ;; if buffer has a file, try the format <file directory>/#<file name>#
3108 (let ((f (concat (file-name-directory buffer-file-name
)
3110 (file-name-nondirectory buffer-file-name
)
3112 (if (file-writable-p f
)
3113 ;; the file is writable, so use it
3115 ;; the file isn't writable, so use the format
3116 ;; ~/#&<file name>&<hash of directory>#
3117 (concat (getenv "HOME")
3119 (file-name-nondirectory buffer-file-name
)
3121 (emerge-hash-string-into-string
3122 (file-name-directory buffer-file-name
))
3124 ;; if buffer has no file, use the format ~/#%<buffer name>%<process id>#
3125 (expand-file-name (concat (getenv "HOME")
3127 ;; quote / into \! and \ into \\
3128 (emerge-unslashify-name (buffer-name))
3133 ;; Hash a string into five characters more-or-less suitable for use in a file
3134 ;; name. (Allowed characters are ! through ~, except /.)
3135 (defun emerge-hash-string-into-string (s)
3136 (let ((bins (vector 0 0 0 0 0))
3138 (while (< i
(length s
))
3139 (aset bins
(% i
5) (%
(+ (* (aref bins
(% i
5)) 35)
3143 (mapconcat (lambda (b)
3144 (setq b
(+ (% b
93) ?
!))
3150 ;; Quote any /s in a string by replacing them with \!.
3151 ;; Also, replace any \s by \\, to make it one-to-one.
3152 (defun emerge-unslashify-name (s)
3154 (while (string-match "[/\\]" s limit
)
3155 (setq s
(concat (substring s
0 (match-beginning 0))
3156 (if (string= (substring s
(match-beginning 0)
3161 (substring s
(match-end 0))))
3162 (setq limit
(1+ (match-end 0)))))
3165 ;; Metacharacters that have to be protected from the shell when executing
3166 ;; a diff/diff3 command.
3167 (defcustom emerge-metachars
3168 (if (memq system-type
'(ms-dos windows-nt
))
3170 "[ \t\n!\"#$&'()*;<=>?[\\^`{|~]")
3171 "Characters that must be quoted when used in a shell command line.
3172 More precisely, a [...] regexp to match any one such character."
3176 ;; Quote metacharacters (using \) when executing a diff/diff3 command.
3177 (defun emerge-protect-metachars (s)
3178 (if (memq system-type
'(ms-dos windows-nt
))
3179 (shell-quote-argument s
)
3181 (while (string-match emerge-metachars s limit
)
3182 (setq s
(concat (substring s
0 (match-beginning 0))
3184 (substring s
(match-beginning 0))))
3185 (setq limit
(1+ (match-end 0)))))
3190 ;;; emerge.el ends here