Skip to content

Commit 4ec157c

Browse files
authored
Merge pull request #149 from dstein64/on_key_for_mouse3
Use vim.on_key for mouse functionality instead of mappings
2 parents 4daaf07 + 5ab73f9 commit 4ec157c

File tree

3 files changed

+285
-150
lines changed

3 files changed

+285
-150
lines changed

autoload/scrollview.vim

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,10 @@ let g:scrollview_ins_mode_buf_lines = 0
322322
" escaping.
323323
let g:scrollview_echo_string = v:null
324324

325+
" Keep track of the initial mouse settings. These are only used for nvim<0.11.
326+
let g:scrollview_init_mouse_primary = g:scrollview_mouse_primary
327+
let g:scrollview_init_mouse_secondary = g:scrollview_mouse_secondary
328+
325329
" *************************************************
326330
" * Versioning
327331
" *************************************************
@@ -432,6 +436,23 @@ endif
432436
" * Mappings
433437
" *************************************************
434438

439+
function! scrollview#HandleMouseFromMapping(button, is_primary) abort
440+
let l:button_repr = nvim_replace_termcodes(
441+
\ printf('<%smouse>', a:button), v:true, v:true, v:true)
442+
let l:packed = luaeval(
443+
\ '{require("scrollview").should_handle_mouse(_A)}', l:button_repr)
444+
let l:should_handle = l:packed[0]
445+
if l:should_handle
446+
let l:data = l:packed[1]
447+
call luaeval(
448+
\ 'require("scrollview").handle_mouse('
449+
\ .. '_A.button, _A.is_primary, _A.props, _A.mousepos)', l:data)
450+
else
451+
" Process the click as it would ordinarily be processed.
452+
call feedkeys(l:button_repr, 'ni')
453+
endif
454+
endfunction
455+
435456
function! s:SetUpMouseMappings(button, primary) abort
436457
if a:button isnot# v:null
437458
" Create a mouse mapping only if mappings don't already exist and "!" is
@@ -447,25 +468,28 @@ function! s:SetUpMouseMappings(button, primary) abort
447468
let l:button =
448469
\ strcharpart(l:button, 0, strchars(l:button, 1) - 1)
449470
endif
450-
" scrollview mouse handling is not supported in select-mode. #140
451-
for l:mapmode in ['n', 'x', 'i']
471+
for l:mapmode in ['n', 'v', 'i']
452472
execute printf(
453473
\ 'silent! %snoremap %s <silent> <%smouse>'
454-
\ .. ' <cmd>lua require("scrollview").handle_mouse("%s", %s)<cr>',
474+
\ .. ' <cmd>call scrollview#HandleMouseFromMapping("%s", %s)<cr>',
455475
\ l:mapmode,
456476
\ l:force ? '' : '<unique>',
457477
\ l:button,
458478
\ l:button,
459-
\ a:primary ? 'true' : 'false',
479+
\ a:primary,
460480
\ )
461481
endfor
462482
endif
463483
endfunction
464484

465-
call s:SetUpMouseMappings(g:scrollview_mouse_primary, v:true)
466-
" :popup doesn't work for nvim<0.8.
467-
if has('nvim-0.8')
468-
call s:SetUpMouseMappings(g:scrollview_mouse_secondary, v:false)
485+
" With Neovim 0.11, mouse functionality is handled with vim.on_key, not
486+
" mappings.
487+
if !has('nvim-0.11')
488+
call s:SetUpMouseMappings(g:scrollview_mouse_primary, v:true)
489+
" :popup doesn't work for nvim<0.8.
490+
if has('nvim-0.8')
491+
call s:SetUpMouseMappings(g:scrollview_mouse_secondary, v:false)
492+
endif
469493
endif
470494

471495
" Additional <plug> mappings are defined for convenience of creating

doc/scrollview.txt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ signs. The plugin is customizable (see |scrollview-configuration|).
2020
1. Requirements *scrollview-requirements*
2121

2222
* `nvim>=0.6`
23-
* Scrollbar mouse dragging requires mouse support (see |'mouse'|)
23+
* Scrollbar mouse functionality requires mouse support (see |'mouse'|)
2424
* Signs require `nvim>=0.9`
2525

2626
============================================================================
@@ -301,19 +301,19 @@ scrollview_mouse_primary *scrollview_mouse_primary*
301301
Possible values include `'left'`, `'middle'`, `'right'`,
302302
`'x1'`, and `'x2'`. These can be prepended with `'c-'` or
303303
`'m-'` for the control-key and alt-key variants (e.g.,
304-
`'c-left'` for control-left). An existing mapping will
305-
not be clobbered, unless `'!'` is added at the end (e.g.,
306-
`'left!'`). Set to `v:null` to disable the functionality.
307-
Defaults to `'left'`. Considered only when the plugin is
308-
loaded.
304+
`'c-left'` for control-left). Set to `v:null` to disable
305+
the functionality. Defaults to `'left'`. For `nvim<0.11`,
306+
an existing mapping will not be clobbered, unless `'!'`
307+
is added at the end (e.g., `'left!'`). For `nvim<0.11`,
308+
the option is considered only when the plugin is loaded.
309309

310310
scrollview_mouse_secondary *scrollview_mouse_secondary*
311311
|String| specifying the button for secondary mouse
312312
operations (clicking signs for additional information).
313313
See |scrollview_mouse_primary| for the possible values,
314-
including how `'c-'`, `'m-'`, `'!'`, and `v:null` can be
315-
utilized. Defaults to `'right'`. Considered only when the
316-
plugin is loaded.
314+
including how `'c-'`, `'m-'`, and `v:null` can be utilized,
315+
as well as the behavior specific to `nvim<0.11`. Defaults
316+
to `'right'`.
317317

318318
*scrollview_on_startup*
319319
scrollview_on_startup |Boolean| specifying whether scrollbars are enabled on

0 commit comments

Comments
 (0)