orbitalquark / textadept

Textadept is a fast, minimalist, and remarkably extensible cross-platform text editor for programmers.
https://orbitalquark.github.io/textadept
MIT License
652 stars 38 forks source link

Closing a non-active buffer via the tab button switches the active buffer when it shouldn't. #553

Closed jxzwp closed 2 months ago

jxzwp commented 2 months ago

Open Textadept. Open multiple files, at least three, so there are multiple tabs. Switch from the current tab to another one, remembering which tab was previously active. Click the X close button on a tab that isn't the active tab and isn't the previously active tab. The active tab/buffer is switched to another one, when it should remain active.

The problem is in the BUFFER_DELETED event handler at textadept/core/ui.lua:387 https://github.com/orbitalquark/textadept/blob/df12db04cbd28126e47ebe82b7c7e54b501eae48/core/ui.lua#L387

It implicitly assumes that the buffer that was just closed was the active buffer, and switches to the previously active buffer stored in view._prev_buffer. Since the tab close buttons were added this assumption isn't always true.

A possible solution is below. In the TAB_CLOSE_CLICKED event handler at textadept/core/ui.lua:272, add a field called inactive to the buffer being closed if it isn't the currently active buffer.

events.connect(events.TAB_CLOSE_CLICKED, function(index) 
    if _BUFFERS[index] ~= buffer then _BUFFERS[index].inactive = true end  -- <<< New Line <<<
    _BUFFERS[index]:close() 
end)

Then in the BUFFER_DELETED at textadept/core/ui.lua:387 check for the presence on the inactive field on the buffer being closed, if it's present exit early so the buffer isn't switched.

events.connect(events.BUFFER_DELETED, function(deleted_buffer)
    if deleted_buffer.inactive then return end  -- <<< New Line <<<
    if not _BUFFERS[view._prev_buffer] or buffer == view._prev_buffer then return end
    view:goto_buffer(view._prev_buffer)
end)

Thanks for all the time you've spent creating Textadept.

orbitalquark commented 2 months ago

Oh, good catch. Thanks for the detailed write up. I'm in the middle of refactoring Textadept's existing tests, so I'll add this case and fix it in due time.

orbitalquark commented 2 months ago

This should be fixed by https://github.com/orbitalquark/textadept/commit/64f4559671490c4f85f9706ef19821ce28497241. I think the simpler fix is to just switch to a non-focused buffer before closing it.