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 0x7F: /* send interrupt */
714 case 0x06: /* ^F: file name completion */
715 case Kins: /* Insert: file name completion */
716 rp = namecomplete(w);
721 q0 = winsert(w, rp, nr, q0);
726 case 0x08: /* ^H: erase character */
727 case 0x15: /* ^U: erase line */
728 case 0x17: /* ^W: erase word */
729 if(w->q0==0 || w->q0==w->qh)
739 wdelete(w, q0, q0+nb);
740 wsetselect(w, q0, q0);
745 /* otherwise ordinary character; just insert */
747 q0 = winsert(w, &r, 1, q0);
757 w->f.cols[TEXT] = w->f.cols[HTEXT] = holdcol;
759 w->f.cols[TEXT] = w->f.cols[HTEXT] = lightholdcol;
762 w->f.cols[TEXT] = w->f.cols[HTEXT] = display->black;
764 w->f.cols[TEXT] = w->f.cols[HTEXT] = darkgrey;
775 wborder(w, wscale(w, Selborder));
778 wborder(w, wscale(w, Unselborder));
782 wbswidth(Window *w, Rune c)
788 /* there is known to be at least one character to erase */
789 if(c == 0x08) /* ^H: erase character */
798 if(r == '\n'){ /* eat at most one more character */
799 if(q == w->q0 && c != '\r') /* eat the newline */
805 if(eq && skipping) /* found one; stop skipping */
807 else if(!eq && !skipping)
820 nsnarf = w->q1-w->q0;
821 snarf = runerealloc(snarf, nsnarf);
822 snarfversion++; /* maybe modified by parent */
823 runemove(snarf, w->r+w->q0, nsnarf);
832 wdelete(w, w->q0, w->q1);
833 wsetselect(w, w->q0, w->q0);
845 if(w->rawing && !w->holding && q0==w->nr){
846 waddraw(w, snarf, nsnarf);
847 wsetselect(w, q0, q0);
849 q0 = winsert(w, snarf, nsnarf, w->q0);
850 wsetselect(w, q0, q0+nsnarf);
864 fd = plumbopenfid("send", OWRITE);
867 m = emalloc(sizeof(Plumbmsg));
868 m->src = estrdup("rio");
870 m->wdir = estrdup(w->dir);
871 m->type = estrdup("text");
877 while(p0>0 && w->r[p0-1]!=' ' && w->r[p0-1]!='\t' && w->r[p0-1]!='\n')
879 while(p1<w->nr && w->r[p1]!=' ' && w->r[p1]!='\t' && w->r[p1]!='\n')
881 sprint(buf, "click=%d", w->q0-p0);
882 m->attr = plumbunpackattr(buf);
884 if(p1-p0 > messagesize-1024){
886 return; /* too large for 9P */
888 m->data = runetobyte(w->r+p0, p1-p0, &m->ndata);
889 if(plumbsendtofid(fd, m) < 0){
891 riosetcursor(&query, 1);
899 winborder(Window *w, Point xy)
901 return ptinrect(xy, w->screenr) && !ptinrect(xy, insetrect(w->screenr, wscale(w, Selborder)));
909 if(w->mc.m.buttons == 1)
911 else if(w->mc.m.buttons == 2)
913 else if(w->mc.m.buttons == 4)
916 if(w->mc.m.buttons == 8)
917 wkeyctl(w, Kscrolloneup);
918 if(w->mc.m.buttons == 16)
919 wkeyctl(w, Kscrollonedown);
923 incref(&w->ref); /* hold up window while we track */
926 if(ptinrect(w->mc.m.xy, w->scrollr)){
933 /* else all is handled by main process */
939 wdelete(Window *w, uint q0, uint q1)
946 runemove(w->r+q0, w->r+q1, w->nr-q1);
949 w->iq1 -= min(n, w->iq1-q0);
951 w->q0 -= min(n, w->q0-q0);
953 w->q1 -= min(n, w->q1-q0);
960 else if(q0 < w->org+w->f.nchars){
969 frdelete(&w->f, p0, p1);
975 static Window *clickwin;
976 static uint clickmsec;
977 static Window *selectwin;
981 * called from frame library
984 framescroll(Frame *f, int dl)
986 if(f != &selectwin->f)
987 error("frameselect not right frame");
988 wframescroll(selectwin, dl);
992 wframescroll(Window *w, int dl)
1001 q0 = wbacknl(w, w->org, -dl);
1002 if(selectq > w->org+w->f.p0)
1003 wsetselect(w, w->org+w->f.p0, selectq);
1005 wsetselect(w, selectq, w->org+w->f.p0);
1007 if(w->org+w->f.nchars == w->nr)
1009 q0 = w->org+frcharofpt(&w->f, Pt(w->f.r.min.x, w->f.r.min.y+dl*w->f.font->height));
1010 if(selectq >= w->org+w->f.p1)
1011 wsetselect(w, w->org+w->f.p1, selectq);
1013 wsetselect(w, selectq, w->org+w->f.p1);
1015 wsetorigin(w, q0, TRUE);
1027 * Double-click immediately if it might make sense.
1029 b = w->mc.m.buttons;
1032 selectq = w->org+frcharofpt(&w->f, w->mc.m.xy);
1033 if(clickwin==w && w->mc.m.msec-clickmsec<500)
1034 if(q0==q1 && selectq==w->q0){
1035 wdoubleclick(w, &q0, &q1);
1036 wsetselect(w, q0, q1);
1037 flushimage(display, 1);
1040 /* stay here until something interesting happens */
1043 while(w->mc.m.buttons==b && abs(w->mc.m.xy.x-x)<3 && abs(w->mc.m.xy.y-y)<3);
1044 w->mc.m.xy.x = x; /* in case we're calling frselect */
1046 q0 = w->q0; /* may have changed */
1050 if(w->mc.m.buttons == b){
1051 w->f.scroll = framescroll;
1052 frselect(&w->f, &w->mc);
1053 /* horrible botch: while asleep, may have lost selection altogether */
1055 selectq = w->org + w->f.p0;
1057 if(selectq < w->org)
1060 q0 = w->org + w->f.p0;
1061 if(selectq > w->org+w->f.nchars)
1064 q1 = w->org+w->f.p1;
1067 if(q0==w->q0 && clickwin==w && w->mc.m.msec-clickmsec<500){
1068 wdoubleclick(w, &q0, &q1);
1072 clickmsec = w->mc.m.msec;
1076 wsetselect(w, q0, q1);
1077 flushimage(display, 1);
1078 while(w->mc.m.buttons){
1080 b = w->mc.m.buttons;
1094 flushimage(display, 1);
1095 while(w->mc.m.buttons == b)
1102 wsendctlmesg(Window *w, int type, Rectangle r, Image *image)
1109 send(w->cctl, &wcm);
1113 wctlmesg(Window *w, int m, Rectangle r, Image *i)
1119 error("unknown control message");
1130 strcpy(buf, w->name);
1131 wresize(w, i, m==Moved);
1136 }else if(w == input)
1138 flushimage(display, 1);
1141 if(w->deleted || Dx(w->screenr)<=0 || !rectclip(&r, w->i->r))
1145 flushimage(display, 1);
1148 if(sweeping || !ptinrect(r.min, w->i->r))
1150 wmovemouse(w, r.min);
1157 wkeyctl(w, w->raw[0]);
1159 runemove(w->raw, w->raw+1, w->nraw);
1167 flushimage(display, 1);
1172 write(w->notefd, "hangup", 6);
1176 frclear(&w->f, TRUE);
1181 chanfree(w->conswrite);
1182 chanfree(w->consread);
1183 chanfree(w->mouseread);
1184 chanfree(w->wctlread);
1196 * Convert back to physical coordinates
1199 wmovemouse(Window *w, Point p)
1201 p.x += w->screenr.min.x-w->i->r.min.x;
1202 p.y += w->screenr.min.y-w->i->r.min.y;
1203 moveto(mousectl, p);
1211 if(wkeyboard!=nil && w==wkeyboard)
1215 if(oi!=w && oi!=nil)
1224 wsendctlmesg(oi, Wakeup, ZR, nil);
1228 wsendctlmesg(w, Wakeup, ZR, nil);
1234 wsetcursor(Window *w, int force)
1238 if(w==nil || /*w!=input || */ w->i==nil || Dx(w->screenr)<=0)
1240 else if(wpointto(mouse->xy) == w){
1242 if(p==nil && w->holding)
1247 riosetcursor(p, force && !menuing);
1251 riosetcursor(Cursor *p, int force)
1253 if(!force && p==lastcursor)
1255 setcursor(mousectl, p);
1266 if(w->topped == topped)
1270 flushimage(display, 1);
1271 w->topped = ++topped;
1279 if(w!=nil && w->i!=nil && !w->deleted && w->topped!=topped){
1281 flushimage(display, 1);
1282 w->topped = ++ topped;
1287 wbottomme(Window *w)
1289 if(w!=nil && w->i!=nil && !w->deleted){
1291 flushimage(display, 1);
1301 for(i=0; i<nwindow; i++)
1302 if(window[i]->id == id)
1308 wclosewin(Window *w)
1320 for(i=0; i<nhidden; i++)
1323 memmove(hidden+i, hidden+i+1, (nhidden-i)*sizeof(hidden[0]));
1326 for(i=0; i<nwindow; i++)
1329 memmove(window+i, window+i+1, (nwindow-i)*sizeof(Window*));
1332 /* move it off-screen to hide it, in case client is slow in letting it go */
1333 MOVEIT originwindow(w->i, r.min, view->r.max);
1338 error("unknown window in closewin");
1342 wsetpid(Window *w, int pid, int dolabel)
1348 sprint(buf, "rc %d", pid);
1350 w->label = estrdup(buf);
1351 drawsetlabel(w->label);
1355 static Rune left1[] = {
1356 '{', '[', '(', '<', 0xAB,
1357 0x207d, 0x2329, 0x27e6, 0x27e8, 0x27ea,
1358 0xfe59, 0xfe5b, 0xfe5d, 0xff08, 0xff3b, 0xff5b,
1361 static Rune right1[] = {
1362 '}', ']', ')', '>', 0xBB,
1363 0x207e, 0x232a, 0x27e7, 0x27e9, 0x27eb,
1364 0xfe5a, 0xfe5c, 0xfe5e, 0xff09, 0xff3d, 0xff5d,
1367 static Rune left2[] = { '\n', 0 };
1368 static Rune left3[] = { '\'', '"', '`', 0 };
1384 wdoubleclick(Window *w, uint *q0, uint *q1)
1390 for(i=0; left[i]!=nil; i++){
1394 /* try matching character to left, looking right */
1401 if(wclickmatch(w, c, r[p-l], 1, &q))
1405 /* try matching character to right, looking left */
1412 if(wclickmatch(w, c, l[p-r], -1, &q)){
1413 *q1 = *q0+(*q0<w->nr && c=='\n');
1415 if(c!='\n' || q!=0 || w->r[0]=='\n')
1421 /* try filling out word to right */
1422 while(*q1<w->nr && isalnum(w->r[*q1]))
1424 /* try filling out word to left */
1425 while(*q0>0 && isalnum(w->r[*q0-1]))
1430 wclickmatch(Window *w, int cl, int cr, int dir, uint *q)
1454 return cl=='\n' && nest==1;
1459 wbacknl(Window *w, uint p, uint n)
1463 /* look for start of this line if n==0 */
1464 if(n==0 && p>0 && w->r[p-1]!='\n')
1467 while(i-->0 && p>0){
1468 --p; /* it's at a newline now; back over it */
1471 /* at 128 chars, call it a line anyway */
1472 for(j=128; --j>0 && p>0; p--)
1480 wshow(Window *w, uint q0)
1486 qe = w->org+w->f.nchars;
1487 if(w->org<=q0 && (q0<qe || (q0==qe && qe==w->nr)))
1490 nl = 4*w->f.maxlines/5;
1491 q = wbacknl(w, q0, nl);
1492 /* avoid going backwards if trying to go forwards - long lines! */
1493 if(!(q0>w->org && q<w->org))
1494 wsetorigin(w, q, TRUE);
1495 while(q0 > w->org+w->f.nchars)
1496 wsetorigin(w, w->org+1, FALSE);
1501 wsetorigin(Window *w, uint org, int exact)
1507 if(org>0 && !exact){
1508 /* org is an estimate of the char posn; find a newline */
1509 /* don't try harder than 256 chars */
1510 for(i=0; i<256 && org<w->nr; i++){
1511 if(w->r[org] == '\n'){
1520 if(a>=0 && a<w->f.nchars){
1521 frdelete(&w->f, 0, a);
1522 fixup = 1; /* frdelete can leave end of last line in wrong selection mode; it doesn't know what follows */
1523 }else if(a<0 && -a<w->f.nchars){
1526 runemove(r, w->r+org, n);
1527 frinsert(&w->f, r, r+n, 0);
1530 frdelete(&w->f, 0, w->f.nchars);
1534 wsetselect(w, w->q0, w->q1);
1535 if(fixup && w->f.p1 > w->f.p0)
1536 frdrawsel(&w->f, frptofchar(&w->f, w->f.p1-1), w->f.p1-1, w->f.p1, 1);
1540 wsetselect(Window *w, uint q0, uint q1)
1544 /* w->f.p0 and w->f.p1 are always right; w->q0 and w->q1 may be off */
1547 /* compute desired p0,p1 from q0,q1 */
1554 if(p0 > w->f.nchars)
1556 if(p1 > w->f.nchars)
1558 if(p0==w->f.p0 && p1==w->f.p1)
1560 /* screen disagrees with desired selection */
1561 if(w->f.p1<=p0 || p1<=w->f.p0 || p0==p1 || w->f.p1==w->f.p0){
1562 /* no overlap or too easy to bother trying */
1563 frdrawsel(&w->f, frptofchar(&w->f, w->f.p0), w->f.p0, w->f.p1, 0);
1564 frdrawsel(&w->f, frptofchar(&w->f, p0), p0, p1, 1);
1567 /* overlap; avoid unnecessary painting */
1569 /* extend selection backwards */
1570 frdrawsel(&w->f, frptofchar(&w->f, p0), p0, w->f.p0, 1);
1571 }else if(p0 > w->f.p0){
1572 /* trim first part of selection */
1573 frdrawsel(&w->f, frptofchar(&w->f, w->f.p0), w->f.p0, p0, 0);
1576 /* extend selection forwards */
1577 frdrawsel(&w->f, frptofchar(&w->f, w->f.p1), w->f.p1, p1, 1);
1578 }else if(p1 < w->f.p1){
1579 /* trim last part of selection */
1580 frdrawsel(&w->f, frptofchar(&w->f, p1), p1, w->f.p1, 0);
1589 winsert(Window *w, Rune *r, int n, uint q0)
1595 if(w->nr+n>HiWater && q0>=w->org && q0>=w->qh){
1596 m = min(HiWater-LoWater, min(w->org, w->qh));
1608 runemove(w->r, w->r+m, w->nr);
1611 if(w->nr+n > w->maxr){
1613 * Minimize realloc breakage:
1614 * Allocate at least MinWater
1615 * Double allocation size each time
1616 * But don't go much above HiWater
1618 m = max(min(2*(w->nr+n), HiWater), w->nr+n)+MinWater;
1620 m = max(HiWater+MinWater, w->nr+n);
1622 w->r = runerealloc(w->r, m);
1626 runemove(w->r+q0+n, w->r+q0, w->nr-q0);
1627 runemove(w->r+q0, r, n);
1629 /* if output touches, advance selection, not qh; works best for keyboard and output */
1640 else if(q0 <= w->org+w->f.nchars)
1641 frinsert(&w->f, r, r+n, q0-w->org);
1651 if(w->f.lastlinefull)
1653 rp = malloc(messagesize);
1655 n = w->nr-(w->org+w->f.nchars);
1658 if(n > 2000) /* educated guess at reasonable amount */
1660 runemove(rp, w->r+(w->org+w->f.nchars), n);
1662 * it's expensive to frinsert more than we need, so
1665 nl = w->f.maxlines-w->f.nlines;
1668 if(rp[i++] == '\n'){
1674 frinsert(&w->f, rp, rp+i, w->f.nchars);
1675 }while(w->f.lastlinefull == FALSE);
1680 wcontents(Window *w, int *ip)
1682 return runetobyte(w->r, w->nr, ip);