neovim / pynvim

Python client and plugin host for Nvim
http://pynvim.readthedocs.io/en/latest/
Apache License 2.0
1.51k stars 118 forks source link

Create event subscription documentation #488

Closed rr- closed 3 years ago

rr- commented 3 years ago

Context: I'm creating headless neovim child instance like so:

            self._nvim = attach(
                "child", argv=["/usr/bin/env", "nvim", "--embed", "--headless"]
            )

I'm looking into subscribing to User events (so that I can self._nvim.command(":map zp doau User blabla<CR>") and then react to it within my Python code), but the only documentation I have found is this:

subscribe(event) Subscribe to a Nvim event.

unsubscribe(event) Unsubscribe to a Nvim event.

I assumed I could self.nvim.subscribe('User', lambda ev: …) but no. All I can do is self.nvim.subscribe('User'). So I went to look at the tests to see how I can actually receive those events in the userland, and there are subscriptions tests indeed:

    vim.subscribe('event2')
    vim.command('call rpcnotify(0, "event1", 1, 2, 3)')
    vim.command('call rpcnotify(0, "event2", 4, 5, 6)')
    vim.command('call rpcnotify(0, "event2", 7, 8, 9)')
    event = vim.next_message()
    assert event[1] == 'event2'
    assert event[2] == [4, 5, 6]

However that is still not very helpful. Does that mean I now have to create an event loop that will poll vim.next_message and filter relevant events? I thought pynvim does that for me as per documentation of the Nvim class?

class Nvim(object):
    …
    When this library is used on python3.4+, asyncio event loop is guaranteed
    to be used. It is available as the "loop" attribute of this class.
justinmk commented 3 years ago

unfortunately subscribe/unsubscribe are mostly useless and unrelated to the vim "events" (autocommands). Instead see :help autocmd. work to improve this is https://github.com/neovim/neovim/pull/11613 for example

filmil commented 7 months ago

Instead see :help autocmd.

Excuse me, I am at a loss as to how :help autocmd helps with OPs question, could you elaborate?