2000-05-25 Michael Kifer <kifer@cs.sunysb.edu>
[emacs.git] / lisp / smerge-mode.el
blobbc6fe40432fda816a0e41180d939dfa5f9c7bbc5
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)
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 (list* "-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 ["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)
138 (4 nil t t)
139 (5 nil t t)))
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:
150 - `diff3-E'
151 - `diff3-A'")
153 ;; Compiler pacifiers
154 (defvar font-lock-mode nil)
155 (defvar font-lock-keywords nil)
156 (eval-when-compile
157 (unless (fboundp 'font-lock-fontify-region)
158 (autoload 'font-lock-fontify-region "font-lock")))
160 ;;;;
161 ;;;; Actual code
162 ;;;;
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))))
177 (smerge-mode -1)))
180 (defun smerge-keep-all ()
181 "Keep all three versions.
182 Convenient for the kind of conflicts that can arise in ChangeLog files."
183 (interactive)
184 (smerge-match-conflict)
185 (replace-match (concat (or (match-string 1) "")
186 (or (match-string 2) "")
187 (or (match-string 3) ""))
188 t t)
189 (smerge-auto-leave))
191 (defun smerge-keep-base ()
192 "Revert to the base version."
193 (interactive)
194 (smerge-match-conflict)
195 (smerge-ensure-match 2)
196 (replace-match (match-string 2) t t)
197 (smerge-auto-leave))
199 (defun smerge-keep-other ()
200 "Use \"other\" version."
201 (interactive)
202 (smerge-match-conflict)
203 ;;(smerge-ensure-match 3)
204 (replace-match (match-string 3) t t)
205 (smerge-auto-leave))
207 (defun smerge-keep-mine ()
208 "Keep your version."
209 (interactive)
210 (smerge-match-conflict)
211 ;;(smerge-ensure-match 1)
212 (replace-match (match-string 1) t t)
213 (smerge-auto-leave))
215 (defun smerge-keep-current ()
216 "Use the current (under the cursor) version."
217 (interactive)
218 (smerge-match-conflict)
219 (let ((i 3))
220 (while (or (not (match-end i))
221 (< (point) (match-beginning i))
222 (>= (point) (match-end i)))
223 (decf 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."
230 (interactive)
231 (smerge-diff 2 1))
233 (defun smerge-diff-base-other ()
234 "Diff 'base' and 'other' version in current conflict region."
235 (interactive)
236 (smerge-diff 2 3))
238 (defun smerge-diff-mine-other ()
239 "Diff 'mine' and 'other' version in current conflict region."
240 (interactive)
241 (smerge-diff 1 3))
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.
247 1: your code.
248 2: the base code.
249 3: other code.
250 An error is raised if not inside a conflict."
251 (save-excursion
252 (condition-case nil
253 (let* ((orig-point (point))
255 (_ (forward-line 1))
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))
266 (end (match-end 0))
268 (_ (re-search-backward smerge-other-re start))
270 (mine-end (match-beginning 0))
271 (other-start (match-end 0))
273 base-start base-end)
275 ;; handle the various conflict styles
276 (cond
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
299 mine-start mine-end
300 base-start base-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)
313 (ignore-errors
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)
327 (unwind-protect
328 (with-current-buffer (get-buffer-create smerge-diff-buffer-name)
329 (let ((inhibit-read-only t))
330 (erase-buffer)
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))
335 (diff-mode)
336 (display-buffer (current-buffer) t))
337 (delete-file file1)
338 (delete-file file2))))
340 (eval-when-compile
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."
352 (interactive)
353 (let* ((buf (current-buffer))
354 (mode major-mode)
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*")))
360 base)
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))
368 (buffer-enable-undo)
369 (set-buffer-modified-p nil)
370 (funcall mode))
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))
378 (buffer-enable-undo)
379 (set-buffer-modified-p nil)
380 (funcall mode))
382 (when base
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))
390 (buffer-enable-undo)
391 (set-buffer-modified-p nil)
392 (funcall mode)))
394 ;; the rest of the code is inspired from vc.el
395 ;; Fire up ediff.
396 (set-buffer
397 (if base
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)
408 (lambda ()
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))
415 (ediff-cleanup-mess)
416 (with-current-buffer buf
417 (erase-buffer)
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")))
428 ;;;###autoload
429 (define-minor-mode smerge-mode
430 "Minor mode to simplify editing output from the diff3 program.
431 \\{smerge-mode-map}"
432 nil " SMerge" nil
433 (when font-lock-mode
434 (save-excursion
435 (if smerge-mode
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)
445 ;;; Change Log:
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