robin/laufey

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

Justin M. Keyesrefactor(exmode): Ex-mode as cmdwin + Lua #40991359459dec6

master
52.5 KiB1198 linesraw
1local t = require('test.testutil')
2local n = require('test.functional.testnvim')()
3local tt = require('test.functional.testterm')
4
5local describe, it, before_each = t.describe, t.it, t.before_each
6local feed, clear = n.feed, n.clear
7local testprg, command = n.testprg, n.command
8local eq, eval = t.eq, n.eval
9local api = n.api
10local exec_lua = n.exec_lua
11local matches = t.matches
12local call = n.call
13local hide_cursor = tt.hide_cursor
14local show_cursor = tt.show_cursor
15local retry = t.retry
16local is_os = t.is_os
17local skip = t.skip
18
19describe(':terminal cursor', function()
20  local screen
21
22  local terminal_mode_idx ---@type number
23
24  before_each(function()
25    clear()
26    screen = tt.setup_screen()
27
28    if terminal_mode_idx == nil then
29      for i, v in ipairs(screen._mode_info) do
30        if v.name == 'terminal' then
31          terminal_mode_idx = i
32        end
33      end
34      assert(terminal_mode_idx)
35    end
36  end)
37
38  it('moves the screen cursor when focused', function()
39    tt.feed_data('testing cursor')
40    screen:expect([[
41      tty ready                                         |
42      testing cursor^                                    |
43                                                        |*4
44      {5:-- TERMINAL --}                                    |
45    ]])
46  end)
47
48  it('is highlighted when not focused', function()
49    feed('<c-\\><c-n>')
50    screen:expect([[
51      tty ready                                         |
52      ^                                                  |
53                                                        |*5
54    ]])
55  end)
56
57  describe('with number column', function()
58    before_each(function()
59      feed('<c-\\><c-n>:set number<cr>')
60    end)
61
62    it('is positioned correctly when unfocused', function()
63      screen:expect([[
64        {121:  1 }tty ready                                     |
65        {121:  2 }^rows: 6, cols: 46                             |
66        {121:  3 }                                              |
67        {121:  4 }                                              |
68        {121:  5 }                                              |
69        {121:  6 }                                              |
70        :set number                                       |
71      ]])
72    end)
73
74    it('is positioned correctly when focused', function()
75      screen:expect([[
76        {121:  1 }tty ready                                     |
77        {121:  2 }^rows: 6, cols: 46                             |
78        {121:  3 }                                              |
79        {121:  4 }                                              |
80        {121:  5 }                                              |
81        {121:  6 }                                              |
82        :set number                                       |
83      ]])
84      feed('i')
85      n.poke_eventloop()
86      screen:expect([[
87        {121:  1 }tty ready                                     |
88        {121:  2 }rows: 6, cols: 46                             |
89        {121:  3 }^                                              |
90        {121:  4 }                                              |
91        {121:  5 }                                              |
92        {121:  6 }                                              |
93        {5:-- TERMINAL --}                                    |
94      ]])
95    end)
96  end)
97
98  describe('when invisible', function()
99    it('is not highlighted', function()
100      hide_cursor()
101      screen:expect([[
102        tty ready                                         |
103                                                          |*5
104        {5:-- TERMINAL --}                                    |
105      ]])
106      show_cursor()
107      screen:expect([[
108        tty ready                                         |
109        ^                                                  |
110                                                          |*4
111        {5:-- TERMINAL --}                                    |
112      ]])
113      -- same for when the terminal is unfocused
114      feed('<c-\\><c-n>')
115      hide_cursor()
116      screen:expect([[
117        tty ready                                         |
118        ^                                                  |
119                                                          |*5
120      ]])
121      show_cursor()
122      screen:expect_unchanged()
123    end)
124
125    it('becomes visible when exiting Terminal mode', function()
126      hide_cursor()
127      screen:expect([[
128        tty ready                                         |
129                                                          |*5
130        {5:-- TERMINAL --}                                    |
131      ]])
132      feed('<c-\\><c-n>')
133      screen:expect([[
134        tty ready                                         |
135        ^                                                  |
136                                                          |*5
137      ]])
138      feed('i')
139      screen:expect([[
140        tty ready                                         |
141                                                          |*5
142        {5:-- TERMINAL --}                                    |
143      ]])
144
145      -- Cursor is hidden; now request to show it while in a TermLeave autocmd.
146      -- Process events (via :sleep) to handle the escape sequence now.
147      n.exec([[
148        autocmd TermLeave * ++once call chansend(&channel, "\e[?25h") | sleep 1m
149                                 \ | let g:did_termleave = 1]])
150      feed([[<C-\><C-N>]]) -- Exit terminal mode; cursor should not remain hidden
151      screen:expect([[
152        tty ready                                         |
153        ^                                                  |
154                                                          |*5
155      ]])
156      -- Wait for the TermLeave autocommand to finish. Sometimes :sleep can be slow.
157      retry(nil, 1000, function()
158        eq(1, api.nvim_get_var('did_termleave'))
159      end)
160
161      command('bwipeout! | let chan = nvim_open_term(0, {})')
162      feed('i')
163      -- Hide the cursor, switch to a non-terminal buffer, then show the cursor; it shouldn't remain
164      -- hidden after we're kicked out of terminal mode in the new buffer.
165      -- Must ensure these actions happen within the same terminal_execute call. The stream is
166      -- internal, so polling the event loop isn't necessary (terminal_receive is directly called).
167      command([[call chansend(chan, "\e[?25l") | new floob | call chansend(chan, "\e[?25h")]])
168      screen:expect([[
169        ^                                                  |
170        {100:~                                                 }|
171        {3:floob                                             }|
172                                                          |*2
173        {119:[Scratch] [-]                                     }|
174                                                          |
175      ]])
176
177      feed('<C-W>pi')
178      screen:expect([[
179                                                          |
180        {100:~                                                 }|
181        {2:floob                                             }|
182        ^                                                  |
183                                                          |
184        {120:[Scratch] [-]                                     }|
185        {5:-- TERMINAL --}                                    |
186      ]])
187    end)
188
189    it('becomes visible on TermLeave if hidden immediately by events #32456', function()
190      -- Reproducing the issue is quite fragile; it's easiest done in a lone test case like this
191      -- with no prior commands.
192      feed([[<C-\><C-N>]])
193      screen:expect([[
194        tty ready                                         |
195        ^                                                  |
196                                                          |*5
197      ]])
198
199      -- Hide the cursor such that the escape sequence is processed as a side effect of showmode in
200      -- terminal_enter handling events (skip_showmode -> char_avail -> vpeekc -> os_breakcheck).
201      -- This requires a particular set of actions; :startinsert repros better than feed('i') here.
202      hide_cursor()
203      command('mode | startinsert')
204      screen:expect([[
205        tty ready                                         |
206                                                          |*5
207        {5:-- TERMINAL --}                                    |
208      ]])
209
210      feed([[<C-\><C-N>]])
211      screen:expect([[
212        tty ready                                         |
213        ^                                                  |
214                                                          |*5
215      ]])
216    end)
217  end)
218
219  it('can be modified by application #3681 #31685', function()
220    local states = {
221      [1] = { blink = true, shape = 'block' },
222      [2] = { blink = false, shape = 'block' },
223      [3] = { blink = true, shape = 'horizontal' },
224      [4] = { blink = false, shape = 'horizontal' },
225      [5] = { blink = true, shape = 'vertical' },
226      [6] = { blink = false, shape = 'vertical' },
227    }
228
229    for k, v in pairs(states) do
230      tt.feed_csi(('%d q'):format(k))
231      screen:expect({
232        grid = [[
233          tty ready                                         |
234          ^                                                  |
235                                                            |*4
236          {5:-- TERMINAL --}                                    |
237        ]],
238        condition = function()
239          if v.blink then
240            eq(500, screen._mode_info[terminal_mode_idx].blinkon)
241            eq(500, screen._mode_info[terminal_mode_idx].blinkoff)
242          else
243            eq(0, screen._mode_info[terminal_mode_idx].blinkon)
244            eq(0, screen._mode_info[terminal_mode_idx].blinkoff)
245          end
246
247          eq(v.shape, screen._mode_info[terminal_mode_idx].cursor_shape)
248
249          -- Cell percentages are hard coded for each shape in terminal.c
250          if v.shape == 'horizontal' then
251            eq(20, screen._mode_info[terminal_mode_idx].cell_percentage)
252          elseif v.shape == 'vertical' then
253            eq(25, screen._mode_info[terminal_mode_idx].cell_percentage)
254          end
255        end,
256      })
257    end
258
259    feed([[<C-\><C-N>]])
260
261    screen:expect([[
262      tty ready                                         |
263      ^                                                  |
264                                                        |*5
265    ]])
266
267    -- Cursor returns to default on TermLeave
268    eq(500, screen._mode_info[terminal_mode_idx].blinkon)
269    eq(500, screen._mode_info[terminal_mode_idx].blinkoff)
270    eq('block', screen._mode_info[terminal_mode_idx].cursor_shape)
271  end)
272
273  it('can be modified per terminal', function()
274    -- Set cursor to vertical bar with blink
275    tt.feed_csi('5 q')
276    screen:expect({
277      grid = [[
278        tty ready                                         |
279        ^                                                  |
280                                                          |*4
281        {5:-- TERMINAL --}                                    |
282      ]],
283      condition = function()
284        eq(500, screen._mode_info[terminal_mode_idx].blinkon)
285        eq(500, screen._mode_info[terminal_mode_idx].blinkoff)
286        eq('vertical', screen._mode_info[terminal_mode_idx].cursor_shape)
287      end,
288    })
289
290    tt.hide_cursor()
291    screen:expect({
292      grid = [[
293        tty ready                                         |
294                                                          |*5
295        {5:-- TERMINAL --}                                    |
296      ]],
297      condition = function()
298        eq(500, screen._mode_info[terminal_mode_idx].blinkon)
299        eq(500, screen._mode_info[terminal_mode_idx].blinkoff)
300        eq('vertical', screen._mode_info[terminal_mode_idx].cursor_shape)
301      end,
302    })
303
304    -- Exit terminal mode to reset terminal cursor settings to default and
305    -- create a new terminal window
306    feed([[<C-\><C-N>]])
307    command('set statusline=~~~')
308    command('new')
309    call('jobstart', { testprg('tty-test') }, { term = true })
310    feed('i')
311    screen:expect({
312      grid = [[
313        tty ready                                         |
314        ^                                                  |
315        {120:~~~                                               }|
316        rows: 2, cols: 50                                 |
317                                                          |
318        {119:~~~                                               }|
319        {5:-- TERMINAL --}                                    |
320      ]],
321      condition = function()
322        -- New terminal, cursor resets to defaults
323        eq(500, screen._mode_info[terminal_mode_idx].blinkon)
324        eq(500, screen._mode_info[terminal_mode_idx].blinkoff)
325        eq('block', screen._mode_info[terminal_mode_idx].cursor_shape)
326      end,
327    })
328
329    -- Set cursor to underline, no blink
330    tt.feed_csi('4 q')
331    screen:expect({
332      grid = [[
333        tty ready                                         |
334        ^                                                  |
335        {120:~~~                                               }|
336        rows: 2, cols: 50                                 |
337                                                          |
338        {119:~~~                                               }|
339        {5:-- TERMINAL --}                                    |
340      ]],
341      condition = function()
342        eq(0, screen._mode_info[terminal_mode_idx].blinkon)
343        eq(0, screen._mode_info[terminal_mode_idx].blinkoff)
344        eq('horizontal', screen._mode_info[terminal_mode_idx].cursor_shape)
345      end,
346    })
347
348    -- Switch back to first terminal, cursor should still be hidden
349    command('wincmd p')
350    screen:expect({
351      grid = [[
352        tty ready                                         |
353                                                          |
354        {119:~~~                                               }|
355        rows: 2, cols: 50                                 |
356                                                          |
357        {120:~~~                                               }|
358        {5:-- TERMINAL --}                                    |
359      ]],
360      condition = function()
361        eq(500, screen._mode_info[terminal_mode_idx].blinkon)
362        eq(500, screen._mode_info[terminal_mode_idx].blinkoff)
363        eq('vertical', screen._mode_info[terminal_mode_idx].cursor_shape)
364      end,
365    })
366  end)
367
368  it('can be positioned arbitrarily', function()
369    clear()
370    screen = tt.setup_child_nvim({
371      '-u',
372      'NONE',
373      '-i',
374      'NONE',
375      '--cmd',
376      n.nvim_set .. ' noshowmode',
377    })
378    screen:expect([[
379      ^                                                  |
380      ~                                                 |*4
381                                                        |
382      {5:-- TERMINAL --}                                    |
383    ]])
384
385    feed('i<Tab>')
386    screen:expect([[
387              ^                                          |
388      ~                                                 |*4
389                                                        |
390      {5:-- TERMINAL --}                                    |
391    ]])
392  end)
393
394  it('preserves guicursor value on TermLeave #31612', function()
395    eq(3, screen._mode_info[terminal_mode_idx].hl_id)
396
397    -- Change 'guicursor' while terminal mode is active
398    command('set guicursor+=t:Error')
399
400    local error_hl_id = call('hlID', 'Error')
401
402    screen:expect({
403      condition = function()
404        eq(error_hl_id, screen._mode_info[terminal_mode_idx].hl_id)
405      end,
406    })
407
408    -- Exit terminal mode
409    feed([[<C-\><C-N>]])
410
411    screen:expect([[
412      tty ready                                         |
413      ^                                                  |
414                                                        |*5
415    ]])
416
417    eq(error_hl_id, screen._mode_info[terminal_mode_idx].hl_id)
418  end)
419
420  it('uses the correct attributes', function()
421    feed([[<C-\><C-N>]])
422    command([[
423      bwipeout!
424      let chan1 = nvim_open_term(0, {})
425      vnew
426      let chan2 = nvim_open_term(0, {})
427    ]])
428    feed('i')
429    local s0 = [[
430      ^                         │                        |
431                               │                        |*4
432      {120:[Scratch] [-]             }{119:[Scratch] [-]           }|
433      {5:-- TERMINAL --}                                    |
434    ]]
435    screen:expect(s0)
436    eq('block', screen._mode_info[terminal_mode_idx].cursor_shape)
437    eq(500, screen._mode_info[terminal_mode_idx].blinkon)
438    eq(500, screen._mode_info[terminal_mode_idx].blinkoff)
439
440    -- Modify cursor in the non-current terminal; should not affect this cursor.
441    command([[call chansend(chan1, "\e[4 q")]])
442    screen:expect({
443      grid = s0,
444      unchanged = true,
445      condition = function()
446        eq('block', screen._mode_info[terminal_mode_idx].cursor_shape)
447        eq(500, screen._mode_info[terminal_mode_idx].blinkon)
448        eq(500, screen._mode_info[terminal_mode_idx].blinkoff)
449      end,
450    })
451
452    -- Modify cursor in the current terminal.
453    command([[call chansend(chan2, "\e[6 q")]])
454    screen:expect({
455      grid = s0,
456      condition = function()
457        eq('vertical', screen._mode_info[terminal_mode_idx].cursor_shape)
458        eq(0, screen._mode_info[terminal_mode_idx].blinkon)
459        eq(0, screen._mode_info[terminal_mode_idx].blinkoff)
460      end,
461    })
462
463    -- Check the cursor in the other terminal reflects our changes from before.
464    command('wincmd p')
465    screen:expect([[
466                               │^                        |
467                               │                        |*4
468      {119:[Scratch] [-]             }{120:[Scratch] [-]           }|
469      {5:-- TERMINAL --}                                    |
470    ]])
471    eq('horizontal', screen._mode_info[terminal_mode_idx].cursor_shape)
472    eq(0, screen._mode_info[terminal_mode_idx].blinkon)
473    eq(0, screen._mode_info[terminal_mode_idx].blinkoff)
474  end)
475
476  it('position correct within events', function()
477    local term, term_unfocused = exec_lua(function()
478      vim.cmd 'bwipeout!'
479      local term_unfocused = vim.api.nvim_open_term(0, {})
480      vim.cmd.vnew()
481      vim.cmd.wincmd '|'
482      local term = vim.api.nvim_open_term(0, {})
483      -- We'll use this keymap to pause the main loop while we send events, as we want the test to
484      -- run within the same terminal_execute call (while using test suite facilities like retry).
485      vim.keymap.set('t', '<F1>', '<Cmd>let g:sleepy = 1 | sleep 5000 | let g:sleepy = 0<CR>')
486      return term, term_unfocused
487    end)
488    feed('i<F1>')
489
490    local function check_pos(expected_pos, expected_virtcol, chan, data)
491      api.nvim_chan_send(chan, data) -- Using nvim_chan_send so terminal_receive is immediate.
492
493      -- Results won't be visible until refresh_terminal is called, which happens on a timer.
494      retry(nil, nil, function()
495        eq(expected_pos, eval("getpos('.')[1:]"))
496      end)
497      eq(expected_virtcol, eval("virtcol('.', 1)"))
498      eq(1, eval('g:sleepy')) -- :sleep shouldn't have timed out.
499    end
500
501    check_pos({ 1, 4, 0 }, { 4, 4 }, term, 'foo')
502    -- double-width char at end (3 bytes)
503    check_pos({ 2, 13, 0 }, { 12, 12 }, term, '\r\nbarbaaaar哦')
504    -- Move to 1,12 (beyond eol; sets coladd)
505    check_pos({ 1, 4, 8 }, { 12, 12 }, term, '\27[1;12H')
506    -- Move to 4,1
507    check_pos({ 4, 1, 0 }, { 1, 1 }, term, '\27[4;1H')
508    -- Move to 4,5 (beyond eol; sets coladd)
509    check_pos({ 4, 1, 4 }, { 5, 5 }, term, '\27[4;5H')
510    -- Move to 2,10 (head of wide char)
511    check_pos({ 2, 10, 0 }, { 10, 11 }, term, '\27[2;10H')
512    -- Move to 2,11 (non-head of wide char)
513    check_pos({ 2, 10, 0 }, { 10, 11 }, term, '\27[2;11H')
514    -- Move to 2,12 (after wide char)
515    check_pos({ 2, 13, 0 }, { 12, 12 }, term, '\27[2;12H')
516    -- Move to 2,13 (beyond eol; sets coladd)
517    check_pos({ 2, 13, 1 }, { 13, 13 }, term, '\27[2;13H')
518    -- Cursor movement in unfocused terminal shouldn't affect us
519    check_pos({ 2, 13, 1 }, { 13, 13 }, term_unfocused, 'amogus')
520  end)
521end)
522
523describe('buffer cursor position is correct in terminal without number column', function()
524  local screen
525
526  local function setup_register_and_prompt(str, prompt)
527    prompt = ':' .. (prompt or '')
528    screen = tt.setup_child_nvim({
529      '-u',
530      'NONE',
531      '-i',
532      'NONE',
533      '--cmd',
534      string.format('let @r = "%s"', str),
535      -- <Left> and <Right> don't always work
536      '--cmd',
537      'cnoremap <C-X> <Left>',
538      '--cmd',
539      'cnoremap <C-O> <Right>',
540      '--cmd',
541      'set notermguicolors',
542      -- A blocked cmdline prompt at the last row, as a stable text-entry to test cursor tracking.
543      ('+call input("%s")'):format(prompt),
544    }, {
545      cols = 70,
546    })
547    screen:expect(([[
548                                                                            |*5
549      %s^%s|
550      {5:-- TERMINAL --}                                                        |
551    ]]):format(prompt, (' '):rep(70 - #prompt)))
552  end
553
554  before_each(function()
555    clear()
556    command('autocmd! nvim.terminal TermOpen')
557  end)
558
559  describe('in a line with no multibyte chars or trailing spaces,', function()
560    before_each(function()
561      setup_register_and_prompt('aaaaaaaa')
562    end)
563
564    it('at the end', function()
565      feed('<C-R>r')
566      screen:expect([[
567                                                                              |*5
568        :aaaaaaaa^                                                             |
569        {5:-- TERMINAL --}                                                        |
570      ]])
571      eq({ 6, 9 }, eval('nvim_win_get_cursor(0)'))
572      feed([[<C-\><C-N>]])
573      screen:expect([[
574                                                                              |*5
575        :aaaaaaa^a                                                             |
576                                                                              |
577      ]])
578      eq({ 6, 8 }, eval('nvim_win_get_cursor(0)'))
579    end)
580
581    it('near the end', function()
582      feed('<C-R>r<C-X><C-X>')
583      screen:expect([[
584                                                                              |*5
585        :aaaaaa^aa                                                             |
586        {5:-- TERMINAL --}                                                        |
587      ]])
588      eq({ 6, 7 }, eval('nvim_win_get_cursor(0)'))
589      feed([[<C-\><C-N>]])
590      screen:expect([[
591                                                                              |*5
592        :aaaaa^aaa                                                             |
593                                                                              |
594      ]])
595      eq({ 6, 6 }, eval('nvim_win_get_cursor(0)'))
596    end)
597
598    it('near the start', function()
599      feed('<C-R>r<C-B><C-O>')
600      screen:expect([[
601                                                                              |*5
602        :a^aaaaaaa                                                             |
603        {5:-- TERMINAL --}                                                        |
604      ]])
605      eq({ 6, 2 }, eval('nvim_win_get_cursor(0)'))
606      feed([[<C-\><C-N>]])
607      screen:expect([[
608                                                                              |*5
609        :^aaaaaaaa                                                             |
610                                                                              |
611      ]])
612      eq({ 6, 1 }, eval('nvim_win_get_cursor(0)'))
613    end)
614  end)
615
616  describe('in a line with single-cell multibyte chars and no trailing spaces,', function()
617    before_each(function()
618      setup_register_and_prompt('µµµµµµµµ')
619    end)
620
621    it('at the end', function()
622      feed('<C-R>r')
623      screen:expect([[
624                                                                              |*5
625        :µµµµµµµµ^                                                             |
626        {5:-- TERMINAL --}                                                        |
627      ]])
628      eq({ 6, 17 }, eval('nvim_win_get_cursor(0)'))
629      feed([[<C-\><C-N>]])
630      screen:expect([[
631                                                                              |*5
632        :µµµµµµµ^µ                                                             |
633                                                                              |
634      ]])
635      eq({ 6, 15 }, eval('nvim_win_get_cursor(0)'))
636    end)
637
638    it('near the end', function()
639      feed('<C-R>r<C-X><C-X>')
640      screen:expect([[
641                                                                              |*5
642        :µµµµµµ^µµ                                                             |
643        {5:-- TERMINAL --}                                                        |
644      ]])
645      eq({ 6, 13 }, eval('nvim_win_get_cursor(0)'))
646      feed([[<C-\><C-N>]])
647      screen:expect([[
648                                                                              |*5
649        :µµµµµ^µµµ                                                             |
650                                                                              |
651      ]])
652      eq({ 6, 11 }, eval('nvim_win_get_cursor(0)'))
653    end)
654
655    it('near the start', function()
656      feed('<C-R>r<C-B><C-O>')
657      screen:expect([[
658                                                                              |*5
659        :µ^µµµµµµµ                                                             |
660        {5:-- TERMINAL --}                                                        |
661      ]])
662      eq({ 6, 3 }, eval('nvim_win_get_cursor(0)'))
663      feed([[<C-\><C-N>]])
664      screen:expect([[
665                                                                              |*5
666        :^µµµµµµµµ                                                             |
667                                                                              |
668      ]])
669      eq({ 6, 1 }, eval('nvim_win_get_cursor(0)'))
670    end)
671  end)
672
673  describe('in a line with single-cell composed multibyte chars and no trailing spaces,', function()
674    before_each(function()
675      setup_register_and_prompt('µ̳µ̳µ̳µ̳µ̳µ̳µ̳µ̳')
676    end)
677
678    it('at the end', function()
679      feed('<C-R>r')
680      screen:expect([[
681                                                                              |*5
682        :µ̳µ̳µ̳µ̳µ̳µ̳µ̳µ̳^                                                             |
683        {5:-- TERMINAL --}                                                        |
684      ]])
685      eq({ 6, 33 }, eval('nvim_win_get_cursor(0)'))
686      feed([[<C-\><C-N>]])
687      screen:expect([[
688                                                                              |*5
689        :µ̳µ̳µ̳µ̳µ̳µ̳µ̳^µ̳                                                             |
690                                                                              |
691      ]])
692      eq({ 6, 29 }, eval('nvim_win_get_cursor(0)'))
693    end)
694
695    it('near the end', function()
696      skip(is_os('win'))
697      feed('<C-R>r<C-X><C-X>')
698      screen:expect([[
699                                                                              |*5
700        :µ̳µ̳µ̳µ̳µ̳µ̳^µ̳µ̳                                                             |
701        {5:-- TERMINAL --}                                                        |
702      ]])
703      eq({ 6, 25 }, eval('nvim_win_get_cursor(0)'))
704      feed([[<C-\><C-N>]])
705      screen:expect([[
706                                                                              |*5
707        :µ̳µ̳µ̳µ̳µ̳^µ̳µ̳µ̳                                                             |
708                                                                              |
709      ]])
710      eq({ 6, 21 }, eval('nvim_win_get_cursor(0)'))
711    end)
712
713    it('near the start', function()
714      skip(is_os('win'))
715      feed('<C-R>r<C-B><C-O>')
716      screen:expect([[
717                                                                              |*5
718        :µ̳^µ̳µ̳µ̳µ̳µ̳µ̳µ̳                                                             |
719        {5:-- TERMINAL --}                                                        |
720      ]])
721      eq({ 6, 5 }, eval('nvim_win_get_cursor(0)'))
722      feed([[<C-\><C-N>]])
723      screen:expect([[
724                                                                              |*5
725        :^µ̳µ̳µ̳µ̳µ̳µ̳µ̳µ̳                                                             |
726                                                                              |
727      ]])
728      eq({ 6, 1 }, eval('nvim_win_get_cursor(0)'))
729    end)
730  end)
731
732  describe('in a line with double-cell multibyte chars and no trailing spaces,', function()
733    before_each(function()
734      setup_register_and_prompt('哦哦哦哦哦哦哦哦')
735    end)
736
737    it('at the end', function()
738      feed('<C-R>r')
739      screen:expect([[
740                                                                              |*5
741        :哦哦哦哦哦哦哦哦^                                                     |
742        {5:-- TERMINAL --}                                                        |
743      ]])
744      eq({ 6, 25 }, eval('nvim_win_get_cursor(0)'))
745      feed([[<C-\><C-N>]])
746      screen:expect([[
747                                                                              |*5
748        :哦哦哦哦哦哦哦^哦                                                     |
749                                                                              |
750      ]])
751      eq({ 6, 22 }, eval('nvim_win_get_cursor(0)'))
752    end)
753
754    it('near the end', function()
755      feed('<C-R>r<C-X><C-X>')
756      screen:expect([[
757                                                                              |*5
758        :哦哦哦哦哦哦^哦哦                                                     |
759        {5:-- TERMINAL --}                                                        |
760      ]])
761      eq({ 6, 19 }, eval('nvim_win_get_cursor(0)'))
762      feed([[<C-\><C-N>]])
763      screen:expect([[
764                                                                              |*5
765        :哦哦哦哦哦^哦哦哦                                                     |
766                                                                              |
767      ]])
768      eq({ 6, 16 }, eval('nvim_win_get_cursor(0)'))
769    end)
770
771    it('near the start', function()
772      feed('<C-R>r<C-B><C-O>')
773      screen:expect([[
774                                                                              |*5
775        :哦^哦哦哦哦哦哦哦                                                     |
776        {5:-- TERMINAL --}                                                        |
777      ]])
778      eq({ 6, 4 }, eval('nvim_win_get_cursor(0)'))
779      feed([[<C-\><C-N>]])
780      screen:expect([[
781                                                                              |*5
782        :^哦哦哦哦哦哦哦哦                                                     |
783                                                                              |
784      ]])
785      eq({ 6, 1 }, eval('nvim_win_get_cursor(0)'))
786    end)
787  end)
788
789  it('at the end of a line with trailing spaces #16234', function()
790    -- Use the raw tty-test child: it echoes input verbatim, so the trailing spaces are real
791    -- terminal cells (a cmdline/TUI child never emits trailing blank cells).
792    screen = tt.setup_screen(nil, nil, 70)
793    tt.feed_data('aaaaaaaa    ')
794    screen:expect([[
795      tty ready                                                             |
796      aaaaaaaa    ^                                                          |
797                                                                            |*4
798      {5:-- TERMINAL --}                                                        |
799    ]])
800    matches('^aaaaaaaa    [ ]*$', eval('nvim_get_current_line()'))
801    eq({ 2, 12 }, eval('nvim_win_get_cursor(0)'))
802    feed([[<C-\><C-N>]])
803    eq({ 2, 11 }, eval('nvim_win_get_cursor(0)'))
804  end)
805end)
806
807describe('buffer cursor position is correct in terminal with number column', function()
808  local screen
809
810  local function setup_register_and_prompt(str, prompt)
811    prompt = ':' .. (prompt or '')
812    screen = tt.setup_child_nvim({
813      '-u',
814      'NONE',
815      '-i',
816      'NONE',
817      '--cmd',
818      string.format('let @r = "%s"', str),
819      -- <Left> and <Right> don't always work
820      '--cmd',
821      'cnoremap <C-X> <Left>',
822      '--cmd',
823      'cnoremap <C-O> <Right>',
824      '--cmd',
825      'set notermguicolors',
826      -- A blocked cmdline prompt at the last row, as a stable text-entry to test cursor tracking.
827      ('+call input("%s")'):format(prompt),
828    }, {
829      cols = 70,
830    })
831    screen:expect(([[
832      {121:  1 }                                                                  |
833      {121:  2 }                                                                  |
834      {121:  3 }                                                                  |
835      {121:  4 }                                                                  |
836      {121:  5 }                                                                  |
837      {121:  6 }%s^%s|
838      {5:-- TERMINAL --}                                                        |
839    ]]):format(prompt, (' '):rep(66 - #prompt)))
840  end
841
842  before_each(function()
843    clear()
844    command('autocmd! nvim.terminal TermOpen')
845    -- 'number' should be set before the terminal process starts, otherwise the resize
846    -- from setting 'number' may cause a redraw that changes the child screen.
847    command('set number')
848  end)
849
850  describe('in a line with no multibyte chars or trailing spaces,', function()
851    before_each(function()
852      setup_register_and_prompt('aaaaaaaa')
853    end)
854
855    it('at the end', function()
856      feed('<C-R>r')
857      screen:expect([[
858        {121:  1 }                                                                  |
859        {121:  2 }                                                                  |
860        {121:  3 }                                                                  |
861        {121:  4 }                                                                  |
862        {121:  5 }                                                                  |
863        {121:  6 }:aaaaaaaa^                                                         |
864        {5:-- TERMINAL --}                                                        |
865      ]])
866      eq({ 6, 9 }, eval('nvim_win_get_cursor(0)'))
867      feed([[<C-\><C-N>]])
868      screen:expect([[
869        {121:  1 }                                                                  |
870        {121:  2 }                                                                  |
871        {121:  3 }                                                                  |
872        {121:  4 }                                                                  |
873        {121:  5 }                                                                  |
874        {121:  6 }:aaaaaaa^a                                                         |
875                                                                              |
876      ]])
877      eq({ 6, 8 }, eval('nvim_win_get_cursor(0)'))
878    end)
879
880    it('near the end', function()
881      feed('<C-R>r<C-X><C-X>')
882      screen:expect([[
883        {121:  1 }                                                                  |
884        {121:  2 }                                                                  |
885        {121:  3 }                                                                  |
886        {121:  4 }                                                                  |
887        {121:  5 }                                                                  |
888        {121:  6 }:aaaaaa^aa                                                         |
889        {5:-- TERMINAL --}                                                        |
890      ]])
891      eq({ 6, 7 }, eval('nvim_win_get_cursor(0)'))
892      feed([[<C-\><C-N>]])
893      screen:expect([[
894        {121:  1 }                                                                  |
895        {121:  2 }                                                                  |
896        {121:  3 }                                                                  |
897        {121:  4 }                                                                  |
898        {121:  5 }                                                                  |
899        {121:  6 }:aaaaa^aaa                                                         |
900                                                                              |
901      ]])
902      eq({ 6, 6 }, eval('nvim_win_get_cursor(0)'))
903    end)
904
905    it('near the start', function()
906      feed('<C-R>r<C-B><C-O>')
907      screen:expect([[
908        {121:  1 }                                                                  |
909        {121:  2 }                                                                  |
910        {121:  3 }                                                                  |
911        {121:  4 }                                                                  |
912        {121:  5 }                                                                  |
913        {121:  6 }:a^aaaaaaa                                                         |
914        {5:-- TERMINAL --}                                                        |
915      ]])
916      eq({ 6, 2 }, eval('nvim_win_get_cursor(0)'))
917      feed([[<C-\><C-N>]])
918      screen:expect([[
919        {121:  1 }                                                                  |
920        {121:  2 }                                                                  |
921        {121:  3 }                                                                  |
922        {121:  4 }                                                                  |
923        {121:  5 }                                                                  |
924        {121:  6 }:^aaaaaaaa                                                         |
925                                                                              |
926      ]])
927      eq({ 6, 1 }, eval('nvim_win_get_cursor(0)'))
928    end)
929  end)
930
931  describe('in a line with single-cell multibyte chars and no trailing spaces,', function()
932    before_each(function()
933      setup_register_and_prompt('µµµµµµµµ')
934    end)
935
936    it('at the end', function()
937      feed('<C-R>r')
938      screen:expect([[
939        {121:  1 }                                                                  |
940        {121:  2 }                                                                  |
941        {121:  3 }                                                                  |
942        {121:  4 }                                                                  |
943        {121:  5 }                                                                  |
944        {121:  6 }:µµµµµµµµ^                                                         |
945        {5:-- TERMINAL --}                                                        |
946      ]])
947      eq({ 6, 17 }, eval('nvim_win_get_cursor(0)'))
948      feed([[<C-\><C-N>]])
949      screen:expect([[
950        {121:  1 }                                                                  |
951        {121:  2 }                                                                  |
952        {121:  3 }                                                                  |
953        {121:  4 }                                                                  |
954        {121:  5 }                                                                  |
955        {121:  6 }:µµµµµµµ^µ                                                         |
956                                                                              |
957      ]])
958      eq({ 6, 15 }, eval('nvim_win_get_cursor(0)'))
959    end)
960
961    it('near the end', function()
962      feed('<C-R>r<C-X><C-X>')
963      screen:expect([[
964        {121:  1 }                                                                  |
965        {121:  2 }                                                                  |
966        {121:  3 }                                                                  |
967        {121:  4 }                                                                  |
968        {121:  5 }                                                                  |
969        {121:  6 }:µµµµµµ^µµ                                                         |
970        {5:-- TERMINAL --}                                                        |
971      ]])
972      eq({ 6, 13 }, eval('nvim_win_get_cursor(0)'))
973      feed([[<C-\><C-N>]])
974      screen:expect([[
975        {121:  1 }                                                                  |
976        {121:  2 }                                                                  |
977        {121:  3 }                                                                  |
978        {121:  4 }                                                                  |
979        {121:  5 }                                                                  |
980        {121:  6 }:µµµµµ^µµµ                                                         |
981                                                                              |
982      ]])
983      eq({ 6, 11 }, eval('nvim_win_get_cursor(0)'))
984    end)
985
986    it('near the start', function()
987      feed('<C-R>r<C-B><C-O>')
988      screen:expect([[
989        {121:  1 }                                                                  |
990        {121:  2 }                                                                  |
991        {121:  3 }                                                                  |
992        {121:  4 }                                                                  |
993        {121:  5 }                                                                  |
994        {121:  6 }:µ^µµµµµµµ                                                         |
995        {5:-- TERMINAL --}                                                        |
996      ]])
997      eq({ 6, 3 }, eval('nvim_win_get_cursor(0)'))
998      feed([[<C-\><C-N>]])
999      screen:expect([[
1000        {121:  1 }                                                                  |
1001        {121:  2 }                                                                  |
1002        {121:  3 }                                                                  |
1003        {121:  4 }                                                                  |
1004        {121:  5 }                                                                  |
1005        {121:  6 }:^µµµµµµµµ                                                         |
1006                                                                              |
1007      ]])
1008      eq({ 6, 1 }, eval('nvim_win_get_cursor(0)'))
1009    end)
1010  end)
1011
1012  describe('in a line with single-cell composed multibyte chars and no trailing spaces,', function()
1013    before_each(function()
1014      setup_register_and_prompt('µ̳µ̳µ̳µ̳µ̳µ̳µ̳µ̳')
1015    end)
1016
1017    it('at the end', function()
1018      feed('<C-R>r')
1019      screen:expect([[
1020        {121:  1 }                                                                  |
1021        {121:  2 }                                                                  |
1022        {121:  3 }                                                                  |
1023        {121:  4 }                                                                  |
1024        {121:  5 }                                                                  |
1025        {121:  6 }:µ̳µ̳µ̳µ̳µ̳µ̳µ̳µ̳^                                                         |
1026        {5:-- TERMINAL --}                                                        |
1027      ]])
1028      eq({ 6, 33 }, eval('nvim_win_get_cursor(0)'))
1029      feed([[<C-\><C-N>]])
1030      screen:expect([[
1031        {121:  1 }                                                                  |
1032        {121:  2 }                                                                  |
1033        {121:  3 }                                                                  |
1034        {121:  4 }                                                                  |
1035        {121:  5 }                                                                  |
1036        {121:  6 }:µ̳µ̳µ̳µ̳µ̳µ̳µ̳^µ̳                                                         |
1037                                                                              |
1038      ]])
1039      eq({ 6, 29 }, eval('nvim_win_get_cursor(0)'))
1040    end)
1041
1042    it('near the end', function()
1043      skip(is_os('win'))
1044      feed('<C-R>r<C-X><C-X>')
1045      screen:expect([[
1046        {121:  1 }                                                                  |
1047        {121:  2 }                                                                  |
1048        {121:  3 }                                                                  |
1049        {121:  4 }                                                                  |
1050        {121:  5 }                                                                  |
1051        {121:  6 }:µ̳µ̳µ̳µ̳µ̳µ̳^µ̳µ̳                                                         |
1052        {5:-- TERMINAL --}                                                        |
1053      ]])
1054      eq({ 6, 25 }, eval('nvim_win_get_cursor(0)'))
1055      feed([[<C-\><C-N>]])
1056      screen:expect([[
1057        {121:  1 }                                                                  |
1058        {121:  2 }                                                                  |
1059        {121:  3 }                                                                  |
1060        {121:  4 }                                                                  |
1061        {121:  5 }                                                                  |
1062        {121:  6 }:µ̳µ̳µ̳µ̳µ̳^µ̳µ̳µ̳                                                         |
1063                                                                              |
1064      ]])
1065      eq({ 6, 21 }, eval('nvim_win_get_cursor(0)'))
1066    end)
1067
1068    it('near the start', function()
1069      skip(is_os('win'))
1070      feed('<C-R>r<C-B><C-O>')
1071      screen:expect([[
1072        {121:  1 }                                                                  |
1073        {121:  2 }                                                                  |
1074        {121:  3 }                                                                  |
1075        {121:  4 }                                                                  |
1076        {121:  5 }                                                                  |
1077        {121:  6 }:µ̳^µ̳µ̳µ̳µ̳µ̳µ̳µ̳                                                         |
1078        {5:-- TERMINAL --}                                                        |
1079      ]])
1080      eq({ 6, 5 }, eval('nvim_win_get_cursor(0)'))
1081      feed([[<C-\><C-N>]])
1082      screen:expect([[
1083        {121:  1 }                                                                  |
1084        {121:  2 }                                                                  |
1085        {121:  3 }                                                                  |
1086        {121:  4 }                                                                  |
1087        {121:  5 }                                                                  |
1088        {121:  6 }:^µ̳µ̳µ̳µ̳µ̳µ̳µ̳µ̳                                                         |
1089                                                                              |
1090      ]])
1091      eq({ 6, 1 }, eval('nvim_win_get_cursor(0)'))
1092    end)
1093  end)
1094
1095  describe('in a line with double-cell multibyte chars and no trailing spaces,', function()
1096    before_each(function()
1097      setup_register_and_prompt('哦哦哦哦哦哦哦哦')
1098    end)
1099
1100    it('at the end', function()
1101      feed('<C-R>r')
1102      screen:expect([[
1103        {121:  1 }                                                                  |
1104        {121:  2 }                                                                  |
1105        {121:  3 }                                                                  |
1106        {121:  4 }                                                                  |
1107        {121:  5 }                                                                  |
1108        {121:  6 }:哦哦哦哦哦哦哦哦^                                                 |
1109        {5:-- TERMINAL --}                                                        |
1110      ]])
1111      eq({ 6, 25 }, eval('nvim_win_get_cursor(0)'))
1112      feed([[<C-\><C-N>]])
1113      screen:expect([[
1114        {121:  1 }                                                                  |
1115        {121:  2 }                                                                  |
1116        {121:  3 }                                                                  |
1117        {121:  4 }                                                                  |
1118        {121:  5 }                                                                  |
1119        {121:  6 }:哦哦哦哦哦哦哦^哦                                                 |
1120                                                                              |
1121      ]])
1122      eq({ 6, 22 }, eval('nvim_win_get_cursor(0)'))
1123    end)
1124
1125    it('near the end', function()
1126      feed('<C-R>r<C-X><C-X>')
1127      screen:expect([[
1128        {121:  1 }                                                                  |
1129        {121:  2 }                                                                  |
1130        {121:  3 }                                                                  |
1131        {121:  4 }                                                                  |
1132        {121:  5 }                                                                  |
1133        {121:  6 }:哦哦哦哦哦哦^哦哦                                                 |
1134        {5:-- TERMINAL --}                                                        |
1135      ]])
1136      eq({ 6, 19 }, eval('nvim_win_get_cursor(0)'))
1137      feed([[<C-\><C-N>]])
1138      screen:expect([[
1139        {121:  1 }                                                                  |
1140        {121:  2 }                                                                  |
1141        {121:  3 }                                                                  |
1142        {121:  4 }                                                                  |
1143        {121:  5 }                                                                  |
1144        {121:  6 }:哦哦哦哦哦^哦哦哦                                                 |
1145                                                                              |
1146      ]])
1147      eq({ 6, 16 }, eval('nvim_win_get_cursor(0)'))
1148    end)
1149
1150    it('near the start', function()
1151      feed('<C-R>r<C-B><C-O>')
1152      screen:expect([[
1153        {121:  1 }                                                                  |
1154        {121:  2 }                                                                  |
1155        {121:  3 }                                                                  |
1156        {121:  4 }                                                                  |
1157        {121:  5 }                                                                  |
1158        {121:  6 }:哦^哦哦哦哦哦哦哦                                                 |
1159        {5:-- TERMINAL --}                                                        |
1160      ]])
1161      eq({ 6, 4 }, eval('nvim_win_get_cursor(0)'))
1162      feed([[<C-\><C-N>]])
1163      screen:expect([[
1164        {121:  1 }                                                                  |
1165        {121:  2 }                                                                  |
1166        {121:  3 }                                                                  |
1167        {121:  4 }                                                                  |
1168        {121:  5 }                                                                  |
1169        {121:  6 }:^哦哦哦哦哦哦哦哦                                                 |
1170                                                                              |
1171      ]])
1172      eq({ 6, 1 }, eval('nvim_win_get_cursor(0)'))
1173    end)
1174  end)
1175
1176  it('at the end of a line with trailing spaces #16234', function()
1177    -- Raw tty-test child: trailing spaces are real terminal cells. See the no-number variant.
1178    -- 'number' is set after the terminal opens: setup_screen() asserts the default layout.
1179    command('set nonumber')
1180    screen = tt.setup_screen(nil, nil, 70)
1181    command('set number')
1182    screen:expect({ any = 'rows: 6, cols: 66' })
1183    tt.feed_data('aaaaaaaa    ')
1184    screen:expect([[
1185      {121:  1 }tty ready                                                         |
1186      {121:  2 }rows: 6, cols: 66                                                 |
1187      {121:  3 }aaaaaaaa    ^                                                      |
1188      {121:  4 }                                                                  |
1189      {121:  5 }                                                                  |
1190      {121:  6 }                                                                  |
1191      {5:-- TERMINAL --}                                                        |
1192    ]])
1193    matches('^aaaaaaaa    [ ]*$', eval('nvim_get_current_line()'))
1194    eq({ 3, 12 }, eval('nvim_win_get_cursor(0)'))
1195    feed([[<C-\><C-N>]])
1196    eq({ 3, 11 }, eval('nvim_win_get_cursor(0)'))
1197  end)
1198end)