blob: 054d479efd37ae9f07b318f3a224a854aed095d0 [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 {{{")
591 call setline(nr * 12 + 11 , "end }}}")
592 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 Moolenaar437a7462019-07-05 20:17:22 +02002147func Test_popup_fitting_scrollbar()
2148 " this was causing a crash, divide by zero
2149 let winid = popup_create([
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002150 \ 'one', 'two', 'longer line that wraps', 'four', 'five'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002151 \ scrollbar: 1,
2152 \ maxwidth: 10,
2153 \ maxheight: 5,
2154 \ firstline: 2})
Bram Moolenaar437a7462019-07-05 20:17:22 +02002155 redraw
2156 call popup_clear()
2157endfunc
2158
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002159func Test_popup_settext()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002160 CheckScreendump
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002161
2162 let lines =<< trim END
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002163 let opts = #{wrap: 0}
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002164 let p = popup_create('test', opts)
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002165 eval p->popup_settext('this is a text')
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002166 END
2167
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002168 call writefile(lines, 'XtestPopupSetText')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002169 let buf = RunVimInTerminal('-S XtestPopupSetText', #{rows: 10})
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002170 call VerifyScreenDump(buf, 'Test_popup_settext_01', {})
2171
2172 " Setting to empty string clears it
2173 call term_sendkeys(buf, ":call popup_settext(p, '')\<CR>")
2174 call VerifyScreenDump(buf, 'Test_popup_settext_02', {})
2175
2176 " Setting a list
2177 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
2178 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
2179
2180 " Shrinking with a list
2181 call term_sendkeys(buf, ":call popup_settext(p, ['a'])\<CR>")
2182 call VerifyScreenDump(buf, 'Test_popup_settext_04', {})
2183
2184 " Growing with a list
2185 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
2186 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
2187
2188 " Empty list clears
2189 call term_sendkeys(buf, ":call popup_settext(p, [])\<CR>")
2190 call VerifyScreenDump(buf, 'Test_popup_settext_05', {})
2191
2192 " Dicts
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002193 call term_sendkeys(buf, ":call popup_settext(p, [#{text: 'aaaa'}, #{text: 'bbbb'}, #{text: 'cccc'}])\<CR>")
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002194 call VerifyScreenDump(buf, 'Test_popup_settext_06', {})
2195
Bram Moolenaarb0992022020-01-30 14:55:42 +01002196 " range() (doesn't work)
2197 call term_sendkeys(buf, ":call popup_settext(p, range(4, 8))\<CR>")
2198 call VerifyScreenDump(buf, 'Test_popup_settext_07', {})
2199
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002200 " clean up
2201 call StopVimInTerminal(buf)
2202 call delete('XtestPopupSetText')
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002203endfunc
2204
2205func Test_popup_hidden()
2206 new
2207
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002208 let winid = popup_atcursor('text', #{hidden: 1})
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002209 redraw
2210 call assert_equal(0, popup_getpos(winid).visible)
2211 call popup_close(winid)
2212
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002213 let winid = popup_create('text', #{hidden: 1})
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002214 redraw
2215 call assert_equal(0, popup_getpos(winid).visible)
2216 call popup_close(winid)
2217
2218 func QuitCallback(id, res)
2219 let s:cb_winid = a:id
2220 let s:cb_res = a:res
2221 endfunc
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002222 let winid = 'make a choice'->popup_dialog(#{hidden: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002223 \ filter: 'popup_filter_yesno',
2224 \ callback: 'QuitCallback',
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002225 \ })
2226 redraw
2227 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarae943152019-06-16 22:54:14 +02002228 call assert_equal(function('popup_filter_yesno'), popup_getoptions(winid).filter)
2229 call assert_equal(function('QuitCallback'), popup_getoptions(winid).callback)
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002230 exe "normal anot used by filter\<Esc>"
2231 call assert_equal('not used by filter', getline(1))
2232
2233 call popup_show(winid)
2234 call feedkeys('y', "xt")
2235 call assert_equal(1, s:cb_res)
2236
2237 bwipe!
2238 delfunc QuitCallback
2239endfunc
Bram Moolenaarae943152019-06-16 22:54:14 +02002240
2241" Test options not checked elsewhere
2242func Test_set_get_options()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002243 let winid = popup_create('some text', #{highlight: 'Beautiful'})
Bram Moolenaarae943152019-06-16 22:54:14 +02002244 let options = popup_getoptions(winid)
2245 call assert_equal(1, options.wrap)
2246 call assert_equal(0, options.drag)
2247 call assert_equal('Beautiful', options.highlight)
2248
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002249 call popup_setoptions(winid, #{wrap: 0, drag: 1, highlight: 'Another'})
Bram Moolenaarae943152019-06-16 22:54:14 +02002250 let options = popup_getoptions(winid)
2251 call assert_equal(0, options.wrap)
2252 call assert_equal(1, options.drag)
2253 call assert_equal('Another', options.highlight)
2254
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02002255 call assert_fails('call popup_setoptions(winid, [])', 'E715:')
2256 call assert_fails('call popup_setoptions(winid, test_null_dict())', 'E715:')
2257
Bram Moolenaarae943152019-06-16 22:54:14 +02002258 call popup_close(winid)
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02002259 call assert_equal(0, popup_setoptions(winid, options.wrap))
Bram Moolenaarae943152019-06-16 22:54:14 +02002260endfunc
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002261
2262func Test_popupwin_garbage_collect()
2263 func MyPopupFilter(x, winid, c)
2264 " NOP
2265 endfunc
2266
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002267 let winid = popup_create('something', #{filter: function('MyPopupFilter', [{}])})
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002268 call test_garbagecollect_now()
2269 redraw
Bram Moolenaar1bc353b2019-09-01 14:45:28 +02002270 " Must not crash caused by invalid memory access
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002271 call feedkeys('j', 'xt')
2272 call assert_true(v:true)
2273
2274 call popup_close(winid)
2275 delfunc MyPopupFilter
2276endfunc
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002277
Bram Moolenaar581ba392019-09-03 22:08:33 +02002278func Test_popupwin_filter_mode()
2279 func MyPopupFilter(winid, c)
2280 let s:typed = a:c
2281 if a:c == ':' || a:c == "\r" || a:c == 'v'
2282 " can start cmdline mode, get out, and start/stop Visual mode
2283 return 0
2284 endif
2285 return 1
2286 endfunc
2287
2288 " Normal, Visual and Insert mode
2289 let winid = popup_create('something', #{filter: 'MyPopupFilter', filtermode: 'nvi'})
2290 redraw
2291 call feedkeys('x', 'xt')
2292 call assert_equal('x', s:typed)
2293
2294 call feedkeys(":let g:foo = 'foo'\<CR>", 'xt')
2295 call assert_equal(':', s:typed)
2296 call assert_equal('foo', g:foo)
2297
2298 let @x = 'something'
2299 call feedkeys('v$"xy', 'xt')
2300 call assert_equal('y', s:typed)
2301 call assert_equal('something', @x) " yank command is filtered out
2302 call feedkeys('v', 'xt') " end Visual mode
2303
2304 call popup_close(winid)
2305
2306 " only Normal mode
2307 let winid = popup_create('something', #{filter: 'MyPopupFilter', filtermode: 'n'})
2308 redraw
2309 call feedkeys('x', 'xt')
2310 call assert_equal('x', s:typed)
2311
2312 call feedkeys(":let g:foo = 'foo'\<CR>", 'xt')
2313 call assert_equal(':', s:typed)
2314 call assert_equal('foo', g:foo)
2315
2316 let @x = 'something'
2317 call feedkeys('v$"xy', 'xt')
2318 call assert_equal('v', s:typed)
2319 call assert_notequal('something', @x)
2320
2321 call popup_close(winid)
2322
2323 " default: all modes
2324 let winid = popup_create('something', #{filter: 'MyPopupFilter'})
2325 redraw
2326 call feedkeys('x', 'xt')
2327 call assert_equal('x', s:typed)
2328
2329 let g:foo = 'bar'
2330 call feedkeys(":let g:foo = 'foo'\<CR>", 'xt')
2331 call assert_equal("\r", s:typed)
2332 call assert_equal('bar', g:foo)
2333
2334 let @x = 'something'
2335 call feedkeys('v$"xy', 'xt')
2336 call assert_equal('y', s:typed)
2337 call assert_equal('something', @x) " yank command is filtered out
2338 call feedkeys('v', 'xt') " end Visual mode
2339
2340 call popup_close(winid)
2341 delfunc MyPopupFilter
2342endfunc
2343
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002344func Test_popupwin_filter_mouse()
2345 func MyPopupFilter(winid, c)
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002346 let g:got_mousepos = getmousepos()
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002347 return 0
2348 endfunc
2349
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002350 call setline(1, ['.'->repeat(25)]->repeat(10))
2351 let winid = popup_create(['short', 'long line that will wrap', 'other'], #{
2352 \ line: 2,
2353 \ col: 4,
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002354 \ maxwidth: 12,
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002355 \ padding: [],
2356 \ border: [],
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002357 \ filter: 'MyPopupFilter',
2358 \ })
2359 redraw
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002360 " 123456789012345678901
2361 " 1 .....................
2362 " 2 ...+--------------+..
2363 " 3 ...| |..
2364 " 4 ...| short |..
2365 " 5 ...| long line th |..
2366 " 6 ...| at will wrap |..
2367 " 7 ...| other |..
2368 " 8 ...| |..
2369 " 9 ...+--------------+..
2370 " 10 .....................
2371 let tests = []
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002372
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002373 func AddItemOutsidePopup(tests, row, col)
2374 eval a:tests->add(#{clickrow: a:row, clickcol: a:col, result: #{
2375 \ screenrow: a:row, screencol: a:col,
2376 \ winid: win_getid(), winrow: a:row, wincol: a:col,
2377 \ line: a:row, column: a:col,
2378 \ }})
2379 endfunc
2380 func AddItemInPopupBorder(tests, winid, row, col)
2381 eval a:tests->add(#{clickrow: a:row, clickcol: a:col, result: #{
2382 \ screenrow: a:row, screencol: a:col,
2383 \ winid: a:winid, winrow: a:row - 1, wincol: a:col - 3,
2384 \ line: 0, column: 0,
2385 \ }})
2386 endfunc
2387 func AddItemInPopupText(tests, winid, row, col, textline, textcol)
2388 eval a:tests->add(#{clickrow: a:row, clickcol: a:col, result: #{
2389 \ screenrow: a:row, screencol: a:col,
2390 \ winid: a:winid, winrow: a:row - 1, wincol: a:col - 3,
2391 \ line: a:textline, column: a:textcol,
2392 \ }})
2393 endfunc
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002394
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002395 " above and below popup
2396 for c in range(1, 21)
2397 call AddItemOutsidePopup(tests, 1, c)
2398 call AddItemOutsidePopup(tests, 10, c)
2399 endfor
2400 " left and right of popup
2401 for r in range(1, 10)
2402 call AddItemOutsidePopup(tests, r, 3)
2403 call AddItemOutsidePopup(tests, r, 20)
2404 endfor
2405 " top and bottom in popup
2406 for c in range(4, 19)
2407 call AddItemInPopupBorder(tests, winid, 2, c)
2408 call AddItemInPopupBorder(tests, winid, 3, c)
2409 call AddItemInPopupBorder(tests, winid, 8, c)
2410 call AddItemInPopupBorder(tests, winid, 9, c)
2411 endfor
2412 " left and right margin in popup
2413 for r in range(2, 9)
2414 call AddItemInPopupBorder(tests, winid, r, 4)
2415 call AddItemInPopupBorder(tests, winid, r, 5)
2416 call AddItemInPopupBorder(tests, winid, r, 18)
2417 call AddItemInPopupBorder(tests, winid, r, 19)
2418 endfor
2419 " text "short"
2420 call AddItemInPopupText(tests, winid, 4, 6, 1, 1)
2421 call AddItemInPopupText(tests, winid, 4, 10, 1, 5)
2422 call AddItemInPopupText(tests, winid, 4, 11, 1, 6)
2423 call AddItemInPopupText(tests, winid, 4, 17, 1, 6)
2424 " text "long line th"
2425 call AddItemInPopupText(tests, winid, 5, 6, 2, 1)
2426 call AddItemInPopupText(tests, winid, 5, 10, 2, 5)
2427 call AddItemInPopupText(tests, winid, 5, 17, 2, 12)
2428 " text "at will wrap"
2429 call AddItemInPopupText(tests, winid, 6, 6, 2, 13)
2430 call AddItemInPopupText(tests, winid, 6, 10, 2, 17)
2431 call AddItemInPopupText(tests, winid, 6, 17, 2, 24)
2432 " text "other"
2433 call AddItemInPopupText(tests, winid, 7, 6, 3, 1)
2434 call AddItemInPopupText(tests, winid, 7, 10, 3, 5)
2435 call AddItemInPopupText(tests, winid, 7, 11, 3, 6)
2436 call AddItemInPopupText(tests, winid, 7, 17, 3, 6)
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002437
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002438 for item in tests
2439 call test_setmouse(item.clickrow, item.clickcol)
2440 call feedkeys("\<LeftMouse>", 'xt')
2441 call assert_equal(item.result, g:got_mousepos)
2442 endfor
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002443
2444 call popup_close(winid)
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002445 enew!
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002446 delfunc MyPopupFilter
2447endfunc
2448
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002449func Test_popupwin_with_buffer()
2450 call writefile(['some text', 'in a buffer'], 'XsomeFile')
2451 let buf = bufadd('XsomeFile')
2452 call assert_equal(0, bufloaded(buf))
Bram Moolenaar46451042019-08-24 15:50:46 +02002453
2454 setlocal number
2455 call setbufvar(buf, "&wrapmargin", 13)
2456
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002457 let winid = popup_create(buf, {})
2458 call assert_notequal(0, winid)
2459 let pos = popup_getpos(winid)
2460 call assert_equal(2, pos.height)
2461 call assert_equal(1, bufloaded(buf))
Bram Moolenaar46451042019-08-24 15:50:46 +02002462
2463 " window-local option is set to default, buffer-local is not
2464 call assert_equal(0, getwinvar(winid, '&number'))
2465 call assert_equal(13, getbufvar(buf, '&wrapmargin'))
2466
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002467 call popup_close(winid)
2468 call assert_equal({}, popup_getpos(winid))
2469 call assert_equal(1, bufloaded(buf))
2470 exe 'bwipe! ' .. buf
Bram Moolenaar46451042019-08-24 15:50:46 +02002471 setlocal nonumber
Bram Moolenaar7866b872019-07-01 22:21:01 +02002472
2473 edit test_popupwin.vim
2474 let winid = popup_create(bufnr(''), {})
2475 redraw
2476 call popup_close(winid)
Bram Moolenaar3940ec62019-07-05 21:53:24 +02002477 call delete('XsomeFile')
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002478endfunc
Bram Moolenaare296e312019-07-03 23:20:18 +02002479
Bram Moolenaare0d749a2019-09-25 22:14:48 +02002480func Test_popupwin_terminal_buffer()
Bram Moolenaard2c1fb42019-09-25 23:06:40 +02002481 CheckFeature terminal
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002482 CheckUnix
Bram Moolenaare06a28f2020-05-13 23:24:12 +02002483 " Starting a terminal to run a shell in is considered flaky.
2484 let g:test_is_flaky = 1
Bram Moolenaard2c1fb42019-09-25 23:06:40 +02002485
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002486 let origwin = win_getid()
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002487 let termbuf = term_start(&shell, #{hidden: 1})
2488 let winid = popup_create(termbuf, #{minwidth: 40, minheight: 10})
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002489 " Wait for shell to start
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002490 call WaitForAssert({-> assert_equal("run", job_status(term_getjob(termbuf)))})
Bram Moolenaare06a28f2020-05-13 23:24:12 +02002491 sleep 100m
Bram Moolenaar631ebc42020-02-03 22:15:26 +01002492 " Check this doesn't crash
2493 call assert_equal(winnr(), winnr('j'))
2494 call assert_equal(winnr(), winnr('k'))
2495 call assert_equal(winnr(), winnr('h'))
2496 call assert_equal(winnr(), winnr('l'))
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02002497
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002498 " Cannot quit while job is running
2499 call assert_fails('call feedkeys("\<C-W>:quit\<CR>", "xt")', 'E948:')
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02002500
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002501 " Cannot enter Terminal-Normal mode. (TODO: but it works...)
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02002502 call feedkeys("xxx\<C-W>N", 'xt')
2503 call assert_fails('call feedkeys("gf", "xt")', 'E863:')
2504 call feedkeys("a\<C-U>", 'xt')
2505
Bram Moolenaar3f65c662020-05-27 23:15:16 +02002506 " Cannot escape from terminal window
2507 call assert_fails('tab drop xxx', 'E863:')
2508
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002509 " Cannot open a second one.
2510 let termbuf2 = term_start(&shell, #{hidden: 1})
2511 call assert_fails('call popup_create(termbuf2, #{})', 'E861:')
2512 call term_sendkeys(termbuf2, "exit\<CR>")
2513
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02002514 " Exiting shell closes popup window
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002515 call feedkeys("exit\<CR>", 'xt')
2516 " Wait for shell to exit
Bram Moolenaarb2b218d2020-06-22 20:22:19 +02002517 call WaitForAssert({-> assert_equal("dead", job_status(term_getjob(termbuf)))})
Bram Moolenaar373c6512020-06-22 19:24:23 +02002518
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002519 call feedkeys(":quit\<CR>", 'xt')
2520 call assert_equal(origwin, win_getid())
Bram Moolenaare0d749a2019-09-25 22:14:48 +02002521endfunc
2522
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002523func Test_popupwin_close_prevwin()
2524 CheckFeature terminal
2525
2526 call assert_equal(1, winnr('$'))
2527 split
2528 wincmd b
2529 call assert_equal(2, winnr())
2530 let buf = term_start(&shell, #{hidden: 1})
2531 call popup_create(buf, {})
2532 call term_wait(buf, 100)
2533 call popup_clear(1)
2534 call assert_equal(2, winnr())
2535
2536 quit
2537 exe 'bwipe! ' .. buf
2538endfunc
2539
Bram Moolenaar934470e2019-09-01 23:27:05 +02002540func Test_popupwin_with_buffer_and_filter()
2541 new Xwithfilter
2542 call setline(1, range(100))
2543 let bufnr = bufnr()
2544 hide
2545
2546 func BufferFilter(win, key)
2547 if a:key == 'G'
2548 " recursive use of "G" does not cause problems.
2549 call win_execute(a:win, 'normal! G')
2550 return 1
2551 endif
2552 return 0
2553 endfunc
2554
2555 let winid = popup_create(bufnr, #{maxheight: 5, filter: 'BufferFilter'})
2556 call assert_equal(1, popup_getpos(winid).firstline)
2557 redraw
2558 call feedkeys("G", 'xt')
2559 call assert_equal(99, popup_getpos(winid).firstline)
2560
2561 call popup_close(winid)
2562 exe 'bwipe! ' .. bufnr
2563endfunc
2564
Bram Moolenaare296e312019-07-03 23:20:18 +02002565func Test_popupwin_width()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002566 let winid = popup_create(repeat(['short', 'long long long line', 'medium width'], 50), #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002567 \ maxwidth: 40,
2568 \ maxheight: 10,
Bram Moolenaare296e312019-07-03 23:20:18 +02002569 \ })
2570 for top in range(1, 20)
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002571 eval winid->popup_setoptions(#{firstline: top})
Bram Moolenaare296e312019-07-03 23:20:18 +02002572 redraw
2573 call assert_equal(19, popup_getpos(winid).width)
2574 endfor
2575 call popup_clear()
2576endfunc
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02002577
2578func Test_popupwin_buf_close()
2579 let buf = bufadd('Xtestbuf')
2580 call bufload(buf)
2581 call setbufline(buf, 1, ['just', 'some', 'lines'])
2582 let winid = popup_create(buf, {})
2583 redraw
2584 call assert_equal(3, popup_getpos(winid).height)
2585 let bufinfo = getbufinfo(buf)[0]
2586 call assert_equal(1, bufinfo.changed)
2587 call assert_equal(0, bufinfo.hidden)
2588 call assert_equal(0, bufinfo.listed)
2589 call assert_equal(1, bufinfo.loaded)
2590 call assert_equal([], bufinfo.windows)
2591 call assert_equal([winid], bufinfo.popups)
2592
2593 call popup_close(winid)
2594 call assert_equal({}, popup_getpos(winid))
2595 let bufinfo = getbufinfo(buf)[0]
2596 call assert_equal(1, bufinfo.changed)
2597 call assert_equal(1, bufinfo.hidden)
2598 call assert_equal(0, bufinfo.listed)
2599 call assert_equal(1, bufinfo.loaded)
2600 call assert_equal([], bufinfo.windows)
2601 call assert_equal([], bufinfo.popups)
2602 exe 'bwipe! ' .. buf
2603endfunc
Bram Moolenaar017c2692019-07-13 14:17:51 +02002604
2605func Test_popup_menu_with_maxwidth()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002606 CheckScreendump
Bram Moolenaar017c2692019-07-13 14:17:51 +02002607
2608 let lines =<< trim END
2609 call setline(1, range(1, 10))
2610 hi ScrollThumb ctermbg=blue
2611 hi ScrollBar ctermbg=red
2612 func PopupMenu(lines, line, col, scrollbar = 0)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002613 return popup_menu(a:lines, #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002614 \ maxwidth: 10,
2615 \ maxheight: 3,
2616 \ pos : 'topleft',
2617 \ col : a:col,
2618 \ line : a:line,
2619 \ scrollbar : a:scrollbar,
Bram Moolenaar017c2692019-07-13 14:17:51 +02002620 \ })
2621 endfunc
2622 call PopupMenu(['x'], 1, 1)
2623 call PopupMenu(['123456789|'], 1, 16)
2624 call PopupMenu(['123456789|' .. ' '], 7, 1)
2625 call PopupMenu([repeat('123456789|', 100)], 7, 16)
2626 call PopupMenu(repeat(['123456789|' .. ' '], 5), 1, 33, 1)
2627 END
2628 call writefile(lines, 'XtestPopupMenuMaxWidth')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002629 let buf = RunVimInTerminal('-S XtestPopupMenuMaxWidth', #{rows: 13})
Bram Moolenaar017c2692019-07-13 14:17:51 +02002630 call VerifyScreenDump(buf, 'Test_popupwin_menu_maxwidth_1', {})
2631
2632 " close the menu popupwin.
2633 call term_sendkeys(buf, " ")
2634 call term_sendkeys(buf, " ")
2635 call term_sendkeys(buf, " ")
2636 call term_sendkeys(buf, " ")
2637 call term_sendkeys(buf, " ")
2638
2639 " clean up
2640 call StopVimInTerminal(buf)
2641 call delete('XtestPopupMenuMaxWidth')
2642endfunc
2643
Bram Moolenaara901a372019-07-13 16:38:50 +02002644func Test_popup_menu_with_scrollbar()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002645 CheckScreendump
Bram Moolenaara901a372019-07-13 16:38:50 +02002646
2647 let lines =<< trim END
2648 call setline(1, range(1, 20))
2649 hi ScrollThumb ctermbg=blue
2650 hi ScrollBar ctermbg=red
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002651 eval ['one', 'two', 'three', 'four', 'five',
2652 \ 'six', 'seven', 'eight', 'nine']
2653 \ ->popup_menu(#{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002654 \ minwidth: 8,
2655 \ maxheight: 3,
Bram Moolenaara901a372019-07-13 16:38:50 +02002656 \ })
2657 END
2658 call writefile(lines, 'XtestPopupMenuScroll')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002659 let buf = RunVimInTerminal('-S XtestPopupMenuScroll', #{rows: 10})
Bram Moolenaara901a372019-07-13 16:38:50 +02002660
2661 call term_sendkeys(buf, "j")
2662 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_1', {})
2663
2664 call term_sendkeys(buf, "jjj")
2665 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_2', {})
2666
2667 " if the cursor is the bottom line, it stays at the bottom line.
2668 call term_sendkeys(buf, repeat("j", 20))
2669 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_3', {})
2670
2671 call term_sendkeys(buf, "kk")
2672 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_4', {})
2673
2674 call term_sendkeys(buf, "k")
2675 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_5', {})
2676
2677 " if the cursor is in the top line, it stays in the top line.
2678 call term_sendkeys(buf, repeat("k", 20))
2679 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_6', {})
2680
2681 " close the menu popupwin.
2682 call term_sendkeys(buf, " ")
2683
2684 " clean up
2685 call StopVimInTerminal(buf)
2686 call delete('XtestPopupMenuScroll')
2687endfunc
2688
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002689func Test_popup_menu_filter()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002690 CheckScreendump
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002691
2692 let lines =<< trim END
2693 function! MyFilter(winid, key) abort
2694 if a:key == "0"
2695 call win_execute(a:winid, "call setpos('.', [0, 1, 1, 0])")
2696 return 1
2697 endif
2698 if a:key == "G"
2699 call win_execute(a:winid, "call setpos('.', [0, line('$'), 1, 0])")
2700 return 1
2701 endif
2702 if a:key == "j"
2703 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0])")
2704 return 1
2705 endif
2706 if a:key == "k"
2707 call win_execute(a:winid, "call setpos('.', [0, line('.') - 1, 1, 0])")
2708 return 1
2709 endif
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02002710 if a:key == ':'
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002711 call popup_close(a:winid)
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02002712 return 0
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002713 endif
2714 return 0
2715 endfunction
2716 call popup_menu(['111', '222', '333', '444', '555', '666', '777', '888', '999'], #{
2717 \ maxheight : 3,
2718 \ filter : 'MyFilter'
2719 \ })
2720 END
2721 call writefile(lines, 'XtestPopupMenuFilter')
2722 let buf = RunVimInTerminal('-S XtestPopupMenuFilter', #{rows: 10})
2723
2724 call term_sendkeys(buf, "j")
2725 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_1', {})
2726
2727 call term_sendkeys(buf, "k")
2728 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_2', {})
2729
2730 call term_sendkeys(buf, "G")
2731 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_3', {})
2732
2733 call term_sendkeys(buf, "0")
2734 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_4', {})
2735
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02002736 " check that when the popup is closed in the filter the screen is redrawn
2737 call term_sendkeys(buf, ":")
2738 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_5', {})
2739 call term_sendkeys(buf, "\<CR>")
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002740
2741 " clean up
2742 call StopVimInTerminal(buf)
2743 call delete('XtestPopupMenuFilter')
2744endfunc
2745
2746func Test_popup_cursorline()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002747 CheckScreendump
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002748
2749 let winid = popup_create('some text', {})
2750 call assert_equal(0, popup_getoptions(winid).cursorline)
2751 call popup_close(winid)
2752
2753 let winid = popup_create('some text', #{ cursorline: 1, })
2754 call assert_equal(1, popup_getoptions(winid).cursorline)
2755 call popup_close(winid)
2756
2757 let winid = popup_create('some text', #{ cursorline: 0, })
2758 call assert_equal(0, popup_getoptions(winid).cursorline)
2759 call popup_close(winid)
2760
2761 let winid = popup_menu('some text', {})
2762 call assert_equal(1, popup_getoptions(winid).cursorline)
2763 call popup_close(winid)
2764
2765 let winid = popup_menu('some text', #{ cursorline: 1, })
2766 call assert_equal(1, popup_getoptions(winid).cursorline)
2767 call popup_close(winid)
2768
2769 let winid = popup_menu('some text', #{ cursorline: 0, })
2770 call assert_equal(0, popup_getoptions(winid).cursorline)
2771 call popup_close(winid)
2772
2773 " ---------
2774 " Pattern 1
2775 " ---------
2776 let lines =<< trim END
2777 call popup_create(['111', '222', '333'], #{ cursorline : 0 })
2778 END
2779 call writefile(lines, 'XtestPopupCursorLine')
2780 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2781 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_1', {})
2782 call term_sendkeys(buf, ":call popup_clear()\<cr>")
2783 call StopVimInTerminal(buf)
2784
2785 " ---------
2786 " Pattern 2
2787 " ---------
2788 let lines =<< trim END
2789 call popup_create(['111', '222', '333'], #{ cursorline : 1 })
2790 END
2791 call writefile(lines, 'XtestPopupCursorLine')
2792 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2793 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_2', {})
2794 call term_sendkeys(buf, ":call popup_clear()\<cr>")
2795 call StopVimInTerminal(buf)
2796
2797 " ---------
2798 " Pattern 3
2799 " ---------
2800 let lines =<< trim END
2801 function! MyFilter(winid, key) abort
2802 if a:key == "j"
2803 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0]) | redraw")
2804 return 1
2805 endif
2806 if a:key == 'x'
2807 call popup_close(a:winid)
2808 return 1
2809 endif
2810 return 0
2811 endfunction
2812 call popup_menu(['111', '222', '333'], #{
2813 \ cursorline : 0,
2814 \ maxheight : 2,
2815 \ filter : 'MyFilter',
2816 \ })
2817 END
2818 call writefile(lines, 'XtestPopupCursorLine')
2819 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2820 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_3', {})
2821 call term_sendkeys(buf, "j")
2822 call term_sendkeys(buf, "j")
2823 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_4', {})
2824 call term_sendkeys(buf, "x")
2825 call StopVimInTerminal(buf)
2826
2827 " ---------
2828 " Pattern 4
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 : 1,
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_5', {})
2851 call term_sendkeys(buf, "j")
2852 call term_sendkeys(buf, "j")
2853 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_6', {})
2854 call term_sendkeys(buf, "x")
2855 call StopVimInTerminal(buf)
2856
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02002857 " ---------
2858 " Cursor in second line when creating the popup
2859 " ---------
2860 let lines =<< trim END
2861 let winid = popup_create(['111', '222', '333'], #{
2862 \ cursorline : 1,
2863 \ })
2864 call win_execute(winid, "2")
2865 END
2866 call writefile(lines, 'XtestPopupCursorLine')
2867 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2868 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_7', {})
2869 call StopVimInTerminal(buf)
2870
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002871 call delete('XtestPopupCursorLine')
Bram Moolenaar4eb7dae2019-11-12 22:33:45 +01002872
2873 " ---------
2874 " Use current buffer for popupmenu
2875 " ---------
2876 let lines =<< trim END
2877 call setline(1, ['one', 'two', 'three'])
2878 let winid = popup_create(bufnr('%'), #{
2879 \ cursorline : 1,
2880 \ })
2881 call win_execute(winid, "2")
2882 END
2883 call writefile(lines, 'XtestPopupCursorLine')
2884 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2885 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_8', {})
2886 call StopVimInTerminal(buf)
2887
2888 call delete('XtestPopupCursorLine')
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002889endfunc
2890
Bram Moolenaarf914a332019-07-20 15:09:56 +02002891func Test_previewpopup()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002892 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002893 CheckFeature quickfix
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002894
Bram Moolenaarf914a332019-07-20 15:09:56 +02002895 call writefile([
2896 \ "!_TAG_FILE_ENCODING\tutf-8\t//",
2897 \ "another\tXtagfile\t/^this is another",
2898 \ "theword\tXtagfile\t/^theword"],
2899 \ 'Xtags')
2900 call writefile(range(1,20)
2901 \ + ['theword is here']
2902 \ + range(22, 27)
2903 \ + ['this is another place']
2904 \ + range(29, 40),
2905 \ "Xtagfile")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002906 call writefile(range(1,10)
2907 \ + ['searched word is here']
2908 \ + range(12, 20),
2909 \ "Xheader.h")
Bram Moolenaarf914a332019-07-20 15:09:56 +02002910 let lines =<< trim END
2911 set tags=Xtags
2912 call setline(1, [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002913 \ 'one',
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002914 \ '#include "Xheader.h"',
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002915 \ 'three',
2916 \ 'four',
2917 \ 'five',
2918 \ 'six',
2919 \ 'seven',
2920 \ 'find theword somewhere',
2921 \ 'nine',
2922 \ 'this is another word',
2923 \ 'very long line where the word is also another'])
Bram Moolenaarf914a332019-07-20 15:09:56 +02002924 set previewpopup=height:4,width:40
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002925 set path=.
Bram Moolenaarf914a332019-07-20 15:09:56 +02002926 END
2927 call writefile(lines, 'XtestPreviewPopup')
2928 let buf = RunVimInTerminal('-S XtestPreviewPopup', #{rows: 14})
2929
2930 call term_sendkeys(buf, "/theword\<CR>\<C-W>}")
2931 call term_sendkeys(buf, ":\<CR>")
2932 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_1', {})
2933
2934 call term_sendkeys(buf, "/another\<CR>\<C-W>}")
2935 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_2', {})
2936
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002937 call term_sendkeys(buf, ":call popup_move(popup_findpreview(), #{col: 15})\<CR>")
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002938 call term_sendkeys(buf, ":\<CR>")
2939 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_3', {})
2940
2941 call term_sendkeys(buf, "/another\<CR>\<C-W>}")
2942 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_4', {})
2943
Bram Moolenaar799439a2020-02-11 21:44:17 +01002944 call term_sendkeys(buf, ":silent cd ..\<CR>:\<CR>")
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002945 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_5', {})
Bram Moolenaar799439a2020-02-11 21:44:17 +01002946 call term_sendkeys(buf, ":silent cd testdir\<CR>")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002947
2948 call term_sendkeys(buf, ":pclose\<CR>")
Bram Moolenaar78d629a2019-08-16 17:31:15 +02002949 call term_sendkeys(buf, ":\<BS>")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002950 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_6', {})
2951
2952 call term_sendkeys(buf, ":pedit +/theword Xtagfile\<CR>")
2953 call term_sendkeys(buf, ":\<CR>")
2954 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_7', {})
2955
2956 call term_sendkeys(buf, ":pclose\<CR>")
2957 call term_sendkeys(buf, ":psearch searched\<CR>")
2958 call term_sendkeys(buf, ":\<CR>")
2959 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_8', {})
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002960
Bram Moolenaar8bf716c2020-01-23 15:33:54 +01002961 call term_sendkeys(buf, "\<C-W>p")
2962 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_9', {})
2963
2964 call term_sendkeys(buf, ":call win_execute(popup_findpreview(), 'call popup_clear()')\<CR>")
2965 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_10', {})
2966
Bram Moolenaarf914a332019-07-20 15:09:56 +02002967 call StopVimInTerminal(buf)
2968 call delete('Xtags')
2969 call delete('Xtagfile')
2970 call delete('XtestPreviewPopup')
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002971 call delete('Xheader.h')
Bram Moolenaarf914a332019-07-20 15:09:56 +02002972endfunc
2973
Bram Moolenaarbd483b32019-08-21 15:13:41 +02002974func Get_popupmenu_lines()
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002975 let lines =<< trim END
2976 set completeopt+=preview,popup
2977 set completefunc=CompleteFuncDict
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02002978 hi InfoPopup ctermbg=yellow
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002979
2980 func CompleteFuncDict(findstart, base)
2981 if a:findstart
2982 if col('.') > 10
2983 return col('.') - 10
2984 endif
2985 return 0
2986 endif
2987
2988 return {
2989 \ 'words': [
2990 \ {
2991 \ 'word': 'aword',
2992 \ 'abbr': 'wrd',
2993 \ 'menu': 'extra text',
2994 \ 'info': 'words are cool',
2995 \ 'kind': 'W',
2996 \ 'user_data': 'test'
2997 \ },
2998 \ {
2999 \ 'word': 'anotherword',
3000 \ 'abbr': 'anotwrd',
3001 \ 'menu': 'extra text',
3002 \ 'info': "other words are\ncooler than this and some more text\nto make wrap",
3003 \ 'kind': 'W',
3004 \ 'user_data': 'notest'
3005 \ },
3006 \ {
3007 \ 'word': 'noinfo',
3008 \ 'abbr': 'noawrd',
3009 \ 'menu': 'extra text',
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02003010 \ 'info': "lets\nshow\na\nscrollbar\nhere",
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003011 \ 'kind': 'W',
3012 \ 'user_data': 'notest'
3013 \ },
3014 \ {
3015 \ 'word': 'thatword',
3016 \ 'abbr': 'thatwrd',
3017 \ 'menu': 'extra text',
3018 \ 'info': 'that word is cool',
3019 \ 'kind': 'W',
3020 \ 'user_data': 'notest'
3021 \ },
3022 \ ]
3023 \ }
3024 endfunc
3025 call setline(1, 'text text text text text text text ')
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003026 func ChangeColor()
3027 let id = popup_findinfo()
Bram Moolenaar6a124e62019-09-04 18:15:19 +02003028 eval id->popup_setoptions(#{highlight: 'InfoPopup'})
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003029 endfunc
Bram Moolenaardca7abe2019-10-20 18:17:57 +02003030
3031 func InfoHidden()
3032 set completepopup=height:4,border:off,align:menu
3033 set completeopt-=popup completeopt+=popuphidden
3034 au CompleteChanged * call HandleChange()
3035 endfunc
3036
3037 let s:counter = 0
3038 func HandleChange()
3039 let s:counter += 1
3040 let selected = complete_info(['selected']).selected
3041 if selected <= 0
3042 " First time: do nothing, info remains hidden
3043 return
3044 endif
3045 if selected == 1
3046 " Second time: show info right away
3047 let id = popup_findinfo()
3048 if id
3049 call popup_settext(id, 'immediate info ' .. s:counter)
3050 call popup_show(id)
3051 endif
3052 else
3053 " Third time: show info after a short delay
3054 call timer_start(100, 'ShowInfo')
3055 endif
3056 endfunc
3057
3058 func ShowInfo(...)
3059 let id = popup_findinfo()
3060 if id
3061 call popup_settext(id, 'async info ' .. s:counter)
3062 call popup_show(id)
3063 endif
3064 endfunc
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003065 END
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003066 return lines
3067endfunc
3068
3069func Test_popupmenu_info_border()
3070 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003071 CheckFeature quickfix
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003072
3073 let lines = Get_popupmenu_lines()
3074 call add(lines, 'set completepopup=height:4,highlight:InfoPopup')
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003075 call writefile(lines, 'XtestInfoPopup')
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003076
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003077 let buf = RunVimInTerminal('-S XtestInfoPopup', #{rows: 14})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003078 call TermWait(buf, 25)
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003079
3080 call term_sendkeys(buf, "A\<C-X>\<C-U>")
3081 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_1', {})
3082
3083 call term_sendkeys(buf, "\<C-N>")
3084 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_2', {})
3085
3086 call term_sendkeys(buf, "\<C-N>")
3087 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_3', {})
3088
3089 call term_sendkeys(buf, "\<C-N>\<C-N>")
3090 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_4', {})
3091
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02003092 " info on the left with scrollbar
3093 call term_sendkeys(buf, "test text test text\<C-X>\<C-U>")
3094 call term_sendkeys(buf, "\<C-N>\<C-N>")
3095 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_5', {})
3096
Bram Moolenaar202c3f72019-11-21 12:12:35 +01003097 " Test that the popupmenu's scrollbar and infopopup do not overlap
3098 call term_sendkeys(buf, "\<Esc>")
3099 call term_sendkeys(buf, ":set pumheight=3\<CR>")
3100 call term_sendkeys(buf, "cc\<C-X>\<C-U>")
3101 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_6', {})
3102
Bram Moolenaarca7c0782020-01-14 20:42:48 +01003103 " Hide the info popup, cycle trough buffers, make sure it didn't get
3104 " deleted.
3105 call term_sendkeys(buf, "\<Esc>")
3106 call term_sendkeys(buf, ":set hidden\<CR>")
3107 call term_sendkeys(buf, ":bn\<CR>")
3108 call term_sendkeys(buf, ":bn\<CR>")
3109 call term_sendkeys(buf, "otest text test text\<C-X>\<C-U>")
3110 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_7', {})
3111
Bram Moolenaar447bfba2020-07-18 16:07:16 +02003112 " Test that when the option is changed the popup changes.
3113 call term_sendkeys(buf, "\<Esc>")
3114 call term_sendkeys(buf, ":set completepopup=border:off\<CR>")
3115 call term_sendkeys(buf, "a\<C-X>\<C-U>")
3116 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_8', {})
3117
Bram Moolenaarca7c0782020-01-14 20:42:48 +01003118 call term_sendkeys(buf, "\<Esc>")
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003119 call StopVimInTerminal(buf)
3120 call delete('XtestInfoPopup')
3121endfunc
3122
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003123func Test_popupmenu_info_noborder()
3124 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003125 CheckFeature quickfix
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003126
3127 let lines = Get_popupmenu_lines()
3128 call add(lines, 'set completepopup=height:4,border:off')
3129 call writefile(lines, 'XtestInfoPopupNb')
3130
3131 let buf = RunVimInTerminal('-S XtestInfoPopupNb', #{rows: 14})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003132 call TermWait(buf, 25)
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003133
3134 call term_sendkeys(buf, "A\<C-X>\<C-U>")
3135 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_nb_1', {})
3136
3137 call StopVimInTerminal(buf)
3138 call delete('XtestInfoPopupNb')
3139endfunc
3140
Bram Moolenaar258cef52019-08-21 17:29:29 +02003141func Test_popupmenu_info_align_menu()
3142 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003143 CheckFeature quickfix
Bram Moolenaar258cef52019-08-21 17:29:29 +02003144
3145 let lines = Get_popupmenu_lines()
3146 call add(lines, 'set completepopup=height:4,border:off,align:menu')
3147 call writefile(lines, 'XtestInfoPopupNb')
3148
3149 let buf = RunVimInTerminal('-S XtestInfoPopupNb', #{rows: 14})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003150 call TermWait(buf, 25)
Bram Moolenaar258cef52019-08-21 17:29:29 +02003151
3152 call term_sendkeys(buf, "A\<C-X>\<C-U>")
3153 call term_sendkeys(buf, "\<C-N>")
3154 call term_sendkeys(buf, "\<C-N>")
3155 call term_sendkeys(buf, "\<C-N>")
3156 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_1', {})
3157
3158 call term_sendkeys(buf, "test text test text test\<C-X>\<C-U>")
3159 call term_sendkeys(buf, "\<C-N>")
3160 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_2', {})
3161
3162 call term_sendkeys(buf, "\<Esc>")
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003163 call term_sendkeys(buf, ":call ChangeColor()\<CR>")
Bram Moolenaar258cef52019-08-21 17:29:29 +02003164 call term_sendkeys(buf, ":call setline(2, ['x']->repeat(10))\<CR>")
3165 call term_sendkeys(buf, "Gotest text test text\<C-X>\<C-U>")
3166 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_3', {})
3167
3168 call StopVimInTerminal(buf)
3169 call delete('XtestInfoPopupNb')
3170endfunc
3171
Bram Moolenaardca7abe2019-10-20 18:17:57 +02003172func Test_popupmenu_info_hidden()
3173 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003174 CheckFeature quickfix
Bram Moolenaardca7abe2019-10-20 18:17:57 +02003175
3176 let lines = Get_popupmenu_lines()
3177 call add(lines, 'call InfoHidden()')
3178 call writefile(lines, 'XtestInfoPopupHidden')
3179
3180 let buf = RunVimInTerminal('-S XtestInfoPopupHidden', #{rows: 14})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003181 call TermWait(buf, 25)
Bram Moolenaardca7abe2019-10-20 18:17:57 +02003182
3183 call term_sendkeys(buf, "A\<C-X>\<C-U>")
3184 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_hidden_1', {})
3185
3186 call term_sendkeys(buf, "\<C-N>")
3187 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_hidden_2', {})
3188
3189 call term_sendkeys(buf, "\<C-N>")
3190 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_hidden_3', {})
3191
3192 call term_sendkeys(buf, "\<Esc>")
3193 call StopVimInTerminal(buf)
3194 call delete('XtestInfoPopupHidden')
3195endfunc
3196
Bram Moolenaarbef93ac2019-12-06 20:17:35 +01003197func Test_popupmenu_info_too_wide()
3198 CheckScreendump
3199 CheckFeature quickfix
3200
3201 let lines =<< trim END
3202 call setline(1, range(10))
3203
3204 set completeopt+=preview,popup
3205 set completepopup=align:menu
3206 set omnifunc=OmniFunc
3207 hi InfoPopup ctermbg=lightgrey
3208
3209 func OmniFunc(findstart, base)
3210 if a:findstart
3211 return 0
3212 endif
3213
3214 let menuText = 'some long text to make sure the menu takes up all of the width of the window'
3215 return #{
3216 \ words: [
3217 \ #{
3218 \ word: 'scrap',
3219 \ menu: menuText,
3220 \ info: "other words are\ncooler than this and some more text\nto make wrap",
3221 \ },
3222 \ #{
3223 \ word: 'scappier',
3224 \ menu: menuText,
3225 \ info: 'words are cool',
3226 \ },
3227 \ #{
3228 \ word: 'scrappier2',
3229 \ menu: menuText,
3230 \ info: 'words are cool',
3231 \ },
3232 \ ]
3233 \ }
3234 endfunc
3235 END
3236
3237 call writefile(lines, 'XtestInfoPopupWide')
3238 let buf = RunVimInTerminal('-S XtestInfoPopupWide', #{rows: 8})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003239 call TermWait(buf, 25)
Bram Moolenaarbef93ac2019-12-06 20:17:35 +01003240
3241 call term_sendkeys(buf, "Ascr\<C-X>\<C-O>")
3242 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_wide_1', {})
3243
3244 call term_sendkeys(buf, "\<Esc>")
3245 call StopVimInTerminal(buf)
3246 call delete('XtestInfoPopupWide')
3247endfunc
3248
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02003249func Test_popupwin_recycle_bnr()
Bram Moolenaare49fbff2019-08-21 22:50:07 +02003250 let bufnr = popup_notification('nothing wrong', {})->winbufnr()
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02003251 call popup_clear()
Bram Moolenaar6a124e62019-09-04 18:15:19 +02003252 let winid = 'nothing wrong'->popup_notification({})
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02003253 call assert_equal(bufnr, winbufnr(winid))
3254 call popup_clear()
3255endfunc
3256
Bram Moolenaar1824f452019-10-02 23:06:46 +02003257func Test_popupwin_getoptions_tablocal()
3258 topleft split
3259 let win1 = popup_create('nothing', #{maxheight: 8})
3260 let win2 = popup_create('something', #{maxheight: 10})
3261 let win3 = popup_create('something', #{maxheight: 15})
3262 call assert_equal(8, popup_getoptions(win1).maxheight)
3263 call assert_equal(10, popup_getoptions(win2).maxheight)
3264 call assert_equal(15, popup_getoptions(win3).maxheight)
3265 call popup_clear()
3266 quit
3267endfunc
3268
Bram Moolenaare8a7dfe2019-10-03 22:35:52 +02003269func Test_popupwin_cancel()
3270 let win1 = popup_create('one', #{line: 5, filter: {... -> 0}})
3271 let win2 = popup_create('two', #{line: 10, filter: {... -> 0}})
3272 let win3 = popup_create('three', #{line: 15, filter: {... -> 0}})
3273 call assert_equal(5, popup_getpos(win1).line)
3274 call assert_equal(10, popup_getpos(win2).line)
3275 call assert_equal(15, popup_getpos(win3).line)
3276 " TODO: this also works without patch 8.1.2110
3277 call feedkeys("\<C-C>", 'xt')
3278 call assert_equal(5, popup_getpos(win1).line)
3279 call assert_equal(10, popup_getpos(win2).line)
3280 call assert_equal({}, popup_getpos(win3))
3281 call feedkeys("\<C-C>", 'xt')
3282 call assert_equal(5, popup_getpos(win1).line)
3283 call assert_equal({}, popup_getpos(win2))
3284 call assert_equal({}, popup_getpos(win3))
3285 call feedkeys("\<C-C>", 'xt')
3286 call assert_equal({}, popup_getpos(win1))
3287 call assert_equal({}, popup_getpos(win2))
3288 call assert_equal({}, popup_getpos(win3))
3289endfunc
3290
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003291func Test_popupwin_filter_redraw()
3292 " Create two popups with a filter that closes the popup when typing "0".
3293 " Both popups should close, even though the redraw also calls
3294 " popup_reset_handled()
3295
3296 func CloseFilter(winid, key)
3297 if a:key == '0'
3298 call popup_close(a:winid)
3299 redraw
3300 endif
3301 return 0 " pass the key
3302 endfunc
3303
3304 let id1 = popup_create('first one', #{
3305 \ line: 1,
3306 \ col: 1,
3307 \ filter: 'CloseFilter',
3308 \ })
3309 let id2 = popup_create('second one', #{
3310 \ line: 9,
3311 \ col: 1,
3312 \ filter: 'CloseFilter',
3313 \ })
3314 call assert_equal(1, popup_getpos(id1).line)
3315 call assert_equal(9, popup_getpos(id2).line)
3316
3317 call feedkeys('0', 'xt')
3318 call assert_equal({}, popup_getpos(id1))
3319 call assert_equal({}, popup_getpos(id2))
3320
3321 call popup_clear()
3322 delfunc CloseFilter
3323endfunc
3324
Bram Moolenaar20ebbea2019-11-30 17:58:27 +01003325func Test_popupwin_double_width()
3326 CheckScreendump
3327
3328 let lines =<< trim END
3329 call setline(1, 'x你好世界你好世你好世界你好')
3330 call setline(2, '你好世界你好世你好世界你好')
3331 call setline(3, 'x你好世界你好世你好世界你好')
3332 call popup_create('你好,世界 - 你好,世界xxxxx', #{line: 1, col: 3, maxwidth: 14})
3333 END
3334 call writefile(lines, 'XtestPopupWide')
3335
3336 let buf = RunVimInTerminal('-S XtestPopupWide', #{rows: 10})
3337 call VerifyScreenDump(buf, 'Test_popupwin_doublewidth_1', {})
3338
3339 call StopVimInTerminal(buf)
3340 call delete('XtestPopupWide')
3341endfunc
3342
3343func Test_popupwin_sign()
3344 CheckScreendump
3345
3346 let lines =<< trim END
3347 call setline(1, range(10))
3348 call sign_define('Current', {
3349 \ 'text': '>>',
3350 \ 'texthl': 'WarningMsg',
3351 \ 'linehl': 'Error',
3352 \ })
3353 call sign_define('Other', {
3354 \ 'text': '#!',
3355 \ 'texthl': 'Error',
3356 \ 'linehl': 'Search',
3357 \ })
3358 let winid = popup_create(['hello', 'bright', 'world'], {
3359 \ 'minwidth': 20,
3360 \ })
3361 call setwinvar(winid, "&signcolumn", "yes")
3362 let winbufnr = winbufnr(winid)
3363
3364 " add sign to current buffer, shows
3365 call sign_place(1, 'Selected', 'Current', bufnr('%'), {'lnum': 1})
3366 " add sign to current buffer, does not show
3367 call sign_place(2, 'PopUpSelected', 'Other', bufnr('%'), {'lnum': 2})
3368
3369 " add sign to popup buffer, shows
3370 call sign_place(3, 'PopUpSelected', 'Other', winbufnr, {'lnum': 1})
3371 " add sign to popup buffer, does not show
3372 call sign_place(4, 'Selected', 'Current', winbufnr, {'lnum': 2})
3373 END
3374 call writefile(lines, 'XtestPopupSign')
3375
3376 let buf = RunVimInTerminal('-S XtestPopupSign', #{rows: 10})
3377 call VerifyScreenDump(buf, 'Test_popupwin_sign_1', {})
3378
3379 call StopVimInTerminal(buf)
3380 call delete('XtestPopupSign')
3381endfunc
3382
Bram Moolenaar99ebf222019-12-10 23:44:48 +01003383func Test_popupwin_bufnr()
3384 let popwin = popup_create(['blah'], #{})
3385 let popbuf = winbufnr(popwin)
3386 split asdfasdf
3387 let newbuf = bufnr()
3388 call assert_true(newbuf > popbuf, 'New buffer number is higher')
3389 call assert_equal(newbuf, bufnr('$'))
3390 call popup_clear()
3391 let popwin = popup_create(['blah'], #{})
3392 " reuses previous buffer number
3393 call assert_equal(popbuf, winbufnr(popwin))
3394 call assert_equal(newbuf, bufnr('$'))
3395
3396 call popup_clear()
3397 bwipe!
3398endfunc
3399
Bram Moolenaarec084d32020-02-28 22:44:47 +01003400func Test_popupwin_filter_input_multibyte()
3401 func MyPopupFilter(winid, c)
3402 let g:bytes = range(a:c->strlen())->map({i -> char2nr(a:c[i])})
3403 return 0
3404 endfunc
3405 let winid = popup_create('', #{mapping: 0, filter: 'MyPopupFilter'})
3406
3407 " UTF-8: E3 80 80, including K_SPECIAL(0x80)
3408 call feedkeys("\u3000", 'xt')
3409 call assert_equal([0xe3, 0x80, 0x80], g:bytes)
3410
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01003411 " UTF-8: E3 80 9B, including CSI(0x9B)
3412 call feedkeys("\u301b", 'xt')
3413 call assert_equal([0xe3, 0x80, 0x9b], g:bytes)
Bram Moolenaarec084d32020-02-28 22:44:47 +01003414
Bram Moolenaar20298ce2020-06-19 21:46:52 +02003415 if has('unix')
3416 " with modifyOtherKeys <M-S-a> does not include a modifier sequence
Bram Moolenaarb326edf2020-06-20 15:03:38 +02003417 if has('gui_running')
3418 call feedkeys("\x9b\xfc\x08A", 'Lx!')
3419 else
3420 call feedkeys("\<Esc>[27;4;65~", 'Lx!')
3421 endif
Bram Moolenaar20298ce2020-06-19 21:46:52 +02003422 call assert_equal([0xc3, 0x81], g:bytes)
3423 endif
3424
Bram Moolenaarec084d32020-02-28 22:44:47 +01003425 call popup_clear()
3426 delfunc MyPopupFilter
3427 unlet g:bytes
3428endfunc
3429
Bram Moolenaarba2920f2020-03-06 21:43:17 +01003430func Test_popupwin_atcursor_far_right()
3431 new
3432
3433 " this was getting stuck
3434 set signcolumn=yes
3435 call setline(1, repeat('=', &columns))
3436 normal! ggg$
Bram Moolenaaref6b9792020-05-13 16:34:15 +02003437 let winid = popup_atcursor(repeat('x', 500), #{moved: 'any', border: []})
Bram Moolenaarba2920f2020-03-06 21:43:17 +01003438
Bram Moolenaaref6b9792020-05-13 16:34:15 +02003439 call popup_close(winid)
Bram Moolenaarba2920f2020-03-06 21:43:17 +01003440 bwipe!
3441 set signcolumn&
3442endfunc
3443
Bram Moolenaar0f1563f2020-03-20 21:15:51 +01003444func Test_popupwin_splitmove()
3445 vsplit
3446 let win2 = win_getid()
3447 let popup_winid = popup_dialog('hello', {})
3448 call assert_fails('call win_splitmove(popup_winid, win2)', 'E957:')
3449 call assert_fails('call win_splitmove(win2, popup_winid)', 'E957:')
3450
3451 call popup_clear()
3452 bwipe
3453endfunc
3454
3455
Bram Moolenaar331bafd2019-07-20 17:46:05 +02003456" vim: shiftwidth=2 sts=2