Make the 'b' command work on prefixes. - sam - An updated version of the sam text editor.
 (HTM) git clone git://vernunftzentrum.de/sam.git
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) LICENSE
       ---
 (DIR) commit b1e08e78199d41e080169fc7716889012f70ff42
 (DIR) parent a162f12e7badc521f2faa61123f3f70bcc98db37
 (HTM) Author: Rob King <jking@deadpixi.com>
       Date:   Thu, 26 May 2016 00:10:34 -0500
       
       Make the 'b' command work on prefixes.
       
       Now, the 'b' command will find exact matches on file names or, if one
       cannot be found, the first file in the menu that has the argument as a
       prefix.
       
       Diffstat:
         sam/multi.c                         |      19 ++++++++++++++-----
         sam/sam.c                           |      27 +++++++++++++++++----------
         sam/sam.h                           |       2 +-
         sam/string.c                        |       3 +++
       
       4 files changed, 35 insertions(+), 16 deletions(-)
       ---
 (DIR) diff --git a/sam/multi.c b/sam/multi.c
       @@ -80,13 +80,22 @@ state(File *f, int cleandirty)
        }
        
        File *
       -lookfile(String *s)
       +lookfile(String *s, int doprefix)
        {
                int i;
       -    String *b;
       +    File *b = NULL;
       +    int l1 = 0;
        
       -        for(i=0; i<file.nused; i++)
       -                if(Strcmp(&file.filepptr[i]->name, s, NULL) == 0)
       +        for(i=0; i<file.nused; i++){
       +        int l2;
       +                if(Strcmp(&file.filepptr[i]->name, s, &l2) == 0)
                                return file.filepptr[i];
       -        return 0;
       +
       +        if (doprefix && l2 > l1 && l2 == s->n - 1){
       +            l1 = l2;
       +            b = file.filepptr[i];
       +        }
       +    }
       +
       +        return b;
        }
 (DIR) diff --git a/sam/sam.c b/sam/sam.c
       @@ -392,7 +392,7 @@ filename(File *f)
        {
                if(genc)
                        free(genc);
       -        genc = Strtoc(&genstr);
       +        genc = Strtoc(&f->name);
                dprint("%c%c%c %s\n", " '"[f->state==Dirty],
                        "-+"[f->rasp!=0], " ."[f==curfile], genc);
        }
       @@ -550,7 +550,7 @@ readflist(int readall, int delete)
                                break;
                        genstr.s[i] = 0;
                        t = tmprstr(genstr.s, i+1);
       -                f = lookfile(t);
       +                f = lookfile(t, 0);
                        if(delete){
                                if(f == 0)
                                        warn_S(Wfile, t);
       @@ -565,17 +565,24 @@ readflist(int readall, int delete)
        File *
        tofile(String *s)
        {
       -        File *f;
       +        File *f = NULL;
        
                if(s->s[0] != ' ')
                        error(Eblank);
       -        if(loadflist(s) == 0){
       -                f = lookfile(&genstr);        /* empty string ==> nameless file */
       -                if(f == 0)
       -                        error_s(Emenu, genc);
       -        }else if((f=readflist(FALSE, FALSE)) == 0)
       -                error_s(Emenu, genc);
       -        return current(f);
       +
       +    if (loadflist(s) == 0)
       +        f = lookfile(&genstr, 0);
       +
       +    if (f == NULL)
       +        f = lookfile(&genstr, 1);
       +
       +    if (f == NULL)
       +        f = readflist(FALSE, FALSE);
       +
       +    if (f == NULL)
       +        error_s(Emenu, genc);
       +
       +    return current(f);
        }
        
        File *
 (DIR) diff --git a/sam/sam.h b/sam/sam.h
       @@ -273,7 +273,7 @@ void        inslist(List*, int, long);
        Address        lineaddr(Posn, Address, int);
        void        listfree(List*);
        void        load(File*);
       -File        *lookfile(String*);
       +File        *lookfile(String*, int);
        void        lookorigin(File*, Posn, Posn);
        int        lookup(int);
        void        move(File*, Address);
 (DIR) diff --git a/sam/string.c b/sam/string.c
       @@ -104,6 +104,9 @@ Strcmp(String *a, String *b, int *l)
        {
                int i, c;
        
       +    if (l)
       +        *l = 0;
       +
                for(i=0; i<a->n && i<b->n; i++){
                        if(c = (a->s[i] - b->s[i]))        /* assign = */
                                return c;