1 ;;; smerge-mode.el --- Minor mode to resolve diff3 conflicts
3 ;; Copyright (C) 1999, 2000 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.5 2000/08/16 19:51:55 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-hooks '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
"*smerge-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 ("d<" . smerge-diff-base-mine
)
125 ("d>" . smerge-diff-base-other
)
126 ("d=" . smerge-diff-mine-other
))
127 "The base keymap for `smerge-mode'.")
129 (defcustom smerge-command-prefix
"\e"
130 "Prefix for `smerge-mode' commands."
132 :type
'(choice (string "\e") (string "C-x^") (string "") string
))
134 (easy-mmode-defmap smerge-mode-map
135 `((,smerge-command-prefix .
,smerge-basic-map
))
136 "Keymap for `smerge-mode'.")
138 (easy-menu-define smerge-mode-menu smerge-mode-map
139 "Menu for `smerge-mode'."
141 ["Next" smerge-next
:help
"Go to next conflict"]
142 ["Previous" smerge-prev
:help
"Go to previous conflict"]
143 ["Keep All" smerge-keep-all
:help
"Keep all three versions"]
144 ["Revert to Base" smerge-keep-base
:help
"Revert to base version"]
145 ["Keep Other" smerge-keep-other
:help
"Keep `other' version"]
146 ["Keep Yours" smerge-keep-mine
:help
"Keep your version"]
147 ["Keep Current" smerge-keep-current
:help
"Use current (at point) version"]
149 ["Diff Base/Mine" smerge-diff-base-mine
150 :help
"Diff `base' and `mine' for current conflict"]
151 ["Diff Base/Other" smerge-diff-base-other
152 :help
"Diff `base' and `other' for current conflict"]
153 ["Diff Mine/Other" smerge-diff-mine-other
154 :help
"Diff `mine' and `other' for current conflict"]
156 ["Invoke Ediff" smerge-ediff
157 :help
"Use Ediff to resolve the conflicts"]
160 (defconst smerge-font-lock-keywords
161 '((smerge-find-conflict
162 (1 smerge-mine-face prepend
)
163 (2 smerge-base-face prepend t
)
164 (3 smerge-other-face prepend t
)
165 (0 smerge-markers-face keep
)
168 "Font lock patterns for `smerge-mode'.")
170 (defconst smerge-begin-re
"^<<<<<<< \\(.*\\)\n")
171 (defconst smerge-end-re
"^>>>>>>> .*\n")
172 (defconst smerge-base-re
"^||||||| .*\n")
173 (defconst smerge-other-re
"^=======\n")
175 (defvar smerge-conflict-style nil
176 "Keep track of which style of conflict is in use.
177 Can be nil if the style is undecided, or else:
181 ;; Compiler pacifiers
182 (defvar font-lock-mode nil
)
183 (defvar font-lock-keywords nil
)
185 (unless (fboundp 'font-lock-fontify-region
)
186 (autoload 'font-lock-fontify-region
"font-lock")))
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-keep-base ()
220 "Revert to the base version."
222 (smerge-match-conflict)
223 (smerge-ensure-match 2)
224 (replace-match (match-string 2) t t
)
227 (defun smerge-keep-other ()
228 "Use \"other\" version."
230 (smerge-match-conflict)
231 ;;(smerge-ensure-match 3)
232 (replace-match (match-string 3) t t
)
235 (defun smerge-keep-mine ()
238 (smerge-match-conflict)
239 ;;(smerge-ensure-match 1)
240 (replace-match (match-string 1) t t
)
243 (defun smerge-keep-current ()
244 "Use the current (under the cursor) version."
246 (smerge-match-conflict)
248 (while (or (not (match-end i
))
249 (< (point) (match-beginning i
))
250 (>= (point) (match-end i
)))
252 (if (<= i
0) (error "Not inside a version")
253 (replace-match (match-string i
) t t
)
254 (smerge-auto-leave))))
256 (defun smerge-diff-base-mine ()
257 "Diff 'base' and 'mine' version in current conflict region."
261 (defun smerge-diff-base-other ()
262 "Diff 'base' and 'other' version in current conflict region."
266 (defun smerge-diff-mine-other ()
267 "Diff 'mine' and 'other' version in current conflict region."
271 (defun smerge-match-conflict ()
272 "Get info about the conflict. Puts the info in the `match-data'.
273 The submatches contain:
274 0: the whole conflict.
278 An error is raised if not inside a conflict."
281 (let* ((orig-point (point))
284 (_ (re-search-backward smerge-begin-re
))
286 (start (match-beginning 0))
287 (mine-start (match-end 0))
288 (filename (match-string 1))
290 (_ (re-search-forward smerge-end-re
))
291 (_ (assert (< orig-point
(match-end 0))))
293 (other-end (match-beginning 0))
296 (_ (re-search-backward smerge-other-re start
))
298 (mine-end (match-beginning 0))
299 (other-start (match-end 0))
303 ;; handle the various conflict styles
305 ((re-search-backward smerge-base-re start t
)
306 ;; a 3-parts conflict
307 (set (make-local-variable 'smerge-conflict-style
) 'diff3-A
)
308 (setq base-end mine-end
)
309 (setq mine-end
(match-beginning 0))
310 (setq base-start
(match-end 0)))
312 ((string= filename
(file-name-nondirectory
313 (or buffer-file-name
"")))
314 ;; a 2-parts conflict
315 (set (make-local-variable 'smerge-conflict-style
) 'diff3-E
))
317 ((and (not base-start
)
318 (or (eq smerge-conflict-style
'diff3-A
)
319 (string-match "^[.0-9]+\\'" filename
)))
320 ;; a same-diff conflict
321 (setq base-start mine-start
)
322 (setq base-end mine-end
)
323 (setq mine-start other-start
)
324 (setq mine-end other-end
)))
326 (store-match-data (list start end
329 other-start other-end
330 (when base-start
(1- base-start
)) base-start
331 (1- other-start
) other-start
))
333 (error "Point not in conflict region"))))
335 (defun smerge-find-conflict (&optional limit
)
336 "Find and match a conflict region. Intended as a font-lock MATCHER.
337 The submatches are the same as in `smerge-match-conflict'.
338 Returns non-nil if a match is found between the point and LIMIT.
339 The point is moved to the end of the conflict."
340 (when (re-search-forward smerge-begin-re limit t
)
342 (smerge-match-conflict)
343 (goto-char (match-end 0)))))
345 (defun smerge-diff (n1 n2
)
346 (smerge-match-conflict)
347 (smerge-ensure-match n1
)
348 (smerge-ensure-match n2
)
349 (let ((name1 (aref smerge-match-names n1
))
350 (name2 (aref smerge-match-names n2
))
351 (file1 (make-temp-file "smerge1"))
352 (file2 (make-temp-file "smerge2")))
353 (write-region (match-beginning n1
) (match-end n1
) file1
)
354 (write-region (match-beginning n2
) (match-end n2
) file2
)
356 (with-current-buffer (get-buffer-create smerge-diff-buffer-name
)
357 (let ((inhibit-read-only t
))
359 (apply 'call-process diff-command nil t nil
360 (append smerge-diff-switches
361 (list "-L" name1
"-L" name2 file1 file2
))))
362 (goto-char (point-min))
364 (display-buffer (current-buffer) t
))
366 (delete-file file2
))))
369 ;; compiler pacifiers
370 (defvar smerge-ediff-windows
)
371 (defvar smerge-ediff-buf
)
372 (defvar ediff-buffer-A
)
373 (defvar ediff-buffer-B
)
374 (defvar ediff-buffer-C
)
375 (unless (fboundp 'ediff-cleanup-mess
)
376 (autoload 'ediff-cleanup-mess
"ediff-util")))
378 (defun smerge-ediff ()
379 "Invoke ediff to resolve the conflicts."
381 (let* ((buf (current-buffer))
383 ;;(ediff-default-variant 'default-B)
384 (config (current-window-configuration))
385 (filename (file-name-nondirectory buffer-file-name
))
386 (mine (generate-new-buffer (concat "*" filename
" MINE*")))
387 (other (generate-new-buffer (concat "*" filename
" OTHER*")))
389 (with-current-buffer mine
390 (buffer-disable-undo)
391 (insert-buffer-substring buf
)
392 (goto-char (point-min))
393 (while (smerge-find-conflict)
394 (when (match-beginning 2) (setq base t
))
395 (replace-match (match-string 1) t t
))
397 (set-buffer-modified-p nil
)
400 (with-current-buffer other
401 (buffer-disable-undo)
402 (insert-buffer-substring buf
)
403 (goto-char (point-min))
404 (while (smerge-find-conflict)
405 (replace-match (match-string 3) t t
))
407 (set-buffer-modified-p nil
)
411 (setq base
(generate-new-buffer (concat "*" filename
" BASE*")))
412 (with-current-buffer base
413 (buffer-disable-undo)
414 (insert-buffer-substring buf
)
415 (goto-char (point-min))
416 (while (smerge-find-conflict)
417 (replace-match (or (match-string 2) "") t t
))
419 (set-buffer-modified-p nil
)
422 ;; the rest of the code is inspired from vc.el
426 (ediff-merge-buffers-with-ancestor mine other base
)
427 ;; nil 'ediff-merge-revisions-with-ancestor buffer-file-name)
428 (ediff-merge-buffers mine other
)))
429 ;; nil 'ediff-merge-revisions buffer-file-name)))
431 ;; Ediff is now set up, and we are in the control buffer.
432 ;; Do a few further adjustments and take precautions for exit.
433 (set (make-local-variable 'smerge-ediff-windows
) config
)
434 (set (make-local-variable 'smerge-ediff-buf
) buf
)
435 (set (make-local-variable 'ediff-quit-hook
)
437 (let ((buffer-A ediff-buffer-A
)
438 (buffer-B ediff-buffer-B
)
439 (buffer-C ediff-buffer-C
)
440 (buffer-Ancestor ediff-ancestor-buffer
)
441 (buf smerge-ediff-buf
)
442 (windows smerge-ediff-windows
))
444 (with-current-buffer buf
446 (insert-buffer buffer-C
)
447 (kill-buffer buffer-A
)
448 (kill-buffer buffer-B
)
449 (kill-buffer buffer-C
)
450 (when (bufferp buffer-Ancestor
) (kill-buffer buffer-Ancestor
))
451 (set-window-configuration windows
)
452 (message "Conflict resolution finished; you may save the buffer")))))
453 (message "Please resolve conflicts now; exit ediff when done")))
457 (define-minor-mode smerge-mode
458 "Minor mode to simplify editing output from the diff3 program.
464 (font-lock-add-keywords nil smerge-font-lock-keywords
'append
)
465 (font-lock-remove-keywords nil smerge-font-lock-keywords
))
466 (goto-char (point-min))
467 (while (smerge-find-conflict)
468 (font-lock-fontify-region (match-beginning 0) (match-end 0) nil
)))))
471 (provide 'smerge-mode
)
474 ;; $Log: smerge-mode.el,v $
475 ;; Revision 1.5 2000/08/16 19:51:55 monnier
476 ;; (smerge-mode-menu): Doc fix.
478 ;; Revision 1.4 2000/07/21 13:52:19 fx
479 ;; (smerge-mode-menu): Fill it out.
481 ;; Revision 1.3 2000/05/25 18:08:26 fx
482 ;; (smerge-diff-switches): Don't use list* in defcustom.
484 ;; Revision 1.2 2000/03/22 00:54:55 monnier
485 ;; (smerge-auto-leave): New function and variable.
486 ;; (smerge-basic-map): Rename from smerge-basic-keymap.
487 ;; Change the bindings for smerge-diff-*.
488 ;; (smerge-*-map): Use easy-mmode-defmap.
489 ;; (smerge-(next|prev)): Use easy-mmode-define-navigation.
490 ;; (smerge-keep-*): Use smerge-auto-leave.
492 ;; Revision 1.1 1999/12/09 13:00:21 monnier
493 ;; New file. Provides a simple minor-mode for files containing
494 ;; diff3-style conflict markers, such as generated by RCS
497 ;;; smerge-mode.el ends here