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?
127 char *str_end
= str
+ strlen(str
);
130 while (str
< str_end
)
132 ch
= step_char(&str
, +1, str_end
);
140 * Compile and save a search pattern.
143 set_pattern(info
, pattern
, search_type
)
144 struct pattern_info
*info
;
150 CLEAR_PATTERN(info
->compiled
);
151 else if (compile_pattern(pattern
, search_type
, &info
->compiled
) < 0)
154 /* Pattern compiled successfully; save the text too. */
155 if (info
->text
!= NULL
)
160 info
->text
= (char *) ecalloc(1, strlen(pattern
)+1);
161 strcpy(info
->text
, pattern
);
163 info
->search_type
= search_type
;
166 * Ignore case if -I is set OR
167 * -i is set AND the pattern is all lowercase.
169 is_ucase_pattern
= is_ucase(pattern
);
170 if (is_ucase_pattern
&& caseless
!= OPT_ONPLUS
)
173 is_caseless
= caseless
;
178 * Discard a saved pattern.
182 struct pattern_info
*info
;
184 if (info
->text
!= NULL
)
188 uncompile_pattern(&info
->compiled
);
193 * Initialize saved pattern to nothing.
197 struct pattern_info
*info
;
199 CLEAR_PATTERN(info
->compiled
);
201 info
->search_type
= 0;
205 * Initialize search variables.
210 init_pattern(&search_info
);
211 init_pattern(&filter_info
);
215 * Determine which text conversions to perform before pattern matching.
221 if (is_caseless
|| bs_mode
== BS_SPECIAL
)
225 if (bs_mode
== BS_SPECIAL
)
227 if (bs_mode
!= BS_CONTROL
)
229 } else if (bs_mode
!= BS_CONTROL
)
233 if (ctldisp
== OPT_ONPLUS
)
239 * Is there a previous (remembered) search pattern?
243 struct pattern_info
*info
;
246 if ((info
->search_type
& SRCH_NO_REGEX
) == 0)
247 return (!is_null_pattern(info
->compiled
));
249 return (info
->text
!= NULL
);
254 * Repaint the hilites currently displayed on the screen.
255 * Repaint each line which contains highlighted text.
256 * If on==0, force all hilites off.
264 int save_hide_hilite
;
269 save_hide_hilite
= hide_hilite
;
280 hide_hilite
= save_hide_hilite
;
284 for (slinenum
= TOP
; slinenum
< TOP
+ sc_height
-1; slinenum
++)
286 pos
= position(slinenum
);
287 if (pos
== NULL_POSITION
)
289 (void) forw_line(pos
);
294 hide_hilite
= save_hide_hilite
;
298 * Clear the attn hilite.
304 POSITION old_start_attnpos
;
305 POSITION old_end_attnpos
;
310 if (start_attnpos
== NULL_POSITION
)
312 old_start_attnpos
= start_attnpos
;
313 old_end_attnpos
= end_attnpos
;
314 start_attnpos
= end_attnpos
= NULL_POSITION
;
324 for (slinenum
= TOP
; slinenum
< TOP
+ sc_height
-1; slinenum
++)
326 pos
= position(slinenum
);
327 if (pos
== NULL_POSITION
)
329 epos
= position(slinenum
+1);
330 if (pos
< old_end_attnpos
&&
331 (epos
== NULL_POSITION
|| epos
> old_start_attnpos
))
333 (void) forw_line(pos
);
345 * Hide search string highlighting.
350 if (!prev_pattern(&search_info
))
352 error("No previous regular expression", NULL_PARG
);
356 hide_hilite
= !hide_hilite
;
363 * Clear the hilite list.
367 struct hilite_tree
*anchor
;
369 struct hilite_storage
*hls
;
370 struct hilite_storage
*nexthls
;
372 for (hls
= anchor
->first
; hls
!= NULL
; hls
= nexthls
)
375 free((void*)hls
->nodes
);
378 anchor
->first
= NULL
;
379 anchor
->current
= NULL
;
382 anchor
->lookaside
= NULL
;
384 prep_startpos
= prep_endpos
= NULL_POSITION
;
390 clr_hlist(&hilite_anchor
);
396 clr_hlist(&filter_anchor
);
401 struct hilite_tree
*anchor
;
403 struct hilite_node
*n
= anchor
->root
;
404 while (n
!= NULL
&& n
->right
!= NULL
)
411 struct hilite_node
*n
;
418 struct hilite_node
*n
;
424 * Find the node covering pos, or the node after it if no node covers it,
425 * or return NULL if pos is after the last range. Remember the found node,
426 * to speed up subsequent searches for the same or similar positions (if
427 * we return NULL, remember the last node.)
430 hlist_find(anchor
, pos
)
431 struct hilite_tree
*anchor
;
434 struct hilite_node
*n
, *m
;
436 if (anchor
->lookaside
)
441 n
= anchor
->lookaside
;
445 if (pos
< n
->r
.hl_endpos
)
447 if (n
->prev
== NULL
|| pos
>= n
->prev
->r
.hl_endpos
)
452 } else if (n
->next
== NULL
)
460 * If we don't find the right node within a small
461 * distance, don't keep doing a linear search!
463 if (steps
>= HILITE_LOOKASIDE_STEPS
)
467 if (pos
< n
->r
.hl_endpos
)
468 anchor
->lookaside
= n
= n
->prev
;
470 anchor
->lookaside
= n
= n
->next
;
482 if (pos
< n
->r
.hl_startpos
)
492 if (pos
>= n
->r
.hl_endpos
)
494 if (n
->right
!= NULL
)
512 anchor
->lookaside
= n
;
514 anchor
->lookaside
= m
;
520 * Should any characters in a specified range be highlighted?
523 is_hilited_range(pos
, epos
)
527 struct hilite_node
*n
= hlist_find(&hilite_anchor
, pos
);
528 return (n
!= NULL
&& (epos
== NULL_POSITION
|| epos
> n
->r
.hl_startpos
));
532 * Is a line "filtered" -- that is, should it be hidden?
538 struct hilite_node
*n
;
540 if (ch_getflags() & CH_HELPFILE
)
543 n
= hlist_find(&filter_anchor
, pos
);
544 return (n
!= NULL
&& pos
>= n
->r
.hl_startpos
);
548 * If pos is hidden, return the next position which isn't, otherwise
555 struct hilite_node
*n
;
557 if (ch_getflags() & CH_HELPFILE
)
560 n
= hlist_find(&filter_anchor
, pos
);
561 while (n
!= NULL
&& pos
>= n
->r
.hl_startpos
)
563 pos
= n
->r
.hl_endpos
;
570 * If pos is hidden, return the previous position which isn't or 0 if
571 * we're filtered right to the beginning, otherwise just return pos.
577 struct hilite_node
*n
;
579 if (ch_getflags() & CH_HELPFILE
)
582 n
= hlist_find(&filter_anchor
, pos
);
583 while (n
!= NULL
&& pos
>= n
->r
.hl_startpos
)
585 pos
= n
->r
.hl_startpos
;
596 * Should any characters in a specified range be highlighted?
597 * If nohide is nonzero, don't consider hide_hilite.
600 is_hilited(pos
, epos
, nohide
, p_matches
)
608 if (p_matches
!= NULL
)
612 start_attnpos
!= NULL_POSITION
&&
614 (epos
== NULL_POSITION
|| epos
> start_attnpos
))
616 * The attn line overlaps this range.
620 match
= is_hilited_range(pos
, epos
);
624 if (p_matches
!= NULL
)
626 * Report matches, even if we're hiding highlights.
630 if (hilite_search
== 0)
632 * Not doing highlighting.
636 if (!nohide
&& hide_hilite
)
638 * Highlighting is hidden.
646 * Tree node storage: get the current block of nodes if it has spare
647 * capacity, or create a new one if not.
649 static struct hilite_storage
*
650 hlist_getstorage(anchor
)
651 struct hilite_tree
*anchor
;
654 struct hilite_storage
*s
;
658 if (anchor
->current
->used
< anchor
->current
->capacity
)
659 return anchor
->current
;
660 capacity
= anchor
->current
->capacity
* 2;
663 s
= (struct hilite_storage
*) ecalloc(1, sizeof(struct hilite_storage
));
664 s
->nodes
= (struct hilite_node
*) ecalloc(capacity
, sizeof(struct hilite_node
));
665 s
->capacity
= capacity
;
669 anchor
->current
->next
= s
;
677 * Tree node storage: retrieve a new empty node to be inserted into the
680 static struct hilite_node
*
681 hlist_getnode(anchor
)
682 struct hilite_tree
*anchor
;
684 struct hilite_storage
*s
= hlist_getstorage(anchor
);
685 return &s
->nodes
[s
->used
++];
689 * Rotate the tree left around a pivot node.
692 hlist_rotate_left(anchor
, n
)
693 struct hilite_tree
*anchor
;
694 struct hilite_node
*n
;
696 struct hilite_node
*np
= n
->parent
;
697 struct hilite_node
*nr
= n
->right
;
698 struct hilite_node
*nrl
= n
->right
->left
;
720 * Rotate the tree right around a pivot node.
723 hlist_rotate_right(anchor
, n
)
724 struct hilite_tree
*anchor
;
725 struct hilite_node
*n
;
727 struct hilite_node
*np
= n
->parent
;
728 struct hilite_node
*nl
= n
->left
;
729 struct hilite_node
*nlr
= n
->left
->right
;
752 * Add a new hilite to a hilite list.
755 add_hilite(anchor
, hl
)
756 struct hilite_tree
*anchor
;
759 struct hilite_node
*p
, *n
, *u
;
761 /* Ignore empty ranges. */
762 if (hl
->hl_startpos
>= hl
->hl_endpos
)
767 /* Inserting the very first node is trivial. */
770 n
= hlist_getnode(anchor
);
773 anchor
->lookaside
= n
;
778 * Find our insertion point. If we come across any overlapping
779 * or adjoining existing ranges, shrink our range and discard
780 * if it become empty.
784 if (hl
->hl_startpos
< p
->r
.hl_startpos
)
786 if (hl
->hl_endpos
> p
->r
.hl_startpos
)
787 hl
->hl_endpos
= p
->r
.hl_startpos
;
795 if (hl
->hl_startpos
< p
->r
.hl_endpos
) {
796 hl
->hl_startpos
= p
->r
.hl_endpos
;
797 if (hl
->hl_startpos
>= hl
->hl_endpos
)
800 if (p
->right
!= NULL
)
809 * Now we're at the right leaf, again check for contiguous ranges
810 * and extend the existing node if possible to avoid the
811 * insertion. Otherwise insert a new node at the leaf.
813 if (hl
->hl_startpos
< p
->r
.hl_startpos
) {
814 if (hl
->hl_endpos
== p
->r
.hl_startpos
)
816 p
->r
.hl_startpos
= hl
->hl_startpos
;
819 if (p
->prev
!= NULL
&& p
->prev
->r
.hl_endpos
== hl
->hl_startpos
)
821 p
->prev
->r
.hl_endpos
= hl
->hl_endpos
;
825 p
->left
= n
= hlist_getnode(anchor
);
834 if (p
->r
.hl_endpos
== hl
->hl_startpos
)
836 p
->r
.hl_endpos
= hl
->hl_endpos
;
839 if (p
->next
!= NULL
&& hl
->hl_endpos
== p
->next
->r
.hl_startpos
) {
840 p
->next
->r
.hl_startpos
= hl
->hl_startpos
;
844 p
->right
= n
= hlist_getnode(anchor
);
858 * The tree is in the correct order and covers the right ranges
859 * now, but may have become unbalanced. Rebalance it using the
860 * standard red-black tree constraints and operations.
864 /* case 1 - current is root, root is always black */
865 if (n
->parent
== NULL
)
871 /* case 2 - parent is black, we can always be red */
876 * constraint: because the root must be black, if our
877 * parent is red it cannot be the root therefore we must
882 * case 3 - parent and uncle are red, repaint them black,
883 * the grandparent red, and start again at the grandparent.
885 u
= n
->parent
->parent
->left
;
887 u
= n
->parent
->parent
->right
;
888 if (u
!= NULL
&& u
->red
)
892 n
= n
->parent
->parent
;
898 * case 4 - parent is red but uncle is black, parent and
899 * grandparent on opposite sides. We need to start
900 * changing the structure now. This and case 5 will shorten
901 * our branch and lengthen the sibling, between them
904 if (n
== n
->parent
->right
&&
905 n
->parent
== n
->parent
->parent
->left
)
907 hlist_rotate_left(anchor
, n
->parent
);
909 } else if (n
== n
->parent
->left
&&
910 n
->parent
== n
->parent
->parent
->right
)
912 hlist_rotate_right(anchor
, n
->parent
);
917 * case 5 - parent is red but uncle is black, parent and
918 * grandparent on same side
921 n
->parent
->parent
->red
= 1;
922 if (n
== n
->parent
->left
)
923 hlist_rotate_right(anchor
, n
->parent
->parent
);
925 hlist_rotate_left(anchor
, n
->parent
->parent
);
931 * Hilight every character in a range of displayed characters.
934 create_hilites(linepos
, start_index
, end_index
, chpos
)
943 /* Start the first hilite. */
944 hl
.hl_startpos
= linepos
+ chpos
[start_index
];
947 * Step through the displayed chars.
948 * If the source position (before cvt) of the char is one more
949 * than the source pos of the previous char (the usual case),
950 * just increase the size of the current hilite by one.
951 * Otherwise (there are backspaces or something involved),
952 * finish the current hilite and start a new one.
954 for (i
= start_index
+1; i
<= end_index
; i
++)
956 if (chpos
[i
] != chpos
[i
-1] + 1 || i
== end_index
)
958 hl
.hl_endpos
= linepos
+ chpos
[i
-1] + 1;
959 add_hilite(&hilite_anchor
, &hl
);
960 /* Start new hilite unless this is the last char. */
963 hl
.hl_startpos
= linepos
+ chpos
[i
];
970 * Make a hilite for each string in a physical line which matches
971 * the current pattern.
972 * sp,ep delimit the first match already found.
975 hilite_line(linepos
, line
, line_len
, chpos
, sp
, ep
, cvt_ops
)
985 char *line_end
= line
+ line_len
;
988 * sp and ep delimit the first match in the line.
989 * Mark the corresponding file positions, then
990 * look for further matches and mark them.
991 * {{ This technique, of calling match_pattern on subsequent
992 * substrings of the line, may mark more than is correct
993 * if the pattern starts with "^". This bug is fixed
994 * for those regex functions that accept a notbol parameter
995 * (currently POSIX, PCRE and V8-with-regexec2). }}
999 if (sp
== NULL
|| ep
== NULL
)
1001 create_hilites(linepos
, sp
-line
, ep
-line
, chpos
);
1003 * If we matched more than zero characters,
1004 * move to the first char after the string we matched.
1005 * If we matched zero, just move to the next char.
1009 else if (searchp
!= line_end
)
1011 else /* end of line */
1013 } while (match_pattern(info_compiled(&search_info
), search_info
.text
,
1014 searchp
, line_end
- searchp
, &sp
, &ep
, 1, search_info
.search_type
));
1019 * Change the caseless-ness of searches.
1020 * Updates the internal search state to reflect a change in the -i flag.
1025 if (!is_ucase_pattern
)
1027 * Pattern did not have uppercase.
1028 * Just set the search caselessness to the global caselessness.
1030 is_caseless
= caseless
;
1033 * Pattern did have uppercase.
1034 * Discard the pattern; we can't change search caselessness now.
1036 clear_pattern(&search_info
);
1041 * Find matching text which is currently on screen and highlight it.
1046 struct scrpos scrpos
;
1048 get_scrpos(&scrpos
);
1049 if (scrpos
.pos
== NULL_POSITION
)
1051 prep_hilite(scrpos
.pos
, position(BOTTOM_PLUS_ONE
), -1);
1056 * Change highlighting parameters.
1062 * Erase any highlights currently on screen.
1067 if (hilite_search
== OPT_ONPLUS
)
1069 * Display highlights.
1076 * Figure out where to start a search.
1079 search_pos(search_type
)
1088 * Start at the beginning (or end) of the file.
1089 * The empty_screen() case is mainly for
1090 * command line initiated searches;
1091 * for example, "+/xyz" on the command line.
1092 * Also for multi-file (SRCH_PAST_EOF) searches.
1094 if (search_type
& SRCH_FORW
)
1100 if (pos
== NULL_POSITION
)
1102 (void) ch_end_seek();
1111 if (how_search
== OPT_ON
)
1114 * Search does not include current screen.
1116 if (search_type
& SRCH_FORW
)
1117 linenum
= BOTTOM_PLUS_ONE
;
1120 } else if (how_search
== OPT_ONPLUS
&& !(search_type
& SRCH_AFTER_TARGET
))
1123 * Search includes all of displayed screen.
1125 if (search_type
& SRCH_FORW
)
1128 linenum
= BOTTOM_PLUS_ONE
;
1132 * Search includes the part of current screen beyond the jump target.
1133 * It starts at the jump target (if searching backwards),
1134 * or at the jump target plus one (if forwards).
1136 linenum
= adjsline(jump_sline
);
1137 if (search_type
& SRCH_FORW
)
1140 pos
= position(linenum
);
1142 pos
= forw_raw_line(pos
, (char **)NULL
, (int *)NULL
);
1146 * If the line is empty, look around for a plausible starting place.
1148 if (search_type
& SRCH_FORW
)
1150 while (pos
== NULL_POSITION
)
1152 if (++linenum
>= sc_height
)
1154 pos
= position(linenum
);
1158 while (pos
== NULL_POSITION
)
1162 pos
= position(linenum
);
1169 * Search a subset of the file, specified by start/end position.
1172 search_range(pos
, endpos
, search_type
, matches
, maxlines
, plinepos
, pendpos
)
1190 POSITION linepos
, oldpos
;
1192 linenum
= find_linenum(pos
);
1197 * Get lines until we find a matching one or until
1198 * we hit end-of-file (or beginning-of-file if we're
1199 * going backwards), or until we hit the end position.
1204 * A signal aborts the search.
1209 if ((endpos
!= NULL_POSITION
&& pos
>= endpos
) || maxlines
== 0)
1212 * Reached end position without a match.
1214 if (pendpos
!= NULL
)
1221 if (search_type
& SRCH_FORW
)
1224 * Read the next line, and save the
1225 * starting position of that line in linepos.
1228 pos
= forw_raw_line(pos
, &line
, &line_len
);
1234 * Read the previous line and save the
1235 * starting position of that line in linepos.
1237 pos
= back_raw_line(pos
, &line
, &line_len
);
1243 if (pos
== NULL_POSITION
)
1246 * Reached EOF/BOF without a match.
1248 if (pendpos
!= NULL
)
1254 * If we're using line numbers, we might as well
1255 * remember the information we have now (the position
1256 * and line number of the current line).
1257 * Don't do it for every line because it slows down
1258 * the search. Remember the line number only if
1259 * we're "far" from the last place we remembered it.
1261 if (linenums
&& abs((int)(pos
- oldpos
)) > 2048)
1262 add_lnum(linenum
, pos
);
1265 if (is_filtered(linepos
))
1269 * If it's a caseless search, convert the line to lowercase.
1270 * If we're doing backspace processing, delete backspaces.
1272 cvt_ops
= get_cvt_ops();
1273 cvt_len
= cvt_length(line_len
, cvt_ops
);
1274 cline
= (char *) ecalloc(1, cvt_len
);
1275 chpos
= cvt_alloc_chpos(cvt_len
);
1276 cvt_text(cline
, line
, chpos
, &line_len
, cvt_ops
);
1280 * Check to see if the line matches the filter pattern.
1281 * If so, add an entry to the filter list.
1283 if (((search_type
& SRCH_FIND_ALL
) ||
1284 prep_startpos
== NULL_POSITION
||
1285 linepos
< prep_startpos
|| linepos
>= prep_endpos
) &&
1286 prev_pattern(&filter_info
)) {
1287 int line_filter
= match_pattern(info_compiled(&filter_info
), filter_info
.text
,
1288 cline
, line_len
, &sp
, &ep
, 0, filter_info
.search_type
);
1292 hl
.hl_startpos
= linepos
;
1294 add_hilite(&filter_anchor
, &hl
);
1301 * Test the next line to see if we have a match.
1302 * We are successful if we either want a match and got one,
1303 * or if we want a non-match and got one.
1305 if (prev_pattern(&search_info
))
1307 line_match
= match_pattern(info_compiled(&search_info
), search_info
.text
,
1308 cline
, line_len
, &sp
, &ep
, 0, search_type
);
1314 if (search_type
& SRCH_FIND_ALL
)
1318 * We are supposed to find all matches in the range.
1319 * Just add the matches in this line to the
1320 * hilite list and keep searching.
1322 hilite_line(linepos
, cline
, line_len
, chpos
, sp
, ep
, cvt_ops
);
1324 } else if (--matches
<= 0)
1327 * Found the one match we're looking for.
1331 if (hilite_search
== OPT_ON
)
1334 * Clear the hilite list and add only
1335 * the matches in this one line.
1338 hilite_line(linepos
, cline
, line_len
, chpos
, sp
, ep
, cvt_ops
);
1343 if (plinepos
!= NULL
)
1344 *plinepos
= linepos
;
1355 * search for a pattern in history. If found, compile that pattern.
1358 hist_pattern(search_type
)
1364 set_mlist(ml_search
, 0);
1365 pattern
= cmd_lastpattern();
1366 if (pattern
== NULL
)
1369 if (set_pattern(&search_info
, pattern
, search_type
) < 0)
1373 if (hilite_search
== OPT_ONPLUS
&& !hide_hilite
)
1378 #else /* CMD_HISTORY */
1380 #endif /* CMD_HISTORY */
1384 * Search for the n-th occurrence of a specified pattern,
1385 * either forward or backward.
1386 * Return the number of matches not yet found in this file
1387 * (that is, n minus the number of matches found).
1388 * Return -1 if the search should be aborted.
1389 * Caller may continue the search in another file
1390 * if less than n matches are found in this file.
1393 search(search_type
, pattern
, n
)
1400 if (pattern
== NULL
|| *pattern
== '\0')
1403 * A null pattern means use the previously compiled pattern.
1405 search_type
|= SRCH_AFTER_TARGET
;
1406 if (!prev_pattern(&search_info
) && !hist_pattern(search_type
))
1408 error("No previous regular expression", NULL_PARG
);
1411 if ((search_type
& SRCH_NO_REGEX
) !=
1412 (search_info
.search_type
& SRCH_NO_REGEX
))
1414 error("Please re-enter search pattern", NULL_PARG
);
1418 if (hilite_search
== OPT_ON
)
1421 * Erase the highlights currently on screen.
1422 * If the search fails, we'll redisplay them later.
1426 if (hilite_search
== OPT_ONPLUS
&& hide_hilite
)
1429 * Highlight any matches currently on screen,
1430 * before we actually start the search.
1440 * Compile the pattern.
1442 if (set_pattern(&search_info
, pattern
, search_type
) < 0)
1448 * Erase the highlights currently on screen.
1449 * Also permanently delete them from the hilite list.
1455 if (hilite_search
== OPT_ONPLUS
)
1458 * Highlight any matches currently on screen,
1459 * before we actually start the search.
1467 * Figure out where to start the search.
1469 pos
= search_pos(search_type
);
1470 if (pos
== NULL_POSITION
)
1473 * Can't find anyplace to start searching from.
1475 if (search_type
& SRCH_PAST_EOF
)
1477 /* repaint(); -- why was this here? */
1478 error("Nothing to search", NULL_PARG
);
1482 n
= search_range(pos
, NULL_POSITION
, search_type
, n
, -1,
1483 &pos
, (POSITION
*)NULL
);
1487 * Search was unsuccessful.
1490 if (hilite_search
== OPT_ON
&& n
> 0)
1492 * Redisplay old hilites.
1499 if (!(search_type
& SRCH_NO_MOVE
))
1502 * Go to the matching line.
1504 jump_loc(pos
, jump_sline
);
1508 if (hilite_search
== OPT_ON
)
1510 * Display new hilites in the matching line.
1520 * Prepare hilites in a given range of the file.
1522 * The pair (prep_startpos,prep_endpos) delimits a contiguous region
1523 * of the file that has been "prepared"; that is, scanned for matches for
1524 * the current search pattern, and hilites have been created for such matches.
1525 * If prep_startpos == NULL_POSITION, the prep region is empty.
1526 * If prep_endpos == NULL_POSITION, the prep region extends to EOF.
1527 * prep_hilite asks that the range (spos,epos) be covered by the prep region.
1530 prep_hilite(spos
, epos
, maxlines
)
1535 POSITION nprep_startpos
= prep_startpos
;
1536 POSITION nprep_endpos
= prep_endpos
;
1543 * Search beyond where we're asked to search, so the prep region covers
1544 * more than we need. Do one big search instead of a bunch of small ones.
1546 #define SEARCH_MORE (3*size_linebuf)
1548 if (!prev_pattern(&search_info
) && !is_filtering())
1552 * Make sure our prep region always starts at the beginning of
1553 * a line. (search_range takes care of the end boundary below.)
1555 spos
= back_raw_line(spos
+1, (char **)NULL
, (int *)NULL
);
1558 * If we're limited to a max number of lines, figure out the
1559 * file position we should stop at.
1562 max_epos
= NULL_POSITION
;
1566 for (i
= 0; i
< maxlines
; i
++)
1567 max_epos
= forw_raw_line(max_epos
, (char **)NULL
, (int *)NULL
);
1572 * The range that we need to search (spos,epos); and the range that
1573 * the "prep" region will then cover (nprep_startpos,nprep_endpos).
1576 if (prep_startpos
== NULL_POSITION
||
1577 (epos
!= NULL_POSITION
&& epos
< prep_startpos
) ||
1581 * New range is not contiguous with old prep region.
1582 * Discard the old prep region and start a new one.
1586 if (epos
!= NULL_POSITION
)
1587 epos
+= SEARCH_MORE
;
1588 nprep_startpos
= spos
;
1592 * New range partially or completely overlaps old prep region.
1594 if (epos
== NULL_POSITION
)
1597 * New range goes to end of file.
1600 } else if (epos
> prep_endpos
)
1603 * New range ends after old prep region.
1604 * Extend prep region to end at end of new range.
1606 epos
+= SEARCH_MORE
;
1607 } else /* (epos <= prep_endpos) */
1610 * New range ends within old prep region.
1611 * Truncate search to end at start of old prep region.
1613 epos
= prep_startpos
;
1616 if (spos
< prep_startpos
)
1619 * New range starts before old prep region.
1620 * Extend old prep region backwards to start at
1621 * start of new range.
1623 if (spos
< SEARCH_MORE
)
1626 spos
-= SEARCH_MORE
;
1627 nprep_startpos
= spos
;
1628 } else /* (spos >= prep_startpos) */
1631 * New range starts within or after old prep region.
1632 * Trim search to start at end of old prep region.
1638 if (epos
!= NULL_POSITION
&& max_epos
!= NULL_POSITION
&&
1641 * Don't go past the max position we're allowed.
1645 if (epos
== NULL_POSITION
|| epos
> spos
)
1647 int search_type
= SRCH_FORW
| SRCH_FIND_ALL
;
1648 search_type
|= (search_info
.search_type
& SRCH_NO_REGEX
);
1651 result
= search_range(spos
, epos
, search_type
, 0, maxlines
, (POSITION
*)NULL
, &new_epos
);
1654 if (prep_endpos
== NULL_POSITION
|| new_epos
> prep_endpos
)
1655 nprep_endpos
= new_epos
;
1658 * Check both ends of the resulting prep region to
1659 * make sure they're not filtered. If they are,
1660 * keep going at least one more line until we find
1661 * something that isn't filtered, or hit the end.
1663 if (prep_endpos
== NULL_POSITION
|| nprep_endpos
> prep_endpos
)
1665 if (new_epos
>= nprep_endpos
&& is_filtered(new_epos
-1))
1667 spos
= nprep_endpos
;
1668 epos
= forw_raw_line(nprep_endpos
, (char **)NULL
, (int *)NULL
);
1669 if (epos
== NULL_POSITION
)
1676 if (prep_startpos
== NULL_POSITION
|| nprep_startpos
< prep_startpos
)
1678 if (nprep_startpos
> 0 && is_filtered(nprep_startpos
))
1680 epos
= nprep_startpos
;
1681 spos
= back_raw_line(nprep_startpos
, (char **)NULL
, (int *)NULL
);
1682 if (spos
== NULL_POSITION
)
1684 nprep_startpos
= spos
;
1692 prep_startpos
= nprep_startpos
;
1693 prep_endpos
= nprep_endpos
;
1697 * Set the pattern to be used for line filtering.
1700 set_filter_pattern(pattern
, search_type
)
1705 if (pattern
== NULL
|| *pattern
== '\0')
1706 clear_pattern(&filter_info
);
1708 set_pattern(&filter_info
, pattern
, search_type
);
1713 * Is there a line filter in effect?
1718 if (ch_getflags() & CH_HELPFILE
)
1720 return prev_pattern(&filter_info
);
1726 * This function is called by the V8 regcomp to report
1727 * errors in regular expressions.
1729 public int reg_show_error
= 1;
1737 if (!reg_show_error
)