if(!w->tagexpand) {
/* use just as many lines as needed to show the Del */
n = tagrunepos(w, delcmd);
if(n < 0)
return 1;
p = subpt(frptofchar(&w->tag, n), w->tag.r.min);
return 1 + p.y / w->tag.font->height;
}
/* can't use more than we have */
if(w->tag.nlines >= w->tag.maxlines)
return w->tag.maxlines;
/* if tag ends with \n, include empty line at end for typing */
n = w->tag.nlines;
if(w->tag.file->nc > 0){
bufread(w->tag.file, w->tag.file->nc-1, &rune, 1);
if(rune == '\n')
n++;
}
if(n == 0)
n = 1;
return n;
}
int
winresize(Window *w, Rectangle r, int safe)
{
int oy, mouseintag, mouseinbody;
Point p;
Rectangle r1;
int y;
Image *b;
Rectangle br;
/* If mouse is in tag, pull up as tag closes. */
if(mouseintag && !ptinrect(mouse->xy, w->tag.all)){
p = mouse->xy;
p.y = w->tag.all.max.y-3;
moveto(mousectl, p);
}
/* If mouse is in body, push down as tag expands. */
if(mouseinbody && ptinrect(mouse->xy, w->tag.all)){
p = mouse->xy;
p.y = w->tag.all.max.y+3;
moveto(mousectl, p);
}
void
winlock(Window *w, int owner)
{
int i;
File *f;
f = w->body.file;
for(i=0; i<f->ntext; i++)
winlock1(f->text[i]->w, owner);
}
void
winunlock(Window *w)
{
int i;
File *f;
/*
* subtle: loop runs backwards to avoid tripping over
* winclose indirectly editing f->text and freeing f
* on the last iteration of the loop.
*/
f = w->body.file;
for(i=f->ntext-1; i>=0; i--){
w = f->text[i]->w;
w->owner = 0;
qunlock(w);
winclose(w);
}
}
/* w must be committed */
n = w->tag.file->nc;
r = runemalloc(n);
bufread(w->tag.file, 0, r, n);
for(i=0; i<n; i++)
if(r[i]==' ' || r[i]=='\t')
break;
for(; i<n; i++)
if(r[i] == '|')
break;
if(i == n)
return;
i++;
textdelete(&w->tag, i, n, TRUE);
free(r);
w->tag.file->mod = FALSE;
if(w->tag.q0 > i)
w->tag.q0 = i;
if(w->tag.q1 > i)
w->tag.q1 = i;
textsetselect(&w->tag, w->tag.q0, w->tag.q1);
}
void
winsettag1(Window *w)
{
int i, j, k, n, bar, dirty;
Rune *new, *old, *r;
Image *b;
uint q0, q1;
Rectangle br;
/* there are races that get us here with stuff in the tag cache, so we take extra care to sync it */
if(w->tag.ncache!=0 || w->tag.file->mod)
wincommit(w, &w->tag); /* check file name; also guarantees we can modify tag contents */
old = runemalloc(w->tag.file->nc+1);
bufread(w->tag.file, 0, old, w->tag.file->nc);
old[w->tag.file->nc] = '\0';
for(i=0; i<w->tag.file->nc; i++)
if(old[i]==' ' || old[i]=='\t')
break;
if(runeeq(old, i, w->body.file->name, w->body.file->nname) == FALSE){
textdelete(&w->tag, 0, i, TRUE);
textinsert(&w->tag, 0, w->body.file->name, w->body.file->nname, TRUE);
free(old);
old = runemalloc(w->tag.file->nc+1);
bufread(w->tag.file, 0, old, w->tag.file->nc);
old[w->tag.file->nc] = '\0';
w->tagsafe = FALSE;
}
new = runemalloc(w->body.file->nname+100);
i = 0;
runemove(new+i, w->body.file->name, w->body.file->nname);
i += w->body.file->nname;
runemove(new+i, L" Del Snarf", 10);
i += 10;
if(w->filemenu){
if(w->body.file->delta.nc>0 || w->body.ncache){
runemove(new+i, L" Undo", 5);
i += 5;
}
if(w->body.file->epsilon.nc > 0){
runemove(new+i, L" Redo", 5);
i += 5;
}
dirty = w->body.file->nname && (w->body.ncache || w->body.file->seq!=w->putseq);
if(!w->isdir && dirty){
runemove(new+i, L" Put", 4);
i += 4;
}
}
if(w->isdir){
runemove(new+i, L" Get", 4);
i += 4;
}
runemove(new+i, L" |", 2);
i += 2;
r = runestrchr(old, '|');
if(r)
k = r-old+1;
else{
k = w->tag.file->nc;
if(w->body.file->seq == 0){
runemove(new+i, L" Look ", 6);
i += 6;
}
}
new[i] = 0;
/* replace tag if the new one is different */
if(runeeq(new, i, old, k) == FALSE){
n = k;
if(n > i)
n = i;
for(j=0; j<n; j++)
if(old[j] != new[j])
break;
q0 = w->tag.q0;
q1 = w->tag.q1;
textdelete(&w->tag, j, k, TRUE);
textinsert(&w->tag, j, new+j, i-j, TRUE);
/* try to preserve user selection */
r = runestrchr(old, '|');
if(r){
bar = r-old;
if(q0 > bar){
bar = (runestrchr(new, '|')-new)-bar;
w->tag.q0 = q0+bar;
w->tag.q1 = q1+bar;
}
}
w->tagsafe = FALSE;
}
free(old);
free(new);
w->tag.file->mod = FALSE;
n = w->tag.file->nc+w->tag.ncache;
if(w->tag.q0 > n)
w->tag.q0 = n;
if(w->tag.q1 > n)
w->tag.q1 = n;
textsetselect(&w->tag, w->tag.q0, w->tag.q1);
b = button;
if(!w->isdir && !w->isscratch && (w->body.file->mod || w->body.ncache))
b = modbutton;
br.min = w->tag.scrollr.min;
br.max.x = br.min.x + Dx(b->r);
br.max.y = br.min.y + Dy(b->r);
draw(screen, br, b, nil, b->r.min);
if(w->tagsafe == FALSE)
winresize(w, w->r, TRUE);
}
void
winsettag(Window *w)
{
int i;
File *f;
Window *v;
f = w->body.file;
for(i=0; i<f->ntext; i++){
v = f->text[i]->w;
if(v->col->safe || v->body.maxlines>0)
winsettag1(v);
}
}
void
wincommit(Window *w, Text *t)
{
Rune *r;
int i;
File *f;