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.3 2000/05/25 18:08:26 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'th 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
136 :help
"Use current (at point) version"]
138 ["Diff Base/Mine" smerge-diff-base-mine
139 :help
"Diff `base' and `mine' for current conflict"]
140 ["Diff Base/Other" smerge-diff-base-other
141 :help
"Diff `base' and `other' for current conflict"]
142 ["Diff Mine/Other" smerge-diff-mine-other
143 :help
"Diff `mine' and `other' for current conflict"]
145 ["Invoke Ediff" smerge-ediff
146 :help
"Use Ediff to resolve the conflicts"]
149 (defconst smerge-font-lock-keywords
150 '((smerge-find-conflict
151 (1 smerge-mine-face prepend
)
152 (2 smerge-base-face prepend t
)
153 (3 smerge-other-face prepend t
)
154 (0 smerge-markers-face keep
)
157 "Font lock patterns for `smerge-mode'.")
159 (defconst smerge-begin-re
"^<<<<<<< \\(.*\\)\n")
160 (defconst smerge-end-re
"^>>>>>>> .*\n")
161 (defconst smerge-base-re
"^||||||| .*\n")
162 (defconst smerge-other-re
"^=======\n")
164 (defvar smerge-conflict-style nil
165 "Keep track of which style of conflict is in use.
166 Can be nil if the style is undecided, or else:
170 ;; Compiler pacifiers
171 (defvar font-lock-mode nil
)
172 (defvar font-lock-keywords nil
)
174 (unless (fboundp 'font-lock-fontify-region
)
175 (autoload 'font-lock-fontify-region
"font-lock")))
181 ;; Define smerge-next and smerge-prev
182 (easy-mmode-define-navigation smerge smerge-begin-re
"conflict")
184 (defconst smerge-match-names
["conflict" "mine" "base" "other"])
186 (defun smerge-ensure-match (n)
187 (unless (match-end n
)
188 (error (format "No `%s'" (aref smerge-match-names n
)))))
190 (defun smerge-auto-leave ()
191 (when (and smerge-auto-leave
192 (save-excursion (goto-char (point-min))
193 (not (re-search-forward smerge-begin-re nil t
))))
197 (defun smerge-keep-all ()
198 "Keep all three versions.
199 Convenient for the kind of conflicts that can arise in ChangeLog files."
201 (smerge-match-conflict)
202 (replace-match (concat (or (match-string 1) "")
203 (or (match-string 2) "")
204 (or (match-string 3) ""))
208 (defun smerge-keep-base ()
209 "Revert to the base version."
211 (smerge-match-conflict)
212 (smerge-ensure-match 2)
213 (replace-match (match-string 2) t t
)
216 (defun smerge-keep-other ()
217 "Use \"other\" version."
219 (smerge-match-conflict)
220 ;;(smerge-ensure-match 3)
221 (replace-match (match-string 3) t t
)
224 (defun smerge-keep-mine ()
227 (smerge-match-conflict)
228 ;;(smerge-ensure-match 1)
229 (replace-match (match-string 1) t t
)
232 (defun smerge-keep-current ()
233 "Use the current (under the cursor) version."
235 (smerge-match-conflict)
237 (while (or (not (match-end i
))
238 (< (point) (match-beginning i
))
239 (>= (point) (match-end i
)))
241 (if (<= i
0) (error "Not inside a version")
242 (replace-match (match-string i
) t t
)
243 (smerge-auto-leave))))
245 (defun smerge-diff-base-mine ()
246 "Diff 'base' and 'mine' version in current conflict region."
250 (defun smerge-diff-base-other ()
251 "Diff 'base' and 'other' version in current conflict region."
255 (defun smerge-diff-mine-other ()
256 "Diff 'mine' and 'other' version in current conflict region."
260 (defun smerge-match-conflict ()
261 "Get info about the conflict. Puts the info in the `match-data'.
262 The submatches contain:
263 0: the whole conflict.
267 An error is raised if not inside a conflict."
270 (let* ((orig-point (point))
273 (_ (re-search-backward smerge-begin-re
))
275 (start (match-beginning 0))
276 (mine-start (match-end 0))
277 (filename (match-string 1))
279 (_ (re-search-forward smerge-end-re
))
280 (_ (assert (< orig-point
(match-end 0))))
282 (other-end (match-beginning 0))
285 (_ (re-search-backward smerge-other-re start
))
287 (mine-end (match-beginning 0))
288 (other-start (match-end 0))
292 ;; handle the various conflict styles
294 ((re-search-backward smerge-base-re start t
)
295 ;; a 3-parts conflict
296 (set (make-local-variable 'smerge-conflict-style
) 'diff3-A
)
297 (setq base-end mine-end
)
298 (setq mine-end
(match-beginning 0))
299 (setq base-start
(match-end 0)))
301 ((string= filename
(file-name-nondirectory
302 (or buffer-file-name
"")))
303 ;; a 2-parts conflict
304 (set (make-local-variable 'smerge-conflict-style
) 'diff3-E
))
306 ((and (not base-start
)
307 (or (eq smerge-conflict-style
'diff3-A
)
308 (string-match "^[.0-9]+\\'" filename
)))
309 ;; a same-diff conflict
310 (setq base-start mine-start
)
311 (setq base-end mine-end
)
312 (setq mine-start other-start
)
313 (setq mine-end other-end
)))
315 (store-match-data (list start end
318 other-start other-end
319 (when base-start
(1- base-start
)) base-start
320 (1- other-start
) other-start
))
322 (error "Point not in conflict region"))))
324 (defun smerge-find-conflict (&optional limit
)
325 "Find and match a conflict region. Intended as a font-lock MATCHER.
326 The submatches are the same as in `smerge-match-conflict'.
327 Returns non-nil if a match is found between the point and LIMIT.
328 The point is moved to the end of the conflict."
329 (when (re-search-forward smerge-begin-re limit t
)
331 (smerge-match-conflict)
332 (goto-char (match-end 0)))))
334 (defun smerge-diff (n1 n2
)
335 (smerge-match-conflict)
336 (smerge-ensure-match n1
)
337 (smerge-ensure-match n2
)
338 (let ((name1 (aref smerge-match-names n1
))
339 (name2 (aref smerge-match-names n2
))
340 (file1 (make-temp-file "smerge1"))
341 (file2 (make-temp-file "smerge2")))
342 (write-region (match-beginning n1
) (match-end n1
) file1
)
343 (write-region (match-beginning n2
) (match-end n2
) file2
)
345 (with-current-buffer (get-buffer-create smerge-diff-buffer-name
)
346 (let ((inhibit-read-only t
))
348 (apply 'call-process diff-command nil t nil
349 (append smerge-diff-switches
350 (list "-L" name1
"-L" name2 file1 file2
))))
351 (goto-char (point-min))
353 (display-buffer (current-buffer) t
))
355 (delete-file file2
))))
358 ;; compiler pacifiers
359 (defvar smerge-ediff-windows
)
360 (defvar smerge-ediff-buf
)
361 (defvar ediff-buffer-A
)
362 (defvar ediff-buffer-B
)
363 (defvar ediff-buffer-C
)
364 (unless (fboundp 'ediff-cleanup-mess
)
365 (autoload 'ediff-cleanup-mess
"ediff-util")))
367 (defun smerge-ediff ()
368 "Invoke ediff to resolve the conflicts."
370 (let* ((buf (current-buffer))
372 ;;(ediff-default-variant 'default-B)
373 (config (current-window-configuration))
374 (filename (file-name-nondirectory buffer-file-name
))
375 (mine (generate-new-buffer (concat "*" filename
" MINE*")))
376 (other (generate-new-buffer (concat "*" filename
" OTHER*")))
378 (with-current-buffer mine
379 (buffer-disable-undo)
380 (insert-buffer-substring buf
)
381 (goto-char (point-min))
382 (while (smerge-find-conflict)
383 (when (match-beginning 2) (setq base t
))
384 (replace-match (match-string 1) t t
))
386 (set-buffer-modified-p nil
)
389 (with-current-buffer other
390 (buffer-disable-undo)
391 (insert-buffer-substring buf
)
392 (goto-char (point-min))
393 (while (smerge-find-conflict)
394 (replace-match (match-string 3) t t
))
396 (set-buffer-modified-p nil
)
400 (setq base
(generate-new-buffer (concat "*" filename
" BASE*")))
401 (with-current-buffer base
402 (buffer-disable-undo)
403 (insert-buffer-substring buf
)
404 (goto-char (point-min))
405 (while (smerge-find-conflict)
406 (replace-match (or (match-string 2) "") t t
))
408 (set-buffer-modified-p nil
)
411 ;; the rest of the code is inspired from vc.el
415 (ediff-merge-buffers-with-ancestor mine other base
)
416 ;; nil 'ediff-merge-revisions-with-ancestor buffer-file-name)
417 (ediff-merge-buffers mine other
)))
418 ;; nil 'ediff-merge-revisions buffer-file-name)))
420 ;; Ediff is now set up, and we are in the control buffer.
421 ;; Do a few further adjustments and take precautions for exit.
422 (set (make-local-variable 'smerge-ediff-windows
) config
)
423 (set (make-local-variable 'smerge-ediff-buf
) buf
)
424 (set (make-local-variable 'ediff-quit-hook
)
426 (let ((buffer-A ediff-buffer-A
)
427 (buffer-B ediff-buffer-B
)
428 (buffer-C ediff-buffer-C
)
429 (buffer-Ancestor ediff-ancestor-buffer
)
430 (buf smerge-ediff-buf
)
431 (windows smerge-ediff-windows
))
433 (with-current-buffer buf
435 (insert-buffer buffer-C
)
436 (kill-buffer buffer-A
)
437 (kill-buffer buffer-B
)
438 (kill-buffer buffer-C
)
439 (when (bufferp buffer-Ancestor
) (kill-buffer buffer-Ancestor
))
440 (set-window-configuration windows
)
441 (message "Conflict resolution finished; you may save the buffer")))))
442 (message "Please resolve conflicts now; exit ediff when done")))
446 (define-minor-mode smerge-mode
447 "Minor mode to simplify editing output from the diff3 program.
453 (font-lock-add-keywords nil smerge-font-lock-keywords
'append
)
454 (font-lock-remove-keywords nil smerge-font-lock-keywords
))
455 (goto-char (point-min))
456 (while (smerge-find-conflict)
457 (font-lock-fontify-region (match-beginning 0) (match-end 0) nil
)))))
460 (provide 'smerge-mode
)
463 ;; $Log: smerge-mode.el,v $
464 ;; Revision 1.3 2000/05/25 18:08:26 fx
465 ;; (smerge-diff-switches): Don't use list* in defcustom.
467 ;; Revision 1.2 2000/03/22 00:54:55 monnier
468 ;; (smerge-auto-leave): New function and variable.
469 ;; (smerge-basic-map): Rename from smerge-basic-keymap.
470 ;; Change the bindings for smerge-diff-*.
471 ;; (smerge-*-map): Use easy-mmode-defmap.
472 ;; (smerge-(next|prev)): Use easy-mmode-define-navigation.
473 ;; (smerge-keep-*): Use smerge-auto-leave.
475 ;; Revision 1.1 1999/12/09 13:00:21 monnier
476 ;; New file. Provides a simple minor-mode for files containing
477 ;; diff3-style conflict markers, such as generated by RCS
480 ;;; smerge-mode.el ends here