@@ -373,10 +373,7 @@ cmdline_pum_create(
373373 columns += vim_strsize (showmatches_gettail (matches [0 ]));
374374 columns -= vim_strsize (matches [0 ]);
375375 }
376- if (columns >= compl_startcol )
377- compl_startcol = 0 ;
378- else
379- compl_startcol -= columns ;
376+ compl_startcol = MAX (0 , compl_startcol - columns );
380377
381378 // no default selection
382379 compl_selected = -1 ;
@@ -735,94 +732,84 @@ win_redr_status_matches(
735732 * in "xp->xp_selected"
736733 */
737734 static char_u *
738- get_next_or_prev_match (
739- int mode ,
740- expand_T * xp )
735+ get_next_or_prev_match (int mode , expand_T * xp )
741736{
742- int findex = xp -> xp_selected ;
743- int ht ;
737+ int findex = xp -> xp_selected ;
738+ int ht ;
744739
740+ // When no files found, return NULL
745741 if (xp -> xp_numfiles <= 0 )
746742 return NULL ;
747743
748744 if (mode == WILD_PREV )
749745 {
746+ // Select last file if at start
750747 if (findex == -1 )
751748 findex = xp -> xp_numfiles ;
752749 -- findex ;
753750 }
754751 else if (mode == WILD_NEXT )
755- ++ findex ;
756- else if (mode == WILD_PAGEUP )
757752 {
758- if (findex == 0 )
759- // at the first entry, don't select any entries
760- findex = -1 ;
761- else if (findex == -1 )
762- // no entry is selected. select the last entry
763- findex = xp -> xp_numfiles - 1 ;
764- else
765- {
766- // go up by the pum height
767- ht = pum_get_height ();
768- if (ht > 3 )
769- ht -= 2 ;
770- findex -= ht ;
771- if (findex < 0 )
772- // few entries left, select the first entry
773- findex = 0 ;
774- }
753+ // Select next file
754+ findex = findex + 1 ;
775755 }
776- else // mode == WILD_PAGEDOWN
756+ else // WILD_PAGEDOWN or WILD_PAGEUP
777757 {
778- if (findex == xp -> xp_numfiles - 1 )
779- // at the last entry, don't select any entries
780- findex = -1 ;
781- else if (findex == -1 )
782- // no entry is selected. select the first entry
783- findex = 0 ;
784- else
758+ // Get the height of popup menu (used for both PAGEUP and PAGEDOWN)
759+ ht = pum_get_height ();
760+ if (ht > 3 )
761+ ht -= 2 ;
762+
763+ if (mode == WILD_PAGEUP )
785764 {
786- // go down by the pum height
787- ht = pum_get_height ();
788- if (ht > 3 )
789- ht -= 2 ;
790- findex += ht ;
791- if (findex >= xp -> xp_numfiles )
792- // few entries left, select the last entry
765+ if (findex == 0 )
766+ // at the first entry, don't select any entries
767+ findex = -1 ;
768+ else if (findex == -1 )
769+ // no entry is selected. select the last entry
793770 findex = xp -> xp_numfiles - 1 ;
771+ else
772+ // go up by the pum height
773+ findex = MAX (findex - ht , 0 );
774+ }
775+ else // mode == WILD_PAGEDOWN
776+ {
777+ if (findex < 0 )
778+ // no entry is selected, select the first entry
779+ findex = 0 ;
780+ else if (findex >= xp -> xp_numfiles - 1 )
781+ // at the last entry, don't select any entries
782+ findex = -1 ;
783+ else
784+ // go down by the pum height
785+ findex = MIN (findex + ht , xp -> xp_numfiles - 1 );
794786 }
795787 }
796788
797- // When wrapping around, return the original string, set findex to -1.
798- if (findex < 0 )
789+ // Handle wrapping around
790+ if (findex < 0 || findex >= xp -> xp_numfiles )
799791 {
800- if (xp -> xp_orig == NULL )
801- findex = xp -> xp_numfiles - 1 ;
802- else
792+ // If original string exists, return to it when wrapping around
793+ if (xp -> xp_orig != NULL )
803794 findex = -1 ;
804- }
805- if (findex >= xp -> xp_numfiles )
806- {
807- if (xp -> xp_orig == NULL )
808- findex = 0 ;
809795 else
810- findex = -1 ;
796+ // Wrap around to opposite end
797+ findex = (findex < 0 ) ? xp -> xp_numfiles - 1 : 0 ;
811798 }
799+
800+ // Display matches on screen
812801 if (compl_match_array )
813802 {
814803 compl_selected = findex ;
815804 cmdline_pum_display ();
816805 }
817806 else if (p_wmnu )
818- win_redr_status_matches (xp , xp -> xp_numfiles , xp -> xp_files ,
819- findex , cmd_showtail );
820- xp -> xp_selected = findex ;
821-
822- if (findex == -1 )
823- return vim_strsave (xp -> xp_orig );
807+ win_redr_status_matches (xp , xp -> xp_numfiles , xp -> xp_files , findex ,
808+ cmd_showtail );
824809
825- return vim_strsave (xp -> xp_files [findex ]);
810+ xp -> xp_selected = findex ;
811+ // Return the original string or the selected match
812+ return vim_strsave (findex == -1 ? xp -> xp_orig : xp -> xp_files [findex ]);
826813}
827814
828815/*
0 commit comments