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 int
308 isexpand(Rune r)
310 return r=='_' || ('0' <= r && r <= '9')
311 || ('a' <= r && r <= 'z')
312 || ('A' <= r && r <= 'Z');
315 void
316 hangupnote(void *a, char *msg)
318 if(getpid() != mainpid)
319 noted(NDFLT);
320 if(strcmp(msg, "hangup") == 0 && rcpid != 0){
321 postnote(PNGROUP, rcpid, "hangup");
322 noted(NDFLT);
324 if(strstr(msg, "child")){
325 char buf[128];
326 int n;
328 n = awaitnohang(buf, sizeof buf-1);
329 if(n > 0){
330 buf[n] = 0;
331 if(atoi(buf) == rcpid)
332 threadexitsall(0);
334 noted(NCONT);
336 noted(NDFLT);
339 void
340 hostproc(void *arg)
342 Channel *c;
343 int i, n, which;
345 c = arg;
347 i = 0;
348 for(;;){
349 /* Let typing have a go -- maybe there's a rubout waiting. */
350 yield();
352 i = 1-i; /* toggle */
353 n = read(rcfd, rcbuf[i].data, sizeof rcbuf[i].data);
354 if(n <= 0){
355 if(n < 0)
356 fprint(2, "9term: host read error: %r\n");
357 threadexitsall("host");
359 rcbuf[i].n = n;
360 which = i;
361 send(c, &which);
365 void
366 hoststart(void)
368 hostc = chancreate(sizeof(int), 0);
369 proccreate(hostproc, hostc, 32*1024);
372 void
373 loop(void)
375 Rune r;
376 int i;
377 Alt a[5];
379 a[0].c = mc->c;
380 a[0].v = &mc->m;
381 a[0].op = CHANRCV;
383 a[1].c = kc->c;
384 a[1].v = &r;
385 a[1].op = CHANRCV;
387 a[2].c = hostc;
388 a[2].v = &i;
389 a[2].op = CHANRCV;
391 a[3].c = mc->resizec;
392 a[3].v = nil;
393 a[3].op = CHANRCV;
395 a[4].c = nil;
396 a[4].v = nil;
397 a[4].op = CHANEND;
399 for(;;) {
400 tcheck();
402 scrdraw();
403 flushimage(display, 1);
404 a[2].op = CHANRCV;
405 if(!scrolling && t.qh > t.org+t.f->nchars)
406 a[2].op = CHANNOP;;
407 switch(alt(a)) {
408 default:
409 sysfatal("impossible");
410 case 0:
411 t.m = mc->m;
412 mouse();
413 break;
414 case 1:
415 key(r);
416 break;
417 case 2:
418 conswrite((char*)rcbuf[i].data, rcbuf[i].n);
419 break;
420 case 3:
421 doreshape();
422 break;
427 void
428 doreshape(void)
430 if(getwindow(display, Refnone) < 0)
431 sysfatal("can't reattach to window");
432 draw(screen, screen->r, cols[BACK], nil, ZP);
433 geom();
434 scrdraw();
437 void
438 geom(void)
440 Point p;
441 Rectangle r;
443 if(!acmecolors){
444 if(_windowhasfocus){
445 cols[TEXT] = cols[HTEXT] = display->black;
446 hcols[TEXT] = hcols[HTEXT] = blue;
447 }else{
448 cols[TEXT] = cols[HTEXT] = palegrey;
449 hcols[TEXT] = hcols[HTEXT] = paleblue;
453 r = screen->r;
454 r.min.y++;
455 r.max.y--;
457 scrollr = r;
458 scrollr.max.x = r.min.x+Scrollwid;
459 lastsr = Rect(0,0,0,0);
461 r.min.x += Scrollwid+Scrollgap;
463 frclear(t.f, 0);
464 frinit(t.f, r, font, screen, holdon ? hcols : cols);
465 t.f->maxtab = maxtab*stringwidth(font, "0");
466 fill();
467 updatesel();
469 p = stringsize(font, "0");
470 if(p.x == 0 || p.y == 0)
471 return;
473 updatewinsize(Dy(r)/p.y, Dx(r)/p.x, Dx(r), Dy(r));
476 void
477 drawhold(int holdon)
479 if(holdon)
480 setcursor(mc, &whitearrow);
481 else
482 setcursor(mc, nil);
484 draw(screen, screen->r, cols[BACK], nil, ZP);
485 geom();
486 scrdraw();
489 void
490 wordclick(uint *q0, uint *q1)
492 while(*q1<t.nr && !isspace(t.r[*q1]))
493 (*q1)++;
494 while(*q0>0 && !isspace(t.r[*q0-1]))
495 (*q0)--;
498 int
499 aselect(uint *q0, uint *q1, Image *color)
501 int cancel;
502 uint oldq0, oldq1, newq0, newq1;
504 /* save old selection */
505 oldq0 = t.q0;
506 oldq1 = t.q1;
508 /* sweep out area and record it */
509 t.f->cols[HIGH] = color;
510 t.f->cols[HTEXT] = display->white;
511 mselect();
512 newq0 = t.q0;
513 newq1 = t.q1;
515 cancel = 0;
516 if(t.m.buttons != 0){
517 while(t.m.buttons){
518 readmouse(mc);
519 t.m = mc->m;
521 cancel = 1;
524 /* restore old selection */
525 t.f->cols[HIGH] = cols[HIGH];
526 t.f->cols[HTEXT] = cols[HTEXT];
527 t.q0 = oldq0;
528 t.q1 = oldq1;
529 updatesel();
531 if(cancel)
532 return -1;
534 /* selected a region */
535 if(newq0 < newq1){
536 *q0 = newq0;
537 *q1 = newq1;
538 return 0;
541 /* clicked inside previous selection */
542 /* the "<=" in newq0 <= oldq1 allows us to click the right edge */
543 if(oldq0 <= newq0 && newq0 <= oldq1){
544 *q0 = oldq0;
545 *q1 = oldq1;
546 return 0;
549 /* just a click */
550 *q0 = newq0;
551 *q1 = newq1;
552 return 0;
555 static Rune Lnl[1] = { '\n' };
557 void
558 mouse(void)
560 int but;
561 uint q0, q1;
563 but = t.m.buttons;
565 if(but != 1 && but != 2 && but != 4 && but != 8 && but != 16)
566 return;
568 if (ptinrect(t.m.xy, scrollr)) {
569 scroll(but);
570 if(t.qh<=t.org+t.f->nchars)
571 consread();
572 return;
575 switch(but) {
576 case 1:
577 mselect();
578 break;
579 case 2:
580 if(button2exec){
581 if(aselect(&q0, &q1, execcolor) >= 0){
582 if(q0 == q1)
583 wordclick(&q0, &q1);
584 if(q0 == q1)
585 break;
586 t.q0 = t.q1 = t.nr;
587 updatesel();
588 paste(t.r+q0, q1-q0, 1);
589 if(t.r[q1-1] != '\n')
590 paste(Lnl, 1, 1);
592 break;
594 domenu2(2);
595 break;
596 case 4:
597 bouncemouse(&t.m);
598 break;
599 /*
600 if(aselect(&q0, &q1, plumbcolor) >= 0)
601 plumb(q0, q1);
602 break;
603 */
604 case 8:
605 scrollup(mousescrollsize(t.f->maxlines));
606 break;
607 case 16:
608 scrolldown(mousescrollsize(t.f->maxlines));
609 break;
613 void
614 mselect(void)
616 int b, x, y;
617 uint q0;
619 b = t.m.buttons;
620 q0 = frcharofpt(t.f, t.m.xy) + t.org;
621 if(t.m.msec-clickmsec<500 && clickq0==q0 && t.q0==t.q1 && b==1){
622 doubleclick(&t.q0, &t.q1);
623 updatesel();
624 /* t.t.i->flush(); */
625 x = t.m.xy.x;
626 y = t.m.xy.y;
627 /* stay here until something interesting happens */
628 do {
629 readmouse(mc);
630 t.m = mc->m;
631 } while(t.m.buttons==b && abs(t.m.xy.x-x)<4 && abs(t.m.xy.y-y)<4);
632 t.m.xy.x = x; /* in case we're calling frselect */
633 t.m.xy.y = y;
634 clickmsec = 0;
637 if(t.m.buttons == b) {
638 frselect(t.f, mc);
639 t.m = mc->m;
640 t.q0 = t.f->p0 + t.org;
641 t.q1 = t.f->p1 + t.org;
642 clickmsec = t.m.msec;
643 clickq0 = t.q0;
645 if((t.m.buttons != b) &&(b&1)){
646 enum{Cancut = 1, Canpaste = 2} state = Cancut | Canpaste;
647 while(t.m.buttons){
648 if(t.m.buttons&2){
649 if(state&Cancut){
650 snarf();
651 cut();
652 state = Canpaste;
654 }else if(t.m.buttons&4){
655 if(state&Canpaste){
656 snarfupdate();
657 if(t.nsnarf){
658 paste(t.snarf, t.nsnarf, 0);
660 state = Cancut;
663 readmouse(mc);
664 t.m = mc->m;
669 Rune newline[] = { '\n', 0 };
671 void
672 domenu2(int but)
674 if(scrolling)
675 menu2str[Scroll] = "+ scroll";
676 else
677 menu2str[Scroll] = "- scroll";
678 if(cooked)
679 menu2str[Cooked] = "+ mustecho";
680 else
681 menu2str[Cooked] = "- mustecho";
683 switch(menuhit(but, mc, &menu2, nil)){
684 case -1:
685 break;
686 case Cut:
687 snarf();
688 cut();
689 if(scrolling)
690 show(t.q0);
691 break;
692 case Paste:
693 snarfupdate();
694 paste(t.snarf, t.nsnarf, 0);
695 if(scrolling)
696 show(t.q0);
697 break;
698 case Snarf:
699 snarf();
700 if(scrolling)
701 show(t.q0);
702 break;
703 case Send:
704 if(t.q0 != t.q1)
705 snarf();
706 else
707 snarfupdate();
708 t.q0 = t.q1 = t.nr;
709 updatesel();
710 paste(t.snarf, t.nsnarf, 1);
711 if(t.nsnarf == 0 || t.snarf[t.nsnarf-1] != '\n')
712 paste(newline, 1, 1);
713 show(t.nr);
714 consread();
715 break;
716 case Scroll:
717 scrolling = !scrolling;
718 if (scrolling) {
719 show(t.nr);
720 consread();
722 break;
723 case Plumb:
724 plumb(t.q0, t.q1);
725 break;
726 case Cooked:
727 cooked = !cooked;
728 break;
729 default:
730 sysfatal("bad menu item");
734 int
735 windfilewidth(uint q0, int oneelement)
737 uint q;
738 Rune r;
740 q = q0;
741 while(q > 0){
742 r = t.r[q-1];
743 if(r<=' ')
744 break;
745 if(oneelement && r=='/')
746 break;
747 --q;
749 return q0-q;
752 void
753 showcandidates(Completion *c)
755 int i;
756 Fmt f;
757 Rune *rp;
758 uint nr, qline, q0;
759 char *s;
761 runefmtstrinit(&f);
762 if (c->nmatch == 0)
763 s = "[no matches in ";
764 else
765 s = "[";
766 if(c->nfile > 32)
767 fmtprint(&f, "%s%d files]\n", s, c->nfile);
768 else{
769 fmtprint(&f, "%s", s);
770 for(i=0; i<c->nfile; i++){
771 if(i > 0)
772 fmtprint(&f, " ");
773 fmtprint(&f, "%s", c->filename[i]);
775 fmtprint(&f, "]\n");
777 /* place text at beginning of line before host point */
778 qline = t.qh;
779 while(qline>0 && t.r[qline-1] != '\n')
780 qline--;
782 rp = runefmtstrflush(&f);
783 nr = runestrlen(rp);
785 q0 = t.q0;
786 q0 += insert(rp, nr, qline, 0) - qline;
787 free(rp);
788 t.q0 = q0+nr;
789 t.q1 = q0+nr;
790 updatesel();
793 Rune*
794 namecomplete(void)
796 int nstr, npath;
797 Rune *rp, *path, *str;
798 Completion *c;
799 char *s, *dir, *root;
801 /* control-f: filename completion; works back to white space or / */
802 if(t.q0<t.nr && t.r[t.q0]>' ') /* must be at end of word */
803 return nil;
804 nstr = windfilewidth(t.q0, TRUE);
805 str = runemalloc(nstr);
806 runemove(str, t.r+(t.q0-nstr), nstr);
807 npath = windfilewidth(t.q0-nstr, FALSE);
808 path = runemalloc(npath);
809 runemove(path, t.r+(t.q0-nstr-npath), npath);
810 rp = nil;
812 /* is path rooted? if not, we need to make it relative to window path */
813 if(npath>0 && path[0]=='/'){
814 dir = malloc(UTFmax*npath+1);
815 sprint(dir, "%.*S", npath, path);
816 }else{
817 if(strcmp(wdir, "") == 0)
818 root = ".";
819 else
820 root = wdir;
821 dir = malloc(strlen(root)+1+UTFmax*npath+1);
822 sprint(dir, "%s/%.*S", root, npath, path);
824 dir = cleanname(dir);
826 s = smprint("%.*S", nstr, str);
827 c = complete(dir, s);
828 free(s);
829 if(c == nil)
830 goto Return;
832 if(!c->advance)
833 showcandidates(c);
835 if(c->advance)
836 rp = runesmprint("%s", c->string);
838 Return:
839 freecompletion(c);
840 free(dir);
841 free(path);
842 free(str);
843 return rp;
846 void
847 scrollup(int n)
849 setorigin(backnl(t.org, n), 1);
852 void
853 scrolldown(int n)
855 setorigin(line2q(n), 1);
856 if(t.qh<=t.org+t.f->nchars)
857 consread();
860 void
861 key(Rune r)
863 Rune *rp;
864 int nr;
866 if(r == 0)
867 return;
868 switch(r){
869 case Kpgup:
870 scrollup(t.f->maxlines*2/3);
871 return;
872 case Kpgdown:
873 scrolldown(t.f->maxlines*2/3);
874 return;
875 case Kup:
876 scrollup(t.f->maxlines/3);
877 return;
878 case Kdown:
879 scrolldown(t.f->maxlines/3);
880 return;
881 case Kleft:
882 if(t.q0 > 0){
883 t.q0--;
884 t.q1 = t.q0;
885 updatesel();
886 show(t.q0);
888 return;
889 case Kright:
890 if(t.q1 < t.nr){
891 t.q1++;
892 t.q0 = t.q1;
893 updatesel();
894 show(t.q1);
896 return;
897 case Khome:
898 show(0);
899 return;
900 case Kend:
901 case 0x05:
902 show(t.nr);
903 return;
905 /*
906 * Non-standard extensions.
907 */
908 case CUT:
909 snarf();
910 cut();
911 if(scrolling)
912 show(t.q0);
913 return;
914 case COPY:
915 snarf();
916 if(scrolling)
917 show(t.q0);
918 return;
919 case PASTE:
920 snarfupdate();
921 paste(t.snarf, t.nsnarf, 0);
922 if(scrolling)
923 show(t.q0);
924 return;
927 if(rawon() && t.q0 == t.nr){
928 addraw(&r, 1);
929 consread();
930 return;
933 if(r == 0x7F){ /* DEL: send interrupt; what a mess */
934 if(holdon){
935 holdon = 0;
936 drawhold(holdon);
938 t.qh = t.q0 = t.q1 = t.nr;
939 show(t.q0);
940 write(rcfd, "\x7F", 1);
941 return;
944 if(r==ESC){ /* toggle hold */
945 holdon = !holdon;
946 drawhold(holdon);
947 /* replaceintegerproperty("_9WM_HOLD_MODE", 1, 32, holdon); */
948 if(!holdon)
949 consread();
950 return;
953 snarf();
955 switch(r) {
956 case 0x06: /* ^F: file name completion */
957 case Kins: /* Insert: file name completion */
958 rp = namecomplete();
959 if(rp == nil)
960 return;
961 nr = runestrlen(rp);
962 paste(rp, nr, 1);
963 free(rp);
964 return;
965 case 0x08: /* ^H: erase character */
966 case 0x15: /* ^U: erase line */
967 case 0x17: /* ^W: erase word */
968 if (t.q0 != 0 && t.q0 != t.qh)
969 t.q0 -= bswidth(r, t.q0, 1);
970 cut();
971 break;
972 default:
973 paste(&r, 1, 1);
974 break;
976 if(scrolling)
977 show(t.q0);
980 int
981 bswidth(Rune c, uint start, int eatnl)
983 uint q, eq, stop;
984 Rune r;
985 int skipping;
987 /* there is known to be at least one character to erase */
988 if(c == 0x08) /* ^H: erase character */
989 return 1;
990 q = start;
991 stop = 0;
992 if(q > t.qh)
993 stop = t.qh;
994 skipping = 1;
995 while(q > stop){
996 r = t.r[q-1];
997 if(r == '\n'){ /* eat at most one more character */
998 if(q == start && eatnl) /* eat the newline */
999 --q;
1000 break;
1002 if(c == 0x17){
1003 eq = isexpand(r);
1004 if(eq && skipping) /* found one; stop skipping */
1005 skipping = 0;
1006 else if(!eq && !skipping)
1007 break;
1009 --q;
1011 return start-q;
1014 int
1015 consready(void)
1017 int i, c;
1019 if(holdon)
1020 return 0;
1022 if(rawon())
1023 return t.nraw != 0;
1025 /* look to see if there is a complete line */
1026 for(i=t.qh; i<t.nr; i++){
1027 c = t.r[i];
1028 if(c=='\n' || c=='\004' || c=='\x7F')
1029 return 1;
1031 return 0;
1035 void
1036 consread(void)
1038 char buf[8000], *p;
1039 int c, width, n;
1040 int s, raw;
1042 raw = rawon();
1043 for(;;) {
1044 if(!consready())
1045 return;
1046 n = sizeof(buf);
1047 p = buf;
1048 c = 0;
1049 while(n >= UTFmax && (t.qh<t.nr || t.nraw > 0)) {
1050 if(t.qh == t.nr){
1051 width = runetochar(p, &t.raw[0]);
1052 t.nraw--;
1053 runemove(t.raw, t.raw+1, t.nraw);
1054 }else
1055 width = runetochar(p, &t.r[t.qh++]);
1056 c = *p;
1057 p += width;
1058 n -= width;
1059 if(!raw && (c == '\n' || c == '\004' || c == '\x7F'))
1060 break;
1062 n = p-buf;
1065 * If we've been echoing, make sure the terminal isn't
1066 * while we do the write. This screws up if someone
1067 * else tries to turn off echo at the same time we do
1068 * (we'll turn it on again after the write), but that's not
1069 * too likely.
1071 s = setecho(sfd, 0);
1072 if(write(rcfd, buf, n) < 0)
1073 threadexitsall(0);
1074 if(s)
1075 setecho(sfd, s);
1079 void
1080 conswrite(char *p, int n)
1082 int n2, i;
1083 Rune buf2[1000], *q;
1085 /* convert to runes */
1086 i = t.npart;
1087 if(i > 0){
1088 /* handle partial runes */
1089 while(i < UTFmax && n>0) {
1090 t.part[i] = *p;
1091 i++;
1092 p++;
1093 n--;
1094 if(fullrune(t.part, i)) {
1095 t.npart = 0;
1096 chartorune(buf2, t.part);
1097 runewrite(buf2, 1);
1098 break;
1101 /* there is a little extra room in a message buf */
1104 while(n >= UTFmax || fullrune(p, n)) {
1105 n2 = nelem(buf2);
1106 q = buf2;
1108 while(n2) {
1109 if(n < UTFmax && !fullrune(p, n))
1110 break;
1111 i = chartorune(q, p);
1112 p += i;
1113 n -= i;
1114 n2--;
1115 q++;
1117 runewrite(buf2, q-buf2);
1120 if(n != 0) {
1121 assert(n+t.npart < UTFmax);
1122 memcpy(t.part+t.npart, p, n);
1123 t.npart += n;
1126 if(scrolling)
1127 show(t.qh);
1130 void
1131 runewrite(Rune *r, int n)
1133 int i;
1134 uint initial;
1135 uint q0, q1;
1136 uint p0, p1;
1137 Rune *p, *q;
1139 n = label(r, n);
1140 if(n == 0)
1141 return;
1143 /* get rid of backspaces */
1144 initial = 0;
1145 p = q = r;
1146 for(i=0; i<n; i++) {
1147 if(*p == '\b') {
1148 if(q == r)
1149 initial++;
1150 else
1151 --q;
1152 } else if(*p == '\r') { /* treat like ^U */
1153 /* convert CR without NL into erased line */
1154 /* i feel really sleazy about this but it helps */
1155 while(i<n-1 && *(p+1) == '\r'){
1156 i++;
1157 p++;
1159 if(i<n-1 && *(p+1) != '\n'){
1160 while(q > r && *(q-1) != '\n')
1161 q--;
1162 if(q==r)
1163 initial = bswidth(0x15, t.qh, 0);
1165 } else if(*p)
1166 *q++ = *p;
1167 p++;
1169 n = q-r;
1171 if(initial){
1172 /* write turned into a delete */
1174 if(initial > t.qh)
1175 initial = t.qh;
1176 q0 = t.qh-initial;
1177 q1 = t.qh;
1179 runemove(t.r+q0, t.r+q1, t.nr-q1);
1180 t.nr -= initial;
1181 t.qh -= initial;
1182 if(t.q0 > q1)
1183 t.q0 -= initial;
1184 else if(t.q0 > q0)
1185 t.q0 = q0;
1186 if(t.q1 > q1)
1187 t.q1 -= initial;
1188 else if(t.q1 > q0)
1189 t.q1 = q0;
1190 if(t.org > q1)
1191 t.org -= initial;
1192 else if(q0 < t.org+t.f->nchars){
1193 if(t.org < q0)
1194 p0 = q0 - t.org;
1195 else {
1196 t.org = q0;
1197 p0 = 0;
1199 p1 = q1 - t.org;
1200 if(p1 > t.f->nchars)
1201 p1 = t.f->nchars;
1202 frdelete(t.f, p0, p1);
1203 fill();
1205 updatesel();
1208 insert(r, n, t.qh, 1);
1212 void
1213 cut(void)
1215 uint n, p0, p1;
1216 uint q0, q1;
1218 q0 = t.q0;
1219 q1 = t.q1;
1221 if (q0 < t.org && q1 >= t.org)
1222 show(q0);
1224 n = q1-q0;
1225 if(n == 0)
1226 return;
1227 runemove(t.r+q0, t.r+q1, t.nr-q1);
1228 t.nr -= n;
1229 t.q0 = t.q1 = q0;
1230 if(q1 < t.qh)
1231 t.qh -= n;
1232 else if(q0 < t.qh)
1233 t.qh = q0;
1234 if(q1 < t.org)
1235 t.org -= n;
1236 else if(q0 < t.org+t.f->nchars){
1237 assert(q0 >= t.org);
1238 p0 = q0 - t.org;
1239 p1 = q1 - t.org;
1240 if(p1 > t.f->nchars)
1241 p1 = t.f->nchars;
1242 frdelete(t.f, p0, p1);
1243 fill();
1245 updatesel();
1248 void
1249 snarfupdate(void)
1251 char *pp;
1252 int n, i;
1253 Rune *p;
1255 pp = getsnarf();
1256 if(pp == nil)
1257 return;
1258 n = strlen(pp);
1259 if(n <= 0) {
1260 /*t.nsnarf = 0;*/
1261 return;
1263 t.snarf = runerealloc(t.snarf, n);
1264 for(i=0,p=t.snarf; i<n; p++)
1265 i += chartorune(p, pp+i);
1266 t.nsnarf = p-t.snarf;
1270 char sbuf[SnarfSize];
1271 void
1272 snarf(void)
1274 char *p;
1275 int i, n;
1276 Rune *rp;
1278 if(t.q1 == t.q0)
1279 return;
1280 n = t.q1-t.q0;
1281 t.snarf = runerealloc(t.snarf, n);
1282 for(i=0,p=sbuf,rp=t.snarf; i<n && p < sbuf+SnarfSize-UTFmax; i++){
1283 *rp++ = *(t.r+t.q0+i);
1284 p += runetochar(p, t.r+t.q0+i);
1286 t.nsnarf = rp-t.snarf;
1287 *p = '\0';
1288 putsnarf(sbuf);
1291 uint
1292 min(uint x, uint y)
1294 if(x < y)
1295 return x;
1296 return y;
1299 uint
1300 max(uint x, uint y)
1302 if(x > y)
1303 return x;
1304 return y;
1307 uint
1308 insert(Rune *r, int n, uint q0, int hostwrite)
1310 uint m;
1312 if(n == 0)
1313 return q0;
1314 if(t.nr+n>HiWater && q0>=t.org && q0>=t.qh){
1315 m = min(HiWater-LoWater, min(t.org, t.qh));
1316 t.org -= m;
1317 t.qh -= m;
1318 if(t.q0 > m)
1319 t.q0 -= m;
1320 else
1321 t.q0 = 0;
1322 if(t.q1 > m)
1323 t.q1 -= m;
1324 else
1325 t.q1 = 0;
1326 t.nr -= m;
1327 runemove(t.r, t.r+m, t.nr);
1328 q0 -= m;
1330 if(t.nr+n > t.maxr){
1332 * Minimize realloc breakage:
1333 * Allocate at least MinWater
1334 * Double allocation size each time
1335 * But don't go much above HiWater
1337 m = max(min(2*(t.nr+n), HiWater), t.nr+n)+MinWater;
1338 if(m > HiWater)
1339 m = max(HiWater+MinWater, t.nr+n);
1340 if(m > t.maxr){
1341 t.r = runerealloc(t.r, m);
1342 t.maxr = m;
1345 runemove(t.r+q0+n, t.r+q0, t.nr-q0);
1346 runemove(t.r+q0, r, n);
1347 t.nr += n;
1348 /* if output touches, advance selection, not qh; works best for keyboard and output */
1349 if(q0 <= t.q1)
1350 t.q1 += n;
1351 if(q0 <= t.q0)
1352 t.q0 += n;
1353 if(q0 < t.qh || (q0==t.qh && hostwrite))
1354 t.qh += n;
1355 else
1356 consread();
1357 if(q0 < t.org)
1358 t.org += n;
1359 else if(q0 <= t.org+t.f->nchars)
1360 frinsert(t.f, r, r+n, q0-t.org);
1361 return q0;
1364 void
1365 paste(Rune *r, int n, int advance)
1367 Rune *rbuf;
1369 if(rawon() && t.q0==t.nr){
1370 addraw(r, n);
1371 consread();
1372 return;
1375 cut();
1376 if(n == 0)
1377 return;
1380 * if this is a button2 execute then we might have been passed
1381 * runes inside the buffer. must save them before realloc.
1383 rbuf = nil;
1384 if(t.r <= r && r < t.r+n){
1385 rbuf = runemalloc(n);
1386 runemove(rbuf, r, n);
1387 r = rbuf;
1390 insert(r, n, t.q0, 0);
1391 updatesel();
1392 free(rbuf);
1395 void
1396 fill(void)
1398 if (t.f->nlines >= t.f->maxlines)
1399 return;
1400 frinsert(t.f, t.r + t.org + t.f->nchars, t.r + t.nr, t.f->nchars);
1403 void
1404 updatesel(void)
1406 Frame *f;
1407 uint n;
1409 f = t.f;
1410 if(t.org+f->p0 == t.q0 && t.org+f->p1 == t.q1)
1411 return;
1413 n = t.f->nchars;
1415 frdrawsel(f, frptofchar(f, f->p0), f->p0, f->p1, 0);
1416 if (t.q0 >= t.org)
1417 f->p0 = t.q0-t.org;
1418 else
1419 f->p0 = 0;
1420 if(f->p0 > n)
1421 f->p0 = n;
1422 if (t.q1 >= t.org)
1423 f->p1 = t.q1-t.org;
1424 else
1425 f->p1 = 0;
1426 if(f->p1 > n)
1427 f->p1 = n;
1428 frdrawsel(f, frptofchar(f, f->p0), f->p0, f->p1, 1);
1431 if(t.qh<=t.org+t.f.nchars && t.cwqueue != 0)
1432 t.cwqueue->wakeup <-= 0;
1435 tcheck();
1438 void
1439 show(uint q0)
1441 int nl;
1442 uint q, oq;
1444 if(cansee(q0))
1445 return;
1447 if (q0<t.org)
1448 nl = t.f->maxlines/5;
1449 else
1450 nl = 4*t.f->maxlines/5;
1451 q = backnl(q0, nl);
1452 /* avoid going in the wrong direction */
1453 if (q0>t.org && q<t.org)
1454 q = t.org;
1455 setorigin(q, 0);
1456 /* keep trying until q0 is on the screen */
1457 while(!cansee(q0)) {
1458 assert(q0 >= t.org);
1459 oq = q;
1460 q = line2q(t.f->maxlines-nl);
1461 assert(q > oq);
1462 setorigin(q, 1);
1466 int
1467 cansee(uint q0)
1469 uint qe;
1471 qe = t.org+t.f->nchars;
1473 if(q0>=t.org && q0 < qe)
1474 return 1;
1475 if (q0 != qe)
1476 return 0;
1477 if (t.f->nlines < t.f->maxlines)
1478 return 1;
1479 if (q0 > 0 && t.r[t.nr-1] == '\n')
1480 return 0;
1481 return 1;
1485 void
1486 setorigin(uint org, int exact)
1488 int i, a;
1489 uint n;
1491 if(org>0 && !exact){
1492 /* try and start after a newline */
1493 /* don't try harder than 256 chars */
1494 for(i=0; i<256 && org<t.nr; i++){
1495 if(t.r[org-1] == '\n')
1496 break;
1497 org++;
1500 a = org-t.org;
1502 if(a>=0 && a<t.f->nchars)
1503 frdelete(t.f, 0, a);
1504 else if(a<0 && -a<100*t.f->maxlines){
1505 n = t.org - org;
1506 frinsert(t.f, t.r+org, t.r+org+n, 0);
1507 }else
1508 frdelete(t.f, 0, t.f->nchars);
1509 t.org = org;
1510 fill();
1511 updatesel();
1515 uint
1516 line2q(uint n)
1518 Frame *f;
1520 f = t.f;
1521 return frcharofpt(f, Pt(f->r.min.x, f->r.min.y + n*font->height))+t.org;
1524 uint
1525 backnl(uint p, uint n)
1527 int i, j;
1529 for (i = n;; i--) {
1530 /* at 256 chars, call it a line anyway */
1531 for(j=256; --j>0 && p>0; p--)
1532 if(t.r[p-1]=='\n')
1533 break;
1534 if (p == 0 || i == 0)
1535 return p;
1536 p--;
1540 void
1541 addraw(Rune *r, int nr)
1543 t.raw = runerealloc(t.raw, t.nraw+nr);
1544 runemove(t.raw+t.nraw, r, nr);
1545 t.nraw += nr;
1547 if(t.crqueue != nil)
1548 t.crqueue->wakeup <-= 0;
1553 Rune left1[] = { '{', '[', '(', '<', 0xab, 0 };
1554 Rune right1[] = { '}', ']', ')', '>', 0xbb, 0 };
1555 Rune left2[] = { '\n', 0 };
1556 Rune left3[] = { '\'', '"', '`', 0 };
1558 Rune *left[] = {
1559 left1,
1560 left2,
1561 left3,
1565 Rune *right[] = {
1566 right1,
1567 left2,
1568 left3,
1572 void
1573 doubleclick(uint *q0, uint *q1)
1575 int c, i;
1576 Rune *r, *l, *p;
1577 uint q;
1579 for(i=0; left[i]!=0; i++){
1580 q = *q0;
1581 l = left[i];
1582 r = right[i];
1583 /* try matching character to left, looking right */
1584 if(q == 0)
1585 c = '\n';
1586 else
1587 c = t.r[q-1];
1588 p = strrune(l, c);
1589 if(p != 0){
1590 if(clickmatch(c, r[p-l], 1, &q))
1591 *q1 = q-(c!='\n');
1592 return;
1594 /* try matching character to right, looking left */
1595 if(q == t.nr)
1596 c = '\n';
1597 else
1598 c = t.r[q];
1599 p = strrune(r, c);
1600 if(p != 0){
1601 if(clickmatch(c, l[p-r], -1, &q)){
1602 *q1 = *q0+(*q0<t.nr && c=='\n');
1603 *q0 = q;
1604 if(c!='\n' || q!=0 || t.r[0]=='\n')
1605 (*q0)++;
1607 return;
1610 /* try filling out word to right */
1611 while(*q1<t.nr && isexpand(t.r[*q1]))
1612 (*q1)++;
1613 /* try filling out word to left */
1614 while(*q0>0 && isexpand(t.r[*q0-1]))
1615 (*q0)--;
1618 int
1619 clickmatch(int cl, int cr, int dir, uint *q)
1621 Rune c;
1622 int nest;
1624 nest = 1;
1625 for(;;){
1626 if(dir > 0){
1627 if(*q == t.nr)
1628 break;
1629 c = t.r[*q];
1630 (*q)++;
1631 }else{
1632 if(*q == 0)
1633 break;
1634 (*q)--;
1635 c = t.r[*q];
1637 if(c == cr){
1638 if(--nest==0)
1639 return 1;
1640 }else if(c == cl)
1641 nest++;
1643 return cl=='\n' && nest==1;
1646 void
1647 tcheck(void)
1649 Frame *f;
1651 f = t.f;
1653 assert(t.q0 <= t.q1 && t.q1 <= t.nr);
1654 assert(t.org <= t.nr && t.qh <= t.nr);
1655 assert(f->p0 <= f->p1 && f->p1 <= f->nchars);
1656 assert(t.org + f->nchars <= t.nr);
1657 assert(t.org+f->nchars==t.nr || (f->nlines >= f->maxlines));
1660 Rune*
1661 strrune(Rune *s, Rune c)
1663 Rune c1;
1665 if(c == 0) {
1666 while(*s++)
1668 return s-1;
1671 while(c1 = *s++)
1672 if(c1 == c)
1673 return s-1;
1674 return 0;
1677 void
1678 scrdraw(void)
1680 Rectangle r, r1, r2;
1681 static Image *scrx;
1683 r = scrollr;
1684 r.min.x += 1; /* border between margin and bar */
1685 r1 = r;
1686 if(scrx==0 || scrx->r.max.y < r.max.y){
1687 if(scrx)
1688 freeimage(scrx);
1689 scrx = allocimage(display, Rect(0, 0, 32, r.max.y), screen->chan, 1, DPaleyellow);
1690 if(scrx == 0)
1691 sysfatal("scroll balloc");
1693 r1.min.x = 0;
1694 r1.max.x = Dx(r);
1695 r2 = scrpos(r1, t.org, t.org+t.f->nchars, t.nr);
1696 if(!eqrect(r2, lastsr)){
1697 lastsr = r2;
1698 draw(scrx, r1, cols[BORD], nil, ZP);
1699 draw(scrx, r2, cols[BACK], nil, r2.min);
1700 // r2 = r1;
1701 // r2.min.x = r2.max.x-1;
1702 // draw(scrx, r2, cols[BORD], nil, ZP);
1703 draw(screen, r, scrx, nil, r1.min);
1707 Rectangle
1708 scrpos(Rectangle r, ulong p0, ulong p1, ulong tot)
1710 long h;
1711 Rectangle q;
1713 q = insetrect(r, 1);
1714 h = q.max.y-q.min.y;
1715 if(tot == 0)
1716 return q;
1717 if(tot > 1024L*1024L)
1718 tot >>= 10, p0 >>= 10, p1 >>= 10;
1719 if(p0 > 0)
1720 q.min.y += h*p0/tot;
1721 if(p1 < tot)
1722 q.max.y -= h*(tot-p1)/tot;
1723 if(q.max.y < q.min.y+2){
1724 if(q.min.y+2 <= r.max.y)
1725 q.max.y = q.min.y+2;
1726 else
1727 q.min.y = q.max.y-2;
1729 return q;
1732 void
1733 scroll(int but)
1735 uint p0, oldp0;
1736 Rectangle s;
1737 int x, y, my, h, first, exact;
1739 s = insetrect(scrollr, 1);
1740 h = s.max.y-s.min.y;
1741 x = (s.min.x+s.max.x)/2;
1742 oldp0 = ~0;
1743 first = 1;
1744 do{
1745 if(t.m.xy.x<s.min.x || s.max.x<=t.m.xy.x){
1746 readmouse(mc);
1747 t.m = mc->m;
1748 }else{
1749 my = t.m.xy.y;
1750 if(my < s.min.y)
1751 my = s.min.y;
1752 if(my >= s.max.y)
1753 my = s.max.y;
1754 // if(!eqpt(t.m.xy, Pt(x, my)))
1755 // cursorset(Pt(x, my));
1756 exact = 1;
1757 if(but == 2){
1758 y = my;
1759 if(y > s.max.y-2)
1760 y = s.max.y-2;
1761 if(t.nr > 1024*1024)
1762 p0 = ((t.nr>>10)*(y-s.min.y)/h)<<10;
1763 else
1764 p0 = t.nr*(y-s.min.y)/h;
1765 exact = 0;
1766 } else if(but == 1)
1767 p0 = backnl(t.org, (my-s.min.y)/font->height);
1768 else
1769 p0 = t.org+frcharofpt(t.f, Pt(s.max.x, my));
1771 if(oldp0 != p0)
1772 setorigin(p0, exact);
1773 oldp0 = p0;
1774 scrdraw();
1775 readmouse(mc);
1776 t.m = mc->m;
1778 }while(t.m.buttons & (1<<(but-1)));
1781 void
1782 plumbstart(void)
1784 if((plumbfd = plumbopen("send", OWRITE)) < 0)
1785 fprint(2, "9term: plumbopen: %r\n");
1788 void
1789 plumb(uint q0, uint q1)
1791 Plumbmsg *pm;
1792 char *p;
1793 int i, p0, n;
1794 char cbuf[100];
1796 pm = malloc(sizeof(Plumbmsg));
1797 pm->src = strdup("9term");
1798 pm->dst = 0;
1799 pm->wdir = strdup(wdir);
1800 pm->type = strdup("text");
1801 pm->data = nil;
1802 if(q1 > q0)
1803 pm->attr = nil;
1804 else{
1805 p0 = q0;
1806 wordclick(&q0, &q1);
1807 sprint(cbuf, "click=%d", p0-q0);
1808 pm->attr = plumbunpackattr(cbuf);
1810 if(q0==q1){
1811 plumbfree(pm);
1812 return;
1814 pm->data = malloc(SnarfSize);
1815 n = q1 - q0;
1816 for(i=0,p=pm->data; i<n && p < pm->data + SnarfSize-UTFmax; i++)
1817 p += runetochar(p, t.r+q0+i);
1818 *p = '\0';
1819 pm->ndata = strlen(pm->data);
1820 if(plumbsend(plumbfd, pm) < 0){
1821 setcursor(mc, &query);
1822 sleep(500);
1823 if(holdon)
1824 setcursor(mc, &whitearrow);
1825 else
1826 setcursor(mc, nil);
1828 plumbfree(pm);
1832 * Process in-band messages about window title changes.
1833 * The messages are of the form:
1835 * \033];xxx\007
1837 * where xxx is the new directory. This format was chosen
1838 * because it changes the label on xterm windows.
1840 int
1841 label(Rune *sr, int n)
1843 Rune *sl, *el, *er, *r;
1844 char *p;
1846 er = sr+n;
1847 for(r=er-1; r>=sr; r--)
1848 if(*r == '\007')
1849 break;
1850 if(r < sr)
1851 return n;
1853 el = r+1;
1854 if(el-sr > sizeof wdir)
1855 sr = el - sizeof wdir;
1856 for(sl=el-3; sl>=sr; sl--)
1857 if(sl[0]=='\033' && sl[1]==']' && sl[2]==';')
1858 break;
1859 if(sl < sr)
1860 return n;
1862 snprint(wdir, sizeof wdir, "%.*S", (el-1)-(sl+3), sl+3);
1863 drawsetlabel(wdir);
1865 /* remove trailing /-sysname if present */
1866 p = strrchr(wdir, '/');
1867 if(p && *(p+1) == '-'){
1868 if(p == wdir)
1869 p++;
1870 *p = 0;
1873 runemove(sl, el, er-el);
1874 n -= (el-sl);
1875 return n;
1878 int
1879 rawon(void)
1881 return !cooked && !isecho(sfd);
1885 * Clumsy hack to make " and "" work.
1886 * Then again, what's not a clumsy hack here in Unix land?
1889 char adir[100];
1890 int afd;
1892 void
1893 removethesocket(void)
1895 if(thesocket[0])
1896 if(remove(thesocket) < 0)
1897 fprint(2, "remove %s: %r\n", thesocket);
1900 void
1901 servedevtext(void)
1903 char buf[100];
1905 snprint(buf, sizeof buf, "unix!/tmp/9term-text.%d", getpid());
1907 if((afd = announce(buf, adir)) < 0){
1908 putenv("text9term", "");
1909 return;
1912 putenv("text9term", buf);
1913 proccreate(listenproc, nil, STACK);
1914 strcpy(thesocket, buf+5);
1915 atexit(removethesocket);
1918 void
1919 listenproc(void *arg)
1921 int fd;
1922 char dir[100];
1924 USED(arg);
1925 for(;;){
1926 fd = listen(adir, dir);
1927 if(fd < 0){
1928 close(afd);
1929 return;
1931 proccreate(textthread, (void*)fd, STACK);
1935 void
1936 textthread(void *arg)
1938 int fd, i, x, n, end;
1939 Rune r;
1940 char buf[4096], *p, *ep;
1942 fd = (int)arg;
1943 p = buf;
1944 ep = buf+sizeof buf;
1945 end = t.org+t.nr; /* avoid possible output loop */
1946 for(i=t.org;; i++){
1947 if(i >= end || ep-p < UTFmax){
1948 for(x=0; x<p-buf; x+=n)
1949 if((n = write(fd, buf+x, (p-x)-buf)) <= 0)
1950 goto break2;
1952 if(i >= end)
1953 break;
1954 p = buf;
1956 if(i < t.org)
1957 i = t.org;
1958 r = t.r[i-t.org];
1959 if(r < Runeself)
1960 *p++ = r;
1961 else
1962 p += runetochar(p, &r);
1964 break2:
1965 close(fd);