From 9ded34f73cd2cf560208865b58c853d24dc69174 Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Thu, 27 Jun 2024 17:18:34 +0200 Subject: [PATCH] nvim: Add custom spellcheck toggle with user events Added a user command `SpellTogle` which toggles on or off spellchecking in the current buffer. Can be invoked like that, or with one or multiple language to spellcheck (e.g. `SpellToggle en_us en_gb`). Can also be invoked with a bang to always enable instead of toggling. Publishes a user event called `SpellEnable` or `SpellDisable` depending on the aciton which autocommands can listen for. --- nvim/.config/nvim/lua/core/commands.lua | 17 +++++++++++++++++ nvim/.config/nvim/lua/core/mappings.lua | 9 +++++---- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/nvim/.config/nvim/lua/core/commands.lua b/nvim/.config/nvim/lua/core/commands.lua index 1a3906b..289cd36 100644 --- a/nvim/.config/nvim/lua/core/commands.lua +++ b/nvim/.config/nvim/lua/core/commands.lua @@ -24,3 +24,20 @@ vim.api.nvim_create_autocmd({ "TermOpen" }, { pattern = "*", command = "setlocal nonumber norelativenumber", }) + +-- Custom spell enable which fires an event that other things can +-- hook into. +-- Toggles off and on by default, only turns on if called with bang. +vim.api.nvim_create_user_command("SpellToggle", function(opt) + vim.opt.spell = opt.bang or not vim.opt.spell:get() + if vim.opt.spell:get() then + if next(opt.fargs) ~= nil then + vim.opt.spelllang = opt.fargs + end + vim.api.nvim_exec_autocmds("User", { pattern = "SpellEnable" }) + vim.notify("Spellcheck enabled") + else + vim.api.nvim_exec_autocmds("User", { pattern = "SpellDisable" }) + vim.notify("Spellcheck disabled") + end +end, { nargs = "*", bang = true }) diff --git a/nvim/.config/nvim/lua/core/mappings.lua b/nvim/.config/nvim/lua/core/mappings.lua index a4d3028..5f61ba8 100644 --- a/nvim/.config/nvim/lua/core/mappings.lua +++ b/nvim/.config/nvim/lua/core/mappings.lua @@ -127,10 +127,11 @@ map("n", "Q", "vapJgqap", { silent = true, desc = "Unformat then fo -- SPELL CHECKING -- Move to the prev/next spelling error with [S ]S -- Move to the prev/next spelling error or suggestion with [s ]s -map("n", "ZZ", ":setlocal spell! spelllang=en_us,en_gb,de_de", { desc = "Toggle spellcheck" }) -map("n", "ZE", ":setlocal spell! spelllang=en_us", { desc = "Toggle EN_US spellcheck" }) -map("n", "ZB", ":setlocal spell! spelllang=en_gb", { desc = "Toggle EN_GB spellcheck" }) -map("n", "ZD", ":setlocal spell! spelllang=de_de", { desc = "Toggle DE_DE spellcheck" }) +map("n", "ZZ", "SpellToggle en_us en_gb de_de", { desc = "Toggle spellcheck" }) +map("n", "ZA", "SpellToggle! en_us en_gb de_de", { desc = "Enable spellcheck" }) +map("n", "ZE", "SpellToggle en_us", { desc = "Toggle EN_US spellcheck" }) +map("n", "ZB", "SpellToggle en_gb", { desc = "Toggle EN_GB spellcheck" }) +map("n", "ZD", "SpellToggle de_de", { desc = "Toggle DE_DE spellcheck" }) -- undo last spelling mistake from insert and normal mode map("i", "", "ue[s1z=`]au") map("n", "z", "mse[s1z=`s", { desc = "Fix last spell error" })