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")){
320 n = awaitnohang(buf, sizeof buf-1);
323 if(atoi(buf) == rcpid)
341 /* Let typing have a go -- maybe there's a rubout waiting. */
344 i = 1-i; /* toggle */
345 n = read(rcfd, rcbuf[i].data, sizeof rcbuf[i].data);
348 fprint(2, "9term: host read error: %r\n");
349 threadexitsall("host");
360 hostc = chancreate(sizeof(int), 0);
361 proccreate(hostproc, hostc, 32*1024);
383 a[3].c = mc->resizec;
395 flushimage(display, 1);
397 if(!scrolling && t.qh > t.org+t.f->nchars)
401 sysfatal("impossible");
410 conswrite((char*)rcbuf[i].data, rcbuf[i].n);
422 if(getwindow(display, Refnone) < 0)
423 sysfatal("can't reattach to window");
424 draw(screen, screen->r, cols[BACK], nil, ZP);
437 cols[TEXT] = cols[HTEXT] = display->black;
438 hcols[TEXT] = hcols[HTEXT] = blue;
440 cols[TEXT] = cols[HTEXT] = palegrey;
441 hcols[TEXT] = hcols[HTEXT] = paleblue;
450 scrollr.max.x = r.min.x+Scrollwid;
451 lastsr = Rect(0,0,0,0);
453 r.min.x += Scrollwid+Scrollgap;
456 frinit(t.f, r, font, screen, holdon ? hcols : cols);
457 t.f->maxtab = maxtab*stringwidth(font, "0");
461 p = stringsize(font, "0");
462 if(p.x == 0 || p.y == 0)
465 updatewinsize(Dy(r)/p.y, Dx(r)/p.x, Dx(r), Dy(r));
472 setcursor(mc, &whitearrow);
476 draw(screen, screen->r, cols[BACK], nil, ZP);
482 wordclick(uint *q0, uint *q1)
484 while(*q1<t.nr && !isspace(t.r[*q1]))
486 while(*q0>0 && !isspace(t.r[*q0-1]))
491 aselect(uint *q0, uint *q1, Image *color)
494 uint oldq0, oldq1, newq0, newq1;
496 /* save old selection */
500 /* sweep out area and record it */
501 t.f->cols[HIGH] = color;
502 t.f->cols[HTEXT] = display->white;
508 if(t.m.buttons != 0){
516 /* restore old selection */
517 t.f->cols[HIGH] = cols[HIGH];
518 t.f->cols[HTEXT] = cols[HTEXT];
526 /* selected a region */
533 /* clicked inside previous selection */
534 /* the "<=" in newq0 <= oldq1 allows us to click the right edge */
535 if(oldq0 <= newq0 && newq0 <= oldq1){
547 static Rune Lnl[1] = { '\n' };
557 if(but != 1 && but != 2 && but != 4 && but != 8 && but != 16)
560 if (ptinrect(t.m.xy, scrollr)) {
562 if(t.qh<=t.org+t.f->nchars)
573 if(aselect(&q0, &q1, execcolor) >= 0){
580 paste(t.r+q0, q1-q0, 1);
581 if(t.r[q1-1] != '\n')
592 if(aselect(&q0, &q1, plumbcolor) >= 0)
597 scrollup(mousescrollsize(t.f->maxlines));
600 scrolldown(mousescrollsize(t.f->maxlines));
612 q0 = frcharofpt(t.f, t.m.xy) + t.org;
613 if(t.m.msec-clickmsec<500 && clickq0==q0 && t.q0==t.q1 && b==1){
614 doubleclick(&t.q0, &t.q1);
616 /* t.t.i->flush(); */
619 /* stay here until something interesting happens */
623 } while(t.m.buttons==b && abs(t.m.xy.x-x)<4 && abs(t.m.xy.y-y)<4);
624 t.m.xy.x = x; /* in case we're calling frselect */
629 if(t.m.buttons == b) {
632 t.q0 = t.f->p0 + t.org;
633 t.q1 = t.f->p1 + t.org;
634 clickmsec = t.m.msec;
637 if((t.m.buttons != b) &&(b&1)){
638 enum{Cancut = 1, Canpaste = 2} state = Cancut | Canpaste;
646 }else if(t.m.buttons&4){
650 paste(t.snarf, t.nsnarf, 0);
661 Rune newline[] = { '\n', 0 };
667 menu2str[Scroll] = "+ scroll";
669 menu2str[Scroll] = "- scroll";
671 menu2str[Cooked] = "+ mustecho";
673 menu2str[Cooked] = "- mustecho";
675 switch(menuhit(but, mc, &menu2, nil)){
686 paste(t.snarf, t.nsnarf, 0);
702 paste(t.snarf, t.nsnarf, 1);
703 if(t.nsnarf == 0 || t.snarf[t.nsnarf-1] != '\n')
704 paste(newline, 1, 1);
709 scrolling = !scrolling;
722 sysfatal("bad menu item");
727 windfilewidth(uint q0, int oneelement)
737 if(oneelement && r=='/')
745 showcandidates(Completion *c)
755 s = "[no matches in ";
759 fmtprint(&f, "%s%d files]\n", s, c->nfile);
761 fmtprint(&f, "%s", s);
762 for(i=0; i<c->nfile; i++){
765 fmtprint(&f, "%s", c->filename[i]);
769 /* place text at beginning of line before host point */
771 while(qline>0 && t.r[qline-1] != '\n')
774 rp = runefmtstrflush(&f);
778 q0 += insert(rp, nr, qline, 0) - qline;
789 Rune *rp, *path, *str;
791 char *s, *dir, *root;
793 /* control-f: filename completion; works back to white space or / */
794 if(t.q0<t.nr && t.r[t.q0]>' ') /* must be at end of word */
796 nstr = windfilewidth(t.q0, TRUE);
797 str = runemalloc(nstr);
798 runemove(str, t.r+(t.q0-nstr), nstr);
799 npath = windfilewidth(t.q0-nstr, FALSE);
800 path = runemalloc(npath);
801 runemove(path, t.r+(t.q0-nstr-npath), npath);
804 /* is path rooted? if not, we need to make it relative to window path */
805 if(npath>0 && path[0]=='/'){
806 dir = malloc(UTFmax*npath+1);
807 sprint(dir, "%.*S", npath, path);
809 if(strcmp(wdir, "") == 0)
813 dir = malloc(strlen(root)+1+UTFmax*npath+1);
814 sprint(dir, "%s/%.*S", root, npath, path);
816 dir = cleanname(dir);
818 s = smprint("%.*S", nstr, str);
819 c = complete(dir, s);
828 rp = runesmprint("%s", c->string);
841 setorigin(backnl(t.org, n), 1);
847 setorigin(line2q(n), 1);
848 if(t.qh<=t.org+t.f->nchars)
862 scrollup(t.f->maxlines*2/3);
865 scrolldown(t.f->maxlines*2/3);
868 scrollup(t.f->maxlines/3);
871 scrolldown(t.f->maxlines/3);
898 * Non-standard extensions.
913 paste(t.snarf, t.nsnarf, 0);
920 /* case 0x03: can't do this because ^C is COPY */
921 case 0x7F: /* DEL: send interrupt */
923 t.qh = t.q0 = t.q1 = t.nr;
925 /* must write the interrupt character in case app is in raw mode (e.g., ssh) */
926 write(rcfd, "\x7F", 1);
927 // postnote(PNGROUP, rcpid, "interrupt");
931 if(rawon() && t.q0==t.nr){
937 if(r==ESC || (holdon && r==0x7F)){ /* toggle hold */
940 // replaceintegerproperty("_9WM_HOLD_MODE", 1, 32, holdon);
950 case 0x06: /* ^F: file name completion */
951 case Kins: /* Insert: file name completion */
959 case 0x08: /* ^H: erase character */
960 case 0x15: /* ^U: erase line */
961 case 0x17: /* ^W: erase word */
962 if (t.q0 != 0 && t.q0 != t.qh)
963 t.q0 -= bswidth(r, t.q0, 1);
975 bswidth(Rune c, uint start, int eatnl)
981 /* there is known to be at least one character to erase */
982 if(c == 0x08) /* ^H: erase character */
991 if(r == '\n'){ /* eat at most one more character */
992 if(q == start && eatnl) /* eat the newline */
998 if(eq && skipping) /* found one; stop skipping */
1000 else if(!eq && !skipping)
1019 /* look to see if there is a complete line */
1020 for(i=t.qh; i<t.nr; i++){
1022 if(c=='\n' || c=='\004' || c=='\x7F')
1043 while(n >= UTFmax && (t.qh<t.nr || t.nraw > 0)) {
1045 width = runetochar(p, &t.raw[0]);
1047 runemove(t.raw, t.raw+1, t.nraw);
1049 width = runetochar(p, &t.r[t.qh++]);
1053 if(!raw && (c == '\n' || c == '\004' || c == '\x7F'))
1059 * If we've been echoing, make sure the terminal isn't
1060 * while we do the write. This screws up if someone
1061 * else tries to turn off echo at the same time we do
1062 * (we'll turn it on again after the write), but that's not
1065 s = setecho(sfd, 0);
1066 if(write(rcfd, buf, n) < 0)
1074 conswrite(char *p, int n)
1077 Rune buf2[1000], *q;
1079 /* convert to runes */
1082 /* handle partial runes */
1083 while(i < UTFmax && n>0) {
1088 if(fullrune(t.part, i)) {
1090 chartorune(buf2, t.part);
1095 /* there is a little extra room in a message buf */
1098 while(n >= UTFmax || fullrune(p, n)) {
1103 if(n < UTFmax && !fullrune(p, n))
1105 i = chartorune(q, p);
1111 runewrite(buf2, q-buf2);
1115 assert(n+t.npart < UTFmax);
1116 memcpy(t.part+t.npart, p, n);
1125 runewrite(Rune *r, int n)
1137 /* get rid of backspaces */
1140 for(i=0; i<n; i++) {
1146 } else if(*p == '\r') { /* treat like ^U */
1147 /* convert CR without NL into erased line */
1148 /* i feel really sleazy about this but it helps */
1149 while(i<n-1 && *(p+1) == '\r'){
1153 if(i<n-1 && *(p+1) != '\n'){
1154 while(q > r && *(q-1) != '\n')
1157 initial = bswidth(0x15, t.qh, 0);
1166 /* write turned into a delete */
1173 runemove(t.r+q0, t.r+q1, t.nr-q1);
1186 else if(q0 < t.org+t.f->nchars){
1194 if(p1 > t.f->nchars)
1196 frdelete(t.f, p0, p1);
1202 insert(r, n, t.qh, 1);
1215 if (q0 < t.org && q1 >= t.org)
1221 runemove(t.r+q0, t.r+q1, t.nr-q1);
1230 else if(q0 < t.org+t.f->nchars){
1231 assert(q0 >= t.org);
1234 if(p1 > t.f->nchars)
1236 frdelete(t.f, p0, p1);
1257 t.snarf = runerealloc(t.snarf, n);
1258 for(i=0,p=t.snarf; i<n; p++)
1259 i += chartorune(p, pp+i);
1260 t.nsnarf = p-t.snarf;
1264 char sbuf[SnarfSize];
1275 t.snarf = runerealloc(t.snarf, n);
1276 for(i=0,p=sbuf,rp=t.snarf; i<n && p < sbuf+SnarfSize-UTFmax; i++){
1277 *rp++ = *(t.r+t.q0+i);
1278 p += runetochar(p, t.r+t.q0+i);
1280 t.nsnarf = rp-t.snarf;
1302 insert(Rune *r, int n, uint q0, int hostwrite)
1308 if(t.nr+n>HiWater && q0>=t.org && q0>=t.qh){
1309 m = min(HiWater-LoWater, min(t.org, t.qh));
1321 runemove(t.r, t.r+m, t.nr);
1324 if(t.nr+n > t.maxr){
1326 * Minimize realloc breakage:
1327 * Allocate at least MinWater
1328 * Double allocation size each time
1329 * But don't go much above HiWater
1331 m = max(min(2*(t.nr+n), HiWater), t.nr+n)+MinWater;
1333 m = max(HiWater+MinWater, t.nr+n);
1335 t.r = runerealloc(t.r, m);
1339 runemove(t.r+q0+n, t.r+q0, t.nr-q0);
1340 runemove(t.r+q0, r, n);
1342 /* if output touches, advance selection, not qh; works best for keyboard and output */
1347 if(q0 < t.qh || (q0==t.qh && hostwrite))
1353 else if(q0 <= t.org+t.f->nchars)
1354 frinsert(t.f, r, r+n, q0-t.org);
1359 paste(Rune *r, int n, int advance)
1363 if(rawon() && t.q0==t.nr){
1373 * if this is a button2 execute then we might have been passed
1374 * runes inside the buffer. must save them before realloc.
1377 if(t.r <= r && r < t.r+n){
1378 rbuf = runemalloc(n);
1379 runemove(rbuf, r, n);
1383 insert(r, n, t.q0, 0);
1391 if (t.f->nlines >= t.f->maxlines)
1393 frinsert(t.f, t.r + t.org + t.f->nchars, t.r + t.nr, t.f->nchars);
1403 if(t.org+f->p0 == t.q0 && t.org+f->p1 == t.q1)
1408 frdrawsel(f, frptofchar(f, f->p0), f->p0, f->p1, 0);
1421 frdrawsel(f, frptofchar(f, f->p0), f->p0, f->p1, 1);
1424 if(t.qh<=t.org+t.f.nchars && t.cwqueue != 0)
1425 t.cwqueue->wakeup <-= 0;
1441 nl = t.f->maxlines/5;
1443 nl = 4*t.f->maxlines/5;
1445 /* avoid going in the wrong direction */
1446 if (q0>t.org && q<t.org)
1449 /* keep trying until q0 is on the screen */
1450 while(!cansee(q0)) {
1451 assert(q0 >= t.org);
1453 q = line2q(t.f->maxlines-nl);
1464 qe = t.org+t.f->nchars;
1466 if(q0>=t.org && q0 < qe)
1470 if (t.f->nlines < t.f->maxlines)
1472 if (q0 > 0 && t.r[t.nr-1] == '\n')
1479 setorigin(uint org, int exact)
1484 if(org>0 && !exact){
1485 /* try and start after a newline */
1486 /* don't try harder than 256 chars */
1487 for(i=0; i<256 && org<t.nr; i++){
1488 if(t.r[org-1] == '\n')
1495 if(a>=0 && a<t.f->nchars)
1496 frdelete(t.f, 0, a);
1497 else if(a<0 && -a<100*t.f->maxlines){
1499 frinsert(t.f, t.r+org, t.r+org+n, 0);
1501 frdelete(t.f, 0, t.f->nchars);
1514 return frcharofpt(f, Pt(f->r.min.x, f->r.min.y + n*font->height))+t.org;
1518 backnl(uint p, uint n)
1523 /* at 256 chars, call it a line anyway */
1524 for(j=256; --j>0 && p>0; p--)
1527 if (p == 0 || i == 0)
1534 addraw(Rune *r, int nr)
1536 t.raw = runerealloc(t.raw, t.nraw+nr);
1537 runemove(t.raw+t.nraw, r, nr);
1540 if(t.crqueue != nil)
1541 t.crqueue->wakeup <-= 0;
1546 Rune left1[] = { '{', '[', '(', '<', 0xab, 0 };
1547 Rune right1[] = { '}', ']', ')', '>', 0xbb, 0 };
1548 Rune left2[] = { '\n', 0 };
1549 Rune left3[] = { '\'', '"', '`', 0 };
1566 doubleclick(uint *q0, uint *q1)
1572 for(i=0; left[i]!=0; i++){
1576 /* try matching character to left, looking right */
1583 if(clickmatch(c, r[p-l], 1, &q))
1587 /* try matching character to right, looking left */
1594 if(clickmatch(c, l[p-r], -1, &q)){
1595 *q1 = *q0+(*q0<t.nr && c=='\n');
1597 if(c!='\n' || q!=0 || t.r[0]=='\n')
1603 /* try filling out word to right */
1604 while(*q1<t.nr && isalnum(t.r[*q1]))
1606 /* try filling out word to left */
1607 while(*q0>0 && isalnum(t.r[*q0-1]))
1612 clickmatch(int cl, int cr, int dir, uint *q)
1636 return cl=='\n' && nest==1;
1646 assert(t.q0 <= t.q1 && t.q1 <= t.nr);
1647 assert(t.org <= t.nr && t.qh <= t.nr);
1648 assert(f->p0 <= f->p1 && f->p1 <= f->nchars);
1649 assert(t.org + f->nchars <= t.nr);
1650 assert(t.org+f->nchars==t.nr || (f->nlines >= f->maxlines));
1654 strrune(Rune *s, Rune c)
1673 Rectangle r, r1, r2;
1677 r.min.x += 1; /* border between margin and bar */
1679 if(scrx==0 || scrx->r.max.y < r.max.y){
1682 scrx = allocimage(display, Rect(0, 0, 32, r.max.y), screen->chan, 1, DPaleyellow);
1684 sysfatal("scroll balloc");
1688 r2 = scrpos(r1, t.org, t.org+t.f->nchars, t.nr);
1689 if(!eqrect(r2, lastsr)){
1691 draw(scrx, r1, cols[BORD], nil, ZP);
1692 draw(scrx, r2, cols[BACK], nil, r2.min);
1694 // r2.min.x = r2.max.x-1;
1695 // draw(scrx, r2, cols[BORD], nil, ZP);
1696 draw(screen, r, scrx, nil, r1.min);
1701 scrpos(Rectangle r, ulong p0, ulong p1, ulong tot)
1706 q = insetrect(r, 1);
1707 h = q.max.y-q.min.y;
1710 if(tot > 1024L*1024L)
1711 tot >>= 10, p0 >>= 10, p1 >>= 10;
1713 q.min.y += h*p0/tot;
1715 q.max.y -= h*(tot-p1)/tot;
1716 if(q.max.y < q.min.y+2){
1717 if(q.min.y+2 <= r.max.y)
1718 q.max.y = q.min.y+2;
1720 q.min.y = q.max.y-2;
1730 int x, y, my, h, first, exact;
1732 s = insetrect(scrollr, 1);
1733 h = s.max.y-s.min.y;
1734 x = (s.min.x+s.max.x)/2;
1738 if(t.m.xy.x<s.min.x || s.max.x<=t.m.xy.x){
1747 // if(!eqpt(t.m.xy, Pt(x, my)))
1748 // cursorset(Pt(x, my));
1754 if(t.nr > 1024*1024)
1755 p0 = ((t.nr>>10)*(y-s.min.y)/h)<<10;
1757 p0 = t.nr*(y-s.min.y)/h;
1760 p0 = backnl(t.org, (my-s.min.y)/font->height);
1762 p0 = t.org+frcharofpt(t.f, Pt(s.max.x, my));
1765 setorigin(p0, exact);
1771 }while(t.m.buttons & (1<<(but-1)));
1777 if((plumbfd = plumbopen("send", OWRITE)) < 0)
1778 fprint(2, "9term: plumbopen: %r\n");
1782 plumb(uint q0, uint q1)
1789 pm = malloc(sizeof(Plumbmsg));
1790 pm->src = strdup("9term");
1792 pm->wdir = strdup(wdir);
1793 pm->type = strdup("text");
1799 wordclick(&q0, &q1);
1800 sprint(cbuf, "click=%d", p0-q0);
1801 pm->attr = plumbunpackattr(cbuf);
1807 pm->data = malloc(SnarfSize);
1809 for(i=0,p=pm->data; i<n && p < pm->data + SnarfSize-UTFmax; i++)
1810 p += runetochar(p, t.r+q0+i);
1812 pm->ndata = strlen(pm->data);
1813 if(plumbsend(plumbfd, pm) < 0){
1814 setcursor(mc, &query);
1817 setcursor(mc, &whitearrow);
1825 * Process in-band messages about window title changes.
1826 * The messages are of the form:
1830 * where xxx is the new directory. This format was chosen
1831 * because it changes the label on xterm windows.
1834 label(Rune *sr, int n)
1836 Rune *sl, *el, *er, *r;
1839 for(r=er-1; r>=sr; r--)
1846 if(el-sr > sizeof wdir)
1847 sr = el - sizeof wdir;
1848 for(sl=el-3; sl>=sr; sl--)
1849 if(sl[0]=='\033' && sl[1]==']' && sl[2]==';')
1854 snprint(wdir, sizeof wdir, "%.*S", (el-1)-(sl+3), sl+3);
1857 runemove(sl, el, er-el);
1865 return !cooked && !isecho(sfd);
1869 * Clumsy hack to make " and "" work.
1870 * Then again, what's not a clumsy hack here in Unix land?
1877 removethesocket(void)
1880 if(remove(thesocket) < 0)
1881 fprint(2, "remove %s: %r\n", thesocket);
1889 snprint(buf, sizeof buf, "unix!/tmp/9term-text.%d", getpid());
1891 if((afd = announce(buf, adir)) < 0){
1892 putenv("text9term", "");
1896 putenv("text9term", buf);
1897 proccreate(listenproc, nil, STACK);
1898 strcpy(thesocket, buf+5);
1899 atexit(removethesocket);
1903 listenproc(void *arg)
1910 fd = listen(adir, dir);
1915 proccreate(textthread, (void*)fd, STACK);
1920 textthread(void *arg)
1922 int fd, i, x, n, end;
1924 char buf[4096], *p, *ep;
1928 ep = buf+sizeof buf;
1929 end = t.org+t.nr; /* avoid possible output loop */
1931 if(i >= end || ep-p < UTFmax){
1932 for(x=0; x<p-buf; x+=n)
1933 if((n = write(fd, buf+x, (p-x)-buf)) <= 0)
1946 p += runetochar(p, &r);