1 ;;; smerge-mode.el --- Minor mode to resolve diff3 conflicts
3 ;; Copyright (C) 1999 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.4 2000/07/21 13:52:19 fx 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 '((t (:foreground
"blue")))
83 (defvar smerge-mine-face
'smerge-mine-face
)
85 (defface smerge-other-face
86 '((t (:foreground
"darkgreen")))
87 "Face for the other code."
89 (defvar smerge-other-face
'smerge-other-face
)
91 (defface smerge-base-face
92 '((t (:foreground
"red")))
93 "Face for the base code."
95 (defvar smerge-base-face
'smerge-base-face
)
97 (defface smerge-markers-face
98 '((t (:background
"grey85")))
99 "Face for the conflict markers."
101 (defvar smerge-markers-face
'smerge-markers-face
)
103 (easy-mmode-defmap smerge-basic-map
104 '(("n" . smerge-next
)
106 ("a" . smerge-keep-all
)
107 ("b" . smerge-keep-base
)
108 ("o" . smerge-keep-other
)
109 ("m" . smerge-keep-mine
)
111 ("\C-m" . smerge-keep-current
)
112 ("d<" . smerge-diff-base-mine
)
113 ("d>" . smerge-diff-base-other
)
114 ("d=" . smerge-diff-mine-other
))
115 "The base keymap for `smerge-mode'.")
117 (defcustom smerge-command-prefix
"\e"
118 "Prefix for `smerge-mode' commands."
120 :type
'(choice (string "\e") (string "C-x^") (string "") string
))
122 (easy-mmode-defmap smerge-mode-map
123 `((,smerge-command-prefix .
,smerge-basic-map
))
124 "Keymap for `smerge-mode'.")
126 (easy-menu-define smerge-mode-menu smerge-mode-map
127 "Menu for `smerge-mode'."
129 ["Next" smerge-next
:help
"Go to next conflict"]
130 ["Previous" smerge-prev
:help
"Go to previous conflict"]
131 ["Keep All" smerge-keep-all
:help
"Keep all three versions"]
132 ["Revert to Base" smerge-keep-base
:help
"Revert to base version"]
133 ["Keep Other" smerge-keep-other
:help
"Keep `other' version"]
134 ["Keep Yours" smerge-keep-mine
:help
"Keep your version"]
135 ["Keep Current" smerge-keep-current
:help
"Use current (at point) version"]
137 ["Diff Base/Mine" smerge-diff-base-mine
138 :help
"Diff `base' and `mine' for current conflict"]
139 ["Diff Base/Other" smerge-diff-base-other
140 :help
"Diff `base' and `other' for current conflict"]
141 ["Diff Mine/Other" smerge-diff-mine-other
142 :help
"Diff `mine' and `other' for current conflict"]
144 ["Invoke Ediff" smerge-ediff
145 :help
"Use Ediff to resolve the conflicts"]
148 (defconst smerge-font-lock-keywords
149 '((smerge-find-conflict
150 (1 smerge-mine-face prepend
)
151 (2 smerge-base-face prepend t
)
152 (3 smerge-other-face prepend t
)
153 (0 smerge-markers-face keep
)
156 "Font lock patterns for `smerge-mode'.")
158 (defconst smerge-begin-re
"^<<<<<<< \\(.*\\)\n")
159 (defconst smerge-end-re
"^>>>>>>> .*\n")
160 (defconst smerge-base-re
"^||||||| .*\n")
161 (defconst smerge-other-re
"^=======\n")
163 (defvar smerge-conflict-style nil
164 "Keep track of which style of conflict is in use.
165 Can be nil if the style is undecided, or else:
169 ;; Compiler pacifiers
170 (defvar font-lock-mode nil
)
171 (defvar font-lock-keywords nil
)
173 (unless (fboundp 'font-lock-fontify-region
)
174 (autoload 'font-lock-fontify-region
"font-lock")))
180 ;; Define smerge-next and smerge-prev
181 (easy-mmode-define-navigation smerge smerge-begin-re
"conflict")
183 (defconst smerge-match-names
["conflict" "mine" "base" "other"])
185 (defun smerge-ensure-match (n)
186 (unless (match-end n
)
187 (error (format "No `%s'" (aref smerge-match-names n
)))))
189 (defun smerge-auto-leave ()
190 (when (and smerge-auto-leave
191 (save-excursion (goto-char (point-min))
192 (not (re-search-forward smerge-begin-re nil t
))))
196 (defun smerge-keep-all ()
197 "Keep all three versions.
198 Convenient for the kind of conflicts that can arise in ChangeLog files."
200 (smerge-match-conflict)
201 (replace-match (concat (or (match-string 1) "")
202 (or (match-string 2) "")
203 (or (match-string 3) ""))
207 (defun smerge-keep-base ()
208 "Revert to the base version."
210 (smerge-match-conflict)
211 (smerge-ensure-match 2)
212 (replace-match (match-string 2) t t
)
215 (defun smerge-keep-other ()
216 "Use \"other\" version."
218 (smerge-match-conflict)
219 ;;(smerge-ensure-match 3)
220 (replace-match (match-string 3) t t
)
223 (defun smerge-keep-mine ()
226 (smerge-match-conflict)
227 ;;(smerge-ensure-match 1)
228 (replace-match (match-string 1) t t
)
231 (defun smerge-keep-current ()
232 "Use the current (under the cursor) version."
234 (smerge-match-conflict)
236 (while (or (not (match-end i
))
237 (< (point) (match-beginning i
))
238 (>= (point) (match-end i
)))
240 (if (<= i
0) (error "Not inside a version")
241 (replace-match (match-string i
) t t
)
242 (smerge-auto-leave))))
244 (defun smerge-diff-base-mine ()
245 "Diff 'base' and 'mine' version in current conflict region."
249 (defun smerge-diff-base-other ()
250 "Diff 'base' and 'other' version in current conflict region."
254 (defun smerge-diff-mine-other ()
255 "Diff 'mine' and 'other' version in current conflict region."
259 (defun smerge-match-conflict ()
260 "Get info about the conflict. Puts the info in the `match-data'.
261 The submatches contain:
262 0: the whole conflict.
266 An error is raised if not inside a conflict."
269 (let* ((orig-point (point))
272 (_ (re-search-backward smerge-begin-re
))
274 (start (match-beginning 0))
275 (mine-start (match-end 0))
276 (filename (match-string 1))
278 (_ (re-search-forward smerge-end-re
))
279 (_ (assert (< orig-point
(match-end 0))))
281 (other-end (match-beginning 0))
284 (_ (re-search-backward smerge-other-re start
))
286 (mine-end (match-beginning 0))
287 (other-start (match-end 0))
291 ;; handle the various conflict styles
293 ((re-search-backward smerge-base-re start t
)
294 ;; a 3-parts conflict
295 (set (make-local-variable 'smerge-conflict-style
) 'diff3-A
)
296 (setq base-end mine-end
)
297 (setq mine-end
(match-beginning 0))
298 (setq base-start
(match-end 0)))
300 ((string= filename
(file-name-nondirectory
301 (or buffer-file-name
"")))
302 ;; a 2-parts conflict
303 (set (make-local-variable 'smerge-conflict-style
) 'diff3-E
))
305 ((and (not base-start
)
306 (or (eq smerge-conflict-style
'diff3-A
)
307 (string-match "^[.0-9]+\\'" filename
)))
308 ;; a same-diff conflict
309 (setq base-start mine-start
)
310 (setq base-end mine-end
)
311 (setq mine-start other-start
)
312 (setq mine-end other-end
)))
314 (store-match-data (list start end
317 other-start other-end
318 (when base-start
(1- base-start
)) base-start
319 (1- other-start
) other-start
))
321 (error "Point not in conflict region"))))
323 (defun smerge-find-conflict (&optional limit
)
324 "Find and match a conflict region. Intended as a font-lock MATCHER.
325 The submatches are the same as in `smerge-match-conflict'.
326 Returns non-nil if a match is found between the point and LIMIT.
327 The point is moved to the end of the conflict."
328 (when (re-search-forward smerge-begin-re limit t
)
330 (smerge-match-conflict)
331 (goto-char (match-end 0)))))
333 (defun smerge-diff (n1 n2
)
334 (smerge-match-conflict)
335 (smerge-ensure-match n1
)
336 (smerge-ensure-match n2
)
337 (let ((name1 (aref smerge-match-names n1
))
338 (name2 (aref smerge-match-names n2
))
339 (file1 (make-temp-file "smerge1"))
340 (file2 (make-temp-file "smerge2")))
341 (write-region (match-beginning n1
) (match-end n1
) file1
)
342 (write-region (match-beginning n2
) (match-end n2
) file2
)
344 (with-current-buffer (get-buffer-create smerge-diff-buffer-name
)
345 (let ((inhibit-read-only t
))
347 (apply 'call-process diff-command nil t nil
348 (append smerge-diff-switches
349 (list "-L" name1
"-L" name2 file1 file2
))))
350 (goto-char (point-min))
352 (display-buffer (current-buffer) t
))
354 (delete-file file2
))))
357 ;; compiler pacifiers
358 (defvar smerge-ediff-windows
)
359 (defvar smerge-ediff-buf
)
360 (defvar ediff-buffer-A
)
361 (defvar ediff-buffer-B
)
362 (defvar ediff-buffer-C
)
363 (unless (fboundp 'ediff-cleanup-mess
)
364 (autoload 'ediff-cleanup-mess
"ediff-util")))
366 (defun smerge-ediff ()
367 "Invoke ediff to resolve the conflicts."
369 (let* ((buf (current-buffer))
371 ;;(ediff-default-variant 'default-B)
372 (config (current-window-configuration))
373 (filename (file-name-nondirectory buffer-file-name
))
374 (mine (generate-new-buffer (concat "*" filename
" MINE*")))
375 (other (generate-new-buffer (concat "*" filename
" OTHER*")))
377 (with-current-buffer mine
378 (buffer-disable-undo)
379 (insert-buffer-substring buf
)
380 (goto-char (point-min))
381 (while (smerge-find-conflict)
382 (when (match-beginning 2) (setq base t
))
383 (replace-match (match-string 1) t t
))
385 (set-buffer-modified-p nil
)
388 (with-current-buffer other
389 (buffer-disable-undo)
390 (insert-buffer-substring buf
)
391 (goto-char (point-min))
392 (while (smerge-find-conflict)
393 (replace-match (match-string 3) t t
))
395 (set-buffer-modified-p nil
)
399 (setq base
(generate-new-buffer (concat "*" filename
" BASE*")))
400 (with-current-buffer base
401 (buffer-disable-undo)
402 (insert-buffer-substring buf
)
403 (goto-char (point-min))
404 (while (smerge-find-conflict)
405 (replace-match (or (match-string 2) "") t t
))
407 (set-buffer-modified-p nil
)
410 ;; the rest of the code is inspired from vc.el
414 (ediff-merge-buffers-with-ancestor mine other base
)
415 ;; nil 'ediff-merge-revisions-with-ancestor buffer-file-name)
416 (ediff-merge-buffers mine other
)))
417 ;; nil 'ediff-merge-revisions buffer-file-name)))
419 ;; Ediff is now set up, and we are in the control buffer.
420 ;; Do a few further adjustments and take precautions for exit.
421 (set (make-local-variable 'smerge-ediff-windows
) config
)
422 (set (make-local-variable 'smerge-ediff-buf
) buf
)
423 (set (make-local-variable 'ediff-quit-hook
)
425 (let ((buffer-A ediff-buffer-A
)
426 (buffer-B ediff-buffer-B
)
427 (buffer-C ediff-buffer-C
)
428 (buffer-Ancestor ediff-ancestor-buffer
)
429 (buf smerge-ediff-buf
)
430 (windows smerge-ediff-windows
))
432 (with-current-buffer buf
434 (insert-buffer buffer-C
)
435 (kill-buffer buffer-A
)
436 (kill-buffer buffer-B
)
437 (kill-buffer buffer-C
)
438 (when (bufferp buffer-Ancestor
) (kill-buffer buffer-Ancestor
))
439 (set-window-configuration windows
)
440 (message "Conflict resolution finished; you may save the buffer")))))
441 (message "Please resolve conflicts now; exit ediff when done")))
445 (define-minor-mode smerge-mode
446 "Minor mode to simplify editing output from the diff3 program.
452 (font-lock-add-keywords nil smerge-font-lock-keywords
'append
)
453 (font-lock-remove-keywords nil smerge-font-lock-keywords
))
454 (goto-char (point-min))
455 (while (smerge-find-conflict)
456 (font-lock-fontify-region (match-beginning 0) (match-end 0) nil
)))))
459 (provide 'smerge-mode
)
462 ;; $Log: smerge-mode.el,v $
463 ;; Revision 1.4 2000/07/21 13:52:19 fx
464 ;; (smerge-mode-menu): Fill it out.
466 ;; Revision 1.3 2000/05/25 18:08:26 fx
467 ;; (smerge-diff-switches): Don't use list* in defcustom.
469 ;; Revision 1.2 2000/03/22 00:54:55 monnier
470 ;; (smerge-auto-leave): New function and variable.
471 ;; (smerge-basic-map): Rename from smerge-basic-keymap.
472 ;; Change the bindings for smerge-diff-*.
473 ;; (smerge-*-map): Use easy-mmode-defmap.
474 ;; (smerge-(next|prev)): Use easy-mmode-define-navigation.
475 ;; (smerge-keep-*): Use smerge-auto-leave.
477 ;; Revision 1.1 1999/12/09 13:00:21 monnier
478 ;; New file. Provides a simple minor-mode for files containing
479 ;; diff3-style conflict markers, such as generated by RCS
482 ;;; smerge-mode.el ends here