2 * Copyright (C) 1984-2017 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.
19 #define MINPOS(a,b) (((a) < (b)) ? (a) : (b))
20 #define MAXPOS(a,b) (((a) > (b)) ? (a) : (b))
23 extern int how_search;
27 extern int jump_sline;
30 extern int status_col;
31 extern void *ml_search;
32 extern POSITION start_attnpos;
33 extern POSITION end_attnpos;
35 extern int screen_trashed;
37 extern int hilite_search;
38 extern int size_linebuf;
40 extern int can_goto_line;
41 static int hide_hilite;
42 static POSITION prep_startpos;
43 static POSITION prep_endpos;
44 static int is_caseless;
45 static int is_ucase_pattern;
48 * Structures for maintaining a set of ranges for hilites and filtered-out
49 * lines. Each range is stored as a node within a red-black tree, and we
50 * try to extend existing ranges (without creating overlaps) rather than
51 * create new nodes if possible. We remember the last node found by a
52 * search for constant-time lookup if the next search is near enough to
53 * the previous. To aid that, we overlay a secondary doubly-linked list
54 * on top of the red-black tree so we can find the preceding/succeeding
55 * nodes also in constant time.
57 * Each node is allocated from a series of pools, each pool double the size
58 * of the previous (for amortised constant time allocation). Since our only
59 * tree operations are clear and node insertion, not node removal, we don't
60 * need to maintain a usage bitmap or freelist and can just return nodes
61 * from the pool in-order until capacity is reached.
70 struct hilite_node *parent;
71 struct hilite_node *left;
72 struct hilite_node *right;
73 struct hilite_node *prev;
74 struct hilite_node *next;
82 struct hilite_storage *next;
83 struct hilite_node *nodes;
87 struct hilite_storage *first;
88 struct hilite_storage *current;
89 struct hilite_node *root;
90 struct hilite_node *lookaside;
92 #define HILITE_INITIALIZER() { NULL, NULL, NULL, NULL }
93 #define HILITE_LOOKASIDE_STEPS 2
95 static struct hilite_tree hilite_anchor = HILITE_INITIALIZER();
96 static struct hilite_tree filter_anchor = HILITE_INITIALIZER();
101 * These are the static variables that represent the "remembered"
102 * search pattern and filter pattern.
104 struct pattern_info {
105 PATTERN_TYPE compiled;
111 #define info_compiled(info) ((void*)0)
113 #define info_compiled(info) ((info)->compiled)
116 static struct pattern_info search_info;
117 static struct pattern_info filter_info;
120 * Are there any uppercase letters in this string?
126 char *str_end = str + strlen(str);
129 while (str < str_end)
131 ch = step_char(&str, +1, str_end);
139 * Compile and save a search pattern.
142 set_pattern(info, pattern, search_type)
143 struct pattern_info *info;
149 CLEAR_PATTERN(info->compiled);
150 else if (compile_pattern(pattern, search_type, &info->compiled) < 0)
153 /* Pattern compiled successfully; save the text too. */
154 if (info->text != NULL)
159 info->text = (char *) ecalloc(1, strlen(pattern)+1);
160 strcpy(info->text, pattern);
162 info->search_type = search_type;
165 * Ignore case if -I is set OR
166 * -i is set AND the pattern is all lowercase.
168 is_ucase_pattern = is_ucase(pattern);
169 if (is_ucase_pattern && caseless != OPT_ONPLUS)
172 is_caseless = caseless;
177 * Discard a saved pattern.
181 struct pattern_info *info;
183 if (info->text != NULL)
187 uncompile_pattern(&info->compiled);
192 * Initialize saved pattern to nothing.
196 struct pattern_info *info;
198 CLEAR_PATTERN(info->compiled);
200 info->search_type = 0;
204 * Initialize search variables.
209 init_pattern(&search_info);
210 init_pattern(&filter_info);
214 * Determine which text conversions to perform before pattern matching.
220 if (is_caseless || bs_mode == BS_SPECIAL)
224 if (bs_mode == BS_SPECIAL)
226 if (bs_mode != BS_CONTROL)
228 } else if (bs_mode != BS_CONTROL)
232 if (ctldisp == OPT_ONPLUS)
238 * Is there a previous (remembered) search pattern?
242 struct pattern_info *info;
245 if ((info->search_type & SRCH_NO_REGEX) == 0)
246 return (!is_null_pattern(info->compiled));
248 return (info->text != NULL);
253 * Repaint the hilites currently displayed on the screen.
254 * Repaint each line which contains highlighted text.
255 * If on==0, force all hilites off.
263 int save_hide_hilite;
268 save_hide_hilite = hide_hilite;
279 hide_hilite = save_hide_hilite;
283 for (slinenum = TOP; slinenum < TOP + sc_height-1; slinenum++)
285 pos = position(slinenum);
286 if (pos == NULL_POSITION)
288 (void) forw_line(pos);
293 hide_hilite = save_hide_hilite;
297 * Clear the attn hilite.
303 POSITION old_start_attnpos;
304 POSITION old_end_attnpos;
309 if (start_attnpos == NULL_POSITION)
311 old_start_attnpos = start_attnpos;
312 old_end_attnpos = end_attnpos;
313 start_attnpos = end_attnpos = NULL_POSITION;
323 for (slinenum = TOP; slinenum < TOP + sc_height-1; slinenum++)
325 pos = position(slinenum);
326 if (pos == NULL_POSITION)
328 epos = position(slinenum+1);
329 if (pos < old_end_attnpos &&
330 (epos == NULL_POSITION || epos > old_start_attnpos))
332 (void) forw_line(pos);
344 * Hide search string highlighting.
349 if (!prev_pattern(&search_info))
351 error("No previous regular expression", NULL_PARG);
355 hide_hilite = !hide_hilite;
362 * Clear the hilite list.
366 struct hilite_tree *anchor;
368 struct hilite_storage *hls;
369 struct hilite_storage *nexthls;
371 for (hls = anchor->first; hls != NULL; hls = nexthls)
374 free((void*)hls->nodes);
377 anchor->first = NULL;
378 anchor->current = NULL;
381 anchor->lookaside = NULL;
383 prep_startpos = prep_endpos = NULL_POSITION;
389 clr_hlist(&hilite_anchor);
395 clr_hlist(&filter_anchor);
400 struct hilite_tree *anchor;
402 struct hilite_node *n = anchor->root;
403 while (n != NULL && n->right != NULL)
410 struct hilite_node *n;
417 struct hilite_node *n;
423 * Find the node covering pos, or the node after it if no node covers it,
424 * or return NULL if pos is after the last range. Remember the found node,
425 * to speed up subsequent searches for the same or similar positions (if
426 * we return NULL, remember the last node.)
429 hlist_find(anchor, pos)
430 struct hilite_tree *anchor;
433 struct hilite_node *n, *m;
435 if (anchor->lookaside)
440 n = anchor->lookaside;
444 if (pos < n->r.hl_endpos)
446 if (n->prev == NULL || pos >= n->prev->r.hl_endpos)
451 } else if (n->next == NULL)
459 * If we don't find the right node within a small
460 * distance, don't keep doing a linear search!
462 if (steps >= HILITE_LOOKASIDE_STEPS)
466 if (pos < n->r.hl_endpos)
467 anchor->lookaside = n = n->prev;
469 anchor->lookaside = n = n->next;
481 if (pos < n->r.hl_startpos)
491 if (pos >= n->r.hl_endpos)
493 if (n->right != NULL)
511 anchor->lookaside = n;
513 anchor->lookaside = m;
519 * Should any characters in a specified range be highlighted?
522 is_hilited_range(pos, epos)
526 struct hilite_node *n = hlist_find(&hilite_anchor, pos);
527 return (n != NULL && (epos == NULL_POSITION || epos > n->r.hl_startpos));
531 * Is a line "filtered" -- that is, should it be hidden?
537 struct hilite_node *n;
539 if (ch_getflags() & CH_HELPFILE)
542 n = hlist_find(&filter_anchor, pos);
543 return (n != NULL && pos >= n->r.hl_startpos);
547 * If pos is hidden, return the next position which isn't, otherwise
554 struct hilite_node *n;
556 if (ch_getflags() & CH_HELPFILE)
559 n = hlist_find(&filter_anchor, pos);
560 while (n != NULL && pos >= n->r.hl_startpos)
562 pos = n->r.hl_endpos;
569 * If pos is hidden, return the previous position which isn't or 0 if
570 * we're filtered right to the beginning, otherwise just return pos.
576 struct hilite_node *n;
578 if (ch_getflags() & CH_HELPFILE)
581 n = hlist_find(&filter_anchor, pos);
582 while (n != NULL && pos >= n->r.hl_startpos)
584 pos = n->r.hl_startpos;
595 * Should any characters in a specified range be highlighted?
596 * If nohide is nonzero, don't consider hide_hilite.
599 is_hilited(pos, epos, nohide, p_matches)
607 if (p_matches != NULL)
611 start_attnpos != NULL_POSITION &&
613 (epos == NULL_POSITION || epos > start_attnpos))
615 * The attn line overlaps this range.
619 match = is_hilited_range(pos, epos);
623 if (p_matches != NULL)
625 * Report matches, even if we're hiding highlights.
629 if (hilite_search == 0)
631 * Not doing highlighting.
635 if (!nohide && hide_hilite)
637 * Highlighting is hidden.
645 * Tree node storage: get the current block of nodes if it has spare
646 * capacity, or create a new one if not.
648 static struct hilite_storage*
649 hlist_getstorage(anchor)
650 struct hilite_tree *anchor;
653 struct hilite_storage *s;
657 if (anchor->current->used < anchor->current->capacity)
658 return anchor->current;
659 capacity = anchor->current->capacity * 2;
662 s = (struct hilite_storage *) ecalloc(1, sizeof(struct hilite_storage));
663 s->nodes = (struct hilite_node *) ecalloc(capacity, sizeof(struct hilite_node));
664 s->capacity = capacity;
668 anchor->current->next = s;
676 * Tree node storage: retrieve a new empty node to be inserted into the
679 static struct hilite_node*
680 hlist_getnode(anchor)
681 struct hilite_tree *anchor;
683 struct hilite_storage *s = hlist_getstorage(anchor);
684 return &s->nodes[s->used++];
688 * Rotate the tree left around a pivot node.
691 hlist_rotate_left(anchor, n)
692 struct hilite_tree *anchor;
693 struct hilite_node *n;
695 struct hilite_node *np = n->parent;
696 struct hilite_node *nr = n->right;
697 struct hilite_node *nrl = n->right->left;
719 * Rotate the tree right around a pivot node.
722 hlist_rotate_right(anchor, n)
723 struct hilite_tree *anchor;
724 struct hilite_node *n;
726 struct hilite_node *np = n->parent;
727 struct hilite_node *nl = n->left;
728 struct hilite_node *nlr = n->left->right;
751 * Add a new hilite to a hilite list.
754 add_hilite(anchor, hl)
755 struct hilite_tree *anchor;
758 struct hilite_node *p, *n, *u;
760 /* Ignore empty ranges. */
761 if (hl->hl_startpos >= hl->hl_endpos)
766 /* Inserting the very first node is trivial. */
769 n = hlist_getnode(anchor);
772 anchor->lookaside = n;
777 * Find our insertion point. If we come across any overlapping
778 * or adjoining existing ranges, shrink our range and discard
779 * if it become empty.
783 if (hl->hl_startpos < p->r.hl_startpos)
785 if (hl->hl_endpos > p->r.hl_startpos)
786 hl->hl_endpos = p->r.hl_startpos;
794 if (hl->hl_startpos < p->r.hl_endpos) {
795 hl->hl_startpos = p->r.hl_endpos;
796 if (hl->hl_startpos >= hl->hl_endpos)
799 if (p->right != NULL)
808 * Now we're at the right leaf, again check for contiguous ranges
809 * and extend the existing node if possible to avoid the
810 * insertion. Otherwise insert a new node at the leaf.
812 if (hl->hl_startpos < p->r.hl_startpos) {
813 if (hl->hl_endpos == p->r.hl_startpos)
815 p->r.hl_startpos = hl->hl_startpos;
818 if (p->prev != NULL && p->prev->r.hl_endpos == hl->hl_startpos)
820 p->prev->r.hl_endpos = hl->hl_endpos;
824 p->left = n = hlist_getnode(anchor);
833 if (p->r.hl_endpos == hl->hl_startpos)
835 p->r.hl_endpos = hl->hl_endpos;
838 if (p->next != NULL && hl->hl_endpos == p->next->r.hl_startpos) {
839 p->next->r.hl_startpos = hl->hl_startpos;
843 p->right = n = hlist_getnode(anchor);
857 * The tree is in the correct order and covers the right ranges
858 * now, but may have become unbalanced. Rebalance it using the
859 * standard red-black tree constraints and operations.
863 /* case 1 - current is root, root is always black */
864 if (n->parent == NULL)
870 /* case 2 - parent is black, we can always be red */
875 * constraint: because the root must be black, if our
876 * parent is red it cannot be the root therefore we must
881 * case 3 - parent and uncle are red, repaint them black,
882 * the grandparent red, and start again at the grandparent.
884 u = n->parent->parent->left;
886 u = n->parent->parent->right;
887 if (u != NULL && u->red)
891 n = n->parent->parent;
897 * case 4 - parent is red but uncle is black, parent and
898 * grandparent on opposite sides. We need to start
899 * changing the structure now. This and case 5 will shorten
900 * our branch and lengthen the sibling, between them
903 if (n == n->parent->right &&
904 n->parent == n->parent->parent->left)
906 hlist_rotate_left(anchor, n->parent);
908 } else if (n == n->parent->left &&
909 n->parent == n->parent->parent->right)
911 hlist_rotate_right(anchor, n->parent);
916 * case 5 - parent is red but uncle is black, parent and
917 * grandparent on same side
920 n->parent->parent->red = 1;
921 if (n == n->parent->left)
922 hlist_rotate_right(anchor, n->parent->parent);
924 hlist_rotate_left(anchor, n->parent->parent);
930 * Hilight every character in a range of displayed characters.
933 create_hilites(linepos, start_index, end_index, chpos)
942 /* Start the first hilite. */
943 hl.hl_startpos = linepos + chpos[start_index];
946 * Step through the displayed chars.
947 * If the source position (before cvt) of the char is one more
948 * than the source pos of the previous char (the usual case),
949 * just increase the size of the current hilite by one.
950 * Otherwise (there are backspaces or something involved),
951 * finish the current hilite and start a new one.
953 for (i = start_index+1; i <= end_index; i++)
955 if (chpos[i] != chpos[i-1] + 1 || i == end_index)
957 hl.hl_endpos = linepos + chpos[i-1] + 1;
958 add_hilite(&hilite_anchor, &hl);
959 /* Start new hilite unless this is the last char. */
962 hl.hl_startpos = linepos + chpos[i];
969 * Make a hilite for each string in a physical line which matches
970 * the current pattern.
971 * sp,ep delimit the first match already found.
974 hilite_line(linepos, line, line_len, chpos, sp, ep, cvt_ops)
984 char *line_end = line + line_len;
987 * sp and ep delimit the first match in the line.
988 * Mark the corresponding file positions, then
989 * look for further matches and mark them.
990 * {{ This technique, of calling match_pattern on subsequent
991 * substrings of the line, may mark more than is correct
992 * if the pattern starts with "^". This bug is fixed
993 * for those regex functions that accept a notbol parameter
994 * (currently POSIX, PCRE and V8-with-regexec2). }}
998 if (sp == NULL || ep == NULL)
1000 create_hilites(linepos, sp-line, ep-line, chpos);
1002 * If we matched more than zero characters,
1003 * move to the first char after the string we matched.
1004 * If we matched zero, just move to the next char.
1008 else if (searchp != line_end)
1010 else /* end of line */
1012 } while (match_pattern(info_compiled(&search_info), search_info.text,
1013 searchp, line_end - searchp, &sp, &ep, 1, search_info.search_type));
1019 * Find matching text which is currently on screen and highlight it.
1024 struct scrpos scrpos;
1026 get_scrpos(&scrpos);
1027 if (scrpos.pos == NULL_POSITION)
1029 prep_hilite(scrpos.pos, position(BOTTOM_PLUS_ONE), -1);
1034 * Change highlighting parameters.
1040 * Erase any highlights currently on screen.
1045 if (hilite_search == OPT_ONPLUS)
1047 * Display highlights.
1054 * Figure out where to start a search.
1057 search_pos(search_type)
1066 * Start at the beginning (or end) of the file.
1067 * The empty_screen() case is mainly for
1068 * command line initiated searches;
1069 * for example, "+/xyz" on the command line.
1070 * Also for multi-file (SRCH_PAST_EOF) searches.
1072 if (search_type & SRCH_FORW)
1078 if (pos == NULL_POSITION)
1080 (void) ch_end_seek();
1089 if (how_search == OPT_ON)
1092 * Search does not include current screen.
1094 if (search_type & SRCH_FORW)
1095 linenum = sc_height-1; /* BOTTOM_PLUS_ONE */
1097 linenum = 0; /* TOP */
1098 } else if (how_search == OPT_ONPLUS && !(search_type & SRCH_AFTER_TARGET))
1101 * Search includes all of displayed screen.
1103 if (search_type & SRCH_FORW)
1104 linenum = 0; /* TOP */
1106 linenum = sc_height-1; /* BOTTOM_PLUS_ONE */
1110 * Search includes the part of current screen beyond the jump target.
1111 * It starts at the jump target (if searching backwards),
1112 * or at the jump target plus one (if forwards).
1114 linenum = adjsline(jump_sline);
1115 if (search_type & SRCH_FORW)
1118 pos = position(linenum);
1120 pos = forw_raw_line(pos, (char **)NULL, (int *)NULL);
1124 * If the line is empty, look around for a plausible starting place.
1126 if (search_type & SRCH_FORW)
1128 while (pos == NULL_POSITION)
1130 if (++linenum >= sc_height)
1132 pos = position(linenum);
1136 while (pos == NULL_POSITION)
1140 pos = position(linenum);
1147 * Search a subset of the file, specified by start/end position.
1150 search_range(pos, endpos, search_type, matches, maxlines, plinepos, pendpos)
1168 POSITION linepos, oldpos;
1170 linenum = find_linenum(pos);
1175 * Get lines until we find a matching one or until
1176 * we hit end-of-file (or beginning-of-file if we're
1177 * going backwards), or until we hit the end position.
1182 * A signal aborts the search.
1187 if ((endpos != NULL_POSITION && pos >= endpos) || maxlines == 0)
1190 * Reached end position without a match.
1192 if (pendpos != NULL)
1199 if (search_type & SRCH_FORW)
1202 * Read the next line, and save the
1203 * starting position of that line in linepos.
1206 pos = forw_raw_line(pos, &line, &line_len);
1212 * Read the previous line and save the
1213 * starting position of that line in linepos.
1215 pos = back_raw_line(pos, &line, &line_len);
1221 if (pos == NULL_POSITION)
1224 * Reached EOF/BOF without a match.
1226 if (pendpos != NULL)
1232 * If we're using line numbers, we might as well
1233 * remember the information we have now (the position
1234 * and line number of the current line).
1235 * Don't do it for every line because it slows down
1236 * the search. Remember the line number only if
1237 * we're "far" from the last place we remembered it.
1239 if (linenums && abs((int)(pos - oldpos)) > 2048)
1240 add_lnum(linenum, pos);
1243 if (is_filtered(linepos))
1247 * If it's a caseless search, convert the line to lowercase.
1248 * If we're doing backspace processing, delete backspaces.
1250 cvt_ops = get_cvt_ops();
1251 cvt_len = cvt_length(line_len, cvt_ops);
1252 cline = (char *) ecalloc(1, cvt_len);
1253 chpos = cvt_alloc_chpos(cvt_len);
1254 cvt_text(cline, line, chpos, &line_len, cvt_ops);
1258 * Check to see if the line matches the filter pattern.
1259 * If so, add an entry to the filter list.
1261 if (((search_type & SRCH_FIND_ALL) ||
1262 prep_startpos == NULL_POSITION ||
1263 linepos < prep_startpos || linepos >= prep_endpos) &&
1264 prev_pattern(&filter_info)) {
1265 int line_filter = match_pattern(info_compiled(&filter_info), filter_info.text,
1266 cline, line_len, &sp, &ep, 0, filter_info.search_type);
1270 hl.hl_startpos = linepos;
1272 add_hilite(&filter_anchor, &hl);
1281 * Test the next line to see if we have a match.
1282 * We are successful if we either want a match and got one,
1283 * or if we want a non-match and got one.
1285 if (prev_pattern(&search_info))
1287 line_match = match_pattern(info_compiled(&search_info), search_info.text,
1288 cline, line_len, &sp, &ep, 0, search_type);
1294 if (search_type & SRCH_FIND_ALL)
1298 * We are supposed to find all matches in the range.
1299 * Just add the matches in this line to the
1300 * hilite list and keep searching.
1302 hilite_line(linepos, cline, line_len, chpos, sp, ep, cvt_ops);
1304 } else if (--matches <= 0)
1307 * Found the one match we're looking for.
1311 if (hilite_search == OPT_ON)
1314 * Clear the hilite list and add only
1315 * the matches in this one line.
1318 hilite_line(linepos, cline, line_len, chpos, sp, ep, cvt_ops);
1323 if (plinepos != NULL)
1324 *plinepos = linepos;
1335 * search for a pattern in history. If found, compile that pattern.
1338 hist_pattern(search_type)
1344 set_mlist(ml_search, 0);
1345 pattern = cmd_lastpattern();
1346 if (pattern == NULL)
1349 if (set_pattern(&search_info, pattern, search_type) < 0)
1353 if (hilite_search == OPT_ONPLUS && !hide_hilite)
1358 #else /* CMD_HISTORY */
1360 #endif /* CMD_HISTORY */
1364 * Change the caseless-ness of searches.
1365 * Updates the internal search state to reflect a change in the -i flag.
1370 if (!is_ucase_pattern)
1372 * Pattern did not have uppercase.
1373 * Just set the search caselessness to the global caselessness.
1375 is_caseless = caseless;
1379 * Pattern did have uppercase.
1380 * Regenerate the pattern using the new state.
1382 clear_pattern(&search_info);
1383 hist_pattern(search_info.search_type);
1388 * Search for the n-th occurrence of a specified pattern,
1389 * either forward or backward.
1390 * Return the number of matches not yet found in this file
1391 * (that is, n minus the number of matches found).
1392 * Return -1 if the search should be aborted.
1393 * Caller may continue the search in another file
1394 * if less than n matches are found in this file.
1397 search(search_type, pattern, n)
1404 if (pattern == NULL || *pattern == '\0')
1407 * A null pattern means use the previously compiled pattern.
1409 search_type |= SRCH_AFTER_TARGET;
1410 if (!prev_pattern(&search_info) && !hist_pattern(search_type))
1412 error("No previous regular expression", NULL_PARG);
1415 if ((search_type & SRCH_NO_REGEX) !=
1416 (search_info.search_type & SRCH_NO_REGEX))
1418 error("Please re-enter search pattern", NULL_PARG);
1422 if (hilite_search == OPT_ON)
1425 * Erase the highlights currently on screen.
1426 * If the search fails, we'll redisplay them later.
1430 if (hilite_search == OPT_ONPLUS && hide_hilite)
1433 * Highlight any matches currently on screen,
1434 * before we actually start the search.
1444 * Compile the pattern.
1446 if (set_pattern(&search_info, pattern, search_type) < 0)
1452 * Erase the highlights currently on screen.
1453 * Also permanently delete them from the hilite list.
1459 if (hilite_search == OPT_ONPLUS)
1462 * Highlight any matches currently on screen,
1463 * before we actually start the search.
1471 * Figure out where to start the search.
1473 pos = search_pos(search_type);
1474 if (pos == NULL_POSITION)
1477 * Can't find anyplace to start searching from.
1479 if (search_type & SRCH_PAST_EOF)
1481 /* repaint(); -- why was this here? */
1482 error("Nothing to search", NULL_PARG);
1486 n = search_range(pos, NULL_POSITION, search_type, n, -1,
1487 &pos, (POSITION*)NULL);
1491 * Search was unsuccessful.
1494 if (hilite_search == OPT_ON && n > 0)
1496 * Redisplay old hilites.
1503 if (!(search_type & SRCH_NO_MOVE))
1506 * Go to the matching line.
1508 jump_loc(pos, jump_sline);
1512 if (hilite_search == OPT_ON)
1514 * Display new hilites in the matching line.
1524 * Prepare hilites in a given range of the file.
1526 * The pair (prep_startpos,prep_endpos) delimits a contiguous region
1527 * of the file that has been "prepared"; that is, scanned for matches for
1528 * the current search pattern, and hilites have been created for such matches.
1529 * If prep_startpos == NULL_POSITION, the prep region is empty.
1530 * If prep_endpos == NULL_POSITION, the prep region extends to EOF.
1531 * prep_hilite asks that the range (spos,epos) be covered by the prep region.
1534 prep_hilite(spos, epos, maxlines)
1539 POSITION nprep_startpos = prep_startpos;
1540 POSITION nprep_endpos = prep_endpos;
1547 * Search beyond where we're asked to search, so the prep region covers
1548 * more than we need. Do one big search instead of a bunch of small ones.
1550 #define SEARCH_MORE (3*size_linebuf)
1552 if (!prev_pattern(&search_info) && !is_filtering())
1556 * Make sure our prep region always starts at the beginning of
1557 * a line. (search_range takes care of the end boundary below.)
1559 spos = back_raw_line(spos+1, (char **)NULL, (int *)NULL);
1562 * If we're limited to a max number of lines, figure out the
1563 * file position we should stop at.
1566 max_epos = NULL_POSITION;
1570 for (i = 0; i < maxlines; i++)
1571 max_epos = forw_raw_line(max_epos, (char **)NULL, (int *)NULL);
1576 * The range that we need to search (spos,epos); and the range that
1577 * the "prep" region will then cover (nprep_startpos,nprep_endpos).
1580 if (prep_startpos == NULL_POSITION ||
1581 (epos != NULL_POSITION && epos < prep_startpos) ||
1585 * New range is not contiguous with old prep region.
1586 * Discard the old prep region and start a new one.
1590 if (epos != NULL_POSITION)
1591 epos += SEARCH_MORE;
1592 nprep_startpos = spos;
1596 * New range partially or completely overlaps old prep region.
1598 if (epos == NULL_POSITION)
1601 * New range goes to end of file.
1604 } else if (epos > prep_endpos)
1607 * New range ends after old prep region.
1608 * Extend prep region to end at end of new range.
1610 epos += SEARCH_MORE;
1611 } else /* (epos <= prep_endpos) */
1614 * New range ends within old prep region.
1615 * Truncate search to end at start of old prep region.
1617 epos = prep_startpos;
1620 if (spos < prep_startpos)
1623 * New range starts before old prep region.
1624 * Extend old prep region backwards to start at
1625 * start of new range.
1627 if (spos < SEARCH_MORE)
1630 spos -= SEARCH_MORE;
1631 nprep_startpos = spos;
1632 } else /* (spos >= prep_startpos) */
1635 * New range starts within or after old prep region.
1636 * Trim search to start at end of old prep region.
1642 if (epos != NULL_POSITION && max_epos != NULL_POSITION &&
1645 * Don't go past the max position we're allowed.
1649 if (epos == NULL_POSITION || epos > spos)
1651 int search_type = SRCH_FORW | SRCH_FIND_ALL;
1652 search_type |= (search_info.search_type & SRCH_NO_REGEX);
1655 result = search_range(spos, epos, search_type, 0, maxlines, (POSITION*)NULL, &new_epos);
1658 if (prep_endpos == NULL_POSITION || new_epos > prep_endpos)
1659 nprep_endpos = new_epos;
1662 * Check both ends of the resulting prep region to
1663 * make sure they're not filtered. If they are,
1664 * keep going at least one more line until we find
1665 * something that isn't filtered, or hit the end.
1667 if (prep_endpos == NULL_POSITION || nprep_endpos > prep_endpos)
1669 if (new_epos >= nprep_endpos && is_filtered(new_epos-1))
1671 spos = nprep_endpos;
1672 epos = forw_raw_line(nprep_endpos, (char **)NULL, (int *)NULL);
1673 if (epos == NULL_POSITION)
1680 if (prep_startpos == NULL_POSITION || nprep_startpos < prep_startpos)
1682 if (nprep_startpos > 0 && is_filtered(nprep_startpos))
1684 epos = nprep_startpos;
1685 spos = back_raw_line(nprep_startpos, (char **)NULL, (int *)NULL);
1686 if (spos == NULL_POSITION)
1688 nprep_startpos = spos;
1696 prep_startpos = nprep_startpos;
1697 prep_endpos = nprep_endpos;
1701 * Set the pattern to be used for line filtering.
1704 set_filter_pattern(pattern, search_type)
1709 if (pattern == NULL || *pattern == '\0')
1710 clear_pattern(&filter_info);
1712 set_pattern(&filter_info, pattern, search_type);
1717 * Is there a line filter in effect?
1722 if (ch_getflags() & CH_HELPFILE)
1724 return prev_pattern(&filter_info);
1730 * This function is called by the V8 regcomp to report
1731 * errors in regular expressions.
1733 public int reg_show_error = 1;
1741 if (!reg_show_error)