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);
54 if(protodebug) print("getscreen\n");
55 getscreen(argc, argv);
56 if(protodebug) print("iconinit\n");
58 if(protodebug) print("initio\n");
60 if(protodebug) print("scratch\n");
61 scratch = alloc(100*RUNESIZE);
64 r.max.y = r.min.y+Dy(r)/5;
65 if(protodebug) print("flstart\n");
66 flstart(screen->clipr);
68 flnew(&cmd.l[0], gettext, 1, &cmd);
69 flinit(&cmd.l[0], r, font, cmdcols);
73 outTs(Tversion, VERSION);
74 startnewfile(Tstartcmdfile, &cmd);
77 if(protodebug) print("loop\n");
78 for(;;got = waitforio()){
79 if(hasunlocked && RESIZED())
84 for(i=0; cmd.l[i].textfn==0; i++)
87 flsetselect(which, cmd.rasp.nrunes, cmd.rasp.nrunes);
90 if(got&(1<<RKeyboard))
92 type(which, RKeyboard);
96 if(hostlock==2 || !ptinrect(mousep->xy, screen->r)){
100 nwhich = flwhich(mousep->xy);
101 scr = which && ptinrect(mousep->xy, which->scroll);
104 if(chording && chord==1 && !mousep->buttons)
106 if(chording && chord)
107 chord |= mousep->buttons;
108 else if(mousep->buttons&1){
115 t=(Text *)which->user1;
117 outTsl(Tdclick, t->tag, which->p0);
121 if(mousep->buttons&1)
122 chord = mousep->buttons;
125 }else if((mousep->buttons&2) && which){
130 }else if((mousep->buttons&4)){
138 if(chording && chord){
139 t = (Text*)which->user1;
140 if(!t->lock && !hostlock){
159 flresize(screen->clipr);
160 for(i = 0; i<nname; i++)
162 hcheck(text[i]->tag);
177 t = (Text *)nw->user1;
178 t->front = nw-&t->l[0];
188 Text *t=(Text *)l->user1;
191 m = whichmenu(t->tag);
197 current(flwhich(Pt(0, 0)));
205 }else if(l == &t->l[t->front]){
206 for(m=0; m<NL; m++) /* find one; any one will do */
219 for(i = 0; i<NL; i++)
220 if(t->l[i].textfn==0)
226 duplicate(Flayer *l, Rectangle r, Font *f, int close)
228 Text *t=(Text *)l->user1;
229 Flayer *nl = findl(t);
234 flnew(nl, gettext, l->user0, (char *)t);
235 flinit(nl, r, f, l->f.cols);
236 nl->origin = l->origin;
237 rp = (*l->textfn)(l, l->f.nchars, &n);
238 flinsert(nl, rp, rp+n, l->origin);
239 flsetselect(nl, l->p0, l->p1);
249 setcursor(mousectl, cursor);
255 while(((mousep->buttons&7)!=0) != updown)
265 *rp = getrect(3, mousectl);
266 if(rp->max.x && rp->max.x-rp->min.x<=5 && rp->max.y-rp->min.y<=5){
268 r = cmd.l[cmd.front].entire;
273 else if (p.y >= r.max.y)
277 else if (p.x >= r.max.x)
281 return rectclip(rp, screen->r) &&
282 rp->max.x-rp->min.x>100 && rp->max.y-rp->min.y>40;
286 snarf(Text *t, int w)
288 Flayer *l = &t->l[w];
291 snarflen = l->p1-l->p0;
292 outTsll(Tsnarf, t->tag, l->p0, l->p1);
297 cut(Text *t, int w, int save, int check)
311 outTsll(Tcut, t->tag, p0, p1);
312 flsetselect(l, p0, p0);
314 hcut(t->tag, p0, p1-p0);
320 paste(Text *t, int w)
325 outTsl(Tpaste, t->tag, t->l[w].p0);
330 scrorigin(Flayer *l, int but, long p0)
332 Text *t=(Text *)l->user1;
336 outTsll(Torigin, t->tag, l->origin, p0);
339 outTsll(Torigin, t->tag, p0, 1L);
350 * Hard to get absolutely right. Use what we know about ASCII
351 * and assume anything above the Latin control characters is
352 * potentially an alphanumeric.
356 if(0x7F<=c && c<=0xA0)
358 if(utfrune("!\"#$%&'()*+,-./:;<=>?@[\\]^`{|}~", c))
364 raspc(Rasp *r, long p)
367 rload(r, p, p+1, &n);
374 ctlw(Rasp *r, long o, long p)
380 if(raspc(r, p)=='\n')
382 for(; p>=o && !alnum(c=raspc(r, p)); --p)
385 for(; p>o && alnum(raspc(r, p-1)); --p)
391 ctlu(Rasp *r, long o, long p)
395 if(raspc(r, p)=='\n')
397 for(; p-1>=o && raspc(r, p-1)!='\n'; --p)
403 center(Flayer *l, long a)
408 if(!t->lock && (a<l->origin || l->origin+l->f.nchars<a)){
409 if(a > t->rasp.nrunes)
411 outTsll(Torigin, t->tag, a, 2L);
418 thirds(Flayer *l, long a, int n)
425 if(!t->lock && (a<l->origin || l->origin+l->f.nchars<a)){
426 if(a > t->rasp.nrunes)
428 s = insetrect(l->scroll, 1);
429 lines = (n*(s.max.y-s.min.y)/l->f.font->height+1)/3;
432 outTsll(Torigin, t->tag, a, lines);
439 onethird(Flayer *l, long a)
441 return thirds(l, a, 1);
445 twothirds(Flayer *l, long a)
447 return thirds(l, a, 2);
451 flushtyping(int clearesc)
458 if(typestart == typeend) {
465 rload(&t->rasp, typestart, typeend, &n);
467 if(t==&cmd && typeend==t->rasp.nrunes && scratch[typeend-typestart-1]=='\n'){
471 outTslS(Ttype, t->tag, typestart, scratch);
476 #define BACKSCROLLKEY Kup
479 #define HOMEKEY Khome
480 #define LEFTARROW Kleft
482 #define LINESTART 0x01
483 #define PAGEDOWN Kpgdown
485 #define RIGHTARROW Kright
486 #define SCROLLKEY Kdown
508 type(Flayer *l, int res) /* what a bloody mess this is */
510 Text *t = (Text *)l->user1;
519 scrollkey = nontypingkey(qpeekc()); /* ICK */
521 if(hostlock || t->lock){
526 if(a!=l->p1 && !scrollkey){
528 cut(t, t->front, 1, 1);
529 return; /* it may now be locked */
532 while((c = kbdchar())>0){
533 if(res == RKeyboard){
534 if(nontypingkey(c) || c==ESC)
536 /* backspace, ctrl-u, ctrl-w, del */
537 if(c=='\b' || c==0x15 || c==0x17 || c==0x7F){
547 cursor = ctlu(&t->rasp, 0, a+(p-buf)-1);
548 while(p < buf+nelem(buf)){
549 ch = raspc(&t->rasp, cursor++);
550 if(ch == ' ' || ch == '\t')
556 if(c == '\n' || p >= buf+nelem(buf))
564 hgrow(t->tag, a, p-buf, 0);
565 t->lock++; /* pretend we Trequest'ed for hdatarune*/
566 hdatarune(t->tag, a, buf, p-buf);
571 if(c=='\n' || typeend-typestart>100)
575 if(c==SCROLLKEY || c==PAGEDOWN){
577 center(l, l->origin+l->f.nchars+1);
578 }else if(c==BACKSCROLLKEY || c==PAGEUP){
580 a0 = l->origin-l->f.nchars;
584 }else if(c == RIGHTARROW){
587 if(a0 < t->rasp.nrunes)
589 flsetselect(l, a0, a0);
591 }else if(c == LEFTARROW){
596 flsetselect(l, a0, a0);
598 }else if(c == HOMEKEY){
601 }else if(c == ENDKEY){
603 center(l, t->rasp.nrunes);
604 }else if(c == LINESTART || c == LINEEND){
607 while(a > 0 && raspc(&t->rasp, a-1)!='\n')
610 while(a < t->rasp.nrunes && raspc(&t->rasp, a)!='\n')
613 for(l=t->l; l<&t->l[NL]; l++)
615 flsetselect(l, l->p0, l->p1);
616 }else if(backspacing && !hostlock){
617 /* backspacing immediately after outcmd(): sorry */
618 if(l->f.p0>0 && a>0){
624 case 0x15: /* ctrl-u */
625 l->p0 = ctlu(&t->rasp, l->origin, a);
627 case 0x17: /* ctrl-w */
628 l->p0 = ctlw(&t->rasp, l->origin, a);
633 /* cut locally if possible */
634 if(typestart<=l->p0 && l->p1<=typeend){
635 t->lock++; /* to call hcut */
636 hcut(t->tag, l->p0, l->p1-l->p0);
637 /* hcheck is local because we know rasp is contiguous */
641 cut(t, t->front, 0, 1);
647 if(typestart >= l->p0)
650 if(typestart == typeend){
658 if(c==ESC && typeesc>=0){
663 for(l=t->l; l<&t->l[NL]; l++)
665 flsetselect(l, l->p0, l->p1);
673 outTsll(Tworkfile, ((Text *)work->user1)->tag, work->p0, work->p1);
683 panic1(Display *d, char *s)
685 fprint(2, "samterm:panic: ");
691 gettext(Flayer *l, long n, ulong *np)
696 rload(&t->rasp, l->origin, l->origin+n, np);
703 return ((Text *)l->user1)->rasp.nrunes;