20 HiWater = 640000, /* max size of history */
21 LoWater = 400000, /* min size of history after max'ed */
22 MinWater = 20000, /* room to leave available when reallocating */
28 static Image *cols[NCOL];
30 static Image *darkgrey;
31 static Cursor *lastcursor;
32 static Image *titlecol;
33 static Image *lighttitlecol;
34 static Image *holdcol;
35 static Image *lightholdcol;
36 static Image *paleholdcol;
39 wmk(Image *i, Mousectl *mc, Channel *ck, Channel *cctl, int scrolling)
45 /* greys are multiples of 0x11111100+0xFF, 14* being palest */
46 grey = allocimage(display, Rect(0,0,1,1), CMAP8, 1, 0xEEEEEEFF);
47 darkgrey = allocimage(display, Rect(0,0,1,1), CMAP8, 1, 0x666666FF);
48 cols[BACK] = display->white;
49 cols[HIGH] = allocimage(display, Rect(0,0,1,1), CMAP8, 1, 0xCCCCCCFF);
50 cols[BORD] = allocimage(display, Rect(0,0,1,1), CMAP8, 1, 0x999999FF);
51 cols[TEXT] = display->black;
52 cols[HTEXT] = display->black;
53 titlecol = allocimage(display, Rect(0,0,1,1), CMAP8, 1, DGreygreen);
54 lighttitlecol = allocimage(display, Rect(0,0,1,1), CMAP8, 1, DPalegreygreen);
55 holdcol = allocimage(display, Rect(0,0,1,1), CMAP8, 1, DMedblue);
56 lightholdcol = allocimage(display, Rect(0,0,1,1), CMAP8, 1, DGreyblue);
57 paleholdcol = allocimage(display, Rect(0,0,1,1), CMAP8, 1, DPalegreyblue);
59 w = emalloc(sizeof(Window));
61 r = insetrect(i->r, Selborder+1);
67 w->conswrite = chancreate(sizeof(Conswritemesg), 0);
68 w->consread = chancreate(sizeof(Consreadmesg), 0);
69 w->mouseread = chancreate(sizeof(Mousereadmesg), 0);
70 w->wctlread = chancreate(sizeof(Consreadmesg), 0);
72 w->scrollr.max.x = r.min.x+Scrollwid;
74 r.min.x += Scrollwid+Scrollgap;
75 frinit(&w->f, r, font, i, cols);
76 w->f.maxtab = maxtab*stringwidth(font, "0");
80 w->scrolling = scrolling;
81 w->dir = estrdup(startdir);
82 w->label = estrdup("<unnamed>");
83 r = insetrect(w->i->r, Selborder);
84 draw(w->i, r, cols[BACK], nil, w->f.entire.min);
85 wborder(w, Selborder);
87 incref(&w->ref); /* ref will be removed after mounting; avoids delete before ready to be deleted */
97 n = sprint(w->name, "window.%d.%d", w->id, w->namecount++);
98 for(i='A'; i<='Z'; i++){
99 if(nameimage(w->i, w->name, 1) > 0)
101 errstr(err, sizeof err);
102 if(strcmp(err, "image name in use") != 0)
108 fprint(2, "rio: setname failed: %s\n", err);
112 wresize(Window *w, Image *i, int move)
117 if(move || (Dx(or)==Dx(i->r) && Dy(or)==Dy(i->r)))
118 draw(i, i->r, w->i, nil, w->i->r.min);
120 fprint(2, "res %p %p\n", w->i, i);
125 //XXX w->mc.image = i;
126 r = insetrect(i->r, Selborder+1);
128 w->scrollr.max.x = r.min.x+Scrollwid;
130 r.min.x += Scrollwid+Scrollgap;
132 frsetrects(&w->f, r, w->i);
134 frclear(&w->f, FALSE);
135 frinit(&w->f, r, w->f.font, w->i, cols);
137 w->f.maxtab = maxtab*stringwidth(w->f.font, "0");
138 r = insetrect(w->i->r, Selborder);
139 draw(w->i, r, cols[BACK], nil, w->f.entire.min);
141 wsetselect(w, w->q0, w->q1);
144 wborder(w, Selborder);
145 w->topped = ++topped;
151 wrefresh(Window *w, Rectangle r)
155 /* BUG: rectangle is ignored */
157 wborder(w, Selborder);
159 wborder(w, Unselborder);
162 draw(w->i, insetrect(w->i->r, Borderwidth), w->f.cols[BACK], nil, w->i->r.min);
165 frdrawsel(&w->f, frptofchar(&w->f, 0), 0, w->f.p0, 0);
166 if(w->f.p1 < w->f.nchars)
167 frdrawsel(&w->f, frptofchar(&w->f, w->f.p1), w->f.p1, w->f.nchars, 0);
168 frdrawsel(&w->f, frptofchar(&w->f, w->f.p0), w->f.p0, w->f.p1, 1);
182 error("negative ref count");
185 wsendctlmesg(w, Exited, ZR, nil);
193 Rune *rp, *bp, *up, *kbdr;
195 int nr, nb, c, wid, i, npart, initial, lastb;
196 char *s, *t, part[3];
199 enum { WKey, WMouse, WMouseread, WCtl, WCwrite, WCread, WWread, NWALT };
210 snprint(buf, sizeof buf, "winctl-id%d", w->id);
213 mrm.cm = chancreate(sizeof(Mouse), 0);
214 cwm.cw = chancreate(sizeof(Stringpair), 0);
215 crm.c1 = chancreate(sizeof(Stringpair), 0);
216 crm.c2 = chancreate(sizeof(Stringpair), 0);
217 cwrm.c1 = chancreate(sizeof(Stringpair), 0);
218 cwrm.c2 = chancreate(sizeof(Stringpair), 0);
221 alts[WKey].c = w->ck;
222 alts[WKey].v = &kbdr;
223 alts[WKey].op = CHANRCV;
224 alts[WMouse].c = w->mc.c;
225 alts[WMouse].v = &w->mc.m;
226 alts[WMouse].op = CHANRCV;
227 alts[WMouseread].c = w->mouseread;
228 alts[WMouseread].v = &mrm;
229 alts[WMouseread].op = CHANSND;
230 alts[WCtl].c = w->cctl;
232 alts[WCtl].op = CHANRCV;
233 alts[WCwrite].c = w->conswrite;
234 alts[WCwrite].v = &cwm;
235 alts[WCwrite].op = CHANSND;
236 alts[WCread].c = w->consread;
237 alts[WCread].v = &crm;
238 alts[WCread].op = CHANSND;
239 alts[WWread].c = w->wctlread;
240 alts[WWread].v = &cwrm;
241 alts[WWread].op = CHANSND;
242 alts[NWALT].op = CHANEND;
247 if(w->mouseopen && w->mouse.counter != w->mouse.lastcounter)
248 alts[WMouseread].op = CHANSND;
250 alts[WMouseread].op = CHANNOP;
251 if(!w->scrolling && !w->mouseopen && w->qh>w->org+w->f.nchars)
252 alts[WCwrite].op = CHANNOP;
254 alts[WCwrite].op = CHANSND;
255 if(w->deleted || !w->wctlready)
256 alts[WWread].op = CHANNOP;
258 alts[WWread].op = CHANSND;
259 /* this code depends on NL and EOT fitting in a single byte */
260 /* kind of expensive for each loop; worth precomputing? */
262 alts[WCread].op = CHANNOP;
263 else if(npart || (w->rawing && w->nraw>0))
264 alts[WCread].op = CHANSND;
266 alts[WCread].op = CHANNOP;
267 for(i=w->qh; i<w->nr; i++){
269 if(c=='\n' || c=='\004'){
270 alts[WCread].op = CHANSND;
277 for(i=0; kbdr[i]!=L'\0'; i++)
280 // while(nbrecv(w->ck, &r))
287 /* queue click events */
288 if(!w->mouse.qfull && lastb != w->mc.m.buttons) { /* add to ring */
289 mp = &w->mouse.queue[w->mouse.wi];
290 if(++w->mouse.wi == nelem(w->mouse.queue))
292 if(w->mouse.wi == w->mouse.ri)
293 w->mouse.qfull = TRUE;
295 mp->counter = w->mouse.counter;
296 lastb = w->mc.m.buttons;
302 /* send a queued event or, if the queue is empty, the current state */
303 /* if the queue has filled, we discard all the events it contained. */
304 /* the intent is to discard frantic clicking by the user during long latencies. */
305 w->mouse.qfull = FALSE;
306 if(w->mouse.wi != w->mouse.ri) {
307 m = w->mouse.queue[w->mouse.ri];
308 if(++w->mouse.ri == nelem(w->mouse.queue))
311 m = (Mousestate){w->mc.m, w->mouse.counter};
313 w->mouse.lastcounter = m.counter;
317 if(wctlmesg(w, wcm.type, wcm.r, wcm.image) == Exited){
345 while(i<nr-1 && *(bp+1) == '\r'){
349 if(i<nr-1 && *(bp+1) != '\n'){
350 while(up > rp && *(up-1) != '\n')
353 initial = wbswidth(w, '\r');
366 qh = w->qh - initial;
367 wdelete(w, qh, qh+initial);
371 w->qh = winsert(w, rp, nr, w->qh)+nr;
372 if(w->scrolling || w->mouseopen)
374 wsetselect(w, w->q0, w->q1);
386 while(i<nb && (w->qh<w->nr || w->nraw>0)){
388 wid = runetochar(t+i, &w->raw[0]);
390 runemove(w->raw, w->raw+1, w->nraw);
392 wid = runetochar(t+i, &w->r[w->qh++]);
393 c = t[i]; /* knows break characters fit in a byte */
395 if(!w->rawing && (c == '\n' || c=='\004')){
401 // if(i==nb && w->qh<w->nr && w->r[w->qh]=='\004')
405 memmove(part, t+nb, npart);
414 recv(cwrm.c1, &pair);
415 if(w->deleted || w->i==nil)
416 pair.ns = sprint(pair.s, "");
419 for(i=0; i<nhidden; i++)
427 pair.ns = snprint(pair.s, pair.ns, "%11d %11d %11d %11d %s %s ",
428 w->i->r.min.x, w->i->r.min.y, w->i->r.max.x, w->i->r.max.y, t, s);
430 send(cwrm.c2, &pair);
434 flushimage(display, 1);
439 waddraw(Window *w, Rune *r, int nr)
441 w->raw = runerealloc(w->raw, w->nraw+nr);
442 runemove(w->raw+w->nraw, r, nr);
447 * Need to do this in a separate proc because if process we're interrupting
448 * is dying and trying to print tombstone, kernel is blocked holding p->debug lock.
451 interruptproc(void *v)
456 write(*notefd, "interrupt", 9);
461 windfilewidth(Window *w, uint q0, int oneelement)
471 if(oneelement && r=='/')
479 showcandidates(Window *w, Completion *c)
489 s = "[no matches in ";
493 fmtprint(&f, "%s%d files]\n", s, c->nfile);
495 fmtprint(&f, "%s", s);
496 for(i=0; i<c->nfile; i++){
499 fmtprint(&f, "%s", c->filename[i]);
503 /* place text at beginning of line before host point */
505 while(qline>0 && w->r[qline-1] != '\n')
508 rp = runefmtstrflush(&f);
512 q0 += winsert(w, rp, runestrlen(rp), qline) - qline;
514 wsetselect(w, q0+nr, q0+nr);
518 namecomplete(Window *w)
521 Rune *rp, *path, *str;
523 char *s, *dir, *root;
525 /* control-f: filename completion; works back to white space or / */
526 if(w->q0<w->nr && w->r[w->q0]>' ') /* must be at end of word */
528 nstr = windfilewidth(w, w->q0, TRUE);
529 str = runemalloc(nstr);
530 runemove(str, w->r+(w->q0-nstr), nstr);
531 npath = windfilewidth(w, w->q0-nstr, FALSE);
532 path = runemalloc(npath);
533 runemove(path, w->r+(w->q0-nstr-npath), npath);
536 /* is path rooted? if not, we need to make it relative to window path */
537 if(npath>0 && path[0]=='/'){
538 dir = malloc(UTFmax*npath+1);
539 sprint(dir, "%.*S", npath, path);
541 if(strcmp(w->dir, "") == 0)
545 dir = malloc(strlen(root)+1+UTFmax*npath+1);
546 sprint(dir, "%s/%.*S", root, npath, path);
548 dir = cleanname(dir);
550 s = smprint("%.*S", nstr, str);
551 c = complete(dir, s);
557 showcandidates(w, c);
560 rp = runesmprint("%s", c->string);
571 wkeyctl(Window *w, Rune r)
582 /* navigation keys work only when mouse is not open */
589 n = mousescrollsize(w->f.maxlines);
594 n = 2*w->f.maxlines/3;
596 q0 = w->org+frcharofpt(&w->f, Pt(w->f.r.min.x, w->f.r.min.y+n*w->f.font->height));
597 wsetorigin(w, q0, TRUE);
603 n = mousescrollsize(w->f.maxlines);
608 n = 2*w->f.maxlines/3;
610 q0 = wbacknl(w, w->org, n);
611 wsetorigin(w, q0, TRUE);
616 wsetselect(w, q0, q0);
623 wsetselect(w, q1, q1);
633 case 0x01: /* ^A: beginning of line */
634 if(w->q0==0 || w->q0==w->qh || w->r[w->q0-1]=='\n')
636 nb = wbswidth(w, 0x15 /* ^U */);
637 wsetselect(w, w->q0-nb, w->q0-nb);
640 case 0x05: /* ^E: end of line */
642 while(q0 < w->nr && w->r[q0]!='\n')
644 wsetselect(w, q0, q0);
649 * This if used to be below the if(w->rawing ...),
650 * but let's try putting it here. This will allow ESC-processing
651 * to toggle hold mode even in remote SSH connections.
652 * The drawback is that vi-style processing gets harder.
653 * If you find yourself in some weird readline mode, good
654 * luck getting out without ESC. Let's see who complains.
656 if(r==0x1B || (w->holding && r==0x7F)){ /* toggle hold */
665 if(!w->holding && w->rawing && (w->q0==w->nr || w->mouseopen)){
674 case 0x7F: /* send interrupt */
679 case 0x06: /* ^F: file name completion */
680 case Kins: /* Insert: file name completion */
681 rp = namecomplete(w);
686 q0 = winsert(w, rp, nr, q0);
690 case 0x08: /* ^H: erase character */
691 case 0x15: /* ^U: erase line */
692 case 0x17: /* ^W: erase word */
693 if(w->q0==0 || w->q0==w->qh)
703 wdelete(w, q0, q0+nb);
704 wsetselect(w, q0, q0);
708 /* otherwise ordinary character; just insert */
710 q0 = winsert(w, &r, 1, q0);
719 w->f.cols[TEXT] = w->f.cols[HTEXT] = holdcol;
721 w->f.cols[TEXT] = w->f.cols[HTEXT] = lightholdcol;
724 w->f.cols[TEXT] = w->f.cols[HTEXT] = display->black;
726 w->f.cols[TEXT] = w->f.cols[HTEXT] = darkgrey;
734 _frredraw(&w->f, w->f.r.min);
736 wborder(w, Selborder);
739 wborder(w, Unselborder);
743 wbswidth(Window *w, Rune c)
749 /* there is known to be at least one character to erase */
750 if(c == 0x08) /* ^H: erase character */
759 if(r == '\n'){ /* eat at most one more character */
760 if(q == w->q0 && c != '\r') /* eat the newline */
766 if(eq && skipping) /* found one; stop skipping */
768 else if(!eq && !skipping)
781 nsnarf = w->q1-w->q0;
782 snarf = runerealloc(snarf, nsnarf);
783 snarfversion++; /* maybe modified by parent */
784 runemove(snarf, w->r+w->q0, nsnarf);
793 wdelete(w, w->q0, w->q1);
794 wsetselect(w, w->q0, w->q0);
806 if(w->rawing && q0==w->nr){
807 waddraw(w, snarf, nsnarf);
808 wsetselect(w, q0, q0);
810 q0 = winsert(w, snarf, nsnarf, w->q0);
811 wsetselect(w, q0, q0+nsnarf);
825 fd = plumbopenfid("send", OWRITE);
828 m = emalloc(sizeof(Plumbmsg));
829 m->src = estrdup("rio");
831 m->wdir = estrdup(w->dir);
832 m->type = estrdup("text");
838 while(p0>0 && w->r[p0-1]!=' ' && w->r[p0-1]!='\t' && w->r[p0-1]!='\n')
840 while(p1<w->nr && w->r[p1]!=' ' && w->r[p1]!='\t' && w->r[p1]!='\n')
842 sprint(buf, "click=%d", w->q0-p0);
843 m->attr = plumbunpackattr(buf);
845 if(p1-p0 > messagesize-1024){
847 return; /* too large for 9P */
849 m->data = runetobyte(w->r+p0, p1-p0, &m->ndata);
850 if(plumbsendtofid(fd, m) < 0){
852 riosetcursor(&query, 1);
860 winborder(Window *w, Point xy)
862 return ptinrect(xy, w->screenr) && !ptinrect(xy, insetrect(w->screenr, Selborder));
870 if(w->mc.m.buttons == 1)
872 else if(w->mc.m.buttons == 2)
874 else if(w->mc.m.buttons == 4)
877 if(w->mc.m.buttons == 8)
878 wkeyctl(w, Kscrolloneup);
879 if(w->mc.m.buttons == 16)
880 wkeyctl(w, Kscrollonedown);
884 incref(&w->ref); /* hold up window while we track */
887 if(ptinrect(w->mc.m.xy, w->scrollr)){
894 /* else all is handled by main process */
900 wdelete(Window *w, uint q0, uint q1)
907 runemove(w->r+q0, w->r+q1, w->nr-q1);
910 w->q0 -= min(n, w->q0-q0);
912 w->q1 -= min(n, w->q1-q0);
919 else if(q0 < w->org+w->f.nchars){
928 frdelete(&w->f, p0, p1);
934 static Window *clickwin;
935 static uint clickmsec;
936 static Window *selectwin;
940 * called from frame library
943 framescroll(Frame *f, int dl)
945 if(f != &selectwin->f)
946 error("frameselect not right frame");
947 wframescroll(selectwin, dl);
951 wframescroll(Window *w, int dl)
960 q0 = wbacknl(w, w->org, -dl);
961 if(selectq > w->org+w->f.p0)
962 wsetselect(w, w->org+w->f.p0, selectq);
964 wsetselect(w, selectq, w->org+w->f.p0);
966 if(w->org+w->f.nchars == w->nr)
968 q0 = w->org+frcharofpt(&w->f, Pt(w->f.r.min.x, w->f.r.min.y+dl*w->f.font->height));
969 if(selectq >= w->org+w->f.p1)
970 wsetselect(w, w->org+w->f.p1, selectq);
972 wsetselect(w, selectq, w->org+w->f.p1);
974 wsetorigin(w, q0, TRUE);
986 * Double-click immediately if it might make sense.
991 selectq = w->org+frcharofpt(&w->f, w->mc.m.xy);
992 if(clickwin==w && w->mc.m.msec-clickmsec<500)
993 if(q0==q1 && selectq==w->q0){
994 wdoubleclick(w, &q0, &q1);
995 wsetselect(w, q0, q1);
996 flushimage(display, 1);
999 /* stay here until something interesting happens */
1002 while(w->mc.m.buttons==b && abs(w->mc.m.xy.x-x)<3 && abs(w->mc.m.xy.y-y)<3);
1003 w->mc.m.xy.x = x; /* in case we're calling frselect */
1005 q0 = w->q0; /* may have changed */
1009 if(w->mc.m.buttons == b){
1010 w->f.scroll = framescroll;
1011 frselect(&w->f, &w->mc);
1012 /* horrible botch: while asleep, may have lost selection altogether */
1014 selectq = w->org + w->f.p0;
1016 if(selectq < w->org)
1019 q0 = w->org + w->f.p0;
1020 if(selectq > w->org+w->f.nchars)
1023 q1 = w->org+w->f.p1;
1026 if(q0==w->q0 && clickwin==w && w->mc.m.msec-clickmsec<500){
1027 wdoubleclick(w, &q0, &q1);
1031 clickmsec = w->mc.m.msec;
1035 wsetselect(w, q0, q1);
1036 flushimage(display, 1);
1037 while(w->mc.m.buttons){
1039 b = w->mc.m.buttons;
1053 flushimage(display, 1);
1054 while(w->mc.m.buttons == b)
1061 wsendctlmesg(Window *w, int type, Rectangle r, Image *image)
1068 send(w->cctl, &wcm);
1072 wctlmesg(Window *w, int m, Rectangle r, Image *i)
1078 error("unknown control message");
1089 strcpy(buf, w->name);
1090 wresize(w, i, m==Moved);
1095 }else if(w == input)
1097 flushimage(display, 1);
1100 if(w->deleted || Dx(w->screenr)<=0 || !rectclip(&r, w->i->r))
1104 flushimage(display, 1);
1107 if(sweeping || !ptinrect(r.min, w->i->r))
1109 wmovemouse(w, r.min);
1116 wkeyctl(w, w->raw[0]);
1118 runemove(w->raw, w->raw+1, w->nraw);
1126 flushimage(display, 1);
1131 write(w->notefd, "hangup", 6);
1135 frclear(&w->f, TRUE);
1140 chanfree(w->conswrite);
1141 chanfree(w->consread);
1142 chanfree(w->mouseread);
1143 chanfree(w->wctlread);
1155 * Convert back to physical coordinates
1158 wmovemouse(Window *w, Point p)
1160 p.x += w->screenr.min.x-w->i->r.min.x;
1161 p.y += w->screenr.min.y-w->i->r.min.y;
1162 moveto(mousectl, p);
1170 if(wkeyboard!=nil && w==wkeyboard)
1174 if(oi!=w && oi!=nil)
1183 wsendctlmesg(oi, Wakeup, ZR, nil);
1187 wsendctlmesg(w, Wakeup, ZR, nil);
1193 wsetcursor(Window *w, int force)
1197 if(w==nil || /*w!=input || */ w->i==nil || Dx(w->screenr)<=0)
1199 else if(wpointto(mouse->xy) == w){
1201 if(p==nil && w->holding)
1206 riosetcursor(p, force && !menuing);
1210 riosetcursor(Cursor *p, int force)
1212 if(!force && p==lastcursor)
1214 setcursor(mousectl, p);
1225 if(w->topped == topped)
1229 flushimage(display, 1);
1230 w->topped = ++topped;
1238 if(w!=nil && w->i!=nil && !w->deleted && w->topped!=topped){
1240 flushimage(display, 1);
1241 w->topped = ++ topped;
1246 wbottomme(Window *w)
1248 if(w!=nil && w->i!=nil && !w->deleted){
1250 flushimage(display, 1);
1260 for(i=0; i<nwindow; i++)
1261 if(window[i]->id == id)
1267 wclosewin(Window *w)
1279 for(i=0; i<nhidden; i++)
1282 memmove(hidden+i, hidden+i+1, (nhidden-i)*sizeof(hidden[0]));
1285 for(i=0; i<nwindow; i++)
1288 memmove(window+i, window+i+1, (nwindow-i)*sizeof(Window*));
1291 /* move it off-screen to hide it, in case client is slow in letting it go */
1292 MOVEIT originwindow(w->i, r.min, view->r.max);
1297 error("unknown window in closewin");
1301 wsetpid(Window *w, int pid, int dolabel)
1307 sprint(buf, "rc %d", pid);
1309 w->label = estrdup(buf);
1310 drawsetlabel(w->label);
1314 static Rune left1[] = { '{', '[', '(', '<', 0xAB, 0 };
1315 static Rune right1[] = { '}', ']', ')', '>', 0xBB, 0 };
1316 static Rune left2[] = { '\n', 0 };
1317 static Rune left3[] = { '\'', '"', '`', 0 };
1333 wdoubleclick(Window *w, uint *q0, uint *q1)
1339 for(i=0; left[i]!=nil; i++){
1343 /* try matching character to left, looking right */
1350 if(wclickmatch(w, c, r[p-l], 1, &q))
1354 /* try matching character to right, looking left */
1361 if(wclickmatch(w, c, l[p-r], -1, &q)){
1362 *q1 = *q0+(*q0<w->nr && c=='\n');
1364 if(c!='\n' || q!=0 || w->r[0]=='\n')
1370 /* try filling out word to right */
1371 while(*q1<w->nr && isalnum(w->r[*q1]))
1373 /* try filling out word to left */
1374 while(*q0>0 && isalnum(w->r[*q0-1]))
1379 wclickmatch(Window *w, int cl, int cr, int dir, uint *q)
1403 return cl=='\n' && nest==1;
1408 wbacknl(Window *w, uint p, uint n)
1412 /* look for start of this line if n==0 */
1413 if(n==0 && p>0 && w->r[p-1]!='\n')
1416 while(i-->0 && p>0){
1417 --p; /* it's at a newline now; back over it */
1420 /* at 128 chars, call it a line anyway */
1421 for(j=128; --j>0 && p>0; p--)
1429 wshow(Window *w, uint q0)
1435 qe = w->org+w->f.nchars;
1436 if(w->org<=q0 && (q0<qe || (q0==qe && qe==w->nr)))
1439 nl = 4*w->f.maxlines/5;
1440 q = wbacknl(w, q0, nl);
1441 /* avoid going backwards if trying to go forwards - long lines! */
1442 if(!(q0>w->org && q<w->org))
1443 wsetorigin(w, q, TRUE);
1444 while(q0 > w->org+w->f.nchars)
1445 wsetorigin(w, w->org+1, FALSE);
1450 wsetorigin(Window *w, uint org, int exact)
1456 if(org>0 && !exact){
1457 /* org is an estimate of the char posn; find a newline */
1458 /* don't try harder than 256 chars */
1459 for(i=0; i<256 && org<w->nr; i++){
1460 if(w->r[org] == '\n'){
1469 if(a>=0 && a<w->f.nchars){
1470 frdelete(&w->f, 0, a);
1471 fixup = 1; /* frdelete can leave end of last line in wrong selection mode; it doesn't know what follows */
1472 }else if(a<0 && -a<w->f.nchars){
1475 runemove(r, w->r+org, n);
1476 frinsert(&w->f, r, r+n, 0);
1479 frdelete(&w->f, 0, w->f.nchars);
1483 wsetselect(w, w->q0, w->q1);
1484 if(fixup && w->f.p1 > w->f.p0)
1485 frdrawsel(&w->f, frptofchar(&w->f, w->f.p1-1), w->f.p1-1, w->f.p1, 1);
1489 wsetselect(Window *w, uint q0, uint q1)
1493 /* w->f.p0 and w->f.p1 are always right; w->q0 and w->q1 may be off */
1496 /* compute desired p0,p1 from q0,q1 */
1503 if(p0 > w->f.nchars)
1505 if(p1 > w->f.nchars)
1507 if(p0==w->f.p0 && p1==w->f.p1)
1509 /* screen disagrees with desired selection */
1510 if(w->f.p1<=p0 || p1<=w->f.p0 || p0==p1 || w->f.p1==w->f.p0){
1511 /* no overlap or too easy to bother trying */
1512 frdrawsel(&w->f, frptofchar(&w->f, w->f.p0), w->f.p0, w->f.p1, 0);
1513 frdrawsel(&w->f, frptofchar(&w->f, p0), p0, p1, 1);
1516 /* overlap; avoid unnecessary painting */
1518 /* extend selection backwards */
1519 frdrawsel(&w->f, frptofchar(&w->f, p0), p0, w->f.p0, 1);
1520 }else if(p0 > w->f.p0){
1521 /* trim first part of selection */
1522 frdrawsel(&w->f, frptofchar(&w->f, w->f.p0), w->f.p0, p0, 0);
1525 /* extend selection forwards */
1526 frdrawsel(&w->f, frptofchar(&w->f, w->f.p1), w->f.p1, p1, 1);
1527 }else if(p1 < w->f.p1){
1528 /* trim last part of selection */
1529 frdrawsel(&w->f, frptofchar(&w->f, p1), p1, w->f.p1, 0);
1538 winsert(Window *w, Rune *r, int n, uint q0)
1544 if(w->nr+n>HiWater && q0>=w->org && q0>=w->qh){
1545 m = min(HiWater-LoWater, min(w->org, w->qh));
1557 runemove(w->r, w->r+m, w->nr);
1560 if(w->nr+n > w->maxr){
1562 * Minimize realloc breakage:
1563 * Allocate at least MinWater
1564 * Double allocation size each time
1565 * But don't go much above HiWater
1567 m = max(min(2*(w->nr+n), HiWater), w->nr+n)+MinWater;
1569 m = max(HiWater+MinWater, w->nr+n);
1571 w->r = runerealloc(w->r, m);
1575 runemove(w->r+q0+n, w->r+q0, w->nr-q0);
1576 runemove(w->r+q0, r, n);
1578 /* if output touches, advance selection, not qh; works best for keyboard and output */
1587 else if(q0 <= w->org+w->f.nchars)
1588 frinsert(&w->f, r, r+n, q0-w->org);
1598 if(w->f.lastlinefull)
1600 rp = malloc(messagesize);
1602 n = w->nr-(w->org+w->f.nchars);
1605 if(n > 2000) /* educated guess at reasonable amount */
1607 runemove(rp, w->r+(w->org+w->f.nchars), n);
1609 * it's expensive to frinsert more than we need, so
1612 nl = w->f.maxlines-w->f.nlines;
1615 if(rp[i++] == '\n'){
1621 frinsert(&w->f, rp, rp+i, w->f.nchars);
1622 }while(w->f.lastlinefull == FALSE);
1627 wcontents(Window *w, int *ip)
1629 return runetobyte(w->r, w->nr, ip);