22 long modified = 0; /* strange lookahead for menus */
29 #define chording 0 /* code here for reference but it causes deadlocks */
32 threadmain(int argc, char *argv[])
40 * sam is talking to us on fd 0 and 1.
41 * move these elsewhere so that if we accidentally
42 * use 0 and 1 in other code, nothing bad happens.
50 open("/dev/null", OREAD);
51 if(open("/dev/tty", OWRITE) < 0)
52 open("/dev/null", OWRITE);
55 if(protodebug) print("getscreen\n");
56 getscreen(argc, argv);
57 if(protodebug) print("iconinit\n");
59 if(protodebug) print("initio\n");
61 if(protodebug) print("scratch\n");
62 scratch = alloc(100*RUNESIZE);
65 r.max.y = r.min.y+Dy(r)/5;
66 if(protodebug) print("flstart\n");
67 flstart(screen->clipr);
69 flnew(&cmd.l[0], gettext, 1, &cmd);
70 flinit(&cmd.l[0], r, font, cmdcols);
74 outTs(Tversion, VERSION);
75 startnewfile(Tstartcmdfile, &cmd);
78 if(protodebug) print("loop\n");
79 for(;;got = waitforio()){
80 if(hasunlocked && RESIZED())
85 for(i=0; cmd.l[i].textfn==0; i++)
88 flsetselect(which, cmd.rasp.nrunes, cmd.rasp.nrunes);
91 if(got&(1<<RKeyboard))
93 type(which, RKeyboard);
97 if(hostlock==2 || !ptinrect(mousep->xy, screen->r)){
101 nwhich = flwhich(mousep->xy);
102 scr = which && ptinrect(mousep->xy, which->scroll);
105 if(chording && chord==1 && !mousep->buttons)
107 if(chording && chord)
108 chord |= mousep->buttons;
109 else if(mousep->buttons&1){
116 t=(Text *)which->user1;
118 outTsl(Tdclick, t->tag, which->p0);
122 if(mousep->buttons&1)
123 chord = mousep->buttons;
126 }else if((mousep->buttons&2) && which){
131 }else if((mousep->buttons&4)){
139 if(chording && chord){
140 t = (Text*)which->user1;
141 if(!t->lock && !hostlock){
160 flresize(screen->clipr);
161 for(i = 0; i<nname; i++)
163 hcheck(text[i]->tag);
178 t = (Text *)nw->user1;
179 t->front = nw-&t->l[0];
189 Text *t=(Text *)l->user1;
192 m = whichmenu(t->tag);
198 current(flwhich(Pt(0, 0)));
206 }else if(l == &t->l[t->front]){
207 for(m=0; m<NL; m++) /* find one; any one will do */
220 for(i = 0; i<NL; i++)
221 if(t->l[i].textfn==0)
227 duplicate(Flayer *l, Rectangle r, Font *f, int close)
229 Text *t=(Text *)l->user1;
230 Flayer *nl = findl(t);
235 flnew(nl, gettext, l->user0, (char *)t);
236 flinit(nl, r, f, l->f.cols);
237 nl->origin = l->origin;
238 rp = (*l->textfn)(l, l->f.nchars, &n);
239 flinsert(nl, rp, rp+n, l->origin);
240 flsetselect(nl, l->p0, l->p1);
250 setcursor(mousectl, cursor);
256 while(((mousep->buttons&7)!=0) != updown)
266 *rp = getrect(3, mousectl);
267 if(rp->max.x && rp->max.x-rp->min.x<=5 && rp->max.y-rp->min.y<=5){
269 r = cmd.l[cmd.front].entire;
274 else if (p.y >= r.max.y)
278 else if (p.x >= r.max.x)
282 return rectclip(rp, screen->r) &&
283 rp->max.x-rp->min.x>100 && rp->max.y-rp->min.y>40;
287 snarf(Text *t, int w)
289 Flayer *l = &t->l[w];
292 snarflen = l->p1-l->p0;
293 outTsll(Tsnarf, t->tag, l->p0, l->p1);
298 cut(Text *t, int w, int save, int check)
312 outTsll(Tcut, t->tag, p0, p1);
313 flsetselect(l, p0, p0);
315 hcut(t->tag, p0, p1-p0);
321 paste(Text *t, int w)
326 outTsl(Tpaste, t->tag, t->l[w].p0);
331 scrorigin(Flayer *l, int but, long p0)
333 Text *t=(Text *)l->user1;
337 outTsll(Torigin, t->tag, l->origin, p0);
340 outTsll(Torigin, t->tag, p0, 1L);
351 * Hard to get absolutely right. Use what we know about ASCII
352 * and assume anything above the Latin control characters is
353 * potentially an alphanumeric.
357 if(0x7F<=c && c<=0xA0)
359 if(utfrune("!\"#$%&'()*+,-./:;<=>?@[\\]^`{|}~", c))
365 raspc(Rasp *r, long p)
368 rload(r, p, p+1, &n);
375 ctlw(Rasp *r, long o, long p)
381 if(raspc(r, p)=='\n')
383 for(; p>=o && !alnum(c=raspc(r, p)); --p)
386 for(; p>o && alnum(raspc(r, p-1)); --p)
392 ctlu(Rasp *r, long o, long p)
396 if(raspc(r, p)=='\n')
398 for(; p-1>=o && raspc(r, p-1)!='\n'; --p)
404 center(Flayer *l, long a)
409 if(!t->lock && (a<l->origin || l->origin+l->f.nchars<a)){
410 if(a > t->rasp.nrunes)
412 outTsll(Torigin, t->tag, a, 2L);
419 thirds(Flayer *l, long a, int n)
426 if(!t->lock && (a<l->origin || l->origin+l->f.nchars<a)){
427 if(a > t->rasp.nrunes)
429 s = insetrect(l->scroll, 1);
430 lines = (n*(s.max.y-s.min.y)/l->f.font->height+1)/3;
433 outTsll(Torigin, t->tag, a, lines);
440 onethird(Flayer *l, long a)
442 return thirds(l, a, 1);
446 twothirds(Flayer *l, long a)
448 return thirds(l, a, 2);
452 flushtyping(int clearesc)
459 if(typestart == typeend) {
466 rload(&t->rasp, typestart, typeend, &n);
468 if(t==&cmd && typeend==t->rasp.nrunes && scratch[typeend-typestart-1]=='\n'){
472 outTslS(Ttype, t->tag, typestart, scratch);
477 #define SCROLLKEY Kdown
478 #define BACKSCROLLKEY Kup
480 #define HOMEKEY Khome
483 #define PAGEDOWN Kpgdown
484 #define LEFTARROW Kleft
485 #define RIGHTARROW Kright
490 return c==SCROLLKEY || c==BACKSCROLLKEY
491 || c==LEFTARROW || c==RIGHTARROW
492 || c==HOMEKEY || c==ENDKEY
493 || c==PAGEUP || c==PAGEDOWN;
497 type(Flayer *l, int res) /* what a bloody mess this is */
499 Text *t = (Text *)l->user1;
508 scrollkey = nontypingkey(qpeekc()); /* ICK */
510 if(hostlock || t->lock){
515 if(a!=l->p1 && !scrollkey){
517 cut(t, t->front, 1, 1);
518 return; /* it may now be locked */
521 while((c = kbdchar())>0){
522 if(res == RKeyboard){
523 if(nontypingkey(c) || c==ESC)
525 /* backspace, ctrl-u, ctrl-w, del */
526 if(c=='\b' || c==0x15 || c==0x17 || c==0x7F){
536 cursor = ctlu(&t->rasp, 0, a+(p-buf)-1);
537 while(p < buf+nelem(buf)){
538 ch = raspc(&t->rasp, cursor++);
539 if(ch == ' ' || ch == '\t')
545 if(c == '\n' || p >= buf+nelem(buf))
553 hgrow(t->tag, a, p-buf, 0);
554 t->lock++; /* pretend we Trequest'ed for hdatarune*/
555 hdatarune(t->tag, a, buf, p-buf);
560 if(c=='\n' || typeend-typestart>100)
564 if(c==SCROLLKEY || c==PAGEDOWN){
566 center(l, l->origin+l->f.nchars+1);
567 }else if(c==BACKSCROLLKEY || c==PAGEUP){
569 a0 = l->origin-l->f.nchars;
573 }else if(c == RIGHTARROW){
576 if(a0 < t->rasp.nrunes)
578 flsetselect(l, a0, a0);
580 }else if(c == LEFTARROW){
585 flsetselect(l, a0, a0);
587 }else if(c == HOMEKEY){
590 }else if(c == ENDKEY){
592 center(l, t->rasp.nrunes);
593 }else if(backspacing && !hostlock){
594 /* backspacing immediately after outcmd(): sorry */
595 if(l->f.p0>0 && a>0){
601 case 0x15: /* ctrl-u */
602 l->p0 = ctlu(&t->rasp, l->origin, a);
604 case 0x17: /* ctrl-w */
605 l->p0 = ctlw(&t->rasp, l->origin, a);
610 /* cut locally if possible */
611 if(typestart<=l->p0 && l->p1<=typeend){
612 t->lock++; /* to call hcut */
613 hcut(t->tag, l->p0, l->p1-l->p0);
614 /* hcheck is local because we know rasp is contiguous */
618 cut(t, t->front, 0, 1);
624 if(typestart >= l->p0)
627 if(typestart == typeend){
635 if(c==ESC && typeesc>=0){
640 for(l=t->l; l<&t->l[NL]; l++)
642 flsetselect(l, l->p0, l->p1);
650 outTsll(Tworkfile, ((Text *)work->user1)->tag, work->p0, work->p1);
660 panic1(Display *d, char *s)
662 fprint(2, "samterm:panic: ");
668 gettext(Flayer *l, long n, ulong *np)
673 rload(&t->rasp, l->origin, l->origin+n, np);
680 return ((Text *)l->user1)->rasp.nrunes;