*** empty log message ***
[emacs.git] / lisp / smerge-mode.el
blob5e56137bf0726e91ffd09192e169becc2791a57b
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)
14 ;; any later version.
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.
26 ;;; Commentary:
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 ()
37 ;; (save-excursion
38 ;; (goto-char (point-min))
39 ;; (when (re-search-forward "^<<<<<<< " nil t)
40 ;; (smerge-mode 1))))
41 ;; (add-hook 'find-file-hooks 'sm-try-smerge t)
43 ;;; Todo:
45 ;; - if requested, ask the user whether he wants to call ediff right away
47 ;;; Code:
49 (eval-when-compile (require 'cl))
52 (defgroup smerge ()
53 "Minor mode to resolve diff3 conflicts."
54 :group 'tools
55 :prefix "smerge-")
57 (defcustom smerge-diff-buffer-name "*smerge-diff*"
58 "Buffer name to use for displaying diffs."
59 :group 'smerge
60 :type '(choice
61 (const "*vc-diff*")
62 (const "*cvs-diff*")
63 (const "*smerge-diff*")
64 string))
66 (defcustom smerge-diff-switches
67 (append '("-d" "-b")
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."
71 :group 'smerge
72 :type '(repeat string))
74 (defcustom smerge-auto-leave t
75 "*Non-nil means to leave `smerge-mode' when the last conflict is resolved."
76 :group 'smerge
77 :type 'boolean)
79 (defface smerge-mine-face
80 '((t (:foreground "blue")))
81 "Face for your code."
82 :group 'smerge)
83 (defvar smerge-mine-face 'smerge-mine-face)
85 (defface smerge-other-face
86 '((t (:foreground "darkgreen")))
87 "Face for the other code."
88 :group 'smerge)
89 (defvar smerge-other-face 'smerge-other-face)
91 (defface smerge-base-face
92 '((t (:foreground "red")))
93 "Face for the base code."
94 :group 'smerge)
95 (defvar smerge-base-face 'smerge-base-face)
97 (defface smerge-markers-face
98 '((t (:background "grey85")))
99 "Face for the conflict markers."
100 :group 'smerge)
101 (defvar smerge-markers-face 'smerge-markers-face)
103 (easy-mmode-defmap smerge-basic-map
104 '(("n" . smerge-next)
105 ("p" . smerge-prev)
106 ("a" . smerge-keep-all)
107 ("b" . smerge-keep-base)
108 ("o" . smerge-keep-other)
109 ("m" . smerge-keep-mine)
110 ("E" . smerge-ediff)
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."
119 :group 'smerge
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'."
128 '("SMerge"
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"]
137 "--"
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"]
144 "--"
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)
155 (4 nil t t)
156 (5 nil t t)))
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:
167 - `diff3-E'
168 - `diff3-A'")
170 ;; Compiler pacifiers
171 (defvar font-lock-mode nil)
172 (defvar font-lock-keywords nil)
173 (eval-when-compile
174 (unless (fboundp 'font-lock-fontify-region)
175 (autoload 'font-lock-fontify-region "font-lock")))
177 ;;;;
178 ;;;; Actual code
179 ;;;;
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))))
194 (smerge-mode -1)))
197 (defun smerge-keep-all ()
198 "Keep all three versions.
199 Convenient for the kind of conflicts that can arise in ChangeLog files."
200 (interactive)
201 (smerge-match-conflict)
202 (replace-match (concat (or (match-string 1) "")
203 (or (match-string 2) "")
204 (or (match-string 3) ""))
205 t t)
206 (smerge-auto-leave))
208 (defun smerge-keep-base ()
209 "Revert to the base version."
210 (interactive)
211 (smerge-match-conflict)
212 (smerge-ensure-match 2)
213 (replace-match (match-string 2) t t)
214 (smerge-auto-leave))
216 (defun smerge-keep-other ()
217 "Use \"other\" version."
218 (interactive)
219 (smerge-match-conflict)
220 ;;(smerge-ensure-match 3)
221 (replace-match (match-string 3) t t)
222 (smerge-auto-leave))
224 (defun smerge-keep-mine ()
225 "Keep your version."
226 (interactive)
227 (smerge-match-conflict)
228 ;;(smerge-ensure-match 1)
229 (replace-match (match-string 1) t t)
230 (smerge-auto-leave))
232 (defun smerge-keep-current ()
233 "Use the current (under the cursor) version."
234 (interactive)
235 (smerge-match-conflict)
236 (let ((i 3))
237 (while (or (not (match-end i))
238 (< (point) (match-beginning i))
239 (>= (point) (match-end i)))
240 (decf 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."
247 (interactive)
248 (smerge-diff 2 1))
250 (defun smerge-diff-base-other ()
251 "Diff 'base' and 'other' version in current conflict region."
252 (interactive)
253 (smerge-diff 2 3))
255 (defun smerge-diff-mine-other ()
256 "Diff 'mine' and 'other' version in current conflict region."
257 (interactive)
258 (smerge-diff 1 3))
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.
264 1: your code.
265 2: the base code.
266 3: other code.
267 An error is raised if not inside a conflict."
268 (save-excursion
269 (condition-case nil
270 (let* ((orig-point (point))
272 (_ (forward-line 1))
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))
283 (end (match-end 0))
285 (_ (re-search-backward smerge-other-re start))
287 (mine-end (match-beginning 0))
288 (other-start (match-end 0))
290 base-start base-end)
292 ;; handle the various conflict styles
293 (cond
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
316 mine-start mine-end
317 base-start base-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)
330 (ignore-errors
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)
344 (unwind-protect
345 (with-current-buffer (get-buffer-create smerge-diff-buffer-name)
346 (let ((inhibit-read-only t))
347 (erase-buffer)
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))
352 (diff-mode)
353 (display-buffer (current-buffer) t))
354 (delete-file file1)
355 (delete-file file2))))
357 (eval-when-compile
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."
369 (interactive)
370 (let* ((buf (current-buffer))
371 (mode major-mode)
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*")))
377 base)
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))
385 (buffer-enable-undo)
386 (set-buffer-modified-p nil)
387 (funcall mode))
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))
395 (buffer-enable-undo)
396 (set-buffer-modified-p nil)
397 (funcall mode))
399 (when base
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))
407 (buffer-enable-undo)
408 (set-buffer-modified-p nil)
409 (funcall mode)))
411 ;; the rest of the code is inspired from vc.el
412 ;; Fire up ediff.
413 (set-buffer
414 (if base
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)
425 (lambda ()
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))
432 (ediff-cleanup-mess)
433 (with-current-buffer buf
434 (erase-buffer)
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")))
445 ;;;###autoload
446 (define-minor-mode smerge-mode
447 "Minor mode to simplify editing output from the diff3 program.
448 \\{smerge-mode-map}"
449 nil " SMerge" nil
450 (when font-lock-mode
451 (save-excursion
452 (if smerge-mode
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)
462 ;;; Change Log:
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