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 while (src
+ 1 != src_end
)
171 if (!is_ansi_middle(*++src
))
177 if ((ops
& CVT_CRLF
) && dst
> odst
&& dst
[-1] == '\r')
185 * Determine which conversions to perform.
191 if (is_caseless
|| bs_mode
== BS_SPECIAL
)
195 if (bs_mode
== BS_SPECIAL
)
197 if (bs_mode
!= BS_CONTROL
)
199 } else if (bs_mode
!= BS_CONTROL
)
203 if (ctldisp
== OPT_ONPLUS
)
209 * Are there any uppercase letters in this string?
215 char *str_end
= str
+ strlen(str
);
218 while (str
< str_end
)
220 ch
= step_char(&str
, +1, str_end
);
228 * Is there a previous (remembered) search pattern?
233 if (last_search_type
& SRCH_NO_REGEX
)
234 return (last_pattern
!= NULL
);
235 #if HAVE_POSIX_REGCOMP
236 return (regpattern
!= NULL
);
239 return (regpattern
!= NULL
);
242 return (re_pattern
!= 0);
245 return (cpattern
!= NULL
);
248 return (regpattern
!= NULL
);
251 return (last_pattern
!= NULL
);
257 * Repaint the hilites currently displayed on the screen.
258 * Repaint each line which contains highlighted text.
259 * If on==0, force all hilites off.
268 int save_hide_hilite
;
273 save_hide_hilite
= hide_hilite
;
284 hide_hilite
= save_hide_hilite
;
288 for (slinenum
= TOP
; slinenum
< TOP
+ sc_height
-1; slinenum
++)
290 pos
= position(slinenum
);
291 if (pos
== NULL_POSITION
)
293 epos
= position(slinenum
+1);
296 * If any character in the line is highlighted,
299 * {{ This doesn't work -- if line is drawn with highlights
300 * which should be erased (e.g. toggle -i with status column),
301 * we must redraw the line even if it has no highlights.
302 * For now, just repaint every line. }}
304 if (is_hilited(pos
, epos
, 1, NULL
))
307 (void) forw_line(pos
);
314 hide_hilite
= save_hide_hilite
;
318 * Clear the attn hilite.
324 POSITION old_start_attnpos
;
325 POSITION old_end_attnpos
;
330 if (start_attnpos
== NULL_POSITION
)
332 old_start_attnpos
= start_attnpos
;
333 old_end_attnpos
= end_attnpos
;
334 start_attnpos
= end_attnpos
= NULL_POSITION
;
344 for (slinenum
= TOP
; slinenum
< TOP
+ sc_height
-1; slinenum
++)
346 pos
= position(slinenum
);
347 if (pos
== NULL_POSITION
)
349 epos
= position(slinenum
+1);
350 if (pos
< old_end_attnpos
&&
351 (epos
== NULL_POSITION
|| epos
> old_start_attnpos
))
353 (void) forw_line(pos
);
365 * Hide search string highlighting.
372 error("No previous regular expression", NULL_PARG
);
376 hide_hilite
= !hide_hilite
;
382 * Compile a search pattern, for future use by match_pattern.
385 compile_pattern2(pattern
, search_type
)
389 if ((search_type
& SRCH_NO_REGEX
) == 0)
391 #if HAVE_POSIX_REGCOMP
392 regex_t
*s
= (regex_t
*) ecalloc(1, sizeof(regex_t
));
393 if (regcomp(s
, pattern
, REGCOMP_FLAG
))
396 error("Invalid pattern", NULL_PARG
);
399 if (regpattern
!= NULL
)
405 const char *errstring
;
408 comp
= pcre_compile(pattern
, 0,
409 &errstring
, &erroffset
, NULL
);
412 parg
.p_string
= (char *) errstring
;
420 if ((parg
.p_string
= re_comp(pattern
)) != NULL
)
429 if ((s
= regcmp(pattern
, 0)) == NULL
)
431 error("Invalid pattern", NULL_PARG
);
434 if (cpattern
!= NULL
)
440 if ((s
= regcomp(pattern
)) == NULL
)
443 * regcomp has already printed an error message
448 if (regpattern
!= NULL
)
454 if (last_pattern
!= NULL
)
456 last_pattern
= (char *) calloc(1, strlen(pattern
)+1);
457 if (last_pattern
!= NULL
)
458 strcpy(last_pattern
, pattern
);
460 last_search_type
= search_type
;
465 * Like compile_pattern, but convert the pattern to lowercase if necessary.
468 compile_pattern(pattern
, search_type
)
475 if (caseless
!= OPT_ONPLUS
)
476 cvt_pattern
= pattern
;
479 cvt_pattern
= (char*) ecalloc(1, cvt_length(strlen(pattern
), CVT_TO_LC
));
480 cvt_text(cvt_pattern
, pattern
, (int *)NULL
, CVT_TO_LC
);
482 result
= compile_pattern2(cvt_pattern
, search_type
);
483 if (cvt_pattern
!= pattern
)
489 * Forget that we have a compiled pattern.
494 #if HAVE_POSIX_REGCOMP
495 if (regpattern
!= NULL
)
500 if (regpattern
!= NULL
)
501 pcre_free(regpattern
);
508 if (cpattern
!= NULL
)
513 if (regpattern
!= NULL
)
521 * Perform a pattern match with the previously compiled pattern.
522 * Set sp and ep to the start and end of the matched string.
525 match_pattern(line
, line_len
, sp
, ep
, notbol
)
534 if (last_search_type
& SRCH_NO_REGEX
)
535 return (match(last_pattern
, strlen(last_pattern
), line
, line_len
, sp
, ep
));
537 #if HAVE_POSIX_REGCOMP
540 int flags
= (notbol
) ? REG_NOTBOL
: 0;
541 matched
= !regexec(regpattern
, line
, 1, &rm
, flags
);
545 *sp
= line
+ rm
.rm_so
;
546 *ep
= line
+ rm
.rm_eo
;
555 int flags
= (notbol
) ? PCRE_NOTBOL
: 0;
557 matched
= pcre_exec(regpattern
, NULL
, line
, line_len
,
558 0, flags
, ovector
, 3) >= 0;
561 *sp
= line
+ ovector
[0];
562 *ep
= line
+ ovector
[1];
566 matched
= (re_exec(line
) == 1);
568 * re_exec doesn't seem to provide a way to get the matched string.
573 *ep
= regex(cpattern
, line
);
574 matched
= (*ep
!= NULL
);
581 matched
= regexec2(regpattern
, line
, notbol
);
583 matched
= regexec(regpattern
, line
);
587 *sp
= regpattern
->startp
[0];
588 *ep
= regpattern
->endp
[0];
591 matched
= match(last_pattern
, strlen(last_pattern
), line
, line_len
, sp
, ep
);
598 * Clear the hilite list.
604 struct hilite
*nexthl
;
606 for (hl
= hilite_anchor
.hl_first
; hl
!= NULL
; hl
= nexthl
)
608 nexthl
= hl
->hl_next
;
611 hilite_anchor
.hl_first
= NULL
;
612 prep_startpos
= prep_endpos
= NULL_POSITION
;
616 * Should any characters in a specified range be highlighted?
619 is_hilited_range(pos
, epos
)
626 * Look at each highlight and see if any part of it falls in the range.
628 for (hl
= hilite_anchor
.hl_first
; hl
!= NULL
; hl
= hl
->hl_next
)
630 if (hl
->hl_endpos
> pos
&&
631 (epos
== NULL_POSITION
|| epos
> hl
->hl_startpos
))
638 * Should any characters in a specified range be highlighted?
639 * If nohide is nonzero, don't consider hide_hilite.
642 is_hilited(pos
, epos
, nohide
, p_matches
)
650 if (p_matches
!= NULL
)
654 start_attnpos
!= NULL_POSITION
&&
656 (epos
== NULL_POSITION
|| epos
> start_attnpos
))
658 * The attn line overlaps this range.
662 match
= is_hilited_range(pos
, epos
);
666 if (p_matches
!= NULL
)
668 * Report matches, even if we're hiding highlights.
672 if (hilite_search
== 0)
674 * Not doing highlighting.
678 if (!nohide
&& hide_hilite
)
680 * Highlighting is hidden.
688 * Add a new hilite to a hilite list.
691 add_hilite(anchor
, hl
)
692 struct hilite
*anchor
;
698 * Hilites are sorted in the list; find where new one belongs.
699 * Insert new one after ihl.
701 for (ihl
= anchor
; ihl
->hl_next
!= NULL
; ihl
= ihl
->hl_next
)
703 if (ihl
->hl_next
->hl_startpos
> hl
->hl_startpos
)
708 * Truncate hilite so it doesn't overlap any existing ones
709 * above and below it.
712 hl
->hl_startpos
= MAXPOS(hl
->hl_startpos
, ihl
->hl_endpos
);
713 if (ihl
->hl_next
!= NULL
)
714 hl
->hl_endpos
= MINPOS(hl
->hl_endpos
, ihl
->hl_next
->hl_startpos
);
715 if (hl
->hl_startpos
>= hl
->hl_endpos
)
718 * Hilite was truncated out of existence.
723 hl
->hl_next
= ihl
->hl_next
;
728 * Adjust hl_startpos & hl_endpos to account for processing by cvt_text.
731 adj_hilite(anchor
, linepos
, cvt_ops
)
732 struct hilite
*anchor
;
748 * The line was already scanned and hilites were added (in hilite_line).
749 * But it was assumed that each char position in the line
750 * correponds to one char position in the file.
751 * This may not be true if cvt_text modified the line.
752 * Get the raw line again. Look at each character.
754 (void) forw_raw_line(linepos
, &line
, &line_len
);
755 line_end
= line
+ line_len
;
756 opos
= npos
= linepos
;
757 hl
= anchor
->hl_first
;
762 * See if we need to adjust the current hl_startpos or
763 * hl_endpos. After adjusting startpos[i], move to endpos[i].
764 * After adjusting endpos[i], move to startpos[i+1].
765 * The hilite list must be sorted thus:
766 * startpos[0] < endpos[0] <= startpos[1] < endpos[1] <= etc.
768 if (checkstart
&& hl
->hl_startpos
== opos
)
770 hl
->hl_startpos
= npos
;
772 continue; /* {{ not really necessary }} */
773 } else if (!checkstart
&& hl
->hl_endpos
== opos
)
775 hl
->hl_endpos
= npos
;
778 continue; /* {{ necessary }} */
780 if (line
== line_end
)
783 /* Get the next char from the line. */
785 ch
= step_char(&line
, +1, line_end
);
786 ncwidth
= line
- oline
;
789 /* Figure out how this char was processed by cvt_text. */
790 if ((cvt_ops
& CVT_BS
) && ch
== '\b')
792 /* Skip the backspace and the following char. */
794 ch
= step_char(&line
, +1, line_end
);
795 ncwidth
= line
- oline
;
797 } else if ((cvt_ops
& CVT_TO_LC
) && IS_UPPER(ch
))
799 /* Converted uppercase to lower.
800 * Note that this may have changed the number of bytes
801 * that the character occupies. */
804 put_wchar(&dst
, TO_LOWER(ch
));
806 } else if ((cvt_ops
& CVT_ANSI
) && IS_CSI_START(ch
))
808 /* Skip to end of ANSI escape sequence. */
809 while (line
< line_end
)
812 if (!is_ansi_middle(*++line
))
817 /* Ordinary unprocessed character. */
824 * Make a hilite for each string in a physical line which matches
825 * the current pattern.
826 * sp,ep delimit the first match already found.
829 hilite_line(linepos
, line
, line_len
, sp
, ep
, cvt_ops
)
838 char *line_end
= line
+ line_len
;
840 struct hilite hilites
;
842 if (sp
== NULL
|| ep
== NULL
)
845 * sp and ep delimit the first match in the line.
846 * Mark the corresponding file positions, then
847 * look for further matches and mark them.
848 * {{ This technique, of calling match_pattern on subsequent
849 * substrings of the line, may mark more than is correct
850 * if the pattern starts with "^". This bug is fixed
851 * for those regex functions that accept a notbol parameter
852 * (currently POSIX, PCRE and V8-with-regexec2). }}
856 * Put the hilites into a temporary list until they're adjusted.
858 hilites
.hl_first
= NULL
;
863 * Assume that each char position in the "line"
864 * buffer corresponds to one char position in the file.
865 * This is not quite true; we need to adjust later.
867 hl
= (struct hilite
*) ecalloc(1, sizeof(struct hilite
));
868 hl
->hl_startpos
= linepos
+ (sp
-line
);
869 hl
->hl_endpos
= linepos
+ (ep
-line
);
870 add_hilite(&hilites
, hl
);
873 * If we matched more than zero characters,
874 * move to the first char after the string we matched.
875 * If we matched zero, just move to the next char.
879 else if (searchp
!= line_end
)
881 else /* end of line */
883 } while (match_pattern(searchp
, line_end
- searchp
, &sp
, &ep
, 1));
886 * If there were backspaces in the original line, they
887 * were removed, and hl_startpos/hl_endpos are not correct.
888 * {{ This is very ugly. }}
890 adj_hilite(&hilites
, linepos
, cvt_ops
);
893 * Now put the hilites into the real list.
895 while ((hl
= hilites
.hl_next
) != NULL
)
897 hilites
.hl_next
= hl
->hl_next
;
898 add_hilite(&hilite_anchor
, hl
);
904 * Change the caseless-ness of searches.
905 * Updates the internal search state to reflect a change in the -i flag.
910 if (!is_ucase_pattern
)
912 * Pattern did not have uppercase.
913 * Just set the search caselessness to the global caselessness.
915 is_caseless
= caseless
;
918 * Pattern did have uppercase.
919 * Discard the pattern; we can't change search caselessness now.
926 * Find matching text which is currently on screen and highlight it.
931 struct scrpos scrpos
;
934 if (scrpos
.pos
== NULL_POSITION
)
936 prep_hilite(scrpos
.pos
, position(BOTTOM_PLUS_ONE
), -1);
941 * Change highlighting parameters.
947 * Erase any highlights currently on screen.
952 if (hilite_search
== OPT_ONPLUS
)
954 * Display highlights.
961 * Figure out where to start a search.
964 search_pos(search_type
)
973 * Start at the beginning (or end) of the file.
974 * The empty_screen() case is mainly for
975 * command line initiated searches;
976 * for example, "+/xyz" on the command line.
977 * Also for multi-file (SRCH_PAST_EOF) searches.
979 if (search_type
& SRCH_FORW
)
985 if (pos
== NULL_POSITION
)
987 (void) ch_end_seek();
996 * Search does not include current screen.
998 if (search_type
& SRCH_FORW
)
999 linenum
= BOTTOM_PLUS_ONE
;
1002 pos
= position(linenum
);
1006 * Search includes current screen.
1007 * It starts at the jump target (if searching backwards),
1008 * or at the jump target plus one (if forwards).
1010 linenum
= adjsline(jump_sline
);
1011 pos
= position(linenum
);
1012 if (search_type
& SRCH_FORW
)
1014 pos
= forw_raw_line(pos
, (char **)NULL
, (int *)NULL
);
1015 while (pos
== NULL_POSITION
)
1017 if (++linenum
>= sc_height
)
1019 pos
= position(linenum
);
1023 while (pos
== NULL_POSITION
)
1027 pos
= position(linenum
);
1035 * Search a subset of the file, specified by start/end position.
1038 search_range(pos
, endpos
, search_type
, matches
, maxlines
, plinepos
, pendpos
)
1054 POSITION linepos
, oldpos
;
1056 linenum
= find_linenum(pos
);
1061 * Get lines until we find a matching one or until
1062 * we hit end-of-file (or beginning-of-file if we're
1063 * going backwards), or until we hit the end position.
1068 * A signal aborts the search.
1073 if ((endpos
!= NULL_POSITION
&& pos
>= endpos
) || maxlines
== 0)
1076 * Reached end position without a match.
1078 if (pendpos
!= NULL
)
1085 if (search_type
& SRCH_FORW
)
1088 * Read the next line, and save the
1089 * starting position of that line in linepos.
1092 pos
= forw_raw_line(pos
, &line
, &line_len
);
1098 * Read the previous line and save the
1099 * starting position of that line in linepos.
1101 pos
= back_raw_line(pos
, &line
, &line_len
);
1107 if (pos
== NULL_POSITION
)
1110 * Reached EOF/BOF without a match.
1112 if (pendpos
!= NULL
)
1118 * If we're using line numbers, we might as well
1119 * remember the information we have now (the position
1120 * and line number of the current line).
1121 * Don't do it for every line because it slows down
1122 * the search. Remember the line number only if
1123 * we're "far" from the last place we remembered it.
1125 if (linenums
&& abs((int)(pos
- oldpos
)) > 1024)
1126 add_lnum(linenum
, pos
);
1130 * If it's a caseless search, convert the line to lowercase.
1131 * If we're doing backspace processing, delete backspaces.
1133 cvt_ops
= get_cvt_ops();
1134 cline
= calloc(1, cvt_length(line_len
, cvt_ops
));
1135 cvt_text(cline
, line
, &line_len
, cvt_ops
);
1138 * Test the next line to see if we have a match.
1139 * We are successful if we either want a match and got one,
1140 * or if we want a non-match and got one.
1142 line_match
= match_pattern(cline
, line_len
, &sp
, &ep
, 0);
1143 line_match
= (!(search_type
& SRCH_NO_MATCH
) && line_match
) ||
1144 ((search_type
& SRCH_NO_MATCH
) && !line_match
);
1153 if (search_type
& SRCH_FIND_ALL
)
1157 * We are supposed to find all matches in the range.
1158 * Just add the matches in this line to the
1159 * hilite list and keep searching.
1162 hilite_line(linepos
, cline
, line_len
, sp
, ep
, cvt_ops
);
1165 } else if (--matches
<= 0)
1168 * Found the one match we're looking for.
1172 if (hilite_search
== OPT_ON
)
1175 * Clear the hilite list and add only
1176 * the matches in this one line.
1180 hilite_line(linepos
, cline
, line_len
, sp
, ep
, cvt_ops
);
1184 if (plinepos
!= NULL
)
1185 *plinepos
= linepos
;
1192 * search for a pattern in history. If found, compile that pattern.
1195 hist_pattern(search_type
)
1201 set_mlist(ml_search
, 0);
1202 pattern
= cmd_lastpattern();
1203 if (pattern
== NULL
)
1206 if (compile_pattern(pattern
, search_type
) < 0)
1209 is_ucase_pattern
= is_ucase(pattern
);
1210 if (is_ucase_pattern
&& caseless
!= OPT_ONPLUS
)
1213 is_caseless
= caseless
;
1216 if (hilite_search
== OPT_ONPLUS
&& !hide_hilite
)
1221 #else /* CMD_HISTORY */
1223 #endif /* CMD_HISTORY */
1227 * Search for the n-th occurrence of a specified pattern,
1228 * either forward or backward.
1229 * Return the number of matches not yet found in this file
1230 * (that is, n minus the number of matches found).
1231 * Return -1 if the search should be aborted.
1232 * Caller may continue the search in another file
1233 * if less than n matches are found in this file.
1236 search(search_type
, pattern
, n
)
1244 if (pattern
== NULL
|| *pattern
== '\0')
1247 * A null pattern means use the previously compiled pattern.
1249 if (!prev_pattern() && !hist_pattern(search_type
))
1251 error("No previous regular expression", NULL_PARG
);
1254 if ((search_type
& SRCH_NO_REGEX
) !=
1255 (last_search_type
& SRCH_NO_REGEX
))
1257 error("Please re-enter search pattern", NULL_PARG
);
1261 if (hilite_search
== OPT_ON
)
1264 * Erase the highlights currently on screen.
1265 * If the search fails, we'll redisplay them later.
1269 if (hilite_search
== OPT_ONPLUS
&& hide_hilite
)
1272 * Highlight any matches currently on screen,
1273 * before we actually start the search.
1283 * Compile the pattern.
1285 if (compile_pattern(pattern
, search_type
) < 0)
1288 * Ignore case if -I is set OR
1289 * -i is set AND the pattern is all lowercase.
1291 is_ucase_pattern
= is_ucase(pattern
);
1292 if (is_ucase_pattern
&& caseless
!= OPT_ONPLUS
)
1295 is_caseless
= caseless
;
1300 * Erase the highlights currently on screen.
1301 * Also permanently delete them from the hilite list.
1307 if (hilite_search
== OPT_ONPLUS
)
1310 * Highlight any matches currently on screen,
1311 * before we actually start the search.
1319 * Figure out where to start the search.
1321 pos
= search_pos(search_type
);
1322 if (pos
== NULL_POSITION
)
1325 * Can't find anyplace to start searching from.
1327 if (search_type
& SRCH_PAST_EOF
)
1329 /* repaint(); -- why was this here? */
1330 error("Nothing to search", NULL_PARG
);
1334 n
= search_range(pos
, NULL_POSITION
, search_type
, n
, -1,
1335 &pos
, (POSITION
*)NULL
);
1339 * Search was unsuccessful.
1342 if (hilite_search
== OPT_ON
&& n
> 0)
1344 * Redisplay old hilites.
1351 if (!(search_type
& SRCH_NO_MOVE
))
1354 * Go to the matching line.
1356 jump_loc(pos
, jump_sline
);
1360 if (hilite_search
== OPT_ON
)
1362 * Display new hilites in the matching line.
1372 * Prepare hilites in a given range of the file.
1374 * The pair (prep_startpos,prep_endpos) delimits a contiguous region
1375 * of the file that has been "prepared"; that is, scanned for matches for
1376 * the current search pattern, and hilites have been created for such matches.
1377 * If prep_startpos == NULL_POSITION, the prep region is empty.
1378 * If prep_endpos == NULL_POSITION, the prep region extends to EOF.
1379 * prep_hilite asks that the range (spos,epos) be covered by the prep region.
1382 prep_hilite(spos
, epos
, maxlines
)
1387 POSITION nprep_startpos
= prep_startpos
;
1388 POSITION nprep_endpos
= prep_endpos
;
1394 * Search beyond where we're asked to search, so the prep region covers
1395 * more than we need. Do one big search instead of a bunch of small ones.
1397 #define SEARCH_MORE (3*size_linebuf)
1399 if (!prev_pattern())
1403 * If we're limited to a max number of lines, figure out the
1404 * file position we should stop at.
1407 max_epos
= NULL_POSITION
;
1411 for (i
= 0; i
< maxlines
; i
++)
1412 max_epos
= forw_raw_line(max_epos
, (char **)NULL
, (int *)NULL
);
1417 * The range that we need to search (spos,epos); and the range that
1418 * the "prep" region will then cover (nprep_startpos,nprep_endpos).
1421 if (prep_startpos
== NULL_POSITION
||
1422 (epos
!= NULL_POSITION
&& epos
< prep_startpos
) ||
1426 * New range is not contiguous with old prep region.
1427 * Discard the old prep region and start a new one.
1430 if (epos
!= NULL_POSITION
)
1431 epos
+= SEARCH_MORE
;
1432 nprep_startpos
= spos
;
1436 * New range partially or completely overlaps old prep region.
1438 if (epos
== NULL_POSITION
)
1441 * New range goes to end of file.
1444 } else if (epos
> prep_endpos
)
1447 * New range ends after old prep region.
1448 * Extend prep region to end at end of new range.
1450 epos
+= SEARCH_MORE
;
1451 } else /* (epos <= prep_endpos) */
1454 * New range ends within old prep region.
1455 * Truncate search to end at start of old prep region.
1457 epos
= prep_startpos
;
1460 if (spos
< prep_startpos
)
1463 * New range starts before old prep region.
1464 * Extend old prep region backwards to start at
1465 * start of new range.
1467 if (spos
< SEARCH_MORE
)
1470 spos
-= SEARCH_MORE
;
1471 nprep_startpos
= spos
;
1472 } else /* (spos >= prep_startpos) */
1475 * New range starts within or after old prep region.
1476 * Trim search to start at end of old prep region.
1482 if (epos
!= NULL_POSITION
&& max_epos
!= NULL_POSITION
&&
1485 * Don't go past the max position we're allowed.
1489 if (epos
== NULL_POSITION
|| epos
> spos
)
1491 result
= search_range(spos
, epos
, SRCH_FORW
|SRCH_FIND_ALL
, 0,
1492 maxlines
, (POSITION
*)NULL
, &new_epos
);
1495 if (prep_endpos
== NULL_POSITION
|| new_epos
> prep_endpos
)
1496 nprep_endpos
= new_epos
;
1498 prep_startpos
= nprep_startpos
;
1499 prep_endpos
= nprep_endpos
;
1504 * Simple pattern matching function.
1505 * It supports no metacharacters like *, etc.
1508 match(pattern
, pattern_len
, buf
, buf_len
, pfound
, pend
)
1513 char **pfound
, **pend
;
1515 register char *pp
, *lp
;
1516 register char *pattern_end
= pattern
+ pattern_len
;
1517 register char *buf_end
= buf
+ buf_len
;
1519 for ( ; buf
< buf_end
; buf
++)
1521 for (pp
= pattern
, lp
= buf
; *pp
== *lp
; pp
++, lp
++)
1522 if (pp
== pattern_end
|| lp
== buf_end
)
1524 if (pp
== pattern_end
)
1538 * This function is called by the V8 regcomp to report
1539 * errors in regular expressions.