(normal-top-level): Don't operate on the initial
[emacs.git] / lisp / smerge-mode.el
blob5c8af81b492fdefac961a65bc63bee587373a95a
1 ;;; smerge-mode.el --- Minor mode to resolve diff3 conflicts
3 ;; Copyright (C) 1999, 2000 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.12 2001/03/07 00:16:29 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 (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)
186 (eval-when-compile
187 (unless (fboundp 'font-lock-fontify-region)
188 (autoload 'font-lock-fontify-region "font-lock")))
190 ;;;;
191 ;;;; Actual code
192 ;;;;
194 ;; Define smerge-next and smerge-prev
195 (easy-mmode-define-navigation smerge smerge-begin-re "conflict")
197 (defconst smerge-match-names ["conflict" "mine" "base" "other"])
199 (defun smerge-ensure-match (n)
200 (unless (match-end n)
201 (error (format "No `%s'" (aref smerge-match-names n)))))
203 (defun smerge-auto-leave ()
204 (when (and smerge-auto-leave
205 (save-excursion (goto-char (point-min))
206 (not (re-search-forward smerge-begin-re nil t))))
207 (smerge-mode -1)))
210 (defun smerge-keep-all ()
211 "Keep all three versions.
212 Convenient for the kind of conflicts that can arise in ChangeLog files."
213 (interactive)
214 (smerge-match-conflict)
215 (replace-match (concat (or (match-string 1) "")
216 (or (match-string 2) "")
217 (or (match-string 3) ""))
218 t t)
219 (smerge-auto-leave))
221 (defun smerge-keep-base ()
222 "Revert to the base version."
223 (interactive)
224 (smerge-match-conflict)
225 (smerge-ensure-match 2)
226 (replace-match (match-string 2) t t)
227 (smerge-auto-leave))
229 (defun smerge-keep-other ()
230 "Use \"other\" version."
231 (interactive)
232 (smerge-match-conflict)
233 ;;(smerge-ensure-match 3)
234 (replace-match (match-string 3) t t)
235 (smerge-auto-leave))
237 (defun smerge-keep-mine ()
238 "Keep your version."
239 (interactive)
240 (smerge-match-conflict)
241 ;;(smerge-ensure-match 1)
242 (replace-match (match-string 1) t t)
243 (smerge-auto-leave))
245 (defun smerge-keep-current ()
246 "Use the current (under the cursor) version."
247 (interactive)
248 (smerge-match-conflict)
249 (let ((i 3))
250 (while (or (not (match-end i))
251 (< (point) (match-beginning i))
252 (>= (point) (match-end i)))
253 (decf i))
254 (if (<= i 0) (error "Not inside a version")
255 (replace-match (match-string i) t t)
256 (smerge-auto-leave))))
258 (defun smerge-diff-base-mine ()
259 "Diff 'base' and 'mine' version in current conflict region."
260 (interactive)
261 (smerge-diff 2 1))
263 (defun smerge-diff-base-other ()
264 "Diff 'base' and 'other' version in current conflict region."
265 (interactive)
266 (smerge-diff 2 3))
268 (defun smerge-diff-mine-other ()
269 "Diff 'mine' and 'other' version in current conflict region."
270 (interactive)
271 (smerge-diff 1 3))
273 (defun smerge-match-conflict ()
274 "Get info about the conflict. Puts the info in the `match-data'.
275 The submatches contain:
276 0: the whole conflict.
277 1: your code.
278 2: the base code.
279 3: other code.
280 An error is raised if not inside a conflict."
281 (save-excursion
282 (condition-case nil
283 (let* ((orig-point (point))
285 (_ (forward-line 1))
286 (_ (re-search-backward smerge-begin-re))
288 (start (match-beginning 0))
289 (mine-start (match-end 0))
290 (filename (match-string 1))
292 (_ (re-search-forward smerge-end-re))
293 (_ (assert (< orig-point (match-end 0))))
295 (other-end (match-beginning 0))
296 (end (match-end 0))
298 (_ (re-search-backward smerge-other-re start))
300 (mine-end (match-beginning 0))
301 (other-start (match-end 0))
303 base-start base-end)
305 ;; handle the various conflict styles
306 (cond
307 ((re-search-backward smerge-base-re start t)
308 ;; a 3-parts conflict
309 (set (make-local-variable 'smerge-conflict-style) 'diff3-A)
310 (setq base-end mine-end)
311 (setq mine-end (match-beginning 0))
312 (setq base-start (match-end 0)))
314 ((string= filename (file-name-nondirectory
315 (or buffer-file-name "")))
316 ;; a 2-parts conflict
317 (set (make-local-variable 'smerge-conflict-style) 'diff3-E))
319 ((and (not base-start)
320 (or (eq smerge-conflict-style 'diff3-A)
321 (string-match "^[.0-9]+\\'" filename)))
322 ;; a same-diff conflict
323 (setq base-start mine-start)
324 (setq base-end mine-end)
325 (setq mine-start other-start)
326 (setq mine-end other-end)))
328 (store-match-data (list start end
329 mine-start mine-end
330 base-start base-end
331 other-start other-end
332 (when base-start (1- base-start)) base-start
333 (1- other-start) other-start))
335 (error "Point not in conflict region"))))
337 (defun smerge-find-conflict (&optional limit)
338 "Find and match a conflict region. Intended as a font-lock MATCHER.
339 The submatches are the same as in `smerge-match-conflict'.
340 Returns non-nil if a match is found between the point and LIMIT.
341 The point is moved to the end of the conflict."
342 (when (re-search-forward smerge-begin-re limit t)
343 (ignore-errors
344 (smerge-match-conflict)
345 (goto-char (match-end 0)))))
347 (defun smerge-diff (n1 n2)
348 (smerge-match-conflict)
349 (smerge-ensure-match n1)
350 (smerge-ensure-match n2)
351 (let ((name1 (aref smerge-match-names n1))
352 (name2 (aref smerge-match-names n2))
353 (file1 (make-temp-file "smerge1"))
354 (file2 (make-temp-file "smerge2"))
355 (dir default-directory)
356 (file (file-relative-name buffer-file-name)))
357 (write-region (match-beginning n1) (match-end n1) file1)
358 (write-region (match-beginning n2) (match-end n2) file2)
359 (unwind-protect
360 (with-current-buffer (get-buffer-create smerge-diff-buffer-name)
361 (setq default-directory dir)
362 (let ((inhibit-read-only t))
363 (erase-buffer)
364 (apply 'call-process diff-command nil t nil
365 (append smerge-diff-switches
366 (list "-L" (concat name1 "/" file)
367 "-L" (concat name2 "/" file)
368 file1 file2))))
369 (goto-char (point-min))
370 (diff-mode)
371 (display-buffer (current-buffer) t))
372 (delete-file file1)
373 (delete-file file2))))
375 (eval-when-compile
376 ;; compiler pacifiers
377 (defvar smerge-ediff-windows)
378 (defvar smerge-ediff-buf)
379 (defvar ediff-buffer-A)
380 (defvar ediff-buffer-B)
381 (defvar ediff-buffer-C)
382 (unless (fboundp 'ediff-cleanup-mess)
383 (autoload 'ediff-cleanup-mess "ediff-util")))
385 (defun smerge-ediff ()
386 "Invoke ediff to resolve the conflicts."
387 (interactive)
388 (let* ((buf (current-buffer))
389 (mode major-mode)
390 ;;(ediff-default-variant 'default-B)
391 (config (current-window-configuration))
392 (filename (file-name-nondirectory buffer-file-name))
393 (mine (generate-new-buffer (concat "*" filename " MINE*")))
394 (other (generate-new-buffer (concat "*" filename " OTHER*")))
395 base)
396 (with-current-buffer mine
397 (buffer-disable-undo)
398 (insert-buffer-substring buf)
399 (goto-char (point-min))
400 (while (smerge-find-conflict)
401 (when (match-beginning 2) (setq base t))
402 (replace-match (match-string 1) t t))
403 (buffer-enable-undo)
404 (set-buffer-modified-p nil)
405 (funcall mode))
407 (with-current-buffer other
408 (buffer-disable-undo)
409 (insert-buffer-substring buf)
410 (goto-char (point-min))
411 (while (smerge-find-conflict)
412 (replace-match (match-string 3) t t))
413 (buffer-enable-undo)
414 (set-buffer-modified-p nil)
415 (funcall mode))
417 (when base
418 (setq base (generate-new-buffer (concat "*" filename " BASE*")))
419 (with-current-buffer base
420 (buffer-disable-undo)
421 (insert-buffer-substring buf)
422 (goto-char (point-min))
423 (while (smerge-find-conflict)
424 (replace-match (or (match-string 2) "") t t))
425 (buffer-enable-undo)
426 (set-buffer-modified-p nil)
427 (funcall mode)))
429 ;; the rest of the code is inspired from vc.el
430 ;; Fire up ediff.
431 (set-buffer
432 (if base
433 (ediff-merge-buffers-with-ancestor mine other base)
434 ;; nil 'ediff-merge-revisions-with-ancestor buffer-file-name)
435 (ediff-merge-buffers mine other)))
436 ;; nil 'ediff-merge-revisions buffer-file-name)))
438 ;; Ediff is now set up, and we are in the control buffer.
439 ;; Do a few further adjustments and take precautions for exit.
440 (set (make-local-variable 'smerge-ediff-windows) config)
441 (set (make-local-variable 'smerge-ediff-buf) buf)
442 (set (make-local-variable 'ediff-quit-hook)
443 (lambda ()
444 (let ((buffer-A ediff-buffer-A)
445 (buffer-B ediff-buffer-B)
446 (buffer-C ediff-buffer-C)
447 (buffer-Ancestor ediff-ancestor-buffer)
448 (buf smerge-ediff-buf)
449 (windows smerge-ediff-windows))
450 (ediff-cleanup-mess)
451 (with-current-buffer buf
452 (erase-buffer)
453 (insert-buffer buffer-C)
454 (kill-buffer buffer-A)
455 (kill-buffer buffer-B)
456 (kill-buffer buffer-C)
457 (when (bufferp buffer-Ancestor) (kill-buffer buffer-Ancestor))
458 (set-window-configuration windows)
459 (message "Conflict resolution finished; you may save the buffer")))))
460 (message "Please resolve conflicts now; exit ediff when done")))
463 ;;;###autoload
464 (define-minor-mode smerge-mode
465 "Minor mode to simplify editing output from the diff3 program.
466 \\{smerge-mode-map}"
467 nil " SMerge" nil
468 (when font-lock-mode
469 (set (make-local-variable 'font-lock-multiline) t)
470 (save-excursion
471 (if smerge-mode
472 (font-lock-add-keywords nil smerge-font-lock-keywords 'append)
473 (font-lock-remove-keywords nil smerge-font-lock-keywords))
474 (goto-char (point-min))
475 (while (smerge-find-conflict)
476 (save-excursion
477 (font-lock-fontify-region (match-beginning 0) (match-end 0) nil))))))
480 (provide 'smerge-mode)
481 ;;; smerge-mode.el ends here