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 *kbdq[8], *kbds;
187 threadsetname("winctl-id%d", w->id);
189 mrm.cm = chancreate(sizeof(Mouse), 0);
190 krm.ck = chancreate(sizeof(char*), 0);
191 cwm.cw = chancreate(sizeof(Stringpair), 0);
192 crm.c1 = chancreate(sizeof(Stringpair), 0);
193 crm.c2 = chancreate(sizeof(Stringpair), 0);
194 cwrm.c1 = chancreate(sizeof(Stringpair), 0);
195 cwrm.c2 = chancreate(sizeof(Stringpair), 0);
197 alts[WKbd].c = w->ck;
198 alts[WKbd].v = &kbds;
199 alts[WKbd].op = CHANRCV;
200 alts[WKbdread].c = w->kbdread;
201 alts[WKbdread].v = &krm;
202 alts[WKbdread].op = CHANSND;
203 alts[WMouse].c = w->mc.c;
204 alts[WMouse].v = &w->mc.Mouse;
205 alts[WMouse].op = CHANRCV;
206 alts[WMouseread].c = w->mouseread;
207 alts[WMouseread].v = &mrm;
208 alts[WMouseread].op = CHANSND;
209 alts[WCtl].c = w->cctl;
211 alts[WCtl].op = CHANRCV;
212 alts[WCwrite].c = w->conswrite;
213 alts[WCwrite].v = &cwm;
214 alts[WCwrite].op = CHANSND;
215 alts[WCread].c = w->consread;
216 alts[WCread].v = &crm;
217 alts[WCread].op = CHANSND;
218 alts[WWread].c = w->wctlread;
219 alts[WWread].v = &cwrm;
220 alts[WWread].op = CHANSND;
221 alts[WComplete].c = w->complete;
222 alts[WComplete].v = &cr;
223 alts[WComplete].op = CHANRCV;
224 alts[Wgone].c = w->gone;
225 alts[Wgone].v = "window deleted";
226 alts[Wgone].op = CHANNOP;
227 alts[NWALT].op = CHANEND;
229 memset(kbdq, 0, sizeof(kbdq));
236 alts[Wgone].op = CHANSND;
238 alts[WKbdread].op = CHANNOP;
239 alts[WMouseread].op = CHANNOP;
240 alts[WCwrite].op = CHANNOP;
241 alts[WWread].op = CHANNOP;
242 alts[WCread].op = CHANNOP;
244 alts[WKbdread].op = (w->kbdopen && kbdqw != kbdqr) ?
246 alts[WMouseread].op = (w->mouseopen && w->mouse.counter != w->mouse.lastcounter) ?
248 alts[WCwrite].op = w->scrolling || w->mouseopen || (w->qh <= w->org+w->nchars) ?
250 alts[WWread].op = w->wctlready ?
252 /* this code depends on NL and EOT fitting in a single byte */
253 /* kind of expensive for each loop; worth precomputing? */
255 alts[WCread].op = CHANNOP;
256 else if(npart || (w->rawing && w->nraw>0))
257 alts[WCread].op = CHANSND;
259 alts[WCread].op = CHANNOP;
260 for(i=w->qh; i<w->nr; i++){
262 if(c=='\n' || c=='\004'){
263 alts[WCread].op = CHANSND;
272 i = (kbdqw+1) % nelem(kbdq);
275 } else if(*kbds == 'c'){
278 chartorune(&r, kbds+1);
287 i = (kbdqr+1) % nelem(kbdq);
291 sendp(krm.ck, kbdq[i]);
294 sendp(krm.ck, strdup("K"));
301 /* queue click events */
302 if(!w->mouse.qfull && lastb != w->mc.buttons) { /* add to ring */
303 mp = &w->mouse.queue[w->mouse.wi];
304 if(++w->mouse.wi == nelem(w->mouse.queue))
306 if(w->mouse.wi == w->mouse.ri)
307 w->mouse.qfull = TRUE;
309 mp->counter = w->mouse.counter;
310 lastb = w->mc.buttons;
316 /* send a queued event or, if the queue is empty, the current state */
317 /* if the queue has filled, we discard all the events it contained. */
318 /* the intent is to discard frantic clicking by the user during long latencies. */
319 w->mouse.qfull = FALSE;
320 if(w->mouse.wi != w->mouse.ri) {
321 m = w->mouse.queue[w->mouse.ri];
322 if(++w->mouse.ri == nelem(w->mouse.queue))
325 m = (Mousestate){w->mc.Mouse, w->mouse.counter};
327 w->mouse.lastcounter = m.counter;
328 send(mrm.cm, &m.Mouse);
331 if(wctlmesg(w, wcm.type, wcm.r, wcm.p) == Exited){
332 for(i=0; i<nelem(kbdq); i++)
365 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 s = Dx(w->screenr) > 0 ? "visible" : "hidden";
419 pair.ns = snprint(pair.s, pair.ns, "%11d %11d %11d %11d %s %s ",
420 w->i->r.min.x, w->i->r.min.y, w->i->r.max.x, w->i->r.max.y, t, s);
421 send(cwrm.c2, &pair);
426 showcandidates(w, cr);
428 rp = runesmprint("%s", cr->string);
432 q0 = winsert(w, rp, nr, q0);
441 if(w->i!=nil && Dx(w->screenr) > 0)
442 flushimage(display, 1);
447 waddraw(Window *w, Rune *r, int nr)
449 w->raw = runerealloc(w->raw, w->nraw+nr);
450 runemove(w->raw+w->nraw, r, nr);
455 * Need to do this in a separate proc because if process we're interrupting
456 * is dying and trying to print tombstone, kernel is blocked holding p->debug lock.
459 interruptproc(void *v)
464 write(*notefd, "interrupt", 9);
470 windfilewidth(Window *w, uint q0, int oneelement)
478 if(r<=' ' || r=='=' || r=='^' || r=='(' || r=='{')
480 if(oneelement && r=='/')
488 showcandidates(Window *w, Completion *c)
498 s = "[no matches in ";
502 fmtprint(&f, "%s%d files]\n", s, c->nfile);
504 fmtprint(&f, "%s", s);
505 for(i=0; i<c->nfile; i++){
508 fmtprint(&f, "%s", c->filename[i]);
512 rp = runefmtstrflush(&f);
515 /* place text at beginning of line before cursor and host point */
516 qline = min(w->qh, w->q0);
517 while(qline>0 && w->r[qline-1] != '\n')
521 /* advance host point to avoid readback */
522 w->qh = winsert(w, rp, nr, qline)+nr;
524 winsert(w, rp, nr, qline);
529 typedef struct Completejob Completejob;
538 completeproc(void *arg)
544 threadsetname("namecomplete %s", job->dir);
546 c = complete(job->dir, job->str);
547 if(c != nil && sendp(job->win->complete, c) <= 0)
558 namecomplete(Window *w)
565 /* control-f: filename completion; works back to white space or / */
566 if(w->q0<w->nr && w->r[w->q0]>' ') /* must be at end of word */
568 nstr = windfilewidth(w, w->q0, TRUE);
569 str = w->r+(w->q0-nstr);
570 npath = windfilewidth(w, w->q0-nstr, FALSE);
571 path = w->r+(w->q0-nstr-npath);
573 /* is path rooted? if not, we need to make it relative to window path */
574 if(npath>0 && path[0]=='/')
575 dir = runetobyte(path, npath, &npath);
577 if(strcmp(w->dir, "") == 0)
581 dir = smprint("%s/%.*S", root, npath, path);
586 /* run in background, winctl will collect the result on w->complete chan */
587 job = emalloc(sizeof *job);
588 job->str = runetobyte(str, nstr, &nstr);
589 job->dir = cleanname(dir);
592 proccreate(completeproc, job, STACK);
596 wkeyctl(Window *w, Rune r)
615 /* navigation keys work only when mouse and kbd is not open */
619 n = shiftdown ? 1 : w->maxlines/3;
622 n = mousescrollsize(w->maxlines);
629 q0 = w->org+frcharofpt(w, Pt(w->Frame.r.min.x, w->Frame.r.min.y+n*w->font->height));
630 wsetorigin(w, q0, TRUE);
633 n = shiftdown ? 1 : w->maxlines/3;
636 n = mousescrollsize(w->maxlines);
643 q0 = wbacknl(w, w->org, n);
644 wsetorigin(w, q0, TRUE);
649 wsetselect(w, q0, q0);
656 wsetselect(w, q1, q1);
670 case Ksoh: /* ^A: beginning of line */
671 if(w->q0==0 || w->q0==w->qh || w->r[w->q0-1]=='\n')
673 nb = wbswidth(w, 0x15 /* ^U */);
674 wsetselect(w, w->q0-nb, w->q0-nb);
677 case Kenq: /* ^E: end of line */
679 while(q0 < w->nr && w->r[q0]!='\n')
681 wsetselect(w, q0, q0);
684 case Kstx: /* ^B: output point */
685 wsetselect(w, w->qh, w->qh);
689 if(w->rawing && (w->q0==w->nr || w->mouseopen)){
693 if(r==Kesc || (w->holding && r==Kdel)){ /* toggle hold */
708 case Kdel: /* send interrupt */
713 notefd = emalloc(sizeof(int));
714 *notefd = dup(w->notefd, -1);
715 proccreate(interruptproc, notefd, 4096);
717 case Kack: /* ^F: file name completion */
718 case Kins: /* Insert: file name completion */
721 case Kbs: /* ^H: erase character */
722 case Knack: /* ^U: erase line */
723 case Ketb: /* ^W: erase word */
724 if(w->q0==0 || w->q0==w->qh)
734 wdelete(w, q0, q0+nb);
735 wsetselect(w, q0, q0);
739 /* otherwise ordinary character; just insert */
741 q0 = winsert(w, &r, 1, q0);
746 wsetcols(Window *w, int topped)
750 w->cols[TEXT] = holdcol;
752 w->cols[TEXT] = lightholdcol;
755 w->cols[TEXT] = cols[TEXT];
757 w->cols[TEXT] = paletextcol;
763 wsetcols(w, w == input);
767 wborder(w, Selborder);
769 wborder(w, Unselborder);
773 wbswidth(Window *w, Rune c)
779 /* there is known to be at least one character to erase */
780 if(c == 0x08) /* ^H: erase character */
789 if(r == '\n'){ /* eat at most one more character */
790 if(q == w->q0) /* eat the newline */
796 if(eq && skipping) /* found one; stop skipping */
798 else if(!eq && !skipping)
811 nsnarf = w->q1-w->q0;
812 snarf = runerealloc(snarf, nsnarf);
813 snarfversion++; /* maybe modified by parent */
814 runemove(snarf, w->r+w->q0, nsnarf);
823 wdelete(w, w->q0, w->q1);
824 wsetselect(w, w->q0, w->q0);
836 if(w->rawing && q0==w->nr){
837 waddraw(w, snarf, nsnarf);
838 wsetselect(w, q0, q0);
840 q0 = winsert(w, snarf, nsnarf, w->q0);
841 wsetselect(w, q0, q0+nsnarf);
855 fd = plumbopen("send", OWRITE|OCEXEC);
858 m = emalloc(sizeof(Plumbmsg));
859 m->src = estrdup("rio");
861 m->wdir = estrdup(w->dir);
862 m->type = estrdup("text");
868 while(p0>0 && w->r[p0-1]!=' ' && w->r[p0-1]!='\t' && w->r[p0-1]!='\n')
870 while(p1<w->nr && w->r[p1]!=' ' && w->r[p1]!='\t' && w->r[p1]!='\n')
872 snprint(buf, sizeof(buf), "click=%d", w->q0-p0);
873 m->attr = plumbunpackattr(buf);
875 if(p1-p0 > messagesize-1024){
877 return; /* too large for 9P */
879 m->data = runetobyte(w->r+p0, p1-p0, &m->ndata);
880 if(plumbsend(fd, m) < 0){
882 riosetcursor(&query, 1);
903 while(runestrncmp(w->r+w->q0, w->r+i, n) != 0){
910 wsetselect(w, i, i+n);
915 winborder(Window *w, Point xy)
917 return ptinrect(xy, w->screenr) && !ptinrect(xy, insetrect(w->screenr, Selborder));
928 if(w->mc.buttons == 1<<(but-1))
932 incref(w); /* hold up window while we track */
934 if(shiftdown && but > 3)
935 wkeyctl(w, but == 4 ? Kscrolloneup : Kscrollonedown);
936 else if(ptinrect(w->mc.xy, w->scrollr) || (but > 3))
945 wdelete(Window *w, uint q0, uint q1)
952 runemove(w->r+q0, w->r+q1, w->nr-q1);
955 w->q0 -= min(n, w->q0-q0);
957 w->q1 -= min(n, w->q1-q0);
964 else if(q0 < w->org+w->nchars){
979 static Window *clickwin;
980 static uint clickmsec;
981 static Window *selectwin;
985 * called from frame library
988 framescroll(Frame *f, int dl)
990 if(f != &selectwin->Frame)
991 error("frameselect not right frame");
992 wframescroll(selectwin, dl);
996 wframescroll(Window *w, int dl)
1005 q0 = wbacknl(w, w->org, -dl);
1006 if(selectq > w->org+w->p0)
1007 wsetselect(w, w->org+w->p0, selectq);
1009 wsetselect(w, selectq, w->org+w->p0);
1011 if(w->org+w->nchars == w->nr)
1013 q0 = w->org+frcharofpt(w, Pt(w->Frame.r.min.x, w->Frame.r.min.y+dl*w->font->height));
1014 if(selectq >= w->org+w->p1)
1015 wsetselect(w, w->org+w->p1, selectq);
1017 wsetselect(w, selectq, w->org+w->p1);
1019 wsetorigin(w, q0, TRUE);
1031 * Double-click immediately if it might make sense.
1036 selectq = w->org+frcharofpt(w, w->mc.xy);
1037 if(clickwin==w && w->mc.msec-clickmsec<500)
1038 if(q0==q1 && selectq==w->q0){
1039 wdoubleclick(w, &q0, &q1);
1040 wsetselect(w, q0, q1);
1041 flushimage(display, 1);
1044 /* stay here until something interesting happens */
1047 while(w->mc.buttons==b && abs(w->mc.xy.x-x)<3 && abs(w->mc.xy.y-y)<3);
1048 w->mc.xy.x = x; /* in case we're calling frselect */
1050 q0 = w->q0; /* may have changed */
1054 if(w->mc.buttons == b){
1055 w->scroll = framescroll;
1056 frselect(w, &w->mc);
1057 /* horrible botch: while asleep, may have lost selection altogether */
1059 selectq = w->org + w->p0;
1060 w->Frame.scroll = nil;
1061 if(selectq < w->org)
1064 q0 = w->org + w->p0;
1065 if(selectq > w->org+w->nchars)
1071 if(q0==w->q0 && clickwin==w && w->mc.msec-clickmsec<500){
1072 wdoubleclick(w, &q0, &q1);
1076 clickmsec = w->mc.msec;
1080 wsetselect(w, q0, q1);
1081 flushimage(display, 1);
1082 while(w->mc.buttons){
1098 flushimage(display, 1);
1099 while(w->mc.buttons == b)
1106 wsendctlmesg(Window *w, int type, Rectangle r, void *p)
1113 send(w->cctl, &wcm);
1117 wctlmesg(Window *w, int m, Rectangle r, void *p)
1124 error("unknown control message");
1128 sendp((Channel*)p, w);
1137 strcpy(buf, w->name);
1138 wresize(w, i, m==Moved);
1139 proccreate(deletetimeoutproc, estrdup(buf), 4096);
1140 flushimage(display, 1);
1141 if(Dx(r)<=0){ /* window got hidden, if we had the input, drop it */
1146 /* fall through to get input if needed */
1148 if(w->deleted || w==input)
1158 * have to wait until old input responds before
1159 * changing input to us because the window might
1160 * currently be mouse tracking and it is not
1161 * prepared for getting its input revoked.
1163 c = chancreate(sizeof(void*), 0);
1164 wsendctlmesg(oi, Wakeup, ZR, c);
1169 * if we are still top window and nobody else has taken
1170 * input from original window, take the input.
1172 if(!w->deleted && w->topped==topped && oi==input){
1176 wsendctlmesg(oi, Repaint, ZR, nil);
1182 if(m!=Topped && w==input)
1184 /* fall thrugh for redraw after input change */
1186 if(w->i==nil || Dx(w->screenr)<=0)
1189 flushimage(display, 1);
1192 if(w->i==nil || Dx(w->screenr)<=0 || !rectclip(&r, w->i->r) || w->mouseopen)
1195 flushimage(display, 1);
1198 if(w->i==nil || Dx(w->screenr)<=0 || !ptinrect(r.min, w->i->r))
1200 wmovemouse(w, r.min);
1205 wkeyctl(w, w->raw[0]);
1207 runemove(w->raw, w->raw+1, w->nraw);
1217 flushimage(display, 1);
1222 write(w->notefd, "hangup", 6);
1224 proccreate(deletetimeoutproc, estrdup(w->name), 4096);
1236 chanfree(w->conswrite);
1237 chanfree(w->consread);
1238 chanfree(w->mouseread);
1239 chanfree(w->wctlread);
1240 chanfree(w->kbdread);
1241 chanfree(w->complete);
1254 * Convert back to physical coordinates
1257 wmovemouse(Window *w, Point p)
1259 if(w != input || menuing || sweeping)
1261 p.x += w->screenr.min.x-w->i->r.min.x;
1262 p.y += w->screenr.min.y-w->i->r.min.y;
1263 moveto(mousectl, p);
1267 wborder(Window *w, int type)
1274 if(type == Selborder)
1279 if(type == Selborder)
1282 col = lighttitlecol;
1284 border(w->i, w->i->r, Selborder, col, ZP);
1294 for(i=0; i<nwindow; i++){
1296 if(ptinrect(pt, v->screenr))
1297 if(w==nil || v->topped>w->topped)
1306 if(w!=nil && w!=input)
1307 wsendctlmesg(w, Topped, ZR, nil);
1311 wsetcursor(Window *w, int force)
1315 if(w==nil || w->i==nil || Dx(w->screenr)<=0)
1317 else if(wpointto(mouse->xy) == w){
1319 if(p==nil && w->holding)
1324 riosetcursor(p, force && !menuing);
1328 riosetcursor(Cursor *p, int force)
1330 if(!force && p==lastcursor)
1332 setcursor(mousectl, p);
1340 if(w!=nil && w->i!=nil && w->topped!=topped){
1341 w->topped = ++topped;
1343 flushimage(display, 1);
1348 wbottomme(Window *w)
1350 if(w!=nil && w->i!=nil){
1351 w->topped = - ++topped;
1353 flushimage(display, 1);
1377 for(i=0; i<nwindow; i++)
1378 if(window[i]->id == id)
1397 for(i=0; i<nhidden; i++)
1400 memmove(hidden+i, hidden+i+1, (nhidden-i)*sizeof(hidden[0]));
1403 for(i=0; i<nwindow; i++)
1406 memmove(window+i, window+i+1, (nwindow-i)*sizeof(window[0]));
1412 wclosewin(Window *w)
1416 assert(w->deleted==TRUE);
1421 /* move it off-screen to hide it, in case client is slow in letting it go */
1422 MOVEIT originwindow(i, i->r.min, view->r.max);
1428 wsetpid(Window *w, int pid, int dolabel)
1438 snprint(buf, sizeof(buf), "rc %d", pid);
1440 w->label = estrdup(buf);
1442 snprint(buf, sizeof(buf), "/proc/%d/notepg", pid);
1443 w->notefd = open(buf, OWRITE|OCEXEC);
1450 winshell(void *args)
1464 rfork(RFNAMEG|RFFDG|RFENVG);
1465 if(filsysmount(filsys, w->id) < 0){
1466 fprint(2, "mount failed: %r\n");
1468 threadexits("mount failed");
1471 if(open("/dev/cons", OREAD) < 0){
1472 fprint(2, "can't open /dev/cons: %r\n");
1474 threadexits("/dev/cons");
1477 if(open("/dev/cons", OWRITE) < 0){
1478 fprint(2, "can't open /dev/cons: %r\n");
1480 threadexits("open"); /* BUG? was terminate() */
1482 if(wclose(w) == 0){ /* remove extra ref hanging from creation */
1487 procexec(pidc, cmd, argv);
1488 _exits("exec failed");
1492 static Rune left1[] = { L'{', L'[', L'(', L'<', L'«', 0 };
1493 static Rune right1[] = { L'}', L']', L')', L'>', L'»', 0 };
1494 static Rune left2[] = { L'\n', 0 };
1495 static Rune left3[] = { L'\'', L'"', L'`', 0 };
1511 wdoubleclick(Window *w, uint *q0, uint *q1)
1517 for(i=0; left[i]!=nil; i++){
1521 /* try matching character to left, looking right */
1528 if(wclickmatch(w, c, r[p-l], 1, &q))
1532 /* try matching character to right, looking left */
1539 if(wclickmatch(w, c, l[p-r], -1, &q)){
1540 *q1 = *q0+(*q0<w->nr && c=='\n');
1542 if(c!='\n' || q!=0 || w->r[0]=='\n')
1548 /* try filling out word to right */
1549 while(*q1<w->nr && isalnum(w->r[*q1]))
1551 /* try filling out word to left */
1552 while(*q0>0 && isalnum(w->r[*q0-1]))
1557 wclickmatch(Window *w, int cl, int cr, int dir, uint *q)
1581 return cl=='\n' && nest==1;
1586 wbacknl(Window *w, uint p, uint n)
1590 /* look for start of this line if n==0 */
1591 if(n==0 && p>0 && w->r[p-1]!='\n')
1594 while(i-->0 && p>0){
1595 --p; /* it's at a newline now; back over it */
1598 /* at 128 chars, call it a line anyway */
1599 for(j=128; --j>0 && p>0; p--)
1607 wshow(Window *w, uint q0)
1613 qe = w->org+w->nchars;
1614 if(w->org<=q0 && (q0<qe || (q0==qe && qe==w->nr)))
1617 nl = 4*w->maxlines/5;
1618 q = wbacknl(w, q0, nl);
1619 /* avoid going backwards if trying to go forwards - long lines! */
1620 if(!(q0>w->org && q<w->org))
1621 wsetorigin(w, q, TRUE);
1622 while(q0 > w->org+w->nchars)
1623 wsetorigin(w, w->org+1, FALSE);
1628 wsetorigin(Window *w, uint org, int exact)
1634 if(org>0 && !exact){
1635 /* org is an estimate of the char posn; find a newline */
1636 /* don't try harder than 256 chars */
1637 for(i=0; i<256 && org<w->nr; i++){
1638 if(w->r[org] == '\n'){
1647 if(a>=0 && a<w->nchars){
1649 fixup = 1; /* frdelete can leave end of last line in wrong selection mode; it doesn't know what follows */
1650 }else if(a<0 && -a<w->nchars){
1653 frinsert(w, r, r+n, 0);
1655 frdelete(w, 0, w->nchars);
1659 wsetselect(w, w->q0, w->q1);
1660 if(fixup && w->p1 > w->p0)
1661 frdrawsel(w, frptofchar(w, w->p1-1), w->p1-1, w->p1, 1);
1665 wsetselect(Window *w, uint q0, uint q1)
1669 /* w->p0 and w->p1 are always right; w->q0 and w->q1 may be off */
1672 /* compute desired p0,p1 from q0,q1 */
1683 if(p0==w->p0 && p1==w->p1)
1685 /* screen disagrees with desired selection */
1686 if(w->p1<=p0 || p1<=w->p0 || p0==p1 || w->p1==w->p0){
1687 /* no overlap or too easy to bother trying */
1688 frdrawsel(w, frptofchar(w, w->p0), w->p0, w->p1, 0);
1689 frdrawsel(w, frptofchar(w, p0), p0, p1, 1);
1692 /* overlap; avoid unnecessary painting */
1694 /* extend selection backwards */
1695 frdrawsel(w, frptofchar(w, p0), p0, w->p0, 1);
1696 }else if(p0 > w->p0){
1697 /* trim first part of selection */
1698 frdrawsel(w, frptofchar(w, w->p0), w->p0, p0, 0);
1701 /* extend selection forwards */
1702 frdrawsel(w, frptofchar(w, w->p1), w->p1, p1, 1);
1703 }else if(p1 < w->p1){
1704 /* trim last part of selection */
1705 frdrawsel(w, frptofchar(w, p1), p1, w->p1, 0);
1714 winsert(Window *w, Rune *r, int n, uint q0)
1720 if(w->nr+n>HiWater && q0>=w->org && q0>=w->qh){
1721 m = min(HiWater-LoWater, min(w->org, w->qh));
1733 runemove(w->r, w->r+m, w->nr);
1736 if(w->nr+n > w->maxr){
1738 * Minimize realloc breakage:
1739 * Allocate at least MinWater
1740 * Double allocation size each time
1741 * But don't go much above HiWater
1743 m = max(min(2*(w->nr+n), HiWater), w->nr+n)+MinWater;
1745 m = max(HiWater+MinWater, w->nr+n);
1747 w->r = runerealloc(w->r, m);
1751 runemove(w->r+q0+n, w->r+q0, w->nr-q0);
1752 runemove(w->r+q0, r, n);
1754 /* if output touches, advance selection, not qh; works best for keyboard and output */
1763 else if(q0 <= w->org+w->nchars)
1764 frinsert(w, r, r+n, q0-w->org);
1774 while(w->lastlinefull == FALSE){
1775 n = w->nr-(w->org+w->nchars);
1778 if(n > 2000) /* educated guess at reasonable amount */
1780 rp = w->r+(w->org+w->nchars);
1783 * it's expensive to frinsert more than we need, so
1786 nl = w->maxlines-w->nlines;
1789 if(rp[i++] == '\n'){
1795 frinsert(w, rp, rp+i, w->nchars);
1800 wcontents(Window *w, int *ip)
1802 return runetobyte(w->r, w->nr, ip);