2 * Copyright (C) 1984-2023 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;
29 extern int proc_backspace;
30 extern int proc_return;
32 extern int status_col;
33 extern void *ml_search;
34 extern POSITION start_attnpos;
35 extern POSITION end_attnpos;
37 extern int screen_trashed;
41 extern int nosearch_headers;
42 extern int header_lines;
43 extern int header_cols;
45 extern int hilite_search;
46 extern int size_linebuf;
48 extern int can_goto_line;
49 static int hide_hilite;
50 static POSITION prep_startpos;
51 static POSITION prep_endpos;
52 extern POSITION xxpos;
55 * Structures for maintaining a set of ranges for hilites and filtered-out
56 * lines. Each range is stored as a node within a red-black tree, and we
57 * try to extend existing ranges (without creating overlaps) rather than
58 * create new nodes if possible. We remember the last node found by a
59 * search for constant-time lookup if the next search is near enough to
60 * the previous. To aid that, we overlay a secondary doubly-linked list
61 * on top of the red-black tree so we can find the preceding/succeeding
62 * nodes also in constant time.
64 * Each node is allocated from a series of pools, each pool double the size
65 * of the previous (for amortised constant time allocation). Since our only
66 * tree operations are clear and node insertion, not node removal, we don't
67 * need to maintain a usage bitmap or freelist and can just return nodes
68 * from the pool in-order until capacity is reached.
78 struct hilite_node *parent;
79 struct hilite_node *left;
80 struct hilite_node *right;
81 struct hilite_node *prev;
82 struct hilite_node *next;
90 struct hilite_storage *next;
91 struct hilite_node *nodes;
95 struct hilite_storage *first;
96 struct hilite_storage *current;
97 struct hilite_node *root;
98 struct hilite_node *lookaside;
100 #define HILITE_INITIALIZER() { NULL, NULL, NULL, NULL }
101 #define HILITE_LOOKASIDE_STEPS 2
103 static struct hilite_tree hilite_anchor = HILITE_INITIALIZER();
104 static struct hilite_tree filter_anchor = HILITE_INITIALIZER();
105 static struct pattern_info *filter_infos = NULL;
110 * These are the static variables that represent the "remembered"
111 * search pattern and filter pattern.
113 struct pattern_info {
114 PATTERN_TYPE compiled;
117 int is_ucase_pattern;
118 struct pattern_info *next;
122 #define info_compiled(info) ((void*)0)
124 #define info_compiled(info) ((info)->compiled)
127 static struct pattern_info search_info;
128 public int is_caseless;
131 * Are there any uppercase letters in this string?
133 static int is_ucase(char *str)
135 char *str_end = str + strlen(str);
138 while (str < str_end)
140 ch = step_char(&str, +1, str_end);
148 * Discard a saved pattern.
150 static void clear_pattern(struct pattern_info *info)
152 if (info->text != NULL)
156 uncompile_pattern(&info->compiled);
161 * Compile and save a search pattern.
163 static int set_pattern(struct pattern_info *info, char *pattern, int search_type, int show_error)
166 * Ignore case if -I is set OR
167 * -i is set AND the pattern is all lowercase.
169 info->is_ucase_pattern = (pattern == NULL) ? FALSE : is_ucase(pattern);
170 is_caseless = (info->is_ucase_pattern && caseless != OPT_ONPLUS) ? 0 : caseless;
173 SET_NULL_PATTERN(info->compiled);
174 else if (compile_pattern(pattern, search_type, show_error, &info->compiled) < 0)
177 /* Pattern compiled successfully; save the text too. */
178 if (info->text != NULL)
183 info->text = (char *) ecalloc(1, strlen(pattern)+1);
184 strcpy(info->text, pattern);
186 info->search_type = search_type;
191 * Initialize saved pattern to nothing.
193 static void init_pattern(struct pattern_info *info)
195 SET_NULL_PATTERN(info->compiled);
197 info->search_type = 0;
202 * Initialize search variables.
204 public void init_search(void)
206 init_pattern(&search_info);
210 * Determine which text conversions to perform before pattern matching.
212 static int get_cvt_ops(int search_type)
216 if (is_caseless && (!re_handles_caseless || (search_type & SRCH_NO_REGEX)))
218 if (proc_backspace == OPT_ON || (bs_mode == BS_SPECIAL && proc_backspace == OPT_OFF))
220 if (proc_return == OPT_ON || (bs_mode != BS_CONTROL && proc_backspace == OPT_OFF))
222 if (ctldisp == OPT_ONPLUS)
228 * Is there a previous (remembered) search pattern?
230 static int prev_pattern(struct pattern_info *info)
233 if ((info->search_type & SRCH_NO_REGEX) == 0)
234 return (!is_null_pattern(info->compiled));
236 return (info->text != NULL);
241 * Repaint the hilites currently displayed on the screen.
242 * Repaint each line which contains highlighted text.
243 * If on==0, force all hilites off.
245 public void repaint_hilite(int on)
249 int save_hide_hilite;
254 save_hide_hilite = hide_hilite;
265 hide_hilite = save_hide_hilite;
269 for (sindex = TOP; sindex < TOP + sc_height-1; sindex++)
271 pos = position(sindex);
272 if (pos == NULL_POSITION)
274 (void) forw_line(pos);
281 hide_hilite = save_hide_hilite;
286 * Clear the attn hilite.
288 public void clear_attn(void)
292 POSITION old_start_attnpos;
293 POSITION old_end_attnpos;
298 if (start_attnpos == NULL_POSITION)
300 old_start_attnpos = start_attnpos;
301 old_end_attnpos = end_attnpos;
302 start_attnpos = end_attnpos = NULL_POSITION;
312 for (sindex = TOP; sindex < TOP + sc_height-1; sindex++)
314 pos = position(sindex);
315 if (pos == NULL_POSITION)
317 epos = position(sindex+1);
318 if (pos <= old_end_attnpos &&
319 (epos == NULL_POSITION || epos > old_start_attnpos))
321 (void) forw_line(pos);
328 if (overlay_header())
336 * Toggle or clear search string highlighting.
338 public void undo_search(int clear)
340 clear_pattern(&search_info);
347 if (hilite_anchor.first == NULL)
349 error("No previous regular expression", NULL_PARG);
352 hide_hilite = !hide_hilite;
360 * Clear the hilite list.
362 public void clr_hlist(struct hilite_tree *anchor)
364 struct hilite_storage *hls;
365 struct hilite_storage *nexthls;
367 for (hls = anchor->first; hls != NULL; hls = nexthls)
370 free((void*)hls->nodes);
373 anchor->first = NULL;
374 anchor->current = NULL;
377 anchor->lookaside = NULL;
379 prep_startpos = prep_endpos = NULL_POSITION;
382 public void clr_hilite(void)
384 clr_hlist(&hilite_anchor);
387 public void clr_filter(void)
389 clr_hlist(&filter_anchor);
393 * Find the node covering pos, or the node after it if no node covers it,
394 * or return NULL if pos is after the last range. Remember the found node,
395 * to speed up subsequent searches for the same or similar positions (if
396 * we return NULL, remember the last node.)
398 static struct hilite_node* hlist_find(struct hilite_tree *anchor, POSITION pos)
400 struct hilite_node *n, *m;
402 if (anchor->lookaside)
407 n = anchor->lookaside;
411 if (pos < n->r.hl_endpos)
413 if (n->prev == NULL || pos >= n->prev->r.hl_endpos)
418 } else if (n->next == NULL)
426 * If we don't find the right node within a small
427 * distance, don't keep doing a linear search!
429 if (steps >= HILITE_LOOKASIDE_STEPS)
433 if (pos < n->r.hl_endpos)
434 anchor->lookaside = n = n->prev;
436 anchor->lookaside = n = n->next;
448 if (pos < n->r.hl_startpos)
458 if (pos >= n->r.hl_endpos)
460 if (n->right != NULL)
478 anchor->lookaside = n;
480 anchor->lookaside = m;
486 * Should any characters in a specified range be highlighted?
488 static int hilited_range_attr(POSITION pos, POSITION epos)
490 struct hilite_node *n = hlist_find(&hilite_anchor, pos);
493 if (epos != NULL_POSITION && epos <= n->r.hl_startpos)
499 * Is a line "filtered" -- that is, should it be hidden?
501 public int is_filtered(POSITION pos)
503 struct hilite_node *n;
505 if (ch_getflags() & CH_HELPFILE)
508 n = hlist_find(&filter_anchor, pos);
509 return (n != NULL && pos >= n->r.hl_startpos);
513 * If pos is hidden, return the next position which isn't, otherwise
516 public POSITION next_unfiltered(POSITION pos)
518 struct hilite_node *n;
520 if (ch_getflags() & CH_HELPFILE)
523 n = hlist_find(&filter_anchor, pos);
524 while (n != NULL && pos >= n->r.hl_startpos)
526 pos = n->r.hl_endpos;
533 * If pos is hidden, return the previous position which isn't or 0 if
534 * we're filtered right to the beginning, otherwise just return pos.
536 public POSITION prev_unfiltered(POSITION pos)
538 struct hilite_node *n;
540 if (ch_getflags() & CH_HELPFILE)
543 n = hlist_find(&filter_anchor, pos);
544 while (n != NULL && pos >= n->r.hl_startpos)
546 pos = n->r.hl_startpos;
557 * Should any characters in a specified range be highlighted?
558 * If nohide is nonzero, don't consider hide_hilite.
560 public int is_hilited_attr(POSITION pos, POSITION epos, int nohide, int *p_matches)
564 if (p_matches != NULL)
568 start_attnpos != NULL_POSITION &&
569 pos <= end_attnpos &&
570 (epos == NULL_POSITION || epos >= start_attnpos))
572 * The attn line overlaps this range.
574 return (AT_HILITE|AT_COLOR_ATTN);
576 attr = hilited_range_attr(pos, epos);
580 if (p_matches == NULL)
582 * Kinda kludgy way to recognize that caller is checking for
583 * hilite in status column. In this case we want to return
584 * hilite status even if hiliting is disabled or hidden.
589 * Report matches, even if we're hiding highlights.
593 if (hilite_search == 0)
595 * Not doing highlighting.
599 if (!nohide && hide_hilite)
601 * Highlighting is hidden.
609 * Tree node storage: get the current block of nodes if it has spare
610 * capacity, or create a new one if not.
612 static struct hilite_storage * hlist_getstorage(struct hilite_tree *anchor)
615 struct hilite_storage *s;
619 if (anchor->current->used < anchor->current->capacity)
620 return anchor->current;
621 capacity = anchor->current->capacity * 2;
624 s = (struct hilite_storage *) ecalloc(1, sizeof(struct hilite_storage));
625 s->nodes = (struct hilite_node *) ecalloc(capacity, sizeof(struct hilite_node));
626 s->capacity = capacity;
630 anchor->current->next = s;
638 * Tree node storage: retrieve a new empty node to be inserted into the
641 static struct hilite_node * hlist_getnode(struct hilite_tree *anchor)
643 struct hilite_storage *s = hlist_getstorage(anchor);
644 return &s->nodes[s->used++];
648 * Rotate the tree left around a pivot node.
650 static void hlist_rotate_left(struct hilite_tree *anchor, struct hilite_node *n)
652 struct hilite_node *np = n->parent;
653 struct hilite_node *nr = n->right;
654 struct hilite_node *nrl = n->right->left;
676 * Rotate the tree right around a pivot node.
678 static void hlist_rotate_right(struct hilite_tree *anchor, struct hilite_node *n)
680 struct hilite_node *np = n->parent;
681 struct hilite_node *nl = n->left;
682 struct hilite_node *nlr = n->left->right;
705 * Add a new hilite to a hilite list.
707 static void add_hilite(struct hilite_tree *anchor, struct hilite *hl)
709 struct hilite_node *p, *n, *u;
711 /* Ignore empty ranges. */
712 if (hl->hl_startpos >= hl->hl_endpos)
717 /* Inserting the very first node is trivial. */
720 n = hlist_getnode(anchor);
723 anchor->lookaside = n;
728 * Find our insertion point. If we come across any overlapping
729 * or adjoining existing ranges, shrink our range and discard
730 * if it become empty.
734 if (hl->hl_startpos < p->r.hl_startpos)
736 if (hl->hl_endpos > p->r.hl_startpos && hl->hl_attr == p->r.hl_attr)
737 hl->hl_endpos = p->r.hl_startpos;
745 if (hl->hl_startpos < p->r.hl_endpos && hl->hl_attr == p->r.hl_attr) {
746 hl->hl_startpos = p->r.hl_endpos;
747 if (hl->hl_startpos >= hl->hl_endpos)
750 if (p->right != NULL)
759 * Now we're at the right leaf, again check for contiguous ranges
760 * and extend the existing node if possible to avoid the
761 * insertion. Otherwise insert a new node at the leaf.
763 if (hl->hl_startpos < p->r.hl_startpos) {
764 if (hl->hl_attr == p->r.hl_attr)
766 if (hl->hl_endpos == p->r.hl_startpos)
768 p->r.hl_startpos = hl->hl_startpos;
771 if (p->prev != NULL && p->prev->r.hl_endpos == hl->hl_startpos)
773 p->prev->r.hl_endpos = hl->hl_endpos;
777 p->left = n = hlist_getnode(anchor);
786 if (hl->hl_attr == p->r.hl_attr)
788 if (p->r.hl_endpos == hl->hl_startpos)
790 p->r.hl_endpos = hl->hl_endpos;
793 if (p->next != NULL && hl->hl_endpos == p->next->r.hl_startpos) {
794 p->next->r.hl_startpos = hl->hl_startpos;
798 p->right = n = hlist_getnode(anchor);
812 * The tree is in the correct order and covers the right ranges
813 * now, but may have become unbalanced. Rebalance it using the
814 * standard red-black tree constraints and operations.
818 /* case 1 - current is root, root is always black */
819 if (n->parent == NULL)
825 /* case 2 - parent is black, we can always be red */
830 * constraint: because the root must be black, if our
831 * parent is red it cannot be the root therefore we must
836 * case 3 - parent and uncle are red, repaint them black,
837 * the grandparent red, and start again at the grandparent.
839 u = n->parent->parent->left;
841 u = n->parent->parent->right;
842 if (u != NULL && u->red)
846 n = n->parent->parent;
852 * case 4 - parent is red but uncle is black, parent and
853 * grandparent on opposite sides. We need to start
854 * changing the structure now. This and case 5 will shorten
855 * our branch and lengthen the sibling, between them
858 if (n == n->parent->right &&
859 n->parent == n->parent->parent->left)
861 hlist_rotate_left(anchor, n->parent);
863 } else if (n == n->parent->left &&
864 n->parent == n->parent->parent->right)
866 hlist_rotate_right(anchor, n->parent);
871 * case 5 - parent is red but uncle is black, parent and
872 * grandparent on same side
875 n->parent->parent->red = 1;
876 if (n == n->parent->left)
877 hlist_rotate_right(anchor, n->parent->parent);
879 hlist_rotate_left(anchor, n->parent->parent);
885 * Highlight every character in a range of displayed characters.
887 static void create_hilites(POSITION linepos, char *line, char *sp, char *ep, int attr, int *chpos)
889 int start_index = sp - line;
890 int end_index = ep - line;
894 /* Start the first hilite. */
895 hl.hl_startpos = linepos + chpos[start_index];
899 * Step through the displayed chars.
900 * If the source position (before cvt) of the char is one more
901 * than the source pos of the previous char (the usual case),
902 * just increase the size of the current hilite by one.
903 * Otherwise (there are backspaces or something involved),
904 * finish the current hilite and start a new one.
906 for (i = start_index+1; i <= end_index; i++)
908 if (chpos[i] != chpos[i-1] + 1 || i == end_index)
910 hl.hl_endpos = linepos + chpos[i-1] + 1;
911 add_hilite(&hilite_anchor, &hl);
912 /* Start new hilite unless this is the last char. */
915 hl.hl_startpos = linepos + chpos[i];
922 * Make a hilite for each string in a physical line which matches
923 * the current pattern.
924 * sp,ep delimit the first match already found.
926 static void hilite_line(POSITION linepos, char *line, int line_len, int *chpos, char **sp, char **ep, int nsp, int cvt_ops)
929 char *line_end = line + line_len;
932 * sp[0] and ep[0] delimit the first match in the line.
933 * Mark the corresponding file positions, then
934 * look for further matches and mark them.
935 * {{ This technique, of calling match_pattern on subsequent
936 * substrings of the line, may mark more than is correct
937 * if the pattern starts with "^". This bug is fixed
938 * for those regex functions that accept a notbol parameter
939 * (currently POSIX, PCRE and V8-with-regexec2). }}
940 * sp[i] and ep[i] for i>0 delimit subpattern matches.
941 * Color each of them with its unique color.
947 if (sp[0] == NULL || ep[0] == NULL)
949 for (i = 1; i < nsp; i++)
951 if (sp[i] == NULL || ep[i] == NULL)
955 create_hilites(linepos, line, lep, sp[i],
956 AT_HILITE | AT_COLOR_SEARCH, chpos);
957 create_hilites(linepos, line, sp[i], ep[i],
958 AT_HILITE | AT_COLOR_SUBSEARCH(i), chpos);
962 create_hilites(linepos, line, lep, ep[0],
963 AT_HILITE | AT_COLOR_SEARCH, chpos);
966 * If we matched more than zero characters,
967 * move to the first char after the string we matched.
968 * If we matched zero, just move to the next char.
972 else if (searchp != line_end)
974 else /* end of line */
976 } while (match_pattern(info_compiled(&search_info), search_info.text,
977 searchp, line_end - searchp, sp, ep, nsp, 1, search_info.search_type));
983 * Find matching text which is currently on screen and highlight it.
985 static void hilite_screen(void)
987 struct scrpos scrpos;
989 get_scrpos(&scrpos, TOP);
990 if (scrpos.pos == NULL_POSITION)
992 prep_hilite(scrpos.pos, position(BOTTOM_PLUS_ONE), -1);
997 * Change highlighting parameters.
999 public void chg_hilite(void)
1002 * Erase any highlights currently on screen.
1007 if (hilite_search == OPT_ONPLUS)
1009 * Display highlights.
1016 * Figure out where to start a search.
1018 static POSITION search_pos(int search_type)
1026 * Start at the beginning (or end) of the file.
1027 * The empty_screen() case is mainly for
1028 * command line initiated searches;
1029 * for example, "+/xyz" on the command line.
1030 * Also for multi-file (SRCH_PAST_EOF) searches.
1032 if (search_type & SRCH_FORW)
1038 if (pos == NULL_POSITION)
1040 (void) ch_end_seek();
1049 if (how_search == OPT_ON)
1052 * Search does not include current screen.
1054 if (search_type & SRCH_FORW)
1055 sindex = sc_height-1; /* BOTTOM_PLUS_ONE */
1057 sindex = 0; /* TOP */
1058 } else if (how_search == OPT_ONPLUS && !(search_type & SRCH_AFTER_TARGET))
1061 * Search includes all of displayed screen.
1063 if (search_type & SRCH_FORW)
1064 sindex = 0; /* TOP */
1066 sindex = sc_height-1; /* BOTTOM_PLUS_ONE */
1070 * Search includes the part of current screen beyond the jump target.
1071 * It starts at the jump target (if searching backwards),
1072 * or at the jump target plus one (if forwards).
1074 sindex = sindex_from_sline(jump_sline);
1075 if (search_type & SRCH_FORW)
1078 pos = position(sindex);
1080 pos = forw_raw_line(pos, (char **)NULL, (int *)NULL);
1084 * If the line is empty, look around for a plausible starting place.
1086 if (search_type & SRCH_FORW)
1088 while (pos == NULL_POSITION)
1090 if (++sindex >= sc_height)
1092 pos = position(sindex);
1096 while (pos == NULL_POSITION)
1100 pos = position(sindex);
1107 * Check to see if the line matches the filter pattern.
1108 * If so, add an entry to the filter list.
1111 static int matches_filters(POSITION pos, char *cline, int line_len, int *chpos, POSITION linepos, char **sp, char **ep, int nsp)
1113 struct pattern_info *filter;
1115 for (filter = filter_infos; filter != NULL; filter = filter->next)
1117 int line_filter = match_pattern(info_compiled(filter), filter->text,
1118 cline, line_len, sp, ep, nsp, 0, filter->search_type);
1122 hl.hl_startpos = linepos;
1124 add_hilite(&filter_anchor, &hl);
1135 * Get the position of the first char in the screen line which
1136 * puts tpos on screen.
1138 static POSITION get_lastlinepos(POSITION pos, POSITION tpos, int sheight)
1142 for (nlines = 0;; nlines++)
1144 POSITION npos = forw_line(pos);
1147 if (nlines < sheight)
1148 return NULL_POSITION;
1156 * Get the segment index of tpos in the line starting at pos.
1157 * A segment is a string of printable chars that fills the screen width.
1159 static int get_seg(POSITION pos, POSITION tpos)
1163 for (seg = 0;; seg++)
1165 POSITION npos = forw_line_seg(pos, FALSE, FALSE, TRUE);
1173 * Search a subset of the file, specified by start/end position.
1175 static int search_range(POSITION pos, POSITION endpos, int search_type, int matches, int maxlines, POSITION *plinepos, POSITION *pendpos, POSITION *plastlinepos)
1181 #define NSP (NUM_SEARCH_COLORS+2)
1188 POSITION linepos, oldpos;
1190 int swidth = sc_width - line_pfx_width();
1191 int sheight = sc_height - sindex_from_sline(jump_sline);
1193 linenum = find_linenum(pos);
1194 if (nosearch_headers && linenum <= header_lines)
1196 linenum = header_lines + 1;
1197 pos = find_pos(linenum);
1199 if (pos == NULL_POSITION)
1202 /* When the search wraps around, end at starting position. */
1203 if ((search_type & SRCH_WRAP) && endpos == NULL_POSITION)
1208 * Get lines until we find a matching one or until
1209 * we hit end-of-file (or beginning-of-file if we're
1210 * going backwards), or until we hit the end position.
1215 * A signal aborts the search.
1220 if ((endpos != NULL_POSITION && !(search_type & SRCH_WRAP) &&
1221 (((search_type & SRCH_FORW) && pos >= endpos) ||
1222 ((search_type & SRCH_BACK) && pos <= endpos))) || maxlines == 0)
1225 * Reached end position without a match.
1227 if (pendpos != NULL)
1234 if (search_type & SRCH_FORW)
1237 * Read the next line, and save the
1238 * starting position of that line in linepos.
1241 pos = forw_raw_line(pos, &line, &line_len);
1247 * Read the previous line and save the
1248 * starting position of that line in linepos.
1250 pos = back_raw_line(pos, &line, &line_len);
1256 if (pos == NULL_POSITION)
1259 * Reached EOF/BOF without a match.
1261 if (search_type & SRCH_WRAP)
1264 * The search wraps around the current file, so
1265 * try to continue at BOF/EOF.
1267 if (search_type & SRCH_FORW)
1273 if (pos == NULL_POSITION)
1275 (void) ch_end_seek();
1279 if (pos != NULL_POSITION) {
1281 * Wrap-around was successful. Clear
1282 * the flag so we don't wrap again, and
1283 * continue the search at new pos.
1285 search_type &= ~SRCH_WRAP;
1286 linenum = find_linenum(pos);
1290 if (pendpos != NULL)
1296 * If we're using line numbers, we might as well
1297 * remember the information we have now (the position
1298 * and line number of the current line).
1299 * Don't do it for every line because it slows down
1300 * the search. Remember the line number only if
1301 * we're "far" from the last place we remembered it.
1303 if (linenums && abs((int)(pos - oldpos)) > 2048)
1304 add_lnum(linenum, pos);
1308 if (is_filtered(linepos))
1311 if (nosearch_headers)
1312 skip_bytes = skip_columns(header_cols, &line, &line_len);
1315 * If it's a caseless search, convert the line to lowercase.
1316 * If we're doing backspace processing, delete backspaces.
1318 cvt_ops = get_cvt_ops(search_type);
1319 cvt_len = cvt_length(line_len, cvt_ops);
1320 cline = (char *) ecalloc(1, cvt_len);
1321 chpos = cvt_alloc_chpos(cvt_len);
1322 cvt_text(cline, line, chpos, &line_len, cvt_ops);
1326 * If any filters are in effect, ignore non-matching lines.
1328 if (filter_infos != NULL &&
1329 ((search_type & SRCH_FIND_ALL) ||
1330 prep_startpos == NULL_POSITION ||
1331 linepos < prep_startpos || linepos >= prep_endpos)) {
1332 if (matches_filters(pos, cline, line_len, chpos, linepos, sp, ep, NSP))
1338 * Test the next line to see if we have a match.
1339 * We are successful if we either want a match and got one,
1340 * or if we want a non-match and got one.
1342 if (prev_pattern(&search_info))
1344 line_match = match_pattern(info_compiled(&search_info), search_info.text,
1345 cline, line_len, sp, ep, NSP, 0, search_type);
1351 if (search_type & SRCH_FIND_ALL)
1355 * We are supposed to find all matches in the range.
1356 * Just add the matches in this line to the
1357 * hilite list and keep searching.
1359 hilite_line(linepos + skip_bytes, cline, line_len, chpos, sp, ep, NSP, cvt_ops);
1361 } else if (--matches <= 0)
1364 * Found the one match we're looking for.
1368 if (hilite_search == OPT_ON)
1371 * Clear the hilite list and add only
1372 * the matches in this one line.
1375 hilite_line(linepos + skip_bytes, cline, line_len, chpos, sp, ep, NSP, cvt_ops);
1381 * If necessary, shift horizontally to make sure
1382 * search match is fully visible.
1384 if (sp[0] != NULL && ep[0] != NULL)
1386 int start_off = sp[0] - cline;
1387 int end_off = ep[0] - cline;
1388 int save_hshift = hshift;
1391 hshift = 0; /* make get_seg count screen lines */
1392 sshift = swidth * get_seg(linepos, linepos + chpos[start_off]);
1393 eshift = swidth * get_seg(linepos, linepos + chpos[end_off]);
1394 if (sshift >= save_hshift && eshift <= save_hshift)
1396 hshift = save_hshift;
1403 } else if (plastlinepos != NULL)
1406 * If the line is so long that the highlighted match
1407 * won't be seen when the line is displayed normally
1408 * (starting at the first char) because it fills the whole
1409 * screen and more, scroll forward until the last char
1410 * of the match appears in the last line on the screen.
1411 * lastlinepos is the position of the first char of that last line.
1415 int end_off = ep[0] - cline;
1416 if (end_off >= swidth * sheight / 4) /* heuristic */
1417 *plastlinepos = get_lastlinepos(linepos, linepos + chpos[end_off], sheight);
1422 if (plinepos != NULL)
1423 *plinepos = linepos;
1434 * search for a pattern in history. If found, compile that pattern.
1436 static int hist_pattern(int search_type)
1441 set_mlist(ml_search, 0);
1442 pattern = cmd_lastpattern();
1443 if (pattern == NULL)
1446 if (set_pattern(&search_info, pattern, search_type, 1) < 0)
1450 if (hilite_search == OPT_ONPLUS && !hide_hilite)
1455 #else /* CMD_HISTORY */
1457 #endif /* CMD_HISTORY */
1461 * Change the caseless-ness of searches.
1462 * Updates the internal search state to reflect a change in the -i flag.
1464 public void chg_caseless(void)
1466 if (!search_info.is_ucase_pattern)
1469 * Pattern did not have uppercase.
1470 * Set the search caselessness to the global caselessness.
1472 is_caseless = caseless;
1474 * If regex handles caseless, we need to discard
1475 * the pattern which was compiled with the old caseless.
1477 if (!re_handles_caseless)
1478 /* We handle caseless, so the pattern doesn't change. */
1482 * Regenerate the pattern using the new state.
1484 clear_pattern(&search_info);
1485 (void) hist_pattern(search_info.search_type);
1489 * Search for the n-th occurrence of a specified pattern,
1490 * either forward or backward.
1491 * Return the number of matches not yet found in this file
1492 * (that is, n minus the number of matches found).
1493 * Return -1 if the search should be aborted.
1494 * Caller may continue the search in another file
1495 * if less than n matches are found in this file.
1497 public int search(int search_type, char *pattern, int n)
1501 POSITION lastlinepos = NULL_POSITION;
1503 if (pattern == NULL || *pattern == '\0')
1506 * A null pattern means use the previously compiled pattern.
1508 search_type |= SRCH_AFTER_TARGET;
1509 if (!prev_pattern(&search_info))
1511 int r = hist_pattern(search_type);
1513 error("No previous regular expression", NULL_PARG);
1517 if ((search_type & SRCH_NO_REGEX) !=
1518 (search_info.search_type & SRCH_NO_REGEX))
1520 error("Please re-enter search pattern", NULL_PARG);
1524 if (hilite_search == OPT_ON || status_col)
1527 * Erase the highlights currently on screen.
1528 * If the search fails, we'll redisplay them later.
1532 if (hilite_search == OPT_ONPLUS && hide_hilite)
1535 * Highlight any matches currently on screen,
1536 * before we actually start the search.
1546 * Compile the pattern.
1548 int show_error = !(search_type & SRCH_INCR);
1549 if (set_pattern(&search_info, pattern, search_type, show_error) < 0)
1552 if (hilite_search || status_col)
1555 * Erase the highlights currently on screen.
1556 * Also permanently delete them from the hilite list.
1562 if (hilite_search == OPT_ONPLUS || status_col)
1565 * Highlight any matches currently on screen,
1566 * before we actually start the search.
1574 * Figure out where to start the search.
1576 pos = search_pos(search_type);
1577 opos = position(sindex_from_sline(jump_sline));
1578 if (pos == NULL_POSITION)
1581 * Can't find anyplace to start searching from.
1583 if (search_type & SRCH_PAST_EOF)
1586 if (hilite_search == OPT_ON || status_col)
1589 error("Nothing to search", NULL_PARG);
1593 n = search_range(pos, NULL_POSITION, search_type, n, -1,
1594 &pos, (POSITION*)NULL, &lastlinepos);
1598 * Search was unsuccessful.
1601 if ((hilite_search == OPT_ON || status_col) && n > 0)
1603 * Redisplay old hilites.
1610 if (!(search_type & SRCH_NO_MOVE))
1613 * Go to the matching line.
1615 if (lastlinepos != NULL_POSITION)
1616 jump_loc(lastlinepos, BOTTOM);
1617 else if (pos != opos)
1618 jump_loc(pos, jump_sline);
1622 if (hilite_search == OPT_ON || status_col)
1624 * Display new hilites in the matching line.
1633 * Prepare hilites in a given range of the file.
1635 * The pair (prep_startpos,prep_endpos) delimits a contiguous region
1636 * of the file that has been "prepared"; that is, scanned for matches for
1637 * the current search pattern, and hilites have been created for such matches.
1638 * If prep_startpos == NULL_POSITION, the prep region is empty.
1639 * If prep_endpos == NULL_POSITION, the prep region extends to EOF.
1640 * prep_hilite asks that the range (spos,epos) be covered by the prep region.
1642 public void prep_hilite(POSITION spos, POSITION epos, int maxlines)
1644 POSITION nprep_startpos = prep_startpos;
1645 POSITION nprep_endpos = prep_endpos;
1652 * Search beyond where we're asked to search, so the prep region covers
1653 * more than we need. Do one big search instead of a bunch of small ones.
1655 #define SEARCH_MORE (3*size_linebuf)
1657 if (!prev_pattern(&search_info) && !is_filtering())
1661 * Make sure our prep region always starts at the beginning of
1662 * a line. (search_range takes care of the end boundary below.)
1664 spos = back_raw_line(spos+1, (char **)NULL, (int *)NULL);
1667 * If we're limited to a max number of lines, figure out the
1668 * file position we should stop at.
1671 max_epos = NULL_POSITION;
1675 for (i = 0; i < maxlines; i++)
1676 max_epos = forw_raw_line(max_epos, (char **)NULL, (int *)NULL);
1681 * The range that we need to search (spos,epos); and the range that
1682 * the "prep" region will then cover (nprep_startpos,nprep_endpos).
1685 if (prep_startpos == NULL_POSITION ||
1686 (epos != NULL_POSITION && epos < prep_startpos) ||
1690 * New range is not contiguous with old prep region.
1691 * Discard the old prep region and start a new one.
1695 if (epos != NULL_POSITION)
1696 epos += SEARCH_MORE;
1697 nprep_startpos = spos;
1701 * New range partially or completely overlaps old prep region.
1703 if (epos == NULL_POSITION)
1706 * New range goes to end of file.
1709 } else if (epos > prep_endpos)
1712 * New range ends after old prep region.
1713 * Extend prep region to end at end of new range.
1715 epos += SEARCH_MORE;
1716 } else /* (epos <= prep_endpos) */
1719 * New range ends within old prep region.
1720 * Truncate search to end at start of old prep region.
1722 epos = prep_startpos;
1725 if (spos < prep_startpos)
1728 * New range starts before old prep region.
1729 * Extend old prep region backwards to start at
1730 * start of new range.
1732 if (spos < SEARCH_MORE)
1735 spos -= SEARCH_MORE;
1736 nprep_startpos = spos;
1737 } else /* (spos >= prep_startpos) */
1740 * New range starts within or after old prep region.
1741 * Trim search to start at end of old prep region.
1747 if (epos != NULL_POSITION && max_epos != NULL_POSITION &&
1750 * Don't go past the max position we're allowed.
1754 if (epos == NULL_POSITION || epos > spos)
1756 int search_type = SRCH_FORW | SRCH_FIND_ALL;
1757 search_type |= (search_info.search_type & (SRCH_NO_REGEX|SRCH_SUBSEARCH_ALL));
1760 result = search_range(spos, epos, search_type, 0, maxlines, (POSITION*)NULL, &new_epos, (POSITION*)NULL);
1763 if (prep_endpos == NULL_POSITION || new_epos > prep_endpos)
1764 nprep_endpos = new_epos;
1767 * Check both ends of the resulting prep region to
1768 * make sure they're not filtered. If they are,
1769 * keep going at least one more line until we find
1770 * something that isn't filtered, or hit the end.
1772 if (prep_endpos == NULL_POSITION || nprep_endpos > prep_endpos)
1774 if (new_epos >= nprep_endpos && is_filtered(new_epos-1))
1776 spos = nprep_endpos;
1777 epos = forw_raw_line(nprep_endpos, (char **)NULL, (int *)NULL);
1778 if (epos == NULL_POSITION)
1785 if (prep_startpos == NULL_POSITION || nprep_startpos < prep_startpos)
1787 if (nprep_startpos > 0 && is_filtered(nprep_startpos))
1789 epos = nprep_startpos;
1790 spos = back_raw_line(nprep_startpos, (char **)NULL, (int *)NULL);
1791 if (spos == NULL_POSITION)
1793 nprep_startpos = spos;
1801 prep_startpos = nprep_startpos;
1802 prep_endpos = nprep_endpos;
1806 * Set the pattern to be used for line filtering.
1808 public void set_filter_pattern(char *pattern, int search_type)
1810 struct pattern_info *filter;
1813 if (pattern == NULL || *pattern == '\0')
1815 /* Clear and free all filters. */
1816 for (filter = filter_infos; filter != NULL; )
1818 struct pattern_info *next_filter = filter->next;
1819 clear_pattern(filter);
1821 filter = next_filter;
1823 filter_infos = NULL;
1826 /* Create a new filter and add it to the filter_infos list. */
1827 filter = ecalloc(1, sizeof(struct pattern_info));
1828 init_pattern(filter);
1829 if (set_pattern(filter, pattern, search_type, 1) < 0)
1834 filter->next = filter_infos;
1835 filter_infos = filter;
1841 * Is there a line filter in effect?
1843 public int is_filtering(void)
1845 if (ch_getflags() & CH_HELPFILE)
1847 return (filter_infos != NULL);
1853 * This function is called by the V8 regcomp to report
1854 * errors in regular expressions.
1856 public int reg_show_error = 1;
1858 void regerror(char *s)
1862 if (!reg_show_error)