2 * Copyright (C) 1984-2015 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;
83 struct hilite_storage *next;
84 struct hilite_node *nodes;
88 struct hilite_storage *first;
89 struct hilite_storage *current;
90 struct hilite_node *root;
91 struct hilite_node *lookaside;
93 #define HILITE_INITIALIZER() { NULL, NULL, NULL, NULL }
94 #define HILITE_LOOKASIDE_STEPS 2
96 static struct hilite_tree hilite_anchor = HILITE_INITIALIZER();
97 static struct hilite_tree filter_anchor = HILITE_INITIALIZER();
102 * These are the static variables that represent the "remembered"
103 * search pattern and filter pattern.
105 struct pattern_info {
106 DEFINE_PATTERN(compiled);
112 #define info_compiled(info) ((void*)0)
114 #define info_compiled(info) ((info)->compiled)
117 static struct pattern_info search_info;
118 static struct pattern_info filter_info;
121 * Are there any uppercase letters in this string?
124 is_ucase(constant char *str)
126 constant 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(struct pattern_info *info, char *pattern, int search_type)
146 CLEAR_PATTERN(info->compiled);
147 else if (compile_pattern(pattern, search_type,
148 (void **)&info->compiled) < 0)
151 /* Pattern compiled successfully; save the text too. */
152 if (info->text != NULL)
157 info->text = (char *) ecalloc(1, strlen(pattern)+1);
158 strcpy(info->text, pattern);
160 info->search_type = search_type;
163 * Ignore case if -I is set OR
164 * -i is set AND the pattern is all lowercase.
166 is_ucase_pattern = is_ucase(pattern);
167 if (is_ucase_pattern && caseless != OPT_ONPLUS)
170 is_caseless = caseless;
175 * Discard a saved pattern.
178 clear_pattern(struct pattern_info *info)
180 if (info->text != NULL)
184 uncompile_pattern((void **)&info->compiled);
189 * Initialize saved pattern to nothing.
192 init_pattern(struct pattern_info *info)
194 CLEAR_PATTERN(info->compiled);
196 info->search_type = 0;
200 * Initialize search variables.
205 init_pattern(&search_info);
206 init_pattern(&filter_info);
210 * Determine which text conversions to perform before pattern matching.
216 if (is_caseless || bs_mode == BS_SPECIAL)
220 if (bs_mode == BS_SPECIAL)
222 if (bs_mode != BS_CONTROL)
224 } else if (bs_mode != BS_CONTROL)
228 if (ctldisp == OPT_ONPLUS)
234 * Is there a previous (remembered) search pattern?
237 prev_pattern(struct pattern_info *info)
240 if ((info->search_type & SRCH_NO_REGEX) == 0)
241 return (!is_null_pattern(info->compiled));
243 return (info->text != NULL);
248 * Repaint the hilites currently displayed on the screen.
249 * Repaint each line which contains highlighted text.
250 * If on==0, force all hilites off.
253 repaint_hilite(int on)
257 int save_hide_hilite;
262 save_hide_hilite = hide_hilite;
273 hide_hilite = save_hide_hilite;
277 for (slinenum = TOP; slinenum < TOP + sc_height-1; slinenum++)
279 pos = position(slinenum);
280 if (pos == NULL_POSITION)
282 (void) forw_line(pos);
287 hide_hilite = save_hide_hilite;
291 * Clear the attn hilite.
297 POSITION old_start_attnpos;
298 POSITION old_end_attnpos;
303 if (start_attnpos == NULL_POSITION)
305 old_start_attnpos = start_attnpos;
306 old_end_attnpos = end_attnpos;
307 start_attnpos = end_attnpos = NULL_POSITION;
317 for (slinenum = TOP; slinenum < TOP + sc_height-1; slinenum++)
319 pos = position(slinenum);
320 if (pos == NULL_POSITION)
322 epos = position(slinenum+1);
323 if (pos < old_end_attnpos &&
324 (epos == NULL_POSITION || epos > old_start_attnpos))
326 (void) forw_line(pos);
338 * Hide search string highlighting.
343 if (!prev_pattern(&search_info))
345 error("No previous regular expression", NULL_PARG);
349 hide_hilite = !hide_hilite;
356 * Clear the hilite list.
359 clr_hlist(struct hilite_tree *anchor)
361 struct hilite_storage *hls;
362 struct hilite_storage *nexthls;
364 for (hls = anchor->first; hls != NULL; hls = nexthls)
367 free((void*)hls->nodes);
370 anchor->first = NULL;
371 anchor->current = NULL;
374 anchor->lookaside = NULL;
376 prep_startpos = prep_endpos = NULL_POSITION;
382 clr_hlist(&hilite_anchor);
388 clr_hlist(&filter_anchor);
393 struct hilite_tree *anchor;
395 struct hilite_node *n = anchor->root;
396 while (n != NULL && n->right != NULL)
403 struct hilite_node *n;
410 struct hilite_node *n;
416 * Find the node covering pos, or the node after it if no node covers it,
417 * or return NULL if pos is after the last range. Remember the found node,
418 * to speed up subsequent searches for the same or similar positions (if
419 * we return NULL, remember the last node.)
422 hlist_find(anchor, pos)
423 struct hilite_tree *anchor;
426 struct hilite_node *n, *m;
428 if (anchor->lookaside)
433 n = anchor->lookaside;
437 if (pos < n->r.hl_endpos)
439 if (n->prev == NULL || pos >= n->prev->r.hl_endpos)
444 } else if (n->next == NULL)
452 * If we don't find the right node within a small
453 * distance, don't keep doing a linear search!
455 if (steps >= HILITE_LOOKASIDE_STEPS)
459 if (pos < n->r.hl_endpos)
460 anchor->lookaside = n = n->prev;
462 anchor->lookaside = n = n->next;
474 if (pos < n->r.hl_startpos)
484 if (pos >= n->r.hl_endpos)
486 if (n->right != NULL)
504 anchor->lookaside = n;
506 anchor->lookaside = m;
512 * Should any characters in a specified range be highlighted?
515 is_hilited_range(POSITION pos, POSITION epos)
517 struct hilite_node *n = hlist_find(&hilite_anchor, pos);
518 return (n != NULL && (epos == NULL_POSITION || epos > n->r.hl_startpos));
522 * Is a line "filtered" -- that is, should it be hidden?
525 is_filtered(POSITION pos)
527 struct hilite_node *n;
529 if (ch_getflags() & CH_HELPFILE)
532 n = hlist_find(&filter_anchor, pos);
533 return (n != NULL && pos >= n->r.hl_startpos);
537 * If pos is hidden, return the next position which isn't, otherwise
541 next_unfiltered(POSITION pos)
543 struct hilite_node *n;
545 if (ch_getflags() & CH_HELPFILE)
548 n = hlist_find(&filter_anchor, pos);
549 while (n != NULL && pos >= n->r.hl_startpos)
551 pos = n->r.hl_endpos;
558 * If pos is hidden, return the previous position which isn't or 0 if
559 * we're filtered right to the beginning, otherwise just return pos.
562 prev_unfiltered(POSITION pos)
564 struct hilite_node *n;
566 if (ch_getflags() & CH_HELPFILE)
569 n = hlist_find(&filter_anchor, pos);
570 while (n != NULL && pos >= n->r.hl_startpos)
572 pos = n->r.hl_startpos;
583 * Should any characters in a specified range be highlighted?
584 * If nohide is nonzero, don't consider hide_hilite.
587 is_hilited(POSITION pos, POSITION epos, int nohide, int *p_matches)
591 if (p_matches != NULL)
595 start_attnpos != NULL_POSITION &&
597 (epos == NULL_POSITION || epos > start_attnpos))
599 * The attn line overlaps this range.
603 match = is_hilited_range(pos, epos);
607 if (p_matches != NULL)
609 * Report matches, even if we're hiding highlights.
613 if (hilite_search == 0)
615 * Not doing highlighting.
619 if (!nohide && hide_hilite)
621 * Highlighting is hidden.
629 * Tree node storage: get the current block of nodes if it has spare
630 * capacity, or create a new one if not.
632 static struct hilite_storage*
633 hlist_getstorage(struct hilite_tree *anchor)
636 struct hilite_storage *s;
640 if (anchor->current->used < anchor->current->capacity)
641 return anchor->current;
642 capacity = anchor->current->capacity * 2;
645 s = (struct hilite_storage *) ecalloc(1, sizeof(struct hilite_storage));
646 s->nodes = (struct hilite_node *) ecalloc(capacity, sizeof(struct hilite_node));
647 s->capacity = capacity;
651 anchor->current->next = s;
659 * Tree node storage: retrieve a new empty node to be inserted into the
662 static struct hilite_node*
663 hlist_getnode(struct hilite_tree *anchor)
665 struct hilite_storage *s = hlist_getstorage(anchor);
666 return &s->nodes[s->used++];
670 * Rotate the tree left around a pivot node.
673 hlist_rotate_left(struct hilite_tree *anchor, struct hilite_node *n)
675 struct hilite_node *np = n->parent;
676 struct hilite_node *nr = n->right;
677 struct hilite_node *nrl = n->right->left;
699 * Rotate the tree right around a pivot node.
702 hlist_rotate_right(struct hilite_tree *anchor, struct hilite_node *n)
704 struct hilite_node *np = n->parent;
705 struct hilite_node *nl = n->left;
706 struct hilite_node *nlr = n->left->right;
729 * Add a new hilite to a hilite list.
732 add_hilite(struct hilite_tree *anchor, struct hilite *hl)
734 struct hilite_node *p, *n, *u;
736 /* Ignore empty ranges. */
737 if (hl->hl_startpos >= hl->hl_endpos)
742 /* Inserting the very first node is trivial. */
745 n = hlist_getnode(anchor);
748 anchor->lookaside = n;
753 * Find our insertion point. If we come across any overlapping
754 * or adjoining existing ranges, shrink our range and discard
755 * if it become empty.
759 if (hl->hl_startpos < p->r.hl_startpos)
761 if (hl->hl_endpos > p->r.hl_startpos)
762 hl->hl_endpos = p->r.hl_startpos;
770 if (hl->hl_startpos < p->r.hl_endpos) {
771 hl->hl_startpos = p->r.hl_endpos;
772 if (hl->hl_startpos >= hl->hl_endpos)
775 if (p->right != NULL)
784 * Now we're at the right leaf, again check for contiguous ranges
785 * and extend the existing node if possible to avoid the
786 * insertion. Otherwise insert a new node at the leaf.
788 if (hl->hl_startpos < p->r.hl_startpos) {
789 if (hl->hl_endpos == p->r.hl_startpos)
791 p->r.hl_startpos = hl->hl_startpos;
794 if (p->prev != NULL && p->prev->r.hl_endpos == hl->hl_startpos)
796 p->prev->r.hl_endpos = hl->hl_endpos;
800 p->left = n = hlist_getnode(anchor);
809 if (p->r.hl_endpos == hl->hl_startpos)
811 p->r.hl_endpos = hl->hl_endpos;
814 if (p->next != NULL && hl->hl_endpos == p->next->r.hl_startpos) {
815 p->next->r.hl_startpos = hl->hl_startpos;
819 p->right = n = hlist_getnode(anchor);
833 * The tree is in the correct order and covers the right ranges
834 * now, but may have become unbalanced. Rebalance it using the
835 * standard red-black tree constraints and operations.
839 /* case 1 - current is root, root is always black */
840 if (n->parent == NULL)
846 /* case 2 - parent is black, we can always be red */
851 * constraint: because the root must be black, if our
852 * parent is red it cannot be the root therefore we must
857 * case 3 - parent and uncle are red, repaint them black,
858 * the grandparent red, and start again at the grandparent.
860 u = n->parent->parent->left;
862 u = n->parent->parent->right;
863 if (u != NULL && u->red)
867 n = n->parent->parent;
873 * case 4 - parent is red but uncle is black, parent and
874 * grandparent on opposite sides. We need to start
875 * changing the structure now. This and case 5 will shorten
876 * our branch and lengthen the sibling, between them
879 if (n == n->parent->right &&
880 n->parent == n->parent->parent->left)
882 hlist_rotate_left(anchor, n->parent);
884 } else if (n == n->parent->left &&
885 n->parent == n->parent->parent->right)
887 hlist_rotate_right(anchor, n->parent);
892 * case 5 - parent is red but uncle is black, parent and
893 * grandparent on same side
896 n->parent->parent->red = 1;
897 if (n == n->parent->left)
898 hlist_rotate_right(anchor, n->parent->parent);
900 hlist_rotate_left(anchor, n->parent->parent);
906 * Hilight every character in a range of displayed characters.
909 create_hilites(POSITION linepos, int start_index, int end_index, int *chpos)
914 /* Start the first hilite. */
915 hl.hl_startpos = linepos + chpos[start_index];
918 * Step through the displayed chars.
919 * If the source position (before cvt) of the char is one more
920 * than the source pos of the previous char (the usual case),
921 * just increase the size of the current hilite by one.
922 * Otherwise (there are backspaces or something involved),
923 * finish the current hilite and start a new one.
925 for (i = start_index+1; i <= end_index; i++)
927 if (chpos[i] != chpos[i-1] + 1 || i == end_index)
929 hl.hl_endpos = linepos + chpos[i-1] + 1;
930 add_hilite(&hilite_anchor, &hl);
931 /* Start new hilite unless this is the last char. */
934 hl.hl_startpos = linepos + chpos[i];
941 * Make a hilite for each string in a physical line which matches
942 * the current pattern.
943 * sp,ep delimit the first match already found.
946 hilite_line(POSITION linepos, char *line, int line_len, int *chpos, char *sp,
947 char *ep, int cvt_ops)
950 char *line_end = line + line_len;
953 * sp and ep delimit the first match in the line.
954 * Mark the corresponding file positions, then
955 * look for further matches and mark them.
956 * {{ This technique, of calling match_pattern on subsequent
957 * substrings of the line, may mark more than is correct
958 * if the pattern starts with "^". This bug is fixed
959 * for those regex functions that accept a notbol parameter
960 * (currently POSIX, PCRE and V8-with-regexec2). }}
964 if (sp == NULL || ep == NULL)
966 create_hilites(linepos, sp-line, ep-line, chpos);
968 * If we matched more than zero characters,
969 * move to the first char after the string we matched.
970 * If we matched zero, just move to the next char.
974 else if (searchp != line_end)
976 else /* end of line */
978 } while (match_pattern(info_compiled(&search_info), search_info.text,
979 searchp, line_end - searchp, &sp, &ep, 1, search_info.search_type));
984 * Change the caseless-ness of searches.
985 * Updates the internal search state to reflect a change in the -i flag.
990 if (!is_ucase_pattern)
992 * Pattern did not have uppercase.
993 * Just set the search caselessness to the global caselessness.
995 is_caseless = caseless;
998 * Pattern did have uppercase.
999 * Discard the pattern; we can't change search caselessness now.
1001 clear_pattern(&search_info);
1006 * Find matching text which is currently on screen and highlight it.
1011 struct scrpos scrpos;
1013 get_scrpos(&scrpos);
1014 if (scrpos.pos == NULL_POSITION)
1016 prep_hilite(scrpos.pos, position(BOTTOM_PLUS_ONE), -1);
1021 * Change highlighting parameters.
1027 * Erase any highlights currently on screen.
1032 if (hilite_search == OPT_ONPLUS)
1034 * Display highlights.
1041 * Figure out where to start a search.
1044 search_pos(int search_type)
1052 * Start at the beginning (or end) of the file.
1053 * The empty_screen() case is mainly for
1054 * command line initiated searches;
1055 * for example, "+/xyz" on the command line.
1056 * Also for multi-file (SRCH_PAST_EOF) searches.
1058 if (search_type & SRCH_FORW)
1064 if (pos == NULL_POSITION)
1066 (void) ch_end_seek();
1075 if (how_search == OPT_ON)
1078 * Search does not include current screen.
1080 if (search_type & SRCH_FORW)
1081 linenum = BOTTOM_PLUS_ONE;
1084 } else if (how_search == OPT_ONPLUS && !(search_type & SRCH_AFTER_TARGET))
1087 * Search includes all of displayed screen.
1089 if (search_type & SRCH_FORW)
1092 linenum = BOTTOM_PLUS_ONE;
1096 * Search includes the part of current screen beyond the jump target.
1097 * It starts at the jump target (if searching backwards),
1098 * or at the jump target plus one (if forwards).
1100 linenum = adjsline(jump_sline);
1101 if (search_type & SRCH_FORW)
1104 pos = position(linenum);
1106 pos = forw_raw_line(pos, (char **)NULL, (int *)NULL);
1110 * If the line is empty, look around for a plausible starting place.
1112 if (search_type & SRCH_FORW)
1114 while (pos == NULL_POSITION)
1116 if (++linenum >= sc_height)
1118 pos = position(linenum);
1122 while (pos == NULL_POSITION)
1126 pos = position(linenum);
1133 * Search a subset of the file, specified by start/end position.
1136 search_range(POSITION pos, POSITION endpos, int search_type, int matches,
1137 int maxlines, POSITION *plinepos, POSITION *pendpos)
1148 POSITION linepos, oldpos;
1150 linenum = find_linenum(pos);
1155 * Get lines until we find a matching one or until
1156 * we hit end-of-file (or beginning-of-file if we're
1157 * going backwards), or until we hit the end position.
1162 * A signal aborts the search.
1167 if ((endpos != NULL_POSITION && pos >= endpos) || maxlines == 0)
1170 * Reached end position without a match.
1172 if (pendpos != NULL)
1179 if (search_type & SRCH_FORW)
1182 * Read the next line, and save the
1183 * starting position of that line in linepos.
1186 pos = forw_raw_line(pos, &line, &line_len);
1192 * Read the previous line and save the
1193 * starting position of that line in linepos.
1195 pos = back_raw_line(pos, &line, &line_len);
1201 if (pos == NULL_POSITION)
1204 * Reached EOF/BOF without a match.
1206 if (pendpos != NULL)
1212 * If we're using line numbers, we might as well
1213 * remember the information we have now (the position
1214 * and line number of the current line).
1215 * Don't do it for every line because it slows down
1216 * the search. Remember the line number only if
1217 * we're "far" from the last place we remembered it.
1219 if (linenums && abs((int)(pos - oldpos)) > 2048)
1220 add_lnum(linenum, pos);
1223 if (is_filtered(linepos))
1227 * If it's a caseless search, convert the line to lowercase.
1228 * If we're doing backspace processing, delete backspaces.
1230 cvt_ops = get_cvt_ops();
1231 cvt_len = cvt_length(line_len, cvt_ops);
1232 cline = (char *) ecalloc(1, cvt_len);
1233 chpos = cvt_alloc_chpos(cvt_len);
1234 cvt_text(cline, line, chpos, &line_len, cvt_ops);
1238 * Check to see if the line matches the filter pattern.
1239 * If so, add an entry to the filter list.
1241 if (((search_type & SRCH_FIND_ALL) ||
1242 prep_startpos == NULL_POSITION ||
1243 linepos < prep_startpos || linepos >= prep_endpos) &&
1244 prev_pattern(&filter_info)) {
1245 int line_filter = match_pattern(info_compiled(&filter_info), filter_info.text,
1246 cline, line_len, &sp, &ep, 0, filter_info.search_type);
1250 hl.hl_startpos = linepos;
1252 add_hilite(&filter_anchor, &hl);
1259 * Test the next line to see if we have a match.
1260 * We are successful if we either want a match and got one,
1261 * or if we want a non-match and got one.
1263 if (prev_pattern(&search_info))
1265 line_match = match_pattern(info_compiled(&search_info), search_info.text,
1266 cline, line_len, &sp, &ep, 0, search_type);
1272 if (search_type & SRCH_FIND_ALL)
1276 * We are supposed to find all matches in the range.
1277 * Just add the matches in this line to the
1278 * hilite list and keep searching.
1280 hilite_line(linepos, cline, line_len, chpos, sp, ep, cvt_ops);
1282 } else if (--matches <= 0)
1285 * Found the one match we're looking for.
1289 if (hilite_search == OPT_ON)
1292 * Clear the hilite list and add only
1293 * the matches in this one line.
1296 hilite_line(linepos, cline, line_len, chpos, sp, ep, cvt_ops);
1301 if (plinepos != NULL)
1302 *plinepos = linepos;
1313 * search for a pattern in history. If found, compile that pattern.
1316 hist_pattern(int search_type)
1321 set_mlist(ml_search, 0);
1322 pattern = cmd_lastpattern();
1323 if (pattern == NULL)
1326 if (set_pattern(&search_info, pattern, search_type) < 0)
1330 if (hilite_search == OPT_ONPLUS && !hide_hilite)
1335 #else /* CMD_HISTORY */
1337 #endif /* CMD_HISTORY */
1341 * Search for the n-th occurrence of a specified pattern,
1342 * either forward or backward.
1343 * Return the number of matches not yet found in this file
1344 * (that is, n minus the number of matches found).
1345 * Return -1 if the search should be aborted.
1346 * Caller may continue the search in another file
1347 * if less than n matches are found in this file.
1350 search(int search_type, char *pattern, int n)
1354 if (pattern == NULL || *pattern == '\0')
1357 * A null pattern means use the previously compiled pattern.
1359 search_type |= SRCH_AFTER_TARGET;
1360 if (!prev_pattern(&search_info) && !hist_pattern(search_type))
1362 error("No previous regular expression", NULL_PARG);
1365 if ((search_type & SRCH_NO_REGEX) !=
1366 (search_info.search_type & SRCH_NO_REGEX))
1368 error("Please re-enter search pattern", NULL_PARG);
1372 if (hilite_search == OPT_ON)
1375 * Erase the highlights currently on screen.
1376 * If the search fails, we'll redisplay them later.
1380 if (hilite_search == OPT_ONPLUS && hide_hilite)
1383 * Highlight any matches currently on screen,
1384 * before we actually start the search.
1394 * Compile the pattern.
1396 if (set_pattern(&search_info, pattern, search_type) < 0)
1402 * Erase the highlights currently on screen.
1403 * Also permanently delete them from the hilite list.
1409 if (hilite_search == OPT_ONPLUS)
1412 * Highlight any matches currently on screen,
1413 * before we actually start the search.
1421 * Figure out where to start the search.
1423 pos = search_pos(search_type);
1424 if (pos == NULL_POSITION)
1427 * Can't find anyplace to start searching from.
1429 if (search_type & SRCH_PAST_EOF)
1431 /* repaint(); -- why was this here? */
1432 error("Nothing to search", NULL_PARG);
1436 n = search_range(pos, NULL_POSITION, search_type, n, -1,
1437 &pos, (POSITION*)NULL);
1441 * Search was unsuccessful.
1444 if (hilite_search == OPT_ON && n > 0)
1446 * Redisplay old hilites.
1453 if (!(search_type & SRCH_NO_MOVE))
1456 * Go to the matching line.
1458 jump_loc(pos, jump_sline);
1462 if (hilite_search == OPT_ON)
1464 * Display new hilites in the matching line.
1474 * Prepare hilites in a given range of the file.
1476 * The pair (prep_startpos,prep_endpos) delimits a contiguous region
1477 * of the file that has been "prepared"; that is, scanned for matches for
1478 * the current search pattern, and hilites have been created for such matches.
1479 * If prep_startpos == NULL_POSITION, the prep region is empty.
1480 * If prep_endpos == NULL_POSITION, the prep region extends to EOF.
1481 * prep_hilite asks that the range (spos,epos) be covered by the prep region.
1484 prep_hilite(POSITION spos, POSITION epos, int maxlines)
1486 POSITION nprep_startpos = prep_startpos;
1487 POSITION nprep_endpos = prep_endpos;
1494 * Search beyond where we're asked to search, so the prep region covers
1495 * more than we need. Do one big search instead of a bunch of small ones.
1497 #define SEARCH_MORE (3*size_linebuf)
1499 if (!prev_pattern(&search_info) && !is_filtering())
1503 * Make sure our prep region always starts at the beginning of
1504 * a line. (search_range takes care of the end boundary below.)
1506 spos = back_raw_line(spos+1, (char **)NULL, (int *)NULL);
1509 * If we're limited to a max number of lines, figure out the
1510 * file position we should stop at.
1513 max_epos = NULL_POSITION;
1517 for (i = 0; i < maxlines; i++)
1518 max_epos = forw_raw_line(max_epos, (char **)NULL, (int *)NULL);
1523 * The range that we need to search (spos,epos); and the range that
1524 * the "prep" region will then cover (nprep_startpos,nprep_endpos).
1527 if (prep_startpos == NULL_POSITION ||
1528 (epos != NULL_POSITION && epos < prep_startpos) ||
1532 * New range is not contiguous with old prep region.
1533 * Discard the old prep region and start a new one.
1537 if (epos != NULL_POSITION)
1538 epos += SEARCH_MORE;
1539 nprep_startpos = spos;
1543 * New range partially or completely overlaps old prep region.
1545 if (epos == NULL_POSITION)
1548 * New range goes to end of file.
1551 } else if (epos > prep_endpos)
1554 * New range ends after old prep region.
1555 * Extend prep region to end at end of new range.
1557 epos += SEARCH_MORE;
1558 } else /* (epos <= prep_endpos) */
1561 * New range ends within old prep region.
1562 * Truncate search to end at start of old prep region.
1564 epos = prep_startpos;
1567 if (spos < prep_startpos)
1570 * New range starts before old prep region.
1571 * Extend old prep region backwards to start at
1572 * start of new range.
1574 if (spos < SEARCH_MORE)
1577 spos -= SEARCH_MORE;
1578 nprep_startpos = spos;
1579 } else /* (spos >= prep_startpos) */
1582 * New range starts within or after old prep region.
1583 * Trim search to start at end of old prep region.
1589 if (epos != NULL_POSITION && max_epos != NULL_POSITION &&
1592 * Don't go past the max position we're allowed.
1596 if (epos == NULL_POSITION || epos > spos)
1598 int search_type = SRCH_FORW | SRCH_FIND_ALL;
1599 search_type |= (search_info.search_type & SRCH_NO_REGEX);
1602 result = search_range(spos, epos, search_type, 0, maxlines, (POSITION*)NULL, &new_epos);
1605 if (prep_endpos == NULL_POSITION || new_epos > prep_endpos)
1606 nprep_endpos = new_epos;
1609 * Check both ends of the resulting prep region to
1610 * make sure they're not filtered. If they are,
1611 * keep going at least one more line until we find
1612 * something that isn't filtered, or hit the end.
1614 if (prep_endpos == NULL_POSITION || nprep_endpos > prep_endpos)
1616 if (new_epos >= nprep_endpos && is_filtered(new_epos-1))
1618 spos = nprep_endpos;
1619 epos = forw_raw_line(nprep_endpos, (char **)NULL, (int *)NULL);
1620 if (epos == NULL_POSITION)
1627 if (prep_startpos == NULL_POSITION || nprep_startpos < prep_startpos)
1629 if (nprep_startpos > 0 && is_filtered(nprep_startpos))
1631 epos = nprep_startpos;
1632 spos = back_raw_line(nprep_startpos, (char **)NULL, (int *)NULL);
1633 if (spos == NULL_POSITION)
1635 nprep_startpos = spos;
1643 prep_startpos = nprep_startpos;
1644 prep_endpos = nprep_endpos;
1648 * Set the pattern to be used for line filtering.
1651 set_filter_pattern(char *pattern, int search_type)
1654 if (pattern == NULL || *pattern == '\0')
1655 clear_pattern(&filter_info);
1657 set_pattern(&filter_info, pattern, search_type);
1662 * Is there a line filter in effect?
1667 if (ch_getflags() & CH_HELPFILE)
1669 return prev_pattern(&filter_info);
1675 * This function is called by the V8 regcomp to report
1676 * errors in regular expressions.
1678 public int reg_show_error = 1;
1686 if (!reg_show_error)