summaryrefslogtreecommitdiff
path: root/sys/src/libframe
diff options
context:
space:
mode:
authorTaru Karttunen <taruti@taruti.net>2011-03-30 15:46:40 +0300
committerTaru Karttunen <taruti@taruti.net>2011-03-30 15:46:40 +0300
commite5888a1ffdae813d7575f5fb02275c6bb07e5199 (patch)
treed8d51eac403f07814b9e936eed0c9a79195e2450 /sys/src/libframe
Import sources from 2011-03-30 iso image
Diffstat (limited to 'sys/src/libframe')
-rwxr-xr-xsys/src/libframe/frbox.c160
-rwxr-xr-xsys/src/libframe/frdelete.c123
-rwxr-xr-xsys/src/libframe/frdraw.c204
-rwxr-xr-xsys/src/libframe/frinit.c84
-rwxr-xr-xsys/src/libframe/frinsert.c286
-rwxr-xr-xsys/src/libframe/frptofchar.c116
-rwxr-xr-xsys/src/libframe/frselect.c133
-rwxr-xr-xsys/src/libframe/frstr.c38
-rwxr-xr-xsys/src/libframe/frutil.c112
-rwxr-xr-xsys/src/libframe/mkfile21
10 files changed, 1277 insertions, 0 deletions
diff --git a/sys/src/libframe/frbox.c b/sys/src/libframe/frbox.c
new file mode 100755
index 000000000..d501ed932
--- /dev/null
+++ b/sys/src/libframe/frbox.c
@@ -0,0 +1,160 @@
+#include <u.h>
+#include <libc.h>
+#include <draw.h>
+#include <thread.h>
+#include <mouse.h>
+#include <frame.h>
+
+#define SLOP 25
+
+void
+_fraddbox(Frame *f, int bn, int n) /* add n boxes after bn, shift the rest up,
+ * box[bn+n]==box[bn] */
+{
+ int i;
+
+ if(bn > f->nbox)
+ drawerror(f->display, "_fraddbox");
+ if(f->nbox+n > f->nalloc)
+ _frgrowbox(f, n+SLOP);
+ for(i=f->nbox; --i>=bn; )
+ f->box[i+n] = f->box[i];
+ f->nbox+=n;
+}
+
+void
+_frclosebox(Frame *f, int n0, int n1) /* inclusive */
+{
+ int i;
+
+ if(n0>=f->nbox || n1>=f->nbox || n1<n0)
+ drawerror(f->display, "_frclosebox");
+ n1++;
+ for(i=n1; i<f->nbox; i++)
+ f->box[i-(n1-n0)] = f->box[i];
+ f->nbox -= n1-n0;
+}
+
+void
+_frdelbox(Frame *f, int n0, int n1) /* inclusive */
+{
+ if(n0>=f->nbox || n1>=f->nbox || n1<n0)
+ drawerror(f->display, "_frdelbox");
+ _frfreebox(f, n0, n1);
+ _frclosebox(f, n0, n1);
+}
+
+void
+_frfreebox(Frame *f, int n0, int n1) /* inclusive */
+{
+ int i;
+
+ if(n1<n0)
+ return;
+ if(n0>=f->nbox || n1>=f->nbox)
+ drawerror(f->display, "_frfreebox");
+ n1++;
+ for(i=n0; i<n1; i++)
+ if(f->box[i].nrune >= 0)
+ free(f->box[i].ptr);
+}
+
+void
+_frgrowbox(Frame *f, int delta)
+{
+ f->nalloc += delta;
+ f->box = realloc(f->box, f->nalloc*sizeof(Frbox));
+ if(f->box == 0)
+ drawerror(f->display, "_frgrowbox");
+}
+
+static
+void
+dupbox(Frame *f, int bn)
+{
+ uchar *p;
+
+ if(f->box[bn].nrune < 0)
+ drawerror(f->display, "dupbox");
+ _fraddbox(f, bn, 1);
+ if(f->box[bn].nrune >= 0){
+ p = _frallocstr(f, NBYTE(&f->box[bn])+1);
+ strcpy((char*)p, (char*)f->box[bn].ptr);
+ f->box[bn+1].ptr = p;
+ }
+}
+
+static
+uchar*
+runeindex(uchar *p, int n)
+{
+ int i, w;
+ Rune rune;
+
+ for(i=0; i<n; i++,p+=w)
+ if(*p < Runeself)
+ w = 1;
+ else{
+ w = chartorune(&rune, (char*)p);
+ USED(rune);
+ }
+ return p;
+}
+
+static
+void
+truncatebox(Frame *f, Frbox *b, int n) /* drop last n chars; no allocation done */
+{
+ if(b->nrune<0 || b->nrune<n)
+ drawerror(f->display, "truncatebox");
+ b->nrune -= n;
+ runeindex(b->ptr, b->nrune)[0] = 0;
+ b->wid = stringwidth(f->font, (char *)b->ptr);
+}
+
+static
+void
+chopbox(Frame *f, Frbox *b, int n) /* drop first n chars; no allocation done */
+{
+ char *p;
+
+ if(b->nrune<0 || b->nrune<n)
+ drawerror(f->display, "chopbox");
+ p = (char*)runeindex(b->ptr, n);
+ memmove((char*)b->ptr, p, strlen(p)+1);
+ b->nrune -= n;
+ b->wid = stringwidth(f->font, (char *)b->ptr);
+}
+
+void
+_frsplitbox(Frame *f, int bn, int n)
+{
+ dupbox(f, bn);
+ truncatebox(f, &f->box[bn], f->box[bn].nrune-n);
+ chopbox(f, &f->box[bn+1], n);
+}
+
+void
+_frmergebox(Frame *f, int bn) /* merge bn and bn+1 */
+{
+ Frbox *b;
+
+ b = &f->box[bn];
+ _frinsure(f, bn, NBYTE(&b[0])+NBYTE(&b[1])+1);
+ strcpy((char*)runeindex(b[0].ptr, b[0].nrune), (char*)b[1].ptr);
+ b[0].wid += b[1].wid;
+ b[0].nrune += b[1].nrune;
+ _frdelbox(f, bn+1, bn+1);
+}
+
+int
+_frfindbox(Frame *f, int bn, ulong p, ulong q) /* find box containing q and put q on a box boundary */
+{
+ Frbox *b;
+
+ for(b = &f->box[bn]; bn<f->nbox && p+NRUNE(b)<=q; bn++, b++)
+ p += NRUNE(b);
+ if(p != q)
+ _frsplitbox(f, bn++, (int)(q-p));
+ return bn;
+}
diff --git a/sys/src/libframe/frdelete.c b/sys/src/libframe/frdelete.c
new file mode 100755
index 000000000..c8c9b6b3b
--- /dev/null
+++ b/sys/src/libframe/frdelete.c
@@ -0,0 +1,123 @@
+#include <u.h>
+#include <libc.h>
+#include <draw.h>
+#include <thread.h>
+#include <mouse.h>
+#include <frame.h>
+
+int
+frdelete(Frame *f, ulong p0, ulong p1)
+{
+ Point pt0, pt1, ppt0;
+ Frbox *b;
+ int n0, n1, n;
+ ulong cn1;
+ Rectangle r;
+ int nn0;
+ Image *col;
+
+ if(p0>=f->nchars || p0==p1 || f->b==nil)
+ return 0;
+ if(p1 > f->nchars)
+ p1 = f->nchars;
+ n0 = _frfindbox(f, 0, 0, p0);
+ if(n0 == f->nbox)
+ drawerror(f->display, "off end in frdelete");
+ n1 = _frfindbox(f, n0, p0, p1);
+ pt0 = _frptofcharnb(f, p0, n0);
+ pt1 = frptofchar(f, p1);
+ if(f->p0 == f->p1)
+ frtick(f, frptofchar(f, f->p0), 0);
+ nn0 = n0;
+ ppt0 = pt0;
+ _frfreebox(f, n0, n1-1);
+ f->modified = 1;
+
+ /*
+ * Invariants:
+ * - pt0 points to beginning, pt1 points to end
+ * - n0 is box containing beginning of stuff being deleted
+ * - n1, b are box containing beginning of stuff to be kept after deletion
+ * - cn1 is char position of n1
+ * - f->p0 and f->p1 are not adjusted until after all deletion is done
+ */
+ b = &f->box[n1];
+ cn1 = p1;
+ while(pt1.x!=pt0.x && n1<f->nbox){
+ _frcklinewrap0(f, &pt0, b);
+ _frcklinewrap(f, &pt1, b);
+ n = _frcanfit(f, pt0, b);
+ if(n==0)
+ drawerror(f->display, "_frcanfit==0");
+ r.min = pt0;
+ r.max = pt0;
+ r.max.y += f->font->height;
+ if(b->nrune > 0){
+ if(n != b->nrune){
+ _frsplitbox(f, n1, n);
+ b = &f->box[n1];
+ }
+ r.max.x += b->wid;
+ draw(f->b, r, f->b, nil, pt1);
+ cn1 += b->nrune;
+ }else{
+ r.max.x += _frnewwid0(f, pt0, b);
+ if(r.max.x > f->r.max.x)
+ r.max.x = f->r.max.x;
+ col = f->cols[BACK];
+ if(f->p0<=cn1 && cn1<f->p1)
+ col = f->cols[HIGH];
+ draw(f->b, r, col, nil, pt0);
+ cn1++;
+ }
+ _fradvance(f, &pt1, b);
+ pt0.x += _frnewwid(f, pt0, b);
+ f->box[n0++] = f->box[n1++];
+ b++;
+ }
+ if(n1==f->nbox && pt0.x!=pt1.x) /* deleting last thing in window; must clean up */
+ frselectpaint(f, pt0, pt1, f->cols[BACK]);
+ if(pt1.y != pt0.y){
+ Point pt2;
+
+ pt2 = _frptofcharptb(f, 32767, pt1, n1);
+ if(pt2.y > f->r.max.y)
+ drawerror(f->display, "frptofchar in frdelete");
+ if(n1 < f->nbox){
+ int q0, q1, q2;
+
+ q0 = pt0.y+f->font->height;
+ q1 = pt1.y+f->font->height;
+ q2 = pt2.y+f->font->height;
+ if(q2 > f->r.max.y)
+ q2 = f->r.max.y;
+ draw(f->b, Rect(pt0.x, pt0.y, pt0.x+(f->r.max.x-pt1.x), q0),
+ f->b, nil, pt1);
+ draw(f->b, Rect(f->r.min.x, q0, f->r.max.x, q0+(q2-q1)),
+ f->b, nil, Pt(f->r.min.x, q1));
+ frselectpaint(f, Pt(pt2.x, pt2.y-(pt1.y-pt0.y)), pt2, f->cols[BACK]);
+ }else
+ frselectpaint(f, pt0, pt2, f->cols[BACK]);
+ }
+ _frclosebox(f, n0, n1-1);
+ if(nn0>0 && f->box[nn0-1].nrune>=0 && ppt0.x-f->box[nn0-1].wid>=(int)f->r.min.x){
+ --nn0;
+ ppt0.x -= f->box[nn0].wid;
+ }
+ _frclean(f, ppt0, nn0, n0<f->nbox-1? n0+1 : n0);
+ if(f->p1 > p1)
+ f->p1 -= p1-p0;
+ else if(f->p1 > p0)
+ f->p1 = p0;
+ if(f->p0 > p1)
+ f->p0 -= p1-p0;
+ else if(f->p0 > p0)
+ f->p0 = p0;
+ f->nchars -= p1-p0;
+ if(f->p0 == f->p1)
+ frtick(f, frptofchar(f, f->p0), 1);
+ pt0 = frptofchar(f, f->nchars);
+ n = f->nlines;
+ f->nlines = (pt0.y-f->r.min.y)/f->font->height+(pt0.x>f->r.min.x);
+ return n - f->nlines;
+}
diff --git a/sys/src/libframe/frdraw.c b/sys/src/libframe/frdraw.c
new file mode 100755
index 000000000..6a31a8383
--- /dev/null
+++ b/sys/src/libframe/frdraw.c
@@ -0,0 +1,204 @@
+#include <u.h>
+#include <libc.h>
+#include <draw.h>
+#include <thread.h>
+#include <mouse.h>
+#include <frame.h>
+
+void
+_frdrawtext(Frame *f, Point pt, Image *text, Image *back)
+{
+ Frbox *b;
+ int nb;
+ static int x;
+
+ for(nb=0,b=f->box; nb<f->nbox; nb++, b++){
+ _frcklinewrap(f, &pt, b);
+ if(b->nrune >= 0){
+ stringbg(f->b, pt, text, ZP, f->font, (char*)b->ptr, back, ZP);
+ }
+ pt.x += b->wid;
+ }
+}
+
+static int
+nbytes(char *s0, int nr)
+{
+ char *s;
+ Rune r;
+
+ s = s0;
+ while(--nr >= 0)
+ s += chartorune(&r, s);
+ return s-s0;
+}
+
+void
+frdrawsel(Frame *f, Point pt, ulong p0, ulong p1, int issel)
+{
+ Image *back, *text;
+
+ if(f->ticked)
+ frtick(f, frptofchar(f, f->p0), 0);
+
+ if(p0 == p1){
+ frtick(f, pt, issel);
+ return;
+ }
+
+ if(issel){
+ back = f->cols[HIGH];
+ text = f->cols[HTEXT];
+ }else{
+ back = f->cols[BACK];
+ text = f->cols[TEXT];
+ }
+
+ frdrawsel0(f, pt, p0, p1, back, text);
+}
+
+Point
+frdrawsel0(Frame *f, Point pt, ulong p0, ulong p1, Image *back, Image *text)
+{
+ Frbox *b;
+ int nb, nr, w, x, trim;
+ Point qt;
+ uint p;
+ char *ptr;
+
+ p = 0;
+ b = f->box;
+ trim = 0;
+ for(nb=0; nb<f->nbox && p<p1; nb++){
+ nr = b->nrune;
+ if(nr < 0)
+ nr = 1;
+ if(p+nr <= p0)
+ goto Continue;
+ if(p >= p0){
+ qt = pt;
+ _frcklinewrap(f, &pt, b);
+ /* fill in the end of a wrapped line */
+ if(pt.y > qt.y)
+ draw(f->b, Rect(qt.x, qt.y, f->r.max.x, pt.y), back, nil, qt);
+ }
+ ptr = (char*)b->ptr;
+ if(p < p0){ /* beginning of region: advance into box */
+ ptr += nbytes(ptr, p0-p);
+ nr -= (p0-p);
+ p = p0;
+ }
+ trim = 0;
+ if(p+nr > p1){ /* end of region: trim box */
+ nr -= (p+nr)-p1;
+ trim = 1;
+ }
+ if(b->nrune<0 || nr==b->nrune)
+ w = b->wid;
+ else
+ w = stringnwidth(f->font, ptr, nr);
+ x = pt.x+w;
+ if(x > f->r.max.x)
+ x = f->r.max.x;
+ draw(f->b, Rect(pt.x, pt.y, x, pt.y+f->font->height), back, nil, pt);
+ if(b->nrune >= 0)
+ stringnbg(f->b, pt, text, ZP, f->font, ptr, nr, back, ZP);
+ pt.x += w;
+ Continue:
+ b++;
+ p += nr;
+ }
+ /* if this is end of last plain text box on wrapped line, fill to end of line */
+ if(p1>p0 && b>f->box && b<f->box+f->nbox && b[-1].nrune>0 && !trim){
+ qt = pt;
+ _frcklinewrap(f, &pt, b);
+ if(pt.y > qt.y)
+ draw(f->b, Rect(qt.x, qt.y, f->r.max.x, pt.y), back, nil, qt);
+ }
+ return pt;
+}
+
+void
+frredraw(Frame *f)
+{
+ int ticked;
+ Point pt;
+
+ if(f->p0 == f->p1){
+ ticked = f->ticked;
+ if(ticked)
+ frtick(f, frptofchar(f, f->p0), 0);
+ frdrawsel0(f, frptofchar(f, 0), 0, f->nchars, f->cols[BACK], f->cols[TEXT]);
+ if(ticked)
+ frtick(f, frptofchar(f, f->p0), 1);
+ return;
+ }
+
+ pt = frptofchar(f, 0);
+ pt = frdrawsel0(f, pt, 0, f->p0, f->cols[BACK], f->cols[TEXT]);
+ pt = frdrawsel0(f, pt, f->p0, f->p1, f->cols[HIGH], f->cols[HTEXT]);
+ pt = frdrawsel0(f, pt, f->p1, f->nchars, f->cols[BACK], f->cols[TEXT]);
+}
+
+void
+frtick(Frame *f, Point pt, int ticked)
+{
+ Rectangle r;
+
+ if(f->ticked==ticked || f->tick==0 || !ptinrect(pt, f->r))
+ return;
+ pt.x--; /* looks best just left of where requested */
+ r = Rect(pt.x, pt.y, pt.x+FRTICKW, pt.y+f->font->height);
+ /* can go into left border but not right */
+ if(r.max.x > f->r.max.x)
+ r.max.x = f->r.max.x;
+ if(ticked){
+ draw(f->tickback, f->tickback->r, f->b, nil, pt);
+ draw(f->b, r, f->tick, nil, ZP);
+ }else
+ draw(f->b, r, f->tickback, nil, ZP);
+ f->ticked = ticked;
+}
+
+Point
+_frdraw(Frame *f, Point pt)
+{
+ Frbox *b;
+ int nb, n;
+
+ for(b=f->box,nb=0; nb<f->nbox; nb++, b++){
+ _frcklinewrap0(f, &pt, b);
+ if(pt.y == f->r.max.y){
+ f->nchars -= _frstrlen(f, nb);
+ _frdelbox(f, nb, f->nbox-1);
+ break;
+ }
+ if(b->nrune > 0){
+ n = _frcanfit(f, pt, b);
+ if(n == 0)
+ drawerror(f->display, "_frcanfit==0");
+ if(n != b->nrune){
+ _frsplitbox(f, nb, n);
+ b = &f->box[nb];
+ }
+ pt.x += b->wid;
+ }else{
+ if(b->bc == '\n'){
+ pt.x = f->r.min.x;
+ pt.y+=f->font->height;
+ }else
+ pt.x += _frnewwid(f, pt, b);
+ }
+ }
+ return pt;
+}
+
+int
+_frstrlen(Frame *f, int nb)
+{
+ int n;
+
+ for(n=0; nb<f->nbox; nb++)
+ n += NRUNE(&f->box[nb]);
+ return n;
+}
diff --git a/sys/src/libframe/frinit.c b/sys/src/libframe/frinit.c
new file mode 100755
index 000000000..e0dc41a03
--- /dev/null
+++ b/sys/src/libframe/frinit.c
@@ -0,0 +1,84 @@
+#include <u.h>
+#include <libc.h>
+#include <draw.h>
+#include <thread.h>
+#include <mouse.h>
+#include <frame.h>
+
+void
+frinit(Frame *f, Rectangle r, Font *ft, Image *b, Image *cols[NCOL])
+{
+ f->font = ft;
+ f->display = b->display;
+ f->maxtab = 8*stringwidth(ft, "0");
+ f->nbox = 0;
+ f->nalloc = 0;
+ f->nchars = 0;
+ f->nlines = 0;
+ f->p0 = 0;
+ f->p1 = 0;
+ f->box = 0;
+ f->lastlinefull = 0;
+ if(cols != 0)
+ memmove(f->cols, cols, sizeof f->cols);
+ frsetrects(f, r, b);
+ if(f->tick==nil && f->cols[BACK]!=0)
+ frinittick(f);
+}
+
+void
+frinittick(Frame *f)
+{
+ Image *b;
+ Font *ft;
+
+ b = f->display->screenimage;
+ ft = f->font;
+ if(f->tick)
+ freeimage(f->tick);
+ f->tick = allocimage(f->display, Rect(0, 0, FRTICKW, ft->height), b->chan, 0, DWhite);
+ if(f->tick == nil)
+ return;
+ if(f->tickback)
+ freeimage(f->tickback);
+ f->tickback = allocimage(f->display, f->tick->r, b->chan, 0, DWhite);
+ if(f->tickback == 0){
+ freeimage(f->tick);
+ f->tick = 0;
+ return;
+ }
+ /* background color */
+ draw(f->tick, f->tick->r, f->cols[BACK], nil, ZP);
+ /* vertical line */
+ draw(f->tick, Rect(FRTICKW/2, 0, FRTICKW/2+1, ft->height), f->display->black, nil, ZP);
+ /* box on each end */
+ draw(f->tick, Rect(0, 0, FRTICKW, FRTICKW), f->cols[TEXT], nil, ZP);
+ draw(f->tick, Rect(0, ft->height-FRTICKW, FRTICKW, ft->height), f->cols[TEXT], nil, ZP);
+}
+
+void
+frsetrects(Frame *f, Rectangle r, Image *b)
+{
+ f->b = b;
+ f->entire = r;
+ f->r = r;
+ f->r.max.y -= (r.max.y-r.min.y)%f->font->height;
+ f->maxlines = (r.max.y-r.min.y)/f->font->height;
+}
+
+void
+frclear(Frame *f, int freeall)
+{
+ if(f->nbox)
+ _frdelbox(f, 0, f->nbox-1);
+ if(f->box)
+ free(f->box);
+ if(freeall){
+ freeimage(f->tick);
+ freeimage(f->tickback);
+ f->tick = 0;
+ f->tickback = 0;
+ }
+ f->box = 0;
+ f->ticked = 0;
+}
diff --git a/sys/src/libframe/frinsert.c b/sys/src/libframe/frinsert.c
new file mode 100755
index 000000000..2c40955ba
--- /dev/null
+++ b/sys/src/libframe/frinsert.c
@@ -0,0 +1,286 @@
+#include <u.h>
+#include <libc.h>
+#include <draw.h>
+#include <thread.h>
+#include <mouse.h>
+#include <frame.h>
+
+#define DELTA 25
+#define TMPSIZE 256
+static Frame frame;
+
+static
+Point
+bxscan(Frame *f, Rune *sp, Rune *ep, Point *ppt)
+{
+ int w, c, nb, delta, nl, nr, rw;
+ Frbox *b;
+ char *s, tmp[TMPSIZE+3]; /* +3 for rune overflow */
+ uchar *p;
+
+ frame.r = f->r;
+ frame.b = f->b;
+ frame.font = f->font;
+ frame.maxtab = f->maxtab;
+ frame.nbox = 0;
+ frame.nchars = 0;
+ memmove(frame.cols, f->cols, sizeof frame.cols);
+ delta = DELTA;
+ nl = 0;
+ for(nb=0; sp<ep && nl<=f->maxlines; nb++,frame.nbox++){
+ if(nb == frame.nalloc){
+ _frgrowbox(&frame, delta);
+ if(delta < 10000)
+ delta *= 2;
+ }
+ b = &frame.box[nb];
+ c = *sp;
+ if(c=='\t' || c=='\n'){
+ b->bc = c;
+ b->wid = 5000;
+ b->minwid = (c=='\n')? 0 : stringwidth(frame.font, " ");
+ b->nrune = -1;
+ if(c=='\n')
+ nl++;
+ frame.nchars++;
+ sp++;
+ }else{
+ s = tmp;
+ nr = 0;
+ w = 0;
+ while(sp < ep){
+ c = *sp;
+ if(c=='\t' || c=='\n')
+ break;
+ rw = runetochar(s, sp);
+ if(s+rw >= tmp+TMPSIZE)
+ break;
+ w += runestringnwidth(frame.font, sp, 1);
+ sp++;
+ s += rw;
+ nr++;
+ }
+ *s++ = 0;
+ p = _frallocstr(f, s-tmp);
+ b = &frame.box[nb];
+ b->ptr = p;
+ memmove(p, tmp, s-tmp);
+ b->wid = w;
+ b->nrune = nr;
+ frame.nchars += nr;
+ }
+ }
+ _frcklinewrap0(f, ppt, &frame.box[0]);
+ return _frdraw(&frame, *ppt);
+}
+
+static
+void
+chopframe(Frame *f, Point pt, ulong p, int bn)
+{
+ Frbox *b;
+
+ for(b = &f->box[bn]; ; b++){
+ if(b >= &f->box[f->nbox])
+ drawerror(f->display, "endofframe");
+ _frcklinewrap(f, &pt, b);
+ if(pt.y >= f->r.max.y)
+ break;
+ p += NRUNE(b);
+ _fradvance(f, &pt, b);
+ }
+ f->nchars = p;
+ f->nlines = f->maxlines;
+ if(b<&f->box[f->nbox]) /* BUG */
+ _frdelbox(f, (int)(b-f->box), f->nbox-1);
+}
+
+void
+frinsert(Frame *f, Rune *sp, Rune *ep, ulong p0)
+{
+ Point pt0, pt1, opt0, ppt0, ppt1, pt;
+ Frbox *b;
+ int n, n0, nn0, y;
+ ulong cn0;
+ Image *col;
+ Rectangle r;
+ static struct{
+ Point pt0, pt1;
+ }*pts;
+ static int nalloc=0;
+ int npts;
+
+ if(p0>f->nchars || sp==ep || f->b==nil)
+ return;
+ n0 = _frfindbox(f, 0, 0, p0);
+ cn0 = p0;
+ nn0 = n0;
+ pt0 = _frptofcharnb(f, p0, n0);
+ ppt0 = pt0;
+ opt0 = pt0;
+ pt1 = bxscan(f, sp, ep, &ppt0);
+ ppt1 = pt1;
+ if(n0 < f->nbox){
+ _frcklinewrap(f, &pt0, b = &f->box[n0]); /* for frdrawsel() */
+ _frcklinewrap0(f, &ppt1, b);
+ }
+ f->modified = 1;
+ /*
+ * ppt0 and ppt1 are start and end of insertion as they will appear when
+ * insertion is complete. pt0 is current location of insertion position
+ * (p0); pt1 is terminal point (without line wrap) of insertion.
+ */
+ if(f->p0 == f->p1)
+ frtick(f, frptofchar(f, f->p0), 0);
+
+ /*
+ * Find point where old and new x's line up
+ * Invariants:
+ * pt0 is where the next box (b, n0) is now
+ * pt1 is where it will be after the insertion
+ * If pt1 goes off the rectangle, we can toss everything from there on
+ */
+ for(b = &f->box[n0],npts=0;
+ pt1.x!=pt0.x && pt1.y!=f->r.max.y && n0<f->nbox; b++,n0++,npts++){
+ _frcklinewrap(f, &pt0, b);
+ _frcklinewrap0(f, &pt1, b);
+ if(b->nrune > 0){
+ n = _frcanfit(f, pt1, b);
+ if(n == 0)
+ drawerror(f->display, "_frcanfit==0");
+ if(n != b->nrune){
+ _frsplitbox(f, n0, n);
+ b = &f->box[n0];
+ }
+ }
+ if(npts == nalloc){
+ pts = realloc(pts, (npts+DELTA)*sizeof(pts[0]));
+ nalloc += DELTA;
+ b = &f->box[n0];
+ }
+ pts[npts].pt0 = pt0;
+ pts[npts].pt1 = pt1;
+ /* has a text box overflowed off the frame? */
+ if(pt1.y == f->r.max.y)
+ break;
+ _fradvance(f, &pt0, b);
+ pt1.x += _frnewwid(f, pt1, b);
+ cn0 += NRUNE(b);
+ }
+ if(pt1.y > f->r.max.y)
+ drawerror(f->display, "frinsert pt1 too far");
+ if(pt1.y==f->r.max.y && n0<f->nbox){
+ f->nchars -= _frstrlen(f, n0);
+ _frdelbox(f, n0, f->nbox-1);
+ }
+ if(n0 == f->nbox)
+ f->nlines = (pt1.y-f->r.min.y)/f->font->height+(pt1.x>f->r.min.x);
+ else if(pt1.y!=pt0.y){
+ int q0, q1;
+
+ y = f->r.max.y;
+ q0 = pt0.y+f->font->height;
+ q1 = pt1.y+f->font->height;
+ f->nlines += (q1-q0)/f->font->height;
+ if(f->nlines > f->maxlines)
+ chopframe(f, ppt1, p0, nn0);
+ if(pt1.y < y){
+ r = f->r;
+ r.min.y = q1;
+ r.max.y = y;
+ if(q1 < y)
+ draw(f->b, r, f->b, nil, Pt(f->r.min.x, q0));
+ r.min = pt1;
+ r.max.x = pt1.x+(f->r.max.x-pt0.x);
+ r.max.y = q1;
+ draw(f->b, r, f->b, nil, pt0);
+ }
+ }
+ /*
+ * Move the old stuff down to make room. The loop will move the stuff
+ * between the insertion and the point where the x's lined up.
+ * The draw()s above moved everything down after the point they lined up.
+ */
+ for((y=pt1.y==f->r.max.y?pt1.y:0),b = &f->box[n0-1]; --npts>=0; --b){
+ pt = pts[npts].pt1;
+ if(b->nrune > 0){
+ r.min = pt;
+ r.max = r.min;
+ r.max.x += b->wid;
+ r.max.y += f->font->height;
+ draw(f->b, r, f->b, nil, pts[npts].pt0);
+ /* clear bit hanging off right */
+ if(npts==0 && pt.y>pt0.y){
+ /*
+ * first new char is bigger than first char we're
+ * displacing, causing line wrap. ugly special case.
+ */
+ r.min = opt0;
+ r.max = opt0;
+ r.max.x = f->r.max.x;
+ r.max.y += f->font->height;
+ if(f->p0<=cn0 && cn0<f->p1) /* b+1 is inside selection */
+ col = f->cols[HIGH];
+ else
+ col = f->cols[BACK];
+ draw(f->b, r, col, nil, r.min);
+ }else if(pt.y < y){
+ r.min = pt;
+ r.max = pt;
+ r.min.x += b->wid;
+ r.max.x = f->r.max.x;
+ r.max.y += f->font->height;
+ if(f->p0<=cn0 && cn0<f->p1) /* b+1 is inside selection */
+ col = f->cols[HIGH];
+ else
+ col = f->cols[BACK];
+ draw(f->b, r, col, nil, r.min);
+ }
+ y = pt.y;
+ cn0 -= b->nrune;
+ }else{
+ r.min = pt;
+ r.max = pt;
+ r.max.x += b->wid;
+ r.max.y += f->font->height;
+ if(r.max.x >= f->r.max.x)
+ r.max.x = f->r.max.x;
+ cn0--;
+ if(f->p0<=cn0 && cn0<f->p1) /* b is inside selection */
+ col = f->cols[HIGH];
+ else
+ col = f->cols[BACK];
+ draw(f->b, r, col, nil, r.min);
+ y = 0;
+ if(pt.x == f->r.min.x)
+ y = pt.y;
+ }
+ }
+ /* insertion can extend the selection, so the condition here is different */
+ if(f->p0<p0 && p0<=f->p1)
+ col = f->cols[HIGH];
+ else
+ col = f->cols[BACK];
+ frselectpaint(f, ppt0, ppt1, col);
+ _frdrawtext(&frame, ppt0, f->cols[TEXT], col);
+ _fraddbox(f, nn0, frame.nbox);
+ for(n=0; n<frame.nbox; n++)
+ f->box[nn0+n] = frame.box[n];
+ if(nn0>0 && f->box[nn0-1].nrune>=0 && ppt0.x-f->box[nn0-1].wid>=f->r.min.x){
+ --nn0;
+ ppt0.x -= f->box[nn0].wid;
+ }
+ n0 += frame.nbox;
+ _frclean(f, ppt0, nn0, n0<f->nbox-1? n0+1 : n0);
+ f->nchars += frame.nchars;
+ if(f->p0 >= p0)
+ f->p0 += frame.nchars;
+ if(f->p0 > f->nchars)
+ f->p0 = f->nchars;
+ if(f->p1 >= p0)
+ f->p1 += frame.nchars;
+ if(f->p1 > f->nchars)
+ f->p1 = f->nchars;
+ if(f->p0 == f->p1)
+ frtick(f, frptofchar(f, f->p0), 1);
+}
diff --git a/sys/src/libframe/frptofchar.c b/sys/src/libframe/frptofchar.c
new file mode 100755
index 000000000..3d1b9f57c
--- /dev/null
+++ b/sys/src/libframe/frptofchar.c
@@ -0,0 +1,116 @@
+#include <u.h>
+#include <libc.h>
+#include <draw.h>
+#include <thread.h>
+#include <mouse.h>
+#include <frame.h>
+
+Point
+_frptofcharptb(Frame *f, ulong p, Point pt, int bn)
+{
+ uchar *s;
+ Frbox *b;
+ int w, l;
+ Rune r;
+
+ for(b = &f->box[bn]; bn<f->nbox; bn++,b++){
+ _frcklinewrap(f, &pt, b);
+ if(p < (l=NRUNE(b))){
+ if(b->nrune > 0)
+ for(s=b->ptr; p>0; s+=w, p--){
+ if((r = *s) < Runeself)
+ w = 1;
+ else
+ w = chartorune(&r, (char*)s);
+ pt.x += stringnwidth(f->font, (char*)s, 1);
+ if(r==0 || pt.x>f->r.max.x)
+ drawerror(f->display, "frptofchar");
+ }
+ break;
+ }
+ p -= l;
+ _fradvance(f, &pt, b);
+ }
+ return pt;
+}
+
+Point
+frptofchar(Frame *f, ulong p)
+{
+ return _frptofcharptb(f, p, f->r.min, 0);
+}
+
+Point
+_frptofcharnb(Frame *f, ulong p, int nb) /* doesn't do final _fradvance to next line */
+{
+ Point pt;
+ int nbox;
+
+ nbox = f->nbox;
+ f->nbox = nb;
+ pt = _frptofcharptb(f, p, f->r.min, 0);
+ f->nbox = nbox;
+ return pt;
+}
+
+static
+Point
+_frgrid(Frame *f, Point p)
+{
+ p.y -= f->r.min.y;
+ p.y -= p.y%f->font->height;
+ p.y += f->r.min.y;
+ if(p.x > f->r.max.x)
+ p.x = f->r.max.x;
+ return p;
+}
+
+ulong
+frcharofpt(Frame *f, Point pt)
+{
+ Point qt;
+ int w, bn;
+ uchar *s;
+ Frbox *b;
+ ulong p;
+ Rune r;
+
+ pt = _frgrid(f, pt);
+ qt = f->r.min;
+ for(b=f->box,bn=0,p=0; bn<f->nbox && qt.y<pt.y; bn++,b++){
+ _frcklinewrap(f, &qt, b);
+ if(qt.y >= pt.y)
+ break;
+ _fradvance(f, &qt, b);
+ p += NRUNE(b);
+ }
+ for(; bn<f->nbox && qt.x<=pt.x; bn++,b++){
+ _frcklinewrap(f, &qt, b);
+ if(qt.y > pt.y)
+ break;
+ if(qt.x+b->wid > pt.x){
+ if(b->nrune < 0)
+ _fradvance(f, &qt, b);
+ else{
+ s = b->ptr;
+ for(;;){
+ if((r = *s) < Runeself)
+ w = 1;
+ else
+ w = chartorune(&r, (char*)s);
+ if(r == 0)
+ drawerror(f->display, "end of string in frcharofpt");
+ qt.x += stringnwidth(f->font, (char*)s, 1);
+ s += w;
+ if(qt.x > pt.x)
+ break;
+ p++;
+ }
+ }
+ }else{
+ p += NRUNE(b);
+ _fradvance(f, &qt, b);
+ }
+ }
+ return p;
+}
diff --git a/sys/src/libframe/frselect.c b/sys/src/libframe/frselect.c
new file mode 100755
index 000000000..f5955e42b
--- /dev/null
+++ b/sys/src/libframe/frselect.c
@@ -0,0 +1,133 @@
+#include <u.h>
+#include <libc.h>
+#include <draw.h>
+#include <thread.h>
+#include <mouse.h>
+#include <frame.h>
+
+static
+int
+region(int a, int b)
+{
+ if(a < b)
+ return -1;
+ if(a == b)
+ return 0;
+ return 1;
+}
+
+void
+frselect(Frame *f, Mousectl *mc) /* when called, button 1 is down */
+{
+ ulong p0, p1, q;
+ Point mp, pt0, pt1, qt;
+ int reg, b, scrled;
+
+ mp = mc->xy;
+ b = mc->buttons;
+
+ f->modified = 0;
+ frdrawsel(f, frptofchar(f, f->p0), f->p0, f->p1, 0);
+ p0 = p1 = frcharofpt(f, mp);
+ f->p0 = p0;
+ f->p1 = p1;
+ pt0 = frptofchar(f, p0);
+ pt1 = frptofchar(f, p1);
+ frdrawsel(f, pt0, p0, p1, 1);
+ reg = 0;
+ do{
+ scrled = 0;
+ if(f->scroll){
+ if(mp.y < f->r.min.y){
+ (*f->scroll)(f, -(f->r.min.y-mp.y)/(int)f->font->height-1);
+ p0 = f->p1;
+ p1 = f->p0;
+ scrled = 1;
+ }else if(mp.y > f->r.max.y){
+ (*f->scroll)(f, (mp.y-f->r.max.y)/(int)f->font->height+1);
+ p0 = f->p0;
+ p1 = f->p1;
+ scrled = 1;
+ }
+ if(scrled){
+ if(reg != region(p1, p0))
+ q = p0, p0 = p1, p1 = q; /* undo the swap that will happen below */
+ pt0 = frptofchar(f, p0);
+ pt1 = frptofchar(f, p1);
+ reg = region(p1, p0);
+ }
+ }
+ q = frcharofpt(f, mp);
+ if(p1 != q){
+ if(reg != region(q, p0)){ /* crossed starting point; reset */
+ if(reg > 0)
+ frdrawsel(f, pt0, p0, p1, 0);
+ else if(reg < 0)
+ frdrawsel(f, pt1, p1, p0, 0);
+ p1 = p0;
+ pt1 = pt0;
+ reg = region(q, p0);
+ if(reg == 0)
+ frdrawsel(f, pt0, p0, p1, 1);
+ }
+ qt = frptofchar(f, q);
+ if(reg > 0){
+ if(q > p1)
+ frdrawsel(f, pt1, p1, q, 1);
+ else if(q < p1)
+ frdrawsel(f, qt, q, p1, 0);
+ }else if(reg < 0){
+ if(q > p1)
+ frdrawsel(f, pt1, p1, q, 0);
+ else
+ frdrawsel(f, qt, q, p1, 1);
+ }
+ p1 = q;
+ pt1 = qt;
+ }
+ f->modified = 0;
+ if(p0 < p1) {
+ f->p0 = p0;
+ f->p1 = p1;
+ }
+ else {
+ f->p0 = p1;
+ f->p1 = p0;
+ }
+ if(scrled)
+ (*f->scroll)(f, 0);
+ flushimage(f->display, 1);
+ if(!scrled)
+ readmouse(mc);
+ mp = mc->xy;
+ }while(mc->buttons == b);
+}
+
+void
+frselectpaint(Frame *f, Point p0, Point p1, Image *col)
+{
+ int n;
+ Point q0, q1;
+
+ q0 = p0;
+ q1 = p1;
+ q0.y += f->font->height;
+ q1.y += f->font->height;
+ n = (p1.y-p0.y)/f->font->height;
+ if(f->b == nil)
+ drawerror(f->display, "frselectpaint b==0");
+ if(p0.y == f->r.max.y)
+ return;
+ if(n == 0)
+ draw(f->b, Rpt(p0, q1), col, nil, ZP);
+ else{
+ if(p0.x >= f->r.max.x)
+ p0.x = f->r.max.x-1;
+ draw(f->b, Rect(p0.x, p0.y, f->r.max.x, q0.y), col, nil, ZP);
+ if(n > 1)
+ draw(f->b, Rect(f->r.min.x, q0.y, f->r.max.x, p1.y),
+ col, nil, ZP);
+ draw(f->b, Rect(f->r.min.x, p1.y, q1.x, q1.y),
+ col, nil, ZP);
+ }
+}
diff --git a/sys/src/libframe/frstr.c b/sys/src/libframe/frstr.c
new file mode 100755
index 000000000..f47d3f00d
--- /dev/null
+++ b/sys/src/libframe/frstr.c
@@ -0,0 +1,38 @@
+#include <u.h>
+#include <libc.h>
+#include <draw.h>
+#include <thread.h>
+#include <mouse.h>
+#include <frame.h>
+
+#define CHUNK 16
+#define ROUNDUP(n) ((n+CHUNK)&~(CHUNK-1))
+
+uchar *
+_frallocstr(Frame *f, unsigned n)
+{
+ uchar *p;
+
+ p = malloc(ROUNDUP(n));
+ if(p == 0)
+ drawerror(f->display, "out of memory");
+ return p;
+}
+
+void
+_frinsure(Frame *f, int bn, unsigned n)
+{
+ Frbox *b;
+ uchar *p;
+
+ b = &f->box[bn];
+ if(b->nrune < 0)
+ drawerror(f->display, "_frinsure");
+ if(ROUNDUP(b->nrune) > n) /* > guarantees room for terminal NUL */
+ return;
+ p = _frallocstr(f, n);
+ b = &f->box[bn];
+ memmove(p, b->ptr, NBYTE(b)+1);
+ free(b->ptr);
+ b->ptr = p;
+}
diff --git a/sys/src/libframe/frutil.c b/sys/src/libframe/frutil.c
new file mode 100755
index 000000000..ad0c69c0a
--- /dev/null
+++ b/sys/src/libframe/frutil.c
@@ -0,0 +1,112 @@
+#include <u.h>
+#include <libc.h>
+#include <draw.h>
+#include <thread.h>
+#include <mouse.h>
+#include <frame.h>
+
+int
+_frcanfit(Frame *f, Point pt, Frbox *b)
+{
+ int left, w, nr;
+ uchar *p;
+ Rune r;
+
+ left = f->r.max.x-pt.x;
+ if(b->nrune < 0)
+ return b->minwid <= left;
+ if(left >= b->wid)
+ return b->nrune;
+ for(nr=0,p=b->ptr; *p; p+=w,nr++){
+ r = *p;
+ if(r < Runeself)
+ w = 1;
+ else
+ w = chartorune(&r, (char*)p);
+ left -= stringnwidth(f->font, (char*)p, 1);
+ if(left < 0)
+ return nr;
+ }
+ drawerror(f->display, "_frcanfit can't");
+ return 0;
+}
+
+void
+_frcklinewrap(Frame *f, Point *p, Frbox *b)
+{
+ if((b->nrune<0? b->minwid : b->wid) > f->r.max.x-p->x){
+ p->x = f->r.min.x;
+ p->y += f->font->height;
+ }
+}
+
+void
+_frcklinewrap0(Frame *f, Point *p, Frbox *b)
+{
+ if(_frcanfit(f, *p, b) == 0){
+ p->x = f->r.min.x;
+ p->y += f->font->height;
+ }
+}
+
+void
+_fradvance(Frame *f, Point *p, Frbox *b)
+{
+ if(b->nrune<0 && b->bc=='\n'){
+ p->x = f->r.min.x;
+ p->y += f->font->height;
+ }else
+ p->x += b->wid;
+}
+
+int
+_frnewwid(Frame *f, Point pt, Frbox *b)
+{
+ b->wid = _frnewwid0(f, pt, b);
+ return b->wid;
+}
+
+int
+_frnewwid0(Frame *f, Point pt, Frbox *b)
+{
+ int c, x;
+
+ c = f->r.max.x;
+ x = pt.x;
+ if(b->nrune>=0 || b->bc!='\t')
+ return b->wid;
+ if(x+b->minwid > c)
+ x = pt.x = f->r.min.x;
+ x += f->maxtab;
+ x -= (x-f->r.min.x)%f->maxtab;
+ if(x-pt.x<b->minwid || x>c)
+ x = pt.x+b->minwid;
+ return x-pt.x;
+}
+
+void
+_frclean(Frame *f, Point pt, int n0, int n1) /* look for mergeable boxes */
+{
+ Frbox *b;
+ int nb, c;
+
+ c = f->r.max.x;
+ for(nb=n0; nb<n1-1; nb++){
+ b = &f->box[nb];
+ _frcklinewrap(f, &pt, b);
+ while(b[0].nrune>=0 && nb<n1-1 && b[1].nrune>=0 && pt.x+b[0].wid+b[1].wid<c){
+ _frmergebox(f, nb);
+ n1--;
+ b = &f->box[nb];
+ }
+ _fradvance(f, &pt, &f->box[nb]);
+ }
+ for(; nb<f->nbox; nb++){
+ b = &f->box[nb];
+ _frcklinewrap(f, &pt, b);
+ _fradvance(f, &pt, &f->box[nb]);
+ }
+ f->lastlinefull = 0;
+ if(pt.y >= f->r.max.y)
+ f->lastlinefull = 1;
+}
diff --git a/sys/src/libframe/mkfile b/sys/src/libframe/mkfile
new file mode 100755
index 000000000..c0b1e2e30
--- /dev/null
+++ b/sys/src/libframe/mkfile
@@ -0,0 +1,21 @@
+</$objtype/mkfile
+
+LIB=/$objtype/lib/libframe.a
+OFILES=\
+ frbox.$O\
+ frdraw.$O\
+ frdelete.$O\
+ frinit.$O\
+ frinsert.$O\
+ frptofchar.$O\
+ frselect.$O\
+ frstr.$O\
+ frutil.$O\
+
+UPDATE=\
+ mkfile\
+ $HFILES\
+ ${OFILES:%.$O=%.c}\
+ ${LIB:/$objtype/%=/386/%}\
+
+< /sys/src/cmd/mksyslib