17 wininit(Window *w, Window *clone, Rectangle r)
38 w->tagtop.max.y = r.min.y + font->height;
39 r1.max.y = r1.min.y + w->taglines*font->height;
42 f = fileaddtext(nil, &w->tag);
43 textinit(&w->tag, f, r1, &reffont, tagcols);
45 /* tag is a copy of the contents, not a tracked image */
47 textdelete(&w->tag, 0, w->tag.file->b.nc, TRUE);
48 nc = clone->tag.file->b.nc;
50 bufread(&clone->tag.file->b, 0, rp, nc);
51 textinsert(&w->tag, 0, rp, nc, TRUE);
53 filereset(w->tag.file);
54 textsetselect(&w->tag, nc, nc);
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 f = fileaddtext(f, &w->body);
70 textinit(&w->body, f, r1, rf, textcols);
72 r1.max.y = r1.min.y+1;
73 draw(screen, r1, tagcols[BORD], nil, ZP);
74 textscrdraw(&w->body);
76 br.min = w->tag.scrollr.min;
77 br.max.x = br.min.x + Dx(button->r);
78 br.max.y = br.min.y + Dy(button->r);
79 draw(screen, br, button, nil, button->r.min);
81 w->maxlines = w->body.fr.maxlines;
82 w->autoindent = globalautoindent;
84 w->dirty = clone->dirty;
85 w->autoindent = clone->autoindent;
86 textsetselect(&w->body, clone->body.q0, clone->body.q1);
92 * Draw the appropriate button.
95 windrawbutton(Window *w)
101 if(!w->isdir && !w->isscratch && (w->body.file->mod || w->body.ncache))
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);
110 * Compute number of tag lines required
111 * to display entire tag text.
114 wintaglines(Window *w, Rectangle r)
121 w->tag.fr.noredraw = 1;
122 textresize(&w->tag, r, TRUE);
123 w->tag.fr.noredraw = 0;
126 /* can't use more than we have */
127 if(w->tag.fr.nlines >= w->tag.fr.maxlines)
128 return w->tag.fr.maxlines;
130 /* if tag ends with \n, include empty line at end for typing */
131 n = w->tag.fr.nlines;
132 if(w->tag.file->b.nc > 0){
133 bufread(&w->tag.file->b, w->tag.file->b.nc-1, &rune, 1);
143 winresize(Window *w, Rectangle r, int safe, int keepextra)
145 int oy, y, mouseintag, mouseinbody, tagresized;
149 mouseintag = ptinrect(mouse->xy, w->tag.all);
150 mouseinbody = ptinrect(mouse->xy, w->body.all);
152 /* tagtop is first line of tag */
154 w->tagtop.max.y = r.min.y+font->height;
157 r1.max.y = min(r.max.y, r1.min.y + w->taglines*font->height);
159 /* If needed, recompute number of lines in tag. */
160 if(!safe || !w->tagsafe || !eqrect(w->tag.all, r1)){
161 w->taglines = wintaglines(w, r);
162 r1.max.y = min(r.max.y, r1.min.y + w->taglines*font->height);
165 /* If needed, resize & redraw tag. */
168 if(!safe || !w->tagsafe || !eqrect(w->tag.all, r1)){
170 textresize(&w->tag, r1, TRUE);
171 y = w->tag.fr.r.max.y;
175 /* If mouse is in tag, pull up as tag closes. */
176 if(mouseintag && !ptinrect(mouse->xy, w->tag.all)){
178 p.y = w->tag.all.max.y-3;
182 /* If mouse is in body, push down as tag expands. */
183 if(mouseinbody && ptinrect(mouse->xy, w->tag.all)){
185 p.y = w->tag.all.max.y+3;
190 /* If needed, resize & redraw body. */
193 if(!safe || !eqrect(w->body.all, r1)){
195 if(y+1+w->body.fr.font->height <= r.max.y){ /* room for one line */
198 draw(screen, r1, tagcols[BORD], nil, ZP);
200 r1.min.y = min(y, r.max.y);
206 y = textresize(&w->body, r1, keepextra);
209 textscrdraw(&w->body);
210 w->body.all.min.y = oy;
212 w->maxlines = min(w->body.fr.nlines, max(w->maxlines, w->body.fr.maxlines));
217 winlock1(Window *w, int owner)
225 winlock(Window *w, int owner)
231 for(i=0; i<f->ntext; i++)
232 winlock1(f->text[i]->w, owner);
242 * subtle: loop runs backwards to avoid tripping over
243 * winclose indirectly editing f->text and freeing f
244 * on the last iteration of the loop.
247 for(i=f->ntext-1; i>=0; i--){
256 winmousebut(Window *w)
258 moveto(mousectl, addpt(w->tag.scrollr.min,
259 divpt(Pt(Dx(w->tag.scrollr), font->height), 2)));
263 windirfree(Window *w)
269 for(i=0; i<w->ndl; i++){
285 if(decref(&w->ref) == 0){
291 for(i=0; i<w->nincl; i++)
310 sendp(x->c, nil); /* wake him up */
315 winundo(Window *w, int isundo)
324 fileundo(body->file, isundo, &body->q0, &body->q1);
325 textshow(body, body->q0, body->q1, 1);
327 for(i=0; i<f->ntext; i++){
329 v->dirty = (f->seq != v->putseq);
331 v->body.q0 = v->body.fr.p0+v->body.org;
332 v->body.q1 = v->body.fr.p1+v->body.org;
339 winsetname(Window *w, Rune *name, int n)
344 static Rune Lslashguide[] = { '/', 'g', 'u', 'i', 'd', 'e', 0 };
345 static Rune Lpluserrors[] = { '+', 'E', 'r', 'r', 'o', 'r', 's', 0 };
348 if(runeeq(t->file->name, t->file->nname, name, n) == TRUE)
350 w->isscratch = FALSE;
351 if(n>=6 && runeeq(Lslashguide, 6, name+(n-6), 6))
353 else if(n>=7 && runeeq(Lpluserrors, 7, name+(n-7), 7))
355 filesetname(t->file, name, n);
356 for(i=0; i<t->file->ntext; i++){
357 v = t->file->text[i]->w;
359 v->isscratch = w->isscratch;
364 wintype(Window *w, Text *t, Rune r)
370 for(i=0; i<t->file->ntext; i++)
371 textscrdraw(t->file->text[i]);
376 wincleartag(Window *w)
381 /* w must be committed */
382 n = w->tag.file->b.nc;
384 bufread(&w->tag.file->b, 0, r, n);
386 if(r[i]==' ' || r[i]=='\t')
394 textdelete(&w->tag, i, n, TRUE);
396 w->tag.file->mod = FALSE;
401 textsetselect(&w->tag, w->tag.q0, w->tag.q1);
405 winsettag1(Window *w)
407 int i, j, k, n, bar, dirty, resize;
410 static Rune Ldelsnarf[] = { ' ', 'D', 'e', 'l', ' ',
411 'S', 'n', 'a', 'r', 'f', 0 };
412 static Rune Lundo[] = { ' ', 'U', 'n', 'd', 'o', 0 };
413 static Rune Lredo[] = { ' ', 'R', 'e', 'd', 'o', 0 };
414 static Rune Lget[] = { ' ', 'G', 'e', 't', 0 };
415 static Rune Lput[] = { ' ', 'P', 'u', 't', 0 };
416 static Rune Llook[] = { ' ', 'L', 'o', 'o', 'k', ' ', 0 };
417 static Rune Lpipe[] = { ' ', '|', 0 };
419 /* there are races that get us here with stuff in the tag cache, so we take extra care to sync it */
420 if(w->tag.ncache!=0 || w->tag.file->mod)
421 wincommit(w, &w->tag); /* check file name; also guarantees we can modify tag contents */
422 old = runemalloc(w->tag.file->b.nc+1);
423 bufread(&w->tag.file->b, 0, old, w->tag.file->b.nc);
424 old[w->tag.file->b.nc] = '\0';
425 for(i=0; i<w->tag.file->b.nc; i++)
426 if(old[i]==' ' || old[i]=='\t')
428 if(runeeq(old, i, w->body.file->name, w->body.file->nname) == FALSE){
429 textdelete(&w->tag, 0, i, TRUE);
430 textinsert(&w->tag, 0, w->body.file->name, w->body.file->nname, TRUE);
432 old = runemalloc(w->tag.file->b.nc+1);
433 bufread(&w->tag.file->b, 0, old, w->tag.file->b.nc);
434 old[w->tag.file->b.nc] = '\0';
437 /* compute the text for the whole tag, replacing current only if it differs */
438 new = runemalloc(w->body.file->nname+100);
440 runemove(new+i, w->body.file->name, w->body.file->nname);
441 i += w->body.file->nname;
442 runemove(new+i, Ldelsnarf, 10);
445 if(w->body.needundo || w->body.file->delta.nc>0 || w->body.ncache){
446 runemove(new+i, Lundo, 5);
449 if(w->body.file->epsilon.nc > 0){
450 runemove(new+i, Lredo, 5);
453 dirty = w->body.file->nname && (w->body.ncache || w->body.file->seq!=w->putseq);
454 if(!w->isdir && dirty){
455 runemove(new+i, Lput, 4);
460 runemove(new+i, Lget, 4);
463 runemove(new+i, Lpipe, 2);
465 r = runestrchr(old, '|');
469 k = w->tag.file->b.nc;
470 if(w->body.file->seq == 0){
471 runemove(new+i, Llook, 6);
477 /* replace tag if the new one is different */
479 if(runeeq(new, i, old, k) == FALSE){
489 textdelete(&w->tag, j, k, TRUE);
490 textinsert(&w->tag, j, new+j, i-j, TRUE);
491 /* try to preserve user selection */
492 r = runestrchr(old, '|');
496 bar = (runestrchr(new, '|')-new)-bar;
504 w->tag.file->mod = FALSE;
505 n = w->tag.file->b.nc+w->tag.ncache;
510 textsetselect(&w->tag, w->tag.q0, w->tag.q1);
514 winresize(w, w->r, TRUE, TRUE);
526 for(i=0; i<f->ntext; i++){
528 if(v->col->safe || v->body.fr.maxlines>0)
534 wincommit(Window *w, Text *t)
543 for(i=0; i<f->ntext; i++)
544 textcommit(f->text[i], FALSE); /* no-op for t */
547 r = runemalloc(w->tag.file->b.nc);
548 bufread(&w->tag.file->b, 0, r, w->tag.file->b.nc);
549 for(i=0; i<w->tag.file->b.nc; i++)
550 if(r[i]==' ' || r[i]=='\t')
552 if(runeeq(r, i, w->body.file->name, w->body.file->nname) == FALSE){
554 filemark(w->body.file);
555 w->body.file->mod = TRUE;
564 winaddincl(Window *w, Rune *r, int n)
570 a = runetobyte(r, n);
575 rs = dirname(&w->body, r, n);
579 a = runetobyte(r, n);
583 r = runerealloc(r, n+1);
587 if((d->qid.type&QTDIR) == 0){
589 warning(nil, "%s: not a directory\n", a);
595 w->incl = realloc(w->incl, w->nincl*sizeof(Rune*));
596 memmove(w->incl+1, w->incl, (w->nincl-1)*sizeof(Rune*));
597 w->incl[0] = runemalloc(n+1);
598 runemove(w->incl[0], r, n);
603 warning(nil, "%s: %r\n", a);
610 winclean(Window *w, int conservative) /* as it stands, conservative is always TRUE */
612 if(w->isscratch || w->isdir) /* don't whine if it's a guide file, error window, etc. */
614 if(!conservative && w->nopen[QWevent]>0)
617 if(w->body.file->nname)
618 warning(nil, "%.*S modified\n", w->body.file->nname, w->body.file->name);
620 if(w->body.file->b.nc < 100) /* don't whine if it's too small */
622 warning(nil, "unnamed file modified\n");
631 winctlprint(Window *w, char *buf, int fonts)
633 sprint(buf, "%11d %11d %11d %11d %11d ", w->id, w->tag.file->b.nc,
634 w->body.file->b.nc, w->isdir, w->dirty);
636 return smprint("%s%11d %q %11d ", buf, Dx(w->body.fr.r),
637 w->body.reffont->f->name, w->body.fr.maxtab);
642 winevent(Window *w, char *fmt, ...)
649 if(w->nopen[QWevent] == 0)
652 error("no window owner");
654 b = vsmprint(fmt, arg);
657 error("vsmprint failed");
659 w->events = erealloc(w->events, w->nevents+1+n);
660 w->events[w->nevents++] = w->owner;
661 memmove(w->events+w->nevents, b, n);