Skip to content

Instantly share code, notes, and snippets.

@numToStr
Last active August 20, 2023 05:15
Show Gist options
  • Select an option

  • Save numToStr/1ab83dd2e919de9235f9f774ef8076da to your computer and use it in GitHub Desktop.

Select an option

Save numToStr/1ab83dd2e919de9235f9f774ef8076da to your computer and use it in GitHub Desktop.

Revisions

  1. numToStr revised this gist Sep 18, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion my_autocmds.lua
    Original file line number Diff line number Diff line change
    @@ -26,7 +26,7 @@ end

    au.BufRead = { '*.txt', 'echo &ft' }

    -- # Autocmd with multiple event: au(events: table, {pattern: string, action: string | fn})
    -- # Autocmd with multiple event: au(events: table, cmd: string | fn | {pattern: string, action: string | fn})

    -- For this you can just call the required module just like a function
    au({ 'BufNewFile', 'BufRead' }, {
  2. numToStr revised this gist Sep 17, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion my_autocmds.lua
    Original file line number Diff line number Diff line change
    @@ -36,7 +36,7 @@ au({ 'BufNewFile', 'BufRead' }, {
    end,
    })

    -- # Autocmd group: au.group(group: string, {event: string, pattern: string, action: string | fn})
    -- # Autocmd group: au.group(group: string, cmds: fn(au) | {event: string, pattern: string, action: string | fn})

    -- 1. Where action is a ex-cmd
    au.group('PackerGroup', {
  3. numToStr revised this gist Sep 17, 2021. 2 changed files with 39 additions and 14 deletions.
    32 changes: 19 additions & 13 deletions au.lua
    Original file line number Diff line number Diff line change
    @@ -14,31 +14,37 @@ local function autocmd(this, event, spec)
    cmd('autocmd ' .. e .. ' ' .. pattern .. ' ' .. action)
    end

    local B = {
    local S = {
    __au = {},
    }

    function B.exec(id)
    B.__au[id]()
    local X = setmetatable({}, {
    __index = S,
    __newindex = autocmd,
    __call = autocmd,
    })

    function S.exec(id)
    S.__au[id]()
    end

    function B.set(fn)
    function S.set(fn)
    local id = string.format('%p', fn)
    B.__au[id] = fn
    S.__au[id] = fn
    return string.format('lua require("au").exec("%s")', id)
    end

    function B.group(grp, cmds)
    function S.group(grp, cmds)
    cmd('augroup ' .. grp)
    cmd('autocmd!')
    for _, au in ipairs(cmds) do
    autocmd(B, au[1], { au[2], au[3] })
    if type(cmds) == 'function' then
    cmds(X)
    else
    for _, au in ipairs(cmds) do
    autocmd(S, au[1], { au[2], au[3] })
    end
    end
    cmd('augroup END')
    end

    return setmetatable({}, {
    __index = B,
    __newindex = autocmd,
    __call = autocmd,
    })
    return X
    21 changes: 20 additions & 1 deletion my_autocmds.lua
    Original file line number Diff line number Diff line change
    @@ -59,4 +59,23 @@ au.group('CocOverrides', {
    vim.fn.CocActionAsync('showSignatureHelp')
    end,
    },
    })
    })


    -- 3. Or behold some meta-magic
    -- You can give a function as a second arg which receives aucmd-metatable as an argument
    -- Which you can use to set autocmd individually
    au.group('CocOverrides', function(grp)
    grp.FileType = {
    'typescript,json',
    function()
    vim.api.nvim_buf_set_option(0, 'formatexpr', "CocAction('formatSelected')")
    end,
    }
    grp.User = {
    'CocJumpPlaceholder',
    function()
    vim.fn.CocActionAsync('showSignatureHelp')
    end,
    }
    end)
  4. numToStr revised this gist Sep 17, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion my_autocmds.lua
    Original file line number Diff line number Diff line change
    @@ -8,7 +8,7 @@ au.TextYankPost = function()
    end

    -- 2. With a pattern
    au2.BufEnter = {
    au.BufEnter = {
    '*.txt',
    function()
    if vim.bo.buftype == 'help' then
  5. numToStr revised this gist Sep 17, 2021. 2 changed files with 3 additions and 3 deletions.
    2 changes: 1 addition & 1 deletion au.lua
    Original file line number Diff line number Diff line change
    @@ -25,7 +25,7 @@ end
    function B.set(fn)
    local id = string.format('%p', fn)
    B.__au[id] = fn
    return string.format('lua require("au2").exec("%s")', id)
    return string.format('lua require("au").exec("%s")', id)
    end

    function B.group(grp, cmds)
    4 changes: 2 additions & 2 deletions my_autocmds.lua
    Original file line number Diff line number Diff line change
    @@ -13,8 +13,8 @@ au2.BufEnter = {
    function()
    if vim.bo.buftype == 'help' then
    cmd('wincmd L')
    local nr = api.nvim_get_current_buf()
    api.nvim_buf_set_keymap(nr, 'n', 'q', ':q<CR>', { noremap = true, silent = true })
    local nr = vim.api.nvim_get_current_buf()
    vim.api.nvim_buf_set_keymap(nr, 'n', 'q', ':q<CR>', { noremap = true, silent = true })
    end
    end,
    }
  6. numToStr created this gist Sep 17, 2021.
    44 changes: 44 additions & 0 deletions au.lua
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,44 @@
    --
    -- Move this file to your neovim lua runtime path ie. ~/.config/nvim/lua/au.lua
    --
    local cmd = vim.api.nvim_command

    local function autocmd(this, event, spec)
    local is_table = type(spec) == 'table'
    local pattern = is_table and spec[1] or '*'
    local action = is_table and spec[2] or spec
    if type(action) == 'function' then
    action = this.set(action)
    end
    local e = type(event) == 'table' and table.concat(event, ',') or event
    cmd('autocmd ' .. e .. ' ' .. pattern .. ' ' .. action)
    end

    local B = {
    __au = {},
    }

    function B.exec(id)
    B.__au[id]()
    end

    function B.set(fn)
    local id = string.format('%p', fn)
    B.__au[id] = fn
    return string.format('lua require("au2").exec("%s")', id)
    end

    function B.group(grp, cmds)
    cmd('augroup ' .. grp)
    cmd('autocmd!')
    for _, au in ipairs(cmds) do
    autocmd(B, au[1], { au[2], au[3] })
    end
    cmd('augroup END')
    end

    return setmetatable({}, {
    __index = B,
    __newindex = autocmd,
    __call = autocmd,
    })
    62 changes: 62 additions & 0 deletions my_autocmds.lua
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,62 @@
    local au = require('au')

    -- # Simple autocmd with one event: au.<event> = string | fn | { pattern: string, action: string | fn }

    -- 1. If you want aucmd to fire on every buffer, you can use the style below
    au.TextYankPost = function()
    vim.highlight.on_yank({ higroup = 'Visual', timeout = 120 })
    end

    -- 2. With a pattern
    au2.BufEnter = {
    '*.txt',
    function()
    if vim.bo.buftype == 'help' then
    cmd('wincmd L')
    local nr = api.nvim_get_current_buf()
    api.nvim_buf_set_keymap(nr, 'n', 'q', ':q<CR>', { noremap = true, silent = true })
    end
    end,
    }

    -- TIP: action can be a ex-cmd or a lua function
    au.BufRead = function()
    print(vim.bo.filetype)
    end

    au.BufRead = { '*.txt', 'echo &ft' }

    -- # Autocmd with multiple event: au(events: table, {pattern: string, action: string | fn})

    -- For this you can just call the required module just like a function
    au({ 'BufNewFile', 'BufRead' }, {
    '.eslintrc,.prettierrc,*.json*',
    function()
    vim.bo.filetype = 'json'
    end,
    })

    -- # Autocmd group: au.group(group: string, {event: string, pattern: string, action: string | fn})

    -- 1. Where action is a ex-cmd
    au.group('PackerGroup', {
    { 'BufWritePost', 'plugins.lua', 'source <afile> | PackerCompile' },
    })

    -- 2. Where action is a function
    au.group('CocOverrides', {
    {
    'FileType',
    'typescript,json',
    function()
    vim.api.nvim_buf_set_option(0, 'formatexpr', "CocAction('formatSelected')")
    end,
    },
    {
    'User',
    'CocJumpPlaceholder',
    function()
    vim.fn.CocActionAsync('showSignatureHelp')
    end,
    },
    })