blob: c44c8476198a091f0d2e01613163752231546ef2 [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,
756 \ zindex: 90,
757 \ padding: [],
758 \ highlight: 'PopupColor',
759 \ mask: [[1,1,1,1], [-5,-1,4,4], [7,9,2,3], [2,4,3,3]]})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200760 call popup_create([
761 \ 'xxxxxxxxx',
762 \ 'yyyyyyyyy',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200763 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200764 \ line: 3,
765 \ col: 18,
766 \ zindex: 20})
Bram Moolenaarba45f1f2019-07-03 22:50:41 +0200767 let winidb = popup_create([
768 \ 'just one line',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200769 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200770 \ line: 7,
771 \ col: 10,
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100772 \ posinvert: 0,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200773 \ wrap: 0,
774 \ fixed: 1,
775 \ close: 'button',
776 \ zindex: 90,
777 \ padding: [],
778 \ border: [],
779 \ 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 +0200780 END
781 call writefile(lines, 'XtestPopupMask')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200782 let buf = RunVimInTerminal('-S XtestPopupMask', #{rows: 13})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200783 call VerifyScreenDump(buf, 'Test_popupwin_mask_1', {})
784
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200785 call term_sendkeys(buf, ":call popup_move(winid, #{col: 11, line: 2})\<CR>")
786 call term_sendkeys(buf, ":call popup_move(winidb, #{col: 12})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200787 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200788 call VerifyScreenDump(buf, 'Test_popupwin_mask_2', {})
789
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200790 call term_sendkeys(buf, ":call popup_move(winid, #{col: 65, line: 2})\<CR>")
791 call term_sendkeys(buf, ":call popup_move(winidb, #{col: 63})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200792 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaard529ba52019-07-02 23:13:53 +0200793 call VerifyScreenDump(buf, 'Test_popupwin_mask_3', {})
794
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200795 call term_sendkeys(buf, ":call popup_move(winid, #{pos: 'topright', col: 12, line: 2})\<CR>")
796 call term_sendkeys(buf, ":call popup_move(winidb, #{pos: 'topright', col: 12})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200797 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaard529ba52019-07-02 23:13:53 +0200798 call VerifyScreenDump(buf, 'Test_popupwin_mask_4', {})
799
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200800 call term_sendkeys(buf, ":call popup_move(winid, #{pos: 'topright', col: 12, line: 11})\<CR>")
801 call term_sendkeys(buf, ":call popup_move(winidb, #{pos: 'topleft', col: 42, line: 11})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200802 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaarb4207472019-07-12 16:05:45 +0200803 call VerifyScreenDump(buf, 'Test_popupwin_mask_5', {})
804
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200805 " clean up
806 call StopVimInTerminal(buf)
807 call delete('XtestPopupMask')
808endfunc
809
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200810func Test_popup_select()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200811 CheckScreendump
812 CheckFeature clipboard_working
813
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200814 " create a popup with some text to be selected
815 let lines =<< trim END
Bram Moolenaar1755ec42019-06-15 13:13:54 +0200816 set clipboard=autoselect
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200817 call setline(1, range(1, 20))
Bram Moolenaar4dd751b2019-08-17 19:10:53 +0200818 let winid = popup_create(['the word', 'some more', 'several words here', 'invisible', '5', '6', '7'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200819 \ drag: 1,
820 \ border: [],
821 \ line: 3,
822 \ col: 10,
Bram Moolenaar4dd751b2019-08-17 19:10:53 +0200823 \ maxheight: 3,
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200824 \ })
825 func Select1()
826 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
827 endfunc
828 map <silent> <F3> :call test_setmouse(4, 15)<CR>
829 map <silent> <F4> :call test_setmouse(6, 23)<CR>
830 END
831 call writefile(lines, 'XtestPopupSelect')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200832 let buf = RunVimInTerminal('-S XtestPopupSelect', #{rows: 10})
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200833 call term_sendkeys(buf, ":call Select1()\<CR>")
834 call VerifyScreenDump(buf, 'Test_popupwin_select_01', {})
835
836 call term_sendkeys(buf, ":call popup_close(winid)\<CR>")
837 call term_sendkeys(buf, "\"*p")
Bram Moolenaar8ccabf62019-07-12 18:12:51 +0200838 " clean the command line, sometimes it still shows a command
839 call term_sendkeys(buf, ":\<esc>")
840
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200841 call VerifyScreenDump(buf, 'Test_popupwin_select_02', {})
842
843 " clean up
844 call StopVimInTerminal(buf)
845 call delete('XtestPopupSelect')
846endfunc
847
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200848func Test_popup_in_tab()
849 " default popup is local to tab, not visible when in other tab
850 let winid = popup_create("text", {})
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200851 let bufnr = winbufnr(winid)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200852 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200853 call assert_equal(0, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200854 tabnew
855 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200856 call assert_equal(1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200857 quit
858 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200859
860 call assert_equal(1, bufexists(bufnr))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200861 call popup_clear()
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200862 " buffer is gone now
863 call assert_equal(0, bufexists(bufnr))
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200864
865 " global popup is visible in any tab
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200866 let winid = popup_create("text", #{tabpage: -1})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200867 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200868 call assert_equal(-1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200869 tabnew
870 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200871 call assert_equal(-1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200872 quit
873 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200874 call popup_clear()
Bram Moolenaara3fce622019-06-20 02:31:49 +0200875
876 " create popup in other tab
877 tabnew
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200878 let winid = popup_create("text", #{tabpage: 1})
Bram Moolenaara3fce622019-06-20 02:31:49 +0200879 call assert_equal(0, popup_getpos(winid).visible)
880 call assert_equal(1, popup_getoptions(winid).tabpage)
881 quit
882 call assert_equal(1, popup_getpos(winid).visible)
883 call assert_equal(0, popup_getoptions(winid).tabpage)
884 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200885endfunc
886
887func Test_popup_valid_arguments()
Bram Moolenaaref6b9792020-05-13 16:34:15 +0200888 call assert_equal(0, len(popup_list()))
889
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200890 " Zero value is like the property wasn't there
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200891 let winid = popup_create("text", #{col: 0})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200892 let pos = popup_getpos(winid)
893 call assert_inrange(&columns / 2 - 1, &columns / 2 + 1, pos.col)
Bram Moolenaaref6b9792020-05-13 16:34:15 +0200894 call assert_equal([winid], popup_list())
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200895 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200896
897 " using cursor column has minimum value of 1
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200898 let winid = popup_create("text", #{col: 'cursor-100'})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200899 let pos = popup_getpos(winid)
900 call assert_equal(1, pos.col)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200901 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200902
903 " center
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200904 let winid = popup_create("text", #{pos: 'center'})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200905 let pos = popup_getpos(winid)
906 let around = (&columns - pos.width) / 2
907 call assert_inrange(around - 1, around + 1, pos.col)
908 let around = (&lines - pos.height) / 2
909 call assert_inrange(around - 1, around + 1, pos.line)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200910 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200911endfunc
912
913func Test_popup_invalid_arguments()
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +0200914 call assert_fails('call popup_create(666, {})', 'E86:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200915 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200916 call assert_fails('call popup_create("text", "none")', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200917 call popup_clear()
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200918 call assert_fails('call popup_create(test_null_string(), {})', 'E450:')
919 call assert_fails('call popup_create(test_null_list(), {})', 'E450:')
920 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200921
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200922 call assert_fails('call popup_create("text", #{col: "xxx"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200923 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200924 call assert_fails('call popup_create("text", #{col: "cursor8"})', 'E15:')
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: "cursor+x"})', '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+8x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200929 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200930
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200931 call assert_fails('call popup_create("text", #{line: "xxx"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200932 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200933 call assert_fails('call popup_create("text", #{line: "cursor8"})', 'E15:')
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: "cursor+x"})', '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+8x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200938 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200939
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200940 call assert_fails('call popup_create("text", #{pos: "there"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200941 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200942 call assert_fails('call popup_create("text", #{padding: "none"})', 'E714:')
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", #{border: "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", #{borderhighlight: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200947 call popup_clear()
Bram Moolenaar403d0902019-07-17 21:37:32 +0200948 call assert_fails('call popup_create("text", #{borderhighlight: test_null_list()})', 'E714:')
949 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200950 call assert_fails('call popup_create("text", #{borderchars: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200951 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200952
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200953 call assert_fails('call popup_create([#{text: "text"}, 666], {})', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200954 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200955 call assert_fails('call popup_create([#{text: "text", props: "none"}], {})', 'E714:')
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"]}], {})', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200958 call popup_clear()
Bram Moolenaarb0992022020-01-30 14:55:42 +0100959 call assert_fails('call popup_create([#{text: "text", props: range(3)}], {})', 'E715:')
960 call popup_clear()
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200961 call assert_fails('call popup_create("text", #{mask: ["asdf"]})', 'E475:')
962 call popup_clear()
Bram Moolenaarb0992022020-01-30 14:55:42 +0100963 call assert_fails('call popup_create("text", #{mask: range(5)})', 'E475:')
964 call popup_clear()
965 call popup_create("text", #{mask: [range(4)]})
966 call popup_clear()
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200967 call assert_fails('call popup_create("text", #{mask: test_null_list()})', 'E475:')
Bram Moolenaar749fa0a2019-08-03 16:18:07 +0200968 call assert_fails('call popup_create("text", #{mapping: []})', 'E745:')
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200969 call popup_clear()
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200970 call assert_fails('call popup_create("text", #{tabpage : 4})', 'E997:')
971 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200972endfunc
973
Bram Moolenaareea16992019-05-31 17:34:48 +0200974func Test_win_execute_closing_curwin()
975 split
976 let winid = popup_create('some text', {})
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200977 call assert_fails('call win_execute(winid, winnr() .. "close")', 'E994')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200978 call popup_clear()
Bram Moolenaar49540192019-12-11 19:34:54 +0100979
980 let winid = popup_create('some text', {})
981 call assert_fails('call win_execute(winid, printf("normal! :\<C-u>call popup_close(%d)\<CR>", winid))', 'E994')
982 call popup_clear()
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200983endfunc
984
985func Test_win_execute_not_allowed()
986 let winid = popup_create('some text', {})
987 call assert_fails('call win_execute(winid, "split")', 'E994:')
988 call assert_fails('call win_execute(winid, "vsplit")', 'E994:')
989 call assert_fails('call win_execute(winid, "close")', 'E994:')
990 call assert_fails('call win_execute(winid, "bdelete")', 'E994:')
Bram Moolenaar2d247842019-06-01 17:06:25 +0200991 call assert_fails('call win_execute(winid, "bwipe!")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200992 call assert_fails('call win_execute(winid, "tabnew")', 'E994:')
993 call assert_fails('call win_execute(winid, "tabnext")', 'E994:')
994 call assert_fails('call win_execute(winid, "next")', 'E994:')
995 call assert_fails('call win_execute(winid, "rewind")', 'E994:')
Bram Moolenaar3a2505c2020-03-09 16:40:41 +0100996 call assert_fails('call win_execute(winid, "pedit filename")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200997 call assert_fails('call win_execute(winid, "buf")', 'E994:')
Bram Moolenaar3029bcc2020-01-18 15:06:19 +0100998 call assert_fails('call win_execute(winid, "bnext")', 'E994:')
999 call assert_fails('call win_execute(winid, "bprev")', 'E994:')
1000 call assert_fails('call win_execute(winid, "bfirst")', 'E994:')
1001 call assert_fails('call win_execute(winid, "blast")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001002 call assert_fails('call win_execute(winid, "edit")', 'E994:')
1003 call assert_fails('call win_execute(winid, "enew")', 'E994:')
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +02001004 call assert_fails('call win_execute(winid, "help")', 'E994:')
1005 call assert_fails('call win_execute(winid, "1only")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001006 call assert_fails('call win_execute(winid, "wincmd x")', 'E994:')
1007 call assert_fails('call win_execute(winid, "wincmd w")', 'E994:')
1008 call assert_fails('call win_execute(winid, "wincmd t")', 'E994:')
1009 call assert_fails('call win_execute(winid, "wincmd b")', 'E994:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001010 call popup_clear()
Bram Moolenaareea16992019-05-31 17:34:48 +02001011endfunc
1012
Bram Moolenaar402502d2019-05-30 22:07:36 +02001013func Test_popup_with_wrap()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001014 CheckScreendump
1015
Bram Moolenaar402502d2019-05-30 22:07:36 +02001016 let lines =<< trim END
1017 call setline(1, range(1, 100))
1018 let winid = popup_create(
1019 \ 'a long line that wont fit',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001020 \ #{line: 3, col: 20, maxwidth: 10, wrap: 1})
Bram Moolenaar402502d2019-05-30 22:07:36 +02001021 END
1022 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001023 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar402502d2019-05-30 22:07:36 +02001024 call VerifyScreenDump(buf, 'Test_popupwin_wrap', {})
1025
1026 " clean up
1027 call StopVimInTerminal(buf)
1028 call delete('XtestPopup')
1029endfunc
1030
1031func Test_popup_without_wrap()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001032 CheckScreendump
1033
Bram Moolenaar402502d2019-05-30 22:07:36 +02001034 let lines =<< trim END
1035 call setline(1, range(1, 100))
1036 let winid = popup_create(
1037 \ 'a long line that wont fit',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001038 \ #{line: 3, col: 20, maxwidth: 10, wrap: 0})
Bram Moolenaar402502d2019-05-30 22:07:36 +02001039 END
1040 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001041 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar402502d2019-05-30 22:07:36 +02001042 call VerifyScreenDump(buf, 'Test_popupwin_nowrap', {})
1043
1044 " clean up
1045 call StopVimInTerminal(buf)
1046 call delete('XtestPopup')
1047endfunc
1048
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001049func Test_popup_with_showbreak()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001050 CheckScreendump
1051
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001052 let lines =<< trim END
1053 set showbreak=>>\
1054 call setline(1, range(1, 20))
1055 let winid = popup_dialog(
Bram Moolenaar8ae54372019-09-15 18:11:16 +02001056 \ 'a long line here that wraps',
1057 \ #{filter: 'popup_filter_yesno',
1058 \ maxwidth: 12})
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001059 END
1060 call writefile(lines, 'XtestPopupShowbreak')
1061 let buf = RunVimInTerminal('-S XtestPopupShowbreak', #{rows: 10})
1062 call VerifyScreenDump(buf, 'Test_popupwin_showbreak', {})
1063
1064 " clean up
1065 call term_sendkeys(buf, "y")
1066 call StopVimInTerminal(buf)
1067 call delete('XtestPopupShowbreak')
1068endfunc
1069
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001070func Test_popup_time()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001071 CheckFeature timers
1072
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001073 topleft vnew
1074 call setline(1, 'hello')
1075
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001076 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001077 \ line: 1,
1078 \ col: 1,
1079 \ minwidth: 20,
1080 \ time: 500,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001081 \})
1082 redraw
1083 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1084 call assert_equal('world', line)
1085
Bram Moolenaarb4f06282019-07-12 21:07:54 +02001086 call assert_equal(winid, popup_locate(1, 1))
1087 call assert_equal(winid, popup_locate(1, 20))
1088 call assert_equal(0, popup_locate(1, 21))
1089 call assert_equal(0, popup_locate(2, 1))
1090
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001091 sleep 700m
Bram Moolenaar35d5af62019-05-26 20:44:10 +02001092 redraw
Bram Moolenaar196b4662019-09-06 21:34:30 +02001093 let line = join(map(range(1, 5), '1->screenstring(v:val)'), '')
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001094 call assert_equal('hello', line)
1095
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001096 call popup_create('on the command line', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001097 \ line: &lines,
1098 \ col: 10,
1099 \ minwidth: 20,
1100 \ time: 500,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001101 \})
1102 redraw
1103 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
1104 call assert_match('.*on the command line.*', line)
1105
1106 sleep 700m
1107 redraw
1108 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
1109 call assert_notmatch('.*on the command line.*', line)
1110
1111 bwipe!
1112endfunc
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001113
1114func Test_popup_hide()
1115 topleft vnew
1116 call setline(1, 'hello')
1117
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001118 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001119 \ line: 1,
1120 \ col: 1,
1121 \ minwidth: 20,
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001122 \})
1123 redraw
1124 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1125 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001126 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001127 " buffer is still listed and active
1128 call assert_match(winbufnr(winid) .. 'u a.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001129
1130 call popup_hide(winid)
1131 redraw
1132 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1133 call assert_equal('hello', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001134 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001135 " buffer is still listed but hidden
Bram Moolenaarca7c0782020-01-14 20:42:48 +01001136 call assert_match(winbufnr(winid) .. 'u a.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001137
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001138 eval winid->popup_show()
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001139 redraw
1140 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1141 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001142 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001143
1144
1145 call popup_close(winid)
1146 redraw
1147 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1148 call assert_equal('hello', line)
1149
1150 " error is given for existing non-popup window
1151 call assert_fails('call popup_hide(win_getid())', 'E993:')
1152
1153 " no error non-existing window
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001154 eval 1234234->popup_hide()
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001155 call popup_show(41234234)
1156
1157 bwipe!
1158endfunc
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001159
1160func Test_popup_move()
1161 topleft vnew
1162 call setline(1, 'hello')
1163
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001164 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001165 \ line: 1,
1166 \ col: 1,
1167 \ minwidth: 20,
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001168 \})
1169 redraw
1170 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
1171 call assert_equal('world ', line)
1172
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001173 call popup_move(winid, #{line: 2, col: 2})
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001174 redraw
1175 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
1176 call assert_equal('hello ', line)
1177 let line = join(map(range(1, 6), 'screenstring(2, v:val)'), '')
1178 call assert_equal('~world', line)
1179
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001180 eval winid->popup_move(#{line: 1})
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001181 redraw
1182 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
1183 call assert_equal('hworld', line)
1184
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02001185 call assert_fails('call popup_move(winid, [])', 'E715:')
1186 call assert_fails('call popup_move(winid, test_null_dict())', 'E715:')
1187
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001188 call popup_close(winid)
1189
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02001190 call assert_equal(0, popup_move(-1, {}))
1191
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001192 bwipe!
1193endfunc
Bram Moolenaarbc133542019-05-29 20:26:46 +02001194
Bram Moolenaar402502d2019-05-30 22:07:36 +02001195func Test_popup_getpos()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001196 let winid = popup_create('hello', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001197 \ line: 2,
1198 \ col: 3,
1199 \ minwidth: 10,
1200 \ minheight: 11,
Bram Moolenaarbc133542019-05-29 20:26:46 +02001201 \})
1202 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +02001203 let res = popup_getpos(winid)
Bram Moolenaarbc133542019-05-29 20:26:46 +02001204 call assert_equal(2, res.line)
1205 call assert_equal(3, res.col)
1206 call assert_equal(10, res.width)
1207 call assert_equal(11, res.height)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001208 call assert_equal(1, res.visible)
Bram Moolenaarbc133542019-05-29 20:26:46 +02001209
1210 call popup_close(winid)
1211endfunc
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001212
1213func Test_popup_width_longest()
1214 let tests = [
1215 \ [['hello', 'this', 'window', 'displays', 'all of its text'], 15],
1216 \ [['hello', 'this', 'window', 'all of its text', 'displays'], 15],
1217 \ [['hello', 'this', 'all of its text', 'window', 'displays'], 15],
1218 \ [['hello', 'all of its text', 'this', 'window', 'displays'], 15],
1219 \ [['all of its text', 'hello', 'this', 'window', 'displays'], 15],
1220 \ ]
1221
1222 for test in tests
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001223 let winid = popup_create(test[0], #{line: 2, col: 3})
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001224 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +02001225 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001226 call assert_equal(test[1], position.width)
1227 call popup_close(winid)
1228 endfor
1229endfunc
1230
1231func Test_popup_wraps()
1232 let tests = [
1233 \ ['nowrap', 6, 1],
1234 \ ['a line that wraps once', 12, 2],
1235 \ ['a line that wraps two times', 12, 3],
1236 \ ]
1237 for test in tests
1238 let winid = popup_create(test[0],
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001239 \ #{line: 2, col: 3, maxwidth: 12})
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001240 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +02001241 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001242 call assert_equal(test[1], position.width)
1243 call assert_equal(test[2], position.height)
1244
1245 call popup_close(winid)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001246 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001247 endfor
1248endfunc
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001249
1250func Test_popup_getoptions()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001251 let winid = popup_create('hello', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001252 \ line: 2,
1253 \ col: 3,
1254 \ minwidth: 10,
1255 \ minheight: 11,
1256 \ maxwidth: 20,
1257 \ maxheight: 21,
1258 \ zindex: 100,
1259 \ time: 5000,
1260 \ fixed: 1
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001261 \})
1262 redraw
1263 let res = popup_getoptions(winid)
1264 call assert_equal(2, res.line)
1265 call assert_equal(3, res.col)
1266 call assert_equal(10, res.minwidth)
1267 call assert_equal(11, res.minheight)
1268 call assert_equal(20, res.maxwidth)
1269 call assert_equal(21, res.maxheight)
1270 call assert_equal(100, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001271 call assert_equal(1, res.fixed)
Bram Moolenaarb8350ab2019-08-04 17:59:49 +02001272 call assert_equal(1, res.mapping)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001273 if has('timers')
1274 call assert_equal(5000, res.time)
1275 endif
1276 call popup_close(winid)
1277
1278 let winid = popup_create('hello', {})
1279 redraw
1280 let res = popup_getoptions(winid)
1281 call assert_equal(0, res.line)
1282 call assert_equal(0, res.col)
1283 call assert_equal(0, res.minwidth)
1284 call assert_equal(0, res.minheight)
1285 call assert_equal(0, res.maxwidth)
1286 call assert_equal(0, res.maxheight)
1287 call assert_equal(50, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001288 call assert_equal(0, res.fixed)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001289 if has('timers')
1290 call assert_equal(0, res.time)
1291 endif
1292 call popup_close(winid)
1293 call assert_equal({}, popup_getoptions(winid))
1294endfunc
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001295
1296func Test_popup_option_values()
1297 new
1298 " window-local
1299 setlocal number
1300 setlocal nowrap
1301 " buffer-local
1302 setlocal omnifunc=Something
1303 " global/buffer-local
1304 setlocal path=/there
1305 " global/window-local
Bram Moolenaara112f2d2019-09-01 17:38:09 +02001306 setlocal statusline=2
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001307
1308 let winid = popup_create('hello', {})
1309 call assert_equal(0, getwinvar(winid, '&number'))
1310 call assert_equal(1, getwinvar(winid, '&wrap'))
1311 call assert_equal('', getwinvar(winid, '&omnifunc'))
1312 call assert_equal(&g:path, getwinvar(winid, '&path'))
Bram Moolenaara112f2d2019-09-01 17:38:09 +02001313 call assert_equal(&g:statusline, getwinvar(winid, '&statusline'))
1314
1315 " 'scrolloff' is reset to zero
1316 call assert_equal(5, &scrolloff)
1317 call assert_equal(0, getwinvar(winid, '&scrolloff'))
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001318
1319 call popup_close(winid)
1320 bwipe
1321endfunc
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001322
1323func Test_popup_atcursor()
1324 topleft vnew
1325 call setline(1, [
1326 \ 'xxxxxxxxxxxxxxxxx',
1327 \ 'xxxxxxxxxxxxxxxxx',
1328 \ 'xxxxxxxxxxxxxxxxx',
1329 \])
1330
1331 call cursor(2, 2)
1332 redraw
1333 let winid = popup_atcursor('vim', {})
1334 redraw
1335 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
1336 call assert_equal('xvimxxxxxxxxxxxxx', line)
1337 call popup_close(winid)
1338
1339 call cursor(3, 4)
1340 redraw
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001341 let winid = 'vim'->popup_atcursor({})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001342 redraw
1343 let line = join(map(range(1, 17), 'screenstring(2, v:val)'), '')
1344 call assert_equal('xxxvimxxxxxxxxxxx', line)
1345 call popup_close(winid)
1346
1347 call cursor(1, 1)
1348 redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001349 let winid = popup_create('vim', #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001350 \ line: 'cursor+2',
1351 \ col: 'cursor+1',
1352 \})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001353 redraw
1354 let line = join(map(range(1, 17), 'screenstring(3, v:val)'), '')
1355 call assert_equal('xvimxxxxxxxxxxxxx', line)
1356 call popup_close(winid)
1357
1358 call cursor(3, 3)
1359 redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001360 let winid = popup_create('vim', #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001361 \ line: 'cursor-2',
1362 \ col: 'cursor-1',
1363 \})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001364 redraw
1365 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
1366 call assert_equal('xvimxxxxxxxxxxxxx', line)
1367 call popup_close(winid)
1368
Bram Moolenaar402502d2019-05-30 22:07:36 +02001369 " just enough room above
1370 call cursor(3, 3)
1371 redraw
1372 let winid = popup_atcursor(['vim', 'is great'], {})
1373 redraw
1374 let pos = popup_getpos(winid)
1375 call assert_equal(1, pos.line)
1376 call popup_close(winid)
1377
1378 " not enough room above, popup goes below the cursor
1379 call cursor(3, 3)
1380 redraw
1381 let winid = popup_atcursor(['vim', 'is', 'great'], {})
1382 redraw
1383 let pos = popup_getpos(winid)
1384 call assert_equal(4, pos.line)
1385 call popup_close(winid)
1386
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001387 " cursor in first line, popup in line 2
1388 call cursor(1, 1)
1389 redraw
1390 let winid = popup_atcursor(['vim', 'is', 'great'], {})
1391 redraw
1392 let pos = popup_getpos(winid)
1393 call assert_equal(2, pos.line)
1394 call popup_close(winid)
1395
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001396 bwipe!
1397endfunc
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001398
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001399func Test_popup_atcursor_pos()
1400 CheckScreendump
1401
1402 let lines =<< trim END
1403 call setline(1, repeat([repeat('-', 60)], 15))
1404 set so=0
1405
1406 normal 9G3|r#
1407 let winid1 = popup_atcursor(['first', 'second'], #{
1408 \ moved: [0, 0, 0],
1409 \ })
1410 normal 9G21|r&
1411 let winid1 = popup_atcursor(['FIrsT', 'SEcoND'], #{
1412 \ pos: 'botright',
1413 \ moved: [0, 0, 0],
1414 \ })
1415 normal 3G27|r%
1416 let winid1 = popup_atcursor(['fiRSt', 'seCOnd'], #{
1417 \ pos: 'topleft',
1418 \ moved: [0, 0, 0],
1419 \ })
1420 normal 3G45|r@
1421 let winid1 = popup_atcursor(['First', 'SeconD'], #{
1422 \ pos: 'topright',
Bram Moolenaarb0992022020-01-30 14:55:42 +01001423 \ moved: range(3),
1424 \ mousemoved: range(3),
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001425 \ })
1426 END
1427 call writefile(lines, 'XtestPopupAtcursorPos')
1428 let buf = RunVimInTerminal('-S XtestPopupAtcursorPos', #{rows: 12})
1429 call VerifyScreenDump(buf, 'Test_popupwin_atcursor_pos', {})
1430
1431 " clean up
1432 call StopVimInTerminal(buf)
1433 call delete('XtestPopupAtcursorPos')
1434endfunc
1435
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001436func Test_popup_beval()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001437 CheckScreendump
Bram Moolenaar1e82a782019-09-21 22:57:06 +02001438 CheckFeature balloon_eval_term
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001439
1440 let lines =<< trim END
1441 call setline(1, range(1, 20))
1442 call setline(5, 'here is some text to hover over')
1443 set balloonevalterm
1444 set balloonexpr=BalloonExpr()
1445 set balloondelay=100
1446 func BalloonExpr()
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001447 let s:winid = [v:beval_text]->popup_beval({})
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001448 return ''
1449 endfunc
1450 func Hover()
1451 call test_setmouse(5, 15)
1452 call feedkeys("\<MouseMove>\<Ignore>", "xt")
1453 sleep 100m
1454 endfunc
1455 func MoveOntoPopup()
1456 call test_setmouse(4, 17)
1457 call feedkeys("\<F4>\<MouseMove>\<Ignore>", "xt")
1458 endfunc
1459 func MoveAway()
1460 call test_setmouse(5, 13)
1461 call feedkeys("\<F5>\<MouseMove>\<Ignore>", "xt")
1462 endfunc
1463 END
1464 call writefile(lines, 'XtestPopupBeval')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001465 let buf = RunVimInTerminal('-S XtestPopupBeval', #{rows: 10})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001466 call TermWait(buf, 50)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001467 call term_sendkeys(buf, 'j')
1468 call term_sendkeys(buf, ":call Hover()\<CR>")
1469 call VerifyScreenDump(buf, 'Test_popupwin_beval_1', {})
1470
1471 call term_sendkeys(buf, ":call MoveOntoPopup()\<CR>")
1472 call VerifyScreenDump(buf, 'Test_popupwin_beval_2', {})
1473
1474 call term_sendkeys(buf, ":call MoveAway()\<CR>")
1475 call VerifyScreenDump(buf, 'Test_popupwin_beval_3', {})
1476
1477 " clean up
1478 call StopVimInTerminal(buf)
1479 call delete('XtestPopupBeval')
1480endfunc
1481
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001482func Test_popup_filter()
1483 new
1484 call setline(1, 'some text')
1485
1486 func MyPopupFilter(winid, c)
Bram Moolenaarec084d32020-02-28 22:44:47 +01001487 if a:c == 'e' || a:c == "\<F9>"
1488 let g:eaten = a:c
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001489 return 1
1490 endif
1491 if a:c == '0'
1492 let g:ignored = '0'
1493 return 0
1494 endif
1495 if a:c == 'x'
1496 call popup_close(a:winid)
1497 return 1
1498 endif
1499 return 0
1500 endfunc
1501
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001502 let winid = 'something'->popup_create(#{filter: 'MyPopupFilter'})
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001503 redraw
1504
1505 " e is consumed by the filter
1506 call feedkeys('e', 'xt')
1507 call assert_equal('e', g:eaten)
Bram Moolenaarec084d32020-02-28 22:44:47 +01001508 call feedkeys("\<F9>", 'xt')
1509 call assert_equal("\<F9>", g:eaten)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001510
1511 " 0 is ignored by the filter
1512 normal $
1513 call assert_equal(9, getcurpos()[2])
1514 call feedkeys('0', 'xt')
1515 call assert_equal('0', g:ignored)
1516 call assert_equal(1, getcurpos()[2])
1517
1518 " x closes the popup
1519 call feedkeys('x', 'xt')
Bram Moolenaarec084d32020-02-28 22:44:47 +01001520 call assert_equal("\<F9>", g:eaten)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001521 call assert_equal(-1, winbufnr(winid))
1522
1523 delfunc MyPopupFilter
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001524 call popup_clear()
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001525endfunc
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001526
Bram Moolenaara42d9452019-06-15 21:46:30 +02001527func ShowDialog(key, result)
1528 let s:cb_res = 999
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001529 let winid = popup_dialog('do you want to quit (Yes/no)?', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001530 \ filter: 'popup_filter_yesno',
1531 \ callback: 'QuitCallback',
Bram Moolenaara42d9452019-06-15 21:46:30 +02001532 \ })
1533 redraw
1534 call feedkeys(a:key, "xt")
1535 call assert_equal(winid, s:cb_winid)
1536 call assert_equal(a:result, s:cb_res)
1537endfunc
1538
1539func Test_popup_dialog()
1540 func QuitCallback(id, res)
1541 let s:cb_winid = a:id
1542 let s:cb_res = a:res
1543 endfunc
1544
1545 let winid = ShowDialog("y", 1)
1546 let winid = ShowDialog("Y", 1)
1547 let winid = ShowDialog("n", 0)
1548 let winid = ShowDialog("N", 0)
1549 let winid = ShowDialog("x", 0)
1550 let winid = ShowDialog("X", 0)
1551 let winid = ShowDialog("\<Esc>", 0)
1552 let winid = ShowDialog("\<C-C>", -1)
1553
1554 delfunc QuitCallback
1555endfunc
1556
Bram Moolenaara730e552019-06-16 19:05:31 +02001557func ShowMenu(key, result)
1558 let s:cb_res = 999
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001559 let winid = popup_menu(['one', 'two', 'something else'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001560 \ callback: 'QuitCallback',
Bram Moolenaara730e552019-06-16 19:05:31 +02001561 \ })
1562 redraw
1563 call feedkeys(a:key, "xt")
1564 call assert_equal(winid, s:cb_winid)
1565 call assert_equal(a:result, s:cb_res)
1566endfunc
1567
1568func Test_popup_menu()
1569 func QuitCallback(id, res)
1570 let s:cb_winid = a:id
1571 let s:cb_res = a:res
1572 endfunc
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001573 " mapping won't be used in popup
1574 map j k
Bram Moolenaara730e552019-06-16 19:05:31 +02001575
1576 let winid = ShowMenu(" ", 1)
1577 let winid = ShowMenu("j \<CR>", 2)
1578 let winid = ShowMenu("JjK \<CR>", 2)
1579 let winid = ShowMenu("jjjjjj ", 3)
1580 let winid = ShowMenu("kkk ", 1)
1581 let winid = ShowMenu("x", -1)
1582 let winid = ShowMenu("X", -1)
1583 let winid = ShowMenu("\<Esc>", -1)
1584 let winid = ShowMenu("\<C-C>", -1)
1585
1586 delfunc QuitCallback
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001587 unmap j
Bram Moolenaara730e552019-06-16 19:05:31 +02001588endfunc
1589
1590func Test_popup_menu_screenshot()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001591 CheckScreendump
Bram Moolenaara730e552019-06-16 19:05:31 +02001592
1593 let lines =<< trim END
1594 call setline(1, range(1, 20))
1595 hi PopupSelected ctermbg=lightblue
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001596 call popup_menu(['one', 'two', 'another'], #{callback: 'MenuDone', title: ' make a choice from the list '})
Bram Moolenaara730e552019-06-16 19:05:31 +02001597 func MenuDone(id, res)
1598 echomsg "selected " .. a:res
1599 endfunc
1600 END
1601 call writefile(lines, 'XtestPopupMenu')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001602 let buf = RunVimInTerminal('-S XtestPopupMenu', #{rows: 10})
Bram Moolenaara730e552019-06-16 19:05:31 +02001603 call VerifyScreenDump(buf, 'Test_popupwin_menu_01', {})
1604
1605 call term_sendkeys(buf, "jj")
1606 call VerifyScreenDump(buf, 'Test_popupwin_menu_02', {})
1607
1608 call term_sendkeys(buf, " ")
1609 call VerifyScreenDump(buf, 'Test_popupwin_menu_03', {})
1610
1611 " clean up
1612 call StopVimInTerminal(buf)
1613 call delete('XtestPopupMenu')
1614endfunc
1615
Bram Moolenaarf914a332019-07-20 15:09:56 +02001616func Test_popup_menu_narrow()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001617 CheckScreendump
Bram Moolenaarf914a332019-07-20 15:09:56 +02001618
1619 let lines =<< trim END
1620 call setline(1, range(1, 20))
1621 hi PopupSelected ctermbg=green
1622 call popup_menu(['one', 'two', 'three'], #{callback: 'MenuDone'})
1623 func MenuDone(id, res)
1624 echomsg "selected " .. a:res
1625 endfunc
1626 END
1627 call writefile(lines, 'XtestPopupNarrowMenu')
1628 let buf = RunVimInTerminal('-S XtestPopupNarrowMenu', #{rows: 10})
1629 call VerifyScreenDump(buf, 'Test_popupwin_menu_04', {})
1630
1631 " clean up
1632 call term_sendkeys(buf, "x")
1633 call StopVimInTerminal(buf)
1634 call delete('XtestPopupNarrowMenu')
1635endfunc
1636
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001637func Test_popup_title()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001638 CheckScreendump
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001639
1640 " Create a popup without title or border, a line of padding will be added to
1641 " put the title on.
1642 let lines =<< trim END
1643 call setline(1, range(1, 20))
Bram Moolenaar5d458a72019-08-04 21:12:15 +02001644 let winid = popup_create(['one', 'two', 'another'], #{title: 'Title String'})
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001645 END
1646 call writefile(lines, 'XtestPopupTitle')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001647 let buf = RunVimInTerminal('-S XtestPopupTitle', #{rows: 10})
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001648 call VerifyScreenDump(buf, 'Test_popupwin_title', {})
1649
Bram Moolenaar5d458a72019-08-04 21:12:15 +02001650 call term_sendkeys(buf, ":call popup_setoptions(winid, #{maxwidth: 20, title: 'a very long title that is not going to fit'})\<CR>")
1651 call term_sendkeys(buf, ":\<CR>")
1652 call VerifyScreenDump(buf, 'Test_popupwin_longtitle_1', {})
1653
1654 call term_sendkeys(buf, ":call popup_setoptions(winid, #{border: []})\<CR>")
1655 call term_sendkeys(buf, ":\<CR>")
1656 call VerifyScreenDump(buf, 'Test_popupwin_longtitle_2', {})
1657
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001658 " clean up
1659 call StopVimInTerminal(buf)
1660 call delete('XtestPopupTitle')
Bram Moolenaarae943152019-06-16 22:54:14 +02001661
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001662 let winid = popup_create('something', #{title: 'Some Title'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001663 call assert_equal('Some Title', popup_getoptions(winid).title)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001664 call popup_setoptions(winid, #{title: 'Another Title'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001665 call assert_equal('Another Title', popup_getoptions(winid).title)
1666
1667 call popup_clear()
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001668endfunc
1669
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001670func Test_popup_close_callback()
1671 func PopupDone(id, result)
1672 let g:result = a:result
1673 endfunc
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001674 let winid = popup_create('something', #{callback: 'PopupDone'})
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001675 redraw
1676 call popup_close(winid, 'done')
1677 call assert_equal('done', g:result)
1678endfunc
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001679
1680func Test_popup_empty()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001681 let winid = popup_create('', #{padding: [2,2,2,2]})
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001682 redraw
1683 let pos = popup_getpos(winid)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001684 call assert_equal(5, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001685 call assert_equal(5, pos.height)
Bram Moolenaaref6b9792020-05-13 16:34:15 +02001686 call popup_close(winid)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001687
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001688 let winid = popup_create([], #{border: []})
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001689 redraw
1690 let pos = popup_getpos(winid)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001691 call assert_equal(3, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001692 call assert_equal(3, pos.height)
Bram Moolenaaref6b9792020-05-13 16:34:15 +02001693 call popup_close(winid)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001694endfunc
Bram Moolenaar988c4332019-06-02 14:12:11 +02001695
1696func Test_popup_never_behind()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001697 CheckScreendump
1698
Bram Moolenaar988c4332019-06-02 14:12:11 +02001699 " +-----------------------------+
1700 " | | |
1701 " | | |
1702 " | | |
1703 " | line1 |
1704 " |------------line2------------|
1705 " | line3 |
1706 " | line4 |
1707 " | |
1708 " | |
1709 " +-----------------------------+
1710 let lines =<< trim END
Bram Moolenaar988c4332019-06-02 14:12:11 +02001711 split
1712 vsplit
1713 let info_window1 = getwininfo()[0]
1714 let line = info_window1['height']
1715 let col = info_window1['width']
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001716 call popup_create(['line1', 'line2', 'line3', 'line4'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001717 \ line : line,
1718 \ col : col,
Bram Moolenaar988c4332019-06-02 14:12:11 +02001719 \ })
1720 END
1721 call writefile(lines, 'XtestPopupBehind')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001722 let buf = RunVimInTerminal('-S XtestPopupBehind', #{rows: 10})
Bram Moolenaar988c4332019-06-02 14:12:11 +02001723 call term_sendkeys(buf, "\<C-W>w")
1724 call VerifyScreenDump(buf, 'Test_popupwin_behind', {})
1725
1726 " clean up
1727 call StopVimInTerminal(buf)
1728 call delete('XtestPopupBehind')
1729endfunc
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001730
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001731func s:VerifyPosition(p, msg, line, col, width, height)
1732 call assert_equal(a:line, popup_getpos(a:p).line, a:msg . ' (l)')
1733 call assert_equal(a:col, popup_getpos(a:p).col, a:msg . ' (c)')
1734 call assert_equal(a:width, popup_getpos(a:p).width, a:msg . ' (w)')
1735 call assert_equal(a:height, popup_getpos(a:p).height, a:msg . ' (h)')
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001736endfunc
1737
1738func Test_popup_position_adjust()
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001739 " Anything placed past the last cell on the right of the screen is moved to
1740 " the left.
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001741 "
1742 " When wrapping is disabled, we also shift to the left to display on the
1743 " screen, unless fixed is set.
1744
1745 " Entries for cases which don't vary based on wrapping.
1746 " Format is per tests described below
1747 let both_wrap_tests = [
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001748 \ ['a', 5, &columns, 5, &columns, 1, 1],
1749 \ ['b', 5, &columns + 1, 5, &columns, 1, 1],
1750 \ ['c', 5, &columns - 1, 5, &columns - 1, 1, 1],
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001751 \ ['d', 5, &columns - 2, 5, &columns - 2, 1, 1],
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001752 \ ['e', 5, &columns - 3, 5, &columns - 3, 1, 1]]
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001753
1754 " these test groups are dicts with:
1755 " - comment: something to identify the group of tests by
1756 " - options: dict of options to merge with the row/col in tests
1757 " - tests: list of cases. Each one is a list with elements:
1758 " - text
1759 " - row
1760 " - col
1761 " - expected row
1762 " - expected col
1763 " - expected width
1764 " - expected height
1765 let tests = [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001766 \ #{
1767 \ comment: 'left-aligned with wrapping',
1768 \ options: #{
1769 \ wrap: 1,
1770 \ pos: 'botleft',
1771 \ },
1772 \ tests: both_wrap_tests + [
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001773 \ ['aa', 5, &columns, 4, &columns, 1, 2],
1774 \ ['bb', 5, &columns + 1, 4, &columns, 1, 2],
1775 \ ['cc', 5, &columns - 1, 5, &columns - 1, 2, 1],
1776 \ ['dd', 5, &columns - 2, 5, &columns - 2, 2, 1],
1777 \ ['ee', 5, &columns - 3, 5, &columns - 3, 2, 1],
1778 \
1779 \ ['aaa', 5, &columns, 3, &columns, 1, 3],
1780 \ ['bbb', 5, &columns + 1, 3, &columns, 1, 3],
1781 \ ['ccc', 5, &columns - 1, 4, &columns - 1, 2, 2],
1782 \ ['ddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
1783 \ ['eee', 5, &columns - 3, 5, &columns - 3, 3, 1],
1784 \
1785 \ ['aaaa', 5, &columns, 2, &columns, 1, 4],
1786 \ ['bbbb', 5, &columns + 1, 2, &columns, 1, 4],
1787 \ ['cccc', 5, &columns - 1, 4, &columns - 1, 2, 2],
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001788 \ ['dddd', 5, &columns - 2, 4, &columns - 2, 3, 2],
1789 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001790 \ ['eeee', 5, &columns - 4, 5, &columns - 4, 4, 1],
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001791 \ ],
1792 \ },
1793 \ #{
1794 \ comment: 'left aligned without wrapping',
1795 \ options: #{
1796 \ wrap: 0,
1797 \ pos: 'botleft',
1798 \ },
1799 \ tests: both_wrap_tests + [
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001800 \ ['aa', 5, &columns, 5, &columns - 1, 2, 1],
1801 \ ['bb', 5, &columns + 1, 5, &columns - 1, 2, 1],
1802 \ ['cc', 5, &columns - 1, 5, &columns - 1, 2, 1],
1803 \ ['dd', 5, &columns - 2, 5, &columns - 2, 2, 1],
1804 \ ['ee', 5, &columns - 3, 5, &columns - 3, 2, 1],
1805 \
1806 \ ['aaa', 5, &columns, 5, &columns - 2, 3, 1],
1807 \ ['bbb', 5, &columns + 1, 5, &columns - 2, 3, 1],
1808 \ ['ccc', 5, &columns - 1, 5, &columns - 2, 3, 1],
1809 \ ['ddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
1810 \ ['eee', 5, &columns - 3, 5, &columns - 3, 3, 1],
1811 \
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001812 \ ['aaaa', 5, &columns, 5, &columns - 3, 4, 1],
1813 \ ['bbbb', 5, &columns + 1, 5, &columns - 3, 4, 1],
1814 \ ['cccc', 5, &columns - 1, 5, &columns - 3, 4, 1],
1815 \ ['dddd', 5, &columns - 2, 5, &columns - 3, 4, 1],
1816 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
1817 \ ],
1818 \ },
1819 \ #{
1820 \ comment: 'left aligned with fixed position',
1821 \ options: #{
1822 \ wrap: 0,
1823 \ fixed: 1,
1824 \ pos: 'botleft',
1825 \ },
1826 \ tests: both_wrap_tests + [
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001827 \ ['aa', 5, &columns, 5, &columns, 1, 1],
1828 \ ['bb', 5, &columns + 1, 5, &columns, 1, 1],
1829 \ ['cc', 5, &columns - 1, 5, &columns - 1, 2, 1],
1830 \ ['dd', 5, &columns - 2, 5, &columns - 2, 2, 1],
1831 \ ['ee', 5, &columns - 3, 5, &columns - 3, 2, 1],
1832 \
1833 \ ['aaa', 5, &columns, 5, &columns, 1, 1],
1834 \ ['bbb', 5, &columns + 1, 5, &columns, 1, 1],
1835 \ ['ccc', 5, &columns - 1, 5, &columns - 1, 2, 1],
1836 \ ['ddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
1837 \ ['eee', 5, &columns - 3, 5, &columns - 3, 3, 1],
1838 \
1839 \ ['aaaa', 5, &columns, 5, &columns, 1, 1],
1840 \ ['bbbb', 5, &columns + 1, 5, &columns, 1, 1],
1841 \ ['cccc', 5, &columns - 1, 5, &columns - 1, 2, 1],
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001842 \ ['dddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
1843 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
1844 \ ],
1845 \ },
1846 \ ]
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001847
1848 for test_group in tests
1849 for test in test_group.tests
1850 let [ text, line, col, e_line, e_col, e_width, e_height ] = test
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001851 let options = #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001852 \ line: line,
1853 \ col: col,
1854 \ }
1855 call extend(options, test_group.options)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001856
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001857 let p = popup_create(text, options)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001858
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001859 let msg = string(extend(options, #{text: text}))
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001860 call s:VerifyPosition(p, msg, e_line, e_col, e_width, e_height)
1861 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001862 endfor
1863 endfor
1864
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001865 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001866 %bwipe!
1867endfunc
1868
Bram Moolenaar3397f742019-06-02 18:40:06 +02001869func Test_adjust_left_past_screen_width()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001870 " width of screen
1871 let X = join(map(range(&columns), {->'X'}), '')
1872
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001873 let p = popup_create(X, #{line: 1, col: 1, wrap: 0})
1874 call s:VerifyPosition(p, 'full width topleft', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001875
1876 redraw
1877 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1878 call assert_equal(X, line)
1879
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001880 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001881 redraw
1882
1883 " Same if placed on the right hand side
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001884 let p = popup_create(X, #{line: 1, col: &columns, wrap: 0})
1885 call s:VerifyPosition(p, 'full width topright', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001886
1887 redraw
1888 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1889 call assert_equal(X, line)
1890
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001891 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001892 redraw
1893
1894 " Extend so > window width
1895 let X .= 'x'
1896
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001897 let p = popup_create(X, #{line: 1, col: 1, wrap: 0})
1898 call s:VerifyPosition(p, 'full width + 1 topleft', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001899
1900 redraw
1901 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1902 call assert_equal(X[ : -2 ], line)
1903
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001904 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001905 redraw
1906
1907 " Shifted then truncated (the x is not visible)
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001908 let p = popup_create(X, #{line: 1, col: &columns - 3, wrap: 0})
1909 call s:VerifyPosition(p, 'full width + 1 topright', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001910
1911 redraw
1912 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1913 call assert_equal(X[ : -2 ], line)
1914
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001915 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001916 redraw
1917
1918 " Not shifted, just truncated
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001919 let p = popup_create(X,
1920 \ #{line: 1, col: 2, wrap: 0, fixed: 1})
1921 call s:VerifyPosition(p, 'full width + 1 fixed', 1, 2, &columns - 1, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001922
1923 redraw
1924 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1925 let e_line = ' ' . X[ 1 : -2 ]
1926 call assert_equal(e_line, line)
1927
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001928 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001929 redraw
1930
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001931 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001932 %bwipe!
Bram Moolenaar3397f742019-06-02 18:40:06 +02001933endfunc
1934
1935func Test_popup_moved()
1936 new
1937 call test_override('char_avail', 1)
1938 call setline(1, ['one word to move around', 'a WORD.and->some thing'])
1939
1940 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001941 let winid = popup_atcursor('text', #{moved: 'any'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001942 redraw
1943 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001944 call assert_equal([1, 4, 4], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001945 " trigger the check for last_cursormoved by going into insert mode
1946 call feedkeys("li\<Esc>", 'xt')
1947 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001948 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001949
1950 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001951 let winid = popup_atcursor('text', #{moved: 'word'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001952 redraw
1953 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001954 call assert_equal([1, 4, 7], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001955 call feedkeys("hi\<Esc>", 'xt')
1956 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001957 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001958
1959 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001960 let winid = popup_atcursor('text', #{moved: 'word'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001961 redraw
1962 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001963 call assert_equal([1, 4, 7], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001964 call feedkeys("li\<Esc>", 'xt')
1965 call assert_equal(1, popup_getpos(winid).visible)
1966 call feedkeys("ei\<Esc>", 'xt')
1967 call assert_equal(1, popup_getpos(winid).visible)
1968 call feedkeys("eli\<Esc>", 'xt')
1969 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001970 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001971
Bram Moolenaar17627312019-06-02 19:53:44 +02001972 " WORD is the default
Bram Moolenaar3397f742019-06-02 18:40:06 +02001973 exe "normal gg0/WORD\<CR>"
Bram Moolenaar17627312019-06-02 19:53:44 +02001974 let winid = popup_atcursor('text', {})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001975 redraw
1976 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001977 call assert_equal([2, 2, 15], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001978 call feedkeys("eli\<Esc>", 'xt')
1979 call assert_equal(1, popup_getpos(winid).visible)
1980 call feedkeys("wi\<Esc>", 'xt')
1981 call assert_equal(1, popup_getpos(winid).visible)
1982 call feedkeys("Eli\<Esc>", 'xt')
1983 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001984 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001985
1986 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001987 let winid = popup_atcursor('text', #{moved: [5, 10]})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001988 redraw
1989 call assert_equal(1, popup_getpos(winid).visible)
1990 call feedkeys("eli\<Esc>", 'xt')
1991 call feedkeys("ei\<Esc>", 'xt')
1992 call assert_equal(1, popup_getpos(winid).visible)
1993 call feedkeys("eli\<Esc>", 'xt')
1994 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001995 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001996
1997 bwipe!
1998 call test_override('ALL', 0)
1999endfunc
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002000
2001func Test_notifications()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002002 CheckFeature timers
2003 CheckScreendump
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002004
Bram Moolenaar0fdddee2019-09-01 15:26:23 +02002005 let lines =<< trim END
2006 call setline(1, range(1, 20))
2007 hi Notification ctermbg=lightblue
2008 call popup_notification('first notification', {})
2009 END
2010 call writefile(lines, 'XtestNotifications')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002011 let buf = RunVimInTerminal('-S XtestNotifications', #{rows: 10})
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002012 call VerifyScreenDump(buf, 'Test_popupwin_notify_01', {})
2013
2014 " second one goes below the first one
Bram Moolenaardfa97f22019-06-15 14:31:55 +02002015 call term_sendkeys(buf, ":hi link PopupNotification Notification\<CR>")
2016 call term_sendkeys(buf, ":call popup_notification('another important notification', {})\<CR>")
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002017 call VerifyScreenDump(buf, 'Test_popupwin_notify_02', {})
2018
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002019 " clean up
2020 call StopVimInTerminal(buf)
2021 call delete('XtestNotifications')
2022endfunc
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002023
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002024func Test_popup_scrollbar()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002025 CheckScreendump
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002026
2027 let lines =<< trim END
2028 call setline(1, range(1, 20))
Bram Moolenaar8da41812019-06-26 18:04:54 +02002029 hi ScrollThumb ctermbg=blue
2030 hi ScrollBar ctermbg=red
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002031 let winid = popup_create(['one', 'two', 'three', 'four', 'five',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002032 \ 'six', 'seven', 'eight', 'nine'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002033 \ minwidth: 8,
2034 \ maxheight: 4,
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002035 \ })
Bram Moolenaar53a95d62019-06-26 03:54:08 +02002036 func ScrollUp()
2037 call feedkeys("\<F3>\<ScrollWheelUp>", "xt")
2038 endfunc
2039 func ScrollDown()
2040 call feedkeys("\<F3>\<ScrollWheelDown>", "xt")
2041 endfunc
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02002042 func ClickTop()
2043 call feedkeys("\<F4>\<LeftMouse>", "xt")
2044 endfunc
2045 func ClickBot()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002046 call popup_setoptions(g:winid, #{border: [], close: 'button'})
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02002047 call feedkeys("\<F5>\<LeftMouse>", "xt")
2048 endfunc
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002049 func Popup_filter(winid, key)
2050 if a:key == 'j'
2051 let line = popup_getoptions(a:winid).firstline
2052 let nlines = line('$', a:winid)
2053 let newline = line < nlines ? (line + 1) : nlines
2054 call popup_setoptions(a:winid, #{firstline: newline})
2055 return v:true
2056 elseif a:key == 'x'
2057 call popup_close(a:winid)
2058 return v:true
2059 endif
2060 endfunc
2061
2062 func PopupScroll()
2063 call popup_clear()
2064 let text =<< trim END
2065 1
2066 2
2067 3
2068 4
2069 long line long line long line long line long line long line
2070 long line long line long line long line long line long line
2071 long line long line long line long line long line long line
2072 END
2073 call popup_create(text, #{
2074 \ minwidth: 30,
2075 \ maxwidth: 30,
2076 \ minheight: 4,
2077 \ maxheight: 4,
2078 \ firstline: 1,
Bram Moolenaar30efcf32019-11-03 22:29:38 +01002079 \ lastline: 4,
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002080 \ wrap: v:true,
2081 \ scrollbar: v:true,
2082 \ mapping: v:false,
2083 \ filter: funcref('Popup_filter')
2084 \ })
2085 endfunc
Bram Moolenaar53a95d62019-06-26 03:54:08 +02002086 map <silent> <F3> :call test_setmouse(5, 36)<CR>
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02002087 map <silent> <F4> :call test_setmouse(4, 42)<CR>
2088 map <silent> <F5> :call test_setmouse(7, 42)<CR>
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002089 END
2090 call writefile(lines, 'XtestPopupScroll')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002091 let buf = RunVimInTerminal('-S XtestPopupScroll', #{rows: 10})
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002092 call VerifyScreenDump(buf, 'Test_popupwin_scroll_1', {})
2093
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002094 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 2})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002095 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002096 call VerifyScreenDump(buf, 'Test_popupwin_scroll_2', {})
2097
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002098 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 6})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002099 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002100 call VerifyScreenDump(buf, 'Test_popupwin_scroll_3', {})
2101
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002102 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 9})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002103 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002104 call VerifyScreenDump(buf, 'Test_popupwin_scroll_4', {})
2105
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02002106 call term_sendkeys(buf, ":call popup_setoptions(winid, #{scrollbarhighlight: 'ScrollBar', thumbhighlight: 'ScrollThumb', firstline: 5})\<CR>")
Bram Moolenaara112f2d2019-09-01 17:38:09 +02002107 " this scrolls two lines (half the window height)
Bram Moolenaar53a95d62019-06-26 03:54:08 +02002108 call term_sendkeys(buf, ":call ScrollUp()\<CR>")
2109 call VerifyScreenDump(buf, 'Test_popupwin_scroll_5', {})
2110
2111 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
2112 call VerifyScreenDump(buf, 'Test_popupwin_scroll_6', {})
2113
2114 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
Bram Moolenaar13b47c32019-06-28 21:55:48 +02002115 " wait a bit, otherwise it fails sometimes (double click recognized?)
2116 sleep 100m
Bram Moolenaar53a95d62019-06-26 03:54:08 +02002117 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
2118 call VerifyScreenDump(buf, 'Test_popupwin_scroll_7', {})
2119
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02002120 call term_sendkeys(buf, ":call ClickTop()\<CR>")
2121 sleep 100m
2122 call term_sendkeys(buf, ":call ClickTop()\<CR>")
2123 call VerifyScreenDump(buf, 'Test_popupwin_scroll_8', {})
2124
2125 call term_sendkeys(buf, ":call ClickBot()\<CR>")
2126 call VerifyScreenDump(buf, 'Test_popupwin_scroll_9', {})
2127
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02002128 " remove the minwidth and maxheight
2129 call term_sendkeys(buf, ":call popup_setoptions(winid, #{maxheight: 0, minwidth: 0})\<CR>")
Bram Moolenaar7e0f4622019-09-17 21:23:39 +02002130 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02002131 call VerifyScreenDump(buf, 'Test_popupwin_scroll_10', {})
2132
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002133 " check size with non-wrapping lines
2134 call term_sendkeys(buf, ":call PopupScroll()\<CR>")
2135 call VerifyScreenDump(buf, 'Test_popupwin_scroll_11', {})
2136
2137 " check size with wrapping lines
2138 call term_sendkeys(buf, "j")
2139 call VerifyScreenDump(buf, 'Test_popupwin_scroll_12', {})
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002140
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002141 " clean up
Bram Moolenaar20298ce2020-06-19 21:46:52 +02002142 call term_sendkeys(buf, "x")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002143 call StopVimInTerminal(buf)
2144 call delete('XtestPopupScroll')
2145endfunc
2146
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02002147func Test_popup_too_high_scrollbar()
2148 CheckScreendump
2149
2150 let lines =<< trim END
2151 call setline(1, range(1, 20)->map({i, v -> repeat(v, 10)}))
2152 set scrolloff=0
2153 func ShowPopup()
2154 let winid = popup_atcursor(['one', 'two', 'three', 'four', 'five',
2155 \ 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve'], #{
2156 \ minwidth: 8,
2157 \ border: [],
2158 \ })
2159 endfunc
2160 normal 3G$
2161 call ShowPopup()
2162 END
2163 call writefile(lines, 'XtestPopupToohigh')
2164 let buf = RunVimInTerminal('-S XtestPopupToohigh', #{rows: 10})
2165 call VerifyScreenDump(buf, 'Test_popupwin_toohigh_1', {})
2166
2167 call term_sendkeys(buf, ":call popup_clear()\<CR>")
2168 call term_sendkeys(buf, "8G$")
2169 call term_sendkeys(buf, ":call ShowPopup()\<CR>")
2170 call VerifyScreenDump(buf, 'Test_popupwin_toohigh_2', {})
2171
2172 " clean up
2173 call StopVimInTerminal(buf)
2174 call delete('XtestPopupToohigh')
2175endfunc
2176
Bram Moolenaar437a7462019-07-05 20:17:22 +02002177func Test_popup_fitting_scrollbar()
2178 " this was causing a crash, divide by zero
2179 let winid = popup_create([
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002180 \ 'one', 'two', 'longer line that wraps', 'four', 'five'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002181 \ scrollbar: 1,
2182 \ maxwidth: 10,
2183 \ maxheight: 5,
2184 \ firstline: 2})
Bram Moolenaar437a7462019-07-05 20:17:22 +02002185 redraw
2186 call popup_clear()
2187endfunc
2188
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002189func Test_popup_settext()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002190 CheckScreendump
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002191
2192 let lines =<< trim END
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002193 let opts = #{wrap: 0}
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002194 let p = popup_create('test', opts)
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002195 eval p->popup_settext('this is a text')
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002196 END
2197
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002198 call writefile(lines, 'XtestPopupSetText')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002199 let buf = RunVimInTerminal('-S XtestPopupSetText', #{rows: 10})
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002200 call VerifyScreenDump(buf, 'Test_popup_settext_01', {})
2201
2202 " Setting to empty string clears it
2203 call term_sendkeys(buf, ":call popup_settext(p, '')\<CR>")
2204 call VerifyScreenDump(buf, 'Test_popup_settext_02', {})
2205
2206 " Setting a list
2207 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
2208 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
2209
2210 " Shrinking with a list
2211 call term_sendkeys(buf, ":call popup_settext(p, ['a'])\<CR>")
2212 call VerifyScreenDump(buf, 'Test_popup_settext_04', {})
2213
2214 " Growing with a list
2215 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
2216 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
2217
2218 " Empty list clears
2219 call term_sendkeys(buf, ":call popup_settext(p, [])\<CR>")
2220 call VerifyScreenDump(buf, 'Test_popup_settext_05', {})
2221
2222 " Dicts
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002223 call term_sendkeys(buf, ":call popup_settext(p, [#{text: 'aaaa'}, #{text: 'bbbb'}, #{text: 'cccc'}])\<CR>")
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002224 call VerifyScreenDump(buf, 'Test_popup_settext_06', {})
2225
Bram Moolenaarb0992022020-01-30 14:55:42 +01002226 " range() (doesn't work)
2227 call term_sendkeys(buf, ":call popup_settext(p, range(4, 8))\<CR>")
2228 call VerifyScreenDump(buf, 'Test_popup_settext_07', {})
2229
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002230 " clean up
2231 call StopVimInTerminal(buf)
2232 call delete('XtestPopupSetText')
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002233endfunc
2234
2235func Test_popup_hidden()
2236 new
2237
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002238 let winid = popup_atcursor('text', #{hidden: 1})
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002239 redraw
2240 call assert_equal(0, popup_getpos(winid).visible)
2241 call popup_close(winid)
2242
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002243 let winid = popup_create('text', #{hidden: 1})
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002244 redraw
2245 call assert_equal(0, popup_getpos(winid).visible)
2246 call popup_close(winid)
2247
2248 func QuitCallback(id, res)
2249 let s:cb_winid = a:id
2250 let s:cb_res = a:res
2251 endfunc
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002252 let winid = 'make a choice'->popup_dialog(#{hidden: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002253 \ filter: 'popup_filter_yesno',
2254 \ callback: 'QuitCallback',
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002255 \ })
2256 redraw
2257 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarae943152019-06-16 22:54:14 +02002258 call assert_equal(function('popup_filter_yesno'), popup_getoptions(winid).filter)
2259 call assert_equal(function('QuitCallback'), popup_getoptions(winid).callback)
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002260 exe "normal anot used by filter\<Esc>"
2261 call assert_equal('not used by filter', getline(1))
2262
2263 call popup_show(winid)
2264 call feedkeys('y', "xt")
2265 call assert_equal(1, s:cb_res)
2266
2267 bwipe!
2268 delfunc QuitCallback
2269endfunc
Bram Moolenaarae943152019-06-16 22:54:14 +02002270
2271" Test options not checked elsewhere
2272func Test_set_get_options()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002273 let winid = popup_create('some text', #{highlight: 'Beautiful'})
Bram Moolenaarae943152019-06-16 22:54:14 +02002274 let options = popup_getoptions(winid)
2275 call assert_equal(1, options.wrap)
2276 call assert_equal(0, options.drag)
2277 call assert_equal('Beautiful', options.highlight)
2278
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002279 call popup_setoptions(winid, #{wrap: 0, drag: 1, highlight: 'Another'})
Bram Moolenaarae943152019-06-16 22:54:14 +02002280 let options = popup_getoptions(winid)
2281 call assert_equal(0, options.wrap)
2282 call assert_equal(1, options.drag)
2283 call assert_equal('Another', options.highlight)
2284
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02002285 call assert_fails('call popup_setoptions(winid, [])', 'E715:')
2286 call assert_fails('call popup_setoptions(winid, test_null_dict())', 'E715:')
2287
Bram Moolenaarae943152019-06-16 22:54:14 +02002288 call popup_close(winid)
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02002289 call assert_equal(0, popup_setoptions(winid, options.wrap))
Bram Moolenaarae943152019-06-16 22:54:14 +02002290endfunc
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002291
2292func Test_popupwin_garbage_collect()
2293 func MyPopupFilter(x, winid, c)
2294 " NOP
2295 endfunc
2296
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002297 let winid = popup_create('something', #{filter: function('MyPopupFilter', [{}])})
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002298 call test_garbagecollect_now()
2299 redraw
Bram Moolenaar1bc353b2019-09-01 14:45:28 +02002300 " Must not crash caused by invalid memory access
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002301 call feedkeys('j', 'xt')
2302 call assert_true(v:true)
2303
2304 call popup_close(winid)
2305 delfunc MyPopupFilter
2306endfunc
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002307
Bram Moolenaar581ba392019-09-03 22:08:33 +02002308func Test_popupwin_filter_mode()
2309 func MyPopupFilter(winid, c)
2310 let s:typed = a:c
2311 if a:c == ':' || a:c == "\r" || a:c == 'v'
2312 " can start cmdline mode, get out, and start/stop Visual mode
2313 return 0
2314 endif
2315 return 1
2316 endfunc
2317
2318 " Normal, Visual and Insert mode
2319 let winid = popup_create('something', #{filter: 'MyPopupFilter', filtermode: 'nvi'})
2320 redraw
2321 call feedkeys('x', 'xt')
2322 call assert_equal('x', s:typed)
2323
2324 call feedkeys(":let g:foo = 'foo'\<CR>", 'xt')
2325 call assert_equal(':', s:typed)
2326 call assert_equal('foo', g:foo)
2327
2328 let @x = 'something'
2329 call feedkeys('v$"xy', 'xt')
2330 call assert_equal('y', s:typed)
2331 call assert_equal('something', @x) " yank command is filtered out
2332 call feedkeys('v', 'xt') " end Visual mode
2333
2334 call popup_close(winid)
2335
2336 " only Normal mode
2337 let winid = popup_create('something', #{filter: 'MyPopupFilter', filtermode: 'n'})
2338 redraw
2339 call feedkeys('x', 'xt')
2340 call assert_equal('x', s:typed)
2341
2342 call feedkeys(":let g:foo = 'foo'\<CR>", 'xt')
2343 call assert_equal(':', s:typed)
2344 call assert_equal('foo', g:foo)
2345
2346 let @x = 'something'
2347 call feedkeys('v$"xy', 'xt')
2348 call assert_equal('v', s:typed)
2349 call assert_notequal('something', @x)
2350
2351 call popup_close(winid)
2352
2353 " default: all modes
2354 let winid = popup_create('something', #{filter: 'MyPopupFilter'})
2355 redraw
2356 call feedkeys('x', 'xt')
2357 call assert_equal('x', s:typed)
2358
2359 let g:foo = 'bar'
2360 call feedkeys(":let g:foo = 'foo'\<CR>", 'xt')
2361 call assert_equal("\r", s:typed)
2362 call assert_equal('bar', g:foo)
2363
2364 let @x = 'something'
2365 call feedkeys('v$"xy', 'xt')
2366 call assert_equal('y', s:typed)
2367 call assert_equal('something', @x) " yank command is filtered out
2368 call feedkeys('v', 'xt') " end Visual mode
2369
2370 call popup_close(winid)
2371 delfunc MyPopupFilter
2372endfunc
2373
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002374func Test_popupwin_filter_mouse()
2375 func MyPopupFilter(winid, c)
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002376 let g:got_mousepos = getmousepos()
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002377 return 0
2378 endfunc
2379
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002380 call setline(1, ['.'->repeat(25)]->repeat(10))
2381 let winid = popup_create(['short', 'long line that will wrap', 'other'], #{
2382 \ line: 2,
2383 \ col: 4,
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002384 \ maxwidth: 12,
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002385 \ padding: [],
2386 \ border: [],
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002387 \ filter: 'MyPopupFilter',
2388 \ })
2389 redraw
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002390 " 123456789012345678901
2391 " 1 .....................
2392 " 2 ...+--------------+..
2393 " 3 ...| |..
2394 " 4 ...| short |..
2395 " 5 ...| long line th |..
2396 " 6 ...| at will wrap |..
2397 " 7 ...| other |..
2398 " 8 ...| |..
2399 " 9 ...+--------------+..
2400 " 10 .....................
2401 let tests = []
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002402
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002403 func AddItemOutsidePopup(tests, row, col)
2404 eval a:tests->add(#{clickrow: a:row, clickcol: a:col, result: #{
2405 \ screenrow: a:row, screencol: a:col,
2406 \ winid: win_getid(), winrow: a:row, wincol: a:col,
2407 \ line: a:row, column: a:col,
2408 \ }})
2409 endfunc
2410 func AddItemInPopupBorder(tests, winid, row, col)
2411 eval a:tests->add(#{clickrow: a:row, clickcol: a:col, result: #{
2412 \ screenrow: a:row, screencol: a:col,
2413 \ winid: a:winid, winrow: a:row - 1, wincol: a:col - 3,
2414 \ line: 0, column: 0,
2415 \ }})
2416 endfunc
2417 func AddItemInPopupText(tests, winid, row, col, textline, textcol)
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: a:textline, column: a:textcol,
2422 \ }})
2423 endfunc
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002424
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002425 " above and below popup
2426 for c in range(1, 21)
2427 call AddItemOutsidePopup(tests, 1, c)
2428 call AddItemOutsidePopup(tests, 10, c)
2429 endfor
2430 " left and right of popup
2431 for r in range(1, 10)
2432 call AddItemOutsidePopup(tests, r, 3)
2433 call AddItemOutsidePopup(tests, r, 20)
2434 endfor
2435 " top and bottom in popup
2436 for c in range(4, 19)
2437 call AddItemInPopupBorder(tests, winid, 2, c)
2438 call AddItemInPopupBorder(tests, winid, 3, c)
2439 call AddItemInPopupBorder(tests, winid, 8, c)
2440 call AddItemInPopupBorder(tests, winid, 9, c)
2441 endfor
2442 " left and right margin in popup
2443 for r in range(2, 9)
2444 call AddItemInPopupBorder(tests, winid, r, 4)
2445 call AddItemInPopupBorder(tests, winid, r, 5)
2446 call AddItemInPopupBorder(tests, winid, r, 18)
2447 call AddItemInPopupBorder(tests, winid, r, 19)
2448 endfor
2449 " text "short"
2450 call AddItemInPopupText(tests, winid, 4, 6, 1, 1)
2451 call AddItemInPopupText(tests, winid, 4, 10, 1, 5)
2452 call AddItemInPopupText(tests, winid, 4, 11, 1, 6)
2453 call AddItemInPopupText(tests, winid, 4, 17, 1, 6)
2454 " text "long line th"
2455 call AddItemInPopupText(tests, winid, 5, 6, 2, 1)
2456 call AddItemInPopupText(tests, winid, 5, 10, 2, 5)
2457 call AddItemInPopupText(tests, winid, 5, 17, 2, 12)
2458 " text "at will wrap"
2459 call AddItemInPopupText(tests, winid, 6, 6, 2, 13)
2460 call AddItemInPopupText(tests, winid, 6, 10, 2, 17)
2461 call AddItemInPopupText(tests, winid, 6, 17, 2, 24)
2462 " text "other"
2463 call AddItemInPopupText(tests, winid, 7, 6, 3, 1)
2464 call AddItemInPopupText(tests, winid, 7, 10, 3, 5)
2465 call AddItemInPopupText(tests, winid, 7, 11, 3, 6)
2466 call AddItemInPopupText(tests, winid, 7, 17, 3, 6)
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002467
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002468 for item in tests
2469 call test_setmouse(item.clickrow, item.clickcol)
2470 call feedkeys("\<LeftMouse>", 'xt')
2471 call assert_equal(item.result, g:got_mousepos)
2472 endfor
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002473
2474 call popup_close(winid)
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002475 enew!
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002476 delfunc MyPopupFilter
2477endfunc
2478
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002479func Test_popupwin_with_buffer()
2480 call writefile(['some text', 'in a buffer'], 'XsomeFile')
2481 let buf = bufadd('XsomeFile')
2482 call assert_equal(0, bufloaded(buf))
Bram Moolenaar46451042019-08-24 15:50:46 +02002483
2484 setlocal number
2485 call setbufvar(buf, "&wrapmargin", 13)
2486
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002487 let winid = popup_create(buf, {})
2488 call assert_notequal(0, winid)
2489 let pos = popup_getpos(winid)
2490 call assert_equal(2, pos.height)
2491 call assert_equal(1, bufloaded(buf))
Bram Moolenaar46451042019-08-24 15:50:46 +02002492
2493 " window-local option is set to default, buffer-local is not
2494 call assert_equal(0, getwinvar(winid, '&number'))
2495 call assert_equal(13, getbufvar(buf, '&wrapmargin'))
2496
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002497 call popup_close(winid)
2498 call assert_equal({}, popup_getpos(winid))
2499 call assert_equal(1, bufloaded(buf))
2500 exe 'bwipe! ' .. buf
Bram Moolenaar46451042019-08-24 15:50:46 +02002501 setlocal nonumber
Bram Moolenaar7866b872019-07-01 22:21:01 +02002502
2503 edit test_popupwin.vim
2504 let winid = popup_create(bufnr(''), {})
2505 redraw
2506 call popup_close(winid)
Bram Moolenaar3940ec62019-07-05 21:53:24 +02002507 call delete('XsomeFile')
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002508endfunc
Bram Moolenaare296e312019-07-03 23:20:18 +02002509
Bram Moolenaare0d749a2019-09-25 22:14:48 +02002510func Test_popupwin_terminal_buffer()
Bram Moolenaard2c1fb42019-09-25 23:06:40 +02002511 CheckFeature terminal
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002512 CheckUnix
Bram Moolenaare06a28f2020-05-13 23:24:12 +02002513 " Starting a terminal to run a shell in is considered flaky.
2514 let g:test_is_flaky = 1
Bram Moolenaard2c1fb42019-09-25 23:06:40 +02002515
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002516 let origwin = win_getid()
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002517 let termbuf = term_start(&shell, #{hidden: 1})
2518 let winid = popup_create(termbuf, #{minwidth: 40, minheight: 10})
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002519 " Wait for shell to start
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002520 call WaitForAssert({-> assert_equal("run", job_status(term_getjob(termbuf)))})
Bram Moolenaare06a28f2020-05-13 23:24:12 +02002521 sleep 100m
Bram Moolenaar631ebc42020-02-03 22:15:26 +01002522 " Check this doesn't crash
2523 call assert_equal(winnr(), winnr('j'))
2524 call assert_equal(winnr(), winnr('k'))
2525 call assert_equal(winnr(), winnr('h'))
2526 call assert_equal(winnr(), winnr('l'))
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02002527
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002528 " Cannot quit while job is running
2529 call assert_fails('call feedkeys("\<C-W>:quit\<CR>", "xt")', 'E948:')
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02002530
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002531 " Cannot enter Terminal-Normal mode. (TODO: but it works...)
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02002532 call feedkeys("xxx\<C-W>N", 'xt')
2533 call assert_fails('call feedkeys("gf", "xt")', 'E863:')
2534 call feedkeys("a\<C-U>", 'xt')
2535
Bram Moolenaar3f65c662020-05-27 23:15:16 +02002536 " Cannot escape from terminal window
2537 call assert_fails('tab drop xxx', 'E863:')
2538
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002539 " Cannot open a second one.
2540 let termbuf2 = term_start(&shell, #{hidden: 1})
2541 call assert_fails('call popup_create(termbuf2, #{})', 'E861:')
2542 call term_sendkeys(termbuf2, "exit\<CR>")
2543
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02002544 " Exiting shell closes popup window
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002545 call feedkeys("exit\<CR>", 'xt')
2546 " Wait for shell to exit
Bram Moolenaarb2b218d2020-06-22 20:22:19 +02002547 call WaitForAssert({-> assert_equal("dead", job_status(term_getjob(termbuf)))})
Bram Moolenaar373c6512020-06-22 19:24:23 +02002548
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002549 call feedkeys(":quit\<CR>", 'xt')
2550 call assert_equal(origwin, win_getid())
Bram Moolenaare0d749a2019-09-25 22:14:48 +02002551endfunc
2552
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002553func Test_popupwin_close_prevwin()
2554 CheckFeature terminal
2555
2556 call assert_equal(1, winnr('$'))
2557 split
2558 wincmd b
2559 call assert_equal(2, winnr())
2560 let buf = term_start(&shell, #{hidden: 1})
2561 call popup_create(buf, {})
2562 call term_wait(buf, 100)
2563 call popup_clear(1)
2564 call assert_equal(2, winnr())
2565
2566 quit
2567 exe 'bwipe! ' .. buf
2568endfunc
2569
Bram Moolenaar934470e2019-09-01 23:27:05 +02002570func Test_popupwin_with_buffer_and_filter()
2571 new Xwithfilter
2572 call setline(1, range(100))
2573 let bufnr = bufnr()
2574 hide
2575
2576 func BufferFilter(win, key)
2577 if a:key == 'G'
2578 " recursive use of "G" does not cause problems.
2579 call win_execute(a:win, 'normal! G')
2580 return 1
2581 endif
2582 return 0
2583 endfunc
2584
2585 let winid = popup_create(bufnr, #{maxheight: 5, filter: 'BufferFilter'})
2586 call assert_equal(1, popup_getpos(winid).firstline)
2587 redraw
2588 call feedkeys("G", 'xt')
2589 call assert_equal(99, popup_getpos(winid).firstline)
2590
2591 call popup_close(winid)
2592 exe 'bwipe! ' .. bufnr
2593endfunc
2594
Bram Moolenaare296e312019-07-03 23:20:18 +02002595func Test_popupwin_width()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002596 let winid = popup_create(repeat(['short', 'long long long line', 'medium width'], 50), #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002597 \ maxwidth: 40,
2598 \ maxheight: 10,
Bram Moolenaare296e312019-07-03 23:20:18 +02002599 \ })
2600 for top in range(1, 20)
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002601 eval winid->popup_setoptions(#{firstline: top})
Bram Moolenaare296e312019-07-03 23:20:18 +02002602 redraw
2603 call assert_equal(19, popup_getpos(winid).width)
2604 endfor
2605 call popup_clear()
2606endfunc
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02002607
2608func Test_popupwin_buf_close()
2609 let buf = bufadd('Xtestbuf')
2610 call bufload(buf)
2611 call setbufline(buf, 1, ['just', 'some', 'lines'])
2612 let winid = popup_create(buf, {})
2613 redraw
2614 call assert_equal(3, popup_getpos(winid).height)
2615 let bufinfo = getbufinfo(buf)[0]
2616 call assert_equal(1, bufinfo.changed)
2617 call assert_equal(0, bufinfo.hidden)
2618 call assert_equal(0, bufinfo.listed)
2619 call assert_equal(1, bufinfo.loaded)
2620 call assert_equal([], bufinfo.windows)
2621 call assert_equal([winid], bufinfo.popups)
2622
2623 call popup_close(winid)
2624 call assert_equal({}, popup_getpos(winid))
2625 let bufinfo = getbufinfo(buf)[0]
2626 call assert_equal(1, bufinfo.changed)
2627 call assert_equal(1, 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([], bufinfo.popups)
2632 exe 'bwipe! ' .. buf
2633endfunc
Bram Moolenaar017c2692019-07-13 14:17:51 +02002634
2635func Test_popup_menu_with_maxwidth()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002636 CheckScreendump
Bram Moolenaar017c2692019-07-13 14:17:51 +02002637
2638 let lines =<< trim END
2639 call setline(1, range(1, 10))
2640 hi ScrollThumb ctermbg=blue
2641 hi ScrollBar ctermbg=red
2642 func PopupMenu(lines, line, col, scrollbar = 0)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002643 return popup_menu(a:lines, #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002644 \ maxwidth: 10,
2645 \ maxheight: 3,
2646 \ pos : 'topleft',
2647 \ col : a:col,
2648 \ line : a:line,
2649 \ scrollbar : a:scrollbar,
Bram Moolenaar017c2692019-07-13 14:17:51 +02002650 \ })
2651 endfunc
2652 call PopupMenu(['x'], 1, 1)
2653 call PopupMenu(['123456789|'], 1, 16)
2654 call PopupMenu(['123456789|' .. ' '], 7, 1)
2655 call PopupMenu([repeat('123456789|', 100)], 7, 16)
2656 call PopupMenu(repeat(['123456789|' .. ' '], 5), 1, 33, 1)
2657 END
2658 call writefile(lines, 'XtestPopupMenuMaxWidth')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002659 let buf = RunVimInTerminal('-S XtestPopupMenuMaxWidth', #{rows: 13})
Bram Moolenaar017c2692019-07-13 14:17:51 +02002660 call VerifyScreenDump(buf, 'Test_popupwin_menu_maxwidth_1', {})
2661
2662 " close the menu popupwin.
2663 call term_sendkeys(buf, " ")
2664 call term_sendkeys(buf, " ")
2665 call term_sendkeys(buf, " ")
2666 call term_sendkeys(buf, " ")
2667 call term_sendkeys(buf, " ")
2668
2669 " clean up
2670 call StopVimInTerminal(buf)
2671 call delete('XtestPopupMenuMaxWidth')
2672endfunc
2673
Bram Moolenaara901a372019-07-13 16:38:50 +02002674func Test_popup_menu_with_scrollbar()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002675 CheckScreendump
Bram Moolenaara901a372019-07-13 16:38:50 +02002676
2677 let lines =<< trim END
2678 call setline(1, range(1, 20))
2679 hi ScrollThumb ctermbg=blue
2680 hi ScrollBar ctermbg=red
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002681 eval ['one', 'two', 'three', 'four', 'five',
2682 \ 'six', 'seven', 'eight', 'nine']
2683 \ ->popup_menu(#{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002684 \ minwidth: 8,
2685 \ maxheight: 3,
Bram Moolenaara901a372019-07-13 16:38:50 +02002686 \ })
2687 END
2688 call writefile(lines, 'XtestPopupMenuScroll')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002689 let buf = RunVimInTerminal('-S XtestPopupMenuScroll', #{rows: 10})
Bram Moolenaara901a372019-07-13 16:38:50 +02002690
2691 call term_sendkeys(buf, "j")
2692 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_1', {})
2693
2694 call term_sendkeys(buf, "jjj")
2695 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_2', {})
2696
2697 " if the cursor is the bottom line, it stays at the bottom line.
2698 call term_sendkeys(buf, repeat("j", 20))
2699 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_3', {})
2700
2701 call term_sendkeys(buf, "kk")
2702 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_4', {})
2703
2704 call term_sendkeys(buf, "k")
2705 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_5', {})
2706
2707 " if the cursor is in the top line, it stays in the top line.
2708 call term_sendkeys(buf, repeat("k", 20))
2709 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_6', {})
2710
2711 " close the menu popupwin.
2712 call term_sendkeys(buf, " ")
2713
2714 " clean up
2715 call StopVimInTerminal(buf)
2716 call delete('XtestPopupMenuScroll')
2717endfunc
2718
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002719func Test_popup_menu_filter()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002720 CheckScreendump
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002721
2722 let lines =<< trim END
2723 function! MyFilter(winid, key) abort
2724 if a:key == "0"
2725 call win_execute(a:winid, "call setpos('.', [0, 1, 1, 0])")
2726 return 1
2727 endif
2728 if a:key == "G"
2729 call win_execute(a:winid, "call setpos('.', [0, line('$'), 1, 0])")
2730 return 1
2731 endif
2732 if a:key == "j"
2733 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0])")
2734 return 1
2735 endif
2736 if a:key == "k"
2737 call win_execute(a:winid, "call setpos('.', [0, line('.') - 1, 1, 0])")
2738 return 1
2739 endif
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02002740 if a:key == ':'
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002741 call popup_close(a:winid)
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02002742 return 0
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002743 endif
2744 return 0
2745 endfunction
2746 call popup_menu(['111', '222', '333', '444', '555', '666', '777', '888', '999'], #{
2747 \ maxheight : 3,
2748 \ filter : 'MyFilter'
2749 \ })
2750 END
2751 call writefile(lines, 'XtestPopupMenuFilter')
2752 let buf = RunVimInTerminal('-S XtestPopupMenuFilter', #{rows: 10})
2753
2754 call term_sendkeys(buf, "j")
2755 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_1', {})
2756
2757 call term_sendkeys(buf, "k")
2758 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_2', {})
2759
2760 call term_sendkeys(buf, "G")
2761 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_3', {})
2762
2763 call term_sendkeys(buf, "0")
2764 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_4', {})
2765
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02002766 " check that when the popup is closed in the filter the screen is redrawn
2767 call term_sendkeys(buf, ":")
2768 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_5', {})
2769 call term_sendkeys(buf, "\<CR>")
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002770
2771 " clean up
2772 call StopVimInTerminal(buf)
2773 call delete('XtestPopupMenuFilter')
2774endfunc
2775
2776func Test_popup_cursorline()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002777 CheckScreendump
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002778
2779 let winid = popup_create('some text', {})
2780 call assert_equal(0, popup_getoptions(winid).cursorline)
2781 call popup_close(winid)
2782
2783 let winid = popup_create('some text', #{ cursorline: 1, })
2784 call assert_equal(1, popup_getoptions(winid).cursorline)
2785 call popup_close(winid)
2786
2787 let winid = popup_create('some text', #{ cursorline: 0, })
2788 call assert_equal(0, popup_getoptions(winid).cursorline)
2789 call popup_close(winid)
2790
2791 let winid = popup_menu('some text', {})
2792 call assert_equal(1, popup_getoptions(winid).cursorline)
2793 call popup_close(winid)
2794
2795 let winid = popup_menu('some text', #{ cursorline: 1, })
2796 call assert_equal(1, popup_getoptions(winid).cursorline)
2797 call popup_close(winid)
2798
2799 let winid = popup_menu('some text', #{ cursorline: 0, })
2800 call assert_equal(0, popup_getoptions(winid).cursorline)
2801 call popup_close(winid)
2802
2803 " ---------
2804 " Pattern 1
2805 " ---------
2806 let lines =<< trim END
2807 call popup_create(['111', '222', '333'], #{ cursorline : 0 })
2808 END
2809 call writefile(lines, 'XtestPopupCursorLine')
2810 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2811 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_1', {})
2812 call term_sendkeys(buf, ":call popup_clear()\<cr>")
2813 call StopVimInTerminal(buf)
2814
2815 " ---------
2816 " Pattern 2
2817 " ---------
2818 let lines =<< trim END
2819 call popup_create(['111', '222', '333'], #{ cursorline : 1 })
2820 END
2821 call writefile(lines, 'XtestPopupCursorLine')
2822 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2823 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_2', {})
2824 call term_sendkeys(buf, ":call popup_clear()\<cr>")
2825 call StopVimInTerminal(buf)
2826
2827 " ---------
2828 " Pattern 3
2829 " ---------
2830 let lines =<< trim END
2831 function! MyFilter(winid, key) abort
2832 if a:key == "j"
2833 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0]) | redraw")
2834 return 1
2835 endif
2836 if a:key == 'x'
2837 call popup_close(a:winid)
2838 return 1
2839 endif
2840 return 0
2841 endfunction
2842 call popup_menu(['111', '222', '333'], #{
2843 \ cursorline : 0,
2844 \ maxheight : 2,
2845 \ filter : 'MyFilter',
2846 \ })
2847 END
2848 call writefile(lines, 'XtestPopupCursorLine')
2849 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2850 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_3', {})
2851 call term_sendkeys(buf, "j")
2852 call term_sendkeys(buf, "j")
2853 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_4', {})
2854 call term_sendkeys(buf, "x")
2855 call StopVimInTerminal(buf)
2856
2857 " ---------
2858 " Pattern 4
2859 " ---------
2860 let lines =<< trim END
2861 function! MyFilter(winid, key) abort
2862 if a:key == "j"
2863 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0]) | redraw")
2864 return 1
2865 endif
2866 if a:key == 'x'
2867 call popup_close(a:winid)
2868 return 1
2869 endif
2870 return 0
2871 endfunction
2872 call popup_menu(['111', '222', '333'], #{
2873 \ cursorline : 1,
2874 \ maxheight : 2,
2875 \ filter : 'MyFilter',
2876 \ })
2877 END
2878 call writefile(lines, 'XtestPopupCursorLine')
2879 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2880 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_5', {})
2881 call term_sendkeys(buf, "j")
2882 call term_sendkeys(buf, "j")
2883 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_6', {})
2884 call term_sendkeys(buf, "x")
2885 call StopVimInTerminal(buf)
2886
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02002887 " ---------
2888 " Cursor in second line when creating the popup
2889 " ---------
2890 let lines =<< trim END
2891 let winid = popup_create(['111', '222', '333'], #{
2892 \ cursorline : 1,
2893 \ })
2894 call win_execute(winid, "2")
2895 END
2896 call writefile(lines, 'XtestPopupCursorLine')
2897 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2898 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_7', {})
2899 call StopVimInTerminal(buf)
2900
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002901 call delete('XtestPopupCursorLine')
Bram Moolenaar4eb7dae2019-11-12 22:33:45 +01002902
2903 " ---------
2904 " Use current buffer for popupmenu
2905 " ---------
2906 let lines =<< trim END
2907 call setline(1, ['one', 'two', 'three'])
2908 let winid = popup_create(bufnr('%'), #{
2909 \ cursorline : 1,
2910 \ })
2911 call win_execute(winid, "2")
2912 END
2913 call writefile(lines, 'XtestPopupCursorLine')
2914 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2915 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_8', {})
2916 call StopVimInTerminal(buf)
2917
2918 call delete('XtestPopupCursorLine')
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002919endfunc
2920
Bram Moolenaarf914a332019-07-20 15:09:56 +02002921func Test_previewpopup()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002922 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002923 CheckFeature quickfix
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002924
Bram Moolenaarf914a332019-07-20 15:09:56 +02002925 call writefile([
2926 \ "!_TAG_FILE_ENCODING\tutf-8\t//",
2927 \ "another\tXtagfile\t/^this is another",
2928 \ "theword\tXtagfile\t/^theword"],
2929 \ 'Xtags')
2930 call writefile(range(1,20)
2931 \ + ['theword is here']
2932 \ + range(22, 27)
2933 \ + ['this is another place']
2934 \ + range(29, 40),
2935 \ "Xtagfile")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002936 call writefile(range(1,10)
2937 \ + ['searched word is here']
2938 \ + range(12, 20),
2939 \ "Xheader.h")
Bram Moolenaarf914a332019-07-20 15:09:56 +02002940 let lines =<< trim END
2941 set tags=Xtags
2942 call setline(1, [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002943 \ 'one',
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002944 \ '#include "Xheader.h"',
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002945 \ 'three',
2946 \ 'four',
2947 \ 'five',
2948 \ 'six',
2949 \ 'seven',
2950 \ 'find theword somewhere',
2951 \ 'nine',
2952 \ 'this is another word',
2953 \ 'very long line where the word is also another'])
Bram Moolenaarf914a332019-07-20 15:09:56 +02002954 set previewpopup=height:4,width:40
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002955 set path=.
Bram Moolenaarf914a332019-07-20 15:09:56 +02002956 END
2957 call writefile(lines, 'XtestPreviewPopup')
2958 let buf = RunVimInTerminal('-S XtestPreviewPopup', #{rows: 14})
2959
2960 call term_sendkeys(buf, "/theword\<CR>\<C-W>}")
2961 call term_sendkeys(buf, ":\<CR>")
2962 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_1', {})
2963
2964 call term_sendkeys(buf, "/another\<CR>\<C-W>}")
2965 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_2', {})
2966
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002967 call term_sendkeys(buf, ":call popup_move(popup_findpreview(), #{col: 15})\<CR>")
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002968 call term_sendkeys(buf, ":\<CR>")
2969 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_3', {})
2970
2971 call term_sendkeys(buf, "/another\<CR>\<C-W>}")
2972 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_4', {})
2973
Bram Moolenaar799439a2020-02-11 21:44:17 +01002974 call term_sendkeys(buf, ":silent cd ..\<CR>:\<CR>")
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002975 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_5', {})
Bram Moolenaar799439a2020-02-11 21:44:17 +01002976 call term_sendkeys(buf, ":silent cd testdir\<CR>")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002977
2978 call term_sendkeys(buf, ":pclose\<CR>")
Bram Moolenaar78d629a2019-08-16 17:31:15 +02002979 call term_sendkeys(buf, ":\<BS>")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002980 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_6', {})
2981
2982 call term_sendkeys(buf, ":pedit +/theword Xtagfile\<CR>")
2983 call term_sendkeys(buf, ":\<CR>")
2984 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_7', {})
2985
2986 call term_sendkeys(buf, ":pclose\<CR>")
2987 call term_sendkeys(buf, ":psearch searched\<CR>")
2988 call term_sendkeys(buf, ":\<CR>")
2989 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_8', {})
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002990
Bram Moolenaar8bf716c2020-01-23 15:33:54 +01002991 call term_sendkeys(buf, "\<C-W>p")
2992 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_9', {})
2993
2994 call term_sendkeys(buf, ":call win_execute(popup_findpreview(), 'call popup_clear()')\<CR>")
2995 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_10', {})
2996
Bram Moolenaarf914a332019-07-20 15:09:56 +02002997 call StopVimInTerminal(buf)
2998 call delete('Xtags')
2999 call delete('Xtagfile')
3000 call delete('XtestPreviewPopup')
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02003001 call delete('Xheader.h')
Bram Moolenaarf914a332019-07-20 15:09:56 +02003002endfunc
3003
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003004func Get_popupmenu_lines()
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003005 let lines =<< trim END
3006 set completeopt+=preview,popup
3007 set completefunc=CompleteFuncDict
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02003008 hi InfoPopup ctermbg=yellow
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003009
3010 func CompleteFuncDict(findstart, base)
3011 if a:findstart
3012 if col('.') > 10
3013 return col('.') - 10
3014 endif
3015 return 0
3016 endif
3017
3018 return {
3019 \ 'words': [
3020 \ {
3021 \ 'word': 'aword',
3022 \ 'abbr': 'wrd',
3023 \ 'menu': 'extra text',
3024 \ 'info': 'words are cool',
3025 \ 'kind': 'W',
3026 \ 'user_data': 'test'
3027 \ },
3028 \ {
3029 \ 'word': 'anotherword',
3030 \ 'abbr': 'anotwrd',
3031 \ 'menu': 'extra text',
3032 \ 'info': "other words are\ncooler than this and some more text\nto make wrap",
3033 \ 'kind': 'W',
3034 \ 'user_data': 'notest'
3035 \ },
3036 \ {
3037 \ 'word': 'noinfo',
3038 \ 'abbr': 'noawrd',
3039 \ 'menu': 'extra text',
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02003040 \ 'info': "lets\nshow\na\nscrollbar\nhere",
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003041 \ 'kind': 'W',
3042 \ 'user_data': 'notest'
3043 \ },
3044 \ {
3045 \ 'word': 'thatword',
3046 \ 'abbr': 'thatwrd',
3047 \ 'menu': 'extra text',
3048 \ 'info': 'that word is cool',
3049 \ 'kind': 'W',
3050 \ 'user_data': 'notest'
3051 \ },
3052 \ ]
3053 \ }
3054 endfunc
3055 call setline(1, 'text text text text text text text ')
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003056 func ChangeColor()
3057 let id = popup_findinfo()
Bram Moolenaar6a124e62019-09-04 18:15:19 +02003058 eval id->popup_setoptions(#{highlight: 'InfoPopup'})
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003059 endfunc
Bram Moolenaardca7abe2019-10-20 18:17:57 +02003060
3061 func InfoHidden()
3062 set completepopup=height:4,border:off,align:menu
3063 set completeopt-=popup completeopt+=popuphidden
3064 au CompleteChanged * call HandleChange()
3065 endfunc
3066
3067 let s:counter = 0
3068 func HandleChange()
3069 let s:counter += 1
3070 let selected = complete_info(['selected']).selected
3071 if selected <= 0
3072 " First time: do nothing, info remains hidden
3073 return
3074 endif
3075 if selected == 1
3076 " Second time: show info right away
3077 let id = popup_findinfo()
3078 if id
3079 call popup_settext(id, 'immediate info ' .. s:counter)
3080 call popup_show(id)
3081 endif
3082 else
3083 " Third time: show info after a short delay
3084 call timer_start(100, 'ShowInfo')
3085 endif
3086 endfunc
3087
3088 func ShowInfo(...)
3089 let id = popup_findinfo()
3090 if id
3091 call popup_settext(id, 'async info ' .. s:counter)
3092 call popup_show(id)
3093 endif
3094 endfunc
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003095 END
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003096 return lines
3097endfunc
3098
3099func Test_popupmenu_info_border()
3100 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003101 CheckFeature quickfix
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003102
3103 let lines = Get_popupmenu_lines()
3104 call add(lines, 'set completepopup=height:4,highlight:InfoPopup')
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003105 call writefile(lines, 'XtestInfoPopup')
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003106
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003107 let buf = RunVimInTerminal('-S XtestInfoPopup', #{rows: 14})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003108 call TermWait(buf, 25)
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003109
3110 call term_sendkeys(buf, "A\<C-X>\<C-U>")
3111 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_1', {})
3112
3113 call term_sendkeys(buf, "\<C-N>")
3114 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_2', {})
3115
3116 call term_sendkeys(buf, "\<C-N>")
3117 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_3', {})
3118
3119 call term_sendkeys(buf, "\<C-N>\<C-N>")
3120 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_4', {})
3121
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02003122 " info on the left with scrollbar
3123 call term_sendkeys(buf, "test text test text\<C-X>\<C-U>")
3124 call term_sendkeys(buf, "\<C-N>\<C-N>")
3125 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_5', {})
3126
Bram Moolenaar202c3f72019-11-21 12:12:35 +01003127 " Test that the popupmenu's scrollbar and infopopup do not overlap
3128 call term_sendkeys(buf, "\<Esc>")
3129 call term_sendkeys(buf, ":set pumheight=3\<CR>")
3130 call term_sendkeys(buf, "cc\<C-X>\<C-U>")
3131 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_6', {})
3132
Bram Moolenaarca7c0782020-01-14 20:42:48 +01003133 " Hide the info popup, cycle trough buffers, make sure it didn't get
3134 " deleted.
3135 call term_sendkeys(buf, "\<Esc>")
3136 call term_sendkeys(buf, ":set hidden\<CR>")
3137 call term_sendkeys(buf, ":bn\<CR>")
3138 call term_sendkeys(buf, ":bn\<CR>")
3139 call term_sendkeys(buf, "otest text test text\<C-X>\<C-U>")
3140 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_7', {})
3141
Bram Moolenaar447bfba2020-07-18 16:07:16 +02003142 " Test that when the option is changed the popup changes.
3143 call term_sendkeys(buf, "\<Esc>")
3144 call term_sendkeys(buf, ":set completepopup=border:off\<CR>")
3145 call term_sendkeys(buf, "a\<C-X>\<C-U>")
3146 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_8', {})
3147
Bram Moolenaar6d585f42020-07-26 22:20:54 +02003148 call term_sendkeys(buf, " \<Esc>")
3149 call term_sendkeys(buf, ":set completepopup+=width:10\<CR>")
3150 call term_sendkeys(buf, "a\<C-X>\<C-U>")
3151 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_9', {})
3152
Bram Moolenaarca7c0782020-01-14 20:42:48 +01003153 call term_sendkeys(buf, "\<Esc>")
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003154 call StopVimInTerminal(buf)
3155 call delete('XtestInfoPopup')
3156endfunc
3157
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003158func Test_popupmenu_info_noborder()
3159 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003160 CheckFeature quickfix
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003161
3162 let lines = Get_popupmenu_lines()
3163 call add(lines, 'set completepopup=height:4,border:off')
3164 call writefile(lines, 'XtestInfoPopupNb')
3165
3166 let buf = RunVimInTerminal('-S XtestInfoPopupNb', #{rows: 14})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003167 call TermWait(buf, 25)
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003168
3169 call term_sendkeys(buf, "A\<C-X>\<C-U>")
3170 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_nb_1', {})
3171
3172 call StopVimInTerminal(buf)
3173 call delete('XtestInfoPopupNb')
3174endfunc
3175
Bram Moolenaar258cef52019-08-21 17:29:29 +02003176func Test_popupmenu_info_align_menu()
3177 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003178 CheckFeature quickfix
Bram Moolenaar258cef52019-08-21 17:29:29 +02003179
3180 let lines = Get_popupmenu_lines()
3181 call add(lines, 'set completepopup=height:4,border:off,align:menu')
3182 call writefile(lines, 'XtestInfoPopupNb')
3183
3184 let buf = RunVimInTerminal('-S XtestInfoPopupNb', #{rows: 14})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003185 call TermWait(buf, 25)
Bram Moolenaar258cef52019-08-21 17:29:29 +02003186
3187 call term_sendkeys(buf, "A\<C-X>\<C-U>")
3188 call term_sendkeys(buf, "\<C-N>")
3189 call term_sendkeys(buf, "\<C-N>")
3190 call term_sendkeys(buf, "\<C-N>")
3191 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_1', {})
3192
3193 call term_sendkeys(buf, "test text test text test\<C-X>\<C-U>")
3194 call term_sendkeys(buf, "\<C-N>")
3195 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_2', {})
3196
3197 call term_sendkeys(buf, "\<Esc>")
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003198 call term_sendkeys(buf, ":call ChangeColor()\<CR>")
Bram Moolenaar258cef52019-08-21 17:29:29 +02003199 call term_sendkeys(buf, ":call setline(2, ['x']->repeat(10))\<CR>")
3200 call term_sendkeys(buf, "Gotest text test text\<C-X>\<C-U>")
3201 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_3', {})
3202
3203 call StopVimInTerminal(buf)
3204 call delete('XtestInfoPopupNb')
3205endfunc
3206
Bram Moolenaardca7abe2019-10-20 18:17:57 +02003207func Test_popupmenu_info_hidden()
3208 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003209 CheckFeature quickfix
Bram Moolenaardca7abe2019-10-20 18:17:57 +02003210
3211 let lines = Get_popupmenu_lines()
3212 call add(lines, 'call InfoHidden()')
3213 call writefile(lines, 'XtestInfoPopupHidden')
3214
3215 let buf = RunVimInTerminal('-S XtestInfoPopupHidden', #{rows: 14})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003216 call TermWait(buf, 25)
Bram Moolenaardca7abe2019-10-20 18:17:57 +02003217
3218 call term_sendkeys(buf, "A\<C-X>\<C-U>")
3219 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_hidden_1', {})
3220
3221 call term_sendkeys(buf, "\<C-N>")
3222 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_hidden_2', {})
3223
3224 call term_sendkeys(buf, "\<C-N>")
3225 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_hidden_3', {})
3226
3227 call term_sendkeys(buf, "\<Esc>")
3228 call StopVimInTerminal(buf)
3229 call delete('XtestInfoPopupHidden')
3230endfunc
3231
Bram Moolenaarbef93ac2019-12-06 20:17:35 +01003232func Test_popupmenu_info_too_wide()
3233 CheckScreendump
3234 CheckFeature quickfix
3235
3236 let lines =<< trim END
3237 call setline(1, range(10))
3238
3239 set completeopt+=preview,popup
3240 set completepopup=align:menu
3241 set omnifunc=OmniFunc
3242 hi InfoPopup ctermbg=lightgrey
3243
3244 func OmniFunc(findstart, base)
3245 if a:findstart
3246 return 0
3247 endif
3248
3249 let menuText = 'some long text to make sure the menu takes up all of the width of the window'
3250 return #{
3251 \ words: [
3252 \ #{
3253 \ word: 'scrap',
3254 \ menu: menuText,
3255 \ info: "other words are\ncooler than this and some more text\nto make wrap",
3256 \ },
3257 \ #{
3258 \ word: 'scappier',
3259 \ menu: menuText,
3260 \ info: 'words are cool',
3261 \ },
3262 \ #{
3263 \ word: 'scrappier2',
3264 \ menu: menuText,
3265 \ info: 'words are cool',
3266 \ },
3267 \ ]
3268 \ }
3269 endfunc
3270 END
3271
3272 call writefile(lines, 'XtestInfoPopupWide')
3273 let buf = RunVimInTerminal('-S XtestInfoPopupWide', #{rows: 8})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003274 call TermWait(buf, 25)
Bram Moolenaarbef93ac2019-12-06 20:17:35 +01003275
3276 call term_sendkeys(buf, "Ascr\<C-X>\<C-O>")
3277 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_wide_1', {})
3278
3279 call term_sendkeys(buf, "\<Esc>")
3280 call StopVimInTerminal(buf)
3281 call delete('XtestInfoPopupWide')
3282endfunc
3283
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02003284func Test_popupwin_recycle_bnr()
Bram Moolenaare49fbff2019-08-21 22:50:07 +02003285 let bufnr = popup_notification('nothing wrong', {})->winbufnr()
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02003286 call popup_clear()
Bram Moolenaar6a124e62019-09-04 18:15:19 +02003287 let winid = 'nothing wrong'->popup_notification({})
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02003288 call assert_equal(bufnr, winbufnr(winid))
3289 call popup_clear()
3290endfunc
3291
Bram Moolenaar1824f452019-10-02 23:06:46 +02003292func Test_popupwin_getoptions_tablocal()
3293 topleft split
3294 let win1 = popup_create('nothing', #{maxheight: 8})
3295 let win2 = popup_create('something', #{maxheight: 10})
3296 let win3 = popup_create('something', #{maxheight: 15})
3297 call assert_equal(8, popup_getoptions(win1).maxheight)
3298 call assert_equal(10, popup_getoptions(win2).maxheight)
3299 call assert_equal(15, popup_getoptions(win3).maxheight)
3300 call popup_clear()
3301 quit
3302endfunc
3303
Bram Moolenaare8a7dfe2019-10-03 22:35:52 +02003304func Test_popupwin_cancel()
3305 let win1 = popup_create('one', #{line: 5, filter: {... -> 0}})
3306 let win2 = popup_create('two', #{line: 10, filter: {... -> 0}})
3307 let win3 = popup_create('three', #{line: 15, filter: {... -> 0}})
3308 call assert_equal(5, popup_getpos(win1).line)
3309 call assert_equal(10, popup_getpos(win2).line)
3310 call assert_equal(15, popup_getpos(win3).line)
3311 " TODO: this also works without patch 8.1.2110
3312 call feedkeys("\<C-C>", 'xt')
3313 call assert_equal(5, popup_getpos(win1).line)
3314 call assert_equal(10, popup_getpos(win2).line)
3315 call assert_equal({}, popup_getpos(win3))
3316 call feedkeys("\<C-C>", 'xt')
3317 call assert_equal(5, popup_getpos(win1).line)
3318 call assert_equal({}, popup_getpos(win2))
3319 call assert_equal({}, popup_getpos(win3))
3320 call feedkeys("\<C-C>", 'xt')
3321 call assert_equal({}, popup_getpos(win1))
3322 call assert_equal({}, popup_getpos(win2))
3323 call assert_equal({}, popup_getpos(win3))
3324endfunc
3325
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003326func Test_popupwin_filter_redraw()
3327 " Create two popups with a filter that closes the popup when typing "0".
3328 " Both popups should close, even though the redraw also calls
3329 " popup_reset_handled()
3330
3331 func CloseFilter(winid, key)
3332 if a:key == '0'
3333 call popup_close(a:winid)
3334 redraw
3335 endif
3336 return 0 " pass the key
3337 endfunc
3338
3339 let id1 = popup_create('first one', #{
3340 \ line: 1,
3341 \ col: 1,
3342 \ filter: 'CloseFilter',
3343 \ })
3344 let id2 = popup_create('second one', #{
3345 \ line: 9,
3346 \ col: 1,
3347 \ filter: 'CloseFilter',
3348 \ })
3349 call assert_equal(1, popup_getpos(id1).line)
3350 call assert_equal(9, popup_getpos(id2).line)
3351
3352 call feedkeys('0', 'xt')
3353 call assert_equal({}, popup_getpos(id1))
3354 call assert_equal({}, popup_getpos(id2))
3355
3356 call popup_clear()
3357 delfunc CloseFilter
3358endfunc
3359
Bram Moolenaar20ebbea2019-11-30 17:58:27 +01003360func Test_popupwin_double_width()
3361 CheckScreendump
3362
3363 let lines =<< trim END
3364 call setline(1, 'x你好世界你好世你好世界你好')
3365 call setline(2, '你好世界你好世你好世界你好')
3366 call setline(3, 'x你好世界你好世你好世界你好')
3367 call popup_create('你好,世界 - 你好,世界xxxxx', #{line: 1, col: 3, maxwidth: 14})
3368 END
3369 call writefile(lines, 'XtestPopupWide')
3370
3371 let buf = RunVimInTerminal('-S XtestPopupWide', #{rows: 10})
3372 call VerifyScreenDump(buf, 'Test_popupwin_doublewidth_1', {})
3373
3374 call StopVimInTerminal(buf)
3375 call delete('XtestPopupWide')
3376endfunc
3377
3378func Test_popupwin_sign()
3379 CheckScreendump
3380
3381 let lines =<< trim END
3382 call setline(1, range(10))
3383 call sign_define('Current', {
3384 \ 'text': '>>',
3385 \ 'texthl': 'WarningMsg',
3386 \ 'linehl': 'Error',
3387 \ })
3388 call sign_define('Other', {
3389 \ 'text': '#!',
3390 \ 'texthl': 'Error',
3391 \ 'linehl': 'Search',
3392 \ })
3393 let winid = popup_create(['hello', 'bright', 'world'], {
3394 \ 'minwidth': 20,
3395 \ })
3396 call setwinvar(winid, "&signcolumn", "yes")
3397 let winbufnr = winbufnr(winid)
3398
3399 " add sign to current buffer, shows
3400 call sign_place(1, 'Selected', 'Current', bufnr('%'), {'lnum': 1})
3401 " add sign to current buffer, does not show
3402 call sign_place(2, 'PopUpSelected', 'Other', bufnr('%'), {'lnum': 2})
3403
3404 " add sign to popup buffer, shows
3405 call sign_place(3, 'PopUpSelected', 'Other', winbufnr, {'lnum': 1})
3406 " add sign to popup buffer, does not show
3407 call sign_place(4, 'Selected', 'Current', winbufnr, {'lnum': 2})
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02003408
3409 func SetOptions()
3410 call setwinvar(g:winid, '&number', 1)
3411 call setwinvar(g:winid, '&foldcolumn', 2)
3412 call popup_settext(g:winid, 'a longer line to check the width')
3413 endfunc
Bram Moolenaar20ebbea2019-11-30 17:58:27 +01003414 END
3415 call writefile(lines, 'XtestPopupSign')
3416
3417 let buf = RunVimInTerminal('-S XtestPopupSign', #{rows: 10})
3418 call VerifyScreenDump(buf, 'Test_popupwin_sign_1', {})
3419
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02003420 " set more options to check the width is adjusted
3421 call term_sendkeys(buf, ":call SetOptions()\<CR>")
3422 call VerifyScreenDump(buf, 'Test_popupwin_sign_2', {})
3423
Bram Moolenaar20ebbea2019-11-30 17:58:27 +01003424 call StopVimInTerminal(buf)
3425 call delete('XtestPopupSign')
3426endfunc
3427
Bram Moolenaar99ebf222019-12-10 23:44:48 +01003428func Test_popupwin_bufnr()
3429 let popwin = popup_create(['blah'], #{})
3430 let popbuf = winbufnr(popwin)
3431 split asdfasdf
3432 let newbuf = bufnr()
3433 call assert_true(newbuf > popbuf, 'New buffer number is higher')
3434 call assert_equal(newbuf, bufnr('$'))
3435 call popup_clear()
3436 let popwin = popup_create(['blah'], #{})
3437 " reuses previous buffer number
3438 call assert_equal(popbuf, winbufnr(popwin))
3439 call assert_equal(newbuf, bufnr('$'))
3440
3441 call popup_clear()
3442 bwipe!
3443endfunc
3444
Bram Moolenaarec084d32020-02-28 22:44:47 +01003445func Test_popupwin_filter_input_multibyte()
3446 func MyPopupFilter(winid, c)
3447 let g:bytes = range(a:c->strlen())->map({i -> char2nr(a:c[i])})
3448 return 0
3449 endfunc
3450 let winid = popup_create('', #{mapping: 0, filter: 'MyPopupFilter'})
3451
3452 " UTF-8: E3 80 80, including K_SPECIAL(0x80)
3453 call feedkeys("\u3000", 'xt')
3454 call assert_equal([0xe3, 0x80, 0x80], g:bytes)
3455
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01003456 " UTF-8: E3 80 9B, including CSI(0x9B)
3457 call feedkeys("\u301b", 'xt')
3458 call assert_equal([0xe3, 0x80, 0x9b], g:bytes)
Bram Moolenaarec084d32020-02-28 22:44:47 +01003459
Bram Moolenaar20298ce2020-06-19 21:46:52 +02003460 if has('unix')
3461 " with modifyOtherKeys <M-S-a> does not include a modifier sequence
Bram Moolenaarb326edf2020-06-20 15:03:38 +02003462 if has('gui_running')
3463 call feedkeys("\x9b\xfc\x08A", 'Lx!')
3464 else
3465 call feedkeys("\<Esc>[27;4;65~", 'Lx!')
3466 endif
Bram Moolenaar20298ce2020-06-19 21:46:52 +02003467 call assert_equal([0xc3, 0x81], g:bytes)
3468 endif
3469
Bram Moolenaarec084d32020-02-28 22:44:47 +01003470 call popup_clear()
3471 delfunc MyPopupFilter
3472 unlet g:bytes
3473endfunc
3474
Bram Moolenaarba2920f2020-03-06 21:43:17 +01003475func Test_popupwin_atcursor_far_right()
3476 new
3477
3478 " this was getting stuck
3479 set signcolumn=yes
3480 call setline(1, repeat('=', &columns))
3481 normal! ggg$
Bram Moolenaaref6b9792020-05-13 16:34:15 +02003482 let winid = popup_atcursor(repeat('x', 500), #{moved: 'any', border: []})
Bram Moolenaarba2920f2020-03-06 21:43:17 +01003483
Bram Moolenaar6d585f42020-07-26 22:20:54 +02003484 " 'signcolumn' was getting reset
3485 call setwinvar(winid, '&signcolumn', 'yes')
3486 call popup_setoptions(winid, {'zindex': 1000})
3487 call assert_equal('yes', getwinvar(winid, '&signcolumn'))
3488
Bram Moolenaaref6b9792020-05-13 16:34:15 +02003489 call popup_close(winid)
Bram Moolenaarba2920f2020-03-06 21:43:17 +01003490 bwipe!
3491 set signcolumn&
3492endfunc
3493
Bram Moolenaar0f1563f2020-03-20 21:15:51 +01003494func Test_popupwin_splitmove()
3495 vsplit
3496 let win2 = win_getid()
3497 let popup_winid = popup_dialog('hello', {})
3498 call assert_fails('call win_splitmove(popup_winid, win2)', 'E957:')
3499 call assert_fails('call win_splitmove(win2, popup_winid)', 'E957:')
3500
3501 call popup_clear()
3502 bwipe
3503endfunc
3504
3505
Bram Moolenaar331bafd2019-07-20 17:46:05 +02003506" vim: shiftwidth=2 sts=2