origami.el icon indicating copy to clipboard operation
origami.el copied to clipboard

query-replace fails with origami

Open cigumo opened this issue 7 years ago • 3 comments

when using query-replace in a buffer that's partially folded with origami.el, I get the following error after the first successful replace: replace-match-maybe-edit: Match data clobbered by buffer modification hooks

I'm using Emacs 25.1 and Origami 20170129.805 from elpa. The problem happened using (origami-markers-parser "{{{" "}}}") as my parser.

Thanks for your help.

cigumo avatar Jun 28 '17 23:06 cigumo

This issue also happens to me using Emacs 24.5.1 and Origami 20170129.805 for single brackets

gvilanova avatar Dec 04 '17 09:12 gvilanova

Happens to me too using Emacs 26.1 (build 1, x86_64-w64-mingw32). Interesting, when changing text throughout the buffer, text BEFORE the fold marker "{{{" is changed, but not after; so, with 10 occurrences of 'TEXTA' total and 2 before "{{{", the first two are changed and then the match is clobbered. I note that if I narrow the buffer to text between the fold markers, there is no error, but as long as the "{{{" exists in the buffer to be parsed, the error occurs.

Hopefully the debugger text will assist.

Debugger entered--Lisp error: (error "Match data clobbered by buffer modification hooks") replace-match("TEXTB" t t) replace-match-maybe-edit("TEXTB" t t nil (520 527 #) nil) perform-replace("TEXTA" "TEXTB" t nil nil nil nil nil nil nil nil) query-replace("TEXTA" "TEXTB" nil nil nil nil nil) funcall-interactively(query-replace "TEXTA" "TEXTB" nil nil nil nil nil) call-interactively(query-replace nil nil) command-execute(query-replace)

[EDIT] I have found that upon visiting a file, if I toggle origami-mode off then back on, query-replace works again. Weird...and I cannot explain it. Of course, if I forget to toggle first, I will get the error, but then can toggle off and resubmit the query-replace to successful completion. Simple matter to create a Hydra for this. It's a kludgy work-around--but it works!

a-hausmann avatar May 22 '19 16:05 a-hausmann

I found a reliable reproduction scenario, and a fix that works for me.

Reproduction scenario:

  1. Visit a C++ file that contains a syntax error. (Presumably the programming language is not important.)
  2. Do revert-buffer. This is a workaround for the issue, so we get to a state where query-replace works.
  3. Do a query-replace that replaces multiple occurrences. This succeeds.
  4. Undo the replacement.
  5. Do compile. The syntax error is reported.
  6. Do next-error to move to the (first) syntax error.
  7. Do a query-replace that replaces multiple occurrences. Only the first occurrence is replaced, and the error message "Match data clobbered by buffer modification hooks" is generated. This reproduces the problem.

The fix that works for me is to edit function origami-header-overlay-range in origami.el by wrapping the body of the save-excursion call in a save-match-data call, yielding

       (save-excursion
         (save-match-data
           (goto-char (overlay-end fold-overlay))
           (when (looking-at ".")
             (forward-char 1)
             (when (looking-at "\n")
               (forward-char 1)))
           (point))

I've created pull request #93 for this.

LouisStrous avatar Apr 17 '20 06:04 LouisStrous