Blob


1 #include <u.h>
2 #include <signal.h>
3 #include <libc.h>
4 #include <ctype.h>
5 #include <draw.h>
6 #include <thread.h>
7 #include <mouse.h>
8 #include <cursor.h>
9 #include <keyboard.h>
10 #include <frame.h>
11 #include <plumb.h>
12 #include <complete.h>
13 #include "term.h"
15 enum
16 {
17 STACK = 32768
18 };
20 int noecho = 0;
22 void servedevtext(void);
23 void listenproc(void*);
24 void textthread(void*);
26 typedef struct Text Text;
27 typedef struct Readbuf Readbuf;
29 enum
30 {
31 HiWater = 640000, /* max size of history */
32 LoWater = 400000, /* min size of history after max'ed */
33 MinWater = 20000,
34 };
36 /* various geometric paramters */
37 enum
38 {
39 Scrollwid = 12, /* width of scroll bar */
40 Scrollgap = 4, /* gap right of scroll bar */
41 Maxtab = 4,
42 };
44 enum
45 {
46 Cut,
47 Paste,
48 Snarf,
49 Send,
50 Plumb,
51 Scroll,
52 Cooked,
53 };
55 #define ESC 0x1B
56 #define CUT 0x18 /* ctrl-x */
57 #define COPY 0x03 /* crtl-c */
58 #define PASTE 0x16 /* crtl-v */
60 #define READBUFSIZE 8192
61 #define TRUE 1
62 #define FALSE 0
65 struct Text
66 {
67 Frame *f; /* frame ofr terminal */
68 Mouse m;
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 */
79 char part[UTFmax];
80 int nsnarf; /* snarf buffer */
81 Rune *snarf;
82 };
84 struct Readbuf
85 {
86 short n; /* # bytes in buf */
87 uchar data[READBUFSIZE]; /* data bytes */
88 };
90 void mouse(void);
91 void domenu2(int);
92 void loop(void);
93 void geom(void);
94 void fill(void);
95 void tcheck(void);
96 void updatesel(void);
97 void doreshape(void);
98 void runewrite(Rune*, int);
99 void consread(void);
100 void conswrite(char*, int);
101 int bswidth(Rune c, uint start, int eatnl);
102 void cut(void);
103 void paste(Rune*, int, int);
104 void snarfupdate(void);
105 void snarf(void);
106 void show(uint);
107 void key(Rune);
108 void setorigin(uint org, int exact);
109 uint line2q(uint);
110 uint backnl(uint, uint);
111 int cansee(uint);
112 uint backnl(uint, uint);
113 void addraw(Rune*, int);
114 void mselect(void);
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);
118 int consready(void);
119 Rectangle scrpos(Rectangle r, ulong p0, ulong p1, ulong tot);
120 void scrdraw(void);
121 void scroll(int);
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);
129 void scrollup(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 */
142 int rcfd;
143 int sfd; /* slave fd, to get/set terminal mode */
144 int rcpid;
145 int maxtab;
146 int use9wm;
147 Mousectl* mc;
148 Keyboardctl* kc;
149 Channel* hostc;
150 Readbuf rcbuf[2];
151 int mainpid;
152 int acmecolors;
153 int plumbfd;
154 int button2exec;
155 int label(Rune*, int);
156 char wdir[1024];
157 char childwdir[1024];
158 void hangupnote(void*, char*);
159 char thesocket[100];
161 char *menu2str[] = {
162 "cut",
163 "paste",
164 "snarf",
165 "send",
166 "plumb",
167 "scroll",
168 "cooked",
170 };
172 Image* cols[NCOL];
173 Image* hcols[NCOL];
174 Image* palegrey;
175 Image* paleblue;
176 Image* blue;
177 Image *plumbcolor;
178 Image *execcolor;
180 Menu menu2 =
182 menu2str
183 };
185 Text t;
187 Cursor whitearrow = {
188 {0, 0},
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, }
197 };
199 Cursor query = {
200 {-7,-7},
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, }
209 };
211 void
212 usage(void)
214 fprint(2, "usage: 9term [-ars] [-W winsize] [cmd ...]\n");
215 threadexitsall("usage");
218 void
219 threadmain(int argc, char *argv[])
221 char *p, *font;
222 char buf[32];
224 rfork(RFNOTEG);
225 font = nil;
226 _wantfocuschanges = 1;
227 mainpid = getpid();
228 ARGBEGIN{
229 default:
230 usage();
231 case 'a': /* acme mode */
232 button2exec++;
233 break;
234 case 'f':
235 font = EARGF(usage());
236 break;
237 case 's':
238 scrolling++;
239 break;
240 case 'w': /* started from "rio" window manager */
241 use9wm = 1;
242 break;
243 case 'W':
244 winsize = EARGF(usage());
245 break;
246 }ARGEND
248 if(font)
249 putenv("font", font);
251 p = getenv("tabstop");
252 if(p == 0)
253 p = getenv("TABSTOP");
254 if(p != 0 && maxtab <= 0)
255 maxtab = strtoul(p, 0, 0);
256 if(maxtab <= 0)
257 maxtab = 4; /* be like rio */
259 snprint(buf, sizeof buf, "%d", maxtab);
260 putenv("tabstop", buf);
262 initdraw(0, nil, "9term");
263 notify(hangupnote);
264 noteenable("sys: child");
265 servedevtext();
267 mc = initmouse(nil, screen);
268 kc = initkeyboard(nil);
269 rcpid = rcstart(argc, argv, &rcfd, &sfd);
270 hoststart();
271 plumbstart();
273 t.f = mallocz(sizeof(Frame), 1);
275 if(acmecolors){
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);
279 }else{
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);
293 hcols[BORD] = blue;
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);
303 geom();
304 loop();
307 void
308 hangupnote(void *a, char *msg)
310 if(getpid() != mainpid)
311 noted(NDFLT);
312 if(strcmp(msg, "hangup") == 0 && rcpid != 0){
313 postnote(PNGROUP, rcpid, "hangup");
314 noted(NDFLT);
316 if(strstr(msg, "child")){
317 char buf[128];
318 int n;
320 n = awaitnohang(buf, sizeof buf-1);
321 if(n > 0){
322 buf[n] = 0;
323 if(atoi(buf) == rcpid)
324 threadexitsall(0);
326 noted(NCONT);
328 noted(NDFLT);
331 void
332 hostproc(void *arg)
334 Channel *c;
335 int i, n, which;
337 c = arg;
339 i = 0;
340 for(;;){
341 /* Let typing have a go -- maybe there's a rubout waiting. */
342 yield();
344 i = 1-i; /* toggle */
345 n = read(rcfd, rcbuf[i].data, sizeof rcbuf[i].data);
346 if(n <= 0){
347 if(n < 0)
348 fprint(2, "9term: host read error: %r\n");
349 threadexitsall("host");
351 rcbuf[i].n = n;
352 which = i;
353 send(c, &which);
357 void
358 hoststart(void)
360 hostc = chancreate(sizeof(int), 0);
361 proccreate(hostproc, hostc, 32*1024);
364 void
365 loop(void)
367 Rune r;
368 int i;
369 Alt a[5];
371 a[0].c = mc->c;
372 a[0].v = &mc->m;
373 a[0].op = CHANRCV;
375 a[1].c = kc->c;
376 a[1].v = &r;
377 a[1].op = CHANRCV;
379 a[2].c = hostc;
380 a[2].v = &i;
381 a[2].op = CHANRCV;
383 a[3].c = mc->resizec;
384 a[3].v = nil;
385 a[3].op = CHANRCV;
387 a[4].c = nil;
388 a[4].v = nil;
389 a[4].op = CHANEND;
391 for(;;) {
392 tcheck();
394 scrdraw();
395 flushimage(display, 1);
396 a[2].op = CHANRCV;
397 if(!scrolling && t.qh > t.org+t.f->nchars)
398 a[2].op = CHANNOP;;
399 switch(alt(a)) {
400 default:
401 sysfatal("impossible");
402 case 0:
403 t.m = mc->m;
404 mouse();
405 break;
406 case 1:
407 key(r);
408 break;
409 case 2:
410 conswrite((char*)rcbuf[i].data, rcbuf[i].n);
411 break;
412 case 3:
413 doreshape();
414 break;
419 void
420 doreshape(void)
422 if(getwindow(display, Refnone) < 0)
423 sysfatal("can't reattach to window");
424 draw(screen, screen->r, cols[BACK], nil, ZP);
425 geom();
426 scrdraw();
429 void
430 geom(void)
432 Point p;
433 Rectangle r;
435 if(!acmecolors){
436 if(_windowhasfocus){
437 cols[TEXT] = cols[HTEXT] = display->black;
438 hcols[TEXT] = hcols[HTEXT] = blue;
439 }else{
440 cols[TEXT] = cols[HTEXT] = palegrey;
441 hcols[TEXT] = hcols[HTEXT] = paleblue;
445 r = screen->r;
446 r.min.y++;
447 r.max.y--;
449 scrollr = r;
450 scrollr.max.x = r.min.x+Scrollwid;
451 lastsr = Rect(0,0,0,0);
453 r.min.x += Scrollwid+Scrollgap;
455 frclear(t.f, 0);
456 frinit(t.f, r, font, screen, holdon ? hcols : cols);
457 t.f->maxtab = maxtab*stringwidth(font, "0");
458 fill();
459 updatesel();
461 p = stringsize(font, "0");
462 if(p.x == 0 || p.y == 0)
463 return;
465 updatewinsize(Dy(r)/p.y, Dx(r)/p.x, Dx(r), Dy(r));
468 void
469 drawhold(int holdon)
471 if(holdon)
472 setcursor(mc, &whitearrow);
473 else
474 setcursor(mc, nil);
476 draw(screen, screen->r, cols[BACK], nil, ZP);
477 geom();
478 scrdraw();
481 void
482 wordclick(uint *q0, uint *q1)
484 while(*q1<t.nr && !isspace(t.r[*q1]))
485 (*q1)++;
486 while(*q0>0 && !isspace(t.r[*q0-1]))
487 (*q0)--;
490 int
491 aselect(uint *q0, uint *q1, Image *color)
493 int cancel;
494 uint oldq0, oldq1, newq0, newq1;
496 /* save old selection */
497 oldq0 = t.q0;
498 oldq1 = t.q1;
500 /* sweep out area and record it */
501 t.f->cols[HIGH] = color;
502 t.f->cols[HTEXT] = display->white;
503 mselect();
504 newq0 = t.q0;
505 newq1 = t.q1;
507 cancel = 0;
508 if(t.m.buttons != 0){
509 while(t.m.buttons){
510 readmouse(mc);
511 t.m = mc->m;
513 cancel = 1;
516 /* restore old selection */
517 t.f->cols[HIGH] = cols[HIGH];
518 t.f->cols[HTEXT] = cols[HTEXT];
519 t.q0 = oldq0;
520 t.q1 = oldq1;
521 updatesel();
523 if(cancel)
524 return -1;
526 /* selected a region */
527 if(newq0 < newq1){
528 *q0 = newq0;
529 *q1 = newq1;
530 return 0;
533 /* clicked inside previous selection */
534 /* the "<=" in newq0 <= oldq1 allows us to click the right edge */
535 if(oldq0 <= newq0 && newq0 <= oldq1){
536 *q0 = oldq0;
537 *q1 = oldq1;
538 return 0;
541 /* just a click */
542 *q0 = newq0;
543 *q1 = newq1;
544 return 0;
547 static Rune Lnl[1] = { '\n' };
549 void
550 mouse(void)
552 int but;
553 uint q0, q1;
555 but = t.m.buttons;
557 if(but != 1 && but != 2 && but != 4 && but != 8 && but != 16)
558 return;
560 if (ptinrect(t.m.xy, scrollr)) {
561 scroll(but);
562 if(t.qh<=t.org+t.f->nchars)
563 consread();
564 return;
567 switch(but) {
568 case 1:
569 mselect();
570 break;
571 case 2:
572 if(button2exec){
573 if(aselect(&q0, &q1, execcolor) >= 0){
574 if(q0 == q1)
575 wordclick(&q0, &q1);
576 if(q0 == q1)
577 break;
578 t.q0 = t.q1 = t.nr;
579 updatesel();
580 paste(t.r+q0, q1-q0, 1);
581 if(t.r[q1-1] != '\n')
582 paste(Lnl, 1, 1);
584 break;
586 domenu2(2);
587 break;
588 case 4:
589 bouncemouse(&t.m);
590 break;
591 /*
592 if(aselect(&q0, &q1, plumbcolor) >= 0)
593 plumb(q0, q1);
594 break;
595 */
596 case 8:
597 scrollup(mousescrollsize(t.f->maxlines));
598 break;
599 case 16:
600 scrolldown(mousescrollsize(t.f->maxlines));
601 break;
605 void
606 mselect(void)
608 int b, x, y;
609 uint q0;
611 b = t.m.buttons;
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);
615 updatesel();
616 /* t.t.i->flush(); */
617 x = t.m.xy.x;
618 y = t.m.xy.y;
619 /* stay here until something interesting happens */
620 do {
621 readmouse(mc);
622 t.m = mc->m;
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 */
625 t.m.xy.y = y;
626 clickmsec = 0;
629 if(t.m.buttons == b) {
630 frselect(t.f, mc);
631 t.m = mc->m;
632 t.q0 = t.f->p0 + t.org;
633 t.q1 = t.f->p1 + t.org;
634 clickmsec = t.m.msec;
635 clickq0 = t.q0;
637 if((t.m.buttons != b) &&(b&1)){
638 enum{Cancut = 1, Canpaste = 2} state = Cancut | Canpaste;
639 while(t.m.buttons){
640 if(t.m.buttons&2){
641 if(state&Cancut){
642 snarf();
643 cut();
644 state = Canpaste;
646 }else if(t.m.buttons&4){
647 if(state&Canpaste){
648 snarfupdate();
649 if(t.nsnarf){
650 paste(t.snarf, t.nsnarf, 0);
652 state = Cancut;
655 readmouse(mc);
656 t.m = mc->m;
661 Rune newline[] = { '\n', 0 };
663 void
664 domenu2(int but)
666 if(scrolling)
667 menu2str[Scroll] = "+ scroll";
668 else
669 menu2str[Scroll] = "- scroll";
670 if(cooked)
671 menu2str[Cooked] = "+ mustecho";
672 else
673 menu2str[Cooked] = "- mustecho";
675 switch(menuhit(but, mc, &menu2, nil)){
676 case -1:
677 break;
678 case Cut:
679 snarf();
680 cut();
681 if(scrolling)
682 show(t.q0);
683 break;
684 case Paste:
685 snarfupdate();
686 paste(t.snarf, t.nsnarf, 0);
687 if(scrolling)
688 show(t.q0);
689 break;
690 case Snarf:
691 snarf();
692 if(scrolling)
693 show(t.q0);
694 break;
695 case Send:
696 if(t.q0 != t.q1)
697 snarf();
698 else
699 snarfupdate();
700 t.q0 = t.q1 = t.nr;
701 updatesel();
702 paste(t.snarf, t.nsnarf, 1);
703 if(t.nsnarf == 0 || t.snarf[t.nsnarf-1] != '\n')
704 paste(newline, 1, 1);
705 show(t.nr);
706 consread();
707 break;
708 case Scroll:
709 scrolling = !scrolling;
710 if (scrolling) {
711 show(t.nr);
712 consread();
714 break;
715 case Plumb:
716 plumb(t.q0, t.q1);
717 break;
718 case Cooked:
719 cooked = !cooked;
720 break;
721 default:
722 sysfatal("bad menu item");
726 int
727 windfilewidth(uint q0, int oneelement)
729 uint q;
730 Rune r;
732 q = q0;
733 while(q > 0){
734 r = t.r[q-1];
735 if(r<=' ')
736 break;
737 if(oneelement && r=='/')
738 break;
739 --q;
741 return q0-q;
744 void
745 showcandidates(Completion *c)
747 int i;
748 Fmt f;
749 Rune *rp;
750 uint nr, qline, q0;
751 char *s;
753 runefmtstrinit(&f);
754 if (c->nmatch == 0)
755 s = "[no matches in ";
756 else
757 s = "[";
758 if(c->nfile > 32)
759 fmtprint(&f, "%s%d files]\n", s, c->nfile);
760 else{
761 fmtprint(&f, "%s", s);
762 for(i=0; i<c->nfile; i++){
763 if(i > 0)
764 fmtprint(&f, " ");
765 fmtprint(&f, "%s", c->filename[i]);
767 fmtprint(&f, "]\n");
769 /* place text at beginning of line before host point */
770 qline = t.qh;
771 while(qline>0 && t.r[qline-1] != '\n')
772 qline--;
774 rp = runefmtstrflush(&f);
775 nr = runestrlen(rp);
777 q0 = t.q0;
778 q0 += insert(rp, nr, qline, 0) - qline;
779 free(rp);
780 t.q0 = q0+nr;
781 t.q1 = q0+nr;
782 updatesel();
785 Rune*
786 namecomplete(void)
788 int nstr, npath;
789 Rune *rp, *path, *str;
790 Completion *c;
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 */
795 return nil;
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);
802 rp = nil;
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);
808 }else{
809 if(strcmp(wdir, "") == 0)
810 root = ".";
811 else
812 root = wdir;
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);
820 free(s);
821 if(c == nil)
822 goto Return;
824 if(!c->advance)
825 showcandidates(c);
827 if(c->advance)
828 rp = runesmprint("%s", c->string);
830 Return:
831 freecompletion(c);
832 free(dir);
833 free(path);
834 free(str);
835 return rp;
838 void
839 scrollup(int n)
841 setorigin(backnl(t.org, n), 1);
844 void
845 scrolldown(int n)
847 setorigin(line2q(n), 1);
848 if(t.qh<=t.org+t.f->nchars)
849 consread();
852 void
853 key(Rune r)
855 Rune *rp;
856 int nr;
858 if(r == 0)
859 return;
860 switch(r){
861 case Kpgup:
862 scrollup(t.f->maxlines*2/3);
863 return;
864 case Kpgdown:
865 scrolldown(t.f->maxlines*2/3);
866 return;
867 case Kup:
868 scrollup(t.f->maxlines/3);
869 return;
870 case Kdown:
871 scrolldown(t.f->maxlines/3);
872 return;
873 case Kleft:
874 if(t.q0 > 0){
875 t.q0--;
876 t.q1 = t.q0;
877 updatesel();
878 show(t.q0);
880 return;
881 case Kright:
882 if(t.q1 < t.nr){
883 t.q1++;
884 t.q0 = t.q1;
885 updatesel();
886 show(t.q1);
888 return;
889 case Khome:
890 show(0);
891 return;
892 case Kend:
893 case 0x05:
894 show(t.nr);
895 return;
897 /*
898 * Non-standard extensions.
899 */
900 case CUT:
901 snarf();
902 cut();
903 if(scrolling)
904 show(t.q0);
905 return;
906 case COPY:
907 snarf();
908 if(scrolling)
909 show(t.q0);
910 return;
911 case PASTE:
912 snarfupdate();
913 paste(t.snarf, t.nsnarf, 0);
914 if(scrolling)
915 show(t.q0);
916 return;
919 switch(r) {
920 /* case 0x03: can't do this because ^C is COPY */
921 case 0x7F: /* DEL: send interrupt */
922 paste(&r, 1, 1);
923 t.qh = t.q0 = t.q1 = t.nr;
924 show(t.q0);
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");
928 return;
931 if(rawon() && t.q0==t.nr){
932 addraw(&r, 1);
933 consread();
934 return;
937 if(r==ESC || (holdon && r==0x7F)){ /* toggle hold */
938 holdon = !holdon;
939 drawhold(holdon);
940 // replaceintegerproperty("_9WM_HOLD_MODE", 1, 32, holdon);
941 if(!holdon)
942 consread();
943 if(r==ESC)
944 return;
947 snarf();
949 switch(r) {
950 case 0x06: /* ^F: file name completion */
951 case Kins: /* Insert: file name completion */
952 rp = namecomplete();
953 if(rp == nil)
954 return;
955 nr = runestrlen(rp);
956 paste(rp, nr, 1);
957 free(rp);
958 return;
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);
964 cut();
965 break;
966 default:
967 paste(&r, 1, 1);
968 break;
970 if(scrolling)
971 show(t.q0);
974 int
975 bswidth(Rune c, uint start, int eatnl)
977 uint q, eq, stop;
978 Rune r;
979 int skipping;
981 /* there is known to be at least one character to erase */
982 if(c == 0x08) /* ^H: erase character */
983 return 1;
984 q = start;
985 stop = 0;
986 if(q > t.qh)
987 stop = t.qh;
988 skipping = 1;
989 while(q > stop){
990 r = t.r[q-1];
991 if(r == '\n'){ /* eat at most one more character */
992 if(q == start && eatnl) /* eat the newline */
993 --q;
994 break;
996 if(c == 0x17){
997 eq = isalnum(r);
998 if(eq && skipping) /* found one; stop skipping */
999 skipping = 0;
1000 else if(!eq && !skipping)
1001 break;
1003 --q;
1005 return start-q;
1008 int
1009 consready(void)
1011 int i, c;
1013 if(holdon)
1014 return 0;
1016 if(rawon())
1017 return t.nraw != 0;
1019 /* look to see if there is a complete line */
1020 for(i=t.qh; i<t.nr; i++){
1021 c = t.r[i];
1022 if(c=='\n' || c=='\004' || c=='\x7F')
1023 return 1;
1025 return 0;
1029 void
1030 consread(void)
1032 char buf[8000], *p;
1033 int c, width, n;
1034 int s, raw;
1036 raw = rawon();
1037 for(;;) {
1038 if(!consready())
1039 return;
1040 n = sizeof(buf);
1041 p = buf;
1042 c = 0;
1043 while(n >= UTFmax && (t.qh<t.nr || t.nraw > 0)) {
1044 if(t.qh == t.nr){
1045 width = runetochar(p, &t.raw[0]);
1046 t.nraw--;
1047 runemove(t.raw, t.raw+1, t.nraw);
1048 }else
1049 width = runetochar(p, &t.r[t.qh++]);
1050 c = *p;
1051 p += width;
1052 n -= width;
1053 if(!raw && (c == '\n' || c == '\004' || c == '\x7F'))
1054 break;
1056 n = p-buf;
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
1063 * too likely.
1065 s = setecho(sfd, 0);
1066 if(write(rcfd, buf, n) < 0)
1067 exits(0);
1068 if(s)
1069 setecho(sfd, s);
1073 void
1074 conswrite(char *p, int n)
1076 int n2, i;
1077 Rune buf2[1000], *q;
1079 /* convert to runes */
1080 i = t.npart;
1081 if(i > 0){
1082 /* handle partial runes */
1083 while(i < UTFmax && n>0) {
1084 t.part[i] = *p;
1085 i++;
1086 p++;
1087 n--;
1088 if(fullrune(t.part, i)) {
1089 t.npart = 0;
1090 chartorune(buf2, t.part);
1091 runewrite(buf2, 1);
1092 break;
1095 /* there is a little extra room in a message buf */
1098 while(n >= UTFmax || fullrune(p, n)) {
1099 n2 = nelem(buf2);
1100 q = buf2;
1102 while(n2) {
1103 if(n < UTFmax && !fullrune(p, n))
1104 break;
1105 i = chartorune(q, p);
1106 p += i;
1107 n -= i;
1108 n2--;
1109 q++;
1111 runewrite(buf2, q-buf2);
1114 if(n != 0) {
1115 assert(n+t.npart < UTFmax);
1116 memcpy(t.part+t.npart, p, n);
1117 t.npart += n;
1120 if(scrolling)
1121 show(t.qh);
1124 void
1125 runewrite(Rune *r, int n)
1127 int i;
1128 uint initial;
1129 uint q0, q1;
1130 uint p0, p1;
1131 Rune *p, *q;
1133 n = label(r, n);
1134 if(n == 0)
1135 return;
1137 /* get rid of backspaces */
1138 initial = 0;
1139 p = q = r;
1140 for(i=0; i<n; i++) {
1141 if(*p == '\b') {
1142 if(q == r)
1143 initial++;
1144 else
1145 --q;
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'){
1150 i++;
1151 p++;
1153 if(i<n-1 && *(p+1) != '\n'){
1154 while(q > r && *(q-1) != '\n')
1155 q--;
1156 if(q==r)
1157 initial = bswidth(0x15, t.qh, 0);
1159 } else if(*p)
1160 *q++ = *p;
1161 p++;
1163 n = q-r;
1165 if(initial){
1166 /* write turned into a delete */
1168 if(initial > t.qh)
1169 initial = t.qh;
1170 q0 = t.qh-initial;
1171 q1 = t.qh;
1173 runemove(t.r+q0, t.r+q1, t.nr-q1);
1174 t.nr -= initial;
1175 t.qh -= initial;
1176 if(t.q0 > q1)
1177 t.q0 -= initial;
1178 else if(t.q0 > q0)
1179 t.q0 = q0;
1180 if(t.q1 > q1)
1181 t.q1 -= initial;
1182 else if(t.q1 > q0)
1183 t.q1 = q0;
1184 if(t.org > q1)
1185 t.org -= initial;
1186 else if(q0 < t.org+t.f->nchars){
1187 if(t.org < q0)
1188 p0 = q0 - t.org;
1189 else {
1190 t.org = q0;
1191 p0 = 0;
1193 p1 = q1 - t.org;
1194 if(p1 > t.f->nchars)
1195 p1 = t.f->nchars;
1196 frdelete(t.f, p0, p1);
1197 fill();
1199 updatesel();
1202 insert(r, n, t.qh, 1);
1206 void
1207 cut(void)
1209 uint n, p0, p1;
1210 uint q0, q1;
1212 q0 = t.q0;
1213 q1 = t.q1;
1215 if (q0 < t.org && q1 >= t.org)
1216 show(q0);
1218 n = q1-q0;
1219 if(n == 0)
1220 return;
1221 runemove(t.r+q0, t.r+q1, t.nr-q1);
1222 t.nr -= n;
1223 t.q0 = t.q1 = q0;
1224 if(q1 < t.qh)
1225 t.qh -= n;
1226 else if(q0 < t.qh)
1227 t.qh = q0;
1228 if(q1 < t.org)
1229 t.org -= n;
1230 else if(q0 < t.org+t.f->nchars){
1231 assert(q0 >= t.org);
1232 p0 = q0 - t.org;
1233 p1 = q1 - t.org;
1234 if(p1 > t.f->nchars)
1235 p1 = t.f->nchars;
1236 frdelete(t.f, p0, p1);
1237 fill();
1239 updatesel();
1242 void
1243 snarfupdate(void)
1245 char *pp;
1246 int n, i;
1247 Rune *p;
1249 pp = getsnarf();
1250 if(pp == nil)
1251 return;
1252 n = strlen(pp);
1253 if(n <= 0) {
1254 /*t.nsnarf = 0;*/
1255 return;
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];
1265 void
1266 snarf(void)
1268 char *p;
1269 int i, n;
1270 Rune *rp;
1272 if(t.q1 == t.q0)
1273 return;
1274 n = t.q1-t.q0;
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;
1281 *p = '\0';
1282 putsnarf(sbuf);
1285 uint
1286 min(uint x, uint y)
1288 if(x < y)
1289 return x;
1290 return y;
1293 uint
1294 max(uint x, uint y)
1296 if(x > y)
1297 return x;
1298 return y;
1301 uint
1302 insert(Rune *r, int n, uint q0, int hostwrite)
1304 uint m;
1306 if(n == 0)
1307 return q0;
1308 if(t.nr+n>HiWater && q0>=t.org && q0>=t.qh){
1309 m = min(HiWater-LoWater, min(t.org, t.qh));
1310 t.org -= m;
1311 t.qh -= m;
1312 if(t.q0 > m)
1313 t.q0 -= m;
1314 else
1315 t.q0 = 0;
1316 if(t.q1 > m)
1317 t.q1 -= m;
1318 else
1319 t.q1 = 0;
1320 t.nr -= m;
1321 runemove(t.r, t.r+m, t.nr);
1322 q0 -= m;
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;
1332 if(m > HiWater)
1333 m = max(HiWater+MinWater, t.nr+n);
1334 if(m > t.maxr){
1335 t.r = runerealloc(t.r, m);
1336 t.maxr = m;
1339 runemove(t.r+q0+n, t.r+q0, t.nr-q0);
1340 runemove(t.r+q0, r, n);
1341 t.nr += n;
1342 /* if output touches, advance selection, not qh; works best for keyboard and output */
1343 if(q0 <= t.q1)
1344 t.q1 += n;
1345 if(q0 <= t.q0)
1346 t.q0 += n;
1347 if(q0 < t.qh || (q0==t.qh && hostwrite))
1348 t.qh += n;
1349 else
1350 consread();
1351 if(q0 < t.org)
1352 t.org += n;
1353 else if(q0 <= t.org+t.f->nchars)
1354 frinsert(t.f, r, r+n, q0-t.org);
1355 return q0;
1358 void
1359 paste(Rune *r, int n, int advance)
1361 Rune *rbuf;
1363 if(rawon() && t.q0==t.nr){
1364 addraw(r, n);
1365 return;
1368 cut();
1369 if(n == 0)
1370 return;
1373 * if this is a button2 execute then we might have been passed
1374 * runes inside the buffer. must save them before realloc.
1376 rbuf = nil;
1377 if(t.r <= r && r < t.r+n){
1378 rbuf = runemalloc(n);
1379 runemove(rbuf, r, n);
1380 r = rbuf;
1383 insert(r, n, t.q0, 0);
1384 updatesel();
1385 free(rbuf);
1388 void
1389 fill(void)
1391 if (t.f->nlines >= t.f->maxlines)
1392 return;
1393 frinsert(t.f, t.r + t.org + t.f->nchars, t.r + t.nr, t.f->nchars);
1396 void
1397 updatesel(void)
1399 Frame *f;
1400 uint n;
1402 f = t.f;
1403 if(t.org+f->p0 == t.q0 && t.org+f->p1 == t.q1)
1404 return;
1406 n = t.f->nchars;
1408 frdrawsel(f, frptofchar(f, f->p0), f->p0, f->p1, 0);
1409 if (t.q0 >= t.org)
1410 f->p0 = t.q0-t.org;
1411 else
1412 f->p0 = 0;
1413 if(f->p0 > n)
1414 f->p0 = n;
1415 if (t.q1 >= t.org)
1416 f->p1 = t.q1-t.org;
1417 else
1418 f->p1 = 0;
1419 if(f->p1 > n)
1420 f->p1 = n;
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;
1428 tcheck();
1431 void
1432 show(uint q0)
1434 int nl;
1435 uint q, oq;
1437 if(cansee(q0))
1438 return;
1440 if (q0<t.org)
1441 nl = t.f->maxlines/5;
1442 else
1443 nl = 4*t.f->maxlines/5;
1444 q = backnl(q0, nl);
1445 /* avoid going in the wrong direction */
1446 if (q0>t.org && q<t.org)
1447 q = t.org;
1448 setorigin(q, 0);
1449 /* keep trying until q0 is on the screen */
1450 while(!cansee(q0)) {
1451 assert(q0 >= t.org);
1452 oq = q;
1453 q = line2q(t.f->maxlines-nl);
1454 assert(q > oq);
1455 setorigin(q, 1);
1459 int
1460 cansee(uint q0)
1462 uint qe;
1464 qe = t.org+t.f->nchars;
1466 if(q0>=t.org && q0 < qe)
1467 return 1;
1468 if (q0 != qe)
1469 return 0;
1470 if (t.f->nlines < t.f->maxlines)
1471 return 1;
1472 if (q0 > 0 && t.r[t.nr-1] == '\n')
1473 return 0;
1474 return 1;
1478 void
1479 setorigin(uint org, int exact)
1481 int i, a;
1482 uint n;
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')
1489 break;
1490 org++;
1493 a = org-t.org;
1495 if(a>=0 && a<t.f->nchars)
1496 frdelete(t.f, 0, a);
1497 else if(a<0 && -a<100*t.f->maxlines){
1498 n = t.org - org;
1499 frinsert(t.f, t.r+org, t.r+org+n, 0);
1500 }else
1501 frdelete(t.f, 0, t.f->nchars);
1502 t.org = org;
1503 fill();
1504 updatesel();
1508 uint
1509 line2q(uint n)
1511 Frame *f;
1513 f = t.f;
1514 return frcharofpt(f, Pt(f->r.min.x, f->r.min.y + n*font->height))+t.org;
1517 uint
1518 backnl(uint p, uint n)
1520 int i, j;
1522 for (i = n;; i--) {
1523 /* at 256 chars, call it a line anyway */
1524 for(j=256; --j>0 && p>0; p--)
1525 if(t.r[p-1]=='\n')
1526 break;
1527 if (p == 0 || i == 0)
1528 return p;
1529 p--;
1533 void
1534 addraw(Rune *r, int nr)
1536 t.raw = runerealloc(t.raw, t.nraw+nr);
1537 runemove(t.raw+t.nraw, r, nr);
1538 t.nraw += 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 };
1551 Rune *left[] = {
1552 left1,
1553 left2,
1554 left3,
1558 Rune *right[] = {
1559 right1,
1560 left2,
1561 left3,
1565 void
1566 doubleclick(uint *q0, uint *q1)
1568 int c, i;
1569 Rune *r, *l, *p;
1570 uint q;
1572 for(i=0; left[i]!=0; i++){
1573 q = *q0;
1574 l = left[i];
1575 r = right[i];
1576 /* try matching character to left, looking right */
1577 if(q == 0)
1578 c = '\n';
1579 else
1580 c = t.r[q-1];
1581 p = strrune(l, c);
1582 if(p != 0){
1583 if(clickmatch(c, r[p-l], 1, &q))
1584 *q1 = q-(c!='\n');
1585 return;
1587 /* try matching character to right, looking left */
1588 if(q == t.nr)
1589 c = '\n';
1590 else
1591 c = t.r[q];
1592 p = strrune(r, c);
1593 if(p != 0){
1594 if(clickmatch(c, l[p-r], -1, &q)){
1595 *q1 = *q0+(*q0<t.nr && c=='\n');
1596 *q0 = q;
1597 if(c!='\n' || q!=0 || t.r[0]=='\n')
1598 (*q0)++;
1600 return;
1603 /* try filling out word to right */
1604 while(*q1<t.nr && isalnum(t.r[*q1]))
1605 (*q1)++;
1606 /* try filling out word to left */
1607 while(*q0>0 && isalnum(t.r[*q0-1]))
1608 (*q0)--;
1611 int
1612 clickmatch(int cl, int cr, int dir, uint *q)
1614 Rune c;
1615 int nest;
1617 nest = 1;
1618 for(;;){
1619 if(dir > 0){
1620 if(*q == t.nr)
1621 break;
1622 c = t.r[*q];
1623 (*q)++;
1624 }else{
1625 if(*q == 0)
1626 break;
1627 (*q)--;
1628 c = t.r[*q];
1630 if(c == cr){
1631 if(--nest==0)
1632 return 1;
1633 }else if(c == cl)
1634 nest++;
1636 return cl=='\n' && nest==1;
1639 void
1640 tcheck(void)
1642 Frame *f;
1644 f = t.f;
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));
1653 Rune*
1654 strrune(Rune *s, Rune c)
1656 Rune c1;
1658 if(c == 0) {
1659 while(*s++)
1661 return s-1;
1664 while(c1 = *s++)
1665 if(c1 == c)
1666 return s-1;
1667 return 0;
1670 void
1671 scrdraw(void)
1673 Rectangle r, r1, r2;
1674 static Image *scrx;
1676 r = scrollr;
1677 r.min.x += 1; /* border between margin and bar */
1678 r1 = r;
1679 if(scrx==0 || scrx->r.max.y < r.max.y){
1680 if(scrx)
1681 freeimage(scrx);
1682 scrx = allocimage(display, Rect(0, 0, 32, r.max.y), screen->chan, 1, DPaleyellow);
1683 if(scrx == 0)
1684 sysfatal("scroll balloc");
1686 r1.min.x = 0;
1687 r1.max.x = Dx(r);
1688 r2 = scrpos(r1, t.org, t.org+t.f->nchars, t.nr);
1689 if(!eqrect(r2, lastsr)){
1690 lastsr = r2;
1691 draw(scrx, r1, cols[BORD], nil, ZP);
1692 draw(scrx, r2, cols[BACK], nil, r2.min);
1693 // r2 = r1;
1694 // r2.min.x = r2.max.x-1;
1695 // draw(scrx, r2, cols[BORD], nil, ZP);
1696 draw(screen, r, scrx, nil, r1.min);
1700 Rectangle
1701 scrpos(Rectangle r, ulong p0, ulong p1, ulong tot)
1703 long h;
1704 Rectangle q;
1706 q = insetrect(r, 1);
1707 h = q.max.y-q.min.y;
1708 if(tot == 0)
1709 return q;
1710 if(tot > 1024L*1024L)
1711 tot >>= 10, p0 >>= 10, p1 >>= 10;
1712 if(p0 > 0)
1713 q.min.y += h*p0/tot;
1714 if(p1 < 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;
1719 else
1720 q.min.y = q.max.y-2;
1722 return q;
1725 void
1726 scroll(int but)
1728 uint p0, oldp0;
1729 Rectangle s;
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;
1735 oldp0 = ~0;
1736 first = 1;
1737 do{
1738 if(t.m.xy.x<s.min.x || s.max.x<=t.m.xy.x){
1739 readmouse(mc);
1740 t.m = mc->m;
1741 }else{
1742 my = t.m.xy.y;
1743 if(my < s.min.y)
1744 my = s.min.y;
1745 if(my >= s.max.y)
1746 my = s.max.y;
1747 // if(!eqpt(t.m.xy, Pt(x, my)))
1748 // cursorset(Pt(x, my));
1749 exact = 1;
1750 if(but == 2){
1751 y = my;
1752 if(y > s.max.y-2)
1753 y = s.max.y-2;
1754 if(t.nr > 1024*1024)
1755 p0 = ((t.nr>>10)*(y-s.min.y)/h)<<10;
1756 else
1757 p0 = t.nr*(y-s.min.y)/h;
1758 exact = 0;
1759 } else if(but == 1)
1760 p0 = backnl(t.org, (my-s.min.y)/font->height);
1761 else
1762 p0 = t.org+frcharofpt(t.f, Pt(s.max.x, my));
1764 if(oldp0 != p0)
1765 setorigin(p0, exact);
1766 oldp0 = p0;
1767 scrdraw();
1768 readmouse(mc);
1769 t.m = mc->m;
1771 }while(t.m.buttons & (1<<(but-1)));
1774 void
1775 plumbstart(void)
1777 if((plumbfd = plumbopen("send", OWRITE)) < 0)
1778 fprint(2, "9term: plumbopen: %r\n");
1781 void
1782 plumb(uint q0, uint q1)
1784 Plumbmsg *pm;
1785 char *p;
1786 int i, p0, n;
1787 char cbuf[100];
1789 pm = malloc(sizeof(Plumbmsg));
1790 pm->src = strdup("9term");
1791 pm->dst = 0;
1792 pm->wdir = strdup(wdir);
1793 pm->type = strdup("text");
1794 pm->data = nil;
1795 if(q1 > q0)
1796 pm->attr = nil;
1797 else{
1798 p0 = q0;
1799 wordclick(&q0, &q1);
1800 sprint(cbuf, "click=%d", p0-q0);
1801 pm->attr = plumbunpackattr(cbuf);
1803 if(q0==q1){
1804 plumbfree(pm);
1805 return;
1807 pm->data = malloc(SnarfSize);
1808 n = q1 - q0;
1809 for(i=0,p=pm->data; i<n && p < pm->data + SnarfSize-UTFmax; i++)
1810 p += runetochar(p, t.r+q0+i);
1811 *p = '\0';
1812 pm->ndata = strlen(pm->data);
1813 if(plumbsend(plumbfd, pm) < 0){
1814 setcursor(mc, &query);
1815 sleep(500);
1816 if(holdon)
1817 setcursor(mc, &whitearrow);
1818 else
1819 setcursor(mc, nil);
1821 plumbfree(pm);
1825 * Process in-band messages about window title changes.
1826 * The messages are of the form:
1828 * \033];xxx\007
1830 * where xxx is the new directory. This format was chosen
1831 * because it changes the label on xterm windows.
1833 int
1834 label(Rune *sr, int n)
1836 Rune *sl, *el, *er, *r;
1838 er = sr+n;
1839 for(r=er-1; r>=sr; r--)
1840 if(*r == '\007')
1841 break;
1842 if(r < sr)
1843 return n;
1845 el = r+1;
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]==';')
1850 break;
1851 if(sl < sr)
1852 return n;
1854 snprint(wdir, sizeof wdir, "%.*S", (el-1)-(sl+3), sl+3);
1855 drawsetlabel(wdir);
1857 runemove(sl, el, er-el);
1858 n -= (el-sl);
1859 return n;
1862 int
1863 rawon(void)
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?
1873 char adir[100];
1874 int afd;
1876 void
1877 removethesocket(void)
1879 if(thesocket[0])
1880 if(remove(thesocket) < 0)
1881 fprint(2, "remove %s: %r\n", thesocket);
1884 void
1885 servedevtext(void)
1887 char buf[100];
1889 snprint(buf, sizeof buf, "unix!/tmp/9term-text.%d", getpid());
1891 if((afd = announce(buf, adir)) < 0){
1892 putenv("text9term", "");
1893 return;
1896 putenv("text9term", buf);
1897 proccreate(listenproc, nil, STACK);
1898 strcpy(thesocket, buf+5);
1899 atexit(removethesocket);
1902 void
1903 listenproc(void *arg)
1905 int fd;
1906 char dir[100];
1908 USED(arg);
1909 for(;;){
1910 fd = listen(adir, dir);
1911 if(fd < 0){
1912 close(afd);
1913 return;
1915 proccreate(textthread, (void*)fd, STACK);
1919 void
1920 textthread(void *arg)
1922 int fd, i, x, n, end;
1923 Rune r;
1924 char buf[4096], *p, *ep;
1926 fd = (int)arg;
1927 p = buf;
1928 ep = buf+sizeof buf;
1929 end = t.org+t.nr; /* avoid possible output loop */
1930 for(i=t.org;; i++){
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)
1934 goto break2;
1936 if(i >= end)
1937 break;
1938 p = buf;
1940 if(i < t.org)
1941 i = t.org;
1942 r = t.r[i-t.org];
1943 if(r < Runeself)
1944 *p++ = r;
1945 else
1946 p += runetochar(p, &r);
1948 break2:
1949 close(fd);