blob: 8631c71e7f4acdbe8a8163bc2c4823e671f14553 [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 Moolenaar2e62b562019-06-30 18:07:00 +0200580func Test_popup_close_with_mouse()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200581 CheckScreendump
582
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200583 let lines =<< trim END
584 call setline(1, range(1, 20))
585 " With border, can click on X
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200586 let winid = popup_create('foobar', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200587 \ close: 'button',
588 \ border: [],
589 \ line: 1,
590 \ col: 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200591 \ })
592 func CloseMsg(id, result)
593 echomsg 'Popup closed with ' .. a:result
594 endfunc
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200595 let winid = popup_create('notification', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200596 \ close: 'click',
597 \ line: 3,
598 \ col: 15,
599 \ callback: 'CloseMsg',
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200600 \ })
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200601 let winid = popup_create('no border here', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200602 \ close: 'button',
603 \ line: 5,
604 \ col: 3,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200605 \ })
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200606 let winid = popup_create('only padding', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200607 \ close: 'button',
608 \ padding: [],
609 \ line: 5,
610 \ col: 23,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200611 \ })
612 func CloseWithX()
613 call feedkeys("\<F3>\<LeftMouse>\<LeftRelease>", "xt")
614 endfunc
615 map <silent> <F3> :call test_setmouse(1, len('foobar') + 2)<CR>
616 func CloseWithClick()
617 call feedkeys("\<F4>\<LeftMouse>\<LeftRelease>", "xt")
618 endfunc
619 map <silent> <F4> :call test_setmouse(3, 17)<CR>
Bram Moolenaarf6396232019-08-24 19:36:00 +0200620 func CreateWithMenuFilter()
621 let winid = popup_create('barfoo', #{
622 \ close: 'button',
623 \ filter: 'popup_filter_menu',
624 \ border: [],
625 \ line: 1,
626 \ col: 40,
627 \ })
628 endfunc
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200629 END
630 call writefile(lines, 'XtestPopupClose')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200631 let buf = RunVimInTerminal('-S XtestPopupClose', #{rows: 10})
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200632 call VerifyScreenDump(buf, 'Test_popupwin_close_01', {})
633
634 call term_sendkeys(buf, ":call CloseWithX()\<CR>")
635 call VerifyScreenDump(buf, 'Test_popupwin_close_02', {})
636
637 call term_sendkeys(buf, ":call CloseWithClick()\<CR>")
638 call VerifyScreenDump(buf, 'Test_popupwin_close_03', {})
639
Bram Moolenaarf6396232019-08-24 19:36:00 +0200640 call term_sendkeys(buf, ":call CreateWithMenuFilter()\<CR>")
641 call VerifyScreenDump(buf, 'Test_popupwin_close_04', {})
642
643 " We have to send the actual mouse code, feedkeys() would be caught the
644 " filter.
645 call term_sendkeys(buf, "\<Esc>[<0;47;1M")
646 call VerifyScreenDump(buf, 'Test_popupwin_close_05', {})
647
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200648 " clean up
649 call StopVimInTerminal(buf)
650 call delete('XtestPopupClose')
651endfunction
652
Bram Moolenaar7b3d9392019-10-16 22:17:07 +0200653func Test_popup_menu_wrap()
654 CheckScreendump
655
656 let lines =<< trim END
657 call setline(1, range(1, 20))
658 call popup_create([
659 \ 'one',
660 \ 'asdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfas',
661 \ 'three',
662 \ 'four',
663 \ ], #{
664 \ pos: "botleft",
665 \ border: [],
666 \ padding: [0,1,0,1],
667 \ maxheight: 3,
668 \ cursorline: 1,
669 \ filter: 'popup_filter_menu',
670 \ })
671 END
672 call writefile(lines, 'XtestPopupWrap')
673 let buf = RunVimInTerminal('-S XtestPopupWrap', #{rows: 10})
674 call VerifyScreenDump(buf, 'Test_popupwin_wrap_1', {})
675
676 call term_sendkeys(buf, "jj")
677 call VerifyScreenDump(buf, 'Test_popupwin_wrap_2', {})
678
679 " clean up
680 call term_sendkeys(buf, "\<Esc>")
681 call StopVimInTerminal(buf)
682 call delete('XtestPopupWrap')
683endfunction
684
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200685func Test_popup_with_mask()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200686 CheckScreendump
687
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200688 let lines =<< trim END
Bram Moolenaarba45f1f2019-07-03 22:50:41 +0200689 call setline(1, repeat([join(range(1, 42), '')], 13))
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200690 hi PopupColor ctermbg=lightgrey
691 let winid = popup_create([
692 \ 'some text',
693 \ 'another line',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200694 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200695 \ line: 1,
696 \ col: 10,
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100697 \ posinvert: 0,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200698 \ wrap: 0,
699 \ fixed: 1,
700 \ zindex: 90,
701 \ padding: [],
702 \ highlight: 'PopupColor',
703 \ mask: [[1,1,1,1], [-5,-1,4,4], [7,9,2,3], [2,4,3,3]]})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200704 call popup_create([
705 \ 'xxxxxxxxx',
706 \ 'yyyyyyyyy',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200707 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200708 \ line: 3,
709 \ col: 18,
710 \ zindex: 20})
Bram Moolenaarba45f1f2019-07-03 22:50:41 +0200711 let winidb = popup_create([
712 \ 'just one line',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200713 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200714 \ line: 7,
715 \ col: 10,
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100716 \ posinvert: 0,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200717 \ wrap: 0,
718 \ fixed: 1,
719 \ close: 'button',
720 \ zindex: 90,
721 \ padding: [],
722 \ border: [],
723 \ 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 +0200724 END
725 call writefile(lines, 'XtestPopupMask')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200726 let buf = RunVimInTerminal('-S XtestPopupMask', #{rows: 13})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200727 call VerifyScreenDump(buf, 'Test_popupwin_mask_1', {})
728
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200729 call term_sendkeys(buf, ":call popup_move(winid, #{col: 11, line: 2})\<CR>")
730 call term_sendkeys(buf, ":call popup_move(winidb, #{col: 12})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200731 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200732 call VerifyScreenDump(buf, 'Test_popupwin_mask_2', {})
733
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200734 call term_sendkeys(buf, ":call popup_move(winid, #{col: 65, line: 2})\<CR>")
735 call term_sendkeys(buf, ":call popup_move(winidb, #{col: 63})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200736 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaard529ba52019-07-02 23:13:53 +0200737 call VerifyScreenDump(buf, 'Test_popupwin_mask_3', {})
738
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200739 call term_sendkeys(buf, ":call popup_move(winid, #{pos: 'topright', col: 12, line: 2})\<CR>")
740 call term_sendkeys(buf, ":call popup_move(winidb, #{pos: 'topright', col: 12})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200741 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaard529ba52019-07-02 23:13:53 +0200742 call VerifyScreenDump(buf, 'Test_popupwin_mask_4', {})
743
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200744 call term_sendkeys(buf, ":call popup_move(winid, #{pos: 'topright', col: 12, line: 11})\<CR>")
745 call term_sendkeys(buf, ":call popup_move(winidb, #{pos: 'topleft', col: 42, line: 11})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200746 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaarb4207472019-07-12 16:05:45 +0200747 call VerifyScreenDump(buf, 'Test_popupwin_mask_5', {})
748
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200749 " clean up
750 call StopVimInTerminal(buf)
751 call delete('XtestPopupMask')
752endfunc
753
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200754func Test_popup_select()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200755 CheckScreendump
756 CheckFeature clipboard_working
757
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200758 " create a popup with some text to be selected
759 let lines =<< trim END
Bram Moolenaar1755ec42019-06-15 13:13:54 +0200760 set clipboard=autoselect
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200761 call setline(1, range(1, 20))
Bram Moolenaar4dd751b2019-08-17 19:10:53 +0200762 let winid = popup_create(['the word', 'some more', 'several words here', 'invisible', '5', '6', '7'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200763 \ drag: 1,
764 \ border: [],
765 \ line: 3,
766 \ col: 10,
Bram Moolenaar4dd751b2019-08-17 19:10:53 +0200767 \ maxheight: 3,
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200768 \ })
769 func Select1()
770 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
771 endfunc
772 map <silent> <F3> :call test_setmouse(4, 15)<CR>
773 map <silent> <F4> :call test_setmouse(6, 23)<CR>
774 END
775 call writefile(lines, 'XtestPopupSelect')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200776 let buf = RunVimInTerminal('-S XtestPopupSelect', #{rows: 10})
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200777 call term_sendkeys(buf, ":call Select1()\<CR>")
778 call VerifyScreenDump(buf, 'Test_popupwin_select_01', {})
779
780 call term_sendkeys(buf, ":call popup_close(winid)\<CR>")
781 call term_sendkeys(buf, "\"*p")
Bram Moolenaar8ccabf62019-07-12 18:12:51 +0200782 " clean the command line, sometimes it still shows a command
783 call term_sendkeys(buf, ":\<esc>")
784
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200785 call VerifyScreenDump(buf, 'Test_popupwin_select_02', {})
786
787 " clean up
788 call StopVimInTerminal(buf)
789 call delete('XtestPopupSelect')
790endfunc
791
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200792func Test_popup_in_tab()
793 " default popup is local to tab, not visible when in other tab
794 let winid = popup_create("text", {})
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200795 let bufnr = winbufnr(winid)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200796 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200797 call assert_equal(0, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200798 tabnew
799 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200800 call assert_equal(1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200801 quit
802 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200803
804 call assert_equal(1, bufexists(bufnr))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200805 call popup_clear()
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200806 " buffer is gone now
807 call assert_equal(0, bufexists(bufnr))
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200808
809 " global popup is visible in any tab
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200810 let winid = popup_create("text", #{tabpage: -1})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200811 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200812 call assert_equal(-1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200813 tabnew
814 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200815 call assert_equal(-1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200816 quit
817 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200818 call popup_clear()
Bram Moolenaara3fce622019-06-20 02:31:49 +0200819
820 " create popup in other tab
821 tabnew
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200822 let winid = popup_create("text", #{tabpage: 1})
Bram Moolenaara3fce622019-06-20 02:31:49 +0200823 call assert_equal(0, popup_getpos(winid).visible)
824 call assert_equal(1, popup_getoptions(winid).tabpage)
825 quit
826 call assert_equal(1, popup_getpos(winid).visible)
827 call assert_equal(0, popup_getoptions(winid).tabpage)
828 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200829endfunc
830
831func Test_popup_valid_arguments()
Bram Moolenaaref6b9792020-05-13 16:34:15 +0200832 call assert_equal(0, len(popup_list()))
833
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200834 " Zero value is like the property wasn't there
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200835 let winid = popup_create("text", #{col: 0})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200836 let pos = popup_getpos(winid)
837 call assert_inrange(&columns / 2 - 1, &columns / 2 + 1, pos.col)
Bram Moolenaaref6b9792020-05-13 16:34:15 +0200838 call assert_equal([winid], popup_list())
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200839 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200840
841 " using cursor column has minimum value of 1
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200842 let winid = popup_create("text", #{col: 'cursor-100'})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200843 let pos = popup_getpos(winid)
844 call assert_equal(1, pos.col)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200845 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200846
847 " center
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200848 let winid = popup_create("text", #{pos: 'center'})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200849 let pos = popup_getpos(winid)
850 let around = (&columns - pos.width) / 2
851 call assert_inrange(around - 1, around + 1, pos.col)
852 let around = (&lines - pos.height) / 2
853 call assert_inrange(around - 1, around + 1, pos.line)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200854 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200855endfunc
856
857func Test_popup_invalid_arguments()
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +0200858 call assert_fails('call popup_create(666, {})', 'E86:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200859 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200860 call assert_fails('call popup_create("text", "none")', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200861 call popup_clear()
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200862 call assert_fails('call popup_create(test_null_string(), {})', 'E450:')
863 call assert_fails('call popup_create(test_null_list(), {})', 'E450:')
864 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200865
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200866 call assert_fails('call popup_create("text", #{col: "xxx"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200867 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200868 call assert_fails('call popup_create("text", #{col: "cursor8"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200869 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200870 call assert_fails('call popup_create("text", #{col: "cursor+x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200871 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200872 call assert_fails('call popup_create("text", #{col: "cursor+8x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200873 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200874
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200875 call assert_fails('call popup_create("text", #{line: "xxx"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200876 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200877 call assert_fails('call popup_create("text", #{line: "cursor8"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200878 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200879 call assert_fails('call popup_create("text", #{line: "cursor+x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200880 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200881 call assert_fails('call popup_create("text", #{line: "cursor+8x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200882 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200883
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200884 call assert_fails('call popup_create("text", #{pos: "there"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200885 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200886 call assert_fails('call popup_create("text", #{padding: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200887 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200888 call assert_fails('call popup_create("text", #{border: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200889 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200890 call assert_fails('call popup_create("text", #{borderhighlight: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200891 call popup_clear()
Bram Moolenaar403d0902019-07-17 21:37:32 +0200892 call assert_fails('call popup_create("text", #{borderhighlight: test_null_list()})', 'E714:')
893 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200894 call assert_fails('call popup_create("text", #{borderchars: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200895 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200896
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200897 call assert_fails('call popup_create([#{text: "text"}, 666], {})', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200898 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200899 call assert_fails('call popup_create([#{text: "text", props: "none"}], {})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200900 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200901 call assert_fails('call popup_create([#{text: "text", props: ["none"]}], {})', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200902 call popup_clear()
Bram Moolenaarb0992022020-01-30 14:55:42 +0100903 call assert_fails('call popup_create([#{text: "text", props: range(3)}], {})', 'E715:')
904 call popup_clear()
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200905 call assert_fails('call popup_create("text", #{mask: ["asdf"]})', 'E475:')
906 call popup_clear()
Bram Moolenaarb0992022020-01-30 14:55:42 +0100907 call assert_fails('call popup_create("text", #{mask: range(5)})', 'E475:')
908 call popup_clear()
909 call popup_create("text", #{mask: [range(4)]})
910 call popup_clear()
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200911 call assert_fails('call popup_create("text", #{mask: test_null_list()})', 'E475:')
Bram Moolenaar749fa0a2019-08-03 16:18:07 +0200912 call assert_fails('call popup_create("text", #{mapping: []})', 'E745:')
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +0200913 call popup_clear()
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200914 call assert_fails('call popup_create("text", #{tabpage : 4})', 'E997:')
915 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200916endfunc
917
Bram Moolenaareea16992019-05-31 17:34:48 +0200918func Test_win_execute_closing_curwin()
919 split
920 let winid = popup_create('some text', {})
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200921 call assert_fails('call win_execute(winid, winnr() .. "close")', 'E994')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200922 call popup_clear()
Bram Moolenaar49540192019-12-11 19:34:54 +0100923
924 let winid = popup_create('some text', {})
925 call assert_fails('call win_execute(winid, printf("normal! :\<C-u>call popup_close(%d)\<CR>", winid))', 'E994')
926 call popup_clear()
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200927endfunc
928
929func Test_win_execute_not_allowed()
930 let winid = popup_create('some text', {})
931 call assert_fails('call win_execute(winid, "split")', 'E994:')
932 call assert_fails('call win_execute(winid, "vsplit")', 'E994:')
933 call assert_fails('call win_execute(winid, "close")', 'E994:')
934 call assert_fails('call win_execute(winid, "bdelete")', 'E994:')
Bram Moolenaar2d247842019-06-01 17:06:25 +0200935 call assert_fails('call win_execute(winid, "bwipe!")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200936 call assert_fails('call win_execute(winid, "tabnew")', 'E994:')
937 call assert_fails('call win_execute(winid, "tabnext")', 'E994:')
938 call assert_fails('call win_execute(winid, "next")', 'E994:')
939 call assert_fails('call win_execute(winid, "rewind")', 'E994:')
Bram Moolenaar3a2505c2020-03-09 16:40:41 +0100940 call assert_fails('call win_execute(winid, "pedit filename")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200941 call assert_fails('call win_execute(winid, "buf")', 'E994:')
Bram Moolenaar3029bcc2020-01-18 15:06:19 +0100942 call assert_fails('call win_execute(winid, "bnext")', 'E994:')
943 call assert_fails('call win_execute(winid, "bprev")', 'E994:')
944 call assert_fails('call win_execute(winid, "bfirst")', 'E994:')
945 call assert_fails('call win_execute(winid, "blast")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200946 call assert_fails('call win_execute(winid, "edit")', 'E994:')
947 call assert_fails('call win_execute(winid, "enew")', 'E994:')
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +0200948 call assert_fails('call win_execute(winid, "help")', 'E994:')
949 call assert_fails('call win_execute(winid, "1only")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200950 call assert_fails('call win_execute(winid, "wincmd x")', 'E994:')
951 call assert_fails('call win_execute(winid, "wincmd w")', 'E994:')
952 call assert_fails('call win_execute(winid, "wincmd t")', 'E994:')
953 call assert_fails('call win_execute(winid, "wincmd b")', 'E994:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200954 call popup_clear()
Bram Moolenaareea16992019-05-31 17:34:48 +0200955endfunc
956
Bram Moolenaar402502d2019-05-30 22:07:36 +0200957func Test_popup_with_wrap()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200958 CheckScreendump
959
Bram Moolenaar402502d2019-05-30 22:07:36 +0200960 let lines =<< trim END
961 call setline(1, range(1, 100))
962 let winid = popup_create(
963 \ 'a long line that wont fit',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200964 \ #{line: 3, col: 20, maxwidth: 10, wrap: 1})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200965 END
966 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200967 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200968 call VerifyScreenDump(buf, 'Test_popupwin_wrap', {})
969
970 " clean up
971 call StopVimInTerminal(buf)
972 call delete('XtestPopup')
973endfunc
974
975func Test_popup_without_wrap()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200976 CheckScreendump
977
Bram Moolenaar402502d2019-05-30 22:07:36 +0200978 let lines =<< trim END
979 call setline(1, range(1, 100))
980 let winid = popup_create(
981 \ 'a long line that wont fit',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200982 \ #{line: 3, col: 20, maxwidth: 10, wrap: 0})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200983 END
984 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200985 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200986 call VerifyScreenDump(buf, 'Test_popupwin_nowrap', {})
987
988 " clean up
989 call StopVimInTerminal(buf)
990 call delete('XtestPopup')
991endfunc
992
Bram Moolenaar331bafd2019-07-20 17:46:05 +0200993func Test_popup_with_showbreak()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200994 CheckScreendump
995
Bram Moolenaar331bafd2019-07-20 17:46:05 +0200996 let lines =<< trim END
997 set showbreak=>>\
998 call setline(1, range(1, 20))
999 let winid = popup_dialog(
Bram Moolenaar8ae54372019-09-15 18:11:16 +02001000 \ 'a long line here that wraps',
1001 \ #{filter: 'popup_filter_yesno',
1002 \ maxwidth: 12})
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001003 END
1004 call writefile(lines, 'XtestPopupShowbreak')
1005 let buf = RunVimInTerminal('-S XtestPopupShowbreak', #{rows: 10})
1006 call VerifyScreenDump(buf, 'Test_popupwin_showbreak', {})
1007
1008 " clean up
1009 call term_sendkeys(buf, "y")
1010 call StopVimInTerminal(buf)
1011 call delete('XtestPopupShowbreak')
1012endfunc
1013
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001014func Test_popup_time()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001015 CheckFeature timers
1016
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001017 topleft vnew
1018 call setline(1, 'hello')
1019
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001020 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001021 \ line: 1,
1022 \ col: 1,
1023 \ minwidth: 20,
1024 \ time: 500,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001025 \})
1026 redraw
1027 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1028 call assert_equal('world', line)
1029
Bram Moolenaarb4f06282019-07-12 21:07:54 +02001030 call assert_equal(winid, popup_locate(1, 1))
1031 call assert_equal(winid, popup_locate(1, 20))
1032 call assert_equal(0, popup_locate(1, 21))
1033 call assert_equal(0, popup_locate(2, 1))
1034
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001035 sleep 700m
Bram Moolenaar35d5af62019-05-26 20:44:10 +02001036 redraw
Bram Moolenaar196b4662019-09-06 21:34:30 +02001037 let line = join(map(range(1, 5), '1->screenstring(v:val)'), '')
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001038 call assert_equal('hello', line)
1039
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001040 call popup_create('on the command line', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001041 \ line: &lines,
1042 \ col: 10,
1043 \ minwidth: 20,
1044 \ time: 500,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001045 \})
1046 redraw
1047 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
1048 call assert_match('.*on the command line.*', line)
1049
1050 sleep 700m
1051 redraw
1052 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
1053 call assert_notmatch('.*on the command line.*', line)
1054
1055 bwipe!
1056endfunc
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001057
1058func Test_popup_hide()
1059 topleft vnew
1060 call setline(1, 'hello')
1061
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001062 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001063 \ line: 1,
1064 \ col: 1,
1065 \ minwidth: 20,
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001066 \})
1067 redraw
1068 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1069 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001070 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001071 " buffer is still listed and active
1072 call assert_match(winbufnr(winid) .. 'u a.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001073
1074 call popup_hide(winid)
1075 redraw
1076 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1077 call assert_equal('hello', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001078 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001079 " buffer is still listed but hidden
Bram Moolenaarca7c0782020-01-14 20:42:48 +01001080 call assert_match(winbufnr(winid) .. 'u a.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001081
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001082 eval winid->popup_show()
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001083 redraw
1084 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1085 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001086 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001087
1088
1089 call popup_close(winid)
1090 redraw
1091 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1092 call assert_equal('hello', line)
1093
1094 " error is given for existing non-popup window
1095 call assert_fails('call popup_hide(win_getid())', 'E993:')
1096
1097 " no error non-existing window
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001098 eval 1234234->popup_hide()
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001099 call popup_show(41234234)
1100
1101 bwipe!
1102endfunc
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001103
1104func Test_popup_move()
1105 topleft vnew
1106 call setline(1, 'hello')
1107
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001108 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001109 \ line: 1,
1110 \ col: 1,
1111 \ minwidth: 20,
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001112 \})
1113 redraw
1114 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
1115 call assert_equal('world ', line)
1116
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001117 call popup_move(winid, #{line: 2, col: 2})
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001118 redraw
1119 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
1120 call assert_equal('hello ', line)
1121 let line = join(map(range(1, 6), 'screenstring(2, v:val)'), '')
1122 call assert_equal('~world', line)
1123
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001124 eval winid->popup_move(#{line: 1})
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001125 redraw
1126 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
1127 call assert_equal('hworld', line)
1128
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02001129 call assert_fails('call popup_move(winid, [])', 'E715:')
1130 call assert_fails('call popup_move(winid, test_null_dict())', 'E715:')
1131
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001132 call popup_close(winid)
1133
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02001134 call assert_equal(0, popup_move(-1, {}))
1135
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001136 bwipe!
1137endfunc
Bram Moolenaarbc133542019-05-29 20:26:46 +02001138
Bram Moolenaar402502d2019-05-30 22:07:36 +02001139func Test_popup_getpos()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001140 let winid = popup_create('hello', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001141 \ line: 2,
1142 \ col: 3,
1143 \ minwidth: 10,
1144 \ minheight: 11,
Bram Moolenaarbc133542019-05-29 20:26:46 +02001145 \})
1146 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +02001147 let res = popup_getpos(winid)
Bram Moolenaarbc133542019-05-29 20:26:46 +02001148 call assert_equal(2, res.line)
1149 call assert_equal(3, res.col)
1150 call assert_equal(10, res.width)
1151 call assert_equal(11, res.height)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001152 call assert_equal(1, res.visible)
Bram Moolenaarbc133542019-05-29 20:26:46 +02001153
1154 call popup_close(winid)
1155endfunc
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001156
1157func Test_popup_width_longest()
1158 let tests = [
1159 \ [['hello', 'this', 'window', 'displays', 'all of its text'], 15],
1160 \ [['hello', 'this', 'window', 'all of its text', 'displays'], 15],
1161 \ [['hello', 'this', 'all of its text', 'window', 'displays'], 15],
1162 \ [['hello', 'all of its text', 'this', 'window', 'displays'], 15],
1163 \ [['all of its text', 'hello', 'this', 'window', 'displays'], 15],
1164 \ ]
1165
1166 for test in tests
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001167 let winid = popup_create(test[0], #{line: 2, col: 3})
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001168 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +02001169 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001170 call assert_equal(test[1], position.width)
1171 call popup_close(winid)
1172 endfor
1173endfunc
1174
1175func Test_popup_wraps()
1176 let tests = [
1177 \ ['nowrap', 6, 1],
1178 \ ['a line that wraps once', 12, 2],
1179 \ ['a line that wraps two times', 12, 3],
1180 \ ]
1181 for test in tests
1182 let winid = popup_create(test[0],
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001183 \ #{line: 2, col: 3, maxwidth: 12})
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001184 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +02001185 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001186 call assert_equal(test[1], position.width)
1187 call assert_equal(test[2], position.height)
1188
1189 call popup_close(winid)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001190 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001191 endfor
1192endfunc
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001193
1194func Test_popup_getoptions()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001195 let winid = popup_create('hello', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001196 \ line: 2,
1197 \ col: 3,
1198 \ minwidth: 10,
1199 \ minheight: 11,
1200 \ maxwidth: 20,
1201 \ maxheight: 21,
1202 \ zindex: 100,
1203 \ time: 5000,
1204 \ fixed: 1
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001205 \})
1206 redraw
1207 let res = popup_getoptions(winid)
1208 call assert_equal(2, res.line)
1209 call assert_equal(3, res.col)
1210 call assert_equal(10, res.minwidth)
1211 call assert_equal(11, res.minheight)
1212 call assert_equal(20, res.maxwidth)
1213 call assert_equal(21, res.maxheight)
1214 call assert_equal(100, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001215 call assert_equal(1, res.fixed)
Bram Moolenaarb8350ab2019-08-04 17:59:49 +02001216 call assert_equal(1, res.mapping)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001217 if has('timers')
1218 call assert_equal(5000, res.time)
1219 endif
1220 call popup_close(winid)
1221
1222 let winid = popup_create('hello', {})
1223 redraw
1224 let res = popup_getoptions(winid)
1225 call assert_equal(0, res.line)
1226 call assert_equal(0, res.col)
1227 call assert_equal(0, res.minwidth)
1228 call assert_equal(0, res.minheight)
1229 call assert_equal(0, res.maxwidth)
1230 call assert_equal(0, res.maxheight)
1231 call assert_equal(50, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001232 call assert_equal(0, res.fixed)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001233 if has('timers')
1234 call assert_equal(0, res.time)
1235 endif
1236 call popup_close(winid)
1237 call assert_equal({}, popup_getoptions(winid))
1238endfunc
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001239
1240func Test_popup_option_values()
1241 new
1242 " window-local
1243 setlocal number
1244 setlocal nowrap
1245 " buffer-local
1246 setlocal omnifunc=Something
1247 " global/buffer-local
1248 setlocal path=/there
1249 " global/window-local
Bram Moolenaara112f2d2019-09-01 17:38:09 +02001250 setlocal statusline=2
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001251
1252 let winid = popup_create('hello', {})
1253 call assert_equal(0, getwinvar(winid, '&number'))
1254 call assert_equal(1, getwinvar(winid, '&wrap'))
1255 call assert_equal('', getwinvar(winid, '&omnifunc'))
1256 call assert_equal(&g:path, getwinvar(winid, '&path'))
Bram Moolenaara112f2d2019-09-01 17:38:09 +02001257 call assert_equal(&g:statusline, getwinvar(winid, '&statusline'))
1258
1259 " 'scrolloff' is reset to zero
1260 call assert_equal(5, &scrolloff)
1261 call assert_equal(0, getwinvar(winid, '&scrolloff'))
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001262
1263 call popup_close(winid)
1264 bwipe
1265endfunc
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001266
1267func Test_popup_atcursor()
1268 topleft vnew
1269 call setline(1, [
1270 \ 'xxxxxxxxxxxxxxxxx',
1271 \ 'xxxxxxxxxxxxxxxxx',
1272 \ 'xxxxxxxxxxxxxxxxx',
1273 \])
1274
1275 call cursor(2, 2)
1276 redraw
1277 let winid = popup_atcursor('vim', {})
1278 redraw
1279 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
1280 call assert_equal('xvimxxxxxxxxxxxxx', line)
1281 call popup_close(winid)
1282
1283 call cursor(3, 4)
1284 redraw
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001285 let winid = 'vim'->popup_atcursor({})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001286 redraw
1287 let line = join(map(range(1, 17), 'screenstring(2, v:val)'), '')
1288 call assert_equal('xxxvimxxxxxxxxxxx', line)
1289 call popup_close(winid)
1290
1291 call cursor(1, 1)
1292 redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001293 let winid = popup_create('vim', #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001294 \ line: 'cursor+2',
1295 \ col: 'cursor+1',
1296 \})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001297 redraw
1298 let line = join(map(range(1, 17), 'screenstring(3, v:val)'), '')
1299 call assert_equal('xvimxxxxxxxxxxxxx', line)
1300 call popup_close(winid)
1301
1302 call cursor(3, 3)
1303 redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001304 let winid = popup_create('vim', #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001305 \ line: 'cursor-2',
1306 \ col: 'cursor-1',
1307 \})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001308 redraw
1309 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
1310 call assert_equal('xvimxxxxxxxxxxxxx', line)
1311 call popup_close(winid)
1312
Bram Moolenaar402502d2019-05-30 22:07:36 +02001313 " just enough room above
1314 call cursor(3, 3)
1315 redraw
1316 let winid = popup_atcursor(['vim', 'is great'], {})
1317 redraw
1318 let pos = popup_getpos(winid)
1319 call assert_equal(1, pos.line)
1320 call popup_close(winid)
1321
1322 " not enough room above, popup goes below the cursor
1323 call cursor(3, 3)
1324 redraw
1325 let winid = popup_atcursor(['vim', 'is', 'great'], {})
1326 redraw
1327 let pos = popup_getpos(winid)
1328 call assert_equal(4, pos.line)
1329 call popup_close(winid)
1330
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001331 " cursor in first line, popup in line 2
1332 call cursor(1, 1)
1333 redraw
1334 let winid = popup_atcursor(['vim', 'is', 'great'], {})
1335 redraw
1336 let pos = popup_getpos(winid)
1337 call assert_equal(2, pos.line)
1338 call popup_close(winid)
1339
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001340 bwipe!
1341endfunc
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001342
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001343func Test_popup_atcursor_pos()
1344 CheckScreendump
1345
1346 let lines =<< trim END
1347 call setline(1, repeat([repeat('-', 60)], 15))
1348 set so=0
1349
1350 normal 9G3|r#
1351 let winid1 = popup_atcursor(['first', 'second'], #{
1352 \ moved: [0, 0, 0],
1353 \ })
1354 normal 9G21|r&
1355 let winid1 = popup_atcursor(['FIrsT', 'SEcoND'], #{
1356 \ pos: 'botright',
1357 \ moved: [0, 0, 0],
1358 \ })
1359 normal 3G27|r%
1360 let winid1 = popup_atcursor(['fiRSt', 'seCOnd'], #{
1361 \ pos: 'topleft',
1362 \ moved: [0, 0, 0],
1363 \ })
1364 normal 3G45|r@
1365 let winid1 = popup_atcursor(['First', 'SeconD'], #{
1366 \ pos: 'topright',
Bram Moolenaarb0992022020-01-30 14:55:42 +01001367 \ moved: range(3),
1368 \ mousemoved: range(3),
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001369 \ })
1370 END
1371 call writefile(lines, 'XtestPopupAtcursorPos')
1372 let buf = RunVimInTerminal('-S XtestPopupAtcursorPos', #{rows: 12})
1373 call VerifyScreenDump(buf, 'Test_popupwin_atcursor_pos', {})
1374
1375 " clean up
1376 call StopVimInTerminal(buf)
1377 call delete('XtestPopupAtcursorPos')
1378endfunc
1379
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001380func Test_popup_beval()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001381 CheckScreendump
Bram Moolenaar1e82a782019-09-21 22:57:06 +02001382 CheckFeature balloon_eval_term
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001383
1384 let lines =<< trim END
1385 call setline(1, range(1, 20))
1386 call setline(5, 'here is some text to hover over')
1387 set balloonevalterm
1388 set balloonexpr=BalloonExpr()
1389 set balloondelay=100
1390 func BalloonExpr()
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001391 let s:winid = [v:beval_text]->popup_beval({})
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001392 return ''
1393 endfunc
1394 func Hover()
1395 call test_setmouse(5, 15)
1396 call feedkeys("\<MouseMove>\<Ignore>", "xt")
1397 sleep 100m
1398 endfunc
1399 func MoveOntoPopup()
1400 call test_setmouse(4, 17)
1401 call feedkeys("\<F4>\<MouseMove>\<Ignore>", "xt")
1402 endfunc
1403 func MoveAway()
1404 call test_setmouse(5, 13)
1405 call feedkeys("\<F5>\<MouseMove>\<Ignore>", "xt")
1406 endfunc
1407 END
1408 call writefile(lines, 'XtestPopupBeval')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001409 let buf = RunVimInTerminal('-S XtestPopupBeval', #{rows: 10})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001410 call TermWait(buf, 50)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001411 call term_sendkeys(buf, 'j')
1412 call term_sendkeys(buf, ":call Hover()\<CR>")
1413 call VerifyScreenDump(buf, 'Test_popupwin_beval_1', {})
1414
1415 call term_sendkeys(buf, ":call MoveOntoPopup()\<CR>")
1416 call VerifyScreenDump(buf, 'Test_popupwin_beval_2', {})
1417
1418 call term_sendkeys(buf, ":call MoveAway()\<CR>")
1419 call VerifyScreenDump(buf, 'Test_popupwin_beval_3', {})
1420
1421 " clean up
1422 call StopVimInTerminal(buf)
1423 call delete('XtestPopupBeval')
1424endfunc
1425
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001426func Test_popup_filter()
1427 new
1428 call setline(1, 'some text')
1429
1430 func MyPopupFilter(winid, c)
Bram Moolenaarec084d32020-02-28 22:44:47 +01001431 if a:c == 'e' || a:c == "\<F9>"
1432 let g:eaten = a:c
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001433 return 1
1434 endif
1435 if a:c == '0'
1436 let g:ignored = '0'
1437 return 0
1438 endif
1439 if a:c == 'x'
1440 call popup_close(a:winid)
1441 return 1
1442 endif
1443 return 0
1444 endfunc
1445
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001446 let winid = 'something'->popup_create(#{filter: 'MyPopupFilter'})
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001447 redraw
1448
1449 " e is consumed by the filter
1450 call feedkeys('e', 'xt')
1451 call assert_equal('e', g:eaten)
Bram Moolenaarec084d32020-02-28 22:44:47 +01001452 call feedkeys("\<F9>", 'xt')
1453 call assert_equal("\<F9>", g:eaten)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001454
1455 " 0 is ignored by the filter
1456 normal $
1457 call assert_equal(9, getcurpos()[2])
1458 call feedkeys('0', 'xt')
1459 call assert_equal('0', g:ignored)
1460 call assert_equal(1, getcurpos()[2])
1461
1462 " x closes the popup
1463 call feedkeys('x', 'xt')
Bram Moolenaarec084d32020-02-28 22:44:47 +01001464 call assert_equal("\<F9>", g:eaten)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001465 call assert_equal(-1, winbufnr(winid))
1466
1467 delfunc MyPopupFilter
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001468 call popup_clear()
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001469endfunc
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001470
Bram Moolenaara42d9452019-06-15 21:46:30 +02001471func ShowDialog(key, result)
1472 let s:cb_res = 999
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001473 let winid = popup_dialog('do you want to quit (Yes/no)?', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001474 \ filter: 'popup_filter_yesno',
1475 \ callback: 'QuitCallback',
Bram Moolenaara42d9452019-06-15 21:46:30 +02001476 \ })
1477 redraw
1478 call feedkeys(a:key, "xt")
1479 call assert_equal(winid, s:cb_winid)
1480 call assert_equal(a:result, s:cb_res)
1481endfunc
1482
1483func Test_popup_dialog()
1484 func QuitCallback(id, res)
1485 let s:cb_winid = a:id
1486 let s:cb_res = a:res
1487 endfunc
1488
1489 let winid = ShowDialog("y", 1)
1490 let winid = ShowDialog("Y", 1)
1491 let winid = ShowDialog("n", 0)
1492 let winid = ShowDialog("N", 0)
1493 let winid = ShowDialog("x", 0)
1494 let winid = ShowDialog("X", 0)
1495 let winid = ShowDialog("\<Esc>", 0)
1496 let winid = ShowDialog("\<C-C>", -1)
1497
1498 delfunc QuitCallback
1499endfunc
1500
Bram Moolenaara730e552019-06-16 19:05:31 +02001501func ShowMenu(key, result)
1502 let s:cb_res = 999
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001503 let winid = popup_menu(['one', 'two', 'something else'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001504 \ callback: 'QuitCallback',
Bram Moolenaara730e552019-06-16 19:05:31 +02001505 \ })
1506 redraw
1507 call feedkeys(a:key, "xt")
1508 call assert_equal(winid, s:cb_winid)
1509 call assert_equal(a:result, s:cb_res)
1510endfunc
1511
1512func Test_popup_menu()
1513 func QuitCallback(id, res)
1514 let s:cb_winid = a:id
1515 let s:cb_res = a:res
1516 endfunc
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001517 " mapping won't be used in popup
1518 map j k
Bram Moolenaara730e552019-06-16 19:05:31 +02001519
1520 let winid = ShowMenu(" ", 1)
1521 let winid = ShowMenu("j \<CR>", 2)
1522 let winid = ShowMenu("JjK \<CR>", 2)
1523 let winid = ShowMenu("jjjjjj ", 3)
1524 let winid = ShowMenu("kkk ", 1)
1525 let winid = ShowMenu("x", -1)
1526 let winid = ShowMenu("X", -1)
1527 let winid = ShowMenu("\<Esc>", -1)
1528 let winid = ShowMenu("\<C-C>", -1)
1529
1530 delfunc QuitCallback
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001531 unmap j
Bram Moolenaara730e552019-06-16 19:05:31 +02001532endfunc
1533
1534func Test_popup_menu_screenshot()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001535 CheckScreendump
Bram Moolenaara730e552019-06-16 19:05:31 +02001536
1537 let lines =<< trim END
1538 call setline(1, range(1, 20))
1539 hi PopupSelected ctermbg=lightblue
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001540 call popup_menu(['one', 'two', 'another'], #{callback: 'MenuDone', title: ' make a choice from the list '})
Bram Moolenaara730e552019-06-16 19:05:31 +02001541 func MenuDone(id, res)
1542 echomsg "selected " .. a:res
1543 endfunc
1544 END
1545 call writefile(lines, 'XtestPopupMenu')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001546 let buf = RunVimInTerminal('-S XtestPopupMenu', #{rows: 10})
Bram Moolenaara730e552019-06-16 19:05:31 +02001547 call VerifyScreenDump(buf, 'Test_popupwin_menu_01', {})
1548
1549 call term_sendkeys(buf, "jj")
1550 call VerifyScreenDump(buf, 'Test_popupwin_menu_02', {})
1551
1552 call term_sendkeys(buf, " ")
1553 call VerifyScreenDump(buf, 'Test_popupwin_menu_03', {})
1554
1555 " clean up
1556 call StopVimInTerminal(buf)
1557 call delete('XtestPopupMenu')
1558endfunc
1559
Bram Moolenaarf914a332019-07-20 15:09:56 +02001560func Test_popup_menu_narrow()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001561 CheckScreendump
Bram Moolenaarf914a332019-07-20 15:09:56 +02001562
1563 let lines =<< trim END
1564 call setline(1, range(1, 20))
1565 hi PopupSelected ctermbg=green
1566 call popup_menu(['one', 'two', 'three'], #{callback: 'MenuDone'})
1567 func MenuDone(id, res)
1568 echomsg "selected " .. a:res
1569 endfunc
1570 END
1571 call writefile(lines, 'XtestPopupNarrowMenu')
1572 let buf = RunVimInTerminal('-S XtestPopupNarrowMenu', #{rows: 10})
1573 call VerifyScreenDump(buf, 'Test_popupwin_menu_04', {})
1574
1575 " clean up
1576 call term_sendkeys(buf, "x")
1577 call StopVimInTerminal(buf)
1578 call delete('XtestPopupNarrowMenu')
1579endfunc
1580
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001581func Test_popup_title()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001582 CheckScreendump
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001583
1584 " Create a popup without title or border, a line of padding will be added to
1585 " put the title on.
1586 let lines =<< trim END
1587 call setline(1, range(1, 20))
Bram Moolenaar5d458a72019-08-04 21:12:15 +02001588 let winid = popup_create(['one', 'two', 'another'], #{title: 'Title String'})
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001589 END
1590 call writefile(lines, 'XtestPopupTitle')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001591 let buf = RunVimInTerminal('-S XtestPopupTitle', #{rows: 10})
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001592 call VerifyScreenDump(buf, 'Test_popupwin_title', {})
1593
Bram Moolenaar5d458a72019-08-04 21:12:15 +02001594 call term_sendkeys(buf, ":call popup_setoptions(winid, #{maxwidth: 20, title: 'a very long title that is not going to fit'})\<CR>")
1595 call term_sendkeys(buf, ":\<CR>")
1596 call VerifyScreenDump(buf, 'Test_popupwin_longtitle_1', {})
1597
1598 call term_sendkeys(buf, ":call popup_setoptions(winid, #{border: []})\<CR>")
1599 call term_sendkeys(buf, ":\<CR>")
1600 call VerifyScreenDump(buf, 'Test_popupwin_longtitle_2', {})
1601
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001602 " clean up
1603 call StopVimInTerminal(buf)
1604 call delete('XtestPopupTitle')
Bram Moolenaarae943152019-06-16 22:54:14 +02001605
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001606 let winid = popup_create('something', #{title: 'Some Title'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001607 call assert_equal('Some Title', popup_getoptions(winid).title)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001608 call popup_setoptions(winid, #{title: 'Another Title'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001609 call assert_equal('Another Title', popup_getoptions(winid).title)
1610
1611 call popup_clear()
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001612endfunc
1613
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001614func Test_popup_close_callback()
1615 func PopupDone(id, result)
1616 let g:result = a:result
1617 endfunc
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001618 let winid = popup_create('something', #{callback: 'PopupDone'})
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001619 redraw
1620 call popup_close(winid, 'done')
1621 call assert_equal('done', g:result)
1622endfunc
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001623
1624func Test_popup_empty()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001625 let winid = popup_create('', #{padding: [2,2,2,2]})
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001626 redraw
1627 let pos = popup_getpos(winid)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001628 call assert_equal(5, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001629 call assert_equal(5, pos.height)
Bram Moolenaaref6b9792020-05-13 16:34:15 +02001630 call popup_close(winid)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001631
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001632 let winid = popup_create([], #{border: []})
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001633 redraw
1634 let pos = popup_getpos(winid)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001635 call assert_equal(3, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001636 call assert_equal(3, pos.height)
Bram Moolenaaref6b9792020-05-13 16:34:15 +02001637 call popup_close(winid)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001638endfunc
Bram Moolenaar988c4332019-06-02 14:12:11 +02001639
1640func Test_popup_never_behind()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001641 CheckScreendump
1642
Bram Moolenaar988c4332019-06-02 14:12:11 +02001643 " +-----------------------------+
1644 " | | |
1645 " | | |
1646 " | | |
1647 " | line1 |
1648 " |------------line2------------|
1649 " | line3 |
1650 " | line4 |
1651 " | |
1652 " | |
1653 " +-----------------------------+
1654 let lines =<< trim END
Bram Moolenaar988c4332019-06-02 14:12:11 +02001655 split
1656 vsplit
1657 let info_window1 = getwininfo()[0]
1658 let line = info_window1['height']
1659 let col = info_window1['width']
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001660 call popup_create(['line1', 'line2', 'line3', 'line4'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001661 \ line : line,
1662 \ col : col,
Bram Moolenaar988c4332019-06-02 14:12:11 +02001663 \ })
1664 END
1665 call writefile(lines, 'XtestPopupBehind')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001666 let buf = RunVimInTerminal('-S XtestPopupBehind', #{rows: 10})
Bram Moolenaar988c4332019-06-02 14:12:11 +02001667 call term_sendkeys(buf, "\<C-W>w")
1668 call VerifyScreenDump(buf, 'Test_popupwin_behind', {})
1669
1670 " clean up
1671 call StopVimInTerminal(buf)
1672 call delete('XtestPopupBehind')
1673endfunc
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001674
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001675func s:VerifyPosition(p, msg, line, col, width, height)
1676 call assert_equal(a:line, popup_getpos(a:p).line, a:msg . ' (l)')
1677 call assert_equal(a:col, popup_getpos(a:p).col, a:msg . ' (c)')
1678 call assert_equal(a:width, popup_getpos(a:p).width, a:msg . ' (w)')
1679 call assert_equal(a:height, popup_getpos(a:p).height, a:msg . ' (h)')
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001680endfunc
1681
1682func Test_popup_position_adjust()
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001683 " Anything placed past the last cell on the right of the screen is moved to
1684 " the left.
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001685 "
1686 " When wrapping is disabled, we also shift to the left to display on the
1687 " screen, unless fixed is set.
1688
1689 " Entries for cases which don't vary based on wrapping.
1690 " Format is per tests described below
1691 let both_wrap_tests = [
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001692 \ ['a', 5, &columns, 5, &columns, 1, 1],
1693 \ ['b', 5, &columns + 1, 5, &columns, 1, 1],
1694 \ ['c', 5, &columns - 1, 5, &columns - 1, 1, 1],
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001695 \ ['d', 5, &columns - 2, 5, &columns - 2, 1, 1],
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001696 \ ['e', 5, &columns - 3, 5, &columns - 3, 1, 1]]
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001697
1698 " these test groups are dicts with:
1699 " - comment: something to identify the group of tests by
1700 " - options: dict of options to merge with the row/col in tests
1701 " - tests: list of cases. Each one is a list with elements:
1702 " - text
1703 " - row
1704 " - col
1705 " - expected row
1706 " - expected col
1707 " - expected width
1708 " - expected height
1709 let tests = [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001710 \ #{
1711 \ comment: 'left-aligned with wrapping',
1712 \ options: #{
1713 \ wrap: 1,
1714 \ pos: 'botleft',
1715 \ },
1716 \ tests: both_wrap_tests + [
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001717 \ ['aa', 5, &columns, 4, &columns, 1, 2],
1718 \ ['bb', 5, &columns + 1, 4, &columns, 1, 2],
1719 \ ['cc', 5, &columns - 1, 5, &columns - 1, 2, 1],
1720 \ ['dd', 5, &columns - 2, 5, &columns - 2, 2, 1],
1721 \ ['ee', 5, &columns - 3, 5, &columns - 3, 2, 1],
1722 \
1723 \ ['aaa', 5, &columns, 3, &columns, 1, 3],
1724 \ ['bbb', 5, &columns + 1, 3, &columns, 1, 3],
1725 \ ['ccc', 5, &columns - 1, 4, &columns - 1, 2, 2],
1726 \ ['ddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
1727 \ ['eee', 5, &columns - 3, 5, &columns - 3, 3, 1],
1728 \
1729 \ ['aaaa', 5, &columns, 2, &columns, 1, 4],
1730 \ ['bbbb', 5, &columns + 1, 2, &columns, 1, 4],
1731 \ ['cccc', 5, &columns - 1, 4, &columns - 1, 2, 2],
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001732 \ ['dddd', 5, &columns - 2, 4, &columns - 2, 3, 2],
1733 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001734 \ ['eeee', 5, &columns - 4, 5, &columns - 4, 4, 1],
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001735 \ ],
1736 \ },
1737 \ #{
1738 \ comment: 'left aligned without wrapping',
1739 \ options: #{
1740 \ wrap: 0,
1741 \ pos: 'botleft',
1742 \ },
1743 \ tests: both_wrap_tests + [
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001744 \ ['aa', 5, &columns, 5, &columns - 1, 2, 1],
1745 \ ['bb', 5, &columns + 1, 5, &columns - 1, 2, 1],
1746 \ ['cc', 5, &columns - 1, 5, &columns - 1, 2, 1],
1747 \ ['dd', 5, &columns - 2, 5, &columns - 2, 2, 1],
1748 \ ['ee', 5, &columns - 3, 5, &columns - 3, 2, 1],
1749 \
1750 \ ['aaa', 5, &columns, 5, &columns - 2, 3, 1],
1751 \ ['bbb', 5, &columns + 1, 5, &columns - 2, 3, 1],
1752 \ ['ccc', 5, &columns - 1, 5, &columns - 2, 3, 1],
1753 \ ['ddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
1754 \ ['eee', 5, &columns - 3, 5, &columns - 3, 3, 1],
1755 \
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001756 \ ['aaaa', 5, &columns, 5, &columns - 3, 4, 1],
1757 \ ['bbbb', 5, &columns + 1, 5, &columns - 3, 4, 1],
1758 \ ['cccc', 5, &columns - 1, 5, &columns - 3, 4, 1],
1759 \ ['dddd', 5, &columns - 2, 5, &columns - 3, 4, 1],
1760 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
1761 \ ],
1762 \ },
1763 \ #{
1764 \ comment: 'left aligned with fixed position',
1765 \ options: #{
1766 \ wrap: 0,
1767 \ fixed: 1,
1768 \ pos: 'botleft',
1769 \ },
1770 \ tests: both_wrap_tests + [
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001771 \ ['aa', 5, &columns, 5, &columns, 1, 1],
1772 \ ['bb', 5, &columns + 1, 5, &columns, 1, 1],
1773 \ ['cc', 5, &columns - 1, 5, &columns - 1, 2, 1],
1774 \ ['dd', 5, &columns - 2, 5, &columns - 2, 2, 1],
1775 \ ['ee', 5, &columns - 3, 5, &columns - 3, 2, 1],
1776 \
1777 \ ['aaa', 5, &columns, 5, &columns, 1, 1],
1778 \ ['bbb', 5, &columns + 1, 5, &columns, 1, 1],
1779 \ ['ccc', 5, &columns - 1, 5, &columns - 1, 2, 1],
1780 \ ['ddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
1781 \ ['eee', 5, &columns - 3, 5, &columns - 3, 3, 1],
1782 \
1783 \ ['aaaa', 5, &columns, 5, &columns, 1, 1],
1784 \ ['bbbb', 5, &columns + 1, 5, &columns, 1, 1],
1785 \ ['cccc', 5, &columns - 1, 5, &columns - 1, 2, 1],
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001786 \ ['dddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
1787 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
1788 \ ],
1789 \ },
1790 \ ]
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001791
1792 for test_group in tests
1793 for test in test_group.tests
1794 let [ text, line, col, e_line, e_col, e_width, e_height ] = test
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001795 let options = #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001796 \ line: line,
1797 \ col: col,
1798 \ }
1799 call extend(options, test_group.options)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001800
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001801 let p = popup_create(text, options)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001802
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001803 let msg = string(extend(options, #{text: text}))
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001804 call s:VerifyPosition(p, msg, e_line, e_col, e_width, e_height)
1805 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001806 endfor
1807 endfor
1808
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001809 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001810 %bwipe!
1811endfunc
1812
Bram Moolenaar3397f742019-06-02 18:40:06 +02001813func Test_adjust_left_past_screen_width()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001814 " width of screen
1815 let X = join(map(range(&columns), {->'X'}), '')
1816
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001817 let p = popup_create(X, #{line: 1, col: 1, wrap: 0})
1818 call s:VerifyPosition(p, 'full width topleft', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001819
1820 redraw
1821 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1822 call assert_equal(X, line)
1823
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001824 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001825 redraw
1826
1827 " Same if placed on the right hand side
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001828 let p = popup_create(X, #{line: 1, col: &columns, wrap: 0})
1829 call s:VerifyPosition(p, 'full width topright', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001830
1831 redraw
1832 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1833 call assert_equal(X, line)
1834
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001835 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001836 redraw
1837
1838 " Extend so > window width
1839 let X .= 'x'
1840
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001841 let p = popup_create(X, #{line: 1, col: 1, wrap: 0})
1842 call s:VerifyPosition(p, 'full width + 1 topleft', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001843
1844 redraw
1845 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1846 call assert_equal(X[ : -2 ], line)
1847
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001848 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001849 redraw
1850
1851 " Shifted then truncated (the x is not visible)
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001852 let p = popup_create(X, #{line: 1, col: &columns - 3, wrap: 0})
1853 call s:VerifyPosition(p, 'full width + 1 topright', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001854
1855 redraw
1856 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1857 call assert_equal(X[ : -2 ], line)
1858
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001859 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001860 redraw
1861
1862 " Not shifted, just truncated
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001863 let p = popup_create(X,
1864 \ #{line: 1, col: 2, wrap: 0, fixed: 1})
1865 call s:VerifyPosition(p, 'full width + 1 fixed', 1, 2, &columns - 1, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001866
1867 redraw
1868 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
1869 let e_line = ' ' . X[ 1 : -2 ]
1870 call assert_equal(e_line, line)
1871
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001872 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001873 redraw
1874
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001875 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001876 %bwipe!
Bram Moolenaar3397f742019-06-02 18:40:06 +02001877endfunc
1878
1879func Test_popup_moved()
1880 new
1881 call test_override('char_avail', 1)
1882 call setline(1, ['one word to move around', 'a WORD.and->some thing'])
1883
1884 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001885 let winid = popup_atcursor('text', #{moved: 'any'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001886 redraw
1887 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001888 call assert_equal([1, 4, 4], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001889 " trigger the check for last_cursormoved by going into insert mode
1890 call feedkeys("li\<Esc>", 'xt')
1891 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001892 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001893
1894 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001895 let winid = popup_atcursor('text', #{moved: 'word'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001896 redraw
1897 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001898 call assert_equal([1, 4, 7], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001899 call feedkeys("hi\<Esc>", 'xt')
1900 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001901 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001902
1903 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001904 let winid = popup_atcursor('text', #{moved: 'word'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001905 redraw
1906 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001907 call assert_equal([1, 4, 7], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001908 call feedkeys("li\<Esc>", 'xt')
1909 call assert_equal(1, popup_getpos(winid).visible)
1910 call feedkeys("ei\<Esc>", 'xt')
1911 call assert_equal(1, popup_getpos(winid).visible)
1912 call feedkeys("eli\<Esc>", 'xt')
1913 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001914 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001915
Bram Moolenaar17627312019-06-02 19:53:44 +02001916 " WORD is the default
Bram Moolenaar3397f742019-06-02 18:40:06 +02001917 exe "normal gg0/WORD\<CR>"
Bram Moolenaar17627312019-06-02 19:53:44 +02001918 let winid = popup_atcursor('text', {})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001919 redraw
1920 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001921 call assert_equal([2, 2, 15], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02001922 call feedkeys("eli\<Esc>", 'xt')
1923 call assert_equal(1, popup_getpos(winid).visible)
1924 call feedkeys("wi\<Esc>", 'xt')
1925 call assert_equal(1, popup_getpos(winid).visible)
1926 call feedkeys("Eli\<Esc>", 'xt')
1927 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001928 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001929
1930 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001931 let winid = popup_atcursor('text', #{moved: [5, 10]})
Bram Moolenaar3397f742019-06-02 18:40:06 +02001932 redraw
1933 call assert_equal(1, popup_getpos(winid).visible)
1934 call feedkeys("eli\<Esc>", 'xt')
1935 call feedkeys("ei\<Esc>", 'xt')
1936 call assert_equal(1, popup_getpos(winid).visible)
1937 call feedkeys("eli\<Esc>", 'xt')
1938 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001939 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02001940
1941 bwipe!
1942 call test_override('ALL', 0)
1943endfunc
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001944
1945func Test_notifications()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001946 CheckFeature timers
1947 CheckScreendump
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001948
Bram Moolenaar0fdddee2019-09-01 15:26:23 +02001949 let lines =<< trim END
1950 call setline(1, range(1, 20))
1951 hi Notification ctermbg=lightblue
1952 call popup_notification('first notification', {})
1953 END
1954 call writefile(lines, 'XtestNotifications')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001955 let buf = RunVimInTerminal('-S XtestNotifications', #{rows: 10})
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001956 call VerifyScreenDump(buf, 'Test_popupwin_notify_01', {})
1957
1958 " second one goes below the first one
Bram Moolenaardfa97f22019-06-15 14:31:55 +02001959 call term_sendkeys(buf, ":hi link PopupNotification Notification\<CR>")
1960 call term_sendkeys(buf, ":call popup_notification('another important notification', {})\<CR>")
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001961 call VerifyScreenDump(buf, 'Test_popupwin_notify_02', {})
1962
Bram Moolenaar68d48f42019-06-12 22:42:41 +02001963 " clean up
1964 call StopVimInTerminal(buf)
1965 call delete('XtestNotifications')
1966endfunc
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001967
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001968func Test_popup_scrollbar()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001969 CheckScreendump
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001970
1971 let lines =<< trim END
1972 call setline(1, range(1, 20))
Bram Moolenaar8da41812019-06-26 18:04:54 +02001973 hi ScrollThumb ctermbg=blue
1974 hi ScrollBar ctermbg=red
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001975 let winid = popup_create(['one', 'two', 'three', 'four', 'five',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001976 \ 'six', 'seven', 'eight', 'nine'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001977 \ minwidth: 8,
1978 \ maxheight: 4,
Bram Moolenaar75fb0852019-06-25 05:15:58 +02001979 \ })
Bram Moolenaar53a95d62019-06-26 03:54:08 +02001980 func ScrollUp()
1981 call feedkeys("\<F3>\<ScrollWheelUp>", "xt")
1982 endfunc
1983 func ScrollDown()
1984 call feedkeys("\<F3>\<ScrollWheelDown>", "xt")
1985 endfunc
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001986 func ClickTop()
1987 call feedkeys("\<F4>\<LeftMouse>", "xt")
1988 endfunc
1989 func ClickBot()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001990 call popup_setoptions(g:winid, #{border: [], close: 'button'})
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02001991 call feedkeys("\<F5>\<LeftMouse>", "xt")
1992 endfunc
Bram Moolenaarf2885d32019-11-02 20:21:25 +01001993 func Popup_filter(winid, key)
1994 if a:key == 'j'
1995 let line = popup_getoptions(a:winid).firstline
1996 let nlines = line('$', a:winid)
1997 let newline = line < nlines ? (line + 1) : nlines
1998 call popup_setoptions(a:winid, #{firstline: newline})
1999 return v:true
2000 elseif a:key == 'x'
2001 call popup_close(a:winid)
2002 return v:true
2003 endif
2004 endfunc
2005
2006 func PopupScroll()
2007 call popup_clear()
2008 let text =<< trim END
2009 1
2010 2
2011 3
2012 4
2013 long line long line long line long line long line long line
2014 long line long line long line long line long line long line
2015 long line long line long line long line long line long line
2016 END
2017 call popup_create(text, #{
2018 \ minwidth: 30,
2019 \ maxwidth: 30,
2020 \ minheight: 4,
2021 \ maxheight: 4,
2022 \ firstline: 1,
Bram Moolenaar30efcf32019-11-03 22:29:38 +01002023 \ lastline: 4,
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002024 \ wrap: v:true,
2025 \ scrollbar: v:true,
2026 \ mapping: v:false,
2027 \ filter: funcref('Popup_filter')
2028 \ })
2029 endfunc
Bram Moolenaar53a95d62019-06-26 03:54:08 +02002030 map <silent> <F3> :call test_setmouse(5, 36)<CR>
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02002031 map <silent> <F4> :call test_setmouse(4, 42)<CR>
2032 map <silent> <F5> :call test_setmouse(7, 42)<CR>
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002033 END
2034 call writefile(lines, 'XtestPopupScroll')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002035 let buf = RunVimInTerminal('-S XtestPopupScroll', #{rows: 10})
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002036 call VerifyScreenDump(buf, 'Test_popupwin_scroll_1', {})
2037
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002038 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 2})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002039 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002040 call VerifyScreenDump(buf, 'Test_popupwin_scroll_2', {})
2041
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002042 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 6})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002043 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002044 call VerifyScreenDump(buf, 'Test_popupwin_scroll_3', {})
2045
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002046 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 9})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002047 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002048 call VerifyScreenDump(buf, 'Test_popupwin_scroll_4', {})
2049
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02002050 call term_sendkeys(buf, ":call popup_setoptions(winid, #{scrollbarhighlight: 'ScrollBar', thumbhighlight: 'ScrollThumb', firstline: 5})\<CR>")
Bram Moolenaara112f2d2019-09-01 17:38:09 +02002051 " this scrolls two lines (half the window height)
Bram Moolenaar53a95d62019-06-26 03:54:08 +02002052 call term_sendkeys(buf, ":call ScrollUp()\<CR>")
2053 call VerifyScreenDump(buf, 'Test_popupwin_scroll_5', {})
2054
2055 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
2056 call VerifyScreenDump(buf, 'Test_popupwin_scroll_6', {})
2057
2058 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
Bram Moolenaar13b47c32019-06-28 21:55:48 +02002059 " wait a bit, otherwise it fails sometimes (double click recognized?)
2060 sleep 100m
Bram Moolenaar53a95d62019-06-26 03:54:08 +02002061 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
2062 call VerifyScreenDump(buf, 'Test_popupwin_scroll_7', {})
2063
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02002064 call term_sendkeys(buf, ":call ClickTop()\<CR>")
2065 sleep 100m
2066 call term_sendkeys(buf, ":call ClickTop()\<CR>")
2067 call VerifyScreenDump(buf, 'Test_popupwin_scroll_8', {})
2068
2069 call term_sendkeys(buf, ":call ClickBot()\<CR>")
2070 call VerifyScreenDump(buf, 'Test_popupwin_scroll_9', {})
2071
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02002072 " remove the minwidth and maxheight
2073 call term_sendkeys(buf, ":call popup_setoptions(winid, #{maxheight: 0, minwidth: 0})\<CR>")
Bram Moolenaar7e0f4622019-09-17 21:23:39 +02002074 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02002075 call VerifyScreenDump(buf, 'Test_popupwin_scroll_10', {})
2076
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002077 " check size with non-wrapping lines
2078 call term_sendkeys(buf, ":call PopupScroll()\<CR>")
2079 call VerifyScreenDump(buf, 'Test_popupwin_scroll_11', {})
2080
2081 " check size with wrapping lines
2082 call term_sendkeys(buf, "j")
2083 call VerifyScreenDump(buf, 'Test_popupwin_scroll_12', {})
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002084
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002085 " clean up
Bram Moolenaar20298ce2020-06-19 21:46:52 +02002086 call term_sendkeys(buf, "x")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002087 call StopVimInTerminal(buf)
2088 call delete('XtestPopupScroll')
2089endfunc
2090
Bram Moolenaar437a7462019-07-05 20:17:22 +02002091func Test_popup_fitting_scrollbar()
2092 " this was causing a crash, divide by zero
2093 let winid = popup_create([
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002094 \ 'one', 'two', 'longer line that wraps', 'four', 'five'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002095 \ scrollbar: 1,
2096 \ maxwidth: 10,
2097 \ maxheight: 5,
2098 \ firstline: 2})
Bram Moolenaar437a7462019-07-05 20:17:22 +02002099 redraw
2100 call popup_clear()
2101endfunc
2102
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002103func Test_popup_settext()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002104 CheckScreendump
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002105
2106 let lines =<< trim END
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002107 let opts = #{wrap: 0}
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002108 let p = popup_create('test', opts)
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002109 eval p->popup_settext('this is a text')
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002110 END
2111
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002112 call writefile(lines, 'XtestPopupSetText')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002113 let buf = RunVimInTerminal('-S XtestPopupSetText', #{rows: 10})
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002114 call VerifyScreenDump(buf, 'Test_popup_settext_01', {})
2115
2116 " Setting to empty string clears it
2117 call term_sendkeys(buf, ":call popup_settext(p, '')\<CR>")
2118 call VerifyScreenDump(buf, 'Test_popup_settext_02', {})
2119
2120 " Setting a list
2121 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
2122 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
2123
2124 " Shrinking with a list
2125 call term_sendkeys(buf, ":call popup_settext(p, ['a'])\<CR>")
2126 call VerifyScreenDump(buf, 'Test_popup_settext_04', {})
2127
2128 " Growing with a list
2129 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
2130 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
2131
2132 " Empty list clears
2133 call term_sendkeys(buf, ":call popup_settext(p, [])\<CR>")
2134 call VerifyScreenDump(buf, 'Test_popup_settext_05', {})
2135
2136 " Dicts
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002137 call term_sendkeys(buf, ":call popup_settext(p, [#{text: 'aaaa'}, #{text: 'bbbb'}, #{text: 'cccc'}])\<CR>")
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002138 call VerifyScreenDump(buf, 'Test_popup_settext_06', {})
2139
Bram Moolenaarb0992022020-01-30 14:55:42 +01002140 " range() (doesn't work)
2141 call term_sendkeys(buf, ":call popup_settext(p, range(4, 8))\<CR>")
2142 call VerifyScreenDump(buf, 'Test_popup_settext_07', {})
2143
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002144 " clean up
2145 call StopVimInTerminal(buf)
2146 call delete('XtestPopupSetText')
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002147endfunc
2148
2149func Test_popup_hidden()
2150 new
2151
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002152 let winid = popup_atcursor('text', #{hidden: 1})
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002153 redraw
2154 call assert_equal(0, popup_getpos(winid).visible)
2155 call popup_close(winid)
2156
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002157 let winid = popup_create('text', #{hidden: 1})
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002158 redraw
2159 call assert_equal(0, popup_getpos(winid).visible)
2160 call popup_close(winid)
2161
2162 func QuitCallback(id, res)
2163 let s:cb_winid = a:id
2164 let s:cb_res = a:res
2165 endfunc
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002166 let winid = 'make a choice'->popup_dialog(#{hidden: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002167 \ filter: 'popup_filter_yesno',
2168 \ callback: 'QuitCallback',
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002169 \ })
2170 redraw
2171 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarae943152019-06-16 22:54:14 +02002172 call assert_equal(function('popup_filter_yesno'), popup_getoptions(winid).filter)
2173 call assert_equal(function('QuitCallback'), popup_getoptions(winid).callback)
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002174 exe "normal anot used by filter\<Esc>"
2175 call assert_equal('not used by filter', getline(1))
2176
2177 call popup_show(winid)
2178 call feedkeys('y', "xt")
2179 call assert_equal(1, s:cb_res)
2180
2181 bwipe!
2182 delfunc QuitCallback
2183endfunc
Bram Moolenaarae943152019-06-16 22:54:14 +02002184
2185" Test options not checked elsewhere
2186func Test_set_get_options()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002187 let winid = popup_create('some text', #{highlight: 'Beautiful'})
Bram Moolenaarae943152019-06-16 22:54:14 +02002188 let options = popup_getoptions(winid)
2189 call assert_equal(1, options.wrap)
2190 call assert_equal(0, options.drag)
2191 call assert_equal('Beautiful', options.highlight)
2192
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002193 call popup_setoptions(winid, #{wrap: 0, drag: 1, highlight: 'Another'})
Bram Moolenaarae943152019-06-16 22:54:14 +02002194 let options = popup_getoptions(winid)
2195 call assert_equal(0, options.wrap)
2196 call assert_equal(1, options.drag)
2197 call assert_equal('Another', options.highlight)
2198
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02002199 call assert_fails('call popup_setoptions(winid, [])', 'E715:')
2200 call assert_fails('call popup_setoptions(winid, test_null_dict())', 'E715:')
2201
Bram Moolenaarae943152019-06-16 22:54:14 +02002202 call popup_close(winid)
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02002203 call assert_equal(0, popup_setoptions(winid, options.wrap))
Bram Moolenaarae943152019-06-16 22:54:14 +02002204endfunc
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002205
2206func Test_popupwin_garbage_collect()
2207 func MyPopupFilter(x, winid, c)
2208 " NOP
2209 endfunc
2210
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002211 let winid = popup_create('something', #{filter: function('MyPopupFilter', [{}])})
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002212 call test_garbagecollect_now()
2213 redraw
Bram Moolenaar1bc353b2019-09-01 14:45:28 +02002214 " Must not crash caused by invalid memory access
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002215 call feedkeys('j', 'xt')
2216 call assert_true(v:true)
2217
2218 call popup_close(winid)
2219 delfunc MyPopupFilter
2220endfunc
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002221
Bram Moolenaar581ba392019-09-03 22:08:33 +02002222func Test_popupwin_filter_mode()
2223 func MyPopupFilter(winid, c)
2224 let s:typed = a:c
2225 if a:c == ':' || a:c == "\r" || a:c == 'v'
2226 " can start cmdline mode, get out, and start/stop Visual mode
2227 return 0
2228 endif
2229 return 1
2230 endfunc
2231
2232 " Normal, Visual and Insert mode
2233 let winid = popup_create('something', #{filter: 'MyPopupFilter', filtermode: 'nvi'})
2234 redraw
2235 call feedkeys('x', 'xt')
2236 call assert_equal('x', s:typed)
2237
2238 call feedkeys(":let g:foo = 'foo'\<CR>", 'xt')
2239 call assert_equal(':', s:typed)
2240 call assert_equal('foo', g:foo)
2241
2242 let @x = 'something'
2243 call feedkeys('v$"xy', 'xt')
2244 call assert_equal('y', s:typed)
2245 call assert_equal('something', @x) " yank command is filtered out
2246 call feedkeys('v', 'xt') " end Visual mode
2247
2248 call popup_close(winid)
2249
2250 " only Normal mode
2251 let winid = popup_create('something', #{filter: 'MyPopupFilter', filtermode: 'n'})
2252 redraw
2253 call feedkeys('x', 'xt')
2254 call assert_equal('x', s:typed)
2255
2256 call feedkeys(":let g:foo = 'foo'\<CR>", 'xt')
2257 call assert_equal(':', s:typed)
2258 call assert_equal('foo', g:foo)
2259
2260 let @x = 'something'
2261 call feedkeys('v$"xy', 'xt')
2262 call assert_equal('v', s:typed)
2263 call assert_notequal('something', @x)
2264
2265 call popup_close(winid)
2266
2267 " default: all modes
2268 let winid = popup_create('something', #{filter: 'MyPopupFilter'})
2269 redraw
2270 call feedkeys('x', 'xt')
2271 call assert_equal('x', s:typed)
2272
2273 let g:foo = 'bar'
2274 call feedkeys(":let g:foo = 'foo'\<CR>", 'xt')
2275 call assert_equal("\r", s:typed)
2276 call assert_equal('bar', g:foo)
2277
2278 let @x = 'something'
2279 call feedkeys('v$"xy', 'xt')
2280 call assert_equal('y', s:typed)
2281 call assert_equal('something', @x) " yank command is filtered out
2282 call feedkeys('v', 'xt') " end Visual mode
2283
2284 call popup_close(winid)
2285 delfunc MyPopupFilter
2286endfunc
2287
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002288func Test_popupwin_filter_mouse()
2289 func MyPopupFilter(winid, c)
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002290 let g:got_mousepos = getmousepos()
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002291 return 0
2292 endfunc
2293
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002294 call setline(1, ['.'->repeat(25)]->repeat(10))
2295 let winid = popup_create(['short', 'long line that will wrap', 'other'], #{
2296 \ line: 2,
2297 \ col: 4,
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002298 \ maxwidth: 12,
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002299 \ padding: [],
2300 \ border: [],
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002301 \ filter: 'MyPopupFilter',
2302 \ })
2303 redraw
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002304 " 123456789012345678901
2305 " 1 .....................
2306 " 2 ...+--------------+..
2307 " 3 ...| |..
2308 " 4 ...| short |..
2309 " 5 ...| long line th |..
2310 " 6 ...| at will wrap |..
2311 " 7 ...| other |..
2312 " 8 ...| |..
2313 " 9 ...+--------------+..
2314 " 10 .....................
2315 let tests = []
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002316
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002317 func AddItemOutsidePopup(tests, row, col)
2318 eval a:tests->add(#{clickrow: a:row, clickcol: a:col, result: #{
2319 \ screenrow: a:row, screencol: a:col,
2320 \ winid: win_getid(), winrow: a:row, wincol: a:col,
2321 \ line: a:row, column: a:col,
2322 \ }})
2323 endfunc
2324 func AddItemInPopupBorder(tests, winid, row, col)
2325 eval a:tests->add(#{clickrow: a:row, clickcol: a:col, result: #{
2326 \ screenrow: a:row, screencol: a:col,
2327 \ winid: a:winid, winrow: a:row - 1, wincol: a:col - 3,
2328 \ line: 0, column: 0,
2329 \ }})
2330 endfunc
2331 func AddItemInPopupText(tests, winid, row, col, textline, textcol)
2332 eval a:tests->add(#{clickrow: a:row, clickcol: a:col, result: #{
2333 \ screenrow: a:row, screencol: a:col,
2334 \ winid: a:winid, winrow: a:row - 1, wincol: a:col - 3,
2335 \ line: a:textline, column: a:textcol,
2336 \ }})
2337 endfunc
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002338
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002339 " above and below popup
2340 for c in range(1, 21)
2341 call AddItemOutsidePopup(tests, 1, c)
2342 call AddItemOutsidePopup(tests, 10, c)
2343 endfor
2344 " left and right of popup
2345 for r in range(1, 10)
2346 call AddItemOutsidePopup(tests, r, 3)
2347 call AddItemOutsidePopup(tests, r, 20)
2348 endfor
2349 " top and bottom in popup
2350 for c in range(4, 19)
2351 call AddItemInPopupBorder(tests, winid, 2, c)
2352 call AddItemInPopupBorder(tests, winid, 3, c)
2353 call AddItemInPopupBorder(tests, winid, 8, c)
2354 call AddItemInPopupBorder(tests, winid, 9, c)
2355 endfor
2356 " left and right margin in popup
2357 for r in range(2, 9)
2358 call AddItemInPopupBorder(tests, winid, r, 4)
2359 call AddItemInPopupBorder(tests, winid, r, 5)
2360 call AddItemInPopupBorder(tests, winid, r, 18)
2361 call AddItemInPopupBorder(tests, winid, r, 19)
2362 endfor
2363 " text "short"
2364 call AddItemInPopupText(tests, winid, 4, 6, 1, 1)
2365 call AddItemInPopupText(tests, winid, 4, 10, 1, 5)
2366 call AddItemInPopupText(tests, winid, 4, 11, 1, 6)
2367 call AddItemInPopupText(tests, winid, 4, 17, 1, 6)
2368 " text "long line th"
2369 call AddItemInPopupText(tests, winid, 5, 6, 2, 1)
2370 call AddItemInPopupText(tests, winid, 5, 10, 2, 5)
2371 call AddItemInPopupText(tests, winid, 5, 17, 2, 12)
2372 " text "at will wrap"
2373 call AddItemInPopupText(tests, winid, 6, 6, 2, 13)
2374 call AddItemInPopupText(tests, winid, 6, 10, 2, 17)
2375 call AddItemInPopupText(tests, winid, 6, 17, 2, 24)
2376 " text "other"
2377 call AddItemInPopupText(tests, winid, 7, 6, 3, 1)
2378 call AddItemInPopupText(tests, winid, 7, 10, 3, 5)
2379 call AddItemInPopupText(tests, winid, 7, 11, 3, 6)
2380 call AddItemInPopupText(tests, winid, 7, 17, 3, 6)
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002381
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002382 for item in tests
2383 call test_setmouse(item.clickrow, item.clickcol)
2384 call feedkeys("\<LeftMouse>", 'xt')
2385 call assert_equal(item.result, g:got_mousepos)
2386 endfor
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002387
2388 call popup_close(winid)
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002389 enew!
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002390 delfunc MyPopupFilter
2391endfunc
2392
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002393func Test_popupwin_with_buffer()
2394 call writefile(['some text', 'in a buffer'], 'XsomeFile')
2395 let buf = bufadd('XsomeFile')
2396 call assert_equal(0, bufloaded(buf))
Bram Moolenaar46451042019-08-24 15:50:46 +02002397
2398 setlocal number
2399 call setbufvar(buf, "&wrapmargin", 13)
2400
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002401 let winid = popup_create(buf, {})
2402 call assert_notequal(0, winid)
2403 let pos = popup_getpos(winid)
2404 call assert_equal(2, pos.height)
2405 call assert_equal(1, bufloaded(buf))
Bram Moolenaar46451042019-08-24 15:50:46 +02002406
2407 " window-local option is set to default, buffer-local is not
2408 call assert_equal(0, getwinvar(winid, '&number'))
2409 call assert_equal(13, getbufvar(buf, '&wrapmargin'))
2410
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002411 call popup_close(winid)
2412 call assert_equal({}, popup_getpos(winid))
2413 call assert_equal(1, bufloaded(buf))
2414 exe 'bwipe! ' .. buf
Bram Moolenaar46451042019-08-24 15:50:46 +02002415 setlocal nonumber
Bram Moolenaar7866b872019-07-01 22:21:01 +02002416
2417 edit test_popupwin.vim
2418 let winid = popup_create(bufnr(''), {})
2419 redraw
2420 call popup_close(winid)
Bram Moolenaar3940ec62019-07-05 21:53:24 +02002421 call delete('XsomeFile')
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002422endfunc
Bram Moolenaare296e312019-07-03 23:20:18 +02002423
Bram Moolenaare0d749a2019-09-25 22:14:48 +02002424func Test_popupwin_terminal_buffer()
Bram Moolenaard2c1fb42019-09-25 23:06:40 +02002425 CheckFeature terminal
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002426 CheckUnix
Bram Moolenaare06a28f2020-05-13 23:24:12 +02002427 " Starting a terminal to run a shell in is considered flaky.
2428 let g:test_is_flaky = 1
Bram Moolenaard2c1fb42019-09-25 23:06:40 +02002429
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002430 let origwin = win_getid()
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002431 let termbuf = term_start(&shell, #{hidden: 1})
2432 let winid = popup_create(termbuf, #{minwidth: 40, minheight: 10})
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002433 " Wait for shell to start
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002434 call WaitForAssert({-> assert_equal("run", job_status(term_getjob(termbuf)))})
Bram Moolenaare06a28f2020-05-13 23:24:12 +02002435 sleep 100m
Bram Moolenaar631ebc42020-02-03 22:15:26 +01002436 " Check this doesn't crash
2437 call assert_equal(winnr(), winnr('j'))
2438 call assert_equal(winnr(), winnr('k'))
2439 call assert_equal(winnr(), winnr('h'))
2440 call assert_equal(winnr(), winnr('l'))
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02002441
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002442 " Cannot quit while job is running
2443 call assert_fails('call feedkeys("\<C-W>:quit\<CR>", "xt")', 'E948:')
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02002444
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002445 " Cannot enter Terminal-Normal mode. (TODO: but it works...)
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02002446 call feedkeys("xxx\<C-W>N", 'xt')
2447 call assert_fails('call feedkeys("gf", "xt")', 'E863:')
2448 call feedkeys("a\<C-U>", 'xt')
2449
Bram Moolenaar3f65c662020-05-27 23:15:16 +02002450 " Cannot escape from terminal window
2451 call assert_fails('tab drop xxx', 'E863:')
2452
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002453 " Cannot open a second one.
2454 let termbuf2 = term_start(&shell, #{hidden: 1})
2455 call assert_fails('call popup_create(termbuf2, #{})', 'E861:')
2456 call term_sendkeys(termbuf2, "exit\<CR>")
2457
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02002458 " Exiting shell closes popup window
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002459 call feedkeys("exit\<CR>", 'xt')
2460 " Wait for shell to exit
Bram Moolenaarb2b218d2020-06-22 20:22:19 +02002461 call WaitForAssert({-> assert_equal("dead", job_status(term_getjob(termbuf)))})
Bram Moolenaar373c6512020-06-22 19:24:23 +02002462
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002463 call feedkeys(":quit\<CR>", 'xt')
2464 call assert_equal(origwin, win_getid())
Bram Moolenaare0d749a2019-09-25 22:14:48 +02002465endfunc
2466
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002467func Test_popupwin_close_prevwin()
2468 CheckFeature terminal
2469
2470 call assert_equal(1, winnr('$'))
2471 split
2472 wincmd b
2473 call assert_equal(2, winnr())
2474 let buf = term_start(&shell, #{hidden: 1})
2475 call popup_create(buf, {})
2476 call term_wait(buf, 100)
2477 call popup_clear(1)
2478 call assert_equal(2, winnr())
2479
2480 quit
2481 exe 'bwipe! ' .. buf
2482endfunc
2483
Bram Moolenaar934470e2019-09-01 23:27:05 +02002484func Test_popupwin_with_buffer_and_filter()
2485 new Xwithfilter
2486 call setline(1, range(100))
2487 let bufnr = bufnr()
2488 hide
2489
2490 func BufferFilter(win, key)
2491 if a:key == 'G'
2492 " recursive use of "G" does not cause problems.
2493 call win_execute(a:win, 'normal! G')
2494 return 1
2495 endif
2496 return 0
2497 endfunc
2498
2499 let winid = popup_create(bufnr, #{maxheight: 5, filter: 'BufferFilter'})
2500 call assert_equal(1, popup_getpos(winid).firstline)
2501 redraw
2502 call feedkeys("G", 'xt')
2503 call assert_equal(99, popup_getpos(winid).firstline)
2504
2505 call popup_close(winid)
2506 exe 'bwipe! ' .. bufnr
2507endfunc
2508
Bram Moolenaare296e312019-07-03 23:20:18 +02002509func Test_popupwin_width()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002510 let winid = popup_create(repeat(['short', 'long long long line', 'medium width'], 50), #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002511 \ maxwidth: 40,
2512 \ maxheight: 10,
Bram Moolenaare296e312019-07-03 23:20:18 +02002513 \ })
2514 for top in range(1, 20)
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002515 eval winid->popup_setoptions(#{firstline: top})
Bram Moolenaare296e312019-07-03 23:20:18 +02002516 redraw
2517 call assert_equal(19, popup_getpos(winid).width)
2518 endfor
2519 call popup_clear()
2520endfunc
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02002521
2522func Test_popupwin_buf_close()
2523 let buf = bufadd('Xtestbuf')
2524 call bufload(buf)
2525 call setbufline(buf, 1, ['just', 'some', 'lines'])
2526 let winid = popup_create(buf, {})
2527 redraw
2528 call assert_equal(3, popup_getpos(winid).height)
2529 let bufinfo = getbufinfo(buf)[0]
2530 call assert_equal(1, bufinfo.changed)
2531 call assert_equal(0, bufinfo.hidden)
2532 call assert_equal(0, bufinfo.listed)
2533 call assert_equal(1, bufinfo.loaded)
2534 call assert_equal([], bufinfo.windows)
2535 call assert_equal([winid], bufinfo.popups)
2536
2537 call popup_close(winid)
2538 call assert_equal({}, popup_getpos(winid))
2539 let bufinfo = getbufinfo(buf)[0]
2540 call assert_equal(1, bufinfo.changed)
2541 call assert_equal(1, bufinfo.hidden)
2542 call assert_equal(0, bufinfo.listed)
2543 call assert_equal(1, bufinfo.loaded)
2544 call assert_equal([], bufinfo.windows)
2545 call assert_equal([], bufinfo.popups)
2546 exe 'bwipe! ' .. buf
2547endfunc
Bram Moolenaar017c2692019-07-13 14:17:51 +02002548
2549func Test_popup_menu_with_maxwidth()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002550 CheckScreendump
Bram Moolenaar017c2692019-07-13 14:17:51 +02002551
2552 let lines =<< trim END
2553 call setline(1, range(1, 10))
2554 hi ScrollThumb ctermbg=blue
2555 hi ScrollBar ctermbg=red
2556 func PopupMenu(lines, line, col, scrollbar = 0)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002557 return popup_menu(a:lines, #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002558 \ maxwidth: 10,
2559 \ maxheight: 3,
2560 \ pos : 'topleft',
2561 \ col : a:col,
2562 \ line : a:line,
2563 \ scrollbar : a:scrollbar,
Bram Moolenaar017c2692019-07-13 14:17:51 +02002564 \ })
2565 endfunc
2566 call PopupMenu(['x'], 1, 1)
2567 call PopupMenu(['123456789|'], 1, 16)
2568 call PopupMenu(['123456789|' .. ' '], 7, 1)
2569 call PopupMenu([repeat('123456789|', 100)], 7, 16)
2570 call PopupMenu(repeat(['123456789|' .. ' '], 5), 1, 33, 1)
2571 END
2572 call writefile(lines, 'XtestPopupMenuMaxWidth')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002573 let buf = RunVimInTerminal('-S XtestPopupMenuMaxWidth', #{rows: 13})
Bram Moolenaar017c2692019-07-13 14:17:51 +02002574 call VerifyScreenDump(buf, 'Test_popupwin_menu_maxwidth_1', {})
2575
2576 " close the menu popupwin.
2577 call term_sendkeys(buf, " ")
2578 call term_sendkeys(buf, " ")
2579 call term_sendkeys(buf, " ")
2580 call term_sendkeys(buf, " ")
2581 call term_sendkeys(buf, " ")
2582
2583 " clean up
2584 call StopVimInTerminal(buf)
2585 call delete('XtestPopupMenuMaxWidth')
2586endfunc
2587
Bram Moolenaara901a372019-07-13 16:38:50 +02002588func Test_popup_menu_with_scrollbar()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002589 CheckScreendump
Bram Moolenaara901a372019-07-13 16:38:50 +02002590
2591 let lines =<< trim END
2592 call setline(1, range(1, 20))
2593 hi ScrollThumb ctermbg=blue
2594 hi ScrollBar ctermbg=red
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002595 eval ['one', 'two', 'three', 'four', 'five',
2596 \ 'six', 'seven', 'eight', 'nine']
2597 \ ->popup_menu(#{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002598 \ minwidth: 8,
2599 \ maxheight: 3,
Bram Moolenaara901a372019-07-13 16:38:50 +02002600 \ })
2601 END
2602 call writefile(lines, 'XtestPopupMenuScroll')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002603 let buf = RunVimInTerminal('-S XtestPopupMenuScroll', #{rows: 10})
Bram Moolenaara901a372019-07-13 16:38:50 +02002604
2605 call term_sendkeys(buf, "j")
2606 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_1', {})
2607
2608 call term_sendkeys(buf, "jjj")
2609 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_2', {})
2610
2611 " if the cursor is the bottom line, it stays at the bottom line.
2612 call term_sendkeys(buf, repeat("j", 20))
2613 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_3', {})
2614
2615 call term_sendkeys(buf, "kk")
2616 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_4', {})
2617
2618 call term_sendkeys(buf, "k")
2619 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_5', {})
2620
2621 " if the cursor is in the top line, it stays in the top line.
2622 call term_sendkeys(buf, repeat("k", 20))
2623 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_6', {})
2624
2625 " close the menu popupwin.
2626 call term_sendkeys(buf, " ")
2627
2628 " clean up
2629 call StopVimInTerminal(buf)
2630 call delete('XtestPopupMenuScroll')
2631endfunc
2632
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002633func Test_popup_menu_filter()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002634 CheckScreendump
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002635
2636 let lines =<< trim END
2637 function! MyFilter(winid, key) abort
2638 if a:key == "0"
2639 call win_execute(a:winid, "call setpos('.', [0, 1, 1, 0])")
2640 return 1
2641 endif
2642 if a:key == "G"
2643 call win_execute(a:winid, "call setpos('.', [0, line('$'), 1, 0])")
2644 return 1
2645 endif
2646 if a:key == "j"
2647 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0])")
2648 return 1
2649 endif
2650 if a:key == "k"
2651 call win_execute(a:winid, "call setpos('.', [0, line('.') - 1, 1, 0])")
2652 return 1
2653 endif
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02002654 if a:key == ':'
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002655 call popup_close(a:winid)
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02002656 return 0
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002657 endif
2658 return 0
2659 endfunction
2660 call popup_menu(['111', '222', '333', '444', '555', '666', '777', '888', '999'], #{
2661 \ maxheight : 3,
2662 \ filter : 'MyFilter'
2663 \ })
2664 END
2665 call writefile(lines, 'XtestPopupMenuFilter')
2666 let buf = RunVimInTerminal('-S XtestPopupMenuFilter', #{rows: 10})
2667
2668 call term_sendkeys(buf, "j")
2669 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_1', {})
2670
2671 call term_sendkeys(buf, "k")
2672 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_2', {})
2673
2674 call term_sendkeys(buf, "G")
2675 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_3', {})
2676
2677 call term_sendkeys(buf, "0")
2678 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_4', {})
2679
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02002680 " check that when the popup is closed in the filter the screen is redrawn
2681 call term_sendkeys(buf, ":")
2682 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_5', {})
2683 call term_sendkeys(buf, "\<CR>")
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002684
2685 " clean up
2686 call StopVimInTerminal(buf)
2687 call delete('XtestPopupMenuFilter')
2688endfunc
2689
2690func Test_popup_cursorline()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002691 CheckScreendump
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002692
2693 let winid = popup_create('some text', {})
2694 call assert_equal(0, popup_getoptions(winid).cursorline)
2695 call popup_close(winid)
2696
2697 let winid = popup_create('some text', #{ cursorline: 1, })
2698 call assert_equal(1, popup_getoptions(winid).cursorline)
2699 call popup_close(winid)
2700
2701 let winid = popup_create('some text', #{ cursorline: 0, })
2702 call assert_equal(0, popup_getoptions(winid).cursorline)
2703 call popup_close(winid)
2704
2705 let winid = popup_menu('some text', {})
2706 call assert_equal(1, popup_getoptions(winid).cursorline)
2707 call popup_close(winid)
2708
2709 let winid = popup_menu('some text', #{ cursorline: 1, })
2710 call assert_equal(1, popup_getoptions(winid).cursorline)
2711 call popup_close(winid)
2712
2713 let winid = popup_menu('some text', #{ cursorline: 0, })
2714 call assert_equal(0, popup_getoptions(winid).cursorline)
2715 call popup_close(winid)
2716
2717 " ---------
2718 " Pattern 1
2719 " ---------
2720 let lines =<< trim END
2721 call popup_create(['111', '222', '333'], #{ cursorline : 0 })
2722 END
2723 call writefile(lines, 'XtestPopupCursorLine')
2724 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2725 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_1', {})
2726 call term_sendkeys(buf, ":call popup_clear()\<cr>")
2727 call StopVimInTerminal(buf)
2728
2729 " ---------
2730 " Pattern 2
2731 " ---------
2732 let lines =<< trim END
2733 call popup_create(['111', '222', '333'], #{ cursorline : 1 })
2734 END
2735 call writefile(lines, 'XtestPopupCursorLine')
2736 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2737 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_2', {})
2738 call term_sendkeys(buf, ":call popup_clear()\<cr>")
2739 call StopVimInTerminal(buf)
2740
2741 " ---------
2742 " Pattern 3
2743 " ---------
2744 let lines =<< trim END
2745 function! MyFilter(winid, key) abort
2746 if a:key == "j"
2747 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0]) | redraw")
2748 return 1
2749 endif
2750 if a:key == 'x'
2751 call popup_close(a:winid)
2752 return 1
2753 endif
2754 return 0
2755 endfunction
2756 call popup_menu(['111', '222', '333'], #{
2757 \ cursorline : 0,
2758 \ maxheight : 2,
2759 \ filter : 'MyFilter',
2760 \ })
2761 END
2762 call writefile(lines, 'XtestPopupCursorLine')
2763 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2764 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_3', {})
2765 call term_sendkeys(buf, "j")
2766 call term_sendkeys(buf, "j")
2767 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_4', {})
2768 call term_sendkeys(buf, "x")
2769 call StopVimInTerminal(buf)
2770
2771 " ---------
2772 " Pattern 4
2773 " ---------
2774 let lines =<< trim END
2775 function! MyFilter(winid, key) abort
2776 if a:key == "j"
2777 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0]) | redraw")
2778 return 1
2779 endif
2780 if a:key == 'x'
2781 call popup_close(a:winid)
2782 return 1
2783 endif
2784 return 0
2785 endfunction
2786 call popup_menu(['111', '222', '333'], #{
2787 \ cursorline : 1,
2788 \ maxheight : 2,
2789 \ filter : 'MyFilter',
2790 \ })
2791 END
2792 call writefile(lines, 'XtestPopupCursorLine')
2793 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2794 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_5', {})
2795 call term_sendkeys(buf, "j")
2796 call term_sendkeys(buf, "j")
2797 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_6', {})
2798 call term_sendkeys(buf, "x")
2799 call StopVimInTerminal(buf)
2800
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02002801 " ---------
2802 " Cursor in second line when creating the popup
2803 " ---------
2804 let lines =<< trim END
2805 let winid = popup_create(['111', '222', '333'], #{
2806 \ cursorline : 1,
2807 \ })
2808 call win_execute(winid, "2")
2809 END
2810 call writefile(lines, 'XtestPopupCursorLine')
2811 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2812 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_7', {})
2813 call StopVimInTerminal(buf)
2814
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002815 call delete('XtestPopupCursorLine')
Bram Moolenaar4eb7dae2019-11-12 22:33:45 +01002816
2817 " ---------
2818 " Use current buffer for popupmenu
2819 " ---------
2820 let lines =<< trim END
2821 call setline(1, ['one', 'two', 'three'])
2822 let winid = popup_create(bufnr('%'), #{
2823 \ cursorline : 1,
2824 \ })
2825 call win_execute(winid, "2")
2826 END
2827 call writefile(lines, 'XtestPopupCursorLine')
2828 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
2829 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_8', {})
2830 call StopVimInTerminal(buf)
2831
2832 call delete('XtestPopupCursorLine')
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002833endfunc
2834
Bram Moolenaarf914a332019-07-20 15:09:56 +02002835func Test_previewpopup()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002836 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002837 CheckFeature quickfix
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002838
Bram Moolenaarf914a332019-07-20 15:09:56 +02002839 call writefile([
2840 \ "!_TAG_FILE_ENCODING\tutf-8\t//",
2841 \ "another\tXtagfile\t/^this is another",
2842 \ "theword\tXtagfile\t/^theword"],
2843 \ 'Xtags')
2844 call writefile(range(1,20)
2845 \ + ['theword is here']
2846 \ + range(22, 27)
2847 \ + ['this is another place']
2848 \ + range(29, 40),
2849 \ "Xtagfile")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002850 call writefile(range(1,10)
2851 \ + ['searched word is here']
2852 \ + range(12, 20),
2853 \ "Xheader.h")
Bram Moolenaarf914a332019-07-20 15:09:56 +02002854 let lines =<< trim END
2855 set tags=Xtags
2856 call setline(1, [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002857 \ 'one',
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002858 \ '#include "Xheader.h"',
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002859 \ 'three',
2860 \ 'four',
2861 \ 'five',
2862 \ 'six',
2863 \ 'seven',
2864 \ 'find theword somewhere',
2865 \ 'nine',
2866 \ 'this is another word',
2867 \ 'very long line where the word is also another'])
Bram Moolenaarf914a332019-07-20 15:09:56 +02002868 set previewpopup=height:4,width:40
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002869 set path=.
Bram Moolenaarf914a332019-07-20 15:09:56 +02002870 END
2871 call writefile(lines, 'XtestPreviewPopup')
2872 let buf = RunVimInTerminal('-S XtestPreviewPopup', #{rows: 14})
2873
2874 call term_sendkeys(buf, "/theword\<CR>\<C-W>}")
2875 call term_sendkeys(buf, ":\<CR>")
2876 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_1', {})
2877
2878 call term_sendkeys(buf, "/another\<CR>\<C-W>}")
2879 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_2', {})
2880
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002881 call term_sendkeys(buf, ":call popup_move(popup_findpreview(), #{col: 15})\<CR>")
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002882 call term_sendkeys(buf, ":\<CR>")
2883 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_3', {})
2884
2885 call term_sendkeys(buf, "/another\<CR>\<C-W>}")
2886 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_4', {})
2887
Bram Moolenaar799439a2020-02-11 21:44:17 +01002888 call term_sendkeys(buf, ":silent cd ..\<CR>:\<CR>")
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002889 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_5', {})
Bram Moolenaar799439a2020-02-11 21:44:17 +01002890 call term_sendkeys(buf, ":silent cd testdir\<CR>")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002891
2892 call term_sendkeys(buf, ":pclose\<CR>")
Bram Moolenaar78d629a2019-08-16 17:31:15 +02002893 call term_sendkeys(buf, ":\<BS>")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002894 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_6', {})
2895
2896 call term_sendkeys(buf, ":pedit +/theword Xtagfile\<CR>")
2897 call term_sendkeys(buf, ":\<CR>")
2898 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_7', {})
2899
2900 call term_sendkeys(buf, ":pclose\<CR>")
2901 call term_sendkeys(buf, ":psearch searched\<CR>")
2902 call term_sendkeys(buf, ":\<CR>")
2903 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_8', {})
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02002904
Bram Moolenaar8bf716c2020-01-23 15:33:54 +01002905 call term_sendkeys(buf, "\<C-W>p")
2906 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_9', {})
2907
2908 call term_sendkeys(buf, ":call win_execute(popup_findpreview(), 'call popup_clear()')\<CR>")
2909 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_10', {})
2910
Bram Moolenaarf914a332019-07-20 15:09:56 +02002911 call StopVimInTerminal(buf)
2912 call delete('Xtags')
2913 call delete('Xtagfile')
2914 call delete('XtestPreviewPopup')
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02002915 call delete('Xheader.h')
Bram Moolenaarf914a332019-07-20 15:09:56 +02002916endfunc
2917
Bram Moolenaarbd483b32019-08-21 15:13:41 +02002918func Get_popupmenu_lines()
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002919 let lines =<< trim END
2920 set completeopt+=preview,popup
2921 set completefunc=CompleteFuncDict
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02002922 hi InfoPopup ctermbg=yellow
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002923
2924 func CompleteFuncDict(findstart, base)
2925 if a:findstart
2926 if col('.') > 10
2927 return col('.') - 10
2928 endif
2929 return 0
2930 endif
2931
2932 return {
2933 \ 'words': [
2934 \ {
2935 \ 'word': 'aword',
2936 \ 'abbr': 'wrd',
2937 \ 'menu': 'extra text',
2938 \ 'info': 'words are cool',
2939 \ 'kind': 'W',
2940 \ 'user_data': 'test'
2941 \ },
2942 \ {
2943 \ 'word': 'anotherword',
2944 \ 'abbr': 'anotwrd',
2945 \ 'menu': 'extra text',
2946 \ 'info': "other words are\ncooler than this and some more text\nto make wrap",
2947 \ 'kind': 'W',
2948 \ 'user_data': 'notest'
2949 \ },
2950 \ {
2951 \ 'word': 'noinfo',
2952 \ 'abbr': 'noawrd',
2953 \ 'menu': 'extra text',
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02002954 \ 'info': "lets\nshow\na\nscrollbar\nhere",
Bram Moolenaar576a4a62019-08-18 15:25:17 +02002955 \ 'kind': 'W',
2956 \ 'user_data': 'notest'
2957 \ },
2958 \ {
2959 \ 'word': 'thatword',
2960 \ 'abbr': 'thatwrd',
2961 \ 'menu': 'extra text',
2962 \ 'info': 'that word is cool',
2963 \ 'kind': 'W',
2964 \ 'user_data': 'notest'
2965 \ },
2966 \ ]
2967 \ }
2968 endfunc
2969 call setline(1, 'text text text text text text text ')
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002970 func ChangeColor()
2971 let id = popup_findinfo()
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002972 eval id->popup_setoptions(#{highlight: 'InfoPopup'})
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02002973 endfunc
Bram Moolenaardca7abe2019-10-20 18:17:57 +02002974
2975 func InfoHidden()
2976 set completepopup=height:4,border:off,align:menu
2977 set completeopt-=popup completeopt+=popuphidden
2978 au CompleteChanged * call HandleChange()
2979 endfunc
2980
2981 let s:counter = 0
2982 func HandleChange()
2983 let s:counter += 1
2984 let selected = complete_info(['selected']).selected
2985 if selected <= 0
2986 " First time: do nothing, info remains hidden
2987 return
2988 endif
2989 if selected == 1
2990 " Second time: show info right away
2991 let id = popup_findinfo()
2992 if id
2993 call popup_settext(id, 'immediate info ' .. s:counter)
2994 call popup_show(id)
2995 endif
2996 else
2997 " Third time: show info after a short delay
2998 call timer_start(100, 'ShowInfo')
2999 endif
3000 endfunc
3001
3002 func ShowInfo(...)
3003 let id = popup_findinfo()
3004 if id
3005 call popup_settext(id, 'async info ' .. s:counter)
3006 call popup_show(id)
3007 endif
3008 endfunc
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003009 END
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003010 return lines
3011endfunc
3012
3013func Test_popupmenu_info_border()
3014 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003015 CheckFeature quickfix
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003016
3017 let lines = Get_popupmenu_lines()
3018 call add(lines, 'set completepopup=height:4,highlight:InfoPopup')
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003019 call writefile(lines, 'XtestInfoPopup')
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003020
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003021 let buf = RunVimInTerminal('-S XtestInfoPopup', #{rows: 14})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003022 call TermWait(buf, 25)
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003023
3024 call term_sendkeys(buf, "A\<C-X>\<C-U>")
3025 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_1', {})
3026
3027 call term_sendkeys(buf, "\<C-N>")
3028 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_2', {})
3029
3030 call term_sendkeys(buf, "\<C-N>")
3031 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_3', {})
3032
3033 call term_sendkeys(buf, "\<C-N>\<C-N>")
3034 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_4', {})
3035
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02003036 " info on the left with scrollbar
3037 call term_sendkeys(buf, "test text test text\<C-X>\<C-U>")
3038 call term_sendkeys(buf, "\<C-N>\<C-N>")
3039 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_5', {})
3040
Bram Moolenaar202c3f72019-11-21 12:12:35 +01003041 " Test that the popupmenu's scrollbar and infopopup do not overlap
3042 call term_sendkeys(buf, "\<Esc>")
3043 call term_sendkeys(buf, ":set pumheight=3\<CR>")
3044 call term_sendkeys(buf, "cc\<C-X>\<C-U>")
3045 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_6', {})
3046
Bram Moolenaarca7c0782020-01-14 20:42:48 +01003047 " Hide the info popup, cycle trough buffers, make sure it didn't get
3048 " deleted.
3049 call term_sendkeys(buf, "\<Esc>")
3050 call term_sendkeys(buf, ":set hidden\<CR>")
3051 call term_sendkeys(buf, ":bn\<CR>")
3052 call term_sendkeys(buf, ":bn\<CR>")
3053 call term_sendkeys(buf, "otest text test text\<C-X>\<C-U>")
3054 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_7', {})
3055
3056 call term_sendkeys(buf, "\<Esc>")
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003057 call StopVimInTerminal(buf)
3058 call delete('XtestInfoPopup')
3059endfunc
3060
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003061func Test_popupmenu_info_noborder()
3062 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003063 CheckFeature quickfix
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003064
3065 let lines = Get_popupmenu_lines()
3066 call add(lines, 'set completepopup=height:4,border:off')
3067 call writefile(lines, 'XtestInfoPopupNb')
3068
3069 let buf = RunVimInTerminal('-S XtestInfoPopupNb', #{rows: 14})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003070 call TermWait(buf, 25)
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003071
3072 call term_sendkeys(buf, "A\<C-X>\<C-U>")
3073 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_nb_1', {})
3074
3075 call StopVimInTerminal(buf)
3076 call delete('XtestInfoPopupNb')
3077endfunc
3078
Bram Moolenaar258cef52019-08-21 17:29:29 +02003079func Test_popupmenu_info_align_menu()
3080 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003081 CheckFeature quickfix
Bram Moolenaar258cef52019-08-21 17:29:29 +02003082
3083 let lines = Get_popupmenu_lines()
3084 call add(lines, 'set completepopup=height:4,border:off,align:menu')
3085 call writefile(lines, 'XtestInfoPopupNb')
3086
3087 let buf = RunVimInTerminal('-S XtestInfoPopupNb', #{rows: 14})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003088 call TermWait(buf, 25)
Bram Moolenaar258cef52019-08-21 17:29:29 +02003089
3090 call term_sendkeys(buf, "A\<C-X>\<C-U>")
3091 call term_sendkeys(buf, "\<C-N>")
3092 call term_sendkeys(buf, "\<C-N>")
3093 call term_sendkeys(buf, "\<C-N>")
3094 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_1', {})
3095
3096 call term_sendkeys(buf, "test text test text test\<C-X>\<C-U>")
3097 call term_sendkeys(buf, "\<C-N>")
3098 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_2', {})
3099
3100 call term_sendkeys(buf, "\<Esc>")
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003101 call term_sendkeys(buf, ":call ChangeColor()\<CR>")
Bram Moolenaar258cef52019-08-21 17:29:29 +02003102 call term_sendkeys(buf, ":call setline(2, ['x']->repeat(10))\<CR>")
3103 call term_sendkeys(buf, "Gotest text test text\<C-X>\<C-U>")
3104 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_3', {})
3105
3106 call StopVimInTerminal(buf)
3107 call delete('XtestInfoPopupNb')
3108endfunc
3109
Bram Moolenaardca7abe2019-10-20 18:17:57 +02003110func Test_popupmenu_info_hidden()
3111 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003112 CheckFeature quickfix
Bram Moolenaardca7abe2019-10-20 18:17:57 +02003113
3114 let lines = Get_popupmenu_lines()
3115 call add(lines, 'call InfoHidden()')
3116 call writefile(lines, 'XtestInfoPopupHidden')
3117
3118 let buf = RunVimInTerminal('-S XtestInfoPopupHidden', #{rows: 14})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003119 call TermWait(buf, 25)
Bram Moolenaardca7abe2019-10-20 18:17:57 +02003120
3121 call term_sendkeys(buf, "A\<C-X>\<C-U>")
3122 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_hidden_1', {})
3123
3124 call term_sendkeys(buf, "\<C-N>")
3125 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_hidden_2', {})
3126
3127 call term_sendkeys(buf, "\<C-N>")
3128 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_hidden_3', {})
3129
3130 call term_sendkeys(buf, "\<Esc>")
3131 call StopVimInTerminal(buf)
3132 call delete('XtestInfoPopupHidden')
3133endfunc
3134
Bram Moolenaarbef93ac2019-12-06 20:17:35 +01003135func Test_popupmenu_info_too_wide()
3136 CheckScreendump
3137 CheckFeature quickfix
3138
3139 let lines =<< trim END
3140 call setline(1, range(10))
3141
3142 set completeopt+=preview,popup
3143 set completepopup=align:menu
3144 set omnifunc=OmniFunc
3145 hi InfoPopup ctermbg=lightgrey
3146
3147 func OmniFunc(findstart, base)
3148 if a:findstart
3149 return 0
3150 endif
3151
3152 let menuText = 'some long text to make sure the menu takes up all of the width of the window'
3153 return #{
3154 \ words: [
3155 \ #{
3156 \ word: 'scrap',
3157 \ menu: menuText,
3158 \ info: "other words are\ncooler than this and some more text\nto make wrap",
3159 \ },
3160 \ #{
3161 \ word: 'scappier',
3162 \ menu: menuText,
3163 \ info: 'words are cool',
3164 \ },
3165 \ #{
3166 \ word: 'scrappier2',
3167 \ menu: menuText,
3168 \ info: 'words are cool',
3169 \ },
3170 \ ]
3171 \ }
3172 endfunc
3173 END
3174
3175 call writefile(lines, 'XtestInfoPopupWide')
3176 let buf = RunVimInTerminal('-S XtestInfoPopupWide', #{rows: 8})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003177 call TermWait(buf, 25)
Bram Moolenaarbef93ac2019-12-06 20:17:35 +01003178
3179 call term_sendkeys(buf, "Ascr\<C-X>\<C-O>")
3180 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_wide_1', {})
3181
3182 call term_sendkeys(buf, "\<Esc>")
3183 call StopVimInTerminal(buf)
3184 call delete('XtestInfoPopupWide')
3185endfunc
3186
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02003187func Test_popupwin_recycle_bnr()
Bram Moolenaare49fbff2019-08-21 22:50:07 +02003188 let bufnr = popup_notification('nothing wrong', {})->winbufnr()
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02003189 call popup_clear()
Bram Moolenaar6a124e62019-09-04 18:15:19 +02003190 let winid = 'nothing wrong'->popup_notification({})
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02003191 call assert_equal(bufnr, winbufnr(winid))
3192 call popup_clear()
3193endfunc
3194
Bram Moolenaar1824f452019-10-02 23:06:46 +02003195func Test_popupwin_getoptions_tablocal()
3196 topleft split
3197 let win1 = popup_create('nothing', #{maxheight: 8})
3198 let win2 = popup_create('something', #{maxheight: 10})
3199 let win3 = popup_create('something', #{maxheight: 15})
3200 call assert_equal(8, popup_getoptions(win1).maxheight)
3201 call assert_equal(10, popup_getoptions(win2).maxheight)
3202 call assert_equal(15, popup_getoptions(win3).maxheight)
3203 call popup_clear()
3204 quit
3205endfunc
3206
Bram Moolenaare8a7dfe2019-10-03 22:35:52 +02003207func Test_popupwin_cancel()
3208 let win1 = popup_create('one', #{line: 5, filter: {... -> 0}})
3209 let win2 = popup_create('two', #{line: 10, filter: {... -> 0}})
3210 let win3 = popup_create('three', #{line: 15, filter: {... -> 0}})
3211 call assert_equal(5, popup_getpos(win1).line)
3212 call assert_equal(10, popup_getpos(win2).line)
3213 call assert_equal(15, popup_getpos(win3).line)
3214 " TODO: this also works without patch 8.1.2110
3215 call feedkeys("\<C-C>", 'xt')
3216 call assert_equal(5, popup_getpos(win1).line)
3217 call assert_equal(10, popup_getpos(win2).line)
3218 call assert_equal({}, popup_getpos(win3))
3219 call feedkeys("\<C-C>", 'xt')
3220 call assert_equal(5, popup_getpos(win1).line)
3221 call assert_equal({}, popup_getpos(win2))
3222 call assert_equal({}, popup_getpos(win3))
3223 call feedkeys("\<C-C>", 'xt')
3224 call assert_equal({}, popup_getpos(win1))
3225 call assert_equal({}, popup_getpos(win2))
3226 call assert_equal({}, popup_getpos(win3))
3227endfunc
3228
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003229func Test_popupwin_filter_redraw()
3230 " Create two popups with a filter that closes the popup when typing "0".
3231 " Both popups should close, even though the redraw also calls
3232 " popup_reset_handled()
3233
3234 func CloseFilter(winid, key)
3235 if a:key == '0'
3236 call popup_close(a:winid)
3237 redraw
3238 endif
3239 return 0 " pass the key
3240 endfunc
3241
3242 let id1 = popup_create('first one', #{
3243 \ line: 1,
3244 \ col: 1,
3245 \ filter: 'CloseFilter',
3246 \ })
3247 let id2 = popup_create('second one', #{
3248 \ line: 9,
3249 \ col: 1,
3250 \ filter: 'CloseFilter',
3251 \ })
3252 call assert_equal(1, popup_getpos(id1).line)
3253 call assert_equal(9, popup_getpos(id2).line)
3254
3255 call feedkeys('0', 'xt')
3256 call assert_equal({}, popup_getpos(id1))
3257 call assert_equal({}, popup_getpos(id2))
3258
3259 call popup_clear()
3260 delfunc CloseFilter
3261endfunc
3262
Bram Moolenaar20ebbea2019-11-30 17:58:27 +01003263func Test_popupwin_double_width()
3264 CheckScreendump
3265
3266 let lines =<< trim END
3267 call setline(1, 'x你好世界你好世你好世界你好')
3268 call setline(2, '你好世界你好世你好世界你好')
3269 call setline(3, 'x你好世界你好世你好世界你好')
3270 call popup_create('你好,世界 - 你好,世界xxxxx', #{line: 1, col: 3, maxwidth: 14})
3271 END
3272 call writefile(lines, 'XtestPopupWide')
3273
3274 let buf = RunVimInTerminal('-S XtestPopupWide', #{rows: 10})
3275 call VerifyScreenDump(buf, 'Test_popupwin_doublewidth_1', {})
3276
3277 call StopVimInTerminal(buf)
3278 call delete('XtestPopupWide')
3279endfunc
3280
3281func Test_popupwin_sign()
3282 CheckScreendump
3283
3284 let lines =<< trim END
3285 call setline(1, range(10))
3286 call sign_define('Current', {
3287 \ 'text': '>>',
3288 \ 'texthl': 'WarningMsg',
3289 \ 'linehl': 'Error',
3290 \ })
3291 call sign_define('Other', {
3292 \ 'text': '#!',
3293 \ 'texthl': 'Error',
3294 \ 'linehl': 'Search',
3295 \ })
3296 let winid = popup_create(['hello', 'bright', 'world'], {
3297 \ 'minwidth': 20,
3298 \ })
3299 call setwinvar(winid, "&signcolumn", "yes")
3300 let winbufnr = winbufnr(winid)
3301
3302 " add sign to current buffer, shows
3303 call sign_place(1, 'Selected', 'Current', bufnr('%'), {'lnum': 1})
3304 " add sign to current buffer, does not show
3305 call sign_place(2, 'PopUpSelected', 'Other', bufnr('%'), {'lnum': 2})
3306
3307 " add sign to popup buffer, shows
3308 call sign_place(3, 'PopUpSelected', 'Other', winbufnr, {'lnum': 1})
3309 " add sign to popup buffer, does not show
3310 call sign_place(4, 'Selected', 'Current', winbufnr, {'lnum': 2})
3311 END
3312 call writefile(lines, 'XtestPopupSign')
3313
3314 let buf = RunVimInTerminal('-S XtestPopupSign', #{rows: 10})
3315 call VerifyScreenDump(buf, 'Test_popupwin_sign_1', {})
3316
3317 call StopVimInTerminal(buf)
3318 call delete('XtestPopupSign')
3319endfunc
3320
Bram Moolenaar99ebf222019-12-10 23:44:48 +01003321func Test_popupwin_bufnr()
3322 let popwin = popup_create(['blah'], #{})
3323 let popbuf = winbufnr(popwin)
3324 split asdfasdf
3325 let newbuf = bufnr()
3326 call assert_true(newbuf > popbuf, 'New buffer number is higher')
3327 call assert_equal(newbuf, bufnr('$'))
3328 call popup_clear()
3329 let popwin = popup_create(['blah'], #{})
3330 " reuses previous buffer number
3331 call assert_equal(popbuf, winbufnr(popwin))
3332 call assert_equal(newbuf, bufnr('$'))
3333
3334 call popup_clear()
3335 bwipe!
3336endfunc
3337
Bram Moolenaarec084d32020-02-28 22:44:47 +01003338func Test_popupwin_filter_input_multibyte()
3339 func MyPopupFilter(winid, c)
3340 let g:bytes = range(a:c->strlen())->map({i -> char2nr(a:c[i])})
3341 return 0
3342 endfunc
3343 let winid = popup_create('', #{mapping: 0, filter: 'MyPopupFilter'})
3344
3345 " UTF-8: E3 80 80, including K_SPECIAL(0x80)
3346 call feedkeys("\u3000", 'xt')
3347 call assert_equal([0xe3, 0x80, 0x80], g:bytes)
3348
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01003349 " UTF-8: E3 80 9B, including CSI(0x9B)
3350 call feedkeys("\u301b", 'xt')
3351 call assert_equal([0xe3, 0x80, 0x9b], g:bytes)
Bram Moolenaarec084d32020-02-28 22:44:47 +01003352
Bram Moolenaar20298ce2020-06-19 21:46:52 +02003353 if has('unix')
3354 " with modifyOtherKeys <M-S-a> does not include a modifier sequence
Bram Moolenaarb326edf2020-06-20 15:03:38 +02003355 if has('gui_running')
3356 call feedkeys("\x9b\xfc\x08A", 'Lx!')
3357 else
3358 call feedkeys("\<Esc>[27;4;65~", 'Lx!')
3359 endif
Bram Moolenaar20298ce2020-06-19 21:46:52 +02003360 call assert_equal([0xc3, 0x81], g:bytes)
3361 endif
3362
Bram Moolenaarec084d32020-02-28 22:44:47 +01003363 call popup_clear()
3364 delfunc MyPopupFilter
3365 unlet g:bytes
3366endfunc
3367
Bram Moolenaarba2920f2020-03-06 21:43:17 +01003368func Test_popupwin_atcursor_far_right()
3369 new
3370
3371 " this was getting stuck
3372 set signcolumn=yes
3373 call setline(1, repeat('=', &columns))
3374 normal! ggg$
Bram Moolenaaref6b9792020-05-13 16:34:15 +02003375 let winid = popup_atcursor(repeat('x', 500), #{moved: 'any', border: []})
Bram Moolenaarba2920f2020-03-06 21:43:17 +01003376
Bram Moolenaaref6b9792020-05-13 16:34:15 +02003377 call popup_close(winid)
Bram Moolenaarba2920f2020-03-06 21:43:17 +01003378 bwipe!
3379 set signcolumn&
3380endfunc
3381
Bram Moolenaar0f1563f2020-03-20 21:15:51 +01003382func Test_popupwin_splitmove()
3383 vsplit
3384 let win2 = win_getid()
3385 let popup_winid = popup_dialog('hello', {})
3386 call assert_fails('call win_splitmove(popup_winid, win2)', 'E957:')
3387 call assert_fails('call win_splitmove(win2, popup_winid)', 'E957:')
3388
3389 call popup_clear()
3390 bwipe
3391endfunc
3392
3393
Bram Moolenaar331bafd2019-07-20 17:46:05 +02003394" vim: shiftwidth=2 sts=2