Skip to content

add function to create augroup autocmd  #6339

@prabirshrestha

Description

@prabirshrestha

Given that now we have a proper lua support one feature I'm currently missing is the capability to add to define/undefined augroup and register for autocmd events without using eval and string concatenation.

Would it be possible to create a vim script function that exposes this so we can easily use vim.fn.* apis from lua?

There is a PR for this in neovim but is lua only api. neovim/neovim#12378. It could be better to make it a vimscript api so lua gets it automatically.

From PR docs it looks like this in lua.

vim.define_autocmd({"DirChanged"}, "*", {
        on_event = function()
            print("New cwd: ", vim.v.event.cwd)
        end
    }, {})

 vim.define_augroup("ExampleGroup", true)
    vim.define_autocmd(
        {"FileType"},
        "*",
        {on_event = function() print(vim.bo.filetype) end},
        {group = "ExampleGroup"} -- NOTE: <- Here's the group assignment
    )

One change could be to make the on_event to also take a parameter so we don't have to use v:event. I have always found v:event sort of hack but that is most likely since autocmd events can't capture the event data.

lua <<EOF
    vim.fn.define_augroup("asyncomplete", true) -- false to unregister
    vim.fn.define_autocmd({
        events = "InsertEnter,InsertLeave", -- could be vim.list too
        pattern = "*",
        once = false,
        nested = false,
        group = "asyncomplete",
        on_event = function (e)
            print(e) -- where e is v:event
                     -- i would also like to have e.type so I know if it is InsertEnter or InsertLeave
        end
    })
EOF

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions