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 //assert(w->body.w == w);
81 w->dirty = clone->dirty;
82 textsetselect(&w->body, clone->body.q0, clone->body.q1);
88 winresize(Window *w, Rectangle r, int safe)
96 r1.max.y = r1.min.y + font->height;
98 if(!safe || !eqrect(w->tag.fr.r, r1)){
99 y = textresize(&w->tag, r1);
101 if(w->body.file->mod && !w->isdir && !w->isscratch)
103 br.min = w->tag.scrollr.min;
104 br.max.x = br.min.x + Dx(b->r);
105 br.max.y = br.min.y + Dy(b->r);
106 draw(screen, br, b, nil, b->r.min);
108 if(!safe || !eqrect(w->body.fr.r, r1)){
109 if(y+1+font->height > r.max.y){ /* no body */
112 textresize(&w->body, r1);
120 draw(screen, r1, tagcols[BORD], nil, ZP);
123 y = textresize(&w->body, r1);
126 textscrdraw(&w->body);
128 w->maxlines = min(w->body.fr.nlines, max(w->maxlines, w->body.fr.maxlines));
133 winlock1(Window *w, int owner)
141 winlock(Window *w, int owner)
146 //fprint(2, "winlock %p %d %lux\n", w, owner, getcallerpc(&w));
148 for(i=0; i<f->ntext; i++)
149 winlock1(f->text[i]->w, owner);
158 //fprint(2, "winunlock %p %lux\n", w, getcallerpc(&w));
160 for(i=0; i<f->ntext; i++){
165 /* winclose() can change up f->text; beware */
166 if(f->ntext>0 && w != f->text[i]->w)
167 --i; /* winclose() deleted window */
172 winmousebut(Window *w)
174 moveto(mousectl, divpt(addpt(w->tag.scrollr.min, w->tag.scrollr.max), 2));
178 windirfree(Window *w)
184 for(i=0; i<w->ndl; i++){
200 if(decref(&w->ref) == 0){
206 for(i=0; i<w->nincl; i++)
225 sendp(x->c, nil); /* wake him up */
230 winundo(Window *w, int isundo)
239 fileundo(body->file, isundo, &body->q0, &body->q1);
240 textshow(body, body->q0, body->q1, 1);
242 for(i=0; i<f->ntext; i++){
244 v->dirty = (f->seq != v->putseq);
246 v->body.q0 = v->body.fr.p0+v->body.org;
247 v->body.q1 = v->body.fr.p1+v->body.org;
254 winsetname(Window *w, Rune *name, int n)
259 static Rune Lslashguide[] = { '/', 'g', 'u', 'i', 'd', 'e', 0 };
260 static Rune Lpluserrors[] = { '+', 'E', 'r', 'r', 'o', 'r', 's', 0 };
262 if(runeeq(t->file->name, t->file->nname, name, n) == TRUE)
264 w->isscratch = FALSE;
265 if(n>=6 && runeeq(Lslashguide, 6, name+(n-6), 6))
267 else if(n>=7 && runeeq(Lpluserrors, 7, name+(n-7), 7))
269 filesetname(t->file, name, n);
270 for(i=0; i<t->file->ntext; i++){
271 v = t->file->text[i]->w;
273 v->isscratch = w->isscratch;
278 wintype(Window *w, Text *t, Rune r)
284 for(i=0; i<t->file->ntext; i++)
285 textscrdraw(t->file->text[i]);
290 wincleartag(Window *w)
295 /* w must be committed */
296 n = w->tag.file->b.nc;
298 bufread(&w->tag.file->b, 0, r, n);
300 if(r[i]==' ' || r[i]=='\t')
308 textdelete(&w->tag, i, n, TRUE);
310 w->tag.file->mod = FALSE;
315 textsetselect(&w->tag, w->tag.q0, w->tag.q1);
319 winsettag1(Window *w)
321 int i, j, k, n, bar, dirty;
326 static Rune Ldelsnarf[] = { ' ', 'D', 'e', 'l', ' ',
327 'S', 'n', 'a', 'r', 'f', 0 };
328 static Rune Lundo[] = { ' ', 'U', 'n', 'd', 'o', 0 };
329 static Rune Lredo[] = { ' ', 'R', 'e', 'd', 'o', 0 };
330 static Rune Lget[] = { ' ', 'G', 'e', 't', 0 };
331 static Rune Lput[] = { ' ', 'P', 'u', 't', 0 };
332 static Rune Llook[] = { ' ', 'L', 'o', 'o', 'k', ' ', 0 };
333 static Rune Lpipe[] = { ' ', '|', 0 };
334 /* there are races that get us here with stuff in the tag cache, so we take extra care to sync it */
335 if(w->tag.ncache!=0 || w->tag.file->mod)
336 wincommit(w, &w->tag); /* check file name; also guarantees we can modify tag contents */
337 old = runemalloc(w->tag.file->b.nc+1);
338 bufread(&w->tag.file->b, 0, old, w->tag.file->b.nc);
339 old[w->tag.file->b.nc] = '\0';
340 for(i=0; i<w->tag.file->b.nc; i++)
341 if(old[i]==' ' || old[i]=='\t')
343 if(runeeq(old, i, w->body.file->name, w->body.file->nname) == FALSE){
344 textdelete(&w->tag, 0, i, TRUE);
345 textinsert(&w->tag, 0, w->body.file->name, w->body.file->nname, TRUE);
347 old = runemalloc(w->tag.file->b.nc+1);
348 bufread(&w->tag.file->b, 0, old, w->tag.file->b.nc);
349 old[w->tag.file->b.nc] = '\0';
351 new = runemalloc(w->body.file->nname+100);
353 runemove(new+i, w->body.file->name, w->body.file->nname);
354 i += w->body.file->nname;
355 runemove(new+i, Ldelsnarf, 10);
358 if(w->body.file->delta.nc>0 || w->body.ncache){
359 runemove(new+i, Lundo, 5);
362 if(w->body.file->epsilon.nc > 0){
363 runemove(new+i, Lredo, 5);
366 dirty = w->body.file->nname && (w->body.ncache || w->body.file->seq!=w->putseq);
367 if(!w->isdir && dirty){
368 runemove(new+i, Lput, 4);
373 runemove(new+i, Lget, 4);
376 runemove(new+i, Lpipe, 2);
378 r = runestrchr(old, '|');
382 k = w->tag.file->b.nc;
383 if(w->body.file->seq == 0){
384 runemove(new+i, Llook, 6);
389 if(runestrlen(new) != i)
390 fprint(2, "s '%S' len not %d\n", new, i);
391 assert(i==runestrlen(new));
392 if(runeeq(new, i, old, k) == FALSE){
401 textdelete(&w->tag, j, k, TRUE);
402 textinsert(&w->tag, j, new+j, i-j, TRUE);
403 /* try to preserve user selection */
404 r = runestrchr(old, '|');
408 bar = (runestrchr(new, '|')-new)-bar;
416 w->tag.file->mod = FALSE;
417 n = w->tag.file->b.nc+w->tag.ncache;
422 textsetselect(&w->tag, w->tag.q0, w->tag.q1);
424 if(!w->isdir && !w->isscratch && (w->body.file->mod || w->body.ncache))
426 br.min = w->tag.scrollr.min;
427 br.max.x = br.min.x + Dx(b->r);
428 br.max.y = br.min.y + Dy(b->r);
429 draw(screen, br, b, nil, b->r.min);
440 for(i=0; i<f->ntext; i++){
442 if(v->col->safe || v->body.fr.maxlines>0)
448 wincommit(Window *w, Text *t)
457 for(i=0; i<f->ntext; i++)
458 textcommit(f->text[i], FALSE); /* no-op for t */
461 r = runemalloc(w->tag.file->b.nc);
462 bufread(&w->tag.file->b, 0, r, w->tag.file->b.nc);
463 for(i=0; i<w->tag.file->b.nc; i++)
464 if(r[i]==' ' || r[i]=='\t')
466 if(runeeq(r, i, w->body.file->name, w->body.file->nname) == FALSE){
468 filemark(w->body.file);
469 w->body.file->mod = TRUE;
478 winaddincl(Window *w, Rune *r, int n)
484 a = runetobyte(r, n);
489 rs = dirname(&w->body, r, n);
493 a = runetobyte(r, n);
497 r = runerealloc(r, n+1);
501 if((d->qid.type&QTDIR) == 0){
503 warning(nil, "%s: not a directory\n", a);
509 w->incl = realloc(w->incl, w->nincl*sizeof(Rune*));
510 memmove(w->incl+1, w->incl, (w->nincl-1)*sizeof(Rune*));
511 w->incl[0] = runemalloc(n+1);
512 runemove(w->incl[0], r, n);
517 warning(nil, "%s: %r\n", a);
524 winclean(Window *w, int conservative) /* as it stands, conservative is always TRUE */
526 if(w->isscratch || w->isdir) /* don't whine if it's a guide file, error window, etc. */
528 if(!conservative && w->nopen[QWevent]>0)
531 if(w->body.file->nname)
532 warning(nil, "%.*S modified\n", w->body.file->nname, w->body.file->name);
534 if(w->body.file->b.nc < 100) /* don't whine if it's too small */
536 warning(nil, "unnamed file modified\n");
545 winctlprint(Window *w, char *buf, int fonts)
549 n = 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 sprint(buf+n, "%11d %q %11d" , Dx(w->body.fr.r), w->body.reffont->f->name, w->body.fr.maxtab);
556 winevent(Window *w, char *fmt, ...)
563 if(w->nopen[QWevent] == 0)
566 error("no window owner");
568 b = vsmprint(fmt, arg);
571 error("vsmprint failed");
573 w->events = realloc(w->events, w->nevents+1+n);
574 w->events[w->nevents++] = w->owner;
575 memmove(w->events+w->nevents, b, n);