1 ;;; smerge-mode.el --- Minor mode to resolve diff3 conflicts
3 ;; Copyright (C) 1999, 2000, 2001, 2003, 2004, 2005 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
))
51 ;;; The real definition comes later.
55 "Minor mode to resolve diff3 conflicts."
59 (defcustom smerge-diff-buffer-name
"*vc-diff*"
60 "Buffer name to use for displaying diffs."
65 (const "*smerge-diff*")
68 (defcustom smerge-diff-switches
70 (if (listp diff-switches
) diff-switches
(list diff-switches
)))
71 "*A list of strings specifying switches to be passed to diff.
72 Used in `smerge-diff-base-mine' and related functions."
74 :type
'(repeat string
))
76 (defcustom smerge-auto-leave t
77 "*Non-nil means to leave `smerge-mode' when the last conflict is resolved."
82 '((((min-colors 88) (background light
))
83 (:foreground
"blue1"))
86 (((min-colors 88) (background dark
))
87 (:foreground
"cyan1"))
89 (:foreground
"cyan")))
92 ;; backward-compatibility alias
93 (put 'smerge-mine-face
'face-alias
'smerge-mine
)
94 (defvar smerge-mine-face
'smerge-mine
)
97 '((((background light
))
98 (:foreground
"darkgreen"))
100 (:foreground
"lightgreen")))
101 "Face for the other code."
103 ;; backward-compatibility alias
104 (put 'smerge-other-face
'face-alias
'smerge-other
)
105 (defvar smerge-other-face
'smerge-other
)
108 '((((min-colors 88) (background light
))
109 (:foreground
"red1"))
110 (((background light
))
113 (:foreground
"orange")))
114 "Face for the base code."
116 ;; backward-compatibility alias
117 (put 'smerge-base-face
'face-alias
'smerge-base
)
118 (defvar smerge-base-face
'smerge-base
)
120 (defface smerge-markers
121 '((((background light
))
122 (:background
"grey85"))
124 (:background
"grey30")))
125 "Face for the conflict markers."
127 ;; backward-compatibility alias
128 (put 'smerge-markers-face
'face-alias
'smerge-markers
)
129 (defvar smerge-markers-face
'smerge-markers
)
131 (easy-mmode-defmap smerge-basic-map
132 `(("n" . smerge-next
)
134 ("r" . smerge-resolve
)
135 ("a" . smerge-keep-all
)
136 ("b" . smerge-keep-base
)
137 ("o" . smerge-keep-other
)
138 ("m" . smerge-keep-mine
)
140 ("\C-m" . smerge-keep-current
)
141 ("=" .
,(make-sparse-keymap "Diff"))
142 ("=<" "base-mine" . smerge-diff-base-mine
)
143 ("=>" "base-other" . smerge-diff-base-other
)
144 ("==" "mine-other" . smerge-diff-mine-other
))
145 "The base keymap for `smerge-mode'.")
147 (defcustom smerge-command-prefix
"\C-c^"
148 "Prefix for `smerge-mode' commands."
150 :type
'(choice (string "\e") (string "\C-c^") (string "") string
))
152 (easy-mmode-defmap smerge-mode-map
153 `((,smerge-command-prefix .
,smerge-basic-map
))
154 "Keymap for `smerge-mode'.")
156 (defvar smerge-check-cache nil
)
157 (make-variable-buffer-local 'smerge-check-cache
)
158 (defun smerge-check (n)
160 (let ((state (cons (point) (buffer-modified-tick))))
161 (unless (equal (cdr smerge-check-cache
) state
)
162 (smerge-match-conflict)
163 (setq smerge-check-cache
(cons (match-data) state
)))
164 (nth (* 2 n
) (car smerge-check-cache
)))
167 (easy-menu-define smerge-mode-menu smerge-mode-map
168 "Menu for `smerge-mode'."
170 ["Next" smerge-next
:help
"Go to next conflict"]
171 ["Previous" smerge-prev
:help
"Go to previous conflict"]
173 ["Keep All" smerge-keep-all
:help
"Keep all three versions"
174 :active
(smerge-check 1)]
175 ["Keep Current" smerge-keep-current
:help
"Use current (at point) version"
176 :active
(and (smerge-check 1) (> (smerge-get-current) 0))]
178 ["Revert to Base" smerge-keep-base
:help
"Revert to base version"
179 :active
(smerge-check 2)]
180 ["Keep Other" smerge-keep-other
:help
"Keep `other' version"
181 :active
(smerge-check 3)]
182 ["Keep Yours" smerge-keep-mine
:help
"Keep your version"
183 :active
(smerge-check 1)]
185 ["Diff Base/Mine" smerge-diff-base-mine
186 :help
"Diff `base' and `mine' for current conflict"
187 :active
(smerge-check 2)]
188 ["Diff Base/Other" smerge-diff-base-other
189 :help
"Diff `base' and `other' for current conflict"
190 :active
(smerge-check 2)]
191 ["Diff Mine/Other" smerge-diff-mine-other
192 :help
"Diff `mine' and `other' for current conflict"
193 :active
(smerge-check 1)]
195 ["Invoke Ediff" smerge-ediff
196 :help
"Use Ediff to resolve the conflicts"
197 :active
(smerge-check 1)]
198 ["Auto Resolve" smerge-resolve
199 :help
"Try auto-resolution heuristics"
200 :active
(smerge-check 1)]
201 ["Combine" smerge-combine-with-next
202 :help
"Combine current conflict with next"
203 :active
(smerge-check 1)]
206 (easy-menu-define smerge-context-menu nil
207 "Context menu for mine area in `smerge-mode'."
209 ["Keep Current" smerge-keep-current
:help
"Use current (at point) version"]
210 ["Kill Current" smerge-kill-current
:help
"Remove current (at point) version"]
211 ["Keep All" smerge-keep-all
:help
"Keep all three versions"]
213 ["More..." (popup-menu smerge-mode-menu
) :help
"Show full SMerge mode menu"]
216 (defconst smerge-font-lock-keywords
217 '((smerge-find-conflict
218 (1 smerge-mine-face prepend t
)
219 (2 smerge-base-face prepend t
)
220 (3 smerge-other-face prepend t
)
221 ;; FIXME: `keep' doesn't work right with syntactic fontification.
222 (0 smerge-markers-face keep
)
225 "Font lock patterns for `smerge-mode'.")
227 (defconst smerge-begin-re
"^<<<<<<< \\(.*\\)\n")
228 (defconst smerge-end-re
"^>>>>>>> .*\n")
229 (defconst smerge-base-re
"^||||||| .*\n")
230 (defconst smerge-other-re
"^=======\n")
232 (defvar smerge-conflict-style nil
233 "Keep track of which style of conflict is in use.
234 Can be nil if the style is undecided, or else:
238 ;; Compiler pacifiers
239 (defvar font-lock-mode
)
240 (defvar font-lock-keywords
)
246 ;; Define smerge-next and smerge-prev
247 (easy-mmode-define-navigation smerge smerge-begin-re
"conflict")
249 (defconst smerge-match-names
["conflict" "mine" "base" "other"])
251 (defun smerge-ensure-match (n)
252 (unless (match-end n
)
253 (error "No `%s'" (aref smerge-match-names n
))))
255 (defun smerge-auto-leave ()
256 (when (and smerge-auto-leave
257 (save-excursion (goto-char (point-min))
258 (not (re-search-forward smerge-begin-re nil t
))))
262 (defun smerge-keep-all ()
263 "Concatenate all versions."
265 (smerge-match-conflict)
266 (let ((mb2 (or (match-beginning 2) (point-max)))
267 (me2 (or (match-end 2) (point-min))))
268 (delete-region (match-end 3) (match-end 0))
269 (delete-region (max me2
(match-end 1)) (match-beginning 3))
270 (if (and (match-end 2) (/= (match-end 1) (match-end 3)))
271 (delete-region (match-end 1) (match-beginning 2)))
272 (delete-region (match-beginning 0) (min (match-beginning 1) mb2
))
273 (smerge-auto-leave)))
275 (defun smerge-keep-n (n)
276 ;; We used to use replace-match, but that did not preserve markers so well.
277 (delete-region (match-end n
) (match-end 0))
278 (delete-region (match-beginning 0) (match-beginning n
)))
280 (defun smerge-combine-with-next ()
281 "Combine the current conflict with the next one."
283 (smerge-match-conflict)
285 (dolist (i '(3 2 1 0))
286 (push (if (match-end i
) (copy-marker (match-end i
) t
)) ends
))
287 (setq ends
(apply 'vector ends
))
288 (goto-char (aref ends
0))
289 (if (not (re-search-forward smerge-begin-re nil t
))
290 (error "No next conflict")
291 (smerge-match-conflict)
292 (let ((match-data (mapcar (lambda (m) (if m
(copy-marker m
)))
294 ;; First copy the in-between text in each alternative.
297 (goto-char (aref ends i
))
298 (insert-buffer-substring (current-buffer)
299 (aref ends
0) (car match-data
))))
300 (delete-region (aref ends
0) (car match-data
))
301 ;; Then move the second conflict's alternatives into the first.
303 (set-match-data match-data
)
304 (when (and (aref ends i
) (match-end i
))
305 (goto-char (aref ends i
))
306 (insert-buffer-substring (current-buffer)
307 (match-beginning i
) (match-end i
))))
308 (delete-region (car match-data
) (cadr match-data
))
310 (dolist (m match-data
) (if m
(move-marker m nil
)))
311 (mapc (lambda (m) (if m
(move-marker m nil
))) ends
)))))
313 (defvar smerge-resolve-function
314 (lambda () (error "Don't know how to resolve"))
315 "Mode-specific merge function.
316 The function is called with no argument and with the match data set
317 according to `smerge-match-conflict'.")
319 (defvar smerge-text-properties
320 `(help-echo "merge conflict: mouse-3 shows a menu"
321 ;; mouse-face highlight
322 keymap
(keymap (down-mouse-3 . smerge-popup-context-menu
))))
324 (defun smerge-remove-props (&optional beg end
)
325 (remove-text-properties
326 (or beg
(match-beginning 0))
327 (or end
(match-end 0))
328 smerge-text-properties
))
330 (defun smerge-popup-context-menu (event)
331 "Pop up the Smerge mode context menu under mouse."
334 (save-excursion (posn-set-point (event-end event
)) (smerge-check 1)))
336 (posn-set-point (event-end event
))
337 (smerge-match-conflict)
338 (let ((i (smerge-get-current))
342 (popup-menu smerge-mode-menu
)
344 (setq o
(make-overlay (match-beginning i
) (match-end i
)))
347 (overlay-put o
'face
'highlight
)
348 (sit-for 0) ;Display the new highlighting.
349 (popup-menu smerge-context-menu
))
351 (delete-overlay o
)))))
352 ;; There's no conflict at point, the text-props are just obsolete.
354 (let ((beg (re-search-backward smerge-end-re nil t
))
355 (end (re-search-forward smerge-begin-re nil t
)))
356 (smerge-remove-props (or beg
(point-min)) (or end
(point-max)))
357 (push event unread-command-events
)))))
359 (defun smerge-resolve ()
360 "Resolve the conflict at point intelligently.
361 This relies on mode-specific knowledge and thus only works in
362 some major modes. Uses `smerge-resolve-function' to do the actual work."
364 (smerge-match-conflict)
365 (smerge-remove-props)
367 ;; Trivial diff3 -A non-conflicts.
368 ((and (eq (match-end 1) (match-end 3))
369 (eq (match-beginning 1) (match-beginning 3)))
370 ;; FIXME: Add "if [ diff -b MINE OTHER ]; then select OTHER; fi"
373 ;; FIXME: Add "diff -b BASE MINE | patch OTHER".
374 ;; FIXME: Add "diff -b BASE OTHER | patch MINE".
377 ((and (not (match-end 2))
378 ;; FIXME: Add "diff -b"-based refinement.
382 ;; Mode-specific conflict resolution.
383 (funcall smerge-resolve-function
)))
386 (defun smerge-keep-base ()
387 "Revert to the base version."
389 (smerge-match-conflict)
390 (smerge-ensure-match 2)
391 (smerge-remove-props)
395 (defun smerge-keep-other ()
396 "Use \"other\" version."
398 (smerge-match-conflict)
399 ;;(smerge-ensure-match 3)
400 (smerge-remove-props)
404 (defun smerge-keep-mine ()
407 (smerge-match-conflict)
408 ;;(smerge-ensure-match 1)
409 (smerge-remove-props)
413 (defun smerge-get-current ()
415 (while (or (not (match-end i
))
416 (< (point) (match-beginning i
))
417 (>= (point) (match-end i
)))
421 (defun smerge-keep-current ()
422 "Use the current (under the cursor) version."
424 (smerge-match-conflict)
425 (let ((i (smerge-get-current)))
426 (if (<= i
0) (error "Not inside a version")
427 (smerge-remove-props)
429 (smerge-auto-leave))))
431 (defun smerge-kill-current ()
432 "Remove the current (under the cursor) version."
434 (smerge-match-conflict)
435 (let ((i (smerge-get-current)))
436 (if (<= i
0) (error "Not inside a version")
437 (smerge-remove-props)
440 (if (and (match-end n
) (/= (match-end n
) (match-end i
)))
443 (/= (match-end (car left
)) (match-end (cadr left
))))
444 (ding) ;We don't know how to do that.
445 (smerge-keep-n (car left
))
446 (smerge-auto-leave))))))
448 (defun smerge-diff-base-mine ()
449 "Diff 'base' and 'mine' version in current conflict region."
453 (defun smerge-diff-base-other ()
454 "Diff 'base' and 'other' version in current conflict region."
458 (defun smerge-diff-mine-other ()
459 "Diff 'mine' and 'other' version in current conflict region."
463 (defun smerge-match-conflict ()
464 "Get info about the conflict. Puts the info in the `match-data'.
465 The submatches contain:
466 0: the whole conflict.
470 An error is raised if not inside a conflict."
473 (let* ((orig-point (point))
476 (_ (re-search-backward smerge-begin-re
))
478 (start (match-beginning 0))
479 (mine-start (match-end 0))
480 (filename (or (match-string 1) ""))
482 (_ (re-search-forward smerge-end-re
))
483 (_ (assert (< orig-point
(match-end 0))))
485 (other-end (match-beginning 0))
488 (_ (re-search-backward smerge-other-re start
))
490 (mine-end (match-beginning 0))
491 (other-start (match-end 0))
495 ;; handle the various conflict styles
498 (goto-char mine-start
)
499 (re-search-forward smerge-begin-re end t
))
500 ;; There's a nested conflict and we're after the the beginning
501 ;; of the outer one but before the beginning of the inner one.
502 (error "There is a nested conflict"))
504 ((re-search-backward smerge-base-re start t
)
505 ;; a 3-parts conflict
506 (set (make-local-variable 'smerge-conflict-style
) 'diff3-A
)
507 (setq base-end mine-end
)
508 (setq mine-end
(match-beginning 0))
509 (setq base-start
(match-end 0)))
511 ((string= filename
(file-name-nondirectory
512 (or buffer-file-name
"")))
513 ;; a 2-parts conflict
514 (set (make-local-variable 'smerge-conflict-style
) 'diff3-E
))
516 ((and (not base-start
)
517 (or (eq smerge-conflict-style
'diff3-A
)
518 (equal filename
"ANCESTOR")
519 (string-match "\\`[.0-9]+\\'" filename
)))
520 ;; a same-diff conflict
521 (setq base-start mine-start
)
522 (setq base-end mine-end
)
523 (setq mine-start other-start
)
524 (setq mine-end other-end
)))
526 (let ((inhibit-read-only t
)
527 (inhibit-modification-hooks t
)
528 (m (buffer-modified-p)))
530 (add-text-properties start end smerge-text-properties
)
531 (restore-buffer-modified-p m
)))
533 (store-match-data (list start end
536 other-start other-end
537 (when base-start
(1- base-start
)) base-start
538 (1- other-start
) other-start
))
540 (search-failed (error "Point not in conflict region")))))
542 (defun smerge-find-conflict (&optional limit
)
543 "Find and match a conflict region. Intended as a font-lock MATCHER.
544 The submatches are the same as in `smerge-match-conflict'.
545 Returns non-nil if a match is found between the point and LIMIT.
546 The point is moved to the end of the conflict."
547 (when (re-search-forward smerge-begin-re limit t
)
550 (smerge-match-conflict)
551 (goto-char (match-end 0)))
552 (error (smerge-find-conflict limit
)))))
554 (defun smerge-diff (n1 n2
)
555 (smerge-match-conflict)
556 (smerge-ensure-match n1
)
557 (smerge-ensure-match n2
)
558 (let ((name1 (aref smerge-match-names n1
))
559 (name2 (aref smerge-match-names n2
))
560 ;; Read them before the match-data gets clobbered.
561 (beg1 (match-beginning n1
))
562 (end1 (match-end n1
))
563 (beg2 (match-beginning n2
))
564 (end2 (match-end n2
))
565 (file1 (make-temp-file "smerge1"))
566 (file2 (make-temp-file "smerge2"))
567 (dir default-directory
)
568 (file (file-relative-name buffer-file-name
))
569 (coding-system-for-read buffer-file-coding-system
))
570 (write-region beg1 end1 file1 nil
'nomessage
)
571 (write-region beg2 end2 file2 nil
'nomessage
)
573 (with-current-buffer (get-buffer-create smerge-diff-buffer-name
)
574 (setq default-directory dir
)
575 (let ((inhibit-read-only t
))
578 (apply 'call-process diff-command nil t nil
579 (append smerge-diff-switches
580 (list "-L" (concat name1
"/" file
)
581 "-L" (concat name2
"/" file
)
583 (if (eq status
0) (insert "No differences found.\n"))))
584 (goto-char (point-min))
586 (display-buffer (current-buffer) t
))
588 (delete-file file2
))))
590 ;; compiler pacifiers
591 (defvar smerge-ediff-windows
)
592 (defvar smerge-ediff-buf
)
593 (defvar ediff-buffer-A
)
594 (defvar ediff-buffer-B
)
595 (defvar ediff-buffer-C
)
598 (defun smerge-ediff (&optional name-mine name-other name-base
)
599 "Invoke ediff to resolve the conflicts.
600 NAME-MINE, NAME-OTHER, and NAME-BASE, if non-nil, are used for the
603 (let* ((buf (current-buffer))
605 ;;(ediff-default-variant 'default-B)
606 (config (current-window-configuration))
607 (filename (file-name-nondirectory buffer-file-name
))
608 (mine (generate-new-buffer
609 (or name-mine
(concat "*" filename
" MINE*"))))
610 (other (generate-new-buffer
611 (or name-other
(concat "*" filename
" OTHER*"))))
613 (with-current-buffer mine
614 (buffer-disable-undo)
615 (insert-buffer-substring buf
)
616 (goto-char (point-min))
617 (while (smerge-find-conflict)
618 (when (match-beginning 2) (setq base t
))
621 (set-buffer-modified-p nil
)
624 (with-current-buffer other
625 (buffer-disable-undo)
626 (insert-buffer-substring buf
)
627 (goto-char (point-min))
628 (while (smerge-find-conflict)
631 (set-buffer-modified-p nil
)
635 (setq base
(generate-new-buffer
636 (or name-base
(concat "*" filename
" BASE*"))))
637 (with-current-buffer base
638 (buffer-disable-undo)
639 (insert-buffer-substring buf
)
640 (goto-char (point-min))
641 (while (smerge-find-conflict)
644 (delete-region (match-beginning 0) (match-end 0))))
646 (set-buffer-modified-p nil
)
649 ;; the rest of the code is inspired from vc.el
653 (ediff-merge-buffers-with-ancestor mine other base
)
654 ;; nil 'ediff-merge-revisions-with-ancestor buffer-file-name)
655 (ediff-merge-buffers mine other
)))
656 ;; nil 'ediff-merge-revisions buffer-file-name)))
658 ;; Ediff is now set up, and we are in the control buffer.
659 ;; Do a few further adjustments and take precautions for exit.
660 (set (make-local-variable 'smerge-ediff-windows
) config
)
661 (set (make-local-variable 'smerge-ediff-buf
) buf
)
662 (set (make-local-variable 'ediff-quit-hook
)
664 (let ((buffer-A ediff-buffer-A
)
665 (buffer-B ediff-buffer-B
)
666 (buffer-C ediff-buffer-C
)
667 (buffer-Ancestor ediff-ancestor-buffer
)
668 (buf smerge-ediff-buf
)
669 (windows smerge-ediff-windows
))
671 (with-current-buffer buf
673 (insert-buffer buffer-C
)
674 (kill-buffer buffer-A
)
675 (kill-buffer buffer-B
)
676 (kill-buffer buffer-C
)
677 (when (bufferp buffer-Ancestor
) (kill-buffer buffer-Ancestor
))
678 (set-window-configuration windows
)
679 (message "Conflict resolution finished; you may save the buffer")))))
680 (message "Please resolve conflicts now; exit ediff when done")))
684 (define-minor-mode smerge-mode
685 "Minor mode to simplify editing output from the diff3 program.
687 :group
'smerge
:lighter
" SMerge"
688 (when (and (boundp 'font-lock-mode
) font-lock-mode
)
689 (set (make-local-variable 'font-lock-multiline
) t
)
692 (font-lock-add-keywords nil smerge-font-lock-keywords
'append
)
693 (font-lock-remove-keywords nil smerge-font-lock-keywords
))
694 (goto-char (point-min))
695 (while (smerge-find-conflict)
697 (font-lock-fontify-region (match-beginning 0) (match-end 0) nil
))))))
700 (provide 'smerge-mode
)
702 ;; arch-tag: 605c8d1e-e43d-4943-a6f3-1bcc4333e690
703 ;;; smerge-mode.el ends here