1 ;;; smerge-mode.el --- Minor mode to resolve diff3 conflicts
3 ;; Copyright (C) 1999, 2000, 01, 03, 2004 Free Software Foundation, Inc.
5 ;; Author: Stefan Monnier <monnier@cs.yale.edu>
6 ;; Keywords: revision-control merge diff3 cvs conflict
8 ;; This file is part of GNU Emacs.
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)
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.
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.
27 ;; Provides a lightweight alternative to emerge/ediff.
28 ;; To use it, simply add to your .emacs the following lines:
30 ;; (autoload 'smerge-mode "smerge-mode" nil t)
32 ;; you can even have it turned on automatically with the following
33 ;; piece of code in your .emacs:
35 ;; (defun sm-try-smerge ()
37 ;; (goto-char (point-min))
38 ;; (when (re-search-forward "^<<<<<<< " nil t)
40 ;; (add-hook 'find-file-hook 'sm-try-smerge t)
44 ;; - if requested, ask the user whether he wants to call ediff right away
48 (eval-when-compile (require 'cl
))
52 "Minor mode to resolve diff3 conflicts."
56 (defcustom smerge-diff-buffer-name
"*vc-diff*"
57 "Buffer name to use for displaying diffs."
62 (const "*smerge-diff*")
65 (defcustom smerge-diff-switches
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."
71 :type
'(repeat string
))
73 (defcustom smerge-auto-leave t
74 "*Non-nil means to leave `smerge-mode' when the last conflict is resolved."
78 (defface smerge-mine-face
79 '((((background light
))
82 (:foreground
"cyan")))
85 (defvar smerge-mine-face
'smerge-mine-face
)
87 (defface smerge-other-face
88 '((((background light
))
89 (:foreground
"darkgreen"))
91 (:foreground
"lightgreen")))
92 "Face for the other code."
94 (defvar smerge-other-face
'smerge-other-face
)
96 (defface smerge-base-face
97 '((((background light
))
100 (:foreground
"orange")))
101 "Face for the base code."
103 (defvar smerge-base-face
'smerge-base-face
)
105 (defface smerge-markers-face
106 '((((background light
))
107 (:background
"grey85"))
109 (:background
"grey30")))
110 "Face for the conflict markers."
112 (defvar smerge-markers-face
'smerge-markers-face
)
114 (easy-mmode-defmap smerge-basic-map
115 `(("n" . smerge-next
)
117 ("r" . smerge-resolve
)
118 ("a" . smerge-keep-all
)
119 ("b" . smerge-keep-base
)
120 ("o" . smerge-keep-other
)
121 ("m" . smerge-keep-mine
)
123 ("\C-m" . smerge-keep-current
)
124 ("=" .
,(make-sparse-keymap "Diff"))
125 ("=<" "base-mine" . smerge-diff-base-mine
)
126 ("=>" "base-other" . smerge-diff-base-other
)
127 ("==" "mine-other" . smerge-diff-mine-other
))
128 "The base keymap for `smerge-mode'.")
130 (defcustom smerge-command-prefix
"\C-c^"
131 "Prefix for `smerge-mode' commands."
133 :type
'(choice (string "\e") (string "\C-c^") (string "") string
))
135 (easy-mmode-defmap smerge-mode-map
136 `((,smerge-command-prefix .
,smerge-basic-map
))
137 "Keymap for `smerge-mode'.")
139 (defvar smerge-check-cache nil
)
140 (make-variable-buffer-local 'smerge-check-cache
)
141 (defun smerge-check (n)
143 (let ((state (cons (point) (buffer-modified-tick))))
144 (unless (equal (cdr smerge-check-cache
) state
)
145 (smerge-match-conflict)
146 (setq smerge-check-cache
(cons (match-data) state
)))
147 (nth (* 2 n
) (car smerge-check-cache
)))
150 (easy-menu-define smerge-mode-menu smerge-mode-map
151 "Menu for `smerge-mode'."
153 ["Next" smerge-next
:help
"Go to next conflict"]
154 ["Previous" smerge-prev
:help
"Go to previous conflict"]
156 ["Keep All" smerge-keep-all
:help
"Keep all three versions"
157 :active
(smerge-check 1)]
158 ["Keep Current" smerge-keep-current
:help
"Use current (at point) version"
159 :active
(and (smerge-check 1) (> (smerge-get-current) 0))]
161 ["Revert to Base" smerge-keep-base
:help
"Revert to base version"
162 :active
(smerge-check 2)]
163 ["Keep Other" smerge-keep-other
:help
"Keep `other' version"
164 :active
(smerge-check 3)]
165 ["Keep Yours" smerge-keep-mine
:help
"Keep your version"
166 :active
(smerge-check 1)]
168 ["Diff Base/Mine" smerge-diff-base-mine
169 :help
"Diff `base' and `mine' for current conflict"
170 :active
(smerge-check 2)]
171 ["Diff Base/Other" smerge-diff-base-other
172 :help
"Diff `base' and `other' for current conflict"
173 :active
(smerge-check 2)]
174 ["Diff Mine/Other" smerge-diff-mine-other
175 :help
"Diff `mine' and `other' for current conflict"
176 :active
(smerge-check 1)]
178 ["Invoke Ediff" smerge-ediff
179 :help
"Use Ediff to resolve the conflicts"
180 :active
(smerge-check 1)]
181 ["Auto Resolve" smerge-resolve
182 :help
"Try auto-resolution heuristics"
183 :active
(smerge-check 1)]
184 ["Combine" smerge-combine-with-next
185 :help
"Combine current conflict with next"
186 :active
(smerge-check 1)]
189 (easy-menu-define smerge-context-menu nil
190 "Context menu for mine area in `smerge-mode'."
192 ["Keep Current" smerge-keep-current
:help
"Use current (at point) version"]
193 ["Kill Current" smerge-kill-current
:help
"Remove current (at point) version"]
194 ["Keep All" smerge-keep-all
:help
"Keep all three versions"]
196 ["More..." (popup-menu smerge-mode-menu
) :help
"Show full SMerge mode menu"]
199 (defconst smerge-font-lock-keywords
200 '((smerge-find-conflict
201 (1 smerge-mine-face prepend t
)
202 (2 smerge-base-face prepend t
)
203 (3 smerge-other-face prepend t
)
204 ;; FIXME: `keep' doesn't work right with syntactic fontification.
205 (0 smerge-markers-face keep
)
208 "Font lock patterns for `smerge-mode'.")
210 (defconst smerge-begin-re
"^<<<<<<< \\(.*\\)\n")
211 (defconst smerge-end-re
"^>>>>>>> .*\n")
212 (defconst smerge-base-re
"^||||||| .*\n")
213 (defconst smerge-other-re
"^=======\n")
215 (defvar smerge-conflict-style nil
216 "Keep track of which style of conflict is in use.
217 Can be nil if the style is undecided, or else:
221 ;; Compiler pacifiers
222 (defvar font-lock-mode
)
223 (defvar font-lock-keywords
)
229 ;; Define smerge-next and smerge-prev
230 (easy-mmode-define-navigation smerge smerge-begin-re
"conflict")
232 (defconst smerge-match-names
["conflict" "mine" "base" "other"])
234 (defun smerge-ensure-match (n)
235 (unless (match-end n
)
236 (error (format "No `%s'" (aref smerge-match-names n
)))))
238 (defun smerge-auto-leave ()
239 (when (and smerge-auto-leave
240 (save-excursion (goto-char (point-min))
241 (not (re-search-forward smerge-begin-re nil t
))))
245 (defun smerge-keep-all ()
246 "Concatenate all versions."
248 (smerge-match-conflict)
249 (let ((mb2 (or (match-beginning 2) (point-max)))
250 (me2 (or (match-end 2) (point-min))))
251 (delete-region (match-end 3) (match-end 0))
252 (delete-region (max me2
(match-end 1)) (match-beginning 3))
253 (if (and (match-end 2) (/= (match-end 1) (match-end 3)))
254 (delete-region (match-end 1) (match-beginning 2)))
255 (delete-region (match-beginning 0) (min (match-beginning 1) mb2
))
256 (smerge-auto-leave)))
258 (defun smerge-keep-n (n)
259 ;; We used to use replace-match, but that did not preserve markers so well.
260 (delete-region (match-end n
) (match-end 0))
261 (delete-region (match-beginning 0) (match-beginning n
)))
263 (defun smerge-combine-with-next ()
264 "Combine the current conflict with the next one."
266 (smerge-match-conflict)
268 (dolist (i '(3 2 1 0))
269 (push (if (match-end i
) (copy-marker (match-end i
) t
)) ends
))
270 (setq ends
(apply 'vector ends
))
271 (goto-char (aref ends
0))
272 (if (not (re-search-forward smerge-begin-re nil t
))
273 (error "No next conflict")
274 (smerge-match-conflict)
275 (let ((match-data (mapcar (lambda (m) (if m
(copy-marker m
)))
277 ;; First copy the in-between text in each alternative.
280 (goto-char (aref ends i
))
281 (insert-buffer-substring (current-buffer)
282 (aref ends
0) (car match-data
))))
283 (delete-region (aref ends
0) (car match-data
))
284 ;; Then move the second conflict's alternatives into the first.
286 (set-match-data match-data
)
287 (when (and (aref ends i
) (match-end i
))
288 (goto-char (aref ends i
))
289 (insert-buffer-substring (current-buffer)
290 (match-beginning i
) (match-end i
))))
291 (delete-region (car match-data
) (cadr match-data
))
293 (dolist (m match-data
) (if m
(move-marker m nil
)))
294 (mapc (lambda (m) (if m
(move-marker m nil
))) ends
)))))
296 (defvar smerge-resolve-function
297 (lambda () (error "Don't know how to resolve"))
298 "Mode-specific merge function.
299 The function is called with no argument and with the match data set
300 according to `smerge-match-conflict'.")
302 (defvar smerge-text-properties
303 `(help-echo "merge conflict: mouse-3 shows a menu"
304 ;; mouse-face highlight
305 keymap
(keymap (down-mouse-3 . smerge-popup-context-menu
))))
307 (defun smerge-remove-props (&optional beg end
)
308 (remove-text-properties
309 (or beg
(match-beginning 0))
310 (or end
(match-end 0))
311 smerge-text-properties
))
313 (defun smerge-popup-context-menu (event)
314 "Pop up the Smerge mode context menu under mouse."
317 (save-excursion (posn-set-point (event-end event
)) (smerge-check 1)))
319 (posn-set-point (event-end event
))
320 (smerge-match-conflict)
321 (let ((i (smerge-get-current))
325 (popup-menu smerge-mode-menu
)
327 (setq o
(make-overlay (match-beginning i
) (match-end i
)))
330 (overlay-put o
'face
'highlight
)
331 (sit-for 0) ;Display the new highlighting.
332 (popup-menu smerge-context-menu
))
334 (delete-overlay o
)))))
335 ;; There's no conflict at point, the text-props are just obsolete.
337 (let ((beg (re-search-backward smerge-end-re nil t
))
338 (end (re-search-forward smerge-begin-re nil t
)))
339 (smerge-remove-props (or beg
(point-min)) (or end
(point-max)))
340 (push event unread-command-events
)))))
342 (defun smerge-resolve ()
343 "Resolve the conflict at point intelligently.
344 This relies on mode-specific knowledge and thus only works in
345 some major modes. Uses `smerge-resolve-function' to do the actual work."
347 (smerge-match-conflict)
348 (smerge-remove-props)
350 ;; Trivial diff3 -A non-conflicts.
351 ((and (eq (match-end 1) (match-end 3))
352 (eq (match-beginning 1) (match-beginning 3)))
353 ;; FIXME: Add "if [ diff -b MINE OTHER ]; then select OTHER; fi"
356 ;; FIXME: Add "diff -b BASE MINE | patch OTHER".
357 ;; FIXME: Add "diff -b BASE OTHER | patch MINE".
360 ((and (not (match-end 2))
361 ;; FIXME: Add "diff -b"-based refinement.
365 ;; Mode-specific conflict resolution.
366 (funcall smerge-resolve-function
)))
369 (defun smerge-keep-base ()
370 "Revert to the base version."
372 (smerge-match-conflict)
373 (smerge-ensure-match 2)
374 (smerge-remove-props)
378 (defun smerge-keep-other ()
379 "Use \"other\" version."
381 (smerge-match-conflict)
382 ;;(smerge-ensure-match 3)
383 (smerge-remove-props)
387 (defun smerge-keep-mine ()
390 (smerge-match-conflict)
391 ;;(smerge-ensure-match 1)
392 (smerge-remove-props)
396 (defun smerge-get-current ()
398 (while (or (not (match-end i
))
399 (< (point) (match-beginning i
))
400 (>= (point) (match-end i
)))
404 (defun smerge-keep-current ()
405 "Use the current (under the cursor) version."
407 (smerge-match-conflict)
408 (let ((i (smerge-get-current)))
409 (if (<= i
0) (error "Not inside a version")
410 (smerge-remove-props)
412 (smerge-auto-leave))))
414 (defun smerge-kill-current ()
415 "Remove the current (under the cursor) version."
417 (smerge-match-conflict)
418 (let ((i (smerge-get-current)))
419 (if (<= i
0) (error "Not inside a version")
420 (smerge-remove-props)
423 (if (and (match-end n
) (/= (match-end n
) (match-end i
)))
426 (/= (match-end (car left
)) (match-end (cadr left
))))
427 (ding) ;We don't know how to do that.
428 (smerge-keep-n (car left
))
429 (smerge-auto-leave))))))
431 (defun smerge-diff-base-mine ()
432 "Diff 'base' and 'mine' version in current conflict region."
436 (defun smerge-diff-base-other ()
437 "Diff 'base' and 'other' version in current conflict region."
441 (defun smerge-diff-mine-other ()
442 "Diff 'mine' and 'other' version in current conflict region."
446 (defun smerge-match-conflict ()
447 "Get info about the conflict. Puts the info in the `match-data'.
448 The submatches contain:
449 0: the whole conflict.
453 An error is raised if not inside a conflict."
456 (let* ((orig-point (point))
459 (_ (re-search-backward smerge-begin-re
))
461 (start (match-beginning 0))
462 (mine-start (match-end 0))
463 (filename (or (match-string 1) ""))
465 (_ (re-search-forward smerge-end-re
))
466 (_ (assert (< orig-point
(match-end 0))))
468 (other-end (match-beginning 0))
471 (_ (re-search-backward smerge-other-re start
))
473 (mine-end (match-beginning 0))
474 (other-start (match-end 0))
478 ;; handle the various conflict styles
481 (goto-char mine-start
)
482 (re-search-forward smerge-begin-re end t
))
483 ;; There's a nested conflict and we're after the the beginning
484 ;; of the outer one but before the beginning of the inner one.
485 (error "There is a nested conflict"))
487 ((re-search-backward smerge-base-re start t
)
488 ;; a 3-parts conflict
489 (set (make-local-variable 'smerge-conflict-style
) 'diff3-A
)
490 (setq base-end mine-end
)
491 (setq mine-end
(match-beginning 0))
492 (setq base-start
(match-end 0)))
494 ((string= filename
(file-name-nondirectory
495 (or buffer-file-name
"")))
496 ;; a 2-parts conflict
497 (set (make-local-variable 'smerge-conflict-style
) 'diff3-E
))
499 ((and (not base-start
)
500 (or (eq smerge-conflict-style
'diff3-A
)
501 (equal filename
"ANCESTOR")
502 (string-match "\\`[.0-9]+\\'" filename
)))
503 ;; a same-diff conflict
504 (setq base-start mine-start
)
505 (setq base-end mine-end
)
506 (setq mine-start other-start
)
507 (setq mine-end other-end
)))
509 (let ((inhibit-read-only t
)
510 (inhibit-modification-hooks t
)
511 (m (buffer-modified-p)))
513 (add-text-properties start end smerge-text-properties
)
514 (restore-buffer-modified-p m
)))
516 (store-match-data (list start end
519 other-start other-end
520 (when base-start
(1- base-start
)) base-start
521 (1- other-start
) other-start
))
523 (search-failed (error "Point not in conflict region")))))
525 (defun smerge-find-conflict (&optional limit
)
526 "Find and match a conflict region. Intended as a font-lock MATCHER.
527 The submatches are the same as in `smerge-match-conflict'.
528 Returns non-nil if a match is found between the point and LIMIT.
529 The point is moved to the end of the conflict."
530 (when (re-search-forward smerge-begin-re limit t
)
533 (smerge-match-conflict)
534 (goto-char (match-end 0)))
535 (error (smerge-find-conflict limit
)))))
537 (defun smerge-diff (n1 n2
)
538 (smerge-match-conflict)
539 (smerge-ensure-match n1
)
540 (smerge-ensure-match n2
)
541 (let ((name1 (aref smerge-match-names n1
))
542 (name2 (aref smerge-match-names n2
))
543 ;; Read them before the match-data gets clobbered.
544 (beg1 (match-beginning n1
))
545 (end1 (match-end n1
))
546 (beg2 (match-beginning n2
))
547 (end2 (match-end n2
))
548 (file1 (make-temp-file "smerge1"))
549 (file2 (make-temp-file "smerge2"))
550 (dir default-directory
)
551 (file (file-relative-name buffer-file-name
))
552 (coding-system-for-read buffer-file-coding-system
))
553 (write-region beg1 end1 file1 nil
'nomessage
)
554 (write-region beg2 end2 file2 nil
'nomessage
)
556 (with-current-buffer (get-buffer-create smerge-diff-buffer-name
)
557 (setq default-directory dir
)
558 (let ((inhibit-read-only t
))
561 (apply 'call-process diff-command nil t nil
562 (append smerge-diff-switches
563 (list "-L" (concat name1
"/" file
)
564 "-L" (concat name2
"/" file
)
566 (if (eq status
0) (insert "No differences found.\n"))))
567 (goto-char (point-min))
569 (display-buffer (current-buffer) t
))
571 (delete-file file2
))))
573 ;; compiler pacifiers
574 (defvar smerge-ediff-windows
)
575 (defvar smerge-ediff-buf
)
576 (defvar ediff-buffer-A
)
577 (defvar ediff-buffer-B
)
578 (defvar ediff-buffer-C
)
581 (defun smerge-ediff (&optional name-mine name-other name-base
)
582 "Invoke ediff to resolve the conflicts.
583 NAME-MINE, NAME-OTHER, and NAME-BASE, if non-nil, are used for the
586 (let* ((buf (current-buffer))
588 ;;(ediff-default-variant 'default-B)
589 (config (current-window-configuration))
590 (filename (file-name-nondirectory buffer-file-name
))
591 (mine (generate-new-buffer
592 (or name-mine
(concat "*" filename
" MINE*"))))
593 (other (generate-new-buffer
594 (or name-other
(concat "*" filename
" OTHER*"))))
596 (with-current-buffer mine
597 (buffer-disable-undo)
598 (insert-buffer-substring buf
)
599 (goto-char (point-min))
600 (while (smerge-find-conflict)
601 (when (match-beginning 2) (setq base t
))
604 (set-buffer-modified-p nil
)
607 (with-current-buffer other
608 (buffer-disable-undo)
609 (insert-buffer-substring buf
)
610 (goto-char (point-min))
611 (while (smerge-find-conflict)
614 (set-buffer-modified-p nil
)
618 (setq base
(generate-new-buffer
619 (or name-base
(concat "*" filename
" BASE*"))))
620 (with-current-buffer base
621 (buffer-disable-undo)
622 (insert-buffer-substring buf
)
623 (goto-char (point-min))
624 (while (smerge-find-conflict)
627 (delete-region (match-beginning 0) (match-end 0))))
629 (set-buffer-modified-p nil
)
632 ;; the rest of the code is inspired from vc.el
636 (ediff-merge-buffers-with-ancestor mine other base
)
637 ;; nil 'ediff-merge-revisions-with-ancestor buffer-file-name)
638 (ediff-merge-buffers mine other
)))
639 ;; nil 'ediff-merge-revisions buffer-file-name)))
641 ;; Ediff is now set up, and we are in the control buffer.
642 ;; Do a few further adjustments and take precautions for exit.
643 (set (make-local-variable 'smerge-ediff-windows
) config
)
644 (set (make-local-variable 'smerge-ediff-buf
) buf
)
645 (set (make-local-variable 'ediff-quit-hook
)
647 (let ((buffer-A ediff-buffer-A
)
648 (buffer-B ediff-buffer-B
)
649 (buffer-C ediff-buffer-C
)
650 (buffer-Ancestor ediff-ancestor-buffer
)
651 (buf smerge-ediff-buf
)
652 (windows smerge-ediff-windows
))
654 (with-current-buffer buf
656 (insert-buffer buffer-C
)
657 (kill-buffer buffer-A
)
658 (kill-buffer buffer-B
)
659 (kill-buffer buffer-C
)
660 (when (bufferp buffer-Ancestor
) (kill-buffer buffer-Ancestor
))
661 (set-window-configuration windows
)
662 (message "Conflict resolution finished; you may save the buffer")))))
663 (message "Please resolve conflicts now; exit ediff when done")))
667 (define-minor-mode smerge-mode
668 "Minor mode to simplify editing output from the diff3 program.
671 (when (and (boundp 'font-lock-mode
) font-lock-mode
)
672 (set (make-local-variable 'font-lock-multiline
) t
)
675 (font-lock-add-keywords nil smerge-font-lock-keywords
'append
)
676 (font-lock-remove-keywords nil smerge-font-lock-keywords
))
677 (goto-char (point-min))
678 (while (smerge-find-conflict)
680 (font-lock-fontify-region (match-beginning 0) (match-end 0) nil
))))))
683 (provide 'smerge-mode
)
685 ;; arch-tag: 605c8d1e-e43d-4943-a6f3-1bcc4333e690
686 ;;; smerge-mode.el ends here