22 long modified = 0; /* strange lookahead for menus */
30 threadmain(int argc, char *argv[])
32 int i, got, scr, chord;
37 getscreen(argc, argv);
40 scratch = alloc(100*RUNESIZE);
43 r.max.y = r.min.y+Dy(r)/5;
44 flstart(screen->clipr);
46 flnew(&cmd.l[0], gettext, 1, &cmd);
47 flinit(&cmd.l[0], r, font, cmdcols);
51 outTs(Tversion, VERSION);
52 startnewfile(Tstartcmdfile, &cmd);
56 for(;;got = waitforio()){
57 if(hasunlocked && RESIZED())
62 for(i=0; cmd.l[i].textfn==0; i++)
65 flsetselect(which, cmd.rasp.nrunes, cmd.rasp.nrunes);
68 if(got&(1<<RKeyboard))
70 type(which, RKeyboard);
74 if(hostlock==2 || !ptinrect(mousep->xy, screen->r)){
78 nwhich = flwhich(mousep->xy);
79 scr = which && (ptinrect(mousep->xy, which->scroll) ||
80 mousep->buttons&(8|16));
83 if((mousep->buttons&1)==0)
85 if(chord && which && which==nwhich){
86 chord |= mousep->buttons;
87 t = (Text *)which->user1;
99 }else if(mousep->buttons&(1|8)){
104 scroll(which, (mousep->buttons&8) ? 4 : 1);
106 t=(Text *)which->user1;
108 outTsl(Tdclick, t->tag, which->p0);
112 if(mousep->buttons&1)
113 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);
473 #define BACKSCROLLKEY Kup
476 #define HOMEKEY Khome
477 #define LEFTARROW Kleft
479 #define LINESTART 0x01
480 #define PAGEDOWN Kpgdown
482 #define RIGHTARROW Kright
483 #define SCROLLKEY Kdown
506 type(Flayer *l, int res) /* what a bloody mess this is */
508 Text *t = (Text *)l->user1;
517 scrollkey = nontypingkey(qpeekc()); /* ICK */
519 if(hostlock || t->lock){
524 if(a!=l->p1 && !scrollkey){
526 cut(t, t->front, 1, 1);
527 return; /* it may now be locked */
530 while((c = kbdchar())>0){
531 if(res == RKeyboard){
532 if(nontypingkey(c) || c==ESC)
534 /* backspace, ctrl-u, ctrl-w, del */
535 if(c=='\b' || c==0x15 || c==0x17 || c==0x7F){
540 if(spacesindent && c == '\t'){
542 col = getcol(&t->rasp, a);
543 n = maxtab - col % maxtab;
544 for(i = 0; i < n && p < buf+nelem(buf); i++)
552 cursor = ctlu(&t->rasp, 0, a+(p-buf)-1);
553 while(p < buf+nelem(buf)){
554 ch = raspc(&t->rasp, cursor++);
555 if(ch == ' ' || ch == '\t')
561 if(c == '\n' || p >= buf+sizeof(buf)/sizeof(buf[0]))
569 hgrow(t->tag, a, p-buf, 0);
570 t->lock++; /* pretend we Trequest'ed for hdatarune*/
571 hdatarune(t->tag, a, buf, p-buf);
576 if(c=='\n' || typeend-typestart>100)
580 if(c==SCROLLKEY || c==PAGEDOWN){
582 center(l, l->origin+l->f.nchars+1);
583 /* backspacing immediately after outcmd(): sorry */
584 }else if(c==BACKSCROLLKEY || c==PAGEUP){
586 a0 = l->origin-l->f.nchars;
590 }else if(c == RIGHTARROW){
593 if(a0 < t->rasp.nrunes)
595 flsetselect(l, a0, a0);
597 }else if(c == LEFTARROW){
602 flsetselect(l, a0, a0);
604 }else if(c == HOMEKEY){
607 }else if(c == ENDKEY){
609 center(l, t->rasp.nrunes);
610 }else if(c == LINESTART || c == LINEEND){
613 while(a > 0 && raspc(&t->rasp, a-1)!='\n')
616 while(a < t->rasp.nrunes && raspc(&t->rasp, a)!='\n')
619 for(l=t->l; l<&t->l[NL]; l++)
621 flsetselect(l, l->p0, l->p1);
622 }else if(backspacing && !hostlock){
623 /* backspacing immediately after outcmd(): sorry */
624 if(l->f.p0>0 && a>0){
628 l->p0 = del(&t->rasp, l->origin, a);
630 case 0x15: /* ctrl-u */
631 l->p0 = ctlu(&t->rasp, l->origin, a);
633 case 0x17: /* ctrl-w */
634 l->p0 = ctlw(&t->rasp, l->origin, a);
639 /* cut locally if possible */
640 if(typestart<=l->p0 && l->p1<=typeend){
641 t->lock++; /* to call hcut */
642 hcut(t->tag, l->p0, l->p1-l->p0);
643 /* hcheck is local because we know rasp is contiguous */
647 cut(t, t->front, 0, 1);
653 if(typestart >= l->p0)
656 if(typestart == typeend){
664 if(c==ESC && typeesc>=0){
669 for(l=t->l; l<&t->l[NL]; l++)
671 flsetselect(l, l->p0, l->p1);
679 outTsll(Tworkfile, ((Text *)work->user1)->tag, work->p0, work->p1);
689 panic1(Display*, char *s)
691 fprint(2, "samterm:panic: ");
697 gettext(Flayer *l, long n, ulong *np)
702 rload(&t->rasp, l->origin, l->origin+n, np);
709 return ((Text *)l->user1)->rasp.nrunes;