(smerge-ensure-match): Don't use `format' on `error' arguments.
[bpt/emacs.git] / lisp / smerge-mode.el
1 ;;; smerge-mode.el --- Minor mode to resolve diff3 conflicts
2
3 ;; Copyright (C) 1999, 2000, 2001, 2003, 2004, 2005 Free Software Foundation, Inc.
4
5 ;; Author: Stefan Monnier <monnier@cs.yale.edu>
6 ;; Keywords: revision-control merge diff3 cvs conflict
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;; Provides a lightweight alternative to emerge/ediff.
28 ;; To use it, simply add to your .emacs the following lines:
29 ;;
30 ;; (autoload 'smerge-mode "smerge-mode" nil t)
31 ;;
32 ;; you can even have it turned on automatically with the following
33 ;; piece of code in your .emacs:
34 ;;
35 ;; (defun sm-try-smerge ()
36 ;; (save-excursion
37 ;; (goto-char (point-min))
38 ;; (when (re-search-forward "^<<<<<<< " nil t)
39 ;; (smerge-mode 1))))
40 ;; (add-hook 'find-file-hook 'sm-try-smerge t)
41
42 ;;; Todo:
43
44 ;; - if requested, ask the user whether he wants to call ediff right away
45
46 ;;; Code:
47
48 (eval-when-compile (require 'cl))
49
50
51 (defgroup smerge ()
52 "Minor mode to resolve diff3 conflicts."
53 :group 'tools
54 :prefix "smerge-")
55
56 (defcustom smerge-diff-buffer-name "*vc-diff*"
57 "Buffer name to use for displaying diffs."
58 :group 'smerge
59 :type '(choice
60 (const "*vc-diff*")
61 (const "*cvs-diff*")
62 (const "*smerge-diff*")
63 string))
64
65 (defcustom smerge-diff-switches
66 (append '("-d" "-b")
67 (if (listp diff-switches) diff-switches (list diff-switches)))
68 "*A list of strings specifying switches to be passed to diff.
69 Used in `smerge-diff-base-mine' and related functions."
70 :group 'smerge
71 :type '(repeat string))
72
73 (defcustom smerge-auto-leave t
74 "*Non-nil means to leave `smerge-mode' when the last conflict is resolved."
75 :group 'smerge
76 :type 'boolean)
77
78 (defface smerge-mine
79 '((((min-colors 88) (background light))
80 (:foreground "blue1"))
81 (((background light))
82 (:foreground "blue"))
83 (((min-colors 88) (background dark))
84 (:foreground "cyan1"))
85 (((background dark))
86 (:foreground "cyan")))
87 "Face for your code."
88 :group 'smerge)
89 ;; backward-compatibility alias
90 (put 'smerge-mine-face 'face-alias 'smerge-mine)
91 (defvar smerge-mine-face 'smerge-mine)
92
93 (defface smerge-other
94 '((((background light))
95 (:foreground "darkgreen"))
96 (((background dark))
97 (:foreground "lightgreen")))
98 "Face for the other code."
99 :group 'smerge)
100 ;; backward-compatibility alias
101 (put 'smerge-other-face 'face-alias 'smerge-other)
102 (defvar smerge-other-face 'smerge-other)
103
104 (defface smerge-base
105 '((((min-colors 88) (background light))
106 (:foreground "red1"))
107 (((background light))
108 (:foreground "red"))
109 (((background dark))
110 (:foreground "orange")))
111 "Face for the base code."
112 :group 'smerge)
113 ;; backward-compatibility alias
114 (put 'smerge-base-face 'face-alias 'smerge-base)
115 (defvar smerge-base-face 'smerge-base)
116
117 (defface smerge-markers
118 '((((background light))
119 (:background "grey85"))
120 (((background dark))
121 (:background "grey30")))
122 "Face for the conflict markers."
123 :group 'smerge)
124 ;; backward-compatibility alias
125 (put 'smerge-markers-face 'face-alias 'smerge-markers)
126 (defvar smerge-markers-face 'smerge-markers)
127
128 (easy-mmode-defmap smerge-basic-map
129 `(("n" . smerge-next)
130 ("p" . smerge-prev)
131 ("r" . smerge-resolve)
132 ("a" . smerge-keep-all)
133 ("b" . smerge-keep-base)
134 ("o" . smerge-keep-other)
135 ("m" . smerge-keep-mine)
136 ("E" . smerge-ediff)
137 ("\C-m" . smerge-keep-current)
138 ("=" . ,(make-sparse-keymap "Diff"))
139 ("=<" "base-mine" . smerge-diff-base-mine)
140 ("=>" "base-other" . smerge-diff-base-other)
141 ("==" "mine-other" . smerge-diff-mine-other))
142 "The base keymap for `smerge-mode'.")
143
144 (defcustom smerge-command-prefix "\C-c^"
145 "Prefix for `smerge-mode' commands."
146 :group 'smerge
147 :type '(choice (string "\e") (string "\C-c^") (string "") string))
148
149 (easy-mmode-defmap smerge-mode-map
150 `((,smerge-command-prefix . ,smerge-basic-map))
151 "Keymap for `smerge-mode'.")
152
153 (defvar smerge-check-cache nil)
154 (make-variable-buffer-local 'smerge-check-cache)
155 (defun smerge-check (n)
156 (condition-case nil
157 (let ((state (cons (point) (buffer-modified-tick))))
158 (unless (equal (cdr smerge-check-cache) state)
159 (smerge-match-conflict)
160 (setq smerge-check-cache (cons (match-data) state)))
161 (nth (* 2 n) (car smerge-check-cache)))
162 (error nil)))
163
164 (easy-menu-define smerge-mode-menu smerge-mode-map
165 "Menu for `smerge-mode'."
166 '("SMerge"
167 ["Next" smerge-next :help "Go to next conflict"]
168 ["Previous" smerge-prev :help "Go to previous conflict"]
169 "--"
170 ["Keep All" smerge-keep-all :help "Keep all three versions"
171 :active (smerge-check 1)]
172 ["Keep Current" smerge-keep-current :help "Use current (at point) version"
173 :active (and (smerge-check 1) (> (smerge-get-current) 0))]
174 "--"
175 ["Revert to Base" smerge-keep-base :help "Revert to base version"
176 :active (smerge-check 2)]
177 ["Keep Other" smerge-keep-other :help "Keep `other' version"
178 :active (smerge-check 3)]
179 ["Keep Yours" smerge-keep-mine :help "Keep your version"
180 :active (smerge-check 1)]
181 "--"
182 ["Diff Base/Mine" smerge-diff-base-mine
183 :help "Diff `base' and `mine' for current conflict"
184 :active (smerge-check 2)]
185 ["Diff Base/Other" smerge-diff-base-other
186 :help "Diff `base' and `other' for current conflict"
187 :active (smerge-check 2)]
188 ["Diff Mine/Other" smerge-diff-mine-other
189 :help "Diff `mine' and `other' for current conflict"
190 :active (smerge-check 1)]
191 "--"
192 ["Invoke Ediff" smerge-ediff
193 :help "Use Ediff to resolve the conflicts"
194 :active (smerge-check 1)]
195 ["Auto Resolve" smerge-resolve
196 :help "Try auto-resolution heuristics"
197 :active (smerge-check 1)]
198 ["Combine" smerge-combine-with-next
199 :help "Combine current conflict with next"
200 :active (smerge-check 1)]
201 ))
202
203 (easy-menu-define smerge-context-menu nil
204 "Context menu for mine area in `smerge-mode'."
205 '(nil
206 ["Keep Current" smerge-keep-current :help "Use current (at point) version"]
207 ["Kill Current" smerge-kill-current :help "Remove current (at point) version"]
208 ["Keep All" smerge-keep-all :help "Keep all three versions"]
209 "---"
210 ["More..." (popup-menu smerge-mode-menu) :help "Show full SMerge mode menu"]
211 ))
212
213 (defconst smerge-font-lock-keywords
214 '((smerge-find-conflict
215 (1 smerge-mine-face prepend t)
216 (2 smerge-base-face prepend t)
217 (3 smerge-other-face prepend t)
218 ;; FIXME: `keep' doesn't work right with syntactic fontification.
219 (0 smerge-markers-face keep)
220 (4 nil t t)
221 (5 nil t t)))
222 "Font lock patterns for `smerge-mode'.")
223
224 (defconst smerge-begin-re "^<<<<<<< \\(.*\\)\n")
225 (defconst smerge-end-re "^>>>>>>> .*\n")
226 (defconst smerge-base-re "^||||||| .*\n")
227 (defconst smerge-other-re "^=======\n")
228
229 (defvar smerge-conflict-style nil
230 "Keep track of which style of conflict is in use.
231 Can be nil if the style is undecided, or else:
232 - `diff3-E'
233 - `diff3-A'")
234
235 ;; Compiler pacifiers
236 (defvar font-lock-mode)
237 (defvar font-lock-keywords)
238
239 ;;;;
240 ;;;; Actual code
241 ;;;;
242
243 ;; Define smerge-next and smerge-prev
244 (easy-mmode-define-navigation smerge smerge-begin-re "conflict")
245
246 (defconst smerge-match-names ["conflict" "mine" "base" "other"])
247
248 (defun smerge-ensure-match (n)
249 (unless (match-end n)
250 (error "No `%s'" (aref smerge-match-names n))))
251
252 (defun smerge-auto-leave ()
253 (when (and smerge-auto-leave
254 (save-excursion (goto-char (point-min))
255 (not (re-search-forward smerge-begin-re nil t))))
256 (smerge-mode -1)))
257
258
259 (defun smerge-keep-all ()
260 "Concatenate all versions."
261 (interactive)
262 (smerge-match-conflict)
263 (let ((mb2 (or (match-beginning 2) (point-max)))
264 (me2 (or (match-end 2) (point-min))))
265 (delete-region (match-end 3) (match-end 0))
266 (delete-region (max me2 (match-end 1)) (match-beginning 3))
267 (if (and (match-end 2) (/= (match-end 1) (match-end 3)))
268 (delete-region (match-end 1) (match-beginning 2)))
269 (delete-region (match-beginning 0) (min (match-beginning 1) mb2))
270 (smerge-auto-leave)))
271
272 (defun smerge-keep-n (n)
273 ;; We used to use replace-match, but that did not preserve markers so well.
274 (delete-region (match-end n) (match-end 0))
275 (delete-region (match-beginning 0) (match-beginning n)))
276
277 (defun smerge-combine-with-next ()
278 "Combine the current conflict with the next one."
279 (interactive)
280 (smerge-match-conflict)
281 (let ((ends nil))
282 (dolist (i '(3 2 1 0))
283 (push (if (match-end i) (copy-marker (match-end i) t)) ends))
284 (setq ends (apply 'vector ends))
285 (goto-char (aref ends 0))
286 (if (not (re-search-forward smerge-begin-re nil t))
287 (error "No next conflict")
288 (smerge-match-conflict)
289 (let ((match-data (mapcar (lambda (m) (if m (copy-marker m)))
290 (match-data))))
291 ;; First copy the in-between text in each alternative.
292 (dolist (i '(1 2 3))
293 (when (aref ends i)
294 (goto-char (aref ends i))
295 (insert-buffer-substring (current-buffer)
296 (aref ends 0) (car match-data))))
297 (delete-region (aref ends 0) (car match-data))
298 ;; Then move the second conflict's alternatives into the first.
299 (dolist (i '(1 2 3))
300 (set-match-data match-data)
301 (when (and (aref ends i) (match-end i))
302 (goto-char (aref ends i))
303 (insert-buffer-substring (current-buffer)
304 (match-beginning i) (match-end i))))
305 (delete-region (car match-data) (cadr match-data))
306 ;; Free the markers.
307 (dolist (m match-data) (if m (move-marker m nil)))
308 (mapc (lambda (m) (if m (move-marker m nil))) ends)))))
309
310 (defvar smerge-resolve-function
311 (lambda () (error "Don't know how to resolve"))
312 "Mode-specific merge function.
313 The function is called with no argument and with the match data set
314 according to `smerge-match-conflict'.")
315
316 (defvar smerge-text-properties
317 `(help-echo "merge conflict: mouse-3 shows a menu"
318 ;; mouse-face highlight
319 keymap (keymap (down-mouse-3 . smerge-popup-context-menu))))
320
321 (defun smerge-remove-props (&optional beg end)
322 (remove-text-properties
323 (or beg (match-beginning 0))
324 (or end (match-end 0))
325 smerge-text-properties))
326
327 (defun smerge-popup-context-menu (event)
328 "Pop up the Smerge mode context menu under mouse."
329 (interactive "e")
330 (if (and smerge-mode
331 (save-excursion (posn-set-point (event-end event)) (smerge-check 1)))
332 (progn
333 (posn-set-point (event-end event))
334 (smerge-match-conflict)
335 (let ((i (smerge-get-current))
336 o)
337 (if (<= i 0)
338 ;; Out of range
339 (popup-menu smerge-mode-menu)
340 ;; Install overlay.
341 (setq o (make-overlay (match-beginning i) (match-end i)))
342 (unwind-protect
343 (progn
344 (overlay-put o 'face 'highlight)
345 (sit-for 0) ;Display the new highlighting.
346 (popup-menu smerge-context-menu))
347 ;; Delete overlay.
348 (delete-overlay o)))))
349 ;; There's no conflict at point, the text-props are just obsolete.
350 (save-excursion
351 (let ((beg (re-search-backward smerge-end-re nil t))
352 (end (re-search-forward smerge-begin-re nil t)))
353 (smerge-remove-props (or beg (point-min)) (or end (point-max)))
354 (push event unread-command-events)))))
355
356 (defun smerge-resolve ()
357 "Resolve the conflict at point intelligently.
358 This relies on mode-specific knowledge and thus only works in
359 some major modes. Uses `smerge-resolve-function' to do the actual work."
360 (interactive)
361 (smerge-match-conflict)
362 (smerge-remove-props)
363 (cond
364 ;; Trivial diff3 -A non-conflicts.
365 ((and (eq (match-end 1) (match-end 3))
366 (eq (match-beginning 1) (match-beginning 3)))
367 ;; FIXME: Add "if [ diff -b MINE OTHER ]; then select OTHER; fi"
368 (smerge-keep-n 3))
369 ((and (match-end 2)
370 ;; FIXME: Add "diff -b BASE MINE | patch OTHER".
371 ;; FIXME: Add "diff -b BASE OTHER | patch MINE".
372 nil)
373 )
374 ((and (not (match-end 2))
375 ;; FIXME: Add "diff -b"-based refinement.
376 nil)
377 )
378 (t
379 ;; Mode-specific conflict resolution.
380 (funcall smerge-resolve-function)))
381 (smerge-auto-leave))
382
383 (defun smerge-keep-base ()
384 "Revert to the base version."
385 (interactive)
386 (smerge-match-conflict)
387 (smerge-ensure-match 2)
388 (smerge-remove-props)
389 (smerge-keep-n 2)
390 (smerge-auto-leave))
391
392 (defun smerge-keep-other ()
393 "Use \"other\" version."
394 (interactive)
395 (smerge-match-conflict)
396 ;;(smerge-ensure-match 3)
397 (smerge-remove-props)
398 (smerge-keep-n 3)
399 (smerge-auto-leave))
400
401 (defun smerge-keep-mine ()
402 "Keep your version."
403 (interactive)
404 (smerge-match-conflict)
405 ;;(smerge-ensure-match 1)
406 (smerge-remove-props)
407 (smerge-keep-n 1)
408 (smerge-auto-leave))
409
410 (defun smerge-get-current ()
411 (let ((i 3))
412 (while (or (not (match-end i))
413 (< (point) (match-beginning i))
414 (>= (point) (match-end i)))
415 (decf i))
416 i))
417
418 (defun smerge-keep-current ()
419 "Use the current (under the cursor) version."
420 (interactive)
421 (smerge-match-conflict)
422 (let ((i (smerge-get-current)))
423 (if (<= i 0) (error "Not inside a version")
424 (smerge-remove-props)
425 (smerge-keep-n i)
426 (smerge-auto-leave))))
427
428 (defun smerge-kill-current ()
429 "Remove the current (under the cursor) version."
430 (interactive)
431 (smerge-match-conflict)
432 (let ((i (smerge-get-current)))
433 (if (<= i 0) (error "Not inside a version")
434 (smerge-remove-props)
435 (let ((left nil))
436 (dolist (n '(3 2 1))
437 (if (and (match-end n) (/= (match-end n) (match-end i)))
438 (push n left)))
439 (if (and (cdr left)
440 (/= (match-end (car left)) (match-end (cadr left))))
441 (ding) ;We don't know how to do that.
442 (smerge-keep-n (car left))
443 (smerge-auto-leave))))))
444
445 (defun smerge-diff-base-mine ()
446 "Diff 'base' and 'mine' version in current conflict region."
447 (interactive)
448 (smerge-diff 2 1))
449
450 (defun smerge-diff-base-other ()
451 "Diff 'base' and 'other' version in current conflict region."
452 (interactive)
453 (smerge-diff 2 3))
454
455 (defun smerge-diff-mine-other ()
456 "Diff 'mine' and 'other' version in current conflict region."
457 (interactive)
458 (smerge-diff 1 3))
459
460 (defun smerge-match-conflict ()
461 "Get info about the conflict. Puts the info in the `match-data'.
462 The submatches contain:
463 0: the whole conflict.
464 1: your code.
465 2: the base code.
466 3: other code.
467 An error is raised if not inside a conflict."
468 (save-excursion
469 (condition-case nil
470 (let* ((orig-point (point))
471
472 (_ (forward-line 1))
473 (_ (re-search-backward smerge-begin-re))
474
475 (start (match-beginning 0))
476 (mine-start (match-end 0))
477 (filename (or (match-string 1) ""))
478
479 (_ (re-search-forward smerge-end-re))
480 (_ (assert (< orig-point (match-end 0))))
481
482 (other-end (match-beginning 0))
483 (end (match-end 0))
484
485 (_ (re-search-backward smerge-other-re start))
486
487 (mine-end (match-beginning 0))
488 (other-start (match-end 0))
489
490 base-start base-end)
491
492 ;; handle the various conflict styles
493 (cond
494 ((save-excursion
495 (goto-char mine-start)
496 (re-search-forward smerge-begin-re end t))
497 ;; There's a nested conflict and we're after the the beginning
498 ;; of the outer one but before the beginning of the inner one.
499 (error "There is a nested conflict"))
500
501 ((re-search-backward smerge-base-re start t)
502 ;; a 3-parts conflict
503 (set (make-local-variable 'smerge-conflict-style) 'diff3-A)
504 (setq base-end mine-end)
505 (setq mine-end (match-beginning 0))
506 (setq base-start (match-end 0)))
507
508 ((string= filename (file-name-nondirectory
509 (or buffer-file-name "")))
510 ;; a 2-parts conflict
511 (set (make-local-variable 'smerge-conflict-style) 'diff3-E))
512
513 ((and (not base-start)
514 (or (eq smerge-conflict-style 'diff3-A)
515 (equal filename "ANCESTOR")
516 (string-match "\\`[.0-9]+\\'" filename)))
517 ;; a same-diff conflict
518 (setq base-start mine-start)
519 (setq base-end mine-end)
520 (setq mine-start other-start)
521 (setq mine-end other-end)))
522
523 (let ((inhibit-read-only t)
524 (inhibit-modification-hooks t)
525 (m (buffer-modified-p)))
526 (unwind-protect
527 (add-text-properties start end smerge-text-properties)
528 (restore-buffer-modified-p m)))
529
530 (store-match-data (list start end
531 mine-start mine-end
532 base-start base-end
533 other-start other-end
534 (when base-start (1- base-start)) base-start
535 (1- other-start) other-start))
536 t)
537 (search-failed (error "Point not in conflict region")))))
538
539 (defun smerge-find-conflict (&optional limit)
540 "Find and match a conflict region. Intended as a font-lock MATCHER.
541 The submatches are the same as in `smerge-match-conflict'.
542 Returns non-nil if a match is found between the point and LIMIT.
543 The point is moved to the end of the conflict."
544 (when (re-search-forward smerge-begin-re limit t)
545 (condition-case err
546 (progn
547 (smerge-match-conflict)
548 (goto-char (match-end 0)))
549 (error (smerge-find-conflict limit)))))
550
551 (defun smerge-diff (n1 n2)
552 (smerge-match-conflict)
553 (smerge-ensure-match n1)
554 (smerge-ensure-match n2)
555 (let ((name1 (aref smerge-match-names n1))
556 (name2 (aref smerge-match-names n2))
557 ;; Read them before the match-data gets clobbered.
558 (beg1 (match-beginning n1))
559 (end1 (match-end n1))
560 (beg2 (match-beginning n2))
561 (end2 (match-end n2))
562 (file1 (make-temp-file "smerge1"))
563 (file2 (make-temp-file "smerge2"))
564 (dir default-directory)
565 (file (file-relative-name buffer-file-name))
566 (coding-system-for-read buffer-file-coding-system))
567 (write-region beg1 end1 file1 nil 'nomessage)
568 (write-region beg2 end2 file2 nil 'nomessage)
569 (unwind-protect
570 (with-current-buffer (get-buffer-create smerge-diff-buffer-name)
571 (setq default-directory dir)
572 (let ((inhibit-read-only t))
573 (erase-buffer)
574 (let ((status
575 (apply 'call-process diff-command nil t nil
576 (append smerge-diff-switches
577 (list "-L" (concat name1 "/" file)
578 "-L" (concat name2 "/" file)
579 file1 file2)))))
580 (if (eq status 0) (insert "No differences found.\n"))))
581 (goto-char (point-min))
582 (diff-mode)
583 (display-buffer (current-buffer) t))
584 (delete-file file1)
585 (delete-file file2))))
586
587 ;; compiler pacifiers
588 (defvar smerge-ediff-windows)
589 (defvar smerge-ediff-buf)
590 (defvar ediff-buffer-A)
591 (defvar ediff-buffer-B)
592 (defvar ediff-buffer-C)
593
594 ;;;###autoload
595 (defun smerge-ediff (&optional name-mine name-other name-base)
596 "Invoke ediff to resolve the conflicts.
597 NAME-MINE, NAME-OTHER, and NAME-BASE, if non-nil, are used for the
598 buffer names."
599 (interactive)
600 (let* ((buf (current-buffer))
601 (mode major-mode)
602 ;;(ediff-default-variant 'default-B)
603 (config (current-window-configuration))
604 (filename (file-name-nondirectory buffer-file-name))
605 (mine (generate-new-buffer
606 (or name-mine (concat "*" filename " MINE*"))))
607 (other (generate-new-buffer
608 (or name-other (concat "*" filename " OTHER*"))))
609 base)
610 (with-current-buffer mine
611 (buffer-disable-undo)
612 (insert-buffer-substring buf)
613 (goto-char (point-min))
614 (while (smerge-find-conflict)
615 (when (match-beginning 2) (setq base t))
616 (smerge-keep-n 1))
617 (buffer-enable-undo)
618 (set-buffer-modified-p nil)
619 (funcall mode))
620
621 (with-current-buffer other
622 (buffer-disable-undo)
623 (insert-buffer-substring buf)
624 (goto-char (point-min))
625 (while (smerge-find-conflict)
626 (smerge-keep-n 3))
627 (buffer-enable-undo)
628 (set-buffer-modified-p nil)
629 (funcall mode))
630
631 (when base
632 (setq base (generate-new-buffer
633 (or name-base (concat "*" filename " BASE*"))))
634 (with-current-buffer base
635 (buffer-disable-undo)
636 (insert-buffer-substring buf)
637 (goto-char (point-min))
638 (while (smerge-find-conflict)
639 (if (match-end 2)
640 (smerge-keep-n 2)
641 (delete-region (match-beginning 0) (match-end 0))))
642 (buffer-enable-undo)
643 (set-buffer-modified-p nil)
644 (funcall mode)))
645
646 ;; the rest of the code is inspired from vc.el
647 ;; Fire up ediff.
648 (set-buffer
649 (if base
650 (ediff-merge-buffers-with-ancestor mine other base)
651 ;; nil 'ediff-merge-revisions-with-ancestor buffer-file-name)
652 (ediff-merge-buffers mine other)))
653 ;; nil 'ediff-merge-revisions buffer-file-name)))
654
655 ;; Ediff is now set up, and we are in the control buffer.
656 ;; Do a few further adjustments and take precautions for exit.
657 (set (make-local-variable 'smerge-ediff-windows) config)
658 (set (make-local-variable 'smerge-ediff-buf) buf)
659 (set (make-local-variable 'ediff-quit-hook)
660 (lambda ()
661 (let ((buffer-A ediff-buffer-A)
662 (buffer-B ediff-buffer-B)
663 (buffer-C ediff-buffer-C)
664 (buffer-Ancestor ediff-ancestor-buffer)
665 (buf smerge-ediff-buf)
666 (windows smerge-ediff-windows))
667 (ediff-cleanup-mess)
668 (with-current-buffer buf
669 (erase-buffer)
670 (insert-buffer buffer-C)
671 (kill-buffer buffer-A)
672 (kill-buffer buffer-B)
673 (kill-buffer buffer-C)
674 (when (bufferp buffer-Ancestor) (kill-buffer buffer-Ancestor))
675 (set-window-configuration windows)
676 (message "Conflict resolution finished; you may save the buffer")))))
677 (message "Please resolve conflicts now; exit ediff when done")))
678
679
680 ;;;###autoload
681 (define-minor-mode smerge-mode
682 "Minor mode to simplify editing output from the diff3 program.
683 \\{smerge-mode-map}"
684 :group 'smerge :lighter " SMerge"
685 (when (and (boundp 'font-lock-mode) font-lock-mode)
686 (set (make-local-variable 'font-lock-multiline) t)
687 (save-excursion
688 (if smerge-mode
689 (font-lock-add-keywords nil smerge-font-lock-keywords 'append)
690 (font-lock-remove-keywords nil smerge-font-lock-keywords))
691 (goto-char (point-min))
692 (while (smerge-find-conflict)
693 (save-excursion
694 (font-lock-fontify-region (match-beginning 0) (match-end 0) nil))))))
695
696
697 (provide 'smerge-mode)
698
699 ;; arch-tag: 605c8d1e-e43d-4943-a6f3-1bcc4333e690
700 ;;; smerge-mode.el ends here