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.19 2002/10/10 13:01:14 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 ("r" . smerge-resolve
)
119 ("a" . smerge-keep-all
)
120 ("b" . smerge-keep-base
)
121 ("o" . smerge-keep-other
)
122 ("m" . smerge-keep-mine
)
124 ("\C-m" . smerge-keep-current
)
125 ("=" .
,(make-sparse-keymap "Diff"))
126 ("=<" "base-mine" . smerge-diff-base-mine
)
127 ("=>" "base-other" . smerge-diff-base-other
)
128 ("==" "mine-other" . smerge-diff-mine-other
))
129 "The base keymap for `smerge-mode'.")
131 (defcustom smerge-command-prefix
"\C-c^"
132 "Prefix for `smerge-mode' commands."
134 :type
'(choice (string "\e") (string "\C-c^") (string "") string
))
136 (easy-mmode-defmap smerge-mode-map
137 `((,smerge-command-prefix .
,smerge-basic-map
))
138 "Keymap for `smerge-mode'.")
140 (easy-menu-define smerge-mode-menu smerge-mode-map
141 "Menu for `smerge-mode'."
143 ["Next" smerge-next
:help
"Go to next conflict"]
144 ["Previous" smerge-prev
:help
"Go to previous conflict"]
145 ["Keep All" smerge-keep-all
:help
"Keep all three versions"]
146 ["Revert to Base" smerge-keep-base
:help
"Revert to base version"]
147 ["Keep Other" smerge-keep-other
:help
"Keep `other' version"]
148 ["Keep Yours" smerge-keep-mine
:help
"Keep your version"]
149 ["Keep Current" smerge-keep-current
:help
"Use current (at point) version"]
151 ["Diff Base/Mine" smerge-diff-base-mine
152 :help
"Diff `base' and `mine' for current conflict"]
153 ["Diff Base/Other" smerge-diff-base-other
154 :help
"Diff `base' and `other' for current conflict"]
155 ["Diff Mine/Other" smerge-diff-mine-other
156 :help
"Diff `mine' and `other' for current conflict"]
158 ["Invoke Ediff" smerge-ediff
159 :help
"Use Ediff to resolve the conflicts"]
162 (defconst smerge-font-lock-keywords
163 '((smerge-find-conflict
164 (1 smerge-mine-face prepend t
)
165 (2 smerge-base-face prepend t
)
166 (3 smerge-other-face prepend t
)
167 ;; FIXME: `keep' doesn't work right with syntactic fontification.
168 (0 smerge-markers-face keep
)
171 "Font lock patterns for `smerge-mode'.")
173 (defconst smerge-begin-re
"^<<<<<<< \\(.*\\)\n")
174 (defconst smerge-end-re
"^>>>>>>> .*\n")
175 (defconst smerge-base-re
"^||||||| .*\n")
176 (defconst smerge-other-re
"^=======\n")
178 (defvar smerge-conflict-style nil
179 "Keep track of which style of conflict is in use.
180 Can be nil if the style is undecided, or else:
184 ;; Compiler pacifiers
185 (defvar font-lock-mode
)
186 (defvar font-lock-keywords
)
192 ;; Define smerge-next and smerge-prev
193 (easy-mmode-define-navigation smerge smerge-begin-re
"conflict")
195 (defconst smerge-match-names
["conflict" "mine" "base" "other"])
197 (defun smerge-ensure-match (n)
198 (unless (match-end n
)
199 (error (format "No `%s'" (aref smerge-match-names n
)))))
201 (defun smerge-auto-leave ()
202 (when (and smerge-auto-leave
203 (save-excursion (goto-char (point-min))
204 (not (re-search-forward smerge-begin-re nil t
))))
208 (defun smerge-keep-all ()
209 "Keep all three versions.
210 Convenient for the kind of conflicts that can arise in ChangeLog files."
212 (smerge-match-conflict)
213 (replace-match (concat (or (match-string 1) "")
214 (or (match-string 2) "")
215 (or (match-string 3) ""))
219 (defun smerge-combine-with-next ()
220 "Combine the current conflict with the next one."
222 (smerge-match-conflict)
224 (dolist (i '(3 2 1 0))
225 (push (if (match-end i
) (copy-marker (match-end i
) t
)) ends
))
226 (setq ends
(apply 'vector ends
))
227 (goto-char (aref ends
0))
228 (if (not (re-search-forward smerge-begin-re nil t
))
229 (error "No next conflict")
230 (smerge-match-conflict)
231 (let ((match-data (mapcar (lambda (m) (if m
(copy-marker m
)))
233 ;; First copy the in-between text in each alternative.
236 (goto-char (aref ends i
))
237 (insert-buffer-substring (current-buffer)
238 (aref ends
0) (car match-data
))))
239 (delete-region (aref ends
0) (car match-data
))
240 ;; Then move the second conflict's alternatives into the first.
242 (set-match-data match-data
)
243 (when (and (aref ends i
) (match-end i
))
244 (goto-char (aref ends i
))
245 (insert-buffer-substring (current-buffer)
246 (match-beginning i
) (match-end i
))))
247 (delete-region (car match-data
) (cadr match-data
))
249 (dolist (m match-data
) (if m
(move-marker m nil
)))
250 (mapc (lambda (m) (if m
(move-marker m nil
))) ends
)))))
252 (defvar smerge-resolve-function
253 (lambda () (error "Don't know how to resolve"))
254 "Mode-specific merge function.
255 The function is called with no argument and with the match data set
256 according to `smerge-match-conflict'.")
258 (defun smerge-resolve ()
259 "Resolve the conflict at point intelligently.
260 This relies on mode-specific knowledge and thus only works in
261 some major modes. Uses `smerge-resolve-function' to do the actual work."
263 (smerge-match-conflict)
264 (funcall smerge-resolve-function
)
267 (defun smerge-keep-base ()
268 "Revert to the base version."
270 (smerge-match-conflict)
271 (smerge-ensure-match 2)
272 (replace-match (match-string 2) t t
)
275 (defun smerge-keep-other ()
276 "Use \"other\" version."
278 (smerge-match-conflict)
279 ;;(smerge-ensure-match 3)
280 (replace-match (match-string 3) t t
)
283 (defun smerge-keep-mine ()
286 (smerge-match-conflict)
287 ;;(smerge-ensure-match 1)
288 (replace-match (match-string 1) t t
)
291 (defun smerge-keep-current ()
292 "Use the current (under the cursor) version."
294 (smerge-match-conflict)
296 (while (or (not (match-end i
))
297 (< (point) (match-beginning i
))
298 (>= (point) (match-end i
)))
300 (if (<= i
0) (error "Not inside a version")
301 (replace-match (match-string i
) t t
)
302 (smerge-auto-leave))))
304 (defun smerge-diff-base-mine ()
305 "Diff 'base' and 'mine' version in current conflict region."
309 (defun smerge-diff-base-other ()
310 "Diff 'base' and 'other' version in current conflict region."
314 (defun smerge-diff-mine-other ()
315 "Diff 'mine' and 'other' version in current conflict region."
319 (defun smerge-match-conflict ()
320 "Get info about the conflict. Puts the info in the `match-data'.
321 The submatches contain:
322 0: the whole conflict.
326 An error is raised if not inside a conflict."
329 (let* ((orig-point (point))
332 (_ (re-search-backward smerge-begin-re
))
334 (start (match-beginning 0))
335 (mine-start (match-end 0))
336 (filename (or (match-string 1) ""))
338 (_ (re-search-forward smerge-end-re
))
339 (_ (assert (< orig-point
(match-end 0))))
341 (other-end (match-beginning 0))
344 (_ (re-search-backward smerge-other-re start
))
346 (mine-end (match-beginning 0))
347 (other-start (match-end 0))
351 ;; handle the various conflict styles
353 ((re-search-backward smerge-base-re start t
)
354 ;; a 3-parts conflict
355 (set (make-local-variable 'smerge-conflict-style
) 'diff3-A
)
356 (setq base-end mine-end
)
357 (setq mine-end
(match-beginning 0))
358 (setq base-start
(match-end 0)))
360 ((string= filename
(file-name-nondirectory
361 (or buffer-file-name
"")))
362 ;; a 2-parts conflict
363 (set (make-local-variable 'smerge-conflict-style
) 'diff3-E
))
365 ((and (not base-start
)
366 (or (eq smerge-conflict-style
'diff3-A
)
367 (string-match "^[.0-9]+\\'" filename
)))
368 ;; a same-diff conflict
369 (setq base-start mine-start
)
370 (setq base-end mine-end
)
371 (setq mine-start other-start
)
372 (setq mine-end other-end
)))
374 (store-match-data (list start end
377 other-start other-end
378 (when base-start
(1- base-start
)) base-start
379 (1- other-start
) other-start
))
381 (search-failed (error "Point not in conflict region")))))
383 (defun smerge-find-conflict (&optional limit
)
384 "Find and match a conflict region. Intended as a font-lock MATCHER.
385 The submatches are the same as in `smerge-match-conflict'.
386 Returns non-nil if a match is found between the point and LIMIT.
387 The point is moved to the end of the conflict."
388 (when (re-search-forward smerge-begin-re limit t
)
390 (smerge-match-conflict)
391 (goto-char (match-end 0)))))
393 (defun smerge-diff (n1 n2
)
394 (smerge-match-conflict)
395 (smerge-ensure-match n1
)
396 (smerge-ensure-match n2
)
397 (let ((name1 (aref smerge-match-names n1
))
398 (name2 (aref smerge-match-names n2
))
399 ;; Read them before the match-data gets clobbered.
400 (beg1 (match-beginning n1
))
401 (end1 (match-end n1
))
402 (beg2 (match-beginning n2
))
403 (end2 (match-end n2
))
404 (file1 (make-temp-file "smerge1"))
405 (file2 (make-temp-file "smerge2"))
406 (dir default-directory
)
407 (file (file-relative-name buffer-file-name
))
408 (coding-system-for-read buffer-file-coding-system
))
409 (write-region beg1 end1 file1 nil
'nomessage
)
410 (write-region beg2 end2 file2 nil
'nomessage
)
412 (with-current-buffer (get-buffer-create smerge-diff-buffer-name
)
413 (setq default-directory dir
)
414 (let ((inhibit-read-only t
))
417 (apply 'call-process diff-command nil t nil
418 (append smerge-diff-switches
419 (list "-L" (concat name1
"/" file
)
420 "-L" (concat name2
"/" file
)
422 (if (eq status
0) (insert "No differences found.\n"))))
423 (goto-char (point-min))
425 (display-buffer (current-buffer) t
))
427 (delete-file file2
))))
429 ;; compiler pacifiers
430 (defvar smerge-ediff-windows
)
431 (defvar smerge-ediff-buf
)
432 (defvar ediff-buffer-A
)
433 (defvar ediff-buffer-B
)
434 (defvar ediff-buffer-C
)
437 (defun smerge-ediff (&optional name-mine name-other name-base
)
438 "Invoke ediff to resolve the conflicts.
439 NAME-MINE, NAME-OTHER, and NAME-BASE, if non-nil, are used for the
442 (let* ((buf (current-buffer))
444 ;;(ediff-default-variant 'default-B)
445 (config (current-window-configuration))
446 (filename (file-name-nondirectory buffer-file-name
))
447 (mine (generate-new-buffer
448 (or name-mine
(concat "*" filename
" MINE*"))))
449 (other (generate-new-buffer
450 (or name-other
(concat "*" filename
" OTHER*"))))
452 (with-current-buffer mine
453 (buffer-disable-undo)
454 (insert-buffer-substring buf
)
455 (goto-char (point-min))
456 (while (smerge-find-conflict)
457 (when (match-beginning 2) (setq base t
))
458 (replace-match (match-string 1) t t
))
460 (set-buffer-modified-p nil
)
463 (with-current-buffer other
464 (buffer-disable-undo)
465 (insert-buffer-substring buf
)
466 (goto-char (point-min))
467 (while (smerge-find-conflict)
468 (replace-match (match-string 3) t t
))
470 (set-buffer-modified-p nil
)
474 (setq base
(generate-new-buffer
475 (or name-base
(concat "*" filename
" BASE*"))))
476 (with-current-buffer base
477 (buffer-disable-undo)
478 (insert-buffer-substring buf
)
479 (goto-char (point-min))
480 (while (smerge-find-conflict)
481 (replace-match (or (match-string 2) "") t t
))
483 (set-buffer-modified-p nil
)
486 ;; the rest of the code is inspired from vc.el
490 (ediff-merge-buffers-with-ancestor mine other base
)
491 ;; nil 'ediff-merge-revisions-with-ancestor buffer-file-name)
492 (ediff-merge-buffers mine other
)))
493 ;; nil 'ediff-merge-revisions buffer-file-name)))
495 ;; Ediff is now set up, and we are in the control buffer.
496 ;; Do a few further adjustments and take precautions for exit.
497 (set (make-local-variable 'smerge-ediff-windows
) config
)
498 (set (make-local-variable 'smerge-ediff-buf
) buf
)
499 (set (make-local-variable 'ediff-quit-hook
)
501 (let ((buffer-A ediff-buffer-A
)
502 (buffer-B ediff-buffer-B
)
503 (buffer-C ediff-buffer-C
)
504 (buffer-Ancestor ediff-ancestor-buffer
)
505 (buf smerge-ediff-buf
)
506 (windows smerge-ediff-windows
))
508 (with-current-buffer buf
510 (insert-buffer buffer-C
)
511 (kill-buffer buffer-A
)
512 (kill-buffer buffer-B
)
513 (kill-buffer buffer-C
)
514 (when (bufferp buffer-Ancestor
) (kill-buffer buffer-Ancestor
))
515 (set-window-configuration windows
)
516 (message "Conflict resolution finished; you may save the buffer")))))
517 (message "Please resolve conflicts now; exit ediff when done")))
521 (define-minor-mode smerge-mode
522 "Minor mode to simplify editing output from the diff3 program.
525 (when (and (boundp 'font-lock-mode
) font-lock-mode
)
526 (set (make-local-variable 'font-lock-multiline
) t
)
529 (font-lock-add-keywords nil smerge-font-lock-keywords
'append
)
530 (font-lock-remove-keywords nil smerge-font-lock-keywords
))
531 (goto-char (point-min))
532 (while (smerge-find-conflict)
534 (font-lock-fontify-region (match-beginning 0) (match-end 0) nil
))))))
537 (provide 'smerge-mode
)
538 ;;; smerge-mode.el ends here