neovim-qt icon indicating copy to clipboard operation
neovim-qt copied to clipboard

FocusGained does not reload buffer when autoread is set

Open przepompownia opened this issue 3 years ago • 5 comments

  • open nvim-qt -- --clean -c 'set autoread' /tmp/test
  • lose focus on nvim-qt, add any change to /tmp/test
  • focus on nvim-qt again
  • check if the buffer was reloaded.

I have to run :checktime manually or write autocommand for nvim-qt only. Is it intentional?

There is no such need on pure nvim.

przepompownia avatar May 06 '21 12:05 przepompownia

I have to run :checktime manually or write autocommand for nvim-qt only. Is it intentional?

No, I dont think it is.

In nvim the autoread check is triggered in check_timestamps (among other places) called when there is a focus change (do_autocmd_focusgained).

Since #329 the focus events are implemented as an autocommand

https://github.com/equalsraf/neovim-qt/blob/e1d5487e69e798a0c743b1770ec50d6de005e7bf/src/gui/shell.cpp#L1566

As far as i can see the focus auto commands are working properly, but the autoread option is not.

There are a couple of conditions in nvim which can cause autoread to be skipped

https://github.com/neovim/neovim/blob/71107e12c7b68d8faf1bcc1a5794a55b21e146f3/src/nvim/fileio.c#L4678

and as you already pointed out calling :checktime does work, and is very similar

https://github.com/neovim/neovim/blob/c2624b87cb6b5ba1fe811a005cc3c0977a864633/src/nvim/ex_cmds2.c#L3509

I would have to trace nvim to figure out why this is not being called.

equalsraf avatar May 06 '21 21:05 equalsraf

As far as i can see the focus auto commands are working properly, but the autoread option is not.

You are right. While I used https://github.com/tmux-plugins/vim-tmux-focus-events it executed :checktime on FocusGained with autocommand. The problem was revealed after removing this autocommand along with the plugin. Now the event is triggered but autoread does not work on it.

przepompownia avatar May 07 '21 10:05 przepompownia

The bug is still present on recent master versions of both Neovim and Neovim-qt.

przepompownia avatar Feb 23 '23 23:02 przepompownia

The 'autoread' feature does not work out-of-the-box for both nvim-qt and neovide:

The 'autoread' feature does work for nvim's TUI.

The reason it works for the TUI is mentioned in this now-closed Neovim issue: https://github.com/neovim/neovim/issues/20082#issuecomment-1236324274

No. I just noticed that neovide already triggers FocusGained, and that triggering FocusGained from :doautocmd doesn't actually trigger a timestamp check. With TUI a timestamp check is done the same time as FocusGained, but it is not an autocommand callback, but something triggered the same time as the autocommand.

In that issue, Neovim maintainer @justinmk has commented on the 'autoread' feature: https://github.com/neovim/neovim/issues/20082#issuecomment-1288913518

As mentioned above, FocusGained can be used to opt-in to the behavior you want.

The general story of improving autoread is tracked in https://github.com/neovim/neovim/issues/1380

This refers to having GUI users add this to their configuration:

autocmd FocusGained * checktime

I can confirm that this work-around does restore the 'autoread' functionality for me, with both GUIs.

It's unclear to me whether there is a long-term plan to restore the 'autoread' feature for Neovim GUIs users without requiring the above work-around.

drmikehenry avatar Jul 30 '23 10:07 drmikehenry

Thanks for that summary @drmikehenry . As noted in https://github.com/neovide/neovide/issues/1477 , the GUI just needs to call nvim_ui_set_focus.

justinmk avatar Jul 30 '23 14:07 justinmk