1 ;;; smerge-mode.el --- Minor mode to resolve diff3 conflicts -*- lexical-binding: t -*-
3 ;; Copyright (C) 1999-2018 Free Software Foundation, Inc.
5 ;; Author: Stefan Monnier <monnier@iro.umontreal.ca>
6 ;; Keywords: vc, tools, revision control, merge, diff3, cvs, conflict
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
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.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
25 ;; Provides a lightweight alternative to emerge/ediff.
26 ;; To use it, simply add to your .emacs the following lines:
28 ;; (autoload 'smerge-mode "smerge-mode" nil t)
30 ;; you can even have it turned on automatically with the following
31 ;; piece of code in your .emacs:
33 ;; (defun sm-try-smerge ()
35 ;; (goto-char (point-min))
36 ;; (when (re-search-forward "^<<<<<<< " nil t)
38 ;; (add-hook 'find-file-hook 'sm-try-smerge t)
42 ;; - if requested, ask the user whether he wants to call ediff right away
46 (eval-when-compile (require 'cl-lib
))
47 (require 'diff-mode
) ;For diff-auto-refine-mode.
50 ;;; The real definition comes later.
54 "Minor mode to highlight and resolve diff3 conflicts."
58 (defcustom smerge-diff-buffer-name
"*vc-diff*"
59 "Buffer name to use for displaying diffs."
63 (const "*smerge-diff*")
66 (defcustom smerge-diff-switches
68 (if (listp diff-switches
) diff-switches
(list diff-switches
)))
69 "A list of strings specifying switches to be passed to diff.
70 Used in `smerge-diff-base-upper' and related functions."
71 :type
'(repeat string
))
73 (defcustom smerge-auto-leave t
74 "Non-nil means to leave `smerge-mode' when the last conflict is resolved."
78 '((((class color
) (min-colors 88) (background light
))
79 :background
"#ffdddd")
80 (((class color
) (min-colors 88) (background dark
))
81 :background
"#553333")
84 "Face for the `upper' version of a conflict.")
85 (define-obsolete-face-alias 'smerge-mine
'smerge-upper
"26.1")
86 (defvar smerge-upper-face
'smerge-upper
)
89 '((((class color
) (min-colors 88) (background light
))
90 :background
"#ddffdd")
91 (((class color
) (min-colors 88) (background dark
))
92 :background
"#335533")
95 "Face for the `lower' version of a conflict.")
96 (define-obsolete-face-alias 'smerge-other
'smerge-lower
"26.1")
97 (defvar smerge-lower-face
'smerge-lower
)
100 '((((class color
) (min-colors 88) (background light
))
101 :background
"#ffffaa")
102 (((class color
) (min-colors 88) (background dark
))
103 :background
"#888833")
105 :foreground
"yellow"))
106 "Face for the base code.")
107 (defvar smerge-base-face
'smerge-base
)
109 (defface smerge-markers
110 '((((background light
))
111 (:background
"grey85"))
113 (:background
"grey30")))
114 "Face for the conflict markers.")
115 (defvar smerge-markers-face
'smerge-markers
)
117 (defface smerge-refined-changed
119 "Face used for char-based changes shown by `smerge-refine'.")
120 (define-obsolete-face-alias 'smerge-refined-change
'smerge-refined-changed
"24.5")
122 (defface smerge-refined-removed
124 :inherit smerge-refined-change
)
125 (((class color
) (min-colors 88) (background light
))
126 :background
"#ffbbbb")
127 (((class color
) (min-colors 88) (background dark
))
128 :background
"#aa2222")
129 (t :inverse-video t
))
130 "Face used for removed characters shown by `smerge-refine'."
133 (defface smerge-refined-added
135 :inherit smerge-refined-change
)
136 (((class color
) (min-colors 88) (background light
))
137 :background
"#aaffaa")
138 (((class color
) (min-colors 88) (background dark
))
139 :background
"#22aa22")
140 (t :inverse-video t
))
141 "Face used for added characters shown by `smerge-refine'."
144 (easy-mmode-defmap smerge-basic-map
145 `(("n" . smerge-next
)
147 ("r" . smerge-resolve
)
148 ("a" . smerge-keep-all
)
149 ("b" . smerge-keep-base
)
150 ("o" . smerge-keep-lower
) ; for the obsolete keep-other
151 ("l" . smerge-keep-lower
)
152 ("m" . smerge-keep-upper
) ; for the obsolete keep-mine
153 ("u" . smerge-keep-upper
)
155 ("C" . smerge-combine-with-next
)
156 ("R" . smerge-refine
)
157 ("\C-m" . smerge-keep-current
)
158 ("=" .
,(make-sparse-keymap "Diff"))
159 ("=<" "base-upper" . smerge-diff-base-upper
)
160 ("=>" "base-lower" . smerge-diff-base-lower
)
161 ("==" "upper-lower" . smerge-diff-upper-lower
))
162 "The base keymap for `smerge-mode'.")
164 (defcustom smerge-command-prefix
"\C-c^"
165 "Prefix for `smerge-mode' commands."
166 :type
'(choice (const :tag
"ESC" "\e")
167 (const :tag
"C-c ^" "\C-c^" )
168 (const :tag
"none" "")
171 (easy-mmode-defmap smerge-mode-map
172 `((,smerge-command-prefix .
,smerge-basic-map
))
173 "Keymap for `smerge-mode'.")
175 (defvar smerge-check-cache nil
)
176 (make-variable-buffer-local 'smerge-check-cache
)
177 (defun smerge-check (n)
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
)))
186 (easy-menu-define smerge-mode-menu smerge-mode-map
187 "Menu for `smerge-mode'."
189 ["Next" smerge-next
:help
"Go to next conflict"]
190 ["Previous" smerge-prev
:help
"Go to previous conflict"]
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))]
197 ["Revert to Base" smerge-keep-base
:help
"Revert to base version"
198 :active
(smerge-check 2)]
199 ["Keep Upper" smerge-keep-upper
:help
"Keep `upper' version"
200 :active
(smerge-check 1)]
201 ["Keep Lower" smerge-keep-lower
:help
"Keep `lower' version"
202 :active
(smerge-check 3)]
204 ["Diff Base/Upper" smerge-diff-base-upper
205 :help
"Diff `base' and `upper' for current conflict"
206 :active
(smerge-check 2)]
207 ["Diff Base/Lower" smerge-diff-base-lower
208 :help
"Diff `base' and `lower' for current conflict"
209 :active
(smerge-check 2)]
210 ["Diff Upper/Lower" smerge-diff-upper-lower
211 :help
"Diff `upper' and `lower' for current conflict"
212 :active
(smerge-check 1)]
214 ["Invoke Ediff" smerge-ediff
215 :help
"Use Ediff to resolve the conflicts"
216 :active
(smerge-check 1)]
217 ["Auto Resolve" smerge-resolve
218 :help
"Try auto-resolution heuristics"
219 :active
(smerge-check 1)]
220 ["Combine" smerge-combine-with-next
221 :help
"Combine current conflict with next"
222 :active
(smerge-check 1)]
225 (easy-menu-define smerge-context-menu nil
226 "Context menu for upper area in `smerge-mode'."
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"]
232 ["More..." (popup-menu smerge-mode-menu
) :help
"Show full SMerge mode menu"]
235 (defconst smerge-font-lock-keywords
236 '((smerge-find-conflict
237 (1 smerge-upper-face prepend t
)
238 (2 smerge-base-face prepend t
)
239 (3 smerge-lower-face prepend t
)
240 ;; FIXME: `keep' doesn't work right with syntactic fontification.
241 (0 smerge-markers-face keep
)
244 "Font lock patterns for `smerge-mode'.")
246 (defconst smerge-begin-re
"^<<<<<<< \\(.*\\)\n")
247 (defconst smerge-end-re
"^>>>>>>> \\(.*\\)\n")
248 (defconst smerge-base-re
"^||||||| \\(.*\\)\n")
249 (defconst smerge-lower-re
"^=======\n")
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:
257 ;; Compiler pacifiers
258 (defvar font-lock-mode
)
259 (defvar font-lock-keywords
)
265 ;; Define smerge-next and smerge-prev
266 (easy-mmode-define-navigation smerge smerge-begin-re
"conflict" nil nil
267 (if diff-auto-refine-mode
268 (condition-case nil
(smerge-refine) (error nil
))))
270 (defconst smerge-match-names
["conflict" "upper" "base" "lower"])
272 (defun smerge-ensure-match (n)
273 (unless (match-end n
)
274 (error "No `%s'" (aref smerge-match-names n
))))
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
))))
280 (when (and (listp buffer-undo-list
) smerge-mode
)
281 (push (list 'apply
'smerge-mode
1) buffer-undo-list
))
285 (defun smerge-keep-all ()
286 "Concatenate all versions."
288 (smerge-match-conflict)
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)))
298 (defun smerge-keep-n (n)
299 (smerge-remove-props (match-beginning 0) (match-end 0))
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
)))
304 (defun smerge-combine-with-next ()
305 "Combine the current conflict with the next one."
306 ;; `smerge-auto-combine' relies on the finish position (at the beginning
307 ;; of the closing marker).
309 (smerge-match-conflict)
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
)))
320 ;; First copy the in-between text in each alternative.
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.
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
))
336 (dolist (m match-data
) (if m
(move-marker m nil
)))
337 (mapc (lambda (m) (if m
(move-marker m nil
))) ends
)))))
339 (defvar smerge-auto-combine-max-separation
2
340 "Max number of lines between conflicts that should be combined.")
342 (defun smerge-auto-combine ()
343 "Automatically combine conflicts that are near each other."
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.
358 (defvar smerge-resolve-function
359 (lambda () (user-error "Don't know how to resolve"))
360 "Mode-specific merge function.
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
363 according to `smerge-match-conflict'.")
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
))))
370 (defun smerge-remove-props (beg end
)
371 (remove-overlays beg end
'smerge
'refine
)
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)
382 (with-silent-modifications
383 (remove-text-properties beg end
'(fontified nil
))))
385 (defun smerge-popup-context-menu (event)
386 "Pop up the Smerge mode context menu under mouse."
389 (save-excursion (posn-set-point (event-end event
)) (smerge-check 1)))
391 (posn-set-point (event-end event
))
392 (smerge-match-conflict)
393 (let ((i (smerge-get-current))
397 (popup-menu smerge-mode-menu
)
399 (setq o
(make-overlay (match-beginning i
) (match-end i
)))
402 (overlay-put o
'face
'highlight
)
403 (sit-for 0) ;Display the new highlighting.
404 (popup-menu smerge-context-menu
))
406 (delete-overlay o
)))))
407 ;; There's no conflict at point, the text-props are just obsolete.
409 (let ((beg (re-search-backward smerge-end-re nil t
))
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
)))))
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."
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
)
429 (setq m3e
(- m3e
(- m3b m0b
)))
431 (with-current-buffer buf
432 (goto-char (point-min))
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))
446 (lines (- endline startline
))
449 ((null (match-end 5)) 1)
450 (t (- (string-to-number (match-string 5))
451 (string-to-number (match-string 4)) -
1))))
453 (forward-line 1) ;Skip header.
454 (forward-line lines
) ;Skip deleted text.
455 (if (eq op ?c
) (forward-line 1)) ;Skip separator.
457 (if (null otherlines
) ""
459 (dotimes (_i otherlines
) (delete-char 2) (forward-line 1))
460 (buffer-substring pos
(point)))))
461 (with-current-buffer textbuf
462 (forward-line (- startline line
))
463 (insert "<<<<<<< " name1
"\n" othertext
464 (if name2
(concat "||||||| " name2
"\n") "")
467 (insert ">>>>>>> " name3
"\n")
468 (setq line endline
))))))))
470 (defconst smerge-resolve--normalize-re
"[\n\t][ \t\n]*\\| [ \t\n]+")
472 (defun smerge-resolve--extract-comment (beg end
)
473 "Extract the text within the comments that span BEG..END."
478 (while (and (< (point) end
)
479 (setq combeg
(comment-search-forward end t
)))
484 (comment-enter-backward)
486 (push (buffer-substring-no-properties beg
(point)) comments
))))
489 (apply #'insert
(nreverse comments
))
490 (goto-char (point-min))
491 (while (re-search-forward smerge-resolve--normalize-re
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
) " ")))
501 (defun smerge-resolve (&optional safe
)
502 "Resolve the conflict at point intelligently.
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."
506 (smerge-match-conflict)
507 (smerge-remove-props (match-beginning 0) (match-end 0))
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))
517 (buf (generate-new-buffer " *smerge*"))
523 ;; Trivial diff3 -A non-conflicts.
524 ((and (eq (match-end 1) (match-end 3))
525 (eq (match-beginning 1) (match-beginning 3)))
527 ;; Mode-specific conflict resolution.
531 (funcall smerge-resolve-function safe
)
532 (funcall smerge-resolve-function
))
535 ;; Nothing to do: the resolution function has done it already.
538 ((and (eq m1e m3e
) (eq m1b m3b
))
539 (set-match-data md
) (smerge-keep-n 3))
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.
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
)))
557 (with-current-buffer buf
558 (goto-char (point-min))
559 ;; Make sure there's some refinement.
561 (concat "1," (number-to-string lines
) "c"))))))
562 (smerge-apply-resolution-patch buf m0b m0e m3b m3e m2b
))
563 ;; "Mere whitespace changes" conflicts.
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
)))
573 ;; Try "diff -b BASE UPPER | patch LOWER".
574 ((when (and (not safe
) m2e b
575 ;; If the BASE is empty, this would just concatenate
576 ;; the two, which is rarely right.
578 ;; BEWARE: we're using here the patch of the previous test.
579 (with-current-buffer buf
580 (zerop (call-process-region
581 (point-min) (point-max) "patch" t nil nil
582 "-r" null-device
"--no-backup-if-mismatch"
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 LOWER | patch UPPER".
589 ((when (and (not safe
) m2e b
590 ;; If the BASE is empty, this would just concatenate
591 ;; the two, which is rarely right.
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
598 "-r" null-device
"--no-backup-if-mismatch"
601 (narrow-to-region m0b m0e
)
602 (smerge-remove-props m0b m0e
)
603 (insert-file-contents m nil nil nil t
)))
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.
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
)))
615 ((and (equal t1 t2
) (not (equal t2 t3
)))
617 ((and (not (equal t1 t2
)) (equal t2 t3
))
620 (smerge-keep-n choice
))
621 ;; Idem, when the conflict is contained within a single comment.
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
)))
631 (let ((t1 (smerge-resolve--normalize m1b m1e
))
632 (t2 (smerge-resolve--normalize m2b m2e
))
633 (t3 (smerge-resolve--normalize m3b m3e
)))
635 ((and (equal t1 t2
) (not (equal t2 t3
)))
637 ((and (not (equal t1 t2
)) (equal t2 t3
))
640 (smerge-keep-n choice
))
642 (user-error "Don't know how to resolve"))))
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
))))
649 (defun smerge-resolve-all ()
650 "Perform automatic resolution on all conflicts."
653 (goto-char (point-min))
654 (while (re-search-forward smerge-begin-re nil t
)
657 (smerge-match-conflict)
658 (smerge-resolve 'safe
))
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
)))
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
672 (setq file
(substring file
0 (match-beginning 0))))
673 (message "Resolving conflicts in %s..." file
)
674 (when (file-readable-p file
)
675 (with-current-buffer (find-file-noselect file
)
678 (kill-buffer (current-buffer)))))))
680 (defun smerge-keep-base ()
681 "Revert to the base version."
683 (smerge-match-conflict)
684 (smerge-ensure-match 2)
688 (defun smerge-keep-lower ()
689 "Keep the \"lower\" version of a merge conflict.
690 In a conflict that looks like:
698 (smerge-match-conflict)
699 ;;(smerge-ensure-match 3)
703 (define-obsolete-function-alias 'smerge-keep-other
'smerge-keep-lower
"26.1")
705 (defun smerge-keep-upper ()
706 "Keep the \"upper\" version of a merge conflict.
707 In a conflict that looks like:
715 (smerge-match-conflict)
716 ;;(smerge-ensure-match 1)
720 (define-obsolete-function-alias 'smerge-keep-mine
'smerge-keep-upper
"26.1")
722 (defun smerge-get-current ()
724 (while (or (not (match-end i
))
725 (< (point) (match-beginning i
))
726 (> (point) (match-end i
)))
730 (defun smerge-keep-current ()
731 "Use the current (under the cursor) version."
733 (smerge-match-conflict)
734 (let ((i (smerge-get-current)))
735 (if (<= i
0) (error "Not inside a version")
737 (smerge-auto-leave))))
739 (defun smerge-kill-current ()
740 "Remove the current (under the cursor) version."
742 (smerge-match-conflict)
743 (let ((i (smerge-get-current)))
744 (if (<= i
0) (error "Not inside a version")
747 (if (and (match-end n
) (/= (match-end n
) (match-end i
)))
750 (/= (match-end (car left
)) (match-end (cadr left
))))
751 (ding) ;We don't know how to do that.
752 (smerge-keep-n (car left
))
753 (smerge-auto-leave))))))
755 (defun smerge-diff-base-upper ()
756 "Diff `base' and `upper' version in current conflict region."
760 (define-obsolete-function-alias 'smerge-diff-base-mine
761 'smerge-diff-base-upper
"26.1")
763 (defun smerge-diff-base-lower ()
764 "Diff `base' and `lower' version in current conflict region."
768 (define-obsolete-function-alias 'smerge-diff-base-other
769 'smerge-diff-base-lower
"26.1")
771 (defun smerge-diff-upper-lower ()
772 "Diff `upper' and `lower' version in current conflict region."
776 (define-obsolete-function-alias 'smerge-diff-mine-other
777 'smerge-diff-upper-lower
"26.1")
779 (defun smerge-match-conflict ()
780 "Get info about the conflict. Puts the info in the `match-data'.
781 The submatches contain:
782 0: the whole conflict.
783 1: upper version of the code.
784 2: base version of the code.
785 3: lower version of the code.
786 An error is raised if not inside a conflict."
789 (let* ((orig-point (point))
792 (_ (re-search-backward smerge-begin-re
))
794 (start (match-beginning 0))
795 (upper-start (match-end 0))
796 (filename (or (match-string 1) ""))
798 (_ (re-search-forward smerge-end-re
))
799 (_ (cl-assert (< orig-point
(match-end 0))))
801 (lower-end (match-beginning 0))
804 (_ (re-search-backward smerge-lower-re start
))
806 (upper-end (match-beginning 0))
807 (lower-start (match-end 0))
811 ;; handle the various conflict styles
814 (goto-char upper-start
)
815 (re-search-forward smerge-begin-re end t
))
816 ;; There's a nested conflict and we're after the beginning
817 ;; of the outer one but before the beginning of the inner one.
818 ;; Of course, maybe this is not a nested conflict but in that
819 ;; case it can only be something nastier that we don't know how
820 ;; to handle, so may as well arbitrarily decide to treat it as
821 ;; a nested conflict. --Stef
822 (error "There is a nested conflict"))
824 ((re-search-backward smerge-base-re start t
)
825 ;; a 3-parts conflict
826 (set (make-local-variable 'smerge-conflict-style
) 'diff3-A
)
827 (setq base-end upper-end
)
828 (setq upper-end
(match-beginning 0))
829 (setq base-start
(match-end 0)))
831 ((string= filename
(file-name-nondirectory
832 (or buffer-file-name
"")))
833 ;; a 2-parts conflict
834 (set (make-local-variable 'smerge-conflict-style
) 'diff3-E
))
836 ((and (not base-start
)
837 (or (eq smerge-conflict-style
'diff3-A
)
838 (equal filename
"ANCESTOR")
839 (string-match "\\`[.0-9]+\\'" filename
)))
840 ;; a same-diff conflict
841 (setq base-start upper-start
)
842 (setq base-end upper-end
)
843 (setq upper-start lower-start
)
844 (setq upper-end lower-end
)))
846 (store-match-data (list start end
847 upper-start upper-end
849 lower-start lower-end
850 (when base-start
(1- base-start
)) base-start
851 (1- lower-start
) lower-start
))
853 (search-failed (user-error "Point not in conflict region")))))
855 (defun smerge-conflict-overlay (pos)
856 "Return the conflict overlay at POS if any."
857 (let ((ols (overlays-at pos
))
860 (if (and (eq (overlay-get ol
'smerge
) 'conflict
)
861 (> (overlay-end ol
) pos
))
865 (defun smerge-find-conflict (&optional limit
)
866 "Find and match a conflict region. Intended as a font-lock MATCHER.
867 The submatches are the same as in `smerge-match-conflict'.
868 Returns non-nil if a match is found between point and LIMIT.
869 Point is moved to the end of the conflict."
873 ;; First check to see if point is already inside a conflict, using
874 ;; the conflict overlays.
875 (while (and (not found
) (setq conflict
(smerge-conflict-overlay pos
)))
876 ;; Check the overlay's validity and kill it if it's out of date.
879 (goto-char (overlay-start conflict
))
880 (smerge-match-conflict)
881 (goto-char (match-end 0))
883 (error "Matching backward!")
885 (error (smerge-remove-props
886 (overlay-start conflict
) (overlay-end conflict
))
888 ;; If we're not already inside a conflict, look for the next conflict
889 ;; and add/update its overlay.
890 (while (and (not found
) (re-search-forward smerge-begin-re limit t
))
893 (smerge-match-conflict)
894 (goto-char (match-end 0))
895 (let ((conflict (smerge-conflict-overlay (1- (point)))))
897 ;; Update its location, just in case it got messed up.
898 (move-overlay conflict
(match-beginning 0) (match-end 0))
899 (setq conflict
(make-overlay (match-beginning 0) (match-end 0)
900 nil
'front-advance nil
))
901 (overlay-put conflict
'evaporate t
)
902 (overlay-put conflict
'smerge
'conflict
)
903 (let ((props smerge-text-properties
))
905 (overlay-put conflict
(pop props
) (pop props
))))))
910 ;;; Refined change highlighting
912 (defvar smerge-refine-forward-function
#'smerge--refine-forward
913 "Function used to determine an \"atomic\" element.
914 You can set it to `forward-char' to get char-level granularity.
915 Its behavior has mainly two restrictions:
916 - if this function encounters a newline, it's important that it stops right
918 This only matters if `smerge-refine-ignore-whitespace' is nil.
919 - it needs to be unaffected by changes performed by the `preproc' argument
920 to `smerge-refine-regions'.
921 This only matters if `smerge-refine-weight-hack' is nil.")
923 (defvar smerge-refine-ignore-whitespace t
924 "If non-nil, indicate that `smerge-refine' should try to ignore change in whitespace.")
926 (defvar smerge-refine-weight-hack t
927 "If non-nil, pass to diff as many lines as there are chars in the region.
928 I.e. each atomic element (e.g. word) will be copied as many times (on different
929 lines) as it has chars. This has two advantages:
930 - if `diff' tries to minimize the number *lines* (rather than chars)
931 added/removed, this adjust the weights so that adding/removing long
932 symbols is considered correspondingly more costly.
933 - `smerge-refine-forward-function' only needs to be called when chopping up
934 the regions, and `forward-char' can be used afterwards.
935 It has the following disadvantages:
936 - cannot use `diff -w' because the weighting causes added spaces in a line
937 to be represented as added copies of some line, so `diff -w' can't do the
938 right thing any more.
939 - Is a bit more costly (may in degenerate cases use temp files that are 10x
940 larger than the refined regions).")
942 (defun smerge--refine-forward (n)
943 (let ((case-fold-search nil
)
944 (re "[[:upper:]]?[[:lower:]]+\\|[[:upper:]]+\\|[[:digit:]]+\\|.\\|\n"))
945 (when (and smerge-refine-ignore-whitespace
946 ;; smerge-refine-weight-hack causes additional spaces to
947 ;; appear as additional lines as well, so even if diff ignores
948 ;; whitespace changes, it'll report added/removed lines :-(
949 (not smerge-refine-weight-hack
))
950 (setq re
(concat "[ \t]*\\(?:" re
"\\)")))
952 (unless (looking-at re
) (error "Smerge refine internal error"))
953 (goto-char (match-end 0)))))
955 (defvar smerge--refine-long-words
)
957 (defun smerge--refine-chopup-region (beg end file
&optional preproc
)
958 "Chopup the region into small elements, one per line.
959 Save the result into FILE.
960 If non-nil, PREPROC is called with no argument in a buffer that contains
961 a copy of the text, just before chopping it up. It can be used to replace
962 chars to try and eliminate some spurious differences."
963 ;; We used to chop up char-by-char rather than word-by-word like ediff
964 ;; does. It had the benefit of simplicity and very fine results, but it
965 ;; often suffered from problem that diff would find correlations where
966 ;; there aren't any, so the resulting "change" didn't make much sense.
967 ;; You can still get this behavior by setting
968 ;; `smerge-refine-forward-function' to `forward-char'.
970 (insert-buffer-substring (marker-buffer beg
) beg end
)
971 (when preproc
(goto-char (point-min)) (funcall preproc
))
972 (when smerge-refine-ignore-whitespace
973 ;; It doesn't make much of a difference for diff-fine-highlight
974 ;; because we still have the _/+/</>/! prefix anyway. Can still be
975 ;; useful in other circumstances.
976 (subst-char-in-region (point-min) (point-max) ?
\n ?\s
))
977 (goto-char (point-min))
980 (let ((start (point)))
981 (funcall smerge-refine-forward-function
1)
982 (let ((len (- (point) start
)))
983 (cl-assert (>= len
1))
984 ;; We add \n after each chunk except after \n, so we get
985 ;; one line per text chunk, where each line contains
986 ;; just one chunk, except for \n chars which are
987 ;; represented by the empty line.
988 (unless (bolp) (insert ?
\n))
989 (when (and smerge-refine-weight-hack
(> len
1))
990 (let ((s (buffer-substring-no-properties start
(point))))
991 ;; The weight-hack inserts N copies of words of size N,
992 ;; so it naturally suffers from an O(N²) blow up.
993 ;; To circumvent this, we map each long word
994 ;; to a shorter (but still unique) replacement.
995 ;; Another option would be to change smerge--refine-forward
996 ;; so it chops up long words into smaller ones.
998 (let ((short (gethash s smerge--refine-long-words
)))
1000 ;; To avoid accidental conflicts with ≤8 words,
1001 ;; we make sure the replacement is >8 chars. Overall,
1002 ;; this should bound the blowup factor to ~10x,
1003 ;; tho if those chars end up encoded as multiple bytes
1004 ;; each, it could probably still reach ~30x in
1005 ;; pathological cases.
1007 (concat (substring s
0 7)
1012 smerge--refine-long-words
)))
1014 (puthash s short smerge--refine-long-words
))
1015 (delete-region start
(point))
1018 (dotimes (_i (1- len
)) (insert s
)))))))
1019 (unless (bolp) (error "Smerge refine internal error"))
1020 (let ((coding-system-for-write 'utf-8-emacs-unix
))
1021 (write-region (point-min) (point-max) file nil
'nomessage
))))
1023 (defun smerge--refine-highlight-change (beg match-num1 match-num2 props
)
1024 ;; TODO: Add a property pointing to the corresponding text in the
1026 (with-current-buffer (marker-buffer beg
)
1028 (let* ((startline (- (string-to-number match-num1
) 1))
1029 (beg (progn (funcall (if smerge-refine-weight-hack
1031 smerge-refine-forward-function
)
1034 (end (progn (funcall (if smerge-refine-weight-hack
1036 smerge-refine-forward-function
)
1038 (- (string-to-number match-num2
)
1042 (when smerge-refine-ignore-whitespace
1043 (skip-chars-backward " \t\n" beg
) (setq end
(point))
1045 (skip-chars-forward " \t\n" end
) (setq beg
(point)))
1047 (let ((ol (make-overlay
1049 ;; Make them tend to shrink rather than spread when editing.
1050 'front-advance nil
)))
1051 (overlay-put ol
'evaporate t
)
1052 (dolist (x props
) (overlay-put ol
(car x
) (cdr x
)))
1056 (defun smerge-refine-regions (beg1 end1 beg2 end2 props-c
&optional preproc props-r props-a
)
1057 "Show fine differences in the two regions BEG1..END1 and BEG2..END2.
1058 PROPS-C is an alist of properties to put (via overlays) on the changes.
1059 PROPS-R is an alist of properties to put on removed characters.
1060 PROPS-A is an alist of properties to put on added characters.
1061 If PROPS-R and PROPS-A are nil, put PROPS-C on all changes.
1062 If PROPS-C is nil, but PROPS-R and PROPS-A are non-nil,
1063 put PROPS-A on added characters, PROPS-R on removed characters.
1064 If PROPS-C, PROPS-R and PROPS-A are non-nil, put PROPS-C on changed characters,
1065 PROPS-A on added characters, and PROPS-R on removed characters.
1067 If non-nil, PREPROC is called with no argument in a buffer that contains
1068 a copy of a region, just before preparing it to for `diff'. It can be
1069 used to replace chars to try and eliminate some spurious differences."
1070 (let* ((pos (point))
1071 deactivate-mark
; The code does not modify any visible buffer.
1072 (file1 (make-temp-file "diff1"))
1073 (file2 (make-temp-file "diff2"))
1074 (smerge--refine-long-words
1075 (if smerge-refine-weight-hack
(make-hash-table :test
#'equal
))))
1076 (unless (markerp beg1
) (setq beg1
(copy-marker beg1
)))
1077 (unless (markerp beg2
) (setq beg2
(copy-marker beg2
)))
1078 ;; Chop up regions into smaller elements and save into files.
1079 (smerge--refine-chopup-region beg1 end1 file1 preproc
)
1080 (smerge--refine-chopup-region beg2 end2 file2 preproc
)
1082 ;; Call diff on those files.
1085 ;; Allow decoding the EOL format, as on MS-Windows the Diff
1086 ;; utility might produce CR-LF EOLs.
1087 (let ((coding-system-for-read 'utf-8-emacs
))
1088 (call-process diff-command nil t nil
1089 (if (and smerge-refine-ignore-whitespace
1090 (not smerge-refine-weight-hack
))
1091 ;; Pass -a so diff treats it as a text file even
1092 ;; if it contains \0 and such.
1093 ;; Pass -d so as to get the smallest change, but
1094 ;; also and more importantly because otherwise it
1095 ;; may happen that diff doesn't behave like
1096 ;; smerge-refine-weight-hack expects it to.
1097 ;; See https://lists.gnu.org/r/emacs-devel/2007-11/msg00401.html
1100 ;; Process diff's output.
1101 (goto-char (point-min))
1105 (if (not (looking-at "\\([0-9]+\\)\\(?:,\\([0-9]+\\)\\)?\\([acd]\\)\\([0-9]+\\)\\(?:,\\([0-9]+\\)\\)?$"))
1106 (error "Unexpected patch hunk header: %s"
1107 (buffer-substring (point) (line-end-position))))
1108 (let ((op (char-after (match-beginning 3)))
1109 (m1 (match-string 1))
1110 (m2 (match-string 2))
1111 (m4 (match-string 4))
1112 (m5 (match-string 5)))
1113 (when (memq op
'(?d ?c
))
1115 (smerge--refine-highlight-change
1117 ;; Try to use props-c only for changed chars,
1118 ;; fallback to props-r for changed/removed chars,
1119 ;; but if props-r is nil then fallback to props-c.
1120 (or (and (eq op
'?c
) props-c
) props-r props-c
))))
1121 (when (memq op
'(?a ?c
))
1123 (smerge--refine-highlight-change
1125 ;; Same logic as for removed chars above.
1126 (or (and (eq op
'?c
) props-c
) props-a props-c
)))))
1127 (forward-line 1) ;Skip hunk header.
1128 (and (re-search-forward "^[0-9]" nil
'move
) ;Skip hunk body.
1129 (goto-char (match-beginning 0))))
1130 ;; (cl-assert (or (null last1) (< (overlay-start last1) end1)))
1131 ;; (cl-assert (or (null last2) (< (overlay-start last2) end2)))
1132 (if smerge-refine-weight-hack
1134 ;; (cl-assert (or (null last1) (<= (overlay-end last1) end1)))
1135 ;; (cl-assert (or (null last2) (<= (overlay-end last2) end2)))
1137 ;; smerge-refine-forward-function when calling in chopup may
1138 ;; have stopped because it bumped into EOB whereas in
1139 ;; smerge-refine-weight-hack it may go a bit further.
1140 (if (and last1
(> (overlay-end last1
) end1
))
1141 (move-overlay last1
(overlay-start last1
) end1
))
1142 (if (and last2
(> (overlay-end last2
) end2
))
1143 (move-overlay last2
(overlay-start last2
) end2
))
1147 (delete-file file2
))))
1148 (define-obsolete-function-alias 'smerge-refine-subst
1149 #'smerge-refine-regions
"26.1")
1151 (defun smerge-refine (&optional part
)
1152 "Highlight the words of the conflict that are different.
1153 For 3-way conflicts, highlights only two of the three parts.
1154 A numeric argument PART can be used to specify which two parts;
1155 repeating the command will highlight other two parts."
1157 (if (integerp current-prefix-arg
) (list current-prefix-arg
)
1158 (smerge-match-conflict)
1159 (let* ((prop (get-text-property (match-beginning 0) 'smerge-refine-part
))
1160 (part (if (and (consp prop
)
1161 (eq (buffer-chars-modified-tick) (car prop
)))
1163 ;; If already highlighted, cycle.
1164 (list (if (integerp part
) (1+ (mod part
3)))))))
1166 (if (and (integerp part
) (or (< part
1) (> part
3)))
1167 (error "No conflict part nb %s" part
))
1168 (smerge-match-conflict)
1169 (remove-overlays (match-beginning 0) (match-end 0) 'smerge
'refine
)
1170 ;; Ignore `part' if not applicable, and default it if not provided.
1171 (setq part
(cond ((null (match-end 2)) 2)
1172 ((eq (match-end 1) (match-end 3)) 1)
1173 ((integerp part
) part
)
1174 ;; If one of the parts is empty, any refinement using
1175 ;; it will be trivial and uninteresting.
1176 ((eq (match-end 1) (match-beginning 1)) 1)
1177 ((eq (match-end 3) (match-beginning 3)) 3)
1179 (let ((n1 (if (eq part
1) 2 1))
1180 (n2 (if (eq part
3) 2 3))
1181 (smerge-use-changed-face
1182 (and (face-differs-from-default-p 'smerge-refined-change
)
1183 (not (face-equal 'smerge-refined-change
'smerge-refined-added
))
1184 (not (face-equal 'smerge-refined-change
'smerge-refined-removed
)))))
1185 (smerge-ensure-match n1
)
1186 (smerge-ensure-match n2
)
1187 (with-silent-modifications
1188 (put-text-property (match-beginning 0) (1+ (match-beginning 0))
1190 (cons (buffer-chars-modified-tick) part
)))
1191 (smerge-refine-regions (match-beginning n1
) (match-end n1
)
1192 (match-beginning n2
) (match-end n2
)
1193 (if smerge-use-changed-face
1194 '((smerge . refine
) (font-lock-face . smerge-refined-change
)))
1196 (unless smerge-use-changed-face
1197 '((smerge . refine
) (font-lock-face . smerge-refined-removed
)))
1198 (unless smerge-use-changed-face
1199 '((smerge . refine
) (font-lock-face . smerge-refined-added
))))))
1201 (defun smerge-swap ()
1202 "Swap the \"Upper\" and the \"Lower\" chunks.
1203 Can be used before things like `smerge-keep-all' or `smerge-resolve' where the
1204 ordering can have some subtle influence on the result, such as preferring the
1205 spacing of the \"Lower\" chunk."
1207 (smerge-match-conflict)
1208 (goto-char (match-beginning 3))
1209 (let ((txt3 (delete-and-extract-region (point) (match-end 3))))
1210 (insert (delete-and-extract-region (match-beginning 1) (match-end 1)))
1211 (goto-char (match-beginning 1))
1214 (defun smerge-diff (n1 n2
)
1215 (smerge-match-conflict)
1216 (smerge-ensure-match n1
)
1217 (smerge-ensure-match n2
)
1218 (let ((name1 (aref smerge-match-names n1
))
1219 (name2 (aref smerge-match-names n2
))
1220 ;; Read them before the match-data gets clobbered.
1221 (beg1 (match-beginning n1
))
1222 (end1 (match-end n1
))
1223 (beg2 (match-beginning n2
))
1224 (end2 (match-end n2
))
1225 (file1 (make-temp-file "smerge1"))
1226 (file2 (make-temp-file "smerge2"))
1227 (dir default-directory
)
1228 (file (if buffer-file-name
(file-relative-name buffer-file-name
)))
1229 ;; We would want to use `emacs-mule-unix' for read&write, but we
1230 ;; bump into problems with the coding-system used by diff to write
1231 ;; the file names and the time stamps in the header.
1232 ;; `buffer-file-coding-system' is not always correct either, but if
1233 ;; the OS/user uses only one coding-system, then it works.
1234 (coding-system-for-read buffer-file-coding-system
))
1235 (write-region beg1 end1 file1 nil
'nomessage
)
1236 (write-region beg2 end2 file2 nil
'nomessage
)
1238 (with-current-buffer (get-buffer-create smerge-diff-buffer-name
)
1239 (setq default-directory dir
)
1240 (let ((inhibit-read-only t
))
1243 (apply 'call-process diff-command nil t nil
1244 (append smerge-diff-switches
1245 (list "-L" (concat name1
"/" file
)
1246 "-L" (concat name2
"/" file
)
1248 (if (eq status
0) (insert "No differences found.\n"))))
1249 (goto-char (point-min))
1251 (display-buffer (current-buffer) t
))
1253 (delete-file file2
))))
1255 ;; compiler pacifiers
1256 (defvar smerge-ediff-windows
)
1257 (defvar smerge-ediff-buf
)
1258 (defvar ediff-buffer-A
)
1259 (defvar ediff-buffer-B
)
1260 (defvar ediff-buffer-C
)
1261 (defvar ediff-ancestor-buffer
)
1262 (defvar ediff-quit-hook
)
1263 (declare-function ediff-cleanup-mess
"ediff-util" nil
)
1265 (defun smerge--get-marker (regexp default
)
1267 (goto-char (point-min))
1268 (if (and (search-forward-regexp regexp nil t
)
1269 (> (match-end 1) (match-beginning 1)))
1270 (concat default
"=" (match-string-no-properties 1))
1274 (defun smerge-ediff (&optional name-upper name-lower name-base
)
1275 "Invoke ediff to resolve the conflicts.
1276 NAME-UPPER, NAME-LOWER, and NAME-BASE, if non-nil, are used for the
1279 (let* ((buf (current-buffer))
1281 ;;(ediff-default-variant 'default-B)
1282 (config (current-window-configuration))
1283 (filename (file-name-nondirectory (or buffer-file-name
"-")))
1284 (upper (generate-new-buffer
1286 (concat "*" filename
" "
1287 (smerge--get-marker smerge-begin-re
"UPPER")
1289 (lower (generate-new-buffer
1291 (concat "*" filename
" "
1292 (smerge--get-marker smerge-end-re
"LOWER")
1295 (with-current-buffer upper
1296 (buffer-disable-undo)
1297 (insert-buffer-substring buf
)
1298 (goto-char (point-min))
1299 (while (smerge-find-conflict)
1300 (when (match-beginning 2) (setq base t
))
1302 (buffer-enable-undo)
1303 (set-buffer-modified-p nil
)
1306 (with-current-buffer lower
1307 (buffer-disable-undo)
1308 (insert-buffer-substring buf
)
1309 (goto-char (point-min))
1310 (while (smerge-find-conflict)
1312 (buffer-enable-undo)
1313 (set-buffer-modified-p nil
)
1317 (setq base
(generate-new-buffer
1319 (concat "*" filename
" "
1320 (smerge--get-marker smerge-base-re
"BASE")
1322 (with-current-buffer base
1323 (buffer-disable-undo)
1324 (insert-buffer-substring buf
)
1325 (goto-char (point-min))
1326 (while (smerge-find-conflict)
1329 (delete-region (match-beginning 0) (match-end 0))))
1330 (buffer-enable-undo)
1331 (set-buffer-modified-p nil
)
1334 ;; the rest of the code is inspired from vc.el
1338 (ediff-merge-buffers-with-ancestor upper lower base
)
1339 ;; nil 'ediff-merge-revisions-with-ancestor buffer-file-name)
1340 (ediff-merge-buffers upper lower
)))
1341 ;; nil 'ediff-merge-revisions buffer-file-name)))
1343 ;; Ediff is now set up, and we are in the control buffer.
1344 ;; Do a few further adjustments and take precautions for exit.
1345 (set (make-local-variable 'smerge-ediff-windows
) config
)
1346 (set (make-local-variable 'smerge-ediff-buf
) buf
)
1347 (set (make-local-variable 'ediff-quit-hook
)
1349 (let ((buffer-A ediff-buffer-A
)
1350 (buffer-B ediff-buffer-B
)
1351 (buffer-C ediff-buffer-C
)
1352 (buffer-Ancestor ediff-ancestor-buffer
)
1353 (buf smerge-ediff-buf
)
1354 (windows smerge-ediff-windows
))
1355 (ediff-cleanup-mess)
1356 (with-current-buffer buf
1358 (insert-buffer-substring buffer-C
)
1359 (kill-buffer buffer-A
)
1360 (kill-buffer buffer-B
)
1361 (kill-buffer buffer-C
)
1362 (when (bufferp buffer-Ancestor
) (kill-buffer buffer-Ancestor
))
1363 (set-window-configuration windows
)
1364 (message "Conflict resolution finished; you may save the buffer")))))
1365 (message "Please resolve conflicts now; exit ediff when done")))
1367 (defun smerge-makeup-conflict (pt1 pt2 pt3
&optional pt4
)
1368 "Insert diff3 markers to make a new conflict.
1369 Uses point and mark for two of the relevant positions and previous marks
1371 By default, makes up a 2-way conflict,
1372 with a \\[universal-argument] prefix, makes up a 3-way conflict."
1376 (progn (pop-mark) (mark))
1377 (when current-prefix-arg
(pop-mark) (mark))))
1378 ;; Start from the end so as to avoid problems with pos-changes.
1379 (pcase-let ((`(,pt1
,pt2
,pt3
,pt4
)
1380 (sort `(,pt1
,pt2
,pt3
,@(if pt4
(list pt4
))) '>=)))
1381 (goto-char pt1
) (beginning-of-line)
1382 (insert ">>>>>>> LOWER\n")
1383 (goto-char pt2
) (beginning-of-line)
1384 (insert "=======\n")
1385 (goto-char pt3
) (beginning-of-line)
1387 (insert "||||||| BASE\n")
1388 (goto-char pt4
) (beginning-of-line))
1389 (insert "<<<<<<< UPPER\n"))
1390 (if smerge-mode nil
(smerge-mode 1))
1394 (defconst smerge-parsep-re
1395 (concat smerge-begin-re
"\\|" smerge-end-re
"\\|"
1396 smerge-base-re
"\\|" smerge-lower-re
"\\|"))
1399 (define-minor-mode smerge-mode
1400 "Minor mode to simplify editing output from the diff3 program.
1401 With a prefix argument ARG, enable the mode if ARG is positive,
1402 and disable it otherwise. If called from Lisp, enable the mode
1403 if ARG is omitted or nil.
1404 \\{smerge-mode-map}"
1405 :group
'smerge
:lighter
" SMerge"
1406 (when (and (boundp 'font-lock-mode
) font-lock-mode
)
1409 (font-lock-add-keywords nil smerge-font-lock-keywords
'append
)
1410 (font-lock-remove-keywords nil smerge-font-lock-keywords
))
1411 (goto-char (point-min))
1412 (while (smerge-find-conflict)
1414 (font-lock-fontify-region (match-beginning 0) (match-end 0) nil
)))))
1415 (if (string-match (regexp-quote smerge-parsep-re
) paragraph-separate
)
1417 (set (make-local-variable 'paragraph-separate
)
1418 (replace-match "" t t paragraph-separate
)))
1420 (set (make-local-variable 'paragraph-separate
)
1421 (concat smerge-parsep-re paragraph-separate
))))
1423 (smerge-remove-props (point-min) (point-max))))
1426 (defun smerge-start-session ()
1427 "Turn on `smerge-mode' and move point to first conflict marker.
1428 If no conflict maker is found, turn off `smerge-mode'."
1432 (unless (looking-at smerge-begin-re
)
1434 (error (smerge-auto-leave))))
1436 (provide 'smerge-mode
)
1438 ;;; smerge-mode.el ends here