robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

robinmerge: 1dc9728daa1d8a2702

master
78.5 KiB2471 linesraw
1local t = require('test.testutil')
2local n = require('test.functional.testnvim')()
3local Screen = require('test.functional.ui.screen')
4
5local describe, it, before_each = t.describe, t.it, t.before_each
6local clear, feed, api = n.clear, n.feed, n.api
7local insert, feed_command = n.insert, n.feed_command
8local eq, fn = t.eq, n.fn
9local poke_eventloop = n.poke_eventloop
10local command = n.command
11local exec = n.exec
12
13describe('ui/mouse/input', function()
14  local function with_ext_multigrid(multigrid)
15    local screen
16
17    before_each(function()
18      clear()
19      api.nvim_set_option_value('mouse', 'a', {})
20      api.nvim_set_option_value('list', true, {})
21      -- NB: this is weird, but mostly irrelevant to the test
22      -- So I didn't bother to change it
23      command('set listchars=eol:$')
24      command('setl listchars=nbsp:x')
25
26      screen = Screen.new(25, 5, { ext_multigrid = multigrid })
27      screen:add_extra_attr_ids {
28        [100] = {
29          bold = true,
30          background = Screen.colors.LightGrey,
31          foreground = Screen.colors.Blue1,
32        },
33      }
34      command('set mousemodel=extend')
35      feed('itesting<cr>mouse<cr>support and selection<esc>')
36      screen:expect({
37        any = {
38          'testing                  ',
39          'mouse                    ',
40          'support and selectio%^n    ',
41        },
42      })
43    end)
44
45    it('single left click moves cursor', function()
46      feed('<LeftMouse><2,1>')
47      screen:expect({
48        any = 'mo%^use',
49        mouse_enabled = true,
50      })
51      feed('<LeftMouse><0,0>')
52      screen:expect({
53        any = '%^testing',
54      })
55    end)
56
57    it('middle click in Visual selection yanks, then puts', function()
58      command('let g:loaded_clipboard_provider = 1') -- Avoid clipboard.
59      feed('<LeftMouse><0,1>')
60      eq({ 2, 0 }, api.nvim_win_get_cursor(0))
61      feed('viw')
62      eq('v', fn.mode())
63      eq({ 2, 4 }, api.nvim_win_get_cursor(0))
64      feed('"a') -- User typed register "a.
65      feed('<MiddleMouse><3,0>')
66      eq('mouse', fn.getreg('a'))
67      eq('mouse', fn.getreg('"'))
68      eq('tesmouseting', api.nvim_get_current_line())
69    end)
70
71    it('shift-click search and CTRL-T pop execute within the click', function()
72      fn.setline(1, { 'foo bar', 'x foo y' })
73      feed('gg0')
74      feed('<S-LeftMouse><0,0>') -- "*": search for the shift-clicked word.
75      eq({ 2, 2 }, api.nvim_win_get_cursor(0))
76      eq('', api.nvim_get_vvar('errmsg'))
77      feed('<C-RightMouse><0,0>') -- "CTRL-T": empty tag stack raises E73.
78      eq('E73: Tag stack empty', api.nvim_get_vvar('errmsg'))
79    end)
80
81    it("in external ui works with unset 'mouse'", function()
82      api.nvim_set_option_value('mouse', '', {})
83      feed('<LeftMouse><2,1>')
84      screen:expect({
85        any = 'mo%^use',
86        mouse_enabled = false,
87      })
88      feed('<LeftMouse><0,0>')
89      screen:expect({
90        any = '%^testing',
91      })
92    end)
93
94    it('double left click enters visual mode', function()
95      feed('<LeftMouse><0,0>')
96      feed('<LeftRelease><0,0>')
97      feed('<LeftMouse><0,0>')
98      feed('<LeftRelease><0,0>')
99      screen:expect({
100        any = {
101          '{17:testin^g}',
102          'VISUAL',
103        },
104      })
105    end)
106
107    it('triple left click enters visual line mode', function()
108      feed('<LeftMouse><0,0>')
109      feed('<LeftRelease><0,0>')
110      feed('<LeftMouse><0,0>')
111      feed('<LeftRelease><0,0>')
112      feed('<LeftMouse><0,0>')
113      feed('<LeftRelease><0,0>')
114      screen:expect({
115        any = {
116          '{17:^testing}',
117          'VISUAL LINE',
118        },
119      })
120    end)
121
122    it('quadruple left click enters visual block mode', function()
123      feed('<LeftMouse><0,0>')
124      feed('<LeftRelease><0,0>')
125      feed('<LeftMouse><0,0>')
126      feed('<LeftRelease><0,0>')
127      feed('<LeftMouse><0,0>')
128      feed('<LeftRelease><0,0>')
129      feed('<LeftMouse><0,0>')
130      feed('<LeftRelease><0,0>')
131      screen:expect({
132        any = {
133          '{17:^t}esting',
134          'VISUAL BLOCK',
135        },
136      })
137    end)
138
139    it('double left click stays in normal mode if mouse does not contain v', function()
140      api.nvim_set_option_value('mouse', 'n', {})
141
142      feed('<LeftMouse><0,0>')
143      feed('<LeftRelease><0,0>')
144      feed('<LeftMouse><0,0>')
145      feed('<LeftRelease><0,0>')
146      screen:expect({
147        any = {
148          '%^testing',
149          'mouse',
150          'support and selection',
151        },
152        mode = 'normal',
153      })
154    end)
155
156    it('triple left click stays in normal mode if mouse does not contain v', function()
157      api.nvim_set_option_value('mouse', 'n', {})
158
159      feed('<LeftMouse><0,0>')
160      feed('<LeftRelease><0,0>')
161      feed('<LeftMouse><0,0>')
162      feed('<LeftRelease><0,0>')
163      feed('<LeftMouse><0,0>')
164      feed('<LeftRelease><0,0>')
165      screen:expect({
166        any = {
167          '%^testing',
168          'mouse',
169          'support and selection',
170        },
171        mode = 'normal',
172      })
173    end)
174
175    it('quadruple left click stays in normal mode if mouse does not contain v', function()
176      api.nvim_set_option_value('mouse', 'n', {})
177
178      feed('<LeftMouse><0,0>')
179      feed('<LeftRelease><0,0>')
180      feed('<LeftMouse><0,0>')
181      feed('<LeftRelease><0,0>')
182      feed('<LeftMouse><0,0>')
183      feed('<LeftRelease><0,0>')
184      feed('<LeftMouse><0,0>')
185      feed('<LeftRelease><0,0>')
186      screen:expect({
187        any = {
188          '%^testing',
189          'mouse',
190          'support and selection',
191        },
192        mode = 'normal',
193      })
194    end)
195
196    describe('tab drag', function()
197      it('in tabline on filler space moves tab to the end', function()
198        feed_command('%delete')
199        insert('this is foo')
200        feed_command('silent file foo | tabnew | file bar')
201        insert('this is bar')
202        screen:expect({
203          any = {
204            '{24: %+ foo }{5: %+ bar }{2:          }{24:X}',
205            'this is ba%^r{1:%$}',
206          },
207        })
208        feed('<LeftMouse><4,0>')
209        screen:expect({
210          any = {
211            '{5: %+ foo }{24: %+ bar }{2:          }{24:X}',
212            'this is fo%^o',
213          },
214        })
215        feed('<LeftDrag><14,0>')
216        screen:expect({
217          any = {
218            '{24: %+ bar }{5: %+ foo }{2:          }{24:X}',
219            'this is fo%^o',
220          },
221        })
222      end)
223
224      it('in tabline to the left moves tab left', function()
225        feed_command('%delete')
226        insert('this is foo')
227        feed_command('silent file foo | tabnew | file bar')
228        insert('this is bar')
229        screen:expect({
230          any = {
231            '{24: %+ foo }{5: %+ bar }{2:          }{24:X}',
232            'this is ba%^r{1:%$}',
233          },
234        })
235        feed('<LeftMouse><11,0>')
236        -- Prevent the case where screen:expect() with "unchanged" returns too early,
237        -- causing the click position to be overwritten by the next drag.
238        poke_eventloop()
239        screen:expect({
240          any = {
241            '{24: %+ foo }{5: %+ bar }{2:          }{24:X}',
242            'this is ba%^r{1:%$}',
243          },
244          unchanged = true,
245        })
246        feed('<LeftDrag><6,0>')
247        screen:expect({
248          any = {
249            '{5: %+ bar }{24: %+ foo }{2:          }{24:X}',
250            'this is ba%^r{1:%$}',
251          },
252        })
253      end)
254
255      it('in tabline to the right moves tab right', function()
256        feed_command('%delete')
257        insert('this is foo')
258        feed_command('silent file foo | tabnew | file bar')
259        insert('this is bar')
260        screen:expect({
261          any = {
262            '{24: %+ foo }{5: %+ bar }{2:          }{24:X}',
263            'this is ba%^r{1:%$}',
264          },
265        })
266        feed('<LeftMouse><4,0>')
267        screen:expect({
268          any = {
269            '{5: %+ foo }{24: %+ bar }{2:          }{24:X}',
270            'this is fo%^o',
271          },
272        })
273        feed('<LeftDrag><7,0>')
274        screen:expect({
275          any = {
276            '{24: %+ bar }{5: %+ foo }{2:          }{24:X}',
277            'this is fo%^o',
278          },
279        })
280      end)
281
282      it('out of tabline under filler space moves tab to the end', function()
283        feed_command('%delete')
284        insert('this is foo')
285        feed_command('silent file foo | tabnew | file bar')
286        insert('this is bar')
287        screen:expect({
288          any = {
289            '{24: %+ foo }{5: %+ bar }{2:          }{24:X}',
290            'this is ba%^r{1:%$}',
291          },
292        })
293        feed('<LeftMouse><4,0>')
294        screen:expect({
295          any = {
296            '{5: %+ foo }{24: %+ bar }{2:          }{24:X}',
297            'this is fo%^o',
298          },
299        })
300        feed('<LeftDrag><4,1>')
301        screen:expect({
302          any = {
303            '{5: %+ foo }{24: %+ bar }{2:          }{24:X}',
304            'this is fo%^o',
305          },
306          unchanged = true,
307        })
308        feed('<LeftDrag><14,1>')
309        screen:expect({
310          any = {
311            '{24: %+ bar }{5: %+ foo }{2:          }{24:X}',
312            'this is fo%^o',
313          },
314        })
315      end)
316
317      it('out of tabline to the left moves tab left', function()
318        feed_command('%delete')
319        insert('this is foo')
320        feed_command('silent file foo | tabnew | file bar')
321        insert('this is bar')
322        screen:expect({
323          any = {
324            '{24: %+ foo }{5: %+ bar }{2:          }{24:X}',
325            'this is ba%^r{1:%$}',
326          },
327        })
328        feed('<LeftMouse><11,0>')
329        -- Prevent the case where screen:expect() with "unchanged" returns too early,
330        -- causing the click position to be overwritten by the next drag.
331        poke_eventloop()
332        screen:expect({
333          any = {
334            '{24: %+ foo }{5: %+ bar }{2:          }{24:X}',
335            'this is ba%^r{1:%$}',
336          },
337          unchanged = true,
338        })
339        feed('<LeftDrag><11,1>')
340        screen:expect({
341          any = {
342            '{24: %+ foo }{5: %+ bar }{2:          }{24:X}',
343            'this is ba%^r{1:%$}',
344          },
345          unchanged = true,
346        })
347        feed('<LeftDrag><6,1>')
348        screen:expect({
349          any = {
350            '{5: %+ bar }{24: %+ foo }{2:          }{24:X}',
351            'this is ba%^r{1:%$}',
352          },
353        })
354      end)
355
356      it('out of tabline to the right moves tab right', function()
357        feed_command('%delete')
358        insert('this is foo')
359        feed_command('silent file foo | tabnew | file bar')
360        insert('this is bar')
361        screen:expect({
362          any = {
363            '{24: %+ foo }{5: %+ bar }{2:          }{24:X}',
364            'this is ba%^r{1:%$}',
365          },
366        })
367        feed('<LeftMouse><4,0>')
368        screen:expect({
369          any = {
370            '{5: %+ foo }{24: %+ bar }{2:          }{24:X}',
371            'this is fo%^o',
372          },
373        })
374        feed('<LeftDrag><4,1>')
375        screen:expect({
376          any = {
377            '{5: %+ foo }{24: %+ bar }{2:          }{24:X}',
378            'this is fo%^o',
379          },
380          unchanged = true,
381        })
382        feed('<LeftDrag><7,1>')
383        screen:expect({
384          any = {
385            '{24: %+ bar }{5: %+ foo }{2:          }{24:X}',
386            'this is fo%^o',
387          },
388        })
389      end)
390    end)
391
392    describe('tabline', function()
393      it('left click in default tabline (tabpage label) switches to tab', function()
394        feed_command('%delete')
395        insert('this is foo')
396        feed_command('silent file foo | tabnew | file bar')
397        insert('this is bar')
398        screen:expect({
399          any = {
400            '{24: %+ foo }{5: %+ bar }{2:          }{24:X}',
401            'this is ba%^r{1:%$}',
402          },
403        })
404        feed('<LeftMouse><4,0>')
405        screen:expect({
406          any = {
407            '{5: %+ foo }{24: %+ bar }{2:          }{24:X}',
408            'this is fo%^o',
409          },
410        })
411        feed('<LeftMouse><6,0>')
412        screen:expect_unchanged()
413        feed('<LeftMouse><10,0>')
414        screen:expect({
415          any = {
416            '{24: %+ foo }{5: %+ bar }{2:          }{24:X}',
417            'this is ba%^r{1:%$}',
418          },
419        })
420        feed('<LeftMouse><12,0>')
421        screen:expect_unchanged()
422      end)
423
424      it('left click in default tabline (blank space) switches tab', function()
425        feed_command('%delete')
426        insert('this is foo')
427        feed_command('silent file foo | tabnew | file bar')
428        insert('this is bar')
429        screen:expect({
430          any = {
431            '{24: %+ foo }{5: %+ bar }{2:          }{24:X}',
432            'this is ba%^r{1:%$}',
433          },
434        })
435        feed('<LeftMouse><20,0>')
436        screen:expect({
437          any = {
438            '{5: %+ foo }{24: %+ bar }{2:          }{24:X}',
439            'this is fo%^o',
440          },
441        })
442        feed('<LeftMouse><22,0>')
443        screen:expect({
444          any = {
445            '{24: %+ foo }{5: %+ bar }{2:          }{24:X}',
446            'this is ba%^r{1:%$}',
447          },
448        })
449      end)
450
451      it('left click in default tabline (close label) closes tab', function()
452        api.nvim_set_option_value('hidden', true, {})
453        feed_command('%delete')
454        insert('this is foo')
455        feed_command('silent file foo | tabnew | file bar')
456        insert('this is bar')
457        screen:expect({
458          any = {
459            '{24: %+ foo }{5: %+ bar }{2:          }{24:X}',
460            'this is ba%^r{1:%$}',
461          },
462        })
463        feed('<LeftMouse><24,0>')
464        screen:expect({
465          any = {
466            'this is fo%^o',
467          },
468          none = {
469            '{24:X}',
470            '%+ foo',
471            '%+ bar',
472          },
473        })
474      end)
475
476      it('double click in default tabline opens new tab before', function()
477        feed_command('%delete')
478        insert('this is foo')
479        feed_command('silent file foo | tabnew | file bar')
480        insert('this is bar')
481        screen:expect({
482          any = {
483            '{24: %+ foo }{5: %+ bar }{2:          }{24:X}',
484            'this is ba%^r{1:%$}',
485          },
486        })
487        feed('<2-LeftMouse><4,0>')
488        screen:expect({
489          any = {
490            '{5:  Name] }{24: %+ foo  %+ bar }{2:  }{24:X}|',
491            '{1:%^$}',
492          },
493        })
494        command('tabclose')
495        screen:expect({
496          any = {
497            '{5: %+ foo }{24: %+ bar }{2:          }{24:X}|',
498            'this is fo%^o',
499          },
500        })
501        feed('<2-LeftMouse><20,0>')
502        screen:expect({
503          any = {
504            '{24: %+ foo  %+ bar }{5:  Name] }{2:  }{24:X}',
505            '{1:%^$}',
506          },
507        })
508        command('tabclose')
509        screen:expect({
510          any = {
511            '{24: %+ foo }{5: %+ bar }{2:          }{24:X}',
512            'this is ba%^r{1:$}',
513          },
514        })
515        feed('<2-LeftMouse><10,0>')
516        screen:expect({
517          any = {
518            '{24: %+ foo }{5:  Name] }{24: %+ bar }{2:  }{24:X}',
519            '{1:%^$}',
520          },
521        })
522      end)
523
524      describe('%@ label', function()
525        before_each(function()
526          feed_command([[
527            function Test(...)
528              let g:reply = a:000
529              return copy(a:000)  " Check for memory leaks: return should be freed
530            endfunction
531          ]])
532          feed_command([[
533            function Test2(...)
534              return call('Test', a:000 + [2])
535            endfunction
536          ]])
537          api.nvim_set_option_value('tabline', '%@Test@test%X-%5@Test2@test2', {})
538          api.nvim_set_option_value('showtabline', 2, {})
539          screen:expect({
540            any = {
541              '{2:test%-test2               }',
542              'testing',
543              'mouse',
544              'support and selectio%^n',
545            },
546          })
547          api.nvim_set_var('reply', {})
548        end)
549
550        local check_reply = function(expected)
551          eq(expected, api.nvim_get_var('reply'))
552          api.nvim_set_var('reply', {})
553        end
554
555        local test_click = function(name, click_str, click_num, mouse_button, modifiers)
556          local function doit(do_click)
557            eq(1, fn.has('tablineat'))
558            do_click(0, 3)
559            check_reply({ 0, click_num, mouse_button, modifiers })
560            do_click(0, 4)
561            check_reply({})
562            do_click(0, 6)
563            check_reply({ 5, click_num, mouse_button, modifiers, 2 })
564            do_click(0, 13)
565            check_reply({ 5, click_num, mouse_button, modifiers, 2 })
566          end
567
568          it(name .. ' works (pseudokey)', function()
569            doit(function(row, col)
570              feed(click_str .. '<' .. col .. ',' .. row .. '>')
571            end)
572          end)
573
574          it(name .. ' works (nvim_input_mouse)', function()
575            doit(function(row, col)
576              local buttons = { l = 'left', m = 'middle', r = 'right' }
577              local modstr = (click_num > 1) and tostring(click_num) or ''
578              for char in string.gmatch(modifiers, '%w') do
579                modstr = modstr .. char .. '-' -- - not needed but should be accepted
580              end
581              api.nvim_input_mouse(buttons[mouse_button], 'press', modstr, 0, row, col)
582            end)
583          end)
584        end
585
586        test_click('single left click', '<LeftMouse>', 1, 'l', '    ')
587        test_click('shifted single left click', '<S-LeftMouse>', 1, 'l', 's   ')
588        test_click('shifted single left click with alt modifier', '<S-A-LeftMouse>', 1, 'l', 's a ')
589        test_click(
590          'shifted single left click with alt and ctrl modifiers',
591          '<S-C-A-LeftMouse>',
592          1,
593          'l',
594          'sca '
595        )
596        -- <C-RightMouse> does not work
597        test_click(
598          'shifted single right click with alt modifier',
599          '<S-A-RightMouse>',
600          1,
601          'r',
602          's a '
603        )
604        -- Modifiers do not work with MiddleMouse
605        test_click(
606          'shifted single middle click with alt and ctrl modifiers',
607          '<MiddleMouse>',
608          1,
609          'm',
610          '    '
611        )
612        -- Modifiers do not work with N-*Mouse
613        test_click('double left click', '<2-LeftMouse>', 2, 'l', '    ')
614        test_click('triple left click', '<3-LeftMouse>', 3, 'l', '    ')
615        test_click('quadruple left click', '<4-LeftMouse>', 4, 'l', '    ')
616        test_click('double right click', '<2-RightMouse>', 2, 'r', '    ')
617        test_click('triple right click', '<3-RightMouse>', 3, 'r', '    ')
618        test_click('quadruple right click', '<4-RightMouse>', 4, 'r', '    ')
619        test_click('double middle click', '<2-MiddleMouse>', 2, 'm', '    ')
620        test_click('triple middle click', '<3-MiddleMouse>', 3, 'm', '    ')
621        test_click('quadruple middle click', '<4-MiddleMouse>', 4, 'm', '    ')
622      end)
623    end)
624
625    it('left drag changes visual selection', function()
626      -- drag events must be preceded by a click
627      feed('<LeftMouse><2,1>')
628      screen:expect({
629        any = {
630          'testing',
631          'mo%^use',
632          'support and selection',
633        },
634      })
635      feed('<LeftDrag><4,1>')
636      screen:expect({
637        any = {
638          'testing',
639          'mo{17:us^e}',
640          'support and selection',
641          'VISUAL',
642        },
643      })
644      feed('<LeftDrag><2,2>')
645      screen:expect({
646        any = {
647          'testing',
648          'mo{17:use}',
649          '{17:su^p}port and selection',
650          'VISUAL',
651        },
652      })
653      feed('<LeftDrag><0,0>')
654      screen:expect({
655        any = {
656          '{17:^testing}',
657          '{17:mou}se ',
658          'support and selection',
659          'VISUAL',
660        },
661      })
662    end)
663
664    it('left drag moves cursor if mouse does not contain v', function()
665      api.nvim_set_option_value('mouse', 'n', {})
666
667      -- drag events must be preceded by a click
668      feed('<LeftMouse><2,1>')
669      screen:expect({
670        any = {
671          'testing',
672          'mo%^use',
673          'support and selection',
674        },
675        mode = 'normal',
676      })
677      feed('<LeftDrag><4,1>')
678      screen:expect({
679        any = {
680          'testing',
681          'mous%^e',
682          'support and selection',
683        },
684        mode = 'normal',
685      })
686      feed('<LeftDrag><2,2>')
687      screen:expect({
688        any = {
689          'testing',
690          'mouse',
691          'su%^pport and selection',
692        },
693        mode = 'normal',
694      })
695      feed('<LeftDrag><0,0>')
696      screen:expect({
697        any = {
698          '%^testing',
699          'mouse',
700          'support and selection',
701        },
702        mode = 'normal',
703      })
704    end)
705
706    it('left drag does not adjust existing visual selection if mouse does not contain v', function()
707      api.nvim_set_option_value('mouse', 'n', {})
708
709      feed('gg^vlj')
710      screen:expect({
711        any = {
712          '{17:testing}',
713          '{17:m^o}use',
714          'support and selection',
715          'VISUAL',
716        },
717      })
718
719      -- drag events must be preceded by a click
720      feed('<LeftMouse><2,1>')
721      screen:expect({
722        any = {
723          '{17:testing}',
724          '{17:m^o}use',
725          'support and selection',
726          'VISUAL',
727        },
728        unchanged = true,
729      })
730      feed('<LeftDrag><4,1>')
731      screen:expect({
732        any = {
733          '{17:testing}',
734          '{17:m^o}use',
735          'support and selection',
736          'VISUAL',
737        },
738        unchanged = true,
739      })
740      feed('<LeftDrag><2,2>')
741      screen:expect({
742        any = {
743          '{17:testing}',
744          '{17:m^o}use',
745          'support and selection',
746          'VISUAL',
747        },
748        unchanged = true,
749      })
750      feed('<LeftRelease>')
751      screen:expect({
752        any = {
753          '{17:testing}',
754          '{17:m^o}use',
755          'support and selection',
756          'VISUAL',
757        },
758        unchanged = true,
759      })
760    end)
761
762    it('left drag changes visual selection after tab click', function()
763      feed_command('silent file foo | tabnew | file bar')
764      insert('this is bar')
765      feed_command('tabprevious') -- go to first tab
766      screen:expect({
767        any = {
768          '{5: %+ foo }{24: %+ bar }{2:          }{24:X}|',
769          'testing',
770          'mouse',
771          'support and selectio%^n',
772          ':tabprevious',
773        },
774      })
775      feed('<LeftMouse><10,0><LeftRelease>') -- go to second tab
776      n.poke_eventloop()
777      feed('<LeftMouse><0,1>')
778      -- This text is hidden when using multigrid
779      local none = {
780        'mouse',
781        'support and selection',
782      }
783      if multigrid then
784        none = nil
785      end
786      screen:expect({
787        any = {
788          '{24: %+ foo }{5: %+ bar }{2:          }{24:X}',
789          '%^this is bar{1:%$}',
790          ':tabprevious',
791        },
792        none = none,
793      })
794      feed('<LeftDrag><4,1>')
795      screen:expect({
796        any = {
797          '{24: %+ foo }{5: %+ bar }{2:          }{24:X}',
798          '{17:this^ }is bar{1:%$}',
799          'VISUAL',
800        },
801      })
802    end)
803
804    it('left drag changes visual selection in split layout', function()
805      screen:try_resize(53, 14)
806      command('set mouse=a')
807      command('vsplit')
808      command('wincmd l')
809      command('below split')
810      command('enew')
811      feed('ifoo\nbar<esc>')
812      screen:expect({
813        any = {
814          'foo{1:%$}',
815          'ba%^r{1:%$}',
816        },
817      })
818      api.nvim_input_mouse('left', 'press', '', 0, 6, 27)
819      screen:expect({
820        any = {
821          '%^foo{1:%$}',
822          'bar{1:%$}',
823        },
824      })
825      api.nvim_input_mouse('left', 'drag', '', 0, 7, 30)
826      screen:expect({
827        any = {
828          '{17:foo}{100:%$}',
829          '{17:bar}{100:^$}',
830          'VISUAL',
831        },
832      })
833    end)
834
835    it('two clicks will enter VISUAL and dragging selects words', function()
836      feed('<LeftMouse><2,2>')
837      feed('<LeftRelease><2,2>')
838      feed('<LeftMouse><2,2>')
839      screen:expect({
840        any = {
841          'testing',
842          'mouse',
843          '{17:suppor^t} and selection',
844          'VISUAL',
845        },
846      })
847      feed('<LeftDrag><0,1>')
848      screen:expect({
849        any = {
850          'testing',
851          '{17:^mouse}',
852          '{17:support} and selection',
853          'VISUAL',
854        },
855      })
856      feed('<LeftDrag><4,0>')
857      screen:expect({
858        any = {
859          '{17:^testing}',
860          '{17:mouse}',
861          '{17:support} and selection',
862          'VISUAL',
863        },
864      })
865      feed('<LeftDrag><14,2>')
866      screen:expect({
867        any = {
868          'testing',
869          'mouse',
870          '{17:support and selectio^n}',
871          'VISUAL',
872        },
873      })
874    end)
875
876    it('three clicks will enter VISUAL LINE and dragging selects lines', function()
877      feed('<LeftMouse><2,2>')
878      feed('<LeftRelease><2,2>')
879      feed('<LeftMouse><2,2>')
880      feed('<LeftRelease><2,2>')
881      feed('<LeftMouse><2,2>')
882      screen:expect({
883        any = {
884          'testing',
885          'mouse',
886          '{17:su^pport and selection}',
887          'VISUAL LINE',
888        },
889      })
890      feed('<LeftDrag><0,1>')
891      screen:expect({
892        any = {
893          'testing',
894          '{17:^mouse}',
895          '{17:support and selection}',
896          'VISUAL LINE',
897        },
898      })
899      feed('<LeftDrag><4,0>')
900      screen:expect({
901        any = {
902          '{17:test^ing}',
903          '{17:mouse}',
904          '{17:support and selection}',
905          'VISUAL LINE',
906        },
907      })
908      feed('<LeftDrag><14,2>')
909      screen:expect({
910        any = {
911          'testing',
912          'mouse',
913          '{17:support and se^lection}',
914          'VISUAL LINE',
915        },
916      })
917    end)
918
919    it('four clicks will enter VISUAL BLOCK and dragging selects blockwise', function()
920      feed('<LeftMouse><2,2>')
921      feed('<LeftRelease><2,2>')
922      feed('<LeftMouse><2,2>')
923      feed('<LeftRelease><2,2>')
924      feed('<LeftMouse><2,2>')
925      feed('<LeftRelease><2,2>')
926      feed('<LeftMouse><2,2>')
927      screen:expect({
928        any = {
929          'testing',
930          'mouse',
931          'su{17:^p}port and selection',
932          'VISUAL BLOCK',
933        },
934      })
935      feed('<LeftDrag><0,1>')
936      screen:expect({
937        any = {
938          'testing',
939          '{17:^mou}se',
940          '{17:sup}port and selection',
941          'VISUAL BLOCK',
942        },
943      })
944      feed('<LeftDrag><4,0>')
945      screen:expect({
946        any = {
947          'te{17:st^i}ng',
948          'mo{17:use}',
949          'su{17:ppo}rt and selection',
950          'VISUAL BLOCK',
951        },
952      })
953      feed('<LeftDrag><14,2>')
954      screen:expect({
955        any = {
956          'testing',
957          'mouse',
958          'su{17:pport and se^l}ection',
959          'VISUAL BLOCK',
960        },
961      })
962    end)
963
964    it('right click extends visual selection to the clicked location', function()
965      feed('<LeftMouse><0,0>')
966      screen:expect({
967        any = {
968          '%^testing',
969          'mouse',
970          'support and selection',
971        },
972      })
973      feed('<RightMouse><2,2>')
974      screen:expect({
975        any = {
976          '{17:testing}',
977          '{17:mouse}',
978          '{17:su^p}port and selection',
979          'VISUAL',
980        },
981      })
982    end)
983
984    it('ctrl + left click places a multicursor', function()
985      command('hi MCursor guifg=Black guibg=LightGrey')
986      feed('<C-LeftMouse><0,0>')
987      -- A cursor at the click; the primary cursor did not move.
988      screen:expect({
989        any = { '{17:t}esting', 'support and selectio^n' },
990      })
991      feed('q<BS>') -- remove all cursors
992    end)
993
994    it('g + left click will search for a tag', function()
995      api.nvim_set_option_value('tags', './non-existent-tags-file', {})
996      feed('g<LeftMouse><0,0>')
997      screen:expect({
998        any = {
999          '{9:E433: No tags file}',
1000          '{9:E426: Tag not found: test}',
1001          '{9:ing}',
1002          '{6:Press ENTER or type comma}',
1003          '{6:nd to continue}%^',
1004        },
1005      })
1006      feed('<cr>')
1007    end)
1008
1009    it('x1 and x2 can be triggered by api', function()
1010      api.nvim_set_var('x1_pressed', 0)
1011      api.nvim_set_var('x1_released', 0)
1012      api.nvim_set_var('x2_pressed', 0)
1013      api.nvim_set_var('x2_released', 0)
1014      command('nnoremap <X1Mouse> <Cmd>let g:x1_pressed += 1<CR>')
1015      command('nnoremap <X1Release> <Cmd>let g:x1_released += 1<CR>')
1016      command('nnoremap <X2Mouse> <Cmd>let g:x2_pressed += 1<CR>')
1017      command('nnoremap <X2Release> <Cmd>let g:x2_released += 1<CR>')
1018      api.nvim_input_mouse('x1', 'press', '', 0, 0, 0)
1019      api.nvim_input_mouse('x1', 'release', '', 0, 0, 0)
1020      api.nvim_input_mouse('x2', 'press', '', 0, 0, 0)
1021      api.nvim_input_mouse('x2', 'release', '', 0, 0, 0)
1022      eq(1, api.nvim_get_var('x1_pressed'), 'x1 pressed once')
1023      eq(1, api.nvim_get_var('x1_released'), 'x1 released once')
1024      eq(1, api.nvim_get_var('x2_pressed'), 'x2 pressed once')
1025      eq(1, api.nvim_get_var('x2_released'), 'x2 released once')
1026    end)
1027
1028    it('dragging vertical separator', function()
1029      screen:try_resize(45, 5)
1030      command('setlocal nowrap')
1031      local oldwin = api.nvim_get_current_win()
1032      command('rightbelow vnew')
1033      screen:expect({
1034        any = {
1035          'testing               ',
1036          'mouse                 ',
1037          'support and selection ',
1038          '{1:%^%$}                     ',
1039          '{2:%[No Name%] %[%+%]          }{3:%[No Name%]             }',
1040        },
1041      })
1042      api.nvim_input_mouse('left', 'press', '', 0, 0, 22)
1043      poke_eventloop()
1044      api.nvim_input_mouse('left', 'drag', '', 0, 1, 12)
1045      screen:expect({
1046        any = {
1047          'testing     ',
1048          'mouse       ',
1049          'support and ',
1050          '{1:%^$}                               ',
1051          '{2:< Name%] %[%+%]  }{3:%[No Name%]                       }',
1052        },
1053      })
1054      api.nvim_input_mouse('left', 'drag', '', 0, 2, 2)
1055      screen:expect({
1056        any = {
1057          'te',
1058          'mo',
1059          'su',
1060          '{1:%^%$}                                         ',
1061          '{2:<  }{3:%[No Name%]                                 }',
1062        },
1063      })
1064      api.nvim_input_mouse('left', 'release', '', 0, 2, 2)
1065      api.nvim_set_option_value('statuscolumn', 'foobar', { win = oldwin })
1066      screen:expect({
1067        any = {
1068          '{8:fo}',
1069          '{1:%^%$}                                         ',
1070          '{2:<  }{3:%[No Name%]                                 }',
1071        },
1072      })
1073      api.nvim_input_mouse('left', 'press', '', 0, 0, 2)
1074      poke_eventloop()
1075      api.nvim_input_mouse('left', 'drag', '', 0, 1, 12)
1076      screen:expect({
1077        any = {
1078          '{8:foobar}testin',
1079          '{8:foobar}mouse ',
1080          '{8:foobar}suppor',
1081          '{1:%^%$}                               ',
1082          '{2:< Name%] %[%+%]  }{3:%[No Name%]                       }',
1083        },
1084      })
1085      api.nvim_input_mouse('left', 'drag', '', 0, 2, 22)
1086      screen:expect({
1087        any = {
1088          '{8:foobar}testing         ',
1089          '{8:foobar}mouse           ',
1090          '{8:foobar}support and sele',
1091          '{1:%^%$}                     ',
1092          '{2:%[No Name%] %[%+%]          }{3:%[No Name%]             }',
1093        },
1094      })
1095      api.nvim_input_mouse('left', 'release', '', 0, 2, 22)
1096    end)
1097
1098    local function wheel(use_api)
1099      feed('ggdG')
1100      insert([[
1101      Inserting
1102      text
1103      with
1104      many
1105      lines
1106      to
1107      test
1108      mouse scrolling
1109      ]])
1110      screen:try_resize(53, 14)
1111      feed('k')
1112      api.nvim_set_option_value('statuscolumn', 'C', { win = api.nvim_get_current_win() })
1113      feed_command('sp')
1114      api.nvim_set_option_value('statuscolumn', 'B', { win = api.nvim_get_current_win() })
1115      feed_command('vsp')
1116      api.nvim_set_option_value('statuscolumn', 'A', { win = api.nvim_get_current_win() })
1117      screen:expect({
1118        any = {
1119          '{8:A}lines                    ',
1120          '{8:A}to                       ',
1121          '{8:A}test                     ',
1122          '{8:A}^mouse scrolling          ',
1123          '{8:B}lines                    ',
1124          '{8:B}to                       ',
1125          '{8:B}test                     ',
1126          '{8:B}mouse scrolling          ',
1127          '{3:%[No Name%] %[%+%]              }{2:%[No Name%] %[%+%]             }',
1128          '{8:C}to                                                  ',
1129          '{8:C}test                                                ',
1130          '{8:C}mouse scrolling                                     ',
1131          '{2:%[No Name%] %[%+%]                                        }',
1132        },
1133        none = {
1134          '{8:A}Inserting',
1135          '{8:A}text',
1136          '{8:A}with',
1137          '{8:A}many',
1138          '{8:B}Inserting',
1139          '{8:B}text',
1140          '{8:B}with',
1141          '{8:B}many',
1142          '{8:C}Inserting',
1143          '{8:C}text',
1144          '{8:C}with',
1145          '{8:C}many',
1146          '{8:C}lines',
1147        },
1148      })
1149      if use_api then
1150        api.nvim_input_mouse('wheel', 'down', '', 0, 0, 0)
1151      else
1152        feed('<ScrollWheelDown><0,0>')
1153      end
1154      screen:expect({
1155        any = {
1156          '{8:A}^mouse scrolling          ',
1157          '{8:B}lines                    ',
1158          '{8:B}to                       ',
1159          '{8:B}test                     ',
1160          '{8:B}mouse scrolling          ',
1161          '{3:%[No Name%] %[%+%]              }{2:%[No Name%] %[%+%]             }',
1162          '{8:C}to                                                  ',
1163          '{8:C}test                                                ',
1164          '{8:C}mouse scrolling                                     ',
1165          '{2:%[No Name%] %[%+%]                                        }',
1166        },
1167        none = {
1168          '{8:A}lines',
1169          '{8:A}to',
1170          '{8:A}test',
1171          '{8:B}Inserting',
1172          '{8:B}text',
1173          '{8:B}with',
1174          '{8:B}many',
1175          '{8:C}Inserting',
1176          '{8:C}text',
1177          '{8:C}with',
1178          '{8:C}many',
1179          '{8:C}lines',
1180        },
1181      })
1182      if use_api then
1183        api.nvim_input_mouse('wheel', 'up', '', 0, 0, 27)
1184      else
1185        feed('<ScrollWheelUp><27,0>')
1186      end
1187      screen:expect({
1188        any = {
1189          '{8:A}^mouse scrolling          ',
1190          '{8:B}lines                    ',
1191          '{8:B}to                       ',
1192          '{8:B}test                     ',
1193          '{3:%[No Name%] %[%+%]              }{2:%[No Name%] %[%+%]             }',
1194          '{8:C}to                                                  ',
1195          '{8:C}test                                                ',
1196          '{8:C}mouse scrolling                                     ',
1197          '{2:%[No Name%] %[%+%]                                        }',
1198        },
1199        none = {
1200          '{8:A}Inserting',
1201          '{8:A}text',
1202          '{8:A}with',
1203          '{8:A}many',
1204          '{8:A}lines',
1205          '{8:A}to',
1206          '{8:A}test',
1207          '{8:B}Inserting',
1208          '{8:B}mouse scrolling',
1209          '{8:C}Inserting',
1210          '{8:C}text',
1211          '{8:C}with',
1212          '{8:C}many',
1213          '{8:C}lines',
1214        },
1215      })
1216      if use_api then
1217        api.nvim_input_mouse('wheel', 'up', '', 0, 7, 27)
1218        api.nvim_input_mouse('wheel', 'up', '', 0, 7, 27)
1219      else
1220        feed('<ScrollWheelUp><27,7><ScrollWheelUp>')
1221      end
1222      screen:expect({
1223        any = {
1224          '{8:A}^mouse scrolling          ',
1225          '{8:B}lines                    ',
1226          '{8:B}to                       ',
1227          '{8:B}test                     ',
1228          '{3:%[No Name%] %[%+%]              }{2:%[No Name%] %[%+%]             }',
1229          '{8:C}Inserting                                           ',
1230          '{8:C}text                                                ',
1231          '{8:C}with                                                ',
1232          '{8:C}many                                                ',
1233          '{8:C}lines                                               ',
1234          '{2:%[No Name%] %[%+%]                                        }',
1235        },
1236        none = {
1237          '{8:A}Inserting',
1238          '{8:A}text',
1239          '{8:A}with',
1240          '{8:A}many',
1241          '{8:A}lines',
1242          '{8:A}to',
1243          '{8:A}test',
1244          '{8:B}Inserting',
1245          '{8:B}mouse scrolling',
1246          '{8:C}to',
1247          '{8:C}test',
1248          '{8:C}mouse scrolling',
1249        },
1250      })
1251    end
1252
1253    it('mouse wheel will target the hovered window (pseudokey)', function()
1254      wheel(false)
1255    end)
1256
1257    it('mouse wheel will target the hovered window (nvim_input_mouse)', function()
1258      wheel(true)
1259    end)
1260
1261    it('horizontal scrolling (pseudokey)', function()
1262      command('set sidescroll=0')
1263      feed('<esc>:set nowrap<cr>')
1264
1265      feed('a <esc>17Ab<esc>3Ab<esc>')
1266      screen:expect({
1267        any = {
1268          'bbbbbbbbbbbbbbb%^b        ',
1269        },
1270      })
1271
1272      feed('<ScrollWheelLeft><0,0>')
1273      screen:expect({
1274        any = {
1275          'n bbbbbbbbbbbbbbbbbbb%^b   ',
1276        },
1277      })
1278
1279      feed('^<ScrollWheelRight><0,0>')
1280      screen:expect({
1281        any = {
1282          'g                        ',
1283          '%^t and selection bbbbbbbbb',
1284        },
1285      })
1286    end)
1287
1288    it('horizontal scrolling (nvim_input_mouse)', function()
1289      command('set sidescroll=0')
1290      feed('<esc>:set nowrap<cr>')
1291
1292      feed('a <esc>17Ab<esc>3Ab<esc>')
1293      screen:expect({
1294        any = {
1295          'bbbbbbbbbbbbbbb%^b        ',
1296        },
1297      })
1298
1299      api.nvim_input_mouse('wheel', 'left', '', 0, 0, 27)
1300      screen:expect({
1301        any = {
1302          'n bbbbbbbbbbbbbbbbbbb%^b   ',
1303        },
1304      })
1305
1306      feed('^')
1307      api.nvim_input_mouse('wheel', 'right', '', 0, 0, 0)
1308      screen:expect({
1309        any = {
1310          'g                        ',
1311          '%^t and selection bbbbbbbbb',
1312        },
1313      })
1314    end)
1315
1316    it("'sidescrolloff' applies to horizontal scrolling", function()
1317      command('set nowrap')
1318      command('set sidescrolloff=4')
1319
1320      feed('I <esc>020ib<esc>0')
1321      screen:expect({
1322        any = {
1323          'testing                  ',
1324          'mouse                    ',
1325          '%^bbbbbbbbbbbbbbbbbbbb supp',
1326        },
1327      })
1328
1329      api.nvim_input_mouse('wheel', 'right', '', 0, 0, 27)
1330      screen:expect({
1331        any = {
1332          'g                        ',
1333          '                         ',
1334          'bbbb%^bbbbbbbbbb support an',
1335        },
1336      })
1337
1338      -- window-local 'sidescrolloff' should override global value. #21162
1339      command('setlocal sidescrolloff=2')
1340      feed('0')
1341      screen:expect({
1342        any = {
1343          'testing                  ',
1344          'mouse                    ',
1345          '%^bbbbbbbbbbbbbbbbbbbb supp',
1346        },
1347      })
1348
1349      api.nvim_input_mouse('wheel', 'right', '', 0, 0, 27)
1350      screen:expect({
1351        any = {
1352          'g                        ',
1353          '                         ',
1354          'bb%^bbbbbbbbbbbb support an',
1355        },
1356      })
1357    end)
1358
1359    local function test_mouse_click_conceal()
1360      it('(level 1) click on non-wrapped lines', function()
1361        feed_command('let &conceallevel=1', 'echo')
1362
1363        feed('<esc><LeftMouse><0,0>')
1364        screen:expect({
1365          any = {
1366            '%^Section{1:>>--->--->---}{14: }t1{14: } ',
1367            '{1:>--->--->---}  {14: }t2{14: } {14: }t3{14: } {14: }',
1368            '{14:>} 私は猫が大好き{1:>---}{14: X } {1:>}',
1369          },
1370        })
1371
1372        feed('<esc><LeftMouse><1,0>')
1373        screen:expect({
1374          any = {
1375            'S%^ection{1:>>--->--->---}{14: }t1{14: } ',
1376            '{1:>--->--->---}  {14: }t2{14: } {14: }t3{14: } {14: }',
1377            '{14:>} 私は猫が大好き{1:>---}{14: X } {1:>}',
1378          },
1379        })
1380
1381        feed('<esc><LeftMouse><21,0>')
1382        screen:expect({
1383          any = {
1384            'Section{1:>>--->--->---}{14: }%^t1{14: } ',
1385            '{1:>--->--->---}  {14: }t2{14: } {14: }t3{14: } {14: }',
1386            '{14:>} 私は猫が大好き{1:>---}{14: X } {1:>}',
1387          },
1388        })
1389
1390        feed('<esc><LeftMouse><21,1>')
1391        screen:expect({
1392          any = {
1393            'Section{1:>>--->--->---}{14: }t1{14: } ',
1394            '{1:>--->--->---}  {14: }t2{14: } {14: }t%^3{14: } {14: }',
1395            '{14:>} 私は猫が大好き{1:>---}{14: X } {1:>}',
1396          },
1397        })
1398
1399        feed('<esc><LeftMouse><0,2>')
1400        screen:expect({
1401          any = {
1402            'Section{1:>>--->--->---}{14: }t1{14: } ',
1403            '{1:>--->--->---}  {14: }t2{14: } {14: }t3{14: } {14: }',
1404            '{14:%^>} 私は猫が大好き{1:>---}{14: X } {1:>}',
1405          },
1406        })
1407
1408        feed('<esc><LeftMouse><7,2>')
1409        screen:expect({
1410          any = {
1411            'Section{1:>>--->--->---}{14: }t1{14: } ',
1412            '{1:>--->--->---}  {14: }t2{14: } {14: }t3{14: } {14: }',
1413            '{14:>} 私は%^猫が大好き{1:>---}{14: X } {1:>}',
1414          },
1415        })
1416
1417        feed('<esc><LeftMouse><21,2>')
1418        screen:expect({
1419          any = {
1420            'Section{1:>>--->--->---}{14: }t1{14: } ',
1421            '{1:>--->--->---}  {14: }t2{14: } {14: }t3{14: } {14: }',
1422            '{14:>} 私は猫が大好き{1:>---}{14: %^X } {1:>}',
1423          },
1424        })
1425      end) -- level 1 - non wrapped
1426
1427      it('(level 1) click on wrapped lines', function()
1428        feed_command('let &conceallevel=1', 'let &wrap=1', 'echo')
1429
1430        feed('<esc><LeftMouse><24,1>')
1431        screen:expect({
1432          any = {
1433            'Section{1:>>--->--->---}{14: }t1{14: } ',
1434            '{1:>--->--->---}  {14: }t2{14: } {14: }t3{14: } {14:%^ }',
1435            't4{14: }                      ',
1436            '{14:>} 私は猫が大好き{1:>---}{14: X}   ',
1437            '{14: } ✨🐈✨                 ',
1438          },
1439        })
1440
1441        feed('<esc><LeftMouse><0,2>')
1442        screen:expect({
1443          any = {
1444            'Section{1:>>--->--->---}{14: }t1{14: } ',
1445            '{1:>--->--->---}  {14: }t2{14: } {14: }t3{14: } {14: }',
1446            '%^t4{14: }                      ',
1447            '{14:>} 私は猫が大好き{1:>---}{14: X}   ',
1448            '{14: } ✨🐈✨                 ',
1449          },
1450        })
1451
1452        feed('<esc><LeftMouse><8,3>')
1453        screen:expect({
1454          any = {
1455            'Section{1:>>--->--->---}{14: }t1{14: } ',
1456            '{1:>--->--->---}  {14: }t2{14: } {14: }t3{14: } {14: }',
1457            't4{14: }                      ',
1458            '{14:>} 私は猫%^が大好き{1:>---}{14: X}   ',
1459            '{14: } ✨🐈✨                 ',
1460          },
1461        })
1462
1463        feed('<esc><LeftMouse><21,3>')
1464        screen:expect({
1465          any = {
1466            'Section{1:>>--->--->---}{14: }t1{14: } ',
1467            '{1:>--->--->---}  {14: }t2{14: } {14: }t3{14: } {14: }',
1468            't4{14: }                      ',
1469            '{14:>} 私は猫が大好き{1:>---}{14: %^X}   ',
1470            '{14: } ✨🐈✨                 ',
1471          },
1472        })
1473
1474        feed('<esc><LeftMouse><4,4>')
1475        screen:expect({
1476          any = {
1477            'Section{1:>>--->--->---}{14: }t1{14: } ',
1478            '{1:>--->--->---}  {14: }t2{14: } {14: }t3{14: } {14: }',
1479            't4{14: }                      ',
1480            '{14:>} 私は猫が大好き{1:>---}{14: X}   ',
1481            '{14: } ✨%^🐈✨                 ',
1482          },
1483        })
1484      end) -- level 1 - wrapped
1485
1486      it('(level 2) click on non-wrapped lines', function()
1487        feed_command('let &conceallevel=2', 'echo')
1488
1489        feed('<esc><LeftMouse><20,0>')
1490        screen:expect({
1491          any = {
1492            'Section{1:>>--->--->---}%^t1   ',
1493            '{1:>--->--->---}  t2 t3 t4   ',
1494            '{14:>} 私は猫が大好き{1:>---}{14:X} ✨{1:>}',
1495          },
1496        })
1497
1498        feed('<esc><LeftMouse><14,1>')
1499        screen:expect({
1500          any = {
1501            'Section{1:>>--->--->---}t1   ',
1502            '{1:>--->--->---}  %^t2 t3 t4   ',
1503            '{14:>} 私は猫が大好き{1:>---}{14:X} ✨{1:>}',
1504          },
1505        })
1506
1507        feed('<esc><LeftMouse><18,1>')
1508        screen:expect({
1509          any = {
1510            'Section{1:>>--->--->---}t1   ',
1511            '{1:>--->--->---}  t2 t%^3 t4   ',
1512            '{14:>} 私は猫が大好き{1:>---}{14:X} ✨{1:>}',
1513          },
1514        })
1515
1516        feed('<esc><LeftMouse><0,2>') -- Weirdness
1517        screen:expect({
1518          any = {
1519            'Section{1:>>--->--->---}t1   ',
1520            '{1:>--->--->---}  t2 t3 t4   ',
1521            '{14:%^>} 私は猫が大好き{1:>---}{14:X} ✨{1:>}',
1522          },
1523        })
1524
1525        feed('<esc><LeftMouse><8,2>')
1526        screen:expect({
1527          any = {
1528            'Section{1:>>--->--->---}t1   ',
1529            '{1:>--->--->---}  t2 t3 t4   ',
1530            '{14:>} 私は猫%^が大好き{1:>---}{14:X} ✨{1:>}',
1531          },
1532        })
1533
1534        feed('<esc><LeftMouse><20,2>')
1535        screen:expect({
1536          any = {
1537            'Section{1:>>--->--->---}t1   ',
1538            '{1:>--->--->---}  t2 t3 t4   ',
1539            '{14:>} 私は猫が大好き{1:>---}{14:%^X} ✨{1:>}',
1540          },
1541        })
1542      end) -- level 2 - non wrapped
1543
1544      it('(level 2) click on non-wrapped lines (insert mode)', function()
1545        feed_command('let &conceallevel=2', 'echo')
1546
1547        feed('<esc>i<LeftMouse><20,0>')
1548        screen:expect({
1549          any = {
1550            'Section{1:>>--->--->---}%^t1   ',
1551            '{1:>--->--->---}  t2 t3 t4   ',
1552            '{14:>} 私は猫が大好き{1:>---}{14:X} ✨{1:>}',
1553          },
1554        })
1555
1556        feed('<LeftMouse><14,1>')
1557        screen:expect({
1558          any = {
1559            'Section{1:>>--->--->---}t1   ',
1560            '{1:>--->--->---}  %^t2 t3 t4   ',
1561            '{14:>} 私は猫が大好き{1:>---}{14:X} ✨{1:>}',
1562          },
1563        })
1564
1565        feed('<LeftMouse><18,1>')
1566        screen:expect({
1567          any = {
1568            'Section{1:>>--->--->---}t1   ',
1569            '{1:>--->--->---}  t2 t%^3 t4   ',
1570            '{14:>} 私は猫が大好き{1:>---}{14:X} ✨{1:>}',
1571          },
1572        })
1573
1574        feed('<LeftMouse><0,2>') -- Weirdness
1575        screen:expect({
1576          any = {
1577            'Section{1:>>--->--->---}t1   ',
1578            '{1:>--->--->---}  t2 t3 t4   ',
1579            '{14:%^>} 私は猫が大好き{1:>---}{14:X} ✨{1:>}',
1580          },
1581        })
1582
1583        feed('<LeftMouse><8,2>')
1584        screen:expect({
1585          any = {
1586            'Section{1:>>--->--->---}t1   ',
1587            '{1:>--->--->---}  t2 t3 t4   ',
1588            '{14:>} 私は猫%^が大好き{1:>---}{14:X} ✨{1:>}',
1589          },
1590        })
1591
1592        feed('<LeftMouse><20,2>')
1593        screen:expect({
1594          any = {
1595            'Section{1:>>--->--->---}t1   ',
1596            '{1:>--->--->---}  t2 t3 t4   ',
1597            '{14:>} 私は猫が大好き{1:>---}{14:%^X} ✨{1:>}',
1598          },
1599        })
1600      end) -- level 2 - non wrapped (insert mode)
1601
1602      it('(level 2) click on wrapped lines', function()
1603        feed_command('let &conceallevel=2', 'let &wrap=1', 'echo')
1604
1605        feed('<esc><LeftMouse><20,0>')
1606        screen:expect({
1607          any = {
1608            'Section{1:>>--->--->---}%^t1   ',
1609            '{1:>--->--->---}  t2 t3      ',
1610            't4                       ',
1611            '{14:>} 私は猫が大好き{1:>---}{14:X}    ',
1612            ' ✨🐈✨                  ',
1613          },
1614        })
1615
1616        feed('<esc><LeftMouse><14,1>')
1617        screen:expect({
1618          any = {
1619            'Section{1:>>--->--->---}t1   ',
1620            '{1:>--->--->---}  %^t2 t3      ',
1621            't4                       ',
1622            '{14:>} 私は猫が大好き{1:>---}{14:X}    ',
1623            ' ✨🐈✨                  ',
1624          },
1625        })
1626
1627        feed('<esc><LeftMouse><18,1>')
1628        screen:expect({
1629          any = {
1630            'Section{1:>>--->--->---}t1   ',
1631            '{1:>--->--->---}  t2 t%^3      ',
1632            't4                       ',
1633            '{14:>} 私は猫が大好き{1:>---}{14:X}    ',
1634            ' ✨🐈✨                  ',
1635          },
1636        })
1637
1638        -- NOTE: The click would ideally be on the 't' in 't4', but wrapping
1639        -- caused the invisible '*' right before 't4' to remain on the previous
1640        -- screen line.  This is being treated as expected because fixing this is
1641        -- out of scope for mouse clicks.  Should the wrapping behavior of
1642        -- concealed characters change in the future, this case should be
1643        -- reevaluated.
1644        feed('<esc><LeftMouse><0,2>')
1645        screen:expect({
1646          any = {
1647            'Section{1:>>--->--->---}t1   ',
1648            '{1:>--->--->---}  t2 t3 %^     ',
1649            't4                       ',
1650            '{14:>} 私は猫が大好き{1:>---}{14:X}    ',
1651            ' ✨🐈✨                  ',
1652          },
1653        })
1654
1655        feed('<esc><LeftMouse><1,2>')
1656        screen:expect({
1657          any = {
1658            'Section{1:>>--->--->---}t1   ',
1659            '{1:>--->--->---}  t2 t3      ',
1660            't%^4                       ',
1661            '{14:>} 私は猫が大好き{1:>---}{14:X}    ',
1662            ' ✨🐈✨                  ',
1663          },
1664        })
1665
1666        feed('<esc><LeftMouse><0,3>')
1667        screen:expect({
1668          any = {
1669            'Section{1:>>--->--->---}t1   ',
1670            '{1:>--->--->---}  t2 t3      ',
1671            't4                       ',
1672            '{14:%^>} 私は猫が大好き{1:>---}{14:X}    ',
1673            ' ✨🐈✨                  ',
1674          },
1675        })
1676
1677        feed('<esc><LeftMouse><20,3>')
1678        screen:expect({
1679          any = {
1680            'Section{1:>>--->--->---}t1   ',
1681            '{1:>--->--->---}  t2 t3      ',
1682            't4                       ',
1683            '{14:>} 私は猫が大好き{1:>---}{14:%^X}    ',
1684            ' ✨🐈✨                  ',
1685          },
1686        })
1687
1688        feed('<esc><LeftMouse><1,4>')
1689        screen:expect({
1690          any = {
1691            'Section{1:>>--->--->---}t1   ',
1692            '{1:>--->--->---}  t2 t3      ',
1693            't4                       ',
1694            '{14:>} 私は猫が大好き{1:>---}{14:X}    ',
1695            '%^✨🐈✨                 ',
1696          },
1697        })
1698
1699        feed('<esc><LeftMouse><5,4>')
1700        screen:expect({
1701          any = {
1702            'Section{1:>>--->--->---}t1   ',
1703            '{1:>--->--->---}  t2 t3      ',
1704            't4                       ',
1705            '{14:>} 私は猫が大好き{1:>---}{14:X}    ',
1706            '✨🐈%^✨                  ',
1707          },
1708        })
1709      end) -- level 2 - wrapped
1710
1711      it('(level 3) click on non-wrapped lines', function()
1712        feed_command('let &conceallevel=3', 'echo')
1713
1714        feed('<esc><LeftMouse><0,2>')
1715        screen:expect({
1716          any = {
1717            'Section{1:>>--->--->---}t1   ',
1718            '{1:>--->--->---}  t2 t3 t4   ',
1719            '%^ 私は猫が大好き{1:>----} ✨🐈',
1720          },
1721        })
1722
1723        feed('<esc><LeftMouse><1,2>')
1724        screen:expect({
1725          any = {
1726            'Section{1:>>--->--->---}t1   ',
1727            '{1:>--->--->---}  t2 t3 t4   ',
1728            ' %^私は猫が大好き{1:>----} ✨🐈',
1729          },
1730        })
1731
1732        feed('<esc><LeftMouse><13,2>')
1733        screen:expect({
1734          any = {
1735            'Section{1:>>--->--->---}t1   ',
1736            '{1:>--->--->---}  t2 t3 t4   ',
1737            ' 私は猫が大好%^き{1:>----} ✨🐈',
1738          },
1739        })
1740
1741        feed('<esc><LeftMouse><20,2>')
1742        feed('zH') -- FIXME: unnecessary horizontal scrolling
1743        screen:expect({
1744          any = {
1745            'Section{1:>>--->--->---}t1   ',
1746            '{1:>--->--->---}  t2 t3 t4   ',
1747            ' 私は猫が大好き{1:>----}%^ ✨🐈',
1748          },
1749        })
1750      end) -- level 3 - non wrapped
1751
1752      it('(level 3) click on wrapped lines', function()
1753        feed_command('let &conceallevel=3', 'let &wrap=1', 'echo')
1754
1755        feed('<esc><LeftMouse><14,1>')
1756        screen:expect({
1757          any = {
1758            'Section{1:>>--->--->---}t1   ',
1759            '{1:>--->--->---}  %^t2 t3      ',
1760            't4                       ',
1761            ' 私は猫が大好き{1:>----}     ',
1762            ' ✨🐈✨                  ',
1763          },
1764        })
1765
1766        feed('<esc><LeftMouse><18,1>')
1767        screen:expect({
1768          any = {
1769            'Section{1:>>--->--->---}t1   ',
1770            '{1:>--->--->---}  t2 t%^3      ',
1771            't4                       ',
1772            ' 私は猫が大好き{1:>----}     ',
1773            ' ✨🐈✨                  ',
1774          },
1775        })
1776
1777        feed('<esc><LeftMouse><1,2>')
1778        screen:expect({
1779          any = {
1780            'Section{1:>>--->--->---}t1   ',
1781            '{1:>--->--->---}  t2 t3      ',
1782            't%^4                       ',
1783            ' 私は猫が大好き{1:>----}     ',
1784            ' ✨🐈✨                  ',
1785          },
1786        })
1787
1788        feed('<esc><LeftMouse><0,3>')
1789        screen:expect({
1790          any = {
1791            'Section{1:>>--->--->---}t1   ',
1792            '{1:>--->--->---}  t2 t3      ',
1793            't4                       ',
1794            '%^ 私は猫が大好き{1:>----}     ',
1795            ' ✨🐈✨                  ',
1796          },
1797        })
1798
1799        feed('<esc><LeftMouse><20,3>')
1800        screen:expect({
1801          any = {
1802            'Section{1:>>--->--->---}t1   ',
1803            '{1:>--->--->---}  t2 t3      ',
1804            't4                       ',
1805            ' 私は猫が大好き{1:>----}%^     ',
1806            ' ✨🐈✨                  ',
1807          },
1808        })
1809
1810        feed('<esc><LeftMouse><1,4>')
1811        screen:expect({
1812          any = {
1813            'Section{1:>>--->--->---}t1   ',
1814            '{1:>--->--->---}  t2 t3      ',
1815            't4                       ',
1816            ' 私は猫が大好き{1:>----}     ',
1817            ' %^✨🐈✨                  ',
1818          },
1819        })
1820
1821        feed('<esc><LeftMouse><3,4>')
1822        screen:expect({
1823          any = {
1824            'Section{1:>>--->--->---}t1   ',
1825            '{1:>--->--->---}  t2 t3      ',
1826            't4                       ',
1827            ' 私は猫が大好き{1:>----}     ',
1828            ' ✨%^🐈✨                  ',
1829          },
1830        })
1831
1832        feed('<esc><LeftMouse><5,4>')
1833        screen:expect({
1834          any = {
1835            'Section{1:>>--->--->---}t1   ',
1836            '{1:>--->--->---}  t2 t3      ',
1837            't4                       ',
1838            ' 私は猫が大好き{1:>----}     ',
1839            ' ✨🐈%^✨                  ',
1840          },
1841        })
1842      end) -- level 3 - wrapped
1843    end
1844
1845    describe('on concealed text', function()
1846      -- Helpful for reading the test expectations:
1847      -- :match Error /\^/
1848
1849      before_each(function()
1850        screen:try_resize(25, 7)
1851        feed('ggdG')
1852
1853        command([[setlocal concealcursor=ni nowrap shiftwidth=2 tabstop=4 list listchars=tab:>-]])
1854        command([[highlight link X0 Normal]])
1855        command([[highlight link X1 NonText]])
1856        command([[highlight link X2 NonText]])
1857        command([[highlight link X3 NonText]])
1858
1859        -- First column is there to retain the tabs.
1860        insert([[
1861        |Section				*t1*
1862        |			  *t2* *t3* *t4*
1863        |x 私は猫が大好き	*cats* ✨🐈✨
1864        ]])
1865
1866        feed('gg<c-v>Gxgg')
1867      end)
1868
1869      describe('(syntax)', function()
1870        before_each(function()
1871          command([[syntax region X0 matchgroup=X1 start=/\*/ end=/\*/ concealends contains=X2]])
1872          command([[syntax match X2 /cats/ conceal cchar=X contained]])
1873          command([[syntax match X3 /\n\@<=x/ conceal cchar=>]])
1874        end)
1875        test_mouse_click_conceal()
1876      end)
1877
1878      describe('(matchadd())', function()
1879        before_each(function()
1880          fn.matchadd('Conceal', [[\*]])
1881          fn.matchadd('Conceal', [[cats]], 10, -1, { conceal = 'X' })
1882          fn.matchadd('Conceal', [[\n\@<=x]], 10, -1, { conceal = '>' })
1883        end)
1884        test_mouse_click_conceal()
1885      end)
1886
1887      describe('(extmarks)', function()
1888        before_each(function()
1889          local ns = api.nvim_create_namespace('conceal')
1890          api.nvim_buf_set_extmark(0, ns, 0, 11, { end_col = 12, conceal = '' })
1891          api.nvim_buf_set_extmark(0, ns, 0, 14, { end_col = 15, conceal = '' })
1892          api.nvim_buf_set_extmark(0, ns, 1, 5, { end_col = 6, conceal = '' })
1893          api.nvim_buf_set_extmark(0, ns, 1, 8, { end_col = 9, conceal = '' })
1894          api.nvim_buf_set_extmark(0, ns, 1, 10, { end_col = 11, conceal = '' })
1895          api.nvim_buf_set_extmark(0, ns, 1, 13, { end_col = 14, conceal = '' })
1896          api.nvim_buf_set_extmark(0, ns, 1, 15, { end_col = 16, conceal = '' })
1897          api.nvim_buf_set_extmark(0, ns, 1, 18, { end_col = 19, conceal = '' })
1898          api.nvim_buf_set_extmark(0, ns, 2, 24, { end_col = 25, conceal = '' })
1899          api.nvim_buf_set_extmark(0, ns, 2, 29, { end_col = 30, conceal = '' })
1900          api.nvim_buf_set_extmark(0, ns, 2, 25, { end_col = 29, conceal = 'X' })
1901          api.nvim_buf_set_extmark(0, ns, 2, 0, { end_col = 1, conceal = '>' })
1902        end)
1903        test_mouse_click_conceal()
1904      end)
1905    end)
1906
1907    it('virtual text does not change cursor placement on concealed line', function()
1908      command('%delete')
1909      insert('aaaaaaaaaa|hidden|bbbbbbbbbb|hidden|cccccccccc')
1910      command('syntax match test /|hidden|/ conceal cchar=X')
1911      command('set conceallevel=2 concealcursor=n virtualedit=all')
1912      screen:expect({
1913        any = {
1914          'aaaaaaaaaa{14:X}bbbbbbb       ',
1915          'bbb{14:X}ccccccccc%^c           ',
1916        },
1917      })
1918      api.nvim_input_mouse('left', 'press', '', 0, 0, 22)
1919      screen:expect({
1920        any = {
1921          'aaaaaaaaaa{14:X}bbbbbb%^b       ',
1922          'bbb{14:X}cccccccccc           ',
1923        },
1924      })
1925      api.nvim_input_mouse('left', 'press', '', 0, 1, 16)
1926      screen:expect({
1927        any = {
1928          'aaaaaaaaaa{14:X}bbbbbbb       ',
1929          'bbb{14:X}cccccccccc  %^         ',
1930        },
1931      })
1932
1933      api.nvim_buf_set_extmark(0, api.nvim_create_namespace(''), 0, 0, {
1934        virt_text = { { '?', 'ErrorMsg' } },
1935        virt_text_pos = 'right_align',
1936        virt_text_repeat_linebreak = true,
1937      })
1938      screen:expect({
1939        any = {
1940          'aaaaaaaaaa{14:X}bbbbbbb      {9:%?}',
1941          'bbb{14:X}cccccccccc  %^        {9:%?}',
1942        },
1943      })
1944      api.nvim_input_mouse('left', 'press', '', 0, 0, 22)
1945      screen:expect({
1946        any = {
1947          'aaaaaaaaaa{14:X}bbbbbb%^b      {9:%?}',
1948          'bbb{14:X}cccccccccc          {9:%?}',
1949        },
1950      })
1951      api.nvim_input_mouse('left', 'press', '', 0, 1, 16)
1952      screen:expect({
1953        any = {
1954          'aaaaaaaaaa{14:X}bbbbbbb      {9:%?}',
1955          'bbb{14:X}cccccccccc  %^        {9:%?}',
1956        },
1957      })
1958    end)
1959
1960    it("mouse click on window separator in statusline doesn't crash", function()
1961      api.nvim_set_option_value('winwidth', 1, {})
1962      api.nvim_set_option_value('statusline', '%f', {})
1963
1964      command('vsplit')
1965      command('redraw')
1966
1967      local lines = api.nvim_get_option_value('lines', {})
1968      local columns = api.nvim_get_option_value('columns', {})
1969
1970      api.nvim_input_mouse('left', 'press', '', 0, lines - 1, math.floor(columns / 2))
1971      command('redraw')
1972    end)
1973
1974    it('getmousepos() works correctly', function()
1975      local winwidth = api.nvim_get_option_value('winwidth', {})
1976      -- Set winwidth=1 so that window sizes don't change.
1977      api.nvim_set_option_value('winwidth', 1, {})
1978      command('tabedit')
1979      local tabpage = api.nvim_get_current_tabpage()
1980      insert('hello')
1981      command('vsplit')
1982      local opts = {
1983        relative = 'editor',
1984        width = 12,
1985        height = 1,
1986        col = 8,
1987        row = 1,
1988        anchor = 'NW',
1989        style = 'minimal',
1990        border = 'single',
1991        focusable = 1,
1992      }
1993      local float = api.nvim_open_win(api.nvim_get_current_buf(), false, opts)
1994      command('redraw')
1995      local lines = api.nvim_get_option_value('lines', {})
1996      local columns = api.nvim_get_option_value('columns', {})
1997
1998      -- Test that screenrow and screencol are set properly for all positions.
1999      for row = 0, lines - 1 do
2000        for col = 0, columns - 1 do
2001          -- Skip the X button that would close the tab.
2002          if row ~= 0 or col ~= columns - 1 then
2003            api.nvim_input_mouse('left', 'press', '', 0, row, col)
2004            api.nvim_set_current_tabpage(tabpage)
2005            local mousepos = fn.getmousepos()
2006            eq(row + 1, mousepos.screenrow)
2007            eq(col + 1, mousepos.screencol)
2008            -- All other values should be 0 when clicking on the command line.
2009            if row == lines - 1 then
2010              eq(0, mousepos.winid)
2011              eq(0, mousepos.winrow)
2012              eq(0, mousepos.wincol)
2013              eq(0, mousepos.line)
2014              eq(0, mousepos.column)
2015              eq(0, mousepos.coladd)
2016            end
2017          end
2018        end
2019      end
2020
2021      -- Test that mouse position values are properly set for the floating window
2022      -- with a border. 1 is added to the height and width to account for the
2023      -- border.
2024      for win_row = 0, opts.height + 1 do
2025        for win_col = 0, opts.width + 1 do
2026          local row = win_row + opts.row
2027          local col = win_col + opts.col
2028          api.nvim_input_mouse('left', 'press', '', 0, row, col)
2029          local mousepos = fn.getmousepos()
2030          eq(float, mousepos.winid)
2031          eq(win_row + 1, mousepos.winrow)
2032          eq(win_col + 1, mousepos.wincol)
2033          local line = 0
2034          local column = 0
2035          local coladd = 0
2036          if
2037            win_row > 0
2038            and win_row < opts.height + 1
2039            and win_col > 0
2040            and win_col < opts.width + 1
2041          then
2042            -- Because of border, win_row and win_col don't need to be
2043            -- incremented by 1.
2044            line = math.min(win_row, fn.line('$'))
2045            column = math.min(win_col, #fn.getline(line) + 1)
2046            coladd = win_col - column
2047          end
2048          eq(line, mousepos.line)
2049          eq(column, mousepos.column)
2050          eq(coladd, mousepos.coladd)
2051        end
2052      end
2053
2054      -- Test that mouse position values are properly set for the floating
2055      -- window, after removing the border.
2056      opts.border = 'none'
2057      api.nvim_win_set_config(float, opts)
2058      command('redraw')
2059      for win_row = 0, opts.height - 1 do
2060        for win_col = 0, opts.width - 1 do
2061          local row = win_row + opts.row
2062          local col = win_col + opts.col
2063          api.nvim_input_mouse('left', 'press', '', 0, row, col)
2064          local mousepos = fn.getmousepos()
2065          eq(float, mousepos.winid)
2066          eq(win_row + 1, mousepos.winrow)
2067          eq(win_col + 1, mousepos.wincol)
2068          local line = math.min(win_row + 1, fn.line('$'))
2069          local column = math.min(win_col + 1, #fn.getline(line) + 1)
2070          local coladd = win_col + 1 - column
2071          eq(line, mousepos.line)
2072          eq(column, mousepos.column)
2073          eq(coladd, mousepos.coladd)
2074        end
2075      end
2076
2077      -- Test that mouse position values are properly set for ordinary windows.
2078      -- Set the float to be unfocusable instead of closing, to additionally test
2079      -- that getmousepos() does not consider unfocusable floats. (see discussion
2080      -- in PR #14937 for details).
2081      opts.focusable = false
2082      api.nvim_win_set_config(float, opts)
2083      command('redraw')
2084      for nr = 1, 2 do
2085        for win_row = 0, fn.winheight(nr) - 1 do
2086          for win_col = 0, fn.winwidth(nr) - 1 do
2087            local row = win_row + fn.win_screenpos(nr)[1] - 1
2088            local col = win_col + fn.win_screenpos(nr)[2] - 1
2089            api.nvim_input_mouse('left', 'press', '', 0, row, col)
2090            local mousepos = fn.getmousepos()
2091            eq(fn.win_getid(nr), mousepos.winid)
2092            eq(win_row + 1, mousepos.winrow)
2093            eq(win_col + 1, mousepos.wincol)
2094            local line = math.min(win_row + 1, fn.line('$'))
2095            local column = math.min(win_col + 1, #fn.getline(line) + 1)
2096            local coladd = win_col + 1 - column
2097            eq(line, mousepos.line)
2098            eq(column, mousepos.column)
2099            eq(coladd, mousepos.coladd)
2100          end
2101        end
2102      end
2103
2104      -- Restore state and release mouse.
2105      command('tabclose!')
2106      api.nvim_set_option_value('winwidth', winwidth, {})
2107      api.nvim_input_mouse('left', 'release', '', 0, 0, 0)
2108    end)
2109
2110    it('scroll keys are not translated into multiclicks and can be mapped #6211 #6989', function()
2111      api.nvim_set_var('mouse_up', 0)
2112      api.nvim_set_var('mouse_up2', 0)
2113      command('nnoremap <ScrollWheelUp> <Cmd>let g:mouse_up += 1<CR>')
2114      command('nnoremap <2-ScrollWheelUp> <Cmd>let g:mouse_up2 += 1<CR>')
2115      feed('<ScrollWheelUp><0,0>')
2116      feed('<ScrollWheelUp><0,0>')
2117      api.nvim_input_mouse('wheel', 'up', '', 0, 0, 0)
2118      api.nvim_input_mouse('wheel', 'up', '', 0, 0, 0)
2119      eq(4, api.nvim_get_var('mouse_up'))
2120      eq(0, api.nvim_get_var('mouse_up2'))
2121    end)
2122
2123    it('<MouseMove> to different locations can be mapped', function()
2124      api.nvim_set_var('mouse_move', 0)
2125      api.nvim_set_var('mouse_move2', 0)
2126      command('nnoremap <MouseMove> <Cmd>let g:mouse_move += 1<CR>')
2127      command('nnoremap <2-MouseMove> <Cmd>let g:mouse_move2 += 1<CR>')
2128      feed('<MouseMove><1,0>')
2129      feed('<MouseMove><2,0>')
2130      api.nvim_input_mouse('move', '', '', 0, 0, 3)
2131      api.nvim_input_mouse('move', '', '', 0, 0, 4)
2132      eq(4, api.nvim_get_var('mouse_move'))
2133      eq(0, api.nvim_get_var('mouse_move2'))
2134    end)
2135
2136    it('<MouseMove> to same location does not generate events #31103', function()
2137      api.nvim_set_var('mouse_move', 0)
2138      api.nvim_set_var('mouse_move2', 0)
2139      command('nnoremap <MouseMove> <Cmd>let g:mouse_move += 1<CR>')
2140      command('nnoremap <2-MouseMove> <Cmd>let g:mouse_move2 += 1<CR>')
2141      api.nvim_input_mouse('move', '', '', 0, 0, 3)
2142      eq(1, api.nvim_get_var('mouse_move'))
2143      eq(0, api.nvim_get_var('mouse_move2'))
2144      feed('<MouseMove><3,0>')
2145      feed('<MouseMove><3,0>')
2146      api.nvim_input_mouse('move', '', '', 0, 0, 3)
2147      api.nvim_input_mouse('move', '', '', 0, 0, 3)
2148      eq(1, api.nvim_get_var('mouse_move'))
2149      eq(0, api.nvim_get_var('mouse_move2'))
2150      eq({ mode = 'n', blocking = false }, api.nvim_get_mode())
2151      feed('<MouseMove><3,0><Insert>')
2152      eq(1, api.nvim_get_var('mouse_move'))
2153      eq(0, api.nvim_get_var('mouse_move2'))
2154      eq({ mode = 'i', blocking = false }, api.nvim_get_mode())
2155    end)
2156
2157    it('dragging at same location does not generate events', function()
2158      api.nvim_set_var('left_click', 0)
2159      api.nvim_set_var('left_drag', 0)
2160      api.nvim_set_var('left_release', 0)
2161      command('nnoremap <LeftMouse> <Cmd>let g:left_click += 1<CR>')
2162      command('nnoremap <LeftDrag> <Cmd>let g:left_drag += 1<CR>')
2163      command('nnoremap <LeftRelease> <Cmd>let g:left_release += 1<CR>')
2164      api.nvim_input_mouse('left', 'press', '', 0, 0, 3)
2165      api.nvim_input_mouse('left', 'drag', '', 0, 0, 3)
2166      api.nvim_input_mouse('left', 'drag', '', 0, 0, 3)
2167      feed('<LeftDrag><3,0>')
2168      feed('<LeftDrag><3,0>')
2169      eq({ 1, 0, 0 }, n.eval('[g:left_click, g:left_drag, g:left_release]'))
2170      api.nvim_input_mouse('left', 'drag', '', 0, 1, 3)
2171      api.nvim_input_mouse('left', 'drag', '', 0, 1, 3)
2172      feed('<LeftDrag><3,1>')
2173      feed('<LeftDrag><3,1>')
2174      eq({ 1, 1, 0 }, n.eval('[g:left_click, g:left_drag, g:left_release]'))
2175      api.nvim_input_mouse('left', 'drag', '', 0, 2, 3)
2176      api.nvim_input_mouse('left', 'drag', '', 0, 2, 3)
2177      eq({ 1, 2, 0 }, n.eval('[g:left_click, g:left_drag, g:left_release]'))
2178      feed('<LeftDrag><3,3>')
2179      feed('<LeftDrag><3,3>')
2180      eq({ 1, 3, 0 }, n.eval('[g:left_click, g:left_drag, g:left_release]'))
2181      api.nvim_input_mouse('left', 'release', '', 0, 3, 3)
2182      eq({ 1, 3, 1 }, n.eval('[g:left_click, g:left_drag, g:left_release]'))
2183    end)
2184
2185    describe('click event at different position resets multiclick count', function()
2186      before_each(function()
2187        api.nvim_set_var('events', {})
2188        command('nnoremap <LeftMouse> <Cmd>let g:events += [1]<CR>')
2189        command('nnoremap <2-LeftMouse> <Cmd>let g:events += [2]<CR>')
2190        command('nnoremap <3-LeftMouse> <Cmd>let g:events += [3]<CR>')
2191        command('nnoremap <4-LeftMouse> <Cmd>let g:events += [4]<CR>')
2192        command('nnoremap <LeftDrag> <Cmd>let g:events += [-1]<CR>')
2193        command('nnoremap <2-LeftDrag> <Cmd>let g:events += [-2]<CR>')
2194        command('nnoremap <3-LeftDrag> <Cmd>let g:events += [-3]<CR>')
2195        command('nnoremap <4-LeftDrag> <Cmd>let g:events += [-4]<CR>')
2196        api.nvim_input_mouse('left', 'press', '', 0, 0, 3)
2197        api.nvim_input_mouse('left', 'release', '', 0, 0, 3)
2198        api.nvim_input_mouse('left', 'press', '', 0, 0, 3)
2199      end)
2200
2201      it('with drag events in between', function()
2202        api.nvim_input_mouse('left', 'drag', '', 0, 1, 3)
2203        api.nvim_input_mouse('left', 'release', '', 0, 1, 3)
2204        api.nvim_input_mouse('left', 'press', '', 0, 1, 3)
2205        api.nvim_input_mouse('left', 'drag', '', 0, 2, 3)
2206        api.nvim_input_mouse('left', 'drag', '', 0, 3, 3)
2207        for _ = 1, 3 do
2208          api.nvim_input_mouse('left', 'release', '', 0, 3, 3)
2209          api.nvim_input_mouse('left', 'press', '', 0, 3, 3)
2210        end
2211        api.nvim_input_mouse('left', 'drag', '', 0, 4, 3)
2212        api.nvim_input_mouse('left', 'release', '', 0, 4, 3)
2213        eq({ 1, 2, -2, 1, -1, -1, 1, 2, 3, -3 }, api.nvim_get_var('events'))
2214      end)
2215
2216      it('with move events in between', function()
2217        api.nvim_input_mouse('left', 'release', '', 0, 0, 3)
2218        api.nvim_input_mouse('move', '', '', 0, 1, 3)
2219        api.nvim_input_mouse('left', 'press', '', 0, 1, 3)
2220        api.nvim_input_mouse('left', 'release', '', 0, 1, 3)
2221        api.nvim_input_mouse('move', '', '', 0, 2, 3)
2222        for _ = 1, 3 do
2223          api.nvim_input_mouse('left', 'press', '', 0, 2, 3)
2224          api.nvim_input_mouse('left', 'release', '', 0, 2, 3)
2225        end
2226        api.nvim_input_mouse('move', '', '', 0, 3, 3)
2227        for _ = 1, 4 do
2228          api.nvim_input_mouse('left', 'press', '', 0, 3, 3)
2229          api.nvim_input_mouse('left', 'release', '', 0, 3, 3)
2230        end
2231        eq({ 1, 2, 1, 1, 2, 3, 1, 2, 3, 4 }, api.nvim_get_var('events'))
2232      end)
2233    end)
2234
2235    it('feeding <MouseMove> in Normal mode does not use uninitialized memory #19480', function()
2236      feed('<MouseMove>')
2237      n.poke_eventloop()
2238      n.assert_alive()
2239    end)
2240
2241    it('mousemodel=popup_setpos', function()
2242      screen:try_resize(80, 24)
2243      exec([[
2244        5new
2245        call setline(1, ['the dish ran away with the spoon',
2246              \ 'the cow jumped over the moon' ])
2247
2248        set mouse=a mousemodel=popup_setpos
2249
2250        aunmenu PopUp
2251        nmenu PopUp.foo :let g:menustr = 'foo'<CR>
2252        nmenu PopUp.bar :let g:menustr = 'bar'<CR>
2253        nmenu PopUp.baz :let g:menustr = 'baz'<CR>
2254        vmenu PopUp.foo y:<C-U>let g:menustr = 'foo'<CR>
2255        vmenu PopUp.bar y:<C-U>let g:menustr = 'bar'<CR>
2256        vmenu PopUp.baz y:<C-U>let g:menustr = 'baz'<CR>
2257      ]])
2258
2259      api.nvim_win_set_cursor(0, { 1, 0 })
2260      api.nvim_input_mouse('right', 'press', '', 0, 0, 4)
2261      api.nvim_input_mouse('right', 'release', '', 0, 0, 4)
2262      feed('<Down><Down><CR>')
2263      eq('bar', api.nvim_get_var('menustr'))
2264      eq({ 1, 4 }, api.nvim_win_get_cursor(0))
2265
2266      -- Test for right click in visual mode inside the selection
2267      fn.setreg('"', '')
2268      api.nvim_win_set_cursor(0, { 1, 9 })
2269      feed('vee')
2270      api.nvim_input_mouse('right', 'press', '', 0, 0, 11)
2271      api.nvim_input_mouse('right', 'release', '', 0, 0, 11)
2272      feed('<Down><CR>')
2273      eq({ 1, 9 }, api.nvim_win_get_cursor(0))
2274      eq('ran away', fn.getreg('"'))
2275
2276      -- Test for right click in visual mode right before the selection
2277      fn.setreg('"', '')
2278      api.nvim_win_set_cursor(0, { 1, 9 })
2279      feed('vee')
2280      api.nvim_input_mouse('right', 'press', '', 0, 0, 8)
2281      api.nvim_input_mouse('right', 'release', '', 0, 0, 8)
2282      feed('<Down><CR>')
2283      eq({ 1, 8 }, api.nvim_win_get_cursor(0))
2284      eq('', fn.getreg('"'))
2285
2286      -- Test for right click in visual mode right after the selection
2287      fn.setreg('"', '')
2288      api.nvim_win_set_cursor(0, { 1, 9 })
2289      feed('vee')
2290      api.nvim_input_mouse('right', 'press', '', 0, 0, 17)
2291      api.nvim_input_mouse('right', 'release', '', 0, 0, 17)
2292      feed('<Down><CR>')
2293      eq({ 1, 17 }, api.nvim_win_get_cursor(0))
2294      eq('', fn.getreg('"'))
2295
2296      -- Test for right click in block-wise visual mode inside the selection
2297      fn.setreg('"', '')
2298      api.nvim_win_set_cursor(0, { 1, 15 })
2299      feed('<C-V>j3l')
2300      api.nvim_input_mouse('right', 'press', '', 0, 1, 16)
2301      api.nvim_input_mouse('right', 'release', '', 0, 1, 16)
2302      feed('<Down><CR>')
2303      eq({ 1, 15 }, api.nvim_win_get_cursor(0))
2304      eq('\0224', fn.getregtype('"'))
2305
2306      -- Test for right click in block-wise visual mode outside the selection
2307      fn.setreg('"', '')
2308      api.nvim_win_set_cursor(0, { 1, 15 })
2309      feed('<C-V>j3l')
2310      api.nvim_input_mouse('right', 'press', '', 0, 1, 1)
2311      api.nvim_input_mouse('right', 'release', '', 0, 1, 1)
2312      feed('<Down><CR>')
2313      eq({ 2, 1 }, api.nvim_win_get_cursor(0))
2314      eq('v', fn.getregtype('"'))
2315      eq('', fn.getreg('"'))
2316
2317      -- Test for right click in line-wise visual mode inside the selection
2318      fn.setreg('"', '')
2319      api.nvim_win_set_cursor(0, { 1, 15 })
2320      feed('V')
2321      api.nvim_input_mouse('right', 'press', '', 0, 0, 9)
2322      api.nvim_input_mouse('right', 'release', '', 0, 0, 9)
2323      feed('<Down><CR>')
2324      eq({ 1, 0 }, api.nvim_win_get_cursor(0)) -- After yanking, the cursor goes to 1,1
2325      eq('V', fn.getregtype('"'))
2326      eq(1, #fn.getreg('"', 1, true))
2327
2328      -- Test for right click in multi-line line-wise visual mode inside the selection
2329      fn.setreg('"', '')
2330      api.nvim_win_set_cursor(0, { 1, 15 })
2331      feed('Vj')
2332      api.nvim_input_mouse('right', 'press', '', 0, 1, 19)
2333      api.nvim_input_mouse('right', 'release', '', 0, 1, 19)
2334      feed('<Down><CR>')
2335      eq({ 1, 0 }, api.nvim_win_get_cursor(0)) -- After yanking, the cursor goes to 1,1
2336      eq('V', fn.getregtype('"'))
2337      eq(2, #fn.getreg('"', 1, true))
2338
2339      -- Test for right click in line-wise visual mode outside the selection
2340      fn.setreg('"', '')
2341      api.nvim_win_set_cursor(0, { 1, 15 })
2342      feed('V')
2343      api.nvim_input_mouse('right', 'press', '', 0, 1, 9)
2344      api.nvim_input_mouse('right', 'release', '', 0, 1, 9)
2345      feed('<Down><CR>')
2346      eq({ 2, 9 }, api.nvim_win_get_cursor(0))
2347      eq('', fn.getreg('"'))
2348
2349      -- Try clicking outside the window
2350      fn.setreg('"', '')
2351      api.nvim_win_set_cursor(0, { 2, 1 })
2352      feed('vee')
2353      api.nvim_input_mouse('right', 'press', '', 0, 6, 1)
2354      api.nvim_input_mouse('right', 'release', '', 0, 6, 1)
2355      feed('<Down><CR>')
2356      eq(2, fn.winnr())
2357      eq('', fn.getreg('"'))
2358
2359      -- Test for right click in visual mode inside the selection with vertical splits
2360      command('wincmd t')
2361      command('rightbelow vsplit')
2362      fn.setreg('"', '')
2363      api.nvim_win_set_cursor(0, { 1, 9 })
2364      feed('vee')
2365      api.nvim_input_mouse('right', 'press', '', 0, 0, 52)
2366      api.nvim_input_mouse('right', 'release', '', 0, 0, 52)
2367      feed('<Down><CR>')
2368      eq({ 1, 9 }, api.nvim_win_get_cursor(0))
2369      eq('ran away', fn.getreg('"'))
2370
2371      -- Test for right click inside visual selection at bottom of window with winbar
2372      command('setlocal winbar=WINBAR')
2373      feed('2yyP')
2374      fn.setreg('"', '')
2375      feed('G$vbb')
2376      api.nvim_input_mouse('right', 'press', '', 0, 4, 61)
2377      api.nvim_input_mouse('right', 'release', '', 0, 4, 61)
2378      feed('<Down><CR>')
2379      eq({ 4, 20 }, api.nvim_win_get_cursor(0))
2380      eq('the moon', fn.getreg('"'))
2381
2382      -- Try clicking in the cmdline
2383      api.nvim_input_mouse('right', 'press', '', 0, 23, 0)
2384      api.nvim_input_mouse('right', 'release', '', 0, 23, 0)
2385      feed('<Down><Down><Down><CR>')
2386      eq('baz', api.nvim_get_var('menustr'))
2387
2388      -- Try clicking in horizontal separator with global statusline
2389      command('set laststatus=3')
2390      api.nvim_input_mouse('right', 'press', '', 0, 5, 0)
2391      api.nvim_input_mouse('right', 'release', '', 0, 5, 0)
2392      feed('<Down><CR>')
2393      eq('foo', api.nvim_get_var('menustr'))
2394
2395      -- Try clicking in the cmdline with global statusline
2396      api.nvim_input_mouse('right', 'press', '', 0, 23, 0)
2397      api.nvim_input_mouse('right', 'release', '', 0, 23, 0)
2398      feed('<Down><Down><CR>')
2399      eq('bar', api.nvim_get_var('menustr'))
2400    end)
2401
2402    it('below a concealed line #33450', function()
2403      api.nvim_set_option_value('conceallevel', 2, {})
2404      api.nvim_buf_set_extmark(0, api.nvim_create_namespace(''), 1, 0, { conceal_lines = '' })
2405      api.nvim_input_mouse('left', 'press', '', 0, 1, 0)
2406      api.nvim_input_mouse('left', 'release', '', 0, 1, 0)
2407      eq(3, fn.line('.'))
2408      -- No error when clicking below last line that is concealed.
2409      screen:try_resize(80, 10) -- Prevent hit-enter
2410      api.nvim_set_option_value('cmdheight', 3, {})
2411      local count = api.nvim_buf_line_count(0)
2412      api.nvim_buf_set_extmark(0, 1, count - 1, 0, { conceal_lines = '' })
2413      api.nvim_input_mouse('left', 'press', '', 0, count, 0)
2414      eq('', api.nvim_get_vvar('errmsg'))
2415    end)
2416
2417    it('on virtual lines (above/below)', function()
2418      screen:try_resize(screen._width, 12)
2419      local ns = api.nvim_create_namespace('')
2420      api.nvim_buf_set_extmark(0, ns, 1, 0, { virt_lines = { { { 'virt_line below', '' } } } })
2421      api.nvim_buf_set_extmark(0, ns, 1, 0, { virt_lines = { { { 'virt_line below', '' } } } })
2422      local virt_above = { virt_lines_above = true, virt_lines = { { { 'virt_line above', '' } } } }
2423      api.nvim_buf_set_extmark(0, ns, 2, 0, virt_above)
2424      api.nvim_input_mouse('left', 'press', '', 0, 2, 0)
2425      api.nvim_input_mouse('left', 'release', '', 0, 2, 0)
2426      eq(2, fn.line('.'))
2427      api.nvim_win_set_cursor(0, { 1, 0 })
2428      api.nvim_input_mouse('left', 'press', '', 0, 3, 0)
2429      api.nvim_input_mouse('left', 'release', '', 0, 3, 0)
2430      eq(2, fn.line('.'))
2431      api.nvim_win_set_cursor(0, { 1, 0 })
2432      api.nvim_input_mouse('left', 'press', '', 0, 4, 0)
2433      api.nvim_input_mouse('left', 'release', '', 0, 4, 0)
2434      eq(3, fn.line('.'))
2435      feed('gg2<C-E>')
2436      api.nvim_input_mouse('left', 'press', '', 0, 0, 0)
2437      api.nvim_input_mouse('left', 'release', '', 0, 0, 0)
2438      eq(2, fn.line('.'))
2439      feed('gg2<C-E>')
2440      api.nvim_input_mouse('left', 'press', '', 0, 1, 0)
2441      api.nvim_input_mouse('left', 'release', '', 0, 1, 0)
2442      eq(2, fn.line('.'))
2443      feed('gg2<C-E>')
2444      api.nvim_input_mouse('left', 'press', '', 0, 2, 0)
2445      api.nvim_input_mouse('left', 'release', '', 0, 2, 0)
2446      eq(3, fn.line('.'))
2447      feed('gg3<C-E>')
2448      api.nvim_input_mouse('left', 'press', '', 0, 0, 0)
2449      api.nvim_input_mouse('left', 'release', '', 0, 0, 0)
2450      eq(2, fn.line('.'))
2451      feed('gg3<C-E>')
2452      api.nvim_input_mouse('left', 'press', '', 0, 1, 0)
2453      api.nvim_input_mouse('left', 'release', '', 0, 1, 0)
2454      eq(3, fn.line('.'))
2455      feed('gg')
2456      command('call setline(3, ["3"->repeat(&columns + 1), "4"])')
2457      api.nvim_buf_set_extmark(0, ns, 2, 0, { virt_lines = { { { 'virt_line below', '' } } } })
2458      api.nvim_input_mouse('left', 'press', '', 0, 7, 0)
2459      api.nvim_input_mouse('left', 'release', '', 0, 7, 0)
2460      eq(3, fn.line('.'))
2461    end)
2462  end
2463
2464  describe('with ext_multigrid', function()
2465    with_ext_multigrid(true)
2466  end)
2467
2468  describe('without ext_multigrid', function()
2469    with_ext_multigrid(false)
2470  end)
2471end)