17 wininit(Window *w, Window *clone, Rectangle r)
36 r1.max.y = r1.min.y + font->height;
38 w->tagtop.max.y = r.min.y + font->height;
40 f = fileaddtext(nil, &w->tag);
41 textinit(&w->tag, f, r1, &reffont, tagcols);
43 /* tag is a copy of the contents, not a tracked image */
45 textdelete(&w->tag, 0, w->tag.file->nc, TRUE);
46 nc = clone->tag.file->nc;
48 bufread(clone->tag.file, 0, rp, nc);
49 textinsert(&w->tag, 0, rp, nc, TRUE);
51 filereset(w->tag.file);
52 textsetselect(&w->tag, nc, nc);
55 r1.min.y += w->taglines*font->height + 1;
56 if(r1.max.y < r1.min.y)
61 w->body.org = clone->body.org;
62 w->isscratch = clone->isscratch;
63 rf = rfget(FALSE, FALSE, FALSE, clone->body.reffont->f->name);
65 rf = rfget(FALSE, FALSE, FALSE, nil);
66 f = fileaddtext(f, &w->body);
68 textinit(&w->body, f, r1, rf, textcols);
70 r1.max.y = r1.min.y+1;
71 draw(screen, r1, tagcols[BORD], nil, ZP);
72 textscrdraw(&w->body);
74 w->r.max.y = w->body.r.max.y;
75 br.min = w->tag.scrollr.min;
76 br.max.x = br.min.x + Dx(button->r);
77 br.max.y = br.min.y + Dy(button->r);
78 draw(screen, br, button, nil, button->r.min);
80 w->maxlines = w->body.maxlines;
81 w->autoindent = globalautoindent;
83 w->dirty = clone->dirty;
84 w->autoindent = clone->autoindent;
85 textsetselect(&w->body, clone->body.q0, clone->body.q1);
87 w->autoindent = clone->autoindent;
97 for(n=0; n<w->tag.file->nc; n++) {
98 bufread(w->tag.file, n, &rune, 1);
103 if(n >= w->tag.file->nc)
116 moveto(mousectl, addpt(frptofchar(&w->tag, n), Pt(4, w->tag.font->height-4)));
120 * Compute number of tag lines required
121 * to display entire tag text.
124 wintaglines(Window *w, Rectangle r)
130 if(!w->tagexpand && !w->showdel)
134 textresize(&w->tag, r);
139 /* use just as many lines as needed to show the Del */
143 p = subpt(frptofchar(&w->tag, n), w->tag.r.min);
144 return 1 + p.y / w->tag.font->height;
147 /* can't use more than we have */
148 if(w->tag.nlines >= w->tag.maxlines)
149 return w->tag.maxlines;
151 /* if tag ends with \n, include empty line at end for typing */
153 if(w->tag.file->nc > 0){
154 bufread(w->tag.file, w->tag.file->nc-1, &rune, 1);
164 winresize(Window *w, Rectangle r, int safe)
166 int oy, mouseintag, mouseinbody;
173 mouseintag = ptinrect(mouse->xy, w->tag.all);
174 mouseinbody = ptinrect(mouse->xy, w->body.all);
177 w->tagtop.max.y = r.min.y+font->height;
180 r1.max.y = r1.min.y + font->height;
181 r1.max.y = min(r.max.y, r1.min.y + w->taglines*font->height);
183 if(!safe || !w->tagsafe || !eqrect(w->tag.all, r1)){
184 w->taglines = wintaglines(w, r);
185 r1.max.y = min(r.max.y, r1.min.y + w->taglines*font->height);
187 if(Dy(r1) < font->height)
188 r1.max.y = r1.min.y+font->height;
190 if(!safe || !eqrect(w->tag.r, r1)){
191 textresize(&w->tag, r1);
194 if(w->body.file->mod && !w->isdir && !w->isscratch)
196 br.min = w->tag.scrollr.min;
197 br.max.x = br.min.x + Dx(b->r);
198 br.max.y = br.min.y + Dy(b->r);
199 draw(screen, br, b, nil, b->r.min);
203 /* If mouse is in tag, pull up as tag closes. */
204 if(mouseintag && !ptinrect(mouse->xy, w->tag.all)){
206 p.y = w->tag.all.max.y-3;
210 /* If mouse is in body, push down as tag expands. */
211 if(mouseinbody && ptinrect(mouse->xy, w->tag.all)){
213 p.y = w->tag.all.max.y+3;
218 if(!safe || !eqrect(w->body.r, r1)){
220 if(y+1+w->body.font->height <= r.max.y){ /* room for one line */
223 draw(screen, r1, tagcols[BORD], nil, ZP);
225 r1.min.y = min(y, r.max.y);
231 y = textresize(&w->body, r1);
234 textscrdraw(&w->body);
235 w->body.all.min.y = oy;
237 w->maxlines = min(w->body.nlines, max(w->maxlines, w->body.maxlines));
242 winlock1(Window *w, int owner)
250 winlock(Window *w, int owner)
256 for(i=0; i<f->ntext; i++)
257 winlock1(f->text[i]->w, owner);
267 * subtle: loop runs backwards to avoid tripping over
268 * winclose indirectly editing f->text and freeing f
269 * on the last iteration of the loop.
272 for(i=f->ntext-1; i>=0; i--){
281 winmousebut(Window *w)
283 moveto(mousectl, addpt(w->tag.scrollr.min, divpt(Pt(Dx(w->tag.scrollr), font->height), 2)));
287 windirfree(Window *w)
293 for(i=0; i<w->ndl; i++){
315 for(i=0; i<w->nincl; i++)
334 sendp(x->c, nil); /* wake him up */
339 winundo(Window *w, int isundo)
348 fileundo(body->file, isundo, &body->q0, &body->q1);
349 textshow(body, body->q0, body->q1, 1);
351 for(i=0; i<f->ntext; i++){
353 v->dirty = (f->seq != v->putseq);
355 v->body.q0 = v->body.p0+v->body.org;
356 v->body.q1 = v->body.p1+v->body.org;
363 winsetname(Window *w, Rune *name, int n)
370 if(runeeq(t->file->name, t->file->nname, name, n) == TRUE)
372 w->isscratch = FALSE;
373 if(n>=6 && runeeq(L"/guide", 6, name+(n-6), 6))
375 else if(n>=7 && runeeq(L"+Errors", 7, name+(n-7), 7))
377 filesetname(t->file, name, n);
378 for(i=0; i<t->file->ntext; i++){
379 v = t->file->text[i]->w;
381 v->isscratch = w->isscratch;
386 wintype(Window *w, Text *t, Rune r)
394 for(i=0; i<t->file->ntext; i++)
395 textscrdraw(t->file->text[i]);
400 wincleartag(Window *w)
405 /* w must be committed */
408 bufread(w->tag.file, 0, r, n);
410 if(r[i]==' ' || r[i]=='\t')
418 textdelete(&w->tag, i, n, TRUE);
420 w->tag.file->mod = FALSE;
425 textsetselect(&w->tag, w->tag.q0, w->tag.q1);
429 winsettag1(Window *w)
431 int i, j, k, n, bar, dirty;
437 /* there are races that get us here with stuff in the tag cache, so we take extra care to sync it */
438 if(w->tag.ncache!=0 || w->tag.file->mod)
439 wincommit(w, &w->tag); /* check file name; also guarantees we can modify tag contents */
440 old = runemalloc(w->tag.file->nc+1);
441 bufread(w->tag.file, 0, old, w->tag.file->nc);
442 old[w->tag.file->nc] = '\0';
443 for(i=0; i<w->tag.file->nc; i++)
444 if(old[i]==' ' || old[i]=='\t')
446 if(runeeq(old, i, w->body.file->name, w->body.file->nname) == FALSE){
447 textdelete(&w->tag, 0, i, TRUE);
448 textinsert(&w->tag, 0, w->body.file->name, w->body.file->nname, TRUE);
450 old = runemalloc(w->tag.file->nc+1);
451 bufread(w->tag.file, 0, old, w->tag.file->nc);
452 old[w->tag.file->nc] = '\0';
455 new = runemalloc(w->body.file->nname+100);
457 runemove(new+i, w->body.file->name, w->body.file->nname);
458 i += w->body.file->nname;
459 runemove(new+i, L" Del Snarf", 10);
462 if(w->body.file->delta.nc>0 || w->body.ncache){
463 runemove(new+i, L" Undo", 5);
466 if(w->body.file->epsilon.nc > 0){
467 runemove(new+i, L" Redo", 5);
470 dirty = w->body.file->nname && (w->body.ncache || w->body.file->seq!=w->putseq);
471 if(!w->isdir && dirty){
472 runemove(new+i, L" Put", 4);
477 runemove(new+i, L" Get", 4);
480 runemove(new+i, L" |", 2);
482 r = runestrchr(old, '|');
487 if(w->body.file->seq == 0){
488 runemove(new+i, L" Look ", 6);
494 /* replace tag if the new one is different */
495 if(runeeq(new, i, old, k) == FALSE){
504 textdelete(&w->tag, j, k, TRUE);
505 textinsert(&w->tag, j, new+j, i-j, TRUE);
506 /* try to preserve user selection */
507 r = runestrchr(old, '|');
511 bar = (runestrchr(new, '|')-new)-bar;
520 w->tag.file->mod = FALSE;
521 n = w->tag.file->nc+w->tag.ncache;
526 textsetselect(&w->tag, w->tag.q0, w->tag.q1);
528 if(!w->isdir && !w->isscratch && (w->body.file->mod || w->body.ncache))
530 br.min = w->tag.scrollr.min;
531 br.max.x = br.min.x + Dx(b->r);
532 br.max.y = br.min.y + Dy(b->r);
533 draw(screen, br, b, nil, b->r.min);
534 if(w->tagsafe == FALSE)
535 winresize(w, w->r, TRUE);
546 for(i=0; i<f->ntext; i++){
548 if(v->col->safe || v->body.maxlines>0)
554 wincommit(Window *w, Text *t)
563 for(i=0; i<f->ntext; i++)
564 textcommit(f->text[i], FALSE); /* no-op for t */
567 r = runemalloc(w->tag.file->nc);
568 bufread(w->tag.file, 0, r, w->tag.file->nc);
569 for(i=0; i<w->tag.file->nc; i++)
570 if(r[i]==' ' || r[i]=='\t')
572 if(runeeq(r, i, w->body.file->name, w->body.file->nname) == FALSE){
574 filemark(w->body.file);
575 w->body.file->mod = TRUE;
584 winaddincl(Window *w, Rune *r, int n)
590 a = runetobyte(r, n);
595 rs = dirname(&w->body, r, n);
599 a = runetobyte(r, n);
603 r = runerealloc(r, n+1);
607 if((d->qid.type&QTDIR) == 0){
609 warning(nil, "%s: not a directory\n", a);
615 w->incl = realloc(w->incl, w->nincl*sizeof(Rune*));
616 memmove(w->incl+1, w->incl, (w->nincl-1)*sizeof(Rune*));
617 w->incl[0] = runemalloc(n+1);
618 runemove(w->incl[0], r, n);
623 warning(nil, "%s: %r\n", a);
630 winclean(Window *w, int conservative) /* as it stands, conservative is always TRUE */
632 if(w->isscratch || w->isdir) /* don't whine if it's a guide file, error window, etc. */
634 if(!conservative && w->nopen[QWevent]>0)
637 if(w->body.file->nname)
638 warning(nil, "%.*S modified\n", w->body.file->nname, w->body.file->name);
640 if(w->body.file->nc < 100) /* don't whine if it's too small */
642 warning(nil, "unnamed file modified\n");
651 winctlprint(Window *w, char *buf, int fonts)
653 sprint(buf, "%11d %11d %11d %11d %11d ", w->id, w->tag.file->nc,
654 w->body.file->nc, w->isdir, w->dirty);
656 return smprint("%s%11d %q %11d " , buf, Dx(w->body.r),
657 w->body.reffont->f->name, w->body.maxtab);
662 winevent(Window *w, char *fmt, ...)
669 if(w->nopen[QWevent] == 0)
672 error("no window owner");
674 b = vsmprint(fmt, arg);
677 error("vsmprint failed");
679 w->events = erealloc(w->events, w->nevents+1+n);
680 w->events[w->nevents++] = w->owner;
681 memmove(w->events+w->nevents, b, n);