12 int mainstacksize = 16*1024;
24 long modified = 0; /* strange lookahead for menus */
32 threadmain(int argc, char *argv[])
34 int i, got, scr, chord;
39 getscreen(argc, argv);
42 scratch = alloc(100*RUNESIZE);
45 r.max.y = r.min.y+Dy(r)/5;
46 flstart(screen->clipr);
48 flnew(&cmd.l[0], gettext, 1, &cmd);
49 flinit(&cmd.l[0], r, font, cmdcols);
53 outTs(Tversion, VERSION);
54 startnewfile(Tstartcmdfile, &cmd);
58 for(;;got = waitforio()){
59 if(hasunlocked && RESIZED())
64 for(i=0; cmd.l[i].textfn==0; i++)
67 flsetselect(which, cmd.rasp.nrunes, cmd.rasp.nrunes);
70 if(got&(1<<RKeyboard))
72 type(which, RKeyboard);
76 if(hostlock==2 || !ptinrect(mousep->xy, screen->r)){
80 nwhich = flwhich(mousep->xy);
81 scr = which && (ptinrect(mousep->xy, which->scroll) ||
82 mousep->buttons&(8|16));
85 if((mousep->buttons&1)==0)
87 if(chord && which && which==nwhich){
88 chord |= mousep->buttons;
89 t = (Text *)which->user1;
101 }else if(mousep->buttons&(1|8)){
103 scroll(which, (mousep->buttons&8) ? 4 : 1);
104 else if(nwhich && nwhich!=which)
107 t=(Text *)which->user1;
109 outTsl(Tdclick, t->tag, which->p0);
113 if(mousep->buttons&1)
114 chord = mousep->buttons;
116 }else if((mousep->buttons&2) && which){
121 }else if(mousep->buttons&(4|16)){
123 scroll(which, (mousep->buttons&16) ? 5 : 3);
138 flresize(screen->clipr);
139 for(i = 0; i<nname; i++)
141 hcheck(text[i]->tag);
156 t = (Text *)nw->user1;
157 t->front = nw-&t->l[0];
167 Text *t=(Text *)l->user1;
170 m = whichmenu(t->tag);
176 current(flwhich(Pt(0, 0)));
184 }else if(l == &t->l[t->front]){
185 for(m=0; m<NL; m++) /* find one; any one will do */
198 for(i = 0; i<NL; i++)
199 if(t->l[i].textfn==0)
205 duplicate(Flayer *l, Rectangle r, Font *f, int close)
207 Text *t=(Text *)l->user1;
208 Flayer *nl = findl(t);
213 flnew(nl, gettext, l->user0, (char *)t);
214 flinit(nl, r, f, l->f.cols);
215 nl->origin = l->origin;
216 rp = (*l->textfn)(l, l->f.nchars, &n);
217 flinsert(nl, rp, rp+n, l->origin);
218 flsetselect(nl, l->p0, l->p1);
228 setcursor(mousectl, cursor);
234 while(((mousep->buttons&7)!=0) != updown)
244 *rp = getrect(3, mousectl);
245 if(rp->max.x && rp->max.x-rp->min.x<=5 && rp->max.y-rp->min.y<=5){
247 r = cmd.l[cmd.front].entire;
252 else if (p.y >= r.max.y)
256 else if (p.x >= r.max.x)
260 return rectclip(rp, screen->r) &&
261 rp->max.x-rp->min.x>100 && rp->max.y-rp->min.y>40;
265 snarf(Text *t, int w)
267 Flayer *l = &t->l[w];
270 snarflen = l->p1-l->p0;
271 outTsll(Tsnarf, t->tag, l->p0, l->p1);
276 cut(Text *t, int w, int save, int check)
290 outTsll(Tcut, t->tag, p0, p1);
291 flsetselect(l, p0, p0);
293 hcut(t->tag, p0, p1-p0);
299 paste(Text *t, int w)
304 outTsl(Tpaste, t->tag, t->l[w].p0);
309 scrorigin(Flayer *l, int but, long p0)
311 Text *t=(Text *)l->user1;
313 if(t->tag == Untagged)
318 outTsll(Torigin, t->tag, l->origin, p0);
321 outTsll(Torigin, t->tag, p0, 1L);
332 * Hard to get absolutely right. Use what we know about ASCII
333 * and assume anything above the Latin control characters is
334 * potentially an alphanumeric.
338 if(0x7F<=c && c<=0xA0)
340 if(utfrune("!\"#$%&'()*+,-./:;<=>?@[\\]^`{|}~", c))
346 raspc(Rasp *r, long p)
349 rload(r, p, p+1, &n);
356 getcol(Rasp *r, long p)
360 for(col = 0; p > 0 && raspc(r, p-1)!='\n'; p--, col++)
366 del(Rasp *r, long o, long p)
372 if(!spacesindent || raspc(r, p)!=' ')
374 col = getcol(r, p) + 1;
375 if((n = col % maxtab) == 0)
377 for(i = 0; p-1>=o && raspc(r, p-1)==' ' && i<n-1; --p, i++)
383 ctlw(Rasp *r, long o, long p)
389 if(raspc(r, p)=='\n')
391 for(; p>=o && !alnum(c=raspc(r, p)); --p)
394 for(; p>o && alnum(raspc(r, p-1)); --p)
400 ctlu(Rasp *r, long o, long p)
404 if(raspc(r, p)=='\n')
406 for(; p-1>=o && raspc(r, p-1)!='\n'; --p)
412 center(Flayer *l, long a)
417 if(!t->lock && (a<l->origin || l->origin+l->f.nchars<a)){
418 if(a > t->rasp.nrunes)
420 outTsll(Torigin, t->tag, a, 2L);
427 onethird(Flayer *l, long a)
434 if(!t->lock && (a<l->origin || l->origin+l->f.nchars<a)){
435 if(a > t->rasp.nrunes)
437 s = insetrect(l->scroll, 1);
438 lines = ((s.max.y-s.min.y)/l->f.font->height+1)/3;
441 outTsll(Torigin, t->tag, a, lines);
448 flushtyping(int clearesc)
455 if(typestart == typeend) {
462 rload(&t->rasp, typestart, typeend, &n);
464 if(t==&cmd && typeend==t->rasp.nrunes && scratch[typeend-typestart-1]=='\n'){
468 outTslS(Ttype, t->tag, typestart, scratch);
495 type(Flayer *l, int res) /* what a bloody mess this is */
497 Text *t = (Text *)l->user1;
506 scrollkey = nontypingkey(qpeekc()); /* ICK */
508 if(hostlock || t->lock){
513 if(a!=l->p1 && !scrollkey){
515 cut(t, t->front, 1, 1);
516 return; /* it may now be locked */
519 while((c = kbdchar())>0){
520 if(res == RKeyboard){
521 if(nontypingkey(c) || c==Kesc)
523 /* backspace, ctrl-u, ctrl-w, del */
524 if(c==Kbs || c==Knack || c==Ketb || c==Kdel){
529 if(spacesindent && c == '\t'){
531 col = getcol(&t->rasp, a);
532 n = maxtab - col % maxtab;
533 for(i = 0; i < n && p < buf+nelem(buf); i++)
541 cursor = ctlu(&t->rasp, 0, a+(p-buf)-1);
542 while(p < buf+nelem(buf)){
543 ch = raspc(&t->rasp, cursor++);
544 if(ch == ' ' || ch == '\t')
550 if(c == '\n' || p >= buf+sizeof(buf)/sizeof(buf[0]))
558 hgrow(t->tag, a, p-buf, 0);
559 t->lock++; /* pretend we Trequest'ed for hdatarune*/
560 hdatarune(t->tag, a, buf, p-buf);
565 if(c=='\n' || typeend-typestart>100)
569 if(c==Kdown || c==Kpgdown){
571 center(l, l->origin+l->f.nchars+1);
572 /* backspacing immediately after outcmd(): sorry */
573 }else if(c==Kup || c==Kpgup){
575 a0 = l->origin-l->f.nchars;
579 }else if(c == Kright){
582 if(a0 < t->rasp.nrunes)
584 flsetselect(l, a0, a0);
586 }else if(c == Kleft){
591 flsetselect(l, a0, a0);
593 }else if(c == Khome){
598 center(l, t->rasp.nrunes);
599 }else if(c == Ksoh || c == Kenq){
602 while(a > 0 && raspc(&t->rasp, a-1)!='\n')
605 while(a < t->rasp.nrunes && raspc(&t->rasp, a)!='\n')
608 for(l=t->l; l<&t->l[NL]; l++)
610 flsetselect(l, l->p0, l->p1);
611 }else if(backspacing && !hostlock){
612 /* backspacing immediately after outcmd(): sorry */
613 if(l->f.p0>0 && a>0){
617 l->p0 = del(&t->rasp, l->origin, a);
619 case Knack: /* ctrl-u */
620 l->p0 = ctlu(&t->rasp, l->origin, a);
622 case Ketb: /* ctrl-w */
623 l->p0 = ctlw(&t->rasp, l->origin, a);
628 /* cut locally if possible */
629 if(typestart<=l->p0 && l->p1<=typeend){
630 t->lock++; /* to call hcut */
631 hcut(t->tag, l->p0, l->p1-l->p0);
632 /* hcheck is local because we know rasp is contiguous */
636 cut(t, t->front, 0, 1);
642 if(typestart >= l->p0)
645 if(typestart == typeend){
654 for(l=t->l; l->textfn==0; l++)
659 flsetselect(l, a, a);
662 if(c==Kesc && typeesc>=0){
667 for(l=t->l; l<&t->l[NL]; l++)
669 flsetselect(l, l->p0, l->p1);
677 outTsll(Tworkfile, ((Text *)work->user1)->tag, work->p0, work->p1);
687 panic1(Display*, char *s)
689 fprint(2, "samterm:panic: ");
695 gettext(Flayer *l, long n, ulong *np)
700 rload(&t->rasp, l->origin, l->origin+n, np);
707 return ((Text *)l->user1)->rasp.nrunes;