1

I am using next-conflict to detect is there any git conflict, using following answer: Jump to next merge conflict in project using Magit and Smerge. I experience that if I am at the end of the file/buffer and if there is a conflict in the same buffer/file, the conflict is not found. But if I switch into another buffer, or go to top of the buffer it iss found.

Is there any workaround to fix this issue?


setup:

(defun smerge-try-smerge ()
  (save-excursion
    (goto-char (point-min))
    (when (re-search-forward "^<<<<<<< " nil t)
      (require 'smerge-mode)
      (smerge-mode 1))))
(add-hook 'find-file-hook 'smerge-try-smerge t)
(add-hook 'after-revert-hook 'smerge-try-smerge t)

(defun smerge-next-safe ()
    "returns t on success, nil otherwise"
  (condition-case err
      (not (smerge-next))
    ('error
     nil)))

(require 'vc)
(defun next-conflict ()  ;; key-binding
  (interactive)
  (let ((buffer (current-buffer)))
    (when (not (smerge-next-safe))
      (vc-find-conflicted-file)
      (if (eq buffer (current-buffer))
          (message "No conflicts found")
        (goto-char 0)
        (smerge-next-safe)))))

1 Answer 1

2

Good idea. Here's a next-conflict function that wraps around whenever there is no more conflicted files, but the current one still has conflicts:

(defun next-conflict ()
  (interactive)
  (let ((buffer (current-buffer)))
    (when (not (smerge-next-safe))
      (vc-find-conflicted-file)
      (when (eq buffer (current-buffer))
        (let ((prev-pos (point)))
          (goto-char (point-min))
          (unless (smerge-next-safe)
            (goto-char prev-pos)
            (message "No conflicts found")))))))

I modified the answer you are referring to as well (it was mine answer) as such behavior is more intuitive. And changed behavior accordingly of the built-in emacs function with similar functional.

Might be worth noting that I looked at how smerge-next is defined, and it is defined with easy-mmode-define-navigation macro. As far as I look through the macro there's no way to make smerge-next wrap around the search (e.g. by changing some variable or something like that), so I open-coded the wrapping higher the stack.

14
  • Thanks for the update! Should I update the changes from this questions or from original answer?
    – alper
    Commented Mar 3, 2021 at 22:53
  • 1
    @alper from the original answer, there was an additional fix that I hesitate to include here, since this question is specific to "how to modify the code from question to resolve the question".
    – Hi-Angel
    Commented Mar 3, 2021 at 22:59
  • I am not sure why but this won't detect conflicts on different files if exist
    – alper
    Commented Mar 7, 2021 at 12:46
  • @alper me neither 😄 I am not sure what you're referring to.
    – Hi-Angel
    Commented Mar 7, 2021 at 12:49
  • Sorry for keep bringing this up , I have tried: gist.github.com/avatar-lavventura/… which does not detect any conflict if I am at the end of file, does it work on your end?
    – alper
    Commented Mar 25, 2021 at 21:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.