tacme: accept expanded URLs in look - plan9port - [fork] Plan 9 from user space
 (HTM) git clone git://src.adamsgaard.dk/plan9port
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit 7a241631b2097b1acda431ff059b771c5d1c391c
 (DIR) parent 161742770e63fec914b0290def3ac063ad3d2cf9
 (HTM) Author: Russ Cox <rsc@swtch.com>
       Date:   Mon, 10 Jun 2019 16:01:10 -0400
       
       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/
       tto the full URL (provided the prefix is http:// or https://).
       Probably more adjustment is needed.
       
       Diffstat:
         M src/cmd/acme/look.c                 |      48 +++++++++++++++++++++++++------
       
       1 file changed, 39 insertions(+), 9 deletions(-)
       ---
 (DIR) diff --git a/src/cmd/acme/look.c b/src/cmd/acme/look.c
       t@@ -30,7 +30,7 @@ plumbthread(void *v)
        
                USED(v);
                threadsetname("plumbproc");
       -        
       +
                /*
                 * Loop so that if plumber is restarted, acme need not be.
                 */
       t@@ -46,7 +46,7 @@ plumbthread(void *v)
                        }
                        plumbeditfid = fid;
                        plumbsendfid = plumbopenfid("send", OWRITE|OCEXEC);
       -        
       +
                        /*
                         * Relay messages.
                         */
       t@@ -432,9 +432,9 @@ includename(Text *t, Rune *r, int n)
                char buf[128];
                Rune Lsysinclude[] = { '/', 's', 'y', 's', '/', 'i', 'n', 'c', 'l', 'u', 'd', 'e', 0 };
                Rune Lusrinclude[] = { '/', 'u', 's', 'r', '/', 'i', 'n', 'c', 'l', 'u', 'd', 'e', 0 };
       -        Rune Lusrlocalinclude[] = { '/', 'u', 's', 'r', '/', 'l', 'o', 'c', 'a', 'l', 
       +        Rune Lusrlocalinclude[] = { '/', 'u', 's', 'r', '/', 'l', 'o', 'c', 'a', 'l',
                                '/', 'i', 'n', 'c', 'l', 'u', 'd', 'e', 0 };
       -        Rune Lusrlocalplan9include[] = { '/', 'u', 's', 'r', '/', 'l', 'o', 'c', 'a', 'l', 
       +        Rune Lusrlocalplan9include[] = { '/', 'u', 's', 'r', '/', 'l', 'o', 'c', 'a', 'l',
                                '/', 'p', 'l', 'a', 'n', '9', '/', 'i', 'n', 'c', 'l', 'u', 'd', 'e', 0 };
                Runestr file;
                int i;
       t@@ -443,7 +443,7 @@ includename(Text *t, Rune *r, int n)
                        sprint(buf, "/%s/include", objtype);
                        objdir = bytetorune(buf, &i);
                        objdir = runerealloc(objdir, i+1);
       -                objdir[i] = '\0';        
       +                objdir[i] = '\0';
                }
        
                w = t->w;
       t@@ -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)
        {
       t@@ -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;
                                }
       t@@ -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;
                        }
                        /*
       t@@ -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++){
       t@@ -728,7 +758,7 @@ openfile(Text *t, Expand *e)
                                /*
                                 * Unrooted path in new window.
                                 * This can happen if we type a pwd-relative path
       -                         * in the topmost tag or the column tags.  
       +                         * in the topmost tag or the column tags.
                                 * Most of the time plumber takes care of these,
                                 * but plumber might not be running or might not
                                 * be configured to accept plumbed directories.