2 * Copyright (C) 1984-2007 Mark Nudelman
4 * You may distribute under the terms of either the GNU General Public
5 * License or the Less License, as specified in the README file.
7 * For more information about less, or for information on how to
8 * contact the author, see the README file.
13 * Routines to search a file for a pattern.
20 #define MINPOS(a,b) (((a) < (b)) ? (a) : (b))
21 #define MAXPOS(a,b) (((a) > (b)) ? (a) : (b))
23 #if HAVE_POSIX_REGCOMP
26 #define REGCOMP_FLAG REG_EXTENDED
28 #define REGCOMP_FLAG 0
50 extern int how_search
;
54 extern int jump_sline
;
57 extern int status_col
;
58 extern void * constant ml_search
;
59 extern POSITION start_attnpos
;
60 extern POSITION end_attnpos
;
62 extern int hilite_search
;
63 extern int screen_trashed
;
64 extern int size_linebuf
;
66 extern int can_goto_line
;
68 static int hide_hilite
;
70 static POSITION prep_startpos
;
71 static POSITION prep_endpos
;
75 struct hilite
*hl_next
;
79 static struct hilite hilite_anchor
= { NULL
, NULL_POSITION
, NULL_POSITION
};
80 #define hl_first hl_next
84 * These are the static variables that represent the "remembered"
87 #if HAVE_POSIX_REGCOMP
88 static regex_t
*regpattern
= NULL
;
91 pcre
*regpattern
= NULL
;
97 static char *cpattern
= NULL
;
100 static struct regexp
*regpattern
= NULL
;
103 static int is_caseless
;
104 static int is_ucase_pattern
;
105 static int last_search_type
;
106 static char *last_pattern
= NULL
;
108 #define CVT_TO_LC 01 /* Convert upper-case to lower-case */
109 #define CVT_BS 02 /* Do backspace processing */
110 #define CVT_CRLF 04 /* Remove CR after LF */
111 #define CVT_ANSI 010 /* Remove ANSI escape sequences */
114 * Get the length of a buffer needed to convert a string.
123 * Just copying a string in UTF-8 mode can cause it to grow
125 * Six output bytes for one input byte is the worst case
126 * (and unfortunately is far more than is needed in any
127 * non-pathological situation, so this is very wasteful).
134 * Convert text. Perform one or more of these transformations:
137 cvt_text(odst
, osrc
, lenp
, ops
)
145 register char *src_end
;
149 src_end
= osrc
+ *lenp
;
151 src_end
= osrc
+ strlen(osrc
);
153 for (src
= osrc
, dst
= odst
; src
< src_end
; )
155 ch
= step_char(&src
, +1, src_end
);
156 if ((ops
& CVT_TO_LC
) && IS_UPPER(ch
))
158 /* Convert uppercase to lowercase. */
159 put_wchar(&dst
, TO_LOWER(ch
));
160 } else if ((ops
& CVT_BS
) && ch
== '\b' && dst
> odst
)
162 /* Delete backspace and preceding char. */
165 } while (dst
> odst
&&
166 !IS_ASCII_OCTET(*dst
) && !IS_UTF8_LEAD(*dst
));
167 } else if ((ops
& CVT_ANSI
) && IS_CSI_START(ch
))
169 /* Skip to end of ANSI escape sequence. */
170 src
++; /* skip the CSI start char */
171 while (src
< src_end
)
172 if (!is_ansi_middle(*src
++))
178 if ((ops
& CVT_CRLF
) && dst
> odst
&& dst
[-1] == '\r')
186 * Determine which conversions to perform.
192 if (is_caseless
|| bs_mode
== BS_SPECIAL
)
196 if (bs_mode
== BS_SPECIAL
)
198 if (bs_mode
!= BS_CONTROL
)
200 } else if (bs_mode
!= BS_CONTROL
)
204 if (ctldisp
== OPT_ONPLUS
)
210 * Are there any uppercase letters in this string?
216 char *str_end
= str
+ strlen(str
);
219 while (str
< str_end
)
221 ch
= step_char(&str
, +1, str_end
);
229 * Is there a previous (remembered) search pattern?
234 if (last_search_type
& SRCH_NO_REGEX
)
235 return (last_pattern
!= NULL
);
236 #if HAVE_POSIX_REGCOMP
237 return (regpattern
!= NULL
);
240 return (regpattern
!= NULL
);
243 return (re_pattern
!= 0);
246 return (cpattern
!= NULL
);
249 return (regpattern
!= NULL
);
252 return (last_pattern
!= NULL
);
258 * Repaint the hilites currently displayed on the screen.
259 * Repaint each line which contains highlighted text.
260 * If on==0, force all hilites off.
269 int save_hide_hilite
;
274 save_hide_hilite
= hide_hilite
;
285 hide_hilite
= save_hide_hilite
;
289 for (slinenum
= TOP
; slinenum
< TOP
+ sc_height
-1; slinenum
++)
291 pos
= position(slinenum
);
292 if (pos
== NULL_POSITION
)
294 epos
= position(slinenum
+1);
297 * If any character in the line is highlighted,
300 * {{ This doesn't work -- if line is drawn with highlights
301 * which should be erased (e.g. toggle -i with status column),
302 * we must redraw the line even if it has no highlights.
303 * For now, just repaint every line. }}
305 if (is_hilited(pos
, epos
, 1, NULL
))
308 (void) forw_line(pos
);
315 hide_hilite
= save_hide_hilite
;
319 * Clear the attn hilite.
325 POSITION old_start_attnpos
;
326 POSITION old_end_attnpos
;
331 if (start_attnpos
== NULL_POSITION
)
333 old_start_attnpos
= start_attnpos
;
334 old_end_attnpos
= end_attnpos
;
335 start_attnpos
= end_attnpos
= NULL_POSITION
;
345 for (slinenum
= TOP
; slinenum
< TOP
+ sc_height
-1; slinenum
++)
347 pos
= position(slinenum
);
348 if (pos
== NULL_POSITION
)
350 epos
= position(slinenum
+1);
351 if (pos
< old_end_attnpos
&&
352 (epos
== NULL_POSITION
|| epos
> old_start_attnpos
))
354 (void) forw_line(pos
);
366 * Hide search string highlighting.
373 error("No previous regular expression", NULL_PARG
);
377 hide_hilite
= !hide_hilite
;
383 * Compile a search pattern, for future use by match_pattern.
386 compile_pattern2(pattern
, search_type
)
390 if ((search_type
& SRCH_NO_REGEX
) == 0)
392 #if HAVE_POSIX_REGCOMP
393 regex_t
*s
= (regex_t
*) ecalloc(1, sizeof(regex_t
));
394 if (regcomp(s
, pattern
, REGCOMP_FLAG
))
397 error("Invalid pattern", NULL_PARG
);
400 if (regpattern
!= NULL
)
406 const char *errstring
;
409 comp
= pcre_compile(pattern
, 0,
410 &errstring
, &erroffset
, NULL
);
413 parg
.p_string
= (char *) errstring
;
421 if ((parg
.p_string
= re_comp(pattern
)) != NULL
)
430 if ((s
= regcmp(pattern
, 0)) == NULL
)
432 error("Invalid pattern", NULL_PARG
);
435 if (cpattern
!= NULL
)
441 if ((s
= regcomp(pattern
)) == NULL
)
444 * regcomp has already printed an error message
449 if (regpattern
!= NULL
)
455 if (last_pattern
!= NULL
)
457 last_pattern
= (char *) calloc(1, strlen(pattern
)+1);
458 if (last_pattern
!= NULL
)
459 strcpy(last_pattern
, pattern
);
461 last_search_type
= search_type
;
466 * Like compile_pattern, but convert the pattern to lowercase if necessary.
469 compile_pattern(pattern
, search_type
)
476 if (caseless
!= OPT_ONPLUS
)
477 cvt_pattern
= pattern
;
480 cvt_pattern
= (char*) ecalloc(1, cvt_length(strlen(pattern
), CVT_TO_LC
));
481 cvt_text(cvt_pattern
, pattern
, (int *)NULL
, CVT_TO_LC
);
483 result
= compile_pattern2(cvt_pattern
, search_type
);
484 if (cvt_pattern
!= pattern
)
490 * Forget that we have a compiled pattern.
495 #if HAVE_POSIX_REGCOMP
496 if (regpattern
!= NULL
)
501 if (regpattern
!= NULL
)
502 pcre_free(regpattern
);
509 if (cpattern
!= NULL
)
514 if (regpattern
!= NULL
)
522 * Perform a pattern match with the previously compiled pattern.
523 * Set sp and ep to the start and end of the matched string.
526 match_pattern(line
, line_len
, sp
, ep
, notbol
)
535 if (last_search_type
& SRCH_NO_REGEX
)
536 return (match(last_pattern
, strlen(last_pattern
), line
, line_len
, sp
, ep
));
538 #if HAVE_POSIX_REGCOMP
541 int flags
= (notbol
) ? REG_NOTBOL
: 0;
542 matched
= !regexec(regpattern
, line
, 1, &rm
, flags
);
546 *sp
= line
+ rm
.rm_so
;
547 *ep
= line
+ rm
.rm_eo
;
556 int flags
= (notbol
) ? PCRE_NOTBOL
: 0;
558 matched
= pcre_exec(regpattern
, NULL
, line
, line_len
,
559 0, flags
, ovector
, 3) >= 0;
562 *sp
= line
+ ovector
[0];
563 *ep
= line
+ ovector
[1];
567 matched
= (re_exec(line
) == 1);
569 * re_exec doesn't seem to provide a way to get the matched string.
574 *ep
= regex(cpattern
, line
);
575 matched
= (*ep
!= NULL
);
582 matched
= regexec2(regpattern
, line
, notbol
);
584 matched
= regexec(regpattern
, line
);
588 *sp
= regpattern
->startp
[0];
589 *ep
= regpattern
->endp
[0];
592 matched
= match(last_pattern
, strlen(last_pattern
), line
, line_len
, sp
, ep
);
599 * Clear the hilite list.
605 struct hilite
*nexthl
;
607 for (hl
= hilite_anchor
.hl_first
; hl
!= NULL
; hl
= nexthl
)
609 nexthl
= hl
->hl_next
;
612 hilite_anchor
.hl_first
= NULL
;
613 prep_startpos
= prep_endpos
= NULL_POSITION
;
617 * Should any characters in a specified range be highlighted?
620 is_hilited_range(pos
, epos
)
627 * Look at each highlight and see if any part of it falls in the range.
629 for (hl
= hilite_anchor
.hl_first
; hl
!= NULL
; hl
= hl
->hl_next
)
631 if (hl
->hl_endpos
> pos
&&
632 (epos
== NULL_POSITION
|| epos
> hl
->hl_startpos
))
639 * Should any characters in a specified range be highlighted?
640 * If nohide is nonzero, don't consider hide_hilite.
643 is_hilited(pos
, epos
, nohide
, p_matches
)
651 if (p_matches
!= NULL
)
655 start_attnpos
!= NULL_POSITION
&&
657 (epos
== NULL_POSITION
|| epos
> start_attnpos
))
659 * The attn line overlaps this range.
663 match
= is_hilited_range(pos
, epos
);
667 if (p_matches
!= NULL
)
669 * Report matches, even if we're hiding highlights.
673 if (hilite_search
== 0)
675 * Not doing highlighting.
679 if (!nohide
&& hide_hilite
)
681 * Highlighting is hidden.
689 * Add a new hilite to a hilite list.
692 add_hilite(anchor
, hl
)
693 struct hilite
*anchor
;
699 * Hilites are sorted in the list; find where new one belongs.
700 * Insert new one after ihl.
702 for (ihl
= anchor
; ihl
->hl_next
!= NULL
; ihl
= ihl
->hl_next
)
704 if (ihl
->hl_next
->hl_startpos
> hl
->hl_startpos
)
709 * Truncate hilite so it doesn't overlap any existing ones
710 * above and below it.
713 hl
->hl_startpos
= MAXPOS(hl
->hl_startpos
, ihl
->hl_endpos
);
714 if (ihl
->hl_next
!= NULL
)
715 hl
->hl_endpos
= MINPOS(hl
->hl_endpos
, ihl
->hl_next
->hl_startpos
);
716 if (hl
->hl_startpos
>= hl
->hl_endpos
)
719 * Hilite was truncated out of existence.
724 hl
->hl_next
= ihl
->hl_next
;
729 * Adjust hl_startpos & hl_endpos to account for processing by cvt_text.
732 adj_hilite(anchor
, linepos
, cvt_ops
)
733 struct hilite
*anchor
;
749 * The line was already scanned and hilites were added (in hilite_line).
750 * But it was assumed that each char position in the line
751 * correponds to one char position in the file.
752 * This may not be true if cvt_text modified the line.
753 * Get the raw line again. Look at each character.
755 (void) forw_raw_line(linepos
, &line
, &line_len
);
756 line_end
= line
+ line_len
;
757 opos
= npos
= linepos
;
758 hl
= anchor
->hl_first
;
763 * See if we need to adjust the current hl_startpos or
764 * hl_endpos. After adjusting startpos[i], move to endpos[i].
765 * After adjusting endpos[i], move to startpos[i+1].
766 * The hilite list must be sorted thus:
767 * startpos[0] < endpos[0] <= startpos[1] < endpos[1] <= etc.
769 if (checkstart
&& hl
->hl_startpos
== opos
)
771 hl
->hl_startpos
= npos
;
773 continue; /* {{ not really necessary }} */
774 } else if (!checkstart
&& hl
->hl_endpos
== opos
)
776 hl
->hl_endpos
= npos
;
779 continue; /* {{ necessary }} */
781 if (line
== line_end
)
784 /* Get the next char from the line. */
786 ch
= step_char(&line
, +1, line_end
);
787 ncwidth
= line
- oline
;
790 /* Figure out how this char was processed by cvt_text. */
791 if ((cvt_ops
& CVT_BS
) && ch
== '\b')
793 /* Skip the backspace and the following char. */
795 ch
= step_char(&line
, +1, line_end
);
796 ncwidth
= line
- oline
;
798 } else if ((cvt_ops
& CVT_TO_LC
) && IS_UPPER(ch
))
800 /* Converted uppercase to lower.
801 * Note that this may have changed the number of bytes
802 * that the character occupies. */
805 put_wchar(&dst
, TO_LOWER(ch
));
807 } else if ((cvt_ops
& CVT_ANSI
) && IS_CSI_START(ch
))
809 /* Skip to end of ANSI escape sequence. */
810 line
++; /* skip the CSI start char */
812 while (line
< line_end
)
815 if (!is_ansi_middle(*line
++))
820 /* Ordinary unprocessed character. */
827 * Make a hilite for each string in a physical line which matches
828 * the current pattern.
829 * sp,ep delimit the first match already found.
832 hilite_line(linepos
, line
, line_len
, sp
, ep
, cvt_ops
)
841 char *line_end
= line
+ line_len
;
843 struct hilite hilites
;
845 if (sp
== NULL
|| ep
== NULL
)
848 * sp and ep delimit the first match in the line.
849 * Mark the corresponding file positions, then
850 * look for further matches and mark them.
851 * {{ This technique, of calling match_pattern on subsequent
852 * substrings of the line, may mark more than is correct
853 * if the pattern starts with "^". This bug is fixed
854 * for those regex functions that accept a notbol parameter
855 * (currently POSIX, PCRE and V8-with-regexec2). }}
859 * Put the hilites into a temporary list until they're adjusted.
861 hilites
.hl_first
= NULL
;
866 * Assume that each char position in the "line"
867 * buffer corresponds to one char position in the file.
868 * This is not quite true; we need to adjust later.
870 hl
= (struct hilite
*) ecalloc(1, sizeof(struct hilite
));
871 hl
->hl_startpos
= linepos
+ (sp
-line
);
872 hl
->hl_endpos
= linepos
+ (ep
-line
);
873 add_hilite(&hilites
, hl
);
876 * If we matched more than zero characters,
877 * move to the first char after the string we matched.
878 * If we matched zero, just move to the next char.
882 else if (searchp
!= line_end
)
884 else /* end of line */
886 } while (match_pattern(searchp
, line_end
- searchp
, &sp
, &ep
, 1));
889 * If there were backspaces in the original line, they
890 * were removed, and hl_startpos/hl_endpos are not correct.
891 * {{ This is very ugly. }}
893 adj_hilite(&hilites
, linepos
, cvt_ops
);
896 * Now put the hilites into the real list.
898 while ((hl
= hilites
.hl_next
) != NULL
)
900 hilites
.hl_next
= hl
->hl_next
;
901 add_hilite(&hilite_anchor
, hl
);
907 * Change the caseless-ness of searches.
908 * Updates the internal search state to reflect a change in the -i flag.
913 if (!is_ucase_pattern
)
915 * Pattern did not have uppercase.
916 * Just set the search caselessness to the global caselessness.
918 is_caseless
= caseless
;
921 * Pattern did have uppercase.
922 * Discard the pattern; we can't change search caselessness now.
929 * Find matching text which is currently on screen and highlight it.
934 struct scrpos scrpos
;
937 if (scrpos
.pos
== NULL_POSITION
)
939 prep_hilite(scrpos
.pos
, position(BOTTOM_PLUS_ONE
), -1);
944 * Change highlighting parameters.
950 * Erase any highlights currently on screen.
955 if (hilite_search
== OPT_ONPLUS
)
957 * Display highlights.
964 * Figure out where to start a search.
967 search_pos(search_type
)
976 * Start at the beginning (or end) of the file.
977 * The empty_screen() case is mainly for
978 * command line initiated searches;
979 * for example, "+/xyz" on the command line.
980 * Also for multi-file (SRCH_PAST_EOF) searches.
982 if (search_type
& SRCH_FORW
)
988 if (pos
== NULL_POSITION
)
990 (void) ch_end_seek();
999 * Search does not include current screen.
1001 if (search_type
& SRCH_FORW
)
1002 linenum
= BOTTOM_PLUS_ONE
;
1005 pos
= position(linenum
);
1009 * Search includes current screen.
1010 * It starts at the jump target (if searching backwards),
1011 * or at the jump target plus one (if forwards).
1013 linenum
= adjsline(jump_sline
);
1014 pos
= position(linenum
);
1015 if (search_type
& SRCH_FORW
)
1017 pos
= forw_raw_line(pos
, (char **)NULL
, (int *)NULL
);
1018 while (pos
== NULL_POSITION
)
1020 if (++linenum
>= sc_height
)
1022 pos
= position(linenum
);
1026 while (pos
== NULL_POSITION
)
1030 pos
= position(linenum
);
1038 * Search a subset of the file, specified by start/end position.
1041 search_range(pos
, endpos
, search_type
, matches
, maxlines
, plinepos
, pendpos
)
1057 POSITION linepos
, oldpos
;
1059 linenum
= find_linenum(pos
);
1064 * Get lines until we find a matching one or until
1065 * we hit end-of-file (or beginning-of-file if we're
1066 * going backwards), or until we hit the end position.
1071 * A signal aborts the search.
1076 if ((endpos
!= NULL_POSITION
&& pos
>= endpos
) || maxlines
== 0)
1079 * Reached end position without a match.
1081 if (pendpos
!= NULL
)
1088 if (search_type
& SRCH_FORW
)
1091 * Read the next line, and save the
1092 * starting position of that line in linepos.
1095 pos
= forw_raw_line(pos
, &line
, &line_len
);
1101 * Read the previous line and save the
1102 * starting position of that line in linepos.
1104 pos
= back_raw_line(pos
, &line
, &line_len
);
1110 if (pos
== NULL_POSITION
)
1113 * Reached EOF/BOF without a match.
1115 if (pendpos
!= NULL
)
1121 * If we're using line numbers, we might as well
1122 * remember the information we have now (the position
1123 * and line number of the current line).
1124 * Don't do it for every line because it slows down
1125 * the search. Remember the line number only if
1126 * we're "far" from the last place we remembered it.
1128 if (linenums
&& abs((int)(pos
- oldpos
)) > 1024)
1129 add_lnum(linenum
, pos
);
1133 * If it's a caseless search, convert the line to lowercase.
1134 * If we're doing backspace processing, delete backspaces.
1136 cvt_ops
= get_cvt_ops();
1137 cline
= calloc(1, cvt_length(line_len
, cvt_ops
));
1138 cvt_text(cline
, line
, &line_len
, cvt_ops
);
1141 * Test the next line to see if we have a match.
1142 * We are successful if we either want a match and got one,
1143 * or if we want a non-match and got one.
1145 line_match
= match_pattern(cline
, line_len
, &sp
, &ep
, 0);
1146 line_match
= (!(search_type
& SRCH_NO_MATCH
) && line_match
) ||
1147 ((search_type
& SRCH_NO_MATCH
) && !line_match
);
1156 if (search_type
& SRCH_FIND_ALL
)
1160 * We are supposed to find all matches in the range.
1161 * Just add the matches in this line to the
1162 * hilite list and keep searching.
1165 hilite_line(linepos
, cline
, line_len
, sp
, ep
, cvt_ops
);
1168 } else if (--matches
<= 0)
1171 * Found the one match we're looking for.
1175 if (hilite_search
== OPT_ON
)
1178 * Clear the hilite list and add only
1179 * the matches in this one line.
1183 hilite_line(linepos
, cline
, line_len
, sp
, ep
, cvt_ops
);
1187 if (plinepos
!= NULL
)
1188 *plinepos
= linepos
;
1195 * search for a pattern in history. If found, compile that pattern.
1198 hist_pattern(search_type
)
1204 set_mlist(ml_search
, 0);
1205 pattern
= cmd_lastpattern();
1206 if (pattern
== NULL
)
1209 if (compile_pattern(pattern
, search_type
) < 0)
1212 is_ucase_pattern
= is_ucase(pattern
);
1213 if (is_ucase_pattern
&& caseless
!= OPT_ONPLUS
)
1216 is_caseless
= caseless
;
1219 if (hilite_search
== OPT_ONPLUS
&& !hide_hilite
)
1224 #else /* CMD_HISTORY */
1226 #endif /* CMD_HISTORY */
1230 * Search for the n-th occurrence of a specified pattern,
1231 * either forward or backward.
1232 * Return the number of matches not yet found in this file
1233 * (that is, n minus the number of matches found).
1234 * Return -1 if the search should be aborted.
1235 * Caller may continue the search in another file
1236 * if less than n matches are found in this file.
1239 search(search_type
, pattern
, n
)
1247 if (pattern
== NULL
|| *pattern
== '\0')
1250 * A null pattern means use the previously compiled pattern.
1252 if (!prev_pattern() && !hist_pattern(search_type
))
1254 error("No previous regular expression", NULL_PARG
);
1257 if ((search_type
& SRCH_NO_REGEX
) !=
1258 (last_search_type
& SRCH_NO_REGEX
))
1260 error("Please re-enter search pattern", NULL_PARG
);
1264 if (hilite_search
== OPT_ON
)
1267 * Erase the highlights currently on screen.
1268 * If the search fails, we'll redisplay them later.
1272 if (hilite_search
== OPT_ONPLUS
&& hide_hilite
)
1275 * Highlight any matches currently on screen,
1276 * before we actually start the search.
1286 * Compile the pattern.
1288 if (compile_pattern(pattern
, search_type
) < 0)
1291 * Ignore case if -I is set OR
1292 * -i is set AND the pattern is all lowercase.
1294 is_ucase_pattern
= is_ucase(pattern
);
1295 if (is_ucase_pattern
&& caseless
!= OPT_ONPLUS
)
1298 is_caseless
= caseless
;
1303 * Erase the highlights currently on screen.
1304 * Also permanently delete them from the hilite list.
1310 if (hilite_search
== OPT_ONPLUS
)
1313 * Highlight any matches currently on screen,
1314 * before we actually start the search.
1322 * Figure out where to start the search.
1324 pos
= search_pos(search_type
);
1325 if (pos
== NULL_POSITION
)
1328 * Can't find anyplace to start searching from.
1330 if (search_type
& SRCH_PAST_EOF
)
1332 /* repaint(); -- why was this here? */
1333 error("Nothing to search", NULL_PARG
);
1337 n
= search_range(pos
, NULL_POSITION
, search_type
, n
, -1,
1338 &pos
, (POSITION
*)NULL
);
1342 * Search was unsuccessful.
1345 if (hilite_search
== OPT_ON
&& n
> 0)
1347 * Redisplay old hilites.
1354 if (!(search_type
& SRCH_NO_MOVE
))
1357 * Go to the matching line.
1359 jump_loc(pos
, jump_sline
);
1363 if (hilite_search
== OPT_ON
)
1365 * Display new hilites in the matching line.
1375 * Prepare hilites in a given range of the file.
1377 * The pair (prep_startpos,prep_endpos) delimits a contiguous region
1378 * of the file that has been "prepared"; that is, scanned for matches for
1379 * the current search pattern, and hilites have been created for such matches.
1380 * If prep_startpos == NULL_POSITION, the prep region is empty.
1381 * If prep_endpos == NULL_POSITION, the prep region extends to EOF.
1382 * prep_hilite asks that the range (spos,epos) be covered by the prep region.
1385 prep_hilite(spos
, epos
, maxlines
)
1390 POSITION nprep_startpos
= prep_startpos
;
1391 POSITION nprep_endpos
= prep_endpos
;
1397 * Search beyond where we're asked to search, so the prep region covers
1398 * more than we need. Do one big search instead of a bunch of small ones.
1400 #define SEARCH_MORE (3*size_linebuf)
1402 if (!prev_pattern())
1406 * If we're limited to a max number of lines, figure out the
1407 * file position we should stop at.
1410 max_epos
= NULL_POSITION
;
1414 for (i
= 0; i
< maxlines
; i
++)
1415 max_epos
= forw_raw_line(max_epos
, (char **)NULL
, (int *)NULL
);
1420 * The range that we need to search (spos,epos); and the range that
1421 * the "prep" region will then cover (nprep_startpos,nprep_endpos).
1424 if (prep_startpos
== NULL_POSITION
||
1425 (epos
!= NULL_POSITION
&& epos
< prep_startpos
) ||
1429 * New range is not contiguous with old prep region.
1430 * Discard the old prep region and start a new one.
1433 if (epos
!= NULL_POSITION
)
1434 epos
+= SEARCH_MORE
;
1435 nprep_startpos
= spos
;
1439 * New range partially or completely overlaps old prep region.
1441 if (epos
== NULL_POSITION
)
1444 * New range goes to end of file.
1447 } else if (epos
> prep_endpos
)
1450 * New range ends after old prep region.
1451 * Extend prep region to end at end of new range.
1453 epos
+= SEARCH_MORE
;
1454 } else /* (epos <= prep_endpos) */
1457 * New range ends within old prep region.
1458 * Truncate search to end at start of old prep region.
1460 epos
= prep_startpos
;
1463 if (spos
< prep_startpos
)
1466 * New range starts before old prep region.
1467 * Extend old prep region backwards to start at
1468 * start of new range.
1470 if (spos
< SEARCH_MORE
)
1473 spos
-= SEARCH_MORE
;
1474 nprep_startpos
= spos
;
1475 } else /* (spos >= prep_startpos) */
1478 * New range starts within or after old prep region.
1479 * Trim search to start at end of old prep region.
1485 if (epos
!= NULL_POSITION
&& max_epos
!= NULL_POSITION
&&
1488 * Don't go past the max position we're allowed.
1492 if (epos
== NULL_POSITION
|| epos
> spos
)
1494 result
= search_range(spos
, epos
, SRCH_FORW
|SRCH_FIND_ALL
, 0,
1495 maxlines
, (POSITION
*)NULL
, &new_epos
);
1498 if (prep_endpos
== NULL_POSITION
|| new_epos
> prep_endpos
)
1499 nprep_endpos
= new_epos
;
1501 prep_startpos
= nprep_startpos
;
1502 prep_endpos
= nprep_endpos
;
1507 * Simple pattern matching function.
1508 * It supports no metacharacters like *, etc.
1511 match(pattern
, pattern_len
, buf
, buf_len
, pfound
, pend
)
1516 char **pfound
, **pend
;
1518 register char *pp
, *lp
;
1519 register char *pattern_end
= pattern
+ pattern_len
;
1520 register char *buf_end
= buf
+ buf_len
;
1522 for ( ; buf
< buf_end
; buf
++)
1524 for (pp
= pattern
, lp
= buf
; *pp
== *lp
; pp
++, lp
++)
1525 if (pp
== pattern_end
|| lp
== buf_end
)
1527 if (pp
== pattern_end
)
1541 * This function is called by the V8 regcomp to report
1542 * errors in regular expressions.