Commit | Line | Data |
---|---|---|
3dac25a9 SM |
1 | ;;; smerge-mode.el --- Minor mode to resolve diff3 conflicts |
2 | ||
0d30b337 TTN |
3 | ;; Copyright (C) 1999, 2000, 2001, 2002, 2003, |
4 | ;; 2004, 2005 Free Software Foundation, Inc. | |
3dac25a9 SM |
5 | |
6 | ;; Author: Stefan Monnier <monnier@cs.yale.edu> | |
11ece56b | 7 | ;; Keywords: revision-control merge diff3 cvs conflict |
3dac25a9 SM |
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 | |
086add15 LK |
23 | ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
24 | ;; Boston, MA 02110-1301, USA. | |
3dac25a9 SM |
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)))) | |
1a4914f3 | 41 | ;; (add-hook 'find-file-hook 'sm-try-smerge t) |
3dac25a9 | 42 | |
f97e9a8a SM |
43 | ;;; Todo: |
44 | ||
45 | ;; - if requested, ask the user whether he wants to call ediff right away | |
46 | ||
3dac25a9 SM |
47 | ;;; Code: |
48 | ||
49 | (eval-when-compile (require 'cl)) | |
50 | ||
51 | ||
7f84c46e RS |
52 | ;;; The real definition comes later. |
53 | (defvar smerge-mode) | |
54 | ||
3dac25a9 | 55 | (defgroup smerge () |
48d59eda | 56 | "Minor mode to highlight and resolve diff3 conflicts." |
3dac25a9 SM |
57 | :group 'tools |
58 | :prefix "smerge-") | |
59 | ||
814838df | 60 | (defcustom smerge-diff-buffer-name "*vc-diff*" |
3dac25a9 SM |
61 | "Buffer name to use for displaying diffs." |
62 | :group 'smerge | |
63 | :type '(choice | |
64 | (const "*vc-diff*") | |
65 | (const "*cvs-diff*") | |
66 | (const "*smerge-diff*") | |
67 | string)) | |
68 | ||
69 | (defcustom smerge-diff-switches | |
a7b77977 DL |
70 | (append '("-d" "-b") |
71 | (if (listp diff-switches) diff-switches (list diff-switches))) | |
48d59eda | 72 | "A list of strings specifying switches to be passed to diff. |
3dac25a9 SM |
73 | Used in `smerge-diff-base-mine' and related functions." |
74 | :group 'smerge | |
75 | :type '(repeat string)) | |
76 | ||
f97e9a8a | 77 | (defcustom smerge-auto-leave t |
48d59eda | 78 | "Non-nil means to leave `smerge-mode' when the last conflict is resolved." |
f97e9a8a SM |
79 | :group 'smerge |
80 | :type 'boolean) | |
81 | ||
e8bfdf82 | 82 | (defface smerge-mine |
ea81d57e DN |
83 | '((((min-colors 88) (background light)) |
84 | (:foreground "blue1")) | |
85 | (((background light)) | |
b25f5aec | 86 | (:foreground "blue")) |
ea81d57e DN |
87 | (((min-colors 88) (background dark)) |
88 | (:foreground "cyan1")) | |
b25f5aec MB |
89 | (((background dark)) |
90 | (:foreground "cyan"))) | |
3dac25a9 SM |
91 | "Face for your code." |
92 | :group 'smerge) | |
e8bfdf82 MB |
93 | ;; backward-compatibility alias |
94 | (put 'smerge-mine-face 'face-alias 'smerge-mine) | |
95 | (defvar smerge-mine-face 'smerge-mine) | |
3dac25a9 | 96 | |
e8bfdf82 | 97 | (defface smerge-other |
b25f5aec MB |
98 | '((((background light)) |
99 | (:foreground "darkgreen")) | |
100 | (((background dark)) | |
101 | (:foreground "lightgreen"))) | |
3dac25a9 SM |
102 | "Face for the other code." |
103 | :group 'smerge) | |
e8bfdf82 MB |
104 | ;; backward-compatibility alias |
105 | (put 'smerge-other-face 'face-alias 'smerge-other) | |
106 | (defvar smerge-other-face 'smerge-other) | |
3dac25a9 | 107 | |
e8bfdf82 | 108 | (defface smerge-base |
ea81d57e DN |
109 | '((((min-colors 88) (background light)) |
110 | (:foreground "red1")) | |
111 | (((background light)) | |
b25f5aec MB |
112 | (:foreground "red")) |
113 | (((background dark)) | |
114 | (:foreground "orange"))) | |
3dac25a9 SM |
115 | "Face for the base code." |
116 | :group 'smerge) | |
e8bfdf82 MB |
117 | ;; backward-compatibility alias |
118 | (put 'smerge-base-face 'face-alias 'smerge-base) | |
119 | (defvar smerge-base-face 'smerge-base) | |
3dac25a9 | 120 | |
e8bfdf82 | 121 | (defface smerge-markers |
b25f5aec MB |
122 | '((((background light)) |
123 | (:background "grey85")) | |
124 | (((background dark)) | |
125 | (:background "grey30"))) | |
3dac25a9 SM |
126 | "Face for the conflict markers." |
127 | :group 'smerge) | |
e8bfdf82 MB |
128 | ;; backward-compatibility alias |
129 | (put 'smerge-markers-face 'face-alias 'smerge-markers) | |
130 | (defvar smerge-markers-face 'smerge-markers) | |
3dac25a9 | 131 | |
41796d09 SM |
132 | (defface smerge-refined-change |
133 | '((t :background "yellow")) | |
a6022f15 JB |
134 | "Face used for char-based changes shown by `smerge-refine'." |
135 | :group 'smerge) | |
41796d09 | 136 | |
f97e9a8a | 137 | (easy-mmode-defmap smerge-basic-map |
0e86b6b0 | 138 | `(("n" . smerge-next) |
3dac25a9 | 139 | ("p" . smerge-prev) |
a48402c9 | 140 | ("r" . smerge-resolve) |
3dac25a9 SM |
141 | ("a" . smerge-keep-all) |
142 | ("b" . smerge-keep-base) | |
143 | ("o" . smerge-keep-other) | |
144 | ("m" . smerge-keep-mine) | |
145 | ("E" . smerge-ediff) | |
48d59eda | 146 | ("C" . smerge-combine-with-next) |
41796d09 | 147 | ("R" . smerge-refine) |
3dac25a9 | 148 | ("\C-m" . smerge-keep-current) |
0e86b6b0 SM |
149 | ("=" . ,(make-sparse-keymap "Diff")) |
150 | ("=<" "base-mine" . smerge-diff-base-mine) | |
151 | ("=>" "base-other" . smerge-diff-base-other) | |
152 | ("==" "mine-other" . smerge-diff-mine-other)) | |
3dac25a9 | 153 | "The base keymap for `smerge-mode'.") |
3dac25a9 | 154 | |
0e86b6b0 | 155 | (defcustom smerge-command-prefix "\C-c^" |
3dac25a9 SM |
156 | "Prefix for `smerge-mode' commands." |
157 | :group 'smerge | |
0e86b6b0 | 158 | :type '(choice (string "\e") (string "\C-c^") (string "") string)) |
3dac25a9 | 159 | |
f97e9a8a SM |
160 | (easy-mmode-defmap smerge-mode-map |
161 | `((,smerge-command-prefix . ,smerge-basic-map)) | |
3dac25a9 SM |
162 | "Keymap for `smerge-mode'.") |
163 | ||
7d85a64e SM |
164 | (defvar smerge-check-cache nil) |
165 | (make-variable-buffer-local 'smerge-check-cache) | |
166 | (defun smerge-check (n) | |
167 | (condition-case nil | |
168 | (let ((state (cons (point) (buffer-modified-tick)))) | |
169 | (unless (equal (cdr smerge-check-cache) state) | |
170 | (smerge-match-conflict) | |
171 | (setq smerge-check-cache (cons (match-data) state))) | |
172 | (nth (* 2 n) (car smerge-check-cache))) | |
173 | (error nil))) | |
174 | ||
3dac25a9 SM |
175 | (easy-menu-define smerge-mode-menu smerge-mode-map |
176 | "Menu for `smerge-mode'." | |
177 | '("SMerge" | |
43e764c9 | 178 | ["Next" smerge-next :help "Go to next conflict"] |
394bd1ca | 179 | ["Previous" smerge-prev :help "Go to previous conflict"] |
7d85a64e SM |
180 | "--" |
181 | ["Keep All" smerge-keep-all :help "Keep all three versions" | |
182 | :active (smerge-check 1)] | |
183 | ["Keep Current" smerge-keep-current :help "Use current (at point) version" | |
184 | :active (and (smerge-check 1) (> (smerge-get-current) 0))] | |
185 | "--" | |
186 | ["Revert to Base" smerge-keep-base :help "Revert to base version" | |
187 | :active (smerge-check 2)] | |
188 | ["Keep Other" smerge-keep-other :help "Keep `other' version" | |
189 | :active (smerge-check 3)] | |
190 | ["Keep Yours" smerge-keep-mine :help "Keep your version" | |
191 | :active (smerge-check 1)] | |
43e764c9 DL |
192 | "--" |
193 | ["Diff Base/Mine" smerge-diff-base-mine | |
7d85a64e SM |
194 | :help "Diff `base' and `mine' for current conflict" |
195 | :active (smerge-check 2)] | |
43e764c9 | 196 | ["Diff Base/Other" smerge-diff-base-other |
7d85a64e SM |
197 | :help "Diff `base' and `other' for current conflict" |
198 | :active (smerge-check 2)] | |
43e764c9 | 199 | ["Diff Mine/Other" smerge-diff-mine-other |
7d85a64e SM |
200 | :help "Diff `mine' and `other' for current conflict" |
201 | :active (smerge-check 1)] | |
43e764c9 DL |
202 | "--" |
203 | ["Invoke Ediff" smerge-ediff | |
7d85a64e SM |
204 | :help "Use Ediff to resolve the conflicts" |
205 | :active (smerge-check 1)] | |
206 | ["Auto Resolve" smerge-resolve | |
5bd8d87b SM |
207 | :help "Try auto-resolution heuristics" |
208 | :active (smerge-check 1)] | |
7d85a64e SM |
209 | ["Combine" smerge-combine-with-next |
210 | :help "Combine current conflict with next" | |
211 | :active (smerge-check 1)] | |
3dac25a9 SM |
212 | )) |
213 | ||
11ece56b MY |
214 | (easy-menu-define smerge-context-menu nil |
215 | "Context menu for mine area in `smerge-mode'." | |
216 | '(nil | |
217 | ["Keep Current" smerge-keep-current :help "Use current (at point) version"] | |
218 | ["Kill Current" smerge-kill-current :help "Remove current (at point) version"] | |
219 | ["Keep All" smerge-keep-all :help "Keep all three versions"] | |
220 | "---" | |
221 | ["More..." (popup-menu smerge-mode-menu) :help "Show full SMerge mode menu"] | |
222 | )) | |
223 | ||
3dac25a9 SM |
224 | (defconst smerge-font-lock-keywords |
225 | '((smerge-find-conflict | |
f0c1adab | 226 | (1 smerge-mine-face prepend t) |
3dac25a9 SM |
227 | (2 smerge-base-face prepend t) |
228 | (3 smerge-other-face prepend t) | |
0e86b6b0 | 229 | ;; FIXME: `keep' doesn't work right with syntactic fontification. |
3dac25a9 SM |
230 | (0 smerge-markers-face keep) |
231 | (4 nil t t) | |
232 | (5 nil t t))) | |
233 | "Font lock patterns for `smerge-mode'.") | |
234 | ||
235 | (defconst smerge-begin-re "^<<<<<<< \\(.*\\)\n") | |
236 | (defconst smerge-end-re "^>>>>>>> .*\n") | |
237 | (defconst smerge-base-re "^||||||| .*\n") | |
238 | (defconst smerge-other-re "^=======\n") | |
239 | ||
240 | (defvar smerge-conflict-style nil | |
241 | "Keep track of which style of conflict is in use. | |
242 | Can be nil if the style is undecided, or else: | |
243 | - `diff3-E' | |
244 | - `diff3-A'") | |
245 | ||
246 | ;; Compiler pacifiers | |
8f6cea29 DL |
247 | (defvar font-lock-mode) |
248 | (defvar font-lock-keywords) | |
3dac25a9 SM |
249 | |
250 | ;;;; | |
251 | ;;;; Actual code | |
252 | ;;;; | |
253 | ||
f97e9a8a SM |
254 | ;; Define smerge-next and smerge-prev |
255 | (easy-mmode-define-navigation smerge smerge-begin-re "conflict") | |
3dac25a9 SM |
256 | |
257 | (defconst smerge-match-names ["conflict" "mine" "base" "other"]) | |
258 | ||
259 | (defun smerge-ensure-match (n) | |
260 | (unless (match-end n) | |
376166e6 | 261 | (error "No `%s'" (aref smerge-match-names n)))) |
3dac25a9 | 262 | |
f97e9a8a SM |
263 | (defun smerge-auto-leave () |
264 | (when (and smerge-auto-leave | |
265 | (save-excursion (goto-char (point-min)) | |
266 | (not (re-search-forward smerge-begin-re nil t)))) | |
48d59eda SM |
267 | (when (and (listp buffer-undo-list) smerge-mode) |
268 | (push (list 'apply 'smerge-mode 1) buffer-undo-list)) | |
f97e9a8a | 269 | (smerge-mode -1))) |
f1180544 | 270 | |
f97e9a8a | 271 | |
3dac25a9 | 272 | (defun smerge-keep-all () |
5bd8d87b | 273 | "Concatenate all versions." |
3dac25a9 SM |
274 | (interactive) |
275 | (smerge-match-conflict) | |
5bd8d87b SM |
276 | (let ((mb2 (or (match-beginning 2) (point-max))) |
277 | (me2 (or (match-end 2) (point-min)))) | |
278 | (delete-region (match-end 3) (match-end 0)) | |
279 | (delete-region (max me2 (match-end 1)) (match-beginning 3)) | |
280 | (if (and (match-end 2) (/= (match-end 1) (match-end 3))) | |
281 | (delete-region (match-end 1) (match-beginning 2))) | |
282 | (delete-region (match-beginning 0) (min (match-beginning 1) mb2)) | |
283 | (smerge-auto-leave))) | |
284 | ||
285 | (defun smerge-keep-n (n) | |
41796d09 | 286 | (smerge-remove-props (match-beginning 0) (match-end 0)) |
5bd8d87b SM |
287 | ;; We used to use replace-match, but that did not preserve markers so well. |
288 | (delete-region (match-end n) (match-end 0)) | |
289 | (delete-region (match-beginning 0) (match-beginning n))) | |
3dac25a9 | 290 | |
814838df SM |
291 | (defun smerge-combine-with-next () |
292 | "Combine the current conflict with the next one." | |
293 | (interactive) | |
294 | (smerge-match-conflict) | |
295 | (let ((ends nil)) | |
296 | (dolist (i '(3 2 1 0)) | |
297 | (push (if (match-end i) (copy-marker (match-end i) t)) ends)) | |
298 | (setq ends (apply 'vector ends)) | |
299 | (goto-char (aref ends 0)) | |
300 | (if (not (re-search-forward smerge-begin-re nil t)) | |
301 | (error "No next conflict") | |
302 | (smerge-match-conflict) | |
303 | (let ((match-data (mapcar (lambda (m) (if m (copy-marker m))) | |
304 | (match-data)))) | |
305 | ;; First copy the in-between text in each alternative. | |
306 | (dolist (i '(1 2 3)) | |
307 | (when (aref ends i) | |
308 | (goto-char (aref ends i)) | |
309 | (insert-buffer-substring (current-buffer) | |
310 | (aref ends 0) (car match-data)))) | |
311 | (delete-region (aref ends 0) (car match-data)) | |
312 | ;; Then move the second conflict's alternatives into the first. | |
313 | (dolist (i '(1 2 3)) | |
314 | (set-match-data match-data) | |
315 | (when (and (aref ends i) (match-end i)) | |
316 | (goto-char (aref ends i)) | |
317 | (insert-buffer-substring (current-buffer) | |
318 | (match-beginning i) (match-end i)))) | |
319 | (delete-region (car match-data) (cadr match-data)) | |
320 | ;; Free the markers. | |
321 | (dolist (m match-data) (if m (move-marker m nil))) | |
322 | (mapc (lambda (m) (if m (move-marker m nil))) ends))))) | |
323 | ||
a48402c9 SM |
324 | (defvar smerge-resolve-function |
325 | (lambda () (error "Don't know how to resolve")) | |
326 | "Mode-specific merge function. | |
327 | The function is called with no argument and with the match data set | |
328 | according to `smerge-match-conflict'.") | |
48d59eda | 329 | (add-to-list 'debug-ignored-errors "Don't know how to resolve") |
a48402c9 | 330 | |
11ece56b MY |
331 | (defvar smerge-text-properties |
332 | `(help-echo "merge conflict: mouse-3 shows a menu" | |
333 | ;; mouse-face highlight | |
334 | keymap (keymap (down-mouse-3 . smerge-popup-context-menu)))) | |
335 | ||
41796d09 SM |
336 | (defun smerge-remove-props (beg end) |
337 | (remove-overlays beg end 'smerge 'refine) | |
48d59eda | 338 | (remove-overlays beg end 'smerge 'conflict)) |
11ece56b MY |
339 | |
340 | (defun smerge-popup-context-menu (event) | |
341 | "Pop up the Smerge mode context menu under mouse." | |
342 | (interactive "e") | |
343 | (if (and smerge-mode | |
65114860 | 344 | (save-excursion (posn-set-point (event-end event)) (smerge-check 1))) |
11ece56b | 345 | (progn |
65114860 | 346 | (posn-set-point (event-end event)) |
5bd8d87b SM |
347 | (smerge-match-conflict) |
348 | (let ((i (smerge-get-current)) | |
349 | o) | |
350 | (if (<= i 0) | |
351 | ;; Out of range | |
352 | (popup-menu smerge-mode-menu) | |
353 | ;; Install overlay. | |
3b0af402 | 354 | (setq o (make-overlay (match-beginning i) (match-end i))) |
5bd8d87b SM |
355 | (unwind-protect |
356 | (progn | |
357 | (overlay-put o 'face 'highlight) | |
358 | (sit-for 0) ;Display the new highlighting. | |
359 | (popup-menu smerge-context-menu)) | |
360 | ;; Delete overlay. | |
361 | (delete-overlay o))))) | |
11ece56b MY |
362 | ;; There's no conflict at point, the text-props are just obsolete. |
363 | (save-excursion | |
364 | (let ((beg (re-search-backward smerge-end-re nil t)) | |
5bd8d87b SM |
365 | (end (re-search-forward smerge-begin-re nil t))) |
366 | (smerge-remove-props (or beg (point-min)) (or end (point-max))) | |
367 | (push event unread-command-events))))) | |
11ece56b | 368 | |
a48402c9 SM |
369 | (defun smerge-resolve () |
370 | "Resolve the conflict at point intelligently. | |
371 | This relies on mode-specific knowledge and thus only works in | |
372 | some major modes. Uses `smerge-resolve-function' to do the actual work." | |
373 | (interactive) | |
374 | (smerge-match-conflict) | |
f57b45cf | 375 | (smerge-remove-props (match-beginning 0) (match-end 0)) |
5bd8d87b SM |
376 | (cond |
377 | ;; Trivial diff3 -A non-conflicts. | |
378 | ((and (eq (match-end 1) (match-end 3)) | |
379 | (eq (match-beginning 1) (match-beginning 3))) | |
5bd8d87b | 380 | (smerge-keep-n 3)) |
48d59eda SM |
381 | ;; Mode-specific conflict resolution. |
382 | ((condition-case nil | |
383 | (atomic-change-group | |
384 | (funcall smerge-resolve-function) | |
385 | t) | |
386 | (error nil)) | |
387 | ;; Nothing to do: the resolution function has done it already. | |
388 | nil) | |
389 | ;; FIXME: Add "if [ diff -b MINE OTHER ]; then select OTHER; fi" | |
5bd8d87b SM |
390 | ((and (match-end 2) |
391 | ;; FIXME: Add "diff -b BASE MINE | patch OTHER". | |
392 | ;; FIXME: Add "diff -b BASE OTHER | patch MINE". | |
393 | nil) | |
394 | ) | |
395 | ((and (not (match-end 2)) | |
396 | ;; FIXME: Add "diff -b"-based refinement. | |
397 | nil) | |
398 | ) | |
399 | (t | |
48d59eda | 400 | (error "Don't know how to resolve"))) |
a48402c9 SM |
401 | (smerge-auto-leave)) |
402 | ||
3dac25a9 SM |
403 | (defun smerge-keep-base () |
404 | "Revert to the base version." | |
405 | (interactive) | |
406 | (smerge-match-conflict) | |
407 | (smerge-ensure-match 2) | |
5bd8d87b | 408 | (smerge-keep-n 2) |
f97e9a8a | 409 | (smerge-auto-leave)) |
3dac25a9 SM |
410 | |
411 | (defun smerge-keep-other () | |
412 | "Use \"other\" version." | |
413 | (interactive) | |
414 | (smerge-match-conflict) | |
415 | ;;(smerge-ensure-match 3) | |
5bd8d87b | 416 | (smerge-keep-n 3) |
f97e9a8a | 417 | (smerge-auto-leave)) |
3dac25a9 SM |
418 | |
419 | (defun smerge-keep-mine () | |
420 | "Keep your version." | |
421 | (interactive) | |
422 | (smerge-match-conflict) | |
423 | ;;(smerge-ensure-match 1) | |
5bd8d87b | 424 | (smerge-keep-n 1) |
f97e9a8a | 425 | (smerge-auto-leave)) |
3dac25a9 | 426 | |
7d85a64e | 427 | (defun smerge-get-current () |
3dac25a9 SM |
428 | (let ((i 3)) |
429 | (while (or (not (match-end i)) | |
430 | (< (point) (match-beginning i)) | |
431 | (>= (point) (match-end i))) | |
432 | (decf i)) | |
7d85a64e SM |
433 | i)) |
434 | ||
435 | (defun smerge-keep-current () | |
436 | "Use the current (under the cursor) version." | |
437 | (interactive) | |
438 | (smerge-match-conflict) | |
439 | (let ((i (smerge-get-current))) | |
3dac25a9 | 440 | (if (<= i 0) (error "Not inside a version") |
5bd8d87b | 441 | (smerge-keep-n i) |
f97e9a8a | 442 | (smerge-auto-leave)))) |
3dac25a9 | 443 | |
11ece56b MY |
444 | (defun smerge-kill-current () |
445 | "Remove the current (under the cursor) version." | |
446 | (interactive) | |
447 | (smerge-match-conflict) | |
448 | (let ((i (smerge-get-current))) | |
449 | (if (<= i 0) (error "Not inside a version") | |
5bd8d87b SM |
450 | (let ((left nil)) |
451 | (dolist (n '(3 2 1)) | |
452 | (if (and (match-end n) (/= (match-end n) (match-end i))) | |
453 | (push n left))) | |
454 | (if (and (cdr left) | |
455 | (/= (match-end (car left)) (match-end (cadr left)))) | |
456 | (ding) ;We don't know how to do that. | |
457 | (smerge-keep-n (car left)) | |
458 | (smerge-auto-leave)))))) | |
11ece56b | 459 | |
3dac25a9 SM |
460 | (defun smerge-diff-base-mine () |
461 | "Diff 'base' and 'mine' version in current conflict region." | |
462 | (interactive) | |
463 | (smerge-diff 2 1)) | |
464 | ||
465 | (defun smerge-diff-base-other () | |
466 | "Diff 'base' and 'other' version in current conflict region." | |
467 | (interactive) | |
468 | (smerge-diff 2 3)) | |
469 | ||
470 | (defun smerge-diff-mine-other () | |
471 | "Diff 'mine' and 'other' version in current conflict region." | |
472 | (interactive) | |
473 | (smerge-diff 1 3)) | |
474 | ||
475 | (defun smerge-match-conflict () | |
476 | "Get info about the conflict. Puts the info in the `match-data'. | |
477 | The submatches contain: | |
478 | 0: the whole conflict. | |
479 | 1: your code. | |
480 | 2: the base code. | |
481 | 3: other code. | |
482 | An error is raised if not inside a conflict." | |
483 | (save-excursion | |
484 | (condition-case nil | |
485 | (let* ((orig-point (point)) | |
486 | ||
487 | (_ (forward-line 1)) | |
488 | (_ (re-search-backward smerge-begin-re)) | |
489 | ||
490 | (start (match-beginning 0)) | |
491 | (mine-start (match-end 0)) | |
a48402c9 | 492 | (filename (or (match-string 1) "")) |
3dac25a9 SM |
493 | |
494 | (_ (re-search-forward smerge-end-re)) | |
495 | (_ (assert (< orig-point (match-end 0)))) | |
f1180544 | 496 | |
3dac25a9 SM |
497 | (other-end (match-beginning 0)) |
498 | (end (match-end 0)) | |
499 | ||
500 | (_ (re-search-backward smerge-other-re start)) | |
501 | ||
502 | (mine-end (match-beginning 0)) | |
503 | (other-start (match-end 0)) | |
504 | ||
505 | base-start base-end) | |
506 | ||
507 | ;; handle the various conflict styles | |
508 | (cond | |
9f0c286d SM |
509 | ((save-excursion |
510 | (goto-char mine-start) | |
2a3d70d4 | 511 | (re-search-forward smerge-begin-re end t)) |
9f0c286d SM |
512 | ;; There's a nested conflict and we're after the the beginning |
513 | ;; of the outer one but before the beginning of the inner one. | |
48d59eda SM |
514 | ;; Of course, maybe this is not a nested conflict but in that |
515 | ;; case it can only be something nastier that we don't know how | |
516 | ;; to handle, so may as well arbitrarily decide to treat it as | |
517 | ;; a nested conflict. --Stef | |
9f0c286d SM |
518 | (error "There is a nested conflict")) |
519 | ||
3dac25a9 SM |
520 | ((re-search-backward smerge-base-re start t) |
521 | ;; a 3-parts conflict | |
522 | (set (make-local-variable 'smerge-conflict-style) 'diff3-A) | |
523 | (setq base-end mine-end) | |
524 | (setq mine-end (match-beginning 0)) | |
525 | (setq base-start (match-end 0))) | |
526 | ||
11ece56b MY |
527 | ((string= filename (file-name-nondirectory |
528 | (or buffer-file-name ""))) | |
529 | ;; a 2-parts conflict | |
530 | (set (make-local-variable 'smerge-conflict-style) 'diff3-E)) | |
531 | ||
532 | ((and (not base-start) | |
533 | (or (eq smerge-conflict-style 'diff3-A) | |
534 | (equal filename "ANCESTOR") | |
535 | (string-match "\\`[.0-9]+\\'" filename))) | |
536 | ;; a same-diff conflict | |
537 | (setq base-start mine-start) | |
538 | (setq base-end mine-end) | |
539 | (setq mine-start other-start) | |
540 | (setq mine-end other-end))) | |
541 | ||
3dac25a9 SM |
542 | (store-match-data (list start end |
543 | mine-start mine-end | |
544 | base-start base-end | |
545 | other-start other-end | |
546 | (when base-start (1- base-start)) base-start | |
547 | (1- other-start) other-start)) | |
548 | t) | |
e29f823e | 549 | (search-failed (error "Point not in conflict region"))))) |
3dac25a9 | 550 | |
48d59eda SM |
551 | (defun smerge-conflict-overlay (pos) |
552 | "Return the conflict overlay at POS if any." | |
553 | (let ((ols (overlays-at pos)) | |
554 | conflict) | |
555 | (dolist (ol ols) | |
556 | (if (and (eq (overlay-get ol 'smerge) 'conflict) | |
557 | (> (overlay-end ol) pos)) | |
558 | (setq conflict ol))) | |
559 | conflict)) | |
560 | ||
3dac25a9 SM |
561 | (defun smerge-find-conflict (&optional limit) |
562 | "Find and match a conflict region. Intended as a font-lock MATCHER. | |
563 | The submatches are the same as in `smerge-match-conflict'. | |
48d59eda SM |
564 | Returns non-nil if a match is found between point and LIMIT. |
565 | Point is moved to the end of the conflict." | |
566 | (let ((found nil) | |
567 | (pos (point)) | |
568 | conflict) | |
569 | ;; First check to see if point is already inside a conflict, using | |
570 | ;; the conflict overlays. | |
571 | (while (and (not found) (setq conflict (smerge-conflict-overlay pos))) | |
572 | ;; Check the overlay's validity and kill it if it's out of date. | |
573 | (condition-case nil | |
574 | (progn | |
575 | (goto-char (overlay-start conflict)) | |
576 | (smerge-match-conflict) | |
577 | (goto-char (match-end 0)) | |
578 | (if (<= (point) pos) | |
579 | (error "Matching backward!") | |
580 | (setq found t))) | |
581 | (error (smerge-remove-props | |
582 | (overlay-start conflict) (overlay-end conflict)) | |
583 | (goto-char pos)))) | |
584 | ;; If we're not already inside a conflict, look for the next conflict | |
585 | ;; and add/update its overlay. | |
586 | (while (and (not found) (re-search-forward smerge-begin-re limit t)) | |
587 | (condition-case nil | |
588 | (progn | |
589 | (smerge-match-conflict) | |
590 | (goto-char (match-end 0)) | |
591 | (let ((conflict (smerge-conflict-overlay (1- (point))))) | |
592 | (if conflict | |
593 | ;; Update its location, just in case it got messed up. | |
594 | (move-overlay conflict (match-beginning 0) (match-end 0)) | |
595 | (setq conflict (make-overlay (match-beginning 0) (match-end 0) | |
596 | nil 'front-advance nil)) | |
597 | (overlay-put conflict 'evaporate t) | |
598 | (overlay-put conflict 'smerge 'conflict) | |
599 | (let ((props smerge-text-properties)) | |
600 | (while props | |
601 | (overlay-put conflict (pop props) (pop props)))))) | |
602 | (setq found t)) | |
603 | (error nil))) | |
604 | found)) | |
3dac25a9 | 605 | |
41796d09 SM |
606 | (defun smerge-refine-chopup-region (beg end file) |
607 | "Chopup the region into small elements, one per line." | |
608 | ;; ediff chops up into words, where the definition of a word is | |
609 | ;; customizable. Instead we here keep only one char per line. | |
610 | ;; The advantages are that there's nothing to configure, that we get very | |
611 | ;; fine results, and that it's trivial to map the line numbers in the | |
612 | ;; output of diff back into buffer positions. The disadvantage is that it | |
613 | ;; can take more time to compute the diff and that the result is sometimes | |
614 | ;; too fine. I'm not too concerned about the slowdown because conflicts | |
615 | ;; are usually significantly smaller than the whole file. As for the | |
616 | ;; problem of too-fine-refinement, I have found it to be unimportant | |
617 | ;; especially when you consider the cases where the fine-grain is just | |
618 | ;; what you want. | |
619 | (let ((buf (current-buffer))) | |
620 | (with-temp-buffer | |
621 | (insert-buffer-substring buf beg end) | |
622 | (goto-char (point-min)) | |
623 | (while (not (eobp)) | |
624 | (forward-char 1) | |
625 | (unless (eq (char-before) ?\n) (insert ?\n))) | |
626 | (let ((coding-system-for-write 'emacs-mule)) | |
627 | (write-region (point-min) (point-max) file nil 'nomessage))))) | |
628 | ||
629 | (defun smerge-refine-highlight-change (buf beg match-num1 match-num2) | |
630 | (let* ((startline (string-to-number (match-string match-num1))) | |
631 | (ol (make-overlay | |
632 | (+ beg startline -1) | |
633 | (+ beg (if (match-end match-num2) | |
634 | (string-to-number (match-string match-num2)) | |
635 | startline)) | |
636 | buf | |
637 | 'front-advance nil))) | |
638 | (overlay-put ol 'smerge 'refine) | |
639 | (overlay-put ol 'evaporate t) | |
640 | (overlay-put ol 'face 'smerge-refined-change))) | |
641 | ||
642 | ||
643 | (defun smerge-refine () | |
644 | "Highlight the parts of the conflict that are different." | |
645 | (interactive) | |
646 | ;; FIXME: make it work with 3-way conflicts. | |
647 | (smerge-match-conflict) | |
648 | (remove-overlays (match-beginning 0) (match-end 0) 'smerge 'refine) | |
649 | (smerge-ensure-match 1) | |
650 | (smerge-ensure-match 3) | |
651 | (let ((buf (current-buffer)) | |
652 | ;; Read them before the match-data gets clobbered. | |
653 | (beg1 (match-beginning 1)) (end1 (match-end 1)) | |
654 | (beg2 (match-beginning 3)) (end2 (match-end 3)) | |
655 | (file1 (make-temp-file "smerge1")) | |
656 | (file2 (make-temp-file "smerge2"))) | |
657 | ||
658 | ;; Chop up regions into smaller elements and save into files. | |
659 | (smerge-refine-chopup-region beg1 end1 file1) | |
660 | (smerge-refine-chopup-region beg2 end2 file2) | |
661 | ||
662 | ;; Call diff on those files. | |
663 | (unwind-protect | |
664 | (with-temp-buffer | |
665 | (let ((coding-system-for-read 'emacs-mule)) | |
666 | (call-process diff-command nil t nil file1 file2)) | |
667 | ;; Process diff's output. | |
668 | (goto-char (point-min)) | |
669 | (while (not (eobp)) | |
670 | (if (not (looking-at "\\([0-9]+\\)\\(?:,\\([0-9]+\\)\\)?\\([acd]\\)\\([0-9]+\\)\\(?:,\\([0-9]+\\)\\)?$")) | |
671 | (error "Unexpected patch hunk header: %s" | |
672 | (buffer-substring (point) (line-end-position))) | |
673 | (let ((op (char-after (match-beginning 3)))) | |
674 | (when (memq op '(?d ?c)) | |
675 | (smerge-refine-highlight-change buf beg1 1 2)) | |
676 | (when (memq op '(?a ?c)) | |
677 | (smerge-refine-highlight-change buf beg2 4 5))) | |
678 | (forward-line 1) ;Skip hunk header. | |
679 | (and (re-search-forward "^[0-9]" nil 'move) ;Skip hunk body. | |
680 | (goto-char (match-beginning 0)))))) | |
681 | (delete-file file1) | |
682 | (delete-file file2)))) | |
683 | ||
3dac25a9 SM |
684 | (defun smerge-diff (n1 n2) |
685 | (smerge-match-conflict) | |
686 | (smerge-ensure-match n1) | |
687 | (smerge-ensure-match n2) | |
688 | (let ((name1 (aref smerge-match-names n1)) | |
689 | (name2 (aref smerge-match-names n2)) | |
e29f823e SM |
690 | ;; Read them before the match-data gets clobbered. |
691 | (beg1 (match-beginning n1)) | |
692 | (end1 (match-end n1)) | |
693 | (beg2 (match-beginning n2)) | |
694 | (end2 (match-end n2)) | |
3dac25a9 | 695 | (file1 (make-temp-file "smerge1")) |
d73aed13 SM |
696 | (file2 (make-temp-file "smerge2")) |
697 | (dir default-directory) | |
48d59eda SM |
698 | (file (if buffer-file-name (file-relative-name buffer-file-name))) |
699 | ;; We would want to use `emacs-mule-unix' for read&write, but we | |
700 | ;; bump into problems with the coding-system used by diff to write | |
701 | ;; the file names and the time stamps in the header. | |
702 | ;; `buffer-file-coding-system' is not always correct either, but if | |
703 | ;; the OS/user uses only one coding-system, then it works. | |
e29f823e | 704 | (coding-system-for-read buffer-file-coding-system)) |
814838df SM |
705 | (write-region beg1 end1 file1 nil 'nomessage) |
706 | (write-region beg2 end2 file2 nil 'nomessage) | |
3dac25a9 SM |
707 | (unwind-protect |
708 | (with-current-buffer (get-buffer-create smerge-diff-buffer-name) | |
d73aed13 | 709 | (setq default-directory dir) |
3dac25a9 SM |
710 | (let ((inhibit-read-only t)) |
711 | (erase-buffer) | |
814838df SM |
712 | (let ((status |
713 | (apply 'call-process diff-command nil t nil | |
714 | (append smerge-diff-switches | |
715 | (list "-L" (concat name1 "/" file) | |
716 | "-L" (concat name2 "/" file) | |
717 | file1 file2))))) | |
718 | (if (eq status 0) (insert "No differences found.\n")))) | |
3dac25a9 SM |
719 | (goto-char (point-min)) |
720 | (diff-mode) | |
721 | (display-buffer (current-buffer) t)) | |
722 | (delete-file file1) | |
723 | (delete-file file2)))) | |
724 | ||
814838df SM |
725 | ;; compiler pacifiers |
726 | (defvar smerge-ediff-windows) | |
727 | (defvar smerge-ediff-buf) | |
728 | (defvar ediff-buffer-A) | |
729 | (defvar ediff-buffer-B) | |
730 | (defvar ediff-buffer-C) | |
48d59eda SM |
731 | (defvar ediff-ancestor-buffer) |
732 | (defvar ediff-quit-hook) | |
3dac25a9 | 733 | |
a1038ca0 | 734 | ;;;###autoload |
15092da1 SM |
735 | (defun smerge-ediff (&optional name-mine name-other name-base) |
736 | "Invoke ediff to resolve the conflicts. | |
737 | NAME-MINE, NAME-OTHER, and NAME-BASE, if non-nil, are used for the | |
738 | buffer names." | |
3dac25a9 SM |
739 | (interactive) |
740 | (let* ((buf (current-buffer)) | |
741 | (mode major-mode) | |
742 | ;;(ediff-default-variant 'default-B) | |
743 | (config (current-window-configuration)) | |
744 | (filename (file-name-nondirectory buffer-file-name)) | |
15092da1 SM |
745 | (mine (generate-new-buffer |
746 | (or name-mine (concat "*" filename " MINE*")))) | |
747 | (other (generate-new-buffer | |
748 | (or name-other (concat "*" filename " OTHER*")))) | |
3dac25a9 SM |
749 | base) |
750 | (with-current-buffer mine | |
751 | (buffer-disable-undo) | |
752 | (insert-buffer-substring buf) | |
753 | (goto-char (point-min)) | |
754 | (while (smerge-find-conflict) | |
755 | (when (match-beginning 2) (setq base t)) | |
5bd8d87b | 756 | (smerge-keep-n 1)) |
3dac25a9 SM |
757 | (buffer-enable-undo) |
758 | (set-buffer-modified-p nil) | |
759 | (funcall mode)) | |
760 | ||
761 | (with-current-buffer other | |
762 | (buffer-disable-undo) | |
763 | (insert-buffer-substring buf) | |
764 | (goto-char (point-min)) | |
765 | (while (smerge-find-conflict) | |
5bd8d87b | 766 | (smerge-keep-n 3)) |
3dac25a9 SM |
767 | (buffer-enable-undo) |
768 | (set-buffer-modified-p nil) | |
769 | (funcall mode)) | |
f1180544 | 770 | |
3dac25a9 | 771 | (when base |
15092da1 SM |
772 | (setq base (generate-new-buffer |
773 | (or name-base (concat "*" filename " BASE*")))) | |
3dac25a9 SM |
774 | (with-current-buffer base |
775 | (buffer-disable-undo) | |
776 | (insert-buffer-substring buf) | |
777 | (goto-char (point-min)) | |
778 | (while (smerge-find-conflict) | |
5bd8d87b SM |
779 | (if (match-end 2) |
780 | (smerge-keep-n 2) | |
781 | (delete-region (match-beginning 0) (match-end 0)))) | |
3dac25a9 SM |
782 | (buffer-enable-undo) |
783 | (set-buffer-modified-p nil) | |
784 | (funcall mode))) | |
f1180544 | 785 | |
3dac25a9 SM |
786 | ;; the rest of the code is inspired from vc.el |
787 | ;; Fire up ediff. | |
788 | (set-buffer | |
789 | (if base | |
790 | (ediff-merge-buffers-with-ancestor mine other base) | |
791 | ;; nil 'ediff-merge-revisions-with-ancestor buffer-file-name) | |
792 | (ediff-merge-buffers mine other))) | |
793 | ;; nil 'ediff-merge-revisions buffer-file-name))) | |
f1180544 | 794 | |
3dac25a9 SM |
795 | ;; Ediff is now set up, and we are in the control buffer. |
796 | ;; Do a few further adjustments and take precautions for exit. | |
797 | (set (make-local-variable 'smerge-ediff-windows) config) | |
798 | (set (make-local-variable 'smerge-ediff-buf) buf) | |
799 | (set (make-local-variable 'ediff-quit-hook) | |
800 | (lambda () | |
801 | (let ((buffer-A ediff-buffer-A) | |
802 | (buffer-B ediff-buffer-B) | |
803 | (buffer-C ediff-buffer-C) | |
804 | (buffer-Ancestor ediff-ancestor-buffer) | |
805 | (buf smerge-ediff-buf) | |
806 | (windows smerge-ediff-windows)) | |
807 | (ediff-cleanup-mess) | |
808 | (with-current-buffer buf | |
809 | (erase-buffer) | |
a34ed813 | 810 | (insert-buffer-substring buffer-C) |
3dac25a9 SM |
811 | (kill-buffer buffer-A) |
812 | (kill-buffer buffer-B) | |
813 | (kill-buffer buffer-C) | |
814 | (when (bufferp buffer-Ancestor) (kill-buffer buffer-Ancestor)) | |
815 | (set-window-configuration windows) | |
816 | (message "Conflict resolution finished; you may save the buffer"))))) | |
817 | (message "Please resolve conflicts now; exit ediff when done"))) | |
818 | ||
819 | ||
820 | ;;;###autoload | |
821 | (define-minor-mode smerge-mode | |
822 | "Minor mode to simplify editing output from the diff3 program. | |
823 | \\{smerge-mode-map}" | |
c06dbb8f | 824 | :group 'smerge :lighter " SMerge" |
0304b9c7 | 825 | (when (and (boundp 'font-lock-mode) font-lock-mode) |
3dac25a9 SM |
826 | (save-excursion |
827 | (if smerge-mode | |
828 | (font-lock-add-keywords nil smerge-font-lock-keywords 'append) | |
829 | (font-lock-remove-keywords nil smerge-font-lock-keywords)) | |
830 | (goto-char (point-min)) | |
831 | (while (smerge-find-conflict) | |
6eabfb26 | 832 | (save-excursion |
48d59eda SM |
833 | (font-lock-fontify-region (match-beginning 0) (match-end 0) nil))))) |
834 | (unless smerge-mode | |
835 | (smerge-remove-props (point-min) (point-max)))) | |
3dac25a9 SM |
836 | |
837 | ||
838 | (provide 'smerge-mode) | |
ab5796a9 | 839 | |
9f0c286d | 840 | ;; arch-tag: 605c8d1e-e43d-4943-a6f3-1bcc4333e690 |
3dac25a9 | 841 | ;;; smerge-mode.el ends here |