blob: 147a3a4ae6d2f05f706b8a2548ecc8b2cc531c85 [file] [log] [blame]
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001" Tests for popup windows
2
Bram Moolenaarb46fecd2019-06-15 17:58:09 +02003source check.vim
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004CheckFeature popupwin
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005
6source screendump.vim
7
8func Test_simple_popup()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02009 CheckScreendump
10
Bram Moolenaare7eb9272019-06-24 00:58:07 +020011 let lines =<< trim END
12 call setline(1, range(1, 100))
13 hi PopupColor1 ctermbg=lightblue
14 hi PopupColor2 ctermbg=lightcyan
Bram Moolenaar1666ac92019-11-10 17:22:31 +010015 hi EndOfBuffer ctermbg=lightgrey
Bram Moolenaare7eb9272019-06-24 00:58:07 +020016 hi Comment ctermfg=red
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020017 call prop_type_add('comment', #{highlight: 'Comment'})
18 let winid = popup_create('hello there', #{line: 3, col: 11, minwidth: 20, highlight: 'PopupColor1'})
19 let winid2 = popup_create(['another one', 'another two', 'another three'], #{line: 3, col: 25, minwidth: 20})
Bram Moolenaare7eb9272019-06-24 00:58:07 +020020 call setwinvar(winid2, '&wincolor', 'PopupColor2')
21 END
22 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020023 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar4d784b22019-05-25 19:51:39 +020024 call VerifyScreenDump(buf, 'Test_popupwin_01', {})
25
Bram Moolenaarec583842019-05-26 14:11:23 +020026 " Add a tabpage
27 call term_sendkeys(buf, ":tabnew\<CR>")
Bram Moolenaar60cdb302019-05-27 21:54:10 +020028 call term_sendkeys(buf, ":let popupwin = popup_create(["
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020029 \ .. "#{text: 'other tab'},"
30 \ .. "#{text: 'a comment line', props: [#{"
Bram Moolenaard5abb4c2019-07-13 22:46:10 +020031 \ .. "col: 3, length: 7, minwidth: 20, type: 'comment'"
Bram Moolenaar7a8d0272019-05-26 23:32:06 +020032 \ .. "}]},"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020033 \ .. "], #{line: 4, col: 9, minwidth: 20})\<CR>")
Bram Moolenaarec583842019-05-26 14:11:23 +020034 call VerifyScreenDump(buf, 'Test_popupwin_02', {})
35
36 " switch back to first tabpage
37 call term_sendkeys(buf, "gt")
38 call VerifyScreenDump(buf, 'Test_popupwin_03', {})
39
40 " close that tabpage
41 call term_sendkeys(buf, ":quit!\<CR>")
42 call VerifyScreenDump(buf, 'Test_popupwin_04', {})
43
Bram Moolenaar202d9822019-06-11 21:56:30 +020044 " set 'columns' to a small value, size must be recomputed
45 call term_sendkeys(buf, ":let cols = &columns\<CR>")
46 call term_sendkeys(buf, ":set columns=12\<CR>")
47 call VerifyScreenDump(buf, 'Test_popupwin_04a', {})
48 call term_sendkeys(buf, ":let &columns = cols\<CR>")
49
Bram Moolenaar17146962019-05-30 00:12:11 +020050 " resize popup, show empty line at bottom
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020051 call term_sendkeys(buf, ":call popup_move(popupwin, #{minwidth: 15, maxwidth: 25, minheight: 3, maxheight: 5})\<CR>")
Bram Moolenaar60cdb302019-05-27 21:54:10 +020052 call term_sendkeys(buf, ":redraw\<CR>")
53 call VerifyScreenDump(buf, 'Test_popupwin_05', {})
54
Bram Moolenaar17146962019-05-30 00:12:11 +020055 " show not fitting line at bottom
56 call term_sendkeys(buf, ":call setbufline(winbufnr(popupwin), 3, 'this line will not fit here')\<CR>")
57 call term_sendkeys(buf, ":redraw\<CR>")
58 call VerifyScreenDump(buf, 'Test_popupwin_06', {})
59
Bram Moolenaar24a5ac52019-06-08 19:01:18 +020060 " move popup over ruler
61 call term_sendkeys(buf, ":set cmdheight=2\<CR>")
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020062 call term_sendkeys(buf, ":call popup_move(popupwin, #{line: 7, col: 55})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +020063 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar24a5ac52019-06-08 19:01:18 +020064 call VerifyScreenDump(buf, 'Test_popupwin_07', {})
65
66 " clear all popups after moving the cursor a bit, so that ruler is updated
67 call term_sendkeys(buf, "axxx\<Esc>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +020068 call TermWait(buf)
Bram Moolenaar24a5ac52019-06-08 19:01:18 +020069 call term_sendkeys(buf, "0")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +020070 call TermWait(buf)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +020071 call term_sendkeys(buf, ":call popup_clear()\<CR>")
Bram Moolenaar24a5ac52019-06-08 19:01:18 +020072 call VerifyScreenDump(buf, 'Test_popupwin_08', {})
73
Bram Moolenaar4d784b22019-05-25 19:51:39 +020074 " clean up
75 call StopVimInTerminal(buf)
76 call delete('XtestPopup')
77endfunc
Bram Moolenaar51fe3b12019-05-26 20:10:06 +020078
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020079func Test_popup_with_border_and_padding()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +020080 CheckScreendump
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020081
Bram Moolenaar3bfd04e2019-06-01 20:45:21 +020082 for iter in range(0, 1)
Bram Moolenaare7eb9272019-06-24 00:58:07 +020083 let lines =<< trim END
84 call setline(1, range(1, 100))
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020085 call popup_create('hello border', #{line: 2, col: 3, border: []})
86 call popup_create('hello padding', #{line: 2, col: 23, padding: []})
Bram Moolenaarc363fe12019-08-04 18:13:46 +020087 call popup_create('hello both', #{line: 2, col: 43, border: [], padding: [], highlight: 'Normal'})
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020088 call popup_create('border TL', #{line: 6, col: 3, border: [1, 0, 0, 4]})
Bram Moolenaarb0992022020-01-30 14:55:42 +010089 call popup_create('paddings', #{line: 6, col: 23, padding: range(1, 4)})
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020090 call popup_create('wrapped longer text', #{line: 8, col: 55, padding: [0, 3, 0, 3], border: [0, 1, 0, 1]})
91 call popup_create('right aligned text', #{line: 11, col: 56, wrap: 0, padding: [0, 3, 0, 3], border: [0, 1, 0, 1]})
Bram Moolenaarfbcdf672020-01-06 23:07:48 +010092 call popup_create('X', #{line: 2, col: 73})
93 call popup_create('X', #{line: 3, col: 74})
94 call popup_create('X', #{line: 4, col: 75})
95 call popup_create('X', #{line: 5, col: 76})
Bram Moolenaare7eb9272019-06-24 00:58:07 +020096 END
97 call insert(lines, iter == 1 ? '' : 'set enc=latin1')
98 call writefile(lines, 'XtestPopupBorder')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020099 let buf = RunVimInTerminal('-S XtestPopupBorder', #{rows: 15})
Bram Moolenaar3bfd04e2019-06-01 20:45:21 +0200100 call VerifyScreenDump(buf, 'Test_popupwin_2' .. iter, {})
101
102 call StopVimInTerminal(buf)
103 call delete('XtestPopupBorder')
104 endfor
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200105
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200106 let lines =<< trim END
107 call setline(1, range(1, 100))
108 hi BlueColor ctermbg=lightblue
109 hi TopColor ctermbg=253
110 hi RightColor ctermbg=245
111 hi BottomColor ctermbg=240
112 hi LeftColor ctermbg=248
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200113 call popup_create('hello border', #{line: 2, col: 3, border: [], borderhighlight: ['BlueColor']})
114 call popup_create(['hello border', 'and more'], #{line: 2, col: 23, border: [], borderhighlight: ['TopColor', 'RightColor', 'BottomColor', 'LeftColor']})
115 call popup_create(['hello border', 'lines only'], #{line: 2, col: 43, border: [], borderhighlight: ['BlueColor'], borderchars: ['x']})
116 call popup_create(['hello border', 'with corners'], #{line: 2, col: 60, border: [], borderhighlight: ['BlueColor'], borderchars: ['x', '#']})
117 let winid = popup_create(['hello border', 'with numbers'], #{line: 6, col: 3, border: [], borderhighlight: ['BlueColor'], borderchars: ['0', '1', '2', '3', '4', '5', '6', '7']})
118 call popup_create(['hello border', 'just blanks'], #{line: 7, col: 23, border: [], borderhighlight: ['BlueColor'], borderchars: [' ']})
Bram Moolenaar3dabd712019-07-08 23:30:22 +0200119 func MultiByte()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200120 call popup_create(['hello'], #{line: 8, col: 43, border: [], borderchars: ['─', '│', '─', '│', '┌', '┐', '┘', '└']})
Bram Moolenaar3dabd712019-07-08 23:30:22 +0200121 endfunc
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200122 END
123 call writefile(lines, 'XtestPopupBorder')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200124 let buf = RunVimInTerminal('-S XtestPopupBorder', #{rows: 12})
Bram Moolenaar790498b2019-06-01 22:15:29 +0200125 call VerifyScreenDump(buf, 'Test_popupwin_22', {})
126
Bram Moolenaarad24a712019-06-17 20:05:45 +0200127 " check that changing borderchars triggers a redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200128 call term_sendkeys(buf, ":call popup_setoptions(winid, #{borderchars: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']})\<CR>")
Bram Moolenaarad24a712019-06-17 20:05:45 +0200129 call VerifyScreenDump(buf, 'Test_popupwin_23', {})
130
Bram Moolenaar3dabd712019-07-08 23:30:22 +0200131 " check multi-byte border only with 'ambiwidth' single
132 if &ambiwidth == 'single'
133 call term_sendkeys(buf, ":call MultiByte()\<CR>")
134 call VerifyScreenDump(buf, 'Test_popupwin_24', {})
135 endif
136
Bram Moolenaar790498b2019-06-01 22:15:29 +0200137 call StopVimInTerminal(buf)
138 call delete('XtestPopupBorder')
139
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200140 let with_border_or_padding = #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200141 \ line: 2,
142 \ core_line: 3,
143 \ col: 3,
144 \ core_col: 4,
145 \ width: 14,
146 \ core_width: 12,
147 \ height: 3,
148 \ core_height: 1,
149 \ firstline: 1,
Bram Moolenaar30efcf32019-11-03 22:29:38 +0100150 \ lastline: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200151 \ scrollbar: 0,
152 \ visible: 1}
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200153 let winid = popup_create('hello border', #{line: 2, col: 3, border: []})",
Bram Moolenaar6a124e62019-09-04 18:15:19 +0200154 call assert_equal(with_border_or_padding, winid->popup_getpos())
Bram Moolenaarae943152019-06-16 22:54:14 +0200155 let options = popup_getoptions(winid)
156 call assert_equal([], options.border)
157 call assert_false(has_key(options, "padding"))
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200158
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200159 let winid = popup_create('hello padding', #{line: 2, col: 3, padding: []})
Bram Moolenaarae943152019-06-16 22:54:14 +0200160 let with_border_or_padding.width = 15
161 let with_border_or_padding.core_width = 13
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200162 call assert_equal(with_border_or_padding, popup_getpos(winid))
Bram Moolenaarae943152019-06-16 22:54:14 +0200163 let options = popup_getoptions(winid)
164 call assert_false(has_key(options, "border"))
165 call assert_equal([], options.padding)
166
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200167 call popup_setoptions(winid, #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200168 \ padding: [1, 2, 3, 4],
169 \ border: [4, 0, 7, 8],
170 \ borderhighlight: ['Top', 'Right', 'Bottom', 'Left'],
171 \ borderchars: ['1', '^', '2', '>', '3', 'v', '4', '<'],
Bram Moolenaarae943152019-06-16 22:54:14 +0200172 \ })
173 let options = popup_getoptions(winid)
174 call assert_equal([1, 0, 1, 1], options.border)
175 call assert_equal([1, 2, 3, 4], options.padding)
176 call assert_equal(['Top', 'Right', 'Bottom', 'Left'], options.borderhighlight)
177 call assert_equal(['1', '^', '2', '>', '3', 'v', '4', '<'], options.borderchars)
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200178
Bram Moolenaara1396152019-10-20 18:46:05 +0200179 " Check that popup_setoptions() takes the output of popup_getoptions()
180 call popup_setoptions(winid, options)
181 call assert_equal(options, popup_getoptions(winid))
182
Bram Moolenaarb0992022020-01-30 14:55:42 +0100183 " Check that range() doesn't crash
184 call popup_setoptions(winid, #{
185 \ padding: range(1, 4),
186 \ border: range(5, 8),
187 \ borderhighlight: range(4),
188 \ borderchars: range(8),
189 \ })
190
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200191 let winid = popup_create('hello both', #{line: 3, col: 8, border: [], padding: []})
192 call assert_equal(#{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200193 \ line: 3,
194 \ core_line: 5,
195 \ col: 8,
196 \ core_col: 10,
197 \ width: 14,
198 \ core_width: 10,
199 \ height: 5,
200 \ scrollbar: 0,
201 \ core_height: 1,
202 \ firstline: 1,
Bram Moolenaar30efcf32019-11-03 22:29:38 +0100203 \ lastline: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200204 \ visible: 1}, popup_getpos(winid))
Bram Moolenaarae943152019-06-16 22:54:14 +0200205
206 call popup_clear()
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200207endfunc
208
Bram Moolenaarb4230122019-05-30 18:40:53 +0200209func Test_popup_with_syntax_win_execute()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200210 CheckScreendump
211
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200212 let lines =<< trim END
213 call setline(1, range(1, 100))
214 hi PopupColor ctermbg=lightblue
215 let winid = popup_create([
216 \ '#include <stdio.h>',
217 \ 'int main(void)',
218 \ '{',
219 \ ' printf(123);',
220 \ '}',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200221 \], #{line: 3, col: 25, highlight: 'PopupColor'})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200222 call win_execute(winid, 'set syntax=cpp')
223 END
224 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200225 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaarb4230122019-05-30 18:40:53 +0200226 call VerifyScreenDump(buf, 'Test_popupwin_10', {})
227
228 " clean up
229 call StopVimInTerminal(buf)
230 call delete('XtestPopup')
231endfunc
232
233func Test_popup_with_syntax_setbufvar()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200234 CheckScreendump
235
Bram Moolenaar402502d2019-05-30 22:07:36 +0200236 let lines =<< trim END
237 call setline(1, range(1, 100))
238 hi PopupColor ctermbg=lightgrey
239 let winid = popup_create([
240 \ '#include <stdio.h>',
241 \ 'int main(void)',
242 \ '{',
Bram Moolenaare089c3f2019-07-09 20:25:25 +0200243 \ "\tprintf(567);",
Bram Moolenaar402502d2019-05-30 22:07:36 +0200244 \ '}',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200245 \], #{line: 3, col: 21, highlight: 'PopupColor'})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200246 call setbufvar(winbufnr(winid), '&syntax', 'cpp')
247 END
248 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200249 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaarb4230122019-05-30 18:40:53 +0200250 call VerifyScreenDump(buf, 'Test_popupwin_11', {})
251
252 " clean up
253 call StopVimInTerminal(buf)
254 call delete('XtestPopup')
255endfunc
256
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200257func Test_popup_with_matches()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200258 CheckScreendump
259
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200260 let lines =<< trim END
261 call setline(1, ['111 222 333', '444 555 666'])
262 let winid = popup_create([
263 \ '111 222 333',
264 \ '444 555 666',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200265 \], #{line: 3, col: 10, border: []})
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200266 set hlsearch
Bram Moolenaar024dbd22019-11-02 22:00:15 +0100267 hi VeryBlue ctermfg=blue guifg=blue
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200268 /666
269 call matchadd('ErrorMsg', '111')
Bram Moolenaar024dbd22019-11-02 22:00:15 +0100270 call matchadd('VeryBlue', '444')
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200271 call win_execute(winid, "call matchadd('ErrorMsg', '111')")
Bram Moolenaar024dbd22019-11-02 22:00:15 +0100272 call win_execute(winid, "call matchadd('VeryBlue', '555')")
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200273 END
274 call writefile(lines, 'XtestPopupMatches')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200275 let buf = RunVimInTerminal('-S XtestPopupMatches', #{rows: 10})
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200276 call VerifyScreenDump(buf, 'Test_popupwin_matches', {})
277
278 " clean up
279 call StopVimInTerminal(buf)
280 call delete('XtestPopupMatches')
281endfunc
282
Bram Moolenaar399d8982019-06-02 15:34:29 +0200283func Test_popup_all_corners()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200284 CheckScreendump
285
Bram Moolenaar399d8982019-06-02 15:34:29 +0200286 let lines =<< trim END
287 call setline(1, repeat([repeat('-', 60)], 15))
288 set so=0
289 normal 2G3|r#
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200290 let winid1 = popup_create(['first', 'second'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200291 \ line: 'cursor+1',
292 \ col: 'cursor',
293 \ pos: 'topleft',
294 \ border: [],
295 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200296 \ })
Bram Moolenaarb754b5b2019-10-24 19:25:00 +0200297 normal 24|r@
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200298 let winid1 = popup_create(['First', 'SeconD'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200299 \ line: 'cursor+1',
300 \ col: 'cursor',
301 \ pos: 'topright',
302 \ border: [],
303 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200304 \ })
Bram Moolenaarb754b5b2019-10-24 19:25:00 +0200305 normal 9G27|r%
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200306 let winid1 = popup_create(['fiRSt', 'seCOnd'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200307 \ line: 'cursor-1',
308 \ col: 'cursor',
309 \ pos: 'botleft',
310 \ border: [],
311 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200312 \ })
Bram Moolenaarb754b5b2019-10-24 19:25:00 +0200313 normal 48|r&
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200314 let winid1 = popup_create(['FIrsT', 'SEcoND'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200315 \ line: 'cursor-1',
316 \ col: 'cursor',
317 \ pos: 'botright',
318 \ border: [],
319 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200320 \ })
Bram Moolenaarb754b5b2019-10-24 19:25:00 +0200321 normal 1G51|r*
322 let winid1 = popup_create(['one', 'two'], #{
323 \ line: 'cursor-1',
324 \ col: 'cursor',
325 \ pos: 'botleft',
326 \ border: [],
327 \ padding: [],
328 \ })
Bram Moolenaar399d8982019-06-02 15:34:29 +0200329 END
330 call writefile(lines, 'XtestPopupCorners')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200331 let buf = RunVimInTerminal('-S XtestPopupCorners', #{rows: 12})
Bram Moolenaar399d8982019-06-02 15:34:29 +0200332 call VerifyScreenDump(buf, 'Test_popupwin_corners', {})
333
334 " clean up
335 call StopVimInTerminal(buf)
336 call delete('XtestPopupCorners')
337endfunc
338
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100339func Test_popup_nospace()
340 CheckScreendump
341
342 let lines =<< trim END
343 call setline(1, repeat([repeat('-', 60)], 15))
344 set so=0
345
346 " cursor in a line in top half, using "botleft" with popup that
347 " does fit
348 normal 5G2|r@
349 let winid1 = popup_create(['one', 'two'], #{
350 \ line: 'cursor-1',
351 \ col: 'cursor',
352 \ pos: 'botleft',
353 \ border: [],
354 \ })
355 " cursor in a line in top half, using "botleft" with popup that
356 " doesn't fit: gets truncated
357 normal 5G9|r#
358 let winid1 = popup_create(['one', 'two', 'tee'], #{
359 \ line: 'cursor-1',
360 \ col: 'cursor',
361 \ pos: 'botleft',
362 \ posinvert: 0,
363 \ border: [],
364 \ })
365 " cursor in a line in top half, using "botleft" with popup that
366 " doesn't fit and 'posinvert' set: flips to below.
367 normal 5G16|r%
368 let winid1 = popup_create(['one', 'two', 'tee'], #{
369 \ line: 'cursor-1',
370 \ col: 'cursor',
371 \ pos: 'botleft',
372 \ border: [],
373 \ })
374 " cursor in a line in bottom half, using "botleft" with popup that
375 " doesn't fit: does not flip.
376 normal 8G23|r*
377 let winid1 = popup_create(['aaa', 'bbb', 'ccc', 'ddd', 'eee', 'fff'], #{
378 \ line: 'cursor-1',
379 \ col: 'cursor',
380 \ pos: 'botleft',
381 \ border: [],
382 \ })
383
384 " cursor in a line in bottom half, using "topleft" with popup that
385 " does fit
386 normal 8G30|r@
387 let winid1 = popup_create(['one', 'two'], #{
388 \ line: 'cursor+1',
389 \ col: 'cursor',
390 \ pos: 'topleft',
391 \ border: [],
392 \ })
393 " cursor in a line in top half, using "topleft" with popup that
394 " doesn't fit: truncated
395 normal 8G37|r#
396 let winid1 = popup_create(['one', 'two', 'tee'], #{
397 \ line: 'cursor+1',
398 \ col: 'cursor',
399 \ pos: 'topleft',
400 \ posinvert: 0,
401 \ border: [],
402 \ })
403 " cursor in a line in top half, using "topleft" with popup that
Bram Moolenaar5c6b6182019-11-10 17:51:38 +0100404 " doesn't fit and "posinvert" set: flips to above.
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100405 normal 8G44|r%
Bram Moolenaar5c6b6182019-11-10 17:51:38 +0100406 let winid1 = popup_create(['one', 'two', 'tee', 'fou', 'fiv'], #{
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100407 \ line: 'cursor+1',
408 \ col: 'cursor',
409 \ pos: 'topleft',
410 \ border: [],
411 \ })
412 " cursor in a line in top half, using "topleft" with popup that
413 " doesn't fit: does not flip.
414 normal 5G51|r*
415 let winid1 = popup_create(['aaa', 'bbb', 'ccc', 'ddd', 'eee', 'fff'], #{
416 \ line: 'cursor+1',
417 \ col: 'cursor',
418 \ pos: 'topleft',
419 \ border: [],
420 \ })
421 END
422 call writefile(lines, 'XtestPopupNospace')
423 let buf = RunVimInTerminal('-S XtestPopupNospace', #{rows: 12})
424 call VerifyScreenDump(buf, 'Test_popupwin_nospace', {})
425
426 " clean up
427 call StopVimInTerminal(buf)
428 call delete('XtestPopupNospace')
429endfunc
430
Bram Moolenaaref6b9792020-05-13 16:34:15 +0200431func Test_popup_firstline_dump()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200432 CheckScreendump
433
Bram Moolenaar8d241042019-06-12 23:40:01 +0200434 let lines =<< trim END
435 call setline(1, range(1, 20))
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200436 let winid = popup_create(['1111', '222222', '33333', '44', '5', '666666', '77777', '888', '9999999999999999'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200437 \ maxheight: 4,
438 \ firstline: 3,
Bram Moolenaar8d241042019-06-12 23:40:01 +0200439 \ })
440 END
441 call writefile(lines, 'XtestPopupFirstline')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200442 let buf = RunVimInTerminal('-S XtestPopupFirstline', #{rows: 10})
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200443 call VerifyScreenDump(buf, 'Test_popupwin_firstline_1', {})
444
445 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: -1})\<CR>")
446 call term_sendkeys(buf, ":\<CR>")
447 call VerifyScreenDump(buf, 'Test_popupwin_firstline_2', {})
Bram Moolenaar8d241042019-06-12 23:40:01 +0200448
449 " clean up
450 call StopVimInTerminal(buf)
451 call delete('XtestPopupFirstline')
Bram Moolenaaref6b9792020-05-13 16:34:15 +0200452endfunc
Bram Moolenaarae943152019-06-16 22:54:14 +0200453
Bram Moolenaaref6b9792020-05-13 16:34:15 +0200454func Test_popup_firstline()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200455 let winid = popup_create(['1111', '222222', '33333', '44444'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200456 \ maxheight: 2,
457 \ firstline: 3,
Bram Moolenaarae943152019-06-16 22:54:14 +0200458 \ })
459 call assert_equal(3, popup_getoptions(winid).firstline)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200460 call popup_setoptions(winid, #{firstline: 1})
Bram Moolenaarae943152019-06-16 22:54:14 +0200461 call assert_equal(1, popup_getoptions(winid).firstline)
Bram Moolenaar6a124e62019-09-04 18:15:19 +0200462 eval winid->popup_close()
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +0200463
464 let winid = popup_create(['xxx']->repeat(50), #{
465 \ maxheight: 3,
466 \ firstline: 11,
467 \ })
468 redraw
469 call assert_equal(11, popup_getoptions(winid).firstline)
470 call assert_equal(11, popup_getpos(winid).firstline)
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +0200471 " check line() works with popup window
472 call assert_equal(11, line('.', winid))
473 call assert_equal(50, line('$', winid))
474 call assert_equal(0, line('$', 123456))
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +0200475
476 " Normal command changes what is displayed but not "firstline"
477 call win_execute(winid, "normal! \<c-y>")
478 call assert_equal(11, popup_getoptions(winid).firstline)
479 call assert_equal(10, popup_getpos(winid).firstline)
480
481 " Making some property change applies "firstline" again
482 call popup_setoptions(winid, #{line: 4})
483 call assert_equal(11, popup_getoptions(winid).firstline)
484 call assert_equal(11, popup_getpos(winid).firstline)
485
486 " Remove "firstline" property and scroll
487 call popup_setoptions(winid, #{firstline: 0})
488 call win_execute(winid, "normal! \<c-y>")
489 call assert_equal(0, popup_getoptions(winid).firstline)
490 call assert_equal(10, popup_getpos(winid).firstline)
491
492 " Making some property change has no side effect
493 call popup_setoptions(winid, #{line: 3})
494 call assert_equal(0, popup_getoptions(winid).firstline)
495 call assert_equal(10, popup_getpos(winid).firstline)
Bram Moolenaaref6b9792020-05-13 16:34:15 +0200496 call popup_close(winid)
Bram Moolenaarae943152019-06-16 22:54:14 +0200497
Bram Moolenaar30efcf32019-11-03 22:29:38 +0100498 " CTRL-D scrolls down half a page
499 let winid = popup_create(['xxx']->repeat(50), #{
500 \ maxheight: 8,
501 \ })
502 redraw
503 call assert_equal(1, popup_getpos(winid).firstline)
504 call win_execute(winid, "normal! \<C-D>")
505 call assert_equal(5, popup_getpos(winid).firstline)
506 call win_execute(winid, "normal! \<C-D>")
507 call assert_equal(9, popup_getpos(winid).firstline)
508 call win_execute(winid, "normal! \<C-U>")
509 call assert_equal(5, popup_getpos(winid).firstline)
510
511 call win_execute(winid, "normal! \<C-F>")
512 call assert_equal(11, popup_getpos(winid).firstline)
513 call win_execute(winid, "normal! \<C-B>")
514 call assert_equal(5, popup_getpos(winid).firstline)
515
Bram Moolenaarae943152019-06-16 22:54:14 +0200516 call popup_close(winid)
Bram Moolenaar8d241042019-06-12 23:40:01 +0200517endfunc
518
Bram Moolenaar99ca9c42020-09-22 21:55:41 +0200519func Test_popup_firstline_cursorline()
520 let winid = popup_create(['1111', '222222', '33333', '44444'], #{
521 \ maxheight: 2,
522 \ firstline: 3,
523 \ cursorline: 1,
524 \ })
525 call assert_equal(3, popup_getoptions(winid).firstline)
526 call assert_equal(3, getwininfo(winid)[0].topline)
527 call assert_equal(3, getcurpos(winid)[1])
528
529 call popup_close(winid)
530endfunc
531
Bram Moolenaara112f2d2019-09-01 17:38:09 +0200532func Test_popup_noscrolloff()
533 set scrolloff=5
534 let winid = popup_create(['xxx']->repeat(50), #{
535 \ maxheight: 5,
536 \ firstline: 11,
537 \ })
538 redraw
539 call assert_equal(11, popup_getoptions(winid).firstline)
540 call assert_equal(11, popup_getpos(winid).firstline)
541
542 call popup_setoptions(winid, #{firstline: 0})
543 call win_execute(winid, "normal! \<c-y>")
544 call assert_equal(0, popup_getoptions(winid).firstline)
545 call assert_equal(10, popup_getpos(winid).firstline)
546
547 call popup_close(winid)
548endfunc
549
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200550func Test_popup_drag()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200551 CheckScreendump
552
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200553 " create a popup that covers the command line
554 let lines =<< trim END
555 call setline(1, range(1, 20))
Bram Moolenaar356375f2019-08-24 14:46:29 +0200556 split
557 vsplit
558 $wincmd w
559 vsplit
560 1wincmd w
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200561 let winid = popup_create(['1111', '222222', '33333'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200562 \ drag: 1,
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200563 \ resize: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200564 \ border: [],
565 \ line: &lines - 4,
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200566 \ })
567 func Dragit()
568 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
569 endfunc
570 map <silent> <F3> :call test_setmouse(&lines - 4, &columns / 2)<CR>
Bram Moolenaar356375f2019-08-24 14:46:29 +0200571 map <silent> <F4> :call test_setmouse(&lines - 8, &columns / 2 - 20)<CR>
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200572 func Resize()
573 call feedkeys("\<F5>\<LeftMouse>\<F6>\<LeftDrag>\<LeftRelease>", "xt")
574 endfunc
Bram Moolenaar356375f2019-08-24 14:46:29 +0200575 map <silent> <F5> :call test_setmouse(6, 21)<CR>
576 map <silent> <F6> :call test_setmouse(7, 25)<CR>
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200577 END
578 call writefile(lines, 'XtestPopupDrag')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200579 let buf = RunVimInTerminal('-S XtestPopupDrag', #{rows: 10})
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200580 call VerifyScreenDump(buf, 'Test_popupwin_drag_01', {})
581
582 call term_sendkeys(buf, ":call Dragit()\<CR>")
583 call VerifyScreenDump(buf, 'Test_popupwin_drag_02', {})
584
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200585 call term_sendkeys(buf, ":call Resize()\<CR>")
586 call VerifyScreenDump(buf, 'Test_popupwin_drag_03', {})
587
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200588 " clean up
589 call StopVimInTerminal(buf)
590 call delete('XtestPopupDrag')
591endfunc
592
Bram Moolenaar35910f22020-07-12 19:24:10 +0200593func Test_popup_drag_termwin()
594 CheckUnix
595 CheckScreendump
596 CheckFeature terminal
597
598 " create a popup that covers the terminal window
599 let lines =<< trim END
Bram Moolenaar452143c2020-07-15 17:38:21 +0200600 set foldmethod=marker
601 call setline(1, range(100))
602 for nr in range(7)
603 call setline(nr * 12 + 1, "fold {{{")
Bram Moolenaar9d489562020-07-30 20:08:50 +0200604 call setline(nr * 12 + 11, "end }}}")
Bram Moolenaar452143c2020-07-15 17:38:21 +0200605 endfor
606 %foldclose
Bram Moolenaar35910f22020-07-12 19:24:10 +0200607 set shell=/bin/sh noruler
Bram Moolenaar4b2ce122020-11-21 21:41:41 +0100608 unlet $PROMPT_COMMAND
Bram Moolenaara4dc6f92020-07-12 19:52:36 +0200609 let $PS1 = 'vim> '
Bram Moolenaar452143c2020-07-15 17:38:21 +0200610 terminal ++rows=4
Bram Moolenaar35910f22020-07-12 19:24:10 +0200611 $wincmd w
612 let winid = popup_create(['1111', '2222'], #{
613 \ drag: 1,
614 \ resize: 1,
615 \ border: [],
616 \ line: 3,
617 \ })
Bram Moolenaar452143c2020-07-15 17:38:21 +0200618 func DragitLeft()
Bram Moolenaar35910f22020-07-12 19:24:10 +0200619 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
620 endfunc
Bram Moolenaar452143c2020-07-15 17:38:21 +0200621 func DragitDown()
622 call feedkeys("\<F4>\<LeftMouse>\<F5>\<LeftDrag>\<LeftRelease>", "xt")
623 endfunc
624 func DragitDownLeft()
625 call feedkeys("\<F5>\<LeftMouse>\<F6>\<LeftDrag>\<LeftRelease>", "xt")
626 endfunc
Bram Moolenaar35910f22020-07-12 19:24:10 +0200627 map <silent> <F3> :call test_setmouse(3, &columns / 2)<CR>
628 map <silent> <F4> :call test_setmouse(3, &columns / 2 - 20)<CR>
Bram Moolenaar452143c2020-07-15 17:38:21 +0200629 map <silent> <F5> :call test_setmouse(12, &columns / 2)<CR>
630 map <silent> <F6> :call test_setmouse(12, &columns / 2 - 20)<CR>
Bram Moolenaar35910f22020-07-12 19:24:10 +0200631 END
632 call writefile(lines, 'XtestPopupTerm')
Bram Moolenaar452143c2020-07-15 17:38:21 +0200633 let buf = RunVimInTerminal('-S XtestPopupTerm', #{rows: 16})
Bram Moolenaar35910f22020-07-12 19:24:10 +0200634 call VerifyScreenDump(buf, 'Test_popupwin_term_01', {})
635
Bram Moolenaar452143c2020-07-15 17:38:21 +0200636 call term_sendkeys(buf, ":call DragitLeft()\<CR>")
Bram Moolenaar35910f22020-07-12 19:24:10 +0200637 call VerifyScreenDump(buf, 'Test_popupwin_term_02', {})
638
Bram Moolenaar452143c2020-07-15 17:38:21 +0200639 call term_sendkeys(buf, ":call DragitDown()\<CR>")
640 call VerifyScreenDump(buf, 'Test_popupwin_term_03', {})
641
642 call term_sendkeys(buf, ":call DragitDownLeft()\<CR>")
643 call VerifyScreenDump(buf, 'Test_popupwin_term_04', {})
644
Bram Moolenaar35910f22020-07-12 19:24:10 +0200645 " clean up
646 call StopVimInTerminal(buf)
647 call delete('XtestPopupTerm')
648endfunc
649
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200650func Test_popup_close_with_mouse()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200651 CheckScreendump
652
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200653 let lines =<< trim END
654 call setline(1, range(1, 20))
655 " With border, can click on X
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200656 let winid = popup_create('foobar', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200657 \ close: 'button',
658 \ border: [],
659 \ line: 1,
660 \ col: 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200661 \ })
662 func CloseMsg(id, result)
663 echomsg 'Popup closed with ' .. a:result
664 endfunc
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200665 let winid = popup_create('notification', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200666 \ close: 'click',
667 \ line: 3,
668 \ col: 15,
669 \ callback: 'CloseMsg',
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200670 \ })
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200671 let winid = popup_create('no border here', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200672 \ close: 'button',
673 \ line: 5,
674 \ col: 3,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200675 \ })
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200676 let winid = popup_create('only padding', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200677 \ close: 'button',
678 \ padding: [],
679 \ line: 5,
680 \ col: 23,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200681 \ })
682 func CloseWithX()
683 call feedkeys("\<F3>\<LeftMouse>\<LeftRelease>", "xt")
684 endfunc
685 map <silent> <F3> :call test_setmouse(1, len('foobar') + 2)<CR>
686 func CloseWithClick()
687 call feedkeys("\<F4>\<LeftMouse>\<LeftRelease>", "xt")
688 endfunc
689 map <silent> <F4> :call test_setmouse(3, 17)<CR>
Bram Moolenaarf6396232019-08-24 19:36:00 +0200690 func CreateWithMenuFilter()
691 let winid = popup_create('barfoo', #{
692 \ close: 'button',
693 \ filter: 'popup_filter_menu',
694 \ border: [],
695 \ line: 1,
696 \ col: 40,
697 \ })
698 endfunc
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200699 END
700 call writefile(lines, 'XtestPopupClose')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200701 let buf = RunVimInTerminal('-S XtestPopupClose', #{rows: 10})
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200702 call VerifyScreenDump(buf, 'Test_popupwin_close_01', {})
703
704 call term_sendkeys(buf, ":call CloseWithX()\<CR>")
705 call VerifyScreenDump(buf, 'Test_popupwin_close_02', {})
706
707 call term_sendkeys(buf, ":call CloseWithClick()\<CR>")
708 call VerifyScreenDump(buf, 'Test_popupwin_close_03', {})
709
Bram Moolenaarf6396232019-08-24 19:36:00 +0200710 call term_sendkeys(buf, ":call CreateWithMenuFilter()\<CR>")
711 call VerifyScreenDump(buf, 'Test_popupwin_close_04', {})
712
713 " We have to send the actual mouse code, feedkeys() would be caught the
714 " filter.
715 call term_sendkeys(buf, "\<Esc>[<0;47;1M")
716 call VerifyScreenDump(buf, 'Test_popupwin_close_05', {})
717
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200718 " clean up
719 call StopVimInTerminal(buf)
720 call delete('XtestPopupClose')
721endfunction
722
Bram Moolenaar7b3d9392019-10-16 22:17:07 +0200723func Test_popup_menu_wrap()
724 CheckScreendump
725
726 let lines =<< trim END
727 call setline(1, range(1, 20))
728 call popup_create([
729 \ 'one',
730 \ 'asdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfas',
731 \ 'three',
732 \ 'four',
733 \ ], #{
734 \ pos: "botleft",
735 \ border: [],
736 \ padding: [0,1,0,1],
737 \ maxheight: 3,
738 \ cursorline: 1,
739 \ filter: 'popup_filter_menu',
740 \ })
741 END
742 call writefile(lines, 'XtestPopupWrap')
743 let buf = RunVimInTerminal('-S XtestPopupWrap', #{rows: 10})
744 call VerifyScreenDump(buf, 'Test_popupwin_wrap_1', {})
745
746 call term_sendkeys(buf, "jj")
747 call VerifyScreenDump(buf, 'Test_popupwin_wrap_2', {})
748
749 " clean up
750 call term_sendkeys(buf, "\<Esc>")
751 call StopVimInTerminal(buf)
752 call delete('XtestPopupWrap')
753endfunction
754
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200755func Test_popup_with_mask()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200756 CheckScreendump
757
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200758 let lines =<< trim END
Bram Moolenaarba45f1f2019-07-03 22:50:41 +0200759 call setline(1, repeat([join(range(1, 42), '')], 13))
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200760 hi PopupColor ctermbg=lightgrey
761 let winid = popup_create([
762 \ 'some text',
763 \ 'another line',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200764 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200765 \ line: 1,
766 \ col: 10,
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100767 \ posinvert: 0,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200768 \ wrap: 0,
769 \ fixed: 1,
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +0200770 \ scrollbar: v:false,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200771 \ zindex: 90,
772 \ padding: [],
773 \ highlight: 'PopupColor',
774 \ mask: [[1,1,1,1], [-5,-1,4,4], [7,9,2,3], [2,4,3,3]]})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200775 call popup_create([
776 \ 'xxxxxxxxx',
777 \ 'yyyyyyyyy',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200778 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200779 \ line: 3,
780 \ col: 18,
781 \ zindex: 20})
Bram Moolenaarba45f1f2019-07-03 22:50:41 +0200782 let winidb = popup_create([
783 \ 'just one line',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200784 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200785 \ line: 7,
786 \ col: 10,
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100787 \ posinvert: 0,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200788 \ wrap: 0,
789 \ fixed: 1,
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +0200790 \ scrollbar: v:false,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200791 \ close: 'button',
792 \ zindex: 90,
793 \ padding: [],
794 \ border: [],
795 \ mask: [[1,2,1,1], [-5,-1,4,4], [7,9,2,3], [3,5,5,5],[-7,-4,5,5]]})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200796 END
797 call writefile(lines, 'XtestPopupMask')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200798 let buf = RunVimInTerminal('-S XtestPopupMask', #{rows: 13})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200799 call VerifyScreenDump(buf, 'Test_popupwin_mask_1', {})
800
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200801 call term_sendkeys(buf, ":call popup_move(winid, #{col: 11, line: 2})\<CR>")
802 call term_sendkeys(buf, ":call popup_move(winidb, #{col: 12})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200803 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200804 call VerifyScreenDump(buf, 'Test_popupwin_mask_2', {})
805
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200806 call term_sendkeys(buf, ":call popup_move(winid, #{col: 65, line: 2})\<CR>")
807 call term_sendkeys(buf, ":call popup_move(winidb, #{col: 63})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200808 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaard529ba52019-07-02 23:13:53 +0200809 call VerifyScreenDump(buf, 'Test_popupwin_mask_3', {})
810
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200811 call term_sendkeys(buf, ":call popup_move(winid, #{pos: 'topright', col: 12, line: 2})\<CR>")
812 call term_sendkeys(buf, ":call popup_move(winidb, #{pos: 'topright', col: 12})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200813 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaard529ba52019-07-02 23:13:53 +0200814 call VerifyScreenDump(buf, 'Test_popupwin_mask_4', {})
815
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200816 call term_sendkeys(buf, ":call popup_move(winid, #{pos: 'topright', col: 12, line: 11})\<CR>")
817 call term_sendkeys(buf, ":call popup_move(winidb, #{pos: 'topleft', col: 42, line: 11})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200818 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaarb4207472019-07-12 16:05:45 +0200819 call VerifyScreenDump(buf, 'Test_popupwin_mask_5', {})
820
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200821 " clean up
822 call StopVimInTerminal(buf)
823 call delete('XtestPopupMask')
Bram Moolenaar4012d262020-12-29 11:57:46 +0100824
825 " this was causing a crash
826 call popup_create('test', #{mask: [[0, 0, 0, 0]]})
827 call popup_clear()
Bram Moolenaar10ccfb22021-02-13 21:31:18 +0100828
829 " this was causing an internal error
830 enew
831 set nowrap
832 call repeat('x', &columns)->setline(1)
833 call prop_type_add('textprop', {})
834 call prop_add(1, 1, #{length: &columns, type: 'textprop'})
835 vsplit
836 let opts = popup_create('', #{textprop: 'textprop'})
837 \ ->popup_getoptions()
838 \ ->extend(#{mask: [[1, 1, 1, 1]]})
839 call popup_create('', opts)
840 redraw
841
842 close!
843 bwipe!
844 call prop_type_delete('textprop')
845 call popup_clear()
846 set wrap&
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200847endfunc
848
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200849func Test_popup_select()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200850 CheckScreendump
851 CheckFeature clipboard_working
852
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200853 " create a popup with some text to be selected
854 let lines =<< trim END
Bram Moolenaar1755ec42019-06-15 13:13:54 +0200855 set clipboard=autoselect
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200856 call setline(1, range(1, 20))
Bram Moolenaar4dd751b2019-08-17 19:10:53 +0200857 let winid = popup_create(['the word', 'some more', 'several words here', 'invisible', '5', '6', '7'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200858 \ drag: 1,
859 \ border: [],
860 \ line: 3,
861 \ col: 10,
Bram Moolenaar4dd751b2019-08-17 19:10:53 +0200862 \ maxheight: 3,
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200863 \ })
864 func Select1()
865 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
866 endfunc
867 map <silent> <F3> :call test_setmouse(4, 15)<CR>
868 map <silent> <F4> :call test_setmouse(6, 23)<CR>
869 END
870 call writefile(lines, 'XtestPopupSelect')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200871 let buf = RunVimInTerminal('-S XtestPopupSelect', #{rows: 10})
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200872 call term_sendkeys(buf, ":call Select1()\<CR>")
873 call VerifyScreenDump(buf, 'Test_popupwin_select_01', {})
874
875 call term_sendkeys(buf, ":call popup_close(winid)\<CR>")
876 call term_sendkeys(buf, "\"*p")
Bram Moolenaar8ccabf62019-07-12 18:12:51 +0200877 " clean the command line, sometimes it still shows a command
878 call term_sendkeys(buf, ":\<esc>")
879
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200880 call VerifyScreenDump(buf, 'Test_popupwin_select_02', {})
881
882 " clean up
883 call StopVimInTerminal(buf)
884 call delete('XtestPopupSelect')
885endfunc
886
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200887func Test_popup_in_tab()
888 " default popup is local to tab, not visible when in other tab
889 let winid = popup_create("text", {})
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200890 let bufnr = winbufnr(winid)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200891 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200892 call assert_equal(0, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200893 tabnew
894 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200895 call assert_equal(1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200896 quit
897 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200898
899 call assert_equal(1, bufexists(bufnr))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200900 call popup_clear()
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200901 " buffer is gone now
902 call assert_equal(0, bufexists(bufnr))
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200903
904 " global popup is visible in any tab
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200905 let winid = popup_create("text", #{tabpage: -1})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200906 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200907 call assert_equal(-1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200908 tabnew
909 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200910 call assert_equal(-1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200911 quit
912 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200913 call popup_clear()
Bram Moolenaara3fce622019-06-20 02:31:49 +0200914
915 " create popup in other tab
916 tabnew
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200917 let winid = popup_create("text", #{tabpage: 1})
Bram Moolenaara3fce622019-06-20 02:31:49 +0200918 call assert_equal(0, popup_getpos(winid).visible)
919 call assert_equal(1, popup_getoptions(winid).tabpage)
920 quit
921 call assert_equal(1, popup_getpos(winid).visible)
922 call assert_equal(0, popup_getoptions(winid).tabpage)
923 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200924endfunc
925
926func Test_popup_valid_arguments()
Bram Moolenaaref6b9792020-05-13 16:34:15 +0200927 call assert_equal(0, len(popup_list()))
928
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200929 " Zero value is like the property wasn't there
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200930 let winid = popup_create("text", #{col: 0})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200931 let pos = popup_getpos(winid)
932 call assert_inrange(&columns / 2 - 1, &columns / 2 + 1, pos.col)
Bram Moolenaaref6b9792020-05-13 16:34:15 +0200933 call assert_equal([winid], popup_list())
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200934 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200935
936 " using cursor column has minimum value of 1
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200937 let winid = popup_create("text", #{col: 'cursor-100'})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200938 let pos = popup_getpos(winid)
939 call assert_equal(1, pos.col)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200940 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200941
942 " center
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200943 let winid = popup_create("text", #{pos: 'center'})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200944 let pos = popup_getpos(winid)
945 let around = (&columns - pos.width) / 2
946 call assert_inrange(around - 1, around + 1, pos.col)
947 let around = (&lines - pos.height) / 2
948 call assert_inrange(around - 1, around + 1, pos.line)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200949 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200950endfunc
951
952func Test_popup_invalid_arguments()
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +0200953 call assert_fails('call popup_create(666, {})', 'E86:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200954 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200955 call assert_fails('call popup_create("text", "none")', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200956 call popup_clear()
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200957 call assert_fails('call popup_create(test_null_string(), {})', 'E450:')
958 call assert_fails('call popup_create(test_null_list(), {})', 'E450:')
959 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200960
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200961 call assert_fails('call popup_create("text", #{col: "xxx"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200962 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200963 call assert_fails('call popup_create("text", #{col: "cursor8"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200964 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200965 call assert_fails('call popup_create("text", #{col: "cursor+x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200966 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200967 call assert_fails('call popup_create("text", #{col: "cursor+8x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200968 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200969
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200970 call assert_fails('call popup_create("text", #{line: "xxx"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200971 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200972 call assert_fails('call popup_create("text", #{line: "cursor8"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200973 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200974 call assert_fails('call popup_create("text", #{line: "cursor+x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200975 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200976 call assert_fails('call popup_create("text", #{line: "cursor+8x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200977 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200978
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200979 call assert_fails('call popup_create("text", #{pos: "there"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200980 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200981 call assert_fails('call popup_create("text", #{padding: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200982 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200983 call assert_fails('call popup_create("text", #{border: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200984 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200985 call assert_fails('call popup_create("text", #{borderhighlight: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200986 call popup_clear()
Bram Moolenaar403d0902019-07-17 21:37:32 +0200987 call assert_fails('call popup_create("text", #{borderhighlight: test_null_list()})', 'E714:')
988 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200989 call assert_fails('call popup_create("text", #{borderchars: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200990 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200991
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200992 call assert_fails('call popup_create([#{text: "text"}, 666], {})', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200993 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200994 call assert_fails('call popup_create([#{text: "text", props: "none"}], {})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200995 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200996 call assert_fails('call popup_create([#{text: "text", props: ["none"]}], {})', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200997 call popup_clear()
Bram Moolenaarb0992022020-01-30 14:55:42 +0100998 call assert_fails('call popup_create([#{text: "text", props: range(3)}], {})', 'E715:')
999 call popup_clear()
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +02001000 call assert_fails('call popup_create("text", #{mask: ["asdf"]})', 'E475:')
1001 call popup_clear()
Bram Moolenaarb0992022020-01-30 14:55:42 +01001002 call assert_fails('call popup_create("text", #{mask: range(5)})', 'E475:')
1003 call popup_clear()
1004 call popup_create("text", #{mask: [range(4)]})
1005 call popup_clear()
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +02001006 call assert_fails('call popup_create("text", #{mask: test_null_list()})', 'E475:')
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001007 call assert_fails('call popup_create("text", #{mapping: []})', 'E745:')
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +02001008 call popup_clear()
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02001009 call assert_fails('call popup_create("text", #{tabpage : 4})', 'E997:')
1010 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001011endfunc
1012
Bram Moolenaareea16992019-05-31 17:34:48 +02001013func Test_win_execute_closing_curwin()
1014 split
1015 let winid = popup_create('some text', {})
Bram Moolenaare2e40752020-09-04 21:18:46 +02001016 call assert_fails('call win_execute(winid, winnr() .. "close")', 'E994:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001017 call popup_clear()
Bram Moolenaar49540192019-12-11 19:34:54 +01001018
1019 let winid = popup_create('some text', {})
Bram Moolenaare2e40752020-09-04 21:18:46 +02001020 call assert_fails('call win_execute(winid, printf("normal! :\<C-u>call popup_close(%d)\<CR>", winid))', 'E994:')
Bram Moolenaar49540192019-12-11 19:34:54 +01001021 call popup_clear()
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001022endfunc
1023
1024func Test_win_execute_not_allowed()
1025 let winid = popup_create('some text', {})
1026 call assert_fails('call win_execute(winid, "split")', 'E994:')
1027 call assert_fails('call win_execute(winid, "vsplit")', 'E994:')
1028 call assert_fails('call win_execute(winid, "close")', 'E994:')
1029 call assert_fails('call win_execute(winid, "bdelete")', 'E994:')
Bram Moolenaar2d247842019-06-01 17:06:25 +02001030 call assert_fails('call win_execute(winid, "bwipe!")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001031 call assert_fails('call win_execute(winid, "tabnew")', 'E994:')
1032 call assert_fails('call win_execute(winid, "tabnext")', 'E994:')
1033 call assert_fails('call win_execute(winid, "next")', 'E994:')
1034 call assert_fails('call win_execute(winid, "rewind")', 'E994:')
Bram Moolenaar3a2505c2020-03-09 16:40:41 +01001035 call assert_fails('call win_execute(winid, "pedit filename")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001036 call assert_fails('call win_execute(winid, "buf")', 'E994:')
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01001037 call assert_fails('call win_execute(winid, "bnext")', 'E994:')
1038 call assert_fails('call win_execute(winid, "bprev")', 'E994:')
1039 call assert_fails('call win_execute(winid, "bfirst")', 'E994:')
1040 call assert_fails('call win_execute(winid, "blast")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001041 call assert_fails('call win_execute(winid, "edit")', 'E994:')
1042 call assert_fails('call win_execute(winid, "enew")', 'E994:')
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +02001043 call assert_fails('call win_execute(winid, "help")', 'E994:')
1044 call assert_fails('call win_execute(winid, "1only")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001045 call assert_fails('call win_execute(winid, "wincmd x")', 'E994:')
1046 call assert_fails('call win_execute(winid, "wincmd w")', 'E994:')
1047 call assert_fails('call win_execute(winid, "wincmd t")', 'E994:')
1048 call assert_fails('call win_execute(winid, "wincmd b")', 'E994:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001049 call popup_clear()
Bram Moolenaareea16992019-05-31 17:34:48 +02001050endfunc
1051
Bram Moolenaar402502d2019-05-30 22:07:36 +02001052func Test_popup_with_wrap()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001053 CheckScreendump
1054
Bram Moolenaar402502d2019-05-30 22:07:36 +02001055 let lines =<< trim END
1056 call setline(1, range(1, 100))
1057 let winid = popup_create(
1058 \ 'a long line that wont fit',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001059 \ #{line: 3, col: 20, maxwidth: 10, wrap: 1})
Bram Moolenaar402502d2019-05-30 22:07:36 +02001060 END
1061 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001062 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar402502d2019-05-30 22:07:36 +02001063 call VerifyScreenDump(buf, 'Test_popupwin_wrap', {})
1064
1065 " clean up
1066 call StopVimInTerminal(buf)
1067 call delete('XtestPopup')
1068endfunc
1069
1070func Test_popup_without_wrap()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001071 CheckScreendump
1072
Bram Moolenaar402502d2019-05-30 22:07:36 +02001073 let lines =<< trim END
1074 call setline(1, range(1, 100))
1075 let winid = popup_create(
1076 \ 'a long line that wont fit',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001077 \ #{line: 3, col: 20, maxwidth: 10, wrap: 0})
Bram Moolenaar402502d2019-05-30 22:07:36 +02001078 END
1079 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001080 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar402502d2019-05-30 22:07:36 +02001081 call VerifyScreenDump(buf, 'Test_popupwin_nowrap', {})
1082
1083 " clean up
1084 call StopVimInTerminal(buf)
1085 call delete('XtestPopup')
1086endfunc
1087
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001088func Test_popup_with_showbreak()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001089 CheckScreendump
1090
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001091 let lines =<< trim END
1092 set showbreak=>>\
1093 call setline(1, range(1, 20))
1094 let winid = popup_dialog(
Bram Moolenaar8ae54372019-09-15 18:11:16 +02001095 \ 'a long line here that wraps',
1096 \ #{filter: 'popup_filter_yesno',
1097 \ maxwidth: 12})
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001098 END
1099 call writefile(lines, 'XtestPopupShowbreak')
1100 let buf = RunVimInTerminal('-S XtestPopupShowbreak', #{rows: 10})
1101 call VerifyScreenDump(buf, 'Test_popupwin_showbreak', {})
1102
1103 " clean up
1104 call term_sendkeys(buf, "y")
1105 call StopVimInTerminal(buf)
1106 call delete('XtestPopupShowbreak')
1107endfunc
1108
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001109func Test_popup_time()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001110 CheckFeature timers
1111
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001112 topleft vnew
1113 call setline(1, 'hello')
1114
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001115 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001116 \ line: 1,
1117 \ col: 1,
1118 \ minwidth: 20,
1119 \ time: 500,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001120 \})
1121 redraw
1122 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1123 call assert_equal('world', line)
1124
Bram Moolenaarb4f06282019-07-12 21:07:54 +02001125 call assert_equal(winid, popup_locate(1, 1))
1126 call assert_equal(winid, popup_locate(1, 20))
1127 call assert_equal(0, popup_locate(1, 21))
1128 call assert_equal(0, popup_locate(2, 1))
1129
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001130 sleep 700m
Bram Moolenaar35d5af62019-05-26 20:44:10 +02001131 redraw
Bram Moolenaar196b4662019-09-06 21:34:30 +02001132 let line = join(map(range(1, 5), '1->screenstring(v:val)'), '')
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001133 call assert_equal('hello', line)
1134
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001135 call popup_create('on the command line', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001136 \ line: &lines,
1137 \ col: 10,
1138 \ minwidth: 20,
1139 \ time: 500,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001140 \})
1141 redraw
1142 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
1143 call assert_match('.*on the command line.*', line)
1144
1145 sleep 700m
1146 redraw
1147 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
1148 call assert_notmatch('.*on the command line.*', line)
1149
1150 bwipe!
1151endfunc
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001152
1153func Test_popup_hide()
1154 topleft vnew
1155 call setline(1, 'hello')
1156
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001157 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001158 \ line: 1,
1159 \ col: 1,
1160 \ minwidth: 20,
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001161 \})
1162 redraw
1163 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1164 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001165 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001166 " buffer is still listed and active
1167 call assert_match(winbufnr(winid) .. 'u a.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001168
1169 call popup_hide(winid)
1170 redraw
1171 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1172 call assert_equal('hello', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001173 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001174 " buffer is still listed but hidden
Bram Moolenaarca7c0782020-01-14 20:42:48 +01001175 call assert_match(winbufnr(winid) .. 'u a.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001176
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001177 eval winid->popup_show()
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001178 redraw
1179 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1180 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001181 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001182
1183
1184 call popup_close(winid)
1185 redraw
1186 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1187 call assert_equal('hello', line)
1188
1189 " error is given for existing non-popup window
1190 call assert_fails('call popup_hide(win_getid())', 'E993:')
1191
1192 " no error non-existing window
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001193 eval 1234234->popup_hide()
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001194 call popup_show(41234234)
1195
1196 bwipe!
1197endfunc
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001198
1199func Test_popup_move()
1200 topleft vnew
1201 call setline(1, 'hello')
1202
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001203 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001204 \ line: 1,
1205 \ col: 1,
1206 \ minwidth: 20,
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001207 \})
1208 redraw
1209 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
1210 call assert_equal('world ', line)
1211
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001212 call popup_move(winid, #{line: 2, col: 2})
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001213 redraw
1214 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
1215 call assert_equal('hello ', line)
1216 let line = join(map(range(1, 6), 'screenstring(2, v:val)'), '')
1217 call assert_equal('~world', line)
1218
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001219 eval winid->popup_move(#{line: 1})
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001220 redraw
1221 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
1222 call assert_equal('hworld', line)
1223
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02001224 call assert_fails('call popup_move(winid, [])', 'E715:')
1225 call assert_fails('call popup_move(winid, test_null_dict())', 'E715:')
1226
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001227 call popup_close(winid)
1228
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02001229 call assert_equal(0, popup_move(-1, {}))
1230
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001231 bwipe!
1232endfunc
Bram Moolenaarbc133542019-05-29 20:26:46 +02001233
Bram Moolenaar402502d2019-05-30 22:07:36 +02001234func Test_popup_getpos()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001235 let winid = popup_create('hello', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001236 \ line: 2,
1237 \ col: 3,
1238 \ minwidth: 10,
1239 \ minheight: 11,
Bram Moolenaarbc133542019-05-29 20:26:46 +02001240 \})
1241 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +02001242 let res = popup_getpos(winid)
Bram Moolenaarbc133542019-05-29 20:26:46 +02001243 call assert_equal(2, res.line)
1244 call assert_equal(3, res.col)
1245 call assert_equal(10, res.width)
1246 call assert_equal(11, res.height)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001247 call assert_equal(1, res.visible)
Bram Moolenaarbc133542019-05-29 20:26:46 +02001248
1249 call popup_close(winid)
1250endfunc
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001251
1252func Test_popup_width_longest()
1253 let tests = [
1254 \ [['hello', 'this', 'window', 'displays', 'all of its text'], 15],
1255 \ [['hello', 'this', 'window', 'all of its text', 'displays'], 15],
1256 \ [['hello', 'this', 'all of its text', 'window', 'displays'], 15],
1257 \ [['hello', 'all of its text', 'this', 'window', 'displays'], 15],
1258 \ [['all of its text', 'hello', 'this', 'window', 'displays'], 15],
1259 \ ]
1260
1261 for test in tests
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001262 let winid = popup_create(test[0], #{line: 2, col: 3})
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001263 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +02001264 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001265 call assert_equal(test[1], position.width)
1266 call popup_close(winid)
1267 endfor
1268endfunc
1269
1270func Test_popup_wraps()
1271 let tests = [
1272 \ ['nowrap', 6, 1],
1273 \ ['a line that wraps once', 12, 2],
1274 \ ['a line that wraps two times', 12, 3],
1275 \ ]
1276 for test in tests
1277 let winid = popup_create(test[0],
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001278 \ #{line: 2, col: 3, maxwidth: 12})
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001279 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +02001280 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001281 call assert_equal(test[1], position.width)
1282 call assert_equal(test[2], position.height)
1283
1284 call popup_close(winid)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001285 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001286 endfor
1287endfunc
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001288
1289func Test_popup_getoptions()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001290 let winid = popup_create('hello', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001291 \ line: 2,
1292 \ col: 3,
1293 \ minwidth: 10,
1294 \ minheight: 11,
1295 \ maxwidth: 20,
1296 \ maxheight: 21,
1297 \ zindex: 100,
1298 \ time: 5000,
1299 \ fixed: 1
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001300 \})
1301 redraw
1302 let res = popup_getoptions(winid)
1303 call assert_equal(2, res.line)
1304 call assert_equal(3, res.col)
1305 call assert_equal(10, res.minwidth)
1306 call assert_equal(11, res.minheight)
1307 call assert_equal(20, res.maxwidth)
1308 call assert_equal(21, res.maxheight)
1309 call assert_equal(100, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001310 call assert_equal(1, res.fixed)
Bram Moolenaarb8350ab2019-08-04 17:59:49 +02001311 call assert_equal(1, res.mapping)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001312 if has('timers')
1313 call assert_equal(5000, res.time)
1314 endif
1315 call popup_close(winid)
1316
1317 let winid = popup_create('hello', {})
1318 redraw
1319 let res = popup_getoptions(winid)
1320 call assert_equal(0, res.line)
1321 call assert_equal(0, res.col)
1322 call assert_equal(0, res.minwidth)
1323 call assert_equal(0, res.minheight)
1324 call assert_equal(0, res.maxwidth)
1325 call assert_equal(0, res.maxheight)
1326 call assert_equal(50, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001327 call assert_equal(0, res.fixed)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001328 if has('timers')
1329 call assert_equal(0, res.time)
1330 endif
1331 call popup_close(winid)
1332 call assert_equal({}, popup_getoptions(winid))
1333endfunc
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001334
1335func Test_popup_option_values()
1336 new
1337 " window-local
1338 setlocal number
1339 setlocal nowrap
1340 " buffer-local
1341 setlocal omnifunc=Something
1342 " global/buffer-local
1343 setlocal path=/there
1344 " global/window-local
Bram Moolenaara112f2d2019-09-01 17:38:09 +02001345 setlocal statusline=2
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001346
1347 let winid = popup_create('hello', {})
1348 call assert_equal(0, getwinvar(winid, '&number'))
1349 call assert_equal(1, getwinvar(winid, '&wrap'))
1350 call assert_equal('', getwinvar(winid, '&omnifunc'))
1351 call assert_equal(&g:path, getwinvar(winid, '&path'))
Bram Moolenaara112f2d2019-09-01 17:38:09 +02001352 call assert_equal(&g:statusline, getwinvar(winid, '&statusline'))
1353
1354 " 'scrolloff' is reset to zero
1355 call assert_equal(5, &scrolloff)
1356 call assert_equal(0, getwinvar(winid, '&scrolloff'))
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001357
1358 call popup_close(winid)
1359 bwipe
1360endfunc
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001361
1362func Test_popup_atcursor()
1363 topleft vnew
1364 call setline(1, [
1365 \ 'xxxxxxxxxxxxxxxxx',
1366 \ 'xxxxxxxxxxxxxxxxx',
1367 \ 'xxxxxxxxxxxxxxxxx',
1368 \])
1369
1370 call cursor(2, 2)
1371 redraw
1372 let winid = popup_atcursor('vim', {})
1373 redraw
1374 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
1375 call assert_equal('xvimxxxxxxxxxxxxx', line)
1376 call popup_close(winid)
1377
1378 call cursor(3, 4)
1379 redraw
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001380 let winid = 'vim'->popup_atcursor({})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001381 redraw
1382 let line = join(map(range(1, 17), 'screenstring(2, v:val)'), '')
1383 call assert_equal('xxxvimxxxxxxxxxxx', line)
1384 call popup_close(winid)
1385
1386 call cursor(1, 1)
1387 redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001388 let winid = popup_create('vim', #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001389 \ line: 'cursor+2',
1390 \ col: 'cursor+1',
1391 \})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001392 redraw
1393 let line = join(map(range(1, 17), 'screenstring(3, v:val)'), '')
1394 call assert_equal('xvimxxxxxxxxxxxxx', line)
1395 call popup_close(winid)
1396
1397 call cursor(3, 3)
1398 redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001399 let winid = popup_create('vim', #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001400 \ line: 'cursor-2',
1401 \ col: 'cursor-1',
1402 \})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001403 redraw
1404 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
1405 call assert_equal('xvimxxxxxxxxxxxxx', line)
1406 call popup_close(winid)
1407
Bram Moolenaar402502d2019-05-30 22:07:36 +02001408 " just enough room above
1409 call cursor(3, 3)
1410 redraw
1411 let winid = popup_atcursor(['vim', 'is great'], {})
1412 redraw
1413 let pos = popup_getpos(winid)
1414 call assert_equal(1, pos.line)
1415 call popup_close(winid)
1416
1417 " not enough room above, popup goes below the cursor
1418 call cursor(3, 3)
1419 redraw
1420 let winid = popup_atcursor(['vim', 'is', 'great'], {})
1421 redraw
1422 let pos = popup_getpos(winid)
1423 call assert_equal(4, pos.line)
1424 call popup_close(winid)
1425
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001426 " cursor in first line, popup in line 2
1427 call cursor(1, 1)
1428 redraw
1429 let winid = popup_atcursor(['vim', 'is', 'great'], {})
1430 redraw
1431 let pos = popup_getpos(winid)
1432 call assert_equal(2, pos.line)
1433 call popup_close(winid)
1434
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001435 bwipe!
1436endfunc
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001437
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001438func Test_popup_atcursor_pos()
1439 CheckScreendump
1440
1441 let lines =<< trim END
1442 call setline(1, repeat([repeat('-', 60)], 15))
1443 set so=0
1444
1445 normal 9G3|r#
1446 let winid1 = popup_atcursor(['first', 'second'], #{
1447 \ moved: [0, 0, 0],
1448 \ })
1449 normal 9G21|r&
1450 let winid1 = popup_atcursor(['FIrsT', 'SEcoND'], #{
1451 \ pos: 'botright',
1452 \ moved: [0, 0, 0],
1453 \ })
1454 normal 3G27|r%
1455 let winid1 = popup_atcursor(['fiRSt', 'seCOnd'], #{
1456 \ pos: 'topleft',
1457 \ moved: [0, 0, 0],
1458 \ })
1459 normal 3G45|r@
1460 let winid1 = popup_atcursor(['First', 'SeconD'], #{
1461 \ pos: 'topright',
Bram Moolenaarb0992022020-01-30 14:55:42 +01001462 \ moved: range(3),
1463 \ mousemoved: range(3),
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001464 \ })
1465 END
1466 call writefile(lines, 'XtestPopupAtcursorPos')
1467 let buf = RunVimInTerminal('-S XtestPopupAtcursorPos', #{rows: 12})
1468 call VerifyScreenDump(buf, 'Test_popupwin_atcursor_pos', {})
1469
1470 " clean up
1471 call StopVimInTerminal(buf)
1472 call delete('XtestPopupAtcursorPos')
1473endfunc
1474
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001475func Test_popup_beval()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001476 CheckScreendump
Bram Moolenaar1e82a782019-09-21 22:57:06 +02001477 CheckFeature balloon_eval_term
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001478
1479 let lines =<< trim END
1480 call setline(1, range(1, 20))
1481 call setline(5, 'here is some text to hover over')
1482 set balloonevalterm
1483 set balloonexpr=BalloonExpr()
1484 set balloondelay=100
1485 func BalloonExpr()
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001486 let s:winid = [v:beval_text]->popup_beval({})
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001487 return ''
1488 endfunc
1489 func Hover()
1490 call test_setmouse(5, 15)
1491 call feedkeys("\<MouseMove>\<Ignore>", "xt")
1492 sleep 100m
1493 endfunc
1494 func MoveOntoPopup()
1495 call test_setmouse(4, 17)
1496 call feedkeys("\<F4>\<MouseMove>\<Ignore>", "xt")
1497 endfunc
1498 func MoveAway()
1499 call test_setmouse(5, 13)
1500 call feedkeys("\<F5>\<MouseMove>\<Ignore>", "xt")
1501 endfunc
1502 END
1503 call writefile(lines, 'XtestPopupBeval')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001504 let buf = RunVimInTerminal('-S XtestPopupBeval', #{rows: 10})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001505 call TermWait(buf, 50)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001506 call term_sendkeys(buf, 'j')
1507 call term_sendkeys(buf, ":call Hover()\<CR>")
1508 call VerifyScreenDump(buf, 'Test_popupwin_beval_1', {})
1509
1510 call term_sendkeys(buf, ":call MoveOntoPopup()\<CR>")
1511 call VerifyScreenDump(buf, 'Test_popupwin_beval_2', {})
1512
1513 call term_sendkeys(buf, ":call MoveAway()\<CR>")
1514 call VerifyScreenDump(buf, 'Test_popupwin_beval_3', {})
1515
1516 " clean up
1517 call StopVimInTerminal(buf)
1518 call delete('XtestPopupBeval')
1519endfunc
1520
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001521func Test_popup_filter()
1522 new
1523 call setline(1, 'some text')
1524
1525 func MyPopupFilter(winid, c)
Bram Moolenaarec084d32020-02-28 22:44:47 +01001526 if a:c == 'e' || a:c == "\<F9>"
1527 let g:eaten = a:c
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001528 return 1
1529 endif
1530 if a:c == '0'
1531 let g:ignored = '0'
1532 return 0
1533 endif
1534 if a:c == 'x'
1535 call popup_close(a:winid)
1536 return 1
1537 endif
1538 return 0
1539 endfunc
1540
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001541 let winid = 'something'->popup_create(#{filter: 'MyPopupFilter'})
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001542 redraw
1543
1544 " e is consumed by the filter
1545 call feedkeys('e', 'xt')
1546 call assert_equal('e', g:eaten)
Bram Moolenaarec084d32020-02-28 22:44:47 +01001547 call feedkeys("\<F9>", 'xt')
1548 call assert_equal("\<F9>", g:eaten)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001549
1550 " 0 is ignored by the filter
1551 normal $
1552 call assert_equal(9, getcurpos()[2])
1553 call feedkeys('0', 'xt')
1554 call assert_equal('0', g:ignored)
Bram Moolenaar0b719552021-06-12 13:59:03 +02001555 redraw
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001556 call assert_equal(1, getcurpos()[2])
1557
1558 " x closes the popup
1559 call feedkeys('x', 'xt')
Bram Moolenaarec084d32020-02-28 22:44:47 +01001560 call assert_equal("\<F9>", g:eaten)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001561 call assert_equal(-1, winbufnr(winid))
1562
1563 delfunc MyPopupFilter
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001564 call popup_clear()
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001565endfunc
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001566
Bram Moolenaar4934ad02020-09-28 22:29:58 +02001567" this tests that the filter is not used for :normal command
Bram Moolenaar189832b2020-09-23 12:29:11 +02001568func Test_popup_filter_normal_cmd()
1569 CheckScreendump
1570
1571 let lines =<< trim END
Bram Moolenaar4934ad02020-09-28 22:29:58 +02001572 let text = range(1, 20)->map({_, v -> string(v)})
1573 let g:winid = popup_create(text, #{maxheight: 5, minwidth: 3, filter: 'invalidfilter'})
1574 call timer_start(0, {-> win_execute(g:winid, 'norm! 10Gzz')})
Bram Moolenaar189832b2020-09-23 12:29:11 +02001575 END
1576 call writefile(lines, 'XtestPopupNormal')
1577 let buf = RunVimInTerminal('-S XtestPopupNormal', #{rows: 10})
1578 call TermWait(buf, 100)
1579 call VerifyScreenDump(buf, 'Test_popupwin_normal_cmd', {})
1580
1581 call StopVimInTerminal(buf)
1582 call delete('XtestPopupNormal')
1583endfunc
1584
Bram Moolenaar10476522020-09-24 22:57:31 +02001585" test that cursor line highlight is updated after using win_execute()
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02001586func Test_popup_filter_win_execute()
1587 CheckScreendump
1588
1589 let lines =<< trim END
Bram Moolenaar10476522020-09-24 22:57:31 +02001590 let lines = range(1, &lines * 2)->map({_, v -> string(v)})
1591 let g:id = popup_create(lines, #{
1592 \ minheight: &lines - 5,
1593 \ maxheight: &lines - 5,
1594 \ cursorline: 1,
1595 \ })
1596 redraw
1597 END
1598 call writefile(lines, 'XtestPopupWinExecute')
1599 let buf = RunVimInTerminal('-S XtestPopupWinExecute', #{rows: 14})
1600
1601 call term_sendkeys(buf, ":call win_execute(g:id, ['normal 17Gzz'])\<CR>")
1602 call term_sendkeys(buf, ":\<CR>")
1603
1604 call VerifyScreenDump(buf, 'Test_popupwin_win_execute_cursorline', {})
1605
1606 call StopVimInTerminal(buf)
1607 call delete('XtestPopupWinExecute')
1608endfunc
1609
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02001610func Test_popup_set_firstline()
1611 CheckScreendump
1612
1613 let lines =<< trim END
1614 let lines = range(1, 50)->map({_, v -> string(v)})
1615 let g:id = popup_create(lines, #{
1616 \ minwidth: 20,
1617 \ maxwidth: 20,
1618 \ minheight: &lines - 5,
1619 \ maxheight: &lines - 5,
1620 \ cursorline: 1,
1621 \ })
1622 call popup_setoptions(g:id, #{firstline: 10})
1623 redraw
1624 END
1625 call writefile(lines, 'XtestPopupWinSetFirstline')
1626 let buf = RunVimInTerminal('-S XtestPopupWinSetFirstline', #{rows: 16})
1627
1628 call VerifyScreenDump(buf, 'Test_popupwin_set_firstline_1', {})
1629
1630 call term_sendkeys(buf, ":call popup_setoptions(g:id, #{firstline: 5})\<CR>")
1631 call term_sendkeys(buf, ":\<CR>")
1632 call VerifyScreenDump(buf, 'Test_popupwin_set_firstline_2', {})
1633
1634 call StopVimInTerminal(buf)
1635 call delete('XtestPopupWinSetFirstline')
1636endfunc
1637
Bram Moolenaar10476522020-09-24 22:57:31 +02001638" this tests that we don't get stuck with an error in "win_execute()"
1639func Test_popup_filter_win_execute_error()
1640 CheckScreendump
1641
1642 let lines =<< trim END
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02001643 let g:winid = popup_create('some text', {'filter': 'invalidfilter'})
1644 call timer_start(0, {-> win_execute(g:winid, 'invalidCommand')})
1645 END
Bram Moolenaar10476522020-09-24 22:57:31 +02001646 call writefile(lines, 'XtestPopupWinExecuteError')
1647 let buf = RunVimInTerminal('-S XtestPopupWinExecuteError', #{rows: 10, wait_for_ruler: 0})
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02001648
1649 call WaitFor({-> term_getline(buf, 9) =~ 'Not an editor command: invalidCommand'})
1650 call term_sendkeys(buf, "\<CR>")
1651 call WaitFor({-> term_getline(buf, 9) =~ 'Unknown function: invalidfilter'})
1652 call term_sendkeys(buf, "\<CR>")
1653 call WaitFor({-> term_getline(buf, 9) =~ 'Not allowed in a popup window'})
1654 call term_sendkeys(buf, "\<CR>")
1655 call term_sendkeys(buf, "\<CR>")
1656 call VerifyScreenDump(buf, 'Test_popupwin_win_execute', {})
1657
1658 call StopVimInTerminal(buf)
Bram Moolenaar10476522020-09-24 22:57:31 +02001659 call delete('XtestPopupWinExecuteError')
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02001660endfunc
1661
Bram Moolenaara42d9452019-06-15 21:46:30 +02001662func ShowDialog(key, result)
1663 let s:cb_res = 999
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001664 let winid = popup_dialog('do you want to quit (Yes/no)?', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001665 \ filter: 'popup_filter_yesno',
1666 \ callback: 'QuitCallback',
Bram Moolenaara42d9452019-06-15 21:46:30 +02001667 \ })
1668 redraw
1669 call feedkeys(a:key, "xt")
1670 call assert_equal(winid, s:cb_winid)
1671 call assert_equal(a:result, s:cb_res)
1672endfunc
1673
1674func Test_popup_dialog()
1675 func QuitCallback(id, res)
1676 let s:cb_winid = a:id
1677 let s:cb_res = a:res
1678 endfunc
1679
1680 let winid = ShowDialog("y", 1)
1681 let winid = ShowDialog("Y", 1)
1682 let winid = ShowDialog("n", 0)
1683 let winid = ShowDialog("N", 0)
1684 let winid = ShowDialog("x", 0)
1685 let winid = ShowDialog("X", 0)
1686 let winid = ShowDialog("\<Esc>", 0)
1687 let winid = ShowDialog("\<C-C>", -1)
1688
1689 delfunc QuitCallback
1690endfunc
1691
Bram Moolenaara730e552019-06-16 19:05:31 +02001692func ShowMenu(key, result)
1693 let s:cb_res = 999
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001694 let winid = popup_menu(['one', 'two', 'something else'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001695 \ callback: 'QuitCallback',
Bram Moolenaara730e552019-06-16 19:05:31 +02001696 \ })
1697 redraw
1698 call feedkeys(a:key, "xt")
1699 call assert_equal(winid, s:cb_winid)
1700 call assert_equal(a:result, s:cb_res)
1701endfunc
1702
1703func Test_popup_menu()
1704 func QuitCallback(id, res)
1705 let s:cb_winid = a:id
1706 let s:cb_res = a:res
1707 endfunc
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001708 " mapping won't be used in popup
1709 map j k
Bram Moolenaara730e552019-06-16 19:05:31 +02001710
1711 let winid = ShowMenu(" ", 1)
1712 let winid = ShowMenu("j \<CR>", 2)
1713 let winid = ShowMenu("JjK \<CR>", 2)
1714 let winid = ShowMenu("jjjjjj ", 3)
1715 let winid = ShowMenu("kkk ", 1)
1716 let winid = ShowMenu("x", -1)
1717 let winid = ShowMenu("X", -1)
1718 let winid = ShowMenu("\<Esc>", -1)
1719 let winid = ShowMenu("\<C-C>", -1)
1720
1721 delfunc QuitCallback
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001722 unmap j
Bram Moolenaara730e552019-06-16 19:05:31 +02001723endfunc
1724
1725func Test_popup_menu_screenshot()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001726 CheckScreendump
Bram Moolenaara730e552019-06-16 19:05:31 +02001727
1728 let lines =<< trim END
1729 call setline(1, range(1, 20))
1730 hi PopupSelected ctermbg=lightblue
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001731 call popup_menu(['one', 'two', 'another'], #{callback: 'MenuDone', title: ' make a choice from the list '})
Bram Moolenaara730e552019-06-16 19:05:31 +02001732 func MenuDone(id, res)
1733 echomsg "selected " .. a:res
1734 endfunc
1735 END
1736 call writefile(lines, 'XtestPopupMenu')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001737 let buf = RunVimInTerminal('-S XtestPopupMenu', #{rows: 10})
Bram Moolenaara730e552019-06-16 19:05:31 +02001738 call VerifyScreenDump(buf, 'Test_popupwin_menu_01', {})
1739
1740 call term_sendkeys(buf, "jj")
1741 call VerifyScreenDump(buf, 'Test_popupwin_menu_02', {})
1742
1743 call term_sendkeys(buf, " ")
1744 call VerifyScreenDump(buf, 'Test_popupwin_menu_03', {})
1745
1746 " clean up
1747 call StopVimInTerminal(buf)
1748 call delete('XtestPopupMenu')
1749endfunc
1750
Bram Moolenaarf914a332019-07-20 15:09:56 +02001751func Test_popup_menu_narrow()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001752 CheckScreendump
Bram Moolenaarf914a332019-07-20 15:09:56 +02001753
1754 let lines =<< trim END
1755 call setline(1, range(1, 20))
1756 hi PopupSelected ctermbg=green
1757 call popup_menu(['one', 'two', 'three'], #{callback: 'MenuDone'})
1758 func MenuDone(id, res)
1759 echomsg "selected " .. a:res
1760 endfunc
1761 END
1762 call writefile(lines, 'XtestPopupNarrowMenu')
1763 let buf = RunVimInTerminal('-S XtestPopupNarrowMenu', #{rows: 10})
1764 call VerifyScreenDump(buf, 'Test_popupwin_menu_04', {})
1765
1766 " clean up
1767 call term_sendkeys(buf, "x")
1768 call StopVimInTerminal(buf)
1769 call delete('XtestPopupNarrowMenu')
1770endfunc
1771
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001772func Test_popup_title()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001773 CheckScreendump
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001774
1775 " Create a popup without title or border, a line of padding will be added to
1776 " put the title on.
1777 let lines =<< trim END
1778 call setline(1, range(1, 20))
Bram Moolenaar5d458a72019-08-04 21:12:15 +02001779 let winid = popup_create(['one', 'two', 'another'], #{title: 'Title String'})
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001780 END
1781 call writefile(lines, 'XtestPopupTitle')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001782 let buf = RunVimInTerminal('-S XtestPopupTitle', #{rows: 10})
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001783 call VerifyScreenDump(buf, 'Test_popupwin_title', {})
1784
Bram Moolenaar5d458a72019-08-04 21:12:15 +02001785 call term_sendkeys(buf, ":call popup_setoptions(winid, #{maxwidth: 20, title: 'a very long title that is not going to fit'})\<CR>")
1786 call term_sendkeys(buf, ":\<CR>")
1787 call VerifyScreenDump(buf, 'Test_popupwin_longtitle_1', {})
1788
1789 call term_sendkeys(buf, ":call popup_setoptions(winid, #{border: []})\<CR>")
1790 call term_sendkeys(buf, ":\<CR>")
1791 call VerifyScreenDump(buf, 'Test_popupwin_longtitle_2', {})
1792
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01001793 call term_sendkeys(buf, ":call popup_clear()\<CR>")
1794 call term_sendkeys(buf, ":call popup_create(['aaa', 'bbb'], #{title: 'Title', minwidth: 12, padding: [2, 2, 2, 2]})\<CR>")
1795 call term_sendkeys(buf, ":\<CR>")
1796 call VerifyScreenDump(buf, 'Test_popupwin_longtitle_3', {})
1797
1798 call term_sendkeys(buf, ":call popup_clear()\<CR>")
1799 call term_sendkeys(buf, ":call popup_create(['aaa', 'bbb'], #{title: 'Title', minwidth: 12, border: [], padding: [2, 2, 2, 2]})\<CR>")
1800 call term_sendkeys(buf, ":\<CR>")
1801 call VerifyScreenDump(buf, 'Test_popupwin_longtitle_4', {})
1802
Ralf Schandlbc869872021-05-28 14:12:14 +02001803 call term_sendkeys(buf, ":call popup_clear()\<CR>")
1804 call term_sendkeys(buf, ":call popup_menu(['This is a line', 'and another line'], #{title: '▶ÄÖÜ◀', })\<CR>")
1805 call VerifyScreenDump(buf, 'Test_popupwin_multibytetitle', {})
1806 call term_sendkeys(buf, "x")
1807
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001808 " clean up
1809 call StopVimInTerminal(buf)
1810 call delete('XtestPopupTitle')
Bram Moolenaarae943152019-06-16 22:54:14 +02001811
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001812 let winid = popup_create('something', #{title: 'Some Title'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001813 call assert_equal('Some Title', popup_getoptions(winid).title)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001814 call popup_setoptions(winid, #{title: 'Another Title'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001815 call assert_equal('Another Title', popup_getoptions(winid).title)
1816
1817 call popup_clear()
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001818endfunc
1819
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001820func Test_popup_close_callback()
1821 func PopupDone(id, result)
1822 let g:result = a:result
1823 endfunc
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001824 let winid = popup_create('something', #{callback: 'PopupDone'})
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001825 redraw
1826 call popup_close(winid, 'done')
1827 call assert_equal('done', g:result)
1828endfunc
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001829
1830func Test_popup_empty()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001831 let winid = popup_create('', #{padding: [2,2,2,2]})
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001832 redraw
1833 let pos = popup_getpos(winid)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001834 call assert_equal(5, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001835 call assert_equal(5, pos.height)
Bram Moolenaaref6b9792020-05-13 16:34:15 +02001836 call popup_close(winid)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001837
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001838 let winid = popup_create([], #{border: []})
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001839 redraw
1840 let pos = popup_getpos(winid)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001841 call assert_equal(3, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001842 call assert_equal(3, pos.height)
Bram Moolenaaref6b9792020-05-13 16:34:15 +02001843 call popup_close(winid)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001844endfunc
Bram Moolenaar988c4332019-06-02 14:12:11 +02001845
1846func Test_popup_never_behind()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001847 CheckScreendump
1848
Bram Moolenaar988c4332019-06-02 14:12:11 +02001849 " +-----------------------------+
1850 " | | |
1851 " | | |
1852 " | | |
1853 " | line1 |
1854 " |------------line2------------|
1855 " | line3 |
1856 " | line4 |
1857 " | |
1858 " | |
1859 " +-----------------------------+
1860 let lines =<< trim END
Bram Moolenaar988c4332019-06-02 14:12:11 +02001861 split
1862 vsplit
1863 let info_window1 = getwininfo()[0]
1864 let line = info_window1['height']
1865 let col = info_window1['width']
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001866 call popup_create(['line1', 'line2', 'line3', 'line4'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001867 \ line : line,
1868 \ col : col,
Bram Moolenaar988c4332019-06-02 14:12:11 +02001869 \ })
1870 END
1871 call writefile(lines, 'XtestPopupBehind')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001872 let buf = RunVimInTerminal('-S XtestPopupBehind', #{rows: 10})
Bram Moolenaar988c4332019-06-02 14:12:11 +02001873 call term_sendkeys(buf, "\<C-W>w")
1874 call VerifyScreenDump(buf, 'Test_popupwin_behind', {})
1875
1876 " clean up
1877 call StopVimInTerminal(buf)
1878 call delete('XtestPopupBehind')
1879endfunc
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001880
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001881func s:VerifyPosition(p, msg, line, col, width, height)
1882 call assert_equal(a:line, popup_getpos(a:p).line, a:msg . ' (l)')
1883 call assert_equal(a:col, popup_getpos(a:p).col, a:msg . ' (c)')
1884 call assert_equal(a:width, popup_getpos(a:p).width, a:msg . ' (w)')
1885 call assert_equal(a:height, popup_getpos(a:p).height, a:msg . ' (h)')
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001886endfunc
1887
1888func Test_popup_position_adjust()
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001889 " Anything placed past the last cell on the right of the screen is moved to
1890 " the left.
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001891 "
1892 " When wrapping is disabled, we also shift to the left to display on the
1893 " screen, unless fixed is set.
1894
1895 " Entries for cases which don't vary based on wrapping.
1896 " Format is per tests described below
1897 let both_wrap_tests = [
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001898 \ ['a', 5, &columns, 5, &columns, 1, 1],
1899 \ ['b', 5, &columns + 1, 5, &columns, 1, 1],
1900 \ ['c', 5, &columns - 1, 5, &columns - 1, 1, 1],
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001901 \ ['d', 5, &columns - 2, 5, &columns - 2, 1, 1],
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001902 \ ['e', 5, &columns - 3, 5, &columns - 3, 1, 1]]
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001903
1904 " these test groups are dicts with:
1905 " - comment: something to identify the group of tests by
1906 " - options: dict of options to merge with the row/col in tests
1907 " - tests: list of cases. Each one is a list with elements:
1908 " - text
1909 " - row
1910 " - col
1911 " - expected row
1912 " - expected col
1913 " - expected width
1914 " - expected height
1915 let tests = [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001916 \ #{
1917 \ comment: 'left-aligned with wrapping',
1918 \ options: #{
1919 \ wrap: 1,
1920 \ pos: 'botleft',
1921 \ },
1922 \ tests: both_wrap_tests + [
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001923 \ ['aa', 5, &columns, 4, &columns, 1, 2],
1924 \ ['bb', 5, &columns + 1, 4, &columns, 1, 2],
1925 \ ['cc', 5, &columns - 1, 5, &columns - 1, 2, 1],
1926 \ ['dd', 5, &columns - 2, 5, &columns - 2, 2, 1],
1927 \ ['ee', 5, &columns - 3, 5, &columns - 3, 2, 1],
1928 \
1929 \ ['aaa', 5, &columns, 3, &columns, 1, 3],
1930 \ ['bbb', 5, &columns + 1, 3, &columns, 1, 3],
1931 \ ['ccc', 5, &columns - 1, 4, &columns - 1, 2, 2],
1932 \ ['ddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
1933 \ ['eee', 5, &columns - 3, 5, &columns - 3, 3, 1],
1934 \
1935 \ ['aaaa', 5, &columns, 2, &columns, 1, 4],
1936 \ ['bbbb', 5, &columns + 1, 2, &columns, 1, 4],
1937 \ ['cccc', 5, &columns - 1, 4, &columns - 1, 2, 2],
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001938 \ ['dddd', 5, &columns - 2, 4, &columns - 2, 3, 2],
1939 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001940 \ ['eeee', 5, &columns - 4, 5, &columns - 4, 4, 1],
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001941 \ ],
1942 \ },
1943 \ #{
1944 \ comment: 'left aligned without wrapping',
1945 \ options: #{
1946 \ wrap: 0,
1947 \ pos: 'botleft',
1948 \ },
1949 \ tests: both_wrap_tests + [
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001950 \ ['aa', 5, &columns, 5, &columns - 1, 2, 1],
1951 \ ['bb', 5, &columns + 1, 5, &columns - 1, 2, 1],
1952 \ ['cc', 5, &columns - 1, 5, &columns - 1, 2, 1],
1953 \ ['dd', 5, &columns - 2, 5, &columns - 2, 2, 1],
1954 \ ['ee', 5, &columns - 3, 5, &columns - 3, 2, 1],
1955 \
1956 \ ['aaa', 5, &columns, 5, &columns - 2, 3, 1],
1957 \ ['bbb', 5, &columns + 1, 5, &columns - 2, 3, 1],
1958 \ ['ccc', 5, &columns - 1, 5, &columns - 2, 3, 1],
1959 \ ['ddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
1960 \ ['eee', 5, &columns - 3, 5, &columns - 3, 3, 1],
1961 \
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001962 \ ['aaaa', 5, &columns, 5, &columns - 3, 4, 1],
1963 \ ['bbbb', 5, &columns + 1, 5, &columns - 3, 4, 1],
1964 \ ['cccc', 5, &columns - 1, 5, &columns - 3, 4, 1],
1965 \ ['dddd', 5, &columns - 2, 5, &columns - 3, 4, 1],
1966 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
1967 \ ],
1968 \ },
1969 \ #{
1970 \ comment: 'left aligned with fixed position',
1971 \ options: #{
1972 \ wrap: 0,
1973 \ fixed: 1,
1974 \ pos: 'botleft',
1975 \ },
1976 \ tests: both_wrap_tests + [
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001977 \ ['aa', 5, &columns, 5, &columns, 1, 1],
1978 \ ['bb', 5, &columns + 1, 5, &columns, 1, 1],
1979 \ ['cc', 5, &columns - 1, 5, &columns - 1, 2, 1],
1980 \ ['dd', 5, &columns - 2, 5, &columns - 2, 2, 1],
1981 \ ['ee', 5, &columns - 3, 5, &columns - 3, 2, 1],
1982 \
1983 \ ['aaa', 5, &columns, 5, &columns, 1, 1],
1984 \ ['bbb', 5, &columns + 1, 5, &columns, 1, 1],
1985 \ ['ccc', 5, &columns - 1, 5, &columns - 1, 2, 1],
1986 \ ['ddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
1987 \ ['eee', 5, &columns - 3, 5, &columns - 3, 3, 1],
1988 \
1989 \ ['aaaa', 5, &columns, 5, &columns, 1, 1],
1990 \ ['bbbb', 5, &columns + 1, 5, &columns, 1, 1],
1991 \ ['cccc', 5, &columns - 1, 5, &columns - 1, 2, 1],
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001992 \ ['dddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
1993 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
1994 \ ],
1995 \ },
1996 \ ]
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001997
1998 for test_group in tests
1999 for test in test_group.tests
2000 let [ text, line, col, e_line, e_col, e_width, e_height ] = test
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002001 let options = #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002002 \ line: line,
2003 \ col: col,
2004 \ }
2005 call extend(options, test_group.options)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002006
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002007 let p = popup_create(text, options)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002008
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002009 let msg = string(extend(options, #{text: text}))
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002010 call s:VerifyPosition(p, msg, e_line, e_col, e_width, e_height)
2011 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002012 endfor
2013 endfor
2014
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002015 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002016 %bwipe!
2017endfunc
2018
Bram Moolenaar3397f742019-06-02 18:40:06 +02002019func Test_adjust_left_past_screen_width()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002020 " width of screen
2021 let X = join(map(range(&columns), {->'X'}), '')
2022
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002023 let p = popup_create(X, #{line: 1, col: 1, wrap: 0})
2024 call s:VerifyPosition(p, 'full width topleft', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002025
2026 redraw
2027 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
2028 call assert_equal(X, line)
2029
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002030 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002031 redraw
2032
2033 " Same if placed on the right hand side
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002034 let p = popup_create(X, #{line: 1, col: &columns, wrap: 0})
2035 call s:VerifyPosition(p, 'full width topright', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002036
2037 redraw
2038 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
2039 call assert_equal(X, line)
2040
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002041 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002042 redraw
2043
2044 " Extend so > window width
2045 let X .= 'x'
2046
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002047 let p = popup_create(X, #{line: 1, col: 1, wrap: 0})
2048 call s:VerifyPosition(p, 'full width + 1 topleft', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002049
2050 redraw
2051 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
2052 call assert_equal(X[ : -2 ], line)
2053
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002054 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002055 redraw
2056
2057 " Shifted then truncated (the x is not visible)
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002058 let p = popup_create(X, #{line: 1, col: &columns - 3, wrap: 0})
2059 call s:VerifyPosition(p, 'full width + 1 topright', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002060
2061 redraw
2062 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
2063 call assert_equal(X[ : -2 ], line)
2064
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002065 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002066 redraw
2067
2068 " Not shifted, just truncated
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002069 let p = popup_create(X,
2070 \ #{line: 1, col: 2, wrap: 0, fixed: 1})
2071 call s:VerifyPosition(p, 'full width + 1 fixed', 1, 2, &columns - 1, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002072
2073 redraw
2074 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
2075 let e_line = ' ' . X[ 1 : -2 ]
2076 call assert_equal(e_line, line)
2077
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002078 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002079 redraw
2080
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002081 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002082 %bwipe!
Bram Moolenaar3397f742019-06-02 18:40:06 +02002083endfunc
2084
2085func Test_popup_moved()
2086 new
2087 call test_override('char_avail', 1)
2088 call setline(1, ['one word to move around', 'a WORD.and->some thing'])
2089
2090 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002091 let winid = popup_atcursor('text', #{moved: 'any'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02002092 redraw
2093 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002094 call assert_equal([1, 4, 4], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02002095 " trigger the check for last_cursormoved by going into insert mode
2096 call feedkeys("li\<Esc>", 'xt')
2097 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002098 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02002099
2100 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002101 let winid = popup_atcursor('text', #{moved: 'word'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02002102 redraw
2103 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002104 call assert_equal([1, 4, 7], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02002105 call feedkeys("hi\<Esc>", 'xt')
2106 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002107 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02002108
2109 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002110 let winid = popup_atcursor('text', #{moved: 'word'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02002111 redraw
2112 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002113 call assert_equal([1, 4, 7], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02002114 call feedkeys("li\<Esc>", 'xt')
2115 call assert_equal(1, popup_getpos(winid).visible)
2116 call feedkeys("ei\<Esc>", 'xt')
2117 call assert_equal(1, popup_getpos(winid).visible)
2118 call feedkeys("eli\<Esc>", 'xt')
2119 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002120 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02002121
Bram Moolenaar17627312019-06-02 19:53:44 +02002122 " WORD is the default
Bram Moolenaar3397f742019-06-02 18:40:06 +02002123 exe "normal gg0/WORD\<CR>"
Bram Moolenaar17627312019-06-02 19:53:44 +02002124 let winid = popup_atcursor('text', {})
Bram Moolenaar3397f742019-06-02 18:40:06 +02002125 redraw
2126 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002127 call assert_equal([2, 2, 15], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02002128 call feedkeys("eli\<Esc>", 'xt')
2129 call assert_equal(1, popup_getpos(winid).visible)
2130 call feedkeys("wi\<Esc>", 'xt')
2131 call assert_equal(1, popup_getpos(winid).visible)
2132 call feedkeys("Eli\<Esc>", 'xt')
2133 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002134 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02002135
2136 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002137 let winid = popup_atcursor('text', #{moved: [5, 10]})
Bram Moolenaar3397f742019-06-02 18:40:06 +02002138 redraw
2139 call assert_equal(1, popup_getpos(winid).visible)
2140 call feedkeys("eli\<Esc>", 'xt')
2141 call feedkeys("ei\<Esc>", 'xt')
2142 call assert_equal(1, popup_getpos(winid).visible)
2143 call feedkeys("eli\<Esc>", 'xt')
2144 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002145 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02002146
2147 bwipe!
2148 call test_override('ALL', 0)
2149endfunc
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002150
2151func Test_notifications()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002152 CheckFeature timers
2153 CheckScreendump
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002154
Bram Moolenaar0fdddee2019-09-01 15:26:23 +02002155 let lines =<< trim END
2156 call setline(1, range(1, 20))
2157 hi Notification ctermbg=lightblue
2158 call popup_notification('first notification', {})
2159 END
2160 call writefile(lines, 'XtestNotifications')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002161 let buf = RunVimInTerminal('-S XtestNotifications', #{rows: 10})
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002162 call VerifyScreenDump(buf, 'Test_popupwin_notify_01', {})
2163
2164 " second one goes below the first one
Bram Moolenaardfa97f22019-06-15 14:31:55 +02002165 call term_sendkeys(buf, ":hi link PopupNotification Notification\<CR>")
2166 call term_sendkeys(buf, ":call popup_notification('another important notification', {})\<CR>")
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002167 call VerifyScreenDump(buf, 'Test_popupwin_notify_02', {})
2168
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002169 " clean up
2170 call StopVimInTerminal(buf)
2171 call delete('XtestNotifications')
2172endfunc
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002173
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002174func Test_popup_scrollbar()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002175 CheckScreendump
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002176
2177 let lines =<< trim END
2178 call setline(1, range(1, 20))
Bram Moolenaar8da41812019-06-26 18:04:54 +02002179 hi ScrollThumb ctermbg=blue
2180 hi ScrollBar ctermbg=red
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002181 let winid = popup_create(['one', 'two', 'three', 'four', 'five',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002182 \ 'six', 'seven', 'eight', 'nine'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002183 \ minwidth: 8,
2184 \ maxheight: 4,
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002185 \ })
Bram Moolenaar53a95d62019-06-26 03:54:08 +02002186 func ScrollUp()
2187 call feedkeys("\<F3>\<ScrollWheelUp>", "xt")
2188 endfunc
2189 func ScrollDown()
2190 call feedkeys("\<F3>\<ScrollWheelDown>", "xt")
2191 endfunc
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02002192 func ClickTop()
2193 call feedkeys("\<F4>\<LeftMouse>", "xt")
2194 endfunc
2195 func ClickBot()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002196 call popup_setoptions(g:winid, #{border: [], close: 'button'})
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02002197 call feedkeys("\<F5>\<LeftMouse>", "xt")
2198 endfunc
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002199 func Popup_filter(winid, key)
2200 if a:key == 'j'
Bram Moolenaar371806e2020-10-22 13:44:54 +02002201 silent! this_throws_an_error_but_is_ignored
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002202 let line = popup_getoptions(a:winid).firstline
2203 let nlines = line('$', a:winid)
2204 let newline = line < nlines ? (line + 1) : nlines
2205 call popup_setoptions(a:winid, #{firstline: newline})
2206 return v:true
2207 elseif a:key == 'x'
2208 call popup_close(a:winid)
2209 return v:true
2210 endif
2211 endfunc
2212
Bram Moolenaar6c542f72020-09-27 21:16:45 +02002213 def CreatePopup(text: list<string>)
Bram Moolenaare0de1712020-12-02 17:36:54 +01002214 popup_create(text, {
Bram Moolenaar6c542f72020-09-27 21:16:45 +02002215 \ minwidth: 30,
2216 \ maxwidth: 30,
2217 \ minheight: 4,
2218 \ maxheight: 4,
2219 \ firstline: 1,
2220 \ lastline: 4,
2221 \ wrap: true,
2222 \ scrollbar: true,
2223 \ mapping: false,
2224 \ filter: Popup_filter,
2225 \ })
2226 enddef
2227
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002228 func PopupScroll()
2229 call popup_clear()
2230 let text =<< trim END
2231 1
2232 2
2233 3
2234 4
2235 long line long line long line long line long line long line
2236 long line long line long line long line long line long line
2237 long line long line long line long line long line long line
2238 END
Bram Moolenaar6c542f72020-09-27 21:16:45 +02002239 call CreatePopup(text)
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002240 endfunc
Bram Moolenaar53a95d62019-06-26 03:54:08 +02002241 map <silent> <F3> :call test_setmouse(5, 36)<CR>
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02002242 map <silent> <F4> :call test_setmouse(4, 42)<CR>
2243 map <silent> <F5> :call test_setmouse(7, 42)<CR>
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002244 END
2245 call writefile(lines, 'XtestPopupScroll')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002246 let buf = RunVimInTerminal('-S XtestPopupScroll', #{rows: 10})
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002247 call VerifyScreenDump(buf, 'Test_popupwin_scroll_1', {})
2248
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002249 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 2})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002250 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002251 call VerifyScreenDump(buf, 'Test_popupwin_scroll_2', {})
2252
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002253 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 6})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002254 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002255 call VerifyScreenDump(buf, 'Test_popupwin_scroll_3', {})
2256
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002257 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 9})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002258 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002259 call VerifyScreenDump(buf, 'Test_popupwin_scroll_4', {})
2260
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02002261 call term_sendkeys(buf, ":call popup_setoptions(winid, #{scrollbarhighlight: 'ScrollBar', thumbhighlight: 'ScrollThumb', firstline: 5})\<CR>")
Bram Moolenaara112f2d2019-09-01 17:38:09 +02002262 " this scrolls two lines (half the window height)
Bram Moolenaar53a95d62019-06-26 03:54:08 +02002263 call term_sendkeys(buf, ":call ScrollUp()\<CR>")
2264 call VerifyScreenDump(buf, 'Test_popupwin_scroll_5', {})
2265
2266 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
2267 call VerifyScreenDump(buf, 'Test_popupwin_scroll_6', {})
2268
2269 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
Bram Moolenaar13b47c32019-06-28 21:55:48 +02002270 " wait a bit, otherwise it fails sometimes (double click recognized?)
2271 sleep 100m
Bram Moolenaar53a95d62019-06-26 03:54:08 +02002272 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
2273 call VerifyScreenDump(buf, 'Test_popupwin_scroll_7', {})
2274
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02002275 call term_sendkeys(buf, ":call ClickTop()\<CR>")
2276 sleep 100m
2277 call term_sendkeys(buf, ":call ClickTop()\<CR>")
2278 call VerifyScreenDump(buf, 'Test_popupwin_scroll_8', {})
2279
2280 call term_sendkeys(buf, ":call ClickBot()\<CR>")
2281 call VerifyScreenDump(buf, 'Test_popupwin_scroll_9', {})
2282
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02002283 " remove the minwidth and maxheight
2284 call term_sendkeys(buf, ":call popup_setoptions(winid, #{maxheight: 0, minwidth: 0})\<CR>")
Bram Moolenaar7e0f4622019-09-17 21:23:39 +02002285 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02002286 call VerifyScreenDump(buf, 'Test_popupwin_scroll_10', {})
2287
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002288 " check size with non-wrapping lines
2289 call term_sendkeys(buf, ":call PopupScroll()\<CR>")
2290 call VerifyScreenDump(buf, 'Test_popupwin_scroll_11', {})
2291
2292 " check size with wrapping lines
2293 call term_sendkeys(buf, "j")
2294 call VerifyScreenDump(buf, 'Test_popupwin_scroll_12', {})
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002295
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002296 " clean up
Bram Moolenaar20298ce2020-06-19 21:46:52 +02002297 call term_sendkeys(buf, "x")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002298 call StopVimInTerminal(buf)
2299 call delete('XtestPopupScroll')
2300endfunc
2301
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02002302func Test_popup_too_high_scrollbar()
2303 CheckScreendump
2304
2305 let lines =<< trim END
2306 call setline(1, range(1, 20)->map({i, v -> repeat(v, 10)}))
2307 set scrolloff=0
2308 func ShowPopup()
2309 let winid = popup_atcursor(['one', 'two', 'three', 'four', 'five',
2310 \ 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve'], #{
2311 \ minwidth: 8,
2312 \ border: [],
2313 \ })
2314 endfunc
2315 normal 3G$
2316 call ShowPopup()
2317 END
2318 call writefile(lines, 'XtestPopupToohigh')
2319 let buf = RunVimInTerminal('-S XtestPopupToohigh', #{rows: 10})
2320 call VerifyScreenDump(buf, 'Test_popupwin_toohigh_1', {})
2321
2322 call term_sendkeys(buf, ":call popup_clear()\<CR>")
2323 call term_sendkeys(buf, "8G$")
2324 call term_sendkeys(buf, ":call ShowPopup()\<CR>")
2325 call VerifyScreenDump(buf, 'Test_popupwin_toohigh_2', {})
2326
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02002327 call term_sendkeys(buf, ":call popup_clear()\<CR>")
2328 call term_sendkeys(buf, "gg$")
2329 call term_sendkeys(buf, ":call ShowPopup()\<CR>")
2330 call VerifyScreenDump(buf, 'Test_popupwin_toohigh_3', {})
2331
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02002332 " clean up
2333 call StopVimInTerminal(buf)
2334 call delete('XtestPopupToohigh')
2335endfunc
2336
Bram Moolenaar437a7462019-07-05 20:17:22 +02002337func Test_popup_fitting_scrollbar()
2338 " this was causing a crash, divide by zero
2339 let winid = popup_create([
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002340 \ 'one', 'two', 'longer line that wraps', 'four', 'five'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002341 \ scrollbar: 1,
2342 \ maxwidth: 10,
2343 \ maxheight: 5,
2344 \ firstline: 2})
Bram Moolenaar437a7462019-07-05 20:17:22 +02002345 redraw
2346 call popup_clear()
2347endfunc
2348
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002349func Test_popup_settext()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002350 CheckScreendump
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002351
2352 let lines =<< trim END
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002353 let opts = #{wrap: 0}
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002354 let p = popup_create('test', opts)
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002355 eval p->popup_settext('this is a text')
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002356 END
2357
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002358 call writefile(lines, 'XtestPopupSetText')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002359 let buf = RunVimInTerminal('-S XtestPopupSetText', #{rows: 10})
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002360 call VerifyScreenDump(buf, 'Test_popup_settext_01', {})
2361
2362 " Setting to empty string clears it
2363 call term_sendkeys(buf, ":call popup_settext(p, '')\<CR>")
2364 call VerifyScreenDump(buf, 'Test_popup_settext_02', {})
2365
2366 " Setting a list
2367 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
2368 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
2369
2370 " Shrinking with a list
2371 call term_sendkeys(buf, ":call popup_settext(p, ['a'])\<CR>")
2372 call VerifyScreenDump(buf, 'Test_popup_settext_04', {})
2373
2374 " Growing with a list
2375 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
2376 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
2377
2378 " Empty list clears
2379 call term_sendkeys(buf, ":call popup_settext(p, [])\<CR>")
2380 call VerifyScreenDump(buf, 'Test_popup_settext_05', {})
2381
2382 " Dicts
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002383 call term_sendkeys(buf, ":call popup_settext(p, [#{text: 'aaaa'}, #{text: 'bbbb'}, #{text: 'cccc'}])\<CR>")
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002384 call VerifyScreenDump(buf, 'Test_popup_settext_06', {})
2385
Bram Moolenaarb0992022020-01-30 14:55:42 +01002386 " range() (doesn't work)
2387 call term_sendkeys(buf, ":call popup_settext(p, range(4, 8))\<CR>")
2388 call VerifyScreenDump(buf, 'Test_popup_settext_07', {})
2389
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002390 " clean up
2391 call StopVimInTerminal(buf)
2392 call delete('XtestPopupSetText')
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002393endfunc
2394
Bram Moolenaar1f42f5a2020-09-03 18:52:24 +02002395func Test_popup_settext_getline()
2396 let id = popup_create('', #{ tabpage: 0 })
2397 call popup_settext(id, ['a','b'])
2398 call assert_equal(2, line('$', id)) " OK :)
2399 call popup_close(id)
2400
2401 let id = popup_create('', #{ tabpage: -1 })
2402 call popup_settext(id, ['a','b'])
2403 call assert_equal(2, line('$', id)) " Fails :(
2404 call popup_close(id)
2405endfunc
2406
Bram Moolenaar74f8eec2020-10-15 19:10:56 +02002407func Test_popup_settext_null()
2408 let id = popup_create('', #{ tabpage: 0 })
2409 call popup_settext(id, test_null_list())
2410 call popup_close(id)
2411
2412 let id = popup_create('', #{ tabpage: 0 })
2413 call popup_settext(id, test_null_string())
2414 call popup_close(id)
2415endfunc
2416
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002417func Test_popup_hidden()
2418 new
2419
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002420 let winid = popup_atcursor('text', #{hidden: 1})
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002421 redraw
2422 call assert_equal(0, popup_getpos(winid).visible)
2423 call popup_close(winid)
2424
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002425 let winid = popup_create('text', #{hidden: 1})
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002426 redraw
2427 call assert_equal(0, popup_getpos(winid).visible)
2428 call popup_close(winid)
2429
2430 func QuitCallback(id, res)
2431 let s:cb_winid = a:id
2432 let s:cb_res = a:res
2433 endfunc
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002434 let winid = 'make a choice'->popup_dialog(#{hidden: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002435 \ filter: 'popup_filter_yesno',
2436 \ callback: 'QuitCallback',
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002437 \ })
2438 redraw
2439 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarae943152019-06-16 22:54:14 +02002440 call assert_equal(function('popup_filter_yesno'), popup_getoptions(winid).filter)
2441 call assert_equal(function('QuitCallback'), popup_getoptions(winid).callback)
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002442 exe "normal anot used by filter\<Esc>"
2443 call assert_equal('not used by filter', getline(1))
2444
2445 call popup_show(winid)
2446 call feedkeys('y', "xt")
2447 call assert_equal(1, s:cb_res)
2448
2449 bwipe!
2450 delfunc QuitCallback
2451endfunc
Bram Moolenaarae943152019-06-16 22:54:14 +02002452
2453" Test options not checked elsewhere
2454func Test_set_get_options()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002455 let winid = popup_create('some text', #{highlight: 'Beautiful'})
Bram Moolenaarae943152019-06-16 22:54:14 +02002456 let options = popup_getoptions(winid)
2457 call assert_equal(1, options.wrap)
2458 call assert_equal(0, options.drag)
2459 call assert_equal('Beautiful', options.highlight)
2460
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002461 call popup_setoptions(winid, #{wrap: 0, drag: 1, highlight: 'Another'})
Bram Moolenaarae943152019-06-16 22:54:14 +02002462 let options = popup_getoptions(winid)
2463 call assert_equal(0, options.wrap)
2464 call assert_equal(1, options.drag)
2465 call assert_equal('Another', options.highlight)
2466
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02002467 call assert_fails('call popup_setoptions(winid, [])', 'E715:')
2468 call assert_fails('call popup_setoptions(winid, test_null_dict())', 'E715:')
2469
Bram Moolenaarae943152019-06-16 22:54:14 +02002470 call popup_close(winid)
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02002471 call assert_equal(0, popup_setoptions(winid, options.wrap))
Bram Moolenaarae943152019-06-16 22:54:14 +02002472endfunc
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002473
2474func Test_popupwin_garbage_collect()
2475 func MyPopupFilter(x, winid, c)
2476 " NOP
2477 endfunc
2478
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002479 let winid = popup_create('something', #{filter: function('MyPopupFilter', [{}])})
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002480 call test_garbagecollect_now()
2481 redraw
Bram Moolenaar1bc353b2019-09-01 14:45:28 +02002482 " Must not crash caused by invalid memory access
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002483 call feedkeys('j', 'xt')
2484 call assert_true(v:true)
2485
2486 call popup_close(winid)
2487 delfunc MyPopupFilter
2488endfunc
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002489
Bram Moolenaar581ba392019-09-03 22:08:33 +02002490func Test_popupwin_filter_mode()
2491 func MyPopupFilter(winid, c)
2492 let s:typed = a:c
2493 if a:c == ':' || a:c == "\r" || a:c == 'v'
2494 " can start cmdline mode, get out, and start/stop Visual mode
2495 return 0
2496 endif
2497 return 1
2498 endfunc
2499
2500 " Normal, Visual and Insert mode
2501 let winid = popup_create('something', #{filter: 'MyPopupFilter', filtermode: 'nvi'})
2502 redraw
2503 call feedkeys('x', 'xt')
2504 call assert_equal('x', s:typed)
2505
2506 call feedkeys(":let g:foo = 'foo'\<CR>", 'xt')
2507 call assert_equal(':', s:typed)
2508 call assert_equal('foo', g:foo)
2509
2510 let @x = 'something'
2511 call feedkeys('v$"xy', 'xt')
2512 call assert_equal('y', s:typed)
2513 call assert_equal('something', @x) " yank command is filtered out
2514 call feedkeys('v', 'xt') " end Visual mode
2515
2516 call popup_close(winid)
2517
2518 " only Normal mode
2519 let winid = popup_create('something', #{filter: 'MyPopupFilter', filtermode: 'n'})
2520 redraw
2521 call feedkeys('x', 'xt')
2522 call assert_equal('x', s:typed)
2523
2524 call feedkeys(":let g:foo = 'foo'\<CR>", 'xt')
2525 call assert_equal(':', s:typed)
2526 call assert_equal('foo', g:foo)
2527
2528 let @x = 'something'
2529 call feedkeys('v$"xy', 'xt')
2530 call assert_equal('v', s:typed)
2531 call assert_notequal('something', @x)
2532
2533 call popup_close(winid)
2534
2535 " default: all modes
2536 let winid = popup_create('something', #{filter: 'MyPopupFilter'})
2537 redraw
2538 call feedkeys('x', 'xt')
2539 call assert_equal('x', s:typed)
2540
2541 let g:foo = 'bar'
2542 call feedkeys(":let g:foo = 'foo'\<CR>", 'xt')
2543 call assert_equal("\r", s:typed)
2544 call assert_equal('bar', g:foo)
2545
2546 let @x = 'something'
2547 call feedkeys('v$"xy', 'xt')
2548 call assert_equal('y', s:typed)
2549 call assert_equal('something', @x) " yank command is filtered out
2550 call feedkeys('v', 'xt') " end Visual mode
2551
2552 call popup_close(winid)
2553 delfunc MyPopupFilter
2554endfunc
2555
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002556func Test_popupwin_filter_mouse()
2557 func MyPopupFilter(winid, c)
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002558 let g:got_mousepos = getmousepos()
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002559 return 0
2560 endfunc
2561
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002562 call setline(1, ['.'->repeat(25)]->repeat(10))
2563 let winid = popup_create(['short', 'long line that will wrap', 'other'], #{
2564 \ line: 2,
2565 \ col: 4,
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002566 \ maxwidth: 12,
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002567 \ padding: [],
2568 \ border: [],
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002569 \ filter: 'MyPopupFilter',
2570 \ })
2571 redraw
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002572 " 123456789012345678901
2573 " 1 .....................
2574 " 2 ...+--------------+..
2575 " 3 ...| |..
2576 " 4 ...| short |..
2577 " 5 ...| long line th |..
2578 " 6 ...| at will wrap |..
2579 " 7 ...| other |..
2580 " 8 ...| |..
2581 " 9 ...+--------------+..
2582 " 10 .....................
2583 let tests = []
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002584
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002585 func AddItemOutsidePopup(tests, row, col)
2586 eval a:tests->add(#{clickrow: a:row, clickcol: a:col, result: #{
2587 \ screenrow: a:row, screencol: a:col,
2588 \ winid: win_getid(), winrow: a:row, wincol: a:col,
2589 \ line: a:row, column: a:col,
2590 \ }})
2591 endfunc
2592 func AddItemInPopupBorder(tests, winid, row, col)
2593 eval a:tests->add(#{clickrow: a:row, clickcol: a:col, result: #{
2594 \ screenrow: a:row, screencol: a:col,
2595 \ winid: a:winid, winrow: a:row - 1, wincol: a:col - 3,
2596 \ line: 0, column: 0,
2597 \ }})
2598 endfunc
2599 func AddItemInPopupText(tests, winid, row, col, textline, textcol)
2600 eval a:tests->add(#{clickrow: a:row, clickcol: a:col, result: #{
2601 \ screenrow: a:row, screencol: a:col,
2602 \ winid: a:winid, winrow: a:row - 1, wincol: a:col - 3,
2603 \ line: a:textline, column: a:textcol,
2604 \ }})
2605 endfunc
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002606
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002607 " above and below popup
2608 for c in range(1, 21)
2609 call AddItemOutsidePopup(tests, 1, c)
2610 call AddItemOutsidePopup(tests, 10, c)
2611 endfor
2612 " left and right of popup
2613 for r in range(1, 10)
2614 call AddItemOutsidePopup(tests, r, 3)
2615 call AddItemOutsidePopup(tests, r, 20)
2616 endfor
2617 " top and bottom in popup
2618 for c in range(4, 19)
2619 call AddItemInPopupBorder(tests, winid, 2, c)
2620 call AddItemInPopupBorder(tests, winid, 3, c)
2621 call AddItemInPopupBorder(tests, winid, 8, c)
2622 call AddItemInPopupBorder(tests, winid, 9, c)
2623 endfor
2624 " left and right margin in popup
2625 for r in range(2, 9)
2626 call AddItemInPopupBorder(tests, winid, r, 4)
2627 call AddItemInPopupBorder(tests, winid, r, 5)
2628 call AddItemInPopupBorder(tests, winid, r, 18)
2629 call AddItemInPopupBorder(tests, winid, r, 19)
2630 endfor
2631 " text "short"
2632 call AddItemInPopupText(tests, winid, 4, 6, 1, 1)
2633 call AddItemInPopupText(tests, winid, 4, 10, 1, 5)
2634 call AddItemInPopupText(tests, winid, 4, 11, 1, 6)
2635 call AddItemInPopupText(tests, winid, 4, 17, 1, 6)
2636 " text "long line th"
2637 call AddItemInPopupText(tests, winid, 5, 6, 2, 1)
2638 call AddItemInPopupText(tests, winid, 5, 10, 2, 5)
2639 call AddItemInPopupText(tests, winid, 5, 17, 2, 12)
2640 " text "at will wrap"
2641 call AddItemInPopupText(tests, winid, 6, 6, 2, 13)
2642 call AddItemInPopupText(tests, winid, 6, 10, 2, 17)
2643 call AddItemInPopupText(tests, winid, 6, 17, 2, 24)
2644 " text "other"
2645 call AddItemInPopupText(tests, winid, 7, 6, 3, 1)
2646 call AddItemInPopupText(tests, winid, 7, 10, 3, 5)
2647 call AddItemInPopupText(tests, winid, 7, 11, 3, 6)
2648 call AddItemInPopupText(tests, winid, 7, 17, 3, 6)
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002649
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002650 for item in tests
2651 call test_setmouse(item.clickrow, item.clickcol)
2652 call feedkeys("\<LeftMouse>", 'xt')
2653 call assert_equal(item.result, g:got_mousepos)
2654 endfor
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002655
2656 call popup_close(winid)
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002657 enew!
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002658 delfunc MyPopupFilter
2659endfunc
2660
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002661func Test_popupwin_with_buffer()
2662 call writefile(['some text', 'in a buffer'], 'XsomeFile')
2663 let buf = bufadd('XsomeFile')
2664 call assert_equal(0, bufloaded(buf))
Bram Moolenaar46451042019-08-24 15:50:46 +02002665
2666 setlocal number
2667 call setbufvar(buf, "&wrapmargin", 13)
2668
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002669 let winid = popup_create(buf, {})
2670 call assert_notequal(0, winid)
2671 let pos = popup_getpos(winid)
2672 call assert_equal(2, pos.height)
2673 call assert_equal(1, bufloaded(buf))
Bram Moolenaar46451042019-08-24 15:50:46 +02002674
2675 " window-local option is set to default, buffer-local is not
2676 call assert_equal(0, getwinvar(winid, '&number'))
2677 call assert_equal(13, getbufvar(buf, '&wrapmargin'))
2678
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002679 call popup_close(winid)
2680 call assert_equal({}, popup_getpos(winid))
2681 call assert_equal(1, bufloaded(buf))
2682 exe 'bwipe! ' .. buf
Bram Moolenaar46451042019-08-24 15:50:46 +02002683 setlocal nonumber
Bram Moolenaar7866b872019-07-01 22:21:01 +02002684
2685 edit test_popupwin.vim
2686 let winid = popup_create(bufnr(''), {})
2687 redraw
2688 call popup_close(winid)
Bram Moolenaar3940ec62019-07-05 21:53:24 +02002689 call delete('XsomeFile')
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002690endfunc
Bram Moolenaare296e312019-07-03 23:20:18 +02002691
Bram Moolenaare0d749a2019-09-25 22:14:48 +02002692func Test_popupwin_terminal_buffer()
Bram Moolenaard2c1fb42019-09-25 23:06:40 +02002693 CheckFeature terminal
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002694 CheckUnix
Bram Moolenaare06a28f2020-05-13 23:24:12 +02002695 " Starting a terminal to run a shell in is considered flaky.
2696 let g:test_is_flaky = 1
Bram Moolenaard2c1fb42019-09-25 23:06:40 +02002697
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002698 let origwin = win_getid()
Bram Moolenaar349f6092020-10-06 20:46:49 +02002699
2700 " open help window to test that :help below fails
2701 help
2702
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002703 let termbuf = term_start(&shell, #{hidden: 1})
Bram Moolenaarf5452692020-11-28 21:56:06 +01002704 let winid = popup_create(termbuf, #{minwidth: 40, minheight: 10, border: []})
Bram Moolenaar0353f562020-12-17 22:27:38 +01002705 " Wait for shell to start
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002706 call WaitForAssert({-> assert_equal("run", job_status(term_getjob(termbuf)))})
Bram Moolenaar0353f562020-12-17 22:27:38 +01002707 " Wait for a prompt (see border char first, then space after prompt)
2708 call WaitForAssert({ -> assert_equal(' ', screenstring(screenrow(), screencol() - 1))})
Bram Moolenaarf5452692020-11-28 21:56:06 +01002709
2710 " When typing a character, the cursor is after it.
2711 call feedkeys("x", 'xt')
Bram Moolenaar0353f562020-12-17 22:27:38 +01002712 call term_wait(termbuf)
Bram Moolenaarf5452692020-11-28 21:56:06 +01002713 redraw
2714 call WaitForAssert({ -> assert_equal('x', screenstring(screenrow(), screencol() - 1))})
2715 call feedkeys("\<BS>", 'xt')
2716
Bram Moolenaar631ebc42020-02-03 22:15:26 +01002717 " Check this doesn't crash
2718 call assert_equal(winnr(), winnr('j'))
2719 call assert_equal(winnr(), winnr('k'))
2720 call assert_equal(winnr(), winnr('h'))
2721 call assert_equal(winnr(), winnr('l'))
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02002722
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002723 " Cannot quit while job is running
2724 call assert_fails('call feedkeys("\<C-W>:quit\<CR>", "xt")', 'E948:')
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02002725
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002726 " Cannot enter Terminal-Normal mode. (TODO: but it works...)
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02002727 call feedkeys("xxx\<C-W>N", 'xt')
2728 call assert_fails('call feedkeys("gf", "xt")', 'E863:')
2729 call feedkeys("a\<C-U>", 'xt')
2730
Bram Moolenaar3f65c662020-05-27 23:15:16 +02002731 " Cannot escape from terminal window
2732 call assert_fails('tab drop xxx', 'E863:')
Bram Moolenaar349f6092020-10-06 20:46:49 +02002733 call assert_fails('help', 'E994:')
Bram Moolenaar3f65c662020-05-27 23:15:16 +02002734
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002735 " Cannot open a second one.
2736 let termbuf2 = term_start(&shell, #{hidden: 1})
2737 call assert_fails('call popup_create(termbuf2, #{})', 'E861:')
2738 call term_sendkeys(termbuf2, "exit\<CR>")
2739
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02002740 " Exiting shell closes popup window
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002741 call feedkeys("exit\<CR>", 'xt')
2742 " Wait for shell to exit
Bram Moolenaarb2b218d2020-06-22 20:22:19 +02002743 call WaitForAssert({-> assert_equal("dead", job_status(term_getjob(termbuf)))})
Bram Moolenaar373c6512020-06-22 19:24:23 +02002744
Bram Moolenaar349f6092020-10-06 20:46:49 +02002745 helpclose
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002746 call feedkeys(":quit\<CR>", 'xt')
2747 call assert_equal(origwin, win_getid())
Bram Moolenaare0d749a2019-09-25 22:14:48 +02002748endfunc
2749
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002750func Test_popupwin_close_prevwin()
2751 CheckFeature terminal
Bram Moolenaar62f93f42020-09-02 22:33:24 +02002752 call Popupwin_close_prevwin()
2753endfunc
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002754
Bram Moolenaar62f93f42020-09-02 22:33:24 +02002755def Popupwin_close_prevwin()
2756 assert_equal(1, winnr('$'))
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002757 split
2758 wincmd b
Bram Moolenaar62f93f42020-09-02 22:33:24 +02002759 assert_equal(2, winnr())
Bram Moolenaare0de1712020-12-02 17:36:54 +01002760 var buf = term_start(&shell, {hidden: 1})
Bram Moolenaar62f93f42020-09-02 22:33:24 +02002761 popup_create(buf, {})
2762 TermWait(buf, 100)
2763 popup_clear(true)
2764 assert_equal(2, winnr())
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002765
2766 quit
2767 exe 'bwipe! ' .. buf
Bram Moolenaar62f93f42020-09-02 22:33:24 +02002768enddef
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002769
Bram Moolenaar934470e2019-09-01 23:27:05 +02002770func Test_popupwin_with_buffer_and_filter()
2771 new Xwithfilter
2772 call setline(1, range(100))
2773 let bufnr = bufnr()
2774 hide
2775
2776 func BufferFilter(win, key)
2777 if a:key == 'G'
2778 " recursive use of "G" does not cause problems.
2779 call win_execute(a:win, 'normal! G')
2780 return 1
2781 endif
2782 return 0
2783 endfunc
2784
2785 let winid = popup_create(bufnr, #{maxheight: 5, filter: 'BufferFilter'})
2786 call assert_equal(1, popup_getpos(winid).firstline)
2787 redraw
2788 call feedkeys("G", 'xt')
2789 call assert_equal(99, popup_getpos(winid).firstline)
2790
2791 call popup_close(winid)
2792 exe 'bwipe! ' .. bufnr
2793endfunc
2794
Bram Moolenaare296e312019-07-03 23:20:18 +02002795func Test_popupwin_width()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002796 let winid = popup_create(repeat(['short', 'long long long line', 'medium width'], 50), #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002797 \ maxwidth: 40,
2798 \ maxheight: 10,
Bram Moolenaare296e312019-07-03 23:20:18 +02002799 \ })
2800 for top in range(1, 20)
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002801 eval winid->popup_setoptions(#{firstline: top})
Bram Moolenaare296e312019-07-03 23:20:18 +02002802 redraw
2803 call assert_equal(19, popup_getpos(winid).width)
2804 endfor
2805 call popup_clear()
2806endfunc
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02002807
2808func Test_popupwin_buf_close()
2809 let buf = bufadd('Xtestbuf')
2810 call bufload(buf)
2811 call setbufline(buf, 1, ['just', 'some', 'lines'])
2812 let winid = popup_create(buf, {})
2813 redraw
2814 call assert_equal(3, popup_getpos(winid).height)
2815 let bufinfo = getbufinfo(buf)[0]
2816 call assert_equal(1, bufinfo.changed)
2817 call assert_equal(0, bufinfo.hidden)
2818 call assert_equal(0, bufinfo.listed)
2819 call assert_equal(1, bufinfo.loaded)
2820 call assert_equal([], bufinfo.windows)
2821 call assert_equal([winid], bufinfo.popups)
2822
2823 call popup_close(winid)
2824 call assert_equal({}, popup_getpos(winid))
2825 let bufinfo = getbufinfo(buf)[0]
2826 call assert_equal(1, bufinfo.changed)
2827 call assert_equal(1, bufinfo.hidden)
2828 call assert_equal(0, bufinfo.listed)
2829 call assert_equal(1, bufinfo.loaded)
2830 call assert_equal([], bufinfo.windows)
2831 call assert_equal([], bufinfo.popups)
2832 exe 'bwipe! ' .. buf
2833endfunc
Bram Moolenaar017c2692019-07-13 14:17:51 +02002834
2835func Test_popup_menu_with_maxwidth()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002836 CheckScreendump
Bram Moolenaar017c2692019-07-13 14:17:51 +02002837
2838 let lines =<< trim END
2839 call setline(1, range(1, 10))
2840 hi ScrollThumb ctermbg=blue
2841 hi ScrollBar ctermbg=red
2842 func PopupMenu(lines, line, col, scrollbar = 0)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002843 return popup_menu(a:lines, #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002844 \ maxwidth: 10,
2845 \ maxheight: 3,
2846 \ pos : 'topleft',
2847 \ col : a:col,
2848 \ line : a:line,
2849 \ scrollbar : a:scrollbar,
Bram Moolenaar017c2692019-07-13 14:17:51 +02002850 \ })
2851 endfunc
2852 call PopupMenu(['x'], 1, 1)
2853 call PopupMenu(['123456789|'], 1, 16)
2854 call PopupMenu(['123456789|' .. ' '], 7, 1)
2855 call PopupMenu([repeat('123456789|', 100)], 7, 16)
2856 call PopupMenu(repeat(['123456789|' .. ' '], 5), 1, 33, 1)
2857 END
2858 call writefile(lines, 'XtestPopupMenuMaxWidth')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002859 let buf = RunVimInTerminal('-S XtestPopupMenuMaxWidth', #{rows: 13})
Bram Moolenaar017c2692019-07-13 14:17:51 +02002860 call VerifyScreenDump(buf, 'Test_popupwin_menu_maxwidth_1', {})
2861
2862 " close the menu popupwin.
2863 call term_sendkeys(buf, " ")
2864 call term_sendkeys(buf, " ")
2865 call term_sendkeys(buf, " ")
2866 call term_sendkeys(buf, " ")
2867 call term_sendkeys(buf, " ")
2868
2869 " clean up
2870 call StopVimInTerminal(buf)
2871 call delete('XtestPopupMenuMaxWidth')
2872endfunc
2873
Bram Moolenaara901a372019-07-13 16:38:50 +02002874func Test_popup_menu_with_scrollbar()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002875 CheckScreendump
Bram Moolenaara901a372019-07-13 16:38:50 +02002876
2877 let lines =<< trim END
2878 call setline(1, range(1, 20))
2879 hi ScrollThumb ctermbg=blue
2880 hi ScrollBar ctermbg=red
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002881 eval ['one', 'two', 'three', 'four', 'five',
2882 \ 'six', 'seven', 'eight', 'nine']
2883 \ ->popup_menu(#{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002884 \ minwidth: 8,
2885 \ maxheight: 3,
Bram Moolenaara901a372019-07-13 16:38:50 +02002886 \ })
2887 END
2888 call writefile(lines, 'XtestPopupMenuScroll')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002889 let buf = RunVimInTerminal('-S XtestPopupMenuScroll', #{rows: 10})
Bram Moolenaara901a372019-07-13 16:38:50 +02002890
2891 call term_sendkeys(buf, "j")
2892 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_1', {})
2893
2894 call term_sendkeys(buf, "jjj")
2895 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_2', {})
2896
2897 " if the cursor is the bottom line, it stays at the bottom line.
2898 call term_sendkeys(buf, repeat("j", 20))
2899 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_3', {})
2900
2901 call term_sendkeys(buf, "kk")
2902 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_4', {})
2903
2904 call term_sendkeys(buf, "k")
2905 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_5', {})
2906
2907 " if the cursor is in the top line, it stays in the top line.
2908 call term_sendkeys(buf, repeat("k", 20))
2909 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_6', {})
2910
2911 " close the menu popupwin.
2912 call term_sendkeys(buf, " ")
2913
2914 " clean up
2915 call StopVimInTerminal(buf)
2916 call delete('XtestPopupMenuScroll')
2917endfunc
2918
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002919func Test_popup_menu_filter()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002920 CheckScreendump
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002921
2922 let lines =<< trim END
2923 function! MyFilter(winid, key) abort
2924 if a:key == "0"
2925 call win_execute(a:winid, "call setpos('.', [0, 1, 1, 0])")
2926 return 1
2927 endif
2928 if a:key == "G"
2929 call win_execute(a:winid, "call setpos('.', [0, line('$'), 1, 0])")
2930 return 1
2931 endif
2932 if a:key == "j"
2933 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0])")
2934 return 1
2935 endif
2936 if a:key == "k"
2937 call win_execute(a:winid, "call setpos('.', [0, line('.') - 1, 1, 0])")
2938 return 1
2939 endif
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02002940 if a:key == ':'
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002941 call popup_close(a:winid)
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02002942 return 0
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002943 endif
2944 return 0
2945 endfunction
2946 call popup_menu(['111', '222', '333', '444', '555', '666', '777', '888', '999'], #{
2947 \ maxheight : 3,
2948 \ filter : 'MyFilter'
2949 \ })
2950 END
2951 call writefile(lines, 'XtestPopupMenuFilter')
2952 let buf = RunVimInTerminal('-S XtestPopupMenuFilter', #{rows: 10})
2953
2954 call term_sendkeys(buf, "j")
2955 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_1', {})
2956
2957 call term_sendkeys(buf, "k")
2958 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_2', {})
2959
2960 call term_sendkeys(buf, "G")
2961 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_3', {})
2962
2963 call term_sendkeys(buf, "0")
2964 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_4', {})
2965
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02002966 " check that when the popup is closed in the filter the screen is redrawn
2967 call term_sendkeys(buf, ":")
2968 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_5', {})
2969 call term_sendkeys(buf, "\<CR>")
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002970
2971 " clean up
2972 call StopVimInTerminal(buf)
2973 call delete('XtestPopupMenuFilter')
2974endfunc
2975
2976func Test_popup_cursorline()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002977 CheckScreendump
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002978
2979 let winid = popup_create('some text', {})
2980 call assert_equal(0, popup_getoptions(winid).cursorline)
2981 call popup_close(winid)
2982
2983 let winid = popup_create('some text', #{ cursorline: 1, })
2984 call assert_equal(1, popup_getoptions(winid).cursorline)
2985 call popup_close(winid)
2986
Bram Moolenaar6bfc4752021-02-21 23:12:18 +01002987 let winid = popup_create('some text', #{ cursorline: v:true, })
2988 call assert_equal(1, popup_getoptions(winid).cursorline)
2989 call popup_close(winid)
2990
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002991 let winid = popup_create('some text', #{ cursorline: 0, })
2992 call assert_equal(0, popup_getoptions(winid).cursorline)
2993 call popup_close(winid)
2994
2995 let winid = popup_menu('some text', {})
2996 call assert_equal(1, popup_getoptions(winid).cursorline)
2997 call popup_close(winid)
2998
2999 let winid = popup_menu('some text', #{ cursorline: 1, })
3000 call assert_equal(1, popup_getoptions(winid).cursorline)
3001 call popup_close(winid)
3002
3003 let winid = popup_menu('some text', #{ cursorline: 0, })
3004 call assert_equal(0, popup_getoptions(winid).cursorline)
3005 call popup_close(winid)
3006
3007 " ---------
3008 " Pattern 1
3009 " ---------
3010 let lines =<< trim END
3011 call popup_create(['111', '222', '333'], #{ cursorline : 0 })
3012 END
3013 call writefile(lines, 'XtestPopupCursorLine')
3014 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
3015 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_1', {})
3016 call term_sendkeys(buf, ":call popup_clear()\<cr>")
3017 call StopVimInTerminal(buf)
3018
3019 " ---------
3020 " Pattern 2
3021 " ---------
3022 let lines =<< trim END
3023 call popup_create(['111', '222', '333'], #{ cursorline : 1 })
3024 END
3025 call writefile(lines, 'XtestPopupCursorLine')
3026 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
3027 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_2', {})
3028 call term_sendkeys(buf, ":call popup_clear()\<cr>")
3029 call StopVimInTerminal(buf)
3030
3031 " ---------
3032 " Pattern 3
3033 " ---------
3034 let lines =<< trim END
3035 function! MyFilter(winid, key) abort
3036 if a:key == "j"
3037 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0]) | redraw")
3038 return 1
3039 endif
3040 if a:key == 'x'
3041 call popup_close(a:winid)
3042 return 1
3043 endif
3044 return 0
3045 endfunction
3046 call popup_menu(['111', '222', '333'], #{
3047 \ cursorline : 0,
3048 \ maxheight : 2,
3049 \ filter : 'MyFilter',
3050 \ })
3051 END
3052 call writefile(lines, 'XtestPopupCursorLine')
3053 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
3054 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_3', {})
3055 call term_sendkeys(buf, "j")
3056 call term_sendkeys(buf, "j")
3057 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_4', {})
3058 call term_sendkeys(buf, "x")
3059 call StopVimInTerminal(buf)
3060
3061 " ---------
3062 " Pattern 4
3063 " ---------
3064 let lines =<< trim END
3065 function! MyFilter(winid, key) abort
3066 if a:key == "j"
3067 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0]) | redraw")
3068 return 1
3069 endif
3070 if a:key == 'x'
3071 call popup_close(a:winid)
3072 return 1
3073 endif
3074 return 0
3075 endfunction
3076 call popup_menu(['111', '222', '333'], #{
3077 \ cursorline : 1,
3078 \ maxheight : 2,
3079 \ filter : 'MyFilter',
3080 \ })
3081 END
3082 call writefile(lines, 'XtestPopupCursorLine')
3083 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
3084 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_5', {})
3085 call term_sendkeys(buf, "j")
3086 call term_sendkeys(buf, "j")
3087 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_6', {})
3088 call term_sendkeys(buf, "x")
3089 call StopVimInTerminal(buf)
3090
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02003091 " ---------
3092 " Cursor in second line when creating the popup
3093 " ---------
3094 let lines =<< trim END
3095 let winid = popup_create(['111', '222', '333'], #{
3096 \ cursorline : 1,
3097 \ })
3098 call win_execute(winid, "2")
3099 END
3100 call writefile(lines, 'XtestPopupCursorLine')
3101 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
3102 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_7', {})
3103 call StopVimInTerminal(buf)
3104
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003105 call delete('XtestPopupCursorLine')
Bram Moolenaar4eb7dae2019-11-12 22:33:45 +01003106
3107 " ---------
3108 " Use current buffer for popupmenu
3109 " ---------
3110 let lines =<< trim END
3111 call setline(1, ['one', 'two', 'three'])
3112 let winid = popup_create(bufnr('%'), #{
3113 \ cursorline : 1,
3114 \ })
3115 call win_execute(winid, "2")
3116 END
3117 call writefile(lines, 'XtestPopupCursorLine')
3118 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
3119 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_8', {})
3120 call StopVimInTerminal(buf)
3121
3122 call delete('XtestPopupCursorLine')
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003123endfunc
3124
Bram Moolenaar6bfc4752021-02-21 23:12:18 +01003125def Test_popup_cursorline_vim9()
3126 var winid = popup_create('some text', { cursorline: true, })
3127 assert_equal(1, popup_getoptions(winid).cursorline)
3128 popup_close(winid)
3129
3130 assert_fails("popup_create('some text', { cursorline: 2, })", 'E1023:')
3131 popup_clear()
3132enddef
3133
Bram Moolenaarf914a332019-07-20 15:09:56 +02003134func Test_previewpopup()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02003135 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003136 CheckFeature quickfix
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02003137
Bram Moolenaarf914a332019-07-20 15:09:56 +02003138 call writefile([
3139 \ "!_TAG_FILE_ENCODING\tutf-8\t//",
3140 \ "another\tXtagfile\t/^this is another",
3141 \ "theword\tXtagfile\t/^theword"],
3142 \ 'Xtags')
3143 call writefile(range(1,20)
3144 \ + ['theword is here']
3145 \ + range(22, 27)
3146 \ + ['this is another place']
3147 \ + range(29, 40),
3148 \ "Xtagfile")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02003149 call writefile(range(1,10)
3150 \ + ['searched word is here']
3151 \ + range(12, 20),
3152 \ "Xheader.h")
Bram Moolenaarf914a332019-07-20 15:09:56 +02003153 let lines =<< trim END
3154 set tags=Xtags
3155 call setline(1, [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02003156 \ 'one',
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02003157 \ '#include "Xheader.h"',
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02003158 \ 'three',
3159 \ 'four',
3160 \ 'five',
3161 \ 'six',
3162 \ 'seven',
3163 \ 'find theword somewhere',
3164 \ 'nine',
3165 \ 'this is another word',
3166 \ 'very long line where the word is also another'])
Bram Moolenaarf914a332019-07-20 15:09:56 +02003167 set previewpopup=height:4,width:40
Bram Moolenaar60577482021-03-04 21:35:07 +01003168 hi OtherColor ctermbg=lightcyan guibg=lightcyan
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02003169 set path=.
Bram Moolenaarf914a332019-07-20 15:09:56 +02003170 END
3171 call writefile(lines, 'XtestPreviewPopup')
3172 let buf = RunVimInTerminal('-S XtestPreviewPopup', #{rows: 14})
3173
3174 call term_sendkeys(buf, "/theword\<CR>\<C-W>}")
3175 call term_sendkeys(buf, ":\<CR>")
3176 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_1', {})
3177
Bram Moolenaar60577482021-03-04 21:35:07 +01003178 call term_sendkeys(buf, ":set previewpopup+=highlight:OtherColor\<CR>")
Bram Moolenaarf914a332019-07-20 15:09:56 +02003179 call term_sendkeys(buf, "/another\<CR>\<C-W>}")
3180 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_2', {})
3181
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003182 call term_sendkeys(buf, ":call popup_move(popup_findpreview(), #{col: 15})\<CR>")
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02003183 call term_sendkeys(buf, ":\<CR>")
3184 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_3', {})
3185
3186 call term_sendkeys(buf, "/another\<CR>\<C-W>}")
3187 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_4', {})
3188
Bram Moolenaar799439a2020-02-11 21:44:17 +01003189 call term_sendkeys(buf, ":silent cd ..\<CR>:\<CR>")
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02003190 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_5', {})
Bram Moolenaar799439a2020-02-11 21:44:17 +01003191 call term_sendkeys(buf, ":silent cd testdir\<CR>")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02003192
Bram Moolenaar60577482021-03-04 21:35:07 +01003193 call term_sendkeys(buf, ":set previewpopup-=highlight:OtherColor\<CR>")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02003194 call term_sendkeys(buf, ":pclose\<CR>")
Bram Moolenaar78d629a2019-08-16 17:31:15 +02003195 call term_sendkeys(buf, ":\<BS>")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02003196 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_6', {})
3197
3198 call term_sendkeys(buf, ":pedit +/theword Xtagfile\<CR>")
3199 call term_sendkeys(buf, ":\<CR>")
3200 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_7', {})
3201
3202 call term_sendkeys(buf, ":pclose\<CR>")
3203 call term_sendkeys(buf, ":psearch searched\<CR>")
3204 call term_sendkeys(buf, ":\<CR>")
3205 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_8', {})
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02003206
Bram Moolenaar8bf716c2020-01-23 15:33:54 +01003207 call term_sendkeys(buf, "\<C-W>p")
3208 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_9', {})
3209
3210 call term_sendkeys(buf, ":call win_execute(popup_findpreview(), 'call popup_clear()')\<CR>")
3211 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_10', {})
3212
Bram Moolenaarf914a332019-07-20 15:09:56 +02003213 call StopVimInTerminal(buf)
3214 call delete('Xtags')
3215 call delete('Xtagfile')
3216 call delete('XtestPreviewPopup')
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02003217 call delete('Xheader.h')
Bram Moolenaarf914a332019-07-20 15:09:56 +02003218endfunc
3219
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003220func Get_popupmenu_lines()
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003221 let lines =<< trim END
3222 set completeopt+=preview,popup
3223 set completefunc=CompleteFuncDict
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02003224 hi InfoPopup ctermbg=yellow
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003225
3226 func CompleteFuncDict(findstart, base)
3227 if a:findstart
3228 if col('.') > 10
3229 return col('.') - 10
3230 endif
3231 return 0
3232 endif
3233
3234 return {
3235 \ 'words': [
3236 \ {
3237 \ 'word': 'aword',
3238 \ 'abbr': 'wrd',
3239 \ 'menu': 'extra text',
3240 \ 'info': 'words are cool',
3241 \ 'kind': 'W',
3242 \ 'user_data': 'test'
3243 \ },
3244 \ {
3245 \ 'word': 'anotherword',
3246 \ 'abbr': 'anotwrd',
3247 \ 'menu': 'extra text',
3248 \ 'info': "other words are\ncooler than this and some more text\nto make wrap",
3249 \ 'kind': 'W',
3250 \ 'user_data': 'notest'
3251 \ },
3252 \ {
3253 \ 'word': 'noinfo',
3254 \ 'abbr': 'noawrd',
3255 \ 'menu': 'extra text',
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02003256 \ 'info': "lets\nshow\na\nscrollbar\nhere",
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003257 \ 'kind': 'W',
3258 \ 'user_data': 'notest'
3259 \ },
3260 \ {
3261 \ 'word': 'thatword',
3262 \ 'abbr': 'thatwrd',
3263 \ 'menu': 'extra text',
3264 \ 'info': 'that word is cool',
3265 \ 'kind': 'W',
3266 \ 'user_data': 'notest'
3267 \ },
3268 \ ]
3269 \ }
3270 endfunc
3271 call setline(1, 'text text text text text text text ')
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003272 func ChangeColor()
3273 let id = popup_findinfo()
Bram Moolenaard356fc62020-12-09 18:13:44 +01003274 if buflisted(winbufnr(id))
3275 call setline(1, 'buffer is listed')
3276 endif
Bram Moolenaar6a124e62019-09-04 18:15:19 +02003277 eval id->popup_setoptions(#{highlight: 'InfoPopup'})
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003278 endfunc
Bram Moolenaardca7abe2019-10-20 18:17:57 +02003279
3280 func InfoHidden()
3281 set completepopup=height:4,border:off,align:menu
3282 set completeopt-=popup completeopt+=popuphidden
3283 au CompleteChanged * call HandleChange()
3284 endfunc
3285
3286 let s:counter = 0
3287 func HandleChange()
3288 let s:counter += 1
3289 let selected = complete_info(['selected']).selected
3290 if selected <= 0
3291 " First time: do nothing, info remains hidden
3292 return
3293 endif
3294 if selected == 1
3295 " Second time: show info right away
3296 let id = popup_findinfo()
3297 if id
3298 call popup_settext(id, 'immediate info ' .. s:counter)
3299 call popup_show(id)
3300 endif
3301 else
3302 " Third time: show info after a short delay
3303 call timer_start(100, 'ShowInfo')
3304 endif
3305 endfunc
3306
3307 func ShowInfo(...)
3308 let id = popup_findinfo()
3309 if id
3310 call popup_settext(id, 'async info ' .. s:counter)
3311 call popup_show(id)
3312 endif
3313 endfunc
Bram Moolenaar2dfae042020-11-15 14:09:37 +01003314
3315 " Check that no autocommands are triggered for the info popup
3316 au WinEnter * if win_gettype() == 'popup' | call setline(2, 'WinEnter') | endif
3317 au WinLeave * if win_gettype() == 'popup' | call setline(2, 'WinLeave') | endif
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003318 END
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003319 return lines
3320endfunc
3321
3322func Test_popupmenu_info_border()
3323 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003324 CheckFeature quickfix
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003325
3326 let lines = Get_popupmenu_lines()
3327 call add(lines, 'set completepopup=height:4,highlight:InfoPopup')
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003328 call writefile(lines, 'XtestInfoPopup')
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003329
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003330 let buf = RunVimInTerminal('-S XtestInfoPopup', #{rows: 14})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003331 call TermWait(buf, 25)
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003332
3333 call term_sendkeys(buf, "A\<C-X>\<C-U>")
3334 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_1', {})
3335
3336 call term_sendkeys(buf, "\<C-N>")
3337 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_2', {})
3338
3339 call term_sendkeys(buf, "\<C-N>")
3340 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_3', {})
3341
3342 call term_sendkeys(buf, "\<C-N>\<C-N>")
3343 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_4', {})
3344
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02003345 " info on the left with scrollbar
3346 call term_sendkeys(buf, "test text test text\<C-X>\<C-U>")
3347 call term_sendkeys(buf, "\<C-N>\<C-N>")
3348 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_5', {})
3349
Bram Moolenaar202c3f72019-11-21 12:12:35 +01003350 " Test that the popupmenu's scrollbar and infopopup do not overlap
3351 call term_sendkeys(buf, "\<Esc>")
3352 call term_sendkeys(buf, ":set pumheight=3\<CR>")
3353 call term_sendkeys(buf, "cc\<C-X>\<C-U>")
3354 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_6', {})
3355
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003356 " Hide the info popup, cycle through buffers, make sure it didn't get
Bram Moolenaarca7c0782020-01-14 20:42:48 +01003357 " deleted.
3358 call term_sendkeys(buf, "\<Esc>")
3359 call term_sendkeys(buf, ":set hidden\<CR>")
3360 call term_sendkeys(buf, ":bn\<CR>")
3361 call term_sendkeys(buf, ":bn\<CR>")
3362 call term_sendkeys(buf, "otest text test text\<C-X>\<C-U>")
3363 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_7', {})
3364
Bram Moolenaar447bfba2020-07-18 16:07:16 +02003365 " Test that when the option is changed the popup changes.
3366 call term_sendkeys(buf, "\<Esc>")
3367 call term_sendkeys(buf, ":set completepopup=border:off\<CR>")
3368 call term_sendkeys(buf, "a\<C-X>\<C-U>")
3369 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_8', {})
3370
Bram Moolenaar6d585f42020-07-26 22:20:54 +02003371 call term_sendkeys(buf, " \<Esc>")
3372 call term_sendkeys(buf, ":set completepopup+=width:10\<CR>")
3373 call term_sendkeys(buf, "a\<C-X>\<C-U>")
3374 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_9', {})
3375
Bram Moolenaarca7c0782020-01-14 20:42:48 +01003376 call term_sendkeys(buf, "\<Esc>")
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003377 call StopVimInTerminal(buf)
3378 call delete('XtestInfoPopup')
3379endfunc
3380
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003381func Test_popupmenu_info_noborder()
3382 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003383 CheckFeature quickfix
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003384
3385 let lines = Get_popupmenu_lines()
3386 call add(lines, 'set completepopup=height:4,border:off')
3387 call writefile(lines, 'XtestInfoPopupNb')
3388
3389 let buf = RunVimInTerminal('-S XtestInfoPopupNb', #{rows: 14})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003390 call TermWait(buf, 25)
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003391
3392 call term_sendkeys(buf, "A\<C-X>\<C-U>")
3393 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_nb_1', {})
3394
3395 call StopVimInTerminal(buf)
3396 call delete('XtestInfoPopupNb')
3397endfunc
3398
Bram Moolenaar258cef52019-08-21 17:29:29 +02003399func Test_popupmenu_info_align_menu()
3400 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003401 CheckFeature quickfix
Bram Moolenaar258cef52019-08-21 17:29:29 +02003402
3403 let lines = Get_popupmenu_lines()
3404 call add(lines, 'set completepopup=height:4,border:off,align:menu')
3405 call writefile(lines, 'XtestInfoPopupNb')
3406
3407 let buf = RunVimInTerminal('-S XtestInfoPopupNb', #{rows: 14})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003408 call TermWait(buf, 25)
Bram Moolenaar258cef52019-08-21 17:29:29 +02003409
3410 call term_sendkeys(buf, "A\<C-X>\<C-U>")
3411 call term_sendkeys(buf, "\<C-N>")
3412 call term_sendkeys(buf, "\<C-N>")
3413 call term_sendkeys(buf, "\<C-N>")
3414 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_1', {})
3415
3416 call term_sendkeys(buf, "test text test text test\<C-X>\<C-U>")
3417 call term_sendkeys(buf, "\<C-N>")
3418 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_2', {})
3419
3420 call term_sendkeys(buf, "\<Esc>")
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003421 call term_sendkeys(buf, ":call ChangeColor()\<CR>")
Bram Moolenaar258cef52019-08-21 17:29:29 +02003422 call term_sendkeys(buf, ":call setline(2, ['x']->repeat(10))\<CR>")
3423 call term_sendkeys(buf, "Gotest text test text\<C-X>\<C-U>")
3424 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_3', {})
3425
3426 call StopVimInTerminal(buf)
3427 call delete('XtestInfoPopupNb')
3428endfunc
3429
Bram Moolenaardca7abe2019-10-20 18:17:57 +02003430func Test_popupmenu_info_hidden()
3431 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003432 CheckFeature quickfix
Bram Moolenaardca7abe2019-10-20 18:17:57 +02003433
3434 let lines = Get_popupmenu_lines()
3435 call add(lines, 'call InfoHidden()')
3436 call writefile(lines, 'XtestInfoPopupHidden')
3437
3438 let buf = RunVimInTerminal('-S XtestInfoPopupHidden', #{rows: 14})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003439 call TermWait(buf, 25)
Bram Moolenaardca7abe2019-10-20 18:17:57 +02003440
3441 call term_sendkeys(buf, "A\<C-X>\<C-U>")
3442 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_hidden_1', {})
3443
3444 call term_sendkeys(buf, "\<C-N>")
3445 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_hidden_2', {})
3446
3447 call term_sendkeys(buf, "\<C-N>")
3448 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_hidden_3', {})
3449
3450 call term_sendkeys(buf, "\<Esc>")
3451 call StopVimInTerminal(buf)
3452 call delete('XtestInfoPopupHidden')
3453endfunc
3454
Bram Moolenaarbef93ac2019-12-06 20:17:35 +01003455func Test_popupmenu_info_too_wide()
3456 CheckScreendump
3457 CheckFeature quickfix
3458
3459 let lines =<< trim END
3460 call setline(1, range(10))
3461
3462 set completeopt+=preview,popup
3463 set completepopup=align:menu
3464 set omnifunc=OmniFunc
3465 hi InfoPopup ctermbg=lightgrey
3466
3467 func OmniFunc(findstart, base)
3468 if a:findstart
3469 return 0
3470 endif
3471
3472 let menuText = 'some long text to make sure the menu takes up all of the width of the window'
3473 return #{
3474 \ words: [
3475 \ #{
3476 \ word: 'scrap',
3477 \ menu: menuText,
3478 \ info: "other words are\ncooler than this and some more text\nto make wrap",
3479 \ },
3480 \ #{
3481 \ word: 'scappier',
3482 \ menu: menuText,
3483 \ info: 'words are cool',
3484 \ },
3485 \ #{
3486 \ word: 'scrappier2',
3487 \ menu: menuText,
3488 \ info: 'words are cool',
3489 \ },
3490 \ ]
3491 \ }
3492 endfunc
3493 END
3494
3495 call writefile(lines, 'XtestInfoPopupWide')
3496 let buf = RunVimInTerminal('-S XtestInfoPopupWide', #{rows: 8})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003497 call TermWait(buf, 25)
Bram Moolenaarbef93ac2019-12-06 20:17:35 +01003498
3499 call term_sendkeys(buf, "Ascr\<C-X>\<C-O>")
3500 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_wide_1', {})
3501
3502 call term_sendkeys(buf, "\<Esc>")
3503 call StopVimInTerminal(buf)
3504 call delete('XtestInfoPopupWide')
3505endfunc
3506
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02003507func Test_popupwin_recycle_bnr()
Bram Moolenaare49fbff2019-08-21 22:50:07 +02003508 let bufnr = popup_notification('nothing wrong', {})->winbufnr()
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02003509 call popup_clear()
Bram Moolenaar6a124e62019-09-04 18:15:19 +02003510 let winid = 'nothing wrong'->popup_notification({})
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02003511 call assert_equal(bufnr, winbufnr(winid))
3512 call popup_clear()
3513endfunc
3514
Bram Moolenaar1824f452019-10-02 23:06:46 +02003515func Test_popupwin_getoptions_tablocal()
3516 topleft split
3517 let win1 = popup_create('nothing', #{maxheight: 8})
3518 let win2 = popup_create('something', #{maxheight: 10})
3519 let win3 = popup_create('something', #{maxheight: 15})
3520 call assert_equal(8, popup_getoptions(win1).maxheight)
3521 call assert_equal(10, popup_getoptions(win2).maxheight)
3522 call assert_equal(15, popup_getoptions(win3).maxheight)
3523 call popup_clear()
3524 quit
3525endfunc
3526
Bram Moolenaare8a7dfe2019-10-03 22:35:52 +02003527func Test_popupwin_cancel()
3528 let win1 = popup_create('one', #{line: 5, filter: {... -> 0}})
3529 let win2 = popup_create('two', #{line: 10, filter: {... -> 0}})
3530 let win3 = popup_create('three', #{line: 15, filter: {... -> 0}})
3531 call assert_equal(5, popup_getpos(win1).line)
3532 call assert_equal(10, popup_getpos(win2).line)
3533 call assert_equal(15, popup_getpos(win3).line)
3534 " TODO: this also works without patch 8.1.2110
3535 call feedkeys("\<C-C>", 'xt')
3536 call assert_equal(5, popup_getpos(win1).line)
3537 call assert_equal(10, popup_getpos(win2).line)
3538 call assert_equal({}, popup_getpos(win3))
3539 call feedkeys("\<C-C>", 'xt')
3540 call assert_equal(5, popup_getpos(win1).line)
3541 call assert_equal({}, popup_getpos(win2))
3542 call assert_equal({}, popup_getpos(win3))
3543 call feedkeys("\<C-C>", 'xt')
3544 call assert_equal({}, popup_getpos(win1))
3545 call assert_equal({}, popup_getpos(win2))
3546 call assert_equal({}, popup_getpos(win3))
3547endfunc
3548
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003549func Test_popupwin_filter_redraw()
3550 " Create two popups with a filter that closes the popup when typing "0".
3551 " Both popups should close, even though the redraw also calls
3552 " popup_reset_handled()
3553
3554 func CloseFilter(winid, key)
3555 if a:key == '0'
3556 call popup_close(a:winid)
3557 redraw
3558 endif
3559 return 0 " pass the key
3560 endfunc
3561
3562 let id1 = popup_create('first one', #{
3563 \ line: 1,
3564 \ col: 1,
3565 \ filter: 'CloseFilter',
3566 \ })
3567 let id2 = popup_create('second one', #{
3568 \ line: 9,
3569 \ col: 1,
3570 \ filter: 'CloseFilter',
3571 \ })
3572 call assert_equal(1, popup_getpos(id1).line)
3573 call assert_equal(9, popup_getpos(id2).line)
3574
3575 call feedkeys('0', 'xt')
3576 call assert_equal({}, popup_getpos(id1))
3577 call assert_equal({}, popup_getpos(id2))
3578
3579 call popup_clear()
3580 delfunc CloseFilter
3581endfunc
3582
Bram Moolenaar20ebbea2019-11-30 17:58:27 +01003583func Test_popupwin_double_width()
3584 CheckScreendump
3585
3586 let lines =<< trim END
3587 call setline(1, 'x你好世界你好世你好世界你好')
3588 call setline(2, '你好世界你好世你好世界你好')
3589 call setline(3, 'x你好世界你好世你好世界你好')
3590 call popup_create('你好,世界 - 你好,世界xxxxx', #{line: 1, col: 3, maxwidth: 14})
3591 END
3592 call writefile(lines, 'XtestPopupWide')
3593
3594 let buf = RunVimInTerminal('-S XtestPopupWide', #{rows: 10})
3595 call VerifyScreenDump(buf, 'Test_popupwin_doublewidth_1', {})
3596
3597 call StopVimInTerminal(buf)
3598 call delete('XtestPopupWide')
3599endfunc
3600
3601func Test_popupwin_sign()
3602 CheckScreendump
3603
3604 let lines =<< trim END
3605 call setline(1, range(10))
3606 call sign_define('Current', {
3607 \ 'text': '>>',
3608 \ 'texthl': 'WarningMsg',
3609 \ 'linehl': 'Error',
3610 \ })
3611 call sign_define('Other', {
3612 \ 'text': '#!',
3613 \ 'texthl': 'Error',
3614 \ 'linehl': 'Search',
3615 \ })
3616 let winid = popup_create(['hello', 'bright', 'world'], {
3617 \ 'minwidth': 20,
3618 \ })
3619 call setwinvar(winid, "&signcolumn", "yes")
3620 let winbufnr = winbufnr(winid)
3621
3622 " add sign to current buffer, shows
3623 call sign_place(1, 'Selected', 'Current', bufnr('%'), {'lnum': 1})
3624 " add sign to current buffer, does not show
3625 call sign_place(2, 'PopUpSelected', 'Other', bufnr('%'), {'lnum': 2})
3626
3627 " add sign to popup buffer, shows
3628 call sign_place(3, 'PopUpSelected', 'Other', winbufnr, {'lnum': 1})
3629 " add sign to popup buffer, does not show
3630 call sign_place(4, 'Selected', 'Current', winbufnr, {'lnum': 2})
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02003631
3632 func SetOptions()
3633 call setwinvar(g:winid, '&number', 1)
3634 call setwinvar(g:winid, '&foldcolumn', 2)
3635 call popup_settext(g:winid, 'a longer line to check the width')
3636 endfunc
Bram Moolenaar20ebbea2019-11-30 17:58:27 +01003637 END
3638 call writefile(lines, 'XtestPopupSign')
3639
3640 let buf = RunVimInTerminal('-S XtestPopupSign', #{rows: 10})
3641 call VerifyScreenDump(buf, 'Test_popupwin_sign_1', {})
3642
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02003643 " set more options to check the width is adjusted
3644 call term_sendkeys(buf, ":call SetOptions()\<CR>")
3645 call VerifyScreenDump(buf, 'Test_popupwin_sign_2', {})
3646
Bram Moolenaar20ebbea2019-11-30 17:58:27 +01003647 call StopVimInTerminal(buf)
3648 call delete('XtestPopupSign')
3649endfunc
3650
Bram Moolenaar99ebf222019-12-10 23:44:48 +01003651func Test_popupwin_bufnr()
3652 let popwin = popup_create(['blah'], #{})
3653 let popbuf = winbufnr(popwin)
3654 split asdfasdf
3655 let newbuf = bufnr()
3656 call assert_true(newbuf > popbuf, 'New buffer number is higher')
3657 call assert_equal(newbuf, bufnr('$'))
3658 call popup_clear()
3659 let popwin = popup_create(['blah'], #{})
3660 " reuses previous buffer number
3661 call assert_equal(popbuf, winbufnr(popwin))
3662 call assert_equal(newbuf, bufnr('$'))
3663
3664 call popup_clear()
3665 bwipe!
3666endfunc
3667
Bram Moolenaarec084d32020-02-28 22:44:47 +01003668func Test_popupwin_filter_input_multibyte()
3669 func MyPopupFilter(winid, c)
3670 let g:bytes = range(a:c->strlen())->map({i -> char2nr(a:c[i])})
3671 return 0
3672 endfunc
3673 let winid = popup_create('', #{mapping: 0, filter: 'MyPopupFilter'})
3674
3675 " UTF-8: E3 80 80, including K_SPECIAL(0x80)
3676 call feedkeys("\u3000", 'xt')
3677 call assert_equal([0xe3, 0x80, 0x80], g:bytes)
3678
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01003679 " UTF-8: E3 80 9B, including CSI(0x9B)
3680 call feedkeys("\u301b", 'xt')
3681 call assert_equal([0xe3, 0x80, 0x9b], g:bytes)
Bram Moolenaarec084d32020-02-28 22:44:47 +01003682
Bram Moolenaar20298ce2020-06-19 21:46:52 +02003683 if has('unix')
3684 " with modifyOtherKeys <M-S-a> does not include a modifier sequence
Bram Moolenaarb326edf2020-06-20 15:03:38 +02003685 if has('gui_running')
3686 call feedkeys("\x9b\xfc\x08A", 'Lx!')
3687 else
3688 call feedkeys("\<Esc>[27;4;65~", 'Lx!')
3689 endif
Bram Moolenaar20298ce2020-06-19 21:46:52 +02003690 call assert_equal([0xc3, 0x81], g:bytes)
3691 endif
3692
Bram Moolenaarec084d32020-02-28 22:44:47 +01003693 call popup_clear()
3694 delfunc MyPopupFilter
3695 unlet g:bytes
3696endfunc
3697
Bram Moolenaar6f8f7332020-08-10 21:19:23 +02003698func Test_popupwin_filter_close_ctrl_c()
3699 CheckScreendump
3700
3701 let lines =<< trim END
3702 vsplit
3703 set laststatus=2
3704 set statusline=%!Statusline()
3705
3706 function Statusline() abort
3707 return '%<%f %h%m%r%=%-14.(%l,%c%V%) %P'
3708 endfunction
3709
3710 call popup_create('test test test test...', {'filter': {-> 0}})
3711 END
3712 call writefile(lines, 'XtestPopupCtrlC')
3713
3714 let buf = RunVimInTerminal('-S XtestPopupCtrlC', #{rows: 10})
3715
3716 call term_sendkeys(buf, "\<C-C>")
3717 call VerifyScreenDump(buf, 'Test_popupwin_ctrl_c', {})
3718
3719 call StopVimInTerminal(buf)
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003720 call delete('XtestPopupCtrlC')
3721endfunc
3722
3723func Test_popupwin_filter_close_wrong_name()
3724 CheckScreendump
3725
3726 let lines =<< trim END
3727 call popup_create('one two three...', {'filter': 'NoSuchFunc'})
3728 END
3729 call writefile(lines, 'XtestPopupWrongName')
3730
3731 let buf = RunVimInTerminal('-S XtestPopupWrongName', #{rows: 10})
3732
3733 call term_sendkeys(buf, "j")
3734 call VerifyScreenDump(buf, 'Test_popupwin_wrong_name', {})
3735
3736 call StopVimInTerminal(buf)
3737 call delete('XtestPopupWrongName')
3738endfunc
3739
3740func Test_popupwin_filter_close_three_errors()
3741 CheckScreendump
3742
3743 let lines =<< trim END
3744 set cmdheight=2
3745 call popup_create('one two three...', {'filter': 'filter'})
3746 END
3747 call writefile(lines, 'XtestPopupThreeErrors')
3748
3749 let buf = RunVimInTerminal('-S XtestPopupThreeErrors', #{rows: 10})
3750
3751 call term_sendkeys(buf, "jj")
3752 call VerifyScreenDump(buf, 'Test_popupwin_three_errors_1', {})
3753 call term_sendkeys(buf, "j")
3754 call VerifyScreenDump(buf, 'Test_popupwin_three_errors_2', {})
3755
3756 call StopVimInTerminal(buf)
3757 call delete('XtestPopupThreeErrors')
Bram Moolenaar6f8f7332020-08-10 21:19:23 +02003758endfunc
3759
Bram Moolenaar927495b2020-11-06 17:58:35 +01003760func Test_popupwin_latin1_encoding()
3761 CheckScreendump
3762 CheckUnix
3763
3764 " When 'encoding' is a single-byte encoding a terminal window will mess up
3765 " the display. Check that showing a popup on top of that doesn't crash.
3766 let lines =<< trim END
3767 set encoding=latin1
3768 terminal cat Xmultibyte
3769 call popup_create(['one', 'two', 'three', 'four'], #{line: 1, col: 10})
Bram Moolenaar4c5bdb92020-12-17 17:45:59 +01003770 redraw
Bram Moolenaarb125b532020-12-17 21:56:09 +01003771 " wait for "cat" to finish
3772 while execute('ls!') !~ 'finished'
3773 sleep 10m
3774 endwhile
Bram Moolenaar4c5bdb92020-12-17 17:45:59 +01003775 echo "Done"
Bram Moolenaar927495b2020-11-06 17:58:35 +01003776 END
3777 call writefile(lines, 'XtestPopupLatin')
3778 call writefile([repeat("\u3042 ", 120)], 'Xmultibyte')
3779
3780 let buf = RunVimInTerminal('-S XtestPopupLatin', #{rows: 10})
Bram Moolenaar4c5bdb92020-12-17 17:45:59 +01003781 call WaitForAssert({-> assert_match('Done', term_getline(buf, 10))})
Bram Moolenaar927495b2020-11-06 17:58:35 +01003782
3783 call term_sendkeys(buf, ":q\<CR>")
3784 call StopVimInTerminal(buf)
3785 call delete('XtestPopupLatin')
3786 call delete('Xmultibyte')
3787endfunc
3788
Bram Moolenaarba2920f2020-03-06 21:43:17 +01003789func Test_popupwin_atcursor_far_right()
3790 new
3791
3792 " this was getting stuck
3793 set signcolumn=yes
3794 call setline(1, repeat('=', &columns))
3795 normal! ggg$
Bram Moolenaaref6b9792020-05-13 16:34:15 +02003796 let winid = popup_atcursor(repeat('x', 500), #{moved: 'any', border: []})
Bram Moolenaarba2920f2020-03-06 21:43:17 +01003797
Bram Moolenaar6d585f42020-07-26 22:20:54 +02003798 " 'signcolumn' was getting reset
3799 call setwinvar(winid, '&signcolumn', 'yes')
3800 call popup_setoptions(winid, {'zindex': 1000})
3801 call assert_equal('yes', getwinvar(winid, '&signcolumn'))
3802
Bram Moolenaaref6b9792020-05-13 16:34:15 +02003803 call popup_close(winid)
Bram Moolenaarba2920f2020-03-06 21:43:17 +01003804 bwipe!
3805 set signcolumn&
3806endfunc
3807
Bram Moolenaar0f1563f2020-03-20 21:15:51 +01003808func Test_popupwin_splitmove()
3809 vsplit
3810 let win2 = win_getid()
3811 let popup_winid = popup_dialog('hello', {})
3812 call assert_fails('call win_splitmove(popup_winid, win2)', 'E957:')
3813 call assert_fails('call win_splitmove(win2, popup_winid)', 'E957:')
3814
3815 call popup_clear()
3816 bwipe
3817endfunc
3818
Bram Moolenaarcbcd9cb2020-11-07 16:58:59 +01003819func Test_popupwin_exiting_terminal()
3820 CheckFeature terminal
3821
3822 " Tests that when creating a popup right after closing a terminal window does
3823 " not make the popup the current window.
3824 let winid = win_getid()
3825 try
3826 augroup Test_popupwin_exiting_terminal
3827 autocmd!
3828 autocmd WinEnter * :call popup_create('test', {})
3829 augroup END
3830 let bnr = term_start(&shell, #{term_finish: 'close'})
3831 call term_sendkeys(bnr, "exit\r\n")
3832 call WaitForAssert({-> assert_equal(winid, win_getid())})
3833 finally
3834 call popup_clear(1)
3835 augroup Test_popupwin_exiting_terminal
3836 autocmd!
3837 augroup END
3838 endtry
3839endfunc
Bram Moolenaar0f1563f2020-03-20 21:15:51 +01003840
Bram Moolenaar014f6982021-01-04 13:18:30 +01003841func Test_popup_filter_menu()
3842 let colors = ['red', 'green', 'blue']
3843 call popup_menu(colors, #{callback: {_, result -> assert_equal('green', colors[result - 1])}})
3844 call feedkeys("\<c-n>\<c-n>\<c-p>\<cr>", 'xt')
3845endfunc
3846
Bram Moolenaar65026432021-02-06 14:22:32 +01003847func Test_popup_getoptions_other_tab()
3848 new
3849 call setline(1, 'some text')
3850 call prop_type_add('textprop', {})
3851 call prop_add(1, 1, #{type: 'textprop', length: 1})
3852 let id = popup_create('TEST', #{textprop: 'textprop', highlight: 'ErrorMsg', tabpage: 1})
3853 tab sp
3854 call assert_equal(['textprop', 'textpropid', 'textpropwin'], popup_getoptions(id)->keys()->filter({_, v -> v =~ 'textprop'}))
3855
3856 tabclose
Bram Moolenaarefa19232021-02-06 14:59:27 +01003857 call popup_close(id)
Bram Moolenaar65026432021-02-06 14:22:32 +01003858 bwipe!
3859 call prop_type_delete('textprop')
3860endfunc
3861
Bram Moolenaarefa19232021-02-06 14:59:27 +01003862
3863func Test_popup_setoptions_other_tab()
3864 new Xfile
3865 let winid = win_getid()
3866 call setline(1, 'some text')
3867 call prop_type_add('textprop', {})
3868 call prop_add(1, 1, #{type: 'textprop', length: 1})
3869 let id = popup_create('TEST', #{textprop: 'textprop'})
3870 tab sp
3871 call popup_setoptions(id, #{textprop: 'textprop', textpropwin: winid})
3872 call assert_equal(winid, popup_getoptions(id).textpropwin)
3873
3874 tabclose
3875 call popup_close(id)
3876 bwipe! Xfile
3877 call prop_type_delete('textprop')
3878endfunc
3879
Bram Moolenaar82db31c2021-02-10 14:56:11 +01003880func Test_popup_prop_not_visible()
3881 CheckScreendump
3882
3883 let lines =<< trim END
3884 vim9script
3885 set nowrap stal=2
3886 rightbelow :31vnew
3887 setline(1, ['', 'some text', '', 'other text'])
3888 prop_type_add('someprop', {})
3889 prop_add(2, 9, {type: 'someprop', length: 5})
3890 popup_create('attached to "some"', {
3891 textprop: 'someprop',
3892 highlight: 'ErrorMsg',
3893 line: -1,
3894 wrap: false,
3895 fixed: true,
3896 })
3897 prop_type_add('otherprop', {})
3898 prop_add(4, 10, {type: 'otherprop', length: 5})
3899 popup_create('attached to "other"', {
3900 textprop: 'otherprop',
3901 highlight: 'ErrorMsg',
3902 line: -1,
3903 wrap: false,
3904 fixed: false,
3905 })
3906 END
3907 call writefile(lines, 'XtestPropNotVisble')
3908 let buf = RunVimInTerminal('-S XtestPropNotVisble', #{rows: 10})
3909 call VerifyScreenDump(buf, 'Test_popup_prop_not_visible_01', {})
3910
3911 call term_sendkeys(buf, ":vert resize -14\<CR>")
3912 call VerifyScreenDump(buf, 'Test_popup_prop_not_visible_02', {})
3913
3914 call term_sendkeys(buf, ":vert resize -8\<CR>")
3915 call VerifyScreenDump(buf, 'Test_popup_prop_not_visible_03', {})
3916
3917 " clean up
3918 call StopVimInTerminal(buf)
3919 call delete('XtestPropNotVisble')
3920endfunction
3921
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02003922func Test_bufdel_skips_popupwin_buffer()
3923 let id = popup_create("Some text", {})
3924 %bd
3925 call popup_close(id)
3926endfunc
3927
Bram Moolenaar82db31c2021-02-10 14:56:11 +01003928
Bram Moolenaar331bafd2019-07-20 17:46:05 +02003929" vim: shiftwidth=2 sts=2