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