18 wininit(Window *w, Window *clone, Rectangle r)
39 w->tagtop.max.y = r.min.y + font->height;
40 r1.max.y = r1.min.y + w->taglines*font->height;
43 f = fileaddtext(nil, &w->tag);
44 textinit(&w->tag, f, r1, &reffont, tagcols);
46 /* tag is a copy of the contents, not a tracked image */
48 textdelete(&w->tag, 0, w->tag.file->b.nc, TRUE);
49 nc = clone->tag.file->b.nc;
51 bufread(&clone->tag.file->b, 0, rp, nc);
52 textinsert(&w->tag, 0, rp, nc, TRUE);
54 filereset(w->tag.file);
55 textsetselect(&w->tag, nc, nc);
58 r1.min.y += w->taglines*font->height + 1;
59 if(r1.max.y < r1.min.y)
64 w->body.org = clone->body.org;
65 w->isscratch = clone->isscratch;
66 rf = rfget(FALSE, FALSE, FALSE, clone->body.reffont->f->name);
68 rf = rfget(FALSE, FALSE, FALSE, nil);
69 f = fileaddtext(f, &w->body);
71 textinit(&w->body, f, r1, rf, textcols);
73 r1.max.y = r1.min.y+1;
74 draw(screen, r1, tagcols[BORD], nil, ZP);
75 textscrdraw(&w->body);
77 br.min = w->tag.scrollr.min;
78 br.max.x = br.min.x + Dx(button->r);
79 br.max.y = br.min.y + Dy(button->r);
80 draw(screen, br, button, nil, button->r.min);
82 w->maxlines = w->body.fr.maxlines;
83 w->autoindent = globalautoindent;
85 w->dirty = clone->dirty;
86 w->autoindent = clone->autoindent;
87 textsetselect(&w->body, clone->body.q0, clone->body.q1);
93 * Draw the appropriate button.
96 windrawbutton(Window *w)
102 if(!w->isdir && !w->isscratch && (w->body.file->mod || w->body.ncache))
104 br.min = w->tag.scrollr.min;
105 br.max.x = br.min.x + Dx(b->r);
106 br.max.y = br.min.y + Dy(b->r);
107 draw(screen, br, b, nil, b->r.min);
111 delrunepos(Window *w)
116 r = parsetag(w, 0, &i);
119 if(i >= w->tag.file->b.nc)
132 moveto(mousectl, addpt(frptofchar(&w->tag.fr, n), Pt(4, w->tag.fr.font->height-4)));
136 * Compute number of tag lines required
137 * to display entire tag text.
140 wintaglines(Window *w, Rectangle r)
146 if(!w->tagexpand && !w->showdel)
149 w->tag.fr.noredraw = 1;
150 textresize(&w->tag, r, TRUE);
151 w->tag.fr.noredraw = 0;
155 /* use just as many lines as needed to show the Del */
159 p = subpt(frptofchar(&w->tag.fr, n), w->tag.fr.r.min);
160 return 1 + p.y / w->tag.fr.font->height;
163 /* can't use more than we have */
164 if(w->tag.fr.nlines >= w->tag.fr.maxlines)
165 return w->tag.fr.maxlines;
167 /* if tag ends with \n, include empty line at end for typing */
168 n = w->tag.fr.nlines;
169 if(w->tag.file->b.nc > 0){
170 bufread(&w->tag.file->b, w->tag.file->b.nc-1, &rune, 1);
180 winresize(Window *w, Rectangle r, int safe, int keepextra)
182 int oy, y, mouseintag, mouseinbody;
186 mouseintag = ptinrect(mouse->xy, w->tag.all);
187 mouseinbody = ptinrect(mouse->xy, w->body.all);
189 /* tagtop is first line of tag */
191 w->tagtop.max.y = r.min.y+font->height;
194 r1.max.y = min(r.max.y, r1.min.y + w->taglines*font->height);
196 /* If needed, recompute number of lines in tag. */
197 if(!safe || !w->tagsafe || !eqrect(w->tag.all, r1)){
198 w->taglines = wintaglines(w, r);
199 r1.max.y = min(r.max.y, r1.min.y + w->taglines*font->height);
202 /* If needed, resize & redraw tag. */
204 if(!safe || !w->tagsafe || !eqrect(w->tag.all, r1)){
205 textresize(&w->tag, r1, TRUE);
206 y = w->tag.fr.r.max.y;
210 /* If mouse is in tag, pull up as tag closes. */
211 if(mouseintag && !ptinrect(mouse->xy, w->tag.all)){
213 p.y = w->tag.all.max.y-3;
217 /* If mouse is in body, push down as tag expands. */
218 if(mouseinbody && ptinrect(mouse->xy, w->tag.all)){
220 p.y = w->tag.all.max.y+3;
225 /* If needed, resize & redraw body. */
228 if(!safe || !eqrect(w->body.all, r1)){
230 if(y+1+w->body.fr.font->height <= r.max.y){ /* room for one line */
233 draw(screen, r1, tagcols[BORD], nil, ZP);
235 r1.min.y = min(y, r.max.y);
241 y = textresize(&w->body, r1, keepextra);
244 textscrdraw(&w->body);
245 w->body.all.min.y = oy;
247 w->maxlines = min(w->body.fr.nlines, max(w->maxlines, w->body.fr.maxlines));
252 winlock1(Window *w, int owner)
260 winlock(Window *w, int owner)
266 for(i=0; i<f->ntext; i++)
267 winlock1(f->text[i]->w, owner);
277 * subtle: loop runs backwards to avoid tripping over
278 * winclose indirectly editing f->text and freeing f
279 * on the last iteration of the loop.
282 for(i=f->ntext-1; i>=0; i--){
291 winmousebut(Window *w)
293 moveto(mousectl, addpt(w->tag.scrollr.min,
294 divpt(Pt(Dx(w->tag.scrollr), font->height), 2)));
298 windirfree(Window *w)
304 for(i=0; i<w->ndl; i++){
320 if(decref(&w->ref) == 0){
327 for(i=0; i<w->nincl; i++)
346 sendp(x->c, nil); /* wake him up */
351 winundo(Window *w, int isundo)
360 fileundo(body->file, isundo, &body->q0, &body->q1);
361 textshow(body, body->q0, body->q1, 1);
363 for(i=0; i<f->ntext; i++){
365 v->dirty = (f->seq != v->putseq);
367 v->body.q0 = v->body.fr.p0+v->body.org;
368 v->body.q1 = v->body.fr.p1+v->body.org;
375 winsetname(Window *w, Rune *name, int n)
380 static Rune Lslashguide[] = { '/', 'g', 'u', 'i', 'd', 'e', 0 };
381 static Rune Lpluserrors[] = { '+', 'E', 'r', 'r', 'o', 'r', 's', 0 };
384 if(runeeq(t->file->name, t->file->nname, name, n) == TRUE)
386 w->isscratch = FALSE;
387 if(n>=6 && runeeq(Lslashguide, 6, name+(n-6), 6))
389 else if(n>=7 && runeeq(Lpluserrors, 7, name+(n-7), 7))
391 filesetname(t->file, name, n);
392 for(i=0; i<t->file->ntext; i++){
393 v = t->file->text[i]->w;
395 v->isscratch = w->isscratch;
400 wintype(Window *w, Text *t, Rune r)
406 for(i=0; i<t->file->ntext; i++)
407 textscrdraw(t->file->text[i]);
412 wincleartag(Window *w)
417 /* w must be committed */
418 n = w->tag.file->b.nc;
419 r = parsetag(w, 0, &i);
426 textdelete(&w->tag, i, n, TRUE);
428 w->tag.file->mod = FALSE;
433 textsetselect(&w->tag, w->tag.q0, w->tag.q1);
437 parsetag(Window *w, int extra, int *len)
439 static Rune Ldelsnarf[] = { ' ', 'D', 'e', 'l', ' ', 'S', 'n', 'a', 'r', 'f', 0 };
440 static Rune Lspacepipe[] = { ' ', '|', 0 };
441 static Rune Ltabpipe[] = { '\t', '|', 0 };
445 r = runemalloc(w->tag.file->b.nc+extra+1);
446 bufread(&w->tag.file->b, 0, r, w->tag.file->b.nc);
447 r[w->tag.file->b.nc] = '\0';
450 * " |" or "\t|" ends left half of tag
451 * If we find " Del Snarf" in the left half of the tag
452 * (before the pipe), that ends the file name.
454 pipe = runestrstr(r, Lspacepipe);
455 if((p = runestrstr(r, Ltabpipe)) != nil && (pipe == nil || p < pipe))
457 if((p = runestrstr(r, Ldelsnarf)) != nil && (pipe == nil || p < pipe))
460 for(i=0; i<w->tag.file->b.nc; i++)
461 if(r[i]==' ' || r[i]=='\t')
469 winsettag1(Window *w)
471 int i, j, k, n, bar, dirty, resize;
474 static Rune Ldelsnarf[] = { ' ', 'D', 'e', 'l', ' ',
475 'S', 'n', 'a', 'r', 'f', 0 };
476 static Rune Lundo[] = { ' ', 'U', 'n', 'd', 'o', 0 };
477 static Rune Lredo[] = { ' ', 'R', 'e', 'd', 'o', 0 };
478 static Rune Lget[] = { ' ', 'G', 'e', 't', 0 };
479 static Rune Lput[] = { ' ', 'P', 'u', 't', 0 };
480 static Rune Llook[] = { ' ', 'L', 'o', 'o', 'k', ' ', 0 };
481 static Rune Lpipe[] = { ' ', '|', 0 };
483 /* there are races that get us here with stuff in the tag cache, so we take extra care to sync it */
484 if(w->tag.ncache!=0 || w->tag.file->mod)
485 wincommit(w, &w->tag); /* check file name; also guarantees we can modify tag contents */
486 old = parsetag(w, 0, &i);
487 if(runeeq(old, i, w->body.file->name, w->body.file->nname) == FALSE){
488 textdelete(&w->tag, 0, i, TRUE);
489 textinsert(&w->tag, 0, w->body.file->name, w->body.file->nname, TRUE);
491 old = runemalloc(w->tag.file->b.nc+1);
492 bufread(&w->tag.file->b, 0, old, w->tag.file->b.nc);
493 old[w->tag.file->b.nc] = '\0';
496 /* compute the text for the whole tag, replacing current only if it differs */
497 new = runemalloc(w->body.file->nname+100);
499 if(w->body.file->nname != 0)
500 runemove(new, w->body.file->name, w->body.file->nname);
501 i += w->body.file->nname;
502 runemove(new+i, Ldelsnarf, 10);
505 if(w->body.needundo || w->body.file->delta.nc>0 || w->body.ncache){
506 runemove(new+i, Lundo, 5);
509 if(w->body.file->epsilon.nc > 0){
510 runemove(new+i, Lredo, 5);
513 dirty = w->body.file->nname && (w->body.ncache || w->body.file->seq!=w->putseq);
514 if(!w->isdir && dirty){
515 runemove(new+i, Lput, 4);
520 runemove(new+i, Lget, 4);
523 runemove(new+i, Lpipe, 2);
525 r = runestrchr(old, '|');
529 k = w->tag.file->b.nc;
530 if(w->body.file->seq == 0){
531 runemove(new+i, Llook, 6);
537 /* replace tag if the new one is different */
539 if(runeeq(new, i, old, k) == FALSE){
549 textdelete(&w->tag, j, k, TRUE);
550 textinsert(&w->tag, j, new+j, i-j, TRUE);
551 /* try to preserve user selection */
552 r = runestrchr(old, '|');
556 bar = (runestrchr(new, '|')-new)-bar;
564 w->tag.file->mod = FALSE;
565 n = w->tag.file->b.nc+w->tag.ncache;
570 textsetselect(&w->tag, w->tag.q0, w->tag.q1);
574 winresize(w, w->r, TRUE, TRUE);
586 for(i=0; i<f->ntext; i++){
588 if(v->col->safe || v->body.fr.maxlines>0)
594 wincommit(Window *w, Text *t)
603 for(i=0; i<f->ntext; i++)
604 textcommit(f->text[i], FALSE); /* no-op for t */
607 r = parsetag(w, 0, &i);
608 if(runeeq(r, i, w->body.file->name, w->body.file->nname) == FALSE){
610 filemark(w->body.file);
611 w->body.file->mod = TRUE;
620 winaddincl(Window *w, Rune *r, int n)
626 a = runetobyte(r, n);
631 rs = dirname(&w->body, r, n);
635 a = runetobyte(r, n);
639 r = runerealloc(r, n+1);
643 if((d->qid.type&QTDIR) == 0){
645 warning(nil, "%s: not a directory\n", a);
651 w->incl = realloc(w->incl, w->nincl*sizeof(Rune*));
652 memmove(w->incl+1, w->incl, (w->nincl-1)*sizeof(Rune*));
653 w->incl[0] = runemalloc(n+1);
654 runemove(w->incl[0], r, n);
659 warning(nil, "%s: %r\n", a);
666 winclean(Window *w, int conservative)
668 if(w->isscratch || w->isdir) /* don't whine if it's a guide file, error window, etc. */
670 if(!conservative && w->nopen[QWevent]>0)
673 if(w->body.file->nname)
674 warning(nil, "%.*S modified\n", w->body.file->nname, w->body.file->name);
676 if(w->body.file->b.nc < 100) /* don't whine if it's too small */
678 warning(nil, "unnamed file modified\n");
687 winctlprint(Window *w, char *buf, int fonts)
689 sprint(buf, "%11d %11d %11d %11d %11d ", w->id, w->tag.file->b.nc,
690 w->body.file->b.nc, w->isdir, w->dirty);
692 return smprint("%s%11d %q %11d %11d %11d ", buf, Dx(w->body.fr.r),
693 w->body.reffont->f->name, w->body.fr.maxtab, seqof(w, 1) != 0, seqof(w, 0) != 0);
698 winevent(Window *w, char *fmt, ...)
705 if(w->nopen[QWevent] == 0)
708 error("no window owner");
710 b = vsmprint(fmt, arg);
713 error("vsmprint failed");
715 w->events = erealloc(w->events, w->nevents+1+n);
716 w->events[w->nevents++] = w->owner;
717 memmove(w->events+w->nevents, b, n);