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
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 (eval-when-compile (require 'cl
))
49 "Minor mode to resolve diff3 conflicts."
53 (defcustom smerge-diff-buffer-name
"*smerge-diff*"
54 "Buffer name to use for displaying diffs."
59 (const "*smerge-diff*")
62 (defcustom smerge-diff-switches
64 (if (listp diff-switches
) diff-switches
(list diff-switches
)))
65 "*A list of strings specifying switches to be be passed to diff.
66 Used in `smerge-diff-base-mine' and related functions."
68 :type
'(repeat string
))
70 (defface smerge-mine-face
71 '((t (:foreground
"blue")))
74 (defvar smerge-mine-face
'smerge-mine-face
)
76 (defface smerge-other-face
77 '((t (:foreground
"darkgreen")))
78 "Face for the other code."
80 (defvar smerge-other-face
'smerge-other-face
)
82 (defface smerge-base-face
83 '((t (:foreground
"red")))
84 "Face for the base code."
86 (defvar smerge-base-face
'smerge-base-face
)
88 (defface smerge-markers-face
89 '((t (:background
"grey85")))
90 "Face for the conflict markers."
92 (defvar smerge-markers-face
'smerge-markers-face
)
94 (defmacro smerge-defmap
(var bindings
&optional doc
)
96 (let ((m (make-sparse-keymap)))
98 (if (equal (car b
) "")
99 (set-keymap-parent m
(cdr b
))
100 (define-key m
(car b
) (cdr b
))))
104 (smerge-defmap smerge-basic-keymap
105 '(("n" . smerge-next
)
107 ("a" . smerge-keep-all
)
108 ("b" . smerge-keep-base
)
109 ("o" . smerge-keep-other
)
110 ("m" . smerge-keep-mine
)
112 ("\C-m" . smerge-keep-current
)
113 ("<" . smerge-diff-base-mine
)
114 (">" . smerge-diff-base-other
)
115 ("=" . smerge-diff-mine-other
))
116 "The base keymap for `smerge-mode'.")
117 (fset 'smerge-basic-keymap smerge-basic-keymap
)
119 (defcustom smerge-command-prefix
"\e"
120 "Prefix for `smerge-mode' commands."
122 :type
'(choice (string "\e") (string "C-x^") (string "") string
))
124 (smerge-defmap smerge-mode-map
125 `((,smerge-command-prefix . smerge-basic-keymap
))
126 "Keymap for `smerge-mode'.")
128 (easy-menu-define smerge-mode-menu smerge-mode-map
129 "Menu for `smerge-mode'."
131 ["Invoke Ediff" smerge-ediff t
]
134 (defconst smerge-font-lock-keywords
135 '((smerge-find-conflict
136 (1 smerge-mine-face prepend
)
137 (2 smerge-base-face prepend t
)
138 (3 smerge-other-face prepend t
)
139 (0 smerge-markers-face keep
)
142 "Font lock patterns for `smerge-mode'.")
144 (defconst smerge-begin-re
"^<<<<<<< \\(.*\\)\n")
145 (defconst smerge-end-re
"^>>>>>>> .*\n")
146 (defconst smerge-base-re
"^||||||| .*\n")
147 (defconst smerge-other-re
"^=======\n")
149 (defvar smerge-conflict-style nil
150 "Keep track of which style of conflict is in use.
151 Can be nil if the style is undecided, or else:
155 ;; Compiler pacifiers
156 (defvar font-lock-mode nil
)
157 (defvar font-lock-keywords nil
)
159 (unless (fboundp 'font-lock-fontify-region
)
160 (autoload 'font-lock-fontify-region
"font-lock")))
166 (defun smerge-next (&optional count
)
167 "Go to the next COUNT'th conflict."
169 (unless count
(setq count
1))
170 (if (< count
0) (smerge-prev (- count
))
171 (if (looking-at smerge-begin-re
) (incf count
))
172 (unless (re-search-forward smerge-begin-re nil t count
)
173 (error "No next conflict"))
174 (goto-char (match-beginning 0))))
176 (defun smerge-prev (&optional count
)
177 "Go to the previous COUNT'th conflict."
179 (unless count
(setq count
1))
180 (if (< count
0) (smerge-next (- count
))
181 (unless (re-search-backward smerge-begin-re nil t count
)
182 (error "No previous 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-keep-all ()
191 "Keep all three versions.
192 Convenient for the kind of conflicts that can arise in ChangeLog files."
194 (smerge-match-conflict)
195 (replace-match (concat (or (match-string 1) "")
196 (or (match-string 2) "")
197 (or (match-string 3) ""))
200 (defun smerge-keep-base ()
201 "Revert to the base version."
203 (smerge-match-conflict)
204 (smerge-ensure-match 2)
205 (replace-match (match-string 2) t t
))
207 (defun smerge-keep-other ()
208 "Use \"other\" version."
210 (smerge-match-conflict)
211 ;;(smerge-ensure-match 3)
212 (replace-match (match-string 3) t t
))
214 (defun smerge-keep-mine ()
217 (smerge-match-conflict)
218 ;;(smerge-ensure-match 1)
219 (replace-match (match-string 1) t t
))
221 (defun smerge-keep-current ()
222 "Use the current (under the cursor) version."
224 (smerge-match-conflict)
226 (while (or (not (match-end i
))
227 (< (point) (match-beginning i
))
228 (>= (point) (match-end i
)))
230 (if (<= i
0) (error "Not inside a version")
231 (replace-match (match-string i
) t t
))))
233 (defun smerge-diff-base-mine ()
234 "Diff 'base' and 'mine' version in current conflict region."
238 (defun smerge-diff-base-other ()
239 "Diff 'base' and 'other' version in current conflict region."
243 (defun smerge-diff-mine-other ()
244 "Diff 'mine' and 'other' version in current conflict region."
248 (defun smerge-match-conflict ()
249 "Get info about the conflict. Puts the info in the `match-data'.
250 The submatches contain:
251 0: the whole conflict.
255 An error is raised if not inside a conflict."
258 (let* ((orig-point (point))
261 (_ (re-search-backward smerge-begin-re
))
263 (start (match-beginning 0))
264 (mine-start (match-end 0))
265 (filename (match-string 1))
267 (_ (re-search-forward smerge-end-re
))
268 (_ (assert (< orig-point
(match-end 0))))
270 (other-end (match-beginning 0))
273 (_ (re-search-backward smerge-other-re start
))
275 (mine-end (match-beginning 0))
276 (other-start (match-end 0))
280 ;; handle the various conflict styles
282 ((re-search-backward smerge-base-re start t
)
283 ;; a 3-parts conflict
284 (set (make-local-variable 'smerge-conflict-style
) 'diff3-A
)
285 (setq base-end mine-end
)
286 (setq mine-end
(match-beginning 0))
287 (setq base-start
(match-end 0)))
289 ((string= filename
(file-name-nondirectory
290 (or buffer-file-name
"")))
291 ;; a 2-parts conflict
292 (set (make-local-variable 'smerge-conflict-style
) 'diff3-E
))
294 ((and (not base-start
)
295 (or (eq smerge-conflict-style
'diff3-A
)
296 (string-match "^[.0-9]+\\'" filename
)))
297 ;; a same-diff conflict
298 (setq base-start mine-start
)
299 (setq base-end mine-end
)
300 (setq mine-start other-start
)
301 (setq mine-end other-end
)))
303 (store-match-data (list start end
306 other-start other-end
307 (when base-start
(1- base-start
)) base-start
308 (1- other-start
) other-start
))
310 (error "Point not in conflict region"))))
312 (defun smerge-find-conflict (&optional limit
)
313 "Find and match a conflict region. Intended as a font-lock MATCHER.
314 The submatches are the same as in `smerge-match-conflict'.
315 Returns non-nil if a match is found between the point and LIMIT.
316 The point is moved to the end of the conflict."
317 (when (re-search-forward smerge-begin-re limit t
)
319 (smerge-match-conflict)
320 (goto-char (match-end 0)))))
322 (defun smerge-diff (n1 n2
)
323 (smerge-match-conflict)
324 (smerge-ensure-match n1
)
325 (smerge-ensure-match n2
)
326 (let ((name1 (aref smerge-match-names n1
))
327 (name2 (aref smerge-match-names n2
))
328 (file1 (make-temp-file "smerge1"))
329 (file2 (make-temp-file "smerge2")))
330 (write-region (match-beginning n1
) (match-end n1
) file1
)
331 (write-region (match-beginning n2
) (match-end n2
) file2
)
333 (with-current-buffer (get-buffer-create smerge-diff-buffer-name
)
334 (let ((inhibit-read-only t
))
336 (apply 'call-process diff-command nil t nil
337 (append smerge-diff-switches
338 (list "-L" name1
"-L" name2 file1 file2
))))
339 (goto-char (point-min))
341 (display-buffer (current-buffer) t
))
343 (delete-file file2
))))
346 ;; compiler pacifiers
347 (defvar smerge-ediff-windows
)
348 (defvar smerge-ediff-buf
)
349 (defvar ediff-buffer-A
)
350 (defvar ediff-buffer-B
)
351 (defvar ediff-buffer-C
)
352 (unless (fboundp 'ediff-cleanup-mess
)
353 (autoload 'ediff-cleanup-mess
"ediff-util")))
355 (defun smerge-ediff ()
356 "Invoke ediff to resolve the conflicts."
358 (let* ((buf (current-buffer))
360 ;;(ediff-default-variant 'default-B)
361 (config (current-window-configuration))
362 (filename (file-name-nondirectory buffer-file-name
))
363 (mine (generate-new-buffer (concat "*" filename
" MINE*")))
364 (other (generate-new-buffer (concat "*" filename
" OTHER*")))
366 (with-current-buffer mine
367 (buffer-disable-undo)
368 (insert-buffer-substring buf
)
369 (goto-char (point-min))
370 (while (smerge-find-conflict)
371 (when (match-beginning 2) (setq base t
))
372 (replace-match (match-string 1) t t
))
374 (set-buffer-modified-p nil
)
377 (with-current-buffer other
378 (buffer-disable-undo)
379 (insert-buffer-substring buf
)
380 (goto-char (point-min))
381 (while (smerge-find-conflict)
382 (replace-match (match-string 3) t t
))
384 (set-buffer-modified-p nil
)
388 (setq base
(generate-new-buffer (concat "*" filename
" BASE*")))
389 (with-current-buffer base
390 (buffer-disable-undo)
391 (insert-buffer-substring buf
)
392 (goto-char (point-min))
393 (while (smerge-find-conflict)
394 (replace-match (or (match-string 2) "") t t
))
396 (set-buffer-modified-p nil
)
399 ;; the rest of the code is inspired from vc.el
403 (ediff-merge-buffers-with-ancestor mine other base
)
404 ;; nil 'ediff-merge-revisions-with-ancestor buffer-file-name)
405 (ediff-merge-buffers mine other
)))
406 ;; nil 'ediff-merge-revisions buffer-file-name)))
408 ;; Ediff is now set up, and we are in the control buffer.
409 ;; Do a few further adjustments and take precautions for exit.
410 (set (make-local-variable 'smerge-ediff-windows
) config
)
411 (set (make-local-variable 'smerge-ediff-buf
) buf
)
412 (set (make-local-variable 'ediff-quit-hook
)
414 (let ((buffer-A ediff-buffer-A
)
415 (buffer-B ediff-buffer-B
)
416 (buffer-C ediff-buffer-C
)
417 (buffer-Ancestor ediff-ancestor-buffer
)
418 (buf smerge-ediff-buf
)
419 (windows smerge-ediff-windows
))
421 (with-current-buffer buf
423 (insert-buffer buffer-C
)
424 (kill-buffer buffer-A
)
425 (kill-buffer buffer-B
)
426 (kill-buffer buffer-C
)
427 (when (bufferp buffer-Ancestor
) (kill-buffer buffer-Ancestor
))
428 (set-window-configuration windows
)
429 (message "Conflict resolution finished; you may save the buffer")))))
430 (message "Please resolve conflicts now; exit ediff when done")))
434 (define-minor-mode smerge-mode
435 "Minor mode to simplify editing output from the diff3 program.
441 (font-lock-add-keywords nil smerge-font-lock-keywords
'append
)
442 (font-lock-remove-keywords nil smerge-font-lock-keywords
))
443 (goto-char (point-min))
444 (while (smerge-find-conflict)
445 (font-lock-fontify-region (match-beginning 0) (match-end 0) nil
)))))
448 (provide 'smerge-mode
)
453 ;;; smerge-mode.el ends here