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