forked from b4winckler/macvim
/
menu.c
2867 lines (2625 loc) · 63.4 KB
/
menu.c
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
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* vi:set ts=8 sts=4 sw=4:
*
* VIM - Vi IMproved by Bram Moolenaar
* GUI/Motif support by Robert Webb
*
* Do ":help uganda" in Vim to read copying and usage conditions.
* Do ":help credits" in Vim to see a list of people who contributed.
* See README.txt for an overview of the Vim source code.
*/
/*
* Code for menus. Used for the GUI and 'wildmenu'.
*/
#include "vim.h"
#if defined(FEAT_MENU) || defined(PROTO)
#define MENUDEPTH 10 /* maximum depth of menus */
#ifdef FEAT_GUI_W32
static int add_menu_path __ARGS((char_u *, vimmenu_T *, int *, char_u *, int));
#else
static int add_menu_path __ARGS((char_u *, vimmenu_T *, int *, char_u *));
#endif
static int menu_nable_recurse __ARGS((vimmenu_T *menu, char_u *name, int modes, int enable));
static int remove_menu __ARGS((vimmenu_T **, char_u *, int, int silent));
static void free_menu __ARGS((vimmenu_T **menup));
static void free_menu_string __ARGS((vimmenu_T *, int));
static int show_menus __ARGS((char_u *, int));
static void show_menus_recursive __ARGS((vimmenu_T *, int, int));
static int menu_name_equal __ARGS((char_u *name, vimmenu_T *menu));
static int menu_namecmp __ARGS((char_u *name, char_u *mname));
static int get_menu_cmd_modes __ARGS((char_u *, int, int *, int *));
static char_u *popup_mode_name __ARGS((char_u *name, int idx));
static char_u *menu_text __ARGS((char_u *text, int *mnemonic, char_u **actext));
#ifdef FEAT_GUI
static int get_menu_mode __ARGS((void));
static void gui_update_menus_recurse __ARGS((vimmenu_T *, int));
#endif
#if defined(FEAT_GUI_W32) && defined(FEAT_TEAROFF)
static void gui_create_tearoffs_recurse __ARGS((vimmenu_T *menu, const char_u *pname, int *pri_tab, int pri_idx));
static void gui_add_tearoff __ARGS((char_u *tearpath, int *pri_tab, int pri_idx));
static void gui_destroy_tearoffs_recurse __ARGS((vimmenu_T *menu));
static int s_tearoffs = FALSE;
#endif
static int menu_is_hidden __ARGS((char_u *name));
#if defined(FEAT_CMDL_COMPL) || (defined(FEAT_GUI_W32) && defined(FEAT_TEAROFF))
static int menu_is_tearoff __ARGS((char_u *name));
#endif
#if defined(FEAT_MULTI_LANG) || defined(FEAT_TOOLBAR)
static char_u *menu_skip_part __ARGS((char_u *p));
#endif
#ifdef FEAT_MULTI_LANG
static char_u *menutrans_lookup __ARGS((char_u *name, int len));
#endif
/* The character for each menu mode */
static char_u menu_mode_chars[] = {'n', 'v', 's', 'o', 'i', 'c', 't'};
static char_u e_notsubmenu[] = N_("E327: Part of menu-item path is not sub-menu");
static char_u e_othermode[] = N_("E328: Menu only exists in another mode");
static char_u e_nomenu[] = N_("E329: No menu \"%s\"");
#ifdef FEAT_TOOLBAR
static const char *toolbar_names[] =
{
/* 0 */ "New", "Open", "Save", "Undo", "Redo",
/* 5 */ "Cut", "Copy", "Paste", "Print", "Help",
/* 10 */ "Find", "SaveAll", "SaveSesn", "NewSesn", "LoadSesn",
/* 15 */ "RunScript", "Replace", "WinClose", "WinMax", "WinMin",
/* 20 */ "WinSplit", "Shell", "FindPrev", "FindNext", "FindHelp",
/* 25 */ "Make", "TagJump", "RunCtags", "WinVSplit", "WinMaxWidth",
/* 30 */ "WinMinWidth", "Exit"
};
# define TOOLBAR_NAME_COUNT (sizeof(toolbar_names) / sizeof(char *))
#endif
/*
* Do the :menu command and relatives.
*/
void
ex_menu(eap)
exarg_T *eap; /* Ex command arguments */
{
char_u *menu_path;
int modes;
char_u *map_to;
int noremap;
int silent = FALSE;
int special = FALSE;
int unmenu;
char_u *map_buf;
char_u *arg;
char_u *p;
int i;
#if defined(FEAT_GUI) && !(defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MACVIM))
int old_menu_height;
# if defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_W32) && !defined(FEAT_GUI_W16)
int old_toolbar_height;
# endif
#endif
int pri_tab[MENUDEPTH + 1];
int enable = MAYBE; /* TRUE for "menu enable", FALSE for "menu
* disable */
#ifdef FEAT_MULTI_LANG
char_u *tofree = NULL;
char_u *new_cmd;
#endif
#ifdef FEAT_TOOLBAR
char_u *icon = NULL;
#endif
vimmenu_T menuarg;
modes = get_menu_cmd_modes(eap->cmd, eap->forceit, &noremap, &unmenu);
arg = eap->arg;
for (;;)
{
if (STRNCMP(arg, "<script>", 8) == 0)
{
noremap = REMAP_SCRIPT;
arg = skipwhite(arg + 8);
continue;
}
if (STRNCMP(arg, "<silent>", 8) == 0)
{
silent = TRUE;
arg = skipwhite(arg + 8);
continue;
}
if (STRNCMP(arg, "<special>", 9) == 0)
{
special = TRUE;
arg = skipwhite(arg + 9);
continue;
}
break;
}
/* Locate an optional "icon=filename" argument. */
if (STRNCMP(arg, "icon=", 5) == 0)
{
arg += 5;
#ifdef FEAT_TOOLBAR
icon = arg;
#endif
while (*arg != NUL && *arg != ' ')
{
if (*arg == '\\')
mch_memmove(arg, arg + 1, STRLEN(arg));
mb_ptr_adv(arg);
}
if (*arg != NUL)
{
*arg++ = NUL;
arg = skipwhite(arg);
}
}
/*
* Fill in the priority table.
*/
for (p = arg; *p; ++p)
if (!VIM_ISDIGIT(*p) && *p != '.')
break;
if (vim_iswhite(*p))
{
for (i = 0; i < MENUDEPTH && !vim_iswhite(*arg); ++i)
{
pri_tab[i] = getdigits(&arg);
if (pri_tab[i] == 0)
pri_tab[i] = 500;
if (*arg == '.')
++arg;
}
arg = skipwhite(arg);
}
else if (eap->addr_count && eap->line2 != 0)
{
pri_tab[0] = eap->line2;
i = 1;
}
else
i = 0;
while (i < MENUDEPTH)
pri_tab[i++] = 500;
pri_tab[MENUDEPTH] = -1; /* mark end of the table */
/*
* Check for "disable" or "enable" argument.
*/
if (STRNCMP(arg, "enable", 6) == 0 && vim_iswhite(arg[6]))
{
enable = TRUE;
arg = skipwhite(arg + 6);
}
else if (STRNCMP(arg, "disable", 7) == 0 && vim_iswhite(arg[7]))
{
enable = FALSE;
arg = skipwhite(arg + 7);
}
/*
* If there is no argument, display all menus.
*/
if (*arg == NUL)
{
show_menus(arg, modes);
return;
}
#ifdef FEAT_TOOLBAR
/*
* Need to get the toolbar icon index before doing the translation.
*/
menuarg.iconidx = -1;
menuarg.icon_builtin = FALSE;
if (menu_is_toolbar(arg))
{
menu_path = menu_skip_part(arg);
if (*menu_path == '.')
{
p = menu_skip_part(++menu_path);
if (STRNCMP(menu_path, "BuiltIn", 7) == 0)
{
if (skipdigits(menu_path + 7) == p)
{
menuarg.iconidx = atoi((char *)menu_path + 7);
if (menuarg.iconidx >= TOOLBAR_NAME_COUNT)
menuarg.iconidx = -1;
else
menuarg.icon_builtin = TRUE;
}
}
else
{
for (i = 0; i < TOOLBAR_NAME_COUNT; ++i)
if (STRNCMP(toolbar_names[i], menu_path, p - menu_path)
== 0)
{
menuarg.iconidx = i;
break;
}
}
}
}
#endif
#ifdef FEAT_MULTI_LANG
/*
* Translate menu names as specified with ":menutrans" commands.
*/
menu_path = arg;
while (*menu_path)
{
/* find the end of one part and check if it should be translated */
p = menu_skip_part(menu_path);
map_to = menutrans_lookup(menu_path, (int)(p - menu_path));
if (map_to != NULL)
{
/* found a match: replace with the translated part */
i = (int)STRLEN(map_to);
new_cmd = alloc((unsigned)STRLEN(arg) + i + 1);
if (new_cmd == NULL)
break;
mch_memmove(new_cmd, arg, menu_path - arg);
mch_memmove(new_cmd + (menu_path - arg), map_to, (size_t)i);
STRCPY(new_cmd + (menu_path - arg) + i, p);
p = new_cmd + (menu_path - arg) + i;
vim_free(tofree);
tofree = new_cmd;
arg = new_cmd;
}
if (*p != '.')
break;
menu_path = p + 1;
}
#endif
/*
* Isolate the menu name.
* Skip the menu name, and translate <Tab> into a real TAB.
*/
menu_path = arg;
if (*menu_path == '.')
{
EMSG2(_(e_invarg2), menu_path);
goto theend;
}
while (*arg && !vim_iswhite(*arg))
{
if ((*arg == '\\' || *arg == Ctrl_V) && arg[1] != NUL)
arg++;
else if (STRNICMP(arg, "<TAB>", 5) == 0)
{
*arg = TAB;
mch_memmove(arg + 1, arg + 5, STRLEN(arg + 4));
}
arg++;
}
if (*arg != NUL)
*arg++ = NUL;
arg = skipwhite(arg);
map_to = arg;
/*
* If there is only a menu name, display menus with that name.
*/
if (*map_to == NUL && !unmenu && enable == MAYBE)
{
show_menus(menu_path, modes);
goto theend;
}
else if (*map_to != NUL && (unmenu || enable != MAYBE))
{
EMSG(_(e_trailing));
goto theend;
}
#if defined(FEAT_GUI) && !(defined(FEAT_GUI_GTK) || defined(FEAT_GUI_PHOTON) \
|| defined(FEAT_GUI_MACVIM))
old_menu_height = gui.menu_height;
# if defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_W32) && !defined(FEAT_GUI_W16)
old_toolbar_height = gui.toolbar_height;
# endif
#endif
if (enable != MAYBE)
{
/*
* Change sensitivity of the menu.
* For the PopUp menu, remove a menu for each mode separately.
* Careful: menu_nable_recurse() changes menu_path.
*/
if (STRCMP(menu_path, "*") == 0) /* meaning: do all menus */
menu_path = (char_u *)"";
if (menu_is_popup(menu_path))
{
for (i = 0; i < MENU_INDEX_TIP; ++i)
if (modes & (1 << i))
{
p = popup_mode_name(menu_path, i);
if (p != NULL)
{
menu_nable_recurse(root_menu, p, MENU_ALL_MODES,
enable);
vim_free(p);
}
}
}
menu_nable_recurse(root_menu, menu_path, modes, enable);
}
else if (unmenu)
{
/*
* Delete menu(s).
*/
if (STRCMP(menu_path, "*") == 0) /* meaning: remove all menus */
menu_path = (char_u *)"";
/*
* For the PopUp menu, remove a menu for each mode separately.
*/
if (menu_is_popup(menu_path))
{
for (i = 0; i < MENU_INDEX_TIP; ++i)
if (modes & (1 << i))
{
p = popup_mode_name(menu_path, i);
if (p != NULL)
{
remove_menu(&root_menu, p, MENU_ALL_MODES, TRUE);
vim_free(p);
}
}
}
/* Careful: remove_menu() changes menu_path */
remove_menu(&root_menu, menu_path, modes, FALSE);
}
else
{
/*
* Add menu(s).
* Replace special key codes.
*/
if (STRICMP(map_to, "<nop>") == 0) /* "<Nop>" means nothing */
{
map_to = (char_u *)"";
map_buf = NULL;
}
else if (modes & MENU_TIP_MODE)
map_buf = NULL; /* Menu tips are plain text. */
else
map_to = replace_termcodes(map_to, &map_buf, FALSE, TRUE, special);
menuarg.modes = modes;
#ifdef FEAT_TOOLBAR
menuarg.iconfile = icon;
#endif
menuarg.noremap[0] = noremap;
menuarg.silent[0] = silent;
add_menu_path(menu_path, &menuarg, pri_tab, map_to
#ifdef FEAT_GUI_W32
, TRUE
#endif
);
/*
* For the PopUp menu, add a menu for each mode separately.
*/
if (menu_is_popup(menu_path))
{
for (i = 0; i < MENU_INDEX_TIP; ++i)
if (modes & (1 << i))
{
p = popup_mode_name(menu_path, i);
if (p != NULL)
{
/* Include all modes, to make ":amenu" work */
menuarg.modes = modes;
#ifdef FEAT_TOOLBAR
menuarg.iconfile = NULL;
menuarg.iconidx = -1;
menuarg.icon_builtin = FALSE;
#endif
add_menu_path(p, &menuarg, pri_tab, map_to
#ifdef FEAT_GUI_W32
, TRUE
#endif
);
vim_free(p);
}
}
}
vim_free(map_buf);
}
#if defined(FEAT_GUI) && !(defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MACVIM))
/* If the menubar height changed, resize the window */
if (gui.in_use
&& (gui.menu_height != old_menu_height
# if defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_W32) && !defined(FEAT_GUI_W16)
|| gui.toolbar_height != old_toolbar_height
# endif
))
gui_set_shellsize(FALSE, FALSE, RESIZE_VERT);
#endif
theend:
#ifdef FEAT_MULTI_LANG
vim_free(tofree);
#else
;
#endif
}
/*
* Add the menu with the given name to the menu hierarchy
*/
static int
add_menu_path(menu_path, menuarg, pri_tab, call_data
#ifdef FEAT_GUI_W32
, addtearoff
#endif
)
char_u *menu_path;
vimmenu_T *menuarg; /* passes modes, iconfile, iconidx,
icon_builtin, silent[0], noremap[0] */
int *pri_tab;
char_u *call_data;
#ifdef FEAT_GUI_W32
int addtearoff; /* may add tearoff item */
#endif
{
char_u *path_name;
int modes = menuarg->modes;
vimmenu_T **menup;
vimmenu_T *menu = NULL;
vimmenu_T *parent;
vimmenu_T **lower_pri;
char_u *p;
char_u *name;
char_u *dname;
char_u *next_name;
int i;
int c;
#ifdef FEAT_GUI
int idx;
int new_idx;
#endif
int pri_idx = 0;
int old_modes = 0;
int amenu;
/* Make a copy so we can stuff around with it, since it could be const */
path_name = vim_strsave(menu_path);
if (path_name == NULL)
return FAIL;
menup = &root_menu;
parent = NULL;
name = path_name;
while (*name)
{
/* Get name of this element in the menu hierarchy, and the simplified
* name (without mnemonic and accelerator text). */
next_name = menu_name_skip(name);
dname = menu_text(name, NULL, NULL);
if (dname == NULL)
goto erret;
if (*dname == NUL)
{
/* Only a mnemonic or accelerator is not valid. */
EMSG(_("E792: Empty menu name"));
goto erret;
}
/* See if it's already there */
lower_pri = menup;
#ifdef FEAT_GUI
idx = 0;
new_idx = 0;
#endif
menu = *menup;
while (menu != NULL)
{
if (menu_name_equal(name, menu) || menu_name_equal(dname, menu))
{
if (*next_name == NUL && menu->children != NULL)
{
if (!sys_menu)
EMSG(_("E330: Menu path must not lead to a sub-menu"));
goto erret;
}
if (*next_name != NUL && menu->children == NULL
#ifdef FEAT_GUI_W32
&& addtearoff
#endif
)
{
if (!sys_menu)
EMSG(_(e_notsubmenu));
goto erret;
}
break;
}
menup = &menu->next;
/* Count menus, to find where this one needs to be inserted.
* Ignore menus that are not in the menubar (PopUp and Toolbar) */
if (parent != NULL || menu_is_menubar(menu->name))
{
#ifdef FEAT_GUI
++idx;
#endif
if (menu->priority <= pri_tab[pri_idx])
{
lower_pri = menup;
#ifdef FEAT_GUI
new_idx = idx;
#endif
}
}
menu = menu->next;
}
if (menu == NULL)
{
if (*next_name == NUL && parent == NULL)
{
EMSG(_("E331: Must not add menu items directly to menu bar"));
goto erret;
}
if (menu_is_separator(dname) && *next_name != NUL)
{
EMSG(_("E332: Separator cannot be part of a menu path"));
goto erret;
}
/* Not already there, so lets add it */
menu = (vimmenu_T *)alloc_clear((unsigned)sizeof(vimmenu_T));
if (menu == NULL)
goto erret;
menu->modes = modes;
menu->enabled = MENU_ALL_MODES;
menu->name = vim_strsave(name);
/* separate mnemonic and accelerator text from actual menu name */
menu->dname = menu_text(name, &menu->mnemonic, &menu->actext);
menu->priority = pri_tab[pri_idx];
menu->parent = parent;
#ifdef FEAT_GUI_MOTIF
menu->sensitive = TRUE; /* the default */
#endif
#ifdef FEAT_BEVAL_TIP
menu->tip = NULL;
#endif
#ifdef FEAT_GUI_ATHENA
menu->image = None; /* X-Windows definition for NULL*/
#endif
/*
* Add after menu that has lower priority.
*/
menu->next = *lower_pri;
*lower_pri = menu;
old_modes = 0;
#ifdef FEAT_TOOLBAR
menu->iconidx = menuarg->iconidx;
menu->icon_builtin = menuarg->icon_builtin;
if (*next_name == NUL && menuarg->iconfile != NULL)
menu->iconfile = vim_strsave(menuarg->iconfile);
#endif
#if defined(FEAT_GUI_W32) && defined(FEAT_TEAROFF)
/* the tearoff item must be present in the modes of each item. */
if (parent != NULL && menu_is_tearoff(parent->children->dname))
parent->children->modes |= modes;
#endif
}
else
{
old_modes = menu->modes;
/*
* If this menu option was previously only available in other
* modes, then make sure it's available for this one now
* Also enable a menu when it's created or changed.
*/
#ifdef FEAT_GUI_W32
/* If adding a tearbar (addtearoff == FALSE) don't update modes */
if (addtearoff)
#endif
{
menu->modes |= modes;
menu->enabled |= modes;
}
}
#ifdef FEAT_GUI
/*
* Add the menu item when it's used in one of the modes, but not when
* only a tooltip is defined.
*/
if ((old_modes & MENU_ALL_MODES) == 0
&& (menu->modes & MENU_ALL_MODES) != 0)
{
if (gui.in_use) /* Otherwise it will be added when GUI starts */
{
if (*next_name == NUL)
{
/* Real menu item, not sub-menu */
gui_mch_add_menu_item(menu, new_idx);
/* Want to update menus now even if mode not changed */
force_menu_update = TRUE;
}
else
{
/* Sub-menu (not at end of path yet) */
gui_mch_add_menu(menu, new_idx);
}
}
# if defined(FEAT_GUI_W32) & defined(FEAT_TEAROFF)
/* When adding a new submenu, may add a tearoff item */
if ( addtearoff
&& *next_name
&& vim_strchr(p_go, GO_TEAROFF) != NULL
&& menu_is_menubar(name))
{
char_u *tearpath;
/*
* The pointers next_name & path_name refer to a string with
* \'s and ^V's stripped out. But menu_path is a "raw"
* string, so we must correct for special characters.
*/
tearpath = alloc((unsigned int)STRLEN(menu_path) + TEAR_LEN + 2);
if (tearpath != NULL)
{
char_u *s;
int idx;
STRCPY(tearpath, menu_path);
idx = (int)(next_name - path_name - 1);
for (s = tearpath; *s && s < tearpath + idx; mb_ptr_adv(s))
{
if ((*s == '\\' || *s == Ctrl_V) && s[1])
{
++idx;
++s;
}
}
tearpath[idx] = NUL;
gui_add_tearoff(tearpath, pri_tab, pri_idx);
vim_free(tearpath);
}
}
# endif
}
#endif /* FEAT_GUI */
menup = &menu->children;
parent = menu;
name = next_name;
vim_free(dname);
dname = NULL;
if (pri_tab[pri_idx + 1] != -1)
++pri_idx;
}
vim_free(path_name);
/*
* Only add system menu items which have not been defined yet.
* First check if this was an ":amenu".
*/
amenu = ((modes & (MENU_NORMAL_MODE | MENU_INSERT_MODE)) ==
(MENU_NORMAL_MODE | MENU_INSERT_MODE));
if (sys_menu)
modes &= ~old_modes;
if (menu != NULL && modes)
{
#ifdef FEAT_GUI
menu->cb = gui_menu_cb;
#endif
p = (call_data == NULL) ? NULL : vim_strsave(call_data);
/* loop over all modes, may add more than one */
for (i = 0; i < MENU_MODES; ++i)
{
if (modes & (1 << i))
{
/* free any old menu */
free_menu_string(menu, i);
/* For "amenu", may insert an extra character.
* Don't do this if adding a tearbar (addtearoff == FALSE).
* Don't do this for "<Nop>". */
c = 0;
if (amenu && call_data != NULL && *call_data != NUL
#ifdef FEAT_GUI_W32
&& addtearoff
#endif
)
{
switch (1 << i)
{
case MENU_VISUAL_MODE:
case MENU_SELECT_MODE:
case MENU_OP_PENDING_MODE:
case MENU_CMDLINE_MODE:
c = Ctrl_C;
break;
case MENU_INSERT_MODE:
c = Ctrl_O;
break;
}
}
if (c)
{
menu->strings[i] = alloc((unsigned)(STRLEN(call_data) + 4));
if (menu->strings[i] != NULL)
{
menu->strings[i][0] = c;
STRCPY(menu->strings[i] + 1, call_data);
if (c == Ctrl_C)
{
int len = (int)STRLEN(menu->strings[i]);
/* Append CTRL-\ CTRL-G to obey 'insertmode'. */
menu->strings[i][len] = Ctrl_BSL;
menu->strings[i][len + 1] = Ctrl_G;
menu->strings[i][len + 2] = NUL;
}
}
}
else
menu->strings[i] = p;
menu->noremap[i] = menuarg->noremap[0];
menu->silent[i] = menuarg->silent[0];
}
}
#if defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_W32) \
&& (defined(FEAT_BEVAL) || defined(FEAT_GUI_GTK))
/* Need to update the menu tip. */
if (modes & MENU_TIP_MODE)
gui_mch_menu_set_tip(menu);
#endif
}
return OK;
erret:
vim_free(path_name);
vim_free(dname);
/* Delete any empty submenu we added before discovering the error. Repeat
* for higher levels. */
while (parent != NULL && parent->children == NULL)
{
if (parent->parent == NULL)
menup = &root_menu;
else
menup = &parent->parent->children;
for ( ; *menup != NULL && *menup != parent; menup = &((*menup)->next))
;
if (*menup == NULL) /* safety check */
break;
parent = parent->parent;
free_menu(menup);
}
return FAIL;
}
/*
* Set the (sub)menu with the given name to enabled or disabled.
* Called recursively.
*/
static int
menu_nable_recurse(menu, name, modes, enable)
vimmenu_T *menu;
char_u *name;
int modes;
int enable;
{
char_u *p;
if (menu == NULL)
return OK; /* Got to bottom of hierarchy */
/* Get name of this element in the menu hierarchy */
p = menu_name_skip(name);
/* Find the menu */
while (menu != NULL)
{
if (*name == NUL || *name == '*' || menu_name_equal(name, menu))
{
if (*p != NUL)
{
if (menu->children == NULL)
{
EMSG(_(e_notsubmenu));
return FAIL;
}
if (menu_nable_recurse(menu->children, p, modes, enable)
== FAIL)
return FAIL;
}
else
if (enable)
menu->enabled |= modes;
else
menu->enabled &= ~modes;
/*
* When name is empty, we are doing all menu items for the given
* modes, so keep looping, otherwise we are just doing the named
* menu item (which has been found) so break here.
*/
if (*name != NUL && *name != '*')
break;
}
menu = menu->next;
}
if (*name != NUL && *name != '*' && menu == NULL)
{
EMSG2(_(e_nomenu), name);
return FAIL;
}
#ifdef FEAT_GUI
/* Want to update menus now even if mode not changed */
force_menu_update = TRUE;
#endif
return OK;
}
/*
* Remove the (sub)menu with the given name from the menu hierarchy
* Called recursively.
*/
static int
remove_menu(menup, name, modes, silent)
vimmenu_T **menup;
char_u *name;
int modes;
int silent; /* don't give error messages */
{
vimmenu_T *menu;
vimmenu_T *child;
char_u *p;
if (*menup == NULL)
return OK; /* Got to bottom of hierarchy */
/* Get name of this element in the menu hierarchy */
p = menu_name_skip(name);
/* Find the menu */
while ((menu = *menup) != NULL)
{
if (*name == NUL || menu_name_equal(name, menu))
{
if (*p != NUL && menu->children == NULL)
{
if (!silent)
EMSG(_(e_notsubmenu));
return FAIL;
}
if ((menu->modes & modes) != 0x0)
{
#if defined(FEAT_GUI_W32) & defined(FEAT_TEAROFF)
/*
* If we are removing all entries for this menu,MENU_ALL_MODES,
* Then kill any tearoff before we start
*/
if (*p == NUL && modes == MENU_ALL_MODES)
{
if (IsWindow(menu->tearoff_handle))
DestroyWindow(menu->tearoff_handle);
}
#endif
if (remove_menu(&menu->children, p, modes, silent) == FAIL)
return FAIL;
}
else if (*name != NUL)
{
if (!silent)
EMSG(_(e_othermode));
return FAIL;
}
/*
* When name is empty, we are removing all menu items for the given
* modes, so keep looping, otherwise we are just removing the named
* menu item (which has been found) so break here.
*/
if (*name != NUL)
break;
/* Remove the menu item for the given mode[s]. If the menu item
* is no longer valid in ANY mode, delete it */
menu->modes &= ~modes;
if (modes & MENU_TIP_MODE)
free_menu_string(menu, MENU_INDEX_TIP);
if ((menu->modes & MENU_ALL_MODES) == 0)
free_menu(menup);
else
menup = &menu->next;
}
else
menup = &menu->next;
}
if (*name != NUL)
{
if (menu == NULL)
{
if (!silent)
EMSG2(_(e_nomenu), name);
return FAIL;
}
/* Recalculate modes for menu based on the new updated children */
menu->modes &= ~modes;
#if defined(FEAT_GUI_W32) & defined(FEAT_TEAROFF)
if ((s_tearoffs) && (menu->children != NULL)) /* there's a tear bar.. */
child = menu->children->next; /* don't count tearoff bar */
else
#endif
child = menu->children;
for ( ; child != NULL; child = child->next)
menu->modes |= child->modes;
if (modes & MENU_TIP_MODE)
{
free_menu_string(menu, MENU_INDEX_TIP);
#if defined(FEAT_TOOLBAR) && !defined(FEAT_GUI_W32) \
&& (defined(FEAT_BEVAL) || defined(FEAT_GUI_GTK))
/* Need to update the menu tip. */
if (gui.in_use)
gui_mch_menu_set_tip(menu);
#endif
}
if ((menu->modes & MENU_ALL_MODES) == 0)
{
/* The menu item is no longer valid in ANY mode, so delete it */
#if defined(FEAT_GUI_W32) & defined(FEAT_TEAROFF)
if (s_tearoffs && menu->children != NULL) /* there's a tear bar.. */