blob: 9e35c7ffc7f3bfb8fe6a6e51725edba9ac3d17f5 [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 Moolenaara112f2d2019-09-01 17:38:09 +0200519func Test_popup_noscrolloff()
520 set scrolloff=5
521 let winid = popup_create(['xxx']->repeat(50), #{
522 \ maxheight: 5,
523 \ firstline: 11,
524 \ })
525 redraw
526 call assert_equal(11, popup_getoptions(winid).firstline)
527 call assert_equal(11, popup_getpos(winid).firstline)
528
529 call popup_setoptions(winid, #{firstline: 0})
530 call win_execute(winid, "normal! \<c-y>")
531 call assert_equal(0, popup_getoptions(winid).firstline)
532 call assert_equal(10, popup_getpos(winid).firstline)
533
534 call popup_close(winid)
535endfunc
536
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200537func Test_popup_drag()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200538 CheckScreendump
539
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200540 " create a popup that covers the command line
541 let lines =<< trim END
542 call setline(1, range(1, 20))
Bram Moolenaar356375f2019-08-24 14:46:29 +0200543 split
544 vsplit
545 $wincmd w
546 vsplit
547 1wincmd w
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200548 let winid = popup_create(['1111', '222222', '33333'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200549 \ drag: 1,
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200550 \ resize: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200551 \ border: [],
552 \ line: &lines - 4,
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200553 \ })
554 func Dragit()
555 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
556 endfunc
557 map <silent> <F3> :call test_setmouse(&lines - 4, &columns / 2)<CR>
Bram Moolenaar356375f2019-08-24 14:46:29 +0200558 map <silent> <F4> :call test_setmouse(&lines - 8, &columns / 2 - 20)<CR>
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200559 func Resize()
560 call feedkeys("\<F5>\<LeftMouse>\<F6>\<LeftDrag>\<LeftRelease>", "xt")
561 endfunc
Bram Moolenaar356375f2019-08-24 14:46:29 +0200562 map <silent> <F5> :call test_setmouse(6, 21)<CR>
563 map <silent> <F6> :call test_setmouse(7, 25)<CR>
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200564 END
565 call writefile(lines, 'XtestPopupDrag')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200566 let buf = RunVimInTerminal('-S XtestPopupDrag', #{rows: 10})
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200567 call VerifyScreenDump(buf, 'Test_popupwin_drag_01', {})
568
569 call term_sendkeys(buf, ":call Dragit()\<CR>")
570 call VerifyScreenDump(buf, 'Test_popupwin_drag_02', {})
571
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200572 call term_sendkeys(buf, ":call Resize()\<CR>")
573 call VerifyScreenDump(buf, 'Test_popupwin_drag_03', {})
574
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200575 " clean up
576 call StopVimInTerminal(buf)
577 call delete('XtestPopupDrag')
578endfunc
579
Bram Moolenaar35910f22020-07-12 19:24:10 +0200580func Test_popup_drag_termwin()
581 CheckUnix
582 CheckScreendump
583 CheckFeature terminal
584
585 " create a popup that covers the terminal window
586 let lines =<< trim END
Bram Moolenaar452143c2020-07-15 17:38:21 +0200587 set foldmethod=marker
588 call setline(1, range(100))
589 for nr in range(7)
590 call setline(nr * 12 + 1, "fold {{{")
Bram Moolenaar9d489562020-07-30 20:08:50 +0200591 call setline(nr * 12 + 11, "end }}}")
Bram Moolenaar452143c2020-07-15 17:38:21 +0200592 endfor
593 %foldclose
Bram Moolenaar35910f22020-07-12 19:24:10 +0200594 set shell=/bin/sh noruler
Bram Moolenaara4dc6f92020-07-12 19:52:36 +0200595 let $PS1 = 'vim> '
Bram Moolenaar452143c2020-07-15 17:38:21 +0200596 terminal ++rows=4
Bram Moolenaar35910f22020-07-12 19:24:10 +0200597 $wincmd w
598 let winid = popup_create(['1111', '2222'], #{
599 \ drag: 1,
600 \ resize: 1,
601 \ border: [],
602 \ line: 3,
603 \ })
Bram Moolenaar452143c2020-07-15 17:38:21 +0200604 func DragitLeft()
Bram Moolenaar35910f22020-07-12 19:24:10 +0200605 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
606 endfunc
Bram Moolenaar452143c2020-07-15 17:38:21 +0200607 func DragitDown()
608 call feedkeys("\<F4>\<LeftMouse>\<F5>\<LeftDrag>\<LeftRelease>", "xt")
609 endfunc
610 func DragitDownLeft()
611 call feedkeys("\<F5>\<LeftMouse>\<F6>\<LeftDrag>\<LeftRelease>", "xt")
612 endfunc
Bram Moolenaar35910f22020-07-12 19:24:10 +0200613 map <silent> <F3> :call test_setmouse(3, &columns / 2)<CR>
614 map <silent> <F4> :call test_setmouse(3, &columns / 2 - 20)<CR>
Bram Moolenaar452143c2020-07-15 17:38:21 +0200615 map <silent> <F5> :call test_setmouse(12, &columns / 2)<CR>
616 map <silent> <F6> :call test_setmouse(12, &columns / 2 - 20)<CR>
Bram Moolenaar35910f22020-07-12 19:24:10 +0200617 END
618 call writefile(lines, 'XtestPopupTerm')
Bram Moolenaar452143c2020-07-15 17:38:21 +0200619 let buf = RunVimInTerminal('-S XtestPopupTerm', #{rows: 16})
Bram Moolenaar35910f22020-07-12 19:24:10 +0200620 call VerifyScreenDump(buf, 'Test_popupwin_term_01', {})
621
Bram Moolenaar452143c2020-07-15 17:38:21 +0200622 call term_sendkeys(buf, ":call DragitLeft()\<CR>")
Bram Moolenaar35910f22020-07-12 19:24:10 +0200623 call VerifyScreenDump(buf, 'Test_popupwin_term_02', {})
624
Bram Moolenaar452143c2020-07-15 17:38:21 +0200625 call term_sendkeys(buf, ":call DragitDown()\<CR>")
626 call VerifyScreenDump(buf, 'Test_popupwin_term_03', {})
627
628 call term_sendkeys(buf, ":call DragitDownLeft()\<CR>")
629 call VerifyScreenDump(buf, 'Test_popupwin_term_04', {})
630
Bram Moolenaar35910f22020-07-12 19:24:10 +0200631 " clean up
632 call StopVimInTerminal(buf)
633 call delete('XtestPopupTerm')
634endfunc
635
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200636func Test_popup_close_with_mouse()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200637 CheckScreendump
638
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200639 let lines =<< trim END
640 call setline(1, range(1, 20))
641 " With border, can click on X
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200642 let winid = popup_create('foobar', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200643 \ close: 'button',
644 \ border: [],
645 \ line: 1,
646 \ col: 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200647 \ })
648 func CloseMsg(id, result)
649 echomsg 'Popup closed with ' .. a:result
650 endfunc
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200651 let winid = popup_create('notification', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200652 \ close: 'click',
653 \ line: 3,
654 \ col: 15,
655 \ callback: 'CloseMsg',
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200656 \ })
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200657 let winid = popup_create('no border here', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200658 \ close: 'button',
659 \ line: 5,
660 \ col: 3,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200661 \ })
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200662 let winid = popup_create('only padding', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200663 \ close: 'button',
664 \ padding: [],
665 \ line: 5,
666 \ col: 23,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200667 \ })
668 func CloseWithX()
669 call feedkeys("\<F3>\<LeftMouse>\<LeftRelease>", "xt")
670 endfunc
671 map <silent> <F3> :call test_setmouse(1, len('foobar') + 2)<CR>
672 func CloseWithClick()
673 call feedkeys("\<F4>\<LeftMouse>\<LeftRelease>", "xt")
674 endfunc
675 map <silent> <F4> :call test_setmouse(3, 17)<CR>
Bram Moolenaarf6396232019-08-24 19:36:00 +0200676 func CreateWithMenuFilter()
677 let winid = popup_create('barfoo', #{
678 \ close: 'button',
679 \ filter: 'popup_filter_menu',
680 \ border: [],
681 \ line: 1,
682 \ col: 40,
683 \ })
684 endfunc
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200685 END
686 call writefile(lines, 'XtestPopupClose')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200687 let buf = RunVimInTerminal('-S XtestPopupClose', #{rows: 10})
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200688 call VerifyScreenDump(buf, 'Test_popupwin_close_01', {})
689
690 call term_sendkeys(buf, ":call CloseWithX()\<CR>")
691 call VerifyScreenDump(buf, 'Test_popupwin_close_02', {})
692
693 call term_sendkeys(buf, ":call CloseWithClick()\<CR>")
694 call VerifyScreenDump(buf, 'Test_popupwin_close_03', {})
695
Bram Moolenaarf6396232019-08-24 19:36:00 +0200696 call term_sendkeys(buf, ":call CreateWithMenuFilter()\<CR>")
697 call VerifyScreenDump(buf, 'Test_popupwin_close_04', {})
698
699 " We have to send the actual mouse code, feedkeys() would be caught the
700 " filter.
701 call term_sendkeys(buf, "\<Esc>[<0;47;1M")
702 call VerifyScreenDump(buf, 'Test_popupwin_close_05', {})
703
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200704 " clean up
705 call StopVimInTerminal(buf)
706 call delete('XtestPopupClose')
707endfunction
708
Bram Moolenaar7b3d9392019-10-16 22:17:07 +0200709func Test_popup_menu_wrap()
710 CheckScreendump
711
712 let lines =<< trim END
713 call setline(1, range(1, 20))
714 call popup_create([
715 \ 'one',
716 \ 'asdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfas',
717 \ 'three',
718 \ 'four',
719 \ ], #{
720 \ pos: "botleft",
721 \ border: [],
722 \ padding: [0,1,0,1],
723 \ maxheight: 3,
724 \ cursorline: 1,
725 \ filter: 'popup_filter_menu',
726 \ })
727 END
728 call writefile(lines, 'XtestPopupWrap')
729 let buf = RunVimInTerminal('-S XtestPopupWrap', #{rows: 10})
730 call VerifyScreenDump(buf, 'Test_popupwin_wrap_1', {})
731
732 call term_sendkeys(buf, "jj")
733 call VerifyScreenDump(buf, 'Test_popupwin_wrap_2', {})
734
735 " clean up
736 call term_sendkeys(buf, "\<Esc>")
737 call StopVimInTerminal(buf)
738 call delete('XtestPopupWrap')
739endfunction
740
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200741func Test_popup_with_mask()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200742 CheckScreendump
743
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200744 let lines =<< trim END
Bram Moolenaarba45f1f2019-07-03 22:50:41 +0200745 call setline(1, repeat([join(range(1, 42), '')], 13))
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200746 hi PopupColor ctermbg=lightgrey
747 let winid = popup_create([
748 \ 'some text',
749 \ 'another line',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200750 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200751 \ line: 1,
752 \ col: 10,
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100753 \ posinvert: 0,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200754 \ wrap: 0,
755 \ fixed: 1,
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +0200756 \ scrollbar: v:false,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200757 \ zindex: 90,
758 \ padding: [],
759 \ highlight: 'PopupColor',
760 \ mask: [[1,1,1,1], [-5,-1,4,4], [7,9,2,3], [2,4,3,3]]})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200761 call popup_create([
762 \ 'xxxxxxxxx',
763 \ 'yyyyyyyyy',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200764 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200765 \ line: 3,
766 \ col: 18,
767 \ zindex: 20})
Bram Moolenaarba45f1f2019-07-03 22:50:41 +0200768 let winidb = popup_create([
769 \ 'just one line',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200770 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200771 \ line: 7,
772 \ col: 10,
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100773 \ posinvert: 0,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200774 \ wrap: 0,
775 \ fixed: 1,
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +0200776 \ scrollbar: v:false,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200777 \ close: 'button',
778 \ zindex: 90,
779 \ padding: [],
780 \ border: [],
781 \ 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 +0200782 END
783 call writefile(lines, 'XtestPopupMask')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200784 let buf = RunVimInTerminal('-S XtestPopupMask', #{rows: 13})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200785 call VerifyScreenDump(buf, 'Test_popupwin_mask_1', {})
786
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200787 call term_sendkeys(buf, ":call popup_move(winid, #{col: 11, line: 2})\<CR>")
788 call term_sendkeys(buf, ":call popup_move(winidb, #{col: 12})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200789 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200790 call VerifyScreenDump(buf, 'Test_popupwin_mask_2', {})
791
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200792 call term_sendkeys(buf, ":call popup_move(winid, #{col: 65, line: 2})\<CR>")
793 call term_sendkeys(buf, ":call popup_move(winidb, #{col: 63})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200794 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaard529ba52019-07-02 23:13:53 +0200795 call VerifyScreenDump(buf, 'Test_popupwin_mask_3', {})
796
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200797 call term_sendkeys(buf, ":call popup_move(winid, #{pos: 'topright', col: 12, line: 2})\<CR>")
798 call term_sendkeys(buf, ":call popup_move(winidb, #{pos: 'topright', col: 12})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200799 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaard529ba52019-07-02 23:13:53 +0200800 call VerifyScreenDump(buf, 'Test_popupwin_mask_4', {})
801
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200802 call term_sendkeys(buf, ":call popup_move(winid, #{pos: 'topright', col: 12, line: 11})\<CR>")
803 call term_sendkeys(buf, ":call popup_move(winidb, #{pos: 'topleft', col: 42, line: 11})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200804 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaarb4207472019-07-12 16:05:45 +0200805 call VerifyScreenDump(buf, 'Test_popupwin_mask_5', {})
806
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200807 " clean up
808 call StopVimInTerminal(buf)
809 call delete('XtestPopupMask')
810endfunc
811
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200812func Test_popup_select()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200813 CheckScreendump
814 CheckFeature clipboard_working
815
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200816 " create a popup with some text to be selected
817 let lines =<< trim END
Bram Moolenaar1755ec42019-06-15 13:13:54 +0200818 set clipboard=autoselect
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200819 call setline(1, range(1, 20))
Bram Moolenaar4dd751b2019-08-17 19:10:53 +0200820 let winid = popup_create(['the word', 'some more', 'several words here', 'invisible', '5', '6', '7'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200821 \ drag: 1,
822 \ border: [],
823 \ line: 3,
824 \ col: 10,
Bram Moolenaar4dd751b2019-08-17 19:10:53 +0200825 \ maxheight: 3,
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200826 \ })
827 func Select1()
828 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
829 endfunc
830 map <silent> <F3> :call test_setmouse(4, 15)<CR>
831 map <silent> <F4> :call test_setmouse(6, 23)<CR>
832 END
833 call writefile(lines, 'XtestPopupSelect')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200834 let buf = RunVimInTerminal('-S XtestPopupSelect', #{rows: 10})
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200835 call term_sendkeys(buf, ":call Select1()\<CR>")
836 call VerifyScreenDump(buf, 'Test_popupwin_select_01', {})
837
838 call term_sendkeys(buf, ":call popup_close(winid)\<CR>")
839 call term_sendkeys(buf, "\"*p")
Bram Moolenaar8ccabf62019-07-12 18:12:51 +0200840 " clean the command line, sometimes it still shows a command
841 call term_sendkeys(buf, ":\<esc>")
842
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200843 call VerifyScreenDump(buf, 'Test_popupwin_select_02', {})
844
845 " clean up
846 call StopVimInTerminal(buf)
847 call delete('XtestPopupSelect')
848endfunc
849
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200850func Test_popup_in_tab()
851 " default popup is local to tab, not visible when in other tab
852 let winid = popup_create("text", {})
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200853 let bufnr = winbufnr(winid)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200854 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200855 call assert_equal(0, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200856 tabnew
857 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200858 call assert_equal(1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200859 quit
860 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200861
862 call assert_equal(1, bufexists(bufnr))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200863 call popup_clear()
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200864 " buffer is gone now
865 call assert_equal(0, bufexists(bufnr))
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200866
867 " global popup is visible in any tab
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200868 let winid = popup_create("text", #{tabpage: -1})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200869 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200870 call assert_equal(-1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200871 tabnew
872 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200873 call assert_equal(-1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200874 quit
875 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200876 call popup_clear()
Bram Moolenaara3fce622019-06-20 02:31:49 +0200877
878 " create popup in other tab
879 tabnew
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200880 let winid = popup_create("text", #{tabpage: 1})
Bram Moolenaara3fce622019-06-20 02:31:49 +0200881 call assert_equal(0, popup_getpos(winid).visible)
882 call assert_equal(1, popup_getoptions(winid).tabpage)
883 quit
884 call assert_equal(1, popup_getpos(winid).visible)
885 call assert_equal(0, popup_getoptions(winid).tabpage)
886 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200887endfunc
888
889func Test_popup_valid_arguments()
Bram Moolenaaref6b9792020-05-13 16:34:15 +0200890 call assert_equal(0, len(popup_list()))
891
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200892 " Zero value is like the property wasn't there
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200893 let winid = popup_create("text", #{col: 0})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200894 let pos = popup_getpos(winid)
895 call assert_inrange(&columns / 2 - 1, &columns / 2 + 1, pos.col)
Bram Moolenaaref6b9792020-05-13 16:34:15 +0200896 call assert_equal([winid], popup_list())
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200897 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200898
899 " using cursor column has minimum value of 1
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200900 let winid = popup_create("text", #{col: 'cursor-100'})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200901 let pos = popup_getpos(winid)
902 call assert_equal(1, pos.col)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200903 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200904
905 " center
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200906 let winid = popup_create("text", #{pos: 'center'})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200907 let pos = popup_getpos(winid)
908 let around = (&columns - pos.width) / 2
909 call assert_inrange(around - 1, around + 1, pos.col)
910 let around = (&lines - pos.height) / 2
911 call assert_inrange(around - 1, around + 1, pos.line)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200912 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200913endfunc
914
915func Test_popup_invalid_arguments()
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +0200916 call assert_fails('call popup_create(666, {})', 'E86:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200917 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200918 call assert_fails('call popup_create("text", "none")', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200919 call popup_clear()
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200920 call assert_fails('call popup_create(test_null_string(), {})', 'E450:')
921 call assert_fails('call popup_create(test_null_list(), {})', 'E450:')
922 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200923
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200924 call assert_fails('call popup_create("text", #{col: "xxx"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200925 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200926 call assert_fails('call popup_create("text", #{col: "cursor8"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200927 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200928 call assert_fails('call popup_create("text", #{col: "cursor+x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200929 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200930 call assert_fails('call popup_create("text", #{col: "cursor+8x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200931 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200932
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200933 call assert_fails('call popup_create("text", #{line: "xxx"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200934 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200935 call assert_fails('call popup_create("text", #{line: "cursor8"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200936 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200937 call assert_fails('call popup_create("text", #{line: "cursor+x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200938 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200939 call assert_fails('call popup_create("text", #{line: "cursor+8x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200940 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200941
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200942 call assert_fails('call popup_create("text", #{pos: "there"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200943 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200944 call assert_fails('call popup_create("text", #{padding: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200945 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200946 call assert_fails('call popup_create("text", #{border: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200947 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200948 call assert_fails('call popup_create("text", #{borderhighlight: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200949 call popup_clear()
Bram Moolenaar403d0902019-07-17 21:37:32 +0200950 call assert_fails('call popup_create("text", #{borderhighlight: test_null_list()})', 'E714:')
951 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200952 call assert_fails('call popup_create("text", #{borderchars: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200953 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200954
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200955 call assert_fails('call popup_create([#{text: "text"}, 666], {})', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200956 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200957 call assert_fails('call popup_create([#{text: "text", props: "none"}], {})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200958 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200959 call assert_fails('call popup_create([#{text: "text", props: ["none"]}], {})', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200960 call popup_clear()
Bram Moolenaarb0992022020-01-30 14:55:42 +0100961 call assert_fails('call popup_create([#{text: "text", props: range(3)}], {})', 'E715:')
962 call popup_clear()
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200963 call assert_fails('call popup_create("text", #{mask: ["asdf"]})', 'E475:')
964 call popup_clear()
Bram Moolenaarb0992022020-01-30 14:55:42 +0100965 call assert_fails('call popup_create("text", #{mask: range(5)})', 'E475:')
966 call popup_clear()
967 call popup_create("text", #{mask: [range(4)]})
968 call popup_clear()
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200969 call assert_fails('call popup_create("text", #{mask: test_null_list()})', 'E475:')
Bram Moolenaar749fa0a2019-08-03 16:18:07 +0200970 call assert_fails('call popup_create("text", #{mapping: []})', 'E745:')
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200971 call popup_clear()
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200972 call assert_fails('call popup_create("text", #{tabpage : 4})', 'E997:')
973 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200974endfunc
975
Bram Moolenaareea16992019-05-31 17:34:48 +0200976func Test_win_execute_closing_curwin()
977 split
978 let winid = popup_create('some text', {})
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200979 call assert_fails('call win_execute(winid, winnr() .. "close")', 'E994')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200980 call popup_clear()
Bram Moolenaar49540192019-12-11 19:34:54 +0100981
982 let winid = popup_create('some text', {})
983 call assert_fails('call win_execute(winid, printf("normal! :\<C-u>call popup_close(%d)\<CR>", winid))', 'E994')
984 call popup_clear()
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200985endfunc
986
987func Test_win_execute_not_allowed()
988 let winid = popup_create('some text', {})
989 call assert_fails('call win_execute(winid, "split")', 'E994:')
990 call assert_fails('call win_execute(winid, "vsplit")', 'E994:')
991 call assert_fails('call win_execute(winid, "close")', 'E994:')
992 call assert_fails('call win_execute(winid, "bdelete")', 'E994:')
Bram Moolenaar2d247842019-06-01 17:06:25 +0200993 call assert_fails('call win_execute(winid, "bwipe!")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200994 call assert_fails('call win_execute(winid, "tabnew")', 'E994:')
995 call assert_fails('call win_execute(winid, "tabnext")', 'E994:')
996 call assert_fails('call win_execute(winid, "next")', 'E994:')
997 call assert_fails('call win_execute(winid, "rewind")', 'E994:')
Bram Moolenaar3a2505c2020-03-09 16:40:41 +0100998 call assert_fails('call win_execute(winid, "pedit filename")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200999 call assert_fails('call win_execute(winid, "buf")', 'E994:')
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01001000 call assert_fails('call win_execute(winid, "bnext")', 'E994:')
1001 call assert_fails('call win_execute(winid, "bprev")', 'E994:')
1002 call assert_fails('call win_execute(winid, "bfirst")', 'E994:')
1003 call assert_fails('call win_execute(winid, "blast")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001004 call assert_fails('call win_execute(winid, "edit")', 'E994:')
1005 call assert_fails('call win_execute(winid, "enew")', 'E994:')
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +02001006 call assert_fails('call win_execute(winid, "help")', 'E994:')
1007 call assert_fails('call win_execute(winid, "1only")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001008 call assert_fails('call win_execute(winid, "wincmd x")', 'E994:')
1009 call assert_fails('call win_execute(winid, "wincmd w")', 'E994:')
1010 call assert_fails('call win_execute(winid, "wincmd t")', 'E994:')
1011 call assert_fails('call win_execute(winid, "wincmd b")', 'E994:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001012 call popup_clear()
Bram Moolenaareea16992019-05-31 17:34:48 +02001013endfunc
1014
Bram Moolenaar402502d2019-05-30 22:07:36 +02001015func Test_popup_with_wrap()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001016 CheckScreendump
1017
Bram Moolenaar402502d2019-05-30 22:07:36 +02001018 let lines =<< trim END
1019 call setline(1, range(1, 100))
1020 let winid = popup_create(
1021 \ 'a long line that wont fit',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001022 \ #{line: 3, col: 20, maxwidth: 10, wrap: 1})
Bram Moolenaar402502d2019-05-30 22:07:36 +02001023 END
1024 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001025 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar402502d2019-05-30 22:07:36 +02001026 call VerifyScreenDump(buf, 'Test_popupwin_wrap', {})
1027
1028 " clean up
1029 call StopVimInTerminal(buf)
1030 call delete('XtestPopup')
1031endfunc
1032
1033func Test_popup_without_wrap()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001034 CheckScreendump
1035
Bram Moolenaar402502d2019-05-30 22:07:36 +02001036 let lines =<< trim END
1037 call setline(1, range(1, 100))
1038 let winid = popup_create(
1039 \ 'a long line that wont fit',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001040 \ #{line: 3, col: 20, maxwidth: 10, wrap: 0})
Bram Moolenaar402502d2019-05-30 22:07:36 +02001041 END
1042 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001043 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar402502d2019-05-30 22:07:36 +02001044 call VerifyScreenDump(buf, 'Test_popupwin_nowrap', {})
1045
1046 " clean up
1047 call StopVimInTerminal(buf)
1048 call delete('XtestPopup')
1049endfunc
1050
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001051func Test_popup_with_showbreak()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001052 CheckScreendump
1053
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001054 let lines =<< trim END
1055 set showbreak=>>\
1056 call setline(1, range(1, 20))
1057 let winid = popup_dialog(
Bram Moolenaar8ae54372019-09-15 18:11:16 +02001058 \ 'a long line here that wraps',
1059 \ #{filter: 'popup_filter_yesno',
1060 \ maxwidth: 12})
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001061 END
1062 call writefile(lines, 'XtestPopupShowbreak')
1063 let buf = RunVimInTerminal('-S XtestPopupShowbreak', #{rows: 10})
1064 call VerifyScreenDump(buf, 'Test_popupwin_showbreak', {})
1065
1066 " clean up
1067 call term_sendkeys(buf, "y")
1068 call StopVimInTerminal(buf)
1069 call delete('XtestPopupShowbreak')
1070endfunc
1071
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001072func Test_popup_time()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001073 CheckFeature timers
1074
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001075 topleft vnew
1076 call setline(1, 'hello')
1077
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001078 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001079 \ line: 1,
1080 \ col: 1,
1081 \ minwidth: 20,
1082 \ time: 500,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001083 \})
1084 redraw
1085 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1086 call assert_equal('world', line)
1087
Bram Moolenaarb4f06282019-07-12 21:07:54 +02001088 call assert_equal(winid, popup_locate(1, 1))
1089 call assert_equal(winid, popup_locate(1, 20))
1090 call assert_equal(0, popup_locate(1, 21))
1091 call assert_equal(0, popup_locate(2, 1))
1092
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001093 sleep 700m
Bram Moolenaar35d5af62019-05-26 20:44:10 +02001094 redraw
Bram Moolenaar196b4662019-09-06 21:34:30 +02001095 let line = join(map(range(1, 5), '1->screenstring(v:val)'), '')
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001096 call assert_equal('hello', line)
1097
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001098 call popup_create('on the command line', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001099 \ line: &lines,
1100 \ col: 10,
1101 \ minwidth: 20,
1102 \ time: 500,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001103 \})
1104 redraw
1105 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
1106 call assert_match('.*on the command line.*', line)
1107
1108 sleep 700m
1109 redraw
1110 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
1111 call assert_notmatch('.*on the command line.*', line)
1112
1113 bwipe!
1114endfunc
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001115
1116func Test_popup_hide()
1117 topleft vnew
1118 call setline(1, 'hello')
1119
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001120 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001121 \ line: 1,
1122 \ col: 1,
1123 \ minwidth: 20,
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001124 \})
1125 redraw
1126 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1127 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001128 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001129 " buffer is still listed and active
1130 call assert_match(winbufnr(winid) .. 'u a.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001131
1132 call popup_hide(winid)
1133 redraw
1134 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1135 call assert_equal('hello', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001136 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001137 " buffer is still listed but hidden
Bram Moolenaarca7c0782020-01-14 20:42:48 +01001138 call assert_match(winbufnr(winid) .. 'u a.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001139
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001140 eval winid->popup_show()
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001141 redraw
1142 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1143 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001144 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001145
1146
1147 call popup_close(winid)
1148 redraw
1149 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1150 call assert_equal('hello', line)
1151
1152 " error is given for existing non-popup window
1153 call assert_fails('call popup_hide(win_getid())', 'E993:')
1154
1155 " no error non-existing window
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001156 eval 1234234->popup_hide()
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001157 call popup_show(41234234)
1158
1159 bwipe!
1160endfunc
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001161
1162func Test_popup_move()
1163 topleft vnew
1164 call setline(1, 'hello')
1165
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001166 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001167 \ line: 1,
1168 \ col: 1,
1169 \ minwidth: 20,
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001170 \})
1171 redraw
1172 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
1173 call assert_equal('world ', line)
1174
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001175 call popup_move(winid, #{line: 2, col: 2})
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001176 redraw
1177 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
1178 call assert_equal('hello ', line)
1179 let line = join(map(range(1, 6), 'screenstring(2, v:val)'), '')
1180 call assert_equal('~world', line)
1181
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001182 eval winid->popup_move(#{line: 1})
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001183 redraw
1184 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
1185 call assert_equal('hworld', line)
1186
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02001187 call assert_fails('call popup_move(winid, [])', 'E715:')
1188 call assert_fails('call popup_move(winid, test_null_dict())', 'E715:')
1189
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001190 call popup_close(winid)
1191
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02001192 call assert_equal(0, popup_move(-1, {}))
1193
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001194 bwipe!
1195endfunc
Bram Moolenaarbc133542019-05-29 20:26:46 +02001196
Bram Moolenaar402502d2019-05-30 22:07:36 +02001197func Test_popup_getpos()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001198 let winid = popup_create('hello', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001199 \ line: 2,
1200 \ col: 3,
1201 \ minwidth: 10,
1202 \ minheight: 11,
Bram Moolenaarbc133542019-05-29 20:26:46 +02001203 \})
1204 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +02001205 let res = popup_getpos(winid)
Bram Moolenaarbc133542019-05-29 20:26:46 +02001206 call assert_equal(2, res.line)
1207 call assert_equal(3, res.col)
1208 call assert_equal(10, res.width)
1209 call assert_equal(11, res.height)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001210 call assert_equal(1, res.visible)
Bram Moolenaarbc133542019-05-29 20:26:46 +02001211
1212 call popup_close(winid)
1213endfunc
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001214
1215func Test_popup_width_longest()
1216 let tests = [
1217 \ [['hello', 'this', 'window', 'displays', 'all of its text'], 15],
1218 \ [['hello', 'this', 'window', 'all of its text', 'displays'], 15],
1219 \ [['hello', 'this', 'all of its text', 'window', 'displays'], 15],
1220 \ [['hello', 'all of its text', 'this', 'window', 'displays'], 15],
1221 \ [['all of its text', 'hello', 'this', 'window', 'displays'], 15],
1222 \ ]
1223
1224 for test in tests
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001225 let winid = popup_create(test[0], #{line: 2, col: 3})
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001226 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +02001227 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001228 call assert_equal(test[1], position.width)
1229 call popup_close(winid)
1230 endfor
1231endfunc
1232
1233func Test_popup_wraps()
1234 let tests = [
1235 \ ['nowrap', 6, 1],
1236 \ ['a line that wraps once', 12, 2],
1237 \ ['a line that wraps two times', 12, 3],
1238 \ ]
1239 for test in tests
1240 let winid = popup_create(test[0],
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001241 \ #{line: 2, col: 3, maxwidth: 12})
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001242 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +02001243 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001244 call assert_equal(test[1], position.width)
1245 call assert_equal(test[2], position.height)
1246
1247 call popup_close(winid)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001248 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001249 endfor
1250endfunc
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001251
1252func Test_popup_getoptions()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001253 let winid = popup_create('hello', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001254 \ line: 2,
1255 \ col: 3,
1256 \ minwidth: 10,
1257 \ minheight: 11,
1258 \ maxwidth: 20,
1259 \ maxheight: 21,
1260 \ zindex: 100,
1261 \ time: 5000,
1262 \ fixed: 1
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001263 \})
1264 redraw
1265 let res = popup_getoptions(winid)
1266 call assert_equal(2, res.line)
1267 call assert_equal(3, res.col)
1268 call assert_equal(10, res.minwidth)
1269 call assert_equal(11, res.minheight)
1270 call assert_equal(20, res.maxwidth)
1271 call assert_equal(21, res.maxheight)
1272 call assert_equal(100, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001273 call assert_equal(1, res.fixed)
Bram Moolenaarb8350ab2019-08-04 17:59:49 +02001274 call assert_equal(1, res.mapping)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001275 if has('timers')
1276 call assert_equal(5000, res.time)
1277 endif
1278 call popup_close(winid)
1279
1280 let winid = popup_create('hello', {})
1281 redraw
1282 let res = popup_getoptions(winid)
1283 call assert_equal(0, res.line)
1284 call assert_equal(0, res.col)
1285 call assert_equal(0, res.minwidth)
1286 call assert_equal(0, res.minheight)
1287 call assert_equal(0, res.maxwidth)
1288 call assert_equal(0, res.maxheight)
1289 call assert_equal(50, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001290 call assert_equal(0, res.fixed)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001291 if has('timers')
1292 call assert_equal(0, res.time)
1293 endif
1294 call popup_close(winid)
1295 call assert_equal({}, popup_getoptions(winid))
1296endfunc
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001297
1298func Test_popup_option_values()
1299 new
1300 " window-local
1301 setlocal number
1302 setlocal nowrap
1303 " buffer-local
1304 setlocal omnifunc=Something
1305 " global/buffer-local
1306 setlocal path=/there
1307 " global/window-local
Bram Moolenaara112f2d2019-09-01 17:38:09 +02001308 setlocal statusline=2
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001309
1310 let winid = popup_create('hello', {})
1311 call assert_equal(0, getwinvar(winid, '&number'))
1312 call assert_equal(1, getwinvar(winid, '&wrap'))
1313 call assert_equal('', getwinvar(winid, '&omnifunc'))
1314 call assert_equal(&g:path, getwinvar(winid, '&path'))
Bram Moolenaara112f2d2019-09-01 17:38:09 +02001315 call assert_equal(&g:statusline, getwinvar(winid, '&statusline'))
1316
1317 " 'scrolloff' is reset to zero
1318 call assert_equal(5, &scrolloff)
1319 call assert_equal(0, getwinvar(winid, '&scrolloff'))
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001320
1321 call popup_close(winid)
1322 bwipe
1323endfunc
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001324
1325func Test_popup_atcursor()
1326 topleft vnew
1327 call setline(1, [
1328 \ 'xxxxxxxxxxxxxxxxx',
1329 \ 'xxxxxxxxxxxxxxxxx',
1330 \ 'xxxxxxxxxxxxxxxxx',
1331 \])
1332
1333 call cursor(2, 2)
1334 redraw
1335 let winid = popup_atcursor('vim', {})
1336 redraw
1337 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
1338 call assert_equal('xvimxxxxxxxxxxxxx', line)
1339 call popup_close(winid)
1340
1341 call cursor(3, 4)
1342 redraw
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001343 let winid = 'vim'->popup_atcursor({})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001344 redraw
1345 let line = join(map(range(1, 17), 'screenstring(2, v:val)'), '')
1346 call assert_equal('xxxvimxxxxxxxxxxx', line)
1347 call popup_close(winid)
1348
1349 call cursor(1, 1)
1350 redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001351 let winid = popup_create('vim', #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001352 \ line: 'cursor+2',
1353 \ col: 'cursor+1',
1354 \})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001355 redraw
1356 let line = join(map(range(1, 17), 'screenstring(3, v:val)'), '')
1357 call assert_equal('xvimxxxxxxxxxxxxx', line)
1358 call popup_close(winid)
1359
1360 call cursor(3, 3)
1361 redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001362 let winid = popup_create('vim', #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001363 \ line: 'cursor-2',
1364 \ col: 'cursor-1',
1365 \})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001366 redraw
1367 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
1368 call assert_equal('xvimxxxxxxxxxxxxx', line)
1369 call popup_close(winid)
1370
Bram Moolenaar402502d2019-05-30 22:07:36 +02001371 " just enough room above
1372 call cursor(3, 3)
1373 redraw
1374 let winid = popup_atcursor(['vim', 'is great'], {})
1375 redraw
1376 let pos = popup_getpos(winid)
1377 call assert_equal(1, pos.line)
1378 call popup_close(winid)
1379
1380 " not enough room above, popup goes below the cursor
1381 call cursor(3, 3)
1382 redraw
1383 let winid = popup_atcursor(['vim', 'is', 'great'], {})
1384 redraw
1385 let pos = popup_getpos(winid)
1386 call assert_equal(4, pos.line)
1387 call popup_close(winid)
1388
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001389 " cursor in first line, popup in line 2
1390 call cursor(1, 1)
1391 redraw
1392 let winid = popup_atcursor(['vim', 'is', 'great'], {})
1393 redraw
1394 let pos = popup_getpos(winid)
1395 call assert_equal(2, pos.line)
1396 call popup_close(winid)
1397
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001398 bwipe!
1399endfunc
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001400
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001401func Test_popup_atcursor_pos()
1402 CheckScreendump
1403
1404 let lines =<< trim END
1405 call setline(1, repeat([repeat('-', 60)], 15))
1406 set so=0
1407
1408 normal 9G3|r#
1409 let winid1 = popup_atcursor(['first', 'second'], #{
1410 \ moved: [0, 0, 0],
1411 \ })
1412 normal 9G21|r&
1413 let winid1 = popup_atcursor(['FIrsT', 'SEcoND'], #{
1414 \ pos: 'botright',
1415 \ moved: [0, 0, 0],
1416 \ })
1417 normal 3G27|r%
1418 let winid1 = popup_atcursor(['fiRSt', 'seCOnd'], #{
1419 \ pos: 'topleft',
1420 \ moved: [0, 0, 0],
1421 \ })
1422 normal 3G45|r@
1423 let winid1 = popup_atcursor(['First', 'SeconD'], #{
1424 \ pos: 'topright',
Bram Moolenaarb0992022020-01-30 14:55:42 +01001425 \ moved: range(3),
1426 \ mousemoved: range(3),
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001427 \ })
1428 END
1429 call writefile(lines, 'XtestPopupAtcursorPos')
1430 let buf = RunVimInTerminal('-S XtestPopupAtcursorPos', #{rows: 12})
1431 call VerifyScreenDump(buf, 'Test_popupwin_atcursor_pos', {})
1432
1433 " clean up
1434 call StopVimInTerminal(buf)
1435 call delete('XtestPopupAtcursorPos')
1436endfunc
1437
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001438func Test_popup_beval()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001439 CheckScreendump
Bram Moolenaar1e82a782019-09-21 22:57:06 +02001440 CheckFeature balloon_eval_term
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001441
1442 let lines =<< trim END
1443 call setline(1, range(1, 20))
1444 call setline(5, 'here is some text to hover over')
1445 set balloonevalterm
1446 set balloonexpr=BalloonExpr()
1447 set balloondelay=100
1448 func BalloonExpr()
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001449 let s:winid = [v:beval_text]->popup_beval({})
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001450 return ''
1451 endfunc
1452 func Hover()
1453 call test_setmouse(5, 15)
1454 call feedkeys("\<MouseMove>\<Ignore>", "xt")
1455 sleep 100m
1456 endfunc
1457 func MoveOntoPopup()
1458 call test_setmouse(4, 17)
1459 call feedkeys("\<F4>\<MouseMove>\<Ignore>", "xt")
1460 endfunc
1461 func MoveAway()
1462 call test_setmouse(5, 13)
1463 call feedkeys("\<F5>\<MouseMove>\<Ignore>", "xt")
1464 endfunc
1465 END
1466 call writefile(lines, 'XtestPopupBeval')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001467 let buf = RunVimInTerminal('-S XtestPopupBeval', #{rows: 10})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001468 call TermWait(buf, 50)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001469 call term_sendkeys(buf, 'j')
1470 call term_sendkeys(buf, ":call Hover()\<CR>")
1471 call VerifyScreenDump(buf, 'Test_popupwin_beval_1', {})
1472
1473 call term_sendkeys(buf, ":call MoveOntoPopup()\<CR>")
1474 call VerifyScreenDump(buf, 'Test_popupwin_beval_2', {})
1475
1476 call term_sendkeys(buf, ":call MoveAway()\<CR>")
1477 call VerifyScreenDump(buf, 'Test_popupwin_beval_3', {})
1478
1479 " clean up
1480 call StopVimInTerminal(buf)
1481 call delete('XtestPopupBeval')
1482endfunc
1483
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001484func Test_popup_filter()
1485 new
1486 call setline(1, 'some text')
1487
1488 func MyPopupFilter(winid, c)
Bram Moolenaarec084d32020-02-28 22:44:47 +01001489 if a:c == 'e' || a:c == "\<F9>"
1490 let g:eaten = a:c
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001491 return 1
1492 endif
1493 if a:c == '0'
1494 let g:ignored = '0'
1495 return 0
1496 endif
1497 if a:c == 'x'
1498 call popup_close(a:winid)
1499 return 1
1500 endif
1501 return 0
1502 endfunc
1503
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001504 let winid = 'something'->popup_create(#{filter: 'MyPopupFilter'})
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001505 redraw
1506
1507 " e is consumed by the filter
1508 call feedkeys('e', 'xt')
1509 call assert_equal('e', g:eaten)
Bram Moolenaarec084d32020-02-28 22:44:47 +01001510 call feedkeys("\<F9>", 'xt')
1511 call assert_equal("\<F9>", g:eaten)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001512
1513 " 0 is ignored by the filter
1514 normal $
1515 call assert_equal(9, getcurpos()[2])
1516 call feedkeys('0', 'xt')
1517 call assert_equal('0', g:ignored)
1518 call assert_equal(1, getcurpos()[2])
1519
1520 " x closes the popup
1521 call feedkeys('x', 'xt')
Bram Moolenaarec084d32020-02-28 22:44:47 +01001522 call assert_equal("\<F9>", g:eaten)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001523 call assert_equal(-1, winbufnr(winid))
1524
1525 delfunc MyPopupFilter
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001526 call popup_clear()
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001527endfunc
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001528
Bram Moolenaara42d9452019-06-15 21:46:30 +02001529func ShowDialog(key, result)
1530 let s:cb_res = 999
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001531 let winid = popup_dialog('do you want to quit (Yes/no)?', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001532 \ filter: 'popup_filter_yesno',
1533 \ callback: 'QuitCallback',
Bram Moolenaara42d9452019-06-15 21:46:30 +02001534 \ })
1535 redraw
1536 call feedkeys(a:key, "xt")
1537 call assert_equal(winid, s:cb_winid)
1538 call assert_equal(a:result, s:cb_res)
1539endfunc
1540
1541func Test_popup_dialog()
1542 func QuitCallback(id, res)
1543 let s:cb_winid = a:id
1544 let s:cb_res = a:res
1545 endfunc
1546
1547 let winid = ShowDialog("y", 1)
1548 let winid = ShowDialog("Y", 1)
1549 let winid = ShowDialog("n", 0)
1550 let winid = ShowDialog("N", 0)
1551 let winid = ShowDialog("x", 0)
1552 let winid = ShowDialog("X", 0)
1553 let winid = ShowDialog("\<Esc>", 0)
1554 let winid = ShowDialog("\<C-C>", -1)
1555
1556 delfunc QuitCallback
1557endfunc
1558
Bram Moolenaara730e552019-06-16 19:05:31 +02001559func ShowMenu(key, result)
1560 let s:cb_res = 999
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001561 let winid = popup_menu(['one', 'two', 'something else'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001562 \ callback: 'QuitCallback',
Bram Moolenaara730e552019-06-16 19:05:31 +02001563 \ })
1564 redraw
1565 call feedkeys(a:key, "xt")
1566 call assert_equal(winid, s:cb_winid)
1567 call assert_equal(a:result, s:cb_res)
1568endfunc
1569
1570func Test_popup_menu()
1571 func QuitCallback(id, res)
1572 let s:cb_winid = a:id
1573 let s:cb_res = a:res
1574 endfunc
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001575 " mapping won't be used in popup
1576 map j k
Bram Moolenaara730e552019-06-16 19:05:31 +02001577
1578 let winid = ShowMenu(" ", 1)
1579 let winid = ShowMenu("j \<CR>", 2)
1580 let winid = ShowMenu("JjK \<CR>", 2)
1581 let winid = ShowMenu("jjjjjj ", 3)
1582 let winid = ShowMenu("kkk ", 1)
1583 let winid = ShowMenu("x", -1)
1584 let winid = ShowMenu("X", -1)
1585 let winid = ShowMenu("\<Esc>", -1)
1586 let winid = ShowMenu("\<C-C>", -1)
1587
1588 delfunc QuitCallback
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001589 unmap j
Bram Moolenaara730e552019-06-16 19:05:31 +02001590endfunc
1591
1592func Test_popup_menu_screenshot()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001593 CheckScreendump
Bram Moolenaara730e552019-06-16 19:05:31 +02001594
1595 let lines =<< trim END
1596 call setline(1, range(1, 20))
1597 hi PopupSelected ctermbg=lightblue
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001598 call popup_menu(['one', 'two', 'another'], #{callback: 'MenuDone', title: ' make a choice from the list '})
Bram Moolenaara730e552019-06-16 19:05:31 +02001599 func MenuDone(id, res)
1600 echomsg "selected " .. a:res
1601 endfunc
1602 END
1603 call writefile(lines, 'XtestPopupMenu')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001604 let buf = RunVimInTerminal('-S XtestPopupMenu', #{rows: 10})
Bram Moolenaara730e552019-06-16 19:05:31 +02001605 call VerifyScreenDump(buf, 'Test_popupwin_menu_01', {})
1606
1607 call term_sendkeys(buf, "jj")
1608 call VerifyScreenDump(buf, 'Test_popupwin_menu_02', {})
1609
1610 call term_sendkeys(buf, " ")
1611 call VerifyScreenDump(buf, 'Test_popupwin_menu_03', {})
1612
1613 " clean up
1614 call StopVimInTerminal(buf)
1615 call delete('XtestPopupMenu')
1616endfunc
1617
Bram Moolenaarf914a332019-07-20 15:09:56 +02001618func Test_popup_menu_narrow()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001619 CheckScreendump
Bram Moolenaarf914a332019-07-20 15:09:56 +02001620
1621 let lines =<< trim END
1622 call setline(1, range(1, 20))
1623 hi PopupSelected ctermbg=green
1624 call popup_menu(['one', 'two', 'three'], #{callback: 'MenuDone'})
1625 func MenuDone(id, res)
1626 echomsg "selected " .. a:res
1627 endfunc
1628 END
1629 call writefile(lines, 'XtestPopupNarrowMenu')
1630 let buf = RunVimInTerminal('-S XtestPopupNarrowMenu', #{rows: 10})
1631 call VerifyScreenDump(buf, 'Test_popupwin_menu_04', {})
1632
1633 " clean up
1634 call term_sendkeys(buf, "x")
1635 call StopVimInTerminal(buf)
1636 call delete('XtestPopupNarrowMenu')
1637endfunc
1638
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001639func Test_popup_title()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001640 CheckScreendump
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001641
1642 " Create a popup without title or border, a line of padding will be added to
1643 " put the title on.
1644 let lines =<< trim END
1645 call setline(1, range(1, 20))
Bram Moolenaar5d458a72019-08-04 21:12:15 +02001646 let winid = popup_create(['one', 'two', 'another'], #{title: 'Title String'})
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001647 END
1648 call writefile(lines, 'XtestPopupTitle')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001649 let buf = RunVimInTerminal('-S XtestPopupTitle', #{rows: 10})
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001650 call VerifyScreenDump(buf, 'Test_popupwin_title', {})
1651
Bram Moolenaar5d458a72019-08-04 21:12:15 +02001652 call term_sendkeys(buf, ":call popup_setoptions(winid, #{maxwidth: 20, title: 'a very long title that is not going to fit'})\<CR>")
1653 call term_sendkeys(buf, ":\<CR>")
1654 call VerifyScreenDump(buf, 'Test_popupwin_longtitle_1', {})
1655
1656 call term_sendkeys(buf, ":call popup_setoptions(winid, #{border: []})\<CR>")
1657 call term_sendkeys(buf, ":\<CR>")
1658 call VerifyScreenDump(buf, 'Test_popupwin_longtitle_2', {})
1659
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001660 " clean up
1661 call StopVimInTerminal(buf)
1662 call delete('XtestPopupTitle')
Bram Moolenaarae943152019-06-16 22:54:14 +02001663
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001664 let winid = popup_create('something', #{title: 'Some Title'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001665 call assert_equal('Some Title', popup_getoptions(winid).title)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001666 call popup_setoptions(winid, #{title: 'Another Title'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001667 call assert_equal('Another Title', popup_getoptions(winid).title)
1668
1669 call popup_clear()
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001670endfunc
1671
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001672func Test_popup_close_callback()
1673 func PopupDone(id, result)
1674 let g:result = a:result
1675 endfunc
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001676 let winid = popup_create('something', #{callback: 'PopupDone'})
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001677 redraw
1678 call popup_close(winid, 'done')
1679 call assert_equal('done', g:result)
1680endfunc
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001681
1682func Test_popup_empty()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001683 let winid = popup_create('', #{padding: [2,2,2,2]})
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001684 redraw
1685 let pos = popup_getpos(winid)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001686 call assert_equal(5, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001687 call assert_equal(5, pos.height)
Bram Moolenaaref6b9792020-05-13 16:34:15 +02001688 call popup_close(winid)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001689
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001690 let winid = popup_create([], #{border: []})
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001691 redraw
1692 let pos = popup_getpos(winid)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001693 call assert_equal(3, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001694 call assert_equal(3, pos.height)
Bram Moolenaaref6b9792020-05-13 16:34:15 +02001695 call popup_close(winid)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001696endfunc
Bram Moolenaar988c4332019-06-02 14:12:11 +02001697
1698func Test_popup_never_behind()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001699 CheckScreendump
1700
Bram Moolenaar988c4332019-06-02 14:12:11 +02001701 " +-----------------------------+
1702 " | | |
1703 " | | |
1704 " | | |
1705 " | line1 |
1706 " |------------line2------------|
1707 " | line3 |
1708 " | line4 |
1709 " | |
1710 " | |
1711 " +-----------------------------+
1712 let lines =<< trim END
Bram Moolenaar988c4332019-06-02 14:12:11 +02001713 split
1714 vsplit
1715 let info_window1 = getwininfo()[0]
1716 let line = info_window1['height']
1717 let col = info_window1['width']
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001718 call popup_create(['line1', 'line2', 'line3', 'line4'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001719 \ line : line,
1720 \ col : col,
Bram Moolenaar988c4332019-06-02 14:12:11 +02001721 \ })
1722 END
1723 call writefile(lines, 'XtestPopupBehind')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001724 let buf = RunVimInTerminal('-S XtestPopupBehind', #{rows: 10})
Bram Moolenaar988c4332019-06-02 14:12:11 +02001725 call term_sendkeys(buf, "\<C-W>w")
1726 call VerifyScreenDump(buf, 'Test_popupwin_behind', {})
1727
1728 " clean up
1729 call StopVimInTerminal(buf)
1730 call delete('XtestPopupBehind')
1731endfunc
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001732
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001733func s:VerifyPosition(p, msg, line, col, width, height)
1734 call assert_equal(a:line, popup_getpos(a:p).line, a:msg . ' (l)')
1735 call assert_equal(a:col, popup_getpos(a:p).col, a:msg . ' (c)')
1736 call assert_equal(a:width, popup_getpos(a:p).width, a:msg . ' (w)')
1737 call assert_equal(a:height, popup_getpos(a:p).height, a:msg . ' (h)')
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001738endfunc
1739
1740func Test_popup_position_adjust()
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001741 " Anything placed past the last cell on the right of the screen is moved to
1742 " the left.
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001743 "
1744 " When wrapping is disabled, we also shift to the left to display on the
1745 " screen, unless fixed is set.
1746
1747 " Entries for cases which don't vary based on wrapping.
1748 " Format is per tests described below
1749 let both_wrap_tests = [
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001750 \ ['a', 5, &columns, 5, &columns, 1, 1],
1751 \ ['b', 5, &columns + 1, 5, &columns, 1, 1],
1752 \ ['c', 5, &columns - 1, 5, &columns - 1, 1, 1],
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001753 \ ['d', 5, &columns - 2, 5, &columns - 2, 1, 1],
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001754 \ ['e', 5, &columns - 3, 5, &columns - 3, 1, 1]]
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001755
1756 " these test groups are dicts with:
1757 " - comment: something to identify the group of tests by
1758 " - options: dict of options to merge with the row/col in tests
1759 " - tests: list of cases. Each one is a list with elements:
1760 " - text
1761 " - row
1762 " - col
1763 " - expected row
1764 " - expected col
1765 " - expected width
1766 " - expected height
1767 let tests = [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001768 \ #{
1769 \ comment: 'left-aligned with wrapping',
1770 \ options: #{
1771 \ wrap: 1,
1772 \ pos: 'botleft',
1773 \ },
1774 \ tests: both_wrap_tests + [
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001775 \ ['aa', 5, &columns, 4, &columns, 1, 2],
1776 \ ['bb', 5, &columns + 1, 4, &columns, 1, 2],
1777 \ ['cc', 5, &columns - 1, 5, &columns - 1, 2, 1],
1778 \ ['dd', 5, &columns - 2, 5, &columns - 2, 2, 1],
1779 \ ['ee', 5, &columns - 3, 5, &columns - 3, 2, 1],
1780 \
1781 \ ['aaa', 5, &columns, 3, &columns, 1, 3],
1782 \ ['bbb', 5, &columns + 1, 3, &columns, 1, 3],
1783 \ ['ccc', 5, &columns - 1, 4, &columns - 1, 2, 2],
1784 \ ['ddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
1785 \ ['eee', 5, &columns - 3, 5, &columns - 3, 3, 1],
1786 \
1787 \ ['aaaa', 5, &columns, 2, &columns, 1, 4],
1788 \ ['bbbb', 5, &columns + 1, 2, &columns, 1, 4],
1789 \ ['cccc', 5, &columns - 1, 4, &columns - 1, 2, 2],
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001790 \ ['dddd', 5, &columns - 2, 4, &columns - 2, 3, 2],
1791 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001792 \ ['eeee', 5, &columns - 4, 5, &columns - 4, 4, 1],
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001793 \ ],
1794 \ },
1795 \ #{
1796 \ comment: 'left aligned without wrapping',
1797 \ options: #{
1798 \ wrap: 0,
1799 \ pos: 'botleft',
1800 \ },
1801 \ tests: both_wrap_tests + [
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001802 \ ['aa', 5, &columns, 5, &columns - 1, 2, 1],
1803 \ ['bb', 5, &columns + 1, 5, &columns - 1, 2, 1],
1804 \ ['cc', 5, &columns - 1, 5, &columns - 1, 2, 1],
1805 \ ['dd', 5, &columns - 2, 5, &columns - 2, 2, 1],
1806 \ ['ee', 5, &columns - 3, 5, &columns - 3, 2, 1],
1807 \
1808 \ ['aaa', 5, &columns, 5, &columns - 2, 3, 1],
1809 \ ['bbb', 5, &columns + 1, 5, &columns - 2, 3, 1],
1810 \ ['ccc', 5, &columns - 1, 5, &columns - 2, 3, 1],
1811 \ ['ddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
1812 \ ['eee', 5, &columns - 3, 5, &columns - 3, 3, 1],
1813 \
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001814 \ ['aaaa', 5, &columns, 5, &columns - 3, 4, 1],
1815 \ ['bbbb', 5, &columns + 1, 5, &columns - 3, 4, 1],
1816 \ ['cccc', 5, &columns - 1, 5, &columns - 3, 4, 1],
1817 \ ['dddd', 5, &columns - 2, 5, &columns - 3, 4, 1],
1818 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
1819 \ ],
1820 \ },
1821 \ #{
1822 \ comment: 'left aligned with fixed position',
1823 \ options: #{
1824 \ wrap: 0,
1825 \ fixed: 1,
1826 \ pos: 'botleft',
1827 \ },
1828 \ tests: both_wrap_tests + [
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001829 \ ['aa', 5, &columns, 5, &columns, 1, 1],
1830 \ ['bb', 5, &columns + 1, 5, &columns, 1, 1],
1831 \ ['cc', 5, &columns - 1, 5, &columns - 1, 2, 1],
1832 \ ['dd', 5, &columns - 2, 5, &columns - 2, 2, 1],
1833 \ ['ee', 5, &columns - 3, 5, &columns - 3, 2, 1],
1834 \
1835 \ ['aaa', 5, &columns, 5, &columns, 1, 1],
1836 \ ['bbb', 5, &columns + 1, 5, &columns, 1, 1],
1837 \ ['ccc', 5, &columns - 1, 5, &columns - 1, 2, 1],
1838 \ ['ddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
1839 \ ['eee', 5, &columns - 3, 5, &columns - 3, 3, 1],
1840 \
1841 \ ['aaaa', 5, &columns, 5, &columns, 1, 1],
1842 \ ['bbbb', 5, &columns + 1, 5, &columns, 1, 1],
1843 \ ['cccc', 5, &columns - 1, 5, &columns - 1, 2, 1],
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001844 \ ['dddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
1845 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
1846 \ ],
1847 \ },
1848 \ ]
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001849
1850 for test_group in tests
1851 for test in test_group.tests
1852 let [ text, line, col, e_line, e_col, e_width, e_height ] = test
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001853 let options = #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001854 \ line: line,
1855 \ col: col,
1856 \ }
1857 call extend(options, test_group.options)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001858
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001859 let p = popup_create(text, options)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001860
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001861 let msg = string(extend(options, #{text: text}))
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001862 call s:VerifyPosition(p, msg, e_line, e_col, e_width, e_height)
1863 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001864 endfor
1865 endfor
1866
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001867 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001868 %bwipe!
1869endfunc
1870
Bram Moolenaar3397f742019-06-02 18:40:06 +02001871func Test_adjust_left_past_screen_width()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001872 " width of screen
1873 let X = join(map(range(&columns), {->'X'}), '')
1874
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001875 let p = popup_create(X, #{line: 1, col: 1, wrap: 0})
1876 call s:VerifyPosition(p, 'full width topleft', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001877
1878 redraw
1879 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1880 call assert_equal(X, line)
1881
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001882 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001883 redraw
1884
1885 " Same if placed on the right hand side
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001886 let p = popup_create(X, #{line: 1, col: &columns, wrap: 0})
1887 call s:VerifyPosition(p, 'full width topright', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001888
1889 redraw
1890 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1891 call assert_equal(X, line)
1892
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001893 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001894 redraw
1895
1896 " Extend so > window width
1897 let X .= 'x'
1898
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001899 let p = popup_create(X, #{line: 1, col: 1, wrap: 0})
1900 call s:VerifyPosition(p, 'full width + 1 topleft', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001901
1902 redraw
1903 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1904 call assert_equal(X[ : -2 ], line)
1905
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001906 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001907 redraw
1908
1909 " Shifted then truncated (the x is not visible)
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001910 let p = popup_create(X, #{line: 1, col: &columns - 3, wrap: 0})
1911 call s:VerifyPosition(p, 'full width + 1 topright', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001912
1913 redraw
1914 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1915 call assert_equal(X[ : -2 ], line)
1916
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001917 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001918 redraw
1919
1920 " Not shifted, just truncated
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001921 let p = popup_create(X,
1922 \ #{line: 1, col: 2, wrap: 0, fixed: 1})
1923 call s:VerifyPosition(p, 'full width + 1 fixed', 1, 2, &columns - 1, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001924
1925 redraw
1926 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1927 let e_line = ' ' . X[ 1 : -2 ]
1928 call assert_equal(e_line, line)
1929
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001930 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001931 redraw
1932
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001933 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001934 %bwipe!
Bram Moolenaar3397f742019-06-02 18:40:06 +02001935endfunc
1936
1937func Test_popup_moved()
1938 new
1939 call test_override('char_avail', 1)
1940 call setline(1, ['one word to move around', 'a WORD.and->some thing'])
1941
1942 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001943 let winid = popup_atcursor('text', #{moved: 'any'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001944 redraw
1945 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001946 call assert_equal([1, 4, 4], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001947 " trigger the check for last_cursormoved by going into insert mode
1948 call feedkeys("li\<Esc>", 'xt')
1949 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001950 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001951
1952 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001953 let winid = popup_atcursor('text', #{moved: 'word'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001954 redraw
1955 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001956 call assert_equal([1, 4, 7], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001957 call feedkeys("hi\<Esc>", 'xt')
1958 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001959 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001960
1961 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001962 let winid = popup_atcursor('text', #{moved: 'word'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001963 redraw
1964 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001965 call assert_equal([1, 4, 7], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001966 call feedkeys("li\<Esc>", 'xt')
1967 call assert_equal(1, popup_getpos(winid).visible)
1968 call feedkeys("ei\<Esc>", 'xt')
1969 call assert_equal(1, popup_getpos(winid).visible)
1970 call feedkeys("eli\<Esc>", 'xt')
1971 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001972 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001973
Bram Moolenaar17627312019-06-02 19:53:44 +02001974 " WORD is the default
Bram Moolenaar3397f742019-06-02 18:40:06 +02001975 exe "normal gg0/WORD\<CR>"
Bram Moolenaar17627312019-06-02 19:53:44 +02001976 let winid = popup_atcursor('text', {})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001977 redraw
1978 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001979 call assert_equal([2, 2, 15], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001980 call feedkeys("eli\<Esc>", 'xt')
1981 call assert_equal(1, popup_getpos(winid).visible)
1982 call feedkeys("wi\<Esc>", 'xt')
1983 call assert_equal(1, popup_getpos(winid).visible)
1984 call feedkeys("Eli\<Esc>", 'xt')
1985 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001986 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001987
1988 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001989 let winid = popup_atcursor('text', #{moved: [5, 10]})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001990 redraw
1991 call assert_equal(1, popup_getpos(winid).visible)
1992 call feedkeys("eli\<Esc>", 'xt')
1993 call feedkeys("ei\<Esc>", 'xt')
1994 call assert_equal(1, popup_getpos(winid).visible)
1995 call feedkeys("eli\<Esc>", 'xt')
1996 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001997 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001998
1999 bwipe!
2000 call test_override('ALL', 0)
2001endfunc
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002002
2003func Test_notifications()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002004 CheckFeature timers
2005 CheckScreendump
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002006
Bram Moolenaar0fdddee2019-09-01 15:26:23 +02002007 let lines =<< trim END
2008 call setline(1, range(1, 20))
2009 hi Notification ctermbg=lightblue
2010 call popup_notification('first notification', {})
2011 END
2012 call writefile(lines, 'XtestNotifications')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002013 let buf = RunVimInTerminal('-S XtestNotifications', #{rows: 10})
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002014 call VerifyScreenDump(buf, 'Test_popupwin_notify_01', {})
2015
2016 " second one goes below the first one
Bram Moolenaardfa97f22019-06-15 14:31:55 +02002017 call term_sendkeys(buf, ":hi link PopupNotification Notification\<CR>")
2018 call term_sendkeys(buf, ":call popup_notification('another important notification', {})\<CR>")
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002019 call VerifyScreenDump(buf, 'Test_popupwin_notify_02', {})
2020
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002021 " clean up
2022 call StopVimInTerminal(buf)
2023 call delete('XtestNotifications')
2024endfunc
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002025
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002026func Test_popup_scrollbar()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002027 CheckScreendump
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002028
2029 let lines =<< trim END
2030 call setline(1, range(1, 20))
Bram Moolenaar8da41812019-06-26 18:04:54 +02002031 hi ScrollThumb ctermbg=blue
2032 hi ScrollBar ctermbg=red
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002033 let winid = popup_create(['one', 'two', 'three', 'four', 'five',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002034 \ 'six', 'seven', 'eight', 'nine'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002035 \ minwidth: 8,
2036 \ maxheight: 4,
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002037 \ })
Bram Moolenaar53a95d62019-06-26 03:54:08 +02002038 func ScrollUp()
2039 call feedkeys("\<F3>\<ScrollWheelUp>", "xt")
2040 endfunc
2041 func ScrollDown()
2042 call feedkeys("\<F3>\<ScrollWheelDown>", "xt")
2043 endfunc
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02002044 func ClickTop()
2045 call feedkeys("\<F4>\<LeftMouse>", "xt")
2046 endfunc
2047 func ClickBot()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002048 call popup_setoptions(g:winid, #{border: [], close: 'button'})
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02002049 call feedkeys("\<F5>\<LeftMouse>", "xt")
2050 endfunc
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002051 func Popup_filter(winid, key)
2052 if a:key == 'j'
2053 let line = popup_getoptions(a:winid).firstline
2054 let nlines = line('$', a:winid)
2055 let newline = line < nlines ? (line + 1) : nlines
2056 call popup_setoptions(a:winid, #{firstline: newline})
2057 return v:true
2058 elseif a:key == 'x'
2059 call popup_close(a:winid)
2060 return v:true
2061 endif
2062 endfunc
2063
2064 func PopupScroll()
2065 call popup_clear()
2066 let text =<< trim END
2067 1
2068 2
2069 3
2070 4
2071 long line long line long line long line long line long line
2072 long line long line long line long line long line long line
2073 long line long line long line long line long line long line
2074 END
2075 call popup_create(text, #{
2076 \ minwidth: 30,
2077 \ maxwidth: 30,
2078 \ minheight: 4,
2079 \ maxheight: 4,
2080 \ firstline: 1,
Bram Moolenaar30efcf32019-11-03 22:29:38 +01002081 \ lastline: 4,
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002082 \ wrap: v:true,
2083 \ scrollbar: v:true,
2084 \ mapping: v:false,
2085 \ filter: funcref('Popup_filter')
2086 \ })
2087 endfunc
Bram Moolenaar53a95d62019-06-26 03:54:08 +02002088 map <silent> <F3> :call test_setmouse(5, 36)<CR>
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02002089 map <silent> <F4> :call test_setmouse(4, 42)<CR>
2090 map <silent> <F5> :call test_setmouse(7, 42)<CR>
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002091 END
2092 call writefile(lines, 'XtestPopupScroll')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002093 let buf = RunVimInTerminal('-S XtestPopupScroll', #{rows: 10})
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002094 call VerifyScreenDump(buf, 'Test_popupwin_scroll_1', {})
2095
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002096 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 2})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002097 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002098 call VerifyScreenDump(buf, 'Test_popupwin_scroll_2', {})
2099
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002100 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 6})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002101 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002102 call VerifyScreenDump(buf, 'Test_popupwin_scroll_3', {})
2103
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002104 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 9})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002105 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002106 call VerifyScreenDump(buf, 'Test_popupwin_scroll_4', {})
2107
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02002108 call term_sendkeys(buf, ":call popup_setoptions(winid, #{scrollbarhighlight: 'ScrollBar', thumbhighlight: 'ScrollThumb', firstline: 5})\<CR>")
Bram Moolenaara112f2d2019-09-01 17:38:09 +02002109 " this scrolls two lines (half the window height)
Bram Moolenaar53a95d62019-06-26 03:54:08 +02002110 call term_sendkeys(buf, ":call ScrollUp()\<CR>")
2111 call VerifyScreenDump(buf, 'Test_popupwin_scroll_5', {})
2112
2113 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
2114 call VerifyScreenDump(buf, 'Test_popupwin_scroll_6', {})
2115
2116 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
Bram Moolenaar13b47c32019-06-28 21:55:48 +02002117 " wait a bit, otherwise it fails sometimes (double click recognized?)
2118 sleep 100m
Bram Moolenaar53a95d62019-06-26 03:54:08 +02002119 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
2120 call VerifyScreenDump(buf, 'Test_popupwin_scroll_7', {})
2121
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02002122 call term_sendkeys(buf, ":call ClickTop()\<CR>")
2123 sleep 100m
2124 call term_sendkeys(buf, ":call ClickTop()\<CR>")
2125 call VerifyScreenDump(buf, 'Test_popupwin_scroll_8', {})
2126
2127 call term_sendkeys(buf, ":call ClickBot()\<CR>")
2128 call VerifyScreenDump(buf, 'Test_popupwin_scroll_9', {})
2129
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02002130 " remove the minwidth and maxheight
2131 call term_sendkeys(buf, ":call popup_setoptions(winid, #{maxheight: 0, minwidth: 0})\<CR>")
Bram Moolenaar7e0f4622019-09-17 21:23:39 +02002132 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02002133 call VerifyScreenDump(buf, 'Test_popupwin_scroll_10', {})
2134
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002135 " check size with non-wrapping lines
2136 call term_sendkeys(buf, ":call PopupScroll()\<CR>")
2137 call VerifyScreenDump(buf, 'Test_popupwin_scroll_11', {})
2138
2139 " check size with wrapping lines
2140 call term_sendkeys(buf, "j")
2141 call VerifyScreenDump(buf, 'Test_popupwin_scroll_12', {})
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002142
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002143 " clean up
Bram Moolenaar20298ce2020-06-19 21:46:52 +02002144 call term_sendkeys(buf, "x")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002145 call StopVimInTerminal(buf)
2146 call delete('XtestPopupScroll')
2147endfunc
2148
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02002149func Test_popup_too_high_scrollbar()
2150 CheckScreendump
2151
2152 let lines =<< trim END
2153 call setline(1, range(1, 20)->map({i, v -> repeat(v, 10)}))
2154 set scrolloff=0
2155 func ShowPopup()
2156 let winid = popup_atcursor(['one', 'two', 'three', 'four', 'five',
2157 \ 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve'], #{
2158 \ minwidth: 8,
2159 \ border: [],
2160 \ })
2161 endfunc
2162 normal 3G$
2163 call ShowPopup()
2164 END
2165 call writefile(lines, 'XtestPopupToohigh')
2166 let buf = RunVimInTerminal('-S XtestPopupToohigh', #{rows: 10})
2167 call VerifyScreenDump(buf, 'Test_popupwin_toohigh_1', {})
2168
2169 call term_sendkeys(buf, ":call popup_clear()\<CR>")
2170 call term_sendkeys(buf, "8G$")
2171 call term_sendkeys(buf, ":call ShowPopup()\<CR>")
2172 call VerifyScreenDump(buf, 'Test_popupwin_toohigh_2', {})
2173
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02002174 call term_sendkeys(buf, ":call popup_clear()\<CR>")
2175 call term_sendkeys(buf, "gg$")
2176 call term_sendkeys(buf, ":call ShowPopup()\<CR>")
2177 call VerifyScreenDump(buf, 'Test_popupwin_toohigh_3', {})
2178
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02002179 " clean up
2180 call StopVimInTerminal(buf)
2181 call delete('XtestPopupToohigh')
2182endfunc
2183
Bram Moolenaar437a7462019-07-05 20:17:22 +02002184func Test_popup_fitting_scrollbar()
2185 " this was causing a crash, divide by zero
2186 let winid = popup_create([
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002187 \ 'one', 'two', 'longer line that wraps', 'four', 'five'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002188 \ scrollbar: 1,
2189 \ maxwidth: 10,
2190 \ maxheight: 5,
2191 \ firstline: 2})
Bram Moolenaar437a7462019-07-05 20:17:22 +02002192 redraw
2193 call popup_clear()
2194endfunc
2195
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002196func Test_popup_settext()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002197 CheckScreendump
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002198
2199 let lines =<< trim END
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002200 let opts = #{wrap: 0}
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002201 let p = popup_create('test', opts)
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002202 eval p->popup_settext('this is a text')
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002203 END
2204
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002205 call writefile(lines, 'XtestPopupSetText')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002206 let buf = RunVimInTerminal('-S XtestPopupSetText', #{rows: 10})
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002207 call VerifyScreenDump(buf, 'Test_popup_settext_01', {})
2208
2209 " Setting to empty string clears it
2210 call term_sendkeys(buf, ":call popup_settext(p, '')\<CR>")
2211 call VerifyScreenDump(buf, 'Test_popup_settext_02', {})
2212
2213 " Setting a list
2214 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
2215 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
2216
2217 " Shrinking with a list
2218 call term_sendkeys(buf, ":call popup_settext(p, ['a'])\<CR>")
2219 call VerifyScreenDump(buf, 'Test_popup_settext_04', {})
2220
2221 " Growing with a list
2222 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
2223 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
2224
2225 " Empty list clears
2226 call term_sendkeys(buf, ":call popup_settext(p, [])\<CR>")
2227 call VerifyScreenDump(buf, 'Test_popup_settext_05', {})
2228
2229 " Dicts
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002230 call term_sendkeys(buf, ":call popup_settext(p, [#{text: 'aaaa'}, #{text: 'bbbb'}, #{text: 'cccc'}])\<CR>")
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002231 call VerifyScreenDump(buf, 'Test_popup_settext_06', {})
2232
Bram Moolenaarb0992022020-01-30 14:55:42 +01002233 " range() (doesn't work)
2234 call term_sendkeys(buf, ":call popup_settext(p, range(4, 8))\<CR>")
2235 call VerifyScreenDump(buf, 'Test_popup_settext_07', {})
2236
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002237 " clean up
2238 call StopVimInTerminal(buf)
2239 call delete('XtestPopupSetText')
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002240endfunc
2241
2242func Test_popup_hidden()
2243 new
2244
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002245 let winid = popup_atcursor('text', #{hidden: 1})
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002246 redraw
2247 call assert_equal(0, popup_getpos(winid).visible)
2248 call popup_close(winid)
2249
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002250 let winid = popup_create('text', #{hidden: 1})
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002251 redraw
2252 call assert_equal(0, popup_getpos(winid).visible)
2253 call popup_close(winid)
2254
2255 func QuitCallback(id, res)
2256 let s:cb_winid = a:id
2257 let s:cb_res = a:res
2258 endfunc
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002259 let winid = 'make a choice'->popup_dialog(#{hidden: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002260 \ filter: 'popup_filter_yesno',
2261 \ callback: 'QuitCallback',
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002262 \ })
2263 redraw
2264 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarae943152019-06-16 22:54:14 +02002265 call assert_equal(function('popup_filter_yesno'), popup_getoptions(winid).filter)
2266 call assert_equal(function('QuitCallback'), popup_getoptions(winid).callback)
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002267 exe "normal anot used by filter\<Esc>"
2268 call assert_equal('not used by filter', getline(1))
2269
2270 call popup_show(winid)
2271 call feedkeys('y', "xt")
2272 call assert_equal(1, s:cb_res)
2273
2274 bwipe!
2275 delfunc QuitCallback
2276endfunc
Bram Moolenaarae943152019-06-16 22:54:14 +02002277
2278" Test options not checked elsewhere
2279func Test_set_get_options()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002280 let winid = popup_create('some text', #{highlight: 'Beautiful'})
Bram Moolenaarae943152019-06-16 22:54:14 +02002281 let options = popup_getoptions(winid)
2282 call assert_equal(1, options.wrap)
2283 call assert_equal(0, options.drag)
2284 call assert_equal('Beautiful', options.highlight)
2285
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002286 call popup_setoptions(winid, #{wrap: 0, drag: 1, highlight: 'Another'})
Bram Moolenaarae943152019-06-16 22:54:14 +02002287 let options = popup_getoptions(winid)
2288 call assert_equal(0, options.wrap)
2289 call assert_equal(1, options.drag)
2290 call assert_equal('Another', options.highlight)
2291
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02002292 call assert_fails('call popup_setoptions(winid, [])', 'E715:')
2293 call assert_fails('call popup_setoptions(winid, test_null_dict())', 'E715:')
2294
Bram Moolenaarae943152019-06-16 22:54:14 +02002295 call popup_close(winid)
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02002296 call assert_equal(0, popup_setoptions(winid, options.wrap))
Bram Moolenaarae943152019-06-16 22:54:14 +02002297endfunc
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002298
2299func Test_popupwin_garbage_collect()
2300 func MyPopupFilter(x, winid, c)
2301 " NOP
2302 endfunc
2303
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002304 let winid = popup_create('something', #{filter: function('MyPopupFilter', [{}])})
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002305 call test_garbagecollect_now()
2306 redraw
Bram Moolenaar1bc353b2019-09-01 14:45:28 +02002307 " Must not crash caused by invalid memory access
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002308 call feedkeys('j', 'xt')
2309 call assert_true(v:true)
2310
2311 call popup_close(winid)
2312 delfunc MyPopupFilter
2313endfunc
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002314
Bram Moolenaar581ba392019-09-03 22:08:33 +02002315func Test_popupwin_filter_mode()
2316 func MyPopupFilter(winid, c)
2317 let s:typed = a:c
2318 if a:c == ':' || a:c == "\r" || a:c == 'v'
2319 " can start cmdline mode, get out, and start/stop Visual mode
2320 return 0
2321 endif
2322 return 1
2323 endfunc
2324
2325 " Normal, Visual and Insert mode
2326 let winid = popup_create('something', #{filter: 'MyPopupFilter', filtermode: 'nvi'})
2327 redraw
2328 call feedkeys('x', 'xt')
2329 call assert_equal('x', s:typed)
2330
2331 call feedkeys(":let g:foo = 'foo'\<CR>", 'xt')
2332 call assert_equal(':', s:typed)
2333 call assert_equal('foo', g:foo)
2334
2335 let @x = 'something'
2336 call feedkeys('v$"xy', 'xt')
2337 call assert_equal('y', s:typed)
2338 call assert_equal('something', @x) " yank command is filtered out
2339 call feedkeys('v', 'xt') " end Visual mode
2340
2341 call popup_close(winid)
2342
2343 " only Normal mode
2344 let winid = popup_create('something', #{filter: 'MyPopupFilter', filtermode: 'n'})
2345 redraw
2346 call feedkeys('x', 'xt')
2347 call assert_equal('x', s:typed)
2348
2349 call feedkeys(":let g:foo = 'foo'\<CR>", 'xt')
2350 call assert_equal(':', s:typed)
2351 call assert_equal('foo', g:foo)
2352
2353 let @x = 'something'
2354 call feedkeys('v$"xy', 'xt')
2355 call assert_equal('v', s:typed)
2356 call assert_notequal('something', @x)
2357
2358 call popup_close(winid)
2359
2360 " default: all modes
2361 let winid = popup_create('something', #{filter: 'MyPopupFilter'})
2362 redraw
2363 call feedkeys('x', 'xt')
2364 call assert_equal('x', s:typed)
2365
2366 let g:foo = 'bar'
2367 call feedkeys(":let g:foo = 'foo'\<CR>", 'xt')
2368 call assert_equal("\r", s:typed)
2369 call assert_equal('bar', g:foo)
2370
2371 let @x = 'something'
2372 call feedkeys('v$"xy', 'xt')
2373 call assert_equal('y', s:typed)
2374 call assert_equal('something', @x) " yank command is filtered out
2375 call feedkeys('v', 'xt') " end Visual mode
2376
2377 call popup_close(winid)
2378 delfunc MyPopupFilter
2379endfunc
2380
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002381func Test_popupwin_filter_mouse()
2382 func MyPopupFilter(winid, c)
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002383 let g:got_mousepos = getmousepos()
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002384 return 0
2385 endfunc
2386
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002387 call setline(1, ['.'->repeat(25)]->repeat(10))
2388 let winid = popup_create(['short', 'long line that will wrap', 'other'], #{
2389 \ line: 2,
2390 \ col: 4,
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002391 \ maxwidth: 12,
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002392 \ padding: [],
2393 \ border: [],
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002394 \ filter: 'MyPopupFilter',
2395 \ })
2396 redraw
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002397 " 123456789012345678901
2398 " 1 .....................
2399 " 2 ...+--------------+..
2400 " 3 ...| |..
2401 " 4 ...| short |..
2402 " 5 ...| long line th |..
2403 " 6 ...| at will wrap |..
2404 " 7 ...| other |..
2405 " 8 ...| |..
2406 " 9 ...+--------------+..
2407 " 10 .....................
2408 let tests = []
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002409
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002410 func AddItemOutsidePopup(tests, row, col)
2411 eval a:tests->add(#{clickrow: a:row, clickcol: a:col, result: #{
2412 \ screenrow: a:row, screencol: a:col,
2413 \ winid: win_getid(), winrow: a:row, wincol: a:col,
2414 \ line: a:row, column: a:col,
2415 \ }})
2416 endfunc
2417 func AddItemInPopupBorder(tests, winid, row, col)
2418 eval a:tests->add(#{clickrow: a:row, clickcol: a:col, result: #{
2419 \ screenrow: a:row, screencol: a:col,
2420 \ winid: a:winid, winrow: a:row - 1, wincol: a:col - 3,
2421 \ line: 0, column: 0,
2422 \ }})
2423 endfunc
2424 func AddItemInPopupText(tests, winid, row, col, textline, textcol)
2425 eval a:tests->add(#{clickrow: a:row, clickcol: a:col, result: #{
2426 \ screenrow: a:row, screencol: a:col,
2427 \ winid: a:winid, winrow: a:row - 1, wincol: a:col - 3,
2428 \ line: a:textline, column: a:textcol,
2429 \ }})
2430 endfunc
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002431
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002432 " above and below popup
2433 for c in range(1, 21)
2434 call AddItemOutsidePopup(tests, 1, c)
2435 call AddItemOutsidePopup(tests, 10, c)
2436 endfor
2437 " left and right of popup
2438 for r in range(1, 10)
2439 call AddItemOutsidePopup(tests, r, 3)
2440 call AddItemOutsidePopup(tests, r, 20)
2441 endfor
2442 " top and bottom in popup
2443 for c in range(4, 19)
2444 call AddItemInPopupBorder(tests, winid, 2, c)
2445 call AddItemInPopupBorder(tests, winid, 3, c)
2446 call AddItemInPopupBorder(tests, winid, 8, c)
2447 call AddItemInPopupBorder(tests, winid, 9, c)
2448 endfor
2449 " left and right margin in popup
2450 for r in range(2, 9)
2451 call AddItemInPopupBorder(tests, winid, r, 4)
2452 call AddItemInPopupBorder(tests, winid, r, 5)
2453 call AddItemInPopupBorder(tests, winid, r, 18)
2454 call AddItemInPopupBorder(tests, winid, r, 19)
2455 endfor
2456 " text "short"
2457 call AddItemInPopupText(tests, winid, 4, 6, 1, 1)
2458 call AddItemInPopupText(tests, winid, 4, 10, 1, 5)
2459 call AddItemInPopupText(tests, winid, 4, 11, 1, 6)
2460 call AddItemInPopupText(tests, winid, 4, 17, 1, 6)
2461 " text "long line th"
2462 call AddItemInPopupText(tests, winid, 5, 6, 2, 1)
2463 call AddItemInPopupText(tests, winid, 5, 10, 2, 5)
2464 call AddItemInPopupText(tests, winid, 5, 17, 2, 12)
2465 " text "at will wrap"
2466 call AddItemInPopupText(tests, winid, 6, 6, 2, 13)
2467 call AddItemInPopupText(tests, winid, 6, 10, 2, 17)
2468 call AddItemInPopupText(tests, winid, 6, 17, 2, 24)
2469 " text "other"
2470 call AddItemInPopupText(tests, winid, 7, 6, 3, 1)
2471 call AddItemInPopupText(tests, winid, 7, 10, 3, 5)
2472 call AddItemInPopupText(tests, winid, 7, 11, 3, 6)
2473 call AddItemInPopupText(tests, winid, 7, 17, 3, 6)
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002474
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002475 for item in tests
2476 call test_setmouse(item.clickrow, item.clickcol)
2477 call feedkeys("\<LeftMouse>", 'xt')
2478 call assert_equal(item.result, g:got_mousepos)
2479 endfor
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002480
2481 call popup_close(winid)
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002482 enew!
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002483 delfunc MyPopupFilter
2484endfunc
2485
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002486func Test_popupwin_with_buffer()
2487 call writefile(['some text', 'in a buffer'], 'XsomeFile')
2488 let buf = bufadd('XsomeFile')
2489 call assert_equal(0, bufloaded(buf))
Bram Moolenaar46451042019-08-24 15:50:46 +02002490
2491 setlocal number
2492 call setbufvar(buf, "&wrapmargin", 13)
2493
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002494 let winid = popup_create(buf, {})
2495 call assert_notequal(0, winid)
2496 let pos = popup_getpos(winid)
2497 call assert_equal(2, pos.height)
2498 call assert_equal(1, bufloaded(buf))
Bram Moolenaar46451042019-08-24 15:50:46 +02002499
2500 " window-local option is set to default, buffer-local is not
2501 call assert_equal(0, getwinvar(winid, '&number'))
2502 call assert_equal(13, getbufvar(buf, '&wrapmargin'))
2503
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002504 call popup_close(winid)
2505 call assert_equal({}, popup_getpos(winid))
2506 call assert_equal(1, bufloaded(buf))
2507 exe 'bwipe! ' .. buf
Bram Moolenaar46451042019-08-24 15:50:46 +02002508 setlocal nonumber
Bram Moolenaar7866b872019-07-01 22:21:01 +02002509
2510 edit test_popupwin.vim
2511 let winid = popup_create(bufnr(''), {})
2512 redraw
2513 call popup_close(winid)
Bram Moolenaar3940ec62019-07-05 21:53:24 +02002514 call delete('XsomeFile')
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002515endfunc
Bram Moolenaare296e312019-07-03 23:20:18 +02002516
Bram Moolenaare0d749a2019-09-25 22:14:48 +02002517func Test_popupwin_terminal_buffer()
Bram Moolenaard2c1fb42019-09-25 23:06:40 +02002518 CheckFeature terminal
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002519 CheckUnix
Bram Moolenaare06a28f2020-05-13 23:24:12 +02002520 " Starting a terminal to run a shell in is considered flaky.
2521 let g:test_is_flaky = 1
Bram Moolenaard2c1fb42019-09-25 23:06:40 +02002522
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002523 let origwin = win_getid()
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002524 let termbuf = term_start(&shell, #{hidden: 1})
2525 let winid = popup_create(termbuf, #{minwidth: 40, minheight: 10})
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002526 " Wait for shell to start
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002527 call WaitForAssert({-> assert_equal("run", job_status(term_getjob(termbuf)))})
Bram Moolenaare06a28f2020-05-13 23:24:12 +02002528 sleep 100m
Bram Moolenaar631ebc42020-02-03 22:15:26 +01002529 " Check this doesn't crash
2530 call assert_equal(winnr(), winnr('j'))
2531 call assert_equal(winnr(), winnr('k'))
2532 call assert_equal(winnr(), winnr('h'))
2533 call assert_equal(winnr(), winnr('l'))
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02002534
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002535 " Cannot quit while job is running
2536 call assert_fails('call feedkeys("\<C-W>:quit\<CR>", "xt")', 'E948:')
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02002537
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002538 " Cannot enter Terminal-Normal mode. (TODO: but it works...)
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02002539 call feedkeys("xxx\<C-W>N", 'xt')
2540 call assert_fails('call feedkeys("gf", "xt")', 'E863:')
2541 call feedkeys("a\<C-U>", 'xt')
2542
Bram Moolenaar3f65c662020-05-27 23:15:16 +02002543 " Cannot escape from terminal window
2544 call assert_fails('tab drop xxx', 'E863:')
2545
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002546 " Cannot open a second one.
2547 let termbuf2 = term_start(&shell, #{hidden: 1})
2548 call assert_fails('call popup_create(termbuf2, #{})', 'E861:')
2549 call term_sendkeys(termbuf2, "exit\<CR>")
2550
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02002551 " Exiting shell closes popup window
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002552 call feedkeys("exit\<CR>", 'xt')
2553 " Wait for shell to exit
Bram Moolenaarb2b218d2020-06-22 20:22:19 +02002554 call WaitForAssert({-> assert_equal("dead", job_status(term_getjob(termbuf)))})
Bram Moolenaar373c6512020-06-22 19:24:23 +02002555
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002556 call feedkeys(":quit\<CR>", 'xt')
2557 call assert_equal(origwin, win_getid())
Bram Moolenaare0d749a2019-09-25 22:14:48 +02002558endfunc
2559
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002560func Test_popupwin_close_prevwin()
2561 CheckFeature terminal
Bram Moolenaar62f93f42020-09-02 22:33:24 +02002562 call Popupwin_close_prevwin()
2563endfunc
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002564
Bram Moolenaar62f93f42020-09-02 22:33:24 +02002565def Popupwin_close_prevwin()
2566 assert_equal(1, winnr('$'))
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002567 split
2568 wincmd b
Bram Moolenaar62f93f42020-09-02 22:33:24 +02002569 assert_equal(2, winnr())
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002570 let buf = term_start(&shell, #{hidden: 1})
Bram Moolenaar62f93f42020-09-02 22:33:24 +02002571 popup_create(buf, {})
2572 TermWait(buf, 100)
2573 popup_clear(true)
2574 assert_equal(2, winnr())
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002575
2576 quit
2577 exe 'bwipe! ' .. buf
Bram Moolenaar62f93f42020-09-02 22:33:24 +02002578enddef
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002579
Bram Moolenaar934470e2019-09-01 23:27:05 +02002580func Test_popupwin_with_buffer_and_filter()
2581 new Xwithfilter
2582 call setline(1, range(100))
2583 let bufnr = bufnr()
2584 hide
2585
2586 func BufferFilter(win, key)
2587 if a:key == 'G'
2588 " recursive use of "G" does not cause problems.
2589 call win_execute(a:win, 'normal! G')
2590 return 1
2591 endif
2592 return 0
2593 endfunc
2594
2595 let winid = popup_create(bufnr, #{maxheight: 5, filter: 'BufferFilter'})
2596 call assert_equal(1, popup_getpos(winid).firstline)
2597 redraw
2598 call feedkeys("G", 'xt')
2599 call assert_equal(99, popup_getpos(winid).firstline)
2600
2601 call popup_close(winid)
2602 exe 'bwipe! ' .. bufnr
2603endfunc
2604
Bram Moolenaare296e312019-07-03 23:20:18 +02002605func Test_popupwin_width()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002606 let winid = popup_create(repeat(['short', 'long long long line', 'medium width'], 50), #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002607 \ maxwidth: 40,
2608 \ maxheight: 10,
Bram Moolenaare296e312019-07-03 23:20:18 +02002609 \ })
2610 for top in range(1, 20)
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002611 eval winid->popup_setoptions(#{firstline: top})
Bram Moolenaare296e312019-07-03 23:20:18 +02002612 redraw
2613 call assert_equal(19, popup_getpos(winid).width)
2614 endfor
2615 call popup_clear()
2616endfunc
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02002617
2618func Test_popupwin_buf_close()
2619 let buf = bufadd('Xtestbuf')
2620 call bufload(buf)
2621 call setbufline(buf, 1, ['just', 'some', 'lines'])
2622 let winid = popup_create(buf, {})
2623 redraw
2624 call assert_equal(3, popup_getpos(winid).height)
2625 let bufinfo = getbufinfo(buf)[0]
2626 call assert_equal(1, bufinfo.changed)
2627 call assert_equal(0, bufinfo.hidden)
2628 call assert_equal(0, bufinfo.listed)
2629 call assert_equal(1, bufinfo.loaded)
2630 call assert_equal([], bufinfo.windows)
2631 call assert_equal([winid], bufinfo.popups)
2632
2633 call popup_close(winid)
2634 call assert_equal({}, popup_getpos(winid))
2635 let bufinfo = getbufinfo(buf)[0]
2636 call assert_equal(1, bufinfo.changed)
2637 call assert_equal(1, bufinfo.hidden)
2638 call assert_equal(0, bufinfo.listed)
2639 call assert_equal(1, bufinfo.loaded)
2640 call assert_equal([], bufinfo.windows)
2641 call assert_equal([], bufinfo.popups)
2642 exe 'bwipe! ' .. buf
2643endfunc
Bram Moolenaar017c2692019-07-13 14:17:51 +02002644
2645func Test_popup_menu_with_maxwidth()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002646 CheckScreendump
Bram Moolenaar017c2692019-07-13 14:17:51 +02002647
2648 let lines =<< trim END
2649 call setline(1, range(1, 10))
2650 hi ScrollThumb ctermbg=blue
2651 hi ScrollBar ctermbg=red
2652 func PopupMenu(lines, line, col, scrollbar = 0)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002653 return popup_menu(a:lines, #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002654 \ maxwidth: 10,
2655 \ maxheight: 3,
2656 \ pos : 'topleft',
2657 \ col : a:col,
2658 \ line : a:line,
2659 \ scrollbar : a:scrollbar,
Bram Moolenaar017c2692019-07-13 14:17:51 +02002660 \ })
2661 endfunc
2662 call PopupMenu(['x'], 1, 1)
2663 call PopupMenu(['123456789|'], 1, 16)
2664 call PopupMenu(['123456789|' .. ' '], 7, 1)
2665 call PopupMenu([repeat('123456789|', 100)], 7, 16)
2666 call PopupMenu(repeat(['123456789|' .. ' '], 5), 1, 33, 1)
2667 END
2668 call writefile(lines, 'XtestPopupMenuMaxWidth')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002669 let buf = RunVimInTerminal('-S XtestPopupMenuMaxWidth', #{rows: 13})
Bram Moolenaar017c2692019-07-13 14:17:51 +02002670 call VerifyScreenDump(buf, 'Test_popupwin_menu_maxwidth_1', {})
2671
2672 " close the menu popupwin.
2673 call term_sendkeys(buf, " ")
2674 call term_sendkeys(buf, " ")
2675 call term_sendkeys(buf, " ")
2676 call term_sendkeys(buf, " ")
2677 call term_sendkeys(buf, " ")
2678
2679 " clean up
2680 call StopVimInTerminal(buf)
2681 call delete('XtestPopupMenuMaxWidth')
2682endfunc
2683
Bram Moolenaara901a372019-07-13 16:38:50 +02002684func Test_popup_menu_with_scrollbar()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002685 CheckScreendump
Bram Moolenaara901a372019-07-13 16:38:50 +02002686
2687 let lines =<< trim END
2688 call setline(1, range(1, 20))
2689 hi ScrollThumb ctermbg=blue
2690 hi ScrollBar ctermbg=red
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002691 eval ['one', 'two', 'three', 'four', 'five',
2692 \ 'six', 'seven', 'eight', 'nine']
2693 \ ->popup_menu(#{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002694 \ minwidth: 8,
2695 \ maxheight: 3,
Bram Moolenaara901a372019-07-13 16:38:50 +02002696 \ })
2697 END
2698 call writefile(lines, 'XtestPopupMenuScroll')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002699 let buf = RunVimInTerminal('-S XtestPopupMenuScroll', #{rows: 10})
Bram Moolenaara901a372019-07-13 16:38:50 +02002700
2701 call term_sendkeys(buf, "j")
2702 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_1', {})
2703
2704 call term_sendkeys(buf, "jjj")
2705 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_2', {})
2706
2707 " if the cursor is the bottom line, it stays at the bottom line.
2708 call term_sendkeys(buf, repeat("j", 20))
2709 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_3', {})
2710
2711 call term_sendkeys(buf, "kk")
2712 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_4', {})
2713
2714 call term_sendkeys(buf, "k")
2715 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_5', {})
2716
2717 " if the cursor is in the top line, it stays in the top line.
2718 call term_sendkeys(buf, repeat("k", 20))
2719 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_6', {})
2720
2721 " close the menu popupwin.
2722 call term_sendkeys(buf, " ")
2723
2724 " clean up
2725 call StopVimInTerminal(buf)
2726 call delete('XtestPopupMenuScroll')
2727endfunc
2728
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002729func Test_popup_menu_filter()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002730 CheckScreendump
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002731
2732 let lines =<< trim END
2733 function! MyFilter(winid, key) abort
2734 if a:key == "0"
2735 call win_execute(a:winid, "call setpos('.', [0, 1, 1, 0])")
2736 return 1
2737 endif
2738 if a:key == "G"
2739 call win_execute(a:winid, "call setpos('.', [0, line('$'), 1, 0])")
2740 return 1
2741 endif
2742 if a:key == "j"
2743 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0])")
2744 return 1
2745 endif
2746 if a:key == "k"
2747 call win_execute(a:winid, "call setpos('.', [0, line('.') - 1, 1, 0])")
2748 return 1
2749 endif
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02002750 if a:key == ':'
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002751 call popup_close(a:winid)
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02002752 return 0
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002753 endif
2754 return 0
2755 endfunction
2756 call popup_menu(['111', '222', '333', '444', '555', '666', '777', '888', '999'], #{
2757 \ maxheight : 3,
2758 \ filter : 'MyFilter'
2759 \ })
2760 END
2761 call writefile(lines, 'XtestPopupMenuFilter')
2762 let buf = RunVimInTerminal('-S XtestPopupMenuFilter', #{rows: 10})
2763
2764 call term_sendkeys(buf, "j")
2765 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_1', {})
2766
2767 call term_sendkeys(buf, "k")
2768 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_2', {})
2769
2770 call term_sendkeys(buf, "G")
2771 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_3', {})
2772
2773 call term_sendkeys(buf, "0")
2774 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_4', {})
2775
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02002776 " check that when the popup is closed in the filter the screen is redrawn
2777 call term_sendkeys(buf, ":")
2778 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_5', {})
2779 call term_sendkeys(buf, "\<CR>")
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002780
2781 " clean up
2782 call StopVimInTerminal(buf)
2783 call delete('XtestPopupMenuFilter')
2784endfunc
2785
2786func Test_popup_cursorline()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002787 CheckScreendump
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002788
2789 let winid = popup_create('some text', {})
2790 call assert_equal(0, popup_getoptions(winid).cursorline)
2791 call popup_close(winid)
2792
2793 let winid = popup_create('some text', #{ cursorline: 1, })
2794 call assert_equal(1, popup_getoptions(winid).cursorline)
2795 call popup_close(winid)
2796
2797 let winid = popup_create('some text', #{ cursorline: 0, })
2798 call assert_equal(0, popup_getoptions(winid).cursorline)
2799 call popup_close(winid)
2800
2801 let winid = popup_menu('some text', {})
2802 call assert_equal(1, popup_getoptions(winid).cursorline)
2803 call popup_close(winid)
2804
2805 let winid = popup_menu('some text', #{ cursorline: 1, })
2806 call assert_equal(1, popup_getoptions(winid).cursorline)
2807 call popup_close(winid)
2808
2809 let winid = popup_menu('some text', #{ cursorline: 0, })
2810 call assert_equal(0, popup_getoptions(winid).cursorline)
2811 call popup_close(winid)
2812
2813 " ---------
2814 " Pattern 1
2815 " ---------
2816 let lines =<< trim END
2817 call popup_create(['111', '222', '333'], #{ cursorline : 0 })
2818 END
2819 call writefile(lines, 'XtestPopupCursorLine')
2820 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2821 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_1', {})
2822 call term_sendkeys(buf, ":call popup_clear()\<cr>")
2823 call StopVimInTerminal(buf)
2824
2825 " ---------
2826 " Pattern 2
2827 " ---------
2828 let lines =<< trim END
2829 call popup_create(['111', '222', '333'], #{ cursorline : 1 })
2830 END
2831 call writefile(lines, 'XtestPopupCursorLine')
2832 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2833 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_2', {})
2834 call term_sendkeys(buf, ":call popup_clear()\<cr>")
2835 call StopVimInTerminal(buf)
2836
2837 " ---------
2838 " Pattern 3
2839 " ---------
2840 let lines =<< trim END
2841 function! MyFilter(winid, key) abort
2842 if a:key == "j"
2843 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0]) | redraw")
2844 return 1
2845 endif
2846 if a:key == 'x'
2847 call popup_close(a:winid)
2848 return 1
2849 endif
2850 return 0
2851 endfunction
2852 call popup_menu(['111', '222', '333'], #{
2853 \ cursorline : 0,
2854 \ maxheight : 2,
2855 \ filter : 'MyFilter',
2856 \ })
2857 END
2858 call writefile(lines, 'XtestPopupCursorLine')
2859 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2860 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_3', {})
2861 call term_sendkeys(buf, "j")
2862 call term_sendkeys(buf, "j")
2863 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_4', {})
2864 call term_sendkeys(buf, "x")
2865 call StopVimInTerminal(buf)
2866
2867 " ---------
2868 " Pattern 4
2869 " ---------
2870 let lines =<< trim END
2871 function! MyFilter(winid, key) abort
2872 if a:key == "j"
2873 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0]) | redraw")
2874 return 1
2875 endif
2876 if a:key == 'x'
2877 call popup_close(a:winid)
2878 return 1
2879 endif
2880 return 0
2881 endfunction
2882 call popup_menu(['111', '222', '333'], #{
2883 \ cursorline : 1,
2884 \ maxheight : 2,
2885 \ filter : 'MyFilter',
2886 \ })
2887 END
2888 call writefile(lines, 'XtestPopupCursorLine')
2889 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2890 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_5', {})
2891 call term_sendkeys(buf, "j")
2892 call term_sendkeys(buf, "j")
2893 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_6', {})
2894 call term_sendkeys(buf, "x")
2895 call StopVimInTerminal(buf)
2896
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02002897 " ---------
2898 " Cursor in second line when creating the popup
2899 " ---------
2900 let lines =<< trim END
2901 let winid = popup_create(['111', '222', '333'], #{
2902 \ cursorline : 1,
2903 \ })
2904 call win_execute(winid, "2")
2905 END
2906 call writefile(lines, 'XtestPopupCursorLine')
2907 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2908 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_7', {})
2909 call StopVimInTerminal(buf)
2910
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002911 call delete('XtestPopupCursorLine')
Bram Moolenaar4eb7dae2019-11-12 22:33:45 +01002912
2913 " ---------
2914 " Use current buffer for popupmenu
2915 " ---------
2916 let lines =<< trim END
2917 call setline(1, ['one', 'two', 'three'])
2918 let winid = popup_create(bufnr('%'), #{
2919 \ cursorline : 1,
2920 \ })
2921 call win_execute(winid, "2")
2922 END
2923 call writefile(lines, 'XtestPopupCursorLine')
2924 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2925 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_8', {})
2926 call StopVimInTerminal(buf)
2927
2928 call delete('XtestPopupCursorLine')
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002929endfunc
2930
Bram Moolenaarf914a332019-07-20 15:09:56 +02002931func Test_previewpopup()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002932 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002933 CheckFeature quickfix
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002934
Bram Moolenaarf914a332019-07-20 15:09:56 +02002935 call writefile([
2936 \ "!_TAG_FILE_ENCODING\tutf-8\t//",
2937 \ "another\tXtagfile\t/^this is another",
2938 \ "theword\tXtagfile\t/^theword"],
2939 \ 'Xtags')
2940 call writefile(range(1,20)
2941 \ + ['theword is here']
2942 \ + range(22, 27)
2943 \ + ['this is another place']
2944 \ + range(29, 40),
2945 \ "Xtagfile")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002946 call writefile(range(1,10)
2947 \ + ['searched word is here']
2948 \ + range(12, 20),
2949 \ "Xheader.h")
Bram Moolenaarf914a332019-07-20 15:09:56 +02002950 let lines =<< trim END
2951 set tags=Xtags
2952 call setline(1, [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002953 \ 'one',
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002954 \ '#include "Xheader.h"',
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002955 \ 'three',
2956 \ 'four',
2957 \ 'five',
2958 \ 'six',
2959 \ 'seven',
2960 \ 'find theword somewhere',
2961 \ 'nine',
2962 \ 'this is another word',
2963 \ 'very long line where the word is also another'])
Bram Moolenaarf914a332019-07-20 15:09:56 +02002964 set previewpopup=height:4,width:40
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002965 set path=.
Bram Moolenaarf914a332019-07-20 15:09:56 +02002966 END
2967 call writefile(lines, 'XtestPreviewPopup')
2968 let buf = RunVimInTerminal('-S XtestPreviewPopup', #{rows: 14})
2969
2970 call term_sendkeys(buf, "/theword\<CR>\<C-W>}")
2971 call term_sendkeys(buf, ":\<CR>")
2972 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_1', {})
2973
2974 call term_sendkeys(buf, "/another\<CR>\<C-W>}")
2975 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_2', {})
2976
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002977 call term_sendkeys(buf, ":call popup_move(popup_findpreview(), #{col: 15})\<CR>")
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002978 call term_sendkeys(buf, ":\<CR>")
2979 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_3', {})
2980
2981 call term_sendkeys(buf, "/another\<CR>\<C-W>}")
2982 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_4', {})
2983
Bram Moolenaar799439a2020-02-11 21:44:17 +01002984 call term_sendkeys(buf, ":silent cd ..\<CR>:\<CR>")
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002985 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_5', {})
Bram Moolenaar799439a2020-02-11 21:44:17 +01002986 call term_sendkeys(buf, ":silent cd testdir\<CR>")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002987
2988 call term_sendkeys(buf, ":pclose\<CR>")
Bram Moolenaar78d629a2019-08-16 17:31:15 +02002989 call term_sendkeys(buf, ":\<BS>")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002990 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_6', {})
2991
2992 call term_sendkeys(buf, ":pedit +/theword Xtagfile\<CR>")
2993 call term_sendkeys(buf, ":\<CR>")
2994 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_7', {})
2995
2996 call term_sendkeys(buf, ":pclose\<CR>")
2997 call term_sendkeys(buf, ":psearch searched\<CR>")
2998 call term_sendkeys(buf, ":\<CR>")
2999 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_8', {})
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02003000
Bram Moolenaar8bf716c2020-01-23 15:33:54 +01003001 call term_sendkeys(buf, "\<C-W>p")
3002 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_9', {})
3003
3004 call term_sendkeys(buf, ":call win_execute(popup_findpreview(), 'call popup_clear()')\<CR>")
3005 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_10', {})
3006
Bram Moolenaarf914a332019-07-20 15:09:56 +02003007 call StopVimInTerminal(buf)
3008 call delete('Xtags')
3009 call delete('Xtagfile')
3010 call delete('XtestPreviewPopup')
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02003011 call delete('Xheader.h')
Bram Moolenaarf914a332019-07-20 15:09:56 +02003012endfunc
3013
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003014func Get_popupmenu_lines()
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003015 let lines =<< trim END
3016 set completeopt+=preview,popup
3017 set completefunc=CompleteFuncDict
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02003018 hi InfoPopup ctermbg=yellow
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003019
3020 func CompleteFuncDict(findstart, base)
3021 if a:findstart
3022 if col('.') > 10
3023 return col('.') - 10
3024 endif
3025 return 0
3026 endif
3027
3028 return {
3029 \ 'words': [
3030 \ {
3031 \ 'word': 'aword',
3032 \ 'abbr': 'wrd',
3033 \ 'menu': 'extra text',
3034 \ 'info': 'words are cool',
3035 \ 'kind': 'W',
3036 \ 'user_data': 'test'
3037 \ },
3038 \ {
3039 \ 'word': 'anotherword',
3040 \ 'abbr': 'anotwrd',
3041 \ 'menu': 'extra text',
3042 \ 'info': "other words are\ncooler than this and some more text\nto make wrap",
3043 \ 'kind': 'W',
3044 \ 'user_data': 'notest'
3045 \ },
3046 \ {
3047 \ 'word': 'noinfo',
3048 \ 'abbr': 'noawrd',
3049 \ 'menu': 'extra text',
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02003050 \ 'info': "lets\nshow\na\nscrollbar\nhere",
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003051 \ 'kind': 'W',
3052 \ 'user_data': 'notest'
3053 \ },
3054 \ {
3055 \ 'word': 'thatword',
3056 \ 'abbr': 'thatwrd',
3057 \ 'menu': 'extra text',
3058 \ 'info': 'that word is cool',
3059 \ 'kind': 'W',
3060 \ 'user_data': 'notest'
3061 \ },
3062 \ ]
3063 \ }
3064 endfunc
3065 call setline(1, 'text text text text text text text ')
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003066 func ChangeColor()
3067 let id = popup_findinfo()
Bram Moolenaar6a124e62019-09-04 18:15:19 +02003068 eval id->popup_setoptions(#{highlight: 'InfoPopup'})
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003069 endfunc
Bram Moolenaardca7abe2019-10-20 18:17:57 +02003070
3071 func InfoHidden()
3072 set completepopup=height:4,border:off,align:menu
3073 set completeopt-=popup completeopt+=popuphidden
3074 au CompleteChanged * call HandleChange()
3075 endfunc
3076
3077 let s:counter = 0
3078 func HandleChange()
3079 let s:counter += 1
3080 let selected = complete_info(['selected']).selected
3081 if selected <= 0
3082 " First time: do nothing, info remains hidden
3083 return
3084 endif
3085 if selected == 1
3086 " Second time: show info right away
3087 let id = popup_findinfo()
3088 if id
3089 call popup_settext(id, 'immediate info ' .. s:counter)
3090 call popup_show(id)
3091 endif
3092 else
3093 " Third time: show info after a short delay
3094 call timer_start(100, 'ShowInfo')
3095 endif
3096 endfunc
3097
3098 func ShowInfo(...)
3099 let id = popup_findinfo()
3100 if id
3101 call popup_settext(id, 'async info ' .. s:counter)
3102 call popup_show(id)
3103 endif
3104 endfunc
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003105 END
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003106 return lines
3107endfunc
3108
3109func Test_popupmenu_info_border()
3110 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003111 CheckFeature quickfix
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003112
3113 let lines = Get_popupmenu_lines()
3114 call add(lines, 'set completepopup=height:4,highlight:InfoPopup')
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003115 call writefile(lines, 'XtestInfoPopup')
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003116
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003117 let buf = RunVimInTerminal('-S XtestInfoPopup', #{rows: 14})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003118 call TermWait(buf, 25)
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003119
3120 call term_sendkeys(buf, "A\<C-X>\<C-U>")
3121 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_1', {})
3122
3123 call term_sendkeys(buf, "\<C-N>")
3124 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_2', {})
3125
3126 call term_sendkeys(buf, "\<C-N>")
3127 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_3', {})
3128
3129 call term_sendkeys(buf, "\<C-N>\<C-N>")
3130 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_4', {})
3131
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02003132 " info on the left with scrollbar
3133 call term_sendkeys(buf, "test text test text\<C-X>\<C-U>")
3134 call term_sendkeys(buf, "\<C-N>\<C-N>")
3135 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_5', {})
3136
Bram Moolenaar202c3f72019-11-21 12:12:35 +01003137 " Test that the popupmenu's scrollbar and infopopup do not overlap
3138 call term_sendkeys(buf, "\<Esc>")
3139 call term_sendkeys(buf, ":set pumheight=3\<CR>")
3140 call term_sendkeys(buf, "cc\<C-X>\<C-U>")
3141 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_6', {})
3142
Bram Moolenaarca7c0782020-01-14 20:42:48 +01003143 " Hide the info popup, cycle trough buffers, make sure it didn't get
3144 " deleted.
3145 call term_sendkeys(buf, "\<Esc>")
3146 call term_sendkeys(buf, ":set hidden\<CR>")
3147 call term_sendkeys(buf, ":bn\<CR>")
3148 call term_sendkeys(buf, ":bn\<CR>")
3149 call term_sendkeys(buf, "otest text test text\<C-X>\<C-U>")
3150 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_7', {})
3151
Bram Moolenaar447bfba2020-07-18 16:07:16 +02003152 " Test that when the option is changed the popup changes.
3153 call term_sendkeys(buf, "\<Esc>")
3154 call term_sendkeys(buf, ":set completepopup=border:off\<CR>")
3155 call term_sendkeys(buf, "a\<C-X>\<C-U>")
3156 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_8', {})
3157
Bram Moolenaar6d585f42020-07-26 22:20:54 +02003158 call term_sendkeys(buf, " \<Esc>")
3159 call term_sendkeys(buf, ":set completepopup+=width:10\<CR>")
3160 call term_sendkeys(buf, "a\<C-X>\<C-U>")
3161 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_9', {})
3162
Bram Moolenaarca7c0782020-01-14 20:42:48 +01003163 call term_sendkeys(buf, "\<Esc>")
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003164 call StopVimInTerminal(buf)
3165 call delete('XtestInfoPopup')
3166endfunc
3167
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003168func Test_popupmenu_info_noborder()
3169 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003170 CheckFeature quickfix
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003171
3172 let lines = Get_popupmenu_lines()
3173 call add(lines, 'set completepopup=height:4,border:off')
3174 call writefile(lines, 'XtestInfoPopupNb')
3175
3176 let buf = RunVimInTerminal('-S XtestInfoPopupNb', #{rows: 14})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003177 call TermWait(buf, 25)
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003178
3179 call term_sendkeys(buf, "A\<C-X>\<C-U>")
3180 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_nb_1', {})
3181
3182 call StopVimInTerminal(buf)
3183 call delete('XtestInfoPopupNb')
3184endfunc
3185
Bram Moolenaar258cef52019-08-21 17:29:29 +02003186func Test_popupmenu_info_align_menu()
3187 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003188 CheckFeature quickfix
Bram Moolenaar258cef52019-08-21 17:29:29 +02003189
3190 let lines = Get_popupmenu_lines()
3191 call add(lines, 'set completepopup=height:4,border:off,align:menu')
3192 call writefile(lines, 'XtestInfoPopupNb')
3193
3194 let buf = RunVimInTerminal('-S XtestInfoPopupNb', #{rows: 14})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003195 call TermWait(buf, 25)
Bram Moolenaar258cef52019-08-21 17:29:29 +02003196
3197 call term_sendkeys(buf, "A\<C-X>\<C-U>")
3198 call term_sendkeys(buf, "\<C-N>")
3199 call term_sendkeys(buf, "\<C-N>")
3200 call term_sendkeys(buf, "\<C-N>")
3201 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_1', {})
3202
3203 call term_sendkeys(buf, "test text test text test\<C-X>\<C-U>")
3204 call term_sendkeys(buf, "\<C-N>")
3205 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_2', {})
3206
3207 call term_sendkeys(buf, "\<Esc>")
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003208 call term_sendkeys(buf, ":call ChangeColor()\<CR>")
Bram Moolenaar258cef52019-08-21 17:29:29 +02003209 call term_sendkeys(buf, ":call setline(2, ['x']->repeat(10))\<CR>")
3210 call term_sendkeys(buf, "Gotest text test text\<C-X>\<C-U>")
3211 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_3', {})
3212
3213 call StopVimInTerminal(buf)
3214 call delete('XtestInfoPopupNb')
3215endfunc
3216
Bram Moolenaardca7abe2019-10-20 18:17:57 +02003217func Test_popupmenu_info_hidden()
3218 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003219 CheckFeature quickfix
Bram Moolenaardca7abe2019-10-20 18:17:57 +02003220
3221 let lines = Get_popupmenu_lines()
3222 call add(lines, 'call InfoHidden()')
3223 call writefile(lines, 'XtestInfoPopupHidden')
3224
3225 let buf = RunVimInTerminal('-S XtestInfoPopupHidden', #{rows: 14})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003226 call TermWait(buf, 25)
Bram Moolenaardca7abe2019-10-20 18:17:57 +02003227
3228 call term_sendkeys(buf, "A\<C-X>\<C-U>")
3229 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_hidden_1', {})
3230
3231 call term_sendkeys(buf, "\<C-N>")
3232 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_hidden_2', {})
3233
3234 call term_sendkeys(buf, "\<C-N>")
3235 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_hidden_3', {})
3236
3237 call term_sendkeys(buf, "\<Esc>")
3238 call StopVimInTerminal(buf)
3239 call delete('XtestInfoPopupHidden')
3240endfunc
3241
Bram Moolenaarbef93ac2019-12-06 20:17:35 +01003242func Test_popupmenu_info_too_wide()
3243 CheckScreendump
3244 CheckFeature quickfix
3245
3246 let lines =<< trim END
3247 call setline(1, range(10))
3248
3249 set completeopt+=preview,popup
3250 set completepopup=align:menu
3251 set omnifunc=OmniFunc
3252 hi InfoPopup ctermbg=lightgrey
3253
3254 func OmniFunc(findstart, base)
3255 if a:findstart
3256 return 0
3257 endif
3258
3259 let menuText = 'some long text to make sure the menu takes up all of the width of the window'
3260 return #{
3261 \ words: [
3262 \ #{
3263 \ word: 'scrap',
3264 \ menu: menuText,
3265 \ info: "other words are\ncooler than this and some more text\nto make wrap",
3266 \ },
3267 \ #{
3268 \ word: 'scappier',
3269 \ menu: menuText,
3270 \ info: 'words are cool',
3271 \ },
3272 \ #{
3273 \ word: 'scrappier2',
3274 \ menu: menuText,
3275 \ info: 'words are cool',
3276 \ },
3277 \ ]
3278 \ }
3279 endfunc
3280 END
3281
3282 call writefile(lines, 'XtestInfoPopupWide')
3283 let buf = RunVimInTerminal('-S XtestInfoPopupWide', #{rows: 8})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003284 call TermWait(buf, 25)
Bram Moolenaarbef93ac2019-12-06 20:17:35 +01003285
3286 call term_sendkeys(buf, "Ascr\<C-X>\<C-O>")
3287 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_wide_1', {})
3288
3289 call term_sendkeys(buf, "\<Esc>")
3290 call StopVimInTerminal(buf)
3291 call delete('XtestInfoPopupWide')
3292endfunc
3293
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02003294func Test_popupwin_recycle_bnr()
Bram Moolenaare49fbff2019-08-21 22:50:07 +02003295 let bufnr = popup_notification('nothing wrong', {})->winbufnr()
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02003296 call popup_clear()
Bram Moolenaar6a124e62019-09-04 18:15:19 +02003297 let winid = 'nothing wrong'->popup_notification({})
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02003298 call assert_equal(bufnr, winbufnr(winid))
3299 call popup_clear()
3300endfunc
3301
Bram Moolenaar1824f452019-10-02 23:06:46 +02003302func Test_popupwin_getoptions_tablocal()
3303 topleft split
3304 let win1 = popup_create('nothing', #{maxheight: 8})
3305 let win2 = popup_create('something', #{maxheight: 10})
3306 let win3 = popup_create('something', #{maxheight: 15})
3307 call assert_equal(8, popup_getoptions(win1).maxheight)
3308 call assert_equal(10, popup_getoptions(win2).maxheight)
3309 call assert_equal(15, popup_getoptions(win3).maxheight)
3310 call popup_clear()
3311 quit
3312endfunc
3313
Bram Moolenaare8a7dfe2019-10-03 22:35:52 +02003314func Test_popupwin_cancel()
3315 let win1 = popup_create('one', #{line: 5, filter: {... -> 0}})
3316 let win2 = popup_create('two', #{line: 10, filter: {... -> 0}})
3317 let win3 = popup_create('three', #{line: 15, filter: {... -> 0}})
3318 call assert_equal(5, popup_getpos(win1).line)
3319 call assert_equal(10, popup_getpos(win2).line)
3320 call assert_equal(15, popup_getpos(win3).line)
3321 " TODO: this also works without patch 8.1.2110
3322 call feedkeys("\<C-C>", 'xt')
3323 call assert_equal(5, popup_getpos(win1).line)
3324 call assert_equal(10, popup_getpos(win2).line)
3325 call assert_equal({}, popup_getpos(win3))
3326 call feedkeys("\<C-C>", 'xt')
3327 call assert_equal(5, popup_getpos(win1).line)
3328 call assert_equal({}, popup_getpos(win2))
3329 call assert_equal({}, popup_getpos(win3))
3330 call feedkeys("\<C-C>", 'xt')
3331 call assert_equal({}, popup_getpos(win1))
3332 call assert_equal({}, popup_getpos(win2))
3333 call assert_equal({}, popup_getpos(win3))
3334endfunc
3335
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003336func Test_popupwin_filter_redraw()
3337 " Create two popups with a filter that closes the popup when typing "0".
3338 " Both popups should close, even though the redraw also calls
3339 " popup_reset_handled()
3340
3341 func CloseFilter(winid, key)
3342 if a:key == '0'
3343 call popup_close(a:winid)
3344 redraw
3345 endif
3346 return 0 " pass the key
3347 endfunc
3348
3349 let id1 = popup_create('first one', #{
3350 \ line: 1,
3351 \ col: 1,
3352 \ filter: 'CloseFilter',
3353 \ })
3354 let id2 = popup_create('second one', #{
3355 \ line: 9,
3356 \ col: 1,
3357 \ filter: 'CloseFilter',
3358 \ })
3359 call assert_equal(1, popup_getpos(id1).line)
3360 call assert_equal(9, popup_getpos(id2).line)
3361
3362 call feedkeys('0', 'xt')
3363 call assert_equal({}, popup_getpos(id1))
3364 call assert_equal({}, popup_getpos(id2))
3365
3366 call popup_clear()
3367 delfunc CloseFilter
3368endfunc
3369
Bram Moolenaar20ebbea2019-11-30 17:58:27 +01003370func Test_popupwin_double_width()
3371 CheckScreendump
3372
3373 let lines =<< trim END
3374 call setline(1, 'x你好世界你好世你好世界你好')
3375 call setline(2, '你好世界你好世你好世界你好')
3376 call setline(3, 'x你好世界你好世你好世界你好')
3377 call popup_create('你好,世界 - 你好,世界xxxxx', #{line: 1, col: 3, maxwidth: 14})
3378 END
3379 call writefile(lines, 'XtestPopupWide')
3380
3381 let buf = RunVimInTerminal('-S XtestPopupWide', #{rows: 10})
3382 call VerifyScreenDump(buf, 'Test_popupwin_doublewidth_1', {})
3383
3384 call StopVimInTerminal(buf)
3385 call delete('XtestPopupWide')
3386endfunc
3387
3388func Test_popupwin_sign()
3389 CheckScreendump
3390
3391 let lines =<< trim END
3392 call setline(1, range(10))
3393 call sign_define('Current', {
3394 \ 'text': '>>',
3395 \ 'texthl': 'WarningMsg',
3396 \ 'linehl': 'Error',
3397 \ })
3398 call sign_define('Other', {
3399 \ 'text': '#!',
3400 \ 'texthl': 'Error',
3401 \ 'linehl': 'Search',
3402 \ })
3403 let winid = popup_create(['hello', 'bright', 'world'], {
3404 \ 'minwidth': 20,
3405 \ })
3406 call setwinvar(winid, "&signcolumn", "yes")
3407 let winbufnr = winbufnr(winid)
3408
3409 " add sign to current buffer, shows
3410 call sign_place(1, 'Selected', 'Current', bufnr('%'), {'lnum': 1})
3411 " add sign to current buffer, does not show
3412 call sign_place(2, 'PopUpSelected', 'Other', bufnr('%'), {'lnum': 2})
3413
3414 " add sign to popup buffer, shows
3415 call sign_place(3, 'PopUpSelected', 'Other', winbufnr, {'lnum': 1})
3416 " add sign to popup buffer, does not show
3417 call sign_place(4, 'Selected', 'Current', winbufnr, {'lnum': 2})
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02003418
3419 func SetOptions()
3420 call setwinvar(g:winid, '&number', 1)
3421 call setwinvar(g:winid, '&foldcolumn', 2)
3422 call popup_settext(g:winid, 'a longer line to check the width')
3423 endfunc
Bram Moolenaar20ebbea2019-11-30 17:58:27 +01003424 END
3425 call writefile(lines, 'XtestPopupSign')
3426
3427 let buf = RunVimInTerminal('-S XtestPopupSign', #{rows: 10})
3428 call VerifyScreenDump(buf, 'Test_popupwin_sign_1', {})
3429
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02003430 " set more options to check the width is adjusted
3431 call term_sendkeys(buf, ":call SetOptions()\<CR>")
3432 call VerifyScreenDump(buf, 'Test_popupwin_sign_2', {})
3433
Bram Moolenaar20ebbea2019-11-30 17:58:27 +01003434 call StopVimInTerminal(buf)
3435 call delete('XtestPopupSign')
3436endfunc
3437
Bram Moolenaar99ebf222019-12-10 23:44:48 +01003438func Test_popupwin_bufnr()
3439 let popwin = popup_create(['blah'], #{})
3440 let popbuf = winbufnr(popwin)
3441 split asdfasdf
3442 let newbuf = bufnr()
3443 call assert_true(newbuf > popbuf, 'New buffer number is higher')
3444 call assert_equal(newbuf, bufnr('$'))
3445 call popup_clear()
3446 let popwin = popup_create(['blah'], #{})
3447 " reuses previous buffer number
3448 call assert_equal(popbuf, winbufnr(popwin))
3449 call assert_equal(newbuf, bufnr('$'))
3450
3451 call popup_clear()
3452 bwipe!
3453endfunc
3454
Bram Moolenaarec084d32020-02-28 22:44:47 +01003455func Test_popupwin_filter_input_multibyte()
3456 func MyPopupFilter(winid, c)
3457 let g:bytes = range(a:c->strlen())->map({i -> char2nr(a:c[i])})
3458 return 0
3459 endfunc
3460 let winid = popup_create('', #{mapping: 0, filter: 'MyPopupFilter'})
3461
3462 " UTF-8: E3 80 80, including K_SPECIAL(0x80)
3463 call feedkeys("\u3000", 'xt')
3464 call assert_equal([0xe3, 0x80, 0x80], g:bytes)
3465
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01003466 " UTF-8: E3 80 9B, including CSI(0x9B)
3467 call feedkeys("\u301b", 'xt')
3468 call assert_equal([0xe3, 0x80, 0x9b], g:bytes)
Bram Moolenaarec084d32020-02-28 22:44:47 +01003469
Bram Moolenaar20298ce2020-06-19 21:46:52 +02003470 if has('unix')
3471 " with modifyOtherKeys <M-S-a> does not include a modifier sequence
Bram Moolenaarb326edf2020-06-20 15:03:38 +02003472 if has('gui_running')
3473 call feedkeys("\x9b\xfc\x08A", 'Lx!')
3474 else
3475 call feedkeys("\<Esc>[27;4;65~", 'Lx!')
3476 endif
Bram Moolenaar20298ce2020-06-19 21:46:52 +02003477 call assert_equal([0xc3, 0x81], g:bytes)
3478 endif
3479
Bram Moolenaarec084d32020-02-28 22:44:47 +01003480 call popup_clear()
3481 delfunc MyPopupFilter
3482 unlet g:bytes
3483endfunc
3484
Bram Moolenaar6f8f7332020-08-10 21:19:23 +02003485func Test_popupwin_filter_close_ctrl_c()
3486 CheckScreendump
3487
3488 let lines =<< trim END
3489 vsplit
3490 set laststatus=2
3491 set statusline=%!Statusline()
3492
3493 function Statusline() abort
3494 return '%<%f %h%m%r%=%-14.(%l,%c%V%) %P'
3495 endfunction
3496
3497 call popup_create('test test test test...', {'filter': {-> 0}})
3498 END
3499 call writefile(lines, 'XtestPopupCtrlC')
3500
3501 let buf = RunVimInTerminal('-S XtestPopupCtrlC', #{rows: 10})
3502
3503 call term_sendkeys(buf, "\<C-C>")
3504 call VerifyScreenDump(buf, 'Test_popupwin_ctrl_c', {})
3505
3506 call StopVimInTerminal(buf)
3507 call delete('XtestPopupCorners')
3508endfunc
3509
Bram Moolenaarba2920f2020-03-06 21:43:17 +01003510func Test_popupwin_atcursor_far_right()
3511 new
3512
3513 " this was getting stuck
3514 set signcolumn=yes
3515 call setline(1, repeat('=', &columns))
3516 normal! ggg$
Bram Moolenaaref6b9792020-05-13 16:34:15 +02003517 let winid = popup_atcursor(repeat('x', 500), #{moved: 'any', border: []})
Bram Moolenaarba2920f2020-03-06 21:43:17 +01003518
Bram Moolenaar6d585f42020-07-26 22:20:54 +02003519 " 'signcolumn' was getting reset
3520 call setwinvar(winid, '&signcolumn', 'yes')
3521 call popup_setoptions(winid, {'zindex': 1000})
3522 call assert_equal('yes', getwinvar(winid, '&signcolumn'))
3523
Bram Moolenaaref6b9792020-05-13 16:34:15 +02003524 call popup_close(winid)
Bram Moolenaarba2920f2020-03-06 21:43:17 +01003525 bwipe!
3526 set signcolumn&
3527endfunc
3528
Bram Moolenaar0f1563f2020-03-20 21:15:51 +01003529func Test_popupwin_splitmove()
3530 vsplit
3531 let win2 = win_getid()
3532 let popup_winid = popup_dialog('hello', {})
3533 call assert_fails('call win_splitmove(popup_winid, win2)', 'E957:')
3534 call assert_fails('call win_splitmove(win2, popup_winid)', 'E957:')
3535
3536 call popup_clear()
3537 bwipe
3538endfunc
3539
3540
Bram Moolenaar331bafd2019-07-20 17:46:05 +02003541" vim: shiftwidth=2 sts=2