xref: /plan9-contrib/sys/src/libframe/frinsert.c (revision 219b2ee8daee37f4aad58d63f21287faa8e4ffdc)
1 #include <u.h>
2 #include <libc.h>
3 #include <libg.h>
4 #include <frame.h>
5 
6 #define	DELTA	25
7 #define	TMPSIZE	256
8 static Frame		frame;
9 
10 static
11 Point
12 bxscan(Frame *f, Rune *sp, Rune *ep, Point *ppt)
13 {
14 	int w, c, nb, delta, nl, nr, rw;
15 	Frbox *b;
16 	char *s, tmp[TMPSIZE+3];	/* +3 for rune overflow */
17 	uchar *p;
18 
19 	frame.r = f->r;
20 	frame.b = f->b;
21 	frame.font = f->font;
22 	frame.maxtab = f->maxtab;
23 	frame.left = f->left;
24 	frame.nbox = 0;
25 	frame.nchars = 0;
26 	delta = DELTA;
27 	nl = 0;
28 	for(nb=0; sp<ep && nl<=f->maxlines; nb++,frame.nbox++){
29 		if(nb == frame.nalloc){
30 			_frgrowbox(&frame, delta);
31 			if(delta < 10000)
32 				delta *= 2;
33 		}
34 		b = &frame.box[nb];
35 		c = *sp;
36 		if(c=='\t' || c=='\n'){
37 			b->bc = c;
38 			b->wid = 5000;
39 			b->minwid = (c=='\n')? 0 : charwidth(frame.font, ' ');
40 			b->nrune = -1;
41 			if(c=='\n')
42 				nl++;
43 			frame.nchars++;
44 			sp++;
45 		}else{
46 			s = tmp;
47 			nr = 0;
48 			w = 0;
49 			while(sp < ep){
50 				c = *sp;
51 				if(c=='\t' || c=='\n')
52 					break;
53 				rw = runetochar(s, sp);
54 				if(s+rw >= tmp+TMPSIZE)
55 					break;
56 				w += charwidth(frame.font, c);
57 				sp++;
58 				s += rw;
59 				nr++;
60 			}
61 			*s++ = 0;
62 			p = _frallocstr(s-tmp);
63 			b = &frame.box[nb];
64 			b->ptr = p;
65 			memmove(p, tmp, s-tmp);
66 			b->wid = w;
67 			b->nrune = nr;
68 			frame.nchars += nr;
69 		}
70 	}
71 	_frcklinewrap0(f, ppt, &frame.box[0]);
72 	return _frdraw(&frame, *ppt);
73 }
74 
75 static
76 void
77 chopframe(Frame *f, Point pt, ulong p, int bn)
78 {
79 	Frbox *b;
80 
81 	for(b = &f->box[bn]; ; b++){
82 		if(b >= &f->box[f->nbox])
83 			berror("endofframe");
84 		_frcklinewrap(f, &pt, b);
85 		if(pt.y >= f->r.max.y)
86 			break;
87 		p += NRUNE(b);
88 		_fradvance(f, &pt, b);
89 	}
90 	f->nchars = p;
91 	f->nlines = f->maxlines;
92 	if(b<&f->box[f->nbox])				/* BUG */
93 		_frdelbox(f, (int)(b-f->box), f->nbox-1);
94 }
95 
96 void
97 frinsert(Frame *f, Rune *sp, Rune *ep, ulong p0)
98 {
99 	Point pt0, pt1, ppt0, ppt1, pt;
100 	Frbox *b;
101 	int n, n0, nn0, y;
102 	Rectangle r;
103 	static struct{
104 		Point pt0, pt1;
105 	}*pts;
106 	static int nalloc=0;
107 	int npts;
108 
109 	if(p0>f->nchars || sp==ep || f->b==0)
110 		return;
111 	n0 = _frfindbox(f, 0, 0, p0);
112 	nn0 = n0;
113 	pt0 = _frptofcharnb(f, p0, n0);
114 	ppt0 = pt0;
115 	pt1 = bxscan(f, sp, ep, &ppt0);
116 	ppt1 = pt1;
117 	if(n0 < f->nbox){
118 		_frcklinewrap(f, &pt0, b = &f->box[n0]);	/* for frselectf() */
119 		_frcklinewrap0(f, &ppt1, b);
120 	}
121 	f->modified = 1;
122 	/*
123 	 * ppt0 and ppt1 are start and end of insertion as they will appear when
124 	 * insertion is complete. pt0 is current location of insertion position
125 	 * (p0); pt1 is terminal point (without line wrap) of insertion.
126 	 */
127 	if(p0==f->p0 && p0==f->p1)		/* quite likely */
128 		frselectf(f, pt0, pt0, F&~D);
129 	else
130 		frselectp(f, F&~D);
131 	/*
132 	 * Find point where old and new x's line up
133 	 * Invariants:
134 	 *	pt0 is where the next box (b, n0) is now
135 	 *	pt1 is where it will be after then insertion
136 	 * If pt1 goes off the rectangle, we can toss everything from there on
137 	 */
138 	for(b = &f->box[n0],npts=0;
139 	     pt1.x!=pt0.x && pt1.y!=f->r.max.y && n0<f->nbox; b++,n0++,npts++){
140 		_frcklinewrap(f, &pt0, b);
141 		_frcklinewrap0(f, &pt1, b);
142 		if(b->nrune > 0){
143 			n = _frcanfit(f, pt1, b);
144 			if(n == 0)
145 				berror("_frcanfit==0");
146 			if(n != b->nrune){
147 				_frsplitbox(f, n0, n);
148 				b = &f->box[n0];
149 			}
150 		}
151 		if(npts == nalloc){
152 			pts = realloc(pts, (npts+DELTA)*sizeof(pts[0]));
153 			nalloc += DELTA;
154 			b = &f->box[n0];
155 		}
156 		pts[npts].pt0 = pt0;
157 		pts[npts].pt1 = pt1;
158 		/* has a text box overflowed off the frame? */
159 		if(pt1.y == f->r.max.y)
160 			break;
161 		_fradvance(f, &pt0, b);
162 		pt1.x += _frnewwid(f, pt1, b);
163 	}
164 	if(pt1.y > f->r.max.y)
165 		berror("frinsert pt1 too far");
166 	if(pt1.y==f->r.max.y && n0<f->nbox){
167 		f->nchars -= _frstrlen(f, n0);
168 		_frdelbox(f, n0, f->nbox-1);
169 	}
170 	if(n0 == f->nbox)
171 		f->nlines = (pt1.y-f->r.min.y)/f->font->height+(pt1.x>f->left);
172 	else if(pt1.y!=pt0.y){
173 		int q0, q1;
174 
175 		y = f->r.max.y;
176 		q0 = pt0.y+f->font->height;
177 		q1 = pt1.y+f->font->height;
178 		f->nlines += (q1-q0)/f->font->height;
179 		if(f->nlines > f->maxlines)
180 			chopframe(f, ppt1, p0, nn0);
181 		if(pt1.y < y){
182 			r = f->r;
183 			r.min.y = q0;
184 			r.max.y = y-(q1-q0);
185 			if(q1 < y)
186 				bitblt(f->b, Pt(f->r.min.x, q1), f->b, r, S);
187 			r.min = pt0;
188 			r.max.y = q0;
189 			bitblt(f->b, pt1, f->b, r, S);
190 		}
191 	}
192 	/*
193 	 * Move the old stuff down to make room.  The loop will move the stuff
194 	 * between the insertion and the point where the x's lined up.
195 	 * The bitblts above moved everything down after the point they lined up.
196 	 */
197 	for((y=pt1.y==f->r.max.y?pt1.y:0),b = &f->box[n0-1]; --npts>=0; --b){
198 		pt = pts[npts].pt1;
199 		if(b->nrune > 0){
200 			r.min = pts[npts].pt0;
201 			r.max = r.min;
202 			r.max.x += b->wid;
203 			r.max.y += f->font->height;
204 			bitblt(f->b, pt, f->b, r, S);
205 			if(pt.y < y){	/* clear bit hanging off right */
206 				r.min = pt;
207 				r.max = pt;
208 				r.min.x += b->wid;
209 				r.max.x = f->r.max.x;
210 				r.max.y += f->font->height;
211 				bitblt(f->b, r.min, f->b, r, 0);
212 			}
213 			y = pt.y;
214 		}else{
215 			r.min = pt;
216 			r.max = pt;
217 			r.max.x += b->wid;
218 			r.max.y += f->font->height;
219 			if(r.max.x >= f->r.max.x)
220 				r.max.x = f->r.max.x;
221 			bitblt(f->b, r.min, f->b, r, 0);
222 			y = (pt.x == f->left)? pt.y : 0;
223 		}
224 	}
225 	frselectf(f, ppt0, ppt1, 0);
226 	_frredraw(&frame, ppt0);
227 	_fraddbox(f, nn0, frame.nbox);
228 	for(n=0; n<frame.nbox; n++)
229 		f->box[nn0+n] = frame.box[n];
230 	if(nn0>0 && f->box[nn0-1].nrune>=0 && ppt0.x-f->box[nn0-1].wid>=(int)f->left){
231 		--nn0;
232 		ppt0.x -= f->box[nn0].wid;
233 	}
234 	n0 += frame.nbox;
235 	_frclean(f, ppt0, nn0, n0<f->nbox-1? n0+1 : n0);
236 	f->nchars += frame.nchars;
237 	if(f->p0 >= p0)
238 		f->p0 += frame.nchars;
239 	if(f->p0 > f->nchars)
240 		f->p0 = f->nchars;
241 	if(f->p1 >= p0)
242 		f->p1 += frame.nchars;
243 	if(f->p1 > f->nchars)
244 		f->p1 = f->nchars;
245 	frselectp(f, F&~D);
246 }
247