5

I know this was asked in other contexts already, but I couldn't find anything related to C++ development with my hooks that could help me. I am working a lot with replace and query-replace and query-replace-regexp and I love it.

Now I have the very annoying problem that after a while I am working in a buffer I suddenly get the error mentioned in the title after the first replace.

This happens not always but after a while it suddenly appears. Reopening the buffer fixes it for a while but is not a solution.

In other answers the before-changes-functions and the after-change-functions are mentioned. Because the error occurs after the first replace, I would assume it should be located in the after-change-functions hook. The value for this in the buffer is:

(c-after-change flycheck-handle-change flymake-after-change-function lsp-on-change jit-lock-after-change t)

I failed to find out what is going on there, so any help on that is highly appreciated.

EDIT: Thanks to Alex Karbivnichiy for the answer. I like your idea of a custom replace.

It inspired me to do some more testing with before-change-functions and after-change-functions without success. I had a buffer with the clobbered problem and started to disable minor modes. Nothing helped. I then set both change functions to nil and even this did not solve the problem.

I killed and reopened the buffer and replace is working as expected again.

Am I missing any other variable or function that can cause this problem.

1 Answers1

1

lsp-on-change probably connects to a server(Language Server Protocol) on each change:

https://github.com/emacs-lsp/lsp-mode/blob/9b5511dbf187348d00be8815d5b7533732f164d6/lsp-mode.el#L1658

c-after-change doc in comments, excerpt:

We can sometimes get two consecutive calls to after-change-functions without an intervening call to before-change-functions when reverting the buffer (see bug #24094). Whatever the cause, assume that the entire buffer has changed.

So due to a lot of unknown runtime context it can be fragile.

A solution to investigate or enhance would be creation of custom wrappers around replace functions, disabling after-change-functions during those function execution.
In c-mode hook:

(defun my-query-replace-regexp ()
  (interactive)
  (let ((after-change-functions nil)) ; empty when executing this func
    (call-interactively 'query-replace-regexp)))

(define-key c-mode-map (kbd "C-M-%") 'my-query-replace-regexp)
  • Thank you very much and sorry for the late reaction. I know this thread is quite old now. Just saw it right now and it is fair enough to accept your great answer. – AltruisticDelay Mar 30 '22 at 07:33