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))
312 m.counter = w->mouse.counter;
314 w->mouse.lastcounter = m.counter;
318 if(wctlmesg(w, wcm.type, wcm.r, wcm.image) == Exited){
346 while(i<nr-1 && *(bp+1) == '\r'){
350 if(i<nr-1 && *(bp+1) != '\n'){
351 while(up > rp && *(up-1) != '\n')
354 initial = wbswidth(w, '\r');
367 qh = w->qh - initial;
368 wdelete(w, qh, qh+initial);
372 w->qh = winsert(w, rp, nr, w->qh)+nr;
373 if(w->scrolling || w->mouseopen)
375 wsetselect(w, w->q0, w->q1);
387 while(i<nb && (w->qh<w->nr || w->nraw>0)){
389 wid = runetochar(t+i, &w->raw[0]);
391 runemove(w->raw, w->raw+1, w->nraw);
393 wid = runetochar(t+i, &w->r[w->qh++]);
394 c = t[i]; /* knows break characters fit in a byte */
396 if(!w->rawing && (c == '\n' || c=='\004')){
397 /* if(c == '\004') */
402 /* if(i==nb && w->qh<w->nr && w->r[w->qh]=='\004') */
406 memmove(part, t+nb, npart);
415 recv(cwrm.c1, &pair);
416 if(w->deleted || w->i==nil)
417 pair.ns = sprint(pair.s, "");
420 for(i=0; i<nhidden; i++)
428 pair.ns = snprint(pair.s, pair.ns, "%11d %11d %11d %11d %s %s ",
429 w->i->r.min.x, w->i->r.min.y, w->i->r.max.x, w->i->r.max.y, t, s);
431 send(cwrm.c2, &pair);
435 flushimage(display, 1);
440 waddraw(Window *w, Rune *r, int nr)
442 w->raw = runerealloc(w->raw, w->nraw+nr);
443 runemove(w->raw+w->nraw, r, nr);
448 * Need to do this in a separate proc because if process we're interrupting
449 * is dying and trying to print tombstone, kernel is blocked holding p->debug lock.
452 interruptproc(void *v)
457 write(*notefd, "interrupt", 9);
462 windfilewidth(Window *w, uint q0, int oneelement)
472 if(oneelement && r=='/')
480 showcandidates(Window *w, Completion *c)
490 s = "[no matches in ";
494 fmtprint(&f, "%s%d files]\n", s, c->nfile);
496 fmtprint(&f, "%s", s);
497 for(i=0; i<c->nfile; i++){
500 fmtprint(&f, "%s", c->filename[i]);
504 /* place text at beginning of line before host point */
506 while(qline>0 && w->r[qline-1] != '\n')
509 rp = runefmtstrflush(&f);
513 q0 += winsert(w, rp, runestrlen(rp), qline) - qline;
515 wsetselect(w, q0+nr, q0+nr);
519 namecomplete(Window *w)
522 Rune *rp, *path, *str;
524 char *s, *dir, *root;
526 /* control-f: filename completion; works back to white space or / */
527 if(w->q0<w->nr && w->r[w->q0]>' ') /* must be at end of word */
529 nstr = windfilewidth(w, w->q0, TRUE);
530 str = runemalloc(nstr);
531 runemove(str, w->r+(w->q0-nstr), nstr);
532 npath = windfilewidth(w, w->q0-nstr, FALSE);
533 path = runemalloc(npath);
534 runemove(path, w->r+(w->q0-nstr-npath), npath);
537 /* is path rooted? if not, we need to make it relative to window path */
538 if(npath>0 && path[0]=='/'){
539 dir = malloc(UTFmax*npath+1);
540 sprint(dir, "%.*S", npath, path);
542 if(strcmp(w->dir, "") == 0)
546 dir = malloc(strlen(root)+1+UTFmax*npath+1);
547 sprint(dir, "%s/%.*S", root, npath, path);
549 dir = cleanname(dir);
551 s = smprint("%.*S", nstr, str);
552 c = complete(dir, s);
558 showcandidates(w, c);
561 rp = runesmprint("%s", c->string);
572 wkeyctl(Window *w, Rune r)
583 /* navigation keys work only when mouse is not open */
590 n = mousescrollsize(w->f.maxlines);
595 n = 2*w->f.maxlines/3;
597 q0 = w->org+frcharofpt(&w->f, Pt(w->f.r.min.x, w->f.r.min.y+n*w->f.font->height));
598 wsetorigin(w, q0, TRUE);
604 n = mousescrollsize(w->f.maxlines);
609 n = 2*w->f.maxlines/3;
611 q0 = wbacknl(w, w->org, n);
612 wsetorigin(w, q0, TRUE);
617 wsetselect(w, q0, q0);
624 wsetselect(w, q1, q1);
634 case 0x01: /* ^A: beginning of line */
635 if(w->q0==0 || w->q0==w->qh || w->r[w->q0-1]=='\n')
637 nb = wbswidth(w, 0x15 /* ^U */);
638 wsetselect(w, w->q0-nb, w->q0-nb);
641 case 0x05: /* ^E: end of line */
643 while(q0 < w->nr && w->r[q0]!='\n')
645 wsetselect(w, q0, q0);
650 * This if used to be below the if(w->rawing ...),
651 * but let's try putting it here. This will allow ESC-processing
652 * to toggle hold mode even in remote SSH connections.
653 * The drawback is that vi-style processing gets harder.
654 * If you find yourself in some weird readline mode, good
655 * luck getting out without ESC. Let's see who complains.
657 if(r==0x1B || (w->holding && r==0x7F)){ /* toggle hold */
666 if(!w->holding && w->rawing && (w->q0==w->nr || w->mouseopen)){
675 case 0x7F: /* send interrupt */
680 case 0x06: /* ^F: file name completion */
681 case Kins: /* Insert: file name completion */
682 rp = namecomplete(w);
687 q0 = winsert(w, rp, nr, q0);
691 case 0x08: /* ^H: erase character */
692 case 0x15: /* ^U: erase line */
693 case 0x17: /* ^W: erase word */
694 if(w->q0==0 || w->q0==w->qh)
704 wdelete(w, q0, q0+nb);
705 wsetselect(w, q0, q0);
709 /* otherwise ordinary character; just insert */
711 q0 = winsert(w, &r, 1, q0);
720 w->f.cols[TEXT] = w->f.cols[HTEXT] = holdcol;
722 w->f.cols[TEXT] = w->f.cols[HTEXT] = lightholdcol;
725 w->f.cols[TEXT] = w->f.cols[HTEXT] = display->black;
727 w->f.cols[TEXT] = w->f.cols[HTEXT] = darkgrey;
735 if(font->maxdepth > 1)
736 draw(w->f.b, w->f.r, cols[BACK], nil, ZP);
737 _frredraw(&w->f, w->f.r.min);
740 wborder(w, Selborder);
743 wborder(w, Unselborder);
747 wbswidth(Window *w, Rune c)
753 /* there is known to be at least one character to erase */
754 if(c == 0x08) /* ^H: erase character */
763 if(r == '\n'){ /* eat at most one more character */
764 if(q == w->q0 && c != '\r') /* eat the newline */
770 if(eq && skipping) /* found one; stop skipping */
772 else if(!eq && !skipping)
785 nsnarf = w->q1-w->q0;
786 snarf = runerealloc(snarf, nsnarf);
787 snarfversion++; /* maybe modified by parent */
788 runemove(snarf, w->r+w->q0, nsnarf);
797 wdelete(w, w->q0, w->q1);
798 wsetselect(w, w->q0, w->q0);
810 if(w->rawing && !w->holding && q0==w->nr){
811 waddraw(w, snarf, nsnarf);
812 wsetselect(w, q0, q0);
814 q0 = winsert(w, snarf, nsnarf, w->q0);
815 wsetselect(w, q0, q0+nsnarf);
829 fd = plumbopenfid("send", OWRITE);
832 m = emalloc(sizeof(Plumbmsg));
833 m->src = estrdup("rio");
835 m->wdir = estrdup(w->dir);
836 m->type = estrdup("text");
842 while(p0>0 && w->r[p0-1]!=' ' && w->r[p0-1]!='\t' && w->r[p0-1]!='\n')
844 while(p1<w->nr && w->r[p1]!=' ' && w->r[p1]!='\t' && w->r[p1]!='\n')
846 sprint(buf, "click=%d", w->q0-p0);
847 m->attr = plumbunpackattr(buf);
849 if(p1-p0 > messagesize-1024){
851 return; /* too large for 9P */
853 m->data = runetobyte(w->r+p0, p1-p0, &m->ndata);
854 if(plumbsendtofid(fd, m) < 0){
856 riosetcursor(&query, 1);
864 winborder(Window *w, Point xy)
866 return ptinrect(xy, w->screenr) && !ptinrect(xy, insetrect(w->screenr, Selborder));
874 if(w->mc.m.buttons == 1)
876 else if(w->mc.m.buttons == 2)
878 else if(w->mc.m.buttons == 4)
881 if(w->mc.m.buttons == 8)
882 wkeyctl(w, Kscrolloneup);
883 if(w->mc.m.buttons == 16)
884 wkeyctl(w, Kscrollonedown);
888 incref(&w->ref); /* hold up window while we track */
891 if(ptinrect(w->mc.m.xy, w->scrollr)){
898 /* else all is handled by main process */
904 wdelete(Window *w, uint q0, uint q1)
911 runemove(w->r+q0, w->r+q1, w->nr-q1);
914 w->q0 -= min(n, w->q0-q0);
916 w->q1 -= min(n, w->q1-q0);
923 else if(q0 < w->org+w->f.nchars){
932 frdelete(&w->f, p0, p1);
938 static Window *clickwin;
939 static uint clickmsec;
940 static Window *selectwin;
944 * called from frame library
947 framescroll(Frame *f, int dl)
949 if(f != &selectwin->f)
950 error("frameselect not right frame");
951 wframescroll(selectwin, dl);
955 wframescroll(Window *w, int dl)
964 q0 = wbacknl(w, w->org, -dl);
965 if(selectq > w->org+w->f.p0)
966 wsetselect(w, w->org+w->f.p0, selectq);
968 wsetselect(w, selectq, w->org+w->f.p0);
970 if(w->org+w->f.nchars == w->nr)
972 q0 = w->org+frcharofpt(&w->f, Pt(w->f.r.min.x, w->f.r.min.y+dl*w->f.font->height));
973 if(selectq >= w->org+w->f.p1)
974 wsetselect(w, w->org+w->f.p1, selectq);
976 wsetselect(w, selectq, w->org+w->f.p1);
978 wsetorigin(w, q0, TRUE);
990 * Double-click immediately if it might make sense.
995 selectq = w->org+frcharofpt(&w->f, w->mc.m.xy);
996 if(clickwin==w && w->mc.m.msec-clickmsec<500)
997 if(q0==q1 && selectq==w->q0){
998 wdoubleclick(w, &q0, &q1);
999 wsetselect(w, q0, q1);
1000 flushimage(display, 1);
1003 /* stay here until something interesting happens */
1006 while(w->mc.m.buttons==b && abs(w->mc.m.xy.x-x)<3 && abs(w->mc.m.xy.y-y)<3);
1007 w->mc.m.xy.x = x; /* in case we're calling frselect */
1009 q0 = w->q0; /* may have changed */
1013 if(w->mc.m.buttons == b){
1014 w->f.scroll = framescroll;
1015 frselect(&w->f, &w->mc);
1016 /* horrible botch: while asleep, may have lost selection altogether */
1018 selectq = w->org + w->f.p0;
1020 if(selectq < w->org)
1023 q0 = w->org + w->f.p0;
1024 if(selectq > w->org+w->f.nchars)
1027 q1 = w->org+w->f.p1;
1030 if(q0==w->q0 && clickwin==w && w->mc.m.msec-clickmsec<500){
1031 wdoubleclick(w, &q0, &q1);
1035 clickmsec = w->mc.m.msec;
1039 wsetselect(w, q0, q1);
1040 flushimage(display, 1);
1041 while(w->mc.m.buttons){
1043 b = w->mc.m.buttons;
1057 flushimage(display, 1);
1058 while(w->mc.m.buttons == b)
1065 wsendctlmesg(Window *w, int type, Rectangle r, Image *image)
1072 send(w->cctl, &wcm);
1076 wctlmesg(Window *w, int m, Rectangle r, Image *i)
1082 error("unknown control message");
1093 strcpy(buf, w->name);
1094 wresize(w, i, m==Moved);
1099 }else if(w == input)
1101 flushimage(display, 1);
1104 if(w->deleted || Dx(w->screenr)<=0 || !rectclip(&r, w->i->r))
1108 flushimage(display, 1);
1111 if(sweeping || !ptinrect(r.min, w->i->r))
1113 wmovemouse(w, r.min);
1120 wkeyctl(w, w->raw[0]);
1122 runemove(w->raw, w->raw+1, w->nraw);
1130 flushimage(display, 1);
1135 write(w->notefd, "hangup", 6);
1139 frclear(&w->f, TRUE);
1144 chanfree(w->conswrite);
1145 chanfree(w->consread);
1146 chanfree(w->mouseread);
1147 chanfree(w->wctlread);
1159 * Convert back to physical coordinates
1162 wmovemouse(Window *w, Point p)
1164 p.x += w->screenr.min.x-w->i->r.min.x;
1165 p.y += w->screenr.min.y-w->i->r.min.y;
1166 moveto(mousectl, p);
1174 if(wkeyboard!=nil && w==wkeyboard)
1178 if(oi!=w && oi!=nil)
1187 wsendctlmesg(oi, Wakeup, ZR, nil);
1191 wsendctlmesg(w, Wakeup, ZR, nil);
1197 wsetcursor(Window *w, int force)
1201 if(w==nil || /*w!=input || */ w->i==nil || Dx(w->screenr)<=0)
1203 else if(wpointto(mouse->xy) == w){
1205 if(p==nil && w->holding)
1210 riosetcursor(p, force && !menuing);
1214 riosetcursor(Cursor *p, int force)
1216 if(!force && p==lastcursor)
1218 setcursor(mousectl, p);
1229 if(w->topped == topped)
1233 flushimage(display, 1);
1234 w->topped = ++topped;
1242 if(w!=nil && w->i!=nil && !w->deleted && w->topped!=topped){
1244 flushimage(display, 1);
1245 w->topped = ++ topped;
1250 wbottomme(Window *w)
1252 if(w!=nil && w->i!=nil && !w->deleted){
1254 flushimage(display, 1);
1264 for(i=0; i<nwindow; i++)
1265 if(window[i]->id == id)
1271 wclosewin(Window *w)
1283 for(i=0; i<nhidden; i++)
1286 memmove(hidden+i, hidden+i+1, (nhidden-i)*sizeof(hidden[0]));
1289 for(i=0; i<nwindow; i++)
1292 memmove(window+i, window+i+1, (nwindow-i)*sizeof(Window*));
1295 /* move it off-screen to hide it, in case client is slow in letting it go */
1296 MOVEIT originwindow(w->i, r.min, view->r.max);
1301 error("unknown window in closewin");
1305 wsetpid(Window *w, int pid, int dolabel)
1311 sprint(buf, "rc %d", pid);
1313 w->label = estrdup(buf);
1314 drawsetlabel(w->label);
1318 static Rune left1[] = {
1319 '{', '[', '(', '<', 0xAB,
1320 0x207d, 0x2329, 0x27e6, 0x27e8, 0x27ea,
1321 0xfe59, 0xfe5b, 0xfe5d, 0xff08, 0xff3b, 0xff5b,
1324 static Rune right1[] = {
1325 '}', ']', ')', '>', 0xBB,
1326 0x207e, 0x232a, 0x27e7, 0x27e9, 0x27eb,
1327 0xfe5a, 0xfe5c, 0xfe5e, 0xff09, 0xff3d, 0xff5d,
1330 static Rune left2[] = { '\n', 0 };
1331 static Rune left3[] = { '\'', '"', '`', 0 };
1347 wdoubleclick(Window *w, uint *q0, uint *q1)
1353 for(i=0; left[i]!=nil; i++){
1357 /* try matching character to left, looking right */
1364 if(wclickmatch(w, c, r[p-l], 1, &q))
1368 /* try matching character to right, looking left */
1375 if(wclickmatch(w, c, l[p-r], -1, &q)){
1376 *q1 = *q0+(*q0<w->nr && c=='\n');
1378 if(c!='\n' || q!=0 || w->r[0]=='\n')
1384 /* try filling out word to right */
1385 while(*q1<w->nr && isalnum(w->r[*q1]))
1387 /* try filling out word to left */
1388 while(*q0>0 && isalnum(w->r[*q0-1]))
1393 wclickmatch(Window *w, int cl, int cr, int dir, uint *q)
1417 return cl=='\n' && nest==1;
1422 wbacknl(Window *w, uint p, uint n)
1426 /* look for start of this line if n==0 */
1427 if(n==0 && p>0 && w->r[p-1]!='\n')
1430 while(i-->0 && p>0){
1431 --p; /* it's at a newline now; back over it */
1434 /* at 128 chars, call it a line anyway */
1435 for(j=128; --j>0 && p>0; p--)
1443 wshow(Window *w, uint q0)
1449 qe = w->org+w->f.nchars;
1450 if(w->org<=q0 && (q0<qe || (q0==qe && qe==w->nr)))
1453 nl = 4*w->f.maxlines/5;
1454 q = wbacknl(w, q0, nl);
1455 /* avoid going backwards if trying to go forwards - long lines! */
1456 if(!(q0>w->org && q<w->org))
1457 wsetorigin(w, q, TRUE);
1458 while(q0 > w->org+w->f.nchars)
1459 wsetorigin(w, w->org+1, FALSE);
1464 wsetorigin(Window *w, uint org, int exact)
1470 if(org>0 && !exact){
1471 /* org is an estimate of the char posn; find a newline */
1472 /* don't try harder than 256 chars */
1473 for(i=0; i<256 && org<w->nr; i++){
1474 if(w->r[org] == '\n'){
1483 if(a>=0 && a<w->f.nchars){
1484 frdelete(&w->f, 0, a);
1485 fixup = 1; /* frdelete can leave end of last line in wrong selection mode; it doesn't know what follows */
1486 }else if(a<0 && -a<w->f.nchars){
1489 runemove(r, w->r+org, n);
1490 frinsert(&w->f, r, r+n, 0);
1493 frdelete(&w->f, 0, w->f.nchars);
1497 wsetselect(w, w->q0, w->q1);
1498 if(fixup && w->f.p1 > w->f.p0)
1499 frdrawsel(&w->f, frptofchar(&w->f, w->f.p1-1), w->f.p1-1, w->f.p1, 1);
1503 wsetselect(Window *w, uint q0, uint q1)
1507 /* w->f.p0 and w->f.p1 are always right; w->q0 and w->q1 may be off */
1510 /* compute desired p0,p1 from q0,q1 */
1517 if(p0 > w->f.nchars)
1519 if(p1 > w->f.nchars)
1521 if(p0==w->f.p0 && p1==w->f.p1)
1523 /* screen disagrees with desired selection */
1524 if(w->f.p1<=p0 || p1<=w->f.p0 || p0==p1 || w->f.p1==w->f.p0){
1525 /* no overlap or too easy to bother trying */
1526 frdrawsel(&w->f, frptofchar(&w->f, w->f.p0), w->f.p0, w->f.p1, 0);
1527 frdrawsel(&w->f, frptofchar(&w->f, p0), p0, p1, 1);
1530 /* overlap; avoid unnecessary painting */
1532 /* extend selection backwards */
1533 frdrawsel(&w->f, frptofchar(&w->f, p0), p0, w->f.p0, 1);
1534 }else if(p0 > w->f.p0){
1535 /* trim first part of selection */
1536 frdrawsel(&w->f, frptofchar(&w->f, w->f.p0), w->f.p0, p0, 0);
1539 /* extend selection forwards */
1540 frdrawsel(&w->f, frptofchar(&w->f, w->f.p1), w->f.p1, p1, 1);
1541 }else if(p1 < w->f.p1){
1542 /* trim last part of selection */
1543 frdrawsel(&w->f, frptofchar(&w->f, p1), p1, w->f.p1, 0);
1552 winsert(Window *w, Rune *r, int n, uint q0)
1558 if(w->nr+n>HiWater && q0>=w->org && q0>=w->qh){
1559 m = min(HiWater-LoWater, min(w->org, w->qh));
1571 runemove(w->r, w->r+m, w->nr);
1574 if(w->nr+n > w->maxr){
1576 * Minimize realloc breakage:
1577 * Allocate at least MinWater
1578 * Double allocation size each time
1579 * But don't go much above HiWater
1581 m = max(min(2*(w->nr+n), HiWater), w->nr+n)+MinWater;
1583 m = max(HiWater+MinWater, w->nr+n);
1585 w->r = runerealloc(w->r, m);
1589 runemove(w->r+q0+n, w->r+q0, w->nr-q0);
1590 runemove(w->r+q0, r, n);
1592 /* if output touches, advance selection, not qh; works best for keyboard and output */
1601 else if(q0 <= w->org+w->f.nchars)
1602 frinsert(&w->f, r, r+n, q0-w->org);
1612 if(w->f.lastlinefull)
1614 rp = malloc(messagesize);
1616 n = w->nr-(w->org+w->f.nchars);
1619 if(n > 2000) /* educated guess at reasonable amount */
1621 runemove(rp, w->r+(w->org+w->f.nchars), n);
1623 * it's expensive to frinsert more than we need, so
1626 nl = w->f.maxlines-w->f.nlines;
1629 if(rp[i++] == '\n'){
1635 frinsert(&w->f, rp, rp+i, w->f.nchars);
1636 }while(w->f.lastlinefull == FALSE);
1641 wcontents(Window *w, int *ip)
1643 return runetobyte(w->r, w->nr, ip);