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

View File

@ -61,7 +61,16 @@ end
-- Don't need the autocommand when dark-notify is installed
local utils = require("utils")
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
-- Initial setting of colors

View File

@ -37,9 +37,16 @@ o.mouse = "a"
-- Autocomplete options
o.completeopt = "menuone,noinsert,noselect,preview"
utils.augroup("close_preview", function()
vim.cmd("autocmd! CompleteDone * if pumvisible() == 0 | pclose | endif")
end)
-- TODO: remove check when dropping v0.6.0
if vim.fn.has("nvim-0.7.0") == 1 then
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
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")
-- Create commands
vim.cmd("command! TagsUpdate !ctags -R .")
vim.cmd("command! Todo grep TODO")
-- TODO: remove check when dropping v0.6.0
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
if vim.fn.executable("rg") == 1 then
@ -20,6 +26,7 @@ elseif vim.fn.executable("ack") == 1 then
end
-- Disable polyglot for langauges I've added special support for
-- TODO: Can this be moved somewhere better?
vim.g.polyglot_disabled = { "go", "rust" }
-- Plugins

View File

@ -131,6 +131,7 @@ use({
use({
"tomtom/tcomment_vim",
config = function()
-- TODO: use which-key?
vim.api.nvim_set_keymap("n", "//", ":TComment<CR>", { silent = true, noremap = true })
vim.api.nvim_set_keymap("v", "//", ":TCommentBlock<CR>", { silent = true, noremap = true })
end,
@ -140,7 +141,12 @@ use({
use({
"FooSoft/vim-argwrap",
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,
})
@ -149,6 +155,7 @@ use({
"tpope/vim-fugitive",
config = function()
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", "gc", "<cmd>Git commit<CR>", opts)
vim.api.nvim_set_keymap("n", "gd", "<cmd>Git diff<CR>", opts)
@ -161,6 +168,7 @@ use({
use({
"milkypostman/vim-togglelist",
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", "<F7>", ":call ToggleLocationList()<CR>", { silent = true, noremap = true })
end,

View File

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