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
))
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."
79 '((((min-colors 88) (background light
))
80 (:foreground
"blue1"))
83 (((min-colors 88) (background dark
))
84 (:foreground
"cyan1"))
86 (:foreground
"cyan")))
89 ;; backward-compatibility alias
90 (put 'smerge-mine-face
'face-alias
'smerge-mine
)
91 (defvar smerge-mine-face
'smerge-mine
)
94 '((((background light
))
95 (:foreground
"darkgreen"))
97 (:foreground
"lightgreen")))
98 "Face for the other code."
100 ;; backward-compatibility alias
101 (put 'smerge-other-face
'face-alias
'smerge-other
)
102 (defvar smerge-other-face
'smerge-other
)
105 '((((min-colors 88) (background light
))
106 (:foreground
"red1"))
107 (((background light
))
110 (:foreground
"orange")))
111 "Face for the base code."
113 ;; backward-compatibility alias
114 (put 'smerge-base-face
'face-alias
'smerge-base
)
115 (defvar smerge-base-face
'smerge-base
)
117 (defface smerge-markers
118 '((((background light
))
119 (:background
"grey85"))
121 (:background
"grey30")))
122 "Face for the conflict markers."
124 ;; backward-compatibility alias
125 (put 'smerge-markers-face
'face-alias
'smerge-markers
)
126 (defvar smerge-markers-face
'smerge-markers
)
128 (easy-mmode-defmap smerge-basic-map
129 `(("n" . smerge-next
)
131 ("r" . smerge-resolve
)
132 ("a" . smerge-keep-all
)
133 ("b" . smerge-keep-base
)
134 ("o" . smerge-keep-other
)
135 ("m" . smerge-keep-mine
)
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'.")
144 (defcustom smerge-command-prefix
"\C-c^"
145 "Prefix for `smerge-mode' commands."
147 :type
'(choice (string "\e") (string "\C-c^") (string "") string
))
149 (easy-mmode-defmap smerge-mode-map
150 `((,smerge-command-prefix .
,smerge-basic-map
))
151 "Keymap for `smerge-mode'.")
153 (defvar smerge-check-cache nil
)
154 (make-variable-buffer-local 'smerge-check-cache
)
155 (defun smerge-check (n)
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
)))
164 (easy-menu-define smerge-mode-menu smerge-mode-map
165 "Menu for `smerge-mode'."
167 ["Next" smerge-next
:help
"Go to next conflict"]
168 ["Previous" smerge-prev
:help
"Go to previous conflict"]
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))]
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)]
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)]
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)]
203 (easy-menu-define smerge-context-menu nil
204 "Context menu for mine area in `smerge-mode'."
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"]
210 ["More..." (popup-menu smerge-mode-menu
) :help
"Show full SMerge mode menu"]
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
)
222 "Font lock patterns for `smerge-mode'.")
224 (defconst smerge-begin-re
"^<<<<<<< \\(.*\\)\n")
225 (defconst smerge-end-re
"^>>>>>>> .*\n")
226 (defconst smerge-base-re
"^||||||| .*\n")
227 (defconst smerge-other-re
"^=======\n")
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:
235 ;; Compiler pacifiers
236 (defvar font-lock-mode
)
237 (defvar font-lock-keywords
)
243 ;; Define smerge-next and smerge-prev
244 (easy-mmode-define-navigation smerge smerge-begin-re
"conflict")
246 (defconst smerge-match-names
["conflict" "mine" "base" "other"])
248 (defun smerge-ensure-match (n)
249 (unless (match-end n
)
250 (error (format "No `%s'" (aref smerge-match-names n
)))))
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
))))
259 (defun smerge-keep-all ()
260 "Concatenate all versions."
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)))
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
)))
277 (defun smerge-combine-with-next ()
278 "Combine the current conflict with the next one."
280 (smerge-match-conflict)
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
)))
291 ;; First copy the in-between text in each alternative.
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.
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
))
307 (dolist (m match-data
) (if m
(move-marker m nil
)))
308 (mapc (lambda (m) (if m
(move-marker m nil
))) ends
)))))
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'.")
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
))))
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
))
327 (defun smerge-popup-context-menu (event)
328 "Pop up the Smerge mode context menu under mouse."
331 (save-excursion (posn-set-point (event-end event
)) (smerge-check 1)))
333 (posn-set-point (event-end event
))
334 (smerge-match-conflict)
335 (let ((i (smerge-get-current))
339 (popup-menu smerge-mode-menu
)
341 (setq o
(make-overlay (match-beginning i
) (match-end i
)))
344 (overlay-put o
'face
'highlight
)
345 (sit-for 0) ;Display the new highlighting.
346 (popup-menu smerge-context-menu
))
348 (delete-overlay o
)))))
349 ;; There's no conflict at point, the text-props are just obsolete.
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
)))))
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."
361 (smerge-match-conflict)
362 (smerge-remove-props)
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"
370 ;; FIXME: Add "diff -b BASE MINE | patch OTHER".
371 ;; FIXME: Add "diff -b BASE OTHER | patch MINE".
374 ((and (not (match-end 2))
375 ;; FIXME: Add "diff -b"-based refinement.
379 ;; Mode-specific conflict resolution.
380 (funcall smerge-resolve-function
)))
383 (defun smerge-keep-base ()
384 "Revert to the base version."
386 (smerge-match-conflict)
387 (smerge-ensure-match 2)
388 (smerge-remove-props)
392 (defun smerge-keep-other ()
393 "Use \"other\" version."
395 (smerge-match-conflict)
396 ;;(smerge-ensure-match 3)
397 (smerge-remove-props)
401 (defun smerge-keep-mine ()
404 (smerge-match-conflict)
405 ;;(smerge-ensure-match 1)
406 (smerge-remove-props)
410 (defun smerge-get-current ()
412 (while (or (not (match-end i
))
413 (< (point) (match-beginning i
))
414 (>= (point) (match-end i
)))
418 (defun smerge-keep-current ()
419 "Use the current (under the cursor) version."
421 (smerge-match-conflict)
422 (let ((i (smerge-get-current)))
423 (if (<= i
0) (error "Not inside a version")
424 (smerge-remove-props)
426 (smerge-auto-leave))))
428 (defun smerge-kill-current ()
429 "Remove the current (under the cursor) version."
431 (smerge-match-conflict)
432 (let ((i (smerge-get-current)))
433 (if (<= i
0) (error "Not inside a version")
434 (smerge-remove-props)
437 (if (and (match-end n
) (/= (match-end n
) (match-end i
)))
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))))))
445 (defun smerge-diff-base-mine ()
446 "Diff 'base' and 'mine' version in current conflict region."
450 (defun smerge-diff-base-other ()
451 "Diff 'base' and 'other' version in current conflict region."
455 (defun smerge-diff-mine-other ()
456 "Diff 'mine' and 'other' version in current conflict region."
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.
467 An error is raised if not inside a conflict."
470 (let* ((orig-point (point))
473 (_ (re-search-backward smerge-begin-re
))
475 (start (match-beginning 0))
476 (mine-start (match-end 0))
477 (filename (or (match-string 1) ""))
479 (_ (re-search-forward smerge-end-re
))
480 (_ (assert (< orig-point
(match-end 0))))
482 (other-end (match-beginning 0))
485 (_ (re-search-backward smerge-other-re start
))
487 (mine-end (match-beginning 0))
488 (other-start (match-end 0))
492 ;; handle the various conflict styles
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"))
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)))
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
))
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
)))
523 (let ((inhibit-read-only t
)
524 (inhibit-modification-hooks t
)
525 (m (buffer-modified-p)))
527 (add-text-properties start end smerge-text-properties
)
528 (restore-buffer-modified-p m
)))
530 (store-match-data (list start end
533 other-start other-end
534 (when base-start
(1- base-start
)) base-start
535 (1- other-start
) other-start
))
537 (search-failed (error "Point not in conflict region")))))
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
)
547 (smerge-match-conflict)
548 (goto-char (match-end 0)))
549 (error (smerge-find-conflict limit
)))))
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
)
570 (with-current-buffer (get-buffer-create smerge-diff-buffer-name
)
571 (setq default-directory dir
)
572 (let ((inhibit-read-only t
))
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
)
580 (if (eq status
0) (insert "No differences found.\n"))))
581 (goto-char (point-min))
583 (display-buffer (current-buffer) t
))
585 (delete-file file2
))))
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
)
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
600 (let* ((buf (current-buffer))
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*"))))
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
))
618 (set-buffer-modified-p nil
)
621 (with-current-buffer other
622 (buffer-disable-undo)
623 (insert-buffer-substring buf
)
624 (goto-char (point-min))
625 (while (smerge-find-conflict)
628 (set-buffer-modified-p nil
)
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)
641 (delete-region (match-beginning 0) (match-end 0))))
643 (set-buffer-modified-p nil
)
646 ;; the rest of the code is inspired from vc.el
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)))
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
)
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
))
668 (with-current-buffer buf
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")))
681 (define-minor-mode smerge-mode
682 "Minor mode to simplify editing output from the diff3 program.
684 :group
'smerge
:lighter
" SMerge"
685 (when (and (boundp 'font-lock-mode
) font-lock-mode
)
686 (set (make-local-variable 'font-lock-multiline
) t
)
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)
694 (font-lock-fontify-region (match-beginning 0) (match-end 0) nil
))))))
697 (provide 'smerge-mode
)
699 ;; arch-tag: 605c8d1e-e43d-4943-a6f3-1bcc4333e690
700 ;;; smerge-mode.el ends here