Last active
August 20, 2023 05:15
-
-
Save numToStr/1ab83dd2e919de9235f9f774ef8076da to your computer and use it in GitHub Desktop.
Revisions
-
numToStr revised this gist
Sep 18, 2021 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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, cmd: string | fn | {pattern: string, action: string | fn}) -- For this you can just call the required module just like a function au({ 'BufNewFile', 'BufRead' }, { -
numToStr revised this gist
Sep 17, 2021 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -36,7 +36,7 @@ au({ 'BufNewFile', 'BufRead' }, { end, }) -- # 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', { -
numToStr revised this gist
Sep 17, 2021 . 2 changed files with 39 additions and 14 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 S = { __au = {}, } local X = setmetatable({}, { __index = S, __newindex = autocmd, __call = autocmd, }) function S.exec(id) S.__au[id]() end function S.set(fn) local id = string.format('%p', fn) S.__au[id] = fn return string.format('lua require("au").exec("%s")', id) end function S.group(grp, cmds) cmd('augroup ' .. grp) cmd('autocmd!') 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 X This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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) -
numToStr revised this gist
Sep 17, 2021 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -8,7 +8,7 @@ au.TextYankPost = function() end -- 2. With a pattern au.BufEnter = { '*.txt', function() if vim.bo.buftype == 'help' then -
numToStr revised this gist
Sep 17, 2021 . 2 changed files with 3 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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("au").exec("%s")', id) end function B.group(grp, cmds) This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 = vim.api.nvim_get_current_buf() vim.api.nvim_buf_set_keymap(nr, 'n', 'q', ':q<CR>', { noremap = true, silent = true }) end end, } -
numToStr created this gist
Sep 17, 2021 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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, }) This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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, }, })