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);
49 w->scrollr.max.x = r.min.x+Scrollwid;
51 r.min.x += Scrollwid+Scrollgap;
52 frinit(w, r, font, i, cols);
53 w->maxtab = maxtab*stringwidth(font, "0");
57 w->scrolling = scrolling;
58 w->dir = estrdup(startdir);
59 w->label = estrdup("<unnamed>");
60 r = insetrect(w->i->r, Selborder);
61 draw(w->i, r, cols[BACK], nil, w->entire.min);
62 wborder(w, Selborder);
64 incref(w); /* ref will be removed after mounting; avoids delete before ready to be deleted */
74 n = snprint(w->name, sizeof(w->name)-2, "window.%d.%d", w->id, w->namecount++);
75 for(i='A'; i<='Z'; i++){
76 if(nameimage(w->i, w->name, 1) > 0)
78 errstr(err, sizeof err);
79 if(strcmp(err, "image name in use") != 0)
85 fprint(2, "rio: setname failed: %s\n", err);
89 wresize(Window *w, Image *i, int move)
94 if(move || (Dx(or)==Dx(i->r) && Dy(or)==Dy(i->r)))
95 draw(i, i->r, w->i, nil, w->i->r.min);
99 r = insetrect(i->r, Selborder+1);
101 w->scrollr.max.x = r.min.x+Scrollwid;
103 r.min.x += Scrollwid+Scrollgap;
105 frsetrects(w, r, w->i);
108 frinit(w, r, w->font, w->i, cols);
110 w->maxtab = maxtab*stringwidth(w->font, "0");
111 r = insetrect(w->i->r, Selborder);
112 draw(w->i, r, cols[BACK], nil, w->entire.min);
114 wsetselect(w, w->q0, w->q1);
117 wborder(w, Selborder);
119 w->topped = ++topped;
126 wrefresh(Window *w, Rectangle r)
128 /* BUG: rectangle is ignored */
130 wborder(w, Selborder);
132 wborder(w, Unselborder);
133 r = insetrect(w->i->r, Selborder);
134 draw(w->i, r, w->cols[BACK], nil, w->entire.min);
137 frdrawsel(w, frptofchar(w, 0), 0, w->p0, 0);
138 if(w->p1 < w->nchars)
139 frdrawsel(w, frptofchar(w, w->p1), w->p1, w->nchars, 0);
140 frdrawsel(w, frptofchar(w, w->p0), w->p0, w->p1, 1);
154 error("negative ref count");
157 wsendctlmesg(w, Exited, ZR, nil);
162 showcandidates(Window *, Completion *);
167 Rune *rp, *bp, *tp, *up;
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, 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[NWALT].op = CHANEND;
227 memset(kbdq, 0, sizeof(kbdq));
232 alts[WKbdread].op = (w->kbdopen && kbdqw != kbdqr) ?
234 alts[WMouseread].op = (w->mouseopen && w->mouse.counter != w->mouse.lastcounter) ?
236 alts[WCwrite].op = (!w->scrolling && !w->mouseopen && w->qh>w->org+w->nchars) ?
238 alts[WWread].op = (w->deleted || !w->wctlready) ?
241 /* this code depends on NL and EOT fitting in a single byte */
242 /* kind of expensive for each loop; worth precomputing? */
244 alts[WCread].op = CHANNOP;
245 else if(npart || (w->rawing && w->nraw>0))
246 alts[WCread].op = CHANSND;
248 alts[WCread].op = CHANNOP;
249 for(i=w->qh; i<w->nr; i++){
251 if(c=='\n' || c=='\004'){
252 alts[WCread].op = CHANSND;
260 i = (kbdqw+1) % nelem(kbdq);
263 } else if(*kbds == 'c'){
266 chartorune(&r, kbds+1);
275 i = (kbdqr+1) % nelem(kbdq);
279 sendp(krm.ck, kbdq[i]);
282 sendp(krm.ck, strdup("K"));
289 /* queue click events */
290 if(!w->mouse.qfull && lastb != w->mc.buttons) { /* add to ring */
291 mp = &w->mouse.queue[w->mouse.wi];
292 if(++w->mouse.wi == nelem(w->mouse.queue))
294 if(w->mouse.wi == w->mouse.ri)
295 w->mouse.qfull = TRUE;
297 mp->counter = w->mouse.counter;
298 lastb = w->mc.buttons;
304 /* send a queued event or, if the queue is empty, the current state */
305 /* if the queue has filled, we discard all the events it contained. */
306 /* the intent is to discard frantic clicking by the user during long latencies. */
307 w->mouse.qfull = FALSE;
308 if(w->mouse.wi != w->mouse.ri) {
309 m = w->mouse.queue[w->mouse.ri];
310 if(++w->mouse.ri == nelem(w->mouse.queue))
313 m = (Mousestate){w->mc.Mouse, w->mouse.counter};
315 w->mouse.lastcounter = m.counter;
316 send(mrm.cm, &m.Mouse);
319 if(wctlmesg(w, wcm.type, wcm.r, wcm.p) == Exited){
320 for(i=0; i<nelem(kbdq); i++)
358 wdelete(w, qh, qh+initial);
367 w->qh = winsert(w, rp, nr, w->qh)+nr;
368 if(w->scrolling || w->mouseopen)
370 wsetselect(w, w->q0, w->q1);
382 while(i<nb && (w->qh<w->nr || w->nraw>0)){
384 wid = runetochar(t+i, &w->raw[0]);
386 runemove(w->raw, w->raw+1, w->nraw);
388 wid = runetochar(t+i, &w->r[w->qh++]);
389 c = t[i]; /* knows break characters fit in a byte */
391 if(!w->rawing && (c == '\n' || c=='\004')){
397 if(i==nb && w->qh<w->nr && w->r[w->qh]=='\004')
401 memmove(part, t+nb, npart);
410 recv(cwrm.c1, &pair);
411 if(w->deleted || w->i==nil)
412 pair.ns = sprint(pair.s, "");
415 for(i=0; i<nhidden; i++)
423 pair.ns = snprint(pair.s, pair.ns, "%11d %11d %11d %11d %s %s ",
424 w->i->r.min.x, w->i->r.min.y, w->i->r.max.x, w->i->r.max.y, t, s);
426 send(cwrm.c2, &pair);
431 showcandidates(w, cr);
433 rp = runesmprint("%s", cr->string);
436 q0 = winsert(w, rp, nr, q0);
445 flushimage(display, 1);
450 waddraw(Window *w, Rune *r, int nr)
452 w->raw = runerealloc(w->raw, w->nraw+nr);
453 runemove(w->raw+w->nraw, r, nr);
458 * Need to do this in a separate proc because if process we're interrupting
459 * is dying and trying to print tombstone, kernel is blocked holding p->debug lock.
462 interruptproc(void *v)
467 write(*notefd, "interrupt", 9);
473 windfilewidth(Window *w, uint q0, int oneelement)
483 if(oneelement && r=='/')
491 showcandidates(Window *w, Completion *c)
501 s = "[no matches in ";
505 fmtprint(&f, "%s%d files]\n", s, c->nfile);
507 fmtprint(&f, "%s", s);
508 for(i=0; i<c->nfile; i++){
511 fmtprint(&f, "%s", c->filename[i]);
515 rp = runefmtstrflush(&f);
518 /* place text at beginning of line before cursor and host point */
519 qline = min(w->qh, w->q0);
520 while(qline>0 && w->r[qline-1] != '\n')
524 /* advance host point to avoid readback */
525 w->qh = winsert(w, rp, nr, qline)+nr;
527 winsert(w, rp, nr, qline);
532 typedef struct Completejob Completejob;
541 completeproc(void *arg)
548 snprint(buf, sizeof(buf), "namecomplete %s", job->dir);
551 c = complete(job->dir, job->str);
552 if(c != nil && sendp(job->win->complete, c) <= 0)
563 namecomplete(Window *w)
570 /* control-f: filename completion; works back to white space or / */
571 if(w->q0<w->nr && w->r[w->q0]>' ') /* must be at end of word */
573 nstr = windfilewidth(w, w->q0, TRUE);
574 str = runemalloc(nstr);
575 runemove(str, w->r+(w->q0-nstr), nstr);
576 npath = windfilewidth(w, w->q0-nstr, FALSE);
577 path = runemalloc(npath);
578 runemove(path, w->r+(w->q0-nstr-npath), npath);
580 /* is path rooted? if not, we need to make it relative to window path */
581 if(npath>0 && path[0]=='/'){
582 dir = malloc(UTFmax*npath+1);
583 sprint(dir, "%.*S", npath, path);
585 if(strcmp(w->dir, "") == 0)
589 dir = malloc(strlen(root)+1+UTFmax*npath+1);
590 sprint(dir, "%s/%.*S", root, npath, path);
593 /* run in background, winctl will collect the result on w->complete chan */
594 job = emalloc(sizeof *job);
595 job->str = smprint("%.*S", nstr, str);
596 job->dir = cleanname(dir);
599 proccreate(completeproc, job, STACK);
606 wkeyctl(Window *w, Rune r)
625 /* navigation keys work only when mouse and kbd is not open */
629 n = shiftdown ? 1 : w->maxlines/3;
632 n = mousescrollsize(w->maxlines);
639 q0 = w->org+frcharofpt(w, Pt(w->Frame.r.min.x, w->Frame.r.min.y+n*w->font->height));
640 wsetorigin(w, q0, TRUE);
643 n = shiftdown ? 1 : w->maxlines/3;
646 n = mousescrollsize(w->maxlines);
653 q0 = wbacknl(w, w->org, n);
654 wsetorigin(w, q0, TRUE);
659 wsetselect(w, q0, q0);
666 wsetselect(w, q1, q1);
680 case Ksoh: /* ^A: beginning of line */
681 if(w->q0==0 || w->q0==w->qh || w->r[w->q0-1]=='\n')
683 nb = wbswidth(w, 0x15 /* ^U */);
684 wsetselect(w, w->q0-nb, w->q0-nb);
687 case Kenq: /* ^E: end of line */
689 while(q0 < w->nr && w->r[q0]!='\n')
691 wsetselect(w, q0, q0);
694 case Kstx: /* ^B: output point */
695 wsetselect(w, w->qh, w->qh);
699 if(w->rawing && (w->q0==w->nr || w->mouseopen)){
703 if(r==Kesc || (w->holding && r==Kdel)){ /* toggle hold */
718 case Kdel: /* send interrupt */
721 notefd = emalloc(sizeof(int));
722 *notefd = dup(w->notefd, -1);
723 proccreate(interruptproc, notefd, 4096);
725 case Kack: /* ^F: file name completion */
726 case Kins: /* Insert: file name completion */
729 case Kbs: /* ^H: erase character */
730 case Knack: /* ^U: erase line */
731 case Ketb: /* ^W: erase word */
732 if(w->q0==0 || w->q0==w->qh)
742 wdelete(w, q0, q0+nb);
743 wsetselect(w, q0, q0);
747 /* otherwise ordinary character; just insert */
749 q0 = winsert(w, &r, 1, q0);
754 wsetcols(Window *w, int topped)
758 w->cols[TEXT] = holdcol;
760 w->cols[TEXT] = lightholdcol;
763 w->cols[TEXT] = cols[TEXT];
765 w->cols[TEXT] = paletextcol;
771 wsetcols(w, w == input);
775 wborder(w, Selborder);
777 wborder(w, Unselborder);
781 wbswidth(Window *w, Rune c)
787 /* there is known to be at least one character to erase */
788 if(c == 0x08) /* ^H: erase character */
797 if(r == '\n'){ /* eat at most one more character */
798 if(q == w->q0) /* eat the newline */
804 if(eq && skipping) /* found one; stop skipping */
806 else if(!eq && !skipping)
819 nsnarf = w->q1-w->q0;
820 snarf = runerealloc(snarf, nsnarf);
821 snarfversion++; /* maybe modified by parent */
822 runemove(snarf, w->r+w->q0, nsnarf);
831 wdelete(w, w->q0, w->q1);
832 wsetselect(w, w->q0, w->q0);
844 if(w->rawing && q0==w->nr){
845 waddraw(w, snarf, nsnarf);
846 wsetselect(w, q0, q0);
848 q0 = winsert(w, snarf, nsnarf, w->q0);
849 wsetselect(w, q0, q0+nsnarf);
863 fd = plumbopen("send", OWRITE|OCEXEC);
866 m = emalloc(sizeof(Plumbmsg));
867 m->src = estrdup("rio");
869 m->wdir = estrdup(w->dir);
870 m->type = estrdup("text");
876 while(p0>0 && w->r[p0-1]!=' ' && w->r[p0-1]!='\t' && w->r[p0-1]!='\n')
878 while(p1<w->nr && w->r[p1]!=' ' && w->r[p1]!='\t' && w->r[p1]!='\n')
880 snprint(buf, sizeof(buf), "click=%d", w->q0-p0);
881 m->attr = plumbunpackattr(buf);
883 if(p1-p0 > messagesize-1024){
885 return; /* too large for 9P */
887 m->data = runetobyte(w->r+p0, p1-p0, &m->ndata);
888 if(plumbsend(fd, m) < 0){
890 riosetcursor(&query, 1);
898 winborder(Window *w, Point xy)
900 return ptinrect(xy, w->screenr) && !ptinrect(xy, insetrect(w->screenr, Selborder));
908 if(w->mc.buttons == 1)
910 else if(w->mc.buttons == 2)
912 else if(w->mc.buttons == 4)
915 if(w->mc.buttons == 8)
916 wkeyctl(w, Kscrolloneup);
917 if(w->mc.buttons == 16)
918 wkeyctl(w, Kscrollonedown);
922 incref(w); /* hold up window while we track */
925 if(ptinrect(w->mc.xy, w->scrollr)){
932 /* else all is handled by main process */
938 wdelete(Window *w, uint q0, uint q1)
945 runemove(w->r+q0, w->r+q1, w->nr-q1);
948 w->q0 -= min(n, w->q0-q0);
950 w->q1 -= min(n, w->q1-q0);
957 else if(q0 < w->org+w->nchars){
972 static Window *clickwin;
973 static uint clickmsec;
974 static Window *selectwin;
978 * called from frame library
981 framescroll(Frame *f, int dl)
983 if(f != &selectwin->Frame)
984 error("frameselect not right frame");
985 wframescroll(selectwin, dl);
989 wframescroll(Window *w, int dl)
998 q0 = wbacknl(w, w->org, -dl);
999 if(selectq > w->org+w->p0)
1000 wsetselect(w, w->org+w->p0, selectq);
1002 wsetselect(w, selectq, w->org+w->p0);
1004 if(w->org+w->nchars == w->nr)
1006 q0 = w->org+frcharofpt(w, Pt(w->Frame.r.min.x, w->Frame.r.min.y+dl*w->font->height));
1007 if(selectq >= w->org+w->p1)
1008 wsetselect(w, w->org+w->p1, selectq);
1010 wsetselect(w, selectq, w->org+w->p1);
1012 wsetorigin(w, q0, TRUE);
1024 * Double-click immediately if it might make sense.
1029 selectq = w->org+frcharofpt(w, w->mc.xy);
1030 if(clickwin==w && w->mc.msec-clickmsec<500)
1031 if(q0==q1 && selectq==w->q0){
1032 wdoubleclick(w, &q0, &q1);
1033 wsetselect(w, q0, q1);
1034 flushimage(display, 1);
1037 /* stay here until something interesting happens */
1040 while(w->mc.buttons==b && abs(w->mc.xy.x-x)<3 && abs(w->mc.xy.y-y)<3);
1041 w->mc.xy.x = x; /* in case we're calling frselect */
1043 q0 = w->q0; /* may have changed */
1047 if(w->mc.buttons == b){
1048 w->scroll = framescroll;
1049 frselect(w, &w->mc);
1050 /* horrible botch: while asleep, may have lost selection altogether */
1052 selectq = w->org + w->p0;
1053 w->Frame.scroll = nil;
1054 if(selectq < w->org)
1057 q0 = w->org + w->p0;
1058 if(selectq > w->org+w->nchars)
1064 if(q0==w->q0 && clickwin==w && w->mc.msec-clickmsec<500){
1065 wdoubleclick(w, &q0, &q1);
1069 clickmsec = w->mc.msec;
1073 wsetselect(w, q0, q1);
1074 flushimage(display, 1);
1075 while(w->mc.buttons){
1091 flushimage(display, 1);
1092 while(w->mc.buttons == b)
1099 wsendctlmesg(Window *w, int type, Rectangle r, void *p)
1106 send(w->cctl, &wcm);
1110 wctlmesg(Window *w, int m, Rectangle r, void *p)
1117 error("unknown control message");
1121 sendp((Channel*)p, w);
1130 strcpy(buf, w->name);
1131 wresize(w, i, m==Moved);
1132 proccreate(deletetimeoutproc, estrdup(buf), 4096);
1133 flushimage(display, 1);
1134 if(Dx(r)<=0){ /* window got hidden, if we had the input, drop it */
1139 /* fall through to get input if needed */
1141 if(w->deleted || w==input)
1151 * have to wait until old input responds before
1152 * changing input to us because the window might
1153 * currently be mouse tracking and it is not
1154 * prepared for getting its input revoked.
1156 c = chancreate(sizeof(void*), 0);
1157 wsendctlmesg(oi, Wakeup, ZR, c);
1162 * if we are still top window and nobody else has taken
1163 * input from original window, take the input.
1165 if(!w->deleted && w->topped==topped && oi==input){
1169 wsendctlmesg(oi, Repaint, ZR, nil);
1175 if(m!=Topped && w==input)
1177 /* fall thrugh for redraw after input change */
1179 if(w->deleted || Dx(w->screenr)<=0)
1182 flushimage(display, 1);
1185 if(w->deleted || Dx(w->screenr)<=0 || !rectclip(&r, w->i->r) || w->mouseopen)
1188 flushimage(display, 1);
1191 if(w->deleted || Dx(w->screenr)<=0 || !ptinrect(r.min, w->i->r))
1193 wmovemouse(w, r.min);
1200 wkeyctl(w, w->raw[0]);
1202 runemove(w->raw, w->raw+1, w->nraw);
1212 flushimage(display, 1);
1218 write(w->notefd, "hangup", 6);
1219 proccreate(deletetimeoutproc, estrdup(w->name), 4096);
1229 chanfree(w->conswrite);
1230 chanfree(w->consread);
1231 chanfree(w->mouseread);
1232 chanfree(w->wctlread);
1233 chanfree(w->kbdread);
1234 chanfree(w->complete);
1246 * Convert back to physical coordinates
1249 wmovemouse(Window *w, Point p)
1251 if(w != input || menuing || sweeping)
1253 p.x += w->screenr.min.x-w->i->r.min.x;
1254 p.y += w->screenr.min.y-w->i->r.min.y;
1255 moveto(mousectl, p);
1259 wborder(Window *w, int type)
1266 if(type == Selborder)
1271 if(type == Selborder)
1274 col = lighttitlecol;
1276 border(w->i, w->i->r, Selborder, col, ZP);
1286 for(i=0; i<nwindow; i++){
1288 if(ptinrect(pt, v->screenr))
1289 if(w==nil || v->topped>w->topped)
1298 if(w!=nil && w!=input)
1299 wsendctlmesg(w, Topped, ZR, nil);
1303 wsetcursor(Window *w, int force)
1307 if(w==nil || w->deleted || w->i==nil || Dx(w->screenr)<=0)
1309 else if(wpointto(mouse->xy) == w){
1311 if(p==nil && w->holding)
1316 riosetcursor(p, force && !menuing);
1320 riosetcursor(Cursor *p, int force)
1322 if(!force && p==lastcursor)
1324 setcursor(mousectl, p);
1332 if(w!=nil && w->i!=nil && !w->deleted && w->topped!=topped){
1333 w->topped = ++topped;
1335 flushimage(display, 1);
1340 wbottomme(Window *w)
1342 if(w!=nil && w->i!=nil && !w->deleted){
1343 w->topped = - ++topped;
1345 flushimage(display, 1);
1369 for(i=0; i<nwindow; i++)
1370 if(window[i]->id == id)
1386 for(i=0; i<nhidden; i++)
1389 memmove(hidden+i, hidden+i+1, (nhidden-i)*sizeof(hidden[0]));
1392 for(i=0; i<nwindow; i++)
1395 memmove(window+i, window+i+1, (nwindow-i)*sizeof(window[0]));
1402 wclosewin(Window *w)
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 runemove(r, w->r+org, n);
1642 frinsert(w, r, r+n, 0);
1645 frdelete(w, 0, w->nchars);
1649 wsetselect(w, w->q0, w->q1);
1650 if(fixup && w->p1 > w->p0)
1651 frdrawsel(w, frptofchar(w, w->p1-1), w->p1-1, w->p1, 1);
1655 wsetselect(Window *w, uint q0, uint q1)
1659 /* w->p0 and w->p1 are always right; w->q0 and w->q1 may be off */
1662 /* compute desired p0,p1 from q0,q1 */
1673 if(p0==w->p0 && p1==w->p1)
1675 /* screen disagrees with desired selection */
1676 if(w->p1<=p0 || p1<=w->p0 || p0==p1 || w->p1==w->p0){
1677 /* no overlap or too easy to bother trying */
1678 frdrawsel(w, frptofchar(w, w->p0), w->p0, w->p1, 0);
1679 frdrawsel(w, frptofchar(w, p0), p0, p1, 1);
1682 /* overlap; avoid unnecessary painting */
1684 /* extend selection backwards */
1685 frdrawsel(w, frptofchar(w, p0), p0, w->p0, 1);
1686 }else if(p0 > w->p0){
1687 /* trim first part of selection */
1688 frdrawsel(w, frptofchar(w, w->p0), w->p0, p0, 0);
1691 /* extend selection forwards */
1692 frdrawsel(w, frptofchar(w, w->p1), w->p1, p1, 1);
1693 }else if(p1 < w->p1){
1694 /* trim last part of selection */
1695 frdrawsel(w, frptofchar(w, p1), p1, w->p1, 0);
1704 winsert(Window *w, Rune *r, int n, uint q0)
1710 if(w->nr+n>HiWater && q0>=w->org && q0>=w->qh){
1711 m = min(HiWater-LoWater, min(w->org, w->qh));
1723 runemove(w->r, w->r+m, w->nr);
1726 if(w->nr+n > w->maxr){
1728 * Minimize realloc breakage:
1729 * Allocate at least MinWater
1730 * Double allocation size each time
1731 * But don't go much above HiWater
1733 m = max(min(2*(w->nr+n), HiWater), w->nr+n)+MinWater;
1735 m = max(HiWater+MinWater, w->nr+n);
1737 w->r = runerealloc(w->r, m);
1741 runemove(w->r+q0+n, w->r+q0, w->nr-q0);
1742 runemove(w->r+q0, r, n);
1744 /* if output touches, advance selection, not qh; works best for keyboard and output */
1753 else if(q0 <= w->org+w->nchars)
1754 frinsert(w, r, r+n, q0-w->org);
1766 rp = malloc(messagesize);
1768 n = w->nr-(w->org+w->nchars);
1771 if(n > 2000) /* educated guess at reasonable amount */
1773 runemove(rp, w->r+(w->org+w->nchars), n);
1775 * it's expensive to frinsert more than we need, so
1778 nl = w->maxlines-w->nlines;
1781 if(rp[i++] == '\n'){
1787 frinsert(w, rp, rp+i, w->nchars);
1788 }while(w->lastlinefull == FALSE);
1793 wcontents(Window *w, int *ip)
1795 return runetobyte(w->r, w->nr, ip);