17 wininit(Window *w, Window *clone, Rectangle r)
34 r1.max.y = r1.min.y + font->height;
36 f = fileaddtext(nil, &w->tag);
37 textinit(&w->tag, f, r1, &reffont, tagcols);
39 /* tag is a copy of the contents, not a tracked image */
41 textdelete(&w->tag, 0, w->tag.file->b.nc, TRUE);
42 nc = clone->tag.file->b.nc;
44 bufread(&clone->tag.file->b, 0, rp, nc);
45 textinsert(&w->tag, 0, rp, nc, TRUE);
47 filereset(w->tag.file);
48 textsetselect(&w->tag, nc, nc);
50 //assert(w->body.w == w);
52 r1.min.y += font->height + 1;
53 if(r1.max.y < r1.min.y)
58 w->body.org = clone->body.org;
59 w->isscratch = clone->isscratch;
60 rf = rfget(FALSE, FALSE, FALSE, clone->body.reffont->f->name);
62 rf = rfget(FALSE, FALSE, FALSE, nil);
63 //assert(w->body.w == w);
64 f = fileaddtext(f, &w->body);
66 textinit(&w->body, f, r1, rf, textcols);
68 r1.max.y = r1.min.y+1;
69 draw(screen, r1, tagcols[BORD], nil, ZP);
70 textscrdraw(&w->body);
72 w->r.max.y = w->body.fr.r.max.y;
73 br.min = w->tag.scrollr.min;
74 br.max.x = br.min.x + Dx(button->r);
75 br.max.y = br.min.y + Dy(button->r);
76 draw(screen, br, button, nil, button->r.min);
78 w->maxlines = w->body.fr.maxlines;
79 w->autoindent = globalautoindent;
80 //assert(w->body.w == w);
82 w->dirty = clone->dirty;
83 w->autoindent = clone->autoindent;
84 textsetselect(&w->body, clone->body.q0, clone->body.q1);
90 winresize(Window *w, Rectangle r, int safe)
98 r1.max.y = r1.min.y + font->height;
100 if(!safe || !eqrect(w->tag.fr.r, r1)){
101 y = textresize(&w->tag, r1);
103 if(w->body.file->mod && !w->isdir && !w->isscratch)
105 br.min = w->tag.scrollr.min;
106 br.max.x = br.min.x + Dx(b->r);
107 br.max.y = br.min.y + Dy(b->r);
108 draw(screen, br, b, nil, b->r.min);
110 if(!safe || !eqrect(w->body.fr.r, r1)){
111 if(y+1+font->height > r.max.y){ /* no body */
114 textresize(&w->body, r1);
122 draw(screen, r1, tagcols[BORD], nil, ZP);
125 y = textresize(&w->body, r1);
128 textscrdraw(&w->body);
130 w->maxlines = min(w->body.fr.nlines, max(w->maxlines, w->body.fr.maxlines));
135 winlock1(Window *w, int owner)
143 winlock(Window *w, int owner)
148 //fprint(2, "winlock %p %d %lux\n", w, owner, getcallerpc(&w));
150 for(i=0; i<f->ntext; i++)
151 winlock1(f->text[i]->w, owner);
160 //fprint(2, "winunlock %p %lux\n", w, getcallerpc(&w));
162 for(i=0; i<f->ntext; i++){
167 /* winclose() can change up f->text; beware */
168 if(f->ntext>0 && w != f->text[i]->w)
169 --i; /* winclose() deleted window */
174 winmousebut(Window *w)
176 moveto(mousectl, divpt(addpt(w->tag.scrollr.min, w->tag.scrollr.max), 2));
180 windirfree(Window *w)
186 for(i=0; i<w->ndl; i++){
202 if(decref(&w->ref) == 0){
208 for(i=0; i<w->nincl; i++)
227 sendp(x->c, nil); /* wake him up */
232 winundo(Window *w, int isundo)
241 fileundo(body->file, isundo, &body->q0, &body->q1);
242 textshow(body, body->q0, body->q1, 1);
244 for(i=0; i<f->ntext; i++){
246 v->dirty = (f->seq != v->putseq);
248 v->body.q0 = v->body.fr.p0+v->body.org;
249 v->body.q1 = v->body.fr.p1+v->body.org;
256 winsetname(Window *w, Rune *name, int n)
261 static Rune Lslashguide[] = { '/', 'g', 'u', 'i', 'd', 'e', 0 };
262 static Rune Lpluserrors[] = { '+', 'E', 'r', 'r', 'o', 'r', 's', 0 };
264 if(runeeq(t->file->name, t->file->nname, name, n) == TRUE)
266 w->isscratch = FALSE;
267 if(n>=6 && runeeq(Lslashguide, 6, name+(n-6), 6))
269 else if(n>=7 && runeeq(Lpluserrors, 7, name+(n-7), 7))
271 filesetname(t->file, name, n);
272 for(i=0; i<t->file->ntext; i++){
273 v = t->file->text[i]->w;
275 v->isscratch = w->isscratch;
280 wintype(Window *w, Text *t, Rune r)
286 for(i=0; i<t->file->ntext; i++)
287 textscrdraw(t->file->text[i]);
292 wincleartag(Window *w)
297 /* w must be committed */
298 n = w->tag.file->b.nc;
300 bufread(&w->tag.file->b, 0, r, n);
302 if(r[i]==' ' || r[i]=='\t')
310 textdelete(&w->tag, i, n, TRUE);
312 w->tag.file->mod = FALSE;
317 textsetselect(&w->tag, w->tag.q0, w->tag.q1);
321 winsettag1(Window *w)
323 int bar, dirty, i, j, k, n, ntagname;
324 Rune *new, *old, *r, *tagname;
328 static Rune Ldelsnarf[] = { ' ', 'D', 'e', 'l', ' ',
329 'S', 'n', 'a', 'r', 'f', 0 };
330 static Rune Lundo[] = { ' ', 'U', 'n', 'd', 'o', 0 };
331 static Rune Lredo[] = { ' ', 'R', 'e', 'd', 'o', 0 };
332 static Rune Lget[] = { ' ', 'G', 'e', 't', 0 };
333 static Rune Lput[] = { ' ', 'P', 'u', 't', 0 };
334 static Rune Llook[] = { ' ', 'L', 'o', 'o', 'k', ' ', 0 };
335 static Rune Lpipe[] = { ' ', '|', 0 };
336 /* there are races that get us here with stuff in the tag cache, so we take extra care to sync it */
337 if(w->tag.ncache!=0 || w->tag.file->mod)
338 wincommit(w, &w->tag); /* check file name; also guarantees we can modify tag contents */
339 old = runemalloc(w->tag.file->b.nc+1);
340 bufread(&w->tag.file->b, 0, old, w->tag.file->b.nc);
341 old[w->tag.file->b.nc] = '\0';
342 for(i=0; i<w->tag.file->b.nc; i++)
343 if(old[i]==' ' || old[i]=='\t')
346 /* make sure the file name is set correctly in the tag */
347 ntagname = w->body.file->nname;
348 tagname = runemalloc(ntagname);
349 runemove(tagname, w->body.file->name, ntagname);
350 abbrevenv(&tagname, &ntagname);
352 if(runeeq(old, i, tagname, ntagname) == FALSE){
353 textdelete(&w->tag, 0, i, TRUE);
354 textinsert(&w->tag, 0, tagname, ntagname, TRUE);
356 old = runemalloc(w->tag.file->b.nc+1);
357 bufread(&w->tag.file->b, 0, old, w->tag.file->b.nc);
358 old[w->tag.file->b.nc] = '\0';
361 /* compute the text for the whole tag, replacing current only if it differs */
362 new = runemalloc(ntagname+100);
364 runemove(new+i, tagname, ntagname);
366 runemove(new+i, Ldelsnarf, 10);
369 if(w->body.file->delta.nc>0 || w->body.ncache){
370 runemove(new+i, Lundo, 5);
373 if(w->body.file->epsilon.nc > 0){
374 runemove(new+i, Lredo, 5);
377 dirty = w->body.file->nname && (w->body.ncache || w->body.file->seq!=w->putseq);
378 if(!w->isdir && dirty){
379 runemove(new+i, Lput, 4);
384 runemove(new+i, Lget, 4);
387 runemove(new+i, Lpipe, 2);
389 r = runestrchr(old, '|');
393 k = w->tag.file->b.nc;
394 if(w->body.file->seq == 0){
395 runemove(new+i, Llook, 6);
400 if(runestrlen(new) != i)
401 fprint(2, "s '%S' len not %d\n", new, i);
402 assert(i==runestrlen(new));
404 /* replace tag if the new one is different */
405 if(runeeq(new, i, old, k) == FALSE){
414 textdelete(&w->tag, j, k, TRUE);
415 textinsert(&w->tag, j, new+j, i-j, TRUE);
416 /* try to preserve user selection */
417 r = runestrchr(old, '|');
421 bar = (runestrchr(new, '|')-new)-bar;
430 w->tag.file->mod = FALSE;
431 n = w->tag.file->b.nc+w->tag.ncache;
436 textsetselect(&w->tag, w->tag.q0, w->tag.q1);
438 if(!w->isdir && !w->isscratch && (w->body.file->mod || w->body.ncache))
440 br.min = w->tag.scrollr.min;
441 br.max.x = br.min.x + Dx(b->r);
442 br.max.y = br.min.y + Dy(b->r);
443 draw(screen, br, b, nil, b->r.min);
454 for(i=0; i<f->ntext; i++){
456 if(v->col->safe || v->body.fr.maxlines>0)
462 wincommit(Window *w, Text *t)
471 for(i=0; i<f->ntext; i++)
472 textcommit(f->text[i], FALSE); /* no-op for t */
475 r = runemalloc(w->tag.file->b.nc);
476 bufread(&w->tag.file->b, 0, r, w->tag.file->b.nc);
477 for(i=0; i<w->tag.file->b.nc; i++)
478 if(r[i]==' ' || r[i]=='\t')
481 if(runeeq(r, i, w->body.file->name, w->body.file->nname) == FALSE){
483 filemark(w->body.file);
484 w->body.file->mod = TRUE;
493 winaddincl(Window *w, Rune *r, int n)
499 a = runetobyte(r, n);
504 rs = dirname(&w->body, r, n);
508 a = runetobyte(r, n);
512 r = runerealloc(r, n+1);
516 if((d->qid.type&QTDIR) == 0){
518 warning(nil, "%s: not a directory\n", a);
524 w->incl = realloc(w->incl, w->nincl*sizeof(Rune*));
525 memmove(w->incl+1, w->incl, (w->nincl-1)*sizeof(Rune*));
526 w->incl[0] = runemalloc(n+1);
527 runemove(w->incl[0], r, n);
532 warning(nil, "%s: %r\n", a);
539 winclean(Window *w, int conservative) /* as it stands, conservative is always TRUE */
541 if(w->isscratch || w->isdir) /* don't whine if it's a guide file, error window, etc. */
543 if(!conservative && w->nopen[QWevent]>0)
546 if(w->body.file->nname)
547 warning(nil, "%.*S modified\n", w->body.file->nname, w->body.file->name);
549 if(w->body.file->b.nc < 100) /* don't whine if it's too small */
551 warning(nil, "unnamed file modified\n");
560 winctlprint(Window *w, char *buf, int fonts)
562 sprint(buf, "%11d %11d %11d %11d %11d ", w->id, w->tag.file->b.nc,
563 w->body.file->b.nc, w->isdir, w->dirty);
565 return smprint("%s%11d %q %11d ", buf, Dx(w->body.fr.r),
566 w->body.reffont->f->name, w->body.fr.maxtab);
571 winevent(Window *w, char *fmt, ...)
578 if(w->nopen[QWevent] == 0)
581 error("no window owner");
583 b = vsmprint(fmt, arg);
586 error("vsmprint failed");
588 w->events = realloc(w->events, w->nevents+1+n);
589 w->events[w->nevents++] = w->owner;
590 memmove(w->events+w->nevents, b, n);
601 * This is here as a first stab at something.
602 * Run acme with the -'$' flag to enable it.
604 * This code isn't quite right, in that it doesn't play well with
605 * the plumber and with other applications. For example:
607 * If the window tag is $home/bin and you execute script, then acme runs
608 * script in $home/bin, via the shell, so everything is fine. If you do
609 * execute "echo $home", it too goes to the shell so you see the value
610 * of $home. And if you right-click on script, then acme plumbs "script"
611 * in the directory "/home/you/bin", so that works, but if you right-click
612 * on "$home/bin/script", then what? It's not correct to expand in acme
613 * since what you're plumbing might be a price tag for all we know. So the
614 * plumber has to expand it, but in order to do that the plumber should
615 * probably publish (and allow users to change) the set of variables it is
616 * using in expansions.
618 * Rob has suggested that a better solution is to make tag lines expand
619 * automatically to fit the necessary number of lines.
621 * The best solution, of course, is to use nice short path names, but this
622 * is not always possible.
626 expandenv(Rune **rp, uint *np)
639 if(n == 0 || r[0] != '$')
641 for(p=r+1; *p && *p != '/'; p++)
644 s = runetobyte(r+1, pref-1);
645 if((t = getenv(s)) == nil){
650 q = runemalloc(utflen(t)+(n-pref));
651 cvttorunes(t, strlen(t), q, &nb, &nr, nil);
652 assert(nr==utflen(t));
653 runemove(q+nr, p, n-pref);
655 rs = runestr(q, nr+(n-pref));
656 slash = rs.nr>0 && q[rs.nr-1] == '/';
659 rs.r = runerealloc(rs.r, rs.nr+1);
668 extern char **environ;
672 * Weird sort order -- shorter names first,
673 * prefer lowercase over uppercase ($home over $HOME),
674 * then do normal string comparison.
677 runeenvcmp(const void *va, const void *vb)
684 na = runestrchr(a, '=') - a;
685 nb = runestrchr(b, '=') - b;
692 if(islowerrune(a[0]) && !islowerrune(b[0]))
694 if(islowerrune(b[0]) && !islowerrune(a[0]))
696 return runestrncmp(a, b, na);
707 for(i=0; environ[i]; i++){
709 * Ignore some pollution.
711 if(environ[i][0] == '_')
713 if(strncmp(environ[i], "PWD=", 4) == 0)
715 if(strncmp(environ[i], "OLDPWD=", 7) == 0)
719 * Must be a rooted path.
721 if((p = strchr(environ[i], '=')) == nil || *(p+1) != '/')
725 * Only use the ones that we accept in look - all isfilec
728 r = bytetorune(environ[i], &nr);
729 for(q=r; *q != '='; q++)
736 runeenv = erealloc(runeenv, (n+1)*sizeof(runeenv[0]));
740 runeenv = erealloc(runeenv, (n+1)*sizeof(runeenv[0]));
742 qsort(runeenv, n, sizeof(runeenv[0]), runeenvcmp);
746 abbrevenv(Rune **rp, uint *np)
757 if(n == 0 || r[0] != '/')
763 for(i=0; runeenv[i]; i++){
764 p = runestrchr(runeenv[i], '=')+1;
766 if(len <= n && (r[len]==0 || r[len]=='/') && runeeq(r, len, p, len)==TRUE){
767 alen = (p-1) - runeenv[i];
768 q = runemalloc(1+alen+n-len);
770 runemove(q+1, runeenv[i], alen);
771 runemove(q+1+alen, r+len, n-len);