1 ;;; smerge-mode.el --- Minor mode to resolve diff3 conflicts
3 ;; Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
5 ;; Author: Stefan Monnier <monnier@cs.yale.edu>
6 ;; Keywords: merge diff3 cvs conflict
7 ;; Revision: $Id: smerge-mode.el,v 1.16 2002/08/15 00:24:56 monnier Exp $
9 ;; This file is part of GNU Emacs.
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)
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.
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., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
28 ;; Provides a lightweight alternative to emerge/ediff.
29 ;; To use it, simply add to your .emacs the following lines:
31 ;; (autoload 'smerge-mode "smerge-mode" nil t)
33 ;; you can even have it turned on automatically with the following
34 ;; piece of code in your .emacs:
36 ;; (defun sm-try-smerge ()
38 ;; (goto-char (point-min))
39 ;; (when (re-search-forward "^<<<<<<< " nil t)
41 ;; (add-hook 'find-file-hook 'sm-try-smerge t)
45 ;; - if requested, ask the user whether he wants to call ediff right away
49 (eval-when-compile (require 'cl
))
53 "Minor mode to resolve diff3 conflicts."
57 (defcustom smerge-diff-buffer-name
"*vc-diff*"
58 "Buffer name to use for displaying diffs."
63 (const "*smerge-diff*")
66 (defcustom smerge-diff-switches
68 (if (listp diff-switches
) diff-switches
(list diff-switches
)))
69 "*A list of strings specifying switches to be be passed to diff.
70 Used in `smerge-diff-base-mine' and related functions."
72 :type
'(repeat string
))
74 (defcustom smerge-auto-leave t
75 "*Non-nil means to leave `smerge-mode' when the last conflict is resolved."
79 (defface smerge-mine-face
80 '((((background light
))
83 (:foreground
"cyan")))
86 (defvar smerge-mine-face
'smerge-mine-face
)
88 (defface smerge-other-face
89 '((((background light
))
90 (:foreground
"darkgreen"))
92 (:foreground
"lightgreen")))
93 "Face for the other code."
95 (defvar smerge-other-face
'smerge-other-face
)
97 (defface smerge-base-face
98 '((((background light
))
101 (:foreground
"orange")))
102 "Face for the base code."
104 (defvar smerge-base-face
'smerge-base-face
)
106 (defface smerge-markers-face
107 '((((background light
))
108 (:background
"grey85"))
110 (:background
"grey30")))
111 "Face for the conflict markers."
113 (defvar smerge-markers-face
'smerge-markers-face
)
115 (easy-mmode-defmap smerge-basic-map
116 `(("n" . smerge-next
)
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 (easy-menu-define smerge-mode-menu smerge-mode-map
140 "Menu for `smerge-mode'."
142 ["Next" smerge-next
:help
"Go to next conflict"]
143 ["Previous" smerge-prev
:help
"Go to previous conflict"]
144 ["Keep All" smerge-keep-all
:help
"Keep all three versions"]
145 ["Revert to Base" smerge-keep-base
:help
"Revert to base version"]
146 ["Keep Other" smerge-keep-other
:help
"Keep `other' version"]
147 ["Keep Yours" smerge-keep-mine
:help
"Keep your version"]
148 ["Keep Current" smerge-keep-current
:help
"Use current (at point) version"]
150 ["Diff Base/Mine" smerge-diff-base-mine
151 :help
"Diff `base' and `mine' for current conflict"]
152 ["Diff Base/Other" smerge-diff-base-other
153 :help
"Diff `base' and `other' for current conflict"]
154 ["Diff Mine/Other" smerge-diff-mine-other
155 :help
"Diff `mine' and `other' for current conflict"]
157 ["Invoke Ediff" smerge-ediff
158 :help
"Use Ediff to resolve the conflicts"]
161 (defconst smerge-font-lock-keywords
162 '((smerge-find-conflict
163 (1 smerge-mine-face prepend t
)
164 (2 smerge-base-face prepend t
)
165 (3 smerge-other-face prepend t
)
166 ;; FIXME: `keep' doesn't work right with syntactic fontification.
167 (0 smerge-markers-face keep
)
170 "Font lock patterns for `smerge-mode'.")
172 (defconst smerge-begin-re
"^<<<<<<< \\(.*\\)\n")
173 (defconst smerge-end-re
"^>>>>>>> .*\n")
174 (defconst smerge-base-re
"^||||||| .*\n")
175 (defconst smerge-other-re
"^=======\n")
177 (defvar smerge-conflict-style nil
178 "Keep track of which style of conflict is in use.
179 Can be nil if the style is undecided, or else:
183 ;; Compiler pacifiers
184 (defvar font-lock-mode
)
185 (defvar font-lock-keywords
)
191 ;; Define smerge-next and smerge-prev
192 (easy-mmode-define-navigation smerge smerge-begin-re
"conflict")
194 (defconst smerge-match-names
["conflict" "mine" "base" "other"])
196 (defun smerge-ensure-match (n)
197 (unless (match-end n
)
198 (error (format "No `%s'" (aref smerge-match-names n
)))))
200 (defun smerge-auto-leave ()
201 (when (and smerge-auto-leave
202 (save-excursion (goto-char (point-min))
203 (not (re-search-forward smerge-begin-re nil t
))))
207 (defun smerge-keep-all ()
208 "Keep all three versions.
209 Convenient for the kind of conflicts that can arise in ChangeLog files."
211 (smerge-match-conflict)
212 (replace-match (concat (or (match-string 1) "")
213 (or (match-string 2) "")
214 (or (match-string 3) ""))
218 (defun smerge-combine-with-next ()
219 "Combine the current conflict with the next one."
221 (smerge-match-conflict)
223 (dolist (i '(3 2 1 0))
224 (push (if (match-end i
) (copy-marker (match-end i
) t
)) ends
))
225 (setq ends
(apply 'vector ends
))
226 (goto-char (aref ends
0))
227 (if (not (re-search-forward smerge-begin-re nil t
))
228 (error "No next conflict")
229 (smerge-match-conflict)
230 (let ((match-data (mapcar (lambda (m) (if m
(copy-marker m
)))
232 ;; First copy the in-between text in each alternative.
235 (goto-char (aref ends i
))
236 (insert-buffer-substring (current-buffer)
237 (aref ends
0) (car match-data
))))
238 (delete-region (aref ends
0) (car match-data
))
239 ;; Then move the second conflict's alternatives into the first.
241 (set-match-data match-data
)
242 (when (and (aref ends i
) (match-end i
))
243 (goto-char (aref ends i
))
244 (insert-buffer-substring (current-buffer)
245 (match-beginning i
) (match-end i
))))
246 (delete-region (car match-data
) (cadr match-data
))
248 (dolist (m match-data
) (if m
(move-marker m nil
)))
249 (mapc (lambda (m) (if m
(move-marker m nil
))) ends
)))))
251 (defun smerge-keep-base ()
252 "Revert to the base version."
254 (smerge-match-conflict)
255 (smerge-ensure-match 2)
256 (replace-match (match-string 2) t t
)
259 (defun smerge-keep-other ()
260 "Use \"other\" version."
262 (smerge-match-conflict)
263 ;;(smerge-ensure-match 3)
264 (replace-match (match-string 3) t t
)
267 (defun smerge-keep-mine ()
270 (smerge-match-conflict)
271 ;;(smerge-ensure-match 1)
272 (replace-match (match-string 1) t t
)
275 (defun smerge-keep-current ()
276 "Use the current (under the cursor) version."
278 (smerge-match-conflict)
280 (while (or (not (match-end i
))
281 (< (point) (match-beginning i
))
282 (>= (point) (match-end i
)))
284 (if (<= i
0) (error "Not inside a version")
285 (replace-match (match-string i
) t t
)
286 (smerge-auto-leave))))
288 (defun smerge-diff-base-mine ()
289 "Diff 'base' and 'mine' version in current conflict region."
293 (defun smerge-diff-base-other ()
294 "Diff 'base' and 'other' version in current conflict region."
298 (defun smerge-diff-mine-other ()
299 "Diff 'mine' and 'other' version in current conflict region."
303 (defun smerge-match-conflict ()
304 "Get info about the conflict. Puts the info in the `match-data'.
305 The submatches contain:
306 0: the whole conflict.
310 An error is raised if not inside a conflict."
313 (let* ((orig-point (point))
316 (_ (re-search-backward smerge-begin-re
))
318 (start (match-beginning 0))
319 (mine-start (match-end 0))
320 (filename (match-string 1))
322 (_ (re-search-forward smerge-end-re
))
323 (_ (assert (< orig-point
(match-end 0))))
325 (other-end (match-beginning 0))
328 (_ (re-search-backward smerge-other-re start
))
330 (mine-end (match-beginning 0))
331 (other-start (match-end 0))
335 ;; handle the various conflict styles
337 ((re-search-backward smerge-base-re start t
)
338 ;; a 3-parts conflict
339 (set (make-local-variable 'smerge-conflict-style
) 'diff3-A
)
340 (setq base-end mine-end
)
341 (setq mine-end
(match-beginning 0))
342 (setq base-start
(match-end 0)))
344 ((string= filename
(file-name-nondirectory
345 (or buffer-file-name
"")))
346 ;; a 2-parts conflict
347 (set (make-local-variable 'smerge-conflict-style
) 'diff3-E
))
349 ((and (not base-start
)
350 (or (eq smerge-conflict-style
'diff3-A
)
351 (string-match "^[.0-9]+\\'" filename
)))
352 ;; a same-diff conflict
353 (setq base-start mine-start
)
354 (setq base-end mine-end
)
355 (setq mine-start other-start
)
356 (setq mine-end other-end
)))
358 (store-match-data (list start end
361 other-start other-end
362 (when base-start
(1- base-start
)) base-start
363 (1- other-start
) other-start
))
365 (search-failed (error "Point not in conflict region")))))
367 (defun smerge-find-conflict (&optional limit
)
368 "Find and match a conflict region. Intended as a font-lock MATCHER.
369 The submatches are the same as in `smerge-match-conflict'.
370 Returns non-nil if a match is found between the point and LIMIT.
371 The point is moved to the end of the conflict."
372 (when (re-search-forward smerge-begin-re limit t
)
374 (smerge-match-conflict)
375 (goto-char (match-end 0)))))
377 (defun smerge-diff (n1 n2
)
378 (smerge-match-conflict)
379 (smerge-ensure-match n1
)
380 (smerge-ensure-match n2
)
381 (let ((name1 (aref smerge-match-names n1
))
382 (name2 (aref smerge-match-names n2
))
383 ;; Read them before the match-data gets clobbered.
384 (beg1 (match-beginning n1
))
385 (end1 (match-end n1
))
386 (beg2 (match-beginning n2
))
387 (end2 (match-end n2
))
388 (file1 (make-temp-file "smerge1"))
389 (file2 (make-temp-file "smerge2"))
390 (dir default-directory
)
391 (file (file-relative-name buffer-file-name
))
392 (coding-system-for-read buffer-file-coding-system
))
393 (write-region beg1 end1 file1 nil
'nomessage
)
394 (write-region beg2 end2 file2 nil
'nomessage
)
396 (with-current-buffer (get-buffer-create smerge-diff-buffer-name
)
397 (setq default-directory dir
)
398 (let ((inhibit-read-only t
))
401 (apply 'call-process diff-command nil t nil
402 (append smerge-diff-switches
403 (list "-L" (concat name1
"/" file
)
404 "-L" (concat name2
"/" file
)
406 (if (eq status
0) (insert "No differences found.\n"))))
407 (goto-char (point-min))
409 (display-buffer (current-buffer) t
))
411 (delete-file file2
))))
413 ;; compiler pacifiers
414 (defvar smerge-ediff-windows
)
415 (defvar smerge-ediff-buf
)
416 (defvar ediff-buffer-A
)
417 (defvar ediff-buffer-B
)
418 (defvar ediff-buffer-C
)
420 (defun smerge-ediff ()
421 "Invoke ediff to resolve the conflicts."
423 (let* ((buf (current-buffer))
425 ;;(ediff-default-variant 'default-B)
426 (config (current-window-configuration))
427 (filename (file-name-nondirectory buffer-file-name
))
428 (mine (generate-new-buffer (concat "*" filename
" MINE*")))
429 (other (generate-new-buffer (concat "*" filename
" OTHER*")))
431 (with-current-buffer mine
432 (buffer-disable-undo)
433 (insert-buffer-substring buf
)
434 (goto-char (point-min))
435 (while (smerge-find-conflict)
436 (when (match-beginning 2) (setq base t
))
437 (replace-match (match-string 1) t t
))
439 (set-buffer-modified-p nil
)
442 (with-current-buffer other
443 (buffer-disable-undo)
444 (insert-buffer-substring buf
)
445 (goto-char (point-min))
446 (while (smerge-find-conflict)
447 (replace-match (match-string 3) t t
))
449 (set-buffer-modified-p nil
)
453 (setq base
(generate-new-buffer (concat "*" filename
" BASE*")))
454 (with-current-buffer base
455 (buffer-disable-undo)
456 (insert-buffer-substring buf
)
457 (goto-char (point-min))
458 (while (smerge-find-conflict)
459 (replace-match (or (match-string 2) "") t t
))
461 (set-buffer-modified-p nil
)
464 ;; the rest of the code is inspired from vc.el
468 (ediff-merge-buffers-with-ancestor mine other base
)
469 ;; nil 'ediff-merge-revisions-with-ancestor buffer-file-name)
470 (ediff-merge-buffers mine other
)))
471 ;; nil 'ediff-merge-revisions buffer-file-name)))
473 ;; Ediff is now set up, and we are in the control buffer.
474 ;; Do a few further adjustments and take precautions for exit.
475 (set (make-local-variable 'smerge-ediff-windows
) config
)
476 (set (make-local-variable 'smerge-ediff-buf
) buf
)
477 (set (make-local-variable 'ediff-quit-hook
)
479 (let ((buffer-A ediff-buffer-A
)
480 (buffer-B ediff-buffer-B
)
481 (buffer-C ediff-buffer-C
)
482 (buffer-Ancestor ediff-ancestor-buffer
)
483 (buf smerge-ediff-buf
)
484 (windows smerge-ediff-windows
))
486 (with-current-buffer buf
488 (insert-buffer buffer-C
)
489 (kill-buffer buffer-A
)
490 (kill-buffer buffer-B
)
491 (kill-buffer buffer-C
)
492 (when (bufferp buffer-Ancestor
) (kill-buffer buffer-Ancestor
))
493 (set-window-configuration windows
)
494 (message "Conflict resolution finished; you may save the buffer")))))
495 (message "Please resolve conflicts now; exit ediff when done")))
499 (define-minor-mode smerge-mode
500 "Minor mode to simplify editing output from the diff3 program.
503 (when (and (boundp 'font-lock-mode
) font-lock-mode
)
504 (set (make-local-variable 'font-lock-multiline
) t
)
507 (font-lock-add-keywords nil smerge-font-lock-keywords
'append
)
508 (font-lock-remove-keywords nil smerge-font-lock-keywords
))
509 (goto-char (point-min))
510 (while (smerge-find-conflict)
512 (font-lock-fontify-region (match-beginning 0) (match-end 0) nil
))))))
515 (provide 'smerge-mode
)
516 ;;; smerge-mode.el ends here