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 textsetselect(&w->body, clone->body.q0, clone->body.q1);
85 w->autoindent = clone->autoindent;
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 i, j, k, n, bar, dirty;
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')
345 if(runeeq(old, i, w->body.file->name, w->body.file->nname) == FALSE){
346 textdelete(&w->tag, 0, i, TRUE);
347 textinsert(&w->tag, 0, w->body.file->name, w->body.file->nname, TRUE);
349 old = runemalloc(w->tag.file->b.nc+1);
350 bufread(&w->tag.file->b, 0, old, w->tag.file->b.nc);
351 old[w->tag.file->b.nc] = '\0';
353 new = runemalloc(w->body.file->nname+100);
355 runemove(new+i, w->body.file->name, w->body.file->nname);
356 i += w->body.file->nname;
357 runemove(new+i, Ldelsnarf, 10);
360 if(w->body.file->delta.nc>0 || w->body.ncache){
361 runemove(new+i, Lundo, 5);
364 if(w->body.file->epsilon.nc > 0){
365 runemove(new+i, Lredo, 5);
368 dirty = w->body.file->nname && (w->body.ncache || w->body.file->seq!=w->putseq);
369 if(!w->isdir && dirty){
370 runemove(new+i, Lput, 4);
375 runemove(new+i, Lget, 4);
378 runemove(new+i, Lpipe, 2);
380 r = runestrchr(old, '|');
384 k = w->tag.file->b.nc;
385 if(w->body.file->seq == 0){
386 runemove(new+i, Llook, 6);
391 if(runestrlen(new) != i)
392 fprint(2, "s '%S' len not %d\n", new, i);
393 assert(i==runestrlen(new));
394 if(runeeq(new, i, old, k) == FALSE){
403 textdelete(&w->tag, j, k, TRUE);
404 textinsert(&w->tag, j, new+j, i-j, TRUE);
405 /* try to preserve user selection */
406 r = runestrchr(old, '|');
410 bar = (runestrchr(new, '|')-new)-bar;
418 w->tag.file->mod = FALSE;
419 n = w->tag.file->b.nc+w->tag.ncache;
424 textsetselect(&w->tag, w->tag.q0, w->tag.q1);
426 if(!w->isdir && !w->isscratch && (w->body.file->mod || w->body.ncache))
428 br.min = w->tag.scrollr.min;
429 br.max.x = br.min.x + Dx(b->r);
430 br.max.y = br.min.y + Dy(b->r);
431 draw(screen, br, b, nil, b->r.min);
442 for(i=0; i<f->ntext; i++){
444 if(v->col->safe || v->body.fr.maxlines>0)
450 wincommit(Window *w, Text *t)
459 for(i=0; i<f->ntext; i++)
460 textcommit(f->text[i], FALSE); /* no-op for t */
463 r = runemalloc(w->tag.file->b.nc);
464 bufread(&w->tag.file->b, 0, r, w->tag.file->b.nc);
465 for(i=0; i<w->tag.file->b.nc; i++)
466 if(r[i]==' ' || r[i]=='\t')
468 if(runeeq(r, i, w->body.file->name, w->body.file->nname) == FALSE){
470 filemark(w->body.file);
471 w->body.file->mod = TRUE;
480 winaddincl(Window *w, Rune *r, int n)
486 a = runetobyte(r, n);
491 rs = dirname(&w->body, r, n);
495 a = runetobyte(r, n);
499 r = runerealloc(r, n+1);
503 if((d->qid.type&QTDIR) == 0){
505 warning(nil, "%s: not a directory\n", a);
511 w->incl = realloc(w->incl, w->nincl*sizeof(Rune*));
512 memmove(w->incl+1, w->incl, (w->nincl-1)*sizeof(Rune*));
513 w->incl[0] = runemalloc(n+1);
514 runemove(w->incl[0], r, n);
519 warning(nil, "%s: %r\n", a);
526 winclean(Window *w, int conservative) /* as it stands, conservative is always TRUE */
528 if(w->isscratch || w->isdir) /* don't whine if it's a guide file, error window, etc. */
530 if(!conservative && w->nopen[QWevent]>0)
533 if(w->body.file->nname)
534 warning(nil, "%.*S modified\n", w->body.file->nname, w->body.file->name);
536 if(w->body.file->b.nc < 100) /* don't whine if it's too small */
538 warning(nil, "unnamed file modified\n");
547 winctlprint(Window *w, char *buf, int fonts)
549 sprint(buf, "%11d %11d %11d %11d %11d ", w->id, w->tag.file->b.nc,
550 w->body.file->b.nc, w->isdir, w->dirty);
552 return smprint("%s%11d %q %11d ", buf, Dx(w->body.fr.r),
553 w->body.reffont->f->name, w->body.fr.maxtab);
558 winevent(Window *w, char *fmt, ...)
565 if(w->nopen[QWevent] == 0)
568 error("no window owner");
570 b = vsmprint(fmt, arg);
573 error("vsmprint failed");
575 w->events = realloc(w->events, w->nevents+1+n);
576 w->events[w->nevents++] = w->owner;
577 memmove(w->events+w->nevents, b, n);