blob: 23f318fd6ef660aebbc1a45d1ba9250792f4b90b [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
Bram Moolenaar9f145572022-11-27 12:45:41 +00007source term_util.vim
Bram Moolenaar4d784b22019-05-25 19:51:39 +02008
9func Test_simple_popup()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +020010 CheckScreendump
11
Bram Moolenaare7eb9272019-06-24 00:58:07 +020012 let lines =<< trim END
13 call setline(1, range(1, 100))
14 hi PopupColor1 ctermbg=lightblue
15 hi PopupColor2 ctermbg=lightcyan
Bram Moolenaar1666ac92019-11-10 17:22:31 +010016 hi EndOfBuffer ctermbg=lightgrey
Bram Moolenaare7eb9272019-06-24 00:58:07 +020017 hi Comment ctermfg=red
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020018 call prop_type_add('comment', #{highlight: 'Comment'})
19 let winid = popup_create('hello there', #{line: 3, col: 11, minwidth: 20, highlight: 'PopupColor1'})
20 let winid2 = popup_create(['another one', 'another two', 'another three'], #{line: 3, col: 25, minwidth: 20})
Bram Moolenaare7eb9272019-06-24 00:58:07 +020021 call setwinvar(winid2, '&wincolor', 'PopupColor2')
22 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +010023 call writefile(lines, 'XtestPopup', 'D')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020024 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar4d784b22019-05-25 19:51:39 +020025 call VerifyScreenDump(buf, 'Test_popupwin_01', {})
26
Bram Moolenaarec583842019-05-26 14:11:23 +020027 " Add a tabpage
28 call term_sendkeys(buf, ":tabnew\<CR>")
Bram Moolenaar60cdb302019-05-27 21:54:10 +020029 call term_sendkeys(buf, ":let popupwin = popup_create(["
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020030 \ .. "#{text: 'other tab'},"
31 \ .. "#{text: 'a comment line', props: [#{"
Bram Moolenaard5abb4c2019-07-13 22:46:10 +020032 \ .. "col: 3, length: 7, minwidth: 20, type: 'comment'"
Bram Moolenaar7a8d0272019-05-26 23:32:06 +020033 \ .. "}]},"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020034 \ .. "], #{line: 4, col: 9, minwidth: 20})\<CR>")
Bram Moolenaarec583842019-05-26 14:11:23 +020035 call VerifyScreenDump(buf, 'Test_popupwin_02', {})
36
37 " switch back to first tabpage
38 call term_sendkeys(buf, "gt")
39 call VerifyScreenDump(buf, 'Test_popupwin_03', {})
40
41 " close that tabpage
42 call term_sendkeys(buf, ":quit!\<CR>")
43 call VerifyScreenDump(buf, 'Test_popupwin_04', {})
44
Bram Moolenaar202d9822019-06-11 21:56:30 +020045 " set 'columns' to a small value, size must be recomputed
46 call term_sendkeys(buf, ":let cols = &columns\<CR>")
47 call term_sendkeys(buf, ":set columns=12\<CR>")
48 call VerifyScreenDump(buf, 'Test_popupwin_04a', {})
49 call term_sendkeys(buf, ":let &columns = cols\<CR>")
50
Bram Moolenaar17146962019-05-30 00:12:11 +020051 " resize popup, show empty line at bottom
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020052 call term_sendkeys(buf, ":call popup_move(popupwin, #{minwidth: 15, maxwidth: 25, minheight: 3, maxheight: 5})\<CR>")
Bram Moolenaar60cdb302019-05-27 21:54:10 +020053 call term_sendkeys(buf, ":redraw\<CR>")
54 call VerifyScreenDump(buf, 'Test_popupwin_05', {})
55
Bram Moolenaar17146962019-05-30 00:12:11 +020056 " show not fitting line at bottom
57 call term_sendkeys(buf, ":call setbufline(winbufnr(popupwin), 3, 'this line will not fit here')\<CR>")
58 call term_sendkeys(buf, ":redraw\<CR>")
59 call VerifyScreenDump(buf, 'Test_popupwin_06', {})
60
Bram Moolenaar24a5ac52019-06-08 19:01:18 +020061 " move popup over ruler
62 call term_sendkeys(buf, ":set cmdheight=2\<CR>")
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020063 call term_sendkeys(buf, ":call popup_move(popupwin, #{line: 7, col: 55})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +020064 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar24a5ac52019-06-08 19:01:18 +020065 call VerifyScreenDump(buf, 'Test_popupwin_07', {})
66
67 " clear all popups after moving the cursor a bit, so that ruler is updated
68 call term_sendkeys(buf, "axxx\<Esc>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +020069 call TermWait(buf)
Bram Moolenaar24a5ac52019-06-08 19:01:18 +020070 call term_sendkeys(buf, "0")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +020071 call TermWait(buf)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +020072 call term_sendkeys(buf, ":call popup_clear()\<CR>")
Bram Moolenaar24a5ac52019-06-08 19:01:18 +020073 call VerifyScreenDump(buf, 'Test_popupwin_08', {})
74
Bram Moolenaar4d784b22019-05-25 19:51:39 +020075 " clean up
76 call StopVimInTerminal(buf)
Bram Moolenaar4d784b22019-05-25 19:51:39 +020077endfunc
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')
Bram Moolenaar145d1fd2022-09-30 21:57:11 +010098 call writefile(lines, 'XtestPopupBorder', 'D')
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)
Bram Moolenaar3bfd04e2019-06-01 20:45:21 +0200103 endfor
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200104
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200105 let lines =<< trim END
106 call setline(1, range(1, 100))
107 hi BlueColor ctermbg=lightblue
108 hi TopColor ctermbg=253
109 hi RightColor ctermbg=245
110 hi BottomColor ctermbg=240
111 hi LeftColor ctermbg=248
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200112 call popup_create('hello border', #{line: 2, col: 3, border: [], borderhighlight: ['BlueColor']})
113 call popup_create(['hello border', 'and more'], #{line: 2, col: 23, border: [], borderhighlight: ['TopColor', 'RightColor', 'BottomColor', 'LeftColor']})
114 call popup_create(['hello border', 'lines only'], #{line: 2, col: 43, border: [], borderhighlight: ['BlueColor'], borderchars: ['x']})
115 call popup_create(['hello border', 'with corners'], #{line: 2, col: 60, border: [], borderhighlight: ['BlueColor'], borderchars: ['x', '#']})
116 let winid = popup_create(['hello border', 'with numbers'], #{line: 6, col: 3, border: [], borderhighlight: ['BlueColor'], borderchars: ['0', '1', '2', '3', '4', '5', '6', '7']})
117 call popup_create(['hello border', 'just blanks'], #{line: 7, col: 23, border: [], borderhighlight: ['BlueColor'], borderchars: [' ']})
Bram Moolenaar3dabd712019-07-08 23:30:22 +0200118 func MultiByte()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200119 call popup_create(['hello'], #{line: 8, col: 43, border: [], borderchars: ['─', '│', '─', '│', '┌', '┐', '┘', '└']})
Bram Moolenaar3dabd712019-07-08 23:30:22 +0200120 endfunc
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200121 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +0100122 call writefile(lines, 'XtestPopupBorder', 'D')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200123 let buf = RunVimInTerminal('-S XtestPopupBorder', #{rows: 12})
Bram Moolenaar790498b2019-06-01 22:15:29 +0200124 call VerifyScreenDump(buf, 'Test_popupwin_22', {})
125
Bram Moolenaarad24a712019-06-17 20:05:45 +0200126 " check that changing borderchars triggers a redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200127 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 +0200128 call VerifyScreenDump(buf, 'Test_popupwin_23', {})
129
Bram Moolenaar3dabd712019-07-08 23:30:22 +0200130 " check multi-byte border only with 'ambiwidth' single
131 if &ambiwidth == 'single'
132 call term_sendkeys(buf, ":call MultiByte()\<CR>")
133 call VerifyScreenDump(buf, 'Test_popupwin_24', {})
134 endif
135
Bram Moolenaar790498b2019-06-01 22:15:29 +0200136 call StopVimInTerminal(buf)
Bram Moolenaar790498b2019-06-01 22:15:29 +0200137
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200138 let with_border_or_padding = #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200139 \ line: 2,
140 \ core_line: 3,
141 \ col: 3,
142 \ core_col: 4,
143 \ width: 14,
144 \ core_width: 12,
145 \ height: 3,
146 \ core_height: 1,
147 \ firstline: 1,
Bram Moolenaar30efcf32019-11-03 22:29:38 +0100148 \ lastline: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200149 \ scrollbar: 0,
150 \ visible: 1}
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200151 let winid = popup_create('hello border', #{line: 2, col: 3, border: []})",
Bram Moolenaar6a124e62019-09-04 18:15:19 +0200152 call assert_equal(with_border_or_padding, winid->popup_getpos())
Bram Moolenaarae943152019-06-16 22:54:14 +0200153 let options = popup_getoptions(winid)
154 call assert_equal([], options.border)
155 call assert_false(has_key(options, "padding"))
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200156
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200157 let winid = popup_create('hello padding', #{line: 2, col: 3, padding: []})
Bram Moolenaarae943152019-06-16 22:54:14 +0200158 let with_border_or_padding.width = 15
159 let with_border_or_padding.core_width = 13
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200160 call assert_equal(with_border_or_padding, popup_getpos(winid))
Bram Moolenaarae943152019-06-16 22:54:14 +0200161 let options = popup_getoptions(winid)
162 call assert_false(has_key(options, "border"))
163 call assert_equal([], options.padding)
164
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200165 call popup_setoptions(winid, #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200166 \ padding: [1, 2, 3, 4],
167 \ border: [4, 0, 7, 8],
168 \ borderhighlight: ['Top', 'Right', 'Bottom', 'Left'],
169 \ borderchars: ['1', '^', '2', '>', '3', 'v', '4', '<'],
Bram Moolenaarae943152019-06-16 22:54:14 +0200170 \ })
171 let options = popup_getoptions(winid)
172 call assert_equal([1, 0, 1, 1], options.border)
173 call assert_equal([1, 2, 3, 4], options.padding)
174 call assert_equal(['Top', 'Right', 'Bottom', 'Left'], options.borderhighlight)
175 call assert_equal(['1', '^', '2', '>', '3', 'v', '4', '<'], options.borderchars)
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200176
Bram Moolenaara1396152019-10-20 18:46:05 +0200177 " Check that popup_setoptions() takes the output of popup_getoptions()
178 call popup_setoptions(winid, options)
179 call assert_equal(options, popup_getoptions(winid))
180
Bram Moolenaarb0992022020-01-30 14:55:42 +0100181 " Check that range() doesn't crash
182 call popup_setoptions(winid, #{
183 \ padding: range(1, 4),
184 \ border: range(5, 8),
185 \ borderhighlight: range(4),
186 \ borderchars: range(8),
187 \ })
188
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200189 let winid = popup_create('hello both', #{line: 3, col: 8, border: [], padding: []})
190 call assert_equal(#{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200191 \ line: 3,
192 \ core_line: 5,
193 \ col: 8,
194 \ core_col: 10,
195 \ width: 14,
196 \ core_width: 10,
197 \ height: 5,
198 \ scrollbar: 0,
199 \ core_height: 1,
200 \ firstline: 1,
Bram Moolenaar30efcf32019-11-03 22:29:38 +0100201 \ lastline: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200202 \ visible: 1}, popup_getpos(winid))
Bram Moolenaarae943152019-06-16 22:54:14 +0200203
204 call popup_clear()
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200205endfunc
206
Bram Moolenaarb4230122019-05-30 18:40:53 +0200207func Test_popup_with_syntax_win_execute()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200208 CheckScreendump
209
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200210 let lines =<< trim END
211 call setline(1, range(1, 100))
212 hi PopupColor ctermbg=lightblue
213 let winid = popup_create([
214 \ '#include <stdio.h>',
215 \ 'int main(void)',
216 \ '{',
217 \ ' printf(123);',
218 \ '}',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200219 \], #{line: 3, col: 25, highlight: 'PopupColor'})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200220 call win_execute(winid, 'set syntax=cpp')
221 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +0100222 call writefile(lines, 'XtestPopup', 'D')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200223 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaarb4230122019-05-30 18:40:53 +0200224 call VerifyScreenDump(buf, 'Test_popupwin_10', {})
225
226 " clean up
227 call StopVimInTerminal(buf)
Bram Moolenaarb4230122019-05-30 18:40:53 +0200228endfunc
229
230func Test_popup_with_syntax_setbufvar()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200231 CheckScreendump
232
Bram Moolenaar402502d2019-05-30 22:07:36 +0200233 let lines =<< trim END
234 call setline(1, range(1, 100))
235 hi PopupColor ctermbg=lightgrey
236 let winid = popup_create([
237 \ '#include <stdio.h>',
238 \ 'int main(void)',
239 \ '{',
Bram Moolenaare089c3f2019-07-09 20:25:25 +0200240 \ "\tprintf(567);",
Bram Moolenaar402502d2019-05-30 22:07:36 +0200241 \ '}',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200242 \], #{line: 3, col: 21, highlight: 'PopupColor'})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200243 call setbufvar(winbufnr(winid), '&syntax', 'cpp')
244 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +0100245 call writefile(lines, 'XtestPopup', 'D')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200246 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaarb4230122019-05-30 18:40:53 +0200247 call VerifyScreenDump(buf, 'Test_popupwin_11', {})
248
249 " clean up
250 call StopVimInTerminal(buf)
Bram Moolenaarb4230122019-05-30 18:40:53 +0200251endfunc
252
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200253func Test_popup_with_matches()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200254 CheckScreendump
255
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200256 let lines =<< trim END
257 call setline(1, ['111 222 333', '444 555 666'])
258 let winid = popup_create([
259 \ '111 222 333',
260 \ '444 555 666',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200261 \], #{line: 3, col: 10, border: []})
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200262 set hlsearch
Bram Moolenaar024dbd22019-11-02 22:00:15 +0100263 hi VeryBlue ctermfg=blue guifg=blue
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200264 /666
265 call matchadd('ErrorMsg', '111')
Bram Moolenaar024dbd22019-11-02 22:00:15 +0100266 call matchadd('VeryBlue', '444')
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200267 call win_execute(winid, "call matchadd('ErrorMsg', '111')")
Bram Moolenaar024dbd22019-11-02 22:00:15 +0100268 call win_execute(winid, "call matchadd('VeryBlue', '555')")
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200269 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +0100270 call writefile(lines, 'XtestPopupMatches', 'D')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200271 let buf = RunVimInTerminal('-S XtestPopupMatches', #{rows: 10})
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200272 call VerifyScreenDump(buf, 'Test_popupwin_matches', {})
273
274 " clean up
275 call StopVimInTerminal(buf)
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200276endfunc
277
Bram Moolenaar399d8982019-06-02 15:34:29 +0200278func Test_popup_all_corners()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200279 CheckScreendump
280
Bram Moolenaar399d8982019-06-02 15:34:29 +0200281 let lines =<< trim END
282 call setline(1, repeat([repeat('-', 60)], 15))
283 set so=0
284 normal 2G3|r#
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200285 let winid1 = popup_create(['first', 'second'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200286 \ line: 'cursor+1',
287 \ col: 'cursor',
288 \ pos: 'topleft',
289 \ border: [],
290 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200291 \ })
Bram Moolenaarb754b5b2019-10-24 19:25:00 +0200292 normal 24|r@
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200293 let winid1 = popup_create(['First', 'SeconD'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200294 \ line: 'cursor+1',
295 \ col: 'cursor',
296 \ pos: 'topright',
297 \ border: [],
298 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200299 \ })
Bram Moolenaarb754b5b2019-10-24 19:25:00 +0200300 normal 9G27|r%
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200301 let winid1 = popup_create(['fiRSt', 'seCOnd'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200302 \ line: 'cursor-1',
303 \ col: 'cursor',
304 \ pos: 'botleft',
305 \ border: [],
306 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200307 \ })
Bram Moolenaarb754b5b2019-10-24 19:25:00 +0200308 normal 48|r&
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200309 let winid1 = popup_create(['FIrsT', 'SEcoND'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200310 \ line: 'cursor-1',
311 \ col: 'cursor',
312 \ pos: 'botright',
313 \ border: [],
314 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200315 \ })
Bram Moolenaarb754b5b2019-10-24 19:25:00 +0200316 normal 1G51|r*
317 let winid1 = popup_create(['one', 'two'], #{
318 \ line: 'cursor-1',
319 \ col: 'cursor',
320 \ pos: 'botleft',
321 \ border: [],
322 \ padding: [],
323 \ })
Bram Moolenaar399d8982019-06-02 15:34:29 +0200324 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +0100325 call writefile(lines, 'XtestPopupCorners', 'D')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200326 let buf = RunVimInTerminal('-S XtestPopupCorners', #{rows: 12})
Bram Moolenaar399d8982019-06-02 15:34:29 +0200327 call VerifyScreenDump(buf, 'Test_popupwin_corners', {})
328
329 " clean up
330 call StopVimInTerminal(buf)
Bram Moolenaar399d8982019-06-02 15:34:29 +0200331endfunc
332
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100333func Test_popup_nospace()
334 CheckScreendump
335
336 let lines =<< trim END
337 call setline(1, repeat([repeat('-', 60)], 15))
338 set so=0
339
340 " cursor in a line in top half, using "botleft" with popup that
341 " does fit
342 normal 5G2|r@
343 let winid1 = popup_create(['one', 'two'], #{
344 \ line: 'cursor-1',
345 \ col: 'cursor',
346 \ pos: 'botleft',
347 \ border: [],
348 \ })
349 " cursor in a line in top half, using "botleft" with popup that
350 " doesn't fit: gets truncated
351 normal 5G9|r#
352 let winid1 = popup_create(['one', 'two', 'tee'], #{
353 \ line: 'cursor-1',
354 \ col: 'cursor',
355 \ pos: 'botleft',
356 \ posinvert: 0,
357 \ border: [],
358 \ })
359 " cursor in a line in top half, using "botleft" with popup that
360 " doesn't fit and 'posinvert' set: flips to below.
361 normal 5G16|r%
362 let winid1 = popup_create(['one', 'two', 'tee'], #{
363 \ line: 'cursor-1',
364 \ col: 'cursor',
365 \ pos: 'botleft',
366 \ border: [],
367 \ })
368 " cursor in a line in bottom half, using "botleft" with popup that
369 " doesn't fit: does not flip.
370 normal 8G23|r*
371 let winid1 = popup_create(['aaa', 'bbb', 'ccc', 'ddd', 'eee', 'fff'], #{
372 \ line: 'cursor-1',
373 \ col: 'cursor',
374 \ pos: 'botleft',
375 \ border: [],
376 \ })
377
378 " cursor in a line in bottom half, using "topleft" with popup that
379 " does fit
380 normal 8G30|r@
381 let winid1 = popup_create(['one', 'two'], #{
382 \ line: 'cursor+1',
383 \ col: 'cursor',
384 \ pos: 'topleft',
385 \ border: [],
386 \ })
387 " cursor in a line in top half, using "topleft" with popup that
388 " doesn't fit: truncated
389 normal 8G37|r#
390 let winid1 = popup_create(['one', 'two', 'tee'], #{
391 \ line: 'cursor+1',
392 \ col: 'cursor',
393 \ pos: 'topleft',
394 \ posinvert: 0,
395 \ border: [],
396 \ })
397 " cursor in a line in top half, using "topleft" with popup that
Bram Moolenaar5c6b6182019-11-10 17:51:38 +0100398 " doesn't fit and "posinvert" set: flips to above.
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100399 normal 8G44|r%
Bram Moolenaar5c6b6182019-11-10 17:51:38 +0100400 let winid1 = popup_create(['one', 'two', 'tee', 'fou', 'fiv'], #{
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100401 \ line: 'cursor+1',
402 \ col: 'cursor',
403 \ pos: 'topleft',
404 \ border: [],
405 \ })
406 " cursor in a line in top half, using "topleft" with popup that
407 " doesn't fit: does not flip.
408 normal 5G51|r*
409 let winid1 = popup_create(['aaa', 'bbb', 'ccc', 'ddd', 'eee', 'fff'], #{
410 \ line: 'cursor+1',
411 \ col: 'cursor',
412 \ pos: 'topleft',
413 \ border: [],
414 \ })
415 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +0100416 call writefile(lines, 'XtestPopupNospace', 'D')
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100417 let buf = RunVimInTerminal('-S XtestPopupNospace', #{rows: 12})
418 call VerifyScreenDump(buf, 'Test_popupwin_nospace', {})
419
420 " clean up
421 call StopVimInTerminal(buf)
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100422endfunc
423
Bram Moolenaaref6b9792020-05-13 16:34:15 +0200424func Test_popup_firstline_dump()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200425 CheckScreendump
426
Bram Moolenaar8d241042019-06-12 23:40:01 +0200427 let lines =<< trim END
428 call setline(1, range(1, 20))
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200429 let winid = popup_create(['1111', '222222', '33333', '44', '5', '666666', '77777', '888', '9999999999999999'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200430 \ maxheight: 4,
431 \ firstline: 3,
Bram Moolenaar8d241042019-06-12 23:40:01 +0200432 \ })
433 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +0100434 call writefile(lines, 'XtestPopupFirstline', 'D')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200435 let buf = RunVimInTerminal('-S XtestPopupFirstline', #{rows: 10})
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200436 call VerifyScreenDump(buf, 'Test_popupwin_firstline_1', {})
437
438 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: -1})\<CR>")
439 call term_sendkeys(buf, ":\<CR>")
440 call VerifyScreenDump(buf, 'Test_popupwin_firstline_2', {})
Bram Moolenaar8d241042019-06-12 23:40:01 +0200441
442 " clean up
443 call StopVimInTerminal(buf)
Bram Moolenaaref6b9792020-05-13 16:34:15 +0200444endfunc
Bram Moolenaarae943152019-06-16 22:54:14 +0200445
Bram Moolenaaref6b9792020-05-13 16:34:15 +0200446func Test_popup_firstline()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200447 let winid = popup_create(['1111', '222222', '33333', '44444'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200448 \ maxheight: 2,
449 \ firstline: 3,
Bram Moolenaarae943152019-06-16 22:54:14 +0200450 \ })
451 call assert_equal(3, popup_getoptions(winid).firstline)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200452 call popup_setoptions(winid, #{firstline: 1})
Bram Moolenaarae943152019-06-16 22:54:14 +0200453 call assert_equal(1, popup_getoptions(winid).firstline)
Bram Moolenaar6a124e62019-09-04 18:15:19 +0200454 eval winid->popup_close()
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +0200455
456 let winid = popup_create(['xxx']->repeat(50), #{
457 \ maxheight: 3,
458 \ firstline: 11,
459 \ })
460 redraw
461 call assert_equal(11, popup_getoptions(winid).firstline)
462 call assert_equal(11, popup_getpos(winid).firstline)
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +0200463 " check line() works with popup window
464 call assert_equal(11, line('.', winid))
465 call assert_equal(50, line('$', winid))
466 call assert_equal(0, line('$', 123456))
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +0200467
468 " Normal command changes what is displayed but not "firstline"
469 call win_execute(winid, "normal! \<c-y>")
470 call assert_equal(11, popup_getoptions(winid).firstline)
471 call assert_equal(10, popup_getpos(winid).firstline)
472
473 " Making some property change applies "firstline" again
474 call popup_setoptions(winid, #{line: 4})
475 call assert_equal(11, popup_getoptions(winid).firstline)
476 call assert_equal(11, popup_getpos(winid).firstline)
477
478 " Remove "firstline" property and scroll
479 call popup_setoptions(winid, #{firstline: 0})
480 call win_execute(winid, "normal! \<c-y>")
481 call assert_equal(0, popup_getoptions(winid).firstline)
482 call assert_equal(10, popup_getpos(winid).firstline)
483
484 " Making some property change has no side effect
485 call popup_setoptions(winid, #{line: 3})
486 call assert_equal(0, popup_getoptions(winid).firstline)
487 call assert_equal(10, popup_getpos(winid).firstline)
Bram Moolenaaref6b9792020-05-13 16:34:15 +0200488 call popup_close(winid)
Bram Moolenaarae943152019-06-16 22:54:14 +0200489
Bram Moolenaar30efcf32019-11-03 22:29:38 +0100490 " CTRL-D scrolls down half a page
491 let winid = popup_create(['xxx']->repeat(50), #{
492 \ maxheight: 8,
493 \ })
494 redraw
495 call assert_equal(1, popup_getpos(winid).firstline)
496 call win_execute(winid, "normal! \<C-D>")
497 call assert_equal(5, popup_getpos(winid).firstline)
498 call win_execute(winid, "normal! \<C-D>")
499 call assert_equal(9, popup_getpos(winid).firstline)
500 call win_execute(winid, "normal! \<C-U>")
501 call assert_equal(5, popup_getpos(winid).firstline)
502
503 call win_execute(winid, "normal! \<C-F>")
504 call assert_equal(11, popup_getpos(winid).firstline)
505 call win_execute(winid, "normal! \<C-B>")
506 call assert_equal(5, popup_getpos(winid).firstline)
507
Bram Moolenaarae943152019-06-16 22:54:14 +0200508 call popup_close(winid)
LemonBoy0044e512022-04-20 19:47:37 +0100509
510 " Popup with less elements than the maximum height and negative firstline:
511 " check that the popup height is correctly computed.
512 let winid = popup_create(['xxx']->repeat(4), #{
513 \ firstline: -1,
514 \ maxheight: 6,
515 \ })
516
517 let pos = popup_getpos(winid)
518 call assert_equal(3, pos.width)
519 call assert_equal(4, pos.height)
520
521 call popup_close(winid)
Bram Moolenaar8d241042019-06-12 23:40:01 +0200522endfunc
523
Bram Moolenaar99ca9c42020-09-22 21:55:41 +0200524func Test_popup_firstline_cursorline()
525 let winid = popup_create(['1111', '222222', '33333', '44444'], #{
526 \ maxheight: 2,
527 \ firstline: 3,
528 \ cursorline: 1,
529 \ })
530 call assert_equal(3, popup_getoptions(winid).firstline)
531 call assert_equal(3, getwininfo(winid)[0].topline)
532 call assert_equal(3, getcurpos(winid)[1])
533
534 call popup_close(winid)
535endfunc
536
Bram Moolenaara112f2d2019-09-01 17:38:09 +0200537func Test_popup_noscrolloff()
538 set scrolloff=5
539 let winid = popup_create(['xxx']->repeat(50), #{
540 \ maxheight: 5,
541 \ firstline: 11,
542 \ })
543 redraw
544 call assert_equal(11, popup_getoptions(winid).firstline)
545 call assert_equal(11, popup_getpos(winid).firstline)
546
547 call popup_setoptions(winid, #{firstline: 0})
548 call win_execute(winid, "normal! \<c-y>")
549 call assert_equal(0, popup_getoptions(winid).firstline)
550 call assert_equal(10, popup_getpos(winid).firstline)
551
552 call popup_close(winid)
553endfunc
554
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200555func Test_popup_drag()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200556 CheckScreendump
557
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200558 " create a popup that covers the command line
559 let lines =<< trim END
560 call setline(1, range(1, 20))
Bram Moolenaar356375f2019-08-24 14:46:29 +0200561 split
562 vsplit
563 $wincmd w
564 vsplit
565 1wincmd w
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200566 let winid = popup_create(['1111', '222222', '33333'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200567 \ drag: 1,
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200568 \ resize: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200569 \ border: [],
570 \ line: &lines - 4,
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200571 \ })
572 func Dragit()
Bram Moolenaar0b74d002021-11-29 17:38:02 +0000573 map <silent> <F3> :call test_setmouse(&lines - 4, &columns / 2)<CR>
574 map <silent> <F4> :call test_setmouse(&lines - 8, &columns / 2 - 20)<CR>
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200575 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
576 endfunc
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200577 func Resize()
Bram Moolenaar0b74d002021-11-29 17:38:02 +0000578 map <silent> <F5> :call test_setmouse(6, 21)<CR>
579 map <silent> <F6> :call test_setmouse(7, 25)<CR>
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200580 call feedkeys("\<F5>\<LeftMouse>\<F6>\<LeftDrag>\<LeftRelease>", "xt")
581 endfunc
Bram Moolenaarbfc57862021-11-26 15:57:40 +0000582 func ClickAndDrag()
Bram Moolenaar0b74d002021-11-29 17:38:02 +0000583 map <silent> <F3> :call test_setmouse(5, 2)<CR>
584 map <silent> <F4> :call test_setmouse(3, 14)<CR>
585 map <silent> <F5> :call test_setmouse(3, 18)<CR>
586 call feedkeys("\<F3>\<LeftMouse>\<LeftRelease>", "xt")
587 call feedkeys("\<F4>\<LeftMouse>\<F5>\<LeftDrag>\<LeftRelease>", "xt")
Bram Moolenaarbfc57862021-11-26 15:57:40 +0000588 endfunc
Bram Moolenaar0b74d002021-11-29 17:38:02 +0000589 func DragAllStart()
590 call popup_clear()
591 call popup_create('hello', #{line: 3, col: 5, dragall: 1})
592 endfunc
593 func DragAllDrag()
594 map <silent> <F3> :call test_setmouse(3, 5)<CR>
595 map <silent> <F4> :call test_setmouse(5, 36)<CR>
596 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
597 endfunc
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200598 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +0100599 call writefile(lines, 'XtestPopupDrag', 'D')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200600 let buf = RunVimInTerminal('-S XtestPopupDrag', #{rows: 10})
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200601 call VerifyScreenDump(buf, 'Test_popupwin_drag_01', {})
602
603 call term_sendkeys(buf, ":call Dragit()\<CR>")
604 call VerifyScreenDump(buf, 'Test_popupwin_drag_02', {})
605
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200606 call term_sendkeys(buf, ":call Resize()\<CR>")
607 call VerifyScreenDump(buf, 'Test_popupwin_drag_03', {})
608
Bram Moolenaarbfc57862021-11-26 15:57:40 +0000609 " dragging works after click on a status line
610 call term_sendkeys(buf, ":call ClickAndDrag()\<CR>")
611 call VerifyScreenDump(buf, 'Test_popupwin_drag_04', {})
612
Bram Moolenaar0b74d002021-11-29 17:38:02 +0000613 " dragging without border
614 call term_sendkeys(buf, ":call DragAllStart()\<CR>")
615 call VerifyScreenDump(buf, 'Test_popupwin_drag_05', {})
616 call term_sendkeys(buf, ":call DragAllDrag()\<CR>")
617 call VerifyScreenDump(buf, 'Test_popupwin_drag_06', {})
618
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200619 " clean up
620 call StopVimInTerminal(buf)
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200621endfunc
622
Bram Moolenaareabddc42022-04-02 15:32:16 +0100623func Test_popup_drag_minwidth()
624 CheckScreendump
625
626 " create a popup that does not fit
627 let lines =<< trim END
628 call range(40)
629 \ ->map({_,i -> string(i)})
630 \ ->popup_create({
631 \ 'drag': 1,
632 \ 'wrap': 0,
633 \ 'border': [],
634 \ 'scrollbar': 1,
635 \ 'minwidth': 100,
636 \ 'filter': {w, k -> k ==# 'q' ? len([popup_close(w)]) : 0},
637 \ })
638 func DragitDown()
639 map <silent> <F3> :call test_setmouse(1, 10)<CR>
640 map <silent> <F4> :call test_setmouse(5, 40)<CR>
641 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
642 endfunc
643 func DragitUp()
644 map <silent> <F3> :call test_setmouse(5, 40)<CR>
645 map <silent> <F4> :call test_setmouse(4, 40)<CR>
646 map <silent> <F5> :call test_setmouse(3, 40)<CR>
647 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<F5>\<LeftDrag>\<LeftRelease>", "xt")
648 endfunc
649 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +0100650 call writefile(lines, 'XtestPopupDrag', 'D')
Bram Moolenaareabddc42022-04-02 15:32:16 +0100651 let buf = RunVimInTerminal('-S XtestPopupDrag', #{rows: 10})
652 call VerifyScreenDump(buf, 'Test_popupwin_drag_minwidth_1', {})
653
654 call term_sendkeys(buf, ":call DragitDown()\<CR>")
655 call VerifyScreenDump(buf, 'Test_popupwin_drag_minwidth_2', {})
656
657 call term_sendkeys(buf, ":call DragitUp()\<CR>")
658 call VerifyScreenDump(buf, 'Test_popupwin_drag_minwidth_3', {})
659
660 call term_sendkeys(buf, 'q')
661
662 " clean up
663 call StopVimInTerminal(buf)
Bram Moolenaareabddc42022-04-02 15:32:16 +0100664endfunc
665
Bram Moolenaar35910f22020-07-12 19:24:10 +0200666func Test_popup_drag_termwin()
667 CheckUnix
668 CheckScreendump
669 CheckFeature terminal
670
671 " create a popup that covers the terminal window
672 let lines =<< trim END
Bram Moolenaar452143c2020-07-15 17:38:21 +0200673 set foldmethod=marker
674 call setline(1, range(100))
675 for nr in range(7)
676 call setline(nr * 12 + 1, "fold {{{")
Bram Moolenaar9d489562020-07-30 20:08:50 +0200677 call setline(nr * 12 + 11, "end }}}")
Bram Moolenaar452143c2020-07-15 17:38:21 +0200678 endfor
679 %foldclose
Bram Moolenaar35910f22020-07-12 19:24:10 +0200680 set shell=/bin/sh noruler
Bram Moolenaar4b2ce122020-11-21 21:41:41 +0100681 unlet $PROMPT_COMMAND
Bram Moolenaara4dc6f92020-07-12 19:52:36 +0200682 let $PS1 = 'vim> '
Bram Moolenaar452143c2020-07-15 17:38:21 +0200683 terminal ++rows=4
Bram Moolenaar35910f22020-07-12 19:24:10 +0200684 $wincmd w
685 let winid = popup_create(['1111', '2222'], #{
686 \ drag: 1,
687 \ resize: 1,
688 \ border: [],
689 \ line: 3,
690 \ })
Bram Moolenaar452143c2020-07-15 17:38:21 +0200691 func DragitLeft()
Bram Moolenaar35910f22020-07-12 19:24:10 +0200692 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
693 endfunc
Bram Moolenaar452143c2020-07-15 17:38:21 +0200694 func DragitDown()
695 call feedkeys("\<F4>\<LeftMouse>\<F5>\<LeftDrag>\<LeftRelease>", "xt")
696 endfunc
697 func DragitDownLeft()
698 call feedkeys("\<F5>\<LeftMouse>\<F6>\<LeftDrag>\<LeftRelease>", "xt")
699 endfunc
Bram Moolenaar35910f22020-07-12 19:24:10 +0200700 map <silent> <F3> :call test_setmouse(3, &columns / 2)<CR>
701 map <silent> <F4> :call test_setmouse(3, &columns / 2 - 20)<CR>
Bram Moolenaar452143c2020-07-15 17:38:21 +0200702 map <silent> <F5> :call test_setmouse(12, &columns / 2)<CR>
703 map <silent> <F6> :call test_setmouse(12, &columns / 2 - 20)<CR>
Bram Moolenaar35910f22020-07-12 19:24:10 +0200704 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +0100705 call writefile(lines, 'XtestPopupTerm', 'D')
Bram Moolenaar452143c2020-07-15 17:38:21 +0200706 let buf = RunVimInTerminal('-S XtestPopupTerm', #{rows: 16})
Bram Moolenaar35910f22020-07-12 19:24:10 +0200707 call VerifyScreenDump(buf, 'Test_popupwin_term_01', {})
708
Bram Moolenaar452143c2020-07-15 17:38:21 +0200709 call term_sendkeys(buf, ":call DragitLeft()\<CR>")
Bram Moolenaar35910f22020-07-12 19:24:10 +0200710 call VerifyScreenDump(buf, 'Test_popupwin_term_02', {})
711
Bram Moolenaar452143c2020-07-15 17:38:21 +0200712 call term_sendkeys(buf, ":call DragitDown()\<CR>")
713 call VerifyScreenDump(buf, 'Test_popupwin_term_03', {})
714
715 call term_sendkeys(buf, ":call DragitDownLeft()\<CR>")
716 call VerifyScreenDump(buf, 'Test_popupwin_term_04', {})
717
Bram Moolenaar35910f22020-07-12 19:24:10 +0200718 " clean up
719 call StopVimInTerminal(buf)
Bram Moolenaar35910f22020-07-12 19:24:10 +0200720endfunc
721
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200722func Test_popup_close_with_mouse()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200723 CheckScreendump
724
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200725 let lines =<< trim END
726 call setline(1, range(1, 20))
727 " With border, can click on X
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200728 let winid = popup_create('foobar', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200729 \ close: 'button',
730 \ border: [],
731 \ line: 1,
732 \ col: 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200733 \ })
734 func CloseMsg(id, result)
735 echomsg 'Popup closed with ' .. a:result
736 endfunc
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200737 let winid = popup_create('notification', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200738 \ close: 'click',
739 \ line: 3,
740 \ col: 15,
741 \ callback: 'CloseMsg',
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200742 \ })
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200743 let winid = popup_create('no border here', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200744 \ close: 'button',
745 \ line: 5,
746 \ col: 3,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200747 \ })
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200748 let winid = popup_create('only padding', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200749 \ close: 'button',
750 \ padding: [],
751 \ line: 5,
752 \ col: 23,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200753 \ })
754 func CloseWithX()
755 call feedkeys("\<F3>\<LeftMouse>\<LeftRelease>", "xt")
756 endfunc
757 map <silent> <F3> :call test_setmouse(1, len('foobar') + 2)<CR>
758 func CloseWithClick()
759 call feedkeys("\<F4>\<LeftMouse>\<LeftRelease>", "xt")
760 endfunc
761 map <silent> <F4> :call test_setmouse(3, 17)<CR>
Bram Moolenaarf6396232019-08-24 19:36:00 +0200762 func CreateWithMenuFilter()
763 let winid = popup_create('barfoo', #{
764 \ close: 'button',
765 \ filter: 'popup_filter_menu',
766 \ border: [],
767 \ line: 1,
768 \ col: 40,
769 \ })
770 endfunc
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200771 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +0100772 call writefile(lines, 'XtestPopupClose', 'D')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200773 let buf = RunVimInTerminal('-S XtestPopupClose', #{rows: 10})
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200774 call VerifyScreenDump(buf, 'Test_popupwin_close_01', {})
775
776 call term_sendkeys(buf, ":call CloseWithX()\<CR>")
777 call VerifyScreenDump(buf, 'Test_popupwin_close_02', {})
778
779 call term_sendkeys(buf, ":call CloseWithClick()\<CR>")
780 call VerifyScreenDump(buf, 'Test_popupwin_close_03', {})
781
Bram Moolenaarf6396232019-08-24 19:36:00 +0200782 call term_sendkeys(buf, ":call CreateWithMenuFilter()\<CR>")
783 call VerifyScreenDump(buf, 'Test_popupwin_close_04', {})
784
785 " We have to send the actual mouse code, feedkeys() would be caught the
786 " filter.
787 call term_sendkeys(buf, "\<Esc>[<0;47;1M")
788 call VerifyScreenDump(buf, 'Test_popupwin_close_05', {})
789
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200790 " clean up
791 call StopVimInTerminal(buf)
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200792endfunction
793
Bram Moolenaar7b3d9392019-10-16 22:17:07 +0200794func Test_popup_menu_wrap()
795 CheckScreendump
796
797 let lines =<< trim END
798 call setline(1, range(1, 20))
799 call popup_create([
800 \ 'one',
801 \ 'asdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfas',
802 \ 'three',
803 \ 'four',
804 \ ], #{
805 \ pos: "botleft",
806 \ border: [],
807 \ padding: [0,1,0,1],
808 \ maxheight: 3,
809 \ cursorline: 1,
810 \ filter: 'popup_filter_menu',
811 \ })
812 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +0100813 call writefile(lines, 'XtestPopupWrap', 'D')
Bram Moolenaar7b3d9392019-10-16 22:17:07 +0200814 let buf = RunVimInTerminal('-S XtestPopupWrap', #{rows: 10})
815 call VerifyScreenDump(buf, 'Test_popupwin_wrap_1', {})
816
817 call term_sendkeys(buf, "jj")
818 call VerifyScreenDump(buf, 'Test_popupwin_wrap_2', {})
819
820 " clean up
821 call term_sendkeys(buf, "\<Esc>")
822 call StopVimInTerminal(buf)
Bram Moolenaar7b3d9392019-10-16 22:17:07 +0200823endfunction
824
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200825func Test_popup_with_mask()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200826 CheckScreendump
827
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200828 let lines =<< trim END
Bram Moolenaarba45f1f2019-07-03 22:50:41 +0200829 call setline(1, repeat([join(range(1, 42), '')], 13))
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200830 hi PopupColor ctermbg=lightgrey
831 let winid = popup_create([
832 \ 'some text',
833 \ 'another line',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200834 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200835 \ line: 1,
836 \ col: 10,
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100837 \ posinvert: 0,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200838 \ wrap: 0,
839 \ fixed: 1,
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +0200840 \ scrollbar: v:false,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200841 \ zindex: 90,
842 \ padding: [],
843 \ highlight: 'PopupColor',
844 \ mask: [[1,1,1,1], [-5,-1,4,4], [7,9,2,3], [2,4,3,3]]})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200845 call popup_create([
846 \ 'xxxxxxxxx',
847 \ 'yyyyyyyyy',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200848 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200849 \ line: 3,
850 \ col: 18,
851 \ zindex: 20})
Bram Moolenaarba45f1f2019-07-03 22:50:41 +0200852 let winidb = popup_create([
853 \ 'just one line',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200854 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200855 \ line: 7,
856 \ col: 10,
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100857 \ posinvert: 0,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200858 \ wrap: 0,
859 \ fixed: 1,
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +0200860 \ scrollbar: v:false,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200861 \ close: 'button',
862 \ zindex: 90,
863 \ padding: [],
864 \ border: [],
865 \ mask: [[1,2,1,1], [-5,-1,4,4], [7,9,2,3], [3,5,5,5],[-7,-4,5,5]]})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200866 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +0100867 call writefile(lines, 'XtestPopupMask', 'D')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200868 let buf = RunVimInTerminal('-S XtestPopupMask', #{rows: 13})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200869 call VerifyScreenDump(buf, 'Test_popupwin_mask_1', {})
870
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200871 call term_sendkeys(buf, ":call popup_move(winid, #{col: 11, line: 2})\<CR>")
872 call term_sendkeys(buf, ":call popup_move(winidb, #{col: 12})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200873 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200874 call VerifyScreenDump(buf, 'Test_popupwin_mask_2', {})
875
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200876 call term_sendkeys(buf, ":call popup_move(winid, #{col: 65, line: 2})\<CR>")
877 call term_sendkeys(buf, ":call popup_move(winidb, #{col: 63})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200878 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaard529ba52019-07-02 23:13:53 +0200879 call VerifyScreenDump(buf, 'Test_popupwin_mask_3', {})
880
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200881 call term_sendkeys(buf, ":call popup_move(winid, #{pos: 'topright', col: 12, line: 2})\<CR>")
882 call term_sendkeys(buf, ":call popup_move(winidb, #{pos: 'topright', col: 12})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200883 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaard529ba52019-07-02 23:13:53 +0200884 call VerifyScreenDump(buf, 'Test_popupwin_mask_4', {})
885
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200886 call term_sendkeys(buf, ":call popup_move(winid, #{pos: 'topright', col: 12, line: 11})\<CR>")
887 call term_sendkeys(buf, ":call popup_move(winidb, #{pos: 'topleft', col: 42, line: 11})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200888 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaarb4207472019-07-12 16:05:45 +0200889 call VerifyScreenDump(buf, 'Test_popupwin_mask_5', {})
890
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200891 " clean up
892 call StopVimInTerminal(buf)
Bram Moolenaar4012d262020-12-29 11:57:46 +0100893
894 " this was causing a crash
895 call popup_create('test', #{mask: [[0, 0, 0, 0]]})
896 call popup_clear()
Bram Moolenaar10ccfb22021-02-13 21:31:18 +0100897
898 " this was causing an internal error
899 enew
900 set nowrap
901 call repeat('x', &columns)->setline(1)
902 call prop_type_add('textprop', {})
903 call prop_add(1, 1, #{length: &columns, type: 'textprop'})
904 vsplit
905 let opts = popup_create('', #{textprop: 'textprop'})
906 \ ->popup_getoptions()
907 \ ->extend(#{mask: [[1, 1, 1, 1]]})
908 call popup_create('', opts)
909 redraw
910
911 close!
912 bwipe!
913 call prop_type_delete('textprop')
914 call popup_clear()
915 set wrap&
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200916endfunc
917
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200918func Test_popup_select()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200919 CheckScreendump
920 CheckFeature clipboard_working
921
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200922 " create a popup with some text to be selected
923 let lines =<< trim END
Bram Moolenaar1755ec42019-06-15 13:13:54 +0200924 set clipboard=autoselect
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200925 call setline(1, range(1, 20))
Bram Moolenaar4dd751b2019-08-17 19:10:53 +0200926 let winid = popup_create(['the word', 'some more', 'several words here', 'invisible', '5', '6', '7'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200927 \ drag: 1,
928 \ border: [],
929 \ line: 3,
930 \ col: 10,
Bram Moolenaar4dd751b2019-08-17 19:10:53 +0200931 \ maxheight: 3,
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200932 \ })
933 func Select1()
934 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
935 endfunc
936 map <silent> <F3> :call test_setmouse(4, 15)<CR>
937 map <silent> <F4> :call test_setmouse(6, 23)<CR>
938 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +0100939 call writefile(lines, 'XtestPopupSelect', 'D')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200940 let buf = RunVimInTerminal('-S XtestPopupSelect', #{rows: 10})
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200941 call term_sendkeys(buf, ":call Select1()\<CR>")
942 call VerifyScreenDump(buf, 'Test_popupwin_select_01', {})
943
944 call term_sendkeys(buf, ":call popup_close(winid)\<CR>")
945 call term_sendkeys(buf, "\"*p")
Bram Moolenaar8ccabf62019-07-12 18:12:51 +0200946 " clean the command line, sometimes it still shows a command
947 call term_sendkeys(buf, ":\<esc>")
948
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200949 call VerifyScreenDump(buf, 'Test_popupwin_select_02', {})
950
951 " clean up
952 call StopVimInTerminal(buf)
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200953endfunc
954
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200955func Test_popup_in_tab()
956 " default popup is local to tab, not visible when in other tab
957 let winid = popup_create("text", {})
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200958 let bufnr = winbufnr(winid)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200959 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200960 call assert_equal(0, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200961 tabnew
962 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200963 call assert_equal(1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200964 quit
965 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200966
967 call assert_equal(1, bufexists(bufnr))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200968 call popup_clear()
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200969 " buffer is gone now
970 call assert_equal(0, bufexists(bufnr))
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200971
972 " global popup is visible in any tab
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200973 let winid = popup_create("text", #{tabpage: -1})
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(-1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200976 tabnew
977 call assert_equal(1, 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 Moolenaar3ff5f0f2019-06-10 13:11:22 +0200981 call popup_clear()
Bram Moolenaara3fce622019-06-20 02:31:49 +0200982
983 " create popup in other tab
984 tabnew
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200985 let winid = popup_create("text", #{tabpage: 1})
Bram Moolenaara3fce622019-06-20 02:31:49 +0200986 call assert_equal(0, popup_getpos(winid).visible)
987 call assert_equal(1, popup_getoptions(winid).tabpage)
988 quit
989 call assert_equal(1, popup_getpos(winid).visible)
990 call assert_equal(0, popup_getoptions(winid).tabpage)
991 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200992endfunc
993
994func Test_popup_valid_arguments()
Bram Moolenaaref6b9792020-05-13 16:34:15 +0200995 call assert_equal(0, len(popup_list()))
996
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200997 " Zero value is like the property wasn't there
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200998 let winid = popup_create("text", #{col: 0})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200999 let pos = popup_getpos(winid)
1000 call assert_inrange(&columns / 2 - 1, &columns / 2 + 1, pos.col)
Bram Moolenaaref6b9792020-05-13 16:34:15 +02001001 call assert_equal([winid], popup_list())
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001002 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001003
1004 " using cursor column has minimum value of 1
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001005 let winid = popup_create("text", #{col: 'cursor-100'})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001006 let pos = popup_getpos(winid)
1007 call assert_equal(1, pos.col)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001008 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001009
1010 " center
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001011 let winid = popup_create("text", #{pos: 'center'})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001012 let pos = popup_getpos(winid)
1013 let around = (&columns - pos.width) / 2
1014 call assert_inrange(around - 1, around + 1, pos.col)
1015 let around = (&lines - pos.height) / 2
1016 call assert_inrange(around - 1, around + 1, pos.line)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001017 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001018endfunc
1019
1020func Test_popup_invalid_arguments()
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02001021 call assert_fails('call popup_create(666, {})', 'E86:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001022 call popup_clear()
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001023 call assert_fails('call popup_create("text", "none")', 'E1206:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001024 call popup_clear()
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02001025 call assert_fails('call popup_create(test_null_string(), {})', 'E450:')
1026 call assert_fails('call popup_create(test_null_list(), {})', 'E450:')
1027 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001028
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001029 call assert_fails('call popup_create("text", #{col: "xxx"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001030 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001031 call assert_fails('call popup_create("text", #{col: "cursor8"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001032 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001033 call assert_fails('call popup_create("text", #{col: "cursor+x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001034 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001035 call assert_fails('call popup_create("text", #{col: "cursor+8x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001036 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001037
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001038 call assert_fails('call popup_create("text", #{line: "xxx"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001039 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001040 call assert_fails('call popup_create("text", #{line: "cursor8"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001041 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001042 call assert_fails('call popup_create("text", #{line: "cursor+x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001043 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001044 call assert_fails('call popup_create("text", #{line: "cursor+8x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001045 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001046
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001047 call assert_fails('call popup_create("text", #{pos: "there"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001048 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001049 call assert_fails('call popup_create("text", #{padding: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001050 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001051 call assert_fails('call popup_create("text", #{border: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001052 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001053 call assert_fails('call popup_create("text", #{borderhighlight: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001054 call popup_clear()
Bram Moolenaar403d0902019-07-17 21:37:32 +02001055 call assert_fails('call popup_create("text", #{borderhighlight: test_null_list()})', 'E714:')
1056 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001057 call assert_fails('call popup_create("text", #{borderchars: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001058 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001059
Bram Moolenaar83bd7a92022-05-29 17:13:24 +01001060 call assert_fails('call popup_create([#{text: "text"}, 666], {})', 'E1284: Argument 1, list item 2: Dictionary required')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001061 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001062 call assert_fails('call popup_create([#{text: "text", props: "none"}], {})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001063 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001064 call assert_fails('call popup_create([#{text: "text", props: ["none"]}], {})', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001065 call popup_clear()
Bram Moolenaarb0992022020-01-30 14:55:42 +01001066 call assert_fails('call popup_create([#{text: "text", props: range(3)}], {})', 'E715:')
1067 call popup_clear()
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +02001068 call assert_fails('call popup_create("text", #{mask: ["asdf"]})', 'E475:')
1069 call popup_clear()
Bram Moolenaarb0992022020-01-30 14:55:42 +01001070 call assert_fails('call popup_create("text", #{mask: range(5)})', 'E475:')
1071 call popup_clear()
1072 call popup_create("text", #{mask: [range(4)]})
1073 call popup_clear()
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +02001074 call assert_fails('call popup_create("text", #{mask: test_null_list()})', 'E475:')
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001075 call assert_fails('call popup_create("text", #{mapping: []})', 'E745:')
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +02001076 call popup_clear()
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02001077 call assert_fails('call popup_create("text", #{tabpage : 4})', 'E997:')
1078 call popup_clear()
Bram Moolenaar83bd7a92022-05-29 17:13:24 +01001079
1080 call assert_fails('call popup_create(range(10), {})', 'E1024:')
1081 call popup_clear()
1082 call assert_fails('call popup_create([1, 2], {})', 'E1284: Argument 1, list item 1: Dictionary required')
1083 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001084endfunc
1085
Bram Moolenaareea16992019-05-31 17:34:48 +02001086func Test_win_execute_closing_curwin()
1087 split
1088 let winid = popup_create('some text', {})
Bram Moolenaare2e40752020-09-04 21:18:46 +02001089 call assert_fails('call win_execute(winid, winnr() .. "close")', 'E994:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001090 call popup_clear()
Bram Moolenaar49540192019-12-11 19:34:54 +01001091
1092 let winid = popup_create('some text', {})
Bram Moolenaare2e40752020-09-04 21:18:46 +02001093 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 +01001094 call popup_clear()
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001095endfunc
1096
1097func Test_win_execute_not_allowed()
1098 let winid = popup_create('some text', {})
1099 call assert_fails('call win_execute(winid, "split")', 'E994:')
1100 call assert_fails('call win_execute(winid, "vsplit")', 'E994:')
1101 call assert_fails('call win_execute(winid, "close")', 'E994:')
1102 call assert_fails('call win_execute(winid, "bdelete")', 'E994:')
Bram Moolenaar2d247842019-06-01 17:06:25 +02001103 call assert_fails('call win_execute(winid, "bwipe!")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001104 call assert_fails('call win_execute(winid, "tabnew")', 'E994:')
1105 call assert_fails('call win_execute(winid, "tabnext")', 'E994:')
1106 call assert_fails('call win_execute(winid, "next")', 'E994:')
1107 call assert_fails('call win_execute(winid, "rewind")', 'E994:')
Bram Moolenaar3a2505c2020-03-09 16:40:41 +01001108 call assert_fails('call win_execute(winid, "pedit filename")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001109 call assert_fails('call win_execute(winid, "buf")', 'E994:')
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01001110 call assert_fails('call win_execute(winid, "bnext")', 'E994:')
1111 call assert_fails('call win_execute(winid, "bprev")', 'E994:')
1112 call assert_fails('call win_execute(winid, "bfirst")', 'E994:')
1113 call assert_fails('call win_execute(winid, "blast")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001114 call assert_fails('call win_execute(winid, "edit")', 'E994:')
1115 call assert_fails('call win_execute(winid, "enew")', 'E994:')
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +02001116 call assert_fails('call win_execute(winid, "help")', 'E994:')
1117 call assert_fails('call win_execute(winid, "1only")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001118 call assert_fails('call win_execute(winid, "wincmd x")', 'E994:')
1119 call assert_fails('call win_execute(winid, "wincmd w")', 'E994:')
1120 call assert_fails('call win_execute(winid, "wincmd t")', 'E994:')
1121 call assert_fails('call win_execute(winid, "wincmd b")', 'E994:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001122 call popup_clear()
Bram Moolenaareea16992019-05-31 17:34:48 +02001123endfunc
1124
Bram Moolenaar402502d2019-05-30 22:07:36 +02001125func Test_popup_with_wrap()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001126 CheckScreendump
1127
Bram Moolenaar402502d2019-05-30 22:07:36 +02001128 let lines =<< trim END
1129 call setline(1, range(1, 100))
1130 let winid = popup_create(
1131 \ 'a long line that wont fit',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001132 \ #{line: 3, col: 20, maxwidth: 10, wrap: 1})
Bram Moolenaar402502d2019-05-30 22:07:36 +02001133 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01001134 call writefile(lines, 'XtestPopup', 'D')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001135 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar402502d2019-05-30 22:07:36 +02001136 call VerifyScreenDump(buf, 'Test_popupwin_wrap', {})
1137
1138 " clean up
1139 call StopVimInTerminal(buf)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001140endfunc
1141
1142func Test_popup_without_wrap()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001143 CheckScreendump
1144
Bram Moolenaar402502d2019-05-30 22:07:36 +02001145 let lines =<< trim END
1146 call setline(1, range(1, 100))
1147 let winid = popup_create(
1148 \ 'a long line that wont fit',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001149 \ #{line: 3, col: 20, maxwidth: 10, wrap: 0})
Bram Moolenaar402502d2019-05-30 22:07:36 +02001150 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01001151 call writefile(lines, 'XtestPopup', 'D')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001152 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar402502d2019-05-30 22:07:36 +02001153 call VerifyScreenDump(buf, 'Test_popupwin_nowrap', {})
1154
1155 " clean up
1156 call StopVimInTerminal(buf)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001157endfunc
1158
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001159func Test_popup_with_showbreak()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001160 CheckScreendump
1161
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001162 let lines =<< trim END
1163 set showbreak=>>\
1164 call setline(1, range(1, 20))
1165 let winid = popup_dialog(
Bram Moolenaar8ae54372019-09-15 18:11:16 +02001166 \ 'a long line here that wraps',
1167 \ #{filter: 'popup_filter_yesno',
1168 \ maxwidth: 12})
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001169 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01001170 call writefile(lines, 'XtestPopupShowbreak', 'D')
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001171 let buf = RunVimInTerminal('-S XtestPopupShowbreak', #{rows: 10})
1172 call VerifyScreenDump(buf, 'Test_popupwin_showbreak', {})
1173
1174 " clean up
1175 call term_sendkeys(buf, "y")
1176 call StopVimInTerminal(buf)
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001177endfunc
1178
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001179func Test_popup_time()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001180 CheckFeature timers
1181
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001182 topleft vnew
1183 call setline(1, 'hello')
1184
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001185 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001186 \ line: 1,
1187 \ col: 1,
1188 \ minwidth: 20,
1189 \ time: 500,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001190 \})
1191 redraw
1192 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1193 call assert_equal('world', line)
1194
Bram Moolenaarb4f06282019-07-12 21:07:54 +02001195 call assert_equal(winid, popup_locate(1, 1))
1196 call assert_equal(winid, popup_locate(1, 20))
1197 call assert_equal(0, popup_locate(1, 21))
1198 call assert_equal(0, popup_locate(2, 1))
1199
Bram Moolenaar9c8d12c2022-12-02 15:06:07 +00001200 " Mac is usually a bit slow
1201 let delay = has('mac') ? '900m' : '700m'
1202 exe 'sleep ' .. delay
Bram Moolenaar35d5af62019-05-26 20:44:10 +02001203 redraw
Bram Moolenaar9c8d12c2022-12-02 15:06:07 +00001204
Bram Moolenaar196b4662019-09-06 21:34:30 +02001205 let line = join(map(range(1, 5), '1->screenstring(v:val)'), '')
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001206 call assert_equal('hello', line)
1207
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001208 call popup_create('on the command line', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001209 \ line: &lines,
1210 \ col: 10,
1211 \ minwidth: 20,
1212 \ time: 500,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001213 \})
1214 redraw
1215 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
1216 call assert_match('.*on the command line.*', line)
1217
Bram Moolenaar9c8d12c2022-12-02 15:06:07 +00001218 exe 'sleep ' .. delay
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001219 redraw
1220 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
1221 call assert_notmatch('.*on the command line.*', line)
1222
1223 bwipe!
1224endfunc
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001225
1226func Test_popup_hide()
1227 topleft vnew
1228 call setline(1, 'hello')
1229
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001230 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001231 \ line: 1,
1232 \ col: 1,
1233 \ minwidth: 20,
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001234 \})
1235 redraw
1236 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1237 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001238 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001239 " buffer is still listed and active
1240 call assert_match(winbufnr(winid) .. 'u a.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001241
1242 call popup_hide(winid)
1243 redraw
1244 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1245 call assert_equal('hello', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001246 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001247 " buffer is still listed but hidden
Bram Moolenaarca7c0782020-01-14 20:42:48 +01001248 call assert_match(winbufnr(winid) .. 'u a.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001249
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001250 eval winid->popup_show()
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001251 redraw
1252 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1253 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001254 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001255
1256
1257 call popup_close(winid)
1258 redraw
1259 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1260 call assert_equal('hello', line)
1261
1262 " error is given for existing non-popup window
1263 call assert_fails('call popup_hide(win_getid())', 'E993:')
1264
1265 " no error non-existing window
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001266 eval 1234234->popup_hide()
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001267 call popup_show(41234234)
1268
1269 bwipe!
1270endfunc
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001271
1272func Test_popup_move()
1273 topleft vnew
1274 call setline(1, 'hello')
1275
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001276 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001277 \ line: 1,
1278 \ col: 1,
1279 \ minwidth: 20,
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001280 \})
1281 redraw
1282 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
1283 call assert_equal('world ', line)
1284
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001285 call popup_move(winid, #{line: 2, col: 2})
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001286 redraw
1287 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
1288 call assert_equal('hello ', line)
1289 let line = join(map(range(1, 6), 'screenstring(2, v:val)'), '')
1290 call assert_equal('~world', line)
1291
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001292 eval winid->popup_move(#{line: 1})
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001293 redraw
1294 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
1295 call assert_equal('hworld', line)
1296
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001297 call assert_fails('call popup_move(winid, [])', 'E1206:')
1298 call assert_fails('call popup_move(winid, test_null_dict())', 'E1297:')
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02001299
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001300 call popup_close(winid)
1301
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02001302 call assert_equal(0, popup_move(-1, {}))
1303
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001304 bwipe!
1305endfunc
Bram Moolenaarbc133542019-05-29 20:26:46 +02001306
Bram Moolenaar402502d2019-05-30 22:07:36 +02001307func Test_popup_getpos()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001308 let winid = popup_create('hello', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001309 \ line: 2,
1310 \ col: 3,
1311 \ minwidth: 10,
1312 \ minheight: 11,
Bram Moolenaarbc133542019-05-29 20:26:46 +02001313 \})
1314 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +02001315 let res = popup_getpos(winid)
Bram Moolenaarbc133542019-05-29 20:26:46 +02001316 call assert_equal(2, res.line)
1317 call assert_equal(3, res.col)
1318 call assert_equal(10, res.width)
1319 call assert_equal(11, res.height)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001320 call assert_equal(1, res.visible)
Bram Moolenaarbc133542019-05-29 20:26:46 +02001321
1322 call popup_close(winid)
1323endfunc
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001324
1325func Test_popup_width_longest()
1326 let tests = [
1327 \ [['hello', 'this', 'window', 'displays', 'all of its text'], 15],
1328 \ [['hello', 'this', 'window', 'all of its text', 'displays'], 15],
1329 \ [['hello', 'this', 'all of its text', 'window', 'displays'], 15],
1330 \ [['hello', 'all of its text', 'this', 'window', 'displays'], 15],
1331 \ [['all of its text', 'hello', 'this', 'window', 'displays'], 15],
1332 \ ]
1333
1334 for test in tests
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001335 let winid = popup_create(test[0], #{line: 2, col: 3})
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001336 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +02001337 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001338 call assert_equal(test[1], position.width)
1339 call popup_close(winid)
1340 endfor
1341endfunc
1342
1343func Test_popup_wraps()
1344 let tests = [
1345 \ ['nowrap', 6, 1],
1346 \ ['a line that wraps once', 12, 2],
1347 \ ['a line that wraps two times', 12, 3],
1348 \ ]
1349 for test in tests
1350 let winid = popup_create(test[0],
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001351 \ #{line: 2, col: 3, maxwidth: 12})
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001352 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +02001353 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001354 call assert_equal(test[1], position.width)
1355 call assert_equal(test[2], position.height)
1356
1357 call popup_close(winid)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001358 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001359 endfor
1360endfunc
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001361
1362func Test_popup_getoptions()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001363 let winid = popup_create('hello', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001364 \ line: 2,
1365 \ col: 3,
1366 \ minwidth: 10,
1367 \ minheight: 11,
1368 \ maxwidth: 20,
1369 \ maxheight: 21,
1370 \ zindex: 100,
1371 \ time: 5000,
1372 \ fixed: 1
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001373 \})
1374 redraw
1375 let res = popup_getoptions(winid)
1376 call assert_equal(2, res.line)
1377 call assert_equal(3, res.col)
1378 call assert_equal(10, res.minwidth)
1379 call assert_equal(11, res.minheight)
1380 call assert_equal(20, res.maxwidth)
1381 call assert_equal(21, res.maxheight)
1382 call assert_equal(100, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001383 call assert_equal(1, res.fixed)
Bram Moolenaarb8350ab2019-08-04 17:59:49 +02001384 call assert_equal(1, res.mapping)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001385 if has('timers')
1386 call assert_equal(5000, res.time)
1387 endif
1388 call popup_close(winid)
1389
1390 let winid = popup_create('hello', {})
1391 redraw
1392 let res = popup_getoptions(winid)
1393 call assert_equal(0, res.line)
1394 call assert_equal(0, res.col)
1395 call assert_equal(0, res.minwidth)
1396 call assert_equal(0, res.minheight)
1397 call assert_equal(0, res.maxwidth)
1398 call assert_equal(0, res.maxheight)
1399 call assert_equal(50, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001400 call assert_equal(0, res.fixed)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001401 if has('timers')
1402 call assert_equal(0, res.time)
1403 endif
1404 call popup_close(winid)
1405 call assert_equal({}, popup_getoptions(winid))
1406endfunc
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001407
1408func Test_popup_option_values()
1409 new
1410 " window-local
1411 setlocal number
1412 setlocal nowrap
1413 " buffer-local
1414 setlocal omnifunc=Something
1415 " global/buffer-local
1416 setlocal path=/there
1417 " global/window-local
Bram Moolenaara112f2d2019-09-01 17:38:09 +02001418 setlocal statusline=2
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001419
1420 let winid = popup_create('hello', {})
1421 call assert_equal(0, getwinvar(winid, '&number'))
1422 call assert_equal(1, getwinvar(winid, '&wrap'))
1423 call assert_equal('', getwinvar(winid, '&omnifunc'))
1424 call assert_equal(&g:path, getwinvar(winid, '&path'))
Bram Moolenaara112f2d2019-09-01 17:38:09 +02001425 call assert_equal(&g:statusline, getwinvar(winid, '&statusline'))
1426
1427 " 'scrolloff' is reset to zero
1428 call assert_equal(5, &scrolloff)
1429 call assert_equal(0, getwinvar(winid, '&scrolloff'))
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001430
1431 call popup_close(winid)
1432 bwipe
1433endfunc
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001434
1435func Test_popup_atcursor()
1436 topleft vnew
1437 call setline(1, [
1438 \ 'xxxxxxxxxxxxxxxxx',
1439 \ 'xxxxxxxxxxxxxxxxx',
1440 \ 'xxxxxxxxxxxxxxxxx',
1441 \])
1442
1443 call cursor(2, 2)
1444 redraw
1445 let winid = popup_atcursor('vim', {})
1446 redraw
1447 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
1448 call assert_equal('xvimxxxxxxxxxxxxx', line)
1449 call popup_close(winid)
1450
1451 call cursor(3, 4)
1452 redraw
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001453 let winid = 'vim'->popup_atcursor({})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001454 redraw
1455 let line = join(map(range(1, 17), 'screenstring(2, v:val)'), '')
1456 call assert_equal('xxxvimxxxxxxxxxxx', line)
1457 call popup_close(winid)
1458
1459 call cursor(1, 1)
1460 redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001461 let winid = popup_create('vim', #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001462 \ line: 'cursor+2',
1463 \ col: 'cursor+1',
1464 \})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001465 redraw
1466 let line = join(map(range(1, 17), 'screenstring(3, v:val)'), '')
1467 call assert_equal('xvimxxxxxxxxxxxxx', line)
1468 call popup_close(winid)
1469
1470 call cursor(3, 3)
1471 redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001472 let winid = popup_create('vim', #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001473 \ line: 'cursor-2',
1474 \ col: 'cursor-1',
1475 \})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001476 redraw
1477 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
1478 call assert_equal('xvimxxxxxxxxxxxxx', line)
1479 call popup_close(winid)
1480
Bram Moolenaar402502d2019-05-30 22:07:36 +02001481 " just enough room above
1482 call cursor(3, 3)
1483 redraw
1484 let winid = popup_atcursor(['vim', 'is great'], {})
1485 redraw
1486 let pos = popup_getpos(winid)
1487 call assert_equal(1, pos.line)
1488 call popup_close(winid)
1489
1490 " not enough room above, popup goes below the cursor
1491 call cursor(3, 3)
1492 redraw
1493 let winid = popup_atcursor(['vim', 'is', 'great'], {})
1494 redraw
1495 let pos = popup_getpos(winid)
1496 call assert_equal(4, pos.line)
1497 call popup_close(winid)
1498
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001499 " cursor in first line, popup in line 2
1500 call cursor(1, 1)
1501 redraw
1502 let winid = popup_atcursor(['vim', 'is', 'great'], {})
1503 redraw
1504 let pos = popup_getpos(winid)
1505 call assert_equal(2, pos.line)
1506 call popup_close(winid)
1507
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001508 bwipe!
1509endfunc
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001510
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001511func Test_popup_atcursor_pos()
1512 CheckScreendump
Dominique Pellec60e9592021-07-03 21:41:38 +02001513 CheckFeature conceal
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001514
1515 let lines =<< trim END
1516 call setline(1, repeat([repeat('-', 60)], 15))
1517 set so=0
1518
1519 normal 9G3|r#
1520 let winid1 = popup_atcursor(['first', 'second'], #{
1521 \ moved: [0, 0, 0],
1522 \ })
1523 normal 9G21|r&
1524 let winid1 = popup_atcursor(['FIrsT', 'SEcoND'], #{
1525 \ pos: 'botright',
1526 \ moved: [0, 0, 0],
1527 \ })
1528 normal 3G27|r%
1529 let winid1 = popup_atcursor(['fiRSt', 'seCOnd'], #{
1530 \ pos: 'topleft',
1531 \ moved: [0, 0, 0],
1532 \ })
1533 normal 3G45|r@
1534 let winid1 = popup_atcursor(['First', 'SeconD'], #{
1535 \ pos: 'topright',
Bram Moolenaarb0992022020-01-30 14:55:42 +01001536 \ moved: range(3),
1537 \ mousemoved: range(3),
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001538 \ })
Bram Moolenaarea042672021-06-29 20:22:32 +02001539
1540 normal 9G27|Rconcealed X
1541 syn match Hidden /concealed/ conceal
1542 set conceallevel=2 concealcursor=n
1543 redraw
1544 normal 0fX
1545 call popup_atcursor('mark', {})
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001546 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01001547 call writefile(lines, 'XtestPopupAtcursorPos', 'D')
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001548 let buf = RunVimInTerminal('-S XtestPopupAtcursorPos', #{rows: 12})
1549 call VerifyScreenDump(buf, 'Test_popupwin_atcursor_pos', {})
1550
1551 " clean up
1552 call StopVimInTerminal(buf)
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001553endfunc
1554
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001555func Test_popup_beval()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001556 CheckScreendump
Bram Moolenaar1e82a782019-09-21 22:57:06 +02001557 CheckFeature balloon_eval_term
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001558
1559 let lines =<< trim END
1560 call setline(1, range(1, 20))
1561 call setline(5, 'here is some text to hover over')
1562 set balloonevalterm
1563 set balloonexpr=BalloonExpr()
1564 set balloondelay=100
1565 func BalloonExpr()
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001566 let s:winid = [v:beval_text]->popup_beval({})
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001567 return ''
1568 endfunc
1569 func Hover()
1570 call test_setmouse(5, 15)
1571 call feedkeys("\<MouseMove>\<Ignore>", "xt")
1572 sleep 100m
1573 endfunc
1574 func MoveOntoPopup()
1575 call test_setmouse(4, 17)
1576 call feedkeys("\<F4>\<MouseMove>\<Ignore>", "xt")
1577 endfunc
1578 func MoveAway()
1579 call test_setmouse(5, 13)
1580 call feedkeys("\<F5>\<MouseMove>\<Ignore>", "xt")
1581 endfunc
1582 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01001583 call writefile(lines, 'XtestPopupBeval', 'D')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001584 let buf = RunVimInTerminal('-S XtestPopupBeval', #{rows: 10})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001585 call TermWait(buf, 50)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001586 call term_sendkeys(buf, 'j')
1587 call term_sendkeys(buf, ":call Hover()\<CR>")
1588 call VerifyScreenDump(buf, 'Test_popupwin_beval_1', {})
1589
1590 call term_sendkeys(buf, ":call MoveOntoPopup()\<CR>")
1591 call VerifyScreenDump(buf, 'Test_popupwin_beval_2', {})
1592
1593 call term_sendkeys(buf, ":call MoveAway()\<CR>")
1594 call VerifyScreenDump(buf, 'Test_popupwin_beval_3', {})
1595
1596 " clean up
1597 call StopVimInTerminal(buf)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001598endfunc
1599
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001600func Test_popup_filter()
1601 new
1602 call setline(1, 'some text')
1603
1604 func MyPopupFilter(winid, c)
Bram Moolenaarec084d32020-02-28 22:44:47 +01001605 if a:c == 'e' || a:c == "\<F9>"
1606 let g:eaten = a:c
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001607 return 1
1608 endif
1609 if a:c == '0'
1610 let g:ignored = '0'
1611 return 0
1612 endif
1613 if a:c == 'x'
1614 call popup_close(a:winid)
1615 return 1
1616 endif
1617 return 0
1618 endfunc
1619
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001620 let winid = 'something'->popup_create(#{filter: 'MyPopupFilter'})
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001621 redraw
1622
1623 " e is consumed by the filter
Bram Moolenaar999db232021-07-04 14:00:55 +02001624 let g:eaten = ''
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001625 call feedkeys('e', 'xt')
1626 call assert_equal('e', g:eaten)
Bram Moolenaarec084d32020-02-28 22:44:47 +01001627 call feedkeys("\<F9>", 'xt')
1628 call assert_equal("\<F9>", g:eaten)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001629
1630 " 0 is ignored by the filter
Bram Moolenaar999db232021-07-04 14:00:55 +02001631 let g:ignored = ''
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001632 normal $
1633 call assert_equal(9, getcurpos()[2])
1634 call feedkeys('0', 'xt')
1635 call assert_equal('0', g:ignored)
Bram Moolenaar999db232021-07-04 14:00:55 +02001636
1637 if has('win32') && has('gui_running')
1638 echo "FIXME: this check is very flaky on MS-Windows GUI, the cursor doesn't move"
1639 else
1640 call assert_equal(1, getcurpos()[2])
1641 endif
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001642
1643 " x closes the popup
1644 call feedkeys('x', 'xt')
Bram Moolenaarec084d32020-02-28 22:44:47 +01001645 call assert_equal("\<F9>", g:eaten)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001646 call assert_equal(-1, winbufnr(winid))
1647
Bram Moolenaar999db232021-07-04 14:00:55 +02001648 unlet g:eaten
1649 unlet g:ignored
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001650 delfunc MyPopupFilter
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001651 call popup_clear()
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001652endfunc
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001653
Bram Moolenaar4934ad02020-09-28 22:29:58 +02001654" this tests that the filter is not used for :normal command
Bram Moolenaar189832b2020-09-23 12:29:11 +02001655func Test_popup_filter_normal_cmd()
1656 CheckScreendump
1657
1658 let lines =<< trim END
Bram Moolenaar4934ad02020-09-28 22:29:58 +02001659 let text = range(1, 20)->map({_, v -> string(v)})
1660 let g:winid = popup_create(text, #{maxheight: 5, minwidth: 3, filter: 'invalidfilter'})
1661 call timer_start(0, {-> win_execute(g:winid, 'norm! 10Gzz')})
Bram Moolenaar189832b2020-09-23 12:29:11 +02001662 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01001663 call writefile(lines, 'XtestPopupNormal', 'D')
Bram Moolenaar189832b2020-09-23 12:29:11 +02001664 let buf = RunVimInTerminal('-S XtestPopupNormal', #{rows: 10})
1665 call TermWait(buf, 100)
1666 call VerifyScreenDump(buf, 'Test_popupwin_normal_cmd', {})
1667
1668 call StopVimInTerminal(buf)
Bram Moolenaar189832b2020-09-23 12:29:11 +02001669endfunc
1670
Bram Moolenaar10476522020-09-24 22:57:31 +02001671" test that cursor line highlight is updated after using win_execute()
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02001672func Test_popup_filter_win_execute()
1673 CheckScreendump
1674
1675 let lines =<< trim END
Bram Moolenaar10476522020-09-24 22:57:31 +02001676 let lines = range(1, &lines * 2)->map({_, v -> string(v)})
1677 let g:id = popup_create(lines, #{
1678 \ minheight: &lines - 5,
1679 \ maxheight: &lines - 5,
1680 \ cursorline: 1,
1681 \ })
1682 redraw
1683 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01001684 call writefile(lines, 'XtestPopupWinExecute', 'D')
Bram Moolenaar10476522020-09-24 22:57:31 +02001685 let buf = RunVimInTerminal('-S XtestPopupWinExecute', #{rows: 14})
1686
1687 call term_sendkeys(buf, ":call win_execute(g:id, ['normal 17Gzz'])\<CR>")
1688 call term_sendkeys(buf, ":\<CR>")
1689
1690 call VerifyScreenDump(buf, 'Test_popupwin_win_execute_cursorline', {})
1691
1692 call StopVimInTerminal(buf)
Bram Moolenaar10476522020-09-24 22:57:31 +02001693endfunc
1694
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02001695func Test_popup_set_firstline()
1696 CheckScreendump
1697
1698 let lines =<< trim END
1699 let lines = range(1, 50)->map({_, v -> string(v)})
1700 let g:id = popup_create(lines, #{
1701 \ minwidth: 20,
1702 \ maxwidth: 20,
1703 \ minheight: &lines - 5,
1704 \ maxheight: &lines - 5,
1705 \ cursorline: 1,
1706 \ })
1707 call popup_setoptions(g:id, #{firstline: 10})
1708 redraw
1709 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01001710 call writefile(lines, 'XtestPopupWinSetFirstline', 'D')
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02001711 let buf = RunVimInTerminal('-S XtestPopupWinSetFirstline', #{rows: 16})
1712
1713 call VerifyScreenDump(buf, 'Test_popupwin_set_firstline_1', {})
1714
1715 call term_sendkeys(buf, ":call popup_setoptions(g:id, #{firstline: 5})\<CR>")
1716 call term_sendkeys(buf, ":\<CR>")
1717 call VerifyScreenDump(buf, 'Test_popupwin_set_firstline_2', {})
1718
1719 call StopVimInTerminal(buf)
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02001720endfunc
1721
Bram Moolenaar10476522020-09-24 22:57:31 +02001722" this tests that we don't get stuck with an error in "win_execute()"
1723func Test_popup_filter_win_execute_error()
1724 CheckScreendump
1725
1726 let lines =<< trim END
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02001727 let g:winid = popup_create('some text', {'filter': 'invalidfilter'})
1728 call timer_start(0, {-> win_execute(g:winid, 'invalidCommand')})
1729 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01001730 call writefile(lines, 'XtestPopupWinExecuteError', 'D')
Bram Moolenaar10476522020-09-24 22:57:31 +02001731 let buf = RunVimInTerminal('-S XtestPopupWinExecuteError', #{rows: 10, wait_for_ruler: 0})
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02001732
1733 call WaitFor({-> term_getline(buf, 9) =~ 'Not an editor command: invalidCommand'})
1734 call term_sendkeys(buf, "\<CR>")
1735 call WaitFor({-> term_getline(buf, 9) =~ 'Unknown function: invalidfilter'})
1736 call term_sendkeys(buf, "\<CR>")
1737 call WaitFor({-> term_getline(buf, 9) =~ 'Not allowed in a popup window'})
1738 call term_sendkeys(buf, "\<CR>")
1739 call term_sendkeys(buf, "\<CR>")
1740 call VerifyScreenDump(buf, 'Test_popupwin_win_execute', {})
1741
1742 call StopVimInTerminal(buf)
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02001743endfunc
1744
Bram Moolenaara42d9452019-06-15 21:46:30 +02001745func ShowDialog(key, result)
1746 let s:cb_res = 999
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001747 let winid = popup_dialog('do you want to quit (Yes/no)?', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001748 \ filter: 'popup_filter_yesno',
1749 \ callback: 'QuitCallback',
Bram Moolenaara42d9452019-06-15 21:46:30 +02001750 \ })
1751 redraw
1752 call feedkeys(a:key, "xt")
1753 call assert_equal(winid, s:cb_winid)
1754 call assert_equal(a:result, s:cb_res)
1755endfunc
1756
1757func Test_popup_dialog()
1758 func QuitCallback(id, res)
1759 let s:cb_winid = a:id
1760 let s:cb_res = a:res
1761 endfunc
1762
1763 let winid = ShowDialog("y", 1)
1764 let winid = ShowDialog("Y", 1)
1765 let winid = ShowDialog("n", 0)
1766 let winid = ShowDialog("N", 0)
1767 let winid = ShowDialog("x", 0)
1768 let winid = ShowDialog("X", 0)
1769 let winid = ShowDialog("\<Esc>", 0)
1770 let winid = ShowDialog("\<C-C>", -1)
1771
1772 delfunc QuitCallback
1773endfunc
1774
Bram Moolenaara730e552019-06-16 19:05:31 +02001775func ShowMenu(key, result)
1776 let s:cb_res = 999
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001777 let winid = popup_menu(['one', 'two', 'something else'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001778 \ callback: 'QuitCallback',
Bram Moolenaara730e552019-06-16 19:05:31 +02001779 \ })
1780 redraw
1781 call feedkeys(a:key, "xt")
1782 call assert_equal(winid, s:cb_winid)
1783 call assert_equal(a:result, s:cb_res)
1784endfunc
1785
1786func Test_popup_menu()
1787 func QuitCallback(id, res)
1788 let s:cb_winid = a:id
1789 let s:cb_res = a:res
1790 endfunc
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001791 " mapping won't be used in popup
1792 map j k
Bram Moolenaara730e552019-06-16 19:05:31 +02001793
1794 let winid = ShowMenu(" ", 1)
1795 let winid = ShowMenu("j \<CR>", 2)
1796 let winid = ShowMenu("JjK \<CR>", 2)
Christian Brabandtbadeedd2023-08-13 19:25:28 +02001797 " wraps around
1798 let winid = ShowMenu("jjjjjj ", 1)
Bram Moolenaara730e552019-06-16 19:05:31 +02001799 let winid = ShowMenu("kkk ", 1)
1800 let winid = ShowMenu("x", -1)
1801 let winid = ShowMenu("X", -1)
1802 let winid = ShowMenu("\<Esc>", -1)
1803 let winid = ShowMenu("\<C-C>", -1)
1804
1805 delfunc QuitCallback
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001806 unmap j
Bram Moolenaara730e552019-06-16 19:05:31 +02001807endfunc
1808
1809func Test_popup_menu_screenshot()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001810 CheckScreendump
Bram Moolenaara730e552019-06-16 19:05:31 +02001811
1812 let lines =<< trim END
1813 call setline(1, range(1, 20))
1814 hi PopupSelected ctermbg=lightblue
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001815 call popup_menu(['one', 'two', 'another'], #{callback: 'MenuDone', title: ' make a choice from the list '})
Bram Moolenaara730e552019-06-16 19:05:31 +02001816 func MenuDone(id, res)
1817 echomsg "selected " .. a:res
1818 endfunc
1819 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01001820 call writefile(lines, 'XtestPopupMenu', 'D')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001821 let buf = RunVimInTerminal('-S XtestPopupMenu', #{rows: 10})
Bram Moolenaara730e552019-06-16 19:05:31 +02001822 call VerifyScreenDump(buf, 'Test_popupwin_menu_01', {})
1823
1824 call term_sendkeys(buf, "jj")
1825 call VerifyScreenDump(buf, 'Test_popupwin_menu_02', {})
1826
1827 call term_sendkeys(buf, " ")
1828 call VerifyScreenDump(buf, 'Test_popupwin_menu_03', {})
1829
1830 " clean up
1831 call StopVimInTerminal(buf)
Bram Moolenaara730e552019-06-16 19:05:31 +02001832endfunc
1833
Bram Moolenaarf914a332019-07-20 15:09:56 +02001834func Test_popup_menu_narrow()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001835 CheckScreendump
Bram Moolenaarf914a332019-07-20 15:09:56 +02001836
1837 let lines =<< trim END
1838 call setline(1, range(1, 20))
1839 hi PopupSelected ctermbg=green
1840 call popup_menu(['one', 'two', 'three'], #{callback: 'MenuDone'})
1841 func MenuDone(id, res)
1842 echomsg "selected " .. a:res
1843 endfunc
1844 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01001845 call writefile(lines, 'XtestPopupNarrowMenu', 'D')
Bram Moolenaarf914a332019-07-20 15:09:56 +02001846 let buf = RunVimInTerminal('-S XtestPopupNarrowMenu', #{rows: 10})
1847 call VerifyScreenDump(buf, 'Test_popupwin_menu_04', {})
1848
1849 " clean up
1850 call term_sendkeys(buf, "x")
1851 call StopVimInTerminal(buf)
Bram Moolenaarf914a332019-07-20 15:09:56 +02001852endfunc
1853
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001854func Test_popup_title()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001855 CheckScreendump
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001856
1857 " Create a popup without title or border, a line of padding will be added to
1858 " put the title on.
1859 let lines =<< trim END
1860 call setline(1, range(1, 20))
Bram Moolenaar5d458a72019-08-04 21:12:15 +02001861 let winid = popup_create(['one', 'two', 'another'], #{title: 'Title String'})
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001862 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01001863 call writefile(lines, 'XtestPopupTitle', 'D')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001864 let buf = RunVimInTerminal('-S XtestPopupTitle', #{rows: 10})
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001865 call VerifyScreenDump(buf, 'Test_popupwin_title', {})
1866
Bram Moolenaar5d458a72019-08-04 21:12:15 +02001867 call term_sendkeys(buf, ":call popup_setoptions(winid, #{maxwidth: 20, title: 'a very long title that is not going to fit'})\<CR>")
1868 call term_sendkeys(buf, ":\<CR>")
1869 call VerifyScreenDump(buf, 'Test_popupwin_longtitle_1', {})
1870
1871 call term_sendkeys(buf, ":call popup_setoptions(winid, #{border: []})\<CR>")
1872 call term_sendkeys(buf, ":\<CR>")
1873 call VerifyScreenDump(buf, 'Test_popupwin_longtitle_2', {})
1874
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01001875 call term_sendkeys(buf, ":call popup_clear()\<CR>")
1876 call term_sendkeys(buf, ":call popup_create(['aaa', 'bbb'], #{title: 'Title', minwidth: 12, padding: [2, 2, 2, 2]})\<CR>")
1877 call term_sendkeys(buf, ":\<CR>")
1878 call VerifyScreenDump(buf, 'Test_popupwin_longtitle_3', {})
1879
1880 call term_sendkeys(buf, ":call popup_clear()\<CR>")
1881 call term_sendkeys(buf, ":call popup_create(['aaa', 'bbb'], #{title: 'Title', minwidth: 12, border: [], padding: [2, 2, 2, 2]})\<CR>")
1882 call term_sendkeys(buf, ":\<CR>")
1883 call VerifyScreenDump(buf, 'Test_popupwin_longtitle_4', {})
1884
Ralf Schandlbc869872021-05-28 14:12:14 +02001885 call term_sendkeys(buf, ":call popup_clear()\<CR>")
rbtnnc6119412021-08-07 13:08:45 +02001886 call term_sendkeys(buf, ":call popup_menu(['This is a line', 'and another line'], #{title: '▶Äあいうえお◀', })\<CR>")
Ralf Schandlbc869872021-05-28 14:12:14 +02001887 call VerifyScreenDump(buf, 'Test_popupwin_multibytetitle', {})
1888 call term_sendkeys(buf, "x")
1889
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001890 " clean up
1891 call StopVimInTerminal(buf)
Bram Moolenaarae943152019-06-16 22:54:14 +02001892
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001893 let winid = popup_create('something', #{title: 'Some Title'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001894 call assert_equal('Some Title', popup_getoptions(winid).title)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001895 call popup_setoptions(winid, #{title: 'Another Title'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001896 call assert_equal('Another Title', popup_getoptions(winid).title)
1897
1898 call popup_clear()
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001899endfunc
1900
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001901func Test_popup_close_callback()
1902 func PopupDone(id, result)
1903 let g:result = a:result
1904 endfunc
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001905 let winid = popup_create('something', #{callback: 'PopupDone'})
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001906 redraw
1907 call popup_close(winid, 'done')
1908 call assert_equal('done', g:result)
1909endfunc
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001910
1911func Test_popup_empty()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001912 let winid = popup_create('', #{padding: [2,2,2,2]})
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001913 redraw
1914 let pos = popup_getpos(winid)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001915 call assert_equal(5, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001916 call assert_equal(5, pos.height)
Bram Moolenaaref6b9792020-05-13 16:34:15 +02001917 call popup_close(winid)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001918
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001919 let winid = popup_create([], #{border: []})
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001920 redraw
1921 let pos = popup_getpos(winid)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001922 call assert_equal(3, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001923 call assert_equal(3, pos.height)
Bram Moolenaaref6b9792020-05-13 16:34:15 +02001924 call popup_close(winid)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001925endfunc
Bram Moolenaar988c4332019-06-02 14:12:11 +02001926
1927func Test_popup_never_behind()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001928 CheckScreendump
1929
Bram Moolenaar988c4332019-06-02 14:12:11 +02001930 " +-----------------------------+
1931 " | | |
1932 " | | |
1933 " | | |
1934 " | line1 |
1935 " |------------line2------------|
1936 " | line3 |
1937 " | line4 |
1938 " | |
1939 " | |
1940 " +-----------------------------+
1941 let lines =<< trim END
Bram Moolenaar988c4332019-06-02 14:12:11 +02001942 split
1943 vsplit
1944 let info_window1 = getwininfo()[0]
1945 let line = info_window1['height']
1946 let col = info_window1['width']
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001947 call popup_create(['line1', 'line2', 'line3', 'line4'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001948 \ line : line,
1949 \ col : col,
Bram Moolenaar988c4332019-06-02 14:12:11 +02001950 \ })
1951 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01001952 call writefile(lines, 'XtestPopupBehind', 'D')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001953 let buf = RunVimInTerminal('-S XtestPopupBehind', #{rows: 10})
Bram Moolenaar988c4332019-06-02 14:12:11 +02001954 call term_sendkeys(buf, "\<C-W>w")
1955 call VerifyScreenDump(buf, 'Test_popupwin_behind', {})
1956
1957 " clean up
1958 call StopVimInTerminal(buf)
Bram Moolenaar988c4332019-06-02 14:12:11 +02001959endfunc
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001960
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001961func s:VerifyPosition(p, msg, line, col, width, height)
1962 call assert_equal(a:line, popup_getpos(a:p).line, a:msg . ' (l)')
1963 call assert_equal(a:col, popup_getpos(a:p).col, a:msg . ' (c)')
1964 call assert_equal(a:width, popup_getpos(a:p).width, a:msg . ' (w)')
1965 call assert_equal(a:height, popup_getpos(a:p).height, a:msg . ' (h)')
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001966endfunc
1967
1968func Test_popup_position_adjust()
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001969 " Anything placed past the last cell on the right of the screen is moved to
1970 " the left.
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001971 "
1972 " When wrapping is disabled, we also shift to the left to display on the
1973 " screen, unless fixed is set.
1974
1975 " Entries for cases which don't vary based on wrapping.
1976 " Format is per tests described below
1977 let both_wrap_tests = [
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001978 \ ['a', 5, &columns, 5, &columns, 1, 1],
1979 \ ['b', 5, &columns + 1, 5, &columns, 1, 1],
1980 \ ['c', 5, &columns - 1, 5, &columns - 1, 1, 1],
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001981 \ ['d', 5, &columns - 2, 5, &columns - 2, 1, 1],
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001982 \ ['e', 5, &columns - 3, 5, &columns - 3, 1, 1]]
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001983
1984 " these test groups are dicts with:
1985 " - comment: something to identify the group of tests by
1986 " - options: dict of options to merge with the row/col in tests
1987 " - tests: list of cases. Each one is a list with elements:
1988 " - text
1989 " - row
1990 " - col
1991 " - expected row
1992 " - expected col
1993 " - expected width
1994 " - expected height
1995 let tests = [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001996 \ #{
1997 \ comment: 'left-aligned with wrapping',
1998 \ options: #{
1999 \ wrap: 1,
2000 \ pos: 'botleft',
2001 \ },
2002 \ tests: both_wrap_tests + [
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01002003 \ ['aa', 5, &columns, 4, &columns, 1, 2],
2004 \ ['bb', 5, &columns + 1, 4, &columns, 1, 2],
2005 \ ['cc', 5, &columns - 1, 5, &columns - 1, 2, 1],
2006 \ ['dd', 5, &columns - 2, 5, &columns - 2, 2, 1],
2007 \ ['ee', 5, &columns - 3, 5, &columns - 3, 2, 1],
2008 \
2009 \ ['aaa', 5, &columns, 3, &columns, 1, 3],
2010 \ ['bbb', 5, &columns + 1, 3, &columns, 1, 3],
2011 \ ['ccc', 5, &columns - 1, 4, &columns - 1, 2, 2],
2012 \ ['ddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
2013 \ ['eee', 5, &columns - 3, 5, &columns - 3, 3, 1],
2014 \
2015 \ ['aaaa', 5, &columns, 2, &columns, 1, 4],
2016 \ ['bbbb', 5, &columns + 1, 2, &columns, 1, 4],
2017 \ ['cccc', 5, &columns - 1, 4, &columns - 1, 2, 2],
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002018 \ ['dddd', 5, &columns - 2, 4, &columns - 2, 3, 2],
2019 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01002020 \ ['eeee', 5, &columns - 4, 5, &columns - 4, 4, 1],
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002021 \ ],
2022 \ },
2023 \ #{
2024 \ comment: 'left aligned without wrapping',
2025 \ options: #{
2026 \ wrap: 0,
2027 \ pos: 'botleft',
2028 \ },
2029 \ tests: both_wrap_tests + [
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01002030 \ ['aa', 5, &columns, 5, &columns - 1, 2, 1],
2031 \ ['bb', 5, &columns + 1, 5, &columns - 1, 2, 1],
2032 \ ['cc', 5, &columns - 1, 5, &columns - 1, 2, 1],
2033 \ ['dd', 5, &columns - 2, 5, &columns - 2, 2, 1],
2034 \ ['ee', 5, &columns - 3, 5, &columns - 3, 2, 1],
2035 \
2036 \ ['aaa', 5, &columns, 5, &columns - 2, 3, 1],
2037 \ ['bbb', 5, &columns + 1, 5, &columns - 2, 3, 1],
2038 \ ['ccc', 5, &columns - 1, 5, &columns - 2, 3, 1],
2039 \ ['ddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
2040 \ ['eee', 5, &columns - 3, 5, &columns - 3, 3, 1],
2041 \
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002042 \ ['aaaa', 5, &columns, 5, &columns - 3, 4, 1],
2043 \ ['bbbb', 5, &columns + 1, 5, &columns - 3, 4, 1],
2044 \ ['cccc', 5, &columns - 1, 5, &columns - 3, 4, 1],
2045 \ ['dddd', 5, &columns - 2, 5, &columns - 3, 4, 1],
2046 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
2047 \ ],
2048 \ },
2049 \ #{
2050 \ comment: 'left aligned with fixed position',
2051 \ options: #{
2052 \ wrap: 0,
2053 \ fixed: 1,
2054 \ pos: 'botleft',
2055 \ },
2056 \ tests: both_wrap_tests + [
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01002057 \ ['aa', 5, &columns, 5, &columns, 1, 1],
2058 \ ['bb', 5, &columns + 1, 5, &columns, 1, 1],
2059 \ ['cc', 5, &columns - 1, 5, &columns - 1, 2, 1],
2060 \ ['dd', 5, &columns - 2, 5, &columns - 2, 2, 1],
2061 \ ['ee', 5, &columns - 3, 5, &columns - 3, 2, 1],
2062 \
2063 \ ['aaa', 5, &columns, 5, &columns, 1, 1],
2064 \ ['bbb', 5, &columns + 1, 5, &columns, 1, 1],
2065 \ ['ccc', 5, &columns - 1, 5, &columns - 1, 2, 1],
2066 \ ['ddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
2067 \ ['eee', 5, &columns - 3, 5, &columns - 3, 3, 1],
2068 \
2069 \ ['aaaa', 5, &columns, 5, &columns, 1, 1],
2070 \ ['bbbb', 5, &columns + 1, 5, &columns, 1, 1],
2071 \ ['cccc', 5, &columns - 1, 5, &columns - 1, 2, 1],
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002072 \ ['dddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
2073 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
2074 \ ],
2075 \ },
2076 \ ]
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002077
2078 for test_group in tests
2079 for test in test_group.tests
2080 let [ text, line, col, e_line, e_col, e_width, e_height ] = test
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002081 let options = #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002082 \ line: line,
2083 \ col: col,
2084 \ }
2085 call extend(options, test_group.options)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002086
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002087 let p = popup_create(text, options)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002088
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002089 let msg = string(extend(options, #{text: text}))
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002090 call s:VerifyPosition(p, msg, e_line, e_col, e_width, e_height)
2091 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002092 endfor
2093 endfor
2094
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002095 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002096 %bwipe!
2097endfunc
2098
Bram Moolenaar3397f742019-06-02 18:40:06 +02002099func Test_adjust_left_past_screen_width()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002100 " width of screen
2101 let X = join(map(range(&columns), {->'X'}), '')
2102
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002103 let p = popup_create(X, #{line: 1, col: 1, wrap: 0})
2104 call s:VerifyPosition(p, 'full width topleft', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002105
2106 redraw
2107 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
2108 call assert_equal(X, line)
2109
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002110 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002111 redraw
2112
2113 " Same if placed on the right hand side
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002114 let p = popup_create(X, #{line: 1, col: &columns, wrap: 0})
2115 call s:VerifyPosition(p, 'full width topright', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002116
2117 redraw
2118 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
2119 call assert_equal(X, line)
2120
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002121 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002122 redraw
2123
2124 " Extend so > window width
2125 let X .= '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 + 1 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[ : -2 ], 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 " Shifted then truncated (the x is not visible)
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002138 let p = popup_create(X, #{line: 1, col: &columns - 3, wrap: 0})
2139 call s:VerifyPosition(p, 'full width + 1 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[ : -2 ], 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 " Not shifted, just truncated
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002149 let p = popup_create(X,
2150 \ #{line: 1, col: 2, wrap: 0, fixed: 1})
2151 call s:VerifyPosition(p, 'full width + 1 fixed', 1, 2, &columns - 1, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002152
2153 redraw
2154 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
2155 let e_line = ' ' . X[ 1 : -2 ]
2156 call assert_equal(e_line, line)
2157
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002158 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002159 redraw
2160
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002161 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002162 %bwipe!
Bram Moolenaar3397f742019-06-02 18:40:06 +02002163endfunc
2164
2165func Test_popup_moved()
2166 new
2167 call test_override('char_avail', 1)
2168 call setline(1, ['one word to move around', 'a WORD.and->some thing'])
2169
2170 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002171 let winid = popup_atcursor('text', #{moved: 'any'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02002172 redraw
2173 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002174 call assert_equal([1, 4, 4], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02002175 " trigger the check for last_cursormoved by going into insert mode
2176 call feedkeys("li\<Esc>", 'xt')
2177 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002178 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02002179
2180 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002181 let winid = popup_atcursor('text', #{moved: 'word'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02002182 redraw
2183 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002184 call assert_equal([1, 4, 7], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02002185 call feedkeys("hi\<Esc>", 'xt')
2186 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002187 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02002188
2189 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002190 let winid = popup_atcursor('text', #{moved: 'word'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02002191 redraw
2192 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002193 call assert_equal([1, 4, 7], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02002194 call feedkeys("li\<Esc>", 'xt')
2195 call assert_equal(1, popup_getpos(winid).visible)
2196 call feedkeys("ei\<Esc>", 'xt')
2197 call assert_equal(1, popup_getpos(winid).visible)
2198 call feedkeys("eli\<Esc>", 'xt')
2199 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002200 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02002201
Bram Moolenaar17627312019-06-02 19:53:44 +02002202 " WORD is the default
Bram Moolenaar3397f742019-06-02 18:40:06 +02002203 exe "normal gg0/WORD\<CR>"
Bram Moolenaar17627312019-06-02 19:53:44 +02002204 let winid = popup_atcursor('text', {})
Bram Moolenaar3397f742019-06-02 18:40:06 +02002205 redraw
2206 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002207 call assert_equal([2, 2, 15], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02002208 call feedkeys("eli\<Esc>", 'xt')
2209 call assert_equal(1, popup_getpos(winid).visible)
2210 call feedkeys("wi\<Esc>", 'xt')
2211 call assert_equal(1, popup_getpos(winid).visible)
2212 call feedkeys("Eli\<Esc>", 'xt')
2213 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002214 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02002215
2216 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002217 let winid = popup_atcursor('text', #{moved: [5, 10]})
Bram Moolenaar3397f742019-06-02 18:40:06 +02002218 redraw
2219 call assert_equal(1, popup_getpos(winid).visible)
2220 call feedkeys("eli\<Esc>", 'xt')
2221 call feedkeys("ei\<Esc>", 'xt')
2222 call assert_equal(1, popup_getpos(winid).visible)
2223 call feedkeys("eli\<Esc>", 'xt')
2224 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002225 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02002226
2227 bwipe!
2228 call test_override('ALL', 0)
2229endfunc
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002230
2231func Test_notifications()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002232 CheckFeature timers
2233 CheckScreendump
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002234
Bram Moolenaar0fdddee2019-09-01 15:26:23 +02002235 let lines =<< trim END
2236 call setline(1, range(1, 20))
2237 hi Notification ctermbg=lightblue
2238 call popup_notification('first notification', {})
2239 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01002240 call writefile(lines, 'XtestNotifications', 'D')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002241 let buf = RunVimInTerminal('-S XtestNotifications', #{rows: 10})
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002242 call VerifyScreenDump(buf, 'Test_popupwin_notify_01', {})
2243
2244 " second one goes below the first one
Bram Moolenaardfa97f22019-06-15 14:31:55 +02002245 call term_sendkeys(buf, ":hi link PopupNotification Notification\<CR>")
2246 call term_sendkeys(buf, ":call popup_notification('another important notification', {})\<CR>")
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002247 call VerifyScreenDump(buf, 'Test_popupwin_notify_02', {})
2248
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002249 " clean up
2250 call StopVimInTerminal(buf)
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002251endfunc
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002252
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002253func Test_popup_scrollbar()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002254 CheckScreendump
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002255
2256 let lines =<< trim END
2257 call setline(1, range(1, 20))
Bram Moolenaar8da41812019-06-26 18:04:54 +02002258 hi ScrollThumb ctermbg=blue
2259 hi ScrollBar ctermbg=red
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002260 let winid = popup_create(['one', 'two', 'three', 'four', 'five',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002261 \ 'six', 'seven', 'eight', 'nine'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002262 \ minwidth: 8,
2263 \ maxheight: 4,
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002264 \ })
Bram Moolenaar53a95d62019-06-26 03:54:08 +02002265 func ScrollUp()
2266 call feedkeys("\<F3>\<ScrollWheelUp>", "xt")
2267 endfunc
2268 func ScrollDown()
2269 call feedkeys("\<F3>\<ScrollWheelDown>", "xt")
2270 endfunc
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02002271 func ClickTop()
2272 call feedkeys("\<F4>\<LeftMouse>", "xt")
2273 endfunc
2274 func ClickBot()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002275 call popup_setoptions(g:winid, #{border: [], close: 'button'})
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02002276 call feedkeys("\<F5>\<LeftMouse>", "xt")
2277 endfunc
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002278 func Popup_filter(winid, key)
2279 if a:key == 'j'
Bram Moolenaar371806e2020-10-22 13:44:54 +02002280 silent! this_throws_an_error_but_is_ignored
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002281 let line = popup_getoptions(a:winid).firstline
2282 let nlines = line('$', a:winid)
2283 let newline = line < nlines ? (line + 1) : nlines
2284 call popup_setoptions(a:winid, #{firstline: newline})
2285 return v:true
2286 elseif a:key == 'x'
2287 call popup_close(a:winid)
2288 return v:true
2289 endif
2290 endfunc
2291
Bram Moolenaarfc376e02022-05-29 18:18:18 +01002292 def CreatePopup(text: list<string>): number
2293 return popup_create(text, {
Bram Moolenaar6c542f72020-09-27 21:16:45 +02002294 \ minwidth: 30,
2295 \ maxwidth: 30,
2296 \ minheight: 4,
2297 \ maxheight: 4,
2298 \ firstline: 1,
2299 \ lastline: 4,
2300 \ wrap: true,
2301 \ scrollbar: true,
2302 \ mapping: false,
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002303 \ filter: g:Popup_filter,
Bram Moolenaar6c542f72020-09-27 21:16:45 +02002304 \ })
2305 enddef
2306
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002307 func PopupScroll()
2308 call popup_clear()
2309 let text =<< trim END
2310 1
2311 2
2312 3
2313 4
2314 long line long line long line long line long line long line
2315 long line long line long line long line long line long line
2316 long line long line long line long line long line long line
2317 END
Bram Moolenaar6c542f72020-09-27 21:16:45 +02002318 call CreatePopup(text)
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002319 endfunc
Bram Moolenaarfc376e02022-05-29 18:18:18 +01002320 func ScrollBottom()
2321 call popup_clear()
Bram Moolenaar89b25582022-05-30 13:20:56 +01002322 let id = CreatePopup(range(100)->map({k, v -> string(v)}))
2323 call popup_setoptions(id, #{firstline: 100, minheight: 9, maxheight: 9})
Bram Moolenaarfc376e02022-05-29 18:18:18 +01002324 endfunc
Bram Moolenaar53a95d62019-06-26 03:54:08 +02002325 map <silent> <F3> :call test_setmouse(5, 36)<CR>
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02002326 map <silent> <F4> :call test_setmouse(4, 42)<CR>
2327 map <silent> <F5> :call test_setmouse(7, 42)<CR>
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002328 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01002329 call writefile(lines, 'XtestPopupScroll', 'D')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002330 let buf = RunVimInTerminal('-S XtestPopupScroll', #{rows: 10})
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002331 call VerifyScreenDump(buf, 'Test_popupwin_scroll_1', {})
2332
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002333 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 2})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002334 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002335 call VerifyScreenDump(buf, 'Test_popupwin_scroll_2', {})
2336
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002337 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 6})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002338 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002339 call VerifyScreenDump(buf, 'Test_popupwin_scroll_3', {})
2340
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002341 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 9})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002342 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002343 call VerifyScreenDump(buf, 'Test_popupwin_scroll_4', {})
2344
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02002345 call term_sendkeys(buf, ":call popup_setoptions(winid, #{scrollbarhighlight: 'ScrollBar', thumbhighlight: 'ScrollThumb', firstline: 5})\<CR>")
Bram Moolenaara112f2d2019-09-01 17:38:09 +02002346 " this scrolls two lines (half the window height)
Bram Moolenaar53a95d62019-06-26 03:54:08 +02002347 call term_sendkeys(buf, ":call ScrollUp()\<CR>")
2348 call VerifyScreenDump(buf, 'Test_popupwin_scroll_5', {})
2349
2350 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
2351 call VerifyScreenDump(buf, 'Test_popupwin_scroll_6', {})
2352
2353 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
Bram Moolenaar13b47c32019-06-28 21:55:48 +02002354 " wait a bit, otherwise it fails sometimes (double click recognized?)
2355 sleep 100m
Bram Moolenaar53a95d62019-06-26 03:54:08 +02002356 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
2357 call VerifyScreenDump(buf, 'Test_popupwin_scroll_7', {})
2358
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02002359 call term_sendkeys(buf, ":call ClickTop()\<CR>")
2360 sleep 100m
2361 call term_sendkeys(buf, ":call ClickTop()\<CR>")
2362 call VerifyScreenDump(buf, 'Test_popupwin_scroll_8', {})
2363
2364 call term_sendkeys(buf, ":call ClickBot()\<CR>")
2365 call VerifyScreenDump(buf, 'Test_popupwin_scroll_9', {})
2366
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02002367 " remove the minwidth and maxheight
2368 call term_sendkeys(buf, ":call popup_setoptions(winid, #{maxheight: 0, minwidth: 0})\<CR>")
Bram Moolenaar7e0f4622019-09-17 21:23:39 +02002369 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02002370 call VerifyScreenDump(buf, 'Test_popupwin_scroll_10', {})
2371
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002372 " check size with non-wrapping lines
Bram Moolenaar848fadd2022-01-30 15:28:30 +00002373 call term_sendkeys(buf, ":call g:PopupScroll()\<CR>")
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002374 call VerifyScreenDump(buf, 'Test_popupwin_scroll_11', {})
2375
2376 " check size with wrapping lines
2377 call term_sendkeys(buf, "j")
2378 call VerifyScreenDump(buf, 'Test_popupwin_scroll_12', {})
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002379
Bram Moolenaarfc376e02022-05-29 18:18:18 +01002380 " check thumb when scrolled all the way down
2381 call term_sendkeys(buf, ":call ScrollBottom()\<CR>")
2382 call VerifyScreenDump(buf, 'Test_popupwin_scroll_13', {})
2383
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002384 " clean up
Bram Moolenaar20298ce2020-06-19 21:46:52 +02002385 call term_sendkeys(buf, "x")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002386 call StopVimInTerminal(buf)
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002387endfunc
2388
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02002389func Test_popup_too_high_scrollbar()
2390 CheckScreendump
2391
2392 let lines =<< trim END
2393 call setline(1, range(1, 20)->map({i, v -> repeat(v, 10)}))
2394 set scrolloff=0
2395 func ShowPopup()
2396 let winid = popup_atcursor(['one', 'two', 'three', 'four', 'five',
2397 \ 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve'], #{
2398 \ minwidth: 8,
2399 \ border: [],
2400 \ })
2401 endfunc
2402 normal 3G$
2403 call ShowPopup()
2404 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01002405 call writefile(lines, 'XtestPopupToohigh', 'D')
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02002406 let buf = RunVimInTerminal('-S XtestPopupToohigh', #{rows: 10})
2407 call VerifyScreenDump(buf, 'Test_popupwin_toohigh_1', {})
2408
2409 call term_sendkeys(buf, ":call popup_clear()\<CR>")
2410 call term_sendkeys(buf, "8G$")
2411 call term_sendkeys(buf, ":call ShowPopup()\<CR>")
2412 call VerifyScreenDump(buf, 'Test_popupwin_toohigh_2', {})
2413
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02002414 call term_sendkeys(buf, ":call popup_clear()\<CR>")
2415 call term_sendkeys(buf, "gg$")
2416 call term_sendkeys(buf, ":call ShowPopup()\<CR>")
2417 call VerifyScreenDump(buf, 'Test_popupwin_toohigh_3', {})
2418
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02002419 " clean up
2420 call StopVimInTerminal(buf)
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02002421endfunc
2422
Bram Moolenaar437a7462019-07-05 20:17:22 +02002423func Test_popup_fitting_scrollbar()
2424 " this was causing a crash, divide by zero
2425 let winid = popup_create([
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002426 \ 'one', 'two', 'longer line that wraps', 'four', 'five'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002427 \ scrollbar: 1,
2428 \ maxwidth: 10,
2429 \ maxheight: 5,
2430 \ firstline: 2})
Bram Moolenaar437a7462019-07-05 20:17:22 +02002431 redraw
2432 call popup_clear()
2433endfunc
2434
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002435func Test_popup_settext()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002436 CheckScreendump
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002437
2438 let lines =<< trim END
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002439 let opts = #{wrap: 0}
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002440 let p = popup_create('test', opts)
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002441 eval p->popup_settext('this is a text')
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002442 END
2443
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01002444 call writefile(lines, 'XtestPopupSetText', 'D')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002445 let buf = RunVimInTerminal('-S XtestPopupSetText', #{rows: 10})
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002446 call VerifyScreenDump(buf, 'Test_popup_settext_01', {})
2447
2448 " Setting to empty string clears it
2449 call term_sendkeys(buf, ":call popup_settext(p, '')\<CR>")
2450 call VerifyScreenDump(buf, 'Test_popup_settext_02', {})
2451
2452 " Setting a list
2453 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
2454 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
2455
2456 " Shrinking with a list
2457 call term_sendkeys(buf, ":call popup_settext(p, ['a'])\<CR>")
2458 call VerifyScreenDump(buf, 'Test_popup_settext_04', {})
2459
2460 " Growing with a list
2461 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
2462 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
2463
2464 " Empty list clears
2465 call term_sendkeys(buf, ":call popup_settext(p, [])\<CR>")
2466 call VerifyScreenDump(buf, 'Test_popup_settext_05', {})
2467
2468 " Dicts
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002469 call term_sendkeys(buf, ":call popup_settext(p, [#{text: 'aaaa'}, #{text: 'bbbb'}, #{text: 'cccc'}])\<CR>")
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002470 call VerifyScreenDump(buf, 'Test_popup_settext_06', {})
2471
Bram Moolenaarb0992022020-01-30 14:55:42 +01002472 " range() (doesn't work)
2473 call term_sendkeys(buf, ":call popup_settext(p, range(4, 8))\<CR>")
2474 call VerifyScreenDump(buf, 'Test_popup_settext_07', {})
2475
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002476 " clean up
2477 call StopVimInTerminal(buf)
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002478endfunc
2479
Bram Moolenaar1f42f5a2020-09-03 18:52:24 +02002480func Test_popup_settext_getline()
2481 let id = popup_create('', #{ tabpage: 0 })
2482 call popup_settext(id, ['a','b'])
2483 call assert_equal(2, line('$', id)) " OK :)
2484 call popup_close(id)
2485
2486 let id = popup_create('', #{ tabpage: -1 })
2487 call popup_settext(id, ['a','b'])
2488 call assert_equal(2, line('$', id)) " Fails :(
2489 call popup_close(id)
2490endfunc
2491
Bram Moolenaar74f8eec2020-10-15 19:10:56 +02002492func Test_popup_settext_null()
2493 let id = popup_create('', #{ tabpage: 0 })
2494 call popup_settext(id, test_null_list())
2495 call popup_close(id)
2496
2497 let id = popup_create('', #{ tabpage: 0 })
2498 call popup_settext(id, test_null_string())
2499 call popup_close(id)
2500endfunc
2501
Christian Brabandtfbc37f12024-06-18 20:50:58 +02002502func Test_popup_setbuf()
2503 CheckScreendump
2504
2505 let lines =<< trim END
2506 let opts = #{wrap: 0}
2507 let p = popup_create('test', opts)
2508 let buf = bufnr('%')
2509 END
2510
2511 call writefile(lines, 'XtestPopupSetBuf', 'D')
2512 let buf = RunVimInTerminal('-S XtestPopupSetBuf', #{rows: 10})
2513 call VerifyScreenDump(buf, 'Test_popup_setbuf_01', {})
2514
2515 " Setting to an non-existing buffer doesn't do anything
2516 call term_sendkeys(buf, ":call popup_setbuf(p, 'foobar.txt')\<CR>")
2517 call VerifyScreenDump(buf, 'Test_popup_setbuf_02', {})
2518
2519 " Error
2520 call term_sendkeys(buf, ":call popup_setbuf(p, ['a','b','c'])\<CR>")
2521 call VerifyScreenDump(buf, 'Test_popup_setbuf_03', {})
2522
2523 " Set to help window
2524 call term_sendkeys(buf, ":help\<CR>")
2525 call term_sendkeys(buf, ":call popup_setbuf(p, 'help.txt')\<CR>")
2526 call VerifyScreenDump(buf, 'Test_popup_setbuf_04', {})
2527
2528 " Setting back to original buffer
2529 call term_sendkeys(buf, ":call popup_setbuf(p, buf)\<CR>")
2530 call VerifyScreenDump(buf, 'Test_popup_setbuf_05', {})
2531
2532 " use method
2533 call term_sendkeys(buf, ":echo p->popup_setbuf('help.txt')\<CR>")
2534 call VerifyScreenDump(buf, 'Test_popup_setbuf_06', {})
2535
2536 call term_sendkeys(buf, ":echo p->popup_setbuf(buf)\<CR>")
2537 call VerifyScreenDump(buf, 'Test_popup_setbuf_05', {})
2538
2539 " clean up
2540 call StopVimInTerminal(buf)
2541endfunc
2542
2543func Test_popup_setbuf_terminal()
2544 CheckFeature terminal
2545
2546 " Check Terminal Feature
2547 let termbuf = term_start(&shell, #{hidden: 1})
2548 " Wait for shell to start
2549 call WaitForAssert({-> assert_equal("run", job_status(term_getjob(termbuf)))})
2550
2551 let popup = popup_create('test', {})
2552 call assert_true(popup->popup_setbuf(termbuf))
2553 call popup_close(popup)
2554
2555 let popup1 = popup_create(termbuf, #{minwidth: 40, minheight: 10, border: []})
2556
2557 let popup = popup_create('test', {})
2558 try
2559 call assert_fails(call popup_setbuf(popup, termbuf))
2560 catch
2561 endtry
2562 call popup_close(popup)
2563 call popup_close(popup1)
2564 call assert_equal([], popup_list())
2565 " Close the terminal
2566 call term_sendkeys(termbuf, "exit\<CR>")
2567 " Wait for shell to exit
2568 call WaitForAssert({-> assert_equal("dead", job_status(term_getjob(termbuf)))})
2569endfunc
2570
2571func Test_popup_setbuf_null()
2572 let id = popup_create('', {})
2573 call assert_false(popup_setbuf(id, -1))
2574 call popup_close(id)
2575
2576 let id = popup_create('', {})
2577 call assert_true(popup_setbuf(id, test_null_string()))
2578 call assert_true(popup_setbuf(id, ''))
2579 call popup_close(id)
2580
2581 call assert_false(popup_setbuf(id, 0))
2582endfunc
2583
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002584func Test_popup_hidden()
2585 new
2586
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002587 let winid = popup_atcursor('text', #{hidden: 1})
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002588 redraw
2589 call assert_equal(0, popup_getpos(winid).visible)
2590 call popup_close(winid)
2591
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002592 let winid = popup_create('text', #{hidden: 1})
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002593 redraw
2594 call assert_equal(0, popup_getpos(winid).visible)
2595 call popup_close(winid)
2596
2597 func QuitCallback(id, res)
2598 let s:cb_winid = a:id
2599 let s:cb_res = a:res
2600 endfunc
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002601 let winid = 'make a choice'->popup_dialog(#{hidden: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002602 \ filter: 'popup_filter_yesno',
2603 \ callback: 'QuitCallback',
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002604 \ })
2605 redraw
2606 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarae943152019-06-16 22:54:14 +02002607 call assert_equal(function('popup_filter_yesno'), popup_getoptions(winid).filter)
2608 call assert_equal(function('QuitCallback'), popup_getoptions(winid).callback)
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002609 exe "normal anot used by filter\<Esc>"
2610 call assert_equal('not used by filter', getline(1))
2611
2612 call popup_show(winid)
2613 call feedkeys('y', "xt")
2614 call assert_equal(1, s:cb_res)
2615
2616 bwipe!
2617 delfunc QuitCallback
2618endfunc
Bram Moolenaarae943152019-06-16 22:54:14 +02002619
2620" Test options not checked elsewhere
2621func Test_set_get_options()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002622 let winid = popup_create('some text', #{highlight: 'Beautiful'})
Bram Moolenaarae943152019-06-16 22:54:14 +02002623 let options = popup_getoptions(winid)
2624 call assert_equal(1, options.wrap)
2625 call assert_equal(0, options.drag)
2626 call assert_equal('Beautiful', options.highlight)
2627
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002628 call popup_setoptions(winid, #{wrap: 0, drag: 1, highlight: 'Another'})
Bram Moolenaarae943152019-06-16 22:54:14 +02002629 let options = popup_getoptions(winid)
2630 call assert_equal(0, options.wrap)
2631 call assert_equal(1, options.drag)
2632 call assert_equal('Another', options.highlight)
2633
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01002634 call assert_fails('call popup_setoptions(winid, [])', 'E1206:')
2635 call assert_fails('call popup_setoptions(winid, test_null_dict())', 'E1297:')
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02002636
Bram Moolenaarae943152019-06-16 22:54:14 +02002637 call popup_close(winid)
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02002638 call assert_equal(0, popup_setoptions(winid, options.wrap))
Bram Moolenaarae943152019-06-16 22:54:14 +02002639endfunc
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002640
2641func Test_popupwin_garbage_collect()
2642 func MyPopupFilter(x, winid, c)
2643 " NOP
2644 endfunc
2645
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002646 let winid = popup_create('something', #{filter: function('MyPopupFilter', [{}])})
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002647 call test_garbagecollect_now()
2648 redraw
Bram Moolenaar1bc353b2019-09-01 14:45:28 +02002649 " Must not crash caused by invalid memory access
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002650 call feedkeys('j', 'xt')
2651 call assert_true(v:true)
2652
2653 call popup_close(winid)
2654 delfunc MyPopupFilter
2655endfunc
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002656
Bram Moolenaar581ba392019-09-03 22:08:33 +02002657func Test_popupwin_filter_mode()
2658 func MyPopupFilter(winid, c)
2659 let s:typed = a:c
2660 if a:c == ':' || a:c == "\r" || a:c == 'v'
2661 " can start cmdline mode, get out, and start/stop Visual mode
2662 return 0
2663 endif
2664 return 1
2665 endfunc
2666
2667 " Normal, Visual and Insert mode
2668 let winid = popup_create('something', #{filter: 'MyPopupFilter', filtermode: 'nvi'})
2669 redraw
2670 call feedkeys('x', 'xt')
2671 call assert_equal('x', s:typed)
2672
2673 call feedkeys(":let g:foo = 'foo'\<CR>", 'xt')
2674 call assert_equal(':', s:typed)
2675 call assert_equal('foo', g:foo)
2676
2677 let @x = 'something'
2678 call feedkeys('v$"xy', 'xt')
2679 call assert_equal('y', s:typed)
2680 call assert_equal('something', @x) " yank command is filtered out
2681 call feedkeys('v', 'xt') " end Visual mode
2682
2683 call popup_close(winid)
2684
2685 " only Normal mode
2686 let winid = popup_create('something', #{filter: 'MyPopupFilter', filtermode: 'n'})
2687 redraw
2688 call feedkeys('x', 'xt')
2689 call assert_equal('x', s:typed)
2690
2691 call feedkeys(":let g:foo = 'foo'\<CR>", 'xt')
2692 call assert_equal(':', s:typed)
2693 call assert_equal('foo', g:foo)
2694
2695 let @x = 'something'
2696 call feedkeys('v$"xy', 'xt')
2697 call assert_equal('v', s:typed)
2698 call assert_notequal('something', @x)
2699
2700 call popup_close(winid)
2701
2702 " default: all modes
2703 let winid = popup_create('something', #{filter: 'MyPopupFilter'})
2704 redraw
2705 call feedkeys('x', 'xt')
2706 call assert_equal('x', s:typed)
2707
2708 let g:foo = 'bar'
2709 call feedkeys(":let g:foo = 'foo'\<CR>", 'xt')
2710 call assert_equal("\r", s:typed)
2711 call assert_equal('bar', g:foo)
2712
2713 let @x = 'something'
2714 call feedkeys('v$"xy', 'xt')
2715 call assert_equal('y', s:typed)
2716 call assert_equal('something', @x) " yank command is filtered out
2717 call feedkeys('v', 'xt') " end Visual mode
2718
2719 call popup_close(winid)
2720 delfunc MyPopupFilter
2721endfunc
2722
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002723func Test_popupwin_filter_mouse()
2724 func MyPopupFilter(winid, c)
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002725 let g:got_mousepos = getmousepos()
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002726 return 0
2727 endfunc
2728
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002729 call setline(1, ['.'->repeat(25)]->repeat(10))
2730 let winid = popup_create(['short', 'long line that will wrap', 'other'], #{
2731 \ line: 2,
2732 \ col: 4,
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002733 \ maxwidth: 12,
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002734 \ padding: [],
2735 \ border: [],
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002736 \ filter: 'MyPopupFilter',
2737 \ })
2738 redraw
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002739 " 123456789012345678901
2740 " 1 .....................
2741 " 2 ...+--------------+..
2742 " 3 ...| |..
2743 " 4 ...| short |..
2744 " 5 ...| long line th |..
2745 " 6 ...| at will wrap |..
2746 " 7 ...| other |..
2747 " 8 ...| |..
2748 " 9 ...+--------------+..
2749 " 10 .....................
2750 let tests = []
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002751
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002752 func AddItemOutsidePopup(tests, row, col)
2753 eval a:tests->add(#{clickrow: a:row, clickcol: a:col, result: #{
2754 \ screenrow: a:row, screencol: a:col,
2755 \ winid: win_getid(), winrow: a:row, wincol: a:col,
zeertzjqf5a94d52023-10-15 10:03:30 +02002756 \ line: a:row, column: a:col, coladd: 0,
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002757 \ }})
2758 endfunc
2759 func AddItemInPopupBorder(tests, winid, row, col)
2760 eval a:tests->add(#{clickrow: a:row, clickcol: a:col, result: #{
2761 \ screenrow: a:row, screencol: a:col,
2762 \ winid: a:winid, winrow: a:row - 1, wincol: a:col - 3,
zeertzjqf5a94d52023-10-15 10:03:30 +02002763 \ line: 0, column: 0, coladd: 0,
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002764 \ }})
2765 endfunc
zeertzjqf5a94d52023-10-15 10:03:30 +02002766 func AddItemInPopupText(tests, winid, row, col, textline, textcol, coladd = 0)
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002767 eval a:tests->add(#{clickrow: a:row, clickcol: a:col, result: #{
2768 \ screenrow: a:row, screencol: a:col,
2769 \ winid: a:winid, winrow: a:row - 1, wincol: a:col - 3,
zeertzjqf5a94d52023-10-15 10:03:30 +02002770 \ line: a:textline, column: a:textcol, coladd: a:coladd,
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002771 \ }})
2772 endfunc
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002773
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002774 " above and below popup
2775 for c in range(1, 21)
2776 call AddItemOutsidePopup(tests, 1, c)
2777 call AddItemOutsidePopup(tests, 10, c)
2778 endfor
2779 " left and right of popup
2780 for r in range(1, 10)
2781 call AddItemOutsidePopup(tests, r, 3)
2782 call AddItemOutsidePopup(tests, r, 20)
2783 endfor
2784 " top and bottom in popup
2785 for c in range(4, 19)
2786 call AddItemInPopupBorder(tests, winid, 2, c)
2787 call AddItemInPopupBorder(tests, winid, 3, c)
2788 call AddItemInPopupBorder(tests, winid, 8, c)
2789 call AddItemInPopupBorder(tests, winid, 9, c)
2790 endfor
2791 " left and right margin in popup
2792 for r in range(2, 9)
2793 call AddItemInPopupBorder(tests, winid, r, 4)
2794 call AddItemInPopupBorder(tests, winid, r, 5)
2795 call AddItemInPopupBorder(tests, winid, r, 18)
2796 call AddItemInPopupBorder(tests, winid, r, 19)
2797 endfor
2798 " text "short"
2799 call AddItemInPopupText(tests, winid, 4, 6, 1, 1)
2800 call AddItemInPopupText(tests, winid, 4, 10, 1, 5)
2801 call AddItemInPopupText(tests, winid, 4, 11, 1, 6)
zeertzjqf5a94d52023-10-15 10:03:30 +02002802 call AddItemInPopupText(tests, winid, 4, 17, 1, 6, 6)
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002803 " text "long line th"
2804 call AddItemInPopupText(tests, winid, 5, 6, 2, 1)
2805 call AddItemInPopupText(tests, winid, 5, 10, 2, 5)
2806 call AddItemInPopupText(tests, winid, 5, 17, 2, 12)
2807 " text "at will wrap"
2808 call AddItemInPopupText(tests, winid, 6, 6, 2, 13)
2809 call AddItemInPopupText(tests, winid, 6, 10, 2, 17)
2810 call AddItemInPopupText(tests, winid, 6, 17, 2, 24)
2811 " text "other"
2812 call AddItemInPopupText(tests, winid, 7, 6, 3, 1)
2813 call AddItemInPopupText(tests, winid, 7, 10, 3, 5)
2814 call AddItemInPopupText(tests, winid, 7, 11, 3, 6)
zeertzjqf5a94d52023-10-15 10:03:30 +02002815 call AddItemInPopupText(tests, winid, 7, 17, 3, 6, 6)
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002816
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002817 for item in tests
2818 call test_setmouse(item.clickrow, item.clickcol)
2819 call feedkeys("\<LeftMouse>", 'xt')
2820 call assert_equal(item.result, g:got_mousepos)
2821 endfor
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002822
2823 call popup_close(winid)
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002824 enew!
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002825 delfunc MyPopupFilter
2826endfunc
2827
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002828func Test_popupwin_with_buffer()
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01002829 call writefile(['some text', 'in a buffer'], 'XsomeFile', 'D')
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002830 let buf = bufadd('XsomeFile')
2831 call assert_equal(0, bufloaded(buf))
Bram Moolenaar46451042019-08-24 15:50:46 +02002832
2833 setlocal number
2834 call setbufvar(buf, "&wrapmargin", 13)
2835
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002836 let winid = popup_create(buf, {})
2837 call assert_notequal(0, winid)
2838 let pos = popup_getpos(winid)
2839 call assert_equal(2, pos.height)
2840 call assert_equal(1, bufloaded(buf))
Bram Moolenaar46451042019-08-24 15:50:46 +02002841
2842 " window-local option is set to default, buffer-local is not
2843 call assert_equal(0, getwinvar(winid, '&number'))
2844 call assert_equal(13, getbufvar(buf, '&wrapmargin'))
2845
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002846 call popup_close(winid)
2847 call assert_equal({}, popup_getpos(winid))
2848 call assert_equal(1, bufloaded(buf))
2849 exe 'bwipe! ' .. buf
Bram Moolenaar46451042019-08-24 15:50:46 +02002850 setlocal nonumber
Bram Moolenaar7866b872019-07-01 22:21:01 +02002851
2852 edit test_popupwin.vim
2853 let winid = popup_create(bufnr(''), {})
2854 redraw
2855 call popup_close(winid)
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002856endfunc
Bram Moolenaare296e312019-07-03 23:20:18 +02002857
Bram Moolenaar188639d2022-04-04 16:57:21 +01002858func Test_popupwin_buffer_with_swapfile()
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01002859 call writefile(['some text', 'in a buffer'], 'XopenFile', 'D')
2860 call writefile([''], '.XopenFile.swp', 'D')
Bram Moolenaar188639d2022-04-04 16:57:21 +01002861 let g:ignoreSwapExists = 1
2862
2863 let bufnr = bufadd('XopenFile')
2864 call assert_equal(0, bufloaded(bufnr))
2865 let winid = popup_create(bufnr, {'hidden': 1})
2866 call assert_equal(1, bufloaded(bufnr))
2867 call popup_close(winid)
2868
2869 exe 'buffer ' .. bufnr
2870 call assert_equal(1, &readonly)
2871 bwipe!
2872
Bram Moolenaar188639d2022-04-04 16:57:21 +01002873 unlet g:ignoreSwapExists
2874endfunc
2875
Bram Moolenaare0d749a2019-09-25 22:14:48 +02002876func Test_popupwin_terminal_buffer()
Bram Moolenaard2c1fb42019-09-25 23:06:40 +02002877 CheckFeature terminal
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002878 CheckUnix
Bram Moolenaare06a28f2020-05-13 23:24:12 +02002879 " Starting a terminal to run a shell in is considered flaky.
2880 let g:test_is_flaky = 1
Bram Moolenaard2c1fb42019-09-25 23:06:40 +02002881
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002882 let origwin = win_getid()
Bram Moolenaar349f6092020-10-06 20:46:49 +02002883
2884 " open help window to test that :help below fails
2885 help
2886
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002887 let termbuf = term_start(&shell, #{hidden: 1})
Bram Moolenaarf5452692020-11-28 21:56:06 +01002888 let winid = popup_create(termbuf, #{minwidth: 40, minheight: 10, border: []})
Bram Moolenaar0353f562020-12-17 22:27:38 +01002889 " Wait for shell to start
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002890 call WaitForAssert({-> assert_equal("run", job_status(term_getjob(termbuf)))})
Bram Moolenaar0353f562020-12-17 22:27:38 +01002891 " Wait for a prompt (see border char first, then space after prompt)
2892 call WaitForAssert({ -> assert_equal(' ', screenstring(screenrow(), screencol() - 1))})
Bram Moolenaarf5452692020-11-28 21:56:06 +01002893
2894 " When typing a character, the cursor is after it.
2895 call feedkeys("x", 'xt')
Bram Moolenaar0353f562020-12-17 22:27:38 +01002896 call term_wait(termbuf)
Bram Moolenaarf5452692020-11-28 21:56:06 +01002897 redraw
2898 call WaitForAssert({ -> assert_equal('x', screenstring(screenrow(), screencol() - 1))})
2899 call feedkeys("\<BS>", 'xt')
2900
Bram Moolenaar631ebc42020-02-03 22:15:26 +01002901 " Check this doesn't crash
2902 call assert_equal(winnr(), winnr('j'))
2903 call assert_equal(winnr(), winnr('k'))
2904 call assert_equal(winnr(), winnr('h'))
2905 call assert_equal(winnr(), winnr('l'))
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02002906
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002907 " Cannot quit while job is running
2908 call assert_fails('call feedkeys("\<C-W>:quit\<CR>", "xt")', 'E948:')
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02002909
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002910 " Cannot enter Terminal-Normal mode. (TODO: but it works...)
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02002911 call feedkeys("xxx\<C-W>N", 'xt')
2912 call assert_fails('call feedkeys("gf", "xt")', 'E863:')
2913 call feedkeys("a\<C-U>", 'xt')
2914
Bram Moolenaar3f65c662020-05-27 23:15:16 +02002915 " Cannot escape from terminal window
2916 call assert_fails('tab drop xxx', 'E863:')
Bram Moolenaar349f6092020-10-06 20:46:49 +02002917 call assert_fails('help', 'E994:')
Bram Moolenaar3f65c662020-05-27 23:15:16 +02002918
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002919 " Cannot open a second one.
2920 let termbuf2 = term_start(&shell, #{hidden: 1})
2921 call assert_fails('call popup_create(termbuf2, #{})', 'E861:')
2922 call term_sendkeys(termbuf2, "exit\<CR>")
2923
Bram Moolenaard28950f2022-05-29 14:13:04 +01002924 " Exiting shell puts popup window in Terminal-Normal mode.
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002925 call feedkeys("exit\<CR>", 'xt')
2926 " Wait for shell to exit
Bram Moolenaarb2b218d2020-06-22 20:22:19 +02002927 call WaitForAssert({-> assert_equal("dead", job_status(term_getjob(termbuf)))})
Bram Moolenaar373c6512020-06-22 19:24:23 +02002928
Bram Moolenaar349f6092020-10-06 20:46:49 +02002929 helpclose
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002930 call feedkeys(":quit\<CR>", 'xt')
2931 call assert_equal(origwin, win_getid())
Bram Moolenaare0d749a2019-09-25 22:14:48 +02002932endfunc
2933
Bram Moolenaar9e636b92022-05-29 22:37:05 +01002934func Test_popupwin_terminal_buffer_none()
2935 CheckFeature terminal
2936 CheckUnix
2937
2938 " Starting a terminal to run a shell in is considered flaky.
2939 let g:test_is_flaky = 1
2940
2941 let origwin = win_getid()
2942 call term_start("NONE", {"hidden": 1})->popup_create({"border": []})
2943 sleep 50m
2944
2945 " since no actual job is running can close the window with :quit
2946 call feedkeys("\<C-W>:q\<CR>", 'xt')
2947 call assert_equal([], popup_list())
2948
2949 call assert_equal(origwin, win_getid())
2950endfunc
2951
Bram Moolenaard28950f2022-05-29 14:13:04 +01002952func Test_popupwin_terminal_scrollbar()
2953 CheckFeature terminal
2954 CheckScreendump
2955 CheckUnix
2956
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01002957 call writefile(range(50), 'Xtestfile', 'D')
Bram Moolenaard28950f2022-05-29 14:13:04 +01002958 let lines =<< trim END
2959 vim9script
2960
Bram Moolenaar10db31f2022-05-30 17:58:03 +01002961 # testing CTRL-W CTRL-W requires two windows
2962 split
2963
Bram Moolenaard28950f2022-05-29 14:13:04 +01002964 term_start(['cat', 'Xtestfile'], {hidden: true})
2965 ->popup_create({
2966 minwidth: 40,
2967 maxwidth: 40,
2968 minheight: 8,
2969 maxheight: 8,
2970 scrollbar: true,
2971 border: []
2972 })
2973 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01002974 call writefile(lines, 'Xpterm', 'D')
Bram Moolenaard28950f2022-05-29 14:13:04 +01002975 let buf = RunVimInTerminal('-S Xpterm', #{rows: 15})
2976 call VerifyScreenDump(buf, 'Test_popupwin_poptermscroll_1', {})
2977
2978 " scroll to the middle
2979 call term_sendkeys(buf, "50%")
2980 call VerifyScreenDump(buf, 'Test_popupwin_poptermscroll_2', {})
2981
Bram Moolenaar10db31f2022-05-30 17:58:03 +01002982 " get error if trying to escape the window
2983 call term_sendkeys(buf, "\<C-W>\<C-W>")
2984 call VerifyScreenDump(buf, 'Test_popupwin_poptermscroll_3', {})
2985
Bram Moolenaard28950f2022-05-29 14:13:04 +01002986 " close the popupwin.
2987 call term_sendkeys(buf, ":q\<CR>")
Bram Moolenaar10db31f2022-05-30 17:58:03 +01002988 call VerifyScreenDump(buf, 'Test_popupwin_poptermscroll_4', {})
Bram Moolenaard28950f2022-05-29 14:13:04 +01002989
2990 call StopVimInTerminal(buf)
Bram Moolenaard28950f2022-05-29 14:13:04 +01002991endfunc
2992
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002993func Test_popupwin_close_prevwin()
2994 CheckFeature terminal
Bram Moolenaar62f93f42020-09-02 22:33:24 +02002995 call Popupwin_close_prevwin()
2996endfunc
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002997
Bram Moolenaar62f93f42020-09-02 22:33:24 +02002998def Popupwin_close_prevwin()
2999 assert_equal(1, winnr('$'))
Bram Moolenaarab176ce2020-06-15 21:19:08 +02003000 split
3001 wincmd b
Bram Moolenaar62f93f42020-09-02 22:33:24 +02003002 assert_equal(2, winnr())
Bram Moolenaare0de1712020-12-02 17:36:54 +01003003 var buf = term_start(&shell, {hidden: 1})
Bram Moolenaar62f93f42020-09-02 22:33:24 +02003004 popup_create(buf, {})
Bram Moolenaar62aec932022-01-29 21:45:34 +00003005 g:TermWait(buf, 100)
Bram Moolenaar62f93f42020-09-02 22:33:24 +02003006 popup_clear(true)
3007 assert_equal(2, winnr())
Bram Moolenaarab176ce2020-06-15 21:19:08 +02003008
3009 quit
3010 exe 'bwipe! ' .. buf
Bram Moolenaar62f93f42020-09-02 22:33:24 +02003011enddef
Bram Moolenaarab176ce2020-06-15 21:19:08 +02003012
Bram Moolenaar934470e2019-09-01 23:27:05 +02003013func Test_popupwin_with_buffer_and_filter()
3014 new Xwithfilter
3015 call setline(1, range(100))
3016 let bufnr = bufnr()
3017 hide
3018
3019 func BufferFilter(win, key)
3020 if a:key == 'G'
3021 " recursive use of "G" does not cause problems.
3022 call win_execute(a:win, 'normal! G')
3023 return 1
3024 endif
3025 return 0
3026 endfunc
3027
3028 let winid = popup_create(bufnr, #{maxheight: 5, filter: 'BufferFilter'})
3029 call assert_equal(1, popup_getpos(winid).firstline)
3030 redraw
3031 call feedkeys("G", 'xt')
3032 call assert_equal(99, popup_getpos(winid).firstline)
3033
3034 call popup_close(winid)
3035 exe 'bwipe! ' .. bufnr
3036endfunc
3037
Bram Moolenaare296e312019-07-03 23:20:18 +02003038func Test_popupwin_width()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003039 let winid = popup_create(repeat(['short', 'long long long line', 'medium width'], 50), #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003040 \ maxwidth: 40,
3041 \ maxheight: 10,
Bram Moolenaare296e312019-07-03 23:20:18 +02003042 \ })
3043 for top in range(1, 20)
Bram Moolenaar6a124e62019-09-04 18:15:19 +02003044 eval winid->popup_setoptions(#{firstline: top})
Bram Moolenaare296e312019-07-03 23:20:18 +02003045 redraw
3046 call assert_equal(19, popup_getpos(winid).width)
3047 endfor
3048 call popup_clear()
3049endfunc
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02003050
3051func Test_popupwin_buf_close()
3052 let buf = bufadd('Xtestbuf')
3053 call bufload(buf)
3054 call setbufline(buf, 1, ['just', 'some', 'lines'])
3055 let winid = popup_create(buf, {})
3056 redraw
3057 call assert_equal(3, popup_getpos(winid).height)
3058 let bufinfo = getbufinfo(buf)[0]
3059 call assert_equal(1, bufinfo.changed)
3060 call assert_equal(0, bufinfo.hidden)
3061 call assert_equal(0, bufinfo.listed)
3062 call assert_equal(1, bufinfo.loaded)
3063 call assert_equal([], bufinfo.windows)
3064 call assert_equal([winid], bufinfo.popups)
3065
3066 call popup_close(winid)
3067 call assert_equal({}, popup_getpos(winid))
3068 let bufinfo = getbufinfo(buf)[0]
3069 call assert_equal(1, bufinfo.changed)
3070 call assert_equal(1, bufinfo.hidden)
3071 call assert_equal(0, bufinfo.listed)
3072 call assert_equal(1, bufinfo.loaded)
3073 call assert_equal([], bufinfo.windows)
3074 call assert_equal([], bufinfo.popups)
3075 exe 'bwipe! ' .. buf
3076endfunc
Bram Moolenaar017c2692019-07-13 14:17:51 +02003077
3078func Test_popup_menu_with_maxwidth()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02003079 CheckScreendump
Bram Moolenaar017c2692019-07-13 14:17:51 +02003080
3081 let lines =<< trim END
3082 call setline(1, range(1, 10))
3083 hi ScrollThumb ctermbg=blue
3084 hi ScrollBar ctermbg=red
3085 func PopupMenu(lines, line, col, scrollbar = 0)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003086 return popup_menu(a:lines, #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003087 \ maxwidth: 10,
3088 \ maxheight: 3,
3089 \ pos : 'topleft',
3090 \ col : a:col,
3091 \ line : a:line,
3092 \ scrollbar : a:scrollbar,
Bram Moolenaar017c2692019-07-13 14:17:51 +02003093 \ })
3094 endfunc
3095 call PopupMenu(['x'], 1, 1)
3096 call PopupMenu(['123456789|'], 1, 16)
3097 call PopupMenu(['123456789|' .. ' '], 7, 1)
3098 call PopupMenu([repeat('123456789|', 100)], 7, 16)
3099 call PopupMenu(repeat(['123456789|' .. ' '], 5), 1, 33, 1)
3100 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01003101 call writefile(lines, 'XtestPopupMenuMaxWidth', 'D')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003102 let buf = RunVimInTerminal('-S XtestPopupMenuMaxWidth', #{rows: 13})
Bram Moolenaar017c2692019-07-13 14:17:51 +02003103 call VerifyScreenDump(buf, 'Test_popupwin_menu_maxwidth_1', {})
3104
3105 " close the menu popupwin.
3106 call term_sendkeys(buf, " ")
3107 call term_sendkeys(buf, " ")
3108 call term_sendkeys(buf, " ")
3109 call term_sendkeys(buf, " ")
3110 call term_sendkeys(buf, " ")
3111
3112 " clean up
3113 call StopVimInTerminal(buf)
Bram Moolenaar017c2692019-07-13 14:17:51 +02003114endfunc
3115
Bram Moolenaara901a372019-07-13 16:38:50 +02003116func Test_popup_menu_with_scrollbar()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02003117 CheckScreendump
Bram Moolenaara901a372019-07-13 16:38:50 +02003118
3119 let lines =<< trim END
3120 call setline(1, range(1, 20))
3121 hi ScrollThumb ctermbg=blue
3122 hi ScrollBar ctermbg=red
Bram Moolenaar6a124e62019-09-04 18:15:19 +02003123 eval ['one', 'two', 'three', 'four', 'five',
3124 \ 'six', 'seven', 'eight', 'nine']
3125 \ ->popup_menu(#{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003126 \ minwidth: 8,
3127 \ maxheight: 3,
Bram Moolenaara901a372019-07-13 16:38:50 +02003128 \ })
3129 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01003130 call writefile(lines, 'XtestPopupMenuScroll', 'D')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003131 let buf = RunVimInTerminal('-S XtestPopupMenuScroll', #{rows: 10})
Bram Moolenaara901a372019-07-13 16:38:50 +02003132
3133 call term_sendkeys(buf, "j")
3134 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_1', {})
3135
3136 call term_sendkeys(buf, "jjj")
3137 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_2', {})
3138
Christian Brabandtbadeedd2023-08-13 19:25:28 +02003139 " the cursor wraps around at the bottom
Bram Moolenaara901a372019-07-13 16:38:50 +02003140 call term_sendkeys(buf, repeat("j", 20))
3141 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_3', {})
3142
Christian Brabandtbadeedd2023-08-13 19:25:28 +02003143 " if the cursor is again at the bottom line
3144 call term_sendkeys(buf, repeat("j", 2))
3145 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_3a', {})
3146
Bram Moolenaara901a372019-07-13 16:38:50 +02003147 call term_sendkeys(buf, "kk")
3148 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_4', {})
3149
3150 call term_sendkeys(buf, "k")
3151 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_5', {})
3152
Christian Brabandtbadeedd2023-08-13 19:25:28 +02003153 " the cursor wraps around at the top
Bram Moolenaara901a372019-07-13 16:38:50 +02003154 call term_sendkeys(buf, repeat("k", 20))
3155 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_6', {})
3156
Christian Brabandtbadeedd2023-08-13 19:25:28 +02003157 " the cursor at the top of the window again
3158 call term_sendkeys(buf, repeat("k", 3))
3159 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_6a', {})
3160
Bram Moolenaara901a372019-07-13 16:38:50 +02003161 " close the menu popupwin.
3162 call term_sendkeys(buf, " ")
3163
3164 " clean up
3165 call StopVimInTerminal(buf)
Bram Moolenaara901a372019-07-13 16:38:50 +02003166endfunc
3167
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003168func Test_popup_menu_filter()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02003169 CheckScreendump
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003170
3171 let lines =<< trim END
3172 function! MyFilter(winid, key) abort
3173 if a:key == "0"
3174 call win_execute(a:winid, "call setpos('.', [0, 1, 1, 0])")
3175 return 1
3176 endif
3177 if a:key == "G"
3178 call win_execute(a:winid, "call setpos('.', [0, line('$'), 1, 0])")
3179 return 1
3180 endif
3181 if a:key == "j"
3182 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0])")
3183 return 1
3184 endif
3185 if a:key == "k"
3186 call win_execute(a:winid, "call setpos('.', [0, line('.') - 1, 1, 0])")
3187 return 1
3188 endif
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02003189 if a:key == ':'
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003190 call popup_close(a:winid)
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02003191 return 0
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003192 endif
3193 return 0
3194 endfunction
3195 call popup_menu(['111', '222', '333', '444', '555', '666', '777', '888', '999'], #{
3196 \ maxheight : 3,
3197 \ filter : 'MyFilter'
3198 \ })
3199 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01003200 call writefile(lines, 'XtestPopupMenuFilter', 'D')
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003201 let buf = RunVimInTerminal('-S XtestPopupMenuFilter', #{rows: 10})
3202
3203 call term_sendkeys(buf, "j")
3204 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_1', {})
3205
3206 call term_sendkeys(buf, "k")
3207 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_2', {})
3208
3209 call term_sendkeys(buf, "G")
3210 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_3', {})
3211
3212 call term_sendkeys(buf, "0")
3213 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_4', {})
3214
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02003215 " check that when the popup is closed in the filter the screen is redrawn
3216 call term_sendkeys(buf, ":")
3217 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_5', {})
3218 call term_sendkeys(buf, "\<CR>")
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003219
3220 " clean up
3221 call StopVimInTerminal(buf)
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003222endfunc
3223
3224func Test_popup_cursorline()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02003225 CheckScreendump
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003226
3227 let winid = popup_create('some text', {})
3228 call assert_equal(0, popup_getoptions(winid).cursorline)
3229 call popup_close(winid)
3230
3231 let winid = popup_create('some text', #{ cursorline: 1, })
3232 call assert_equal(1, popup_getoptions(winid).cursorline)
3233 call popup_close(winid)
3234
Bram Moolenaar6bfc4752021-02-21 23:12:18 +01003235 let winid = popup_create('some text', #{ cursorline: v:true, })
3236 call assert_equal(1, popup_getoptions(winid).cursorline)
3237 call popup_close(winid)
3238
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003239 let winid = popup_create('some text', #{ cursorline: 0, })
3240 call assert_equal(0, popup_getoptions(winid).cursorline)
3241 call popup_close(winid)
3242
3243 let winid = popup_menu('some text', {})
3244 call assert_equal(1, popup_getoptions(winid).cursorline)
3245 call popup_close(winid)
3246
3247 let winid = popup_menu('some text', #{ cursorline: 1, })
3248 call assert_equal(1, popup_getoptions(winid).cursorline)
3249 call popup_close(winid)
3250
3251 let winid = popup_menu('some text', #{ cursorline: 0, })
3252 call assert_equal(0, popup_getoptions(winid).cursorline)
3253 call popup_close(winid)
3254
3255 " ---------
3256 " Pattern 1
3257 " ---------
3258 let lines =<< trim END
3259 call popup_create(['111', '222', '333'], #{ cursorline : 0 })
3260 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01003261 call writefile(lines, 'XtestPopupCursorLine', 'D')
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003262 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
3263 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_1', {})
3264 call term_sendkeys(buf, ":call popup_clear()\<cr>")
3265 call StopVimInTerminal(buf)
3266
3267 " ---------
3268 " Pattern 2
3269 " ---------
3270 let lines =<< trim END
3271 call popup_create(['111', '222', '333'], #{ cursorline : 1 })
3272 END
3273 call writefile(lines, 'XtestPopupCursorLine')
3274 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
3275 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_2', {})
3276 call term_sendkeys(buf, ":call popup_clear()\<cr>")
3277 call StopVimInTerminal(buf)
3278
3279 " ---------
3280 " Pattern 3
3281 " ---------
3282 let lines =<< trim END
3283 function! MyFilter(winid, key) abort
3284 if a:key == "j"
3285 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0]) | redraw")
3286 return 1
3287 endif
3288 if a:key == 'x'
3289 call popup_close(a:winid)
3290 return 1
3291 endif
3292 return 0
3293 endfunction
3294 call popup_menu(['111', '222', '333'], #{
3295 \ cursorline : 0,
3296 \ maxheight : 2,
3297 \ filter : 'MyFilter',
3298 \ })
3299 END
3300 call writefile(lines, 'XtestPopupCursorLine')
3301 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
3302 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_3', {})
3303 call term_sendkeys(buf, "j")
3304 call term_sendkeys(buf, "j")
3305 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_4', {})
3306 call term_sendkeys(buf, "x")
3307 call StopVimInTerminal(buf)
3308
3309 " ---------
3310 " Pattern 4
3311 " ---------
3312 let lines =<< trim END
3313 function! MyFilter(winid, key) abort
3314 if a:key == "j"
3315 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0]) | redraw")
3316 return 1
3317 endif
3318 if a:key == 'x'
3319 call popup_close(a:winid)
3320 return 1
3321 endif
3322 return 0
3323 endfunction
3324 call popup_menu(['111', '222', '333'], #{
3325 \ cursorline : 1,
3326 \ maxheight : 2,
3327 \ filter : 'MyFilter',
3328 \ })
3329 END
3330 call writefile(lines, 'XtestPopupCursorLine')
3331 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
3332 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_5', {})
3333 call term_sendkeys(buf, "j")
3334 call term_sendkeys(buf, "j")
3335 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_6', {})
3336 call term_sendkeys(buf, "x")
3337 call StopVimInTerminal(buf)
3338
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02003339 " ---------
3340 " Cursor in second line when creating the popup
3341 " ---------
3342 let lines =<< trim END
3343 let winid = popup_create(['111', '222', '333'], #{
3344 \ cursorline : 1,
3345 \ })
3346 call win_execute(winid, "2")
3347 END
3348 call writefile(lines, 'XtestPopupCursorLine')
3349 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
3350 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_7', {})
3351 call StopVimInTerminal(buf)
3352
Bram Moolenaar4eb7dae2019-11-12 22:33:45 +01003353 " ---------
3354 " Use current buffer for popupmenu
3355 " ---------
3356 let lines =<< trim END
3357 call setline(1, ['one', 'two', 'three'])
3358 let winid = popup_create(bufnr('%'), #{
3359 \ cursorline : 1,
3360 \ })
3361 call win_execute(winid, "2")
3362 END
3363 call writefile(lines, 'XtestPopupCursorLine')
3364 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
3365 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_8', {})
3366 call StopVimInTerminal(buf)
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003367endfunc
3368
Bram Moolenaar6bfc4752021-02-21 23:12:18 +01003369def Test_popup_cursorline_vim9()
3370 var winid = popup_create('some text', { cursorline: true, })
3371 assert_equal(1, popup_getoptions(winid).cursorline)
3372 popup_close(winid)
3373
3374 assert_fails("popup_create('some text', { cursorline: 2, })", 'E1023:')
3375 popup_clear()
3376enddef
3377
Bram Moolenaarf914a332019-07-20 15:09:56 +02003378func Test_previewpopup()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02003379 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003380 CheckFeature quickfix
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02003381
Bram Moolenaarf914a332019-07-20 15:09:56 +02003382 call writefile([
3383 \ "!_TAG_FILE_ENCODING\tutf-8\t//",
3384 \ "another\tXtagfile\t/^this is another",
3385 \ "theword\tXtagfile\t/^theword"],
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01003386 \ 'Xtags', 'D')
Bram Moolenaarf914a332019-07-20 15:09:56 +02003387 call writefile(range(1,20)
3388 \ + ['theword is here']
3389 \ + range(22, 27)
3390 \ + ['this is another place']
3391 \ + range(29, 40),
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01003392 \ "Xtagfile", 'D')
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02003393 call writefile(range(1,10)
3394 \ + ['searched word is here']
3395 \ + range(12, 20),
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01003396 \ "Xheader.h", 'D')
Bram Moolenaarf914a332019-07-20 15:09:56 +02003397 let lines =<< trim END
3398 set tags=Xtags
3399 call setline(1, [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02003400 \ 'one',
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02003401 \ '#include "Xheader.h"',
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02003402 \ 'three',
3403 \ 'four',
3404 \ 'five',
3405 \ 'six',
3406 \ 'seven',
3407 \ 'find theword somewhere',
3408 \ 'nine',
3409 \ 'this is another word',
3410 \ 'very long line where the word is also another'])
Bram Moolenaarf914a332019-07-20 15:09:56 +02003411 set previewpopup=height:4,width:40
Bram Moolenaar60577482021-03-04 21:35:07 +01003412 hi OtherColor ctermbg=lightcyan guibg=lightcyan
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02003413 set path=.
Bram Moolenaarf914a332019-07-20 15:09:56 +02003414 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01003415 call writefile(lines, 'XtestPreviewPopup', 'D')
Bram Moolenaarf914a332019-07-20 15:09:56 +02003416 let buf = RunVimInTerminal('-S XtestPreviewPopup', #{rows: 14})
3417
3418 call term_sendkeys(buf, "/theword\<CR>\<C-W>}")
3419 call term_sendkeys(buf, ":\<CR>")
3420 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_1', {})
3421
Bram Moolenaar60577482021-03-04 21:35:07 +01003422 call term_sendkeys(buf, ":set previewpopup+=highlight:OtherColor\<CR>")
Bram Moolenaarf914a332019-07-20 15:09:56 +02003423 call term_sendkeys(buf, "/another\<CR>\<C-W>}")
3424 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_2', {})
3425
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003426 call term_sendkeys(buf, ":call popup_move(popup_findpreview(), #{col: 15})\<CR>")
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02003427 call term_sendkeys(buf, ":\<CR>")
3428 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_3', {})
3429
3430 call term_sendkeys(buf, "/another\<CR>\<C-W>}")
3431 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_4', {})
3432
Bram Moolenaar799439a2020-02-11 21:44:17 +01003433 call term_sendkeys(buf, ":silent cd ..\<CR>:\<CR>")
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02003434 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_5', {})
Bram Moolenaar799439a2020-02-11 21:44:17 +01003435 call term_sendkeys(buf, ":silent cd testdir\<CR>")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02003436
Bram Moolenaar60577482021-03-04 21:35:07 +01003437 call term_sendkeys(buf, ":set previewpopup-=highlight:OtherColor\<CR>")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02003438 call term_sendkeys(buf, ":pclose\<CR>")
Bram Moolenaar78d629a2019-08-16 17:31:15 +02003439 call term_sendkeys(buf, ":\<BS>")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02003440 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_6', {})
3441
3442 call term_sendkeys(buf, ":pedit +/theword Xtagfile\<CR>")
3443 call term_sendkeys(buf, ":\<CR>")
3444 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_7', {})
3445
3446 call term_sendkeys(buf, ":pclose\<CR>")
3447 call term_sendkeys(buf, ":psearch searched\<CR>")
3448 call term_sendkeys(buf, ":\<CR>")
3449 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_8', {})
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02003450
Bram Moolenaar8bf716c2020-01-23 15:33:54 +01003451 call term_sendkeys(buf, "\<C-W>p")
3452 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_9', {})
3453
3454 call term_sendkeys(buf, ":call win_execute(popup_findpreview(), 'call popup_clear()')\<CR>")
3455 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_10', {})
3456
Bram Moolenaarf914a332019-07-20 15:09:56 +02003457 call StopVimInTerminal(buf)
Bram Moolenaarf914a332019-07-20 15:09:56 +02003458endfunc
3459
Bram Moolenaar393f8d62022-10-02 14:28:30 +01003460func Test_previewpopup_pum()
3461 CheckScreendump
3462 CheckFeature quickfix
3463
3464 let lines =<< trim END
3465 let a = 3
3466 let b = 1
3467 echo a
3468 echo b
3469 call system('echo hello')
3470 " the end
3471 END
3472 call writefile(lines, 'XpreviewText.vim', 'D')
3473
3474 let lines =<< trim END
3475 call setline(1, ['one', 'two', 'three', 'other', 'once', 'only', 'off'])
3476 set previewpopup=height:6,width:40
3477 pedit XpreviewText.vim
3478 END
3479 call writefile(lines, 'XtestPreviewPum', 'D')
3480 let buf = RunVimInTerminal('-S XtestPreviewPum', #{rows: 12})
3481
3482 call term_sendkeys(buf, "A o\<C-N>")
3483 call VerifyScreenDump(buf, 'Test_pum_preview_1', {})
3484
3485 call term_sendkeys(buf, "\<C-N>")
3486 call VerifyScreenDump(buf, 'Test_pum_preview_2', {})
3487
3488 call term_sendkeys(buf, "\<C-N>")
3489 call VerifyScreenDump(buf, 'Test_pum_preview_3', {})
3490
3491 call term_sendkeys(buf, "\<C-N>")
3492 call VerifyScreenDump(buf, 'Test_pum_preview_4', {})
3493
3494 call term_sendkeys(buf, "\<Esc>")
3495 call StopVimInTerminal(buf)
3496endfunc
3497
3498
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003499func Get_popupmenu_lines()
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003500 let lines =<< trim END
3501 set completeopt+=preview,popup
3502 set completefunc=CompleteFuncDict
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02003503 hi InfoPopup ctermbg=yellow
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003504
3505 func CompleteFuncDict(findstart, base)
3506 if a:findstart
3507 if col('.') > 10
3508 return col('.') - 10
3509 endif
3510 return 0
3511 endif
3512
3513 return {
3514 \ 'words': [
3515 \ {
3516 \ 'word': 'aword',
3517 \ 'abbr': 'wrd',
3518 \ 'menu': 'extra text',
3519 \ 'info': 'words are cool',
3520 \ 'kind': 'W',
3521 \ 'user_data': 'test'
3522 \ },
3523 \ {
3524 \ 'word': 'anotherword',
3525 \ 'abbr': 'anotwrd',
3526 \ 'menu': 'extra text',
3527 \ 'info': "other words are\ncooler than this and some more text\nto make wrap",
3528 \ 'kind': 'W',
3529 \ 'user_data': 'notest'
3530 \ },
3531 \ {
3532 \ 'word': 'noinfo',
3533 \ 'abbr': 'noawrd',
3534 \ 'menu': 'extra text',
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02003535 \ 'info': "lets\nshow\na\nscrollbar\nhere",
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003536 \ 'kind': 'W',
3537 \ 'user_data': 'notest'
3538 \ },
3539 \ {
3540 \ 'word': 'thatword',
3541 \ 'abbr': 'thatwrd',
3542 \ 'menu': 'extra text',
3543 \ 'info': 'that word is cool',
3544 \ 'kind': 'W',
3545 \ 'user_data': 'notest'
3546 \ },
3547 \ ]
3548 \ }
3549 endfunc
3550 call setline(1, 'text text text text text text text ')
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003551 func ChangeColor()
3552 let id = popup_findinfo()
Bram Moolenaard356fc62020-12-09 18:13:44 +01003553 if buflisted(winbufnr(id))
3554 call setline(1, 'buffer is listed')
3555 endif
Bram Moolenaar6a124e62019-09-04 18:15:19 +02003556 eval id->popup_setoptions(#{highlight: 'InfoPopup'})
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003557 endfunc
Bram Moolenaardca7abe2019-10-20 18:17:57 +02003558
3559 func InfoHidden()
3560 set completepopup=height:4,border:off,align:menu
3561 set completeopt-=popup completeopt+=popuphidden
3562 au CompleteChanged * call HandleChange()
3563 endfunc
3564
3565 let s:counter = 0
3566 func HandleChange()
3567 let s:counter += 1
3568 let selected = complete_info(['selected']).selected
3569 if selected <= 0
3570 " First time: do nothing, info remains hidden
3571 return
3572 endif
3573 if selected == 1
3574 " Second time: show info right away
3575 let id = popup_findinfo()
3576 if id
3577 call popup_settext(id, 'immediate info ' .. s:counter)
3578 call popup_show(id)
3579 endif
3580 else
3581 " Third time: show info after a short delay
3582 call timer_start(100, 'ShowInfo')
3583 endif
3584 endfunc
3585
3586 func ShowInfo(...)
3587 let id = popup_findinfo()
3588 if id
3589 call popup_settext(id, 'async info ' .. s:counter)
3590 call popup_show(id)
3591 endif
3592 endfunc
Bram Moolenaar2dfae042020-11-15 14:09:37 +01003593
Bakudankun65555002021-11-17 20:40:16 +00003594 func OpenOtherPopups()
3595 call popup_create([
3596 \ 'popup below',
3597 \ 'popup below',
3598 \ 'popup below',
3599 \ 'popup below',
3600 \ ], #{
3601 \ line: 'cursor',
3602 \ col: 'cursor+3',
3603 \ highlight: 'ErrorMsg',
3604 \ minwidth: 17,
3605 \ zindex: 50,
3606 \ })
3607 call popup_create([
3608 \ 'popup on top',
3609 \ 'popup on top',
3610 \ 'popup on top',
3611 \ ], #{
3612 \ line: 'cursor+3',
3613 \ col: 'cursor-10',
3614 \ highlight: 'Search',
3615 \ minwidth: 10,
3616 \ zindex: 200,
3617 \ })
3618 endfunc
3619
Bram Moolenaar2dfae042020-11-15 14:09:37 +01003620 " Check that no autocommands are triggered for the info popup
3621 au WinEnter * if win_gettype() == 'popup' | call setline(2, 'WinEnter') | endif
3622 au WinLeave * if win_gettype() == 'popup' | call setline(2, 'WinLeave') | endif
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003623 END
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003624 return lines
3625endfunc
3626
3627func Test_popupmenu_info_border()
3628 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003629 CheckFeature quickfix
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003630
3631 let lines = Get_popupmenu_lines()
3632 call add(lines, 'set completepopup=height:4,highlight:InfoPopup')
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01003633 call writefile(lines, 'XtestInfoPopup', 'D')
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003634
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003635 let buf = RunVimInTerminal('-S XtestInfoPopup', #{rows: 14})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003636 call TermWait(buf, 25)
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003637
3638 call term_sendkeys(buf, "A\<C-X>\<C-U>")
3639 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_1', {})
3640
3641 call term_sendkeys(buf, "\<C-N>")
3642 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_2', {})
3643
3644 call term_sendkeys(buf, "\<C-N>")
3645 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_3', {})
3646
3647 call term_sendkeys(buf, "\<C-N>\<C-N>")
3648 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_4', {})
3649
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02003650 " info on the left with scrollbar
3651 call term_sendkeys(buf, "test text test text\<C-X>\<C-U>")
3652 call term_sendkeys(buf, "\<C-N>\<C-N>")
3653 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_5', {})
3654
Bram Moolenaar202c3f72019-11-21 12:12:35 +01003655 " Test that the popupmenu's scrollbar and infopopup do not overlap
3656 call term_sendkeys(buf, "\<Esc>")
3657 call term_sendkeys(buf, ":set pumheight=3\<CR>")
3658 call term_sendkeys(buf, "cc\<C-X>\<C-U>")
3659 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_6', {})
3660
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003661 " Hide the info popup, cycle through buffers, make sure it didn't get
Bram Moolenaarca7c0782020-01-14 20:42:48 +01003662 " deleted.
3663 call term_sendkeys(buf, "\<Esc>")
3664 call term_sendkeys(buf, ":set hidden\<CR>")
3665 call term_sendkeys(buf, ":bn\<CR>")
3666 call term_sendkeys(buf, ":bn\<CR>")
3667 call term_sendkeys(buf, "otest text test text\<C-X>\<C-U>")
3668 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_7', {})
3669
Bram Moolenaar447bfba2020-07-18 16:07:16 +02003670 " Test that when the option is changed the popup changes.
3671 call term_sendkeys(buf, "\<Esc>")
3672 call term_sendkeys(buf, ":set completepopup=border:off\<CR>")
3673 call term_sendkeys(buf, "a\<C-X>\<C-U>")
3674 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_8', {})
3675
Bram Moolenaar6d585f42020-07-26 22:20:54 +02003676 call term_sendkeys(buf, " \<Esc>")
3677 call term_sendkeys(buf, ":set completepopup+=width:10\<CR>")
3678 call term_sendkeys(buf, "a\<C-X>\<C-U>")
3679 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_9', {})
3680
Bram Moolenaarca7c0782020-01-14 20:42:48 +01003681 call term_sendkeys(buf, "\<Esc>")
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003682 call StopVimInTerminal(buf)
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003683endfunc
3684
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003685func Test_popupmenu_info_noborder()
3686 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003687 CheckFeature quickfix
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003688
3689 let lines = Get_popupmenu_lines()
3690 call add(lines, 'set completepopup=height:4,border:off')
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01003691 call writefile(lines, 'XtestInfoPopupNb', 'D')
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003692
3693 let buf = RunVimInTerminal('-S XtestInfoPopupNb', #{rows: 14})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003694 call TermWait(buf, 25)
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003695
3696 call term_sendkeys(buf, "A\<C-X>\<C-U>")
3697 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_nb_1', {})
3698
3699 call StopVimInTerminal(buf)
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003700endfunc
3701
Bram Moolenaar258cef52019-08-21 17:29:29 +02003702func Test_popupmenu_info_align_menu()
3703 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003704 CheckFeature quickfix
Bram Moolenaar258cef52019-08-21 17:29:29 +02003705
3706 let lines = Get_popupmenu_lines()
3707 call add(lines, 'set completepopup=height:4,border:off,align:menu')
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01003708 call writefile(lines, 'XtestInfoPopupNb', 'D')
Bram Moolenaar258cef52019-08-21 17:29:29 +02003709
3710 let buf = RunVimInTerminal('-S XtestInfoPopupNb', #{rows: 14})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003711 call TermWait(buf, 25)
Bram Moolenaar258cef52019-08-21 17:29:29 +02003712
3713 call term_sendkeys(buf, "A\<C-X>\<C-U>")
3714 call term_sendkeys(buf, "\<C-N>")
3715 call term_sendkeys(buf, "\<C-N>")
3716 call term_sendkeys(buf, "\<C-N>")
3717 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_1', {})
3718
3719 call term_sendkeys(buf, "test text test text test\<C-X>\<C-U>")
3720 call term_sendkeys(buf, "\<C-N>")
3721 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_2', {})
3722
3723 call term_sendkeys(buf, "\<Esc>")
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003724 call term_sendkeys(buf, ":call ChangeColor()\<CR>")
Bram Moolenaar258cef52019-08-21 17:29:29 +02003725 call term_sendkeys(buf, ":call setline(2, ['x']->repeat(10))\<CR>")
3726 call term_sendkeys(buf, "Gotest text test text\<C-X>\<C-U>")
3727 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_3', {})
3728
3729 call StopVimInTerminal(buf)
Bram Moolenaar258cef52019-08-21 17:29:29 +02003730endfunc
3731
Bram Moolenaardca7abe2019-10-20 18:17:57 +02003732func Test_popupmenu_info_hidden()
3733 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003734 CheckFeature quickfix
Bram Moolenaardca7abe2019-10-20 18:17:57 +02003735
3736 let lines = Get_popupmenu_lines()
3737 call add(lines, 'call InfoHidden()')
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01003738 call writefile(lines, 'XtestInfoPopupHidden', 'D')
Bram Moolenaardca7abe2019-10-20 18:17:57 +02003739
3740 let buf = RunVimInTerminal('-S XtestInfoPopupHidden', #{rows: 14})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003741 call TermWait(buf, 25)
Bram Moolenaardca7abe2019-10-20 18:17:57 +02003742
3743 call term_sendkeys(buf, "A\<C-X>\<C-U>")
3744 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_hidden_1', {})
3745
3746 call term_sendkeys(buf, "\<C-N>")
3747 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_hidden_2', {})
3748
3749 call term_sendkeys(buf, "\<C-N>")
3750 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_hidden_3', {})
3751
3752 call term_sendkeys(buf, "\<Esc>")
3753 call StopVimInTerminal(buf)
Bram Moolenaardca7abe2019-10-20 18:17:57 +02003754endfunc
3755
Bram Moolenaarbef93ac2019-12-06 20:17:35 +01003756func Test_popupmenu_info_too_wide()
3757 CheckScreendump
3758 CheckFeature quickfix
3759
3760 let lines =<< trim END
3761 call setline(1, range(10))
3762
3763 set completeopt+=preview,popup
3764 set completepopup=align:menu
3765 set omnifunc=OmniFunc
3766 hi InfoPopup ctermbg=lightgrey
3767
3768 func OmniFunc(findstart, base)
3769 if a:findstart
3770 return 0
3771 endif
3772
3773 let menuText = 'some long text to make sure the menu takes up all of the width of the window'
3774 return #{
Bram Moolenaar755bf2b2023-01-28 19:38:49 +00003775 \ words: [
3776 \ #{
3777 \ word: 'scrap',
3778 \ menu: menuText,
3779 \ info: "other words are\ncooler than this and some more text\nto make wrap",
3780 \ },
3781 \ #{
3782 \ word: 'scappier',
3783 \ menu: menuText,
3784 \ info: 'words are cool',
3785 \ },
3786 \ #{
3787 \ word: 'scrappier2',
3788 \ menu: menuText,
3789 \ info: 'words are cool',
3790 \ },
3791 \ ]
Bram Moolenaarbef93ac2019-12-06 20:17:35 +01003792 \ }
3793 endfunc
3794 END
3795
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01003796 call writefile(lines, 'XtestInfoPopupWide', 'D')
Bram Moolenaarbef93ac2019-12-06 20:17:35 +01003797 let buf = RunVimInTerminal('-S XtestInfoPopupWide', #{rows: 8})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003798 call TermWait(buf, 25)
Bram Moolenaarbef93ac2019-12-06 20:17:35 +01003799
3800 call term_sendkeys(buf, "Ascr\<C-X>\<C-O>")
3801 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_wide_1', {})
3802
3803 call term_sendkeys(buf, "\<Esc>")
3804 call StopVimInTerminal(buf)
Bram Moolenaarbef93ac2019-12-06 20:17:35 +01003805endfunc
3806
Bakudankun65555002021-11-17 20:40:16 +00003807func Test_popupmenu_masking()
3808 " Test that popup windows that are opened while popup menu is open are
3809 " properly displayed.
3810 CheckScreendump
3811 CheckFeature quickfix
3812
3813 let lines = Get_popupmenu_lines()
3814 call add(lines, 'inoremap <C-A> <Cmd>call OpenOtherPopups()<CR>')
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01003815 call writefile(lines, 'XtestPopupmenuMasking', 'D')
Bakudankun65555002021-11-17 20:40:16 +00003816
3817 let buf = RunVimInTerminal('-S XtestPopupmenuMasking', #{rows: 14})
3818 call TermWait(buf, 25)
3819
Bram Moolenaar9f145572022-11-27 12:45:41 +00003820 call term_sendkeys(buf, "A" .. GetEscCodeWithModifier('C', 'X')
3821 \ .. GetEscCodeWithModifier('C', 'U')
3822 \ .. GetEscCodeWithModifier('C', 'A'))
Bakudankun65555002021-11-17 20:40:16 +00003823 call VerifyScreenDump(buf, 'Test_popupwin_popupmenu_masking_1', {})
3824
3825 call term_sendkeys(buf, "\<Esc>")
3826 call VerifyScreenDump(buf, 'Test_popupwin_popupmenu_masking_2', {})
3827
3828 call StopVimInTerminal(buf)
Bakudankun65555002021-11-17 20:40:16 +00003829endfunc
3830
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02003831func Test_popupwin_recycle_bnr()
Bram Moolenaare49fbff2019-08-21 22:50:07 +02003832 let bufnr = popup_notification('nothing wrong', {})->winbufnr()
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02003833 call popup_clear()
Bram Moolenaar6a124e62019-09-04 18:15:19 +02003834 let winid = 'nothing wrong'->popup_notification({})
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02003835 call assert_equal(bufnr, winbufnr(winid))
3836 call popup_clear()
3837endfunc
3838
Bram Moolenaar1824f452019-10-02 23:06:46 +02003839func Test_popupwin_getoptions_tablocal()
3840 topleft split
3841 let win1 = popup_create('nothing', #{maxheight: 8})
3842 let win2 = popup_create('something', #{maxheight: 10})
3843 let win3 = popup_create('something', #{maxheight: 15})
3844 call assert_equal(8, popup_getoptions(win1).maxheight)
3845 call assert_equal(10, popup_getoptions(win2).maxheight)
3846 call assert_equal(15, popup_getoptions(win3).maxheight)
3847 call popup_clear()
3848 quit
3849endfunc
3850
Bram Moolenaare8a7dfe2019-10-03 22:35:52 +02003851func Test_popupwin_cancel()
3852 let win1 = popup_create('one', #{line: 5, filter: {... -> 0}})
3853 let win2 = popup_create('two', #{line: 10, filter: {... -> 0}})
3854 let win3 = popup_create('three', #{line: 15, filter: {... -> 0}})
3855 call assert_equal(5, popup_getpos(win1).line)
3856 call assert_equal(10, popup_getpos(win2).line)
3857 call assert_equal(15, popup_getpos(win3).line)
3858 " TODO: this also works without patch 8.1.2110
3859 call feedkeys("\<C-C>", 'xt')
3860 call assert_equal(5, popup_getpos(win1).line)
3861 call assert_equal(10, popup_getpos(win2).line)
3862 call assert_equal({}, popup_getpos(win3))
3863 call feedkeys("\<C-C>", 'xt')
3864 call assert_equal(5, popup_getpos(win1).line)
3865 call assert_equal({}, popup_getpos(win2))
3866 call assert_equal({}, popup_getpos(win3))
3867 call feedkeys("\<C-C>", 'xt')
3868 call assert_equal({}, popup_getpos(win1))
3869 call assert_equal({}, popup_getpos(win2))
3870 call assert_equal({}, popup_getpos(win3))
3871endfunc
3872
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003873func Test_popupwin_filter_redraw()
3874 " Create two popups with a filter that closes the popup when typing "0".
3875 " Both popups should close, even though the redraw also calls
3876 " popup_reset_handled()
3877
3878 func CloseFilter(winid, key)
3879 if a:key == '0'
3880 call popup_close(a:winid)
3881 redraw
3882 endif
3883 return 0 " pass the key
3884 endfunc
3885
3886 let id1 = popup_create('first one', #{
3887 \ line: 1,
3888 \ col: 1,
3889 \ filter: 'CloseFilter',
3890 \ })
3891 let id2 = popup_create('second one', #{
3892 \ line: 9,
3893 \ col: 1,
3894 \ filter: 'CloseFilter',
3895 \ })
3896 call assert_equal(1, popup_getpos(id1).line)
3897 call assert_equal(9, popup_getpos(id2).line)
3898
3899 call feedkeys('0', 'xt')
3900 call assert_equal({}, popup_getpos(id1))
3901 call assert_equal({}, popup_getpos(id2))
3902
3903 call popup_clear()
3904 delfunc CloseFilter
3905endfunc
3906
Bram Moolenaar20ebbea2019-11-30 17:58:27 +01003907func Test_popupwin_double_width()
3908 CheckScreendump
3909
3910 let lines =<< trim END
3911 call setline(1, 'x你好世界你好世你好世界你好')
3912 call setline(2, '你好世界你好世你好世界你好')
3913 call setline(3, 'x你好世界你好世你好世界你好')
3914 call popup_create('你好,世界 - 你好,世界xxxxx', #{line: 1, col: 3, maxwidth: 14})
3915 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01003916 call writefile(lines, 'XtestPopupWide', 'D')
Bram Moolenaar20ebbea2019-11-30 17:58:27 +01003917
3918 let buf = RunVimInTerminal('-S XtestPopupWide', #{rows: 10})
3919 call VerifyScreenDump(buf, 'Test_popupwin_doublewidth_1', {})
3920
3921 call StopVimInTerminal(buf)
Bram Moolenaar20ebbea2019-11-30 17:58:27 +01003922endfunc
3923
3924func Test_popupwin_sign()
3925 CheckScreendump
3926
3927 let lines =<< trim END
3928 call setline(1, range(10))
3929 call sign_define('Current', {
3930 \ 'text': '>>',
3931 \ 'texthl': 'WarningMsg',
3932 \ 'linehl': 'Error',
3933 \ })
3934 call sign_define('Other', {
3935 \ 'text': '#!',
3936 \ 'texthl': 'Error',
3937 \ 'linehl': 'Search',
3938 \ })
3939 let winid = popup_create(['hello', 'bright', 'world'], {
3940 \ 'minwidth': 20,
3941 \ })
3942 call setwinvar(winid, "&signcolumn", "yes")
3943 let winbufnr = winbufnr(winid)
3944
3945 " add sign to current buffer, shows
3946 call sign_place(1, 'Selected', 'Current', bufnr('%'), {'lnum': 1})
3947 " add sign to current buffer, does not show
3948 call sign_place(2, 'PopUpSelected', 'Other', bufnr('%'), {'lnum': 2})
3949
3950 " add sign to popup buffer, shows
3951 call sign_place(3, 'PopUpSelected', 'Other', winbufnr, {'lnum': 1})
3952 " add sign to popup buffer, does not show
3953 call sign_place(4, 'Selected', 'Current', winbufnr, {'lnum': 2})
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02003954
3955 func SetOptions()
3956 call setwinvar(g:winid, '&number', 1)
3957 call setwinvar(g:winid, '&foldcolumn', 2)
3958 call popup_settext(g:winid, 'a longer line to check the width')
3959 endfunc
Bram Moolenaar20ebbea2019-11-30 17:58:27 +01003960 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01003961 call writefile(lines, 'XtestPopupSign', 'D')
Bram Moolenaar20ebbea2019-11-30 17:58:27 +01003962
3963 let buf = RunVimInTerminal('-S XtestPopupSign', #{rows: 10})
3964 call VerifyScreenDump(buf, 'Test_popupwin_sign_1', {})
3965
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02003966 " set more options to check the width is adjusted
3967 call term_sendkeys(buf, ":call SetOptions()\<CR>")
3968 call VerifyScreenDump(buf, 'Test_popupwin_sign_2', {})
3969
Bram Moolenaar20ebbea2019-11-30 17:58:27 +01003970 call StopVimInTerminal(buf)
Bram Moolenaar20ebbea2019-11-30 17:58:27 +01003971endfunc
3972
Bram Moolenaar99ebf222019-12-10 23:44:48 +01003973func Test_popupwin_bufnr()
3974 let popwin = popup_create(['blah'], #{})
3975 let popbuf = winbufnr(popwin)
3976 split asdfasdf
3977 let newbuf = bufnr()
3978 call assert_true(newbuf > popbuf, 'New buffer number is higher')
3979 call assert_equal(newbuf, bufnr('$'))
3980 call popup_clear()
3981 let popwin = popup_create(['blah'], #{})
3982 " reuses previous buffer number
3983 call assert_equal(popbuf, winbufnr(popwin))
3984 call assert_equal(newbuf, bufnr('$'))
3985
3986 call popup_clear()
3987 bwipe!
3988endfunc
3989
Bram Moolenaarec084d32020-02-28 22:44:47 +01003990func Test_popupwin_filter_input_multibyte()
3991 func MyPopupFilter(winid, c)
3992 let g:bytes = range(a:c->strlen())->map({i -> char2nr(a:c[i])})
3993 return 0
3994 endfunc
3995 let winid = popup_create('', #{mapping: 0, filter: 'MyPopupFilter'})
3996
3997 " UTF-8: E3 80 80, including K_SPECIAL(0x80)
3998 call feedkeys("\u3000", 'xt')
3999 call assert_equal([0xe3, 0x80, 0x80], g:bytes)
4000
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01004001 " UTF-8: E3 80 9B, including CSI(0x9B)
4002 call feedkeys("\u301b", 'xt')
4003 call assert_equal([0xe3, 0x80, 0x9b], g:bytes)
Bram Moolenaarec084d32020-02-28 22:44:47 +01004004
Bram Moolenaar20298ce2020-06-19 21:46:52 +02004005 if has('unix')
4006 " with modifyOtherKeys <M-S-a> does not include a modifier sequence
Bram Moolenaarb326edf2020-06-20 15:03:38 +02004007 if has('gui_running')
4008 call feedkeys("\x9b\xfc\x08A", 'Lx!')
4009 else
4010 call feedkeys("\<Esc>[27;4;65~", 'Lx!')
4011 endif
Bram Moolenaar20298ce2020-06-19 21:46:52 +02004012 call assert_equal([0xc3, 0x81], g:bytes)
4013 endif
4014
Bram Moolenaarec084d32020-02-28 22:44:47 +01004015 call popup_clear()
4016 delfunc MyPopupFilter
4017 unlet g:bytes
4018endfunc
4019
Bram Moolenaar6f8f7332020-08-10 21:19:23 +02004020func Test_popupwin_filter_close_ctrl_c()
4021 CheckScreendump
4022
4023 let lines =<< trim END
4024 vsplit
4025 set laststatus=2
4026 set statusline=%!Statusline()
4027
4028 function Statusline() abort
4029 return '%<%f %h%m%r%=%-14.(%l,%c%V%) %P'
4030 endfunction
4031
4032 call popup_create('test test test test...', {'filter': {-> 0}})
4033 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01004034 call writefile(lines, 'XtestPopupCtrlC', 'D')
Bram Moolenaar6f8f7332020-08-10 21:19:23 +02004035
4036 let buf = RunVimInTerminal('-S XtestPopupCtrlC', #{rows: 10})
4037
4038 call term_sendkeys(buf, "\<C-C>")
4039 call VerifyScreenDump(buf, 'Test_popupwin_ctrl_c', {})
4040
4041 call StopVimInTerminal(buf)
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02004042endfunc
4043
4044func Test_popupwin_filter_close_wrong_name()
4045 CheckScreendump
4046
4047 let lines =<< trim END
4048 call popup_create('one two three...', {'filter': 'NoSuchFunc'})
4049 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01004050 call writefile(lines, 'XtestPopupWrongName', 'D')
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02004051
4052 let buf = RunVimInTerminal('-S XtestPopupWrongName', #{rows: 10})
4053
4054 call term_sendkeys(buf, "j")
4055 call VerifyScreenDump(buf, 'Test_popupwin_wrong_name', {})
4056
4057 call StopVimInTerminal(buf)
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02004058endfunc
4059
4060func Test_popupwin_filter_close_three_errors()
4061 CheckScreendump
4062
4063 let lines =<< trim END
4064 set cmdheight=2
4065 call popup_create('one two three...', {'filter': 'filter'})
4066 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01004067 call writefile(lines, 'XtestPopupThreeErrors', 'D')
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02004068
4069 let buf = RunVimInTerminal('-S XtestPopupThreeErrors', #{rows: 10})
4070
4071 call term_sendkeys(buf, "jj")
4072 call VerifyScreenDump(buf, 'Test_popupwin_three_errors_1', {})
4073 call term_sendkeys(buf, "j")
4074 call VerifyScreenDump(buf, 'Test_popupwin_three_errors_2', {})
4075
4076 call StopVimInTerminal(buf)
Bram Moolenaar6f8f7332020-08-10 21:19:23 +02004077endfunc
4078
Bram Moolenaar927495b2020-11-06 17:58:35 +01004079func Test_popupwin_latin1_encoding()
4080 CheckScreendump
4081 CheckUnix
4082
4083 " When 'encoding' is a single-byte encoding a terminal window will mess up
4084 " the display. Check that showing a popup on top of that doesn't crash.
4085 let lines =<< trim END
4086 set encoding=latin1
4087 terminal cat Xmultibyte
4088 call popup_create(['one', 'two', 'three', 'four'], #{line: 1, col: 10})
Bram Moolenaar4c5bdb92020-12-17 17:45:59 +01004089 redraw
Bram Moolenaarb125b532020-12-17 21:56:09 +01004090 " wait for "cat" to finish
4091 while execute('ls!') !~ 'finished'
4092 sleep 10m
4093 endwhile
Bram Moolenaar4c5bdb92020-12-17 17:45:59 +01004094 echo "Done"
Bram Moolenaar927495b2020-11-06 17:58:35 +01004095 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +01004096 call writefile(lines, 'XtestPopupLatin', 'D')
4097 call writefile([repeat("\u3042 ", 120)], 'Xmultibyte', 'D')
Bram Moolenaar927495b2020-11-06 17:58:35 +01004098
4099 let buf = RunVimInTerminal('-S XtestPopupLatin', #{rows: 10})
Bram Moolenaar4c5bdb92020-12-17 17:45:59 +01004100 call WaitForAssert({-> assert_match('Done', term_getline(buf, 10))})
Bram Moolenaar927495b2020-11-06 17:58:35 +01004101
4102 call term_sendkeys(buf, ":q\<CR>")
4103 call StopVimInTerminal(buf)
Bram Moolenaar927495b2020-11-06 17:58:35 +01004104endfunc
4105
Bram Moolenaarba2920f2020-03-06 21:43:17 +01004106func Test_popupwin_atcursor_far_right()
4107 new
4108
4109 " this was getting stuck
4110 set signcolumn=yes
4111 call setline(1, repeat('=', &columns))
4112 normal! ggg$
Bram Moolenaaref6b9792020-05-13 16:34:15 +02004113 let winid = popup_atcursor(repeat('x', 500), #{moved: 'any', border: []})
Bram Moolenaarba2920f2020-03-06 21:43:17 +01004114
Bram Moolenaar6d585f42020-07-26 22:20:54 +02004115 " 'signcolumn' was getting reset
4116 call setwinvar(winid, '&signcolumn', 'yes')
4117 call popup_setoptions(winid, {'zindex': 1000})
4118 call assert_equal('yes', getwinvar(winid, '&signcolumn'))
4119
Bram Moolenaaref6b9792020-05-13 16:34:15 +02004120 call popup_close(winid)
Bram Moolenaarba2920f2020-03-06 21:43:17 +01004121 bwipe!
4122 set signcolumn&
4123endfunc
4124
Bram Moolenaar0f1563f2020-03-20 21:15:51 +01004125func Test_popupwin_splitmove()
4126 vsplit
4127 let win2 = win_getid()
4128 let popup_winid = popup_dialog('hello', {})
4129 call assert_fails('call win_splitmove(popup_winid, win2)', 'E957:')
4130 call assert_fails('call win_splitmove(win2, popup_winid)', 'E957:')
4131
4132 call popup_clear()
4133 bwipe
4134endfunc
4135
Bram Moolenaarcbcd9cb2020-11-07 16:58:59 +01004136func Test_popupwin_exiting_terminal()
4137 CheckFeature terminal
4138
4139 " Tests that when creating a popup right after closing a terminal window does
4140 " not make the popup the current window.
4141 let winid = win_getid()
4142 try
4143 augroup Test_popupwin_exiting_terminal
4144 autocmd!
4145 autocmd WinEnter * :call popup_create('test', {})
4146 augroup END
4147 let bnr = term_start(&shell, #{term_finish: 'close'})
4148 call term_sendkeys(bnr, "exit\r\n")
4149 call WaitForAssert({-> assert_equal(winid, win_getid())})
4150 finally
4151 call popup_clear(1)
4152 augroup Test_popupwin_exiting_terminal
4153 autocmd!
4154 augroup END
4155 endtry
4156endfunc
Bram Moolenaar0f1563f2020-03-20 21:15:51 +01004157
Bram Moolenaar014f6982021-01-04 13:18:30 +01004158func Test_popup_filter_menu()
4159 let colors = ['red', 'green', 'blue']
4160 call popup_menu(colors, #{callback: {_, result -> assert_equal('green', colors[result - 1])}})
4161 call feedkeys("\<c-n>\<c-n>\<c-p>\<cr>", 'xt')
4162endfunc
4163
Bram Moolenaar65026432021-02-06 14:22:32 +01004164func Test_popup_getoptions_other_tab()
4165 new
4166 call setline(1, 'some text')
4167 call prop_type_add('textprop', {})
4168 call prop_add(1, 1, #{type: 'textprop', length: 1})
4169 let id = popup_create('TEST', #{textprop: 'textprop', highlight: 'ErrorMsg', tabpage: 1})
4170 tab sp
4171 call assert_equal(['textprop', 'textpropid', 'textpropwin'], popup_getoptions(id)->keys()->filter({_, v -> v =~ 'textprop'}))
4172
4173 tabclose
Bram Moolenaarefa19232021-02-06 14:59:27 +01004174 call popup_close(id)
Bram Moolenaar65026432021-02-06 14:22:32 +01004175 bwipe!
4176 call prop_type_delete('textprop')
4177endfunc
4178
Bram Moolenaarefa19232021-02-06 14:59:27 +01004179
4180func Test_popup_setoptions_other_tab()
Bram Moolenaarb18b4962022-09-02 21:55:50 +01004181 new Xpotfile
Bram Moolenaarefa19232021-02-06 14:59:27 +01004182 let winid = win_getid()
4183 call setline(1, 'some text')
4184 call prop_type_add('textprop', {})
4185 call prop_add(1, 1, #{type: 'textprop', length: 1})
4186 let id = popup_create('TEST', #{textprop: 'textprop'})
4187 tab sp
4188 call popup_setoptions(id, #{textprop: 'textprop', textpropwin: winid})
4189 call assert_equal(winid, popup_getoptions(id).textpropwin)
4190
4191 tabclose
4192 call popup_close(id)
Bram Moolenaarb18b4962022-09-02 21:55:50 +01004193 bwipe! Xpotfile
Bram Moolenaarefa19232021-02-06 14:59:27 +01004194 call prop_type_delete('textprop')
4195endfunc
4196
Bram Moolenaar82db31c2021-02-10 14:56:11 +01004197func Test_popup_prop_not_visible()
4198 CheckScreendump
4199
4200 let lines =<< trim END
4201 vim9script
4202 set nowrap stal=2
4203 rightbelow :31vnew
4204 setline(1, ['', 'some text', '', 'other text'])
4205 prop_type_add('someprop', {})
4206 prop_add(2, 9, {type: 'someprop', length: 5})
Bram Moolenaar27724252022-05-08 15:00:04 +01004207 g:some_id = popup_create('attached to "some"', {
Bram Moolenaar82db31c2021-02-10 14:56:11 +01004208 textprop: 'someprop',
4209 highlight: 'ErrorMsg',
4210 line: -1,
4211 wrap: false,
4212 fixed: true,
4213 })
4214 prop_type_add('otherprop', {})
4215 prop_add(4, 10, {type: 'otherprop', length: 5})
4216 popup_create('attached to "other"', {
4217 textprop: 'otherprop',
4218 highlight: 'ErrorMsg',
4219 line: -1,
4220 wrap: false,
4221 fixed: false,
4222 })
4223 END
dundargocc57b5bc2022-11-02 13:30:51 +00004224 call writefile(lines, 'XtestPropNotVisible', 'D')
4225 let buf = RunVimInTerminal('-S XtestPropNotVisible', #{rows: 10})
Bram Moolenaar82db31c2021-02-10 14:56:11 +01004226 call VerifyScreenDump(buf, 'Test_popup_prop_not_visible_01', {})
4227
Bram Moolenaar27724252022-05-08 15:00:04 +01004228 " check that hiding and unhiding the popup works
4229 call term_sendkeys(buf, ":call popup_hide(g:some_id)\<CR>")
4230 call VerifyScreenDump(buf, 'Test_popup_prop_not_visible_01a', {})
4231 call term_sendkeys(buf, ":call popup_show(g:some_id)\<CR>")
4232 call VerifyScreenDump(buf, 'Test_popup_prop_not_visible_01b', {})
4233
Bram Moolenaar82db31c2021-02-10 14:56:11 +01004234 call term_sendkeys(buf, ":vert resize -14\<CR>")
4235 call VerifyScreenDump(buf, 'Test_popup_prop_not_visible_02', {})
4236
4237 call term_sendkeys(buf, ":vert resize -8\<CR>")
4238 call VerifyScreenDump(buf, 'Test_popup_prop_not_visible_03', {})
4239
4240 " clean up
4241 call StopVimInTerminal(buf)
Bram Moolenaar82db31c2021-02-10 14:56:11 +01004242endfunction
4243
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02004244func Test_bufdel_skips_popupwin_buffer()
4245 let id = popup_create("Some text", {})
4246 %bd
4247 call popup_close(id)
4248endfunc
4249
Bram Moolenaar58a3cae2022-09-08 13:43:10 +01004250func Test_term_popup_bufline()
4251 " very specific situation where a non-existing buffer line is used, leading
4252 " to an ml_get error
4253 CheckScreendump
4254
4255 let lines =<< trim END
4256 vim9script
4257 &scrolloff = 5
4258 term_start('seq 1 5', {term_finish: 'open'})
4259 timer_start(50, (_) => {
4260 set cpoptions&vim
4261 var buf = popup_create([], {})->winbufnr()
4262 appendbufline(buf, 0, range(5))
4263 })
4264 END
4265 call writefile(lines, 'XtestTermPopup', 'D')
4266 let buf = RunVimInTerminal('-S XtestTermPopup', #{rows: 15})
4267 call VerifyScreenDump(buf, 'Test_term_popup_bufline', {})
4268
4269 " clean up
4270 call StopVimInTerminal(buf)
4271endfunc
4272
Christian Brabandtf6cdab32023-08-11 23:42:02 +02004273func Test_popupwin_with_error()
4274 CheckScreendump
4275
4276 let lines =<< trim END
4277 let options = {'border': 'ERROR', 'line': 1, 'col': 1, 'minwidth': &columns, 'title': 'TITLE'}
4278
4279 END
4280 "call popup_create('Hello world!', options)
4281 call writefile(lines, 'XtestPopupError', 'D')
4282 let buf = RunVimInTerminal('-S XtestPopupError', {})
4283 call term_sendkeys(buf, ":call popup_create('Hello world!', options)\<CR>")
4284 call VerifyScreenDump(buf, 'Test_popupwin_with_error_1', {})
4285
4286 " clean up
4287 call StopVimInTerminal(buf)
4288endfunc
4289
Christian Brabandt47510f32023-10-15 09:56:16 +02004290func Test_popup_close_callback_recursive()
zeertzjqfe583b12023-12-21 16:59:26 +01004291 set maxfuncdepth=20
Christian Brabandt47510f32023-10-15 09:56:16 +02004292 " this invokes the callback recursively
4293 let winid = popup_create('something', #{callback: 'popup_close'})
4294 redraw
zeertzjqfe583b12023-12-21 16:59:26 +01004295 call assert_fails('call popup_close(winid)', 'E169:')
4296
4297 set maxfuncdepth&
Christian Brabandt47510f32023-10-15 09:56:16 +02004298endfunc
Bram Moolenaar82db31c2021-02-10 14:56:11 +01004299
Christian Brabandtac4cffc2024-01-16 17:22:38 +01004300func Test_popupwin_setbufvar_changing_window_view()
4301 " Test for Github Issue https://github.com/vim/vim/issues/13863
4302 " using setbufvar(buf, '&option') should not scroll
4303 " the current window
4304 20new
4305 call append(0, range(1, 25))
4306 setlocal scrollbind
4307 norm! G
4308 let topline = winsaveview()['topline']
4309 call setbufvar(winbufnr(popup_atcursor(['foobar'], {})), '&syntax', 'python')
4310 " close popup
4311 call popup_clear()
4312 call assert_equal(topline, winsaveview()['topline'])
4313
4314 " clean up
4315 bw!
4316endfunc
4317
Christian Brabandtf00f4d92024-09-03 18:20:13 +02004318func Test_popupwin_clears_cmdline_on_hide()
4319 " Test that the command line is properly cleared for overlong
4320 " popup windows and using popup_hide()
4321 CheckScreendump
4322
4323 let lines =<< trim END
4324 vim9script
4325 var id: number
4326 def Filter(winid: number, key: string): bool
4327 if key == 'q'
4328 popup_hide(winid)
4329 else
4330 return false
4331 endif
4332 return true
4333 enddef
4334 setline(1, repeat(['foobar one two three'], &lines))
4335 id = popup_create(1, {
4336 col: 1,
4337 minwidth: &columns,
4338 maxwidth: &columns,
4339 minheight: &lines,
4340 maxheight: &lines,
4341 filter: Filter,
4342 })
4343 END
4344 call writefile(lines, 'XtestPopup_win', 'D')
4345 let buf = RunVimInTerminal('-S XtestPopup_win', #{rows: 10})
4346 call term_sendkeys(buf, "q")
4347 call term_wait(buf)
4348 call VerifyScreenDump(buf, 'Test_popupwin_hide_clear_cmdline_01', {})
4349 call StopVimInTerminal(buf)
4350 let lines =<< trim END
4351 vim9script
4352 var id: number
4353 def Filter(winid: number, key: string): bool
4354 if key == 'q'
4355 popup_close(winid)
4356 else
4357 return false
4358 endif
4359 return true
4360 enddef
4361 setline(1, repeat(['foobar one two three'], &lines))
4362 id = popup_create(1, {
4363 col: 1,
4364 minwidth: &columns,
4365 maxwidth: &columns,
4366 minheight: &lines,
4367 maxheight: &lines,
4368 filter: Filter,
4369 })
4370 END
4371 call writefile(lines, 'XtestPopup_win2', 'D')
4372 let buf = RunVimInTerminal('-S XtestPopup_win2', #{rows: 10})
4373 call term_sendkeys(buf, "q")
4374 call term_wait(buf)
4375 call VerifyScreenDump(buf, 'Test_popupwin_hide_clear_cmdline_01', {})
4376
4377 " clean up
4378 call StopVimInTerminal(buf)
4379endfunc
4380
Bram Moolenaar331bafd2019-07-20 17:46:05 +02004381" vim: shiftwidth=2 sts=2