Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: vim.v.count is always 0 #25

Closed
mawkler opened this issue Mar 7, 2024 · 7 comments · Fixed by #26
Closed

Bug: vim.v.count is always 0 #25

mawkler opened this issue Mar 7, 2024 · 7 comments · Fixed by #26
Assignees
Labels
enhancement New feature or request

Comments

@mawkler
Copy link

mawkler commented Mar 7, 2024

Hi! I'm trying to use vim.v.count in a mode mapping. However, it seems to always be 0.

To reproduce:

local my_mode = {
  g = function()
    vim.notify('count: ' .. vim.v.count)
  end
}

vim.keymap.set('n', 'M', function()
  require('libmodal').mode.enter('My mode', my_mode)
end)
  • Press M
  • Press 3g
  • Notification says count: 0
@Iron-E
Copy link
Owner

Iron-E commented Mar 7, 2024

Thanks for bringing it to my attention!

I have some time to look at this and the other issue today; this should hopefully be a simple fix

@Iron-E
Copy link
Owner

Iron-E commented Mar 7, 2024

I've pushed to a new branch. v:count can't be updated manually, so I've created an alternate facility which tracks count until commands are executed:

local my_mode = {
	g = function()
		vim.notify('count: ' .. vim.g.myModeModeCount)
	end
}

vim.keymap.set('n', 'M', function()
	require('libmodal').mode.enter('My mode', my_mode)
end)

Let me know if this will work for you :)

@Iron-E Iron-E added the enhancement New feature or request label Mar 8, 2024
@Iron-E Iron-E self-assigned this Mar 8, 2024
@mawkler
Copy link
Author

mawkler commented Mar 9, 2024

Too bad v:count can't be set manually. It doesn't look as clean but it does work for me. Thank you :)

@Iron-E
Copy link
Owner

Iron-E commented Mar 9, 2024

Given that #30 allows the self param, I can add self.count in a followup PR. That might look a little nicer 😅

@mawkler
Copy link
Author

mawkler commented Mar 9, 2024

Yeah that might be a good idea 🙂

@Iron-E
Copy link
Owner

Iron-E commented Mar 11, 2024

The PR was updated. This now works:

local libmodal = require 'libmodal'

-- register keymaps for splitting windows and then closing windows
local fooModeKeymaps =
{
	h = function(self)
		self.count:set(0)
	end,
	G = function(self)
		local count = self.count:get()
		vim.api.nvim_command('norm! ' .. count .. 'G')
	end,
}

-- enter the mode using the keymaps
libmodal.mode.enter('FOO', fooModeKeymaps)

And FYI, I changed :exit() to just be a simple flag:

local libmodal = require 'libmodal'

local barModeKeymaps = {
	p = function() vim.notify('Hello!') end,
}

-- register key commands and what they do
local fooModeKeymaps =
{
	q = 'let g:fooModeExit = 1', -- exits all instances of this mode
	w = function(self)
		self.exit:set_global(true) -- exits all instances of the mode (with lua)
	end,
	x = function(self)
		self.exit:set_local(true) -- exits this instance of the mode
	end,
	y = function(self)
		self:switch('Bar', barModeKeymaps) -- enters Bar and then exits Foo when it is done
	end,
	z = libmodal.mode.switch('Bar', barModeKeymaps), -- the same as above, but more convenience
}

-- enter the mode using the keymaps created before
libmodal.mode.enter('FOO', fooModeKeymaps)

Documentation has been updated for what values are available in self, and what functions (e.g. :get(), :set_local()) are available:

@mawkler
Copy link
Author

mawkler commented Mar 12, 2024

Awesome! Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants