@@ -365,6 +365,56 @@ static void print_highlight_menu_stuff(struct menu_stuff *stuff, int **chosen)
365365 string_list_clear (& menu_list , 0 );
366366}
367367
368+ static int find_unique (const char * choice , struct menu_stuff * menu_stuff )
369+ {
370+ struct menu_item * menu_item ;
371+ struct string_list_item * string_list_item ;
372+ int i , len , found = 0 ;
373+
374+ len = strlen (choice );
375+ switch (menu_stuff -> type ) {
376+ default :
377+ die ("Bad type of menu_stuff when parse choice" );
378+ case MENU_STUFF_TYPE_MENU_ITEM :
379+
380+ menu_item = (struct menu_item * )menu_stuff -> stuff ;
381+ for (i = 0 ; i < menu_stuff -> nr ; i ++ , menu_item ++ ) {
382+ if (len == 1 && * choice == menu_item -> hotkey ) {
383+ found = i + 1 ;
384+ break ;
385+ }
386+ if (!strncasecmp (choice , menu_item -> title , len )) {
387+ if (found ) {
388+ if (len == 1 ) {
389+ /* continue for hotkey matching */
390+ found = -1 ;
391+ } else {
392+ found = 0 ;
393+ break ;
394+ }
395+ } else {
396+ found = i + 1 ;
397+ }
398+ }
399+ }
400+ break ;
401+ case MENU_STUFF_TYPE_STRING_LIST :
402+ string_list_item = ((struct string_list * )menu_stuff -> stuff )-> items ;
403+ for (i = 0 ; i < menu_stuff -> nr ; i ++ , string_list_item ++ ) {
404+ if (!strncasecmp (choice , string_list_item -> string , len )) {
405+ if (found ) {
406+ found = 0 ;
407+ break ;
408+ }
409+ found = i + 1 ;
410+ }
411+ }
412+ break ;
413+ }
414+ return found ;
415+ }
416+
417+
368418/*
369419 * Parse user input, and return choice(s) for menu (menu_stuff).
370420 *
@@ -392,8 +442,6 @@ static int parse_choice(struct menu_stuff *menu_stuff,
392442 int * * chosen )
393443{
394444 struct strbuf * * choice_list , * * ptr ;
395- struct menu_item * menu_item ;
396- struct string_list_item * string_list_item ;
397445 int nr = 0 ;
398446 int i ;
399447
@@ -457,32 +505,8 @@ static int parse_choice(struct menu_stuff *menu_stuff,
457505 bottom = 1 ;
458506 top = menu_stuff -> nr ;
459507 } else {
460- switch (menu_stuff -> type ) {
461- default :
462- die ("Bad type of menu_stuff when parse choice" );
463- case MENU_STUFF_TYPE_MENU_ITEM :
464- menu_item = (struct menu_item * )menu_stuff -> stuff ;
465- for (i = 0 ; i < menu_stuff -> nr ; i ++ , menu_item ++ ) {
466- if (((* ptr )-> len == 1 &&
467- * (* ptr )-> buf == menu_item -> hotkey ) ||
468- !strcasecmp ((* ptr )-> buf , menu_item -> title )) {
469- bottom = i + 1 ;
470- top = bottom ;
471- break ;
472- }
473- }
474- break ;
475- case MENU_STUFF_TYPE_STRING_LIST :
476- string_list_item = ((struct string_list * )menu_stuff -> stuff )-> items ;
477- for (i = 0 ; i < menu_stuff -> nr ; i ++ , string_list_item ++ ) {
478- if (!strcasecmp ((* ptr )-> buf , string_list_item -> string )) {
479- bottom = i + 1 ;
480- top = bottom ;
481- break ;
482- }
483- }
484- break ;
485- }
508+ bottom = find_unique ((* ptr )-> buf , menu_stuff );
509+ top = bottom ;
486510 }
487511
488512 if (top <= 0 || bottom <= 0 || top > menu_stuff -> nr || bottom > top ||
0 commit comments