tacme: drop trailing spaces during Put of auto-indent window - 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 43f1873709d39405b55f676ef21c42065cc1408d
 (DIR) parent 2607cc565ee3d5facb8949e9acfed35c8ae300c9
 (HTM) Author: Russ Cox <rsc@swtch.com>
       Date:   Mon, 30 Jul 2018 21:37:37 -0400
       
       acme: drop trailing spaces during Put of auto-indent window
       
       Auto-indent mode leaves trailing spaces on blank lines
       as you type past them, so silently elide them from the
       window content as it gets written back to disk.
       
       Another option would be to remove them from the
       window entirely during Put, but they're actually nice
       tto have while editing, and to date Put has never
       modified the window content.
       
       Diffstat:
         M src/cmd/acme/exec.c                 |      41 ++++++++++++++++++++++++++++---
       
       1 file changed, 38 insertions(+), 3 deletions(-)
       ---
 (DIR) diff --git a/src/cmd/acme/exec.c b/src/cmd/acme/exec.c
       t@@ -690,12 +690,44 @@ checksha1(char *name, File *f, Dir *d)
                        f->qidpath = d->qid.path;
                        f->mtime = d->mtime;
                }
       -}        
       +}
       +
       +static uint
       +trimspaces(Rune *r, uint *np, int eof)
       +{
       +        uint i, w, nonspace, n;
       +        Rune c;
       +
       +        nonspace = 0;
       +        w = 0;
       +        n = *np;        
       +        for(i=0; i<n; i++) {
       +                c = r[i];
       +                if(c == '\n')
       +                        w = nonspace;
       +                r[w++] = c;
       +                if(c != ' ' && c != '\t')
       +                        nonspace = w;
       +        }
       +        if(nonspace > 0 && nonspace < w) {
       +                // Trailing spaces at end of buffer.
       +                // Tell caller to reread them with what follows,
       +                // so we can determine whether they need trimming.
       +                // Unless the trailing spaces are the entire buffer,
       +                // in which case let them through to avoid an infinite loop
       +                // if an entire buffer fills with spaces.
       +                // At EOF, just consume the spaces.
       +                if(!eof)
       +                        *np = n - (w - nonspace);
       +                w = nonspace;
       +        }
       +        return w;
       +}
        
        void
        putfile(File *f, int q0, int q1, Rune *namer, int nname)
        {
       -        uint n, m;
       +        uint n, nn, m;
                Rune *r;
                Biobuf *b;
                char *s, *name;
       t@@ -750,7 +782,10 @@ putfile(File *f, int q0, int q1, Rune *namer, int nname)
                        if(n > BUFSIZE/UTFmax)
                                n = BUFSIZE/UTFmax;
                        bufread(&f->b, q, r, n);
       -                m = snprint(s, BUFSIZE+1, "%.*S", n, r);
       +                nn = n;
       +                if(w->autoindent)
       +                        nn = trimspaces(r, &n, q+n==q1);
       +                m = snprint(s, BUFSIZE+1, "%.*S", nn, r);
                        sha1((uchar*)s, m, nil, h);
                        if(Bwrite(b, s, m) != m){
                                warning(nil, "can't write file %s: %r\n", name);