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;
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 n = 0;
421 while(n<m && r[n]!='\n')
422 n++;
423 r[n++] = '\n';
424 Bprint(b, "%.*S", n, r);
425 if(dumped){
426 q0 = 0;
427 q1 = t->file->b.nc;
428 while(q0 < q1){
429 n = q1 - q0;
430 if(n > BUFSIZE/UTFmax)
431 n = BUFSIZE/UTFmax;
432 bufread(&t->file->b, q0, r, n);
433 Bprint(b, "%.*S", n, r);
434 q0 += n;
437 if(w->dumpstr){
438 if(w->dumpdir)
439 Bprint(b, "%s\n%s\n", w->dumpdir, w->dumpstr);
440 else
441 Bprint(b, "\n%s\n", w->dumpstr);
443 Continue2:;
446 Bterm(b);
447 close(fd);
448 free(b);
449 fbuffree(r);
451 Rescue:
452 fbuffree(buf);
455 static
456 char*
457 rdline(Biobuf *b, int *linep)
459 char *l;
461 l = Brdline(b, '\n');
462 if(l)
463 (*linep)++;
464 return l;
467 /*
468 * Get font names from load file so we don't load fonts we won't use
469 */
470 void
471 rowloadfonts(char *file)
473 int i;
474 Biobuf *b;
475 char *l;
477 b = Bopen(file, OREAD);
478 if(b == nil)
479 return;
480 /* current directory */
481 l = Brdline(b, '\n');
482 if(l == nil)
483 goto Return;
484 /* global fonts */
485 for(i=0; i<2; i++){
486 l = Brdline(b, '\n');
487 if(l == nil)
488 goto Return;
489 l[Blinelen(b)-1] = 0;
490 if(*l && strcmp(l, fontnames[i])!=0){
491 free(fontnames[i]);
492 fontnames[i] = estrdup(l);
495 Return:
496 Bterm(b);
499 int
500 rowload(Row *row, char *file, int initing)
502 int i, j, line, y, nr, nfontr, n, ns, ndumped, dumpid, x, fd;
503 double percent;
504 Biobuf *b, *bout;
505 char *buf, *l, *t, *fontname;
506 Rune *r, rune, *fontr;
507 Column *c, *c1, *c2;
508 uint q0, q1;
509 Rectangle r1, r2;
510 Window *w;
512 buf = fbufalloc();
513 if(file == nil){
514 if(home == nil){
515 warning(nil, "can't find file for load: $home not defined\n");
516 goto Rescue1;
518 sprint(buf, "%s/acme.dump", home);
519 file = buf;
521 b = Bopen(file, OREAD);
522 if(b == nil){
523 warning(nil, "can't open load file %s: %r\n", file);
524 goto Rescue1;
526 /* current directory */
527 line = 0;
528 l = rdline(b, &line);
529 if(l == nil)
530 goto Rescue2;
531 l[Blinelen(b)-1] = 0;
532 if(chdir(l) < 0){
533 warning(nil, "can't chdir %s\n", l);
534 goto Rescue2;
536 /* global fonts */
537 for(i=0; i<2; i++){
538 l = rdline(b, &line);
539 if(l == nil)
540 goto Rescue2;
541 l[Blinelen(b)-1] = 0;
542 if(*l && strcmp(l, fontnames[i])!=0)
543 rfget(i, TRUE, i==0 && initing, l);
545 if(initing && row->ncol==0)
546 rowinit(row, screen->clipr);
547 l = rdline(b, &line);
548 if(l == nil)
549 goto Rescue2;
550 j = Blinelen(b)/12;
551 if(j<=0 || j>10)
552 goto Rescue2;
553 for(i=0; i<j; i++){
554 percent = atof(l+i*12);
555 if(percent<0 || percent>=100)
556 goto Rescue2;
557 x = row->r.min.x+percent*Dx(row->r)/100+0.5;
558 if(i < row->ncol){
559 if(i == 0)
560 continue;
561 c1 = row->col[i-1];
562 c2 = row->col[i];
563 r1 = c1->r;
564 r2 = c2->r;
565 if(x<Border)
566 x = Border;
567 r1.max.x = x-Border;
568 r2.min.x = x;
569 if(Dx(r1) < 50 || Dx(r2) < 50)
570 continue;
571 draw(screen, Rpt(r1.min, r2.max), display->white, nil, ZP);
572 colresize(c1, r1);
573 colresize(c2, r2);
574 r2.min.x = x-Border;
575 r2.max.x = x;
576 draw(screen, r2, display->black, nil, ZP);
578 if(i >= row->ncol)
579 rowadd(row, nil, x);
581 for(;;){
582 l = rdline(b, &line);
583 if(l == nil)
584 break;
585 dumpid = 0;
586 switch(l[0]){
587 case 'e':
588 if(Blinelen(b) < 1+5*12+1)
589 goto Rescue2;
590 l = rdline(b, &line); /* ctl line; ignored */
591 if(l == nil)
592 goto Rescue2;
593 l = rdline(b, &line); /* directory */
594 if(l == nil)
595 goto Rescue2;
596 l[Blinelen(b)-1] = 0;
597 if(*l == '\0'){
598 if(home == nil)
599 r = bytetorune("./", &nr);
600 else{
601 t = emalloc(strlen(home)+1+1);
602 sprint(t, "%s/", home);
603 r = bytetorune(t, &nr);
604 free(t);
606 }else
607 r = bytetorune(l, &nr);
608 l = rdline(b, &line); /* command */
609 if(l == nil)
610 goto Rescue2;
611 t = emalloc(Blinelen(b)+1);
612 memmove(t, l, Blinelen(b));
613 run(nil, t, r, nr, TRUE, nil, nil, FALSE);
614 /* r is freed in run() */
615 continue;
616 case 'f':
617 if(Blinelen(b) < 1+5*12+1)
618 goto Rescue2;
619 fontname = l+1+5*12;
620 ndumped = -1;
621 break;
622 case 'F':
623 if(Blinelen(b) < 1+6*12+1)
624 goto Rescue2;
625 fontname = l+1+6*12;
626 ndumped = atoi(l+1+5*12+1);
627 break;
628 case 'x':
629 if(Blinelen(b) < 1+5*12+1)
630 goto Rescue2;
631 fontname = l+1+5*12;
632 ndumped = -1;
633 dumpid = atoi(l+1+1*12);
634 break;
635 default:
636 goto Rescue2;
638 l[Blinelen(b)-1] = 0;
639 fontr = nil;
640 nfontr = 0;
641 if(*fontname)
642 fontr = bytetorune(fontname, &nfontr);
643 i = atoi(l+1+0*12);
644 j = atoi(l+1+1*12);
645 q0 = atoi(l+1+2*12);
646 q1 = atoi(l+1+3*12);
647 percent = atof(l+1+4*12);
648 if(i<0 || i>10)
649 goto Rescue2;
650 if(i > row->ncol)
651 i = row->ncol;
652 c = row->col[i];
653 y = c->r.min.y+(percent*Dy(c->r))/100+0.5;
654 if(y<c->r.min.y || y>=c->r.max.y)
655 y = -1;
656 if(dumpid == 0)
657 w = coladd(c, nil, nil, y);
658 else
659 w = coladd(c, nil, lookid(dumpid, TRUE), y);
660 if(w == nil)
661 continue;
662 w->dumpid = j;
663 l = rdline(b, &line);
664 if(l == nil)
665 goto Rescue2;
666 l[Blinelen(b)-1] = 0;
667 r = bytetorune(l+5*12, &nr);
668 ns = -1;
669 for(n=0; n<nr; n++){
670 if(r[n] == '/')
671 ns = n;
672 if(r[n] == ' ')
673 break;
675 if(dumpid == 0)
676 winsetname(w, r, n);
677 for(; n<nr; n++)
678 if(r[n] == '|')
679 break;
680 wincleartag(w);
681 textinsert(&w->tag, w->tag.file->b.nc, r+n+1, nr-(n+1), TRUE);
682 if(ndumped >= 0){
683 /* simplest thing is to put it in a file and load that */
684 sprint(buf, "/tmp/d%d.%.4sacme", getpid(), getuser());
685 fd = create(buf, OWRITE, 0600);
686 if(fd < 0){
687 free(r);
688 warning(nil, "can't create temp file: %r\n");
689 goto Rescue2;
691 bout = emalloc(sizeof(Biobuf));
692 Binit(bout, fd, OWRITE);
693 for(n=0; n<ndumped; n++){
694 rune = Bgetrune(b);
695 if(rune == '\n')
696 line++;
697 if(rune == (Rune)Beof){
698 free(r);
699 Bterm(bout);
700 free(bout);
701 close(fd);
702 goto Rescue2;
704 Bputrune(bout, rune);
706 Bterm(bout);
707 free(bout);
708 textload(&w->body, 0, buf, 1);
709 remove(buf);
710 close(fd);
711 w->body.file->mod = TRUE;
712 for(n=0; n<w->body.file->ntext; n++)
713 w->body.file->text[n]->w->dirty = TRUE;
714 winsettag(w);
715 }else if(dumpid==0 && r[ns+1]!='+' && r[ns+1]!='-')
716 get(&w->body, nil, nil, FALSE, XXX, nil, 0);
717 if(fontr){
718 fontx(&w->body, nil, nil, 0, 0, fontr, nfontr);
719 free(fontr);
721 free(r);
722 if(q0>w->body.file->b.nc || q1>w->body.file->b.nc || q0>q1)
723 q0 = q1 = 0;
724 textshow(&w->body, q0, q1, 1);
725 w->maxlines = min(w->body.fr.nlines, max(w->maxlines, w->body.fr.maxlines));
727 Bterm(b);
729 fbuffree(buf);
730 return TRUE;
732 Rescue2:
733 warning(nil, "bad load file %s:%d\n", file, line);
734 Bterm(b);
735 Rescue1:
736 fbuffree(buf);
737 return FALSE;
740 void
741 allwindows(void (*f)(Window*, void*), void *arg)
743 int i, j;
744 Column *c;
746 for(i=0; i<row.ncol; i++){
747 c = row.col[i];
748 for(j=0; j<c->nw; j++)
749 (*f)(c->w[j], arg);