(#includes): Allow compilation with only Xaw.
[emacs.git] / lisp / smerge-mode.el
blob14b0a3acaca35412c8757d69dff05f767c061f80
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$
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 ;;; Code:
45 (eval-when-compile (require 'cl))
48 (defgroup smerge ()
49 "Minor mode to resolve diff3 conflicts."
50 :group 'tools
51 :prefix "smerge-")
53 (defcustom smerge-diff-buffer-name "*smerge-diff*"
54 "Buffer name to use for displaying diffs."
55 :group 'smerge
56 :type '(choice
57 (const "*vc-diff*")
58 (const "*cvs-diff*")
59 (const "*smerge-diff*")
60 string))
62 (defcustom smerge-diff-switches
63 (list* "-d" "-b"
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."
67 :group 'smerge
68 :type '(repeat string))
70 (defface smerge-mine-face
71 '((t (:foreground "blue")))
72 "Face for your code."
73 :group 'smerge)
74 (defvar smerge-mine-face 'smerge-mine-face)
76 (defface smerge-other-face
77 '((t (:foreground "darkgreen")))
78 "Face for the other code."
79 :group 'smerge)
80 (defvar smerge-other-face 'smerge-other-face)
82 (defface smerge-base-face
83 '((t (:foreground "red")))
84 "Face for the base code."
85 :group 'smerge)
86 (defvar smerge-base-face 'smerge-base-face)
88 (defface smerge-markers-face
89 '((t (:background "grey85")))
90 "Face for the conflict markers."
91 :group 'smerge)
92 (defvar smerge-markers-face 'smerge-markers-face)
94 (defmacro smerge-defmap (var bindings &optional doc)
95 `(defvar ,var
96 (let ((m (make-sparse-keymap)))
97 (dolist (b ,bindings)
98 (if (equal (car b) "")
99 (set-keymap-parent m (cdr b))
100 (define-key m (car b) (cdr b))))
102 ,doc))
104 (smerge-defmap smerge-basic-keymap
105 '(("n" . smerge-next)
106 ("p" . smerge-prev)
107 ("a" . smerge-keep-all)
108 ("b" . smerge-keep-base)
109 ("o" . smerge-keep-other)
110 ("m" . smerge-keep-mine)
111 ("E" . smerge-ediff)
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."
121 :group 'smerge
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'."
130 '("SMerge"
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)
140 (4 nil t t)
141 (5 nil t t)))
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:
152 - `diff3-E'
153 - `diff3-A'")
155 ;; Compiler pacifiers
156 (defvar font-lock-mode nil)
157 (defvar font-lock-keywords nil)
158 (eval-when-compile
159 (unless (fboundp 'font-lock-fontify-region)
160 (autoload 'font-lock-fontify-region "font-lock")))
162 ;;;;
163 ;;;; Actual code
164 ;;;;
166 (defun smerge-next (&optional count)
167 "Go to the next COUNT'th conflict."
168 (interactive)
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."
178 (interactive)
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."
193 (interactive)
194 (smerge-match-conflict)
195 (replace-match (concat (or (match-string 1) "")
196 (or (match-string 2) "")
197 (or (match-string 3) ""))
198 t t))
200 (defun smerge-keep-base ()
201 "Revert to the base version."
202 (interactive)
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."
209 (interactive)
210 (smerge-match-conflict)
211 ;;(smerge-ensure-match 3)
212 (replace-match (match-string 3) t t))
214 (defun smerge-keep-mine ()
215 "Keep your version."
216 (interactive)
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."
223 (interactive)
224 (smerge-match-conflict)
225 (let ((i 3))
226 (while (or (not (match-end i))
227 (< (point) (match-beginning i))
228 (>= (point) (match-end i)))
229 (decf 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."
235 (interactive)
236 (smerge-diff 2 1))
238 (defun smerge-diff-base-other ()
239 "Diff 'base' and 'other' version in current conflict region."
240 (interactive)
241 (smerge-diff 2 3))
243 (defun smerge-diff-mine-other ()
244 "Diff 'mine' and 'other' version in current conflict region."
245 (interactive)
246 (smerge-diff 1 3))
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.
252 1: your code.
253 2: the base code.
254 3: other code.
255 An error is raised if not inside a conflict."
256 (save-excursion
257 (condition-case nil
258 (let* ((orig-point (point))
260 (_ (forward-line 1))
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))
271 (end (match-end 0))
273 (_ (re-search-backward smerge-other-re start))
275 (mine-end (match-beginning 0))
276 (other-start (match-end 0))
278 base-start base-end)
280 ;; handle the various conflict styles
281 (cond
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
304 mine-start mine-end
305 base-start base-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)
318 (ignore-errors
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)
332 (unwind-protect
333 (with-current-buffer (get-buffer-create smerge-diff-buffer-name)
334 (let ((inhibit-read-only t))
335 (erase-buffer)
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))
340 (diff-mode)
341 (display-buffer (current-buffer) t))
342 (delete-file file1)
343 (delete-file file2))))
345 (eval-when-compile
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."
357 (interactive)
358 (let* ((buf (current-buffer))
359 (mode major-mode)
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*")))
365 base)
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))
373 (buffer-enable-undo)
374 (set-buffer-modified-p nil)
375 (funcall mode))
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))
383 (buffer-enable-undo)
384 (set-buffer-modified-p nil)
385 (funcall mode))
387 (when base
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))
395 (buffer-enable-undo)
396 (set-buffer-modified-p nil)
397 (funcall mode)))
399 ;; the rest of the code is inspired from vc.el
400 ;; Fire up ediff.
401 (set-buffer
402 (if base
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)
413 (lambda ()
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))
420 (ediff-cleanup-mess)
421 (with-current-buffer buf
422 (erase-buffer)
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")))
433 ;;;###autoload
434 (define-minor-mode smerge-mode
435 "Minor mode to simplify editing output from the diff3 program.
436 \\{smerge-mode-map}"
437 nil " SMerge" nil
438 (when font-lock-mode
439 (save-excursion
440 (if smerge-mode
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)
450 ;;; Change Log:
451 ;; $Log$
453 ;;; smerge-mode.el ends here