blob: b91689e50ca47666bf089c0453a92e3e6020202b [file] [log] [blame]
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001" Tests for popup windows
2
Bram Moolenaarb46fecd2019-06-15 17:58:09 +02003source check.vim
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004CheckFeature popupwin
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005
6source screendump.vim
7
8func Test_simple_popup()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02009 CheckScreendump
10
Bram Moolenaare7eb9272019-06-24 00:58:07 +020011 let lines =<< trim END
12 call setline(1, range(1, 100))
13 hi PopupColor1 ctermbg=lightblue
14 hi PopupColor2 ctermbg=lightcyan
Bram Moolenaar1666ac92019-11-10 17:22:31 +010015 hi EndOfBuffer ctermbg=lightgrey
Bram Moolenaare7eb9272019-06-24 00:58:07 +020016 hi Comment ctermfg=red
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020017 call prop_type_add('comment', #{highlight: 'Comment'})
18 let winid = popup_create('hello there', #{line: 3, col: 11, minwidth: 20, highlight: 'PopupColor1'})
19 let winid2 = popup_create(['another one', 'another two', 'another three'], #{line: 3, col: 25, minwidth: 20})
Bram Moolenaare7eb9272019-06-24 00:58:07 +020020 call setwinvar(winid2, '&wincolor', 'PopupColor2')
21 END
22 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020023 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar4d784b22019-05-25 19:51:39 +020024 call VerifyScreenDump(buf, 'Test_popupwin_01', {})
25
Bram Moolenaarec583842019-05-26 14:11:23 +020026 " Add a tabpage
27 call term_sendkeys(buf, ":tabnew\<CR>")
Bram Moolenaar60cdb302019-05-27 21:54:10 +020028 call term_sendkeys(buf, ":let popupwin = popup_create(["
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020029 \ .. "#{text: 'other tab'},"
30 \ .. "#{text: 'a comment line', props: [#{"
Bram Moolenaard5abb4c2019-07-13 22:46:10 +020031 \ .. "col: 3, length: 7, minwidth: 20, type: 'comment'"
Bram Moolenaar7a8d0272019-05-26 23:32:06 +020032 \ .. "}]},"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020033 \ .. "], #{line: 4, col: 9, minwidth: 20})\<CR>")
Bram Moolenaarec583842019-05-26 14:11:23 +020034 call VerifyScreenDump(buf, 'Test_popupwin_02', {})
35
36 " switch back to first tabpage
37 call term_sendkeys(buf, "gt")
38 call VerifyScreenDump(buf, 'Test_popupwin_03', {})
39
40 " close that tabpage
41 call term_sendkeys(buf, ":quit!\<CR>")
42 call VerifyScreenDump(buf, 'Test_popupwin_04', {})
43
Bram Moolenaar202d9822019-06-11 21:56:30 +020044 " set 'columns' to a small value, size must be recomputed
45 call term_sendkeys(buf, ":let cols = &columns\<CR>")
46 call term_sendkeys(buf, ":set columns=12\<CR>")
47 call VerifyScreenDump(buf, 'Test_popupwin_04a', {})
48 call term_sendkeys(buf, ":let &columns = cols\<CR>")
49
Bram Moolenaar17146962019-05-30 00:12:11 +020050 " resize popup, show empty line at bottom
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020051 call term_sendkeys(buf, ":call popup_move(popupwin, #{minwidth: 15, maxwidth: 25, minheight: 3, maxheight: 5})\<CR>")
Bram Moolenaar60cdb302019-05-27 21:54:10 +020052 call term_sendkeys(buf, ":redraw\<CR>")
53 call VerifyScreenDump(buf, 'Test_popupwin_05', {})
54
Bram Moolenaar17146962019-05-30 00:12:11 +020055 " show not fitting line at bottom
56 call term_sendkeys(buf, ":call setbufline(winbufnr(popupwin), 3, 'this line will not fit here')\<CR>")
57 call term_sendkeys(buf, ":redraw\<CR>")
58 call VerifyScreenDump(buf, 'Test_popupwin_06', {})
59
Bram Moolenaar24a5ac52019-06-08 19:01:18 +020060 " move popup over ruler
61 call term_sendkeys(buf, ":set cmdheight=2\<CR>")
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020062 call term_sendkeys(buf, ":call popup_move(popupwin, #{line: 7, col: 55})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +020063 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar24a5ac52019-06-08 19:01:18 +020064 call VerifyScreenDump(buf, 'Test_popupwin_07', {})
65
66 " clear all popups after moving the cursor a bit, so that ruler is updated
67 call term_sendkeys(buf, "axxx\<Esc>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +020068 call TermWait(buf)
Bram Moolenaar24a5ac52019-06-08 19:01:18 +020069 call term_sendkeys(buf, "0")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +020070 call TermWait(buf)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +020071 call term_sendkeys(buf, ":call popup_clear()\<CR>")
Bram Moolenaar24a5ac52019-06-08 19:01:18 +020072 call VerifyScreenDump(buf, 'Test_popupwin_08', {})
73
Bram Moolenaar4d784b22019-05-25 19:51:39 +020074 " clean up
75 call StopVimInTerminal(buf)
76 call delete('XtestPopup')
77endfunc
Bram Moolenaar51fe3b12019-05-26 20:10:06 +020078
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020079func Test_popup_with_border_and_padding()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +020080 CheckScreendump
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020081
Bram Moolenaar3bfd04e2019-06-01 20:45:21 +020082 for iter in range(0, 1)
Bram Moolenaare7eb9272019-06-24 00:58:07 +020083 let lines =<< trim END
84 call setline(1, range(1, 100))
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020085 call popup_create('hello border', #{line: 2, col: 3, border: []})
86 call popup_create('hello padding', #{line: 2, col: 23, padding: []})
Bram Moolenaarc363fe12019-08-04 18:13:46 +020087 call popup_create('hello both', #{line: 2, col: 43, border: [], padding: [], highlight: 'Normal'})
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020088 call popup_create('border TL', #{line: 6, col: 3, border: [1, 0, 0, 4]})
Bram Moolenaarb0992022020-01-30 14:55:42 +010089 call popup_create('paddings', #{line: 6, col: 23, padding: range(1, 4)})
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020090 call popup_create('wrapped longer text', #{line: 8, col: 55, padding: [0, 3, 0, 3], border: [0, 1, 0, 1]})
91 call popup_create('right aligned text', #{line: 11, col: 56, wrap: 0, padding: [0, 3, 0, 3], border: [0, 1, 0, 1]})
Bram Moolenaarfbcdf672020-01-06 23:07:48 +010092 call popup_create('X', #{line: 2, col: 73})
93 call popup_create('X', #{line: 3, col: 74})
94 call popup_create('X', #{line: 4, col: 75})
95 call popup_create('X', #{line: 5, col: 76})
Bram Moolenaare7eb9272019-06-24 00:58:07 +020096 END
97 call insert(lines, iter == 1 ? '' : 'set enc=latin1')
98 call writefile(lines, 'XtestPopupBorder')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020099 let buf = RunVimInTerminal('-S XtestPopupBorder', #{rows: 15})
Bram Moolenaar3bfd04e2019-06-01 20:45:21 +0200100 call VerifyScreenDump(buf, 'Test_popupwin_2' .. iter, {})
101
102 call StopVimInTerminal(buf)
103 call delete('XtestPopupBorder')
104 endfor
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200105
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200106 let lines =<< trim END
107 call setline(1, range(1, 100))
108 hi BlueColor ctermbg=lightblue
109 hi TopColor ctermbg=253
110 hi RightColor ctermbg=245
111 hi BottomColor ctermbg=240
112 hi LeftColor ctermbg=248
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200113 call popup_create('hello border', #{line: 2, col: 3, border: [], borderhighlight: ['BlueColor']})
114 call popup_create(['hello border', 'and more'], #{line: 2, col: 23, border: [], borderhighlight: ['TopColor', 'RightColor', 'BottomColor', 'LeftColor']})
115 call popup_create(['hello border', 'lines only'], #{line: 2, col: 43, border: [], borderhighlight: ['BlueColor'], borderchars: ['x']})
116 call popup_create(['hello border', 'with corners'], #{line: 2, col: 60, border: [], borderhighlight: ['BlueColor'], borderchars: ['x', '#']})
117 let winid = popup_create(['hello border', 'with numbers'], #{line: 6, col: 3, border: [], borderhighlight: ['BlueColor'], borderchars: ['0', '1', '2', '3', '4', '5', '6', '7']})
118 call popup_create(['hello border', 'just blanks'], #{line: 7, col: 23, border: [], borderhighlight: ['BlueColor'], borderchars: [' ']})
Bram Moolenaar3dabd712019-07-08 23:30:22 +0200119 func MultiByte()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200120 call popup_create(['hello'], #{line: 8, col: 43, border: [], borderchars: ['─', '│', '─', '│', '┌', '┐', '┘', '└']})
Bram Moolenaar3dabd712019-07-08 23:30:22 +0200121 endfunc
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200122 END
123 call writefile(lines, 'XtestPopupBorder')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200124 let buf = RunVimInTerminal('-S XtestPopupBorder', #{rows: 12})
Bram Moolenaar790498b2019-06-01 22:15:29 +0200125 call VerifyScreenDump(buf, 'Test_popupwin_22', {})
126
Bram Moolenaarad24a712019-06-17 20:05:45 +0200127 " check that changing borderchars triggers a redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200128 call term_sendkeys(buf, ":call popup_setoptions(winid, #{borderchars: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']})\<CR>")
Bram Moolenaarad24a712019-06-17 20:05:45 +0200129 call VerifyScreenDump(buf, 'Test_popupwin_23', {})
130
Bram Moolenaar3dabd712019-07-08 23:30:22 +0200131 " check multi-byte border only with 'ambiwidth' single
132 if &ambiwidth == 'single'
133 call term_sendkeys(buf, ":call MultiByte()\<CR>")
134 call VerifyScreenDump(buf, 'Test_popupwin_24', {})
135 endif
136
Bram Moolenaar790498b2019-06-01 22:15:29 +0200137 call StopVimInTerminal(buf)
138 call delete('XtestPopupBorder')
139
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200140 let with_border_or_padding = #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200141 \ line: 2,
142 \ core_line: 3,
143 \ col: 3,
144 \ core_col: 4,
145 \ width: 14,
146 \ core_width: 12,
147 \ height: 3,
148 \ core_height: 1,
149 \ firstline: 1,
Bram Moolenaar30efcf32019-11-03 22:29:38 +0100150 \ lastline: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200151 \ scrollbar: 0,
152 \ visible: 1}
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200153 let winid = popup_create('hello border', #{line: 2, col: 3, border: []})",
Bram Moolenaar6a124e62019-09-04 18:15:19 +0200154 call assert_equal(with_border_or_padding, winid->popup_getpos())
Bram Moolenaarae943152019-06-16 22:54:14 +0200155 let options = popup_getoptions(winid)
156 call assert_equal([], options.border)
157 call assert_false(has_key(options, "padding"))
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200158
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200159 let winid = popup_create('hello padding', #{line: 2, col: 3, padding: []})
Bram Moolenaarae943152019-06-16 22:54:14 +0200160 let with_border_or_padding.width = 15
161 let with_border_or_padding.core_width = 13
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200162 call assert_equal(with_border_or_padding, popup_getpos(winid))
Bram Moolenaarae943152019-06-16 22:54:14 +0200163 let options = popup_getoptions(winid)
164 call assert_false(has_key(options, "border"))
165 call assert_equal([], options.padding)
166
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200167 call popup_setoptions(winid, #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200168 \ padding: [1, 2, 3, 4],
169 \ border: [4, 0, 7, 8],
170 \ borderhighlight: ['Top', 'Right', 'Bottom', 'Left'],
171 \ borderchars: ['1', '^', '2', '>', '3', 'v', '4', '<'],
Bram Moolenaarae943152019-06-16 22:54:14 +0200172 \ })
173 let options = popup_getoptions(winid)
174 call assert_equal([1, 0, 1, 1], options.border)
175 call assert_equal([1, 2, 3, 4], options.padding)
176 call assert_equal(['Top', 'Right', 'Bottom', 'Left'], options.borderhighlight)
177 call assert_equal(['1', '^', '2', '>', '3', 'v', '4', '<'], options.borderchars)
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200178
Bram Moolenaara1396152019-10-20 18:46:05 +0200179 " Check that popup_setoptions() takes the output of popup_getoptions()
180 call popup_setoptions(winid, options)
181 call assert_equal(options, popup_getoptions(winid))
182
Bram Moolenaarb0992022020-01-30 14:55:42 +0100183 " Check that range() doesn't crash
184 call popup_setoptions(winid, #{
185 \ padding: range(1, 4),
186 \ border: range(5, 8),
187 \ borderhighlight: range(4),
188 \ borderchars: range(8),
189 \ })
190
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200191 let winid = popup_create('hello both', #{line: 3, col: 8, border: [], padding: []})
192 call assert_equal(#{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200193 \ line: 3,
194 \ core_line: 5,
195 \ col: 8,
196 \ core_col: 10,
197 \ width: 14,
198 \ core_width: 10,
199 \ height: 5,
200 \ scrollbar: 0,
201 \ core_height: 1,
202 \ firstline: 1,
Bram Moolenaar30efcf32019-11-03 22:29:38 +0100203 \ lastline: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200204 \ visible: 1}, popup_getpos(winid))
Bram Moolenaarae943152019-06-16 22:54:14 +0200205
206 call popup_clear()
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200207endfunc
208
Bram Moolenaarb4230122019-05-30 18:40:53 +0200209func Test_popup_with_syntax_win_execute()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200210 CheckScreendump
211
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200212 let lines =<< trim END
213 call setline(1, range(1, 100))
214 hi PopupColor ctermbg=lightblue
215 let winid = popup_create([
216 \ '#include <stdio.h>',
217 \ 'int main(void)',
218 \ '{',
219 \ ' printf(123);',
220 \ '}',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200221 \], #{line: 3, col: 25, highlight: 'PopupColor'})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200222 call win_execute(winid, 'set syntax=cpp')
223 END
224 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200225 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaarb4230122019-05-30 18:40:53 +0200226 call VerifyScreenDump(buf, 'Test_popupwin_10', {})
227
228 " clean up
229 call StopVimInTerminal(buf)
230 call delete('XtestPopup')
231endfunc
232
233func Test_popup_with_syntax_setbufvar()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200234 CheckScreendump
235
Bram Moolenaar402502d2019-05-30 22:07:36 +0200236 let lines =<< trim END
237 call setline(1, range(1, 100))
238 hi PopupColor ctermbg=lightgrey
239 let winid = popup_create([
240 \ '#include <stdio.h>',
241 \ 'int main(void)',
242 \ '{',
Bram Moolenaare089c3f2019-07-09 20:25:25 +0200243 \ "\tprintf(567);",
Bram Moolenaar402502d2019-05-30 22:07:36 +0200244 \ '}',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200245 \], #{line: 3, col: 21, highlight: 'PopupColor'})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200246 call setbufvar(winbufnr(winid), '&syntax', 'cpp')
247 END
248 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200249 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaarb4230122019-05-30 18:40:53 +0200250 call VerifyScreenDump(buf, 'Test_popupwin_11', {})
251
252 " clean up
253 call StopVimInTerminal(buf)
254 call delete('XtestPopup')
255endfunc
256
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200257func Test_popup_with_matches()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200258 CheckScreendump
259
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200260 let lines =<< trim END
261 call setline(1, ['111 222 333', '444 555 666'])
262 let winid = popup_create([
263 \ '111 222 333',
264 \ '444 555 666',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200265 \], #{line: 3, col: 10, border: []})
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200266 set hlsearch
Bram Moolenaar024dbd22019-11-02 22:00:15 +0100267 hi VeryBlue ctermfg=blue guifg=blue
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200268 /666
269 call matchadd('ErrorMsg', '111')
Bram Moolenaar024dbd22019-11-02 22:00:15 +0100270 call matchadd('VeryBlue', '444')
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200271 call win_execute(winid, "call matchadd('ErrorMsg', '111')")
Bram Moolenaar024dbd22019-11-02 22:00:15 +0100272 call win_execute(winid, "call matchadd('VeryBlue', '555')")
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200273 END
274 call writefile(lines, 'XtestPopupMatches')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200275 let buf = RunVimInTerminal('-S XtestPopupMatches', #{rows: 10})
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200276 call VerifyScreenDump(buf, 'Test_popupwin_matches', {})
277
278 " clean up
279 call StopVimInTerminal(buf)
280 call delete('XtestPopupMatches')
281endfunc
282
Bram Moolenaar399d8982019-06-02 15:34:29 +0200283func Test_popup_all_corners()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200284 CheckScreendump
285
Bram Moolenaar399d8982019-06-02 15:34:29 +0200286 let lines =<< trim END
287 call setline(1, repeat([repeat('-', 60)], 15))
288 set so=0
289 normal 2G3|r#
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200290 let winid1 = popup_create(['first', 'second'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200291 \ line: 'cursor+1',
292 \ col: 'cursor',
293 \ pos: 'topleft',
294 \ border: [],
295 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200296 \ })
Bram Moolenaarb754b5b2019-10-24 19:25:00 +0200297 normal 24|r@
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200298 let winid1 = popup_create(['First', 'SeconD'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200299 \ line: 'cursor+1',
300 \ col: 'cursor',
301 \ pos: 'topright',
302 \ border: [],
303 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200304 \ })
Bram Moolenaarb754b5b2019-10-24 19:25:00 +0200305 normal 9G27|r%
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200306 let winid1 = popup_create(['fiRSt', 'seCOnd'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200307 \ line: 'cursor-1',
308 \ col: 'cursor',
309 \ pos: 'botleft',
310 \ border: [],
311 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200312 \ })
Bram Moolenaarb754b5b2019-10-24 19:25:00 +0200313 normal 48|r&
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200314 let winid1 = popup_create(['FIrsT', 'SEcoND'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200315 \ line: 'cursor-1',
316 \ col: 'cursor',
317 \ pos: 'botright',
318 \ border: [],
319 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200320 \ })
Bram Moolenaarb754b5b2019-10-24 19:25:00 +0200321 normal 1G51|r*
322 let winid1 = popup_create(['one', 'two'], #{
323 \ line: 'cursor-1',
324 \ col: 'cursor',
325 \ pos: 'botleft',
326 \ border: [],
327 \ padding: [],
328 \ })
Bram Moolenaar399d8982019-06-02 15:34:29 +0200329 END
330 call writefile(lines, 'XtestPopupCorners')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200331 let buf = RunVimInTerminal('-S XtestPopupCorners', #{rows: 12})
Bram Moolenaar399d8982019-06-02 15:34:29 +0200332 call VerifyScreenDump(buf, 'Test_popupwin_corners', {})
333
334 " clean up
335 call StopVimInTerminal(buf)
336 call delete('XtestPopupCorners')
337endfunc
338
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100339func Test_popup_nospace()
340 CheckScreendump
341
342 let lines =<< trim END
343 call setline(1, repeat([repeat('-', 60)], 15))
344 set so=0
345
346 " cursor in a line in top half, using "botleft" with popup that
347 " does fit
348 normal 5G2|r@
349 let winid1 = popup_create(['one', 'two'], #{
350 \ line: 'cursor-1',
351 \ col: 'cursor',
352 \ pos: 'botleft',
353 \ border: [],
354 \ })
355 " cursor in a line in top half, using "botleft" with popup that
356 " doesn't fit: gets truncated
357 normal 5G9|r#
358 let winid1 = popup_create(['one', 'two', 'tee'], #{
359 \ line: 'cursor-1',
360 \ col: 'cursor',
361 \ pos: 'botleft',
362 \ posinvert: 0,
363 \ border: [],
364 \ })
365 " cursor in a line in top half, using "botleft" with popup that
366 " doesn't fit and 'posinvert' set: flips to below.
367 normal 5G16|r%
368 let winid1 = popup_create(['one', 'two', 'tee'], #{
369 \ line: 'cursor-1',
370 \ col: 'cursor',
371 \ pos: 'botleft',
372 \ border: [],
373 \ })
374 " cursor in a line in bottom half, using "botleft" with popup that
375 " doesn't fit: does not flip.
376 normal 8G23|r*
377 let winid1 = popup_create(['aaa', 'bbb', 'ccc', 'ddd', 'eee', 'fff'], #{
378 \ line: 'cursor-1',
379 \ col: 'cursor',
380 \ pos: 'botleft',
381 \ border: [],
382 \ })
383
384 " cursor in a line in bottom half, using "topleft" with popup that
385 " does fit
386 normal 8G30|r@
387 let winid1 = popup_create(['one', 'two'], #{
388 \ line: 'cursor+1',
389 \ col: 'cursor',
390 \ pos: 'topleft',
391 \ border: [],
392 \ })
393 " cursor in a line in top half, using "topleft" with popup that
394 " doesn't fit: truncated
395 normal 8G37|r#
396 let winid1 = popup_create(['one', 'two', 'tee'], #{
397 \ line: 'cursor+1',
398 \ col: 'cursor',
399 \ pos: 'topleft',
400 \ posinvert: 0,
401 \ border: [],
402 \ })
403 " cursor in a line in top half, using "topleft" with popup that
Bram Moolenaar5c6b6182019-11-10 17:51:38 +0100404 " doesn't fit and "posinvert" set: flips to above.
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100405 normal 8G44|r%
Bram Moolenaar5c6b6182019-11-10 17:51:38 +0100406 let winid1 = popup_create(['one', 'two', 'tee', 'fou', 'fiv'], #{
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100407 \ line: 'cursor+1',
408 \ col: 'cursor',
409 \ pos: 'topleft',
410 \ border: [],
411 \ })
412 " cursor in a line in top half, using "topleft" with popup that
413 " doesn't fit: does not flip.
414 normal 5G51|r*
415 let winid1 = popup_create(['aaa', 'bbb', 'ccc', 'ddd', 'eee', 'fff'], #{
416 \ line: 'cursor+1',
417 \ col: 'cursor',
418 \ pos: 'topleft',
419 \ border: [],
420 \ })
421 END
422 call writefile(lines, 'XtestPopupNospace')
423 let buf = RunVimInTerminal('-S XtestPopupNospace', #{rows: 12})
424 call VerifyScreenDump(buf, 'Test_popupwin_nospace', {})
425
426 " clean up
427 call StopVimInTerminal(buf)
428 call delete('XtestPopupNospace')
429endfunc
430
Bram Moolenaaref6b9792020-05-13 16:34:15 +0200431func Test_popup_firstline_dump()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200432 CheckScreendump
433
Bram Moolenaar8d241042019-06-12 23:40:01 +0200434 let lines =<< trim END
435 call setline(1, range(1, 20))
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200436 let winid = popup_create(['1111', '222222', '33333', '44', '5', '666666', '77777', '888', '9999999999999999'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200437 \ maxheight: 4,
438 \ firstline: 3,
Bram Moolenaar8d241042019-06-12 23:40:01 +0200439 \ })
440 END
441 call writefile(lines, 'XtestPopupFirstline')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200442 let buf = RunVimInTerminal('-S XtestPopupFirstline', #{rows: 10})
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200443 call VerifyScreenDump(buf, 'Test_popupwin_firstline_1', {})
444
445 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: -1})\<CR>")
446 call term_sendkeys(buf, ":\<CR>")
447 call VerifyScreenDump(buf, 'Test_popupwin_firstline_2', {})
Bram Moolenaar8d241042019-06-12 23:40:01 +0200448
449 " clean up
450 call StopVimInTerminal(buf)
451 call delete('XtestPopupFirstline')
Bram Moolenaaref6b9792020-05-13 16:34:15 +0200452endfunc
Bram Moolenaarae943152019-06-16 22:54:14 +0200453
Bram Moolenaaref6b9792020-05-13 16:34:15 +0200454func Test_popup_firstline()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200455 let winid = popup_create(['1111', '222222', '33333', '44444'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200456 \ maxheight: 2,
457 \ firstline: 3,
Bram Moolenaarae943152019-06-16 22:54:14 +0200458 \ })
459 call assert_equal(3, popup_getoptions(winid).firstline)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200460 call popup_setoptions(winid, #{firstline: 1})
Bram Moolenaarae943152019-06-16 22:54:14 +0200461 call assert_equal(1, popup_getoptions(winid).firstline)
Bram Moolenaar6a124e62019-09-04 18:15:19 +0200462 eval winid->popup_close()
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +0200463
464 let winid = popup_create(['xxx']->repeat(50), #{
465 \ maxheight: 3,
466 \ firstline: 11,
467 \ })
468 redraw
469 call assert_equal(11, popup_getoptions(winid).firstline)
470 call assert_equal(11, popup_getpos(winid).firstline)
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +0200471 " check line() works with popup window
472 call assert_equal(11, line('.', winid))
473 call assert_equal(50, line('$', winid))
474 call assert_equal(0, line('$', 123456))
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +0200475
476 " Normal command changes what is displayed but not "firstline"
477 call win_execute(winid, "normal! \<c-y>")
478 call assert_equal(11, popup_getoptions(winid).firstline)
479 call assert_equal(10, popup_getpos(winid).firstline)
480
481 " Making some property change applies "firstline" again
482 call popup_setoptions(winid, #{line: 4})
483 call assert_equal(11, popup_getoptions(winid).firstline)
484 call assert_equal(11, popup_getpos(winid).firstline)
485
486 " Remove "firstline" property and scroll
487 call popup_setoptions(winid, #{firstline: 0})
488 call win_execute(winid, "normal! \<c-y>")
489 call assert_equal(0, popup_getoptions(winid).firstline)
490 call assert_equal(10, popup_getpos(winid).firstline)
491
492 " Making some property change has no side effect
493 call popup_setoptions(winid, #{line: 3})
494 call assert_equal(0, popup_getoptions(winid).firstline)
495 call assert_equal(10, popup_getpos(winid).firstline)
Bram Moolenaaref6b9792020-05-13 16:34:15 +0200496 call popup_close(winid)
Bram Moolenaarae943152019-06-16 22:54:14 +0200497
Bram Moolenaar30efcf32019-11-03 22:29:38 +0100498 " CTRL-D scrolls down half a page
499 let winid = popup_create(['xxx']->repeat(50), #{
500 \ maxheight: 8,
501 \ })
502 redraw
503 call assert_equal(1, popup_getpos(winid).firstline)
504 call win_execute(winid, "normal! \<C-D>")
505 call assert_equal(5, popup_getpos(winid).firstline)
506 call win_execute(winid, "normal! \<C-D>")
507 call assert_equal(9, popup_getpos(winid).firstline)
508 call win_execute(winid, "normal! \<C-U>")
509 call assert_equal(5, popup_getpos(winid).firstline)
510
511 call win_execute(winid, "normal! \<C-F>")
512 call assert_equal(11, popup_getpos(winid).firstline)
513 call win_execute(winid, "normal! \<C-B>")
514 call assert_equal(5, popup_getpos(winid).firstline)
515
Bram Moolenaarae943152019-06-16 22:54:14 +0200516 call popup_close(winid)
Bram Moolenaar8d241042019-06-12 23:40:01 +0200517endfunc
518
Bram Moolenaar99ca9c42020-09-22 21:55:41 +0200519func Test_popup_firstline_cursorline()
520 let winid = popup_create(['1111', '222222', '33333', '44444'], #{
521 \ maxheight: 2,
522 \ firstline: 3,
523 \ cursorline: 1,
524 \ })
525 call assert_equal(3, popup_getoptions(winid).firstline)
526 call assert_equal(3, getwininfo(winid)[0].topline)
527 call assert_equal(3, getcurpos(winid)[1])
528
529 call popup_close(winid)
530endfunc
531
Bram Moolenaara112f2d2019-09-01 17:38:09 +0200532func Test_popup_noscrolloff()
533 set scrolloff=5
534 let winid = popup_create(['xxx']->repeat(50), #{
535 \ maxheight: 5,
536 \ firstline: 11,
537 \ })
538 redraw
539 call assert_equal(11, popup_getoptions(winid).firstline)
540 call assert_equal(11, popup_getpos(winid).firstline)
541
542 call popup_setoptions(winid, #{firstline: 0})
543 call win_execute(winid, "normal! \<c-y>")
544 call assert_equal(0, popup_getoptions(winid).firstline)
545 call assert_equal(10, popup_getpos(winid).firstline)
546
547 call popup_close(winid)
548endfunc
549
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200550func Test_popup_drag()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200551 CheckScreendump
552
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200553 " create a popup that covers the command line
554 let lines =<< trim END
555 call setline(1, range(1, 20))
Bram Moolenaar356375f2019-08-24 14:46:29 +0200556 split
557 vsplit
558 $wincmd w
559 vsplit
560 1wincmd w
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200561 let winid = popup_create(['1111', '222222', '33333'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200562 \ drag: 1,
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200563 \ resize: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200564 \ border: [],
565 \ line: &lines - 4,
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200566 \ })
567 func Dragit()
Bram Moolenaar0b74d002021-11-29 17:38:02 +0000568 map <silent> <F3> :call test_setmouse(&lines - 4, &columns / 2)<CR>
569 map <silent> <F4> :call test_setmouse(&lines - 8, &columns / 2 - 20)<CR>
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200570 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
571 endfunc
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200572 func Resize()
Bram Moolenaar0b74d002021-11-29 17:38:02 +0000573 map <silent> <F5> :call test_setmouse(6, 21)<CR>
574 map <silent> <F6> :call test_setmouse(7, 25)<CR>
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200575 call feedkeys("\<F5>\<LeftMouse>\<F6>\<LeftDrag>\<LeftRelease>", "xt")
576 endfunc
Bram Moolenaarbfc57862021-11-26 15:57:40 +0000577 func ClickAndDrag()
Bram Moolenaar0b74d002021-11-29 17:38:02 +0000578 map <silent> <F3> :call test_setmouse(5, 2)<CR>
579 map <silent> <F4> :call test_setmouse(3, 14)<CR>
580 map <silent> <F5> :call test_setmouse(3, 18)<CR>
581 call feedkeys("\<F3>\<LeftMouse>\<LeftRelease>", "xt")
582 call feedkeys("\<F4>\<LeftMouse>\<F5>\<LeftDrag>\<LeftRelease>", "xt")
Bram Moolenaarbfc57862021-11-26 15:57:40 +0000583 endfunc
Bram Moolenaar0b74d002021-11-29 17:38:02 +0000584 func DragAllStart()
585 call popup_clear()
586 call popup_create('hello', #{line: 3, col: 5, dragall: 1})
587 endfunc
588 func DragAllDrag()
589 map <silent> <F3> :call test_setmouse(3, 5)<CR>
590 map <silent> <F4> :call test_setmouse(5, 36)<CR>
591 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
592 endfunc
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200593 END
594 call writefile(lines, 'XtestPopupDrag')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200595 let buf = RunVimInTerminal('-S XtestPopupDrag', #{rows: 10})
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200596 call VerifyScreenDump(buf, 'Test_popupwin_drag_01', {})
597
598 call term_sendkeys(buf, ":call Dragit()\<CR>")
599 call VerifyScreenDump(buf, 'Test_popupwin_drag_02', {})
600
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200601 call term_sendkeys(buf, ":call Resize()\<CR>")
602 call VerifyScreenDump(buf, 'Test_popupwin_drag_03', {})
603
Bram Moolenaarbfc57862021-11-26 15:57:40 +0000604 " dragging works after click on a status line
605 call term_sendkeys(buf, ":call ClickAndDrag()\<CR>")
606 call VerifyScreenDump(buf, 'Test_popupwin_drag_04', {})
607
Bram Moolenaar0b74d002021-11-29 17:38:02 +0000608 " dragging without border
609 call term_sendkeys(buf, ":call DragAllStart()\<CR>")
610 call VerifyScreenDump(buf, 'Test_popupwin_drag_05', {})
611 call term_sendkeys(buf, ":call DragAllDrag()\<CR>")
612 call VerifyScreenDump(buf, 'Test_popupwin_drag_06', {})
613
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200614 " clean up
615 call StopVimInTerminal(buf)
616 call delete('XtestPopupDrag')
617endfunc
618
Bram Moolenaareabddc42022-04-02 15:32:16 +0100619func Test_popup_drag_minwidth()
620 CheckScreendump
621
622 " create a popup that does not fit
623 let lines =<< trim END
624 call range(40)
625 \ ->map({_,i -> string(i)})
626 \ ->popup_create({
627 \ 'drag': 1,
628 \ 'wrap': 0,
629 \ 'border': [],
630 \ 'scrollbar': 1,
631 \ 'minwidth': 100,
632 \ 'filter': {w, k -> k ==# 'q' ? len([popup_close(w)]) : 0},
633 \ })
634 func DragitDown()
635 map <silent> <F3> :call test_setmouse(1, 10)<CR>
636 map <silent> <F4> :call test_setmouse(5, 40)<CR>
637 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
638 endfunc
639 func DragitUp()
640 map <silent> <F3> :call test_setmouse(5, 40)<CR>
641 map <silent> <F4> :call test_setmouse(4, 40)<CR>
642 map <silent> <F5> :call test_setmouse(3, 40)<CR>
643 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<F5>\<LeftDrag>\<LeftRelease>", "xt")
644 endfunc
645 END
646 call writefile(lines, 'XtestPopupDrag')
647 let buf = RunVimInTerminal('-S XtestPopupDrag', #{rows: 10})
648 call VerifyScreenDump(buf, 'Test_popupwin_drag_minwidth_1', {})
649
650 call term_sendkeys(buf, ":call DragitDown()\<CR>")
651 call VerifyScreenDump(buf, 'Test_popupwin_drag_minwidth_2', {})
652
653 call term_sendkeys(buf, ":call DragitUp()\<CR>")
654 call VerifyScreenDump(buf, 'Test_popupwin_drag_minwidth_3', {})
655
656 call term_sendkeys(buf, 'q')
657
658 " clean up
659 call StopVimInTerminal(buf)
660 call delete('XtestPopupDrag')
661endfunc
662
Bram Moolenaar35910f22020-07-12 19:24:10 +0200663func Test_popup_drag_termwin()
664 CheckUnix
665 CheckScreendump
666 CheckFeature terminal
667
668 " create a popup that covers the terminal window
669 let lines =<< trim END
Bram Moolenaar452143c2020-07-15 17:38:21 +0200670 set foldmethod=marker
671 call setline(1, range(100))
672 for nr in range(7)
673 call setline(nr * 12 + 1, "fold {{{")
Bram Moolenaar9d489562020-07-30 20:08:50 +0200674 call setline(nr * 12 + 11, "end }}}")
Bram Moolenaar452143c2020-07-15 17:38:21 +0200675 endfor
676 %foldclose
Bram Moolenaar35910f22020-07-12 19:24:10 +0200677 set shell=/bin/sh noruler
Bram Moolenaar4b2ce122020-11-21 21:41:41 +0100678 unlet $PROMPT_COMMAND
Bram Moolenaara4dc6f92020-07-12 19:52:36 +0200679 let $PS1 = 'vim> '
Bram Moolenaar452143c2020-07-15 17:38:21 +0200680 terminal ++rows=4
Bram Moolenaar35910f22020-07-12 19:24:10 +0200681 $wincmd w
682 let winid = popup_create(['1111', '2222'], #{
683 \ drag: 1,
684 \ resize: 1,
685 \ border: [],
686 \ line: 3,
687 \ })
Bram Moolenaar452143c2020-07-15 17:38:21 +0200688 func DragitLeft()
Bram Moolenaar35910f22020-07-12 19:24:10 +0200689 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
690 endfunc
Bram Moolenaar452143c2020-07-15 17:38:21 +0200691 func DragitDown()
692 call feedkeys("\<F4>\<LeftMouse>\<F5>\<LeftDrag>\<LeftRelease>", "xt")
693 endfunc
694 func DragitDownLeft()
695 call feedkeys("\<F5>\<LeftMouse>\<F6>\<LeftDrag>\<LeftRelease>", "xt")
696 endfunc
Bram Moolenaar35910f22020-07-12 19:24:10 +0200697 map <silent> <F3> :call test_setmouse(3, &columns / 2)<CR>
698 map <silent> <F4> :call test_setmouse(3, &columns / 2 - 20)<CR>
Bram Moolenaar452143c2020-07-15 17:38:21 +0200699 map <silent> <F5> :call test_setmouse(12, &columns / 2)<CR>
700 map <silent> <F6> :call test_setmouse(12, &columns / 2 - 20)<CR>
Bram Moolenaar35910f22020-07-12 19:24:10 +0200701 END
702 call writefile(lines, 'XtestPopupTerm')
Bram Moolenaar452143c2020-07-15 17:38:21 +0200703 let buf = RunVimInTerminal('-S XtestPopupTerm', #{rows: 16})
Bram Moolenaar35910f22020-07-12 19:24:10 +0200704 call VerifyScreenDump(buf, 'Test_popupwin_term_01', {})
705
Bram Moolenaar452143c2020-07-15 17:38:21 +0200706 call term_sendkeys(buf, ":call DragitLeft()\<CR>")
Bram Moolenaar35910f22020-07-12 19:24:10 +0200707 call VerifyScreenDump(buf, 'Test_popupwin_term_02', {})
708
Bram Moolenaar452143c2020-07-15 17:38:21 +0200709 call term_sendkeys(buf, ":call DragitDown()\<CR>")
710 call VerifyScreenDump(buf, 'Test_popupwin_term_03', {})
711
712 call term_sendkeys(buf, ":call DragitDownLeft()\<CR>")
713 call VerifyScreenDump(buf, 'Test_popupwin_term_04', {})
714
Bram Moolenaar35910f22020-07-12 19:24:10 +0200715 " clean up
716 call StopVimInTerminal(buf)
717 call delete('XtestPopupTerm')
718endfunc
719
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200720func Test_popup_close_with_mouse()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200721 CheckScreendump
722
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200723 let lines =<< trim END
724 call setline(1, range(1, 20))
725 " With border, can click on X
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200726 let winid = popup_create('foobar', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200727 \ close: 'button',
728 \ border: [],
729 \ line: 1,
730 \ col: 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200731 \ })
732 func CloseMsg(id, result)
733 echomsg 'Popup closed with ' .. a:result
734 endfunc
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200735 let winid = popup_create('notification', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200736 \ close: 'click',
737 \ line: 3,
738 \ col: 15,
739 \ callback: 'CloseMsg',
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200740 \ })
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200741 let winid = popup_create('no border here', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200742 \ close: 'button',
743 \ line: 5,
744 \ col: 3,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200745 \ })
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200746 let winid = popup_create('only padding', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200747 \ close: 'button',
748 \ padding: [],
749 \ line: 5,
750 \ col: 23,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200751 \ })
752 func CloseWithX()
753 call feedkeys("\<F3>\<LeftMouse>\<LeftRelease>", "xt")
754 endfunc
755 map <silent> <F3> :call test_setmouse(1, len('foobar') + 2)<CR>
756 func CloseWithClick()
757 call feedkeys("\<F4>\<LeftMouse>\<LeftRelease>", "xt")
758 endfunc
759 map <silent> <F4> :call test_setmouse(3, 17)<CR>
Bram Moolenaarf6396232019-08-24 19:36:00 +0200760 func CreateWithMenuFilter()
761 let winid = popup_create('barfoo', #{
762 \ close: 'button',
763 \ filter: 'popup_filter_menu',
764 \ border: [],
765 \ line: 1,
766 \ col: 40,
767 \ })
768 endfunc
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200769 END
770 call writefile(lines, 'XtestPopupClose')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200771 let buf = RunVimInTerminal('-S XtestPopupClose', #{rows: 10})
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200772 call VerifyScreenDump(buf, 'Test_popupwin_close_01', {})
773
774 call term_sendkeys(buf, ":call CloseWithX()\<CR>")
775 call VerifyScreenDump(buf, 'Test_popupwin_close_02', {})
776
777 call term_sendkeys(buf, ":call CloseWithClick()\<CR>")
778 call VerifyScreenDump(buf, 'Test_popupwin_close_03', {})
779
Bram Moolenaarf6396232019-08-24 19:36:00 +0200780 call term_sendkeys(buf, ":call CreateWithMenuFilter()\<CR>")
781 call VerifyScreenDump(buf, 'Test_popupwin_close_04', {})
782
783 " We have to send the actual mouse code, feedkeys() would be caught the
784 " filter.
785 call term_sendkeys(buf, "\<Esc>[<0;47;1M")
786 call VerifyScreenDump(buf, 'Test_popupwin_close_05', {})
787
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200788 " clean up
789 call StopVimInTerminal(buf)
790 call delete('XtestPopupClose')
791endfunction
792
Bram Moolenaar7b3d9392019-10-16 22:17:07 +0200793func Test_popup_menu_wrap()
794 CheckScreendump
795
796 let lines =<< trim END
797 call setline(1, range(1, 20))
798 call popup_create([
799 \ 'one',
800 \ 'asdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfas',
801 \ 'three',
802 \ 'four',
803 \ ], #{
804 \ pos: "botleft",
805 \ border: [],
806 \ padding: [0,1,0,1],
807 \ maxheight: 3,
808 \ cursorline: 1,
809 \ filter: 'popup_filter_menu',
810 \ })
811 END
812 call writefile(lines, 'XtestPopupWrap')
813 let buf = RunVimInTerminal('-S XtestPopupWrap', #{rows: 10})
814 call VerifyScreenDump(buf, 'Test_popupwin_wrap_1', {})
815
816 call term_sendkeys(buf, "jj")
817 call VerifyScreenDump(buf, 'Test_popupwin_wrap_2', {})
818
819 " clean up
820 call term_sendkeys(buf, "\<Esc>")
821 call StopVimInTerminal(buf)
822 call delete('XtestPopupWrap')
823endfunction
824
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200825func Test_popup_with_mask()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200826 CheckScreendump
827
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200828 let lines =<< trim END
Bram Moolenaarba45f1f2019-07-03 22:50:41 +0200829 call setline(1, repeat([join(range(1, 42), '')], 13))
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200830 hi PopupColor ctermbg=lightgrey
831 let winid = popup_create([
832 \ 'some text',
833 \ 'another line',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200834 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200835 \ line: 1,
836 \ col: 10,
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100837 \ posinvert: 0,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200838 \ wrap: 0,
839 \ fixed: 1,
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +0200840 \ scrollbar: v:false,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200841 \ zindex: 90,
842 \ padding: [],
843 \ highlight: 'PopupColor',
844 \ mask: [[1,1,1,1], [-5,-1,4,4], [7,9,2,3], [2,4,3,3]]})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200845 call popup_create([
846 \ 'xxxxxxxxx',
847 \ 'yyyyyyyyy',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200848 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200849 \ line: 3,
850 \ col: 18,
851 \ zindex: 20})
Bram Moolenaarba45f1f2019-07-03 22:50:41 +0200852 let winidb = popup_create([
853 \ 'just one line',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200854 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200855 \ line: 7,
856 \ col: 10,
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100857 \ posinvert: 0,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200858 \ wrap: 0,
859 \ fixed: 1,
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +0200860 \ scrollbar: v:false,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200861 \ close: 'button',
862 \ zindex: 90,
863 \ padding: [],
864 \ border: [],
865 \ 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 +0200866 END
867 call writefile(lines, 'XtestPopupMask')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200868 let buf = RunVimInTerminal('-S XtestPopupMask', #{rows: 13})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200869 call VerifyScreenDump(buf, 'Test_popupwin_mask_1', {})
870
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200871 call term_sendkeys(buf, ":call popup_move(winid, #{col: 11, line: 2})\<CR>")
872 call term_sendkeys(buf, ":call popup_move(winidb, #{col: 12})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200873 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200874 call VerifyScreenDump(buf, 'Test_popupwin_mask_2', {})
875
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200876 call term_sendkeys(buf, ":call popup_move(winid, #{col: 65, line: 2})\<CR>")
877 call term_sendkeys(buf, ":call popup_move(winidb, #{col: 63})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200878 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaard529ba52019-07-02 23:13:53 +0200879 call VerifyScreenDump(buf, 'Test_popupwin_mask_3', {})
880
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200881 call term_sendkeys(buf, ":call popup_move(winid, #{pos: 'topright', col: 12, line: 2})\<CR>")
882 call term_sendkeys(buf, ":call popup_move(winidb, #{pos: 'topright', col: 12})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200883 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaard529ba52019-07-02 23:13:53 +0200884 call VerifyScreenDump(buf, 'Test_popupwin_mask_4', {})
885
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200886 call term_sendkeys(buf, ":call popup_move(winid, #{pos: 'topright', col: 12, line: 11})\<CR>")
887 call term_sendkeys(buf, ":call popup_move(winidb, #{pos: 'topleft', col: 42, line: 11})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200888 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaarb4207472019-07-12 16:05:45 +0200889 call VerifyScreenDump(buf, 'Test_popupwin_mask_5', {})
890
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200891 " clean up
892 call StopVimInTerminal(buf)
893 call delete('XtestPopupMask')
Bram Moolenaar4012d262020-12-29 11:57:46 +0100894
895 " this was causing a crash
896 call popup_create('test', #{mask: [[0, 0, 0, 0]]})
897 call popup_clear()
Bram Moolenaar10ccfb22021-02-13 21:31:18 +0100898
899 " this was causing an internal error
900 enew
901 set nowrap
902 call repeat('x', &columns)->setline(1)
903 call prop_type_add('textprop', {})
904 call prop_add(1, 1, #{length: &columns, type: 'textprop'})
905 vsplit
906 let opts = popup_create('', #{textprop: 'textprop'})
907 \ ->popup_getoptions()
908 \ ->extend(#{mask: [[1, 1, 1, 1]]})
909 call popup_create('', opts)
910 redraw
911
912 close!
913 bwipe!
914 call prop_type_delete('textprop')
915 call popup_clear()
916 set wrap&
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200917endfunc
918
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200919func Test_popup_select()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200920 CheckScreendump
921 CheckFeature clipboard_working
922
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200923 " create a popup with some text to be selected
924 let lines =<< trim END
Bram Moolenaar1755ec42019-06-15 13:13:54 +0200925 set clipboard=autoselect
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200926 call setline(1, range(1, 20))
Bram Moolenaar4dd751b2019-08-17 19:10:53 +0200927 let winid = popup_create(['the word', 'some more', 'several words here', 'invisible', '5', '6', '7'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200928 \ drag: 1,
929 \ border: [],
930 \ line: 3,
931 \ col: 10,
Bram Moolenaar4dd751b2019-08-17 19:10:53 +0200932 \ maxheight: 3,
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200933 \ })
934 func Select1()
935 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
936 endfunc
937 map <silent> <F3> :call test_setmouse(4, 15)<CR>
938 map <silent> <F4> :call test_setmouse(6, 23)<CR>
939 END
940 call writefile(lines, 'XtestPopupSelect')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200941 let buf = RunVimInTerminal('-S XtestPopupSelect', #{rows: 10})
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200942 call term_sendkeys(buf, ":call Select1()\<CR>")
943 call VerifyScreenDump(buf, 'Test_popupwin_select_01', {})
944
945 call term_sendkeys(buf, ":call popup_close(winid)\<CR>")
946 call term_sendkeys(buf, "\"*p")
Bram Moolenaar8ccabf62019-07-12 18:12:51 +0200947 " clean the command line, sometimes it still shows a command
948 call term_sendkeys(buf, ":\<esc>")
949
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200950 call VerifyScreenDump(buf, 'Test_popupwin_select_02', {})
951
952 " clean up
953 call StopVimInTerminal(buf)
954 call delete('XtestPopupSelect')
955endfunc
956
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200957func Test_popup_in_tab()
958 " default popup is local to tab, not visible when in other tab
959 let winid = popup_create("text", {})
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200960 let bufnr = winbufnr(winid)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200961 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200962 call assert_equal(0, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200963 tabnew
964 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200965 call assert_equal(1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200966 quit
967 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200968
969 call assert_equal(1, bufexists(bufnr))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200970 call popup_clear()
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200971 " buffer is gone now
972 call assert_equal(0, bufexists(bufnr))
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200973
974 " global popup is visible in any tab
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200975 let winid = popup_create("text", #{tabpage: -1})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200976 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200977 call assert_equal(-1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200978 tabnew
979 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200980 call assert_equal(-1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200981 quit
982 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200983 call popup_clear()
Bram Moolenaara3fce622019-06-20 02:31:49 +0200984
985 " create popup in other tab
986 tabnew
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200987 let winid = popup_create("text", #{tabpage: 1})
Bram Moolenaara3fce622019-06-20 02:31:49 +0200988 call assert_equal(0, popup_getpos(winid).visible)
989 call assert_equal(1, popup_getoptions(winid).tabpage)
990 quit
991 call assert_equal(1, popup_getpos(winid).visible)
992 call assert_equal(0, popup_getoptions(winid).tabpage)
993 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200994endfunc
995
996func Test_popup_valid_arguments()
Bram Moolenaaref6b9792020-05-13 16:34:15 +0200997 call assert_equal(0, len(popup_list()))
998
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200999 " Zero value is like the property wasn't there
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001000 let winid = popup_create("text", #{col: 0})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001001 let pos = popup_getpos(winid)
1002 call assert_inrange(&columns / 2 - 1, &columns / 2 + 1, pos.col)
Bram Moolenaaref6b9792020-05-13 16:34:15 +02001003 call assert_equal([winid], popup_list())
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001004 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001005
1006 " using cursor column has minimum value of 1
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001007 let winid = popup_create("text", #{col: 'cursor-100'})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001008 let pos = popup_getpos(winid)
1009 call assert_equal(1, pos.col)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001010 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001011
1012 " center
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001013 let winid = popup_create("text", #{pos: 'center'})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001014 let pos = popup_getpos(winid)
1015 let around = (&columns - pos.width) / 2
1016 call assert_inrange(around - 1, around + 1, pos.col)
1017 let around = (&lines - pos.height) / 2
1018 call assert_inrange(around - 1, around + 1, pos.line)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001019 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001020endfunc
1021
1022func Test_popup_invalid_arguments()
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001023 call assert_fails('call popup_create(666, {})', 'E86:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001024 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001025 call assert_fails('call popup_create("text", "none")', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001026 call popup_clear()
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02001027 call assert_fails('call popup_create(test_null_string(), {})', 'E450:')
1028 call assert_fails('call popup_create(test_null_list(), {})', 'E450:')
1029 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001030
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001031 call assert_fails('call popup_create("text", #{col: "xxx"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001032 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001033 call assert_fails('call popup_create("text", #{col: "cursor8"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001034 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001035 call assert_fails('call popup_create("text", #{col: "cursor+x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001036 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001037 call assert_fails('call popup_create("text", #{col: "cursor+8x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001038 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001039
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001040 call assert_fails('call popup_create("text", #{line: "xxx"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001041 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001042 call assert_fails('call popup_create("text", #{line: "cursor8"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001043 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001044 call assert_fails('call popup_create("text", #{line: "cursor+x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001045 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001046 call assert_fails('call popup_create("text", #{line: "cursor+8x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001047 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001048
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001049 call assert_fails('call popup_create("text", #{pos: "there"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001050 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001051 call assert_fails('call popup_create("text", #{padding: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001052 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001053 call assert_fails('call popup_create("text", #{border: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001054 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001055 call assert_fails('call popup_create("text", #{borderhighlight: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001056 call popup_clear()
Bram Moolenaar403d0902019-07-17 21:37:32 +02001057 call assert_fails('call popup_create("text", #{borderhighlight: test_null_list()})', 'E714:')
1058 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001059 call assert_fails('call popup_create("text", #{borderchars: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001060 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001061
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001062 call assert_fails('call popup_create([#{text: "text"}, 666], {})', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001063 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001064 call assert_fails('call popup_create([#{text: "text", props: "none"}], {})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001065 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001066 call assert_fails('call popup_create([#{text: "text", props: ["none"]}], {})', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001067 call popup_clear()
Bram Moolenaarb0992022020-01-30 14:55:42 +01001068 call assert_fails('call popup_create([#{text: "text", props: range(3)}], {})', 'E715:')
1069 call popup_clear()
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +02001070 call assert_fails('call popup_create("text", #{mask: ["asdf"]})', 'E475:')
1071 call popup_clear()
Bram Moolenaarb0992022020-01-30 14:55:42 +01001072 call assert_fails('call popup_create("text", #{mask: range(5)})', 'E475:')
1073 call popup_clear()
1074 call popup_create("text", #{mask: [range(4)]})
1075 call popup_clear()
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +02001076 call assert_fails('call popup_create("text", #{mask: test_null_list()})', 'E475:')
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001077 call assert_fails('call popup_create("text", #{mapping: []})', 'E745:')
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +02001078 call popup_clear()
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02001079 call assert_fails('call popup_create("text", #{tabpage : 4})', 'E997:')
1080 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001081endfunc
1082
Bram Moolenaareea16992019-05-31 17:34:48 +02001083func Test_win_execute_closing_curwin()
1084 split
1085 let winid = popup_create('some text', {})
Bram Moolenaare2e40752020-09-04 21:18:46 +02001086 call assert_fails('call win_execute(winid, winnr() .. "close")', 'E994:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001087 call popup_clear()
Bram Moolenaar49540192019-12-11 19:34:54 +01001088
1089 let winid = popup_create('some text', {})
Bram Moolenaare2e40752020-09-04 21:18:46 +02001090 call assert_fails('call win_execute(winid, printf("normal! :\<C-u>call popup_close(%d)\<CR>", winid))', 'E994:')
Bram Moolenaar49540192019-12-11 19:34:54 +01001091 call popup_clear()
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001092endfunc
1093
1094func Test_win_execute_not_allowed()
1095 let winid = popup_create('some text', {})
1096 call assert_fails('call win_execute(winid, "split")', 'E994:')
1097 call assert_fails('call win_execute(winid, "vsplit")', 'E994:')
1098 call assert_fails('call win_execute(winid, "close")', 'E994:')
1099 call assert_fails('call win_execute(winid, "bdelete")', 'E994:')
Bram Moolenaar2d247842019-06-01 17:06:25 +02001100 call assert_fails('call win_execute(winid, "bwipe!")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001101 call assert_fails('call win_execute(winid, "tabnew")', 'E994:')
1102 call assert_fails('call win_execute(winid, "tabnext")', 'E994:')
1103 call assert_fails('call win_execute(winid, "next")', 'E994:')
1104 call assert_fails('call win_execute(winid, "rewind")', 'E994:')
Bram Moolenaar3a2505c2020-03-09 16:40:41 +01001105 call assert_fails('call win_execute(winid, "pedit filename")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001106 call assert_fails('call win_execute(winid, "buf")', 'E994:')
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01001107 call assert_fails('call win_execute(winid, "bnext")', 'E994:')
1108 call assert_fails('call win_execute(winid, "bprev")', 'E994:')
1109 call assert_fails('call win_execute(winid, "bfirst")', 'E994:')
1110 call assert_fails('call win_execute(winid, "blast")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001111 call assert_fails('call win_execute(winid, "edit")', 'E994:')
1112 call assert_fails('call win_execute(winid, "enew")', 'E994:')
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +02001113 call assert_fails('call win_execute(winid, "help")', 'E994:')
1114 call assert_fails('call win_execute(winid, "1only")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001115 call assert_fails('call win_execute(winid, "wincmd x")', 'E994:')
1116 call assert_fails('call win_execute(winid, "wincmd w")', 'E994:')
1117 call assert_fails('call win_execute(winid, "wincmd t")', 'E994:')
1118 call assert_fails('call win_execute(winid, "wincmd b")', 'E994:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001119 call popup_clear()
Bram Moolenaareea16992019-05-31 17:34:48 +02001120endfunc
1121
Bram Moolenaar402502d2019-05-30 22:07:36 +02001122func Test_popup_with_wrap()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001123 CheckScreendump
1124
Bram Moolenaar402502d2019-05-30 22:07:36 +02001125 let lines =<< trim END
1126 call setline(1, range(1, 100))
1127 let winid = popup_create(
1128 \ 'a long line that wont fit',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001129 \ #{line: 3, col: 20, maxwidth: 10, wrap: 1})
Bram Moolenaar402502d2019-05-30 22:07:36 +02001130 END
1131 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001132 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar402502d2019-05-30 22:07:36 +02001133 call VerifyScreenDump(buf, 'Test_popupwin_wrap', {})
1134
1135 " clean up
1136 call StopVimInTerminal(buf)
1137 call delete('XtestPopup')
1138endfunc
1139
1140func Test_popup_without_wrap()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001141 CheckScreendump
1142
Bram Moolenaar402502d2019-05-30 22:07:36 +02001143 let lines =<< trim END
1144 call setline(1, range(1, 100))
1145 let winid = popup_create(
1146 \ 'a long line that wont fit',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001147 \ #{line: 3, col: 20, maxwidth: 10, wrap: 0})
Bram Moolenaar402502d2019-05-30 22:07:36 +02001148 END
1149 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001150 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar402502d2019-05-30 22:07:36 +02001151 call VerifyScreenDump(buf, 'Test_popupwin_nowrap', {})
1152
1153 " clean up
1154 call StopVimInTerminal(buf)
1155 call delete('XtestPopup')
1156endfunc
1157
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001158func Test_popup_with_showbreak()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001159 CheckScreendump
1160
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001161 let lines =<< trim END
1162 set showbreak=>>\
1163 call setline(1, range(1, 20))
1164 let winid = popup_dialog(
Bram Moolenaar8ae54372019-09-15 18:11:16 +02001165 \ 'a long line here that wraps',
1166 \ #{filter: 'popup_filter_yesno',
1167 \ maxwidth: 12})
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001168 END
1169 call writefile(lines, 'XtestPopupShowbreak')
1170 let buf = RunVimInTerminal('-S XtestPopupShowbreak', #{rows: 10})
1171 call VerifyScreenDump(buf, 'Test_popupwin_showbreak', {})
1172
1173 " clean up
1174 call term_sendkeys(buf, "y")
1175 call StopVimInTerminal(buf)
1176 call delete('XtestPopupShowbreak')
1177endfunc
1178
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001179func Test_popup_time()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001180 CheckFeature timers
1181
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001182 topleft vnew
1183 call setline(1, 'hello')
1184
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001185 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001186 \ line: 1,
1187 \ col: 1,
1188 \ minwidth: 20,
1189 \ time: 500,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001190 \})
1191 redraw
1192 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1193 call assert_equal('world', line)
1194
Bram Moolenaarb4f06282019-07-12 21:07:54 +02001195 call assert_equal(winid, popup_locate(1, 1))
1196 call assert_equal(winid, popup_locate(1, 20))
1197 call assert_equal(0, popup_locate(1, 21))
1198 call assert_equal(0, popup_locate(2, 1))
1199
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001200 sleep 700m
Bram Moolenaar35d5af62019-05-26 20:44:10 +02001201 redraw
Bram Moolenaar196b4662019-09-06 21:34:30 +02001202 let line = join(map(range(1, 5), '1->screenstring(v:val)'), '')
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001203 call assert_equal('hello', line)
1204
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001205 call popup_create('on the command line', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001206 \ line: &lines,
1207 \ col: 10,
1208 \ minwidth: 20,
1209 \ time: 500,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001210 \})
1211 redraw
1212 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
1213 call assert_match('.*on the command line.*', line)
1214
1215 sleep 700m
1216 redraw
1217 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
1218 call assert_notmatch('.*on the command line.*', line)
1219
1220 bwipe!
1221endfunc
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001222
1223func Test_popup_hide()
1224 topleft vnew
1225 call setline(1, 'hello')
1226
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001227 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001228 \ line: 1,
1229 \ col: 1,
1230 \ minwidth: 20,
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001231 \})
1232 redraw
1233 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1234 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001235 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001236 " buffer is still listed and active
1237 call assert_match(winbufnr(winid) .. 'u a.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001238
1239 call popup_hide(winid)
1240 redraw
1241 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1242 call assert_equal('hello', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001243 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001244 " buffer is still listed but hidden
Bram Moolenaarca7c0782020-01-14 20:42:48 +01001245 call assert_match(winbufnr(winid) .. 'u a.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001246
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001247 eval winid->popup_show()
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001248 redraw
1249 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1250 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001251 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001252
1253
1254 call popup_close(winid)
1255 redraw
1256 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1257 call assert_equal('hello', line)
1258
1259 " error is given for existing non-popup window
1260 call assert_fails('call popup_hide(win_getid())', 'E993:')
1261
1262 " no error non-existing window
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001263 eval 1234234->popup_hide()
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001264 call popup_show(41234234)
1265
1266 bwipe!
1267endfunc
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001268
1269func Test_popup_move()
1270 topleft vnew
1271 call setline(1, 'hello')
1272
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001273 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001274 \ line: 1,
1275 \ col: 1,
1276 \ minwidth: 20,
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001277 \})
1278 redraw
1279 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
1280 call assert_equal('world ', line)
1281
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001282 call popup_move(winid, #{line: 2, col: 2})
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001283 redraw
1284 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
1285 call assert_equal('hello ', line)
1286 let line = join(map(range(1, 6), 'screenstring(2, v:val)'), '')
1287 call assert_equal('~world', line)
1288
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001289 eval winid->popup_move(#{line: 1})
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001290 redraw
1291 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
1292 call assert_equal('hworld', line)
1293
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02001294 call assert_fails('call popup_move(winid, [])', 'E715:')
1295 call assert_fails('call popup_move(winid, test_null_dict())', 'E715:')
1296
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001297 call popup_close(winid)
1298
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02001299 call assert_equal(0, popup_move(-1, {}))
1300
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001301 bwipe!
1302endfunc
Bram Moolenaarbc133542019-05-29 20:26:46 +02001303
Bram Moolenaar402502d2019-05-30 22:07:36 +02001304func Test_popup_getpos()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001305 let winid = popup_create('hello', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001306 \ line: 2,
1307 \ col: 3,
1308 \ minwidth: 10,
1309 \ minheight: 11,
Bram Moolenaarbc133542019-05-29 20:26:46 +02001310 \})
1311 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +02001312 let res = popup_getpos(winid)
Bram Moolenaarbc133542019-05-29 20:26:46 +02001313 call assert_equal(2, res.line)
1314 call assert_equal(3, res.col)
1315 call assert_equal(10, res.width)
1316 call assert_equal(11, res.height)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001317 call assert_equal(1, res.visible)
Bram Moolenaarbc133542019-05-29 20:26:46 +02001318
1319 call popup_close(winid)
1320endfunc
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001321
1322func Test_popup_width_longest()
1323 let tests = [
1324 \ [['hello', 'this', 'window', 'displays', 'all of its text'], 15],
1325 \ [['hello', 'this', 'window', 'all of its text', 'displays'], 15],
1326 \ [['hello', 'this', 'all of its text', 'window', 'displays'], 15],
1327 \ [['hello', 'all of its text', 'this', 'window', 'displays'], 15],
1328 \ [['all of its text', 'hello', 'this', 'window', 'displays'], 15],
1329 \ ]
1330
1331 for test in tests
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001332 let winid = popup_create(test[0], #{line: 2, col: 3})
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001333 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +02001334 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001335 call assert_equal(test[1], position.width)
1336 call popup_close(winid)
1337 endfor
1338endfunc
1339
1340func Test_popup_wraps()
1341 let tests = [
1342 \ ['nowrap', 6, 1],
1343 \ ['a line that wraps once', 12, 2],
1344 \ ['a line that wraps two times', 12, 3],
1345 \ ]
1346 for test in tests
1347 let winid = popup_create(test[0],
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001348 \ #{line: 2, col: 3, maxwidth: 12})
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001349 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +02001350 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001351 call assert_equal(test[1], position.width)
1352 call assert_equal(test[2], position.height)
1353
1354 call popup_close(winid)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001355 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001356 endfor
1357endfunc
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001358
1359func Test_popup_getoptions()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001360 let winid = popup_create('hello', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001361 \ line: 2,
1362 \ col: 3,
1363 \ minwidth: 10,
1364 \ minheight: 11,
1365 \ maxwidth: 20,
1366 \ maxheight: 21,
1367 \ zindex: 100,
1368 \ time: 5000,
1369 \ fixed: 1
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001370 \})
1371 redraw
1372 let res = popup_getoptions(winid)
1373 call assert_equal(2, res.line)
1374 call assert_equal(3, res.col)
1375 call assert_equal(10, res.minwidth)
1376 call assert_equal(11, res.minheight)
1377 call assert_equal(20, res.maxwidth)
1378 call assert_equal(21, res.maxheight)
1379 call assert_equal(100, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001380 call assert_equal(1, res.fixed)
Bram Moolenaarb8350ab2019-08-04 17:59:49 +02001381 call assert_equal(1, res.mapping)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001382 if has('timers')
1383 call assert_equal(5000, res.time)
1384 endif
1385 call popup_close(winid)
1386
1387 let winid = popup_create('hello', {})
1388 redraw
1389 let res = popup_getoptions(winid)
1390 call assert_equal(0, res.line)
1391 call assert_equal(0, res.col)
1392 call assert_equal(0, res.minwidth)
1393 call assert_equal(0, res.minheight)
1394 call assert_equal(0, res.maxwidth)
1395 call assert_equal(0, res.maxheight)
1396 call assert_equal(50, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001397 call assert_equal(0, res.fixed)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001398 if has('timers')
1399 call assert_equal(0, res.time)
1400 endif
1401 call popup_close(winid)
1402 call assert_equal({}, popup_getoptions(winid))
1403endfunc
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001404
1405func Test_popup_option_values()
1406 new
1407 " window-local
1408 setlocal number
1409 setlocal nowrap
1410 " buffer-local
1411 setlocal omnifunc=Something
1412 " global/buffer-local
1413 setlocal path=/there
1414 " global/window-local
Bram Moolenaara112f2d2019-09-01 17:38:09 +02001415 setlocal statusline=2
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001416
1417 let winid = popup_create('hello', {})
1418 call assert_equal(0, getwinvar(winid, '&number'))
1419 call assert_equal(1, getwinvar(winid, '&wrap'))
1420 call assert_equal('', getwinvar(winid, '&omnifunc'))
1421 call assert_equal(&g:path, getwinvar(winid, '&path'))
Bram Moolenaara112f2d2019-09-01 17:38:09 +02001422 call assert_equal(&g:statusline, getwinvar(winid, '&statusline'))
1423
1424 " 'scrolloff' is reset to zero
1425 call assert_equal(5, &scrolloff)
1426 call assert_equal(0, getwinvar(winid, '&scrolloff'))
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001427
1428 call popup_close(winid)
1429 bwipe
1430endfunc
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001431
1432func Test_popup_atcursor()
1433 topleft vnew
1434 call setline(1, [
1435 \ 'xxxxxxxxxxxxxxxxx',
1436 \ 'xxxxxxxxxxxxxxxxx',
1437 \ 'xxxxxxxxxxxxxxxxx',
1438 \])
1439
1440 call cursor(2, 2)
1441 redraw
1442 let winid = popup_atcursor('vim', {})
1443 redraw
1444 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
1445 call assert_equal('xvimxxxxxxxxxxxxx', line)
1446 call popup_close(winid)
1447
1448 call cursor(3, 4)
1449 redraw
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001450 let winid = 'vim'->popup_atcursor({})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001451 redraw
1452 let line = join(map(range(1, 17), 'screenstring(2, v:val)'), '')
1453 call assert_equal('xxxvimxxxxxxxxxxx', line)
1454 call popup_close(winid)
1455
1456 call cursor(1, 1)
1457 redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001458 let winid = popup_create('vim', #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001459 \ line: 'cursor+2',
1460 \ col: 'cursor+1',
1461 \})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001462 redraw
1463 let line = join(map(range(1, 17), 'screenstring(3, v:val)'), '')
1464 call assert_equal('xvimxxxxxxxxxxxxx', line)
1465 call popup_close(winid)
1466
1467 call cursor(3, 3)
1468 redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001469 let winid = popup_create('vim', #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001470 \ line: 'cursor-2',
1471 \ col: 'cursor-1',
1472 \})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001473 redraw
1474 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
1475 call assert_equal('xvimxxxxxxxxxxxxx', line)
1476 call popup_close(winid)
1477
Bram Moolenaar402502d2019-05-30 22:07:36 +02001478 " just enough room above
1479 call cursor(3, 3)
1480 redraw
1481 let winid = popup_atcursor(['vim', 'is great'], {})
1482 redraw
1483 let pos = popup_getpos(winid)
1484 call assert_equal(1, pos.line)
1485 call popup_close(winid)
1486
1487 " not enough room above, popup goes below the cursor
1488 call cursor(3, 3)
1489 redraw
1490 let winid = popup_atcursor(['vim', 'is', 'great'], {})
1491 redraw
1492 let pos = popup_getpos(winid)
1493 call assert_equal(4, pos.line)
1494 call popup_close(winid)
1495
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001496 " cursor in first line, popup in line 2
1497 call cursor(1, 1)
1498 redraw
1499 let winid = popup_atcursor(['vim', 'is', 'great'], {})
1500 redraw
1501 let pos = popup_getpos(winid)
1502 call assert_equal(2, pos.line)
1503 call popup_close(winid)
1504
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001505 bwipe!
1506endfunc
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001507
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001508func Test_popup_atcursor_pos()
1509 CheckScreendump
Dominique Pellec60e9592021-07-03 21:41:38 +02001510 CheckFeature conceal
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001511
1512 let lines =<< trim END
1513 call setline(1, repeat([repeat('-', 60)], 15))
1514 set so=0
1515
1516 normal 9G3|r#
1517 let winid1 = popup_atcursor(['first', 'second'], #{
1518 \ moved: [0, 0, 0],
1519 \ })
1520 normal 9G21|r&
1521 let winid1 = popup_atcursor(['FIrsT', 'SEcoND'], #{
1522 \ pos: 'botright',
1523 \ moved: [0, 0, 0],
1524 \ })
1525 normal 3G27|r%
1526 let winid1 = popup_atcursor(['fiRSt', 'seCOnd'], #{
1527 \ pos: 'topleft',
1528 \ moved: [0, 0, 0],
1529 \ })
1530 normal 3G45|r@
1531 let winid1 = popup_atcursor(['First', 'SeconD'], #{
1532 \ pos: 'topright',
Bram Moolenaarb0992022020-01-30 14:55:42 +01001533 \ moved: range(3),
1534 \ mousemoved: range(3),
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001535 \ })
Bram Moolenaarea042672021-06-29 20:22:32 +02001536
1537 normal 9G27|Rconcealed X
1538 syn match Hidden /concealed/ conceal
1539 set conceallevel=2 concealcursor=n
1540 redraw
1541 normal 0fX
1542 call popup_atcursor('mark', {})
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001543 END
1544 call writefile(lines, 'XtestPopupAtcursorPos')
1545 let buf = RunVimInTerminal('-S XtestPopupAtcursorPos', #{rows: 12})
1546 call VerifyScreenDump(buf, 'Test_popupwin_atcursor_pos', {})
1547
1548 " clean up
1549 call StopVimInTerminal(buf)
1550 call delete('XtestPopupAtcursorPos')
1551endfunc
1552
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001553func Test_popup_beval()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001554 CheckScreendump
Bram Moolenaar1e82a782019-09-21 22:57:06 +02001555 CheckFeature balloon_eval_term
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001556
1557 let lines =<< trim END
1558 call setline(1, range(1, 20))
1559 call setline(5, 'here is some text to hover over')
1560 set balloonevalterm
1561 set balloonexpr=BalloonExpr()
1562 set balloondelay=100
1563 func BalloonExpr()
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001564 let s:winid = [v:beval_text]->popup_beval({})
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001565 return ''
1566 endfunc
1567 func Hover()
1568 call test_setmouse(5, 15)
1569 call feedkeys("\<MouseMove>\<Ignore>", "xt")
1570 sleep 100m
1571 endfunc
1572 func MoveOntoPopup()
1573 call test_setmouse(4, 17)
1574 call feedkeys("\<F4>\<MouseMove>\<Ignore>", "xt")
1575 endfunc
1576 func MoveAway()
1577 call test_setmouse(5, 13)
1578 call feedkeys("\<F5>\<MouseMove>\<Ignore>", "xt")
1579 endfunc
1580 END
1581 call writefile(lines, 'XtestPopupBeval')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001582 let buf = RunVimInTerminal('-S XtestPopupBeval', #{rows: 10})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001583 call TermWait(buf, 50)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001584 call term_sendkeys(buf, 'j')
1585 call term_sendkeys(buf, ":call Hover()\<CR>")
1586 call VerifyScreenDump(buf, 'Test_popupwin_beval_1', {})
1587
1588 call term_sendkeys(buf, ":call MoveOntoPopup()\<CR>")
1589 call VerifyScreenDump(buf, 'Test_popupwin_beval_2', {})
1590
1591 call term_sendkeys(buf, ":call MoveAway()\<CR>")
1592 call VerifyScreenDump(buf, 'Test_popupwin_beval_3', {})
1593
1594 " clean up
1595 call StopVimInTerminal(buf)
1596 call delete('XtestPopupBeval')
1597endfunc
1598
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001599func Test_popup_filter()
1600 new
1601 call setline(1, 'some text')
1602
1603 func MyPopupFilter(winid, c)
Bram Moolenaarec084d32020-02-28 22:44:47 +01001604 if a:c == 'e' || a:c == "\<F9>"
1605 let g:eaten = a:c
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001606 return 1
1607 endif
1608 if a:c == '0'
1609 let g:ignored = '0'
1610 return 0
1611 endif
1612 if a:c == 'x'
1613 call popup_close(a:winid)
1614 return 1
1615 endif
1616 return 0
1617 endfunc
1618
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001619 let winid = 'something'->popup_create(#{filter: 'MyPopupFilter'})
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001620 redraw
1621
1622 " e is consumed by the filter
Bram Moolenaar999db232021-07-04 14:00:55 +02001623 let g:eaten = ''
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001624 call feedkeys('e', 'xt')
1625 call assert_equal('e', g:eaten)
Bram Moolenaarec084d32020-02-28 22:44:47 +01001626 call feedkeys("\<F9>", 'xt')
1627 call assert_equal("\<F9>", g:eaten)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001628
1629 " 0 is ignored by the filter
Bram Moolenaar999db232021-07-04 14:00:55 +02001630 let g:ignored = ''
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001631 normal $
1632 call assert_equal(9, getcurpos()[2])
1633 call feedkeys('0', 'xt')
1634 call assert_equal('0', g:ignored)
Bram Moolenaar999db232021-07-04 14:00:55 +02001635
1636 if has('win32') && has('gui_running')
1637 echo "FIXME: this check is very flaky on MS-Windows GUI, the cursor doesn't move"
1638 else
1639 call assert_equal(1, getcurpos()[2])
1640 endif
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001641
1642 " x closes the popup
1643 call feedkeys('x', 'xt')
Bram Moolenaarec084d32020-02-28 22:44:47 +01001644 call assert_equal("\<F9>", g:eaten)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001645 call assert_equal(-1, winbufnr(winid))
1646
Bram Moolenaar999db232021-07-04 14:00:55 +02001647 unlet g:eaten
1648 unlet g:ignored
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001649 delfunc MyPopupFilter
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001650 call popup_clear()
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001651endfunc
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001652
Bram Moolenaar4934ad02020-09-28 22:29:58 +02001653" this tests that the filter is not used for :normal command
Bram Moolenaar189832b2020-09-23 12:29:11 +02001654func Test_popup_filter_normal_cmd()
1655 CheckScreendump
1656
1657 let lines =<< trim END
Bram Moolenaar4934ad02020-09-28 22:29:58 +02001658 let text = range(1, 20)->map({_, v -> string(v)})
1659 let g:winid = popup_create(text, #{maxheight: 5, minwidth: 3, filter: 'invalidfilter'})
1660 call timer_start(0, {-> win_execute(g:winid, 'norm! 10Gzz')})
Bram Moolenaar189832b2020-09-23 12:29:11 +02001661 END
1662 call writefile(lines, 'XtestPopupNormal')
1663 let buf = RunVimInTerminal('-S XtestPopupNormal', #{rows: 10})
1664 call TermWait(buf, 100)
1665 call VerifyScreenDump(buf, 'Test_popupwin_normal_cmd', {})
1666
1667 call StopVimInTerminal(buf)
1668 call delete('XtestPopupNormal')
1669endfunc
1670
Bram Moolenaar10476522020-09-24 22:57:31 +02001671" test that cursor line highlight is updated after using win_execute()
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02001672func Test_popup_filter_win_execute()
1673 CheckScreendump
1674
1675 let lines =<< trim END
Bram Moolenaar10476522020-09-24 22:57:31 +02001676 let lines = range(1, &lines * 2)->map({_, v -> string(v)})
1677 let g:id = popup_create(lines, #{
1678 \ minheight: &lines - 5,
1679 \ maxheight: &lines - 5,
1680 \ cursorline: 1,
1681 \ })
1682 redraw
1683 END
1684 call writefile(lines, 'XtestPopupWinExecute')
1685 let buf = RunVimInTerminal('-S XtestPopupWinExecute', #{rows: 14})
1686
1687 call term_sendkeys(buf, ":call win_execute(g:id, ['normal 17Gzz'])\<CR>")
1688 call term_sendkeys(buf, ":\<CR>")
1689
1690 call VerifyScreenDump(buf, 'Test_popupwin_win_execute_cursorline', {})
1691
1692 call StopVimInTerminal(buf)
1693 call delete('XtestPopupWinExecute')
1694endfunc
1695
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02001696func Test_popup_set_firstline()
1697 CheckScreendump
1698
1699 let lines =<< trim END
1700 let lines = range(1, 50)->map({_, v -> string(v)})
1701 let g:id = popup_create(lines, #{
1702 \ minwidth: 20,
1703 \ maxwidth: 20,
1704 \ minheight: &lines - 5,
1705 \ maxheight: &lines - 5,
1706 \ cursorline: 1,
1707 \ })
1708 call popup_setoptions(g:id, #{firstline: 10})
1709 redraw
1710 END
1711 call writefile(lines, 'XtestPopupWinSetFirstline')
1712 let buf = RunVimInTerminal('-S XtestPopupWinSetFirstline', #{rows: 16})
1713
1714 call VerifyScreenDump(buf, 'Test_popupwin_set_firstline_1', {})
1715
1716 call term_sendkeys(buf, ":call popup_setoptions(g:id, #{firstline: 5})\<CR>")
1717 call term_sendkeys(buf, ":\<CR>")
1718 call VerifyScreenDump(buf, 'Test_popupwin_set_firstline_2', {})
1719
1720 call StopVimInTerminal(buf)
1721 call delete('XtestPopupWinSetFirstline')
1722endfunc
1723
Bram Moolenaar10476522020-09-24 22:57:31 +02001724" this tests that we don't get stuck with an error in "win_execute()"
1725func Test_popup_filter_win_execute_error()
1726 CheckScreendump
1727
1728 let lines =<< trim END
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02001729 let g:winid = popup_create('some text', {'filter': 'invalidfilter'})
1730 call timer_start(0, {-> win_execute(g:winid, 'invalidCommand')})
1731 END
Bram Moolenaar10476522020-09-24 22:57:31 +02001732 call writefile(lines, 'XtestPopupWinExecuteError')
1733 let buf = RunVimInTerminal('-S XtestPopupWinExecuteError', #{rows: 10, wait_for_ruler: 0})
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02001734
1735 call WaitFor({-> term_getline(buf, 9) =~ 'Not an editor command: invalidCommand'})
1736 call term_sendkeys(buf, "\<CR>")
1737 call WaitFor({-> term_getline(buf, 9) =~ 'Unknown function: invalidfilter'})
1738 call term_sendkeys(buf, "\<CR>")
1739 call WaitFor({-> term_getline(buf, 9) =~ 'Not allowed in a popup window'})
1740 call term_sendkeys(buf, "\<CR>")
1741 call term_sendkeys(buf, "\<CR>")
1742 call VerifyScreenDump(buf, 'Test_popupwin_win_execute', {})
1743
1744 call StopVimInTerminal(buf)
Bram Moolenaar10476522020-09-24 22:57:31 +02001745 call delete('XtestPopupWinExecuteError')
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02001746endfunc
1747
Bram Moolenaara42d9452019-06-15 21:46:30 +02001748func ShowDialog(key, result)
1749 let s:cb_res = 999
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001750 let winid = popup_dialog('do you want to quit (Yes/no)?', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001751 \ filter: 'popup_filter_yesno',
1752 \ callback: 'QuitCallback',
Bram Moolenaara42d9452019-06-15 21:46:30 +02001753 \ })
1754 redraw
1755 call feedkeys(a:key, "xt")
1756 call assert_equal(winid, s:cb_winid)
1757 call assert_equal(a:result, s:cb_res)
1758endfunc
1759
1760func Test_popup_dialog()
1761 func QuitCallback(id, res)
1762 let s:cb_winid = a:id
1763 let s:cb_res = a:res
1764 endfunc
1765
1766 let winid = ShowDialog("y", 1)
1767 let winid = ShowDialog("Y", 1)
1768 let winid = ShowDialog("n", 0)
1769 let winid = ShowDialog("N", 0)
1770 let winid = ShowDialog("x", 0)
1771 let winid = ShowDialog("X", 0)
1772 let winid = ShowDialog("\<Esc>", 0)
1773 let winid = ShowDialog("\<C-C>", -1)
1774
1775 delfunc QuitCallback
1776endfunc
1777
Bram Moolenaara730e552019-06-16 19:05:31 +02001778func ShowMenu(key, result)
1779 let s:cb_res = 999
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001780 let winid = popup_menu(['one', 'two', 'something else'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001781 \ callback: 'QuitCallback',
Bram Moolenaara730e552019-06-16 19:05:31 +02001782 \ })
1783 redraw
1784 call feedkeys(a:key, "xt")
1785 call assert_equal(winid, s:cb_winid)
1786 call assert_equal(a:result, s:cb_res)
1787endfunc
1788
1789func Test_popup_menu()
1790 func QuitCallback(id, res)
1791 let s:cb_winid = a:id
1792 let s:cb_res = a:res
1793 endfunc
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001794 " mapping won't be used in popup
1795 map j k
Bram Moolenaara730e552019-06-16 19:05:31 +02001796
1797 let winid = ShowMenu(" ", 1)
1798 let winid = ShowMenu("j \<CR>", 2)
1799 let winid = ShowMenu("JjK \<CR>", 2)
1800 let winid = ShowMenu("jjjjjj ", 3)
1801 let winid = ShowMenu("kkk ", 1)
1802 let winid = ShowMenu("x", -1)
1803 let winid = ShowMenu("X", -1)
1804 let winid = ShowMenu("\<Esc>", -1)
1805 let winid = ShowMenu("\<C-C>", -1)
1806
1807 delfunc QuitCallback
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001808 unmap j
Bram Moolenaara730e552019-06-16 19:05:31 +02001809endfunc
1810
1811func Test_popup_menu_screenshot()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001812 CheckScreendump
Bram Moolenaara730e552019-06-16 19:05:31 +02001813
1814 let lines =<< trim END
1815 call setline(1, range(1, 20))
1816 hi PopupSelected ctermbg=lightblue
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001817 call popup_menu(['one', 'two', 'another'], #{callback: 'MenuDone', title: ' make a choice from the list '})
Bram Moolenaara730e552019-06-16 19:05:31 +02001818 func MenuDone(id, res)
1819 echomsg "selected " .. a:res
1820 endfunc
1821 END
1822 call writefile(lines, 'XtestPopupMenu')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001823 let buf = RunVimInTerminal('-S XtestPopupMenu', #{rows: 10})
Bram Moolenaara730e552019-06-16 19:05:31 +02001824 call VerifyScreenDump(buf, 'Test_popupwin_menu_01', {})
1825
1826 call term_sendkeys(buf, "jj")
1827 call VerifyScreenDump(buf, 'Test_popupwin_menu_02', {})
1828
1829 call term_sendkeys(buf, " ")
1830 call VerifyScreenDump(buf, 'Test_popupwin_menu_03', {})
1831
1832 " clean up
1833 call StopVimInTerminal(buf)
1834 call delete('XtestPopupMenu')
1835endfunc
1836
Bram Moolenaarf914a332019-07-20 15:09:56 +02001837func Test_popup_menu_narrow()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001838 CheckScreendump
Bram Moolenaarf914a332019-07-20 15:09:56 +02001839
1840 let lines =<< trim END
1841 call setline(1, range(1, 20))
1842 hi PopupSelected ctermbg=green
1843 call popup_menu(['one', 'two', 'three'], #{callback: 'MenuDone'})
1844 func MenuDone(id, res)
1845 echomsg "selected " .. a:res
1846 endfunc
1847 END
1848 call writefile(lines, 'XtestPopupNarrowMenu')
1849 let buf = RunVimInTerminal('-S XtestPopupNarrowMenu', #{rows: 10})
1850 call VerifyScreenDump(buf, 'Test_popupwin_menu_04', {})
1851
1852 " clean up
1853 call term_sendkeys(buf, "x")
1854 call StopVimInTerminal(buf)
1855 call delete('XtestPopupNarrowMenu')
1856endfunc
1857
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001858func Test_popup_title()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001859 CheckScreendump
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001860
1861 " Create a popup without title or border, a line of padding will be added to
1862 " put the title on.
1863 let lines =<< trim END
1864 call setline(1, range(1, 20))
Bram Moolenaar5d458a72019-08-04 21:12:15 +02001865 let winid = popup_create(['one', 'two', 'another'], #{title: 'Title String'})
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001866 END
1867 call writefile(lines, 'XtestPopupTitle')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001868 let buf = RunVimInTerminal('-S XtestPopupTitle', #{rows: 10})
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001869 call VerifyScreenDump(buf, 'Test_popupwin_title', {})
1870
Bram Moolenaar5d458a72019-08-04 21:12:15 +02001871 call term_sendkeys(buf, ":call popup_setoptions(winid, #{maxwidth: 20, title: 'a very long title that is not going to fit'})\<CR>")
1872 call term_sendkeys(buf, ":\<CR>")
1873 call VerifyScreenDump(buf, 'Test_popupwin_longtitle_1', {})
1874
1875 call term_sendkeys(buf, ":call popup_setoptions(winid, #{border: []})\<CR>")
1876 call term_sendkeys(buf, ":\<CR>")
1877 call VerifyScreenDump(buf, 'Test_popupwin_longtitle_2', {})
1878
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01001879 call term_sendkeys(buf, ":call popup_clear()\<CR>")
1880 call term_sendkeys(buf, ":call popup_create(['aaa', 'bbb'], #{title: 'Title', minwidth: 12, padding: [2, 2, 2, 2]})\<CR>")
1881 call term_sendkeys(buf, ":\<CR>")
1882 call VerifyScreenDump(buf, 'Test_popupwin_longtitle_3', {})
1883
1884 call term_sendkeys(buf, ":call popup_clear()\<CR>")
1885 call term_sendkeys(buf, ":call popup_create(['aaa', 'bbb'], #{title: 'Title', minwidth: 12, border: [], padding: [2, 2, 2, 2]})\<CR>")
1886 call term_sendkeys(buf, ":\<CR>")
1887 call VerifyScreenDump(buf, 'Test_popupwin_longtitle_4', {})
1888
Ralf Schandlbc869872021-05-28 14:12:14 +02001889 call term_sendkeys(buf, ":call popup_clear()\<CR>")
rbtnnc6119412021-08-07 13:08:45 +02001890 call term_sendkeys(buf, ":call popup_menu(['This is a line', 'and another line'], #{title: '▶Äあいうえお◀', })\<CR>")
Ralf Schandlbc869872021-05-28 14:12:14 +02001891 call VerifyScreenDump(buf, 'Test_popupwin_multibytetitle', {})
1892 call term_sendkeys(buf, "x")
1893
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001894 " clean up
1895 call StopVimInTerminal(buf)
1896 call delete('XtestPopupTitle')
Bram Moolenaarae943152019-06-16 22:54:14 +02001897
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001898 let winid = popup_create('something', #{title: 'Some Title'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001899 call assert_equal('Some Title', popup_getoptions(winid).title)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001900 call popup_setoptions(winid, #{title: 'Another Title'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001901 call assert_equal('Another Title', popup_getoptions(winid).title)
1902
1903 call popup_clear()
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001904endfunc
1905
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001906func Test_popup_close_callback()
1907 func PopupDone(id, result)
1908 let g:result = a:result
1909 endfunc
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001910 let winid = popup_create('something', #{callback: 'PopupDone'})
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001911 redraw
1912 call popup_close(winid, 'done')
1913 call assert_equal('done', g:result)
1914endfunc
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001915
1916func Test_popup_empty()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001917 let winid = popup_create('', #{padding: [2,2,2,2]})
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001918 redraw
1919 let pos = popup_getpos(winid)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001920 call assert_equal(5, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001921 call assert_equal(5, pos.height)
Bram Moolenaaref6b9792020-05-13 16:34:15 +02001922 call popup_close(winid)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001923
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001924 let winid = popup_create([], #{border: []})
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001925 redraw
1926 let pos = popup_getpos(winid)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001927 call assert_equal(3, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001928 call assert_equal(3, pos.height)
Bram Moolenaaref6b9792020-05-13 16:34:15 +02001929 call popup_close(winid)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001930endfunc
Bram Moolenaar988c4332019-06-02 14:12:11 +02001931
1932func Test_popup_never_behind()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001933 CheckScreendump
1934
Bram Moolenaar988c4332019-06-02 14:12:11 +02001935 " +-----------------------------+
1936 " | | |
1937 " | | |
1938 " | | |
1939 " | line1 |
1940 " |------------line2------------|
1941 " | line3 |
1942 " | line4 |
1943 " | |
1944 " | |
1945 " +-----------------------------+
1946 let lines =<< trim END
Bram Moolenaar988c4332019-06-02 14:12:11 +02001947 split
1948 vsplit
1949 let info_window1 = getwininfo()[0]
1950 let line = info_window1['height']
1951 let col = info_window1['width']
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001952 call popup_create(['line1', 'line2', 'line3', 'line4'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001953 \ line : line,
1954 \ col : col,
Bram Moolenaar988c4332019-06-02 14:12:11 +02001955 \ })
1956 END
1957 call writefile(lines, 'XtestPopupBehind')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001958 let buf = RunVimInTerminal('-S XtestPopupBehind', #{rows: 10})
Bram Moolenaar988c4332019-06-02 14:12:11 +02001959 call term_sendkeys(buf, "\<C-W>w")
1960 call VerifyScreenDump(buf, 'Test_popupwin_behind', {})
1961
1962 " clean up
1963 call StopVimInTerminal(buf)
1964 call delete('XtestPopupBehind')
1965endfunc
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001966
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001967func s:VerifyPosition(p, msg, line, col, width, height)
1968 call assert_equal(a:line, popup_getpos(a:p).line, a:msg . ' (l)')
1969 call assert_equal(a:col, popup_getpos(a:p).col, a:msg . ' (c)')
1970 call assert_equal(a:width, popup_getpos(a:p).width, a:msg . ' (w)')
1971 call assert_equal(a:height, popup_getpos(a:p).height, a:msg . ' (h)')
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001972endfunc
1973
1974func Test_popup_position_adjust()
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001975 " Anything placed past the last cell on the right of the screen is moved to
1976 " the left.
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001977 "
1978 " When wrapping is disabled, we also shift to the left to display on the
1979 " screen, unless fixed is set.
1980
1981 " Entries for cases which don't vary based on wrapping.
1982 " Format is per tests described below
1983 let both_wrap_tests = [
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001984 \ ['a', 5, &columns, 5, &columns, 1, 1],
1985 \ ['b', 5, &columns + 1, 5, &columns, 1, 1],
1986 \ ['c', 5, &columns - 1, 5, &columns - 1, 1, 1],
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001987 \ ['d', 5, &columns - 2, 5, &columns - 2, 1, 1],
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001988 \ ['e', 5, &columns - 3, 5, &columns - 3, 1, 1]]
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001989
1990 " these test groups are dicts with:
1991 " - comment: something to identify the group of tests by
1992 " - options: dict of options to merge with the row/col in tests
1993 " - tests: list of cases. Each one is a list with elements:
1994 " - text
1995 " - row
1996 " - col
1997 " - expected row
1998 " - expected col
1999 " - expected width
2000 " - expected height
2001 let tests = [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002002 \ #{
2003 \ comment: 'left-aligned with wrapping',
2004 \ options: #{
2005 \ wrap: 1,
2006 \ pos: 'botleft',
2007 \ },
2008 \ tests: both_wrap_tests + [
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01002009 \ ['aa', 5, &columns, 4, &columns, 1, 2],
2010 \ ['bb', 5, &columns + 1, 4, &columns, 1, 2],
2011 \ ['cc', 5, &columns - 1, 5, &columns - 1, 2, 1],
2012 \ ['dd', 5, &columns - 2, 5, &columns - 2, 2, 1],
2013 \ ['ee', 5, &columns - 3, 5, &columns - 3, 2, 1],
2014 \
2015 \ ['aaa', 5, &columns, 3, &columns, 1, 3],
2016 \ ['bbb', 5, &columns + 1, 3, &columns, 1, 3],
2017 \ ['ccc', 5, &columns - 1, 4, &columns - 1, 2, 2],
2018 \ ['ddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
2019 \ ['eee', 5, &columns - 3, 5, &columns - 3, 3, 1],
2020 \
2021 \ ['aaaa', 5, &columns, 2, &columns, 1, 4],
2022 \ ['bbbb', 5, &columns + 1, 2, &columns, 1, 4],
2023 \ ['cccc', 5, &columns - 1, 4, &columns - 1, 2, 2],
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002024 \ ['dddd', 5, &columns - 2, 4, &columns - 2, 3, 2],
2025 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01002026 \ ['eeee', 5, &columns - 4, 5, &columns - 4, 4, 1],
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002027 \ ],
2028 \ },
2029 \ #{
2030 \ comment: 'left aligned without wrapping',
2031 \ options: #{
2032 \ wrap: 0,
2033 \ pos: 'botleft',
2034 \ },
2035 \ tests: both_wrap_tests + [
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01002036 \ ['aa', 5, &columns, 5, &columns - 1, 2, 1],
2037 \ ['bb', 5, &columns + 1, 5, &columns - 1, 2, 1],
2038 \ ['cc', 5, &columns - 1, 5, &columns - 1, 2, 1],
2039 \ ['dd', 5, &columns - 2, 5, &columns - 2, 2, 1],
2040 \ ['ee', 5, &columns - 3, 5, &columns - 3, 2, 1],
2041 \
2042 \ ['aaa', 5, &columns, 5, &columns - 2, 3, 1],
2043 \ ['bbb', 5, &columns + 1, 5, &columns - 2, 3, 1],
2044 \ ['ccc', 5, &columns - 1, 5, &columns - 2, 3, 1],
2045 \ ['ddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
2046 \ ['eee', 5, &columns - 3, 5, &columns - 3, 3, 1],
2047 \
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002048 \ ['aaaa', 5, &columns, 5, &columns - 3, 4, 1],
2049 \ ['bbbb', 5, &columns + 1, 5, &columns - 3, 4, 1],
2050 \ ['cccc', 5, &columns - 1, 5, &columns - 3, 4, 1],
2051 \ ['dddd', 5, &columns - 2, 5, &columns - 3, 4, 1],
2052 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
2053 \ ],
2054 \ },
2055 \ #{
2056 \ comment: 'left aligned with fixed position',
2057 \ options: #{
2058 \ wrap: 0,
2059 \ fixed: 1,
2060 \ pos: 'botleft',
2061 \ },
2062 \ tests: both_wrap_tests + [
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01002063 \ ['aa', 5, &columns, 5, &columns, 1, 1],
2064 \ ['bb', 5, &columns + 1, 5, &columns, 1, 1],
2065 \ ['cc', 5, &columns - 1, 5, &columns - 1, 2, 1],
2066 \ ['dd', 5, &columns - 2, 5, &columns - 2, 2, 1],
2067 \ ['ee', 5, &columns - 3, 5, &columns - 3, 2, 1],
2068 \
2069 \ ['aaa', 5, &columns, 5, &columns, 1, 1],
2070 \ ['bbb', 5, &columns + 1, 5, &columns, 1, 1],
2071 \ ['ccc', 5, &columns - 1, 5, &columns - 1, 2, 1],
2072 \ ['ddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
2073 \ ['eee', 5, &columns - 3, 5, &columns - 3, 3, 1],
2074 \
2075 \ ['aaaa', 5, &columns, 5, &columns, 1, 1],
2076 \ ['bbbb', 5, &columns + 1, 5, &columns, 1, 1],
2077 \ ['cccc', 5, &columns - 1, 5, &columns - 1, 2, 1],
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002078 \ ['dddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
2079 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
2080 \ ],
2081 \ },
2082 \ ]
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002083
2084 for test_group in tests
2085 for test in test_group.tests
2086 let [ text, line, col, e_line, e_col, e_width, e_height ] = test
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002087 let options = #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002088 \ line: line,
2089 \ col: col,
2090 \ }
2091 call extend(options, test_group.options)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002092
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002093 let p = popup_create(text, options)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002094
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002095 let msg = string(extend(options, #{text: text}))
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002096 call s:VerifyPosition(p, msg, e_line, e_col, e_width, e_height)
2097 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002098 endfor
2099 endfor
2100
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002101 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002102 %bwipe!
2103endfunc
2104
Bram Moolenaar3397f742019-06-02 18:40:06 +02002105func Test_adjust_left_past_screen_width()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002106 " width of screen
2107 let X = join(map(range(&columns), {->'X'}), '')
2108
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002109 let p = popup_create(X, #{line: 1, col: 1, wrap: 0})
2110 call s:VerifyPosition(p, 'full width topleft', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002111
2112 redraw
2113 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
2114 call assert_equal(X, line)
2115
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002116 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002117 redraw
2118
2119 " Same if placed on the right hand side
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002120 let p = popup_create(X, #{line: 1, col: &columns, wrap: 0})
2121 call s:VerifyPosition(p, 'full width topright', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002122
2123 redraw
2124 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
2125 call assert_equal(X, line)
2126
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002127 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002128 redraw
2129
2130 " Extend so > window width
2131 let X .= 'x'
2132
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002133 let p = popup_create(X, #{line: 1, col: 1, wrap: 0})
2134 call s:VerifyPosition(p, 'full width + 1 topleft', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002135
2136 redraw
2137 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
2138 call assert_equal(X[ : -2 ], line)
2139
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002140 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002141 redraw
2142
2143 " Shifted then truncated (the x is not visible)
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002144 let p = popup_create(X, #{line: 1, col: &columns - 3, wrap: 0})
2145 call s:VerifyPosition(p, 'full width + 1 topright', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002146
2147 redraw
2148 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
2149 call assert_equal(X[ : -2 ], line)
2150
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002151 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002152 redraw
2153
2154 " Not shifted, just truncated
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002155 let p = popup_create(X,
2156 \ #{line: 1, col: 2, wrap: 0, fixed: 1})
2157 call s:VerifyPosition(p, 'full width + 1 fixed', 1, 2, &columns - 1, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002158
2159 redraw
2160 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
2161 let e_line = ' ' . X[ 1 : -2 ]
2162 call assert_equal(e_line, line)
2163
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002164 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002165 redraw
2166
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002167 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002168 %bwipe!
Bram Moolenaar3397f742019-06-02 18:40:06 +02002169endfunc
2170
2171func Test_popup_moved()
2172 new
2173 call test_override('char_avail', 1)
2174 call setline(1, ['one word to move around', 'a WORD.and->some thing'])
2175
2176 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002177 let winid = popup_atcursor('text', #{moved: 'any'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02002178 redraw
2179 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002180 call assert_equal([1, 4, 4], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02002181 " trigger the check for last_cursormoved by going into insert mode
2182 call feedkeys("li\<Esc>", 'xt')
2183 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002184 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02002185
2186 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002187 let winid = popup_atcursor('text', #{moved: 'word'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02002188 redraw
2189 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002190 call assert_equal([1, 4, 7], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02002191 call feedkeys("hi\<Esc>", 'xt')
2192 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002193 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02002194
2195 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002196 let winid = popup_atcursor('text', #{moved: 'word'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02002197 redraw
2198 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002199 call assert_equal([1, 4, 7], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02002200 call feedkeys("li\<Esc>", 'xt')
2201 call assert_equal(1, popup_getpos(winid).visible)
2202 call feedkeys("ei\<Esc>", 'xt')
2203 call assert_equal(1, popup_getpos(winid).visible)
2204 call feedkeys("eli\<Esc>", 'xt')
2205 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002206 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02002207
Bram Moolenaar17627312019-06-02 19:53:44 +02002208 " WORD is the default
Bram Moolenaar3397f742019-06-02 18:40:06 +02002209 exe "normal gg0/WORD\<CR>"
Bram Moolenaar17627312019-06-02 19:53:44 +02002210 let winid = popup_atcursor('text', {})
Bram Moolenaar3397f742019-06-02 18:40:06 +02002211 redraw
2212 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002213 call assert_equal([2, 2, 15], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02002214 call feedkeys("eli\<Esc>", 'xt')
2215 call assert_equal(1, popup_getpos(winid).visible)
2216 call feedkeys("wi\<Esc>", 'xt')
2217 call assert_equal(1, popup_getpos(winid).visible)
2218 call feedkeys("Eli\<Esc>", 'xt')
2219 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002220 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02002221
2222 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002223 let winid = popup_atcursor('text', #{moved: [5, 10]})
Bram Moolenaar3397f742019-06-02 18:40:06 +02002224 redraw
2225 call assert_equal(1, popup_getpos(winid).visible)
2226 call feedkeys("eli\<Esc>", 'xt')
2227 call feedkeys("ei\<Esc>", 'xt')
2228 call assert_equal(1, popup_getpos(winid).visible)
2229 call feedkeys("eli\<Esc>", 'xt')
2230 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002231 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02002232
2233 bwipe!
2234 call test_override('ALL', 0)
2235endfunc
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002236
2237func Test_notifications()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002238 CheckFeature timers
2239 CheckScreendump
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002240
Bram Moolenaar0fdddee2019-09-01 15:26:23 +02002241 let lines =<< trim END
2242 call setline(1, range(1, 20))
2243 hi Notification ctermbg=lightblue
2244 call popup_notification('first notification', {})
2245 END
2246 call writefile(lines, 'XtestNotifications')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002247 let buf = RunVimInTerminal('-S XtestNotifications', #{rows: 10})
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002248 call VerifyScreenDump(buf, 'Test_popupwin_notify_01', {})
2249
2250 " second one goes below the first one
Bram Moolenaardfa97f22019-06-15 14:31:55 +02002251 call term_sendkeys(buf, ":hi link PopupNotification Notification\<CR>")
2252 call term_sendkeys(buf, ":call popup_notification('another important notification', {})\<CR>")
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002253 call VerifyScreenDump(buf, 'Test_popupwin_notify_02', {})
2254
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002255 " clean up
2256 call StopVimInTerminal(buf)
2257 call delete('XtestNotifications')
2258endfunc
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002259
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002260func Test_popup_scrollbar()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002261 CheckScreendump
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002262
2263 let lines =<< trim END
2264 call setline(1, range(1, 20))
Bram Moolenaar8da41812019-06-26 18:04:54 +02002265 hi ScrollThumb ctermbg=blue
2266 hi ScrollBar ctermbg=red
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002267 let winid = popup_create(['one', 'two', 'three', 'four', 'five',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002268 \ 'six', 'seven', 'eight', 'nine'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002269 \ minwidth: 8,
2270 \ maxheight: 4,
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002271 \ })
Bram Moolenaar53a95d62019-06-26 03:54:08 +02002272 func ScrollUp()
2273 call feedkeys("\<F3>\<ScrollWheelUp>", "xt")
2274 endfunc
2275 func ScrollDown()
2276 call feedkeys("\<F3>\<ScrollWheelDown>", "xt")
2277 endfunc
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02002278 func ClickTop()
2279 call feedkeys("\<F4>\<LeftMouse>", "xt")
2280 endfunc
2281 func ClickBot()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002282 call popup_setoptions(g:winid, #{border: [], close: 'button'})
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02002283 call feedkeys("\<F5>\<LeftMouse>", "xt")
2284 endfunc
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002285 func Popup_filter(winid, key)
2286 if a:key == 'j'
Bram Moolenaar371806e2020-10-22 13:44:54 +02002287 silent! this_throws_an_error_but_is_ignored
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002288 let line = popup_getoptions(a:winid).firstline
2289 let nlines = line('$', a:winid)
2290 let newline = line < nlines ? (line + 1) : nlines
2291 call popup_setoptions(a:winid, #{firstline: newline})
2292 return v:true
2293 elseif a:key == 'x'
2294 call popup_close(a:winid)
2295 return v:true
2296 endif
2297 endfunc
2298
Bram Moolenaar6c542f72020-09-27 21:16:45 +02002299 def CreatePopup(text: list<string>)
Bram Moolenaare0de1712020-12-02 17:36:54 +01002300 popup_create(text, {
Bram Moolenaar6c542f72020-09-27 21:16:45 +02002301 \ minwidth: 30,
2302 \ maxwidth: 30,
2303 \ minheight: 4,
2304 \ maxheight: 4,
2305 \ firstline: 1,
2306 \ lastline: 4,
2307 \ wrap: true,
2308 \ scrollbar: true,
2309 \ mapping: false,
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002310 \ filter: g:Popup_filter,
Bram Moolenaar6c542f72020-09-27 21:16:45 +02002311 \ })
2312 enddef
2313
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002314 func PopupScroll()
2315 call popup_clear()
2316 let text =<< trim END
2317 1
2318 2
2319 3
2320 4
2321 long line long line long line long line long line long line
2322 long line long line long line long line long line long line
2323 long line long line long line long line long line long line
2324 END
Bram Moolenaar6c542f72020-09-27 21:16:45 +02002325 call CreatePopup(text)
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002326 endfunc
Bram Moolenaar53a95d62019-06-26 03:54:08 +02002327 map <silent> <F3> :call test_setmouse(5, 36)<CR>
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02002328 map <silent> <F4> :call test_setmouse(4, 42)<CR>
2329 map <silent> <F5> :call test_setmouse(7, 42)<CR>
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002330 END
2331 call writefile(lines, 'XtestPopupScroll')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002332 let buf = RunVimInTerminal('-S XtestPopupScroll', #{rows: 10})
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002333 call VerifyScreenDump(buf, 'Test_popupwin_scroll_1', {})
2334
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002335 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 2})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002336 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002337 call VerifyScreenDump(buf, 'Test_popupwin_scroll_2', {})
2338
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002339 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 6})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002340 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002341 call VerifyScreenDump(buf, 'Test_popupwin_scroll_3', {})
2342
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002343 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 9})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002344 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002345 call VerifyScreenDump(buf, 'Test_popupwin_scroll_4', {})
2346
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02002347 call term_sendkeys(buf, ":call popup_setoptions(winid, #{scrollbarhighlight: 'ScrollBar', thumbhighlight: 'ScrollThumb', firstline: 5})\<CR>")
Bram Moolenaara112f2d2019-09-01 17:38:09 +02002348 " this scrolls two lines (half the window height)
Bram Moolenaar53a95d62019-06-26 03:54:08 +02002349 call term_sendkeys(buf, ":call ScrollUp()\<CR>")
2350 call VerifyScreenDump(buf, 'Test_popupwin_scroll_5', {})
2351
2352 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
2353 call VerifyScreenDump(buf, 'Test_popupwin_scroll_6', {})
2354
2355 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
Bram Moolenaar13b47c32019-06-28 21:55:48 +02002356 " wait a bit, otherwise it fails sometimes (double click recognized?)
2357 sleep 100m
Bram Moolenaar53a95d62019-06-26 03:54:08 +02002358 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
2359 call VerifyScreenDump(buf, 'Test_popupwin_scroll_7', {})
2360
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02002361 call term_sendkeys(buf, ":call ClickTop()\<CR>")
2362 sleep 100m
2363 call term_sendkeys(buf, ":call ClickTop()\<CR>")
2364 call VerifyScreenDump(buf, 'Test_popupwin_scroll_8', {})
2365
2366 call term_sendkeys(buf, ":call ClickBot()\<CR>")
2367 call VerifyScreenDump(buf, 'Test_popupwin_scroll_9', {})
2368
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02002369 " remove the minwidth and maxheight
2370 call term_sendkeys(buf, ":call popup_setoptions(winid, #{maxheight: 0, minwidth: 0})\<CR>")
Bram Moolenaar7e0f4622019-09-17 21:23:39 +02002371 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02002372 call VerifyScreenDump(buf, 'Test_popupwin_scroll_10', {})
2373
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002374 " check size with non-wrapping lines
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002375 call term_sendkeys(buf, ":call g:PopupScroll()\<CR>")
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002376 call VerifyScreenDump(buf, 'Test_popupwin_scroll_11', {})
2377
2378 " check size with wrapping lines
2379 call term_sendkeys(buf, "j")
2380 call VerifyScreenDump(buf, 'Test_popupwin_scroll_12', {})
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002381
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002382 " clean up
Bram Moolenaar20298ce2020-06-19 21:46:52 +02002383 call term_sendkeys(buf, "x")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002384 call StopVimInTerminal(buf)
2385 call delete('XtestPopupScroll')
2386endfunc
2387
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02002388func Test_popup_too_high_scrollbar()
2389 CheckScreendump
2390
2391 let lines =<< trim END
2392 call setline(1, range(1, 20)->map({i, v -> repeat(v, 10)}))
2393 set scrolloff=0
2394 func ShowPopup()
2395 let winid = popup_atcursor(['one', 'two', 'three', 'four', 'five',
2396 \ 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve'], #{
2397 \ minwidth: 8,
2398 \ border: [],
2399 \ })
2400 endfunc
2401 normal 3G$
2402 call ShowPopup()
2403 END
2404 call writefile(lines, 'XtestPopupToohigh')
2405 let buf = RunVimInTerminal('-S XtestPopupToohigh', #{rows: 10})
2406 call VerifyScreenDump(buf, 'Test_popupwin_toohigh_1', {})
2407
2408 call term_sendkeys(buf, ":call popup_clear()\<CR>")
2409 call term_sendkeys(buf, "8G$")
2410 call term_sendkeys(buf, ":call ShowPopup()\<CR>")
2411 call VerifyScreenDump(buf, 'Test_popupwin_toohigh_2', {})
2412
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02002413 call term_sendkeys(buf, ":call popup_clear()\<CR>")
2414 call term_sendkeys(buf, "gg$")
2415 call term_sendkeys(buf, ":call ShowPopup()\<CR>")
2416 call VerifyScreenDump(buf, 'Test_popupwin_toohigh_3', {})
2417
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02002418 " clean up
2419 call StopVimInTerminal(buf)
2420 call delete('XtestPopupToohigh')
2421endfunc
2422
Bram Moolenaar437a7462019-07-05 20:17:22 +02002423func Test_popup_fitting_scrollbar()
2424 " this was causing a crash, divide by zero
2425 let winid = popup_create([
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002426 \ 'one', 'two', 'longer line that wraps', 'four', 'five'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002427 \ scrollbar: 1,
2428 \ maxwidth: 10,
2429 \ maxheight: 5,
2430 \ firstline: 2})
Bram Moolenaar437a7462019-07-05 20:17:22 +02002431 redraw
2432 call popup_clear()
2433endfunc
2434
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002435func Test_popup_settext()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002436 CheckScreendump
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002437
2438 let lines =<< trim END
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002439 let opts = #{wrap: 0}
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002440 let p = popup_create('test', opts)
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002441 eval p->popup_settext('this is a text')
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002442 END
2443
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002444 call writefile(lines, 'XtestPopupSetText')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002445 let buf = RunVimInTerminal('-S XtestPopupSetText', #{rows: 10})
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002446 call VerifyScreenDump(buf, 'Test_popup_settext_01', {})
2447
2448 " Setting to empty string clears it
2449 call term_sendkeys(buf, ":call popup_settext(p, '')\<CR>")
2450 call VerifyScreenDump(buf, 'Test_popup_settext_02', {})
2451
2452 " Setting a list
2453 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
2454 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
2455
2456 " Shrinking with a list
2457 call term_sendkeys(buf, ":call popup_settext(p, ['a'])\<CR>")
2458 call VerifyScreenDump(buf, 'Test_popup_settext_04', {})
2459
2460 " Growing with a list
2461 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
2462 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
2463
2464 " Empty list clears
2465 call term_sendkeys(buf, ":call popup_settext(p, [])\<CR>")
2466 call VerifyScreenDump(buf, 'Test_popup_settext_05', {})
2467
2468 " Dicts
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002469 call term_sendkeys(buf, ":call popup_settext(p, [#{text: 'aaaa'}, #{text: 'bbbb'}, #{text: 'cccc'}])\<CR>")
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002470 call VerifyScreenDump(buf, 'Test_popup_settext_06', {})
2471
Bram Moolenaarb0992022020-01-30 14:55:42 +01002472 " range() (doesn't work)
2473 call term_sendkeys(buf, ":call popup_settext(p, range(4, 8))\<CR>")
2474 call VerifyScreenDump(buf, 'Test_popup_settext_07', {})
2475
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002476 " clean up
2477 call StopVimInTerminal(buf)
2478 call delete('XtestPopupSetText')
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002479endfunc
2480
Bram Moolenaar1f42f5a2020-09-03 18:52:24 +02002481func Test_popup_settext_getline()
2482 let id = popup_create('', #{ tabpage: 0 })
2483 call popup_settext(id, ['a','b'])
2484 call assert_equal(2, line('$', id)) " OK :)
2485 call popup_close(id)
2486
2487 let id = popup_create('', #{ tabpage: -1 })
2488 call popup_settext(id, ['a','b'])
2489 call assert_equal(2, line('$', id)) " Fails :(
2490 call popup_close(id)
2491endfunc
2492
Bram Moolenaar74f8eec2020-10-15 19:10:56 +02002493func Test_popup_settext_null()
2494 let id = popup_create('', #{ tabpage: 0 })
2495 call popup_settext(id, test_null_list())
2496 call popup_close(id)
2497
2498 let id = popup_create('', #{ tabpage: 0 })
2499 call popup_settext(id, test_null_string())
2500 call popup_close(id)
2501endfunc
2502
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002503func Test_popup_hidden()
2504 new
2505
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002506 let winid = popup_atcursor('text', #{hidden: 1})
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002507 redraw
2508 call assert_equal(0, popup_getpos(winid).visible)
2509 call popup_close(winid)
2510
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002511 let winid = popup_create('text', #{hidden: 1})
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002512 redraw
2513 call assert_equal(0, popup_getpos(winid).visible)
2514 call popup_close(winid)
2515
2516 func QuitCallback(id, res)
2517 let s:cb_winid = a:id
2518 let s:cb_res = a:res
2519 endfunc
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002520 let winid = 'make a choice'->popup_dialog(#{hidden: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002521 \ filter: 'popup_filter_yesno',
2522 \ callback: 'QuitCallback',
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002523 \ })
2524 redraw
2525 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarae943152019-06-16 22:54:14 +02002526 call assert_equal(function('popup_filter_yesno'), popup_getoptions(winid).filter)
2527 call assert_equal(function('QuitCallback'), popup_getoptions(winid).callback)
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002528 exe "normal anot used by filter\<Esc>"
2529 call assert_equal('not used by filter', getline(1))
2530
2531 call popup_show(winid)
2532 call feedkeys('y', "xt")
2533 call assert_equal(1, s:cb_res)
2534
2535 bwipe!
2536 delfunc QuitCallback
2537endfunc
Bram Moolenaarae943152019-06-16 22:54:14 +02002538
2539" Test options not checked elsewhere
2540func Test_set_get_options()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002541 let winid = popup_create('some text', #{highlight: 'Beautiful'})
Bram Moolenaarae943152019-06-16 22:54:14 +02002542 let options = popup_getoptions(winid)
2543 call assert_equal(1, options.wrap)
2544 call assert_equal(0, options.drag)
2545 call assert_equal('Beautiful', options.highlight)
2546
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002547 call popup_setoptions(winid, #{wrap: 0, drag: 1, highlight: 'Another'})
Bram Moolenaarae943152019-06-16 22:54:14 +02002548 let options = popup_getoptions(winid)
2549 call assert_equal(0, options.wrap)
2550 call assert_equal(1, options.drag)
2551 call assert_equal('Another', options.highlight)
2552
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02002553 call assert_fails('call popup_setoptions(winid, [])', 'E715:')
2554 call assert_fails('call popup_setoptions(winid, test_null_dict())', 'E715:')
2555
Bram Moolenaarae943152019-06-16 22:54:14 +02002556 call popup_close(winid)
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02002557 call assert_equal(0, popup_setoptions(winid, options.wrap))
Bram Moolenaarae943152019-06-16 22:54:14 +02002558endfunc
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002559
2560func Test_popupwin_garbage_collect()
2561 func MyPopupFilter(x, winid, c)
2562 " NOP
2563 endfunc
2564
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002565 let winid = popup_create('something', #{filter: function('MyPopupFilter', [{}])})
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002566 call test_garbagecollect_now()
2567 redraw
Bram Moolenaar1bc353b2019-09-01 14:45:28 +02002568 " Must not crash caused by invalid memory access
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002569 call feedkeys('j', 'xt')
2570 call assert_true(v:true)
2571
2572 call popup_close(winid)
2573 delfunc MyPopupFilter
2574endfunc
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002575
Bram Moolenaar581ba392019-09-03 22:08:33 +02002576func Test_popupwin_filter_mode()
2577 func MyPopupFilter(winid, c)
2578 let s:typed = a:c
2579 if a:c == ':' || a:c == "\r" || a:c == 'v'
2580 " can start cmdline mode, get out, and start/stop Visual mode
2581 return 0
2582 endif
2583 return 1
2584 endfunc
2585
2586 " Normal, Visual and Insert mode
2587 let winid = popup_create('something', #{filter: 'MyPopupFilter', filtermode: 'nvi'})
2588 redraw
2589 call feedkeys('x', 'xt')
2590 call assert_equal('x', s:typed)
2591
2592 call feedkeys(":let g:foo = 'foo'\<CR>", 'xt')
2593 call assert_equal(':', s:typed)
2594 call assert_equal('foo', g:foo)
2595
2596 let @x = 'something'
2597 call feedkeys('v$"xy', 'xt')
2598 call assert_equal('y', s:typed)
2599 call assert_equal('something', @x) " yank command is filtered out
2600 call feedkeys('v', 'xt') " end Visual mode
2601
2602 call popup_close(winid)
2603
2604 " only Normal mode
2605 let winid = popup_create('something', #{filter: 'MyPopupFilter', filtermode: 'n'})
2606 redraw
2607 call feedkeys('x', 'xt')
2608 call assert_equal('x', s:typed)
2609
2610 call feedkeys(":let g:foo = 'foo'\<CR>", 'xt')
2611 call assert_equal(':', s:typed)
2612 call assert_equal('foo', g:foo)
2613
2614 let @x = 'something'
2615 call feedkeys('v$"xy', 'xt')
2616 call assert_equal('v', s:typed)
2617 call assert_notequal('something', @x)
2618
2619 call popup_close(winid)
2620
2621 " default: all modes
2622 let winid = popup_create('something', #{filter: 'MyPopupFilter'})
2623 redraw
2624 call feedkeys('x', 'xt')
2625 call assert_equal('x', s:typed)
2626
2627 let g:foo = 'bar'
2628 call feedkeys(":let g:foo = 'foo'\<CR>", 'xt')
2629 call assert_equal("\r", s:typed)
2630 call assert_equal('bar', g:foo)
2631
2632 let @x = 'something'
2633 call feedkeys('v$"xy', 'xt')
2634 call assert_equal('y', s:typed)
2635 call assert_equal('something', @x) " yank command is filtered out
2636 call feedkeys('v', 'xt') " end Visual mode
2637
2638 call popup_close(winid)
2639 delfunc MyPopupFilter
2640endfunc
2641
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002642func Test_popupwin_filter_mouse()
2643 func MyPopupFilter(winid, c)
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002644 let g:got_mousepos = getmousepos()
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002645 return 0
2646 endfunc
2647
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002648 call setline(1, ['.'->repeat(25)]->repeat(10))
2649 let winid = popup_create(['short', 'long line that will wrap', 'other'], #{
2650 \ line: 2,
2651 \ col: 4,
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002652 \ maxwidth: 12,
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002653 \ padding: [],
2654 \ border: [],
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002655 \ filter: 'MyPopupFilter',
2656 \ })
2657 redraw
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002658 " 123456789012345678901
2659 " 1 .....................
2660 " 2 ...+--------------+..
2661 " 3 ...| |..
2662 " 4 ...| short |..
2663 " 5 ...| long line th |..
2664 " 6 ...| at will wrap |..
2665 " 7 ...| other |..
2666 " 8 ...| |..
2667 " 9 ...+--------------+..
2668 " 10 .....................
2669 let tests = []
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002670
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002671 func AddItemOutsidePopup(tests, row, col)
2672 eval a:tests->add(#{clickrow: a:row, clickcol: a:col, result: #{
2673 \ screenrow: a:row, screencol: a:col,
2674 \ winid: win_getid(), winrow: a:row, wincol: a:col,
2675 \ line: a:row, column: a:col,
2676 \ }})
2677 endfunc
2678 func AddItemInPopupBorder(tests, winid, row, col)
2679 eval a:tests->add(#{clickrow: a:row, clickcol: a:col, result: #{
2680 \ screenrow: a:row, screencol: a:col,
2681 \ winid: a:winid, winrow: a:row - 1, wincol: a:col - 3,
2682 \ line: 0, column: 0,
2683 \ }})
2684 endfunc
2685 func AddItemInPopupText(tests, winid, row, col, textline, textcol)
2686 eval a:tests->add(#{clickrow: a:row, clickcol: a:col, result: #{
2687 \ screenrow: a:row, screencol: a:col,
2688 \ winid: a:winid, winrow: a:row - 1, wincol: a:col - 3,
2689 \ line: a:textline, column: a:textcol,
2690 \ }})
2691 endfunc
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002692
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002693 " above and below popup
2694 for c in range(1, 21)
2695 call AddItemOutsidePopup(tests, 1, c)
2696 call AddItemOutsidePopup(tests, 10, c)
2697 endfor
2698 " left and right of popup
2699 for r in range(1, 10)
2700 call AddItemOutsidePopup(tests, r, 3)
2701 call AddItemOutsidePopup(tests, r, 20)
2702 endfor
2703 " top and bottom in popup
2704 for c in range(4, 19)
2705 call AddItemInPopupBorder(tests, winid, 2, c)
2706 call AddItemInPopupBorder(tests, winid, 3, c)
2707 call AddItemInPopupBorder(tests, winid, 8, c)
2708 call AddItemInPopupBorder(tests, winid, 9, c)
2709 endfor
2710 " left and right margin in popup
2711 for r in range(2, 9)
2712 call AddItemInPopupBorder(tests, winid, r, 4)
2713 call AddItemInPopupBorder(tests, winid, r, 5)
2714 call AddItemInPopupBorder(tests, winid, r, 18)
2715 call AddItemInPopupBorder(tests, winid, r, 19)
2716 endfor
2717 " text "short"
2718 call AddItemInPopupText(tests, winid, 4, 6, 1, 1)
2719 call AddItemInPopupText(tests, winid, 4, 10, 1, 5)
2720 call AddItemInPopupText(tests, winid, 4, 11, 1, 6)
2721 call AddItemInPopupText(tests, winid, 4, 17, 1, 6)
2722 " text "long line th"
2723 call AddItemInPopupText(tests, winid, 5, 6, 2, 1)
2724 call AddItemInPopupText(tests, winid, 5, 10, 2, 5)
2725 call AddItemInPopupText(tests, winid, 5, 17, 2, 12)
2726 " text "at will wrap"
2727 call AddItemInPopupText(tests, winid, 6, 6, 2, 13)
2728 call AddItemInPopupText(tests, winid, 6, 10, 2, 17)
2729 call AddItemInPopupText(tests, winid, 6, 17, 2, 24)
2730 " text "other"
2731 call AddItemInPopupText(tests, winid, 7, 6, 3, 1)
2732 call AddItemInPopupText(tests, winid, 7, 10, 3, 5)
2733 call AddItemInPopupText(tests, winid, 7, 11, 3, 6)
2734 call AddItemInPopupText(tests, winid, 7, 17, 3, 6)
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002735
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002736 for item in tests
2737 call test_setmouse(item.clickrow, item.clickcol)
2738 call feedkeys("\<LeftMouse>", 'xt')
2739 call assert_equal(item.result, g:got_mousepos)
2740 endfor
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002741
2742 call popup_close(winid)
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002743 enew!
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002744 delfunc MyPopupFilter
2745endfunc
2746
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002747func Test_popupwin_with_buffer()
2748 call writefile(['some text', 'in a buffer'], 'XsomeFile')
2749 let buf = bufadd('XsomeFile')
2750 call assert_equal(0, bufloaded(buf))
Bram Moolenaar46451042019-08-24 15:50:46 +02002751
2752 setlocal number
2753 call setbufvar(buf, "&wrapmargin", 13)
2754
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002755 let winid = popup_create(buf, {})
2756 call assert_notequal(0, winid)
2757 let pos = popup_getpos(winid)
2758 call assert_equal(2, pos.height)
2759 call assert_equal(1, bufloaded(buf))
Bram Moolenaar46451042019-08-24 15:50:46 +02002760
2761 " window-local option is set to default, buffer-local is not
2762 call assert_equal(0, getwinvar(winid, '&number'))
2763 call assert_equal(13, getbufvar(buf, '&wrapmargin'))
2764
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002765 call popup_close(winid)
2766 call assert_equal({}, popup_getpos(winid))
2767 call assert_equal(1, bufloaded(buf))
2768 exe 'bwipe! ' .. buf
Bram Moolenaar46451042019-08-24 15:50:46 +02002769 setlocal nonumber
Bram Moolenaar7866b872019-07-01 22:21:01 +02002770
2771 edit test_popupwin.vim
2772 let winid = popup_create(bufnr(''), {})
2773 redraw
2774 call popup_close(winid)
Bram Moolenaar3940ec62019-07-05 21:53:24 +02002775 call delete('XsomeFile')
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002776endfunc
Bram Moolenaare296e312019-07-03 23:20:18 +02002777
Bram Moolenaar188639d2022-04-04 16:57:21 +01002778func Test_popupwin_buffer_with_swapfile()
2779 call writefile(['some text', 'in a buffer'], 'XopenFile')
2780 call writefile([''], '.XopenFile.swp')
2781 let g:ignoreSwapExists = 1
2782
2783 let bufnr = bufadd('XopenFile')
2784 call assert_equal(0, bufloaded(bufnr))
2785 let winid = popup_create(bufnr, {'hidden': 1})
2786 call assert_equal(1, bufloaded(bufnr))
2787 call popup_close(winid)
2788
2789 exe 'buffer ' .. bufnr
2790 call assert_equal(1, &readonly)
2791 bwipe!
2792
2793 call delete('XopenFile')
2794 call delete('.XopenFile.swp')
2795 unlet g:ignoreSwapExists
2796endfunc
2797
Bram Moolenaare0d749a2019-09-25 22:14:48 +02002798func Test_popupwin_terminal_buffer()
Bram Moolenaard2c1fb42019-09-25 23:06:40 +02002799 CheckFeature terminal
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002800 CheckUnix
Bram Moolenaare06a28f2020-05-13 23:24:12 +02002801 " Starting a terminal to run a shell in is considered flaky.
2802 let g:test_is_flaky = 1
Bram Moolenaard2c1fb42019-09-25 23:06:40 +02002803
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002804 let origwin = win_getid()
Bram Moolenaar349f6092020-10-06 20:46:49 +02002805
2806 " open help window to test that :help below fails
2807 help
2808
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002809 let termbuf = term_start(&shell, #{hidden: 1})
Bram Moolenaarf5452692020-11-28 21:56:06 +01002810 let winid = popup_create(termbuf, #{minwidth: 40, minheight: 10, border: []})
Bram Moolenaar0353f562020-12-17 22:27:38 +01002811 " Wait for shell to start
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002812 call WaitForAssert({-> assert_equal("run", job_status(term_getjob(termbuf)))})
Bram Moolenaar0353f562020-12-17 22:27:38 +01002813 " Wait for a prompt (see border char first, then space after prompt)
2814 call WaitForAssert({ -> assert_equal(' ', screenstring(screenrow(), screencol() - 1))})
Bram Moolenaarf5452692020-11-28 21:56:06 +01002815
2816 " When typing a character, the cursor is after it.
2817 call feedkeys("x", 'xt')
Bram Moolenaar0353f562020-12-17 22:27:38 +01002818 call term_wait(termbuf)
Bram Moolenaarf5452692020-11-28 21:56:06 +01002819 redraw
2820 call WaitForAssert({ -> assert_equal('x', screenstring(screenrow(), screencol() - 1))})
2821 call feedkeys("\<BS>", 'xt')
2822
Bram Moolenaar631ebc42020-02-03 22:15:26 +01002823 " Check this doesn't crash
2824 call assert_equal(winnr(), winnr('j'))
2825 call assert_equal(winnr(), winnr('k'))
2826 call assert_equal(winnr(), winnr('h'))
2827 call assert_equal(winnr(), winnr('l'))
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02002828
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002829 " Cannot quit while job is running
2830 call assert_fails('call feedkeys("\<C-W>:quit\<CR>", "xt")', 'E948:')
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02002831
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002832 " Cannot enter Terminal-Normal mode. (TODO: but it works...)
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02002833 call feedkeys("xxx\<C-W>N", 'xt')
2834 call assert_fails('call feedkeys("gf", "xt")', 'E863:')
2835 call feedkeys("a\<C-U>", 'xt')
2836
Bram Moolenaar3f65c662020-05-27 23:15:16 +02002837 " Cannot escape from terminal window
2838 call assert_fails('tab drop xxx', 'E863:')
Bram Moolenaar349f6092020-10-06 20:46:49 +02002839 call assert_fails('help', 'E994:')
Bram Moolenaar3f65c662020-05-27 23:15:16 +02002840
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002841 " Cannot open a second one.
2842 let termbuf2 = term_start(&shell, #{hidden: 1})
2843 call assert_fails('call popup_create(termbuf2, #{})', 'E861:')
2844 call term_sendkeys(termbuf2, "exit\<CR>")
2845
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02002846 " Exiting shell closes popup window
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002847 call feedkeys("exit\<CR>", 'xt')
2848 " Wait for shell to exit
Bram Moolenaarb2b218d2020-06-22 20:22:19 +02002849 call WaitForAssert({-> assert_equal("dead", job_status(term_getjob(termbuf)))})
Bram Moolenaar373c6512020-06-22 19:24:23 +02002850
Bram Moolenaar349f6092020-10-06 20:46:49 +02002851 helpclose
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002852 call feedkeys(":quit\<CR>", 'xt')
2853 call assert_equal(origwin, win_getid())
Bram Moolenaare0d749a2019-09-25 22:14:48 +02002854endfunc
2855
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002856func Test_popupwin_close_prevwin()
2857 CheckFeature terminal
Bram Moolenaar62f93f42020-09-02 22:33:24 +02002858 call Popupwin_close_prevwin()
2859endfunc
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002860
Bram Moolenaar62f93f42020-09-02 22:33:24 +02002861def Popupwin_close_prevwin()
2862 assert_equal(1, winnr('$'))
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002863 split
2864 wincmd b
Bram Moolenaar62f93f42020-09-02 22:33:24 +02002865 assert_equal(2, winnr())
Bram Moolenaare0de1712020-12-02 17:36:54 +01002866 var buf = term_start(&shell, {hidden: 1})
Bram Moolenaar62f93f42020-09-02 22:33:24 +02002867 popup_create(buf, {})
Bram Moolenaar62aec932022-01-29 21:45:34 +00002868 g:TermWait(buf, 100)
Bram Moolenaar62f93f42020-09-02 22:33:24 +02002869 popup_clear(true)
2870 assert_equal(2, winnr())
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002871
2872 quit
2873 exe 'bwipe! ' .. buf
Bram Moolenaar62f93f42020-09-02 22:33:24 +02002874enddef
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002875
Bram Moolenaar934470e2019-09-01 23:27:05 +02002876func Test_popupwin_with_buffer_and_filter()
2877 new Xwithfilter
2878 call setline(1, range(100))
2879 let bufnr = bufnr()
2880 hide
2881
2882 func BufferFilter(win, key)
2883 if a:key == 'G'
2884 " recursive use of "G" does not cause problems.
2885 call win_execute(a:win, 'normal! G')
2886 return 1
2887 endif
2888 return 0
2889 endfunc
2890
2891 let winid = popup_create(bufnr, #{maxheight: 5, filter: 'BufferFilter'})
2892 call assert_equal(1, popup_getpos(winid).firstline)
2893 redraw
2894 call feedkeys("G", 'xt')
2895 call assert_equal(99, popup_getpos(winid).firstline)
2896
2897 call popup_close(winid)
2898 exe 'bwipe! ' .. bufnr
2899endfunc
2900
Bram Moolenaare296e312019-07-03 23:20:18 +02002901func Test_popupwin_width()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002902 let winid = popup_create(repeat(['short', 'long long long line', 'medium width'], 50), #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002903 \ maxwidth: 40,
2904 \ maxheight: 10,
Bram Moolenaare296e312019-07-03 23:20:18 +02002905 \ })
2906 for top in range(1, 20)
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002907 eval winid->popup_setoptions(#{firstline: top})
Bram Moolenaare296e312019-07-03 23:20:18 +02002908 redraw
2909 call assert_equal(19, popup_getpos(winid).width)
2910 endfor
2911 call popup_clear()
2912endfunc
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02002913
2914func Test_popupwin_buf_close()
2915 let buf = bufadd('Xtestbuf')
2916 call bufload(buf)
2917 call setbufline(buf, 1, ['just', 'some', 'lines'])
2918 let winid = popup_create(buf, {})
2919 redraw
2920 call assert_equal(3, popup_getpos(winid).height)
2921 let bufinfo = getbufinfo(buf)[0]
2922 call assert_equal(1, bufinfo.changed)
2923 call assert_equal(0, bufinfo.hidden)
2924 call assert_equal(0, bufinfo.listed)
2925 call assert_equal(1, bufinfo.loaded)
2926 call assert_equal([], bufinfo.windows)
2927 call assert_equal([winid], bufinfo.popups)
2928
2929 call popup_close(winid)
2930 call assert_equal({}, popup_getpos(winid))
2931 let bufinfo = getbufinfo(buf)[0]
2932 call assert_equal(1, bufinfo.changed)
2933 call assert_equal(1, bufinfo.hidden)
2934 call assert_equal(0, bufinfo.listed)
2935 call assert_equal(1, bufinfo.loaded)
2936 call assert_equal([], bufinfo.windows)
2937 call assert_equal([], bufinfo.popups)
2938 exe 'bwipe! ' .. buf
2939endfunc
Bram Moolenaar017c2692019-07-13 14:17:51 +02002940
2941func Test_popup_menu_with_maxwidth()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002942 CheckScreendump
Bram Moolenaar017c2692019-07-13 14:17:51 +02002943
2944 let lines =<< trim END
2945 call setline(1, range(1, 10))
2946 hi ScrollThumb ctermbg=blue
2947 hi ScrollBar ctermbg=red
2948 func PopupMenu(lines, line, col, scrollbar = 0)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002949 return popup_menu(a:lines, #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002950 \ maxwidth: 10,
2951 \ maxheight: 3,
2952 \ pos : 'topleft',
2953 \ col : a:col,
2954 \ line : a:line,
2955 \ scrollbar : a:scrollbar,
Bram Moolenaar017c2692019-07-13 14:17:51 +02002956 \ })
2957 endfunc
2958 call PopupMenu(['x'], 1, 1)
2959 call PopupMenu(['123456789|'], 1, 16)
2960 call PopupMenu(['123456789|' .. ' '], 7, 1)
2961 call PopupMenu([repeat('123456789|', 100)], 7, 16)
2962 call PopupMenu(repeat(['123456789|' .. ' '], 5), 1, 33, 1)
2963 END
2964 call writefile(lines, 'XtestPopupMenuMaxWidth')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002965 let buf = RunVimInTerminal('-S XtestPopupMenuMaxWidth', #{rows: 13})
Bram Moolenaar017c2692019-07-13 14:17:51 +02002966 call VerifyScreenDump(buf, 'Test_popupwin_menu_maxwidth_1', {})
2967
2968 " close the menu popupwin.
2969 call term_sendkeys(buf, " ")
2970 call term_sendkeys(buf, " ")
2971 call term_sendkeys(buf, " ")
2972 call term_sendkeys(buf, " ")
2973 call term_sendkeys(buf, " ")
2974
2975 " clean up
2976 call StopVimInTerminal(buf)
2977 call delete('XtestPopupMenuMaxWidth')
2978endfunc
2979
Bram Moolenaara901a372019-07-13 16:38:50 +02002980func Test_popup_menu_with_scrollbar()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002981 CheckScreendump
Bram Moolenaara901a372019-07-13 16:38:50 +02002982
2983 let lines =<< trim END
2984 call setline(1, range(1, 20))
2985 hi ScrollThumb ctermbg=blue
2986 hi ScrollBar ctermbg=red
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002987 eval ['one', 'two', 'three', 'four', 'five',
2988 \ 'six', 'seven', 'eight', 'nine']
2989 \ ->popup_menu(#{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002990 \ minwidth: 8,
2991 \ maxheight: 3,
Bram Moolenaara901a372019-07-13 16:38:50 +02002992 \ })
2993 END
2994 call writefile(lines, 'XtestPopupMenuScroll')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002995 let buf = RunVimInTerminal('-S XtestPopupMenuScroll', #{rows: 10})
Bram Moolenaara901a372019-07-13 16:38:50 +02002996
2997 call term_sendkeys(buf, "j")
2998 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_1', {})
2999
3000 call term_sendkeys(buf, "jjj")
3001 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_2', {})
3002
3003 " if the cursor is the bottom line, it stays at the bottom line.
3004 call term_sendkeys(buf, repeat("j", 20))
3005 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_3', {})
3006
3007 call term_sendkeys(buf, "kk")
3008 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_4', {})
3009
3010 call term_sendkeys(buf, "k")
3011 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_5', {})
3012
3013 " if the cursor is in the top line, it stays in the top line.
3014 call term_sendkeys(buf, repeat("k", 20))
3015 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_6', {})
3016
3017 " close the menu popupwin.
3018 call term_sendkeys(buf, " ")
3019
3020 " clean up
3021 call StopVimInTerminal(buf)
3022 call delete('XtestPopupMenuScroll')
3023endfunc
3024
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003025func Test_popup_menu_filter()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02003026 CheckScreendump
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003027
3028 let lines =<< trim END
3029 function! MyFilter(winid, key) abort
3030 if a:key == "0"
3031 call win_execute(a:winid, "call setpos('.', [0, 1, 1, 0])")
3032 return 1
3033 endif
3034 if a:key == "G"
3035 call win_execute(a:winid, "call setpos('.', [0, line('$'), 1, 0])")
3036 return 1
3037 endif
3038 if a:key == "j"
3039 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0])")
3040 return 1
3041 endif
3042 if a:key == "k"
3043 call win_execute(a:winid, "call setpos('.', [0, line('.') - 1, 1, 0])")
3044 return 1
3045 endif
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02003046 if a:key == ':'
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003047 call popup_close(a:winid)
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02003048 return 0
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003049 endif
3050 return 0
3051 endfunction
3052 call popup_menu(['111', '222', '333', '444', '555', '666', '777', '888', '999'], #{
3053 \ maxheight : 3,
3054 \ filter : 'MyFilter'
3055 \ })
3056 END
3057 call writefile(lines, 'XtestPopupMenuFilter')
3058 let buf = RunVimInTerminal('-S XtestPopupMenuFilter', #{rows: 10})
3059
3060 call term_sendkeys(buf, "j")
3061 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_1', {})
3062
3063 call term_sendkeys(buf, "k")
3064 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_2', {})
3065
3066 call term_sendkeys(buf, "G")
3067 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_3', {})
3068
3069 call term_sendkeys(buf, "0")
3070 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_4', {})
3071
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02003072 " check that when the popup is closed in the filter the screen is redrawn
3073 call term_sendkeys(buf, ":")
3074 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_5', {})
3075 call term_sendkeys(buf, "\<CR>")
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003076
3077 " clean up
3078 call StopVimInTerminal(buf)
3079 call delete('XtestPopupMenuFilter')
3080endfunc
3081
3082func Test_popup_cursorline()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02003083 CheckScreendump
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003084
3085 let winid = popup_create('some text', {})
3086 call assert_equal(0, popup_getoptions(winid).cursorline)
3087 call popup_close(winid)
3088
3089 let winid = popup_create('some text', #{ cursorline: 1, })
3090 call assert_equal(1, popup_getoptions(winid).cursorline)
3091 call popup_close(winid)
3092
Bram Moolenaar6bfc4752021-02-21 23:12:18 +01003093 let winid = popup_create('some text', #{ cursorline: v:true, })
3094 call assert_equal(1, popup_getoptions(winid).cursorline)
3095 call popup_close(winid)
3096
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003097 let winid = popup_create('some text', #{ cursorline: 0, })
3098 call assert_equal(0, popup_getoptions(winid).cursorline)
3099 call popup_close(winid)
3100
3101 let winid = popup_menu('some text', {})
3102 call assert_equal(1, popup_getoptions(winid).cursorline)
3103 call popup_close(winid)
3104
3105 let winid = popup_menu('some text', #{ cursorline: 1, })
3106 call assert_equal(1, popup_getoptions(winid).cursorline)
3107 call popup_close(winid)
3108
3109 let winid = popup_menu('some text', #{ cursorline: 0, })
3110 call assert_equal(0, popup_getoptions(winid).cursorline)
3111 call popup_close(winid)
3112
3113 " ---------
3114 " Pattern 1
3115 " ---------
3116 let lines =<< trim END
3117 call popup_create(['111', '222', '333'], #{ cursorline : 0 })
3118 END
3119 call writefile(lines, 'XtestPopupCursorLine')
3120 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
3121 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_1', {})
3122 call term_sendkeys(buf, ":call popup_clear()\<cr>")
3123 call StopVimInTerminal(buf)
3124
3125 " ---------
3126 " Pattern 2
3127 " ---------
3128 let lines =<< trim END
3129 call popup_create(['111', '222', '333'], #{ cursorline : 1 })
3130 END
3131 call writefile(lines, 'XtestPopupCursorLine')
3132 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
3133 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_2', {})
3134 call term_sendkeys(buf, ":call popup_clear()\<cr>")
3135 call StopVimInTerminal(buf)
3136
3137 " ---------
3138 " Pattern 3
3139 " ---------
3140 let lines =<< trim END
3141 function! MyFilter(winid, key) abort
3142 if a:key == "j"
3143 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0]) | redraw")
3144 return 1
3145 endif
3146 if a:key == 'x'
3147 call popup_close(a:winid)
3148 return 1
3149 endif
3150 return 0
3151 endfunction
3152 call popup_menu(['111', '222', '333'], #{
3153 \ cursorline : 0,
3154 \ maxheight : 2,
3155 \ filter : 'MyFilter',
3156 \ })
3157 END
3158 call writefile(lines, 'XtestPopupCursorLine')
3159 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
3160 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_3', {})
3161 call term_sendkeys(buf, "j")
3162 call term_sendkeys(buf, "j")
3163 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_4', {})
3164 call term_sendkeys(buf, "x")
3165 call StopVimInTerminal(buf)
3166
3167 " ---------
3168 " Pattern 4
3169 " ---------
3170 let lines =<< trim END
3171 function! MyFilter(winid, key) abort
3172 if a:key == "j"
3173 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0]) | redraw")
3174 return 1
3175 endif
3176 if a:key == 'x'
3177 call popup_close(a:winid)
3178 return 1
3179 endif
3180 return 0
3181 endfunction
3182 call popup_menu(['111', '222', '333'], #{
3183 \ cursorline : 1,
3184 \ maxheight : 2,
3185 \ filter : 'MyFilter',
3186 \ })
3187 END
3188 call writefile(lines, 'XtestPopupCursorLine')
3189 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
3190 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_5', {})
3191 call term_sendkeys(buf, "j")
3192 call term_sendkeys(buf, "j")
3193 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_6', {})
3194 call term_sendkeys(buf, "x")
3195 call StopVimInTerminal(buf)
3196
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02003197 " ---------
3198 " Cursor in second line when creating the popup
3199 " ---------
3200 let lines =<< trim END
3201 let winid = popup_create(['111', '222', '333'], #{
3202 \ cursorline : 1,
3203 \ })
3204 call win_execute(winid, "2")
3205 END
3206 call writefile(lines, 'XtestPopupCursorLine')
3207 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
3208 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_7', {})
3209 call StopVimInTerminal(buf)
3210
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003211 call delete('XtestPopupCursorLine')
Bram Moolenaar4eb7dae2019-11-12 22:33:45 +01003212
3213 " ---------
3214 " Use current buffer for popupmenu
3215 " ---------
3216 let lines =<< trim END
3217 call setline(1, ['one', 'two', 'three'])
3218 let winid = popup_create(bufnr('%'), #{
3219 \ cursorline : 1,
3220 \ })
3221 call win_execute(winid, "2")
3222 END
3223 call writefile(lines, 'XtestPopupCursorLine')
3224 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
3225 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_8', {})
3226 call StopVimInTerminal(buf)
3227
3228 call delete('XtestPopupCursorLine')
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003229endfunc
3230
Bram Moolenaar6bfc4752021-02-21 23:12:18 +01003231def Test_popup_cursorline_vim9()
3232 var winid = popup_create('some text', { cursorline: true, })
3233 assert_equal(1, popup_getoptions(winid).cursorline)
3234 popup_close(winid)
3235
3236 assert_fails("popup_create('some text', { cursorline: 2, })", 'E1023:')
3237 popup_clear()
3238enddef
3239
Bram Moolenaarf914a332019-07-20 15:09:56 +02003240func Test_previewpopup()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02003241 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003242 CheckFeature quickfix
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02003243
Bram Moolenaarf914a332019-07-20 15:09:56 +02003244 call writefile([
3245 \ "!_TAG_FILE_ENCODING\tutf-8\t//",
3246 \ "another\tXtagfile\t/^this is another",
3247 \ "theword\tXtagfile\t/^theword"],
3248 \ 'Xtags')
3249 call writefile(range(1,20)
3250 \ + ['theword is here']
3251 \ + range(22, 27)
3252 \ + ['this is another place']
3253 \ + range(29, 40),
3254 \ "Xtagfile")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02003255 call writefile(range(1,10)
3256 \ + ['searched word is here']
3257 \ + range(12, 20),
3258 \ "Xheader.h")
Bram Moolenaarf914a332019-07-20 15:09:56 +02003259 let lines =<< trim END
3260 set tags=Xtags
3261 call setline(1, [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02003262 \ 'one',
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02003263 \ '#include "Xheader.h"',
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02003264 \ 'three',
3265 \ 'four',
3266 \ 'five',
3267 \ 'six',
3268 \ 'seven',
3269 \ 'find theword somewhere',
3270 \ 'nine',
3271 \ 'this is another word',
3272 \ 'very long line where the word is also another'])
Bram Moolenaarf914a332019-07-20 15:09:56 +02003273 set previewpopup=height:4,width:40
Bram Moolenaar60577482021-03-04 21:35:07 +01003274 hi OtherColor ctermbg=lightcyan guibg=lightcyan
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02003275 set path=.
Bram Moolenaarf914a332019-07-20 15:09:56 +02003276 END
3277 call writefile(lines, 'XtestPreviewPopup')
3278 let buf = RunVimInTerminal('-S XtestPreviewPopup', #{rows: 14})
3279
3280 call term_sendkeys(buf, "/theword\<CR>\<C-W>}")
3281 call term_sendkeys(buf, ":\<CR>")
3282 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_1', {})
3283
Bram Moolenaar60577482021-03-04 21:35:07 +01003284 call term_sendkeys(buf, ":set previewpopup+=highlight:OtherColor\<CR>")
Bram Moolenaarf914a332019-07-20 15:09:56 +02003285 call term_sendkeys(buf, "/another\<CR>\<C-W>}")
3286 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_2', {})
3287
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003288 call term_sendkeys(buf, ":call popup_move(popup_findpreview(), #{col: 15})\<CR>")
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02003289 call term_sendkeys(buf, ":\<CR>")
3290 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_3', {})
3291
3292 call term_sendkeys(buf, "/another\<CR>\<C-W>}")
3293 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_4', {})
3294
Bram Moolenaar799439a2020-02-11 21:44:17 +01003295 call term_sendkeys(buf, ":silent cd ..\<CR>:\<CR>")
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02003296 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_5', {})
Bram Moolenaar799439a2020-02-11 21:44:17 +01003297 call term_sendkeys(buf, ":silent cd testdir\<CR>")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02003298
Bram Moolenaar60577482021-03-04 21:35:07 +01003299 call term_sendkeys(buf, ":set previewpopup-=highlight:OtherColor\<CR>")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02003300 call term_sendkeys(buf, ":pclose\<CR>")
Bram Moolenaar78d629a2019-08-16 17:31:15 +02003301 call term_sendkeys(buf, ":\<BS>")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02003302 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_6', {})
3303
3304 call term_sendkeys(buf, ":pedit +/theword Xtagfile\<CR>")
3305 call term_sendkeys(buf, ":\<CR>")
3306 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_7', {})
3307
3308 call term_sendkeys(buf, ":pclose\<CR>")
3309 call term_sendkeys(buf, ":psearch searched\<CR>")
3310 call term_sendkeys(buf, ":\<CR>")
3311 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_8', {})
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02003312
Bram Moolenaar8bf716c2020-01-23 15:33:54 +01003313 call term_sendkeys(buf, "\<C-W>p")
3314 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_9', {})
3315
3316 call term_sendkeys(buf, ":call win_execute(popup_findpreview(), 'call popup_clear()')\<CR>")
3317 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_10', {})
3318
Bram Moolenaarf914a332019-07-20 15:09:56 +02003319 call StopVimInTerminal(buf)
3320 call delete('Xtags')
3321 call delete('Xtagfile')
3322 call delete('XtestPreviewPopup')
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02003323 call delete('Xheader.h')
Bram Moolenaarf914a332019-07-20 15:09:56 +02003324endfunc
3325
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003326func Get_popupmenu_lines()
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003327 let lines =<< trim END
3328 set completeopt+=preview,popup
3329 set completefunc=CompleteFuncDict
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02003330 hi InfoPopup ctermbg=yellow
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003331
3332 func CompleteFuncDict(findstart, base)
3333 if a:findstart
3334 if col('.') > 10
3335 return col('.') - 10
3336 endif
3337 return 0
3338 endif
3339
3340 return {
3341 \ 'words': [
3342 \ {
3343 \ 'word': 'aword',
3344 \ 'abbr': 'wrd',
3345 \ 'menu': 'extra text',
3346 \ 'info': 'words are cool',
3347 \ 'kind': 'W',
3348 \ 'user_data': 'test'
3349 \ },
3350 \ {
3351 \ 'word': 'anotherword',
3352 \ 'abbr': 'anotwrd',
3353 \ 'menu': 'extra text',
3354 \ 'info': "other words are\ncooler than this and some more text\nto make wrap",
3355 \ 'kind': 'W',
3356 \ 'user_data': 'notest'
3357 \ },
3358 \ {
3359 \ 'word': 'noinfo',
3360 \ 'abbr': 'noawrd',
3361 \ 'menu': 'extra text',
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02003362 \ 'info': "lets\nshow\na\nscrollbar\nhere",
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003363 \ 'kind': 'W',
3364 \ 'user_data': 'notest'
3365 \ },
3366 \ {
3367 \ 'word': 'thatword',
3368 \ 'abbr': 'thatwrd',
3369 \ 'menu': 'extra text',
3370 \ 'info': 'that word is cool',
3371 \ 'kind': 'W',
3372 \ 'user_data': 'notest'
3373 \ },
3374 \ ]
3375 \ }
3376 endfunc
3377 call setline(1, 'text text text text text text text ')
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003378 func ChangeColor()
3379 let id = popup_findinfo()
Bram Moolenaard356fc62020-12-09 18:13:44 +01003380 if buflisted(winbufnr(id))
3381 call setline(1, 'buffer is listed')
3382 endif
Bram Moolenaar6a124e62019-09-04 18:15:19 +02003383 eval id->popup_setoptions(#{highlight: 'InfoPopup'})
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003384 endfunc
Bram Moolenaardca7abe2019-10-20 18:17:57 +02003385
3386 func InfoHidden()
3387 set completepopup=height:4,border:off,align:menu
3388 set completeopt-=popup completeopt+=popuphidden
3389 au CompleteChanged * call HandleChange()
3390 endfunc
3391
3392 let s:counter = 0
3393 func HandleChange()
3394 let s:counter += 1
3395 let selected = complete_info(['selected']).selected
3396 if selected <= 0
3397 " First time: do nothing, info remains hidden
3398 return
3399 endif
3400 if selected == 1
3401 " Second time: show info right away
3402 let id = popup_findinfo()
3403 if id
3404 call popup_settext(id, 'immediate info ' .. s:counter)
3405 call popup_show(id)
3406 endif
3407 else
3408 " Third time: show info after a short delay
3409 call timer_start(100, 'ShowInfo')
3410 endif
3411 endfunc
3412
3413 func ShowInfo(...)
3414 let id = popup_findinfo()
3415 if id
3416 call popup_settext(id, 'async info ' .. s:counter)
3417 call popup_show(id)
3418 endif
3419 endfunc
Bram Moolenaar2dfae042020-11-15 14:09:37 +01003420
Bakudankun65555002021-11-17 20:40:16 +00003421 func OpenOtherPopups()
3422 call popup_create([
3423 \ 'popup below',
3424 \ 'popup below',
3425 \ 'popup below',
3426 \ 'popup below',
3427 \ ], #{
3428 \ line: 'cursor',
3429 \ col: 'cursor+3',
3430 \ highlight: 'ErrorMsg',
3431 \ minwidth: 17,
3432 \ zindex: 50,
3433 \ })
3434 call popup_create([
3435 \ 'popup on top',
3436 \ 'popup on top',
3437 \ 'popup on top',
3438 \ ], #{
3439 \ line: 'cursor+3',
3440 \ col: 'cursor-10',
3441 \ highlight: 'Search',
3442 \ minwidth: 10,
3443 \ zindex: 200,
3444 \ })
3445 endfunc
3446
Bram Moolenaar2dfae042020-11-15 14:09:37 +01003447 " Check that no autocommands are triggered for the info popup
3448 au WinEnter * if win_gettype() == 'popup' | call setline(2, 'WinEnter') | endif
3449 au WinLeave * if win_gettype() == 'popup' | call setline(2, 'WinLeave') | endif
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003450 END
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003451 return lines
3452endfunc
3453
3454func Test_popupmenu_info_border()
3455 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003456 CheckFeature quickfix
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003457
3458 let lines = Get_popupmenu_lines()
3459 call add(lines, 'set completepopup=height:4,highlight:InfoPopup')
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003460 call writefile(lines, 'XtestInfoPopup')
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003461
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003462 let buf = RunVimInTerminal('-S XtestInfoPopup', #{rows: 14})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003463 call TermWait(buf, 25)
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003464
3465 call term_sendkeys(buf, "A\<C-X>\<C-U>")
3466 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_1', {})
3467
3468 call term_sendkeys(buf, "\<C-N>")
3469 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_2', {})
3470
3471 call term_sendkeys(buf, "\<C-N>")
3472 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_3', {})
3473
3474 call term_sendkeys(buf, "\<C-N>\<C-N>")
3475 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_4', {})
3476
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02003477 " info on the left with scrollbar
3478 call term_sendkeys(buf, "test text test text\<C-X>\<C-U>")
3479 call term_sendkeys(buf, "\<C-N>\<C-N>")
3480 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_5', {})
3481
Bram Moolenaar202c3f72019-11-21 12:12:35 +01003482 " Test that the popupmenu's scrollbar and infopopup do not overlap
3483 call term_sendkeys(buf, "\<Esc>")
3484 call term_sendkeys(buf, ":set pumheight=3\<CR>")
3485 call term_sendkeys(buf, "cc\<C-X>\<C-U>")
3486 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_6', {})
3487
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003488 " Hide the info popup, cycle through buffers, make sure it didn't get
Bram Moolenaarca7c0782020-01-14 20:42:48 +01003489 " deleted.
3490 call term_sendkeys(buf, "\<Esc>")
3491 call term_sendkeys(buf, ":set hidden\<CR>")
3492 call term_sendkeys(buf, ":bn\<CR>")
3493 call term_sendkeys(buf, ":bn\<CR>")
3494 call term_sendkeys(buf, "otest text test text\<C-X>\<C-U>")
3495 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_7', {})
3496
Bram Moolenaar447bfba2020-07-18 16:07:16 +02003497 " Test that when the option is changed the popup changes.
3498 call term_sendkeys(buf, "\<Esc>")
3499 call term_sendkeys(buf, ":set completepopup=border:off\<CR>")
3500 call term_sendkeys(buf, "a\<C-X>\<C-U>")
3501 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_8', {})
3502
Bram Moolenaar6d585f42020-07-26 22:20:54 +02003503 call term_sendkeys(buf, " \<Esc>")
3504 call term_sendkeys(buf, ":set completepopup+=width:10\<CR>")
3505 call term_sendkeys(buf, "a\<C-X>\<C-U>")
3506 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_9', {})
3507
Bram Moolenaarca7c0782020-01-14 20:42:48 +01003508 call term_sendkeys(buf, "\<Esc>")
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003509 call StopVimInTerminal(buf)
3510 call delete('XtestInfoPopup')
3511endfunc
3512
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003513func Test_popupmenu_info_noborder()
3514 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003515 CheckFeature quickfix
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003516
3517 let lines = Get_popupmenu_lines()
3518 call add(lines, 'set completepopup=height:4,border:off')
3519 call writefile(lines, 'XtestInfoPopupNb')
3520
3521 let buf = RunVimInTerminal('-S XtestInfoPopupNb', #{rows: 14})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003522 call TermWait(buf, 25)
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003523
3524 call term_sendkeys(buf, "A\<C-X>\<C-U>")
3525 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_nb_1', {})
3526
3527 call StopVimInTerminal(buf)
3528 call delete('XtestInfoPopupNb')
3529endfunc
3530
Bram Moolenaar258cef52019-08-21 17:29:29 +02003531func Test_popupmenu_info_align_menu()
3532 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003533 CheckFeature quickfix
Bram Moolenaar258cef52019-08-21 17:29:29 +02003534
3535 let lines = Get_popupmenu_lines()
3536 call add(lines, 'set completepopup=height:4,border:off,align:menu')
3537 call writefile(lines, 'XtestInfoPopupNb')
3538
3539 let buf = RunVimInTerminal('-S XtestInfoPopupNb', #{rows: 14})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003540 call TermWait(buf, 25)
Bram Moolenaar258cef52019-08-21 17:29:29 +02003541
3542 call term_sendkeys(buf, "A\<C-X>\<C-U>")
3543 call term_sendkeys(buf, "\<C-N>")
3544 call term_sendkeys(buf, "\<C-N>")
3545 call term_sendkeys(buf, "\<C-N>")
3546 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_1', {})
3547
3548 call term_sendkeys(buf, "test text test text test\<C-X>\<C-U>")
3549 call term_sendkeys(buf, "\<C-N>")
3550 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_2', {})
3551
3552 call term_sendkeys(buf, "\<Esc>")
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003553 call term_sendkeys(buf, ":call ChangeColor()\<CR>")
Bram Moolenaar258cef52019-08-21 17:29:29 +02003554 call term_sendkeys(buf, ":call setline(2, ['x']->repeat(10))\<CR>")
3555 call term_sendkeys(buf, "Gotest text test text\<C-X>\<C-U>")
3556 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_3', {})
3557
3558 call StopVimInTerminal(buf)
3559 call delete('XtestInfoPopupNb')
3560endfunc
3561
Bram Moolenaardca7abe2019-10-20 18:17:57 +02003562func Test_popupmenu_info_hidden()
3563 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003564 CheckFeature quickfix
Bram Moolenaardca7abe2019-10-20 18:17:57 +02003565
3566 let lines = Get_popupmenu_lines()
3567 call add(lines, 'call InfoHidden()')
3568 call writefile(lines, 'XtestInfoPopupHidden')
3569
3570 let buf = RunVimInTerminal('-S XtestInfoPopupHidden', #{rows: 14})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003571 call TermWait(buf, 25)
Bram Moolenaardca7abe2019-10-20 18:17:57 +02003572
3573 call term_sendkeys(buf, "A\<C-X>\<C-U>")
3574 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_hidden_1', {})
3575
3576 call term_sendkeys(buf, "\<C-N>")
3577 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_hidden_2', {})
3578
3579 call term_sendkeys(buf, "\<C-N>")
3580 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_hidden_3', {})
3581
3582 call term_sendkeys(buf, "\<Esc>")
3583 call StopVimInTerminal(buf)
3584 call delete('XtestInfoPopupHidden')
3585endfunc
3586
Bram Moolenaarbef93ac2019-12-06 20:17:35 +01003587func Test_popupmenu_info_too_wide()
3588 CheckScreendump
3589 CheckFeature quickfix
3590
3591 let lines =<< trim END
3592 call setline(1, range(10))
3593
3594 set completeopt+=preview,popup
3595 set completepopup=align:menu
3596 set omnifunc=OmniFunc
3597 hi InfoPopup ctermbg=lightgrey
3598
3599 func OmniFunc(findstart, base)
3600 if a:findstart
3601 return 0
3602 endif
3603
3604 let menuText = 'some long text to make sure the menu takes up all of the width of the window'
3605 return #{
3606 \ words: [
3607 \ #{
3608 \ word: 'scrap',
3609 \ menu: menuText,
3610 \ info: "other words are\ncooler than this and some more text\nto make wrap",
3611 \ },
3612 \ #{
3613 \ word: 'scappier',
3614 \ menu: menuText,
3615 \ info: 'words are cool',
3616 \ },
3617 \ #{
3618 \ word: 'scrappier2',
3619 \ menu: menuText,
3620 \ info: 'words are cool',
3621 \ },
3622 \ ]
3623 \ }
3624 endfunc
3625 END
3626
3627 call writefile(lines, 'XtestInfoPopupWide')
3628 let buf = RunVimInTerminal('-S XtestInfoPopupWide', #{rows: 8})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003629 call TermWait(buf, 25)
Bram Moolenaarbef93ac2019-12-06 20:17:35 +01003630
3631 call term_sendkeys(buf, "Ascr\<C-X>\<C-O>")
3632 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_wide_1', {})
3633
3634 call term_sendkeys(buf, "\<Esc>")
3635 call StopVimInTerminal(buf)
3636 call delete('XtestInfoPopupWide')
3637endfunc
3638
Bakudankun65555002021-11-17 20:40:16 +00003639func Test_popupmenu_masking()
3640 " Test that popup windows that are opened while popup menu is open are
3641 " properly displayed.
3642 CheckScreendump
3643 CheckFeature quickfix
3644
3645 let lines = Get_popupmenu_lines()
3646 call add(lines, 'inoremap <C-A> <Cmd>call OpenOtherPopups()<CR>')
3647 call writefile(lines, 'XtestPopupmenuMasking')
3648
3649 let buf = RunVimInTerminal('-S XtestPopupmenuMasking', #{rows: 14})
3650 call TermWait(buf, 25)
3651
3652 call term_sendkeys(buf, "A\<C-X>\<C-U>\<C-A>")
3653 call VerifyScreenDump(buf, 'Test_popupwin_popupmenu_masking_1', {})
3654
3655 call term_sendkeys(buf, "\<Esc>")
3656 call VerifyScreenDump(buf, 'Test_popupwin_popupmenu_masking_2', {})
3657
3658 call StopVimInTerminal(buf)
3659 call delete('XtestPopupmenuMasking')
3660endfunc
3661
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02003662func Test_popupwin_recycle_bnr()
Bram Moolenaare49fbff2019-08-21 22:50:07 +02003663 let bufnr = popup_notification('nothing wrong', {})->winbufnr()
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02003664 call popup_clear()
Bram Moolenaar6a124e62019-09-04 18:15:19 +02003665 let winid = 'nothing wrong'->popup_notification({})
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02003666 call assert_equal(bufnr, winbufnr(winid))
3667 call popup_clear()
3668endfunc
3669
Bram Moolenaar1824f452019-10-02 23:06:46 +02003670func Test_popupwin_getoptions_tablocal()
3671 topleft split
3672 let win1 = popup_create('nothing', #{maxheight: 8})
3673 let win2 = popup_create('something', #{maxheight: 10})
3674 let win3 = popup_create('something', #{maxheight: 15})
3675 call assert_equal(8, popup_getoptions(win1).maxheight)
3676 call assert_equal(10, popup_getoptions(win2).maxheight)
3677 call assert_equal(15, popup_getoptions(win3).maxheight)
3678 call popup_clear()
3679 quit
3680endfunc
3681
Bram Moolenaare8a7dfe2019-10-03 22:35:52 +02003682func Test_popupwin_cancel()
3683 let win1 = popup_create('one', #{line: 5, filter: {... -> 0}})
3684 let win2 = popup_create('two', #{line: 10, filter: {... -> 0}})
3685 let win3 = popup_create('three', #{line: 15, filter: {... -> 0}})
3686 call assert_equal(5, popup_getpos(win1).line)
3687 call assert_equal(10, popup_getpos(win2).line)
3688 call assert_equal(15, popup_getpos(win3).line)
3689 " TODO: this also works without patch 8.1.2110
3690 call feedkeys("\<C-C>", 'xt')
3691 call assert_equal(5, popup_getpos(win1).line)
3692 call assert_equal(10, popup_getpos(win2).line)
3693 call assert_equal({}, popup_getpos(win3))
3694 call feedkeys("\<C-C>", 'xt')
3695 call assert_equal(5, popup_getpos(win1).line)
3696 call assert_equal({}, popup_getpos(win2))
3697 call assert_equal({}, popup_getpos(win3))
3698 call feedkeys("\<C-C>", 'xt')
3699 call assert_equal({}, popup_getpos(win1))
3700 call assert_equal({}, popup_getpos(win2))
3701 call assert_equal({}, popup_getpos(win3))
3702endfunc
3703
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003704func Test_popupwin_filter_redraw()
3705 " Create two popups with a filter that closes the popup when typing "0".
3706 " Both popups should close, even though the redraw also calls
3707 " popup_reset_handled()
3708
3709 func CloseFilter(winid, key)
3710 if a:key == '0'
3711 call popup_close(a:winid)
3712 redraw
3713 endif
3714 return 0 " pass the key
3715 endfunc
3716
3717 let id1 = popup_create('first one', #{
3718 \ line: 1,
3719 \ col: 1,
3720 \ filter: 'CloseFilter',
3721 \ })
3722 let id2 = popup_create('second one', #{
3723 \ line: 9,
3724 \ col: 1,
3725 \ filter: 'CloseFilter',
3726 \ })
3727 call assert_equal(1, popup_getpos(id1).line)
3728 call assert_equal(9, popup_getpos(id2).line)
3729
3730 call feedkeys('0', 'xt')
3731 call assert_equal({}, popup_getpos(id1))
3732 call assert_equal({}, popup_getpos(id2))
3733
3734 call popup_clear()
3735 delfunc CloseFilter
3736endfunc
3737
Bram Moolenaar20ebbea2019-11-30 17:58:27 +01003738func Test_popupwin_double_width()
3739 CheckScreendump
3740
3741 let lines =<< trim END
3742 call setline(1, 'x你好世界你好世你好世界你好')
3743 call setline(2, '你好世界你好世你好世界你好')
3744 call setline(3, 'x你好世界你好世你好世界你好')
3745 call popup_create('你好,世界 - 你好,世界xxxxx', #{line: 1, col: 3, maxwidth: 14})
3746 END
3747 call writefile(lines, 'XtestPopupWide')
3748
3749 let buf = RunVimInTerminal('-S XtestPopupWide', #{rows: 10})
3750 call VerifyScreenDump(buf, 'Test_popupwin_doublewidth_1', {})
3751
3752 call StopVimInTerminal(buf)
3753 call delete('XtestPopupWide')
3754endfunc
3755
3756func Test_popupwin_sign()
3757 CheckScreendump
3758
3759 let lines =<< trim END
3760 call setline(1, range(10))
3761 call sign_define('Current', {
3762 \ 'text': '>>',
3763 \ 'texthl': 'WarningMsg',
3764 \ 'linehl': 'Error',
3765 \ })
3766 call sign_define('Other', {
3767 \ 'text': '#!',
3768 \ 'texthl': 'Error',
3769 \ 'linehl': 'Search',
3770 \ })
3771 let winid = popup_create(['hello', 'bright', 'world'], {
3772 \ 'minwidth': 20,
3773 \ })
3774 call setwinvar(winid, "&signcolumn", "yes")
3775 let winbufnr = winbufnr(winid)
3776
3777 " add sign to current buffer, shows
3778 call sign_place(1, 'Selected', 'Current', bufnr('%'), {'lnum': 1})
3779 " add sign to current buffer, does not show
3780 call sign_place(2, 'PopUpSelected', 'Other', bufnr('%'), {'lnum': 2})
3781
3782 " add sign to popup buffer, shows
3783 call sign_place(3, 'PopUpSelected', 'Other', winbufnr, {'lnum': 1})
3784 " add sign to popup buffer, does not show
3785 call sign_place(4, 'Selected', 'Current', winbufnr, {'lnum': 2})
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02003786
3787 func SetOptions()
3788 call setwinvar(g:winid, '&number', 1)
3789 call setwinvar(g:winid, '&foldcolumn', 2)
3790 call popup_settext(g:winid, 'a longer line to check the width')
3791 endfunc
Bram Moolenaar20ebbea2019-11-30 17:58:27 +01003792 END
3793 call writefile(lines, 'XtestPopupSign')
3794
3795 let buf = RunVimInTerminal('-S XtestPopupSign', #{rows: 10})
3796 call VerifyScreenDump(buf, 'Test_popupwin_sign_1', {})
3797
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02003798 " set more options to check the width is adjusted
3799 call term_sendkeys(buf, ":call SetOptions()\<CR>")
3800 call VerifyScreenDump(buf, 'Test_popupwin_sign_2', {})
3801
Bram Moolenaar20ebbea2019-11-30 17:58:27 +01003802 call StopVimInTerminal(buf)
3803 call delete('XtestPopupSign')
3804endfunc
3805
Bram Moolenaar99ebf222019-12-10 23:44:48 +01003806func Test_popupwin_bufnr()
3807 let popwin = popup_create(['blah'], #{})
3808 let popbuf = winbufnr(popwin)
3809 split asdfasdf
3810 let newbuf = bufnr()
3811 call assert_true(newbuf > popbuf, 'New buffer number is higher')
3812 call assert_equal(newbuf, bufnr('$'))
3813 call popup_clear()
3814 let popwin = popup_create(['blah'], #{})
3815 " reuses previous buffer number
3816 call assert_equal(popbuf, winbufnr(popwin))
3817 call assert_equal(newbuf, bufnr('$'))
3818
3819 call popup_clear()
3820 bwipe!
3821endfunc
3822
Bram Moolenaarec084d32020-02-28 22:44:47 +01003823func Test_popupwin_filter_input_multibyte()
3824 func MyPopupFilter(winid, c)
3825 let g:bytes = range(a:c->strlen())->map({i -> char2nr(a:c[i])})
3826 return 0
3827 endfunc
3828 let winid = popup_create('', #{mapping: 0, filter: 'MyPopupFilter'})
3829
3830 " UTF-8: E3 80 80, including K_SPECIAL(0x80)
3831 call feedkeys("\u3000", 'xt')
3832 call assert_equal([0xe3, 0x80, 0x80], g:bytes)
3833
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01003834 " UTF-8: E3 80 9B, including CSI(0x9B)
3835 call feedkeys("\u301b", 'xt')
3836 call assert_equal([0xe3, 0x80, 0x9b], g:bytes)
Bram Moolenaarec084d32020-02-28 22:44:47 +01003837
Bram Moolenaar20298ce2020-06-19 21:46:52 +02003838 if has('unix')
3839 " with modifyOtherKeys <M-S-a> does not include a modifier sequence
Bram Moolenaarb326edf2020-06-20 15:03:38 +02003840 if has('gui_running')
3841 call feedkeys("\x9b\xfc\x08A", 'Lx!')
3842 else
3843 call feedkeys("\<Esc>[27;4;65~", 'Lx!')
3844 endif
Bram Moolenaar20298ce2020-06-19 21:46:52 +02003845 call assert_equal([0xc3, 0x81], g:bytes)
3846 endif
3847
Bram Moolenaarec084d32020-02-28 22:44:47 +01003848 call popup_clear()
3849 delfunc MyPopupFilter
3850 unlet g:bytes
3851endfunc
3852
Bram Moolenaar6f8f7332020-08-10 21:19:23 +02003853func Test_popupwin_filter_close_ctrl_c()
3854 CheckScreendump
3855
3856 let lines =<< trim END
3857 vsplit
3858 set laststatus=2
3859 set statusline=%!Statusline()
3860
3861 function Statusline() abort
3862 return '%<%f %h%m%r%=%-14.(%l,%c%V%) %P'
3863 endfunction
3864
3865 call popup_create('test test test test...', {'filter': {-> 0}})
3866 END
3867 call writefile(lines, 'XtestPopupCtrlC')
3868
3869 let buf = RunVimInTerminal('-S XtestPopupCtrlC', #{rows: 10})
3870
3871 call term_sendkeys(buf, "\<C-C>")
3872 call VerifyScreenDump(buf, 'Test_popupwin_ctrl_c', {})
3873
3874 call StopVimInTerminal(buf)
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003875 call delete('XtestPopupCtrlC')
3876endfunc
3877
3878func Test_popupwin_filter_close_wrong_name()
3879 CheckScreendump
3880
3881 let lines =<< trim END
3882 call popup_create('one two three...', {'filter': 'NoSuchFunc'})
3883 END
3884 call writefile(lines, 'XtestPopupWrongName')
3885
3886 let buf = RunVimInTerminal('-S XtestPopupWrongName', #{rows: 10})
3887
3888 call term_sendkeys(buf, "j")
3889 call VerifyScreenDump(buf, 'Test_popupwin_wrong_name', {})
3890
3891 call StopVimInTerminal(buf)
3892 call delete('XtestPopupWrongName')
3893endfunc
3894
3895func Test_popupwin_filter_close_three_errors()
3896 CheckScreendump
3897
3898 let lines =<< trim END
3899 set cmdheight=2
3900 call popup_create('one two three...', {'filter': 'filter'})
3901 END
3902 call writefile(lines, 'XtestPopupThreeErrors')
3903
3904 let buf = RunVimInTerminal('-S XtestPopupThreeErrors', #{rows: 10})
3905
3906 call term_sendkeys(buf, "jj")
3907 call VerifyScreenDump(buf, 'Test_popupwin_three_errors_1', {})
3908 call term_sendkeys(buf, "j")
3909 call VerifyScreenDump(buf, 'Test_popupwin_three_errors_2', {})
3910
3911 call StopVimInTerminal(buf)
3912 call delete('XtestPopupThreeErrors')
Bram Moolenaar6f8f7332020-08-10 21:19:23 +02003913endfunc
3914
Bram Moolenaar927495b2020-11-06 17:58:35 +01003915func Test_popupwin_latin1_encoding()
3916 CheckScreendump
3917 CheckUnix
3918
3919 " When 'encoding' is a single-byte encoding a terminal window will mess up
3920 " the display. Check that showing a popup on top of that doesn't crash.
3921 let lines =<< trim END
3922 set encoding=latin1
3923 terminal cat Xmultibyte
3924 call popup_create(['one', 'two', 'three', 'four'], #{line: 1, col: 10})
Bram Moolenaar4c5bdb92020-12-17 17:45:59 +01003925 redraw
Bram Moolenaarb125b532020-12-17 21:56:09 +01003926 " wait for "cat" to finish
3927 while execute('ls!') !~ 'finished'
3928 sleep 10m
3929 endwhile
Bram Moolenaar4c5bdb92020-12-17 17:45:59 +01003930 echo "Done"
Bram Moolenaar927495b2020-11-06 17:58:35 +01003931 END
3932 call writefile(lines, 'XtestPopupLatin')
3933 call writefile([repeat("\u3042 ", 120)], 'Xmultibyte')
3934
3935 let buf = RunVimInTerminal('-S XtestPopupLatin', #{rows: 10})
Bram Moolenaar4c5bdb92020-12-17 17:45:59 +01003936 call WaitForAssert({-> assert_match('Done', term_getline(buf, 10))})
Bram Moolenaar927495b2020-11-06 17:58:35 +01003937
3938 call term_sendkeys(buf, ":q\<CR>")
3939 call StopVimInTerminal(buf)
3940 call delete('XtestPopupLatin')
3941 call delete('Xmultibyte')
3942endfunc
3943
Bram Moolenaarba2920f2020-03-06 21:43:17 +01003944func Test_popupwin_atcursor_far_right()
3945 new
3946
3947 " this was getting stuck
3948 set signcolumn=yes
3949 call setline(1, repeat('=', &columns))
3950 normal! ggg$
Bram Moolenaaref6b9792020-05-13 16:34:15 +02003951 let winid = popup_atcursor(repeat('x', 500), #{moved: 'any', border: []})
Bram Moolenaarba2920f2020-03-06 21:43:17 +01003952
Bram Moolenaar6d585f42020-07-26 22:20:54 +02003953 " 'signcolumn' was getting reset
3954 call setwinvar(winid, '&signcolumn', 'yes')
3955 call popup_setoptions(winid, {'zindex': 1000})
3956 call assert_equal('yes', getwinvar(winid, '&signcolumn'))
3957
Bram Moolenaaref6b9792020-05-13 16:34:15 +02003958 call popup_close(winid)
Bram Moolenaarba2920f2020-03-06 21:43:17 +01003959 bwipe!
3960 set signcolumn&
3961endfunc
3962
Bram Moolenaar0f1563f2020-03-20 21:15:51 +01003963func Test_popupwin_splitmove()
3964 vsplit
3965 let win2 = win_getid()
3966 let popup_winid = popup_dialog('hello', {})
3967 call assert_fails('call win_splitmove(popup_winid, win2)', 'E957:')
3968 call assert_fails('call win_splitmove(win2, popup_winid)', 'E957:')
3969
3970 call popup_clear()
3971 bwipe
3972endfunc
3973
Bram Moolenaarcbcd9cb2020-11-07 16:58:59 +01003974func Test_popupwin_exiting_terminal()
3975 CheckFeature terminal
3976
3977 " Tests that when creating a popup right after closing a terminal window does
3978 " not make the popup the current window.
3979 let winid = win_getid()
3980 try
3981 augroup Test_popupwin_exiting_terminal
3982 autocmd!
3983 autocmd WinEnter * :call popup_create('test', {})
3984 augroup END
3985 let bnr = term_start(&shell, #{term_finish: 'close'})
3986 call term_sendkeys(bnr, "exit\r\n")
3987 call WaitForAssert({-> assert_equal(winid, win_getid())})
3988 finally
3989 call popup_clear(1)
3990 augroup Test_popupwin_exiting_terminal
3991 autocmd!
3992 augroup END
3993 endtry
3994endfunc
Bram Moolenaar0f1563f2020-03-20 21:15:51 +01003995
Bram Moolenaar014f6982021-01-04 13:18:30 +01003996func Test_popup_filter_menu()
3997 let colors = ['red', 'green', 'blue']
3998 call popup_menu(colors, #{callback: {_, result -> assert_equal('green', colors[result - 1])}})
3999 call feedkeys("\<c-n>\<c-n>\<c-p>\<cr>", 'xt')
4000endfunc
4001
Bram Moolenaar65026432021-02-06 14:22:32 +01004002func Test_popup_getoptions_other_tab()
4003 new
4004 call setline(1, 'some text')
4005 call prop_type_add('textprop', {})
4006 call prop_add(1, 1, #{type: 'textprop', length: 1})
4007 let id = popup_create('TEST', #{textprop: 'textprop', highlight: 'ErrorMsg', tabpage: 1})
4008 tab sp
4009 call assert_equal(['textprop', 'textpropid', 'textpropwin'], popup_getoptions(id)->keys()->filter({_, v -> v =~ 'textprop'}))
4010
4011 tabclose
Bram Moolenaarefa19232021-02-06 14:59:27 +01004012 call popup_close(id)
Bram Moolenaar65026432021-02-06 14:22:32 +01004013 bwipe!
4014 call prop_type_delete('textprop')
4015endfunc
4016
Bram Moolenaarefa19232021-02-06 14:59:27 +01004017
4018func Test_popup_setoptions_other_tab()
4019 new Xfile
4020 let winid = win_getid()
4021 call setline(1, 'some text')
4022 call prop_type_add('textprop', {})
4023 call prop_add(1, 1, #{type: 'textprop', length: 1})
4024 let id = popup_create('TEST', #{textprop: 'textprop'})
4025 tab sp
4026 call popup_setoptions(id, #{textprop: 'textprop', textpropwin: winid})
4027 call assert_equal(winid, popup_getoptions(id).textpropwin)
4028
4029 tabclose
4030 call popup_close(id)
4031 bwipe! Xfile
4032 call prop_type_delete('textprop')
4033endfunc
4034
Bram Moolenaar82db31c2021-02-10 14:56:11 +01004035func Test_popup_prop_not_visible()
4036 CheckScreendump
4037
4038 let lines =<< trim END
4039 vim9script
4040 set nowrap stal=2
4041 rightbelow :31vnew
4042 setline(1, ['', 'some text', '', 'other text'])
4043 prop_type_add('someprop', {})
4044 prop_add(2, 9, {type: 'someprop', length: 5})
4045 popup_create('attached to "some"', {
4046 textprop: 'someprop',
4047 highlight: 'ErrorMsg',
4048 line: -1,
4049 wrap: false,
4050 fixed: true,
4051 })
4052 prop_type_add('otherprop', {})
4053 prop_add(4, 10, {type: 'otherprop', length: 5})
4054 popup_create('attached to "other"', {
4055 textprop: 'otherprop',
4056 highlight: 'ErrorMsg',
4057 line: -1,
4058 wrap: false,
4059 fixed: false,
4060 })
4061 END
4062 call writefile(lines, 'XtestPropNotVisble')
4063 let buf = RunVimInTerminal('-S XtestPropNotVisble', #{rows: 10})
4064 call VerifyScreenDump(buf, 'Test_popup_prop_not_visible_01', {})
4065
4066 call term_sendkeys(buf, ":vert resize -14\<CR>")
4067 call VerifyScreenDump(buf, 'Test_popup_prop_not_visible_02', {})
4068
4069 call term_sendkeys(buf, ":vert resize -8\<CR>")
4070 call VerifyScreenDump(buf, 'Test_popup_prop_not_visible_03', {})
4071
4072 " clean up
4073 call StopVimInTerminal(buf)
4074 call delete('XtestPropNotVisble')
4075endfunction
4076
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02004077func Test_bufdel_skips_popupwin_buffer()
4078 let id = popup_create("Some text", {})
4079 %bd
4080 call popup_close(id)
4081endfunc
4082
Bram Moolenaar82db31c2021-02-10 14:56:11 +01004083
Bram Moolenaar331bafd2019-07-20 17:46:05 +02004084" vim: shiftwidth=2 sts=2