New `nvim_create_autocmd` for User custom event setup

Hi there, I need help with porting the Vimscript autocmd to vim.api.nvim_create_autocmd, all my plugin upgrade works fine except the Goyo autocmd:

-- Enable the relative number when going into `GOYO` mode!
function Goyo_enter()
    -- Enable hybird number (Absoluate and relative number)
    vim.wo.number = true
    vim.wo.relativenumber = true

    print(">>> GoyoEnter trigged.")
end

function Goyo_leave()
    print(">>> GoyoLeave trigged.")
end


--[[
This setting works fine:)
This setting works fine:)
This setting works fine:)
--]]
--vim.cmd([[
--    autocmd! User GoyoEnter :call luaeval('Goyo_enter()')
--    autocmd! User GoyoLeave :call luaeval('Goyo_leave()')
--]])

--[[
This setting doesn't work :(
This setting doesn't work :(
This setting doesn't work :(
This setting doesn't work :(
--]]
local GoyoGroup = vim.api.nvim_create_augroup('GoyoGroup', { clear = true })
vim.api.nvim_create_autocmd('GoyoEnter', {
    --command = ":call luaeval('Goyo_enter()')",
    callback = Goyo_enter,
    group = GoyoGroup
})
vim.api.nvim_create_autocmd('GoyoLeave', {
    --command = ":call luaeval('Goyo_leave()')",
    callback = Goyo_leave,
    group = GoyoGroup
})

As you can see above, the old autocmd use User custom event (I check the :h events), but I still can’t figure out how to make it works in Lua setup:) If I make a change like this:

-- vim.api.nvim_create_augroup('GoyoGroup', { clear = true })

-- --> to 

vim.api.nvim_create_augroup('User', { clear = true })

Then all another plugins can’t work at all with the error I have idea what’s happening:) Any idea is welcome, many thanks:)

It may be something like vim.api.nvim_create_autocmd('User', { pattern = 'GoyoEnter' }).

Thanks for the help, it works, here is the final version:

local GoyoGroup = vim.api.nvim_create_augroup('GoyoGroup', { clear = true })

vim.api.nvim_create_autocmd('User', {
    pattern = 'GoyoEnter',
    callback = Goyo_enter,
    group = GoyoGroup
})

vim.api.nvim_create_autocmd('User', {
    pattern = 'GoyoLeave',
    callback = Goyo_leave,
    group = GoyoGroup
})

It works as expected :dancer:

Here is the auto group settings result when running :au GoyoGroup:

:au GoyoGroup

--- Autocommands ---
GoyoGroup  User
    GoyoEnter <Lua function 8>
    GoyoLeave <Lua function 8>
1 Like

I have to say thank you , Wisonye and Zeertzig.

I was working on a solution for the same module, Goyo, for same reason. This was my hardest challenge yet.

I am running vim with Lazy.nvim, which I must say I do like, it seems fast and very convenient.
So I patiently read examples in the lua-guide, as well as began the structured learning of lua in lua.org’s tutorials, both of which are excellent; the tutorials are enjoyable and well written.

However, your example as a solution not merely helps me attain with great satisfaction my desired goal in writing in vim with Goyo immediately, but the working example speeds up my learning of lua!

My solution incorporates the functionality of having ‘lualine’ staus line disappear when entering Goyo; Lualine comes with lazy.nvim, but being an nvim lua module, does not automatically get hidden by the present Goyo module, as written in vimscript (it does hide default status and airline).

local Goyo_enter = function()
  require("lualine").hide({
    place = { "statusline", "tabline", "winbar" }, -- The segment this change applies to.
    unhide = false, -- whether to re-enable lualine again/
    hide = true,
  })
end
--return {
--
local function Goyo_leave()
  require("lualine").hide({
    place = { "statusline", "tabline", "winbar" }, -- The segment this change applies to.
    unhide = true, -- whether to re-enable lualine again/
  })
end

local GoyoGroup = vim.api.nvim_create_augroup("GoyoGroup", { clear = true })
vim.api.nvim_create_autocmd("User", {
  pattern = "GoyoEnter",
  callback = Goyo_enter,
  group = GoyoGroup,
})

vim.api.nvim_create_autocmd("User", {
  pattern = "GoyoLeave",
  callback = Goyo_leave,
  group = GoyoGroup,
})

-- # Here is the auto group settings result:
-- --  :au GoyoGroup::au GoyoGroup

--- Autocommands ---
-- GoyoGroup  User
-- GoyoEnter<Lua 48960: ~/.config/nvim/lua/config/goyo.lua:34>
-- GoyoLeave<Lua 49115: ~/.config/nvim/lua/config/goyo.lua:43>