3 * Copyright (C) 1984-2015 Mark Nudelman
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Less License, as specified in the README file.
8 * For more information, see the README file.
13 * Routines to search a file for a pattern.
21 #define MINPOS(a,b) (((a) < (b)) ? (a) : (b))
22 #define MAXPOS(a,b) (((a) > (b)) ? (a) : (b))
25 extern int how_search;
29 extern int jump_sline;
31 extern int less_is_more;
33 extern int status_col;
34 extern void * constant ml_search;
35 extern POSITION start_attnpos;
36 extern POSITION end_attnpos;
38 extern int screen_trashed;
40 extern int hilite_search;
41 extern int size_linebuf;
43 extern int can_goto_line;
44 static int hide_hilite;
45 static POSITION prep_startpos;
46 static POSITION prep_endpos;
47 static int is_caseless;
48 static int is_ucase_pattern;
51 * Structures for maintaining a set of ranges for hilites and filtered-out
52 * lines. Each range is stored as a node within a red-black tree, and we
53 * try to extend existing ranges (without creating overlaps) rather than
54 * create new nodes if possible. We remember the last node found by a
55 * search for constant-time lookup if the next search is near enough to
56 * the previous. To aid that, we overlay a secondary doubly-linked list
57 * on top of the red-black tree so we can find the preceding/succeeding
58 * nodes also in constant time.
60 * Each node is allocated from a series of pools, each pool double the size
61 * of the previous (for amortised constant time allocation). Since our only
62 * tree operations are clear and node insertion, not node removal, we don't
63 * need to maintain a usage bitmap or freelist and can just return nodes
64 * from the pool in-order until capacity is reached.
73 struct hilite_node *parent;
74 struct hilite_node *left;
75 struct hilite_node *right;
76 struct hilite_node *prev;
77 struct hilite_node *next;
85 struct hilite_storage *next;
86 struct hilite_node *nodes;
90 struct hilite_storage *first;
91 struct hilite_storage *current;
92 struct hilite_node *root;
93 struct hilite_node *lookaside;
95 #define HILITE_INITIALIZER() { NULL, NULL, NULL, NULL }
96 #define HILITE_LOOKASIDE_STEPS 2
98 static struct hilite_tree hilite_anchor = HILITE_INITIALIZER();
99 static struct hilite_tree filter_anchor = HILITE_INITIALIZER();
104 * These are the static variables that represent the "remembered"
105 * search pattern and filter pattern.
107 struct pattern_info {
108 DEFINE_PATTERN(compiled);
114 #define info_compiled(info) ((void*)0)
116 #define info_compiled(info) ((info)->compiled)
119 static struct pattern_info search_info;
120 static struct pattern_info filter_info;
123 * Are there any uppercase letters in this string?
129 char *str_end = str + strlen(str);
132 while (str < str_end)
134 ch = step_char(&str, +1, str_end);
142 * Compile and save a search pattern.
145 set_pattern(info, pattern, search_type)
146 struct pattern_info *info;
152 CLEAR_PATTERN(info->compiled);
153 else if (compile_pattern(pattern, search_type, &info->compiled) < 0)
156 /* Pattern compiled successfully; save the text too. */
157 if (info->text != NULL)
162 info->text = (char *) ecalloc(1, strlen(pattern)+1);
163 strcpy(info->text, pattern);
165 info->search_type = search_type;
168 * Ignore case if -I is set OR
169 * -i is set AND the pattern is all lowercase.
171 is_ucase_pattern = is_ucase(pattern);
172 if (is_ucase_pattern && caseless != OPT_ONPLUS)
175 is_caseless = caseless;
180 * Discard a saved pattern.
184 struct pattern_info *info;
186 if (info->text != NULL)
190 uncompile_pattern(&info->compiled);
195 * Initialize saved pattern to nothing.
199 struct pattern_info *info;
201 CLEAR_PATTERN(info->compiled);
203 info->search_type = 0;
207 * Initialize search variables.
212 init_pattern(&search_info);
213 init_pattern(&filter_info);
217 * Determine which text conversions to perform before pattern matching.
223 if (is_caseless || bs_mode == BS_SPECIAL)
227 if (bs_mode == BS_SPECIAL)
229 if (bs_mode != BS_CONTROL)
231 } else if (bs_mode != BS_CONTROL)
235 if (ctldisp == OPT_ONPLUS)
241 * Is there a previous (remembered) search pattern?
245 struct pattern_info *info;
248 if ((info->search_type & SRCH_NO_REGEX) == 0)
249 return (!is_null_pattern(info->compiled));
251 return (info->text != NULL);
256 * Repaint the hilites currently displayed on the screen.
257 * Repaint each line which contains highlighted text.
258 * If on==0, force all hilites off.
266 int save_hide_hilite;
271 save_hide_hilite = hide_hilite;
282 hide_hilite = save_hide_hilite;
286 for (slinenum = TOP; slinenum < TOP + sc_height-1; slinenum++)
288 pos = position(slinenum);
289 if (pos == NULL_POSITION)
291 (void) forw_line(pos);
296 hide_hilite = save_hide_hilite;
300 * Clear the attn hilite.
306 POSITION old_start_attnpos;
307 POSITION old_end_attnpos;
312 if (start_attnpos == NULL_POSITION)
314 old_start_attnpos = start_attnpos;
315 old_end_attnpos = end_attnpos;
316 start_attnpos = end_attnpos = NULL_POSITION;
326 for (slinenum = TOP; slinenum < TOP + sc_height-1; slinenum++)
328 pos = position(slinenum);
329 if (pos == NULL_POSITION)
331 epos = position(slinenum+1);
332 if (pos < old_end_attnpos &&
333 (epos == NULL_POSITION || epos > old_start_attnpos))
335 (void) forw_line(pos);
347 * Hide search string highlighting.
352 if (!prev_pattern(&search_info))
354 error("No previous regular expression", NULL_PARG);
358 hide_hilite = !hide_hilite;
365 * Clear the hilite list.
369 struct hilite_tree *anchor;
371 struct hilite_storage *hls;
372 struct hilite_storage *nexthls;
374 for (hls = anchor->first; hls != NULL; hls = nexthls)
377 free((void*)hls->nodes);
380 anchor->first = NULL;
381 anchor->current = NULL;
384 anchor->lookaside = NULL;
386 prep_startpos = prep_endpos = NULL_POSITION;
392 clr_hlist(&hilite_anchor);
398 clr_hlist(&filter_anchor);
403 struct hilite_tree *anchor;
405 struct hilite_node *n = anchor->root;
406 while (n != NULL && n->right != NULL)
413 struct hilite_node *n;
420 struct hilite_node *n;
426 * Find the node covering pos, or the node after it if no node covers it,
427 * or return NULL if pos is after the last range. Remember the found node,
428 * to speed up subsequent searches for the same or similar positions (if
429 * we return NULL, remember the last node.)
432 hlist_find(anchor, pos)
433 struct hilite_tree *anchor;
436 struct hilite_node *n, *m;
438 if (anchor->lookaside)
443 n = anchor->lookaside;
447 if (pos < n->r.hl_endpos)
449 if (n->prev == NULL || pos >= n->prev->r.hl_endpos)
454 } else if (n->next == NULL)
462 * If we don't find the right node within a small
463 * distance, don't keep doing a linear search!
465 if (steps >= HILITE_LOOKASIDE_STEPS)
469 if (pos < n->r.hl_endpos)
470 anchor->lookaside = n = n->prev;
472 anchor->lookaside = n = n->next;
484 if (pos < n->r.hl_startpos)
494 if (pos >= n->r.hl_endpos)
496 if (n->right != NULL)
514 anchor->lookaside = n;
516 anchor->lookaside = m;
522 * Should any characters in a specified range be highlighted?
525 is_hilited_range(pos, epos)
529 struct hilite_node *n = hlist_find(&hilite_anchor, pos);
530 return (n != NULL && (epos == NULL_POSITION || epos > n->r.hl_startpos));
534 * Is a line "filtered" -- that is, should it be hidden?
540 struct hilite_node *n;
542 if (ch_getflags() & CH_HELPFILE)
545 n = hlist_find(&filter_anchor, pos);
546 return (n != NULL && pos >= n->r.hl_startpos);
550 * If pos is hidden, return the next position which isn't, otherwise
557 struct hilite_node *n;
559 if (ch_getflags() & CH_HELPFILE)
562 n = hlist_find(&filter_anchor, pos);
563 while (n != NULL && pos >= n->r.hl_startpos)
565 pos = n->r.hl_endpos;
572 * If pos is hidden, return the previous position which isn't or 0 if
573 * we're filtered right to the beginning, otherwise just return pos.
579 struct hilite_node *n;
581 if (ch_getflags() & CH_HELPFILE)
584 n = hlist_find(&filter_anchor, pos);
585 while (n != NULL && pos >= n->r.hl_startpos)
587 pos = n->r.hl_startpos;
598 * Should any characters in a specified range be highlighted?
599 * If nohide is nonzero, don't consider hide_hilite.
602 is_hilited(pos, epos, nohide, p_matches)
610 if (p_matches != NULL)
614 start_attnpos != NULL_POSITION &&
616 (epos == NULL_POSITION || epos > start_attnpos))
618 * The attn line overlaps this range.
622 match = is_hilited_range(pos, epos);
626 if (p_matches != NULL)
628 * Report matches, even if we're hiding highlights.
632 if (hilite_search == 0)
634 * Not doing highlighting.
638 if (!nohide && hide_hilite)
640 * Highlighting is hidden.
648 * Tree node storage: get the current block of nodes if it has spare
649 * capacity, or create a new one if not.
651 static struct hilite_storage*
652 hlist_getstorage(anchor)
653 struct hilite_tree *anchor;
656 struct hilite_storage *s;
660 if (anchor->current->used < anchor->current->capacity)
661 return anchor->current;
662 capacity = anchor->current->capacity * 2;
665 s = (struct hilite_storage *) ecalloc(1, sizeof(struct hilite_storage));
666 s->nodes = (struct hilite_node *) ecalloc(capacity, sizeof(struct hilite_node));
667 s->capacity = capacity;
671 anchor->current->next = s;
679 * Tree node storage: retrieve a new empty node to be inserted into the
682 static struct hilite_node*
683 hlist_getnode(anchor)
684 struct hilite_tree *anchor;
686 struct hilite_storage *s = hlist_getstorage(anchor);
687 return &s->nodes[s->used++];
691 * Rotate the tree left around a pivot node.
694 hlist_rotate_left(anchor, n)
695 struct hilite_tree *anchor;
696 struct hilite_node *n;
698 struct hilite_node *np = n->parent;
699 struct hilite_node *nr = n->right;
700 struct hilite_node *nrl = n->right->left;
722 * Rotate the tree right around a pivot node.
725 hlist_rotate_right(anchor, n)
726 struct hilite_tree *anchor;
727 struct hilite_node *n;
729 struct hilite_node *np = n->parent;
730 struct hilite_node *nl = n->left;
731 struct hilite_node *nlr = n->left->right;
754 * Add a new hilite to a hilite list.
757 add_hilite(anchor, hl)
758 struct hilite_tree *anchor;
761 struct hilite_node *p, *n, *u;
763 /* Ignore empty ranges. */
764 if (hl->hl_startpos >= hl->hl_endpos)
769 /* Inserting the very first node is trivial. */
772 n = hlist_getnode(anchor);
775 anchor->lookaside = n;
780 * Find our insertion point. If we come across any overlapping
781 * or adjoining existing ranges, shrink our range and discard
782 * if it become empty.
786 if (hl->hl_startpos < p->r.hl_startpos)
788 if (hl->hl_endpos > p->r.hl_startpos)
789 hl->hl_endpos = p->r.hl_startpos;
797 if (hl->hl_startpos < p->r.hl_endpos) {
798 hl->hl_startpos = p->r.hl_endpos;
799 if (hl->hl_startpos >= hl->hl_endpos)
802 if (p->right != NULL)
811 * Now we're at the right leaf, again check for contiguous ranges
812 * and extend the existing node if possible to avoid the
813 * insertion. Otherwise insert a new node at the leaf.
815 if (hl->hl_startpos < p->r.hl_startpos) {
816 if (hl->hl_endpos == p->r.hl_startpos)
818 p->r.hl_startpos = hl->hl_startpos;
821 if (p->prev != NULL && p->prev->r.hl_endpos == hl->hl_startpos)
823 p->prev->r.hl_endpos = hl->hl_endpos;
827 p->left = n = hlist_getnode(anchor);
836 if (p->r.hl_endpos == hl->hl_startpos)
838 p->r.hl_endpos = hl->hl_endpos;
841 if (p->next != NULL && hl->hl_endpos == p->next->r.hl_startpos) {
842 p->next->r.hl_startpos = hl->hl_startpos;
846 p->right = n = hlist_getnode(anchor);
860 * The tree is in the correct order and covers the right ranges
861 * now, but may have become unbalanced. Rebalance it using the
862 * standard red-black tree constraints and operations.
866 /* case 1 - current is root, root is always black */
867 if (n->parent == NULL)
873 /* case 2 - parent is black, we can always be red */
878 * constraint: because the root must be black, if our
879 * parent is red it cannot be the root therefore we must
884 * case 3 - parent and uncle are red, repaint them black,
885 * the grandparent red, and start again at the grandparent.
887 u = n->parent->parent->left;
889 u = n->parent->parent->right;
890 if (u != NULL && u->red)
894 n = n->parent->parent;
900 * case 4 - parent is red but uncle is black, parent and
901 * grandparent on opposite sides. We need to start
902 * changing the structure now. This and case 5 will shorten
903 * our branch and lengthen the sibling, between them
906 if (n == n->parent->right &&
907 n->parent == n->parent->parent->left)
909 hlist_rotate_left(anchor, n->parent);
911 } else if (n == n->parent->left &&
912 n->parent == n->parent->parent->right)
914 hlist_rotate_right(anchor, n->parent);
919 * case 5 - parent is red but uncle is black, parent and
920 * grandparent on same side
923 n->parent->parent->red = 1;
924 if (n == n->parent->left)
925 hlist_rotate_right(anchor, n->parent->parent);
927 hlist_rotate_left(anchor, n->parent->parent);
933 * Hilight every character in a range of displayed characters.
936 create_hilites(linepos, start_index, end_index, chpos)
945 /* Start the first hilite. */
946 hl.hl_startpos = linepos + chpos[start_index];
949 * Step through the displayed chars.
950 * If the source position (before cvt) of the char is one more
951 * than the source pos of the previous char (the usual case),
952 * just increase the size of the current hilite by one.
953 * Otherwise (there are backspaces or something involved),
954 * finish the current hilite and start a new one.
956 for (i = start_index+1; i <= end_index; i++)
958 if (chpos[i] != chpos[i-1] + 1 || i == end_index)
960 hl.hl_endpos = linepos + chpos[i-1] + 1;
961 add_hilite(&hilite_anchor, &hl);
962 /* Start new hilite unless this is the last char. */
965 hl.hl_startpos = linepos + chpos[i];
972 * Make a hilite for each string in a physical line which matches
973 * the current pattern.
974 * sp,ep delimit the first match already found.
977 hilite_line(linepos, line, line_len, chpos, sp, ep, cvt_ops)
987 char *line_end = line + line_len;
990 * sp and ep delimit the first match in the line.
991 * Mark the corresponding file positions, then
992 * look for further matches and mark them.
993 * {{ This technique, of calling match_pattern on subsequent
994 * substrings of the line, may mark more than is correct
995 * if the pattern starts with "^". This bug is fixed
996 * for those regex functions that accept a notbol parameter
997 * (currently POSIX, PCRE and V8-with-regexec2). }}
1001 if (sp == NULL || ep == NULL)
1003 create_hilites(linepos, sp-line, ep-line, chpos);
1005 * If we matched more than zero characters,
1006 * move to the first char after the string we matched.
1007 * If we matched zero, just move to the next char.
1011 else if (searchp != line_end)
1013 else /* end of line */
1015 } while (match_pattern(info_compiled(&search_info), search_info.text,
1016 searchp, line_end - searchp, &sp, &ep, 1, search_info.search_type));
1021 * Change the caseless-ness of searches.
1022 * Updates the internal search state to reflect a change in the -i flag.
1027 if (!is_ucase_pattern)
1029 * Pattern did not have uppercase.
1030 * Just set the search caselessness to the global caselessness.
1032 is_caseless = caseless;
1035 * Pattern did have uppercase.
1036 * Discard the pattern; we can't change search caselessness now.
1038 clear_pattern(&search_info);
1043 * Find matching text which is currently on screen and highlight it.
1048 struct scrpos scrpos;
1050 get_scrpos(&scrpos);
1051 if (scrpos.pos == NULL_POSITION)
1053 prep_hilite(scrpos.pos, position(BOTTOM_PLUS_ONE), -1);
1058 * Change highlighting parameters.
1064 * Erase any highlights currently on screen.
1069 if (hilite_search == OPT_ONPLUS)
1071 * Display highlights.
1078 * Figure out where to start a search.
1081 search_pos(search_type)
1090 * Start at the beginning (or end) of the file.
1091 * The empty_screen() case is mainly for
1092 * command line initiated searches;
1093 * for example, "+/xyz" on the command line.
1094 * Also for multi-file (SRCH_PAST_EOF) searches.
1096 if (search_type & SRCH_FORW)
1102 if (pos == NULL_POSITION)
1104 (void) ch_end_seek();
1113 if (how_search == OPT_ON)
1116 * Search does not include current screen.
1118 if (search_type & SRCH_FORW)
1119 linenum = BOTTOM_PLUS_ONE;
1122 } else if (how_search == OPT_ONPLUS && !(search_type & SRCH_AFTER_TARGET))
1125 * Search includes all of displayed screen.
1127 if (search_type & SRCH_FORW)
1130 linenum = BOTTOM_PLUS_ONE;
1134 * Search includes the part of current screen beyond the jump target.
1135 * It starts at the jump target (if searching backwards),
1136 * or at the jump target plus one (if forwards).
1138 linenum = adjsline(jump_sline);
1139 if (search_type & SRCH_FORW)
1142 pos = position(linenum);
1144 pos = forw_raw_line(pos, (char **)NULL, (int *)NULL);
1148 * If the line is empty, look around for a plausible starting place.
1150 if (search_type & SRCH_FORW)
1152 while (pos == NULL_POSITION)
1154 if (++linenum >= sc_height)
1156 pos = position(linenum);
1160 while (pos == NULL_POSITION)
1164 pos = position(linenum);
1171 * Search a subset of the file, specified by start/end position.
1174 search_range(pos, endpos, search_type, matches, maxlines, plinepos, pendpos)
1192 POSITION linepos, oldpos;
1194 linenum = find_linenum(pos);
1199 * Get lines until we find a matching one or until
1200 * we hit end-of-file (or beginning-of-file if we're
1201 * going backwards), or until we hit the end position.
1206 * A signal aborts the search.
1211 if ((endpos != NULL_POSITION && pos >= endpos) || maxlines == 0)
1214 * Reached end position without a match.
1216 if (pendpos != NULL)
1223 if (search_type & SRCH_FORW)
1226 * Read the next line, and save the
1227 * starting position of that line in linepos.
1230 pos = forw_raw_line(pos, &line, &line_len);
1236 * Read the previous line and save the
1237 * starting position of that line in linepos.
1239 pos = back_raw_line(pos, &line, &line_len);
1245 if (pos == NULL_POSITION)
1248 * Reached EOF/BOF without a match.
1250 if (pendpos != NULL)
1256 * If we're using line numbers, we might as well
1257 * remember the information we have now (the position
1258 * and line number of the current line).
1259 * Don't do it for every line because it slows down
1260 * the search. Remember the line number only if
1261 * we're "far" from the last place we remembered it.
1263 if (linenums && abs((int)(pos - oldpos)) > 2048)
1264 add_lnum(linenum, pos);
1267 if (is_filtered(linepos))
1271 * If it's a caseless search, convert the line to lowercase.
1272 * If we're doing backspace processing, delete backspaces.
1274 cvt_ops = get_cvt_ops();
1275 cvt_len = cvt_length(line_len, cvt_ops);
1276 cline = (char *) ecalloc(1, cvt_len);
1277 chpos = cvt_alloc_chpos(cvt_len);
1278 cvt_text(cline, line, chpos, &line_len, cvt_ops);
1282 * Check to see if the line matches the filter pattern.
1283 * If so, add an entry to the filter list.
1285 if (((search_type & SRCH_FIND_ALL) ||
1286 prep_startpos == NULL_POSITION ||
1287 linepos < prep_startpos || linepos >= prep_endpos) &&
1288 prev_pattern(&filter_info)) {
1289 int line_filter = match_pattern(info_compiled(&filter_info), filter_info.text,
1290 cline, line_len, &sp, &ep, 0, filter_info.search_type);
1294 hl.hl_startpos = linepos;
1296 add_hilite(&filter_anchor, &hl);
1303 * Test the next line to see if we have a match.
1304 * We are successful if we either want a match and got one,
1305 * or if we want a non-match and got one.
1307 if (prev_pattern(&search_info))
1309 line_match = match_pattern(info_compiled(&search_info), search_info.text,
1310 cline, line_len, &sp, &ep, 0, search_type);
1316 if (search_type & SRCH_FIND_ALL)
1320 * We are supposed to find all matches in the range.
1321 * Just add the matches in this line to the
1322 * hilite list and keep searching.
1324 hilite_line(linepos, cline, line_len, chpos, sp, ep, cvt_ops);
1326 } else if (--matches <= 0)
1329 * Found the one match we're looking for.
1333 if (hilite_search == OPT_ON)
1336 * Clear the hilite list and add only
1337 * the matches in this one line.
1340 hilite_line(linepos, cline, line_len, chpos, sp, ep, cvt_ops);
1345 if (plinepos != NULL)
1346 *plinepos = linepos;
1357 * search for a pattern in history. If found, compile that pattern.
1360 hist_pattern(search_type)
1366 set_mlist(ml_search, 0);
1367 pattern = cmd_lastpattern();
1368 if (pattern == NULL)
1371 if (set_pattern(&search_info, pattern, search_type) < 0)
1375 if (hilite_search == OPT_ONPLUS && !hide_hilite)
1380 #else /* CMD_HISTORY */
1382 #endif /* CMD_HISTORY */
1386 * Search for the n-th occurrence of a specified pattern,
1387 * either forward or backward.
1388 * Return the number of matches not yet found in this file
1389 * (that is, n minus the number of matches found).
1390 * Return -1 if the search should be aborted.
1391 * Caller may continue the search in another file
1392 * if less than n matches are found in this file.
1395 search(search_type, pattern, n)
1402 if (pattern == NULL || *pattern == '\0')
1405 * A null pattern means use the previously compiled pattern.
1407 search_type |= SRCH_AFTER_TARGET;
1408 if (!prev_pattern(&search_info) && !hist_pattern(search_type))
1410 error("No previous regular expression", NULL_PARG);
1413 if ((search_type & SRCH_NO_REGEX) !=
1414 (search_info.search_type & SRCH_NO_REGEX))
1416 error("Please re-enter search pattern", NULL_PARG);
1420 if (hilite_search == OPT_ON)
1423 * Erase the highlights currently on screen.
1424 * If the search fails, we'll redisplay them later.
1428 if (hilite_search == OPT_ONPLUS && hide_hilite)
1431 * Highlight any matches currently on screen,
1432 * before we actually start the search.
1442 * Compile the pattern.
1444 if (set_pattern(&search_info, pattern, search_type) < 0)
1450 * Erase the highlights currently on screen.
1451 * Also permanently delete them from the hilite list.
1457 if (hilite_search == OPT_ONPLUS)
1460 * Highlight any matches currently on screen,
1461 * before we actually start the search.
1469 * Figure out where to start the search.
1471 pos = search_pos(search_type);
1472 if (pos == NULL_POSITION)
1475 * Can't find anyplace to start searching from.
1477 if (search_type & SRCH_PAST_EOF)
1479 /* repaint(); -- why was this here? */
1480 error("Nothing to search", NULL_PARG);
1484 n = search_range(pos, NULL_POSITION, search_type, n, -1,
1485 &pos, (POSITION*)NULL);
1489 * Search was unsuccessful.
1492 if (hilite_search == OPT_ON && n > 0)
1494 * Redisplay old hilites.
1501 if (!(search_type & SRCH_NO_MOVE))
1504 * Go to the matching line.
1506 jump_loc(pos, jump_sline);
1510 if (hilite_search == OPT_ON)
1512 * Display new hilites in the matching line.
1522 * Prepare hilites in a given range of the file.
1524 * The pair (prep_startpos,prep_endpos) delimits a contiguous region
1525 * of the file that has been "prepared"; that is, scanned for matches for
1526 * the current search pattern, and hilites have been created for such matches.
1527 * If prep_startpos == NULL_POSITION, the prep region is empty.
1528 * If prep_endpos == NULL_POSITION, the prep region extends to EOF.
1529 * prep_hilite asks that the range (spos,epos) be covered by the prep region.
1532 prep_hilite(spos, epos, maxlines)
1537 POSITION nprep_startpos = prep_startpos;
1538 POSITION nprep_endpos = prep_endpos;
1545 * Search beyond where we're asked to search, so the prep region covers
1546 * more than we need. Do one big search instead of a bunch of small ones.
1548 #define SEARCH_MORE (3*size_linebuf)
1550 if (!prev_pattern(&search_info) && !is_filtering())
1554 * Make sure our prep region always starts at the beginning of
1555 * a line. (search_range takes care of the end boundary below.)
1557 spos = back_raw_line(spos+1, (char **)NULL, (int *)NULL);
1560 * If we're limited to a max number of lines, figure out the
1561 * file position we should stop at.
1564 max_epos = NULL_POSITION;
1568 for (i = 0; i < maxlines; i++)
1569 max_epos = forw_raw_line(max_epos, (char **)NULL, (int *)NULL);
1574 * The range that we need to search (spos,epos); and the range that
1575 * the "prep" region will then cover (nprep_startpos,nprep_endpos).
1578 if (prep_startpos == NULL_POSITION ||
1579 (epos != NULL_POSITION && epos < prep_startpos) ||
1583 * New range is not contiguous with old prep region.
1584 * Discard the old prep region and start a new one.
1588 if (epos != NULL_POSITION)
1589 epos += SEARCH_MORE;
1590 nprep_startpos = spos;
1594 * New range partially or completely overlaps old prep region.
1596 if (epos == NULL_POSITION)
1599 * New range goes to end of file.
1602 } else if (epos > prep_endpos)
1605 * New range ends after old prep region.
1606 * Extend prep region to end at end of new range.
1608 epos += SEARCH_MORE;
1609 } else /* (epos <= prep_endpos) */
1612 * New range ends within old prep region.
1613 * Truncate search to end at start of old prep region.
1615 epos = prep_startpos;
1618 if (spos < prep_startpos)
1621 * New range starts before old prep region.
1622 * Extend old prep region backwards to start at
1623 * start of new range.
1625 if (spos < SEARCH_MORE)
1628 spos -= SEARCH_MORE;
1629 nprep_startpos = spos;
1630 } else /* (spos >= prep_startpos) */
1633 * New range starts within or after old prep region.
1634 * Trim search to start at end of old prep region.
1640 if (epos != NULL_POSITION && max_epos != NULL_POSITION &&
1643 * Don't go past the max position we're allowed.
1647 if (epos == NULL_POSITION || epos > spos)
1649 int search_type = SRCH_FORW | SRCH_FIND_ALL;
1650 search_type |= (search_info.search_type & SRCH_NO_REGEX);
1653 result = search_range(spos, epos, search_type, 0, maxlines, (POSITION*)NULL, &new_epos);
1656 if (prep_endpos == NULL_POSITION || new_epos > prep_endpos)
1657 nprep_endpos = new_epos;
1660 * Check both ends of the resulting prep region to
1661 * make sure they're not filtered. If they are,
1662 * keep going at least one more line until we find
1663 * something that isn't filtered, or hit the end.
1665 if (prep_endpos == NULL_POSITION || nprep_endpos > prep_endpos)
1667 if (new_epos >= nprep_endpos && is_filtered(new_epos-1))
1669 spos = nprep_endpos;
1670 epos = forw_raw_line(nprep_endpos, (char **)NULL, (int *)NULL);
1671 if (epos == NULL_POSITION)
1678 if (prep_startpos == NULL_POSITION || nprep_startpos < prep_startpos)
1680 if (nprep_startpos > 0 && is_filtered(nprep_startpos))
1682 epos = nprep_startpos;
1683 spos = back_raw_line(nprep_startpos, (char **)NULL, (int *)NULL);
1684 if (spos == NULL_POSITION)
1686 nprep_startpos = spos;
1694 prep_startpos = nprep_startpos;
1695 prep_endpos = nprep_endpos;
1699 * Set the pattern to be used for line filtering.
1702 set_filter_pattern(pattern, search_type)
1707 if (pattern == NULL || *pattern == '\0')
1708 clear_pattern(&filter_info);
1710 set_pattern(&filter_info, pattern, search_type);
1715 * Is there a line filter in effect?
1720 if (ch_getflags() & CH_HELPFILE)
1722 return prev_pattern(&filter_info);
1728 * This function is called by the V8 regcomp to report
1729 * errors in regular expressions.
1731 public int reg_show_error = 1;
1739 if (!reg_show_error)