diff --git a/README.md b/README.md index a64ced0..69e609a 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,13 @@ Use your favorite package manager. Example config with [packer.nvim](https://git use { "sitiom/nvim-numbertoggle" } ``` +## Disabling + +This plugin can be disabled in two ways: + +- Globally: set `vim.g.numbertoggle_disable` to `true`. +- Locally for a buffer: set `vim.b.numbertoggle_disable` to `true`. + ## Acknowledgment https://github.com/jeffkreeftmeijer/vim-numbertoggle diff --git a/plugin/numbertoggle.lua b/plugin/numbertoggle.lua index 5c38dbf..bf5fbf8 100644 --- a/plugin/numbertoggle.lua +++ b/plugin/numbertoggle.lua @@ -1,9 +1,22 @@ local augroup = vim.api.nvim_create_augroup("numbertoggle", {}) +local function is_disabled(buf) + if vim.g.numbertoggle_disable then + return true + end + if not vim.api.nvim_buf_is_valid(buf) then + return nil + end + return vim.b[buf or 0].numbertoggle_disable +end + vim.api.nvim_create_autocmd({ "BufEnter", "FocusGained", "InsertLeave", "CmdlineLeave", "WinEnter" }, { pattern = "*", group = augroup, - callback = function() + callback = function(args) + if is_disabled(args.buf) then + return + end if vim.o.nu and vim.api.nvim_get_mode().mode ~= "i" then vim.opt.relativenumber = true end @@ -13,12 +26,15 @@ vim.api.nvim_create_autocmd({ "BufEnter", "FocusGained", "InsertLeave", "Cmdline vim.api.nvim_create_autocmd({ "BufLeave", "FocusLost", "InsertEnter", "CmdlineEnter", "WinLeave" }, { pattern = "*", group = augroup, - callback = function() + callback = function(args) + if is_disabled(args.buf) then + return + end if vim.o.nu then vim.opt.relativenumber = false -- Conditional taken from https://github.com/rockyzhang24/dotfiles/commit/03dd14b5d43f812661b88c4660c03d714132abcf -- Workaround for https://github.com/neovim/neovim/issues/32068 - if not vim.tbl_contains({"@", "-"}, vim.v.event.cmdtype) then + if not vim.tbl_contains({ "@", "-" }, vim.v.event.cmdtype) then vim.cmd "redraw" end end