]> git.lizzy.rs Git - plan9front.git/blob - sys/src/cmd/samterm/main.c
awk: make empty FS unicodely-correct.
[plan9front.git] / sys / src / cmd / samterm / main.c
1 #include <u.h>
2 #include <libc.h>
3 #include <draw.h>
4 #include <thread.h>
5 #include <cursor.h>
6 #include <mouse.h>
7 #include <keyboard.h>
8 #include <frame.h>
9 #include "flayer.h"
10 #include "samterm.h"
11
12 int     mainstacksize = 16*1024;
13
14 Text    cmd;
15 Rune    *scratch;
16 long    nscralloc;
17 Cursor  *cursor;
18 Flayer  *which = 0;
19 Flayer  *work = 0;
20 long    snarflen;
21 long    typestart = -1;
22 long    typeend = -1;
23 long    typeesc = -1;
24 long    modified = 0;           /* strange lookahead for menus */
25 char    hostlock = 1;
26 char    hasunlocked = 0;
27 int     maxtab = 8;
28 int     autoindent;
29 int     spacesindent;
30
31 void
32 threadmain(int argc, char *argv[])
33 {
34         int i, got, scr, chord;
35         Text *t;
36         Rectangle r;
37         Flayer *nwhich;
38
39         getscreen(argc, argv);
40         iconinit();
41         initio();
42         scratch = alloc(100*RUNESIZE);
43         nscralloc = 100;
44         r = screen->r;
45         r.max.y = r.min.y+Dy(r)/5;
46         flstart(screen->clipr);
47         rinit(&cmd.rasp);
48         flnew(&cmd.l[0], gettext, 1, &cmd);
49         flinit(&cmd.l[0], r, font, cmdcols);
50         cmd.nwin = 1;
51         which = &cmd.l[0];
52         cmd.tag = Untagged;
53         outTs(Tversion, VERSION);
54         startnewfile(Tstartcmdfile, &cmd);
55
56         got = 0;
57         chord = 0;
58         for(;;got = waitforio()){
59                 if(hasunlocked && RESIZED())
60                         resize();
61                 if(got&(1<<RHost))
62                         rcv();
63                 if(got&(1<<RPlumb)){
64                         for(i=0; cmd.l[i].textfn==0; i++)
65                                 ;
66                         current(&cmd.l[i]);
67                         flsetselect(which, cmd.rasp.nrunes, cmd.rasp.nrunes);
68                         type(which, RPlumb);
69                 }
70                 if(got&(1<<RKeyboard))
71                         if(which)
72                                 type(which, RKeyboard);
73                         else
74                                 kbdblock();
75                 if(got&(1<<RMouse)){
76                         if(hostlock==2 || !ptinrect(mousep->xy, screen->r)){
77                                 mouseunblock();
78                                 continue;
79                         }
80                         nwhich = flwhich(mousep->xy);
81                         scr = which && (ptinrect(mousep->xy, which->scroll) ||
82                                 mousep->buttons&(8|16));
83                         if(mousep->buttons)
84                                 flushtyping(1);
85                         if((mousep->buttons&1)==0)
86                                 chord = 0;
87                         if(chord && which && which==nwhich){
88                                 chord |= mousep->buttons;
89                                 t = (Text *)which->user1;
90                                 if(!t->lock){
91                                         int w = which-t->l;
92                                         if(chord&2){
93                                                 cut(t, w, 1, 1);
94                                                 chord &= ~2;
95                                         }
96                                         if(chord&4){
97                                                 paste(t, w);
98                                                 chord &= ~4;
99                                         }
100                                 }
101                         }else if(mousep->buttons&(1|8)){
102                                 if(scr)
103                                         scroll(which, (mousep->buttons&8) ? 4 : 1);
104                                 else if(nwhich && nwhich!=which)
105                                         current(nwhich);
106                                 else{
107                                         t=(Text *)which->user1;
108                                         if(flselect(which)){
109                                                 outTsl(Tdclick, t->tag, which->p0);
110                                                 t->lock++;
111                                         }else if(t!=&cmd)
112                                                 outcmd();
113                                         if(mousep->buttons&1)
114                                                 chord = mousep->buttons;
115                                 }
116                         }else if((mousep->buttons&2) && which){
117                                 if(scr)
118                                         scroll(which, 2);
119                                 else
120                                         menu2hit();
121                         }else if(mousep->buttons&(4|16)){
122                                 if(scr)
123                                         scroll(which, (mousep->buttons&16) ? 5 : 3);
124                                 else
125                                         menu3hit();
126                         }
127                         mouseunblock();
128                 }
129         }
130 }
131
132
133 void
134 resize(void)
135 {
136         int i;
137
138         flresize(screen->clipr);
139         for(i = 0; i<nname; i++)
140                 if(text[i])
141                         hcheck(text[i]->tag);
142 }
143
144 void
145 current(Flayer *nw)
146 {
147         Text *t;
148
149         if(which)
150                 flborder(which, 0);
151         if(nw){
152                 flushtyping(1);
153                 flupfront(nw);
154                 flborder(nw, 1);
155                 buttons(Up);
156                 t = (Text *)nw->user1;
157                 t->front = nw-&t->l[0];
158                 if(t != &cmd)
159                         work = nw;
160         }
161         which = nw;
162 }
163
164 void
165 closeup(Flayer *l)
166 {
167         Text *t=(Text *)l->user1;
168         int m;
169
170         m = whichmenu(t->tag);
171         if(m < 0)
172                 return;
173         flclose(l);
174         if(l == which){
175                 which = 0;
176                 current(flwhich(Pt(0, 0)));
177         }
178         if(l == work)
179                 work = 0;
180         if(--t->nwin == 0){
181                 rclear(&t->rasp);
182                 free((uchar *)t);
183                 text[m] = 0;
184         }else if(l == &t->l[t->front]){
185                 for(m=0; m<NL; m++)     /* find one; any one will do */
186                         if(t->l[m].textfn){
187                                 t->front = m;
188                                 return;
189                         }
190                 panic("close");
191         }
192 }
193
194 Flayer *
195 findl(Text *t)
196 {
197         int i;
198         for(i = 0; i<NL; i++)
199                 if(t->l[i].textfn==0)
200                         return &t->l[i];
201         return 0;
202 }
203
204 void
205 duplicate(Flayer *l, Rectangle r, Font *f, int close)
206 {
207         Text *t=(Text *)l->user1;
208         Flayer *nl = findl(t);
209         Rune *rp;
210         ulong n;
211
212         if(nl){
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);
219                 if(close){
220                         flclose(l);
221                         if(l==which)
222                                 which = 0;
223                 }else
224                         t->nwin++;
225                 current(nl);
226                 hcheck(t->tag);
227         }
228         setcursor(mousectl, cursor);
229 }
230
231 void
232 buttons(int updown)
233 {
234         while(((mousep->buttons&7)!=0) != updown)
235                 getmouse();
236 }
237
238 int
239 getr(Rectangle *rp)
240 {
241         Point p;
242         Rectangle r;
243
244         *rp = getrect(3, mousectl);
245         if(rp->max.x && rp->max.x-rp->min.x<=5 && rp->max.y-rp->min.y<=5){
246                 p = rp->min;
247                 r = cmd.l[cmd.front].entire;
248                 *rp = screen->r;
249                 if(cmd.nwin==1){
250                         if (p.y <= r.min.y)
251                                 rp->max.y = r.min.y;
252                         else if (p.y >= r.max.y)
253                                 rp->min.y = r.max.y;
254                         if (p.x <= r.min.x)
255                                 rp->max.x = r.min.x;
256                         else if (p.x >= r.max.x)
257                                 rp->min.x = r.max.x;
258                 }
259         }
260         return rectclip(rp, screen->r) &&
261            rp->max.x-rp->min.x>100 && rp->max.y-rp->min.y>40;
262 }
263
264 void
265 snarf(Text *t, int w)
266 {
267         Flayer *l = &t->l[w];
268
269         if(l->p1>l->p0){
270                 snarflen = l->p1-l->p0;
271                 outTsll(Tsnarf, t->tag, l->p0, l->p1);
272         }
273 }
274
275 void
276 cut(Text *t, int w, int save, int check)
277 {
278         long p0, p1;
279         Flayer *l;
280
281         l = &t->l[w];
282         p0 = l->p0;
283         p1 = l->p1;
284         if(p0 == p1)
285                 return;
286         if(p0 < 0)
287                 panic("cut");
288         if(save)
289                 snarf(t, w);
290         outTsll(Tcut, t->tag, p0, p1);
291         flsetselect(l, p0, p0);
292         t->lock++;
293         hcut(t->tag, p0, p1-p0);
294         if(check)
295                 hcheck(t->tag);
296 }
297
298 void
299 paste(Text *t, int w)
300 {
301         if(snarflen){
302                 cut(t, w, 0, 0);
303                 t->lock++;
304                 outTsl(Tpaste, t->tag, t->l[w].p0);
305         }
306 }
307
308 void
309 scrorigin(Flayer *l, int but, long p0)
310 {
311         Text *t=(Text *)l->user1;
312
313         if(t->tag == Untagged)
314                 return;
315
316         switch(but){
317         case 1:
318                 outTsll(Torigin, t->tag, l->origin, p0);
319                 break;
320         case 2:
321                 outTsll(Torigin, t->tag, p0, 1L);
322                 break;
323         case 3:
324                 horigin(t->tag,p0);
325         }
326 }
327
328 int
329 alnum(int c)
330 {
331         /*
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.
335          */
336         if(c<=' ')
337                 return 0;
338         if(0x7F<=c && c<=0xA0)
339                 return 0;
340         if(utfrune("!\"#$%&'()*+,-./:;<=>?@[\\]^`{|}~", c))
341                 return 0;
342         return 1;
343 }
344
345 int
346 raspc(Rasp *r, long p)
347 {
348         ulong n;
349         rload(r, p, p+1, &n);
350         if(n)
351                 return scratch[0];
352         return 0;
353 }
354
355 int
356 getcol(Rasp *r, long p)
357 {
358         int col;
359
360         for(col = 0; p > 0 && raspc(r, p-1)!='\n'; p--, col++)
361                 ;
362         return col;
363 }
364
365 long
366 del(Rasp *r, long o, long p)
367 {
368         int i, col, n;
369
370         if(--p < o)
371                 return o;
372         if(!spacesindent || raspc(r, p)!=' ')
373                 return p;
374         col = getcol(r, p) + 1;
375         if((n = col % maxtab) == 0)
376                 n = maxtab;
377         for(i = 0; p-1>=o && raspc(r, p-1)==' ' && i<n-1; --p, i++)
378                 ;
379         return p>=o? p : o;
380 }
381
382 long
383 ctlw(Rasp *r, long o, long p)
384 {
385         int c;
386
387         if(--p < o)
388                 return o;
389         if(raspc(r, p)=='\n')
390                 return p;
391         for(; p>=o && !alnum(c=raspc(r, p)); --p)
392                 if(c=='\n')
393                         return p+1;
394         for(; p>o && alnum(raspc(r, p-1)); --p)
395                 ;
396         return p>=o? p : o;
397 }
398
399 long
400 ctlu(Rasp *r, long o, long p)
401 {
402         if(--p < o)
403                 return o;
404         if(raspc(r, p)=='\n')
405                 return p;
406         for(; p-1>=o && raspc(r, p-1)!='\n'; --p)
407                 ;
408         return p>=o? p : o;
409 }
410
411 int
412 center(Flayer *l, long a)
413 {
414         Text *t;
415
416         t = l->user1;
417         if(!t->lock && (a<l->origin || l->origin+l->f.nchars<a)){
418                 if(a > t->rasp.nrunes)
419                         a = t->rasp.nrunes;
420                 outTsll(Torigin, t->tag, a, 2L);
421                 return 1;
422         }
423         return 0;
424 }
425
426 int
427 onethird(Flayer *l, long a)
428 {
429         Text *t;
430         Rectangle s;
431         long lines;
432
433         t = l->user1;
434         if(!t->lock && (a<l->origin || l->origin+l->f.nchars<a)){
435                 if(a > t->rasp.nrunes)
436                         a = t->rasp.nrunes;
437                 s = insetrect(l->scroll, 1);
438                 lines = ((s.max.y-s.min.y)/l->f.font->height+1)/3;
439                 if (lines < 2)
440                         lines = 2;
441                 outTsll(Torigin, t->tag, a, lines);
442                 return 1;
443         }
444         return 0;
445 }
446
447 void
448 flushtyping(int clearesc)
449 {
450         Text *t;
451         ulong n;
452
453         if(clearesc)
454                 typeesc = -1;   
455         if(typestart == typeend) {
456                 modified = 0;
457                 return;
458         }
459         t = which->user1;
460         if(t != &cmd)
461                 modified = 1;
462         rload(&t->rasp, typestart, typeend, &n);
463         scratch[n] = 0;
464         if(t==&cmd && typeend==t->rasp.nrunes && scratch[typeend-typestart-1]=='\n'){
465                 setlock();
466                 outcmd();
467         }
468         outTslS(Ttype, t->tag, typestart, scratch);
469         typestart = -1;
470         typeend = -1;
471 }
472
473 int
474 nontypingkey(int c)
475 {
476         switch(c){
477         case Kup:
478         case Kdown:
479         case Khome:
480         case Kend:
481         case Kpgdown:
482         case Kpgup:
483         case Kleft:
484         case Kright:
485         case Ksoh:
486         case Kenq:
487         case Kstx:
488                 return 1;
489         }
490         return 0;
491 }
492
493
494 void
495 type(Flayer *l, int res)        /* what a bloody mess this is */
496 {
497         Text *t = (Text *)l->user1;
498         Rune buf[100];
499         Rune *p = buf;
500         int c, backspacing;
501         long a, a0;
502         int scrollkey;
503
504         scrollkey = 0;
505         if(res == RKeyboard)
506                 scrollkey = nontypingkey(qpeekc());     /* ICK */
507
508         if(hostlock || t->lock){
509                 kbdblock();
510                 return;
511         }
512         a = l->p0;
513         if(a!=l->p1 && !scrollkey){
514                 flushtyping(1);
515                 cut(t, t->front, 1, 1);
516                 return; /* it may now be locked */
517         }
518         backspacing = 0;
519         while((c = kbdchar())>0){
520                 if(res == RKeyboard){
521                         if(nontypingkey(c) || c==Kesc)
522                                 break;
523                         /* backspace, ctrl-u, ctrl-w, del */
524                         if(c==Kbs || c==Knack || c==Ketb || c==Kdel){
525                                 backspacing = 1;
526                                 break;
527                         }
528                 }
529                 if(spacesindent && c == '\t'){
530                         int i, col, n;
531                         col = getcol(&t->rasp, a);
532                         n = maxtab - col % maxtab;
533                         for(i = 0; i < n && p < buf+nelem(buf); i++)
534                                 *p++ = ' ';
535                 } else
536                         *p++ = c;
537                 if(autoindent)
538                 if(c == '\n'){
539                         /* autoindent */
540                         int cursor, ch;
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')
545                                         *p++ = ch;
546                                 else
547                                         break;
548                         }
549                 }
550                 if(c == '\n' || p >= buf+sizeof(buf)/sizeof(buf[0]))
551                         break;
552         }
553         if(p > buf){
554                 if(typestart < 0)
555                         typestart = a;
556                 if(typeesc < 0)
557                         typeesc = a;
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);
561                 a += p-buf;
562                 l->p0 = a;
563                 l->p1 = a;
564                 typeend = a;
565                 if(c=='\n' || typeend-typestart>100)
566                         flushtyping(0);
567                 onethird(l, a);
568         }
569         if(c==Kdown || c==Kpgdown){
570                 flushtyping(0);
571                 center(l, l->origin+l->f.nchars+1);
572                 /* backspacing immediately after outcmd(): sorry */
573         }else if(c==Kup || c==Kpgup){
574                 flushtyping(0);
575                 a0 = l->origin-l->f.nchars;
576                 if(a0 < 0)
577                         a0 = 0;
578                 center(l, a0);
579         }else if(c == Kright){
580                 flushtyping(0);
581                 a0 = l->p0;
582                 if(a0 < t->rasp.nrunes)
583                         a0++;
584                 flsetselect(l, a0, a0);
585                 center(l, a0);
586         }else if(c == Kleft){
587                 flushtyping(0);
588                 a0 = l->p0;
589                 if(a0 > 0)
590                         a0--;
591                 flsetselect(l, a0, a0);
592                 center(l, a0);
593         }else if(c == Khome){
594                 flushtyping(0);
595                 center(l, 0);
596         }else if(c == Kend){
597                 flushtyping(0);
598                 center(l, t->rasp.nrunes);
599         }else if(c == Ksoh || c == Kenq){
600                 flushtyping(1);
601                 if(c == Ksoh)
602                         while(a > 0 && raspc(&t->rasp, a-1)!='\n')
603                                 a--;
604                 else
605                         while(a < t->rasp.nrunes && raspc(&t->rasp, a)!='\n')
606                                 a++;
607                 l->p0 = l->p1 = a;
608                 for(l=t->l; l<&t->l[NL]; l++)
609                         if(l->textfn)
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){
614                         switch(c){
615                         case Kbs:
616                         case Kdel:      /* del */
617                                 l->p0 = del(&t->rasp, l->origin, a);
618                                 break;
619                         case Knack:     /* ctrl-u */
620                                 l->p0 = ctlu(&t->rasp, l->origin, a);
621                                 break;
622                         case Ketb:      /* ctrl-w */
623                                 l->p0 = ctlw(&t->rasp, l->origin, a);
624                                 break;
625                         }
626                         l->p1 = a;
627                         if(l->p1 != l->p0){
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 */
633                                         hcheck(t->tag);
634                                 }else{
635                                         flushtyping(0);
636                                         cut(t, t->front, 0, 1);
637                                 }
638                         }
639                         if(typeesc >= l->p0)
640                                 typeesc = l->p0;
641                         if(typestart >= 0){
642                                 if(typestart >= l->p0)
643                                         typestart = l->p0;
644                                 typeend = l->p0;
645                                 if(typestart == typeend){
646                                         typestart = -1;
647                                         typeend = -1;
648                                         modified = 0;
649                                 }
650                         }
651                 }
652         }else if(c == Kstx){
653                 t = &cmd;
654                 for(l=t->l; l->textfn==0; l++)
655                         ;
656                 current(l);
657                 flushtyping(0);
658                 a = t->rasp.nrunes;
659                 flsetselect(l, a, a);
660                 center(l, a);
661         }else{
662                 if(c==Kesc && typeesc>=0){
663                         l->p0 = typeesc;
664                         l->p1 = a;
665                         flushtyping(1);
666                 }
667                 for(l=t->l; l<&t->l[NL]; l++)
668                         if(l->textfn)
669                                 flsetselect(l, l->p0, l->p1);
670         }
671 }
672
673
674 void
675 outcmd(void){
676         if(work)
677                 outTsll(Tworkfile, ((Text *)work->user1)->tag, work->p0, work->p1);
678 }
679
680 void
681 panic(char *s)
682 {
683         panic1(display, s);
684 }
685
686 void
687 panic1(Display*, char *s)
688 {
689         fprint(2, "samterm:panic: ");
690         perror(s);
691         abort();
692 }
693
694 Rune*
695 gettext(Flayer *l, long n, ulong *np)
696 {
697         Text *t;
698
699         t = l->user1;
700         rload(&t->rasp, l->origin, l->origin+n, np);
701         return scratch;
702 }
703
704 long
705 scrtotal(Flayer *l)
706 {
707         return ((Text *)l->user1)->rasp.nrunes;
708 }
709
710 void*
711 alloc(ulong n)
712 {
713         void *p;
714
715         p = malloc(n);
716         if(p == 0)
717                 panic("alloc");
718         memset(p, 0, n);
719         return p;
720 }