Use new neovim api for augroup, autocommands, and user commands

Also starts using WhichKey for more thigns, but in a very basic way.
This part should probably be refactored and tested when devising a
standard way to do mappings with descriptions.
This commit is contained in:
ViViDboarder 2023-06-07 16:26:46 -07:00
parent 2e95001b5b
commit 72ba9591e7
6 changed files with 59 additions and 25 deletions

View File

@ -1,5 +1,6 @@
local utils = require("utils") local utils = require("utils")
-- TODO: Use which-key for mappings
local map = vim.api.nvim_set_keymap local map = vim.api.nvim_set_keymap
local opt_silent = { silent = true } local opt_silent = { silent = true }
@ -32,8 +33,8 @@ map("v", "pp", "p", opt_default)
map("v", "po", '"_dP', opt_default) map("v", "po", '"_dP', opt_default)
-- Buffer nav -- Buffer nav
map("n", "gb", ":bnext<CR>", {}) map("n", "gb", ":bnext<CR>", { desc = "Next buffer" })
map("n", "gB", ":bprevious<CR>", {}) map("n", "gB", ":bprevious<CR>", { desc = "Previous buffer" })
-- Easy redo -- Easy redo
map("n", "U", ":redo<CR>", opt_default) map("n", "U", ":redo<CR>", opt_default)

View File

@ -61,7 +61,16 @@ end
-- Don't need the autocommand when dark-notify is installed -- Don't need the autocommand when dark-notify is installed
local utils = require("utils") local utils = require("utils")
if not utils.is_plugin_loaded("dark-notify") then if not utils.is_plugin_loaded("dark-notify") then
utils.autocmd("auto_colors", "FocusGained * call v:lua.update_colors()") -- TODO: remove check when dropping v0.6.0
if vim.fn.has("nvim-0.7.0") == 1 then
vim.api.nvim_create_autocmd({ "FocusGained" }, {
pattern = "*",
callback = _G.update_colors,
group = vim.api.nvim_create_augroup("auto_colors", { clear = true }),
})
else
utils.autocmd("auto_colors", "FocusGained * call v:lua.update_colors()")
end
end end
-- Initial setting of colors -- Initial setting of colors

View File

@ -37,9 +37,16 @@ o.mouse = "a"
-- Autocomplete options -- Autocomplete options
o.completeopt = "menuone,noinsert,noselect,preview" o.completeopt = "menuone,noinsert,noselect,preview"
utils.augroup("close_preview", function() -- TODO: remove check when dropping v0.6.0
vim.cmd("autocmd! CompleteDone * if pumvisible() == 0 | pclose | endif") if vim.fn.has("nvim-0.7.0") == 1 then
end) vim.api.nvim_create_autocmd({ "CompleteDone" }, {
pattern = "*",
command = "if pumvisible() == 0 | pclose | endif",
group = vim.api.nvim_create_augroup("close_preview", { clear = true }),
})
else
utils.autocmd("close_preview", "CompleteDone * if pumvisible() == 0 | pclose | endif", true)
end
local has = vim.fn.has local has = vim.fn.has
g.is_mac = (has("mac") or has("macunix") or has("gui_macvim") or vim.fn.system("uname"):find("^darwin") ~= nil) g.is_mac = (has("mac") or has("macunix") or has("gui_macvim") or vim.fn.system("uname"):find("^darwin") ~= nil)

View File

@ -6,8 +6,14 @@ require("_bindings")
require("_colors") require("_colors")
-- Create commands -- Create commands
vim.cmd("command! TagsUpdate !ctags -R .") -- TODO: remove check when dropping v0.6.0
vim.cmd("command! Todo grep TODO") if vim.fn.has("nvim-0.7.0") == 1 then
vim.api.nvim_create_user_command("TagsUpdate", "!ctags -R .", { desc = "Update ctags" })
vim.api.nvim_create_user_command("Todo", "grep TODO", { desc = "Search for TODO tags" })
else
vim.cmd("command! TagsUpdate !ctags -R .")
vim.cmd("command! Todo grep TODO")
end
-- Use better grep programs -- Use better grep programs
if vim.fn.executable("rg") == 1 then if vim.fn.executable("rg") == 1 then
@ -20,6 +26,7 @@ elseif vim.fn.executable("ack") == 1 then
end end
-- Disable polyglot for langauges I've added special support for -- Disable polyglot for langauges I've added special support for
-- TODO: Can this be moved somewhere better?
vim.g.polyglot_disabled = { "go", "rust" } vim.g.polyglot_disabled = { "go", "rust" }
-- Plugins -- Plugins

View File

@ -131,6 +131,7 @@ use({
use({ use({
"tomtom/tcomment_vim", "tomtom/tcomment_vim",
config = function() config = function()
-- TODO: use which-key?
vim.api.nvim_set_keymap("n", "//", ":TComment<CR>", { silent = true, noremap = true }) vim.api.nvim_set_keymap("n", "//", ":TComment<CR>", { silent = true, noremap = true })
vim.api.nvim_set_keymap("v", "//", ":TCommentBlock<CR>", { silent = true, noremap = true }) vim.api.nvim_set_keymap("v", "//", ":TCommentBlock<CR>", { silent = true, noremap = true })
end, end,
@ -140,7 +141,12 @@ use({
use({ use({
"FooSoft/vim-argwrap", "FooSoft/vim-argwrap",
config = function() config = function()
vim.api.nvim_set_keymap("n", "<Leader>a", "<cmd>ArgWrap<CR>", { silent = true, noremap = true }) -- TODO: use which-key?
vim.api.nvim_set_keymap("n", "<Leader>a", "<cmd>ArgWrap<CR>", {
silent = true,
noremap = true,
desc = "Wrap or unwrap arguments",
})
end, end,
}) })
@ -149,6 +155,7 @@ use({
"tpope/vim-fugitive", "tpope/vim-fugitive",
config = function() config = function()
local opts = { silent = true, noremap = true } local opts = { silent = true, noremap = true }
-- TODO: use which-key?
vim.api.nvim_set_keymap("n", "gb", "<cmd>Git blame<CR>", opts) vim.api.nvim_set_keymap("n", "gb", "<cmd>Git blame<CR>", opts)
vim.api.nvim_set_keymap("n", "gc", "<cmd>Git commit<CR>", opts) vim.api.nvim_set_keymap("n", "gc", "<cmd>Git commit<CR>", opts)
vim.api.nvim_set_keymap("n", "gd", "<cmd>Git diff<CR>", opts) vim.api.nvim_set_keymap("n", "gd", "<cmd>Git diff<CR>", opts)
@ -161,6 +168,7 @@ use({
use({ use({
"milkypostman/vim-togglelist", "milkypostman/vim-togglelist",
config = function() config = function()
-- TODO: use which-key?
vim.api.nvim_set_keymap("n", "<F6>", ":call ToggleQuickfixList()<CR>", { silent = true, noremap = true }) vim.api.nvim_set_keymap("n", "<F6>", ":call ToggleQuickfixList()<CR>", { silent = true, noremap = true })
vim.api.nvim_set_keymap("n", "<F7>", ":call ToggleLocationList()<CR>", { silent = true, noremap = true }) vim.api.nvim_set_keymap("n", "<F7>", ":call ToggleLocationList()<CR>", { silent = true, noremap = true })
end, end,

View File

@ -1,30 +1,32 @@
-- luacheck: globals packer_plugins -- luacheck: globals packer_plugins
local M = {} local M = {}
-- Utils taken from https://github.com/zzzeyez/dots/blob/master/nvim/lua/utils.lua
-- Key mapping -- Key mapping
function M.map(mode, key, result, opts) function M.map(mode, key, result, opts)
vim.fn.nvim_set_keymap(mode, key, result, { M.try_require("which-key", function(wk)
noremap = true, local mappings = {}
silent = opts.silent or false, mappings[key] = result
expr = opts.expr or false, wk.register(mappings, {
script = opts.script or false, mode = mode,
}) noremap = true,
end silent = opts.silent or false,
expr = opts.expr or false,
function M.augroup(group, fn) script = opts.script or false,
vim.api.nvim_command("augroup " .. group) })
vim.api.nvim_command("autocmd!") end, function()
fn() vim.fn.nvim_set_keymap(mode, key, result, {
vim.api.nvim_command("augroup END") noremap = true,
silent = opts.silent or false,
expr = opts.expr or false,
script = opts.script or false,
})
end)
end end
function M.get_color(synID, what, mode) function M.get_color(synID, what, mode)
return vim.fn.synIDattr(vim.fn.synIDtrans(vim.fn.hlID(synID)), what, mode) return vim.fn.synIDattr(vim.fn.synIDtrans(vim.fn.hlID(synID)), what, mode)
end end
-- end zzzeyez utils
-- Create an autocmd -- Create an autocmd
function M.autocmd(group, cmds, clear) function M.autocmd(group, cmds, clear)
clear = clear == nil and false or clear clear = clear == nil and false or clear