Add $foo window names back to acme, enabled with -'$'.

See the comment in wind.c about why this isn't the right
solution.
This commit is contained in:
rsc 2005-03-14 20:53:10 +00:00
parent 7551b2ec8b
commit d96da29b37
4 changed files with 209 additions and 7 deletions

View file

@ -69,6 +69,9 @@ threadmain(int argc, char *argv[])
loadfile = nil;
ARGBEGIN{
case '$':
dodollarsigns = TRUE;
break;
case 'D':
{extern int _threaddebuglevel;
_threaddebuglevel = ~0;

View file

@ -536,6 +536,7 @@ int editing;
int erroutfd;
int messagesize; /* negotiated in 9P version setup */
int globalautoindent;
int dodollarsigns;
enum
{

View file

@ -96,6 +96,9 @@ void startplumbing(void);
Runestr runestr(Rune*, uint);
Range range(int, int);
int expandenv(Rune**, uint*);
int abbrevenv(Rune**, uint*);
#define runemalloc(a) (Rune*)emalloc((a)*sizeof(Rune))
#define runerealloc(a, b) (Rune*)erealloc((a), (b)*sizeof(Rune))
#define runemove(a, b, c) memmove((a), (b), (c)*sizeof(Rune))

View file

@ -320,8 +320,8 @@ wincleartag(Window *w)
void
winsettag1(Window *w)
{
int i, j, k, n, bar, dirty;
Rune *new, *old, *r;
int bar, dirty, i, j, k, n, ntagname;
Rune *new, *old, *r, *tagname;
Image *b;
uint q0, q1;
Rectangle br;
@ -342,18 +342,27 @@ winsettag1(Window *w)
for(i=0; i<w->tag.file->b.nc; i++)
if(old[i]==' ' || old[i]=='\t')
break;
if(runeeq(old, i, w->body.file->name, w->body.file->nname) == FALSE){
/* make sure the file name is set correctly in the tag */
ntagname = w->body.file->nname;
tagname = runemalloc(ntagname);
runemove(tagname, w->body.file->name, ntagname);
abbrevenv(&tagname, &ntagname);
if(runeeq(old, i, tagname, ntagname) == FALSE){
textdelete(&w->tag, 0, i, TRUE);
textinsert(&w->tag, 0, w->body.file->name, w->body.file->nname, TRUE);
textinsert(&w->tag, 0, tagname, ntagname, TRUE);
free(old);
old = runemalloc(w->tag.file->b.nc+1);
bufread(&w->tag.file->b, 0, old, w->tag.file->b.nc);
old[w->tag.file->b.nc] = '\0';
}
new = runemalloc(w->body.file->nname+100);
/* compute the text for the whole tag, replacing current only if it differs */
new = runemalloc(ntagname+100);
i = 0;
runemove(new+i, w->body.file->name, w->body.file->nname);
i += w->body.file->nname;
runemove(new+i, tagname, ntagname);
i += ntagname;
runemove(new+i, Ldelsnarf, 10);
i += 10;
if(w->filemenu){
@ -391,6 +400,8 @@ winsettag1(Window *w)
if(runestrlen(new) != i)
fprint(2, "s '%S' len not %d\n", new, i);
assert(i==runestrlen(new));
/* replace tag if the new one is different */
if(runeeq(new, i, old, k) == FALSE){
n = k;
if(n > i)
@ -413,6 +424,7 @@ winsettag1(Window *w)
}
}
}
free(tagname);
free(old);
free(new);
w->tag.file->mod = FALSE;
@ -465,6 +477,7 @@ wincommit(Window *w, Text *t)
for(i=0; i<w->tag.file->b.nc; i++)
if(r[i]==' ' || r[i]=='\t')
break;
expandenv(&r, &i);
if(runeeq(r, i, w->body.file->name, w->body.file->nname) == FALSE){
seq++;
filemark(w->body.file);
@ -583,3 +596,185 @@ winevent(Window *w, char *fmt, ...)
sendp(x->c, nil);
}
}
/*
* This is here as a first stab at something.
* Run acme with the -'$' flag to enable it.
*
* This code isn't quite right, in that it doesn't play well with
* the plumber and with other applications. For example:
*
* If the window tag is $home/bin and you execute script, then acme runs
* script in $home/bin, via the shell, so everything is fine. If you do
* execute "echo $home", it too goes to the shell so you see the value
* of $home. And if you right-click on script, then acme plumbs "script"
* in the directory "/home/you/bin", so that works, but if you right-click
* on "$home/bin/script", then what? It's not correct to expand in acme
* since what you're plumbing might be a price tag for all we know. So the
* plumber has to expand it, but in order to do that the plumber should
* probably publish (and allow users to change) the set of variables it is
* using in expansions.
*
* Rob has suggested that a better solution is to make tag lines expand
* automatically to fit the necessary number of lines.
*
* The best solution, of course, is to use nice short path names, but this
* is not always possible.
*/
int
expandenv(Rune **rp, uint *np)
{
char *s, *t;
Rune *p, *r, *q;
uint n, pref;
int nb, nr, slash;
Runestr rs;
if(!dodollarsigns)
return FALSE;
r = *rp;
n = *np;
if(n == 0 || r[0] != '$')
return FALSE;
for(p=r+1; *p && *p != '/'; p++)
;
pref = p-r;
s = runetobyte(r+1, pref-1);
if((t = getenv(s)) == nil){
free(s);
return FALSE;
}
q = runemalloc(utflen(t)+(n-pref));
cvttorunes(t, strlen(t), q, &nb, &nr, nil);
assert(nr==utflen(t));
runemove(q+nr, p, n-pref);
free(r);
rs = runestr(q, nr+(n-pref));
slash = rs.nr>0 && q[rs.nr-1] == '/';
rs = cleanrname(rs);
if(slash){
rs.r = runerealloc(rs.r, rs.nr+1);
rs.r[rs.nr++] = '/';
}
*rp = rs.r;
*np = rs.nr;
free(t);
return TRUE;
}
extern char **environ;
Rune **runeenv;
/*
* Weird sort order -- shorter names first,
* prefer lowercase over uppercase ($home over $HOME),
* then do normal string comparison.
*/
int
runeenvcmp(const void *va, const void *vb)
{
Rune *a, *b;
int na, nb;
a = *(Rune**)va;
b = *(Rune**)vb;
na = runestrchr(a, '=') - a;
nb = runestrchr(b, '=') - b;
if(na < nb)
return -1;
if(nb > na)
return 1;
if(na == 0)
return 0;
if(islowerrune(a[0]) && !islowerrune(b[0]))
return -1;
if(islowerrune(b[0]) && !islowerrune(a[0]))
return 1;
return runestrncmp(a, b, na);
}
void
mkruneenv(void)
{
int i, bad, n, nr;
char *p;
Rune *r, *q;
n = 0;
for(i=0; environ[i]; i++){
/*
* Ignore some pollution.
*/
if(environ[i][0] == '_')
continue;
if(strncmp(environ[i], "PWD=", 4) == 0)
continue;
if(strncmp(environ[i], "OLDPWD=", 7) == 0)
continue;
/*
* Must be a rooted path.
*/
if((p = strchr(environ[i], '=')) == nil || *(p+1) != '/')
continue;
/*
* Only use the ones that we accept in look - all isfilec
*/
bad = 0;
r = bytetorune(environ[i], &nr);
for(q=r; *q != '='; q++)
if(!isfilec(*q)){
free(r);
bad = 1;
break;
}
if(!bad){
runeenv = erealloc(runeenv, (n+1)*sizeof(runeenv[0]));
runeenv[n++] = r;
}
}
runeenv = erealloc(runeenv, (n+1)*sizeof(runeenv[0]));
runeenv[n] = nil;
qsort(runeenv, n, sizeof(runeenv[0]), runeenvcmp);
}
int
abbrevenv(Rune **rp, uint *np)
{
int i, len, alen;
Rune *r, *p, *q;
uint n;
if(!dodollarsigns)
return FALSE;
r = *rp;
n = *np;
if(n == 0 || r[0] != '/')
return FALSE;
if(runeenv == nil)
mkruneenv();
for(i=0; runeenv[i]; i++){
p = runestrchr(runeenv[i], '=')+1;
len = runestrlen(p);
if(len <= n && (r[len]==0 || r[len]=='/') && runeeq(r, len, p, len)==TRUE){
alen = (p-1) - runeenv[i];
q = runemalloc(1+alen+n-len);
q[0] = '$';
runemove(q+1, runeenv[i], alen);
runemove(q+1+alen, r+len, n-len);
free(r);
*rp = q;
*np = 1+alen+n-len;
return TRUE;
}
}
return FALSE;
}