From 27ca7913d2339d88cc240e21ef226f275b6662c8 Mon Sep 17 00:00:00 2001 From: Price Hiller Date: Fri, 30 Jun 2023 17:15:29 -0500 Subject: [PATCH] refactor(nvim): register autolist mappings with an autocmd This permits buffer-only mappings --- dots/.config/nvim/lua/plugins/mappings.lua | 90 +++++++++++++++++++--- dots/.config/nvim/lua/plugins/plugins.lua | 1 + 2 files changed, 79 insertions(+), 12 deletions(-) diff --git a/dots/.config/nvim/lua/plugins/mappings.lua b/dots/.config/nvim/lua/plugins/mappings.lua index 86a3a6c9..ef1d94c7 100755 --- a/dots/.config/nvim/lua/plugins/mappings.lua +++ b/dots/.config/nvim/lua/plugins/mappings.lua @@ -248,16 +248,82 @@ vim.keymap.set("n", "xq", "TroubleToggle quickfix", { silent = vim.keymap.set("n", "xt", "TodoTrouble", { silent = true, desc = "Trouble: Todo Items" }) -- Autolist mappings -vim.keymap.set("i", "", "AutolistTab", { silent = true, desc = "Trouble: Todo Items" }) -vim.keymap.set("i", "", "AutolistShiftTab", { silent = true, desc = "Trouble: Todo Items" }) -vim.keymap.set("i", "", "AutolistNewBullet", { silent = true, desc = "Trouble: Todo Items" }) -vim.keymap.set("n", "o", "oAutolistNewBullet", { silent = true, desc = "Trouble: Todo Items" }) -vim.keymap.set("n", "O", "OAutolistNewBulletBefore", { silent = true, desc = "Trouble: Todo Items" }) -vim.keymap.set("n", "", "AutolistToggleCheckbox", { silent = true, desc = "Trouble: Todo Items" }) -vim.keymap.set("n", "", "AutolistRecalculate", { silent = true, desc = "Trouble: Todo Items" }) +local autolist_group = vim.api.nvim_create_augroup("Autolist", {}) +-- NOTE: Why is this wrapped in an autocmd you ask? Because we want to bind these for buffer local *only*. We do not +-- want to pollute the global keymaps with autolist as autolist only supports certain filetypes. +vim.api.nvim_create_autocmd("filetype", { + group = autolist_group, + pattern = { + "markdown", + "text", + "tex", + "plaintex", + "norg" + }, + callback = function() + if pcall(require, "autolist") then + vim.keymap.set( + "i", + "", + "AutolistTab", + { silent = true, desc = "Autolist: Tab", buffer = true } + ) + vim.keymap.set( + "i", + "", + "AutolistShiftTab", + { silent = true, desc = "Autolist: Shift Tab", buffer = true } + ) + vim.keymap.set( + "i", + "", + "AutolistNewBullet", + { silent = true, desc = "Autolist: New Bullet", buffer = true } + ) + vim.keymap.set( + "n", + "o", + "oAutolistNewBullet", + { silent = true, desc = "Autolist: New Bullet", buffer = true } + ) + vim.keymap.set( + "n", + "O", + "OAutolistNewBulletBefore", + { silent = true, desc = "Autolist: New Bullet Before", buffer = true } + ) + vim.keymap.set( + "n", + "", + "AutolistToggleCheckbox", + { silent = true, desc = "Autolist: Toggle Checkbox", buffer = true } + ) --- functions to recalculate list on edit -vim.keymap.set("n", ">>", ">>AutolistRecalculate", { silent = true, desc = "Trouble: Todo Items" }) -vim.keymap.set("n", "<<", "<<AutolistRecalculate", { silent = true, desc = "Trouble: Todo Items" }) -vim.keymap.set("n", "dd", "ddAutolistRecalculate", { silent = true, desc = "Trouble: Todo Items" }) -vim.keymap.set("v", "d", "dAutolistRecalculate", { silent = true, desc = "Trouble: Todo Items" }) + -- functions to recalculate list on edit + vim.keymap.set( + "n", + ">>", + ">>AutolistRecalculate", + { silent = true, desc = "Autolist: Indent", buffer = true } + ) + vim.keymap.set( + "n", + "<<", + "<<AutolistRecalculate", + { silent = true, desc = "Autolist: Dedent", buffer = true } + ) + vim.keymap.set( + "n", + "dd", + "ddAutolistRecalculate", + { silent = true, desc = "Autolist: Delete", buffer = true } + ) + vim.keymap.set( + "v", + "d", + "dAutolistRecalculate", + { silent = true, desc = "Autolist: Delete", buffer = true } + ) + end + end, +}) diff --git a/dots/.config/nvim/lua/plugins/plugins.lua b/dots/.config/nvim/lua/plugins/plugins.lua index 5e06f0f9..fd31b8be 100755 --- a/dots/.config/nvim/lua/plugins/plugins.lua +++ b/dots/.config/nvim/lua/plugins/plugins.lua @@ -572,6 +572,7 @@ lazy.setup({ "text", "tex", "plaintex", + "norg" }, opts = {}, },