Blob


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 <fcall.h>
10 #include <bio.h>
11 #include <plumb.h>
12 #include "dat.h"
13 #include "fns.h"
15 static Rune Lcolhdr[] = {
16 'N', 'e', 'w', 'c', 'o', 'l', ' ',
17 'K', 'i', 'l', 'l', ' ',
18 'P', 'u', 't', 'a', 'l', 'l', ' ',
19 'D', 'u', 'm', 'p', ' ',
20 'E', 'x', 'i', 't', ' ',
21 0
22 };
24 void
25 rowinit(Row *row, Rectangle r)
26 {
27 Rectangle r1;
28 Text *t;
30 draw(screen, r, display->white, nil, ZP);
31 row->r = r;
32 row->col = nil;
33 row->ncol = 0;
34 r1 = r;
35 r1.max.y = r1.min.y + font->height;
36 t = &row->tag;
37 textinit(t, fileaddtext(nil, t), r1, rfget(FALSE, FALSE, FALSE, nil), tagcols);
38 t->what = Rowtag;
39 t->row = row;
40 t->w = nil;
41 t->col = nil;
42 r1.min.y = r1.max.y;
43 r1.max.y += Border;
44 draw(screen, r1, display->black, nil, ZP);
45 textinsert(t, 0, Lcolhdr, 29, TRUE);
46 textsetselect(t, t->file->b.nc, t->file->b.nc);
47 }
49 Column*
50 rowadd(Row *row, Column *c, int x)
51 {
52 Rectangle r, r1;
53 Column *d;
54 int i;
56 d = nil;
57 r = row->r;
58 r.min.y = row->tag.fr.r.max.y+Border;
59 if(x<r.min.x && row->ncol>0){ /*steal 40% of last column by default */
60 d = row->col[row->ncol-1];
61 x = d->r.min.x + 3*Dx(d->r)/5;
62 }
63 /* look for column we'll land on */
64 for(i=0; i<row->ncol; i++){
65 d = row->col[i];
66 if(x < d->r.max.x)
67 break;
68 }
69 if(row->ncol > 0){
70 if(i < row->ncol)
71 i++; /* new column will go after d */
72 r = d->r;
73 if(Dx(r) < 100)
74 return nil;
75 draw(screen, r, display->white, nil, ZP);
76 r1 = r;
77 r1.max.x = min(x-Border, r.max.x-50);
78 if(Dx(r1) < 50)
79 r1.max.x = r1.min.x+50;
80 colresize(d, r1);
81 r1.min.x = r1.max.x;
82 r1.max.x = r1.min.x+Border;
83 draw(screen, r1, display->black, nil, ZP);
84 r.min.x = r1.max.x;
85 }
86 if(c == nil){
87 c = emalloc(sizeof(Column));
88 colinit(c, r);
89 incref(&reffont.ref);
90 }else
91 colresize(c, r);
92 c->row = row;
93 c->tag.row = row;
94 row->col = realloc(row->col, (row->ncol+1)*sizeof(Column*));
95 memmove(row->col+i+1, row->col+i, (row->ncol-i)*sizeof(Column*));
96 row->col[i] = c;
97 row->ncol++;
98 clearmouse();
99 return c;
102 void
103 rowresize(Row *row, Rectangle r)
105 int i, dx, odx;
106 Rectangle r1, r2;
107 Column *c;
109 dx = Dx(r);
110 odx = Dx(row->r);
111 row->r = r;
112 r1 = r;
113 r1.max.y = r1.min.y + font->height;
114 textresize(&row->tag, r1, TRUE);
115 r1.min.y = r1.max.y;
116 r1.max.y += Border;
117 draw(screen, r1, display->black, nil, ZP);
118 r.min.y = r1.max.y;
119 r1 = r;
120 r1.max.x = r1.min.x;
121 for(i=0; i<row->ncol; i++){
122 c = row->col[i];
123 r1.min.x = r1.max.x;
124 if(i == row->ncol-1)
125 r1.max.x = r.max.x;
126 else
127 r1.max.x = r1.min.x+Dx(c->r)*dx/odx;
128 if(i > 0){
129 r2 = r1;
130 r2.max.x = r2.min.x+Border;
131 draw(screen, r2, display->black, nil, ZP);
132 r1.min.x = r2.max.x;
134 colresize(c, r1);
138 void
139 rowdragcol(Row *row, Column *c, int _0)
141 Rectangle r;
142 int i, b, x;
143 Point p, op;
144 Column *d;
146 USED(_0);
148 clearmouse();
149 setcursor(mousectl, &boxcursor);
150 b = mouse->buttons;
151 op = mouse->xy;
152 while(mouse->buttons == b)
153 readmouse(mousectl);
154 setcursor(mousectl, nil);
155 if(mouse->buttons){
156 while(mouse->buttons)
157 readmouse(mousectl);
158 return;
161 for(i=0; i<row->ncol; i++)
162 if(row->col[i] == c)
163 goto Found;
164 error("can't find column");
166 Found:
167 if(i == 0)
168 return;
169 p = mouse->xy;
170 if((abs(p.x-op.x)<5 && abs(p.y-op.y)<5))
171 return;
172 if((i>0 && p.x<row->col[i-1]->r.min.x) || (i<row->ncol-1 && p.x>c->r.max.x)){
173 /* shuffle */
174 x = c->r.min.x;
175 rowclose(row, c, FALSE);
176 if(rowadd(row, c, p.x) == nil) /* whoops! */
177 if(rowadd(row, c, x) == nil) /* WHOOPS! */
178 if(rowadd(row, c, -1)==nil){ /* shit! */
179 rowclose(row, c, TRUE);
180 return;
182 colmousebut(c);
183 return;
185 d = row->col[i-1];
186 if(p.x < d->r.min.x+80+Scrollwid)
187 p.x = d->r.min.x+80+Scrollwid;
188 if(p.x > c->r.max.x-80-Scrollwid)
189 p.x = c->r.max.x-80-Scrollwid;
190 r = d->r;
191 r.max.x = c->r.max.x;
192 draw(screen, r, display->white, nil, ZP);
193 r.max.x = p.x;
194 colresize(d, r);
195 r = c->r;
196 r.min.x = p.x;
197 r.max.x = r.min.x;
198 r.max.x += Border;
199 draw(screen, r, display->black, nil, ZP);
200 r.min.x = r.max.x;
201 r.max.x = c->r.max.x;
202 colresize(c, r);
203 colmousebut(c);
206 void
207 rowclose(Row *row, Column *c, int dofree)
209 Rectangle r;
210 int i;
212 for(i=0; i<row->ncol; i++)
213 if(row->col[i] == c)
214 goto Found;
215 error("can't find column");
216 Found:
217 r = c->r;
218 if(dofree)
219 colcloseall(c);
220 memmove(row->col+i, row->col+i+1, (row->ncol-i)*sizeof(Column*));
221 row->ncol--;
222 row->col = realloc(row->col, row->ncol*sizeof(Column*));
223 if(row->ncol == 0){
224 draw(screen, r, display->white, nil, ZP);
225 return;
227 if(i == row->ncol){ /* extend last column right */
228 c = row->col[i-1];
229 r.min.x = c->r.min.x;
230 r.max.x = row->r.max.x;
231 }else{ /* extend next window left */
232 c = row->col[i];
233 r.max.x = c->r.max.x;
235 draw(screen, r, display->white, nil, ZP);
236 colresize(c, r);
239 Column*
240 rowwhichcol(Row *row, Point p)
242 int i;
243 Column *c;
245 for(i=0; i<row->ncol; i++){
246 c = row->col[i];
247 if(ptinrect(p, c->r))
248 return c;
250 return nil;
253 Text*
254 rowwhich(Row *row, Point p)
256 Column *c;
258 if(ptinrect(p, row->tag.all))
259 return &row->tag;
260 c = rowwhichcol(row, p);
261 if(c)
262 return colwhich(c, p);
263 return nil;
266 Text*
267 rowtype(Row *row, Rune r, Point p)
269 Window *w;
270 Text *t;
272 clearmouse();
273 qlock(&row->lk);
274 if(bartflag)
275 t = barttext;
276 else
277 t = rowwhich(row, p);
278 if(t!=nil && !(t->what==Tag && ptinrect(p, t->scrollr))){
279 w = t->w;
280 if(w == nil)
281 texttype(t, r);
282 else{
283 winlock(w, 'K');
284 wintype(w, t, r);
285 /*
286 * TAG If we typed in the tag, might need to make it
287 * bigger to show text. \n causes tag to expand.
288 */
289 if(t->what == Tag){
290 t->w->tagsafe = FALSE;
291 if(r == '\n')
292 t->w->tagexpand = TRUE;
293 winresize(w, w->r, TRUE, TRUE);
295 /* END TAG */
296 winunlock(w);
299 qunlock(&row->lk);
300 return t;
303 int
304 rowclean(Row *row)
306 int clean;
307 int i;
309 clean = TRUE;
310 for(i=0; i<row->ncol; i++)
311 clean &= colclean(row->col[i]);
312 return clean;
315 void
316 rowdump(Row *row, char *file)
318 int i, j, fd, m, n, dumped;
319 uint q0, q1;
320 Biobuf *b;
321 char *buf, *a, *fontname;
322 Rune *r, *rp;
323 Column *c;
324 Window *w, *w1;
325 Text *t;
327 if(row->ncol == 0)
328 return;
329 buf = fbufalloc();
330 if(file == nil){
331 if(home == nil){
332 warning(nil, "can't find file for dump: $home not defined\n");
333 goto Rescue;
335 sprint(buf, "%s/acme.dump", home);
336 file = buf;
338 fd = create(file, OWRITE, 0600);
339 if(fd < 0){
340 warning(nil, "can't open %s: %r\n", file);
341 goto Rescue;
343 b = emalloc(sizeof(Biobuf));
344 Binit(b, fd, OWRITE);
345 r = fbufalloc();
346 Bprint(b, "%s\n", wdir);
347 Bprint(b, "%s\n", fontnames[0]);
348 Bprint(b, "%s\n", fontnames[1]);
349 for(i=0; i<row->ncol; i++){
350 c = row->col[i];
351 Bprint(b, "%11.7f", 100.0*(c->r.min.x-row->r.min.x)/Dx(row->r));
352 if(i == row->ncol-1)
353 Bputc(b, '\n');
354 else
355 Bputc(b, ' ');
357 for(i=0; i<row->ncol; i++){
358 c = row->col[i];
359 for(j=0; j<c->nw; j++)
360 c->w[j]->body.file->dumpid = 0;
362 for(i=0; i<row->ncol; i++){
363 c = row->col[i];
364 for(j=0; j<c->nw; j++){
365 w = c->w[j];
366 wincommit(w, &w->tag);
367 t = &w->body;
368 /* windows owned by others get special treatment */
369 if(w->nopen[QWevent] > 0)
370 if(w->dumpstr == nil)
371 continue;
372 /* zeroxes of external windows are tossed */
373 if(t->file->ntext > 1)
374 for(n=0; n<t->file->ntext; n++){
375 w1 = t->file->text[n]->w;
376 if(w == w1)
377 continue;
378 if(w1->nopen[QWevent])
379 goto Continue2;
381 fontname = "";
382 if(t->reffont->f != font)
383 fontname = t->reffont->f->name;
384 if(t->file->nname)
385 a = runetobyte(t->file->name, t->file->nname);
386 else
387 a = emalloc(1);
388 if(t->file->dumpid){
389 dumped = FALSE;
390 Bprint(b, "x%11d %11d %11d %11d %11.7f %s\n", i, t->file->dumpid,
391 w->body.q0, w->body.q1,
392 100.0*(w->r.min.y-c->r.min.y)/Dy(c->r),
393 fontname);
394 }else if(w->dumpstr){
395 dumped = FALSE;
396 Bprint(b, "e%11d %11d %11d %11d %11.7f %s\n", i, t->file->dumpid,
397 0, 0,
398 100.0*(w->r.min.y-c->r.min.y)/Dy(c->r),
399 fontname);
400 }else if((w->dirty==FALSE && access(a, 0)==0) || w->isdir){
401 dumped = FALSE;
402 t->file->dumpid = w->id;
403 Bprint(b, "f%11d %11d %11d %11d %11.7f %s\n", i, w->id,
404 w->body.q0, w->body.q1,
405 100.0*(w->r.min.y-c->r.min.y)/Dy(c->r),
406 fontname);
407 }else{
408 dumped = TRUE;
409 t->file->dumpid = w->id;
410 Bprint(b, "F%11d %11d %11d %11d %11.7f %11d %s\n", i, j,
411 w->body.q0, w->body.q1,
412 100.0*(w->r.min.y-c->r.min.y)/Dy(c->r),
413 w->body.file->b.nc, fontname);
415 free(a);
416 winctlprint(w, buf, 0);
417 Bwrite(b, buf, strlen(buf));
418 m = min(RBUFSIZE, w->tag.file->b.nc);
419 bufread(&w->tag.file->b, 0, r, m);
420 if(dodollarsigns && r[0] == '$'){
421 rp = runestrdup(r);
422 expandenv(&rp, (uint*)&m);
423 }else
424 rp = r;
425 n = 0;
426 while(n<m && rp[n]!='\n')
427 n++;
428 Bprint(b, "%.*S\n", n, rp);
429 if(rp != r)
430 free(rp);
431 if(dumped){
432 q0 = 0;
433 q1 = t->file->b.nc;
434 while(q0 < q1){
435 n = q1 - q0;
436 if(n > BUFSIZE/UTFmax)
437 n = BUFSIZE/UTFmax;
438 bufread(&t->file->b, q0, r, n);
439 Bprint(b, "%.*S", n, r);
440 q0 += n;
443 if(w->dumpstr){
444 if(w->dumpdir)
445 Bprint(b, "%s\n%s\n", w->dumpdir, w->dumpstr);
446 else
447 Bprint(b, "\n%s\n", w->dumpstr);
449 Continue2:;
452 Bterm(b);
453 close(fd);
454 free(b);
455 fbuffree(r);
457 Rescue:
458 fbuffree(buf);
461 static
462 char*
463 rdline(Biobuf *b, int *linep)
465 char *l;
467 l = Brdline(b, '\n');
468 if(l)
469 (*linep)++;
470 return l;
473 /*
474 * Get font names from load file so we don't load fonts we won't use
475 */
476 void
477 rowloadfonts(char *file)
479 int i;
480 Biobuf *b;
481 char *l;
483 b = Bopen(file, OREAD);
484 if(b == nil)
485 return;
486 /* current directory */
487 l = Brdline(b, '\n');
488 if(l == nil)
489 goto Return;
490 /* global fonts */
491 for(i=0; i<2; i++){
492 l = Brdline(b, '\n');
493 if(l == nil)
494 goto Return;
495 l[Blinelen(b)-1] = 0;
496 if(*l && strcmp(l, fontnames[i])!=0){
497 free(fontnames[i]);
498 fontnames[i] = estrdup(l);
501 Return:
502 Bterm(b);
505 int
506 rowload(Row *row, char *file, int initing)
508 int i, j, line, y, nr, nfontr, n, ns, ndumped, dumpid, x, fd;
509 double percent;
510 Biobuf *b, *bout;
511 char *buf, *l, *t, *fontname;
512 Rune *r, rune, *fontr;
513 Column *c, *c1, *c2;
514 uint q0, q1;
515 Rectangle r1, r2;
516 Window *w;
518 buf = fbufalloc();
519 if(file == nil){
520 if(home == nil){
521 warning(nil, "can't find file for load: $home not defined\n");
522 goto Rescue1;
524 sprint(buf, "%s/acme.dump", home);
525 file = buf;
527 b = Bopen(file, OREAD);
528 if(b == nil){
529 warning(nil, "can't open load file %s: %r\n", file);
530 goto Rescue1;
532 /* current directory */
533 line = 0;
534 l = rdline(b, &line);
535 if(l == nil)
536 goto Rescue2;
537 l[Blinelen(b)-1] = 0;
538 if(chdir(l) < 0){
539 warning(nil, "can't chdir %s\n", l);
540 goto Rescue2;
542 /* global fonts */
543 for(i=0; i<2; i++){
544 l = rdline(b, &line);
545 if(l == nil)
546 goto Rescue2;
547 l[Blinelen(b)-1] = 0;
548 if(*l && strcmp(l, fontnames[i])!=0)
549 rfget(i, TRUE, i==0 && initing, l);
551 if(initing && row->ncol==0)
552 rowinit(row, screen->clipr);
553 l = rdline(b, &line);
554 if(l == nil)
555 goto Rescue2;
556 j = Blinelen(b)/12;
557 if(j<=0 || j>10)
558 goto Rescue2;
559 for(i=0; i<j; i++){
560 percent = atof(l+i*12);
561 if(percent<0 || percent>=100)
562 goto Rescue2;
563 x = row->r.min.x+percent*Dx(row->r)/100+0.5;
564 if(i < row->ncol){
565 if(i == 0)
566 continue;
567 c1 = row->col[i-1];
568 c2 = row->col[i];
569 r1 = c1->r;
570 r2 = c2->r;
571 if(x<Border)
572 x = Border;
573 r1.max.x = x-Border;
574 r2.min.x = x;
575 if(Dx(r1) < 50 || Dx(r2) < 50)
576 continue;
577 draw(screen, Rpt(r1.min, r2.max), display->white, nil, ZP);
578 colresize(c1, r1);
579 colresize(c2, r2);
580 r2.min.x = x-Border;
581 r2.max.x = x;
582 draw(screen, r2, display->black, nil, ZP);
584 if(i >= row->ncol)
585 rowadd(row, nil, x);
587 for(;;){
588 l = rdline(b, &line);
589 if(l == nil)
590 break;
591 dumpid = 0;
592 switch(l[0]){
593 case 'e':
594 if(Blinelen(b) < 1+5*12+1)
595 goto Rescue2;
596 l = rdline(b, &line); /* ctl line; ignored */
597 if(l == nil)
598 goto Rescue2;
599 l = rdline(b, &line); /* directory */
600 if(l == nil)
601 goto Rescue2;
602 l[Blinelen(b)-1] = 0;
603 if(*l == '\0'){
604 if(home == nil)
605 r = bytetorune("./", &nr);
606 else{
607 t = emalloc(strlen(home)+1+1);
608 sprint(t, "%s/", home);
609 r = bytetorune(t, &nr);
610 free(t);
612 }else
613 r = bytetorune(l, &nr);
614 l = rdline(b, &line); /* command */
615 if(l == nil)
616 goto Rescue2;
617 t = emalloc(Blinelen(b)+1);
618 memmove(t, l, Blinelen(b));
619 run(nil, t, r, nr, TRUE, nil, nil, FALSE);
620 /* r is freed in run() */
621 continue;
622 case 'f':
623 if(Blinelen(b) < 1+5*12+1)
624 goto Rescue2;
625 fontname = l+1+5*12;
626 ndumped = -1;
627 break;
628 case 'F':
629 if(Blinelen(b) < 1+6*12+1)
630 goto Rescue2;
631 fontname = l+1+6*12;
632 ndumped = atoi(l+1+5*12+1);
633 break;
634 case 'x':
635 if(Blinelen(b) < 1+5*12+1)
636 goto Rescue2;
637 fontname = l+1+5*12;
638 ndumped = -1;
639 dumpid = atoi(l+1+1*12);
640 break;
641 default:
642 goto Rescue2;
644 l[Blinelen(b)-1] = 0;
645 fontr = nil;
646 nfontr = 0;
647 if(*fontname)
648 fontr = bytetorune(fontname, &nfontr);
649 i = atoi(l+1+0*12);
650 j = atoi(l+1+1*12);
651 q0 = atoi(l+1+2*12);
652 q1 = atoi(l+1+3*12);
653 percent = atof(l+1+4*12);
654 if(i<0 || i>10)
655 goto Rescue2;
656 if(i > row->ncol)
657 i = row->ncol;
658 c = row->col[i];
659 y = c->r.min.y+(percent*Dy(c->r))/100+0.5;
660 if(y<c->r.min.y || y>=c->r.max.y)
661 y = -1;
662 if(dumpid == 0)
663 w = coladd(c, nil, nil, y);
664 else
665 w = coladd(c, nil, lookid(dumpid, TRUE), y);
666 if(w == nil)
667 continue;
668 w->dumpid = j;
669 l = rdline(b, &line);
670 if(l == nil)
671 goto Rescue2;
672 l[Blinelen(b)-1] = 0;
673 r = bytetorune(l+5*12, &nr);
674 ns = -1;
675 for(n=0; n<nr; n++){
676 if(r[n] == '/')
677 ns = n;
678 if(r[n] == ' ')
679 break;
681 if(dumpid == 0)
682 winsetname(w, r, n);
683 for(; n<nr; n++)
684 if(r[n] == '|')
685 break;
686 wincleartag(w);
687 textinsert(&w->tag, w->tag.file->b.nc, r+n+1, nr-(n+1), TRUE);
688 if(ndumped >= 0){
689 /* simplest thing is to put it in a file and load that */
690 sprint(buf, "/tmp/d%d.%.4sacme", getpid(), getuser());
691 fd = create(buf, OWRITE, 0600);
692 if(fd < 0){
693 free(r);
694 warning(nil, "can't create temp file: %r\n");
695 goto Rescue2;
697 bout = emalloc(sizeof(Biobuf));
698 Binit(bout, fd, OWRITE);
699 for(n=0; n<ndumped; n++){
700 rune = Bgetrune(b);
701 if(rune == '\n')
702 line++;
703 if(rune == (Rune)Beof){
704 free(r);
705 Bterm(bout);
706 free(bout);
707 close(fd);
708 goto Rescue2;
710 Bputrune(bout, rune);
712 Bterm(bout);
713 free(bout);
714 textload(&w->body, 0, buf, 1);
715 remove(buf);
716 close(fd);
717 w->body.file->mod = TRUE;
718 for(n=0; n<w->body.file->ntext; n++)
719 w->body.file->text[n]->w->dirty = TRUE;
720 winsettag(w);
721 }else if(dumpid==0 && r[ns+1]!='+' && r[ns+1]!='-')
722 get(&w->body, nil, nil, FALSE, XXX, nil, 0);
723 if(fontr){
724 fontx(&w->body, nil, nil, 0, 0, fontr, nfontr);
725 free(fontr);
727 free(r);
728 if(q0>w->body.file->b.nc || q1>w->body.file->b.nc || q0>q1)
729 q0 = q1 = 0;
730 textshow(&w->body, q0, q1, 1);
731 w->maxlines = min(w->body.fr.nlines, max(w->maxlines, w->body.fr.maxlines));
733 Bterm(b);
735 fbuffree(buf);
736 return TRUE;
738 Rescue2:
739 warning(nil, "bad load file %s:%d\n", file, line);
740 Bterm(b);
741 Rescue1:
742 fbuffree(buf);
743 return FALSE;
746 void
747 allwindows(void (*f)(Window*, void*), void *arg)
749 int i, j;
750 Column *c;
752 for(i=0; i<row.ncol; i++){
753 c = row.col[i];
754 for(j=0; j<c->nw; j++)
755 (*f)(c->w[j], arg);