19 HiWater = 640000, /* max size of history */
20 LoWater = 400000, /* min size of history after max'ed */
21 MinWater = 20000, /* room to leave available when reallocating */
26 static Cursor *lastcursor;
29 wmk(Image *i, Mousectl *mc, Channel *ck, Channel *cctl, int scrolling)
34 w = emalloc(sizeof(Window));
36 r = insetrect(i->r, Selborder+1);
42 w->conswrite = chancreate(sizeof(Conswritemesg), 0);
43 w->consread = chancreate(sizeof(Consreadmesg), 0);
44 w->kbdread = chancreate(sizeof(Kbdreadmesg), 0);
45 w->mouseread = chancreate(sizeof(Mousereadmesg), 0);
46 w->wctlread = chancreate(sizeof(Consreadmesg), 0);
47 w->complete = chancreate(sizeof(Completion*), 0);
48 w->gone = chancreate(sizeof(char*), 0);
50 w->scrollr.max.x = r.min.x+Scrollwid;
52 r.min.x += Scrollwid+Scrollgap;
53 frinit(w, r, font, i, cols);
54 w->maxtab = maxtab*stringwidth(font, "0");
58 w->scrolling = scrolling;
59 w->dir = estrdup(startdir);
60 w->label = estrdup("<unnamed>");
61 r = insetrect(w->i->r, Selborder);
62 draw(w->i, r, cols[BACK], nil, w->entire.min);
63 wborder(w, Selborder);
65 incref(w); /* ref will be removed after mounting; avoids delete before ready to be deleted */
75 n = snprint(w->name, sizeof(w->name)-2, "window.%d.%d", w->id, w->namecount++);
76 for(i='A'; i<='Z'; i++){
77 if(nameimage(w->i, w->name, 1) > 0)
79 errstr(err, sizeof err);
80 if(strcmp(err, "image name in use") != 0)
86 fprint(2, "rio: setname failed: %s\n", err);
90 wresize(Window *w, Image *i, int move)
95 if(move || (Dx(or)==Dx(i->r) && Dy(or)==Dy(i->r)))
96 draw(i, i->r, w->i, nil, w->i->r.min);
100 r = insetrect(i->r, Selborder+1);
102 w->scrollr.max.x = r.min.x+Scrollwid;
104 r.min.x += Scrollwid+Scrollgap;
106 frsetrects(w, r, w->i);
109 frinit(w, r, w->font, w->i, cols);
111 w->maxtab = maxtab*stringwidth(w->font, "0");
112 r = insetrect(w->i->r, Selborder);
113 draw(w->i, r, cols[BACK], nil, w->entire.min);
115 wsetselect(w, w->q0, w->q1);
118 wborder(w, Selborder);
120 w->topped = ++topped;
127 wrefresh(Window *w, Rectangle r)
129 /* BUG: rectangle is ignored */
131 wborder(w, Selborder);
133 wborder(w, Unselborder);
134 r = insetrect(w->i->r, Selborder);
135 draw(w->i, r, w->cols[BACK], nil, w->entire.min);
138 frdrawsel(w, frptofchar(w, 0), 0, w->p0, 0);
139 if(w->p1 < w->nchars)
140 frdrawsel(w, frptofchar(w, w->p1), w->p1, w->nchars, 0);
141 frdrawsel(w, frptofchar(w, w->p0), w->p0, w->p1, 1);
155 error("negative ref count");
157 wsendctlmesg(w, Exited, ZR, nil);
162 showcandidates(Window *, Completion *);
169 int nr, nb, c, wid, i, npart, initial, lastb;
170 char *s, *t, part[3];
173 enum { WKbd, WKbdread, WMouse, WMouseread, WCtl, WCwrite, WCread, WWread, WComplete, Wgone, NWALT };
183 char buf[4*12+1], *kbdq[8], *kbds;
187 snprint(buf, sizeof buf, "winctl-id%d", w->id);
190 mrm.cm = chancreate(sizeof(Mouse), 0);
191 krm.ck = chancreate(sizeof(char*), 0);
192 cwm.cw = chancreate(sizeof(Stringpair), 0);
193 crm.c1 = chancreate(sizeof(Stringpair), 0);
194 crm.c2 = chancreate(sizeof(Stringpair), 0);
195 cwrm.c1 = chancreate(sizeof(Stringpair), 0);
196 cwrm.c2 = chancreate(sizeof(Stringpair), 0);
198 alts[WKbd].c = w->ck;
199 alts[WKbd].v = &kbds;
200 alts[WKbd].op = CHANRCV;
201 alts[WKbdread].c = w->kbdread;
202 alts[WKbdread].v = &krm;
203 alts[WKbdread].op = CHANSND;
204 alts[WMouse].c = w->mc.c;
205 alts[WMouse].v = &w->mc.Mouse;
206 alts[WMouse].op = CHANRCV;
207 alts[WMouseread].c = w->mouseread;
208 alts[WMouseread].v = &mrm;
209 alts[WMouseread].op = CHANSND;
210 alts[WCtl].c = w->cctl;
212 alts[WCtl].op = CHANRCV;
213 alts[WCwrite].c = w->conswrite;
214 alts[WCwrite].v = &cwm;
215 alts[WCwrite].op = CHANSND;
216 alts[WCread].c = w->consread;
217 alts[WCread].v = &crm;
218 alts[WCread].op = CHANSND;
219 alts[WWread].c = w->wctlread;
220 alts[WWread].v = &cwrm;
221 alts[WWread].op = CHANSND;
222 alts[WComplete].c = w->complete;
223 alts[WComplete].v = &cr;
224 alts[WComplete].op = CHANRCV;
225 alts[Wgone].c = w->gone;
226 alts[Wgone].v = "window deleted";
227 alts[Wgone].op = CHANNOP;
228 alts[NWALT].op = CHANEND;
230 memset(kbdq, 0, sizeof(kbdq));
237 alts[Wgone].op = CHANSND;
239 alts[WKbdread].op = CHANNOP;
240 alts[WMouseread].op = CHANNOP;
241 alts[WCwrite].op = CHANNOP;
242 alts[WWread].op = CHANNOP;
243 alts[WCread].op = CHANNOP;
245 alts[WKbdread].op = (w->kbdopen && kbdqw != kbdqr) ?
247 alts[WMouseread].op = (w->mouseopen && w->mouse.counter != w->mouse.lastcounter) ?
249 alts[WCwrite].op = w->scrolling || w->mouseopen || (w->qh <= w->org+w->nchars) ?
251 alts[WWread].op = w->wctlready ?
253 /* this code depends on NL and EOT fitting in a single byte */
254 /* kind of expensive for each loop; worth precomputing? */
256 alts[WCread].op = CHANNOP;
257 else if(npart || (w->rawing && w->nraw>0))
258 alts[WCread].op = CHANSND;
260 alts[WCread].op = CHANNOP;
261 for(i=w->qh; i<w->nr; i++){
263 if(c=='\n' || c=='\004'){
264 alts[WCread].op = CHANSND;
273 i = (kbdqw+1) % nelem(kbdq);
276 } else if(*kbds == 'c'){
279 chartorune(&r, kbds+1);
288 i = (kbdqr+1) % nelem(kbdq);
292 sendp(krm.ck, kbdq[i]);
295 sendp(krm.ck, strdup("K"));
302 /* queue click events */
303 if(!w->mouse.qfull && lastb != w->mc.buttons) { /* add to ring */
304 mp = &w->mouse.queue[w->mouse.wi];
305 if(++w->mouse.wi == nelem(w->mouse.queue))
307 if(w->mouse.wi == w->mouse.ri)
308 w->mouse.qfull = TRUE;
310 mp->counter = w->mouse.counter;
311 lastb = w->mc.buttons;
317 /* send a queued event or, if the queue is empty, the current state */
318 /* if the queue has filled, we discard all the events it contained. */
319 /* the intent is to discard frantic clicking by the user during long latencies. */
320 w->mouse.qfull = FALSE;
321 if(w->mouse.wi != w->mouse.ri) {
322 m = w->mouse.queue[w->mouse.ri];
323 if(++w->mouse.ri == nelem(w->mouse.queue))
326 m = (Mousestate){w->mc.Mouse, w->mouse.counter};
328 w->mouse.lastcounter = m.counter;
329 send(mrm.cm, &m.Mouse);
332 if(wctlmesg(w, wcm.type, wcm.r, wcm.p) == Exited){
333 for(i=0; i<nelem(kbdq); i++)
366 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')){
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 s = Dx(w->screenr) > 0 ? "visible" : "hidden";
420 pair.ns = snprint(pair.s, pair.ns, "%11d %11d %11d %11d %s %s ",
421 w->i->r.min.x, w->i->r.min.y, w->i->r.max.x, w->i->r.max.y, t, s);
422 send(cwrm.c2, &pair);
427 showcandidates(w, cr);
429 rp = runesmprint("%s", cr->string);
433 q0 = winsert(w, rp, nr, q0);
442 if(w->i!=nil && Dx(w->screenr) > 0)
443 flushimage(display, 1);
448 waddraw(Window *w, Rune *r, int nr)
450 w->raw = runerealloc(w->raw, w->nraw+nr);
451 runemove(w->raw+w->nraw, r, nr);
456 * Need to do this in a separate proc because if process we're interrupting
457 * is dying and trying to print tombstone, kernel is blocked holding p->debug lock.
460 interruptproc(void *v)
465 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 rp = runefmtstrflush(&f);
516 /* place text at beginning of line before cursor and host point */
517 qline = min(w->qh, w->q0);
518 while(qline>0 && w->r[qline-1] != '\n')
522 /* advance host point to avoid readback */
523 w->qh = winsert(w, rp, nr, qline)+nr;
525 winsert(w, rp, nr, qline);
530 typedef struct Completejob Completejob;
539 completeproc(void *arg)
546 snprint(buf, sizeof(buf), "namecomplete %s", job->dir);
549 c = complete(job->dir, job->str);
550 if(c != nil && sendp(job->win->complete, c) <= 0)
561 namecomplete(Window *w)
568 /* control-f: filename completion; works back to white space or / */
569 if(w->q0<w->nr && w->r[w->q0]>' ') /* must be at end of word */
571 nstr = windfilewidth(w, w->q0, TRUE);
572 str = w->r+(w->q0-nstr);
573 npath = windfilewidth(w, w->q0-nstr, FALSE);
574 path = w->r+(w->q0-nstr-npath);
576 /* is path rooted? if not, we need to make it relative to window path */
577 if(npath>0 && path[0]=='/')
578 dir = runetobyte(path, npath, &npath);
580 if(strcmp(w->dir, "") == 0)
584 dir = smprint("%s/%.*S", root, npath, path);
589 /* run in background, winctl will collect the result on w->complete chan */
590 job = emalloc(sizeof *job);
591 job->str = runetobyte(str, nstr, &nstr);
592 job->dir = cleanname(dir);
595 proccreate(completeproc, job, STACK);
599 wkeyctl(Window *w, Rune r)
618 /* navigation keys work only when mouse and kbd is not open */
622 n = shiftdown ? 1 : w->maxlines/3;
625 n = mousescrollsize(w->maxlines);
632 q0 = w->org+frcharofpt(w, Pt(w->Frame.r.min.x, w->Frame.r.min.y+n*w->font->height));
633 wsetorigin(w, q0, TRUE);
636 n = shiftdown ? 1 : w->maxlines/3;
639 n = mousescrollsize(w->maxlines);
646 q0 = wbacknl(w, w->org, n);
647 wsetorigin(w, q0, TRUE);
652 wsetselect(w, q0, q0);
659 wsetselect(w, q1, q1);
673 case Ksoh: /* ^A: beginning of line */
674 if(w->q0==0 || w->q0==w->qh || w->r[w->q0-1]=='\n')
676 nb = wbswidth(w, 0x15 /* ^U */);
677 wsetselect(w, w->q0-nb, w->q0-nb);
680 case Kenq: /* ^E: end of line */
682 while(q0 < w->nr && w->r[q0]!='\n')
684 wsetselect(w, q0, q0);
687 case Kstx: /* ^B: output point */
688 wsetselect(w, w->qh, w->qh);
692 if(w->rawing && (w->q0==w->nr || w->mouseopen)){
696 if(r==Kesc || (w->holding && r==Kdel)){ /* toggle hold */
711 case Kdel: /* send interrupt */
716 notefd = emalloc(sizeof(int));
717 *notefd = dup(w->notefd, -1);
718 proccreate(interruptproc, notefd, 4096);
720 case Kack: /* ^F: file name completion */
721 case Kins: /* Insert: file name completion */
724 case Kbs: /* ^H: erase character */
725 case Knack: /* ^U: erase line */
726 case Ketb: /* ^W: erase word */
727 if(w->q0==0 || w->q0==w->qh)
737 wdelete(w, q0, q0+nb);
738 wsetselect(w, q0, q0);
742 /* otherwise ordinary character; just insert */
744 q0 = winsert(w, &r, 1, q0);
749 wsetcols(Window *w, int topped)
753 w->cols[TEXT] = holdcol;
755 w->cols[TEXT] = lightholdcol;
758 w->cols[TEXT] = cols[TEXT];
760 w->cols[TEXT] = paletextcol;
766 wsetcols(w, w == input);
770 wborder(w, Selborder);
772 wborder(w, Unselborder);
776 wbswidth(Window *w, Rune c)
782 /* there is known to be at least one character to erase */
783 if(c == 0x08) /* ^H: erase character */
792 if(r == '\n'){ /* eat at most one more character */
793 if(q == w->q0) /* eat the newline */
799 if(eq && skipping) /* found one; stop skipping */
801 else if(!eq && !skipping)
814 nsnarf = w->q1-w->q0;
815 snarf = runerealloc(snarf, nsnarf);
816 snarfversion++; /* maybe modified by parent */
817 runemove(snarf, w->r+w->q0, nsnarf);
826 wdelete(w, w->q0, w->q1);
827 wsetselect(w, w->q0, w->q0);
839 if(w->rawing && q0==w->nr){
840 waddraw(w, snarf, nsnarf);
841 wsetselect(w, q0, q0);
843 q0 = winsert(w, snarf, nsnarf, w->q0);
844 wsetselect(w, q0, q0+nsnarf);
858 fd = plumbopen("send", OWRITE|OCEXEC);
861 m = emalloc(sizeof(Plumbmsg));
862 m->src = estrdup("rio");
864 m->wdir = estrdup(w->dir);
865 m->type = estrdup("text");
871 while(p0>0 && w->r[p0-1]!=' ' && w->r[p0-1]!='\t' && w->r[p0-1]!='\n')
873 while(p1<w->nr && w->r[p1]!=' ' && w->r[p1]!='\t' && w->r[p1]!='\n')
875 snprint(buf, sizeof(buf), "click=%d", w->q0-p0);
876 m->attr = plumbunpackattr(buf);
878 if(p1-p0 > messagesize-1024){
880 return; /* too large for 9P */
882 m->data = runetobyte(w->r+p0, p1-p0, &m->ndata);
883 if(plumbsend(fd, m) < 0){
885 riosetcursor(&query, 1);
893 winborder(Window *w, Point xy)
895 return ptinrect(xy, w->screenr) && !ptinrect(xy, insetrect(w->screenr, Selborder));
903 if(w->mc.buttons == 1)
905 else if(w->mc.buttons == 2)
907 else if(w->mc.buttons == 4)
910 if(w->mc.buttons == 8)
911 wkeyctl(w, Kscrolloneup);
912 if(w->mc.buttons == 16)
913 wkeyctl(w, Kscrollonedown);
917 incref(w); /* hold up window while we track */
920 if(ptinrect(w->mc.xy, w->scrollr)){
927 /* else all is handled by main process */
933 wdelete(Window *w, uint q0, uint q1)
940 runemove(w->r+q0, w->r+q1, w->nr-q1);
943 w->q0 -= min(n, w->q0-q0);
945 w->q1 -= min(n, w->q1-q0);
952 else if(q0 < w->org+w->nchars){
967 static Window *clickwin;
968 static uint clickmsec;
969 static Window *selectwin;
973 * called from frame library
976 framescroll(Frame *f, int dl)
978 if(f != &selectwin->Frame)
979 error("frameselect not right frame");
980 wframescroll(selectwin, dl);
984 wframescroll(Window *w, int dl)
993 q0 = wbacknl(w, w->org, -dl);
994 if(selectq > w->org+w->p0)
995 wsetselect(w, w->org+w->p0, selectq);
997 wsetselect(w, selectq, w->org+w->p0);
999 if(w->org+w->nchars == w->nr)
1001 q0 = w->org+frcharofpt(w, Pt(w->Frame.r.min.x, w->Frame.r.min.y+dl*w->font->height));
1002 if(selectq >= w->org+w->p1)
1003 wsetselect(w, w->org+w->p1, selectq);
1005 wsetselect(w, selectq, w->org+w->p1);
1007 wsetorigin(w, q0, TRUE);
1019 * Double-click immediately if it might make sense.
1024 selectq = w->org+frcharofpt(w, w->mc.xy);
1025 if(clickwin==w && w->mc.msec-clickmsec<500)
1026 if(q0==q1 && selectq==w->q0){
1027 wdoubleclick(w, &q0, &q1);
1028 wsetselect(w, q0, q1);
1029 flushimage(display, 1);
1032 /* stay here until something interesting happens */
1035 while(w->mc.buttons==b && abs(w->mc.xy.x-x)<3 && abs(w->mc.xy.y-y)<3);
1036 w->mc.xy.x = x; /* in case we're calling frselect */
1038 q0 = w->q0; /* may have changed */
1042 if(w->mc.buttons == b){
1043 w->scroll = framescroll;
1044 frselect(w, &w->mc);
1045 /* horrible botch: while asleep, may have lost selection altogether */
1047 selectq = w->org + w->p0;
1048 w->Frame.scroll = nil;
1049 if(selectq < w->org)
1052 q0 = w->org + w->p0;
1053 if(selectq > w->org+w->nchars)
1059 if(q0==w->q0 && clickwin==w && w->mc.msec-clickmsec<500){
1060 wdoubleclick(w, &q0, &q1);
1064 clickmsec = w->mc.msec;
1068 wsetselect(w, q0, q1);
1069 flushimage(display, 1);
1070 while(w->mc.buttons){
1086 flushimage(display, 1);
1087 while(w->mc.buttons == b)
1094 wsendctlmesg(Window *w, int type, Rectangle r, void *p)
1101 send(w->cctl, &wcm);
1105 wctlmesg(Window *w, int m, Rectangle r, void *p)
1112 error("unknown control message");
1116 sendp((Channel*)p, w);
1125 strcpy(buf, w->name);
1126 wresize(w, i, m==Moved);
1127 proccreate(deletetimeoutproc, estrdup(buf), 4096);
1128 flushimage(display, 1);
1129 if(Dx(r)<=0){ /* window got hidden, if we had the input, drop it */
1134 /* fall through to get input if needed */
1136 if(w->deleted || w==input)
1146 * have to wait until old input responds before
1147 * changing input to us because the window might
1148 * currently be mouse tracking and it is not
1149 * prepared for getting its input revoked.
1151 c = chancreate(sizeof(void*), 0);
1152 wsendctlmesg(oi, Wakeup, ZR, c);
1157 * if we are still top window and nobody else has taken
1158 * input from original window, take the input.
1160 if(!w->deleted && w->topped==topped && oi==input){
1164 wsendctlmesg(oi, Repaint, ZR, nil);
1170 if(m!=Topped && w==input)
1172 /* fall thrugh for redraw after input change */
1174 if(w->i==nil || Dx(w->screenr)<=0)
1177 flushimage(display, 1);
1180 if(w->i==nil || Dx(w->screenr)<=0 || !rectclip(&r, w->i->r) || w->mouseopen)
1183 flushimage(display, 1);
1186 if(w->i==nil || Dx(w->screenr)<=0 || !ptinrect(r.min, w->i->r))
1188 wmovemouse(w, r.min);
1193 wkeyctl(w, w->raw[0]);
1195 runemove(w->raw, w->raw+1, w->nraw);
1205 flushimage(display, 1);
1210 write(w->notefd, "hangup", 6);
1212 proccreate(deletetimeoutproc, estrdup(w->name), 4096);
1224 chanfree(w->conswrite);
1225 chanfree(w->consread);
1226 chanfree(w->mouseread);
1227 chanfree(w->wctlread);
1228 chanfree(w->kbdread);
1229 chanfree(w->complete);
1242 * Convert back to physical coordinates
1245 wmovemouse(Window *w, Point p)
1247 if(w != input || menuing || sweeping)
1249 p.x += w->screenr.min.x-w->i->r.min.x;
1250 p.y += w->screenr.min.y-w->i->r.min.y;
1251 moveto(mousectl, p);
1255 wborder(Window *w, int type)
1262 if(type == Selborder)
1267 if(type == Selborder)
1270 col = lighttitlecol;
1272 border(w->i, w->i->r, Selborder, col, ZP);
1282 for(i=0; i<nwindow; i++){
1284 if(ptinrect(pt, v->screenr))
1285 if(w==nil || v->topped>w->topped)
1294 if(w!=nil && w!=input)
1295 wsendctlmesg(w, Topped, ZR, nil);
1299 wsetcursor(Window *w, int force)
1303 if(w==nil || w->i==nil || Dx(w->screenr)<=0)
1305 else if(wpointto(mouse->xy) == w){
1307 if(p==nil && w->holding)
1312 riosetcursor(p, force && !menuing);
1316 riosetcursor(Cursor *p, int force)
1318 if(!force && p==lastcursor)
1320 setcursor(mousectl, p);
1328 if(w!=nil && w->i!=nil && w->topped!=topped){
1329 w->topped = ++topped;
1331 flushimage(display, 1);
1336 wbottomme(Window *w)
1338 if(w!=nil && w->i!=nil){
1339 w->topped = - ++topped;
1341 flushimage(display, 1);
1365 for(i=0; i<nwindow; i++)
1366 if(window[i]->id == id)
1385 for(i=0; i<nhidden; i++)
1388 memmove(hidden+i, hidden+i+1, (nhidden-i)*sizeof(hidden[0]));
1391 for(i=0; i<nwindow; i++)
1394 memmove(window+i, window+i+1, (nwindow-i)*sizeof(window[0]));
1400 wclosewin(Window *w)
1404 assert(w->deleted==TRUE);
1409 /* move it off-screen to hide it, in case client is slow in letting it go */
1410 MOVEIT originwindow(i, i->r.min, view->r.max);
1416 wsetpid(Window *w, int pid, int dolabel)
1426 snprint(buf, sizeof(buf), "rc %d", pid);
1428 w->label = estrdup(buf);
1430 snprint(buf, sizeof(buf), "/proc/%d/notepg", pid);
1431 w->notefd = open(buf, OWRITE|OCEXEC);
1438 winshell(void *args)
1452 rfork(RFNAMEG|RFFDG|RFENVG);
1453 if(filsysmount(filsys, w->id) < 0){
1454 fprint(2, "mount failed: %r\n");
1456 threadexits("mount failed");
1459 if(open("/dev/cons", OREAD) < 0){
1460 fprint(2, "can't open /dev/cons: %r\n");
1462 threadexits("/dev/cons");
1465 if(open("/dev/cons", OWRITE) < 0){
1466 fprint(2, "can't open /dev/cons: %r\n");
1468 threadexits("open"); /* BUG? was terminate() */
1470 if(wclose(w) == 0){ /* remove extra ref hanging from creation */
1475 procexec(pidc, cmd, argv);
1476 _exits("exec failed");
1480 static Rune left1[] = { L'{', L'[', L'(', L'<', L'«', 0 };
1481 static Rune right1[] = { L'}', L']', L')', L'>', L'»', 0 };
1482 static Rune left2[] = { L'\n', 0 };
1483 static Rune left3[] = { L'\'', L'"', L'`', 0 };
1499 wdoubleclick(Window *w, uint *q0, uint *q1)
1505 for(i=0; left[i]!=nil; i++){
1509 /* try matching character to left, looking right */
1516 if(wclickmatch(w, c, r[p-l], 1, &q))
1520 /* try matching character to right, looking left */
1527 if(wclickmatch(w, c, l[p-r], -1, &q)){
1528 *q1 = *q0+(*q0<w->nr && c=='\n');
1530 if(c!='\n' || q!=0 || w->r[0]=='\n')
1536 /* try filling out word to right */
1537 while(*q1<w->nr && isalnum(w->r[*q1]))
1539 /* try filling out word to left */
1540 while(*q0>0 && isalnum(w->r[*q0-1]))
1545 wclickmatch(Window *w, int cl, int cr, int dir, uint *q)
1569 return cl=='\n' && nest==1;
1574 wbacknl(Window *w, uint p, uint n)
1578 /* look for start of this line if n==0 */
1579 if(n==0 && p>0 && w->r[p-1]!='\n')
1582 while(i-->0 && p>0){
1583 --p; /* it's at a newline now; back over it */
1586 /* at 128 chars, call it a line anyway */
1587 for(j=128; --j>0 && p>0; p--)
1595 wshow(Window *w, uint q0)
1601 qe = w->org+w->nchars;
1602 if(w->org<=q0 && (q0<qe || (q0==qe && qe==w->nr)))
1605 nl = 4*w->maxlines/5;
1606 q = wbacknl(w, q0, nl);
1607 /* avoid going backwards if trying to go forwards - long lines! */
1608 if(!(q0>w->org && q<w->org))
1609 wsetorigin(w, q, TRUE);
1610 while(q0 > w->org+w->nchars)
1611 wsetorigin(w, w->org+1, FALSE);
1616 wsetorigin(Window *w, uint org, int exact)
1622 if(org>0 && !exact){
1623 /* org is an estimate of the char posn; find a newline */
1624 /* don't try harder than 256 chars */
1625 for(i=0; i<256 && org<w->nr; i++){
1626 if(w->r[org] == '\n'){
1635 if(a>=0 && a<w->nchars){
1637 fixup = 1; /* frdelete can leave end of last line in wrong selection mode; it doesn't know what follows */
1638 }else if(a<0 && -a<w->nchars){
1641 frinsert(w, r, r+n, 0);
1643 frdelete(w, 0, w->nchars);
1647 wsetselect(w, w->q0, w->q1);
1648 if(fixup && w->p1 > w->p0)
1649 frdrawsel(w, frptofchar(w, w->p1-1), w->p1-1, w->p1, 1);
1653 wsetselect(Window *w, uint q0, uint q1)
1657 /* w->p0 and w->p1 are always right; w->q0 and w->q1 may be off */
1660 /* compute desired p0,p1 from q0,q1 */
1671 if(p0==w->p0 && p1==w->p1)
1673 /* screen disagrees with desired selection */
1674 if(w->p1<=p0 || p1<=w->p0 || p0==p1 || w->p1==w->p0){
1675 /* no overlap or too easy to bother trying */
1676 frdrawsel(w, frptofchar(w, w->p0), w->p0, w->p1, 0);
1677 frdrawsel(w, frptofchar(w, p0), p0, p1, 1);
1680 /* overlap; avoid unnecessary painting */
1682 /* extend selection backwards */
1683 frdrawsel(w, frptofchar(w, p0), p0, w->p0, 1);
1684 }else if(p0 > w->p0){
1685 /* trim first part of selection */
1686 frdrawsel(w, frptofchar(w, w->p0), w->p0, p0, 0);
1689 /* extend selection forwards */
1690 frdrawsel(w, frptofchar(w, w->p1), w->p1, p1, 1);
1691 }else if(p1 < w->p1){
1692 /* trim last part of selection */
1693 frdrawsel(w, frptofchar(w, p1), p1, w->p1, 0);
1702 winsert(Window *w, Rune *r, int n, uint q0)
1708 if(w->nr+n>HiWater && q0>=w->org && q0>=w->qh){
1709 m = min(HiWater-LoWater, min(w->org, w->qh));
1721 runemove(w->r, w->r+m, w->nr);
1724 if(w->nr+n > w->maxr){
1726 * Minimize realloc breakage:
1727 * Allocate at least MinWater
1728 * Double allocation size each time
1729 * But don't go much above HiWater
1731 m = max(min(2*(w->nr+n), HiWater), w->nr+n)+MinWater;
1733 m = max(HiWater+MinWater, w->nr+n);
1735 w->r = runerealloc(w->r, m);
1739 runemove(w->r+q0+n, w->r+q0, w->nr-q0);
1740 runemove(w->r+q0, r, n);
1742 /* if output touches, advance selection, not qh; works best for keyboard and output */
1751 else if(q0 <= w->org+w->nchars)
1752 frinsert(w, r, r+n, q0-w->org);
1762 while(w->lastlinefull == FALSE){
1763 n = w->nr-(w->org+w->nchars);
1766 if(n > 2000) /* educated guess at reasonable amount */
1768 rp = w->r+(w->org+w->nchars);
1771 * it's expensive to frinsert more than we need, so
1774 nl = w->maxlines-w->nlines;
1777 if(rp[i++] == '\n'){
1783 frinsert(w, rp, rp+i, w->nchars);
1788 wcontents(Window *w, int *ip)
1790 return runetobyte(w->r, w->nr, ip);