22 long modified = 0; /* strange lookahead for menus */
28 threadmain(int argc, char *argv[])
36 * sam is talking to us on fd 0 and 1.
37 * move these elsewhere so that if we accidentally
38 * use 0 and 1 in other code, nothing bad happens.
46 open("/dev/null", OREAD);
47 if(open("/dev/tty", OWRITE) < 0)
48 open("/dev/null", OWRITE);
51 if(protodebug) print("getscreen\n");
52 getscreen(argc, argv);
53 if(protodebug) print("iconinit\n");
55 if(protodebug) print("initio\n");
57 if(protodebug) print("scratch\n");
58 scratch = alloc(100*RUNESIZE);
61 r.max.y = r.min.y+Dy(r)/5;
62 if(protodebug) print("flstart\n");
63 flstart(screen->clipr);
65 flnew(&cmd.l[0], gettext, 1, &cmd);
66 flinit(&cmd.l[0], r, font, cmdcols);
70 outTs(Tversion, VERSION);
71 startnewfile(Tstartcmdfile, &cmd);
74 if(protodebug) print("loop\n");
75 for(;;got = waitforio()){
76 if(hasunlocked && RESIZED())
81 for(i=0; cmd.l[i].textfn==0; i++)
84 flsetselect(which, cmd.rasp.nrunes, cmd.rasp.nrunes);
87 if(got&(1<<RKeyboard))
89 type(which, RKeyboard);
93 if(hostlock==2 || !ptinrect(mousep->xy, screen->r)){
97 nwhich = flwhich(mousep->xy);
98 scr = which && ptinrect(mousep->xy, which->scroll);
101 if(mousep->buttons&1){
108 t=(Text *)which->user1;
110 outTsl(Tdclick, t->tag, which->p0);
116 }else if((mousep->buttons&2) && which){
121 }else if((mousep->buttons&4)){
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;
315 outTsll(Torigin, t->tag, l->origin, p0);
318 outTsll(Torigin, t->tag, p0, 1L);
329 * Hard to get absolutely right. Use what we know about ASCII
330 * and assume anything above the Latin control characters is
331 * potentially an alphanumeric.
335 if(0x7F<=c && c<=0xA0)
337 if(utfrune("!\"#$%&'()*+,-./:;<=>?@[\\]^`{|}~", c))
343 raspc(Rasp *r, long p)
346 rload(r, p, p+1, &n);
353 ctlw(Rasp *r, long o, long p)
359 if(raspc(r, p)=='\n')
361 for(; p>=o && !alnum(c=raspc(r, p)); --p)
364 for(; p>o && alnum(raspc(r, p-1)); --p)
370 ctlu(Rasp *r, long o, long p)
374 if(raspc(r, p)=='\n')
376 for(; p-1>=o && raspc(r, p-1)!='\n'; --p)
382 center(Flayer *l, long a)
387 if(!t->lock && (a<l->origin || l->origin+l->f.nchars<a)){
388 if(a > t->rasp.nrunes)
390 outTsll(Torigin, t->tag, a, 2L);
397 onethird(Flayer *l, long a)
404 if(!t->lock && (a<l->origin || l->origin+l->f.nchars<a)){
405 if(a > t->rasp.nrunes)
407 s = insetrect(l->scroll, 1);
408 lines = ((s.max.y-s.min.y)/l->f.font->height+1)/3;
411 outTsll(Torigin, t->tag, a, lines);
418 flushtyping(int clearesc)
425 if(typestart == typeend) {
432 rload(&t->rasp, typestart, typeend, &n);
434 if(t==&cmd && typeend==t->rasp.nrunes && scratch[typeend-typestart-1]=='\n'){
438 outTslS(Ttype, t->tag, typestart, scratch);
443 #define SCROLLKEY Kdown
444 #define BACKSCROLLKEY Kup
446 #define HOMEKEY Khome
449 #define PAGEDOWN Kpgdown
450 #define LEFTARROW Kleft
451 #define RIGHTARROW Kright
456 return c==SCROLLKEY || c==BACKSCROLLKEY
457 || c==LEFTARROW || c==RIGHTARROW
458 || c==HOMEKEY || c==ENDKEY
459 || c==PAGEUP || c==PAGEDOWN;
463 type(Flayer *l, int res) /* what a bloody mess this is */
465 Text *t = (Text *)l->user1;
474 scrollkey = nontypingkey(qpeekc()); /* ICK */
476 if(hostlock || t->lock){
481 if(a!=l->p1 && !scrollkey){
483 cut(t, t->front, 1, 1);
484 return; /* it may now be locked */
487 while((c = kbdchar())>0){
488 if(res == RKeyboard){
489 if(nontypingkey(c) || c==ESC)
491 /* backspace, ctrl-u, ctrl-w, del */
492 if(c=='\b' || c==0x15 || c==0x17 || c==0x7F){
498 if(c == '\n' || p >= buf+sizeof(buf)/sizeof(buf[0]))
506 hgrow(t->tag, a, p-buf, 0);
507 t->lock++; /* pretend we Trequest'ed for hdatarune*/
508 hdatarune(t->tag, a, buf, p-buf);
513 if(c=='\n' || typeend-typestart>100)
517 if(c==SCROLLKEY || c==PAGEDOWN){
519 center(l, l->origin+l->f.nchars+1);
520 }else if(c==BACKSCROLLKEY || c==PAGEUP){
522 a0 = l->origin-l->f.nchars;
526 }else if(c == RIGHTARROW){
529 if(a0 < t->rasp.nrunes)
531 flsetselect(l, a0, a0);
533 }else if(c == LEFTARROW){
538 flsetselect(l, a0, a0);
540 }else if(c == HOMEKEY){
543 }else if(c == ENDKEY){
545 center(l, t->rasp.nrunes);
546 }else if(backspacing && !hostlock){
547 /* backspacing immediately after outcmd(): sorry */
548 if(l->f.p0>0 && a>0){
554 case 0x15: /* ctrl-u */
555 l->p0 = ctlu(&t->rasp, l->origin, a);
557 case 0x17: /* ctrl-w */
558 l->p0 = ctlw(&t->rasp, l->origin, a);
563 /* cut locally if possible */
564 if(typestart<=l->p0 && l->p1<=typeend){
565 t->lock++; /* to call hcut */
566 hcut(t->tag, l->p0, l->p1-l->p0);
567 /* hcheck is local because we know rasp is contiguous */
571 cut(t, t->front, 0, 1);
577 if(typestart >= l->p0)
580 if(typestart == typeend){
588 if(c==ESC && typeesc>=0){
593 for(l=t->l; l<&t->l[NL]; l++)
595 flsetselect(l, l->p0, l->p1);
603 outTsll(Tworkfile, ((Text *)work->user1)->tag, work->p0, work->p1);
613 panic1(Display *d, char *s)
615 fprint(2, "samterm:panic: ");
621 gettext(Flayer *l, long n, ulong *np)
626 rload(&t->rasp, l->origin, l->origin+n, np);
633 return ((Text *)l->user1)->rasp.nrunes;