blob: 71cb071ab75faf1765742b213d874578301ed398 [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 Moolenaara9a6b032023-02-05 18:00:42 +000054" Create a new window with the requested size and fix it.
Bram Moolenaar97f0eb12022-10-06 19:49:13 +010055func NewWindow(height, width) abort
Bram Moolenaar544d3bc2017-02-05 21:14:50 +010056 exe a:height . 'new'
57 exe a:width . 'vsp'
Bram Moolenaarbffba7f2019-09-20 17:00:17 +020058 set winfixwidth winfixheight
Bram Moolenaar544d3bc2017-02-05 21:14:50 +010059 redraw!
Bram Moolenaar97f0eb12022-10-06 19:49:13 +010060endfunc
Bram Moolenaar544d3bc2017-02-05 21:14:50 +010061
Bram Moolenaar97f0eb12022-10-06 19:49:13 +010062func CloseWindow() abort
Bram Moolenaar544d3bc2017-02-05 21:14:50 +010063 bw!
64 redraw!
Bram Moolenaar97f0eb12022-10-06 19:49:13 +010065endfunc
Bram Moolenaar64fabf32022-11-27 13:51:22 +000066
67
68" When using RunVimInTerminal() we expect modifyOtherKeys level 2 to be enabled
69" automatically. The key + modifier Escape codes must then use the
70" modifyOtherKeys encoding. They are recognized anyway, thus it's safer to use
71" than the raw code.
72
73" Return the modifyOtherKeys level 2 encoding for "key" with "modifier"
74" (number value, e.g. CTRL is 5).
75func GetEscCodeCSI27(key, modifier)
76 let key = printf("%d", char2nr(a:key))
77 let mod = printf("%d", a:modifier)
78 return "\<Esc>[27;" .. mod .. ';' .. key .. '~'
79endfunc
80
81" Return the modifyOtherKeys level 2 encoding for "key" with "modifier"
82" (character value, e.g. CTRL is "C").
83func GetEscCodeWithModifier(modifier, key)
84 let modifier = get({'C': 5}, a:modifier, '')
85 if modifier == ''
86 echoerr 'Unknown modifier: ' .. a:modifier
87 endif
88 return GetEscCodeCSI27(a:key, modifier)
89endfunc
90
91" Return the kitty keyboard protocol encoding for "key" with "modifier"
92" (number value, e.g. CTRL is 5).
93func GetEscCodeCSIu(key, modifier)
94 let key = printf("%d", char2nr(a:key))
95 let mod = printf("%d", a:modifier)
96 return "\<Esc>[" .. key .. ';' .. mod .. 'u'
97endfunc
98
Bram Moolenaar1a173402022-12-02 12:28:47 +000099" Return the kitty keyboard protocol encoding for a function key:
100" CSI {key}
101" CSS 1;{modifier} {key}
102func GetEscCodeFunckey(key, modifier)
103 if a:modifier == 0
104 return "\<Esc>[" .. a:key
105 endif
106
107 let mod = printf("%d", a:modifier)
108 return "\<Esc>[1;".. mod .. a:key
109endfunc
110
Bram Moolenaar64fabf32022-11-27 13:51:22 +0000111" Return the kitty keyboard protocol encoding for "key" without a modifier.
112" Used for the Escape key.
113func GetEscCodeCSIuWithoutModifier(key)
114 let key = printf("%d", char2nr(a:key))
115 return "\<Esc>[" .. key .. 'u'
116endfunc
117