]> code.delx.au - gnu-emacs/blob - lisp/smerge-mode.el
(smerge-check-cache, smerge-check): New var and fun.
[gnu-emacs] / lisp / smerge-mode.el
1 ;;; smerge-mode.el --- Minor mode to resolve diff3 conflicts
2
3 ;; Copyright (C) 1999, 2000, 01, 03, 2004 Free Software Foundation, Inc.
4
5 ;; Author: Stefan Monnier <monnier@cs.yale.edu>
6 ;; Keywords: merge diff3 cvs conflict
7 ;; Revision: $Id: smerge-mode.el,v 1.24 2003/10/06 16:34:59 fx Exp $
8
9 ;; This file is part of GNU Emacs.
10
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.
15
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.
20
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.
25
26 ;;; Commentary:
27
28 ;; Provides a lightweight alternative to emerge/ediff.
29 ;; To use it, simply add to your .emacs the following lines:
30 ;;
31 ;; (autoload 'smerge-mode "smerge-mode" nil t)
32 ;;
33 ;; you can even have it turned on automatically with the following
34 ;; piece of code in your .emacs:
35 ;;
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)
42
43 ;;; Todo:
44
45 ;; - if requested, ask the user whether he wants to call ediff right away
46
47 ;;; Code:
48
49 (eval-when-compile (require 'cl))
50
51
52 (defgroup smerge ()
53 "Minor mode to resolve diff3 conflicts."
54 :group 'tools
55 :prefix "smerge-")
56
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))
65
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))
73
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)
78
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)
87
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)
96
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)
105
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)
114
115 (easy-mmode-defmap smerge-basic-map
116 `(("n" . smerge-next)
117 ("p" . smerge-prev)
118 ("r" . smerge-resolve)
119 ("a" . smerge-keep-all)
120 ("b" . smerge-keep-base)
121 ("o" . smerge-keep-other)
122 ("m" . smerge-keep-mine)
123 ("E" . smerge-ediff)
124 ("\C-m" . smerge-keep-current)
125 ("=" . ,(make-sparse-keymap "Diff"))
126 ("=<" "base-mine" . smerge-diff-base-mine)
127 ("=>" "base-other" . smerge-diff-base-other)
128 ("==" "mine-other" . smerge-diff-mine-other))
129 "The base keymap for `smerge-mode'.")
130
131 (defcustom smerge-command-prefix "\C-c^"
132 "Prefix for `smerge-mode' commands."
133 :group 'smerge
134 :type '(choice (string "\e") (string "\C-c^") (string "") string))
135
136 (easy-mmode-defmap smerge-mode-map
137 `((,smerge-command-prefix . ,smerge-basic-map))
138 "Keymap for `smerge-mode'.")
139
140 (defvar smerge-check-cache nil)
141 (make-variable-buffer-local 'smerge-check-cache)
142 (defun smerge-check (n)
143 (condition-case nil
144 (let ((state (cons (point) (buffer-modified-tick))))
145 (unless (equal (cdr smerge-check-cache) state)
146 (smerge-match-conflict)
147 (setq smerge-check-cache (cons (match-data) state)))
148 (nth (* 2 n) (car smerge-check-cache)))
149 (error nil)))
150
151 (easy-menu-define smerge-mode-menu smerge-mode-map
152 "Menu for `smerge-mode'."
153 '("SMerge"
154 ["Next" smerge-next :help "Go to next conflict"]
155 ["Previous" smerge-prev :help "Go to previous conflict"]
156 "--"
157 ["Keep All" smerge-keep-all :help "Keep all three versions"
158 :active (smerge-check 1)]
159 ["Keep Current" smerge-keep-current :help "Use current (at point) version"
160 :active (and (smerge-check 1) (> (smerge-get-current) 0))]
161 "--"
162 ["Revert to Base" smerge-keep-base :help "Revert to base version"
163 :active (smerge-check 2)]
164 ["Keep Other" smerge-keep-other :help "Keep `other' version"
165 :active (smerge-check 3)]
166 ["Keep Yours" smerge-keep-mine :help "Keep your version"
167 :active (smerge-check 1)]
168 "--"
169 ["Diff Base/Mine" smerge-diff-base-mine
170 :help "Diff `base' and `mine' for current conflict"
171 :active (smerge-check 2)]
172 ["Diff Base/Other" smerge-diff-base-other
173 :help "Diff `base' and `other' for current conflict"
174 :active (smerge-check 2)]
175 ["Diff Mine/Other" smerge-diff-mine-other
176 :help "Diff `mine' and `other' for current conflict"
177 :active (smerge-check 1)]
178 "--"
179 ["Invoke Ediff" smerge-ediff
180 :help "Use Ediff to resolve the conflicts"
181 :active (smerge-check 1)]
182 ["Auto Resolve" smerge-resolve
183 :help "Use mode-provided resolution function"
184 :active (and (smerge-check 1) (local-variable-p 'smerge-resolve-function))]
185 ["Combine" smerge-combine-with-next
186 :help "Combine current conflict with next"
187 :active (smerge-check 1)]
188 ))
189
190 (defconst smerge-font-lock-keywords
191 '((smerge-find-conflict
192 (1 smerge-mine-face prepend t)
193 (2 smerge-base-face prepend t)
194 (3 smerge-other-face prepend t)
195 ;; FIXME: `keep' doesn't work right with syntactic fontification.
196 (0 smerge-markers-face keep)
197 (4 nil t t)
198 (5 nil t t)))
199 "Font lock patterns for `smerge-mode'.")
200
201 (defconst smerge-begin-re "^<<<<<<< \\(.*\\)\n")
202 (defconst smerge-end-re "^>>>>>>> .*\n")
203 (defconst smerge-base-re "^||||||| .*\n")
204 (defconst smerge-other-re "^=======\n")
205
206 (defvar smerge-conflict-style nil
207 "Keep track of which style of conflict is in use.
208 Can be nil if the style is undecided, or else:
209 - `diff3-E'
210 - `diff3-A'")
211
212 ;; Compiler pacifiers
213 (defvar font-lock-mode)
214 (defvar font-lock-keywords)
215
216 ;;;;
217 ;;;; Actual code
218 ;;;;
219
220 ;; Define smerge-next and smerge-prev
221 (easy-mmode-define-navigation smerge smerge-begin-re "conflict")
222
223 (defconst smerge-match-names ["conflict" "mine" "base" "other"])
224
225 (defun smerge-ensure-match (n)
226 (unless (match-end n)
227 (error (format "No `%s'" (aref smerge-match-names n)))))
228
229 (defun smerge-auto-leave ()
230 (when (and smerge-auto-leave
231 (save-excursion (goto-char (point-min))
232 (not (re-search-forward smerge-begin-re nil t))))
233 (smerge-mode -1)))
234
235
236 (defun smerge-keep-all ()
237 "Keep all three versions.
238 Convenient for the kind of conflicts that can arise in ChangeLog files."
239 (interactive)
240 (smerge-match-conflict)
241 (replace-match (concat (or (match-string 1) "")
242 (or (match-string 2) "")
243 (or (match-string 3) ""))
244 t t)
245 (smerge-auto-leave))
246
247 (defun smerge-combine-with-next ()
248 "Combine the current conflict with the next one."
249 (interactive)
250 (smerge-match-conflict)
251 (let ((ends nil))
252 (dolist (i '(3 2 1 0))
253 (push (if (match-end i) (copy-marker (match-end i) t)) ends))
254 (setq ends (apply 'vector ends))
255 (goto-char (aref ends 0))
256 (if (not (re-search-forward smerge-begin-re nil t))
257 (error "No next conflict")
258 (smerge-match-conflict)
259 (let ((match-data (mapcar (lambda (m) (if m (copy-marker m)))
260 (match-data))))
261 ;; First copy the in-between text in each alternative.
262 (dolist (i '(1 2 3))
263 (when (aref ends i)
264 (goto-char (aref ends i))
265 (insert-buffer-substring (current-buffer)
266 (aref ends 0) (car match-data))))
267 (delete-region (aref ends 0) (car match-data))
268 ;; Then move the second conflict's alternatives into the first.
269 (dolist (i '(1 2 3))
270 (set-match-data match-data)
271 (when (and (aref ends i) (match-end i))
272 (goto-char (aref ends i))
273 (insert-buffer-substring (current-buffer)
274 (match-beginning i) (match-end i))))
275 (delete-region (car match-data) (cadr match-data))
276 ;; Free the markers.
277 (dolist (m match-data) (if m (move-marker m nil)))
278 (mapc (lambda (m) (if m (move-marker m nil))) ends)))))
279
280 (defvar smerge-resolve-function
281 (lambda () (error "Don't know how to resolve"))
282 "Mode-specific merge function.
283 The function is called with no argument and with the match data set
284 according to `smerge-match-conflict'.")
285
286 (defun smerge-resolve ()
287 "Resolve the conflict at point intelligently.
288 This relies on mode-specific knowledge and thus only works in
289 some major modes. Uses `smerge-resolve-function' to do the actual work."
290 (interactive)
291 (smerge-match-conflict)
292 (funcall smerge-resolve-function)
293 (smerge-auto-leave))
294
295 (defun smerge-keep-base ()
296 "Revert to the base version."
297 (interactive)
298 (smerge-match-conflict)
299 (smerge-ensure-match 2)
300 (replace-match (match-string 2) t t)
301 (smerge-auto-leave))
302
303 (defun smerge-keep-other ()
304 "Use \"other\" version."
305 (interactive)
306 (smerge-match-conflict)
307 ;;(smerge-ensure-match 3)
308 (replace-match (match-string 3) t t)
309 (smerge-auto-leave))
310
311 (defun smerge-keep-mine ()
312 "Keep your version."
313 (interactive)
314 (smerge-match-conflict)
315 ;;(smerge-ensure-match 1)
316 (replace-match (match-string 1) t t)
317 (smerge-auto-leave))
318
319 (defun smerge-get-current ()
320 (let ((i 3))
321 (while (or (not (match-end i))
322 (< (point) (match-beginning i))
323 (>= (point) (match-end i)))
324 (decf i))
325 i))
326
327 (defun smerge-keep-current ()
328 "Use the current (under the cursor) version."
329 (interactive)
330 (smerge-match-conflict)
331 (let ((i (smerge-get-current)))
332 (if (<= i 0) (error "Not inside a version")
333 (replace-match (match-string i) t t)
334 (smerge-auto-leave))))
335
336 (defun smerge-diff-base-mine ()
337 "Diff 'base' and 'mine' version in current conflict region."
338 (interactive)
339 (smerge-diff 2 1))
340
341 (defun smerge-diff-base-other ()
342 "Diff 'base' and 'other' version in current conflict region."
343 (interactive)
344 (smerge-diff 2 3))
345
346 (defun smerge-diff-mine-other ()
347 "Diff 'mine' and 'other' version in current conflict region."
348 (interactive)
349 (smerge-diff 1 3))
350
351 (defun smerge-match-conflict ()
352 "Get info about the conflict. Puts the info in the `match-data'.
353 The submatches contain:
354 0: the whole conflict.
355 1: your code.
356 2: the base code.
357 3: other code.
358 An error is raised if not inside a conflict."
359 (save-excursion
360 (condition-case nil
361 (let* ((orig-point (point))
362
363 (_ (forward-line 1))
364 (_ (re-search-backward smerge-begin-re))
365
366 (start (match-beginning 0))
367 (mine-start (match-end 0))
368 (filename (or (match-string 1) ""))
369
370 (_ (re-search-forward smerge-end-re))
371 (_ (assert (< orig-point (match-end 0))))
372
373 (other-end (match-beginning 0))
374 (end (match-end 0))
375
376 (_ (re-search-backward smerge-other-re start))
377
378 (mine-end (match-beginning 0))
379 (other-start (match-end 0))
380
381 base-start base-end)
382
383 ;; handle the various conflict styles
384 (cond
385 ((re-search-backward smerge-base-re start t)
386 ;; a 3-parts conflict
387 (set (make-local-variable 'smerge-conflict-style) 'diff3-A)
388 (setq base-end mine-end)
389 (setq mine-end (match-beginning 0))
390 (setq base-start (match-end 0)))
391
392 ((string= filename (file-name-nondirectory
393 (or buffer-file-name "")))
394 ;; a 2-parts conflict
395 (set (make-local-variable 'smerge-conflict-style) 'diff3-E))
396
397 ((and (not base-start)
398 (or (eq smerge-conflict-style 'diff3-A)
399 (string-match "^[.0-9]+\\'" filename)))
400 ;; a same-diff conflict
401 (setq base-start mine-start)
402 (setq base-end mine-end)
403 (setq mine-start other-start)
404 (setq mine-end other-end)))
405
406 (store-match-data (list start end
407 mine-start mine-end
408 base-start base-end
409 other-start other-end
410 (when base-start (1- base-start)) base-start
411 (1- other-start) other-start))
412 t)
413 (search-failed (error "Point not in conflict region")))))
414
415 (defun smerge-find-conflict (&optional limit)
416 "Find and match a conflict region. Intended as a font-lock MATCHER.
417 The submatches are the same as in `smerge-match-conflict'.
418 Returns non-nil if a match is found between the point and LIMIT.
419 The point is moved to the end of the conflict."
420 (when (re-search-forward smerge-begin-re limit t)
421 (ignore-errors
422 (smerge-match-conflict)
423 (goto-char (match-end 0)))))
424
425 (defun smerge-diff (n1 n2)
426 (smerge-match-conflict)
427 (smerge-ensure-match n1)
428 (smerge-ensure-match n2)
429 (let ((name1 (aref smerge-match-names n1))
430 (name2 (aref smerge-match-names n2))
431 ;; Read them before the match-data gets clobbered.
432 (beg1 (match-beginning n1))
433 (end1 (match-end n1))
434 (beg2 (match-beginning n2))
435 (end2 (match-end n2))
436 (file1 (make-temp-file "smerge1"))
437 (file2 (make-temp-file "smerge2"))
438 (dir default-directory)
439 (file (file-relative-name buffer-file-name))
440 (coding-system-for-read buffer-file-coding-system))
441 (write-region beg1 end1 file1 nil 'nomessage)
442 (write-region beg2 end2 file2 nil 'nomessage)
443 (unwind-protect
444 (with-current-buffer (get-buffer-create smerge-diff-buffer-name)
445 (setq default-directory dir)
446 (let ((inhibit-read-only t))
447 (erase-buffer)
448 (let ((status
449 (apply 'call-process diff-command nil t nil
450 (append smerge-diff-switches
451 (list "-L" (concat name1 "/" file)
452 "-L" (concat name2 "/" file)
453 file1 file2)))))
454 (if (eq status 0) (insert "No differences found.\n"))))
455 (goto-char (point-min))
456 (diff-mode)
457 (display-buffer (current-buffer) t))
458 (delete-file file1)
459 (delete-file file2))))
460
461 ;; compiler pacifiers
462 (defvar smerge-ediff-windows)
463 (defvar smerge-ediff-buf)
464 (defvar ediff-buffer-A)
465 (defvar ediff-buffer-B)
466 (defvar ediff-buffer-C)
467
468 ;;;###autoload
469 (defun smerge-ediff (&optional name-mine name-other name-base)
470 "Invoke ediff to resolve the conflicts.
471 NAME-MINE, NAME-OTHER, and NAME-BASE, if non-nil, are used for the
472 buffer names."
473 (interactive)
474 (let* ((buf (current-buffer))
475 (mode major-mode)
476 ;;(ediff-default-variant 'default-B)
477 (config (current-window-configuration))
478 (filename (file-name-nondirectory buffer-file-name))
479 (mine (generate-new-buffer
480 (or name-mine (concat "*" filename " MINE*"))))
481 (other (generate-new-buffer
482 (or name-other (concat "*" filename " OTHER*"))))
483 base)
484 (with-current-buffer mine
485 (buffer-disable-undo)
486 (insert-buffer-substring buf)
487 (goto-char (point-min))
488 (while (smerge-find-conflict)
489 (when (match-beginning 2) (setq base t))
490 (replace-match (match-string 1) t t))
491 (buffer-enable-undo)
492 (set-buffer-modified-p nil)
493 (funcall mode))
494
495 (with-current-buffer other
496 (buffer-disable-undo)
497 (insert-buffer-substring buf)
498 (goto-char (point-min))
499 (while (smerge-find-conflict)
500 (replace-match (match-string 3) t t))
501 (buffer-enable-undo)
502 (set-buffer-modified-p nil)
503 (funcall mode))
504
505 (when base
506 (setq base (generate-new-buffer
507 (or name-base (concat "*" filename " BASE*"))))
508 (with-current-buffer base
509 (buffer-disable-undo)
510 (insert-buffer-substring buf)
511 (goto-char (point-min))
512 (while (smerge-find-conflict)
513 (replace-match (or (match-string 2) "") t t))
514 (buffer-enable-undo)
515 (set-buffer-modified-p nil)
516 (funcall mode)))
517
518 ;; the rest of the code is inspired from vc.el
519 ;; Fire up ediff.
520 (set-buffer
521 (if base
522 (ediff-merge-buffers-with-ancestor mine other base)
523 ;; nil 'ediff-merge-revisions-with-ancestor buffer-file-name)
524 (ediff-merge-buffers mine other)))
525 ;; nil 'ediff-merge-revisions buffer-file-name)))
526
527 ;; Ediff is now set up, and we are in the control buffer.
528 ;; Do a few further adjustments and take precautions for exit.
529 (set (make-local-variable 'smerge-ediff-windows) config)
530 (set (make-local-variable 'smerge-ediff-buf) buf)
531 (set (make-local-variable 'ediff-quit-hook)
532 (lambda ()
533 (let ((buffer-A ediff-buffer-A)
534 (buffer-B ediff-buffer-B)
535 (buffer-C ediff-buffer-C)
536 (buffer-Ancestor ediff-ancestor-buffer)
537 (buf smerge-ediff-buf)
538 (windows smerge-ediff-windows))
539 (ediff-cleanup-mess)
540 (with-current-buffer buf
541 (erase-buffer)
542 (insert-buffer buffer-C)
543 (kill-buffer buffer-A)
544 (kill-buffer buffer-B)
545 (kill-buffer buffer-C)
546 (when (bufferp buffer-Ancestor) (kill-buffer buffer-Ancestor))
547 (set-window-configuration windows)
548 (message "Conflict resolution finished; you may save the buffer")))))
549 (message "Please resolve conflicts now; exit ediff when done")))
550
551
552 ;;;###autoload
553 (define-minor-mode smerge-mode
554 "Minor mode to simplify editing output from the diff3 program.
555 \\{smerge-mode-map}"
556 nil " SMerge" nil
557 (when (and (boundp 'font-lock-mode) font-lock-mode)
558 (set (make-local-variable 'font-lock-multiline) t)
559 (save-excursion
560 (if smerge-mode
561 (font-lock-add-keywords nil smerge-font-lock-keywords 'append)
562 (font-lock-remove-keywords nil smerge-font-lock-keywords))
563 (goto-char (point-min))
564 (while (smerge-find-conflict)
565 (save-excursion
566 (font-lock-fontify-region (match-beginning 0) (match-end 0) nil))))))
567
568
569 (provide 'smerge-mode)
570
571 ;;; arch-tag: 605c8d1e-e43d-4943-a6f3-1bcc4333e690
572 ;;; smerge-mode.el ends here