@@ -1179,6 +1179,9 @@ search_range(pos, endpos, search_type, matches, maxlines, plinepos, pendpos)
11791179
11801180 linenum = find_linenum (pos );
11811181 oldpos = pos ;
1182+ /* When the search wraps around, end at starting position. */
1183+ if ((search_type & SRCH_WRAP_AROUND ) && endpos == NULL_POSITION )
1184+ endpos = pos ;
11821185 for (;;)
11831186 {
11841187 /*
@@ -1194,7 +1197,9 @@ search_range(pos, endpos, search_type, matches, maxlines, plinepos, pendpos)
11941197 return (-1 );
11951198 }
11961199
1197- if ((endpos != NULL_POSITION && pos >= endpos ) || maxlines == 0 )
1200+ if ((endpos != NULL_POSITION && !(search_type & SRCH_WRAP_AROUND ) &&
1201+ (((search_type & SRCH_FORW ) && pos >= endpos ) ||
1202+ ((search_type & SRCH_BACK ) && pos <= endpos ))) || maxlines == 0 )
11981203 {
11991204 /*
12001205 * Reached end position without a match.
@@ -1233,6 +1238,35 @@ search_range(pos, endpos, search_type, matches, maxlines, plinepos, pendpos)
12331238 /*
12341239 * Reached EOF/BOF without a match.
12351240 */
1241+ if (search_type & SRCH_WRAP_AROUND )
1242+ {
1243+ /*
1244+ * The search wraps around the current file, so
1245+ * try to continue at BOF/EOF.
1246+ */
1247+ if (search_type & SRCH_FORW )
1248+ {
1249+ pos = ch_zero ();
1250+ } else
1251+ {
1252+ pos = ch_length ();
1253+ if (pos == NULL_POSITION )
1254+ {
1255+ (void ) ch_end_seek ();
1256+ pos = ch_length ();
1257+ }
1258+ }
1259+ if (pos != NULL_POSITION ) {
1260+ /*
1261+ * Wrap-around was successful. Clear
1262+ * the flag so we don't wrap again, and
1263+ * continue the search at new pos.
1264+ */
1265+ search_type &= ~SRCH_WRAP_AROUND ;
1266+ linenum = find_linenum (pos );
1267+ continue ;
1268+ }
1269+ }
12361270 if (pendpos != NULL )
12371271 * pendpos = oldpos ;
12381272 return (matches );
0 commit comments