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."
78 (defface smerge-mine-face
79 '((((min-colors 88) (background light
))
80 (:foreground
"blue1"))
83 (((min-colors 88) (background dark
))
84 (:foreground
"cyan1"))
86 (:foreground
"cyan")))
89 (defvar smerge-mine-face
'smerge-mine-face
)
91 (defface smerge-other-face
92 '((((background light
))
93 (:foreground
"darkgreen"))
95 (:foreground
"lightgreen")))
96 "Face for the other code."
98 (defvar smerge-other-face
'smerge-other-face
)
100 (defface smerge-base-face
101 '((((min-colors 88) (background light
))
102 (:foreground
"red1"))
103 (((background light
))
106 (:foreground
"orange")))
107 "Face for the base code."
109 (defvar smerge-base-face
'smerge-base-face
)
111 (defface smerge-markers-face
112 '((((background light
))
113 (:background
"grey85"))
115 (:background
"grey30")))
116 "Face for the conflict markers."
118 (defvar smerge-markers-face
'smerge-markers-face
)
120 (easy-mmode-defmap smerge-basic-map
121 `(("n" . smerge-next
)
123 ("r" . smerge-resolve
)
124 ("a" . smerge-keep-all
)
125 ("b" . smerge-keep-base
)
126 ("o" . smerge-keep-other
)
127 ("m" . smerge-keep-mine
)
129 ("\C-m" . smerge-keep-current
)
130 ("=" .
,(make-sparse-keymap "Diff"))
131 ("=<" "base-mine" . smerge-diff-base-mine
)
132 ("=>" "base-other" . smerge-diff-base-other
)
133 ("==" "mine-other" . smerge-diff-mine-other
))
134 "The base keymap for `smerge-mode'.")
136 (defcustom smerge-command-prefix
"\C-c^"
137 "Prefix for `smerge-mode' commands."
139 :type
'(choice (string "\e") (string "\C-c^") (string "") string
))
141 (easy-mmode-defmap smerge-mode-map
142 `((,smerge-command-prefix .
,smerge-basic-map
))
143 "Keymap for `smerge-mode'.")
145 (defvar smerge-check-cache nil
)
146 (make-variable-buffer-local 'smerge-check-cache
)
147 (defun smerge-check (n)
149 (let ((state (cons (point) (buffer-modified-tick))))
150 (unless (equal (cdr smerge-check-cache
) state
)
151 (smerge-match-conflict)
152 (setq smerge-check-cache
(cons (match-data) state
)))
153 (nth (* 2 n
) (car smerge-check-cache
)))
156 (easy-menu-define smerge-mode-menu smerge-mode-map
157 "Menu for `smerge-mode'."
159 ["Next" smerge-next
:help
"Go to next conflict"]
160 ["Previous" smerge-prev
:help
"Go to previous conflict"]
162 ["Keep All" smerge-keep-all
:help
"Keep all three versions"
163 :active
(smerge-check 1)]
164 ["Keep Current" smerge-keep-current
:help
"Use current (at point) version"
165 :active
(and (smerge-check 1) (> (smerge-get-current) 0))]
167 ["Revert to Base" smerge-keep-base
:help
"Revert to base version"
168 :active
(smerge-check 2)]
169 ["Keep Other" smerge-keep-other
:help
"Keep `other' version"
170 :active
(smerge-check 3)]
171 ["Keep Yours" smerge-keep-mine
:help
"Keep your version"
172 :active
(smerge-check 1)]
174 ["Diff Base/Mine" smerge-diff-base-mine
175 :help
"Diff `base' and `mine' for current conflict"
176 :active
(smerge-check 2)]
177 ["Diff Base/Other" smerge-diff-base-other
178 :help
"Diff `base' and `other' for current conflict"
179 :active
(smerge-check 2)]
180 ["Diff Mine/Other" smerge-diff-mine-other
181 :help
"Diff `mine' and `other' for current conflict"
182 :active
(smerge-check 1)]
184 ["Invoke Ediff" smerge-ediff
185 :help
"Use Ediff to resolve the conflicts"
186 :active
(smerge-check 1)]
187 ["Auto Resolve" smerge-resolve
188 :help
"Try auto-resolution heuristics"
189 :active
(smerge-check 1)]
190 ["Combine" smerge-combine-with-next
191 :help
"Combine current conflict with next"
192 :active
(smerge-check 1)]
195 (easy-menu-define smerge-context-menu nil
196 "Context menu for mine area in `smerge-mode'."
198 ["Keep Current" smerge-keep-current
:help
"Use current (at point) version"]
199 ["Kill Current" smerge-kill-current
:help
"Remove current (at point) version"]
200 ["Keep All" smerge-keep-all
:help
"Keep all three versions"]
202 ["More..." (popup-menu smerge-mode-menu
) :help
"Show full SMerge mode menu"]
205 (defconst smerge-font-lock-keywords
206 '((smerge-find-conflict
207 (1 smerge-mine-face prepend t
)
208 (2 smerge-base-face prepend t
)
209 (3 smerge-other-face prepend t
)
210 ;; FIXME: `keep' doesn't work right with syntactic fontification.
211 (0 smerge-markers-face keep
)
214 "Font lock patterns for `smerge-mode'.")
216 (defconst smerge-begin-re
"^<<<<<<< \\(.*\\)\n")
217 (defconst smerge-end-re
"^>>>>>>> .*\n")
218 (defconst smerge-base-re
"^||||||| .*\n")
219 (defconst smerge-other-re
"^=======\n")
221 (defvar smerge-conflict-style nil
222 "Keep track of which style of conflict is in use.
223 Can be nil if the style is undecided, or else:
227 ;; Compiler pacifiers
228 (defvar font-lock-mode
)
229 (defvar font-lock-keywords
)
235 ;; Define smerge-next and smerge-prev
236 (easy-mmode-define-navigation smerge smerge-begin-re
"conflict")
238 (defconst smerge-match-names
["conflict" "mine" "base" "other"])
240 (defun smerge-ensure-match (n)
241 (unless (match-end n
)
242 (error (format "No `%s'" (aref smerge-match-names n
)))))
244 (defun smerge-auto-leave ()
245 (when (and smerge-auto-leave
246 (save-excursion (goto-char (point-min))
247 (not (re-search-forward smerge-begin-re nil t
))))
251 (defun smerge-keep-all ()
252 "Concatenate all versions."
254 (smerge-match-conflict)
255 (let ((mb2 (or (match-beginning 2) (point-max)))
256 (me2 (or (match-end 2) (point-min))))
257 (delete-region (match-end 3) (match-end 0))
258 (delete-region (max me2
(match-end 1)) (match-beginning 3))
259 (if (and (match-end 2) (/= (match-end 1) (match-end 3)))
260 (delete-region (match-end 1) (match-beginning 2)))
261 (delete-region (match-beginning 0) (min (match-beginning 1) mb2
))
262 (smerge-auto-leave)))
264 (defun smerge-keep-n (n)
265 ;; We used to use replace-match, but that did not preserve markers so well.
266 (delete-region (match-end n
) (match-end 0))
267 (delete-region (match-beginning 0) (match-beginning n
)))
269 (defun smerge-combine-with-next ()
270 "Combine the current conflict with the next one."
272 (smerge-match-conflict)
274 (dolist (i '(3 2 1 0))
275 (push (if (match-end i
) (copy-marker (match-end i
) t
)) ends
))
276 (setq ends
(apply 'vector ends
))
277 (goto-char (aref ends
0))
278 (if (not (re-search-forward smerge-begin-re nil t
))
279 (error "No next conflict")
280 (smerge-match-conflict)
281 (let ((match-data (mapcar (lambda (m) (if m
(copy-marker m
)))
283 ;; First copy the in-between text in each alternative.
286 (goto-char (aref ends i
))
287 (insert-buffer-substring (current-buffer)
288 (aref ends
0) (car match-data
))))
289 (delete-region (aref ends
0) (car match-data
))
290 ;; Then move the second conflict's alternatives into the first.
292 (set-match-data match-data
)
293 (when (and (aref ends i
) (match-end i
))
294 (goto-char (aref ends i
))
295 (insert-buffer-substring (current-buffer)
296 (match-beginning i
) (match-end i
))))
297 (delete-region (car match-data
) (cadr match-data
))
299 (dolist (m match-data
) (if m
(move-marker m nil
)))
300 (mapc (lambda (m) (if m
(move-marker m nil
))) ends
)))))
302 (defvar smerge-resolve-function
303 (lambda () (error "Don't know how to resolve"))
304 "Mode-specific merge function.
305 The function is called with no argument and with the match data set
306 according to `smerge-match-conflict'.")
308 (defvar smerge-text-properties
309 `(help-echo "merge conflict: mouse-3 shows a menu"
310 ;; mouse-face highlight
311 keymap
(keymap (down-mouse-3 . smerge-popup-context-menu
))))
313 (defun smerge-remove-props (&optional beg end
)
314 (remove-text-properties
315 (or beg
(match-beginning 0))
316 (or end
(match-end 0))
317 smerge-text-properties
))
319 (defun smerge-popup-context-menu (event)
320 "Pop up the Smerge mode context menu under mouse."
323 (save-excursion (posn-set-point (event-end event
)) (smerge-check 1)))
325 (posn-set-point (event-end event
))
326 (smerge-match-conflict)
327 (let ((i (smerge-get-current))
331 (popup-menu smerge-mode-menu
)
333 (setq o
(make-overlay (match-beginning i
) (match-end i
)))
336 (overlay-put o
'face
'highlight
)
337 (sit-for 0) ;Display the new highlighting.
338 (popup-menu smerge-context-menu
))
340 (delete-overlay o
)))))
341 ;; There's no conflict at point, the text-props are just obsolete.
343 (let ((beg (re-search-backward smerge-end-re nil t
))
344 (end (re-search-forward smerge-begin-re nil t
)))
345 (smerge-remove-props (or beg
(point-min)) (or end
(point-max)))
346 (push event unread-command-events
)))))
348 (defun smerge-resolve ()
349 "Resolve the conflict at point intelligently.
350 This relies on mode-specific knowledge and thus only works in
351 some major modes. Uses `smerge-resolve-function' to do the actual work."
353 (smerge-match-conflict)
354 (smerge-remove-props)
356 ;; Trivial diff3 -A non-conflicts.
357 ((and (eq (match-end 1) (match-end 3))
358 (eq (match-beginning 1) (match-beginning 3)))
359 ;; FIXME: Add "if [ diff -b MINE OTHER ]; then select OTHER; fi"
362 ;; FIXME: Add "diff -b BASE MINE | patch OTHER".
363 ;; FIXME: Add "diff -b BASE OTHER | patch MINE".
366 ((and (not (match-end 2))
367 ;; FIXME: Add "diff -b"-based refinement.
371 ;; Mode-specific conflict resolution.
372 (funcall smerge-resolve-function
)))
375 (defun smerge-keep-base ()
376 "Revert to the base version."
378 (smerge-match-conflict)
379 (smerge-ensure-match 2)
380 (smerge-remove-props)
384 (defun smerge-keep-other ()
385 "Use \"other\" version."
387 (smerge-match-conflict)
388 ;;(smerge-ensure-match 3)
389 (smerge-remove-props)
393 (defun smerge-keep-mine ()
396 (smerge-match-conflict)
397 ;;(smerge-ensure-match 1)
398 (smerge-remove-props)
402 (defun smerge-get-current ()
404 (while (or (not (match-end i
))
405 (< (point) (match-beginning i
))
406 (>= (point) (match-end i
)))
410 (defun smerge-keep-current ()
411 "Use the current (under the cursor) version."
413 (smerge-match-conflict)
414 (let ((i (smerge-get-current)))
415 (if (<= i
0) (error "Not inside a version")
416 (smerge-remove-props)
418 (smerge-auto-leave))))
420 (defun smerge-kill-current ()
421 "Remove the current (under the cursor) version."
423 (smerge-match-conflict)
424 (let ((i (smerge-get-current)))
425 (if (<= i
0) (error "Not inside a version")
426 (smerge-remove-props)
429 (if (and (match-end n
) (/= (match-end n
) (match-end i
)))
432 (/= (match-end (car left
)) (match-end (cadr left
))))
433 (ding) ;We don't know how to do that.
434 (smerge-keep-n (car left
))
435 (smerge-auto-leave))))))
437 (defun smerge-diff-base-mine ()
438 "Diff 'base' and 'mine' version in current conflict region."
442 (defun smerge-diff-base-other ()
443 "Diff 'base' and 'other' version in current conflict region."
447 (defun smerge-diff-mine-other ()
448 "Diff 'mine' and 'other' version in current conflict region."
452 (defun smerge-match-conflict ()
453 "Get info about the conflict. Puts the info in the `match-data'.
454 The submatches contain:
455 0: the whole conflict.
459 An error is raised if not inside a conflict."
462 (let* ((orig-point (point))
465 (_ (re-search-backward smerge-begin-re
))
467 (start (match-beginning 0))
468 (mine-start (match-end 0))
469 (filename (or (match-string 1) ""))
471 (_ (re-search-forward smerge-end-re
))
472 (_ (assert (< orig-point
(match-end 0))))
474 (other-end (match-beginning 0))
477 (_ (re-search-backward smerge-other-re start
))
479 (mine-end (match-beginning 0))
480 (other-start (match-end 0))
484 ;; handle the various conflict styles
487 (goto-char mine-start
)
488 (re-search-forward smerge-begin-re end t
))
489 ;; There's a nested conflict and we're after the the beginning
490 ;; of the outer one but before the beginning of the inner one.
491 (error "There is a nested conflict"))
493 ((re-search-backward smerge-base-re start t
)
494 ;; a 3-parts conflict
495 (set (make-local-variable 'smerge-conflict-style
) 'diff3-A
)
496 (setq base-end mine-end
)
497 (setq mine-end
(match-beginning 0))
498 (setq base-start
(match-end 0)))
500 ((string= filename
(file-name-nondirectory
501 (or buffer-file-name
"")))
502 ;; a 2-parts conflict
503 (set (make-local-variable 'smerge-conflict-style
) 'diff3-E
))
505 ((and (not base-start
)
506 (or (eq smerge-conflict-style
'diff3-A
)
507 (equal filename
"ANCESTOR")
508 (string-match "\\`[.0-9]+\\'" filename
)))
509 ;; a same-diff conflict
510 (setq base-start mine-start
)
511 (setq base-end mine-end
)
512 (setq mine-start other-start
)
513 (setq mine-end other-end
)))
515 (let ((inhibit-read-only t
)
516 (inhibit-modification-hooks t
)
517 (m (buffer-modified-p)))
519 (add-text-properties start end smerge-text-properties
)
520 (restore-buffer-modified-p m
)))
522 (store-match-data (list start end
525 other-start other-end
526 (when base-start
(1- base-start
)) base-start
527 (1- other-start
) other-start
))
529 (search-failed (error "Point not in conflict region")))))
531 (defun smerge-find-conflict (&optional limit
)
532 "Find and match a conflict region. Intended as a font-lock MATCHER.
533 The submatches are the same as in `smerge-match-conflict'.
534 Returns non-nil if a match is found between the point and LIMIT.
535 The point is moved to the end of the conflict."
536 (when (re-search-forward smerge-begin-re limit t
)
539 (smerge-match-conflict)
540 (goto-char (match-end 0)))
541 (error (smerge-find-conflict limit
)))))
543 (defun smerge-diff (n1 n2
)
544 (smerge-match-conflict)
545 (smerge-ensure-match n1
)
546 (smerge-ensure-match n2
)
547 (let ((name1 (aref smerge-match-names n1
))
548 (name2 (aref smerge-match-names n2
))
549 ;; Read them before the match-data gets clobbered.
550 (beg1 (match-beginning n1
))
551 (end1 (match-end n1
))
552 (beg2 (match-beginning n2
))
553 (end2 (match-end n2
))
554 (file1 (make-temp-file "smerge1"))
555 (file2 (make-temp-file "smerge2"))
556 (dir default-directory
)
557 (file (file-relative-name buffer-file-name
))
558 (coding-system-for-read buffer-file-coding-system
))
559 (write-region beg1 end1 file1 nil
'nomessage
)
560 (write-region beg2 end2 file2 nil
'nomessage
)
562 (with-current-buffer (get-buffer-create smerge-diff-buffer-name
)
563 (setq default-directory dir
)
564 (let ((inhibit-read-only t
))
567 (apply 'call-process diff-command nil t nil
568 (append smerge-diff-switches
569 (list "-L" (concat name1
"/" file
)
570 "-L" (concat name2
"/" file
)
572 (if (eq status
0) (insert "No differences found.\n"))))
573 (goto-char (point-min))
575 (display-buffer (current-buffer) t
))
577 (delete-file file2
))))
579 ;; compiler pacifiers
580 (defvar smerge-ediff-windows
)
581 (defvar smerge-ediff-buf
)
582 (defvar ediff-buffer-A
)
583 (defvar ediff-buffer-B
)
584 (defvar ediff-buffer-C
)
587 (defun smerge-ediff (&optional name-mine name-other name-base
)
588 "Invoke ediff to resolve the conflicts.
589 NAME-MINE, NAME-OTHER, and NAME-BASE, if non-nil, are used for the
592 (let* ((buf (current-buffer))
594 ;;(ediff-default-variant 'default-B)
595 (config (current-window-configuration))
596 (filename (file-name-nondirectory buffer-file-name
))
597 (mine (generate-new-buffer
598 (or name-mine
(concat "*" filename
" MINE*"))))
599 (other (generate-new-buffer
600 (or name-other
(concat "*" filename
" OTHER*"))))
602 (with-current-buffer mine
603 (buffer-disable-undo)
604 (insert-buffer-substring buf
)
605 (goto-char (point-min))
606 (while (smerge-find-conflict)
607 (when (match-beginning 2) (setq base t
))
610 (set-buffer-modified-p nil
)
613 (with-current-buffer other
614 (buffer-disable-undo)
615 (insert-buffer-substring buf
)
616 (goto-char (point-min))
617 (while (smerge-find-conflict)
620 (set-buffer-modified-p nil
)
624 (setq base
(generate-new-buffer
625 (or name-base
(concat "*" filename
" BASE*"))))
626 (with-current-buffer base
627 (buffer-disable-undo)
628 (insert-buffer-substring buf
)
629 (goto-char (point-min))
630 (while (smerge-find-conflict)
633 (delete-region (match-beginning 0) (match-end 0))))
635 (set-buffer-modified-p nil
)
638 ;; the rest of the code is inspired from vc.el
642 (ediff-merge-buffers-with-ancestor mine other base
)
643 ;; nil 'ediff-merge-revisions-with-ancestor buffer-file-name)
644 (ediff-merge-buffers mine other
)))
645 ;; nil 'ediff-merge-revisions buffer-file-name)))
647 ;; Ediff is now set up, and we are in the control buffer.
648 ;; Do a few further adjustments and take precautions for exit.
649 (set (make-local-variable 'smerge-ediff-windows
) config
)
650 (set (make-local-variable 'smerge-ediff-buf
) buf
)
651 (set (make-local-variable 'ediff-quit-hook
)
653 (let ((buffer-A ediff-buffer-A
)
654 (buffer-B ediff-buffer-B
)
655 (buffer-C ediff-buffer-C
)
656 (buffer-Ancestor ediff-ancestor-buffer
)
657 (buf smerge-ediff-buf
)
658 (windows smerge-ediff-windows
))
660 (with-current-buffer buf
662 (insert-buffer buffer-C
)
663 (kill-buffer buffer-A
)
664 (kill-buffer buffer-B
)
665 (kill-buffer buffer-C
)
666 (when (bufferp buffer-Ancestor
) (kill-buffer buffer-Ancestor
))
667 (set-window-configuration windows
)
668 (message "Conflict resolution finished; you may save the buffer")))))
669 (message "Please resolve conflicts now; exit ediff when done")))
673 (define-minor-mode smerge-mode
674 "Minor mode to simplify editing output from the diff3 program.
676 :group
'smerge
:lighter
" SMerge"
677 (when (and (boundp 'font-lock-mode
) font-lock-mode
)
678 (set (make-local-variable 'font-lock-multiline
) t
)
681 (font-lock-add-keywords nil smerge-font-lock-keywords
'append
)
682 (font-lock-remove-keywords nil smerge-font-lock-keywords
))
683 (goto-char (point-min))
684 (while (smerge-find-conflict)
686 (font-lock-fontify-region (match-beginning 0) (match-end 0) nil
))))))
689 (provide 'smerge-mode
)
691 ;; arch-tag: 605c8d1e-e43d-4943-a6f3-1bcc4333e690
692 ;;; smerge-mode.el ends here