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.15 2001/11/15 01:25:35 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
"*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 ("=" .
,(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
)
187 (unless (fboundp 'font-lock-fontify-region
)
188 (autoload 'font-lock-fontify-region
"font-lock")))
194 ;; Define smerge-next and smerge-prev
195 (easy-mmode-define-navigation smerge smerge-begin-re
"conflict")
197 (defconst smerge-match-names
["conflict" "mine" "base" "other"])
199 (defun smerge-ensure-match (n)
200 (unless (match-end n
)
201 (error (format "No `%s'" (aref smerge-match-names n
)))))
203 (defun smerge-auto-leave ()
204 (when (and smerge-auto-leave
205 (save-excursion (goto-char (point-min))
206 (not (re-search-forward smerge-begin-re nil t
))))
210 (defun smerge-keep-all ()
211 "Keep all three versions.
212 Convenient for the kind of conflicts that can arise in ChangeLog files."
214 (smerge-match-conflict)
215 (replace-match (concat (or (match-string 1) "")
216 (or (match-string 2) "")
217 (or (match-string 3) ""))
221 (defun smerge-keep-base ()
222 "Revert to the base version."
224 (smerge-match-conflict)
225 (smerge-ensure-match 2)
226 (replace-match (match-string 2) t t
)
229 (defun smerge-keep-other ()
230 "Use \"other\" version."
232 (smerge-match-conflict)
233 ;;(smerge-ensure-match 3)
234 (replace-match (match-string 3) t t
)
237 (defun smerge-keep-mine ()
240 (smerge-match-conflict)
241 ;;(smerge-ensure-match 1)
242 (replace-match (match-string 1) t t
)
245 (defun smerge-keep-current ()
246 "Use the current (under the cursor) version."
248 (smerge-match-conflict)
250 (while (or (not (match-end i
))
251 (< (point) (match-beginning i
))
252 (>= (point) (match-end i
)))
254 (if (<= i
0) (error "Not inside a version")
255 (replace-match (match-string i
) t t
)
256 (smerge-auto-leave))))
258 (defun smerge-diff-base-mine ()
259 "Diff 'base' and 'mine' version in current conflict region."
263 (defun smerge-diff-base-other ()
264 "Diff 'base' and 'other' version in current conflict region."
268 (defun smerge-diff-mine-other ()
269 "Diff 'mine' and 'other' version in current conflict region."
273 (defun smerge-match-conflict ()
274 "Get info about the conflict. Puts the info in the `match-data'.
275 The submatches contain:
276 0: the whole conflict.
280 An error is raised if not inside a conflict."
283 (let* ((orig-point (point))
286 (_ (re-search-backward smerge-begin-re
))
288 (start (match-beginning 0))
289 (mine-start (match-end 0))
290 (filename (match-string 1))
292 (_ (re-search-forward smerge-end-re
))
293 (_ (assert (< orig-point
(match-end 0))))
295 (other-end (match-beginning 0))
298 (_ (re-search-backward smerge-other-re start
))
300 (mine-end (match-beginning 0))
301 (other-start (match-end 0))
305 ;; handle the various conflict styles
307 ((re-search-backward smerge-base-re start t
)
308 ;; a 3-parts conflict
309 (set (make-local-variable 'smerge-conflict-style
) 'diff3-A
)
310 (setq base-end mine-end
)
311 (setq mine-end
(match-beginning 0))
312 (setq base-start
(match-end 0)))
314 ((string= filename
(file-name-nondirectory
315 (or buffer-file-name
"")))
316 ;; a 2-parts conflict
317 (set (make-local-variable 'smerge-conflict-style
) 'diff3-E
))
319 ((and (not base-start
)
320 (or (eq smerge-conflict-style
'diff3-A
)
321 (string-match "^[.0-9]+\\'" filename
)))
322 ;; a same-diff conflict
323 (setq base-start mine-start
)
324 (setq base-end mine-end
)
325 (setq mine-start other-start
)
326 (setq mine-end other-end
)))
328 (store-match-data (list start end
331 other-start other-end
332 (when base-start
(1- base-start
)) base-start
333 (1- other-start
) other-start
))
335 (search-failed (error "Point not in conflict region")))))
337 (defun smerge-find-conflict (&optional limit
)
338 "Find and match a conflict region. Intended as a font-lock MATCHER.
339 The submatches are the same as in `smerge-match-conflict'.
340 Returns non-nil if a match is found between the point and LIMIT.
341 The point is moved to the end of the conflict."
342 (when (re-search-forward smerge-begin-re limit t
)
344 (smerge-match-conflict)
345 (goto-char (match-end 0)))))
347 (defun smerge-diff (n1 n2
)
348 (smerge-match-conflict)
349 (smerge-ensure-match n1
)
350 (smerge-ensure-match n2
)
351 (let ((name1 (aref smerge-match-names n1
))
352 (name2 (aref smerge-match-names n2
))
353 ;; Read them before the match-data gets clobbered.
354 (beg1 (match-beginning n1
))
355 (end1 (match-end n1
))
356 (beg2 (match-beginning n2
))
357 (end2 (match-end n2
))
358 (file1 (make-temp-file "smerge1"))
359 (file2 (make-temp-file "smerge2"))
360 (dir default-directory
)
361 (file (file-relative-name buffer-file-name
))
362 (coding-system-for-read buffer-file-coding-system
))
363 (write-region beg1 end1 file1
)
364 (write-region beg2 end2 file2
)
366 (with-current-buffer (get-buffer-create smerge-diff-buffer-name
)
367 (setq default-directory dir
)
368 (let ((inhibit-read-only t
))
370 (apply 'call-process diff-command nil t nil
371 (append smerge-diff-switches
372 (list "-L" (concat name1
"/" file
)
373 "-L" (concat name2
"/" file
)
375 (goto-char (point-min))
377 (display-buffer (current-buffer) t
))
379 (delete-file file2
))))
382 ;; compiler pacifiers
383 (defvar smerge-ediff-windows
)
384 (defvar smerge-ediff-buf
)
385 (defvar ediff-buffer-A
)
386 (defvar ediff-buffer-B
)
387 (defvar ediff-buffer-C
)
388 (unless (fboundp 'ediff-cleanup-mess
)
389 (autoload 'ediff-cleanup-mess
"ediff-util")))
391 (defun smerge-ediff ()
392 "Invoke ediff to resolve the conflicts."
394 (let* ((buf (current-buffer))
396 ;;(ediff-default-variant 'default-B)
397 (config (current-window-configuration))
398 (filename (file-name-nondirectory buffer-file-name
))
399 (mine (generate-new-buffer (concat "*" filename
" MINE*")))
400 (other (generate-new-buffer (concat "*" filename
" OTHER*")))
402 (with-current-buffer mine
403 (buffer-disable-undo)
404 (insert-buffer-substring buf
)
405 (goto-char (point-min))
406 (while (smerge-find-conflict)
407 (when (match-beginning 2) (setq base t
))
408 (replace-match (match-string 1) t t
))
410 (set-buffer-modified-p nil
)
413 (with-current-buffer other
414 (buffer-disable-undo)
415 (insert-buffer-substring buf
)
416 (goto-char (point-min))
417 (while (smerge-find-conflict)
418 (replace-match (match-string 3) t t
))
420 (set-buffer-modified-p nil
)
424 (setq base
(generate-new-buffer (concat "*" filename
" BASE*")))
425 (with-current-buffer base
426 (buffer-disable-undo)
427 (insert-buffer-substring buf
)
428 (goto-char (point-min))
429 (while (smerge-find-conflict)
430 (replace-match (or (match-string 2) "") t t
))
432 (set-buffer-modified-p nil
)
435 ;; the rest of the code is inspired from vc.el
439 (ediff-merge-buffers-with-ancestor mine other base
)
440 ;; nil 'ediff-merge-revisions-with-ancestor buffer-file-name)
441 (ediff-merge-buffers mine other
)))
442 ;; nil 'ediff-merge-revisions buffer-file-name)))
444 ;; Ediff is now set up, and we are in the control buffer.
445 ;; Do a few further adjustments and take precautions for exit.
446 (set (make-local-variable 'smerge-ediff-windows
) config
)
447 (set (make-local-variable 'smerge-ediff-buf
) buf
)
448 (set (make-local-variable 'ediff-quit-hook
)
450 (let ((buffer-A ediff-buffer-A
)
451 (buffer-B ediff-buffer-B
)
452 (buffer-C ediff-buffer-C
)
453 (buffer-Ancestor ediff-ancestor-buffer
)
454 (buf smerge-ediff-buf
)
455 (windows smerge-ediff-windows
))
457 (with-current-buffer buf
459 (insert-buffer buffer-C
)
460 (kill-buffer buffer-A
)
461 (kill-buffer buffer-B
)
462 (kill-buffer buffer-C
)
463 (when (bufferp buffer-Ancestor
) (kill-buffer buffer-Ancestor
))
464 (set-window-configuration windows
)
465 (message "Conflict resolution finished; you may save the buffer")))))
466 (message "Please resolve conflicts now; exit ediff when done")))
470 (define-minor-mode smerge-mode
471 "Minor mode to simplify editing output from the diff3 program.
474 (when (and (boundp 'font-lock-mode
) font-lock-mode
)
475 (set (make-local-variable 'font-lock-multiline
) t
)
478 (font-lock-add-keywords nil smerge-font-lock-keywords
'append
)
479 (font-lock-remove-keywords nil smerge-font-lock-keywords
))
480 (goto-char (point-min))
481 (while (smerge-find-conflict)
483 (font-lock-fontify-region (match-beginning 0) (match-end 0) nil
))))))
486 (provide 'smerge-mode
)
487 ;;; smerge-mode.el ends here