20 HiWater = 64000000, /* 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 wscale(Window *w, int n)
41 if(w == nil || w->i == nil)
43 return scalesize(w->i->display, n);
47 wmk(Image *i, Mousectl *mc, Channel *ck, Channel *cctl, int scrolling)
53 /* greys are multiples of 0x11111100+0xFF, 14* being palest */
54 grey = allocimage(display, Rect(0,0,1,1), CMAP8, 1, 0xEEEEEEFF);
55 darkgrey = allocimage(display, Rect(0,0,1,1), CMAP8, 1, 0x666666FF);
56 cols[BACK] = display->white;
57 cols[HIGH] = allocimage(display, Rect(0,0,1,1), CMAP8, 1, 0xCCCCCCFF);
58 cols[BORD] = allocimage(display, Rect(0,0,1,1), CMAP8, 1, 0x999999FF);
59 cols[TEXT] = display->black;
60 cols[HTEXT] = display->black;
61 titlecol = allocimage(display, Rect(0,0,1,1), CMAP8, 1, DGreygreen);
62 lighttitlecol = allocimage(display, Rect(0,0,1,1), CMAP8, 1, DPalegreygreen);
63 holdcol = allocimage(display, Rect(0,0,1,1), CMAP8, 1, DMedblue);
64 lightholdcol = allocimage(display, Rect(0,0,1,1), CMAP8, 1, DGreyblue);
65 paleholdcol = allocimage(display, Rect(0,0,1,1), CMAP8, 1, DPalegreyblue);
67 w = emalloc(sizeof(Window));
69 r = insetrect(i->r, wscale(w, Selborder)+wscale(w, 1));
75 w->conswrite = chancreate(sizeof(Conswritemesg), 0);
76 w->consread = chancreate(sizeof(Consreadmesg), 0);
77 w->mouseread = chancreate(sizeof(Mousereadmesg), 0);
78 w->wctlread = chancreate(sizeof(Consreadmesg), 0);
80 w->scrollr.max.x = r.min.x+wscale(w, Scrollwid);
82 r.min.x += wscale(w, Scrollwid)+wscale(w, Scrollgap);
83 frinit(&w->f, r, font, i, cols);
84 w->f.maxtab = maxtab*stringwidth(font, "0");
88 w->scrolling = scrolling;
89 w->dir = estrdup(startdir);
90 w->label = estrdup("<unnamed>");
91 r = insetrect(w->i->r, wscale(w, Selborder));
92 draw(w->i, r, cols[BACK], nil, w->f.entire.min);
93 wborder(w, wscale(w, Selborder));
95 incref(&w->ref); /* ref will be removed after mounting; avoids delete before ready to be deleted */
105 n = sprint(w->name, "window.%d.%d", w->id, w->namecount++);
106 for(i='A'; i<='Z'; i++){
107 if(nameimage(w->i, w->name, 1) > 0)
109 errstr(err, sizeof err);
110 if(strcmp(err, "image name in use") != 0)
116 fprint(2, "rio: setname failed: %s\n", err);
120 wresize(Window *w, Image *i, int move)
125 if(move || (Dx(or)==Dx(i->r) && Dy(or)==Dy(i->r)))
126 draw(i, i->r, w->i, nil, w->i->r.min);
128 fprint(2, "res %p %p\n", w->i, i);
133 /*XXX w->mc.image = i; */
134 r = insetrect(i->r, wscale(w, Selborder)+wscale(w, 1));
136 w->scrollr.max.x = r.min.x+wscale(w, Scrollwid);
138 r.min.x += wscale(w, Scrollwid)+wscale(w, Scrollgap);
140 frsetrects(&w->f, r, w->i);
142 frclear(&w->f, FALSE);
143 frinit(&w->f, r, w->f.font, w->i, cols);
145 w->f.maxtab = maxtab*stringwidth(w->f.font, "0");
146 r = insetrect(w->i->r, wscale(w, Selborder));
147 draw(w->i, r, cols[BACK], nil, w->f.entire.min);
149 wsetselect(w, w->q0, w->q1);
152 wborder(w, wscale(w, Selborder));
153 w->topped = ++topped;
159 wrefresh(Window *w, Rectangle r)
163 /* BUG: rectangle is ignored */
165 wborder(w, wscale(w, Selborder));
167 wborder(w, wscale(w, Unselborder));
170 draw(w->i, insetrect(w->i->r, Borderwidth), w->f.cols[BACK], nil, w->i->r.min);
173 frdrawsel(&w->f, frptofchar(&w->f, 0), 0, w->f.p0, 0);
174 if(w->f.p1 < w->f.nchars)
175 frdrawsel(&w->f, frptofchar(&w->f, w->f.p1), w->f.p1, w->f.nchars, 0);
176 frdrawsel(&w->f, frptofchar(&w->f, w->f.p0), w->f.p0, w->f.p1, 1);
190 error("negative ref count");
193 wsendctlmesg(w, Exited, ZR, nil);
201 Rune *rp, *bp, *up, *kbdr;
203 int nr, nb, c, wid, i, npart, initial, lastb, scrolling;
204 char *s, *t, part[UTFmax];
207 enum { WKey, WMouse, WMouseread, WCtl, WCwrite, WCread, WWread, NWALT };
218 snprint(buf, sizeof buf, "winctl-id%d", w->id);
221 mrm.cm = chancreate(sizeof(Mouse), 0);
222 cwm.cw = chancreate(sizeof(Stringpair), 0);
223 crm.c1 = chancreate(sizeof(Stringpair), 0);
224 crm.c2 = chancreate(sizeof(Stringpair), 0);
225 cwrm.c1 = chancreate(sizeof(Stringpair), 0);
226 cwrm.c2 = chancreate(sizeof(Stringpair), 0);
229 alts[WKey].c = w->ck;
230 alts[WKey].v = &kbdr;
231 alts[WKey].op = CHANRCV;
232 alts[WMouse].c = w->mc.c;
233 alts[WMouse].v = &w->mc.m;
234 alts[WMouse].op = CHANRCV;
235 alts[WMouseread].c = w->mouseread;
236 alts[WMouseread].v = &mrm;
237 alts[WMouseread].op = CHANSND;
238 alts[WCtl].c = w->cctl;
240 alts[WCtl].op = CHANRCV;
241 alts[WCwrite].c = w->conswrite;
242 alts[WCwrite].v = &cwm;
243 alts[WCwrite].op = CHANSND;
244 alts[WCread].c = w->consread;
245 alts[WCread].v = &crm;
246 alts[WCread].op = CHANSND;
247 alts[WWread].c = w->wctlread;
248 alts[WWread].v = &cwrm;
249 alts[WWread].op = CHANSND;
250 alts[NWALT].op = CHANEND;
255 if(w->mouseopen && w->mouse.counter != w->mouse.lastcounter)
256 alts[WMouseread].op = CHANSND;
258 alts[WMouseread].op = CHANNOP;
259 // if(!w->scrolling && !w->mouseopen && w->qh>w->org+w->f.nchars)
260 // alts[WCwrite].op = CHANNOP;
262 alts[WCwrite].op = CHANSND;
263 if(w->deleted || !w->wctlready)
264 alts[WWread].op = CHANNOP;
266 alts[WWread].op = CHANSND;
267 /* this code depends on NL and EOT fitting in a single byte */
268 /* kind of expensive for each loop; worth precomputing? */
270 alts[WCread].op = CHANNOP;
271 else if(npart || (w->rawing && w->nraw>0))
272 alts[WCread].op = CHANSND;
274 alts[WCread].op = CHANNOP;
275 for(i=w->qh; i<w->nr; i++){
277 if(c=='\n' || c=='\004'){
278 alts[WCread].op = CHANSND;
285 for(i=0; kbdr[i]!=L'\0'; i++)
288 /* while(nbrecv(w->ck, &r)) */
295 /* queue click events */
296 if(!w->mouse.qfull && lastb != w->mc.m.buttons) { /* add to ring */
297 mp = &w->mouse.queue[w->mouse.wi];
298 if(++w->mouse.wi == nelem(w->mouse.queue))
300 if(w->mouse.wi == w->mouse.ri)
301 w->mouse.qfull = TRUE;
303 mp->counter = w->mouse.counter;
304 lastb = w->mc.m.buttons;
310 /* send a queued event or, if the queue is empty, the current state */
311 /* if the queue has filled, we discard all the events it contained. */
312 /* the intent is to discard frantic clicking by the user during long latencies. */
313 w->mouse.qfull = FALSE;
314 if(w->mouse.wi != w->mouse.ri) {
315 m = w->mouse.queue[w->mouse.ri];
316 if(++w->mouse.ri == nelem(w->mouse.queue))
320 m.counter = w->mouse.counter;
322 w->mouse.lastcounter = m.counter;
326 if(wctlmesg(w, wcm.type, wcm.r, wcm.image) == Exited){
354 while(i<nr-1 && *(bp+1) == '\r'){
358 if(i<nr-1 && *(bp+1) != '\n'){
359 while(up > rp && *(up-1) != '\n')
362 initial = wbswidth(w, '\r');
375 qh = w->qh - initial;
376 wdelete(w, qh, qh+initial);
380 scrolling = w->scrolling && w->org <= w->qh && w->qh <= w->org + w->f.nchars;
381 w->qh = winsert(w, rp, nr, w->qh)+nr;
384 wsetselect(w, w->q0, w->q1);
396 while(i<nb && (w->qh<w->nr || w->nraw>0)){
398 wid = runetochar(t+i, &w->raw[0]);
400 runemove(w->raw, w->raw+1, w->nraw);
402 wid = runetochar(t+i, &w->r[w->qh++]);
403 c = t[i]; /* knows break characters fit in a byte */
405 if(!w->rawing && (c == '\n' || c=='\004')){
406 /* if(c == '\004') */
411 /* if(i==nb && w->qh<w->nr && w->r[w->qh]=='\004') */
415 memmove(part, t+nb, npart);
424 recv(cwrm.c1, &pair);
425 if(w->deleted || w->i==nil)
426 pair.ns = sprint(pair.s, "");
429 for(i=0; i<nhidden; i++)
437 pair.ns = snprint(pair.s, pair.ns, "%11d %11d %11d %11d %s %s ",
438 w->i->r.min.x, w->i->r.min.y, w->i->r.max.x, w->i->r.max.y, t, s);
440 send(cwrm.c2, &pair);
444 flushimage(display, 1);
449 waddraw(Window *w, Rune *r, int nr)
451 w->raw = runerealloc(w->raw, w->nraw+nr);
452 runemove(w->raw+w->nraw, r, nr);
457 * Need to do this in a separate proc because if process we're interrupting
458 * is dying and trying to print tombstone, kernel is blocked holding p->debug lock.
461 interruptproc(void *v)
466 write(*notefd, "interrupt", 9);
471 windfilewidth(Window *w, uint q0, int oneelement)
481 if(oneelement && r=='/')
489 showcandidates(Window *w, Completion *c)
499 s = "[no matches in ";
503 fmtprint(&f, "%s%d files]\n", s, c->nfile);
505 fmtprint(&f, "%s", s);
506 for(i=0; i<c->nfile; i++){
509 fmtprint(&f, "%s", c->filename[i]);
513 /* place text at beginning of line before host point */
515 while(qline>0 && w->r[qline-1] != '\n')
518 rp = runefmtstrflush(&f);
522 q0 += winsert(w, rp, runestrlen(rp), qline) - qline;
524 wsetselect(w, q0+nr, q0+nr);
528 namecomplete(Window *w)
531 Rune *rp, *path, *str;
533 char *s, *dir, *root;
535 /* control-f: filename completion; works back to white space or / */
536 if(w->q0<w->nr && w->r[w->q0]>' ') /* must be at end of word */
538 nstr = windfilewidth(w, w->q0, TRUE);
539 str = runemalloc(nstr);
540 runemove(str, w->r+(w->q0-nstr), nstr);
541 npath = windfilewidth(w, w->q0-nstr, FALSE);
542 path = runemalloc(npath);
543 runemove(path, w->r+(w->q0-nstr-npath), npath);
546 /* is path rooted? if not, we need to make it relative to window path */
547 if(npath>0 && path[0]=='/'){
548 dir = malloc(UTFmax*npath+1);
549 sprint(dir, "%.*S", npath, path);
551 if(strcmp(w->dir, "") == 0)
555 dir = malloc(strlen(root)+1+UTFmax*npath+1);
556 sprint(dir, "%s/%.*S", root, npath, path);
558 dir = cleanname(dir);
560 s = smprint("%.*S", nstr, str);
561 c = complete(dir, s);
567 showcandidates(w, c);
570 rp = runesmprint("%s", c->string);
581 wkeyctl(Window *w, Rune r)
592 /* navigation keys work only when mouse is not open */
599 n = mousescrollsize(w->f.maxlines);
604 n = 2*w->f.maxlines/3;
606 q0 = w->org+frcharofpt(&w->f, Pt(w->f.r.min.x, w->f.r.min.y+n*w->f.font->height));
607 wsetorigin(w, q0, TRUE);
613 n = mousescrollsize(w->f.maxlines);
618 n = 2*w->f.maxlines/3;
620 q0 = wbacknl(w, w->org, n);
621 wsetorigin(w, q0, TRUE);
626 wsetselect(w, q0, q0);
633 wsetselect(w, q1, q1);
638 if(w->org > w->iq1) {
639 q0 = wbacknl(w, w->iq1, 1);
640 wsetorigin(w, q0, TRUE);
645 if(w->iq1 > w->org+w->f.nchars) {
646 q0 = wbacknl(w, w->iq1, 1);
647 wsetorigin(w, q0, TRUE);
651 case 0x01: /* ^A: beginning of line */
652 if(w->q0==0 || w->q0==w->qh || w->r[w->q0-1]=='\n')
654 nb = wbswidth(w, 0x15 /* ^U */);
655 wsetselect(w, w->q0-nb, w->q0-nb);
658 case 0x05: /* ^E: end of line */
660 while(q0 < w->nr && w->r[q0]!='\n')
662 wsetselect(w, q0, q0);
667 * This if used to be below the if(w->rawing ...),
668 * but let's try putting it here. This will allow ESC-processing
669 * to toggle hold mode even in remote SSH connections.
670 * The drawback is that vi-style processing gets harder.
671 * If you find yourself in some weird readline mode, good
672 * luck getting out without ESC. Let's see who complains.
674 if(r==0x1B || (w->holding && r==0x7F)){ /* toggle hold */
683 if(!w->holding && w->rawing && (w->q0==w->nr || w->mouseopen)){
708 case 0x03: /* maybe send interrupt */
709 /* since ^C is so commonly used as interrupt, special case it */
713 case 0x7F: /* send interrupt */
719 case 0x06: /* ^F: file name completion */
720 case Kins: /* Insert: file name completion */
721 rp = namecomplete(w);
726 q0 = winsert(w, rp, nr, q0);
731 case 0x08: /* ^H: erase character */
732 case 0x15: /* ^U: erase line */
733 case 0x17: /* ^W: erase word */
734 if(w->q0==0 || w->q0==w->qh)
744 wdelete(w, q0, q0+nb);
745 wsetselect(w, q0, q0);
750 /* otherwise ordinary character; just insert */
752 q0 = winsert(w, &r, 1, q0);
762 w->f.cols[TEXT] = w->f.cols[HTEXT] = holdcol;
764 w->f.cols[TEXT] = w->f.cols[HTEXT] = lightholdcol;
767 w->f.cols[TEXT] = w->f.cols[HTEXT] = display->black;
769 w->f.cols[TEXT] = w->f.cols[HTEXT] = darkgrey;
780 wborder(w, wscale(w, Selborder));
783 wborder(w, wscale(w, Unselborder));
787 wbswidth(Window *w, Rune c)
793 /* there is known to be at least one character to erase */
794 if(c == 0x08) /* ^H: erase character */
803 if(r == '\n'){ /* eat at most one more character */
804 if(q == w->q0 && c != '\r') /* eat the newline */
810 if(eq && skipping) /* found one; stop skipping */
812 else if(!eq && !skipping)
825 nsnarf = w->q1-w->q0;
826 snarf = runerealloc(snarf, nsnarf);
827 snarfversion++; /* maybe modified by parent */
828 runemove(snarf, w->r+w->q0, nsnarf);
837 wdelete(w, w->q0, w->q1);
838 wsetselect(w, w->q0, w->q0);
850 if(w->rawing && !w->holding && q0==w->nr){
851 waddraw(w, snarf, nsnarf);
852 wsetselect(w, q0, q0);
854 q0 = winsert(w, snarf, nsnarf, w->q0);
855 wsetselect(w, q0, q0+nsnarf);
869 fd = plumbopenfid("send", OWRITE);
872 m = emalloc(sizeof(Plumbmsg));
873 m->src = estrdup("rio");
875 m->wdir = estrdup(w->dir);
876 m->type = estrdup("text");
882 while(p0>0 && w->r[p0-1]!=' ' && w->r[p0-1]!='\t' && w->r[p0-1]!='\n')
884 while(p1<w->nr && w->r[p1]!=' ' && w->r[p1]!='\t' && w->r[p1]!='\n')
886 sprint(buf, "click=%d", w->q0-p0);
887 m->attr = plumbunpackattr(buf);
889 if(p1-p0 > messagesize-1024){
891 return; /* too large for 9P */
893 m->data = runetobyte(w->r+p0, p1-p0, &m->ndata);
894 if(plumbsendtofid(fd, m) < 0){
896 riosetcursor(&query, 1);
904 winborder(Window *w, Point xy)
906 return ptinrect(xy, w->screenr) && !ptinrect(xy, insetrect(w->screenr, wscale(w, Selborder)));
923 while(runestrncmp(w->r+w->q0, w->r+i, n) != 0){
930 wsetselect(w, i, i+n);
939 if(w->mc.m.buttons == 1)
941 else if(w->mc.m.buttons == 2)
943 else if(w->mc.m.buttons == 4)
946 if(w->mc.m.buttons == 8)
947 wkeyctl(w, Kscrolloneup);
948 if(w->mc.m.buttons == 16)
949 wkeyctl(w, Kscrollonedown);
953 incref(&w->ref); /* hold up window while we track */
956 if(ptinrect(w->mc.m.xy, w->scrollr)){
963 /* else all is handled by main process */
969 wdelete(Window *w, uint q0, uint q1)
976 runemove(w->r+q0, w->r+q1, w->nr-q1);
979 w->iq1 -= min(n, w->iq1-q0);
981 w->q0 -= min(n, w->q0-q0);
983 w->q1 -= min(n, w->q1-q0);
990 else if(q0 < w->org+w->f.nchars){
999 frdelete(&w->f, p0, p1);
1005 static Window *clickwin;
1006 static uint clickmsec;
1007 static Window *selectwin;
1008 static uint selectq;
1011 * called from frame library
1014 framescroll(Frame *f, int dl)
1016 if(f != &selectwin->f)
1017 error("frameselect not right frame");
1018 wframescroll(selectwin, dl);
1022 wframescroll(Window *w, int dl)
1031 q0 = wbacknl(w, w->org, -dl);
1032 if(selectq > w->org+w->f.p0)
1033 wsetselect(w, w->org+w->f.p0, selectq);
1035 wsetselect(w, selectq, w->org+w->f.p0);
1037 if(w->org+w->f.nchars == w->nr)
1039 q0 = w->org+frcharofpt(&w->f, Pt(w->f.r.min.x, w->f.r.min.y+dl*w->f.font->height));
1040 if(selectq >= w->org+w->f.p1)
1041 wsetselect(w, w->org+w->f.p1, selectq);
1043 wsetselect(w, selectq, w->org+w->f.p1);
1045 wsetorigin(w, q0, TRUE);
1057 * Double-click immediately if it might make sense.
1059 b = w->mc.m.buttons;
1062 selectq = w->org+frcharofpt(&w->f, w->mc.m.xy);
1063 if(clickwin==w && w->mc.m.msec-clickmsec<500)
1064 if(q0==q1 && selectq==w->q0){
1065 wdoubleclick(w, &q0, &q1);
1066 wsetselect(w, q0, q1);
1067 flushimage(display, 1);
1070 /* stay here until something interesting happens */
1073 while(w->mc.m.buttons==b && abs(w->mc.m.xy.x-x)<3 && abs(w->mc.m.xy.y-y)<3);
1074 w->mc.m.xy.x = x; /* in case we're calling frselect */
1076 q0 = w->q0; /* may have changed */
1080 if(w->mc.m.buttons == b){
1081 w->f.scroll = framescroll;
1082 frselect(&w->f, &w->mc);
1083 /* horrible botch: while asleep, may have lost selection altogether */
1085 selectq = w->org + w->f.p0;
1087 if(selectq < w->org)
1090 q0 = w->org + w->f.p0;
1091 if(selectq > w->org+w->f.nchars)
1094 q1 = w->org+w->f.p1;
1097 if(q0==w->q0 && clickwin==w && w->mc.m.msec-clickmsec<500){
1098 wdoubleclick(w, &q0, &q1);
1102 clickmsec = w->mc.m.msec;
1106 wsetselect(w, q0, q1);
1107 flushimage(display, 1);
1108 while(w->mc.m.buttons){
1110 b = w->mc.m.buttons;
1124 flushimage(display, 1);
1125 while(w->mc.m.buttons == b)
1132 wsendctlmesg(Window *w, int type, Rectangle r, Image *image)
1139 send(w->cctl, &wcm);
1143 wctlmesg(Window *w, int m, Rectangle r, Image *i)
1149 error("unknown control message");
1160 strcpy(buf, w->name);
1161 wresize(w, i, m==Moved);
1166 }else if(w == input)
1168 flushimage(display, 1);
1171 if(w->deleted || Dx(w->screenr)<=0 || !rectclip(&r, w->i->r))
1175 flushimage(display, 1);
1178 if(sweeping || !ptinrect(r.min, w->i->r))
1180 wmovemouse(w, r.min);
1187 wkeyctl(w, w->raw[0]);
1189 runemove(w->raw, w->raw+1, w->nraw);
1197 flushimage(display, 1);
1202 write(w->notefd, "hangup", 6);
1206 frclear(&w->f, TRUE);
1211 chanfree(w->conswrite);
1212 chanfree(w->consread);
1213 chanfree(w->mouseread);
1214 chanfree(w->wctlread);
1226 * Convert back to physical coordinates
1229 wmovemouse(Window *w, Point p)
1231 p.x += w->screenr.min.x-w->i->r.min.x;
1232 p.y += w->screenr.min.y-w->i->r.min.y;
1233 moveto(mousectl, p);
1241 if(wkeyboard!=nil && w==wkeyboard)
1245 if(oi!=w && oi!=nil)
1254 wsendctlmesg(oi, Wakeup, ZR, nil);
1258 wsendctlmesg(w, Wakeup, ZR, nil);
1264 wsetcursor(Window *w, int force)
1268 if(w==nil || /*w!=input || */ w->i==nil || Dx(w->screenr)<=0)
1270 else if(wpointto(mouse->xy) == w){
1272 if(p==nil && w->holding)
1277 riosetcursor(p, force && !menuing);
1281 riosetcursor(Cursor *p, int force)
1283 if(!force && p==lastcursor)
1285 setcursor(mousectl, p);
1296 if(w->topped == topped)
1300 flushimage(display, 1);
1301 w->topped = ++topped;
1309 if(w!=nil && w->i!=nil && !w->deleted && w->topped!=topped){
1311 flushimage(display, 1);
1312 w->topped = ++ topped;
1317 wbottomme(Window *w)
1319 if(w!=nil && w->i!=nil && !w->deleted){
1321 flushimage(display, 1);
1331 for(i=0; i<nwindow; i++)
1332 if(window[i]->id == id)
1338 wclosewin(Window *w)
1350 for(i=0; i<nhidden; i++)
1353 memmove(hidden+i, hidden+i+1, (nhidden-i)*sizeof(hidden[0]));
1356 for(i=0; i<nwindow; i++)
1359 memmove(window+i, window+i+1, (nwindow-i)*sizeof(Window*));
1362 /* move it off-screen to hide it, in case client is slow in letting it go */
1363 MOVEIT originwindow(w->i, r.min, view->r.max);
1368 error("unknown window in closewin");
1372 wsetpid(Window *w, int pid, int dolabel)
1378 sprint(buf, "rc %d", pid);
1380 w->label = estrdup(buf);
1381 drawsetlabel(w->label);
1385 static Rune left1[] = {
1386 '{', '[', '(', '<', 0xAB,
1387 0x207d, 0x2329, 0x27e6, 0x27e8, 0x27ea,
1388 0xfe59, 0xfe5b, 0xfe5d, 0xff08, 0xff3b, 0xff5b,
1391 static Rune right1[] = {
1392 '}', ']', ')', '>', 0xBB,
1393 0x207e, 0x232a, 0x27e7, 0x27e9, 0x27eb,
1394 0xfe5a, 0xfe5c, 0xfe5e, 0xff09, 0xff3d, 0xff5d,
1397 static Rune left2[] = { '\n', 0 };
1398 static Rune left3[] = { '\'', '"', '`', 0 };
1414 wdoubleclick(Window *w, uint *q0, uint *q1)
1420 for(i=0; left[i]!=nil; i++){
1424 /* try matching character to left, looking right */
1431 if(wclickmatch(w, c, r[p-l], 1, &q))
1435 /* try matching character to right, looking left */
1442 if(wclickmatch(w, c, l[p-r], -1, &q)){
1443 *q1 = *q0+(*q0<w->nr && c=='\n');
1445 if(c!='\n' || q!=0 || w->r[0]=='\n')
1451 /* try filling out word to right */
1452 while(*q1<w->nr && isalnum(w->r[*q1]))
1454 /* try filling out word to left */
1455 while(*q0>0 && isalnum(w->r[*q0-1]))
1460 wclickmatch(Window *w, int cl, int cr, int dir, uint *q)
1484 return cl=='\n' && nest==1;
1489 wbacknl(Window *w, uint p, uint n)
1493 /* look for start of this line if n==0 */
1494 if(n==0 && p>0 && w->r[p-1]!='\n')
1497 while(i-->0 && p>0){
1498 --p; /* it's at a newline now; back over it */
1501 /* at 128 chars, call it a line anyway */
1502 for(j=128; --j>0 && p>0; p--)
1510 wshow(Window *w, uint q0)
1516 qe = w->org+w->f.nchars;
1517 if(w->org<=q0 && (q0<qe || (q0==qe && qe==w->nr)))
1520 nl = 4*w->f.maxlines/5;
1521 q = wbacknl(w, q0, nl);
1522 /* avoid going backwards if trying to go forwards - long lines! */
1523 if(!(q0>w->org && q<w->org))
1524 wsetorigin(w, q, TRUE);
1525 while(q0 > w->org+w->f.nchars)
1526 wsetorigin(w, w->org+1, FALSE);
1531 wsetorigin(Window *w, uint org, int exact)
1537 if(org>0 && !exact){
1538 /* org is an estimate of the char posn; find a newline */
1539 /* don't try harder than 256 chars */
1540 for(i=0; i<256 && org<w->nr; i++){
1541 if(w->r[org] == '\n'){
1550 if(a>=0 && a<w->f.nchars){
1551 frdelete(&w->f, 0, a);
1552 fixup = 1; /* frdelete can leave end of last line in wrong selection mode; it doesn't know what follows */
1553 }else if(a<0 && -a<w->f.nchars){
1556 runemove(r, w->r+org, n);
1557 frinsert(&w->f, r, r+n, 0);
1560 frdelete(&w->f, 0, w->f.nchars);
1564 wsetselect(w, w->q0, w->q1);
1565 if(fixup && w->f.p1 > w->f.p0)
1566 frdrawsel(&w->f, frptofchar(&w->f, w->f.p1-1), w->f.p1-1, w->f.p1, 1);
1570 wsetselect(Window *w, uint q0, uint q1)
1574 /* w->f.p0 and w->f.p1 are always right; w->q0 and w->q1 may be off */
1577 /* compute desired p0,p1 from q0,q1 */
1584 if(p0 > w->f.nchars)
1586 if(p1 > w->f.nchars)
1588 if(p0==w->f.p0 && p1==w->f.p1)
1590 /* screen disagrees with desired selection */
1591 if(w->f.p1<=p0 || p1<=w->f.p0 || p0==p1 || w->f.p1==w->f.p0){
1592 /* no overlap or too easy to bother trying */
1593 frdrawsel(&w->f, frptofchar(&w->f, w->f.p0), w->f.p0, w->f.p1, 0);
1594 frdrawsel(&w->f, frptofchar(&w->f, p0), p0, p1, 1);
1597 /* overlap; avoid unnecessary painting */
1599 /* extend selection backwards */
1600 frdrawsel(&w->f, frptofchar(&w->f, p0), p0, w->f.p0, 1);
1601 }else if(p0 > w->f.p0){
1602 /* trim first part of selection */
1603 frdrawsel(&w->f, frptofchar(&w->f, w->f.p0), w->f.p0, p0, 0);
1606 /* extend selection forwards */
1607 frdrawsel(&w->f, frptofchar(&w->f, w->f.p1), w->f.p1, p1, 1);
1608 }else if(p1 < w->f.p1){
1609 /* trim last part of selection */
1610 frdrawsel(&w->f, frptofchar(&w->f, p1), p1, w->f.p1, 0);
1619 winsert(Window *w, Rune *r, int n, uint q0)
1625 if(w->nr+n>HiWater && q0>=w->org && q0>=w->qh){
1626 m = min(HiWater-LoWater, min(w->org, w->qh));
1638 runemove(w->r, w->r+m, w->nr);
1641 if(w->nr+n > w->maxr){
1643 * Minimize realloc breakage:
1644 * Allocate at least MinWater
1645 * Double allocation size each time
1646 * But don't go much above HiWater
1648 m = max(min(2*(w->nr+n), HiWater), w->nr+n)+MinWater;
1650 m = max(HiWater+MinWater, w->nr+n);
1652 w->r = runerealloc(w->r, m);
1656 runemove(w->r+q0+n, w->r+q0, w->nr-q0);
1657 runemove(w->r+q0, r, n);
1659 /* if output touches, advance selection, not qh; works best for keyboard and output */
1670 else if(q0 <= w->org+w->f.nchars)
1671 frinsert(&w->f, r, r+n, q0-w->org);
1681 if(w->f.lastlinefull)
1683 rp = malloc(messagesize);
1685 n = w->nr-(w->org+w->f.nchars);
1688 if(n > 2000) /* educated guess at reasonable amount */
1690 runemove(rp, w->r+(w->org+w->f.nchars), n);
1692 * it's expensive to frinsert more than we need, so
1695 nl = w->f.maxlines-w->f.nlines;
1698 if(rp[i++] == '\n'){
1704 frinsert(&w->f, rp, rp+i, w->f.nchars);
1705 }while(w->f.lastlinefull == FALSE);
1710 wcontents(Window *w, int *ip)
1712 return runetobyte(w->r, w->nr, ip);