+ (let ((mb2 (or (match-beginning 2) (point-max)))
+ (me2 (or (match-end 2) (point-min))))
+ (delete-region (match-end 3) (match-end 0))
+ (delete-region (max me2 (match-end 1)) (match-beginning 3))
+ (if (and (match-end 2) (/= (match-end 1) (match-end 3)))
+ (delete-region (match-end 1) (match-beginning 2)))
+ (delete-region (match-beginning 0) (min (match-beginning 1) mb2))
+ (smerge-auto-leave)))
+
+(defun smerge-keep-n (n)
+ ;; We used to use replace-match, but that did not preserve markers so well.
+ (delete-region (match-end n) (match-end 0))
+ (delete-region (match-beginning 0) (match-beginning n)))
+
+(defun smerge-combine-with-next ()
+ "Combine the current conflict with the next one."
+ (interactive)
+ (smerge-match-conflict)
+ (let ((ends nil))
+ (dolist (i '(3 2 1 0))
+ (push (if (match-end i) (copy-marker (match-end i) t)) ends))
+ (setq ends (apply 'vector ends))
+ (goto-char (aref ends 0))
+ (if (not (re-search-forward smerge-begin-re nil t))
+ (error "No next conflict")
+ (smerge-match-conflict)
+ (let ((match-data (mapcar (lambda (m) (if m (copy-marker m)))
+ (match-data))))
+ ;; First copy the in-between text in each alternative.
+ (dolist (i '(1 2 3))
+ (when (aref ends i)
+ (goto-char (aref ends i))
+ (insert-buffer-substring (current-buffer)
+ (aref ends 0) (car match-data))))
+ (delete-region (aref ends 0) (car match-data))
+ ;; Then move the second conflict's alternatives into the first.
+ (dolist (i '(1 2 3))
+ (set-match-data match-data)
+ (when (and (aref ends i) (match-end i))
+ (goto-char (aref ends i))
+ (insert-buffer-substring (current-buffer)
+ (match-beginning i) (match-end i))))
+ (delete-region (car match-data) (cadr match-data))
+ ;; Free the markers.
+ (dolist (m match-data) (if m (move-marker m nil)))
+ (mapc (lambda (m) (if m (move-marker m nil))) ends)))))
+
+(defvar smerge-resolve-function
+ (lambda () (error "Don't know how to resolve"))
+ "Mode-specific merge function.
+The function is called with no argument and with the match data set
+according to `smerge-match-conflict'.")
+
+(defvar smerge-text-properties
+ `(help-echo "merge conflict: mouse-3 shows a menu"
+ ;; mouse-face highlight
+ keymap (keymap (down-mouse-3 . smerge-popup-context-menu))))
+
+(defun smerge-remove-props (&optional beg end)
+ (remove-text-properties
+ (or beg (match-beginning 0))
+ (or end (match-end 0))
+ smerge-text-properties))
+
+(defun smerge-popup-context-menu (event)
+ "Pop up the Smerge mode context menu under mouse."
+ (interactive "e")
+ (if (and smerge-mode
+ (save-excursion (posn-set-point (event-end event)) (smerge-check 1)))
+ (progn
+ (posn-set-point (event-end event))
+ (smerge-match-conflict)
+ (let ((i (smerge-get-current))
+ o)
+ (if (<= i 0)
+ ;; Out of range
+ (popup-menu smerge-mode-menu)
+ ;; Install overlay.
+ (setq o (make-overlay (match-beginning i) (match-end i)))
+ (unwind-protect
+ (progn
+ (overlay-put o 'face 'highlight)
+ (sit-for 0) ;Display the new highlighting.
+ (popup-menu smerge-context-menu))
+ ;; Delete overlay.
+ (delete-overlay o)))))
+ ;; There's no conflict at point, the text-props are just obsolete.
+ (save-excursion
+ (let ((beg (re-search-backward smerge-end-re nil t))
+ (end (re-search-forward smerge-begin-re nil t)))
+ (smerge-remove-props (or beg (point-min)) (or end (point-max)))
+ (push event unread-command-events)))))
+
+(defun smerge-resolve ()
+ "Resolve the conflict at point intelligently.
+This relies on mode-specific knowledge and thus only works in
+some major modes. Uses `smerge-resolve-function' to do the actual work."
+ (interactive)
+ (smerge-match-conflict)
+ (smerge-remove-props)
+ (cond
+ ;; Trivial diff3 -A non-conflicts.
+ ((and (eq (match-end 1) (match-end 3))
+ (eq (match-beginning 1) (match-beginning 3)))
+ ;; FIXME: Add "if [ diff -b MINE OTHER ]; then select OTHER; fi"
+ (smerge-keep-n 3))
+ ((and (match-end 2)
+ ;; FIXME: Add "diff -b BASE MINE | patch OTHER".
+ ;; FIXME: Add "diff -b BASE OTHER | patch MINE".
+ nil)
+ )
+ ((and (not (match-end 2))
+ ;; FIXME: Add "diff -b"-based refinement.
+ nil)
+ )
+ (t
+ ;; Mode-specific conflict resolution.
+ (funcall smerge-resolve-function)))