From b0326ce7928a7ab26078b642b03699a6691dedd8 Mon Sep 17 00:00:00 2001 From: notomo Date: Sat, 19 Aug 2023 21:02:15 +0900 Subject: [PATCH] Use getmousepos() if mousemoveevent is enabled --- README.md | 1 + doc/gesture.nvim.txt | 1 + lua/gesture/core/state.lua | 4 ++-- lua/gesture/view/background.lua | 29 ++++++++++++++++++++++++----- lua/gesture/view/init.lua | 18 +++++------------- spec/lua/gesture/example.lua | 1 + spec/lua/gesture/init_spec.lua | 16 ++++++++++++++++ 7 files changed, 50 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 65bbe88..f1609c6 100644 --- a/README.md +++ b/README.md @@ -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", "", [[lua require("gesture").draw()]], { silent = true }) vim.keymap.set("n", "", [[lua require("gesture").finish()]], { silent = true }) diff --git a/doc/gesture.nvim.txt b/doc/gesture.nvim.txt index b03bbef..fd82991 100644 --- a/doc/gesture.nvim.txt +++ b/doc/gesture.nvim.txt @@ -131,6 +131,7 @@ EXAMPLE *gesture.nvim-EXAMPLE* >lua vim.opt.mouse = "a" + vim.opt.mousemoveevent = true vim.keymap.set("n", "", [[lua require("gesture").draw()]], { silent = true }) vim.keymap.set("n", "", [[lua require("gesture").finish()]], { silent = true }) diff --git a/lua/gesture/core/state.lua b/lua/gesture/core/state.lua index 5e39ce2..849788d 100644 --- a/lua/gesture/core/state.lua +++ b/lua/gesture/core/state.lua @@ -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, @@ -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, diff --git a/lua/gesture/view/background.lua b/lua/gesture/view/background.lua index 3987a02..bd580e1 100644 --- a/lua/gesture/view/background.lua +++ b/lua/gesture/view/background.lua @@ -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") @@ -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 - 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 + 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, @@ -52,6 +70,7 @@ function Background.open(winblend) local ns = vim.api.nvim_create_namespace("gesture") local tbl = { + get_position = get_position, _window_id = window_id, _ns = ns, } @@ -59,7 +78,7 @@ function Background.open(winblend) 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 }) @@ -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 diff --git a/lua/gesture/view/init.lua b/lua/gesture/view/init.lua index d09e838..5c80252 100644 --- a/lua/gesture/view/init.lua +++ b/lua/gesture/view/init.lua @@ -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 @@ -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 @@ -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) diff --git a/spec/lua/gesture/example.lua b/spec/lua/gesture/example.lua index 6cbcf44..689fff5 100644 --- a/spec/lua/gesture/example.lua +++ b/spec/lua/gesture/example.lua @@ -1,4 +1,5 @@ vim.opt.mouse = "a" +vim.opt.mousemoveevent = true vim.keymap.set("n", "", [[lua require("gesture").draw()]], { silent = true }) vim.keymap.set("n", "", [[lua require("gesture").finish()]], { silent = true }) diff --git a/spec/lua/gesture/init_spec.lua b/spec/lua/gesture/init_spec.lua index acc4e8c..12ecc20 100644 --- a/spec/lua/gesture/init_spec.lua +++ b/spec/lua/gesture/init_spec.lua @@ -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()