1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
|
" Test that the system menu can be loaded.
source check.vim
CheckFeature menu
func Test_load_menu()
try
source $VIMRUNTIME/menu.vim
catch
call assert_report('error while loading menus: ' . v:exception)
endtry
call assert_match('browse confirm w', execute(':menu File.Save'))
let v:errmsg = ''
doautocmd LoadBufferMenu VimEnter
call assert_equal('', v:errmsg)
source $VIMRUNTIME/delmenu.vim
call assert_equal('', v:errmsg)
endfunc
func Test_buffer_menu_special_buffers()
" Load in runtime menus
try
source $VIMRUNTIME/menu.vim
catch
call assert_report('error while loading menus: ' . v:exception)
endtry
let v:errmsg = ''
doautocmd LoadBufferMenu VimEnter
call assert_equal('', v:errmsg)
let orig_buffer_menus = execute("nmenu Buffers")
" Test that regular new buffer results in a new buffer menu item.
new
let new_buffer_menus = execute('nmenu Buffers')
call assert_equal(len(split(orig_buffer_menus, "\n")) + 2, len(split(new_buffer_menus, "\n")))
bwipe!
call assert_equal(orig_buffer_menus, execute("nmenu Buffers"))
" Make a new command-line window, test that it does not create a new buffer
" menu.
call feedkeys("q::let cmdline_buffer_menus=execute('nmenu Buffers')\<CR>:q\<CR>", 'ntx')
call assert_equal(len(split(orig_buffer_menus, "\n")) + 2, len(split(cmdline_buffer_menus, "\n")))
call assert_equal(orig_buffer_menus, execute("nmenu Buffers"))
if has('terminal')
" Open a terminal window and test that it does not create a buffer menu
" item.
terminal
let term_buffer_menus = execute('nmenu Buffers')
call assert_equal(len(split(orig_buffer_menus, "\n")) + 2, len(split(term_buffer_menus, "\n")))
bwipe!
call assert_equal(orig_buffer_menus, execute("nmenu Buffers"))
endif
" Remove menus to clean up
source $VIMRUNTIME/delmenu.vim
call assert_equal('', v:errmsg)
endfunc
func Test_translate_menu()
CheckFeature multi_lang
if !filereadable($VIMRUNTIME . '/lang/menu_de_de.latin1.vim')
throw 'Skipped: translated menu not found'
endif
" First delete any English menus.
source $VIMRUNTIME/delmenu.vim
set langmenu=de_de
source $VIMRUNTIME/menu.vim
call assert_match('browse confirm w', execute(':menu Datei.Speichern'))
source $VIMRUNTIME/delmenu.vim
endfunc
func Test_menu_commands()
nmenu 2 Test.FooBar :let g:did_menu = 'normal'<CR>
vmenu 2 Test.FooBar :let g:did_menu = 'visual'<CR>
smenu 2 Test.FooBar :let g:did_menu = 'select'<CR>
omenu 2 Test.FooBar :let g:did_menu = 'op-pending'<CR>
tlmenu 2 Test.FooBar :let g:did_menu = 'terminal'<CR>
imenu 2 Test.FooBar :let g:did_menu = 'insert'<CR>
cmenu 2 Test.FooBar :let g:did_menu = 'cmdline'<CR>
emenu n Test.FooBar
call feedkeys(":menu Test.FooB\<C-A>\<C-B>\"\<CR>", 'tx')
call assert_equal('"menu Test.FooBar', @:)
call assert_equal('normal', g:did_menu)
emenu v Test.FooBar
call assert_equal('visual', g:did_menu)
emenu s Test.FooBar
call assert_equal('select', g:did_menu)
emenu o Test.FooBar
call assert_equal('op-pending', g:did_menu)
emenu t Test.FooBar
call assert_equal('terminal', g:did_menu)
emenu i Test.FooBar
call assert_equal('insert', g:did_menu)
emenu c Test.FooBar
call assert_equal('cmdline', g:did_menu)
nunmenu Test.FooBar
call assert_fails('emenu n Test.FooBar', 'E335: Menu not defined for Normal mode')
vunmenu Test.FooBar
call assert_fails('emenu v Test.FooBar', 'E335: Menu not defined for Visual mode')
vmenu 2 Test.FooBar :let g:did_menu = 'visual'<CR>
sunmenu Test.FooBar
call assert_fails('emenu s Test.FooBar', 'E335: Menu not defined for Select mode')
ounmenu Test.FooBar
call assert_fails('emenu o Test.FooBar', 'E335: Menu not defined for Op-pending mode')
iunmenu Test.FooBar
call assert_fails('emenu i Test.FooBar', 'E335: Menu not defined for Insert mode')
cunmenu Test.FooBar
call assert_fails('emenu c Test.FooBar', 'E335: Menu not defined for Cmdline mode')
tlunmenu Test.FooBar
call assert_fails('emenu t Test.FooBar', 'E335: Menu not defined for Terminal mode')
aunmenu Test.FooBar
call assert_fails('emenu n Test.FooBar', 'E334:')
nmenu 2 Test.FooBar.Child :let g:did_menu = 'foobar'<CR>
call assert_fails('emenu n Test.FooBar', 'E333:')
nunmenu Test.FooBar.Child
unlet g:did_menu
endfun
" Test various menu related errors
func Test_menu_errors()
menu Test.Foo :version<CR>
" Error cases
call assert_fails('menu .Test.Foo :ls<CR>', 'E475:')
call assert_fails('menu Test. :ls<CR>', 'E330:')
call assert_fails('menu Foo. :ls<CR>', 'E331:')
call assert_fails('unmenu Test.Foo abc', 'E488:')
call assert_fails('menu <Tab>:ls :ls<CR>', 'E792:')
call assert_fails('menu Test.<Tab>:ls :ls<CR>', 'E792:')
call assert_fails('menu Test.Foo.Bar :ls<CR>', 'E327:')
call assert_fails('menu Test.-Sep-.Baz :ls<CR>', 'E332:')
call assert_fails('menu Foo.Bar.--.Baz :ls<CR>', 'E332:')
call assert_fails('menu disable Test.Foo.Bar', 'E327:')
call assert_fails('menu disable T.Foo', 'E329:')
call assert_fails('unmenu Test.Foo.Bar', 'E327:')
call assert_fails('cunmenu Test.Foo', 'E328:')
call assert_fails('unmenu Test.Bar', 'E329:')
call assert_fails('menu Test.Foo.Bar', 'E327:')
call assert_fails('cmenu Test.Foo', 'E328:')
call assert_fails('emenu x Test.Foo', 'E475:')
call assert_fails('emenu Test.Foo.Bar', 'E327:')
call assert_fails('menutranslate Test', 'E474:')
silent! unmenu Foo
unmenu Test
endfunc
" Test for menu item completion in command line
func Test_menu_expand()
" Make sure we don't have stale menu items like Buffers menu.
source $VIMRUNTIME/delmenu.vim
" Create the menu itmes for test
menu Dummy.Nothing lll
for i in range(1, 4)
let m = 'menu Xmenu.A' .. i .. '.A' .. i
for j in range(1, 4)
exe m .. 'B' .. j .. ' :echo "A' .. i .. 'B' .. j .. '"' .. "<CR>"
endfor
endfor
set wildmenu
" Test for <CR> selecting a submenu
call feedkeys(":emenu Xmenu.A\<Tab>\<CR>\<Right>x\<BS>\<C-B>\"\<CR>", 'xt')
call assert_equal('"emenu Xmenu.A1.A1B2', @:)
" Test for <Down> selecting a submenu
call feedkeys(":emenu Xmenu.A\<Tab>\<Right>\<Right>\<Down>" ..
\ "\<C-A>\<C-B>\"\<CR>", 'xt')
call assert_equal('"emenu Xmenu.A3.A3B1 A3B2 A3B3 A3B4', @:)
" Test for <Up> to go up a submenu
call feedkeys(":emenu Xmenu.A\<Tab>\<Down>\<Up>\<Right>\<Right>" ..
\ "\<Left>\<Down>\<C-A>\<C-B>\"\<CR>", 'xt')
call assert_equal('"emenu Xmenu.A2.A2B1 A2B2 A2B3 A2B4', @:)
" Test for <Up> to go up a menu
call feedkeys(":emenu Xmenu.A\<Tab>\<Down>\<Up>\<Up>\<Up>" ..
\ "\<C-A>\<C-B>\"\<CR>", 'xt')
call assert_equal('"emenu Dummy. Xmenu.', @:)
" Test for expanding only submenus
call feedkeys(":popup Xmenu.\<C-A>\<C-B>\"\<CR>", 'xt')
call assert_equal('"popup Xmenu.A1 A2 A3 A4', @:)
" Test for expanding menus after enable/disable
call feedkeys(":menu enable Xmenu.\<C-A>\<C-B>\"\<CR>", 'xt')
call assert_equal('"menu enable Xmenu.A1. A2. A3. A4.', @:)
call feedkeys(":menu disable Xmenu.\<C-A>\<C-B>\"\<CR>", 'xt')
call assert_equal('"menu disable Xmenu.A1. A2. A3. A4.', @:)
" Test for expanding non-existing menu path
call feedkeys(":menu xyz.\<C-A>\<C-B>\"\<CR>", 'xt')
call assert_equal('"menu xyz.', @:)
call feedkeys(":menu Xmenu.A1.A1B1.xyz.\<C-A>\<C-B>\"\<CR>", 'xt')
call assert_equal('"menu Xmenu.A1.A1B1.xyz.', @:)
set wildmenu&
unmenu Xmenu
unmenu Dummy
" Test for expanding popup menus with some hidden items
menu Xmenu.foo.A1 a1
menu Xmenu.]bar bar
menu Xmenu.]baz.B1 b1
menu Xmenu.-sep- :
call feedkeys(":popup Xmenu.\<C-A>\<C-B>\"\<CR>", 'xt')
call assert_equal('"popup Xmenu.foo', @:)
unmenu Xmenu
endfunc
" Test for the menu_info() function
func Test_menu_info()
" Define menus with various attributes
10nnoremenu 10.10 T&est.F&oo :echo 'foo'<CR>
10nmenu <silent> 10.20 T&est.B&ar<Tab>:bar :echo 'bar'<CR>
10nmenu <script> 10.30.5 T&est.Ba&z.Qu&x :echo 'qux'<CR>
let d = #{name: "B&ar\t:bar", display: 'Bar', modes: 'n', shortcut: 'a',
\ accel: ':bar', priority: 20, enabled: v:true, silent: v:true,
\ noremenu: v:false, script: v:false, rhs: ":echo 'bar'<CR>"}
call assert_equal(d, menu_info('Test.Bar'))
let d = #{name: 'Ba&z', display: 'Baz', modes: 'n', shortcut: 'z',
\ priority: 30, submenus: ['Qux']}
call assert_equal(d, menu_info('Test.Baz'))
let d = #{name: 'T&est', display: 'Test', modes: 'n', shortcut: 'e',
\ priority: 10, submenus: ['Foo', 'Bar', 'Baz']}
call assert_equal(d, menu_info('Test'))
call assert_equal({}, menu_info('Test.Dummy'))
call assert_equal({}, menu_info('Dummy'))
nmenu disable Test.Foo
call assert_equal(v:false, menu_info('Test.Foo').enabled)
nmenu enable Test.Foo
call assert_equal(v:true, menu_info('Test.Foo').enabled)
call assert_equal(menu_info('Test.Foo'), menu_info('Test.Foo', ''))
nmenu Test.abc <Nop>
call assert_equal('<Nop>', menu_info('Test.abc').rhs)
call assert_fails('call menu_info([])', 'E730:')
call assert_fails('call menu_info("", [])', 'E730:')
nunmenu Test
" Test for defining menus in different modes
menu Test.menu :menu<CR>
menu! Test.menu! :menu!<CR>
amenu Test.amenu :amenu<CR>
nmenu Test.nmenu :nmenu<CR>
omenu Test.omenu :omenu<CR>
vmenu Test.vmenu :vmenu<CR>
xmenu Test.xmenu :xmenu<CR>
smenu Test.smenu :smenu<CR>
imenu <silent> <script> Test.imenu :imenu<CR>
cmenu Test.cmenu :cmenu<CR>
tlmenu Test.tlmenu :tlmenu<CR>
tmenu Test.nmenu Normal mode menu
tmenu Test.omenu Op-pending mode menu
noremenu Test.noremenu :noremenu<CR>
noremenu! Test.noremenu! :noremenu!<CR>
anoremenu Test.anoremenu :anoremenu<CR>
nnoremenu Test.nnoremenu :nnoremenu<CR>
onoremenu Test.onoremenu :onoremenu<CR>
vnoremenu Test.vnoremenu :vnoremenu<CR>
xnoremenu Test.xnoremenu :xnoremenu<CR>
snoremenu Test.snoremenu :snoremenu<CR>
inoremenu <silent> Test.inoremenu :inoremenu<CR>
cnoremenu Test.cnoremenu :cnoremenu<CR>
tlnoremenu Test.tlnoremenu :tlnoremenu<CR>
call assert_equal(#{name: 'menu', priority: 500, shortcut: '',
\ display: 'menu', modes: ' ', enabled: v:true, silent: v:false,
\ rhs: ":menu<CR>", noremenu: v:false, script: v:false},
\ menu_info('Test.menu'))
call assert_equal(#{name: 'menu!', priority: 500, shortcut: '',
\ display: 'menu!', modes: '!', enabled: v:true, silent: v:false,
\ rhs: ":menu!<CR>", noremenu: v:false, script: v:false},
\ menu_info('Test.menu!', '!'))
call assert_equal(#{name: 'amenu', priority: 500, shortcut: '',
\ display: 'amenu', modes: 'a', enabled: v:true, silent: v:false,
\ rhs: ":amenu<CR>", noremenu: v:false, script: v:false},
\ menu_info('Test.amenu', 'a'))
call assert_equal(#{name: 'nmenu', priority: 500, shortcut: '',
\ display: 'nmenu', modes: 'n', enabled: v:true, silent: v:false,
\ rhs: ':nmenu<CR>', noremenu: v:false, script: v:false},
\ menu_info('Test.nmenu', 'n'))
call assert_equal(#{name: 'omenu', priority: 500, shortcut: '',
\ display: 'omenu', modes: 'o', enabled: v:true, silent: v:false,
\ rhs: ':omenu<CR>', noremenu: v:false, script: v:false},
|