summaryrefslogtreecommitdiff
path: root/sys/src/cmd/mothra/libpanel/edit.c
blob: f089f3b8e5f6c32986b02b5994e717a737972204 (plain)
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
 * Interface includes:
 *	void plescroll(Panel *p, int top);
 *		move the given character position onto the top line
 *	void plegetsel(Panel *p, int *sel0, int *sel1);
 *		read the selection back
 *	int plelen(Panel *p);
 *		read the length of the text back
 *	Rune *pleget(Panel *p);
 *		get a pointer to the text
 *	void plesel(Panel *p, int sel0, int sel1);
 *		set the selection -- adjusts hiliting
 *	void plepaste(Panel *p, Rune *text, int ntext);
 *		replace the selection with the given text
 */
#include <u.h>
#include <libc.h>
#include <draw.h>
#include <event.h>
#include <panel.h>
#include "pldefs.h"
#include <keyboard.h>

typedef struct Edit Edit;
struct Edit{
	Point minsize;
	void (*hit)(Panel *);
	int sel0, sel1;
	Textwin *t;
	Rune *text;
	int ntext;
};
void pl_drawedit(Panel *p){
	Edit *ep;
	Panel *sb;
	ep=p->data;
	if(ep->t==0){
		ep->t=twnew(p->b, font, ep->text, ep->ntext);
		if(ep->t==0){
			fprint(2, "pl_drawedit: can't allocate\n");
			exits("no mem");
		}
	}
	ep->t->b=p->b;
	twreshape(ep->t, p->r);
	twhilite(ep->t, ep->sel0, ep->sel1, 1);
	sb=p->yscroller;
	if(sb && sb->setscrollbar)
		sb->setscrollbar(sb, ep->t->top, ep->t->bot, ep->t->etext-ep->t->text);
}

char *pl_snarfedit(Panel *p){
	int s0, s1;
	Rune *t;

	t=pleget(p);
	plegetsel(p, &s0, &s1);
	if(t==0 || s0>=s1)
		return nil;
	return smprint("%.*S", s1-s0, t+s0);
}
void pl_pasteedit(Panel *p, char *s){
	Rune *t;
	if(t=runesmprint("%s", s)){
		plepaste(p, t, runestrlen(t));
		free(t);
	}
}

/*
 * Should do double-clicks:
 *	If ep->sel0==ep->sel1 on entry and the
 *	call to twselect returns the same selection, then
 *	expand selections (| marks possible selection points, ... is expanded selection)
 *	<|...|>			<> must nest
 *	(|...|)			() must nest
 *	[|...|]			[] must nest
 *	{|...|}			{} must nest
 *	'|...|'			no ' in ...
 *	"|...|"			no " in ...
 *	\n|...|\n		either newline may be the corresponding end of text
 *				include the trailing newline in the selection
 *	...|I...		I and ... are characters satisfying pl_idchar(I)
 *	...I|
 */
int pl_hitedit(Panel *p, Mouse *m){
	Edit *ep;
	if(m->buttons&1){
		plgrabkb(p);
		ep=p->data;
		ep->t->b=p->b;
		twhilite(ep->t, ep->sel0, ep->sel1, 0);
		twselect(ep->t, m);
		ep->sel0=ep->t->sel0;
		ep->sel1=ep->t->sel1;
		if((m->buttons&7)==3){
			plsnarf(p);
			plepaste(p, 0, 0);	/* cut */
		}
		else if((m->buttons&7)==5)
			plpaste(p);
		else if(ep->hit)
			(*ep->hit)(p);
	}
	return 0;
}
void pl_scrolledit(Panel *p, int dir, int buttons, int num, int den){
	Edit *ep;
	Textwin *t;
	Panel *sb;
	int index, nline;
	if(dir!=VERT) return;
	ep=p->data;
	t=ep->t;
	t->b=p->b;
	switch(buttons){
	default:
		return;
	case 1:		/* top line moves to mouse position */
		nline=(t->r.max.y-t->r.min.y)/t->hgt*num/den;
		index=t->top;
		while(index!=0 && nline!=0)
			if(t->text[--index]=='\n') --nline;
		break;
	case 2:		/* absolute */
		index=(t->etext-t->text)*num/den;
		break;
	case 4:		/* mouse points at new top line */
		index=twpt2rune(t,
			Pt(t->r.min.x, t->r.min.y+(t->r.max.y-t->r.min.y)*num/den));
		break;
	}
	while(index!=0 && t->text[index-1]!='\n') --index;
	if(index!=t->top){
		twhilite(ep->t, ep->sel0, ep->sel1, 0);
		twscroll(t, index);
		p->scr.pos.y=t->top;
		twhilite(ep->t, ep->sel0, ep->sel1, 1);
		sb=p->yscroller;
		if(sb && sb->setscrollbar)
			sb->setscrollbar(sb, t->top, t->bot, t->etext-t->text);
	}
}
void pl_typeedit(Panel *p, Rune c){
	Edit *ep;
	Textwin *t;
	int bot, scrolled;
	Panel *sb;
	ep=p->data;
	t=ep->t;
	t->b=p->b;
	twhilite(t, ep->sel0, ep->sel1, 0);
	switch(c){
	case Kesc:
		plsnarf(p);
		plepaste(p, 0, 0);	/* cut */
		break;
	case Kdel:	/* clear */
		ep->sel0=0;
		ep->sel1=plelen(p);
		plepaste(p, 0, 0);	/* cut */
		break;
	case Kbs:	/* ^H: erase character */
		if(ep->sel0!=0) --ep->sel0;
		twreplace(t, ep->sel0, ep->sel1, 0, 0);
		break;
	case Knack:	/* ^U: erase line */
		while(ep->sel0!=0 && t->text[ep->sel0-1]!='\n') --ep->sel0;
		twreplace(t, ep->sel0, ep->sel1, 0, 0);
		break;
	case Ketb:	/* ^W: erase word */
		while(ep->sel0!=0 && !pl_idchar(t->text[ep->sel0-1])) --ep->sel0;
		while(ep->sel0!=0 && pl_idchar(t->text[ep->sel0-1])) --ep->sel0;
		twreplace(t, ep->sel0, ep->sel1, 0, 0);
		break;
	default:
		if((c & 0xFF00) == KF || (c & 0xFF00) == Spec)
			break;
		twreplace(t, ep->sel0, ep->sel1, &c, 1);
		++ep->sel0;
		break;
	}
	ep->sel1=ep->sel0;
	/*
	 * Scroll up until ep->sel0 is above t->bot.
	 */
	scrolled=0;
	do{
		bot=t->bot;
		if(ep->sel0<=bot) break;
		twscroll(t, twpt2rune(t, Pt(t->r.min.x, t->r.min.y+font->height)));
		scrolled++;
	}while(bot!=t->bot);
	if(scrolled){
		sb=p->yscroller;
		if(sb && sb->setscrollbar)
			sb->setscrollbar(sb, t->top, t->bot, t->etext-t->text);
	}
	twhilite(t, ep->sel0, ep->sel1, 1);
}
Point pl_getsizeedit(Panel *p, Point children){
	USED(children);
	return pl_boxsize(((Edit *)p->data)->minsize, p->state);
}
void pl_childspaceedit(Panel *g, Point *ul, Point *size){
	USED(g, ul, size);
}
void pl_freeedit(Panel *p){
	Edit *ep;
	ep=p->data;
	if(ep->t!=nil) twfree(ep->t);
	ep->t=0;
}
void plinitedit(Panel *v, int flags, Point minsize, Rune *text, int ntext, void (*hit)(Panel *)){
	Edit *ep;
	ep=v->data;
	v->flags=flags|LEAF;
	v->state=UP;
	v->draw=pl_drawedit;
	v->hit=pl_hitedit;
	v->type=pl_typeedit;
	v->getsize=pl_getsizeedit;
	v->childspace=pl_childspaceedit;
	v->free=pl_freeedit;
	v->snarf=pl_snarfedit;
	v->paste=pl_pasteedit;
	v->kind="edit";
	ep->hit=hit;
	ep->minsize=minsize;
	ep->text=text;
	ep->ntext=ntext;
	if(ep->t!=0) twfree(ep->t);
	ep->t=0;
	ep->sel0=-1;
	ep->sel1=-1;
	v->scroll=pl_scrolledit;
	v->scr.pos=Pt(0,0);
	v->scr.size=Pt(ntext,0);
}
Panel *pledit(Panel *parent, int flags, Point minsize, Rune *text, int ntext, void (*hit)(Panel *)){
	Panel *v;
	v=pl_newpanel(parent, sizeof(Edit));
	((Edit *)v->data)->t=0;
	plinitedit(v, flags, minsize, text, ntext, hit);
	return v;
}
void plescroll(Panel *p, int top){
	twscroll(((Edit *)p->data)->t, top);
}
void plegetsel(Panel *p, int *sel0, int *sel1){
	Edit *ep;
	ep=p->data;
	*sel0=ep->sel0;
	*sel1=ep->sel1;
}
int plelen(Panel *p){
	Textwin *t;
	t=((Edit *)p->data)->t;
	if(t==0) return 0;
	return t->etext-t->text;
}
Rune *pleget(Panel *p){
	return ((Edit *)p->data)->t->text;
}
void plesel(Panel *p, int sel0, int sel1){
	Edit *ep;
	ep=p->data;
	ep->t->b=p->b;
	twhilite(ep->t, ep->sel0, ep->sel1, 0);
	ep->sel0=sel0;
	ep->sel1=sel1;
	twhilite(ep->t, ep->sel0, ep->sel1, 1);
}
void plepaste(Panel *p, Rune *text, int ntext){
	Edit *ep;
	ep=p->data;
	ep->t->b=p->b;
	twhilite(ep->t, ep->sel0, ep->sel1, 0);
	twreplace(ep->t, ep->sel0, ep->sel1, text, ntext);
	ep->sel1=ep->sel0+ntext;
	twhilite(ep->t, ep->sel0, ep->sel1, 1);
	p->scr.size.y=ep->t->etext-ep->t->text;
	p->scr.pos.y=ep->t->top;
}
void plemove(Panel *p, Point d){
	Edit *ep;
	ep=p->data;
	if(ep->t && !eqpt(d, Pt(0,0))) twmove(ep->t, d);
}