blob: d3a57b04b0151d48a30e62178777d3cb8df13f03 [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)
LemonBoy0044e512022-04-20 19:47:37 +0100517
518 " Popup with less elements than the maximum height and negative firstline:
519 " check that the popup height is correctly computed.
520 let winid = popup_create(['xxx']->repeat(4), #{
521 \ firstline: -1,
522 \ maxheight: 6,
523 \ })
524
525 let pos = popup_getpos(winid)
526 call assert_equal(3, pos.width)
527 call assert_equal(4, pos.height)
528
529 call popup_close(winid)
Bram Moolenaar8d241042019-06-12 23:40:01 +0200530endfunc
531
Bram Moolenaar99ca9c42020-09-22 21:55:41 +0200532func Test_popup_firstline_cursorline()
533 let winid = popup_create(['1111', '222222', '33333', '44444'], #{
534 \ maxheight: 2,
535 \ firstline: 3,
536 \ cursorline: 1,
537 \ })
538 call assert_equal(3, popup_getoptions(winid).firstline)
539 call assert_equal(3, getwininfo(winid)[0].topline)
540 call assert_equal(3, getcurpos(winid)[1])
541
542 call popup_close(winid)
543endfunc
544
Bram Moolenaara112f2d2019-09-01 17:38:09 +0200545func Test_popup_noscrolloff()
546 set scrolloff=5
547 let winid = popup_create(['xxx']->repeat(50), #{
548 \ maxheight: 5,
549 \ firstline: 11,
550 \ })
551 redraw
552 call assert_equal(11, popup_getoptions(winid).firstline)
553 call assert_equal(11, popup_getpos(winid).firstline)
554
555 call popup_setoptions(winid, #{firstline: 0})
556 call win_execute(winid, "normal! \<c-y>")
557 call assert_equal(0, popup_getoptions(winid).firstline)
558 call assert_equal(10, popup_getpos(winid).firstline)
559
560 call popup_close(winid)
561endfunc
562
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200563func Test_popup_drag()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200564 CheckScreendump
565
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200566 " create a popup that covers the command line
567 let lines =<< trim END
568 call setline(1, range(1, 20))
Bram Moolenaar356375f2019-08-24 14:46:29 +0200569 split
570 vsplit
571 $wincmd w
572 vsplit
573 1wincmd w
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200574 let winid = popup_create(['1111', '222222', '33333'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200575 \ drag: 1,
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200576 \ resize: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200577 \ border: [],
578 \ line: &lines - 4,
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200579 \ })
580 func Dragit()
Bram Moolenaar0b74d002021-11-29 17:38:02 +0000581 map <silent> <F3> :call test_setmouse(&lines - 4, &columns / 2)<CR>
582 map <silent> <F4> :call test_setmouse(&lines - 8, &columns / 2 - 20)<CR>
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200583 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
584 endfunc
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200585 func Resize()
Bram Moolenaar0b74d002021-11-29 17:38:02 +0000586 map <silent> <F5> :call test_setmouse(6, 21)<CR>
587 map <silent> <F6> :call test_setmouse(7, 25)<CR>
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200588 call feedkeys("\<F5>\<LeftMouse>\<F6>\<LeftDrag>\<LeftRelease>", "xt")
589 endfunc
Bram Moolenaarbfc57862021-11-26 15:57:40 +0000590 func ClickAndDrag()
Bram Moolenaar0b74d002021-11-29 17:38:02 +0000591 map <silent> <F3> :call test_setmouse(5, 2)<CR>
592 map <silent> <F4> :call test_setmouse(3, 14)<CR>
593 map <silent> <F5> :call test_setmouse(3, 18)<CR>
594 call feedkeys("\<F3>\<LeftMouse>\<LeftRelease>", "xt")
595 call feedkeys("\<F4>\<LeftMouse>\<F5>\<LeftDrag>\<LeftRelease>", "xt")
Bram Moolenaarbfc57862021-11-26 15:57:40 +0000596 endfunc
Bram Moolenaar0b74d002021-11-29 17:38:02 +0000597 func DragAllStart()
598 call popup_clear()
599 call popup_create('hello', #{line: 3, col: 5, dragall: 1})
600 endfunc
601 func DragAllDrag()
602 map <silent> <F3> :call test_setmouse(3, 5)<CR>
603 map <silent> <F4> :call test_setmouse(5, 36)<CR>
604 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
605 endfunc
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200606 END
607 call writefile(lines, 'XtestPopupDrag')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200608 let buf = RunVimInTerminal('-S XtestPopupDrag', #{rows: 10})
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200609 call VerifyScreenDump(buf, 'Test_popupwin_drag_01', {})
610
611 call term_sendkeys(buf, ":call Dragit()\<CR>")
612 call VerifyScreenDump(buf, 'Test_popupwin_drag_02', {})
613
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200614 call term_sendkeys(buf, ":call Resize()\<CR>")
615 call VerifyScreenDump(buf, 'Test_popupwin_drag_03', {})
616
Bram Moolenaarbfc57862021-11-26 15:57:40 +0000617 " dragging works after click on a status line
618 call term_sendkeys(buf, ":call ClickAndDrag()\<CR>")
619 call VerifyScreenDump(buf, 'Test_popupwin_drag_04', {})
620
Bram Moolenaar0b74d002021-11-29 17:38:02 +0000621 " dragging without border
622 call term_sendkeys(buf, ":call DragAllStart()\<CR>")
623 call VerifyScreenDump(buf, 'Test_popupwin_drag_05', {})
624 call term_sendkeys(buf, ":call DragAllDrag()\<CR>")
625 call VerifyScreenDump(buf, 'Test_popupwin_drag_06', {})
626
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200627 " clean up
628 call StopVimInTerminal(buf)
629 call delete('XtestPopupDrag')
630endfunc
631
Bram Moolenaareabddc42022-04-02 15:32:16 +0100632func Test_popup_drag_minwidth()
633 CheckScreendump
634
635 " create a popup that does not fit
636 let lines =<< trim END
637 call range(40)
638 \ ->map({_,i -> string(i)})
639 \ ->popup_create({
640 \ 'drag': 1,
641 \ 'wrap': 0,
642 \ 'border': [],
643 \ 'scrollbar': 1,
644 \ 'minwidth': 100,
645 \ 'filter': {w, k -> k ==# 'q' ? len([popup_close(w)]) : 0},
646 \ })
647 func DragitDown()
648 map <silent> <F3> :call test_setmouse(1, 10)<CR>
649 map <silent> <F4> :call test_setmouse(5, 40)<CR>
650 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
651 endfunc
652 func DragitUp()
653 map <silent> <F3> :call test_setmouse(5, 40)<CR>
654 map <silent> <F4> :call test_setmouse(4, 40)<CR>
655 map <silent> <F5> :call test_setmouse(3, 40)<CR>
656 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<F5>\<LeftDrag>\<LeftRelease>", "xt")
657 endfunc
658 END
659 call writefile(lines, 'XtestPopupDrag')
660 let buf = RunVimInTerminal('-S XtestPopupDrag', #{rows: 10})
661 call VerifyScreenDump(buf, 'Test_popupwin_drag_minwidth_1', {})
662
663 call term_sendkeys(buf, ":call DragitDown()\<CR>")
664 call VerifyScreenDump(buf, 'Test_popupwin_drag_minwidth_2', {})
665
666 call term_sendkeys(buf, ":call DragitUp()\<CR>")
667 call VerifyScreenDump(buf, 'Test_popupwin_drag_minwidth_3', {})
668
669 call term_sendkeys(buf, 'q')
670
671 " clean up
672 call StopVimInTerminal(buf)
673 call delete('XtestPopupDrag')
674endfunc
675
Bram Moolenaar35910f22020-07-12 19:24:10 +0200676func Test_popup_drag_termwin()
677 CheckUnix
678 CheckScreendump
679 CheckFeature terminal
680
681 " create a popup that covers the terminal window
682 let lines =<< trim END
Bram Moolenaar452143c2020-07-15 17:38:21 +0200683 set foldmethod=marker
684 call setline(1, range(100))
685 for nr in range(7)
686 call setline(nr * 12 + 1, "fold {{{")
Bram Moolenaar9d489562020-07-30 20:08:50 +0200687 call setline(nr * 12 + 11, "end }}}")
Bram Moolenaar452143c2020-07-15 17:38:21 +0200688 endfor
689 %foldclose
Bram Moolenaar35910f22020-07-12 19:24:10 +0200690 set shell=/bin/sh noruler
Bram Moolenaar4b2ce122020-11-21 21:41:41 +0100691 unlet $PROMPT_COMMAND
Bram Moolenaara4dc6f92020-07-12 19:52:36 +0200692 let $PS1 = 'vim> '
Bram Moolenaar452143c2020-07-15 17:38:21 +0200693 terminal ++rows=4
Bram Moolenaar35910f22020-07-12 19:24:10 +0200694 $wincmd w
695 let winid = popup_create(['1111', '2222'], #{
696 \ drag: 1,
697 \ resize: 1,
698 \ border: [],
699 \ line: 3,
700 \ })
Bram Moolenaar452143c2020-07-15 17:38:21 +0200701 func DragitLeft()
Bram Moolenaar35910f22020-07-12 19:24:10 +0200702 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
703 endfunc
Bram Moolenaar452143c2020-07-15 17:38:21 +0200704 func DragitDown()
705 call feedkeys("\<F4>\<LeftMouse>\<F5>\<LeftDrag>\<LeftRelease>", "xt")
706 endfunc
707 func DragitDownLeft()
708 call feedkeys("\<F5>\<LeftMouse>\<F6>\<LeftDrag>\<LeftRelease>", "xt")
709 endfunc
Bram Moolenaar35910f22020-07-12 19:24:10 +0200710 map <silent> <F3> :call test_setmouse(3, &columns / 2)<CR>
711 map <silent> <F4> :call test_setmouse(3, &columns / 2 - 20)<CR>
Bram Moolenaar452143c2020-07-15 17:38:21 +0200712 map <silent> <F5> :call test_setmouse(12, &columns / 2)<CR>
713 map <silent> <F6> :call test_setmouse(12, &columns / 2 - 20)<CR>
Bram Moolenaar35910f22020-07-12 19:24:10 +0200714 END
715 call writefile(lines, 'XtestPopupTerm')
Bram Moolenaar452143c2020-07-15 17:38:21 +0200716 let buf = RunVimInTerminal('-S XtestPopupTerm', #{rows: 16})
Bram Moolenaar35910f22020-07-12 19:24:10 +0200717 call VerifyScreenDump(buf, 'Test_popupwin_term_01', {})
718
Bram Moolenaar452143c2020-07-15 17:38:21 +0200719 call term_sendkeys(buf, ":call DragitLeft()\<CR>")
Bram Moolenaar35910f22020-07-12 19:24:10 +0200720 call VerifyScreenDump(buf, 'Test_popupwin_term_02', {})
721
Bram Moolenaar452143c2020-07-15 17:38:21 +0200722 call term_sendkeys(buf, ":call DragitDown()\<CR>")
723 call VerifyScreenDump(buf, 'Test_popupwin_term_03', {})
724
725 call term_sendkeys(buf, ":call DragitDownLeft()\<CR>")
726 call VerifyScreenDump(buf, 'Test_popupwin_term_04', {})
727
Bram Moolenaar35910f22020-07-12 19:24:10 +0200728 " clean up
729 call StopVimInTerminal(buf)
730 call delete('XtestPopupTerm')
731endfunc
732
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200733func Test_popup_close_with_mouse()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200734 CheckScreendump
735
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200736 let lines =<< trim END
737 call setline(1, range(1, 20))
738 " With border, can click on X
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200739 let winid = popup_create('foobar', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200740 \ close: 'button',
741 \ border: [],
742 \ line: 1,
743 \ col: 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200744 \ })
745 func CloseMsg(id, result)
746 echomsg 'Popup closed with ' .. a:result
747 endfunc
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200748 let winid = popup_create('notification', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200749 \ close: 'click',
750 \ line: 3,
751 \ col: 15,
752 \ callback: 'CloseMsg',
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200753 \ })
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200754 let winid = popup_create('no border here', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200755 \ close: 'button',
756 \ line: 5,
757 \ col: 3,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200758 \ })
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200759 let winid = popup_create('only padding', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200760 \ close: 'button',
761 \ padding: [],
762 \ line: 5,
763 \ col: 23,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200764 \ })
765 func CloseWithX()
766 call feedkeys("\<F3>\<LeftMouse>\<LeftRelease>", "xt")
767 endfunc
768 map <silent> <F3> :call test_setmouse(1, len('foobar') + 2)<CR>
769 func CloseWithClick()
770 call feedkeys("\<F4>\<LeftMouse>\<LeftRelease>", "xt")
771 endfunc
772 map <silent> <F4> :call test_setmouse(3, 17)<CR>
Bram Moolenaarf6396232019-08-24 19:36:00 +0200773 func CreateWithMenuFilter()
774 let winid = popup_create('barfoo', #{
775 \ close: 'button',
776 \ filter: 'popup_filter_menu',
777 \ border: [],
778 \ line: 1,
779 \ col: 40,
780 \ })
781 endfunc
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200782 END
783 call writefile(lines, 'XtestPopupClose')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200784 let buf = RunVimInTerminal('-S XtestPopupClose', #{rows: 10})
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200785 call VerifyScreenDump(buf, 'Test_popupwin_close_01', {})
786
787 call term_sendkeys(buf, ":call CloseWithX()\<CR>")
788 call VerifyScreenDump(buf, 'Test_popupwin_close_02', {})
789
790 call term_sendkeys(buf, ":call CloseWithClick()\<CR>")
791 call VerifyScreenDump(buf, 'Test_popupwin_close_03', {})
792
Bram Moolenaarf6396232019-08-24 19:36:00 +0200793 call term_sendkeys(buf, ":call CreateWithMenuFilter()\<CR>")
794 call VerifyScreenDump(buf, 'Test_popupwin_close_04', {})
795
796 " We have to send the actual mouse code, feedkeys() would be caught the
797 " filter.
798 call term_sendkeys(buf, "\<Esc>[<0;47;1M")
799 call VerifyScreenDump(buf, 'Test_popupwin_close_05', {})
800
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200801 " clean up
802 call StopVimInTerminal(buf)
803 call delete('XtestPopupClose')
804endfunction
805
Bram Moolenaar7b3d9392019-10-16 22:17:07 +0200806func Test_popup_menu_wrap()
807 CheckScreendump
808
809 let lines =<< trim END
810 call setline(1, range(1, 20))
811 call popup_create([
812 \ 'one',
813 \ 'asdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfas',
814 \ 'three',
815 \ 'four',
816 \ ], #{
817 \ pos: "botleft",
818 \ border: [],
819 \ padding: [0,1,0,1],
820 \ maxheight: 3,
821 \ cursorline: 1,
822 \ filter: 'popup_filter_menu',
823 \ })
824 END
825 call writefile(lines, 'XtestPopupWrap')
826 let buf = RunVimInTerminal('-S XtestPopupWrap', #{rows: 10})
827 call VerifyScreenDump(buf, 'Test_popupwin_wrap_1', {})
828
829 call term_sendkeys(buf, "jj")
830 call VerifyScreenDump(buf, 'Test_popupwin_wrap_2', {})
831
832 " clean up
833 call term_sendkeys(buf, "\<Esc>")
834 call StopVimInTerminal(buf)
835 call delete('XtestPopupWrap')
836endfunction
837
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200838func Test_popup_with_mask()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200839 CheckScreendump
840
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200841 let lines =<< trim END
Bram Moolenaarba45f1f2019-07-03 22:50:41 +0200842 call setline(1, repeat([join(range(1, 42), '')], 13))
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200843 hi PopupColor ctermbg=lightgrey
844 let winid = popup_create([
845 \ 'some text',
846 \ 'another line',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200847 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200848 \ line: 1,
849 \ col: 10,
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100850 \ posinvert: 0,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200851 \ wrap: 0,
852 \ fixed: 1,
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +0200853 \ scrollbar: v:false,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200854 \ zindex: 90,
855 \ padding: [],
856 \ highlight: 'PopupColor',
857 \ mask: [[1,1,1,1], [-5,-1,4,4], [7,9,2,3], [2,4,3,3]]})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200858 call popup_create([
859 \ 'xxxxxxxxx',
860 \ 'yyyyyyyyy',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200861 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200862 \ line: 3,
863 \ col: 18,
864 \ zindex: 20})
Bram Moolenaarba45f1f2019-07-03 22:50:41 +0200865 let winidb = popup_create([
866 \ 'just one line',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200867 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200868 \ line: 7,
869 \ col: 10,
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100870 \ posinvert: 0,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200871 \ wrap: 0,
872 \ fixed: 1,
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +0200873 \ scrollbar: v:false,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200874 \ close: 'button',
875 \ zindex: 90,
876 \ padding: [],
877 \ border: [],
878 \ 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 +0200879 END
880 call writefile(lines, 'XtestPopupMask')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200881 let buf = RunVimInTerminal('-S XtestPopupMask', #{rows: 13})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200882 call VerifyScreenDump(buf, 'Test_popupwin_mask_1', {})
883
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200884 call term_sendkeys(buf, ":call popup_move(winid, #{col: 11, line: 2})\<CR>")
885 call term_sendkeys(buf, ":call popup_move(winidb, #{col: 12})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200886 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200887 call VerifyScreenDump(buf, 'Test_popupwin_mask_2', {})
888
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200889 call term_sendkeys(buf, ":call popup_move(winid, #{col: 65, line: 2})\<CR>")
890 call term_sendkeys(buf, ":call popup_move(winidb, #{col: 63})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200891 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaard529ba52019-07-02 23:13:53 +0200892 call VerifyScreenDump(buf, 'Test_popupwin_mask_3', {})
893
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200894 call term_sendkeys(buf, ":call popup_move(winid, #{pos: 'topright', col: 12, line: 2})\<CR>")
895 call term_sendkeys(buf, ":call popup_move(winidb, #{pos: 'topright', col: 12})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200896 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaard529ba52019-07-02 23:13:53 +0200897 call VerifyScreenDump(buf, 'Test_popupwin_mask_4', {})
898
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200899 call term_sendkeys(buf, ":call popup_move(winid, #{pos: 'topright', col: 12, line: 11})\<CR>")
900 call term_sendkeys(buf, ":call popup_move(winidb, #{pos: 'topleft', col: 42, line: 11})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200901 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaarb4207472019-07-12 16:05:45 +0200902 call VerifyScreenDump(buf, 'Test_popupwin_mask_5', {})
903
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200904 " clean up
905 call StopVimInTerminal(buf)
906 call delete('XtestPopupMask')
Bram Moolenaar4012d262020-12-29 11:57:46 +0100907
908 " this was causing a crash
909 call popup_create('test', #{mask: [[0, 0, 0, 0]]})
910 call popup_clear()
Bram Moolenaar10ccfb22021-02-13 21:31:18 +0100911
912 " this was causing an internal error
913 enew
914 set nowrap
915 call repeat('x', &columns)->setline(1)
916 call prop_type_add('textprop', {})
917 call prop_add(1, 1, #{length: &columns, type: 'textprop'})
918 vsplit
919 let opts = popup_create('', #{textprop: 'textprop'})
920 \ ->popup_getoptions()
921 \ ->extend(#{mask: [[1, 1, 1, 1]]})
922 call popup_create('', opts)
923 redraw
924
925 close!
926 bwipe!
927 call prop_type_delete('textprop')
928 call popup_clear()
929 set wrap&
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200930endfunc
931
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200932func Test_popup_select()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200933 CheckScreendump
934 CheckFeature clipboard_working
935
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200936 " create a popup with some text to be selected
937 let lines =<< trim END
Bram Moolenaar1755ec42019-06-15 13:13:54 +0200938 set clipboard=autoselect
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200939 call setline(1, range(1, 20))
Bram Moolenaar4dd751b2019-08-17 19:10:53 +0200940 let winid = popup_create(['the word', 'some more', 'several words here', 'invisible', '5', '6', '7'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200941 \ drag: 1,
942 \ border: [],
943 \ line: 3,
944 \ col: 10,
Bram Moolenaar4dd751b2019-08-17 19:10:53 +0200945 \ maxheight: 3,
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200946 \ })
947 func Select1()
948 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
949 endfunc
950 map <silent> <F3> :call test_setmouse(4, 15)<CR>
951 map <silent> <F4> :call test_setmouse(6, 23)<CR>
952 END
953 call writefile(lines, 'XtestPopupSelect')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200954 let buf = RunVimInTerminal('-S XtestPopupSelect', #{rows: 10})
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200955 call term_sendkeys(buf, ":call Select1()\<CR>")
956 call VerifyScreenDump(buf, 'Test_popupwin_select_01', {})
957
958 call term_sendkeys(buf, ":call popup_close(winid)\<CR>")
959 call term_sendkeys(buf, "\"*p")
Bram Moolenaar8ccabf62019-07-12 18:12:51 +0200960 " clean the command line, sometimes it still shows a command
961 call term_sendkeys(buf, ":\<esc>")
962
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200963 call VerifyScreenDump(buf, 'Test_popupwin_select_02', {})
964
965 " clean up
966 call StopVimInTerminal(buf)
967 call delete('XtestPopupSelect')
968endfunc
969
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200970func Test_popup_in_tab()
971 " default popup is local to tab, not visible when in other tab
972 let winid = popup_create("text", {})
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200973 let bufnr = winbufnr(winid)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200974 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200975 call assert_equal(0, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200976 tabnew
977 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200978 call assert_equal(1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200979 quit
980 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200981
982 call assert_equal(1, bufexists(bufnr))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200983 call popup_clear()
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200984 " buffer is gone now
985 call assert_equal(0, bufexists(bufnr))
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200986
987 " global popup is visible in any tab
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200988 let winid = popup_create("text", #{tabpage: -1})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200989 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200990 call assert_equal(-1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200991 tabnew
992 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200993 call assert_equal(-1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200994 quit
995 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200996 call popup_clear()
Bram Moolenaara3fce622019-06-20 02:31:49 +0200997
998 " create popup in other tab
999 tabnew
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001000 let winid = popup_create("text", #{tabpage: 1})
Bram Moolenaara3fce622019-06-20 02:31:49 +02001001 call assert_equal(0, popup_getpos(winid).visible)
1002 call assert_equal(1, popup_getoptions(winid).tabpage)
1003 quit
1004 call assert_equal(1, popup_getpos(winid).visible)
1005 call assert_equal(0, popup_getoptions(winid).tabpage)
1006 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001007endfunc
1008
1009func Test_popup_valid_arguments()
Bram Moolenaaref6b9792020-05-13 16:34:15 +02001010 call assert_equal(0, len(popup_list()))
1011
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001012 " Zero value is like the property wasn't there
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001013 let winid = popup_create("text", #{col: 0})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001014 let pos = popup_getpos(winid)
1015 call assert_inrange(&columns / 2 - 1, &columns / 2 + 1, pos.col)
Bram Moolenaaref6b9792020-05-13 16:34:15 +02001016 call assert_equal([winid], popup_list())
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001017 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001018
1019 " using cursor column has minimum value of 1
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001020 let winid = popup_create("text", #{col: 'cursor-100'})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001021 let pos = popup_getpos(winid)
1022 call assert_equal(1, pos.col)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001023 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001024
1025 " center
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001026 let winid = popup_create("text", #{pos: 'center'})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001027 let pos = popup_getpos(winid)
1028 let around = (&columns - pos.width) / 2
1029 call assert_inrange(around - 1, around + 1, pos.col)
1030 let around = (&lines - pos.height) / 2
1031 call assert_inrange(around - 1, around + 1, pos.line)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001032 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001033endfunc
1034
1035func Test_popup_invalid_arguments()
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001036 call assert_fails('call popup_create(666, {})', 'E86:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001037 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001038 call assert_fails('call popup_create("text", "none")', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001039 call popup_clear()
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02001040 call assert_fails('call popup_create(test_null_string(), {})', 'E450:')
1041 call assert_fails('call popup_create(test_null_list(), {})', 'E450:')
1042 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001043
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001044 call assert_fails('call popup_create("text", #{col: "xxx"})', 'E475:')
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", #{col: "cursor8"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001047 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001048 call assert_fails('call popup_create("text", #{col: "cursor+x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001049 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001050 call assert_fails('call popup_create("text", #{col: "cursor+8x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001051 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001052
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001053 call assert_fails('call popup_create("text", #{line: "xxx"})', 'E475:')
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", #{line: "cursor8"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001056 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001057 call assert_fails('call popup_create("text", #{line: "cursor+x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001058 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001059 call assert_fails('call popup_create("text", #{line: "cursor+8x"})', 'E15:')
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", #{pos: "there"})', 'E475:')
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", #{padding: "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", #{border: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001067 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001068 call assert_fails('call popup_create("text", #{borderhighlight: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001069 call popup_clear()
Bram Moolenaar403d0902019-07-17 21:37:32 +02001070 call assert_fails('call popup_create("text", #{borderhighlight: test_null_list()})', 'E714:')
1071 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001072 call assert_fails('call popup_create("text", #{borderchars: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001073 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001074
Bram Moolenaar83bd7a92022-05-29 17:13:24 +01001075 call assert_fails('call popup_create([#{text: "text"}, 666], {})', 'E1284: Argument 1, list item 2: Dictionary required')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001076 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001077 call assert_fails('call popup_create([#{text: "text", props: "none"}], {})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001078 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001079 call assert_fails('call popup_create([#{text: "text", props: ["none"]}], {})', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001080 call popup_clear()
Bram Moolenaarb0992022020-01-30 14:55:42 +01001081 call assert_fails('call popup_create([#{text: "text", props: range(3)}], {})', 'E715:')
1082 call popup_clear()
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +02001083 call assert_fails('call popup_create("text", #{mask: ["asdf"]})', 'E475:')
1084 call popup_clear()
Bram Moolenaarb0992022020-01-30 14:55:42 +01001085 call assert_fails('call popup_create("text", #{mask: range(5)})', 'E475:')
1086 call popup_clear()
1087 call popup_create("text", #{mask: [range(4)]})
1088 call popup_clear()
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +02001089 call assert_fails('call popup_create("text", #{mask: test_null_list()})', 'E475:')
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001090 call assert_fails('call popup_create("text", #{mapping: []})', 'E745:')
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +02001091 call popup_clear()
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02001092 call assert_fails('call popup_create("text", #{tabpage : 4})', 'E997:')
1093 call popup_clear()
Bram Moolenaar83bd7a92022-05-29 17:13:24 +01001094
1095 call assert_fails('call popup_create(range(10), {})', 'E1024:')
1096 call popup_clear()
1097 call assert_fails('call popup_create([1, 2], {})', 'E1284: Argument 1, list item 1: Dictionary required')
1098 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001099endfunc
1100
Bram Moolenaareea16992019-05-31 17:34:48 +02001101func Test_win_execute_closing_curwin()
1102 split
1103 let winid = popup_create('some text', {})
Bram Moolenaare2e40752020-09-04 21:18:46 +02001104 call assert_fails('call win_execute(winid, winnr() .. "close")', 'E994:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001105 call popup_clear()
Bram Moolenaar49540192019-12-11 19:34:54 +01001106
1107 let winid = popup_create('some text', {})
Bram Moolenaare2e40752020-09-04 21:18:46 +02001108 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 +01001109 call popup_clear()
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001110endfunc
1111
1112func Test_win_execute_not_allowed()
1113 let winid = popup_create('some text', {})
1114 call assert_fails('call win_execute(winid, "split")', 'E994:')
1115 call assert_fails('call win_execute(winid, "vsplit")', 'E994:')
1116 call assert_fails('call win_execute(winid, "close")', 'E994:')
1117 call assert_fails('call win_execute(winid, "bdelete")', 'E994:')
Bram Moolenaar2d247842019-06-01 17:06:25 +02001118 call assert_fails('call win_execute(winid, "bwipe!")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001119 call assert_fails('call win_execute(winid, "tabnew")', 'E994:')
1120 call assert_fails('call win_execute(winid, "tabnext")', 'E994:')
1121 call assert_fails('call win_execute(winid, "next")', 'E994:')
1122 call assert_fails('call win_execute(winid, "rewind")', 'E994:')
Bram Moolenaar3a2505c2020-03-09 16:40:41 +01001123 call assert_fails('call win_execute(winid, "pedit filename")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001124 call assert_fails('call win_execute(winid, "buf")', 'E994:')
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01001125 call assert_fails('call win_execute(winid, "bnext")', 'E994:')
1126 call assert_fails('call win_execute(winid, "bprev")', 'E994:')
1127 call assert_fails('call win_execute(winid, "bfirst")', 'E994:')
1128 call assert_fails('call win_execute(winid, "blast")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001129 call assert_fails('call win_execute(winid, "edit")', 'E994:')
1130 call assert_fails('call win_execute(winid, "enew")', 'E994:')
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +02001131 call assert_fails('call win_execute(winid, "help")', 'E994:')
1132 call assert_fails('call win_execute(winid, "1only")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001133 call assert_fails('call win_execute(winid, "wincmd x")', 'E994:')
1134 call assert_fails('call win_execute(winid, "wincmd w")', 'E994:')
1135 call assert_fails('call win_execute(winid, "wincmd t")', 'E994:')
1136 call assert_fails('call win_execute(winid, "wincmd b")', 'E994:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001137 call popup_clear()
Bram Moolenaareea16992019-05-31 17:34:48 +02001138endfunc
1139
Bram Moolenaar402502d2019-05-30 22:07:36 +02001140func Test_popup_with_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: 1})
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_wrap', {})
1152
1153 " clean up
1154 call StopVimInTerminal(buf)
1155 call delete('XtestPopup')
1156endfunc
1157
1158func Test_popup_without_wrap()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001159 CheckScreendump
1160
Bram Moolenaar402502d2019-05-30 22:07:36 +02001161 let lines =<< trim END
1162 call setline(1, range(1, 100))
1163 let winid = popup_create(
1164 \ 'a long line that wont fit',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001165 \ #{line: 3, col: 20, maxwidth: 10, wrap: 0})
Bram Moolenaar402502d2019-05-30 22:07:36 +02001166 END
1167 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001168 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar402502d2019-05-30 22:07:36 +02001169 call VerifyScreenDump(buf, 'Test_popupwin_nowrap', {})
1170
1171 " clean up
1172 call StopVimInTerminal(buf)
1173 call delete('XtestPopup')
1174endfunc
1175
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001176func Test_popup_with_showbreak()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001177 CheckScreendump
1178
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001179 let lines =<< trim END
1180 set showbreak=>>\
1181 call setline(1, range(1, 20))
1182 let winid = popup_dialog(
Bram Moolenaar8ae54372019-09-15 18:11:16 +02001183 \ 'a long line here that wraps',
1184 \ #{filter: 'popup_filter_yesno',
1185 \ maxwidth: 12})
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001186 END
1187 call writefile(lines, 'XtestPopupShowbreak')
1188 let buf = RunVimInTerminal('-S XtestPopupShowbreak', #{rows: 10})
1189 call VerifyScreenDump(buf, 'Test_popupwin_showbreak', {})
1190
1191 " clean up
1192 call term_sendkeys(buf, "y")
1193 call StopVimInTerminal(buf)
1194 call delete('XtestPopupShowbreak')
1195endfunc
1196
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001197func Test_popup_time()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001198 CheckFeature timers
1199
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001200 topleft vnew
1201 call setline(1, 'hello')
1202
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001203 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001204 \ line: 1,
1205 \ col: 1,
1206 \ minwidth: 20,
1207 \ time: 500,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001208 \})
1209 redraw
1210 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1211 call assert_equal('world', line)
1212
Bram Moolenaarb4f06282019-07-12 21:07:54 +02001213 call assert_equal(winid, popup_locate(1, 1))
1214 call assert_equal(winid, popup_locate(1, 20))
1215 call assert_equal(0, popup_locate(1, 21))
1216 call assert_equal(0, popup_locate(2, 1))
1217
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001218 sleep 700m
Bram Moolenaar35d5af62019-05-26 20:44:10 +02001219 redraw
Bram Moolenaar196b4662019-09-06 21:34:30 +02001220 let line = join(map(range(1, 5), '1->screenstring(v:val)'), '')
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001221 call assert_equal('hello', line)
1222
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001223 call popup_create('on the command line', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001224 \ line: &lines,
1225 \ col: 10,
1226 \ minwidth: 20,
1227 \ time: 500,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001228 \})
1229 redraw
1230 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
1231 call assert_match('.*on the command line.*', line)
1232
1233 sleep 700m
1234 redraw
1235 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
1236 call assert_notmatch('.*on the command line.*', line)
1237
1238 bwipe!
1239endfunc
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001240
1241func Test_popup_hide()
1242 topleft vnew
1243 call setline(1, 'hello')
1244
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001245 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001246 \ line: 1,
1247 \ col: 1,
1248 \ minwidth: 20,
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001249 \})
1250 redraw
1251 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1252 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001253 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001254 " buffer is still listed and active
1255 call assert_match(winbufnr(winid) .. 'u a.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001256
1257 call popup_hide(winid)
1258 redraw
1259 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1260 call assert_equal('hello', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001261 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001262 " buffer is still listed but hidden
Bram Moolenaarca7c0782020-01-14 20:42:48 +01001263 call assert_match(winbufnr(winid) .. 'u a.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001264
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001265 eval winid->popup_show()
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001266 redraw
1267 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1268 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001269 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001270
1271
1272 call popup_close(winid)
1273 redraw
1274 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1275 call assert_equal('hello', line)
1276
1277 " error is given for existing non-popup window
1278 call assert_fails('call popup_hide(win_getid())', 'E993:')
1279
1280 " no error non-existing window
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001281 eval 1234234->popup_hide()
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001282 call popup_show(41234234)
1283
1284 bwipe!
1285endfunc
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001286
1287func Test_popup_move()
1288 topleft vnew
1289 call setline(1, 'hello')
1290
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001291 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001292 \ line: 1,
1293 \ col: 1,
1294 \ minwidth: 20,
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001295 \})
1296 redraw
1297 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
1298 call assert_equal('world ', line)
1299
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001300 call popup_move(winid, #{line: 2, col: 2})
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001301 redraw
1302 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
1303 call assert_equal('hello ', line)
1304 let line = join(map(range(1, 6), 'screenstring(2, v:val)'), '')
1305 call assert_equal('~world', line)
1306
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001307 eval winid->popup_move(#{line: 1})
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001308 redraw
1309 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
1310 call assert_equal('hworld', line)
1311
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02001312 call assert_fails('call popup_move(winid, [])', 'E715:')
1313 call assert_fails('call popup_move(winid, test_null_dict())', 'E715:')
1314
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001315 call popup_close(winid)
1316
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02001317 call assert_equal(0, popup_move(-1, {}))
1318
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001319 bwipe!
1320endfunc
Bram Moolenaarbc133542019-05-29 20:26:46 +02001321
Bram Moolenaar402502d2019-05-30 22:07:36 +02001322func Test_popup_getpos()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001323 let winid = popup_create('hello', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001324 \ line: 2,
1325 \ col: 3,
1326 \ minwidth: 10,
1327 \ minheight: 11,
Bram Moolenaarbc133542019-05-29 20:26:46 +02001328 \})
1329 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +02001330 let res = popup_getpos(winid)
Bram Moolenaarbc133542019-05-29 20:26:46 +02001331 call assert_equal(2, res.line)
1332 call assert_equal(3, res.col)
1333 call assert_equal(10, res.width)
1334 call assert_equal(11, res.height)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001335 call assert_equal(1, res.visible)
Bram Moolenaarbc133542019-05-29 20:26:46 +02001336
1337 call popup_close(winid)
1338endfunc
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001339
1340func Test_popup_width_longest()
1341 let tests = [
1342 \ [['hello', 'this', 'window', 'displays', 'all of its text'], 15],
1343 \ [['hello', 'this', 'window', 'all of its text', 'displays'], 15],
1344 \ [['hello', 'this', 'all of its text', 'window', 'displays'], 15],
1345 \ [['hello', 'all of its text', 'this', 'window', 'displays'], 15],
1346 \ [['all of its text', 'hello', 'this', 'window', 'displays'], 15],
1347 \ ]
1348
1349 for test in tests
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001350 let winid = popup_create(test[0], #{line: 2, col: 3})
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001351 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +02001352 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001353 call assert_equal(test[1], position.width)
1354 call popup_close(winid)
1355 endfor
1356endfunc
1357
1358func Test_popup_wraps()
1359 let tests = [
1360 \ ['nowrap', 6, 1],
1361 \ ['a line that wraps once', 12, 2],
1362 \ ['a line that wraps two times', 12, 3],
1363 \ ]
1364 for test in tests
1365 let winid = popup_create(test[0],
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001366 \ #{line: 2, col: 3, maxwidth: 12})
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001367 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +02001368 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001369 call assert_equal(test[1], position.width)
1370 call assert_equal(test[2], position.height)
1371
1372 call popup_close(winid)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001373 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001374 endfor
1375endfunc
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001376
1377func Test_popup_getoptions()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001378 let winid = popup_create('hello', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001379 \ line: 2,
1380 \ col: 3,
1381 \ minwidth: 10,
1382 \ minheight: 11,
1383 \ maxwidth: 20,
1384 \ maxheight: 21,
1385 \ zindex: 100,
1386 \ time: 5000,
1387 \ fixed: 1
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001388 \})
1389 redraw
1390 let res = popup_getoptions(winid)
1391 call assert_equal(2, res.line)
1392 call assert_equal(3, res.col)
1393 call assert_equal(10, res.minwidth)
1394 call assert_equal(11, res.minheight)
1395 call assert_equal(20, res.maxwidth)
1396 call assert_equal(21, res.maxheight)
1397 call assert_equal(100, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001398 call assert_equal(1, res.fixed)
Bram Moolenaarb8350ab2019-08-04 17:59:49 +02001399 call assert_equal(1, res.mapping)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001400 if has('timers')
1401 call assert_equal(5000, res.time)
1402 endif
1403 call popup_close(winid)
1404
1405 let winid = popup_create('hello', {})
1406 redraw
1407 let res = popup_getoptions(winid)
1408 call assert_equal(0, res.line)
1409 call assert_equal(0, res.col)
1410 call assert_equal(0, res.minwidth)
1411 call assert_equal(0, res.minheight)
1412 call assert_equal(0, res.maxwidth)
1413 call assert_equal(0, res.maxheight)
1414 call assert_equal(50, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001415 call assert_equal(0, res.fixed)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001416 if has('timers')
1417 call assert_equal(0, res.time)
1418 endif
1419 call popup_close(winid)
1420 call assert_equal({}, popup_getoptions(winid))
1421endfunc
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001422
1423func Test_popup_option_values()
1424 new
1425 " window-local
1426 setlocal number
1427 setlocal nowrap
1428 " buffer-local
1429 setlocal omnifunc=Something
1430 " global/buffer-local
1431 setlocal path=/there
1432 " global/window-local
Bram Moolenaara112f2d2019-09-01 17:38:09 +02001433 setlocal statusline=2
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001434
1435 let winid = popup_create('hello', {})
1436 call assert_equal(0, getwinvar(winid, '&number'))
1437 call assert_equal(1, getwinvar(winid, '&wrap'))
1438 call assert_equal('', getwinvar(winid, '&omnifunc'))
1439 call assert_equal(&g:path, getwinvar(winid, '&path'))
Bram Moolenaara112f2d2019-09-01 17:38:09 +02001440 call assert_equal(&g:statusline, getwinvar(winid, '&statusline'))
1441
1442 " 'scrolloff' is reset to zero
1443 call assert_equal(5, &scrolloff)
1444 call assert_equal(0, getwinvar(winid, '&scrolloff'))
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001445
1446 call popup_close(winid)
1447 bwipe
1448endfunc
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001449
1450func Test_popup_atcursor()
1451 topleft vnew
1452 call setline(1, [
1453 \ 'xxxxxxxxxxxxxxxxx',
1454 \ 'xxxxxxxxxxxxxxxxx',
1455 \ 'xxxxxxxxxxxxxxxxx',
1456 \])
1457
1458 call cursor(2, 2)
1459 redraw
1460 let winid = popup_atcursor('vim', {})
1461 redraw
1462 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
1463 call assert_equal('xvimxxxxxxxxxxxxx', line)
1464 call popup_close(winid)
1465
1466 call cursor(3, 4)
1467 redraw
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001468 let winid = 'vim'->popup_atcursor({})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001469 redraw
1470 let line = join(map(range(1, 17), 'screenstring(2, v:val)'), '')
1471 call assert_equal('xxxvimxxxxxxxxxxx', line)
1472 call popup_close(winid)
1473
1474 call cursor(1, 1)
1475 redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001476 let winid = popup_create('vim', #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001477 \ line: 'cursor+2',
1478 \ col: 'cursor+1',
1479 \})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001480 redraw
1481 let line = join(map(range(1, 17), 'screenstring(3, v:val)'), '')
1482 call assert_equal('xvimxxxxxxxxxxxxx', line)
1483 call popup_close(winid)
1484
1485 call cursor(3, 3)
1486 redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001487 let winid = popup_create('vim', #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001488 \ line: 'cursor-2',
1489 \ col: 'cursor-1',
1490 \})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001491 redraw
1492 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
1493 call assert_equal('xvimxxxxxxxxxxxxx', line)
1494 call popup_close(winid)
1495
Bram Moolenaar402502d2019-05-30 22:07:36 +02001496 " just enough room above
1497 call cursor(3, 3)
1498 redraw
1499 let winid = popup_atcursor(['vim', 'is great'], {})
1500 redraw
1501 let pos = popup_getpos(winid)
1502 call assert_equal(1, pos.line)
1503 call popup_close(winid)
1504
1505 " not enough room above, popup goes below the cursor
1506 call cursor(3, 3)
1507 redraw
1508 let winid = popup_atcursor(['vim', 'is', 'great'], {})
1509 redraw
1510 let pos = popup_getpos(winid)
1511 call assert_equal(4, pos.line)
1512 call popup_close(winid)
1513
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001514 " cursor in first line, popup in line 2
1515 call cursor(1, 1)
1516 redraw
1517 let winid = popup_atcursor(['vim', 'is', 'great'], {})
1518 redraw
1519 let pos = popup_getpos(winid)
1520 call assert_equal(2, pos.line)
1521 call popup_close(winid)
1522
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001523 bwipe!
1524endfunc
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001525
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001526func Test_popup_atcursor_pos()
1527 CheckScreendump
Dominique Pellec60e9592021-07-03 21:41:38 +02001528 CheckFeature conceal
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001529
1530 let lines =<< trim END
1531 call setline(1, repeat([repeat('-', 60)], 15))
1532 set so=0
1533
1534 normal 9G3|r#
1535 let winid1 = popup_atcursor(['first', 'second'], #{
1536 \ moved: [0, 0, 0],
1537 \ })
1538 normal 9G21|r&
1539 let winid1 = popup_atcursor(['FIrsT', 'SEcoND'], #{
1540 \ pos: 'botright',
1541 \ moved: [0, 0, 0],
1542 \ })
1543 normal 3G27|r%
1544 let winid1 = popup_atcursor(['fiRSt', 'seCOnd'], #{
1545 \ pos: 'topleft',
1546 \ moved: [0, 0, 0],
1547 \ })
1548 normal 3G45|r@
1549 let winid1 = popup_atcursor(['First', 'SeconD'], #{
1550 \ pos: 'topright',
Bram Moolenaarb0992022020-01-30 14:55:42 +01001551 \ moved: range(3),
1552 \ mousemoved: range(3),
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001553 \ })
Bram Moolenaarea042672021-06-29 20:22:32 +02001554
1555 normal 9G27|Rconcealed X
1556 syn match Hidden /concealed/ conceal
1557 set conceallevel=2 concealcursor=n
1558 redraw
1559 normal 0fX
1560 call popup_atcursor('mark', {})
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001561 END
1562 call writefile(lines, 'XtestPopupAtcursorPos')
1563 let buf = RunVimInTerminal('-S XtestPopupAtcursorPos', #{rows: 12})
1564 call VerifyScreenDump(buf, 'Test_popupwin_atcursor_pos', {})
1565
1566 " clean up
1567 call StopVimInTerminal(buf)
1568 call delete('XtestPopupAtcursorPos')
1569endfunc
1570
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001571func Test_popup_beval()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001572 CheckScreendump
Bram Moolenaar1e82a782019-09-21 22:57:06 +02001573 CheckFeature balloon_eval_term
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001574
1575 let lines =<< trim END
1576 call setline(1, range(1, 20))
1577 call setline(5, 'here is some text to hover over')
1578 set balloonevalterm
1579 set balloonexpr=BalloonExpr()
1580 set balloondelay=100
1581 func BalloonExpr()
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001582 let s:winid = [v:beval_text]->popup_beval({})
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001583 return ''
1584 endfunc
1585 func Hover()
1586 call test_setmouse(5, 15)
1587 call feedkeys("\<MouseMove>\<Ignore>", "xt")
1588 sleep 100m
1589 endfunc
1590 func MoveOntoPopup()
1591 call test_setmouse(4, 17)
1592 call feedkeys("\<F4>\<MouseMove>\<Ignore>", "xt")
1593 endfunc
1594 func MoveAway()
1595 call test_setmouse(5, 13)
1596 call feedkeys("\<F5>\<MouseMove>\<Ignore>", "xt")
1597 endfunc
1598 END
1599 call writefile(lines, 'XtestPopupBeval')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001600 let buf = RunVimInTerminal('-S XtestPopupBeval', #{rows: 10})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001601 call TermWait(buf, 50)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001602 call term_sendkeys(buf, 'j')
1603 call term_sendkeys(buf, ":call Hover()\<CR>")
1604 call VerifyScreenDump(buf, 'Test_popupwin_beval_1', {})
1605
1606 call term_sendkeys(buf, ":call MoveOntoPopup()\<CR>")
1607 call VerifyScreenDump(buf, 'Test_popupwin_beval_2', {})
1608
1609 call term_sendkeys(buf, ":call MoveAway()\<CR>")
1610 call VerifyScreenDump(buf, 'Test_popupwin_beval_3', {})
1611
1612 " clean up
1613 call StopVimInTerminal(buf)
1614 call delete('XtestPopupBeval')
1615endfunc
1616
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001617func Test_popup_filter()
1618 new
1619 call setline(1, 'some text')
1620
1621 func MyPopupFilter(winid, c)
Bram Moolenaarec084d32020-02-28 22:44:47 +01001622 if a:c == 'e' || a:c == "\<F9>"
1623 let g:eaten = a:c
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001624 return 1
1625 endif
1626 if a:c == '0'
1627 let g:ignored = '0'
1628 return 0
1629 endif
1630 if a:c == 'x'
1631 call popup_close(a:winid)
1632 return 1
1633 endif
1634 return 0
1635 endfunc
1636
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001637 let winid = 'something'->popup_create(#{filter: 'MyPopupFilter'})
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001638 redraw
1639
1640 " e is consumed by the filter
Bram Moolenaar999db232021-07-04 14:00:55 +02001641 let g:eaten = ''
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001642 call feedkeys('e', 'xt')
1643 call assert_equal('e', g:eaten)
Bram Moolenaarec084d32020-02-28 22:44:47 +01001644 call feedkeys("\<F9>", 'xt')
1645 call assert_equal("\<F9>", g:eaten)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001646
1647 " 0 is ignored by the filter
Bram Moolenaar999db232021-07-04 14:00:55 +02001648 let g:ignored = ''
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001649 normal $
1650 call assert_equal(9, getcurpos()[2])
1651 call feedkeys('0', 'xt')
1652 call assert_equal('0', g:ignored)
Bram Moolenaar999db232021-07-04 14:00:55 +02001653
1654 if has('win32') && has('gui_running')
1655 echo "FIXME: this check is very flaky on MS-Windows GUI, the cursor doesn't move"
1656 else
1657 call assert_equal(1, getcurpos()[2])
1658 endif
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001659
1660 " x closes the popup
1661 call feedkeys('x', 'xt')
Bram Moolenaarec084d32020-02-28 22:44:47 +01001662 call assert_equal("\<F9>", g:eaten)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001663 call assert_equal(-1, winbufnr(winid))
1664
Bram Moolenaar999db232021-07-04 14:00:55 +02001665 unlet g:eaten
1666 unlet g:ignored
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001667 delfunc MyPopupFilter
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001668 call popup_clear()
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001669endfunc
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001670
Bram Moolenaar4934ad02020-09-28 22:29:58 +02001671" this tests that the filter is not used for :normal command
Bram Moolenaar189832b2020-09-23 12:29:11 +02001672func Test_popup_filter_normal_cmd()
1673 CheckScreendump
1674
1675 let lines =<< trim END
Bram Moolenaar4934ad02020-09-28 22:29:58 +02001676 let text = range(1, 20)->map({_, v -> string(v)})
1677 let g:winid = popup_create(text, #{maxheight: 5, minwidth: 3, filter: 'invalidfilter'})
1678 call timer_start(0, {-> win_execute(g:winid, 'norm! 10Gzz')})
Bram Moolenaar189832b2020-09-23 12:29:11 +02001679 END
1680 call writefile(lines, 'XtestPopupNormal')
1681 let buf = RunVimInTerminal('-S XtestPopupNormal', #{rows: 10})
1682 call TermWait(buf, 100)
1683 call VerifyScreenDump(buf, 'Test_popupwin_normal_cmd', {})
1684
1685 call StopVimInTerminal(buf)
1686 call delete('XtestPopupNormal')
1687endfunc
1688
Bram Moolenaar10476522020-09-24 22:57:31 +02001689" test that cursor line highlight is updated after using win_execute()
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02001690func Test_popup_filter_win_execute()
1691 CheckScreendump
1692
1693 let lines =<< trim END
Bram Moolenaar10476522020-09-24 22:57:31 +02001694 let lines = range(1, &lines * 2)->map({_, v -> string(v)})
1695 let g:id = popup_create(lines, #{
1696 \ minheight: &lines - 5,
1697 \ maxheight: &lines - 5,
1698 \ cursorline: 1,
1699 \ })
1700 redraw
1701 END
1702 call writefile(lines, 'XtestPopupWinExecute')
1703 let buf = RunVimInTerminal('-S XtestPopupWinExecute', #{rows: 14})
1704
1705 call term_sendkeys(buf, ":call win_execute(g:id, ['normal 17Gzz'])\<CR>")
1706 call term_sendkeys(buf, ":\<CR>")
1707
1708 call VerifyScreenDump(buf, 'Test_popupwin_win_execute_cursorline', {})
1709
1710 call StopVimInTerminal(buf)
1711 call delete('XtestPopupWinExecute')
1712endfunc
1713
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02001714func Test_popup_set_firstline()
1715 CheckScreendump
1716
1717 let lines =<< trim END
1718 let lines = range(1, 50)->map({_, v -> string(v)})
1719 let g:id = popup_create(lines, #{
1720 \ minwidth: 20,
1721 \ maxwidth: 20,
1722 \ minheight: &lines - 5,
1723 \ maxheight: &lines - 5,
1724 \ cursorline: 1,
1725 \ })
1726 call popup_setoptions(g:id, #{firstline: 10})
1727 redraw
1728 END
1729 call writefile(lines, 'XtestPopupWinSetFirstline')
1730 let buf = RunVimInTerminal('-S XtestPopupWinSetFirstline', #{rows: 16})
1731
1732 call VerifyScreenDump(buf, 'Test_popupwin_set_firstline_1', {})
1733
1734 call term_sendkeys(buf, ":call popup_setoptions(g:id, #{firstline: 5})\<CR>")
1735 call term_sendkeys(buf, ":\<CR>")
1736 call VerifyScreenDump(buf, 'Test_popupwin_set_firstline_2', {})
1737
1738 call StopVimInTerminal(buf)
1739 call delete('XtestPopupWinSetFirstline')
1740endfunc
1741
Bram Moolenaar10476522020-09-24 22:57:31 +02001742" this tests that we don't get stuck with an error in "win_execute()"
1743func Test_popup_filter_win_execute_error()
1744 CheckScreendump
1745
1746 let lines =<< trim END
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02001747 let g:winid = popup_create('some text', {'filter': 'invalidfilter'})
1748 call timer_start(0, {-> win_execute(g:winid, 'invalidCommand')})
1749 END
Bram Moolenaar10476522020-09-24 22:57:31 +02001750 call writefile(lines, 'XtestPopupWinExecuteError')
1751 let buf = RunVimInTerminal('-S XtestPopupWinExecuteError', #{rows: 10, wait_for_ruler: 0})
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02001752
1753 call WaitFor({-> term_getline(buf, 9) =~ 'Not an editor command: invalidCommand'})
1754 call term_sendkeys(buf, "\<CR>")
1755 call WaitFor({-> term_getline(buf, 9) =~ 'Unknown function: invalidfilter'})
1756 call term_sendkeys(buf, "\<CR>")
1757 call WaitFor({-> term_getline(buf, 9) =~ 'Not allowed in a popup window'})
1758 call term_sendkeys(buf, "\<CR>")
1759 call term_sendkeys(buf, "\<CR>")
1760 call VerifyScreenDump(buf, 'Test_popupwin_win_execute', {})
1761
1762 call StopVimInTerminal(buf)
Bram Moolenaar10476522020-09-24 22:57:31 +02001763 call delete('XtestPopupWinExecuteError')
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02001764endfunc
1765
Bram Moolenaara42d9452019-06-15 21:46:30 +02001766func ShowDialog(key, result)
1767 let s:cb_res = 999
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001768 let winid = popup_dialog('do you want to quit (Yes/no)?', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001769 \ filter: 'popup_filter_yesno',
1770 \ callback: 'QuitCallback',
Bram Moolenaara42d9452019-06-15 21:46:30 +02001771 \ })
1772 redraw
1773 call feedkeys(a:key, "xt")
1774 call assert_equal(winid, s:cb_winid)
1775 call assert_equal(a:result, s:cb_res)
1776endfunc
1777
1778func Test_popup_dialog()
1779 func QuitCallback(id, res)
1780 let s:cb_winid = a:id
1781 let s:cb_res = a:res
1782 endfunc
1783
1784 let winid = ShowDialog("y", 1)
1785 let winid = ShowDialog("Y", 1)
1786 let winid = ShowDialog("n", 0)
1787 let winid = ShowDialog("N", 0)
1788 let winid = ShowDialog("x", 0)
1789 let winid = ShowDialog("X", 0)
1790 let winid = ShowDialog("\<Esc>", 0)
1791 let winid = ShowDialog("\<C-C>", -1)
1792
1793 delfunc QuitCallback
1794endfunc
1795
Bram Moolenaara730e552019-06-16 19:05:31 +02001796func ShowMenu(key, result)
1797 let s:cb_res = 999
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001798 let winid = popup_menu(['one', 'two', 'something else'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001799 \ callback: 'QuitCallback',
Bram Moolenaara730e552019-06-16 19:05:31 +02001800 \ })
1801 redraw
1802 call feedkeys(a:key, "xt")
1803 call assert_equal(winid, s:cb_winid)
1804 call assert_equal(a:result, s:cb_res)
1805endfunc
1806
1807func Test_popup_menu()
1808 func QuitCallback(id, res)
1809 let s:cb_winid = a:id
1810 let s:cb_res = a:res
1811 endfunc
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001812 " mapping won't be used in popup
1813 map j k
Bram Moolenaara730e552019-06-16 19:05:31 +02001814
1815 let winid = ShowMenu(" ", 1)
1816 let winid = ShowMenu("j \<CR>", 2)
1817 let winid = ShowMenu("JjK \<CR>", 2)
1818 let winid = ShowMenu("jjjjjj ", 3)
1819 let winid = ShowMenu("kkk ", 1)
1820 let winid = ShowMenu("x", -1)
1821 let winid = ShowMenu("X", -1)
1822 let winid = ShowMenu("\<Esc>", -1)
1823 let winid = ShowMenu("\<C-C>", -1)
1824
1825 delfunc QuitCallback
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001826 unmap j
Bram Moolenaara730e552019-06-16 19:05:31 +02001827endfunc
1828
1829func Test_popup_menu_screenshot()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001830 CheckScreendump
Bram Moolenaara730e552019-06-16 19:05:31 +02001831
1832 let lines =<< trim END
1833 call setline(1, range(1, 20))
1834 hi PopupSelected ctermbg=lightblue
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001835 call popup_menu(['one', 'two', 'another'], #{callback: 'MenuDone', title: ' make a choice from the list '})
Bram Moolenaara730e552019-06-16 19:05:31 +02001836 func MenuDone(id, res)
1837 echomsg "selected " .. a:res
1838 endfunc
1839 END
1840 call writefile(lines, 'XtestPopupMenu')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001841 let buf = RunVimInTerminal('-S XtestPopupMenu', #{rows: 10})
Bram Moolenaara730e552019-06-16 19:05:31 +02001842 call VerifyScreenDump(buf, 'Test_popupwin_menu_01', {})
1843
1844 call term_sendkeys(buf, "jj")
1845 call VerifyScreenDump(buf, 'Test_popupwin_menu_02', {})
1846
1847 call term_sendkeys(buf, " ")
1848 call VerifyScreenDump(buf, 'Test_popupwin_menu_03', {})
1849
1850 " clean up
1851 call StopVimInTerminal(buf)
1852 call delete('XtestPopupMenu')
1853endfunc
1854
Bram Moolenaarf914a332019-07-20 15:09:56 +02001855func Test_popup_menu_narrow()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001856 CheckScreendump
Bram Moolenaarf914a332019-07-20 15:09:56 +02001857
1858 let lines =<< trim END
1859 call setline(1, range(1, 20))
1860 hi PopupSelected ctermbg=green
1861 call popup_menu(['one', 'two', 'three'], #{callback: 'MenuDone'})
1862 func MenuDone(id, res)
1863 echomsg "selected " .. a:res
1864 endfunc
1865 END
1866 call writefile(lines, 'XtestPopupNarrowMenu')
1867 let buf = RunVimInTerminal('-S XtestPopupNarrowMenu', #{rows: 10})
1868 call VerifyScreenDump(buf, 'Test_popupwin_menu_04', {})
1869
1870 " clean up
1871 call term_sendkeys(buf, "x")
1872 call StopVimInTerminal(buf)
1873 call delete('XtestPopupNarrowMenu')
1874endfunc
1875
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001876func Test_popup_title()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001877 CheckScreendump
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001878
1879 " Create a popup without title or border, a line of padding will be added to
1880 " put the title on.
1881 let lines =<< trim END
1882 call setline(1, range(1, 20))
Bram Moolenaar5d458a72019-08-04 21:12:15 +02001883 let winid = popup_create(['one', 'two', 'another'], #{title: 'Title String'})
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001884 END
1885 call writefile(lines, 'XtestPopupTitle')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001886 let buf = RunVimInTerminal('-S XtestPopupTitle', #{rows: 10})
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001887 call VerifyScreenDump(buf, 'Test_popupwin_title', {})
1888
Bram Moolenaar5d458a72019-08-04 21:12:15 +02001889 call term_sendkeys(buf, ":call popup_setoptions(winid, #{maxwidth: 20, title: 'a very long title that is not going to fit'})\<CR>")
1890 call term_sendkeys(buf, ":\<CR>")
1891 call VerifyScreenDump(buf, 'Test_popupwin_longtitle_1', {})
1892
1893 call term_sendkeys(buf, ":call popup_setoptions(winid, #{border: []})\<CR>")
1894 call term_sendkeys(buf, ":\<CR>")
1895 call VerifyScreenDump(buf, 'Test_popupwin_longtitle_2', {})
1896
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01001897 call term_sendkeys(buf, ":call popup_clear()\<CR>")
1898 call term_sendkeys(buf, ":call popup_create(['aaa', 'bbb'], #{title: 'Title', minwidth: 12, padding: [2, 2, 2, 2]})\<CR>")
1899 call term_sendkeys(buf, ":\<CR>")
1900 call VerifyScreenDump(buf, 'Test_popupwin_longtitle_3', {})
1901
1902 call term_sendkeys(buf, ":call popup_clear()\<CR>")
1903 call term_sendkeys(buf, ":call popup_create(['aaa', 'bbb'], #{title: 'Title', minwidth: 12, border: [], padding: [2, 2, 2, 2]})\<CR>")
1904 call term_sendkeys(buf, ":\<CR>")
1905 call VerifyScreenDump(buf, 'Test_popupwin_longtitle_4', {})
1906
Ralf Schandlbc869872021-05-28 14:12:14 +02001907 call term_sendkeys(buf, ":call popup_clear()\<CR>")
rbtnnc6119412021-08-07 13:08:45 +02001908 call term_sendkeys(buf, ":call popup_menu(['This is a line', 'and another line'], #{title: '▶Äあいうえお◀', })\<CR>")
Ralf Schandlbc869872021-05-28 14:12:14 +02001909 call VerifyScreenDump(buf, 'Test_popupwin_multibytetitle', {})
1910 call term_sendkeys(buf, "x")
1911
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001912 " clean up
1913 call StopVimInTerminal(buf)
1914 call delete('XtestPopupTitle')
Bram Moolenaarae943152019-06-16 22:54:14 +02001915
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001916 let winid = popup_create('something', #{title: 'Some Title'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001917 call assert_equal('Some Title', popup_getoptions(winid).title)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001918 call popup_setoptions(winid, #{title: 'Another Title'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001919 call assert_equal('Another Title', popup_getoptions(winid).title)
1920
1921 call popup_clear()
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001922endfunc
1923
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001924func Test_popup_close_callback()
1925 func PopupDone(id, result)
1926 let g:result = a:result
1927 endfunc
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001928 let winid = popup_create('something', #{callback: 'PopupDone'})
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001929 redraw
1930 call popup_close(winid, 'done')
1931 call assert_equal('done', g:result)
1932endfunc
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001933
1934func Test_popup_empty()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001935 let winid = popup_create('', #{padding: [2,2,2,2]})
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001936 redraw
1937 let pos = popup_getpos(winid)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001938 call assert_equal(5, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001939 call assert_equal(5, pos.height)
Bram Moolenaaref6b9792020-05-13 16:34:15 +02001940 call popup_close(winid)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001941
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001942 let winid = popup_create([], #{border: []})
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001943 redraw
1944 let pos = popup_getpos(winid)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001945 call assert_equal(3, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001946 call assert_equal(3, pos.height)
Bram Moolenaaref6b9792020-05-13 16:34:15 +02001947 call popup_close(winid)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001948endfunc
Bram Moolenaar988c4332019-06-02 14:12:11 +02001949
1950func Test_popup_never_behind()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001951 CheckScreendump
1952
Bram Moolenaar988c4332019-06-02 14:12:11 +02001953 " +-----------------------------+
1954 " | | |
1955 " | | |
1956 " | | |
1957 " | line1 |
1958 " |------------line2------------|
1959 " | line3 |
1960 " | line4 |
1961 " | |
1962 " | |
1963 " +-----------------------------+
1964 let lines =<< trim END
Bram Moolenaar988c4332019-06-02 14:12:11 +02001965 split
1966 vsplit
1967 let info_window1 = getwininfo()[0]
1968 let line = info_window1['height']
1969 let col = info_window1['width']
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001970 call popup_create(['line1', 'line2', 'line3', 'line4'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001971 \ line : line,
1972 \ col : col,
Bram Moolenaar988c4332019-06-02 14:12:11 +02001973 \ })
1974 END
1975 call writefile(lines, 'XtestPopupBehind')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001976 let buf = RunVimInTerminal('-S XtestPopupBehind', #{rows: 10})
Bram Moolenaar988c4332019-06-02 14:12:11 +02001977 call term_sendkeys(buf, "\<C-W>w")
1978 call VerifyScreenDump(buf, 'Test_popupwin_behind', {})
1979
1980 " clean up
1981 call StopVimInTerminal(buf)
1982 call delete('XtestPopupBehind')
1983endfunc
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001984
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001985func s:VerifyPosition(p, msg, line, col, width, height)
1986 call assert_equal(a:line, popup_getpos(a:p).line, a:msg . ' (l)')
1987 call assert_equal(a:col, popup_getpos(a:p).col, a:msg . ' (c)')
1988 call assert_equal(a:width, popup_getpos(a:p).width, a:msg . ' (w)')
1989 call assert_equal(a:height, popup_getpos(a:p).height, a:msg . ' (h)')
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001990endfunc
1991
1992func Test_popup_position_adjust()
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001993 " Anything placed past the last cell on the right of the screen is moved to
1994 " the left.
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001995 "
1996 " When wrapping is disabled, we also shift to the left to display on the
1997 " screen, unless fixed is set.
1998
1999 " Entries for cases which don't vary based on wrapping.
2000 " Format is per tests described below
2001 let both_wrap_tests = [
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01002002 \ ['a', 5, &columns, 5, &columns, 1, 1],
2003 \ ['b', 5, &columns + 1, 5, &columns, 1, 1],
2004 \ ['c', 5, &columns - 1, 5, &columns - 1, 1, 1],
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002005 \ ['d', 5, &columns - 2, 5, &columns - 2, 1, 1],
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01002006 \ ['e', 5, &columns - 3, 5, &columns - 3, 1, 1]]
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002007
2008 " these test groups are dicts with:
2009 " - comment: something to identify the group of tests by
2010 " - options: dict of options to merge with the row/col in tests
2011 " - tests: list of cases. Each one is a list with elements:
2012 " - text
2013 " - row
2014 " - col
2015 " - expected row
2016 " - expected col
2017 " - expected width
2018 " - expected height
2019 let tests = [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002020 \ #{
2021 \ comment: 'left-aligned with wrapping',
2022 \ options: #{
2023 \ wrap: 1,
2024 \ pos: 'botleft',
2025 \ },
2026 \ tests: both_wrap_tests + [
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01002027 \ ['aa', 5, &columns, 4, &columns, 1, 2],
2028 \ ['bb', 5, &columns + 1, 4, &columns, 1, 2],
2029 \ ['cc', 5, &columns - 1, 5, &columns - 1, 2, 1],
2030 \ ['dd', 5, &columns - 2, 5, &columns - 2, 2, 1],
2031 \ ['ee', 5, &columns - 3, 5, &columns - 3, 2, 1],
2032 \
2033 \ ['aaa', 5, &columns, 3, &columns, 1, 3],
2034 \ ['bbb', 5, &columns + 1, 3, &columns, 1, 3],
2035 \ ['ccc', 5, &columns - 1, 4, &columns - 1, 2, 2],
2036 \ ['ddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
2037 \ ['eee', 5, &columns - 3, 5, &columns - 3, 3, 1],
2038 \
2039 \ ['aaaa', 5, &columns, 2, &columns, 1, 4],
2040 \ ['bbbb', 5, &columns + 1, 2, &columns, 1, 4],
2041 \ ['cccc', 5, &columns - 1, 4, &columns - 1, 2, 2],
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002042 \ ['dddd', 5, &columns - 2, 4, &columns - 2, 3, 2],
2043 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01002044 \ ['eeee', 5, &columns - 4, 5, &columns - 4, 4, 1],
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002045 \ ],
2046 \ },
2047 \ #{
2048 \ comment: 'left aligned without wrapping',
2049 \ options: #{
2050 \ wrap: 0,
2051 \ pos: 'botleft',
2052 \ },
2053 \ tests: both_wrap_tests + [
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01002054 \ ['aa', 5, &columns, 5, &columns - 1, 2, 1],
2055 \ ['bb', 5, &columns + 1, 5, &columns - 1, 2, 1],
2056 \ ['cc', 5, &columns - 1, 5, &columns - 1, 2, 1],
2057 \ ['dd', 5, &columns - 2, 5, &columns - 2, 2, 1],
2058 \ ['ee', 5, &columns - 3, 5, &columns - 3, 2, 1],
2059 \
2060 \ ['aaa', 5, &columns, 5, &columns - 2, 3, 1],
2061 \ ['bbb', 5, &columns + 1, 5, &columns - 2, 3, 1],
2062 \ ['ccc', 5, &columns - 1, 5, &columns - 2, 3, 1],
2063 \ ['ddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
2064 \ ['eee', 5, &columns - 3, 5, &columns - 3, 3, 1],
2065 \
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002066 \ ['aaaa', 5, &columns, 5, &columns - 3, 4, 1],
2067 \ ['bbbb', 5, &columns + 1, 5, &columns - 3, 4, 1],
2068 \ ['cccc', 5, &columns - 1, 5, &columns - 3, 4, 1],
2069 \ ['dddd', 5, &columns - 2, 5, &columns - 3, 4, 1],
2070 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
2071 \ ],
2072 \ },
2073 \ #{
2074 \ comment: 'left aligned with fixed position',
2075 \ options: #{
2076 \ wrap: 0,
2077 \ fixed: 1,
2078 \ pos: 'botleft',
2079 \ },
2080 \ tests: both_wrap_tests + [
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01002081 \ ['aa', 5, &columns, 5, &columns, 1, 1],
2082 \ ['bb', 5, &columns + 1, 5, &columns, 1, 1],
2083 \ ['cc', 5, &columns - 1, 5, &columns - 1, 2, 1],
2084 \ ['dd', 5, &columns - 2, 5, &columns - 2, 2, 1],
2085 \ ['ee', 5, &columns - 3, 5, &columns - 3, 2, 1],
2086 \
2087 \ ['aaa', 5, &columns, 5, &columns, 1, 1],
2088 \ ['bbb', 5, &columns + 1, 5, &columns, 1, 1],
2089 \ ['ccc', 5, &columns - 1, 5, &columns - 1, 2, 1],
2090 \ ['ddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
2091 \ ['eee', 5, &columns - 3, 5, &columns - 3, 3, 1],
2092 \
2093 \ ['aaaa', 5, &columns, 5, &columns, 1, 1],
2094 \ ['bbbb', 5, &columns + 1, 5, &columns, 1, 1],
2095 \ ['cccc', 5, &columns - 1, 5, &columns - 1, 2, 1],
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002096 \ ['dddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
2097 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
2098 \ ],
2099 \ },
2100 \ ]
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002101
2102 for test_group in tests
2103 for test in test_group.tests
2104 let [ text, line, col, e_line, e_col, e_width, e_height ] = test
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002105 let options = #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002106 \ line: line,
2107 \ col: col,
2108 \ }
2109 call extend(options, test_group.options)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002110
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002111 let p = popup_create(text, options)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002112
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002113 let msg = string(extend(options, #{text: text}))
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002114 call s:VerifyPosition(p, msg, e_line, e_col, e_width, e_height)
2115 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002116 endfor
2117 endfor
2118
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002119 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002120 %bwipe!
2121endfunc
2122
Bram Moolenaar3397f742019-06-02 18:40:06 +02002123func Test_adjust_left_past_screen_width()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002124 " width of screen
2125 let X = join(map(range(&columns), {->'X'}), '')
2126
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002127 let p = popup_create(X, #{line: 1, col: 1, wrap: 0})
2128 call s:VerifyPosition(p, 'full width topleft', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002129
2130 redraw
2131 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
2132 call assert_equal(X, line)
2133
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002134 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002135 redraw
2136
2137 " Same if placed on the right hand side
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002138 let p = popup_create(X, #{line: 1, col: &columns, wrap: 0})
2139 call s:VerifyPosition(p, 'full width topright', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002140
2141 redraw
2142 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
2143 call assert_equal(X, line)
2144
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002145 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002146 redraw
2147
2148 " Extend so > window width
2149 let X .= 'x'
2150
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002151 let p = popup_create(X, #{line: 1, col: 1, wrap: 0})
2152 call s:VerifyPosition(p, 'full width + 1 topleft', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002153
2154 redraw
2155 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
2156 call assert_equal(X[ : -2 ], line)
2157
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002158 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002159 redraw
2160
2161 " Shifted then truncated (the x is not visible)
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002162 let p = popup_create(X, #{line: 1, col: &columns - 3, wrap: 0})
2163 call s:VerifyPosition(p, 'full width + 1 topright', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002164
2165 redraw
2166 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
2167 call assert_equal(X[ : -2 ], line)
2168
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002169 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002170 redraw
2171
2172 " Not shifted, just truncated
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002173 let p = popup_create(X,
2174 \ #{line: 1, col: 2, wrap: 0, fixed: 1})
2175 call s:VerifyPosition(p, 'full width + 1 fixed', 1, 2, &columns - 1, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002176
2177 redraw
2178 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
2179 let e_line = ' ' . X[ 1 : -2 ]
2180 call assert_equal(e_line, line)
2181
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002182 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002183 redraw
2184
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002185 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002186 %bwipe!
Bram Moolenaar3397f742019-06-02 18:40:06 +02002187endfunc
2188
2189func Test_popup_moved()
2190 new
2191 call test_override('char_avail', 1)
2192 call setline(1, ['one word to move around', 'a WORD.and->some thing'])
2193
2194 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002195 let winid = popup_atcursor('text', #{moved: 'any'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02002196 redraw
2197 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002198 call assert_equal([1, 4, 4], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02002199 " trigger the check for last_cursormoved by going into insert mode
2200 call feedkeys("li\<Esc>", 'xt')
2201 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002202 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02002203
2204 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002205 let winid = popup_atcursor('text', #{moved: 'word'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02002206 redraw
2207 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002208 call assert_equal([1, 4, 7], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02002209 call feedkeys("hi\<Esc>", 'xt')
2210 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002211 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02002212
2213 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002214 let winid = popup_atcursor('text', #{moved: 'word'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02002215 redraw
2216 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002217 call assert_equal([1, 4, 7], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02002218 call feedkeys("li\<Esc>", 'xt')
2219 call assert_equal(1, popup_getpos(winid).visible)
2220 call feedkeys("ei\<Esc>", 'xt')
2221 call assert_equal(1, popup_getpos(winid).visible)
2222 call feedkeys("eli\<Esc>", 'xt')
2223 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002224 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02002225
Bram Moolenaar17627312019-06-02 19:53:44 +02002226 " WORD is the default
Bram Moolenaar3397f742019-06-02 18:40:06 +02002227 exe "normal gg0/WORD\<CR>"
Bram Moolenaar17627312019-06-02 19:53:44 +02002228 let winid = popup_atcursor('text', {})
Bram Moolenaar3397f742019-06-02 18:40:06 +02002229 redraw
2230 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002231 call assert_equal([2, 2, 15], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02002232 call feedkeys("eli\<Esc>", 'xt')
2233 call assert_equal(1, popup_getpos(winid).visible)
2234 call feedkeys("wi\<Esc>", 'xt')
2235 call assert_equal(1, popup_getpos(winid).visible)
2236 call feedkeys("Eli\<Esc>", 'xt')
2237 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002238 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02002239
2240 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002241 let winid = popup_atcursor('text', #{moved: [5, 10]})
Bram Moolenaar3397f742019-06-02 18:40:06 +02002242 redraw
2243 call assert_equal(1, popup_getpos(winid).visible)
2244 call feedkeys("eli\<Esc>", 'xt')
2245 call feedkeys("ei\<Esc>", 'xt')
2246 call assert_equal(1, popup_getpos(winid).visible)
2247 call feedkeys("eli\<Esc>", 'xt')
2248 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002249 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02002250
2251 bwipe!
2252 call test_override('ALL', 0)
2253endfunc
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002254
2255func Test_notifications()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002256 CheckFeature timers
2257 CheckScreendump
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002258
Bram Moolenaar0fdddee2019-09-01 15:26:23 +02002259 let lines =<< trim END
2260 call setline(1, range(1, 20))
2261 hi Notification ctermbg=lightblue
2262 call popup_notification('first notification', {})
2263 END
2264 call writefile(lines, 'XtestNotifications')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002265 let buf = RunVimInTerminal('-S XtestNotifications', #{rows: 10})
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002266 call VerifyScreenDump(buf, 'Test_popupwin_notify_01', {})
2267
2268 " second one goes below the first one
Bram Moolenaardfa97f22019-06-15 14:31:55 +02002269 call term_sendkeys(buf, ":hi link PopupNotification Notification\<CR>")
2270 call term_sendkeys(buf, ":call popup_notification('another important notification', {})\<CR>")
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002271 call VerifyScreenDump(buf, 'Test_popupwin_notify_02', {})
2272
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002273 " clean up
2274 call StopVimInTerminal(buf)
2275 call delete('XtestNotifications')
2276endfunc
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002277
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002278func Test_popup_scrollbar()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002279 CheckScreendump
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002280
2281 let lines =<< trim END
2282 call setline(1, range(1, 20))
Bram Moolenaar8da41812019-06-26 18:04:54 +02002283 hi ScrollThumb ctermbg=blue
2284 hi ScrollBar ctermbg=red
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002285 let winid = popup_create(['one', 'two', 'three', 'four', 'five',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002286 \ 'six', 'seven', 'eight', 'nine'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002287 \ minwidth: 8,
2288 \ maxheight: 4,
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002289 \ })
Bram Moolenaar53a95d62019-06-26 03:54:08 +02002290 func ScrollUp()
2291 call feedkeys("\<F3>\<ScrollWheelUp>", "xt")
2292 endfunc
2293 func ScrollDown()
2294 call feedkeys("\<F3>\<ScrollWheelDown>", "xt")
2295 endfunc
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02002296 func ClickTop()
2297 call feedkeys("\<F4>\<LeftMouse>", "xt")
2298 endfunc
2299 func ClickBot()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002300 call popup_setoptions(g:winid, #{border: [], close: 'button'})
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02002301 call feedkeys("\<F5>\<LeftMouse>", "xt")
2302 endfunc
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002303 func Popup_filter(winid, key)
2304 if a:key == 'j'
Bram Moolenaar371806e2020-10-22 13:44:54 +02002305 silent! this_throws_an_error_but_is_ignored
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002306 let line = popup_getoptions(a:winid).firstline
2307 let nlines = line('$', a:winid)
2308 let newline = line < nlines ? (line + 1) : nlines
2309 call popup_setoptions(a:winid, #{firstline: newline})
2310 return v:true
2311 elseif a:key == 'x'
2312 call popup_close(a:winid)
2313 return v:true
2314 endif
2315 endfunc
2316
Bram Moolenaarfc376e02022-05-29 18:18:18 +01002317 def CreatePopup(text: list<string>): number
2318 return popup_create(text, {
Bram Moolenaar6c542f72020-09-27 21:16:45 +02002319 \ minwidth: 30,
2320 \ maxwidth: 30,
2321 \ minheight: 4,
2322 \ maxheight: 4,
2323 \ firstline: 1,
2324 \ lastline: 4,
2325 \ wrap: true,
2326 \ scrollbar: true,
2327 \ mapping: false,
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002328 \ filter: g:Popup_filter,
Bram Moolenaar6c542f72020-09-27 21:16:45 +02002329 \ })
2330 enddef
2331
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002332 func PopupScroll()
2333 call popup_clear()
2334 let text =<< trim END
2335 1
2336 2
2337 3
2338 4
2339 long line long line long line long line long line long line
2340 long line long line long line long line long line long line
2341 long line long line long line long line long line long line
2342 END
Bram Moolenaar6c542f72020-09-27 21:16:45 +02002343 call CreatePopup(text)
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002344 endfunc
Bram Moolenaarfc376e02022-05-29 18:18:18 +01002345 func ScrollBottom()
2346 call popup_clear()
2347 let id = CreatePopup(range(20)->map({k, v -> string(v)}))
2348 call popup_setoptions(id, #{firstline: 20})
2349 endfunc
Bram Moolenaar53a95d62019-06-26 03:54:08 +02002350 map <silent> <F3> :call test_setmouse(5, 36)<CR>
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02002351 map <silent> <F4> :call test_setmouse(4, 42)<CR>
2352 map <silent> <F5> :call test_setmouse(7, 42)<CR>
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002353 END
2354 call writefile(lines, 'XtestPopupScroll')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002355 let buf = RunVimInTerminal('-S XtestPopupScroll', #{rows: 10})
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002356 call VerifyScreenDump(buf, 'Test_popupwin_scroll_1', {})
2357
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002358 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 2})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002359 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002360 call VerifyScreenDump(buf, 'Test_popupwin_scroll_2', {})
2361
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002362 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 6})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002363 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002364 call VerifyScreenDump(buf, 'Test_popupwin_scroll_3', {})
2365
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002366 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 9})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002367 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002368 call VerifyScreenDump(buf, 'Test_popupwin_scroll_4', {})
2369
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02002370 call term_sendkeys(buf, ":call popup_setoptions(winid, #{scrollbarhighlight: 'ScrollBar', thumbhighlight: 'ScrollThumb', firstline: 5})\<CR>")
Bram Moolenaara112f2d2019-09-01 17:38:09 +02002371 " this scrolls two lines (half the window height)
Bram Moolenaar53a95d62019-06-26 03:54:08 +02002372 call term_sendkeys(buf, ":call ScrollUp()\<CR>")
2373 call VerifyScreenDump(buf, 'Test_popupwin_scroll_5', {})
2374
2375 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
2376 call VerifyScreenDump(buf, 'Test_popupwin_scroll_6', {})
2377
2378 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
Bram Moolenaar13b47c32019-06-28 21:55:48 +02002379 " wait a bit, otherwise it fails sometimes (double click recognized?)
2380 sleep 100m
Bram Moolenaar53a95d62019-06-26 03:54:08 +02002381 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
2382 call VerifyScreenDump(buf, 'Test_popupwin_scroll_7', {})
2383
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02002384 call term_sendkeys(buf, ":call ClickTop()\<CR>")
2385 sleep 100m
2386 call term_sendkeys(buf, ":call ClickTop()\<CR>")
2387 call VerifyScreenDump(buf, 'Test_popupwin_scroll_8', {})
2388
2389 call term_sendkeys(buf, ":call ClickBot()\<CR>")
2390 call VerifyScreenDump(buf, 'Test_popupwin_scroll_9', {})
2391
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02002392 " remove the minwidth and maxheight
2393 call term_sendkeys(buf, ":call popup_setoptions(winid, #{maxheight: 0, minwidth: 0})\<CR>")
Bram Moolenaar7e0f4622019-09-17 21:23:39 +02002394 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02002395 call VerifyScreenDump(buf, 'Test_popupwin_scroll_10', {})
2396
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002397 " check size with non-wrapping lines
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002398 call term_sendkeys(buf, ":call g:PopupScroll()\<CR>")
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002399 call VerifyScreenDump(buf, 'Test_popupwin_scroll_11', {})
2400
2401 " check size with wrapping lines
2402 call term_sendkeys(buf, "j")
2403 call VerifyScreenDump(buf, 'Test_popupwin_scroll_12', {})
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002404
Bram Moolenaarfc376e02022-05-29 18:18:18 +01002405 " check thumb when scrolled all the way down
2406 call term_sendkeys(buf, ":call ScrollBottom()\<CR>")
2407 call VerifyScreenDump(buf, 'Test_popupwin_scroll_13', {})
2408
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002409 " clean up
Bram Moolenaar20298ce2020-06-19 21:46:52 +02002410 call term_sendkeys(buf, "x")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002411 call StopVimInTerminal(buf)
2412 call delete('XtestPopupScroll')
2413endfunc
2414
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02002415func Test_popup_too_high_scrollbar()
2416 CheckScreendump
2417
2418 let lines =<< trim END
2419 call setline(1, range(1, 20)->map({i, v -> repeat(v, 10)}))
2420 set scrolloff=0
2421 func ShowPopup()
2422 let winid = popup_atcursor(['one', 'two', 'three', 'four', 'five',
2423 \ 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve'], #{
2424 \ minwidth: 8,
2425 \ border: [],
2426 \ })
2427 endfunc
2428 normal 3G$
2429 call ShowPopup()
2430 END
2431 call writefile(lines, 'XtestPopupToohigh')
2432 let buf = RunVimInTerminal('-S XtestPopupToohigh', #{rows: 10})
2433 call VerifyScreenDump(buf, 'Test_popupwin_toohigh_1', {})
2434
2435 call term_sendkeys(buf, ":call popup_clear()\<CR>")
2436 call term_sendkeys(buf, "8G$")
2437 call term_sendkeys(buf, ":call ShowPopup()\<CR>")
2438 call VerifyScreenDump(buf, 'Test_popupwin_toohigh_2', {})
2439
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02002440 call term_sendkeys(buf, ":call popup_clear()\<CR>")
2441 call term_sendkeys(buf, "gg$")
2442 call term_sendkeys(buf, ":call ShowPopup()\<CR>")
2443 call VerifyScreenDump(buf, 'Test_popupwin_toohigh_3', {})
2444
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02002445 " clean up
2446 call StopVimInTerminal(buf)
2447 call delete('XtestPopupToohigh')
2448endfunc
2449
Bram Moolenaar437a7462019-07-05 20:17:22 +02002450func Test_popup_fitting_scrollbar()
2451 " this was causing a crash, divide by zero
2452 let winid = popup_create([
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002453 \ 'one', 'two', 'longer line that wraps', 'four', 'five'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002454 \ scrollbar: 1,
2455 \ maxwidth: 10,
2456 \ maxheight: 5,
2457 \ firstline: 2})
Bram Moolenaar437a7462019-07-05 20:17:22 +02002458 redraw
2459 call popup_clear()
2460endfunc
2461
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002462func Test_popup_settext()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002463 CheckScreendump
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002464
2465 let lines =<< trim END
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002466 let opts = #{wrap: 0}
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002467 let p = popup_create('test', opts)
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002468 eval p->popup_settext('this is a text')
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002469 END
2470
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002471 call writefile(lines, 'XtestPopupSetText')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002472 let buf = RunVimInTerminal('-S XtestPopupSetText', #{rows: 10})
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002473 call VerifyScreenDump(buf, 'Test_popup_settext_01', {})
2474
2475 " Setting to empty string clears it
2476 call term_sendkeys(buf, ":call popup_settext(p, '')\<CR>")
2477 call VerifyScreenDump(buf, 'Test_popup_settext_02', {})
2478
2479 " Setting a list
2480 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
2481 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
2482
2483 " Shrinking with a list
2484 call term_sendkeys(buf, ":call popup_settext(p, ['a'])\<CR>")
2485 call VerifyScreenDump(buf, 'Test_popup_settext_04', {})
2486
2487 " Growing with a list
2488 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
2489 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
2490
2491 " Empty list clears
2492 call term_sendkeys(buf, ":call popup_settext(p, [])\<CR>")
2493 call VerifyScreenDump(buf, 'Test_popup_settext_05', {})
2494
2495 " Dicts
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002496 call term_sendkeys(buf, ":call popup_settext(p, [#{text: 'aaaa'}, #{text: 'bbbb'}, #{text: 'cccc'}])\<CR>")
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002497 call VerifyScreenDump(buf, 'Test_popup_settext_06', {})
2498
Bram Moolenaarb0992022020-01-30 14:55:42 +01002499 " range() (doesn't work)
2500 call term_sendkeys(buf, ":call popup_settext(p, range(4, 8))\<CR>")
2501 call VerifyScreenDump(buf, 'Test_popup_settext_07', {})
2502
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002503 " clean up
2504 call StopVimInTerminal(buf)
2505 call delete('XtestPopupSetText')
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002506endfunc
2507
Bram Moolenaar1f42f5a2020-09-03 18:52:24 +02002508func Test_popup_settext_getline()
2509 let id = popup_create('', #{ tabpage: 0 })
2510 call popup_settext(id, ['a','b'])
2511 call assert_equal(2, line('$', id)) " OK :)
2512 call popup_close(id)
2513
2514 let id = popup_create('', #{ tabpage: -1 })
2515 call popup_settext(id, ['a','b'])
2516 call assert_equal(2, line('$', id)) " Fails :(
2517 call popup_close(id)
2518endfunc
2519
Bram Moolenaar74f8eec2020-10-15 19:10:56 +02002520func Test_popup_settext_null()
2521 let id = popup_create('', #{ tabpage: 0 })
2522 call popup_settext(id, test_null_list())
2523 call popup_close(id)
2524
2525 let id = popup_create('', #{ tabpage: 0 })
2526 call popup_settext(id, test_null_string())
2527 call popup_close(id)
2528endfunc
2529
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002530func Test_popup_hidden()
2531 new
2532
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002533 let winid = popup_atcursor('text', #{hidden: 1})
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002534 redraw
2535 call assert_equal(0, popup_getpos(winid).visible)
2536 call popup_close(winid)
2537
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002538 let winid = popup_create('text', #{hidden: 1})
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002539 redraw
2540 call assert_equal(0, popup_getpos(winid).visible)
2541 call popup_close(winid)
2542
2543 func QuitCallback(id, res)
2544 let s:cb_winid = a:id
2545 let s:cb_res = a:res
2546 endfunc
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002547 let winid = 'make a choice'->popup_dialog(#{hidden: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002548 \ filter: 'popup_filter_yesno',
2549 \ callback: 'QuitCallback',
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002550 \ })
2551 redraw
2552 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarae943152019-06-16 22:54:14 +02002553 call assert_equal(function('popup_filter_yesno'), popup_getoptions(winid).filter)
2554 call assert_equal(function('QuitCallback'), popup_getoptions(winid).callback)
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002555 exe "normal anot used by filter\<Esc>"
2556 call assert_equal('not used by filter', getline(1))
2557
2558 call popup_show(winid)
2559 call feedkeys('y', "xt")
2560 call assert_equal(1, s:cb_res)
2561
2562 bwipe!
2563 delfunc QuitCallback
2564endfunc
Bram Moolenaarae943152019-06-16 22:54:14 +02002565
2566" Test options not checked elsewhere
2567func Test_set_get_options()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002568 let winid = popup_create('some text', #{highlight: 'Beautiful'})
Bram Moolenaarae943152019-06-16 22:54:14 +02002569 let options = popup_getoptions(winid)
2570 call assert_equal(1, options.wrap)
2571 call assert_equal(0, options.drag)
2572 call assert_equal('Beautiful', options.highlight)
2573
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002574 call popup_setoptions(winid, #{wrap: 0, drag: 1, highlight: 'Another'})
Bram Moolenaarae943152019-06-16 22:54:14 +02002575 let options = popup_getoptions(winid)
2576 call assert_equal(0, options.wrap)
2577 call assert_equal(1, options.drag)
2578 call assert_equal('Another', options.highlight)
2579
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02002580 call assert_fails('call popup_setoptions(winid, [])', 'E715:')
2581 call assert_fails('call popup_setoptions(winid, test_null_dict())', 'E715:')
2582
Bram Moolenaarae943152019-06-16 22:54:14 +02002583 call popup_close(winid)
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02002584 call assert_equal(0, popup_setoptions(winid, options.wrap))
Bram Moolenaarae943152019-06-16 22:54:14 +02002585endfunc
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002586
2587func Test_popupwin_garbage_collect()
2588 func MyPopupFilter(x, winid, c)
2589 " NOP
2590 endfunc
2591
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002592 let winid = popup_create('something', #{filter: function('MyPopupFilter', [{}])})
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002593 call test_garbagecollect_now()
2594 redraw
Bram Moolenaar1bc353b2019-09-01 14:45:28 +02002595 " Must not crash caused by invalid memory access
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002596 call feedkeys('j', 'xt')
2597 call assert_true(v:true)
2598
2599 call popup_close(winid)
2600 delfunc MyPopupFilter
2601endfunc
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002602
Bram Moolenaar581ba392019-09-03 22:08:33 +02002603func Test_popupwin_filter_mode()
2604 func MyPopupFilter(winid, c)
2605 let s:typed = a:c
2606 if a:c == ':' || a:c == "\r" || a:c == 'v'
2607 " can start cmdline mode, get out, and start/stop Visual mode
2608 return 0
2609 endif
2610 return 1
2611 endfunc
2612
2613 " Normal, Visual and Insert mode
2614 let winid = popup_create('something', #{filter: 'MyPopupFilter', filtermode: 'nvi'})
2615 redraw
2616 call feedkeys('x', 'xt')
2617 call assert_equal('x', s:typed)
2618
2619 call feedkeys(":let g:foo = 'foo'\<CR>", 'xt')
2620 call assert_equal(':', s:typed)
2621 call assert_equal('foo', g:foo)
2622
2623 let @x = 'something'
2624 call feedkeys('v$"xy', 'xt')
2625 call assert_equal('y', s:typed)
2626 call assert_equal('something', @x) " yank command is filtered out
2627 call feedkeys('v', 'xt') " end Visual mode
2628
2629 call popup_close(winid)
2630
2631 " only Normal mode
2632 let winid = popup_create('something', #{filter: 'MyPopupFilter', filtermode: 'n'})
2633 redraw
2634 call feedkeys('x', 'xt')
2635 call assert_equal('x', s:typed)
2636
2637 call feedkeys(":let g:foo = 'foo'\<CR>", 'xt')
2638 call assert_equal(':', s:typed)
2639 call assert_equal('foo', g:foo)
2640
2641 let @x = 'something'
2642 call feedkeys('v$"xy', 'xt')
2643 call assert_equal('v', s:typed)
2644 call assert_notequal('something', @x)
2645
2646 call popup_close(winid)
2647
2648 " default: all modes
2649 let winid = popup_create('something', #{filter: 'MyPopupFilter'})
2650 redraw
2651 call feedkeys('x', 'xt')
2652 call assert_equal('x', s:typed)
2653
2654 let g:foo = 'bar'
2655 call feedkeys(":let g:foo = 'foo'\<CR>", 'xt')
2656 call assert_equal("\r", s:typed)
2657 call assert_equal('bar', g:foo)
2658
2659 let @x = 'something'
2660 call feedkeys('v$"xy', 'xt')
2661 call assert_equal('y', s:typed)
2662 call assert_equal('something', @x) " yank command is filtered out
2663 call feedkeys('v', 'xt') " end Visual mode
2664
2665 call popup_close(winid)
2666 delfunc MyPopupFilter
2667endfunc
2668
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002669func Test_popupwin_filter_mouse()
2670 func MyPopupFilter(winid, c)
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002671 let g:got_mousepos = getmousepos()
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002672 return 0
2673 endfunc
2674
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002675 call setline(1, ['.'->repeat(25)]->repeat(10))
2676 let winid = popup_create(['short', 'long line that will wrap', 'other'], #{
2677 \ line: 2,
2678 \ col: 4,
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002679 \ maxwidth: 12,
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002680 \ padding: [],
2681 \ border: [],
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002682 \ filter: 'MyPopupFilter',
2683 \ })
2684 redraw
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002685 " 123456789012345678901
2686 " 1 .....................
2687 " 2 ...+--------------+..
2688 " 3 ...| |..
2689 " 4 ...| short |..
2690 " 5 ...| long line th |..
2691 " 6 ...| at will wrap |..
2692 " 7 ...| other |..
2693 " 8 ...| |..
2694 " 9 ...+--------------+..
2695 " 10 .....................
2696 let tests = []
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002697
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002698 func AddItemOutsidePopup(tests, row, col)
2699 eval a:tests->add(#{clickrow: a:row, clickcol: a:col, result: #{
2700 \ screenrow: a:row, screencol: a:col,
2701 \ winid: win_getid(), winrow: a:row, wincol: a:col,
2702 \ line: a:row, column: a:col,
2703 \ }})
2704 endfunc
2705 func AddItemInPopupBorder(tests, winid, row, col)
2706 eval a:tests->add(#{clickrow: a:row, clickcol: a:col, result: #{
2707 \ screenrow: a:row, screencol: a:col,
2708 \ winid: a:winid, winrow: a:row - 1, wincol: a:col - 3,
2709 \ line: 0, column: 0,
2710 \ }})
2711 endfunc
2712 func AddItemInPopupText(tests, winid, row, col, textline, textcol)
2713 eval a:tests->add(#{clickrow: a:row, clickcol: a:col, result: #{
2714 \ screenrow: a:row, screencol: a:col,
2715 \ winid: a:winid, winrow: a:row - 1, wincol: a:col - 3,
2716 \ line: a:textline, column: a:textcol,
2717 \ }})
2718 endfunc
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002719
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002720 " above and below popup
2721 for c in range(1, 21)
2722 call AddItemOutsidePopup(tests, 1, c)
2723 call AddItemOutsidePopup(tests, 10, c)
2724 endfor
2725 " left and right of popup
2726 for r in range(1, 10)
2727 call AddItemOutsidePopup(tests, r, 3)
2728 call AddItemOutsidePopup(tests, r, 20)
2729 endfor
2730 " top and bottom in popup
2731 for c in range(4, 19)
2732 call AddItemInPopupBorder(tests, winid, 2, c)
2733 call AddItemInPopupBorder(tests, winid, 3, c)
2734 call AddItemInPopupBorder(tests, winid, 8, c)
2735 call AddItemInPopupBorder(tests, winid, 9, c)
2736 endfor
2737 " left and right margin in popup
2738 for r in range(2, 9)
2739 call AddItemInPopupBorder(tests, winid, r, 4)
2740 call AddItemInPopupBorder(tests, winid, r, 5)
2741 call AddItemInPopupBorder(tests, winid, r, 18)
2742 call AddItemInPopupBorder(tests, winid, r, 19)
2743 endfor
2744 " text "short"
2745 call AddItemInPopupText(tests, winid, 4, 6, 1, 1)
2746 call AddItemInPopupText(tests, winid, 4, 10, 1, 5)
2747 call AddItemInPopupText(tests, winid, 4, 11, 1, 6)
2748 call AddItemInPopupText(tests, winid, 4, 17, 1, 6)
2749 " text "long line th"
2750 call AddItemInPopupText(tests, winid, 5, 6, 2, 1)
2751 call AddItemInPopupText(tests, winid, 5, 10, 2, 5)
2752 call AddItemInPopupText(tests, winid, 5, 17, 2, 12)
2753 " text "at will wrap"
2754 call AddItemInPopupText(tests, winid, 6, 6, 2, 13)
2755 call AddItemInPopupText(tests, winid, 6, 10, 2, 17)
2756 call AddItemInPopupText(tests, winid, 6, 17, 2, 24)
2757 " text "other"
2758 call AddItemInPopupText(tests, winid, 7, 6, 3, 1)
2759 call AddItemInPopupText(tests, winid, 7, 10, 3, 5)
2760 call AddItemInPopupText(tests, winid, 7, 11, 3, 6)
2761 call AddItemInPopupText(tests, winid, 7, 17, 3, 6)
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002762
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002763 for item in tests
2764 call test_setmouse(item.clickrow, item.clickcol)
2765 call feedkeys("\<LeftMouse>", 'xt')
2766 call assert_equal(item.result, g:got_mousepos)
2767 endfor
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002768
2769 call popup_close(winid)
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002770 enew!
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002771 delfunc MyPopupFilter
2772endfunc
2773
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002774func Test_popupwin_with_buffer()
2775 call writefile(['some text', 'in a buffer'], 'XsomeFile')
2776 let buf = bufadd('XsomeFile')
2777 call assert_equal(0, bufloaded(buf))
Bram Moolenaar46451042019-08-24 15:50:46 +02002778
2779 setlocal number
2780 call setbufvar(buf, "&wrapmargin", 13)
2781
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002782 let winid = popup_create(buf, {})
2783 call assert_notequal(0, winid)
2784 let pos = popup_getpos(winid)
2785 call assert_equal(2, pos.height)
2786 call assert_equal(1, bufloaded(buf))
Bram Moolenaar46451042019-08-24 15:50:46 +02002787
2788 " window-local option is set to default, buffer-local is not
2789 call assert_equal(0, getwinvar(winid, '&number'))
2790 call assert_equal(13, getbufvar(buf, '&wrapmargin'))
2791
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002792 call popup_close(winid)
2793 call assert_equal({}, popup_getpos(winid))
2794 call assert_equal(1, bufloaded(buf))
2795 exe 'bwipe! ' .. buf
Bram Moolenaar46451042019-08-24 15:50:46 +02002796 setlocal nonumber
Bram Moolenaar7866b872019-07-01 22:21:01 +02002797
2798 edit test_popupwin.vim
2799 let winid = popup_create(bufnr(''), {})
2800 redraw
2801 call popup_close(winid)
Bram Moolenaar3940ec62019-07-05 21:53:24 +02002802 call delete('XsomeFile')
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002803endfunc
Bram Moolenaare296e312019-07-03 23:20:18 +02002804
Bram Moolenaar188639d2022-04-04 16:57:21 +01002805func Test_popupwin_buffer_with_swapfile()
2806 call writefile(['some text', 'in a buffer'], 'XopenFile')
2807 call writefile([''], '.XopenFile.swp')
2808 let g:ignoreSwapExists = 1
2809
2810 let bufnr = bufadd('XopenFile')
2811 call assert_equal(0, bufloaded(bufnr))
2812 let winid = popup_create(bufnr, {'hidden': 1})
2813 call assert_equal(1, bufloaded(bufnr))
2814 call popup_close(winid)
2815
2816 exe 'buffer ' .. bufnr
2817 call assert_equal(1, &readonly)
2818 bwipe!
2819
2820 call delete('XopenFile')
2821 call delete('.XopenFile.swp')
2822 unlet g:ignoreSwapExists
2823endfunc
2824
Bram Moolenaare0d749a2019-09-25 22:14:48 +02002825func Test_popupwin_terminal_buffer()
Bram Moolenaard2c1fb42019-09-25 23:06:40 +02002826 CheckFeature terminal
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002827 CheckUnix
Bram Moolenaare06a28f2020-05-13 23:24:12 +02002828 " Starting a terminal to run a shell in is considered flaky.
2829 let g:test_is_flaky = 1
Bram Moolenaard2c1fb42019-09-25 23:06:40 +02002830
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002831 let origwin = win_getid()
Bram Moolenaar349f6092020-10-06 20:46:49 +02002832
2833 " open help window to test that :help below fails
2834 help
2835
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002836 let termbuf = term_start(&shell, #{hidden: 1})
Bram Moolenaarf5452692020-11-28 21:56:06 +01002837 let winid = popup_create(termbuf, #{minwidth: 40, minheight: 10, border: []})
Bram Moolenaar0353f562020-12-17 22:27:38 +01002838 " Wait for shell to start
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002839 call WaitForAssert({-> assert_equal("run", job_status(term_getjob(termbuf)))})
Bram Moolenaar0353f562020-12-17 22:27:38 +01002840 " Wait for a prompt (see border char first, then space after prompt)
2841 call WaitForAssert({ -> assert_equal(' ', screenstring(screenrow(), screencol() - 1))})
Bram Moolenaarf5452692020-11-28 21:56:06 +01002842
2843 " When typing a character, the cursor is after it.
2844 call feedkeys("x", 'xt')
Bram Moolenaar0353f562020-12-17 22:27:38 +01002845 call term_wait(termbuf)
Bram Moolenaarf5452692020-11-28 21:56:06 +01002846 redraw
2847 call WaitForAssert({ -> assert_equal('x', screenstring(screenrow(), screencol() - 1))})
2848 call feedkeys("\<BS>", 'xt')
2849
Bram Moolenaar631ebc42020-02-03 22:15:26 +01002850 " Check this doesn't crash
2851 call assert_equal(winnr(), winnr('j'))
2852 call assert_equal(winnr(), winnr('k'))
2853 call assert_equal(winnr(), winnr('h'))
2854 call assert_equal(winnr(), winnr('l'))
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02002855
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002856 " Cannot quit while job is running
2857 call assert_fails('call feedkeys("\<C-W>:quit\<CR>", "xt")', 'E948:')
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02002858
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002859 " Cannot enter Terminal-Normal mode. (TODO: but it works...)
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02002860 call feedkeys("xxx\<C-W>N", 'xt')
2861 call assert_fails('call feedkeys("gf", "xt")', 'E863:')
2862 call feedkeys("a\<C-U>", 'xt')
2863
Bram Moolenaar3f65c662020-05-27 23:15:16 +02002864 " Cannot escape from terminal window
2865 call assert_fails('tab drop xxx', 'E863:')
Bram Moolenaar349f6092020-10-06 20:46:49 +02002866 call assert_fails('help', 'E994:')
Bram Moolenaar3f65c662020-05-27 23:15:16 +02002867
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002868 " Cannot open a second one.
2869 let termbuf2 = term_start(&shell, #{hidden: 1})
2870 call assert_fails('call popup_create(termbuf2, #{})', 'E861:')
2871 call term_sendkeys(termbuf2, "exit\<CR>")
2872
Bram Moolenaard28950f2022-05-29 14:13:04 +01002873 " Exiting shell puts popup window in Terminal-Normal mode.
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002874 call feedkeys("exit\<CR>", 'xt')
2875 " Wait for shell to exit
Bram Moolenaarb2b218d2020-06-22 20:22:19 +02002876 call WaitForAssert({-> assert_equal("dead", job_status(term_getjob(termbuf)))})
Bram Moolenaar373c6512020-06-22 19:24:23 +02002877
Bram Moolenaar349f6092020-10-06 20:46:49 +02002878 helpclose
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002879 call feedkeys(":quit\<CR>", 'xt')
2880 call assert_equal(origwin, win_getid())
Bram Moolenaare0d749a2019-09-25 22:14:48 +02002881endfunc
2882
Bram Moolenaard28950f2022-05-29 14:13:04 +01002883func Test_popupwin_terminal_scrollbar()
2884 CheckFeature terminal
2885 CheckScreendump
2886 CheckUnix
2887
2888 call writefile(range(50), 'Xtestfile')
2889 let lines =<< trim END
2890 vim9script
2891
2892 term_start(['cat', 'Xtestfile'], {hidden: true})
2893 ->popup_create({
2894 minwidth: 40,
2895 maxwidth: 40,
2896 minheight: 8,
2897 maxheight: 8,
2898 scrollbar: true,
2899 border: []
2900 })
2901 END
2902 call writefile(lines, 'Xpterm')
2903 let buf = RunVimInTerminal('-S Xpterm', #{rows: 15})
2904 call VerifyScreenDump(buf, 'Test_popupwin_poptermscroll_1', {})
2905
2906 " scroll to the middle
2907 call term_sendkeys(buf, "50%")
2908 call VerifyScreenDump(buf, 'Test_popupwin_poptermscroll_2', {})
2909
2910 " close the popupwin.
2911 call term_sendkeys(buf, ":q\<CR>")
2912 call VerifyScreenDump(buf, 'Test_popupwin_poptermscroll_3', {})
2913
2914 call StopVimInTerminal(buf)
2915 call delete('Xtestfile')
2916 call delete('Xpterm')
2917endfunc
2918
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002919func Test_popupwin_close_prevwin()
2920 CheckFeature terminal
Bram Moolenaar62f93f42020-09-02 22:33:24 +02002921 call Popupwin_close_prevwin()
2922endfunc
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002923
Bram Moolenaar62f93f42020-09-02 22:33:24 +02002924def Popupwin_close_prevwin()
2925 assert_equal(1, winnr('$'))
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002926 split
2927 wincmd b
Bram Moolenaar62f93f42020-09-02 22:33:24 +02002928 assert_equal(2, winnr())
Bram Moolenaare0de1712020-12-02 17:36:54 +01002929 var buf = term_start(&shell, {hidden: 1})
Bram Moolenaar62f93f42020-09-02 22:33:24 +02002930 popup_create(buf, {})
Bram Moolenaar62aec932022-01-29 21:45:34 +00002931 g:TermWait(buf, 100)
Bram Moolenaar62f93f42020-09-02 22:33:24 +02002932 popup_clear(true)
2933 assert_equal(2, winnr())
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002934
2935 quit
2936 exe 'bwipe! ' .. buf
Bram Moolenaar62f93f42020-09-02 22:33:24 +02002937enddef
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002938
Bram Moolenaar934470e2019-09-01 23:27:05 +02002939func Test_popupwin_with_buffer_and_filter()
2940 new Xwithfilter
2941 call setline(1, range(100))
2942 let bufnr = bufnr()
2943 hide
2944
2945 func BufferFilter(win, key)
2946 if a:key == 'G'
2947 " recursive use of "G" does not cause problems.
2948 call win_execute(a:win, 'normal! G')
2949 return 1
2950 endif
2951 return 0
2952 endfunc
2953
2954 let winid = popup_create(bufnr, #{maxheight: 5, filter: 'BufferFilter'})
2955 call assert_equal(1, popup_getpos(winid).firstline)
2956 redraw
2957 call feedkeys("G", 'xt')
2958 call assert_equal(99, popup_getpos(winid).firstline)
2959
2960 call popup_close(winid)
2961 exe 'bwipe! ' .. bufnr
2962endfunc
2963
Bram Moolenaare296e312019-07-03 23:20:18 +02002964func Test_popupwin_width()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002965 let winid = popup_create(repeat(['short', 'long long long line', 'medium width'], 50), #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002966 \ maxwidth: 40,
2967 \ maxheight: 10,
Bram Moolenaare296e312019-07-03 23:20:18 +02002968 \ })
2969 for top in range(1, 20)
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002970 eval winid->popup_setoptions(#{firstline: top})
Bram Moolenaare296e312019-07-03 23:20:18 +02002971 redraw
2972 call assert_equal(19, popup_getpos(winid).width)
2973 endfor
2974 call popup_clear()
2975endfunc
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02002976
2977func Test_popupwin_buf_close()
2978 let buf = bufadd('Xtestbuf')
2979 call bufload(buf)
2980 call setbufline(buf, 1, ['just', 'some', 'lines'])
2981 let winid = popup_create(buf, {})
2982 redraw
2983 call assert_equal(3, popup_getpos(winid).height)
2984 let bufinfo = getbufinfo(buf)[0]
2985 call assert_equal(1, bufinfo.changed)
2986 call assert_equal(0, bufinfo.hidden)
2987 call assert_equal(0, bufinfo.listed)
2988 call assert_equal(1, bufinfo.loaded)
2989 call assert_equal([], bufinfo.windows)
2990 call assert_equal([winid], bufinfo.popups)
2991
2992 call popup_close(winid)
2993 call assert_equal({}, popup_getpos(winid))
2994 let bufinfo = getbufinfo(buf)[0]
2995 call assert_equal(1, bufinfo.changed)
2996 call assert_equal(1, bufinfo.hidden)
2997 call assert_equal(0, bufinfo.listed)
2998 call assert_equal(1, bufinfo.loaded)
2999 call assert_equal([], bufinfo.windows)
3000 call assert_equal([], bufinfo.popups)
3001 exe 'bwipe! ' .. buf
3002endfunc
Bram Moolenaar017c2692019-07-13 14:17:51 +02003003
3004func Test_popup_menu_with_maxwidth()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02003005 CheckScreendump
Bram Moolenaar017c2692019-07-13 14:17:51 +02003006
3007 let lines =<< trim END
3008 call setline(1, range(1, 10))
3009 hi ScrollThumb ctermbg=blue
3010 hi ScrollBar ctermbg=red
3011 func PopupMenu(lines, line, col, scrollbar = 0)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003012 return popup_menu(a:lines, #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003013 \ maxwidth: 10,
3014 \ maxheight: 3,
3015 \ pos : 'topleft',
3016 \ col : a:col,
3017 \ line : a:line,
3018 \ scrollbar : a:scrollbar,
Bram Moolenaar017c2692019-07-13 14:17:51 +02003019 \ })
3020 endfunc
3021 call PopupMenu(['x'], 1, 1)
3022 call PopupMenu(['123456789|'], 1, 16)
3023 call PopupMenu(['123456789|' .. ' '], 7, 1)
3024 call PopupMenu([repeat('123456789|', 100)], 7, 16)
3025 call PopupMenu(repeat(['123456789|' .. ' '], 5), 1, 33, 1)
3026 END
3027 call writefile(lines, 'XtestPopupMenuMaxWidth')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003028 let buf = RunVimInTerminal('-S XtestPopupMenuMaxWidth', #{rows: 13})
Bram Moolenaar017c2692019-07-13 14:17:51 +02003029 call VerifyScreenDump(buf, 'Test_popupwin_menu_maxwidth_1', {})
3030
3031 " close the menu popupwin.
3032 call term_sendkeys(buf, " ")
3033 call term_sendkeys(buf, " ")
3034 call term_sendkeys(buf, " ")
3035 call term_sendkeys(buf, " ")
3036 call term_sendkeys(buf, " ")
3037
3038 " clean up
3039 call StopVimInTerminal(buf)
3040 call delete('XtestPopupMenuMaxWidth')
3041endfunc
3042
Bram Moolenaara901a372019-07-13 16:38:50 +02003043func Test_popup_menu_with_scrollbar()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02003044 CheckScreendump
Bram Moolenaara901a372019-07-13 16:38:50 +02003045
3046 let lines =<< trim END
3047 call setline(1, range(1, 20))
3048 hi ScrollThumb ctermbg=blue
3049 hi ScrollBar ctermbg=red
Bram Moolenaar6a124e62019-09-04 18:15:19 +02003050 eval ['one', 'two', 'three', 'four', 'five',
3051 \ 'six', 'seven', 'eight', 'nine']
3052 \ ->popup_menu(#{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003053 \ minwidth: 8,
3054 \ maxheight: 3,
Bram Moolenaara901a372019-07-13 16:38:50 +02003055 \ })
3056 END
3057 call writefile(lines, 'XtestPopupMenuScroll')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003058 let buf = RunVimInTerminal('-S XtestPopupMenuScroll', #{rows: 10})
Bram Moolenaara901a372019-07-13 16:38:50 +02003059
3060 call term_sendkeys(buf, "j")
3061 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_1', {})
3062
3063 call term_sendkeys(buf, "jjj")
3064 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_2', {})
3065
3066 " if the cursor is the bottom line, it stays at the bottom line.
3067 call term_sendkeys(buf, repeat("j", 20))
3068 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_3', {})
3069
3070 call term_sendkeys(buf, "kk")
3071 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_4', {})
3072
3073 call term_sendkeys(buf, "k")
3074 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_5', {})
3075
3076 " if the cursor is in the top line, it stays in the top line.
3077 call term_sendkeys(buf, repeat("k", 20))
3078 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_6', {})
3079
3080 " close the menu popupwin.
3081 call term_sendkeys(buf, " ")
3082
3083 " clean up
3084 call StopVimInTerminal(buf)
3085 call delete('XtestPopupMenuScroll')
3086endfunc
3087
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003088func Test_popup_menu_filter()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02003089 CheckScreendump
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003090
3091 let lines =<< trim END
3092 function! MyFilter(winid, key) abort
3093 if a:key == "0"
3094 call win_execute(a:winid, "call setpos('.', [0, 1, 1, 0])")
3095 return 1
3096 endif
3097 if a:key == "G"
3098 call win_execute(a:winid, "call setpos('.', [0, line('$'), 1, 0])")
3099 return 1
3100 endif
3101 if a:key == "j"
3102 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0])")
3103 return 1
3104 endif
3105 if a:key == "k"
3106 call win_execute(a:winid, "call setpos('.', [0, line('.') - 1, 1, 0])")
3107 return 1
3108 endif
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02003109 if a:key == ':'
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003110 call popup_close(a:winid)
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02003111 return 0
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003112 endif
3113 return 0
3114 endfunction
3115 call popup_menu(['111', '222', '333', '444', '555', '666', '777', '888', '999'], #{
3116 \ maxheight : 3,
3117 \ filter : 'MyFilter'
3118 \ })
3119 END
3120 call writefile(lines, 'XtestPopupMenuFilter')
3121 let buf = RunVimInTerminal('-S XtestPopupMenuFilter', #{rows: 10})
3122
3123 call term_sendkeys(buf, "j")
3124 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_1', {})
3125
3126 call term_sendkeys(buf, "k")
3127 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_2', {})
3128
3129 call term_sendkeys(buf, "G")
3130 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_3', {})
3131
3132 call term_sendkeys(buf, "0")
3133 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_4', {})
3134
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02003135 " check that when the popup is closed in the filter the screen is redrawn
3136 call term_sendkeys(buf, ":")
3137 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_5', {})
3138 call term_sendkeys(buf, "\<CR>")
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003139
3140 " clean up
3141 call StopVimInTerminal(buf)
3142 call delete('XtestPopupMenuFilter')
3143endfunc
3144
3145func Test_popup_cursorline()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02003146 CheckScreendump
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003147
3148 let winid = popup_create('some text', {})
3149 call assert_equal(0, popup_getoptions(winid).cursorline)
3150 call popup_close(winid)
3151
3152 let winid = popup_create('some text', #{ cursorline: 1, })
3153 call assert_equal(1, popup_getoptions(winid).cursorline)
3154 call popup_close(winid)
3155
Bram Moolenaar6bfc4752021-02-21 23:12:18 +01003156 let winid = popup_create('some text', #{ cursorline: v:true, })
3157 call assert_equal(1, popup_getoptions(winid).cursorline)
3158 call popup_close(winid)
3159
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003160 let winid = popup_create('some text', #{ cursorline: 0, })
3161 call assert_equal(0, popup_getoptions(winid).cursorline)
3162 call popup_close(winid)
3163
3164 let winid = popup_menu('some text', {})
3165 call assert_equal(1, popup_getoptions(winid).cursorline)
3166 call popup_close(winid)
3167
3168 let winid = popup_menu('some text', #{ cursorline: 1, })
3169 call assert_equal(1, popup_getoptions(winid).cursorline)
3170 call popup_close(winid)
3171
3172 let winid = popup_menu('some text', #{ cursorline: 0, })
3173 call assert_equal(0, popup_getoptions(winid).cursorline)
3174 call popup_close(winid)
3175
3176 " ---------
3177 " Pattern 1
3178 " ---------
3179 let lines =<< trim END
3180 call popup_create(['111', '222', '333'], #{ cursorline : 0 })
3181 END
3182 call writefile(lines, 'XtestPopupCursorLine')
3183 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
3184 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_1', {})
3185 call term_sendkeys(buf, ":call popup_clear()\<cr>")
3186 call StopVimInTerminal(buf)
3187
3188 " ---------
3189 " Pattern 2
3190 " ---------
3191 let lines =<< trim END
3192 call popup_create(['111', '222', '333'], #{ cursorline : 1 })
3193 END
3194 call writefile(lines, 'XtestPopupCursorLine')
3195 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
3196 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_2', {})
3197 call term_sendkeys(buf, ":call popup_clear()\<cr>")
3198 call StopVimInTerminal(buf)
3199
3200 " ---------
3201 " Pattern 3
3202 " ---------
3203 let lines =<< trim END
3204 function! MyFilter(winid, key) abort
3205 if a:key == "j"
3206 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0]) | redraw")
3207 return 1
3208 endif
3209 if a:key == 'x'
3210 call popup_close(a:winid)
3211 return 1
3212 endif
3213 return 0
3214 endfunction
3215 call popup_menu(['111', '222', '333'], #{
3216 \ cursorline : 0,
3217 \ maxheight : 2,
3218 \ filter : 'MyFilter',
3219 \ })
3220 END
3221 call writefile(lines, 'XtestPopupCursorLine')
3222 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
3223 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_3', {})
3224 call term_sendkeys(buf, "j")
3225 call term_sendkeys(buf, "j")
3226 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_4', {})
3227 call term_sendkeys(buf, "x")
3228 call StopVimInTerminal(buf)
3229
3230 " ---------
3231 " Pattern 4
3232 " ---------
3233 let lines =<< trim END
3234 function! MyFilter(winid, key) abort
3235 if a:key == "j"
3236 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0]) | redraw")
3237 return 1
3238 endif
3239 if a:key == 'x'
3240 call popup_close(a:winid)
3241 return 1
3242 endif
3243 return 0
3244 endfunction
3245 call popup_menu(['111', '222', '333'], #{
3246 \ cursorline : 1,
3247 \ maxheight : 2,
3248 \ filter : 'MyFilter',
3249 \ })
3250 END
3251 call writefile(lines, 'XtestPopupCursorLine')
3252 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
3253 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_5', {})
3254 call term_sendkeys(buf, "j")
3255 call term_sendkeys(buf, "j")
3256 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_6', {})
3257 call term_sendkeys(buf, "x")
3258 call StopVimInTerminal(buf)
3259
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02003260 " ---------
3261 " Cursor in second line when creating the popup
3262 " ---------
3263 let lines =<< trim END
3264 let winid = popup_create(['111', '222', '333'], #{
3265 \ cursorline : 1,
3266 \ })
3267 call win_execute(winid, "2")
3268 END
3269 call writefile(lines, 'XtestPopupCursorLine')
3270 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
3271 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_7', {})
3272 call StopVimInTerminal(buf)
3273
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003274 call delete('XtestPopupCursorLine')
Bram Moolenaar4eb7dae2019-11-12 22:33:45 +01003275
3276 " ---------
3277 " Use current buffer for popupmenu
3278 " ---------
3279 let lines =<< trim END
3280 call setline(1, ['one', 'two', 'three'])
3281 let winid = popup_create(bufnr('%'), #{
3282 \ cursorline : 1,
3283 \ })
3284 call win_execute(winid, "2")
3285 END
3286 call writefile(lines, 'XtestPopupCursorLine')
3287 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
3288 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_8', {})
3289 call StopVimInTerminal(buf)
3290
3291 call delete('XtestPopupCursorLine')
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003292endfunc
3293
Bram Moolenaar6bfc4752021-02-21 23:12:18 +01003294def Test_popup_cursorline_vim9()
3295 var winid = popup_create('some text', { cursorline: true, })
3296 assert_equal(1, popup_getoptions(winid).cursorline)
3297 popup_close(winid)
3298
3299 assert_fails("popup_create('some text', { cursorline: 2, })", 'E1023:')
3300 popup_clear()
3301enddef
3302
Bram Moolenaarf914a332019-07-20 15:09:56 +02003303func Test_previewpopup()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02003304 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003305 CheckFeature quickfix
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02003306
Bram Moolenaarf914a332019-07-20 15:09:56 +02003307 call writefile([
3308 \ "!_TAG_FILE_ENCODING\tutf-8\t//",
3309 \ "another\tXtagfile\t/^this is another",
3310 \ "theword\tXtagfile\t/^theword"],
3311 \ 'Xtags')
3312 call writefile(range(1,20)
3313 \ + ['theword is here']
3314 \ + range(22, 27)
3315 \ + ['this is another place']
3316 \ + range(29, 40),
3317 \ "Xtagfile")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02003318 call writefile(range(1,10)
3319 \ + ['searched word is here']
3320 \ + range(12, 20),
3321 \ "Xheader.h")
Bram Moolenaarf914a332019-07-20 15:09:56 +02003322 let lines =<< trim END
3323 set tags=Xtags
3324 call setline(1, [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02003325 \ 'one',
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02003326 \ '#include "Xheader.h"',
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02003327 \ 'three',
3328 \ 'four',
3329 \ 'five',
3330 \ 'six',
3331 \ 'seven',
3332 \ 'find theword somewhere',
3333 \ 'nine',
3334 \ 'this is another word',
3335 \ 'very long line where the word is also another'])
Bram Moolenaarf914a332019-07-20 15:09:56 +02003336 set previewpopup=height:4,width:40
Bram Moolenaar60577482021-03-04 21:35:07 +01003337 hi OtherColor ctermbg=lightcyan guibg=lightcyan
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02003338 set path=.
Bram Moolenaarf914a332019-07-20 15:09:56 +02003339 END
3340 call writefile(lines, 'XtestPreviewPopup')
3341 let buf = RunVimInTerminal('-S XtestPreviewPopup', #{rows: 14})
3342
3343 call term_sendkeys(buf, "/theword\<CR>\<C-W>}")
3344 call term_sendkeys(buf, ":\<CR>")
3345 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_1', {})
3346
Bram Moolenaar60577482021-03-04 21:35:07 +01003347 call term_sendkeys(buf, ":set previewpopup+=highlight:OtherColor\<CR>")
Bram Moolenaarf914a332019-07-20 15:09:56 +02003348 call term_sendkeys(buf, "/another\<CR>\<C-W>}")
3349 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_2', {})
3350
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003351 call term_sendkeys(buf, ":call popup_move(popup_findpreview(), #{col: 15})\<CR>")
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02003352 call term_sendkeys(buf, ":\<CR>")
3353 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_3', {})
3354
3355 call term_sendkeys(buf, "/another\<CR>\<C-W>}")
3356 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_4', {})
3357
Bram Moolenaar799439a2020-02-11 21:44:17 +01003358 call term_sendkeys(buf, ":silent cd ..\<CR>:\<CR>")
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02003359 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_5', {})
Bram Moolenaar799439a2020-02-11 21:44:17 +01003360 call term_sendkeys(buf, ":silent cd testdir\<CR>")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02003361
Bram Moolenaar60577482021-03-04 21:35:07 +01003362 call term_sendkeys(buf, ":set previewpopup-=highlight:OtherColor\<CR>")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02003363 call term_sendkeys(buf, ":pclose\<CR>")
Bram Moolenaar78d629a2019-08-16 17:31:15 +02003364 call term_sendkeys(buf, ":\<BS>")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02003365 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_6', {})
3366
3367 call term_sendkeys(buf, ":pedit +/theword Xtagfile\<CR>")
3368 call term_sendkeys(buf, ":\<CR>")
3369 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_7', {})
3370
3371 call term_sendkeys(buf, ":pclose\<CR>")
3372 call term_sendkeys(buf, ":psearch searched\<CR>")
3373 call term_sendkeys(buf, ":\<CR>")
3374 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_8', {})
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02003375
Bram Moolenaar8bf716c2020-01-23 15:33:54 +01003376 call term_sendkeys(buf, "\<C-W>p")
3377 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_9', {})
3378
3379 call term_sendkeys(buf, ":call win_execute(popup_findpreview(), 'call popup_clear()')\<CR>")
3380 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_10', {})
3381
Bram Moolenaarf914a332019-07-20 15:09:56 +02003382 call StopVimInTerminal(buf)
3383 call delete('Xtags')
3384 call delete('Xtagfile')
3385 call delete('XtestPreviewPopup')
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02003386 call delete('Xheader.h')
Bram Moolenaarf914a332019-07-20 15:09:56 +02003387endfunc
3388
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003389func Get_popupmenu_lines()
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003390 let lines =<< trim END
3391 set completeopt+=preview,popup
3392 set completefunc=CompleteFuncDict
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02003393 hi InfoPopup ctermbg=yellow
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003394
3395 func CompleteFuncDict(findstart, base)
3396 if a:findstart
3397 if col('.') > 10
3398 return col('.') - 10
3399 endif
3400 return 0
3401 endif
3402
3403 return {
3404 \ 'words': [
3405 \ {
3406 \ 'word': 'aword',
3407 \ 'abbr': 'wrd',
3408 \ 'menu': 'extra text',
3409 \ 'info': 'words are cool',
3410 \ 'kind': 'W',
3411 \ 'user_data': 'test'
3412 \ },
3413 \ {
3414 \ 'word': 'anotherword',
3415 \ 'abbr': 'anotwrd',
3416 \ 'menu': 'extra text',
3417 \ 'info': "other words are\ncooler than this and some more text\nto make wrap",
3418 \ 'kind': 'W',
3419 \ 'user_data': 'notest'
3420 \ },
3421 \ {
3422 \ 'word': 'noinfo',
3423 \ 'abbr': 'noawrd',
3424 \ 'menu': 'extra text',
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02003425 \ 'info': "lets\nshow\na\nscrollbar\nhere",
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003426 \ 'kind': 'W',
3427 \ 'user_data': 'notest'
3428 \ },
3429 \ {
3430 \ 'word': 'thatword',
3431 \ 'abbr': 'thatwrd',
3432 \ 'menu': 'extra text',
3433 \ 'info': 'that word is cool',
3434 \ 'kind': 'W',
3435 \ 'user_data': 'notest'
3436 \ },
3437 \ ]
3438 \ }
3439 endfunc
3440 call setline(1, 'text text text text text text text ')
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003441 func ChangeColor()
3442 let id = popup_findinfo()
Bram Moolenaard356fc62020-12-09 18:13:44 +01003443 if buflisted(winbufnr(id))
3444 call setline(1, 'buffer is listed')
3445 endif
Bram Moolenaar6a124e62019-09-04 18:15:19 +02003446 eval id->popup_setoptions(#{highlight: 'InfoPopup'})
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003447 endfunc
Bram Moolenaardca7abe2019-10-20 18:17:57 +02003448
3449 func InfoHidden()
3450 set completepopup=height:4,border:off,align:menu
3451 set completeopt-=popup completeopt+=popuphidden
3452 au CompleteChanged * call HandleChange()
3453 endfunc
3454
3455 let s:counter = 0
3456 func HandleChange()
3457 let s:counter += 1
3458 let selected = complete_info(['selected']).selected
3459 if selected <= 0
3460 " First time: do nothing, info remains hidden
3461 return
3462 endif
3463 if selected == 1
3464 " Second time: show info right away
3465 let id = popup_findinfo()
3466 if id
3467 call popup_settext(id, 'immediate info ' .. s:counter)
3468 call popup_show(id)
3469 endif
3470 else
3471 " Third time: show info after a short delay
3472 call timer_start(100, 'ShowInfo')
3473 endif
3474 endfunc
3475
3476 func ShowInfo(...)
3477 let id = popup_findinfo()
3478 if id
3479 call popup_settext(id, 'async info ' .. s:counter)
3480 call popup_show(id)
3481 endif
3482 endfunc
Bram Moolenaar2dfae042020-11-15 14:09:37 +01003483
Bakudankun65555002021-11-17 20:40:16 +00003484 func OpenOtherPopups()
3485 call popup_create([
3486 \ 'popup below',
3487 \ 'popup below',
3488 \ 'popup below',
3489 \ 'popup below',
3490 \ ], #{
3491 \ line: 'cursor',
3492 \ col: 'cursor+3',
3493 \ highlight: 'ErrorMsg',
3494 \ minwidth: 17,
3495 \ zindex: 50,
3496 \ })
3497 call popup_create([
3498 \ 'popup on top',
3499 \ 'popup on top',
3500 \ 'popup on top',
3501 \ ], #{
3502 \ line: 'cursor+3',
3503 \ col: 'cursor-10',
3504 \ highlight: 'Search',
3505 \ minwidth: 10,
3506 \ zindex: 200,
3507 \ })
3508 endfunc
3509
Bram Moolenaar2dfae042020-11-15 14:09:37 +01003510 " Check that no autocommands are triggered for the info popup
3511 au WinEnter * if win_gettype() == 'popup' | call setline(2, 'WinEnter') | endif
3512 au WinLeave * if win_gettype() == 'popup' | call setline(2, 'WinLeave') | endif
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003513 END
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003514 return lines
3515endfunc
3516
3517func Test_popupmenu_info_border()
3518 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003519 CheckFeature quickfix
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003520
3521 let lines = Get_popupmenu_lines()
3522 call add(lines, 'set completepopup=height:4,highlight:InfoPopup')
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003523 call writefile(lines, 'XtestInfoPopup')
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003524
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003525 let buf = RunVimInTerminal('-S XtestInfoPopup', #{rows: 14})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003526 call TermWait(buf, 25)
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003527
3528 call term_sendkeys(buf, "A\<C-X>\<C-U>")
3529 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_1', {})
3530
3531 call term_sendkeys(buf, "\<C-N>")
3532 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_2', {})
3533
3534 call term_sendkeys(buf, "\<C-N>")
3535 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_3', {})
3536
3537 call term_sendkeys(buf, "\<C-N>\<C-N>")
3538 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_4', {})
3539
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02003540 " info on the left with scrollbar
3541 call term_sendkeys(buf, "test text test text\<C-X>\<C-U>")
3542 call term_sendkeys(buf, "\<C-N>\<C-N>")
3543 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_5', {})
3544
Bram Moolenaar202c3f72019-11-21 12:12:35 +01003545 " Test that the popupmenu's scrollbar and infopopup do not overlap
3546 call term_sendkeys(buf, "\<Esc>")
3547 call term_sendkeys(buf, ":set pumheight=3\<CR>")
3548 call term_sendkeys(buf, "cc\<C-X>\<C-U>")
3549 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_6', {})
3550
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003551 " Hide the info popup, cycle through buffers, make sure it didn't get
Bram Moolenaarca7c0782020-01-14 20:42:48 +01003552 " deleted.
3553 call term_sendkeys(buf, "\<Esc>")
3554 call term_sendkeys(buf, ":set hidden\<CR>")
3555 call term_sendkeys(buf, ":bn\<CR>")
3556 call term_sendkeys(buf, ":bn\<CR>")
3557 call term_sendkeys(buf, "otest text test text\<C-X>\<C-U>")
3558 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_7', {})
3559
Bram Moolenaar447bfba2020-07-18 16:07:16 +02003560 " Test that when the option is changed the popup changes.
3561 call term_sendkeys(buf, "\<Esc>")
3562 call term_sendkeys(buf, ":set completepopup=border:off\<CR>")
3563 call term_sendkeys(buf, "a\<C-X>\<C-U>")
3564 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_8', {})
3565
Bram Moolenaar6d585f42020-07-26 22:20:54 +02003566 call term_sendkeys(buf, " \<Esc>")
3567 call term_sendkeys(buf, ":set completepopup+=width:10\<CR>")
3568 call term_sendkeys(buf, "a\<C-X>\<C-U>")
3569 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_9', {})
3570
Bram Moolenaarca7c0782020-01-14 20:42:48 +01003571 call term_sendkeys(buf, "\<Esc>")
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003572 call StopVimInTerminal(buf)
3573 call delete('XtestInfoPopup')
3574endfunc
3575
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003576func Test_popupmenu_info_noborder()
3577 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003578 CheckFeature quickfix
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003579
3580 let lines = Get_popupmenu_lines()
3581 call add(lines, 'set completepopup=height:4,border:off')
3582 call writefile(lines, 'XtestInfoPopupNb')
3583
3584 let buf = RunVimInTerminal('-S XtestInfoPopupNb', #{rows: 14})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003585 call TermWait(buf, 25)
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003586
3587 call term_sendkeys(buf, "A\<C-X>\<C-U>")
3588 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_nb_1', {})
3589
3590 call StopVimInTerminal(buf)
3591 call delete('XtestInfoPopupNb')
3592endfunc
3593
Bram Moolenaar258cef52019-08-21 17:29:29 +02003594func Test_popupmenu_info_align_menu()
3595 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003596 CheckFeature quickfix
Bram Moolenaar258cef52019-08-21 17:29:29 +02003597
3598 let lines = Get_popupmenu_lines()
3599 call add(lines, 'set completepopup=height:4,border:off,align:menu')
3600 call writefile(lines, 'XtestInfoPopupNb')
3601
3602 let buf = RunVimInTerminal('-S XtestInfoPopupNb', #{rows: 14})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003603 call TermWait(buf, 25)
Bram Moolenaar258cef52019-08-21 17:29:29 +02003604
3605 call term_sendkeys(buf, "A\<C-X>\<C-U>")
3606 call term_sendkeys(buf, "\<C-N>")
3607 call term_sendkeys(buf, "\<C-N>")
3608 call term_sendkeys(buf, "\<C-N>")
3609 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_1', {})
3610
3611 call term_sendkeys(buf, "test text test text test\<C-X>\<C-U>")
3612 call term_sendkeys(buf, "\<C-N>")
3613 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_2', {})
3614
3615 call term_sendkeys(buf, "\<Esc>")
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003616 call term_sendkeys(buf, ":call ChangeColor()\<CR>")
Bram Moolenaar258cef52019-08-21 17:29:29 +02003617 call term_sendkeys(buf, ":call setline(2, ['x']->repeat(10))\<CR>")
3618 call term_sendkeys(buf, "Gotest text test text\<C-X>\<C-U>")
3619 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_3', {})
3620
3621 call StopVimInTerminal(buf)
3622 call delete('XtestInfoPopupNb')
3623endfunc
3624
Bram Moolenaardca7abe2019-10-20 18:17:57 +02003625func Test_popupmenu_info_hidden()
3626 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003627 CheckFeature quickfix
Bram Moolenaardca7abe2019-10-20 18:17:57 +02003628
3629 let lines = Get_popupmenu_lines()
3630 call add(lines, 'call InfoHidden()')
3631 call writefile(lines, 'XtestInfoPopupHidden')
3632
3633 let buf = RunVimInTerminal('-S XtestInfoPopupHidden', #{rows: 14})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003634 call TermWait(buf, 25)
Bram Moolenaardca7abe2019-10-20 18:17:57 +02003635
3636 call term_sendkeys(buf, "A\<C-X>\<C-U>")
3637 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_hidden_1', {})
3638
3639 call term_sendkeys(buf, "\<C-N>")
3640 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_hidden_2', {})
3641
3642 call term_sendkeys(buf, "\<C-N>")
3643 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_hidden_3', {})
3644
3645 call term_sendkeys(buf, "\<Esc>")
3646 call StopVimInTerminal(buf)
3647 call delete('XtestInfoPopupHidden')
3648endfunc
3649
Bram Moolenaarbef93ac2019-12-06 20:17:35 +01003650func Test_popupmenu_info_too_wide()
3651 CheckScreendump
3652 CheckFeature quickfix
3653
3654 let lines =<< trim END
3655 call setline(1, range(10))
3656
3657 set completeopt+=preview,popup
3658 set completepopup=align:menu
3659 set omnifunc=OmniFunc
3660 hi InfoPopup ctermbg=lightgrey
3661
3662 func OmniFunc(findstart, base)
3663 if a:findstart
3664 return 0
3665 endif
3666
3667 let menuText = 'some long text to make sure the menu takes up all of the width of the window'
3668 return #{
3669 \ words: [
3670 \ #{
3671 \ word: 'scrap',
3672 \ menu: menuText,
3673 \ info: "other words are\ncooler than this and some more text\nto make wrap",
3674 \ },
3675 \ #{
3676 \ word: 'scappier',
3677 \ menu: menuText,
3678 \ info: 'words are cool',
3679 \ },
3680 \ #{
3681 \ word: 'scrappier2',
3682 \ menu: menuText,
3683 \ info: 'words are cool',
3684 \ },
3685 \ ]
3686 \ }
3687 endfunc
3688 END
3689
3690 call writefile(lines, 'XtestInfoPopupWide')
3691 let buf = RunVimInTerminal('-S XtestInfoPopupWide', #{rows: 8})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003692 call TermWait(buf, 25)
Bram Moolenaarbef93ac2019-12-06 20:17:35 +01003693
3694 call term_sendkeys(buf, "Ascr\<C-X>\<C-O>")
3695 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_wide_1', {})
3696
3697 call term_sendkeys(buf, "\<Esc>")
3698 call StopVimInTerminal(buf)
3699 call delete('XtestInfoPopupWide')
3700endfunc
3701
Bakudankun65555002021-11-17 20:40:16 +00003702func Test_popupmenu_masking()
3703 " Test that popup windows that are opened while popup menu is open are
3704 " properly displayed.
3705 CheckScreendump
3706 CheckFeature quickfix
3707
3708 let lines = Get_popupmenu_lines()
3709 call add(lines, 'inoremap <C-A> <Cmd>call OpenOtherPopups()<CR>')
3710 call writefile(lines, 'XtestPopupmenuMasking')
3711
3712 let buf = RunVimInTerminal('-S XtestPopupmenuMasking', #{rows: 14})
3713 call TermWait(buf, 25)
3714
3715 call term_sendkeys(buf, "A\<C-X>\<C-U>\<C-A>")
3716 call VerifyScreenDump(buf, 'Test_popupwin_popupmenu_masking_1', {})
3717
3718 call term_sendkeys(buf, "\<Esc>")
3719 call VerifyScreenDump(buf, 'Test_popupwin_popupmenu_masking_2', {})
3720
3721 call StopVimInTerminal(buf)
3722 call delete('XtestPopupmenuMasking')
3723endfunc
3724
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02003725func Test_popupwin_recycle_bnr()
Bram Moolenaare49fbff2019-08-21 22:50:07 +02003726 let bufnr = popup_notification('nothing wrong', {})->winbufnr()
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02003727 call popup_clear()
Bram Moolenaar6a124e62019-09-04 18:15:19 +02003728 let winid = 'nothing wrong'->popup_notification({})
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02003729 call assert_equal(bufnr, winbufnr(winid))
3730 call popup_clear()
3731endfunc
3732
Bram Moolenaar1824f452019-10-02 23:06:46 +02003733func Test_popupwin_getoptions_tablocal()
3734 topleft split
3735 let win1 = popup_create('nothing', #{maxheight: 8})
3736 let win2 = popup_create('something', #{maxheight: 10})
3737 let win3 = popup_create('something', #{maxheight: 15})
3738 call assert_equal(8, popup_getoptions(win1).maxheight)
3739 call assert_equal(10, popup_getoptions(win2).maxheight)
3740 call assert_equal(15, popup_getoptions(win3).maxheight)
3741 call popup_clear()
3742 quit
3743endfunc
3744
Bram Moolenaare8a7dfe2019-10-03 22:35:52 +02003745func Test_popupwin_cancel()
3746 let win1 = popup_create('one', #{line: 5, filter: {... -> 0}})
3747 let win2 = popup_create('two', #{line: 10, filter: {... -> 0}})
3748 let win3 = popup_create('three', #{line: 15, filter: {... -> 0}})
3749 call assert_equal(5, popup_getpos(win1).line)
3750 call assert_equal(10, popup_getpos(win2).line)
3751 call assert_equal(15, popup_getpos(win3).line)
3752 " TODO: this also works without patch 8.1.2110
3753 call feedkeys("\<C-C>", 'xt')
3754 call assert_equal(5, popup_getpos(win1).line)
3755 call assert_equal(10, popup_getpos(win2).line)
3756 call assert_equal({}, popup_getpos(win3))
3757 call feedkeys("\<C-C>", 'xt')
3758 call assert_equal(5, popup_getpos(win1).line)
3759 call assert_equal({}, popup_getpos(win2))
3760 call assert_equal({}, popup_getpos(win3))
3761 call feedkeys("\<C-C>", 'xt')
3762 call assert_equal({}, popup_getpos(win1))
3763 call assert_equal({}, popup_getpos(win2))
3764 call assert_equal({}, popup_getpos(win3))
3765endfunc
3766
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003767func Test_popupwin_filter_redraw()
3768 " Create two popups with a filter that closes the popup when typing "0".
3769 " Both popups should close, even though the redraw also calls
3770 " popup_reset_handled()
3771
3772 func CloseFilter(winid, key)
3773 if a:key == '0'
3774 call popup_close(a:winid)
3775 redraw
3776 endif
3777 return 0 " pass the key
3778 endfunc
3779
3780 let id1 = popup_create('first one', #{
3781 \ line: 1,
3782 \ col: 1,
3783 \ filter: 'CloseFilter',
3784 \ })
3785 let id2 = popup_create('second one', #{
3786 \ line: 9,
3787 \ col: 1,
3788 \ filter: 'CloseFilter',
3789 \ })
3790 call assert_equal(1, popup_getpos(id1).line)
3791 call assert_equal(9, popup_getpos(id2).line)
3792
3793 call feedkeys('0', 'xt')
3794 call assert_equal({}, popup_getpos(id1))
3795 call assert_equal({}, popup_getpos(id2))
3796
3797 call popup_clear()
3798 delfunc CloseFilter
3799endfunc
3800
Bram Moolenaar20ebbea2019-11-30 17:58:27 +01003801func Test_popupwin_double_width()
3802 CheckScreendump
3803
3804 let lines =<< trim END
3805 call setline(1, 'x你好世界你好世你好世界你好')
3806 call setline(2, '你好世界你好世你好世界你好')
3807 call setline(3, 'x你好世界你好世你好世界你好')
3808 call popup_create('你好,世界 - 你好,世界xxxxx', #{line: 1, col: 3, maxwidth: 14})
3809 END
3810 call writefile(lines, 'XtestPopupWide')
3811
3812 let buf = RunVimInTerminal('-S XtestPopupWide', #{rows: 10})
3813 call VerifyScreenDump(buf, 'Test_popupwin_doublewidth_1', {})
3814
3815 call StopVimInTerminal(buf)
3816 call delete('XtestPopupWide')
3817endfunc
3818
3819func Test_popupwin_sign()
3820 CheckScreendump
3821
3822 let lines =<< trim END
3823 call setline(1, range(10))
3824 call sign_define('Current', {
3825 \ 'text': '>>',
3826 \ 'texthl': 'WarningMsg',
3827 \ 'linehl': 'Error',
3828 \ })
3829 call sign_define('Other', {
3830 \ 'text': '#!',
3831 \ 'texthl': 'Error',
3832 \ 'linehl': 'Search',
3833 \ })
3834 let winid = popup_create(['hello', 'bright', 'world'], {
3835 \ 'minwidth': 20,
3836 \ })
3837 call setwinvar(winid, "&signcolumn", "yes")
3838 let winbufnr = winbufnr(winid)
3839
3840 " add sign to current buffer, shows
3841 call sign_place(1, 'Selected', 'Current', bufnr('%'), {'lnum': 1})
3842 " add sign to current buffer, does not show
3843 call sign_place(2, 'PopUpSelected', 'Other', bufnr('%'), {'lnum': 2})
3844
3845 " add sign to popup buffer, shows
3846 call sign_place(3, 'PopUpSelected', 'Other', winbufnr, {'lnum': 1})
3847 " add sign to popup buffer, does not show
3848 call sign_place(4, 'Selected', 'Current', winbufnr, {'lnum': 2})
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02003849
3850 func SetOptions()
3851 call setwinvar(g:winid, '&number', 1)
3852 call setwinvar(g:winid, '&foldcolumn', 2)
3853 call popup_settext(g:winid, 'a longer line to check the width')
3854 endfunc
Bram Moolenaar20ebbea2019-11-30 17:58:27 +01003855 END
3856 call writefile(lines, 'XtestPopupSign')
3857
3858 let buf = RunVimInTerminal('-S XtestPopupSign', #{rows: 10})
3859 call VerifyScreenDump(buf, 'Test_popupwin_sign_1', {})
3860
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02003861 " set more options to check the width is adjusted
3862 call term_sendkeys(buf, ":call SetOptions()\<CR>")
3863 call VerifyScreenDump(buf, 'Test_popupwin_sign_2', {})
3864
Bram Moolenaar20ebbea2019-11-30 17:58:27 +01003865 call StopVimInTerminal(buf)
3866 call delete('XtestPopupSign')
3867endfunc
3868
Bram Moolenaar99ebf222019-12-10 23:44:48 +01003869func Test_popupwin_bufnr()
3870 let popwin = popup_create(['blah'], #{})
3871 let popbuf = winbufnr(popwin)
3872 split asdfasdf
3873 let newbuf = bufnr()
3874 call assert_true(newbuf > popbuf, 'New buffer number is higher')
3875 call assert_equal(newbuf, bufnr('$'))
3876 call popup_clear()
3877 let popwin = popup_create(['blah'], #{})
3878 " reuses previous buffer number
3879 call assert_equal(popbuf, winbufnr(popwin))
3880 call assert_equal(newbuf, bufnr('$'))
3881
3882 call popup_clear()
3883 bwipe!
3884endfunc
3885
Bram Moolenaarec084d32020-02-28 22:44:47 +01003886func Test_popupwin_filter_input_multibyte()
3887 func MyPopupFilter(winid, c)
3888 let g:bytes = range(a:c->strlen())->map({i -> char2nr(a:c[i])})
3889 return 0
3890 endfunc
3891 let winid = popup_create('', #{mapping: 0, filter: 'MyPopupFilter'})
3892
3893 " UTF-8: E3 80 80, including K_SPECIAL(0x80)
3894 call feedkeys("\u3000", 'xt')
3895 call assert_equal([0xe3, 0x80, 0x80], g:bytes)
3896
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01003897 " UTF-8: E3 80 9B, including CSI(0x9B)
3898 call feedkeys("\u301b", 'xt')
3899 call assert_equal([0xe3, 0x80, 0x9b], g:bytes)
Bram Moolenaarec084d32020-02-28 22:44:47 +01003900
Bram Moolenaar20298ce2020-06-19 21:46:52 +02003901 if has('unix')
3902 " with modifyOtherKeys <M-S-a> does not include a modifier sequence
Bram Moolenaarb326edf2020-06-20 15:03:38 +02003903 if has('gui_running')
3904 call feedkeys("\x9b\xfc\x08A", 'Lx!')
3905 else
3906 call feedkeys("\<Esc>[27;4;65~", 'Lx!')
3907 endif
Bram Moolenaar20298ce2020-06-19 21:46:52 +02003908 call assert_equal([0xc3, 0x81], g:bytes)
3909 endif
3910
Bram Moolenaarec084d32020-02-28 22:44:47 +01003911 call popup_clear()
3912 delfunc MyPopupFilter
3913 unlet g:bytes
3914endfunc
3915
Bram Moolenaar6f8f7332020-08-10 21:19:23 +02003916func Test_popupwin_filter_close_ctrl_c()
3917 CheckScreendump
3918
3919 let lines =<< trim END
3920 vsplit
3921 set laststatus=2
3922 set statusline=%!Statusline()
3923
3924 function Statusline() abort
3925 return '%<%f %h%m%r%=%-14.(%l,%c%V%) %P'
3926 endfunction
3927
3928 call popup_create('test test test test...', {'filter': {-> 0}})
3929 END
3930 call writefile(lines, 'XtestPopupCtrlC')
3931
3932 let buf = RunVimInTerminal('-S XtestPopupCtrlC', #{rows: 10})
3933
3934 call term_sendkeys(buf, "\<C-C>")
3935 call VerifyScreenDump(buf, 'Test_popupwin_ctrl_c', {})
3936
3937 call StopVimInTerminal(buf)
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003938 call delete('XtestPopupCtrlC')
3939endfunc
3940
3941func Test_popupwin_filter_close_wrong_name()
3942 CheckScreendump
3943
3944 let lines =<< trim END
3945 call popup_create('one two three...', {'filter': 'NoSuchFunc'})
3946 END
3947 call writefile(lines, 'XtestPopupWrongName')
3948
3949 let buf = RunVimInTerminal('-S XtestPopupWrongName', #{rows: 10})
3950
3951 call term_sendkeys(buf, "j")
3952 call VerifyScreenDump(buf, 'Test_popupwin_wrong_name', {})
3953
3954 call StopVimInTerminal(buf)
3955 call delete('XtestPopupWrongName')
3956endfunc
3957
3958func Test_popupwin_filter_close_three_errors()
3959 CheckScreendump
3960
3961 let lines =<< trim END
3962 set cmdheight=2
3963 call popup_create('one two three...', {'filter': 'filter'})
3964 END
3965 call writefile(lines, 'XtestPopupThreeErrors')
3966
3967 let buf = RunVimInTerminal('-S XtestPopupThreeErrors', #{rows: 10})
3968
3969 call term_sendkeys(buf, "jj")
3970 call VerifyScreenDump(buf, 'Test_popupwin_three_errors_1', {})
3971 call term_sendkeys(buf, "j")
3972 call VerifyScreenDump(buf, 'Test_popupwin_three_errors_2', {})
3973
3974 call StopVimInTerminal(buf)
3975 call delete('XtestPopupThreeErrors')
Bram Moolenaar6f8f7332020-08-10 21:19:23 +02003976endfunc
3977
Bram Moolenaar927495b2020-11-06 17:58:35 +01003978func Test_popupwin_latin1_encoding()
3979 CheckScreendump
3980 CheckUnix
3981
3982 " When 'encoding' is a single-byte encoding a terminal window will mess up
3983 " the display. Check that showing a popup on top of that doesn't crash.
3984 let lines =<< trim END
3985 set encoding=latin1
3986 terminal cat Xmultibyte
3987 call popup_create(['one', 'two', 'three', 'four'], #{line: 1, col: 10})
Bram Moolenaar4c5bdb92020-12-17 17:45:59 +01003988 redraw
Bram Moolenaarb125b532020-12-17 21:56:09 +01003989 " wait for "cat" to finish
3990 while execute('ls!') !~ 'finished'
3991 sleep 10m
3992 endwhile
Bram Moolenaar4c5bdb92020-12-17 17:45:59 +01003993 echo "Done"
Bram Moolenaar927495b2020-11-06 17:58:35 +01003994 END
3995 call writefile(lines, 'XtestPopupLatin')
3996 call writefile([repeat("\u3042 ", 120)], 'Xmultibyte')
3997
3998 let buf = RunVimInTerminal('-S XtestPopupLatin', #{rows: 10})
Bram Moolenaar4c5bdb92020-12-17 17:45:59 +01003999 call WaitForAssert({-> assert_match('Done', term_getline(buf, 10))})
Bram Moolenaar927495b2020-11-06 17:58:35 +01004000
4001 call term_sendkeys(buf, ":q\<CR>")
4002 call StopVimInTerminal(buf)
4003 call delete('XtestPopupLatin')
4004 call delete('Xmultibyte')
4005endfunc
4006
Bram Moolenaarba2920f2020-03-06 21:43:17 +01004007func Test_popupwin_atcursor_far_right()
4008 new
4009
4010 " this was getting stuck
4011 set signcolumn=yes
4012 call setline(1, repeat('=', &columns))
4013 normal! ggg$
Bram Moolenaaref6b9792020-05-13 16:34:15 +02004014 let winid = popup_atcursor(repeat('x', 500), #{moved: 'any', border: []})
Bram Moolenaarba2920f2020-03-06 21:43:17 +01004015
Bram Moolenaar6d585f42020-07-26 22:20:54 +02004016 " 'signcolumn' was getting reset
4017 call setwinvar(winid, '&signcolumn', 'yes')
4018 call popup_setoptions(winid, {'zindex': 1000})
4019 call assert_equal('yes', getwinvar(winid, '&signcolumn'))
4020
Bram Moolenaaref6b9792020-05-13 16:34:15 +02004021 call popup_close(winid)
Bram Moolenaarba2920f2020-03-06 21:43:17 +01004022 bwipe!
4023 set signcolumn&
4024endfunc
4025
Bram Moolenaar0f1563f2020-03-20 21:15:51 +01004026func Test_popupwin_splitmove()
4027 vsplit
4028 let win2 = win_getid()
4029 let popup_winid = popup_dialog('hello', {})
4030 call assert_fails('call win_splitmove(popup_winid, win2)', 'E957:')
4031 call assert_fails('call win_splitmove(win2, popup_winid)', 'E957:')
4032
4033 call popup_clear()
4034 bwipe
4035endfunc
4036
Bram Moolenaarcbcd9cb2020-11-07 16:58:59 +01004037func Test_popupwin_exiting_terminal()
4038 CheckFeature terminal
4039
4040 " Tests that when creating a popup right after closing a terminal window does
4041 " not make the popup the current window.
4042 let winid = win_getid()
4043 try
4044 augroup Test_popupwin_exiting_terminal
4045 autocmd!
4046 autocmd WinEnter * :call popup_create('test', {})
4047 augroup END
4048 let bnr = term_start(&shell, #{term_finish: 'close'})
4049 call term_sendkeys(bnr, "exit\r\n")
4050 call WaitForAssert({-> assert_equal(winid, win_getid())})
4051 finally
4052 call popup_clear(1)
4053 augroup Test_popupwin_exiting_terminal
4054 autocmd!
4055 augroup END
4056 endtry
4057endfunc
Bram Moolenaar0f1563f2020-03-20 21:15:51 +01004058
Bram Moolenaar014f6982021-01-04 13:18:30 +01004059func Test_popup_filter_menu()
4060 let colors = ['red', 'green', 'blue']
4061 call popup_menu(colors, #{callback: {_, result -> assert_equal('green', colors[result - 1])}})
4062 call feedkeys("\<c-n>\<c-n>\<c-p>\<cr>", 'xt')
4063endfunc
4064
Bram Moolenaar65026432021-02-06 14:22:32 +01004065func Test_popup_getoptions_other_tab()
4066 new
4067 call setline(1, 'some text')
4068 call prop_type_add('textprop', {})
4069 call prop_add(1, 1, #{type: 'textprop', length: 1})
4070 let id = popup_create('TEST', #{textprop: 'textprop', highlight: 'ErrorMsg', tabpage: 1})
4071 tab sp
4072 call assert_equal(['textprop', 'textpropid', 'textpropwin'], popup_getoptions(id)->keys()->filter({_, v -> v =~ 'textprop'}))
4073
4074 tabclose
Bram Moolenaarefa19232021-02-06 14:59:27 +01004075 call popup_close(id)
Bram Moolenaar65026432021-02-06 14:22:32 +01004076 bwipe!
4077 call prop_type_delete('textprop')
4078endfunc
4079
Bram Moolenaarefa19232021-02-06 14:59:27 +01004080
4081func Test_popup_setoptions_other_tab()
4082 new Xfile
4083 let winid = win_getid()
4084 call setline(1, 'some text')
4085 call prop_type_add('textprop', {})
4086 call prop_add(1, 1, #{type: 'textprop', length: 1})
4087 let id = popup_create('TEST', #{textprop: 'textprop'})
4088 tab sp
4089 call popup_setoptions(id, #{textprop: 'textprop', textpropwin: winid})
4090 call assert_equal(winid, popup_getoptions(id).textpropwin)
4091
4092 tabclose
4093 call popup_close(id)
4094 bwipe! Xfile
4095 call prop_type_delete('textprop')
4096endfunc
4097
Bram Moolenaar82db31c2021-02-10 14:56:11 +01004098func Test_popup_prop_not_visible()
4099 CheckScreendump
4100
4101 let lines =<< trim END
4102 vim9script
4103 set nowrap stal=2
4104 rightbelow :31vnew
4105 setline(1, ['', 'some text', '', 'other text'])
4106 prop_type_add('someprop', {})
4107 prop_add(2, 9, {type: 'someprop', length: 5})
Bram Moolenaar27724252022-05-08 15:00:04 +01004108 g:some_id = popup_create('attached to "some"', {
Bram Moolenaar82db31c2021-02-10 14:56:11 +01004109 textprop: 'someprop',
4110 highlight: 'ErrorMsg',
4111 line: -1,
4112 wrap: false,
4113 fixed: true,
4114 })
4115 prop_type_add('otherprop', {})
4116 prop_add(4, 10, {type: 'otherprop', length: 5})
4117 popup_create('attached to "other"', {
4118 textprop: 'otherprop',
4119 highlight: 'ErrorMsg',
4120 line: -1,
4121 wrap: false,
4122 fixed: false,
4123 })
4124 END
4125 call writefile(lines, 'XtestPropNotVisble')
4126 let buf = RunVimInTerminal('-S XtestPropNotVisble', #{rows: 10})
4127 call VerifyScreenDump(buf, 'Test_popup_prop_not_visible_01', {})
4128
Bram Moolenaar27724252022-05-08 15:00:04 +01004129 " check that hiding and unhiding the popup works
4130 call term_sendkeys(buf, ":call popup_hide(g:some_id)\<CR>")
4131 call VerifyScreenDump(buf, 'Test_popup_prop_not_visible_01a', {})
4132 call term_sendkeys(buf, ":call popup_show(g:some_id)\<CR>")
4133 call VerifyScreenDump(buf, 'Test_popup_prop_not_visible_01b', {})
4134
Bram Moolenaar82db31c2021-02-10 14:56:11 +01004135 call term_sendkeys(buf, ":vert resize -14\<CR>")
4136 call VerifyScreenDump(buf, 'Test_popup_prop_not_visible_02', {})
4137
4138 call term_sendkeys(buf, ":vert resize -8\<CR>")
4139 call VerifyScreenDump(buf, 'Test_popup_prop_not_visible_03', {})
4140
4141 " clean up
4142 call StopVimInTerminal(buf)
4143 call delete('XtestPropNotVisble')
4144endfunction
4145
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02004146func Test_bufdel_skips_popupwin_buffer()
4147 let id = popup_create("Some text", {})
4148 %bd
4149 call popup_close(id)
4150endfunc
4151
Bram Moolenaar82db31c2021-02-10 14:56:11 +01004152
Bram Moolenaar331bafd2019-07-20 17:46:05 +02004153" vim: shiftwidth=2 sts=2