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