From 428edcde7068ab44040e19b43343741e5ca59770 Mon Sep 17 00:00:00 2001 From: Amanda Graven Date: Tue, 28 Nov 2023 21:05:33 +0100 Subject: [PATCH] feat(api): add forward and back mouse buttons --- runtime/doc/api.txt | 2 +- runtime/doc/news.txt | 2 ++ runtime/lua/vim/_meta/api.lua | 2 +- src/nvim/api/vim.c | 7 ++++++- src/nvim/os/input.c | 3 ++- test/functional/ui/mouse_spec.lua | 19 +++++++++++++++++++ 6 files changed, 31 insertions(+), 4 deletions(-) diff --git a/runtime/doc/api.txt b/runtime/doc/api.txt index 72ac357ac0..7cb6051020 100644 --- a/runtime/doc/api.txt +++ b/runtime/doc/api.txt @@ -1145,7 +1145,7 @@ nvim_input_mouse({button}, {action}, {modifier}, {grid}, {row}, {col}) Parameters: ~ • {button} Mouse button: one of "left", "right", "middle", "wheel", - "move". + "move", "x1", "x2". • {action} For ordinary buttons, one of "press", "drag", "release". For the wheel, one of "up", "down", "left", "right". Ignored for "move". diff --git a/runtime/doc/news.txt b/runtime/doc/news.txt index 7def21941b..90065349ef 100644 --- a/runtime/doc/news.txt +++ b/runtime/doc/news.txt @@ -268,6 +268,8 @@ The following new APIs and features were added. • 'completeopt' option supports "popup" flags to show extra information in in floating window. +• Added `x1` and `x2` mouse buttons as possible arguments to |nvim_input_mouse()| + ============================================================================== CHANGED FEATURES *news-changed* diff --git a/runtime/lua/vim/_meta/api.lua b/runtime/lua/vim/_meta/api.lua index c8afbd58dd..c8c3e1c3af 100644 --- a/runtime/lua/vim/_meta/api.lua +++ b/runtime/lua/vim/_meta/api.lua @@ -1375,7 +1375,7 @@ function vim.api.nvim_input(keys) end --- processed soon by the event loop. --- --- @param button string Mouse button: one of "left", "right", "middle", "wheel", ---- "move". +--- "move", "x1", "x2". --- @param action string For ordinary buttons, one of "press", "drag", "release". --- For the wheel, one of "up", "down", "left", "right". --- Ignored for "move". diff --git a/src/nvim/api/vim.c b/src/nvim/api/vim.c index 70e6b840de..aed286165a 100644 --- a/src/nvim/api/vim.c +++ b/src/nvim/api/vim.c @@ -345,7 +345,8 @@ Integer nvim_input(String keys) /// mouse input in a GUI. The deprecated pseudokey form /// ("") of |nvim_input()| has the same limitation. /// -/// @param button Mouse button: one of "left", "right", "middle", "wheel", "move". +/// @param button Mouse button: one of "left", "right", "middle", "wheel", "move", +/// "x1", "x2". /// @param action For ordinary buttons, one of "press", "drag", "release". /// For the wheel, one of "up", "down", "left", "right". Ignored for "move". /// @param modifier String of modifiers each represented by a single char. @@ -376,6 +377,10 @@ void nvim_input_mouse(String button, String action, String modifier, Integer gri code = KE_RIGHTMOUSE; } else if (strequal(button.data, "wheel")) { code = KE_MOUSEDOWN; + } else if (strequal(button.data, "x1")) { + code = KE_X1MOUSE; + } else if (strequal(button.data, "x2")) { + code = KE_X2MOUSE; } else if (strequal(button.data, "move")) { code = KE_MOUSEMOVE; } else { diff --git a/src/nvim/os/input.c b/src/nvim/os/input.c index 5b759e9d1e..ed4267dcbb 100644 --- a/src/nvim/os/input.c +++ b/src/nvim/os/input.c @@ -311,7 +311,8 @@ static uint8_t check_multiclick(int code, int grid, int row, int col) } // For click events the number of clicks is updated. - if (code == KE_LEFTMOUSE || code == KE_RIGHTMOUSE || code == KE_MIDDLEMOUSE) { + if (code == KE_LEFTMOUSE || code == KE_RIGHTMOUSE || code == KE_MIDDLEMOUSE + || code == KE_X1MOUSE || code == KE_X2MOUSE) { uint64_t mouse_time = os_hrtime(); // time of current mouse click (ns) // compute the time elapsed since the previous mouse click and // convert p_mouse from ms to ns diff --git a/test/functional/ui/mouse_spec.lua b/test/functional/ui/mouse_spec.lua index e408df7096..448b39994b 100644 --- a/test/functional/ui/mouse_spec.lua +++ b/test/functional/ui/mouse_spec.lua @@ -758,6 +758,25 @@ describe('ui/mouse/input', function() feed('') end) + it('x1 and x2 can be triggered by api', function() + meths.set_var('x1_pressed', 0) + meths.set_var('x1_released', 0) + meths.set_var('x2_pressed', 0) + meths.set_var('x2_released', 0) + command('nnoremap let g:x1_pressed += 1') + command('nnoremap let g:x1_released += 1') + command('nnoremap let g:x2_pressed += 1') + command('nnoremap let g:x2_released += 1') + meths.input_mouse('x1', 'press', '', 0, 0, 0) + meths.input_mouse('x1', 'release', '', 0, 0, 0) + meths.input_mouse('x2', 'press', '', 0, 0, 0) + meths.input_mouse('x2', 'release', '', 0, 0, 0) + eq(1, meths.get_var('x1_pressed'), 'x1 pressed once') + eq(1, meths.get_var('x1_released'), 'x1 released once') + eq(1, meths.get_var('x2_pressed'), 'x2 pressed once') + eq(1, meths.get_var('x2_released'), 'x2 released once') + end) + it('dragging vertical separator', function() screen:try_resize(45, 5) command('setlocal nowrap')