22 void servedevtext(void);
23 void listenproc(void*);
24 void textthread(void*);
26 typedef struct Text Text;
27 typedef struct Readbuf Readbuf;
31 HiWater = 640000, /* max size of history */
32 LoWater = 400000, /* min size of history after max'ed */
36 /* various geometric paramters */
39 Scrollwid = 12, /* width of scroll bar */
40 Scrollgap = 4, /* gap right of scroll bar */
56 #define CUT 0x18 /* ctrl-x */
57 #define COPY 0x03 /* crtl-c */
58 #define PASTE 0x16 /* crtl-v */
60 #define READBUFSIZE 8192
67 Frame *f; /* frame ofr terminal */
69 uint nr; /* num of runes in term */
70 uint maxr; /* max num of runes in r */
71 Rune *r; /* runes for term */
72 uint nraw; /* num of runes in raw buffer */
73 Rune *raw; /* raw buffer */
74 uint org; /* first rune on the screen */
75 uint q0; /* start of selection region */
76 uint q1; /* end of selection region */
77 uint qh; /* unix point */
78 int npart; /* partial runes read from console */
80 int nsnarf; /* snarf buffer */
86 short n; /* # bytes in buf */
87 uchar data[READBUFSIZE]; /* data bytes */
98 void runewrite(Rune*, int);
100 void conswrite(char*, int);
101 int bswidth(Rune c, uint start, int eatnl);
103 void paste(Rune*, int, int);
104 void snarfupdate(void);
108 void setorigin(uint org, int exact);
110 uint backnl(uint, uint);
112 uint backnl(uint, uint);
113 void addraw(Rune*, int);
115 void doubleclick(uint *q0, uint *q1);
116 int clickmatch(int cl, int cr, int dir, uint *q);
117 Rune *strrune(Rune *s, Rune c);
119 Rectangle scrpos(Rectangle r, ulong p0, ulong p1, ulong tot);
122 void hostproc(void *arg);
123 void hoststart(void);
124 void plumbstart(void);
125 void plumb(uint, uint);
126 void plumbclick(uint*, uint*);
127 uint insert(Rune*, int, uint, int);
128 void scrolldown(int);
131 #define runemalloc(n) malloc((n)*sizeof(Rune))
132 #define runerealloc(a, n) realloc(a, (n)*sizeof(Rune))
133 #define runemove(a, b, n) memmove(a, b, (n)*sizeof(Rune))
134 Rectangle scrollr; /* scroll bar rectangle */
135 Rectangle lastsr; /* used for scroll bar */
136 int holdon; /* hold mode */
137 int rawon(void); /* raw mode */
138 int cooked; /* force cooked */
139 int scrolling; /* window scrolls */
140 int clickmsec; /* time of last click */
141 uint clickq0; /* point of last click */
143 int sfd; /* slave fd, to get/set terminal mode */
155 int label(Rune*, int);
157 char childwdir[1024];
158 void hangupnote(void*, char*);
187 Cursor whitearrow = {
189 {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0xFF, 0xFC,
190 0xFF, 0xF0, 0xFF, 0xF0, 0xFF, 0xF8, 0xFF, 0xFC,
191 0xFF, 0xFE, 0xFF, 0xFF, 0xFF, 0xFE, 0xFF, 0xFC,
192 0xF3, 0xF8, 0xF1, 0xF0, 0xE0, 0xE0, 0xC0, 0x40, },
193 {0xFF, 0xFF, 0xFF, 0xFF, 0xC0, 0x06, 0xC0, 0x1C,
194 0xC0, 0x30, 0xC0, 0x30, 0xC0, 0x38, 0xC0, 0x1C,
195 0xC0, 0x0E, 0xC0, 0x07, 0xCE, 0x0E, 0xDF, 0x1C,
196 0xD3, 0xB8, 0xF1, 0xF0, 0xE0, 0xE0, 0xC0, 0x40, }
201 {0x0f, 0xf0, 0x1f, 0xf8, 0x3f, 0xfc, 0x7f, 0xfe,
202 0x7c, 0x7e, 0x78, 0x7e, 0x00, 0xfc, 0x01, 0xf8,
203 0x03, 0xf0, 0x07, 0xe0, 0x07, 0xc0, 0x07, 0xc0,
204 0x07, 0xc0, 0x07, 0xc0, 0x07, 0xc0, 0x07, 0xc0, },
205 {0x00, 0x00, 0x0f, 0xf0, 0x1f, 0xf8, 0x3c, 0x3c,
206 0x38, 0x1c, 0x00, 0x3c, 0x00, 0x78, 0x00, 0xf0,
207 0x01, 0xe0, 0x03, 0xc0, 0x03, 0x80, 0x03, 0x80,
208 0x00, 0x00, 0x03, 0x80, 0x03, 0x80, 0x00, 0x00, }
214 fprint(2, "usage: 9term [-ars] [-W winsize] [cmd ...]\n");
215 threadexitsall("usage");
219 threadmain(int argc, char *argv[])
226 _wantfocuschanges = 1;
231 case 'a': /* acme mode */
235 font = EARGF(usage());
240 case 'w': /* started from "rio" window manager */
244 winsize = EARGF(usage());
249 putenv("font", font);
251 p = getenv("tabstop");
253 p = getenv("TABSTOP");
254 if(p != 0 && maxtab <= 0)
255 maxtab = strtoul(p, 0, 0);
257 maxtab = 4; /* be like rio */
259 snprint(buf, sizeof buf, "%d", maxtab);
260 putenv("tabstop", buf);
262 initdraw(0, nil, "9term");
264 noteenable("sys: child");
267 mc = initmouse(nil, screen);
268 kc = initkeyboard(nil);
269 rcpid = rcstart(argc, argv, &rcfd, &sfd);
273 t.f = mallocz(sizeof(Frame), 1);
276 cols[BACK] = allocimagemix(display, DPaleyellow, DWhite);
277 cols[HIGH] = allocimage(display, Rect(0, 0, 1, 1), screen->chan, 1, DDarkyellow);
278 cols[BORD] = allocimage(display, Rect(0, 0, 1, 1), screen->chan, 1, DYellowgreen);
280 cols[BACK] = allocimage(display, Rect(0, 0, 1, 1), screen->chan, 1, DWhite);
281 cols[HIGH] = allocimage(display, Rect(0, 0, 1, 1), screen->chan, 1, 0xCCCCCCFF);
282 cols[BORD] = allocimage(display, Rect(0, 0, 1, 1), screen->chan, 1, 0x999999FF);
284 cols[TEXT] = display->black;
285 cols[HTEXT] = display->black;
286 palegrey = allocimage(display, Rect(0, 0, 1, 1), screen->chan, 1, 0x666666FF);
288 hcols[BACK] = cols[BACK];
289 hcols[HIGH] = cols[HIGH];
290 blue = allocimage(display, Rect(0, 0, 1, 1), screen->chan, 1, DMedblue);
291 paleblue = allocimage(display, Rect(0, 0, 1, 1), screen->chan, 1, DGreyblue);
294 hcols[TEXT] = hcols[BORD];
295 hcols[HTEXT] = hcols[TEXT];
297 plumbcolor = allocimage(display, Rect(0,0,1,1), screen->chan, 1, 0x006600FF);
298 execcolor = allocimage(display, Rect(0,0,1,1), screen->chan, 1, 0xAA0000FF);
300 if(!blue || !palegrey || !paleblue || !plumbcolor || !execcolor)
301 sysfatal("alloc colors: %r");
302 draw(screen, screen->r, cols[BACK], nil, ZP);
310 return r=='_' || ('0' <= r && r <= '9') || isalpharune(r);
314 hangupnote(void *a, char *msg)
316 if(getpid() != mainpid)
318 if(strcmp(msg, "hangup") == 0 && rcpid != 0){
319 postnote(PNGROUP, rcpid, "hangup");
322 if(strstr(msg, "child")){
326 n = awaitnohang(buf, sizeof buf-1);
329 if(atoi(buf) == rcpid)
347 /* Let typing have a go -- maybe there's a rubout waiting. */
350 i = 1-i; /* toggle */
351 n = read(rcfd, rcbuf[i].data, sizeof rcbuf[i].data);
354 fprint(2, "9term: host read error: %r\n");
355 threadexitsall("host");
366 hostc = chancreate(sizeof(int), 0);
367 proccreate(hostproc, hostc, 32*1024);
389 a[3].c = mc->resizec;
401 flushimage(display, 1);
403 if(!scrolling && t.qh > t.org+t.f->nchars)
407 sysfatal("impossible");
416 conswrite((char*)rcbuf[i].data, rcbuf[i].n);
428 if(getwindow(display, Refnone) < 0)
429 sysfatal("can't reattach to window");
430 draw(screen, screen->r, cols[BACK], nil, ZP);
443 cols[TEXT] = cols[HTEXT] = display->black;
444 hcols[TEXT] = hcols[HTEXT] = blue;
446 cols[TEXT] = cols[HTEXT] = palegrey;
447 hcols[TEXT] = hcols[HTEXT] = paleblue;
456 scrollr.max.x = r.min.x+Scrollwid;
457 lastsr = Rect(0,0,0,0);
459 r.min.x += Scrollwid+Scrollgap;
462 frinit(t.f, r, font, screen, holdon ? hcols : cols);
463 t.f->maxtab = maxtab*stringwidth(font, "0");
467 p = stringsize(font, "0");
468 if(p.x == 0 || p.y == 0)
471 updatewinsize(Dy(r)/p.y, Dx(r)/p.x, Dx(r), Dy(r));
478 setcursor(mc, &whitearrow);
482 draw(screen, screen->r, cols[BACK], nil, ZP);
488 wordclick(uint *q0, uint *q1)
490 while(*q1<t.nr && !isspace(t.r[*q1]))
492 while(*q0>0 && !isspace(t.r[*q0-1]))
497 aselect(uint *q0, uint *q1, Image *color)
500 uint oldq0, oldq1, newq0, newq1;
502 /* save old selection */
506 /* sweep out area and record it */
507 t.f->cols[HIGH] = color;
508 t.f->cols[HTEXT] = display->white;
514 if(t.m.buttons != 0){
522 /* restore old selection */
523 t.f->cols[HIGH] = cols[HIGH];
524 t.f->cols[HTEXT] = cols[HTEXT];
532 /* selected a region */
539 /* clicked inside previous selection */
540 /* the "<=" in newq0 <= oldq1 allows us to click the right edge */
541 if(oldq0 <= newq0 && newq0 <= oldq1){
553 static Rune Lnl[1] = { '\n' };
563 if(but != 1 && but != 2 && but != 4 && but != 8 && but != 16)
566 if (ptinrect(t.m.xy, scrollr)) {
568 if(t.qh<=t.org+t.f->nchars)
579 if(aselect(&q0, &q1, execcolor) >= 0){
586 paste(t.r+q0, q1-q0, 1);
587 if(t.r[q1-1] != '\n')
598 if(aselect(&q0, &q1, plumbcolor) >= 0)
603 scrollup(mousescrollsize(t.f->maxlines));
606 scrolldown(mousescrollsize(t.f->maxlines));
618 q0 = frcharofpt(t.f, t.m.xy) + t.org;
619 if(t.m.msec-clickmsec<500 && clickq0==q0 && t.q0==t.q1 && b==1){
620 doubleclick(&t.q0, &t.q1);
622 /* t.t.i->flush(); */
625 /* stay here until something interesting happens */
629 } while(t.m.buttons==b && abs(t.m.xy.x-x)<4 && abs(t.m.xy.y-y)<4);
630 t.m.xy.x = x; /* in case we're calling frselect */
635 if(t.m.buttons == b) {
638 t.q0 = t.f->p0 + t.org;
639 t.q1 = t.f->p1 + t.org;
640 clickmsec = t.m.msec;
643 if((t.m.buttons != b) &&(b&1)){
644 enum{Cancut = 1, Canpaste = 2} state = Cancut | Canpaste;
652 }else if(t.m.buttons&4){
656 paste(t.snarf, t.nsnarf, 0);
667 Rune newline[] = { '\n', 0 };
673 menu2str[Scroll] = "+ scroll";
675 menu2str[Scroll] = "- scroll";
677 menu2str[Cooked] = "+ mustecho";
679 menu2str[Cooked] = "- mustecho";
681 switch(menuhit(but, mc, &menu2, nil)){
692 paste(t.snarf, t.nsnarf, 0);
708 paste(t.snarf, t.nsnarf, 1);
709 if(t.nsnarf == 0 || t.snarf[t.nsnarf-1] != '\n')
710 paste(newline, 1, 1);
715 scrolling = !scrolling;
728 sysfatal("bad menu item");
733 windfilewidth(uint q0, int oneelement)
743 if(oneelement && r=='/')
751 showcandidates(Completion *c)
761 s = "[no matches in ";
765 fmtprint(&f, "%s%d files]\n", s, c->nfile);
767 fmtprint(&f, "%s", s);
768 for(i=0; i<c->nfile; i++){
771 fmtprint(&f, "%s", c->filename[i]);
775 /* place text at beginning of line before host point */
777 while(qline>0 && t.r[qline-1] != '\n')
780 rp = runefmtstrflush(&f);
784 q0 += insert(rp, nr, qline, 0) - qline;
795 Rune *rp, *path, *str;
797 char *s, *dir, *root;
799 /* control-f: filename completion; works back to white space or / */
800 if(t.q0<t.nr && t.r[t.q0]>' ') /* must be at end of word */
802 nstr = windfilewidth(t.q0, TRUE);
803 str = runemalloc(nstr);
804 runemove(str, t.r+(t.q0-nstr), nstr);
805 npath = windfilewidth(t.q0-nstr, FALSE);
806 path = runemalloc(npath);
807 runemove(path, t.r+(t.q0-nstr-npath), npath);
810 /* is path rooted? if not, we need to make it relative to window path */
811 if(npath>0 && path[0]=='/'){
812 dir = malloc(UTFmax*npath+1);
813 sprint(dir, "%.*S", npath, path);
815 if(strcmp(wdir, "") == 0)
819 dir = malloc(strlen(root)+1+UTFmax*npath+1);
820 sprint(dir, "%s/%.*S", root, npath, path);
822 dir = cleanname(dir);
824 s = smprint("%.*S", nstr, str);
825 c = complete(dir, s);
834 rp = runesmprint("%s", c->string);
847 setorigin(backnl(t.org, n), 1);
853 setorigin(line2q(n), 1);
854 if(t.qh<=t.org+t.f->nchars)
868 scrollup(t.f->maxlines*2/3);
871 scrolldown(t.f->maxlines*2/3);
874 scrollup(t.f->maxlines/3);
877 scrolldown(t.f->maxlines/3);
904 * Non-standard extensions.
919 paste(t.snarf, t.nsnarf, 0);
926 * This if used to be below the if(rawon() && t.q0==t.nr),
927 * but let's try putting it here. This will allow ESC-processing
928 * to toggle hold mode even in remote SSH connections.
929 * The drawback is that vi-style processing gets harder.
930 * If you find yourself in some weird readline mode, good
931 * luck getting out without ESC. Let's see who complains.
933 if(r==ESC){ /* toggle hold */
936 /* replaceintegerproperty("_9WM_HOLD_MODE", 1, 32, holdon); */
942 if(!holdon && rawon() && t.q0 == t.nr){
948 if(r == 0x7F){ /* DEL: send interrupt; what a mess */
953 t.qh = t.q0 = t.q1 = t.nr;
955 write(rcfd, "\x7F", 1);
962 case 0x06: /* ^F: file name completion */
963 case Kins: /* Insert: file name completion */
971 case 0x08: /* ^H: erase character */
972 case 0x15: /* ^U: erase line */
973 case 0x17: /* ^W: erase word */
974 if (t.q0 != 0 && t.q0 != t.qh)
975 t.q0 -= bswidth(r, t.q0, 1);
987 bswidth(Rune c, uint start, int eatnl)
993 /* there is known to be at least one character to erase */
994 if(c == 0x08) /* ^H: erase character */
1003 if(r == '\n'){ /* eat at most one more character */
1004 if(q == start && eatnl) /* eat the newline */
1010 if(eq && skipping) /* found one; stop skipping */
1012 else if(!eq && !skipping)
1029 return t.nraw != 0 || t.qh < t.nr;
1031 /* look to see if there is a complete line */
1032 for(i=t.qh; i<t.nr; i++){
1034 if(c=='\n' || c=='\004' || c=='\x7F')
1055 while(n >= UTFmax && (t.qh<t.nr || t.nraw > 0)) {
1057 width = runetochar(p, &t.raw[0]);
1059 runemove(t.raw, t.raw+1, t.nraw);
1061 width = runetochar(p, &t.r[t.qh++]);
1065 if(!raw && (c == '\n' || c == '\004' || c == '\x7F'))
1071 * If we've been echoing, make sure the terminal isn't
1072 * while we do the write. This screws up if someone
1073 * else tries to turn off echo at the same time we do
1074 * (we'll turn it on again after the write), but that's not
1077 s = setecho(sfd, 0);
1078 if(write(rcfd, buf, n) < 0)
1086 conswrite(char *p, int n)
1089 Rune buf2[1000], *q;
1091 /* convert to runes */
1094 /* handle partial runes */
1095 while(i < UTFmax && n>0) {
1100 if(fullrune(t.part, i)) {
1102 chartorune(buf2, t.part);
1107 /* there is a little extra room in a message buf */
1110 while(n >= UTFmax || fullrune(p, n)) {
1115 if(n < UTFmax && !fullrune(p, n))
1117 i = chartorune(q, p);
1123 runewrite(buf2, q-buf2);
1127 assert(n+t.npart < UTFmax);
1128 memcpy(t.part+t.npart, p, n);
1137 runewrite(Rune *r, int n)
1150 /* process trailing \r from previous write */
1152 if(havecr && *r != '\r' && *r != '\n')
1153 initial = bswidth(0x15, t.qh, 0);
1156 /* get rid of backspaces */
1158 for(i=0; i<n; i++) {
1164 } else if(*p == '\r') { /* treat like ^U */
1165 /* convert CR without NL into erased line */
1166 /* i feel really sleazy about this but it helps */
1167 while(i<n-1 && *(p+1) == '\r'){
1171 if(i<n-1 && *(p+1) != '\n'){
1172 while(q > r && *(q-1) != '\n')
1175 initial = bswidth(0x15, t.qh, 0);
1185 /* write turned into a delete */
1192 runemove(t.r+q0, t.r+q1, t.nr-q1);
1205 else if(q0 < t.org+t.f->nchars){
1213 if(p1 > t.f->nchars)
1215 frdelete(t.f, p0, p1);
1221 insert(r, n, t.qh, 1);
1234 if (q0 < t.org && q1 >= t.org)
1240 runemove(t.r+q0, t.r+q1, t.nr-q1);
1249 else if(q0 < t.org+t.f->nchars){
1250 assert(q0 >= t.org);
1253 if(p1 > t.f->nchars)
1255 frdelete(t.f, p0, p1);
1276 t.snarf = runerealloc(t.snarf, n);
1277 for(i=0,p=t.snarf; i<n; p++)
1278 i += chartorune(p, pp+i);
1279 t.nsnarf = p-t.snarf;
1283 char sbuf[SnarfSize];
1294 t.snarf = runerealloc(t.snarf, n);
1295 for(i=0,p=sbuf,rp=t.snarf; i<n && p < sbuf+SnarfSize-UTFmax; i++){
1296 *rp++ = *(t.r+t.q0+i);
1297 p += runetochar(p, t.r+t.q0+i);
1299 t.nsnarf = rp-t.snarf;
1321 insert(Rune *r, int n, uint q0, int hostwrite)
1327 if(t.nr+n>HiWater && q0>=t.org && q0>=t.qh){
1328 m = min(HiWater-LoWater, min(t.org, t.qh));
1340 runemove(t.r, t.r+m, t.nr);
1343 if(t.nr+n > t.maxr){
1345 * Minimize realloc breakage:
1346 * Allocate at least MinWater
1347 * Double allocation size each time
1348 * But don't go much above HiWater
1350 m = max(min(2*(t.nr+n), HiWater), t.nr+n)+MinWater;
1352 m = max(HiWater+MinWater, t.nr+n);
1354 t.r = runerealloc(t.r, m);
1358 runemove(t.r+q0+n, t.r+q0, t.nr-q0);
1359 runemove(t.r+q0, r, n);
1361 /* if output touches, advance selection, not qh; works best for keyboard and output */
1366 if(q0 < t.qh || (q0==t.qh && hostwrite))
1372 else if(q0 <= t.org+t.f->nchars)
1373 frinsert(t.f, r, r+n, q0-t.org);
1378 paste(Rune *r, int n, int advance)
1382 if(!holdon && rawon() && t.q0==t.nr){
1393 * if this is a button2 execute then we might have been passed
1394 * runes inside the buffer. must save them before realloc.
1397 if(t.r <= r && r < t.r+n){
1398 rbuf = runemalloc(n);
1399 runemove(rbuf, r, n);
1403 insert(r, n, t.q0, 0);
1411 if (t.f->nlines >= t.f->maxlines)
1413 frinsert(t.f, t.r + t.org + t.f->nchars, t.r + t.nr, t.f->nchars);
1423 if(t.org+f->p0 == t.q0 && t.org+f->p1 == t.q1)
1428 frdrawsel(f, frptofchar(f, f->p0), f->p0, f->p1, 0);
1441 frdrawsel(f, frptofchar(f, f->p0), f->p0, f->p1, 1);
1444 if(t.qh<=t.org+t.f.nchars && t.cwqueue != 0)
1445 t.cwqueue->wakeup <-= 0;
1461 nl = t.f->maxlines/5;
1463 nl = 4*t.f->maxlines/5;
1465 /* avoid going in the wrong direction */
1466 if (q0>t.org && q<t.org)
1469 /* keep trying until q0 is on the screen */
1470 while(!cansee(q0)) {
1471 assert(q0 >= t.org);
1473 q = line2q(t.f->maxlines-nl);
1484 qe = t.org+t.f->nchars;
1486 if(q0>=t.org && q0 < qe)
1490 if (t.f->nlines < t.f->maxlines)
1492 if (q0 > 0 && t.r[t.nr-1] == '\n')
1499 setorigin(uint org, int exact)
1504 if(org>0 && !exact){
1505 /* try and start after a newline */
1506 /* don't try harder than 256 chars */
1507 for(i=0; i<256 && org<t.nr; i++){
1508 if(t.r[org-1] == '\n')
1515 if(a>=0 && a<t.f->nchars)
1516 frdelete(t.f, 0, a);
1517 else if(a<0 && -a<100*t.f->maxlines){
1519 frinsert(t.f, t.r+org, t.r+org+n, 0);
1521 frdelete(t.f, 0, t.f->nchars);
1534 return frcharofpt(f, Pt(f->r.min.x, f->r.min.y + n*font->height))+t.org;
1538 backnl(uint p, uint n)
1543 /* at 256 chars, call it a line anyway */
1544 for(j=256; --j>0 && p>0; p--)
1547 if (p == 0 || i == 0)
1554 addraw(Rune *r, int nr)
1556 t.raw = runerealloc(t.raw, t.nraw+nr);
1557 runemove(t.raw+t.nraw, r, nr);
1560 if(t.crqueue != nil)
1561 t.crqueue->wakeup <-= 0;
1566 Rune left1[] = { '{', '[', '(', '<', 0xab, 0 };
1567 Rune right1[] = { '}', ']', ')', '>', 0xbb, 0 };
1568 Rune left2[] = { '\n', 0 };
1569 Rune left3[] = { '\'', '"', '`', 0 };
1586 doubleclick(uint *q0, uint *q1)
1592 for(i=0; left[i]!=0; i++){
1596 /* try matching character to left, looking right */
1603 if(clickmatch(c, r[p-l], 1, &q))
1607 /* try matching character to right, looking left */
1614 if(clickmatch(c, l[p-r], -1, &q)){
1615 *q1 = *q0+(*q0<t.nr && c=='\n');
1617 if(c!='\n' || q!=0 || t.r[0]=='\n')
1623 /* try filling out word to right */
1624 while(*q1<t.nr && isexpand(t.r[*q1]))
1626 /* try filling out word to left */
1627 while(*q0>0 && isexpand(t.r[*q0-1]))
1632 clickmatch(int cl, int cr, int dir, uint *q)
1656 return cl=='\n' && nest==1;
1666 assert(t.q0 <= t.q1 && t.q1 <= t.nr);
1667 assert(t.org <= t.nr && t.qh <= t.nr);
1668 assert(f->p0 <= f->p1 && f->p1 <= f->nchars);
1669 assert(t.org + f->nchars <= t.nr);
1670 assert(t.org+f->nchars==t.nr || (f->nlines >= f->maxlines));
1674 strrune(Rune *s, Rune c)
1693 Rectangle r, r1, r2;
1697 r.min.x += 1; /* border between margin and bar */
1699 if(scrx==0 || scrx->r.max.y < r.max.y){
1702 scrx = allocimage(display, Rect(0, 0, 32, r.max.y), screen->chan, 1, DPaleyellow);
1704 sysfatal("scroll balloc");
1708 r2 = scrpos(r1, t.org, t.org+t.f->nchars, t.nr);
1709 if(!eqrect(r2, lastsr)){
1711 draw(scrx, r1, cols[BORD], nil, ZP);
1712 draw(scrx, r2, cols[BACK], nil, r2.min);
1714 // r2.min.x = r2.max.x-1;
1715 // draw(scrx, r2, cols[BORD], nil, ZP);
1716 draw(screen, r, scrx, nil, r1.min);
1721 scrpos(Rectangle r, ulong p0, ulong p1, ulong tot)
1726 q = insetrect(r, 1);
1727 h = q.max.y-q.min.y;
1730 if(tot > 1024L*1024L)
1731 tot >>= 10, p0 >>= 10, p1 >>= 10;
1733 q.min.y += h*p0/tot;
1735 q.max.y -= h*(tot-p1)/tot;
1736 if(q.max.y < q.min.y+2){
1737 if(q.min.y+2 <= r.max.y)
1738 q.max.y = q.min.y+2;
1740 q.min.y = q.max.y-2;
1750 int x, y, my, h, first, exact;
1752 s = insetrect(scrollr, 1);
1753 h = s.max.y-s.min.y;
1754 x = (s.min.x+s.max.x)/2;
1758 if(t.m.xy.x<s.min.x || s.max.x<=t.m.xy.x){
1767 // if(!eqpt(t.m.xy, Pt(x, my)))
1768 // cursorset(Pt(x, my));
1774 if(t.nr > 1024*1024)
1775 p0 = ((t.nr>>10)*(y-s.min.y)/h)<<10;
1777 p0 = t.nr*(y-s.min.y)/h;
1780 p0 = backnl(t.org, (my-s.min.y)/font->height);
1782 p0 = t.org+frcharofpt(t.f, Pt(s.max.x, my));
1785 setorigin(p0, exact);
1791 }while(t.m.buttons & (1<<(but-1)));
1797 if((plumbfd = plumbopen("send", OWRITE)) < 0)
1798 fprint(2, "9term: plumbopen: %r\n");
1802 plumb(uint q0, uint q1)
1809 pm = malloc(sizeof(Plumbmsg));
1810 pm->src = strdup("9term");
1812 pm->wdir = strdup(wdir);
1813 pm->type = strdup("text");
1819 wordclick(&q0, &q1);
1820 sprint(cbuf, "click=%d", p0-q0);
1821 pm->attr = plumbunpackattr(cbuf);
1827 pm->data = malloc(SnarfSize);
1829 for(i=0,p=pm->data; i<n && p < pm->data + SnarfSize-UTFmax; i++)
1830 p += runetochar(p, t.r+q0+i);
1832 pm->ndata = strlen(pm->data);
1833 if(plumbsend(plumbfd, pm) < 0){
1834 setcursor(mc, &query);
1837 setcursor(mc, &whitearrow);
1845 * Process in-band messages about window title changes.
1846 * The messages are of the form:
1850 * where xxx is the new directory. This format was chosen
1851 * because it changes the label on xterm windows.
1854 label(Rune *sr, int n)
1856 Rune *sl, *el, *er, *r;
1860 for(r=er-1; r>=sr; r--)
1867 if(el-sr > sizeof wdir)
1868 sr = el - sizeof wdir;
1869 for(sl=el-3; sl>=sr; sl--)
1870 if(sl[0]=='\033' && sl[1]==']' && sl[2]==';')
1875 snprint(wdir, sizeof wdir, "%.*S", (el-1)-(sl+3), sl+3);
1878 /* remove trailing /-sysname if present */
1879 p = strrchr(wdir, '/');
1880 if(p && *(p+1) == '-'){
1886 runemove(sl, el, er-el);
1894 return !cooked && !isecho(sfd);
1898 * Clumsy hack to make " and "" work.
1899 * Then again, what's not a clumsy hack here in Unix land?
1906 removethesocket(void)
1909 if(remove(thesocket) < 0)
1910 fprint(2, "remove %s: %r\n", thesocket);
1918 snprint(buf, sizeof buf, "unix!/tmp/9term-text.%d", getpid());
1920 if((afd = announce(buf, adir)) < 0){
1921 putenv("text9term", "");
1925 putenv("text9term", buf);
1926 proccreate(listenproc, nil, STACK);
1927 strcpy(thesocket, buf+5);
1928 atexit(removethesocket);
1932 listenproc(void *arg)
1939 fd = listen(adir, dir);
1944 proccreate(textthread, (void*)fd, STACK);
1949 textthread(void *arg)
1951 int fd, i, x, n, end;
1953 char buf[4096], *p, *ep;
1957 ep = buf+sizeof buf;
1958 end = t.org+t.nr; /* avoid possible output loop */
1960 if(i >= end || ep-p < UTFmax){
1961 for(x=0; x<p-buf; x+=n)
1962 if((n = write(fd, buf+x, (p-x)-buf)) <= 0)
1975 p += runetochar(p, &r);