neovim / pynvim

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

Is possible to register autocmds dynamically? #440

Closed Haagaau22 closed 4 years ago

Haagaau22 commented 4 years ago

Hi! I want to register some autocmds from a config dynamically, and I tried some code, but registration failed and no exceptions. Following is my code:

import pynvim

AUTOCMDS = [
    {
        'name': 'BufEnter',
        'kwargs': {
             'pattern': '~/notes/*'
            },
        'cmds': [
            'nnoremap <buffer> <c-x>a :CommandA<cr>',
            'nnoremap <buffer> <c-x>b :CommandB<cr>',
            'nnoremap <buffer> <c-x>c :CommandC<cr>'
            ]
    },
]

def generate_autocmd_func(cmd):
    return lambda self: self.nvim.command(cmd)

@pynvim.plugin
class AutocmdsPlugin(object):

    def __init__(self, nvim):
        self.nvim = nvim
        # self.init_autocmds()

    @pynvim.autocmd('VimEnter')
    def add_autocmds(self):
        self.nvim.out_write('vimEnter autocmd\n')
        self.init_autocmds()

    def init_autocmds(self):

        for autocmd in AUTOCMDS:
            for cmd in autocmd['cmds']:
                pynvim.autocmd(autocmd['name'], **autocmd['kwargs'])(generate_autocmd_func(cmd))

        self.nvim.out_write('init autocmds\n')

Any suggestions? Thanks!

justinmk commented 4 years ago

I don't see why not. Did you check :messages

Haagaau22 commented 4 years ago

@justinmk Hi, I just checked the :messages and I didn't find any valuable information. After I use UpdateRemotePlugins, there are no errors and the plugin is in the list of registered plugins. Then I restart the neovim and check :messages, it shows:

vimEnter autocmd
init autocmds

And my directory structure is like this:

/home/zz/.config/nvim/rplugin/python3
├── autocmds
│   ├── __init__.py

neovim version: v0.4.3 os: arch linux

I simplified the code and maybe it's easier to reproduce at now.

import pynvim

def echo_py1():
    return lambda self: self.nvim.out_write('echo_py1 \n')

@pynvim.plugin
class AutocmdsPlugin(object):

    def __init__(self, nvim):
        self.nvim = nvim
        # self.init_autocmds()

    @pynvim.autocmd('VimEnter')
    def add_autocmds(self):
        self.nvim.out_write('vimEnter autocmd\n')
        self.init_autocmds()

    def echo_py2(self):
        self.nvim.out_write('echo_py2\n')

    @pynvim.autocmd('BufEnter',  pattern='*.py')
    def echo_py3(self):
        self.nvim.out_write('echo_py3\n')

    def init_autocmds(self):

        pynvim.autocmd('BufEnter',  pattern='*.py')(AutocmdsPlugin.echo_py2)
        pynvim.autocmd('BufEnter',  pattern='*.py')(echo_py1())

        self.nvim.out_write('init autocmds\n')

the echo_py3 can work well and both echo_py1 and echo_py2 can't work. Thanks again

Shougo commented 4 years ago

Unfortunately, autocmds are registered in rplugin.vim(~/.local/share/nvim/rplugin.vim). It is updated by :UpdateRemotePlugins.

So you cannot register autocmds dynamically.

justinmk commented 4 years ago

oh right. thanks @Shougo .

covered by https://github.com/neovim/neovim/issues/5532