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);
308 hangupnote(void *a, char *msg)
310 if(getpid() != mainpid)
312 if(strcmp(msg, "hangup") == 0 && rcpid != 0){
313 postnote(PNGROUP, rcpid, "hangup");
316 if(strstr(msg, "child")){
333 /* Let typing have a go -- maybe there's a rubout waiting. */
336 i = 1-i; /* toggle */
337 n = read(rcfd, rcbuf[i].data, sizeof rcbuf[i].data);
340 fprint(2, "9term: host read error: %r\n");
341 threadexitsall("host");
352 hostc = chancreate(sizeof(int), 0);
353 proccreate(hostproc, hostc, 32*1024);
375 a[3].c = mc->resizec;
387 flushimage(display, 1);
389 if(!scrolling && t.qh > t.org+t.f->nchars)
393 sysfatal("impossible");
402 conswrite((char*)rcbuf[i].data, rcbuf[i].n);
414 if(getwindow(display, Refnone) < 0)
415 sysfatal("can't reattach to window");
416 draw(screen, screen->r, cols[BACK], nil, ZP);
429 cols[TEXT] = cols[HTEXT] = display->black;
430 hcols[TEXT] = hcols[HTEXT] = blue;
432 cols[TEXT] = cols[HTEXT] = palegrey;
433 hcols[TEXT] = hcols[HTEXT] = paleblue;
442 scrollr.max.x = r.min.x+Scrollwid;
443 lastsr = Rect(0,0,0,0);
445 r.min.x += Scrollwid+Scrollgap;
448 frinit(t.f, r, font, screen, holdon ? hcols : cols);
449 t.f->maxtab = maxtab*stringwidth(font, "0");
453 p = stringsize(font, "0");
454 if(p.x == 0 || p.y == 0)
457 updatewinsize(Dy(r)/p.y, Dx(r)/p.x, Dx(r), Dy(r));
464 setcursor(mc, &whitearrow);
468 draw(screen, screen->r, cols[BACK], nil, ZP);
474 wordclick(uint *q0, uint *q1)
476 while(*q1<t.nr && !isspace(t.r[*q1]))
478 while(*q0>0 && !isspace(t.r[*q0-1]))
483 aselect(uint *q0, uint *q1, Image *color)
486 uint oldq0, oldq1, newq0, newq1;
488 /* save old selection */
492 /* sweep out area and record it */
493 t.f->cols[HIGH] = color;
494 t.f->cols[HTEXT] = display->white;
500 if(t.m.buttons != 0){
508 /* restore old selection */
509 t.f->cols[HIGH] = cols[HIGH];
510 t.f->cols[HTEXT] = cols[HTEXT];
518 /* selected a region */
525 /* clicked inside previous selection */
526 /* the "<=" in newq0 <= oldq1 allows us to click the right edge */
527 if(oldq0 <= newq0 && newq0 <= oldq1){
539 static Rune Lnl[1] = { '\n' };
549 if(but != 1 && but != 2 && but != 4 && but != 8 && but != 16)
552 if (ptinrect(t.m.xy, scrollr)) {
554 if(t.qh<=t.org+t.f->nchars)
565 if(aselect(&q0, &q1, execcolor) >= 0){
572 paste(t.r+q0, q1-q0, 1);
573 if(t.r[q1-1] != '\n')
584 if(aselect(&q0, &q1, plumbcolor) >= 0)
589 scrollup(mousescrollsize(t.f->maxlines));
592 scrolldown(mousescrollsize(t.f->maxlines));
604 q0 = frcharofpt(t.f, t.m.xy) + t.org;
605 if(t.m.msec-clickmsec<500 && clickq0==q0 && t.q0==t.q1 && b==1){
606 doubleclick(&t.q0, &t.q1);
608 /* t.t.i->flush(); */
611 /* stay here until something interesting happens */
615 } while(t.m.buttons==b && abs(t.m.xy.x-x)<4 && abs(t.m.xy.y-y)<4);
616 t.m.xy.x = x; /* in case we're calling frselect */
621 if(t.m.buttons == b) {
624 t.q0 = t.f->p0 + t.org;
625 t.q1 = t.f->p1 + t.org;
626 clickmsec = t.m.msec;
629 if((t.m.buttons != b) &&(b&1)){
630 enum{Cancut = 1, Canpaste = 2} state = Cancut | Canpaste;
638 }else if(t.m.buttons&4){
642 paste(t.snarf, t.nsnarf, 0);
653 Rune newline[] = { '\n', 0 };
659 menu2str[Scroll] = "+ scroll";
661 menu2str[Scroll] = "- scroll";
663 menu2str[Cooked] = "+ mustecho";
665 menu2str[Cooked] = "- mustecho";
667 switch(menuhit(but, mc, &menu2, nil)){
678 paste(t.snarf, t.nsnarf, 0);
694 paste(t.snarf, t.nsnarf, 1);
695 if(t.nsnarf == 0 || t.snarf[t.nsnarf-1] != '\n')
696 paste(newline, 1, 1);
701 scrolling = !scrolling;
714 sysfatal("bad menu item");
719 windfilewidth(uint q0, int oneelement)
729 if(oneelement && r=='/')
737 showcandidates(Completion *c)
747 s = "[no matches in ";
751 fmtprint(&f, "%s%d files]\n", s, c->nfile);
753 fmtprint(&f, "%s", s);
754 for(i=0; i<c->nfile; i++){
757 fmtprint(&f, "%s", c->filename[i]);
761 /* place text at beginning of line before host point */
763 while(qline>0 && t.r[qline-1] != '\n')
766 rp = runefmtstrflush(&f);
770 q0 += insert(rp, nr, qline, 0) - qline;
781 Rune *rp, *path, *str;
783 char *s, *dir, *root;
785 /* control-f: filename completion; works back to white space or / */
786 if(t.q0<t.nr && t.r[t.q0]>' ') /* must be at end of word */
788 nstr = windfilewidth(t.q0, TRUE);
789 str = runemalloc(nstr);
790 runemove(str, t.r+(t.q0-nstr), nstr);
791 npath = windfilewidth(t.q0-nstr, FALSE);
792 path = runemalloc(npath);
793 runemove(path, t.r+(t.q0-nstr-npath), npath);
796 /* is path rooted? if not, we need to make it relative to window path */
797 if(npath>0 && path[0]=='/'){
798 dir = malloc(UTFmax*npath+1);
799 sprint(dir, "%.*S", npath, path);
801 if(strcmp(wdir, "") == 0)
805 dir = malloc(strlen(root)+1+UTFmax*npath+1);
806 sprint(dir, "%s/%.*S", root, npath, path);
808 dir = cleanname(dir);
810 s = smprint("%.*S", nstr, str);
811 c = complete(dir, s);
820 rp = runesmprint("%s", c->string);
833 setorigin(backnl(t.org, n), 1);
839 setorigin(line2q(n), 1);
840 if(t.qh<=t.org+t.f->nchars)
854 scrollup(t.f->maxlines*2/3);
857 scrolldown(t.f->maxlines*2/3);
860 scrollup(t.f->maxlines/3);
863 scrolldown(t.f->maxlines/3);
890 * Non-standard extensions.
905 paste(t.snarf, t.nsnarf, 0);
912 /* case 0x03: can't do this because ^C is COPY */
913 case 0x7F: /* DEL: send interrupt */
915 t.qh = t.q0 = t.q1 = t.nr;
917 /* must write the interrupt character in case app is in raw mode (e.g., ssh) */
918 write(rcfd, "\x7F", 1);
919 // postnote(PNGROUP, rcpid, "interrupt");
923 if(rawon() && t.q0==t.nr){
929 if(r==ESC || (holdon && r==0x7F)){ /* toggle hold */
932 // replaceintegerproperty("_9WM_HOLD_MODE", 1, 32, holdon);
942 case 0x06: /* ^F: file name completion */
943 case Kins: /* Insert: file name completion */
951 case 0x08: /* ^H: erase character */
952 case 0x15: /* ^U: erase line */
953 case 0x17: /* ^W: erase word */
954 if (t.q0 != 0 && t.q0 != t.qh)
955 t.q0 -= bswidth(r, t.q0, 1);
967 bswidth(Rune c, uint start, int eatnl)
973 /* there is known to be at least one character to erase */
974 if(c == 0x08) /* ^H: erase character */
983 if(r == '\n'){ /* eat at most one more character */
984 if(q == start && eatnl) /* eat the newline */
990 if(eq && skipping) /* found one; stop skipping */
992 else if(!eq && !skipping)
1011 /* look to see if there is a complete line */
1012 for(i=t.qh; i<t.nr; i++){
1014 if(c=='\n' || c=='\004' || c=='\x7F')
1035 while(n >= UTFmax && (t.qh<t.nr || t.nraw > 0)) {
1037 width = runetochar(p, &t.raw[0]);
1039 runemove(t.raw, t.raw+1, t.nraw);
1041 width = runetochar(p, &t.r[t.qh++]);
1045 if(!raw && (c == '\n' || c == '\004' || c == '\x7F'))
1051 * If we've been echoing, make sure the terminal isn't
1052 * while we do the write. This screws up if someone
1053 * else tries to turn off echo at the same time we do
1054 * (we'll turn it on again after the write), but that's not
1057 s = setecho(sfd, 0);
1058 if(write(rcfd, buf, n) < 0)
1066 conswrite(char *p, int n)
1069 Rune buf2[1000], *q;
1071 /* convert to runes */
1074 /* handle partial runes */
1075 while(i < UTFmax && n>0) {
1080 if(fullrune(t.part, i)) {
1082 chartorune(buf2, t.part);
1087 /* there is a little extra room in a message buf */
1090 while(n >= UTFmax || fullrune(p, n)) {
1095 if(n < UTFmax && !fullrune(p, n))
1097 i = chartorune(q, p);
1103 runewrite(buf2, q-buf2);
1107 assert(n+t.npart < UTFmax);
1108 memcpy(t.part+t.npart, p, n);
1117 runewrite(Rune *r, int n)
1129 /* get rid of backspaces */
1132 for(i=0; i<n; i++) {
1138 } else if(*p == '\r') { /* treat like ^U */
1139 /* convert CR without NL into erased line */
1140 /* i feel really sleazy about this but it helps */
1141 while(i<n-1 && *(p+1) == '\r'){
1145 if(i<n-1 && *(p+1) != '\n'){
1146 while(q > r && *(q-1) != '\n')
1149 initial = bswidth(0x15, t.qh, 0);
1158 /* write turned into a delete */
1165 runemove(t.r+q0, t.r+q1, t.nr-q1);
1178 else if(q0 < t.org+t.f->nchars){
1186 if(p1 > t.f->nchars)
1188 frdelete(t.f, p0, p1);
1194 insert(r, n, t.qh, 1);
1207 if (q0 < t.org && q1 >= t.org)
1213 runemove(t.r+q0, t.r+q1, t.nr-q1);
1222 else if(q0 < t.org+t.f->nchars){
1223 assert(q0 >= t.org);
1226 if(p1 > t.f->nchars)
1228 frdelete(t.f, p0, p1);
1249 t.snarf = runerealloc(t.snarf, n);
1250 for(i=0,p=t.snarf; i<n; p++)
1251 i += chartorune(p, pp+i);
1252 t.nsnarf = p-t.snarf;
1256 char sbuf[SnarfSize];
1267 t.snarf = runerealloc(t.snarf, n);
1268 for(i=0,p=sbuf,rp=t.snarf; i<n && p < sbuf+SnarfSize-UTFmax; i++){
1269 *rp++ = *(t.r+t.q0+i);
1270 p += runetochar(p, t.r+t.q0+i);
1272 t.nsnarf = rp-t.snarf;
1294 insert(Rune *r, int n, uint q0, int hostwrite)
1300 if(t.nr+n>HiWater && q0>=t.org && q0>=t.qh){
1301 m = min(HiWater-LoWater, min(t.org, t.qh));
1313 runemove(t.r, t.r+m, t.nr);
1316 if(t.nr+n > t.maxr){
1318 * Minimize realloc breakage:
1319 * Allocate at least MinWater
1320 * Double allocation size each time
1321 * But don't go much above HiWater
1323 m = max(min(2*(t.nr+n), HiWater), t.nr+n)+MinWater;
1325 m = max(HiWater+MinWater, t.nr+n);
1327 t.r = runerealloc(t.r, m);
1331 runemove(t.r+q0+n, t.r+q0, t.nr-q0);
1332 runemove(t.r+q0, r, n);
1334 /* if output touches, advance selection, not qh; works best for keyboard and output */
1339 if(q0 < t.qh || (q0==t.qh && hostwrite))
1345 else if(q0 <= t.org+t.f->nchars)
1346 frinsert(t.f, r, r+n, q0-t.org);
1351 paste(Rune *r, int n, int advance)
1355 if(rawon() && t.q0==t.nr){
1365 * if this is a button2 execute then we might have been passed
1366 * runes inside the buffer. must save them before realloc.
1369 if(t.r <= r && r < t.r+n){
1370 rbuf = runemalloc(n);
1371 runemove(rbuf, r, n);
1375 insert(r, n, t.q0, 0);
1383 if (t.f->nlines >= t.f->maxlines)
1385 frinsert(t.f, t.r + t.org + t.f->nchars, t.r + t.nr, t.f->nchars);
1395 if(t.org+f->p0 == t.q0 && t.org+f->p1 == t.q1)
1400 frdrawsel(f, frptofchar(f, f->p0), f->p0, f->p1, 0);
1413 frdrawsel(f, frptofchar(f, f->p0), f->p0, f->p1, 1);
1416 if(t.qh<=t.org+t.f.nchars && t.cwqueue != 0)
1417 t.cwqueue->wakeup <-= 0;
1433 nl = t.f->maxlines/5;
1435 nl = 4*t.f->maxlines/5;
1437 /* avoid going in the wrong direction */
1438 if (q0>t.org && q<t.org)
1441 /* keep trying until q0 is on the screen */
1442 while(!cansee(q0)) {
1443 assert(q0 >= t.org);
1445 q = line2q(t.f->maxlines-nl);
1456 qe = t.org+t.f->nchars;
1458 if(q0>=t.org && q0 < qe)
1462 if (t.f->nlines < t.f->maxlines)
1464 if (q0 > 0 && t.r[t.nr-1] == '\n')
1471 setorigin(uint org, int exact)
1476 if(org>0 && !exact){
1477 /* try and start after a newline */
1478 /* don't try harder than 256 chars */
1479 for(i=0; i<256 && org<t.nr; i++){
1480 if(t.r[org-1] == '\n')
1487 if(a>=0 && a<t.f->nchars)
1488 frdelete(t.f, 0, a);
1489 else if(a<0 && -a<100*t.f->maxlines){
1491 frinsert(t.f, t.r+org, t.r+org+n, 0);
1493 frdelete(t.f, 0, t.f->nchars);
1506 return frcharofpt(f, Pt(f->r.min.x, f->r.min.y + n*font->height))+t.org;
1510 backnl(uint p, uint n)
1515 /* at 256 chars, call it a line anyway */
1516 for(j=256; --j>0 && p>0; p--)
1519 if (p == 0 || i == 0)
1523 return 0; /* alef bug */
1527 addraw(Rune *r, int nr)
1529 t.raw = runerealloc(t.raw, t.nraw+nr);
1530 runemove(t.raw+t.nraw, r, nr);
1533 if(t.crqueue != nil)
1534 t.crqueue->wakeup <-= 0;
1539 Rune left1[] = { '{', '[', '(', '<', 0xab, 0 };
1540 Rune right1[] = { '}', ']', ')', '>', 0xbb, 0 };
1541 Rune left2[] = { '\n', 0 };
1542 Rune left3[] = { '\'', '"', '`', 0 };
1559 doubleclick(uint *q0, uint *q1)
1565 for(i=0; left[i]!=0; i++){
1569 /* try matching character to left, looking right */
1576 if(clickmatch(c, r[p-l], 1, &q))
1580 /* try matching character to right, looking left */
1587 if(clickmatch(c, l[p-r], -1, &q)){
1588 *q1 = *q0+(*q0<t.nr && c=='\n');
1590 if(c!='\n' || q!=0 || t.r[0]=='\n')
1596 /* try filling out word to right */
1597 while(*q1<t.nr && isalnum(t.r[*q1]))
1599 /* try filling out word to left */
1600 while(*q0>0 && isalnum(t.r[*q0-1]))
1605 clickmatch(int cl, int cr, int dir, uint *q)
1629 return cl=='\n' && nest==1;
1639 assert(t.q0 <= t.q1 && t.q1 <= t.nr);
1640 assert(t.org <= t.nr && t.qh <= t.nr);
1641 assert(f->p0 <= f->p1 && f->p1 <= f->nchars);
1642 assert(t.org + f->nchars <= t.nr);
1643 assert(t.org+f->nchars==t.nr || (f->nlines >= f->maxlines));
1647 strrune(Rune *s, Rune c)
1666 Rectangle r, r1, r2;
1670 r.min.x += 1; /* border between margin and bar */
1672 if(scrx==0 || scrx->r.max.y < r.max.y){
1675 scrx = allocimage(display, Rect(0, 0, 32, r.max.y), screen->chan, 1, DPaleyellow);
1677 sysfatal("scroll balloc");
1681 r2 = scrpos(r1, t.org, t.org+t.f->nchars, t.nr);
1682 if(!eqrect(r2, lastsr)){
1684 draw(scrx, r1, cols[BORD], nil, ZP);
1685 draw(scrx, r2, cols[BACK], nil, r2.min);
1687 // r2.min.x = r2.max.x-1;
1688 // draw(scrx, r2, cols[BORD], nil, ZP);
1689 draw(screen, r, scrx, nil, r1.min);
1694 scrpos(Rectangle r, ulong p0, ulong p1, ulong tot)
1699 q = insetrect(r, 1);
1700 h = q.max.y-q.min.y;
1703 if(tot > 1024L*1024L)
1704 tot >>= 10, p0 >>= 10, p1 >>= 10;
1706 q.min.y += h*p0/tot;
1708 q.max.y -= h*(tot-p1)/tot;
1709 if(q.max.y < q.min.y+2){
1710 if(q.min.y+2 <= r.max.y)
1711 q.max.y = q.min.y+2;
1713 q.min.y = q.max.y-2;
1723 int x, y, my, h, first, exact;
1725 s = insetrect(scrollr, 1);
1726 h = s.max.y-s.min.y;
1727 x = (s.min.x+s.max.x)/2;
1731 if(t.m.xy.x<s.min.x || s.max.x<=t.m.xy.x){
1740 // if(!eqpt(t.m.xy, Pt(x, my)))
1741 // cursorset(Pt(x, my));
1747 if(t.nr > 1024*1024)
1748 p0 = ((t.nr>>10)*(y-s.min.y)/h)<<10;
1750 p0 = t.nr*(y-s.min.y)/h;
1753 p0 = backnl(t.org, (my-s.min.y)/font->height);
1755 p0 = t.org+frcharofpt(t.f, Pt(s.max.x, my));
1758 setorigin(p0, exact);
1764 }while(t.m.buttons & (1<<(but-1)));
1770 if((plumbfd = plumbopen("send", OWRITE)) < 0)
1771 fprint(2, "9term: plumbopen: %r\n");
1775 plumb(uint q0, uint q1)
1782 pm = malloc(sizeof(Plumbmsg));
1783 pm->src = strdup("9term");
1785 pm->wdir = strdup(wdir);
1786 pm->type = strdup("text");
1792 wordclick(&q0, &q1);
1793 sprint(cbuf, "click=%d", p0-q0);
1794 pm->attr = plumbunpackattr(cbuf);
1800 pm->data = malloc(SnarfSize);
1802 for(i=0,p=pm->data; i<n && p < pm->data + SnarfSize-UTFmax; i++)
1803 p += runetochar(p, t.r+q0+i);
1805 pm->ndata = strlen(pm->data);
1806 if(plumbsend(plumbfd, pm) < 0){
1807 setcursor(mc, &query);
1810 setcursor(mc, &whitearrow);
1818 * Process in-band messages about window title changes.
1819 * The messages are of the form:
1823 * where xxx is the new directory. This format was chosen
1824 * because it changes the label on xterm windows.
1827 label(Rune *sr, int n)
1829 Rune *sl, *el, *er, *r;
1832 for(r=er-1; r>=sr; r--)
1839 if(el-sr > sizeof wdir)
1840 sr = el - sizeof wdir;
1841 for(sl=el-3; sl>=sr; sl--)
1842 if(sl[0]=='\033' && sl[1]==']' && sl[2]==';')
1847 snprint(wdir, sizeof wdir, "%.*S", (el-1)-(sl+3), sl+3);
1850 runemove(sl, el, er-el);
1858 return !cooked && !isecho(sfd);
1862 * Clumsy hack to make " and "" work.
1863 * Then again, what's not a clumsy hack here in Unix land?
1870 removethesocket(void)
1873 if(remove(thesocket) < 0)
1874 fprint(2, "remove %s: %r\n", thesocket);
1882 snprint(buf, sizeof buf, "unix!/tmp/9term-text.%d", getpid());
1884 if((afd = announce(buf, adir)) < 0){
1885 putenv("text9term", "");
1889 putenv("text9term", buf);
1890 proccreate(listenproc, nil, STACK);
1891 strcpy(thesocket, buf+5);
1892 atexit(removethesocket);
1896 listenproc(void *arg)
1903 fd = listen(adir, dir);
1908 proccreate(textthread, (void*)fd, STACK);
1913 textthread(void *arg)
1915 int fd, i, x, n, end;
1917 char buf[4096], *p, *ep;
1921 ep = buf+sizeof buf;
1922 end = t.org+t.nr; /* avoid possible output loop */
1924 if(i >= end || ep-p < UTFmax){
1925 for(x=0; x<p-buf; x+=n)
1926 if((n = write(fd, buf+x, (p-x)-buf)) <= 0)
1939 p += runetochar(p, &r);