Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Buffer switch behaviour #33

Closed
rgieseke opened this issue Jan 15, 2021 · 7 comments
Closed

Buffer switch behaviour #33

rgieseke opened this issue Jan 15, 2021 · 7 comments

Comments

@rgieseke
Copy link

I'm trying to understand the buffer switch behaviour as the view state restoring is sometimes messed up when using Textredux. E.g. i have wrap_mode set to None in a Textredux buffer, but by word otherwise.
When closing a Textredux buffer the wrong wrap_mode is applied to a regular Text buffer.

With

events.connect(events.BUFFER_AFTER_SWITCH, function()
  print(buffer.filename or buffer._type)
end, 1)

in my init.lua and

-- Restore view state.
local function restore_view_state()
  if not buffer._margin_type_n then return end
  print("Restoring", buffer.filename)
  view.view_ws, view.wrap_mode = buffer._view_ws, buffer._wrap_mode
  for i = 1, view.margins do
    view.margin_type_n[i] = buffer._margin_type_n[i]
    view.margin_width_n[i] = buffer._margin_width_n[i]
  end
end
events.connect(events.BUFFER_AFTER_SWITCH, restore_view_state)
events.connect(events.VIEW_AFTER_SWITCH, restore_view_state)

in Textadept's core/ui.lua (just added a print statement) and a similar statement in the save_view_store function it seems, that when I

  • create a new buffer
  • close the new buffer
  • the last buffer's view (the last one to have been opened) state is saved again and then
  • it switches to the original "previous" buffer (and the restore event seems to fire twice)

I suspect that this is the reason for the problem with the view state being applied to the wrong buffer.
Any ideas why it switches to the wrong buffer first? Should it save view state also after a buffer_new event?

@orbitalquark
Copy link
Owner

orbitalquark commented Jan 17, 2021 via email

@rgieseke
Copy link
Author

Say you have 2 files open:

  • init.lua
  • session

You go to the first opened init.lua, then you open a new buffer and close the new buffer:

What happens is:

Saving view state	session
Switch to init.lua
Restoring	init.lua
Switch to init.lua
Restoring	init.lua

So if you change wrap mode oder margins in the new buffer (like i do in Textredux) the session file passed by while closing inherits the view state from the new buffer.

The problem seems to be that there is no "after_switch" event fired for the passing-by of the last buffer in _BUFFERS.

@orbitalquark
Copy link
Owner

orbitalquark commented Jan 17, 2021 via email

@rgieseke
Copy link
Author

rgieseke commented Jan 17, 2021

Thanks!

Maybe disconnecting events before the switch to the previous buffer could be a fix.

-- Keeps track of, and switches back to the previous buffer after buffer close.
events.connect(
  events.BUFFER_BEFORE_SWITCH, function() view._prev_buffer = buffer end)
events.connect(events.BUFFER_DELETED, function()
  if _BUFFERS[view._prev_buffer] and buffer ~= view._prev_buffer then
    events.disconnect(events.VIEW_BEFORE_SWITCH, save_view_state)
    events.disconnect(events.BUFFER_BEFORE_SWITCH, save_buffer_state)
    view:goto_buffer(view._prev_buffer)
    events.connect(events.BUFFER_BEFORE_SWITCH, save_view_state)
    events.connect(events.VIEW_BEFORE_SWITCH, save_view_state)
  end
end)

Seems to fix the example scenario above, will check further. Maybe not both need to be disconnected. [Edited to use correct event]

@rgieseke
Copy link
Author

The above would likely work with save_view_state only.

Thus, the following also seems to do the trick:

-- Keeps track of, and switches back to the previous buffer after buffer close.
events.connect(
  events.BUFFER_BEFORE_SWITCH, function() view._prev_buffer = buffer end)
events.connect(events.BUFFER_DELETED, function()
  if _BUFFERS[view._prev_buffer] and buffer ~= view._prev_buffer then
    restore_view_state()
    view:goto_buffer(view._prev_buffer)
  end
end)

@orbitalquark
Copy link
Owner

orbitalquark commented Jan 24, 2021 via email

@rgieseke
Copy link
Author

Awesome, thank you!
Next time i'll try to submit a reproducible test case, this is nice :-)

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

No branches or pull requests

2 participants