17 wininit(Window *w, Window *clone, Rectangle r)
37 w->tagtop.max.y = r.min.y + font->height;
39 r1.max.y = r1.min.y + w->taglines*font->height;
41 f = fileaddtext(nil, &w->tag);
42 textinit(&w->tag, f, r1, &reffont, tagcols);
44 /* tag is a copy of the contents, not a tracked image */
46 textdelete(&w->tag, 0, w->tag.file->b.nc, TRUE);
47 nc = clone->tag.file->b.nc;
49 bufread(&clone->tag.file->b, 0, rp, nc);
50 textinsert(&w->tag, 0, rp, nc, TRUE);
52 filereset(w->tag.file);
53 textsetselect(&w->tag, nc, nc);
55 //assert(w->body.w == w);
57 r1.min.y += w->taglines*font->height + 1;
58 if(r1.max.y < r1.min.y)
63 w->body.org = clone->body.org;
64 w->isscratch = clone->isscratch;
65 rf = rfget(FALSE, FALSE, FALSE, clone->body.reffont->f->name);
67 rf = rfget(FALSE, FALSE, FALSE, nil);
68 //assert(w->body.w == w);
69 f = fileaddtext(f, &w->body);
71 textinit(&w->body, f, r1, rf, textcols);
73 r1.max.y = r1.min.y+1;
74 draw(screen, r1, tagcols[BORD], nil, ZP);
75 textscrdraw(&w->body);
77 br.min = w->tag.scrollr.min;
78 br.max.x = br.min.x + Dx(button->r);
79 br.max.y = br.min.y + Dy(button->r);
80 draw(screen, br, button, nil, button->r.min);
82 w->maxlines = w->body.fr.maxlines;
83 w->autoindent = globalautoindent;
84 //assert(w->body.w == w);
86 w->dirty = clone->dirty;
87 w->autoindent = clone->autoindent;
88 textsetselect(&w->body, clone->body.q0, clone->body.q1);
94 * Compute number of tag lines required
95 * to display entire tag text.
98 wintaglines(Window *w, Rectangle r)
105 w->tag.fr.noredraw = 1;
106 textresize(&w->tag, r, TRUE);
107 w->tag.fr.noredraw = 0;
108 if(w->tag.fr.nlines >= w->tag.fr.maxlines)
109 return w->tag.fr.maxlines;
110 n = w->tag.fr.nlines;
111 bufread(&w->tag.file->b, w->tag.file->b.nc-1, &rune, 1);
118 winresize(Window *w, Rectangle r, int safe, int keepextra)
125 w->tagtop.max.y = r.min.y+font->height;
128 r1.max.y = min(r.max.y, r1.min.y + w->taglines*font->height);
130 if(1 || !safe || !eqrect(w->tag.all, r1)){
131 textresize(&w->tag, r1, TRUE);
132 y = w->tag.fr.r.max.y;
134 if(w->body.file->mod && !w->isdir && !w->isscratch)
136 br.min = w->tag.scrollr.min;
137 br.max.x = br.min.x + Dx(b->r);
138 br.max.y = br.min.y + Dy(b->r);
139 draw(screen, br, b, nil, b->r.min);
144 if(!safe || !eqrect(w->body.all, r1)){
145 if(y+1+w->body.fr.font->height <= r.max.y){ /* room for one line */
148 draw(screen, r1, tagcols[BORD], nil, ZP);
150 r1.min.y = min(y, r.max.y);
157 w->r.max.y = textresize(&w->body, r1, keepextra);
158 textscrdraw(&w->body);
160 w->maxlines = min(w->body.fr.nlines, max(w->maxlines, w->body.fr.maxlines));
165 winlock1(Window *w, int owner)
173 winlock(Window *w, int owner)
179 for(i=0; i<f->ntext; i++)
180 winlock1(f->text[i]->w, owner);
190 * subtle: loop runs backwards to avoid tripping over
191 * winclose indirectly editing f->text and freeing f
192 * on the last iteration of the loop.
195 for(i=f->ntext-1; i>=0; i--){
204 winmousebut(Window *w)
206 moveto(mousectl, addpt(w->tag.scrollr.min,
207 divpt(Pt(Dx(w->tag.scrollr), font->height), 2)));
211 windirfree(Window *w)
217 for(i=0; i<w->ndl; i++){
233 if(decref(&w->ref) == 0){
239 for(i=0; i<w->nincl; i++)
258 sendp(x->c, nil); /* wake him up */
263 winundo(Window *w, int isundo)
272 fileundo(body->file, isundo, &body->q0, &body->q1);
273 textshow(body, body->q0, body->q1, 1);
275 for(i=0; i<f->ntext; i++){
277 v->dirty = (f->seq != v->putseq);
279 v->body.q0 = v->body.fr.p0+v->body.org;
280 v->body.q1 = v->body.fr.p1+v->body.org;
287 winsetname(Window *w, Rune *name, int n)
292 static Rune Lslashguide[] = { '/', 'g', 'u', 'i', 'd', 'e', 0 };
293 static Rune Lpluserrors[] = { '+', 'E', 'r', 'r', 'o', 'r', 's', 0 };
295 if(runeeq(t->file->name, t->file->nname, name, n) == TRUE)
297 w->isscratch = FALSE;
298 if(n>=6 && runeeq(Lslashguide, 6, name+(n-6), 6))
300 else if(n>=7 && runeeq(Lpluserrors, 7, name+(n-7), 7))
302 filesetname(t->file, name, n);
303 for(i=0; i<t->file->ntext; i++){
304 v = t->file->text[i]->w;
306 v->isscratch = w->isscratch;
311 wintype(Window *w, Text *t, Rune r)
317 for(i=0; i<t->file->ntext; i++)
318 textscrdraw(t->file->text[i]);
323 wincleartag(Window *w)
328 /* w must be committed */
329 n = w->tag.file->b.nc;
331 bufread(&w->tag.file->b, 0, r, n);
333 if(r[i]==' ' || r[i]=='\t')
341 textdelete(&w->tag, i, n, TRUE);
343 w->tag.file->mod = FALSE;
348 textsetselect(&w->tag, w->tag.q0, w->tag.q1);
352 winsettag1(Window *w)
354 int bar, dirty, i, j, k, n, ntagname;
355 Rune *new, *old, *r, *tagname;
359 static Rune Ldelsnarf[] = { ' ', 'D', 'e', 'l', ' ',
360 'S', 'n', 'a', 'r', 'f', 0 };
361 static Rune Lundo[] = { ' ', 'U', 'n', 'd', 'o', 0 };
362 static Rune Lredo[] = { ' ', 'R', 'e', 'd', 'o', 0 };
363 static Rune Lget[] = { ' ', 'G', 'e', 't', 0 };
364 static Rune Lput[] = { ' ', 'P', 'u', 't', 0 };
365 static Rune Llook[] = { ' ', 'L', 'o', 'o', 'k', ' ', 0 };
366 static Rune Lpipe[] = { ' ', '|', 0 };
367 /* there are races that get us here with stuff in the tag cache, so we take extra care to sync it */
368 if(w->tag.ncache!=0 || w->tag.file->mod)
369 wincommit(w, &w->tag); /* check file name; also guarantees we can modify tag contents */
370 old = runemalloc(w->tag.file->b.nc+1);
371 bufread(&w->tag.file->b, 0, old, w->tag.file->b.nc);
372 old[w->tag.file->b.nc] = '\0';
373 for(i=0; i<w->tag.file->b.nc; i++)
374 if(old[i]==' ' || old[i]=='\t')
377 /* make sure the file name is set correctly in the tag */
378 ntagname = w->body.file->nname;
379 tagname = runemalloc(ntagname);
380 runemove(tagname, w->body.file->name, ntagname);
381 abbrevenv(&tagname, (uint*)&ntagname);
384 * XXX Why is this here instead of letting the code
385 * down below do the work?
387 if(runeeq(old, i, tagname, ntagname) == FALSE){
390 textdelete(&w->tag, 0, i, TRUE);
391 textinsert(&w->tag, 0, tagname, ntagname, TRUE);
393 old = runemalloc(w->tag.file->b.nc+1);
394 bufread(&w->tag.file->b, 0, old, w->tag.file->b.nc);
395 old[w->tag.file->b.nc] = '\0';
398 * XXX common case - typing at end of name
400 w->tag.q0 = q0+ntagname-i;
401 w->tag.q1 = q1+ntagname-i;
405 /* compute the text for the whole tag, replacing current only if it differs */
406 new = runemalloc(ntagname+100);
408 runemove(new+i, tagname, ntagname);
410 runemove(new+i, Ldelsnarf, 10);
413 if(w->body.file->delta.nc>0 || w->body.ncache){
414 runemove(new+i, Lundo, 5);
417 if(w->body.file->epsilon.nc > 0){
418 runemove(new+i, Lredo, 5);
421 dirty = w->body.file->nname && (w->body.ncache || w->body.file->seq!=w->putseq);
422 if(!w->isdir && dirty){
423 runemove(new+i, Lput, 4);
428 runemove(new+i, Lget, 4);
431 runemove(new+i, Lpipe, 2);
433 r = runestrchr(old, '|');
437 k = w->tag.file->b.nc;
438 if(w->body.file->seq == 0){
439 runemove(new+i, Llook, 6);
444 if(runestrlen(new) != i)
445 fprint(2, "s '%S' len not %d\n", new, i);
446 assert(i==runestrlen(new));
448 /* replace tag if the new one is different */
449 if(runeeq(new, i, old, k) == FALSE){
458 textdelete(&w->tag, j, k, TRUE);
459 textinsert(&w->tag, j, new+j, i-j, TRUE);
460 /* try to preserve user selection */
461 r = runestrchr(old, '|');
465 bar = (runestrchr(new, '|')-new)-bar;
474 w->tag.file->mod = FALSE;
475 n = w->tag.file->b.nc+w->tag.ncache;
480 textsetselect(&w->tag, w->tag.q0, w->tag.q1);
482 if(!w->isdir && !w->isscratch && (w->body.file->mod || w->body.ncache))
484 br.min = w->tag.scrollr.min;
485 br.max.x = br.min.x + Dx(b->r);
486 br.max.y = br.min.y + Dy(b->r);
487 draw(screen, br, b, nil, b->r.min);
498 for(i=0; i<f->ntext; i++){
500 if(v->col->safe || v->body.fr.maxlines>0)
506 wincommit(Window *w, Text *t)
515 for(i=0; i<f->ntext; i++)
516 textcommit(f->text[i], FALSE); /* no-op for t */
519 r = runemalloc(w->tag.file->b.nc);
520 bufread(&w->tag.file->b, 0, r, w->tag.file->b.nc);
521 for(i=0; i<w->tag.file->b.nc; i++)
522 if(r[i]==' ' || r[i]=='\t')
524 expandenv(&r, (uint*)&i);
525 if(runeeq(r, i, w->body.file->name, w->body.file->nname) == FALSE){
527 filemark(w->body.file);
528 w->body.file->mod = TRUE;
537 winaddincl(Window *w, Rune *r, int n)
543 a = runetobyte(r, n);
548 rs = dirname(&w->body, r, n);
552 a = runetobyte(r, n);
556 r = runerealloc(r, n+1);
560 if((d->qid.type&QTDIR) == 0){
562 warning(nil, "%s: not a directory\n", a);
568 w->incl = realloc(w->incl, w->nincl*sizeof(Rune*));
569 memmove(w->incl+1, w->incl, (w->nincl-1)*sizeof(Rune*));
570 w->incl[0] = runemalloc(n+1);
571 runemove(w->incl[0], r, n);
576 warning(nil, "%s: %r\n", a);
583 winclean(Window *w, int conservative) /* as it stands, conservative is always TRUE */
585 if(w->isscratch || w->isdir) /* don't whine if it's a guide file, error window, etc. */
587 if(!conservative && w->nopen[QWevent]>0)
590 if(w->body.file->nname)
591 warning(nil, "%.*S modified\n", w->body.file->nname, w->body.file->name);
593 if(w->body.file->b.nc < 100) /* don't whine if it's too small */
595 warning(nil, "unnamed file modified\n");
604 winctlprint(Window *w, char *buf, int fonts)
606 sprint(buf, "%11d %11d %11d %11d %11d ", w->id, w->tag.file->b.nc,
607 w->body.file->b.nc, w->isdir, w->dirty);
609 return smprint("%s%11d %q %11d ", buf, Dx(w->body.fr.r),
610 w->body.reffont->f->name, w->body.fr.maxtab);
615 winevent(Window *w, char *fmt, ...)
622 if(w->nopen[QWevent] == 0)
625 error("no window owner");
627 b = vsmprint(fmt, arg);
630 error("vsmprint failed");
632 w->events = realloc(w->events, w->nevents+1+n);
633 w->events[w->nevents++] = w->owner;
634 memmove(w->events+w->nevents, b, n);
645 * This is here as a first stab at something.
646 * Run acme with the -'$' flag to enable it.
648 * This code isn't quite right, in that it doesn't play well with
649 * the plumber and with other applications. For example:
651 * If the window tag is $home/bin and you execute script, then acme runs
652 * script in $home/bin, via the shell, so everything is fine. If you do
653 * execute "echo $home", it too goes to the shell so you see the value
654 * of $home. And if you right-click on script, then acme plumbs "script"
655 * in the directory "/home/you/bin", so that works, but if you right-click
656 * on "$home/bin/script", then what? It's not correct to expand in acme
657 * since what you're plumbing might be a price tag for all we know. So the
658 * plumber has to expand it, but in order to do that the plumber should
659 * probably publish (and allow users to change) the set of variables it is
660 * using in expansions.
662 * Rob has suggested that a better solution is to make tag lines expand
663 * automatically to fit the necessary number of lines.
665 * The best solution, of course, is to use nice short path names, but this
666 * is not always possible.
670 expandenv(Rune **rp, uint *np)
683 if(n == 0 || r[0] != '$')
685 for(p=r+1; *p && *p != '/'; p++)
688 s = runetobyte(r+1, pref-1);
689 if((t = getenv(s)) == nil){
694 q = runemalloc(utflen(t)+(n-pref));
695 cvttorunes(t, strlen(t), q, &nb, &nr, nil);
696 assert(nr==utflen(t));
697 runemove(q+nr, p, n-pref);
699 rs = runestr(q, nr+(n-pref));
700 slash = rs.nr>0 && q[rs.nr-1] == '/';
703 rs.r = runerealloc(rs.r, rs.nr+1);
712 extern char **environ;
716 * Weird sort order -- shorter names first,
717 * prefer lowercase over uppercase ($home over $HOME),
718 * then do normal string comparison.
721 runeenvcmp(const void *va, const void *vb)
728 na = runestrchr(a, '=') - a;
729 nb = runestrchr(b, '=') - b;
736 if(islowerrune(a[0]) && !islowerrune(b[0]))
738 if(islowerrune(b[0]) && !islowerrune(a[0]))
740 return runestrncmp(a, b, na);
751 for(i=0; environ[i]; i++){
753 * Ignore some pollution.
755 if(environ[i][0] == '_')
757 if(strncmp(environ[i], "PWD=", 4) == 0)
759 if(strncmp(environ[i], "OLDPWD=", 7) == 0)
763 * Must be a rooted path.
765 if((p = strchr(environ[i], '=')) == nil || *(p+1) != '/')
769 * Only use the ones that we accept in look - all isfilec
772 r = bytetorune(environ[i], &nr);
773 for(q=r; *q != '='; q++)
780 runeenv = erealloc(runeenv, (n+1)*sizeof(runeenv[0]));
784 runeenv = erealloc(runeenv, (n+1)*sizeof(runeenv[0]));
786 qsort(runeenv, n, sizeof(runeenv[0]), runeenvcmp);
790 abbrevenv(Rune **rp, uint *np)
801 if(n == 0 || r[0] != '/')
807 for(i=0; runeenv[i]; i++){
808 p = runestrchr(runeenv[i], '=')+1;
810 if(len <= n && (r[len]==0 || r[len]=='/') && runeeq(r, len, p, len)==TRUE){
811 alen = (p-1) - runeenv[i];
812 q = runemalloc(1+alen+n-len);
814 runemove(q+1, runeenv[i], alen);
815 runemove(q+1+alen, r+len, n-len);