@@ -4546,6 +4546,9 @@ current_search(count, forward)
45464546 int visual_active = FALSE;
45474547 int flags = 0 ;
45484548 pos_T save_VIsual ;
4549+ regmmatch_T regmatch ;
4550+ int nmatched = 0 ;
4551+ int zerowidth = FALSE;
45494552
45504553
45514554 /* wrapping should not occur */
@@ -4580,24 +4583,43 @@ current_search(count, forward)
45804583 else
45814584 orig_pos = pos = start_pos = curwin -> w_cursor ;
45824585
4586+ /*
4587+ * Check for zero-width pattern.
4588+ */
4589+ if (search_regcomp (spats [last_idx ].pat , RE_SEARCH , RE_SEARCH ,
4590+ ((SEARCH_HIS + SEARCH_KEEP )), & regmatch ) == FAIL )
4591+ return FAIL ;
4592+
4593+ /* Zero-width pattern should match somewhere, then we can check if start
4594+ * and end are in the same position. */
4595+ nmatched = vim_regexec_multi (& regmatch , curwin , curbuf ,
4596+ curwin -> w_cursor .lnum , (colnr_T )0 , NULL );
4597+ if (called_emsg )
4598+ return FAIL ;
4599+ if (nmatched && regmatch .startpos [0 ].lnum == regmatch .endpos [0 ].lnum
4600+ && regmatch .endpos [0 ].col == regmatch .startpos [0 ].col )
4601+ zerowidth = TRUE;
4602+ vim_free (regmatch .regprog );
4603+
45834604 /*
45844605 * The trick is to first search backwards and then search forward again,
45854606 * so that a match at the current cursor position will be correctly
45864607 * captured.
45874608 */
45884609 for (i = 0 ; i < 2 ; i ++ )
45894610 {
4590- if (i && count == 1 )
4591- flags = SEARCH_START ;
4592-
45934611 if (forward )
45944612 dir = i ;
45954613 else
45964614 dir = !i ;
4615+
4616+ flags = 0 ;
4617+ if (!dir && !zerowidth )
4618+ flags = SEARCH_END ;
4619+
45974620 result = searchit (curwin , curbuf , & pos , (dir ? FORWARD : BACKWARD ),
45984621 spats [last_idx ].pat , (long ) (i ? count : 1 ),
4599- SEARCH_KEEP | flags | (dir ? 0 : SEARCH_END ),
4600- RE_SEARCH , 0 , NULL );
4622+ SEARCH_KEEP | flags , RE_SEARCH , 0 , NULL );
46014623
46024624 /* First search may fail, but then start searching from the
46034625 * beginning of the file (cursor might be on the search match)
@@ -4629,10 +4651,12 @@ current_search(count, forward)
46294651 }
46304652
46314653 start_pos = pos ;
4632- flags = ( forward ? SEARCH_END : 0 ) ;
4654+ flags = forward ? SEARCH_END : 0 ;
46334655
4634- /* move to match */
4635- result = searchit (curwin , curbuf , & pos , (forward ? FORWARD : BACKWARD ),
4656+ /* move to match, except for zero-width matches, in which case, we are
4657+ * already on the next match */
4658+ if (!zerowidth )
4659+ result = searchit (curwin , curbuf , & pos , (forward ? FORWARD : BACKWARD ),
46364660 spats [last_idx ].pat , 0L , flags | SEARCH_KEEP , RE_SEARCH , 0 , NULL );
46374661
46384662 if (!VIsual_active )
0 commit comments