xref: /dragonfly/contrib/less/search.c (revision 320d7c8a)
11133e27eSPeter Avalos /*
2*320d7c8aSAaron LI  * Copyright (C) 1984-2023  Mark Nudelman
31133e27eSPeter Avalos  *
41133e27eSPeter Avalos  * You may distribute under the terms of either the GNU General Public
51133e27eSPeter Avalos  * License or the Less License, as specified in the README file.
61133e27eSPeter Avalos  *
7e639dc31SJohn Marino  * For more information, see the README file.
81133e27eSPeter Avalos  */
91133e27eSPeter Avalos 
101133e27eSPeter Avalos 
111133e27eSPeter Avalos /*
121133e27eSPeter Avalos  * Routines to search a file for a pattern.
131133e27eSPeter Avalos  */
141133e27eSPeter Avalos 
151133e27eSPeter Avalos #include "less.h"
161133e27eSPeter Avalos #include "position.h"
171133e27eSPeter Avalos #include "charset.h"
181133e27eSPeter Avalos 
191133e27eSPeter Avalos #define MINPOS(a,b)     (((a) < (b)) ? (a) : (b))
201133e27eSPeter Avalos #define MAXPOS(a,b)     (((a) > (b)) ? (a) : (b))
211133e27eSPeter Avalos 
221133e27eSPeter Avalos extern int sigs;
231133e27eSPeter Avalos extern int how_search;
241133e27eSPeter Avalos extern int caseless;
251133e27eSPeter Avalos extern int linenums;
261133e27eSPeter Avalos extern int sc_height;
271133e27eSPeter Avalos extern int jump_sline;
281133e27eSPeter Avalos extern int bs_mode;
29*320d7c8aSAaron LI extern int proc_backspace;
30*320d7c8aSAaron LI extern int proc_return;
311133e27eSPeter Avalos extern int ctldisp;
321133e27eSPeter Avalos extern int status_col;
3302d62a0fSDaniel Fojt extern void *ml_search;
341133e27eSPeter Avalos extern POSITION start_attnpos;
351133e27eSPeter Avalos extern POSITION end_attnpos;
368be36e5bSPeter Avalos extern int utf_mode;
378be36e5bSPeter Avalos extern int screen_trashed;
380c7ad07eSAntonio Huete Jimenez extern int sc_width;
390c7ad07eSAntonio Huete Jimenez extern int sc_height;
400c7ad07eSAntonio Huete Jimenez extern int hshift;
41*320d7c8aSAaron LI extern int nosearch_headers;
42*320d7c8aSAaron LI extern int header_lines;
43*320d7c8aSAaron LI extern int header_cols;
441133e27eSPeter Avalos #if HILITE_SEARCH
451133e27eSPeter Avalos extern int hilite_search;
461133e27eSPeter Avalos extern int size_linebuf;
471133e27eSPeter Avalos extern int squished;
481133e27eSPeter Avalos extern int can_goto_line;
491133e27eSPeter Avalos static int hide_hilite;
501133e27eSPeter Avalos static POSITION prep_startpos;
511133e27eSPeter Avalos static POSITION prep_endpos;
520c7ad07eSAntonio Huete Jimenez extern POSITION xxpos;
531133e27eSPeter Avalos 
54fa0be7c5SJohn Marino /*
55fa0be7c5SJohn Marino  * Structures for maintaining a set of ranges for hilites and filtered-out
56fa0be7c5SJohn Marino  * lines. Each range is stored as a node within a red-black tree, and we
57fa0be7c5SJohn Marino  * try to extend existing ranges (without creating overlaps) rather than
58fa0be7c5SJohn Marino  * create new nodes if possible. We remember the last node found by a
59fa0be7c5SJohn Marino  * search for constant-time lookup if the next search is near enough to
60fa0be7c5SJohn Marino  * the previous. To aid that, we overlay a secondary doubly-linked list
61fa0be7c5SJohn Marino  * on top of the red-black tree so we can find the preceding/succeeding
62fa0be7c5SJohn Marino  * nodes also in constant time.
63fa0be7c5SJohn Marino  *
64fa0be7c5SJohn Marino  * Each node is allocated from a series of pools, each pool double the size
65fa0be7c5SJohn Marino  * of the previous (for amortised constant time allocation). Since our only
66fa0be7c5SJohn Marino  * tree operations are clear and node insertion, not node removal, we don't
67fa0be7c5SJohn Marino  * need to maintain a usage bitmap or freelist and can just return nodes
68fa0be7c5SJohn Marino  * from the pool in-order until capacity is reached.
69fa0be7c5SJohn Marino  */
701133e27eSPeter Avalos struct hilite
711133e27eSPeter Avalos {
721133e27eSPeter Avalos 	POSITION hl_startpos;
731133e27eSPeter Avalos 	POSITION hl_endpos;
74*320d7c8aSAaron LI 	int hl_attr;
751133e27eSPeter Avalos };
76fa0be7c5SJohn Marino struct hilite_node
77fa0be7c5SJohn Marino {
78fa0be7c5SJohn Marino 	struct hilite_node *parent;
79fa0be7c5SJohn Marino 	struct hilite_node *left;
80fa0be7c5SJohn Marino 	struct hilite_node *right;
81fa0be7c5SJohn Marino 	struct hilite_node *prev;
82fa0be7c5SJohn Marino 	struct hilite_node *next;
83fa0be7c5SJohn Marino 	int red;
84fa0be7c5SJohn Marino 	struct hilite r;
85fa0be7c5SJohn Marino };
86fa0be7c5SJohn Marino struct hilite_storage
87fa0be7c5SJohn Marino {
88fa0be7c5SJohn Marino 	int capacity;
89fa0be7c5SJohn Marino 	int used;
90fa0be7c5SJohn Marino 	struct hilite_storage *next;
919b760066SJohn Marino 	struct hilite_node *nodes;
92fa0be7c5SJohn Marino };
93fa0be7c5SJohn Marino struct hilite_tree
94fa0be7c5SJohn Marino {
95fa0be7c5SJohn Marino 	struct hilite_storage *first;
96fa0be7c5SJohn Marino 	struct hilite_storage *current;
97fa0be7c5SJohn Marino 	struct hilite_node *root;
98fa0be7c5SJohn Marino 	struct hilite_node *lookaside;
99fa0be7c5SJohn Marino };
100fa0be7c5SJohn Marino #define HILITE_INITIALIZER() { NULL, NULL, NULL, NULL }
101fa0be7c5SJohn Marino #define HILITE_LOOKASIDE_STEPS 2
102fa0be7c5SJohn Marino 
103fa0be7c5SJohn Marino static struct hilite_tree hilite_anchor = HILITE_INITIALIZER();
104fa0be7c5SJohn Marino static struct hilite_tree filter_anchor = HILITE_INITIALIZER();
1050c7ad07eSAntonio Huete Jimenez static struct pattern_info *filter_infos = NULL;
106fa0be7c5SJohn Marino 
1071133e27eSPeter Avalos #endif
1081133e27eSPeter Avalos 
1091133e27eSPeter Avalos /*
1101133e27eSPeter Avalos  * These are the static variables that represent the "remembered"
111a9adbba3SJan Lentfer  * search pattern and filter pattern.
1121133e27eSPeter Avalos  */
113a9adbba3SJan Lentfer struct pattern_info {
11402d62a0fSDaniel Fojt 	PATTERN_TYPE compiled;
115a9adbba3SJan Lentfer 	char* text;
116a9adbba3SJan Lentfer 	int search_type;
117*320d7c8aSAaron LI 	int is_ucase_pattern;
1180c7ad07eSAntonio Huete Jimenez 	struct pattern_info *next;
119a9adbba3SJan Lentfer };
1201133e27eSPeter Avalos 
121e639dc31SJohn Marino #if NO_REGEX
122e639dc31SJohn Marino #define info_compiled(info) ((void*)0)
123e639dc31SJohn Marino #else
124e639dc31SJohn Marino #define info_compiled(info) ((info)->compiled)
125e639dc31SJohn Marino #endif
126e639dc31SJohn Marino 
127a9adbba3SJan Lentfer static struct pattern_info search_info;
1280c7ad07eSAntonio Huete Jimenez public int is_caseless;
1291133e27eSPeter Avalos 
1301133e27eSPeter Avalos /*
13125ce721eSPeter Avalos  * Are there any uppercase letters in this string?
13225ce721eSPeter Avalos  */
is_ucase(char * str)133*320d7c8aSAaron LI static int is_ucase(char *str)
13425ce721eSPeter Avalos {
13525ce721eSPeter Avalos 	char *str_end = str + strlen(str);
13625ce721eSPeter Avalos 	LWCHAR ch;
13725ce721eSPeter Avalos 
13825ce721eSPeter Avalos 	while (str < str_end)
13925ce721eSPeter Avalos 	{
14025ce721eSPeter Avalos 		ch = step_char(&str, +1, str_end);
14125ce721eSPeter Avalos 		if (IS_UPPER(ch))
14225ce721eSPeter Avalos 			return (1);
14325ce721eSPeter Avalos 	}
14425ce721eSPeter Avalos 	return (0);
14525ce721eSPeter Avalos }
14625ce721eSPeter Avalos 
14725ce721eSPeter Avalos /*
148a9adbba3SJan Lentfer  * Discard a saved pattern.
1491133e27eSPeter Avalos  */
clear_pattern(struct pattern_info * info)150*320d7c8aSAaron LI static void clear_pattern(struct pattern_info *info)
1511133e27eSPeter Avalos {
152a9adbba3SJan Lentfer 	if (info->text != NULL)
153a9adbba3SJan Lentfer 		free(info->text);
154a9adbba3SJan Lentfer 	info->text = NULL;
155e639dc31SJohn Marino #if !NO_REGEX
156a9adbba3SJan Lentfer 	uncompile_pattern(&info->compiled);
157e639dc31SJohn Marino #endif
1581133e27eSPeter Avalos }
1591133e27eSPeter Avalos 
1601133e27eSPeter Avalos /*
1610c7ad07eSAntonio Huete Jimenez  * Compile and save a search pattern.
1620c7ad07eSAntonio Huete Jimenez  */
set_pattern(struct pattern_info * info,char * pattern,int search_type,int show_error)163*320d7c8aSAaron LI static int set_pattern(struct pattern_info *info, char *pattern, int search_type, int show_error)
1640c7ad07eSAntonio Huete Jimenez {
1650c7ad07eSAntonio Huete Jimenez 	/*
1660c7ad07eSAntonio Huete Jimenez 	 * Ignore case if -I is set OR
1670c7ad07eSAntonio Huete Jimenez 	 * -i is set AND the pattern is all lowercase.
1680c7ad07eSAntonio Huete Jimenez 	 */
169*320d7c8aSAaron LI 	info->is_ucase_pattern = (pattern == NULL) ? FALSE : is_ucase(pattern);
170*320d7c8aSAaron LI 	is_caseless = (info->is_ucase_pattern && caseless != OPT_ONPLUS) ? 0 : caseless;
1710c7ad07eSAntonio Huete Jimenez #if !NO_REGEX
1720c7ad07eSAntonio Huete Jimenez 	if (pattern == NULL)
1730c7ad07eSAntonio Huete Jimenez 		SET_NULL_PATTERN(info->compiled);
1740c7ad07eSAntonio Huete Jimenez 	else if (compile_pattern(pattern, search_type, show_error, &info->compiled) < 0)
1750c7ad07eSAntonio Huete Jimenez 		return -1;
1760c7ad07eSAntonio Huete Jimenez #endif
1770c7ad07eSAntonio Huete Jimenez 	/* Pattern compiled successfully; save the text too. */
1780c7ad07eSAntonio Huete Jimenez 	if (info->text != NULL)
1790c7ad07eSAntonio Huete Jimenez 		free(info->text);
1800c7ad07eSAntonio Huete Jimenez 	info->text = NULL;
1810c7ad07eSAntonio Huete Jimenez 	if (pattern != NULL)
1820c7ad07eSAntonio Huete Jimenez 	{
1830c7ad07eSAntonio Huete Jimenez 		info->text = (char *) ecalloc(1, strlen(pattern)+1);
1840c7ad07eSAntonio Huete Jimenez 		strcpy(info->text, pattern);
1850c7ad07eSAntonio Huete Jimenez 	}
1860c7ad07eSAntonio Huete Jimenez 	info->search_type = search_type;
1870c7ad07eSAntonio Huete Jimenez 	return 0;
1880c7ad07eSAntonio Huete Jimenez }
1890c7ad07eSAntonio Huete Jimenez 
1900c7ad07eSAntonio Huete Jimenez /*
191a9adbba3SJan Lentfer  * Initialize saved pattern to nothing.
192a9adbba3SJan Lentfer  */
init_pattern(struct pattern_info * info)193*320d7c8aSAaron LI static void init_pattern(struct pattern_info *info)
194a9adbba3SJan Lentfer {
1950c7ad07eSAntonio Huete Jimenez 	SET_NULL_PATTERN(info->compiled);
196a9adbba3SJan Lentfer 	info->text = NULL;
197a9adbba3SJan Lentfer 	info->search_type = 0;
1980c7ad07eSAntonio Huete Jimenez 	info->next = NULL;
199a9adbba3SJan Lentfer }
200a9adbba3SJan Lentfer 
201a9adbba3SJan Lentfer /*
202a9adbba3SJan Lentfer  * Initialize search variables.
203a9adbba3SJan Lentfer  */
init_search(void)204*320d7c8aSAaron LI public void init_search(void)
205a9adbba3SJan Lentfer {
206a9adbba3SJan Lentfer 	init_pattern(&search_info);
207a9adbba3SJan Lentfer }
208a9adbba3SJan Lentfer 
209a9adbba3SJan Lentfer /*
210a9adbba3SJan Lentfer  * Determine which text conversions to perform before pattern matching.
2111133e27eSPeter Avalos  */
get_cvt_ops(int search_type)212*320d7c8aSAaron LI static int get_cvt_ops(int search_type)
2131133e27eSPeter Avalos {
2141133e27eSPeter Avalos 	int ops = 0;
2150c7ad07eSAntonio Huete Jimenez 
216*320d7c8aSAaron LI 	if (is_caseless && (!re_handles_caseless || (search_type & SRCH_NO_REGEX)))
2171133e27eSPeter Avalos 		ops |= CVT_TO_LC;
218*320d7c8aSAaron LI 	if (proc_backspace == OPT_ON || (bs_mode == BS_SPECIAL && proc_backspace == OPT_OFF))
2191133e27eSPeter Avalos 		ops |= CVT_BS;
220*320d7c8aSAaron LI 	if (proc_return == OPT_ON || (bs_mode != BS_CONTROL && proc_backspace == OPT_OFF))
2211133e27eSPeter Avalos 		ops |= CVT_CRLF;
2221133e27eSPeter Avalos 	if (ctldisp == OPT_ONPLUS)
2231133e27eSPeter Avalos 		ops |= CVT_ANSI;
2241133e27eSPeter Avalos 	return (ops);
2251133e27eSPeter Avalos }
2261133e27eSPeter Avalos 
2271133e27eSPeter Avalos /*
2281133e27eSPeter Avalos  * Is there a previous (remembered) search pattern?
2291133e27eSPeter Avalos  */
prev_pattern(struct pattern_info * info)230*320d7c8aSAaron LI static int prev_pattern(struct pattern_info *info)
2311133e27eSPeter Avalos {
232e639dc31SJohn Marino #if !NO_REGEX
233e639dc31SJohn Marino 	if ((info->search_type & SRCH_NO_REGEX) == 0)
234a9adbba3SJan Lentfer 		return (!is_null_pattern(info->compiled));
235e639dc31SJohn Marino #endif
236e639dc31SJohn Marino 	return (info->text != NULL);
2371133e27eSPeter Avalos }
2381133e27eSPeter Avalos 
2391133e27eSPeter Avalos #if HILITE_SEARCH
2401133e27eSPeter Avalos /*
2411133e27eSPeter Avalos  * Repaint the hilites currently displayed on the screen.
2421133e27eSPeter Avalos  * Repaint each line which contains highlighted text.
2431133e27eSPeter Avalos  * If on==0, force all hilites off.
2441133e27eSPeter Avalos  */
repaint_hilite(int on)245*320d7c8aSAaron LI public void repaint_hilite(int on)
2461133e27eSPeter Avalos {
24702d62a0fSDaniel Fojt 	int sindex;
2481133e27eSPeter Avalos 	POSITION pos;
2491133e27eSPeter Avalos 	int save_hide_hilite;
2501133e27eSPeter Avalos 
2511133e27eSPeter Avalos 	if (squished)
2521133e27eSPeter Avalos 		repaint();
2531133e27eSPeter Avalos 
2541133e27eSPeter Avalos 	save_hide_hilite = hide_hilite;
2551133e27eSPeter Avalos 	if (!on)
2561133e27eSPeter Avalos 	{
2571133e27eSPeter Avalos 		if (hide_hilite)
2581133e27eSPeter Avalos 			return;
2591133e27eSPeter Avalos 		hide_hilite = 1;
2601133e27eSPeter Avalos 	}
2611133e27eSPeter Avalos 
2621133e27eSPeter Avalos 	if (!can_goto_line)
2631133e27eSPeter Avalos 	{
2641133e27eSPeter Avalos 		repaint();
2651133e27eSPeter Avalos 		hide_hilite = save_hide_hilite;
2661133e27eSPeter Avalos 		return;
2671133e27eSPeter Avalos 	}
2681133e27eSPeter Avalos 
26902d62a0fSDaniel Fojt 	for (sindex = TOP;  sindex < TOP + sc_height-1;  sindex++)
2701133e27eSPeter Avalos 	{
27102d62a0fSDaniel Fojt 		pos = position(sindex);
2721133e27eSPeter Avalos 		if (pos == NULL_POSITION)
2731133e27eSPeter Avalos 			continue;
2741133e27eSPeter Avalos 		(void) forw_line(pos);
27502d62a0fSDaniel Fojt 		goto_line(sindex);
276*320d7c8aSAaron LI 		clear_eol();
2771133e27eSPeter Avalos 		put_line();
2781133e27eSPeter Avalos 	}
2790c7ad07eSAntonio Huete Jimenez 	overlay_header();
28025ce721eSPeter Avalos 	lower_left();
2811133e27eSPeter Avalos 	hide_hilite = save_hide_hilite;
2821133e27eSPeter Avalos }
2830c7ad07eSAntonio Huete Jimenez #endif
2841133e27eSPeter Avalos 
2851133e27eSPeter Avalos /*
2861133e27eSPeter Avalos  * Clear the attn hilite.
2871133e27eSPeter Avalos  */
clear_attn(void)288*320d7c8aSAaron LI public void clear_attn(void)
2891133e27eSPeter Avalos {
2900c7ad07eSAntonio Huete Jimenez #if HILITE_SEARCH
29102d62a0fSDaniel Fojt 	int sindex;
2921133e27eSPeter Avalos 	POSITION old_start_attnpos;
2931133e27eSPeter Avalos 	POSITION old_end_attnpos;
2941133e27eSPeter Avalos 	POSITION pos;
2951133e27eSPeter Avalos 	POSITION epos;
2961133e27eSPeter Avalos 	int moved = 0;
2971133e27eSPeter Avalos 
2981133e27eSPeter Avalos 	if (start_attnpos == NULL_POSITION)
2991133e27eSPeter Avalos 		return;
3001133e27eSPeter Avalos 	old_start_attnpos = start_attnpos;
3011133e27eSPeter Avalos 	old_end_attnpos = end_attnpos;
3021133e27eSPeter Avalos 	start_attnpos = end_attnpos = NULL_POSITION;
3031133e27eSPeter Avalos 
3041133e27eSPeter Avalos 	if (!can_goto_line)
3051133e27eSPeter Avalos 	{
3061133e27eSPeter Avalos 		repaint();
3071133e27eSPeter Avalos 		return;
3081133e27eSPeter Avalos 	}
3091133e27eSPeter Avalos 	if (squished)
3101133e27eSPeter Avalos 		repaint();
3111133e27eSPeter Avalos 
31202d62a0fSDaniel Fojt 	for (sindex = TOP;  sindex < TOP + sc_height-1;  sindex++)
3131133e27eSPeter Avalos 	{
31402d62a0fSDaniel Fojt 		pos = position(sindex);
3151133e27eSPeter Avalos 		if (pos == NULL_POSITION)
3161133e27eSPeter Avalos 			continue;
31702d62a0fSDaniel Fojt 		epos = position(sindex+1);
31802d62a0fSDaniel Fojt 		if (pos <= old_end_attnpos &&
3191133e27eSPeter Avalos 		     (epos == NULL_POSITION || epos > old_start_attnpos))
3201133e27eSPeter Avalos 		{
3211133e27eSPeter Avalos 			(void) forw_line(pos);
32202d62a0fSDaniel Fojt 			goto_line(sindex);
323*320d7c8aSAaron LI 			clear_eol();
3241133e27eSPeter Avalos 			put_line();
3251133e27eSPeter Avalos 			moved = 1;
3261133e27eSPeter Avalos 		}
3271133e27eSPeter Avalos 	}
3280c7ad07eSAntonio Huete Jimenez 	if (overlay_header())
3290c7ad07eSAntonio Huete Jimenez 		moved = 1;
3301133e27eSPeter Avalos 	if (moved)
3311133e27eSPeter Avalos 		lower_left();
3321133e27eSPeter Avalos #endif
3330c7ad07eSAntonio Huete Jimenez }
3341133e27eSPeter Avalos 
3351133e27eSPeter Avalos /*
3360c7ad07eSAntonio Huete Jimenez  * Toggle or clear search string highlighting.
3371133e27eSPeter Avalos  */
undo_search(int clear)338*320d7c8aSAaron LI public void undo_search(int clear)
3391133e27eSPeter Avalos {
3400c7ad07eSAntonio Huete Jimenez 	clear_pattern(&search_info);
3410c7ad07eSAntonio Huete Jimenez #if HILITE_SEARCH
3420c7ad07eSAntonio Huete Jimenez 	if (clear)
3430c7ad07eSAntonio Huete Jimenez 	{
3440c7ad07eSAntonio Huete Jimenez 		clr_hilite();
3450c7ad07eSAntonio Huete Jimenez 	} else
3461133e27eSPeter Avalos 	{
34702d62a0fSDaniel Fojt 		if (hilite_anchor.first == NULL)
34802d62a0fSDaniel Fojt 		{
3491133e27eSPeter Avalos 			error("No previous regular expression", NULL_PARG);
3501133e27eSPeter Avalos 			return;
3511133e27eSPeter Avalos 		}
3521133e27eSPeter Avalos 		hide_hilite = !hide_hilite;
3530c7ad07eSAntonio Huete Jimenez 	}
3541133e27eSPeter Avalos 	repaint_hilite(1);
3551133e27eSPeter Avalos #endif
3561133e27eSPeter Avalos }
3571133e27eSPeter Avalos 
3581133e27eSPeter Avalos #if HILITE_SEARCH
3591133e27eSPeter Avalos /*
3601133e27eSPeter Avalos  * Clear the hilite list.
3611133e27eSPeter Avalos  */
clr_hlist(struct hilite_tree * anchor)362*320d7c8aSAaron LI public void clr_hlist(struct hilite_tree *anchor)
3631133e27eSPeter Avalos {
364fa0be7c5SJohn Marino 	struct hilite_storage *hls;
365fa0be7c5SJohn Marino 	struct hilite_storage *nexthls;
3661133e27eSPeter Avalos 
367fa0be7c5SJohn Marino 	for (hls = anchor->first;  hls != NULL;  hls = nexthls)
3681133e27eSPeter Avalos 	{
369fa0be7c5SJohn Marino 		nexthls = hls->next;
3709b760066SJohn Marino 		free((void*)hls->nodes);
371fa0be7c5SJohn Marino 		free((void*)hls);
3721133e27eSPeter Avalos 	}
373fa0be7c5SJohn Marino 	anchor->first = NULL;
374fa0be7c5SJohn Marino 	anchor->current = NULL;
375fa0be7c5SJohn Marino 	anchor->root = NULL;
376fa0be7c5SJohn Marino 
377fa0be7c5SJohn Marino 	anchor->lookaside = NULL;
378fa0be7c5SJohn Marino 
3791133e27eSPeter Avalos 	prep_startpos = prep_endpos = NULL_POSITION;
3801133e27eSPeter Avalos }
3811133e27eSPeter Avalos 
clr_hilite(void)382*320d7c8aSAaron LI public void clr_hilite(void)
3838be36e5bSPeter Avalos {
3848be36e5bSPeter Avalos 	clr_hlist(&hilite_anchor);
3858be36e5bSPeter Avalos }
3868be36e5bSPeter Avalos 
clr_filter(void)387*320d7c8aSAaron LI public void clr_filter(void)
3888be36e5bSPeter Avalos {
3898be36e5bSPeter Avalos 	clr_hlist(&filter_anchor);
3908be36e5bSPeter Avalos }
3918be36e5bSPeter Avalos 
392fa0be7c5SJohn Marino /*
393fa0be7c5SJohn Marino  * Find the node covering pos, or the node after it if no node covers it,
394fa0be7c5SJohn Marino  * or return NULL if pos is after the last range. Remember the found node,
395fa0be7c5SJohn Marino  * to speed up subsequent searches for the same or similar positions (if
396fa0be7c5SJohn Marino  * we return NULL, remember the last node.)
397fa0be7c5SJohn Marino  */
hlist_find(struct hilite_tree * anchor,POSITION pos)398*320d7c8aSAaron LI static struct hilite_node* hlist_find(struct hilite_tree *anchor, POSITION pos)
399fa0be7c5SJohn Marino {
400fa0be7c5SJohn Marino 	struct hilite_node *n, *m;
401fa0be7c5SJohn Marino 
402fa0be7c5SJohn Marino 	if (anchor->lookaside)
403fa0be7c5SJohn Marino 	{
404fa0be7c5SJohn Marino 		int steps = 0;
405fa0be7c5SJohn Marino 		int hit = 0;
406fa0be7c5SJohn Marino 
407fa0be7c5SJohn Marino 		n = anchor->lookaside;
408fa0be7c5SJohn Marino 
409fa0be7c5SJohn Marino 		for (;;)
410fa0be7c5SJohn Marino 		{
411fa0be7c5SJohn Marino 			if (pos < n->r.hl_endpos)
412fa0be7c5SJohn Marino 			{
413fa0be7c5SJohn Marino 				if (n->prev == NULL || pos >= n->prev->r.hl_endpos)
414fa0be7c5SJohn Marino 				{
415fa0be7c5SJohn Marino 					hit = 1;
416fa0be7c5SJohn Marino 					break;
417fa0be7c5SJohn Marino 				}
418fa0be7c5SJohn Marino 			} else if (n->next == NULL)
419fa0be7c5SJohn Marino 			{
420fa0be7c5SJohn Marino 				n = NULL;
421fa0be7c5SJohn Marino 				hit = 1;
422fa0be7c5SJohn Marino 				break;
423fa0be7c5SJohn Marino 			}
424fa0be7c5SJohn Marino 
425fa0be7c5SJohn Marino 			/*
426fa0be7c5SJohn Marino 			 * If we don't find the right node within a small
427fa0be7c5SJohn Marino 			 * distance, don't keep doing a linear search!
428fa0be7c5SJohn Marino 			 */
429fa0be7c5SJohn Marino 			if (steps >= HILITE_LOOKASIDE_STEPS)
430fa0be7c5SJohn Marino 				break;
431fa0be7c5SJohn Marino 			steps++;
432fa0be7c5SJohn Marino 
433fa0be7c5SJohn Marino 			if (pos < n->r.hl_endpos)
434fa0be7c5SJohn Marino 				anchor->lookaside = n = n->prev;
435fa0be7c5SJohn Marino 			else
436fa0be7c5SJohn Marino 				anchor->lookaside = n = n->next;
437fa0be7c5SJohn Marino 		}
438fa0be7c5SJohn Marino 
439fa0be7c5SJohn Marino 		if (hit)
440fa0be7c5SJohn Marino 			return n;
441fa0be7c5SJohn Marino 	}
442fa0be7c5SJohn Marino 
443fa0be7c5SJohn Marino 	n = anchor->root;
444fa0be7c5SJohn Marino 	m = NULL;
445fa0be7c5SJohn Marino 
446fa0be7c5SJohn Marino 	while (n != NULL)
447fa0be7c5SJohn Marino 	{
448fa0be7c5SJohn Marino 		if (pos < n->r.hl_startpos)
449fa0be7c5SJohn Marino 		{
450fa0be7c5SJohn Marino 			if (n->left != NULL)
451fa0be7c5SJohn Marino 			{
452fa0be7c5SJohn Marino 				m = n;
453fa0be7c5SJohn Marino 				n = n->left;
454fa0be7c5SJohn Marino 				continue;
455fa0be7c5SJohn Marino 			}
456fa0be7c5SJohn Marino 			break;
457fa0be7c5SJohn Marino 		}
458fa0be7c5SJohn Marino 		if (pos >= n->r.hl_endpos)
459fa0be7c5SJohn Marino 		{
460fa0be7c5SJohn Marino 			if (n->right != NULL)
461fa0be7c5SJohn Marino 			{
462fa0be7c5SJohn Marino 				n = n->right;
463fa0be7c5SJohn Marino 				continue;
464fa0be7c5SJohn Marino 			}
465fa0be7c5SJohn Marino 			if (m != NULL)
466fa0be7c5SJohn Marino 			{
467fa0be7c5SJohn Marino 				n = m;
468fa0be7c5SJohn Marino 			} else
469fa0be7c5SJohn Marino 			{
470fa0be7c5SJohn Marino 				m = n;
471fa0be7c5SJohn Marino 				n = NULL;
472fa0be7c5SJohn Marino 			}
473fa0be7c5SJohn Marino 		}
474fa0be7c5SJohn Marino 		break;
475fa0be7c5SJohn Marino 	}
476fa0be7c5SJohn Marino 
477fa0be7c5SJohn Marino 	if (n != NULL)
478fa0be7c5SJohn Marino 		anchor->lookaside = n;
479fa0be7c5SJohn Marino 	else if (m != NULL)
480fa0be7c5SJohn Marino 		anchor->lookaside = m;
481fa0be7c5SJohn Marino 
482fa0be7c5SJohn Marino 	return n;
483fa0be7c5SJohn Marino }
484fa0be7c5SJohn Marino 
4851133e27eSPeter Avalos /*
4861133e27eSPeter Avalos  * Should any characters in a specified range be highlighted?
4871133e27eSPeter Avalos  */
hilited_range_attr(POSITION pos,POSITION epos)488*320d7c8aSAaron LI static int hilited_range_attr(POSITION pos, POSITION epos)
4891133e27eSPeter Avalos {
490fa0be7c5SJohn Marino 	struct hilite_node *n = hlist_find(&hilite_anchor, pos);
491*320d7c8aSAaron LI 	if (n == NULL)
492*320d7c8aSAaron LI 		return 0;
493*320d7c8aSAaron LI 	if (epos != NULL_POSITION && epos <= n->r.hl_startpos)
494*320d7c8aSAaron LI 		return 0;
495*320d7c8aSAaron LI 	return n->r.hl_attr;
4961133e27eSPeter Avalos }
4971133e27eSPeter Avalos 
4981133e27eSPeter Avalos /*
4998be36e5bSPeter Avalos  * Is a line "filtered" -- that is, should it be hidden?
5008be36e5bSPeter Avalos  */
is_filtered(POSITION pos)501*320d7c8aSAaron LI public int is_filtered(POSITION pos)
5028be36e5bSPeter Avalos {
503fa0be7c5SJohn Marino 	struct hilite_node *n;
5048be36e5bSPeter Avalos 
5058be36e5bSPeter Avalos 	if (ch_getflags() & CH_HELPFILE)
5068be36e5bSPeter Avalos 		return (0);
5078be36e5bSPeter Avalos 
508fa0be7c5SJohn Marino 	n = hlist_find(&filter_anchor, pos);
509fa0be7c5SJohn Marino 	return (n != NULL && pos >= n->r.hl_startpos);
510fa0be7c5SJohn Marino }
511fa0be7c5SJohn Marino 
5128be36e5bSPeter Avalos /*
513fa0be7c5SJohn Marino  * If pos is hidden, return the next position which isn't, otherwise
514fa0be7c5SJohn Marino  * just return pos.
5158be36e5bSPeter Avalos  */
next_unfiltered(POSITION pos)516*320d7c8aSAaron LI public POSITION next_unfiltered(POSITION pos)
5178be36e5bSPeter Avalos {
518fa0be7c5SJohn Marino 	struct hilite_node *n;
519fa0be7c5SJohn Marino 
520fa0be7c5SJohn Marino 	if (ch_getflags() & CH_HELPFILE)
521fa0be7c5SJohn Marino 		return (pos);
522fa0be7c5SJohn Marino 
523fa0be7c5SJohn Marino 	n = hlist_find(&filter_anchor, pos);
524fa0be7c5SJohn Marino 	while (n != NULL && pos >= n->r.hl_startpos)
525fa0be7c5SJohn Marino 	{
526fa0be7c5SJohn Marino 		pos = n->r.hl_endpos;
527fa0be7c5SJohn Marino 		n = n->next;
5288be36e5bSPeter Avalos 	}
529fa0be7c5SJohn Marino 	return (pos);
5308be36e5bSPeter Avalos }
5318be36e5bSPeter Avalos 
5328be36e5bSPeter Avalos /*
533fa0be7c5SJohn Marino  * If pos is hidden, return the previous position which isn't or 0 if
534fa0be7c5SJohn Marino  * we're filtered right to the beginning, otherwise just return pos.
535fa0be7c5SJohn Marino  */
prev_unfiltered(POSITION pos)536*320d7c8aSAaron LI public POSITION prev_unfiltered(POSITION pos)
537fa0be7c5SJohn Marino {
538fa0be7c5SJohn Marino 	struct hilite_node *n;
539fa0be7c5SJohn Marino 
540fa0be7c5SJohn Marino 	if (ch_getflags() & CH_HELPFILE)
541fa0be7c5SJohn Marino 		return (pos);
542fa0be7c5SJohn Marino 
543fa0be7c5SJohn Marino 	n = hlist_find(&filter_anchor, pos);
544fa0be7c5SJohn Marino 	while (n != NULL && pos >= n->r.hl_startpos)
545fa0be7c5SJohn Marino 	{
546fa0be7c5SJohn Marino 		pos = n->r.hl_startpos;
547fa0be7c5SJohn Marino 		if (pos == 0)
548fa0be7c5SJohn Marino 			break;
549fa0be7c5SJohn Marino 		pos--;
550fa0be7c5SJohn Marino 		n = n->prev;
551fa0be7c5SJohn Marino 	}
552fa0be7c5SJohn Marino 	return (pos);
553fa0be7c5SJohn Marino }
554fa0be7c5SJohn Marino 
555fa0be7c5SJohn Marino 
556fa0be7c5SJohn Marino /*
5571133e27eSPeter Avalos  * Should any characters in a specified range be highlighted?
5581133e27eSPeter Avalos  * If nohide is nonzero, don't consider hide_hilite.
5591133e27eSPeter Avalos  */
is_hilited_attr(POSITION pos,POSITION epos,int nohide,int * p_matches)560*320d7c8aSAaron LI public int is_hilited_attr(POSITION pos, POSITION epos, int nohide, int *p_matches)
5611133e27eSPeter Avalos {
562*320d7c8aSAaron LI 	int attr;
5631133e27eSPeter Avalos 
5641133e27eSPeter Avalos 	if (p_matches != NULL)
5651133e27eSPeter Avalos 		*p_matches = 0;
5661133e27eSPeter Avalos 
5671133e27eSPeter Avalos 	if (!status_col &&
5681133e27eSPeter Avalos 	    start_attnpos != NULL_POSITION &&
5690c7ad07eSAntonio Huete Jimenez 	    pos <= end_attnpos &&
5700c7ad07eSAntonio Huete Jimenez 	     (epos == NULL_POSITION || epos >= start_attnpos))
5711133e27eSPeter Avalos 		/*
5721133e27eSPeter Avalos 		 * The attn line overlaps this range.
5731133e27eSPeter Avalos 		 */
5740c7ad07eSAntonio Huete Jimenez 		return (AT_HILITE|AT_COLOR_ATTN);
5751133e27eSPeter Avalos 
576*320d7c8aSAaron LI 	attr = hilited_range_attr(pos, epos);
577*320d7c8aSAaron LI 	if (attr == 0)
5781133e27eSPeter Avalos 		return (0);
5791133e27eSPeter Avalos 
58002d62a0fSDaniel Fojt 	if (p_matches == NULL)
58102d62a0fSDaniel Fojt 		/*
58202d62a0fSDaniel Fojt 		 * Kinda kludgy way to recognize that caller is checking for
58302d62a0fSDaniel Fojt 		 * hilite in status column. In this case we want to return
58402d62a0fSDaniel Fojt 		 * hilite status even if hiliting is disabled or hidden.
58502d62a0fSDaniel Fojt 		 */
586*320d7c8aSAaron LI 		return (attr);
58702d62a0fSDaniel Fojt 
5881133e27eSPeter Avalos 	/*
5891133e27eSPeter Avalos 	 * Report matches, even if we're hiding highlights.
5901133e27eSPeter Avalos 	 */
5911133e27eSPeter Avalos 	*p_matches = 1;
5921133e27eSPeter Avalos 
5931133e27eSPeter Avalos 	if (hilite_search == 0)
5941133e27eSPeter Avalos 		/*
5951133e27eSPeter Avalos 		 * Not doing highlighting.
5961133e27eSPeter Avalos 		 */
5971133e27eSPeter Avalos 		return (0);
5981133e27eSPeter Avalos 
5991133e27eSPeter Avalos 	if (!nohide && hide_hilite)
6001133e27eSPeter Avalos 		/*
6011133e27eSPeter Avalos 		 * Highlighting is hidden.
6021133e27eSPeter Avalos 		 */
6031133e27eSPeter Avalos 		return (0);
6041133e27eSPeter Avalos 
605*320d7c8aSAaron LI 	return (attr);
6061133e27eSPeter Avalos }
6071133e27eSPeter Avalos 
6081133e27eSPeter Avalos /*
609fa0be7c5SJohn Marino  * Tree node storage: get the current block of nodes if it has spare
610fa0be7c5SJohn Marino  * capacity, or create a new one if not.
611fa0be7c5SJohn Marino  */
hlist_getstorage(struct hilite_tree * anchor)612*320d7c8aSAaron LI static struct hilite_storage * hlist_getstorage(struct hilite_tree *anchor)
613fa0be7c5SJohn Marino {
614fa0be7c5SJohn Marino 	int capacity = 1;
615fa0be7c5SJohn Marino 	struct hilite_storage *s;
616fa0be7c5SJohn Marino 
617fa0be7c5SJohn Marino 	if (anchor->current)
618fa0be7c5SJohn Marino 	{
619fa0be7c5SJohn Marino 		if (anchor->current->used < anchor->current->capacity)
620fa0be7c5SJohn Marino 			return anchor->current;
621fa0be7c5SJohn Marino 		capacity = anchor->current->capacity * 2;
622fa0be7c5SJohn Marino 	}
6239b760066SJohn Marino 
6249b760066SJohn Marino 	s = (struct hilite_storage *) ecalloc(1, sizeof(struct hilite_storage));
6259b760066SJohn Marino 	s->nodes = (struct hilite_node *) ecalloc(capacity, sizeof(struct hilite_node));
626fa0be7c5SJohn Marino 	s->capacity = capacity;
627fa0be7c5SJohn Marino 	s->used = 0;
628fa0be7c5SJohn Marino 	s->next = NULL;
629fa0be7c5SJohn Marino 	if (anchor->current)
630fa0be7c5SJohn Marino 		anchor->current->next = s;
631fa0be7c5SJohn Marino 	else
632fa0be7c5SJohn Marino 		anchor->first = s;
633fa0be7c5SJohn Marino 	anchor->current = s;
634fa0be7c5SJohn Marino 	return s;
635fa0be7c5SJohn Marino }
636fa0be7c5SJohn Marino 
637fa0be7c5SJohn Marino /*
638fa0be7c5SJohn Marino  * Tree node storage: retrieve a new empty node to be inserted into the
639fa0be7c5SJohn Marino  * tree.
640fa0be7c5SJohn Marino  */
hlist_getnode(struct hilite_tree * anchor)641*320d7c8aSAaron LI static struct hilite_node * hlist_getnode(struct hilite_tree *anchor)
642fa0be7c5SJohn Marino {
643fa0be7c5SJohn Marino 	struct hilite_storage *s = hlist_getstorage(anchor);
6449b760066SJohn Marino 	return &s->nodes[s->used++];
645fa0be7c5SJohn Marino }
646fa0be7c5SJohn Marino 
647fa0be7c5SJohn Marino /*
648fa0be7c5SJohn Marino  * Rotate the tree left around a pivot node.
649fa0be7c5SJohn Marino  */
hlist_rotate_left(struct hilite_tree * anchor,struct hilite_node * n)650*320d7c8aSAaron LI static void hlist_rotate_left(struct hilite_tree *anchor, struct hilite_node *n)
651fa0be7c5SJohn Marino {
652fa0be7c5SJohn Marino 	struct hilite_node *np = n->parent;
653fa0be7c5SJohn Marino 	struct hilite_node *nr = n->right;
654fa0be7c5SJohn Marino 	struct hilite_node *nrl = n->right->left;
655fa0be7c5SJohn Marino 
656fa0be7c5SJohn Marino 	if (np != NULL)
657fa0be7c5SJohn Marino 	{
658fa0be7c5SJohn Marino 		if (n == np->left)
659fa0be7c5SJohn Marino 			np->left = nr;
660fa0be7c5SJohn Marino 		else
661fa0be7c5SJohn Marino 			np->right = nr;
662fa0be7c5SJohn Marino 	} else
663fa0be7c5SJohn Marino 	{
664fa0be7c5SJohn Marino 		anchor->root = nr;
665fa0be7c5SJohn Marino 	}
666fa0be7c5SJohn Marino 	nr->left = n;
667fa0be7c5SJohn Marino 	n->right = nrl;
668fa0be7c5SJohn Marino 
669fa0be7c5SJohn Marino 	nr->parent = np;
670fa0be7c5SJohn Marino 	n->parent = nr;
671fa0be7c5SJohn Marino 	if (nrl != NULL)
672fa0be7c5SJohn Marino 		nrl->parent = n;
673fa0be7c5SJohn Marino }
674fa0be7c5SJohn Marino 
675fa0be7c5SJohn Marino /*
676fa0be7c5SJohn Marino  * Rotate the tree right around a pivot node.
677fa0be7c5SJohn Marino  */
hlist_rotate_right(struct hilite_tree * anchor,struct hilite_node * n)678*320d7c8aSAaron LI static void hlist_rotate_right(struct hilite_tree *anchor, struct hilite_node *n)
679fa0be7c5SJohn Marino {
680fa0be7c5SJohn Marino 	struct hilite_node *np = n->parent;
681fa0be7c5SJohn Marino 	struct hilite_node *nl = n->left;
682fa0be7c5SJohn Marino 	struct hilite_node *nlr = n->left->right;
683fa0be7c5SJohn Marino 
684fa0be7c5SJohn Marino 	if (np != NULL)
685fa0be7c5SJohn Marino 	{
686fa0be7c5SJohn Marino 		if (n == np->right)
687fa0be7c5SJohn Marino 			np->right = nl;
688fa0be7c5SJohn Marino 		else
689fa0be7c5SJohn Marino 			np->left = nl;
690fa0be7c5SJohn Marino 	} else
691fa0be7c5SJohn Marino 	{
692fa0be7c5SJohn Marino 		anchor->root = nl;
693fa0be7c5SJohn Marino 	}
694fa0be7c5SJohn Marino 	nl->right = n;
695fa0be7c5SJohn Marino 	n->left = nlr;
696fa0be7c5SJohn Marino 
697fa0be7c5SJohn Marino 	nl->parent = np;
698fa0be7c5SJohn Marino 	n->parent = nl;
699fa0be7c5SJohn Marino 	if (nlr != NULL)
700fa0be7c5SJohn Marino 		nlr->parent = n;
701fa0be7c5SJohn Marino }
702fa0be7c5SJohn Marino 
703fa0be7c5SJohn Marino 
704fa0be7c5SJohn Marino /*
7051133e27eSPeter Avalos  * Add a new hilite to a hilite list.
7061133e27eSPeter Avalos  */
add_hilite(struct hilite_tree * anchor,struct hilite * hl)707*320d7c8aSAaron LI static void add_hilite(struct hilite_tree *anchor, struct hilite *hl)
7081133e27eSPeter Avalos {
709fa0be7c5SJohn Marino 	struct hilite_node *p, *n, *u;
710fa0be7c5SJohn Marino 
711fa0be7c5SJohn Marino 	/* Ignore empty ranges. */
712fa0be7c5SJohn Marino 	if (hl->hl_startpos >= hl->hl_endpos)
713fa0be7c5SJohn Marino 		return;
714fa0be7c5SJohn Marino 
715fa0be7c5SJohn Marino 	p = anchor->root;
716fa0be7c5SJohn Marino 
717fa0be7c5SJohn Marino 	/* Inserting the very first node is trivial. */
718fa0be7c5SJohn Marino 	if (p == NULL)
719fa0be7c5SJohn Marino 	{
720fa0be7c5SJohn Marino 		n = hlist_getnode(anchor);
721fa0be7c5SJohn Marino 		n->r = *hl;
722fa0be7c5SJohn Marino 		anchor->root = n;
723fa0be7c5SJohn Marino 		anchor->lookaside = n;
724fa0be7c5SJohn Marino 		return;
725fa0be7c5SJohn Marino 	}
7261133e27eSPeter Avalos 
7271133e27eSPeter Avalos 	/*
728fa0be7c5SJohn Marino 	 * Find our insertion point. If we come across any overlapping
729fa0be7c5SJohn Marino 	 * or adjoining existing ranges, shrink our range and discard
730fa0be7c5SJohn Marino 	 * if it become empty.
7311133e27eSPeter Avalos 	 */
732fa0be7c5SJohn Marino 	for (;;)
7331133e27eSPeter Avalos 	{
734fa0be7c5SJohn Marino 		if (hl->hl_startpos < p->r.hl_startpos)
735fa0be7c5SJohn Marino 		{
736*320d7c8aSAaron LI 			if (hl->hl_endpos > p->r.hl_startpos && hl->hl_attr == p->r.hl_attr)
737fa0be7c5SJohn Marino 				hl->hl_endpos = p->r.hl_startpos;
738fa0be7c5SJohn Marino 			if (p->left != NULL)
739fa0be7c5SJohn Marino 			{
740fa0be7c5SJohn Marino 				p = p->left;
741fa0be7c5SJohn Marino 				continue;
742fa0be7c5SJohn Marino 			}
743fa0be7c5SJohn Marino 			break;
744fa0be7c5SJohn Marino 		}
745*320d7c8aSAaron LI 		if (hl->hl_startpos < p->r.hl_endpos && hl->hl_attr == p->r.hl_attr) {
746fa0be7c5SJohn Marino 			hl->hl_startpos = p->r.hl_endpos;
747fa0be7c5SJohn Marino 			if (hl->hl_startpos >= hl->hl_endpos)
748fa0be7c5SJohn Marino 				return;
749fa0be7c5SJohn Marino 		}
750fa0be7c5SJohn Marino 		if (p->right != NULL)
751fa0be7c5SJohn Marino 		{
752fa0be7c5SJohn Marino 			p = p->right;
753fa0be7c5SJohn Marino 			continue;
754fa0be7c5SJohn Marino 		}
7551133e27eSPeter Avalos 		break;
7561133e27eSPeter Avalos 	}
7571133e27eSPeter Avalos 
7581133e27eSPeter Avalos 	/*
759fa0be7c5SJohn Marino 	 * Now we're at the right leaf, again check for contiguous ranges
760fa0be7c5SJohn Marino 	 * and extend the existing node if possible to avoid the
761fa0be7c5SJohn Marino 	 * insertion. Otherwise insert a new node at the leaf.
7621133e27eSPeter Avalos 	 */
763fa0be7c5SJohn Marino 	if (hl->hl_startpos < p->r.hl_startpos) {
764*320d7c8aSAaron LI 		if (hl->hl_attr == p->r.hl_attr)
765*320d7c8aSAaron LI 		{
766fa0be7c5SJohn Marino 			if (hl->hl_endpos == p->r.hl_startpos)
7671133e27eSPeter Avalos 			{
768fa0be7c5SJohn Marino 				p->r.hl_startpos = hl->hl_startpos;
7691133e27eSPeter Avalos 				return;
7701133e27eSPeter Avalos 			}
771fa0be7c5SJohn Marino 			if (p->prev != NULL && p->prev->r.hl_endpos == hl->hl_startpos)
772fa0be7c5SJohn Marino 			{
773fa0be7c5SJohn Marino 				p->prev->r.hl_endpos = hl->hl_endpos;
774fa0be7c5SJohn Marino 				return;
775fa0be7c5SJohn Marino 			}
776*320d7c8aSAaron LI 		}
777fa0be7c5SJohn Marino 		p->left = n = hlist_getnode(anchor);
778fa0be7c5SJohn Marino 		n->next = p;
779fa0be7c5SJohn Marino 		if (p->prev != NULL)
780fa0be7c5SJohn Marino 		{
781fa0be7c5SJohn Marino 			n->prev = p->prev;
782fa0be7c5SJohn Marino 			p->prev->next = n;
783fa0be7c5SJohn Marino 		}
784fa0be7c5SJohn Marino 		p->prev = n;
785fa0be7c5SJohn Marino 	} else {
786*320d7c8aSAaron LI 		if (hl->hl_attr == p->r.hl_attr)
787*320d7c8aSAaron LI 		{
788fa0be7c5SJohn Marino 			if (p->r.hl_endpos == hl->hl_startpos)
789fa0be7c5SJohn Marino 			{
790fa0be7c5SJohn Marino 				p->r.hl_endpos = hl->hl_endpos;
791fa0be7c5SJohn Marino 				return;
792fa0be7c5SJohn Marino 			}
793fa0be7c5SJohn Marino 			if (p->next != NULL && hl->hl_endpos == p->next->r.hl_startpos) {
794fa0be7c5SJohn Marino 				p->next->r.hl_startpos = hl->hl_startpos;
795fa0be7c5SJohn Marino 				return;
796fa0be7c5SJohn Marino 			}
797*320d7c8aSAaron LI 		}
798fa0be7c5SJohn Marino 		p->right = n = hlist_getnode(anchor);
799fa0be7c5SJohn Marino 		n->prev = p;
800fa0be7c5SJohn Marino 		if (p->next != NULL)
801fa0be7c5SJohn Marino 		{
802fa0be7c5SJohn Marino 			n->next = p->next;
803fa0be7c5SJohn Marino 			p->next->prev = n;
804fa0be7c5SJohn Marino 		}
805fa0be7c5SJohn Marino 		p->next = n;
806fa0be7c5SJohn Marino 	}
807fa0be7c5SJohn Marino 	n->parent = p;
808fa0be7c5SJohn Marino 	n->red = 1;
809fa0be7c5SJohn Marino 	n->r = *hl;
810fa0be7c5SJohn Marino 
811fa0be7c5SJohn Marino 	/*
812fa0be7c5SJohn Marino 	 * The tree is in the correct order and covers the right ranges
813fa0be7c5SJohn Marino 	 * now, but may have become unbalanced. Rebalance it using the
814fa0be7c5SJohn Marino 	 * standard red-black tree constraints and operations.
815fa0be7c5SJohn Marino 	 */
816fa0be7c5SJohn Marino 	for (;;)
817fa0be7c5SJohn Marino 	{
818fa0be7c5SJohn Marino 		/* case 1 - current is root, root is always black */
819fa0be7c5SJohn Marino 		if (n->parent == NULL)
820fa0be7c5SJohn Marino 		{
821fa0be7c5SJohn Marino 			n->red = 0;
822fa0be7c5SJohn Marino 			break;
823fa0be7c5SJohn Marino 		}
824fa0be7c5SJohn Marino 
825fa0be7c5SJohn Marino 		/* case 2 - parent is black, we can always be red */
826fa0be7c5SJohn Marino 		if (!n->parent->red)
827fa0be7c5SJohn Marino 			break;
828fa0be7c5SJohn Marino 
829fa0be7c5SJohn Marino 		/*
830fa0be7c5SJohn Marino 		 * constraint: because the root must be black, if our
831fa0be7c5SJohn Marino 		 * parent is red it cannot be the root therefore we must
832fa0be7c5SJohn Marino 		 * have a grandparent
833fa0be7c5SJohn Marino 		 */
834fa0be7c5SJohn Marino 
835fa0be7c5SJohn Marino 		/*
836fa0be7c5SJohn Marino 		 * case 3 - parent and uncle are red, repaint them black,
837fa0be7c5SJohn Marino 		 * the grandparent red, and start again at the grandparent.
838fa0be7c5SJohn Marino 		 */
839fa0be7c5SJohn Marino 		u = n->parent->parent->left;
840fa0be7c5SJohn Marino 		if (n->parent == u)
841fa0be7c5SJohn Marino 			u = n->parent->parent->right;
842fa0be7c5SJohn Marino 		if (u != NULL && u->red)
843fa0be7c5SJohn Marino 		{
844fa0be7c5SJohn Marino 			n->parent->red = 0;
845fa0be7c5SJohn Marino 			u->red = 0;
846fa0be7c5SJohn Marino 			n = n->parent->parent;
847fa0be7c5SJohn Marino 			n->red = 1;
848fa0be7c5SJohn Marino 			continue;
849fa0be7c5SJohn Marino 		}
850fa0be7c5SJohn Marino 
851fa0be7c5SJohn Marino 		/*
852fa0be7c5SJohn Marino 		 * case 4 - parent is red but uncle is black, parent and
853fa0be7c5SJohn Marino 		 * grandparent on opposite sides. We need to start
854fa0be7c5SJohn Marino 		 * changing the structure now. This and case 5 will shorten
855fa0be7c5SJohn Marino 		 * our branch and lengthen the sibling, between them
856fa0be7c5SJohn Marino 		 * restoring balance.
857fa0be7c5SJohn Marino 		 */
858fa0be7c5SJohn Marino 		if (n == n->parent->right &&
859fa0be7c5SJohn Marino 		    n->parent == n->parent->parent->left)
860fa0be7c5SJohn Marino 		{
861fa0be7c5SJohn Marino 			hlist_rotate_left(anchor, n->parent);
862fa0be7c5SJohn Marino 			n = n->left;
863fa0be7c5SJohn Marino 		} else if (n == n->parent->left &&
864fa0be7c5SJohn Marino 			   n->parent == n->parent->parent->right)
865fa0be7c5SJohn Marino 		{
866fa0be7c5SJohn Marino 			hlist_rotate_right(anchor, n->parent);
867fa0be7c5SJohn Marino 			n = n->right;
868fa0be7c5SJohn Marino 		}
869fa0be7c5SJohn Marino 
870fa0be7c5SJohn Marino 		/*
871fa0be7c5SJohn Marino 		 * case 5 - parent is red but uncle is black, parent and
872fa0be7c5SJohn Marino 		 * grandparent on same side
873fa0be7c5SJohn Marino 		 */
874fa0be7c5SJohn Marino 		n->parent->red = 0;
875fa0be7c5SJohn Marino 		n->parent->parent->red = 1;
876fa0be7c5SJohn Marino 		if (n == n->parent->left)
877fa0be7c5SJohn Marino 			hlist_rotate_right(anchor, n->parent->parent);
878fa0be7c5SJohn Marino 		else
879fa0be7c5SJohn Marino 			hlist_rotate_left(anchor, n->parent->parent);
880fa0be7c5SJohn Marino 		break;
881fa0be7c5SJohn Marino 	}
8821133e27eSPeter Avalos }
8831133e27eSPeter Avalos 
8841133e27eSPeter Avalos /*
8850c7ad07eSAntonio Huete Jimenez  * Highlight every character in a range of displayed characters.
886e639dc31SJohn Marino  */
create_hilites(POSITION linepos,char * line,char * sp,char * ep,int attr,int * chpos)887*320d7c8aSAaron LI static void create_hilites(POSITION linepos, char *line, char *sp, char *ep, int attr, int *chpos)
888e639dc31SJohn Marino {
889*320d7c8aSAaron LI 	int start_index = sp - line;
890*320d7c8aSAaron LI 	int end_index = ep - line;
891fa0be7c5SJohn Marino 	struct hilite hl;
892e639dc31SJohn Marino 	int i;
893e639dc31SJohn Marino 
894e639dc31SJohn Marino 	/* Start the first hilite. */
895fa0be7c5SJohn Marino 	hl.hl_startpos = linepos + chpos[start_index];
896*320d7c8aSAaron LI 	hl.hl_attr = attr;
897e639dc31SJohn Marino 
898e639dc31SJohn Marino 	/*
899e639dc31SJohn Marino 	 * Step through the displayed chars.
900e639dc31SJohn Marino 	 * If the source position (before cvt) of the char is one more
901e639dc31SJohn Marino 	 * than the source pos of the previous char (the usual case),
902e639dc31SJohn Marino 	 * just increase the size of the current hilite by one.
903e639dc31SJohn Marino 	 * Otherwise (there are backspaces or something involved),
904e639dc31SJohn Marino 	 * finish the current hilite and start a new one.
905e639dc31SJohn Marino 	 */
906e639dc31SJohn Marino 	for (i = start_index+1;  i <= end_index;  i++)
907e639dc31SJohn Marino 	{
908e639dc31SJohn Marino 		if (chpos[i] != chpos[i-1] + 1 || i == end_index)
909e639dc31SJohn Marino 		{
910fa0be7c5SJohn Marino 			hl.hl_endpos = linepos + chpos[i-1] + 1;
911fa0be7c5SJohn Marino 			add_hilite(&hilite_anchor, &hl);
912e639dc31SJohn Marino 			/* Start new hilite unless this is the last char. */
913e639dc31SJohn Marino 			if (i < end_index)
914e639dc31SJohn Marino 			{
915fa0be7c5SJohn Marino 				hl.hl_startpos = linepos + chpos[i];
916e639dc31SJohn Marino 			}
917e639dc31SJohn Marino 		}
918e639dc31SJohn Marino 	}
919e639dc31SJohn Marino }
920e639dc31SJohn Marino 
921e639dc31SJohn Marino /*
9221133e27eSPeter Avalos  * Make a hilite for each string in a physical line which matches
9231133e27eSPeter Avalos  * the current pattern.
9241133e27eSPeter Avalos  * sp,ep delimit the first match already found.
9251133e27eSPeter Avalos  */
hilite_line(POSITION linepos,char * line,int line_len,int * chpos,char ** sp,char ** ep,int nsp,int cvt_ops)926*320d7c8aSAaron LI static void hilite_line(POSITION linepos, char *line, int line_len, int *chpos, char **sp, char **ep, int nsp, int cvt_ops)
9271133e27eSPeter Avalos {
9281133e27eSPeter Avalos 	char *searchp;
9291133e27eSPeter Avalos 	char *line_end = line + line_len;
9301133e27eSPeter Avalos 
9311133e27eSPeter Avalos 	/*
932*320d7c8aSAaron LI 	 * sp[0] and ep[0] delimit the first match in the line.
9331133e27eSPeter Avalos 	 * Mark the corresponding file positions, then
9341133e27eSPeter Avalos 	 * look for further matches and mark them.
9351133e27eSPeter Avalos 	 * {{ This technique, of calling match_pattern on subsequent
9361133e27eSPeter Avalos 	 *    substrings of the line, may mark more than is correct
9371133e27eSPeter Avalos 	 *    if the pattern starts with "^".  This bug is fixed
9381133e27eSPeter Avalos 	 *    for those regex functions that accept a notbol parameter
9391133e27eSPeter Avalos 	 *    (currently POSIX, PCRE and V8-with-regexec2). }}
940*320d7c8aSAaron LI 	 * sp[i] and ep[i] for i>0 delimit subpattern matches.
941*320d7c8aSAaron LI 	 * Color each of them with its unique color.
9421133e27eSPeter Avalos 	 */
9431133e27eSPeter Avalos 	searchp = line;
9441133e27eSPeter Avalos 	do {
945*320d7c8aSAaron LI 		char *lep = sp[0];
946*320d7c8aSAaron LI 		int i;
947*320d7c8aSAaron LI 		if (sp[0] == NULL || ep[0] == NULL)
948*320d7c8aSAaron LI 			break;
949*320d7c8aSAaron LI 		for (i = 1;  i < nsp;  i++)
950*320d7c8aSAaron LI 		{
951*320d7c8aSAaron LI 			if (sp[i] == NULL || ep[i] == NULL)
952*320d7c8aSAaron LI 				break;
953*320d7c8aSAaron LI 			if (ep[i] > sp[i])
954*320d7c8aSAaron LI 			{
955*320d7c8aSAaron LI 				create_hilites(linepos, line, lep, sp[i],
956*320d7c8aSAaron LI 					AT_HILITE | AT_COLOR_SEARCH, chpos);
957*320d7c8aSAaron LI 				create_hilites(linepos, line, sp[i], ep[i],
958*320d7c8aSAaron LI 					AT_HILITE | AT_COLOR_SUBSEARCH(i), chpos);
959*320d7c8aSAaron LI 				lep = ep[i];
960*320d7c8aSAaron LI 			}
961*320d7c8aSAaron LI 		}
962*320d7c8aSAaron LI 		create_hilites(linepos, line, lep, ep[0],
963*320d7c8aSAaron LI 			AT_HILITE | AT_COLOR_SEARCH, chpos);
964*320d7c8aSAaron LI 
9651133e27eSPeter Avalos 		/*
9661133e27eSPeter Avalos 		 * If we matched more than zero characters,
9671133e27eSPeter Avalos 		 * move to the first char after the string we matched.
9681133e27eSPeter Avalos 		 * If we matched zero, just move to the next char.
9691133e27eSPeter Avalos 		 */
970*320d7c8aSAaron LI 		if (ep[0] > searchp)
971*320d7c8aSAaron LI 			searchp = ep[0];
9721133e27eSPeter Avalos 		else if (searchp != line_end)
9731133e27eSPeter Avalos 			searchp++;
9741133e27eSPeter Avalos 		else /* end of line */
9751133e27eSPeter Avalos 			break;
976e639dc31SJohn Marino 	} while (match_pattern(info_compiled(&search_info), search_info.text,
977*320d7c8aSAaron LI 			searchp, line_end - searchp, sp, ep, nsp, 1, search_info.search_type));
9781133e27eSPeter Avalos }
9791133e27eSPeter Avalos #endif
9801133e27eSPeter Avalos 
9811133e27eSPeter Avalos #if HILITE_SEARCH
9821133e27eSPeter Avalos /*
9831133e27eSPeter Avalos  * Find matching text which is currently on screen and highlight it.
9841133e27eSPeter Avalos  */
hilite_screen(void)985*320d7c8aSAaron LI static void hilite_screen(void)
9861133e27eSPeter Avalos {
9871133e27eSPeter Avalos 	struct scrpos scrpos;
9881133e27eSPeter Avalos 
98902d62a0fSDaniel Fojt 	get_scrpos(&scrpos, TOP);
9901133e27eSPeter Avalos 	if (scrpos.pos == NULL_POSITION)
9911133e27eSPeter Avalos 		return;
9921133e27eSPeter Avalos 	prep_hilite(scrpos.pos, position(BOTTOM_PLUS_ONE), -1);
9931133e27eSPeter Avalos 	repaint_hilite(1);
9941133e27eSPeter Avalos }
9951133e27eSPeter Avalos 
9961133e27eSPeter Avalos /*
9971133e27eSPeter Avalos  * Change highlighting parameters.
9981133e27eSPeter Avalos  */
chg_hilite(void)999*320d7c8aSAaron LI public void chg_hilite(void)
10001133e27eSPeter Avalos {
10011133e27eSPeter Avalos 	/*
10021133e27eSPeter Avalos 	 * Erase any highlights currently on screen.
10031133e27eSPeter Avalos 	 */
10041133e27eSPeter Avalos 	clr_hilite();
10051133e27eSPeter Avalos 	hide_hilite = 0;
10061133e27eSPeter Avalos 
10071133e27eSPeter Avalos 	if (hilite_search == OPT_ONPLUS)
10081133e27eSPeter Avalos 		/*
10091133e27eSPeter Avalos 		 * Display highlights.
10101133e27eSPeter Avalos 		 */
10111133e27eSPeter Avalos 		hilite_screen();
10121133e27eSPeter Avalos }
10131133e27eSPeter Avalos #endif
10141133e27eSPeter Avalos 
10151133e27eSPeter Avalos /*
10161133e27eSPeter Avalos  * Figure out where to start a search.
10171133e27eSPeter Avalos  */
search_pos(int search_type)1018*320d7c8aSAaron LI static POSITION search_pos(int search_type)
10191133e27eSPeter Avalos {
10201133e27eSPeter Avalos 	POSITION pos;
102102d62a0fSDaniel Fojt 	int sindex;
10221133e27eSPeter Avalos 
10231133e27eSPeter Avalos 	if (empty_screen())
10241133e27eSPeter Avalos 	{
10251133e27eSPeter Avalos 		/*
10261133e27eSPeter Avalos 		 * Start at the beginning (or end) of the file.
10271133e27eSPeter Avalos 		 * The empty_screen() case is mainly for
10281133e27eSPeter Avalos 		 * command line initiated searches;
10291133e27eSPeter Avalos 		 * for example, "+/xyz" on the command line.
10301133e27eSPeter Avalos 		 * Also for multi-file (SRCH_PAST_EOF) searches.
10311133e27eSPeter Avalos 		 */
10321133e27eSPeter Avalos 		if (search_type & SRCH_FORW)
10331133e27eSPeter Avalos 		{
103425ce721eSPeter Avalos 			pos = ch_zero();
10351133e27eSPeter Avalos 		} else
10361133e27eSPeter Avalos 		{
10371133e27eSPeter Avalos 			pos = ch_length();
10381133e27eSPeter Avalos 			if (pos == NULL_POSITION)
10391133e27eSPeter Avalos 			{
10401133e27eSPeter Avalos 				(void) ch_end_seek();
10411133e27eSPeter Avalos 				pos = ch_length();
10421133e27eSPeter Avalos 			}
10431133e27eSPeter Avalos 		}
104402d62a0fSDaniel Fojt 		sindex = 0;
104525ce721eSPeter Avalos 	} else
104625ce721eSPeter Avalos 	{
104725ce721eSPeter Avalos 		int add_one = 0;
104825ce721eSPeter Avalos 
104925ce721eSPeter Avalos 		if (how_search == OPT_ON)
10501133e27eSPeter Avalos 		{
10511133e27eSPeter Avalos 			/*
10521133e27eSPeter Avalos 			 * Search does not include current screen.
10531133e27eSPeter Avalos 			 */
10541133e27eSPeter Avalos 			if (search_type & SRCH_FORW)
105502d62a0fSDaniel Fojt 				sindex = sc_height-1; /* BOTTOM_PLUS_ONE */
10561133e27eSPeter Avalos 			else
105702d62a0fSDaniel Fojt 				sindex = 0; /* TOP */
105825ce721eSPeter Avalos 		} else if (how_search == OPT_ONPLUS && !(search_type & SRCH_AFTER_TARGET))
105925ce721eSPeter Avalos 		{
106025ce721eSPeter Avalos 			/*
106125ce721eSPeter Avalos 			 * Search includes all of displayed screen.
106225ce721eSPeter Avalos 			 */
106325ce721eSPeter Avalos 			if (search_type & SRCH_FORW)
106402d62a0fSDaniel Fojt 				sindex = 0; /* TOP */
106525ce721eSPeter Avalos 			else
106602d62a0fSDaniel Fojt 				sindex = sc_height-1; /* BOTTOM_PLUS_ONE */
10671133e27eSPeter Avalos 		} else
10681133e27eSPeter Avalos 		{
10691133e27eSPeter Avalos 			/*
107025ce721eSPeter Avalos 			 * Search includes the part of current screen beyond the jump target.
10711133e27eSPeter Avalos 			 * It starts at the jump target (if searching backwards),
10721133e27eSPeter Avalos 			 * or at the jump target plus one (if forwards).
10731133e27eSPeter Avalos 			 */
107402d62a0fSDaniel Fojt 			sindex = sindex_from_sline(jump_sline);
107525ce721eSPeter Avalos 			if (search_type & SRCH_FORW)
107625ce721eSPeter Avalos 				add_one = 1;
107725ce721eSPeter Avalos 		}
107802d62a0fSDaniel Fojt 		pos = position(sindex);
107925ce721eSPeter Avalos 		if (add_one)
108025ce721eSPeter Avalos 			pos = forw_raw_line(pos, (char **)NULL, (int *)NULL);
108125ce721eSPeter Avalos 	}
108225ce721eSPeter Avalos 
108325ce721eSPeter Avalos 	/*
108425ce721eSPeter Avalos 	 * If the line is empty, look around for a plausible starting place.
108525ce721eSPeter Avalos 	 */
10861133e27eSPeter Avalos 	if (search_type & SRCH_FORW)
10871133e27eSPeter Avalos 	{
10881133e27eSPeter Avalos 		while (pos == NULL_POSITION)
10891133e27eSPeter Avalos 		{
109002d62a0fSDaniel Fojt 			if (++sindex >= sc_height)
10911133e27eSPeter Avalos 				break;
109202d62a0fSDaniel Fojt 			pos = position(sindex);
10931133e27eSPeter Avalos 		}
10941133e27eSPeter Avalos 	} else
10951133e27eSPeter Avalos 	{
10961133e27eSPeter Avalos 		while (pos == NULL_POSITION)
10971133e27eSPeter Avalos 		{
109802d62a0fSDaniel Fojt 			if (--sindex < 0)
10991133e27eSPeter Avalos 				break;
110002d62a0fSDaniel Fojt 			pos = position(sindex);
11011133e27eSPeter Avalos 		}
11021133e27eSPeter Avalos 	}
11031133e27eSPeter Avalos 	return (pos);
11041133e27eSPeter Avalos }
11051133e27eSPeter Avalos 
11061133e27eSPeter Avalos /*
11070c7ad07eSAntonio Huete Jimenez  * Check to see if the line matches the filter pattern.
11080c7ad07eSAntonio Huete Jimenez  * If so, add an entry to the filter list.
11090c7ad07eSAntonio Huete Jimenez  */
11100c7ad07eSAntonio Huete Jimenez #if HILITE_SEARCH
matches_filters(POSITION pos,char * cline,int line_len,int * chpos,POSITION linepos,char ** sp,char ** ep,int nsp)1111*320d7c8aSAaron LI static int matches_filters(POSITION pos, char *cline, int line_len, int *chpos, POSITION linepos, char **sp, char **ep, int nsp)
11120c7ad07eSAntonio Huete Jimenez {
11130c7ad07eSAntonio Huete Jimenez 	struct pattern_info *filter;
11140c7ad07eSAntonio Huete Jimenez 
11150c7ad07eSAntonio Huete Jimenez 	for (filter = filter_infos; filter != NULL; filter = filter->next)
11160c7ad07eSAntonio Huete Jimenez 	{
11170c7ad07eSAntonio Huete Jimenez 		int line_filter = match_pattern(info_compiled(filter), filter->text,
1118*320d7c8aSAaron LI 			cline, line_len, sp, ep, nsp, 0, filter->search_type);
11190c7ad07eSAntonio Huete Jimenez 		if (line_filter)
11200c7ad07eSAntonio Huete Jimenez 		{
11210c7ad07eSAntonio Huete Jimenez 			struct hilite hl;
11220c7ad07eSAntonio Huete Jimenez 			hl.hl_startpos = linepos;
11230c7ad07eSAntonio Huete Jimenez 			hl.hl_endpos = pos;
11240c7ad07eSAntonio Huete Jimenez 			add_hilite(&filter_anchor, &hl);
11250c7ad07eSAntonio Huete Jimenez 			free(cline);
11260c7ad07eSAntonio Huete Jimenez 			free(chpos);
11270c7ad07eSAntonio Huete Jimenez 			return (1);
11280c7ad07eSAntonio Huete Jimenez 		}
11290c7ad07eSAntonio Huete Jimenez 	}
11300c7ad07eSAntonio Huete Jimenez 	return (0);
11310c7ad07eSAntonio Huete Jimenez }
11320c7ad07eSAntonio Huete Jimenez #endif
11330c7ad07eSAntonio Huete Jimenez 
11340c7ad07eSAntonio Huete Jimenez /*
11350c7ad07eSAntonio Huete Jimenez  * Get the position of the first char in the screen line which
11360c7ad07eSAntonio Huete Jimenez  * puts tpos on screen.
11370c7ad07eSAntonio Huete Jimenez  */
get_lastlinepos(POSITION pos,POSITION tpos,int sheight)1138*320d7c8aSAaron LI static POSITION get_lastlinepos(POSITION pos, POSITION tpos, int sheight)
11390c7ad07eSAntonio Huete Jimenez {
11400c7ad07eSAntonio Huete Jimenez 	int nlines;
11410c7ad07eSAntonio Huete Jimenez 
11420c7ad07eSAntonio Huete Jimenez 	for (nlines = 0;;  nlines++)
11430c7ad07eSAntonio Huete Jimenez 	{
11440c7ad07eSAntonio Huete Jimenez 		POSITION npos = forw_line(pos);
11450c7ad07eSAntonio Huete Jimenez 		if (npos > tpos)
11460c7ad07eSAntonio Huete Jimenez 		{
11470c7ad07eSAntonio Huete Jimenez 			if (nlines < sheight)
11480c7ad07eSAntonio Huete Jimenez 				return NULL_POSITION;
11490c7ad07eSAntonio Huete Jimenez 			return pos;
11500c7ad07eSAntonio Huete Jimenez 		}
11510c7ad07eSAntonio Huete Jimenez 		pos = npos;
11520c7ad07eSAntonio Huete Jimenez 	}
11530c7ad07eSAntonio Huete Jimenez }
11540c7ad07eSAntonio Huete Jimenez 
11550c7ad07eSAntonio Huete Jimenez /*
11560c7ad07eSAntonio Huete Jimenez  * Get the segment index of tpos in the line starting at pos.
11570c7ad07eSAntonio Huete Jimenez  * A segment is a string of printable chars that fills the screen width.
11580c7ad07eSAntonio Huete Jimenez  */
get_seg(POSITION pos,POSITION tpos)1159*320d7c8aSAaron LI static int get_seg(POSITION pos, POSITION tpos)
11600c7ad07eSAntonio Huete Jimenez {
11610c7ad07eSAntonio Huete Jimenez 	int seg;
11620c7ad07eSAntonio Huete Jimenez 
11630c7ad07eSAntonio Huete Jimenez 	for (seg = 0;;  seg++)
11640c7ad07eSAntonio Huete Jimenez 	{
11650c7ad07eSAntonio Huete Jimenez 		POSITION npos = forw_line_seg(pos, FALSE, FALSE, TRUE);
11660c7ad07eSAntonio Huete Jimenez 		if (npos > tpos)
11670c7ad07eSAntonio Huete Jimenez 			return seg;
11680c7ad07eSAntonio Huete Jimenez 		pos = npos;
11690c7ad07eSAntonio Huete Jimenez 	}
11700c7ad07eSAntonio Huete Jimenez }
11710c7ad07eSAntonio Huete Jimenez 
11720c7ad07eSAntonio Huete Jimenez /*
11731133e27eSPeter Avalos  * Search a subset of the file, specified by start/end position.
11741133e27eSPeter Avalos  */
search_range(POSITION pos,POSITION endpos,int search_type,int matches,int maxlines,POSITION * plinepos,POSITION * pendpos,POSITION * plastlinepos)1175*320d7c8aSAaron LI static int search_range(POSITION pos, POSITION endpos, int search_type, int matches, int maxlines, POSITION *plinepos, POSITION *pendpos, POSITION *plastlinepos)
11761133e27eSPeter Avalos {
11771133e27eSPeter Avalos 	char *line;
11781133e27eSPeter Avalos 	char *cline;
11791133e27eSPeter Avalos 	int line_len;
11801133e27eSPeter Avalos 	LINENUM linenum;
1181*320d7c8aSAaron LI 	#define NSP (NUM_SEARCH_COLORS+2)
1182*320d7c8aSAaron LI 	char *sp[NSP];
1183*320d7c8aSAaron LI 	char *ep[NSP];
11841133e27eSPeter Avalos 	int line_match;
11851133e27eSPeter Avalos 	int cvt_ops;
1186a9adbba3SJan Lentfer 	int cvt_len;
1187a9adbba3SJan Lentfer 	int *chpos;
11881133e27eSPeter Avalos 	POSITION linepos, oldpos;
1189*320d7c8aSAaron LI 	int skip_bytes = 0;
11900c7ad07eSAntonio Huete Jimenez 	int swidth = sc_width - line_pfx_width();
11910c7ad07eSAntonio Huete Jimenez 	int sheight = sc_height - sindex_from_sline(jump_sline);
11921133e27eSPeter Avalos 
11931133e27eSPeter Avalos 	linenum = find_linenum(pos);
1194*320d7c8aSAaron LI 	if (nosearch_headers && linenum <= header_lines)
1195*320d7c8aSAaron LI 	{
1196*320d7c8aSAaron LI 		linenum = header_lines + 1;
1197*320d7c8aSAaron LI 		pos = find_pos(linenum);
1198*320d7c8aSAaron LI 	}
1199*320d7c8aSAaron LI 	if (pos == NULL_POSITION)
1200*320d7c8aSAaron LI 		return (-1);
12011133e27eSPeter Avalos 	oldpos = pos;
12020c7ad07eSAntonio Huete Jimenez 	/* When the search wraps around, end at starting position. */
12030c7ad07eSAntonio Huete Jimenez 	if ((search_type & SRCH_WRAP) && endpos == NULL_POSITION)
12040c7ad07eSAntonio Huete Jimenez 		endpos = pos;
12051133e27eSPeter Avalos 	for (;;)
12061133e27eSPeter Avalos 	{
12071133e27eSPeter Avalos 		/*
12081133e27eSPeter Avalos 		 * Get lines until we find a matching one or until
12091133e27eSPeter Avalos 		 * we hit end-of-file (or beginning-of-file if we're
12101133e27eSPeter Avalos 		 * going backwards), or until we hit the end position.
12111133e27eSPeter Avalos 		 */
12121133e27eSPeter Avalos 		if (ABORT_SIGS())
12131133e27eSPeter Avalos 		{
12141133e27eSPeter Avalos 			/*
12151133e27eSPeter Avalos 			 * A signal aborts the search.
12161133e27eSPeter Avalos 			 */
12171133e27eSPeter Avalos 			return (-1);
12181133e27eSPeter Avalos 		}
12191133e27eSPeter Avalos 
12200c7ad07eSAntonio Huete Jimenez 		if ((endpos != NULL_POSITION && !(search_type & SRCH_WRAP) &&
12210c7ad07eSAntonio Huete Jimenez 			(((search_type & SRCH_FORW) && pos >= endpos) ||
12220c7ad07eSAntonio Huete Jimenez 			 ((search_type & SRCH_BACK) && pos <= endpos))) || maxlines == 0)
12231133e27eSPeter Avalos 		{
12241133e27eSPeter Avalos 			/*
12251133e27eSPeter Avalos 			 * Reached end position without a match.
12261133e27eSPeter Avalos 			 */
12271133e27eSPeter Avalos 			if (pendpos != NULL)
12281133e27eSPeter Avalos 				*pendpos = pos;
12291133e27eSPeter Avalos 			return (matches);
12301133e27eSPeter Avalos 		}
12311133e27eSPeter Avalos 		if (maxlines > 0)
12321133e27eSPeter Avalos 			maxlines--;
12331133e27eSPeter Avalos 
12341133e27eSPeter Avalos 		if (search_type & SRCH_FORW)
12351133e27eSPeter Avalos 		{
12361133e27eSPeter Avalos 			/*
12371133e27eSPeter Avalos 			 * Read the next line, and save the
12381133e27eSPeter Avalos 			 * starting position of that line in linepos.
12391133e27eSPeter Avalos 			 */
12401133e27eSPeter Avalos 			linepos = pos;
12411133e27eSPeter Avalos 			pos = forw_raw_line(pos, &line, &line_len);
12421133e27eSPeter Avalos 			if (linenum != 0)
12431133e27eSPeter Avalos 				linenum++;
12441133e27eSPeter Avalos 		} else
12451133e27eSPeter Avalos 		{
12461133e27eSPeter Avalos 			/*
12471133e27eSPeter Avalos 			 * Read the previous line and save the
12481133e27eSPeter Avalos 			 * starting position of that line in linepos.
12491133e27eSPeter Avalos 			 */
12501133e27eSPeter Avalos 			pos = back_raw_line(pos, &line, &line_len);
12511133e27eSPeter Avalos 			linepos = pos;
12521133e27eSPeter Avalos 			if (linenum != 0)
12531133e27eSPeter Avalos 				linenum--;
12541133e27eSPeter Avalos 		}
12551133e27eSPeter Avalos 
12561133e27eSPeter Avalos 		if (pos == NULL_POSITION)
12571133e27eSPeter Avalos 		{
12581133e27eSPeter Avalos 			/*
12591133e27eSPeter Avalos 			 * Reached EOF/BOF without a match.
12601133e27eSPeter Avalos 			 */
12610c7ad07eSAntonio Huete Jimenez 			if (search_type & SRCH_WRAP)
12620c7ad07eSAntonio Huete Jimenez 			{
12630c7ad07eSAntonio Huete Jimenez 				/*
12640c7ad07eSAntonio Huete Jimenez 				 * The search wraps around the current file, so
12650c7ad07eSAntonio Huete Jimenez 				 * try to continue at BOF/EOF.
12660c7ad07eSAntonio Huete Jimenez 				 */
12670c7ad07eSAntonio Huete Jimenez 				if (search_type & SRCH_FORW)
12680c7ad07eSAntonio Huete Jimenez 				{
12690c7ad07eSAntonio Huete Jimenez 					pos = ch_zero();
12700c7ad07eSAntonio Huete Jimenez 				} else
12710c7ad07eSAntonio Huete Jimenez 				{
12720c7ad07eSAntonio Huete Jimenez 					pos = ch_length();
12730c7ad07eSAntonio Huete Jimenez 					if (pos == NULL_POSITION)
12740c7ad07eSAntonio Huete Jimenez 					{
12750c7ad07eSAntonio Huete Jimenez 						(void) ch_end_seek();
12760c7ad07eSAntonio Huete Jimenez 						pos = ch_length();
12770c7ad07eSAntonio Huete Jimenez 					}
12780c7ad07eSAntonio Huete Jimenez 				}
12790c7ad07eSAntonio Huete Jimenez 				if (pos != NULL_POSITION) {
12800c7ad07eSAntonio Huete Jimenez 					/*
12810c7ad07eSAntonio Huete Jimenez 					 * Wrap-around was successful. Clear
12820c7ad07eSAntonio Huete Jimenez 					 * the flag so we don't wrap again, and
12830c7ad07eSAntonio Huete Jimenez 					 * continue the search at new pos.
12840c7ad07eSAntonio Huete Jimenez 					 */
12850c7ad07eSAntonio Huete Jimenez 					search_type &= ~SRCH_WRAP;
12860c7ad07eSAntonio Huete Jimenez 					linenum = find_linenum(pos);
12870c7ad07eSAntonio Huete Jimenez 					continue;
12880c7ad07eSAntonio Huete Jimenez 				}
12890c7ad07eSAntonio Huete Jimenez 			}
12901133e27eSPeter Avalos 			if (pendpos != NULL)
12911133e27eSPeter Avalos 				*pendpos = oldpos;
12921133e27eSPeter Avalos 			return (matches);
12931133e27eSPeter Avalos 		}
12941133e27eSPeter Avalos 
12951133e27eSPeter Avalos 		/*
12961133e27eSPeter Avalos 		 * If we're using line numbers, we might as well
12971133e27eSPeter Avalos 		 * remember the information we have now (the position
12981133e27eSPeter Avalos 		 * and line number of the current line).
12991133e27eSPeter Avalos 		 * Don't do it for every line because it slows down
13001133e27eSPeter Avalos 		 * the search.  Remember the line number only if
13011133e27eSPeter Avalos 		 * we're "far" from the last place we remembered it.
13021133e27eSPeter Avalos 		 */
1303a9adbba3SJan Lentfer 		if (linenums && abs((int)(pos - oldpos)) > 2048)
13041133e27eSPeter Avalos 			add_lnum(linenum, pos);
13051133e27eSPeter Avalos 		oldpos = pos;
13061133e27eSPeter Avalos 
13070c7ad07eSAntonio Huete Jimenez #if HILITE_SEARCH
13088be36e5bSPeter Avalos 		if (is_filtered(linepos))
13098be36e5bSPeter Avalos 			continue;
13100c7ad07eSAntonio Huete Jimenez #endif
1311*320d7c8aSAaron LI 		if (nosearch_headers)
1312*320d7c8aSAaron LI 			skip_bytes = skip_columns(header_cols, &line, &line_len);
13138be36e5bSPeter Avalos 
13141133e27eSPeter Avalos 		/*
13151133e27eSPeter Avalos 		 * If it's a caseless search, convert the line to lowercase.
13161133e27eSPeter Avalos 		 * If we're doing backspace processing, delete backspaces.
13171133e27eSPeter Avalos 		 */
1318*320d7c8aSAaron LI 		cvt_ops = get_cvt_ops(search_type);
1319a9adbba3SJan Lentfer 		cvt_len = cvt_length(line_len, cvt_ops);
1320a9adbba3SJan Lentfer 		cline = (char *) ecalloc(1, cvt_len);
1321a9adbba3SJan Lentfer 		chpos = cvt_alloc_chpos(cvt_len);
1322a9adbba3SJan Lentfer 		cvt_text(cline, line, chpos, &line_len, cvt_ops);
13231133e27eSPeter Avalos 
13248be36e5bSPeter Avalos #if HILITE_SEARCH
13258be36e5bSPeter Avalos 		/*
13260c7ad07eSAntonio Huete Jimenez 		 * If any filters are in effect, ignore non-matching lines.
13278be36e5bSPeter Avalos 		 */
13280c7ad07eSAntonio Huete Jimenez 		if (filter_infos != NULL &&
13290c7ad07eSAntonio Huete Jimenez 		   ((search_type & SRCH_FIND_ALL) ||
1330fa0be7c5SJohn Marino 		     prep_startpos == NULL_POSITION ||
13310c7ad07eSAntonio Huete Jimenez 		     linepos < prep_startpos || linepos >= prep_endpos)) {
1332*320d7c8aSAaron LI 			if (matches_filters(pos, cline, line_len, chpos, linepos, sp, ep, NSP))
1333fa0be7c5SJohn Marino 				continue;
13348be36e5bSPeter Avalos 		}
13358be36e5bSPeter Avalos #endif
13368be36e5bSPeter Avalos 
13371133e27eSPeter Avalos 		/*
13381133e27eSPeter Avalos 		 * Test the next line to see if we have a match.
13391133e27eSPeter Avalos 		 * We are successful if we either want a match and got one,
13401133e27eSPeter Avalos 		 * or if we want a non-match and got one.
13411133e27eSPeter Avalos 		 */
1342a9adbba3SJan Lentfer 		if (prev_pattern(&search_info))
13431133e27eSPeter Avalos 		{
1344e639dc31SJohn Marino 			line_match = match_pattern(info_compiled(&search_info), search_info.text,
1345*320d7c8aSAaron LI 				cline, line_len, sp, ep, NSP, 0, search_type);
13468be36e5bSPeter Avalos 			if (line_match)
13478be36e5bSPeter Avalos 			{
13481133e27eSPeter Avalos 				/*
13491133e27eSPeter Avalos 				 * Got a match.
13501133e27eSPeter Avalos 				 */
13511133e27eSPeter Avalos 				if (search_type & SRCH_FIND_ALL)
13521133e27eSPeter Avalos 				{
13531133e27eSPeter Avalos #if HILITE_SEARCH
13541133e27eSPeter Avalos 					/*
13551133e27eSPeter Avalos 					 * We are supposed to find all matches in the range.
13561133e27eSPeter Avalos 					 * Just add the matches in this line to the
13571133e27eSPeter Avalos 					 * hilite list and keep searching.
13581133e27eSPeter Avalos 					 */
1359*320d7c8aSAaron LI 					hilite_line(linepos + skip_bytes, cline, line_len, chpos, sp, ep, NSP, cvt_ops);
13601133e27eSPeter Avalos #endif
13611133e27eSPeter Avalos 				} else if (--matches <= 0)
13621133e27eSPeter Avalos 				{
13631133e27eSPeter Avalos 					/*
13641133e27eSPeter Avalos 					 * Found the one match we're looking for.
13651133e27eSPeter Avalos 					 * Return it.
13661133e27eSPeter Avalos 					 */
13671133e27eSPeter Avalos #if HILITE_SEARCH
13681133e27eSPeter Avalos 					if (hilite_search == OPT_ON)
13691133e27eSPeter Avalos 					{
13701133e27eSPeter Avalos 						/*
13711133e27eSPeter Avalos 						 * Clear the hilite list and add only
13721133e27eSPeter Avalos 						 * the matches in this one line.
13731133e27eSPeter Avalos 						 */
13741133e27eSPeter Avalos 						clr_hilite();
1375*320d7c8aSAaron LI 						hilite_line(linepos + skip_bytes, cline, line_len, chpos, sp, ep, NSP, cvt_ops);
13761133e27eSPeter Avalos 					}
13771133e27eSPeter Avalos #endif
13780c7ad07eSAntonio Huete Jimenez 					if (chop_line())
13790c7ad07eSAntonio Huete Jimenez 					{
13800c7ad07eSAntonio Huete Jimenez 						/*
13810c7ad07eSAntonio Huete Jimenez 						 * If necessary, shift horizontally to make sure
13820c7ad07eSAntonio Huete Jimenez 						 * search match is fully visible.
13830c7ad07eSAntonio Huete Jimenez 						 */
1384*320d7c8aSAaron LI 						if (sp[0] != NULL && ep[0] != NULL)
13850c7ad07eSAntonio Huete Jimenez 						{
1386*320d7c8aSAaron LI 							int start_off = sp[0] - cline;
1387*320d7c8aSAaron LI 							int end_off = ep[0] - cline;
13880c7ad07eSAntonio Huete Jimenez 							int save_hshift = hshift;
13890c7ad07eSAntonio Huete Jimenez 							int sshift;
13900c7ad07eSAntonio Huete Jimenez 							int eshift;
13910c7ad07eSAntonio Huete Jimenez 							hshift = 0; /* make get_seg count screen lines */
13920c7ad07eSAntonio Huete Jimenez 							sshift = swidth * get_seg(linepos, linepos + chpos[start_off]);
13930c7ad07eSAntonio Huete Jimenez 							eshift = swidth * get_seg(linepos, linepos + chpos[end_off]);
13940c7ad07eSAntonio Huete Jimenez 							if (sshift >= save_hshift && eshift <= save_hshift)
13950c7ad07eSAntonio Huete Jimenez 							{
13960c7ad07eSAntonio Huete Jimenez 								hshift = save_hshift;
13970c7ad07eSAntonio Huete Jimenez 							} else
13980c7ad07eSAntonio Huete Jimenez 							{
13990c7ad07eSAntonio Huete Jimenez 								hshift = sshift;
14000c7ad07eSAntonio Huete Jimenez 								screen_trashed = 1;
14010c7ad07eSAntonio Huete Jimenez 							}
14020c7ad07eSAntonio Huete Jimenez 						}
14030c7ad07eSAntonio Huete Jimenez 					} else if (plastlinepos != NULL)
14040c7ad07eSAntonio Huete Jimenez 					{
14050c7ad07eSAntonio Huete Jimenez 						/*
14060c7ad07eSAntonio Huete Jimenez 						 * If the line is so long that the highlighted match
14070c7ad07eSAntonio Huete Jimenez 						 * won't be seen when the line is displayed normally
14080c7ad07eSAntonio Huete Jimenez 						 * (starting at the first char) because it fills the whole
14090c7ad07eSAntonio Huete Jimenez 						 * screen and more, scroll forward until the last char
14100c7ad07eSAntonio Huete Jimenez 						 * of the match appears in the last line on the screen.
14110c7ad07eSAntonio Huete Jimenez 						 * lastlinepos is the position of the first char of that last line.
14120c7ad07eSAntonio Huete Jimenez 						 */
1413*320d7c8aSAaron LI 						if (ep[0] != NULL)
14140c7ad07eSAntonio Huete Jimenez 						{
1415*320d7c8aSAaron LI 							int end_off = ep[0] - cline;
14160c7ad07eSAntonio Huete Jimenez 							if (end_off >= swidth * sheight / 4) /* heuristic */
14170c7ad07eSAntonio Huete Jimenez 								*plastlinepos = get_lastlinepos(linepos, linepos + chpos[end_off], sheight);
14180c7ad07eSAntonio Huete Jimenez 						}
14190c7ad07eSAntonio Huete Jimenez 					}
14201133e27eSPeter Avalos 					free(cline);
1421a9adbba3SJan Lentfer 					free(chpos);
14221133e27eSPeter Avalos 					if (plinepos != NULL)
14231133e27eSPeter Avalos 						*plinepos = linepos;
14241133e27eSPeter Avalos 					return (0);
14251133e27eSPeter Avalos 				}
14261133e27eSPeter Avalos 			}
14271133e27eSPeter Avalos 		}
14288be36e5bSPeter Avalos 		free(cline);
1429a9adbba3SJan Lentfer 		free(chpos);
14308be36e5bSPeter Avalos 	}
14318be36e5bSPeter Avalos }
14321133e27eSPeter Avalos 
14331133e27eSPeter Avalos /*
14341133e27eSPeter Avalos  * search for a pattern in history. If found, compile that pattern.
14351133e27eSPeter Avalos  */
hist_pattern(int search_type)1436*320d7c8aSAaron LI static int hist_pattern(int search_type)
14371133e27eSPeter Avalos {
14381133e27eSPeter Avalos #if CMD_HISTORY
14391133e27eSPeter Avalos 	char *pattern;
14401133e27eSPeter Avalos 
14411133e27eSPeter Avalos 	set_mlist(ml_search, 0);
14421133e27eSPeter Avalos 	pattern = cmd_lastpattern();
14431133e27eSPeter Avalos 	if (pattern == NULL)
14441133e27eSPeter Avalos 		return (0);
14451133e27eSPeter Avalos 
14460c7ad07eSAntonio Huete Jimenez 	if (set_pattern(&search_info, pattern, search_type, 1) < 0)
14470c7ad07eSAntonio Huete Jimenez 		return (-1);
14481133e27eSPeter Avalos 
14491133e27eSPeter Avalos #if HILITE_SEARCH
14501133e27eSPeter Avalos 	if (hilite_search == OPT_ONPLUS && !hide_hilite)
14511133e27eSPeter Avalos 		hilite_screen();
14521133e27eSPeter Avalos #endif
14531133e27eSPeter Avalos 
14541133e27eSPeter Avalos 	return (1);
14551133e27eSPeter Avalos #else /* CMD_HISTORY */
14561133e27eSPeter Avalos 	return (0);
14571133e27eSPeter Avalos #endif /* CMD_HISTORY */
14581133e27eSPeter Avalos }
14591133e27eSPeter Avalos 
14601133e27eSPeter Avalos /*
146102d62a0fSDaniel Fojt  * Change the caseless-ness of searches.
146202d62a0fSDaniel Fojt  * Updates the internal search state to reflect a change in the -i flag.
146302d62a0fSDaniel Fojt  */
chg_caseless(void)1464*320d7c8aSAaron LI public void chg_caseless(void)
146502d62a0fSDaniel Fojt {
1466*320d7c8aSAaron LI 	if (!search_info.is_ucase_pattern)
146702d62a0fSDaniel Fojt 	{
146802d62a0fSDaniel Fojt 		/*
14690c7ad07eSAntonio Huete Jimenez 		 * Pattern did not have uppercase.
14700c7ad07eSAntonio Huete Jimenez 		 * Set the search caselessness to the global caselessness.
14710c7ad07eSAntonio Huete Jimenez 		 */
14720c7ad07eSAntonio Huete Jimenez 		is_caseless = caseless;
14730c7ad07eSAntonio Huete Jimenez 		/*
14740c7ad07eSAntonio Huete Jimenez 		 * If regex handles caseless, we need to discard
14750c7ad07eSAntonio Huete Jimenez 		 * the pattern which was compiled with the old caseless.
14760c7ad07eSAntonio Huete Jimenez 		 */
14770c7ad07eSAntonio Huete Jimenez 		if (!re_handles_caseless)
14780c7ad07eSAntonio Huete Jimenez 			/* We handle caseless, so the pattern doesn't change. */
14790c7ad07eSAntonio Huete Jimenez 			return;
14800c7ad07eSAntonio Huete Jimenez 	}
14810c7ad07eSAntonio Huete Jimenez 	/*
148202d62a0fSDaniel Fojt 	 * Regenerate the pattern using the new state.
148302d62a0fSDaniel Fojt 	 */
148402d62a0fSDaniel Fojt 	clear_pattern(&search_info);
14850c7ad07eSAntonio Huete Jimenez 	(void) hist_pattern(search_info.search_type);
148602d62a0fSDaniel Fojt }
148702d62a0fSDaniel Fojt 
148802d62a0fSDaniel Fojt /*
14891133e27eSPeter Avalos  * Search for the n-th occurrence of a specified pattern,
14901133e27eSPeter Avalos  * either forward or backward.
14911133e27eSPeter Avalos  * Return the number of matches not yet found in this file
14921133e27eSPeter Avalos  * (that is, n minus the number of matches found).
14931133e27eSPeter Avalos  * Return -1 if the search should be aborted.
14941133e27eSPeter Avalos  * Caller may continue the search in another file
14951133e27eSPeter Avalos  * if less than n matches are found in this file.
14961133e27eSPeter Avalos  */
search(int search_type,char * pattern,int n)1497*320d7c8aSAaron LI public int search(int search_type, char *pattern, int n)
14981133e27eSPeter Avalos {
14991133e27eSPeter Avalos 	POSITION pos;
15000c7ad07eSAntonio Huete Jimenez 	POSITION opos;
15010c7ad07eSAntonio Huete Jimenez 	POSITION lastlinepos = NULL_POSITION;
15021133e27eSPeter Avalos 
15031133e27eSPeter Avalos 	if (pattern == NULL || *pattern == '\0')
15041133e27eSPeter Avalos 	{
15051133e27eSPeter Avalos 		/*
15061133e27eSPeter Avalos 		 * A null pattern means use the previously compiled pattern.
15071133e27eSPeter Avalos 		 */
150825ce721eSPeter Avalos 		search_type |= SRCH_AFTER_TARGET;
15090c7ad07eSAntonio Huete Jimenez 		if (!prev_pattern(&search_info))
15101133e27eSPeter Avalos 		{
15110c7ad07eSAntonio Huete Jimenez 			int r = hist_pattern(search_type);
15120c7ad07eSAntonio Huete Jimenez 			if (r == 0)
15131133e27eSPeter Avalos 				error("No previous regular expression", NULL_PARG);
15140c7ad07eSAntonio Huete Jimenez 			if (r <= 0)
15151133e27eSPeter Avalos 				return (-1);
15161133e27eSPeter Avalos 		}
15171133e27eSPeter Avalos 		if ((search_type & SRCH_NO_REGEX) !=
1518a9adbba3SJan Lentfer 		      (search_info.search_type & SRCH_NO_REGEX))
15191133e27eSPeter Avalos 		{
15201133e27eSPeter Avalos 			error("Please re-enter search pattern", NULL_PARG);
15211133e27eSPeter Avalos 			return -1;
15221133e27eSPeter Avalos 		}
15231133e27eSPeter Avalos #if HILITE_SEARCH
152402d62a0fSDaniel Fojt 		if (hilite_search == OPT_ON || status_col)
15251133e27eSPeter Avalos 		{
15261133e27eSPeter Avalos 			/*
15271133e27eSPeter Avalos 			 * Erase the highlights currently on screen.
15281133e27eSPeter Avalos 			 * If the search fails, we'll redisplay them later.
15291133e27eSPeter Avalos 			 */
15301133e27eSPeter Avalos 			repaint_hilite(0);
15311133e27eSPeter Avalos 		}
15321133e27eSPeter Avalos 		if (hilite_search == OPT_ONPLUS && hide_hilite)
15331133e27eSPeter Avalos 		{
15341133e27eSPeter Avalos 			/*
15351133e27eSPeter Avalos 			 * Highlight any matches currently on screen,
15361133e27eSPeter Avalos 			 * before we actually start the search.
15371133e27eSPeter Avalos 			 */
15381133e27eSPeter Avalos 			hide_hilite = 0;
15391133e27eSPeter Avalos 			hilite_screen();
15401133e27eSPeter Avalos 		}
15411133e27eSPeter Avalos 		hide_hilite = 0;
15421133e27eSPeter Avalos #endif
15431133e27eSPeter Avalos 	} else
15441133e27eSPeter Avalos 	{
15451133e27eSPeter Avalos 		/*
15461133e27eSPeter Avalos 		 * Compile the pattern.
15471133e27eSPeter Avalos 		 */
15480c7ad07eSAntonio Huete Jimenez 		int show_error = !(search_type & SRCH_INCR);
15490c7ad07eSAntonio Huete Jimenez 		if (set_pattern(&search_info, pattern, search_type, show_error) < 0)
15501133e27eSPeter Avalos 			return (-1);
15511133e27eSPeter Avalos #if HILITE_SEARCH
155202d62a0fSDaniel Fojt 		if (hilite_search || status_col)
15531133e27eSPeter Avalos 		{
15541133e27eSPeter Avalos 			/*
15551133e27eSPeter Avalos 			 * Erase the highlights currently on screen.
15561133e27eSPeter Avalos 			 * Also permanently delete them from the hilite list.
15571133e27eSPeter Avalos 			 */
15581133e27eSPeter Avalos 			repaint_hilite(0);
15591133e27eSPeter Avalos 			hide_hilite = 0;
15601133e27eSPeter Avalos 			clr_hilite();
15611133e27eSPeter Avalos 		}
156202d62a0fSDaniel Fojt 		if (hilite_search == OPT_ONPLUS || status_col)
15631133e27eSPeter Avalos 		{
15641133e27eSPeter Avalos 			/*
15651133e27eSPeter Avalos 			 * Highlight any matches currently on screen,
15661133e27eSPeter Avalos 			 * before we actually start the search.
15671133e27eSPeter Avalos 			 */
15681133e27eSPeter Avalos 			hilite_screen();
15691133e27eSPeter Avalos 		}
15701133e27eSPeter Avalos #endif
15711133e27eSPeter Avalos 	}
15721133e27eSPeter Avalos 
15731133e27eSPeter Avalos 	/*
15741133e27eSPeter Avalos 	 * Figure out where to start the search.
15751133e27eSPeter Avalos 	 */
15761133e27eSPeter Avalos 	pos = search_pos(search_type);
15770c7ad07eSAntonio Huete Jimenez 	opos = position(sindex_from_sline(jump_sline));
15781133e27eSPeter Avalos 	if (pos == NULL_POSITION)
15791133e27eSPeter Avalos 	{
15801133e27eSPeter Avalos 		/*
15811133e27eSPeter Avalos 		 * Can't find anyplace to start searching from.
15821133e27eSPeter Avalos 		 */
15831133e27eSPeter Avalos 		if (search_type & SRCH_PAST_EOF)
15841133e27eSPeter Avalos 			return (n);
15850c7ad07eSAntonio Huete Jimenez #if HILITE_SEARCH
158602d62a0fSDaniel Fojt 		if (hilite_search == OPT_ON || status_col)
158702d62a0fSDaniel Fojt 			repaint_hilite(1);
15880c7ad07eSAntonio Huete Jimenez #endif
15891133e27eSPeter Avalos 		error("Nothing to search", NULL_PARG);
15901133e27eSPeter Avalos 		return (-1);
15911133e27eSPeter Avalos 	}
15921133e27eSPeter Avalos 
15931133e27eSPeter Avalos 	n = search_range(pos, NULL_POSITION, search_type, n, -1,
15940c7ad07eSAntonio Huete Jimenez 			&pos, (POSITION*)NULL, &lastlinepos);
15951133e27eSPeter Avalos 	if (n != 0)
15961133e27eSPeter Avalos 	{
15971133e27eSPeter Avalos 		/*
15981133e27eSPeter Avalos 		 * Search was unsuccessful.
15991133e27eSPeter Avalos 		 */
16001133e27eSPeter Avalos #if HILITE_SEARCH
160102d62a0fSDaniel Fojt 		if ((hilite_search == OPT_ON || status_col) && n > 0)
16021133e27eSPeter Avalos 			/*
16031133e27eSPeter Avalos 			 * Redisplay old hilites.
16041133e27eSPeter Avalos 			 */
16051133e27eSPeter Avalos 			repaint_hilite(1);
16061133e27eSPeter Avalos #endif
16071133e27eSPeter Avalos 		return (n);
16081133e27eSPeter Avalos 	}
16091133e27eSPeter Avalos 
16101133e27eSPeter Avalos 	if (!(search_type & SRCH_NO_MOVE))
16111133e27eSPeter Avalos 	{
16121133e27eSPeter Avalos 		/*
16131133e27eSPeter Avalos 		 * Go to the matching line.
16141133e27eSPeter Avalos 		 */
16150c7ad07eSAntonio Huete Jimenez 		if (lastlinepos != NULL_POSITION)
16160c7ad07eSAntonio Huete Jimenez 			jump_loc(lastlinepos, BOTTOM);
16170c7ad07eSAntonio Huete Jimenez 		else if (pos != opos)
16181133e27eSPeter Avalos 			jump_loc(pos, jump_sline);
16191133e27eSPeter Avalos 	}
16201133e27eSPeter Avalos 
16211133e27eSPeter Avalos #if HILITE_SEARCH
162202d62a0fSDaniel Fojt 	if (hilite_search == OPT_ON || status_col)
16231133e27eSPeter Avalos 		/*
16241133e27eSPeter Avalos 		 * Display new hilites in the matching line.
16251133e27eSPeter Avalos 		 */
16261133e27eSPeter Avalos 		repaint_hilite(1);
16271133e27eSPeter Avalos #endif
16281133e27eSPeter Avalos 	return (0);
16291133e27eSPeter Avalos }
16301133e27eSPeter Avalos 
16311133e27eSPeter Avalos #if HILITE_SEARCH
16321133e27eSPeter Avalos /*
16331133e27eSPeter Avalos  * Prepare hilites in a given range of the file.
16341133e27eSPeter Avalos  *
16351133e27eSPeter Avalos  * The pair (prep_startpos,prep_endpos) delimits a contiguous region
16361133e27eSPeter Avalos  * of the file that has been "prepared"; that is, scanned for matches for
16371133e27eSPeter Avalos  * the current search pattern, and hilites have been created for such matches.
16381133e27eSPeter Avalos  * If prep_startpos == NULL_POSITION, the prep region is empty.
16391133e27eSPeter Avalos  * If prep_endpos == NULL_POSITION, the prep region extends to EOF.
16401133e27eSPeter Avalos  * prep_hilite asks that the range (spos,epos) be covered by the prep region.
16411133e27eSPeter Avalos  */
prep_hilite(POSITION spos,POSITION epos,int maxlines)1642*320d7c8aSAaron LI public void prep_hilite(POSITION spos, POSITION epos, int maxlines)
16431133e27eSPeter Avalos {
16441133e27eSPeter Avalos 	POSITION nprep_startpos = prep_startpos;
16451133e27eSPeter Avalos 	POSITION nprep_endpos = prep_endpos;
16461133e27eSPeter Avalos 	POSITION new_epos;
16471133e27eSPeter Avalos 	POSITION max_epos;
16481133e27eSPeter Avalos 	int result;
16491133e27eSPeter Avalos 	int i;
1650a9adbba3SJan Lentfer 
16511133e27eSPeter Avalos /*
16521133e27eSPeter Avalos  * Search beyond where we're asked to search, so the prep region covers
16531133e27eSPeter Avalos  * more than we need.  Do one big search instead of a bunch of small ones.
16541133e27eSPeter Avalos  */
16551133e27eSPeter Avalos #define SEARCH_MORE (3*size_linebuf)
16561133e27eSPeter Avalos 
1657a9adbba3SJan Lentfer 	if (!prev_pattern(&search_info) && !is_filtering())
16581133e27eSPeter Avalos 		return;
16591133e27eSPeter Avalos 
16601133e27eSPeter Avalos 	/*
1661fa0be7c5SJohn Marino 	 * Make sure our prep region always starts at the beginning of
1662fa0be7c5SJohn Marino 	 * a line. (search_range takes care of the end boundary below.)
1663fa0be7c5SJohn Marino 	 */
1664fa0be7c5SJohn Marino 	spos = back_raw_line(spos+1, (char **)NULL, (int *)NULL);
1665fa0be7c5SJohn Marino 
1666fa0be7c5SJohn Marino 	/*
16671133e27eSPeter Avalos 	 * If we're limited to a max number of lines, figure out the
16681133e27eSPeter Avalos 	 * file position we should stop at.
16691133e27eSPeter Avalos 	 */
16701133e27eSPeter Avalos 	if (maxlines < 0)
16711133e27eSPeter Avalos 		max_epos = NULL_POSITION;
16721133e27eSPeter Avalos 	else
16731133e27eSPeter Avalos 	{
16741133e27eSPeter Avalos 		max_epos = spos;
16751133e27eSPeter Avalos 		for (i = 0;  i < maxlines;  i++)
16761133e27eSPeter Avalos 			max_epos = forw_raw_line(max_epos, (char **)NULL, (int *)NULL);
16771133e27eSPeter Avalos 	}
16781133e27eSPeter Avalos 
16791133e27eSPeter Avalos 	/*
16801133e27eSPeter Avalos 	 * Find two ranges:
16811133e27eSPeter Avalos 	 * The range that we need to search (spos,epos); and the range that
16821133e27eSPeter Avalos 	 * the "prep" region will then cover (nprep_startpos,nprep_endpos).
16831133e27eSPeter Avalos 	 */
16841133e27eSPeter Avalos 
16851133e27eSPeter Avalos 	if (prep_startpos == NULL_POSITION ||
16861133e27eSPeter Avalos 	    (epos != NULL_POSITION && epos < prep_startpos) ||
16871133e27eSPeter Avalos 	    spos > prep_endpos)
16881133e27eSPeter Avalos 	{
16891133e27eSPeter Avalos 		/*
16901133e27eSPeter Avalos 		 * New range is not contiguous with old prep region.
16911133e27eSPeter Avalos 		 * Discard the old prep region and start a new one.
16921133e27eSPeter Avalos 		 */
16931133e27eSPeter Avalos 		clr_hilite();
16948be36e5bSPeter Avalos 		clr_filter();
16951133e27eSPeter Avalos 		if (epos != NULL_POSITION)
16961133e27eSPeter Avalos 			epos += SEARCH_MORE;
16971133e27eSPeter Avalos 		nprep_startpos = spos;
16981133e27eSPeter Avalos 	} else
16991133e27eSPeter Avalos 	{
17001133e27eSPeter Avalos 		/*
17011133e27eSPeter Avalos 		 * New range partially or completely overlaps old prep region.
17021133e27eSPeter Avalos 		 */
17031133e27eSPeter Avalos 		if (epos == NULL_POSITION)
17041133e27eSPeter Avalos 		{
17051133e27eSPeter Avalos 			/*
17061133e27eSPeter Avalos 			 * New range goes to end of file.
17071133e27eSPeter Avalos 			 */
17081133e27eSPeter Avalos 			;
17091133e27eSPeter Avalos 		} else if (epos > prep_endpos)
17101133e27eSPeter Avalos 		{
17111133e27eSPeter Avalos 			/*
17121133e27eSPeter Avalos 			 * New range ends after old prep region.
17131133e27eSPeter Avalos 			 * Extend prep region to end at end of new range.
17141133e27eSPeter Avalos 			 */
17151133e27eSPeter Avalos 			epos += SEARCH_MORE;
17161133e27eSPeter Avalos 		} else /* (epos <= prep_endpos) */
17171133e27eSPeter Avalos 		{
17181133e27eSPeter Avalos 			/*
17191133e27eSPeter Avalos 			 * New range ends within old prep region.
17201133e27eSPeter Avalos 			 * Truncate search to end at start of old prep region.
17211133e27eSPeter Avalos 			 */
17221133e27eSPeter Avalos 			epos = prep_startpos;
17231133e27eSPeter Avalos 		}
17241133e27eSPeter Avalos 
17251133e27eSPeter Avalos 		if (spos < prep_startpos)
17261133e27eSPeter Avalos 		{
17271133e27eSPeter Avalos 			/*
17281133e27eSPeter Avalos 			 * New range starts before old prep region.
17291133e27eSPeter Avalos 			 * Extend old prep region backwards to start at
17301133e27eSPeter Avalos 			 * start of new range.
17311133e27eSPeter Avalos 			 */
17321133e27eSPeter Avalos 			if (spos < SEARCH_MORE)
17331133e27eSPeter Avalos 				spos = 0;
17341133e27eSPeter Avalos 			else
17351133e27eSPeter Avalos 				spos -= SEARCH_MORE;
17361133e27eSPeter Avalos 			nprep_startpos = spos;
17371133e27eSPeter Avalos 		} else /* (spos >= prep_startpos) */
17381133e27eSPeter Avalos 		{
17391133e27eSPeter Avalos 			/*
17401133e27eSPeter Avalos 			 * New range starts within or after old prep region.
17411133e27eSPeter Avalos 			 * Trim search to start at end of old prep region.
17421133e27eSPeter Avalos 			 */
17431133e27eSPeter Avalos 			spos = prep_endpos;
17441133e27eSPeter Avalos 		}
17451133e27eSPeter Avalos 	}
17461133e27eSPeter Avalos 
17471133e27eSPeter Avalos 	if (epos != NULL_POSITION && max_epos != NULL_POSITION &&
17481133e27eSPeter Avalos 	    epos > max_epos)
17491133e27eSPeter Avalos 		/*
17501133e27eSPeter Avalos 		 * Don't go past the max position we're allowed.
17511133e27eSPeter Avalos 		 */
17521133e27eSPeter Avalos 		epos = max_epos;
17531133e27eSPeter Avalos 
17541133e27eSPeter Avalos 	if (epos == NULL_POSITION || epos > spos)
17551133e27eSPeter Avalos 	{
1756a9adbba3SJan Lentfer 		int search_type = SRCH_FORW | SRCH_FIND_ALL;
1757*320d7c8aSAaron LI 		search_type |= (search_info.search_type & (SRCH_NO_REGEX|SRCH_SUBSEARCH_ALL));
1758fa0be7c5SJohn Marino 		for (;;)
1759fa0be7c5SJohn Marino 		{
17600c7ad07eSAntonio Huete Jimenez 			result = search_range(spos, epos, search_type, 0, maxlines, (POSITION*)NULL, &new_epos, (POSITION*)NULL);
17611133e27eSPeter Avalos 			if (result < 0)
17621133e27eSPeter Avalos 				return;
17631133e27eSPeter Avalos 			if (prep_endpos == NULL_POSITION || new_epos > prep_endpos)
17641133e27eSPeter Avalos 				nprep_endpos = new_epos;
1765fa0be7c5SJohn Marino 
1766fa0be7c5SJohn Marino 			/*
1767fa0be7c5SJohn Marino 			 * Check both ends of the resulting prep region to
1768fa0be7c5SJohn Marino 			 * make sure they're not filtered. If they are,
1769fa0be7c5SJohn Marino 			 * keep going at least one more line until we find
1770fa0be7c5SJohn Marino 			 * something that isn't filtered, or hit the end.
1771fa0be7c5SJohn Marino 			 */
1772fa0be7c5SJohn Marino 			if (prep_endpos == NULL_POSITION || nprep_endpos > prep_endpos)
1773fa0be7c5SJohn Marino 			{
1774fa0be7c5SJohn Marino 				if (new_epos >= nprep_endpos && is_filtered(new_epos-1))
1775fa0be7c5SJohn Marino 				{
1776fa0be7c5SJohn Marino 					spos = nprep_endpos;
1777fa0be7c5SJohn Marino 					epos = forw_raw_line(nprep_endpos, (char **)NULL, (int *)NULL);
1778fa0be7c5SJohn Marino 					if (epos == NULL_POSITION)
1779fa0be7c5SJohn Marino 						break;
1780fa0be7c5SJohn Marino 					maxlines = 1;
1781fa0be7c5SJohn Marino 					continue;
1782fa0be7c5SJohn Marino 				}
1783fa0be7c5SJohn Marino 			}
1784fa0be7c5SJohn Marino 
1785fa0be7c5SJohn Marino 			if (prep_startpos == NULL_POSITION || nprep_startpos < prep_startpos)
1786fa0be7c5SJohn Marino 			{
1787fa0be7c5SJohn Marino 				if (nprep_startpos > 0 && is_filtered(nprep_startpos))
1788fa0be7c5SJohn Marino 				{
1789fa0be7c5SJohn Marino 					epos = nprep_startpos;
1790fa0be7c5SJohn Marino 					spos = back_raw_line(nprep_startpos, (char **)NULL, (int *)NULL);
1791fa0be7c5SJohn Marino 					if (spos == NULL_POSITION)
1792fa0be7c5SJohn Marino 						break;
1793fa0be7c5SJohn Marino 					nprep_startpos = spos;
1794fa0be7c5SJohn Marino 					maxlines = 1;
1795fa0be7c5SJohn Marino 					continue;
1796fa0be7c5SJohn Marino 				}
1797fa0be7c5SJohn Marino 			}
1798fa0be7c5SJohn Marino 			break;
1799fa0be7c5SJohn Marino 		}
18001133e27eSPeter Avalos 	}
18011133e27eSPeter Avalos 	prep_startpos = nprep_startpos;
18021133e27eSPeter Avalos 	prep_endpos = nprep_endpos;
18031133e27eSPeter Avalos }
18048be36e5bSPeter Avalos 
18058be36e5bSPeter Avalos /*
18068be36e5bSPeter Avalos  * Set the pattern to be used for line filtering.
18078be36e5bSPeter Avalos  */
set_filter_pattern(char * pattern,int search_type)1808*320d7c8aSAaron LI public void set_filter_pattern(char *pattern, int search_type)
18098be36e5bSPeter Avalos {
18100c7ad07eSAntonio Huete Jimenez 	struct pattern_info *filter;
18110c7ad07eSAntonio Huete Jimenez 
18128be36e5bSPeter Avalos 	clr_filter();
18138be36e5bSPeter Avalos 	if (pattern == NULL || *pattern == '\0')
18140c7ad07eSAntonio Huete Jimenez 	{
18150c7ad07eSAntonio Huete Jimenez 		/* Clear and free all filters. */
18160c7ad07eSAntonio Huete Jimenez 		for (filter = filter_infos; filter != NULL; )
18170c7ad07eSAntonio Huete Jimenez 		{
18180c7ad07eSAntonio Huete Jimenez 			struct pattern_info *next_filter = filter->next;
18190c7ad07eSAntonio Huete Jimenez 			clear_pattern(filter);
18200c7ad07eSAntonio Huete Jimenez 			free(filter);
18210c7ad07eSAntonio Huete Jimenez 			filter = next_filter;
18220c7ad07eSAntonio Huete Jimenez 		}
18230c7ad07eSAntonio Huete Jimenez 		filter_infos = NULL;
18240c7ad07eSAntonio Huete Jimenez 	} else
18250c7ad07eSAntonio Huete Jimenez 	{
18260c7ad07eSAntonio Huete Jimenez 		/* Create a new filter and add it to the filter_infos list. */
18270c7ad07eSAntonio Huete Jimenez 		filter = ecalloc(1, sizeof(struct pattern_info));
18280c7ad07eSAntonio Huete Jimenez 		init_pattern(filter);
18290c7ad07eSAntonio Huete Jimenez 		if (set_pattern(filter, pattern, search_type, 1) < 0)
18300c7ad07eSAntonio Huete Jimenez 		{
18310c7ad07eSAntonio Huete Jimenez 			free(filter);
18320c7ad07eSAntonio Huete Jimenez 			return;
18330c7ad07eSAntonio Huete Jimenez 		}
18340c7ad07eSAntonio Huete Jimenez 		filter->next = filter_infos;
18350c7ad07eSAntonio Huete Jimenez 		filter_infos = filter;
18360c7ad07eSAntonio Huete Jimenez 	}
18378be36e5bSPeter Avalos 	screen_trashed = 1;
18388be36e5bSPeter Avalos }
18398be36e5bSPeter Avalos 
18408be36e5bSPeter Avalos /*
18418be36e5bSPeter Avalos  * Is there a line filter in effect?
18428be36e5bSPeter Avalos  */
is_filtering(void)1843*320d7c8aSAaron LI public int is_filtering(void)
18448be36e5bSPeter Avalos {
18458be36e5bSPeter Avalos 	if (ch_getflags() & CH_HELPFILE)
18468be36e5bSPeter Avalos 		return (0);
18470c7ad07eSAntonio Huete Jimenez 	return (filter_infos != NULL);
18488be36e5bSPeter Avalos }
18491133e27eSPeter Avalos #endif
18501133e27eSPeter Avalos 
18511133e27eSPeter Avalos #if HAVE_V8_REGCOMP
18521133e27eSPeter Avalos /*
18531133e27eSPeter Avalos  * This function is called by the V8 regcomp to report
18541133e27eSPeter Avalos  * errors in regular expressions.
18551133e27eSPeter Avalos  */
1856fa0be7c5SJohn Marino public int reg_show_error = 1;
1857fa0be7c5SJohn Marino 
regerror(char * s)1858*320d7c8aSAaron LI void regerror(char *s)
18591133e27eSPeter Avalos {
18601133e27eSPeter Avalos 	PARG parg;
18611133e27eSPeter Avalos 
1862fa0be7c5SJohn Marino 	if (!reg_show_error)
1863fa0be7c5SJohn Marino 		return;
18641133e27eSPeter Avalos 	parg.p_string = s;
18651133e27eSPeter Avalos 	error("%s", &parg);
18661133e27eSPeter Avalos }
18671133e27eSPeter Avalos #endif
18681133e27eSPeter Avalos 
1869