2 * Copyright (C) 1984-2014 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, see the README file.
12 * 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))
24 extern int how_search;
28 extern int jump_sline;
31 extern int status_col;
32 extern void * constant ml_search;
33 extern POSITION start_attnpos;
34 extern POSITION end_attnpos;
36 extern int screen_trashed;
38 extern int hilite_search;
39 extern int size_linebuf;
41 extern int can_goto_line;
42 static int hide_hilite;
43 static POSITION prep_startpos;
44 static POSITION prep_endpos;
45 static int is_caseless;
46 static int is_ucase_pattern;
49 * Structures for maintaining a set of ranges for hilites and filtered-out
50 * lines. Each range is stored as a node within a red-black tree, and we
51 * try to extend existing ranges (without creating overlaps) rather than
52 * create new nodes if possible. We remember the last node found by a
53 * search for constant-time lookup if the next search is near enough to
54 * the previous. To aid that, we overlay a secondary doubly-linked list
55 * on top of the red-black tree so we can find the preceding/succeeding
56 * nodes also in constant time.
58 * Each node is allocated from a series of pools, each pool double the size
59 * of the previous (for amortised constant time allocation). Since our only
60 * tree operations are clear and node insertion, not node removal, we don't
61 * need to maintain a usage bitmap or freelist and can just return nodes
62 * from the pool in-order until capacity is reached.
71 struct hilite_node *parent;
72 struct hilite_node *left;
73 struct hilite_node *right;
74 struct hilite_node *prev;
75 struct hilite_node *next;
84 struct hilite_storage *next;
88 struct hilite_storage *first;
89 struct hilite_storage *current;
90 struct hilite_node *root;
92 struct hilite_node *lookaside;
94 #define HILITE_INITIALIZER() { NULL, NULL, NULL, NULL }
95 #define HILITE_LOOKASIDE_STEPS 2
97 static struct hilite_tree hilite_anchor = HILITE_INITIALIZER();
98 static struct hilite_tree filter_anchor = HILITE_INITIALIZER();
103 * These are the static variables that represent the "remembered"
104 * search pattern and filter pattern.
106 struct pattern_info {
107 DEFINE_PATTERN(compiled);
113 #define info_compiled(info) ((void*)0)
115 #define info_compiled(info) ((info)->compiled)
118 static struct pattern_info search_info;
119 static struct pattern_info filter_info;
122 * Are there any uppercase letters in this string?
128 char *str_end = str + strlen(str);
131 while (str < str_end)
133 ch = step_char(&str, +1, str_end);
141 * Compile and save a search pattern.
144 set_pattern(info, pattern, search_type)
145 struct pattern_info *info;
151 CLEAR_PATTERN(info->compiled);
152 else if (compile_pattern(pattern, search_type, &info->compiled) < 0)
155 /* Pattern compiled successfully; save the text too. */
156 if (info->text != NULL)
161 info->text = (char *) ecalloc(1, strlen(pattern)+1);
162 strcpy(info->text, pattern);
164 info->search_type = search_type;
167 * Ignore case if -I is set OR
168 * -i is set AND the pattern is all lowercase.
170 is_ucase_pattern = is_ucase(pattern);
171 if (is_ucase_pattern && caseless != OPT_ONPLUS)
174 is_caseless = caseless;
179 * Discard a saved pattern.
183 struct pattern_info *info;
185 if (info->text != NULL)
189 uncompile_pattern(&info->compiled);
194 * Initialize saved pattern to nothing.
198 struct pattern_info *info;
200 CLEAR_PATTERN(info->compiled);
202 info->search_type = 0;
206 * Initialize search variables.
211 init_pattern(&search_info);
212 init_pattern(&filter_info);
216 * Determine which text conversions to perform before pattern matching.
222 if (is_caseless || bs_mode == BS_SPECIAL)
226 if (bs_mode == BS_SPECIAL)
228 if (bs_mode != BS_CONTROL)
230 } else if (bs_mode != BS_CONTROL)
234 if (ctldisp == OPT_ONPLUS)
240 * Is there a previous (remembered) search pattern?
244 struct pattern_info *info;
247 if ((info->search_type & SRCH_NO_REGEX) == 0)
248 return (!is_null_pattern(info->compiled));
250 return (info->text != NULL);
255 * Repaint the hilites currently displayed on the screen.
256 * Repaint each line which contains highlighted text.
257 * 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 epos = position(slinenum+1);
292 (void) forw_line(pos);
297 hide_hilite = save_hide_hilite;
301 * Clear the attn hilite.
307 POSITION old_start_attnpos;
308 POSITION old_end_attnpos;
313 if (start_attnpos == NULL_POSITION)
315 old_start_attnpos = start_attnpos;
316 old_end_attnpos = end_attnpos;
317 start_attnpos = end_attnpos = NULL_POSITION;
327 for (slinenum = TOP; slinenum < TOP + sc_height-1; slinenum++)
329 pos = position(slinenum);
330 if (pos == NULL_POSITION)
332 epos = position(slinenum+1);
333 if (pos < old_end_attnpos &&
334 (epos == NULL_POSITION || epos > old_start_attnpos))
336 (void) forw_line(pos);
348 * Hide search string highlighting.
353 if (!prev_pattern(&search_info))
355 error("No previous regular expression", NULL_PARG);
359 hide_hilite = !hide_hilite;
366 * Clear the hilite list.
370 struct hilite_tree *anchor;
372 struct hilite_storage *hls;
373 struct hilite_storage *nexthls;
375 for (hls = anchor->first; hls != NULL; hls = nexthls)
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, *pl;
438 pl = anchor->lookaside;
440 if (anchor->lookaside)
445 n = anchor->lookaside;
449 if (pos < n->r.hl_endpos)
451 if (n->prev == NULL || pos >= n->prev->r.hl_endpos)
456 } else if (n->next == NULL)
464 * If we don't find the right node within a small
465 * distance, don't keep doing a linear search!
467 if (steps >= HILITE_LOOKASIDE_STEPS)
471 if (pos < n->r.hl_endpos)
472 anchor->lookaside = n = n->prev;
474 anchor->lookaside = n = n->next;
486 if (pos < n->r.hl_startpos)
496 if (pos >= n->r.hl_endpos)
498 if (n->right != NULL)
516 anchor->lookaside = n;
518 anchor->lookaside = m;
524 * Should any characters in a specified range be highlighted?
527 is_hilited_range(pos, epos)
531 struct hilite_node *n = hlist_find(&hilite_anchor, pos);
532 return (n != NULL && (epos == NULL_POSITION || epos > n->r.hl_startpos));
536 * Is a line "filtered" -- that is, should it be hidden?
542 struct hilite_node *n;
544 if (ch_getflags() & CH_HELPFILE)
547 n = hlist_find(&filter_anchor, pos);
548 return (n != NULL && pos >= n->r.hl_startpos);
552 * If pos is hidden, return the next position which isn't, otherwise
559 struct hilite_node *n;
561 if (ch_getflags() & CH_HELPFILE)
564 n = hlist_find(&filter_anchor, pos);
565 while (n != NULL && pos >= n->r.hl_startpos)
567 pos = n->r.hl_endpos;
574 * If pos is hidden, return the previous position which isn't or 0 if
575 * we're filtered right to the beginning, otherwise just return pos.
581 struct hilite_node *n;
583 if (ch_getflags() & CH_HELPFILE)
586 n = hlist_find(&filter_anchor, pos);
587 while (n != NULL && pos >= n->r.hl_startpos)
589 pos = n->r.hl_startpos;
600 * Should any characters in a specified range be highlighted?
601 * If nohide is nonzero, don't consider hide_hilite.
604 is_hilited(pos, epos, nohide, p_matches)
612 if (p_matches != NULL)
616 start_attnpos != NULL_POSITION &&
618 (epos == NULL_POSITION || epos > start_attnpos))
620 * The attn line overlaps this range.
624 match = is_hilited_range(pos, epos);
628 if (p_matches != NULL)
630 * Report matches, even if we're hiding highlights.
634 if (hilite_search == 0)
636 * Not doing highlighting.
640 if (!nohide && hide_hilite)
642 * Highlighting is hidden.
650 * Tree node storage: get the current block of nodes if it has spare
651 * capacity, or create a new one if not.
653 static struct hilite_storage*
654 hlist_getstorage(anchor)
655 struct hilite_tree *anchor;
658 size_t allocsize = sizeof(struct hilite_storage);
659 struct hilite_storage *s;
663 if (anchor->current->used < anchor->current->capacity)
664 return anchor->current;
665 capacity = anchor->current->capacity * 2;
667 allocsize += capacity * sizeof(struct hilite_node);
668 s = ecalloc(1, allocsize);
669 s->capacity = capacity;
673 anchor->current->next = s;
681 * Tree node storage: retrieve a new empty node to be inserted into the
684 static struct hilite_node*
685 hlist_getnode(anchor)
686 struct hilite_tree *anchor;
688 struct hilite_storage *s = hlist_getstorage(anchor);
690 struct hilite_node *n = ((struct hilite_node*)(s+1))+s->used;
696 * Rotate the tree left around a pivot node.
699 hlist_rotate_left(anchor, n)
700 struct hilite_tree *anchor;
701 struct hilite_node *n;
703 struct hilite_node *np = n->parent;
704 struct hilite_node *nr = n->right;
705 struct hilite_node *nrl = n->right->left;
727 * Rotate the tree right around a pivot node.
730 hlist_rotate_right(anchor, n)
731 struct hilite_tree *anchor;
732 struct hilite_node *n;
734 struct hilite_node *np = n->parent;
735 struct hilite_node *nl = n->left;
736 struct hilite_node *nlr = n->left->right;
759 * Add a new hilite to a hilite list.
762 add_hilite(anchor, hl)
763 struct hilite_tree *anchor;
766 struct hilite_node *p, *n, *u;
768 /* Ignore empty ranges. */
769 if (hl->hl_startpos >= hl->hl_endpos)
774 /* Inserting the very first node is trivial. */
777 n = hlist_getnode(anchor);
780 anchor->lookaside = n;
785 * Find our insertion point. If we come across any overlapping
786 * or adjoining existing ranges, shrink our range and discard
787 * if it become empty.
791 if (hl->hl_startpos < p->r.hl_startpos)
793 if (hl->hl_endpos > p->r.hl_startpos)
794 hl->hl_endpos = p->r.hl_startpos;
802 if (hl->hl_startpos < p->r.hl_endpos) {
803 hl->hl_startpos = p->r.hl_endpos;
804 if (hl->hl_startpos >= hl->hl_endpos)
807 if (p->right != NULL)
816 * Now we're at the right leaf, again check for contiguous ranges
817 * and extend the existing node if possible to avoid the
818 * insertion. Otherwise insert a new node at the leaf.
820 if (hl->hl_startpos < p->r.hl_startpos) {
821 if (hl->hl_endpos == p->r.hl_startpos)
823 p->r.hl_startpos = hl->hl_startpos;
826 if (p->prev != NULL && p->prev->r.hl_endpos == hl->hl_startpos)
828 p->prev->r.hl_endpos = hl->hl_endpos;
832 p->left = n = hlist_getnode(anchor);
841 if (p->r.hl_endpos == hl->hl_startpos)
843 p->r.hl_endpos = hl->hl_endpos;
846 if (p->next != NULL && hl->hl_endpos == p->next->r.hl_startpos) {
847 p->next->r.hl_startpos = hl->hl_startpos;
851 p->right = n = hlist_getnode(anchor);
865 * The tree is in the correct order and covers the right ranges
866 * now, but may have become unbalanced. Rebalance it using the
867 * standard red-black tree constraints and operations.
871 /* case 1 - current is root, root is always black */
872 if (n->parent == NULL)
878 /* case 2 - parent is black, we can always be red */
883 * constraint: because the root must be black, if our
884 * parent is red it cannot be the root therefore we must
889 * case 3 - parent and uncle are red, repaint them black,
890 * the grandparent red, and start again at the grandparent.
892 u = n->parent->parent->left;
894 u = n->parent->parent->right;
895 if (u != NULL && u->red)
899 n = n->parent->parent;
905 * case 4 - parent is red but uncle is black, parent and
906 * grandparent on opposite sides. We need to start
907 * changing the structure now. This and case 5 will shorten
908 * our branch and lengthen the sibling, between them
911 if (n == n->parent->right &&
912 n->parent == n->parent->parent->left)
914 hlist_rotate_left(anchor, n->parent);
916 } else if (n == n->parent->left &&
917 n->parent == n->parent->parent->right)
919 hlist_rotate_right(anchor, n->parent);
924 * case 5 - parent is red but uncle is black, parent and
925 * grandparent on same side
928 n->parent->parent->red = 1;
929 if (n == n->parent->left)
930 hlist_rotate_right(anchor, n->parent->parent);
932 hlist_rotate_left(anchor, n->parent->parent);
938 * Hilight every character in a range of displayed characters.
941 create_hilites(linepos, start_index, end_index, chpos)
950 /* Start the first hilite. */
951 hl.hl_startpos = linepos + chpos[start_index];
954 * Step through the displayed chars.
955 * If the source position (before cvt) of the char is one more
956 * than the source pos of the previous char (the usual case),
957 * just increase the size of the current hilite by one.
958 * Otherwise (there are backspaces or something involved),
959 * finish the current hilite and start a new one.
961 for (i = start_index+1; i <= end_index; i++)
963 if (chpos[i] != chpos[i-1] + 1 || i == end_index)
965 hl.hl_endpos = linepos + chpos[i-1] + 1;
966 add_hilite(&hilite_anchor, &hl);
967 /* Start new hilite unless this is the last char. */
970 hl.hl_startpos = linepos + chpos[i];
977 * Make a hilite for each string in a physical line which matches
978 * the current pattern.
979 * sp,ep delimit the first match already found.
982 hilite_line(linepos, line, line_len, chpos, sp, ep, cvt_ops)
992 char *line_end = line + line_len;
994 if (sp == NULL || ep == NULL)
997 * sp and ep delimit the first match in the line.
998 * Mark the corresponding file positions, then
999 * look for further matches and mark them.
1000 * {{ This technique, of calling match_pattern on subsequent
1001 * substrings of the line, may mark more than is correct
1002 * if the pattern starts with "^". This bug is fixed
1003 * for those regex functions that accept a notbol parameter
1004 * (currently POSIX, PCRE and V8-with-regexec2). }}
1008 create_hilites(linepos, sp-line, ep-line, chpos);
1010 * If we matched more than zero characters,
1011 * move to the first char after the string we matched.
1012 * If we matched zero, just move to the next char.
1016 else if (searchp != line_end)
1018 else /* end of line */
1020 } while (match_pattern(info_compiled(&search_info), search_info.text,
1021 searchp, line_end - searchp, &sp, &ep, 1, search_info.search_type));
1026 * Change the caseless-ness of searches.
1027 * Updates the internal search state to reflect a change in the -i flag.
1032 if (!is_ucase_pattern)
1034 * Pattern did not have uppercase.
1035 * Just set the search caselessness to the global caselessness.
1037 is_caseless = caseless;
1040 * Pattern did have uppercase.
1041 * Discard the pattern; we can't change search caselessness now.
1043 clear_pattern(&search_info);
1048 * Find matching text which is currently on screen and highlight it.
1053 struct scrpos scrpos;
1055 get_scrpos(&scrpos);
1056 if (scrpos.pos == NULL_POSITION)
1058 prep_hilite(scrpos.pos, position(BOTTOM_PLUS_ONE), -1);
1063 * Change highlighting parameters.
1069 * Erase any highlights currently on screen.
1074 if (hilite_search == OPT_ONPLUS)
1076 * Display highlights.
1083 * Figure out where to start a search.
1086 search_pos(search_type)
1095 * Start at the beginning (or end) of the file.
1096 * The empty_screen() case is mainly for
1097 * command line initiated searches;
1098 * for example, "+/xyz" on the command line.
1099 * Also for multi-file (SRCH_PAST_EOF) searches.
1101 if (search_type & SRCH_FORW)
1107 if (pos == NULL_POSITION)
1109 (void) ch_end_seek();
1118 if (how_search == OPT_ON)
1121 * Search does not include current screen.
1123 if (search_type & SRCH_FORW)
1124 linenum = BOTTOM_PLUS_ONE;
1127 } else if (how_search == OPT_ONPLUS && !(search_type & SRCH_AFTER_TARGET))
1130 * Search includes all of displayed screen.
1132 if (search_type & SRCH_FORW)
1135 linenum = BOTTOM_PLUS_ONE;
1139 * Search includes the part of current screen beyond the jump target.
1140 * It starts at the jump target (if searching backwards),
1141 * or at the jump target plus one (if forwards).
1143 linenum = jump_sline;
1144 if (search_type & SRCH_FORW)
1147 linenum = adjsline(linenum);
1148 pos = position(linenum);
1150 pos = forw_raw_line(pos, (char **)NULL, (int *)NULL);
1154 * If the line is empty, look around for a plausible starting place.
1156 if (search_type & SRCH_FORW)
1158 while (pos == NULL_POSITION)
1160 if (++linenum >= sc_height)
1162 pos = position(linenum);
1166 while (pos == NULL_POSITION)
1170 pos = position(linenum);
1177 * Search a subset of the file, specified by start/end position.
1180 search_range(pos, endpos, search_type, matches, maxlines, plinepos, pendpos)
1198 POSITION linepos, oldpos;
1200 linenum = find_linenum(pos);
1205 * Get lines until we find a matching one or until
1206 * we hit end-of-file (or beginning-of-file if we're
1207 * going backwards), or until we hit the end position.
1212 * A signal aborts the search.
1217 if ((endpos != NULL_POSITION && pos >= endpos) || maxlines == 0)
1220 * Reached end position without a match.
1222 if (pendpos != NULL)
1229 if (search_type & SRCH_FORW)
1232 * Read the next line, and save the
1233 * starting position of that line in linepos.
1236 pos = forw_raw_line(pos, &line, &line_len);
1242 * Read the previous line and save the
1243 * starting position of that line in linepos.
1245 pos = back_raw_line(pos, &line, &line_len);
1251 if (pos == NULL_POSITION)
1254 * Reached EOF/BOF without a match.
1256 if (pendpos != NULL)
1262 * If we're using line numbers, we might as well
1263 * remember the information we have now (the position
1264 * and line number of the current line).
1265 * Don't do it for every line because it slows down
1266 * the search. Remember the line number only if
1267 * we're "far" from the last place we remembered it.
1269 if (linenums && abs((int)(pos - oldpos)) > 2048)
1270 add_lnum(linenum, pos);
1273 if (is_filtered(linepos))
1277 * If it's a caseless search, convert the line to lowercase.
1278 * If we're doing backspace processing, delete backspaces.
1280 cvt_ops = get_cvt_ops();
1281 cvt_len = cvt_length(line_len, cvt_ops);
1282 cline = (char *) ecalloc(1, cvt_len);
1283 chpos = cvt_alloc_chpos(cvt_len);
1284 cvt_text(cline, line, chpos, &line_len, cvt_ops);
1288 * Check to see if the line matches the filter pattern.
1289 * If so, add an entry to the filter list.
1291 if (((search_type & SRCH_FIND_ALL) ||
1292 prep_startpos == NULL_POSITION ||
1293 linepos < prep_startpos || linepos >= prep_endpos) &&
1294 prev_pattern(&filter_info)) {
1295 int line_filter = match_pattern(info_compiled(&filter_info), filter_info.text,
1296 cline, line_len, &sp, &ep, 0, filter_info.search_type);
1300 hl.hl_startpos = linepos;
1302 add_hilite(&filter_anchor, &hl);
1309 * Test the next line to see if we have a match.
1310 * We are successful if we either want a match and got one,
1311 * or if we want a non-match and got one.
1313 if (prev_pattern(&search_info))
1315 line_match = match_pattern(info_compiled(&search_info), search_info.text,
1316 cline, line_len, &sp, &ep, 0, search_type);
1322 if (search_type & SRCH_FIND_ALL)
1326 * We are supposed to find all matches in the range.
1327 * Just add the matches in this line to the
1328 * hilite list and keep searching.
1330 hilite_line(linepos, cline, line_len, chpos, sp, ep, cvt_ops);
1332 } else if (--matches <= 0)
1335 * Found the one match we're looking for.
1339 if (hilite_search == OPT_ON)
1342 * Clear the hilite list and add only
1343 * the matches in this one line.
1346 hilite_line(linepos, cline, line_len, chpos, sp, ep, cvt_ops);
1351 if (plinepos != NULL)
1352 *plinepos = linepos;
1363 * search for a pattern in history. If found, compile that pattern.
1366 hist_pattern(search_type)
1372 set_mlist(ml_search, 0);
1373 pattern = cmd_lastpattern();
1374 if (pattern == NULL)
1377 if (set_pattern(&search_info, pattern, search_type) < 0)
1381 if (hilite_search == OPT_ONPLUS && !hide_hilite)
1386 #else /* CMD_HISTORY */
1388 #endif /* CMD_HISTORY */
1392 * Search for the n-th occurrence of a specified pattern,
1393 * either forward or backward.
1394 * Return the number of matches not yet found in this file
1395 * (that is, n minus the number of matches found).
1396 * Return -1 if the search should be aborted.
1397 * Caller may continue the search in another file
1398 * if less than n matches are found in this file.
1401 search(search_type, pattern, n)
1408 if (pattern == NULL || *pattern == '\0')
1411 * A null pattern means use the previously compiled pattern.
1413 search_type |= SRCH_AFTER_TARGET;
1414 if (!prev_pattern(&search_info) && !hist_pattern(search_type))
1416 error("No previous regular expression", NULL_PARG);
1419 if ((search_type & SRCH_NO_REGEX) !=
1420 (search_info.search_type & SRCH_NO_REGEX))
1422 error("Please re-enter search pattern", NULL_PARG);
1426 if (hilite_search == OPT_ON)
1429 * Erase the highlights currently on screen.
1430 * If the search fails, we'll redisplay them later.
1434 if (hilite_search == OPT_ONPLUS && hide_hilite)
1437 * Highlight any matches currently on screen,
1438 * before we actually start the search.
1448 * Compile the pattern.
1450 if (set_pattern(&search_info, pattern, search_type) < 0)
1456 * Erase the highlights currently on screen.
1457 * Also permanently delete them from the hilite list.
1463 if (hilite_search == OPT_ONPLUS)
1466 * Highlight any matches currently on screen,
1467 * before we actually start the search.
1475 * Figure out where to start the search.
1477 pos = search_pos(search_type);
1478 if (pos == NULL_POSITION)
1481 * Can't find anyplace to start searching from.
1483 if (search_type & SRCH_PAST_EOF)
1485 /* repaint(); -- why was this here? */
1486 error("Nothing to search", NULL_PARG);
1490 n = search_range(pos, NULL_POSITION, search_type, n, -1,
1491 &pos, (POSITION*)NULL);
1495 * Search was unsuccessful.
1498 if (hilite_search == OPT_ON && n > 0)
1500 * Redisplay old hilites.
1507 if (!(search_type & SRCH_NO_MOVE))
1510 * Go to the matching line.
1512 jump_loc(pos, jump_sline);
1516 if (hilite_search == OPT_ON)
1518 * Display new hilites in the matching line.
1528 * Prepare hilites in a given range of the file.
1530 * The pair (prep_startpos,prep_endpos) delimits a contiguous region
1531 * of the file that has been "prepared"; that is, scanned for matches for
1532 * the current search pattern, and hilites have been created for such matches.
1533 * If prep_startpos == NULL_POSITION, the prep region is empty.
1534 * If prep_endpos == NULL_POSITION, the prep region extends to EOF.
1535 * prep_hilite asks that the range (spos,epos) be covered by the prep region.
1538 prep_hilite(spos, epos, maxlines)
1543 POSITION nprep_startpos = prep_startpos;
1544 POSITION nprep_endpos = prep_endpos;
1551 * Search beyond where we're asked to search, so the prep region covers
1552 * more than we need. Do one big search instead of a bunch of small ones.
1554 #define SEARCH_MORE (3*size_linebuf)
1556 if (!prev_pattern(&search_info) && !is_filtering())
1560 * Make sure our prep region always starts at the beginning of
1561 * a line. (search_range takes care of the end boundary below.)
1563 spos = back_raw_line(spos+1, (char **)NULL, (int *)NULL);
1566 * If we're limited to a max number of lines, figure out the
1567 * file position we should stop at.
1570 max_epos = NULL_POSITION;
1574 for (i = 0; i < maxlines; i++)
1575 max_epos = forw_raw_line(max_epos, (char **)NULL, (int *)NULL);
1580 * The range that we need to search (spos,epos); and the range that
1581 * the "prep" region will then cover (nprep_startpos,nprep_endpos).
1584 if (prep_startpos == NULL_POSITION ||
1585 (epos != NULL_POSITION && epos < prep_startpos) ||
1589 * New range is not contiguous with old prep region.
1590 * Discard the old prep region and start a new one.
1594 if (epos != NULL_POSITION)
1595 epos += SEARCH_MORE;
1596 nprep_startpos = spos;
1600 * New range partially or completely overlaps old prep region.
1602 if (epos == NULL_POSITION)
1605 * New range goes to end of file.
1608 } else if (epos > prep_endpos)
1611 * New range ends after old prep region.
1612 * Extend prep region to end at end of new range.
1614 epos += SEARCH_MORE;
1615 } else /* (epos <= prep_endpos) */
1618 * New range ends within old prep region.
1619 * Truncate search to end at start of old prep region.
1621 epos = prep_startpos;
1624 if (spos < prep_startpos)
1627 * New range starts before old prep region.
1628 * Extend old prep region backwards to start at
1629 * start of new range.
1631 if (spos < SEARCH_MORE)
1634 spos -= SEARCH_MORE;
1635 nprep_startpos = spos;
1636 } else /* (spos >= prep_startpos) */
1639 * New range starts within or after old prep region.
1640 * Trim search to start at end of old prep region.
1646 if (epos != NULL_POSITION && max_epos != NULL_POSITION &&
1649 * Don't go past the max position we're allowed.
1653 if (epos == NULL_POSITION || epos > spos)
1655 int search_type = SRCH_FORW | SRCH_FIND_ALL;
1656 search_type |= (search_info.search_type & SRCH_NO_REGEX);
1659 result = search_range(spos, epos, search_type, 0, maxlines, (POSITION*)NULL, &new_epos);
1662 if (prep_endpos == NULL_POSITION || new_epos > prep_endpos)
1663 nprep_endpos = new_epos;
1666 * Check both ends of the resulting prep region to
1667 * make sure they're not filtered. If they are,
1668 * keep going at least one more line until we find
1669 * something that isn't filtered, or hit the end.
1671 if (prep_endpos == NULL_POSITION || nprep_endpos > prep_endpos)
1673 if (new_epos >= nprep_endpos && is_filtered(new_epos-1))
1675 spos = nprep_endpos;
1676 epos = forw_raw_line(nprep_endpos, (char **)NULL, (int *)NULL);
1677 if (epos == NULL_POSITION)
1684 if (prep_startpos == NULL_POSITION || nprep_startpos < prep_startpos)
1686 if (nprep_startpos > 0 && is_filtered(nprep_startpos))
1688 epos = nprep_startpos;
1689 spos = back_raw_line(nprep_startpos, (char **)NULL, (int *)NULL);
1690 if (spos == NULL_POSITION)
1692 nprep_startpos = spos;
1700 prep_startpos = nprep_startpos;
1701 prep_endpos = nprep_endpos;
1705 * Set the pattern to be used for line filtering.
1708 set_filter_pattern(pattern, search_type)
1713 if (pattern == NULL || *pattern == '\0')
1714 clear_pattern(&filter_info);
1716 set_pattern(&filter_info, pattern, search_type);
1721 * Is there a line filter in effect?
1726 if (ch_getflags() & CH_HELPFILE)
1728 return prev_pattern(&filter_info);
1734 * This function is called by the V8 regcomp to report
1735 * errors in regular expressions.
1737 public int reg_show_error = 1;
1745 if (!reg_show_error)