Skip to content

Commit

Permalink
Use getmousepos() if mousemoveevent is enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
notomo committed Aug 19, 2023
1 parent a1b744b commit b0326ce
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 20 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ gesture.nvim is a mouse gesture plugin for Neovim (nightly).

```lua
vim.opt.mouse = "a"
vim.opt.mousemoveevent = true

vim.keymap.set("n", "<LeftDrag>", [[<Cmd>lua require("gesture").draw()<CR>]], { silent = true })
vim.keymap.set("n", "<LeftRelease>", [[<Cmd>lua require("gesture").finish()<CR>]], { silent = true })
Expand Down
1 change: 1 addition & 0 deletions doc/gesture.nvim.txt
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ EXAMPLE *gesture.nvim-EXAMPLE*

>lua
vim.opt.mouse = "a"
vim.opt.mousemoveevent = true

vim.keymap.set("n", "<LeftDrag>", [[<Cmd>lua require("gesture").draw()<CR>]], { silent = true })
vim.keymap.set("n", "<LeftRelease>", [[<Cmd>lua require("gesture").finish()<CR>]], { silent = true })
Expand Down
4 changes: 2 additions & 2 deletions lua/gesture/core/state.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function State.get_or_create(open_view)
local first_window_id = vim.api.nvim_get_current_win()
local matcher = require("gesture.core.matcher").new(vim.api.nvim_get_current_buf())
local view, window_id = open_view()
local point = view.current_point()
local point = view:current_point()
local tbl = {
_last_point = point,
_suspended = false,
Expand Down Expand Up @@ -80,7 +80,7 @@ function State.close(self)
end

function State.action_context(self)
local point = self.view.current_point()
local point = self.view:current_point()
return {
last_position = {
point.y,
Expand Down
29 changes: 24 additions & 5 deletions lua/gesture/view/background.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
local Point = require("gesture.core.point")
local Canvas = require("gesture.view.canvas")
local windowlib = require("gesture.lib.window")
local mouse = require("gesture.view.mouse")
Expand Down Expand Up @@ -38,9 +39,26 @@ function Background.open(winblend)
end
vim.api.nvim_buf_set_name(bufnr, ("gesture://%d/GESTURE"):format(bufnr))

-- NOTE: show and move cursor to the window by <LeftDrag>
vim.cmd.redraw()
mouse.click()
local get_position
if vim.o.mousemoveevent then
get_position = function()
local mouse_pos = vim.fn.getmousepos()
return Point.new(mouse_pos.screencol - 1, mouse_pos.screenrow)
end
else
-- NOTE: show and move cursor to the window by <LeftDrag>
vim.cmd.redraw()
mouse.click()
get_position = function()
mouse.click()
if not vim.api.nvim_win_is_valid(window_id) then
return nil
end
local x = vim.fn.wincol()
local y = vim.fn.winline()
return Point.new(x, y)
end
end

vim.api.nvim_create_autocmd({ "WinLeave", "TabLeave", "BufLeave" }, {
buffer = bufnr,
Expand All @@ -52,14 +70,15 @@ function Background.open(winblend)

local ns = vim.api.nvim_create_namespace("gesture")
local tbl = {
get_position = get_position,
_window_id = window_id,
_ns = ns,
}
local self = setmetatable(tbl, Background)

vim.api.nvim_set_decoration_provider(self._ns, {
on_win = function(_, _, buf, topline)
if topline == 0 or buf ~= bufnr or not self:is_valid() then
if topline == 0 or buf ~= bufnr or not self:_is_valid() then
return false
end
vim.fn.winrestview({ topline = 0, leftcol = 0 })
Expand All @@ -74,7 +93,7 @@ function Background.close(self)
vim.api.nvim_set_decoration_provider(self._ns, {})
end

function Background.is_valid(self)
function Background._is_valid(self)
return vim.api.nvim_win_is_valid(self._window_id)
end

Expand Down
18 changes: 5 additions & 13 deletions lua/gesture/view/init.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
local Point = require("gesture.core.point")
local Background = require("gesture.view.background")
local GestureBoard = require("gesture.view.board")
local mouse = require("gesture.view.mouse")

local vim = vim

local View = {}
View.__index = View
Expand All @@ -24,14 +21,11 @@ function View.render_input(self, inputs, gesture, can_match, show_board)
end

function View.focus(self, last_point)
mouse.click()

if not self._background:is_valid() then
return
local current_point = self._background.get_position()
if not current_point then
return nil
end

local current_point = self.current_point()

local last
if last_point == nil then
last = current_point
Expand All @@ -43,10 +37,8 @@ function View.focus(self, last_point)
return current_point
end

function View.current_point()
local x = vim.fn.wincol()
local y = vim.fn.winline()
return Point.new(x, y)
function View.current_point(self)
return self._background.get_position()
end

function View.close(self)
Expand Down
1 change: 1 addition & 0 deletions spec/lua/gesture/example.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
vim.opt.mouse = "a"
vim.opt.mousemoveevent = true

vim.keymap.set("n", "<LeftDrag>", [[<Cmd>lua require("gesture").draw()<CR>]], { silent = true })
vim.keymap.set("n", "<LeftRelease>", [[<Cmd>lua require("gesture").finish()<CR>]], { silent = true })
Expand Down
16 changes: 16 additions & 0 deletions spec/lua/gesture/init_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,22 @@ describe("gesture.draw()", function()
assert.match("can_match error", err)
assert.window_count(1)
end)

it("can draw by mouse cursor position if mousemoveevent is enabled", function()
vim.o.mousemoveevent = true

gesture.register({
name = "to bottom",
inputs = { gesture.down() },
action = "normal! G",
})

gesture.draw()
vim.api.nvim_input_mouse("left", "press", "", 0, vim.o.lines, 0)
gesture.draw()

assert.shown_in_view("DOWN")
end)
end)

describe("gesture.finish()", function()
Expand Down

0 comments on commit b0326ce

Please sign in to comment.