blob: a0c1781dd445f7beba80b0889a125e5cbecefa74 [file] [log] [blame]
Bram Moolenaar544d3bc2017-02-05 21:14:50 +01001" Functions about view shared by several tests
2
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02003" Only load this script once.
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02004if exists('*Screenline')
Bram Moolenaar0aa398f2017-09-30 21:23:55 +02005 finish
6endif
7
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02008" Get line "lnum" as displayed on the screen.
9" Trailing white space is trimmed.
10func Screenline(lnum)
11 let chars = []
12 for c in range(1, winwidth(0))
13 call add(chars, nr2char(screenchar(a:lnum, c)))
14 endfor
15 let line = join(chars, '')
16 return matchstr(line, '^.\{-}\ze\s*$')
17endfunc
18
Bram Moolenaar2912abb2019-03-29 14:16:42 +010019" Get text on the screen, including composing characters.
20" ScreenLines(lnum, width) or
21" ScreenLines([start, end], width)
Bram Moolenaar97f0eb12022-10-06 19:49:13 +010022func ScreenLines(lnum, width) abort
Bram Moolenaar2912abb2019-03-29 14:16:42 +010023 redraw!
24 if type(a:lnum) == v:t_list
25 let start = a:lnum[0]
26 let end = a:lnum[1]
27 else
28 let start = a:lnum
29 let end = a:lnum
30 endif
31 let lines = []
32 for l in range(start, end)
33 let lines += [join(map(range(1, a:width), 'screenstring(l, v:val)'), '')]
34 endfor
35 return lines
Bram Moolenaar97f0eb12022-10-06 19:49:13 +010036endfunc
Bram Moolenaar2912abb2019-03-29 14:16:42 +010037
Bram Moolenaar97f0eb12022-10-06 19:49:13 +010038func ScreenAttrs(lnum, width) abort
Bram Moolenaar0aa398f2017-09-30 21:23:55 +020039 redraw!
40 if type(a:lnum) == v:t_list
41 let start = a:lnum[0]
42 let end = a:lnum[1]
43 else
44 let start = a:lnum
45 let end = a:lnum
46 endif
47 let attrs = []
48 for l in range(start, end)
49 let attrs += [map(range(1, a:width), 'screenattr(l, v:val)')]
50 endfor
51 return attrs
Bram Moolenaar97f0eb12022-10-06 19:49:13 +010052endfunc
Bram Moolenaar0aa398f2017-09-30 21:23:55 +020053
Bram Moolenaar97f0eb12022-10-06 19:49:13 +010054func NewWindow(height, width) abort
Bram Moolenaar544d3bc2017-02-05 21:14:50 +010055 exe a:height . 'new'
56 exe a:width . 'vsp'
Bram Moolenaarbffba7f2019-09-20 17:00:17 +020057 set winfixwidth winfixheight
Bram Moolenaar544d3bc2017-02-05 21:14:50 +010058 redraw!
Bram Moolenaar97f0eb12022-10-06 19:49:13 +010059endfunc
Bram Moolenaar544d3bc2017-02-05 21:14:50 +010060
Bram Moolenaar97f0eb12022-10-06 19:49:13 +010061func CloseWindow() abort
Bram Moolenaar544d3bc2017-02-05 21:14:50 +010062 bw!
63 redraw!
Bram Moolenaar97f0eb12022-10-06 19:49:13 +010064endfunc
Bram Moolenaar64fabf32022-11-27 13:51:22 +000065
66
67" When using RunVimInTerminal() we expect modifyOtherKeys level 2 to be enabled
68" automatically. The key + modifier Escape codes must then use the
69" modifyOtherKeys encoding. They are recognized anyway, thus it's safer to use
70" than the raw code.
71
72" Return the modifyOtherKeys level 2 encoding for "key" with "modifier"
73" (number value, e.g. CTRL is 5).
74func GetEscCodeCSI27(key, modifier)
75 let key = printf("%d", char2nr(a:key))
76 let mod = printf("%d", a:modifier)
77 return "\<Esc>[27;" .. mod .. ';' .. key .. '~'
78endfunc
79
80" Return the modifyOtherKeys level 2 encoding for "key" with "modifier"
81" (character value, e.g. CTRL is "C").
82func GetEscCodeWithModifier(modifier, key)
83 let modifier = get({'C': 5}, a:modifier, '')
84 if modifier == ''
85 echoerr 'Unknown modifier: ' .. a:modifier
86 endif
87 return GetEscCodeCSI27(a:key, modifier)
88endfunc
89
90" Return the kitty keyboard protocol encoding for "key" with "modifier"
91" (number value, e.g. CTRL is 5).
92func GetEscCodeCSIu(key, modifier)
93 let key = printf("%d", char2nr(a:key))
94 let mod = printf("%d", a:modifier)
95 return "\<Esc>[" .. key .. ';' .. mod .. 'u'
96endfunc
97
98" Return the kitty keyboard protocol encoding for "key" without a modifier.
99" Used for the Escape key.
100func GetEscCodeCSIuWithoutModifier(key)
101 let key = printf("%d", char2nr(a:key))
102 return "\<Esc>[" .. key .. 'u'
103endfunc
104