Skip to content

Add support for indirect buffers#276

Open
jamescherti wants to merge 3 commits into
dgutov:masterfrom
jamescherti:fix-indirect-buffers
Open

Add support for indirect buffers#276
jamescherti wants to merge 3 commits into
dgutov:masterfrom
jamescherti:fix-indirect-buffers

Conversation

@jamescherti
Copy link
Copy Markdown

@jamescherti jamescherti commented Apr 29, 2026

Here is a first attempt to fix the indirect buffer issue: #272 (comment)

I replaced all instances of buffer-file-name with a function that supports indirect buffers:

  • Added the diff-hl--buffer-file-name function to retrieve the file name of the current buffer or its base buffer.
  • Replaced direct accesses of the buffer-file-name variable with the new helper changes to all live buffers sharing the same base buffer.

It successfully returns the correct file name. However, it does not fix the issue of the margin and fringe not updating. An additional change is required to fix #272 (comment) .

Let us collaborate to address the issue, given your familiarity with the diff-hl codebase and your ability to assist or contribute to fixing it once and for all.

@jamescherti jamescherti force-pushed the fix-indirect-buffers branch 2 times, most recently from 8c6969d to 6915bc4 Compare April 29, 2026 13:03
- Add diff-hl--buffer-file-name helper to retrieve the file name of the
  current buffer or its base buffer.
- Replace direct accesses of the buffer-file-name variable with the new helper
  changes to all live buffers sharing the same base buffer.
@jamescherti jamescherti force-pushed the fix-indirect-buffers branch from 6915bc4 to 1256073 Compare April 29, 2026 13:04
@dgutov
Copy link
Copy Markdown
Owner

dgutov commented Apr 29, 2026

Try removing the (with-current-buffer (or (buffer-base-buffer) (current-buffer)) wrappers too, there are 4 of them.

@jamescherti
Copy link
Copy Markdown
Author

jamescherti commented Apr 29, 2026

I replaced (with-current-buffer (or (buffer-base-buffer) (current-buffer)) with (current-buffer) in the latest commit, but it doesn't seem to fix the issue.

…ffers

This commit makes diff-hl--update identify the base buffer of the current file
and iterate through all live buffers to apply diff highlighting to any indirect
clones.
@jamescherti jamescherti force-pushed the fix-indirect-buffers branch from efd30f4 to 34cb601 Compare April 29, 2026 14:55
@jamescherti
Copy link
Copy Markdown
Author

jamescherti commented Apr 29, 2026

I found a way to fix it. Try the latest commit.

I updated diff-hl--update to synchronize overlays across indirect buffers. The latest commit makes diff-hl--update identify the base buffer of the current file and iterate through all live buffers to apply diff highlighting to any indirect clones.

@jamescherti
Copy link
Copy Markdown
Author

jamescherti commented Apr 29, 2026

We could optimize this by making diff-hl track indirect buffers to avoid using buffer-list. Please read and test the latest commit and send me your feedback.

@dgutov
Copy link
Copy Markdown
Owner

dgutov commented Apr 29, 2026

but it doesn't seem to fix the issue

When you said "the issue", did you mean that the current indirect buffer was not updated after editing and saving? Or that some other related buffers were not updated.

@jamescherti
Copy link
Copy Markdown
Author

jamescherti commented Apr 29, 2026

The issue: diff-hl-mode fails to display or update version control indicators within indirect buffers when the base buffer is modified, or within base buffers when the indirect buffer is modified.

Applying this patch ensures that diff-hl highlights correctly synchronize and render across both the base buffer and any associated indirect buffers whenever file changes are saved.

@dgutov
Copy link
Copy Markdown
Owner

dgutov commented Apr 29, 2026

I'm asking specifically about the last commit. It seems to be more in the "nice to have" category. Iterating along the whole buffer list (or managing the indirect buffers list) has a cost as well. So how about we clarify that first.

@jamescherti
Copy link
Copy Markdown
Author

I can optimize this by storing a list of indirect buffers in a local variable in the base buffer each time an indirect buffer with diff-hl-mode enabled is created or when diff-hl-mode is started. This way, instead of looping over all buffers, the function will only loop over the indirect buffers and the base buffer.

Please confirm that:

  1. You have tested it and it works for you too.
  2. You are interested in merging this pull request if I implement it.

This way, I will not spend time on it unnecessarily.

@dgutov
Copy link
Copy Markdown
Owner

dgutov commented Apr 30, 2026

Could we get the answer to my last question first?

If the last change is less critical, we could split the improvement this way:

  • Simpler change that only updates the current buffer (whether it's indirect or base).
  • New hook that allows you to set up the update of related buffers.

Or something like that, I haven't decided yet. You don't need to polish the implementation further yet, but testing the alternatives and describing the results (and possible downsides) is what's valuable here.

@jamescherti
Copy link
Copy Markdown
Author

Hi Dmitry,

I am currently using this implementation with many indirect buffers and it works well. This pull request can be merged as is after more testing. (The buffer-list loop is fast because it skips buffers using (eq (buffer-base-buffer buf) base), and as I mentioned, it can easily be optimized in the future.)

To answer your questions: the last change is not just an optional enhancement. It is required because without it, the highlighting does not work at all in indirect buffers.

While my initial commits fixed the file name resolution, the margin and fringe still failed to update or display any indicators in the indirect buffer. The logic introduced in the last commit is what actually ensures the visual elements to render in the indirect buffers/clones.

Splitting the improvement into a simpler change for the active buffer and a separate hook for related buffers will not resolve the issue.

Just try it with and without the latest commit. You'll see the difference.

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.

Does this work with indirect buffers?

2 participants