Commit | Line | Data |
---|---|---|
3dac25a9 SM |
1 | ;;; smerge-mode.el --- Minor mode to resolve diff3 conflicts |
2 | ||
0d30b337 | 3 | ;; Copyright (C) 1999, 2000, 2001, 2002, 2003, |
409cc4a3 | 4 | ;; 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc. |
3dac25a9 | 5 | |
cc1eecfd | 6 | ;; Author: Stefan Monnier <monnier@iro.umontreal.ca> |
9700a45f | 7 | ;; Keywords: tools 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 | |
b4aa6026 | 13 | ;; the Free Software Foundation; either version 3, or (at your option) |
3dac25a9 SM |
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 | ||
2daf4bc6 SM |
82 | (defcustom smerge-auto-refine t |
83 | "Automatically highlight changes in detail as the user visits conflicts." | |
97546017 | 84 | :group 'smerge |
2daf4bc6 SM |
85 | :type 'boolean) |
86 | ||
e8bfdf82 | 87 | (defface smerge-mine |
ea81d57e DN |
88 | '((((min-colors 88) (background light)) |
89 | (:foreground "blue1")) | |
90 | (((background light)) | |
b25f5aec | 91 | (:foreground "blue")) |
ea81d57e DN |
92 | (((min-colors 88) (background dark)) |
93 | (:foreground "cyan1")) | |
b25f5aec MB |
94 | (((background dark)) |
95 | (:foreground "cyan"))) | |
3dac25a9 SM |
96 | "Face for your code." |
97 | :group 'smerge) | |
e8bfdf82 MB |
98 | ;; backward-compatibility alias |
99 | (put 'smerge-mine-face 'face-alias 'smerge-mine) | |
100 | (defvar smerge-mine-face 'smerge-mine) | |
3dac25a9 | 101 | |
e8bfdf82 | 102 | (defface smerge-other |
b25f5aec MB |
103 | '((((background light)) |
104 | (:foreground "darkgreen")) | |
105 | (((background dark)) | |
106 | (:foreground "lightgreen"))) | |
3dac25a9 SM |
107 | "Face for the other code." |
108 | :group 'smerge) | |
e8bfdf82 MB |
109 | ;; backward-compatibility alias |
110 | (put 'smerge-other-face 'face-alias 'smerge-other) | |
111 | (defvar smerge-other-face 'smerge-other) | |
3dac25a9 | 112 | |
e8bfdf82 | 113 | (defface smerge-base |
ea81d57e DN |
114 | '((((min-colors 88) (background light)) |
115 | (:foreground "red1")) | |
116 | (((background light)) | |
b25f5aec MB |
117 | (:foreground "red")) |
118 | (((background dark)) | |
119 | (:foreground "orange"))) | |
3dac25a9 SM |
120 | "Face for the base code." |
121 | :group 'smerge) | |
e8bfdf82 MB |
122 | ;; backward-compatibility alias |
123 | (put 'smerge-base-face 'face-alias 'smerge-base) | |
124 | (defvar smerge-base-face 'smerge-base) | |
3dac25a9 | 125 | |
e8bfdf82 | 126 | (defface smerge-markers |
b25f5aec MB |
127 | '((((background light)) |
128 | (:background "grey85")) | |
129 | (((background dark)) | |
130 | (:background "grey30"))) | |
3dac25a9 SM |
131 | "Face for the conflict markers." |
132 | :group 'smerge) | |
e8bfdf82 MB |
133 | ;; backward-compatibility alias |
134 | (put 'smerge-markers-face 'face-alias 'smerge-markers) | |
135 | (defvar smerge-markers-face 'smerge-markers) | |
3dac25a9 | 136 | |
41796d09 SM |
137 | (defface smerge-refined-change |
138 | '((t :background "yellow")) | |
a6022f15 JB |
139 | "Face used for char-based changes shown by `smerge-refine'." |
140 | :group 'smerge) | |
41796d09 | 141 | |
f97e9a8a | 142 | (easy-mmode-defmap smerge-basic-map |
0e86b6b0 | 143 | `(("n" . smerge-next) |
3dac25a9 | 144 | ("p" . smerge-prev) |
a48402c9 | 145 | ("r" . smerge-resolve) |
3dac25a9 SM |
146 | ("a" . smerge-keep-all) |
147 | ("b" . smerge-keep-base) | |
148 | ("o" . smerge-keep-other) | |
149 | ("m" . smerge-keep-mine) | |
150 | ("E" . smerge-ediff) | |
48d59eda | 151 | ("C" . smerge-combine-with-next) |
41796d09 | 152 | ("R" . smerge-refine) |
3dac25a9 | 153 | ("\C-m" . smerge-keep-current) |
0e86b6b0 SM |
154 | ("=" . ,(make-sparse-keymap "Diff")) |
155 | ("=<" "base-mine" . smerge-diff-base-mine) | |
156 | ("=>" "base-other" . smerge-diff-base-other) | |
157 | ("==" "mine-other" . smerge-diff-mine-other)) | |
3dac25a9 | 158 | "The base keymap for `smerge-mode'.") |
3dac25a9 | 159 | |
0e86b6b0 | 160 | (defcustom smerge-command-prefix "\C-c^" |
3dac25a9 SM |
161 | "Prefix for `smerge-mode' commands." |
162 | :group 'smerge | |
0e86b6b0 | 163 | :type '(choice (string "\e") (string "\C-c^") (string "") string)) |
3dac25a9 | 164 | |
f97e9a8a SM |
165 | (easy-mmode-defmap smerge-mode-map |
166 | `((,smerge-command-prefix . ,smerge-basic-map)) | |
3dac25a9 SM |
167 | "Keymap for `smerge-mode'.") |
168 | ||
7d85a64e SM |
169 | (defvar smerge-check-cache nil) |
170 | (make-variable-buffer-local 'smerge-check-cache) | |
171 | (defun smerge-check (n) | |
172 | (condition-case nil | |
173 | (let ((state (cons (point) (buffer-modified-tick)))) | |
174 | (unless (equal (cdr smerge-check-cache) state) | |
175 | (smerge-match-conflict) | |
176 | (setq smerge-check-cache (cons (match-data) state))) | |
177 | (nth (* 2 n) (car smerge-check-cache))) | |
178 | (error nil))) | |
179 | ||
3dac25a9 SM |
180 | (easy-menu-define smerge-mode-menu smerge-mode-map |
181 | "Menu for `smerge-mode'." | |
182 | '("SMerge" | |
43e764c9 | 183 | ["Next" smerge-next :help "Go to next conflict"] |
394bd1ca | 184 | ["Previous" smerge-prev :help "Go to previous conflict"] |
7d85a64e SM |
185 | "--" |
186 | ["Keep All" smerge-keep-all :help "Keep all three versions" | |
187 | :active (smerge-check 1)] | |
188 | ["Keep Current" smerge-keep-current :help "Use current (at point) version" | |
189 | :active (and (smerge-check 1) (> (smerge-get-current) 0))] | |
190 | "--" | |
191 | ["Revert to Base" smerge-keep-base :help "Revert to base version" | |
192 | :active (smerge-check 2)] | |
193 | ["Keep Other" smerge-keep-other :help "Keep `other' version" | |
194 | :active (smerge-check 3)] | |
195 | ["Keep Yours" smerge-keep-mine :help "Keep your version" | |
196 | :active (smerge-check 1)] | |
43e764c9 DL |
197 | "--" |
198 | ["Diff Base/Mine" smerge-diff-base-mine | |
7d85a64e SM |
199 | :help "Diff `base' and `mine' for current conflict" |
200 | :active (smerge-check 2)] | |
43e764c9 | 201 | ["Diff Base/Other" smerge-diff-base-other |
7d85a64e SM |
202 | :help "Diff `base' and `other' for current conflict" |
203 | :active (smerge-check 2)] | |
43e764c9 | 204 | ["Diff Mine/Other" smerge-diff-mine-other |
7d85a64e SM |
205 | :help "Diff `mine' and `other' for current conflict" |
206 | :active (smerge-check 1)] | |
43e764c9 DL |
207 | "--" |
208 | ["Invoke Ediff" smerge-ediff | |
7d85a64e SM |
209 | :help "Use Ediff to resolve the conflicts" |
210 | :active (smerge-check 1)] | |
211 | ["Auto Resolve" smerge-resolve | |
5bd8d87b SM |
212 | :help "Try auto-resolution heuristics" |
213 | :active (smerge-check 1)] | |
7d85a64e SM |
214 | ["Combine" smerge-combine-with-next |
215 | :help "Combine current conflict with next" | |
216 | :active (smerge-check 1)] | |
3dac25a9 SM |
217 | )) |
218 | ||
11ece56b MY |
219 | (easy-menu-define smerge-context-menu nil |
220 | "Context menu for mine area in `smerge-mode'." | |
221 | '(nil | |
222 | ["Keep Current" smerge-keep-current :help "Use current (at point) version"] | |
223 | ["Kill Current" smerge-kill-current :help "Remove current (at point) version"] | |
224 | ["Keep All" smerge-keep-all :help "Keep all three versions"] | |
225 | "---" | |
226 | ["More..." (popup-menu smerge-mode-menu) :help "Show full SMerge mode menu"] | |
227 | )) | |
228 | ||
3dac25a9 SM |
229 | (defconst smerge-font-lock-keywords |
230 | '((smerge-find-conflict | |
f0c1adab | 231 | (1 smerge-mine-face prepend t) |
3dac25a9 SM |
232 | (2 smerge-base-face prepend t) |
233 | (3 smerge-other-face prepend t) | |
0e86b6b0 | 234 | ;; FIXME: `keep' doesn't work right with syntactic fontification. |
3dac25a9 SM |
235 | (0 smerge-markers-face keep) |
236 | (4 nil t t) | |
237 | (5 nil t t))) | |
238 | "Font lock patterns for `smerge-mode'.") | |
239 | ||
240 | (defconst smerge-begin-re "^<<<<<<< \\(.*\\)\n") | |
241 | (defconst smerge-end-re "^>>>>>>> .*\n") | |
242 | (defconst smerge-base-re "^||||||| .*\n") | |
243 | (defconst smerge-other-re "^=======\n") | |
244 | ||
245 | (defvar smerge-conflict-style nil | |
246 | "Keep track of which style of conflict is in use. | |
247 | Can be nil if the style is undecided, or else: | |
248 | - `diff3-E' | |
249 | - `diff3-A'") | |
250 | ||
251 | ;; Compiler pacifiers | |
8f6cea29 DL |
252 | (defvar font-lock-mode) |
253 | (defvar font-lock-keywords) | |
3dac25a9 SM |
254 | |
255 | ;;;; | |
256 | ;;;; Actual code | |
257 | ;;;; | |
258 | ||
f97e9a8a | 259 | ;; Define smerge-next and smerge-prev |
2daf4bc6 SM |
260 | (easy-mmode-define-navigation smerge smerge-begin-re "conflict" nil nil |
261 | (if smerge-auto-refine | |
262 | (condition-case nil (smerge-refine) (error nil)))) | |
3dac25a9 SM |
263 | |
264 | (defconst smerge-match-names ["conflict" "mine" "base" "other"]) | |
265 | ||
266 | (defun smerge-ensure-match (n) | |
267 | (unless (match-end n) | |
376166e6 | 268 | (error "No `%s'" (aref smerge-match-names n)))) |
3dac25a9 | 269 | |
f97e9a8a SM |
270 | (defun smerge-auto-leave () |
271 | (when (and smerge-auto-leave | |
272 | (save-excursion (goto-char (point-min)) | |
273 | (not (re-search-forward smerge-begin-re nil t)))) | |
48d59eda SM |
274 | (when (and (listp buffer-undo-list) smerge-mode) |
275 | (push (list 'apply 'smerge-mode 1) buffer-undo-list)) | |
f97e9a8a | 276 | (smerge-mode -1))) |
f1180544 | 277 | |
f97e9a8a | 278 | |
3dac25a9 | 279 | (defun smerge-keep-all () |
5bd8d87b | 280 | "Concatenate all versions." |
3dac25a9 SM |
281 | (interactive) |
282 | (smerge-match-conflict) | |
5bd8d87b SM |
283 | (let ((mb2 (or (match-beginning 2) (point-max))) |
284 | (me2 (or (match-end 2) (point-min)))) | |
285 | (delete-region (match-end 3) (match-end 0)) | |
286 | (delete-region (max me2 (match-end 1)) (match-beginning 3)) | |
287 | (if (and (match-end 2) (/= (match-end 1) (match-end 3))) | |
288 | (delete-region (match-end 1) (match-beginning 2))) | |
289 | (delete-region (match-beginning 0) (min (match-beginning 1) mb2)) | |
290 | (smerge-auto-leave))) | |
291 | ||
292 | (defun smerge-keep-n (n) | |
41796d09 | 293 | (smerge-remove-props (match-beginning 0) (match-end 0)) |
5bd8d87b SM |
294 | ;; We used to use replace-match, but that did not preserve markers so well. |
295 | (delete-region (match-end n) (match-end 0)) | |
296 | (delete-region (match-beginning 0) (match-beginning n))) | |
3dac25a9 | 297 | |
814838df SM |
298 | (defun smerge-combine-with-next () |
299 | "Combine the current conflict with the next one." | |
02dfeba8 SM |
300 | ;; `smerge-auto-combine' relies on the finish position (at the beginning |
301 | ;; of the closing marker). | |
814838df SM |
302 | (interactive) |
303 | (smerge-match-conflict) | |
304 | (let ((ends nil)) | |
305 | (dolist (i '(3 2 1 0)) | |
306 | (push (if (match-end i) (copy-marker (match-end i) t)) ends)) | |
307 | (setq ends (apply 'vector ends)) | |
308 | (goto-char (aref ends 0)) | |
309 | (if (not (re-search-forward smerge-begin-re nil t)) | |
310 | (error "No next conflict") | |
311 | (smerge-match-conflict) | |
312 | (let ((match-data (mapcar (lambda (m) (if m (copy-marker m))) | |
313 | (match-data)))) | |
314 | ;; First copy the in-between text in each alternative. | |
315 | (dolist (i '(1 2 3)) | |
316 | (when (aref ends i) | |
317 | (goto-char (aref ends i)) | |
318 | (insert-buffer-substring (current-buffer) | |
319 | (aref ends 0) (car match-data)))) | |
320 | (delete-region (aref ends 0) (car match-data)) | |
321 | ;; Then move the second conflict's alternatives into the first. | |
322 | (dolist (i '(1 2 3)) | |
323 | (set-match-data match-data) | |
324 | (when (and (aref ends i) (match-end i)) | |
325 | (goto-char (aref ends i)) | |
326 | (insert-buffer-substring (current-buffer) | |
327 | (match-beginning i) (match-end i)))) | |
328 | (delete-region (car match-data) (cadr match-data)) | |
329 | ;; Free the markers. | |
330 | (dolist (m match-data) (if m (move-marker m nil))) | |
331 | (mapc (lambda (m) (if m (move-marker m nil))) ends))))) | |
332 | ||
02dfeba8 SM |
333 | (defvar smerge-auto-combine-max-separation 2 |
334 | "Max number of lines between conflicts that should be combined.") | |
335 | ||
336 | (defun smerge-auto-combine () | |
337 | "Automatically combine conflicts that are near each other." | |
338 | (interactive) | |
339 | (save-excursion | |
340 | (goto-char (point-min)) | |
341 | (while (smerge-find-conflict) | |
342 | ;; 2 is 1 (default) + 1 (the begin markers). | |
343 | (while (save-excursion | |
344 | (smerge-find-conflict | |
345 | (line-beginning-position | |
346 | (+ 2 smerge-auto-combine-max-separation)))) | |
347 | (forward-line -1) ;Go back inside the conflict. | |
348 | (smerge-combine-with-next) | |
349 | (forward-line 1) ;Move past the end of the conflict. | |
350 | )))) | |
351 | ||
a48402c9 SM |
352 | (defvar smerge-resolve-function |
353 | (lambda () (error "Don't know how to resolve")) | |
354 | "Mode-specific merge function. | |
de689511 SM |
355 | The function is called with zero or one argument (non-nil if the resolution |
356 | function should only apply safe heuristics) and with the match data set | |
a48402c9 | 357 | according to `smerge-match-conflict'.") |
48d59eda | 358 | (add-to-list 'debug-ignored-errors "Don't know how to resolve") |
a48402c9 | 359 | |
11ece56b MY |
360 | (defvar smerge-text-properties |
361 | `(help-echo "merge conflict: mouse-3 shows a menu" | |
362 | ;; mouse-face highlight | |
363 | keymap (keymap (down-mouse-3 . smerge-popup-context-menu)))) | |
364 | ||
41796d09 SM |
365 | (defun smerge-remove-props (beg end) |
366 | (remove-overlays beg end 'smerge 'refine) | |
91773964 SM |
367 | (remove-overlays beg end 'smerge 'conflict) |
368 | ;; Now that we use overlays rather than text-properties, this function | |
369 | ;; does not cause refontification any more. It can be seen very clearly | |
370 | ;; in buffers where jit-lock-contextually is not t, in which case deleting | |
371 | ;; the "<<<<<<< foobar" leading line leaves the rest of the conflict | |
372 | ;; highlighted as if it were still a valid conflict. Note that in many | |
373 | ;; important cases (such as the previous example) we're actually called | |
374 | ;; during font-locking so inhibit-modification-hooks is non-nil, so we | |
375 | ;; can't just modify the buffer and expect font-lock to be triggered as in: | |
376 | ;; (put-text-property beg end 'smerge-force-highlighting nil) | |
0778a62f SM |
377 | (let ((modified (buffer-modified-p))) |
378 | (remove-text-properties beg end '(fontified nil)) | |
379 | (restore-buffer-modified-p modified))) | |
11ece56b MY |
380 | |
381 | (defun smerge-popup-context-menu (event) | |
382 | "Pop up the Smerge mode context menu under mouse." | |
383 | (interactive "e") | |
384 | (if (and smerge-mode | |
65114860 | 385 | (save-excursion (posn-set-point (event-end event)) (smerge-check 1))) |
11ece56b | 386 | (progn |
65114860 | 387 | (posn-set-point (event-end event)) |
5bd8d87b SM |
388 | (smerge-match-conflict) |
389 | (let ((i (smerge-get-current)) | |
390 | o) | |
391 | (if (<= i 0) | |
392 | ;; Out of range | |
393 | (popup-menu smerge-mode-menu) | |
394 | ;; Install overlay. | |
3b0af402 | 395 | (setq o (make-overlay (match-beginning i) (match-end i))) |
5bd8d87b SM |
396 | (unwind-protect |
397 | (progn | |
398 | (overlay-put o 'face 'highlight) | |
399 | (sit-for 0) ;Display the new highlighting. | |
400 | (popup-menu smerge-context-menu)) | |
401 | ;; Delete overlay. | |
402 | (delete-overlay o))))) | |
11ece56b MY |
403 | ;; There's no conflict at point, the text-props are just obsolete. |
404 | (save-excursion | |
405 | (let ((beg (re-search-backward smerge-end-re nil t)) | |
5bd8d87b SM |
406 | (end (re-search-forward smerge-begin-re nil t))) |
407 | (smerge-remove-props (or beg (point-min)) (or end (point-max))) | |
408 | (push event unread-command-events))))) | |
11ece56b | 409 | |
56d707f1 SM |
410 | (defun smerge-apply-resolution-patch (buf m0b m0e m3b m3e &optional m2b) |
411 | "Replace the conflict with a bunch of subconflicts. | |
412 | BUF contains a plain diff between match-1 and match-3." | |
413 | (let ((line 1) | |
414 | (textbuf (current-buffer)) | |
415 | (name1 (progn (goto-char m0b) | |
416 | (buffer-substring (+ (point) 8) (line-end-position)))) | |
417 | (name2 (when m2b (goto-char m2b) (forward-line -1) | |
418 | (buffer-substring (+ (point) 8) (line-end-position)))) | |
419 | (name3 (progn (goto-char m0e) (forward-line -1) | |
420 | (buffer-substring (+ (point) 8) (line-end-position))))) | |
421 | (smerge-remove-props m0b m0e) | |
422 | (delete-region m3e m0e) | |
423 | (delete-region m0b m3b) | |
424 | (setq m3b m0b) | |
425 | (setq m3e (- m3e (- m3b m0b))) | |
426 | (goto-char m3b) | |
427 | (with-current-buffer buf | |
428 | (goto-char (point-min)) | |
429 | (while (not (eobp)) | |
430 | (if (not (looking-at "\\([0-9]+\\)\\(?:,\\([0-9]+\\)\\)?\\([acd]\\)\\([0-9]+\\)\\(?:,\\([0-9]+\\)\\)?$")) | |
431 | (error "Unexpected patch hunk header: %s" | |
432 | (buffer-substring (point) (line-end-position))) | |
433 | (let* ((op (char-after (match-beginning 3))) | |
434 | (startline (+ (string-to-number (match-string 1)) | |
435 | ;; No clue why this is the way it is, but line | |
436 | ;; numbers seem to be off-by-one for `a' ops. | |
437 | (if (eq op ?a) 1 0))) | |
438 | (endline (if (eq op ?a) startline | |
439 | (1+ (if (match-end 2) | |
440 | (string-to-number (match-string 2)) | |
441 | startline)))) | |
442 | (lines (- endline startline)) | |
443 | (otherlines (cond | |
444 | ((eq op ?d) nil) | |
445 | ((null (match-end 5)) 1) | |
446 | (t (- (string-to-number (match-string 5)) | |
447 | (string-to-number (match-string 4)) -1)))) | |
448 | othertext) | |
449 | (forward-line 1) ;Skip header. | |
450 | (forward-line lines) ;Skip deleted text. | |
451 | (if (eq op ?c) (forward-line 1)) ;Skip separator. | |
452 | (setq othertext | |
453 | (if (null otherlines) "" | |
454 | (let ((pos (point))) | |
455 | (dotimes (i otherlines) (delete-char 2) (forward-line 1)) | |
456 | (buffer-substring pos (point))))) | |
457 | (with-current-buffer textbuf | |
458 | (forward-line (- startline line)) | |
459 | (insert "<<<<<<< " name1 "\n" othertext | |
0e05d8fc | 460 | (if name2 (concat "||||||| " name2 "\n")) |
56d707f1 SM |
461 | "=======\n") |
462 | (forward-line lines) | |
463 | (insert ">>>>>>> " name3 "\n") | |
464 | (setq line endline)))))))) | |
465 | ||
de689511 | 466 | (defun smerge-resolve (&optional safe) |
a48402c9 SM |
467 | "Resolve the conflict at point intelligently. |
468 | This relies on mode-specific knowledge and thus only works in | |
469 | some major modes. Uses `smerge-resolve-function' to do the actual work." | |
470 | (interactive) | |
471 | (smerge-match-conflict) | |
f57b45cf | 472 | (smerge-remove-props (match-beginning 0) (match-end 0)) |
56d707f1 SM |
473 | (let ((md (match-data)) |
474 | (m0b (match-beginning 0)) | |
475 | (m1b (match-beginning 1)) | |
476 | (m2b (match-beginning 2)) | |
477 | (m3b (match-beginning 3)) | |
478 | (m0e (match-end 0)) | |
479 | (m1e (match-end 1)) | |
480 | (m2e (match-end 2)) | |
481 | (m3e (match-end 3)) | |
482 | (buf (generate-new-buffer " *smerge*")) | |
483 | m b o) | |
484 | (unwind-protect | |
485 | (progn | |
486 | (cond | |
487 | ;; Trivial diff3 -A non-conflicts. | |
488 | ((and (eq (match-end 1) (match-end 3)) | |
489 | (eq (match-beginning 1) (match-beginning 3))) | |
490 | (smerge-keep-n 3)) | |
491 | ;; Mode-specific conflict resolution. | |
492 | ((condition-case nil | |
493 | (atomic-change-group | |
494 | (if safe | |
495 | (funcall smerge-resolve-function safe) | |
496 | (funcall smerge-resolve-function)) | |
497 | t) | |
498 | (error nil)) | |
499 | ;; Nothing to do: the resolution function has done it already. | |
500 | nil) | |
0e05d8fc SM |
501 | ;; Non-conflict. |
502 | ((and (eq m1e m3e) (eq m1b m3b)) | |
503 | (set-match-data md) (smerge-keep-n 3)) | |
56d707f1 SM |
504 | ;; Refine a 2-way conflict using "diff -b". |
505 | ;; In case of a 3-way conflict with an empty base | |
506 | ;; (i.e. 2 conflicting additions), we do the same, presuming | |
507 | ;; that the 2 additions should be somehow merged rather | |
508 | ;; than concatenated. | |
0e05d8fc SM |
509 | ((let ((lines (count-lines m3b m3e))) |
510 | (setq m (make-temp-file "smm")) | |
511 | (write-region m1b m1e m nil 'silent) | |
512 | (setq o (make-temp-file "smo")) | |
513 | (write-region m3b m3e o nil 'silent) | |
514 | (not (or (eq m1b m1e) (eq m3b m3e) | |
515 | (and (not (zerop (call-process diff-command | |
516 | nil buf nil "-b" o m))) | |
517 | ;; TODO: We don't know how to do the refinement | |
518 | ;; if there's a non-empty ancestor and m1 and m3 | |
519 | ;; aren't just plain equal. | |
520 | m2b (not (eq m2b m2e))) | |
56d707f1 SM |
521 | (with-current-buffer buf |
522 | (goto-char (point-min)) | |
523 | ;; Make sure there's some refinement. | |
524 | (looking-at | |
525 | (concat "1," (number-to-string lines) "c")))))) | |
526 | (smerge-apply-resolution-patch buf m0b m0e m3b m3e m2b)) | |
0e05d8fc SM |
527 | ;; "Mere whitespace changes" conflicts. |
528 | ((when m2e | |
529 | (setq b (make-temp-file "smb")) | |
530 | (write-region m2b m2e b nil 'silent) | |
531 | (with-current-buffer buf (erase-buffer)) | |
532 | ;; Only minor whitespace changes made locally. | |
533 | ;; BEWARE: pass "-c" 'cause the output is reused in the next test. | |
534 | (zerop (call-process diff-command nil buf nil "-bc" b m))) | |
535 | (set-match-data md) | |
536 | (smerge-keep-n 3)) | |
56d707f1 SM |
537 | ;; Try "diff -b BASE MINE | patch OTHER". |
538 | ((when (and (not safe) m2e b | |
539 | ;; If the BASE is empty, this would just concatenate | |
540 | ;; the two, which is rarely right. | |
541 | (not (eq m2b m2e))) | |
0e05d8fc | 542 | ;; BEWARE: we're using here the patch of the previous test. |
56d707f1 SM |
543 | (with-current-buffer buf |
544 | (zerop (call-process-region | |
545 | (point-min) (point-max) "patch" t nil nil | |
546 | "-r" "/dev/null" "--no-backup-if-mismatch" | |
547 | "-fl" o)))) | |
548 | (save-restriction | |
549 | (narrow-to-region m0b m0e) | |
550 | (smerge-remove-props m0b m0e) | |
551 | (insert-file-contents o nil nil nil t))) | |
552 | ;; Try "diff -b BASE OTHER | patch MINE". | |
553 | ((when (and (not safe) m2e b | |
554 | ;; If the BASE is empty, this would just concatenate | |
555 | ;; the two, which is rarely right. | |
556 | (not (eq m2b m2e))) | |
557 | (write-region m3b m3e o nil 'silent) | |
558 | (call-process diff-command nil buf nil "-bc" b o) | |
559 | (with-current-buffer buf | |
560 | (zerop (call-process-region | |
561 | (point-min) (point-max) "patch" t nil nil | |
562 | "-r" "/dev/null" "--no-backup-if-mismatch" | |
563 | "-fl" m)))) | |
564 | (save-restriction | |
565 | (narrow-to-region m0b m0e) | |
566 | (smerge-remove-props m0b m0e) | |
567 | (insert-file-contents m nil nil nil t))) | |
568 | (t | |
569 | (error "Don't know how to resolve")))) | |
570 | (if (buffer-name buf) (kill-buffer buf)) | |
571 | (if m (delete-file m)) | |
572 | (if b (delete-file b)) | |
573 | (if o (delete-file o)))) | |
a48402c9 SM |
574 | (smerge-auto-leave)) |
575 | ||
de689511 SM |
576 | (defun smerge-resolve-all () |
577 | "Perform automatic resolution on all conflicts." | |
578 | (interactive) | |
579 | (save-excursion | |
580 | (goto-char (point-min)) | |
581 | (while (re-search-forward smerge-begin-re nil t) | |
582 | (condition-case nil | |
583 | (progn | |
584 | (smerge-match-conflict) | |
585 | (smerge-resolve 'safe)) | |
586 | (error nil))))) | |
587 | ||
588 | (defun smerge-batch-resolve () | |
589 | ;; command-line-args-left is what is left of the command line. | |
590 | (if (not noninteractive) | |
591 | (error "`smerge-batch-resolve' is to be used only with -batch")) | |
592 | (while command-line-args-left | |
593 | (let ((file (pop command-line-args-left))) | |
2daf4bc6 SM |
594 | (if (string-match "\\.rej\\'" file) |
595 | ;; .rej files should never contain diff3 markers, on the other hand, | |
596 | ;; in Arch, .rej files are sometimes used to indicate that the | |
597 | ;; main file has diff3 markers. So you can pass **/*.rej and | |
598 | ;; it will DTRT. | |
599 | (setq file (substring file 0 (match-beginning 0)))) | |
de689511 SM |
600 | (message "Resolving conflicts in %s..." file) |
601 | (when (file-readable-p file) | |
602 | (with-current-buffer (find-file-noselect file) | |
603 | (smerge-resolve-all) | |
604 | (save-buffer) | |
605 | (kill-buffer (current-buffer))))))) | |
606 | ||
3dac25a9 SM |
607 | (defun smerge-keep-base () |
608 | "Revert to the base version." | |
609 | (interactive) | |
610 | (smerge-match-conflict) | |
611 | (smerge-ensure-match 2) | |
5bd8d87b | 612 | (smerge-keep-n 2) |
f97e9a8a | 613 | (smerge-auto-leave)) |
3dac25a9 SM |
614 | |
615 | (defun smerge-keep-other () | |
616 | "Use \"other\" version." | |
617 | (interactive) | |
618 | (smerge-match-conflict) | |
619 | ;;(smerge-ensure-match 3) | |
5bd8d87b | 620 | (smerge-keep-n 3) |
f97e9a8a | 621 | (smerge-auto-leave)) |
3dac25a9 SM |
622 | |
623 | (defun smerge-keep-mine () | |
624 | "Keep your version." | |
625 | (interactive) | |
626 | (smerge-match-conflict) | |
627 | ;;(smerge-ensure-match 1) | |
5bd8d87b | 628 | (smerge-keep-n 1) |
f97e9a8a | 629 | (smerge-auto-leave)) |
3dac25a9 | 630 | |
7d85a64e | 631 | (defun smerge-get-current () |
3dac25a9 SM |
632 | (let ((i 3)) |
633 | (while (or (not (match-end i)) | |
634 | (< (point) (match-beginning i)) | |
635 | (>= (point) (match-end i))) | |
636 | (decf i)) | |
7d85a64e SM |
637 | i)) |
638 | ||
639 | (defun smerge-keep-current () | |
640 | "Use the current (under the cursor) version." | |
641 | (interactive) | |
642 | (smerge-match-conflict) | |
643 | (let ((i (smerge-get-current))) | |
3dac25a9 | 644 | (if (<= i 0) (error "Not inside a version") |
5bd8d87b | 645 | (smerge-keep-n i) |
f97e9a8a | 646 | (smerge-auto-leave)))) |
3dac25a9 | 647 | |
11ece56b MY |
648 | (defun smerge-kill-current () |
649 | "Remove the current (under the cursor) version." | |
650 | (interactive) | |
651 | (smerge-match-conflict) | |
652 | (let ((i (smerge-get-current))) | |
653 | (if (<= i 0) (error "Not inside a version") | |
5bd8d87b SM |
654 | (let ((left nil)) |
655 | (dolist (n '(3 2 1)) | |
656 | (if (and (match-end n) (/= (match-end n) (match-end i))) | |
657 | (push n left))) | |
658 | (if (and (cdr left) | |
659 | (/= (match-end (car left)) (match-end (cadr left)))) | |
660 | (ding) ;We don't know how to do that. | |
661 | (smerge-keep-n (car left)) | |
662 | (smerge-auto-leave)))))) | |
11ece56b | 663 | |
3dac25a9 SM |
664 | (defun smerge-diff-base-mine () |
665 | "Diff 'base' and 'mine' version in current conflict region." | |
666 | (interactive) | |
667 | (smerge-diff 2 1)) | |
668 | ||
669 | (defun smerge-diff-base-other () | |
670 | "Diff 'base' and 'other' version in current conflict region." | |
671 | (interactive) | |
672 | (smerge-diff 2 3)) | |
673 | ||
674 | (defun smerge-diff-mine-other () | |
675 | "Diff 'mine' and 'other' version in current conflict region." | |
676 | (interactive) | |
677 | (smerge-diff 1 3)) | |
678 | ||
679 | (defun smerge-match-conflict () | |
680 | "Get info about the conflict. Puts the info in the `match-data'. | |
681 | The submatches contain: | |
682 | 0: the whole conflict. | |
683 | 1: your code. | |
684 | 2: the base code. | |
685 | 3: other code. | |
686 | An error is raised if not inside a conflict." | |
687 | (save-excursion | |
688 | (condition-case nil | |
689 | (let* ((orig-point (point)) | |
690 | ||
691 | (_ (forward-line 1)) | |
692 | (_ (re-search-backward smerge-begin-re)) | |
693 | ||
694 | (start (match-beginning 0)) | |
695 | (mine-start (match-end 0)) | |
a48402c9 | 696 | (filename (or (match-string 1) "")) |
3dac25a9 SM |
697 | |
698 | (_ (re-search-forward smerge-end-re)) | |
699 | (_ (assert (< orig-point (match-end 0)))) | |
f1180544 | 700 | |
3dac25a9 SM |
701 | (other-end (match-beginning 0)) |
702 | (end (match-end 0)) | |
703 | ||
704 | (_ (re-search-backward smerge-other-re start)) | |
705 | ||
706 | (mine-end (match-beginning 0)) | |
707 | (other-start (match-end 0)) | |
708 | ||
709 | base-start base-end) | |
710 | ||
711 | ;; handle the various conflict styles | |
712 | (cond | |
9f0c286d SM |
713 | ((save-excursion |
714 | (goto-char mine-start) | |
2a3d70d4 | 715 | (re-search-forward smerge-begin-re end t)) |
9f0c286d SM |
716 | ;; There's a nested conflict and we're after the the beginning |
717 | ;; of the outer one but before the beginning of the inner one. | |
48d59eda SM |
718 | ;; Of course, maybe this is not a nested conflict but in that |
719 | ;; case it can only be something nastier that we don't know how | |
720 | ;; to handle, so may as well arbitrarily decide to treat it as | |
721 | ;; a nested conflict. --Stef | |
9f0c286d SM |
722 | (error "There is a nested conflict")) |
723 | ||
3dac25a9 SM |
724 | ((re-search-backward smerge-base-re start t) |
725 | ;; a 3-parts conflict | |
726 | (set (make-local-variable 'smerge-conflict-style) 'diff3-A) | |
727 | (setq base-end mine-end) | |
728 | (setq mine-end (match-beginning 0)) | |
729 | (setq base-start (match-end 0))) | |
730 | ||
11ece56b MY |
731 | ((string= filename (file-name-nondirectory |
732 | (or buffer-file-name ""))) | |
733 | ;; a 2-parts conflict | |
734 | (set (make-local-variable 'smerge-conflict-style) 'diff3-E)) | |
735 | ||
736 | ((and (not base-start) | |
737 | (or (eq smerge-conflict-style 'diff3-A) | |
738 | (equal filename "ANCESTOR") | |
739 | (string-match "\\`[.0-9]+\\'" filename))) | |
740 | ;; a same-diff conflict | |
741 | (setq base-start mine-start) | |
742 | (setq base-end mine-end) | |
743 | (setq mine-start other-start) | |
744 | (setq mine-end other-end))) | |
745 | ||
3dac25a9 SM |
746 | (store-match-data (list start end |
747 | mine-start mine-end | |
748 | base-start base-end | |
749 | other-start other-end | |
750 | (when base-start (1- base-start)) base-start | |
751 | (1- other-start) other-start)) | |
752 | t) | |
e29f823e | 753 | (search-failed (error "Point not in conflict region"))))) |
3dac25a9 | 754 | |
0778a62f SM |
755 | (add-to-list 'debug-ignored-errors "Point not in conflict region") |
756 | ||
48d59eda SM |
757 | (defun smerge-conflict-overlay (pos) |
758 | "Return the conflict overlay at POS if any." | |
759 | (let ((ols (overlays-at pos)) | |
760 | conflict) | |
761 | (dolist (ol ols) | |
762 | (if (and (eq (overlay-get ol 'smerge) 'conflict) | |
763 | (> (overlay-end ol) pos)) | |
764 | (setq conflict ol))) | |
765 | conflict)) | |
766 | ||
3dac25a9 SM |
767 | (defun smerge-find-conflict (&optional limit) |
768 | "Find and match a conflict region. Intended as a font-lock MATCHER. | |
769 | The submatches are the same as in `smerge-match-conflict'. | |
48d59eda SM |
770 | Returns non-nil if a match is found between point and LIMIT. |
771 | Point is moved to the end of the conflict." | |
772 | (let ((found nil) | |
773 | (pos (point)) | |
774 | conflict) | |
775 | ;; First check to see if point is already inside a conflict, using | |
776 | ;; the conflict overlays. | |
777 | (while (and (not found) (setq conflict (smerge-conflict-overlay pos))) | |
778 | ;; Check the overlay's validity and kill it if it's out of date. | |
779 | (condition-case nil | |
780 | (progn | |
781 | (goto-char (overlay-start conflict)) | |
782 | (smerge-match-conflict) | |
783 | (goto-char (match-end 0)) | |
784 | (if (<= (point) pos) | |
785 | (error "Matching backward!") | |
786 | (setq found t))) | |
787 | (error (smerge-remove-props | |
788 | (overlay-start conflict) (overlay-end conflict)) | |
789 | (goto-char pos)))) | |
790 | ;; If we're not already inside a conflict, look for the next conflict | |
791 | ;; and add/update its overlay. | |
792 | (while (and (not found) (re-search-forward smerge-begin-re limit t)) | |
793 | (condition-case nil | |
794 | (progn | |
795 | (smerge-match-conflict) | |
796 | (goto-char (match-end 0)) | |
797 | (let ((conflict (smerge-conflict-overlay (1- (point))))) | |
798 | (if conflict | |
799 | ;; Update its location, just in case it got messed up. | |
800 | (move-overlay conflict (match-beginning 0) (match-end 0)) | |
801 | (setq conflict (make-overlay (match-beginning 0) (match-end 0) | |
802 | nil 'front-advance nil)) | |
803 | (overlay-put conflict 'evaporate t) | |
804 | (overlay-put conflict 'smerge 'conflict) | |
805 | (let ((props smerge-text-properties)) | |
806 | (while props | |
807 | (overlay-put conflict (pop props) (pop props)))))) | |
808 | (setq found t)) | |
809 | (error nil))) | |
810 | found)) | |
3dac25a9 | 811 | |
cd62539f SM |
812 | ;;; Refined change highlighting |
813 | ||
814 | (defvar smerge-refine-forward-function 'smerge-refine-forward | |
815 | "Function used to determine an \"atomic\" element. | |
816 | You can set it to `forward-char' to get char-level granularity. | |
817 | Its behavior has mainly two restrictions: | |
818 | - if this function encounters a newline, it's important that it stops right | |
819 | after the newline. | |
820 | This only matters if `smerge-refine-ignore-whitespace' is nil. | |
821 | - it needs to be unaffected by changes performed by the `preproc' argument | |
822 | to `smerge-refine-subst'. | |
823 | This only matters if `smerge-refine-weight-hack' is nil.") | |
824 | ||
825 | (defvar smerge-refine-ignore-whitespace t | |
826 | "If non-nil,Indicate that smerge-refine should try to ignore change in whitespace.") | |
827 | ||
828 | (defvar smerge-refine-weight-hack t | |
829 | "If non-nil, pass to diff as many lines as there are chars in the region. | |
830 | I.e. each atomic element (e.g. word) will be copied as many times (on different | |
831 | lines) as it has chars. This has 2 advantages: | |
832 | - if `diff' tries to minimize the number *lines* (rather than chars) | |
833 | added/removed, this adjust the weights so that adding/removing long | |
834 | symbols is considered correspondingly more costly. | |
835 | - `smerge-refine-forward-function' only needs to be called when chopping up | |
836 | the regions, and `forward-char' can be used afterwards. | |
837 | It has the following disadvantages: | |
838 | - cannot use `diff -w' because the weighting causes added spaces in a line | |
839 | to be represented as added copies of some line, so `diff -w' can't do the | |
840 | right thing any more. | |
841 | - may in degenerate cases take a 1KB input region and turn it into a 1MB | |
842 | file to pass to diff.") | |
843 | ||
844 | (defun smerge-refine-forward (n) | |
845 | (let ((case-fold-search nil) | |
846 | (re "[[:upper:]]?[[:lower:]]+\\|[[:upper:]]+\\|[[:digit:]]+\\|.\\|\n")) | |
847 | (when (and smerge-refine-ignore-whitespace | |
848 | ;; smerge-refine-weight-hack causes additional spaces to | |
849 | ;; appear as additional lines as well, so even if diff ignore | |
850 | ;; whitespace changes, it'll report added/removed lines :-( | |
851 | (not smerge-refine-weight-hack)) | |
852 | (setq re (concat "[ \t]*\\(?:" re "\\)"))) | |
853 | (dotimes (i n) | |
854 | (unless (looking-at re) (error "Smerge refine internal error")) | |
855 | (goto-char (match-end 0))))) | |
856 | ||
9f2e22a0 SM |
857 | (defun smerge-refine-chopup-region (beg end file &optional preproc) |
858 | "Chopup the region into small elements, one per line. | |
859 | Save the result into FILE. | |
860 | If non-nil, PREPROC is called with no argument in a buffer that contains | |
861 | a copy of the text, just before chopping it up. It can be used to replace | |
862 | chars to try and eliminate some spurious differences." | |
cd62539f SM |
863 | ;; We used to chop up char-by-char rather than word-by-word like ediff |
864 | ;; does. It had the benefit of simplicity and very fine results, but it | |
865 | ;; often suffered from problem that diff would find correlations where | |
866 | ;; there aren't any, so the resulting "change" didn't make much sense. | |
867 | ;; You can still get this behavior by setting | |
868 | ;; `smerge-refine-forward-function' to `forward-char'. | |
41796d09 SM |
869 | (let ((buf (current-buffer))) |
870 | (with-temp-buffer | |
871 | (insert-buffer-substring buf beg end) | |
9f2e22a0 | 872 | (when preproc (goto-char (point-min)) (funcall preproc)) |
cd62539f SM |
873 | (when smerge-refine-ignore-whitespace |
874 | ;; It doesn't make much of a difference for diff-fine-highlight | |
875 | ;; because we still have the _/+/</>/! prefix anyway. Can still be | |
876 | ;; useful in other circumstances. | |
877 | (subst-char-in-region (point-min) (point-max) ?\n ?\s)) | |
41796d09 SM |
878 | (goto-char (point-min)) |
879 | (while (not (eobp)) | |
cd62539f SM |
880 | (funcall smerge-refine-forward-function 1) |
881 | (let ((s (if (prog2 (forward-char -1) (bolp) (forward-char 1)) | |
882 | nil | |
883 | (buffer-substring (line-beginning-position) (point))))) | |
884 | ;; We add \n after each char except after \n, so we get | |
885 | ;; one line per text char, where each line contains | |
886 | ;; just one char, except for \n chars which are | |
887 | ;; represented by the empty line. | |
888 | (unless (eq (char-before) ?\n) (insert ?\n)) | |
889 | ;; HACK ALERT!! | |
890 | (if smerge-refine-weight-hack | |
891 | (dotimes (i (1- (length s))) (insert s "\n"))))) | |
892 | (unless (bolp) (error "Smerge refine internal error")) | |
41796d09 SM |
893 | (let ((coding-system-for-write 'emacs-mule)) |
894 | (write-region (point-min) (point-max) file nil 'nomessage))))) | |
895 | ||
9f2e22a0 | 896 | (defun smerge-refine-highlight-change (buf beg match-num1 match-num2 props) |
cd62539f SM |
897 | (with-current-buffer buf |
898 | (goto-char beg) | |
899 | (let* ((startline (- (string-to-number match-num1) 1)) | |
900 | (beg (progn (funcall (if smerge-refine-weight-hack | |
901 | 'forward-char | |
902 | smerge-refine-forward-function) | |
903 | startline) | |
904 | (point))) | |
905 | (end (progn (funcall (if smerge-refine-weight-hack | |
906 | 'forward-char | |
907 | smerge-refine-forward-function) | |
908 | (if match-num2 | |
909 | (- (string-to-number match-num2) | |
910 | startline) | |
911 | 1)) | |
912 | (point)))) | |
913 | (when smerge-refine-ignore-whitespace | |
914 | (skip-chars-backward " \t\n" beg) (setq end (point)) | |
915 | (goto-char beg) | |
916 | (skip-chars-forward " \t\n" end) (setq beg (point))) | |
917 | (when (> end beg) | |
918 | (let ((ol (make-overlay | |
919 | beg end nil | |
920 | ;; Make them tend to shrink rather than spread when editing. | |
921 | 'front-advance nil))) | |
922 | (overlay-put ol 'evaporate t) | |
923 | (dolist (x props) (overlay-put ol (car x) (cdr x))) | |
924 | ol))))) | |
9f2e22a0 SM |
925 | |
926 | (defun smerge-refine-subst (beg1 end1 beg2 end2 props &optional preproc) | |
927 | "Show fine differences in the two regions BEG1..END1 and BEG2..END2. | |
928 | PROPS is an alist of properties to put (via overlays) on the changes. | |
929 | If non-nil, PREPROC is called with no argument in a buffer that contains | |
930 | a copy of a region, just before preparing it to for `diff'. It can be used to | |
931 | replace chars to try and eliminate some spurious differences." | |
932 | (let* ((buf (current-buffer)) | |
cd62539f | 933 | (pos (point)) |
9f2e22a0 SM |
934 | (file1 (make-temp-file "diff1")) |
935 | (file2 (make-temp-file "diff2"))) | |
41796d09 | 936 | ;; Chop up regions into smaller elements and save into files. |
9f2e22a0 SM |
937 | (smerge-refine-chopup-region beg1 end1 file1 preproc) |
938 | (smerge-refine-chopup-region beg2 end2 file2 preproc) | |
41796d09 SM |
939 | |
940 | ;; Call diff on those files. | |
941 | (unwind-protect | |
942 | (with-temp-buffer | |
943 | (let ((coding-system-for-read 'emacs-mule)) | |
cd62539f SM |
944 | (call-process diff-command nil t nil |
945 | (if (and smerge-refine-ignore-whitespace | |
946 | (not smerge-refine-weight-hack)) | |
f56f00fa SM |
947 | ;; Pass -a so diff treats it as a text file even |
948 | ;; if it contains \0 and such. | |
949 | ;; Pass -d so as to get the smallest change, but | |
950 | ;; also and more importantly because otherwise it | |
951 | ;; may happen that diff doesn't behave like | |
952 | ;; smerge-refine-weight-hack expects it to. | |
953 | ;; See http://thread.gmane.org/gmane.emacs.devel/82685. | |
954 | "-awd" "-ad") | |
cd62539f | 955 | file1 file2)) |
41796d09 SM |
956 | ;; Process diff's output. |
957 | (goto-char (point-min)) | |
cd62539f SM |
958 | (let ((last1 nil) |
959 | (last2 nil)) | |
960 | (while (not (eobp)) | |
961 | (if (not (looking-at "\\([0-9]+\\)\\(?:,\\([0-9]+\\)\\)?\\([acd]\\)\\([0-9]+\\)\\(?:,\\([0-9]+\\)\\)?$")) | |
962 | (error "Unexpected patch hunk header: %s" | |
963 | (buffer-substring (point) (line-end-position)))) | |
964 | (let ((op (char-after (match-beginning 3))) | |
965 | (m1 (match-string 1)) | |
966 | (m2 (match-string 2)) | |
967 | (m4 (match-string 4)) | |
968 | (m5 (match-string 5))) | |
41796d09 | 969 | (when (memq op '(?d ?c)) |
cd62539f SM |
970 | (setq last1 |
971 | (smerge-refine-highlight-change buf beg1 m1 m2 props))) | |
41796d09 | 972 | (when (memq op '(?a ?c)) |
cd62539f SM |
973 | (setq last2 |
974 | (smerge-refine-highlight-change buf beg2 m4 m5 props)))) | |
41796d09 SM |
975 | (forward-line 1) ;Skip hunk header. |
976 | (and (re-search-forward "^[0-9]" nil 'move) ;Skip hunk body. | |
cd62539f SM |
977 | (goto-char (match-beginning 0)))) |
978 | ;; (assert (or (null last1) (< (overlay-start last1) end1))) | |
979 | ;; (assert (or (null last2) (< (overlay-start last2) end2))) | |
980 | (if smerge-refine-weight-hack | |
981 | (progn | |
982 | ;; (assert (or (null last1) (<= (overlay-end last1) end1))) | |
983 | ;; (assert (or (null last2) (<= (overlay-end last2) end2))) | |
984 | ) | |
985 | ;; smerge-refine-forward-function when calling in chopup may | |
986 | ;; have stopped because it bumped into EOB whereas in | |
987 | ;; smerge-refine-weight-hack it may go a bit further. | |
988 | (if (and last1 (> (overlay-end last1) end1)) | |
989 | (move-overlay last1 (overlay-start last1) end1)) | |
990 | (if (and last2 (> (overlay-end last2) end2)) | |
991 | (move-overlay last2 (overlay-start last2) end2)) | |
992 | ))) | |
993 | (goto-char pos) | |
41796d09 SM |
994 | (delete-file file1) |
995 | (delete-file file2)))) | |
996 | ||
2fa42bb7 SM |
997 | (defun smerge-refine (&optional part) |
998 | "Highlight the words of the conflict that are different. | |
999 | For 3-way conflicts, highlights only 2 of the 3 parts. | |
1000 | A numeric argument PART can be used to specify which 2 parts; | |
1001 | repeating the command will highlight other 2 parts." | |
1002 | (interactive | |
1003 | (if (integerp current-prefix-arg) (list current-prefix-arg) | |
1004 | (smerge-match-conflict) | |
1005 | (let* ((prop (get-text-property (match-beginning 0) 'smerge-refine-part)) | |
1006 | (part (if (and (consp prop) | |
1007 | (eq (buffer-chars-modified-tick) (car prop))) | |
1008 | (cdr prop)))) | |
1009 | ;; If already highlighted, cycle. | |
1010 | (list (if (integerp part) (1+ (mod part 3))))))) | |
1011 | ||
1012 | (if (and (integerp part) (or (< part 1) (> part 3))) | |
1013 | (error "No conflict part nb %s" part)) | |
9f2e22a0 SM |
1014 | (smerge-match-conflict) |
1015 | (remove-overlays (match-beginning 0) (match-end 0) 'smerge 'refine) | |
2fa42bb7 SM |
1016 | ;; Ignore `part' if not applicable, and default it if not provided. |
1017 | (setq part (cond ((null (match-end 2)) 2) | |
1018 | ((eq (match-end 1) (match-end 3)) 1) | |
1019 | ((integerp part) part) | |
1020 | (t 2))) | |
1021 | (let ((n1 (if (eq part 1) 2 1)) | |
1022 | (n2 (if (eq part 3) 2 3))) | |
1023 | (smerge-ensure-match n1) | |
1024 | (smerge-ensure-match n2) | |
1025 | (put-text-property (match-beginning 0) (1+ (match-beginning 0)) | |
1026 | 'smerge-refine-part | |
1027 | (cons (buffer-chars-modified-tick) part)) | |
30e68410 | 1028 | (smerge-refine-subst (match-beginning n1) (match-end n1) |
2fa42bb7 | 1029 | (match-beginning n2) (match-end n2) |
30e68410 SM |
1030 | '((smerge . refine) |
1031 | (face . smerge-refined-change))))) | |
9f2e22a0 | 1032 | |
3dac25a9 SM |
1033 | (defun smerge-diff (n1 n2) |
1034 | (smerge-match-conflict) | |
1035 | (smerge-ensure-match n1) | |
1036 | (smerge-ensure-match n2) | |
1037 | (let ((name1 (aref smerge-match-names n1)) | |
1038 | (name2 (aref smerge-match-names n2)) | |
e29f823e SM |
1039 | ;; Read them before the match-data gets clobbered. |
1040 | (beg1 (match-beginning n1)) | |
1041 | (end1 (match-end n1)) | |
1042 | (beg2 (match-beginning n2)) | |
1043 | (end2 (match-end n2)) | |
3dac25a9 | 1044 | (file1 (make-temp-file "smerge1")) |
d73aed13 SM |
1045 | (file2 (make-temp-file "smerge2")) |
1046 | (dir default-directory) | |
48d59eda SM |
1047 | (file (if buffer-file-name (file-relative-name buffer-file-name))) |
1048 | ;; We would want to use `emacs-mule-unix' for read&write, but we | |
1049 | ;; bump into problems with the coding-system used by diff to write | |
1050 | ;; the file names and the time stamps in the header. | |
1051 | ;; `buffer-file-coding-system' is not always correct either, but if | |
1052 | ;; the OS/user uses only one coding-system, then it works. | |
e29f823e | 1053 | (coding-system-for-read buffer-file-coding-system)) |
814838df SM |
1054 | (write-region beg1 end1 file1 nil 'nomessage) |
1055 | (write-region beg2 end2 file2 nil 'nomessage) | |
3dac25a9 SM |
1056 | (unwind-protect |
1057 | (with-current-buffer (get-buffer-create smerge-diff-buffer-name) | |
d73aed13 | 1058 | (setq default-directory dir) |
3dac25a9 SM |
1059 | (let ((inhibit-read-only t)) |
1060 | (erase-buffer) | |
814838df SM |
1061 | (let ((status |
1062 | (apply 'call-process diff-command nil t nil | |
1063 | (append smerge-diff-switches | |
1064 | (list "-L" (concat name1 "/" file) | |
1065 | "-L" (concat name2 "/" file) | |
1066 | file1 file2))))) | |
1067 | (if (eq status 0) (insert "No differences found.\n")))) | |
3dac25a9 SM |
1068 | (goto-char (point-min)) |
1069 | (diff-mode) | |
1070 | (display-buffer (current-buffer) t)) | |
1071 | (delete-file file1) | |
1072 | (delete-file file2)))) | |
1073 | ||
814838df SM |
1074 | ;; compiler pacifiers |
1075 | (defvar smerge-ediff-windows) | |
1076 | (defvar smerge-ediff-buf) | |
1077 | (defvar ediff-buffer-A) | |
1078 | (defvar ediff-buffer-B) | |
1079 | (defvar ediff-buffer-C) | |
48d59eda SM |
1080 | (defvar ediff-ancestor-buffer) |
1081 | (defvar ediff-quit-hook) | |
004a00f4 | 1082 | (declare-function ediff-cleanup-mess "ediff-util" nil) |
3dac25a9 | 1083 | |
a1038ca0 | 1084 | ;;;###autoload |
15092da1 SM |
1085 | (defun smerge-ediff (&optional name-mine name-other name-base) |
1086 | "Invoke ediff to resolve the conflicts. | |
1087 | NAME-MINE, NAME-OTHER, and NAME-BASE, if non-nil, are used for the | |
1088 | buffer names." | |
3dac25a9 SM |
1089 | (interactive) |
1090 | (let* ((buf (current-buffer)) | |
1091 | (mode major-mode) | |
1092 | ;;(ediff-default-variant 'default-B) | |
1093 | (config (current-window-configuration)) | |
1094 | (filename (file-name-nondirectory buffer-file-name)) | |
15092da1 SM |
1095 | (mine (generate-new-buffer |
1096 | (or name-mine (concat "*" filename " MINE*")))) | |
1097 | (other (generate-new-buffer | |
1098 | (or name-other (concat "*" filename " OTHER*")))) | |
3dac25a9 SM |
1099 | base) |
1100 | (with-current-buffer mine | |
1101 | (buffer-disable-undo) | |
1102 | (insert-buffer-substring buf) | |
1103 | (goto-char (point-min)) | |
1104 | (while (smerge-find-conflict) | |
1105 | (when (match-beginning 2) (setq base t)) | |
5bd8d87b | 1106 | (smerge-keep-n 1)) |
3dac25a9 SM |
1107 | (buffer-enable-undo) |
1108 | (set-buffer-modified-p nil) | |
1109 | (funcall mode)) | |
1110 | ||
1111 | (with-current-buffer other | |
1112 | (buffer-disable-undo) | |
1113 | (insert-buffer-substring buf) | |
1114 | (goto-char (point-min)) | |
1115 | (while (smerge-find-conflict) | |
5bd8d87b | 1116 | (smerge-keep-n 3)) |
3dac25a9 SM |
1117 | (buffer-enable-undo) |
1118 | (set-buffer-modified-p nil) | |
1119 | (funcall mode)) | |
f1180544 | 1120 | |
3dac25a9 | 1121 | (when base |
15092da1 SM |
1122 | (setq base (generate-new-buffer |
1123 | (or name-base (concat "*" filename " BASE*")))) | |
3dac25a9 SM |
1124 | (with-current-buffer base |
1125 | (buffer-disable-undo) | |
1126 | (insert-buffer-substring buf) | |
1127 | (goto-char (point-min)) | |
1128 | (while (smerge-find-conflict) | |
5bd8d87b SM |
1129 | (if (match-end 2) |
1130 | (smerge-keep-n 2) | |
1131 | (delete-region (match-beginning 0) (match-end 0)))) | |
3dac25a9 SM |
1132 | (buffer-enable-undo) |
1133 | (set-buffer-modified-p nil) | |
1134 | (funcall mode))) | |
f1180544 | 1135 | |
3dac25a9 SM |
1136 | ;; the rest of the code is inspired from vc.el |
1137 | ;; Fire up ediff. | |
1138 | (set-buffer | |
1139 | (if base | |
1140 | (ediff-merge-buffers-with-ancestor mine other base) | |
1141 | ;; nil 'ediff-merge-revisions-with-ancestor buffer-file-name) | |
1142 | (ediff-merge-buffers mine other))) | |
1143 | ;; nil 'ediff-merge-revisions buffer-file-name))) | |
f1180544 | 1144 | |
3dac25a9 SM |
1145 | ;; Ediff is now set up, and we are in the control buffer. |
1146 | ;; Do a few further adjustments and take precautions for exit. | |
1147 | (set (make-local-variable 'smerge-ediff-windows) config) | |
1148 | (set (make-local-variable 'smerge-ediff-buf) buf) | |
1149 | (set (make-local-variable 'ediff-quit-hook) | |
1150 | (lambda () | |
1151 | (let ((buffer-A ediff-buffer-A) | |
1152 | (buffer-B ediff-buffer-B) | |
1153 | (buffer-C ediff-buffer-C) | |
1154 | (buffer-Ancestor ediff-ancestor-buffer) | |
1155 | (buf smerge-ediff-buf) | |
1156 | (windows smerge-ediff-windows)) | |
1157 | (ediff-cleanup-mess) | |
1158 | (with-current-buffer buf | |
1159 | (erase-buffer) | |
a34ed813 | 1160 | (insert-buffer-substring buffer-C) |
3dac25a9 SM |
1161 | (kill-buffer buffer-A) |
1162 | (kill-buffer buffer-B) | |
1163 | (kill-buffer buffer-C) | |
1164 | (when (bufferp buffer-Ancestor) (kill-buffer buffer-Ancestor)) | |
1165 | (set-window-configuration windows) | |
1166 | (message "Conflict resolution finished; you may save the buffer"))))) | |
1167 | (message "Please resolve conflicts now; exit ediff when done"))) | |
1168 | ||
30e68410 SM |
1169 | (defun smerge-makeup-conflict (pt1 pt2 pt3 &optional pt4) |
1170 | "Insert diff3 markers to make a new conflict. | |
1171 | Uses point and mark for 2 of the relevant positions and previous marks | |
1172 | for the other ones. | |
1173 | By default, makes up a 2-way conflict, | |
1174 | with a \\[universal-argument] prefix, makes up a 3-way conflict." | |
1175 | (interactive | |
1176 | (list (point) | |
1177 | (mark) | |
1178 | (progn (pop-mark) (mark)) | |
1179 | (when current-prefix-arg (pop-mark) (mark)))) | |
1180 | ;; Start from the end so as to avoid problems with pos-changes. | |
1181 | (destructuring-bind (pt1 pt2 pt3 &optional pt4) | |
1182 | (sort (list* pt1 pt2 pt3 (if pt4 (list pt4))) '>=) | |
1183 | (goto-char pt1) (beginning-of-line) | |
1184 | (insert ">>>>>>> OTHER\n") | |
1185 | (goto-char pt2) (beginning-of-line) | |
1186 | (insert "=======\n") | |
1187 | (goto-char pt3) (beginning-of-line) | |
1188 | (when pt4 | |
1189 | (insert "||||||| BASE\n") | |
1190 | (goto-char pt4) (beginning-of-line)) | |
1191 | (insert "<<<<<<< MINE\n")) | |
1192 | (if smerge-mode nil (smerge-mode 1)) | |
1193 | (smerge-refine)) | |
1194 | ||
3dac25a9 | 1195 | |
de689511 SM |
1196 | (defconst smerge-parsep-re |
1197 | (concat smerge-begin-re "\\|" smerge-end-re "\\|" | |
1198 | smerge-base-re "\\|" smerge-other-re "\\|")) | |
1199 | ||
3dac25a9 SM |
1200 | ;;;###autoload |
1201 | (define-minor-mode smerge-mode | |
1202 | "Minor mode to simplify editing output from the diff3 program. | |
1203 | \\{smerge-mode-map}" | |
c06dbb8f | 1204 | :group 'smerge :lighter " SMerge" |
0304b9c7 | 1205 | (when (and (boundp 'font-lock-mode) font-lock-mode) |
3dac25a9 SM |
1206 | (save-excursion |
1207 | (if smerge-mode | |
1208 | (font-lock-add-keywords nil smerge-font-lock-keywords 'append) | |
1209 | (font-lock-remove-keywords nil smerge-font-lock-keywords)) | |
1210 | (goto-char (point-min)) | |
1211 | (while (smerge-find-conflict) | |
6eabfb26 | 1212 | (save-excursion |
48d59eda | 1213 | (font-lock-fontify-region (match-beginning 0) (match-end 0) nil))))) |
de689511 SM |
1214 | (if (string-match (regexp-quote smerge-parsep-re) paragraph-separate) |
1215 | (unless smerge-mode | |
1216 | (set (make-local-variable 'paragraph-separate) | |
1217 | (replace-match "" t t paragraph-separate))) | |
1218 | (when smerge-mode | |
1219 | (set (make-local-variable 'paragraph-separate) | |
1220 | (concat smerge-parsep-re paragraph-separate)))) | |
48d59eda SM |
1221 | (unless smerge-mode |
1222 | (smerge-remove-props (point-min) (point-max)))) | |
3dac25a9 | 1223 | |
ba463d9e | 1224 | ;;;###autoload |
28e4e2b4 | 1225 | (defun smerge-start-session () |
ba463d9e DN |
1226 | "Turn on `smerge-mode' and move point to first conflict marker. |
1227 | If no conflict maker is found, turn off `smerge-mode'." | |
1228 | (smerge-mode 1) | |
1229 | (condition-case nil | |
1230 | (smerge-next) | |
1231 | (error (smerge-auto-leave)))) | |
3dac25a9 SM |
1232 | |
1233 | (provide 'smerge-mode) | |
ab5796a9 | 1234 | |
9f0c286d | 1235 | ;; arch-tag: 605c8d1e-e43d-4943-a6f3-1bcc4333e690 |
3dac25a9 | 1236 | ;;; smerge-mode.el ends here |