17 wininit(Window *w, Window *clone, Rectangle r)
38 w->tagtop.max.y = r.min.y + font->height;
40 r1.max.y = r1.min.y + w->taglines*font->height;
42 f = fileaddtext(nil, &w->tag);
43 textinit(&w->tag, f, r1, &reffont, tagcols);
45 /* tag is a copy of the contents, not a tracked image */
47 textdelete(&w->tag, 0, w->tag.file->b.nc, TRUE);
48 nc = clone->tag.file->b.nc;
50 bufread(&clone->tag.file->b, 0, rp, nc);
51 textinsert(&w->tag, 0, rp, nc, TRUE);
53 filereset(w->tag.file);
54 textsetselect(&w->tag, nc, nc);
56 //assert(w->body.w == w);
58 r1.min.y += w->taglines*font->height + 1;
59 if(r1.max.y < r1.min.y)
64 w->body.org = clone->body.org;
65 w->isscratch = clone->isscratch;
66 rf = rfget(FALSE, FALSE, FALSE, clone->body.reffont->f->name);
68 rf = rfget(FALSE, FALSE, FALSE, nil);
69 //assert(w->body.w == w);
70 f = fileaddtext(f, &w->body);
72 textinit(&w->body, f, r1, rf, textcols);
74 r1.max.y = r1.min.y+1;
75 draw(screen, r1, tagcols[BORD], nil, ZP);
76 textscrdraw(&w->body);
78 br.min = w->tag.scrollr.min;
79 br.max.x = br.min.x + Dx(button->r);
80 br.max.y = br.min.y + Dy(button->r);
81 draw(screen, br, button, nil, button->r.min);
83 w->maxlines = w->body.fr.maxlines;
84 w->autoindent = globalautoindent;
85 //assert(w->body.w == w);
87 w->dirty = clone->dirty;
88 w->autoindent = clone->autoindent;
89 textsetselect(&w->body, clone->body.q0, clone->body.q1);
95 * Compute number of tag lines required
96 * to display entire tag text.
99 wintaglines(Window *w, Rectangle r)
104 /* TAG policy here */
108 w->tag.fr.noredraw = 1;
109 textresize(&w->tag, r, TRUE);
110 w->tag.fr.noredraw = 0;
112 /* can't use more than we have */
113 if(w->tag.fr.nlines >= w->tag.fr.maxlines)
114 return w->tag.fr.maxlines;
116 /* if tag ends with \n, include empty line at end for typing */
117 n = w->tag.fr.nlines;
118 if(w->tag.file->b.nc > 0)
119 bufread(&w->tag.file->b, w->tag.file->b.nc-1, &rune, 1);
128 winresize(Window *w, Rectangle r, int safe, int keepextra)
130 int oy, y, mouseintag, tagresized;
135 if(0) fprint(2, "winresize %d %R safe=%d keep=%d h=%d\n", w->id, r, safe, keepextra, font->height);
137 w->tagtop.max.y = r.min.y+font->height;
140 * TAG If necessary, recompute the number of lines that should
144 r1.max.y = min(r.max.y, r1.min.y + w->taglines*font->height);
146 mouseintag = ptinrect(mouse->xy, w->tag.all);
147 if(!safe || !w->tagsafe || !eqrect(w->tag.all, r1)){
149 w->taglines = wintaglines(w, r);
155 r1.max.y = min(r.max.y, r1.min.y + w->taglines*font->height);
158 if(1 || !safe || !eqrect(w->tag.all, r1)){
160 if(0) fprint(2, "resize tag %R => %R\n", w->tag.all, r1);
161 textresize(&w->tag, r1, TRUE);
162 if(0) fprint(2, "=> %R (%R)\n", w->tag.all, w->tag.fr.r);
163 y = w->tag.fr.r.max.y;
165 if(w->body.file->mod && !w->isdir && !w->isscratch)
167 br.min = w->tag.scrollr.min;
168 br.max.x = br.min.x + Dx(b->r);
169 br.max.y = br.min.y + Dy(b->r);
170 draw(screen, br, b, nil, b->r.min);
172 if(mouseintag && !ptinrect(mouse->xy, w->tag.all)){
174 p.y = w->tag.all.max.y-3;
183 if(tagresized || !safe || !eqrect(w->body.all, r1)){
185 if(0) fprint(2, "resizing body; safe=%d all=%R r1=%R\n", safe, w->body.all, r1);
186 if(y+1+w->body.fr.font->height <= r.max.y){ /* room for one line */
189 draw(screen, r1, tagcols[BORD], nil, ZP);
191 r1.min.y = min(y, r.max.y);
197 if(0) fprint(2, "resizing body; new r=%R; r1=%R\n", r, r1);
199 w->r.max.y = textresize(&w->body, r1, keepextra);
200 if(0) fprint(2, "after textresize: body.all=%R\n", w->body.all);
201 textscrdraw(&w->body);
202 w->body.all.min.y = oy;
204 w->maxlines = min(w->body.fr.nlines, max(w->maxlines, w->body.fr.maxlines));
209 winlock1(Window *w, int owner)
217 winlock(Window *w, int owner)
223 for(i=0; i<f->ntext; i++)
224 winlock1(f->text[i]->w, owner);
234 * subtle: loop runs backwards to avoid tripping over
235 * winclose indirectly editing f->text and freeing f
236 * on the last iteration of the loop.
239 for(i=f->ntext-1; i>=0; i--){
248 winmousebut(Window *w)
250 moveto(mousectl, addpt(w->tag.scrollr.min,
251 divpt(Pt(Dx(w->tag.scrollr), font->height), 2)));
255 windirfree(Window *w)
261 for(i=0; i<w->ndl; i++){
277 if(decref(&w->ref) == 0){
283 for(i=0; i<w->nincl; i++)
302 sendp(x->c, nil); /* wake him up */
307 winundo(Window *w, int isundo)
316 fileundo(body->file, isundo, &body->q0, &body->q1);
317 textshow(body, body->q0, body->q1, 1);
319 for(i=0; i<f->ntext; i++){
321 v->dirty = (f->seq != v->putseq);
323 v->body.q0 = v->body.fr.p0+v->body.org;
324 v->body.q1 = v->body.fr.p1+v->body.org;
331 winsetname(Window *w, Rune *name, int n)
336 static Rune Lslashguide[] = { '/', 'g', 'u', 'i', 'd', 'e', 0 };
337 static Rune Lpluserrors[] = { '+', 'E', 'r', 'r', 'o', 'r', 's', 0 };
339 if(runeeq(t->file->name, t->file->nname, name, n) == TRUE)
341 w->isscratch = FALSE;
342 if(n>=6 && runeeq(Lslashguide, 6, name+(n-6), 6))
344 else if(n>=7 && runeeq(Lpluserrors, 7, name+(n-7), 7))
346 filesetname(t->file, name, n);
347 for(i=0; i<t->file->ntext; i++){
348 v = t->file->text[i]->w;
350 v->isscratch = w->isscratch;
355 wintype(Window *w, Text *t, Rune r)
361 for(i=0; i<t->file->ntext; i++)
362 textscrdraw(t->file->text[i]);
367 wincleartag(Window *w)
372 /* w must be committed */
373 n = w->tag.file->b.nc;
375 bufread(&w->tag.file->b, 0, r, n);
377 if(r[i]==' ' || r[i]=='\t')
385 textdelete(&w->tag, i, n, TRUE);
387 w->tag.file->mod = FALSE;
392 textsetselect(&w->tag, w->tag.q0, w->tag.q1);
396 winsettag1(Window *w)
398 int bar, dirty, i, j, k, n, ntagname, resize;
399 Rune *new, *old, *r, *tagname;
403 static Rune Ldelsnarf[] = { ' ', 'D', 'e', 'l', ' ',
404 'S', 'n', 'a', 'r', 'f', 0 };
405 static Rune Lundo[] = { ' ', 'U', 'n', 'd', 'o', 0 };
406 static Rune Lredo[] = { ' ', 'R', 'e', 'd', 'o', 0 };
407 static Rune Lget[] = { ' ', 'G', 'e', 't', 0 };
408 static Rune Lput[] = { ' ', 'P', 'u', 't', 0 };
409 static Rune Llook[] = { ' ', 'L', 'o', 'o', 'k', ' ', 0 };
410 static Rune Lpipe[] = { ' ', '|', 0 };
411 /* there are races that get us here with stuff in the tag cache, so we take extra care to sync it */
412 if(w->tag.ncache!=0 || w->tag.file->mod)
413 wincommit(w, &w->tag); /* check file name; also guarantees we can modify tag contents */
414 old = runemalloc(w->tag.file->b.nc+1);
415 bufread(&w->tag.file->b, 0, old, w->tag.file->b.nc);
416 old[w->tag.file->b.nc] = '\0';
417 for(i=0; i<w->tag.file->b.nc; i++)
418 if(old[i]==' ' || old[i]=='\t')
421 /* make sure the file name is set correctly in the tag */
422 ntagname = w->body.file->nname;
423 tagname = runemalloc(ntagname);
424 runemove(tagname, w->body.file->name, ntagname);
425 abbrevenv(&tagname, (uint*)&ntagname);
428 * XXX Why is this here instead of letting the code
429 * down below do the work?
431 if(runeeq(old, i, tagname, ntagname) == FALSE){
434 textdelete(&w->tag, 0, i, TRUE);
435 textinsert(&w->tag, 0, tagname, ntagname, TRUE);
437 old = runemalloc(w->tag.file->b.nc+1);
438 bufread(&w->tag.file->b, 0, old, w->tag.file->b.nc);
439 old[w->tag.file->b.nc] = '\0';
442 * XXX common case - typing at end of name
444 w->tag.q0 = q0+ntagname-i;
445 w->tag.q1 = q1+ntagname-i;
449 /* compute the text for the whole tag, replacing current only if it differs */
450 new = runemalloc(ntagname+100);
452 runemove(new+i, tagname, ntagname);
454 runemove(new+i, Ldelsnarf, 10);
457 if(w->body.needundo || w->body.file->delta.nc>0 || w->body.ncache){
458 runemove(new+i, Lundo, 5);
461 if(w->body.file->epsilon.nc > 0){
462 runemove(new+i, Lredo, 5);
465 dirty = w->body.file->nname && (w->body.ncache || w->body.file->seq!=w->putseq);
466 if(!w->isdir && dirty){
467 runemove(new+i, Lput, 4);
472 runemove(new+i, Lget, 4);
475 runemove(new+i, Lpipe, 2);
477 r = runestrchr(old, '|');
481 k = w->tag.file->b.nc;
482 if(w->body.file->seq == 0){
483 runemove(new+i, Llook, 6);
488 if(runestrlen(new) != i)
489 fprint(2, "s '%S' len not %d\n", new, i);
490 assert(i==runestrlen(new));
492 /* replace tag if the new one is different */
494 if(runeeq(new, i, old, k) == FALSE){
504 textdelete(&w->tag, j, k, TRUE);
505 textinsert(&w->tag, j, new+j, i-j, TRUE);
506 /* try to preserve user selection */
507 r = runestrchr(old, '|');
511 bar = (runestrchr(new, '|')-new)-bar;
520 w->tag.file->mod = FALSE;
521 n = w->tag.file->b.nc+w->tag.ncache;
526 textsetselect(&w->tag, w->tag.q0, w->tag.q1);
528 if(!w->isdir && !w->isscratch && (w->body.file->mod || w->body.ncache))
530 br.min = w->tag.scrollr.min;
531 br.max.x = br.min.x + Dx(b->r);
532 br.max.y = br.min.y + Dy(b->r);
533 draw(screen, br, b, nil, b->r.min);
536 winresize(w, w->r, TRUE, TRUE);
548 for(i=0; i<f->ntext; i++){
550 if(v->col->safe || v->body.fr.maxlines>0)
556 wincommit(Window *w, Text *t)
565 for(i=0; i<f->ntext; i++)
566 textcommit(f->text[i], FALSE); /* no-op for t */
569 r = runemalloc(w->tag.file->b.nc);
570 bufread(&w->tag.file->b, 0, r, w->tag.file->b.nc);
571 for(i=0; i<w->tag.file->b.nc; i++)
572 if(r[i]==' ' || r[i]=='\t')
574 expandenv(&r, (uint*)&i);
575 if(runeeq(r, i, w->body.file->name, w->body.file->nname) == FALSE){
577 filemark(w->body.file);
578 w->body.file->mod = TRUE;
587 winaddincl(Window *w, Rune *r, int n)
593 a = runetobyte(r, n);
598 rs = dirname(&w->body, r, n);
602 a = runetobyte(r, n);
606 r = runerealloc(r, n+1);
610 if((d->qid.type&QTDIR) == 0){
612 warning(nil, "%s: not a directory\n", a);
618 w->incl = realloc(w->incl, w->nincl*sizeof(Rune*));
619 memmove(w->incl+1, w->incl, (w->nincl-1)*sizeof(Rune*));
620 w->incl[0] = runemalloc(n+1);
621 runemove(w->incl[0], r, n);
626 warning(nil, "%s: %r\n", a);
633 winclean(Window *w, int conservative) /* as it stands, conservative is always TRUE */
635 if(w->isscratch || w->isdir) /* don't whine if it's a guide file, error window, etc. */
637 if(!conservative && w->nopen[QWevent]>0)
640 if(w->body.file->nname)
641 warning(nil, "%.*S modified\n", w->body.file->nname, w->body.file->name);
643 if(w->body.file->b.nc < 100) /* don't whine if it's too small */
645 warning(nil, "unnamed file modified\n");
654 winctlprint(Window *w, char *buf, int fonts)
656 sprint(buf, "%11d %11d %11d %11d %11d ", w->id, w->tag.file->b.nc,
657 w->body.file->b.nc, w->isdir, w->dirty);
659 return smprint("%s%11d %q %11d ", buf, Dx(w->body.fr.r),
660 w->body.reffont->f->name, w->body.fr.maxtab);
665 winevent(Window *w, char *fmt, ...)
672 if(w->nopen[QWevent] == 0)
675 error("no window owner");
677 b = vsmprint(fmt, arg);
680 error("vsmprint failed");
682 w->events = realloc(w->events, w->nevents+1+n);
683 w->events[w->nevents++] = w->owner;
684 memmove(w->events+w->nevents, b, n);
695 * This is here as a first stab at something.
696 * Run acme with the -'$' flag to enable it.
698 * This code isn't quite right, in that it doesn't play well with
699 * the plumber and with other applications. For example:
701 * If the window tag is $home/bin and you execute script, then acme runs
702 * script in $home/bin, via the shell, so everything is fine. If you do
703 * execute "echo $home", it too goes to the shell so you see the value
704 * of $home. And if you right-click on script, then acme plumbs "script"
705 * in the directory "/home/you/bin", so that works, but if you right-click
706 * on "$home/bin/script", then what? It's not correct to expand in acme
707 * since what you're plumbing might be a price tag for all we know. So the
708 * plumber has to expand it, but in order to do that the plumber should
709 * probably publish (and allow users to change) the set of variables it is
710 * using in expansions.
712 * Rob has suggested that a better solution is to make tag lines expand
713 * automatically to fit the necessary number of lines.
715 * The best solution, of course, is to use nice short path names, but this
716 * is not always possible.
720 expandenv(Rune **rp, uint *np)
733 if(n == 0 || r[0] != '$')
735 for(p=r+1; *p && *p != '/'; p++)
738 s = runetobyte(r+1, pref-1);
739 if((t = getenv(s)) == nil){
744 q = runemalloc(utflen(t)+(n-pref));
745 cvttorunes(t, strlen(t), q, &nb, &nr, nil);
746 assert(nr==utflen(t));
747 runemove(q+nr, p, n-pref);
749 rs = runestr(q, nr+(n-pref));
750 slash = rs.nr>0 && q[rs.nr-1] == '/';
753 rs.r = runerealloc(rs.r, rs.nr+1);
762 extern char **environ;
766 * Weird sort order -- shorter names first,
767 * prefer lowercase over uppercase ($home over $HOME),
768 * then do normal string comparison.
771 runeenvcmp(const void *va, const void *vb)
778 na = runestrchr(a, '=') - a;
779 nb = runestrchr(b, '=') - b;
786 if(islowerrune(a[0]) && !islowerrune(b[0]))
788 if(islowerrune(b[0]) && !islowerrune(a[0]))
790 return runestrncmp(a, b, na);
801 for(i=0; environ[i]; i++){
803 * Ignore some pollution.
805 if(environ[i][0] == '_')
807 if(strncmp(environ[i], "PWD=", 4) == 0)
809 if(strncmp(environ[i], "OLDPWD=", 7) == 0)
813 * Must be a rooted path.
815 if((p = strchr(environ[i], '=')) == nil || *(p+1) != '/')
819 * Only use the ones that we accept in look - all isfilec
822 r = bytetorune(environ[i], &nr);
823 for(q=r; *q != '='; q++)
830 runeenv = erealloc(runeenv, (n+1)*sizeof(runeenv[0]));
834 runeenv = erealloc(runeenv, (n+1)*sizeof(runeenv[0]));
836 qsort(runeenv, n, sizeof(runeenv[0]), runeenvcmp);
840 abbrevenv(Rune **rp, uint *np)
851 if(n == 0 || r[0] != '/')
857 for(i=0; runeenv[i]; i++){
858 p = runestrchr(runeenv[i], '=')+1;
860 if(len <= n && (r[len]==0 || r[len]=='/') && runeeq(r, len, p, len)==TRUE){
861 alen = (p-1) - runeenv[i];
862 q = runemalloc(1+alen+n-len);
864 runemove(q+1, runeenv[i], alen);
865 runemove(q+1+alen, r+len, n-len);