emerge.el 121 KB
Newer Older
1
;;; emerge.el --- merge diffs under Emacs control
Richard M. Stallman's avatar
Richard M. Stallman committed
2

3 4
;;; The author has placed this file in the public domain.

5 6
;; This file is part of GNU Emacs.

Richard M. Stallman's avatar
Richard M. Stallman committed
7
;; Author: Dale R. Worley <worley@world.std.com>
8 9
;; Version: 5fsf
;; Keywords: unix, tools
Richard M. Stallman's avatar
Richard M. Stallman committed
10 11 12 13 14 15 16 17 18 19 20 21 22 23

;; This software was created by Dale R. Worley and is
;; distributed free of charge.  It is placed in the public domain and
;; permission is granted to anyone to use, duplicate, modify and redistribute
;; it provided that this notice is attached.

;; Dale R. Worley provides absolutely NO WARRANTY OF ANY KIND
;; with respect to this software.  The entire risk as to the quality and
;; performance of this software is with the user.  IN NO EVENT WILL DALE
;; R. WORLEY BE LIABLE TO ANYONE FOR ANY DAMAGES ARISING OUT THE
;; USE OF THIS SOFTWARE, INCLUDING, WITHOUT LIMITATION, DAMAGES RESULTING FROM
;; LOST DATA OR LOST PROFITS, OR FOR ANY SPECIAL, INCIDENTAL OR CONSEQUENTIAL
;; DAMAGES.

24 25
;;; Commentary:

26
;;; Code:
Jim Blandy's avatar
Jim Blandy committed
27

28
;;;###autoload
29
(defvar menu-bar-emerge-menu (make-sparse-keymap "Emerge"))
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
;;;###autoload (fset 'menu-bar-emerge-menu (symbol-value 'menu-bar-emerge-menu))

;;;###autoload (define-key menu-bar-emerge-menu [emerge-merge-directories]
;;;###autoload   '("Merge Directories..." . emerge-merge-directories))
;;;###autoload (define-key menu-bar-emerge-menu [emerge-revisions-with-ancestor]
;;;###autoload   '("Revisions with Ancestor..." . emerge-revisions-with-ancestor))
;;;###autoload (define-key menu-bar-emerge-menu [emerge-revisions]
;;;###autoload   '("Revisions..." . emerge-revisions))
;;;###autoload (define-key menu-bar-emerge-menu [emerge-files-with-ancestor]
;;;###autoload   '("Files with Ancestor..." . emerge-files-with-ancestor))
;;;###autoload (define-key menu-bar-emerge-menu [emerge-files]
;;;###autoload   '("Files..." . emerge-files))
;;;###autoload (define-key menu-bar-emerge-menu [emerge-buffers-with-ancestor]
;;;###autoload   '("Buffers with Ancestor..." . emerge-buffers-with-ancestor))
;;;###autoload (define-key menu-bar-emerge-menu [emerge-buffers]
;;;###autoload   '("Buffers..." . emerge-buffers))
46

47 48 49 50 51 52
(eval-when-compile
  ;; There aren't really global variables, just dynamic bindings
  (defvar merge-begin) (defvar merge-end) (defvar template)
  (defvar A-begin) (defvar A-end) (defvar B-begin) (defvar B-end)
  (defvar diff) (defvar diff-vector) (defvar valid-diff))

Jim Blandy's avatar
Jim Blandy committed
53 54 55 56
;;; Macros

(defmacro emerge-eval-in-buffer (buffer &rest forms)
  "Macro to switch to BUFFER, evaluate FORMS, returns to original buffer.
57
Differs from `save-excursion' in that it doesn't save the point and mark."
58
  `(let ((StartBuffer (current-buffer)))
Jim Blandy's avatar
Jim Blandy committed
59
    (unwind-protect
60 61 62 63
         (progn
           (set-buffer ,buffer)
           ,@forms)
      (set-buffer StartBuffer))))
Jim Blandy's avatar
Jim Blandy committed
64

65 66
(defmacro emerge-defvar-local (var value doc)
  "Defines SYMBOL as an advertised variable.
67 68
Performs a defvar, then executes `make-variable-buffer-local' on
the variable.  Also sets the `preserved' property, so that
69
`kill-all-local-variables' (called by major-mode setting commands)
70
won't destroy Emerge control variables."
71 72 73 74
  `(progn
    (defvar ,var ,value ,doc)
    (make-variable-buffer-local ',var)
    (put ',var 'preserved t)))
Jim Blandy's avatar
Jim Blandy committed
75 76

;; Add entries to minor-mode-alist so that emerge modes show correctly
77 78 79 80 81 82
(defvar emerge-minor-modes-list
  '((emerge-mode " Emerge")
    (emerge-fast-mode " F")
    (emerge-edit-mode " E")
    (emerge-auto-advance " A")
    (emerge-skip-prefers " S")))
Jim Blandy's avatar
Jim Blandy committed
83 84 85 86 87 88
(if (not (assq 'emerge-mode minor-mode-alist))
    (setq minor-mode-alist (append emerge-minor-modes-list
				   minor-mode-alist)))

;; We need to define this function so describe-mode can describe Emerge mode.
(defun emerge-mode ()
89 90 91 92 93 94 95 96 97 98
  "Emerge mode is used by the Emerge file-merging package.
It is entered only through one of the functions:
	`emerge-files'
	`emerge-files-with-ancestor'
	`emerge-buffers'
	`emerge-buffers-with-ancestor'
	`emerge-files-command'
	`emerge-files-with-ancestor-command'
	`emerge-files-remote'
	`emerge-files-with-ancestor-remote'
Jim Blandy's avatar
Jim Blandy committed
99 100 101

Commands:
\\{emerge-basic-keymap}
102 103
Commands must be prefixed by \\<emerge-fast-keymap>\\[emerge-basic-keymap] in `edit' mode,
but can be invoked directly in `fast' mode.")
Jim Blandy's avatar
Jim Blandy committed
104

105
(defvar emerge-version "5fsf"
Jim Blandy's avatar
Jim Blandy committed
106 107 108
  "The version of Emerge.")

(defun emerge-version ()
109 110
  "Return string describing the version of Emerge.
When called interactively, displays the version."
Jim Blandy's avatar
Jim Blandy committed
111 112 113 114 115 116 117
  (interactive)
  (if (interactive-p)
      (message "Emerge version %s" (emerge-version))
    emerge-version))

;;; Emerge configuration variables

Andreas Schwab's avatar
Andreas Schwab committed
118 119 120 121
(defgroup emerge nil
  "Merge diffs under Emacs control."
  :group 'tools)

Jim Blandy's avatar
Jim Blandy committed
122 123 124 125 126 127
;; Commands that produce difference files
;; All that can be configured is the name of the programs to execute
;; (emerge-diff-program and emerge-diff3-program) and the options
;; to be provided (emerge-diff-options).  The order in which the file names
;; are given is fixed.
;; The file names are always expanded (see expand-file-name) before being
128
;; passed to diff, thus they need not be invoked under a shell that
129
;; understands `~'.
Jim Blandy's avatar
Jim Blandy committed
130 131 132
;; The code which processes the diff/diff3 output depends on all the
;; finicky details of their output, including the somewhat strange
;; way they number lines of a file.
Andreas Schwab's avatar
Andreas Schwab committed
133 134 135 136 137
(defcustom emerge-diff-program "diff"
  "*Name of the program which compares two files."
  :type 'string
  :group 'emerge)
(defcustom emerge-diff3-program "diff3"
138
  "*Name of the program which compares three files.
Andreas Schwab's avatar
Andreas Schwab committed
139 140 141 142 143 144 145 146 147 148
Its arguments are the ancestor file and the two variant files."
  :type 'string
  :group 'emerge)
(defcustom emerge-diff-options ""
  "*Options to pass to `emerge-diff-program' and `emerge-diff3-program'."
  :type 'string
  :group 'emerge)
(defcustom emerge-match-diff-line
  (let ((x "\\([0-9]+\\)\\(\\|,\\([0-9]+\\)\\)"))
    (concat "^" x "\\([acd]\\)" x "$"))
149
  "*Pattern to match lines produced by diff that describe differences.
Andreas Schwab's avatar
Andreas Schwab committed
150 151 152 153
This is as opposed to lines from the source files."
  :type 'regexp
  :group 'emerge)
(defcustom emerge-diff-ok-lines-regexp
Jim Blandy's avatar
Jim Blandy committed
154
  "^\\([0-9,]+[acd][0-9,]+$\\|[<>] \\|---\\)"
155
  "*Regexp that matches normal output lines from `emerge-diff-program'.
Andreas Schwab's avatar
Andreas Schwab committed
156 157 158 159
Lines that do not match are assumed to be error messages."
  :type 'regexp
  :group 'emerge)
(defcustom emerge-diff3-ok-lines-regexp
Jim Blandy's avatar
Jim Blandy committed
160
  "^\\([1-3]:\\|====\\|  \\)"
161
  "*Regexp that matches normal output lines from `emerge-diff3-program'.
Andreas Schwab's avatar
Andreas Schwab committed
162 163 164 165 166 167 168 169 170 171 172 173 174 175
Lines that do not match are assumed to be error messages."
  :type 'regexp
  :group 'emerge)

(defcustom emerge-rcs-ci-program "ci"
  "*Name of the program that checks in RCS revisions."
  :type 'string
  :group 'emerge)
(defcustom emerge-rcs-co-program "co"
  "*Name of the program that checks out RCS revisions."
  :type 'string
  :group 'emerge)

(defcustom emerge-process-local-variables nil
176 177
  "*Non-nil if Emerge should process local-variables lists in merge buffers.
\(You can explicitly request processing the local-variables
Andreas Schwab's avatar
Andreas Schwab committed
178 179 180 181
by executing `(hack-local-variables)'.)"
  :type 'boolean
  :group 'emerge)
(defcustom emerge-execute-line-deletions nil
182 183 184
  "*If non-nil: `emerge-execute-line' makes no output if an input was deleted.
It concludes that an input version has been deleted when an ancestor entry
is present, only one A or B entry is present, and an output entry is present.
Richard M. Stallman's avatar
Richard M. Stallman committed
185
If nil: In such circumstances, the A or B file that is present will be
Andreas Schwab's avatar
Andreas Schwab committed
186 187 188
copied to the designated output file."
  :type 'boolean
  :group 'emerge)
Richard M. Stallman's avatar
Richard M. Stallman committed
189

Andreas Schwab's avatar
Andreas Schwab committed
190
(defcustom emerge-before-flag "vvvvvvvvvvvvvvvvvvvv\n"
191 192
  "*Flag placed above the highlighted block of code.  Must end with newline.
Must be set before Emerge is loaded, or  emerge-new-flags  must be run
Andreas Schwab's avatar
Andreas Schwab committed
193 194 195 196
after setting."
  :type 'string
  :group 'emerge)
(defcustom emerge-after-flag "^^^^^^^^^^^^^^^^^^^^\n"
197 198
  "*Flag placed below the highlighted block of code.  Must end with newline.
Must be set before Emerge is loaded, or  emerge-new-flags  must be run
Andreas Schwab's avatar
Andreas Schwab committed
199 200 201
after setting."
  :type 'string
  :group 'emerge)
202

Richard M. Stallman's avatar
Richard M. Stallman committed
203 204
;; Hook variables

Andreas Schwab's avatar
Andreas Schwab committed
205 206 207 208 209
(defcustom emerge-startup-hook nil
  "*Hook to run in the merge buffer after the merge has been set up."
  :type 'hook
  :group 'emerge)
(defcustom emerge-select-hook nil
210
  "*Hook to run after a difference has been selected.
Andreas Schwab's avatar
Andreas Schwab committed
211 212 213 214
The variable `n' holds the (internal) number of the difference."
  :type 'hook
  :group 'emerge)
(defcustom emerge-unselect-hook nil
215
  "*Hook to run after a difference has been unselected.
Andreas Schwab's avatar
Andreas Schwab committed
216 217 218
The variable `n' holds the (internal) number of the difference."
  :type 'hook
  :group 'emerge)
Richard M. Stallman's avatar
Richard M. Stallman committed
219 220 221 222

;; Variables to control the default directories of the arguments to
;; Emerge commands.

Andreas Schwab's avatar
Andreas Schwab committed
223
(defcustom emerge-default-last-directories nil
224
  "*If nil, default dir for filenames in emerge is `default-directory'.
Richard M. Stallman's avatar
Richard M. Stallman committed
225
If non-nil, filenames complete in the directory of the last argument of the
Andreas Schwab's avatar
Andreas Schwab committed
226 227 228
same type to an `emerge-files...' command."
  :type 'boolean
  :group 'emerge)
Richard M. Stallman's avatar
Richard M. Stallman committed
229 230

(defvar emerge-last-dir-A nil
231
  "Last directory for the first file of an `emerge-files...' command.")
Richard M. Stallman's avatar
Richard M. Stallman committed
232
(defvar emerge-last-dir-B nil
233
  "Last directory for the second file of an `emerge-files...' command.")
Richard M. Stallman's avatar
Richard M. Stallman committed
234
(defvar emerge-last-dir-ancestor nil
235
  "Last directory for the ancestor file of an `emerge-files...' command.")
Richard M. Stallman's avatar
Richard M. Stallman committed
236
(defvar emerge-last-dir-output nil
237
  "Last directory for the output file of an `emerge-files...' command.")
Richard M. Stallman's avatar
Richard M. Stallman committed
238
(defvar emerge-last-revision-A nil
239
  "Last RCS revision used for first file of an `emerge-revisions...' command.")
Richard M. Stallman's avatar
Richard M. Stallman committed
240
(defvar emerge-last-revision-B nil
241
  "Last RCS revision used for second file of an `emerge-revisions...' command.")
Richard M. Stallman's avatar
Richard M. Stallman committed
242
(defvar emerge-last-revision-ancestor nil
243
  "Last RCS revision used for ancestor file of an `emerge-revisions...' command.")
Richard M. Stallman's avatar
Richard M. Stallman committed
244

245 246 247 248 249 250 251 252 253 254 255 256 257 258
(defvar emerge-before-flag-length)
(defvar emerge-before-flag-lines)
(defvar emerge-before-flag-match)
(defvar emerge-after-flag-length)
(defvar emerge-after-flag-lines)
(defvar emerge-after-flag-match)
(defvar emerge-diff-buffer)
(defvar emerge-diff-error-buffer)
(defvar emerge-prefix-argument)
(defvar emerge-file-out)
(defvar emerge-exit-func)
(defvar emerge-globalized-difference-list)
(defvar emerge-globalized-number-of-differences)

Jim Blandy's avatar
Jim Blandy committed
259 260 261 262 263
;; The flags used to mark differences in the buffers.

;; These function definitions need to be up here, because they are used
;; during loading.
(defun emerge-new-flags ()
264 265 266
  "Function to be called after `emerge-{before,after}-flag'.
This is called after these functions are changed to compute values that
depend on the flags."
Jim Blandy's avatar
Jim Blandy committed
267 268
  (setq emerge-before-flag-length (length emerge-before-flag))
  (setq emerge-before-flag-lines
269
	(emerge-count-matches-string emerge-before-flag "\n"))
Jim Blandy's avatar
Jim Blandy committed
270 271 272
  (setq emerge-before-flag-match (regexp-quote emerge-before-flag))
  (setq emerge-after-flag-length (length emerge-after-flag))
  (setq emerge-after-flag-lines
273
	(emerge-count-matches-string emerge-after-flag "\n"))
Jim Blandy's avatar
Jim Blandy committed
274
  (setq emerge-after-flag-match (regexp-quote emerge-after-flag)))
275 276

(defun emerge-count-matches-string (string regexp)
Jim Blandy's avatar
Jim Blandy committed
277 278 279 280 281 282 283 284 285 286 287
  "Return the number of matches in STRING for REGEXP."
  (let ((i 0)
	(count 0))
    (while (string-match regexp string i)
      (setq count (1+ count))
      (setq i (match-end 0)))
    count))

;; Calculate dependent variables
(emerge-new-flags)

Andreas Schwab's avatar
Andreas Schwab committed
288
(defcustom emerge-min-visible-lines 3
Jim Blandy's avatar
Jim Blandy committed
289
  "*Number of lines that we want to show above and below the flags when we are
Andreas Schwab's avatar
Andreas Schwab committed
290 291 292
displaying a difference."
  :type 'integer
  :group 'emerge)
Jim Blandy's avatar
Jim Blandy committed
293

Andreas Schwab's avatar
Andreas Schwab committed
294
(defcustom emerge-temp-file-prefix
295
  (expand-file-name "emerge" temporary-file-directory)
Jim Blandy's avatar
Jim Blandy committed
296
  "*Prefix to put on Emerge temporary file names.
297
Do not start with `~/' or `~USERNAME/'."
Andreas Schwab's avatar
Andreas Schwab committed
298 299
  :type 'string
  :group 'emerge)
Jim Blandy's avatar
Jim Blandy committed
300

Andreas Schwab's avatar
Andreas Schwab committed
301 302 303 304
(defcustom emerge-temp-file-mode 384	; u=rw only
  "*Mode for Emerge temporary files."
  :type 'integer
  :group 'emerge)
Jim Blandy's avatar
Jim Blandy committed
305

Andreas Schwab's avatar
Andreas Schwab committed
306
(defcustom emerge-combine-versions-template
307
  "#ifdef NEW\n%b#else /* not NEW */\n%a#endif /* not NEW */\n"
308
  "*Template for `emerge-combine-versions' to combine the two versions.
Jim Blandy's avatar
Jim Blandy committed
309 310 311
The template is inserted as a string, with the following interpolations:
	%a	the A version of the difference
	%b	the B version of the difference
312
	%%	the character `%'
Jim Blandy's avatar
Jim Blandy committed
313 314
Don't forget to end the template with a newline.
Note that this variable can be made local to a particular merge buffer by
Andreas Schwab's avatar
Andreas Schwab committed
315 316 317
giving a prefix argument to `emerge-set-combine-versions-template'."
  :type 'string
  :group 'emerge)
Jim Blandy's avatar
Jim Blandy committed
318 319 320 321 322

;; Build keymaps

(defvar emerge-basic-keymap nil
  "Keymap of Emerge commands.
323 324
Directly available in `fast' mode;
must be prefixed by \\<emerge-fast-keymap>\\[emerge-basic-keymap] in `edit' mode.")
Jim Blandy's avatar
Jim Blandy committed
325 326

(defvar emerge-fast-keymap nil
327
  "Local keymap used in Emerge `fast' mode.
Jim Blandy's avatar
Jim Blandy committed
328 329
Makes Emerge commands directly available.")

330 331 332 333 334 335 336 337 338
(defvar emerge-options-menu
  (make-sparse-keymap "Options"))

(defvar emerge-merge-menu
  (make-sparse-keymap "Merge"))

(defvar emerge-move-menu
  (make-sparse-keymap "Move"))

Andreas Schwab's avatar
Andreas Schwab committed
339
(defcustom emerge-command-prefix "\C-c\C-c"
340
  "*Command prefix for Emerge commands in `edit' mode.
Andreas Schwab's avatar
Andreas Schwab committed
341 342 343
Must be set before Emerge is loaded."
  :type 'string
  :group 'emerge)
Jim Blandy's avatar
Jim Blandy committed
344 345 346 347 348 349 350 351 352 353 354 355 356

;; This function sets up the fixed keymaps.  It is executed when the first
;; Emerge is done to allow the user maximum time to set up the global keymap.
(defun emerge-setup-fixed-keymaps ()
  ;; Set up the basic keymap
  (setq emerge-basic-keymap (make-keymap))
  (suppress-keymap emerge-basic-keymap)	; this sets 0..9 to digit-argument and
					; - to negative-argument
  (define-key emerge-basic-keymap "p" 'emerge-previous-difference)
  (define-key emerge-basic-keymap "n" 'emerge-next-difference)
  (define-key emerge-basic-keymap "a" 'emerge-select-A)
  (define-key emerge-basic-keymap "b" 'emerge-select-B)
  (define-key emerge-basic-keymap "j" 'emerge-jump-to-difference)
357
  (define-key emerge-basic-keymap "." 'emerge-find-difference)
Jim Blandy's avatar
Jim Blandy committed
358
  (define-key emerge-basic-keymap "q" 'emerge-quit)
359
  (define-key emerge-basic-keymap "\C-]" 'emerge-abort)
Jim Blandy's avatar
Jim Blandy committed
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
  (define-key emerge-basic-keymap "f" 'emerge-fast-mode)
  (define-key emerge-basic-keymap "e" 'emerge-edit-mode)
  (define-key emerge-basic-keymap "s" nil)
  (define-key emerge-basic-keymap "sa" 'emerge-auto-advance)
  (define-key emerge-basic-keymap "ss" 'emerge-skip-prefers)
  (define-key emerge-basic-keymap "l" 'emerge-recenter)
  (define-key emerge-basic-keymap "d" nil)
  (define-key emerge-basic-keymap "da" 'emerge-default-A)
  (define-key emerge-basic-keymap "db" 'emerge-default-B)
  (define-key emerge-basic-keymap "c" nil)
  (define-key emerge-basic-keymap "ca" 'emerge-copy-as-kill-A)
  (define-key emerge-basic-keymap "cb" 'emerge-copy-as-kill-B)
  (define-key emerge-basic-keymap "i" nil)
  (define-key emerge-basic-keymap "ia" 'emerge-insert-A)
  (define-key emerge-basic-keymap "ib" 'emerge-insert-B)
  (define-key emerge-basic-keymap "m" 'emerge-mark-difference)
  (define-key emerge-basic-keymap "v" 'emerge-scroll-up)
  (define-key emerge-basic-keymap "^" 'emerge-scroll-down)
  (define-key emerge-basic-keymap "<" 'emerge-scroll-left)
  (define-key emerge-basic-keymap ">" 'emerge-scroll-right)
  (define-key emerge-basic-keymap "|" 'emerge-scroll-reset)
  (define-key emerge-basic-keymap "x" nil)
  (define-key emerge-basic-keymap "x1" 'emerge-one-line-window)
  (define-key emerge-basic-keymap "xc" 'emerge-combine-versions)
  (define-key emerge-basic-keymap "xC" 'emerge-combine-versions-register)
  (define-key emerge-basic-keymap "xf" 'emerge-file-names)
  (define-key emerge-basic-keymap "xj" 'emerge-join-differences)
  (define-key emerge-basic-keymap "xl" 'emerge-line-numbers)
  (define-key emerge-basic-keymap "xm" 'emerge-set-merge-mode)
  (define-key emerge-basic-keymap "xs" 'emerge-split-difference)
  (define-key emerge-basic-keymap "xt" 'emerge-trim-difference)
  (define-key emerge-basic-keymap "xx" 'emerge-set-combine-versions-template)
  ;; Allow emerge-basic-keymap to be referenced indirectly
  (fset 'emerge-basic-keymap emerge-basic-keymap)
  ;; Set up the fast mode keymap
  (setq emerge-fast-keymap (copy-keymap emerge-basic-keymap))
  ;; Allow prefixed commands to work in fast mode
  (define-key emerge-fast-keymap emerge-command-prefix 'emerge-basic-keymap)
  ;; Allow emerge-fast-keymap to be referenced indirectly
  (fset 'emerge-fast-keymap emerge-fast-keymap)
  ;; Suppress write-file and save-buffer
401 402
  (define-key emerge-fast-keymap [remap write-file] 'emerge-query-write-file)
  (define-key emerge-fast-keymap [remap save-buffer] 'emerge-query-save-buffer)
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478

  (define-key emerge-basic-keymap [menu-bar] (make-sparse-keymap))

  (define-key emerge-fast-keymap [menu-bar options]
    (cons "Options" emerge-options-menu))
  (define-key emerge-fast-keymap [menu-bar merge]
    (cons "Merge" emerge-merge-menu))
  (define-key emerge-fast-keymap [menu-bar move]
    (cons "Move" emerge-move-menu))

  (define-key emerge-move-menu [emerge-scroll-reset]
    '("Scroll Reset" . emerge-scroll-reset))
  (define-key emerge-move-menu [emerge-scroll-right]
    '("Scroll Right" . emerge-scroll-right))
  (define-key emerge-move-menu [emerge-scroll-left]
    '("Scroll Left" . emerge-scroll-left))
  (define-key emerge-move-menu [emerge-scroll-down]
    '("Scroll Down" . emerge-scroll-down))
  (define-key emerge-move-menu [emerge-scroll-up]
    '("Scroll Up" . emerge-scroll-up))
  (define-key emerge-move-menu [emerge-recenter]
    '("Recenter" . emerge-recenter))
  (define-key emerge-move-menu [emerge-mark-difference]
    '("Mark Difference" . emerge-mark-difference))
  (define-key emerge-move-menu [emerge-jump-to-difference]
    '("Jump To Difference" . emerge-jump-to-difference))
  (define-key emerge-move-menu [emerge-find-difference]
    '("Find Difference" . emerge-find-difference))
  (define-key emerge-move-menu [emerge-previous-difference]
    '("Previous Difference" . emerge-previous-difference))
  (define-key emerge-move-menu [emerge-next-difference]
    '("Next Difference" . emerge-next-difference))


  (define-key emerge-options-menu [emerge-one-line-window]
    '("One Line Window" . emerge-one-line-window))
  (define-key emerge-options-menu [emerge-set-merge-mode]
    '("Set Merge Mode" . emerge-set-merge-mode))
  (define-key emerge-options-menu [emerge-set-combine-template]
    '("Set Combine Template..." . emerge-set-combine-template))
  (define-key emerge-options-menu [emerge-default-B]
    '("Default B" . emerge-default-B))
  (define-key emerge-options-menu [emerge-default-A]
    '("Default A" . emerge-default-A))
  (define-key emerge-options-menu [emerge-skip-prefers]
    '("Skip Prefers" . emerge-skip-prefers))
  (define-key emerge-options-menu [emerge-auto-advance]
    '("Auto Advance" . emerge-auto-advance))
  (define-key emerge-options-menu [emerge-edit-mode]
    '("Edit Mode" . emerge-edit-mode))
  (define-key emerge-options-menu [emerge-fast-mode]
    '("Fast Mode" . emerge-fast-mode))

  (define-key emerge-merge-menu [emerge-abort] '("Abort" . emerge-abort))
  (define-key emerge-merge-menu [emerge-quit] '("Quit" . emerge-quit))
  (define-key emerge-merge-menu [emerge-split-difference]
    '("Split Difference" . emerge-split-difference))
  (define-key emerge-merge-menu [emerge-join-differences]
    '("Join Differences" . emerge-join-differences))
  (define-key emerge-merge-menu [emerge-trim-difference]
    '("Trim Difference" . emerge-trim-difference))
  (define-key emerge-merge-menu [emerge-combine-versions]
    '("Combine Versions" . emerge-combine-versions))
  (define-key emerge-merge-menu [emerge-copy-as-kill-B]
    '("Copy B as Kill" . emerge-copy-as-kill-B))
  (define-key emerge-merge-menu [emerge-copy-as-kill-A]
    '("Copy A as Kill" . emerge-copy-as-kill-A))
  (define-key emerge-merge-menu [emerge-insert-B]
    '("Insert B" . emerge-insert-B))
  (define-key emerge-merge-menu [emerge-insert-A]
    '("Insert A" . emerge-insert-A))
  (define-key emerge-merge-menu [emerge-select-B]
    '("Select B" . emerge-select-B))
  (define-key emerge-merge-menu [emerge-select-A]
    '("Select A" . emerge-select-A)))

Jim Blandy's avatar
Jim Blandy committed
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534

;; Variables which control each merge.  They are local to the merge buffer.

;; Mode variables
(emerge-defvar-local emerge-mode nil
  "Indicator for emerge-mode.")
(emerge-defvar-local emerge-fast-mode nil
  "Indicator for emerge-mode fast submode.")
(emerge-defvar-local emerge-edit-mode nil
  "Indicator for emerge-mode edit submode.")
(emerge-defvar-local emerge-A-buffer nil
  "The buffer in which the A variant is stored.")
(emerge-defvar-local emerge-B-buffer nil
  "The buffer in which the B variant is stored.")
(emerge-defvar-local emerge-merge-buffer nil
  "The buffer in which the merged file is manipulated.")
(emerge-defvar-local emerge-ancestor-buffer nil
  "The buffer in which the ancestor variant is stored,
or nil if there is none.")

(defconst emerge-saved-variables
  '((buffer-modified-p set-buffer-modified-p)
    buffer-read-only
    buffer-auto-save-file-name)
  "Variables and properties of a buffer which are saved, modified and restored
during a merge.")
(defconst emerge-merging-values '(nil t nil)
  "Values to be assigned to emerge-saved-variables during a merge.")

(emerge-defvar-local emerge-A-buffer-values nil
  "Remembers emerge-saved-variables for emerge-A-buffer.")
(emerge-defvar-local emerge-B-buffer-values nil
  "Remembers emerge-saved-variables for emerge-B-buffer.")

(emerge-defvar-local emerge-difference-list nil
  "Vector of differences between the variants, and markers in the buffers to
show where they are.  Each difference is represented by a vector of seven
elements.  The first two are markers to the beginning and end of the difference
section in the A buffer, the second two are markers for the B buffer, the third
two are markers for the merge buffer, and the last element is the \"state\" of
that difference in the merge buffer.
  A section of a buffer is described by two markers, one to the beginning of
the first line of the section, and one to the beginning of the first line
after the section.  (If the section is empty, both markers point to the same
point.)  If the section is part of the selected difference, then the markers
are moved into the flags, so the user can edit the section without disturbing
the markers.
  The \"states\" are:
	A		the merge buffer currently contains the A variant
	B		the merge buffer currently contains the B variant
	default-A	the merge buffer contains the A variant by default,
			but this difference hasn't been selected yet, so
			change-default commands can alter it
	default-B	the merge buffer contains the B variant by default,
			but this difference hasn't been selected yet, so
			change-default commands can alter it
535
	prefer-A	in a three-file merge, the A variant is the preferred
Jim Blandy's avatar
Jim Blandy committed
536
			choice
537
	prefer-B	in a three-file merge, the B variant is the preferred
Jim Blandy's avatar
Jim Blandy committed
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554
			choice")
(emerge-defvar-local emerge-current-difference -1
  "The difference that is currently selected.")
(emerge-defvar-local emerge-number-of-differences nil
  "Number of differences found.")
(emerge-defvar-local emerge-edit-keymap nil
  "The local keymap for the merge buffer, with the emerge commands defined in
it.  Used to save the local keymap during fast mode, when the local keymap is
replaced by emerge-fast-keymap.")
(emerge-defvar-local emerge-old-keymap nil
  "The original local keymap for the merge buffer.")
(emerge-defvar-local emerge-auto-advance nil
  "*If non-nil, emerge-select-A and emerge-select-B automatically advance to
the next difference.")
(emerge-defvar-local emerge-skip-prefers nil
  "*If non-nil, differences for which there is a preference are automatically
skipped.")
555
(emerge-defvar-local emerge-quit-hook nil
Jim Blandy's avatar
Jim Blandy committed
556
  "Hooks to run in the merge buffer after the merge has been finished.
557
`emerge-prefix-argument' will hold the prefix argument of the `emerge-quit'
Jim Blandy's avatar
Jim Blandy committed
558
command.
559
This is *not* a user option, since Emerge uses it for its own processing.")
Jim Blandy's avatar
Jim Blandy committed
560
(emerge-defvar-local emerge-output-description nil
561
  "Describes output destination of emerge, for `emerge-file-names'.")
Jim Blandy's avatar
Jim Blandy committed
562 563 564 565

;;; Setup functions for two-file mode.

(defun emerge-files-internal (file-A file-B &optional startup-hooks quit-hooks
566
                              output-file)
Richard M. Stallman's avatar
Richard M. Stallman committed
567
  (if (not (file-readable-p file-A))
568
      (error "File `%s' does not exist or is not readable" file-A))
Richard M. Stallman's avatar
Richard M. Stallman committed
569
  (if (not (file-readable-p file-B))
570
      (error "File `%s' does not exist or is not readable" file-B))
Jim Blandy's avatar
Jim Blandy committed
571 572
  (let ((buffer-A (find-file-noselect file-A))
	(buffer-B (find-file-noselect file-B)))
Richard M. Stallman's avatar
Richard M. Stallman committed
573 574 575 576 577
    ;; Record the directories of the files
    (setq emerge-last-dir-A (file-name-directory file-A))
    (setq emerge-last-dir-B (file-name-directory file-B))
    (if output-file
	(setq emerge-last-dir-output (file-name-directory output-file)))
Jim Blandy's avatar
Jim Blandy committed
578
    ;; Make sure the entire files are seen, and they reflect what is on disk
579
    (emerge-eval-in-buffer
Richard M. Stallman's avatar
Richard M. Stallman committed
580 581
     buffer-A
     (widen)
582 583 584 585
     (let ((temp (file-local-copy file-A)))
       (if temp
	   (setq file-A temp
		 startup-hooks
586
		 (cons `(lambda () (delete-file ,file-A))
587
		       startup-hooks))
588 589
           ;; Verify that the file matches the buffer
           (emerge-verify-file-buffer))))
Richard M. Stallman's avatar
Richard M. Stallman committed
590 591 592
    (emerge-eval-in-buffer
     buffer-B
     (widen)
593 594 595 596
     (let ((temp (file-local-copy file-B)))
       (if temp
	   (setq file-B temp
		 startup-hooks
597
		 (cons `(lambda () (delete-file ,file-B))
598
		       startup-hooks))
599 600
           ;; Verify that the file matches the buffer
           (emerge-verify-file-buffer))))
Jim Blandy's avatar
Jim Blandy committed
601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634
    (emerge-setup buffer-A file-A buffer-B file-B startup-hooks quit-hooks
		  output-file)))

;; Start up Emerge on two files
(defun emerge-setup (buffer-A file-A buffer-B file-B startup-hooks quit-hooks
			      output-file)
  (setq file-A (expand-file-name file-A))
  (setq file-B (expand-file-name file-B))
  (setq output-file (and output-file (expand-file-name output-file)))
  (let* ((merge-buffer-name (emerge-unique-buffer-name "*merge" "*"))
	 ;; create the merge buffer from buffer A, so it inherits buffer A's
	 ;; default directory, etc.
	 (merge-buffer (emerge-eval-in-buffer
			buffer-A
			(get-buffer-create merge-buffer-name))))
    (emerge-eval-in-buffer
     merge-buffer
     (emerge-copy-modes buffer-A)
     (setq buffer-read-only nil)
     (auto-save-mode 1)
     (setq emerge-mode t)
     (setq emerge-A-buffer buffer-A)
     (setq emerge-B-buffer buffer-B)
     (setq emerge-ancestor-buffer nil)
     (setq emerge-merge-buffer merge-buffer)
     (setq emerge-output-description
	   (if output-file
	       (concat "Output to file: " output-file)
	     (concat "Output to buffer: " (buffer-name merge-buffer))))
     (insert-buffer emerge-A-buffer)
     (emerge-set-keys)
     (setq emerge-difference-list (emerge-make-diff-list file-A file-B))
     (setq emerge-number-of-differences (length emerge-difference-list))
     (setq emerge-current-difference -1)
635
     (setq emerge-quit-hook quit-hooks)
Richard M. Stallman's avatar
Richard M. Stallman committed
636 637
     (emerge-remember-buffer-characteristics)
     (emerge-handle-local-variables))
Jim Blandy's avatar
Jim Blandy committed
638 639
    (emerge-setup-windows buffer-A buffer-B merge-buffer t)
    (emerge-eval-in-buffer merge-buffer
640
			   (run-hooks 'startup-hooks 'emerge-startup-hook)
Jim Blandy's avatar
Jim Blandy committed
641 642 643 644 645 646 647 648 649 650
			   (setq buffer-read-only t))))

;; Generate the Emerge difference list between two files
(defun emerge-make-diff-list (file-A file-B)
  (setq emerge-diff-buffer (get-buffer-create "*emerge-diff*"))
  (emerge-eval-in-buffer
   emerge-diff-buffer
   (erase-buffer)
   (shell-command
    (format "%s %s %s %s"
Richard M. Stallman's avatar
Richard M. Stallman committed
651 652 653
	    emerge-diff-program emerge-diff-options
	    (emerge-protect-metachars file-A)
	    (emerge-protect-metachars file-B))
Jim Blandy's avatar
Jim Blandy committed
654
    t))
655
  (emerge-prepare-error-list emerge-diff-ok-lines-regexp)
Jim Blandy's avatar
Jim Blandy committed
656 657 658 659 660 661 662 663 664 665
  (emerge-convert-diffs-to-markers
   emerge-A-buffer emerge-B-buffer emerge-merge-buffer
   (emerge-extract-diffs emerge-diff-buffer)))

(defun emerge-extract-diffs (diff-buffer)
  (let (list)
    (emerge-eval-in-buffer
     diff-buffer
     (goto-char (point-min))
     (while (re-search-forward emerge-match-diff-line nil t)
666 667
       (let* ((a-begin (string-to-number (buffer-substring (match-beginning 1)
                                                           (match-end 1))))
Jim Blandy's avatar
Jim Blandy committed
668 669 670
	      (a-end  (let ((b (match-beginning 3))
			    (e (match-end 3)))
			(if b
671
			    (string-to-number (buffer-substring b e))
Jim Blandy's avatar
Jim Blandy committed
672 673
			  a-begin)))
	      (diff-type (buffer-substring (match-beginning 4) (match-end 4)))
674 675
	      (b-begin (string-to-number (buffer-substring (match-beginning 5)
                                                           (match-end 5))))
Jim Blandy's avatar
Jim Blandy committed
676 677 678
	      (b-end (let ((b (match-beginning 7))
			   (e (match-end 7)))
		       (if b
679
			   (string-to-number (buffer-substring b e))
Jim Blandy's avatar
Jim Blandy committed
680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714
			 b-begin))))
	 ;; fix the beginning and end numbers, because diff is somewhat
	 ;; strange about how it numbers lines
	 (if (string-equal diff-type "a")
	     (progn
	       (setq b-end (1+ b-end))
	       (setq a-begin (1+ a-begin))
	       (setq a-end a-begin))
	   (if (string-equal diff-type "d")
	       (progn
		 (setq a-end (1+ a-end))
		 (setq b-begin (1+ b-begin))
		 (setq b-end b-begin))
	     ;; (string-equal diff-type "c")
	     (progn
	       (setq a-end (1+ a-end))
	       (setq b-end (1+ b-end)))))
	 (setq list (cons (vector a-begin a-end
				  b-begin b-end
				  'default-A)
			  list)))))
    (nreverse list)))

;; Set up buffer of diff/diff3 error messages.
(defun emerge-prepare-error-list (ok-regexp)
  (setq emerge-diff-error-buffer (get-buffer-create "*emerge-diff-errors*"))
  (emerge-eval-in-buffer
   emerge-diff-error-buffer
   (erase-buffer)
   (insert-buffer emerge-diff-buffer)
   (delete-matching-lines ok-regexp)))

;;; Top-level and setup functions for three-file mode.

(defun emerge-files-with-ancestor-internal (file-A file-B file-ancestor
715 716
					    &optional startup-hooks quit-hooks
					    output-file)
Richard M. Stallman's avatar
Richard M. Stallman committed
717
  (if (not (file-readable-p file-A))
718
      (error "File `%s' does not exist or is not readable" file-A))
Richard M. Stallman's avatar
Richard M. Stallman committed
719
  (if (not (file-readable-p file-B))
720
      (error "File `%s' does not exist or is not readable" file-B))
Richard M. Stallman's avatar
Richard M. Stallman committed
721
  (if (not (file-readable-p file-ancestor))
722
      (error "File `%s' does not exist or is not readable" file-ancestor))
Jim Blandy's avatar
Jim Blandy committed
723 724 725
  (let ((buffer-A (find-file-noselect file-A))
	(buffer-B (find-file-noselect file-B))
	(buffer-ancestor (find-file-noselect file-ancestor)))
Richard M. Stallman's avatar
Richard M. Stallman committed
726 727 728 729 730 731
    ;; Record the directories of the files
    (setq emerge-last-dir-A (file-name-directory file-A))
    (setq emerge-last-dir-B (file-name-directory file-B))
    (setq emerge-last-dir-ancestor (file-name-directory file-ancestor))
    (if output-file
	(setq emerge-last-dir-output (file-name-directory output-file)))
Jim Blandy's avatar
Jim Blandy committed
732
    ;; Make sure the entire files are seen, and they reflect what is on disk
Richard M. Stallman's avatar
Richard M. Stallman committed
733 734 735
    (emerge-eval-in-buffer
     buffer-A
     (widen)
736 737 738 739
     (let ((temp (file-local-copy file-A)))
       (if temp
	   (setq file-A temp
		 startup-hooks
740
		 (cons `(lambda () (delete-file ,file-A))
741
		       startup-hooks))
742 743
           ;; Verify that the file matches the buffer
           (emerge-verify-file-buffer))))
Richard M. Stallman's avatar
Richard M. Stallman committed
744 745 746
    (emerge-eval-in-buffer
     buffer-B
     (widen)
747 748 749 750
     (let ((temp (file-local-copy file-B)))
       (if temp
	   (setq file-B temp
		 startup-hooks
751
		 (cons `(lambda () (delete-file ,file-B))
752
		       startup-hooks))
753 754
           ;; Verify that the file matches the buffer
           (emerge-verify-file-buffer))))
Richard M. Stallman's avatar
Richard M. Stallman committed
755 756 757
    (emerge-eval-in-buffer
     buffer-ancestor
     (widen)
758 759 760 761
     (let ((temp (file-local-copy file-ancestor)))
       (if temp
	   (setq file-ancestor temp
		 startup-hooks
762
		 (cons `(lambda () (delete-file ,file-ancestor))
763
		       startup-hooks))
764 765
           ;; Verify that the file matches the buffer
           (emerge-verify-file-buffer))))
Jim Blandy's avatar
Jim Blandy committed
766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804
    (emerge-setup-with-ancestor buffer-A file-A buffer-B file-B
				buffer-ancestor file-ancestor
				startup-hooks quit-hooks output-file)))

;; Start up Emerge on two files with an ancestor
(defun emerge-setup-with-ancestor (buffer-A file-A buffer-B file-B
					    buffer-ancestor file-ancestor
					    &optional startup-hooks quit-hooks
					    output-file)
  (setq file-A (expand-file-name file-A))
  (setq file-B (expand-file-name file-B))
  (setq file-ancestor (expand-file-name file-ancestor))
  (setq output-file (and output-file (expand-file-name output-file)))
  (let* ((merge-buffer-name (emerge-unique-buffer-name "*merge" "*"))
	 ;; create the merge buffer from buffer A, so it inherits buffer A's
	 ;; default directory, etc.
	 (merge-buffer (emerge-eval-in-buffer
			buffer-A
			(get-buffer-create merge-buffer-name))))
    (emerge-eval-in-buffer
     merge-buffer
     (emerge-copy-modes buffer-A)
     (setq buffer-read-only nil)
     (auto-save-mode 1)
     (setq emerge-mode t)
     (setq emerge-A-buffer buffer-A)
     (setq emerge-B-buffer buffer-B)
     (setq emerge-ancestor-buffer buffer-ancestor)
     (setq emerge-merge-buffer merge-buffer)
     (setq emerge-output-description
	   (if output-file
	       (concat "Output to file: " output-file)
	     (concat "Output to buffer: " (buffer-name merge-buffer))))
     (insert-buffer emerge-A-buffer)
     (emerge-set-keys)
     (setq emerge-difference-list
	   (emerge-make-diff3-list file-A file-B file-ancestor))
     (setq emerge-number-of-differences (length emerge-difference-list))
     (setq emerge-current-difference -1)
805
     (setq emerge-quit-hook quit-hooks)
Jim Blandy's avatar
Jim Blandy committed
806
     (emerge-remember-buffer-characteristics)
Richard M. Stallman's avatar
Richard M. Stallman committed
807 808
     (emerge-select-prefer-Bs)
     (emerge-handle-local-variables))
Jim Blandy's avatar
Jim Blandy committed
809 810
    (emerge-setup-windows buffer-A buffer-B merge-buffer t)
    (emerge-eval-in-buffer merge-buffer
811
			   (run-hooks 'startup-hooks 'emerge-startup-hook)
Jim Blandy's avatar
Jim Blandy committed
812 813 814 815 816 817 818 819 820 821 822
			   (setq buffer-read-only t))))

;; Generate the Emerge difference list between two files with an ancestor
(defun emerge-make-diff3-list (file-A file-B file-ancestor)
  (setq emerge-diff-buffer (get-buffer-create "*emerge-diff*"))
  (emerge-eval-in-buffer
   emerge-diff-buffer
   (erase-buffer)
   (shell-command
    (format "%s %s %s %s %s"
	    emerge-diff3-program emerge-diff-options
Richard M. Stallman's avatar
Richard M. Stallman committed
823
	    (emerge-protect-metachars file-A)
824
	    (emerge-protect-metachars file-ancestor)
Richard M. Stallman's avatar
Richard M. Stallman committed
825
	    (emerge-protect-metachars file-B))
Jim Blandy's avatar
Jim Blandy committed
826
    t))
827
  (emerge-prepare-error-list emerge-diff3-ok-lines-regexp)
Jim Blandy's avatar
Jim Blandy committed
828 829 830 831 832 833 834 835 836 837 838 839 840
  (emerge-convert-diffs-to-markers
   emerge-A-buffer emerge-B-buffer emerge-merge-buffer
   (emerge-extract-diffs3 emerge-diff-buffer)))

(defun emerge-extract-diffs3 (diff-buffer)
  (let (list)
    (emerge-eval-in-buffer
     diff-buffer
     (while (re-search-forward "^====\\(.?\\)$" nil t)
       ;; leave point after matched line
       (beginning-of-line 2)
       (let ((agreement (buffer-substring (match-beginning 1) (match-end 1))))
	 ;; if the A and B files are the same, ignore the difference
841
	 (if (not (string-equal agreement "2"))
Jim Blandy's avatar
Jim Blandy committed
842
	     (setq list
843
		   (cons
844
		    (let (group-1 group-3 pos)
845
		      (setq pos (point))
846
		      (setq group-1 (emerge-get-diff3-group "1"))
847 848
		      (goto-char pos)
		      (setq group-3 (emerge-get-diff3-group "3"))
849
		      (vector (car group-1) (car (cdr group-1))
Jim Blandy's avatar
Jim Blandy committed
850
			      (car group-3) (car (cdr group-3))
851
			      (cond ((string-equal agreement "1") 'prefer-A)
Jim Blandy's avatar
Jim Blandy committed
852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870
				    ((string-equal agreement "3") 'prefer-B)
				    (t 'default-A))))
		    list))))))
    (nreverse list)))

(defun emerge-get-diff3-group (file)
  ;; This save-excursion allows emerge-get-diff3-group to be called for the
  ;; various groups of lines (1, 2, 3) in any order, and for the lines to
  ;; appear in any order.  The reason this is necessary is that Gnu diff3
  ;; can produce the groups in the order 1, 2, 3 or 1, 3, 2.
  (save-excursion
    (re-search-forward
     (concat "^" file ":\\([0-9]+\\)\\(,\\([0-9]+\\)\\)?\\([ac]\\)$"))
    (beginning-of-line 2)
    ;; treatment depends on whether it is an "a" group or a "c" group
    (if (string-equal (buffer-substring (match-beginning 4) (match-end 4)) "c")
	;; it is a "c" group
	(if (match-beginning 2)
	    ;; it has two numbers
871
	    (list (string-to-number
Jim Blandy's avatar
Jim Blandy committed
872
		   (buffer-substring (match-beginning 1) (match-end 1)))
873
		  (1+ (string-to-number
Jim Blandy's avatar
Jim Blandy committed
874 875
		       (buffer-substring (match-beginning 3) (match-end 3)))))
	  ;; it has one number
876
	  (let ((x (string-to-number
Jim Blandy's avatar
Jim Blandy committed
877 878 879
		    (buffer-substring (match-beginning 1) (match-end 1)))))
	    (list x (1+ x))))
      ;; it is an "a" group
880
      (let ((x (1+ (string-to-number
Jim Blandy's avatar
Jim Blandy committed
881 882 883 884 885
		    (buffer-substring (match-beginning 1) (match-end 1))))))
	(list x x)))))

;;; Functions to start Emerge on files

886
;;;###autoload
Jim Blandy's avatar
Jim Blandy committed
887 888 889 890 891 892
(defun emerge-files (arg file-A file-B file-out &optional startup-hooks
		     quit-hooks)
  "Run Emerge on two files."
  (interactive
   (let (f)
     (list current-prefix-arg
Richard M. Stallman's avatar
Richard M. Stallman committed
893
	   (setq f (emerge-read-file-name "File A to merge" emerge-last-dir-A
894 895
					  nil nil t))
	   (emerge-read-file-name "File B to merge" emerge-last-dir-B nil f t)
Jim Blandy's avatar
Jim Blandy committed
896
	   (and current-prefix-arg
Richard M. Stallman's avatar
Richard M. Stallman committed
897
		(emerge-read-file-name "Output file" emerge-last-dir-output
898
				       f f nil)))))
899
  (if file-out
900
      (add-hook 'quit-hooks `(lambda () (emerge-files-exit ,file-out))))
Jim Blandy's avatar
Jim Blandy committed
901 902
  (emerge-files-internal
   file-A file-B startup-hooks
903
   quit-hooks
Jim Blandy's avatar
Jim Blandy committed
904 905
   file-out))

906
;;;###autoload
Jim Blandy's avatar
Jim Blandy committed
907 908 909 910 911 912
(defun emerge-files-with-ancestor (arg file-A file-B file-ancestor file-out
				   &optional startup-hooks quit-hooks)
  "Run Emerge on two files, giving another file as the ancestor."
  (interactive
   (let (f)
     (list current-prefix-arg
Richard M. Stallman's avatar
Richard M. Stallman committed
913
	   (setq f (emerge-read-file-name "File A to merge" emerge-last-dir-A
914 915
					  nil nil t))
	   (emerge-read-file-name "File B to merge" emerge-last-dir-B nil f t)
Richard M. Stallman's avatar
Richard M. Stallman committed
916
	   (emerge-read-file-name "Ancestor file" emerge-last-dir-ancestor
917
				  nil f t)
Jim Blandy's avatar
Jim Blandy committed
918
	   (and current-prefix-arg
Richard M. Stallman's avatar
Richard M. Stallman committed
919
		(emerge-read-file-name "Output file" emerge-last-dir-output
920
				       f f nil)))))
921
  (if file-out
922
      (add-hook 'quit-hooks `(lambda () (emerge-files-exit ,file-out))))
Jim Blandy's avatar
Jim Blandy committed
923 924
  (emerge-files-with-ancestor-internal
   file-A file-B file-ancestor startup-hooks
925
   quit-hooks
Jim Blandy's avatar
Jim Blandy committed
926 927 928 929 930 931 932 933 934 935
   file-out))

;; Write the merge buffer out in place of the file the A buffer is visiting.
(defun emerge-files-exit (file-out)
  ;; if merge was successful was given, save to disk
  (if (not emerge-prefix-argument)
      (emerge-write-and-delete file-out)))

;;; Functions to start Emerge on buffers

936
;;;###autoload
Jim Blandy's avatar
Jim Blandy committed
937 938 939 940 941 942 943 944 945 946 947 948 949
(defun emerge-buffers (buffer-A buffer-B &optional startup-hooks quit-hooks)
  "Run Emerge on two buffers."
  (interactive "bBuffer A to merge: \nbBuffer B to merge: ")
  (let ((emerge-file-A (emerge-make-temp-file "A"))
	(emerge-file-B (emerge-make-temp-file "B")))
    (emerge-eval-in-buffer
     buffer-A
     (write-region (point-min) (point-max) emerge-file-A nil 'no-message))
    (emerge-eval-in-buffer
     buffer-B
     (write-region (point-min) (point-max) emerge-file-B nil 'no-message))
    (emerge-setup (get-buffer buffer-A) emerge-file-A
		  (get-buffer buffer-B) emerge-file-B
950 951 952
		  (cons `(lambda ()
                          (delete-file ,emerge-file-A)
                          (delete-file ,emerge-file-B))
Jim Blandy's avatar
Jim Blandy committed
953 954 955 956
			startup-hooks)
		  quit-hooks
		  nil)))

957
;;;###autoload
Jim Blandy's avatar
Jim Blandy committed
958
(defun emerge-buffers-with-ancestor (buffer-A buffer-B buffer-ancestor
959 960
                                     &optional startup-hooks
                                     quit-hooks)
Jim Blandy's avatar
Jim Blandy committed
961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980
  "Run Emerge on two buffers, giving another buffer as the ancestor."
  (interactive
   "bBuffer A to merge: \nbBuffer B to merge: \nbAncestor buffer: ")
  (let ((emerge-file-A (emerge-make-temp-file "A"))
	(emerge-file-B (emerge-make-temp-file "B"))
	(emerge-file-ancestor (emerge-make-temp-file "anc")))
    (emerge-eval-in-buffer
     buffer-A
     (write-region (point-min) (point-max) emerge-file-A nil 'no-message))
    (emerge-eval-in-buffer
     buffer-B
     (write-region (point-min) (point-max) emerge-file-B nil 'no-message))
    (emerge-eval-in-buffer
     buffer-ancestor
     (write-region (point-min) (point-max) emerge-file-ancestor nil
		   'no-message))
    (emerge-setup-with-ancestor (get-buffer buffer-A) emerge-file-A
				(get-buffer buffer-B) emerge-file-B
				(get-buffer buffer-ancestor)
				emerge-file-ancestor
981 982 983 984 985
				(cons `(lambda ()
                                        (delete-file ,emerge-file-A)
                                        (delete-file ,emerge-file-B)
                                        (delete-file
                                         ,emerge-file-ancestor))
Jim Blandy's avatar
Jim Blandy committed
986 987 988 989 990 991
				      startup-hooks)
				quit-hooks
				nil)))

;;; Functions to start Emerge from the command line

992
;;;###autoload
Jim Blandy's avatar
Jim Blandy committed
993 994 995 996 997 998 999
(defun emerge-files-command ()
  (let ((file-a (nth 0 command-line-args-left))
	(file-b (nth 1 command-line-args-left))
	(file-out (nth 2 command-line-args-left)))
    (setq command-line-args-left (nthcdr 3 command-line-args-left))
    (emerge-files-internal
     file-a file-b nil
1000
     (list `(lambda () (emerge-command-exit ,file-out))))))
Jim Blandy's avatar
Jim Blandy committed
1001

1002
;;;###autoload
Jim Blandy's avatar
Jim Blandy committed
1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013
(defun emerge-files-with-ancestor-command ()
  (let (file-a file-b file-anc file-out)
    ;; check for a -a flag, for filemerge compatibility
    (if (string= (car command-line-args-left) "-a")
	;; arguments are "-a ancestor file-a file-b file-out"
	(progn
	  (setq file-a (nth 2 command-line-args-left))
	  (setq file-b (nth 3 command-line-args-left))
	  (setq file-anc (nth 1 command-line-args-left))
	  (setq file-out (nth 4 command-line-args-left))
	  (setq command-line-args-left (nthcdr 5 command-line-args-left)))
1014 1015 1016 1017 1018 1019
        ;; arguments are "file-a file-b ancestor file-out"
        (setq file-a (nth 0 command-line-args-left))
        (setq file-b (nth 1 command-line-args-left))
        (setq file-anc (nth 2 command-line-args-left))
        (setq file-out (nth 3 command-line-args-left))
        (setq command-line-args-left (nthcdr 4 command-line-args-left)))
Jim Blandy's avatar
Jim Blandy committed
1020 1021
    (emerge-files-with-ancestor-internal
     file-a file-b file-anc nil
1022
     (list `(lambda () (emerge-command-exit ,file-out))))))
1023

Jim Blandy's avatar
Jim Blandy committed
1024 1025 1026 1027 1028 1029
(defun emerge-command-exit (file-out)
  (emerge-write-and-delete file-out)
  (kill-emacs (if emerge-prefix-argument 1 0)))

;;; Functions to start Emerge via remote request

1030
;;;###autoload
Jim Blandy's avatar
Jim Blandy committed
1031 1032 1033 1034
(defun emerge-files-remote (file-a file-b file-out)
  (setq emerge-file-out file-out)
  (emerge-files-internal
   file-a file-b nil
1035
   (list `(lambda () (emerge-remote-exit ,file-out ',emerge-exit-func)))
Jim Blandy's avatar
Jim Blandy committed
1036 1037 1038
   file-out)
  (throw 'client-wait nil))

1039
;;;###autoload
Jim Blandy's avatar
Jim Blandy committed
1040 1041 1042 1043
(defun emerge-files-with-ancestor-remote (file-a file-b file-anc file-out)
  (setq emerge-file-out file-out)
  (emerge-files-with-ancestor-internal
   file-a file-b file-anc nil
1044
   (list `(lambda () (emerge-remote-exit ,file-out ',emerge-exit-func)))
Jim Blandy's avatar
Jim Blandy committed
1045 1046 1047
   file-out)
  (throw 'client-wait nil))

1048
(defun emerge-remote-exit (file-out emerge-exit-func)
Jim Blandy's avatar
Jim Blandy committed
1049 1050
  (emerge-write-and-delete file-out)
  (kill-buffer emerge-merge-buffer)
1051
  (funcall emerge-exit-func (if emerge-prefix-argument 1 0)))
Jim Blandy's avatar
Jim Blandy committed
1052

Richard M. Stallman's avatar
Richard M. Stallman committed
1053 1054
;;; Functions to start Emerge on RCS versions

1055
;;;###autoload
Richard M. Stallman's avatar
Richard M. Stallman committed
1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068
(defun emerge-revisions (arg file revision-A revision-B
			 &optional startup-hooks quit-hooks)
  "Emerge two RCS revisions of a file."
  (interactive
   (list current-prefix-arg
	 (read-file-name "File to merge: " nil nil 'confirm)
	 (read-string "Revision A to merge: " emerge-last-revision-A)
	 (read-string "Revision B to merge: " emerge-last-revision-B)))
  (setq emerge-last-revision-A revision-A
	emerge-last-revision-B revision-B)
  (emerge-revisions-internal
   file revision-A revision-B startup-hooks
   (if arg
1069 1070 1071
       (cons `(lambda ()
               (shell-command
                ,(format "%s %s" emerge-rcs-ci-program file)))
Richard M. Stallman's avatar
Richard M. Stallman committed
1072
	     quit-hooks)
1073
       quit-hooks)))
Richard M. Stallman's avatar
Richard M. Stallman committed
1074

1075
;;;###autoload
Richard M. Stallman's avatar
Richard M. Stallman committed
1076
(defun emerge-revisions-with-ancestor (arg file revision-A
1077 1078 1079
                                       revision-B ancestor
                                       &optional
                                       startup-hooks quit-hooks)
1080
  "Emerge two RCS revisions of a file, with another revision as ancestor."
Richard M. Stallman's avatar
Richard M. Stallman committed
1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093
  (interactive
   (list current-prefix-arg
	 (read-file-name "File to merge: " nil nil 'confirm)
	 (read-string "Revision A to merge: " emerge-last-revision-A)
	 (read-string "Revision B to merge: " emerge-last-revision-B)
	 (read-string "Ancestor: " emerge-last-revision-ancestor)))
  (setq emerge-last-revision-A revision-A
	emerge-last-revision-B revision-B
	emerge-last-revision-ancestor ancestor)
  (emerge-revision-with-ancestor-internal
   file revision-A revision-B ancestor startup-hooks
   (if arg
       (let ((cmd ))
1094 1095 1096
	 (cons `(lambda ()
                 (shell-command
                  ,(format "%s %s" emerge-rcs-ci-program file)))
Richard M. Stallman's avatar
Richard M. Stallman committed
1097
	       quit-hooks))
1098
       quit-hooks)))
Richard M. Stallman's avatar
Richard M. Stallman committed
1099 1100

(defun emerge-revisions-internal (file revision-A revision-B &optional
1101
                                  startup-hooks quit-hooks output-file)
Richard M. Stallman's avatar
Richard M. Stallman committed
1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122