File: vsearch.c

package info (click to toggle)
wily 0.13.33-2
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 1,500 kB
  • ctags: 1,720
  • sloc: ansic: 12,830; sh: 245; makefile: 188
file content (201 lines) | stat: -rw-r--r-- 4,397 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include "wily.h"
#include "view.h"

static Bool	view_literal(View**vp, Range*r, char*s);
static Bool	view_gotofile(View**vp, Range *r, char *s);

/* If 'label' is the label of some existing file, return View for it. */
View*
openlabel(char*label, Bool create) {
	View*v;
	Path	contracted;
	
	pathcontract(contracted,label);
	
	if ( (v=data_find(contracted)) ) {
		tile_show(v->tile);
		return v;
	} else if ( (v=data_open(contracted,create)) )
		return v;
	else
		return 0;
}

/* Look (builtin) for 'arg' in the context of 'v' */
void
view_look (View *v, char *arg) {
	Bool		found = false;
	ulong	p;
	Range	r;

	assert(ISBODY(v));

	p = (v->sel.p0 + 1) % text_length(v->t);
	r = range(p, p);

	if(arg) {
		found = text_findliteralutf(v->t, &r, arg);
	} else if (RLEN(v->sel) ){
		found = text_look(v->t, &r, v->sel);
	} else {
		found = false;
	}

	if(found){
		view_show(v,r);
		view_select(v, r);
		view_setlastselection(v);
	}
}

/*
 * Search for address 's'.  Use *vp for context, *r as the starting point
 * for searches.  May create a new window, but doesn't otherwise affect
 * the screen.  If it finds something, return true, and fill in *vp and
 * and *r with the location of where we found something.
 *
 * 's' may be of the form path:addr, :addr, path or a literal, searched
 * for in that order.
 *
 * NB modifies 's'
 */
Bool
view_goto(View**vp, Range *r, char *s) {
	return view_gotofile(vp,r,s) || view_literal(vp, r,s);
}

/* We've clicked the 'goto' button, selecting 'r' in 'v'
 * If this window is under external control, just send the event,
 * otherwise expand the selection, and 'goto' it, in the context of 'v'.
 */
void
b3(View *v, Range r) {
	char	*s;
	View *oldv;
	Range expanded;
	Data	*d;
	View*found;
	
	/* Try to send simply expanded version to remote process */
	expanded = view_expand(v, r, notaddress);
	if (!RLEN(expanded))
		return;	/* empty click nowhere -- ignore */
	s = text_duputf(v->t, expanded);
	d = view_data(v);
	oldv = v;
	
	/* Send to remote process? */
	if(data_sendgoto(d,expanded, s))
		goto cleanup;
	
	
	if (view_gotofile(&v, &expanded, s)) { /* Simple file? */
		r = expanded;
	} else if ( (found = openinclude(v,r)) ) {
		v = found;
		r = found->sel;
	} else if (view_literal(&v, &expanded, s)) { /* Literal? */
		r = expanded;
	} else {	/* found nothing */
		goto cleanup;
	}
	
	view_show(v,r);
	view_select(v,r);
	view_setlastselection(v);

	/* warp unless b3 in the tag jumps to the body. */
	if (oldv != tile_tag(view_win(v)))
		view_warp(v,r);
	
cleanup:
	free(s);
}

/* Search for literal 's' in '*vp' starting at '*r'.
 * If we find a match, update 'vp' and 'r' and return true,
 * otherwise, return false.
 */
static Bool
view_literal(View**vp, Range*r, char*s) {
	View*v;
	Text	*t;
	Range	tmp;
	
	v = *vp;
	tmp = *r;
	/* Only makes sense if 'v' is a body or we can find a useful body */
	if(!ISBODY(v)){
		if ((v = view_body(v)) || (v = view_body(last_selection))) {
			tmp = v->sel;
		} else {
			return false;
		}
	}
	assert(ISBODY(v));
	
	t = v->t;
	if(text_findliteralutf(t, &tmp, s)) {
		*vp = v;
		*r = tmp;
		return true;
	}
	return false;
}

/*
 * Search for address 's'.  Use *vp for context, *r as the starting point
 * for searches.  May create a new window, but doesn't otherwise affect
 * the screen.  If it finds something, update *vp and *r and return
 * true, otherwise return false and leave vp and r alone
 *
 * 's' may be of the form path:addr, or path, or :addr.
 */
static Bool
view_gotofile(View**vp, Range *r, char *s) {
	View	*v = *vp;
	View	*v2;
	char	*addr;
	char *colon;	/* preserve the colon */
	Path	label;

	colon = strchr(s, ':');
	if (colon) {
		addr = colon + 1;
		*colon=0;
	}else
		addr = 0;

	/* look for 's' as a file or directory */
	if(strlen(s)) {
		data_addcontext(view_data(v), label, s);
		if(colon)
			*colon=':';
		if( (v2=openlabel(label, false))){
			*vp = v = v2;
			*r = v->sel;
			if(!addr)
				return true;	/* "label" */
		} else if (addr)
			return false;	/* label:addr, bad path */
	}
	/*
         * If we get this far, either we didn't find a matching file, or
         * we still have to evaluate an address.
	 */
	 if(colon)
	 	*colon = ':';
	if(!addr)
		return false;
	
	/* :addr by itself then */
	if(!ISBODY(v)){
		if( ! ((v = view_body(v)) || (v = view_body(last_selection))) )
			return false;
		*vp = v;
 		*r = v->sel;
	}

	return text_search(v->t, r, addr, v->sel);
}