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, }
202 fprint(2, "usage: 9term [-ars] [-W winsize] [cmd ...]\n");
203 threadexitsall("usage");
207 threadmain(int argc, char *argv[])
214 _wantfocuschanges = 1;
219 case 'a': /* acme mode */
223 font = EARGF(usage());
228 case 'w': /* started from "rio" window manager */
232 winsize = EARGF(usage());
237 putenv("font", font);
239 p = getenv("tabstop");
241 p = getenv("TABSTOP");
242 if(p != 0 && maxtab <= 0)
243 maxtab = strtoul(p, 0, 0);
245 maxtab = 4; /* be like rio */
247 snprint(buf, sizeof buf, "%d", maxtab);
248 putenv("tabstop", buf);
250 initdraw(0, nil, "9term");
252 noteenable("sys: child");
255 mc = initmouse(nil, screen);
256 kc = initkeyboard(nil);
257 rcpid = rcstart(argc, argv, &rcfd, &sfd);
261 t.f = mallocz(sizeof(Frame), 1);
264 cols[BACK] = allocimagemix(display, DPaleyellow, DWhite);
265 cols[HIGH] = allocimage(display, Rect(0, 0, 1, 1), screen->chan, 1, DDarkyellow);
266 cols[BORD] = allocimage(display, Rect(0, 0, 1, 1), screen->chan, 1, DYellowgreen);
268 cols[BACK] = allocimage(display, Rect(0, 0, 1, 1), screen->chan, 1, DWhite);
269 cols[HIGH] = allocimage(display, Rect(0, 0, 1, 1), screen->chan, 1, 0xCCCCCCFF);
270 cols[BORD] = allocimage(display, Rect(0, 0, 1, 1), screen->chan, 1, 0x999999FF);
272 cols[TEXT] = display->black;
273 cols[HTEXT] = display->black;
274 palegrey = allocimage(display, Rect(0, 0, 1, 1), screen->chan, 1, 0x666666FF);
276 hcols[BACK] = cols[BACK];
277 hcols[HIGH] = cols[HIGH];
278 blue = allocimage(display, Rect(0, 0, 1, 1), screen->chan, 1, DMedblue);
279 paleblue = allocimage(display, Rect(0, 0, 1, 1), screen->chan, 1, DGreyblue);
282 hcols[TEXT] = hcols[BORD];
283 hcols[HTEXT] = hcols[TEXT];
285 plumbcolor = allocimage(display, Rect(0,0,1,1), screen->chan, 1, 0x006600FF);
286 execcolor = allocimage(display, Rect(0,0,1,1), screen->chan, 1, 0xAA0000FF);
288 if(!blue || !palegrey || !paleblue || !plumbcolor || !execcolor)
289 sysfatal("alloc colors: %r");
290 draw(screen, screen->r, cols[BACK], nil, ZP);
296 hangupnote(void *a, char *msg)
298 if(getpid() != mainpid)
300 if(strcmp(msg, "hangup") == 0 && rcpid != 0){
301 postnote(PNGROUP, rcpid, "hangup");
304 if(strstr(msg, "child")){
321 /* Let typing have a go -- maybe there's a rubout waiting. */
324 i = 1-i; /* toggle */
325 n = read(rcfd, rcbuf[i].data, sizeof rcbuf[i].data);
328 fprint(2, "9term: host read error: %r\n");
329 threadexitsall("host");
340 hostc = chancreate(sizeof(int), 0);
341 proccreate(hostproc, hostc, 32*1024);
363 a[3].c = mc->resizec;
375 flushimage(display, 1);
377 if(!scrolling && t.qh > t.org+t.f->nchars)
381 sysfatal("impossible");
390 conswrite((char*)rcbuf[i].data, rcbuf[i].n);
402 if(getwindow(display, Refnone) < 0)
403 sysfatal("can't reattach to window");
404 draw(screen, screen->r, cols[BACK], nil, ZP);
417 cols[TEXT] = cols[HTEXT] = display->black;
418 hcols[TEXT] = hcols[HTEXT] = blue;
420 cols[TEXT] = cols[HTEXT] = palegrey;
421 hcols[TEXT] = hcols[HTEXT] = paleblue;
430 scrollr.max.x = r.min.x+Scrollwid;
431 lastsr = Rect(0,0,0,0);
433 r.min.x += Scrollwid+Scrollgap;
436 frinit(t.f, r, font, screen, holdon ? hcols : cols);
437 t.f->maxtab = maxtab*stringwidth(font, "0");
441 p = stringsize(font, "0");
442 if(p.x == 0 || p.y == 0)
445 updatewinsize(Dy(r)/p.y, Dx(r)/p.x, Dx(r), Dy(r));
452 setcursor(mc, &whitearrow);
456 draw(screen, screen->r, cols[BACK], nil, ZP);
462 wordclick(uint *q0, uint *q1)
464 while(*q1<t.nr && !isspace(t.r[*q1]))
466 while(*q0>0 && !isspace(t.r[*q0-1]))
471 aselect(uint *q0, uint *q1, Image *color)
474 uint oldq0, oldq1, newq0, newq1;
476 /* save old selection */
480 /* sweep out area and record it */
481 t.f->cols[HIGH] = color;
482 t.f->cols[HTEXT] = display->white;
488 if(t.m.buttons != 0){
496 /* restore old selection */
497 t.f->cols[HIGH] = cols[HIGH];
498 t.f->cols[HTEXT] = cols[HTEXT];
506 /* selected a region */
513 /* clicked inside previous selection */
514 /* the "<=" in newq0 <= oldq1 allows us to click the right edge */
515 if(oldq0 <= newq0 && newq0 <= oldq1){
527 static Rune Lnl[1] = { '\n' };
537 if(but != 1 && but != 2 && but != 4 && but != 8 && but != 16)
540 if (ptinrect(t.m.xy, scrollr)) {
542 if(t.qh<=t.org+t.f->nchars)
553 if(aselect(&q0, &q1, execcolor) >= 0){
560 paste(t.r+q0, q1-q0, 1);
561 if(t.r[q1-1] != '\n')
572 if(aselect(&q0, &q1, plumbcolor) >= 0)
577 scrollup(mousescrollsize(t.f->maxlines));
580 scrolldown(mousescrollsize(t.f->maxlines));
592 q0 = frcharofpt(t.f, t.m.xy) + t.org;
593 if(t.m.msec-clickmsec<500 && clickq0==q0 && t.q0==t.q1 && b==1){
594 doubleclick(&t.q0, &t.q1);
596 /* t.t.i->flush(); */
599 /* stay here until something interesting happens */
603 } while(t.m.buttons==b && abs(t.m.xy.x-x)<4 && abs(t.m.xy.y-y)<4);
604 t.m.xy.x = x; /* in case we're calling frselect */
609 if(t.m.buttons == b) {
612 t.q0 = t.f->p0 + t.org;
613 t.q1 = t.f->p1 + t.org;
614 clickmsec = t.m.msec;
617 if((t.m.buttons != b) &&(b&1)){
618 enum{Cancut = 1, Canpaste = 2} state = Cancut | Canpaste;
626 }else if(t.m.buttons&4){
630 paste(t.snarf, t.nsnarf, 0);
641 Rune newline[] = { '\n', 0 };
647 menu2str[Scroll] = "+ scroll";
649 menu2str[Scroll] = "- scroll";
651 menu2str[Cooked] = "+ mustecho";
653 menu2str[Cooked] = "- mustecho";
655 switch(menuhit(but, mc, &menu2, nil)){
666 paste(t.snarf, t.nsnarf, 0);
682 paste(t.snarf, t.nsnarf, 1);
683 if(t.nsnarf == 0 || t.snarf[t.nsnarf-1] != '\n')
684 paste(newline, 1, 1);
689 scrolling = !scrolling;
702 sysfatal("bad menu item");
707 windfilewidth(uint q0, int oneelement)
717 if(oneelement && r=='/')
725 showcandidates(Completion *c)
735 s = "[no matches in ";
739 fmtprint(&f, "%s%d files]\n", s, c->nfile);
741 fmtprint(&f, "%s", s);
742 for(i=0; i<c->nfile; i++){
745 fmtprint(&f, "%s", c->filename[i]);
749 /* place text at beginning of line before host point */
751 while(qline>0 && t.r[qline-1] != '\n')
754 rp = runefmtstrflush(&f);
758 q0 += insert(rp, nr, qline, 0) - qline;
769 Rune *rp, *path, *str;
771 char *s, *dir, *root;
773 /* control-f: filename completion; works back to white space or / */
774 if(t.q0<t.nr && t.r[t.q0]>' ') /* must be at end of word */
776 nstr = windfilewidth(t.q0, TRUE);
777 str = runemalloc(nstr);
778 runemove(str, t.r+(t.q0-nstr), nstr);
779 npath = windfilewidth(t.q0-nstr, FALSE);
780 path = runemalloc(npath);
781 runemove(path, t.r+(t.q0-nstr-npath), npath);
784 /* is path rooted? if not, we need to make it relative to window path */
785 if(npath>0 && path[0]=='/'){
786 dir = malloc(UTFmax*npath+1);
787 sprint(dir, "%.*S", npath, path);
789 if(strcmp(wdir, "") == 0)
793 dir = malloc(strlen(root)+1+UTFmax*npath+1);
794 sprint(dir, "%s/%.*S", root, npath, path);
796 dir = cleanname(dir);
798 s = smprint("%.*S", nstr, str);
799 c = complete(dir, s);
808 rp = runesmprint("%s", c->string);
821 setorigin(backnl(t.org, n), 1);
827 setorigin(line2q(n), 1);
828 if(t.qh<=t.org+t.f->nchars)
842 scrollup(t.f->maxlines*2/3);
845 scrolldown(t.f->maxlines*2/3);
848 scrollup(t.f->maxlines/3);
851 scrolldown(t.f->maxlines/3);
878 * Non-standard extensions.
893 paste(t.snarf, t.nsnarf, 0);
900 /* case 0x03: can't do this because ^C is COPY */
901 case 0x7F: /* DEL: send interrupt */
903 t.qh = t.q0 = t.q1 = t.nr;
905 /* must write the interrupt character in case app is in raw mode (e.g., ssh) */
906 write(rcfd, "\x7F", 1);
907 // postnote(PNGROUP, rcpid, "interrupt");
911 if(rawon() && t.q0==t.nr){
917 if(r==ESC || (holdon && r==0x7F)){ /* toggle hold */
920 // replaceintegerproperty("_9WM_HOLD_MODE", 1, 32, holdon);
930 case 0x06: /* ^F: file name completion */
931 case Kins: /* Insert: file name completion */
939 case 0x08: /* ^H: erase character */
940 case 0x15: /* ^U: erase line */
941 case 0x17: /* ^W: erase word */
942 if (t.q0 != 0 && t.q0 != t.qh)
943 t.q0 -= bswidth(r, t.q0, 1);
955 bswidth(Rune c, uint start, int eatnl)
961 /* there is known to be at least one character to erase */
962 if(c == 0x08) /* ^H: erase character */
971 if(r == '\n'){ /* eat at most one more character */
972 if(q == start && eatnl) /* eat the newline */
978 if(eq && skipping) /* found one; stop skipping */
980 else if(!eq && !skipping)
999 /* look to see if there is a complete line */
1000 for(i=t.qh; i<t.nr; i++){
1002 if(c=='\n' || c=='\004' || c=='\x7F')
1023 while(n >= UTFmax && (t.qh<t.nr || t.nraw > 0)) {
1025 width = runetochar(p, &t.raw[0]);
1027 runemove(t.raw, t.raw+1, t.nraw);
1029 width = runetochar(p, &t.r[t.qh++]);
1033 if(!raw && (c == '\n' || c == '\004' || c == '\x7F'))
1039 * If we've been echoing, make sure the terminal isn't
1040 * while we do the write. This screws up if someone
1041 * else tries to turn off echo at the same time we do
1042 * (we'll turn it on again after the write), but that's not
1045 s = setecho(sfd, 0);
1046 if(write(rcfd, buf, n) < 0)
1054 conswrite(char *p, int n)
1057 Rune buf2[1000], *q;
1059 /* convert to runes */
1062 /* handle partial runes */
1063 while(i < UTFmax && n>0) {
1068 if(fullrune(t.part, i)) {
1070 chartorune(buf2, t.part);
1075 /* there is a little extra room in a message buf */
1078 while(n >= UTFmax || fullrune(p, n)) {
1083 if(n < UTFmax && !fullrune(p, n))
1085 i = chartorune(q, p);
1091 runewrite(buf2, q-buf2);
1095 assert(n+t.npart < UTFmax);
1096 memcpy(t.part+t.npart, p, n);
1105 runewrite(Rune *r, int n)
1117 /* get rid of backspaces */
1120 for(i=0; i<n; i++) {
1126 } else if(*p == '\r') { /* treat like ^U */
1127 /* convert CR without NL into erased line */
1128 /* i feel really sleazy about this but it helps */
1129 while(i<n-1 && *(p+1) == '\r'){
1133 if(i<n-1 && *(p+1) != '\n'){
1134 while(q > r && *(q-1) != '\n')
1137 initial = bswidth(0x15, t.qh, 0);
1146 /* write turned into a delete */
1153 runemove(t.r+q0, t.r+q1, t.nr-q1);
1166 else if(q0 < t.org+t.f->nchars){
1174 if(p1 > t.f->nchars)
1176 frdelete(t.f, p0, p1);
1182 insert(r, n, t.qh, 1);
1195 if (q0 < t.org && q1 >= t.org)
1201 runemove(t.r+q0, t.r+q1, t.nr-q1);
1210 else if(q0 < t.org+t.f->nchars){
1211 assert(q0 >= t.org);
1214 if(p1 > t.f->nchars)
1216 frdelete(t.f, p0, p1);
1237 t.snarf = runerealloc(t.snarf, n);
1238 for(i=0,p=t.snarf; i<n; p++)
1239 i += chartorune(p, pp+i);
1240 t.nsnarf = p-t.snarf;
1244 char sbuf[SnarfSize];
1255 t.snarf = runerealloc(t.snarf, n);
1256 for(i=0,p=sbuf,rp=t.snarf; i<n && p < sbuf+SnarfSize-UTFmax; i++){
1257 *rp++ = *(t.r+t.q0+i);
1258 p += runetochar(p, t.r+t.q0+i);
1260 t.nsnarf = rp-t.snarf;
1282 insert(Rune *r, int n, uint q0, int hostwrite)
1288 if(t.nr+n>HiWater && q0>=t.org && q0>=t.qh){
1289 m = min(HiWater-LoWater, min(t.org, t.qh));
1301 runemove(t.r, t.r+m, t.nr);
1304 if(t.nr+n > t.maxr){
1306 * Minimize realloc breakage:
1307 * Allocate at least MinWater
1308 * Double allocation size each time
1309 * But don't go much above HiWater
1311 m = max(min(2*(t.nr+n), HiWater), t.nr+n)+MinWater;
1313 m = max(HiWater+MinWater, t.nr+n);
1315 t.r = runerealloc(t.r, m);
1319 runemove(t.r+q0+n, t.r+q0, t.nr-q0);
1320 runemove(t.r+q0, r, n);
1322 /* if output touches, advance selection, not qh; works best for keyboard and output */
1327 if(q0 < t.qh || (q0==t.qh && hostwrite))
1333 else if(q0 <= t.org+t.f->nchars)
1334 frinsert(t.f, r, r+n, q0-t.org);
1339 paste(Rune *r, int n, int advance)
1343 if(rawon() && t.q0==t.nr){
1353 * if this is a button2 execute then we might have been passed
1354 * runes inside the buffer. must save them before realloc.
1357 if(t.r <= r && r < t.r+n){
1358 rbuf = runemalloc(n);
1359 runemove(rbuf, r, n);
1363 insert(r, n, t.q0, 0);
1371 if (t.f->nlines >= t.f->maxlines)
1373 frinsert(t.f, t.r + t.org + t.f->nchars, t.r + t.nr, t.f->nchars);
1383 if(t.org+f->p0 == t.q0 && t.org+f->p1 == t.q1)
1388 frdrawsel(f, frptofchar(f, f->p0), f->p0, f->p1, 0);
1401 frdrawsel(f, frptofchar(f, f->p0), f->p0, f->p1, 1);
1404 if(t.qh<=t.org+t.f.nchars && t.cwqueue != 0)
1405 t.cwqueue->wakeup <-= 0;
1421 nl = t.f->maxlines/5;
1423 nl = 4*t.f->maxlines/5;
1425 /* avoid going in the wrong direction */
1426 if (q0>t.org && q<t.org)
1429 /* keep trying until q0 is on the screen */
1430 while(!cansee(q0)) {
1431 assert(q0 >= t.org);
1433 q = line2q(t.f->maxlines-nl);
1444 qe = t.org+t.f->nchars;
1446 if(q0>=t.org && q0 < qe)
1450 if (t.f->nlines < t.f->maxlines)
1452 if (q0 > 0 && t.r[t.nr-1] == '\n')
1459 setorigin(uint org, int exact)
1464 if(org>0 && !exact){
1465 /* try and start after a newline */
1466 /* don't try harder than 256 chars */
1467 for(i=0; i<256 && org<t.nr; i++){
1468 if(t.r[org-1] == '\n')
1475 if(a>=0 && a<t.f->nchars)
1476 frdelete(t.f, 0, a);
1477 else if(a<0 && -a<100*t.f->maxlines){
1479 frinsert(t.f, t.r+org, t.r+org+n, 0);
1481 frdelete(t.f, 0, t.f->nchars);
1494 return frcharofpt(f, Pt(f->r.min.x, f->r.min.y + n*font->height))+t.org;
1498 backnl(uint p, uint n)
1503 /* at 256 chars, call it a line anyway */
1504 for(j=256; --j>0 && p>0; p--)
1507 if (p == 0 || i == 0)
1511 return 0; /* alef bug */
1515 addraw(Rune *r, int nr)
1517 t.raw = runerealloc(t.raw, t.nraw+nr);
1518 runemove(t.raw+t.nraw, r, nr);
1521 if(t.crqueue != nil)
1522 t.crqueue->wakeup <-= 0;
1527 Rune left1[] = { '{', '[', '(', '<', 0xab, 0 };
1528 Rune right1[] = { '}', ']', ')', '>', 0xbb, 0 };
1529 Rune left2[] = { '\n', 0 };
1530 Rune left3[] = { '\'', '"', '`', 0 };
1547 doubleclick(uint *q0, uint *q1)
1553 for(i=0; left[i]!=0; i++){
1557 /* try matching character to left, looking right */
1564 if(clickmatch(c, r[p-l], 1, &q))
1568 /* try matching character to right, looking left */
1575 if(clickmatch(c, l[p-r], -1, &q)){
1576 *q1 = *q0+(*q0<t.nr && c=='\n');
1578 if(c!='\n' || q!=0 || t.r[0]=='\n')
1584 /* try filling out word to right */
1585 while(*q1<t.nr && isalnum(t.r[*q1]))
1587 /* try filling out word to left */
1588 while(*q0>0 && isalnum(t.r[*q0-1]))
1593 clickmatch(int cl, int cr, int dir, uint *q)
1617 return cl=='\n' && nest==1;
1627 assert(t.q0 <= t.q1 && t.q1 <= t.nr);
1628 assert(t.org <= t.nr && t.qh <= t.nr);
1629 assert(f->p0 <= f->p1 && f->p1 <= f->nchars);
1630 assert(t.org + f->nchars <= t.nr);
1631 assert(t.org+f->nchars==t.nr || (f->nlines >= f->maxlines));
1635 strrune(Rune *s, Rune c)
1654 Rectangle r, r1, r2;
1658 r.min.x += 1; /* border between margin and bar */
1660 if(scrx==0 || scrx->r.max.y < r.max.y){
1663 scrx = allocimage(display, Rect(0, 0, 32, r.max.y), screen->chan, 1, DPaleyellow);
1665 sysfatal("scroll balloc");
1669 r2 = scrpos(r1, t.org, t.org+t.f->nchars, t.nr);
1670 if(!eqrect(r2, lastsr)){
1672 draw(scrx, r1, cols[BORD], nil, ZP);
1673 draw(scrx, r2, cols[BACK], nil, r2.min);
1675 // r2.min.x = r2.max.x-1;
1676 // draw(scrx, r2, cols[BORD], nil, ZP);
1677 draw(screen, r, scrx, nil, r1.min);
1682 scrpos(Rectangle r, ulong p0, ulong p1, ulong tot)
1687 q = insetrect(r, 1);
1688 h = q.max.y-q.min.y;
1691 if(tot > 1024L*1024L)
1692 tot >>= 10, p0 >>= 10, p1 >>= 10;
1694 q.min.y += h*p0/tot;
1696 q.max.y -= h*(tot-p1)/tot;
1697 if(q.max.y < q.min.y+2){
1698 if(q.min.y+2 <= r.max.y)
1699 q.max.y = q.min.y+2;
1701 q.min.y = q.max.y-2;
1711 int x, y, my, h, first, exact;
1713 s = insetrect(scrollr, 1);
1714 h = s.max.y-s.min.y;
1715 x = (s.min.x+s.max.x)/2;
1719 if(t.m.xy.x<s.min.x || s.max.x<=t.m.xy.x){
1728 // if(!eqpt(t.m.xy, Pt(x, my)))
1729 // cursorset(Pt(x, my));
1735 if(t.nr > 1024*1024)
1736 p0 = ((t.nr>>10)*(y-s.min.y)/h)<<10;
1738 p0 = t.nr*(y-s.min.y)/h;
1741 p0 = backnl(t.org, (my-s.min.y)/font->height);
1743 p0 = t.org+frcharofpt(t.f, Pt(s.max.x, my));
1746 setorigin(p0, exact);
1752 }while(t.m.buttons & (1<<(but-1)));
1758 if((plumbfd = plumbopen("send", OWRITE)) < 0)
1759 fprint(2, "9term: plumbopen: %r\n");
1763 plumb(uint q0, uint q1)
1770 pm = malloc(sizeof(Plumbmsg));
1771 pm->src = strdup("9term");
1773 pm->wdir = strdup(wdir);
1774 pm->type = strdup("text");
1780 wordclick(&q0, &q1);
1781 sprint(cbuf, "click=%d", p0-q0);
1782 pm->attr = plumbunpackattr(cbuf);
1788 pm->data = malloc(SnarfSize);
1790 for(i=0,p=pm->data; i<n && p < pm->data + SnarfSize-UTFmax; i++)
1791 p += runetochar(p, t.r+q0+i);
1793 pm->ndata = strlen(pm->data);
1794 plumbsend(plumbfd, pm);
1799 * Process in-band messages about window title changes.
1800 * The messages are of the form:
1804 * where xxx is the new directory. This format was chosen
1805 * because it changes the label on xterm windows.
1808 label(Rune *sr, int n)
1810 Rune *sl, *el, *er, *r;
1813 for(r=er-1; r>=sr; r--)
1820 if(el-sr > sizeof wdir)
1821 sr = el - sizeof wdir;
1822 for(sl=el-3; sl>=sr; sl--)
1823 if(sl[0]=='\033' && sl[1]==']' && sl[2]==';')
1828 snprint(wdir, sizeof wdir, "%.*S", (el-1)-(sl+3), sl+3);
1829 drawsetlabel(display, wdir);
1831 runemove(sl, el, er-el);
1839 return !cooked && !isecho(sfd);
1843 * Clumsy hack to make " and "" work.
1844 * Then again, what's not a clumsy hack here in Unix land?
1851 removethesocket(void)
1854 if(remove(thesocket) < 0)
1855 fprint(2, "remove %s: %r\n", thesocket);
1863 snprint(buf, sizeof buf, "unix!/tmp/9term-text.%d", getpid());
1865 if((afd = announce(buf, adir)) < 0){
1866 putenv("text9term", "");
1870 putenv("text9term", buf);
1871 proccreate(listenproc, nil, STACK);
1872 strcpy(thesocket, buf+5);
1873 atexit(removethesocket);
1877 listenproc(void *arg)
1884 fd = listen(adir, dir);
1889 proccreate(textthread, (void*)fd, STACK);
1894 textthread(void *arg)
1896 int fd, i, x, n, end;
1898 char buf[4096], *p, *ep;
1902 ep = buf+sizeof buf;
1903 end = t.org+t.nr; /* avoid possible output loop */
1905 if(i >= end || ep-p < UTFmax){
1906 for(x=0; x<p-buf; x+=n)
1907 if((n = write(fd, buf+x, (p-x)-buf)) <= 0)
1920 p += runetochar(p, &r);