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 * Draw the appropriate button.
98 windrawbutton(Window *w)
104 if(!w->isdir && !w->isscratch && (w->body.file->mod || w->body.ncache))
106 br.min = w->tag.scrollr.min;
107 br.max.x = br.min.x + Dx(b->r);
108 br.max.y = br.min.y + Dy(b->r);
109 draw(screen, br, b, nil, b->r.min);
113 * Compute number of tag lines required
114 * to display entire tag text.
117 wintaglines(Window *w, Rectangle r)
122 /* TAG policy here */
126 w->tag.fr.noredraw = 1;
127 textresize(&w->tag, r, TRUE);
128 w->tag.fr.noredraw = 0;
131 /* can't use more than we have */
132 if(w->tag.fr.nlines >= w->tag.fr.maxlines)
133 return w->tag.fr.maxlines;
135 /* if tag ends with \n, include empty line at end for typing */
136 n = w->tag.fr.nlines;
137 if(w->tag.file->b.nc > 0)
138 bufread(&w->tag.file->b, w->tag.file->b.nc-1, &rune, 1);
147 winresize(Window *w, Rectangle r, int safe, int keepextra)
149 int oy, y, mouseintag, tagresized;
153 if(0) fprint(2, "winresize %d %R safe=%d keep=%d h=%d\n", w->id, r, safe, keepextra, font->height);
155 w->tagtop.max.y = r.min.y+font->height;
158 * TAG If necessary, recompute the number of lines that should
162 r1.max.y = min(r.max.y, r1.min.y + w->taglines*font->height);
164 mouseintag = ptinrect(mouse->xy, w->tag.all);
165 if(!safe || !w->tagsafe || !eqrect(w->tag.all, r1)){
167 w->taglines = wintaglines(w, r);
172 r1.max.y = min(r.max.y, r1.min.y + w->taglines*font->height);
175 if(0) fprint(2, "winresize tag %R %R\n", w->tag.all, r1);
176 if(!safe || !w->tagsafe || !eqrect(w->tag.all, r1)){
178 if(0) fprint(2, "resize tag %R => %R\n", w->tag.all, r1);
179 textresize(&w->tag, r1, TRUE);
180 if(0) fprint(2, "=> %R (%R)\n", w->tag.all, w->tag.fr.r);
181 y = w->tag.fr.r.max.y;
185 if(mouseintag && !ptinrect(mouse->xy, w->tag.all)){
187 p.y = w->tag.all.max.y-3;
196 if(tagresized || !safe || !eqrect(w->body.all, r1)){
198 if(0) fprint(2, "resizing body; safe=%d all=%R r1=%R\n", safe, w->body.all, r1);
199 if(y+1+w->body.fr.font->height <= r.max.y){ /* room for one line */
202 draw(screen, r1, tagcols[BORD], nil, ZP);
204 r1.min.y = min(y, r.max.y);
210 if(0) fprint(2, "resizing body; new r=%R; r1=%R\n", r, r1);
212 w->r.max.y = textresize(&w->body, r1, keepextra);
213 if(0) fprint(2, "after textresize: body.all=%R\n", w->body.all);
214 textscrdraw(&w->body);
215 w->body.all.min.y = oy;
217 w->maxlines = min(w->body.fr.nlines, max(w->maxlines, w->body.fr.maxlines));
222 winlock1(Window *w, int owner)
230 winlock(Window *w, int owner)
236 for(i=0; i<f->ntext; i++)
237 winlock1(f->text[i]->w, owner);
247 * subtle: loop runs backwards to avoid tripping over
248 * winclose indirectly editing f->text and freeing f
249 * on the last iteration of the loop.
252 for(i=f->ntext-1; i>=0; i--){
261 winmousebut(Window *w)
263 moveto(mousectl, addpt(w->tag.scrollr.min,
264 divpt(Pt(Dx(w->tag.scrollr), font->height), 2)));
268 windirfree(Window *w)
274 for(i=0; i<w->ndl; i++){
290 if(decref(&w->ref) == 0){
296 for(i=0; i<w->nincl; i++)
315 sendp(x->c, nil); /* wake him up */
320 winundo(Window *w, int isundo)
329 fileundo(body->file, isundo, &body->q0, &body->q1);
330 textshow(body, body->q0, body->q1, 1);
332 for(i=0; i<f->ntext; i++){
334 v->dirty = (f->seq != v->putseq);
336 v->body.q0 = v->body.fr.p0+v->body.org;
337 v->body.q1 = v->body.fr.p1+v->body.org;
344 winsetname(Window *w, Rune *name, int n)
349 static Rune Lslashguide[] = { '/', 'g', 'u', 'i', 'd', 'e', 0 };
350 static Rune Lpluserrors[] = { '+', 'E', 'r', 'r', 'o', 'r', 's', 0 };
352 if(runeeq(t->file->name, t->file->nname, name, n) == TRUE)
354 w->isscratch = FALSE;
355 if(n>=6 && runeeq(Lslashguide, 6, name+(n-6), 6))
357 else if(n>=7 && runeeq(Lpluserrors, 7, name+(n-7), 7))
359 filesetname(t->file, name, n);
360 for(i=0; i<t->file->ntext; i++){
361 v = t->file->text[i]->w;
363 v->isscratch = w->isscratch;
368 wintype(Window *w, Text *t, Rune r)
374 for(i=0; i<t->file->ntext; i++)
375 textscrdraw(t->file->text[i]);
380 wincleartag(Window *w)
385 /* w must be committed */
386 n = w->tag.file->b.nc;
388 bufread(&w->tag.file->b, 0, r, n);
390 if(r[i]==' ' || r[i]=='\t')
398 textdelete(&w->tag, i, n, TRUE);
400 w->tag.file->mod = FALSE;
405 textsetselect(&w->tag, w->tag.q0, w->tag.q1);
409 winsettag1(Window *w)
411 int bar, dirty, i, j, k, n, ntagname, resize;
412 Rune *new, *old, *r, *tagname;
414 static Rune Ldelsnarf[] = { ' ', 'D', 'e', 'l', ' ',
415 'S', 'n', 'a', 'r', 'f', 0 };
416 static Rune Lundo[] = { ' ', 'U', 'n', 'd', 'o', 0 };
417 static Rune Lredo[] = { ' ', 'R', 'e', 'd', 'o', 0 };
418 static Rune Lget[] = { ' ', 'G', 'e', 't', 0 };
419 static Rune Lput[] = { ' ', 'P', 'u', 't', 0 };
420 static Rune Llook[] = { ' ', 'L', 'o', 'o', 'k', ' ', 0 };
421 static Rune Lpipe[] = { ' ', '|', 0 };
422 /* there are races that get us here with stuff in the tag cache, so we take extra care to sync it */
423 if(w->tag.ncache!=0 || w->tag.file->mod)
424 wincommit(w, &w->tag); /* check file name; also guarantees we can modify tag contents */
425 old = runemalloc(w->tag.file->b.nc+1);
426 bufread(&w->tag.file->b, 0, old, w->tag.file->b.nc);
427 old[w->tag.file->b.nc] = '\0';
428 for(i=0; i<w->tag.file->b.nc; i++)
429 if(old[i]==' ' || old[i]=='\t')
432 /* make sure the file name is set correctly in the tag */
433 ntagname = w->body.file->nname;
434 tagname = runemalloc(ntagname);
435 runemove(tagname, w->body.file->name, ntagname);
436 abbrevenv(&tagname, (uint*)&ntagname);
439 * XXX Why is this here instead of letting the code
440 * down below do the work?
442 if(runeeq(old, i, tagname, ntagname) == FALSE){
445 textdelete(&w->tag, 0, i, TRUE);
446 textinsert(&w->tag, 0, tagname, ntagname, TRUE);
448 old = runemalloc(w->tag.file->b.nc+1);
449 bufread(&w->tag.file->b, 0, old, w->tag.file->b.nc);
450 old[w->tag.file->b.nc] = '\0';
453 * XXX common case - typing at end of name
455 w->tag.q0 = q0+ntagname-i;
456 w->tag.q1 = q1+ntagname-i;
460 /* compute the text for the whole tag, replacing current only if it differs */
461 new = runemalloc(ntagname+100);
463 runemove(new+i, tagname, ntagname);
465 runemove(new+i, Ldelsnarf, 10);
468 if(w->body.needundo || w->body.file->delta.nc>0 || w->body.ncache){
469 runemove(new+i, Lundo, 5);
472 if(w->body.file->epsilon.nc > 0){
473 runemove(new+i, Lredo, 5);
476 dirty = w->body.file->nname && (w->body.ncache || w->body.file->seq!=w->putseq);
477 if(!w->isdir && dirty){
478 runemove(new+i, Lput, 4);
483 runemove(new+i, Lget, 4);
486 runemove(new+i, Lpipe, 2);
488 r = runestrchr(old, '|');
492 k = w->tag.file->b.nc;
493 if(w->body.file->seq == 0){
494 runemove(new+i, Llook, 6);
499 if(runestrlen(new) != i)
500 fprint(2, "s '%S' len not %d\n", new, i);
501 assert(i==runestrlen(new));
503 /* replace tag if the new one is different */
505 if(runeeq(new, i, old, k) == FALSE){
515 textdelete(&w->tag, j, k, TRUE);
516 textinsert(&w->tag, j, new+j, i-j, TRUE);
517 /* try to preserve user selection */
518 r = runestrchr(old, '|');
522 bar = (runestrchr(new, '|')-new)-bar;
531 w->tag.file->mod = FALSE;
532 n = w->tag.file->b.nc+w->tag.ncache;
537 textsetselect(&w->tag, w->tag.q0, w->tag.q1);
541 winresize(w, w->r, TRUE, TRUE);
553 for(i=0; i<f->ntext; i++){
555 if(v->col->safe || v->body.fr.maxlines>0)
561 wincommit(Window *w, Text *t)
570 for(i=0; i<f->ntext; i++)
571 textcommit(f->text[i], FALSE); /* no-op for t */
574 r = runemalloc(w->tag.file->b.nc);
575 bufread(&w->tag.file->b, 0, r, w->tag.file->b.nc);
576 for(i=0; i<w->tag.file->b.nc; i++)
577 if(r[i]==' ' || r[i]=='\t')
579 expandenv(&r, (uint*)&i);
580 if(runeeq(r, i, w->body.file->name, w->body.file->nname) == FALSE){
582 filemark(w->body.file);
583 w->body.file->mod = TRUE;
592 winaddincl(Window *w, Rune *r, int n)
598 a = runetobyte(r, n);
603 rs = dirname(&w->body, r, n);
607 a = runetobyte(r, n);
611 r = runerealloc(r, n+1);
615 if((d->qid.type&QTDIR) == 0){
617 warning(nil, "%s: not a directory\n", a);
623 w->incl = realloc(w->incl, w->nincl*sizeof(Rune*));
624 memmove(w->incl+1, w->incl, (w->nincl-1)*sizeof(Rune*));
625 w->incl[0] = runemalloc(n+1);
626 runemove(w->incl[0], r, n);
631 warning(nil, "%s: %r\n", a);
638 winclean(Window *w, int conservative) /* as it stands, conservative is always TRUE */
640 if(w->isscratch || w->isdir) /* don't whine if it's a guide file, error window, etc. */
642 if(!conservative && w->nopen[QWevent]>0)
645 if(w->body.file->nname)
646 warning(nil, "%.*S modified\n", w->body.file->nname, w->body.file->name);
648 if(w->body.file->b.nc < 100) /* don't whine if it's too small */
650 warning(nil, "unnamed file modified\n");
659 winctlprint(Window *w, char *buf, int fonts)
661 sprint(buf, "%11d %11d %11d %11d %11d ", w->id, w->tag.file->b.nc,
662 w->body.file->b.nc, w->isdir, w->dirty);
664 return smprint("%s%11d %q %11d ", buf, Dx(w->body.fr.r),
665 w->body.reffont->f->name, w->body.fr.maxtab);
670 winevent(Window *w, char *fmt, ...)
677 if(w->nopen[QWevent] == 0)
680 error("no window owner");
682 b = vsmprint(fmt, arg);
685 error("vsmprint failed");
687 w->events = erealloc(w->events, w->nevents+1+n);
688 w->events[w->nevents++] = w->owner;
689 memmove(w->events+w->nevents, b, n);
700 * This is here as a first stab at something.
701 * Run acme with the -'$' flag to enable it.
703 * This code isn't quite right, in that it doesn't play well with
704 * the plumber and with other applications. For example:
706 * If the window tag is $home/bin and you execute script, then acme runs
707 * script in $home/bin, via the shell, so everything is fine. If you do
708 * execute "echo $home", it too goes to the shell so you see the value
709 * of $home. And if you right-click on script, then acme plumbs "script"
710 * in the directory "/home/you/bin", so that works, but if you right-click
711 * on "$home/bin/script", then what? It's not correct to expand in acme
712 * since what you're plumbing might be a price tag for all we know. So the
713 * plumber has to expand it, but in order to do that the plumber should
714 * probably publish (and allow users to change) the set of variables it is
715 * using in expansions.
717 * Rob has suggested that a better solution is to make tag lines expand
718 * automatically to fit the necessary number of lines.
720 * The best solution, of course, is to use nice short path names, but this
721 * is not always possible.
725 expandenv(Rune **rp, uint *np)
738 if(n == 0 || r[0] != '$')
740 for(p=r+1; *p && *p != '/'; p++)
743 s = runetobyte(r+1, pref-1);
744 if((t = getenv(s)) == nil){
749 q = runemalloc(utflen(t)+(n-pref));
750 cvttorunes(t, strlen(t), q, &nb, &nr, nil);
751 assert(nr==utflen(t));
752 runemove(q+nr, p, n-pref);
754 rs = runestr(q, nr+(n-pref));
755 slash = rs.nr>0 && q[rs.nr-1] == '/';
758 rs.r = runerealloc(rs.r, rs.nr+1);
767 extern char **environ;
771 * Weird sort order -- shorter names first,
772 * prefer lowercase over uppercase ($home over $HOME),
773 * then do normal string comparison.
776 runeenvcmp(const void *va, const void *vb)
783 na = runestrchr(a, '=') - a;
784 nb = runestrchr(b, '=') - b;
791 if(islowerrune(a[0]) && !islowerrune(b[0]))
793 if(islowerrune(b[0]) && !islowerrune(a[0]))
795 return runestrncmp(a, b, na);
806 for(i=0; environ[i]; i++){
808 * Ignore some pollution.
810 if(environ[i][0] == '_')
812 if(strncmp(environ[i], "PWD=", 4) == 0)
814 if(strncmp(environ[i], "OLDPWD=", 7) == 0)
818 * Must be a rooted path.
820 if((p = strchr(environ[i], '=')) == nil || *(p+1) != '/')
824 * Only use the ones that we accept in look - all isfilec
827 r = bytetorune(environ[i], &nr);
828 for(q=r; *q != '='; q++)
835 runeenv = erealloc(runeenv, (n+1)*sizeof(runeenv[0]));
839 runeenv = erealloc(runeenv, (n+1)*sizeof(runeenv[0]));
841 qsort(runeenv, n, sizeof(runeenv[0]), runeenvcmp);
845 abbrevenv(Rune **rp, uint *np)
856 if(n == 0 || r[0] != '/')
862 for(i=0; runeenv[i]; i++){
863 p = runestrchr(runeenv[i], '=')+1;
865 if(len <= n && (r[len]==0 || r[len]=='/') && runeeq(r, len, p, len)==TRUE){
866 alen = (p-1) - runeenv[i];
867 q = runemalloc(1+alen+n-len);
869 runemove(q+1, runeenv[i], alen);
870 runemove(q+1+alen, r+len, n-len);