Skip to content

fix: Prevent overlay duplication by making overlay list permanent-local - #5

Merged
jcs090218 merged 1 commit into
emacs-vs:masterfrom
saulotoledo:fix/dup-overlays-on-refresh
Apr 14, 2026
Merged

fix: Prevent overlay duplication by making overlay list permanent-local#5
jcs090218 merged 1 commit into
emacs-vs:masterfrom
saulotoledo:fix/dup-overlays-on-refresh

Conversation

@saulotoledo

Copy link
Copy Markdown
Contributor

The fix removes the internal overlay tracking variable and instead uses remove-overlays with the 'cognitive-complexity property to reliably clean up all overlays, ensuring no duplicates remain after buffer state changes.

Fixes #4.

@saulotoledo
saulotoledo force-pushed the fix/dup-overlays-on-refresh branch from 39e8825 to 0d38ede Compare April 2, 2026 13:57
@saulotoledo saulotoledo changed the title fix: Prevent overlay duplication by removing orphaned overlays directly fix: Prevent overlay duplication by making overlay list permanent-local Apr 2, 2026
@saulotoledo

Copy link
Copy Markdown
Contributor Author

@jcs090218

@jcs090218

Copy link
Copy Markdown
Member

I thought you originally used remove-overlays? Why the current changes? 😮

@saulotoledo

saulotoledo commented Apr 3, 2026

Copy link
Copy Markdown
Contributor Author

Hi @jcs090218,

Keeping track of the overlays might be slightly more performant, because we know exactly what to remove, but breaks if you modify the variable manually in a different way. The original fix works in any case because there is no variable, thus it is more reliable. I pushed the change because I thought you would prefer tracking it. But I can revert it quickly and push it for you. Just let me know which one do you prefer.

@saulotoledo

Copy link
Copy Markdown
Contributor Author

Hi @jcs090218. A quick reminder on this. Let me know how to proceed here. :)

Comment thread cognitive-complexity.el

(defvar-local cognitive-complexity--ovs nil
"List of overlays.")
(put 'cognitive-complexity--ovs 'permanent-local t)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm no very familiar to the property permanent-local. How does it work? 🤔 I thought defvar-local would do everything for us.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Basically, defvar-local makes a variable unique to a buffer, but it doesn't make it "permanent". Every time you switch modes or revert a buffer (M-x revert-buffer), Emacs does a "clean sweep" and deletes those local variables so old settings do not leak into new modes. The issue here is that while the variable disappears, the overlays stay stuck to the text. The variable just has references to the overlays, and only this reference is deleted, not the overlays themselves. When the minor mode starts back up, it sees a blank variable, thinks "there are no overlays yet", and creates a whole new set right on top of the old ones. If you refresh the buffer multiple times, you end up with multiple overlays for the same thing.

That is where the permanent-local comes in: it tells Emacs, "do not delete this specific variable during the cleanup". That way, the variable survives the refresh, the minor mode sees the overlays are already there, and it won't keep stacking new ones every time.

@jcs090218
jcs090218 merged commit b45afe9 into emacs-vs:master Apr 14, 2026
0 of 9 checks passed
@jcs090218

Copy link
Copy Markdown
Member

Thank you for fixing this! :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Duplicate overlays on buffer refresh

2 participants