Commit | Line | Data |
---|---|---|
3dac25a9 SM |
1 | ;;; smerge-mode.el --- Minor mode to resolve diff3 conflicts |
2 | ||
6b61353c | 3 | ;; Copyright (C) 1999, 2000, 01, 03, 2004 Free Software Foundation, Inc. |
3dac25a9 SM |
4 | |
5 | ;; Author: Stefan Monnier <monnier@cs.yale.edu> | |
6b61353c | 6 | ;; Keywords: revision-control merge diff3 cvs conflict |
3dac25a9 SM |
7 | |
8 | ;; This file is part of GNU Emacs. | |
9 | ||
10 | ;; GNU Emacs is free software; you can redistribute it and/or modify | |
11 | ;; it under the terms of the GNU General Public License as published by | |
12 | ;; the Free Software Foundation; either version 2, or (at your option) | |
13 | ;; any later version. | |
14 | ||
15 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
16 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | ;; GNU General Public License for more details. | |
19 | ||
20 | ;; You should have received a copy of the GNU General Public License | |
21 | ;; along with GNU Emacs; see the file COPYING. If not, write to the | |
22 | ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
23 | ;; Boston, MA 02111-1307, USA. | |
24 | ||
25 | ;;; Commentary: | |
26 | ||
27 | ;; Provides a lightweight alternative to emerge/ediff. | |
28 | ;; To use it, simply add to your .emacs the following lines: | |
29 | ;; | |
30 | ;; (autoload 'smerge-mode "smerge-mode" nil t) | |
31 | ;; | |
32 | ;; you can even have it turned on automatically with the following | |
33 | ;; piece of code in your .emacs: | |
34 | ;; | |
35 | ;; (defun sm-try-smerge () | |
36 | ;; (save-excursion | |
37 | ;; (goto-char (point-min)) | |
38 | ;; (when (re-search-forward "^<<<<<<< " nil t) | |
39 | ;; (smerge-mode 1)))) | |
1a4914f3 | 40 | ;; (add-hook 'find-file-hook 'sm-try-smerge t) |
3dac25a9 | 41 | |
f97e9a8a SM |
42 | ;;; Todo: |
43 | ||
44 | ;; - if requested, ask the user whether he wants to call ediff right away | |
45 | ||
3dac25a9 SM |
46 | ;;; Code: |
47 | ||
48 | (eval-when-compile (require 'cl)) | |
49 | ||
50 | ||
51 | (defgroup smerge () | |
52 | "Minor mode to resolve diff3 conflicts." | |
53 | :group 'tools | |
54 | :prefix "smerge-") | |
55 | ||
814838df | 56 | (defcustom smerge-diff-buffer-name "*vc-diff*" |
3dac25a9 SM |
57 | "Buffer name to use for displaying diffs." |
58 | :group 'smerge | |
59 | :type '(choice | |
60 | (const "*vc-diff*") | |
61 | (const "*cvs-diff*") | |
62 | (const "*smerge-diff*") | |
63 | string)) | |
64 | ||
65 | (defcustom smerge-diff-switches | |
a7b77977 DL |
66 | (append '("-d" "-b") |
67 | (if (listp diff-switches) diff-switches (list diff-switches))) | |
3dac25a9 SM |
68 | "*A list of strings specifying switches to be be passed to diff. |
69 | Used in `smerge-diff-base-mine' and related functions." | |
70 | :group 'smerge | |
71 | :type '(repeat string)) | |
72 | ||
f97e9a8a SM |
73 | (defcustom smerge-auto-leave t |
74 | "*Non-nil means to leave `smerge-mode' when the last conflict is resolved." | |
75 | :group 'smerge | |
76 | :type 'boolean) | |
77 | ||
3dac25a9 | 78 | (defface smerge-mine-face |
b25f5aec MB |
79 | '((((background light)) |
80 | (:foreground "blue")) | |
81 | (((background dark)) | |
82 | (:foreground "cyan"))) | |
3dac25a9 SM |
83 | "Face for your code." |
84 | :group 'smerge) | |
85 | (defvar smerge-mine-face 'smerge-mine-face) | |
86 | ||
87 | (defface smerge-other-face | |
b25f5aec MB |
88 | '((((background light)) |
89 | (:foreground "darkgreen")) | |
90 | (((background dark)) | |
91 | (:foreground "lightgreen"))) | |
3dac25a9 SM |
92 | "Face for the other code." |
93 | :group 'smerge) | |
94 | (defvar smerge-other-face 'smerge-other-face) | |
95 | ||
96 | (defface smerge-base-face | |
b25f5aec MB |
97 | '((((background light)) |
98 | (:foreground "red")) | |
99 | (((background dark)) | |
100 | (:foreground "orange"))) | |
3dac25a9 SM |
101 | "Face for the base code." |
102 | :group 'smerge) | |
103 | (defvar smerge-base-face 'smerge-base-face) | |
104 | ||
105 | (defface smerge-markers-face | |
b25f5aec MB |
106 | '((((background light)) |
107 | (:background "grey85")) | |
108 | (((background dark)) | |
109 | (:background "grey30"))) | |
3dac25a9 SM |
110 | "Face for the conflict markers." |
111 | :group 'smerge) | |
112 | (defvar smerge-markers-face 'smerge-markers-face) | |
113 | ||
f97e9a8a | 114 | (easy-mmode-defmap smerge-basic-map |
0e86b6b0 | 115 | `(("n" . smerge-next) |
3dac25a9 | 116 | ("p" . smerge-prev) |
a48402c9 | 117 | ("r" . smerge-resolve) |
3dac25a9 SM |
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) | |
0e86b6b0 SM |
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)) | |
3dac25a9 | 128 | "The base keymap for `smerge-mode'.") |
3dac25a9 | 129 | |
0e86b6b0 | 130 | (defcustom smerge-command-prefix "\C-c^" |
3dac25a9 SM |
131 | "Prefix for `smerge-mode' commands." |
132 | :group 'smerge | |
0e86b6b0 | 133 | :type '(choice (string "\e") (string "\C-c^") (string "") string)) |
3dac25a9 | 134 | |
f97e9a8a SM |
135 | (easy-mmode-defmap smerge-mode-map |
136 | `((,smerge-command-prefix . ,smerge-basic-map)) | |
3dac25a9 SM |
137 | "Keymap for `smerge-mode'.") |
138 | ||
6b61353c KH |
139 | (defvar smerge-check-cache nil) |
140 | (make-variable-buffer-local 'smerge-check-cache) | |
141 | (defun smerge-check (n) | |
142 | (condition-case nil | |
143 | (let ((state (cons (point) (buffer-modified-tick)))) | |
144 | (unless (equal (cdr smerge-check-cache) state) | |
145 | (smerge-match-conflict) | |
146 | (setq smerge-check-cache (cons (match-data) state))) | |
147 | (nth (* 2 n) (car smerge-check-cache))) | |
148 | (error nil))) | |
149 | ||
3dac25a9 SM |
150 | (easy-menu-define smerge-mode-menu smerge-mode-map |
151 | "Menu for `smerge-mode'." | |
152 | '("SMerge" | |
43e764c9 | 153 | ["Next" smerge-next :help "Go to next conflict"] |
394bd1ca | 154 | ["Previous" smerge-prev :help "Go to previous conflict"] |
6b61353c KH |
155 | "--" |
156 | ["Keep All" smerge-keep-all :help "Keep all three versions" | |
157 | :active (smerge-check 1)] | |
158 | ["Keep Current" smerge-keep-current :help "Use current (at point) version" | |
159 | :active (and (smerge-check 1) (> (smerge-get-current) 0))] | |
160 | "--" | |
161 | ["Revert to Base" smerge-keep-base :help "Revert to base version" | |
162 | :active (smerge-check 2)] | |
163 | ["Keep Other" smerge-keep-other :help "Keep `other' version" | |
164 | :active (smerge-check 3)] | |
165 | ["Keep Yours" smerge-keep-mine :help "Keep your version" | |
166 | :active (smerge-check 1)] | |
43e764c9 DL |
167 | "--" |
168 | ["Diff Base/Mine" smerge-diff-base-mine | |
6b61353c KH |
169 | :help "Diff `base' and `mine' for current conflict" |
170 | :active (smerge-check 2)] | |
43e764c9 | 171 | ["Diff Base/Other" smerge-diff-base-other |
6b61353c KH |
172 | :help "Diff `base' and `other' for current conflict" |
173 | :active (smerge-check 2)] | |
43e764c9 | 174 | ["Diff Mine/Other" smerge-diff-mine-other |
6b61353c KH |
175 | :help "Diff `mine' and `other' for current conflict" |
176 | :active (smerge-check 1)] | |
43e764c9 DL |
177 | "--" |
178 | ["Invoke Ediff" smerge-ediff | |
6b61353c KH |
179 | :help "Use Ediff to resolve the conflicts" |
180 | :active (smerge-check 1)] | |
181 | ["Auto Resolve" smerge-resolve | |
182 | :help "Try auto-resolution heuristics" | |
183 | :active (smerge-check 1)] | |
184 | ["Combine" smerge-combine-with-next | |
185 | :help "Combine current conflict with next" | |
186 | :active (smerge-check 1)] | |
187 | )) | |
188 | ||
189 | (easy-menu-define smerge-context-menu nil | |
190 | "Context menu for mine area in `smerge-mode'." | |
191 | '(nil | |
192 | ["Keep Current" smerge-keep-current :help "Use current (at point) version"] | |
193 | ["Kill Current" smerge-kill-current :help "Remove current (at point) version"] | |
194 | ["Keep All" smerge-keep-all :help "Keep all three versions"] | |
195 | "---" | |
196 | ["More..." (popup-menu smerge-mode-menu) :help "Show full SMerge mode menu"] | |
3dac25a9 SM |
197 | )) |
198 | ||
199 | (defconst smerge-font-lock-keywords | |
200 | '((smerge-find-conflict | |
f0c1adab | 201 | (1 smerge-mine-face prepend t) |
3dac25a9 SM |
202 | (2 smerge-base-face prepend t) |
203 | (3 smerge-other-face prepend t) | |
0e86b6b0 | 204 | ;; FIXME: `keep' doesn't work right with syntactic fontification. |
3dac25a9 SM |
205 | (0 smerge-markers-face keep) |
206 | (4 nil t t) | |
207 | (5 nil t t))) | |
208 | "Font lock patterns for `smerge-mode'.") | |
209 | ||
210 | (defconst smerge-begin-re "^<<<<<<< \\(.*\\)\n") | |
211 | (defconst smerge-end-re "^>>>>>>> .*\n") | |
212 | (defconst smerge-base-re "^||||||| .*\n") | |
213 | (defconst smerge-other-re "^=======\n") | |
214 | ||
215 | (defvar smerge-conflict-style nil | |
216 | "Keep track of which style of conflict is in use. | |
217 | Can be nil if the style is undecided, or else: | |
218 | - `diff3-E' | |
219 | - `diff3-A'") | |
220 | ||
221 | ;; Compiler pacifiers | |
f5cd98c0 SM |
222 | (defvar font-lock-mode) |
223 | (defvar font-lock-keywords) | |
3dac25a9 SM |
224 | |
225 | ;;;; | |
226 | ;;;; Actual code | |
227 | ;;;; | |
228 | ||
f97e9a8a SM |
229 | ;; Define smerge-next and smerge-prev |
230 | (easy-mmode-define-navigation smerge smerge-begin-re "conflict") | |
3dac25a9 SM |
231 | |
232 | (defconst smerge-match-names ["conflict" "mine" "base" "other"]) | |
233 | ||
234 | (defun smerge-ensure-match (n) | |
235 | (unless (match-end n) | |
236 | (error (format "No `%s'" (aref smerge-match-names n))))) | |
237 | ||
f97e9a8a SM |
238 | (defun smerge-auto-leave () |
239 | (when (and smerge-auto-leave | |
240 | (save-excursion (goto-char (point-min)) | |
241 | (not (re-search-forward smerge-begin-re nil t)))) | |
242 | (smerge-mode -1))) | |
f1180544 | 243 | |
f97e9a8a | 244 | |
3dac25a9 | 245 | (defun smerge-keep-all () |
6b61353c | 246 | "Concatenate all versions." |
3dac25a9 SM |
247 | (interactive) |
248 | (smerge-match-conflict) | |
6b61353c KH |
249 | (let ((mb2 (or (match-beginning 2) (point-max))) |
250 | (me2 (or (match-end 2) (point-min)))) | |
251 | (delete-region (match-end 3) (match-end 0)) | |
252 | (delete-region (max me2 (match-end 1)) (match-beginning 3)) | |
253 | (if (and (match-end 2) (/= (match-end 1) (match-end 3))) | |
254 | (delete-region (match-end 1) (match-beginning 2))) | |
255 | (delete-region (match-beginning 0) (min (match-beginning 1) mb2)) | |
256 | (smerge-auto-leave))) | |
257 | ||
258 | (defun smerge-keep-n (n) | |
259 | ;; We used to use replace-match, but that did not preserve markers so well. | |
260 | (delete-region (match-end n) (match-end 0)) | |
261 | (delete-region (match-beginning 0) (match-beginning n))) | |
3dac25a9 | 262 | |
814838df SM |
263 | (defun smerge-combine-with-next () |
264 | "Combine the current conflict with the next one." | |
265 | (interactive) | |
266 | (smerge-match-conflict) | |
267 | (let ((ends nil)) | |
268 | (dolist (i '(3 2 1 0)) | |
269 | (push (if (match-end i) (copy-marker (match-end i) t)) ends)) | |
270 | (setq ends (apply 'vector ends)) | |
271 | (goto-char (aref ends 0)) | |
272 | (if (not (re-search-forward smerge-begin-re nil t)) | |
273 | (error "No next conflict") | |
274 | (smerge-match-conflict) | |
275 | (let ((match-data (mapcar (lambda (m) (if m (copy-marker m))) | |
276 | (match-data)))) | |
277 | ;; First copy the in-between text in each alternative. | |
278 | (dolist (i '(1 2 3)) | |
279 | (when (aref ends i) | |
280 | (goto-char (aref ends i)) | |
281 | (insert-buffer-substring (current-buffer) | |
282 | (aref ends 0) (car match-data)))) | |
283 | (delete-region (aref ends 0) (car match-data)) | |
284 | ;; Then move the second conflict's alternatives into the first. | |
285 | (dolist (i '(1 2 3)) | |
286 | (set-match-data match-data) | |
287 | (when (and (aref ends i) (match-end i)) | |
288 | (goto-char (aref ends i)) | |
289 | (insert-buffer-substring (current-buffer) | |
290 | (match-beginning i) (match-end i)))) | |
291 | (delete-region (car match-data) (cadr match-data)) | |
292 | ;; Free the markers. | |
293 | (dolist (m match-data) (if m (move-marker m nil))) | |
294 | (mapc (lambda (m) (if m (move-marker m nil))) ends))))) | |
295 | ||
a48402c9 SM |
296 | (defvar smerge-resolve-function |
297 | (lambda () (error "Don't know how to resolve")) | |
298 | "Mode-specific merge function. | |
299 | The function is called with no argument and with the match data set | |
300 | according to `smerge-match-conflict'.") | |
301 | ||
6b61353c KH |
302 | (defvar smerge-text-properties |
303 | `(help-echo "merge conflict: mouse-3 shows a menu" | |
304 | ;; mouse-face highlight | |
305 | keymap (keymap (down-mouse-3 . smerge-popup-context-menu)))) | |
306 | ||
307 | (defun smerge-remove-props (&optional beg end) | |
308 | (remove-text-properties | |
309 | (or beg (match-beginning 0)) | |
310 | (or end (match-end 0)) | |
311 | smerge-text-properties)) | |
312 | ||
313 | (defun smerge-popup-context-menu (event) | |
314 | "Pop up the Smerge mode context menu under mouse." | |
315 | (interactive "e") | |
316 | (if (and smerge-mode | |
317 | (save-excursion (posn-set-point (event-end event)) (smerge-check 1))) | |
318 | (progn | |
319 | (posn-set-point (event-end event)) | |
320 | (smerge-match-conflict) | |
321 | (let ((i (smerge-get-current)) | |
322 | o) | |
323 | (if (<= i 0) | |
324 | ;; Out of range | |
325 | (popup-menu smerge-mode-menu) | |
326 | ;; Install overlay. | |
327 | (setq o (make-overlay (match-beginning i) (match-end i))) | |
328 | (unwind-protect | |
329 | (progn | |
330 | (overlay-put o 'face 'highlight) | |
331 | (sit-for 0) ;Display the new highlighting. | |
332 | (popup-menu smerge-context-menu)) | |
333 | ;; Delete overlay. | |
334 | (delete-overlay o))))) | |
335 | ;; There's no conflict at point, the text-props are just obsolete. | |
336 | (save-excursion | |
337 | (let ((beg (re-search-backward smerge-end-re nil t)) | |
338 | (end (re-search-forward smerge-begin-re nil t))) | |
339 | (smerge-remove-props (or beg (point-min)) (or end (point-max))) | |
340 | (push event unread-command-events))))) | |
341 | ||
a48402c9 SM |
342 | (defun smerge-resolve () |
343 | "Resolve the conflict at point intelligently. | |
344 | This relies on mode-specific knowledge and thus only works in | |
345 | some major modes. Uses `smerge-resolve-function' to do the actual work." | |
346 | (interactive) | |
347 | (smerge-match-conflict) | |
6b61353c KH |
348 | (smerge-remove-props) |
349 | (cond | |
350 | ;; Trivial diff3 -A non-conflicts. | |
351 | ((and (eq (match-end 1) (match-end 3)) | |
352 | (eq (match-beginning 1) (match-beginning 3))) | |
353 | ;; FIXME: Add "if [ diff -b MINE OTHER ]; then select OTHER; fi" | |
354 | (smerge-keep-n 3)) | |
355 | ((and (match-end 2) | |
356 | ;; FIXME: Add "diff -b BASE MINE | patch OTHER". | |
357 | ;; FIXME: Add "diff -b BASE OTHER | patch MINE". | |
358 | nil) | |
359 | ) | |
360 | ((and (not (match-end 2)) | |
361 | ;; FIXME: Add "diff -b"-based refinement. | |
362 | nil) | |
363 | ) | |
364 | (t | |
365 | ;; Mode-specific conflict resolution. | |
366 | (funcall smerge-resolve-function))) | |
a48402c9 SM |
367 | (smerge-auto-leave)) |
368 | ||
3dac25a9 SM |
369 | (defun smerge-keep-base () |
370 | "Revert to the base version." | |
371 | (interactive) | |
372 | (smerge-match-conflict) | |
373 | (smerge-ensure-match 2) | |
6b61353c KH |
374 | (smerge-remove-props) |
375 | (smerge-keep-n 2) | |
f97e9a8a | 376 | (smerge-auto-leave)) |
3dac25a9 SM |
377 | |
378 | (defun smerge-keep-other () | |
379 | "Use \"other\" version." | |
380 | (interactive) | |
381 | (smerge-match-conflict) | |
382 | ;;(smerge-ensure-match 3) | |
6b61353c KH |
383 | (smerge-remove-props) |
384 | (smerge-keep-n 3) | |
f97e9a8a | 385 | (smerge-auto-leave)) |
3dac25a9 SM |
386 | |
387 | (defun smerge-keep-mine () | |
388 | "Keep your version." | |
389 | (interactive) | |
390 | (smerge-match-conflict) | |
391 | ;;(smerge-ensure-match 1) | |
6b61353c KH |
392 | (smerge-remove-props) |
393 | (smerge-keep-n 1) | |
f97e9a8a | 394 | (smerge-auto-leave)) |
3dac25a9 | 395 | |
6b61353c | 396 | (defun smerge-get-current () |
3dac25a9 SM |
397 | (let ((i 3)) |
398 | (while (or (not (match-end i)) | |
399 | (< (point) (match-beginning i)) | |
400 | (>= (point) (match-end i))) | |
401 | (decf i)) | |
6b61353c KH |
402 | i)) |
403 | ||
404 | (defun smerge-keep-current () | |
405 | "Use the current (under the cursor) version." | |
406 | (interactive) | |
407 | (smerge-match-conflict) | |
408 | (let ((i (smerge-get-current))) | |
3dac25a9 | 409 | (if (<= i 0) (error "Not inside a version") |
6b61353c KH |
410 | (smerge-remove-props) |
411 | (smerge-keep-n i) | |
f97e9a8a | 412 | (smerge-auto-leave)))) |
3dac25a9 | 413 | |
6b61353c KH |
414 | (defun smerge-kill-current () |
415 | "Remove the current (under the cursor) version." | |
416 | (interactive) | |
417 | (smerge-match-conflict) | |
418 | (let ((i (smerge-get-current))) | |
419 | (if (<= i 0) (error "Not inside a version") | |
420 | (smerge-remove-props) | |
421 | (let ((left nil)) | |
422 | (dolist (n '(3 2 1)) | |
423 | (if (and (match-end n) (/= (match-end n) (match-end i))) | |
424 | (push n left))) | |
425 | (if (and (cdr left) | |
426 | (/= (match-end (car left)) (match-end (cadr left)))) | |
427 | (ding) ;We don't know how to do that. | |
428 | (smerge-keep-n (car left)) | |
429 | (smerge-auto-leave)))))) | |
430 | ||
3dac25a9 SM |
431 | (defun smerge-diff-base-mine () |
432 | "Diff 'base' and 'mine' version in current conflict region." | |
433 | (interactive) | |
434 | (smerge-diff 2 1)) | |
435 | ||
436 | (defun smerge-diff-base-other () | |
437 | "Diff 'base' and 'other' version in current conflict region." | |
438 | (interactive) | |
439 | (smerge-diff 2 3)) | |
440 | ||
441 | (defun smerge-diff-mine-other () | |
442 | "Diff 'mine' and 'other' version in current conflict region." | |
443 | (interactive) | |
444 | (smerge-diff 1 3)) | |
445 | ||
446 | (defun smerge-match-conflict () | |
447 | "Get info about the conflict. Puts the info in the `match-data'. | |
448 | The submatches contain: | |
449 | 0: the whole conflict. | |
450 | 1: your code. | |
451 | 2: the base code. | |
452 | 3: other code. | |
453 | An error is raised if not inside a conflict." | |
454 | (save-excursion | |
455 | (condition-case nil | |
456 | (let* ((orig-point (point)) | |
457 | ||
458 | (_ (forward-line 1)) | |
459 | (_ (re-search-backward smerge-begin-re)) | |
460 | ||
461 | (start (match-beginning 0)) | |
462 | (mine-start (match-end 0)) | |
a48402c9 | 463 | (filename (or (match-string 1) "")) |
3dac25a9 SM |
464 | |
465 | (_ (re-search-forward smerge-end-re)) | |
466 | (_ (assert (< orig-point (match-end 0)))) | |
f1180544 | 467 | |
3dac25a9 SM |
468 | (other-end (match-beginning 0)) |
469 | (end (match-end 0)) | |
470 | ||
471 | (_ (re-search-backward smerge-other-re start)) | |
472 | ||
473 | (mine-end (match-beginning 0)) | |
474 | (other-start (match-end 0)) | |
475 | ||
476 | base-start base-end) | |
477 | ||
478 | ;; handle the various conflict styles | |
479 | (cond | |
480 | ((re-search-backward smerge-base-re start t) | |
481 | ;; a 3-parts conflict | |
482 | (set (make-local-variable 'smerge-conflict-style) 'diff3-A) | |
483 | (setq base-end mine-end) | |
484 | (setq mine-end (match-beginning 0)) | |
485 | (setq base-start (match-end 0))) | |
486 | ||
6b61353c KH |
487 | ((string= filename (file-name-nondirectory |
488 | (or buffer-file-name ""))) | |
489 | ;; a 2-parts conflict | |
490 | (set (make-local-variable 'smerge-conflict-style) 'diff3-E)) | |
491 | ||
492 | ((and (not base-start) | |
493 | (or (eq smerge-conflict-style 'diff3-A) | |
494 | (equal filename "ANCESTOR") | |
495 | (string-match "\\`[.0-9]+\\'" filename))) | |
496 | ;; a same-diff conflict | |
497 | (setq base-start mine-start) | |
498 | (setq base-end mine-end) | |
499 | (setq mine-start other-start) | |
500 | (setq mine-end other-end))) | |
501 | ||
502 | (let ((inhibit-read-only t) | |
503 | (inhibit-modification-hooks t) | |
504 | (m (buffer-modified-p))) | |
505 | (unwind-protect | |
506 | (add-text-properties start end smerge-text-properties) | |
507 | (restore-buffer-modified-p m))) | |
508 | ||
3dac25a9 SM |
509 | (store-match-data (list start end |
510 | mine-start mine-end | |
511 | base-start base-end | |
512 | other-start other-end | |
513 | (when base-start (1- base-start)) base-start | |
514 | (1- other-start) other-start)) | |
515 | t) | |
e29f823e | 516 | (search-failed (error "Point not in conflict region"))))) |
3dac25a9 SM |
517 | |
518 | (defun smerge-find-conflict (&optional limit) | |
519 | "Find and match a conflict region. Intended as a font-lock MATCHER. | |
520 | The submatches are the same as in `smerge-match-conflict'. | |
521 | Returns non-nil if a match is found between the point and LIMIT. | |
522 | The point is moved to the end of the conflict." | |
523 | (when (re-search-forward smerge-begin-re limit t) | |
524 | (ignore-errors | |
525 | (smerge-match-conflict) | |
526 | (goto-char (match-end 0))))) | |
527 | ||
528 | (defun smerge-diff (n1 n2) | |
529 | (smerge-match-conflict) | |
530 | (smerge-ensure-match n1) | |
531 | (smerge-ensure-match n2) | |
532 | (let ((name1 (aref smerge-match-names n1)) | |
533 | (name2 (aref smerge-match-names n2)) | |
e29f823e SM |
534 | ;; Read them before the match-data gets clobbered. |
535 | (beg1 (match-beginning n1)) | |
536 | (end1 (match-end n1)) | |
537 | (beg2 (match-beginning n2)) | |
538 | (end2 (match-end n2)) | |
3dac25a9 | 539 | (file1 (make-temp-file "smerge1")) |
d73aed13 SM |
540 | (file2 (make-temp-file "smerge2")) |
541 | (dir default-directory) | |
e29f823e SM |
542 | (file (file-relative-name buffer-file-name)) |
543 | (coding-system-for-read buffer-file-coding-system)) | |
814838df SM |
544 | (write-region beg1 end1 file1 nil 'nomessage) |
545 | (write-region beg2 end2 file2 nil 'nomessage) | |
3dac25a9 SM |
546 | (unwind-protect |
547 | (with-current-buffer (get-buffer-create smerge-diff-buffer-name) | |
d73aed13 | 548 | (setq default-directory dir) |
3dac25a9 SM |
549 | (let ((inhibit-read-only t)) |
550 | (erase-buffer) | |
814838df SM |
551 | (let ((status |
552 | (apply 'call-process diff-command nil t nil | |
553 | (append smerge-diff-switches | |
554 | (list "-L" (concat name1 "/" file) | |
555 | "-L" (concat name2 "/" file) | |
556 | file1 file2))))) | |
557 | (if (eq status 0) (insert "No differences found.\n")))) | |
3dac25a9 SM |
558 | (goto-char (point-min)) |
559 | (diff-mode) | |
560 | (display-buffer (current-buffer) t)) | |
561 | (delete-file file1) | |
562 | (delete-file file2)))) | |
563 | ||
814838df SM |
564 | ;; compiler pacifiers |
565 | (defvar smerge-ediff-windows) | |
566 | (defvar smerge-ediff-buf) | |
567 | (defvar ediff-buffer-A) | |
568 | (defvar ediff-buffer-B) | |
569 | (defvar ediff-buffer-C) | |
3dac25a9 | 570 | |
a1038ca0 | 571 | ;;;###autoload |
15092da1 SM |
572 | (defun smerge-ediff (&optional name-mine name-other name-base) |
573 | "Invoke ediff to resolve the conflicts. | |
574 | NAME-MINE, NAME-OTHER, and NAME-BASE, if non-nil, are used for the | |
575 | buffer names." | |
3dac25a9 SM |
576 | (interactive) |
577 | (let* ((buf (current-buffer)) | |
578 | (mode major-mode) | |
579 | ;;(ediff-default-variant 'default-B) | |
580 | (config (current-window-configuration)) | |
581 | (filename (file-name-nondirectory buffer-file-name)) | |
15092da1 SM |
582 | (mine (generate-new-buffer |
583 | (or name-mine (concat "*" filename " MINE*")))) | |
584 | (other (generate-new-buffer | |
585 | (or name-other (concat "*" filename " OTHER*")))) | |
3dac25a9 SM |
586 | base) |
587 | (with-current-buffer mine | |
588 | (buffer-disable-undo) | |
589 | (insert-buffer-substring buf) | |
590 | (goto-char (point-min)) | |
591 | (while (smerge-find-conflict) | |
592 | (when (match-beginning 2) (setq base t)) | |
6b61353c | 593 | (smerge-keep-n 1)) |
3dac25a9 SM |
594 | (buffer-enable-undo) |
595 | (set-buffer-modified-p nil) | |
596 | (funcall mode)) | |
597 | ||
598 | (with-current-buffer other | |
599 | (buffer-disable-undo) | |
600 | (insert-buffer-substring buf) | |
601 | (goto-char (point-min)) | |
602 | (while (smerge-find-conflict) | |
6b61353c | 603 | (smerge-keep-n 3)) |
3dac25a9 SM |
604 | (buffer-enable-undo) |
605 | (set-buffer-modified-p nil) | |
606 | (funcall mode)) | |
f1180544 | 607 | |
3dac25a9 | 608 | (when base |
15092da1 SM |
609 | (setq base (generate-new-buffer |
610 | (or name-base (concat "*" filename " BASE*")))) | |
3dac25a9 SM |
611 | (with-current-buffer base |
612 | (buffer-disable-undo) | |
613 | (insert-buffer-substring buf) | |
614 | (goto-char (point-min)) | |
615 | (while (smerge-find-conflict) | |
6b61353c KH |
616 | (if (match-end 2) |
617 | (smerge-keep-n 2) | |
618 | (delete-region (match-beginning 0) (match-end 0)))) | |
3dac25a9 SM |
619 | (buffer-enable-undo) |
620 | (set-buffer-modified-p nil) | |
621 | (funcall mode))) | |
f1180544 | 622 | |
3dac25a9 SM |
623 | ;; the rest of the code is inspired from vc.el |
624 | ;; Fire up ediff. | |
625 | (set-buffer | |
626 | (if base | |
627 | (ediff-merge-buffers-with-ancestor mine other base) | |
628 | ;; nil 'ediff-merge-revisions-with-ancestor buffer-file-name) | |
629 | (ediff-merge-buffers mine other))) | |
630 | ;; nil 'ediff-merge-revisions buffer-file-name))) | |
f1180544 | 631 | |
3dac25a9 SM |
632 | ;; Ediff is now set up, and we are in the control buffer. |
633 | ;; Do a few further adjustments and take precautions for exit. | |
634 | (set (make-local-variable 'smerge-ediff-windows) config) | |
635 | (set (make-local-variable 'smerge-ediff-buf) buf) | |
636 | (set (make-local-variable 'ediff-quit-hook) | |
637 | (lambda () | |
638 | (let ((buffer-A ediff-buffer-A) | |
639 | (buffer-B ediff-buffer-B) | |
640 | (buffer-C ediff-buffer-C) | |
641 | (buffer-Ancestor ediff-ancestor-buffer) | |
642 | (buf smerge-ediff-buf) | |
643 | (windows smerge-ediff-windows)) | |
644 | (ediff-cleanup-mess) | |
645 | (with-current-buffer buf | |
646 | (erase-buffer) | |
647 | (insert-buffer buffer-C) | |
648 | (kill-buffer buffer-A) | |
649 | (kill-buffer buffer-B) | |
650 | (kill-buffer buffer-C) | |
651 | (when (bufferp buffer-Ancestor) (kill-buffer buffer-Ancestor)) | |
652 | (set-window-configuration windows) | |
653 | (message "Conflict resolution finished; you may save the buffer"))))) | |
654 | (message "Please resolve conflicts now; exit ediff when done"))) | |
655 | ||
656 | ||
657 | ;;;###autoload | |
658 | (define-minor-mode smerge-mode | |
659 | "Minor mode to simplify editing output from the diff3 program. | |
660 | \\{smerge-mode-map}" | |
661 | nil " SMerge" nil | |
0304b9c7 | 662 | (when (and (boundp 'font-lock-mode) font-lock-mode) |
0f49941f | 663 | (set (make-local-variable 'font-lock-multiline) t) |
3dac25a9 SM |
664 | (save-excursion |
665 | (if smerge-mode | |
666 | (font-lock-add-keywords nil smerge-font-lock-keywords 'append) | |
667 | (font-lock-remove-keywords nil smerge-font-lock-keywords)) | |
668 | (goto-char (point-min)) | |
669 | (while (smerge-find-conflict) | |
6eabfb26 SM |
670 | (save-excursion |
671 | (font-lock-fontify-region (match-beginning 0) (match-end 0) nil)))))) | |
3dac25a9 SM |
672 | |
673 | ||
674 | (provide 'smerge-mode) | |
6b61353c KH |
675 | |
676 | ;;; arch-tag: 605c8d1e-e43d-4943-a6f3-1bcc4333e690 | |
3dac25a9 | 677 | ;;; smerge-mode.el ends here |