17 wininit(Window *w, Window *clone, Rectangle r)
32 r1.max.y = r1.min.y + font->height;
34 f = fileaddtext(nil, &w->tag);
35 textinit(&w->tag, f, r1, &reffont, tagcols);
37 /* tag is a copy of the contents, not a tracked image */
39 textdelete(&w->tag, 0, w->tag.file->b.nc, TRUE);
40 nc = clone->tag.file->b.nc;
42 bufread(&clone->tag.file->b, 0, rp, nc);
43 textinsert(&w->tag, 0, rp, nc, TRUE);
45 filereset(w->tag.file);
46 textsetselect(&w->tag, nc, nc);
49 r1.min.y += font->height + 1;
50 if(r1.max.y < r1.min.y)
55 w->body.org = clone->body.org;
56 w->isscratch = clone->isscratch;
57 rf = rfget(FALSE, FALSE, FALSE, clone->body.reffont->f->name);
59 rf = rfget(FALSE, FALSE, FALSE, nil);
60 f = fileaddtext(f, &w->body);
62 textinit(&w->body, f, r1, rf, textcols);
64 r1.max.y = r1.min.y+1;
65 draw(screen, r1, tagcols[BORD], nil, ZP);
66 textscrdraw(&w->body);
68 w->r.max.y = w->body.fr.r.max.y;
69 br.min = w->tag.scrollr.min;
70 br.max.x = br.min.x + Dx(button->r);
71 br.max.y = br.min.y + Dy(button->r);
72 draw(screen, br, button, nil, button->r.min);
74 w->maxlines = w->body.fr.maxlines;
76 w->dirty = clone->dirty;
77 textsetselect(&w->body, clone->body.q0, clone->body.q1);
83 winresize(Window *w, Rectangle r, int safe)
91 r1.max.y = r1.min.y + font->height;
93 if(!safe || !eqrect(w->tag.fr.r, r1)){
94 y = textresize(&w->tag, r1);
96 if(w->body.file->mod && !w->isdir && !w->isscratch)
98 br.min = w->tag.scrollr.min;
99 br.max.x = br.min.x + Dx(b->r);
100 br.max.y = br.min.y + Dy(b->r);
101 draw(screen, br, b, nil, b->r.min);
103 if(!safe || !eqrect(w->body.fr.r, r1)){
104 if(y+1+font->height > r.max.y){ /* no body */
107 textresize(&w->body, r1);
115 draw(screen, r1, tagcols[BORD], nil, ZP);
118 y = textresize(&w->body, r1);
121 textscrdraw(&w->body);
123 w->maxlines = min(w->body.fr.nlines, max(w->maxlines, w->body.fr.maxlines));
128 winlock1(Window *w, int owner)
136 winlock(Window *w, int owner)
142 for(i=0; i<f->ntext; i++)
143 winlock1(f->text[i]->w, owner);
153 for(i=0; i<f->ntext; i++){
158 /* winclose() can change up f->text; beware */
159 if(f->ntext>0 && w != f->text[i]->w)
160 --i; /* winclose() deleted window */
165 winmousebut(Window *w)
167 moveto(mousectl, divpt(addpt(w->tag.scrollr.min, w->tag.scrollr.max), 2));
171 windirfree(Window *w)
177 for(i=0; i<w->ndl; i++){
193 if(decref(&w->ref) == 0){
199 for(i=0; i<w->nincl; i++)
218 sendp(x->c, nil); /* wake him up */
223 winundo(Window *w, int isundo)
232 fileundo(body->file, isundo, &body->q0, &body->q1);
233 textshow(body, body->q0, body->q1, 1);
235 for(i=0; i<f->ntext; i++){
237 v->dirty = (f->seq != v->putseq);
239 v->body.q0 = v->body.fr.p0+v->body.org;
240 v->body.q1 = v->body.fr.p1+v->body.org;
247 winsetname(Window *w, Rune *name, int n)
252 static Rune Lslashguide[] = { '/', 'g', 'u', 'i', 'd', 'e', 0 };
253 static Rune Lpluserrors[] = { '+', 'E', 'r', 'r', 'o', 'r', 's', 0 };
255 if(runeeq(t->file->name, t->file->nname, name, n) == TRUE)
257 w->isscratch = FALSE;
258 if(n>=6 && runeeq(Lslashguide, 6, name+(n-6), 6))
260 else if(n>=7 && runeeq(Lpluserrors, 7, name+(n-7), 7))
262 filesetname(t->file, name, n);
263 for(i=0; i<t->file->ntext; i++){
264 v = t->file->text[i]->w;
266 v->isscratch = w->isscratch;
271 wintype(Window *w, Text *t, Rune r)
277 for(i=0; i<t->file->ntext; i++)
278 textscrdraw(t->file->text[i]);
283 wincleartag(Window *w)
288 /* w must be committed */
289 n = w->tag.file->b.nc;
291 bufread(&w->tag.file->b, 0, r, n);
293 if(r[i]==' ' || r[i]=='\t')
301 textdelete(&w->tag, i, n, TRUE);
303 w->tag.file->mod = FALSE;
308 textsetselect(&w->tag, w->tag.q0, w->tag.q1);
312 winsettag1(Window *w)
314 int i, j, k, n, bar, dirty;
319 static Rune Ldelsnarf[] = { ' ', 'D', 'e', 'l', ' ',
320 'S', 'n', 'a', 'r', 'f', 0 };
321 static Rune Lundo[] = { ' ', 'U', 'n', 'd', 'o', 0 };
322 static Rune Lredo[] = { ' ', 'R', 'e', 'd', 'o', 0 };
323 static Rune Lget[] = { ' ', 'G', 'e', 't', 0 };
324 static Rune Lput[] = { ' ', 'P', 'u', 't', 0 };
325 static Rune Llook[] = { ' ', 'L', 'o', 'o', 'k', ' ', 0 };
326 static Rune Lpipe[] = { ' ', '|', 0 };
327 /* there are races that get us here with stuff in the tag cache, so we take extra care to sync it */
328 if(w->tag.ncache!=0 || w->tag.file->mod)
329 wincommit(w, &w->tag); /* check file name; also guarantees we can modify tag contents */
330 old = runemalloc(w->tag.file->b.nc+1);
331 bufread(&w->tag.file->b, 0, old, w->tag.file->b.nc);
332 old[w->tag.file->b.nc] = '\0';
333 for(i=0; i<w->tag.file->b.nc; i++)
334 if(old[i]==' ' || old[i]=='\t')
336 if(runeeq(old, i, w->body.file->name, w->body.file->nname) == FALSE){
337 textdelete(&w->tag, 0, i, TRUE);
338 textinsert(&w->tag, 0, w->body.file->name, w->body.file->nname, TRUE);
340 old = runemalloc(w->tag.file->b.nc+1);
341 bufread(&w->tag.file->b, 0, old, w->tag.file->b.nc);
342 old[w->tag.file->b.nc] = '\0';
344 new = runemalloc(w->body.file->nname+100);
346 runemove(new+i, w->body.file->name, w->body.file->nname);
347 i += w->body.file->nname;
348 runemove(new+i, Ldelsnarf, 10);
351 if(w->body.file->delta.nc>0 || w->body.ncache){
352 runemove(new+i, Lundo, 5);
355 if(w->body.file->epsilon.nc > 0){
356 runemove(new+i, Lredo, 5);
359 dirty = w->body.file->nname && (w->body.ncache || w->body.file->seq!=w->putseq);
360 if(!w->isdir && dirty){
361 runemove(new+i, Lput, 4);
366 runemove(new+i, Lget, 4);
369 runemove(new+i, Lpipe, 2);
371 r = runestrchr(old, '|');
375 k = w->tag.file->b.nc;
376 if(w->body.file->seq == 0){
377 runemove(new+i, Llook, 6);
382 if(runestrlen(new) != i)
383 fprint(2, "s '%S' len not %d\n", new, i);
384 assert(i==runestrlen(new));
385 if(runeeq(new, i, old, k) == FALSE){
394 textdelete(&w->tag, j, k, TRUE);
395 textinsert(&w->tag, j, new+j, i-j, TRUE);
396 /* try to preserve user selection */
397 r = runestrchr(old, '|');
401 bar = (runestrchr(new, '|')-new)-bar;
409 w->tag.file->mod = FALSE;
410 n = w->tag.file->b.nc+w->tag.ncache;
415 textsetselect(&w->tag, w->tag.q0, w->tag.q1);
417 if(!w->isdir && !w->isscratch && (w->body.file->mod || w->body.ncache))
419 br.min = w->tag.scrollr.min;
420 br.max.x = br.min.x + Dx(b->r);
421 br.max.y = br.min.y + Dy(b->r);
422 draw(screen, br, b, nil, b->r.min);
433 for(i=0; i<f->ntext; i++){
435 if(v->col->safe || v->body.fr.maxlines>0)
441 wincommit(Window *w, Text *t)
450 for(i=0; i<f->ntext; i++)
451 textcommit(f->text[i], FALSE); /* no-op for t */
454 r = runemalloc(w->tag.file->b.nc);
455 bufread(&w->tag.file->b, 0, r, w->tag.file->b.nc);
456 for(i=0; i<w->tag.file->b.nc; i++)
457 if(r[i]==' ' || r[i]=='\t')
459 if(runeeq(r, i, w->body.file->name, w->body.file->nname) == FALSE){
461 filemark(w->body.file);
462 w->body.file->mod = TRUE;
471 winaddincl(Window *w, Rune *r, int n)
477 a = runetobyte(r, n);
482 rs = dirname(&w->body, r, n);
486 a = runetobyte(r, n);
490 r = runerealloc(r, n+1);
494 if((d->qid.type&QTDIR) == 0){
496 warning(nil, "%s: not a directory\n", a);
502 w->incl = realloc(w->incl, w->nincl*sizeof(Rune*));
503 memmove(w->incl+1, w->incl, (w->nincl-1)*sizeof(Rune*));
504 w->incl[0] = runemalloc(n+1);
505 runemove(w->incl[0], r, n);
510 warning(nil, "%s: %r\n", a);
517 winclean(Window *w, int conservative) /* as it stands, conservative is always TRUE */
519 if(w->isscratch || w->isdir) /* don't whine if it's a guide file, error window, etc. */
521 if(!conservative && w->nopen[QWevent]>0)
524 if(w->body.file->nname)
525 warning(nil, "%.*S modified\n", w->body.file->nname, w->body.file->name);
527 if(w->body.file->b.nc < 100) /* don't whine if it's too small */
529 warning(nil, "unnamed file modified\n");
538 winctlprint(Window *w, char *buf, int fonts)
542 n = sprint(buf, "%11d %11d %11d %11d %11d ", w->id, w->tag.file->b.nc,
543 w->body.file->b.nc, w->isdir, w->dirty);
545 sprint(buf+n, "%11d %s" , Dx(w->body.fr.r), w->body.reffont->f->name);
549 winevent(Window *w, char *fmt, ...)
556 if(w->nopen[QWevent] == 0)
559 error("no window owner");
561 b = vsmprint(fmt, arg);
564 error("vsmprint failed");
566 w->events = realloc(w->events, w->nevents+1+n);
567 w->events[w->nevents++] = w->owner;
568 memmove(w->events+w->nevents, b, n);