* xscheme.el (scheme-interaction-mode): Doc fix.
[emacs.git] / lisp / smerge-mode.el
blob80b0d9945a91c455a736ca11182196a80beffbbe
1 ;;; smerge-mode.el --- Minor mode to resolve diff3 conflicts
3 ;; Copyright (C) 1999, 2000, 2001 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.16 2002/08/15 00:24:56 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-hook '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 "*vc-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 '((((background light))
81 (:foreground "blue"))
82 (((background dark))
83 (:foreground "cyan")))
84 "Face for your code."
85 :group 'smerge)
86 (defvar smerge-mine-face 'smerge-mine-face)
88 (defface smerge-other-face
89 '((((background light))
90 (:foreground "darkgreen"))
91 (((background dark))
92 (:foreground "lightgreen")))
93 "Face for the other code."
94 :group 'smerge)
95 (defvar smerge-other-face 'smerge-other-face)
97 (defface smerge-base-face
98 '((((background light))
99 (:foreground "red"))
100 (((background dark))
101 (:foreground "orange")))
102 "Face for the base code."
103 :group 'smerge)
104 (defvar smerge-base-face 'smerge-base-face)
106 (defface smerge-markers-face
107 '((((background light))
108 (:background "grey85"))
109 (((background dark))
110 (:background "grey30")))
111 "Face for the conflict markers."
112 :group 'smerge)
113 (defvar smerge-markers-face 'smerge-markers-face)
115 (easy-mmode-defmap smerge-basic-map
116 `(("n" . smerge-next)
117 ("p" . smerge-prev)
118 ("a" . smerge-keep-all)
119 ("b" . smerge-keep-base)
120 ("o" . smerge-keep-other)
121 ("m" . smerge-keep-mine)
122 ("E" . smerge-ediff)
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."
132 :group 'smerge
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'."
141 '("SMerge"
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"]
149 "--"
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"]
156 "--"
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)
168 (4 nil t t)
169 (5 nil t t)))
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:
180 - `diff3-E'
181 - `diff3-A'")
183 ;; Compiler pacifiers
184 (defvar font-lock-mode)
185 (defvar font-lock-keywords)
187 ;;;;
188 ;;;; Actual code
189 ;;;;
191 ;; Define smerge-next and smerge-prev
192 (easy-mmode-define-navigation smerge smerge-begin-re "conflict")
194 (defconst smerge-match-names ["conflict" "mine" "base" "other"])
196 (defun smerge-ensure-match (n)
197 (unless (match-end n)
198 (error (format "No `%s'" (aref smerge-match-names n)))))
200 (defun smerge-auto-leave ()
201 (when (and smerge-auto-leave
202 (save-excursion (goto-char (point-min))
203 (not (re-search-forward smerge-begin-re nil t))))
204 (smerge-mode -1)))
207 (defun smerge-keep-all ()
208 "Keep all three versions.
209 Convenient for the kind of conflicts that can arise in ChangeLog files."
210 (interactive)
211 (smerge-match-conflict)
212 (replace-match (concat (or (match-string 1) "")
213 (or (match-string 2) "")
214 (or (match-string 3) ""))
215 t t)
216 (smerge-auto-leave))
218 (defun smerge-combine-with-next ()
219 "Combine the current conflict with the next one."
220 (interactive)
221 (smerge-match-conflict)
222 (let ((ends nil))
223 (dolist (i '(3 2 1 0))
224 (push (if (match-end i) (copy-marker (match-end i) t)) ends))
225 (setq ends (apply 'vector ends))
226 (goto-char (aref ends 0))
227 (if (not (re-search-forward smerge-begin-re nil t))
228 (error "No next conflict")
229 (smerge-match-conflict)
230 (let ((match-data (mapcar (lambda (m) (if m (copy-marker m)))
231 (match-data))))
232 ;; First copy the in-between text in each alternative.
233 (dolist (i '(1 2 3))
234 (when (aref ends i)
235 (goto-char (aref ends i))
236 (insert-buffer-substring (current-buffer)
237 (aref ends 0) (car match-data))))
238 (delete-region (aref ends 0) (car match-data))
239 ;; Then move the second conflict's alternatives into the first.
240 (dolist (i '(1 2 3))
241 (set-match-data match-data)
242 (when (and (aref ends i) (match-end i))
243 (goto-char (aref ends i))
244 (insert-buffer-substring (current-buffer)
245 (match-beginning i) (match-end i))))
246 (delete-region (car match-data) (cadr match-data))
247 ;; Free the markers.
248 (dolist (m match-data) (if m (move-marker m nil)))
249 (mapc (lambda (m) (if m (move-marker m nil))) ends)))))
251 (defun smerge-keep-base ()
252 "Revert to the base version."
253 (interactive)
254 (smerge-match-conflict)
255 (smerge-ensure-match 2)
256 (replace-match (match-string 2) t t)
257 (smerge-auto-leave))
259 (defun smerge-keep-other ()
260 "Use \"other\" version."
261 (interactive)
262 (smerge-match-conflict)
263 ;;(smerge-ensure-match 3)
264 (replace-match (match-string 3) t t)
265 (smerge-auto-leave))
267 (defun smerge-keep-mine ()
268 "Keep your version."
269 (interactive)
270 (smerge-match-conflict)
271 ;;(smerge-ensure-match 1)
272 (replace-match (match-string 1) t t)
273 (smerge-auto-leave))
275 (defun smerge-keep-current ()
276 "Use the current (under the cursor) version."
277 (interactive)
278 (smerge-match-conflict)
279 (let ((i 3))
280 (while (or (not (match-end i))
281 (< (point) (match-beginning i))
282 (>= (point) (match-end i)))
283 (decf i))
284 (if (<= i 0) (error "Not inside a version")
285 (replace-match (match-string i) t t)
286 (smerge-auto-leave))))
288 (defun smerge-diff-base-mine ()
289 "Diff 'base' and 'mine' version in current conflict region."
290 (interactive)
291 (smerge-diff 2 1))
293 (defun smerge-diff-base-other ()
294 "Diff 'base' and 'other' version in current conflict region."
295 (interactive)
296 (smerge-diff 2 3))
298 (defun smerge-diff-mine-other ()
299 "Diff 'mine' and 'other' version in current conflict region."
300 (interactive)
301 (smerge-diff 1 3))
303 (defun smerge-match-conflict ()
304 "Get info about the conflict. Puts the info in the `match-data'.
305 The submatches contain:
306 0: the whole conflict.
307 1: your code.
308 2: the base code.
309 3: other code.
310 An error is raised if not inside a conflict."
311 (save-excursion
312 (condition-case nil
313 (let* ((orig-point (point))
315 (_ (forward-line 1))
316 (_ (re-search-backward smerge-begin-re))
318 (start (match-beginning 0))
319 (mine-start (match-end 0))
320 (filename (match-string 1))
322 (_ (re-search-forward smerge-end-re))
323 (_ (assert (< orig-point (match-end 0))))
325 (other-end (match-beginning 0))
326 (end (match-end 0))
328 (_ (re-search-backward smerge-other-re start))
330 (mine-end (match-beginning 0))
331 (other-start (match-end 0))
333 base-start base-end)
335 ;; handle the various conflict styles
336 (cond
337 ((re-search-backward smerge-base-re start t)
338 ;; a 3-parts conflict
339 (set (make-local-variable 'smerge-conflict-style) 'diff3-A)
340 (setq base-end mine-end)
341 (setq mine-end (match-beginning 0))
342 (setq base-start (match-end 0)))
344 ((string= filename (file-name-nondirectory
345 (or buffer-file-name "")))
346 ;; a 2-parts conflict
347 (set (make-local-variable 'smerge-conflict-style) 'diff3-E))
349 ((and (not base-start)
350 (or (eq smerge-conflict-style 'diff3-A)
351 (string-match "^[.0-9]+\\'" filename)))
352 ;; a same-diff conflict
353 (setq base-start mine-start)
354 (setq base-end mine-end)
355 (setq mine-start other-start)
356 (setq mine-end other-end)))
358 (store-match-data (list start end
359 mine-start mine-end
360 base-start base-end
361 other-start other-end
362 (when base-start (1- base-start)) base-start
363 (1- other-start) other-start))
365 (search-failed (error "Point not in conflict region")))))
367 (defun smerge-find-conflict (&optional limit)
368 "Find and match a conflict region. Intended as a font-lock MATCHER.
369 The submatches are the same as in `smerge-match-conflict'.
370 Returns non-nil if a match is found between the point and LIMIT.
371 The point is moved to the end of the conflict."
372 (when (re-search-forward smerge-begin-re limit t)
373 (ignore-errors
374 (smerge-match-conflict)
375 (goto-char (match-end 0)))))
377 (defun smerge-diff (n1 n2)
378 (smerge-match-conflict)
379 (smerge-ensure-match n1)
380 (smerge-ensure-match n2)
381 (let ((name1 (aref smerge-match-names n1))
382 (name2 (aref smerge-match-names n2))
383 ;; Read them before the match-data gets clobbered.
384 (beg1 (match-beginning n1))
385 (end1 (match-end n1))
386 (beg2 (match-beginning n2))
387 (end2 (match-end n2))
388 (file1 (make-temp-file "smerge1"))
389 (file2 (make-temp-file "smerge2"))
390 (dir default-directory)
391 (file (file-relative-name buffer-file-name))
392 (coding-system-for-read buffer-file-coding-system))
393 (write-region beg1 end1 file1 nil 'nomessage)
394 (write-region beg2 end2 file2 nil 'nomessage)
395 (unwind-protect
396 (with-current-buffer (get-buffer-create smerge-diff-buffer-name)
397 (setq default-directory dir)
398 (let ((inhibit-read-only t))
399 (erase-buffer)
400 (let ((status
401 (apply 'call-process diff-command nil t nil
402 (append smerge-diff-switches
403 (list "-L" (concat name1 "/" file)
404 "-L" (concat name2 "/" file)
405 file1 file2)))))
406 (if (eq status 0) (insert "No differences found.\n"))))
407 (goto-char (point-min))
408 (diff-mode)
409 (display-buffer (current-buffer) t))
410 (delete-file file1)
411 (delete-file file2))))
413 ;; compiler pacifiers
414 (defvar smerge-ediff-windows)
415 (defvar smerge-ediff-buf)
416 (defvar ediff-buffer-A)
417 (defvar ediff-buffer-B)
418 (defvar ediff-buffer-C)
420 (defun smerge-ediff ()
421 "Invoke ediff to resolve the conflicts."
422 (interactive)
423 (let* ((buf (current-buffer))
424 (mode major-mode)
425 ;;(ediff-default-variant 'default-B)
426 (config (current-window-configuration))
427 (filename (file-name-nondirectory buffer-file-name))
428 (mine (generate-new-buffer (concat "*" filename " MINE*")))
429 (other (generate-new-buffer (concat "*" filename " OTHER*")))
430 base)
431 (with-current-buffer mine
432 (buffer-disable-undo)
433 (insert-buffer-substring buf)
434 (goto-char (point-min))
435 (while (smerge-find-conflict)
436 (when (match-beginning 2) (setq base t))
437 (replace-match (match-string 1) t t))
438 (buffer-enable-undo)
439 (set-buffer-modified-p nil)
440 (funcall mode))
442 (with-current-buffer other
443 (buffer-disable-undo)
444 (insert-buffer-substring buf)
445 (goto-char (point-min))
446 (while (smerge-find-conflict)
447 (replace-match (match-string 3) t t))
448 (buffer-enable-undo)
449 (set-buffer-modified-p nil)
450 (funcall mode))
452 (when base
453 (setq base (generate-new-buffer (concat "*" filename " BASE*")))
454 (with-current-buffer base
455 (buffer-disable-undo)
456 (insert-buffer-substring buf)
457 (goto-char (point-min))
458 (while (smerge-find-conflict)
459 (replace-match (or (match-string 2) "") t t))
460 (buffer-enable-undo)
461 (set-buffer-modified-p nil)
462 (funcall mode)))
464 ;; the rest of the code is inspired from vc.el
465 ;; Fire up ediff.
466 (set-buffer
467 (if base
468 (ediff-merge-buffers-with-ancestor mine other base)
469 ;; nil 'ediff-merge-revisions-with-ancestor buffer-file-name)
470 (ediff-merge-buffers mine other)))
471 ;; nil 'ediff-merge-revisions buffer-file-name)))
473 ;; Ediff is now set up, and we are in the control buffer.
474 ;; Do a few further adjustments and take precautions for exit.
475 (set (make-local-variable 'smerge-ediff-windows) config)
476 (set (make-local-variable 'smerge-ediff-buf) buf)
477 (set (make-local-variable 'ediff-quit-hook)
478 (lambda ()
479 (let ((buffer-A ediff-buffer-A)
480 (buffer-B ediff-buffer-B)
481 (buffer-C ediff-buffer-C)
482 (buffer-Ancestor ediff-ancestor-buffer)
483 (buf smerge-ediff-buf)
484 (windows smerge-ediff-windows))
485 (ediff-cleanup-mess)
486 (with-current-buffer buf
487 (erase-buffer)
488 (insert-buffer buffer-C)
489 (kill-buffer buffer-A)
490 (kill-buffer buffer-B)
491 (kill-buffer buffer-C)
492 (when (bufferp buffer-Ancestor) (kill-buffer buffer-Ancestor))
493 (set-window-configuration windows)
494 (message "Conflict resolution finished; you may save the buffer")))))
495 (message "Please resolve conflicts now; exit ediff when done")))
498 ;;;###autoload
499 (define-minor-mode smerge-mode
500 "Minor mode to simplify editing output from the diff3 program.
501 \\{smerge-mode-map}"
502 nil " SMerge" nil
503 (when (and (boundp 'font-lock-mode) font-lock-mode)
504 (set (make-local-variable 'font-lock-multiline) t)
505 (save-excursion
506 (if smerge-mode
507 (font-lock-add-keywords nil smerge-font-lock-keywords 'append)
508 (font-lock-remove-keywords nil smerge-font-lock-keywords))
509 (goto-char (point-min))
510 (while (smerge-find-conflict)
511 (save-excursion
512 (font-lock-fontify-region (match-beginning 0) (match-end 0) nil))))))
515 (provide 'smerge-mode)
516 ;;; smerge-mode.el ends here