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