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)
149 for(i=0; i<f->ntext; i++)
150 winlock1(f->text[i]->w, owner);
160 * subtle: loop runs backwards to avoid tripping over
161 * winclose indirectly editing f->text and freeing f
162 * on the last iteration of the loop.
165 for(i=f->ntext-1; i>=0; i--){
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, (uint*)&ntagname);
353 * XXX Why is this here instead of letting the code
354 * down below do the work?
356 if(runeeq(old, i, tagname, ntagname) == FALSE){
359 textdelete(&w->tag, 0, i, TRUE);
360 textinsert(&w->tag, 0, tagname, ntagname, TRUE);
362 old = runemalloc(w->tag.file->b.nc+1);
363 bufread(&w->tag.file->b, 0, old, w->tag.file->b.nc);
364 old[w->tag.file->b.nc] = '\0';
367 * XXX common case - typing at end of name
369 w->tag.q0 = q0+ntagname-i;
370 w->tag.q1 = q1+ntagname-i;
374 /* compute the text for the whole tag, replacing current only if it differs */
375 new = runemalloc(ntagname+100);
377 runemove(new+i, tagname, ntagname);
379 runemove(new+i, Ldelsnarf, 10);
382 if(w->body.file->delta.nc>0 || w->body.ncache){
383 runemove(new+i, Lundo, 5);
386 if(w->body.file->epsilon.nc > 0){
387 runemove(new+i, Lredo, 5);
390 dirty = w->body.file->nname && (w->body.ncache || w->body.file->seq!=w->putseq);
391 if(!w->isdir && dirty){
392 runemove(new+i, Lput, 4);
397 runemove(new+i, Lget, 4);
400 runemove(new+i, Lpipe, 2);
402 r = runestrchr(old, '|');
406 k = w->tag.file->b.nc;
407 if(w->body.file->seq == 0){
408 runemove(new+i, Llook, 6);
413 if(runestrlen(new) != i)
414 fprint(2, "s '%S' len not %d\n", new, i);
415 assert(i==runestrlen(new));
417 /* replace tag if the new one is different */
418 if(runeeq(new, i, old, k) == FALSE){
427 textdelete(&w->tag, j, k, TRUE);
428 textinsert(&w->tag, j, new+j, i-j, TRUE);
429 /* try to preserve user selection */
430 r = runestrchr(old, '|');
434 bar = (runestrchr(new, '|')-new)-bar;
443 w->tag.file->mod = FALSE;
444 n = w->tag.file->b.nc+w->tag.ncache;
449 textsetselect(&w->tag, w->tag.q0, w->tag.q1);
451 if(!w->isdir && !w->isscratch && (w->body.file->mod || w->body.ncache))
453 br.min = w->tag.scrollr.min;
454 br.max.x = br.min.x + Dx(b->r);
455 br.max.y = br.min.y + Dy(b->r);
456 draw(screen, br, b, nil, b->r.min);
467 for(i=0; i<f->ntext; i++){
469 if(v->col->safe || v->body.fr.maxlines>0)
475 wincommit(Window *w, Text *t)
484 for(i=0; i<f->ntext; i++)
485 textcommit(f->text[i], FALSE); /* no-op for t */
488 r = runemalloc(w->tag.file->b.nc);
489 bufread(&w->tag.file->b, 0, r, w->tag.file->b.nc);
490 for(i=0; i<w->tag.file->b.nc; i++)
491 if(r[i]==' ' || r[i]=='\t')
493 expandenv(&r, (uint*)&i);
494 if(runeeq(r, i, w->body.file->name, w->body.file->nname) == FALSE){
496 filemark(w->body.file);
497 w->body.file->mod = TRUE;
506 winaddincl(Window *w, Rune *r, int n)
512 a = runetobyte(r, n);
517 rs = dirname(&w->body, r, n);
521 a = runetobyte(r, n);
525 r = runerealloc(r, n+1);
529 if((d->qid.type&QTDIR) == 0){
531 warning(nil, "%s: not a directory\n", a);
537 w->incl = realloc(w->incl, w->nincl*sizeof(Rune*));
538 memmove(w->incl+1, w->incl, (w->nincl-1)*sizeof(Rune*));
539 w->incl[0] = runemalloc(n+1);
540 runemove(w->incl[0], r, n);
545 warning(nil, "%s: %r\n", a);
552 winclean(Window *w, int conservative) /* as it stands, conservative is always TRUE */
554 if(w->isscratch || w->isdir) /* don't whine if it's a guide file, error window, etc. */
556 if(!conservative && w->nopen[QWevent]>0)
559 if(w->body.file->nname)
560 warning(nil, "%.*S modified\n", w->body.file->nname, w->body.file->name);
562 if(w->body.file->b.nc < 100) /* don't whine if it's too small */
564 warning(nil, "unnamed file modified\n");
573 winctlprint(Window *w, char *buf, int fonts)
575 sprint(buf, "%11d %11d %11d %11d %11d ", w->id, w->tag.file->b.nc,
576 w->body.file->b.nc, w->isdir, w->dirty);
578 return smprint("%s%11d %q %11d ", buf, Dx(w->body.fr.r),
579 w->body.reffont->f->name, w->body.fr.maxtab);
584 winevent(Window *w, char *fmt, ...)
591 if(w->nopen[QWevent] == 0)
594 error("no window owner");
596 b = vsmprint(fmt, arg);
599 error("vsmprint failed");
601 w->events = realloc(w->events, w->nevents+1+n);
602 w->events[w->nevents++] = w->owner;
603 memmove(w->events+w->nevents, b, n);
614 * This is here as a first stab at something.
615 * Run acme with the -'$' flag to enable it.
617 * This code isn't quite right, in that it doesn't play well with
618 * the plumber and with other applications. For example:
620 * If the window tag is $home/bin and you execute script, then acme runs
621 * script in $home/bin, via the shell, so everything is fine. If you do
622 * execute "echo $home", it too goes to the shell so you see the value
623 * of $home. And if you right-click on script, then acme plumbs "script"
624 * in the directory "/home/you/bin", so that works, but if you right-click
625 * on "$home/bin/script", then what? It's not correct to expand in acme
626 * since what you're plumbing might be a price tag for all we know. So the
627 * plumber has to expand it, but in order to do that the plumber should
628 * probably publish (and allow users to change) the set of variables it is
629 * using in expansions.
631 * Rob has suggested that a better solution is to make tag lines expand
632 * automatically to fit the necessary number of lines.
634 * The best solution, of course, is to use nice short path names, but this
635 * is not always possible.
639 expandenv(Rune **rp, uint *np)
652 if(n == 0 || r[0] != '$')
654 for(p=r+1; *p && *p != '/'; p++)
657 s = runetobyte(r+1, pref-1);
658 if((t = getenv(s)) == nil){
663 q = runemalloc(utflen(t)+(n-pref));
664 cvttorunes(t, strlen(t), q, &nb, &nr, nil);
665 assert(nr==utflen(t));
666 runemove(q+nr, p, n-pref);
668 rs = runestr(q, nr+(n-pref));
669 slash = rs.nr>0 && q[rs.nr-1] == '/';
672 rs.r = runerealloc(rs.r, rs.nr+1);
681 extern char **environ;
685 * Weird sort order -- shorter names first,
686 * prefer lowercase over uppercase ($home over $HOME),
687 * then do normal string comparison.
690 runeenvcmp(const void *va, const void *vb)
697 na = runestrchr(a, '=') - a;
698 nb = runestrchr(b, '=') - b;
705 if(islowerrune(a[0]) && !islowerrune(b[0]))
707 if(islowerrune(b[0]) && !islowerrune(a[0]))
709 return runestrncmp(a, b, na);
720 for(i=0; environ[i]; i++){
722 * Ignore some pollution.
724 if(environ[i][0] == '_')
726 if(strncmp(environ[i], "PWD=", 4) == 0)
728 if(strncmp(environ[i], "OLDPWD=", 7) == 0)
732 * Must be a rooted path.
734 if((p = strchr(environ[i], '=')) == nil || *(p+1) != '/')
738 * Only use the ones that we accept in look - all isfilec
741 r = bytetorune(environ[i], &nr);
742 for(q=r; *q != '='; q++)
749 runeenv = erealloc(runeenv, (n+1)*sizeof(runeenv[0]));
753 runeenv = erealloc(runeenv, (n+1)*sizeof(runeenv[0]));
755 qsort(runeenv, n, sizeof(runeenv[0]), runeenvcmp);
759 abbrevenv(Rune **rp, uint *np)
770 if(n == 0 || r[0] != '/')
776 for(i=0; runeenv[i]; i++){
777 p = runestrchr(runeenv[i], '=')+1;
779 if(len <= n && (r[len]==0 || r[len]=='/') && runeeq(r, len, p, len)==TRUE){
780 alen = (p-1) - runeenv[i];
781 q = runemalloc(1+alen+n-len);
783 runemove(q+1, runeenv[i], alen);
784 runemove(q+1+alen, r+len, n-len);