robin/laufey

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

robinmerge: 9a29622b3b00ddb601

master
109.0 KiB3958 linesraw
1local t = require('test.unit.testutil')
2local describe, before_each, pending = t.describe, t.before_each, t.pending
3local itp = t.gen_itp(t.it)
4local bit = require('bit')
5
6--- @class vterm
7--- @field ENC_UTF8 integer
8--- @field VTERM_ATTR_BLINK integer
9--- @field VTERM_ATTR_BOLD integer
10--- @field VTERM_ATTR_FONT integer
11--- @field VTERM_ATTR_ITALIC integer
12--- @field VTERM_ATTR_REVERSE integer
13--- @field VTERM_ATTR_UNDERLINE integer
14--- @field VTERM_BASELINE_RAISE integer
15--- @field VTERM_KEY_ENTER integer
16--- @field VTERM_KEY_FUNCTION_0 integer
17--- @field VTERM_KEY_KP_0 integer
18--- @field VTERM_KEY_NONE integer
19--- @field VTERM_KEY_TAB integer
20--- @field VTERM_KEY_UP integer
21--- @field VTERM_KEY_BACKSPACE integer
22--- @field VTERM_KEY_ESCAPE integer
23--- @field VTERM_KEY_DEL integer
24--- @field VTERM_MOD_ALT integer
25--- @field VTERM_MOD_CTRL integer
26--- @field VTERM_MOD_SHIFT integer
27--- @field parser_apc function
28--- @field parser_csi function
29--- @field parser_dcs function
30--- @field parser_osc function
31--- @field parser_pm function
32--- @field parser_sos function
33--- @field parser_text function
34--- @field print_color function
35--- @field schar_get fun(any, any):integer
36--- @field screen_sb_clear function
37--- @field screen_sb_popline function
38--- @field screen_sb_pushline function
39--- @field selection_query function
40--- @field selection_set function
41--- @field state_erase function
42--- @field state_movecursor function
43--- @field state_moverect function
44--- @field state_pos function
45--- @field state_putglyph function
46--- @field state_sb_clear function
47--- @field state_scrollrect function
48--- @field state_setpenattr function
49--- @field state_settermprop function
50--- @field term_output function
51--- @field utf_ptr2char fun(any):integer
52--- @field utf_ptr2len fun(any):integer
53--- @field vterm_input_write function
54--- @field vterm_keyboard_end_paste function
55--- @field vterm_keyboard_key function
56--- @field vterm_keyboard_start_paste function
57--- @field vterm_keyboard_unichar function
58--- @field vterm_lookup_encoding fun(any, any):any
59--- @field vterm_mouse_button function
60--- @field vterm_mouse_move function
61--- @field vterm_new fun(any, any):any
62--- @field vterm_obtain_screen fun(any):any
63--- @field vterm_obtain_state fun(any): any
64--- @field vterm_output_set_callback function
65--- @field vterm_parser_set_callbacks fun(any, any, any):any
66--- @field vterm_screen_convert_color_to_rgb function
67--- @field vterm_screen_enable_altscreen function
68--- @field vterm_screen_enable_reflow function
69--- @field vterm_screen_get_attrs_extent function
70--- @field vterm_screen_get_cell function
71--- @field vterm_screen_get_text fun(any, any, any, any):any
72--- @field vterm_screen_is_eol fun(any, any):any
73--- @field vterm_screen_reset function
74--- @field vterm_screen_set_callbacks function
75--- @field vterm_set_size function
76--- @field vterm_set_utf8 fun(any, any, any):any
77--- @field vterm_state_focus_in function
78--- @field vterm_state_focus_out function
79--- @field vterm_state_get_cursorpos fun(any, any)
80--- @field vterm_state_get_lineinfo fun(any, any):any
81--- @field vterm_state_get_penattr function
82--- @field vterm_state_reset function
83--- @field vterm_state_set_bold_highbright function
84--- @field vterm_state_set_callbacks function
85--- @field vterm_state_set_selection_callbacks function
86--- @field vterm_state_set_unrecognised_fallbacks function
87local vterm = t.cimport(
88  './src/nvim/grid.h',
89  './src/nvim/mbyte.h',
90  './src/nvim/vterm/encoding.h',
91  './src/nvim/vterm/keyboard.h',
92  './src/nvim/vterm/mouse.h',
93  './src/nvim/vterm/parser.h',
94  './src/nvim/vterm/pen.h',
95  './src/nvim/vterm/screen.h',
96  './src/nvim/vterm/state.h',
97  './src/nvim/vterm/vterm.h',
98  './src/nvim/vterm/vterm_internal_defs.h',
99  './test/unit/fixtures/vterm_test.h'
100)
101
102-- libvterm stores pointers to these callback structs instead of copying them.
103-- Keep the cdata alive for as long as the owning handle exists, and let the
104-- weak-key table release everything once the handle itself becomes unreachable.
105local callback_structs = setmetatable({}, { __mode = 'k' })
106
107---@param owner any
108---@param ... any
109local function keep_callback_structs(owner, ...)
110  local refs = callback_structs[owner]
111  if refs == nil then
112    refs = {}
113    callback_structs[owner] = refs
114  end
115
116  for i = 1, select('#', ...) do
117    local ref = select(i, ...)
118    if ref ~= nil then
119      refs[#refs + 1] = ref
120    end
121  end
122end
123
124--- @return string
125local function read_rm()
126  local f = assert(io.open(t.paths.vterm_test_file, 'rb'))
127  local text = f:read('*a')
128  f:close()
129  vim.fs.rm(t.paths.vterm_test_file, { force = true })
130  return text
131end
132
133local function append(str)
134  local f = assert(io.open(t.paths.vterm_test_file, 'a'))
135  f:write(str)
136  f:close()
137  return 1
138end
139
140local function parser_control(control)
141  return append(string.format('control %02x\n', control))
142end
143
144local function parser_escape(bytes)
145  return append(string.format('escape %s\n', t.ffi.string(bytes)))
146end
147
148local function wantparser(vt)
149  assert(vt)
150
151  local parser_cbs = t.ffi.new('VTermParserCallbacks')
152  parser_cbs['text'] = vterm.parser_text
153  parser_cbs['control'] = parser_control
154  parser_cbs['escape'] = parser_escape
155  parser_cbs['csi'] = vterm.parser_csi
156  parser_cbs['osc'] = vterm.parser_osc
157  parser_cbs['dcs'] = vterm.parser_dcs
158  parser_cbs['apc'] = vterm.parser_apc
159  parser_cbs['pm'] = vterm.parser_pm
160  parser_cbs['sos'] = vterm.parser_sos
161
162  vterm.vterm_parser_set_callbacks(vt, parser_cbs, nil)
163  keep_callback_structs(vt, parser_cbs)
164end
165
166--- @return any
167local function init()
168  local vt = vterm.vterm_new(25, 80)
169  vterm.vterm_output_set_callback(vt, vterm.term_output, nil)
170  vterm.vterm_set_utf8(vt, true)
171  return vt
172end
173
174local function state_setlineinfo()
175  return 1
176end
177
178--- @return any
179local function wantstate(vt, opts)
180  opts = opts or {}
181  assert(vt)
182  local state = vterm.vterm_obtain_state(vt)
183
184  local state_cbs = t.ffi.new('VTermStateCallbacks')
185  state_cbs['putglyph'] = vterm.state_putglyph
186  state_cbs['movecursor'] = vterm.state_movecursor
187  state_cbs['scrollrect'] = vterm.state_scrollrect
188  state_cbs['moverect'] = vterm.state_moverect
189  state_cbs['erase'] = vterm.state_erase
190  state_cbs['setpenattr'] = vterm.state_setpenattr
191  state_cbs['settermprop'] = vterm.state_settermprop
192  state_cbs['setlineinfo'] = state_setlineinfo
193  state_cbs['sb_clear'] = vterm.state_sb_clear
194
195  local selection_cbs = t.ffi.new('VTermSelectionCallbacks')
196  selection_cbs['set'] = vterm.selection_set
197  selection_cbs['query'] = vterm.selection_query
198
199  vterm.vterm_state_set_callbacks(state, state_cbs, nil)
200
201  -- In some tests we want to check the behaviour of overflowing the buffer, so make it nicely small
202  vterm.vterm_state_set_selection_callbacks(state, selection_cbs, nil, nil, 16)
203  vterm.vterm_state_set_bold_highbright(state, 1)
204  vterm.vterm_state_reset(state, 1)
205
206  local fallback_cbs
207  if opts.f then
208    fallback_cbs = t.ffi.new('VTermStateFallbacks')
209    fallback_cbs['control'] = parser_control
210    fallback_cbs['csi'] = vterm.parser_csi
211    fallback_cbs['osc'] = vterm.parser_osc
212    fallback_cbs['dcs'] = vterm.parser_dcs
213    fallback_cbs['apc'] = vterm.parser_apc
214    fallback_cbs['pm'] = vterm.parser_pm
215    fallback_cbs['sos'] = vterm.parser_sos
216  end
217
218  vterm.want_state_scrollback = opts.b or false
219  vterm.want_state_erase = opts.e or false
220  vterm.vterm_state_set_unrecognised_fallbacks(state, fallback_cbs, nil)
221  vterm.want_state_putglyph = opts.g or false
222  vterm.want_state_moverect = opts.m or false
223  vterm.want_state_settermprop = opts.p or false
224  vterm.want_state_scrollrect = opts.s or false
225
226  keep_callback_structs(state, state_cbs, selection_cbs, fallback_cbs)
227  return state
228end
229
230--- @return any
231local function wantscreen(vt, opts)
232  opts = opts or {}
233  local screen = vterm.vterm_obtain_screen(vt)
234  local screen_cbs = t.ffi.new('VTermScreenCallbacks')
235
236  -- TODO(dundargoc): fix
237  -- screen_cbs['damage']      = vterm.screen_damage
238  screen_cbs['moverect'] = vterm.state_moverect
239  screen_cbs['movecursor'] = vterm.state_movecursor
240  screen_cbs['settermprop'] = vterm.state_settermprop
241  screen_cbs['sb_pushline'] = vterm.screen_sb_pushline
242  screen_cbs['sb_popline'] = vterm.screen_sb_popline
243  screen_cbs['sb_clear'] = vterm.screen_sb_clear
244
245  vterm.vterm_screen_set_callbacks(screen, screen_cbs, nil)
246
247  if opts.a then
248    vterm.vterm_screen_enable_altscreen(screen, 1)
249  end
250  vterm.want_screen_scrollback = opts.b or false
251  vterm.want_state_movecursor = opts.c or false
252  -- TODO(dundargoc): fix
253  -- vterm.want_screen_damage = opts.d or opts.D or false
254  -- vterm.want_screen_cells = opts.D or false
255  vterm.want_state_moverect = opts.m or false
256  vterm.want_state_settermprop = opts.p or false
257  if opts.r then
258    vterm.vterm_screen_enable_reflow(screen, true)
259  end
260
261  keep_callback_structs(screen, screen_cbs)
262  return screen
263end
264
265local function reset(state, screen)
266  if state then
267    vterm.vterm_state_reset(state, 1)
268    vterm.vterm_state_get_cursorpos(state, vterm.state_pos)
269  end
270  if screen then
271    vterm.vterm_screen_reset(screen, 1)
272  end
273end
274
275local function push(input, vt)
276  vterm.vterm_input_write(vt, input, string.len(input))
277end
278
279-- vterm_input_write() can synchronously invoke the Lua callbacks installed
280-- above. LuaJIT must not JIT-compile an FFI call that re-enters Lua, or the
281-- test can panic with "bad callback".
282if jit then
283  jit.off(push, true)
284end
285
286local function expect(expected)
287  local actual = read_rm()
288  t.eq(expected .. '\n', actual)
289end
290
291local function expect_output(expected_preformat)
292  local actual = read_rm()
293  local expected = 'output '
294
295  for c in string.gmatch(expected_preformat, '.') do
296    if expected ~= 'output ' then
297      expected = expected .. ','
298    end
299    expected = string.format('%s%x', expected, string.byte(c))
300  end
301
302  t.eq(expected .. '\n', actual)
303end
304
305local function cursor(row, col, state)
306  local pos = t.ffi.new('VTermPos') --- @type {row: integer, col: integer}
307  vterm.vterm_state_get_cursorpos(state, pos)
308  t.eq(row, pos.row)
309  t.eq(col, pos.col)
310end
311
312local function lineinfo(row, expected, state)
313  local info = vterm.vterm_state_get_lineinfo(state, row)
314  local dwl = info.doublewidth == 1
315  local dhl = info.doubleheight == 1
316  local cont = info.continuation == 1
317
318  t.eq(expected.dwl or false, dwl)
319  t.eq(expected.dhl or false, dhl)
320  t.eq(expected.cont or false, cont)
321end
322
323local function pen(attribute, expected, state)
324  local is_bool =
325    { bold = true, italic = true, blink = true, reverse = true, dim = true, overline = true }
326  local vterm_attribute = {
327    bold = vterm.VTERM_ATTR_BOLD,
328    underline = vterm.VTERM_ATTR_UNDERLINE,
329    italic = vterm.VTERM_ATTR_ITALIC,
330    blink = vterm.VTERM_ATTR_BLINK,
331    reverse = vterm.VTERM_ATTR_REVERSE,
332    font = vterm.VTERM_ATTR_FONT,
333    dim = vterm.VTERM_ATTR_DIM,
334    overline = vterm.VTERM_ATTR_OVERLINE,
335  }
336
337  local val = t.ffi.new('VTermValue') --- @type {boolean: integer}
338  vterm.vterm_state_get_penattr(state, vterm_attribute[attribute], val)
339  local actual = val.boolean --- @type integer|boolean
340  if is_bool[attribute] then
341    actual = val.boolean == 1
342  end
343  t.eq(expected, actual)
344end
345
346local function resize(rows, cols, vt)
347  vterm.vterm_set_size(vt, rows, cols)
348end
349
350local function screen_chars(start_row, start_col, end_row, end_col, expected, screen)
351  local rect = t.ffi.new('VTermRect')
352  rect['start_row'] = start_row
353  rect['start_col'] = start_col
354  rect['end_row'] = end_row
355  rect['end_col'] = end_col
356
357  local len = vterm.vterm_screen_get_text(screen, nil, 0, rect)
358
359  local text = t.ffi.new('unsigned char[?]', len)
360  vterm.vterm_screen_get_text(screen, text, len, rect)
361
362  local actual = t.ffi.string(text, len)
363  t.eq(expected, actual)
364end
365
366local function screen_text(start_row, start_col, end_row, end_col, expected, screen)
367  local rect = t.ffi.new('VTermRect')
368  rect['start_row'] = start_row
369  rect['start_col'] = start_col
370  rect['end_row'] = end_row
371  rect['end_col'] = end_col
372
373  local len = vterm.vterm_screen_get_text(screen, nil, 0, rect)
374
375  local text = t.ffi.new('unsigned char[?]', len)
376  vterm.vterm_screen_get_text(screen, text, len, rect)
377
378  local actual = ''
379  for i = 0, tonumber(len) - 1 do
380    actual = string.format('%s%02x,', actual, text[i])
381  end
382  actual = actual:sub(1, -2)
383
384  t.eq(expected, actual)
385end
386
387--- @param row integer
388local function screen_row(row, expected, screen, end_col)
389  local rect = t.ffi.new('VTermRect')
390  rect['start_row'] = row
391  rect['start_col'] = 0
392  rect['end_row'] = row + 1
393  rect['end_col'] = end_col or 80
394
395  local len = vterm.vterm_screen_get_text(screen, nil, 0, rect)
396
397  local text = t.ffi.new('unsigned char[?]', len)
398  vterm.vterm_screen_get_text(screen, text, len, rect)
399
400  t.eq(expected, t.ffi.string(text, len))
401end
402
403local function screen_cell(row, col, expected, screen)
404  local pos = t.ffi.new('VTermPos')
405  pos['row'] = row
406  pos['col'] = col
407
408  local cell = t.ffi.new('VTermScreenCell') ---@type any
409  vterm.vterm_screen_get_cell(screen, pos, cell)
410
411  local buf = t.ffi.new('unsigned char[32]')
412  vterm.schar_get(buf, cell.schar)
413
414  local actual = '{'
415  local i = 0
416  while buf[i] > 0 do
417    local char = vterm.utf_ptr2char(buf + i)
418    local charlen = vterm.utf_ptr2len(buf + i)
419    if i > 0 then
420      actual = actual .. ','
421    end
422    local invalid = char >= 128 and charlen == 1
423    actual = string.format('%s%s%02x', actual, invalid and '?' or '', char)
424    i = i + charlen
425  end
426  actual = string.format('%s} width=%d attrs={', actual, cell['width'])
427  actual = actual .. (cell['attrs'].bold ~= 0 and 'B' or '')
428  actual = actual .. (cell['attrs'].dim ~= 0 and 'D' or '')
429  actual = actual
430    .. (cell['attrs'].underline ~= 0 and string.format('U%d', cell['attrs'].underline) or '')
431  actual = actual .. (cell['attrs'].italic ~= 0 and 'I' or '')
432  actual = actual .. (cell['attrs'].blink ~= 0 and 'K' or '')
433  actual = actual .. (cell['attrs'].reverse ~= 0 and 'R' or '')
434  actual = actual .. (cell['attrs'].overline ~= 0 and 'O' or '')
435  actual = actual .. (cell['attrs'].font ~= 0 and string.format('F%d', cell['attrs'].font) or '')
436  actual = actual .. (cell['attrs'].small ~= 0 and 'S' or '')
437  if cell['attrs'].baseline ~= 0 then
438    actual = actual .. (cell['attrs'].baseline == vterm.VTERM_BASELINE_RAISE and '^' or '_')
439  end
440  actual = actual .. '} '
441
442  actual = actual .. (cell['attrs'].dwl ~= 0 and 'dwl ' or '')
443  if cell['attrs'].dhl ~= 0 then
444    actual = actual .. string.format('dhl-%s ', cell['attrs'].dhl == 2 and 'bottom' or 'top')
445  end
446
447  actual = string.format('%sfg=', actual)
448  vterm.vterm_screen_convert_color_to_rgb(screen, cell['fg'])
449  vterm.print_color(cell['fg'])
450
451  actual = actual .. read_rm()
452  actual = actual .. ' bg='
453
454  vterm.vterm_screen_convert_color_to_rgb(screen, cell['bg'])
455  vterm.print_color(cell['bg'])
456
457  actual = actual .. read_rm()
458
459  t.eq(expected, actual)
460end
461
462local function screen_eol(row, col, expected, screen)
463  local pos = t.ffi.new('VTermPos')
464  pos['row'] = row
465  pos['col'] = col
466
467  local is_eol = vterm.vterm_screen_is_eol(screen, pos)
468  t.eq(expected, is_eol)
469end
470
471local function screen_attrs_extent(row, col, expected, screen)
472  local pos = t.ffi.new('VTermPos')
473  pos['row'] = row
474  pos['col'] = col
475
476  local rect = t.ffi.new('VTermRect')
477  rect['start_col'] = 0
478  rect['end_col'] = -1
479  vterm.vterm_screen_get_attrs_extent(screen, rect, pos, 1)
480
481  local actual = string.format(
482    '%d,%d-%d,%d',
483    rect['start_row'],
484    rect['start_col'],
485    rect['end_row'],
486    rect['end_col']
487  )
488
489  t.eq(expected, actual)
490end
491
492local function wantencoding()
493  local encoding = t.ffi.new('VTermEncodingInstance')
494  encoding['enc'] = vterm.vterm_lookup_encoding(vterm.ENC_UTF8, string.byte('u'))
495  if encoding.enc.init then
496    encoding.enc.init(encoding.enc, encoding['data'])
497  end
498  return encoding
499end
500
501local function encin(input, encoding)
502  local len = string.len(input)
503
504  local cp = t.ffi.new('uint32_t[?]', len)
505  local cpi = t.ffi.new('int[1]')
506  local pos = t.ffi.new('size_t[1]', 0)
507
508  encoding.enc.decode(encoding.enc, encoding.data, cp, cpi, len, input, pos, len)
509
510  local f = assert(io.open(t.paths.vterm_test_file, 'w'))
511  if tonumber(cpi[0]) > 0 then
512    f:write('encout ')
513    for i = 0, cpi[0] - 1 do
514      if i == 0 then
515        f:write(string.format('%x', cp[i]))
516      else
517        f:write(string.format(',%x', cp[i]))
518      end
519    end
520    f:write('\n')
521  end
522  f:close()
523end
524
525local function strpe_modifiers(input_mod)
526  local mod = t.ffi.new('VTermModifier') ---@type any
527  if input_mod.C then
528    mod = bit.bor(mod, vterm.VTERM_MOD_CTRL)
529  end
530  if input_mod.S then
531    mod = bit.bor(mod, vterm.VTERM_MOD_SHIFT)
532  end
533  if input_mod.A then
534    mod = bit.bor(mod, vterm.VTERM_MOD_ALT)
535  end
536  return mod
537end
538
539local function strp_key(input_key)
540  if input_key == 'up' then
541    return vterm.VTERM_KEY_UP
542  end
543
544  if input_key == 'tab' then
545    return vterm.VTERM_KEY_TAB
546  end
547
548  if input_key == 'enter' then
549    return vterm.VTERM_KEY_ENTER
550  end
551
552  if input_key == 'bs' then
553    return vterm.VTERM_KEY_BACKSPACE
554  end
555
556  if input_key == 'del' then
557    return vterm.VTERM_KEY_DEL
558  end
559
560  if input_key == 'esc' then
561    return vterm.VTERM_KEY_ESCAPE
562  end
563
564  if input_key == 'f1' then
565    return vterm.VTERM_KEY_FUNCTION_0 + 1
566  end
567
568  if input_key == 'kp0' then
569    return vterm.VTERM_KEY_KP_0
570  end
571
572  return vterm.VTERM_KEY_NONE
573end
574
575local function mousemove(row, col, vt, input_mod)
576  input_mod = input_mod or {}
577  local mod = strpe_modifiers(input_mod)
578  vterm.vterm_mouse_move(vt, row, col, mod)
579end
580
581local function mousebtn(press, button, vt, input_mod)
582  input_mod = input_mod or {}
583  local mod = strpe_modifiers(input_mod)
584  local flag = press == 'd' or press == 'D'
585  vterm.vterm_mouse_button(vt, button, flag, mod)
586end
587
588local function inchar(c, vt, input_mod)
589  input_mod = input_mod or {}
590  local mod = strpe_modifiers(input_mod)
591  vterm.vterm_keyboard_unichar(vt, c, mod)
592end
593
594local function inkey(input_key, vt, input_mod)
595  input_mod = input_mod or {}
596  local mod = strpe_modifiers(input_mod)
597  local key = strp_key(input_key)
598  vterm.vterm_keyboard_key(vt, key, mod)
599end
600
601before_each(function()
602  vim.fs.rm(t.paths.vterm_test_file, { force = true })
603end)
604
605describe('vterm', function()
606  itp('02parser', function()
607    local vt = init()
608    vterm.vterm_set_utf8(vt, false)
609    wantparser(vt)
610
611    -- Basic text
612    push('hello', vt)
613    expect('text 68,65,6c,6c,6f')
614
615    -- C0
616    push('\x03', vt)
617    expect('control 03')
618    push('\x1f', vt)
619    expect('control 1f')
620
621    -- C1 8bit
622    push('\x83', vt)
623    expect('control 83')
624    push('\x99', vt)
625    expect('control 99')
626
627    -- C1 7bit
628    push('\x1b\x43', vt)
629    expect('control 83')
630    push('\x1b\x59', vt)
631    expect('control 99')
632
633    -- High bytes
634    push('\xa0\xcc\xfe', vt)
635    expect('text a0,cc,fe')
636
637    -- Mixed
638    push('1\n2', vt)
639    expect('text 31\ncontrol 0a\ntext 32')
640
641    -- Escape
642    push('\x1b=', vt)
643    expect('escape =')
644
645    -- Escape 2-byte
646    push('\x1b(X', vt)
647    expect('escape (X')
648
649    -- Split write Escape
650    push('\x1b(', vt)
651    push('Y', vt)
652    expect('escape (Y')
653
654    -- Escape cancels Escape, starts another
655    push('\x1b(\x1b)Z', vt)
656    expect('escape )Z')
657
658    -- CAN cancels Escape, returns to normal mode
659    push('\x1b(\x18AB', vt)
660    expect('text 41,42')
661
662    -- C0 in Escape interrupts and continues
663    push('\x1b(\nX', vt)
664    expect('control 0a\nescape (X')
665
666    -- CSI 0 args
667    push('\x1b[a', vt)
668    expect('csi 61 *')
669
670    -- CSI 1 arg
671    push('\x1b[9b', vt)
672    expect('csi 62 9')
673
674    -- CSI 2 args
675    push('\x1b[3;4c', vt)
676    expect('csi 63 3,4')
677
678    -- CSI 1 arg 1 sub
679    push('\x1b[1:2c', vt)
680    expect('csi 63 1+,2')
681
682    -- CSI many digits
683    push('\x1b[678d', vt)
684    expect('csi 64 678')
685
686    -- CSI leading zero
687    push('\x1b[007e', vt)
688    expect('csi 65 7')
689
690    -- CSI qmark
691    push('\x1b[?2;7f', vt)
692    expect('csi 66 L=3f 2,7')
693
694    -- CSI greater
695    push('\x1b[>c', vt)
696    expect('csi 63 L=3e *')
697
698    -- CSI SP
699    push('\x1b[12 q', vt)
700    expect('csi 71 12 I=20')
701
702    -- Mixed CSI
703    push('A\x1b[8mB', vt)
704    expect('text 41\ncsi 6d 8\ntext 42')
705
706    -- Split write
707    push('\x1b', vt)
708    push('[a', vt)
709    expect('csi 61 *')
710    push('foo\x1b[', vt)
711    expect('text 66,6f,6f')
712    push('4b', vt)
713    expect('csi 62 4')
714    push('\x1b[12;', vt)
715    push('3c', vt)
716    expect('csi 63 12,3')
717
718    -- Escape cancels CSI, starts Escape
719    push('\x1b[123\x1b9', vt)
720    expect('escape 9')
721
722    -- CAN cancels CSI, returns to normal mode
723    push('\x1b[12\x18AB', vt)
724    expect('text 41,42')
725
726    -- C0 in Escape interrupts and continues
727    push('\x1b(\nX', vt)
728    expect('control 0a\nescape (X')
729
730    -- OSC BEL
731    push('\x1b]1;Hello\x07', vt)
732    expect('osc [1;Hello]')
733
734    -- OSC ST (7bit)
735    push('\x1b]1;Hello\x1b\\', vt)
736    expect('osc [1;Hello]')
737
738    -- OSC ST (8bit)
739    push('\x9d1;Hello\x9c', vt)
740    expect('osc [1;Hello]')
741
742    -- OSC in parts
743    push('\x1b]52;abc', vt)
744    expect('osc [52;abc')
745    push('def', vt)
746    expect('osc def')
747    push('ghi\x1b\\', vt)
748    expect('osc ghi]')
749
750    -- OSC BEL without semicolon
751    push('\x1b]1234\x07', vt)
752    expect('osc [1234;]')
753
754    -- OSC ST without semicolon
755    push('\x1b]1234\x1b\\', vt)
756    expect('osc [1234;]')
757
758    -- Escape cancels OSC, starts Escape
759    push('\x1b]Something\x1b9', vt)
760    expect('escape 9')
761
762    -- CAN cancels OSC, returns to normal mode
763    push('\x1b]12\x18AB', vt)
764    expect('text 41,42')
765
766    -- C0 in OSC interrupts and continues
767    push('\x1b]2;\nBye\x07', vt)
768    expect('osc [2;\ncontrol 0a\nosc Bye]')
769
770    -- DCS BEL
771    push('\x1bPHello\x07', vt)
772    expect('dcs [Hello]')
773
774    -- DCS ST (7bit)
775    push('\x1bPHello\x1b\\', vt)
776    expect('dcs [Hello]')
777
778    -- DCS ST (8bit)
779    push('\x90Hello\x9c', vt)
780    expect('dcs [Hello]')
781
782    -- Split write of 7bit ST
783    push('\x1bPABC\x1b', vt)
784    expect('dcs [ABC')
785    push('\\', vt)
786    expect('dcs ]')
787
788    -- Escape cancels DCS, starts Escape
789    push('\x1bPSomething\x1b9', vt)
790    expect('escape 9')
791
792    -- CAN cancels DCS, returns to normal mode
793    push('\x1bP12\x18AB', vt)
794    expect('text 41,42')
795
796    -- C0 in OSC interrupts and continues
797    push('\x1bPBy\ne\x07', vt)
798    expect('dcs [By\ncontrol 0a\ndcs e]')
799
800    -- APC BEL
801    push('\x1b_Hello\x07', vt)
802    expect('apc [Hello]')
803
804    -- APC ST (7bit)
805    push('\x1b_Hello\x1b\\', vt)
806    expect('apc [Hello]')
807
808    -- APC ST (8bit)
809    push('\x9fHello\x9c', vt)
810    expect('apc [Hello]')
811
812    -- PM BEL
813    push('\x1b^Hello\x07', vt)
814    expect('pm [Hello]')
815
816    -- PM ST (7bit)
817    push('\x1b^Hello\x1b\\', vt)
818    expect('pm [Hello]')
819
820    -- PM ST (8bit)
821    push('\x9eHello\x9c', vt)
822    expect('pm [Hello]')
823
824    -- SOS BEL
825    push('\x1bXHello\x07', vt)
826    expect('sos [Hello]')
827
828    -- SOS ST (7bit)
829    push('\x1bXHello\x1b\\', vt)
830    expect('sos [Hello]')
831
832    -- SOS ST (8bit)
833    push('\x98Hello\x9c', vt)
834    expect('sos [Hello]')
835
836    push('\x1bXABC\x01DEF\x1b\\', vt)
837    expect('sos [ABC\x01DEF]')
838    push('\x1bXABC\x99DEF\x1b\\', vt)
839    expect('sos [ABC\x99DEF]')
840
841    -- NUL ignored
842    push('\x00', vt)
843
844    -- NUL ignored within CSI
845    push('\x1b[12\x003m', vt)
846    expect('csi 6d 123')
847
848    -- DEL ignored
849    push('\x7f', vt)
850
851    -- DEL ignored within CSI
852    push('\x1b[12\x7f3m', vt)
853    expect('csi 6d 123')
854
855    -- DEL inside text"
856    push('AB\x7fC', vt)
857    expect('text 41,42\ntext 43')
858  end)
859
860  itp('03encoding_utf8', function()
861    local encoding = wantencoding()
862
863    -- Low
864    encin('123', encoding)
865    expect('encout 31,32,33')
866
867    -- We want to prove the UTF-8 parser correctly handles all the sequences.
868    -- Easy way to do this is to check it does low/high boundary cases, as that
869    -- leaves only two for each sequence length
870    --
871    -- These ranges are therefore:
872    --
873    -- Two bytes:
874    -- U+0080 = 000 10000000 =>    00010   000000
875    --                       => 11000010 10000000 = C2 80
876    -- U+07FF = 111 11111111 =>    11111   111111
877    --                       => 11011111 10111111 = DF BF
878    --
879    -- Three bytes:
880    -- U+0800 = 00001000 00000000 =>     0000   100000   000000
881    --                            => 11100000 10100000 10000000 = E0 A0 80
882    -- U+FFFD = 11111111 11111101 =>     1111   111111   111101
883    --                            => 11101111 10111111 10111101 = EF BF BD
884    -- (We avoid U+FFFE and U+FFFF as they're invalid codepoints)
885    --
886    -- Four bytes:
887    -- U+10000  = 00001 00000000 00000000 =>      000   010000   000000   000000
888    --                                    => 11110000 10010000 10000000 10000000 = F0 90 80 80
889    -- U+1FFFFF = 11111 11111111 11111111 =>      111   111111   111111   111111
890    --                                    => 11110111 10111111 10111111 10111111 = F7 BF BF BF
891
892    -- 2 byte
893    encin('\xC2\x80\xDF\xBF', encoding)
894    expect('encout 80,7ff')
895
896    -- 3 byte
897    encin('\xE0\xA0\x80\xEF\xBF\xBD', encoding)
898    expect('encout 800,fffd')
899
900    -- 4 byte
901    encin('\xF0\x90\x80\x80\xF7\xBF\xBF\xBF', encoding)
902    expect('encout 10000,1fffff')
903
904    -- Next up, we check some invalid sequences
905    --  + Early termination (back to low bytes too soon)
906    --  + Early restart (another sequence introduction before the previous one was finished)
907
908    -- Early termination
909    encin('\xC2!', encoding)
910    expect('encout fffd,21')
911
912    encin('\xE0!\xE0\xA0!', encoding)
913    expect('encout fffd,21,fffd,21')
914
915    encin('\xF0!\xF0\x90!\xF0\x90\x80!', encoding)
916    expect('encout fffd,21,fffd,21,fffd,21')
917
918    -- Early restart
919    encin('\xC2\xC2\x90', encoding)
920    expect('encout fffd,90')
921
922    encin('\xE0\xC2\x90\xE0\xA0\xC2\x90', encoding)
923    expect('encout fffd,90,fffd,90')
924
925    encin('\xF0\xC2\x90\xF0\x90\xC2\x90\xF0\x90\x80\xC2\x90', encoding)
926    expect('encout fffd,90,fffd,90,fffd,90')
927
928    -- Test the overlong sequences by giving an overlong encoding of U+0000 and
929    -- an encoding of the highest codepoint still too short
930    --
931    -- Two bytes:
932    -- U+0000 = C0 80
933    -- U+007F = 000 01111111 =>    00001   111111 =>
934    --                       => 11000001 10111111 => C1 BF
935    --
936    -- Three bytes:
937    -- U+0000 = E0 80 80
938    -- U+07FF = 00000111 11111111 =>     0000   011111   111111
939    --                            => 11100000 10011111 10111111 = E0 9F BF
940    --
941    -- Four bytes:
942    -- U+0000 = F0 80 80 80
943    -- U+FFFF = 11111111 11111111 =>      000   001111   111111   111111
944    --                            => 11110000 10001111 10111111 10111111 = F0 8F BF BF
945
946    -- Overlong
947    encin('\xC0\x80\xC1\xBF', encoding)
948    expect('encout fffd,fffd')
949
950    encin('\xE0\x80\x80\xE0\x9F\xBF', encoding)
951    expect('encout fffd,fffd')
952
953    encin('\xF0\x80\x80\x80\xF0\x8F\xBF\xBF', encoding)
954    expect('encout fffd,fffd')
955
956    -- UTF-16 surrogates U+D800 and U+DFFF
957    -- UTF-16 Surrogates
958    encin('\xED\xA0\x80\xED\xBF\xBF', encoding)
959    expect('encout fffd,fffd')
960
961    -- Split write
962    encin('\xC2', encoding)
963    encin('\xA0', encoding)
964    expect('encout a0')
965
966    encin('\xE0', encoding)
967    encin('\xA0\x80', encoding)
968    expect('encout 800')
969    encin('\xE0\xA0', encoding)
970    encin('\x80', encoding)
971    expect('encout 800')
972
973    encin('\xF0', encoding)
974    encin('\x90\x80\x80', encoding)
975    expect('encout 10000')
976    encin('\xF0\x90', encoding)
977    encin('\x80\x80', encoding)
978    expect('encout 10000')
979    encin('\xF0\x90\x80', encoding)
980    encin('\x80', encoding)
981    expect('encout 10000')
982  end)
983
984  itp('10state_putglyph', function()
985    local vt = init()
986    local state = wantstate(vt, { g = true })
987
988    -- Low
989    reset(state, nil)
990    push('ABC', vt)
991    expect('putglyph 41 1 0,0\nputglyph 42 1 0,1\nputglyph 43 1 0,2')
992
993    -- UTF-8 1 char
994    -- U+00C1 = 0xC3 0x81  name: LATIN CAPITAL LETTER A WITH ACUTE
995    -- U+00E9 = 0xC3 0xA9  name: LATIN SMALL LETTER E WITH ACUTE
996    reset(state, nil)
997    push('\xC3\x81\xC3\xA9', vt)
998    expect('putglyph c1 1 0,0\nputglyph e9 1 0,1')
999
1000    -- UTF-8 split writes
1001    reset(state, nil)
1002    push('\xC3', vt)
1003    push('\x81', vt)
1004    expect('putglyph c1 1 0,0')
1005
1006    -- UTF-8 wide char
1007    -- U+FF10 = EF BC 90  name: FULLWIDTH DIGIT ZERO
1008    reset(state, nil)
1009    push('\xEF\xBC\x90 ', vt)
1010    expect('putglyph ff10 2 0,0\nputglyph 20 1 0,2')
1011
1012    -- UTF-8 emoji wide char
1013    -- U+1F600 = F0 9F 98 80  name: GRINNING FACE
1014    reset(state, nil)
1015    push('\xF0\x9F\x98\x80 ', vt)
1016    expect('putglyph 1f600 2 0,0\nputglyph 20 1 0,2')
1017
1018    -- UTF-8 combining chars
1019    -- U+0301 = CC 81  name: COMBINING ACUTE
1020    reset(state, nil)
1021    push('e\xCC\x81Z', vt)
1022    expect('putglyph 65,301 1 0,0\nputglyph 5a 1 0,1')
1023
1024    -- Combining across buffers
1025    reset(state, nil)
1026    push('e', vt)
1027    expect('putglyph 65 1 0,0')
1028    push('\xCC\x81Z', vt)
1029    expect('putglyph 65,301 1 0,0\nputglyph 5a 1 0,1')
1030
1031    -- Spare combining chars get truncated
1032    reset(state, nil)
1033    push('e' .. string.rep('\xCC\x81', 20), vt)
1034    expect('putglyph 65,301,301,301,301,301,301,301,301,301,301,301,301,301,301 1 0,0') -- and nothing more
1035
1036    -- Combining across buffers multiple times
1037    reset(state, nil)
1038    push('e', vt)
1039    expect('putglyph 65 1 0,0')
1040    push('\xCC\x81', vt)
1041    expect('putglyph 65,301 1 0,0')
1042    push('\xCC\x82', vt)
1043    expect('putglyph 65,301,302 1 0,0')
1044
1045    -- Combining across buffers at right edge
1046    reset(state, nil)
1047    push('\x1b[5;80H', vt)
1048    push('e', vt)
1049    expect('putglyph 65 1 4,79')
1050    push('\xCC\x81', vt)
1051    expect('putglyph 65,301 1 4,79')
1052    push('\xCC\x82Z', vt)
1053    expect('putglyph 65,301,302 1 4,79\nputglyph 5a 1 5,0')
1054
1055    -- Combining regional indicators
1056    reset(state, nil)
1057    push('\x1b[5;77H', vt)
1058    push('🇦', vt)
1059    expect('putglyph 1f1e6 2 4,76')
1060    push('🇩', vt)
1061    expect('putglyph 1f1e6,1f1e9 2 4,76')
1062    push('🇱', vt)
1063    expect('putglyph 1f1f1 2 4,78')
1064    push('🇮', vt)
1065    expect('putglyph 1f1f1,1f1ee 2 4,78')
1066    push('🇲', vt)
1067    expect('putglyph 1f1f2 2 5,0')
1068    push('🇨', vt)
1069    expect('putglyph 1f1f2,1f1e8 2 5,0')
1070
1071    -- emoji with ZWJ and variant selectors, as one chunk
1072    reset(state, nil)
1073    push('🏳️‍🌈🏳️‍⚧️🏴‍☠️', vt)
1074    expect([[putglyph 1f3f3,fe0f,200d,1f308 2 0,0
1075putglyph 1f3f3,fe0f,200d,26a7,fe0f 2 0,2
1076putglyph 1f3f4,200d,2620,fe0f 2 0,4]])
1077
1078    -- emoji, one code point at a time
1079    reset(state, nil)
1080    push('🏳', vt)
1081    expect('putglyph 1f3f3 2 0,0')
1082    push('\xef\xb8\x8f', vt)
1083    expect('putglyph 1f3f3,fe0f 2 0,0')
1084    push('\xe2\x80\x8d', vt)
1085    expect('putglyph 1f3f3,fe0f,200d 2 0,0')
1086    push('🌈', vt)
1087    expect('putglyph 1f3f3,fe0f,200d,1f308 2 0,0')
1088
1089    -- modifier can change width
1090    push('❤', vt)
1091    expect('putglyph 2764 1 0,2')
1092    push('\xef\xb8\x8f', vt)
1093    expect('putglyph 2764,fe0f 2 0,2')
1094
1095    -- also works batched
1096    push('❤️', vt)
1097    expect('putglyph 2764,fe0f 2 0,4')
1098
1099    -- DECSCA protected
1100    reset(state, nil)
1101    push('A\x1b[1"qB\x1b[2"qC', vt)
1102    expect('putglyph 41 1 0,0\nputglyph 42 1 0,1 prot\nputglyph 43 1 0,2')
1103  end)
1104
1105  itp('11state_movecursor', function()
1106    local vt = init()
1107    local state = wantstate(vt)
1108
1109    -- Implicit
1110    push('ABC', vt)
1111    cursor(0, 3, state)
1112
1113    -- Backspace
1114    push('\b', vt)
1115    cursor(0, 2, state)
1116    -- Horizontal Tab
1117    push('\t', vt)
1118    cursor(0, 8, state)
1119    -- Carriage Return
1120    push('\r', vt)
1121    cursor(0, 0, state)
1122    -- Linefeed
1123    push('\n', vt)
1124    cursor(1, 0, state)
1125
1126    -- Backspace bounded by lefthand edge
1127    push('\x1b[4;2H', vt)
1128    cursor(3, 1, state)
1129    push('\b', vt)
1130    cursor(3, 0, state)
1131    push('\b', vt)
1132    cursor(3, 0, state)
1133
1134    -- Backspace cancels phantom
1135    push('\x1b[4;80H', vt)
1136    cursor(3, 79, state)
1137    push('X', vt)
1138    cursor(3, 79, state)
1139    push('\b', vt)
1140    cursor(3, 78, state)
1141
1142    -- HT bounded by righthand edge
1143    push('\x1b[1;78H', vt)
1144    cursor(0, 77, state)
1145    push('\t', vt)
1146    cursor(0, 79, state)
1147    push('\t', vt)
1148    cursor(0, 79, state)
1149
1150    reset(state, nil)
1151
1152    -- Index
1153    push('ABC\x1bD', vt)
1154    cursor(1, 3, state)
1155    -- Reverse Index
1156    push('\x1bM', vt)
1157    cursor(0, 3, state)
1158    -- Newline
1159    push('\x1bE', vt)
1160    cursor(1, 0, state)
1161
1162    reset(state, nil)
1163
1164    -- Cursor Forward
1165    push('\x1b[B', vt)
1166    cursor(1, 0, state)
1167    push('\x1b[3B', vt)
1168    cursor(4, 0, state)
1169    push('\x1b[0B', vt)
1170    cursor(5, 0, state)
1171
1172    -- Cursor Down
1173    push('\x1b[C', vt)
1174    cursor(5, 1, state)
1175    push('\x1b[3C', vt)
1176    cursor(5, 4, state)
1177    push('\x1b[0C', vt)
1178    cursor(5, 5, state)
1179
1180    -- Cursor Up
1181    push('\x1b[A', vt)
1182    cursor(4, 5, state)
1183    push('\x1b[3A', vt)
1184    cursor(1, 5, state)
1185    push('\x1b[0A', vt)
1186    cursor(0, 5, state)
1187
1188    -- Cursor Backward
1189    push('\x1b[D', vt)
1190    cursor(0, 4, state)
1191    push('\x1b[3D', vt)
1192    cursor(0, 1, state)
1193    push('\x1b[0D', vt)
1194    cursor(0, 0, state)
1195
1196    -- Cursor Next Line
1197    push('   ', vt)
1198    cursor(0, 3, state)
1199    push('\x1b[E', vt)
1200    cursor(1, 0, state)
1201    push('   ', vt)
1202    cursor(1, 3, state)
1203    push('\x1b[2E', vt)
1204    cursor(3, 0, state)
1205    push('\x1b[0E', vt)
1206    cursor(4, 0, state)
1207
1208    -- Cursor Previous Line
1209    push('   ', vt)
1210    cursor(4, 3, state)
1211    push('\x1b[F', vt)
1212    cursor(3, 0, state)
1213    push('   ', vt)
1214    cursor(3, 3, state)
1215    push('\x1b[2F', vt)
1216    cursor(1, 0, state)
1217    push('\x1b[0F', vt)
1218    cursor(0, 0, state)
1219
1220    -- Cursor Horizontal Absolute
1221    push('\n', vt)
1222    cursor(1, 0, state)
1223    push('\x1b[20G', vt)
1224    cursor(1, 19, state)
1225    push('\x1b[G', vt)
1226    cursor(1, 0, state)
1227
1228    -- Cursor Position
1229    push('\x1b[10;5H', vt)
1230    cursor(9, 4, state)
1231    push('\x1b[8H', vt)
1232    cursor(7, 0, state)
1233    push('\x1b[H', vt)
1234    cursor(0, 0, state)
1235
1236    -- Cursor Position cancels phantom
1237    push('\x1b[10;78H', vt)
1238    cursor(9, 77, state)
1239    push('ABC', vt)
1240    cursor(9, 79, state)
1241    push('\x1b[10;80H', vt)
1242    push('C', vt)
1243    cursor(9, 79, state)
1244    push('X', vt)
1245    cursor(10, 1, state)
1246
1247    reset(state, nil)
1248
1249    -- Bounds Checking
1250    push('\x1b[A', vt)
1251    cursor(0, 0, state)
1252    push('\x1b[D', vt)
1253    cursor(0, 0, state)
1254    push('\x1b[25;80H', vt)
1255    cursor(24, 79, state)
1256    push('\x1b[B', vt)
1257    cursor(24, 79, state)
1258    push('\x1b[C', vt)
1259    cursor(24, 79, state)
1260    push('\x1b[E', vt)
1261    cursor(24, 0, state)
1262    push('\x1b[H', vt)
1263    cursor(0, 0, state)
1264    push('\x1b[F', vt)
1265    cursor(0, 0, state)
1266    push('\x1b[999G', vt)
1267    cursor(0, 79, state)
1268    push('\x1b[99;99H', vt)
1269    cursor(24, 79, state)
1270
1271    reset(state, nil)
1272
1273    -- Horizontal Position Absolute
1274    push('\x1b[5`', vt)
1275    cursor(0, 4, state)
1276
1277    -- Horizontal Position Relative
1278    push('\x1b[3a', vt)
1279    cursor(0, 7, state)
1280
1281    -- Horizontal Position Backward
1282    push('\x1b[3j', vt)
1283    cursor(0, 4, state)
1284
1285    -- Horizontal and Vertical Position
1286    push('\x1b[3;3f', vt)
1287    cursor(2, 2, state)
1288
1289    -- Vertical Position Absolute
1290    push('\x1b[5d', vt)
1291    cursor(4, 2, state)
1292
1293    -- Vertical Position Relative
1294    push('\x1b[2e', vt)
1295    cursor(6, 2, state)
1296
1297    -- Vertical Position Backward
1298    push('\x1b[2k', vt)
1299    cursor(4, 2, state)
1300
1301    reset(state, nil)
1302
1303    -- Horizontal Tab
1304    push('\t', vt)
1305    cursor(0, 8, state)
1306    push('   ', vt)
1307    cursor(0, 11, state)
1308    push('\t', vt)
1309    cursor(0, 16, state)
1310    push('       ', vt)
1311    cursor(0, 23, state)
1312    push('\t', vt)
1313    cursor(0, 24, state)
1314    push('        ', vt)
1315    cursor(0, 32, state)
1316    push('\t', vt)
1317    cursor(0, 40, state)
1318
1319    -- Cursor Horizontal Tab
1320    push('\x1b[I', vt)
1321    cursor(0, 48, state)
1322    push('\x1b[2I', vt)
1323    cursor(0, 64, state)
1324
1325    -- Cursor Backward Tab
1326    push('\x1b[Z', vt)
1327    cursor(0, 56, state)
1328    push('\x1b[2Z', vt)
1329    cursor(0, 40, state)
1330  end)
1331
1332  itp('12state_scroll', function()
1333    local vt = init()
1334    local state = wantstate(vt, { s = true })
1335
1336    -- Linefeed
1337    push(string.rep('\n', 24), vt)
1338    cursor(24, 0, state)
1339    push('\n', vt)
1340    expect('scrollrect 0..25,0..80 => +1,+0')
1341    cursor(24, 0, state)
1342
1343    reset(state, nil)
1344
1345    -- Index
1346    push('\x1b[25H', vt)
1347    push('\x1bD', vt)
1348    expect('scrollrect 0..25,0..80 => +1,+0')
1349
1350    reset(state, nil)
1351
1352    -- Reverse Index
1353    push('\x1bM', vt)
1354    expect('scrollrect 0..25,0..80 => -1,+0')
1355
1356    reset(state, nil)
1357
1358    -- Linefeed in DECSTBM
1359    push('\x1b[1;10r', vt)
1360    cursor(0, 0, state)
1361    push(string.rep('\n', 9), vt)
1362    cursor(9, 0, state)
1363    push('\n', vt)
1364    expect('scrollrect 0..10,0..80 => +1,+0')
1365    cursor(9, 0, state)
1366
1367    -- Linefeed outside DECSTBM
1368    push('\x1b[20H', vt)
1369    cursor(19, 0, state)
1370    push('\n', vt)
1371    cursor(20, 0, state)
1372
1373    -- Index in DECSTBM
1374    push('\x1b[9;10r', vt)
1375    push('\x1b[10H', vt)
1376    push('\x1bM', vt)
1377    cursor(8, 0, state)
1378    push('\x1bM', vt)
1379    expect('scrollrect 8..10,0..80 => -1,+0')
1380
1381    -- Reverse Index in DECSTBM
1382    push('\x1b[25H', vt)
1383    cursor(24, 0, state)
1384    push('\n', vt)
1385    -- no scrollrect
1386    cursor(24, 0, state)
1387
1388    -- Linefeed in DECSTBM+DECSLRM
1389    push('\x1b[?69h', vt)
1390    push('\x1b[3;10r\x1b[10;40s', vt)
1391    push('\x1b[10;10H\n', vt)
1392    expect('scrollrect 2..10,9..40 => +1,+0')
1393
1394    -- IND/RI in DECSTBM+DECSLRM
1395    push('\x1bD', vt)
1396    expect('scrollrect 2..10,9..40 => +1,+0')
1397    push('\x1b[3;10H\x1bM', vt)
1398    expect('scrollrect 2..10,9..40 => -1,+0')
1399
1400    -- DECRQSS on DECSTBM
1401    push('\x1bP$qr\x1b\\', vt)
1402    expect_output('\x1bP1$r3;10r\x1b\\')
1403
1404    -- DECRQSS on DECSLRM
1405    push('\x1bP$qs\x1b\\', vt)
1406    expect_output('\x1bP1$r10;40s\x1b\\')
1407
1408    -- Setting invalid DECSLRM with !DECVSSM is still rejected
1409    push('\x1b[?69l\x1b[;0s\x1b[?69h', vt)
1410
1411    reset(state, nil)
1412
1413    -- Scroll Down
1414    push('\x1b[S', vt)
1415    expect('scrollrect 0..25,0..80 => +1,+0')
1416    cursor(0, 0, state)
1417    push('\x1b[2S', vt)
1418    expect('scrollrect 0..25,0..80 => +2,+0')
1419    cursor(0, 0, state)
1420    push('\x1b[100S', vt)
1421    expect('scrollrect 0..25,0..80 => +25,+0')
1422
1423    -- Scroll Up
1424    push('\x1b[T', vt)
1425    expect('scrollrect 0..25,0..80 => -1,+0')
1426    cursor(0, 0, state)
1427    push('\x1b[2T', vt)
1428    expect('scrollrect 0..25,0..80 => -2,+0')
1429    cursor(0, 0, state)
1430    push('\x1b[100T', vt)
1431    expect('scrollrect 0..25,0..80 => -25,+0')
1432
1433    -- SD/SU in DECSTBM
1434    push('\x1b[5;20r', vt)
1435    push('\x1b[S', vt)
1436    expect('scrollrect 4..20,0..80 => +1,+0')
1437    push('\x1b[T', vt)
1438    expect('scrollrect 4..20,0..80 => -1,+0')
1439
1440    reset(state, nil)
1441
1442    -- SD/SU in DECSTBM+DECSLRM
1443    push('\x1b[?69h', vt)
1444    push('\x1b[3;10r\x1b[10;40s', vt)
1445    cursor(0, 0, state)
1446    push('\x1b[3;10H', vt)
1447    cursor(2, 9, state)
1448    push('\x1b[S', vt)
1449    expect('scrollrect 2..10,9..40 => +1,+0')
1450    push('\x1b[?69l', vt)
1451    push('\x1b[S', vt)
1452    expect('scrollrect 2..10,0..80 => +1,+0')
1453
1454    -- Invalid boundaries
1455    reset(state, nil)
1456
1457    push('\x1b[100;105r\x1bD', vt)
1458    push('\x1b[5;2r\x1bD', vt)
1459
1460    reset(state, nil)
1461    state = wantstate(vt, { m = true, e = true })
1462
1463    -- Scroll Down move+erase emulation
1464    push('\x1b[S', vt)
1465    expect('moverect 1..25,0..80 -> 0..24,0..80\nerase 24..25,0..80')
1466    cursor(0, 0, state)
1467    push('\x1b[2S', vt)
1468    expect('moverect 2..25,0..80 -> 0..23,0..80\nerase 23..25,0..80')
1469    cursor(0, 0, state)
1470
1471    -- Scroll Up move+erase emulation
1472    push('\x1b[T', vt)
1473    expect('moverect 0..24,0..80 -> 1..25,0..80\nerase 0..1,0..80')
1474    cursor(0, 0, state)
1475    push('\x1b[2T', vt)
1476    expect('moverect 0..23,0..80 -> 2..25,0..80\nerase 0..2,0..80')
1477    cursor(0, 0, state)
1478
1479    -- DECSTBM resets cursor position
1480    push('\x1b[5;5H', vt)
1481    cursor(4, 4, state)
1482    push('\x1b[r', vt)
1483    cursor(0, 0, state)
1484  end)
1485
1486  itp('13state_edit', function()
1487    local vt = init()
1488    local state = wantstate(vt, { s = true, e = true, b = true })
1489
1490    -- ICH
1491    reset(state, nil)
1492    expect('erase 0..25,0..80')
1493    cursor(0, 0, state)
1494    push('ACD', vt)
1495    push('\x1b[2D', vt)
1496    cursor(0, 1, state)
1497    push('\x1b[@', vt)
1498    expect('scrollrect 0..1,1..80 => +0,-1')
1499    cursor(0, 1, state)
1500    push('B', vt)
1501    cursor(0, 2, state)
1502    push('\x1b[3@', vt)
1503    expect('scrollrect 0..1,2..80 => +0,-3')
1504
1505    -- ICH with DECSLRM
1506    push('\x1b[?69h', vt)
1507    push('\x1b[;50s', vt)
1508    push('\x1b[20G\x1b[@', vt)
1509    expect('scrollrect 0..1,19..50 => +0,-1')
1510
1511    -- ICH outside DECSLRM
1512    push('\x1b[70G\x1b[@', vt)
1513    -- nothing happens
1514
1515    -- DCH
1516    reset(state, nil)
1517    expect('erase 0..25,0..80')
1518    cursor(0, 0, state)
1519    push('ABBC', vt)
1520    push('\x1b[3D', vt)
1521    cursor(0, 1, state)
1522    push('\x1b[P', vt)
1523    expect('scrollrect 0..1,1..80 => +0,+1')
1524    cursor(0, 1, state)
1525    push('\x1b[3P', vt)
1526    expect('scrollrect 0..1,1..80 => +0,+3')
1527    cursor(0, 1, state)
1528
1529    -- DCH with DECSLRM
1530    push('\x1b[?69h', vt)
1531    push('\x1b[;50s', vt)
1532    push('\x1b[20G\x1b[P', vt)
1533    expect('scrollrect 0..1,19..50 => +0,+1')
1534
1535    -- DCH outside DECSLRM
1536    push('\x1b[70G\x1b[P', vt)
1537    -- nothing happens
1538
1539    -- ECH
1540    reset(state, nil)
1541    expect('erase 0..25,0..80')
1542    cursor(0, 0, state)
1543    push('ABC', vt)
1544    push('\x1b[2D', vt)
1545    cursor(0, 1, state)
1546    push('\x1b[X', vt)
1547    expect('erase 0..1,1..2')
1548    cursor(0, 1, state)
1549    push('\x1b[3X', vt)
1550    expect('erase 0..1,1..4')
1551    cursor(0, 1, state)
1552    -- ECH more columns than there are should be bounded
1553    push('\x1b[100X', vt)
1554    expect('erase 0..1,1..80')
1555
1556    -- IL
1557    reset(state, nil)
1558    expect('erase 0..25,0..80')
1559    cursor(0, 0, state)
1560    push('A\r\nC', vt)
1561    cursor(1, 1, state)
1562    push('\x1b[L', vt)
1563    expect('scrollrect 1..25,0..80 => -1,+0')
1564    -- TODO(libvterm): ECMA-48 says we should move to line home, but neither xterm nor xfce4-terminal do this
1565    cursor(1, 1, state)
1566    push('\rB', vt)
1567    cursor(1, 1, state)
1568    push('\x1b[3L', vt)
1569    expect('scrollrect 1..25,0..80 => -3,+0')
1570
1571    -- IL with DECSTBM
1572    push('\x1b[5;15r', vt)
1573    push('\x1b[5H\x1b[L', vt)
1574    expect('scrollrect 4..15,0..80 => -1,+0')
1575
1576    -- IL outside DECSTBM
1577    push('\x1b[20H\x1b[L', vt)
1578    -- nothing happens
1579
1580    -- IL with DECSTBM+DECSLRM
1581    push('\x1b[?69h', vt)
1582    push('\x1b[10;50s', vt)
1583    push('\x1b[5;10H\x1b[L', vt)
1584    expect('scrollrect 4..15,9..50 => -1,+0')
1585
1586    -- DL
1587    reset(state, nil)
1588    expect('erase 0..25,0..80')
1589    cursor(0, 0, state)
1590    push('A\r\nB\r\nB\r\nC', vt)
1591    cursor(3, 1, state)
1592    push('\x1b[2H', vt)
1593    cursor(1, 0, state)
1594    push('\x1b[M', vt)
1595    expect('scrollrect 1..25,0..80 => +1,+0')
1596    cursor(1, 0, state)
1597    push('\x1b[3M', vt)
1598    expect('scrollrect 1..25,0..80 => +3,+0')
1599    cursor(1, 0, state)
1600
1601    -- DL with DECSTBM
1602    push('\x1b[5;15r', vt)
1603    push('\x1b[5H\x1b[M', vt)
1604    expect('scrollrect 4..15,0..80 => +1,+0')
1605
1606    -- DL outside DECSTBM
1607    push('\x1b[20H\x1b[M', vt)
1608    -- nothing happens
1609
1610    -- DL with DECSTBM+DECSLRM
1611    push('\x1b[?69h', vt)
1612    push('\x1b[10;50s', vt)
1613    push('\x1b[5;10H\x1b[M', vt)
1614    expect('scrollrect 4..15,9..50 => +1,+0')
1615
1616    -- DECIC
1617    reset(state, nil)
1618    expect('erase 0..25,0..80')
1619    push("\x1b[20G\x1b[5'}", vt)
1620    expect('scrollrect 0..25,19..80 => +0,-5')
1621
1622    -- DECIC with DECSTBM+DECSLRM
1623    push('\x1b[?69h', vt)
1624    push('\x1b[4;20r\x1b[20;60s', vt)
1625    push("\x1b[4;20H\x1b[3'}", vt)
1626    expect('scrollrect 3..20,19..60 => +0,-3')
1627
1628    -- DECIC outside DECSLRM
1629    push("\x1b[70G\x1b['}", vt)
1630    -- nothing happens
1631
1632    -- DECDC
1633    reset(state, nil)
1634    expect('erase 0..25,0..80')
1635    push("\x1b[20G\x1b[5'~", vt)
1636    expect('scrollrect 0..25,19..80 => +0,+5')
1637
1638    -- DECDC with DECSTBM+DECSLRM
1639    push('\x1b[?69h', vt)
1640    push('\x1b[4;20r\x1b[20;60s', vt)
1641    push("\x1b[4;20H\x1b[3'~", vt)
1642    expect('scrollrect 3..20,19..60 => +0,+3')
1643
1644    -- DECDC outside DECSLRM
1645    push("\x1b[70G\x1b['~", vt)
1646    -- nothing happens
1647
1648    -- EL 0
1649    reset(state, nil)
1650    expect('erase 0..25,0..80')
1651    cursor(0, 0, state)
1652    push('ABCDE', vt)
1653    push('\x1b[3D', vt)
1654    cursor(0, 2, state)
1655    push('\x1b[0K', vt)
1656    expect('erase 0..1,2..80')
1657    cursor(0, 2, state)
1658
1659    -- EL 1
1660    reset(state, nil)
1661    expect('erase 0..25,0..80')
1662    cursor(0, 0, state)
1663    push('ABCDE', vt)
1664    push('\x1b[3D', vt)
1665    cursor(0, 2, state)
1666    push('\x1b[1K', vt)
1667    expect('erase 0..1,0..3')
1668    cursor(0, 2, state)
1669
1670    -- EL 2
1671    reset(state, nil)
1672    expect('erase 0..25,0..80')
1673    cursor(0, 0, state)
1674    push('ABCDE', vt)
1675    push('\x1b[3D', vt)
1676    cursor(0, 2, state)
1677    push('\x1b[2K', vt)
1678    expect('erase 0..1,0..80')
1679    cursor(0, 2, state)
1680
1681    -- SEL
1682    reset(state, nil)
1683    expect('erase 0..25,0..80')
1684    cursor(0, 0, state)
1685    push('\x1b[11G', vt)
1686    cursor(0, 10, state)
1687    push('\x1b[?0K', vt)
1688    expect('erase 0..1,10..80 selective')
1689    cursor(0, 10, state)
1690    push('\x1b[?1K', vt)
1691    expect('erase 0..1,0..11 selective')
1692    cursor(0, 10, state)
1693    push('\x1b[?2K', vt)
1694    expect('erase 0..1,0..80 selective')
1695    cursor(0, 10, state)
1696
1697    -- ED 0
1698    reset(state, nil)
1699    expect('erase 0..25,0..80')
1700    cursor(0, 0, state)
1701    push('\x1b[2;2H', vt)
1702    cursor(1, 1, state)
1703    push('\x1b[0J', vt)
1704    expect('erase 1..2,1..80\nerase 2..25,0..80')
1705    cursor(1, 1, state)
1706
1707    -- ED 1
1708    reset(state, nil)
1709    expect('erase 0..25,0..80')
1710    cursor(0, 0, state)
1711    push('\x1b[2;2H', vt)
1712    cursor(1, 1, state)
1713    push('\x1b[1J', vt)
1714    expect('erase 0..1,0..80\nerase 1..2,0..2')
1715    cursor(1, 1, state)
1716
1717    -- ED 2
1718    reset(state, nil)
1719    expect('erase 0..25,0..80')
1720    cursor(0, 0, state)
1721    push('\x1b[2;2H', vt)
1722    cursor(1, 1, state)
1723    push('\x1b[2J', vt)
1724    expect('erase 0..25,0..80')
1725    cursor(1, 1, state)
1726
1727    -- ED 3
1728    push('\x1b[3J', vt)
1729    expect('sb_clear')
1730
1731    -- SED
1732    reset(state, nil)
1733    expect('erase 0..25,0..80')
1734    push('\x1b[5;5H', vt)
1735    cursor(4, 4, state)
1736    push('\x1b[?0J', vt)
1737    expect('erase 4..5,4..80 selective\nerase 5..25,0..80 selective')
1738    cursor(4, 4, state)
1739    push('\x1b[?1J', vt)
1740    expect('erase 0..4,0..80 selective\nerase 4..5,0..5 selective')
1741    cursor(4, 4, state)
1742    push('\x1b[?2J', vt)
1743    expect('erase 0..25,0..80 selective')
1744    cursor(4, 4, state)
1745
1746    -- DECRQSS on DECSCA
1747    push('\x1b[2"q', vt)
1748    push('\x1bP$q"q\x1b\\', vt)
1749    expect_output('\x1bP1$r2"q\x1b\\')
1750
1751    state = wantstate(vt, { m = true, e = true, b = true })
1752    expect('erase 0..25,0..80') -- TODO(dundargoc): strange, this should not be needed according to the original code
1753
1754    -- ICH move+erase emuation
1755    reset(state, nil)
1756    expect('erase 0..25,0..80')
1757    cursor(0, 0, state)
1758    push('ACD', vt)
1759    push('\x1b[2D', vt)
1760    cursor(0, 1, state)
1761    push('\x1b[@', vt)
1762    expect('moverect 0..1,1..79 -> 0..1,2..80\nerase 0..1,1..2')
1763    cursor(0, 1, state)
1764    push('B', vt)
1765    cursor(0, 2, state)
1766    push('\x1b[3@', vt)
1767    expect('moverect 0..1,2..77 -> 0..1,5..80\nerase 0..1,2..5')
1768
1769    -- DCH move+erase emulation
1770    reset(state, nil)
1771    expect('erase 0..25,0..80')
1772    cursor(0, 0, state)
1773    push('ABBC', vt)
1774    push('\x1b[3D', vt)
1775    cursor(0, 1, state)
1776    push('\x1b[P', vt)
1777    expect('moverect 0..1,2..80 -> 0..1,1..79\nerase 0..1,79..80')
1778    cursor(0, 1, state)
1779    push('\x1b[3P', vt)
1780    expect('moverect 0..1,4..80 -> 0..1,1..77\nerase 0..1,77..80')
1781    cursor(0, 1, state)
1782  end)
1783
1784  itp('14state_encoding', function()
1785    local vt = init()
1786    vterm.vterm_set_utf8(vt, false)
1787    local state = wantstate(vt, { g = true })
1788
1789    -- Default
1790    reset(state, nil)
1791    push('#', vt)
1792    expect('putglyph 23 1 0,0')
1793
1794    -- Designate G0=DEC drawing
1795    reset(state, nil)
1796    push('\x1b(0', vt)
1797    push('a', vt)
1798    expect('putglyph 2592 1 0,0')
1799
1800    -- Designate G1 + LS1
1801    reset(state, nil)
1802    push('\x1b)0', vt)
1803    push('a', vt)
1804    expect('putglyph 61 1 0,0')
1805    push('\x0e', vt)
1806    push('a', vt)
1807    expect('putglyph 2592 1 0,1')
1808    -- LS0
1809    push('\x0f', vt)
1810    push('a', vt)
1811    expect('putglyph 61 1 0,2')
1812
1813    -- Designate G2 + LS2
1814    push('\x1b*0', vt)
1815    push('a', vt)
1816    expect('putglyph 61 1 0,3')
1817    push('\x1bn', vt)
1818    push('a', vt)
1819    expect('putglyph 2592 1 0,4')
1820    push('\x0f', vt)
1821    push('a', vt)
1822    expect('putglyph 61 1 0,5')
1823
1824    -- Designate G3 + LS3
1825    push('\x1b+0', vt)
1826    push('a', vt)
1827    expect('putglyph 61 1 0,6')
1828    push('\x1bo', vt)
1829    push('a', vt)
1830    expect('putglyph 2592 1 0,7')
1831    push('\x0f', vt)
1832    push('a', vt)
1833    expect('putglyph 61 1 0,8')
1834
1835    -- SS2
1836    push('a\x8eaa', vt)
1837    expect('putglyph 61 1 0,9\nputglyph 2592 1 0,10\nputglyph 61 1 0,11')
1838
1839    -- SS3
1840    push('a\x8faa', vt)
1841    expect('putglyph 61 1 0,12\nputglyph 2592 1 0,13\nputglyph 61 1 0,14')
1842
1843    -- LS1R
1844    reset(state, nil)
1845    push('\x1b~', vt)
1846    push('\xe1', vt)
1847    expect('putglyph 61 1 0,0')
1848    push('\x1b)0', vt)
1849    push('\xe1', vt)
1850    expect('putglyph 2592 1 0,1')
1851
1852    -- LS2R
1853    reset(state, nil)
1854    push('\x1b}', vt)
1855    push('\xe1', vt)
1856    expect('putglyph 61 1 0,0')
1857    push('\x1b*0', vt)
1858    push('\xe1', vt)
1859    expect('putglyph 2592 1 0,1')
1860
1861    -- LS3R
1862    reset(state, nil)
1863    push('\x1b|', vt)
1864    push('\xe1', vt)
1865    expect('putglyph 61 1 0,0')
1866    push('\x1b+0', vt)
1867    push('\xe1', vt)
1868    expect('putglyph 2592 1 0,1')
1869
1870    vterm.vterm_set_utf8(vt, true)
1871
1872    -- Mixed US-ASCII and UTF-8
1873    -- U+0108 == c4 88
1874    reset(state, nil)
1875    push('\x1b(B', vt)
1876    push('AB\xc4\x88D', vt)
1877    expect('putglyph 41 1 0,0\nputglyph 42 1 0,1\nputglyph 108 1 0,2\nputglyph 44 1 0,3')
1878
1879    -- Split UTF-8 after US-ASCII
1880    reset(state, nil)
1881    push('AB\xc4', vt)
1882    expect('putglyph 41 1 0,0\nputglyph 42 1 0,1')
1883    push('\x88D', vt)
1884    expect('putglyph 108 1 0,2\nputglyph 44 1 0,3')
1885  end)
1886
1887  itp('15state_mode', function()
1888    local vt = init()
1889    local state = wantstate(vt, { g = true, m = true, e = true })
1890
1891    -- Insert/Replace Mode
1892    reset(state, nil)
1893    expect('erase 0..25,0..80')
1894    cursor(0, 0, state)
1895    push('AC\x1b[DB', vt)
1896    expect('putglyph 41 1 0,0\nputglyph 43 1 0,1\nputglyph 42 1 0,1')
1897    push('\x1b[4h', vt)
1898    push('\x1b[G', vt)
1899    push('AC\x1b[DB', vt)
1900    expect(
1901      'moverect 0..1,0..79 -> 0..1,1..80\nerase 0..1,0..1\nputglyph 41 1 0,0\nmoverect 0..1,1..79 -> 0..1,2..80\nerase 0..1,1..2\nputglyph 43 1 0,1\nmoverect 0..1,1..79 -> 0..1,2..80\nerase 0..1,1..2\nputglyph 42 1 0,1'
1902    )
1903
1904    -- Insert mode only happens once for UTF-8 combining
1905    push('e', vt)
1906    expect('moverect 0..1,2..79 -> 0..1,3..80\nerase 0..1,2..3\nputglyph 65 1 0,2')
1907    push('\xCC\x81', vt)
1908    expect('putglyph 65,301 1 0,2')
1909
1910    -- Newline/Linefeed mode
1911    reset(state, nil)
1912    expect('erase 0..25,0..80')
1913    cursor(0, 0, state)
1914    push('\x1b[5G\n', vt)
1915    cursor(1, 4, state)
1916    push('\x1b[20h', vt)
1917    push('\x1b[5G\n', vt)
1918    cursor(2, 0, state)
1919
1920    -- DEC origin mode
1921    reset(state, nil)
1922    expect('erase 0..25,0..80')
1923    cursor(0, 0, state)
1924    push('\x1b[5;15r', vt)
1925    push('\x1b[H', vt)
1926    cursor(0, 0, state)
1927    push('\x1b[3;3H', vt)
1928    cursor(2, 2, state)
1929    push('\x1b[?6h', vt)
1930    push('\x1b[H', vt)
1931    cursor(4, 0, state)
1932    push('\x1b[3;3H', vt)
1933    cursor(6, 2, state)
1934
1935    -- DECRQM on DECOM
1936    push('\x1b[?6h', vt)
1937    push('\x1b[?6$p', vt)
1938    expect_output('\x1b[?6;1$y')
1939    push('\x1b[?6l', vt)
1940    push('\x1b[?6$p', vt)
1941    expect_output('\x1b[?6;2$y')
1942
1943    -- Origin mode with DECSLRM
1944    push('\x1b[?6h', vt)
1945    push('\x1b[?69h', vt)
1946    push('\x1b[20;60s', vt)
1947    push('\x1b[H', vt)
1948    cursor(4, 19, state)
1949
1950    push('\x1b[?69l', vt)
1951
1952    -- Origin mode bounds cursor to scrolling region
1953    push('\x1b[H', vt)
1954    push('\x1b[10A', vt)
1955    cursor(4, 0, state)
1956    push('\x1b[20B', vt)
1957    cursor(14, 0, state)
1958
1959    -- Origin mode without scroll region
1960    push('\x1b[?6l', vt)
1961    push('\x1b[r\x1b[?6h', vt)
1962    cursor(0, 0, state)
1963  end)
1964
1965  itp('16state_resize', function()
1966    local vt = init()
1967    local state = wantstate(vt, { g = true })
1968
1969    -- Placement
1970    reset(state, nil)
1971    push('AB\x1b[79GCDE', vt)
1972    expect(
1973      'putglyph 41 1 0,0\nputglyph 42 1 0,1\nputglyph 43 1 0,78\nputglyph 44 1 0,79\nputglyph 45 1 1,0'
1974    )
1975
1976    -- Resize
1977    reset(state, nil)
1978    resize(27, 85, vt)
1979    push('AB\x1b[79GCDE', vt)
1980    expect(
1981      'putglyph 41 1 0,0\nputglyph 42 1 0,1\nputglyph 43 1 0,78\nputglyph 44 1 0,79\nputglyph 45 1 0,80'
1982    )
1983    cursor(0, 81, state)
1984
1985    -- Resize without reset
1986    resize(28, 90, vt)
1987    cursor(0, 81, state)
1988    push('FGHI', vt)
1989    expect('putglyph 46 1 0,81\nputglyph 47 1 0,82\nputglyph 48 1 0,83\nputglyph 49 1 0,84')
1990    cursor(0, 85, state)
1991
1992    -- Resize shrink moves cursor
1993    resize(25, 80, vt)
1994    cursor(0, 79, state)
1995
1996    -- Resize grow doesn't cancel phantom
1997    reset(state, nil)
1998    push('\x1b[79GAB', vt)
1999    expect('putglyph 41 1 0,78\nputglyph 42 1 0,79')
2000    cursor(0, 79, state)
2001    resize(30, 100, vt)
2002    cursor(0, 80, state)
2003    push('C', vt)
2004    expect('putglyph 43 1 0,80')
2005    cursor(0, 81, state)
2006  end)
2007
2008  itp('17state_mouse', function()
2009    local vt = init()
2010    local state = wantstate(vt, { p = true })
2011
2012    -- DECRQM on with mouse off
2013    push('\x1b[?1000$p', vt)
2014    expect_output('\x1b[?1000;2$y')
2015    push('\x1b[?1002$p', vt)
2016    expect_output('\x1b[?1002;2$y')
2017    push('\x1b[?1003$p', vt)
2018    expect_output('\x1b[?1003;2$y')
2019
2020    -- Mouse in simple button report mode
2021    reset(state, nil)
2022    expect('settermprop 1 true\nsettermprop 2 true\nsettermprop 7 1')
2023    push('\x1b[?1000h', vt)
2024    expect('settermprop 8 1')
2025
2026    -- Press 1
2027    mousemove(0, 0, vt)
2028    mousebtn('d', 1, vt)
2029    expect_output('\x1b[M\x20\x21\x21')
2030
2031    -- Release 1
2032    mousebtn('u', 1, vt)
2033    expect_output('\x1b[M\x23\x21\x21')
2034
2035    -- Ctrl-Press 1
2036    mousebtn('d', 1, vt, { C = true })
2037    expect_output('\x1b[M\x30\x21\x21')
2038    mousebtn('u', 1, vt, { C = true })
2039    expect_output('\x1b[M\x33\x21\x21')
2040
2041    -- Button 2
2042    mousebtn('d', 2, vt)
2043    expect_output('\x1b[M\x21\x21\x21')
2044    mousebtn('u', 2, vt)
2045    expect_output('\x1b[M\x23\x21\x21')
2046
2047    -- Position
2048    mousemove(10, 20, vt)
2049    mousebtn('d', 1, vt)
2050    expect_output('\x1b[M\x20\x35\x2b')
2051
2052    mousebtn('u', 1, vt)
2053    expect_output('\x1b[M\x23\x35\x2b')
2054    mousemove(10, 21, vt)
2055    -- no output
2056
2057    -- Wheel events
2058    mousebtn('d', 4, vt)
2059    expect_output('\x1b[M\x60\x36\x2b')
2060    mousebtn('d', 4, vt)
2061    expect_output('\x1b[M\x60\x36\x2b')
2062    mousebtn('d', 5, vt)
2063    expect_output('\x1b[M\x61\x36\x2b')
2064    mousebtn('d', 6, vt)
2065    expect_output('\x1b[M\x62\x36\x2b')
2066    mousebtn('d', 7, vt)
2067    expect_output('\x1b[M\x63\x36\x2b')
2068
2069    -- DECRQM on mouse button mode
2070    push('\x1b[?1000$p', vt)
2071    expect_output('\x1b[?1000;1$y')
2072    push('\x1b[?1002$p', vt)
2073    expect_output('\x1b[?1002;2$y')
2074    push('\x1b[?1003$p', vt)
2075    expect_output('\x1b[?1003;2$y')
2076
2077    -- Drag events
2078    reset(state, nil)
2079    expect('settermprop 1 true\nsettermprop 2 true\nsettermprop 7 1')
2080    push('\x1b[?1002h', vt)
2081    expect('settermprop 8 2')
2082
2083    mousemove(5, 5, vt)
2084    mousebtn('d', 1, vt)
2085    expect_output('\x1b[M\x20\x26\x26')
2086    mousemove(5, 6, vt)
2087    expect_output('\x1b[M\x40\x27\x26')
2088    mousemove(6, 6, vt)
2089    expect_output('\x1b[M\x40\x27\x27')
2090    mousemove(6, 6, vt)
2091    -- no output
2092    mousebtn('u', 1, vt)
2093    expect_output('\x1b[M\x23\x27\x27')
2094    mousemove(6, 7, vt)
2095    -- no output
2096
2097    -- DECRQM on mouse drag mode
2098    push('\x1b[?1000$p', vt)
2099    expect_output('\x1b[?1000;2$y')
2100    push('\x1b[?1002$p', vt)
2101    expect_output('\x1b[?1002;1$y')
2102    push('\x1b[?1003$p', vt)
2103    expect_output('\x1b[?1003;2$y')
2104
2105    -- Non-drag motion events
2106    push('\x1b[?1003h', vt)
2107    expect('settermprop 8 3')
2108
2109    mousemove(6, 8, vt)
2110    expect_output('\x1b[M\x43\x29\x27')
2111
2112    -- DECRQM on mouse motion mode
2113    push('\x1b[?1000$p', vt)
2114    expect_output('\x1b[?1000;2$y')
2115    push('\x1b[?1002$p', vt)
2116    expect_output('\x1b[?1002;2$y')
2117    push('\x1b[?1003$p', vt)
2118    expect_output('\x1b[?1003;1$y')
2119
2120    -- Bounds checking
2121    mousemove(300, 300, vt)
2122    expect_output('\x1b[M\x43\xff\xff')
2123    mousebtn('d', 1, vt)
2124    expect_output('\x1b[M\x20\xff\xff')
2125    mousebtn('u', 1, vt)
2126    expect_output('\x1b[M\x23\xff\xff')
2127
2128    -- DECRQM on standard encoding mode
2129    push('\x1b[?1005$p', vt)
2130    expect_output('\x1b[?1005;2$y')
2131    push('\x1b[?1006$p', vt)
2132    expect_output('\x1b[?1006;2$y')
2133    push('\x1b[?1015$p', vt)
2134    expect_output('\x1b[?1015;2$y')
2135
2136    -- UTF-8 extended encoding mode
2137    -- 300 + 32 + 1 = 333 = U+014d = \xc5\x8d
2138    push('\x1b[?1005h', vt)
2139    mousebtn('d', 1, vt)
2140    expect_output('\x1b[M\x20\xc5\x8d\xc5\x8d')
2141    mousebtn('u', 1, vt)
2142    expect_output('\x1b[M\x23\xc5\x8d\xc5\x8d')
2143
2144    -- DECRQM on UTF-8 extended encoding mode
2145    push('\x1b[?1005$p', vt)
2146    expect_output('\x1b[?1005;1$y')
2147    push('\x1b[?1006$p', vt)
2148    expect_output('\x1b[?1006;2$y')
2149    push('\x1b[?1015$p', vt)
2150    expect_output('\x1b[?1015;2$y')
2151
2152    -- SGR extended encoding mode
2153    push('\x1b[?1006h', vt)
2154    mousebtn('d', 1, vt)
2155    expect_output('\x1b[<0;301;301M')
2156    mousebtn('u', 1, vt)
2157    expect_output('\x1b[<0;301;301m')
2158
2159    -- Button 8 on SGR extended encoding mode
2160    mousebtn('d', 8, vt)
2161    expect_output('\x1b[<128;301;301M')
2162    mousebtn('u', 8, vt)
2163    expect_output('\x1b[<128;301;301m')
2164
2165    -- Button 9 on SGR extended encoding mode
2166    mousebtn('d', 9, vt)
2167    expect_output('\x1b[<129;301;301M')
2168    mousebtn('u', 9, vt)
2169    expect_output('\x1b[<129;301;301m')
2170
2171    -- DECRQM on SGR extended encoding mode
2172    push('\x1b[?1005$p', vt)
2173    expect_output('\x1b[?1005;2$y')
2174    push('\x1b[?1006$p', vt)
2175    expect_output('\x1b[?1006;1$y')
2176    push('\x1b[?1015$p', vt)
2177    expect_output('\x1b[?1015;2$y')
2178
2179    -- rxvt extended encoding mode
2180    push('\x1b[?1015h', vt)
2181    mousebtn('d', 1, vt)
2182    expect_output('\x1b[0;301;301M')
2183    mousebtn('u', 1, vt)
2184    expect_output('\x1b[3;301;301M')
2185
2186    -- Button 8 on rxvt extended encoding mode
2187    mousebtn('d', 8, vt)
2188    expect_output('\x1b[128;301;301M')
2189    mousebtn('u', 8, vt)
2190    expect_output('\x1b[3;301;301M')
2191
2192    -- Button 9 on rxvt extended encoding mode
2193    mousebtn('d', 9, vt)
2194    expect_output('\x1b[129;301;301M')
2195    mousebtn('u', 9, vt)
2196    expect_output('\x1b[3;301;301M')
2197
2198    -- DECRQM on rxvt extended encoding mode
2199    push('\x1b[?1005$p', vt)
2200    expect_output('\x1b[?1005;2$y')
2201    push('\x1b[?1006$p', vt)
2202    expect_output('\x1b[?1006;2$y')
2203    push('\x1b[?1015$p', vt)
2204    expect_output('\x1b[?1015;1$y')
2205
2206    -- Mouse disabled reports nothing
2207    reset(state, nil)
2208    expect('settermprop 1 true\nsettermprop 2 true\nsettermprop 7 1')
2209    mousemove(0, 0, vt)
2210    mousebtn('d', 1, vt)
2211    mousebtn('u', 1, vt)
2212
2213    -- DECSM can set multiple modes at once
2214    push('\x1b[?1002;1006h', vt)
2215    expect('settermprop 8 2')
2216    mousebtn('d', 1, vt)
2217    expect_output('\x1b[<0;1;1M')
2218  end)
2219
2220  itp('18state_termprops', function()
2221    local vt = init()
2222    local state = wantstate(vt, { p = true })
2223
2224    reset(state, nil)
2225    expect('settermprop 1 true\nsettermprop 2 true\nsettermprop 7 1')
2226
2227    -- Cursor visibility
2228    push('\x1b[?25h', vt)
2229    expect('settermprop 1 true')
2230    push('\x1b[?25$p', vt)
2231    expect_output('\x1b[?25;1$y')
2232    push('\x1b[?25l', vt)
2233    expect('settermprop 1 false')
2234    push('\x1b[?25$p', vt)
2235    expect_output('\x1b[?25;2$y')
2236
2237    -- Cursor blink
2238    push('\x1b[?12h', vt)
2239    expect('settermprop 2 true')
2240    push('\x1b[?12$p', vt)
2241    expect_output('\x1b[?12;1$y')
2242    push('\x1b[?12l', vt)
2243    expect('settermprop 2 false')
2244    push('\x1b[?12$p', vt)
2245    expect_output('\x1b[?12;2$y')
2246
2247    -- Cursor shape
2248    push('\x1b[3 q', vt)
2249    expect('settermprop 2 true\nsettermprop 7 2')
2250
2251    -- Title
2252    push('\x1b]2;Here is my title\a', vt)
2253    expect('settermprop 4 ["Here is my title"]')
2254
2255    -- Title split write
2256    push('\x1b]2;Here is', vt)
2257    expect('settermprop 4 ["Here is"')
2258    push(' another title\a', vt)
2259    expect('settermprop 4 " another title"]')
2260  end)
2261
2262  itp('20state_wrapping', function()
2263    local vt = init()
2264    local state = wantstate(vt, { g = true, m = true })
2265
2266    -- 79th Column
2267    push('\x1b[75G', vt)
2268    push(string.rep('A', 5), vt)
2269    expect(
2270      'putglyph 41 1 0,74\nputglyph 41 1 0,75\nputglyph 41 1 0,76\nputglyph 41 1 0,77\nputglyph 41 1 0,78'
2271    )
2272    cursor(0, 79, state)
2273
2274    -- 80th Column Phantom
2275    push('A', vt)
2276    expect('putglyph 41 1 0,79')
2277    cursor(0, 79, state)
2278
2279    -- Line Wraparound
2280    push('B', vt)
2281    expect('putglyph 42 1 1,0')
2282    cursor(1, 1, state)
2283
2284    -- Line Wraparound during combined write
2285    push('\x1b[78G', vt)
2286    push('BBBCC', vt)
2287    expect(
2288      'putglyph 42 1 1,77\nputglyph 42 1 1,78\nputglyph 42 1 1,79\nputglyph 43 1 2,0\nputglyph 43 1 2,1'
2289    )
2290    cursor(2, 2, state)
2291
2292    -- DEC Auto Wrap Mode
2293    reset(state, nil)
2294    push('\x1b[?7l', vt)
2295    push('\x1b[75G', vt)
2296    push(string.rep('D', 6), vt)
2297    expect(
2298      'putglyph 44 1 0,74\nputglyph 44 1 0,75\nputglyph 44 1 0,76\nputglyph 44 1 0,77\nputglyph 44 1 0,78\nputglyph 44 1 0,79'
2299    )
2300    cursor(0, 79, state)
2301    push('D', vt)
2302    expect('putglyph 44 1 0,79')
2303    cursor(0, 79, state)
2304    push('\x1b[?7h', vt)
2305
2306    -- 80th column causes linefeed on wraparound
2307    push('\x1b[25;78HABC', vt)
2308    expect('putglyph 41 1 24,77\nputglyph 42 1 24,78\nputglyph 43 1 24,79')
2309    cursor(24, 79, state)
2310    push('D', vt)
2311    expect('moverect 1..25,0..80 -> 0..24,0..80\nputglyph 44 1 24,0')
2312
2313    -- 80th column phantom linefeed phantom cancelled by explicit cursor move
2314    push('\x1b[25;78HABC', vt)
2315    expect('putglyph 41 1 24,77\nputglyph 42 1 24,78\nputglyph 43 1 24,79')
2316    cursor(24, 79, state)
2317    push('\x1b[25;1HD', vt)
2318    expect('putglyph 44 1 24,0')
2319  end)
2320
2321  itp('21state_tabstops', function()
2322    local vt = init()
2323    local state = wantstate(vt, { g = true })
2324
2325    -- Initial
2326    reset(state, nil)
2327    push('\tX', vt)
2328    expect('putglyph 58 1 0,8')
2329    push('\tX', vt)
2330    expect('putglyph 58 1 0,16')
2331    cursor(0, 17, state)
2332
2333    -- HTS
2334    push('\x1b[5G\x1bH', vt)
2335    push('\x1b[G\tX', vt)
2336    expect('putglyph 58 1 0,4')
2337    cursor(0, 5, state)
2338
2339    -- TBC 0
2340    push('\x1b[9G\x1b[g', vt)
2341    push('\x1b[G\tX\tX', vt)
2342    expect('putglyph 58 1 0,4\nputglyph 58 1 0,16')
2343    cursor(0, 17, state)
2344
2345    -- TBC 3
2346    push('\x1b[3g\x1b[50G\x1bH\x1b[G', vt)
2347    cursor(0, 0, state)
2348    push('\tX', vt)
2349    expect('putglyph 58 1 0,49')
2350    cursor(0, 50, state)
2351
2352    -- Tabstops after resize
2353    reset(state, nil)
2354    resize(30, 100, vt)
2355    -- Should be 100/8 = 12 tabstops
2356    push('\tX', vt)
2357    expect('putglyph 58 1 0,8')
2358    push('\tX', vt)
2359    expect('putglyph 58 1 0,16')
2360    push('\tX', vt)
2361    expect('putglyph 58 1 0,24')
2362    push('\tX', vt)
2363    expect('putglyph 58 1 0,32')
2364    push('\tX', vt)
2365    expect('putglyph 58 1 0,40')
2366    push('\tX', vt)
2367    expect('putglyph 58 1 0,48')
2368    push('\tX', vt)
2369    expect('putglyph 58 1 0,56')
2370    push('\tX', vt)
2371    expect('putglyph 58 1 0,64')
2372    push('\tX', vt)
2373    expect('putglyph 58 1 0,72')
2374    push('\tX', vt)
2375    expect('putglyph 58 1 0,80')
2376    push('\tX', vt)
2377    expect('putglyph 58 1 0,88')
2378    push('\tX', vt)
2379    expect('putglyph 58 1 0,96')
2380    cursor(0, 97, state)
2381  end)
2382
2383  itp('22state_save', function()
2384    local vt = init()
2385    local state = wantstate(vt, { p = true })
2386
2387    reset(state, nil)
2388    expect('settermprop 1 true\nsettermprop 2 true\nsettermprop 7 1')
2389
2390    -- Set up state
2391    push('\x1b[2;2H', vt)
2392    cursor(1, 1, state)
2393    push('\x1b[1m', vt)
2394    pen('bold', true, state)
2395
2396    -- Save
2397    push('\x1b[?1048h', vt)
2398
2399    -- Change state
2400    push('\x1b[5;5H', vt)
2401    cursor(4, 4, state)
2402    push('\x1b[4 q', vt)
2403    expect('settermprop 2 false\nsettermprop 7 2')
2404    push('\x1b[22;4m', vt)
2405    pen('bold', false, state)
2406    pen('underline', 1, state)
2407
2408    -- Restore
2409    push('\x1b[?1048l', vt)
2410    expect('settermprop 1 true\nsettermprop 2 true\nsettermprop 7 1')
2411    cursor(1, 1, state)
2412    pen('bold', true, state)
2413    pen('underline', 0, state)
2414
2415    -- Save/restore using DECSC/DECRC
2416    push('\x1b[2;2H\x1b7', vt)
2417    cursor(1, 1, state)
2418
2419    push('\x1b[5;5H', vt)
2420    cursor(4, 4, state)
2421    push('\x1b8', vt)
2422    expect('settermprop 1 true\nsettermprop 2 true\nsettermprop 7 1')
2423    cursor(1, 1, state)
2424
2425    -- Save twice, restore twice happens on both edge transitions
2426    push('\x1b[2;10H\x1b[?1048h\x1b[6;10H\x1b[?1048h', vt)
2427    push('\x1b[H', vt)
2428    cursor(0, 0, state)
2429    push('\x1b[?1048l', vt)
2430    expect('settermprop 1 true\nsettermprop 2 true\nsettermprop 7 1')
2431    cursor(5, 9, state)
2432    push('\x1b[H', vt)
2433    cursor(0, 0, state)
2434    push('\x1b[?1048l', vt)
2435    expect('settermprop 1 true\nsettermprop 2 true\nsettermprop 7 1')
2436    cursor(5, 9, state)
2437  end)
2438
2439  itp('25state_input', function()
2440    local vt = init()
2441    local state = wantstate(vt)
2442
2443    -- Disambiguate escape codes enabled
2444    push('\x1b[>1u', vt)
2445
2446    -- Unmodified ASCII
2447    inchar(0x41, vt)
2448    expect_output('A')
2449    inchar(0x61, vt)
2450    expect_output('a')
2451
2452    -- Ctrl modifier on ASCII letters
2453    inchar(0x41, vt, { C = true })
2454    expect_output('\x1b[97;6u')
2455    inchar(0x61, vt, { C = true })
2456    expect_output('\x1b[97;5u')
2457
2458    -- Alt modifier on ASCII letters
2459    inchar(0x41, vt, { A = true })
2460    expect_output('\x1b[97;4u')
2461    inchar(0x61, vt, { A = true })
2462    expect_output('\x1b[97;3u')
2463
2464    -- Ctrl-Alt modifier on ASCII letters
2465    inchar(0x41, vt, { C = true, A = true })
2466    expect_output('\x1b[97;8u')
2467    inchar(0x61, vt, { C = true, A = true })
2468    expect_output('\x1b[97;7u')
2469
2470    -- Ctrl-I is disambiguated
2471    inchar(0x49, vt)
2472    expect_output('I')
2473    inchar(0x69, vt)
2474    expect_output('i')
2475    inchar(0x49, vt, { C = true })
2476    expect_output('\x1b[105;6u')
2477    inchar(0x69, vt, { C = true })
2478    expect_output('\x1b[105;5u')
2479    inchar(0x49, vt, { A = true })
2480    expect_output('\x1b[105;4u')
2481    inchar(0x69, vt, { A = true })
2482    expect_output('\x1b[105;3u')
2483    inchar(0x49, vt, { A = true, C = true })
2484    expect_output('\x1b[105;8u')
2485    inchar(0x69, vt, { A = true, C = true })
2486    expect_output('\x1b[105;7u')
2487
2488    -- Ctrl+Digits
2489    for i = 0, 9 do
2490      local c = 0x30 + i
2491      inchar(c, vt)
2492      expect_output(tostring(i))
2493      inchar(c, vt, { C = true })
2494      expect_output(string.format('\x1b[%d;5u', c))
2495      inchar(c, vt, { C = true, S = true })
2496      expect_output(string.format('\x1b[%d;6u', c))
2497      inchar(c, vt, { C = true, A = true })
2498      expect_output(string.format('\x1b[%d;7u', c))
2499      inchar(c, vt, { C = true, A = true, S = true })
2500      expect_output(string.format('\x1b[%d;8u', c))
2501    end
2502
2503    -- Special handling of Space
2504    inchar(0x20, vt)
2505    expect_output(' ')
2506    inchar(0x20, vt, { S = true })
2507    expect_output('\x1b[32;2u')
2508    inchar(0x20, vt, { C = true })
2509    expect_output('\x1b[32;5u')
2510    inchar(0x20, vt, { C = true, S = true })
2511    expect_output('\x1b[32;6u')
2512    inchar(0x20, vt, { A = true })
2513    expect_output('\x1b[32;3u')
2514    inchar(0x20, vt, { S = true, A = true })
2515    expect_output('\x1b[32;4u')
2516    inchar(0x20, vt, { C = true, A = true })
2517    expect_output('\x1b[32;7u')
2518    inchar(0x20, vt, { S = true, C = true, A = true })
2519    expect_output('\x1b[32;8u')
2520
2521    -- Cursor keys in reset (cursor) mode
2522    inkey('up', vt)
2523    expect_output('\x1b[A')
2524    inkey('up', vt, { S = true })
2525    expect_output('\x1b[1;2A')
2526    inkey('up', vt, { C = true })
2527    expect_output('\x1b[1;5A')
2528    inkey('up', vt, { S = true, C = true })
2529    expect_output('\x1b[1;6A')
2530    inkey('up', vt, { A = true })
2531    expect_output('\x1b[1;3A')
2532    inkey('up', vt, { S = true, A = true })
2533    expect_output('\x1b[1;4A')
2534    inkey('up', vt, { C = true, A = true })
2535    expect_output('\x1b[1;7A')
2536    inkey('up', vt, { S = true, C = true, A = true })
2537    expect_output('\x1b[1;8A')
2538
2539    -- Cursor keys in application mode
2540    push('\x1b[?1h', vt)
2541    -- Plain "Up" should be SS3 A now
2542    inkey('up', vt)
2543    expect_output('\x1bOA')
2544    -- Modified keys should still use CSI
2545    inkey('up', vt, { S = true })
2546    expect_output('\x1b[1;2A')
2547    inkey('up', vt, { C = true })
2548    expect_output('\x1b[1;5A')
2549
2550    -- Tab
2551    inkey('tab', vt)
2552    expect_output('\x09')
2553    inkey('tab', vt, { S = true })
2554    expect_output('\x1b[9;2u')
2555    inkey('tab', vt, { C = true })
2556    expect_output('\x1b[9;5u')
2557    inkey('tab', vt, { A = true })
2558    expect_output('\x1b[9;3u')
2559    inkey('tab', vt, { C = true, A = true })
2560    expect_output('\x1b[9;7u')
2561
2562    -- Backspace
2563    inkey('bs', vt)
2564    expect_output('\x7f')
2565    inkey('bs', vt, { S = true })
2566    expect_output('\x1b[127;2u')
2567    inkey('bs', vt, { C = true })
2568    expect_output('\x1b[127;5u')
2569    inkey('bs', vt, { A = true })
2570    expect_output('\x1b[127;3u')
2571    inkey('bs', vt, { C = true, A = true })
2572    expect_output('\x1b[127;7u')
2573
2574    -- DEL
2575    inkey('del', vt)
2576    expect_output('\x1b[3~')
2577    inkey('del', vt, { S = true })
2578    expect_output('\x1b[3;2~')
2579    inkey('del', vt, { C = true })
2580    expect_output('\x1b[3;5~')
2581    inkey('del', vt, { A = true })
2582    expect_output('\x1b[3;3~')
2583    inkey('del', vt, { C = true, A = true })
2584    expect_output('\x1b[3;7~')
2585
2586    -- ESC
2587    inkey('esc', vt)
2588    expect_output('\x1b[27;1u')
2589    inkey('esc', vt, { S = true })
2590    expect_output('\x1b[27;2u')
2591    inkey('esc', vt, { C = true })
2592    expect_output('\x1b[27;5u')
2593    inkey('esc', vt, { A = true })
2594    expect_output('\x1b[27;3u')
2595    inkey('esc', vt, { C = true, A = true })
2596    expect_output('\x1b[27;7u')
2597
2598    -- Enter in linefeed mode
2599    inkey('enter', vt)
2600    expect_output('\x0d')
2601    inkey('enter', vt, { S = true })
2602    expect_output('\x1b[13;2u')
2603    inkey('enter', vt, { C = true })
2604    expect_output('\x1b[13;5u')
2605    inkey('enter', vt, { A = true })
2606    expect_output('\x1b[13;3u')
2607    inkey('enter', vt, { C = true, A = true })
2608    expect_output('\x1b[13;7u')
2609
2610    -- Enter in newline mode
2611    push('\x1b[20h', vt)
2612    inkey('enter', vt)
2613    expect_output('\x0d\x0a')
2614
2615    -- Unmodified F1 is SS3 P
2616    inkey('f1', vt)
2617    expect_output('\x1bOP')
2618
2619    -- Modified F1 is CSI P
2620    inkey('f1', vt, { S = true })
2621    expect_output('\x1b[1;2P')
2622    inkey('f1', vt, { A = true })
2623    expect_output('\x1b[1;3P')
2624    inkey('f1', vt, { C = true })
2625    expect_output('\x1b[1;5P')
2626
2627    -- Keypad in DECKPNM
2628    inkey('kp0', vt)
2629    expect_output('\x1b[57399;1u')
2630
2631    -- Keypad in DECKPAM
2632    push('\x1b=', vt)
2633    inkey('kp0', vt)
2634    expect_output('\x1bOp')
2635
2636    -- Bracketed paste mode off
2637    vterm.vterm_keyboard_start_paste(vt)
2638    vterm.vterm_keyboard_end_paste(vt)
2639
2640    -- Bracketed paste mode on
2641    push('\x1b[?2004h', vt)
2642    vterm.vterm_keyboard_start_paste(vt)
2643    expect_output('\x1b[200~')
2644    vterm.vterm_keyboard_end_paste(vt)
2645    expect_output('\x1b[201~')
2646
2647    -- Focus reporting disabled
2648    vterm.vterm_state_focus_in(state)
2649    vterm.vterm_state_focus_out(state)
2650
2651    -- Focus reporting enabled
2652    state = wantstate(vt, { p = true })
2653    push('\x1b[?1004h', vt)
2654    expect('settermprop 9 true')
2655    vterm.vterm_state_focus_in(state)
2656    expect_output('\x1b[I')
2657    vterm.vterm_state_focus_out(state)
2658    expect_output('\x1b[O')
2659
2660    -- Synchronized output mode 2026
2661    push('\x1b[?2026h', vt)
2662    expect('settermprop 11 true')
2663    push('\x1b[?2026l', vt)
2664    expect('settermprop 11 false')
2665
2666    -- DECRQM on synchronized output
2667    push('\x1b[?2026h', vt)
2668    expect('settermprop 11 true')
2669    push('\x1b[?2026$p', vt)
2670    expect_output('\x1b[?2026;1$y')
2671    push('\x1b[?2026l', vt)
2672    expect('settermprop 11 false')
2673    push('\x1b[?2026$p', vt)
2674    expect_output('\x1b[?2026;2$y')
2675
2676    -- Disambiguate escape codes disabled
2677    push('\x1b[<u', vt)
2678
2679    -- Unmodified ASCII
2680    inchar(0x41, vt)
2681    expect_output('A')
2682    inchar(0x61, vt)
2683    expect_output('a')
2684
2685    -- Ctrl modifier on ASCII letters
2686    inchar(0x41, vt, { C = true })
2687    expect_output('\x01')
2688    inchar(0x61, vt, { C = true })
2689    expect_output('\x01')
2690
2691    -- Alt modifier on ASCII letters
2692    inchar(0x41, vt, { A = true })
2693    expect_output('\x1bA')
2694    inchar(0x61, vt, { A = true })
2695    expect_output('\x1ba')
2696
2697    -- Ctrl-Alt modifier on ASCII letters
2698    inchar(0x41, vt, { C = true, A = true })
2699    expect_output('\x1b\x01')
2700    inchar(0x61, vt, { C = true, A = true })
2701    expect_output('\x1b\x01')
2702
2703    -- Ctrl-I is ambiguous
2704    inchar(0x49, vt)
2705    expect_output('I')
2706    inchar(0x69, vt)
2707    expect_output('i')
2708    inchar(0x49, vt, { C = true })
2709    expect_output('\x09')
2710    inchar(0x69, vt, { C = true })
2711    expect_output('\x09')
2712    inchar(0x49, vt, { A = true })
2713    expect_output('\x1bI')
2714    inchar(0x69, vt, { A = true })
2715    expect_output('\x1bi')
2716    inchar(0x49, vt, { A = true, C = true })
2717    expect_output('\x1b\x09')
2718    inchar(0x69, vt, { A = true, C = true })
2719    expect_output('\x1b\x09')
2720
2721    -- Ctrl+Digits
2722    inchar(0x30, vt, { C = true })
2723    expect_output('0')
2724    inchar(0x31, vt, { C = true })
2725    expect_output('1')
2726    inchar(0x32, vt, { C = true })
2727    expect_output('\x00')
2728    inchar(0x33, vt, { C = true })
2729    expect_output('\x1b')
2730    inchar(0x34, vt, { C = true })
2731    expect_output('\x1c')
2732    inchar(0x35, vt, { C = true })
2733    expect_output('\x1d')
2734    inchar(0x36, vt, { C = true })
2735    expect_output('\x1e')
2736    inchar(0x37, vt, { C = true })
2737    expect_output('\x1f')
2738    inchar(0x38, vt, { C = true })
2739    expect_output('\x7f')
2740    inchar(0x39, vt, { C = true })
2741    expect_output('9')
2742
2743    -- Ctrl+/
2744    inchar(0x2F, vt, { C = true })
2745    expect_output('\x1f')
2746  end)
2747
2748  itp('26state_query', function()
2749    local vt = init()
2750    local state = wantstate(vt)
2751
2752    -- DA
2753    reset(state, nil)
2754    push('\x1b[c', vt)
2755    expect_output('\x1b[?61;22;52c')
2756
2757    -- XTVERSION
2758    reset(state, nil)
2759    push('\x1b[>q', vt)
2760    expect_output('\x1bP>|libvterm(0.3)\x1b\\')
2761
2762    -- DSR
2763    reset(state, nil)
2764    push('\x1b[5n', vt)
2765    expect_output('\x1b[0n')
2766
2767    -- CPR
2768    push('\x1b[6n', vt)
2769    expect_output('\x1b[1;1R')
2770    push('\x1b[10;10H\x1b[6n', vt)
2771    expect_output('\x1b[10;10R')
2772
2773    -- DECCPR
2774    push('\x1b[?6n', vt)
2775    expect_output('\x1b[?10;10R')
2776
2777    -- DECRQSS on DECSCUSR
2778    push('\x1b[3 q', vt)
2779    push('\x1bP$q q\x1b\\', vt)
2780    expect_output('\x1bP1$r3 q\x1b\\')
2781
2782    -- DECRQSS on SGR
2783    push('\x1b[1;5;7m', vt)
2784    push('\x1bP$qm\x1b\\', vt)
2785    expect_output('\x1bP1$r1;5;7m\x1b\\')
2786
2787    -- DECRQSS on SGR ANSI colours
2788    push('\x1b[0;31;42m', vt)
2789    push('\x1bP$qm\x1b\\', vt)
2790    expect_output('\x1bP1$r31;42m\x1b\\')
2791
2792    -- DECRQSS on SGR ANSI hi-bright colours
2793    push('\x1b[0;93;104m', vt)
2794    push('\x1bP$qm\x1b\\', vt)
2795    expect_output('\x1bP1$r93;104m\x1b\\')
2796
2797    -- DECRQSS on SGR 256-palette colours
2798    push('\x1b[0;38:5:56;48:5:78m', vt)
2799    push('\x1bP$qm\x1b\\', vt)
2800    expect_output('\x1bP1$r38:5:56;48:5:78m\x1b\\')
2801
2802    -- DECRQSS on SGR RGB8 colours
2803    push('\x1b[0;38:2:24:68:112;48:2:13:57:101m', vt)
2804    push('\x1bP$qm\x1b\\', vt)
2805    expect_output('\x1bP1$r38:2:24:68:112;48:2:13:57:101m\x1b\\')
2806
2807    -- S8C1T on DSR
2808    push('\x1b G', vt)
2809    push('\x1b[5n', vt)
2810    expect_output('\x9b0n')
2811    push('\x1b F', vt)
2812  end)
2813
2814  itp('27state_reset', function()
2815    local vt = init()
2816    local state = wantstate(vt)
2817
2818    reset(state, nil)
2819
2820    -- RIS homes cursor
2821    push('\x1b[5;5H', vt)
2822    cursor(4, 4, state)
2823    state = wantstate(vt, { m = true })
2824    push('\x1bc', vt)
2825    cursor(0, 0, state)
2826    wantstate(vt)
2827
2828    -- RIS cancels scrolling region
2829    push('\x1b[5;10r', vt)
2830    wantstate(vt, { s = true })
2831    push('\x1bc\x1b[25H\n', vt)
2832    expect('scrollrect 0..25,0..80 => +1,+0')
2833    wantstate(vt)
2834
2835    -- RIS erases screen
2836    push('ABCDE', vt)
2837    state = wantstate(vt, { e = true })
2838    push('\x1bc', vt)
2839    expect('erase 0..25,0..80')
2840    wantstate(vt)
2841
2842    -- RIS clears tabstops
2843    push('\x1b[5G\x1bH\x1b[G\t', vt)
2844    cursor(0, 4, state)
2845    push('\x1bc\t', vt)
2846    cursor(0, 8, state)
2847  end)
2848
2849  itp('28state_dbl_wh', function()
2850    local vt = init()
2851    local state = wantstate(vt, { g = true })
2852
2853    -- Single Width, Single Height
2854    reset(state, nil)
2855    push('\x1b#5', vt)
2856    push('Hello', vt)
2857    expect(
2858      'putglyph 48 1 0,0\nputglyph 65 1 0,1\nputglyph 6c 1 0,2\nputglyph 6c 1 0,3\nputglyph 6f 1 0,4'
2859    )
2860
2861    -- Double Width, Single Height
2862    reset(state, nil)
2863    push('\x1b#6', vt)
2864    push('Hello', vt)
2865    expect(
2866      'putglyph 48 1 0,0 dwl\nputglyph 65 1 0,1 dwl\nputglyph 6c 1 0,2 dwl\nputglyph 6c 1 0,3 dwl\nputglyph 6f 1 0,4 dwl'
2867    )
2868    cursor(0, 5, state)
2869    push('\x1b[40GAB', vt)
2870    expect('putglyph 41 1 0,39 dwl\nputglyph 42 1 1,0')
2871    cursor(1, 1, state)
2872
2873    -- Double Height
2874    reset(state, nil)
2875    push('\x1b#3', vt)
2876    push('Hello', vt)
2877    expect(
2878      'putglyph 48 1 0,0 dwl dhl-top\nputglyph 65 1 0,1 dwl dhl-top\nputglyph 6c 1 0,2 dwl dhl-top\nputglyph 6c 1 0,3 dwl dhl-top\nputglyph 6f 1 0,4 dwl dhl-top'
2879    )
2880    cursor(0, 5, state)
2881    push('\r\n\x1b#4', vt)
2882    push('Hello', vt)
2883    expect(
2884      'putglyph 48 1 1,0 dwl dhl-bottom\nputglyph 65 1 1,1 dwl dhl-bottom\nputglyph 6c 1 1,2 dwl dhl-bottom\nputglyph 6c 1 1,3 dwl dhl-bottom\nputglyph 6f 1 1,4 dwl dhl-bottom'
2885    )
2886    cursor(1, 5, state)
2887
2888    -- Double Width scrolling
2889    reset(state, nil)
2890    push('\x1b[20H\x1b#6ABC', vt)
2891    expect('putglyph 41 1 19,0 dwl\nputglyph 42 1 19,1 dwl\nputglyph 43 1 19,2 dwl')
2892    push('\x1b[25H\n', vt)
2893    push('\x1b[19;4HDE', vt)
2894    expect('putglyph 44 1 18,3 dwl\nputglyph 45 1 18,4 dwl')
2895    push('\x1b[H\x1bM', vt)
2896    push('\x1b[20;6HFG', vt)
2897    expect('putglyph 46 1 19,5 dwl\nputglyph 47 1 19,6 dwl')
2898  end)
2899
2900  itp('29state_fallback', function()
2901    local vt = init()
2902    local state = wantstate(vt, { f = true })
2903    reset(state, nil)
2904
2905    -- Unrecognised control
2906    push('\x03', vt)
2907    expect('control 03')
2908
2909    -- Unrecognised CSI
2910    push('\x1b[?15;2z', vt)
2911    expect('csi 7a L=3f 15,2')
2912
2913    -- Unrecognised OSC
2914    push('\x1b]27;Something\x1b\\', vt)
2915    expect('osc [27;Something]')
2916
2917    -- Unrecognised DCS
2918    push('\x1bPz123\x1b\\', vt)
2919    expect('dcs [z123]')
2920
2921    -- Unrecognised APC
2922    push('\x1b_z123\x1b\\', vt)
2923    expect('apc [z123]')
2924
2925    -- Unrecognised PM
2926    push('\x1b^z123\x1b\\', vt)
2927    expect('pm [z123]')
2928
2929    -- Unrecognised SOS
2930    push('\x1bXz123\x1b\\', vt)
2931    expect('sos [z123]')
2932  end)
2933
2934  itp('30state_pen', function()
2935    local vt = init()
2936    local state = wantstate(vt)
2937
2938    -- Reset
2939    push('\x1b[m', vt)
2940    pen('bold', false, state)
2941    pen('underline', 0, state)
2942    pen('italic', false, state)
2943    pen('blink', false, state)
2944    pen('reverse', false, state)
2945    pen('font', 0, state)
2946    -- TODO(dundargoc): fix
2947    -- ?pen foreground = rgb(240,240,240,is_default_fg)
2948    -- ?pen background = rgb(0,0,0,is_default_bg)
2949
2950    -- Bold
2951    push('\x1b[1m', vt)
2952    pen('bold', true, state)
2953    push('\x1b[22m', vt)
2954    pen('bold', false, state)
2955    push('\x1b[1m\x1b[m', vt)
2956    pen('bold', false, state)
2957
2958    -- Underline
2959    push('\x1b[4m', vt)
2960    pen('underline', 1, state)
2961    push('\x1b[21m', vt)
2962    pen('underline', 2, state)
2963    push('\x1b[24m', vt)
2964    pen('underline', 0, state)
2965    push('\x1b[4m\x1b[4:0m', vt)
2966    pen('underline', 0, state)
2967    push('\x1b[4:1m', vt)
2968    pen('underline', 1, state)
2969    push('\x1b[4:2m', vt)
2970    pen('underline', 2, state)
2971    push('\x1b[4:3m', vt)
2972    pen('underline', 3, state)
2973    push('\x1b[4m\x1b[m', vt)
2974    pen('underline', 0, state)
2975
2976    -- Italic
2977    push('\x1b[3m', vt)
2978    pen('italic', true, state)
2979    push('\x1b[23m', vt)
2980    pen('italic', false, state)
2981    push('\x1b[3m\x1b[m', vt)
2982    pen('italic', false, state)
2983
2984    -- Blink
2985    push('\x1b[5m', vt)
2986    pen('blink', true, state)
2987    push('\x1b[25m', vt)
2988    pen('blink', false, state)
2989    push('\x1b[5m\x1b[m', vt)
2990    pen('blink', false, state)
2991
2992    -- Reverse
2993    push('\x1b[7m', vt)
2994    pen('reverse', true, state)
2995    push('\x1b[27m', vt)
2996    pen('reverse', false, state)
2997    push('\x1b[7m\x1b[m', vt)
2998    pen('reverse', false, state)
2999
3000    -- Font Selection
3001    push('\x1b[11m', vt)
3002    pen('font', 1, state)
3003    push('\x1b[19m', vt)
3004    pen('font', 9, state)
3005    push('\x1b[10m', vt)
3006    pen('font', 0, state)
3007    push('\x1b[11m\x1b[m', vt)
3008    pen('font', 0, state)
3009
3010    -- Dim
3011    push('\x1b[2m', vt)
3012    pen('dim', true, state)
3013    push('\x1b[22m', vt)
3014    pen('dim', false, state)
3015    push('\x1b[2m\x1b[m', vt)
3016    pen('dim', false, state)
3017
3018    -- Bold and Dim interaction (SGR 22 turns off both)
3019    push('\x1b[1;2m', vt)
3020    pen('bold', true, state)
3021    pen('dim', true, state)
3022    push('\x1b[22m', vt)
3023    pen('bold', false, state)
3024    pen('dim', false, state)
3025
3026    -- Overline
3027    push('\x1b[53m', vt)
3028    pen('overline', true, state)
3029    push('\x1b[55m', vt)
3030    pen('overline', false, state)
3031    push('\x1b[53m\x1b[m', vt)
3032    pen('overline', false, state)
3033
3034    -- TODO(dundargoc): fix
3035    -- Foreground
3036    -- push "\x1b[31m"
3037    --   ?pen foreground = idx(1)
3038    -- push "\x1b[32m"
3039    --   ?pen foreground = idx(2)
3040    -- push "\x1b[34m"
3041    --   ?pen foreground = idx(4)
3042    -- push "\x1b[91m"
3043    --   ?pen foreground = idx(9)
3044    -- push "\x1b[38:2:10:20:30m"
3045    --   ?pen foreground = rgb(10,20,30)
3046    -- push "\x1b[38:5:1m"
3047    --   ?pen foreground = idx(1)
3048    -- push "\x1b[39m"
3049    --   ?pen foreground = rgb(240,240,240,is_default_fg)
3050    --
3051    -- Background
3052    -- push "\x1b[41m"
3053    --   ?pen background = idx(1)
3054    -- push "\x1b[42m"
3055    --   ?pen background = idx(2)
3056    -- push "\x1b[44m"
3057    --   ?pen background = idx(4)
3058    -- push "\x1b[101m"
3059    --   ?pen background = idx(9)
3060    -- push "\x1b[48:2:10:20:30m"
3061    --   ?pen background = rgb(10,20,30)
3062    -- push "\x1b[48:5:1m"
3063    --   ?pen background = idx(1)
3064    -- push "\x1b[49m"
3065    --   ?pen background = rgb(0,0,0,is_default_bg)
3066    --
3067    -- Bold+ANSI colour == highbright
3068    -- push "\x1b[m\x1b[1;37m"
3069    --   ?pen bold = on
3070    --   ?pen foreground = idx(15)
3071    -- push "\x1b[m\x1b[37;1m"
3072    --   ?pen bold = on
3073    --   ?pen foreground = idx(15)
3074    --
3075    -- Super/Subscript
3076    -- push "\x1b[73m"
3077    --   ?pen small = on
3078    --   ?pen baseline = raise
3079    -- push "\x1b[74m"
3080    --   ?pen small = on
3081    --   ?pen baseline = lower
3082    -- push "\x1b[75m"
3083    --   ?pen small = off
3084    --   ?pen baseline = normal
3085    --
3086    -- DECSTR resets pen attributes
3087    -- push "\x1b[1;4m"
3088    --   ?pen bold = on
3089    --   ?pen underline = 1
3090    -- push "\x1b[!p"
3091    --   ?pen bold = off
3092    --   ?pen underline = 0
3093  end)
3094
3095  itp('31state_rep', function()
3096    local vt = init()
3097    local state = wantstate(vt, { g = true })
3098
3099    -- REP no argument
3100    reset(state, nil)
3101    push('a\x1b[b', vt)
3102    expect('putglyph 61 1 0,0\nputglyph 61 1 0,1')
3103
3104    -- REP zero (zero should be interpreted as one)
3105    reset(state, nil)
3106    push('a\x1b[0b', vt)
3107    expect('putglyph 61 1 0,0\nputglyph 61 1 0,1')
3108
3109    -- REP lowercase a times two
3110    reset(state, nil)
3111    push('a\x1b[2b', vt)
3112    expect('putglyph 61 1 0,0\nputglyph 61 1 0,1\nputglyph 61 1 0,2')
3113
3114    -- REP with UTF-8 1 char
3115    -- U+00E9 = C3 A9  name: LATIN SMALL LETTER E WITH ACUTE
3116    reset(state, nil)
3117    push('\xC3\xA9\x1b[b', vt)
3118    expect('putglyph e9 1 0,0\nputglyph e9 1 0,1')
3119
3120    -- REP with UTF-8 wide char
3121    -- U+00E9 = C3 A9  name: LATIN SMALL LETTER E WITH ACUTE
3122    reset(state, nil)
3123    push('\xEF\xBC\x90\x1b[b', vt)
3124    expect('putglyph ff10 2 0,0\nputglyph ff10 2 0,2')
3125
3126    -- REP with UTF-8 combining character
3127    reset(state, nil)
3128    push('e\xCC\x81\x1b[b', vt)
3129    expect('putglyph 65,301 1 0,0\nputglyph 65,301 1 0,1')
3130
3131    -- REP till end of line
3132    reset(state, nil)
3133    push('a\x1b[1000bb', vt)
3134    expect(
3135      'putglyph 61 1 0,0\nputglyph 61 1 0,1\nputglyph 61 1 0,2\nputglyph 61 1 0,3\nputglyph 61 1 0,4\nputglyph 61 1 0,5\nputglyph 61 1 0,6\nputglyph 61 1 0,7\nputglyph 61 1 0,8\nputglyph 61 1 0,9\nputglyph 61 1 0,10\nputglyph 61 1 0,11\nputglyph 61 1 0,12\nputglyph 61 1 0,13\nputglyph 61 1 0,14\nputglyph 61 1 0,15\nputglyph 61 1 0,16\nputglyph 61 1 0,17\nputglyph 61 1 0,18\nputglyph 61 1 0,19\nputglyph 61 1 0,20\nputglyph 61 1 0,21\nputglyph 61 1 0,22\nputglyph 61 1 0,23\nputglyph 61 1 0,24\nputglyph 61 1 0,25\nputglyph 61 1 0,26\nputglyph 61 1 0,27\nputglyph 61 1 0,28\nputglyph 61 1 0,29\nputglyph 61 1 0,30\nputglyph 61 1 0,31\nputglyph 61 1 0,32\nputglyph 61 1 0,33\nputglyph 61 1 0,34\nputglyph 61 1 0,35\nputglyph 61 1 0,36\nputglyph 61 1 0,37\nputglyph 61 1 0,38\nputglyph 61 1 0,39\nputglyph 61 1 0,40\nputglyph 61 1 0,41\nputglyph 61 1 0,42\nputglyph 61 1 0,43\nputglyph 61 1 0,44\nputglyph 61 1 0,45\nputglyph 61 1 0,46\nputglyph 61 1 0,47\nputglyph 61 1 0,48\nputglyph 61 1 0,49\nputglyph 61 1 0,50\nputglyph 61 1 0,51\nputglyph 61 1 0,52\nputglyph 61 1 0,53\nputglyph 61 1 0,54\nputglyph 61 1 0,55\nputglyph 61 1 0,56\nputglyph 61 1 0,57\nputglyph 61 1 0,58\nputglyph 61 1 0,59\nputglyph 61 1 0,60\nputglyph 61 1 0,61\nputglyph 61 1 0,62\nputglyph 61 1 0,63\nputglyph 61 1 0,64\nputglyph 61 1 0,65\nputglyph 61 1 0,66\nputglyph 61 1 0,67\nputglyph 61 1 0,68\nputglyph 61 1 0,69\nputglyph 61 1 0,70\nputglyph 61 1 0,71\nputglyph 61 1 0,72\nputglyph 61 1 0,73\nputglyph 61 1 0,74\nputglyph 61 1 0,75\nputglyph 61 1 0,76\nputglyph 61 1 0,77\nputglyph 61 1 0,78\nputglyph 61 1 0,79\nputglyph 62 1 1,0'
3136    )
3137  end)
3138
3139  itp('32state_flow', function()
3140    local vt = init()
3141    local state = wantstate(vt)
3142
3143    -- Many of these test cases inspired by
3144    -- https://blueprints.launchpad.net/libvterm/+spec/reflow-cases
3145
3146    -- Spillover text marks continuation on second line
3147    reset(state, nil)
3148    push(string.rep('A', 100), vt)
3149    push('\r\n', vt)
3150    lineinfo(0, {}, state)
3151    lineinfo(1, { cont = true }, state)
3152
3153    -- CRLF in column 80 does not mark continuation
3154    reset(state, nil)
3155    push(string.rep('B', 80), vt)
3156    push('\r\n', vt)
3157    push(string.rep('B', 20), vt)
3158    push('\r\n', vt)
3159    lineinfo(0, {}, state)
3160    lineinfo(1, {}, state)
3161
3162    -- EL cancels continuation of following line
3163    reset(state, nil)
3164    push(string.rep('D', 100), vt)
3165    lineinfo(1, { cont = true }, state)
3166    push('\x1bM\x1b[79G\x1b[K', vt)
3167    lineinfo(1, {}, state)
3168  end)
3169
3170  itp('40state_selection', function()
3171    local vt = init()
3172    wantstate(vt)
3173
3174    -- Set clipboard; final chunk len 4
3175    push('\x1b]52;c;SGVsbG8s\x1b\\', vt)
3176    expect('selection-set mask=0001 [Hello,]')
3177
3178    -- Set clipboard; final chunk len 3
3179    push('\x1b]52;c;SGVsbG8sIHc=\x1b\\', vt)
3180    expect('selection-set mask=0001 [Hello, w]')
3181
3182    -- Set clipboard; final chunk len 2
3183    push('\x1b]52;c;SGVsbG8sIHdvcmxkCg==\x1b\\', vt)
3184    expect('selection-set mask=0001 [Hello, world\n]')
3185
3186    -- Set clipboard; split between chunks
3187    push('\x1b]52;c;SGVs', vt)
3188    expect('selection-set mask=0001 [Hel')
3189    push('bG8s\x1b\\', vt)
3190    expect('selection-set mask=0001 lo,]')
3191
3192    -- Set clipboard; split within chunk
3193    push('\x1b]52;c;SGVsbG', vt)
3194    expect('selection-set mask=0001 [Hel')
3195    push('8s\x1b\\', vt)
3196    expect('selection-set mask=0001 lo,]')
3197
3198    -- Set clipboard; empty first chunk
3199    push('\x1b]52;c;', vt)
3200    push('SGVsbG8s\x1b\\', vt)
3201    expect('selection-set mask=0001 [Hello,]')
3202
3203    -- Set clipboard; empty final chunk
3204    push('\x1b]52;c;SGVsbG8s', vt)
3205    expect('selection-set mask=0001 [Hello,')
3206    push('\x1b\\', vt)
3207    expect('selection-set mask=0001 ]')
3208
3209    -- Set clipboard; longer than buffer
3210    push('\x1b]52;c;' .. string.rep('LS0t', 10) .. '\x1b\\', vt)
3211    expect('selection-set mask=0001 [---------------\nselection-set mask=0001 ---------------]')
3212
3213    -- Clear clipboard
3214    push('\x1b]52;c;\x1b\\', vt)
3215    expect('selection-set mask=0001 []')
3216
3217    -- Set invalid data clears and ignores
3218    push('\x1b]52;c;SGVs*SGVsbG8s\x1b\\', vt)
3219    expect('selection-set mask=0001 []')
3220
3221    -- Query clipboard
3222    push('\x1b]52;c;?\x1b\\', vt)
3223    expect('selection-query mask=0001')
3224
3225    -- TODO(dundargoc): fix
3226    -- Send clipboard; final chunk len 4
3227    -- SELECTION 1 ["Hello,"]
3228    --   output "\x1b]52;c;"
3229    --   output "SGVsbG8s"
3230    --   output "\x1b\\"
3231    --
3232    -- Send clipboard; final chunk len 3
3233    -- SELECTION 1 ["Hello, w"]
3234    --   output "\x1b]52;c;"
3235    --   output "SGVsbG8s"
3236    --   output "IHc=\x1b\\"
3237    --
3238    -- Send clipboard; final chunk len 2
3239    -- SELECTION 1 ["Hello, world\n"]
3240    --   output "\x1b]52;c;"
3241    --   output "SGVsbG8sIHdvcmxk"
3242    --   output "Cg==\x1b\\"
3243    --
3244    -- Send clipboard; split between chunks
3245    -- SELECTION 1 ["Hel"
3246    --   output "\x1b]52;c;"
3247    --   output "SGVs"
3248    -- SELECTION 1  "lo,"]
3249    --   output "bG8s"
3250    --   output "\x1b\\"
3251    --
3252    -- Send clipboard; split within chunk
3253    -- SELECTION 1 ["Hello"
3254    --   output "\x1b]52;c;"
3255    --   output "SGVs"
3256    -- SELECTION 1 ","]
3257    --   output "bG8s"
3258    --   output "\x1b\\"
3259  end)
3260
3261  itp('60screen_ascii', function()
3262    local vt = init()
3263    local screen = wantscreen(vt, { a = true, c = true })
3264
3265    -- Get
3266    reset(nil, screen)
3267    push('ABC', vt)
3268    expect('movecursor 0,3')
3269    screen_chars(0, 0, 1, 3, 'ABC', screen)
3270    screen_chars(0, 0, 1, 80, 'ABC', screen)
3271    screen_text(0, 0, 1, 3, '41,42,43', screen)
3272    screen_text(0, 0, 1, 80, '41,42,43', screen)
3273    screen_cell(0, 0, '{41} width=1 attrs={} fg=rgb(240,240,240) bg=rgb(0,0,0)', screen)
3274    screen_cell(0, 1, '{42} width=1 attrs={} fg=rgb(240,240,240) bg=rgb(0,0,0)', screen)
3275    screen_cell(0, 2, '{43} width=1 attrs={} fg=rgb(240,240,240) bg=rgb(0,0,0)', screen)
3276    screen_row(0, 'ABC', screen)
3277    screen_eol(0, 0, 0, screen)
3278    screen_eol(0, 2, 0, screen)
3279    screen_eol(0, 3, 1, screen)
3280    push('\x1b[H', vt)
3281    expect('movecursor 0,0')
3282    screen_row(0, 'ABC', screen)
3283    screen_text(0, 0, 1, 80, '41,42,43', screen)
3284    push('E', vt)
3285    expect('movecursor 0,1')
3286    screen_row(0, 'EBC', screen)
3287    screen_text(0, 0, 1, 80, '45,42,43', screen)
3288
3289    screen = wantscreen(vt, { a = true })
3290
3291    -- Erase
3292    reset(nil, screen)
3293    push('ABCDE\x1b[H\x1b[K', vt)
3294    -- TODO(dundargoc): fix
3295    -- screen_row(0, '', screen)
3296    screen_text(0, 0, 1, 80, '', screen)
3297
3298    -- Copycell
3299    reset(nil, screen)
3300    push('ABC\x1b[H\x1b[@', vt)
3301    push('1', vt)
3302    screen_row(0, '1ABC', screen)
3303
3304    reset(nil, screen)
3305    push('ABC\x1b[H\x1b[P', vt)
3306    screen_chars(0, 0, 1, 1, 'B', screen)
3307    screen_chars(0, 1, 1, 2, 'C', screen)
3308    screen_chars(0, 0, 1, 80, 'BC', screen)
3309
3310    -- Space padding
3311    reset(nil, screen)
3312    push('Hello\x1b[CWorld', vt)
3313    screen_row(0, 'Hello World', screen)
3314    screen_text(0, 0, 1, 80, '48,65,6c,6c,6f,20,57,6f,72,6c,64', screen)
3315
3316    -- Linefeed padding
3317    reset(nil, screen)
3318    push('Hello\r\nWorld', vt)
3319    screen_chars(0, 0, 2, 80, 'Hello\nWorld', screen)
3320    screen_text(0, 0, 2, 80, '48,65,6c,6c,6f,0a,57,6f,72,6c,64', screen)
3321
3322    -- Altscreen
3323    reset(nil, screen)
3324    push('P', vt)
3325    screen_row(0, 'P', screen)
3326    -- TODO(dundargoc): fix
3327    -- push('\x1b[?1049h', vt)
3328    -- screen_row(0, '', screen)
3329    -- push('\x1b[2K\x1b[HA', vt)
3330    -- screen_row(0, 'A', screen)
3331    -- push('\x1b[?1049l', vt)
3332    -- screen_row(0, 'P', screen)
3333  end)
3334
3335  itp('61screen_unicode', function()
3336    local vt = init()
3337    local screen = wantscreen(vt)
3338
3339    -- Single width UTF-8
3340    -- U+00C1 = C3 81  name: LATIN CAPITAL LETTER A WITH ACUTE
3341    -- U+00E9 = C3 A9  name: LATIN SMALL LETTER E WITH ACUTE
3342    reset(nil, screen)
3343    push('\xC3\x81\xC3\xA9', vt)
3344    screen_row(0, 'Áé', screen)
3345    screen_text(0, 0, 1, 80, 'c3,81,c3,a9', screen)
3346    screen_cell(0, 0, '{c1} width=1 attrs={} fg=rgb(240,240,240) bg=rgb(0,0,0)', screen)
3347
3348    -- Wide char
3349    -- U+FF10 = EF BC 90  name: FULLWIDTH DIGIT ZERO
3350    reset(nil, screen)
3351    push('0123\x1b[H', vt)
3352    push('\xEF\xBC\x90', vt)
3353    screen_row(0, '023', screen)
3354    screen_text(0, 0, 1, 80, 'ef,bc,90,32,33', screen)
3355    screen_cell(0, 0, '{ff10} width=2 attrs={} fg=rgb(240,240,240) bg=rgb(0,0,0)', screen)
3356
3357    -- Combining char
3358    -- U+0301 = CC 81  name: COMBINING ACUTE
3359    reset(nil, screen)
3360    push('0123\x1b[H', vt)
3361    push('e\xCC\x81', vt)
3362    screen_row(0, 'é123', screen)
3363    screen_text(0, 0, 1, 80, '65,cc,81,31,32,33', screen)
3364    screen_cell(0, 0, '{65,301} width=1 attrs={} fg=rgb(240,240,240) bg=rgb(0,0,0)', screen)
3365
3366    -- 10 combining accents should not crash
3367    reset(nil, screen)
3368    push('e\xCC\x81\xCC\x82\xCC\x83\xCC\x84\xCC\x85\xCC\x86\xCC\x87\xCC\x88\xCC\x89\xCC\x8A', vt)
3369    screen_cell(
3370      0,
3371      0,
3372      '{65,301,302,303,304,305,306,307,308,309,30a} width=1 attrs={} fg=rgb(240,240,240) bg=rgb(0,0,0)',
3373      screen
3374    )
3375
3376    -- 40 combining accents in two split writes of 20 should not crash
3377    reset(nil, screen)
3378    push(
3379      'e\xCC\x81\xCC\x81\xCC\x81\xCC\x81\xCC\x81\xCC\x81\xCC\x81\xCC\x81\xCC\x81\xCC\x81\xCC\x81\xCC\x81\xCC\x81\xCC\x81\xCC\x81\xCC\x81\xCC\x81\xCC\x81\xCC\x81\xCC\x81',
3380      vt
3381    )
3382    push(
3383      '\xCC\x81\xCC\x81\xCC\x81\xCC\x81\xCC\x81\xCC\x81\xCC\x81\xCC\x81\xCC\x81\xCC\x81\xCC\x81\xCC\x81\xCC\x81\xCC\x81\xCC\x81\xCC\x81\xCC\x81\xCC\x81\xCC\x81\xCC\x81',
3384      vt
3385    )
3386    screen_cell(
3387      0,
3388      0,
3389      '{65,301,301,301,301,301,301,301,301,301,301,301,301,301,301} width=1 attrs={} fg=rgb(240,240,240) bg=rgb(0,0,0)',
3390      screen
3391    )
3392
3393    -- Outputting CJK doublewidth in 80th column should wraparound to next line and not crash"
3394    reset(nil, screen)
3395    push('\x1b[80G\xEF\xBC\x90', vt)
3396    screen_cell(0, 79, '{} width=1 attrs={} fg=rgb(240,240,240) bg=rgb(0,0,0)', screen)
3397    screen_cell(1, 0, '{ff10} width=2 attrs={} fg=rgb(240,240,240) bg=rgb(0,0,0)', screen)
3398
3399    -- Outputting emoji with ZWJ and variant selectors
3400    reset(nil, screen)
3401    push('🏳️‍🌈🏳️‍⚧️🏴‍☠️', vt)
3402
3403    -- stylua: ignore start
3404    screen_cell(0, 0, '{1f3f3,fe0f,200d,1f308} width=2 attrs={} fg=rgb(240,240,240) bg=rgb(0,0,0)', screen)
3405    screen_cell(0, 2, '{1f3f3,fe0f,200d,26a7,fe0f} width=2 attrs={} fg=rgb(240,240,240) bg=rgb(0,0,0)', screen)
3406    screen_cell(0, 4, '{1f3f4,200d,2620,fe0f} width=2 attrs={} fg=rgb(240,240,240) bg=rgb(0,0,0)', screen)
3407    -- stylua: ignore end
3408  end)
3409
3410  pending('62screen_damage', function() end)
3411
3412  itp('63screen_resize', function()
3413    local vt = init()
3414    local state = wantstate(vt)
3415    local screen = wantscreen(vt)
3416
3417    -- Resize wider preserves cells
3418    reset(state, screen)
3419    resize(25, 80, vt)
3420    push('AB\r\nCD', vt)
3421    screen_chars(0, 0, 1, 80, 'AB', screen)
3422    screen_chars(1, 0, 2, 80, 'CD', screen)
3423    resize(25, 100, vt)
3424    screen_chars(0, 0, 1, 100, 'AB', screen)
3425    screen_chars(1, 0, 2, 100, 'CD', screen)
3426
3427    -- Resize wider allows print in new area
3428    reset(state, screen)
3429    resize(25, 80, vt)
3430    push('AB\x1b[79GCD', vt)
3431    screen_chars(0, 0, 1, 2, 'AB', screen)
3432    screen_chars(0, 78, 1, 80, 'CD', screen)
3433    resize(25, 100, vt)
3434    screen_chars(0, 0, 1, 2, 'AB', screen)
3435    screen_chars(0, 78, 1, 80, 'CD', screen)
3436    push('E', vt)
3437    screen_chars(0, 78, 1, 81, 'CDE', screen)
3438
3439    -- Resize shorter with blanks just truncates
3440    reset(state, screen)
3441    resize(25, 80, vt)
3442    push('Top\x1b[10HLine 10', vt)
3443    screen_row(0, 'Top', screen)
3444    screen_row(9, 'Line 10', screen)
3445    cursor(9, 7, state)
3446    resize(20, 80, vt)
3447    screen_row(0, 'Top', screen)
3448    screen_row(9, 'Line 10', screen)
3449    cursor(9, 7, state)
3450
3451    -- Resize shorter with content must scroll
3452    reset(state, screen)
3453    resize(25, 80, vt)
3454    push('Top\x1b[25HLine 25\x1b[15H', vt)
3455    screen_row(0, 'Top', screen)
3456    screen_row(24, 'Line 25', screen)
3457    cursor(14, 0, state)
3458    screen = wantscreen(vt, { b = true })
3459    resize(20, 80, vt)
3460    expect(
3461      'sb_pushline 80 = 54 6f 70\nsb_pushline 80 =\nsb_pushline 80 =\nsb_pushline 80 =\nsb_pushline 80 ='
3462    )
3463    -- TODO(dundargoc): fix or remove
3464    -- screen_row( 0  , "",screen)
3465    screen_row(19, 'Line 25', screen)
3466    cursor(9, 0, state)
3467
3468    -- Resize shorter does not lose line with cursor
3469    -- See also https://github.com/neovim/libvterm/commit/1b745d29d45623aa8d22a7b9288c7b0e331c7088
3470    reset(state, screen)
3471    wantscreen(vt)
3472    resize(25, 80, vt)
3473    screen = wantscreen(vt, { b = true })
3474    push('\x1b[24HLine 24\r\nLine 25\r\n', vt)
3475    expect('sb_pushline 80 =')
3476    screen_row(23, 'Line 25', screen)
3477    cursor(24, 0, state)
3478    resize(24, 80, vt)
3479    expect('sb_pushline 80 =')
3480    screen_row(22, 'Line 25', screen)
3481    cursor(23, 0, state)
3482
3483    -- Resize shorter does not send the cursor to a negative row
3484    -- See also https://github.com/vim/vim/pull/6141
3485    reset(state, screen)
3486    wantscreen(vt)
3487    resize(25, 80, vt)
3488    screen = wantscreen(vt, { b = true })
3489    push('\x1b[24HLine 24\r\nLine 25\x1b[H', vt)
3490    cursor(0, 0, state)
3491    resize(20, 80, vt)
3492    expect(
3493      'sb_pushline 80 =\nsb_pushline 80 =\nsb_pushline 80 =\nsb_pushline 80 =\nsb_pushline 80 ='
3494    )
3495    cursor(0, 0, state)
3496
3497    -- Resize taller attempts to pop scrollback
3498    reset(state, screen)
3499    screen = wantscreen(vt)
3500    resize(25, 80, vt)
3501    push('Line 1\x1b[25HBottom\x1b[15H', vt)
3502    screen_row(0, 'Line 1', screen)
3503    screen_row(24, 'Bottom', screen)
3504    cursor(14, 0, state)
3505    screen = wantscreen(vt, { b = true })
3506    resize(30, 80, vt)
3507    expect('sb_popline 80\nsb_popline 80\nsb_popline 80\nsb_popline 80\nsb_popline 80')
3508    screen_row(0, 'ABCDE', screen)
3509    screen_row(5, 'Line 1', screen)
3510    screen_row(29, 'Bottom', screen)
3511    cursor(19, 0, state)
3512    screen = wantscreen(vt)
3513
3514    -- Resize can operate on altscreen
3515    reset(state, screen)
3516    screen = wantscreen(vt, { a = true })
3517    resize(25, 80, vt)
3518    push('Main screen\x1b[?1049h\x1b[HAlt screen', vt)
3519    resize(30, 80, vt)
3520    screen_row(0, 'Alt screen', screen)
3521    push('\x1b[?1049l', vt)
3522    screen_row(0, 'Main screen', screen)
3523  end)
3524
3525  itp('64screen_pen', function()
3526    local vt = init()
3527    local screen = wantscreen(vt)
3528
3529    reset(nil, screen)
3530
3531    -- Plain
3532    push('A', vt)
3533    screen_cell(0, 0, '{41} width=1 attrs={} fg=rgb(240,240,240) bg=rgb(0,0,0)', screen)
3534
3535    -- Bold
3536    push('\x1b[1mB', vt)
3537    screen_cell(0, 1, '{42} width=1 attrs={B} fg=rgb(240,240,240) bg=rgb(0,0,0)', screen)
3538
3539    -- Italic
3540    push('\x1b[3mC', vt)
3541    screen_cell(0, 2, '{43} width=1 attrs={BI} fg=rgb(240,240,240) bg=rgb(0,0,0)', screen)
3542
3543    -- Underline
3544    push('\x1b[4mD', vt)
3545    screen_cell(0, 3, '{44} width=1 attrs={BU1I} fg=rgb(240,240,240) bg=rgb(0,0,0)', screen)
3546
3547    -- Reset
3548    push('\x1b[mE', vt)
3549    screen_cell(0, 4, '{45} width=1 attrs={} fg=rgb(240,240,240) bg=rgb(0,0,0)', screen)
3550
3551    -- Font
3552    push('\x1b[11mF\x1b[m', vt)
3553    screen_cell(0, 5, '{46} width=1 attrs={F1} fg=rgb(240,240,240) bg=rgb(0,0,0)', screen)
3554
3555    -- Foreground
3556    push('\x1b[31mG\x1b[m', vt)
3557    screen_cell(0, 6, '{47} width=1 attrs={} fg=rgb(224,0,0) bg=rgb(0,0,0)', screen)
3558
3559    -- Background
3560    push('\x1b[42mH\x1b[m', vt)
3561    screen_cell(0, 7, '{48} width=1 attrs={} fg=rgb(240,240,240) bg=rgb(0,224,0)', screen)
3562
3563    -- Super/subscript
3564    push('x\x1b[74m0\x1b[73m2\x1b[m', vt)
3565    screen_cell(0, 8, '{78} width=1 attrs={} fg=rgb(240,240,240) bg=rgb(0,0,0)', screen)
3566    screen_cell(0, 9, '{30} width=1 attrs={S_} fg=rgb(240,240,240) bg=rgb(0,0,0)', screen)
3567    screen_cell(0, 10, '{32} width=1 attrs={S^} fg=rgb(240,240,240) bg=rgb(0,0,0)', screen)
3568
3569    -- Dim
3570    push('\x1b[2mI\x1b[m', vt)
3571    screen_cell(0, 11, '{49} width=1 attrs={D} fg=rgb(240,240,240) bg=rgb(0,0,0)', screen)
3572
3573    -- Overline
3574    push('\x1b[53mJ\x1b[m', vt)
3575    screen_cell(0, 12, '{4a} width=1 attrs={O} fg=rgb(240,240,240) bg=rgb(0,0,0)', screen)
3576
3577    -- EL sets only colours to end of line, not other attrs
3578    push('\x1b[H\x1b[7;33;44m\x1b[K', vt)
3579    screen_cell(0, 0, '{} width=1 attrs={} fg=rgb(224,224,0) bg=rgb(0,0,224)', screen)
3580    screen_cell(0, 79, '{} width=1 attrs={} fg=rgb(224,224,0) bg=rgb(0,0,224)', screen)
3581
3582    -- DECSCNM xors reverse for entire screen
3583    push('R\x1b[?5h', vt)
3584    screen_cell(0, 0, '{52} width=1 attrs={} fg=rgb(224,224,0) bg=rgb(0,0,224)', screen)
3585    screen_cell(1, 0, '{} width=1 attrs={R} fg=rgb(240,240,240) bg=rgb(0,0,0)', screen)
3586    push('\x1b[?5$p', vt)
3587    expect_output('\x1b[?5;1$y')
3588    push('\x1b[?5l', vt)
3589    screen_cell(0, 0, '{52} width=1 attrs={R} fg=rgb(224,224,0) bg=rgb(0,0,224)', screen)
3590    screen_cell(1, 0, '{} width=1 attrs={} fg=rgb(240,240,240) bg=rgb(0,0,0)', screen)
3591    -- TODO(dundargoc): fix
3592    -- push('\x1b[?5$p')
3593    -- expect_output('\x1b[?5;2$y')
3594
3595    -- Set default colours
3596    reset(nil, screen)
3597    push('ABC\x1b[31mDEF\x1b[m', vt)
3598    screen_cell(0, 0, '{41} width=1 attrs={} fg=rgb(240,240,240) bg=rgb(0,0,0)', screen)
3599    screen_cell(0, 3, '{44} width=1 attrs={} fg=rgb(224,0,0) bg=rgb(0,0,0)', screen)
3600    -- TODO(dundargoc): fix
3601    -- SETDEFAULTCOL rgb(252,253,254)
3602    --   ?screen_cell 0,0  = {41} width=1 attrs={} fg=rgb(252,253,254) bg=rgb(0,0,0)
3603    --   ?screen_cell 0,3  = {44} width=1 attrs={} fg=rgb(224,0,0) bg=rgb(0,0,0)
3604    -- SETDEFAULTCOL rgb(250,250,250) rgb(10,20,30)
3605    --   ?screen_cell 0,0  = {41} width=1 attrs={} fg=rgb(250,250,250) bg=rgb(10,20,30)
3606    --   ?screen_cell 0,3  = {44} width=1 attrs={} fg=rgb(224,0,0) bg=rgb(10,20,30)
3607  end)
3608
3609  itp('65screen_protect', function()
3610    local vt = init()
3611    local screen = wantscreen(vt)
3612
3613    -- Selective erase
3614    reset(nil, screen)
3615    push('A\x1b[1"qB\x1b["qC', vt)
3616    screen_row(0, 'ABC', screen)
3617    push('\x1b[G\x1b[?J', vt)
3618    screen_row(0, ' B', screen)
3619
3620    -- Non-selective erase
3621    reset(nil, screen)
3622    push('A\x1b[1"qB\x1b["qC', vt)
3623    screen_row(0, 'ABC', screen)
3624    -- TODO(dundargoc): fix
3625    -- push('\x1b[G\x1b[J', vt)
3626    -- screen_row(0, '', screen)
3627  end)
3628
3629  itp('66screen_extent', function()
3630    local vt = init()
3631    local screen = wantscreen(vt)
3632
3633    -- Bold extent
3634    reset(nil, screen)
3635    push('AB\x1b[1mCD\x1b[mE', vt)
3636    screen_attrs_extent(0, 0, '0,0-1,1', screen)
3637    screen_attrs_extent(0, 1, '0,0-1,1', screen)
3638    screen_attrs_extent(0, 2, '0,2-1,3', screen)
3639    screen_attrs_extent(0, 3, '0,2-1,3', screen)
3640    screen_attrs_extent(0, 4, '0,4-1,79', screen)
3641  end)
3642
3643  itp('67screen_dbl_wh', function()
3644    local vt = init()
3645    local screen = wantscreen(vt)
3646
3647    reset(nil, screen)
3648
3649    -- Single Width, Single Height
3650    reset(nil, screen)
3651    push('\x1b#5', vt)
3652    push('abcde', vt)
3653    screen_cell(0, 0, '{61} width=1 attrs={} fg=rgb(240,240,240) bg=rgb(0,0,0)', screen)
3654
3655    -- Double Width, Single Height
3656    reset(nil, screen)
3657    push('\x1b#6', vt)
3658    push('abcde', vt)
3659    screen_cell(0, 0, '{61} width=1 attrs={} dwl fg=rgb(240,240,240) bg=rgb(0,0,0)', screen)
3660
3661    -- Double Height
3662    reset(nil, screen)
3663    push('\x1b#3', vt)
3664    push('abcde', vt)
3665    push('\r\n\x1b#4', vt)
3666    push('abcde', vt)
3667    screen_cell(0, 0, '{61} width=1 attrs={} dwl dhl-top fg=rgb(240,240,240) bg=rgb(0,0,0)', screen)
3668    screen_cell(
3669      1,
3670      0,
3671      '{61} width=1 attrs={} dwl dhl-bottom fg=rgb(240,240,240) bg=rgb(0,0,0)',
3672      screen
3673    )
3674
3675    -- Late change
3676    reset(nil, screen)
3677    push('abcde', vt)
3678    screen_cell(0, 0, '{61} width=1 attrs={} fg=rgb(240,240,240) bg=rgb(0,0,0)', screen)
3679    push('\x1b#6', vt)
3680    screen_cell(0, 0, '{61} width=1 attrs={} dwl fg=rgb(240,240,240) bg=rgb(0,0,0)', screen)
3681
3682    -- DWL doesn't spill over on scroll
3683    reset(nil, screen)
3684    push('\x1b[25H\x1b#6Final\r\n', vt)
3685    screen_cell(23, 0, '{46} width=1 attrs={} dwl fg=rgb(240,240,240) bg=rgb(0,0,0)', screen)
3686    screen_cell(24, 0, '{} width=1 attrs={} fg=rgb(240,240,240) bg=rgb(0,0,0)', screen)
3687  end)
3688
3689  itp('68screen_termprops', function()
3690    local vt = init()
3691    local screen = wantscreen(vt, { p = true })
3692
3693    reset(nil, screen)
3694    expect('settermprop 1 true\nsettermprop 2 true\nsettermprop 7 1')
3695
3696    -- Cursor visibility
3697    push('\x1b[?25h', vt)
3698    expect('settermprop 1 true')
3699    push('\x1b[?25l', vt)
3700    expect('settermprop 1 false')
3701
3702    -- Title
3703    push('\x1b]2;Here is my title\a', vt)
3704    expect('settermprop 4 ["Here is my title"]')
3705  end)
3706
3707  itp('69screen_pushline', function()
3708    local vt = init()
3709    -- Run these tests on a much smaller default screen, so debug output is nowhere near as noisy
3710    resize(5, 10, vt)
3711    local state = wantstate(vt)
3712    local screen = wantscreen(vt, { r = true })
3713    reset(state, screen)
3714
3715    -- Resize wider reflows wide lines
3716    reset(state, screen)
3717    push(string.rep('A', 12), vt)
3718    screen_row(0, 'AAAAAAAAAA', screen, vt.cols)
3719    screen_row(1, 'AA', screen, vt.cols)
3720    lineinfo(1, { cont = true }, state)
3721    cursor(1, 2, state)
3722    resize(5, 15, vt)
3723    screen_row(0, 'AAAAAAAAAAAA', screen, vt.cols)
3724    -- TODO(dundargoc): fix
3725    -- screen_row(1, '', screen, vt.cols)
3726    lineinfo(1, {}, state)
3727    cursor(0, 12, state)
3728    resize(5, 20, vt)
3729    screen_row(0, 'AAAAAAAAAAAA', screen, vt.cols)
3730    -- TODO(dundargoc): fix
3731    -- screen_row( 1 ,'',screen, vt.cols)
3732    lineinfo(1, {}, state)
3733    cursor(0, 12, state)
3734
3735    -- Resize narrower can create continuation lines
3736    reset(state, screen)
3737    resize(5, 10, vt)
3738    push('ABCDEFGHI', vt)
3739    screen_row(0, 'ABCDEFGHI', screen, vt.cols)
3740    -- TODO(dundargoc): fix
3741    -- screen_row( 1 , "",screen, vt.cols)
3742    lineinfo(1, {}, state)
3743    cursor(0, 9, state)
3744    resize(5, 8, vt)
3745    -- TODO(dundargoc): fix
3746    -- screen_row( 0 , "ABCDEFGH",screen,vt.cols)
3747    screen_row(1, 'I', screen, vt.cols)
3748    lineinfo(1, { cont = true }, state)
3749    cursor(1, 1, state)
3750    resize(5, 6, vt)
3751    screen_row(0, 'ABCDEF', screen, vt.cols)
3752    screen_row(1, 'GHI', screen, vt.cols)
3753    lineinfo(1, { cont = true }, state)
3754    cursor(1, 3, state)
3755
3756    -- Shell wrapped prompt behaviour
3757    reset(state, screen)
3758    resize(5, 10, vt)
3759    push('PROMPT GOES HERE\r\n> \r\n\r\nPROMPT GOES HERE\r\n> ', vt)
3760    screen_row(0, '> ', screen, vt.cols)
3761    -- TODO(dundargoc): fix
3762    -- screen_row( 1 , "",screen,vt.cols)
3763    screen_row(2, 'PROMPT GOE', screen, vt.cols)
3764    screen_row(3, 'S HERE', screen, vt.cols)
3765    lineinfo(3, { cont = true }, state)
3766    screen_row(4, '> ', screen, vt.cols)
3767    cursor(4, 2, state)
3768    resize(5, 11, vt)
3769    screen_row(0, '> ', screen, vt.cols)
3770    -- TODO(dundargoc): fix
3771    -- screen_row( 1 , "",screen,vt.cols)
3772    screen_row(2, 'PROMPT GOES', screen, vt.cols)
3773    screen_row(3, ' HERE', screen, vt.cols)
3774    lineinfo(3, { cont = true }, state)
3775    screen_row(4, '> ', screen, vt.cols)
3776    cursor(4, 2, state)
3777    resize(5, 12, vt)
3778    screen_row(0, '> ', screen, vt.cols)
3779    -- TODO(dundargoc): fix
3780    -- screen_row( 1 , "",screen,vt.cols)
3781    screen_row(2, 'PROMPT GOES ', screen, vt.cols)
3782    screen_row(3, 'HERE', screen, vt.cols)
3783    lineinfo(3, { cont = true }, state)
3784    screen_row(4, '> ', screen, vt.cols)
3785    cursor(4, 2, state)
3786    resize(5, 16, vt)
3787    screen_row(0, '> ', screen, vt.cols)
3788    -- TODO(dundargoc): fix
3789    -- screen_row( 1 , "",screen,vt.cols)
3790    -- screen_row( 2 , "PROMPT GOES HERE",screen,vt.cols)
3791    lineinfo(3, {}, state)
3792    screen_row(3, '> ', screen, vt.cols)
3793    cursor(3, 2, state)
3794
3795    -- Cursor goes missing
3796    -- For more context: https://github.com/neovim/neovim/pull/21124
3797    reset(state, screen)
3798    resize(5, 5, vt)
3799    resize(3, 1, vt)
3800    push('\x1b[2;1Habc\r\n\x1b[H', vt)
3801    resize(1, 1, vt)
3802    cursor(0, 0, state)
3803
3804    reset(state, screen)
3805    resize(4, 4, vt)
3806    push('ABCD\r\nX', vt)
3807    screen_row(0, 'ABCD', screen, vt.cols)
3808    screen_row(1, 'X', screen, vt.cols)
3809    cursor(1, 1, state)
3810    resize(4, 5, vt)
3811    screen_row(0, 'ABCD', screen, vt.cols)
3812    screen_row(1, 'X', screen, vt.cols)
3813    cursor(1, 1, state)
3814
3815    reset(state, screen)
3816    resize(4, 4, vt)
3817    push('ABCD\r\nX', vt)
3818    cursor(1, 1, state)
3819    resize(3, 4, vt)
3820    screen_row(0, 'ABCD', screen, vt.cols)
3821    screen_row(1, 'X', screen, vt.cols)
3822    cursor(1, 1, state)
3823
3824    resize(5, 10, vt)
3825  end)
3826
3827  pending('90vttest_01-movement-1', function() end)
3828  pending('90vttest_01-movement-2', function() end)
3829
3830  itp('90vttest_01-movement-3', function()
3831    -- Test of cursor-control characters inside ESC sequences
3832    local vt = init()
3833    local state = wantstate(vt)
3834    local screen = wantscreen(vt)
3835
3836    reset(state, screen)
3837
3838    push('A B C D E F G H I', vt)
3839    push('\x0d\x0a', vt)
3840    push('A\x1b[2\bCB\x1b[2\bCC\x1b[2\bCD\x1b[2\bCE\x1b[2\bCF\x1b[2\bCG\x1b[2\bCH\x1b[2\bCI', vt)
3841    push('\x0d\x0a', vt)
3842    push(
3843      'A \x1b[\x0d2CB\x1b[\x0d4CC\x1b[\x0d6CD\x1b[\x0d8CE\x1b[\x0d10CF\x1b[\x0d12CG\x1b[\x0d14CH\x1b[\x0d16CI',
3844      vt
3845    )
3846    push('\x0d\x0a', vt)
3847    push(
3848      'A \x1b[1\x0bAB \x1b[1\x0bAC \x1b[1\x0bAD \x1b[1\x0bAE \x1b[1\x0bAF \x1b[1\x0bAG \x1b[1\x0bAH \x1b[1\x0bAI \x1b[1\x0bA',
3849      vt
3850    )
3851
3852    -- Output
3853
3854    for i = 0, 2 do
3855      screen_row(i, 'A B C D E F G H I', screen)
3856    end
3857    screen_row(3, 'A B C D E F G H I ', screen)
3858
3859    cursor(3, 18, state)
3860  end)
3861
3862  itp('90vttest_01-movement-4', function()
3863    -- Test of leading zeroes in ESC sequences
3864    local vt = init()
3865    local screen = wantscreen(vt)
3866
3867    reset(nil, screen)
3868
3869    push('\x1b[00000000004;000000001HT', vt)
3870    push('\x1b[00000000004;000000002Hh', vt)
3871    push('\x1b[00000000004;000000003Hi', vt)
3872    push('\x1b[00000000004;000000004Hs', vt)
3873    push('\x1b[00000000004;000000005H ', vt)
3874    push('\x1b[00000000004;000000006Hi', vt)
3875    push('\x1b[00000000004;000000007Hs', vt)
3876    push('\x1b[00000000004;000000008H ', vt)
3877    push('\x1b[00000000004;000000009Ha', vt)
3878    push('\x1b[00000000004;0000000010H ', vt)
3879    push('\x1b[00000000004;0000000011Hc', vt)
3880    push('\x1b[00000000004;0000000012Ho', vt)
3881    push('\x1b[00000000004;0000000013Hr', vt)
3882    push('\x1b[00000000004;0000000014Hr', vt)
3883    push('\x1b[00000000004;0000000015He', vt)
3884    push('\x1b[00000000004;0000000016Hc', vt)
3885    push('\x1b[00000000004;0000000017Ht', vt)
3886    push('\x1b[00000000004;0000000018H ', vt)
3887    push('\x1b[00000000004;0000000019Hs', vt)
3888    push('\x1b[00000000004;0000000020He', vt)
3889    push('\x1b[00000000004;0000000021Hn', vt)
3890    push('\x1b[00000000004;0000000022Ht', vt)
3891    push('\x1b[00000000004;0000000023He', vt)
3892    push('\x1b[00000000004;0000000024Hn', vt)
3893    push('\x1b[00000000004;0000000025Hc', vt)
3894    push('\x1b[00000000004;0000000026He', vt)
3895
3896    -- Output
3897
3898    screen_row(3, 'This is a correct sentence', screen)
3899  end)
3900
3901  pending('90vttest_02-screen-1', function() end)
3902  pending('90vttest_02-screen-2', function() end)
3903
3904  itp('90vttest_02-screen-3', function()
3905    -- Origin mode
3906    local vt = init()
3907    local screen = wantscreen(vt)
3908
3909    reset(nil, screen)
3910
3911    push('\x1b[?6h', vt)
3912    push('\x1b[23;24r', vt)
3913    push('\n', vt)
3914    push('Bottom', vt)
3915    push('\x1b[1;1H', vt)
3916    push('Above', vt)
3917
3918    -- Output
3919    screen_row(22, 'Above', screen)
3920    screen_row(23, 'Bottom', screen)
3921  end)
3922
3923  itp('90vttest_02-screen-4', function()
3924    -- Origin mode (2)
3925    local vt = init()
3926    local screen = wantscreen(vt)
3927
3928    reset(nil, screen)
3929
3930    push('\x1b[?6l', vt)
3931    push('\x1b[23;24r', vt)
3932    push('\x1b[24;1H', vt)
3933    push('Bottom', vt)
3934    push('\x1b[1;1H', vt)
3935    push('Top', vt)
3936
3937    -- Output
3938    screen_row(23, 'Bottom', screen)
3939    screen_row(0, 'Top', screen)
3940  end)
3941
3942  itp('Mouse reporting should not break by idempotent DECSM 1002', function()
3943    -- Regression test for https://bugs.launchpad.net/libvterm/+bug/1640917
3944    -- Related: https://github.com/neovim/neovim/issues/5583
3945    local vt = init()
3946    wantstate(vt, {})
3947
3948    push('\x1b[?1002h', vt)
3949    mousemove(0, 0, vt)
3950    mousebtn('d', 1, vt)
3951    expect_output('\x1b[M\x20\x21\x21')
3952    mousemove(1, 0, vt)
3953    expect_output('\x1b[M\x40\x21\x22')
3954    push('\x1b[?1002h', vt)
3955    mousemove(2, 0, vt)
3956    expect_output('\x1b[M\x40\x21\x23')
3957  end)
3958end)