13 bxscan(Frame *f, Rune *sp, Rune *ep, Point *ppt)
15 int w, c, nb, delta, nl, nr, rw;
17 char *s, tmp[TMPSIZE+3]; /* +3 for rune overflow */
23 frame.maxtab = f->maxtab;
26 memmove(frame.cols, f->cols, sizeof frame.cols);
29 for(nb=0; sp<ep && nl<=f->maxlines; nb++,frame.nbox++){
30 if(nb == frame.nalloc){
31 _frgrowbox(&frame, delta);
37 if(c=='\t' || c=='\n'){
40 b->minwid = (c=='\n')? 0 : stringwidth(frame.font, " ");
52 if(c=='\t' || c=='\n')
54 rw = runetochar(s, sp);
55 if(s+rw >= tmp+TMPSIZE)
57 w += runestringnwidth(frame.font, sp, 1);
63 p = _frallocstr(f, s-tmp);
66 memmove(p, tmp, s-tmp);
72 _frcklinewrap0(f, ppt, &frame.box[0]);
73 return _frdraw(&frame, *ppt);
78 chopframe(Frame *f, Point pt, ulong p, int bn)
82 for(b = &f->box[bn]; ; b++){
83 if(b >= &f->box[f->nbox])
84 drawerror(f->display, "endofframe");
85 _frcklinewrap(f, &pt, b);
86 if(pt.y >= f->r.max.y)
89 _fradvance(f, &pt, b);
92 f->nlines = f->maxlines;
93 if(b<&f->box[f->nbox]) /* BUG */
94 _frdelbox(f, (int)(b-f->box), f->nbox-1);
98 frinsert(Frame *f, Rune *sp, Rune *ep, ulong p0)
100 Point pt0, pt1, opt0, ppt0, ppt1, pt;
112 if(p0>f->nchars || sp==ep || f->b==nil)
114 n0 = _frfindbox(f, 0, 0, p0);
117 pt0 = _frptofcharnb(f, p0, n0);
120 pt1 = bxscan(f, sp, ep, &ppt0);
123 _frcklinewrap(f, &pt0, b = &f->box[n0]); /* for frdrawsel() */
124 _frcklinewrap0(f, &ppt1, b);
128 * ppt0 and ppt1 are start and end of insertion as they will appear when
129 * insertion is complete. pt0 is current location of insertion position
130 * (p0); pt1 is terminal point (without line wrap) of insertion.
133 frtick(f, frptofchar(f, f->p0), 0);
136 * Find point where old and new x's line up
138 * pt0 is where the next box (b, n0) is now
139 * pt1 is where it will be after the insertion
140 * If pt1 goes off the rectangle, we can toss everything from there on
142 for(b = &f->box[n0],npts=0;
143 pt1.x!=pt0.x && pt1.y!=f->r.max.y && n0<f->nbox; b++,n0++,npts++){
144 _frcklinewrap(f, &pt0, b);
145 _frcklinewrap0(f, &pt1, b);
147 n = _frcanfit(f, pt1, b);
149 drawerror(f->display, "_frcanfit==0");
151 _frsplitbox(f, n0, n);
156 pts = realloc(pts, (npts+DELTA)*sizeof(pts[0]));
162 /* has a text box overflowed off the frame? */
163 if(pt1.y == f->r.max.y)
165 _fradvance(f, &pt0, b);
166 pt1.x += _frnewwid(f, pt1, b);
169 if(pt1.y > f->r.max.y)
170 drawerror(f->display, "frinsert pt1 too far");
171 if(pt1.y==f->r.max.y && n0<f->nbox){
172 f->nchars -= _frstrlen(f, n0);
173 _frdelbox(f, n0, f->nbox-1);
176 f->nlines = (pt1.y-f->r.min.y)/f->font->height+(pt1.x>f->r.min.x);
177 else if(pt1.y!=pt0.y){
181 q0 = pt0.y+f->font->height;
182 q1 = pt1.y+f->font->height;
183 f->nlines += (q1-q0)/f->font->height;
184 if(f->nlines > f->maxlines)
185 chopframe(f, ppt1, p0, nn0);
191 draw(f->b, r, f->b, nil, Pt(f->r.min.x, q0));
193 r.max.x = pt1.x+(f->r.max.x-pt0.x);
195 draw(f->b, r, f->b, nil, pt0);
199 * Move the old stuff down to make room. The loop will move the stuff
200 * between the insertion and the point where the x's lined up.
201 * The draw()s above moved everything down after the point they lined up.
203 for((y=pt1.y==f->r.max.y?pt1.y:0),b = &f->box[n0-1]; --npts>=0; --b){
209 r.max.y += f->font->height;
210 draw(f->b, r, f->b, nil, pts[npts].pt0);
211 /* clear bit hanging off right */
212 if(npts==0 && pt.y>pt0.y){
214 * first new char is bigger than first char we're
215 * displacing, causing line wrap. ugly special case.
219 r.max.x = f->r.max.x;
220 r.max.y += f->font->height;
221 if(f->p0<=cn0 && cn0<f->p1) /* b+1 is inside selection */
225 draw(f->b, r, col, nil, r.min);
230 r.max.x = f->r.max.x;
231 r.max.y += f->font->height;
232 if(f->p0<=cn0 && cn0<f->p1) /* b+1 is inside selection */
236 draw(f->b, r, col, nil, r.min);
244 r.max.y += f->font->height;
245 if(r.max.x >= f->r.max.x)
246 r.max.x = f->r.max.x;
248 if(f->p0<=cn0 && cn0<f->p1){ /* b is inside selection */
250 tcol = f->cols[HTEXT];
253 tcol = f->cols[TEXT];
255 draw(f->b, r, col, nil, r.min);
257 if(pt.x == f->r.min.x)
261 /* insertion can extend the selection, so the condition here is different */
262 if(f->p0<p0 && p0<=f->p1){
264 tcol = f->cols[HTEXT];
267 tcol = f->cols[TEXT];
269 frselectpaint(f, ppt0, ppt1, col);
270 _frdrawtext(&frame, ppt0, tcol, col);
271 _fraddbox(f, nn0, frame.nbox);
272 for(n=0; n<frame.nbox; n++)
273 f->box[nn0+n] = frame.box[n];
274 if(nn0>0 && f->box[nn0-1].nrune>=0 && ppt0.x-f->box[nn0-1].wid>=f->r.min.x){
276 ppt0.x -= f->box[nn0].wid;
279 _frclean(f, ppt0, nn0, n0<f->nbox-1? n0+1 : n0);
280 f->nchars += frame.nchars;
282 f->p0 += frame.nchars;
283 if(f->p0 > f->nchars)
286 f->p1 += frame.nchars;
287 if(f->p1 > f->nchars)
290 frtick(f, frptofchar(f, f->p0), 1);