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.1 1999/12/09 13:00:21 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 '((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 ["Invoke Ediff" smerge-ediff t
]
132 (defconst smerge-font-lock-keywords
133 '((smerge-find-conflict
134 (1 smerge-mine-face prepend
)
135 (2 smerge-base-face prepend t
)
136 (3 smerge-other-face prepend t
)
137 (0 smerge-markers-face keep
)
140 "Font lock patterns for `smerge-mode'.")
142 (defconst smerge-begin-re
"^<<<<<<< \\(.*\\)\n")
143 (defconst smerge-end-re
"^>>>>>>> .*\n")
144 (defconst smerge-base-re
"^||||||| .*\n")
145 (defconst smerge-other-re
"^=======\n")
147 (defvar smerge-conflict-style nil
148 "Keep track of which style of conflict is in use.
149 Can be nil if the style is undecided, or else:
153 ;; Compiler pacifiers
154 (defvar font-lock-mode nil
)
155 (defvar font-lock-keywords nil
)
157 (unless (fboundp 'font-lock-fontify-region
)
158 (autoload 'font-lock-fontify-region
"font-lock")))
164 ;; Define smerge-next and smerge-prev
165 (easy-mmode-define-navigation smerge smerge-begin-re
"conflict")
167 (defconst smerge-match-names
["conflict" "mine" "base" "other"])
169 (defun smerge-ensure-match (n)
170 (unless (match-end n
)
171 (error (format "No `%s'" (aref smerge-match-names n
)))))
173 (defun smerge-auto-leave ()
174 (when (and smerge-auto-leave
175 (save-excursion (goto-char (point-min))
176 (not (re-search-forward smerge-begin-re nil t
))))
180 (defun smerge-keep-all ()
181 "Keep all three versions.
182 Convenient for the kind of conflicts that can arise in ChangeLog files."
184 (smerge-match-conflict)
185 (replace-match (concat (or (match-string 1) "")
186 (or (match-string 2) "")
187 (or (match-string 3) ""))
191 (defun smerge-keep-base ()
192 "Revert to the base version."
194 (smerge-match-conflict)
195 (smerge-ensure-match 2)
196 (replace-match (match-string 2) t t
)
199 (defun smerge-keep-other ()
200 "Use \"other\" version."
202 (smerge-match-conflict)
203 ;;(smerge-ensure-match 3)
204 (replace-match (match-string 3) t t
)
207 (defun smerge-keep-mine ()
210 (smerge-match-conflict)
211 ;;(smerge-ensure-match 1)
212 (replace-match (match-string 1) t t
)
215 (defun smerge-keep-current ()
216 "Use the current (under the cursor) version."
218 (smerge-match-conflict)
220 (while (or (not (match-end i
))
221 (< (point) (match-beginning i
))
222 (>= (point) (match-end i
)))
224 (if (<= i
0) (error "Not inside a version")
225 (replace-match (match-string i
) t t
)
226 (smerge-auto-leave))))
228 (defun smerge-diff-base-mine ()
229 "Diff 'base' and 'mine' version in current conflict region."
233 (defun smerge-diff-base-other ()
234 "Diff 'base' and 'other' version in current conflict region."
238 (defun smerge-diff-mine-other ()
239 "Diff 'mine' and 'other' version in current conflict region."
243 (defun smerge-match-conflict ()
244 "Get info about the conflict. Puts the info in the `match-data'.
245 The submatches contain:
246 0: the whole conflict.
250 An error is raised if not inside a conflict."
253 (let* ((orig-point (point))
256 (_ (re-search-backward smerge-begin-re
))
258 (start (match-beginning 0))
259 (mine-start (match-end 0))
260 (filename (match-string 1))
262 (_ (re-search-forward smerge-end-re
))
263 (_ (assert (< orig-point
(match-end 0))))
265 (other-end (match-beginning 0))
268 (_ (re-search-backward smerge-other-re start
))
270 (mine-end (match-beginning 0))
271 (other-start (match-end 0))
275 ;; handle the various conflict styles
277 ((re-search-backward smerge-base-re start t
)
278 ;; a 3-parts conflict
279 (set (make-local-variable 'smerge-conflict-style
) 'diff3-A
)
280 (setq base-end mine-end
)
281 (setq mine-end
(match-beginning 0))
282 (setq base-start
(match-end 0)))
284 ((string= filename
(file-name-nondirectory
285 (or buffer-file-name
"")))
286 ;; a 2-parts conflict
287 (set (make-local-variable 'smerge-conflict-style
) 'diff3-E
))
289 ((and (not base-start
)
290 (or (eq smerge-conflict-style
'diff3-A
)
291 (string-match "^[.0-9]+\\'" filename
)))
292 ;; a same-diff conflict
293 (setq base-start mine-start
)
294 (setq base-end mine-end
)
295 (setq mine-start other-start
)
296 (setq mine-end other-end
)))
298 (store-match-data (list start end
301 other-start other-end
302 (when base-start
(1- base-start
)) base-start
303 (1- other-start
) other-start
))
305 (error "Point not in conflict region"))))
307 (defun smerge-find-conflict (&optional limit
)
308 "Find and match a conflict region. Intended as a font-lock MATCHER.
309 The submatches are the same as in `smerge-match-conflict'.
310 Returns non-nil if a match is found between the point and LIMIT.
311 The point is moved to the end of the conflict."
312 (when (re-search-forward smerge-begin-re limit t
)
314 (smerge-match-conflict)
315 (goto-char (match-end 0)))))
317 (defun smerge-diff (n1 n2
)
318 (smerge-match-conflict)
319 (smerge-ensure-match n1
)
320 (smerge-ensure-match n2
)
321 (let ((name1 (aref smerge-match-names n1
))
322 (name2 (aref smerge-match-names n2
))
323 (file1 (make-temp-file "smerge1"))
324 (file2 (make-temp-file "smerge2")))
325 (write-region (match-beginning n1
) (match-end n1
) file1
)
326 (write-region (match-beginning n2
) (match-end n2
) file2
)
328 (with-current-buffer (get-buffer-create smerge-diff-buffer-name
)
329 (let ((inhibit-read-only t
))
331 (apply 'call-process diff-command nil t nil
332 (append smerge-diff-switches
333 (list "-L" name1
"-L" name2 file1 file2
))))
334 (goto-char (point-min))
336 (display-buffer (current-buffer) t
))
338 (delete-file file2
))))
341 ;; compiler pacifiers
342 (defvar smerge-ediff-windows
)
343 (defvar smerge-ediff-buf
)
344 (defvar ediff-buffer-A
)
345 (defvar ediff-buffer-B
)
346 (defvar ediff-buffer-C
)
347 (unless (fboundp 'ediff-cleanup-mess
)
348 (autoload 'ediff-cleanup-mess
"ediff-util")))
350 (defun smerge-ediff ()
351 "Invoke ediff to resolve the conflicts."
353 (let* ((buf (current-buffer))
355 ;;(ediff-default-variant 'default-B)
356 (config (current-window-configuration))
357 (filename (file-name-nondirectory buffer-file-name
))
358 (mine (generate-new-buffer (concat "*" filename
" MINE*")))
359 (other (generate-new-buffer (concat "*" filename
" OTHER*")))
361 (with-current-buffer mine
362 (buffer-disable-undo)
363 (insert-buffer-substring buf
)
364 (goto-char (point-min))
365 (while (smerge-find-conflict)
366 (when (match-beginning 2) (setq base t
))
367 (replace-match (match-string 1) t t
))
369 (set-buffer-modified-p nil
)
372 (with-current-buffer other
373 (buffer-disable-undo)
374 (insert-buffer-substring buf
)
375 (goto-char (point-min))
376 (while (smerge-find-conflict)
377 (replace-match (match-string 3) t t
))
379 (set-buffer-modified-p nil
)
383 (setq base
(generate-new-buffer (concat "*" filename
" BASE*")))
384 (with-current-buffer base
385 (buffer-disable-undo)
386 (insert-buffer-substring buf
)
387 (goto-char (point-min))
388 (while (smerge-find-conflict)
389 (replace-match (or (match-string 2) "") t t
))
391 (set-buffer-modified-p nil
)
394 ;; the rest of the code is inspired from vc.el
398 (ediff-merge-buffers-with-ancestor mine other base
)
399 ;; nil 'ediff-merge-revisions-with-ancestor buffer-file-name)
400 (ediff-merge-buffers mine other
)))
401 ;; nil 'ediff-merge-revisions buffer-file-name)))
403 ;; Ediff is now set up, and we are in the control buffer.
404 ;; Do a few further adjustments and take precautions for exit.
405 (set (make-local-variable 'smerge-ediff-windows
) config
)
406 (set (make-local-variable 'smerge-ediff-buf
) buf
)
407 (set (make-local-variable 'ediff-quit-hook
)
409 (let ((buffer-A ediff-buffer-A
)
410 (buffer-B ediff-buffer-B
)
411 (buffer-C ediff-buffer-C
)
412 (buffer-Ancestor ediff-ancestor-buffer
)
413 (buf smerge-ediff-buf
)
414 (windows smerge-ediff-windows
))
416 (with-current-buffer buf
418 (insert-buffer buffer-C
)
419 (kill-buffer buffer-A
)
420 (kill-buffer buffer-B
)
421 (kill-buffer buffer-C
)
422 (when (bufferp buffer-Ancestor
) (kill-buffer buffer-Ancestor
))
423 (set-window-configuration windows
)
424 (message "Conflict resolution finished; you may save the buffer")))))
425 (message "Please resolve conflicts now; exit ediff when done")))
429 (define-minor-mode smerge-mode
430 "Minor mode to simplify editing output from the diff3 program.
436 (font-lock-add-keywords nil smerge-font-lock-keywords
'append
)
437 (font-lock-remove-keywords nil smerge-font-lock-keywords
))
438 (goto-char (point-min))
439 (while (smerge-find-conflict)
440 (font-lock-fontify-region (match-beginning 0) (match-end 0) nil
)))))
443 (provide 'smerge-mode
)
446 ;; $Log: smerge-mode.el,v $
447 ;; Revision 1.1 1999/12/09 13:00:21 monnier
448 ;; New file. Provides a simple minor-mode for files containing
449 ;; diff3-style conflict markers, such as generated by RCS
452 ;;; smerge-mode.el ends here