aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2022-04-30 20:46:42 +0900
committernsfisis <nsfisis@gmail.com>2022-04-30 20:46:42 +0900
commitf291e62f4d8aeda4f9fb5b3eece7a6223121526b (patch)
treea52ff9e053c73dee15d6d174b06a820c9bafc7c5
parent1a32b613a4e794f387c8f642ed67a5a3b2f35a43 (diff)
downloaddotfiles-f291e62f4d8aeda4f9fb5b3eece7a6223121526b.tar.gz
dotfiles-f291e62f4d8aeda4f9fb5b3eece7a6223121526b.tar.zst
dotfiles-f291e62f4d8aeda4f9fb5b3eece7a6223121526b.zip
neovim: replace :autocmd with Lua API
-rw-r--r--.config/nvim/init.lua95
-rw-r--r--.config/nvim/lua/vimrc.lua54
-rw-r--r--TODO1
3 files changed, 85 insertions, 65 deletions
diff --git a/.config/nvim/init.lua b/.config/nvim/init.lua
index a96caf9..48d452b 100644
--- a/.config/nvim/init.lua
+++ b/.config/nvim/init.lua
@@ -66,8 +66,7 @@ end
-- The augroup used in .vimrc {{{2
-vimrc.augroup = vimrc.augroup or {}
-vimrc.augroup.vimrc = vim.api.nvim_create_augroup('Vimrc', {})
+vimrc.create_augroup_for_vimrc()
@@ -235,42 +234,48 @@ O.fileencodings = 'utf-8,cp932,euc-jp'
-- Autocommands {{{1
-- Auto-resize windows when Vim is resized.
-vimrc.autocmd('VimResized', '*', function()
- vim.cmd('wincmd =')
-end)
+vimrc.autocmd('VimResized', {
+ command = 'wincmd =',
+})
-- Calculate 'numberwidth' to fit file size.
-- Note: extra 2 is the room of left and right spaces.
-vimrc.autocmd('BufEnter,WinEnter,BufWinEnter', '*', function()
- vim.wo.numberwidth = #tostring(F.line('$')) + 2
-end)
+vimrc.autocmd({'BufEnter', 'WinEnter', 'BufWinEnter'}, {
+ callback = function()
+ vim.wo.numberwidth = #tostring(F.line('$')) + 2
+ end,
+})
-- Jump to the last cursor position when you open a file.
-vimrc.autocmd('BufRead', '*', function()
- if 0 < F.line("'\"") and F.line("'\"") <= F.line('$') and
- not O.filetype:match('commit') and not O.filetype:match('rebase')
- then
- vim.cmd('normal g`"')
- end
-end)
+vimrc.autocmd('BufRead', {
+ callback = function()
+ if 0 < F.line("'\"") and F.line("'\"") <= F.line('$') and
+ not O.filetype:match('commit') and not O.filetype:match('rebase')
+ then
+ vim.cmd('normal g`"')
+ end
+ end,
+})
-- Lua version of https://github.com/mopp/autodirmake.vim
-- License: NYSL
-vimrc.autocmd('BufWritePre', '*', function()
- local dir = F.expand('<afile>:p:h')
- if F.isdirectory(dir) ~= 0 then
- return
- end
- vimrc.echo(('"%s" does not exist. Create? [y/N] '):format(dir), 'Question')
- local answer = vimrc.getchar()
- if answer ~= 'y' and answer ~= 'Y' then
- return
- end
- F.mkdir(dir, 'p')
-end)
+vimrc.autocmd('BufWritePre', {
+ callback = function()
+ local dir = F.expand('<afile>:p:h')
+ if F.isdirectory(dir) ~= 0 then
+ return
+ end
+ vimrc.echo(('"%s" does not exist. Create? [y/N] '):format(dir), 'Question')
+ local answer = vimrc.getchar()
+ if answer ~= 'y' and answer ~= 'Y' then
+ return
+ end
+ F.mkdir(dir, 'p')
+ end,
+})
vimrc.register_filetype_autocmds_for_indentation()
@@ -1323,9 +1328,10 @@ vimrc.map_plug('x', 'm/b', '(caw:box:comment)')
G['clang_format#auto_format'] = true
-vimrc.autocmd('FileType', 'javascript,typescript', function()
- vim.cmd('ClangFormatAutoDisable')
-end)
+vimrc.autocmd('FileType', {
+ pattern = {'javascript', 'typescript'},
+ command = 'ClangFormatAutoDisable',
+})
@@ -1360,9 +1366,10 @@ vimrc.map_plug('x', '=', '(EasyAlign)')
-- emmet {{{2
G.user_emmet_install_global = false
-vimrc.autocmd('FileType', 'html,css', function()
- vim.cmd('EmmetInstall')
-end)
+vimrc.autocmd('FileType', {
+ pattern = {'html', 'css'},
+ command = 'EmmetInstall',
+})
@@ -1786,12 +1793,22 @@ vimrc.map_plug('x', 'aL', '(textobj-continuous-cpp-a)')
vimrc.map_plug('o', 'iL', '(textobj-continuous-cpp-i)')
vimrc.map_plug('x', 'iL', '(textobj-continuous-cpp-i)')
-vim.cmd([[
-autocmd Vimrc FileType vim omap <buffer> aL <Plug>(textobj-continuous-vim-a)
-autocmd Vimrc FileType vim xmap <buffer> aL <Plug>(textobj-continuous-vim-a)
-autocmd Vimrc FileType vim omap <buffer> iL <Plug>(textobj-continuous-vim-i)
-autocmd Vimrc FileType vim xmap <buffer> iL <Plug>(textobj-continuous-vim-i)
-]])
+vimrc.autocmd('FileType', {
+ pattern = 'vim',
+ command = 'omap <buffer> aL <Plug>(textobj-continuous-vim-a)',
+})
+vimrc.autocmd('FileType', {
+ pattern = 'vim',
+ command = 'xmap <buffer> aL <Plug>(textobj-continuous-vim-a)',
+})
+vimrc.autocmd('FileType', {
+ pattern = 'vim',
+ command = 'omap <buffer> iL <Plug>(textobj-continuous-vim-i)',
+})
+vimrc.autocmd('FileType', {
+ pattern = 'vim',
+ command = 'xmap <buffer> iL <Plug>(textobj-continuous-vim-i)',
+})
diff --git a/.config/nvim/lua/vimrc.lua b/.config/nvim/lua/vimrc.lua
index 5f7475b..7de36d5 100644
--- a/.config/nvim/lua/vimrc.lua
+++ b/.config/nvim/lua/vimrc.lua
@@ -1,16 +1,18 @@
local M = {}
-local autocmd_callbacks = {}
-M.autocmd_callbacks = autocmd_callbacks
-function M.autocmd(event, filter, callback)
- local callback_id = #autocmd_callbacks + 1
- autocmd_callbacks[callback_id] = callback
- vim.cmd(('autocmd Vimrc %s %s lua vimrc.autocmd_callbacks[%d]()'):format(
- event,
- filter,
- callback_id))
+local vimrc_augroup
+
+function M.create_augroup_for_vimrc()
+ vimrc_augroup = vim.api.nvim_create_augroup('Vimrc', {})
+end
+
+function M.autocmd(event, opts)
+ if not opts.group then
+ opts.group = vimrc_augroup
+ end
+ vim.api.nvim_create_autocmd(event, opts)
end
@@ -28,6 +30,18 @@ end
+local function set_indentation(style, width)
+ vim.bo.expandtab = style
+ vim.bo.tabstop = width
+ vim.bo.shiftwidth = width
+ vim.bo.softtabstop = width
+
+ if vim.fn.exists(':IndentLinesReset') == 2 then
+ vim.cmd('IndentLinesReset')
+ end
+end
+
+
function M.register_filetype_autocmds_for_indentation()
local SPACE = true
local TAB = false
@@ -56,22 +70,12 @@ function M.register_filetype_autocmds_for_indentation()
}
for ft, setting in pairs(indentation_settings) do
- vim.cmd(([[autocmd Vimrc FileType %s lua vimrc._set_indentation(%s, %d)]]):format(
- ft,
- setting.style,
- setting.width
- ))
- end
-end
-
-function M._set_indentation(style, width)
- vim.bo.expandtab = style
- vim.bo.tabstop = width
- vim.bo.shiftwidth = width
- vim.bo.softtabstop = width
-
- if vim.fn.exists(':IndentLinesReset') == 2 then
- vim.cmd('IndentLinesReset')
+ vimrc.autocmd('FileType', {
+ pattern = ft,
+ callback = function()
+ set_indentation(setting.style, setting.width)
+ end,
+ })
end
end
diff --git a/TODO b/TODO
index 310792a..098b619 100644
--- a/TODO
+++ b/TODO
@@ -2,7 +2,6 @@ nvim 0.7
https://github.com/neovim/neovim/releases/tag/v0.7.0
keymap
- autocmd
highlight
nvim_{add,del}_user_command