acme: accept expanded URLs in look

Just as look expands a click in /etc/passwd to the full name
(provided that file exists), it now expands a click in https://9fans.net/
to the full URL (provided the prefix is http:// or https://).
Probably more adjustment is needed.
This commit is contained in:
Russ Cox 2019-06-10 16:01:10 -04:00
parent 161742770e
commit 7a241631b2

View file

@ -514,6 +514,19 @@ dirname(Text *t, Rune *r, int n)
return tmp;
}
static int
texthas(Text *t, uint q0, Rune *r)
{
int i;
if((int)q0 < 0)
return FALSE;
for(i=0; r[i]; i++)
if(q0+i >= t->file->b.nc || textreadc(t, q0+i) != r[i])
return FALSE;
return TRUE;
}
int
expandfile(Text *t, uint q0, uint q1, Expand *e)
{
@ -522,12 +535,14 @@ expandfile(Text *t, uint q0, uint q1, Expand *e)
Rune *r, c;
Window *w;
Runestr rs;
Rune Lhttpcss[] = {'h', 't', 't', 'p', ':', '/', '/', 0};
Rune Lhttpscss[] = {'h', 't', 't', 'p', 's', ':', '/', '/', 0};
amax = q1;
if(q1 == q0){
colon = -1;
while(q1<t->file->b.nc && isfilec(c=textreadc(t, q1))){
if(c == ':'){
if(c == ':' && !texthas(t, q1-4, Lhttpcss) && !texthas(t, q1-5, Lhttpscss)){
colon = q1;
break;
}
@ -535,7 +550,7 @@ expandfile(Text *t, uint q0, uint q1, Expand *e)
}
while(q0>0 && (isfilec(c=textreadc(t, q0-1)) || isaddrc(c) || isregexc(c))){
q0--;
if(colon<0 && c==':')
if(colon<0 && c==':' && !texthas(t, q0-4, Lhttpcss) && !texthas(t, q0-5, Lhttpscss))
colon = q0;
}
/*
@ -565,8 +580,23 @@ expandfile(Text *t, uint q0, uint q1, Expand *e)
if(n == 0)
return FALSE;
/* see if it's a file name */
r = runemalloc(n);
r = runemalloc(n+1);
bufread(&t->file->b, q0, r, n);
r[n] = 0;
/* is it a URL? look for http:// and https:// prefix */
if(runestrncmp(r, Lhttpcss, 7) == 0 || runestrncmp(r, Lhttpscss, 8) == 0){
// Avoid capturing end-of-sentence punctuation.
if(r[n-1] == '.') {
e->q1--;
n--;
}
e->name = r;
e->nname = n;
e->u.at = t;
e->a0 = e->q1;
e->a1 = e->q1;
return TRUE;
}
/* first, does it have bad chars? */
nname = -1;
for(i=0; i<n; i++){