blob: 3b38102ddb21d61fc4468af8198951df1d6e4b82 [file] [log] [blame]
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001" Tests for popup windows
2
Bram Moolenaarb46fecd2019-06-15 17:58:09 +02003source check.vim
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004CheckFeature popupwin
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005
6source screendump.vim
7
8func Test_simple_popup()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02009 CheckScreendump
10
Bram Moolenaare7eb9272019-06-24 00:58:07 +020011 let lines =<< trim END
12 call setline(1, range(1, 100))
13 hi PopupColor1 ctermbg=lightblue
14 hi PopupColor2 ctermbg=lightcyan
Bram Moolenaar1666ac92019-11-10 17:22:31 +010015 hi EndOfBuffer ctermbg=lightgrey
Bram Moolenaare7eb9272019-06-24 00:58:07 +020016 hi Comment ctermfg=red
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020017 call prop_type_add('comment', #{highlight: 'Comment'})
18 let winid = popup_create('hello there', #{line: 3, col: 11, minwidth: 20, highlight: 'PopupColor1'})
19 let winid2 = popup_create(['another one', 'another two', 'another three'], #{line: 3, col: 25, minwidth: 20})
Bram Moolenaare7eb9272019-06-24 00:58:07 +020020 call setwinvar(winid2, '&wincolor', 'PopupColor2')
21 END
22 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020023 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar4d784b22019-05-25 19:51:39 +020024 call VerifyScreenDump(buf, 'Test_popupwin_01', {})
25
Bram Moolenaarec583842019-05-26 14:11:23 +020026 " Add a tabpage
27 call term_sendkeys(buf, ":tabnew\<CR>")
Bram Moolenaar60cdb302019-05-27 21:54:10 +020028 call term_sendkeys(buf, ":let popupwin = popup_create(["
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020029 \ .. "#{text: 'other tab'},"
30 \ .. "#{text: 'a comment line', props: [#{"
Bram Moolenaard5abb4c2019-07-13 22:46:10 +020031 \ .. "col: 3, length: 7, minwidth: 20, type: 'comment'"
Bram Moolenaar7a8d0272019-05-26 23:32:06 +020032 \ .. "}]},"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020033 \ .. "], #{line: 4, col: 9, minwidth: 20})\<CR>")
Bram Moolenaarec583842019-05-26 14:11:23 +020034 call VerifyScreenDump(buf, 'Test_popupwin_02', {})
35
36 " switch back to first tabpage
37 call term_sendkeys(buf, "gt")
38 call VerifyScreenDump(buf, 'Test_popupwin_03', {})
39
40 " close that tabpage
41 call term_sendkeys(buf, ":quit!\<CR>")
42 call VerifyScreenDump(buf, 'Test_popupwin_04', {})
43
Bram Moolenaar202d9822019-06-11 21:56:30 +020044 " set 'columns' to a small value, size must be recomputed
45 call term_sendkeys(buf, ":let cols = &columns\<CR>")
46 call term_sendkeys(buf, ":set columns=12\<CR>")
47 call VerifyScreenDump(buf, 'Test_popupwin_04a', {})
48 call term_sendkeys(buf, ":let &columns = cols\<CR>")
49
Bram Moolenaar17146962019-05-30 00:12:11 +020050 " resize popup, show empty line at bottom
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020051 call term_sendkeys(buf, ":call popup_move(popupwin, #{minwidth: 15, maxwidth: 25, minheight: 3, maxheight: 5})\<CR>")
Bram Moolenaar60cdb302019-05-27 21:54:10 +020052 call term_sendkeys(buf, ":redraw\<CR>")
53 call VerifyScreenDump(buf, 'Test_popupwin_05', {})
54
Bram Moolenaar17146962019-05-30 00:12:11 +020055 " show not fitting line at bottom
56 call term_sendkeys(buf, ":call setbufline(winbufnr(popupwin), 3, 'this line will not fit here')\<CR>")
57 call term_sendkeys(buf, ":redraw\<CR>")
58 call VerifyScreenDump(buf, 'Test_popupwin_06', {})
59
Bram Moolenaar24a5ac52019-06-08 19:01:18 +020060 " move popup over ruler
61 call term_sendkeys(buf, ":set cmdheight=2\<CR>")
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020062 call term_sendkeys(buf, ":call popup_move(popupwin, #{line: 7, col: 55})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +020063 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar24a5ac52019-06-08 19:01:18 +020064 call VerifyScreenDump(buf, 'Test_popupwin_07', {})
65
66 " clear all popups after moving the cursor a bit, so that ruler is updated
67 call term_sendkeys(buf, "axxx\<Esc>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +020068 call TermWait(buf)
Bram Moolenaar24a5ac52019-06-08 19:01:18 +020069 call term_sendkeys(buf, "0")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +020070 call TermWait(buf)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +020071 call term_sendkeys(buf, ":call popup_clear()\<CR>")
Bram Moolenaar24a5ac52019-06-08 19:01:18 +020072 call VerifyScreenDump(buf, 'Test_popupwin_08', {})
73
Bram Moolenaar4d784b22019-05-25 19:51:39 +020074 " clean up
75 call StopVimInTerminal(buf)
76 call delete('XtestPopup')
77endfunc
Bram Moolenaar51fe3b12019-05-26 20:10:06 +020078
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020079func Test_popup_with_border_and_padding()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +020080 CheckScreendump
Bram Moolenaar2fd8e352019-06-01 20:16:48 +020081
Bram Moolenaar3bfd04e2019-06-01 20:45:21 +020082 for iter in range(0, 1)
Bram Moolenaare7eb9272019-06-24 00:58:07 +020083 let lines =<< trim END
84 call setline(1, range(1, 100))
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020085 call popup_create('hello border', #{line: 2, col: 3, border: []})
86 call popup_create('hello padding', #{line: 2, col: 23, padding: []})
Bram Moolenaarc363fe12019-08-04 18:13:46 +020087 call popup_create('hello both', #{line: 2, col: 43, border: [], padding: [], highlight: 'Normal'})
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020088 call popup_create('border TL', #{line: 6, col: 3, border: [1, 0, 0, 4]})
Bram Moolenaarb0992022020-01-30 14:55:42 +010089 call popup_create('paddings', #{line: 6, col: 23, padding: range(1, 4)})
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020090 call popup_create('wrapped longer text', #{line: 8, col: 55, padding: [0, 3, 0, 3], border: [0, 1, 0, 1]})
91 call popup_create('right aligned text', #{line: 11, col: 56, wrap: 0, padding: [0, 3, 0, 3], border: [0, 1, 0, 1]})
Bram Moolenaarfbcdf672020-01-06 23:07:48 +010092 call popup_create('X', #{line: 2, col: 73})
93 call popup_create('X', #{line: 3, col: 74})
94 call popup_create('X', #{line: 4, col: 75})
95 call popup_create('X', #{line: 5, col: 76})
Bram Moolenaare7eb9272019-06-24 00:58:07 +020096 END
97 call insert(lines, iter == 1 ? '' : 'set enc=latin1')
98 call writefile(lines, 'XtestPopupBorder')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +020099 let buf = RunVimInTerminal('-S XtestPopupBorder', #{rows: 15})
Bram Moolenaar3bfd04e2019-06-01 20:45:21 +0200100 call VerifyScreenDump(buf, 'Test_popupwin_2' .. iter, {})
101
102 call StopVimInTerminal(buf)
103 call delete('XtestPopupBorder')
104 endfor
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200105
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200106 let lines =<< trim END
107 call setline(1, range(1, 100))
108 hi BlueColor ctermbg=lightblue
109 hi TopColor ctermbg=253
110 hi RightColor ctermbg=245
111 hi BottomColor ctermbg=240
112 hi LeftColor ctermbg=248
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200113 call popup_create('hello border', #{line: 2, col: 3, border: [], borderhighlight: ['BlueColor']})
114 call popup_create(['hello border', 'and more'], #{line: 2, col: 23, border: [], borderhighlight: ['TopColor', 'RightColor', 'BottomColor', 'LeftColor']})
115 call popup_create(['hello border', 'lines only'], #{line: 2, col: 43, border: [], borderhighlight: ['BlueColor'], borderchars: ['x']})
116 call popup_create(['hello border', 'with corners'], #{line: 2, col: 60, border: [], borderhighlight: ['BlueColor'], borderchars: ['x', '#']})
117 let winid = popup_create(['hello border', 'with numbers'], #{line: 6, col: 3, border: [], borderhighlight: ['BlueColor'], borderchars: ['0', '1', '2', '3', '4', '5', '6', '7']})
118 call popup_create(['hello border', 'just blanks'], #{line: 7, col: 23, border: [], borderhighlight: ['BlueColor'], borderchars: [' ']})
Bram Moolenaar3dabd712019-07-08 23:30:22 +0200119 func MultiByte()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200120 call popup_create(['hello'], #{line: 8, col: 43, border: [], borderchars: ['─', '│', '─', '│', '┌', '┐', '┘', '└']})
Bram Moolenaar3dabd712019-07-08 23:30:22 +0200121 endfunc
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200122 END
123 call writefile(lines, 'XtestPopupBorder')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200124 let buf = RunVimInTerminal('-S XtestPopupBorder', #{rows: 12})
Bram Moolenaar790498b2019-06-01 22:15:29 +0200125 call VerifyScreenDump(buf, 'Test_popupwin_22', {})
126
Bram Moolenaarad24a712019-06-17 20:05:45 +0200127 " check that changing borderchars triggers a redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200128 call term_sendkeys(buf, ":call popup_setoptions(winid, #{borderchars: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']})\<CR>")
Bram Moolenaarad24a712019-06-17 20:05:45 +0200129 call VerifyScreenDump(buf, 'Test_popupwin_23', {})
130
Bram Moolenaar3dabd712019-07-08 23:30:22 +0200131 " check multi-byte border only with 'ambiwidth' single
132 if &ambiwidth == 'single'
133 call term_sendkeys(buf, ":call MultiByte()\<CR>")
134 call VerifyScreenDump(buf, 'Test_popupwin_24', {})
135 endif
136
Bram Moolenaar790498b2019-06-01 22:15:29 +0200137 call StopVimInTerminal(buf)
138 call delete('XtestPopupBorder')
139
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200140 let with_border_or_padding = #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200141 \ line: 2,
142 \ core_line: 3,
143 \ col: 3,
144 \ core_col: 4,
145 \ width: 14,
146 \ core_width: 12,
147 \ height: 3,
148 \ core_height: 1,
149 \ firstline: 1,
Bram Moolenaar30efcf32019-11-03 22:29:38 +0100150 \ lastline: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200151 \ scrollbar: 0,
152 \ visible: 1}
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200153 let winid = popup_create('hello border', #{line: 2, col: 3, border: []})",
Bram Moolenaar6a124e62019-09-04 18:15:19 +0200154 call assert_equal(with_border_or_padding, winid->popup_getpos())
Bram Moolenaarae943152019-06-16 22:54:14 +0200155 let options = popup_getoptions(winid)
156 call assert_equal([], options.border)
157 call assert_false(has_key(options, "padding"))
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200158
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200159 let winid = popup_create('hello padding', #{line: 2, col: 3, padding: []})
Bram Moolenaarae943152019-06-16 22:54:14 +0200160 let with_border_or_padding.width = 15
161 let with_border_or_padding.core_width = 13
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200162 call assert_equal(with_border_or_padding, popup_getpos(winid))
Bram Moolenaarae943152019-06-16 22:54:14 +0200163 let options = popup_getoptions(winid)
164 call assert_false(has_key(options, "border"))
165 call assert_equal([], options.padding)
166
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200167 call popup_setoptions(winid, #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200168 \ padding: [1, 2, 3, 4],
169 \ border: [4, 0, 7, 8],
170 \ borderhighlight: ['Top', 'Right', 'Bottom', 'Left'],
171 \ borderchars: ['1', '^', '2', '>', '3', 'v', '4', '<'],
Bram Moolenaarae943152019-06-16 22:54:14 +0200172 \ })
173 let options = popup_getoptions(winid)
174 call assert_equal([1, 0, 1, 1], options.border)
175 call assert_equal([1, 2, 3, 4], options.padding)
176 call assert_equal(['Top', 'Right', 'Bottom', 'Left'], options.borderhighlight)
177 call assert_equal(['1', '^', '2', '>', '3', 'v', '4', '<'], options.borderchars)
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200178
Bram Moolenaara1396152019-10-20 18:46:05 +0200179 " Check that popup_setoptions() takes the output of popup_getoptions()
180 call popup_setoptions(winid, options)
181 call assert_equal(options, popup_getoptions(winid))
182
Bram Moolenaarb0992022020-01-30 14:55:42 +0100183 " Check that range() doesn't crash
184 call popup_setoptions(winid, #{
185 \ padding: range(1, 4),
186 \ border: range(5, 8),
187 \ borderhighlight: range(4),
188 \ borderchars: range(8),
189 \ })
190
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200191 let winid = popup_create('hello both', #{line: 3, col: 8, border: [], padding: []})
192 call assert_equal(#{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200193 \ line: 3,
194 \ core_line: 5,
195 \ col: 8,
196 \ core_col: 10,
197 \ width: 14,
198 \ core_width: 10,
199 \ height: 5,
200 \ scrollbar: 0,
201 \ core_height: 1,
202 \ firstline: 1,
Bram Moolenaar30efcf32019-11-03 22:29:38 +0100203 \ lastline: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200204 \ visible: 1}, popup_getpos(winid))
Bram Moolenaarae943152019-06-16 22:54:14 +0200205
206 call popup_clear()
Bram Moolenaar2fd8e352019-06-01 20:16:48 +0200207endfunc
208
Bram Moolenaarb4230122019-05-30 18:40:53 +0200209func Test_popup_with_syntax_win_execute()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200210 CheckScreendump
211
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200212 let lines =<< trim END
213 call setline(1, range(1, 100))
214 hi PopupColor ctermbg=lightblue
215 let winid = popup_create([
216 \ '#include <stdio.h>',
217 \ 'int main(void)',
218 \ '{',
219 \ ' printf(123);',
220 \ '}',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200221 \], #{line: 3, col: 25, highlight: 'PopupColor'})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200222 call win_execute(winid, 'set syntax=cpp')
223 END
224 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200225 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaarb4230122019-05-30 18:40:53 +0200226 call VerifyScreenDump(buf, 'Test_popupwin_10', {})
227
228 " clean up
229 call StopVimInTerminal(buf)
230 call delete('XtestPopup')
231endfunc
232
233func Test_popup_with_syntax_setbufvar()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200234 CheckScreendump
235
Bram Moolenaar402502d2019-05-30 22:07:36 +0200236 let lines =<< trim END
237 call setline(1, range(1, 100))
238 hi PopupColor ctermbg=lightgrey
239 let winid = popup_create([
240 \ '#include <stdio.h>',
241 \ 'int main(void)',
242 \ '{',
Bram Moolenaare089c3f2019-07-09 20:25:25 +0200243 \ "\tprintf(567);",
Bram Moolenaar402502d2019-05-30 22:07:36 +0200244 \ '}',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200245 \], #{line: 3, col: 21, highlight: 'PopupColor'})
Bram Moolenaar402502d2019-05-30 22:07:36 +0200246 call setbufvar(winbufnr(winid), '&syntax', 'cpp')
247 END
248 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200249 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaarb4230122019-05-30 18:40:53 +0200250 call VerifyScreenDump(buf, 'Test_popupwin_11', {})
251
252 " clean up
253 call StopVimInTerminal(buf)
254 call delete('XtestPopup')
255endfunc
256
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200257func Test_popup_with_matches()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200258 CheckScreendump
259
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200260 let lines =<< trim END
261 call setline(1, ['111 222 333', '444 555 666'])
262 let winid = popup_create([
263 \ '111 222 333',
264 \ '444 555 666',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200265 \], #{line: 3, col: 10, border: []})
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200266 set hlsearch
Bram Moolenaar024dbd22019-11-02 22:00:15 +0100267 hi VeryBlue ctermfg=blue guifg=blue
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200268 /666
269 call matchadd('ErrorMsg', '111')
Bram Moolenaar024dbd22019-11-02 22:00:15 +0100270 call matchadd('VeryBlue', '444')
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200271 call win_execute(winid, "call matchadd('ErrorMsg', '111')")
Bram Moolenaar024dbd22019-11-02 22:00:15 +0100272 call win_execute(winid, "call matchadd('VeryBlue', '555')")
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200273 END
274 call writefile(lines, 'XtestPopupMatches')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200275 let buf = RunVimInTerminal('-S XtestPopupMatches', #{rows: 10})
Bram Moolenaarac2450a2019-06-09 18:04:28 +0200276 call VerifyScreenDump(buf, 'Test_popupwin_matches', {})
277
278 " clean up
279 call StopVimInTerminal(buf)
280 call delete('XtestPopupMatches')
281endfunc
282
Bram Moolenaar399d8982019-06-02 15:34:29 +0200283func Test_popup_all_corners()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200284 CheckScreendump
285
Bram Moolenaar399d8982019-06-02 15:34:29 +0200286 let lines =<< trim END
287 call setline(1, repeat([repeat('-', 60)], 15))
288 set so=0
289 normal 2G3|r#
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200290 let winid1 = popup_create(['first', 'second'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200291 \ line: 'cursor+1',
292 \ col: 'cursor',
293 \ pos: 'topleft',
294 \ border: [],
295 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200296 \ })
Bram Moolenaarb754b5b2019-10-24 19:25:00 +0200297 normal 24|r@
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200298 let winid1 = popup_create(['First', 'SeconD'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200299 \ line: 'cursor+1',
300 \ col: 'cursor',
301 \ pos: 'topright',
302 \ border: [],
303 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200304 \ })
Bram Moolenaarb754b5b2019-10-24 19:25:00 +0200305 normal 9G27|r%
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200306 let winid1 = popup_create(['fiRSt', 'seCOnd'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200307 \ line: 'cursor-1',
308 \ col: 'cursor',
309 \ pos: 'botleft',
310 \ border: [],
311 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200312 \ })
Bram Moolenaarb754b5b2019-10-24 19:25:00 +0200313 normal 48|r&
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200314 let winid1 = popup_create(['FIrsT', 'SEcoND'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200315 \ line: 'cursor-1',
316 \ col: 'cursor',
317 \ pos: 'botright',
318 \ border: [],
319 \ padding: [],
Bram Moolenaar399d8982019-06-02 15:34:29 +0200320 \ })
Bram Moolenaarb754b5b2019-10-24 19:25:00 +0200321 normal 1G51|r*
322 let winid1 = popup_create(['one', 'two'], #{
323 \ line: 'cursor-1',
324 \ col: 'cursor',
325 \ pos: 'botleft',
326 \ border: [],
327 \ padding: [],
328 \ })
Bram Moolenaar399d8982019-06-02 15:34:29 +0200329 END
330 call writefile(lines, 'XtestPopupCorners')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200331 let buf = RunVimInTerminal('-S XtestPopupCorners', #{rows: 12})
Bram Moolenaar399d8982019-06-02 15:34:29 +0200332 call VerifyScreenDump(buf, 'Test_popupwin_corners', {})
333
334 " clean up
335 call StopVimInTerminal(buf)
336 call delete('XtestPopupCorners')
337endfunc
338
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100339func Test_popup_nospace()
340 CheckScreendump
341
342 let lines =<< trim END
343 call setline(1, repeat([repeat('-', 60)], 15))
344 set so=0
345
346 " cursor in a line in top half, using "botleft" with popup that
347 " does fit
348 normal 5G2|r@
349 let winid1 = popup_create(['one', 'two'], #{
350 \ line: 'cursor-1',
351 \ col: 'cursor',
352 \ pos: 'botleft',
353 \ border: [],
354 \ })
355 " cursor in a line in top half, using "botleft" with popup that
356 " doesn't fit: gets truncated
357 normal 5G9|r#
358 let winid1 = popup_create(['one', 'two', 'tee'], #{
359 \ line: 'cursor-1',
360 \ col: 'cursor',
361 \ pos: 'botleft',
362 \ posinvert: 0,
363 \ border: [],
364 \ })
365 " cursor in a line in top half, using "botleft" with popup that
366 " doesn't fit and 'posinvert' set: flips to below.
367 normal 5G16|r%
368 let winid1 = popup_create(['one', 'two', 'tee'], #{
369 \ line: 'cursor-1',
370 \ col: 'cursor',
371 \ pos: 'botleft',
372 \ border: [],
373 \ })
374 " cursor in a line in bottom half, using "botleft" with popup that
375 " doesn't fit: does not flip.
376 normal 8G23|r*
377 let winid1 = popup_create(['aaa', 'bbb', 'ccc', 'ddd', 'eee', 'fff'], #{
378 \ line: 'cursor-1',
379 \ col: 'cursor',
380 \ pos: 'botleft',
381 \ border: [],
382 \ })
383
384 " cursor in a line in bottom half, using "topleft" with popup that
385 " does fit
386 normal 8G30|r@
387 let winid1 = popup_create(['one', 'two'], #{
388 \ line: 'cursor+1',
389 \ col: 'cursor',
390 \ pos: 'topleft',
391 \ border: [],
392 \ })
393 " cursor in a line in top half, using "topleft" with popup that
394 " doesn't fit: truncated
395 normal 8G37|r#
396 let winid1 = popup_create(['one', 'two', 'tee'], #{
397 \ line: 'cursor+1',
398 \ col: 'cursor',
399 \ pos: 'topleft',
400 \ posinvert: 0,
401 \ border: [],
402 \ })
403 " cursor in a line in top half, using "topleft" with popup that
Bram Moolenaar5c6b6182019-11-10 17:51:38 +0100404 " doesn't fit and "posinvert" set: flips to above.
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100405 normal 8G44|r%
Bram Moolenaar5c6b6182019-11-10 17:51:38 +0100406 let winid1 = popup_create(['one', 'two', 'tee', 'fou', 'fiv'], #{
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100407 \ line: 'cursor+1',
408 \ col: 'cursor',
409 \ pos: 'topleft',
410 \ border: [],
411 \ })
412 " cursor in a line in top half, using "topleft" with popup that
413 " doesn't fit: does not flip.
414 normal 5G51|r*
415 let winid1 = popup_create(['aaa', 'bbb', 'ccc', 'ddd', 'eee', 'fff'], #{
416 \ line: 'cursor+1',
417 \ col: 'cursor',
418 \ pos: 'topleft',
419 \ border: [],
420 \ })
421 END
422 call writefile(lines, 'XtestPopupNospace')
423 let buf = RunVimInTerminal('-S XtestPopupNospace', #{rows: 12})
424 call VerifyScreenDump(buf, 'Test_popupwin_nospace', {})
425
426 " clean up
427 call StopVimInTerminal(buf)
428 call delete('XtestPopupNospace')
429endfunc
430
Bram Moolenaaref6b9792020-05-13 16:34:15 +0200431func Test_popup_firstline_dump()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200432 CheckScreendump
433
Bram Moolenaar8d241042019-06-12 23:40:01 +0200434 let lines =<< trim END
435 call setline(1, range(1, 20))
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200436 let winid = popup_create(['1111', '222222', '33333', '44', '5', '666666', '77777', '888', '9999999999999999'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200437 \ maxheight: 4,
438 \ firstline: 3,
Bram Moolenaar8d241042019-06-12 23:40:01 +0200439 \ })
440 END
441 call writefile(lines, 'XtestPopupFirstline')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200442 let buf = RunVimInTerminal('-S XtestPopupFirstline', #{rows: 10})
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200443 call VerifyScreenDump(buf, 'Test_popupwin_firstline_1', {})
444
445 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: -1})\<CR>")
446 call term_sendkeys(buf, ":\<CR>")
447 call VerifyScreenDump(buf, 'Test_popupwin_firstline_2', {})
Bram Moolenaar8d241042019-06-12 23:40:01 +0200448
449 " clean up
450 call StopVimInTerminal(buf)
451 call delete('XtestPopupFirstline')
Bram Moolenaaref6b9792020-05-13 16:34:15 +0200452endfunc
Bram Moolenaarae943152019-06-16 22:54:14 +0200453
Bram Moolenaaref6b9792020-05-13 16:34:15 +0200454func Test_popup_firstline()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200455 let winid = popup_create(['1111', '222222', '33333', '44444'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200456 \ maxheight: 2,
457 \ firstline: 3,
Bram Moolenaarae943152019-06-16 22:54:14 +0200458 \ })
459 call assert_equal(3, popup_getoptions(winid).firstline)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200460 call popup_setoptions(winid, #{firstline: 1})
Bram Moolenaarae943152019-06-16 22:54:14 +0200461 call assert_equal(1, popup_getoptions(winid).firstline)
Bram Moolenaar6a124e62019-09-04 18:15:19 +0200462 eval winid->popup_close()
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +0200463
464 let winid = popup_create(['xxx']->repeat(50), #{
465 \ maxheight: 3,
466 \ firstline: 11,
467 \ })
468 redraw
469 call assert_equal(11, popup_getoptions(winid).firstline)
470 call assert_equal(11, popup_getpos(winid).firstline)
Bram Moolenaar8e0a8e72019-09-02 22:56:24 +0200471 " check line() works with popup window
472 call assert_equal(11, line('.', winid))
473 call assert_equal(50, line('$', winid))
474 call assert_equal(0, line('$', 123456))
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +0200475
476 " Normal command changes what is displayed but not "firstline"
477 call win_execute(winid, "normal! \<c-y>")
478 call assert_equal(11, popup_getoptions(winid).firstline)
479 call assert_equal(10, popup_getpos(winid).firstline)
480
481 " Making some property change applies "firstline" again
482 call popup_setoptions(winid, #{line: 4})
483 call assert_equal(11, popup_getoptions(winid).firstline)
484 call assert_equal(11, popup_getpos(winid).firstline)
485
486 " Remove "firstline" property and scroll
487 call popup_setoptions(winid, #{firstline: 0})
488 call win_execute(winid, "normal! \<c-y>")
489 call assert_equal(0, popup_getoptions(winid).firstline)
490 call assert_equal(10, popup_getpos(winid).firstline)
491
492 " Making some property change has no side effect
493 call popup_setoptions(winid, #{line: 3})
494 call assert_equal(0, popup_getoptions(winid).firstline)
495 call assert_equal(10, popup_getpos(winid).firstline)
Bram Moolenaaref6b9792020-05-13 16:34:15 +0200496 call popup_close(winid)
Bram Moolenaarae943152019-06-16 22:54:14 +0200497
Bram Moolenaar30efcf32019-11-03 22:29:38 +0100498 " CTRL-D scrolls down half a page
499 let winid = popup_create(['xxx']->repeat(50), #{
500 \ maxheight: 8,
501 \ })
502 redraw
503 call assert_equal(1, popup_getpos(winid).firstline)
504 call win_execute(winid, "normal! \<C-D>")
505 call assert_equal(5, popup_getpos(winid).firstline)
506 call win_execute(winid, "normal! \<C-D>")
507 call assert_equal(9, popup_getpos(winid).firstline)
508 call win_execute(winid, "normal! \<C-U>")
509 call assert_equal(5, popup_getpos(winid).firstline)
510
511 call win_execute(winid, "normal! \<C-F>")
512 call assert_equal(11, popup_getpos(winid).firstline)
513 call win_execute(winid, "normal! \<C-B>")
514 call assert_equal(5, popup_getpos(winid).firstline)
515
Bram Moolenaarae943152019-06-16 22:54:14 +0200516 call popup_close(winid)
Bram Moolenaar8d241042019-06-12 23:40:01 +0200517endfunc
518
Bram Moolenaar99ca9c42020-09-22 21:55:41 +0200519func Test_popup_firstline_cursorline()
520 let winid = popup_create(['1111', '222222', '33333', '44444'], #{
521 \ maxheight: 2,
522 \ firstline: 3,
523 \ cursorline: 1,
524 \ })
525 call assert_equal(3, popup_getoptions(winid).firstline)
526 call assert_equal(3, getwininfo(winid)[0].topline)
527 call assert_equal(3, getcurpos(winid)[1])
528
529 call popup_close(winid)
530endfunc
531
Bram Moolenaara112f2d2019-09-01 17:38:09 +0200532func Test_popup_noscrolloff()
533 set scrolloff=5
534 let winid = popup_create(['xxx']->repeat(50), #{
535 \ maxheight: 5,
536 \ firstline: 11,
537 \ })
538 redraw
539 call assert_equal(11, popup_getoptions(winid).firstline)
540 call assert_equal(11, popup_getpos(winid).firstline)
541
542 call popup_setoptions(winid, #{firstline: 0})
543 call win_execute(winid, "normal! \<c-y>")
544 call assert_equal(0, popup_getoptions(winid).firstline)
545 call assert_equal(10, popup_getpos(winid).firstline)
546
547 call popup_close(winid)
548endfunc
549
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200550func Test_popup_drag()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200551 CheckScreendump
552
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200553 " create a popup that covers the command line
554 let lines =<< trim END
555 call setline(1, range(1, 20))
Bram Moolenaar356375f2019-08-24 14:46:29 +0200556 split
557 vsplit
558 $wincmd w
559 vsplit
560 1wincmd w
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200561 let winid = popup_create(['1111', '222222', '33333'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200562 \ drag: 1,
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200563 \ resize: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200564 \ border: [],
565 \ line: &lines - 4,
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200566 \ })
567 func Dragit()
568 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
569 endfunc
570 map <silent> <F3> :call test_setmouse(&lines - 4, &columns / 2)<CR>
Bram Moolenaar356375f2019-08-24 14:46:29 +0200571 map <silent> <F4> :call test_setmouse(&lines - 8, &columns / 2 - 20)<CR>
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200572 func Resize()
573 call feedkeys("\<F5>\<LeftMouse>\<F6>\<LeftDrag>\<LeftRelease>", "xt")
574 endfunc
Bram Moolenaar356375f2019-08-24 14:46:29 +0200575 map <silent> <F5> :call test_setmouse(6, 21)<CR>
576 map <silent> <F6> :call test_setmouse(7, 25)<CR>
Bram Moolenaarbfc57862021-11-26 15:57:40 +0000577 func ClickAndDrag()
578 call feedkeys("\<F7>\<LeftMouse>\<LeftRelease>", "xt")
579 call feedkeys("\<F8>\<LeftMouse>\<F9>\<LeftDrag>\<LeftRelease>", "xt")
580 endfunc
581 map <silent> <F7> :call test_setmouse(5, 2)<CR>
582 map <silent> <F8> :call test_setmouse(3, 14)<CR>
583 map <silent> <F9> :call test_setmouse(3, 18)<CR>
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200584 END
585 call writefile(lines, 'XtestPopupDrag')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200586 let buf = RunVimInTerminal('-S XtestPopupDrag', #{rows: 10})
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200587 call VerifyScreenDump(buf, 'Test_popupwin_drag_01', {})
588
589 call term_sendkeys(buf, ":call Dragit()\<CR>")
590 call VerifyScreenDump(buf, 'Test_popupwin_drag_02', {})
591
Bram Moolenaar9bcb70c2019-08-01 21:11:05 +0200592 call term_sendkeys(buf, ":call Resize()\<CR>")
593 call VerifyScreenDump(buf, 'Test_popupwin_drag_03', {})
594
Bram Moolenaarbfc57862021-11-26 15:57:40 +0000595 " dragging works after click on a status line
596 call term_sendkeys(buf, ":call ClickAndDrag()\<CR>")
597 call VerifyScreenDump(buf, 'Test_popupwin_drag_04', {})
598
Bram Moolenaara540f8a2019-06-14 19:23:57 +0200599 " clean up
600 call StopVimInTerminal(buf)
601 call delete('XtestPopupDrag')
602endfunc
603
Bram Moolenaar35910f22020-07-12 19:24:10 +0200604func Test_popup_drag_termwin()
605 CheckUnix
606 CheckScreendump
607 CheckFeature terminal
608
609 " create a popup that covers the terminal window
610 let lines =<< trim END
Bram Moolenaar452143c2020-07-15 17:38:21 +0200611 set foldmethod=marker
612 call setline(1, range(100))
613 for nr in range(7)
614 call setline(nr * 12 + 1, "fold {{{")
Bram Moolenaar9d489562020-07-30 20:08:50 +0200615 call setline(nr * 12 + 11, "end }}}")
Bram Moolenaar452143c2020-07-15 17:38:21 +0200616 endfor
617 %foldclose
Bram Moolenaar35910f22020-07-12 19:24:10 +0200618 set shell=/bin/sh noruler
Bram Moolenaar4b2ce122020-11-21 21:41:41 +0100619 unlet $PROMPT_COMMAND
Bram Moolenaara4dc6f92020-07-12 19:52:36 +0200620 let $PS1 = 'vim> '
Bram Moolenaar452143c2020-07-15 17:38:21 +0200621 terminal ++rows=4
Bram Moolenaar35910f22020-07-12 19:24:10 +0200622 $wincmd w
623 let winid = popup_create(['1111', '2222'], #{
624 \ drag: 1,
625 \ resize: 1,
626 \ border: [],
627 \ line: 3,
628 \ })
Bram Moolenaar452143c2020-07-15 17:38:21 +0200629 func DragitLeft()
Bram Moolenaar35910f22020-07-12 19:24:10 +0200630 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
631 endfunc
Bram Moolenaar452143c2020-07-15 17:38:21 +0200632 func DragitDown()
633 call feedkeys("\<F4>\<LeftMouse>\<F5>\<LeftDrag>\<LeftRelease>", "xt")
634 endfunc
635 func DragitDownLeft()
636 call feedkeys("\<F5>\<LeftMouse>\<F6>\<LeftDrag>\<LeftRelease>", "xt")
637 endfunc
Bram Moolenaar35910f22020-07-12 19:24:10 +0200638 map <silent> <F3> :call test_setmouse(3, &columns / 2)<CR>
639 map <silent> <F4> :call test_setmouse(3, &columns / 2 - 20)<CR>
Bram Moolenaar452143c2020-07-15 17:38:21 +0200640 map <silent> <F5> :call test_setmouse(12, &columns / 2)<CR>
641 map <silent> <F6> :call test_setmouse(12, &columns / 2 - 20)<CR>
Bram Moolenaar35910f22020-07-12 19:24:10 +0200642 END
643 call writefile(lines, 'XtestPopupTerm')
Bram Moolenaar452143c2020-07-15 17:38:21 +0200644 let buf = RunVimInTerminal('-S XtestPopupTerm', #{rows: 16})
Bram Moolenaar35910f22020-07-12 19:24:10 +0200645 call VerifyScreenDump(buf, 'Test_popupwin_term_01', {})
646
Bram Moolenaar452143c2020-07-15 17:38:21 +0200647 call term_sendkeys(buf, ":call DragitLeft()\<CR>")
Bram Moolenaar35910f22020-07-12 19:24:10 +0200648 call VerifyScreenDump(buf, 'Test_popupwin_term_02', {})
649
Bram Moolenaar452143c2020-07-15 17:38:21 +0200650 call term_sendkeys(buf, ":call DragitDown()\<CR>")
651 call VerifyScreenDump(buf, 'Test_popupwin_term_03', {})
652
653 call term_sendkeys(buf, ":call DragitDownLeft()\<CR>")
654 call VerifyScreenDump(buf, 'Test_popupwin_term_04', {})
655
Bram Moolenaar35910f22020-07-12 19:24:10 +0200656 " clean up
657 call StopVimInTerminal(buf)
658 call delete('XtestPopupTerm')
659endfunc
660
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200661func Test_popup_close_with_mouse()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200662 CheckScreendump
663
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200664 let lines =<< trim END
665 call setline(1, range(1, 20))
666 " With border, can click on X
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200667 let winid = popup_create('foobar', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200668 \ close: 'button',
669 \ border: [],
670 \ line: 1,
671 \ col: 1,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200672 \ })
673 func CloseMsg(id, result)
674 echomsg 'Popup closed with ' .. a:result
675 endfunc
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200676 let winid = popup_create('notification', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200677 \ close: 'click',
678 \ line: 3,
679 \ col: 15,
680 \ callback: 'CloseMsg',
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200681 \ })
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200682 let winid = popup_create('no border here', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200683 \ close: 'button',
684 \ line: 5,
685 \ col: 3,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200686 \ })
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200687 let winid = popup_create('only padding', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200688 \ close: 'button',
689 \ padding: [],
690 \ line: 5,
691 \ col: 23,
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200692 \ })
693 func CloseWithX()
694 call feedkeys("\<F3>\<LeftMouse>\<LeftRelease>", "xt")
695 endfunc
696 map <silent> <F3> :call test_setmouse(1, len('foobar') + 2)<CR>
697 func CloseWithClick()
698 call feedkeys("\<F4>\<LeftMouse>\<LeftRelease>", "xt")
699 endfunc
700 map <silent> <F4> :call test_setmouse(3, 17)<CR>
Bram Moolenaarf6396232019-08-24 19:36:00 +0200701 func CreateWithMenuFilter()
702 let winid = popup_create('barfoo', #{
703 \ close: 'button',
704 \ filter: 'popup_filter_menu',
705 \ border: [],
706 \ line: 1,
707 \ col: 40,
708 \ })
709 endfunc
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200710 END
711 call writefile(lines, 'XtestPopupClose')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200712 let buf = RunVimInTerminal('-S XtestPopupClose', #{rows: 10})
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200713 call VerifyScreenDump(buf, 'Test_popupwin_close_01', {})
714
715 call term_sendkeys(buf, ":call CloseWithX()\<CR>")
716 call VerifyScreenDump(buf, 'Test_popupwin_close_02', {})
717
718 call term_sendkeys(buf, ":call CloseWithClick()\<CR>")
719 call VerifyScreenDump(buf, 'Test_popupwin_close_03', {})
720
Bram Moolenaarf6396232019-08-24 19:36:00 +0200721 call term_sendkeys(buf, ":call CreateWithMenuFilter()\<CR>")
722 call VerifyScreenDump(buf, 'Test_popupwin_close_04', {})
723
724 " We have to send the actual mouse code, feedkeys() would be caught the
725 " filter.
726 call term_sendkeys(buf, "\<Esc>[<0;47;1M")
727 call VerifyScreenDump(buf, 'Test_popupwin_close_05', {})
728
Bram Moolenaar2e62b562019-06-30 18:07:00 +0200729 " clean up
730 call StopVimInTerminal(buf)
731 call delete('XtestPopupClose')
732endfunction
733
Bram Moolenaar7b3d9392019-10-16 22:17:07 +0200734func Test_popup_menu_wrap()
735 CheckScreendump
736
737 let lines =<< trim END
738 call setline(1, range(1, 20))
739 call popup_create([
740 \ 'one',
741 \ 'asdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdfas',
742 \ 'three',
743 \ 'four',
744 \ ], #{
745 \ pos: "botleft",
746 \ border: [],
747 \ padding: [0,1,0,1],
748 \ maxheight: 3,
749 \ cursorline: 1,
750 \ filter: 'popup_filter_menu',
751 \ })
752 END
753 call writefile(lines, 'XtestPopupWrap')
754 let buf = RunVimInTerminal('-S XtestPopupWrap', #{rows: 10})
755 call VerifyScreenDump(buf, 'Test_popupwin_wrap_1', {})
756
757 call term_sendkeys(buf, "jj")
758 call VerifyScreenDump(buf, 'Test_popupwin_wrap_2', {})
759
760 " clean up
761 call term_sendkeys(buf, "\<Esc>")
762 call StopVimInTerminal(buf)
763 call delete('XtestPopupWrap')
764endfunction
765
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200766func Test_popup_with_mask()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200767 CheckScreendump
768
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200769 let lines =<< trim END
Bram Moolenaarba45f1f2019-07-03 22:50:41 +0200770 call setline(1, repeat([join(range(1, 42), '')], 13))
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200771 hi PopupColor ctermbg=lightgrey
772 let winid = popup_create([
773 \ 'some text',
774 \ 'another line',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200775 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200776 \ line: 1,
777 \ col: 10,
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100778 \ posinvert: 0,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200779 \ wrap: 0,
780 \ fixed: 1,
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +0200781 \ scrollbar: v:false,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200782 \ zindex: 90,
783 \ padding: [],
784 \ highlight: 'PopupColor',
785 \ mask: [[1,1,1,1], [-5,-1,4,4], [7,9,2,3], [2,4,3,3]]})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200786 call popup_create([
787 \ 'xxxxxxxxx',
788 \ 'yyyyyyyyy',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200789 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200790 \ line: 3,
791 \ col: 18,
792 \ zindex: 20})
Bram Moolenaarba45f1f2019-07-03 22:50:41 +0200793 let winidb = popup_create([
794 \ 'just one line',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200795 \], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200796 \ line: 7,
797 \ col: 10,
Bram Moolenaar638a4a72019-11-06 19:25:22 +0100798 \ posinvert: 0,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200799 \ wrap: 0,
800 \ fixed: 1,
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +0200801 \ scrollbar: v:false,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200802 \ close: 'button',
803 \ zindex: 90,
804 \ padding: [],
805 \ border: [],
806 \ 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 +0200807 END
808 call writefile(lines, 'XtestPopupMask')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200809 let buf = RunVimInTerminal('-S XtestPopupMask', #{rows: 13})
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200810 call VerifyScreenDump(buf, 'Test_popupwin_mask_1', {})
811
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200812 call term_sendkeys(buf, ":call popup_move(winid, #{col: 11, line: 2})\<CR>")
813 call term_sendkeys(buf, ":call popup_move(winidb, #{col: 12})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200814 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200815 call VerifyScreenDump(buf, 'Test_popupwin_mask_2', {})
816
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200817 call term_sendkeys(buf, ":call popup_move(winid, #{col: 65, line: 2})\<CR>")
818 call term_sendkeys(buf, ":call popup_move(winidb, #{col: 63})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200819 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaard529ba52019-07-02 23:13:53 +0200820 call VerifyScreenDump(buf, 'Test_popupwin_mask_3', {})
821
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200822 call term_sendkeys(buf, ":call popup_move(winid, #{pos: 'topright', col: 12, line: 2})\<CR>")
823 call term_sendkeys(buf, ":call popup_move(winidb, #{pos: 'topright', col: 12})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200824 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaard529ba52019-07-02 23:13:53 +0200825 call VerifyScreenDump(buf, 'Test_popupwin_mask_4', {})
826
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200827 call term_sendkeys(buf, ":call popup_move(winid, #{pos: 'topright', col: 12, line: 11})\<CR>")
828 call term_sendkeys(buf, ":call popup_move(winidb, #{pos: 'topleft', col: 42, line: 11})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200829 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaarb4207472019-07-12 16:05:45 +0200830 call VerifyScreenDump(buf, 'Test_popupwin_mask_5', {})
831
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200832 " clean up
833 call StopVimInTerminal(buf)
834 call delete('XtestPopupMask')
Bram Moolenaar4012d262020-12-29 11:57:46 +0100835
836 " this was causing a crash
837 call popup_create('test', #{mask: [[0, 0, 0, 0]]})
838 call popup_clear()
Bram Moolenaar10ccfb22021-02-13 21:31:18 +0100839
840 " this was causing an internal error
841 enew
842 set nowrap
843 call repeat('x', &columns)->setline(1)
844 call prop_type_add('textprop', {})
845 call prop_add(1, 1, #{length: &columns, type: 'textprop'})
846 vsplit
847 let opts = popup_create('', #{textprop: 'textprop'})
848 \ ->popup_getoptions()
849 \ ->extend(#{mask: [[1, 1, 1, 1]]})
850 call popup_create('', opts)
851 redraw
852
853 close!
854 bwipe!
855 call prop_type_delete('textprop')
856 call popup_clear()
857 set wrap&
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200858endfunc
859
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200860func Test_popup_select()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +0200861 CheckScreendump
862 CheckFeature clipboard_working
863
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200864 " create a popup with some text to be selected
865 let lines =<< trim END
Bram Moolenaar1755ec42019-06-15 13:13:54 +0200866 set clipboard=autoselect
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200867 call setline(1, range(1, 20))
Bram Moolenaar4dd751b2019-08-17 19:10:53 +0200868 let winid = popup_create(['the word', 'some more', 'several words here', 'invisible', '5', '6', '7'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200869 \ drag: 1,
870 \ border: [],
871 \ line: 3,
872 \ col: 10,
Bram Moolenaar4dd751b2019-08-17 19:10:53 +0200873 \ maxheight: 3,
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200874 \ })
875 func Select1()
876 call feedkeys("\<F3>\<LeftMouse>\<F4>\<LeftDrag>\<LeftRelease>", "xt")
877 endfunc
878 map <silent> <F3> :call test_setmouse(4, 15)<CR>
879 map <silent> <F4> :call test_setmouse(6, 23)<CR>
880 END
881 call writefile(lines, 'XtestPopupSelect')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200882 let buf = RunVimInTerminal('-S XtestPopupSelect', #{rows: 10})
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200883 call term_sendkeys(buf, ":call Select1()\<CR>")
884 call VerifyScreenDump(buf, 'Test_popupwin_select_01', {})
885
886 call term_sendkeys(buf, ":call popup_close(winid)\<CR>")
887 call term_sendkeys(buf, "\"*p")
Bram Moolenaar8ccabf62019-07-12 18:12:51 +0200888 " clean the command line, sometimes it still shows a command
889 call term_sendkeys(buf, ":\<esc>")
890
Bram Moolenaarbd75b532019-06-14 23:41:55 +0200891 call VerifyScreenDump(buf, 'Test_popupwin_select_02', {})
892
893 " clean up
894 call StopVimInTerminal(buf)
895 call delete('XtestPopupSelect')
896endfunc
897
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200898func Test_popup_in_tab()
899 " default popup is local to tab, not visible when in other tab
900 let winid = popup_create("text", {})
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200901 let bufnr = winbufnr(winid)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200902 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200903 call assert_equal(0, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200904 tabnew
905 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200906 call assert_equal(1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200907 quit
908 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200909
910 call assert_equal(1, bufexists(bufnr))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200911 call popup_clear()
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +0200912 " buffer is gone now
913 call assert_equal(0, bufexists(bufnr))
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200914
915 " global popup is visible in any tab
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200916 let winid = popup_create("text", #{tabpage: -1})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200917 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200918 call assert_equal(-1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200919 tabnew
920 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaara3fce622019-06-20 02:31:49 +0200921 call assert_equal(-1, popup_getoptions(winid).tabpage)
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200922 quit
923 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200924 call popup_clear()
Bram Moolenaara3fce622019-06-20 02:31:49 +0200925
926 " create popup in other tab
927 tabnew
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200928 let winid = popup_create("text", #{tabpage: 1})
Bram Moolenaara3fce622019-06-20 02:31:49 +0200929 call assert_equal(0, popup_getpos(winid).visible)
930 call assert_equal(1, popup_getoptions(winid).tabpage)
931 quit
932 call assert_equal(1, popup_getpos(winid).visible)
933 call assert_equal(0, popup_getoptions(winid).tabpage)
934 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200935endfunc
936
937func Test_popup_valid_arguments()
Bram Moolenaaref6b9792020-05-13 16:34:15 +0200938 call assert_equal(0, len(popup_list()))
939
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200940 " Zero value is like the property wasn't there
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200941 let winid = popup_create("text", #{col: 0})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200942 let pos = popup_getpos(winid)
943 call assert_inrange(&columns / 2 - 1, &columns / 2 + 1, pos.col)
Bram Moolenaaref6b9792020-05-13 16:34:15 +0200944 call assert_equal([winid], popup_list())
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200945 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200946
947 " using cursor column has minimum value of 1
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200948 let winid = popup_create("text", #{col: 'cursor-100'})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200949 let pos = popup_getpos(winid)
950 call assert_equal(1, pos.col)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200951 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200952
953 " center
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200954 let winid = popup_create("text", #{pos: 'center'})
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200955 let pos = popup_getpos(winid)
956 let around = (&columns - pos.width) / 2
957 call assert_inrange(around - 1, around + 1, pos.col)
958 let around = (&lines - pos.height) / 2
959 call assert_inrange(around - 1, around + 1, pos.line)
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200960 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200961endfunc
962
963func Test_popup_invalid_arguments()
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +0200964 call assert_fails('call popup_create(666, {})', 'E86:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200965 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200966 call assert_fails('call popup_create("text", "none")', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200967 call popup_clear()
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200968 call assert_fails('call popup_create(test_null_string(), {})', 'E450:')
969 call assert_fails('call popup_create(test_null_list(), {})', 'E450:')
970 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200971
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200972 call assert_fails('call popup_create("text", #{col: "xxx"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200973 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200974 call assert_fails('call popup_create("text", #{col: "cursor8"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200975 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200976 call assert_fails('call popup_create("text", #{col: "cursor+x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200977 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200978 call assert_fails('call popup_create("text", #{col: "cursor+8x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200979 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200980
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200981 call assert_fails('call popup_create("text", #{line: "xxx"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200982 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200983 call assert_fails('call popup_create("text", #{line: "cursor8"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200984 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200985 call assert_fails('call popup_create("text", #{line: "cursor+x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200986 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200987 call assert_fails('call popup_create("text", #{line: "cursor+8x"})', 'E15:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200988 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200989
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200990 call assert_fails('call popup_create("text", #{pos: "there"})', 'E475:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200991 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200992 call assert_fails('call popup_create("text", #{padding: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200993 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200994 call assert_fails('call popup_create("text", #{border: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200995 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200996 call assert_fails('call popup_create("text", #{borderhighlight: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200997 call popup_clear()
Bram Moolenaar403d0902019-07-17 21:37:32 +0200998 call assert_fails('call popup_create("text", #{borderhighlight: test_null_list()})', 'E714:')
999 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001000 call assert_fails('call popup_create("text", #{borderchars: "none"})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001001 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001002
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001003 call assert_fails('call popup_create([#{text: "text"}, 666], {})', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001004 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001005 call assert_fails('call popup_create([#{text: "text", props: "none"}], {})', 'E714:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001006 call popup_clear()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001007 call assert_fails('call popup_create([#{text: "text", props: ["none"]}], {})', 'E715:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001008 call popup_clear()
Bram Moolenaarb0992022020-01-30 14:55:42 +01001009 call assert_fails('call popup_create([#{text: "text", props: range(3)}], {})', 'E715:')
1010 call popup_clear()
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +02001011 call assert_fails('call popup_create("text", #{mask: ["asdf"]})', 'E475:')
1012 call popup_clear()
Bram Moolenaarb0992022020-01-30 14:55:42 +01001013 call assert_fails('call popup_create("text", #{mask: range(5)})', 'E475:')
1014 call popup_clear()
1015 call popup_create("text", #{mask: [range(4)]})
1016 call popup_clear()
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +02001017 call assert_fails('call popup_create("text", #{mask: test_null_list()})', 'E475:')
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001018 call assert_fails('call popup_create("text", #{mapping: []})', 'E745:')
Bram Moolenaarcfdbc5a2019-07-17 21:27:52 +02001019 call popup_clear()
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02001020 call assert_fails('call popup_create("text", #{tabpage : 4})', 'E997:')
1021 call popup_clear()
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001022endfunc
1023
Bram Moolenaareea16992019-05-31 17:34:48 +02001024func Test_win_execute_closing_curwin()
1025 split
1026 let winid = popup_create('some text', {})
Bram Moolenaare2e40752020-09-04 21:18:46 +02001027 call assert_fails('call win_execute(winid, winnr() .. "close")', 'E994:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001028 call popup_clear()
Bram Moolenaar49540192019-12-11 19:34:54 +01001029
1030 let winid = popup_create('some text', {})
Bram Moolenaare2e40752020-09-04 21:18:46 +02001031 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 +01001032 call popup_clear()
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001033endfunc
1034
1035func Test_win_execute_not_allowed()
1036 let winid = popup_create('some text', {})
1037 call assert_fails('call win_execute(winid, "split")', 'E994:')
1038 call assert_fails('call win_execute(winid, "vsplit")', 'E994:')
1039 call assert_fails('call win_execute(winid, "close")', 'E994:')
1040 call assert_fails('call win_execute(winid, "bdelete")', 'E994:')
Bram Moolenaar2d247842019-06-01 17:06:25 +02001041 call assert_fails('call win_execute(winid, "bwipe!")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001042 call assert_fails('call win_execute(winid, "tabnew")', 'E994:')
1043 call assert_fails('call win_execute(winid, "tabnext")', 'E994:')
1044 call assert_fails('call win_execute(winid, "next")', 'E994:')
1045 call assert_fails('call win_execute(winid, "rewind")', 'E994:')
Bram Moolenaar3a2505c2020-03-09 16:40:41 +01001046 call assert_fails('call win_execute(winid, "pedit filename")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001047 call assert_fails('call win_execute(winid, "buf")', 'E994:')
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01001048 call assert_fails('call win_execute(winid, "bnext")', 'E994:')
1049 call assert_fails('call win_execute(winid, "bprev")', 'E994:')
1050 call assert_fails('call win_execute(winid, "bfirst")', 'E994:')
1051 call assert_fails('call win_execute(winid, "blast")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001052 call assert_fails('call win_execute(winid, "edit")', 'E994:')
1053 call assert_fails('call win_execute(winid, "enew")', 'E994:')
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +02001054 call assert_fails('call win_execute(winid, "help")', 'E994:')
1055 call assert_fails('call win_execute(winid, "1only")', 'E994:')
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001056 call assert_fails('call win_execute(winid, "wincmd x")', 'E994:')
1057 call assert_fails('call win_execute(winid, "wincmd w")', 'E994:')
1058 call assert_fails('call win_execute(winid, "wincmd t")', 'E994:')
1059 call assert_fails('call win_execute(winid, "wincmd b")', 'E994:')
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001060 call popup_clear()
Bram Moolenaareea16992019-05-31 17:34:48 +02001061endfunc
1062
Bram Moolenaar402502d2019-05-30 22:07:36 +02001063func Test_popup_with_wrap()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001064 CheckScreendump
1065
Bram Moolenaar402502d2019-05-30 22:07:36 +02001066 let lines =<< trim END
1067 call setline(1, range(1, 100))
1068 let winid = popup_create(
1069 \ 'a long line that wont fit',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001070 \ #{line: 3, col: 20, maxwidth: 10, wrap: 1})
Bram Moolenaar402502d2019-05-30 22:07:36 +02001071 END
1072 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001073 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar402502d2019-05-30 22:07:36 +02001074 call VerifyScreenDump(buf, 'Test_popupwin_wrap', {})
1075
1076 " clean up
1077 call StopVimInTerminal(buf)
1078 call delete('XtestPopup')
1079endfunc
1080
1081func Test_popup_without_wrap()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001082 CheckScreendump
1083
Bram Moolenaar402502d2019-05-30 22:07:36 +02001084 let lines =<< trim END
1085 call setline(1, range(1, 100))
1086 let winid = popup_create(
1087 \ 'a long line that wont fit',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001088 \ #{line: 3, col: 20, maxwidth: 10, wrap: 0})
Bram Moolenaar402502d2019-05-30 22:07:36 +02001089 END
1090 call writefile(lines, 'XtestPopup')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001091 let buf = RunVimInTerminal('-S XtestPopup', #{rows: 10})
Bram Moolenaar402502d2019-05-30 22:07:36 +02001092 call VerifyScreenDump(buf, 'Test_popupwin_nowrap', {})
1093
1094 " clean up
1095 call StopVimInTerminal(buf)
1096 call delete('XtestPopup')
1097endfunc
1098
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001099func Test_popup_with_showbreak()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001100 CheckScreendump
1101
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001102 let lines =<< trim END
1103 set showbreak=>>\
1104 call setline(1, range(1, 20))
1105 let winid = popup_dialog(
Bram Moolenaar8ae54372019-09-15 18:11:16 +02001106 \ 'a long line here that wraps',
1107 \ #{filter: 'popup_filter_yesno',
1108 \ maxwidth: 12})
Bram Moolenaar331bafd2019-07-20 17:46:05 +02001109 END
1110 call writefile(lines, 'XtestPopupShowbreak')
1111 let buf = RunVimInTerminal('-S XtestPopupShowbreak', #{rows: 10})
1112 call VerifyScreenDump(buf, 'Test_popupwin_showbreak', {})
1113
1114 " clean up
1115 call term_sendkeys(buf, "y")
1116 call StopVimInTerminal(buf)
1117 call delete('XtestPopupShowbreak')
1118endfunc
1119
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001120func Test_popup_time()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001121 CheckFeature timers
1122
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001123 topleft vnew
1124 call setline(1, 'hello')
1125
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001126 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001127 \ line: 1,
1128 \ col: 1,
1129 \ minwidth: 20,
1130 \ time: 500,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001131 \})
1132 redraw
1133 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1134 call assert_equal('world', line)
1135
Bram Moolenaarb4f06282019-07-12 21:07:54 +02001136 call assert_equal(winid, popup_locate(1, 1))
1137 call assert_equal(winid, popup_locate(1, 20))
1138 call assert_equal(0, popup_locate(1, 21))
1139 call assert_equal(0, popup_locate(2, 1))
1140
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001141 sleep 700m
Bram Moolenaar35d5af62019-05-26 20:44:10 +02001142 redraw
Bram Moolenaar196b4662019-09-06 21:34:30 +02001143 let line = join(map(range(1, 5), '1->screenstring(v:val)'), '')
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001144 call assert_equal('hello', line)
1145
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001146 call popup_create('on the command line', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001147 \ line: &lines,
1148 \ col: 10,
1149 \ minwidth: 20,
1150 \ time: 500,
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02001151 \})
1152 redraw
1153 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
1154 call assert_match('.*on the command line.*', line)
1155
1156 sleep 700m
1157 redraw
1158 let line = join(map(range(1, 30), 'screenstring(&lines, v:val)'), '')
1159 call assert_notmatch('.*on the command line.*', line)
1160
1161 bwipe!
1162endfunc
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001163
1164func Test_popup_hide()
1165 topleft vnew
1166 call setline(1, 'hello')
1167
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001168 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001169 \ line: 1,
1170 \ col: 1,
1171 \ minwidth: 20,
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001172 \})
1173 redraw
1174 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1175 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001176 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001177 " buffer is still listed and active
1178 call assert_match(winbufnr(winid) .. 'u a.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001179
1180 call popup_hide(winid)
1181 redraw
1182 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1183 call assert_equal('hello', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001184 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarc6896e22019-05-30 22:32:34 +02001185 " buffer is still listed but hidden
Bram Moolenaarca7c0782020-01-14 20:42:48 +01001186 call assert_match(winbufnr(winid) .. 'u a.*\[Popup\]', execute('ls u'))
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001187
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001188 eval winid->popup_show()
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001189 redraw
1190 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1191 call assert_equal('world', line)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001192 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001193
1194
1195 call popup_close(winid)
1196 redraw
1197 let line = join(map(range(1, 5), 'screenstring(1, v:val)'), '')
1198 call assert_equal('hello', line)
1199
1200 " error is given for existing non-popup window
1201 call assert_fails('call popup_hide(win_getid())', 'E993:')
1202
1203 " no error non-existing window
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001204 eval 1234234->popup_hide()
Bram Moolenaar2cd0dce2019-05-26 22:17:52 +02001205 call popup_show(41234234)
1206
1207 bwipe!
1208endfunc
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001209
1210func Test_popup_move()
1211 topleft vnew
1212 call setline(1, 'hello')
1213
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001214 let winid = popup_create('world', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001215 \ line: 1,
1216 \ col: 1,
1217 \ minwidth: 20,
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001218 \})
1219 redraw
1220 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
1221 call assert_equal('world ', line)
1222
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001223 call popup_move(winid, #{line: 2, col: 2})
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001224 redraw
1225 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
1226 call assert_equal('hello ', line)
1227 let line = join(map(range(1, 6), 'screenstring(2, v:val)'), '')
1228 call assert_equal('~world', line)
1229
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001230 eval winid->popup_move(#{line: 1})
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001231 redraw
1232 let line = join(map(range(1, 6), 'screenstring(1, v:val)'), '')
1233 call assert_equal('hworld', line)
1234
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02001235 call assert_fails('call popup_move(winid, [])', 'E715:')
1236 call assert_fails('call popup_move(winid, test_null_dict())', 'E715:')
1237
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001238 call popup_close(winid)
1239
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02001240 call assert_equal(0, popup_move(-1, {}))
1241
Bram Moolenaar60cdb302019-05-27 21:54:10 +02001242 bwipe!
1243endfunc
Bram Moolenaarbc133542019-05-29 20:26:46 +02001244
Bram Moolenaar402502d2019-05-30 22:07:36 +02001245func Test_popup_getpos()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001246 let winid = popup_create('hello', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001247 \ line: 2,
1248 \ col: 3,
1249 \ minwidth: 10,
1250 \ minheight: 11,
Bram Moolenaarbc133542019-05-29 20:26:46 +02001251 \})
1252 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +02001253 let res = popup_getpos(winid)
Bram Moolenaarbc133542019-05-29 20:26:46 +02001254 call assert_equal(2, res.line)
1255 call assert_equal(3, res.col)
1256 call assert_equal(10, res.width)
1257 call assert_equal(11, res.height)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001258 call assert_equal(1, res.visible)
Bram Moolenaarbc133542019-05-29 20:26:46 +02001259
1260 call popup_close(winid)
1261endfunc
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001262
1263func Test_popup_width_longest()
1264 let tests = [
1265 \ [['hello', 'this', 'window', 'displays', 'all of its text'], 15],
1266 \ [['hello', 'this', 'window', 'all of its text', 'displays'], 15],
1267 \ [['hello', 'this', 'all of its text', 'window', 'displays'], 15],
1268 \ [['hello', 'all of its text', 'this', 'window', 'displays'], 15],
1269 \ [['all of its text', 'hello', 'this', 'window', 'displays'], 15],
1270 \ ]
1271
1272 for test in tests
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001273 let winid = popup_create(test[0], #{line: 2, col: 3})
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001274 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +02001275 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001276 call assert_equal(test[1], position.width)
1277 call popup_close(winid)
1278 endfor
1279endfunc
1280
1281func Test_popup_wraps()
1282 let tests = [
1283 \ ['nowrap', 6, 1],
1284 \ ['a line that wraps once', 12, 2],
1285 \ ['a line that wraps two times', 12, 3],
1286 \ ]
1287 for test in tests
1288 let winid = popup_create(test[0],
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001289 \ #{line: 2, col: 3, maxwidth: 12})
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001290 redraw
Bram Moolenaar402502d2019-05-30 22:07:36 +02001291 let position = popup_getpos(winid)
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001292 call assert_equal(test[1], position.width)
1293 call assert_equal(test[2], position.height)
1294
1295 call popup_close(winid)
Bram Moolenaar402502d2019-05-30 22:07:36 +02001296 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar88c4e1f2019-05-29 23:14:28 +02001297 endfor
1298endfunc
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001299
1300func Test_popup_getoptions()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001301 let winid = popup_create('hello', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001302 \ line: 2,
1303 \ col: 3,
1304 \ minwidth: 10,
1305 \ minheight: 11,
1306 \ maxwidth: 20,
1307 \ maxheight: 21,
1308 \ zindex: 100,
1309 \ time: 5000,
1310 \ fixed: 1
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001311 \})
1312 redraw
1313 let res = popup_getoptions(winid)
1314 call assert_equal(2, res.line)
1315 call assert_equal(3, res.col)
1316 call assert_equal(10, res.minwidth)
1317 call assert_equal(11, res.minheight)
1318 call assert_equal(20, res.maxwidth)
1319 call assert_equal(21, res.maxheight)
1320 call assert_equal(100, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001321 call assert_equal(1, res.fixed)
Bram Moolenaarb8350ab2019-08-04 17:59:49 +02001322 call assert_equal(1, res.mapping)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001323 if has('timers')
1324 call assert_equal(5000, res.time)
1325 endif
1326 call popup_close(winid)
1327
1328 let winid = popup_create('hello', {})
1329 redraw
1330 let res = popup_getoptions(winid)
1331 call assert_equal(0, res.line)
1332 call assert_equal(0, res.col)
1333 call assert_equal(0, res.minwidth)
1334 call assert_equal(0, res.minheight)
1335 call assert_equal(0, res.maxwidth)
1336 call assert_equal(0, res.maxheight)
1337 call assert_equal(50, res.zindex)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001338 call assert_equal(0, res.fixed)
Bram Moolenaar8c2a6002019-05-30 14:29:45 +02001339 if has('timers')
1340 call assert_equal(0, res.time)
1341 endif
1342 call popup_close(winid)
1343 call assert_equal({}, popup_getoptions(winid))
1344endfunc
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001345
1346func Test_popup_option_values()
1347 new
1348 " window-local
1349 setlocal number
1350 setlocal nowrap
1351 " buffer-local
1352 setlocal omnifunc=Something
1353 " global/buffer-local
1354 setlocal path=/there
1355 " global/window-local
Bram Moolenaara112f2d2019-09-01 17:38:09 +02001356 setlocal statusline=2
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001357
1358 let winid = popup_create('hello', {})
1359 call assert_equal(0, getwinvar(winid, '&number'))
1360 call assert_equal(1, getwinvar(winid, '&wrap'))
1361 call assert_equal('', getwinvar(winid, '&omnifunc'))
1362 call assert_equal(&g:path, getwinvar(winid, '&path'))
Bram Moolenaara112f2d2019-09-01 17:38:09 +02001363 call assert_equal(&g:statusline, getwinvar(winid, '&statusline'))
1364
1365 " 'scrolloff' is reset to zero
1366 call assert_equal(5, &scrolloff)
1367 call assert_equal(0, getwinvar(winid, '&scrolloff'))
Bram Moolenaarcacc6a52019-05-30 15:22:43 +02001368
1369 call popup_close(winid)
1370 bwipe
1371endfunc
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001372
1373func Test_popup_atcursor()
1374 topleft vnew
1375 call setline(1, [
1376 \ 'xxxxxxxxxxxxxxxxx',
1377 \ 'xxxxxxxxxxxxxxxxx',
1378 \ 'xxxxxxxxxxxxxxxxx',
1379 \])
1380
1381 call cursor(2, 2)
1382 redraw
1383 let winid = popup_atcursor('vim', {})
1384 redraw
1385 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
1386 call assert_equal('xvimxxxxxxxxxxxxx', line)
1387 call popup_close(winid)
1388
1389 call cursor(3, 4)
1390 redraw
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001391 let winid = 'vim'->popup_atcursor({})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001392 redraw
1393 let line = join(map(range(1, 17), 'screenstring(2, v:val)'), '')
1394 call assert_equal('xxxvimxxxxxxxxxxx', line)
1395 call popup_close(winid)
1396
1397 call cursor(1, 1)
1398 redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001399 let winid = popup_create('vim', #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001400 \ line: 'cursor+2',
1401 \ col: 'cursor+1',
1402 \})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001403 redraw
1404 let line = join(map(range(1, 17), 'screenstring(3, v:val)'), '')
1405 call assert_equal('xvimxxxxxxxxxxxxx', line)
1406 call popup_close(winid)
1407
1408 call cursor(3, 3)
1409 redraw
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001410 let winid = popup_create('vim', #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001411 \ line: 'cursor-2',
1412 \ col: 'cursor-1',
1413 \})
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001414 redraw
1415 let line = join(map(range(1, 17), 'screenstring(1, v:val)'), '')
1416 call assert_equal('xvimxxxxxxxxxxxxx', line)
1417 call popup_close(winid)
1418
Bram Moolenaar402502d2019-05-30 22:07:36 +02001419 " just enough room above
1420 call cursor(3, 3)
1421 redraw
1422 let winid = popup_atcursor(['vim', 'is great'], {})
1423 redraw
1424 let pos = popup_getpos(winid)
1425 call assert_equal(1, pos.line)
1426 call popup_close(winid)
1427
1428 " not enough room above, popup goes below the cursor
1429 call cursor(3, 3)
1430 redraw
1431 let winid = popup_atcursor(['vim', 'is', 'great'], {})
1432 redraw
1433 let pos = popup_getpos(winid)
1434 call assert_equal(4, pos.line)
1435 call popup_close(winid)
1436
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001437 " cursor in first line, popup in line 2
1438 call cursor(1, 1)
1439 redraw
1440 let winid = popup_atcursor(['vim', 'is', 'great'], {})
1441 redraw
1442 let pos = popup_getpos(winid)
1443 call assert_equal(2, pos.line)
1444 call popup_close(winid)
1445
Bram Moolenaarcc31ad92019-05-30 19:25:06 +02001446 bwipe!
1447endfunc
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001448
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001449func Test_popup_atcursor_pos()
1450 CheckScreendump
Dominique Pellec60e9592021-07-03 21:41:38 +02001451 CheckFeature conceal
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001452
1453 let lines =<< trim END
1454 call setline(1, repeat([repeat('-', 60)], 15))
1455 set so=0
1456
1457 normal 9G3|r#
1458 let winid1 = popup_atcursor(['first', 'second'], #{
1459 \ moved: [0, 0, 0],
1460 \ })
1461 normal 9G21|r&
1462 let winid1 = popup_atcursor(['FIrsT', 'SEcoND'], #{
1463 \ pos: 'botright',
1464 \ moved: [0, 0, 0],
1465 \ })
1466 normal 3G27|r%
1467 let winid1 = popup_atcursor(['fiRSt', 'seCOnd'], #{
1468 \ pos: 'topleft',
1469 \ moved: [0, 0, 0],
1470 \ })
1471 normal 3G45|r@
1472 let winid1 = popup_atcursor(['First', 'SeconD'], #{
1473 \ pos: 'topright',
Bram Moolenaarb0992022020-01-30 14:55:42 +01001474 \ moved: range(3),
1475 \ mousemoved: range(3),
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001476 \ })
Bram Moolenaarea042672021-06-29 20:22:32 +02001477
1478 normal 9G27|Rconcealed X
1479 syn match Hidden /concealed/ conceal
1480 set conceallevel=2 concealcursor=n
1481 redraw
1482 normal 0fX
1483 call popup_atcursor('mark', {})
Bram Moolenaar4dd8fe02019-11-09 15:33:31 +01001484 END
1485 call writefile(lines, 'XtestPopupAtcursorPos')
1486 let buf = RunVimInTerminal('-S XtestPopupAtcursorPos', #{rows: 12})
1487 call VerifyScreenDump(buf, 'Test_popupwin_atcursor_pos', {})
1488
1489 " clean up
1490 call StopVimInTerminal(buf)
1491 call delete('XtestPopupAtcursorPos')
1492endfunc
1493
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001494func Test_popup_beval()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001495 CheckScreendump
Bram Moolenaar1e82a782019-09-21 22:57:06 +02001496 CheckFeature balloon_eval_term
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001497
1498 let lines =<< trim END
1499 call setline(1, range(1, 20))
1500 call setline(5, 'here is some text to hover over')
1501 set balloonevalterm
1502 set balloonexpr=BalloonExpr()
1503 set balloondelay=100
1504 func BalloonExpr()
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001505 let s:winid = [v:beval_text]->popup_beval({})
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001506 return ''
1507 endfunc
1508 func Hover()
1509 call test_setmouse(5, 15)
1510 call feedkeys("\<MouseMove>\<Ignore>", "xt")
1511 sleep 100m
1512 endfunc
1513 func MoveOntoPopup()
1514 call test_setmouse(4, 17)
1515 call feedkeys("\<F4>\<MouseMove>\<Ignore>", "xt")
1516 endfunc
1517 func MoveAway()
1518 call test_setmouse(5, 13)
1519 call feedkeys("\<F5>\<MouseMove>\<Ignore>", "xt")
1520 endfunc
1521 END
1522 call writefile(lines, 'XtestPopupBeval')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001523 let buf = RunVimInTerminal('-S XtestPopupBeval', #{rows: 10})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001524 call TermWait(buf, 50)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02001525 call term_sendkeys(buf, 'j')
1526 call term_sendkeys(buf, ":call Hover()\<CR>")
1527 call VerifyScreenDump(buf, 'Test_popupwin_beval_1', {})
1528
1529 call term_sendkeys(buf, ":call MoveOntoPopup()\<CR>")
1530 call VerifyScreenDump(buf, 'Test_popupwin_beval_2', {})
1531
1532 call term_sendkeys(buf, ":call MoveAway()\<CR>")
1533 call VerifyScreenDump(buf, 'Test_popupwin_beval_3', {})
1534
1535 " clean up
1536 call StopVimInTerminal(buf)
1537 call delete('XtestPopupBeval')
1538endfunc
1539
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001540func Test_popup_filter()
1541 new
1542 call setline(1, 'some text')
1543
1544 func MyPopupFilter(winid, c)
Bram Moolenaarec084d32020-02-28 22:44:47 +01001545 if a:c == 'e' || a:c == "\<F9>"
1546 let g:eaten = a:c
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001547 return 1
1548 endif
1549 if a:c == '0'
1550 let g:ignored = '0'
1551 return 0
1552 endif
1553 if a:c == 'x'
1554 call popup_close(a:winid)
1555 return 1
1556 endif
1557 return 0
1558 endfunc
1559
Bram Moolenaar6a124e62019-09-04 18:15:19 +02001560 let winid = 'something'->popup_create(#{filter: 'MyPopupFilter'})
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001561 redraw
1562
1563 " e is consumed by the filter
Bram Moolenaar999db232021-07-04 14:00:55 +02001564 let g:eaten = ''
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001565 call feedkeys('e', 'xt')
1566 call assert_equal('e', g:eaten)
Bram Moolenaarec084d32020-02-28 22:44:47 +01001567 call feedkeys("\<F9>", 'xt')
1568 call assert_equal("\<F9>", g:eaten)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001569
1570 " 0 is ignored by the filter
Bram Moolenaar999db232021-07-04 14:00:55 +02001571 let g:ignored = ''
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001572 normal $
1573 call assert_equal(9, getcurpos()[2])
1574 call feedkeys('0', 'xt')
1575 call assert_equal('0', g:ignored)
Bram Moolenaar999db232021-07-04 14:00:55 +02001576
1577 if has('win32') && has('gui_running')
1578 echo "FIXME: this check is very flaky on MS-Windows GUI, the cursor doesn't move"
1579 else
1580 call assert_equal(1, getcurpos()[2])
1581 endif
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001582
1583 " x closes the popup
1584 call feedkeys('x', 'xt')
Bram Moolenaarec084d32020-02-28 22:44:47 +01001585 call assert_equal("\<F9>", g:eaten)
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001586 call assert_equal(-1, winbufnr(winid))
1587
Bram Moolenaar999db232021-07-04 14:00:55 +02001588 unlet g:eaten
1589 unlet g:ignored
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001590 delfunc MyPopupFilter
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02001591 call popup_clear()
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02001592endfunc
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001593
Bram Moolenaar4934ad02020-09-28 22:29:58 +02001594" this tests that the filter is not used for :normal command
Bram Moolenaar189832b2020-09-23 12:29:11 +02001595func Test_popup_filter_normal_cmd()
1596 CheckScreendump
1597
1598 let lines =<< trim END
Bram Moolenaar4934ad02020-09-28 22:29:58 +02001599 let text = range(1, 20)->map({_, v -> string(v)})
1600 let g:winid = popup_create(text, #{maxheight: 5, minwidth: 3, filter: 'invalidfilter'})
1601 call timer_start(0, {-> win_execute(g:winid, 'norm! 10Gzz')})
Bram Moolenaar189832b2020-09-23 12:29:11 +02001602 END
1603 call writefile(lines, 'XtestPopupNormal')
1604 let buf = RunVimInTerminal('-S XtestPopupNormal', #{rows: 10})
1605 call TermWait(buf, 100)
1606 call VerifyScreenDump(buf, 'Test_popupwin_normal_cmd', {})
1607
1608 call StopVimInTerminal(buf)
1609 call delete('XtestPopupNormal')
1610endfunc
1611
Bram Moolenaar10476522020-09-24 22:57:31 +02001612" test that cursor line highlight is updated after using win_execute()
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02001613func Test_popup_filter_win_execute()
1614 CheckScreendump
1615
1616 let lines =<< trim END
Bram Moolenaar10476522020-09-24 22:57:31 +02001617 let lines = range(1, &lines * 2)->map({_, v -> string(v)})
1618 let g:id = popup_create(lines, #{
1619 \ minheight: &lines - 5,
1620 \ maxheight: &lines - 5,
1621 \ cursorline: 1,
1622 \ })
1623 redraw
1624 END
1625 call writefile(lines, 'XtestPopupWinExecute')
1626 let buf = RunVimInTerminal('-S XtestPopupWinExecute', #{rows: 14})
1627
1628 call term_sendkeys(buf, ":call win_execute(g:id, ['normal 17Gzz'])\<CR>")
1629 call term_sendkeys(buf, ":\<CR>")
1630
1631 call VerifyScreenDump(buf, 'Test_popupwin_win_execute_cursorline', {})
1632
1633 call StopVimInTerminal(buf)
1634 call delete('XtestPopupWinExecute')
1635endfunc
1636
Bram Moolenaar3697c9b2020-09-26 22:03:00 +02001637func Test_popup_set_firstline()
1638 CheckScreendump
1639
1640 let lines =<< trim END
1641 let lines = range(1, 50)->map({_, v -> string(v)})
1642 let g:id = popup_create(lines, #{
1643 \ minwidth: 20,
1644 \ maxwidth: 20,
1645 \ minheight: &lines - 5,
1646 \ maxheight: &lines - 5,
1647 \ cursorline: 1,
1648 \ })
1649 call popup_setoptions(g:id, #{firstline: 10})
1650 redraw
1651 END
1652 call writefile(lines, 'XtestPopupWinSetFirstline')
1653 let buf = RunVimInTerminal('-S XtestPopupWinSetFirstline', #{rows: 16})
1654
1655 call VerifyScreenDump(buf, 'Test_popupwin_set_firstline_1', {})
1656
1657 call term_sendkeys(buf, ":call popup_setoptions(g:id, #{firstline: 5})\<CR>")
1658 call term_sendkeys(buf, ":\<CR>")
1659 call VerifyScreenDump(buf, 'Test_popupwin_set_firstline_2', {})
1660
1661 call StopVimInTerminal(buf)
1662 call delete('XtestPopupWinSetFirstline')
1663endfunc
1664
Bram Moolenaar10476522020-09-24 22:57:31 +02001665" this tests that we don't get stuck with an error in "win_execute()"
1666func Test_popup_filter_win_execute_error()
1667 CheckScreendump
1668
1669 let lines =<< trim END
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02001670 let g:winid = popup_create('some text', {'filter': 'invalidfilter'})
1671 call timer_start(0, {-> win_execute(g:winid, 'invalidCommand')})
1672 END
Bram Moolenaar10476522020-09-24 22:57:31 +02001673 call writefile(lines, 'XtestPopupWinExecuteError')
1674 let buf = RunVimInTerminal('-S XtestPopupWinExecuteError', #{rows: 10, wait_for_ruler: 0})
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02001675
1676 call WaitFor({-> term_getline(buf, 9) =~ 'Not an editor command: invalidCommand'})
1677 call term_sendkeys(buf, "\<CR>")
1678 call WaitFor({-> term_getline(buf, 9) =~ 'Unknown function: invalidfilter'})
1679 call term_sendkeys(buf, "\<CR>")
1680 call WaitFor({-> term_getline(buf, 9) =~ 'Not allowed in a popup window'})
1681 call term_sendkeys(buf, "\<CR>")
1682 call term_sendkeys(buf, "\<CR>")
1683 call VerifyScreenDump(buf, 'Test_popupwin_win_execute', {})
1684
1685 call StopVimInTerminal(buf)
Bram Moolenaar10476522020-09-24 22:57:31 +02001686 call delete('XtestPopupWinExecuteError')
Bram Moolenaar6bf1b522020-09-23 17:41:26 +02001687endfunc
1688
Bram Moolenaara42d9452019-06-15 21:46:30 +02001689func ShowDialog(key, result)
1690 let s:cb_res = 999
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001691 let winid = popup_dialog('do you want to quit (Yes/no)?', #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001692 \ filter: 'popup_filter_yesno',
1693 \ callback: 'QuitCallback',
Bram Moolenaara42d9452019-06-15 21:46:30 +02001694 \ })
1695 redraw
1696 call feedkeys(a:key, "xt")
1697 call assert_equal(winid, s:cb_winid)
1698 call assert_equal(a:result, s:cb_res)
1699endfunc
1700
1701func Test_popup_dialog()
1702 func QuitCallback(id, res)
1703 let s:cb_winid = a:id
1704 let s:cb_res = a:res
1705 endfunc
1706
1707 let winid = ShowDialog("y", 1)
1708 let winid = ShowDialog("Y", 1)
1709 let winid = ShowDialog("n", 0)
1710 let winid = ShowDialog("N", 0)
1711 let winid = ShowDialog("x", 0)
1712 let winid = ShowDialog("X", 0)
1713 let winid = ShowDialog("\<Esc>", 0)
1714 let winid = ShowDialog("\<C-C>", -1)
1715
1716 delfunc QuitCallback
1717endfunc
1718
Bram Moolenaara730e552019-06-16 19:05:31 +02001719func ShowMenu(key, result)
1720 let s:cb_res = 999
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001721 let winid = popup_menu(['one', 'two', 'something else'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001722 \ callback: 'QuitCallback',
Bram Moolenaara730e552019-06-16 19:05:31 +02001723 \ })
1724 redraw
1725 call feedkeys(a:key, "xt")
1726 call assert_equal(winid, s:cb_winid)
1727 call assert_equal(a:result, s:cb_res)
1728endfunc
1729
1730func Test_popup_menu()
1731 func QuitCallback(id, res)
1732 let s:cb_winid = a:id
1733 let s:cb_res = a:res
1734 endfunc
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001735 " mapping won't be used in popup
1736 map j k
Bram Moolenaara730e552019-06-16 19:05:31 +02001737
1738 let winid = ShowMenu(" ", 1)
1739 let winid = ShowMenu("j \<CR>", 2)
1740 let winid = ShowMenu("JjK \<CR>", 2)
1741 let winid = ShowMenu("jjjjjj ", 3)
1742 let winid = ShowMenu("kkk ", 1)
1743 let winid = ShowMenu("x", -1)
1744 let winid = ShowMenu("X", -1)
1745 let winid = ShowMenu("\<Esc>", -1)
1746 let winid = ShowMenu("\<C-C>", -1)
1747
1748 delfunc QuitCallback
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02001749 unmap j
Bram Moolenaara730e552019-06-16 19:05:31 +02001750endfunc
1751
1752func Test_popup_menu_screenshot()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001753 CheckScreendump
Bram Moolenaara730e552019-06-16 19:05:31 +02001754
1755 let lines =<< trim END
1756 call setline(1, range(1, 20))
1757 hi PopupSelected ctermbg=lightblue
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001758 call popup_menu(['one', 'two', 'another'], #{callback: 'MenuDone', title: ' make a choice from the list '})
Bram Moolenaara730e552019-06-16 19:05:31 +02001759 func MenuDone(id, res)
1760 echomsg "selected " .. a:res
1761 endfunc
1762 END
1763 call writefile(lines, 'XtestPopupMenu')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001764 let buf = RunVimInTerminal('-S XtestPopupMenu', #{rows: 10})
Bram Moolenaara730e552019-06-16 19:05:31 +02001765 call VerifyScreenDump(buf, 'Test_popupwin_menu_01', {})
1766
1767 call term_sendkeys(buf, "jj")
1768 call VerifyScreenDump(buf, 'Test_popupwin_menu_02', {})
1769
1770 call term_sendkeys(buf, " ")
1771 call VerifyScreenDump(buf, 'Test_popupwin_menu_03', {})
1772
1773 " clean up
1774 call StopVimInTerminal(buf)
1775 call delete('XtestPopupMenu')
1776endfunc
1777
Bram Moolenaarf914a332019-07-20 15:09:56 +02001778func Test_popup_menu_narrow()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001779 CheckScreendump
Bram Moolenaarf914a332019-07-20 15:09:56 +02001780
1781 let lines =<< trim END
1782 call setline(1, range(1, 20))
1783 hi PopupSelected ctermbg=green
1784 call popup_menu(['one', 'two', 'three'], #{callback: 'MenuDone'})
1785 func MenuDone(id, res)
1786 echomsg "selected " .. a:res
1787 endfunc
1788 END
1789 call writefile(lines, 'XtestPopupNarrowMenu')
1790 let buf = RunVimInTerminal('-S XtestPopupNarrowMenu', #{rows: 10})
1791 call VerifyScreenDump(buf, 'Test_popupwin_menu_04', {})
1792
1793 " clean up
1794 call term_sendkeys(buf, "x")
1795 call StopVimInTerminal(buf)
1796 call delete('XtestPopupNarrowMenu')
1797endfunc
1798
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001799func Test_popup_title()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001800 CheckScreendump
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001801
1802 " Create a popup without title or border, a line of padding will be added to
1803 " put the title on.
1804 let lines =<< trim END
1805 call setline(1, range(1, 20))
Bram Moolenaar5d458a72019-08-04 21:12:15 +02001806 let winid = popup_create(['one', 'two', 'another'], #{title: 'Title String'})
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001807 END
1808 call writefile(lines, 'XtestPopupTitle')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001809 let buf = RunVimInTerminal('-S XtestPopupTitle', #{rows: 10})
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001810 call VerifyScreenDump(buf, 'Test_popupwin_title', {})
1811
Bram Moolenaar5d458a72019-08-04 21:12:15 +02001812 call term_sendkeys(buf, ":call popup_setoptions(winid, #{maxwidth: 20, title: 'a very long title that is not going to fit'})\<CR>")
1813 call term_sendkeys(buf, ":\<CR>")
1814 call VerifyScreenDump(buf, 'Test_popupwin_longtitle_1', {})
1815
1816 call term_sendkeys(buf, ":call popup_setoptions(winid, #{border: []})\<CR>")
1817 call term_sendkeys(buf, ":\<CR>")
1818 call VerifyScreenDump(buf, 'Test_popupwin_longtitle_2', {})
1819
Bram Moolenaar3ae50c72020-12-12 18:18:06 +01001820 call term_sendkeys(buf, ":call popup_clear()\<CR>")
1821 call term_sendkeys(buf, ":call popup_create(['aaa', 'bbb'], #{title: 'Title', minwidth: 12, padding: [2, 2, 2, 2]})\<CR>")
1822 call term_sendkeys(buf, ":\<CR>")
1823 call VerifyScreenDump(buf, 'Test_popupwin_longtitle_3', {})
1824
1825 call term_sendkeys(buf, ":call popup_clear()\<CR>")
1826 call term_sendkeys(buf, ":call popup_create(['aaa', 'bbb'], #{title: 'Title', minwidth: 12, border: [], padding: [2, 2, 2, 2]})\<CR>")
1827 call term_sendkeys(buf, ":\<CR>")
1828 call VerifyScreenDump(buf, 'Test_popupwin_longtitle_4', {})
1829
Ralf Schandlbc869872021-05-28 14:12:14 +02001830 call term_sendkeys(buf, ":call popup_clear()\<CR>")
rbtnnc6119412021-08-07 13:08:45 +02001831 call term_sendkeys(buf, ":call popup_menu(['This is a line', 'and another line'], #{title: '▶Äあいうえお◀', })\<CR>")
Ralf Schandlbc869872021-05-28 14:12:14 +02001832 call VerifyScreenDump(buf, 'Test_popupwin_multibytetitle', {})
1833 call term_sendkeys(buf, "x")
1834
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001835 " clean up
1836 call StopVimInTerminal(buf)
1837 call delete('XtestPopupTitle')
Bram Moolenaarae943152019-06-16 22:54:14 +02001838
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001839 let winid = popup_create('something', #{title: 'Some Title'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001840 call assert_equal('Some Title', popup_getoptions(winid).title)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001841 call popup_setoptions(winid, #{title: 'Another Title'})
Bram Moolenaarae943152019-06-16 22:54:14 +02001842 call assert_equal('Another Title', popup_getoptions(winid).title)
1843
1844 call popup_clear()
Bram Moolenaareb2310d2019-06-16 20:09:10 +02001845endfunc
1846
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001847func Test_popup_close_callback()
1848 func PopupDone(id, result)
1849 let g:result = a:result
1850 endfunc
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001851 let winid = popup_create('something', #{callback: 'PopupDone'})
Bram Moolenaar9eaac892019-06-01 22:49:29 +02001852 redraw
1853 call popup_close(winid, 'done')
1854 call assert_equal('done', g:result)
1855endfunc
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001856
1857func Test_popup_empty()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001858 let winid = popup_create('', #{padding: [2,2,2,2]})
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001859 redraw
1860 let pos = popup_getpos(winid)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001861 call assert_equal(5, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001862 call assert_equal(5, pos.height)
Bram Moolenaaref6b9792020-05-13 16:34:15 +02001863 call popup_close(winid)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001864
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001865 let winid = popup_create([], #{border: []})
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001866 redraw
1867 let pos = popup_getpos(winid)
Bram Moolenaardc2ce582019-06-16 15:32:14 +02001868 call assert_equal(3, pos.width)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001869 call assert_equal(3, pos.height)
Bram Moolenaaref6b9792020-05-13 16:34:15 +02001870 call popup_close(winid)
Bram Moolenaar7b29dd82019-06-02 13:22:11 +02001871endfunc
Bram Moolenaar988c4332019-06-02 14:12:11 +02001872
1873func Test_popup_never_behind()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02001874 CheckScreendump
1875
Bram Moolenaar988c4332019-06-02 14:12:11 +02001876 " +-----------------------------+
1877 " | | |
1878 " | | |
1879 " | | |
1880 " | line1 |
1881 " |------------line2------------|
1882 " | line3 |
1883 " | line4 |
1884 " | |
1885 " | |
1886 " +-----------------------------+
1887 let lines =<< trim END
Bram Moolenaar988c4332019-06-02 14:12:11 +02001888 split
1889 vsplit
1890 let info_window1 = getwininfo()[0]
1891 let line = info_window1['height']
1892 let col = info_window1['width']
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001893 call popup_create(['line1', 'line2', 'line3', 'line4'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02001894 \ line : line,
1895 \ col : col,
Bram Moolenaar988c4332019-06-02 14:12:11 +02001896 \ })
1897 END
1898 call writefile(lines, 'XtestPopupBehind')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02001899 let buf = RunVimInTerminal('-S XtestPopupBehind', #{rows: 10})
Bram Moolenaar988c4332019-06-02 14:12:11 +02001900 call term_sendkeys(buf, "\<C-W>w")
1901 call VerifyScreenDump(buf, 'Test_popupwin_behind', {})
1902
1903 " clean up
1904 call StopVimInTerminal(buf)
1905 call delete('XtestPopupBehind')
1906endfunc
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001907
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001908func s:VerifyPosition(p, msg, line, col, width, height)
1909 call assert_equal(a:line, popup_getpos(a:p).line, a:msg . ' (l)')
1910 call assert_equal(a:col, popup_getpos(a:p).col, a:msg . ' (c)')
1911 call assert_equal(a:width, popup_getpos(a:p).width, a:msg . ' (w)')
1912 call assert_equal(a:height, popup_getpos(a:p).height, a:msg . ' (h)')
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001913endfunc
1914
1915func Test_popup_position_adjust()
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001916 " Anything placed past the last cell on the right of the screen is moved to
1917 " the left.
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001918 "
1919 " When wrapping is disabled, we also shift to the left to display on the
1920 " screen, unless fixed is set.
1921
1922 " Entries for cases which don't vary based on wrapping.
1923 " Format is per tests described below
1924 let both_wrap_tests = [
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001925 \ ['a', 5, &columns, 5, &columns, 1, 1],
1926 \ ['b', 5, &columns + 1, 5, &columns, 1, 1],
1927 \ ['c', 5, &columns - 1, 5, &columns - 1, 1, 1],
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001928 \ ['d', 5, &columns - 2, 5, &columns - 2, 1, 1],
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001929 \ ['e', 5, &columns - 3, 5, &columns - 3, 1, 1]]
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02001930
1931 " these test groups are dicts with:
1932 " - comment: something to identify the group of tests by
1933 " - options: dict of options to merge with the row/col in tests
1934 " - tests: list of cases. Each one is a list with elements:
1935 " - text
1936 " - row
1937 " - col
1938 " - expected row
1939 " - expected col
1940 " - expected width
1941 " - expected height
1942 let tests = [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001943 \ #{
1944 \ comment: 'left-aligned with wrapping',
1945 \ options: #{
1946 \ wrap: 1,
1947 \ pos: 'botleft',
1948 \ },
1949 \ tests: both_wrap_tests + [
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001950 \ ['aa', 5, &columns, 4, &columns, 1, 2],
1951 \ ['bb', 5, &columns + 1, 4, &columns, 1, 2],
1952 \ ['cc', 5, &columns - 1, 5, &columns - 1, 2, 1],
1953 \ ['dd', 5, &columns - 2, 5, &columns - 2, 2, 1],
1954 \ ['ee', 5, &columns - 3, 5, &columns - 3, 2, 1],
1955 \
1956 \ ['aaa', 5, &columns, 3, &columns, 1, 3],
1957 \ ['bbb', 5, &columns + 1, 3, &columns, 1, 3],
1958 \ ['ccc', 5, &columns - 1, 4, &columns - 1, 2, 2],
1959 \ ['ddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
1960 \ ['eee', 5, &columns - 3, 5, &columns - 3, 3, 1],
1961 \
1962 \ ['aaaa', 5, &columns, 2, &columns, 1, 4],
1963 \ ['bbbb', 5, &columns + 1, 2, &columns, 1, 4],
1964 \ ['cccc', 5, &columns - 1, 4, &columns - 1, 2, 2],
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001965 \ ['dddd', 5, &columns - 2, 4, &columns - 2, 3, 2],
1966 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001967 \ ['eeee', 5, &columns - 4, 5, &columns - 4, 4, 1],
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001968 \ ],
1969 \ },
1970 \ #{
1971 \ comment: 'left aligned without wrapping',
1972 \ options: #{
1973 \ wrap: 0,
1974 \ pos: 'botleft',
1975 \ },
1976 \ tests: both_wrap_tests + [
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01001977 \ ['aa', 5, &columns, 5, &columns - 1, 2, 1],
1978 \ ['bb', 5, &columns + 1, 5, &columns - 1, 2, 1],
1979 \ ['cc', 5, &columns - 1, 5, &columns - 1, 2, 1],
1980 \ ['dd', 5, &columns - 2, 5, &columns - 2, 2, 1],
1981 \ ['ee', 5, &columns - 3, 5, &columns - 3, 2, 1],
1982 \
1983 \ ['aaa', 5, &columns, 5, &columns - 2, 3, 1],
1984 \ ['bbb', 5, &columns + 1, 5, &columns - 2, 3, 1],
1985 \ ['ccc', 5, &columns - 1, 5, &columns - 2, 3, 1],
1986 \ ['ddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
1987 \ ['eee', 5, &columns - 3, 5, &columns - 3, 3, 1],
1988 \
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02001989 \ ['aaaa', 5, &columns, 5, &columns - 3, 4, 1],
1990 \ ['bbbb', 5, &columns + 1, 5, &columns - 3, 4, 1],
1991 \ ['cccc', 5, &columns - 1, 5, &columns - 3, 4, 1],
1992 \ ['dddd', 5, &columns - 2, 5, &columns - 3, 4, 1],
1993 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
1994 \ ],
1995 \ },
1996 \ #{
1997 \ comment: 'left aligned with fixed position',
1998 \ options: #{
1999 \ wrap: 0,
2000 \ fixed: 1,
2001 \ pos: 'botleft',
2002 \ },
2003 \ tests: both_wrap_tests + [
Bram Moolenaarfbcdf672020-01-06 23:07:48 +01002004 \ ['aa', 5, &columns, 5, &columns, 1, 1],
2005 \ ['bb', 5, &columns + 1, 5, &columns, 1, 1],
2006 \ ['cc', 5, &columns - 1, 5, &columns - 1, 2, 1],
2007 \ ['dd', 5, &columns - 2, 5, &columns - 2, 2, 1],
2008 \ ['ee', 5, &columns - 3, 5, &columns - 3, 2, 1],
2009 \
2010 \ ['aaa', 5, &columns, 5, &columns, 1, 1],
2011 \ ['bbb', 5, &columns + 1, 5, &columns, 1, 1],
2012 \ ['ccc', 5, &columns - 1, 5, &columns - 1, 2, 1],
2013 \ ['ddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
2014 \ ['eee', 5, &columns - 3, 5, &columns - 3, 3, 1],
2015 \
2016 \ ['aaaa', 5, &columns, 5, &columns, 1, 1],
2017 \ ['bbbb', 5, &columns + 1, 5, &columns, 1, 1],
2018 \ ['cccc', 5, &columns - 1, 5, &columns - 1, 2, 1],
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002019 \ ['dddd', 5, &columns - 2, 5, &columns - 2, 3, 1],
2020 \ ['eeee', 5, &columns - 3, 5, &columns - 3, 4, 1],
2021 \ ],
2022 \ },
2023 \ ]
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002024
2025 for test_group in tests
2026 for test in test_group.tests
2027 let [ text, line, col, e_line, e_col, e_width, e_height ] = test
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002028 let options = #{
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002029 \ line: line,
2030 \ col: col,
2031 \ }
2032 call extend(options, test_group.options)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002033
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002034 let p = popup_create(text, options)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002035
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002036 let msg = string(extend(options, #{text: text}))
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002037 call s:VerifyPosition(p, msg, e_line, e_col, e_width, e_height)
2038 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002039 endfor
2040 endfor
2041
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002042 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002043 %bwipe!
2044endfunc
2045
Bram Moolenaar3397f742019-06-02 18:40:06 +02002046func Test_adjust_left_past_screen_width()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002047 " width of screen
2048 let X = join(map(range(&columns), {->'X'}), '')
2049
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002050 let p = popup_create(X, #{line: 1, col: 1, wrap: 0})
2051 call s:VerifyPosition(p, 'full width topleft', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002052
2053 redraw
2054 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
2055 call assert_equal(X, line)
2056
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002057 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002058 redraw
2059
2060 " Same if placed on the right hand side
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002061 let p = popup_create(X, #{line: 1, col: &columns, wrap: 0})
2062 call s:VerifyPosition(p, 'full width topright', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002063
2064 redraw
2065 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
2066 call assert_equal(X, line)
2067
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002068 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002069 redraw
2070
2071 " Extend so > window width
2072 let X .= 'x'
2073
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002074 let p = popup_create(X, #{line: 1, col: 1, wrap: 0})
2075 call s:VerifyPosition(p, 'full width + 1 topleft', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002076
2077 redraw
2078 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
2079 call assert_equal(X[ : -2 ], line)
2080
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002081 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002082 redraw
2083
2084 " Shifted then truncated (the x is not visible)
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002085 let p = popup_create(X, #{line: 1, col: &columns - 3, wrap: 0})
2086 call s:VerifyPosition(p, 'full width + 1 topright', 1, 1, &columns, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002087
2088 redraw
2089 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
2090 call assert_equal(X[ : -2 ], line)
2091
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002092 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002093 redraw
2094
2095 " Not shifted, just truncated
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002096 let p = popup_create(X,
2097 \ #{line: 1, col: 2, wrap: 0, fixed: 1})
2098 call s:VerifyPosition(p, 'full width + 1 fixed', 1, 2, &columns - 1, 1)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002099
2100 redraw
2101 let line = join(map(range(1, &columns + 1), 'screenstring(1, v:val)'), '')
2102 let e_line = ' ' . X[ 1 : -2 ]
2103 call assert_equal(e_line, line)
2104
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002105 call popup_close(p)
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002106 redraw
2107
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002108 call popup_clear()
Bram Moolenaar042fb4b2019-06-02 14:49:56 +02002109 %bwipe!
Bram Moolenaar3397f742019-06-02 18:40:06 +02002110endfunc
2111
2112func Test_popup_moved()
2113 new
2114 call test_override('char_avail', 1)
2115 call setline(1, ['one word to move around', 'a WORD.and->some thing'])
2116
2117 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002118 let winid = popup_atcursor('text', #{moved: 'any'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02002119 redraw
2120 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002121 call assert_equal([1, 4, 4], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02002122 " trigger the check for last_cursormoved by going into insert mode
2123 call feedkeys("li\<Esc>", 'xt')
2124 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002125 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02002126
2127 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002128 let winid = popup_atcursor('text', #{moved: 'word'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02002129 redraw
2130 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002131 call assert_equal([1, 4, 7], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02002132 call feedkeys("hi\<Esc>", 'xt')
2133 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002134 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02002135
2136 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002137 let winid = popup_atcursor('text', #{moved: 'word'})
Bram Moolenaar3397f742019-06-02 18:40:06 +02002138 redraw
2139 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002140 call assert_equal([1, 4, 7], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02002141 call feedkeys("li\<Esc>", 'xt')
2142 call assert_equal(1, popup_getpos(winid).visible)
2143 call feedkeys("ei\<Esc>", 'xt')
2144 call assert_equal(1, popup_getpos(winid).visible)
2145 call feedkeys("eli\<Esc>", 'xt')
2146 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002147 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02002148
Bram Moolenaar17627312019-06-02 19:53:44 +02002149 " WORD is the default
Bram Moolenaar3397f742019-06-02 18:40:06 +02002150 exe "normal gg0/WORD\<CR>"
Bram Moolenaar17627312019-06-02 19:53:44 +02002151 let winid = popup_atcursor('text', {})
Bram Moolenaar3397f742019-06-02 18:40:06 +02002152 redraw
2153 call assert_equal(1, popup_getpos(winid).visible)
Bram Moolenaarb3d17a22019-07-07 18:28:14 +02002154 call assert_equal([2, 2, 15], popup_getoptions(winid).moved)
Bram Moolenaar3397f742019-06-02 18:40:06 +02002155 call feedkeys("eli\<Esc>", 'xt')
2156 call assert_equal(1, popup_getpos(winid).visible)
2157 call feedkeys("wi\<Esc>", 'xt')
2158 call assert_equal(1, popup_getpos(winid).visible)
2159 call feedkeys("Eli\<Esc>", 'xt')
2160 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002161 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02002162
2163 exe "normal gg0/word\<CR>"
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002164 let winid = popup_atcursor('text', #{moved: [5, 10]})
Bram Moolenaar3397f742019-06-02 18:40:06 +02002165 redraw
2166 call assert_equal(1, popup_getpos(winid).visible)
2167 call feedkeys("eli\<Esc>", 'xt')
2168 call feedkeys("ei\<Esc>", 'xt')
2169 call assert_equal(1, popup_getpos(winid).visible)
2170 call feedkeys("eli\<Esc>", 'xt')
2171 call assert_equal({}, popup_getpos(winid))
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +02002172 call popup_clear()
Bram Moolenaar3397f742019-06-02 18:40:06 +02002173
2174 bwipe!
2175 call test_override('ALL', 0)
2176endfunc
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002177
2178func Test_notifications()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002179 CheckFeature timers
2180 CheckScreendump
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002181
Bram Moolenaar0fdddee2019-09-01 15:26:23 +02002182 let lines =<< trim END
2183 call setline(1, range(1, 20))
2184 hi Notification ctermbg=lightblue
2185 call popup_notification('first notification', {})
2186 END
2187 call writefile(lines, 'XtestNotifications')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002188 let buf = RunVimInTerminal('-S XtestNotifications', #{rows: 10})
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002189 call VerifyScreenDump(buf, 'Test_popupwin_notify_01', {})
2190
2191 " second one goes below the first one
Bram Moolenaardfa97f22019-06-15 14:31:55 +02002192 call term_sendkeys(buf, ":hi link PopupNotification Notification\<CR>")
2193 call term_sendkeys(buf, ":call popup_notification('another important notification', {})\<CR>")
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002194 call VerifyScreenDump(buf, 'Test_popupwin_notify_02', {})
2195
Bram Moolenaar68d48f42019-06-12 22:42:41 +02002196 " clean up
2197 call StopVimInTerminal(buf)
2198 call delete('XtestNotifications')
2199endfunc
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002200
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002201func Test_popup_scrollbar()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002202 CheckScreendump
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002203
2204 let lines =<< trim END
2205 call setline(1, range(1, 20))
Bram Moolenaar8da41812019-06-26 18:04:54 +02002206 hi ScrollThumb ctermbg=blue
2207 hi ScrollBar ctermbg=red
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002208 let winid = popup_create(['one', 'two', 'three', 'four', 'five',
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002209 \ 'six', 'seven', 'eight', 'nine'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002210 \ minwidth: 8,
2211 \ maxheight: 4,
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002212 \ })
Bram Moolenaar53a95d62019-06-26 03:54:08 +02002213 func ScrollUp()
2214 call feedkeys("\<F3>\<ScrollWheelUp>", "xt")
2215 endfunc
2216 func ScrollDown()
2217 call feedkeys("\<F3>\<ScrollWheelDown>", "xt")
2218 endfunc
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02002219 func ClickTop()
2220 call feedkeys("\<F4>\<LeftMouse>", "xt")
2221 endfunc
2222 func ClickBot()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002223 call popup_setoptions(g:winid, #{border: [], close: 'button'})
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02002224 call feedkeys("\<F5>\<LeftMouse>", "xt")
2225 endfunc
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002226 func Popup_filter(winid, key)
2227 if a:key == 'j'
Bram Moolenaar371806e2020-10-22 13:44:54 +02002228 silent! this_throws_an_error_but_is_ignored
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002229 let line = popup_getoptions(a:winid).firstline
2230 let nlines = line('$', a:winid)
2231 let newline = line < nlines ? (line + 1) : nlines
2232 call popup_setoptions(a:winid, #{firstline: newline})
2233 return v:true
2234 elseif a:key == 'x'
2235 call popup_close(a:winid)
2236 return v:true
2237 endif
2238 endfunc
2239
Bram Moolenaar6c542f72020-09-27 21:16:45 +02002240 def CreatePopup(text: list<string>)
Bram Moolenaare0de1712020-12-02 17:36:54 +01002241 popup_create(text, {
Bram Moolenaar6c542f72020-09-27 21:16:45 +02002242 \ minwidth: 30,
2243 \ maxwidth: 30,
2244 \ minheight: 4,
2245 \ maxheight: 4,
2246 \ firstline: 1,
2247 \ lastline: 4,
2248 \ wrap: true,
2249 \ scrollbar: true,
2250 \ mapping: false,
2251 \ filter: Popup_filter,
2252 \ })
2253 enddef
2254
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002255 func PopupScroll()
2256 call popup_clear()
2257 let text =<< trim END
2258 1
2259 2
2260 3
2261 4
2262 long line long line long line long line long line long line
2263 long line long line long line long line long line long line
2264 long line long line long line long line long line long line
2265 END
Bram Moolenaar6c542f72020-09-27 21:16:45 +02002266 call CreatePopup(text)
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002267 endfunc
Bram Moolenaar53a95d62019-06-26 03:54:08 +02002268 map <silent> <F3> :call test_setmouse(5, 36)<CR>
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02002269 map <silent> <F4> :call test_setmouse(4, 42)<CR>
2270 map <silent> <F5> :call test_setmouse(7, 42)<CR>
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002271 END
2272 call writefile(lines, 'XtestPopupScroll')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002273 let buf = RunVimInTerminal('-S XtestPopupScroll', #{rows: 10})
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002274 call VerifyScreenDump(buf, 'Test_popupwin_scroll_1', {})
2275
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002276 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 2})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002277 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002278 call VerifyScreenDump(buf, 'Test_popupwin_scroll_2', {})
2279
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002280 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 6})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002281 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002282 call VerifyScreenDump(buf, 'Test_popupwin_scroll_3', {})
2283
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002284 call term_sendkeys(buf, ":call popup_setoptions(winid, #{firstline: 9})\<CR>")
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002285 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002286 call VerifyScreenDump(buf, 'Test_popupwin_scroll_4', {})
2287
Bram Moolenaar9e67b6a2019-08-30 17:34:08 +02002288 call term_sendkeys(buf, ":call popup_setoptions(winid, #{scrollbarhighlight: 'ScrollBar', thumbhighlight: 'ScrollThumb', firstline: 5})\<CR>")
Bram Moolenaara112f2d2019-09-01 17:38:09 +02002289 " this scrolls two lines (half the window height)
Bram Moolenaar53a95d62019-06-26 03:54:08 +02002290 call term_sendkeys(buf, ":call ScrollUp()\<CR>")
2291 call VerifyScreenDump(buf, 'Test_popupwin_scroll_5', {})
2292
2293 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
2294 call VerifyScreenDump(buf, 'Test_popupwin_scroll_6', {})
2295
2296 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
Bram Moolenaar13b47c32019-06-28 21:55:48 +02002297 " wait a bit, otherwise it fails sometimes (double click recognized?)
2298 sleep 100m
Bram Moolenaar53a95d62019-06-26 03:54:08 +02002299 call term_sendkeys(buf, ":call ScrollDown()\<CR>")
2300 call VerifyScreenDump(buf, 'Test_popupwin_scroll_7', {})
2301
Bram Moolenaarf9c85f52019-06-29 07:41:35 +02002302 call term_sendkeys(buf, ":call ClickTop()\<CR>")
2303 sleep 100m
2304 call term_sendkeys(buf, ":call ClickTop()\<CR>")
2305 call VerifyScreenDump(buf, 'Test_popupwin_scroll_8', {})
2306
2307 call term_sendkeys(buf, ":call ClickBot()\<CR>")
2308 call VerifyScreenDump(buf, 'Test_popupwin_scroll_9', {})
2309
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02002310 " remove the minwidth and maxheight
2311 call term_sendkeys(buf, ":call popup_setoptions(winid, #{maxheight: 0, minwidth: 0})\<CR>")
Bram Moolenaar7e0f4622019-09-17 21:23:39 +02002312 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar8c6173c2019-08-30 22:08:34 +02002313 call VerifyScreenDump(buf, 'Test_popupwin_scroll_10', {})
2314
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002315 " check size with non-wrapping lines
2316 call term_sendkeys(buf, ":call PopupScroll()\<CR>")
2317 call VerifyScreenDump(buf, 'Test_popupwin_scroll_11', {})
2318
2319 " check size with wrapping lines
2320 call term_sendkeys(buf, "j")
2321 call VerifyScreenDump(buf, 'Test_popupwin_scroll_12', {})
Bram Moolenaarf2885d32019-11-02 20:21:25 +01002322
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002323 " clean up
Bram Moolenaar20298ce2020-06-19 21:46:52 +02002324 call term_sendkeys(buf, "x")
Bram Moolenaar75fb0852019-06-25 05:15:58 +02002325 call StopVimInTerminal(buf)
2326 call delete('XtestPopupScroll')
2327endfunc
2328
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02002329func Test_popup_too_high_scrollbar()
2330 CheckScreendump
2331
2332 let lines =<< trim END
2333 call setline(1, range(1, 20)->map({i, v -> repeat(v, 10)}))
2334 set scrolloff=0
2335 func ShowPopup()
2336 let winid = popup_atcursor(['one', 'two', 'three', 'four', 'five',
2337 \ 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve'], #{
2338 \ minwidth: 8,
2339 \ border: [],
2340 \ })
2341 endfunc
2342 normal 3G$
2343 call ShowPopup()
2344 END
2345 call writefile(lines, 'XtestPopupToohigh')
2346 let buf = RunVimInTerminal('-S XtestPopupToohigh', #{rows: 10})
2347 call VerifyScreenDump(buf, 'Test_popupwin_toohigh_1', {})
2348
2349 call term_sendkeys(buf, ":call popup_clear()\<CR>")
2350 call term_sendkeys(buf, "8G$")
2351 call term_sendkeys(buf, ":call ShowPopup()\<CR>")
2352 call VerifyScreenDump(buf, 'Test_popupwin_toohigh_2', {})
2353
Bram Moolenaarbf61fdd2020-08-10 20:39:17 +02002354 call term_sendkeys(buf, ":call popup_clear()\<CR>")
2355 call term_sendkeys(buf, "gg$")
2356 call term_sendkeys(buf, ":call ShowPopup()\<CR>")
2357 call VerifyScreenDump(buf, 'Test_popupwin_toohigh_3', {})
2358
Bram Moolenaara1b9b0c2020-08-09 16:37:48 +02002359 " clean up
2360 call StopVimInTerminal(buf)
2361 call delete('XtestPopupToohigh')
2362endfunc
2363
Bram Moolenaar437a7462019-07-05 20:17:22 +02002364func Test_popup_fitting_scrollbar()
2365 " this was causing a crash, divide by zero
2366 let winid = popup_create([
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002367 \ 'one', 'two', 'longer line that wraps', 'four', 'five'], #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002368 \ scrollbar: 1,
2369 \ maxwidth: 10,
2370 \ maxheight: 5,
2371 \ firstline: 2})
Bram Moolenaar437a7462019-07-05 20:17:22 +02002372 redraw
2373 call popup_clear()
2374endfunc
2375
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002376func Test_popup_settext()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002377 CheckScreendump
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002378
2379 let lines =<< trim END
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002380 let opts = #{wrap: 0}
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002381 let p = popup_create('test', opts)
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002382 eval p->popup_settext('this is a text')
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002383 END
2384
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02002385 call writefile(lines, 'XtestPopupSetText')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002386 let buf = RunVimInTerminal('-S XtestPopupSetText', #{rows: 10})
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002387 call VerifyScreenDump(buf, 'Test_popup_settext_01', {})
2388
2389 " Setting to empty string clears it
2390 call term_sendkeys(buf, ":call popup_settext(p, '')\<CR>")
2391 call VerifyScreenDump(buf, 'Test_popup_settext_02', {})
2392
2393 " Setting a list
2394 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
2395 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
2396
2397 " Shrinking with a list
2398 call term_sendkeys(buf, ":call popup_settext(p, ['a'])\<CR>")
2399 call VerifyScreenDump(buf, 'Test_popup_settext_04', {})
2400
2401 " Growing with a list
2402 call term_sendkeys(buf, ":call popup_settext(p, ['a','b','c'])\<CR>")
2403 call VerifyScreenDump(buf, 'Test_popup_settext_03', {})
2404
2405 " Empty list clears
2406 call term_sendkeys(buf, ":call popup_settext(p, [])\<CR>")
2407 call VerifyScreenDump(buf, 'Test_popup_settext_05', {})
2408
2409 " Dicts
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002410 call term_sendkeys(buf, ":call popup_settext(p, [#{text: 'aaaa'}, #{text: 'bbbb'}, #{text: 'cccc'}])\<CR>")
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002411 call VerifyScreenDump(buf, 'Test_popup_settext_06', {})
2412
Bram Moolenaarb0992022020-01-30 14:55:42 +01002413 " range() (doesn't work)
2414 call term_sendkeys(buf, ":call popup_settext(p, range(4, 8))\<CR>")
2415 call VerifyScreenDump(buf, 'Test_popup_settext_07', {})
2416
Bram Moolenaardc2ce582019-06-16 15:32:14 +02002417 " clean up
2418 call StopVimInTerminal(buf)
2419 call delete('XtestPopupSetText')
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002420endfunc
2421
Bram Moolenaar1f42f5a2020-09-03 18:52:24 +02002422func Test_popup_settext_getline()
2423 let id = popup_create('', #{ tabpage: 0 })
2424 call popup_settext(id, ['a','b'])
2425 call assert_equal(2, line('$', id)) " OK :)
2426 call popup_close(id)
2427
2428 let id = popup_create('', #{ tabpage: -1 })
2429 call popup_settext(id, ['a','b'])
2430 call assert_equal(2, line('$', id)) " Fails :(
2431 call popup_close(id)
2432endfunc
2433
Bram Moolenaar74f8eec2020-10-15 19:10:56 +02002434func Test_popup_settext_null()
2435 let id = popup_create('', #{ tabpage: 0 })
2436 call popup_settext(id, test_null_list())
2437 call popup_close(id)
2438
2439 let id = popup_create('', #{ tabpage: 0 })
2440 call popup_settext(id, test_null_string())
2441 call popup_close(id)
2442endfunc
2443
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002444func Test_popup_hidden()
2445 new
2446
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002447 let winid = popup_atcursor('text', #{hidden: 1})
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002448 redraw
2449 call assert_equal(0, popup_getpos(winid).visible)
2450 call popup_close(winid)
2451
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002452 let winid = popup_create('text', #{hidden: 1})
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002453 redraw
2454 call assert_equal(0, popup_getpos(winid).visible)
2455 call popup_close(winid)
2456
2457 func QuitCallback(id, res)
2458 let s:cb_winid = a:id
2459 let s:cb_res = a:res
2460 endfunc
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002461 let winid = 'make a choice'->popup_dialog(#{hidden: 1,
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002462 \ filter: 'popup_filter_yesno',
2463 \ callback: 'QuitCallback',
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002464 \ })
2465 redraw
2466 call assert_equal(0, popup_getpos(winid).visible)
Bram Moolenaarae943152019-06-16 22:54:14 +02002467 call assert_equal(function('popup_filter_yesno'), popup_getoptions(winid).filter)
2468 call assert_equal(function('QuitCallback'), popup_getoptions(winid).callback)
Bram Moolenaar6313c4f2019-06-16 20:39:13 +02002469 exe "normal anot used by filter\<Esc>"
2470 call assert_equal('not used by filter', getline(1))
2471
2472 call popup_show(winid)
2473 call feedkeys('y', "xt")
2474 call assert_equal(1, s:cb_res)
2475
2476 bwipe!
2477 delfunc QuitCallback
2478endfunc
Bram Moolenaarae943152019-06-16 22:54:14 +02002479
2480" Test options not checked elsewhere
2481func Test_set_get_options()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002482 let winid = popup_create('some text', #{highlight: 'Beautiful'})
Bram Moolenaarae943152019-06-16 22:54:14 +02002483 let options = popup_getoptions(winid)
2484 call assert_equal(1, options.wrap)
2485 call assert_equal(0, options.drag)
2486 call assert_equal('Beautiful', options.highlight)
2487
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002488 call popup_setoptions(winid, #{wrap: 0, drag: 1, highlight: 'Another'})
Bram Moolenaarae943152019-06-16 22:54:14 +02002489 let options = popup_getoptions(winid)
2490 call assert_equal(0, options.wrap)
2491 call assert_equal(1, options.drag)
2492 call assert_equal('Another', options.highlight)
2493
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02002494 call assert_fails('call popup_setoptions(winid, [])', 'E715:')
2495 call assert_fails('call popup_setoptions(winid, test_null_dict())', 'E715:')
2496
Bram Moolenaarae943152019-06-16 22:54:14 +02002497 call popup_close(winid)
Bram Moolenaarad48e6c2020-04-21 22:19:45 +02002498 call assert_equal(0, popup_setoptions(winid, options.wrap))
Bram Moolenaarae943152019-06-16 22:54:14 +02002499endfunc
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002500
2501func Test_popupwin_garbage_collect()
2502 func MyPopupFilter(x, winid, c)
2503 " NOP
2504 endfunc
2505
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002506 let winid = popup_create('something', #{filter: function('MyPopupFilter', [{}])})
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002507 call test_garbagecollect_now()
2508 redraw
Bram Moolenaar1bc353b2019-09-01 14:45:28 +02002509 " Must not crash caused by invalid memory access
Bram Moolenaar75a1a942019-06-20 03:45:36 +02002510 call feedkeys('j', 'xt')
2511 call assert_true(v:true)
2512
2513 call popup_close(winid)
2514 delfunc MyPopupFilter
2515endfunc
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002516
Bram Moolenaar581ba392019-09-03 22:08:33 +02002517func Test_popupwin_filter_mode()
2518 func MyPopupFilter(winid, c)
2519 let s:typed = a:c
2520 if a:c == ':' || a:c == "\r" || a:c == 'v'
2521 " can start cmdline mode, get out, and start/stop Visual mode
2522 return 0
2523 endif
2524 return 1
2525 endfunc
2526
2527 " Normal, Visual and Insert mode
2528 let winid = popup_create('something', #{filter: 'MyPopupFilter', filtermode: 'nvi'})
2529 redraw
2530 call feedkeys('x', 'xt')
2531 call assert_equal('x', s:typed)
2532
2533 call feedkeys(":let g:foo = 'foo'\<CR>", 'xt')
2534 call assert_equal(':', s:typed)
2535 call assert_equal('foo', g:foo)
2536
2537 let @x = 'something'
2538 call feedkeys('v$"xy', 'xt')
2539 call assert_equal('y', s:typed)
2540 call assert_equal('something', @x) " yank command is filtered out
2541 call feedkeys('v', 'xt') " end Visual mode
2542
2543 call popup_close(winid)
2544
2545 " only Normal mode
2546 let winid = popup_create('something', #{filter: 'MyPopupFilter', filtermode: 'n'})
2547 redraw
2548 call feedkeys('x', 'xt')
2549 call assert_equal('x', s:typed)
2550
2551 call feedkeys(":let g:foo = 'foo'\<CR>", 'xt')
2552 call assert_equal(':', s:typed)
2553 call assert_equal('foo', g:foo)
2554
2555 let @x = 'something'
2556 call feedkeys('v$"xy', 'xt')
2557 call assert_equal('v', s:typed)
2558 call assert_notequal('something', @x)
2559
2560 call popup_close(winid)
2561
2562 " default: all modes
2563 let winid = popup_create('something', #{filter: 'MyPopupFilter'})
2564 redraw
2565 call feedkeys('x', 'xt')
2566 call assert_equal('x', s:typed)
2567
2568 let g:foo = 'bar'
2569 call feedkeys(":let g:foo = 'foo'\<CR>", 'xt')
2570 call assert_equal("\r", s:typed)
2571 call assert_equal('bar', g:foo)
2572
2573 let @x = 'something'
2574 call feedkeys('v$"xy', 'xt')
2575 call assert_equal('y', s:typed)
2576 call assert_equal('something', @x) " yank command is filtered out
2577 call feedkeys('v', 'xt') " end Visual mode
2578
2579 call popup_close(winid)
2580 delfunc MyPopupFilter
2581endfunc
2582
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002583func Test_popupwin_filter_mouse()
2584 func MyPopupFilter(winid, c)
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002585 let g:got_mousepos = getmousepos()
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002586 return 0
2587 endfunc
2588
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002589 call setline(1, ['.'->repeat(25)]->repeat(10))
2590 let winid = popup_create(['short', 'long line that will wrap', 'other'], #{
2591 \ line: 2,
2592 \ col: 4,
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002593 \ maxwidth: 12,
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002594 \ padding: [],
2595 \ border: [],
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002596 \ filter: 'MyPopupFilter',
2597 \ })
2598 redraw
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002599 " 123456789012345678901
2600 " 1 .....................
2601 " 2 ...+--------------+..
2602 " 3 ...| |..
2603 " 4 ...| short |..
2604 " 5 ...| long line th |..
2605 " 6 ...| at will wrap |..
2606 " 7 ...| other |..
2607 " 8 ...| |..
2608 " 9 ...+--------------+..
2609 " 10 .....................
2610 let tests = []
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002611
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002612 func AddItemOutsidePopup(tests, row, col)
2613 eval a:tests->add(#{clickrow: a:row, clickcol: a:col, result: #{
2614 \ screenrow: a:row, screencol: a:col,
2615 \ winid: win_getid(), winrow: a:row, wincol: a:col,
2616 \ line: a:row, column: a:col,
2617 \ }})
2618 endfunc
2619 func AddItemInPopupBorder(tests, winid, row, col)
2620 eval a:tests->add(#{clickrow: a:row, clickcol: a:col, result: #{
2621 \ screenrow: a:row, screencol: a:col,
2622 \ winid: a:winid, winrow: a:row - 1, wincol: a:col - 3,
2623 \ line: 0, column: 0,
2624 \ }})
2625 endfunc
2626 func AddItemInPopupText(tests, winid, row, col, textline, textcol)
2627 eval a:tests->add(#{clickrow: a:row, clickcol: a:col, result: #{
2628 \ screenrow: a:row, screencol: a:col,
2629 \ winid: a:winid, winrow: a:row - 1, wincol: a:col - 3,
2630 \ line: a:textline, column: a:textcol,
2631 \ }})
2632 endfunc
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002633
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002634 " above and below popup
2635 for c in range(1, 21)
2636 call AddItemOutsidePopup(tests, 1, c)
2637 call AddItemOutsidePopup(tests, 10, c)
2638 endfor
2639 " left and right of popup
2640 for r in range(1, 10)
2641 call AddItemOutsidePopup(tests, r, 3)
2642 call AddItemOutsidePopup(tests, r, 20)
2643 endfor
2644 " top and bottom in popup
2645 for c in range(4, 19)
2646 call AddItemInPopupBorder(tests, winid, 2, c)
2647 call AddItemInPopupBorder(tests, winid, 3, c)
2648 call AddItemInPopupBorder(tests, winid, 8, c)
2649 call AddItemInPopupBorder(tests, winid, 9, c)
2650 endfor
2651 " left and right margin in popup
2652 for r in range(2, 9)
2653 call AddItemInPopupBorder(tests, winid, r, 4)
2654 call AddItemInPopupBorder(tests, winid, r, 5)
2655 call AddItemInPopupBorder(tests, winid, r, 18)
2656 call AddItemInPopupBorder(tests, winid, r, 19)
2657 endfor
2658 " text "short"
2659 call AddItemInPopupText(tests, winid, 4, 6, 1, 1)
2660 call AddItemInPopupText(tests, winid, 4, 10, 1, 5)
2661 call AddItemInPopupText(tests, winid, 4, 11, 1, 6)
2662 call AddItemInPopupText(tests, winid, 4, 17, 1, 6)
2663 " text "long line th"
2664 call AddItemInPopupText(tests, winid, 5, 6, 2, 1)
2665 call AddItemInPopupText(tests, winid, 5, 10, 2, 5)
2666 call AddItemInPopupText(tests, winid, 5, 17, 2, 12)
2667 " text "at will wrap"
2668 call AddItemInPopupText(tests, winid, 6, 6, 2, 13)
2669 call AddItemInPopupText(tests, winid, 6, 10, 2, 17)
2670 call AddItemInPopupText(tests, winid, 6, 17, 2, 24)
2671 " text "other"
2672 call AddItemInPopupText(tests, winid, 7, 6, 3, 1)
2673 call AddItemInPopupText(tests, winid, 7, 10, 3, 5)
2674 call AddItemInPopupText(tests, winid, 7, 11, 3, 6)
2675 call AddItemInPopupText(tests, winid, 7, 17, 3, 6)
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002676
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002677 for item in tests
2678 call test_setmouse(item.clickrow, item.clickcol)
2679 call feedkeys("\<LeftMouse>", 'xt')
2680 call assert_equal(item.result, g:got_mousepos)
2681 endfor
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002682
2683 call popup_close(winid)
Bram Moolenaardb3a2052019-11-16 18:22:41 +01002684 enew!
Bram Moolenaarf8b036b2019-11-06 21:09:17 +01002685 delfunc MyPopupFilter
2686endfunc
2687
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002688func Test_popupwin_with_buffer()
2689 call writefile(['some text', 'in a buffer'], 'XsomeFile')
2690 let buf = bufadd('XsomeFile')
2691 call assert_equal(0, bufloaded(buf))
Bram Moolenaar46451042019-08-24 15:50:46 +02002692
2693 setlocal number
2694 call setbufvar(buf, "&wrapmargin", 13)
2695
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002696 let winid = popup_create(buf, {})
2697 call assert_notequal(0, winid)
2698 let pos = popup_getpos(winid)
2699 call assert_equal(2, pos.height)
2700 call assert_equal(1, bufloaded(buf))
Bram Moolenaar46451042019-08-24 15:50:46 +02002701
2702 " window-local option is set to default, buffer-local is not
2703 call assert_equal(0, getwinvar(winid, '&number'))
2704 call assert_equal(13, getbufvar(buf, '&wrapmargin'))
2705
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002706 call popup_close(winid)
2707 call assert_equal({}, popup_getpos(winid))
2708 call assert_equal(1, bufloaded(buf))
2709 exe 'bwipe! ' .. buf
Bram Moolenaar46451042019-08-24 15:50:46 +02002710 setlocal nonumber
Bram Moolenaar7866b872019-07-01 22:21:01 +02002711
2712 edit test_popupwin.vim
2713 let winid = popup_create(bufnr(''), {})
2714 redraw
2715 call popup_close(winid)
Bram Moolenaar3940ec62019-07-05 21:53:24 +02002716 call delete('XsomeFile')
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02002717endfunc
Bram Moolenaare296e312019-07-03 23:20:18 +02002718
Bram Moolenaare0d749a2019-09-25 22:14:48 +02002719func Test_popupwin_terminal_buffer()
Bram Moolenaard2c1fb42019-09-25 23:06:40 +02002720 CheckFeature terminal
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002721 CheckUnix
Bram Moolenaare06a28f2020-05-13 23:24:12 +02002722 " Starting a terminal to run a shell in is considered flaky.
2723 let g:test_is_flaky = 1
Bram Moolenaard2c1fb42019-09-25 23:06:40 +02002724
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002725 let origwin = win_getid()
Bram Moolenaar349f6092020-10-06 20:46:49 +02002726
2727 " open help window to test that :help below fails
2728 help
2729
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002730 let termbuf = term_start(&shell, #{hidden: 1})
Bram Moolenaarf5452692020-11-28 21:56:06 +01002731 let winid = popup_create(termbuf, #{minwidth: 40, minheight: 10, border: []})
Bram Moolenaar0353f562020-12-17 22:27:38 +01002732 " Wait for shell to start
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002733 call WaitForAssert({-> assert_equal("run", job_status(term_getjob(termbuf)))})
Bram Moolenaar0353f562020-12-17 22:27:38 +01002734 " Wait for a prompt (see border char first, then space after prompt)
2735 call WaitForAssert({ -> assert_equal(' ', screenstring(screenrow(), screencol() - 1))})
Bram Moolenaarf5452692020-11-28 21:56:06 +01002736
2737 " When typing a character, the cursor is after it.
2738 call feedkeys("x", 'xt')
Bram Moolenaar0353f562020-12-17 22:27:38 +01002739 call term_wait(termbuf)
Bram Moolenaarf5452692020-11-28 21:56:06 +01002740 redraw
2741 call WaitForAssert({ -> assert_equal('x', screenstring(screenrow(), screencol() - 1))})
2742 call feedkeys("\<BS>", 'xt')
2743
Bram Moolenaar631ebc42020-02-03 22:15:26 +01002744 " Check this doesn't crash
2745 call assert_equal(winnr(), winnr('j'))
2746 call assert_equal(winnr(), winnr('k'))
2747 call assert_equal(winnr(), winnr('h'))
2748 call assert_equal(winnr(), winnr('l'))
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02002749
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002750 " Cannot quit while job is running
2751 call assert_fails('call feedkeys("\<C-W>:quit\<CR>", "xt")', 'E948:')
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02002752
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002753 " Cannot enter Terminal-Normal mode. (TODO: but it works...)
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02002754 call feedkeys("xxx\<C-W>N", 'xt')
2755 call assert_fails('call feedkeys("gf", "xt")', 'E863:')
2756 call feedkeys("a\<C-U>", 'xt')
2757
Bram Moolenaar3f65c662020-05-27 23:15:16 +02002758 " Cannot escape from terminal window
2759 call assert_fails('tab drop xxx', 'E863:')
Bram Moolenaar349f6092020-10-06 20:46:49 +02002760 call assert_fails('help', 'E994:')
Bram Moolenaar3f65c662020-05-27 23:15:16 +02002761
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002762 " Cannot open a second one.
2763 let termbuf2 = term_start(&shell, #{hidden: 1})
2764 call assert_fails('call popup_create(termbuf2, #{})', 'E861:')
2765 call term_sendkeys(termbuf2, "exit\<CR>")
2766
Bram Moolenaar5aed0cc2020-05-12 22:02:21 +02002767 " Exiting shell closes popup window
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002768 call feedkeys("exit\<CR>", 'xt')
2769 " Wait for shell to exit
Bram Moolenaarb2b218d2020-06-22 20:22:19 +02002770 call WaitForAssert({-> assert_equal("dead", job_status(term_getjob(termbuf)))})
Bram Moolenaar373c6512020-06-22 19:24:23 +02002771
Bram Moolenaar349f6092020-10-06 20:46:49 +02002772 helpclose
Bram Moolenaard98c0b62020-02-02 15:25:16 +01002773 call feedkeys(":quit\<CR>", 'xt')
2774 call assert_equal(origwin, win_getid())
Bram Moolenaare0d749a2019-09-25 22:14:48 +02002775endfunc
2776
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002777func Test_popupwin_close_prevwin()
2778 CheckFeature terminal
Bram Moolenaar62f93f42020-09-02 22:33:24 +02002779 call Popupwin_close_prevwin()
2780endfunc
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002781
Bram Moolenaar62f93f42020-09-02 22:33:24 +02002782def Popupwin_close_prevwin()
2783 assert_equal(1, winnr('$'))
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002784 split
2785 wincmd b
Bram Moolenaar62f93f42020-09-02 22:33:24 +02002786 assert_equal(2, winnr())
Bram Moolenaare0de1712020-12-02 17:36:54 +01002787 var buf = term_start(&shell, {hidden: 1})
Bram Moolenaar62f93f42020-09-02 22:33:24 +02002788 popup_create(buf, {})
2789 TermWait(buf, 100)
2790 popup_clear(true)
2791 assert_equal(2, winnr())
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002792
2793 quit
2794 exe 'bwipe! ' .. buf
Bram Moolenaar62f93f42020-09-02 22:33:24 +02002795enddef
Bram Moolenaarab176ce2020-06-15 21:19:08 +02002796
Bram Moolenaar934470e2019-09-01 23:27:05 +02002797func Test_popupwin_with_buffer_and_filter()
2798 new Xwithfilter
2799 call setline(1, range(100))
2800 let bufnr = bufnr()
2801 hide
2802
2803 func BufferFilter(win, key)
2804 if a:key == 'G'
2805 " recursive use of "G" does not cause problems.
2806 call win_execute(a:win, 'normal! G')
2807 return 1
2808 endif
2809 return 0
2810 endfunc
2811
2812 let winid = popup_create(bufnr, #{maxheight: 5, filter: 'BufferFilter'})
2813 call assert_equal(1, popup_getpos(winid).firstline)
2814 redraw
2815 call feedkeys("G", 'xt')
2816 call assert_equal(99, popup_getpos(winid).firstline)
2817
2818 call popup_close(winid)
2819 exe 'bwipe! ' .. bufnr
2820endfunc
2821
Bram Moolenaare296e312019-07-03 23:20:18 +02002822func Test_popupwin_width()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002823 let winid = popup_create(repeat(['short', 'long long long line', 'medium width'], 50), #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002824 \ maxwidth: 40,
2825 \ maxheight: 10,
Bram Moolenaare296e312019-07-03 23:20:18 +02002826 \ })
2827 for top in range(1, 20)
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002828 eval winid->popup_setoptions(#{firstline: top})
Bram Moolenaare296e312019-07-03 23:20:18 +02002829 redraw
2830 call assert_equal(19, popup_getpos(winid).width)
2831 endfor
2832 call popup_clear()
2833endfunc
Bram Moolenaar5ca1ac32019-07-04 15:39:28 +02002834
2835func Test_popupwin_buf_close()
2836 let buf = bufadd('Xtestbuf')
2837 call bufload(buf)
2838 call setbufline(buf, 1, ['just', 'some', 'lines'])
2839 let winid = popup_create(buf, {})
2840 redraw
2841 call assert_equal(3, popup_getpos(winid).height)
2842 let bufinfo = getbufinfo(buf)[0]
2843 call assert_equal(1, bufinfo.changed)
2844 call assert_equal(0, bufinfo.hidden)
2845 call assert_equal(0, bufinfo.listed)
2846 call assert_equal(1, bufinfo.loaded)
2847 call assert_equal([], bufinfo.windows)
2848 call assert_equal([winid], bufinfo.popups)
2849
2850 call popup_close(winid)
2851 call assert_equal({}, popup_getpos(winid))
2852 let bufinfo = getbufinfo(buf)[0]
2853 call assert_equal(1, bufinfo.changed)
2854 call assert_equal(1, bufinfo.hidden)
2855 call assert_equal(0, bufinfo.listed)
2856 call assert_equal(1, bufinfo.loaded)
2857 call assert_equal([], bufinfo.windows)
2858 call assert_equal([], bufinfo.popups)
2859 exe 'bwipe! ' .. buf
2860endfunc
Bram Moolenaar017c2692019-07-13 14:17:51 +02002861
2862func Test_popup_menu_with_maxwidth()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002863 CheckScreendump
Bram Moolenaar017c2692019-07-13 14:17:51 +02002864
2865 let lines =<< trim END
2866 call setline(1, range(1, 10))
2867 hi ScrollThumb ctermbg=blue
2868 hi ScrollBar ctermbg=red
2869 func PopupMenu(lines, line, col, scrollbar = 0)
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002870 return popup_menu(a:lines, #{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002871 \ maxwidth: 10,
2872 \ maxheight: 3,
2873 \ pos : 'topleft',
2874 \ col : a:col,
2875 \ line : a:line,
2876 \ scrollbar : a:scrollbar,
Bram Moolenaar017c2692019-07-13 14:17:51 +02002877 \ })
2878 endfunc
2879 call PopupMenu(['x'], 1, 1)
2880 call PopupMenu(['123456789|'], 1, 16)
2881 call PopupMenu(['123456789|' .. ' '], 7, 1)
2882 call PopupMenu([repeat('123456789|', 100)], 7, 16)
2883 call PopupMenu(repeat(['123456789|' .. ' '], 5), 1, 33, 1)
2884 END
2885 call writefile(lines, 'XtestPopupMenuMaxWidth')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002886 let buf = RunVimInTerminal('-S XtestPopupMenuMaxWidth', #{rows: 13})
Bram Moolenaar017c2692019-07-13 14:17:51 +02002887 call VerifyScreenDump(buf, 'Test_popupwin_menu_maxwidth_1', {})
2888
2889 " close the menu popupwin.
2890 call term_sendkeys(buf, " ")
2891 call term_sendkeys(buf, " ")
2892 call term_sendkeys(buf, " ")
2893 call term_sendkeys(buf, " ")
2894 call term_sendkeys(buf, " ")
2895
2896 " clean up
2897 call StopVimInTerminal(buf)
2898 call delete('XtestPopupMenuMaxWidth')
2899endfunc
2900
Bram Moolenaara901a372019-07-13 16:38:50 +02002901func Test_popup_menu_with_scrollbar()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002902 CheckScreendump
Bram Moolenaara901a372019-07-13 16:38:50 +02002903
2904 let lines =<< trim END
2905 call setline(1, range(1, 20))
2906 hi ScrollThumb ctermbg=blue
2907 hi ScrollBar ctermbg=red
Bram Moolenaar6a124e62019-09-04 18:15:19 +02002908 eval ['one', 'two', 'three', 'four', 'five',
2909 \ 'six', 'seven', 'eight', 'nine']
2910 \ ->popup_menu(#{
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002911 \ minwidth: 8,
2912 \ maxheight: 3,
Bram Moolenaara901a372019-07-13 16:38:50 +02002913 \ })
2914 END
2915 call writefile(lines, 'XtestPopupMenuScroll')
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002916 let buf = RunVimInTerminal('-S XtestPopupMenuScroll', #{rows: 10})
Bram Moolenaara901a372019-07-13 16:38:50 +02002917
2918 call term_sendkeys(buf, "j")
2919 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_1', {})
2920
2921 call term_sendkeys(buf, "jjj")
2922 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_2', {})
2923
2924 " if the cursor is the bottom line, it stays at the bottom line.
2925 call term_sendkeys(buf, repeat("j", 20))
2926 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_3', {})
2927
2928 call term_sendkeys(buf, "kk")
2929 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_4', {})
2930
2931 call term_sendkeys(buf, "k")
2932 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_5', {})
2933
2934 " if the cursor is in the top line, it stays in the top line.
2935 call term_sendkeys(buf, repeat("k", 20))
2936 call VerifyScreenDump(buf, 'Test_popupwin_menu_scroll_6', {})
2937
2938 " close the menu popupwin.
2939 call term_sendkeys(buf, " ")
2940
2941 " clean up
2942 call StopVimInTerminal(buf)
2943 call delete('XtestPopupMenuScroll')
2944endfunc
2945
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002946func Test_popup_menu_filter()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02002947 CheckScreendump
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002948
2949 let lines =<< trim END
2950 function! MyFilter(winid, key) abort
2951 if a:key == "0"
2952 call win_execute(a:winid, "call setpos('.', [0, 1, 1, 0])")
2953 return 1
2954 endif
2955 if a:key == "G"
2956 call win_execute(a:winid, "call setpos('.', [0, line('$'), 1, 0])")
2957 return 1
2958 endif
2959 if a:key == "j"
2960 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0])")
2961 return 1
2962 endif
2963 if a:key == "k"
2964 call win_execute(a:winid, "call setpos('.', [0, line('.') - 1, 1, 0])")
2965 return 1
2966 endif
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02002967 if a:key == ':'
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002968 call popup_close(a:winid)
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02002969 return 0
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002970 endif
2971 return 0
2972 endfunction
2973 call popup_menu(['111', '222', '333', '444', '555', '666', '777', '888', '999'], #{
2974 \ maxheight : 3,
2975 \ filter : 'MyFilter'
2976 \ })
2977 END
2978 call writefile(lines, 'XtestPopupMenuFilter')
2979 let buf = RunVimInTerminal('-S XtestPopupMenuFilter', #{rows: 10})
2980
2981 call term_sendkeys(buf, "j")
2982 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_1', {})
2983
2984 call term_sendkeys(buf, "k")
2985 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_2', {})
2986
2987 call term_sendkeys(buf, "G")
2988 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_3', {})
2989
2990 call term_sendkeys(buf, "0")
2991 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_4', {})
2992
Bram Moolenaarbcb4c8f2019-09-07 14:06:52 +02002993 " check that when the popup is closed in the filter the screen is redrawn
2994 call term_sendkeys(buf, ":")
2995 call VerifyScreenDump(buf, 'Test_popupwin_menu_filter_5', {})
2996 call term_sendkeys(buf, "\<CR>")
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02002997
2998 " clean up
2999 call StopVimInTerminal(buf)
3000 call delete('XtestPopupMenuFilter')
3001endfunc
3002
3003func Test_popup_cursorline()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02003004 CheckScreendump
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003005
3006 let winid = popup_create('some text', {})
3007 call assert_equal(0, popup_getoptions(winid).cursorline)
3008 call popup_close(winid)
3009
3010 let winid = popup_create('some text', #{ cursorline: 1, })
3011 call assert_equal(1, popup_getoptions(winid).cursorline)
3012 call popup_close(winid)
3013
Bram Moolenaar6bfc4752021-02-21 23:12:18 +01003014 let winid = popup_create('some text', #{ cursorline: v:true, })
3015 call assert_equal(1, popup_getoptions(winid).cursorline)
3016 call popup_close(winid)
3017
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003018 let winid = popup_create('some text', #{ cursorline: 0, })
3019 call assert_equal(0, popup_getoptions(winid).cursorline)
3020 call popup_close(winid)
3021
3022 let winid = popup_menu('some text', {})
3023 call assert_equal(1, popup_getoptions(winid).cursorline)
3024 call popup_close(winid)
3025
3026 let winid = popup_menu('some text', #{ cursorline: 1, })
3027 call assert_equal(1, popup_getoptions(winid).cursorline)
3028 call popup_close(winid)
3029
3030 let winid = popup_menu('some text', #{ cursorline: 0, })
3031 call assert_equal(0, popup_getoptions(winid).cursorline)
3032 call popup_close(winid)
3033
3034 " ---------
3035 " Pattern 1
3036 " ---------
3037 let lines =<< trim END
3038 call popup_create(['111', '222', '333'], #{ cursorline : 0 })
3039 END
3040 call writefile(lines, 'XtestPopupCursorLine')
3041 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
3042 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_1', {})
3043 call term_sendkeys(buf, ":call popup_clear()\<cr>")
3044 call StopVimInTerminal(buf)
3045
3046 " ---------
3047 " Pattern 2
3048 " ---------
3049 let lines =<< trim END
3050 call popup_create(['111', '222', '333'], #{ cursorline : 1 })
3051 END
3052 call writefile(lines, 'XtestPopupCursorLine')
3053 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
3054 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_2', {})
3055 call term_sendkeys(buf, ":call popup_clear()\<cr>")
3056 call StopVimInTerminal(buf)
3057
3058 " ---------
3059 " Pattern 3
3060 " ---------
3061 let lines =<< trim END
3062 function! MyFilter(winid, key) abort
3063 if a:key == "j"
3064 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0]) | redraw")
3065 return 1
3066 endif
3067 if a:key == 'x'
3068 call popup_close(a:winid)
3069 return 1
3070 endif
3071 return 0
3072 endfunction
3073 call popup_menu(['111', '222', '333'], #{
3074 \ cursorline : 0,
3075 \ maxheight : 2,
3076 \ filter : 'MyFilter',
3077 \ })
3078 END
3079 call writefile(lines, 'XtestPopupCursorLine')
3080 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
3081 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_3', {})
3082 call term_sendkeys(buf, "j")
3083 call term_sendkeys(buf, "j")
3084 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_4', {})
3085 call term_sendkeys(buf, "x")
3086 call StopVimInTerminal(buf)
3087
3088 " ---------
3089 " Pattern 4
3090 " ---------
3091 let lines =<< trim END
3092 function! MyFilter(winid, key) abort
3093 if a:key == "j"
3094 call win_execute(a:winid, "call setpos('.', [0, line('.') + 1, 1, 0]) | redraw")
3095 return 1
3096 endif
3097 if a:key == 'x'
3098 call popup_close(a:winid)
3099 return 1
3100 endif
3101 return 0
3102 endfunction
3103 call popup_menu(['111', '222', '333'], #{
3104 \ cursorline : 1,
3105 \ maxheight : 2,
3106 \ filter : 'MyFilter',
3107 \ })
3108 END
3109 call writefile(lines, 'XtestPopupCursorLine')
3110 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
3111 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_5', {})
3112 call term_sendkeys(buf, "j")
3113 call term_sendkeys(buf, "j")
3114 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_6', {})
3115 call term_sendkeys(buf, "x")
3116 call StopVimInTerminal(buf)
3117
Bram Moolenaar3d2a3cb2019-09-08 17:12:01 +02003118 " ---------
3119 " Cursor in second line when creating the popup
3120 " ---------
3121 let lines =<< trim END
3122 let winid = popup_create(['111', '222', '333'], #{
3123 \ cursorline : 1,
3124 \ })
3125 call win_execute(winid, "2")
3126 END
3127 call writefile(lines, 'XtestPopupCursorLine')
3128 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
3129 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_7', {})
3130 call StopVimInTerminal(buf)
3131
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003132 call delete('XtestPopupCursorLine')
Bram Moolenaar4eb7dae2019-11-12 22:33:45 +01003133
3134 " ---------
3135 " Use current buffer for popupmenu
3136 " ---------
3137 let lines =<< trim END
3138 call setline(1, ['one', 'two', 'three'])
3139 let winid = popup_create(bufnr('%'), #{
3140 \ cursorline : 1,
3141 \ })
3142 call win_execute(winid, "2")
3143 END
3144 call writefile(lines, 'XtestPopupCursorLine')
3145 let buf = RunVimInTerminal('-S XtestPopupCursorLine', #{rows: 10})
3146 call VerifyScreenDump(buf, 'Test_popupwin_cursorline_8', {})
3147 call StopVimInTerminal(buf)
3148
3149 call delete('XtestPopupCursorLine')
Bram Moolenaardf9c6ca2019-07-18 13:46:42 +02003150endfunc
3151
Bram Moolenaar6bfc4752021-02-21 23:12:18 +01003152def Test_popup_cursorline_vim9()
3153 var winid = popup_create('some text', { cursorline: true, })
3154 assert_equal(1, popup_getoptions(winid).cursorline)
3155 popup_close(winid)
3156
3157 assert_fails("popup_create('some text', { cursorline: 2, })", 'E1023:')
3158 popup_clear()
3159enddef
3160
Bram Moolenaarf914a332019-07-20 15:09:56 +02003161func Test_previewpopup()
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02003162 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003163 CheckFeature quickfix
Bram Moolenaar4999a7f2019-08-10 22:21:48 +02003164
Bram Moolenaarf914a332019-07-20 15:09:56 +02003165 call writefile([
3166 \ "!_TAG_FILE_ENCODING\tutf-8\t//",
3167 \ "another\tXtagfile\t/^this is another",
3168 \ "theword\tXtagfile\t/^theword"],
3169 \ 'Xtags')
3170 call writefile(range(1,20)
3171 \ + ['theword is here']
3172 \ + range(22, 27)
3173 \ + ['this is another place']
3174 \ + range(29, 40),
3175 \ "Xtagfile")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02003176 call writefile(range(1,10)
3177 \ + ['searched word is here']
3178 \ + range(12, 20),
3179 \ "Xheader.h")
Bram Moolenaarf914a332019-07-20 15:09:56 +02003180 let lines =<< trim END
3181 set tags=Xtags
3182 call setline(1, [
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02003183 \ 'one',
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02003184 \ '#include "Xheader.h"',
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02003185 \ 'three',
3186 \ 'four',
3187 \ 'five',
3188 \ 'six',
3189 \ 'seven',
3190 \ 'find theword somewhere',
3191 \ 'nine',
3192 \ 'this is another word',
3193 \ 'very long line where the word is also another'])
Bram Moolenaarf914a332019-07-20 15:09:56 +02003194 set previewpopup=height:4,width:40
Bram Moolenaar60577482021-03-04 21:35:07 +01003195 hi OtherColor ctermbg=lightcyan guibg=lightcyan
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02003196 set path=.
Bram Moolenaarf914a332019-07-20 15:09:56 +02003197 END
3198 call writefile(lines, 'XtestPreviewPopup')
3199 let buf = RunVimInTerminal('-S XtestPreviewPopup', #{rows: 14})
3200
3201 call term_sendkeys(buf, "/theword\<CR>\<C-W>}")
3202 call term_sendkeys(buf, ":\<CR>")
3203 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_1', {})
3204
Bram Moolenaar60577482021-03-04 21:35:07 +01003205 call term_sendkeys(buf, ":set previewpopup+=highlight:OtherColor\<CR>")
Bram Moolenaarf914a332019-07-20 15:09:56 +02003206 call term_sendkeys(buf, "/another\<CR>\<C-W>}")
3207 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_2', {})
3208
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003209 call term_sendkeys(buf, ":call popup_move(popup_findpreview(), #{col: 15})\<CR>")
Bram Moolenaar13d5c3f2019-07-28 21:42:38 +02003210 call term_sendkeys(buf, ":\<CR>")
3211 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_3', {})
3212
3213 call term_sendkeys(buf, "/another\<CR>\<C-W>}")
3214 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_4', {})
3215
Bram Moolenaar799439a2020-02-11 21:44:17 +01003216 call term_sendkeys(buf, ":silent cd ..\<CR>:\<CR>")
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02003217 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_5', {})
Bram Moolenaar799439a2020-02-11 21:44:17 +01003218 call term_sendkeys(buf, ":silent cd testdir\<CR>")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02003219
Bram Moolenaar60577482021-03-04 21:35:07 +01003220 call term_sendkeys(buf, ":set previewpopup-=highlight:OtherColor\<CR>")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02003221 call term_sendkeys(buf, ":pclose\<CR>")
Bram Moolenaar78d629a2019-08-16 17:31:15 +02003222 call term_sendkeys(buf, ":\<BS>")
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02003223 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_6', {})
3224
3225 call term_sendkeys(buf, ":pedit +/theword Xtagfile\<CR>")
3226 call term_sendkeys(buf, ":\<CR>")
3227 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_7', {})
3228
3229 call term_sendkeys(buf, ":pclose\<CR>")
3230 call term_sendkeys(buf, ":psearch searched\<CR>")
3231 call term_sendkeys(buf, ":\<CR>")
3232 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_8', {})
Bram Moolenaar749fa0a2019-08-03 16:18:07 +02003233
Bram Moolenaar8bf716c2020-01-23 15:33:54 +01003234 call term_sendkeys(buf, "\<C-W>p")
3235 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_9', {})
3236
3237 call term_sendkeys(buf, ":call win_execute(popup_findpreview(), 'call popup_clear()')\<CR>")
3238 call VerifyScreenDump(buf, 'Test_popupwin_previewpopup_10', {})
3239
Bram Moolenaarf914a332019-07-20 15:09:56 +02003240 call StopVimInTerminal(buf)
3241 call delete('Xtags')
3242 call delete('Xtagfile')
3243 call delete('XtestPreviewPopup')
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02003244 call delete('Xheader.h')
Bram Moolenaarf914a332019-07-20 15:09:56 +02003245endfunc
3246
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003247func Get_popupmenu_lines()
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003248 let lines =<< trim END
3249 set completeopt+=preview,popup
3250 set completefunc=CompleteFuncDict
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02003251 hi InfoPopup ctermbg=yellow
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003252
3253 func CompleteFuncDict(findstart, base)
3254 if a:findstart
3255 if col('.') > 10
3256 return col('.') - 10
3257 endif
3258 return 0
3259 endif
3260
3261 return {
3262 \ 'words': [
3263 \ {
3264 \ 'word': 'aword',
3265 \ 'abbr': 'wrd',
3266 \ 'menu': 'extra text',
3267 \ 'info': 'words are cool',
3268 \ 'kind': 'W',
3269 \ 'user_data': 'test'
3270 \ },
3271 \ {
3272 \ 'word': 'anotherword',
3273 \ 'abbr': 'anotwrd',
3274 \ 'menu': 'extra text',
3275 \ 'info': "other words are\ncooler than this and some more text\nto make wrap",
3276 \ 'kind': 'W',
3277 \ 'user_data': 'notest'
3278 \ },
3279 \ {
3280 \ 'word': 'noinfo',
3281 \ 'abbr': 'noawrd',
3282 \ 'menu': 'extra text',
Bram Moolenaar62a0cb42019-08-18 16:35:23 +02003283 \ 'info': "lets\nshow\na\nscrollbar\nhere",
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003284 \ 'kind': 'W',
3285 \ 'user_data': 'notest'
3286 \ },
3287 \ {
3288 \ 'word': 'thatword',
3289 \ 'abbr': 'thatwrd',
3290 \ 'menu': 'extra text',
3291 \ 'info': 'that word is cool',
3292 \ 'kind': 'W',
3293 \ 'user_data': 'notest'
3294 \ },
3295 \ ]
3296 \ }
3297 endfunc
3298 call setline(1, 'text text text text text text text ')
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003299 func ChangeColor()
3300 let id = popup_findinfo()
Bram Moolenaard356fc62020-12-09 18:13:44 +01003301 if buflisted(winbufnr(id))
3302 call setline(1, 'buffer is listed')
3303 endif
Bram Moolenaar6a124e62019-09-04 18:15:19 +02003304 eval id->popup_setoptions(#{highlight: 'InfoPopup'})
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003305 endfunc
Bram Moolenaardca7abe2019-10-20 18:17:57 +02003306
3307 func InfoHidden()
3308 set completepopup=height:4,border:off,align:menu
3309 set completeopt-=popup completeopt+=popuphidden
3310 au CompleteChanged * call HandleChange()
3311 endfunc
3312
3313 let s:counter = 0
3314 func HandleChange()
3315 let s:counter += 1
3316 let selected = complete_info(['selected']).selected
3317 if selected <= 0
3318 " First time: do nothing, info remains hidden
3319 return
3320 endif
3321 if selected == 1
3322 " Second time: show info right away
3323 let id = popup_findinfo()
3324 if id
3325 call popup_settext(id, 'immediate info ' .. s:counter)
3326 call popup_show(id)
3327 endif
3328 else
3329 " Third time: show info after a short delay
3330 call timer_start(100, 'ShowInfo')
3331 endif
3332 endfunc
3333
3334 func ShowInfo(...)
3335 let id = popup_findinfo()
3336 if id
3337 call popup_settext(id, 'async info ' .. s:counter)
3338 call popup_show(id)
3339 endif
3340 endfunc
Bram Moolenaar2dfae042020-11-15 14:09:37 +01003341
Bakudankun65555002021-11-17 20:40:16 +00003342 func OpenOtherPopups()
3343 call popup_create([
3344 \ 'popup below',
3345 \ 'popup below',
3346 \ 'popup below',
3347 \ 'popup below',
3348 \ ], #{
3349 \ line: 'cursor',
3350 \ col: 'cursor+3',
3351 \ highlight: 'ErrorMsg',
3352 \ minwidth: 17,
3353 \ zindex: 50,
3354 \ })
3355 call popup_create([
3356 \ 'popup on top',
3357 \ 'popup on top',
3358 \ 'popup on top',
3359 \ ], #{
3360 \ line: 'cursor+3',
3361 \ col: 'cursor-10',
3362 \ highlight: 'Search',
3363 \ minwidth: 10,
3364 \ zindex: 200,
3365 \ })
3366 endfunc
3367
Bram Moolenaar2dfae042020-11-15 14:09:37 +01003368 " Check that no autocommands are triggered for the info popup
3369 au WinEnter * if win_gettype() == 'popup' | call setline(2, 'WinEnter') | endif
3370 au WinLeave * if win_gettype() == 'popup' | call setline(2, 'WinLeave') | endif
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003371 END
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003372 return lines
3373endfunc
3374
3375func Test_popupmenu_info_border()
3376 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003377 CheckFeature quickfix
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003378
3379 let lines = Get_popupmenu_lines()
3380 call add(lines, 'set completepopup=height:4,highlight:InfoPopup')
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003381 call writefile(lines, 'XtestInfoPopup')
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003382
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003383 let buf = RunVimInTerminal('-S XtestInfoPopup', #{rows: 14})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003384 call TermWait(buf, 25)
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003385
3386 call term_sendkeys(buf, "A\<C-X>\<C-U>")
3387 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_1', {})
3388
3389 call term_sendkeys(buf, "\<C-N>")
3390 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_2', {})
3391
3392 call term_sendkeys(buf, "\<C-N>")
3393 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_3', {})
3394
3395 call term_sendkeys(buf, "\<C-N>\<C-N>")
3396 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_4', {})
3397
Bram Moolenaarfe6e7612019-08-21 20:57:20 +02003398 " info on the left with scrollbar
3399 call term_sendkeys(buf, "test text test text\<C-X>\<C-U>")
3400 call term_sendkeys(buf, "\<C-N>\<C-N>")
3401 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_5', {})
3402
Bram Moolenaar202c3f72019-11-21 12:12:35 +01003403 " Test that the popupmenu's scrollbar and infopopup do not overlap
3404 call term_sendkeys(buf, "\<Esc>")
3405 call term_sendkeys(buf, ":set pumheight=3\<CR>")
3406 call term_sendkeys(buf, "cc\<C-X>\<C-U>")
3407 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_6', {})
3408
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003409 " Hide the info popup, cycle through buffers, make sure it didn't get
Bram Moolenaarca7c0782020-01-14 20:42:48 +01003410 " deleted.
3411 call term_sendkeys(buf, "\<Esc>")
3412 call term_sendkeys(buf, ":set hidden\<CR>")
3413 call term_sendkeys(buf, ":bn\<CR>")
3414 call term_sendkeys(buf, ":bn\<CR>")
3415 call term_sendkeys(buf, "otest text test text\<C-X>\<C-U>")
3416 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_7', {})
3417
Bram Moolenaar447bfba2020-07-18 16:07:16 +02003418 " Test that when the option is changed the popup changes.
3419 call term_sendkeys(buf, "\<Esc>")
3420 call term_sendkeys(buf, ":set completepopup=border:off\<CR>")
3421 call term_sendkeys(buf, "a\<C-X>\<C-U>")
3422 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_8', {})
3423
Bram Moolenaar6d585f42020-07-26 22:20:54 +02003424 call term_sendkeys(buf, " \<Esc>")
3425 call term_sendkeys(buf, ":set completepopup+=width:10\<CR>")
3426 call term_sendkeys(buf, "a\<C-X>\<C-U>")
3427 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_9', {})
3428
Bram Moolenaarca7c0782020-01-14 20:42:48 +01003429 call term_sendkeys(buf, "\<Esc>")
Bram Moolenaar576a4a62019-08-18 15:25:17 +02003430 call StopVimInTerminal(buf)
3431 call delete('XtestInfoPopup')
3432endfunc
3433
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003434func Test_popupmenu_info_noborder()
3435 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003436 CheckFeature quickfix
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003437
3438 let lines = Get_popupmenu_lines()
3439 call add(lines, 'set completepopup=height:4,border:off')
3440 call writefile(lines, 'XtestInfoPopupNb')
3441
3442 let buf = RunVimInTerminal('-S XtestInfoPopupNb', #{rows: 14})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003443 call TermWait(buf, 25)
Bram Moolenaarbd483b32019-08-21 15:13:41 +02003444
3445 call term_sendkeys(buf, "A\<C-X>\<C-U>")
3446 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_nb_1', {})
3447
3448 call StopVimInTerminal(buf)
3449 call delete('XtestInfoPopupNb')
3450endfunc
3451
Bram Moolenaar258cef52019-08-21 17:29:29 +02003452func Test_popupmenu_info_align_menu()
3453 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003454 CheckFeature quickfix
Bram Moolenaar258cef52019-08-21 17:29:29 +02003455
3456 let lines = Get_popupmenu_lines()
3457 call add(lines, 'set completepopup=height:4,border:off,align:menu')
3458 call writefile(lines, 'XtestInfoPopupNb')
3459
3460 let buf = RunVimInTerminal('-S XtestInfoPopupNb', #{rows: 14})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003461 call TermWait(buf, 25)
Bram Moolenaar258cef52019-08-21 17:29:29 +02003462
3463 call term_sendkeys(buf, "A\<C-X>\<C-U>")
3464 call term_sendkeys(buf, "\<C-N>")
3465 call term_sendkeys(buf, "\<C-N>")
3466 call term_sendkeys(buf, "\<C-N>")
3467 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_1', {})
3468
3469 call term_sendkeys(buf, "test text test text test\<C-X>\<C-U>")
3470 call term_sendkeys(buf, "\<C-N>")
3471 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_2', {})
3472
3473 call term_sendkeys(buf, "\<Esc>")
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02003474 call term_sendkeys(buf, ":call ChangeColor()\<CR>")
Bram Moolenaar258cef52019-08-21 17:29:29 +02003475 call term_sendkeys(buf, ":call setline(2, ['x']->repeat(10))\<CR>")
3476 call term_sendkeys(buf, "Gotest text test text\<C-X>\<C-U>")
3477 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_align_3', {})
3478
3479 call StopVimInTerminal(buf)
3480 call delete('XtestInfoPopupNb')
3481endfunc
3482
Bram Moolenaardca7abe2019-10-20 18:17:57 +02003483func Test_popupmenu_info_hidden()
3484 CheckScreendump
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003485 CheckFeature quickfix
Bram Moolenaardca7abe2019-10-20 18:17:57 +02003486
3487 let lines = Get_popupmenu_lines()
3488 call add(lines, 'call InfoHidden()')
3489 call writefile(lines, 'XtestInfoPopupHidden')
3490
3491 let buf = RunVimInTerminal('-S XtestInfoPopupHidden', #{rows: 14})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003492 call TermWait(buf, 25)
Bram Moolenaardca7abe2019-10-20 18:17:57 +02003493
3494 call term_sendkeys(buf, "A\<C-X>\<C-U>")
3495 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_hidden_1', {})
3496
3497 call term_sendkeys(buf, "\<C-N>")
3498 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_hidden_2', {})
3499
3500 call term_sendkeys(buf, "\<C-N>")
3501 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_hidden_3', {})
3502
3503 call term_sendkeys(buf, "\<Esc>")
3504 call StopVimInTerminal(buf)
3505 call delete('XtestInfoPopupHidden')
3506endfunc
3507
Bram Moolenaarbef93ac2019-12-06 20:17:35 +01003508func Test_popupmenu_info_too_wide()
3509 CheckScreendump
3510 CheckFeature quickfix
3511
3512 let lines =<< trim END
3513 call setline(1, range(10))
3514
3515 set completeopt+=preview,popup
3516 set completepopup=align:menu
3517 set omnifunc=OmniFunc
3518 hi InfoPopup ctermbg=lightgrey
3519
3520 func OmniFunc(findstart, base)
3521 if a:findstart
3522 return 0
3523 endif
3524
3525 let menuText = 'some long text to make sure the menu takes up all of the width of the window'
3526 return #{
3527 \ words: [
3528 \ #{
3529 \ word: 'scrap',
3530 \ menu: menuText,
3531 \ info: "other words are\ncooler than this and some more text\nto make wrap",
3532 \ },
3533 \ #{
3534 \ word: 'scappier',
3535 \ menu: menuText,
3536 \ info: 'words are cool',
3537 \ },
3538 \ #{
3539 \ word: 'scrappier2',
3540 \ menu: menuText,
3541 \ info: 'words are cool',
3542 \ },
3543 \ ]
3544 \ }
3545 endfunc
3546 END
3547
3548 call writefile(lines, 'XtestInfoPopupWide')
3549 let buf = RunVimInTerminal('-S XtestInfoPopupWide', #{rows: 8})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003550 call TermWait(buf, 25)
Bram Moolenaarbef93ac2019-12-06 20:17:35 +01003551
3552 call term_sendkeys(buf, "Ascr\<C-X>\<C-O>")
3553 call VerifyScreenDump(buf, 'Test_popupwin_infopopup_wide_1', {})
3554
3555 call term_sendkeys(buf, "\<Esc>")
3556 call StopVimInTerminal(buf)
3557 call delete('XtestInfoPopupWide')
3558endfunc
3559
Bakudankun65555002021-11-17 20:40:16 +00003560func Test_popupmenu_masking()
3561 " Test that popup windows that are opened while popup menu is open are
3562 " properly displayed.
3563 CheckScreendump
3564 CheckFeature quickfix
3565
3566 let lines = Get_popupmenu_lines()
3567 call add(lines, 'inoremap <C-A> <Cmd>call OpenOtherPopups()<CR>')
3568 call writefile(lines, 'XtestPopupmenuMasking')
3569
3570 let buf = RunVimInTerminal('-S XtestPopupmenuMasking', #{rows: 14})
3571 call TermWait(buf, 25)
3572
3573 call term_sendkeys(buf, "A\<C-X>\<C-U>\<C-A>")
3574 call VerifyScreenDump(buf, 'Test_popupwin_popupmenu_masking_1', {})
3575
3576 call term_sendkeys(buf, "\<Esc>")
3577 call VerifyScreenDump(buf, 'Test_popupwin_popupmenu_masking_2', {})
3578
3579 call StopVimInTerminal(buf)
3580 call delete('XtestPopupmenuMasking')
3581endfunc
3582
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02003583func Test_popupwin_recycle_bnr()
Bram Moolenaare49fbff2019-08-21 22:50:07 +02003584 let bufnr = popup_notification('nothing wrong', {})->winbufnr()
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02003585 call popup_clear()
Bram Moolenaar6a124e62019-09-04 18:15:19 +02003586 let winid = 'nothing wrong'->popup_notification({})
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02003587 call assert_equal(bufnr, winbufnr(winid))
3588 call popup_clear()
3589endfunc
3590
Bram Moolenaar1824f452019-10-02 23:06:46 +02003591func Test_popupwin_getoptions_tablocal()
3592 topleft split
3593 let win1 = popup_create('nothing', #{maxheight: 8})
3594 let win2 = popup_create('something', #{maxheight: 10})
3595 let win3 = popup_create('something', #{maxheight: 15})
3596 call assert_equal(8, popup_getoptions(win1).maxheight)
3597 call assert_equal(10, popup_getoptions(win2).maxheight)
3598 call assert_equal(15, popup_getoptions(win3).maxheight)
3599 call popup_clear()
3600 quit
3601endfunc
3602
Bram Moolenaare8a7dfe2019-10-03 22:35:52 +02003603func Test_popupwin_cancel()
3604 let win1 = popup_create('one', #{line: 5, filter: {... -> 0}})
3605 let win2 = popup_create('two', #{line: 10, filter: {... -> 0}})
3606 let win3 = popup_create('three', #{line: 15, filter: {... -> 0}})
3607 call assert_equal(5, popup_getpos(win1).line)
3608 call assert_equal(10, popup_getpos(win2).line)
3609 call assert_equal(15, popup_getpos(win3).line)
3610 " TODO: this also works without patch 8.1.2110
3611 call feedkeys("\<C-C>", 'xt')
3612 call assert_equal(5, popup_getpos(win1).line)
3613 call assert_equal(10, popup_getpos(win2).line)
3614 call assert_equal({}, popup_getpos(win3))
3615 call feedkeys("\<C-C>", 'xt')
3616 call assert_equal(5, popup_getpos(win1).line)
3617 call assert_equal({}, popup_getpos(win2))
3618 call assert_equal({}, popup_getpos(win3))
3619 call feedkeys("\<C-C>", 'xt')
3620 call assert_equal({}, popup_getpos(win1))
3621 call assert_equal({}, popup_getpos(win2))
3622 call assert_equal({}, popup_getpos(win3))
3623endfunc
3624
Bram Moolenaarafe45b62019-11-13 22:35:19 +01003625func Test_popupwin_filter_redraw()
3626 " Create two popups with a filter that closes the popup when typing "0".
3627 " Both popups should close, even though the redraw also calls
3628 " popup_reset_handled()
3629
3630 func CloseFilter(winid, key)
3631 if a:key == '0'
3632 call popup_close(a:winid)
3633 redraw
3634 endif
3635 return 0 " pass the key
3636 endfunc
3637
3638 let id1 = popup_create('first one', #{
3639 \ line: 1,
3640 \ col: 1,
3641 \ filter: 'CloseFilter',
3642 \ })
3643 let id2 = popup_create('second one', #{
3644 \ line: 9,
3645 \ col: 1,
3646 \ filter: 'CloseFilter',
3647 \ })
3648 call assert_equal(1, popup_getpos(id1).line)
3649 call assert_equal(9, popup_getpos(id2).line)
3650
3651 call feedkeys('0', 'xt')
3652 call assert_equal({}, popup_getpos(id1))
3653 call assert_equal({}, popup_getpos(id2))
3654
3655 call popup_clear()
3656 delfunc CloseFilter
3657endfunc
3658
Bram Moolenaar20ebbea2019-11-30 17:58:27 +01003659func Test_popupwin_double_width()
3660 CheckScreendump
3661
3662 let lines =<< trim END
3663 call setline(1, 'x你好世界你好世你好世界你好')
3664 call setline(2, '你好世界你好世你好世界你好')
3665 call setline(3, 'x你好世界你好世你好世界你好')
3666 call popup_create('你好,世界 - 你好,世界xxxxx', #{line: 1, col: 3, maxwidth: 14})
3667 END
3668 call writefile(lines, 'XtestPopupWide')
3669
3670 let buf = RunVimInTerminal('-S XtestPopupWide', #{rows: 10})
3671 call VerifyScreenDump(buf, 'Test_popupwin_doublewidth_1', {})
3672
3673 call StopVimInTerminal(buf)
3674 call delete('XtestPopupWide')
3675endfunc
3676
3677func Test_popupwin_sign()
3678 CheckScreendump
3679
3680 let lines =<< trim END
3681 call setline(1, range(10))
3682 call sign_define('Current', {
3683 \ 'text': '>>',
3684 \ 'texthl': 'WarningMsg',
3685 \ 'linehl': 'Error',
3686 \ })
3687 call sign_define('Other', {
3688 \ 'text': '#!',
3689 \ 'texthl': 'Error',
3690 \ 'linehl': 'Search',
3691 \ })
3692 let winid = popup_create(['hello', 'bright', 'world'], {
3693 \ 'minwidth': 20,
3694 \ })
3695 call setwinvar(winid, "&signcolumn", "yes")
3696 let winbufnr = winbufnr(winid)
3697
3698 " add sign to current buffer, shows
3699 call sign_place(1, 'Selected', 'Current', bufnr('%'), {'lnum': 1})
3700 " add sign to current buffer, does not show
3701 call sign_place(2, 'PopUpSelected', 'Other', bufnr('%'), {'lnum': 2})
3702
3703 " add sign to popup buffer, shows
3704 call sign_place(3, 'PopUpSelected', 'Other', winbufnr, {'lnum': 1})
3705 " add sign to popup buffer, does not show
3706 call sign_place(4, 'Selected', 'Current', winbufnr, {'lnum': 2})
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02003707
3708 func SetOptions()
3709 call setwinvar(g:winid, '&number', 1)
3710 call setwinvar(g:winid, '&foldcolumn', 2)
3711 call popup_settext(g:winid, 'a longer line to check the width')
3712 endfunc
Bram Moolenaar20ebbea2019-11-30 17:58:27 +01003713 END
3714 call writefile(lines, 'XtestPopupSign')
3715
3716 let buf = RunVimInTerminal('-S XtestPopupSign', #{rows: 10})
3717 call VerifyScreenDump(buf, 'Test_popupwin_sign_1', {})
3718
Bram Moolenaar0aac67a2020-07-27 22:40:37 +02003719 " set more options to check the width is adjusted
3720 call term_sendkeys(buf, ":call SetOptions()\<CR>")
3721 call VerifyScreenDump(buf, 'Test_popupwin_sign_2', {})
3722
Bram Moolenaar20ebbea2019-11-30 17:58:27 +01003723 call StopVimInTerminal(buf)
3724 call delete('XtestPopupSign')
3725endfunc
3726
Bram Moolenaar99ebf222019-12-10 23:44:48 +01003727func Test_popupwin_bufnr()
3728 let popwin = popup_create(['blah'], #{})
3729 let popbuf = winbufnr(popwin)
3730 split asdfasdf
3731 let newbuf = bufnr()
3732 call assert_true(newbuf > popbuf, 'New buffer number is higher')
3733 call assert_equal(newbuf, bufnr('$'))
3734 call popup_clear()
3735 let popwin = popup_create(['blah'], #{})
3736 " reuses previous buffer number
3737 call assert_equal(popbuf, winbufnr(popwin))
3738 call assert_equal(newbuf, bufnr('$'))
3739
3740 call popup_clear()
3741 bwipe!
3742endfunc
3743
Bram Moolenaarec084d32020-02-28 22:44:47 +01003744func Test_popupwin_filter_input_multibyte()
3745 func MyPopupFilter(winid, c)
3746 let g:bytes = range(a:c->strlen())->map({i -> char2nr(a:c[i])})
3747 return 0
3748 endfunc
3749 let winid = popup_create('', #{mapping: 0, filter: 'MyPopupFilter'})
3750
3751 " UTF-8: E3 80 80, including K_SPECIAL(0x80)
3752 call feedkeys("\u3000", 'xt')
3753 call assert_equal([0xe3, 0x80, 0x80], g:bytes)
3754
Bram Moolenaar8f027fe2020-03-04 23:21:35 +01003755 " UTF-8: E3 80 9B, including CSI(0x9B)
3756 call feedkeys("\u301b", 'xt')
3757 call assert_equal([0xe3, 0x80, 0x9b], g:bytes)
Bram Moolenaarec084d32020-02-28 22:44:47 +01003758
Bram Moolenaar20298ce2020-06-19 21:46:52 +02003759 if has('unix')
3760 " with modifyOtherKeys <M-S-a> does not include a modifier sequence
Bram Moolenaarb326edf2020-06-20 15:03:38 +02003761 if has('gui_running')
3762 call feedkeys("\x9b\xfc\x08A", 'Lx!')
3763 else
3764 call feedkeys("\<Esc>[27;4;65~", 'Lx!')
3765 endif
Bram Moolenaar20298ce2020-06-19 21:46:52 +02003766 call assert_equal([0xc3, 0x81], g:bytes)
3767 endif
3768
Bram Moolenaarec084d32020-02-28 22:44:47 +01003769 call popup_clear()
3770 delfunc MyPopupFilter
3771 unlet g:bytes
3772endfunc
3773
Bram Moolenaar6f8f7332020-08-10 21:19:23 +02003774func Test_popupwin_filter_close_ctrl_c()
3775 CheckScreendump
3776
3777 let lines =<< trim END
3778 vsplit
3779 set laststatus=2
3780 set statusline=%!Statusline()
3781
3782 function Statusline() abort
3783 return '%<%f %h%m%r%=%-14.(%l,%c%V%) %P'
3784 endfunction
3785
3786 call popup_create('test test test test...', {'filter': {-> 0}})
3787 END
3788 call writefile(lines, 'XtestPopupCtrlC')
3789
3790 let buf = RunVimInTerminal('-S XtestPopupCtrlC', #{rows: 10})
3791
3792 call term_sendkeys(buf, "\<C-C>")
3793 call VerifyScreenDump(buf, 'Test_popupwin_ctrl_c', {})
3794
3795 call StopVimInTerminal(buf)
Bram Moolenaar6defa7b2020-09-08 22:06:44 +02003796 call delete('XtestPopupCtrlC')
3797endfunc
3798
3799func Test_popupwin_filter_close_wrong_name()
3800 CheckScreendump
3801
3802 let lines =<< trim END
3803 call popup_create('one two three...', {'filter': 'NoSuchFunc'})
3804 END
3805 call writefile(lines, 'XtestPopupWrongName')
3806
3807 let buf = RunVimInTerminal('-S XtestPopupWrongName', #{rows: 10})
3808
3809 call term_sendkeys(buf, "j")
3810 call VerifyScreenDump(buf, 'Test_popupwin_wrong_name', {})
3811
3812 call StopVimInTerminal(buf)
3813 call delete('XtestPopupWrongName')
3814endfunc
3815
3816func Test_popupwin_filter_close_three_errors()
3817 CheckScreendump
3818
3819 let lines =<< trim END
3820 set cmdheight=2
3821 call popup_create('one two three...', {'filter': 'filter'})
3822 END
3823 call writefile(lines, 'XtestPopupThreeErrors')
3824
3825 let buf = RunVimInTerminal('-S XtestPopupThreeErrors', #{rows: 10})
3826
3827 call term_sendkeys(buf, "jj")
3828 call VerifyScreenDump(buf, 'Test_popupwin_three_errors_1', {})
3829 call term_sendkeys(buf, "j")
3830 call VerifyScreenDump(buf, 'Test_popupwin_three_errors_2', {})
3831
3832 call StopVimInTerminal(buf)
3833 call delete('XtestPopupThreeErrors')
Bram Moolenaar6f8f7332020-08-10 21:19:23 +02003834endfunc
3835
Bram Moolenaar927495b2020-11-06 17:58:35 +01003836func Test_popupwin_latin1_encoding()
3837 CheckScreendump
3838 CheckUnix
3839
3840 " When 'encoding' is a single-byte encoding a terminal window will mess up
3841 " the display. Check that showing a popup on top of that doesn't crash.
3842 let lines =<< trim END
3843 set encoding=latin1
3844 terminal cat Xmultibyte
3845 call popup_create(['one', 'two', 'three', 'four'], #{line: 1, col: 10})
Bram Moolenaar4c5bdb92020-12-17 17:45:59 +01003846 redraw
Bram Moolenaarb125b532020-12-17 21:56:09 +01003847 " wait for "cat" to finish
3848 while execute('ls!') !~ 'finished'
3849 sleep 10m
3850 endwhile
Bram Moolenaar4c5bdb92020-12-17 17:45:59 +01003851 echo "Done"
Bram Moolenaar927495b2020-11-06 17:58:35 +01003852 END
3853 call writefile(lines, 'XtestPopupLatin')
3854 call writefile([repeat("\u3042 ", 120)], 'Xmultibyte')
3855
3856 let buf = RunVimInTerminal('-S XtestPopupLatin', #{rows: 10})
Bram Moolenaar4c5bdb92020-12-17 17:45:59 +01003857 call WaitForAssert({-> assert_match('Done', term_getline(buf, 10))})
Bram Moolenaar927495b2020-11-06 17:58:35 +01003858
3859 call term_sendkeys(buf, ":q\<CR>")
3860 call StopVimInTerminal(buf)
3861 call delete('XtestPopupLatin')
3862 call delete('Xmultibyte')
3863endfunc
3864
Bram Moolenaarba2920f2020-03-06 21:43:17 +01003865func Test_popupwin_atcursor_far_right()
3866 new
3867
3868 " this was getting stuck
3869 set signcolumn=yes
3870 call setline(1, repeat('=', &columns))
3871 normal! ggg$
Bram Moolenaaref6b9792020-05-13 16:34:15 +02003872 let winid = popup_atcursor(repeat('x', 500), #{moved: 'any', border: []})
Bram Moolenaarba2920f2020-03-06 21:43:17 +01003873
Bram Moolenaar6d585f42020-07-26 22:20:54 +02003874 " 'signcolumn' was getting reset
3875 call setwinvar(winid, '&signcolumn', 'yes')
3876 call popup_setoptions(winid, {'zindex': 1000})
3877 call assert_equal('yes', getwinvar(winid, '&signcolumn'))
3878
Bram Moolenaaref6b9792020-05-13 16:34:15 +02003879 call popup_close(winid)
Bram Moolenaarba2920f2020-03-06 21:43:17 +01003880 bwipe!
3881 set signcolumn&
3882endfunc
3883
Bram Moolenaar0f1563f2020-03-20 21:15:51 +01003884func Test_popupwin_splitmove()
3885 vsplit
3886 let win2 = win_getid()
3887 let popup_winid = popup_dialog('hello', {})
3888 call assert_fails('call win_splitmove(popup_winid, win2)', 'E957:')
3889 call assert_fails('call win_splitmove(win2, popup_winid)', 'E957:')
3890
3891 call popup_clear()
3892 bwipe
3893endfunc
3894
Bram Moolenaarcbcd9cb2020-11-07 16:58:59 +01003895func Test_popupwin_exiting_terminal()
3896 CheckFeature terminal
3897
3898 " Tests that when creating a popup right after closing a terminal window does
3899 " not make the popup the current window.
3900 let winid = win_getid()
3901 try
3902 augroup Test_popupwin_exiting_terminal
3903 autocmd!
3904 autocmd WinEnter * :call popup_create('test', {})
3905 augroup END
3906 let bnr = term_start(&shell, #{term_finish: 'close'})
3907 call term_sendkeys(bnr, "exit\r\n")
3908 call WaitForAssert({-> assert_equal(winid, win_getid())})
3909 finally
3910 call popup_clear(1)
3911 augroup Test_popupwin_exiting_terminal
3912 autocmd!
3913 augroup END
3914 endtry
3915endfunc
Bram Moolenaar0f1563f2020-03-20 21:15:51 +01003916
Bram Moolenaar014f6982021-01-04 13:18:30 +01003917func Test_popup_filter_menu()
3918 let colors = ['red', 'green', 'blue']
3919 call popup_menu(colors, #{callback: {_, result -> assert_equal('green', colors[result - 1])}})
3920 call feedkeys("\<c-n>\<c-n>\<c-p>\<cr>", 'xt')
3921endfunc
3922
Bram Moolenaar65026432021-02-06 14:22:32 +01003923func Test_popup_getoptions_other_tab()
3924 new
3925 call setline(1, 'some text')
3926 call prop_type_add('textprop', {})
3927 call prop_add(1, 1, #{type: 'textprop', length: 1})
3928 let id = popup_create('TEST', #{textprop: 'textprop', highlight: 'ErrorMsg', tabpage: 1})
3929 tab sp
3930 call assert_equal(['textprop', 'textpropid', 'textpropwin'], popup_getoptions(id)->keys()->filter({_, v -> v =~ 'textprop'}))
3931
3932 tabclose
Bram Moolenaarefa19232021-02-06 14:59:27 +01003933 call popup_close(id)
Bram Moolenaar65026432021-02-06 14:22:32 +01003934 bwipe!
3935 call prop_type_delete('textprop')
3936endfunc
3937
Bram Moolenaarefa19232021-02-06 14:59:27 +01003938
3939func Test_popup_setoptions_other_tab()
3940 new Xfile
3941 let winid = win_getid()
3942 call setline(1, 'some text')
3943 call prop_type_add('textprop', {})
3944 call prop_add(1, 1, #{type: 'textprop', length: 1})
3945 let id = popup_create('TEST', #{textprop: 'textprop'})
3946 tab sp
3947 call popup_setoptions(id, #{textprop: 'textprop', textpropwin: winid})
3948 call assert_equal(winid, popup_getoptions(id).textpropwin)
3949
3950 tabclose
3951 call popup_close(id)
3952 bwipe! Xfile
3953 call prop_type_delete('textprop')
3954endfunc
3955
Bram Moolenaar82db31c2021-02-10 14:56:11 +01003956func Test_popup_prop_not_visible()
3957 CheckScreendump
3958
3959 let lines =<< trim END
3960 vim9script
3961 set nowrap stal=2
3962 rightbelow :31vnew
3963 setline(1, ['', 'some text', '', 'other text'])
3964 prop_type_add('someprop', {})
3965 prop_add(2, 9, {type: 'someprop', length: 5})
3966 popup_create('attached to "some"', {
3967 textprop: 'someprop',
3968 highlight: 'ErrorMsg',
3969 line: -1,
3970 wrap: false,
3971 fixed: true,
3972 })
3973 prop_type_add('otherprop', {})
3974 prop_add(4, 10, {type: 'otherprop', length: 5})
3975 popup_create('attached to "other"', {
3976 textprop: 'otherprop',
3977 highlight: 'ErrorMsg',
3978 line: -1,
3979 wrap: false,
3980 fixed: false,
3981 })
3982 END
3983 call writefile(lines, 'XtestPropNotVisble')
3984 let buf = RunVimInTerminal('-S XtestPropNotVisble', #{rows: 10})
3985 call VerifyScreenDump(buf, 'Test_popup_prop_not_visible_01', {})
3986
3987 call term_sendkeys(buf, ":vert resize -14\<CR>")
3988 call VerifyScreenDump(buf, 'Test_popup_prop_not_visible_02', {})
3989
3990 call term_sendkeys(buf, ":vert resize -8\<CR>")
3991 call VerifyScreenDump(buf, 'Test_popup_prop_not_visible_03', {})
3992
3993 " clean up
3994 call StopVimInTerminal(buf)
3995 call delete('XtestPropNotVisble')
3996endfunction
3997
Bram Moolenaar7b4f76c2021-06-10 21:07:48 +02003998func Test_bufdel_skips_popupwin_buffer()
3999 let id = popup_create("Some text", {})
4000 %bd
4001 call popup_close(id)
4002endfunc
4003
Bram Moolenaar82db31c2021-02-10 14:56:11 +01004004
Bram Moolenaar331bafd2019-07-20 17:46:05 +02004005" vim: shiftwidth=2 sts=2