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.12 2001/03/07 00:16:29 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 ("=" .
,(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 (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 (file1 (make-temp-file "smerge1"))
354 (file2 (make-temp-file "smerge2"))
355 (dir default-directory
)
356 (file (file-relative-name buffer-file-name
)))
357 (write-region (match-beginning n1
) (match-end n1
) file1
)
358 (write-region (match-beginning n2
) (match-end n2
) file2
)
360 (with-current-buffer (get-buffer-create smerge-diff-buffer-name
)
361 (setq default-directory dir
)
362 (let ((inhibit-read-only t
))
364 (apply 'call-process diff-command nil t nil
365 (append smerge-diff-switches
366 (list "-L" (concat name1
"/" file
)
367 "-L" (concat name2
"/" file
)
369 (goto-char (point-min))
371 (display-buffer (current-buffer) t
))
373 (delete-file file2
))))
376 ;; compiler pacifiers
377 (defvar smerge-ediff-windows
)
378 (defvar smerge-ediff-buf
)
379 (defvar ediff-buffer-A
)
380 (defvar ediff-buffer-B
)
381 (defvar ediff-buffer-C
)
382 (unless (fboundp 'ediff-cleanup-mess
)
383 (autoload 'ediff-cleanup-mess
"ediff-util")))
385 (defun smerge-ediff ()
386 "Invoke ediff to resolve the conflicts."
388 (let* ((buf (current-buffer))
390 ;;(ediff-default-variant 'default-B)
391 (config (current-window-configuration))
392 (filename (file-name-nondirectory buffer-file-name
))
393 (mine (generate-new-buffer (concat "*" filename
" MINE*")))
394 (other (generate-new-buffer (concat "*" filename
" OTHER*")))
396 (with-current-buffer mine
397 (buffer-disable-undo)
398 (insert-buffer-substring buf
)
399 (goto-char (point-min))
400 (while (smerge-find-conflict)
401 (when (match-beginning 2) (setq base t
))
402 (replace-match (match-string 1) t t
))
404 (set-buffer-modified-p nil
)
407 (with-current-buffer other
408 (buffer-disable-undo)
409 (insert-buffer-substring buf
)
410 (goto-char (point-min))
411 (while (smerge-find-conflict)
412 (replace-match (match-string 3) t t
))
414 (set-buffer-modified-p nil
)
418 (setq base
(generate-new-buffer (concat "*" filename
" BASE*")))
419 (with-current-buffer base
420 (buffer-disable-undo)
421 (insert-buffer-substring buf
)
422 (goto-char (point-min))
423 (while (smerge-find-conflict)
424 (replace-match (or (match-string 2) "") t t
))
426 (set-buffer-modified-p nil
)
429 ;; the rest of the code is inspired from vc.el
433 (ediff-merge-buffers-with-ancestor mine other base
)
434 ;; nil 'ediff-merge-revisions-with-ancestor buffer-file-name)
435 (ediff-merge-buffers mine other
)))
436 ;; nil 'ediff-merge-revisions buffer-file-name)))
438 ;; Ediff is now set up, and we are in the control buffer.
439 ;; Do a few further adjustments and take precautions for exit.
440 (set (make-local-variable 'smerge-ediff-windows
) config
)
441 (set (make-local-variable 'smerge-ediff-buf
) buf
)
442 (set (make-local-variable 'ediff-quit-hook
)
444 (let ((buffer-A ediff-buffer-A
)
445 (buffer-B ediff-buffer-B
)
446 (buffer-C ediff-buffer-C
)
447 (buffer-Ancestor ediff-ancestor-buffer
)
448 (buf smerge-ediff-buf
)
449 (windows smerge-ediff-windows
))
451 (with-current-buffer buf
453 (insert-buffer buffer-C
)
454 (kill-buffer buffer-A
)
455 (kill-buffer buffer-B
)
456 (kill-buffer buffer-C
)
457 (when (bufferp buffer-Ancestor
) (kill-buffer buffer-Ancestor
))
458 (set-window-configuration windows
)
459 (message "Conflict resolution finished; you may save the buffer")))))
460 (message "Please resolve conflicts now; exit ediff when done")))
464 (define-minor-mode smerge-mode
465 "Minor mode to simplify editing output from the diff3 program.
469 (set (make-local-variable 'font-lock-multiline
) t
)
472 (font-lock-add-keywords nil smerge-font-lock-keywords
'append
)
473 (font-lock-remove-keywords nil smerge-font-lock-keywords
))
474 (goto-char (point-min))
475 (while (smerge-find-conflict)
477 (font-lock-fontify-region (match-beginning 0) (match-end 0) nil
))))))
480 (provide 'smerge-mode
)
481 ;;; smerge-mode.el ends here